]> Git Repo - qemu.git/blob - target-ppc/translate.c
target-ppc: Move To/From VSR Instructions
[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
26 #include "helper.h"
27 #define GEN_HELPER 1
28 #include "helper.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 /* Destination */
391 EXTRACT_HELPER(rD, 21, 5);
392 /* Source */
393 EXTRACT_HELPER(rS, 21, 5);
394 /* First operand */
395 EXTRACT_HELPER(rA, 16, 5);
396 /* Second operand */
397 EXTRACT_HELPER(rB, 11, 5);
398 /* Third operand */
399 EXTRACT_HELPER(rC, 6, 5);
400 /***                               Get CRn                                 ***/
401 EXTRACT_HELPER(crfD, 23, 3);
402 EXTRACT_HELPER(crfS, 18, 3);
403 EXTRACT_HELPER(crbD, 21, 5);
404 EXTRACT_HELPER(crbA, 16, 5);
405 EXTRACT_HELPER(crbB, 11, 5);
406 /* SPR / TBL */
407 EXTRACT_HELPER(_SPR, 11, 10);
408 static inline uint32_t SPR(uint32_t opcode)
409 {
410     uint32_t sprn = _SPR(opcode);
411
412     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
413 }
414 /***                              Get constants                            ***/
415 EXTRACT_HELPER(IMM, 12, 8);
416 /* 16 bits signed immediate value */
417 EXTRACT_SHELPER(SIMM, 0, 16);
418 /* 16 bits unsigned immediate value */
419 EXTRACT_HELPER(UIMM, 0, 16);
420 /* 5 bits signed immediate value */
421 EXTRACT_HELPER(SIMM5, 16, 5);
422 /* 5 bits signed immediate value */
423 EXTRACT_HELPER(UIMM5, 16, 5);
424 /* Bit count */
425 EXTRACT_HELPER(NB, 11, 5);
426 /* Shift count */
427 EXTRACT_HELPER(SH, 11, 5);
428 /* Vector shift count */
429 EXTRACT_HELPER(VSH, 6, 4);
430 /* Mask start */
431 EXTRACT_HELPER(MB, 6, 5);
432 /* Mask end */
433 EXTRACT_HELPER(ME, 1, 5);
434 /* Trap operand */
435 EXTRACT_HELPER(TO, 21, 5);
436
437 EXTRACT_HELPER(CRM, 12, 8);
438 EXTRACT_HELPER(SR, 16, 4);
439
440 /* mtfsf/mtfsfi */
441 EXTRACT_HELPER(FPBF, 23, 3);
442 EXTRACT_HELPER(FPIMM, 12, 4);
443 EXTRACT_HELPER(FPL, 25, 1);
444 EXTRACT_HELPER(FPFLM, 17, 8);
445 EXTRACT_HELPER(FPW, 16, 1);
446
447 /***                            Jump target decoding                       ***/
448 /* Displacement */
449 EXTRACT_SHELPER(d, 0, 16);
450 /* Immediate address */
451 static inline target_ulong LI(uint32_t opcode)
452 {
453     return (opcode >> 0) & 0x03FFFFFC;
454 }
455
456 static inline uint32_t BD(uint32_t opcode)
457 {
458     return (opcode >> 0) & 0xFFFC;
459 }
460
461 EXTRACT_HELPER(BO, 21, 5);
462 EXTRACT_HELPER(BI, 16, 5);
463 /* Absolute/relative address */
464 EXTRACT_HELPER(AA, 1, 1);
465 /* Link */
466 EXTRACT_HELPER(LK, 0, 1);
467
468 /* Create a mask between <start> and <end> bits */
469 static inline target_ulong MASK(uint32_t start, uint32_t end)
470 {
471     target_ulong ret;
472
473 #if defined(TARGET_PPC64)
474     if (likely(start == 0)) {
475         ret = UINT64_MAX << (63 - end);
476     } else if (likely(end == 63)) {
477         ret = UINT64_MAX >> start;
478     }
479 #else
480     if (likely(start == 0)) {
481         ret = UINT32_MAX << (31  - end);
482     } else if (likely(end == 31)) {
483         ret = UINT32_MAX >> start;
484     }
485 #endif
486     else {
487         ret = (((target_ulong)(-1ULL)) >> (start)) ^
488             (((target_ulong)(-1ULL) >> (end)) >> 1);
489         if (unlikely(start > end))
490             return ~ret;
491     }
492
493     return ret;
494 }
495
496 EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
497 EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
498 EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
499 EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
500 EXTRACT_HELPER_SPLIT(xC, 3, 1,  6, 5);
501 EXTRACT_HELPER(DM, 8, 2);
502 EXTRACT_HELPER(UIM, 16, 2);
503 EXTRACT_HELPER(SHW, 8, 2);
504 /*****************************************************************************/
505 /* PowerPC instructions table                                                */
506
507 #if defined(DO_PPC_STATISTICS)
508 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
509 {                                                                             \
510     .opc1 = op1,                                                              \
511     .opc2 = op2,                                                              \
512     .opc3 = op3,                                                              \
513     .pad  = { 0, },                                                           \
514     .handler = {                                                              \
515         .inval1  = invl,                                                      \
516         .type = _typ,                                                         \
517         .type2 = _typ2,                                                       \
518         .handler = &gen_##name,                                               \
519         .oname = stringify(name),                                             \
520     },                                                                        \
521     .oname = stringify(name),                                                 \
522 }
523 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
524 {                                                                             \
525     .opc1 = op1,                                                              \
526     .opc2 = op2,                                                              \
527     .opc3 = op3,                                                              \
528     .pad  = { 0, },                                                           \
529     .handler = {                                                              \
530         .inval1  = invl1,                                                     \
531         .inval2  = invl2,                                                     \
532         .type = _typ,                                                         \
533         .type2 = _typ2,                                                       \
534         .handler = &gen_##name,                                               \
535         .oname = stringify(name),                                             \
536     },                                                                        \
537     .oname = stringify(name),                                                 \
538 }
539 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
540 {                                                                             \
541     .opc1 = op1,                                                              \
542     .opc2 = op2,                                                              \
543     .opc3 = op3,                                                              \
544     .pad  = { 0, },                                                           \
545     .handler = {                                                              \
546         .inval1  = invl,                                                      \
547         .type = _typ,                                                         \
548         .type2 = _typ2,                                                       \
549         .handler = &gen_##name,                                               \
550         .oname = onam,                                                        \
551     },                                                                        \
552     .oname = onam,                                                            \
553 }
554 #else
555 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
556 {                                                                             \
557     .opc1 = op1,                                                              \
558     .opc2 = op2,                                                              \
559     .opc3 = op3,                                                              \
560     .pad  = { 0, },                                                           \
561     .handler = {                                                              \
562         .inval1  = invl,                                                      \
563         .type = _typ,                                                         \
564         .type2 = _typ2,                                                       \
565         .handler = &gen_##name,                                               \
566     },                                                                        \
567     .oname = stringify(name),                                                 \
568 }
569 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
570 {                                                                             \
571     .opc1 = op1,                                                              \
572     .opc2 = op2,                                                              \
573     .opc3 = op3,                                                              \
574     .pad  = { 0, },                                                           \
575     .handler = {                                                              \
576         .inval1  = invl1,                                                     \
577         .inval2  = invl2,                                                     \
578         .type = _typ,                                                         \
579         .type2 = _typ2,                                                       \
580         .handler = &gen_##name,                                               \
581     },                                                                        \
582     .oname = stringify(name),                                                 \
583 }
584 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
585 {                                                                             \
586     .opc1 = op1,                                                              \
587     .opc2 = op2,                                                              \
588     .opc3 = op3,                                                              \
589     .pad  = { 0, },                                                           \
590     .handler = {                                                              \
591         .inval1  = invl,                                                      \
592         .type = _typ,                                                         \
593         .type2 = _typ2,                                                       \
594         .handler = &gen_##name,                                               \
595     },                                                                        \
596     .oname = onam,                                                            \
597 }
598 #endif
599
600 /* SPR load/store helpers */
601 static inline void gen_load_spr(TCGv t, int reg)
602 {
603     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
604 }
605
606 static inline void gen_store_spr(int reg, TCGv t)
607 {
608     tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
609 }
610
611 /* Invalid instruction */
612 static void gen_invalid(DisasContext *ctx)
613 {
614     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
615 }
616
617 static opc_handler_t invalid_handler = {
618     .inval1  = 0xFFFFFFFF,
619     .inval2  = 0xFFFFFFFF,
620     .type    = PPC_NONE,
621     .type2   = PPC_NONE,
622     .handler = gen_invalid,
623 };
624
625 /***                           Integer comparison                          ***/
626
627 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
628 {
629     TCGv t0 = tcg_temp_new();
630     TCGv_i32 t1 = tcg_temp_new_i32();
631
632     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
633
634     tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
635     tcg_gen_trunc_tl_i32(t1, t0);
636     tcg_gen_shli_i32(t1, t1, CRF_LT);
637     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
638
639     tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
640     tcg_gen_trunc_tl_i32(t1, t0);
641     tcg_gen_shli_i32(t1, t1, CRF_GT);
642     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
643
644     tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
645     tcg_gen_trunc_tl_i32(t1, t0);
646     tcg_gen_shli_i32(t1, t1, CRF_EQ);
647     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
648
649     tcg_temp_free(t0);
650     tcg_temp_free_i32(t1);
651 }
652
653 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
654 {
655     TCGv t0 = tcg_const_tl(arg1);
656     gen_op_cmp(arg0, t0, s, crf);
657     tcg_temp_free(t0);
658 }
659
660 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
661 {
662     TCGv t0, t1;
663     t0 = tcg_temp_new();
664     t1 = tcg_temp_new();
665     if (s) {
666         tcg_gen_ext32s_tl(t0, arg0);
667         tcg_gen_ext32s_tl(t1, arg1);
668     } else {
669         tcg_gen_ext32u_tl(t0, arg0);
670         tcg_gen_ext32u_tl(t1, arg1);
671     }
672     gen_op_cmp(t0, t1, s, crf);
673     tcg_temp_free(t1);
674     tcg_temp_free(t0);
675 }
676
677 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
678 {
679     TCGv t0 = tcg_const_tl(arg1);
680     gen_op_cmp32(arg0, t0, s, crf);
681     tcg_temp_free(t0);
682 }
683
684 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
685 {
686     if (NARROW_MODE(ctx)) {
687         gen_op_cmpi32(reg, 0, 1, 0);
688     } else {
689         gen_op_cmpi(reg, 0, 1, 0);
690     }
691 }
692
693 /* cmp */
694 static void gen_cmp(DisasContext *ctx)
695 {
696     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
697         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
698                    1, crfD(ctx->opcode));
699     } else {
700         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
701                      1, crfD(ctx->opcode));
702     }
703 }
704
705 /* cmpi */
706 static void gen_cmpi(DisasContext *ctx)
707 {
708     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
709         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
710                     1, crfD(ctx->opcode));
711     } else {
712         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
713                       1, crfD(ctx->opcode));
714     }
715 }
716
717 /* cmpl */
718 static void gen_cmpl(DisasContext *ctx)
719 {
720     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
721         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
722                    0, crfD(ctx->opcode));
723     } else {
724         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
725                      0, crfD(ctx->opcode));
726     }
727 }
728
729 /* cmpli */
730 static void gen_cmpli(DisasContext *ctx)
731 {
732     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
733         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
734                     0, crfD(ctx->opcode));
735     } else {
736         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
737                       0, crfD(ctx->opcode));
738     }
739 }
740
741 /* isel (PowerPC 2.03 specification) */
742 static void gen_isel(DisasContext *ctx)
743 {
744     int l1, l2;
745     uint32_t bi = rC(ctx->opcode);
746     uint32_t mask;
747     TCGv_i32 t0;
748
749     l1 = gen_new_label();
750     l2 = gen_new_label();
751
752     mask = 1 << (3 - (bi & 0x03));
753     t0 = tcg_temp_new_i32();
754     tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
755     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
756     if (rA(ctx->opcode) == 0)
757         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
758     else
759         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
760     tcg_gen_br(l2);
761     gen_set_label(l1);
762     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
763     gen_set_label(l2);
764     tcg_temp_free_i32(t0);
765 }
766
767 /* cmpb: PowerPC 2.05 specification */
768 static void gen_cmpb(DisasContext *ctx)
769 {
770     gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
771                     cpu_gpr[rB(ctx->opcode)]);
772 }
773
774 /***                           Integer arithmetic                          ***/
775
776 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
777                                            TCGv arg1, TCGv arg2, int sub)
778 {
779     TCGv t0 = tcg_temp_new();
780
781     tcg_gen_xor_tl(cpu_ov, arg0, arg2);
782     tcg_gen_xor_tl(t0, arg1, arg2);
783     if (sub) {
784         tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
785     } else {
786         tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
787     }
788     tcg_temp_free(t0);
789     if (NARROW_MODE(ctx)) {
790         tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
791     }
792     tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
793     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
794 }
795
796 /* Common add function */
797 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
798                                     TCGv arg2, bool add_ca, bool compute_ca,
799                                     bool compute_ov, bool compute_rc0)
800 {
801     TCGv t0 = ret;
802
803     if (compute_ca || compute_ov) {
804         t0 = tcg_temp_new();
805     }
806
807     if (compute_ca) {
808         if (NARROW_MODE(ctx)) {
809             /* Caution: a non-obvious corner case of the spec is that we
810                must produce the *entire* 64-bit addition, but produce the
811                carry into bit 32.  */
812             TCGv t1 = tcg_temp_new();
813             tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
814             tcg_gen_add_tl(t0, arg1, arg2);
815             if (add_ca) {
816                 tcg_gen_add_tl(t0, t0, cpu_ca);
817             }
818             tcg_gen_xor_tl(cpu_ca, t0, t1);        /* bits changed w/ carry */
819             tcg_temp_free(t1);
820             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);   /* extract bit 32 */
821             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
822         } else {
823             TCGv zero = tcg_const_tl(0);
824             if (add_ca) {
825                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
826                 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
827             } else {
828                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
829             }
830             tcg_temp_free(zero);
831         }
832     } else {
833         tcg_gen_add_tl(t0, arg1, arg2);
834         if (add_ca) {
835             tcg_gen_add_tl(t0, t0, cpu_ca);
836         }
837     }
838
839     if (compute_ov) {
840         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
841     }
842     if (unlikely(compute_rc0)) {
843         gen_set_Rc0(ctx, t0);
844     }
845
846     if (!TCGV_EQUAL(t0, ret)) {
847         tcg_gen_mov_tl(ret, t0);
848         tcg_temp_free(t0);
849     }
850 }
851 /* Add functions with two operands */
852 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
853 static void glue(gen_, name)(DisasContext *ctx)                               \
854 {                                                                             \
855     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
856                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
857                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
858 }
859 /* Add functions with one operand and one immediate */
860 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
861                                 add_ca, compute_ca, compute_ov)               \
862 static void glue(gen_, name)(DisasContext *ctx)                               \
863 {                                                                             \
864     TCGv t0 = tcg_const_tl(const_val);                                        \
865     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
866                      cpu_gpr[rA(ctx->opcode)], t0,                            \
867                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
868     tcg_temp_free(t0);                                                        \
869 }
870
871 /* add  add.  addo  addo. */
872 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
873 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
874 /* addc  addc.  addco  addco. */
875 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
876 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
877 /* adde  adde.  addeo  addeo. */
878 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
879 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
880 /* addme  addme.  addmeo  addmeo.  */
881 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
882 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
883 /* addze  addze.  addzeo  addzeo.*/
884 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
885 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
886 /* addi */
887 static void gen_addi(DisasContext *ctx)
888 {
889     target_long simm = SIMM(ctx->opcode);
890
891     if (rA(ctx->opcode) == 0) {
892         /* li case */
893         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
894     } else {
895         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
896                         cpu_gpr[rA(ctx->opcode)], simm);
897     }
898 }
899 /* addic  addic.*/
900 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
901 {
902     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
903     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
904                      c, 0, 1, 0, compute_rc0);
905     tcg_temp_free(c);
906 }
907
908 static void gen_addic(DisasContext *ctx)
909 {
910     gen_op_addic(ctx, 0);
911 }
912
913 static void gen_addic_(DisasContext *ctx)
914 {
915     gen_op_addic(ctx, 1);
916 }
917
918 /* addis */
919 static void gen_addis(DisasContext *ctx)
920 {
921     target_long simm = SIMM(ctx->opcode);
922
923     if (rA(ctx->opcode) == 0) {
924         /* lis case */
925         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
926     } else {
927         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
928                         cpu_gpr[rA(ctx->opcode)], simm << 16);
929     }
930 }
931
932 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
933                                      TCGv arg2, int sign, int compute_ov)
934 {
935     int l1 = gen_new_label();
936     int l2 = gen_new_label();
937     TCGv_i32 t0 = tcg_temp_local_new_i32();
938     TCGv_i32 t1 = tcg_temp_local_new_i32();
939
940     tcg_gen_trunc_tl_i32(t0, arg1);
941     tcg_gen_trunc_tl_i32(t1, arg2);
942     tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
943     if (sign) {
944         int l3 = gen_new_label();
945         tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
946         tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
947         gen_set_label(l3);
948         tcg_gen_div_i32(t0, t0, t1);
949     } else {
950         tcg_gen_divu_i32(t0, t0, t1);
951     }
952     if (compute_ov) {
953         tcg_gen_movi_tl(cpu_ov, 0);
954     }
955     tcg_gen_br(l2);
956     gen_set_label(l1);
957     if (sign) {
958         tcg_gen_sari_i32(t0, t0, 31);
959     } else {
960         tcg_gen_movi_i32(t0, 0);
961     }
962     if (compute_ov) {
963         tcg_gen_movi_tl(cpu_ov, 1);
964         tcg_gen_movi_tl(cpu_so, 1);
965     }
966     gen_set_label(l2);
967     tcg_gen_extu_i32_tl(ret, t0);
968     tcg_temp_free_i32(t0);
969     tcg_temp_free_i32(t1);
970     if (unlikely(Rc(ctx->opcode) != 0))
971         gen_set_Rc0(ctx, ret);
972 }
973 /* Div functions */
974 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
975 static void glue(gen_, name)(DisasContext *ctx)                                       \
976 {                                                                             \
977     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
978                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
979                      sign, compute_ov);                                       \
980 }
981 /* divwu  divwu.  divwuo  divwuo.   */
982 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
983 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
984 /* divw  divw.  divwo  divwo.   */
985 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
986 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
987 #if defined(TARGET_PPC64)
988 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
989                                      TCGv arg2, int sign, int compute_ov)
990 {
991     int l1 = gen_new_label();
992     int l2 = gen_new_label();
993
994     tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
995     if (sign) {
996         int l3 = gen_new_label();
997         tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
998         tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
999         gen_set_label(l3);
1000         tcg_gen_div_i64(ret, arg1, arg2);
1001     } else {
1002         tcg_gen_divu_i64(ret, arg1, arg2);
1003     }
1004     if (compute_ov) {
1005         tcg_gen_movi_tl(cpu_ov, 0);
1006     }
1007     tcg_gen_br(l2);
1008     gen_set_label(l1);
1009     if (sign) {
1010         tcg_gen_sari_i64(ret, arg1, 63);
1011     } else {
1012         tcg_gen_movi_i64(ret, 0);
1013     }
1014     if (compute_ov) {
1015         tcg_gen_movi_tl(cpu_ov, 1);
1016         tcg_gen_movi_tl(cpu_so, 1);
1017     }
1018     gen_set_label(l2);
1019     if (unlikely(Rc(ctx->opcode) != 0))
1020         gen_set_Rc0(ctx, ret);
1021 }
1022 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1023 static void glue(gen_, name)(DisasContext *ctx)                                       \
1024 {                                                                             \
1025     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1026                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1027                       sign, compute_ov);                                      \
1028 }
1029 /* divwu  divwu.  divwuo  divwuo.   */
1030 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1031 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1032 /* divw  divw.  divwo  divwo.   */
1033 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1034 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1035 #endif
1036
1037 /* mulhw  mulhw. */
1038 static void gen_mulhw(DisasContext *ctx)
1039 {
1040     TCGv_i32 t0 = tcg_temp_new_i32();
1041     TCGv_i32 t1 = tcg_temp_new_i32();
1042
1043     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1044     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1045     tcg_gen_muls2_i32(t0, t1, t0, t1);
1046     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1047     tcg_temp_free_i32(t0);
1048     tcg_temp_free_i32(t1);
1049     if (unlikely(Rc(ctx->opcode) != 0))
1050         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1051 }
1052
1053 /* mulhwu  mulhwu.  */
1054 static void gen_mulhwu(DisasContext *ctx)
1055 {
1056     TCGv_i32 t0 = tcg_temp_new_i32();
1057     TCGv_i32 t1 = tcg_temp_new_i32();
1058
1059     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1060     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1061     tcg_gen_mulu2_i32(t0, t1, t0, t1);
1062     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1063     tcg_temp_free_i32(t0);
1064     tcg_temp_free_i32(t1);
1065     if (unlikely(Rc(ctx->opcode) != 0))
1066         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1067 }
1068
1069 /* mullw  mullw. */
1070 static void gen_mullw(DisasContext *ctx)
1071 {
1072     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1073                    cpu_gpr[rB(ctx->opcode)]);
1074     tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1075     if (unlikely(Rc(ctx->opcode) != 0))
1076         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1077 }
1078
1079 /* mullwo  mullwo. */
1080 static void gen_mullwo(DisasContext *ctx)
1081 {
1082     TCGv_i32 t0 = tcg_temp_new_i32();
1083     TCGv_i32 t1 = tcg_temp_new_i32();
1084
1085     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1086     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1087     tcg_gen_muls2_i32(t0, t1, t0, t1);
1088     tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
1089
1090     tcg_gen_sari_i32(t0, t0, 31);
1091     tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1092     tcg_gen_extu_i32_tl(cpu_ov, t0);
1093     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1094
1095     tcg_temp_free_i32(t0);
1096     tcg_temp_free_i32(t1);
1097     if (unlikely(Rc(ctx->opcode) != 0))
1098         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1099 }
1100
1101 /* mulli */
1102 static void gen_mulli(DisasContext *ctx)
1103 {
1104     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1105                     SIMM(ctx->opcode));
1106 }
1107
1108 #if defined(TARGET_PPC64)
1109 /* mulhd  mulhd. */
1110 static void gen_mulhd(DisasContext *ctx)
1111 {
1112     TCGv lo = tcg_temp_new();
1113     tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1114                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1115     tcg_temp_free(lo);
1116     if (unlikely(Rc(ctx->opcode) != 0)) {
1117         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1118     }
1119 }
1120
1121 /* mulhdu  mulhdu. */
1122 static void gen_mulhdu(DisasContext *ctx)
1123 {
1124     TCGv lo = tcg_temp_new();
1125     tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1126                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1127     tcg_temp_free(lo);
1128     if (unlikely(Rc(ctx->opcode) != 0)) {
1129         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1130     }
1131 }
1132
1133 /* mulld  mulld. */
1134 static void gen_mulld(DisasContext *ctx)
1135 {
1136     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1137                    cpu_gpr[rB(ctx->opcode)]);
1138     if (unlikely(Rc(ctx->opcode) != 0))
1139         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1140 }
1141
1142 /* mulldo  mulldo. */
1143 static void gen_mulldo(DisasContext *ctx)
1144 {
1145     gen_helper_mulldo(cpu_gpr[rD(ctx->opcode)], cpu_env,
1146                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1147     if (unlikely(Rc(ctx->opcode) != 0)) {
1148         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1149     }
1150 }
1151 #endif
1152
1153 /* Common subf function */
1154 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1155                                      TCGv arg2, bool add_ca, bool compute_ca,
1156                                      bool compute_ov, bool compute_rc0)
1157 {
1158     TCGv t0 = ret;
1159
1160     if (compute_ca || compute_ov) {
1161         t0 = tcg_temp_new();
1162     }
1163
1164     if (compute_ca) {
1165         /* dest = ~arg1 + arg2 [+ ca].  */
1166         if (NARROW_MODE(ctx)) {
1167             /* Caution: a non-obvious corner case of the spec is that we
1168                must produce the *entire* 64-bit addition, but produce the
1169                carry into bit 32.  */
1170             TCGv inv1 = tcg_temp_new();
1171             TCGv t1 = tcg_temp_new();
1172             tcg_gen_not_tl(inv1, arg1);
1173             if (add_ca) {
1174                 tcg_gen_add_tl(t0, arg2, cpu_ca);
1175             } else {
1176                 tcg_gen_addi_tl(t0, arg2, 1);
1177             }
1178             tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1179             tcg_gen_add_tl(t0, t0, inv1);
1180             tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1181             tcg_temp_free(t1);
1182             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);    /* extract bit 32 */
1183             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
1184         } else if (add_ca) {
1185             TCGv zero, inv1 = tcg_temp_new();
1186             tcg_gen_not_tl(inv1, arg1);
1187             zero = tcg_const_tl(0);
1188             tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1189             tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1190             tcg_temp_free(zero);
1191             tcg_temp_free(inv1);
1192         } else {
1193             tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1194             tcg_gen_sub_tl(t0, arg2, arg1);
1195         }
1196     } else if (add_ca) {
1197         /* Since we're ignoring carry-out, we can simplify the
1198            standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.  */
1199         tcg_gen_sub_tl(t0, arg2, arg1);
1200         tcg_gen_add_tl(t0, t0, cpu_ca);
1201         tcg_gen_subi_tl(t0, t0, 1);
1202     } else {
1203         tcg_gen_sub_tl(t0, arg2, arg1);
1204     }
1205
1206     if (compute_ov) {
1207         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1208     }
1209     if (unlikely(compute_rc0)) {
1210         gen_set_Rc0(ctx, t0);
1211     }
1212
1213     if (!TCGV_EQUAL(t0, ret)) {
1214         tcg_gen_mov_tl(ret, t0);
1215         tcg_temp_free(t0);
1216     }
1217 }
1218 /* Sub functions with Two operands functions */
1219 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1220 static void glue(gen_, name)(DisasContext *ctx)                               \
1221 {                                                                             \
1222     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1223                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1224                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1225 }
1226 /* Sub functions with one operand and one immediate */
1227 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1228                                 add_ca, compute_ca, compute_ov)               \
1229 static void glue(gen_, name)(DisasContext *ctx)                               \
1230 {                                                                             \
1231     TCGv t0 = tcg_const_tl(const_val);                                        \
1232     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1233                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1234                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1235     tcg_temp_free(t0);                                                        \
1236 }
1237 /* subf  subf.  subfo  subfo. */
1238 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1239 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1240 /* subfc  subfc.  subfco  subfco. */
1241 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1242 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1243 /* subfe  subfe.  subfeo  subfo. */
1244 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1245 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1246 /* subfme  subfme.  subfmeo  subfmeo.  */
1247 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1248 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1249 /* subfze  subfze.  subfzeo  subfzeo.*/
1250 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1251 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1252
1253 /* subfic */
1254 static void gen_subfic(DisasContext *ctx)
1255 {
1256     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1257     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1258                       c, 0, 1, 0, 0);
1259     tcg_temp_free(c);
1260 }
1261
1262 /* neg neg. nego nego. */
1263 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1264 {
1265     TCGv zero = tcg_const_tl(0);
1266     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1267                       zero, 0, 0, compute_ov, Rc(ctx->opcode));
1268     tcg_temp_free(zero);
1269 }
1270
1271 static void gen_neg(DisasContext *ctx)
1272 {
1273     gen_op_arith_neg(ctx, 0);
1274 }
1275
1276 static void gen_nego(DisasContext *ctx)
1277 {
1278     gen_op_arith_neg(ctx, 1);
1279 }
1280
1281 /***                            Integer logical                            ***/
1282 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1283 static void glue(gen_, name)(DisasContext *ctx)                                       \
1284 {                                                                             \
1285     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1286        cpu_gpr[rB(ctx->opcode)]);                                             \
1287     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1288         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1289 }
1290
1291 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1292 static void glue(gen_, name)(DisasContext *ctx)                                       \
1293 {                                                                             \
1294     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1295     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1296         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1297 }
1298
1299 /* and & and. */
1300 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1301 /* andc & andc. */
1302 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1303
1304 /* andi. */
1305 static void gen_andi_(DisasContext *ctx)
1306 {
1307     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1308     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1309 }
1310
1311 /* andis. */
1312 static void gen_andis_(DisasContext *ctx)
1313 {
1314     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1315     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1316 }
1317
1318 /* cntlzw */
1319 static void gen_cntlzw(DisasContext *ctx)
1320 {
1321     gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1322     if (unlikely(Rc(ctx->opcode) != 0))
1323         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1324 }
1325 /* eqv & eqv. */
1326 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1327 /* extsb & extsb. */
1328 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1329 /* extsh & extsh. */
1330 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1331 /* nand & nand. */
1332 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1333 /* nor & nor. */
1334 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1335
1336 /* or & or. */
1337 static void gen_or(DisasContext *ctx)
1338 {
1339     int rs, ra, rb;
1340
1341     rs = rS(ctx->opcode);
1342     ra = rA(ctx->opcode);
1343     rb = rB(ctx->opcode);
1344     /* Optimisation for mr. ri case */
1345     if (rs != ra || rs != rb) {
1346         if (rs != rb)
1347             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1348         else
1349             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1350         if (unlikely(Rc(ctx->opcode) != 0))
1351             gen_set_Rc0(ctx, cpu_gpr[ra]);
1352     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1353         gen_set_Rc0(ctx, cpu_gpr[rs]);
1354 #if defined(TARGET_PPC64)
1355     } else {
1356         int prio = 0;
1357
1358         switch (rs) {
1359         case 1:
1360             /* Set process priority to low */
1361             prio = 2;
1362             break;
1363         case 6:
1364             /* Set process priority to medium-low */
1365             prio = 3;
1366             break;
1367         case 2:
1368             /* Set process priority to normal */
1369             prio = 4;
1370             break;
1371 #if !defined(CONFIG_USER_ONLY)
1372         case 31:
1373             if (ctx->mem_idx > 0) {
1374                 /* Set process priority to very low */
1375                 prio = 1;
1376             }
1377             break;
1378         case 5:
1379             if (ctx->mem_idx > 0) {
1380                 /* Set process priority to medium-hight */
1381                 prio = 5;
1382             }
1383             break;
1384         case 3:
1385             if (ctx->mem_idx > 0) {
1386                 /* Set process priority to high */
1387                 prio = 6;
1388             }
1389             break;
1390         case 7:
1391             if (ctx->mem_idx > 1) {
1392                 /* Set process priority to very high */
1393                 prio = 7;
1394             }
1395             break;
1396 #endif
1397         default:
1398             /* nop */
1399             break;
1400         }
1401         if (prio) {
1402             TCGv t0 = tcg_temp_new();
1403             gen_load_spr(t0, SPR_PPR);
1404             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1405             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1406             gen_store_spr(SPR_PPR, t0);
1407             tcg_temp_free(t0);
1408         }
1409 #endif
1410     }
1411 }
1412 /* orc & orc. */
1413 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1414
1415 /* xor & xor. */
1416 static void gen_xor(DisasContext *ctx)
1417 {
1418     /* Optimisation for "set to zero" case */
1419     if (rS(ctx->opcode) != rB(ctx->opcode))
1420         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1421     else
1422         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1423     if (unlikely(Rc(ctx->opcode) != 0))
1424         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1425 }
1426
1427 /* ori */
1428 static void gen_ori(DisasContext *ctx)
1429 {
1430     target_ulong uimm = UIMM(ctx->opcode);
1431
1432     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1433         /* NOP */
1434         /* XXX: should handle special NOPs for POWER series */
1435         return;
1436     }
1437     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1438 }
1439
1440 /* oris */
1441 static void gen_oris(DisasContext *ctx)
1442 {
1443     target_ulong uimm = UIMM(ctx->opcode);
1444
1445     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1446         /* NOP */
1447         return;
1448     }
1449     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1450 }
1451
1452 /* xori */
1453 static void gen_xori(DisasContext *ctx)
1454 {
1455     target_ulong uimm = UIMM(ctx->opcode);
1456
1457     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1458         /* NOP */
1459         return;
1460     }
1461     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1462 }
1463
1464 /* xoris */
1465 static void gen_xoris(DisasContext *ctx)
1466 {
1467     target_ulong uimm = UIMM(ctx->opcode);
1468
1469     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1470         /* NOP */
1471         return;
1472     }
1473     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1474 }
1475
1476 /* popcntb : PowerPC 2.03 specification */
1477 static void gen_popcntb(DisasContext *ctx)
1478 {
1479     gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1480 }
1481
1482 static void gen_popcntw(DisasContext *ctx)
1483 {
1484     gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1485 }
1486
1487 #if defined(TARGET_PPC64)
1488 /* popcntd: PowerPC 2.06 specification */
1489 static void gen_popcntd(DisasContext *ctx)
1490 {
1491     gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1492 }
1493 #endif
1494
1495 /* prtyw: PowerPC 2.05 specification */
1496 static void gen_prtyw(DisasContext *ctx)
1497 {
1498     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1499     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1500     TCGv t0 = tcg_temp_new();
1501     tcg_gen_shri_tl(t0, rs, 16);
1502     tcg_gen_xor_tl(ra, rs, t0);
1503     tcg_gen_shri_tl(t0, ra, 8);
1504     tcg_gen_xor_tl(ra, ra, t0);
1505     tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1506     tcg_temp_free(t0);
1507 }
1508
1509 #if defined(TARGET_PPC64)
1510 /* prtyd: PowerPC 2.05 specification */
1511 static void gen_prtyd(DisasContext *ctx)
1512 {
1513     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1514     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1515     TCGv t0 = tcg_temp_new();
1516     tcg_gen_shri_tl(t0, rs, 32);
1517     tcg_gen_xor_tl(ra, rs, t0);
1518     tcg_gen_shri_tl(t0, ra, 16);
1519     tcg_gen_xor_tl(ra, ra, t0);
1520     tcg_gen_shri_tl(t0, ra, 8);
1521     tcg_gen_xor_tl(ra, ra, t0);
1522     tcg_gen_andi_tl(ra, ra, 1);
1523     tcg_temp_free(t0);
1524 }
1525 #endif
1526
1527 #if defined(TARGET_PPC64)
1528 /* extsw & extsw. */
1529 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1530
1531 /* cntlzd */
1532 static void gen_cntlzd(DisasContext *ctx)
1533 {
1534     gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1535     if (unlikely(Rc(ctx->opcode) != 0))
1536         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1537 }
1538 #endif
1539
1540 /***                             Integer rotate                            ***/
1541
1542 /* rlwimi & rlwimi. */
1543 static void gen_rlwimi(DisasContext *ctx)
1544 {
1545     uint32_t mb, me, sh;
1546
1547     mb = MB(ctx->opcode);
1548     me = ME(ctx->opcode);
1549     sh = SH(ctx->opcode);
1550     if (likely(sh == 0 && mb == 0 && me == 31)) {
1551         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1552     } else {
1553         target_ulong mask;
1554         TCGv t1;
1555         TCGv t0 = tcg_temp_new();
1556 #if defined(TARGET_PPC64)
1557         TCGv_i32 t2 = tcg_temp_new_i32();
1558         tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1559         tcg_gen_rotli_i32(t2, t2, sh);
1560         tcg_gen_extu_i32_i64(t0, t2);
1561         tcg_temp_free_i32(t2);
1562 #else
1563         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1564 #endif
1565 #if defined(TARGET_PPC64)
1566         mb += 32;
1567         me += 32;
1568 #endif
1569         mask = MASK(mb, me);
1570         t1 = tcg_temp_new();
1571         tcg_gen_andi_tl(t0, t0, mask);
1572         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1573         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1574         tcg_temp_free(t0);
1575         tcg_temp_free(t1);
1576     }
1577     if (unlikely(Rc(ctx->opcode) != 0))
1578         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1579 }
1580
1581 /* rlwinm & rlwinm. */
1582 static void gen_rlwinm(DisasContext *ctx)
1583 {
1584     uint32_t mb, me, sh;
1585
1586     sh = SH(ctx->opcode);
1587     mb = MB(ctx->opcode);
1588     me = ME(ctx->opcode);
1589
1590     if (likely(mb == 0 && me == (31 - sh))) {
1591         if (likely(sh == 0)) {
1592             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1593         } else {
1594             TCGv t0 = tcg_temp_new();
1595             tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1596             tcg_gen_shli_tl(t0, t0, sh);
1597             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1598             tcg_temp_free(t0);
1599         }
1600     } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1601         TCGv t0 = tcg_temp_new();
1602         tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1603         tcg_gen_shri_tl(t0, t0, mb);
1604         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1605         tcg_temp_free(t0);
1606     } else {
1607         TCGv t0 = tcg_temp_new();
1608 #if defined(TARGET_PPC64)
1609         TCGv_i32 t1 = tcg_temp_new_i32();
1610         tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1611         tcg_gen_rotli_i32(t1, t1, sh);
1612         tcg_gen_extu_i32_i64(t0, t1);
1613         tcg_temp_free_i32(t1);
1614 #else
1615         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1616 #endif
1617 #if defined(TARGET_PPC64)
1618         mb += 32;
1619         me += 32;
1620 #endif
1621         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1622         tcg_temp_free(t0);
1623     }
1624     if (unlikely(Rc(ctx->opcode) != 0))
1625         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1626 }
1627
1628 /* rlwnm & rlwnm. */
1629 static void gen_rlwnm(DisasContext *ctx)
1630 {
1631     uint32_t mb, me;
1632     TCGv t0;
1633 #if defined(TARGET_PPC64)
1634     TCGv_i32 t1, t2;
1635 #endif
1636
1637     mb = MB(ctx->opcode);
1638     me = ME(ctx->opcode);
1639     t0 = tcg_temp_new();
1640     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1641 #if defined(TARGET_PPC64)
1642     t1 = tcg_temp_new_i32();
1643     t2 = tcg_temp_new_i32();
1644     tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1645     tcg_gen_trunc_i64_i32(t2, t0);
1646     tcg_gen_rotl_i32(t1, t1, t2);
1647     tcg_gen_extu_i32_i64(t0, t1);
1648     tcg_temp_free_i32(t1);
1649     tcg_temp_free_i32(t2);
1650 #else
1651     tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1652 #endif
1653     if (unlikely(mb != 0 || me != 31)) {
1654 #if defined(TARGET_PPC64)
1655         mb += 32;
1656         me += 32;
1657 #endif
1658         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1659     } else {
1660         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1661     }
1662     tcg_temp_free(t0);
1663     if (unlikely(Rc(ctx->opcode) != 0))
1664         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1665 }
1666
1667 #if defined(TARGET_PPC64)
1668 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
1669 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1670 {                                                                             \
1671     gen_##name(ctx, 0);                                                       \
1672 }                                                                             \
1673                                                                               \
1674 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1675 {                                                                             \
1676     gen_##name(ctx, 1);                                                       \
1677 }
1678 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
1679 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1680 {                                                                             \
1681     gen_##name(ctx, 0, 0);                                                    \
1682 }                                                                             \
1683                                                                               \
1684 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1685 {                                                                             \
1686     gen_##name(ctx, 0, 1);                                                    \
1687 }                                                                             \
1688                                                                               \
1689 static void glue(gen_, name##2)(DisasContext *ctx)                            \
1690 {                                                                             \
1691     gen_##name(ctx, 1, 0);                                                    \
1692 }                                                                             \
1693                                                                               \
1694 static void glue(gen_, name##3)(DisasContext *ctx)                            \
1695 {                                                                             \
1696     gen_##name(ctx, 1, 1);                                                    \
1697 }
1698
1699 static inline void gen_rldinm(DisasContext *ctx, uint32_t mb, uint32_t me,
1700                               uint32_t sh)
1701 {
1702     if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1703         tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1704     } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1705         tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1706     } else {
1707         TCGv t0 = tcg_temp_new();
1708         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1709         if (likely(mb == 0 && me == 63)) {
1710             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1711         } else {
1712             tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1713         }
1714         tcg_temp_free(t0);
1715     }
1716     if (unlikely(Rc(ctx->opcode) != 0))
1717         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1718 }
1719 /* rldicl - rldicl. */
1720 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
1721 {
1722     uint32_t sh, mb;
1723
1724     sh = SH(ctx->opcode) | (shn << 5);
1725     mb = MB(ctx->opcode) | (mbn << 5);
1726     gen_rldinm(ctx, mb, 63, sh);
1727 }
1728 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1729 /* rldicr - rldicr. */
1730 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
1731 {
1732     uint32_t sh, me;
1733
1734     sh = SH(ctx->opcode) | (shn << 5);
1735     me = MB(ctx->opcode) | (men << 5);
1736     gen_rldinm(ctx, 0, me, sh);
1737 }
1738 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1739 /* rldic - rldic. */
1740 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
1741 {
1742     uint32_t sh, mb;
1743
1744     sh = SH(ctx->opcode) | (shn << 5);
1745     mb = MB(ctx->opcode) | (mbn << 5);
1746     gen_rldinm(ctx, mb, 63 - sh, sh);
1747 }
1748 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1749
1750 static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
1751 {
1752     TCGv t0;
1753
1754     t0 = tcg_temp_new();
1755     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1756     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1757     if (unlikely(mb != 0 || me != 63)) {
1758         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1759     } else {
1760         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1761     }
1762     tcg_temp_free(t0);
1763     if (unlikely(Rc(ctx->opcode) != 0))
1764         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1765 }
1766
1767 /* rldcl - rldcl. */
1768 static inline void gen_rldcl(DisasContext *ctx, int mbn)
1769 {
1770     uint32_t mb;
1771
1772     mb = MB(ctx->opcode) | (mbn << 5);
1773     gen_rldnm(ctx, mb, 63);
1774 }
1775 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1776 /* rldcr - rldcr. */
1777 static inline void gen_rldcr(DisasContext *ctx, int men)
1778 {
1779     uint32_t me;
1780
1781     me = MB(ctx->opcode) | (men << 5);
1782     gen_rldnm(ctx, 0, me);
1783 }
1784 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1785 /* rldimi - rldimi. */
1786 static inline void gen_rldimi(DisasContext *ctx, int mbn, int shn)
1787 {
1788     uint32_t sh, mb, me;
1789
1790     sh = SH(ctx->opcode) | (shn << 5);
1791     mb = MB(ctx->opcode) | (mbn << 5);
1792     me = 63 - sh;
1793     if (unlikely(sh == 0 && mb == 0)) {
1794         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1795     } else {
1796         TCGv t0, t1;
1797         target_ulong mask;
1798
1799         t0 = tcg_temp_new();
1800         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1801         t1 = tcg_temp_new();
1802         mask = MASK(mb, me);
1803         tcg_gen_andi_tl(t0, t0, mask);
1804         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1805         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1806         tcg_temp_free(t0);
1807         tcg_temp_free(t1);
1808     }
1809     if (unlikely(Rc(ctx->opcode) != 0))
1810         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1811 }
1812 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1813 #endif
1814
1815 /***                             Integer shift                             ***/
1816
1817 /* slw & slw. */
1818 static void gen_slw(DisasContext *ctx)
1819 {
1820     TCGv t0, t1;
1821
1822     t0 = tcg_temp_new();
1823     /* AND rS with a mask that is 0 when rB >= 0x20 */
1824 #if defined(TARGET_PPC64)
1825     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1826     tcg_gen_sari_tl(t0, t0, 0x3f);
1827 #else
1828     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1829     tcg_gen_sari_tl(t0, t0, 0x1f);
1830 #endif
1831     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1832     t1 = tcg_temp_new();
1833     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1834     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1835     tcg_temp_free(t1);
1836     tcg_temp_free(t0);
1837     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1838     if (unlikely(Rc(ctx->opcode) != 0))
1839         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1840 }
1841
1842 /* sraw & sraw. */
1843 static void gen_sraw(DisasContext *ctx)
1844 {
1845     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
1846                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1847     if (unlikely(Rc(ctx->opcode) != 0))
1848         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1849 }
1850
1851 /* srawi & srawi. */
1852 static void gen_srawi(DisasContext *ctx)
1853 {
1854     int sh = SH(ctx->opcode);
1855     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1856     TCGv src = cpu_gpr[rS(ctx->opcode)];
1857     if (sh == 0) {
1858         tcg_gen_mov_tl(dst, src);
1859         tcg_gen_movi_tl(cpu_ca, 0);
1860     } else {
1861         TCGv t0;
1862         tcg_gen_ext32s_tl(dst, src);
1863         tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1864         t0 = tcg_temp_new();
1865         tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1866         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1867         tcg_temp_free(t0);
1868         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1869         tcg_gen_sari_tl(dst, dst, sh);
1870     }
1871     if (unlikely(Rc(ctx->opcode) != 0)) {
1872         gen_set_Rc0(ctx, dst);
1873     }
1874 }
1875
1876 /* srw & srw. */
1877 static void gen_srw(DisasContext *ctx)
1878 {
1879     TCGv t0, t1;
1880
1881     t0 = tcg_temp_new();
1882     /* AND rS with a mask that is 0 when rB >= 0x20 */
1883 #if defined(TARGET_PPC64)
1884     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1885     tcg_gen_sari_tl(t0, t0, 0x3f);
1886 #else
1887     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1888     tcg_gen_sari_tl(t0, t0, 0x1f);
1889 #endif
1890     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1891     tcg_gen_ext32u_tl(t0, t0);
1892     t1 = tcg_temp_new();
1893     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1894     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1895     tcg_temp_free(t1);
1896     tcg_temp_free(t0);
1897     if (unlikely(Rc(ctx->opcode) != 0))
1898         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1899 }
1900
1901 #if defined(TARGET_PPC64)
1902 /* sld & sld. */
1903 static void gen_sld(DisasContext *ctx)
1904 {
1905     TCGv t0, t1;
1906
1907     t0 = tcg_temp_new();
1908     /* AND rS with a mask that is 0 when rB >= 0x40 */
1909     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1910     tcg_gen_sari_tl(t0, t0, 0x3f);
1911     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1912     t1 = tcg_temp_new();
1913     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1914     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1915     tcg_temp_free(t1);
1916     tcg_temp_free(t0);
1917     if (unlikely(Rc(ctx->opcode) != 0))
1918         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1919 }
1920
1921 /* srad & srad. */
1922 static void gen_srad(DisasContext *ctx)
1923 {
1924     gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
1925                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1926     if (unlikely(Rc(ctx->opcode) != 0))
1927         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1928 }
1929 /* sradi & sradi. */
1930 static inline void gen_sradi(DisasContext *ctx, int n)
1931 {
1932     int sh = SH(ctx->opcode) + (n << 5);
1933     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1934     TCGv src = cpu_gpr[rS(ctx->opcode)];
1935     if (sh == 0) {
1936         tcg_gen_mov_tl(dst, src);
1937         tcg_gen_movi_tl(cpu_ca, 0);
1938     } else {
1939         TCGv t0;
1940         tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1941         t0 = tcg_temp_new();
1942         tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1943         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1944         tcg_temp_free(t0);
1945         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1946         tcg_gen_sari_tl(dst, src, sh);
1947     }
1948     if (unlikely(Rc(ctx->opcode) != 0)) {
1949         gen_set_Rc0(ctx, dst);
1950     }
1951 }
1952
1953 static void gen_sradi0(DisasContext *ctx)
1954 {
1955     gen_sradi(ctx, 0);
1956 }
1957
1958 static void gen_sradi1(DisasContext *ctx)
1959 {
1960     gen_sradi(ctx, 1);
1961 }
1962
1963 /* srd & srd. */
1964 static void gen_srd(DisasContext *ctx)
1965 {
1966     TCGv t0, t1;
1967
1968     t0 = tcg_temp_new();
1969     /* AND rS with a mask that is 0 when rB >= 0x40 */
1970     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1971     tcg_gen_sari_tl(t0, t0, 0x3f);
1972     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1973     t1 = tcg_temp_new();
1974     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1975     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1976     tcg_temp_free(t1);
1977     tcg_temp_free(t0);
1978     if (unlikely(Rc(ctx->opcode) != 0))
1979         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1980 }
1981 #endif
1982
1983 /***                       Floating-Point arithmetic                       ***/
1984 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
1985 static void gen_f##name(DisasContext *ctx)                                    \
1986 {                                                                             \
1987     if (unlikely(!ctx->fpu_enabled)) {                                        \
1988         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
1989         return;                                                               \
1990     }                                                                         \
1991     /* NIP cannot be restored if the memory exception comes from an helper */ \
1992     gen_update_nip(ctx, ctx->nip - 4);                                        \
1993     gen_reset_fpstatus();                                                     \
1994     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
1995                      cpu_fpr[rA(ctx->opcode)],                                \
1996                      cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
1997     if (isfloat) {                                                            \
1998         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
1999                         cpu_fpr[rD(ctx->opcode)]);                            \
2000     }                                                                         \
2001     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
2002                      Rc(ctx->opcode) != 0);                                   \
2003 }
2004
2005 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2006 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2007 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2008
2009 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2010 static void gen_f##name(DisasContext *ctx)                                    \
2011 {                                                                             \
2012     if (unlikely(!ctx->fpu_enabled)) {                                        \
2013         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2014         return;                                                               \
2015     }                                                                         \
2016     /* NIP cannot be restored if the memory exception comes from an helper */ \
2017     gen_update_nip(ctx, ctx->nip - 4);                                        \
2018     gen_reset_fpstatus();                                                     \
2019     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2020                      cpu_fpr[rA(ctx->opcode)],                                \
2021                      cpu_fpr[rB(ctx->opcode)]);                               \
2022     if (isfloat) {                                                            \
2023         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2024                         cpu_fpr[rD(ctx->opcode)]);                            \
2025     }                                                                         \
2026     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2027                      set_fprf, Rc(ctx->opcode) != 0);                         \
2028 }
2029 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2030 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2031 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2032
2033 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2034 static void gen_f##name(DisasContext *ctx)                                    \
2035 {                                                                             \
2036     if (unlikely(!ctx->fpu_enabled)) {                                        \
2037         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2038         return;                                                               \
2039     }                                                                         \
2040     /* NIP cannot be restored if the memory exception comes from an helper */ \
2041     gen_update_nip(ctx, ctx->nip - 4);                                        \
2042     gen_reset_fpstatus();                                                     \
2043     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2044                      cpu_fpr[rA(ctx->opcode)],                                \
2045                      cpu_fpr[rC(ctx->opcode)]);                               \
2046     if (isfloat) {                                                            \
2047         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2048                         cpu_fpr[rD(ctx->opcode)]);                            \
2049     }                                                                         \
2050     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2051                      set_fprf, Rc(ctx->opcode) != 0);                         \
2052 }
2053 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2054 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2055 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2056
2057 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2058 static void gen_f##name(DisasContext *ctx)                                    \
2059 {                                                                             \
2060     if (unlikely(!ctx->fpu_enabled)) {                                        \
2061         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2062         return;                                                               \
2063     }                                                                         \
2064     /* NIP cannot be restored if the memory exception comes from an helper */ \
2065     gen_update_nip(ctx, ctx->nip - 4);                                        \
2066     gen_reset_fpstatus();                                                     \
2067     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2068                        cpu_fpr[rB(ctx->opcode)]);                             \
2069     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2070                      set_fprf, Rc(ctx->opcode) != 0);                         \
2071 }
2072
2073 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2074 static void gen_f##name(DisasContext *ctx)                                    \
2075 {                                                                             \
2076     if (unlikely(!ctx->fpu_enabled)) {                                        \
2077         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2078         return;                                                               \
2079     }                                                                         \
2080     /* NIP cannot be restored if the memory exception comes from an helper */ \
2081     gen_update_nip(ctx, ctx->nip - 4);                                        \
2082     gen_reset_fpstatus();                                                     \
2083     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2084                        cpu_fpr[rB(ctx->opcode)]);                             \
2085     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2086                      set_fprf, Rc(ctx->opcode) != 0);                         \
2087 }
2088
2089 /* fadd - fadds */
2090 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2091 /* fdiv - fdivs */
2092 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2093 /* fmul - fmuls */
2094 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2095
2096 /* fre */
2097 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2098
2099 /* fres */
2100 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2101
2102 /* frsqrte */
2103 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2104
2105 /* frsqrtes */
2106 static void gen_frsqrtes(DisasContext *ctx)
2107 {
2108     if (unlikely(!ctx->fpu_enabled)) {
2109         gen_exception(ctx, POWERPC_EXCP_FPU);
2110         return;
2111     }
2112     /* NIP cannot be restored if the memory exception comes from an helper */
2113     gen_update_nip(ctx, ctx->nip - 4);
2114     gen_reset_fpstatus();
2115     gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2116                        cpu_fpr[rB(ctx->opcode)]);
2117     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2118                     cpu_fpr[rD(ctx->opcode)]);
2119     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2120 }
2121
2122 /* fsel */
2123 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2124 /* fsub - fsubs */
2125 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2126 /* Optional: */
2127
2128 /* fsqrt */
2129 static void gen_fsqrt(DisasContext *ctx)
2130 {
2131     if (unlikely(!ctx->fpu_enabled)) {
2132         gen_exception(ctx, POWERPC_EXCP_FPU);
2133         return;
2134     }
2135     /* NIP cannot be restored if the memory exception comes from an helper */
2136     gen_update_nip(ctx, ctx->nip - 4);
2137     gen_reset_fpstatus();
2138     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2139                      cpu_fpr[rB(ctx->opcode)]);
2140     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2141 }
2142
2143 static void gen_fsqrts(DisasContext *ctx)
2144 {
2145     if (unlikely(!ctx->fpu_enabled)) {
2146         gen_exception(ctx, POWERPC_EXCP_FPU);
2147         return;
2148     }
2149     /* NIP cannot be restored if the memory exception comes from an helper */
2150     gen_update_nip(ctx, ctx->nip - 4);
2151     gen_reset_fpstatus();
2152     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2153                      cpu_fpr[rB(ctx->opcode)]);
2154     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2155                     cpu_fpr[rD(ctx->opcode)]);
2156     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2157 }
2158
2159 /***                     Floating-Point multiply-and-add                   ***/
2160 /* fmadd - fmadds */
2161 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2162 /* fmsub - fmsubs */
2163 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2164 /* fnmadd - fnmadds */
2165 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2166 /* fnmsub - fnmsubs */
2167 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2168
2169 /***                     Floating-Point round & convert                    ***/
2170 /* fctiw */
2171 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2172 /* fctiwz */
2173 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2174 /* frsp */
2175 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2176 #if defined(TARGET_PPC64)
2177 /* fcfid */
2178 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2179 /* fctid */
2180 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2181 /* fctidz */
2182 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2183 #endif
2184
2185 /* frin */
2186 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2187 /* friz */
2188 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2189 /* frip */
2190 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2191 /* frim */
2192 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2193
2194 /***                         Floating-Point compare                        ***/
2195
2196 /* fcmpo */
2197 static void gen_fcmpo(DisasContext *ctx)
2198 {
2199     TCGv_i32 crf;
2200     if (unlikely(!ctx->fpu_enabled)) {
2201         gen_exception(ctx, POWERPC_EXCP_FPU);
2202         return;
2203     }
2204     /* NIP cannot be restored if the memory exception comes from an helper */
2205     gen_update_nip(ctx, ctx->nip - 4);
2206     gen_reset_fpstatus();
2207     crf = tcg_const_i32(crfD(ctx->opcode));
2208     gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2209                      cpu_fpr[rB(ctx->opcode)], crf);
2210     tcg_temp_free_i32(crf);
2211     gen_helper_float_check_status(cpu_env);
2212 }
2213
2214 /* fcmpu */
2215 static void gen_fcmpu(DisasContext *ctx)
2216 {
2217     TCGv_i32 crf;
2218     if (unlikely(!ctx->fpu_enabled)) {
2219         gen_exception(ctx, POWERPC_EXCP_FPU);
2220         return;
2221     }
2222     /* NIP cannot be restored if the memory exception comes from an helper */
2223     gen_update_nip(ctx, ctx->nip - 4);
2224     gen_reset_fpstatus();
2225     crf = tcg_const_i32(crfD(ctx->opcode));
2226     gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2227                      cpu_fpr[rB(ctx->opcode)], crf);
2228     tcg_temp_free_i32(crf);
2229     gen_helper_float_check_status(cpu_env);
2230 }
2231
2232 /***                         Floating-point move                           ***/
2233 /* fabs */
2234 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2235 static void gen_fabs(DisasContext *ctx)
2236 {
2237     if (unlikely(!ctx->fpu_enabled)) {
2238         gen_exception(ctx, POWERPC_EXCP_FPU);
2239         return;
2240     }
2241     tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2242                      ~(1ULL << 63));
2243     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2244 }
2245
2246 /* fmr  - fmr. */
2247 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2248 static void gen_fmr(DisasContext *ctx)
2249 {
2250     if (unlikely(!ctx->fpu_enabled)) {
2251         gen_exception(ctx, POWERPC_EXCP_FPU);
2252         return;
2253     }
2254     tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2255     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2256 }
2257
2258 /* fnabs */
2259 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2260 static void gen_fnabs(DisasContext *ctx)
2261 {
2262     if (unlikely(!ctx->fpu_enabled)) {
2263         gen_exception(ctx, POWERPC_EXCP_FPU);
2264         return;
2265     }
2266     tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2267                     1ULL << 63);
2268     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2269 }
2270
2271 /* fneg */
2272 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2273 static void gen_fneg(DisasContext *ctx)
2274 {
2275     if (unlikely(!ctx->fpu_enabled)) {
2276         gen_exception(ctx, POWERPC_EXCP_FPU);
2277         return;
2278     }
2279     tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2280                      1ULL << 63);
2281     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2282 }
2283
2284 /* fcpsgn: PowerPC 2.05 specification */
2285 /* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2286 static void gen_fcpsgn(DisasContext *ctx)
2287 {
2288     if (unlikely(!ctx->fpu_enabled)) {
2289         gen_exception(ctx, POWERPC_EXCP_FPU);
2290         return;
2291     }
2292     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2293                         cpu_fpr[rB(ctx->opcode)], 0, 63);
2294     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2295 }
2296
2297 /***                  Floating-Point status & ctrl register                ***/
2298
2299 /* mcrfs */
2300 static void gen_mcrfs(DisasContext *ctx)
2301 {
2302     TCGv tmp = tcg_temp_new();
2303     int bfa;
2304
2305     if (unlikely(!ctx->fpu_enabled)) {
2306         gen_exception(ctx, POWERPC_EXCP_FPU);
2307         return;
2308     }
2309     bfa = 4 * (7 - crfS(ctx->opcode));
2310     tcg_gen_shri_tl(tmp, cpu_fpscr, bfa);
2311     tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2312     tcg_temp_free(tmp);
2313     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2314     tcg_gen_andi_tl(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2315 }
2316
2317 /* mffs */
2318 static void gen_mffs(DisasContext *ctx)
2319 {
2320     if (unlikely(!ctx->fpu_enabled)) {
2321         gen_exception(ctx, POWERPC_EXCP_FPU);
2322         return;
2323     }
2324     gen_reset_fpstatus();
2325     tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2326     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2327 }
2328
2329 /* mtfsb0 */
2330 static void gen_mtfsb0(DisasContext *ctx)
2331 {
2332     uint8_t crb;
2333
2334     if (unlikely(!ctx->fpu_enabled)) {
2335         gen_exception(ctx, POWERPC_EXCP_FPU);
2336         return;
2337     }
2338     crb = 31 - crbD(ctx->opcode);
2339     gen_reset_fpstatus();
2340     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2341         TCGv_i32 t0;
2342         /* NIP cannot be restored if the memory exception comes from an helper */
2343         gen_update_nip(ctx, ctx->nip - 4);
2344         t0 = tcg_const_i32(crb);
2345         gen_helper_fpscr_clrbit(cpu_env, t0);
2346         tcg_temp_free_i32(t0);
2347     }
2348     if (unlikely(Rc(ctx->opcode) != 0)) {
2349         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2350         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2351     }
2352 }
2353
2354 /* mtfsb1 */
2355 static void gen_mtfsb1(DisasContext *ctx)
2356 {
2357     uint8_t crb;
2358
2359     if (unlikely(!ctx->fpu_enabled)) {
2360         gen_exception(ctx, POWERPC_EXCP_FPU);
2361         return;
2362     }
2363     crb = 31 - crbD(ctx->opcode);
2364     gen_reset_fpstatus();
2365     /* XXX: we pretend we can only do IEEE floating-point computations */
2366     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2367         TCGv_i32 t0;
2368         /* NIP cannot be restored if the memory exception comes from an helper */
2369         gen_update_nip(ctx, ctx->nip - 4);
2370         t0 = tcg_const_i32(crb);
2371         gen_helper_fpscr_setbit(cpu_env, t0);
2372         tcg_temp_free_i32(t0);
2373     }
2374     if (unlikely(Rc(ctx->opcode) != 0)) {
2375         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2376         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2377     }
2378     /* We can raise a differed exception */
2379     gen_helper_float_check_status(cpu_env);
2380 }
2381
2382 /* mtfsf */
2383 static void gen_mtfsf(DisasContext *ctx)
2384 {
2385     TCGv_i32 t0;
2386     int flm, l, w;
2387
2388     if (unlikely(!ctx->fpu_enabled)) {
2389         gen_exception(ctx, POWERPC_EXCP_FPU);
2390         return;
2391     }
2392     flm = FPFLM(ctx->opcode);
2393     l = FPL(ctx->opcode);
2394     w = FPW(ctx->opcode);
2395     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2396         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2397         return;
2398     }
2399     /* NIP cannot be restored if the memory exception comes from an helper */
2400     gen_update_nip(ctx, ctx->nip - 4);
2401     gen_reset_fpstatus();
2402     if (l) {
2403         t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2404     } else {
2405         t0 = tcg_const_i32(flm << (w * 8));
2406     }
2407     gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
2408     tcg_temp_free_i32(t0);
2409     if (unlikely(Rc(ctx->opcode) != 0)) {
2410         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2411         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2412     }
2413     /* We can raise a differed exception */
2414     gen_helper_float_check_status(cpu_env);
2415 }
2416
2417 /* mtfsfi */
2418 static void gen_mtfsfi(DisasContext *ctx)
2419 {
2420     int bf, sh, w;
2421     TCGv_i64 t0;
2422     TCGv_i32 t1;
2423
2424     if (unlikely(!ctx->fpu_enabled)) {
2425         gen_exception(ctx, POWERPC_EXCP_FPU);
2426         return;
2427     }
2428     w = FPW(ctx->opcode);
2429     bf = FPBF(ctx->opcode);
2430     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2431         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2432         return;
2433     }
2434     sh = (8 * w) + 7 - bf;
2435     /* NIP cannot be restored if the memory exception comes from an helper */
2436     gen_update_nip(ctx, ctx->nip - 4);
2437     gen_reset_fpstatus();
2438     t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
2439     t1 = tcg_const_i32(1 << sh);
2440     gen_helper_store_fpscr(cpu_env, t0, t1);
2441     tcg_temp_free_i64(t0);
2442     tcg_temp_free_i32(t1);
2443     if (unlikely(Rc(ctx->opcode) != 0)) {
2444         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2445         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2446     }
2447     /* We can raise a differed exception */
2448     gen_helper_float_check_status(cpu_env);
2449 }
2450
2451 /***                           Addressing modes                            ***/
2452 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2453 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2454                                       target_long maskl)
2455 {
2456     target_long simm = SIMM(ctx->opcode);
2457
2458     simm &= ~maskl;
2459     if (rA(ctx->opcode) == 0) {
2460         if (NARROW_MODE(ctx)) {
2461             simm = (uint32_t)simm;
2462         }
2463         tcg_gen_movi_tl(EA, simm);
2464     } else if (likely(simm != 0)) {
2465         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2466         if (NARROW_MODE(ctx)) {
2467             tcg_gen_ext32u_tl(EA, EA);
2468         }
2469     } else {
2470         if (NARROW_MODE(ctx)) {
2471             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2472         } else {
2473             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2474         }
2475     }
2476 }
2477
2478 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2479 {
2480     if (rA(ctx->opcode) == 0) {
2481         if (NARROW_MODE(ctx)) {
2482             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2483         } else {
2484             tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2485         }
2486     } else {
2487         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2488         if (NARROW_MODE(ctx)) {
2489             tcg_gen_ext32u_tl(EA, EA);
2490         }
2491     }
2492 }
2493
2494 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2495 {
2496     if (rA(ctx->opcode) == 0) {
2497         tcg_gen_movi_tl(EA, 0);
2498     } else if (NARROW_MODE(ctx)) {
2499         tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2500     } else {
2501         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2502     }
2503 }
2504
2505 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2506                                 target_long val)
2507 {
2508     tcg_gen_addi_tl(ret, arg1, val);
2509     if (NARROW_MODE(ctx)) {
2510         tcg_gen_ext32u_tl(ret, ret);
2511     }
2512 }
2513
2514 static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
2515 {
2516     int l1 = gen_new_label();
2517     TCGv t0 = tcg_temp_new();
2518     TCGv_i32 t1, t2;
2519     /* NIP cannot be restored if the memory exception comes from an helper */
2520     gen_update_nip(ctx, ctx->nip - 4);
2521     tcg_gen_andi_tl(t0, EA, mask);
2522     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2523     t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2524     t2 = tcg_const_i32(0);
2525     gen_helper_raise_exception_err(cpu_env, t1, t2);
2526     tcg_temp_free_i32(t1);
2527     tcg_temp_free_i32(t2);
2528     gen_set_label(l1);
2529     tcg_temp_free(t0);
2530 }
2531
2532 /***                             Integer load                              ***/
2533 static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2534 {
2535     tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2536 }
2537
2538 static inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2539 {
2540     tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2541 }
2542
2543 static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2544 {
2545     tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2546     if (unlikely(ctx->le_mode)) {
2547         tcg_gen_bswap16_tl(arg1, arg1);
2548     }
2549 }
2550
2551 static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2552 {
2553     if (unlikely(ctx->le_mode)) {
2554         tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2555         tcg_gen_bswap16_tl(arg1, arg1);
2556         tcg_gen_ext16s_tl(arg1, arg1);
2557     } else {
2558         tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2559     }
2560 }
2561
2562 static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2563 {
2564     tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2565     if (unlikely(ctx->le_mode)) {
2566         tcg_gen_bswap32_tl(arg1, arg1);
2567     }
2568 }
2569
2570 static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2571 {
2572     TCGv tmp = tcg_temp_new();
2573     gen_qemu_ld32u(ctx, tmp, addr);
2574     tcg_gen_extu_tl_i64(val, tmp);
2575     tcg_temp_free(tmp);
2576 }
2577
2578 static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2579 {
2580     if (unlikely(ctx->le_mode)) {
2581         tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2582         tcg_gen_bswap32_tl(arg1, arg1);
2583         tcg_gen_ext32s_tl(arg1, arg1);
2584     } else
2585         tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
2586 }
2587
2588 static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2589 {
2590     TCGv tmp = tcg_temp_new();
2591     gen_qemu_ld32s(ctx, tmp, addr);
2592     tcg_gen_ext_tl_i64(val, tmp);
2593     tcg_temp_free(tmp);
2594 }
2595
2596 static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2597 {
2598     tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2599     if (unlikely(ctx->le_mode)) {
2600         tcg_gen_bswap64_i64(arg1, arg1);
2601     }
2602 }
2603
2604 static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2605 {
2606     tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2607 }
2608
2609 static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2610 {
2611     if (unlikely(ctx->le_mode)) {
2612         TCGv t0 = tcg_temp_new();
2613         tcg_gen_ext16u_tl(t0, arg1);
2614         tcg_gen_bswap16_tl(t0, t0);
2615         tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2616         tcg_temp_free(t0);
2617     } else {
2618         tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2619     }
2620 }
2621
2622 static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2623 {
2624     if (unlikely(ctx->le_mode)) {
2625         TCGv t0 = tcg_temp_new();
2626         tcg_gen_ext32u_tl(t0, arg1);
2627         tcg_gen_bswap32_tl(t0, t0);
2628         tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2629         tcg_temp_free(t0);
2630     } else {
2631         tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2632     }
2633 }
2634
2635 static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2636 {
2637     TCGv tmp = tcg_temp_new();
2638     tcg_gen_trunc_i64_tl(tmp, val);
2639     gen_qemu_st32(ctx, tmp, addr);
2640     tcg_temp_free(tmp);
2641 }
2642
2643 static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2644 {
2645     if (unlikely(ctx->le_mode)) {
2646         TCGv_i64 t0 = tcg_temp_new_i64();
2647         tcg_gen_bswap64_i64(t0, arg1);
2648         tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2649         tcg_temp_free_i64(t0);
2650     } else
2651         tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2652 }
2653
2654 #define GEN_LD(name, ldop, opc, type)                                         \
2655 static void glue(gen_, name)(DisasContext *ctx)                                       \
2656 {                                                                             \
2657     TCGv EA;                                                                  \
2658     gen_set_access_type(ctx, ACCESS_INT);                                     \
2659     EA = tcg_temp_new();                                                      \
2660     gen_addr_imm_index(ctx, EA, 0);                                           \
2661     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2662     tcg_temp_free(EA);                                                        \
2663 }
2664
2665 #define GEN_LDU(name, ldop, opc, type)                                        \
2666 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
2667 {                                                                             \
2668     TCGv EA;                                                                  \
2669     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2670                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2671         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2672         return;                                                               \
2673     }                                                                         \
2674     gen_set_access_type(ctx, ACCESS_INT);                                     \
2675     EA = tcg_temp_new();                                                      \
2676     if (type == PPC_64B)                                                      \
2677         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2678     else                                                                      \
2679         gen_addr_imm_index(ctx, EA, 0);                                       \
2680     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2681     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2682     tcg_temp_free(EA);                                                        \
2683 }
2684
2685 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2686 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2687 {                                                                             \
2688     TCGv EA;                                                                  \
2689     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2690                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2691         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2692         return;                                                               \
2693     }                                                                         \
2694     gen_set_access_type(ctx, ACCESS_INT);                                     \
2695     EA = tcg_temp_new();                                                      \
2696     gen_addr_reg_index(ctx, EA);                                              \
2697     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2698     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2699     tcg_temp_free(EA);                                                        \
2700 }
2701
2702 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
2703 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2704 {                                                                             \
2705     TCGv EA;                                                                  \
2706     gen_set_access_type(ctx, ACCESS_INT);                                     \
2707     EA = tcg_temp_new();                                                      \
2708     gen_addr_reg_index(ctx, EA);                                              \
2709     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2710     tcg_temp_free(EA);                                                        \
2711 }
2712 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2713     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
2714
2715 #define GEN_LDS(name, ldop, op, type)                                         \
2716 GEN_LD(name, ldop, op | 0x20, type);                                          \
2717 GEN_LDU(name, ldop, op | 0x21, type);                                         \
2718 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2719 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2720
2721 /* lbz lbzu lbzux lbzx */
2722 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2723 /* lha lhau lhaux lhax */
2724 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2725 /* lhz lhzu lhzux lhzx */
2726 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2727 /* lwz lwzu lwzux lwzx */
2728 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2729 #if defined(TARGET_PPC64)
2730 /* lwaux */
2731 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2732 /* lwax */
2733 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2734 /* ldux */
2735 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2736 /* ldx */
2737 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2738
2739 static void gen_ld(DisasContext *ctx)
2740 {
2741     TCGv EA;
2742     if (Rc(ctx->opcode)) {
2743         if (unlikely(rA(ctx->opcode) == 0 ||
2744                      rA(ctx->opcode) == rD(ctx->opcode))) {
2745             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2746             return;
2747         }
2748     }
2749     gen_set_access_type(ctx, ACCESS_INT);
2750     EA = tcg_temp_new();
2751     gen_addr_imm_index(ctx, EA, 0x03);
2752     if (ctx->opcode & 0x02) {
2753         /* lwa (lwau is undefined) */
2754         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2755     } else {
2756         /* ld - ldu */
2757         gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2758     }
2759     if (Rc(ctx->opcode))
2760         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2761     tcg_temp_free(EA);
2762 }
2763
2764 /* lq */
2765 static void gen_lq(DisasContext *ctx)
2766 {
2767 #if defined(CONFIG_USER_ONLY)
2768     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2769 #else
2770     int ra, rd;
2771     TCGv EA;
2772
2773     /* Restore CPU state */
2774     if (unlikely(ctx->mem_idx == 0)) {
2775         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2776         return;
2777     }
2778     ra = rA(ctx->opcode);
2779     rd = rD(ctx->opcode);
2780     if (unlikely((rd & 1) || rd == ra)) {
2781         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2782         return;
2783     }
2784     if (unlikely(ctx->le_mode)) {
2785         /* Little-endian mode is not handled */
2786         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2787         return;
2788     }
2789     gen_set_access_type(ctx, ACCESS_INT);
2790     EA = tcg_temp_new();
2791     gen_addr_imm_index(ctx, EA, 0x0F);
2792     gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2793     gen_addr_add(ctx, EA, EA, 8);
2794     gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2795     tcg_temp_free(EA);
2796 #endif
2797 }
2798 #endif
2799
2800 /***                              Integer store                            ***/
2801 #define GEN_ST(name, stop, opc, type)                                         \
2802 static void glue(gen_, name)(DisasContext *ctx)                                       \
2803 {                                                                             \
2804     TCGv EA;                                                                  \
2805     gen_set_access_type(ctx, ACCESS_INT);                                     \
2806     EA = tcg_temp_new();                                                      \
2807     gen_addr_imm_index(ctx, EA, 0);                                           \
2808     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2809     tcg_temp_free(EA);                                                        \
2810 }
2811
2812 #define GEN_STU(name, stop, opc, type)                                        \
2813 static void glue(gen_, stop##u)(DisasContext *ctx)                                    \
2814 {                                                                             \
2815     TCGv EA;                                                                  \
2816     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2817         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2818         return;                                                               \
2819     }                                                                         \
2820     gen_set_access_type(ctx, ACCESS_INT);                                     \
2821     EA = tcg_temp_new();                                                      \
2822     if (type == PPC_64B)                                                      \
2823         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2824     else                                                                      \
2825         gen_addr_imm_index(ctx, EA, 0);                                       \
2826     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2827     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2828     tcg_temp_free(EA);                                                        \
2829 }
2830
2831 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
2832 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2833 {                                                                             \
2834     TCGv EA;                                                                  \
2835     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2836         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2837         return;                                                               \
2838     }                                                                         \
2839     gen_set_access_type(ctx, ACCESS_INT);                                     \
2840     EA = tcg_temp_new();                                                      \
2841     gen_addr_reg_index(ctx, EA);                                              \
2842     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2843     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2844     tcg_temp_free(EA);                                                        \
2845 }
2846
2847 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
2848 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2849 {                                                                             \
2850     TCGv EA;                                                                  \
2851     gen_set_access_type(ctx, ACCESS_INT);                                     \
2852     EA = tcg_temp_new();                                                      \
2853     gen_addr_reg_index(ctx, EA);                                              \
2854     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2855     tcg_temp_free(EA);                                                        \
2856 }
2857 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
2858     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
2859
2860 #define GEN_STS(name, stop, op, type)                                         \
2861 GEN_ST(name, stop, op | 0x20, type);                                          \
2862 GEN_STU(name, stop, op | 0x21, type);                                         \
2863 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2864 GEN_STX(name, stop, 0x17, op | 0x00, type)
2865
2866 /* stb stbu stbux stbx */
2867 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2868 /* sth sthu sthux sthx */
2869 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2870 /* stw stwu stwux stwx */
2871 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2872 #if defined(TARGET_PPC64)
2873 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2874 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2875
2876 static void gen_std(DisasContext *ctx)
2877 {
2878     int rs;
2879     TCGv EA;
2880
2881     rs = rS(ctx->opcode);
2882     if ((ctx->opcode & 0x3) == 0x2) {
2883 #if defined(CONFIG_USER_ONLY)
2884         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2885 #else
2886         /* stq */
2887         if (unlikely(ctx->mem_idx == 0)) {
2888             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2889             return;
2890         }
2891         if (unlikely(rs & 1)) {
2892             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2893             return;
2894         }
2895         if (unlikely(ctx->le_mode)) {
2896             /* Little-endian mode is not handled */
2897             gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2898             return;
2899         }
2900         gen_set_access_type(ctx, ACCESS_INT);
2901         EA = tcg_temp_new();
2902         gen_addr_imm_index(ctx, EA, 0x03);
2903         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2904         gen_addr_add(ctx, EA, EA, 8);
2905         gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
2906         tcg_temp_free(EA);
2907 #endif
2908     } else {
2909         /* std / stdu */
2910         if (Rc(ctx->opcode)) {
2911             if (unlikely(rA(ctx->opcode) == 0)) {
2912                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2913                 return;
2914             }
2915         }
2916         gen_set_access_type(ctx, ACCESS_INT);
2917         EA = tcg_temp_new();
2918         gen_addr_imm_index(ctx, EA, 0x03);
2919         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2920         if (Rc(ctx->opcode))
2921             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2922         tcg_temp_free(EA);
2923     }
2924 }
2925 #endif
2926 /***                Integer load and store with byte reverse               ***/
2927 /* lhbrx */
2928 static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2929 {
2930     tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2931     if (likely(!ctx->le_mode)) {
2932         tcg_gen_bswap16_tl(arg1, arg1);
2933     }
2934 }
2935 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
2936
2937 /* lwbrx */
2938 static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2939 {
2940     tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2941     if (likely(!ctx->le_mode)) {
2942         tcg_gen_bswap32_tl(arg1, arg1);
2943     }
2944 }
2945 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
2946
2947 #if defined(TARGET_PPC64)
2948 /* ldbrx */
2949 static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2950 {
2951     tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2952     if (likely(!ctx->le_mode)) {
2953         tcg_gen_bswap64_tl(arg1, arg1);
2954     }
2955 }
2956 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
2957 #endif  /* TARGET_PPC64 */
2958
2959 /* sthbrx */
2960 static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2961 {
2962     if (likely(!ctx->le_mode)) {
2963         TCGv t0 = tcg_temp_new();
2964         tcg_gen_ext16u_tl(t0, arg1);
2965         tcg_gen_bswap16_tl(t0, t0);
2966         tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2967         tcg_temp_free(t0);
2968     } else {
2969         tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2970     }
2971 }
2972 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
2973
2974 /* stwbrx */
2975 static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2976 {
2977     if (likely(!ctx->le_mode)) {
2978         TCGv t0 = tcg_temp_new();
2979         tcg_gen_ext32u_tl(t0, arg1);
2980         tcg_gen_bswap32_tl(t0, t0);
2981         tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2982         tcg_temp_free(t0);
2983     } else {
2984         tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2985     }
2986 }
2987 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
2988
2989 #if defined(TARGET_PPC64)
2990 /* stdbrx */
2991 static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2992 {
2993     if (likely(!ctx->le_mode)) {
2994         TCGv t0 = tcg_temp_new();
2995         tcg_gen_bswap64_tl(t0, arg1);
2996         tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2997         tcg_temp_free(t0);
2998     } else {
2999         tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
3000     }
3001 }
3002 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3003 #endif  /* TARGET_PPC64 */
3004
3005 /***                    Integer load and store multiple                    ***/
3006
3007 /* lmw */
3008 static void gen_lmw(DisasContext *ctx)
3009 {
3010     TCGv t0;
3011     TCGv_i32 t1;
3012     gen_set_access_type(ctx, ACCESS_INT);
3013     /* NIP cannot be restored if the memory exception comes from an helper */
3014     gen_update_nip(ctx, ctx->nip - 4);
3015     t0 = tcg_temp_new();
3016     t1 = tcg_const_i32(rD(ctx->opcode));
3017     gen_addr_imm_index(ctx, t0, 0);
3018     gen_helper_lmw(cpu_env, t0, t1);
3019     tcg_temp_free(t0);
3020     tcg_temp_free_i32(t1);
3021 }
3022
3023 /* stmw */
3024 static void gen_stmw(DisasContext *ctx)
3025 {
3026     TCGv t0;
3027     TCGv_i32 t1;
3028     gen_set_access_type(ctx, ACCESS_INT);
3029     /* NIP cannot be restored if the memory exception comes from an helper */
3030     gen_update_nip(ctx, ctx->nip - 4);
3031     t0 = tcg_temp_new();
3032     t1 = tcg_const_i32(rS(ctx->opcode));
3033     gen_addr_imm_index(ctx, t0, 0);
3034     gen_helper_stmw(cpu_env, t0, t1);
3035     tcg_temp_free(t0);
3036     tcg_temp_free_i32(t1);
3037 }
3038
3039 /***                    Integer load and store strings                     ***/
3040
3041 /* lswi */
3042 /* PowerPC32 specification says we must generate an exception if
3043  * rA is in the range of registers to be loaded.
3044  * In an other hand, IBM says this is valid, but rA won't be loaded.
3045  * For now, I'll follow the spec...
3046  */
3047 static void gen_lswi(DisasContext *ctx)
3048 {
3049     TCGv t0;
3050     TCGv_i32 t1, t2;
3051     int nb = NB(ctx->opcode);
3052     int start = rD(ctx->opcode);
3053     int ra = rA(ctx->opcode);
3054     int nr;
3055
3056     if (nb == 0)
3057         nb = 32;
3058     nr = nb / 4;
3059     if (unlikely(((start + nr) > 32  &&
3060                   start <= ra && (start + nr - 32) > ra) ||
3061                  ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3062         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3063         return;
3064     }
3065     gen_set_access_type(ctx, ACCESS_INT);
3066     /* NIP cannot be restored if the memory exception comes from an helper */
3067     gen_update_nip(ctx, ctx->nip - 4);
3068     t0 = tcg_temp_new();
3069     gen_addr_register(ctx, t0);
3070     t1 = tcg_const_i32(nb);
3071     t2 = tcg_const_i32(start);
3072     gen_helper_lsw(cpu_env, t0, t1, t2);
3073     tcg_temp_free(t0);
3074     tcg_temp_free_i32(t1);
3075     tcg_temp_free_i32(t2);
3076 }
3077
3078 /* lswx */
3079 static void gen_lswx(DisasContext *ctx)
3080 {
3081     TCGv t0;
3082     TCGv_i32 t1, t2, t3;
3083     gen_set_access_type(ctx, ACCESS_INT);
3084     /* NIP cannot be restored if the memory exception comes from an helper */
3085     gen_update_nip(ctx, ctx->nip - 4);
3086     t0 = tcg_temp_new();
3087     gen_addr_reg_index(ctx, t0);
3088     t1 = tcg_const_i32(rD(ctx->opcode));
3089     t2 = tcg_const_i32(rA(ctx->opcode));
3090     t3 = tcg_const_i32(rB(ctx->opcode));
3091     gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3092     tcg_temp_free(t0);
3093     tcg_temp_free_i32(t1);
3094     tcg_temp_free_i32(t2);
3095     tcg_temp_free_i32(t3);
3096 }
3097
3098 /* stswi */
3099 static void gen_stswi(DisasContext *ctx)
3100 {
3101     TCGv t0;
3102     TCGv_i32 t1, t2;
3103     int nb = NB(ctx->opcode);
3104     gen_set_access_type(ctx, ACCESS_INT);
3105     /* NIP cannot be restored if the memory exception comes from an helper */
3106     gen_update_nip(ctx, ctx->nip - 4);
3107     t0 = tcg_temp_new();
3108     gen_addr_register(ctx, t0);
3109     if (nb == 0)
3110         nb = 32;
3111     t1 = tcg_const_i32(nb);
3112     t2 = tcg_const_i32(rS(ctx->opcode));
3113     gen_helper_stsw(cpu_env, t0, t1, t2);
3114     tcg_temp_free(t0);
3115     tcg_temp_free_i32(t1);
3116     tcg_temp_free_i32(t2);
3117 }
3118
3119 /* stswx */
3120 static void gen_stswx(DisasContext *ctx)
3121 {
3122     TCGv t0;
3123     TCGv_i32 t1, t2;
3124     gen_set_access_type(ctx, ACCESS_INT);
3125     /* NIP cannot be restored if the memory exception comes from an helper */
3126     gen_update_nip(ctx, ctx->nip - 4);
3127     t0 = tcg_temp_new();
3128     gen_addr_reg_index(ctx, t0);
3129     t1 = tcg_temp_new_i32();
3130     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3131     tcg_gen_andi_i32(t1, t1, 0x7F);
3132     t2 = tcg_const_i32(rS(ctx->opcode));
3133     gen_helper_stsw(cpu_env, t0, t1, t2);
3134     tcg_temp_free(t0);
3135     tcg_temp_free_i32(t1);
3136     tcg_temp_free_i32(t2);
3137 }
3138
3139 /***                        Memory synchronisation                         ***/
3140 /* eieio */
3141 static void gen_eieio(DisasContext *ctx)
3142 {
3143 }
3144
3145 /* isync */
3146 static void gen_isync(DisasContext *ctx)
3147 {
3148     gen_stop_exception(ctx);
3149 }
3150
3151 /* lwarx */
3152 static void gen_lwarx(DisasContext *ctx)
3153 {
3154     TCGv t0;
3155     TCGv gpr = cpu_gpr[rD(ctx->opcode)];
3156     gen_set_access_type(ctx, ACCESS_RES);
3157     t0 = tcg_temp_local_new();
3158     gen_addr_reg_index(ctx, t0);
3159     gen_check_align(ctx, t0, 0x03);
3160     gen_qemu_ld32u(ctx, gpr, t0);
3161     tcg_gen_mov_tl(cpu_reserve, t0);
3162     tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
3163     tcg_temp_free(t0);
3164 }
3165
3166 #if defined(CONFIG_USER_ONLY)
3167 static void gen_conditional_store (DisasContext *ctx, TCGv EA,
3168                                    int reg, int size)
3169 {
3170     TCGv t0 = tcg_temp_new();
3171     uint32_t save_exception = ctx->exception;
3172
3173     tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
3174     tcg_gen_movi_tl(t0, (size << 5) | reg);
3175     tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
3176     tcg_temp_free(t0);
3177     gen_update_nip(ctx, ctx->nip-4);
3178     ctx->exception = POWERPC_EXCP_BRANCH;
3179     gen_exception(ctx, POWERPC_EXCP_STCX);
3180     ctx->exception = save_exception;
3181 }
3182 #endif
3183
3184 /* stwcx. */
3185 static void gen_stwcx_(DisasContext *ctx)
3186 {
3187     TCGv t0;
3188     gen_set_access_type(ctx, ACCESS_RES);
3189     t0 = tcg_temp_local_new();
3190     gen_addr_reg_index(ctx, t0);
3191     gen_check_align(ctx, t0, 0x03);
3192 #if defined(CONFIG_USER_ONLY)
3193     gen_conditional_store(ctx, t0, rS(ctx->opcode), 4);
3194 #else
3195     {
3196         int l1;
3197
3198         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3199         l1 = gen_new_label();
3200         tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3201         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3202         gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3203         gen_set_label(l1);
3204         tcg_gen_movi_tl(cpu_reserve, -1);
3205     }
3206 #endif
3207     tcg_temp_free(t0);
3208 }
3209
3210 #if defined(TARGET_PPC64)
3211 /* ldarx */
3212 static void gen_ldarx(DisasContext *ctx)
3213 {
3214     TCGv t0;
3215     TCGv gpr = cpu_gpr[rD(ctx->opcode)];
3216     gen_set_access_type(ctx, ACCESS_RES);
3217     t0 = tcg_temp_local_new();
3218     gen_addr_reg_index(ctx, t0);
3219     gen_check_align(ctx, t0, 0x07);
3220     gen_qemu_ld64(ctx, gpr, t0);
3221     tcg_gen_mov_tl(cpu_reserve, t0);
3222     tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val));
3223     tcg_temp_free(t0);
3224 }
3225
3226 /* stdcx. */
3227 static void gen_stdcx_(DisasContext *ctx)
3228 {
3229     TCGv t0;
3230     gen_set_access_type(ctx, ACCESS_RES);
3231     t0 = tcg_temp_local_new();
3232     gen_addr_reg_index(ctx, t0);
3233     gen_check_align(ctx, t0, 0x07);
3234 #if defined(CONFIG_USER_ONLY)
3235     gen_conditional_store(ctx, t0, rS(ctx->opcode), 8);
3236 #else
3237     {
3238         int l1;
3239         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3240         l1 = gen_new_label();
3241         tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3242         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3243         gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3244         gen_set_label(l1);
3245         tcg_gen_movi_tl(cpu_reserve, -1);
3246     }
3247 #endif
3248     tcg_temp_free(t0);
3249 }
3250 #endif /* defined(TARGET_PPC64) */
3251
3252 /* sync */
3253 static void gen_sync(DisasContext *ctx)
3254 {
3255 }
3256
3257 /* wait */
3258 static void gen_wait(DisasContext *ctx)
3259 {
3260     TCGv_i32 t0 = tcg_temp_new_i32();
3261     tcg_gen_st_i32(t0, cpu_env,
3262                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3263     tcg_temp_free_i32(t0);
3264     /* Stop translation, as the CPU is supposed to sleep from now */
3265     gen_exception_err(ctx, EXCP_HLT, 1);
3266 }
3267
3268 /***                         Floating-point load                           ***/
3269 #define GEN_LDF(name, ldop, opc, type)                                        \
3270 static void glue(gen_, name)(DisasContext *ctx)                                       \
3271 {                                                                             \
3272     TCGv EA;                                                                  \
3273     if (unlikely(!ctx->fpu_enabled)) {                                        \
3274         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3275         return;                                                               \
3276     }                                                                         \
3277     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3278     EA = tcg_temp_new();                                                      \
3279     gen_addr_imm_index(ctx, EA, 0);                                           \
3280     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3281     tcg_temp_free(EA);                                                        \
3282 }
3283
3284 #define GEN_LDUF(name, ldop, opc, type)                                       \
3285 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3286 {                                                                             \
3287     TCGv EA;                                                                  \
3288     if (unlikely(!ctx->fpu_enabled)) {                                        \
3289         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3290         return;                                                               \
3291     }                                                                         \
3292     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3293         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3294         return;                                                               \
3295     }                                                                         \
3296     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3297     EA = tcg_temp_new();                                                      \
3298     gen_addr_imm_index(ctx, EA, 0);                                           \
3299     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3300     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3301     tcg_temp_free(EA);                                                        \
3302 }
3303
3304 #define GEN_LDUXF(name, ldop, opc, type)                                      \
3305 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3306 {                                                                             \
3307     TCGv EA;                                                                  \
3308     if (unlikely(!ctx->fpu_enabled)) {                                        \
3309         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3310         return;                                                               \
3311     }                                                                         \
3312     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3313         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3314         return;                                                               \
3315     }                                                                         \
3316     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3317     EA = tcg_temp_new();                                                      \
3318     gen_addr_reg_index(ctx, EA);                                              \
3319     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3320     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3321     tcg_temp_free(EA);                                                        \
3322 }
3323
3324 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3325 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3326 {                                                                             \
3327     TCGv EA;                                                                  \
3328     if (unlikely(!ctx->fpu_enabled)) {                                        \
3329         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3330         return;                                                               \
3331     }                                                                         \
3332     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3333     EA = tcg_temp_new();                                                      \
3334     gen_addr_reg_index(ctx, EA);                                              \
3335     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3336     tcg_temp_free(EA);                                                        \
3337 }
3338
3339 #define GEN_LDFS(name, ldop, op, type)                                        \
3340 GEN_LDF(name, ldop, op | 0x20, type);                                         \
3341 GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3342 GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3343 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3344
3345 static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3346 {
3347     TCGv t0 = tcg_temp_new();
3348     TCGv_i32 t1 = tcg_temp_new_i32();
3349     gen_qemu_ld32u(ctx, t0, arg2);
3350     tcg_gen_trunc_tl_i32(t1, t0);
3351     tcg_temp_free(t0);
3352     gen_helper_float32_to_float64(arg1, cpu_env, t1);
3353     tcg_temp_free_i32(t1);
3354 }
3355
3356  /* lfd lfdu lfdux lfdx */
3357 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3358  /* lfs lfsu lfsux lfsx */
3359 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3360
3361 /* lfdp */
3362 static void gen_lfdp(DisasContext *ctx)
3363 {
3364     TCGv EA;
3365     if (unlikely(!ctx->fpu_enabled)) {
3366         gen_exception(ctx, POWERPC_EXCP_FPU);
3367         return;
3368     }
3369     gen_set_access_type(ctx, ACCESS_FLOAT);
3370     EA = tcg_temp_new();
3371     gen_addr_imm_index(ctx, EA, 0);                                           \
3372     if (unlikely(ctx->le_mode)) {
3373         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3374         tcg_gen_addi_tl(EA, EA, 8);
3375         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3376     } else {
3377         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3378         tcg_gen_addi_tl(EA, EA, 8);
3379         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3380     }
3381     tcg_temp_free(EA);
3382 }
3383
3384 /* lfdpx */
3385 static void gen_lfdpx(DisasContext *ctx)
3386 {
3387     TCGv EA;
3388     if (unlikely(!ctx->fpu_enabled)) {
3389         gen_exception(ctx, POWERPC_EXCP_FPU);
3390         return;
3391     }
3392     gen_set_access_type(ctx, ACCESS_FLOAT);
3393     EA = tcg_temp_new();
3394     gen_addr_reg_index(ctx, EA);
3395     if (unlikely(ctx->le_mode)) {
3396         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3397         tcg_gen_addi_tl(EA, EA, 8);
3398         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3399     } else {
3400         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3401         tcg_gen_addi_tl(EA, EA, 8);
3402         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3403     }
3404     tcg_temp_free(EA);
3405 }
3406
3407 /* lfiwax */
3408 static void gen_lfiwax(DisasContext *ctx)
3409 {
3410     TCGv EA;
3411     TCGv t0;
3412     if (unlikely(!ctx->fpu_enabled)) {
3413         gen_exception(ctx, POWERPC_EXCP_FPU);
3414         return;
3415     }
3416     gen_set_access_type(ctx, ACCESS_FLOAT);
3417     EA = tcg_temp_new();
3418     t0 = tcg_temp_new();
3419     gen_addr_reg_index(ctx, EA);
3420     gen_qemu_ld32s(ctx, t0, EA);
3421     tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
3422     tcg_temp_free(EA);
3423     tcg_temp_free(t0);
3424 }
3425
3426 /***                         Floating-point store                          ***/
3427 #define GEN_STF(name, stop, opc, type)                                        \
3428 static void glue(gen_, name)(DisasContext *ctx)                                       \
3429 {                                                                             \
3430     TCGv EA;                                                                  \
3431     if (unlikely(!ctx->fpu_enabled)) {                                        \
3432         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3433         return;                                                               \
3434     }                                                                         \
3435     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3436     EA = tcg_temp_new();                                                      \
3437     gen_addr_imm_index(ctx, EA, 0);                                           \
3438     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3439     tcg_temp_free(EA);                                                        \
3440 }
3441
3442 #define GEN_STUF(name, stop, opc, type)                                       \
3443 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3444 {                                                                             \
3445     TCGv EA;                                                                  \
3446     if (unlikely(!ctx->fpu_enabled)) {                                        \
3447         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3448         return;                                                               \
3449     }                                                                         \
3450     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3451         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3452         return;                                                               \
3453     }                                                                         \
3454     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3455     EA = tcg_temp_new();                                                      \
3456     gen_addr_imm_index(ctx, EA, 0);                                           \
3457     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3458     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3459     tcg_temp_free(EA);                                                        \
3460 }
3461
3462 #define GEN_STUXF(name, stop, opc, type)                                      \
3463 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3464 {                                                                             \
3465     TCGv EA;                                                                  \
3466     if (unlikely(!ctx->fpu_enabled)) {                                        \
3467         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3468         return;                                                               \
3469     }                                                                         \
3470     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3471         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3472         return;                                                               \
3473     }                                                                         \
3474     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3475     EA = tcg_temp_new();                                                      \
3476     gen_addr_reg_index(ctx, EA);                                              \
3477     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3478     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3479     tcg_temp_free(EA);                                                        \
3480 }
3481
3482 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
3483 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3484 {                                                                             \
3485     TCGv EA;                                                                  \
3486     if (unlikely(!ctx->fpu_enabled)) {                                        \
3487         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3488         return;                                                               \
3489     }                                                                         \
3490     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3491     EA = tcg_temp_new();                                                      \
3492     gen_addr_reg_index(ctx, EA);                                              \
3493     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3494     tcg_temp_free(EA);                                                        \
3495 }
3496
3497 #define GEN_STFS(name, stop, op, type)                                        \
3498 GEN_STF(name, stop, op | 0x20, type);                                         \
3499 GEN_STUF(name, stop, op | 0x21, type);                                        \
3500 GEN_STUXF(name, stop, op | 0x01, type);                                       \
3501 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3502
3503 static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3504 {
3505     TCGv_i32 t0 = tcg_temp_new_i32();
3506     TCGv t1 = tcg_temp_new();
3507     gen_helper_float64_to_float32(t0, cpu_env, arg1);
3508     tcg_gen_extu_i32_tl(t1, t0);
3509     tcg_temp_free_i32(t0);
3510     gen_qemu_st32(ctx, t1, arg2);
3511     tcg_temp_free(t1);
3512 }
3513
3514 /* stfd stfdu stfdux stfdx */
3515 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3516 /* stfs stfsu stfsux stfsx */
3517 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3518
3519 /* stfdp */
3520 static void gen_stfdp(DisasContext *ctx)
3521 {
3522     TCGv EA;
3523     if (unlikely(!ctx->fpu_enabled)) {
3524         gen_exception(ctx, POWERPC_EXCP_FPU);
3525         return;
3526     }
3527     gen_set_access_type(ctx, ACCESS_FLOAT);
3528     EA = tcg_temp_new();
3529     gen_addr_imm_index(ctx, EA, 0);                                           \
3530     if (unlikely(ctx->le_mode)) {
3531         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3532         tcg_gen_addi_tl(EA, EA, 8);
3533         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3534     } else {
3535         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3536         tcg_gen_addi_tl(EA, EA, 8);
3537         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3538     }
3539     tcg_temp_free(EA);
3540 }
3541
3542 /* stfdpx */
3543 static void gen_stfdpx(DisasContext *ctx)
3544 {
3545     TCGv EA;
3546     if (unlikely(!ctx->fpu_enabled)) {
3547         gen_exception(ctx, POWERPC_EXCP_FPU);
3548         return;
3549     }
3550     gen_set_access_type(ctx, ACCESS_FLOAT);
3551     EA = tcg_temp_new();
3552     gen_addr_reg_index(ctx, EA);
3553     if (unlikely(ctx->le_mode)) {
3554         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3555         tcg_gen_addi_tl(EA, EA, 8);
3556         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3557     } else {
3558         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3559         tcg_gen_addi_tl(EA, EA, 8);
3560         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3561     }
3562     tcg_temp_free(EA);
3563 }
3564
3565 /* Optional: */
3566 static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3567 {
3568     TCGv t0 = tcg_temp_new();
3569     tcg_gen_trunc_i64_tl(t0, arg1),
3570     gen_qemu_st32(ctx, t0, arg2);
3571     tcg_temp_free(t0);
3572 }
3573 /* stfiwx */
3574 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3575
3576 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3577 {
3578 #if defined(TARGET_PPC64)
3579     if (ctx->has_cfar)
3580         tcg_gen_movi_tl(cpu_cfar, nip);
3581 #endif
3582 }
3583
3584 /***                                Branch                                 ***/
3585 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
3586 {
3587     TranslationBlock *tb;
3588     tb = ctx->tb;
3589     if (NARROW_MODE(ctx)) {
3590         dest = (uint32_t) dest;
3591     }
3592     if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3593         likely(!ctx->singlestep_enabled)) {
3594         tcg_gen_goto_tb(n);
3595         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3596         tcg_gen_exit_tb((uintptr_t)tb + n);
3597     } else {
3598         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3599         if (unlikely(ctx->singlestep_enabled)) {
3600             if ((ctx->singlestep_enabled &
3601                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3602                 (ctx->exception == POWERPC_EXCP_BRANCH ||
3603                  ctx->exception == POWERPC_EXCP_TRACE)) {
3604                 target_ulong tmp = ctx->nip;
3605                 ctx->nip = dest;
3606                 gen_exception(ctx, POWERPC_EXCP_TRACE);
3607                 ctx->nip = tmp;
3608             }
3609             if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3610                 gen_debug_exception(ctx);
3611             }
3612         }
3613         tcg_gen_exit_tb(0);
3614     }
3615 }
3616
3617 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
3618 {
3619     if (NARROW_MODE(ctx)) {
3620         nip = (uint32_t)nip;
3621     }
3622     tcg_gen_movi_tl(cpu_lr, nip);
3623 }
3624
3625 /* b ba bl bla */
3626 static void gen_b(DisasContext *ctx)
3627 {
3628     target_ulong li, target;
3629
3630     ctx->exception = POWERPC_EXCP_BRANCH;
3631     /* sign extend LI */
3632     li = LI(ctx->opcode);
3633     li = (li ^ 0x02000000) - 0x02000000;
3634     if (likely(AA(ctx->opcode) == 0)) {
3635         target = ctx->nip + li - 4;
3636     } else {
3637         target = li;
3638     }
3639     if (LK(ctx->opcode)) {
3640         gen_setlr(ctx, ctx->nip);
3641     }
3642     gen_update_cfar(ctx, ctx->nip);
3643     gen_goto_tb(ctx, 0, target);
3644 }
3645
3646 #define BCOND_IM  0
3647 #define BCOND_LR  1
3648 #define BCOND_CTR 2
3649
3650 static inline void gen_bcond(DisasContext *ctx, int type)
3651 {
3652     uint32_t bo = BO(ctx->opcode);
3653     int l1;
3654     TCGv target;
3655
3656     ctx->exception = POWERPC_EXCP_BRANCH;
3657     if (type == BCOND_LR || type == BCOND_CTR) {
3658         target = tcg_temp_local_new();
3659         if (type == BCOND_CTR)
3660             tcg_gen_mov_tl(target, cpu_ctr);
3661         else
3662             tcg_gen_mov_tl(target, cpu_lr);
3663     } else {
3664         TCGV_UNUSED(target);
3665     }
3666     if (LK(ctx->opcode))
3667         gen_setlr(ctx, ctx->nip);
3668     l1 = gen_new_label();
3669     if ((bo & 0x4) == 0) {
3670         /* Decrement and test CTR */
3671         TCGv temp = tcg_temp_new();
3672         if (unlikely(type == BCOND_CTR)) {
3673             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3674             return;
3675         }
3676         tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3677         if (NARROW_MODE(ctx)) {
3678             tcg_gen_ext32u_tl(temp, cpu_ctr);
3679         } else {
3680             tcg_gen_mov_tl(temp, cpu_ctr);
3681         }
3682         if (bo & 0x2) {
3683             tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3684         } else {
3685             tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3686         }
3687         tcg_temp_free(temp);
3688     }
3689     if ((bo & 0x10) == 0) {
3690         /* Test CR */
3691         uint32_t bi = BI(ctx->opcode);
3692         uint32_t mask = 1 << (3 - (bi & 0x03));
3693         TCGv_i32 temp = tcg_temp_new_i32();
3694
3695         if (bo & 0x8) {
3696             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3697             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3698         } else {
3699             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3700             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3701         }
3702         tcg_temp_free_i32(temp);
3703     }
3704     gen_update_cfar(ctx, ctx->nip);
3705     if (type == BCOND_IM) {
3706         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3707         if (likely(AA(ctx->opcode) == 0)) {
3708             gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3709         } else {
3710             gen_goto_tb(ctx, 0, li);
3711         }
3712         gen_set_label(l1);
3713         gen_goto_tb(ctx, 1, ctx->nip);
3714     } else {
3715         if (NARROW_MODE(ctx)) {
3716             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3717         } else {
3718             tcg_gen_andi_tl(cpu_nip, target, ~3);
3719         }
3720         tcg_gen_exit_tb(0);
3721         gen_set_label(l1);
3722         gen_update_nip(ctx, ctx->nip);
3723         tcg_gen_exit_tb(0);
3724     }
3725 }
3726
3727 static void gen_bc(DisasContext *ctx)
3728 {
3729     gen_bcond(ctx, BCOND_IM);
3730 }
3731
3732 static void gen_bcctr(DisasContext *ctx)
3733 {
3734     gen_bcond(ctx, BCOND_CTR);
3735 }
3736
3737 static void gen_bclr(DisasContext *ctx)
3738 {
3739     gen_bcond(ctx, BCOND_LR);
3740 }
3741
3742 /***                      Condition register logical                       ***/
3743 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3744 static void glue(gen_, name)(DisasContext *ctx)                                       \
3745 {                                                                             \
3746     uint8_t bitmask;                                                          \
3747     int sh;                                                                   \
3748     TCGv_i32 t0, t1;                                                          \
3749     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3750     t0 = tcg_temp_new_i32();                                                  \
3751     if (sh > 0)                                                               \
3752         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3753     else if (sh < 0)                                                          \
3754         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3755     else                                                                      \
3756         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3757     t1 = tcg_temp_new_i32();                                                  \
3758     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3759     if (sh > 0)                                                               \
3760         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3761     else if (sh < 0)                                                          \
3762         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3763     else                                                                      \
3764         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3765     tcg_op(t0, t0, t1);                                                       \
3766     bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3767     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3768     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3769     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3770     tcg_temp_free_i32(t0);                                                    \
3771     tcg_temp_free_i32(t1);                                                    \
3772 }
3773
3774 /* crand */
3775 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3776 /* crandc */
3777 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3778 /* creqv */
3779 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3780 /* crnand */
3781 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3782 /* crnor */
3783 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3784 /* cror */
3785 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3786 /* crorc */
3787 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3788 /* crxor */
3789 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3790
3791 /* mcrf */
3792 static void gen_mcrf(DisasContext *ctx)
3793 {
3794     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3795 }
3796
3797 /***                           System linkage                              ***/
3798
3799 /* rfi (mem_idx only) */
3800 static void gen_rfi(DisasContext *ctx)
3801 {
3802 #if defined(CONFIG_USER_ONLY)
3803     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3804 #else
3805     /* Restore CPU state */
3806     if (unlikely(!ctx->mem_idx)) {
3807         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3808         return;
3809     }
3810     gen_update_cfar(ctx, ctx->nip);
3811     gen_helper_rfi(cpu_env);
3812     gen_sync_exception(ctx);
3813 #endif
3814 }
3815
3816 #if defined(TARGET_PPC64)
3817 static void gen_rfid(DisasContext *ctx)
3818 {
3819 #if defined(CONFIG_USER_ONLY)
3820     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3821 #else
3822     /* Restore CPU state */
3823     if (unlikely(!ctx->mem_idx)) {
3824         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3825         return;
3826     }
3827     gen_update_cfar(ctx, ctx->nip);
3828     gen_helper_rfid(cpu_env);
3829     gen_sync_exception(ctx);
3830 #endif
3831 }
3832
3833 static void gen_hrfid(DisasContext *ctx)
3834 {
3835 #if defined(CONFIG_USER_ONLY)
3836     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3837 #else
3838     /* Restore CPU state */
3839     if (unlikely(ctx->mem_idx <= 1)) {
3840         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3841         return;
3842     }
3843     gen_helper_hrfid(cpu_env);
3844     gen_sync_exception(ctx);
3845 #endif
3846 }
3847 #endif
3848
3849 /* sc */
3850 #if defined(CONFIG_USER_ONLY)
3851 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3852 #else
3853 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3854 #endif
3855 static void gen_sc(DisasContext *ctx)
3856 {
3857     uint32_t lev;
3858
3859     lev = (ctx->opcode >> 5) & 0x7F;
3860     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
3861 }
3862
3863 /***                                Trap                                   ***/
3864
3865 /* tw */
3866 static void gen_tw(DisasContext *ctx)
3867 {
3868     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3869     /* Update the nip since this might generate a trap exception */
3870     gen_update_nip(ctx, ctx->nip);
3871     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3872                   t0);
3873     tcg_temp_free_i32(t0);
3874 }
3875
3876 /* twi */
3877 static void gen_twi(DisasContext *ctx)
3878 {
3879     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3880     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3881     /* Update the nip since this might generate a trap exception */
3882     gen_update_nip(ctx, ctx->nip);
3883     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
3884     tcg_temp_free(t0);
3885     tcg_temp_free_i32(t1);
3886 }
3887
3888 #if defined(TARGET_PPC64)
3889 /* td */
3890 static void gen_td(DisasContext *ctx)
3891 {
3892     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3893     /* Update the nip since this might generate a trap exception */
3894     gen_update_nip(ctx, ctx->nip);
3895     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
3896                   t0);
3897     tcg_temp_free_i32(t0);
3898 }
3899
3900 /* tdi */
3901 static void gen_tdi(DisasContext *ctx)
3902 {
3903     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3904     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3905     /* Update the nip since this might generate a trap exception */
3906     gen_update_nip(ctx, ctx->nip);
3907     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
3908     tcg_temp_free(t0);
3909     tcg_temp_free_i32(t1);
3910 }
3911 #endif
3912
3913 /***                          Processor control                            ***/
3914
3915 static void gen_read_xer(TCGv dst)
3916 {
3917     TCGv t0 = tcg_temp_new();
3918     TCGv t1 = tcg_temp_new();
3919     TCGv t2 = tcg_temp_new();
3920     tcg_gen_mov_tl(dst, cpu_xer);
3921     tcg_gen_shli_tl(t0, cpu_so, XER_SO);
3922     tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
3923     tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
3924     tcg_gen_or_tl(t0, t0, t1);
3925     tcg_gen_or_tl(dst, dst, t2);
3926     tcg_gen_or_tl(dst, dst, t0);
3927     tcg_temp_free(t0);
3928     tcg_temp_free(t1);
3929     tcg_temp_free(t2);
3930 }
3931
3932 static void gen_write_xer(TCGv src)
3933 {
3934     tcg_gen_andi_tl(cpu_xer, src,
3935                     ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
3936     tcg_gen_shri_tl(cpu_so, src, XER_SO);
3937     tcg_gen_shri_tl(cpu_ov, src, XER_OV);
3938     tcg_gen_shri_tl(cpu_ca, src, XER_CA);
3939     tcg_gen_andi_tl(cpu_so, cpu_so, 1);
3940     tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
3941     tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
3942 }
3943
3944 /* mcrxr */
3945 static void gen_mcrxr(DisasContext *ctx)
3946 {
3947     TCGv_i32 t0 = tcg_temp_new_i32();
3948     TCGv_i32 t1 = tcg_temp_new_i32();
3949     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
3950
3951     tcg_gen_trunc_tl_i32(t0, cpu_so);
3952     tcg_gen_trunc_tl_i32(t1, cpu_ov);
3953     tcg_gen_trunc_tl_i32(dst, cpu_ca);
3954     tcg_gen_shri_i32(t0, t0, 2);
3955     tcg_gen_shri_i32(t1, t1, 1);
3956     tcg_gen_or_i32(dst, dst, t0);
3957     tcg_gen_or_i32(dst, dst, t1);
3958     tcg_temp_free_i32(t0);
3959     tcg_temp_free_i32(t1);
3960
3961     tcg_gen_movi_tl(cpu_so, 0);
3962     tcg_gen_movi_tl(cpu_ov, 0);
3963     tcg_gen_movi_tl(cpu_ca, 0);
3964 }
3965
3966 /* mfcr mfocrf */
3967 static void gen_mfcr(DisasContext *ctx)
3968 {
3969     uint32_t crm, crn;
3970
3971     if (likely(ctx->opcode & 0x00100000)) {
3972         crm = CRM(ctx->opcode);
3973         if (likely(crm && ((crm & (crm - 1)) == 0))) {
3974             crn = ctz32 (crm);
3975             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3976             tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
3977                             cpu_gpr[rD(ctx->opcode)], crn * 4);
3978         }
3979     } else {
3980         TCGv_i32 t0 = tcg_temp_new_i32();
3981         tcg_gen_mov_i32(t0, cpu_crf[0]);
3982         tcg_gen_shli_i32(t0, t0, 4);
3983         tcg_gen_or_i32(t0, t0, cpu_crf[1]);
3984         tcg_gen_shli_i32(t0, t0, 4);
3985         tcg_gen_or_i32(t0, t0, cpu_crf[2]);
3986         tcg_gen_shli_i32(t0, t0, 4);
3987         tcg_gen_or_i32(t0, t0, cpu_crf[3]);
3988         tcg_gen_shli_i32(t0, t0, 4);
3989         tcg_gen_or_i32(t0, t0, cpu_crf[4]);
3990         tcg_gen_shli_i32(t0, t0, 4);
3991         tcg_gen_or_i32(t0, t0, cpu_crf[5]);
3992         tcg_gen_shli_i32(t0, t0, 4);
3993         tcg_gen_or_i32(t0, t0, cpu_crf[6]);
3994         tcg_gen_shli_i32(t0, t0, 4);
3995         tcg_gen_or_i32(t0, t0, cpu_crf[7]);
3996         tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
3997         tcg_temp_free_i32(t0);
3998     }
3999 }
4000
4001 /* mfmsr */
4002 static void gen_mfmsr(DisasContext *ctx)
4003 {
4004 #if defined(CONFIG_USER_ONLY)
4005     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4006 #else
4007     if (unlikely(!ctx->mem_idx)) {
4008         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4009         return;
4010     }
4011     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4012 #endif
4013 }
4014
4015 static void spr_noaccess(void *opaque, int gprn, int sprn)
4016 {
4017 #if 0
4018     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4019     printf("ERROR: try to access SPR %d !\n", sprn);
4020 #endif
4021 }
4022 #define SPR_NOACCESS (&spr_noaccess)
4023
4024 /* mfspr */
4025 static inline void gen_op_mfspr(DisasContext *ctx)
4026 {
4027     void (*read_cb)(void *opaque, int gprn, int sprn);
4028     uint32_t sprn = SPR(ctx->opcode);
4029
4030 #if !defined(CONFIG_USER_ONLY)
4031     if (ctx->mem_idx == 2)
4032         read_cb = ctx->spr_cb[sprn].hea_read;
4033     else if (ctx->mem_idx)
4034         read_cb = ctx->spr_cb[sprn].oea_read;
4035     else
4036 #endif
4037         read_cb = ctx->spr_cb[sprn].uea_read;
4038     if (likely(read_cb != NULL)) {
4039         if (likely(read_cb != SPR_NOACCESS)) {
4040             (*read_cb)(ctx, rD(ctx->opcode), sprn);
4041         } else {
4042             /* Privilege exception */
4043             /* This is a hack to avoid warnings when running Linux:
4044              * this OS breaks the PowerPC virtualisation model,
4045              * allowing userland application to read the PVR
4046              */
4047             if (sprn != SPR_PVR) {
4048                 qemu_log("Trying to read privileged spr %d (0x%03x) at "
4049                          TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4050                 printf("Trying to read privileged spr %d (0x%03x) at "
4051                        TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4052             }
4053             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4054         }
4055     } else {
4056         /* Not defined */
4057         qemu_log("Trying to read invalid spr %d (0x%03x) at "
4058                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4059         printf("Trying to read invalid spr %d (0x%03x) at "
4060                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4061         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4062     }
4063 }
4064
4065 static void gen_mfspr(DisasContext *ctx)
4066 {
4067     gen_op_mfspr(ctx);
4068 }
4069
4070 /* mftb */
4071 static void gen_mftb(DisasContext *ctx)
4072 {
4073     gen_op_mfspr(ctx);
4074 }
4075
4076 /* mtcrf mtocrf*/
4077 static void gen_mtcrf(DisasContext *ctx)
4078 {
4079     uint32_t crm, crn;
4080
4081     crm = CRM(ctx->opcode);
4082     if (likely((ctx->opcode & 0x00100000))) {
4083         if (crm && ((crm & (crm - 1)) == 0)) {
4084             TCGv_i32 temp = tcg_temp_new_i32();
4085             crn = ctz32 (crm);
4086             tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4087             tcg_gen_shri_i32(temp, temp, crn * 4);
4088             tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4089             tcg_temp_free_i32(temp);
4090         }
4091     } else {
4092         TCGv_i32 temp = tcg_temp_new_i32();
4093         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4094         for (crn = 0 ; crn < 8 ; crn++) {
4095             if (crm & (1 << crn)) {
4096                     tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4097                     tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4098             }
4099         }
4100         tcg_temp_free_i32(temp);
4101     }
4102 }
4103
4104 /* mtmsr */
4105 #if defined(TARGET_PPC64)
4106 static void gen_mtmsrd(DisasContext *ctx)
4107 {
4108 #if defined(CONFIG_USER_ONLY)
4109     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4110 #else
4111     if (unlikely(!ctx->mem_idx)) {
4112         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4113         return;
4114     }
4115     if (ctx->opcode & 0x00010000) {
4116         /* Special form that does not need any synchronisation */
4117         TCGv t0 = tcg_temp_new();
4118         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4119         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4120         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4121         tcg_temp_free(t0);
4122     } else {
4123         /* XXX: we need to update nip before the store
4124          *      if we enter power saving mode, we will exit the loop
4125          *      directly from ppc_store_msr
4126          */
4127         gen_update_nip(ctx, ctx->nip);
4128         gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4129         /* Must stop the translation as machine state (may have) changed */
4130         /* Note that mtmsr is not always defined as context-synchronizing */
4131         gen_stop_exception(ctx);
4132     }
4133 #endif
4134 }
4135 #endif
4136
4137 static void gen_mtmsr(DisasContext *ctx)
4138 {
4139 #if defined(CONFIG_USER_ONLY)
4140     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4141 #else
4142     if (unlikely(!ctx->mem_idx)) {
4143         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4144         return;
4145     }
4146     if (ctx->opcode & 0x00010000) {
4147         /* Special form that does not need any synchronisation */
4148         TCGv t0 = tcg_temp_new();
4149         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4150         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4151         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4152         tcg_temp_free(t0);
4153     } else {
4154         TCGv msr = tcg_temp_new();
4155
4156         /* XXX: we need to update nip before the store
4157          *      if we enter power saving mode, we will exit the loop
4158          *      directly from ppc_store_msr
4159          */
4160         gen_update_nip(ctx, ctx->nip);
4161 #if defined(TARGET_PPC64)
4162         tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4163 #else
4164         tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4165 #endif
4166         gen_helper_store_msr(cpu_env, msr);
4167         /* Must stop the translation as machine state (may have) changed */
4168         /* Note that mtmsr is not always defined as context-synchronizing */
4169         gen_stop_exception(ctx);
4170     }
4171 #endif
4172 }
4173
4174 /* mtspr */
4175 static void gen_mtspr(DisasContext *ctx)
4176 {
4177     void (*write_cb)(void *opaque, int sprn, int gprn);
4178     uint32_t sprn = SPR(ctx->opcode);
4179
4180 #if !defined(CONFIG_USER_ONLY)
4181     if (ctx->mem_idx == 2)
4182         write_cb = ctx->spr_cb[sprn].hea_write;
4183     else if (ctx->mem_idx)
4184         write_cb = ctx->spr_cb[sprn].oea_write;
4185     else
4186 #endif
4187         write_cb = ctx->spr_cb[sprn].uea_write;
4188     if (likely(write_cb != NULL)) {
4189         if (likely(write_cb != SPR_NOACCESS)) {
4190             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4191         } else {
4192             /* Privilege exception */
4193             qemu_log("Trying to write privileged spr %d (0x%03x) at "
4194                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4195             printf("Trying to write privileged spr %d (0x%03x) at "
4196                    TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4197             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4198         }
4199     } else {
4200         /* Not defined */
4201         qemu_log("Trying to write invalid spr %d (0x%03x) at "
4202                  TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4203         printf("Trying to write invalid spr %d (0x%03x) at "
4204                TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4205         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4206     }
4207 }
4208
4209 /***                         Cache management                              ***/
4210
4211 /* dcbf */
4212 static void gen_dcbf(DisasContext *ctx)
4213 {
4214     /* XXX: specification says this is treated as a load by the MMU */
4215     TCGv t0;
4216     gen_set_access_type(ctx, ACCESS_CACHE);
4217     t0 = tcg_temp_new();
4218     gen_addr_reg_index(ctx, t0);
4219     gen_qemu_ld8u(ctx, t0, t0);
4220     tcg_temp_free(t0);
4221 }
4222
4223 /* dcbi (Supervisor only) */
4224 static void gen_dcbi(DisasContext *ctx)
4225 {
4226 #if defined(CONFIG_USER_ONLY)
4227     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4228 #else
4229     TCGv EA, val;
4230     if (unlikely(!ctx->mem_idx)) {
4231         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4232         return;
4233     }
4234     EA = tcg_temp_new();
4235     gen_set_access_type(ctx, ACCESS_CACHE);
4236     gen_addr_reg_index(ctx, EA);
4237     val = tcg_temp_new();
4238     /* XXX: specification says this should be treated as a store by the MMU */
4239     gen_qemu_ld8u(ctx, val, EA);
4240     gen_qemu_st8(ctx, val, EA);
4241     tcg_temp_free(val);
4242     tcg_temp_free(EA);
4243 #endif
4244 }
4245
4246 /* dcdst */
4247 static void gen_dcbst(DisasContext *ctx)
4248 {
4249     /* XXX: specification say this is treated as a load by the MMU */
4250     TCGv t0;
4251     gen_set_access_type(ctx, ACCESS_CACHE);
4252     t0 = tcg_temp_new();
4253     gen_addr_reg_index(ctx, t0);
4254     gen_qemu_ld8u(ctx, t0, t0);
4255     tcg_temp_free(t0);
4256 }
4257
4258 /* dcbt */
4259 static void gen_dcbt(DisasContext *ctx)
4260 {
4261     /* interpreted as no-op */
4262     /* XXX: specification say this is treated as a load by the MMU
4263      *      but does not generate any exception
4264      */
4265 }
4266
4267 /* dcbtst */
4268 static void gen_dcbtst(DisasContext *ctx)
4269 {
4270     /* interpreted as no-op */
4271     /* XXX: specification say this is treated as a load by the MMU
4272      *      but does not generate any exception
4273      */
4274 }
4275
4276 /* dcbz */
4277 static void gen_dcbz(DisasContext *ctx)
4278 {
4279     TCGv tcgv_addr;
4280     TCGv_i32 tcgv_is_dcbzl;
4281     int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
4282
4283     gen_set_access_type(ctx, ACCESS_CACHE);
4284     /* NIP cannot be restored if the memory exception comes from an helper */
4285     gen_update_nip(ctx, ctx->nip - 4);
4286     tcgv_addr = tcg_temp_new();
4287     tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4288
4289     gen_addr_reg_index(ctx, tcgv_addr);
4290     gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4291
4292     tcg_temp_free(tcgv_addr);
4293     tcg_temp_free_i32(tcgv_is_dcbzl);
4294 }
4295
4296 /* dst / dstt */
4297 static void gen_dst(DisasContext *ctx)
4298 {
4299     if (rA(ctx->opcode) == 0) {
4300         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4301     } else {
4302         /* interpreted as no-op */
4303     }
4304 }
4305
4306 /* dstst /dststt */
4307 static void gen_dstst(DisasContext *ctx)
4308 {
4309     if (rA(ctx->opcode) == 0) {
4310         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4311     } else {
4312         /* interpreted as no-op */
4313     }
4314
4315 }
4316
4317 /* dss / dssall */
4318 static void gen_dss(DisasContext *ctx)
4319 {
4320     /* interpreted as no-op */
4321 }
4322
4323 /* icbi */
4324 static void gen_icbi(DisasContext *ctx)
4325 {
4326     TCGv t0;
4327     gen_set_access_type(ctx, ACCESS_CACHE);
4328     /* NIP cannot be restored if the memory exception comes from an helper */
4329     gen_update_nip(ctx, ctx->nip - 4);
4330     t0 = tcg_temp_new();
4331     gen_addr_reg_index(ctx, t0);
4332     gen_helper_icbi(cpu_env, t0);
4333     tcg_temp_free(t0);
4334 }
4335
4336 /* Optional: */
4337 /* dcba */
4338 static void gen_dcba(DisasContext *ctx)
4339 {
4340     /* interpreted as no-op */
4341     /* XXX: specification say this is treated as a store by the MMU
4342      *      but does not generate any exception
4343      */
4344 }
4345
4346 /***                    Segment register manipulation                      ***/
4347 /* Supervisor only: */
4348
4349 /* mfsr */
4350 static void gen_mfsr(DisasContext *ctx)
4351 {
4352 #if defined(CONFIG_USER_ONLY)
4353     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4354 #else
4355     TCGv t0;
4356     if (unlikely(!ctx->mem_idx)) {
4357         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4358         return;
4359     }
4360     t0 = tcg_const_tl(SR(ctx->opcode));
4361     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4362     tcg_temp_free(t0);
4363 #endif
4364 }
4365
4366 /* mfsrin */
4367 static void gen_mfsrin(DisasContext *ctx)
4368 {
4369 #if defined(CONFIG_USER_ONLY)
4370     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4371 #else
4372     TCGv t0;
4373     if (unlikely(!ctx->mem_idx)) {
4374         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4375         return;
4376     }
4377     t0 = tcg_temp_new();
4378     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4379     tcg_gen_andi_tl(t0, t0, 0xF);
4380     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4381     tcg_temp_free(t0);
4382 #endif
4383 }
4384
4385 /* mtsr */
4386 static void gen_mtsr(DisasContext *ctx)
4387 {
4388 #if defined(CONFIG_USER_ONLY)
4389     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4390 #else
4391     TCGv t0;
4392     if (unlikely(!ctx->mem_idx)) {
4393         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4394         return;
4395     }
4396     t0 = tcg_const_tl(SR(ctx->opcode));
4397     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4398     tcg_temp_free(t0);
4399 #endif
4400 }
4401
4402 /* mtsrin */
4403 static void gen_mtsrin(DisasContext *ctx)
4404 {
4405 #if defined(CONFIG_USER_ONLY)
4406     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4407 #else
4408     TCGv t0;
4409     if (unlikely(!ctx->mem_idx)) {
4410         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4411         return;
4412     }
4413     t0 = tcg_temp_new();
4414     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4415     tcg_gen_andi_tl(t0, t0, 0xF);
4416     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
4417     tcg_temp_free(t0);
4418 #endif
4419 }
4420
4421 #if defined(TARGET_PPC64)
4422 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4423
4424 /* mfsr */
4425 static void gen_mfsr_64b(DisasContext *ctx)
4426 {
4427 #if defined(CONFIG_USER_ONLY)
4428     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4429 #else
4430     TCGv t0;
4431     if (unlikely(!ctx->mem_idx)) {
4432         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4433         return;
4434     }
4435     t0 = tcg_const_tl(SR(ctx->opcode));
4436     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4437     tcg_temp_free(t0);
4438 #endif
4439 }
4440
4441 /* mfsrin */
4442 static void gen_mfsrin_64b(DisasContext *ctx)
4443 {
4444 #if defined(CONFIG_USER_ONLY)
4445     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4446 #else
4447     TCGv t0;
4448     if (unlikely(!ctx->mem_idx)) {
4449         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4450         return;
4451     }
4452     t0 = tcg_temp_new();
4453     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4454     tcg_gen_andi_tl(t0, t0, 0xF);
4455     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4456     tcg_temp_free(t0);
4457 #endif
4458 }
4459
4460 /* mtsr */
4461 static void gen_mtsr_64b(DisasContext *ctx)
4462 {
4463 #if defined(CONFIG_USER_ONLY)
4464     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4465 #else
4466     TCGv t0;
4467     if (unlikely(!ctx->mem_idx)) {
4468         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4469         return;
4470     }
4471     t0 = tcg_const_tl(SR(ctx->opcode));
4472     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4473     tcg_temp_free(t0);
4474 #endif
4475 }
4476
4477 /* mtsrin */
4478 static void gen_mtsrin_64b(DisasContext *ctx)
4479 {
4480 #if defined(CONFIG_USER_ONLY)
4481     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4482 #else
4483     TCGv t0;
4484     if (unlikely(!ctx->mem_idx)) {
4485         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4486         return;
4487     }
4488     t0 = tcg_temp_new();
4489     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4490     tcg_gen_andi_tl(t0, t0, 0xF);
4491     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4492     tcg_temp_free(t0);
4493 #endif
4494 }
4495
4496 /* slbmte */
4497 static void gen_slbmte(DisasContext *ctx)
4498 {
4499 #if defined(CONFIG_USER_ONLY)
4500     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4501 #else
4502     if (unlikely(!ctx->mem_idx)) {
4503         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4504         return;
4505     }
4506     gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4507                          cpu_gpr[rS(ctx->opcode)]);
4508 #endif
4509 }
4510
4511 static void gen_slbmfee(DisasContext *ctx)
4512 {
4513 #if defined(CONFIG_USER_ONLY)
4514     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4515 #else
4516     if (unlikely(!ctx->mem_idx)) {
4517         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4518         return;
4519     }
4520     gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4521                              cpu_gpr[rB(ctx->opcode)]);
4522 #endif
4523 }
4524
4525 static void gen_slbmfev(DisasContext *ctx)
4526 {
4527 #if defined(CONFIG_USER_ONLY)
4528     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4529 #else
4530     if (unlikely(!ctx->mem_idx)) {
4531         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4532         return;
4533     }
4534     gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4535                              cpu_gpr[rB(ctx->opcode)]);
4536 #endif
4537 }
4538 #endif /* defined(TARGET_PPC64) */
4539
4540 /***                      Lookaside buffer management                      ***/
4541 /* Optional & mem_idx only: */
4542
4543 /* tlbia */
4544 static void gen_tlbia(DisasContext *ctx)
4545 {
4546 #if defined(CONFIG_USER_ONLY)
4547     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4548 #else
4549     if (unlikely(!ctx->mem_idx)) {
4550         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4551         return;
4552     }
4553     gen_helper_tlbia(cpu_env);
4554 #endif
4555 }
4556
4557 /* tlbiel */
4558 static void gen_tlbiel(DisasContext *ctx)
4559 {
4560 #if defined(CONFIG_USER_ONLY)
4561     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4562 #else
4563     if (unlikely(!ctx->mem_idx)) {
4564         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4565         return;
4566     }
4567     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4568 #endif
4569 }
4570
4571 /* tlbie */
4572 static void gen_tlbie(DisasContext *ctx)
4573 {
4574 #if defined(CONFIG_USER_ONLY)
4575     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4576 #else
4577     if (unlikely(!ctx->mem_idx)) {
4578         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4579         return;
4580     }
4581     if (NARROW_MODE(ctx)) {
4582         TCGv t0 = tcg_temp_new();
4583         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4584         gen_helper_tlbie(cpu_env, t0);
4585         tcg_temp_free(t0);
4586     } else {
4587         gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4588     }
4589 #endif
4590 }
4591
4592 /* tlbsync */
4593 static void gen_tlbsync(DisasContext *ctx)
4594 {
4595 #if defined(CONFIG_USER_ONLY)
4596     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4597 #else
4598     if (unlikely(!ctx->mem_idx)) {
4599         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4600         return;
4601     }
4602     /* This has no effect: it should ensure that all previous
4603      * tlbie have completed
4604      */
4605     gen_stop_exception(ctx);
4606 #endif
4607 }
4608
4609 #if defined(TARGET_PPC64)
4610 /* slbia */
4611 static void gen_slbia(DisasContext *ctx)
4612 {
4613 #if defined(CONFIG_USER_ONLY)
4614     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4615 #else
4616     if (unlikely(!ctx->mem_idx)) {
4617         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4618         return;
4619     }
4620     gen_helper_slbia(cpu_env);
4621 #endif
4622 }
4623
4624 /* slbie */
4625 static void gen_slbie(DisasContext *ctx)
4626 {
4627 #if defined(CONFIG_USER_ONLY)
4628     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4629 #else
4630     if (unlikely(!ctx->mem_idx)) {
4631         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4632         return;
4633     }
4634     gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4635 #endif
4636 }
4637 #endif
4638
4639 /***                              External control                         ***/
4640 /* Optional: */
4641
4642 /* eciwx */
4643 static void gen_eciwx(DisasContext *ctx)
4644 {
4645     TCGv t0;
4646     /* Should check EAR[E] ! */
4647     gen_set_access_type(ctx, ACCESS_EXT);
4648     t0 = tcg_temp_new();
4649     gen_addr_reg_index(ctx, t0);
4650     gen_check_align(ctx, t0, 0x03);
4651     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4652     tcg_temp_free(t0);
4653 }
4654
4655 /* ecowx */
4656 static void gen_ecowx(DisasContext *ctx)
4657 {
4658     TCGv t0;
4659     /* Should check EAR[E] ! */
4660     gen_set_access_type(ctx, ACCESS_EXT);
4661     t0 = tcg_temp_new();
4662     gen_addr_reg_index(ctx, t0);
4663     gen_check_align(ctx, t0, 0x03);
4664     gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4665     tcg_temp_free(t0);
4666 }
4667
4668 /* PowerPC 601 specific instructions */
4669
4670 /* abs - abs. */
4671 static void gen_abs(DisasContext *ctx)
4672 {
4673     int l1 = gen_new_label();
4674     int l2 = gen_new_label();
4675     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4676     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4677     tcg_gen_br(l2);
4678     gen_set_label(l1);
4679     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4680     gen_set_label(l2);
4681     if (unlikely(Rc(ctx->opcode) != 0))
4682         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4683 }
4684
4685 /* abso - abso. */
4686 static void gen_abso(DisasContext *ctx)
4687 {
4688     int l1 = gen_new_label();
4689     int l2 = gen_new_label();
4690     int l3 = gen_new_label();
4691     /* Start with XER OV disabled, the most likely case */
4692     tcg_gen_movi_tl(cpu_ov, 0);
4693     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4694     tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4695     tcg_gen_movi_tl(cpu_ov, 1);
4696     tcg_gen_movi_tl(cpu_so, 1);
4697     tcg_gen_br(l2);
4698     gen_set_label(l1);
4699     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4700     tcg_gen_br(l3);
4701     gen_set_label(l2);
4702     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4703     gen_set_label(l3);
4704     if (unlikely(Rc(ctx->opcode) != 0))
4705         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4706 }
4707
4708 /* clcs */
4709 static void gen_clcs(DisasContext *ctx)
4710 {
4711     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4712     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4713     tcg_temp_free_i32(t0);
4714     /* Rc=1 sets CR0 to an undefined state */
4715 }
4716
4717 /* div - div. */
4718 static void gen_div(DisasContext *ctx)
4719 {
4720     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4721                    cpu_gpr[rB(ctx->opcode)]);
4722     if (unlikely(Rc(ctx->opcode) != 0))
4723         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4724 }
4725
4726 /* divo - divo. */
4727 static void gen_divo(DisasContext *ctx)
4728 {
4729     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4730                     cpu_gpr[rB(ctx->opcode)]);
4731     if (unlikely(Rc(ctx->opcode) != 0))
4732         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4733 }
4734
4735 /* divs - divs. */
4736 static void gen_divs(DisasContext *ctx)
4737 {
4738     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
4739                     cpu_gpr[rB(ctx->opcode)]);
4740     if (unlikely(Rc(ctx->opcode) != 0))
4741         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4742 }
4743
4744 /* divso - divso. */
4745 static void gen_divso(DisasContext *ctx)
4746 {
4747     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
4748                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4749     if (unlikely(Rc(ctx->opcode) != 0))
4750         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4751 }
4752
4753 /* doz - doz. */
4754 static void gen_doz(DisasContext *ctx)
4755 {
4756     int l1 = gen_new_label();
4757     int l2 = gen_new_label();
4758     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4759     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4760     tcg_gen_br(l2);
4761     gen_set_label(l1);
4762     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4763     gen_set_label(l2);
4764     if (unlikely(Rc(ctx->opcode) != 0))
4765         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4766 }
4767
4768 /* dozo - dozo. */
4769 static void gen_dozo(DisasContext *ctx)
4770 {
4771     int l1 = gen_new_label();
4772     int l2 = gen_new_label();
4773     TCGv t0 = tcg_temp_new();
4774     TCGv t1 = tcg_temp_new();
4775     TCGv t2 = tcg_temp_new();
4776     /* Start with XER OV disabled, the most likely case */
4777     tcg_gen_movi_tl(cpu_ov, 0);
4778     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4779     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4780     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4781     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4782     tcg_gen_andc_tl(t1, t1, t2);
4783     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4784     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4785     tcg_gen_movi_tl(cpu_ov, 1);
4786     tcg_gen_movi_tl(cpu_so, 1);
4787     tcg_gen_br(l2);
4788     gen_set_label(l1);
4789     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4790     gen_set_label(l2);
4791     tcg_temp_free(t0);
4792     tcg_temp_free(t1);
4793     tcg_temp_free(t2);
4794     if (unlikely(Rc(ctx->opcode) != 0))
4795         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4796 }
4797
4798 /* dozi */
4799 static void gen_dozi(DisasContext *ctx)
4800 {
4801     target_long simm = SIMM(ctx->opcode);
4802     int l1 = gen_new_label();
4803     int l2 = gen_new_label();
4804     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4805     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4806     tcg_gen_br(l2);
4807     gen_set_label(l1);
4808     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4809     gen_set_label(l2);
4810     if (unlikely(Rc(ctx->opcode) != 0))
4811         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4812 }
4813
4814 /* lscbx - lscbx. */
4815 static void gen_lscbx(DisasContext *ctx)
4816 {
4817     TCGv t0 = tcg_temp_new();
4818     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4819     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4820     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
4821
4822     gen_addr_reg_index(ctx, t0);
4823     /* NIP cannot be restored if the memory exception comes from an helper */
4824     gen_update_nip(ctx, ctx->nip - 4);
4825     gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
4826     tcg_temp_free_i32(t1);
4827     tcg_temp_free_i32(t2);
4828     tcg_temp_free_i32(t3);
4829     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4830     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4831     if (unlikely(Rc(ctx->opcode) != 0))
4832         gen_set_Rc0(ctx, t0);
4833     tcg_temp_free(t0);
4834 }
4835
4836 /* maskg - maskg. */
4837 static void gen_maskg(DisasContext *ctx)
4838 {
4839     int l1 = gen_new_label();
4840     TCGv t0 = tcg_temp_new();
4841     TCGv t1 = tcg_temp_new();
4842     TCGv t2 = tcg_temp_new();
4843     TCGv t3 = tcg_temp_new();
4844     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4845     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4846     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4847     tcg_gen_addi_tl(t2, t0, 1);
4848     tcg_gen_shr_tl(t2, t3, t2);
4849     tcg_gen_shr_tl(t3, t3, t1);
4850     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4851     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4852     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4853     gen_set_label(l1);
4854     tcg_temp_free(t0);
4855     tcg_temp_free(t1);
4856     tcg_temp_free(t2);
4857     tcg_temp_free(t3);
4858     if (unlikely(Rc(ctx->opcode) != 0))
4859         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4860 }
4861
4862 /* maskir - maskir. */
4863 static void gen_maskir(DisasContext *ctx)
4864 {
4865     TCGv t0 = tcg_temp_new();
4866     TCGv t1 = tcg_temp_new();
4867     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4868     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4869     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4870     tcg_temp_free(t0);
4871     tcg_temp_free(t1);
4872     if (unlikely(Rc(ctx->opcode) != 0))
4873         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4874 }
4875
4876 /* mul - mul. */
4877 static void gen_mul(DisasContext *ctx)
4878 {
4879     TCGv_i64 t0 = tcg_temp_new_i64();
4880     TCGv_i64 t1 = tcg_temp_new_i64();
4881     TCGv t2 = tcg_temp_new();
4882     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4883     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4884     tcg_gen_mul_i64(t0, t0, t1);
4885     tcg_gen_trunc_i64_tl(t2, t0);
4886     gen_store_spr(SPR_MQ, t2);
4887     tcg_gen_shri_i64(t1, t0, 32);
4888     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4889     tcg_temp_free_i64(t0);
4890     tcg_temp_free_i64(t1);
4891     tcg_temp_free(t2);
4892     if (unlikely(Rc(ctx->opcode) != 0))
4893         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4894 }
4895
4896 /* mulo - mulo. */
4897 static void gen_mulo(DisasContext *ctx)
4898 {
4899     int l1 = gen_new_label();
4900     TCGv_i64 t0 = tcg_temp_new_i64();
4901     TCGv_i64 t1 = tcg_temp_new_i64();
4902     TCGv t2 = tcg_temp_new();
4903     /* Start with XER OV disabled, the most likely case */
4904     tcg_gen_movi_tl(cpu_ov, 0);
4905     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4906     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4907     tcg_gen_mul_i64(t0, t0, t1);
4908     tcg_gen_trunc_i64_tl(t2, t0);
4909     gen_store_spr(SPR_MQ, t2);
4910     tcg_gen_shri_i64(t1, t0, 32);
4911     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4912     tcg_gen_ext32s_i64(t1, t0);
4913     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
4914     tcg_gen_movi_tl(cpu_ov, 1);
4915     tcg_gen_movi_tl(cpu_so, 1);
4916     gen_set_label(l1);
4917     tcg_temp_free_i64(t0);
4918     tcg_temp_free_i64(t1);
4919     tcg_temp_free(t2);
4920     if (unlikely(Rc(ctx->opcode) != 0))
4921         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4922 }
4923
4924 /* nabs - nabs. */
4925 static void gen_nabs(DisasContext *ctx)
4926 {
4927     int l1 = gen_new_label();
4928     int l2 = gen_new_label();
4929     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4930     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4931     tcg_gen_br(l2);
4932     gen_set_label(l1);
4933     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4934     gen_set_label(l2);
4935     if (unlikely(Rc(ctx->opcode) != 0))
4936         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4937 }
4938
4939 /* nabso - nabso. */
4940 static void gen_nabso(DisasContext *ctx)
4941 {
4942     int l1 = gen_new_label();
4943     int l2 = gen_new_label();
4944     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4945     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4946     tcg_gen_br(l2);
4947     gen_set_label(l1);
4948     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4949     gen_set_label(l2);
4950     /* nabs never overflows */
4951     tcg_gen_movi_tl(cpu_ov, 0);
4952     if (unlikely(Rc(ctx->opcode) != 0))
4953         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4954 }
4955
4956 /* rlmi - rlmi. */
4957 static void gen_rlmi(DisasContext *ctx)
4958 {
4959     uint32_t mb = MB(ctx->opcode);
4960     uint32_t me = ME(ctx->opcode);
4961     TCGv t0 = tcg_temp_new();
4962     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4963     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4964     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4965     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4966     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4967     tcg_temp_free(t0);
4968     if (unlikely(Rc(ctx->opcode) != 0))
4969         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4970 }
4971
4972 /* rrib - rrib. */
4973 static void gen_rrib(DisasContext *ctx)
4974 {
4975     TCGv t0 = tcg_temp_new();
4976     TCGv t1 = tcg_temp_new();
4977     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4978     tcg_gen_movi_tl(t1, 0x80000000);
4979     tcg_gen_shr_tl(t1, t1, t0);
4980     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4981     tcg_gen_and_tl(t0, t0, t1);
4982     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4983     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4984     tcg_temp_free(t0);
4985     tcg_temp_free(t1);
4986     if (unlikely(Rc(ctx->opcode) != 0))
4987         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4988 }
4989
4990 /* sle - sle. */
4991 static void gen_sle(DisasContext *ctx)
4992 {
4993     TCGv t0 = tcg_temp_new();
4994     TCGv t1 = tcg_temp_new();
4995     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4996     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4997     tcg_gen_subfi_tl(t1, 32, t1);
4998     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4999     tcg_gen_or_tl(t1, t0, t1);
5000     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5001     gen_store_spr(SPR_MQ, t1);
5002     tcg_temp_free(t0);
5003     tcg_temp_free(t1);
5004     if (unlikely(Rc(ctx->opcode) != 0))
5005         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5006 }
5007
5008 /* sleq - sleq. */
5009 static void gen_sleq(DisasContext *ctx)
5010 {
5011     TCGv t0 = tcg_temp_new();
5012     TCGv t1 = tcg_temp_new();
5013     TCGv t2 = tcg_temp_new();
5014     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5015     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5016     tcg_gen_shl_tl(t2, t2, t0);
5017     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5018     gen_load_spr(t1, SPR_MQ);
5019     gen_store_spr(SPR_MQ, t0);
5020     tcg_gen_and_tl(t0, t0, t2);
5021     tcg_gen_andc_tl(t1, t1, t2);
5022     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5023     tcg_temp_free(t0);
5024     tcg_temp_free(t1);
5025     tcg_temp_free(t2);
5026     if (unlikely(Rc(ctx->opcode) != 0))
5027         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5028 }
5029
5030 /* sliq - sliq. */
5031 static void gen_sliq(DisasContext *ctx)
5032 {
5033     int sh = SH(ctx->opcode);
5034     TCGv t0 = tcg_temp_new();
5035     TCGv t1 = tcg_temp_new();
5036     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5037     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5038     tcg_gen_or_tl(t1, t0, t1);
5039     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5040     gen_store_spr(SPR_MQ, t1);
5041     tcg_temp_free(t0);
5042     tcg_temp_free(t1);
5043     if (unlikely(Rc(ctx->opcode) != 0))
5044         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5045 }
5046
5047 /* slliq - slliq. */
5048 static void gen_slliq(DisasContext *ctx)
5049 {
5050     int sh = SH(ctx->opcode);
5051     TCGv t0 = tcg_temp_new();
5052     TCGv t1 = tcg_temp_new();
5053     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5054     gen_load_spr(t1, SPR_MQ);
5055     gen_store_spr(SPR_MQ, t0);
5056     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5057     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5058     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5059     tcg_temp_free(t0);
5060     tcg_temp_free(t1);
5061     if (unlikely(Rc(ctx->opcode) != 0))
5062         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5063 }
5064
5065 /* sllq - sllq. */
5066 static void gen_sllq(DisasContext *ctx)
5067 {
5068     int l1 = gen_new_label();
5069     int l2 = gen_new_label();
5070     TCGv t0 = tcg_temp_local_new();
5071     TCGv t1 = tcg_temp_local_new();
5072     TCGv t2 = tcg_temp_local_new();
5073     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5074     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5075     tcg_gen_shl_tl(t1, t1, t2);
5076     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5077     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5078     gen_load_spr(t0, SPR_MQ);
5079     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5080     tcg_gen_br(l2);
5081     gen_set_label(l1);
5082     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5083     gen_load_spr(t2, SPR_MQ);
5084     tcg_gen_andc_tl(t1, t2, t1);
5085     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5086     gen_set_label(l2);
5087     tcg_temp_free(t0);
5088     tcg_temp_free(t1);
5089     tcg_temp_free(t2);
5090     if (unlikely(Rc(ctx->opcode) != 0))
5091         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5092 }
5093
5094 /* slq - slq. */
5095 static void gen_slq(DisasContext *ctx)
5096 {
5097     int l1 = gen_new_label();
5098     TCGv t0 = tcg_temp_new();
5099     TCGv t1 = tcg_temp_new();
5100     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5101     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5102     tcg_gen_subfi_tl(t1, 32, t1);
5103     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5104     tcg_gen_or_tl(t1, t0, t1);
5105     gen_store_spr(SPR_MQ, t1);
5106     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5107     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5108     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5109     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5110     gen_set_label(l1);
5111     tcg_temp_free(t0);
5112     tcg_temp_free(t1);
5113     if (unlikely(Rc(ctx->opcode) != 0))
5114         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5115 }
5116
5117 /* sraiq - sraiq. */
5118 static void gen_sraiq(DisasContext *ctx)
5119 {
5120     int sh = SH(ctx->opcode);
5121     int l1 = gen_new_label();
5122     TCGv t0 = tcg_temp_new();
5123     TCGv t1 = tcg_temp_new();
5124     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5125     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5126     tcg_gen_or_tl(t0, t0, t1);
5127     gen_store_spr(SPR_MQ, t0);
5128     tcg_gen_movi_tl(cpu_ca, 0);
5129     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5130     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5131     tcg_gen_movi_tl(cpu_ca, 1);
5132     gen_set_label(l1);
5133     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5134     tcg_temp_free(t0);
5135     tcg_temp_free(t1);
5136     if (unlikely(Rc(ctx->opcode) != 0))
5137         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5138 }
5139
5140 /* sraq - sraq. */
5141 static void gen_sraq(DisasContext *ctx)
5142 {
5143     int l1 = gen_new_label();
5144     int l2 = gen_new_label();
5145     TCGv t0 = tcg_temp_new();
5146     TCGv t1 = tcg_temp_local_new();
5147     TCGv t2 = tcg_temp_local_new();
5148     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5149     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5150     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5151     tcg_gen_subfi_tl(t2, 32, t2);
5152     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5153     tcg_gen_or_tl(t0, t0, t2);
5154     gen_store_spr(SPR_MQ, t0);
5155     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5156     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5157     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5158     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5159     gen_set_label(l1);
5160     tcg_temp_free(t0);
5161     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5162     tcg_gen_movi_tl(cpu_ca, 0);
5163     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5164     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5165     tcg_gen_movi_tl(cpu_ca, 1);
5166     gen_set_label(l2);
5167     tcg_temp_free(t1);
5168     tcg_temp_free(t2);
5169     if (unlikely(Rc(ctx->opcode) != 0))
5170         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5171 }
5172
5173 /* sre - sre. */
5174 static void gen_sre(DisasContext *ctx)
5175 {
5176     TCGv t0 = tcg_temp_new();
5177     TCGv t1 = tcg_temp_new();
5178     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5179     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5180     tcg_gen_subfi_tl(t1, 32, t1);
5181     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5182     tcg_gen_or_tl(t1, t0, t1);
5183     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5184     gen_store_spr(SPR_MQ, t1);
5185     tcg_temp_free(t0);
5186     tcg_temp_free(t1);
5187     if (unlikely(Rc(ctx->opcode) != 0))
5188         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5189 }
5190
5191 /* srea - srea. */
5192 static void gen_srea(DisasContext *ctx)
5193 {
5194     TCGv t0 = tcg_temp_new();
5195     TCGv t1 = tcg_temp_new();
5196     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5197     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5198     gen_store_spr(SPR_MQ, t0);
5199     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5200     tcg_temp_free(t0);
5201     tcg_temp_free(t1);
5202     if (unlikely(Rc(ctx->opcode) != 0))
5203         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5204 }
5205
5206 /* sreq */
5207 static void gen_sreq(DisasContext *ctx)
5208 {
5209     TCGv t0 = tcg_temp_new();
5210     TCGv t1 = tcg_temp_new();
5211     TCGv t2 = tcg_temp_new();
5212     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5213     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5214     tcg_gen_shr_tl(t1, t1, t0);
5215     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5216     gen_load_spr(t2, SPR_MQ);
5217     gen_store_spr(SPR_MQ, t0);
5218     tcg_gen_and_tl(t0, t0, t1);
5219     tcg_gen_andc_tl(t2, t2, t1);
5220     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5221     tcg_temp_free(t0);
5222     tcg_temp_free(t1);
5223     tcg_temp_free(t2);
5224     if (unlikely(Rc(ctx->opcode) != 0))
5225         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5226 }
5227
5228 /* sriq */
5229 static void gen_sriq(DisasContext *ctx)
5230 {
5231     int sh = SH(ctx->opcode);
5232     TCGv t0 = tcg_temp_new();
5233     TCGv t1 = tcg_temp_new();
5234     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5235     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5236     tcg_gen_or_tl(t1, t0, t1);
5237     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5238     gen_store_spr(SPR_MQ, t1);
5239     tcg_temp_free(t0);
5240     tcg_temp_free(t1);
5241     if (unlikely(Rc(ctx->opcode) != 0))
5242         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5243 }
5244
5245 /* srliq */
5246 static void gen_srliq(DisasContext *ctx)
5247 {
5248     int sh = SH(ctx->opcode);
5249     TCGv t0 = tcg_temp_new();
5250     TCGv t1 = tcg_temp_new();
5251     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5252     gen_load_spr(t1, SPR_MQ);
5253     gen_store_spr(SPR_MQ, t0);
5254     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5255     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5256     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5257     tcg_temp_free(t0);
5258     tcg_temp_free(t1);
5259     if (unlikely(Rc(ctx->opcode) != 0))
5260         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5261 }
5262
5263 /* srlq */
5264 static void gen_srlq(DisasContext *ctx)
5265 {
5266     int l1 = gen_new_label();
5267     int l2 = gen_new_label();
5268     TCGv t0 = tcg_temp_local_new();
5269     TCGv t1 = tcg_temp_local_new();
5270     TCGv t2 = tcg_temp_local_new();
5271     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5272     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5273     tcg_gen_shr_tl(t2, t1, t2);
5274     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5275     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5276     gen_load_spr(t0, SPR_MQ);
5277     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5278     tcg_gen_br(l2);
5279     gen_set_label(l1);
5280     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5281     tcg_gen_and_tl(t0, t0, t2);
5282     gen_load_spr(t1, SPR_MQ);
5283     tcg_gen_andc_tl(t1, t1, t2);
5284     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5285     gen_set_label(l2);
5286     tcg_temp_free(t0);
5287     tcg_temp_free(t1);
5288     tcg_temp_free(t2);
5289     if (unlikely(Rc(ctx->opcode) != 0))
5290         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5291 }
5292
5293 /* srq */
5294 static void gen_srq(DisasContext *ctx)
5295 {
5296     int l1 = gen_new_label();
5297     TCGv t0 = tcg_temp_new();
5298     TCGv t1 = tcg_temp_new();
5299     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5300     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5301     tcg_gen_subfi_tl(t1, 32, t1);
5302     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5303     tcg_gen_or_tl(t1, t0, t1);
5304     gen_store_spr(SPR_MQ, t1);
5305     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5306     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5307     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5308     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5309     gen_set_label(l1);
5310     tcg_temp_free(t0);
5311     tcg_temp_free(t1);
5312     if (unlikely(Rc(ctx->opcode) != 0))
5313         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5314 }
5315
5316 /* PowerPC 602 specific instructions */
5317
5318 /* dsa  */
5319 static void gen_dsa(DisasContext *ctx)
5320 {
5321     /* XXX: TODO */
5322     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5323 }
5324
5325 /* esa */
5326 static void gen_esa(DisasContext *ctx)
5327 {
5328     /* XXX: TODO */
5329     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5330 }
5331
5332 /* mfrom */
5333 static void gen_mfrom(DisasContext *ctx)
5334 {
5335 #if defined(CONFIG_USER_ONLY)
5336     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5337 #else
5338     if (unlikely(!ctx->mem_idx)) {
5339         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5340         return;
5341     }
5342     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5343 #endif
5344 }
5345
5346 /* 602 - 603 - G2 TLB management */
5347
5348 /* tlbld */
5349 static void gen_tlbld_6xx(DisasContext *ctx)
5350 {
5351 #if defined(CONFIG_USER_ONLY)
5352     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5353 #else
5354     if (unlikely(!ctx->mem_idx)) {
5355         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5356         return;
5357     }
5358     gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5359 #endif
5360 }
5361
5362 /* tlbli */
5363 static void gen_tlbli_6xx(DisasContext *ctx)
5364 {
5365 #if defined(CONFIG_USER_ONLY)
5366     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5367 #else
5368     if (unlikely(!ctx->mem_idx)) {
5369         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5370         return;
5371     }
5372     gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5373 #endif
5374 }
5375
5376 /* 74xx TLB management */
5377
5378 /* tlbld */
5379 static void gen_tlbld_74xx(DisasContext *ctx)
5380 {
5381 #if defined(CONFIG_USER_ONLY)
5382     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5383 #else
5384     if (unlikely(!ctx->mem_idx)) {
5385         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5386         return;
5387     }
5388     gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5389 #endif
5390 }
5391
5392 /* tlbli */
5393 static void gen_tlbli_74xx(DisasContext *ctx)
5394 {
5395 #if defined(CONFIG_USER_ONLY)
5396     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5397 #else
5398     if (unlikely(!ctx->mem_idx)) {
5399         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5400         return;
5401     }
5402     gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5403 #endif
5404 }
5405
5406 /* POWER instructions not in PowerPC 601 */
5407
5408 /* clf */
5409 static void gen_clf(DisasContext *ctx)
5410 {
5411     /* Cache line flush: implemented as no-op */
5412 }
5413
5414 /* cli */
5415 static void gen_cli(DisasContext *ctx)
5416 {
5417     /* Cache line invalidate: privileged and treated as no-op */
5418 #if defined(CONFIG_USER_ONLY)
5419     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5420 #else
5421     if (unlikely(!ctx->mem_idx)) {
5422         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5423         return;
5424     }
5425 #endif
5426 }
5427
5428 /* dclst */
5429 static void gen_dclst(DisasContext *ctx)
5430 {
5431     /* Data cache line store: treated as no-op */
5432 }
5433
5434 static void gen_mfsri(DisasContext *ctx)
5435 {
5436 #if defined(CONFIG_USER_ONLY)
5437     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5438 #else
5439     int ra = rA(ctx->opcode);
5440     int rd = rD(ctx->opcode);
5441     TCGv t0;
5442     if (unlikely(!ctx->mem_idx)) {
5443         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5444         return;
5445     }
5446     t0 = tcg_temp_new();
5447     gen_addr_reg_index(ctx, t0);
5448     tcg_gen_shri_tl(t0, t0, 28);
5449     tcg_gen_andi_tl(t0, t0, 0xF);
5450     gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
5451     tcg_temp_free(t0);
5452     if (ra != 0 && ra != rd)
5453         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5454 #endif
5455 }
5456
5457 static void gen_rac(DisasContext *ctx)
5458 {
5459 #if defined(CONFIG_USER_ONLY)
5460     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5461 #else
5462     TCGv t0;
5463     if (unlikely(!ctx->mem_idx)) {
5464         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5465         return;
5466     }
5467     t0 = tcg_temp_new();
5468     gen_addr_reg_index(ctx, t0);
5469     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5470     tcg_temp_free(t0);
5471 #endif
5472 }
5473
5474 static void gen_rfsvc(DisasContext *ctx)
5475 {
5476 #if defined(CONFIG_USER_ONLY)
5477     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5478 #else
5479     if (unlikely(!ctx->mem_idx)) {
5480         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5481         return;
5482     }
5483     gen_helper_rfsvc(cpu_env);
5484     gen_sync_exception(ctx);
5485 #endif
5486 }
5487
5488 /* svc is not implemented for now */
5489
5490 /* POWER2 specific instructions */
5491 /* Quad manipulation (load/store two floats at a time) */
5492
5493 /* lfq */
5494 static void gen_lfq(DisasContext *ctx)
5495 {
5496     int rd = rD(ctx->opcode);
5497     TCGv t0;
5498     gen_set_access_type(ctx, ACCESS_FLOAT);
5499     t0 = tcg_temp_new();
5500     gen_addr_imm_index(ctx, t0, 0);
5501     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5502     gen_addr_add(ctx, t0, t0, 8);
5503     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5504     tcg_temp_free(t0);
5505 }
5506
5507 /* lfqu */
5508 static void gen_lfqu(DisasContext *ctx)
5509 {
5510     int ra = rA(ctx->opcode);
5511     int rd = rD(ctx->opcode);
5512     TCGv t0, t1;
5513     gen_set_access_type(ctx, ACCESS_FLOAT);
5514     t0 = tcg_temp_new();
5515     t1 = tcg_temp_new();
5516     gen_addr_imm_index(ctx, t0, 0);
5517     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5518     gen_addr_add(ctx, t1, t0, 8);
5519     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5520     if (ra != 0)
5521         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5522     tcg_temp_free(t0);
5523     tcg_temp_free(t1);
5524 }
5525
5526 /* lfqux */
5527 static void gen_lfqux(DisasContext *ctx)
5528 {
5529     int ra = rA(ctx->opcode);
5530     int rd = rD(ctx->opcode);
5531     gen_set_access_type(ctx, ACCESS_FLOAT);
5532     TCGv t0, t1;
5533     t0 = tcg_temp_new();
5534     gen_addr_reg_index(ctx, t0);
5535     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5536     t1 = tcg_temp_new();
5537     gen_addr_add(ctx, t1, t0, 8);
5538     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5539     tcg_temp_free(t1);
5540     if (ra != 0)
5541         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5542     tcg_temp_free(t0);
5543 }
5544
5545 /* lfqx */
5546 static void gen_lfqx(DisasContext *ctx)
5547 {
5548     int rd = rD(ctx->opcode);
5549     TCGv t0;
5550     gen_set_access_type(ctx, ACCESS_FLOAT);
5551     t0 = tcg_temp_new();
5552     gen_addr_reg_index(ctx, t0);
5553     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5554     gen_addr_add(ctx, t0, t0, 8);
5555     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5556     tcg_temp_free(t0);
5557 }
5558
5559 /* stfq */
5560 static void gen_stfq(DisasContext *ctx)
5561 {
5562     int rd = rD(ctx->opcode);
5563     TCGv t0;
5564     gen_set_access_type(ctx, ACCESS_FLOAT);
5565     t0 = tcg_temp_new();
5566     gen_addr_imm_index(ctx, t0, 0);
5567     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5568     gen_addr_add(ctx, t0, t0, 8);
5569     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5570     tcg_temp_free(t0);
5571 }
5572
5573 /* stfqu */
5574 static void gen_stfqu(DisasContext *ctx)
5575 {
5576     int ra = rA(ctx->opcode);
5577     int rd = rD(ctx->opcode);
5578     TCGv t0, t1;
5579     gen_set_access_type(ctx, ACCESS_FLOAT);
5580     t0 = tcg_temp_new();
5581     gen_addr_imm_index(ctx, t0, 0);
5582     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5583     t1 = tcg_temp_new();
5584     gen_addr_add(ctx, t1, t0, 8);
5585     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5586     tcg_temp_free(t1);
5587     if (ra != 0)
5588         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5589     tcg_temp_free(t0);
5590 }
5591
5592 /* stfqux */
5593 static void gen_stfqux(DisasContext *ctx)
5594 {
5595     int ra = rA(ctx->opcode);
5596     int rd = rD(ctx->opcode);
5597     TCGv t0, t1;
5598     gen_set_access_type(ctx, ACCESS_FLOAT);
5599     t0 = tcg_temp_new();
5600     gen_addr_reg_index(ctx, t0);
5601     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5602     t1 = tcg_temp_new();
5603     gen_addr_add(ctx, t1, t0, 8);
5604     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5605     tcg_temp_free(t1);
5606     if (ra != 0)
5607         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5608     tcg_temp_free(t0);
5609 }
5610
5611 /* stfqx */
5612 static void gen_stfqx(DisasContext *ctx)
5613 {
5614     int rd = rD(ctx->opcode);
5615     TCGv t0;
5616     gen_set_access_type(ctx, ACCESS_FLOAT);
5617     t0 = tcg_temp_new();
5618     gen_addr_reg_index(ctx, t0);
5619     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5620     gen_addr_add(ctx, t0, t0, 8);
5621     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5622     tcg_temp_free(t0);
5623 }
5624
5625 /* BookE specific instructions */
5626
5627 /* XXX: not implemented on 440 ? */
5628 static void gen_mfapidi(DisasContext *ctx)
5629 {
5630     /* XXX: TODO */
5631     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5632 }
5633
5634 /* XXX: not implemented on 440 ? */
5635 static void gen_tlbiva(DisasContext *ctx)
5636 {
5637 #if defined(CONFIG_USER_ONLY)
5638     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5639 #else
5640     TCGv t0;
5641     if (unlikely(!ctx->mem_idx)) {
5642         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5643         return;
5644     }
5645     t0 = tcg_temp_new();
5646     gen_addr_reg_index(ctx, t0);
5647     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5648     tcg_temp_free(t0);
5649 #endif
5650 }
5651
5652 /* All 405 MAC instructions are translated here */
5653 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5654                                         int ra, int rb, int rt, int Rc)
5655 {
5656     TCGv t0, t1;
5657
5658     t0 = tcg_temp_local_new();
5659     t1 = tcg_temp_local_new();
5660
5661     switch (opc3 & 0x0D) {
5662     case 0x05:
5663         /* macchw    - macchw.    - macchwo   - macchwo.   */
5664         /* macchws   - macchws.   - macchwso  - macchwso.  */
5665         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5666         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5667         /* mulchw - mulchw. */
5668         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5669         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5670         tcg_gen_ext16s_tl(t1, t1);
5671         break;
5672     case 0x04:
5673         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5674         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5675         /* mulchwu - mulchwu. */
5676         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5677         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5678         tcg_gen_ext16u_tl(t1, t1);
5679         break;
5680     case 0x01:
5681         /* machhw    - machhw.    - machhwo   - machhwo.   */
5682         /* machhws   - machhws.   - machhwso  - machhwso.  */
5683         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5684         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5685         /* mulhhw - mulhhw. */
5686         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5687         tcg_gen_ext16s_tl(t0, t0);
5688         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5689         tcg_gen_ext16s_tl(t1, t1);
5690         break;
5691     case 0x00:
5692         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5693         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5694         /* mulhhwu - mulhhwu. */
5695         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5696         tcg_gen_ext16u_tl(t0, t0);
5697         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5698         tcg_gen_ext16u_tl(t1, t1);
5699         break;
5700     case 0x0D:
5701         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5702         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5703         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5704         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5705         /* mullhw - mullhw. */
5706         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5707         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5708         break;
5709     case 0x0C:
5710         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5711         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5712         /* mullhwu - mullhwu. */
5713         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5714         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5715         break;
5716     }
5717     if (opc2 & 0x04) {
5718         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5719         tcg_gen_mul_tl(t1, t0, t1);
5720         if (opc2 & 0x02) {
5721             /* nmultiply-and-accumulate (0x0E) */
5722             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5723         } else {
5724             /* multiply-and-accumulate (0x0C) */
5725             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5726         }
5727
5728         if (opc3 & 0x12) {
5729             /* Check overflow and/or saturate */
5730             int l1 = gen_new_label();
5731
5732             if (opc3 & 0x10) {
5733                 /* Start with XER OV disabled, the most likely case */
5734                 tcg_gen_movi_tl(cpu_ov, 0);
5735             }
5736             if (opc3 & 0x01) {
5737                 /* Signed */
5738                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5739                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5740                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5741                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5742                 if (opc3 & 0x02) {
5743                     /* Saturate */
5744                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5745                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5746                 }
5747             } else {
5748                 /* Unsigned */
5749                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5750                 if (opc3 & 0x02) {
5751                     /* Saturate */
5752                     tcg_gen_movi_tl(t0, UINT32_MAX);
5753                 }
5754             }
5755             if (opc3 & 0x10) {
5756                 /* Check overflow */
5757                 tcg_gen_movi_tl(cpu_ov, 1);
5758                 tcg_gen_movi_tl(cpu_so, 1);
5759             }
5760             gen_set_label(l1);
5761             tcg_gen_mov_tl(cpu_gpr[rt], t0);
5762         }
5763     } else {
5764         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5765     }
5766     tcg_temp_free(t0);
5767     tcg_temp_free(t1);
5768     if (unlikely(Rc) != 0) {
5769         /* Update Rc0 */
5770         gen_set_Rc0(ctx, cpu_gpr[rt]);
5771     }
5772 }
5773
5774 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5775 static void glue(gen_, name)(DisasContext *ctx)                               \
5776 {                                                                             \
5777     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5778                          rD(ctx->opcode), Rc(ctx->opcode));                   \
5779 }
5780
5781 /* macchw    - macchw.    */
5782 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5783 /* macchwo   - macchwo.   */
5784 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5785 /* macchws   - macchws.   */
5786 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5787 /* macchwso  - macchwso.  */
5788 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5789 /* macchwsu  - macchwsu.  */
5790 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5791 /* macchwsuo - macchwsuo. */
5792 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5793 /* macchwu   - macchwu.   */
5794 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5795 /* macchwuo  - macchwuo.  */
5796 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5797 /* machhw    - machhw.    */
5798 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5799 /* machhwo   - machhwo.   */
5800 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5801 /* machhws   - machhws.   */
5802 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5803 /* machhwso  - machhwso.  */
5804 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5805 /* machhwsu  - machhwsu.  */
5806 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5807 /* machhwsuo - machhwsuo. */
5808 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5809 /* machhwu   - machhwu.   */
5810 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5811 /* machhwuo  - machhwuo.  */
5812 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5813 /* maclhw    - maclhw.    */
5814 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5815 /* maclhwo   - maclhwo.   */
5816 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5817 /* maclhws   - maclhws.   */
5818 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5819 /* maclhwso  - maclhwso.  */
5820 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5821 /* maclhwu   - maclhwu.   */
5822 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5823 /* maclhwuo  - maclhwuo.  */
5824 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5825 /* maclhwsu  - maclhwsu.  */
5826 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5827 /* maclhwsuo - maclhwsuo. */
5828 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5829 /* nmacchw   - nmacchw.   */
5830 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5831 /* nmacchwo  - nmacchwo.  */
5832 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5833 /* nmacchws  - nmacchws.  */
5834 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5835 /* nmacchwso - nmacchwso. */
5836 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5837 /* nmachhw   - nmachhw.   */
5838 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5839 /* nmachhwo  - nmachhwo.  */
5840 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5841 /* nmachhws  - nmachhws.  */
5842 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5843 /* nmachhwso - nmachhwso. */
5844 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5845 /* nmaclhw   - nmaclhw.   */
5846 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5847 /* nmaclhwo  - nmaclhwo.  */
5848 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5849 /* nmaclhws  - nmaclhws.  */
5850 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5851 /* nmaclhwso - nmaclhwso. */
5852 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5853
5854 /* mulchw  - mulchw.  */
5855 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5856 /* mulchwu - mulchwu. */
5857 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5858 /* mulhhw  - mulhhw.  */
5859 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5860 /* mulhhwu - mulhhwu. */
5861 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5862 /* mullhw  - mullhw.  */
5863 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5864 /* mullhwu - mullhwu. */
5865 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5866
5867 /* mfdcr */
5868 static void gen_mfdcr(DisasContext *ctx)
5869 {
5870 #if defined(CONFIG_USER_ONLY)
5871     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5872 #else
5873     TCGv dcrn;
5874     if (unlikely(!ctx->mem_idx)) {
5875         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5876         return;
5877     }
5878     /* NIP cannot be restored if the memory exception comes from an helper */
5879     gen_update_nip(ctx, ctx->nip - 4);
5880     dcrn = tcg_const_tl(SPR(ctx->opcode));
5881     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
5882     tcg_temp_free(dcrn);
5883 #endif
5884 }
5885
5886 /* mtdcr */
5887 static void gen_mtdcr(DisasContext *ctx)
5888 {
5889 #if defined(CONFIG_USER_ONLY)
5890     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5891 #else
5892     TCGv dcrn;
5893     if (unlikely(!ctx->mem_idx)) {
5894         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5895         return;
5896     }
5897     /* NIP cannot be restored if the memory exception comes from an helper */
5898     gen_update_nip(ctx, ctx->nip - 4);
5899     dcrn = tcg_const_tl(SPR(ctx->opcode));
5900     gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
5901     tcg_temp_free(dcrn);
5902 #endif
5903 }
5904
5905 /* mfdcrx */
5906 /* XXX: not implemented on 440 ? */
5907 static void gen_mfdcrx(DisasContext *ctx)
5908 {
5909 #if defined(CONFIG_USER_ONLY)
5910     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5911 #else
5912     if (unlikely(!ctx->mem_idx)) {
5913         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5914         return;
5915     }
5916     /* NIP cannot be restored if the memory exception comes from an helper */
5917     gen_update_nip(ctx, ctx->nip - 4);
5918     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5919                         cpu_gpr[rA(ctx->opcode)]);
5920     /* Note: Rc update flag set leads to undefined state of Rc0 */
5921 #endif
5922 }
5923
5924 /* mtdcrx */
5925 /* XXX: not implemented on 440 ? */
5926 static void gen_mtdcrx(DisasContext *ctx)
5927 {
5928 #if defined(CONFIG_USER_ONLY)
5929     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5930 #else
5931     if (unlikely(!ctx->mem_idx)) {
5932         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5933         return;
5934     }
5935     /* NIP cannot be restored if the memory exception comes from an helper */
5936     gen_update_nip(ctx, ctx->nip - 4);
5937     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5938                          cpu_gpr[rS(ctx->opcode)]);
5939     /* Note: Rc update flag set leads to undefined state of Rc0 */
5940 #endif
5941 }
5942
5943 /* mfdcrux (PPC 460) : user-mode access to DCR */
5944 static void gen_mfdcrux(DisasContext *ctx)
5945 {
5946     /* NIP cannot be restored if the memory exception comes from an helper */
5947     gen_update_nip(ctx, ctx->nip - 4);
5948     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5949                         cpu_gpr[rA(ctx->opcode)]);
5950     /* Note: Rc update flag set leads to undefined state of Rc0 */
5951 }
5952
5953 /* mtdcrux (PPC 460) : user-mode access to DCR */
5954 static void gen_mtdcrux(DisasContext *ctx)
5955 {
5956     /* NIP cannot be restored if the memory exception comes from an helper */
5957     gen_update_nip(ctx, ctx->nip - 4);
5958     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5959                          cpu_gpr[rS(ctx->opcode)]);
5960     /* Note: Rc update flag set leads to undefined state of Rc0 */
5961 }
5962
5963 /* dccci */
5964 static void gen_dccci(DisasContext *ctx)
5965 {
5966 #if defined(CONFIG_USER_ONLY)
5967     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5968 #else
5969     if (unlikely(!ctx->mem_idx)) {
5970         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5971         return;
5972     }
5973     /* interpreted as no-op */
5974 #endif
5975 }
5976
5977 /* dcread */
5978 static void gen_dcread(DisasContext *ctx)
5979 {
5980 #if defined(CONFIG_USER_ONLY)
5981     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5982 #else
5983     TCGv EA, val;
5984     if (unlikely(!ctx->mem_idx)) {
5985         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5986         return;
5987     }
5988     gen_set_access_type(ctx, ACCESS_CACHE);
5989     EA = tcg_temp_new();
5990     gen_addr_reg_index(ctx, EA);
5991     val = tcg_temp_new();
5992     gen_qemu_ld32u(ctx, val, EA);
5993     tcg_temp_free(val);
5994     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5995     tcg_temp_free(EA);
5996 #endif
5997 }
5998
5999 /* icbt */
6000 static void gen_icbt_40x(DisasContext *ctx)
6001 {
6002     /* interpreted as no-op */
6003     /* XXX: specification say this is treated as a load by the MMU
6004      *      but does not generate any exception
6005      */
6006 }
6007
6008 /* iccci */
6009 static void gen_iccci(DisasContext *ctx)
6010 {
6011 #if defined(CONFIG_USER_ONLY)
6012     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6013 #else
6014     if (unlikely(!ctx->mem_idx)) {
6015         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6016         return;
6017     }
6018     /* interpreted as no-op */
6019 #endif
6020 }
6021
6022 /* icread */
6023 static void gen_icread(DisasContext *ctx)
6024 {
6025 #if defined(CONFIG_USER_ONLY)
6026     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6027 #else
6028     if (unlikely(!ctx->mem_idx)) {
6029         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6030         return;
6031     }
6032     /* interpreted as no-op */
6033 #endif
6034 }
6035
6036 /* rfci (mem_idx only) */
6037 static void gen_rfci_40x(DisasContext *ctx)
6038 {
6039 #if defined(CONFIG_USER_ONLY)
6040     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6041 #else
6042     if (unlikely(!ctx->mem_idx)) {
6043         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6044         return;
6045     }
6046     /* Restore CPU state */
6047     gen_helper_40x_rfci(cpu_env);
6048     gen_sync_exception(ctx);
6049 #endif
6050 }
6051
6052 static void gen_rfci(DisasContext *ctx)
6053 {
6054 #if defined(CONFIG_USER_ONLY)
6055     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6056 #else
6057     if (unlikely(!ctx->mem_idx)) {
6058         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6059         return;
6060     }
6061     /* Restore CPU state */
6062     gen_helper_rfci(cpu_env);
6063     gen_sync_exception(ctx);
6064 #endif
6065 }
6066
6067 /* BookE specific */
6068
6069 /* XXX: not implemented on 440 ? */
6070 static void gen_rfdi(DisasContext *ctx)
6071 {
6072 #if defined(CONFIG_USER_ONLY)
6073     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6074 #else
6075     if (unlikely(!ctx->mem_idx)) {
6076         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6077         return;
6078     }
6079     /* Restore CPU state */
6080     gen_helper_rfdi(cpu_env);
6081     gen_sync_exception(ctx);
6082 #endif
6083 }
6084
6085 /* XXX: not implemented on 440 ? */
6086 static void gen_rfmci(DisasContext *ctx)
6087 {
6088 #if defined(CONFIG_USER_ONLY)
6089     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6090 #else
6091     if (unlikely(!ctx->mem_idx)) {
6092         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6093         return;
6094     }
6095     /* Restore CPU state */
6096     gen_helper_rfmci(cpu_env);
6097     gen_sync_exception(ctx);
6098 #endif
6099 }
6100
6101 /* TLB management - PowerPC 405 implementation */
6102
6103 /* tlbre */
6104 static void gen_tlbre_40x(DisasContext *ctx)
6105 {
6106 #if defined(CONFIG_USER_ONLY)
6107     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6108 #else
6109     if (unlikely(!ctx->mem_idx)) {
6110         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6111         return;
6112     }
6113     switch (rB(ctx->opcode)) {
6114     case 0:
6115         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6116                                 cpu_gpr[rA(ctx->opcode)]);
6117         break;
6118     case 1:
6119         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6120                                 cpu_gpr[rA(ctx->opcode)]);
6121         break;
6122     default:
6123         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6124         break;
6125     }
6126 #endif
6127 }
6128
6129 /* tlbsx - tlbsx. */
6130 static void gen_tlbsx_40x(DisasContext *ctx)
6131 {
6132 #if defined(CONFIG_USER_ONLY)
6133     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6134 #else
6135     TCGv t0;
6136     if (unlikely(!ctx->mem_idx)) {
6137         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6138         return;
6139     }
6140     t0 = tcg_temp_new();
6141     gen_addr_reg_index(ctx, t0);
6142     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6143     tcg_temp_free(t0);
6144     if (Rc(ctx->opcode)) {
6145         int l1 = gen_new_label();
6146         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6147         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6148         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6149         gen_set_label(l1);
6150     }
6151 #endif
6152 }
6153
6154 /* tlbwe */
6155 static void gen_tlbwe_40x(DisasContext *ctx)
6156 {
6157 #if defined(CONFIG_USER_ONLY)
6158     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6159 #else
6160     if (unlikely(!ctx->mem_idx)) {
6161         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6162         return;
6163     }
6164     switch (rB(ctx->opcode)) {
6165     case 0:
6166         gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6167                                 cpu_gpr[rS(ctx->opcode)]);
6168         break;
6169     case 1:
6170         gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6171                                 cpu_gpr[rS(ctx->opcode)]);
6172         break;
6173     default:
6174         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6175         break;
6176     }
6177 #endif
6178 }
6179
6180 /* TLB management - PowerPC 440 implementation */
6181
6182 /* tlbre */
6183 static void gen_tlbre_440(DisasContext *ctx)
6184 {
6185 #if defined(CONFIG_USER_ONLY)
6186     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6187 #else
6188     if (unlikely(!ctx->mem_idx)) {
6189         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6190         return;
6191     }
6192     switch (rB(ctx->opcode)) {
6193     case 0:
6194     case 1:
6195     case 2:
6196         {
6197             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6198             gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6199                                  t0, cpu_gpr[rA(ctx->opcode)]);
6200             tcg_temp_free_i32(t0);
6201         }
6202         break;
6203     default:
6204         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6205         break;
6206     }
6207 #endif
6208 }
6209
6210 /* tlbsx - tlbsx. */
6211 static void gen_tlbsx_440(DisasContext *ctx)
6212 {
6213 #if defined(CONFIG_USER_ONLY)
6214     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6215 #else
6216     TCGv t0;
6217     if (unlikely(!ctx->mem_idx)) {
6218         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6219         return;
6220     }
6221     t0 = tcg_temp_new();
6222     gen_addr_reg_index(ctx, t0);
6223     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6224     tcg_temp_free(t0);
6225     if (Rc(ctx->opcode)) {
6226         int l1 = gen_new_label();
6227         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6228         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6229         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6230         gen_set_label(l1);
6231     }
6232 #endif
6233 }
6234
6235 /* tlbwe */
6236 static void gen_tlbwe_440(DisasContext *ctx)
6237 {
6238 #if defined(CONFIG_USER_ONLY)
6239     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6240 #else
6241     if (unlikely(!ctx->mem_idx)) {
6242         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6243         return;
6244     }
6245     switch (rB(ctx->opcode)) {
6246     case 0:
6247     case 1:
6248     case 2:
6249         {
6250             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6251             gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6252                                  cpu_gpr[rS(ctx->opcode)]);
6253             tcg_temp_free_i32(t0);
6254         }
6255         break;
6256     default:
6257         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6258         break;
6259     }
6260 #endif
6261 }
6262
6263 /* TLB management - PowerPC BookE 2.06 implementation */
6264
6265 /* tlbre */
6266 static void gen_tlbre_booke206(DisasContext *ctx)
6267 {
6268 #if defined(CONFIG_USER_ONLY)
6269     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6270 #else
6271     if (unlikely(!ctx->mem_idx)) {
6272         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6273         return;
6274     }
6275
6276     gen_helper_booke206_tlbre(cpu_env);
6277 #endif
6278 }
6279
6280 /* tlbsx - tlbsx. */
6281 static void gen_tlbsx_booke206(DisasContext *ctx)
6282 {
6283 #if defined(CONFIG_USER_ONLY)
6284     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6285 #else
6286     TCGv t0;
6287     if (unlikely(!ctx->mem_idx)) {
6288         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6289         return;
6290     }
6291
6292     if (rA(ctx->opcode)) {
6293         t0 = tcg_temp_new();
6294         tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6295     } else {
6296         t0 = tcg_const_tl(0);
6297     }
6298
6299     tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6300     gen_helper_booke206_tlbsx(cpu_env, t0);
6301 #endif
6302 }
6303
6304 /* tlbwe */
6305 static void gen_tlbwe_booke206(DisasContext *ctx)
6306 {
6307 #if defined(CONFIG_USER_ONLY)
6308     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6309 #else
6310     if (unlikely(!ctx->mem_idx)) {
6311         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6312         return;
6313     }
6314     gen_update_nip(ctx, ctx->nip - 4);
6315     gen_helper_booke206_tlbwe(cpu_env);
6316 #endif
6317 }
6318
6319 static void gen_tlbivax_booke206(DisasContext *ctx)
6320 {
6321 #if defined(CONFIG_USER_ONLY)
6322     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6323 #else
6324     TCGv t0;
6325     if (unlikely(!ctx->mem_idx)) {
6326         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6327         return;
6328     }
6329
6330     t0 = tcg_temp_new();
6331     gen_addr_reg_index(ctx, t0);
6332
6333     gen_helper_booke206_tlbivax(cpu_env, t0);
6334 #endif
6335 }
6336
6337 static void gen_tlbilx_booke206(DisasContext *ctx)
6338 {
6339 #if defined(CONFIG_USER_ONLY)
6340     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6341 #else
6342     TCGv t0;
6343     if (unlikely(!ctx->mem_idx)) {
6344         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6345         return;
6346     }
6347
6348     t0 = tcg_temp_new();
6349     gen_addr_reg_index(ctx, t0);
6350
6351     switch((ctx->opcode >> 21) & 0x3) {
6352     case 0:
6353         gen_helper_booke206_tlbilx0(cpu_env, t0);
6354         break;
6355     case 1:
6356         gen_helper_booke206_tlbilx1(cpu_env, t0);
6357         break;
6358     case 3:
6359         gen_helper_booke206_tlbilx3(cpu_env, t0);
6360         break;
6361     default:
6362         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6363         break;
6364     }
6365
6366     tcg_temp_free(t0);
6367 #endif
6368 }
6369
6370
6371 /* wrtee */
6372 static void gen_wrtee(DisasContext *ctx)
6373 {
6374 #if defined(CONFIG_USER_ONLY)
6375     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6376 #else
6377     TCGv t0;
6378     if (unlikely(!ctx->mem_idx)) {
6379         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6380         return;
6381     }
6382     t0 = tcg_temp_new();
6383     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6384     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6385     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6386     tcg_temp_free(t0);
6387     /* Stop translation to have a chance to raise an exception
6388      * if we just set msr_ee to 1
6389      */
6390     gen_stop_exception(ctx);
6391 #endif
6392 }
6393
6394 /* wrteei */
6395 static void gen_wrteei(DisasContext *ctx)
6396 {
6397 #if defined(CONFIG_USER_ONLY)
6398     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6399 #else
6400     if (unlikely(!ctx->mem_idx)) {
6401         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6402         return;
6403     }
6404     if (ctx->opcode & 0x00008000) {
6405         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6406         /* Stop translation to have a chance to raise an exception */
6407         gen_stop_exception(ctx);
6408     } else {
6409         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6410     }
6411 #endif
6412 }
6413
6414 /* PowerPC 440 specific instructions */
6415
6416 /* dlmzb */
6417 static void gen_dlmzb(DisasContext *ctx)
6418 {
6419     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6420     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6421                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6422     tcg_temp_free_i32(t0);
6423 }
6424
6425 /* mbar replaces eieio on 440 */
6426 static void gen_mbar(DisasContext *ctx)
6427 {
6428     /* interpreted as no-op */
6429 }
6430
6431 /* msync replaces sync on 440 */
6432 static void gen_msync_4xx(DisasContext *ctx)
6433 {
6434     /* interpreted as no-op */
6435 }
6436
6437 /* icbt */
6438 static void gen_icbt_440(DisasContext *ctx)
6439 {
6440     /* interpreted as no-op */
6441     /* XXX: specification say this is treated as a load by the MMU
6442      *      but does not generate any exception
6443      */
6444 }
6445
6446 /* Embedded.Processor Control */
6447
6448 static void gen_msgclr(DisasContext *ctx)
6449 {
6450 #if defined(CONFIG_USER_ONLY)
6451     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6452 #else
6453     if (unlikely(ctx->mem_idx == 0)) {
6454         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6455         return;
6456     }
6457
6458     gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6459 #endif
6460 }
6461
6462 static void gen_msgsnd(DisasContext *ctx)
6463 {
6464 #if defined(CONFIG_USER_ONLY)
6465     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6466 #else
6467     if (unlikely(ctx->mem_idx == 0)) {
6468         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6469         return;
6470     }
6471
6472     gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6473 #endif
6474 }
6475
6476 /***                      Altivec vector extension                         ***/
6477 /* Altivec registers moves */
6478
6479 static inline TCGv_ptr gen_avr_ptr(int reg)
6480 {
6481     TCGv_ptr r = tcg_temp_new_ptr();
6482     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6483     return r;
6484 }
6485
6486 #define GEN_VR_LDX(name, opc2, opc3)                                          \
6487 static void glue(gen_, name)(DisasContext *ctx)                                       \
6488 {                                                                             \
6489     TCGv EA;                                                                  \
6490     if (unlikely(!ctx->altivec_enabled)) {                                    \
6491         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6492         return;                                                               \
6493     }                                                                         \
6494     gen_set_access_type(ctx, ACCESS_INT);                                     \
6495     EA = tcg_temp_new();                                                      \
6496     gen_addr_reg_index(ctx, EA);                                              \
6497     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6498     if (ctx->le_mode) {                                                       \
6499         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6500         tcg_gen_addi_tl(EA, EA, 8);                                           \
6501         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6502     } else {                                                                  \
6503         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6504         tcg_gen_addi_tl(EA, EA, 8);                                           \
6505         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6506     }                                                                         \
6507     tcg_temp_free(EA);                                                        \
6508 }
6509
6510 #define GEN_VR_STX(name, opc2, opc3)                                          \
6511 static void gen_st##name(DisasContext *ctx)                                   \
6512 {                                                                             \
6513     TCGv EA;                                                                  \
6514     if (unlikely(!ctx->altivec_enabled)) {                                    \
6515         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6516         return;                                                               \
6517     }                                                                         \
6518     gen_set_access_type(ctx, ACCESS_INT);                                     \
6519     EA = tcg_temp_new();                                                      \
6520     gen_addr_reg_index(ctx, EA);                                              \
6521     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6522     if (ctx->le_mode) {                                                       \
6523         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6524         tcg_gen_addi_tl(EA, EA, 8);                                           \
6525         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6526     } else {                                                                  \
6527         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6528         tcg_gen_addi_tl(EA, EA, 8);                                           \
6529         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6530     }                                                                         \
6531     tcg_temp_free(EA);                                                        \
6532 }
6533
6534 #define GEN_VR_LVE(name, opc2, opc3)                                    \
6535 static void gen_lve##name(DisasContext *ctx)                            \
6536     {                                                                   \
6537         TCGv EA;                                                        \
6538         TCGv_ptr rs;                                                    \
6539         if (unlikely(!ctx->altivec_enabled)) {                          \
6540             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6541             return;                                                     \
6542         }                                                               \
6543         gen_set_access_type(ctx, ACCESS_INT);                           \
6544         EA = tcg_temp_new();                                            \
6545         gen_addr_reg_index(ctx, EA);                                    \
6546         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6547         gen_helper_lve##name(cpu_env, rs, EA);                          \
6548         tcg_temp_free(EA);                                              \
6549         tcg_temp_free_ptr(rs);                                          \
6550     }
6551
6552 #define GEN_VR_STVE(name, opc2, opc3)                                   \
6553 static void gen_stve##name(DisasContext *ctx)                           \
6554     {                                                                   \
6555         TCGv EA;                                                        \
6556         TCGv_ptr rs;                                                    \
6557         if (unlikely(!ctx->altivec_enabled)) {                          \
6558             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6559             return;                                                     \
6560         }                                                               \
6561         gen_set_access_type(ctx, ACCESS_INT);                           \
6562         EA = tcg_temp_new();                                            \
6563         gen_addr_reg_index(ctx, EA);                                    \
6564         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6565         gen_helper_stve##name(cpu_env, rs, EA);                         \
6566         tcg_temp_free(EA);                                              \
6567         tcg_temp_free_ptr(rs);                                          \
6568     }
6569
6570 GEN_VR_LDX(lvx, 0x07, 0x03);
6571 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6572 GEN_VR_LDX(lvxl, 0x07, 0x0B);
6573
6574 GEN_VR_LVE(bx, 0x07, 0x00);
6575 GEN_VR_LVE(hx, 0x07, 0x01);
6576 GEN_VR_LVE(wx, 0x07, 0x02);
6577
6578 GEN_VR_STX(svx, 0x07, 0x07);
6579 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6580 GEN_VR_STX(svxl, 0x07, 0x0F);
6581
6582 GEN_VR_STVE(bx, 0x07, 0x04);
6583 GEN_VR_STVE(hx, 0x07, 0x05);
6584 GEN_VR_STVE(wx, 0x07, 0x06);
6585
6586 static void gen_lvsl(DisasContext *ctx)
6587 {
6588     TCGv_ptr rd;
6589     TCGv EA;
6590     if (unlikely(!ctx->altivec_enabled)) {
6591         gen_exception(ctx, POWERPC_EXCP_VPU);
6592         return;
6593     }
6594     EA = tcg_temp_new();
6595     gen_addr_reg_index(ctx, EA);
6596     rd = gen_avr_ptr(rD(ctx->opcode));
6597     gen_helper_lvsl(rd, EA);
6598     tcg_temp_free(EA);
6599     tcg_temp_free_ptr(rd);
6600 }
6601
6602 static void gen_lvsr(DisasContext *ctx)
6603 {
6604     TCGv_ptr rd;
6605     TCGv EA;
6606     if (unlikely(!ctx->altivec_enabled)) {
6607         gen_exception(ctx, POWERPC_EXCP_VPU);
6608         return;
6609     }
6610     EA = tcg_temp_new();
6611     gen_addr_reg_index(ctx, EA);
6612     rd = gen_avr_ptr(rD(ctx->opcode));
6613     gen_helper_lvsr(rd, EA);
6614     tcg_temp_free(EA);
6615     tcg_temp_free_ptr(rd);
6616 }
6617
6618 static void gen_mfvscr(DisasContext *ctx)
6619 {
6620     TCGv_i32 t;
6621     if (unlikely(!ctx->altivec_enabled)) {
6622         gen_exception(ctx, POWERPC_EXCP_VPU);
6623         return;
6624     }
6625     tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6626     t = tcg_temp_new_i32();
6627     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
6628     tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
6629     tcg_temp_free_i32(t);
6630 }
6631
6632 static void gen_mtvscr(DisasContext *ctx)
6633 {
6634     TCGv_ptr p;
6635     if (unlikely(!ctx->altivec_enabled)) {
6636         gen_exception(ctx, POWERPC_EXCP_VPU);
6637         return;
6638     }
6639     p = gen_avr_ptr(rD(ctx->opcode));
6640     gen_helper_mtvscr(cpu_env, p);
6641     tcg_temp_free_ptr(p);
6642 }
6643
6644 /* Logical operations */
6645 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
6646 static void glue(gen_, name)(DisasContext *ctx)                                 \
6647 {                                                                       \
6648     if (unlikely(!ctx->altivec_enabled)) {                              \
6649         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6650         return;                                                         \
6651     }                                                                   \
6652     tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6653     tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6654 }
6655
6656 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6657 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6658 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6659 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6660 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6661
6662 #define GEN_VXFORM(name, opc2, opc3)                                    \
6663 static void glue(gen_, name)(DisasContext *ctx)                                 \
6664 {                                                                       \
6665     TCGv_ptr ra, rb, rd;                                                \
6666     if (unlikely(!ctx->altivec_enabled)) {                              \
6667         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6668         return;                                                         \
6669     }                                                                   \
6670     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6671     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6672     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6673     gen_helper_##name (rd, ra, rb);                                     \
6674     tcg_temp_free_ptr(ra);                                              \
6675     tcg_temp_free_ptr(rb);                                              \
6676     tcg_temp_free_ptr(rd);                                              \
6677 }
6678
6679 #define GEN_VXFORM_ENV(name, opc2, opc3)                                \
6680 static void glue(gen_, name)(DisasContext *ctx)                         \
6681 {                                                                       \
6682     TCGv_ptr ra, rb, rd;                                                \
6683     if (unlikely(!ctx->altivec_enabled)) {                              \
6684         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6685         return;                                                         \
6686     }                                                                   \
6687     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6688     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6689     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6690     gen_helper_##name(cpu_env, rd, ra, rb);                             \
6691     tcg_temp_free_ptr(ra);                                              \
6692     tcg_temp_free_ptr(rb);                                              \
6693     tcg_temp_free_ptr(rd);                                              \
6694 }
6695
6696 GEN_VXFORM(vaddubm, 0, 0);
6697 GEN_VXFORM(vadduhm, 0, 1);
6698 GEN_VXFORM(vadduwm, 0, 2);
6699 GEN_VXFORM(vsububm, 0, 16);
6700 GEN_VXFORM(vsubuhm, 0, 17);
6701 GEN_VXFORM(vsubuwm, 0, 18);
6702 GEN_VXFORM(vmaxub, 1, 0);
6703 GEN_VXFORM(vmaxuh, 1, 1);
6704 GEN_VXFORM(vmaxuw, 1, 2);
6705 GEN_VXFORM(vmaxsb, 1, 4);
6706 GEN_VXFORM(vmaxsh, 1, 5);
6707 GEN_VXFORM(vmaxsw, 1, 6);
6708 GEN_VXFORM(vminub, 1, 8);
6709 GEN_VXFORM(vminuh, 1, 9);
6710 GEN_VXFORM(vminuw, 1, 10);
6711 GEN_VXFORM(vminsb, 1, 12);
6712 GEN_VXFORM(vminsh, 1, 13);
6713 GEN_VXFORM(vminsw, 1, 14);
6714 GEN_VXFORM(vavgub, 1, 16);
6715 GEN_VXFORM(vavguh, 1, 17);
6716 GEN_VXFORM(vavguw, 1, 18);
6717 GEN_VXFORM(vavgsb, 1, 20);
6718 GEN_VXFORM(vavgsh, 1, 21);
6719 GEN_VXFORM(vavgsw, 1, 22);
6720 GEN_VXFORM(vmrghb, 6, 0);
6721 GEN_VXFORM(vmrghh, 6, 1);
6722 GEN_VXFORM(vmrghw, 6, 2);
6723 GEN_VXFORM(vmrglb, 6, 4);
6724 GEN_VXFORM(vmrglh, 6, 5);
6725 GEN_VXFORM(vmrglw, 6, 6);
6726 GEN_VXFORM(vmuloub, 4, 0);
6727 GEN_VXFORM(vmulouh, 4, 1);
6728 GEN_VXFORM(vmulosb, 4, 4);
6729 GEN_VXFORM(vmulosh, 4, 5);
6730 GEN_VXFORM(vmuleub, 4, 8);
6731 GEN_VXFORM(vmuleuh, 4, 9);
6732 GEN_VXFORM(vmulesb, 4, 12);
6733 GEN_VXFORM(vmulesh, 4, 13);
6734 GEN_VXFORM(vslb, 2, 4);
6735 GEN_VXFORM(vslh, 2, 5);
6736 GEN_VXFORM(vslw, 2, 6);
6737 GEN_VXFORM(vsrb, 2, 8);
6738 GEN_VXFORM(vsrh, 2, 9);
6739 GEN_VXFORM(vsrw, 2, 10);
6740 GEN_VXFORM(vsrab, 2, 12);
6741 GEN_VXFORM(vsrah, 2, 13);
6742 GEN_VXFORM(vsraw, 2, 14);
6743 GEN_VXFORM(vslo, 6, 16);
6744 GEN_VXFORM(vsro, 6, 17);
6745 GEN_VXFORM(vaddcuw, 0, 6);
6746 GEN_VXFORM(vsubcuw, 0, 22);
6747 GEN_VXFORM_ENV(vaddubs, 0, 8);
6748 GEN_VXFORM_ENV(vadduhs, 0, 9);
6749 GEN_VXFORM_ENV(vadduws, 0, 10);
6750 GEN_VXFORM_ENV(vaddsbs, 0, 12);
6751 GEN_VXFORM_ENV(vaddshs, 0, 13);
6752 GEN_VXFORM_ENV(vaddsws, 0, 14);
6753 GEN_VXFORM_ENV(vsububs, 0, 24);
6754 GEN_VXFORM_ENV(vsubuhs, 0, 25);
6755 GEN_VXFORM_ENV(vsubuws, 0, 26);
6756 GEN_VXFORM_ENV(vsubsbs, 0, 28);
6757 GEN_VXFORM_ENV(vsubshs, 0, 29);
6758 GEN_VXFORM_ENV(vsubsws, 0, 30);
6759 GEN_VXFORM(vrlb, 2, 0);
6760 GEN_VXFORM(vrlh, 2, 1);
6761 GEN_VXFORM(vrlw, 2, 2);
6762 GEN_VXFORM(vsl, 2, 7);
6763 GEN_VXFORM(vsr, 2, 11);
6764 GEN_VXFORM_ENV(vpkuhum, 7, 0);
6765 GEN_VXFORM_ENV(vpkuwum, 7, 1);
6766 GEN_VXFORM_ENV(vpkuhus, 7, 2);
6767 GEN_VXFORM_ENV(vpkuwus, 7, 3);
6768 GEN_VXFORM_ENV(vpkshus, 7, 4);
6769 GEN_VXFORM_ENV(vpkswus, 7, 5);
6770 GEN_VXFORM_ENV(vpkshss, 7, 6);
6771 GEN_VXFORM_ENV(vpkswss, 7, 7);
6772 GEN_VXFORM(vpkpx, 7, 12);
6773 GEN_VXFORM_ENV(vsum4ubs, 4, 24);
6774 GEN_VXFORM_ENV(vsum4sbs, 4, 28);
6775 GEN_VXFORM_ENV(vsum4shs, 4, 25);
6776 GEN_VXFORM_ENV(vsum2sws, 4, 26);
6777 GEN_VXFORM_ENV(vsumsws, 4, 30);
6778 GEN_VXFORM_ENV(vaddfp, 5, 0);
6779 GEN_VXFORM_ENV(vsubfp, 5, 1);
6780 GEN_VXFORM_ENV(vmaxfp, 5, 16);
6781 GEN_VXFORM_ENV(vminfp, 5, 17);
6782
6783 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
6784 static void glue(gen_, name)(DisasContext *ctx)                         \
6785     {                                                                   \
6786         TCGv_ptr ra, rb, rd;                                            \
6787         if (unlikely(!ctx->altivec_enabled)) {                          \
6788             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6789             return;                                                     \
6790         }                                                               \
6791         ra = gen_avr_ptr(rA(ctx->opcode));                              \
6792         rb = gen_avr_ptr(rB(ctx->opcode));                              \
6793         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6794         gen_helper_##opname(cpu_env, rd, ra, rb);                       \
6795         tcg_temp_free_ptr(ra);                                          \
6796         tcg_temp_free_ptr(rb);                                          \
6797         tcg_temp_free_ptr(rd);                                          \
6798     }
6799
6800 #define GEN_VXRFORM(name, opc2, opc3)                                \
6801     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
6802     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
6803
6804 GEN_VXRFORM(vcmpequb, 3, 0)
6805 GEN_VXRFORM(vcmpequh, 3, 1)
6806 GEN_VXRFORM(vcmpequw, 3, 2)
6807 GEN_VXRFORM(vcmpgtsb, 3, 12)
6808 GEN_VXRFORM(vcmpgtsh, 3, 13)
6809 GEN_VXRFORM(vcmpgtsw, 3, 14)
6810 GEN_VXRFORM(vcmpgtub, 3, 8)
6811 GEN_VXRFORM(vcmpgtuh, 3, 9)
6812 GEN_VXRFORM(vcmpgtuw, 3, 10)
6813 GEN_VXRFORM(vcmpeqfp, 3, 3)
6814 GEN_VXRFORM(vcmpgefp, 3, 7)
6815 GEN_VXRFORM(vcmpgtfp, 3, 11)
6816 GEN_VXRFORM(vcmpbfp, 3, 15)
6817
6818 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
6819 static void glue(gen_, name)(DisasContext *ctx)                         \
6820     {                                                                   \
6821         TCGv_ptr rd;                                                    \
6822         TCGv_i32 simm;                                                  \
6823         if (unlikely(!ctx->altivec_enabled)) {                          \
6824             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6825             return;                                                     \
6826         }                                                               \
6827         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
6828         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6829         gen_helper_##name (rd, simm);                                   \
6830         tcg_temp_free_i32(simm);                                        \
6831         tcg_temp_free_ptr(rd);                                          \
6832     }
6833
6834 GEN_VXFORM_SIMM(vspltisb, 6, 12);
6835 GEN_VXFORM_SIMM(vspltish, 6, 13);
6836 GEN_VXFORM_SIMM(vspltisw, 6, 14);
6837
6838 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
6839 static void glue(gen_, name)(DisasContext *ctx)                                 \
6840     {                                                                   \
6841         TCGv_ptr rb, rd;                                                \
6842         if (unlikely(!ctx->altivec_enabled)) {                          \
6843             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6844             return;                                                     \
6845         }                                                               \
6846         rb = gen_avr_ptr(rB(ctx->opcode));                              \
6847         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6848         gen_helper_##name (rd, rb);                                     \
6849         tcg_temp_free_ptr(rb);                                          \
6850         tcg_temp_free_ptr(rd);                                         \
6851     }
6852
6853 #define GEN_VXFORM_NOA_ENV(name, opc2, opc3)                            \
6854 static void glue(gen_, name)(DisasContext *ctx)                         \
6855     {                                                                   \
6856         TCGv_ptr rb, rd;                                                \
6857                                                                         \
6858         if (unlikely(!ctx->altivec_enabled)) {                          \
6859             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6860             return;                                                     \
6861         }                                                               \
6862         rb = gen_avr_ptr(rB(ctx->opcode));                              \
6863         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6864         gen_helper_##name(cpu_env, rd, rb);                             \
6865         tcg_temp_free_ptr(rb);                                          \
6866         tcg_temp_free_ptr(rd);                                          \
6867     }
6868
6869 GEN_VXFORM_NOA(vupkhsb, 7, 8);
6870 GEN_VXFORM_NOA(vupkhsh, 7, 9);
6871 GEN_VXFORM_NOA(vupklsb, 7, 10);
6872 GEN_VXFORM_NOA(vupklsh, 7, 11);
6873 GEN_VXFORM_NOA(vupkhpx, 7, 13);
6874 GEN_VXFORM_NOA(vupklpx, 7, 15);
6875 GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
6876 GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
6877 GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
6878 GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
6879 GEN_VXFORM_NOA_ENV(vrfim, 5, 8);
6880 GEN_VXFORM_NOA_ENV(vrfin, 5, 9);
6881 GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
6882 GEN_VXFORM_NOA_ENV(vrfiz, 5, 11);
6883
6884 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
6885 static void glue(gen_, name)(DisasContext *ctx)                                 \
6886     {                                                                   \
6887         TCGv_ptr rd;                                                    \
6888         TCGv_i32 simm;                                                  \
6889         if (unlikely(!ctx->altivec_enabled)) {                          \
6890             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6891             return;                                                     \
6892         }                                                               \
6893         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
6894         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6895         gen_helper_##name (rd, simm);                                   \
6896         tcg_temp_free_i32(simm);                                        \
6897         tcg_temp_free_ptr(rd);                                          \
6898     }
6899
6900 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
6901 static void glue(gen_, name)(DisasContext *ctx)                                 \
6902     {                                                                   \
6903         TCGv_ptr rb, rd;                                                \
6904         TCGv_i32 uimm;                                                  \
6905         if (unlikely(!ctx->altivec_enabled)) {                          \
6906             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6907             return;                                                     \
6908         }                                                               \
6909         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
6910         rb = gen_avr_ptr(rB(ctx->opcode));                              \
6911         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6912         gen_helper_##name (rd, rb, uimm);                               \
6913         tcg_temp_free_i32(uimm);                                        \
6914         tcg_temp_free_ptr(rb);                                          \
6915         tcg_temp_free_ptr(rd);                                          \
6916     }
6917
6918 #define GEN_VXFORM_UIMM_ENV(name, opc2, opc3)                           \
6919 static void glue(gen_, name)(DisasContext *ctx)                         \
6920     {                                                                   \
6921         TCGv_ptr rb, rd;                                                \
6922         TCGv_i32 uimm;                                                  \
6923                                                                         \
6924         if (unlikely(!ctx->altivec_enabled)) {                          \
6925             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6926             return;                                                     \
6927         }                                                               \
6928         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
6929         rb = gen_avr_ptr(rB(ctx->opcode));                              \
6930         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6931         gen_helper_##name(cpu_env, rd, rb, uimm);                       \
6932         tcg_temp_free_i32(uimm);                                        \
6933         tcg_temp_free_ptr(rb);                                          \
6934         tcg_temp_free_ptr(rd);                                          \
6935     }
6936
6937 GEN_VXFORM_UIMM(vspltb, 6, 8);
6938 GEN_VXFORM_UIMM(vsplth, 6, 9);
6939 GEN_VXFORM_UIMM(vspltw, 6, 10);
6940 GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
6941 GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
6942 GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
6943 GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
6944
6945 static void gen_vsldoi(DisasContext *ctx)
6946 {
6947     TCGv_ptr ra, rb, rd;
6948     TCGv_i32 sh;
6949     if (unlikely(!ctx->altivec_enabled)) {
6950         gen_exception(ctx, POWERPC_EXCP_VPU);
6951         return;
6952     }
6953     ra = gen_avr_ptr(rA(ctx->opcode));
6954     rb = gen_avr_ptr(rB(ctx->opcode));
6955     rd = gen_avr_ptr(rD(ctx->opcode));
6956     sh = tcg_const_i32(VSH(ctx->opcode));
6957     gen_helper_vsldoi (rd, ra, rb, sh);
6958     tcg_temp_free_ptr(ra);
6959     tcg_temp_free_ptr(rb);
6960     tcg_temp_free_ptr(rd);
6961     tcg_temp_free_i32(sh);
6962 }
6963
6964 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
6965 static void glue(gen_, name0##_##name1)(DisasContext *ctx)              \
6966     {                                                                   \
6967         TCGv_ptr ra, rb, rc, rd;                                        \
6968         if (unlikely(!ctx->altivec_enabled)) {                          \
6969             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6970             return;                                                     \
6971         }                                                               \
6972         ra = gen_avr_ptr(rA(ctx->opcode));                              \
6973         rb = gen_avr_ptr(rB(ctx->opcode));                              \
6974         rc = gen_avr_ptr(rC(ctx->opcode));                              \
6975         rd = gen_avr_ptr(rD(ctx->opcode));                              \
6976         if (Rc(ctx->opcode)) {                                          \
6977             gen_helper_##name1(cpu_env, rd, ra, rb, rc);                \
6978         } else {                                                        \
6979             gen_helper_##name0(cpu_env, rd, ra, rb, rc);                \
6980         }                                                               \
6981         tcg_temp_free_ptr(ra);                                          \
6982         tcg_temp_free_ptr(rb);                                          \
6983         tcg_temp_free_ptr(rc);                                          \
6984         tcg_temp_free_ptr(rd);                                          \
6985     }
6986
6987 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
6988
6989 static void gen_vmladduhm(DisasContext *ctx)
6990 {
6991     TCGv_ptr ra, rb, rc, rd;
6992     if (unlikely(!ctx->altivec_enabled)) {
6993         gen_exception(ctx, POWERPC_EXCP_VPU);
6994         return;
6995     }
6996     ra = gen_avr_ptr(rA(ctx->opcode));
6997     rb = gen_avr_ptr(rB(ctx->opcode));
6998     rc = gen_avr_ptr(rC(ctx->opcode));
6999     rd = gen_avr_ptr(rD(ctx->opcode));
7000     gen_helper_vmladduhm(rd, ra, rb, rc);
7001     tcg_temp_free_ptr(ra);
7002     tcg_temp_free_ptr(rb);
7003     tcg_temp_free_ptr(rc);
7004     tcg_temp_free_ptr(rd);
7005 }
7006
7007 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
7008 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
7009 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
7010 GEN_VAFORM_PAIRED(vsel, vperm, 21)
7011 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
7012
7013 /***                           VSX extension                               ***/
7014
7015 static inline TCGv_i64 cpu_vsrh(int n)
7016 {
7017     if (n < 32) {
7018         return cpu_fpr[n];
7019     } else {
7020         return cpu_avrh[n-32];
7021     }
7022 }
7023
7024 static inline TCGv_i64 cpu_vsrl(int n)
7025 {
7026     if (n < 32) {
7027         return cpu_vsr[n];
7028     } else {
7029         return cpu_avrl[n-32];
7030     }
7031 }
7032
7033 #define VSX_LOAD_SCALAR(name, operation)                      \
7034 static void gen_##name(DisasContext *ctx)                     \
7035 {                                                             \
7036     TCGv EA;                                                  \
7037     if (unlikely(!ctx->vsx_enabled)) {                        \
7038         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7039         return;                                               \
7040     }                                                         \
7041     gen_set_access_type(ctx, ACCESS_INT);                     \
7042     EA = tcg_temp_new();                                      \
7043     gen_addr_reg_index(ctx, EA);                              \
7044     gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7045     /* NOTE: cpu_vsrl is undefined */                         \
7046     tcg_temp_free(EA);                                        \
7047 }
7048
7049 VSX_LOAD_SCALAR(lxsdx, ld64)
7050 VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7051 VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7052 VSX_LOAD_SCALAR(lxsspx, ld32fs)
7053
7054 static void gen_lxvd2x(DisasContext *ctx)
7055 {
7056     TCGv EA;
7057     if (unlikely(!ctx->vsx_enabled)) {
7058         gen_exception(ctx, POWERPC_EXCP_VSXU);
7059         return;
7060     }
7061     gen_set_access_type(ctx, ACCESS_INT);
7062     EA = tcg_temp_new();
7063     gen_addr_reg_index(ctx, EA);
7064     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7065     tcg_gen_addi_tl(EA, EA, 8);
7066     gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7067     tcg_temp_free(EA);
7068 }
7069
7070 static void gen_lxvdsx(DisasContext *ctx)
7071 {
7072     TCGv EA;
7073     if (unlikely(!ctx->vsx_enabled)) {
7074         gen_exception(ctx, POWERPC_EXCP_VSXU);
7075         return;
7076     }
7077     gen_set_access_type(ctx, ACCESS_INT);
7078     EA = tcg_temp_new();
7079     gen_addr_reg_index(ctx, EA);
7080     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7081     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7082     tcg_temp_free(EA);
7083 }
7084
7085 static void gen_lxvw4x(DisasContext *ctx)
7086 {
7087     TCGv EA;
7088     TCGv_i64 tmp;
7089     TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7090     TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7091     if (unlikely(!ctx->vsx_enabled)) {
7092         gen_exception(ctx, POWERPC_EXCP_VSXU);
7093         return;
7094     }
7095     gen_set_access_type(ctx, ACCESS_INT);
7096     EA = tcg_temp_new();
7097     tmp = tcg_temp_new_i64();
7098
7099     gen_addr_reg_index(ctx, EA);
7100     gen_qemu_ld32u_i64(ctx, tmp, EA);
7101     tcg_gen_addi_tl(EA, EA, 4);
7102     gen_qemu_ld32u_i64(ctx, xth, EA);
7103     tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7104
7105     tcg_gen_addi_tl(EA, EA, 4);
7106     gen_qemu_ld32u_i64(ctx, tmp, EA);
7107     tcg_gen_addi_tl(EA, EA, 4);
7108     gen_qemu_ld32u_i64(ctx, xtl, EA);
7109     tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7110
7111     tcg_temp_free(EA);
7112     tcg_temp_free_i64(tmp);
7113 }
7114
7115 #define VSX_STORE_SCALAR(name, operation)                     \
7116 static void gen_##name(DisasContext *ctx)                     \
7117 {                                                             \
7118     TCGv EA;                                                  \
7119     if (unlikely(!ctx->vsx_enabled)) {                        \
7120         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7121         return;                                               \
7122     }                                                         \
7123     gen_set_access_type(ctx, ACCESS_INT);                     \
7124     EA = tcg_temp_new();                                      \
7125     gen_addr_reg_index(ctx, EA);                              \
7126     gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7127     tcg_temp_free(EA);                                        \
7128 }
7129
7130 VSX_STORE_SCALAR(stxsdx, st64)
7131 VSX_STORE_SCALAR(stxsiwx, st32_i64)
7132 VSX_STORE_SCALAR(stxsspx, st32fs)
7133
7134 static void gen_stxvd2x(DisasContext *ctx)
7135 {
7136     TCGv EA;
7137     if (unlikely(!ctx->vsx_enabled)) {
7138         gen_exception(ctx, POWERPC_EXCP_VSXU);
7139         return;
7140     }
7141     gen_set_access_type(ctx, ACCESS_INT);
7142     EA = tcg_temp_new();
7143     gen_addr_reg_index(ctx, EA);
7144     gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7145     tcg_gen_addi_tl(EA, EA, 8);
7146     gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7147     tcg_temp_free(EA);
7148 }
7149
7150 static void gen_stxvw4x(DisasContext *ctx)
7151 {
7152     TCGv_i64 tmp;
7153     TCGv EA;
7154     if (unlikely(!ctx->vsx_enabled)) {
7155         gen_exception(ctx, POWERPC_EXCP_VSXU);
7156         return;
7157     }
7158     gen_set_access_type(ctx, ACCESS_INT);
7159     EA = tcg_temp_new();
7160     gen_addr_reg_index(ctx, EA);
7161     tmp = tcg_temp_new_i64();
7162
7163     tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
7164     gen_qemu_st32_i64(ctx, tmp, EA);
7165     tcg_gen_addi_tl(EA, EA, 4);
7166     gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7167
7168     tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7169     tcg_gen_addi_tl(EA, EA, 4);
7170     gen_qemu_st32_i64(ctx, tmp, EA);
7171     tcg_gen_addi_tl(EA, EA, 4);
7172     gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7173
7174     tcg_temp_free(EA);
7175     tcg_temp_free_i64(tmp);
7176 }
7177
7178 #define MV_VSRW(name, tcgop1, tcgop2, target, source)           \
7179 static void gen_##name(DisasContext *ctx)                       \
7180 {                                                               \
7181     if (xS(ctx->opcode) < 32) {                                 \
7182         if (unlikely(!ctx->fpu_enabled)) {                      \
7183             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7184             return;                                             \
7185         }                                                       \
7186     } else {                                                    \
7187         if (unlikely(!ctx->altivec_enabled)) {                  \
7188             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7189             return;                                             \
7190         }                                                       \
7191     }                                                           \
7192     TCGv_i64 tmp = tcg_temp_new_i64();                          \
7193     tcg_gen_##tcgop1(tmp, source);                              \
7194     tcg_gen_##tcgop2(target, tmp);                              \
7195     tcg_temp_free_i64(tmp);                                     \
7196 }
7197
7198
7199 MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7200         cpu_vsrh(xS(ctx->opcode)))
7201 MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7202         cpu_gpr[rA(ctx->opcode)])
7203 MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7204         cpu_gpr[rA(ctx->opcode)])
7205
7206 #if defined(TARGET_PPC64)
7207 #define MV_VSRD(name, target, source)                           \
7208 static void gen_##name(DisasContext *ctx)                       \
7209 {                                                               \
7210     if (xS(ctx->opcode) < 32) {                                 \
7211         if (unlikely(!ctx->fpu_enabled)) {                      \
7212             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7213             return;                                             \
7214         }                                                       \
7215     } else {                                                    \
7216         if (unlikely(!ctx->altivec_enabled)) {                  \
7217             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7218             return;                                             \
7219         }                                                       \
7220     }                                                           \
7221     tcg_gen_mov_i64(target, source);                            \
7222 }
7223
7224 MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7225 MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7226
7227 #endif
7228
7229 static void gen_xxpermdi(DisasContext *ctx)
7230 {
7231     if (unlikely(!ctx->vsx_enabled)) {
7232         gen_exception(ctx, POWERPC_EXCP_VSXU);
7233         return;
7234     }
7235
7236     if ((DM(ctx->opcode) & 2) == 0) {
7237         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7238     } else {
7239         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7240     }
7241     if ((DM(ctx->opcode) & 1) == 0) {
7242         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7243     } else {
7244         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7245     }
7246 }
7247
7248 #define OP_ABS 1
7249 #define OP_NABS 2
7250 #define OP_NEG 3
7251 #define OP_CPSGN 4
7252 #define SGN_MASK_DP  0x8000000000000000ul
7253 #define SGN_MASK_SP 0x8000000080000000ul
7254
7255 #define VSX_SCALAR_MOVE(name, op, sgn_mask)                       \
7256 static void glue(gen_, name)(DisasContext * ctx)                  \
7257     {                                                             \
7258         TCGv_i64 xb, sgm;                                         \
7259         if (unlikely(!ctx->vsx_enabled)) {                        \
7260             gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7261             return;                                               \
7262         }                                                         \
7263         xb = tcg_temp_new_i64();                                  \
7264         sgm = tcg_temp_new_i64();                                 \
7265         tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode)));           \
7266         tcg_gen_movi_i64(sgm, sgn_mask);                          \
7267         switch (op) {                                             \
7268             case OP_ABS: {                                        \
7269                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7270                 break;                                            \
7271             }                                                     \
7272             case OP_NABS: {                                       \
7273                 tcg_gen_or_i64(xb, xb, sgm);                      \
7274                 break;                                            \
7275             }                                                     \
7276             case OP_NEG: {                                        \
7277                 tcg_gen_xor_i64(xb, xb, sgm);                     \
7278                 break;                                            \
7279             }                                                     \
7280             case OP_CPSGN: {                                      \
7281                 TCGv_i64 xa = tcg_temp_new_i64();                 \
7282                 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode)));   \
7283                 tcg_gen_and_i64(xa, xa, sgm);                     \
7284                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7285                 tcg_gen_or_i64(xb, xb, xa);                       \
7286                 tcg_temp_free_i64(xa);                            \
7287                 break;                                            \
7288             }                                                     \
7289         }                                                         \
7290         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb);           \
7291         tcg_temp_free_i64(xb);                                    \
7292         tcg_temp_free_i64(sgm);                                   \
7293     }
7294
7295 VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7296 VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7297 VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7298 VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7299
7300 #define VSX_VECTOR_MOVE(name, op, sgn_mask)                      \
7301 static void glue(gen_, name)(DisasContext * ctx)                 \
7302     {                                                            \
7303         TCGv_i64 xbh, xbl, sgm;                                  \
7304         if (unlikely(!ctx->vsx_enabled)) {                       \
7305             gen_exception(ctx, POWERPC_EXCP_VSXU);               \
7306             return;                                              \
7307         }                                                        \
7308         xbh = tcg_temp_new_i64();                                \
7309         xbl = tcg_temp_new_i64();                                \
7310         sgm = tcg_temp_new_i64();                                \
7311         tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode)));         \
7312         tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode)));         \
7313         tcg_gen_movi_i64(sgm, sgn_mask);                         \
7314         switch (op) {                                            \
7315             case OP_ABS: {                                       \
7316                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7317                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7318                 break;                                           \
7319             }                                                    \
7320             case OP_NABS: {                                      \
7321                 tcg_gen_or_i64(xbh, xbh, sgm);                   \
7322                 tcg_gen_or_i64(xbl, xbl, sgm);                   \
7323                 break;                                           \
7324             }                                                    \
7325             case OP_NEG: {                                       \
7326                 tcg_gen_xor_i64(xbh, xbh, sgm);                  \
7327                 tcg_gen_xor_i64(xbl, xbl, sgm);                  \
7328                 break;                                           \
7329             }                                                    \
7330             case OP_CPSGN: {                                     \
7331                 TCGv_i64 xah = tcg_temp_new_i64();               \
7332                 TCGv_i64 xal = tcg_temp_new_i64();               \
7333                 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7334                 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7335                 tcg_gen_and_i64(xah, xah, sgm);                  \
7336                 tcg_gen_and_i64(xal, xal, sgm);                  \
7337                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7338                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7339                 tcg_gen_or_i64(xbh, xbh, xah);                   \
7340                 tcg_gen_or_i64(xbl, xbl, xal);                   \
7341                 tcg_temp_free_i64(xah);                          \
7342                 tcg_temp_free_i64(xal);                          \
7343                 break;                                           \
7344             }                                                    \
7345         }                                                        \
7346         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh);         \
7347         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl);         \
7348         tcg_temp_free_i64(xbh);                                  \
7349         tcg_temp_free_i64(xbl);                                  \
7350         tcg_temp_free_i64(sgm);                                  \
7351     }
7352
7353 VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7354 VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7355 VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7356 VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7357 VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7358 VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7359 VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7360 VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7361
7362 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
7363 static void gen_##name(DisasContext * ctx)                                    \
7364 {                                                                             \
7365     TCGv_i32 opc;                                                             \
7366     if (unlikely(!ctx->vsx_enabled)) {                                        \
7367         gen_exception(ctx, POWERPC_EXCP_VSXU);                                \
7368         return;                                                               \
7369     }                                                                         \
7370     /* NIP cannot be restored if the memory exception comes from an helper */ \
7371     gen_update_nip(ctx, ctx->nip - 4);                                        \
7372     opc = tcg_const_i32(ctx->opcode);                                         \
7373     gen_helper_##name(cpu_env, opc);                                          \
7374     tcg_temp_free_i32(opc);                                                   \
7375 }
7376
7377 GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7378 GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
7379 GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
7380 GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
7381 GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
7382 GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
7383 GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
7384 GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
7385 GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
7386 GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7387 GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7388 GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7389 GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7390 GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7391 GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7392 GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7393 GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
7394 GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7395 GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
7396 GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7397 GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
7398 GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7399 GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
7400 GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7401 GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
7402 GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
7403 GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
7404 GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
7405 GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
7406 GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
7407 GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
7408 GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
7409 GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
7410 GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
7411
7412 GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
7413 GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
7414 GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
7415 GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
7416 GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
7417 GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
7418 GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
7419 GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
7420 GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
7421 GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
7422 GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
7423 GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
7424 GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
7425 GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
7426 GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
7427 GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
7428 GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
7429
7430 GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
7431 GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
7432 GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
7433 GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
7434 GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
7435 GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
7436 GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
7437 GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
7438 GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
7439 GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
7440 GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
7441 GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
7442 GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
7443 GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
7444 GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
7445 GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
7446 GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
7447 GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
7448 GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
7449 GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
7450 GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
7451 GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
7452 GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
7453 GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
7454 GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
7455 GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
7456 GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
7457 GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
7458 GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
7459 GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
7460 GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
7461 GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
7462 GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
7463 GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
7464 GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
7465 GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
7466
7467 GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
7468 GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
7469 GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
7470 GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
7471 GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
7472 GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
7473 GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
7474 GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
7475 GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
7476 GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
7477 GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
7478 GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
7479 GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
7480 GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
7481 GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
7482 GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
7483 GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
7484 GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
7485 GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
7486 GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
7487 GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
7488 GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
7489 GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
7490 GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
7491 GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
7492 GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
7493 GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
7494 GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
7495 GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
7496 GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
7497 GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
7498 GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
7499 GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
7500 GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
7501 GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
7502 GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
7503
7504 #define VSX_LOGICAL(name, tcg_op)                                    \
7505 static void glue(gen_, name)(DisasContext * ctx)                     \
7506     {                                                                \
7507         if (unlikely(!ctx->vsx_enabled)) {                           \
7508             gen_exception(ctx, POWERPC_EXCP_VSXU);                   \
7509             return;                                                  \
7510         }                                                            \
7511         tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
7512             cpu_vsrh(xB(ctx->opcode)));                              \
7513         tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
7514             cpu_vsrl(xB(ctx->opcode)));                              \
7515     }
7516
7517 VSX_LOGICAL(xxland, tcg_gen_and_i64)
7518 VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
7519 VSX_LOGICAL(xxlor, tcg_gen_or_i64)
7520 VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
7521 VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
7522 VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
7523 VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
7524 VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
7525
7526 #define VSX_XXMRG(name, high)                               \
7527 static void glue(gen_, name)(DisasContext * ctx)            \
7528     {                                                       \
7529         TCGv_i64 a0, a1, b0, b1;                            \
7530         if (unlikely(!ctx->vsx_enabled)) {                  \
7531             gen_exception(ctx, POWERPC_EXCP_VSXU);          \
7532             return;                                         \
7533         }                                                   \
7534         a0 = tcg_temp_new_i64();                            \
7535         a1 = tcg_temp_new_i64();                            \
7536         b0 = tcg_temp_new_i64();                            \
7537         b1 = tcg_temp_new_i64();                            \
7538         if (high) {                                         \
7539             tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
7540             tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
7541             tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
7542             tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
7543         } else {                                            \
7544             tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
7545             tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
7546             tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
7547             tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
7548         }                                                   \
7549         tcg_gen_shri_i64(a0, a0, 32);                       \
7550         tcg_gen_shri_i64(b0, b0, 32);                       \
7551         tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)),      \
7552                             b0, a0, 32, 32);                \
7553         tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)),      \
7554                             b1, a1, 32, 32);                \
7555         tcg_temp_free_i64(a0);                              \
7556         tcg_temp_free_i64(a1);                              \
7557         tcg_temp_free_i64(b0);                              \
7558         tcg_temp_free_i64(b1);                              \
7559     }
7560
7561 VSX_XXMRG(xxmrghw, 1)
7562 VSX_XXMRG(xxmrglw, 0)
7563
7564 static void gen_xxsel(DisasContext * ctx)
7565 {
7566     TCGv_i64 a, b, c;
7567     if (unlikely(!ctx->vsx_enabled)) {
7568         gen_exception(ctx, POWERPC_EXCP_VSXU);
7569         return;
7570     }
7571     a = tcg_temp_new_i64();
7572     b = tcg_temp_new_i64();
7573     c = tcg_temp_new_i64();
7574
7575     tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
7576     tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
7577     tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
7578
7579     tcg_gen_and_i64(b, b, c);
7580     tcg_gen_andc_i64(a, a, c);
7581     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
7582
7583     tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
7584     tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
7585     tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
7586
7587     tcg_gen_and_i64(b, b, c);
7588     tcg_gen_andc_i64(a, a, c);
7589     tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
7590
7591     tcg_temp_free_i64(a);
7592     tcg_temp_free_i64(b);
7593     tcg_temp_free_i64(c);
7594 }
7595
7596 static void gen_xxspltw(DisasContext *ctx)
7597 {
7598     TCGv_i64 b, b2;
7599     TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
7600                    cpu_vsrl(xB(ctx->opcode)) :
7601                    cpu_vsrh(xB(ctx->opcode));
7602
7603     if (unlikely(!ctx->vsx_enabled)) {
7604         gen_exception(ctx, POWERPC_EXCP_VSXU);
7605         return;
7606     }
7607
7608     b = tcg_temp_new_i64();
7609     b2 = tcg_temp_new_i64();
7610
7611     if (UIM(ctx->opcode) & 1) {
7612         tcg_gen_ext32u_i64(b, vsr);
7613     } else {
7614         tcg_gen_shri_i64(b, vsr, 32);
7615     }
7616
7617     tcg_gen_shli_i64(b2, b, 32);
7618     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
7619     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7620
7621     tcg_temp_free_i64(b);
7622     tcg_temp_free_i64(b2);
7623 }
7624
7625 static void gen_xxsldwi(DisasContext *ctx)
7626 {
7627     TCGv_i64 xth, xtl;
7628     if (unlikely(!ctx->vsx_enabled)) {
7629         gen_exception(ctx, POWERPC_EXCP_VSXU);
7630         return;
7631     }
7632     xth = tcg_temp_new_i64();
7633     xtl = tcg_temp_new_i64();
7634
7635     switch (SHW(ctx->opcode)) {
7636         case 0: {
7637             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7638             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7639             break;
7640         }
7641         case 1: {
7642             TCGv_i64 t0 = tcg_temp_new_i64();
7643             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
7644             tcg_gen_shli_i64(xth, xth, 32);
7645             tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
7646             tcg_gen_shri_i64(t0, t0, 32);
7647             tcg_gen_or_i64(xth, xth, t0);
7648             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
7649             tcg_gen_shli_i64(xtl, xtl, 32);
7650             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7651             tcg_gen_shri_i64(t0, t0, 32);
7652             tcg_gen_or_i64(xtl, xtl, t0);
7653             tcg_temp_free_i64(t0);
7654             break;
7655         }
7656         case 2: {
7657             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7658             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7659             break;
7660         }
7661         case 3: {
7662             TCGv_i64 t0 = tcg_temp_new_i64();
7663             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
7664             tcg_gen_shli_i64(xth, xth, 32);
7665             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
7666             tcg_gen_shri_i64(t0, t0, 32);
7667             tcg_gen_or_i64(xth, xth, t0);
7668             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
7669             tcg_gen_shli_i64(xtl, xtl, 32);
7670             tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
7671             tcg_gen_shri_i64(t0, t0, 32);
7672             tcg_gen_or_i64(xtl, xtl, t0);
7673             tcg_temp_free_i64(t0);
7674             break;
7675         }
7676     }
7677
7678     tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
7679     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
7680
7681     tcg_temp_free_i64(xth);
7682     tcg_temp_free_i64(xtl);
7683 }
7684
7685
7686 /***                           SPE extension                               ***/
7687 /* Register moves */
7688
7689 static inline void gen_evmra(DisasContext *ctx)
7690 {
7691
7692     if (unlikely(!ctx->spe_enabled)) {
7693         gen_exception(ctx, POWERPC_EXCP_SPEU);
7694         return;
7695     }
7696
7697 #if defined(TARGET_PPC64)
7698     /* rD := rA */
7699     tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7700
7701     /* spe_acc := rA */
7702     tcg_gen_st_i64(cpu_gpr[rA(ctx->opcode)],
7703                    cpu_env,
7704                    offsetof(CPUPPCState, spe_acc));
7705 #else
7706     TCGv_i64 tmp = tcg_temp_new_i64();
7707
7708     /* tmp := rA_lo + rA_hi << 32 */
7709     tcg_gen_concat_i32_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7710
7711     /* spe_acc := tmp */
7712     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
7713     tcg_temp_free_i64(tmp);
7714
7715     /* rD := rA */
7716     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
7717     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
7718 #endif
7719 }
7720
7721 static inline void gen_load_gpr64(TCGv_i64 t, int reg)
7722 {
7723 #if defined(TARGET_PPC64)
7724     tcg_gen_mov_i64(t, cpu_gpr[reg]);
7725 #else
7726     tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
7727 #endif
7728 }
7729
7730 static inline void gen_store_gpr64(int reg, TCGv_i64 t)
7731 {
7732 #if defined(TARGET_PPC64)
7733     tcg_gen_mov_i64(cpu_gpr[reg], t);
7734 #else
7735     TCGv_i64 tmp = tcg_temp_new_i64();
7736     tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
7737     tcg_gen_shri_i64(tmp, t, 32);
7738     tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
7739     tcg_temp_free_i64(tmp);
7740 #endif
7741 }
7742
7743 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type)         \
7744 static void glue(gen_, name0##_##name1)(DisasContext *ctx)                    \
7745 {                                                                             \
7746     if (Rc(ctx->opcode))                                                      \
7747         gen_##name1(ctx);                                                     \
7748     else                                                                      \
7749         gen_##name0(ctx);                                                     \
7750 }
7751
7752 /* Handler for undefined SPE opcodes */
7753 static inline void gen_speundef(DisasContext *ctx)
7754 {
7755     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
7756 }
7757
7758 /* SPE logic */
7759 #if defined(TARGET_PPC64)
7760 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
7761 static inline void gen_##name(DisasContext *ctx)                              \
7762 {                                                                             \
7763     if (unlikely(!ctx->spe_enabled)) {                                        \
7764         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7765         return;                                                               \
7766     }                                                                         \
7767     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
7768            cpu_gpr[rB(ctx->opcode)]);                                         \
7769 }
7770 #else
7771 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
7772 static inline void gen_##name(DisasContext *ctx)                              \
7773 {                                                                             \
7774     if (unlikely(!ctx->spe_enabled)) {                                        \
7775         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7776         return;                                                               \
7777     }                                                                         \
7778     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
7779            cpu_gpr[rB(ctx->opcode)]);                                         \
7780     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
7781            cpu_gprh[rB(ctx->opcode)]);                                        \
7782 }
7783 #endif
7784
7785 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
7786 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
7787 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
7788 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
7789 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
7790 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
7791 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
7792 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
7793
7794 /* SPE logic immediate */
7795 #if defined(TARGET_PPC64)
7796 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
7797 static inline void gen_##name(DisasContext *ctx)                              \
7798 {                                                                             \
7799     if (unlikely(!ctx->spe_enabled)) {                                        \
7800         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7801         return;                                                               \
7802     }                                                                         \
7803     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
7804     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
7805     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
7806     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
7807     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
7808     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
7809     tcg_gen_trunc_i64_i32(t1, t2);                                            \
7810     tcg_temp_free_i64(t2);                                                    \
7811     tcg_opi(t1, t1, rB(ctx->opcode));                                         \
7812     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
7813     tcg_temp_free_i32(t0);                                                    \
7814     tcg_temp_free_i32(t1);                                                    \
7815 }
7816 #else
7817 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
7818 static inline void gen_##name(DisasContext *ctx)                              \
7819 {                                                                             \
7820     if (unlikely(!ctx->spe_enabled)) {                                        \
7821         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7822         return;                                                               \
7823     }                                                                         \
7824     tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
7825             rB(ctx->opcode));                                                 \
7826     tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
7827             rB(ctx->opcode));                                                 \
7828 }
7829 #endif
7830 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
7831 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
7832 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
7833 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
7834
7835 /* SPE arithmetic */
7836 #if defined(TARGET_PPC64)
7837 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
7838 static inline void gen_##name(DisasContext *ctx)                              \
7839 {                                                                             \
7840     if (unlikely(!ctx->spe_enabled)) {                                        \
7841         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7842         return;                                                               \
7843     }                                                                         \
7844     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
7845     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
7846     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
7847     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
7848     tcg_op(t0, t0);                                                           \
7849     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
7850     tcg_gen_trunc_i64_i32(t1, t2);                                            \
7851     tcg_temp_free_i64(t2);                                                    \
7852     tcg_op(t1, t1);                                                           \
7853     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
7854     tcg_temp_free_i32(t0);                                                    \
7855     tcg_temp_free_i32(t1);                                                    \
7856 }
7857 #else
7858 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
7859 static inline void gen_##name(DisasContext *ctx)                              \
7860 {                                                                             \
7861     if (unlikely(!ctx->spe_enabled)) {                                        \
7862         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7863         return;                                                               \
7864     }                                                                         \
7865     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
7866     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
7867 }
7868 #endif
7869
7870 static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
7871 {
7872     int l1 = gen_new_label();
7873     int l2 = gen_new_label();
7874
7875     tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
7876     tcg_gen_neg_i32(ret, arg1);
7877     tcg_gen_br(l2);
7878     gen_set_label(l1);
7879     tcg_gen_mov_i32(ret, arg1);
7880     gen_set_label(l2);
7881 }
7882 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
7883 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
7884 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
7885 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
7886 static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
7887 {
7888     tcg_gen_addi_i32(ret, arg1, 0x8000);
7889     tcg_gen_ext16u_i32(ret, ret);
7890 }
7891 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
7892 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
7893 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
7894
7895 #if defined(TARGET_PPC64)
7896 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
7897 static inline void gen_##name(DisasContext *ctx)                              \
7898 {                                                                             \
7899     if (unlikely(!ctx->spe_enabled)) {                                        \
7900         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7901         return;                                                               \
7902     }                                                                         \
7903     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
7904     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
7905     TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
7906     TCGv_i64 t3 = tcg_temp_local_new_i64();                                   \
7907     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
7908     tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
7909     tcg_op(t0, t0, t2);                                                       \
7910     tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
7911     tcg_gen_trunc_i64_i32(t1, t3);                                            \
7912     tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
7913     tcg_gen_trunc_i64_i32(t2, t3);                                            \
7914     tcg_temp_free_i64(t3);                                                    \
7915     tcg_op(t1, t1, t2);                                                       \
7916     tcg_temp_free_i32(t2);                                                    \
7917     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
7918     tcg_temp_free_i32(t0);                                                    \
7919     tcg_temp_free_i32(t1);                                                    \
7920 }
7921 #else
7922 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
7923 static inline void gen_##name(DisasContext *ctx)                              \
7924 {                                                                             \
7925     if (unlikely(!ctx->spe_enabled)) {                                        \
7926         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
7927         return;                                                               \
7928     }                                                                         \
7929     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
7930            cpu_gpr[rB(ctx->opcode)]);                                         \
7931     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
7932            cpu_gprh[rB(ctx->opcode)]);                                        \
7933 }
7934 #endif
7935
7936 static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
7937 {
7938     TCGv_i32 t0;
7939     int l1, l2;
7940
7941     l1 = gen_new_label();
7942     l2 = gen_new_label();
7943     t0 = tcg_temp_local_new_i32();
7944     /* No error here: 6 bits are used */
7945     tcg_gen_andi_i32(t0, arg2, 0x3F);
7946     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7947     tcg_gen_shr_i32(ret, arg1, t0);
7948     tcg_gen_br(l2);
7949     gen_set_label(l1);
7950     tcg_gen_movi_i32(ret, 0);
7951     gen_set_label(l2);
7952     tcg_temp_free_i32(t0);
7953 }
7954 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
7955 static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
7956 {
7957     TCGv_i32 t0;
7958     int l1, l2;
7959
7960     l1 = gen_new_label();
7961     l2 = gen_new_label();
7962     t0 = tcg_temp_local_new_i32();
7963     /* No error here: 6 bits are used */
7964     tcg_gen_andi_i32(t0, arg2, 0x3F);
7965     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7966     tcg_gen_sar_i32(ret, arg1, t0);
7967     tcg_gen_br(l2);
7968     gen_set_label(l1);
7969     tcg_gen_movi_i32(ret, 0);
7970     gen_set_label(l2);
7971     tcg_temp_free_i32(t0);
7972 }
7973 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
7974 static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
7975 {
7976     TCGv_i32 t0;
7977     int l1, l2;
7978
7979     l1 = gen_new_label();
7980     l2 = gen_new_label();
7981     t0 = tcg_temp_local_new_i32();
7982     /* No error here: 6 bits are used */
7983     tcg_gen_andi_i32(t0, arg2, 0x3F);
7984     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
7985     tcg_gen_shl_i32(ret, arg1, t0);
7986     tcg_gen_br(l2);
7987     gen_set_label(l1);
7988     tcg_gen_movi_i32(ret, 0);
7989     gen_set_label(l2);
7990     tcg_temp_free_i32(t0);
7991 }
7992 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
7993 static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
7994 {
7995     TCGv_i32 t0 = tcg_temp_new_i32();
7996     tcg_gen_andi_i32(t0, arg2, 0x1F);
7997     tcg_gen_rotl_i32(ret, arg1, t0);
7998     tcg_temp_free_i32(t0);
7999 }
8000 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
8001 static inline void gen_evmergehi(DisasContext *ctx)
8002 {
8003     if (unlikely(!ctx->spe_enabled)) {
8004         gen_exception(ctx, POWERPC_EXCP_SPEU);
8005         return;
8006     }
8007 #if defined(TARGET_PPC64)
8008     TCGv t0 = tcg_temp_new();
8009     TCGv t1 = tcg_temp_new();
8010     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8011     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8012     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8013     tcg_temp_free(t0);
8014     tcg_temp_free(t1);
8015 #else
8016     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8017     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8018 #endif
8019 }
8020 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
8021 static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8022 {
8023     tcg_gen_sub_i32(ret, arg2, arg1);
8024 }
8025 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
8026
8027 /* SPE arithmetic immediate */
8028 #if defined(TARGET_PPC64)
8029 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8030 static inline void gen_##name(DisasContext *ctx)                              \
8031 {                                                                             \
8032     if (unlikely(!ctx->spe_enabled)) {                                        \
8033         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8034         return;                                                               \
8035     }                                                                         \
8036     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8037     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8038     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
8039     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
8040     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8041     tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
8042     tcg_gen_trunc_i64_i32(t1, t2);                                            \
8043     tcg_temp_free_i64(t2);                                                    \
8044     tcg_op(t1, t1, rA(ctx->opcode));                                          \
8045     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
8046     tcg_temp_free_i32(t0);                                                    \
8047     tcg_temp_free_i32(t1);                                                    \
8048 }
8049 #else
8050 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8051 static inline void gen_##name(DisasContext *ctx)                              \
8052 {                                                                             \
8053     if (unlikely(!ctx->spe_enabled)) {                                        \
8054         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8055         return;                                                               \
8056     }                                                                         \
8057     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
8058            rA(ctx->opcode));                                                  \
8059     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
8060            rA(ctx->opcode));                                                  \
8061 }
8062 #endif
8063 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8064 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8065
8066 /* SPE comparison */
8067 #if defined(TARGET_PPC64)
8068 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8069 static inline void gen_##name(DisasContext *ctx)                              \
8070 {                                                                             \
8071     if (unlikely(!ctx->spe_enabled)) {                                        \
8072         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8073         return;                                                               \
8074     }                                                                         \
8075     int l1 = gen_new_label();                                                 \
8076     int l2 = gen_new_label();                                                 \
8077     int l3 = gen_new_label();                                                 \
8078     int l4 = gen_new_label();                                                 \
8079     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
8080     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
8081     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
8082     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
8083     tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
8084     tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
8085     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
8086     tcg_gen_br(l2);                                                           \
8087     gen_set_label(l1);                                                        \
8088     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8089                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8090     gen_set_label(l2);                                                        \
8091     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
8092     tcg_gen_trunc_i64_i32(t0, t2);                                            \
8093     tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
8094     tcg_gen_trunc_i64_i32(t1, t2);                                            \
8095     tcg_temp_free_i64(t2);                                                    \
8096     tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
8097     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8098                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8099     tcg_gen_br(l4);                                                           \
8100     gen_set_label(l3);                                                        \
8101     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8102                     CRF_CH | CRF_CH_OR_CL);                                   \
8103     gen_set_label(l4);                                                        \
8104     tcg_temp_free_i32(t0);                                                    \
8105     tcg_temp_free_i32(t1);                                                    \
8106 }
8107 #else
8108 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8109 static inline void gen_##name(DisasContext *ctx)                              \
8110 {                                                                             \
8111     if (unlikely(!ctx->spe_enabled)) {                                        \
8112         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8113         return;                                                               \
8114     }                                                                         \
8115     int l1 = gen_new_label();                                                 \
8116     int l2 = gen_new_label();                                                 \
8117     int l3 = gen_new_label();                                                 \
8118     int l4 = gen_new_label();                                                 \
8119                                                                               \
8120     tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
8121                        cpu_gpr[rB(ctx->opcode)], l1);                         \
8122     tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
8123     tcg_gen_br(l2);                                                           \
8124     gen_set_label(l1);                                                        \
8125     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8126                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8127     gen_set_label(l2);                                                        \
8128     tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
8129                        cpu_gprh[rB(ctx->opcode)], l3);                        \
8130     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8131                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8132     tcg_gen_br(l4);                                                           \
8133     gen_set_label(l3);                                                        \
8134     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8135                     CRF_CH | CRF_CH_OR_CL);                                   \
8136     gen_set_label(l4);                                                        \
8137 }
8138 #endif
8139 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8140 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8141 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8142 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8143 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8144
8145 /* SPE misc */
8146 static inline void gen_brinc(DisasContext *ctx)
8147 {
8148     /* Note: brinc is usable even if SPE is disabled */
8149     gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8150                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8151 }
8152 static inline void gen_evmergelo(DisasContext *ctx)
8153 {
8154     if (unlikely(!ctx->spe_enabled)) {
8155         gen_exception(ctx, POWERPC_EXCP_SPEU);
8156         return;
8157     }
8158 #if defined(TARGET_PPC64)
8159     TCGv t0 = tcg_temp_new();
8160     TCGv t1 = tcg_temp_new();
8161     tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
8162     tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8163     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8164     tcg_temp_free(t0);
8165     tcg_temp_free(t1);
8166 #else
8167     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8168     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8169 #endif
8170 }
8171 static inline void gen_evmergehilo(DisasContext *ctx)
8172 {
8173     if (unlikely(!ctx->spe_enabled)) {
8174         gen_exception(ctx, POWERPC_EXCP_SPEU);
8175         return;
8176     }
8177 #if defined(TARGET_PPC64)
8178     TCGv t0 = tcg_temp_new();
8179     TCGv t1 = tcg_temp_new();
8180     tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
8181     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
8182     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8183     tcg_temp_free(t0);
8184     tcg_temp_free(t1);
8185 #else
8186     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8187     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8188 #endif
8189 }
8190 static inline void gen_evmergelohi(DisasContext *ctx)
8191 {
8192     if (unlikely(!ctx->spe_enabled)) {
8193         gen_exception(ctx, POWERPC_EXCP_SPEU);
8194         return;
8195     }
8196 #if defined(TARGET_PPC64)
8197     TCGv t0 = tcg_temp_new();
8198     TCGv t1 = tcg_temp_new();
8199     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
8200     tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
8201     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
8202     tcg_temp_free(t0);
8203     tcg_temp_free(t1);
8204 #else
8205     if (rD(ctx->opcode) == rA(ctx->opcode)) {
8206         TCGv_i32 tmp = tcg_temp_new_i32();
8207         tcg_gen_mov_i32(tmp, cpu_gpr[rA(ctx->opcode)]);
8208         tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8209         tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], tmp);
8210         tcg_temp_free_i32(tmp);
8211     } else {
8212         tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8213         tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8214     }
8215 #endif
8216 }
8217 static inline void gen_evsplati(DisasContext *ctx)
8218 {
8219     uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
8220
8221 #if defined(TARGET_PPC64)
8222     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
8223 #else
8224     tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8225     tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8226 #endif
8227 }
8228 static inline void gen_evsplatfi(DisasContext *ctx)
8229 {
8230     uint64_t imm = rA(ctx->opcode) << 27;
8231
8232 #if defined(TARGET_PPC64)
8233     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
8234 #else
8235     tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
8236     tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
8237 #endif
8238 }
8239
8240 static inline void gen_evsel(DisasContext *ctx)
8241 {
8242     int l1 = gen_new_label();
8243     int l2 = gen_new_label();
8244     int l3 = gen_new_label();
8245     int l4 = gen_new_label();
8246     TCGv_i32 t0 = tcg_temp_local_new_i32();
8247 #if defined(TARGET_PPC64)
8248     TCGv t1 = tcg_temp_local_new();
8249     TCGv t2 = tcg_temp_local_new();
8250 #endif
8251     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8252     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8253 #if defined(TARGET_PPC64)
8254     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8255 #else
8256     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8257 #endif
8258     tcg_gen_br(l2);
8259     gen_set_label(l1);
8260 #if defined(TARGET_PPC64)
8261     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
8262 #else
8263     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8264 #endif
8265     gen_set_label(l2);
8266     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8267     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8268 #if defined(TARGET_PPC64)
8269     tcg_gen_ext32u_tl(t2, cpu_gpr[rA(ctx->opcode)]);
8270 #else
8271     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8272 #endif
8273     tcg_gen_br(l4);
8274     gen_set_label(l3);
8275 #if defined(TARGET_PPC64)
8276     tcg_gen_ext32u_tl(t2, cpu_gpr[rB(ctx->opcode)]);
8277 #else
8278     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8279 #endif
8280     gen_set_label(l4);
8281     tcg_temp_free_i32(t0);
8282 #if defined(TARGET_PPC64)
8283     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
8284     tcg_temp_free(t1);
8285     tcg_temp_free(t2);
8286 #endif
8287 }
8288
8289 static void gen_evsel0(DisasContext *ctx)
8290 {
8291     gen_evsel(ctx);
8292 }
8293
8294 static void gen_evsel1(DisasContext *ctx)
8295 {
8296     gen_evsel(ctx);
8297 }
8298
8299 static void gen_evsel2(DisasContext *ctx)
8300 {
8301     gen_evsel(ctx);
8302 }
8303
8304 static void gen_evsel3(DisasContext *ctx)
8305 {
8306     gen_evsel(ctx);
8307 }
8308
8309 /* Multiply */
8310
8311 static inline void gen_evmwumi(DisasContext *ctx)
8312 {
8313     TCGv_i64 t0, t1;
8314
8315     if (unlikely(!ctx->spe_enabled)) {
8316         gen_exception(ctx, POWERPC_EXCP_SPEU);
8317         return;
8318     }
8319
8320     t0 = tcg_temp_new_i64();
8321     t1 = tcg_temp_new_i64();
8322
8323     /* t0 := rA; t1 := rB */
8324 #if defined(TARGET_PPC64)
8325     tcg_gen_ext32u_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8326     tcg_gen_ext32u_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8327 #else
8328     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8329     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8330 #endif
8331
8332     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
8333
8334     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8335
8336     tcg_temp_free_i64(t0);
8337     tcg_temp_free_i64(t1);
8338 }
8339
8340 static inline void gen_evmwumia(DisasContext *ctx)
8341 {
8342     TCGv_i64 tmp;
8343
8344     if (unlikely(!ctx->spe_enabled)) {
8345         gen_exception(ctx, POWERPC_EXCP_SPEU);
8346         return;
8347     }
8348
8349     gen_evmwumi(ctx);            /* rD := rA * rB */
8350
8351     tmp = tcg_temp_new_i64();
8352
8353     /* acc := rD */
8354     gen_load_gpr64(tmp, rD(ctx->opcode));
8355     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8356     tcg_temp_free_i64(tmp);
8357 }
8358
8359 static inline void gen_evmwumiaa(DisasContext *ctx)
8360 {
8361     TCGv_i64 acc;
8362     TCGv_i64 tmp;
8363
8364     if (unlikely(!ctx->spe_enabled)) {
8365         gen_exception(ctx, POWERPC_EXCP_SPEU);
8366         return;
8367     }
8368
8369     gen_evmwumi(ctx);           /* rD := rA * rB */
8370
8371     acc = tcg_temp_new_i64();
8372     tmp = tcg_temp_new_i64();
8373
8374     /* tmp := rD */
8375     gen_load_gpr64(tmp, rD(ctx->opcode));
8376
8377     /* Load acc */
8378     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8379
8380     /* acc := tmp + acc */
8381     tcg_gen_add_i64(acc, acc, tmp);
8382
8383     /* Store acc */
8384     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8385
8386     /* rD := acc */
8387     gen_store_gpr64(rD(ctx->opcode), acc);
8388
8389     tcg_temp_free_i64(acc);
8390     tcg_temp_free_i64(tmp);
8391 }
8392
8393 static inline void gen_evmwsmi(DisasContext *ctx)
8394 {
8395     TCGv_i64 t0, t1;
8396
8397     if (unlikely(!ctx->spe_enabled)) {
8398         gen_exception(ctx, POWERPC_EXCP_SPEU);
8399         return;
8400     }
8401
8402     t0 = tcg_temp_new_i64();
8403     t1 = tcg_temp_new_i64();
8404
8405     /* t0 := rA; t1 := rB */
8406 #if defined(TARGET_PPC64)
8407     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
8408     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
8409 #else
8410     tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8411     tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8412 #endif
8413
8414     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
8415
8416     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8417
8418     tcg_temp_free_i64(t0);
8419     tcg_temp_free_i64(t1);
8420 }
8421
8422 static inline void gen_evmwsmia(DisasContext *ctx)
8423 {
8424     TCGv_i64 tmp;
8425
8426     gen_evmwsmi(ctx);            /* rD := rA * rB */
8427
8428     tmp = tcg_temp_new_i64();
8429
8430     /* acc := rD */
8431     gen_load_gpr64(tmp, rD(ctx->opcode));
8432     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8433
8434     tcg_temp_free_i64(tmp);
8435 }
8436
8437 static inline void gen_evmwsmiaa(DisasContext *ctx)
8438 {
8439     TCGv_i64 acc = tcg_temp_new_i64();
8440     TCGv_i64 tmp = tcg_temp_new_i64();
8441
8442     gen_evmwsmi(ctx);           /* rD := rA * rB */
8443
8444     acc = tcg_temp_new_i64();
8445     tmp = tcg_temp_new_i64();
8446
8447     /* tmp := rD */
8448     gen_load_gpr64(tmp, rD(ctx->opcode));
8449
8450     /* Load acc */
8451     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8452
8453     /* acc := tmp + acc */
8454     tcg_gen_add_i64(acc, acc, tmp);
8455
8456     /* Store acc */
8457     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8458
8459     /* rD := acc */
8460     gen_store_gpr64(rD(ctx->opcode), acc);
8461
8462     tcg_temp_free_i64(acc);
8463     tcg_temp_free_i64(tmp);
8464 }
8465
8466 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8467 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8468 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8469 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8470 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8471 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8472 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
8473 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
8474 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
8475 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8476 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8477 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8478 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8479 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8480 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8481 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8482 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
8483 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8484 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8485 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
8486 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
8487 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8488 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
8489 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
8490 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8491 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
8492 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8493 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
8494 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
8495
8496 /* SPE load and stores */
8497 static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
8498 {
8499     target_ulong uimm = rB(ctx->opcode);
8500
8501     if (rA(ctx->opcode) == 0) {
8502         tcg_gen_movi_tl(EA, uimm << sh);
8503     } else {
8504         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
8505         if (NARROW_MODE(ctx)) {
8506             tcg_gen_ext32u_tl(EA, EA);
8507         }
8508     }
8509 }
8510
8511 static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
8512 {
8513 #if defined(TARGET_PPC64)
8514     gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8515 #else
8516     TCGv_i64 t0 = tcg_temp_new_i64();
8517     gen_qemu_ld64(ctx, t0, addr);
8518     tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
8519     tcg_gen_shri_i64(t0, t0, 32);
8520     tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
8521     tcg_temp_free_i64(t0);
8522 #endif
8523 }
8524
8525 static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
8526 {
8527 #if defined(TARGET_PPC64)
8528     TCGv t0 = tcg_temp_new();
8529     gen_qemu_ld32u(ctx, t0, addr);
8530     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8531     gen_addr_add(ctx, addr, addr, 4);
8532     gen_qemu_ld32u(ctx, t0, addr);
8533     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8534     tcg_temp_free(t0);
8535 #else
8536     gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8537     gen_addr_add(ctx, addr, addr, 4);
8538     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8539 #endif
8540 }
8541
8542 static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
8543 {
8544     TCGv t0 = tcg_temp_new();
8545 #if defined(TARGET_PPC64)
8546     gen_qemu_ld16u(ctx, t0, addr);
8547     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8548     gen_addr_add(ctx, addr, addr, 2);
8549     gen_qemu_ld16u(ctx, t0, addr);
8550     tcg_gen_shli_tl(t0, t0, 32);
8551     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8552     gen_addr_add(ctx, addr, addr, 2);
8553     gen_qemu_ld16u(ctx, t0, addr);
8554     tcg_gen_shli_tl(t0, t0, 16);
8555     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8556     gen_addr_add(ctx, addr, addr, 2);
8557     gen_qemu_ld16u(ctx, t0, addr);
8558     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8559 #else
8560     gen_qemu_ld16u(ctx, t0, addr);
8561     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8562     gen_addr_add(ctx, addr, addr, 2);
8563     gen_qemu_ld16u(ctx, t0, addr);
8564     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
8565     gen_addr_add(ctx, addr, addr, 2);
8566     gen_qemu_ld16u(ctx, t0, addr);
8567     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8568     gen_addr_add(ctx, addr, addr, 2);
8569     gen_qemu_ld16u(ctx, t0, addr);
8570     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8571 #endif
8572     tcg_temp_free(t0);
8573 }
8574
8575 static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
8576 {
8577     TCGv t0 = tcg_temp_new();
8578     gen_qemu_ld16u(ctx, t0, addr);
8579 #if defined(TARGET_PPC64)
8580     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8581     tcg_gen_shli_tl(t0, t0, 16);
8582     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8583 #else
8584     tcg_gen_shli_tl(t0, t0, 16);
8585     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8586     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8587 #endif
8588     tcg_temp_free(t0);
8589 }
8590
8591 static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
8592 {
8593     TCGv t0 = tcg_temp_new();
8594     gen_qemu_ld16u(ctx, t0, addr);
8595 #if defined(TARGET_PPC64)
8596     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8597     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8598 #else
8599     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8600     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8601 #endif
8602     tcg_temp_free(t0);
8603 }
8604
8605 static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
8606 {
8607     TCGv t0 = tcg_temp_new();
8608     gen_qemu_ld16s(ctx, t0, addr);
8609 #if defined(TARGET_PPC64)
8610     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8611     tcg_gen_ext32u_tl(t0, t0);
8612     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8613 #else
8614     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8615     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8616 #endif
8617     tcg_temp_free(t0);
8618 }
8619
8620 static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
8621 {
8622     TCGv t0 = tcg_temp_new();
8623 #if defined(TARGET_PPC64)
8624     gen_qemu_ld16u(ctx, t0, addr);
8625     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8626     gen_addr_add(ctx, addr, addr, 2);
8627     gen_qemu_ld16u(ctx, t0, addr);
8628     tcg_gen_shli_tl(t0, t0, 16);
8629     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8630 #else
8631     gen_qemu_ld16u(ctx, t0, addr);
8632     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8633     gen_addr_add(ctx, addr, addr, 2);
8634     gen_qemu_ld16u(ctx, t0, addr);
8635     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8636 #endif
8637     tcg_temp_free(t0);
8638 }
8639
8640 static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
8641 {
8642 #if defined(TARGET_PPC64)
8643     TCGv t0 = tcg_temp_new();
8644     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8645     gen_addr_add(ctx, addr, addr, 2);
8646     gen_qemu_ld16u(ctx, t0, addr);
8647     tcg_gen_shli_tl(t0, t0, 32);
8648     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8649     tcg_temp_free(t0);
8650 #else
8651     gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8652     gen_addr_add(ctx, addr, addr, 2);
8653     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8654 #endif
8655 }
8656
8657 static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
8658 {
8659 #if defined(TARGET_PPC64)
8660     TCGv t0 = tcg_temp_new();
8661     gen_qemu_ld16s(ctx, t0, addr);
8662     tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
8663     gen_addr_add(ctx, addr, addr, 2);
8664     gen_qemu_ld16s(ctx, t0, addr);
8665     tcg_gen_shli_tl(t0, t0, 32);
8666     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8667     tcg_temp_free(t0);
8668 #else
8669     gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
8670     gen_addr_add(ctx, addr, addr, 2);
8671     gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
8672 #endif
8673 }
8674
8675 static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
8676 {
8677     TCGv t0 = tcg_temp_new();
8678     gen_qemu_ld32u(ctx, t0, addr);
8679 #if defined(TARGET_PPC64)
8680     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
8681     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8682 #else
8683     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
8684     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
8685 #endif
8686     tcg_temp_free(t0);
8687 }
8688
8689 static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
8690 {
8691     TCGv t0 = tcg_temp_new();
8692 #if defined(TARGET_PPC64)
8693     gen_qemu_ld16u(ctx, t0, addr);
8694     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
8695     tcg_gen_shli_tl(t0, t0, 32);
8696     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8697     gen_addr_add(ctx, addr, addr, 2);
8698     gen_qemu_ld16u(ctx, t0, addr);
8699     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8700     tcg_gen_shli_tl(t0, t0, 16);
8701     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
8702 #else
8703     gen_qemu_ld16u(ctx, t0, addr);
8704     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
8705     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
8706     gen_addr_add(ctx, addr, addr, 2);
8707     gen_qemu_ld16u(ctx, t0, addr);
8708     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
8709     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
8710 #endif
8711     tcg_temp_free(t0);
8712 }
8713
8714 static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
8715 {
8716 #if defined(TARGET_PPC64)
8717     gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
8718 #else
8719     TCGv_i64 t0 = tcg_temp_new_i64();
8720     tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
8721     gen_qemu_st64(ctx, t0, addr);
8722     tcg_temp_free_i64(t0);
8723 #endif
8724 }
8725
8726 static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
8727 {
8728 #if defined(TARGET_PPC64)
8729     TCGv t0 = tcg_temp_new();
8730     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
8731     gen_qemu_st32(ctx, t0, addr);
8732     tcg_temp_free(t0);
8733 #else
8734     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
8735 #endif
8736     gen_addr_add(ctx, addr, addr, 4);
8737     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
8738 }
8739
8740 static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
8741 {
8742     TCGv t0 = tcg_temp_new();
8743 #if defined(TARGET_PPC64)
8744     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8745 #else
8746     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8747 #endif
8748     gen_qemu_st16(ctx, t0, addr);
8749     gen_addr_add(ctx, addr, addr, 2);
8750 #if defined(TARGET_PPC64)
8751     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
8752     gen_qemu_st16(ctx, t0, addr);
8753 #else
8754     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
8755 #endif
8756     gen_addr_add(ctx, addr, addr, 2);
8757     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
8758     gen_qemu_st16(ctx, t0, addr);
8759     tcg_temp_free(t0);
8760     gen_addr_add(ctx, addr, addr, 2);
8761     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
8762 }
8763
8764 static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
8765 {
8766     TCGv t0 = tcg_temp_new();
8767 #if defined(TARGET_PPC64)
8768     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
8769 #else
8770     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
8771 #endif
8772     gen_qemu_st16(ctx, t0, addr);
8773     gen_addr_add(ctx, addr, addr, 2);
8774     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
8775     gen_qemu_st16(ctx, t0, addr);
8776     tcg_temp_free(t0);
8777 }
8778
8779 static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
8780 {
8781 #if defined(TARGET_PPC64)
8782     TCGv t0 = tcg_temp_new();
8783     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
8784     gen_qemu_st16(ctx, t0, addr);
8785     tcg_temp_free(t0);
8786 #else
8787     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
8788 #endif
8789     gen_addr_add(ctx, addr, addr, 2);
8790     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
8791 }
8792
8793 static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
8794 {
8795 #if defined(TARGET_PPC64)
8796     TCGv t0 = tcg_temp_new();
8797     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
8798     gen_qemu_st32(ctx, t0, addr);
8799     tcg_temp_free(t0);
8800 #else
8801     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
8802 #endif
8803 }
8804
8805 static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
8806 {
8807     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
8808 }
8809
8810 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
8811 static void glue(gen_, name)(DisasContext *ctx)                                       \
8812 {                                                                             \
8813     TCGv t0;                                                                  \
8814     if (unlikely(!ctx->spe_enabled)) {                                        \
8815         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8816         return;                                                               \
8817     }                                                                         \
8818     gen_set_access_type(ctx, ACCESS_INT);                                     \
8819     t0 = tcg_temp_new();                                                      \
8820     if (Rc(ctx->opcode)) {                                                    \
8821         gen_addr_spe_imm_index(ctx, t0, sh);                                  \
8822     } else {                                                                  \
8823         gen_addr_reg_index(ctx, t0);                                          \
8824     }                                                                         \
8825     gen_op_##name(ctx, t0);                                                   \
8826     tcg_temp_free(t0);                                                        \
8827 }
8828
8829 GEN_SPEOP_LDST(evldd, 0x00, 3);
8830 GEN_SPEOP_LDST(evldw, 0x01, 3);
8831 GEN_SPEOP_LDST(evldh, 0x02, 3);
8832 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
8833 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
8834 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
8835 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
8836 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
8837 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
8838 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
8839 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
8840
8841 GEN_SPEOP_LDST(evstdd, 0x10, 3);
8842 GEN_SPEOP_LDST(evstdw, 0x11, 3);
8843 GEN_SPEOP_LDST(evstdh, 0x12, 3);
8844 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
8845 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
8846 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
8847 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
8848
8849 /* Multiply and add - TODO */
8850 #if 0
8851 GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
8852 GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8853 GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8854 GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8855 GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8856 GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8857 GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8858 GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8859 GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8860 GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8861 GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
8862 GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8863
8864 GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8865 GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8866 GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8867 GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8868 GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8869 GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8870 GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8871 GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
8872 GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
8873 GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8874 GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8875 GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8876
8877 GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8878 GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8879 GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8880 GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
8881 GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
8882
8883 GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8884 GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8885 GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8886 GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8887 GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8888 GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8889 GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8890 GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8891 GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8892 GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8893 GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
8894 GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8895
8896 GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8897 GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
8898 GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8899 GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8900
8901 GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8902 GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8903 GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8904 GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8905 GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8906 GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8907 GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8908 GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8909 GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8910 GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8911 GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
8912 GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8913
8914 GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8915 GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8916 GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8917 GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
8918 GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
8919 #endif
8920
8921 /***                      SPE floating-point extension                     ***/
8922 #if defined(TARGET_PPC64)
8923 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
8924 static inline void gen_##name(DisasContext *ctx)                              \
8925 {                                                                             \
8926     TCGv_i32 t0;                                                              \
8927     TCGv t1;                                                                  \
8928     t0 = tcg_temp_new_i32();                                                  \
8929     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
8930     gen_helper_##name(t0, cpu_env, t0);                                       \
8931     t1 = tcg_temp_new();                                                      \
8932     tcg_gen_extu_i32_tl(t1, t0);                                              \
8933     tcg_temp_free_i32(t0);                                                    \
8934     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
8935                     0xFFFFFFFF00000000ULL);                                   \
8936     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
8937     tcg_temp_free(t1);                                                        \
8938 }
8939 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
8940 static inline void gen_##name(DisasContext *ctx)                              \
8941 {                                                                             \
8942     TCGv_i32 t0;                                                              \
8943     TCGv t1;                                                                  \
8944     t0 = tcg_temp_new_i32();                                                  \
8945     gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]);                 \
8946     t1 = tcg_temp_new();                                                      \
8947     tcg_gen_extu_i32_tl(t1, t0);                                              \
8948     tcg_temp_free_i32(t0);                                                    \
8949     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
8950                     0xFFFFFFFF00000000ULL);                                   \
8951     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
8952     tcg_temp_free(t1);                                                        \
8953 }
8954 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
8955 static inline void gen_##name(DisasContext *ctx)                              \
8956 {                                                                             \
8957     TCGv_i32 t0 = tcg_temp_new_i32();                                         \
8958     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
8959     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);                 \
8960     tcg_temp_free_i32(t0);                                                    \
8961 }
8962 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
8963 static inline void gen_##name(DisasContext *ctx)                              \
8964 {                                                                             \
8965     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
8966                       cpu_gpr[rB(ctx->opcode)]);                              \
8967 }
8968 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
8969 static inline void gen_##name(DisasContext *ctx)                              \
8970 {                                                                             \
8971     TCGv_i32 t0, t1;                                                          \
8972     TCGv_i64 t2;                                                              \
8973     if (unlikely(!ctx->spe_enabled)) {                                        \
8974         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8975         return;                                                               \
8976     }                                                                         \
8977     t0 = tcg_temp_new_i32();                                                  \
8978     t1 = tcg_temp_new_i32();                                                  \
8979     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8980     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
8981     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
8982     tcg_temp_free_i32(t1);                                                    \
8983     t2 = tcg_temp_new();                                                      \
8984     tcg_gen_extu_i32_tl(t2, t0);                                              \
8985     tcg_temp_free_i32(t0);                                                    \
8986     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
8987                     0xFFFFFFFF00000000ULL);                                   \
8988     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
8989     tcg_temp_free(t2);                                                        \
8990 }
8991 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
8992 static inline void gen_##name(DisasContext *ctx)                              \
8993 {                                                                             \
8994     if (unlikely(!ctx->spe_enabled)) {                                        \
8995         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8996         return;                                                               \
8997     }                                                                         \
8998     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
8999                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9000 }
9001 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9002 static inline void gen_##name(DisasContext *ctx)                              \
9003 {                                                                             \
9004     TCGv_i32 t0, t1;                                                          \
9005     if (unlikely(!ctx->spe_enabled)) {                                        \
9006         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9007         return;                                                               \
9008     }                                                                         \
9009     t0 = tcg_temp_new_i32();                                                  \
9010     t1 = tcg_temp_new_i32();                                                  \
9011     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9012     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9013     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9014     tcg_temp_free_i32(t0);                                                    \
9015     tcg_temp_free_i32(t1);                                                    \
9016 }
9017 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9018 static inline void gen_##name(DisasContext *ctx)                              \
9019 {                                                                             \
9020     if (unlikely(!ctx->spe_enabled)) {                                        \
9021         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9022         return;                                                               \
9023     }                                                                         \
9024     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env,                    \
9025                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9026 }
9027 #else
9028 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
9029 static inline void gen_##name(DisasContext *ctx)                              \
9030 {                                                                             \
9031     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
9032                       cpu_gpr[rB(ctx->opcode)]);                              \
9033 }
9034 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
9035 static inline void gen_##name(DisasContext *ctx)                              \
9036 {                                                                             \
9037     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9038     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9039     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);                 \
9040     tcg_temp_free_i64(t0);                                                    \
9041 }
9042 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
9043 static inline void gen_##name(DisasContext *ctx)                              \
9044 {                                                                             \
9045     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9046     gen_helper_##name(t0, cpu_env, cpu_gpr[rB(ctx->opcode)]);                 \
9047     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9048     tcg_temp_free_i64(t0);                                                    \
9049 }
9050 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
9051 static inline void gen_##name(DisasContext *ctx)                              \
9052 {                                                                             \
9053     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9054     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9055     gen_helper_##name(t0, cpu_env, t0);                                       \
9056     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9057     tcg_temp_free_i64(t0);                                                    \
9058 }
9059 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
9060 static inline void gen_##name(DisasContext *ctx)                              \
9061 {                                                                             \
9062     if (unlikely(!ctx->spe_enabled)) {                                        \
9063         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9064         return;                                                               \
9065     }                                                                         \
9066     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
9067                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9068 }
9069 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
9070 static inline void gen_##name(DisasContext *ctx)                              \
9071 {                                                                             \
9072     TCGv_i64 t0, t1;                                                          \
9073     if (unlikely(!ctx->spe_enabled)) {                                        \
9074         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9075         return;                                                               \
9076     }                                                                         \
9077     t0 = tcg_temp_new_i64();                                                  \
9078     t1 = tcg_temp_new_i64();                                                  \
9079     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9080     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9081     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9082     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9083     tcg_temp_free_i64(t0);                                                    \
9084     tcg_temp_free_i64(t1);                                                    \
9085 }
9086 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9087 static inline void gen_##name(DisasContext *ctx)                              \
9088 {                                                                             \
9089     if (unlikely(!ctx->spe_enabled)) {                                        \
9090         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9091         return;                                                               \
9092     }                                                                         \
9093     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env,                    \
9094                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
9095 }
9096 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9097 static inline void gen_##name(DisasContext *ctx)                              \
9098 {                                                                             \
9099     TCGv_i64 t0, t1;                                                          \
9100     if (unlikely(!ctx->spe_enabled)) {                                        \
9101         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9102         return;                                                               \
9103     }                                                                         \
9104     t0 = tcg_temp_new_i64();                                                  \
9105     t1 = tcg_temp_new_i64();                                                  \
9106     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9107     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9108     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9109     tcg_temp_free_i64(t0);                                                    \
9110     tcg_temp_free_i64(t1);                                                    \
9111 }
9112 #endif
9113
9114 /* Single precision floating-point vectors operations */
9115 /* Arithmetic */
9116 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9117 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9118 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9119 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
9120 static inline void gen_evfsabs(DisasContext *ctx)
9121 {
9122     if (unlikely(!ctx->spe_enabled)) {
9123         gen_exception(ctx, POWERPC_EXCP_SPEU);
9124         return;
9125     }
9126 #if defined(TARGET_PPC64)
9127     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
9128 #else
9129     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
9130     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
9131 #endif
9132 }
9133 static inline void gen_evfsnabs(DisasContext *ctx)
9134 {
9135     if (unlikely(!ctx->spe_enabled)) {
9136         gen_exception(ctx, POWERPC_EXCP_SPEU);
9137         return;
9138     }
9139 #if defined(TARGET_PPC64)
9140     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
9141 #else
9142     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9143     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
9144 #endif
9145 }
9146 static inline void gen_evfsneg(DisasContext *ctx)
9147 {
9148     if (unlikely(!ctx->spe_enabled)) {
9149         gen_exception(ctx, POWERPC_EXCP_SPEU);
9150         return;
9151     }
9152 #if defined(TARGET_PPC64)
9153     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
9154 #else
9155     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9156     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
9157 #endif
9158 }
9159
9160 /* Conversion */
9161 GEN_SPEFPUOP_CONV_64_64(evfscfui);
9162 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9163 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9164 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9165 GEN_SPEFPUOP_CONV_64_64(evfsctui);
9166 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9167 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9168 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9169 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9170 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9171
9172 /* Comparison */
9173 GEN_SPEFPUOP_COMP_64(evfscmpgt);
9174 GEN_SPEFPUOP_COMP_64(evfscmplt);
9175 GEN_SPEFPUOP_COMP_64(evfscmpeq);
9176 GEN_SPEFPUOP_COMP_64(evfststgt);
9177 GEN_SPEFPUOP_COMP_64(evfststlt);
9178 GEN_SPEFPUOP_COMP_64(evfststeq);
9179
9180 /* Opcodes definitions */
9181 GEN_SPE(evfsadd,   evfssub,   0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9182 GEN_SPE(evfsabs,   evfsnabs,  0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9183 GEN_SPE(evfsneg,   speundef,  0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9184 GEN_SPE(evfsmul,   evfsdiv,   0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9185 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9186 GEN_SPE(evfscmpeq, speundef,  0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9187 GEN_SPE(evfscfui,  evfscfsi,  0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9188 GEN_SPE(evfscfuf,  evfscfsf,  0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9189 GEN_SPE(evfsctui,  evfsctsi,  0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9190 GEN_SPE(evfsctuf,  evfsctsf,  0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9191 GEN_SPE(evfsctuiz, speundef,  0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9192 GEN_SPE(evfsctsiz, speundef,  0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9193 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9194 GEN_SPE(evfststeq, speundef,  0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9195
9196 /* Single precision floating-point operations */
9197 /* Arithmetic */
9198 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9199 GEN_SPEFPUOP_ARITH2_32_32(efssub);
9200 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9201 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
9202 static inline void gen_efsabs(DisasContext *ctx)
9203 {
9204     if (unlikely(!ctx->spe_enabled)) {
9205         gen_exception(ctx, POWERPC_EXCP_SPEU);
9206         return;
9207     }
9208     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
9209 }
9210 static inline void gen_efsnabs(DisasContext *ctx)
9211 {
9212     if (unlikely(!ctx->spe_enabled)) {
9213         gen_exception(ctx, POWERPC_EXCP_SPEU);
9214         return;
9215     }
9216     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9217 }
9218 static inline void gen_efsneg(DisasContext *ctx)
9219 {
9220     if (unlikely(!ctx->spe_enabled)) {
9221         gen_exception(ctx, POWERPC_EXCP_SPEU);
9222         return;
9223     }
9224     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9225 }
9226
9227 /* Conversion */
9228 GEN_SPEFPUOP_CONV_32_32(efscfui);
9229 GEN_SPEFPUOP_CONV_32_32(efscfsi);
9230 GEN_SPEFPUOP_CONV_32_32(efscfuf);
9231 GEN_SPEFPUOP_CONV_32_32(efscfsf);
9232 GEN_SPEFPUOP_CONV_32_32(efsctui);
9233 GEN_SPEFPUOP_CONV_32_32(efsctsi);
9234 GEN_SPEFPUOP_CONV_32_32(efsctuf);
9235 GEN_SPEFPUOP_CONV_32_32(efsctsf);
9236 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9237 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9238 GEN_SPEFPUOP_CONV_32_64(efscfd);
9239
9240 /* Comparison */
9241 GEN_SPEFPUOP_COMP_32(efscmpgt);
9242 GEN_SPEFPUOP_COMP_32(efscmplt);
9243 GEN_SPEFPUOP_COMP_32(efscmpeq);
9244 GEN_SPEFPUOP_COMP_32(efststgt);
9245 GEN_SPEFPUOP_COMP_32(efststlt);
9246 GEN_SPEFPUOP_COMP_32(efststeq);
9247
9248 /* Opcodes definitions */
9249 GEN_SPE(efsadd,   efssub,   0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9250 GEN_SPE(efsabs,   efsnabs,  0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9251 GEN_SPE(efsneg,   speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9252 GEN_SPE(efsmul,   efsdiv,   0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9253 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9254 GEN_SPE(efscmpeq, efscfd,   0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9255 GEN_SPE(efscfui,  efscfsi,  0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9256 GEN_SPE(efscfuf,  efscfsf,  0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9257 GEN_SPE(efsctui,  efsctsi,  0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9258 GEN_SPE(efsctuf,  efsctsf,  0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9259 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9260 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9261 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9262 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9263
9264 /* Double precision floating-point operations */
9265 /* Arithmetic */
9266 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9267 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9268 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9269 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
9270 static inline void gen_efdabs(DisasContext *ctx)
9271 {
9272     if (unlikely(!ctx->spe_enabled)) {
9273         gen_exception(ctx, POWERPC_EXCP_SPEU);
9274         return;
9275     }
9276 #if defined(TARGET_PPC64)
9277     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
9278 #else
9279     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9280     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
9281 #endif
9282 }
9283 static inline void gen_efdnabs(DisasContext *ctx)
9284 {
9285     if (unlikely(!ctx->spe_enabled)) {
9286         gen_exception(ctx, POWERPC_EXCP_SPEU);
9287         return;
9288     }
9289 #if defined(TARGET_PPC64)
9290     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
9291 #else
9292     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9293     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
9294 #endif
9295 }
9296 static inline void gen_efdneg(DisasContext *ctx)
9297 {
9298     if (unlikely(!ctx->spe_enabled)) {
9299         gen_exception(ctx, POWERPC_EXCP_SPEU);
9300         return;
9301     }
9302 #if defined(TARGET_PPC64)
9303     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
9304 #else
9305     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9306     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
9307 #endif
9308 }
9309
9310 /* Conversion */
9311 GEN_SPEFPUOP_CONV_64_32(efdcfui);
9312 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9313 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9314 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9315 GEN_SPEFPUOP_CONV_32_64(efdctui);
9316 GEN_SPEFPUOP_CONV_32_64(efdctsi);
9317 GEN_SPEFPUOP_CONV_32_64(efdctuf);
9318 GEN_SPEFPUOP_CONV_32_64(efdctsf);
9319 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9320 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9321 GEN_SPEFPUOP_CONV_64_32(efdcfs);
9322 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9323 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9324 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9325 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
9326
9327 /* Comparison */
9328 GEN_SPEFPUOP_COMP_64(efdcmpgt);
9329 GEN_SPEFPUOP_COMP_64(efdcmplt);
9330 GEN_SPEFPUOP_COMP_64(efdcmpeq);
9331 GEN_SPEFPUOP_COMP_64(efdtstgt);
9332 GEN_SPEFPUOP_COMP_64(efdtstlt);
9333 GEN_SPEFPUOP_COMP_64(efdtsteq);
9334
9335 /* Opcodes definitions */
9336 GEN_SPE(efdadd,    efdsub,    0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9337 GEN_SPE(efdcfuid,  efdcfsid,  0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9338 GEN_SPE(efdabs,    efdnabs,   0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9339 GEN_SPE(efdneg,    speundef,  0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9340 GEN_SPE(efdmul,    efddiv,    0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9341 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9342 GEN_SPE(efdcmpgt,  efdcmplt,  0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9343 GEN_SPE(efdcmpeq,  efdcfs,    0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9344 GEN_SPE(efdcfui,   efdcfsi,   0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9345 GEN_SPE(efdcfuf,   efdcfsf,   0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9346 GEN_SPE(efdctui,   efdctsi,   0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9347 GEN_SPE(efdctuf,   efdctsf,   0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9348 GEN_SPE(efdctuiz,  speundef,  0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9349 GEN_SPE(efdctsiz,  speundef,  0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9350 GEN_SPE(efdtstgt,  efdtstlt,  0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9351 GEN_SPE(efdtsteq,  speundef,  0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9352
9353 static opcode_t opcodes[] = {
9354 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9355 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9356 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9357 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9358 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9359 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
9360 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9361 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9362 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9363 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9364 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9365 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9366 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9367 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9368 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9369 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9370 #if defined(TARGET_PPC64)
9371 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9372 #endif
9373 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9374 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9375 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9376 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9377 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9378 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9379 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9380 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9381 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9382 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9383 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9384 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9385 GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB),
9386 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
9387 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
9388 #if defined(TARGET_PPC64)
9389 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
9390 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
9391 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
9392 #endif
9393 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9394 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9395 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9396 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9397 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9398 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9399 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9400 #if defined(TARGET_PPC64)
9401 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9402 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9403 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9404 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9405 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9406 #endif
9407 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9408 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9409 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9410 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9411 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
9412 GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
9413 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
9414 GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9415 GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
9416 GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
9417 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9418 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9419 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9420 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
9421 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9422 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
9423 #if defined(TARGET_PPC64)
9424 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9425 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9426 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9427 #endif
9428 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9429 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9430 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9431 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9432 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9433 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9434 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9435 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
9436 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
9437 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9438 #if defined(TARGET_PPC64)
9439 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
9440 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9441 #endif
9442 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9443 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9444 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9445 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9446 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9447 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9448 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9449 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9450 #if defined(TARGET_PPC64)
9451 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9452 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9453 #endif
9454 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9455 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9456 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9457 #if defined(TARGET_PPC64)
9458 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9459 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9460 #endif
9461 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9462 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9463 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9464 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9465 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9466 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9467 #if defined(TARGET_PPC64)
9468 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9469 #endif
9470 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9471 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC),
9472 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9473 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9474 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9475 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE),
9476 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE),
9477 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
9478 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9479 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9480 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9481 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9482 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9483 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9484 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9485 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9486 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9487 #if defined(TARGET_PPC64)
9488 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9489 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9490              PPC_SEGMENT_64B),
9491 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9492 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9493              PPC_SEGMENT_64B),
9494 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9495 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9496 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
9497 #endif
9498 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9499 GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9500 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9501 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9502 #if defined(TARGET_PPC64)
9503 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9504 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9505 #endif
9506 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9507 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9508 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9509 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9510 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9511 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9512 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9513 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9514 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9515 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9516 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9517 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9518 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9519 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9520 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9521 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9522 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9523 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9524 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9525 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9526 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9527 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9528 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9529 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9530 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9531 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9532 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9533 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9534 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9535 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9536 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9537 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9538 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9539 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9540 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9541 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9542 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9543 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9544 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9545 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9546 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9547 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9548 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9549 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
9550 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
9551 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
9552 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
9553 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
9554 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
9555 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9556 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9557 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
9558 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
9559 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9560 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
9561 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
9562 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
9563 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
9564 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
9565 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
9566 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
9567 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
9568 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
9569 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
9570 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
9571 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
9572 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
9573 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
9574 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
9575 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
9576 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
9577 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
9578 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
9579 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
9580 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
9581 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
9582 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
9583 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
9584 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
9585 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
9586 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
9587                PPC_NONE, PPC2_BOOKE206),
9588 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
9589                PPC_NONE, PPC2_BOOKE206),
9590 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
9591                PPC_NONE, PPC2_BOOKE206),
9592 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
9593                PPC_NONE, PPC2_BOOKE206),
9594 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
9595                PPC_NONE, PPC2_BOOKE206),
9596 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
9597                PPC_NONE, PPC2_PRCNTL),
9598 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
9599                PPC_NONE, PPC2_PRCNTL),
9600 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
9601 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
9602 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
9603 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
9604               PPC_BOOKE, PPC2_BOOKE206),
9605 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
9606 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
9607                PPC_BOOKE, PPC2_BOOKE206),
9608 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
9609 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
9610 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
9611 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
9612 GEN_HANDLER(vsldoi, 0x04, 0x16, 0xFF, 0x00000400, PPC_ALTIVEC),
9613 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
9614 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
9615 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
9616 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
9617 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
9618
9619 #undef GEN_INT_ARITH_ADD
9620 #undef GEN_INT_ARITH_ADD_CONST
9621 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
9622 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
9623 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
9624                                 add_ca, compute_ca, compute_ov)               \
9625 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
9626 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
9627 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
9628 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
9629 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
9630 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
9631 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
9632 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
9633 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
9634 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
9635 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
9636
9637 #undef GEN_INT_ARITH_DIVW
9638 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
9639 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
9640 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
9641 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
9642 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
9643 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
9644
9645 #if defined(TARGET_PPC64)
9646 #undef GEN_INT_ARITH_DIVD
9647 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
9648 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9649 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
9650 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
9651 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
9652 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
9653
9654 #undef GEN_INT_ARITH_MUL_HELPER
9655 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
9656 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
9657 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
9658 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
9659 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
9660 #endif
9661
9662 #undef GEN_INT_ARITH_SUBF
9663 #undef GEN_INT_ARITH_SUBF_CONST
9664 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
9665 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
9666 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
9667                                 add_ca, compute_ca, compute_ov)               \
9668 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
9669 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
9670 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
9671 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
9672 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
9673 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
9674 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
9675 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
9676 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
9677 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
9678 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
9679
9680 #undef GEN_LOGICAL1
9681 #undef GEN_LOGICAL2
9682 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
9683 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
9684 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
9685 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
9686 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
9687 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
9688 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
9689 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
9690 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
9691 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
9692 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
9693 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
9694 #if defined(TARGET_PPC64)
9695 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
9696 #endif
9697
9698 #if defined(TARGET_PPC64)
9699 #undef GEN_PPC64_R2
9700 #undef GEN_PPC64_R4
9701 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
9702 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9703 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
9704              PPC_64B)
9705 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
9706 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
9707 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
9708              PPC_64B),                                                        \
9709 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
9710              PPC_64B),                                                        \
9711 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
9712              PPC_64B)
9713 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
9714 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
9715 GEN_PPC64_R4(rldic, 0x1E, 0x04),
9716 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
9717 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
9718 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
9719 #endif
9720
9721 #undef _GEN_FLOAT_ACB
9722 #undef GEN_FLOAT_ACB
9723 #undef _GEN_FLOAT_AB
9724 #undef GEN_FLOAT_AB
9725 #undef _GEN_FLOAT_AC
9726 #undef GEN_FLOAT_AC
9727 #undef GEN_FLOAT_B
9728 #undef GEN_FLOAT_BS
9729 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
9730 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
9731 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
9732 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type),                     \
9733 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
9734 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
9735 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9736 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
9737 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
9738 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9739 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
9740 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
9741 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
9742 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
9743 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
9744 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
9745 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
9746 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
9747 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
9748
9749 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
9750 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
9751 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
9752 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
9753 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
9754 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
9755 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
9756 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
9757 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
9758 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
9759 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
9760 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
9761 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
9762 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
9763 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
9764 #if defined(TARGET_PPC64)
9765 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B),
9766 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B),
9767 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B),
9768 #endif
9769 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
9770 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
9771 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
9772 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
9773
9774 #undef GEN_LD
9775 #undef GEN_LDU
9776 #undef GEN_LDUX
9777 #undef GEN_LDX_E
9778 #undef GEN_LDS
9779 #define GEN_LD(name, ldop, opc, type)                                         \
9780 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9781 #define GEN_LDU(name, ldop, opc, type)                                        \
9782 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9783 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
9784 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
9785 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
9786 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
9787 #define GEN_LDS(name, ldop, op, type)                                         \
9788 GEN_LD(name, ldop, op | 0x20, type)                                           \
9789 GEN_LDU(name, ldop, op | 0x21, type)                                          \
9790 GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
9791 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
9792
9793 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
9794 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
9795 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
9796 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
9797 #if defined(TARGET_PPC64)
9798 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
9799 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
9800 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
9801 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
9802 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
9803 #endif
9804 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
9805 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
9806
9807 #undef GEN_ST
9808 #undef GEN_STU
9809 #undef GEN_STUX
9810 #undef GEN_STX_E
9811 #undef GEN_STS
9812 #define GEN_ST(name, stop, opc, type)                                         \
9813 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9814 #define GEN_STU(name, stop, opc, type)                                        \
9815 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
9816 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
9817 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
9818 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
9819 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
9820 #define GEN_STS(name, stop, op, type)                                         \
9821 GEN_ST(name, stop, op | 0x20, type)                                           \
9822 GEN_STU(name, stop, op | 0x21, type)                                          \
9823 GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
9824 GEN_STX(name, stop, 0x17, op | 0x00, type)
9825
9826 GEN_STS(stb, st8, 0x06, PPC_INTEGER)
9827 GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
9828 GEN_STS(stw, st32, 0x04, PPC_INTEGER)
9829 #if defined(TARGET_PPC64)
9830 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
9831 GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
9832 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
9833 #endif
9834 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
9835 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
9836
9837 #undef GEN_LDF
9838 #undef GEN_LDUF
9839 #undef GEN_LDUXF
9840 #undef GEN_LDXF
9841 #undef GEN_LDFS
9842 #define GEN_LDF(name, ldop, opc, type)                                        \
9843 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9844 #define GEN_LDUF(name, ldop, opc, type)                                       \
9845 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9846 #define GEN_LDUXF(name, ldop, opc, type)                                      \
9847 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9848 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
9849 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9850 #define GEN_LDFS(name, ldop, op, type)                                        \
9851 GEN_LDF(name, ldop, op | 0x20, type)                                          \
9852 GEN_LDUF(name, ldop, op | 0x21, type)                                         \
9853 GEN_LDUXF(name, ldop, op | 0x01, type)                                        \
9854 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
9855
9856 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
9857 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
9858 GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
9859 GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9860 GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
9861
9862 #undef GEN_STF
9863 #undef GEN_STUF
9864 #undef GEN_STUXF
9865 #undef GEN_STXF
9866 #undef GEN_STFS
9867 #define GEN_STF(name, stop, opc, type)                                        \
9868 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
9869 #define GEN_STUF(name, stop, opc, type)                                       \
9870 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
9871 #define GEN_STUXF(name, stop, opc, type)                                      \
9872 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
9873 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
9874 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
9875 #define GEN_STFS(name, stop, op, type)                                        \
9876 GEN_STF(name, stop, op | 0x20, type)                                          \
9877 GEN_STUF(name, stop, op | 0x21, type)                                         \
9878 GEN_STUXF(name, stop, op | 0x01, type)                                        \
9879 GEN_STXF(name, stop, 0x17, op | 0x00, type)
9880
9881 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
9882 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
9883 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
9884 GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
9885 GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
9886
9887 #undef GEN_CRLOGIC
9888 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
9889 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
9890 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
9891 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
9892 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
9893 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
9894 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
9895 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
9896 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
9897 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
9898
9899 #undef GEN_MAC_HANDLER
9900 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
9901 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
9902 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
9903 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
9904 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
9905 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
9906 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
9907 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
9908 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
9909 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
9910 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
9911 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
9912 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
9913 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
9914 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
9915 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
9916 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
9917 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
9918 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
9919 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
9920 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
9921 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
9922 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
9923 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
9924 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
9925 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
9926 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
9927 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
9928 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
9929 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
9930 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
9931 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
9932 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
9933 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
9934 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
9935 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
9936 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
9937 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
9938 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
9939 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
9940 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
9941 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
9942 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
9943 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
9944
9945 #undef GEN_VR_LDX
9946 #undef GEN_VR_STX
9947 #undef GEN_VR_LVE
9948 #undef GEN_VR_STVE
9949 #define GEN_VR_LDX(name, opc2, opc3)                                          \
9950 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9951 #define GEN_VR_STX(name, opc2, opc3)                                          \
9952 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9953 #define GEN_VR_LVE(name, opc2, opc3)                                    \
9954     GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9955 #define GEN_VR_STVE(name, opc2, opc3)                                   \
9956     GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
9957 GEN_VR_LDX(lvx, 0x07, 0x03),
9958 GEN_VR_LDX(lvxl, 0x07, 0x0B),
9959 GEN_VR_LVE(bx, 0x07, 0x00),
9960 GEN_VR_LVE(hx, 0x07, 0x01),
9961 GEN_VR_LVE(wx, 0x07, 0x02),
9962 GEN_VR_STX(svx, 0x07, 0x07),
9963 GEN_VR_STX(svxl, 0x07, 0x0F),
9964 GEN_VR_STVE(bx, 0x07, 0x04),
9965 GEN_VR_STVE(hx, 0x07, 0x05),
9966 GEN_VR_STVE(wx, 0x07, 0x06),
9967
9968 #undef GEN_VX_LOGICAL
9969 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
9970 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9971 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
9972 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
9973 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
9974 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
9975 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
9976
9977 #undef GEN_VXFORM
9978 #define GEN_VXFORM(name, opc2, opc3)                                    \
9979 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
9980 GEN_VXFORM(vaddubm, 0, 0),
9981 GEN_VXFORM(vadduhm, 0, 1),
9982 GEN_VXFORM(vadduwm, 0, 2),
9983 GEN_VXFORM(vsububm, 0, 16),
9984 GEN_VXFORM(vsubuhm, 0, 17),
9985 GEN_VXFORM(vsubuwm, 0, 18),
9986 GEN_VXFORM(vmaxub, 1, 0),
9987 GEN_VXFORM(vmaxuh, 1, 1),
9988 GEN_VXFORM(vmaxuw, 1, 2),
9989 GEN_VXFORM(vmaxsb, 1, 4),
9990 GEN_VXFORM(vmaxsh, 1, 5),
9991 GEN_VXFORM(vmaxsw, 1, 6),
9992 GEN_VXFORM(vminub, 1, 8),
9993 GEN_VXFORM(vminuh, 1, 9),
9994 GEN_VXFORM(vminuw, 1, 10),
9995 GEN_VXFORM(vminsb, 1, 12),
9996 GEN_VXFORM(vminsh, 1, 13),
9997 GEN_VXFORM(vminsw, 1, 14),
9998 GEN_VXFORM(vavgub, 1, 16),
9999 GEN_VXFORM(vavguh, 1, 17),
10000 GEN_VXFORM(vavguw, 1, 18),
10001 GEN_VXFORM(vavgsb, 1, 20),
10002 GEN_VXFORM(vavgsh, 1, 21),
10003 GEN_VXFORM(vavgsw, 1, 22),
10004 GEN_VXFORM(vmrghb, 6, 0),
10005 GEN_VXFORM(vmrghh, 6, 1),
10006 GEN_VXFORM(vmrghw, 6, 2),
10007 GEN_VXFORM(vmrglb, 6, 4),
10008 GEN_VXFORM(vmrglh, 6, 5),
10009 GEN_VXFORM(vmrglw, 6, 6),
10010 GEN_VXFORM(vmuloub, 4, 0),
10011 GEN_VXFORM(vmulouh, 4, 1),
10012 GEN_VXFORM(vmulosb, 4, 4),
10013 GEN_VXFORM(vmulosh, 4, 5),
10014 GEN_VXFORM(vmuleub, 4, 8),
10015 GEN_VXFORM(vmuleuh, 4, 9),
10016 GEN_VXFORM(vmulesb, 4, 12),
10017 GEN_VXFORM(vmulesh, 4, 13),
10018 GEN_VXFORM(vslb, 2, 4),
10019 GEN_VXFORM(vslh, 2, 5),
10020 GEN_VXFORM(vslw, 2, 6),
10021 GEN_VXFORM(vsrb, 2, 8),
10022 GEN_VXFORM(vsrh, 2, 9),
10023 GEN_VXFORM(vsrw, 2, 10),
10024 GEN_VXFORM(vsrab, 2, 12),
10025 GEN_VXFORM(vsrah, 2, 13),
10026 GEN_VXFORM(vsraw, 2, 14),
10027 GEN_VXFORM(vslo, 6, 16),
10028 GEN_VXFORM(vsro, 6, 17),
10029 GEN_VXFORM(vaddcuw, 0, 6),
10030 GEN_VXFORM(vsubcuw, 0, 22),
10031 GEN_VXFORM(vaddubs, 0, 8),
10032 GEN_VXFORM(vadduhs, 0, 9),
10033 GEN_VXFORM(vadduws, 0, 10),
10034 GEN_VXFORM(vaddsbs, 0, 12),
10035 GEN_VXFORM(vaddshs, 0, 13),
10036 GEN_VXFORM(vaddsws, 0, 14),
10037 GEN_VXFORM(vsububs, 0, 24),
10038 GEN_VXFORM(vsubuhs, 0, 25),
10039 GEN_VXFORM(vsubuws, 0, 26),
10040 GEN_VXFORM(vsubsbs, 0, 28),
10041 GEN_VXFORM(vsubshs, 0, 29),
10042 GEN_VXFORM(vsubsws, 0, 30),
10043 GEN_VXFORM(vrlb, 2, 0),
10044 GEN_VXFORM(vrlh, 2, 1),
10045 GEN_VXFORM(vrlw, 2, 2),
10046 GEN_VXFORM(vsl, 2, 7),
10047 GEN_VXFORM(vsr, 2, 11),
10048 GEN_VXFORM(vpkuhum, 7, 0),
10049 GEN_VXFORM(vpkuwum, 7, 1),
10050 GEN_VXFORM(vpkuhus, 7, 2),
10051 GEN_VXFORM(vpkuwus, 7, 3),
10052 GEN_VXFORM(vpkshus, 7, 4),
10053 GEN_VXFORM(vpkswus, 7, 5),
10054 GEN_VXFORM(vpkshss, 7, 6),
10055 GEN_VXFORM(vpkswss, 7, 7),
10056 GEN_VXFORM(vpkpx, 7, 12),
10057 GEN_VXFORM(vsum4ubs, 4, 24),
10058 GEN_VXFORM(vsum4sbs, 4, 28),
10059 GEN_VXFORM(vsum4shs, 4, 25),
10060 GEN_VXFORM(vsum2sws, 4, 26),
10061 GEN_VXFORM(vsumsws, 4, 30),
10062 GEN_VXFORM(vaddfp, 5, 0),
10063 GEN_VXFORM(vsubfp, 5, 1),
10064 GEN_VXFORM(vmaxfp, 5, 16),
10065 GEN_VXFORM(vminfp, 5, 17),
10066
10067 #undef GEN_VXRFORM1
10068 #undef GEN_VXRFORM
10069 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
10070     GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10071 #define GEN_VXRFORM(name, opc2, opc3)                                \
10072     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
10073     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10074 GEN_VXRFORM(vcmpequb, 3, 0)
10075 GEN_VXRFORM(vcmpequh, 3, 1)
10076 GEN_VXRFORM(vcmpequw, 3, 2)
10077 GEN_VXRFORM(vcmpgtsb, 3, 12)
10078 GEN_VXRFORM(vcmpgtsh, 3, 13)
10079 GEN_VXRFORM(vcmpgtsw, 3, 14)
10080 GEN_VXRFORM(vcmpgtub, 3, 8)
10081 GEN_VXRFORM(vcmpgtuh, 3, 9)
10082 GEN_VXRFORM(vcmpgtuw, 3, 10)
10083 GEN_VXRFORM(vcmpeqfp, 3, 3)
10084 GEN_VXRFORM(vcmpgefp, 3, 7)
10085 GEN_VXRFORM(vcmpgtfp, 3, 11)
10086 GEN_VXRFORM(vcmpbfp, 3, 15)
10087
10088 #undef GEN_VXFORM_SIMM
10089 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
10090     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10091 GEN_VXFORM_SIMM(vspltisb, 6, 12),
10092 GEN_VXFORM_SIMM(vspltish, 6, 13),
10093 GEN_VXFORM_SIMM(vspltisw, 6, 14),
10094
10095 #undef GEN_VXFORM_NOA
10096 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
10097     GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10098 GEN_VXFORM_NOA(vupkhsb, 7, 8),
10099 GEN_VXFORM_NOA(vupkhsh, 7, 9),
10100 GEN_VXFORM_NOA(vupklsb, 7, 10),
10101 GEN_VXFORM_NOA(vupklsh, 7, 11),
10102 GEN_VXFORM_NOA(vupkhpx, 7, 13),
10103 GEN_VXFORM_NOA(vupklpx, 7, 15),
10104 GEN_VXFORM_NOA(vrefp, 5, 4),
10105 GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
10106 GEN_VXFORM_NOA(vexptefp, 5, 6),
10107 GEN_VXFORM_NOA(vlogefp, 5, 7),
10108 GEN_VXFORM_NOA(vrfim, 5, 8),
10109 GEN_VXFORM_NOA(vrfin, 5, 9),
10110 GEN_VXFORM_NOA(vrfip, 5, 10),
10111 GEN_VXFORM_NOA(vrfiz, 5, 11),
10112
10113 #undef GEN_VXFORM_UIMM
10114 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
10115     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10116 GEN_VXFORM_UIMM(vspltb, 6, 8),
10117 GEN_VXFORM_UIMM(vsplth, 6, 9),
10118 GEN_VXFORM_UIMM(vspltw, 6, 10),
10119 GEN_VXFORM_UIMM(vcfux, 5, 12),
10120 GEN_VXFORM_UIMM(vcfsx, 5, 13),
10121 GEN_VXFORM_UIMM(vctuxs, 5, 14),
10122 GEN_VXFORM_UIMM(vctsxs, 5, 15),
10123
10124 #undef GEN_VAFORM_PAIRED
10125 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
10126     GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10127 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10128 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10129 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10130 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10131 GEN_VAFORM_PAIRED(vsel, vperm, 21),
10132 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10133
10134 GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
10135 GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10136 GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10137 GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
10138 GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
10139 GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
10140 GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
10141
10142 GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
10143 GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10144 GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
10145 GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
10146 GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
10147
10148 GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10149 GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10150 GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10151 #if defined(TARGET_PPC64)
10152 GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10153 GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10154 #endif
10155
10156 #undef GEN_XX2FORM
10157 #define GEN_XX2FORM(name, opc2, opc3, fl2)                           \
10158 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10159 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10160
10161 #undef GEN_XX3FORM
10162 #define GEN_XX3FORM(name, opc2, opc3, fl2)                           \
10163 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10164 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10165 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10166 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10167
10168 #undef GEN_XX3_RC_FORM
10169 #define GEN_XX3_RC_FORM(name, opc2, opc3, fl2)                          \
10170 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10171 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10172 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10173 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10174 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10175 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10176 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10177 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10178
10179 #undef GEN_XX3FORM_DM
10180 #define GEN_XX3FORM_DM(name, opc2, opc3) \
10181 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10182 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10183 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10184 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10185 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10186 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10187 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10188 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10189 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10190 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10191 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10192 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10193 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10194 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10195 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10196 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10197
10198 GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10199 GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10200 GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10201 GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10202
10203 GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10204 GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10205 GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10206 GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10207 GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10208 GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10209 GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10210 GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
10211
10212 GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10213 GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
10214 GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
10215 GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
10216 GEN_XX2FORM(xsredp,  0x14, 0x05, PPC2_VSX),
10217 GEN_XX2FORM(xssqrtdp,  0x16, 0x04, PPC2_VSX),
10218 GEN_XX2FORM(xsrsqrtedp,  0x14, 0x04, PPC2_VSX),
10219 GEN_XX3FORM(xstdivdp,  0x14, 0x07, PPC2_VSX),
10220 GEN_XX2FORM(xstsqrtdp,  0x14, 0x06, PPC2_VSX),
10221 GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10222 GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10223 GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10224 GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10225 GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10226 GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10227 GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10228 GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
10229 GEN_XX2FORM(xscmpodp,  0x0C, 0x05, PPC2_VSX),
10230 GEN_XX2FORM(xscmpudp,  0x0C, 0x04, PPC2_VSX),
10231 GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10232 GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
10233 GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
10234 GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
10235 GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10236 GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10237 GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10238 GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10239 GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10240 GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
10241 GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10242 GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10243 GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10244 GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10245 GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
10246
10247 GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
10248 GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
10249 GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
10250 GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
10251 GEN_XX2FORM(xsresp,  0x14, 0x01, PPC2_VSX207),
10252 GEN_XX2FORM(xssqrtsp,  0x16, 0x00, PPC2_VSX207),
10253 GEN_XX2FORM(xsrsqrtesp,  0x14, 0x00, PPC2_VSX207),
10254 GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
10255 GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
10256 GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
10257 GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
10258 GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
10259 GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
10260 GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
10261 GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
10262 GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
10263 GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
10264
10265 GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10266 GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
10267 GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
10268 GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
10269 GEN_XX2FORM(xvredp,  0x14, 0x0D, PPC2_VSX),
10270 GEN_XX2FORM(xvsqrtdp,  0x16, 0x0C, PPC2_VSX),
10271 GEN_XX2FORM(xvrsqrtedp,  0x14, 0x0C, PPC2_VSX),
10272 GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
10273 GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
10274 GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
10275 GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
10276 GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
10277 GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
10278 GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
10279 GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
10280 GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
10281 GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
10282 GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
10283 GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
10284 GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
10285 GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
10286 GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
10287 GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
10288 GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
10289 GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
10290 GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
10291 GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
10292 GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
10293 GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
10294 GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
10295 GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
10296 GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
10297 GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
10298 GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
10299 GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
10300 GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
10301
10302 GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10303 GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
10304 GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
10305 GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
10306 GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
10307 GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
10308 GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
10309 GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
10310 GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
10311 GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
10312 GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
10313 GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
10314 GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
10315 GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
10316 GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
10317 GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
10318 GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
10319 GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
10320 GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
10321 GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
10322 GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
10323 GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
10324 GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
10325 GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
10326 GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
10327 GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
10328 GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
10329 GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
10330 GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
10331 GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
10332 GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
10333 GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
10334 GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
10335 GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
10336 GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
10337 GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
10338
10339 #undef VSX_LOGICAL
10340 #define VSX_LOGICAL(name, opc2, opc3, fl2) \
10341 GEN_XX3FORM(name, opc2, opc3, fl2)
10342
10343 VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10344 VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10345 VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10346 VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10347 VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
10348 VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
10349 VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
10350 VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
10351 GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10352 GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
10353 GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
10354 GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
10355
10356 #define GEN_XXSEL_ROW(opc3) \
10357 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10358 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10359 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10360 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10361 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10362 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10363 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10364 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10365
10366 GEN_XXSEL_ROW(0x00)
10367 GEN_XXSEL_ROW(0x01)
10368 GEN_XXSEL_ROW(0x02)
10369 GEN_XXSEL_ROW(0x03)
10370 GEN_XXSEL_ROW(0x04)
10371 GEN_XXSEL_ROW(0x05)
10372 GEN_XXSEL_ROW(0x06)
10373 GEN_XXSEL_ROW(0x07)
10374 GEN_XXSEL_ROW(0x08)
10375 GEN_XXSEL_ROW(0x09)
10376 GEN_XXSEL_ROW(0x0A)
10377 GEN_XXSEL_ROW(0x0B)
10378 GEN_XXSEL_ROW(0x0C)
10379 GEN_XXSEL_ROW(0x0D)
10380 GEN_XXSEL_ROW(0x0E)
10381 GEN_XXSEL_ROW(0x0F)
10382 GEN_XXSEL_ROW(0x10)
10383 GEN_XXSEL_ROW(0x11)
10384 GEN_XXSEL_ROW(0x12)
10385 GEN_XXSEL_ROW(0x13)
10386 GEN_XXSEL_ROW(0x14)
10387 GEN_XXSEL_ROW(0x15)
10388 GEN_XXSEL_ROW(0x16)
10389 GEN_XXSEL_ROW(0x17)
10390 GEN_XXSEL_ROW(0x18)
10391 GEN_XXSEL_ROW(0x19)
10392 GEN_XXSEL_ROW(0x1A)
10393 GEN_XXSEL_ROW(0x1B)
10394 GEN_XXSEL_ROW(0x1C)
10395 GEN_XXSEL_ROW(0x1D)
10396 GEN_XXSEL_ROW(0x1E)
10397 GEN_XXSEL_ROW(0x1F)
10398
10399 GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10400
10401 #undef GEN_SPE
10402 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
10403     GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
10404 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10405 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10406 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10407 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10408 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10409 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10410 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
10411 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
10412 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
10413 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10414 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10415 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10416 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10417 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10418 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
10419 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
10420 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
10421 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10422 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10423 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10424 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10425 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
10426 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10427 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
10428 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10429 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
10430 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10431 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
10432 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
10433
10434 GEN_SPE(evfsadd,     evfssub,     0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10435 GEN_SPE(evfsabs,     evfsnabs,    0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10436 GEN_SPE(evfsneg,     speundef,    0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10437 GEN_SPE(evfsmul,     evfsdiv,     0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10438 GEN_SPE(evfscmpgt,   evfscmplt,   0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10439 GEN_SPE(evfscmpeq,   speundef,    0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10440 GEN_SPE(evfscfui,    evfscfsi,    0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10441 GEN_SPE(evfscfuf,    evfscfsf,    0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10442 GEN_SPE(evfsctui,    evfsctsi,    0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10443 GEN_SPE(evfsctuf,    evfsctsf,    0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10444 GEN_SPE(evfsctuiz,   speundef,    0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10445 GEN_SPE(evfsctsiz,   speundef,    0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10446 GEN_SPE(evfststgt,   evfststlt,   0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10447 GEN_SPE(evfststeq,   speundef,    0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10448
10449 GEN_SPE(efsadd,      efssub,      0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10450 GEN_SPE(efsabs,      efsnabs,     0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
10451 GEN_SPE(efsneg,      speundef,    0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
10452 GEN_SPE(efsmul,      efsdiv,      0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
10453 GEN_SPE(efscmpgt,    efscmplt,    0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10454 GEN_SPE(efscmpeq,    efscfd,      0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
10455 GEN_SPE(efscfui,     efscfsi,     0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10456 GEN_SPE(efscfuf,     efscfsf,     0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10457 GEN_SPE(efsctui,     efsctsi,     0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10458 GEN_SPE(efsctuf,     efsctsf,     0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
10459 GEN_SPE(efsctuiz,    speundef,    0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10460 GEN_SPE(efsctsiz,    speundef,    0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10461 GEN_SPE(efststgt,    efststlt,    0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
10462 GEN_SPE(efststeq,    speundef,    0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
10463
10464 GEN_SPE(efdadd,      efdsub,      0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10465 GEN_SPE(efdcfuid,    efdcfsid,    0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10466 GEN_SPE(efdabs,      efdnabs,     0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
10467 GEN_SPE(efdneg,      speundef,    0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10468 GEN_SPE(efdmul,      efddiv,      0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
10469 GEN_SPE(efdctuidz,   efdctsidz,   0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10470 GEN_SPE(efdcmpgt,    efdcmplt,    0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10471 GEN_SPE(efdcmpeq,    efdcfs,      0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
10472 GEN_SPE(efdcfui,     efdcfsi,     0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10473 GEN_SPE(efdcfuf,     efdcfsf,     0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10474 GEN_SPE(efdctui,     efdctsi,     0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10475 GEN_SPE(efdctuf,     efdctsf,     0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
10476 GEN_SPE(efdctuiz,    speundef,    0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10477 GEN_SPE(efdctsiz,    speundef,    0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10478 GEN_SPE(efdtstgt,    efdtstlt,    0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
10479 GEN_SPE(efdtsteq,    speundef,    0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
10480
10481 #undef GEN_SPEOP_LDST
10482 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
10483 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
10484 GEN_SPEOP_LDST(evldd, 0x00, 3),
10485 GEN_SPEOP_LDST(evldw, 0x01, 3),
10486 GEN_SPEOP_LDST(evldh, 0x02, 3),
10487 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
10488 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
10489 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
10490 GEN_SPEOP_LDST(evlwhe, 0x08, 2),
10491 GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
10492 GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
10493 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
10494 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
10495
10496 GEN_SPEOP_LDST(evstdd, 0x10, 3),
10497 GEN_SPEOP_LDST(evstdw, 0x11, 3),
10498 GEN_SPEOP_LDST(evstdh, 0x12, 3),
10499 GEN_SPEOP_LDST(evstwhe, 0x18, 2),
10500 GEN_SPEOP_LDST(evstwho, 0x1A, 2),
10501 GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
10502 GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
10503 };
10504
10505 #include "helper_regs.h"
10506 #include "translate_init.c"
10507
10508 /*****************************************************************************/
10509 /* Misc PowerPC helpers */
10510 void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
10511                         int flags)
10512 {
10513 #define RGPL  4
10514 #define RFPL  4
10515
10516     PowerPCCPU *cpu = POWERPC_CPU(cs);
10517     CPUPPCState *env = &cpu->env;
10518     int i;
10519
10520     cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
10521                 TARGET_FMT_lx " XER " TARGET_FMT_lx "\n",
10522                 env->nip, env->lr, env->ctr, cpu_read_xer(env));
10523     cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
10524                 TARGET_FMT_lx " idx %d\n", env->msr, env->spr[SPR_HID0],
10525                 env->hflags, env->mmu_idx);
10526 #if !defined(NO_TIMER_DUMP)
10527     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
10528 #if !defined(CONFIG_USER_ONLY)
10529                 " DECR %08" PRIu32
10530 #endif
10531                 "\n",
10532                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
10533 #if !defined(CONFIG_USER_ONLY)
10534                 , cpu_ppc_load_decr(env)
10535 #endif
10536                 );
10537 #endif
10538     for (i = 0; i < 32; i++) {
10539         if ((i & (RGPL - 1)) == 0)
10540             cpu_fprintf(f, "GPR%02d", i);
10541         cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
10542         if ((i & (RGPL - 1)) == (RGPL - 1))
10543             cpu_fprintf(f, "\n");
10544     }
10545     cpu_fprintf(f, "CR ");
10546     for (i = 0; i < 8; i++)
10547         cpu_fprintf(f, "%01x", env->crf[i]);
10548     cpu_fprintf(f, "  [");
10549     for (i = 0; i < 8; i++) {
10550         char a = '-';
10551         if (env->crf[i] & 0x08)
10552             a = 'L';
10553         else if (env->crf[i] & 0x04)
10554             a = 'G';
10555         else if (env->crf[i] & 0x02)
10556             a = 'E';
10557         cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
10558     }
10559     cpu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
10560                 env->reserve_addr);
10561     for (i = 0; i < 32; i++) {
10562         if ((i & (RFPL - 1)) == 0)
10563             cpu_fprintf(f, "FPR%02d", i);
10564         cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
10565         if ((i & (RFPL - 1)) == (RFPL - 1))
10566             cpu_fprintf(f, "\n");
10567     }
10568     cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
10569 #if !defined(CONFIG_USER_ONLY)
10570     cpu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
10571                    "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
10572                 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
10573                 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
10574
10575     cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
10576                    "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
10577                 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
10578                 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
10579
10580     cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
10581                    "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
10582                 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
10583                 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
10584
10585     if (env->excp_model == POWERPC_EXCP_BOOKE) {
10586         cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
10587                        " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
10588                     env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
10589                     env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
10590
10591         cpu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
10592                        "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
10593                     env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
10594                     env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
10595
10596         cpu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
10597                        "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
10598                     env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
10599                     env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
10600
10601         cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
10602                        "    EPR " TARGET_FMT_lx "\n",
10603                     env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
10604                     env->spr[SPR_BOOKE_EPR]);
10605
10606         /* FSL-specific */
10607         cpu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
10608                        "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
10609                     env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
10610                     env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
10611
10612         /*
10613          * IVORs are left out as they are large and do not change often --
10614          * they can be read with "p $ivor0", "p $ivor1", etc.
10615          */
10616     }
10617
10618 #if defined(TARGET_PPC64)
10619     if (env->flags & POWERPC_FLAG_CFAR) {
10620         cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
10621     }
10622 #endif
10623
10624     switch (env->mmu_model) {
10625     case POWERPC_MMU_32B:
10626     case POWERPC_MMU_601:
10627     case POWERPC_MMU_SOFT_6xx:
10628     case POWERPC_MMU_SOFT_74xx:
10629 #if defined(TARGET_PPC64)
10630     case POWERPC_MMU_64B:
10631     case POWERPC_MMU_2_06:
10632     case POWERPC_MMU_2_06a:
10633     case POWERPC_MMU_2_06d:
10634 #endif
10635         cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "   DAR " TARGET_FMT_lx
10636                        "  DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
10637                     env->spr[SPR_DAR], env->spr[SPR_DSISR]);
10638         break;
10639     case POWERPC_MMU_BOOKE206:
10640         cpu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
10641                        "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
10642                     env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
10643                     env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
10644
10645         cpu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
10646                        "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
10647                     env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
10648                     env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
10649
10650         cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
10651                        " TLB1CFG " TARGET_FMT_lx "\n",
10652                     env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
10653                     env->spr[SPR_BOOKE_TLB1CFG]);
10654         break;
10655     default:
10656         break;
10657     }
10658 #endif
10659
10660 #undef RGPL
10661 #undef RFPL
10662 }
10663
10664 void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
10665                              fprintf_function cpu_fprintf, int flags)
10666 {
10667 #if defined(DO_PPC_STATISTICS)
10668     PowerPCCPU *cpu = POWERPC_CPU(cs);
10669     opc_handler_t **t1, **t2, **t3, *handler;
10670     int op1, op2, op3;
10671
10672     t1 = cpu->env.opcodes;
10673     for (op1 = 0; op1 < 64; op1++) {
10674         handler = t1[op1];
10675         if (is_indirect_opcode(handler)) {
10676             t2 = ind_table(handler);
10677             for (op2 = 0; op2 < 32; op2++) {
10678                 handler = t2[op2];
10679                 if (is_indirect_opcode(handler)) {
10680                     t3 = ind_table(handler);
10681                     for (op3 = 0; op3 < 32; op3++) {
10682                         handler = t3[op3];
10683                         if (handler->count == 0)
10684                             continue;
10685                         cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
10686                                     "%016" PRIx64 " %" PRId64 "\n",
10687                                     op1, op2, op3, op1, (op3 << 5) | op2,
10688                                     handler->oname,
10689                                     handler->count, handler->count);
10690                     }
10691                 } else {
10692                     if (handler->count == 0)
10693                         continue;
10694                     cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
10695                                 "%016" PRIx64 " %" PRId64 "\n",
10696                                 op1, op2, op1, op2, handler->oname,
10697                                 handler->count, handler->count);
10698                 }
10699             }
10700         } else {
10701             if (handler->count == 0)
10702                 continue;
10703             cpu_fprintf(f, "%02x       (%02x     ) %16s: %016" PRIx64
10704                         " %" PRId64 "\n",
10705                         op1, op1, handler->oname,
10706                         handler->count, handler->count);
10707         }
10708     }
10709 #endif
10710 }
10711
10712 /*****************************************************************************/
10713 static inline void gen_intermediate_code_internal(PowerPCCPU *cpu,
10714                                                   TranslationBlock *tb,
10715                                                   bool search_pc)
10716 {
10717     CPUState *cs = CPU(cpu);
10718     CPUPPCState *env = &cpu->env;
10719     DisasContext ctx, *ctxp = &ctx;
10720     opc_handler_t **table, *handler;
10721     target_ulong pc_start;
10722     uint16_t *gen_opc_end;
10723     CPUBreakpoint *bp;
10724     int j, lj = -1;
10725     int num_insns;
10726     int max_insns;
10727
10728     pc_start = tb->pc;
10729     gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
10730     ctx.nip = pc_start;
10731     ctx.tb = tb;
10732     ctx.exception = POWERPC_EXCP_NONE;
10733     ctx.spr_cb = env->spr_cb;
10734     ctx.mem_idx = env->mmu_idx;
10735     ctx.insns_flags = env->insns_flags;
10736     ctx.insns_flags2 = env->insns_flags2;
10737     ctx.access_type = -1;
10738     ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
10739 #if defined(TARGET_PPC64)
10740     ctx.sf_mode = msr_is_64bit(env, env->msr);
10741     ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
10742 #endif
10743     ctx.fpu_enabled = msr_fp;
10744     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
10745         ctx.spe_enabled = msr_spe;
10746     else
10747         ctx.spe_enabled = 0;
10748     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
10749         ctx.altivec_enabled = msr_vr;
10750     else
10751         ctx.altivec_enabled = 0;
10752     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
10753         ctx.vsx_enabled = msr_vsx;
10754     } else {
10755         ctx.vsx_enabled = 0;
10756     }
10757     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
10758         ctx.singlestep_enabled = CPU_SINGLE_STEP;
10759     else
10760         ctx.singlestep_enabled = 0;
10761     if ((env->flags & POWERPC_FLAG_BE) && msr_be)
10762         ctx.singlestep_enabled |= CPU_BRANCH_STEP;
10763     if (unlikely(cs->singlestep_enabled)) {
10764         ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
10765     }
10766 #if defined (DO_SINGLE_STEP) && 0
10767     /* Single step trace mode */
10768     msr_se = 1;
10769 #endif
10770     num_insns = 0;
10771     max_insns = tb->cflags & CF_COUNT_MASK;
10772     if (max_insns == 0)
10773         max_insns = CF_COUNT_MASK;
10774
10775     gen_tb_start();
10776     /* Set env in case of segfault during code fetch */
10777     while (ctx.exception == POWERPC_EXCP_NONE
10778             && tcg_ctx.gen_opc_ptr < gen_opc_end) {
10779         if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
10780             QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
10781                 if (bp->pc == ctx.nip) {
10782                     gen_debug_exception(ctxp);
10783                     break;
10784                 }
10785             }
10786         }
10787         if (unlikely(search_pc)) {
10788             j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
10789             if (lj < j) {
10790                 lj++;
10791                 while (lj < j)
10792                     tcg_ctx.gen_opc_instr_start[lj++] = 0;
10793             }
10794             tcg_ctx.gen_opc_pc[lj] = ctx.nip;
10795             tcg_ctx.gen_opc_instr_start[lj] = 1;
10796             tcg_ctx.gen_opc_icount[lj] = num_insns;
10797         }
10798         LOG_DISAS("----------------\n");
10799         LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
10800                   ctx.nip, ctx.mem_idx, (int)msr_ir);
10801         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
10802             gen_io_start();
10803         if (unlikely(ctx.le_mode)) {
10804             ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
10805         } else {
10806             ctx.opcode = cpu_ldl_code(env, ctx.nip);
10807         }
10808         LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
10809                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
10810                     opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
10811         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
10812             tcg_gen_debug_insn_start(ctx.nip);
10813         }
10814         ctx.nip += 4;
10815         table = env->opcodes;
10816         num_insns++;
10817         handler = table[opc1(ctx.opcode)];
10818         if (is_indirect_opcode(handler)) {
10819             table = ind_table(handler);
10820             handler = table[opc2(ctx.opcode)];
10821             if (is_indirect_opcode(handler)) {
10822                 table = ind_table(handler);
10823                 handler = table[opc3(ctx.opcode)];
10824             }
10825         }
10826         /* Is opcode *REALLY* valid ? */
10827         if (unlikely(handler->handler == &gen_invalid)) {
10828             if (qemu_log_enabled()) {
10829                 qemu_log("invalid/unsupported opcode: "
10830                          "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
10831                          opc1(ctx.opcode), opc2(ctx.opcode),
10832                          opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
10833             }
10834         } else {
10835             uint32_t inval;
10836
10837             if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
10838                 inval = handler->inval2;
10839             } else {
10840                 inval = handler->inval1;
10841             }
10842
10843             if (unlikely((ctx.opcode & inval) != 0)) {
10844                 if (qemu_log_enabled()) {
10845                     qemu_log("invalid bits: %08x for opcode: "
10846                              "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
10847                              ctx.opcode & inval, opc1(ctx.opcode),
10848                              opc2(ctx.opcode), opc3(ctx.opcode),
10849                              ctx.opcode, ctx.nip - 4);
10850                 }
10851                 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
10852                 break;
10853             }
10854         }
10855         (*(handler->handler))(&ctx);
10856 #if defined(DO_PPC_STATISTICS)
10857         handler->count++;
10858 #endif
10859         /* Check trace mode exceptions */
10860         if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
10861                      (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
10862                      ctx.exception != POWERPC_SYSCALL &&
10863                      ctx.exception != POWERPC_EXCP_TRAP &&
10864                      ctx.exception != POWERPC_EXCP_BRANCH)) {
10865             gen_exception(ctxp, POWERPC_EXCP_TRACE);
10866         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
10867                             (cs->singlestep_enabled) ||
10868                             singlestep ||
10869                             num_insns >= max_insns)) {
10870             /* if we reach a page boundary or are single stepping, stop
10871              * generation
10872              */
10873             break;
10874         }
10875     }
10876     if (tb->cflags & CF_LAST_IO)
10877         gen_io_end();
10878     if (ctx.exception == POWERPC_EXCP_NONE) {
10879         gen_goto_tb(&ctx, 0, ctx.nip);
10880     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
10881         if (unlikely(cs->singlestep_enabled)) {
10882             gen_debug_exception(ctxp);
10883         }
10884         /* Generate the return instruction */
10885         tcg_gen_exit_tb(0);
10886     }
10887     gen_tb_end(tb, num_insns);
10888     *tcg_ctx.gen_opc_ptr = INDEX_op_end;
10889     if (unlikely(search_pc)) {
10890         j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
10891         lj++;
10892         while (lj <= j)
10893             tcg_ctx.gen_opc_instr_start[lj++] = 0;
10894     } else {
10895         tb->size = ctx.nip - pc_start;
10896         tb->icount = num_insns;
10897     }
10898 #if defined(DEBUG_DISAS)
10899     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
10900         int flags;
10901         flags = env->bfd_mach;
10902         flags |= ctx.le_mode << 16;
10903         qemu_log("IN: %s\n", lookup_symbol(pc_start));
10904         log_target_disas(env, pc_start, ctx.nip - pc_start, flags);
10905         qemu_log("\n");
10906     }
10907 #endif
10908 }
10909
10910 void gen_intermediate_code (CPUPPCState *env, struct TranslationBlock *tb)
10911 {
10912     gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, false);
10913 }
10914
10915 void gen_intermediate_code_pc (CPUPPCState *env, struct TranslationBlock *tb)
10916 {
10917     gen_intermediate_code_internal(ppc_env_get_cpu(env), tb, true);
10918 }
10919
10920 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb, int pc_pos)
10921 {
10922     env->nip = tcg_ctx.gen_opc_pc[pc_pos];
10923 }
This page took 0.620895 seconds and 4 git commands to generate.