]> Git Repo - qemu.git/blob - target-ppc/translate.c
ppc: tlbie, tlbia and tlbisync are HV only
[qemu.git] / target-ppc / translate.c
1 /*
2  *  PowerPC emulation for qemu: main translation routines.
3  *
4  *  Copyright (c) 2003-2007 Jocelyn Mayer
5  *  Copyright (C) 2011 Freescale Semiconductor, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
25 #include "tcg-op.h"
26 #include "qemu/host-utils.h"
27 #include "exec/cpu_ldst.h"
28
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
31
32 #include "trace-tcg.h"
33 #include "exec/log.h"
34
35
36 #define CPU_SINGLE_STEP 0x1
37 #define CPU_BRANCH_STEP 0x2
38 #define GDBSTUB_SINGLE_STEP 0x4
39
40 /* Include definitions for instructions classes and implementations flags */
41 //#define PPC_DEBUG_DISAS
42 //#define DO_PPC_STATISTICS
43
44 #ifdef PPC_DEBUG_DISAS
45 #  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
46 #else
47 #  define LOG_DISAS(...) do { } while (0)
48 #endif
49 /*****************************************************************************/
50 /* Code translation helpers                                                  */
51
52 /* global register indexes */
53 static TCGv_env cpu_env;
54 static char cpu_reg_names[10*3 + 22*4 /* GPR */
55     + 10*4 + 22*5 /* SPE GPRh */
56     + 10*4 + 22*5 /* FPR */
57     + 2*(10*6 + 22*7) /* AVRh, AVRl */
58     + 10*5 + 22*6 /* VSR */
59     + 8*5 /* CRF */];
60 static TCGv cpu_gpr[32];
61 static TCGv cpu_gprh[32];
62 static TCGv_i64 cpu_fpr[32];
63 static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64 static TCGv_i64 cpu_vsr[32];
65 static TCGv_i32 cpu_crf[8];
66 static TCGv cpu_nip;
67 static TCGv cpu_msr;
68 static TCGv cpu_ctr;
69 static TCGv cpu_lr;
70 #if defined(TARGET_PPC64)
71 static TCGv cpu_cfar;
72 #endif
73 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca;
74 static TCGv cpu_reserve;
75 static TCGv cpu_fpscr;
76 static TCGv_i32 cpu_access_type;
77
78 #include "exec/gen-icount.h"
79
80 void ppc_translate_init(void)
81 {
82     int i;
83     char* p;
84     size_t cpu_reg_names_size;
85     static int done_init = 0;
86
87     if (done_init)
88         return;
89
90     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
91
92     p = cpu_reg_names;
93     cpu_reg_names_size = sizeof(cpu_reg_names);
94
95     for (i = 0; i < 8; i++) {
96         snprintf(p, cpu_reg_names_size, "crf%d", i);
97         cpu_crf[i] = tcg_global_mem_new_i32(cpu_env,
98                                             offsetof(CPUPPCState, crf[i]), p);
99         p += 5;
100         cpu_reg_names_size -= 5;
101     }
102
103     for (i = 0; i < 32; i++) {
104         snprintf(p, cpu_reg_names_size, "r%d", i);
105         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
106                                         offsetof(CPUPPCState, gpr[i]), p);
107         p += (i < 10) ? 3 : 4;
108         cpu_reg_names_size -= (i < 10) ? 3 : 4;
109         snprintf(p, cpu_reg_names_size, "r%dH", i);
110         cpu_gprh[i] = tcg_global_mem_new(cpu_env,
111                                          offsetof(CPUPPCState, gprh[i]), p);
112         p += (i < 10) ? 4 : 5;
113         cpu_reg_names_size -= (i < 10) ? 4 : 5;
114
115         snprintf(p, cpu_reg_names_size, "fp%d", i);
116         cpu_fpr[i] = tcg_global_mem_new_i64(cpu_env,
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(cpu_env,
124                                              offsetof(CPUPPCState, avr[i].u64[0]), p);
125 #else
126         cpu_avrh[i] = tcg_global_mem_new_i64(cpu_env,
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(cpu_env,
135                                              offsetof(CPUPPCState, avr[i].u64[1]), p);
136 #else
137         cpu_avrl[i] = tcg_global_mem_new_i64(cpu_env,
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(cpu_env,
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(cpu_env,
150                                  offsetof(CPUPPCState, nip), "nip");
151
152     cpu_msr = tcg_global_mem_new(cpu_env,
153                                  offsetof(CPUPPCState, msr), "msr");
154
155     cpu_ctr = tcg_global_mem_new(cpu_env,
156                                  offsetof(CPUPPCState, ctr), "ctr");
157
158     cpu_lr = tcg_global_mem_new(cpu_env,
159                                 offsetof(CPUPPCState, lr), "lr");
160
161 #if defined(TARGET_PPC64)
162     cpu_cfar = tcg_global_mem_new(cpu_env,
163                                   offsetof(CPUPPCState, cfar), "cfar");
164 #endif
165
166     cpu_xer = tcg_global_mem_new(cpu_env,
167                                  offsetof(CPUPPCState, xer), "xer");
168     cpu_so = tcg_global_mem_new(cpu_env,
169                                 offsetof(CPUPPCState, so), "SO");
170     cpu_ov = tcg_global_mem_new(cpu_env,
171                                 offsetof(CPUPPCState, ov), "OV");
172     cpu_ca = tcg_global_mem_new(cpu_env,
173                                 offsetof(CPUPPCState, ca), "CA");
174
175     cpu_reserve = tcg_global_mem_new(cpu_env,
176                                      offsetof(CPUPPCState, reserve_addr),
177                                      "reserve_addr");
178
179     cpu_fpscr = tcg_global_mem_new(cpu_env,
180                                    offsetof(CPUPPCState, fpscr), "fpscr");
181
182     cpu_access_type = tcg_global_mem_new_i32(cpu_env,
183                                              offsetof(CPUPPCState, access_type), "access_type");
184
185     done_init = 1;
186 }
187
188 /* internal defines */
189 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     bool pr, hv;
196     int mem_idx;
197     int access_type;
198     /* Translation flags */
199     int le_mode;
200     TCGMemOp default_tcg_memop_mask;
201 #if defined(TARGET_PPC64)
202     int sf_mode;
203     int has_cfar;
204 #endif
205     int fpu_enabled;
206     int altivec_enabled;
207     int vsx_enabled;
208     int spe_enabled;
209     int tm_enabled;
210     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
211     int singlestep_enabled;
212     uint64_t insns_flags;
213     uint64_t insns_flags2;
214 };
215
216 /* Return true iff byteswap is needed in a scalar memop */
217 static inline bool need_byteswap(const DisasContext *ctx)
218 {
219 #if defined(TARGET_WORDS_BIGENDIAN)
220      return ctx->le_mode;
221 #else
222      return !ctx->le_mode;
223 #endif
224 }
225
226 /* True when active word size < size of target_long.  */
227 #ifdef TARGET_PPC64
228 # define NARROW_MODE(C)  (!(C)->sf_mode)
229 #else
230 # define NARROW_MODE(C)  0
231 #endif
232
233 struct opc_handler_t {
234     /* invalid bits for instruction 1 (Rc(opcode) == 0) */
235     uint32_t inval1;
236     /* invalid bits for instruction 2 (Rc(opcode) == 1) */
237     uint32_t inval2;
238     /* instruction type */
239     uint64_t type;
240     /* extended instruction type */
241     uint64_t type2;
242     /* handler */
243     void (*handler)(DisasContext *ctx);
244 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
245     const char *oname;
246 #endif
247 #if defined(DO_PPC_STATISTICS)
248     uint64_t count;
249 #endif
250 };
251
252 static inline void gen_reset_fpstatus(void)
253 {
254     gen_helper_reset_fpstatus(cpu_env);
255 }
256
257 static inline void gen_compute_fprf(TCGv_i64 arg)
258 {
259     gen_helper_compute_fprf(cpu_env, arg);
260     gen_helper_float_check_status(cpu_env);
261 }
262
263 static inline void gen_set_access_type(DisasContext *ctx, int access_type)
264 {
265     if (ctx->access_type != access_type) {
266         tcg_gen_movi_i32(cpu_access_type, access_type);
267         ctx->access_type = access_type;
268     }
269 }
270
271 static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
272 {
273     if (NARROW_MODE(ctx)) {
274         nip = (uint32_t)nip;
275     }
276     tcg_gen_movi_tl(cpu_nip, nip);
277 }
278
279 void gen_update_current_nip(void *opaque)
280 {
281     DisasContext *ctx = opaque;
282
283     tcg_gen_movi_tl(cpu_nip, ctx->nip);
284 }
285
286 static inline void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
287 {
288     TCGv_i32 t0, t1;
289     if (ctx->exception == POWERPC_EXCP_NONE) {
290         gen_update_nip(ctx, ctx->nip);
291     }
292     t0 = tcg_const_i32(excp);
293     t1 = tcg_const_i32(error);
294     gen_helper_raise_exception_err(cpu_env, t0, t1);
295     tcg_temp_free_i32(t0);
296     tcg_temp_free_i32(t1);
297     ctx->exception = (excp);
298 }
299
300 static inline void gen_exception(DisasContext *ctx, uint32_t excp)
301 {
302     TCGv_i32 t0;
303     if (ctx->exception == POWERPC_EXCP_NONE) {
304         gen_update_nip(ctx, ctx->nip);
305     }
306     t0 = tcg_const_i32(excp);
307     gen_helper_raise_exception(cpu_env, t0);
308     tcg_temp_free_i32(t0);
309     ctx->exception = (excp);
310 }
311
312 static inline void gen_debug_exception(DisasContext *ctx)
313 {
314     TCGv_i32 t0;
315
316     if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
317         (ctx->exception != POWERPC_EXCP_SYNC)) {
318         gen_update_nip(ctx, ctx->nip);
319     }
320     t0 = tcg_const_i32(EXCP_DEBUG);
321     gen_helper_raise_exception(cpu_env, t0);
322     tcg_temp_free_i32(t0);
323 }
324
325 static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
326 {
327     gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
328 }
329
330 /* Stop translation */
331 static inline void gen_stop_exception(DisasContext *ctx)
332 {
333     gen_update_nip(ctx, ctx->nip);
334     ctx->exception = POWERPC_EXCP_STOP;
335 }
336
337 #ifndef CONFIG_USER_ONLY
338 /* No need to update nip here, as execution flow will change */
339 static inline void gen_sync_exception(DisasContext *ctx)
340 {
341     ctx->exception = POWERPC_EXCP_SYNC;
342 }
343 #endif
344
345 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
346 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
347
348 #define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2)             \
349 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
350
351 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
352 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
353
354 #define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2)      \
355 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
356
357 typedef struct opcode_t {
358     unsigned char opc1, opc2, opc3;
359 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
360     unsigned char pad[5];
361 #else
362     unsigned char pad[1];
363 #endif
364     opc_handler_t handler;
365     const char *oname;
366 } opcode_t;
367
368 /*****************************************************************************/
369 /***                           Instruction decoding                        ***/
370 #define EXTRACT_HELPER(name, shift, nb)                                       \
371 static inline uint32_t name(uint32_t opcode)                                  \
372 {                                                                             \
373     return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
374 }
375
376 #define EXTRACT_SHELPER(name, shift, nb)                                      \
377 static inline int32_t name(uint32_t opcode)                                   \
378 {                                                                             \
379     return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
380 }
381
382 #define EXTRACT_HELPER_SPLIT(name, shift1, nb1, shift2, nb2)                  \
383 static inline uint32_t name(uint32_t opcode)                                  \
384 {                                                                             \
385     return (((opcode >> (shift1)) & ((1 << (nb1)) - 1)) << nb2) |             \
386             ((opcode >> (shift2)) & ((1 << (nb2)) - 1));                      \
387 }
388 /* Opcode part 1 */
389 EXTRACT_HELPER(opc1, 26, 6);
390 /* Opcode part 2 */
391 EXTRACT_HELPER(opc2, 1, 5);
392 /* Opcode part 3 */
393 EXTRACT_HELPER(opc3, 6, 5);
394 /* Update Cr0 flags */
395 EXTRACT_HELPER(Rc, 0, 1);
396 /* Update Cr6 flags (Altivec) */
397 EXTRACT_HELPER(Rc21, 10, 1);
398 /* Destination */
399 EXTRACT_HELPER(rD, 21, 5);
400 /* Source */
401 EXTRACT_HELPER(rS, 21, 5);
402 /* First operand */
403 EXTRACT_HELPER(rA, 16, 5);
404 /* Second operand */
405 EXTRACT_HELPER(rB, 11, 5);
406 /* Third operand */
407 EXTRACT_HELPER(rC, 6, 5);
408 /***                               Get CRn                                 ***/
409 EXTRACT_HELPER(crfD, 23, 3);
410 EXTRACT_HELPER(crfS, 18, 3);
411 EXTRACT_HELPER(crbD, 21, 5);
412 EXTRACT_HELPER(crbA, 16, 5);
413 EXTRACT_HELPER(crbB, 11, 5);
414 /* SPR / TBL */
415 EXTRACT_HELPER(_SPR, 11, 10);
416 static inline uint32_t SPR(uint32_t opcode)
417 {
418     uint32_t sprn = _SPR(opcode);
419
420     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
421 }
422 /***                              Get constants                            ***/
423 /* 16 bits signed immediate value */
424 EXTRACT_SHELPER(SIMM, 0, 16);
425 /* 16 bits unsigned immediate value */
426 EXTRACT_HELPER(UIMM, 0, 16);
427 /* 5 bits signed immediate value */
428 EXTRACT_HELPER(SIMM5, 16, 5);
429 /* 5 bits signed immediate value */
430 EXTRACT_HELPER(UIMM5, 16, 5);
431 /* Bit count */
432 EXTRACT_HELPER(NB, 11, 5);
433 /* Shift count */
434 EXTRACT_HELPER(SH, 11, 5);
435 /* Vector shift count */
436 EXTRACT_HELPER(VSH, 6, 4);
437 /* Mask start */
438 EXTRACT_HELPER(MB, 6, 5);
439 /* Mask end */
440 EXTRACT_HELPER(ME, 1, 5);
441 /* Trap operand */
442 EXTRACT_HELPER(TO, 21, 5);
443
444 EXTRACT_HELPER(CRM, 12, 8);
445
446 #ifndef CONFIG_USER_ONLY
447 EXTRACT_HELPER(SR, 16, 4);
448 #endif
449
450 /* mtfsf/mtfsfi */
451 EXTRACT_HELPER(FPBF, 23, 3);
452 EXTRACT_HELPER(FPIMM, 12, 4);
453 EXTRACT_HELPER(FPL, 25, 1);
454 EXTRACT_HELPER(FPFLM, 17, 8);
455 EXTRACT_HELPER(FPW, 16, 1);
456
457 /***                            Jump target decoding                       ***/
458 /* Immediate address */
459 static inline target_ulong LI(uint32_t opcode)
460 {
461     return (opcode >> 0) & 0x03FFFFFC;
462 }
463
464 static inline uint32_t BD(uint32_t opcode)
465 {
466     return (opcode >> 0) & 0xFFFC;
467 }
468
469 EXTRACT_HELPER(BO, 21, 5);
470 EXTRACT_HELPER(BI, 16, 5);
471 /* Absolute/relative address */
472 EXTRACT_HELPER(AA, 1, 1);
473 /* Link */
474 EXTRACT_HELPER(LK, 0, 1);
475
476 /* DFP Z22-form */
477 EXTRACT_HELPER(DCM, 10, 6)
478
479 /* DFP Z23-form */
480 EXTRACT_HELPER(RMC, 9, 2)
481
482 /* Create a mask between <start> and <end> bits */
483 static inline target_ulong MASK(uint32_t start, uint32_t end)
484 {
485     target_ulong ret;
486
487 #if defined(TARGET_PPC64)
488     if (likely(start == 0)) {
489         ret = UINT64_MAX << (63 - end);
490     } else if (likely(end == 63)) {
491         ret = UINT64_MAX >> start;
492     }
493 #else
494     if (likely(start == 0)) {
495         ret = UINT32_MAX << (31  - end);
496     } else if (likely(end == 31)) {
497         ret = UINT32_MAX >> start;
498     }
499 #endif
500     else {
501         ret = (((target_ulong)(-1ULL)) >> (start)) ^
502             (((target_ulong)(-1ULL) >> (end)) >> 1);
503         if (unlikely(start > end))
504             return ~ret;
505     }
506
507     return ret;
508 }
509
510 EXTRACT_HELPER_SPLIT(xT, 0, 1, 21, 5);
511 EXTRACT_HELPER_SPLIT(xS, 0, 1, 21, 5);
512 EXTRACT_HELPER_SPLIT(xA, 2, 1, 16, 5);
513 EXTRACT_HELPER_SPLIT(xB, 1, 1, 11, 5);
514 EXTRACT_HELPER_SPLIT(xC, 3, 1,  6, 5);
515 EXTRACT_HELPER(DM, 8, 2);
516 EXTRACT_HELPER(UIM, 16, 2);
517 EXTRACT_HELPER(SHW, 8, 2);
518 EXTRACT_HELPER(SP, 19, 2);
519 /*****************************************************************************/
520 /* PowerPC instructions table                                                */
521
522 #if defined(DO_PPC_STATISTICS)
523 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
524 {                                                                             \
525     .opc1 = op1,                                                              \
526     .opc2 = op2,                                                              \
527     .opc3 = op3,                                                              \
528     .pad  = { 0, },                                                           \
529     .handler = {                                                              \
530         .inval1  = invl,                                                      \
531         .type = _typ,                                                         \
532         .type2 = _typ2,                                                       \
533         .handler = &gen_##name,                                               \
534         .oname = stringify(name),                                             \
535     },                                                                        \
536     .oname = stringify(name),                                                 \
537 }
538 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
539 {                                                                             \
540     .opc1 = op1,                                                              \
541     .opc2 = op2,                                                              \
542     .opc3 = op3,                                                              \
543     .pad  = { 0, },                                                           \
544     .handler = {                                                              \
545         .inval1  = invl1,                                                     \
546         .inval2  = invl2,                                                     \
547         .type = _typ,                                                         \
548         .type2 = _typ2,                                                       \
549         .handler = &gen_##name,                                               \
550         .oname = stringify(name),                                             \
551     },                                                                        \
552     .oname = stringify(name),                                                 \
553 }
554 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
555 {                                                                             \
556     .opc1 = op1,                                                              \
557     .opc2 = op2,                                                              \
558     .opc3 = op3,                                                              \
559     .pad  = { 0, },                                                           \
560     .handler = {                                                              \
561         .inval1  = invl,                                                      \
562         .type = _typ,                                                         \
563         .type2 = _typ2,                                                       \
564         .handler = &gen_##name,                                               \
565         .oname = onam,                                                        \
566     },                                                                        \
567     .oname = onam,                                                            \
568 }
569 #else
570 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
571 {                                                                             \
572     .opc1 = op1,                                                              \
573     .opc2 = op2,                                                              \
574     .opc3 = op3,                                                              \
575     .pad  = { 0, },                                                           \
576     .handler = {                                                              \
577         .inval1  = invl,                                                      \
578         .type = _typ,                                                         \
579         .type2 = _typ2,                                                       \
580         .handler = &gen_##name,                                               \
581     },                                                                        \
582     .oname = stringify(name),                                                 \
583 }
584 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
585 {                                                                             \
586     .opc1 = op1,                                                              \
587     .opc2 = op2,                                                              \
588     .opc3 = op3,                                                              \
589     .pad  = { 0, },                                                           \
590     .handler = {                                                              \
591         .inval1  = invl1,                                                     \
592         .inval2  = invl2,                                                     \
593         .type = _typ,                                                         \
594         .type2 = _typ2,                                                       \
595         .handler = &gen_##name,                                               \
596     },                                                                        \
597     .oname = stringify(name),                                                 \
598 }
599 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
600 {                                                                             \
601     .opc1 = op1,                                                              \
602     .opc2 = op2,                                                              \
603     .opc3 = op3,                                                              \
604     .pad  = { 0, },                                                           \
605     .handler = {                                                              \
606         .inval1  = invl,                                                      \
607         .type = _typ,                                                         \
608         .type2 = _typ2,                                                       \
609         .handler = &gen_##name,                                               \
610     },                                                                        \
611     .oname = onam,                                                            \
612 }
613 #endif
614
615 /* SPR load/store helpers */
616 static inline void gen_load_spr(TCGv t, int reg)
617 {
618     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
619 }
620
621 static inline void gen_store_spr(int reg, TCGv t)
622 {
623     tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
624 }
625
626 /* Invalid instruction */
627 static void gen_invalid(DisasContext *ctx)
628 {
629     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
630 }
631
632 static opc_handler_t invalid_handler = {
633     .inval1  = 0xFFFFFFFF,
634     .inval2  = 0xFFFFFFFF,
635     .type    = PPC_NONE,
636     .type2   = PPC_NONE,
637     .handler = gen_invalid,
638 };
639
640 /***                           Integer comparison                          ***/
641
642 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
643 {
644     TCGv t0 = tcg_temp_new();
645     TCGv_i32 t1 = tcg_temp_new_i32();
646
647     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
648
649     tcg_gen_setcond_tl((s ? TCG_COND_LT: TCG_COND_LTU), t0, arg0, arg1);
650     tcg_gen_trunc_tl_i32(t1, t0);
651     tcg_gen_shli_i32(t1, t1, CRF_LT);
652     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
653
654     tcg_gen_setcond_tl((s ? TCG_COND_GT: TCG_COND_GTU), t0, arg0, arg1);
655     tcg_gen_trunc_tl_i32(t1, t0);
656     tcg_gen_shli_i32(t1, t1, CRF_GT);
657     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
658
659     tcg_gen_setcond_tl(TCG_COND_EQ, t0, arg0, arg1);
660     tcg_gen_trunc_tl_i32(t1, t0);
661     tcg_gen_shli_i32(t1, t1, CRF_EQ);
662     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t1);
663
664     tcg_temp_free(t0);
665     tcg_temp_free_i32(t1);
666 }
667
668 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
669 {
670     TCGv t0 = tcg_const_tl(arg1);
671     gen_op_cmp(arg0, t0, s, crf);
672     tcg_temp_free(t0);
673 }
674
675 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
676 {
677     TCGv t0, t1;
678     t0 = tcg_temp_new();
679     t1 = tcg_temp_new();
680     if (s) {
681         tcg_gen_ext32s_tl(t0, arg0);
682         tcg_gen_ext32s_tl(t1, arg1);
683     } else {
684         tcg_gen_ext32u_tl(t0, arg0);
685         tcg_gen_ext32u_tl(t1, arg1);
686     }
687     gen_op_cmp(t0, t1, s, crf);
688     tcg_temp_free(t1);
689     tcg_temp_free(t0);
690 }
691
692 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
693 {
694     TCGv t0 = tcg_const_tl(arg1);
695     gen_op_cmp32(arg0, t0, s, crf);
696     tcg_temp_free(t0);
697 }
698
699 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
700 {
701     if (NARROW_MODE(ctx)) {
702         gen_op_cmpi32(reg, 0, 1, 0);
703     } else {
704         gen_op_cmpi(reg, 0, 1, 0);
705     }
706 }
707
708 /* cmp */
709 static void gen_cmp(DisasContext *ctx)
710 {
711     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
712         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
713                    1, crfD(ctx->opcode));
714     } else {
715         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
716                      1, crfD(ctx->opcode));
717     }
718 }
719
720 /* cmpi */
721 static void gen_cmpi(DisasContext *ctx)
722 {
723     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
724         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
725                     1, crfD(ctx->opcode));
726     } else {
727         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
728                       1, crfD(ctx->opcode));
729     }
730 }
731
732 /* cmpl */
733 static void gen_cmpl(DisasContext *ctx)
734 {
735     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
736         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
737                    0, crfD(ctx->opcode));
738     } else {
739         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
740                      0, crfD(ctx->opcode));
741     }
742 }
743
744 /* cmpli */
745 static void gen_cmpli(DisasContext *ctx)
746 {
747     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
748         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
749                     0, crfD(ctx->opcode));
750     } else {
751         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
752                       0, crfD(ctx->opcode));
753     }
754 }
755
756 /* isel (PowerPC 2.03 specification) */
757 static void gen_isel(DisasContext *ctx)
758 {
759     uint32_t bi = rC(ctx->opcode);
760     uint32_t mask = 0x08 >> (bi & 0x03);
761     TCGv t0 = tcg_temp_new();
762     TCGv zr;
763
764     tcg_gen_extu_i32_tl(t0, cpu_crf[bi >> 2]);
765     tcg_gen_andi_tl(t0, t0, mask);
766
767     zr = tcg_const_tl(0);
768     tcg_gen_movcond_tl(TCG_COND_NE, cpu_gpr[rD(ctx->opcode)], t0, zr,
769                        rA(ctx->opcode) ? cpu_gpr[rA(ctx->opcode)] : zr,
770                        cpu_gpr[rB(ctx->opcode)]);
771     tcg_temp_free(zr);
772     tcg_temp_free(t0);
773 }
774
775 /* cmpb: PowerPC 2.05 specification */
776 static void gen_cmpb(DisasContext *ctx)
777 {
778     gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
779                     cpu_gpr[rB(ctx->opcode)]);
780 }
781
782 /***                           Integer arithmetic                          ***/
783
784 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
785                                            TCGv arg1, TCGv arg2, int sub)
786 {
787     TCGv t0 = tcg_temp_new();
788
789     tcg_gen_xor_tl(cpu_ov, arg0, arg2);
790     tcg_gen_xor_tl(t0, arg1, arg2);
791     if (sub) {
792         tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
793     } else {
794         tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
795     }
796     tcg_temp_free(t0);
797     if (NARROW_MODE(ctx)) {
798         tcg_gen_ext32s_tl(cpu_ov, cpu_ov);
799     }
800     tcg_gen_shri_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1);
801     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
802 }
803
804 /* Common add function */
805 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
806                                     TCGv arg2, bool add_ca, bool compute_ca,
807                                     bool compute_ov, bool compute_rc0)
808 {
809     TCGv t0 = ret;
810
811     if (compute_ca || compute_ov) {
812         t0 = tcg_temp_new();
813     }
814
815     if (compute_ca) {
816         if (NARROW_MODE(ctx)) {
817             /* Caution: a non-obvious corner case of the spec is that we
818                must produce the *entire* 64-bit addition, but produce the
819                carry into bit 32.  */
820             TCGv t1 = tcg_temp_new();
821             tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
822             tcg_gen_add_tl(t0, arg1, arg2);
823             if (add_ca) {
824                 tcg_gen_add_tl(t0, t0, cpu_ca);
825             }
826             tcg_gen_xor_tl(cpu_ca, t0, t1);        /* bits changed w/ carry */
827             tcg_temp_free(t1);
828             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);   /* extract bit 32 */
829             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
830         } else {
831             TCGv zero = tcg_const_tl(0);
832             if (add_ca) {
833                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, cpu_ca, zero);
834                 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, arg2, zero);
835             } else {
836                 tcg_gen_add2_tl(t0, cpu_ca, arg1, zero, arg2, zero);
837             }
838             tcg_temp_free(zero);
839         }
840     } else {
841         tcg_gen_add_tl(t0, arg1, arg2);
842         if (add_ca) {
843             tcg_gen_add_tl(t0, t0, cpu_ca);
844         }
845     }
846
847     if (compute_ov) {
848         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
849     }
850     if (unlikely(compute_rc0)) {
851         gen_set_Rc0(ctx, t0);
852     }
853
854     if (!TCGV_EQUAL(t0, ret)) {
855         tcg_gen_mov_tl(ret, t0);
856         tcg_temp_free(t0);
857     }
858 }
859 /* Add functions with two operands */
860 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
861 static void glue(gen_, name)(DisasContext *ctx)                               \
862 {                                                                             \
863     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
864                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
865                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
866 }
867 /* Add functions with one operand and one immediate */
868 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
869                                 add_ca, compute_ca, compute_ov)               \
870 static void glue(gen_, name)(DisasContext *ctx)                               \
871 {                                                                             \
872     TCGv t0 = tcg_const_tl(const_val);                                        \
873     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
874                      cpu_gpr[rA(ctx->opcode)], t0,                            \
875                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
876     tcg_temp_free(t0);                                                        \
877 }
878
879 /* add  add.  addo  addo. */
880 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
881 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
882 /* addc  addc.  addco  addco. */
883 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
884 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
885 /* adde  adde.  addeo  addeo. */
886 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
887 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
888 /* addme  addme.  addmeo  addmeo.  */
889 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
890 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
891 /* addze  addze.  addzeo  addzeo.*/
892 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
893 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
894 /* addi */
895 static void gen_addi(DisasContext *ctx)
896 {
897     target_long simm = SIMM(ctx->opcode);
898
899     if (rA(ctx->opcode) == 0) {
900         /* li case */
901         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
902     } else {
903         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
904                         cpu_gpr[rA(ctx->opcode)], simm);
905     }
906 }
907 /* addic  addic.*/
908 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
909 {
910     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
911     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
912                      c, 0, 1, 0, compute_rc0);
913     tcg_temp_free(c);
914 }
915
916 static void gen_addic(DisasContext *ctx)
917 {
918     gen_op_addic(ctx, 0);
919 }
920
921 static void gen_addic_(DisasContext *ctx)
922 {
923     gen_op_addic(ctx, 1);
924 }
925
926 /* addis */
927 static void gen_addis(DisasContext *ctx)
928 {
929     target_long simm = SIMM(ctx->opcode);
930
931     if (rA(ctx->opcode) == 0) {
932         /* lis case */
933         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
934     } else {
935         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
936                         cpu_gpr[rA(ctx->opcode)], simm << 16);
937     }
938 }
939
940 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
941                                      TCGv arg2, int sign, int compute_ov)
942 {
943     TCGLabel *l1 = gen_new_label();
944     TCGLabel *l2 = gen_new_label();
945     TCGv_i32 t0 = tcg_temp_local_new_i32();
946     TCGv_i32 t1 = tcg_temp_local_new_i32();
947
948     tcg_gen_trunc_tl_i32(t0, arg1);
949     tcg_gen_trunc_tl_i32(t1, arg2);
950     tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
951     if (sign) {
952         TCGLabel *l3 = gen_new_label();
953         tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
954         tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
955         gen_set_label(l3);
956         tcg_gen_div_i32(t0, t0, t1);
957     } else {
958         tcg_gen_divu_i32(t0, t0, t1);
959     }
960     if (compute_ov) {
961         tcg_gen_movi_tl(cpu_ov, 0);
962     }
963     tcg_gen_br(l2);
964     gen_set_label(l1);
965     if (sign) {
966         tcg_gen_sari_i32(t0, t0, 31);
967     } else {
968         tcg_gen_movi_i32(t0, 0);
969     }
970     if (compute_ov) {
971         tcg_gen_movi_tl(cpu_ov, 1);
972         tcg_gen_movi_tl(cpu_so, 1);
973     }
974     gen_set_label(l2);
975     tcg_gen_extu_i32_tl(ret, t0);
976     tcg_temp_free_i32(t0);
977     tcg_temp_free_i32(t1);
978     if (unlikely(Rc(ctx->opcode) != 0))
979         gen_set_Rc0(ctx, ret);
980 }
981 /* Div functions */
982 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
983 static void glue(gen_, name)(DisasContext *ctx)                                       \
984 {                                                                             \
985     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
986                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
987                      sign, compute_ov);                                       \
988 }
989 /* divwu  divwu.  divwuo  divwuo.   */
990 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
991 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
992 /* divw  divw.  divwo  divwo.   */
993 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
994 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
995
996 /* div[wd]eu[o][.] */
997 #define GEN_DIVE(name, hlpr, compute_ov)                                      \
998 static void gen_##name(DisasContext *ctx)                                     \
999 {                                                                             \
1000     TCGv_i32 t0 = tcg_const_i32(compute_ov);                                  \
1001     gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
1002                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1003     tcg_temp_free_i32(t0);                                                    \
1004     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
1005         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1006     }                                                                         \
1007 }
1008
1009 GEN_DIVE(divweu, divweu, 0);
1010 GEN_DIVE(divweuo, divweu, 1);
1011 GEN_DIVE(divwe, divwe, 0);
1012 GEN_DIVE(divweo, divwe, 1);
1013
1014 #if defined(TARGET_PPC64)
1015 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1016                                      TCGv arg2, int sign, int compute_ov)
1017 {
1018     TCGLabel *l1 = gen_new_label();
1019     TCGLabel *l2 = gen_new_label();
1020
1021     tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1022     if (sign) {
1023         TCGLabel *l3 = gen_new_label();
1024         tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1025         tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1026         gen_set_label(l3);
1027         tcg_gen_div_i64(ret, arg1, arg2);
1028     } else {
1029         tcg_gen_divu_i64(ret, arg1, arg2);
1030     }
1031     if (compute_ov) {
1032         tcg_gen_movi_tl(cpu_ov, 0);
1033     }
1034     tcg_gen_br(l2);
1035     gen_set_label(l1);
1036     if (sign) {
1037         tcg_gen_sari_i64(ret, arg1, 63);
1038     } else {
1039         tcg_gen_movi_i64(ret, 0);
1040     }
1041     if (compute_ov) {
1042         tcg_gen_movi_tl(cpu_ov, 1);
1043         tcg_gen_movi_tl(cpu_so, 1);
1044     }
1045     gen_set_label(l2);
1046     if (unlikely(Rc(ctx->opcode) != 0))
1047         gen_set_Rc0(ctx, ret);
1048 }
1049 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1050 static void glue(gen_, name)(DisasContext *ctx)                                       \
1051 {                                                                             \
1052     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1053                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1054                       sign, compute_ov);                                      \
1055 }
1056 /* divwu  divwu.  divwuo  divwuo.   */
1057 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1058 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1059 /* divw  divw.  divwo  divwo.   */
1060 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1061 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1062
1063 GEN_DIVE(divdeu, divdeu, 0);
1064 GEN_DIVE(divdeuo, divdeu, 1);
1065 GEN_DIVE(divde, divde, 0);
1066 GEN_DIVE(divdeo, divde, 1);
1067 #endif
1068
1069 /* mulhw  mulhw. */
1070 static void gen_mulhw(DisasContext *ctx)
1071 {
1072     TCGv_i32 t0 = tcg_temp_new_i32();
1073     TCGv_i32 t1 = tcg_temp_new_i32();
1074
1075     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1076     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1077     tcg_gen_muls2_i32(t0, t1, t0, t1);
1078     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1079     tcg_temp_free_i32(t0);
1080     tcg_temp_free_i32(t1);
1081     if (unlikely(Rc(ctx->opcode) != 0))
1082         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1083 }
1084
1085 /* mulhwu  mulhwu.  */
1086 static void gen_mulhwu(DisasContext *ctx)
1087 {
1088     TCGv_i32 t0 = tcg_temp_new_i32();
1089     TCGv_i32 t1 = tcg_temp_new_i32();
1090
1091     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1092     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1093     tcg_gen_mulu2_i32(t0, t1, t0, t1);
1094     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
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 /* mullw  mullw. */
1102 static void gen_mullw(DisasContext *ctx)
1103 {
1104 #if defined(TARGET_PPC64)
1105     TCGv_i64 t0, t1;
1106     t0 = tcg_temp_new_i64();
1107     t1 = tcg_temp_new_i64();
1108     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1109     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1110     tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1111     tcg_temp_free(t0);
1112     tcg_temp_free(t1);
1113 #else
1114     tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1115                     cpu_gpr[rB(ctx->opcode)]);
1116 #endif
1117     if (unlikely(Rc(ctx->opcode) != 0))
1118         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1119 }
1120
1121 /* mullwo  mullwo. */
1122 static void gen_mullwo(DisasContext *ctx)
1123 {
1124     TCGv_i32 t0 = tcg_temp_new_i32();
1125     TCGv_i32 t1 = tcg_temp_new_i32();
1126
1127     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1128     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1129     tcg_gen_muls2_i32(t0, t1, t0, t1);
1130 #if defined(TARGET_PPC64)
1131     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1132 #else
1133     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
1134 #endif
1135
1136     tcg_gen_sari_i32(t0, t0, 31);
1137     tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1138     tcg_gen_extu_i32_tl(cpu_ov, t0);
1139     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1140
1141     tcg_temp_free_i32(t0);
1142     tcg_temp_free_i32(t1);
1143     if (unlikely(Rc(ctx->opcode) != 0))
1144         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1145 }
1146
1147 /* mulli */
1148 static void gen_mulli(DisasContext *ctx)
1149 {
1150     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1151                     SIMM(ctx->opcode));
1152 }
1153
1154 #if defined(TARGET_PPC64)
1155 /* mulhd  mulhd. */
1156 static void gen_mulhd(DisasContext *ctx)
1157 {
1158     TCGv lo = tcg_temp_new();
1159     tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1160                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1161     tcg_temp_free(lo);
1162     if (unlikely(Rc(ctx->opcode) != 0)) {
1163         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1164     }
1165 }
1166
1167 /* mulhdu  mulhdu. */
1168 static void gen_mulhdu(DisasContext *ctx)
1169 {
1170     TCGv lo = tcg_temp_new();
1171     tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1172                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1173     tcg_temp_free(lo);
1174     if (unlikely(Rc(ctx->opcode) != 0)) {
1175         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1176     }
1177 }
1178
1179 /* mulld  mulld. */
1180 static void gen_mulld(DisasContext *ctx)
1181 {
1182     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1183                    cpu_gpr[rB(ctx->opcode)]);
1184     if (unlikely(Rc(ctx->opcode) != 0))
1185         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1186 }
1187
1188 /* mulldo  mulldo. */
1189 static void gen_mulldo(DisasContext *ctx)
1190 {
1191     TCGv_i64 t0 = tcg_temp_new_i64();
1192     TCGv_i64 t1 = tcg_temp_new_i64();
1193
1194     tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
1195                       cpu_gpr[rB(ctx->opcode)]);
1196     tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
1197
1198     tcg_gen_sari_i64(t0, t0, 63);
1199     tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
1200     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1201
1202     tcg_temp_free_i64(t0);
1203     tcg_temp_free_i64(t1);
1204
1205     if (unlikely(Rc(ctx->opcode) != 0)) {
1206         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1207     }
1208 }
1209 #endif
1210
1211 /* Common subf function */
1212 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1213                                      TCGv arg2, bool add_ca, bool compute_ca,
1214                                      bool compute_ov, bool compute_rc0)
1215 {
1216     TCGv t0 = ret;
1217
1218     if (compute_ca || compute_ov) {
1219         t0 = tcg_temp_new();
1220     }
1221
1222     if (compute_ca) {
1223         /* dest = ~arg1 + arg2 [+ ca].  */
1224         if (NARROW_MODE(ctx)) {
1225             /* Caution: a non-obvious corner case of the spec is that we
1226                must produce the *entire* 64-bit addition, but produce the
1227                carry into bit 32.  */
1228             TCGv inv1 = tcg_temp_new();
1229             TCGv t1 = tcg_temp_new();
1230             tcg_gen_not_tl(inv1, arg1);
1231             if (add_ca) {
1232                 tcg_gen_add_tl(t0, arg2, cpu_ca);
1233             } else {
1234                 tcg_gen_addi_tl(t0, arg2, 1);
1235             }
1236             tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1237             tcg_gen_add_tl(t0, t0, inv1);
1238             tcg_temp_free(inv1);
1239             tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1240             tcg_temp_free(t1);
1241             tcg_gen_shri_tl(cpu_ca, cpu_ca, 32);    /* extract bit 32 */
1242             tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
1243         } else if (add_ca) {
1244             TCGv zero, inv1 = tcg_temp_new();
1245             tcg_gen_not_tl(inv1, arg1);
1246             zero = tcg_const_tl(0);
1247             tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1248             tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1249             tcg_temp_free(zero);
1250             tcg_temp_free(inv1);
1251         } else {
1252             tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1253             tcg_gen_sub_tl(t0, arg2, arg1);
1254         }
1255     } else if (add_ca) {
1256         /* Since we're ignoring carry-out, we can simplify the
1257            standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.  */
1258         tcg_gen_sub_tl(t0, arg2, arg1);
1259         tcg_gen_add_tl(t0, t0, cpu_ca);
1260         tcg_gen_subi_tl(t0, t0, 1);
1261     } else {
1262         tcg_gen_sub_tl(t0, arg2, arg1);
1263     }
1264
1265     if (compute_ov) {
1266         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1267     }
1268     if (unlikely(compute_rc0)) {
1269         gen_set_Rc0(ctx, t0);
1270     }
1271
1272     if (!TCGV_EQUAL(t0, ret)) {
1273         tcg_gen_mov_tl(ret, t0);
1274         tcg_temp_free(t0);
1275     }
1276 }
1277 /* Sub functions with Two operands functions */
1278 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1279 static void glue(gen_, name)(DisasContext *ctx)                               \
1280 {                                                                             \
1281     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1282                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1283                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1284 }
1285 /* Sub functions with one operand and one immediate */
1286 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1287                                 add_ca, compute_ca, compute_ov)               \
1288 static void glue(gen_, name)(DisasContext *ctx)                               \
1289 {                                                                             \
1290     TCGv t0 = tcg_const_tl(const_val);                                        \
1291     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1292                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1293                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1294     tcg_temp_free(t0);                                                        \
1295 }
1296 /* subf  subf.  subfo  subfo. */
1297 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1298 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1299 /* subfc  subfc.  subfco  subfco. */
1300 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1301 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1302 /* subfe  subfe.  subfeo  subfo. */
1303 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1304 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1305 /* subfme  subfme.  subfmeo  subfmeo.  */
1306 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1307 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1308 /* subfze  subfze.  subfzeo  subfzeo.*/
1309 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1310 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1311
1312 /* subfic */
1313 static void gen_subfic(DisasContext *ctx)
1314 {
1315     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1316     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1317                       c, 0, 1, 0, 0);
1318     tcg_temp_free(c);
1319 }
1320
1321 /* neg neg. nego nego. */
1322 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1323 {
1324     TCGv zero = tcg_const_tl(0);
1325     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1326                       zero, 0, 0, compute_ov, Rc(ctx->opcode));
1327     tcg_temp_free(zero);
1328 }
1329
1330 static void gen_neg(DisasContext *ctx)
1331 {
1332     gen_op_arith_neg(ctx, 0);
1333 }
1334
1335 static void gen_nego(DisasContext *ctx)
1336 {
1337     gen_op_arith_neg(ctx, 1);
1338 }
1339
1340 /***                            Integer logical                            ***/
1341 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1342 static void glue(gen_, name)(DisasContext *ctx)                                       \
1343 {                                                                             \
1344     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1345        cpu_gpr[rB(ctx->opcode)]);                                             \
1346     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1347         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1348 }
1349
1350 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1351 static void glue(gen_, name)(DisasContext *ctx)                                       \
1352 {                                                                             \
1353     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1354     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1355         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1356 }
1357
1358 /* and & and. */
1359 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1360 /* andc & andc. */
1361 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1362
1363 /* andi. */
1364 static void gen_andi_(DisasContext *ctx)
1365 {
1366     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1367     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1368 }
1369
1370 /* andis. */
1371 static void gen_andis_(DisasContext *ctx)
1372 {
1373     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1374     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1375 }
1376
1377 /* cntlzw */
1378 static void gen_cntlzw(DisasContext *ctx)
1379 {
1380     gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1381     if (unlikely(Rc(ctx->opcode) != 0))
1382         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1383 }
1384 /* eqv & eqv. */
1385 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1386 /* extsb & extsb. */
1387 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1388 /* extsh & extsh. */
1389 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1390 /* nand & nand. */
1391 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1392 /* nor & nor. */
1393 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1394
1395 /* or & or. */
1396 static void gen_or(DisasContext *ctx)
1397 {
1398     int rs, ra, rb;
1399
1400     rs = rS(ctx->opcode);
1401     ra = rA(ctx->opcode);
1402     rb = rB(ctx->opcode);
1403     /* Optimisation for mr. ri case */
1404     if (rs != ra || rs != rb) {
1405         if (rs != rb)
1406             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1407         else
1408             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1409         if (unlikely(Rc(ctx->opcode) != 0))
1410             gen_set_Rc0(ctx, cpu_gpr[ra]);
1411     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1412         gen_set_Rc0(ctx, cpu_gpr[rs]);
1413 #if defined(TARGET_PPC64)
1414     } else {
1415         int prio = 0;
1416
1417         switch (rs) {
1418         case 1:
1419             /* Set process priority to low */
1420             prio = 2;
1421             break;
1422         case 6:
1423             /* Set process priority to medium-low */
1424             prio = 3;
1425             break;
1426         case 2:
1427             /* Set process priority to normal */
1428             prio = 4;
1429             break;
1430 #if !defined(CONFIG_USER_ONLY)
1431         case 31:
1432             if (!ctx->pr) {
1433                 /* Set process priority to very low */
1434                 prio = 1;
1435             }
1436             break;
1437         case 5:
1438             if (!ctx->pr) {
1439                 /* Set process priority to medium-hight */
1440                 prio = 5;
1441             }
1442             break;
1443         case 3:
1444             if (!ctx->pr) {
1445                 /* Set process priority to high */
1446                 prio = 6;
1447             }
1448             break;
1449         case 7:
1450             if (ctx->hv) {
1451                 /* Set process priority to very high */
1452                 prio = 7;
1453             }
1454             break;
1455 #endif
1456         default:
1457             /* nop */
1458             break;
1459         }
1460         if (prio) {
1461             TCGv t0 = tcg_temp_new();
1462             gen_load_spr(t0, SPR_PPR);
1463             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1464             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1465             gen_store_spr(SPR_PPR, t0);
1466             tcg_temp_free(t0);
1467         }
1468 #endif
1469     }
1470 }
1471 /* orc & orc. */
1472 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1473
1474 /* xor & xor. */
1475 static void gen_xor(DisasContext *ctx)
1476 {
1477     /* Optimisation for "set to zero" case */
1478     if (rS(ctx->opcode) != rB(ctx->opcode))
1479         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1480     else
1481         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1482     if (unlikely(Rc(ctx->opcode) != 0))
1483         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1484 }
1485
1486 /* ori */
1487 static void gen_ori(DisasContext *ctx)
1488 {
1489     target_ulong uimm = UIMM(ctx->opcode);
1490
1491     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1492         /* NOP */
1493         /* XXX: should handle special NOPs for POWER series */
1494         return;
1495     }
1496     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1497 }
1498
1499 /* oris */
1500 static void gen_oris(DisasContext *ctx)
1501 {
1502     target_ulong uimm = UIMM(ctx->opcode);
1503
1504     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1505         /* NOP */
1506         return;
1507     }
1508     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1509 }
1510
1511 /* xori */
1512 static void gen_xori(DisasContext *ctx)
1513 {
1514     target_ulong uimm = UIMM(ctx->opcode);
1515
1516     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1517         /* NOP */
1518         return;
1519     }
1520     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1521 }
1522
1523 /* xoris */
1524 static void gen_xoris(DisasContext *ctx)
1525 {
1526     target_ulong uimm = UIMM(ctx->opcode);
1527
1528     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1529         /* NOP */
1530         return;
1531     }
1532     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1533 }
1534
1535 /* popcntb : PowerPC 2.03 specification */
1536 static void gen_popcntb(DisasContext *ctx)
1537 {
1538     gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1539 }
1540
1541 static void gen_popcntw(DisasContext *ctx)
1542 {
1543     gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1544 }
1545
1546 #if defined(TARGET_PPC64)
1547 /* popcntd: PowerPC 2.06 specification */
1548 static void gen_popcntd(DisasContext *ctx)
1549 {
1550     gen_helper_popcntd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1551 }
1552 #endif
1553
1554 /* prtyw: PowerPC 2.05 specification */
1555 static void gen_prtyw(DisasContext *ctx)
1556 {
1557     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1558     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1559     TCGv t0 = tcg_temp_new();
1560     tcg_gen_shri_tl(t0, rs, 16);
1561     tcg_gen_xor_tl(ra, rs, t0);
1562     tcg_gen_shri_tl(t0, ra, 8);
1563     tcg_gen_xor_tl(ra, ra, t0);
1564     tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1565     tcg_temp_free(t0);
1566 }
1567
1568 #if defined(TARGET_PPC64)
1569 /* prtyd: PowerPC 2.05 specification */
1570 static void gen_prtyd(DisasContext *ctx)
1571 {
1572     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1573     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1574     TCGv t0 = tcg_temp_new();
1575     tcg_gen_shri_tl(t0, rs, 32);
1576     tcg_gen_xor_tl(ra, rs, t0);
1577     tcg_gen_shri_tl(t0, ra, 16);
1578     tcg_gen_xor_tl(ra, ra, t0);
1579     tcg_gen_shri_tl(t0, ra, 8);
1580     tcg_gen_xor_tl(ra, ra, t0);
1581     tcg_gen_andi_tl(ra, ra, 1);
1582     tcg_temp_free(t0);
1583 }
1584 #endif
1585
1586 #if defined(TARGET_PPC64)
1587 /* bpermd */
1588 static void gen_bpermd(DisasContext *ctx)
1589 {
1590     gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1591                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1592 }
1593 #endif
1594
1595 #if defined(TARGET_PPC64)
1596 /* extsw & extsw. */
1597 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1598
1599 /* cntlzd */
1600 static void gen_cntlzd(DisasContext *ctx)
1601 {
1602     gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1603     if (unlikely(Rc(ctx->opcode) != 0))
1604         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1605 }
1606 #endif
1607
1608 /***                             Integer rotate                            ***/
1609
1610 /* rlwimi & rlwimi. */
1611 static void gen_rlwimi(DisasContext *ctx)
1612 {
1613     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1614     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1615     uint32_t sh = SH(ctx->opcode);
1616     uint32_t mb = MB(ctx->opcode);
1617     uint32_t me = ME(ctx->opcode);
1618
1619     if (sh == (31-me) && mb <= me) {
1620         tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
1621     } else {
1622         target_ulong mask;
1623         TCGv_i32 t0;
1624         TCGv t1;
1625
1626 #if defined(TARGET_PPC64)
1627         mb += 32;
1628         me += 32;
1629 #endif
1630         mask = MASK(mb, me);
1631
1632         t0 = tcg_temp_new_i32();
1633         t1 = tcg_temp_new();
1634         tcg_gen_trunc_tl_i32(t0, t_rs);
1635         tcg_gen_rotli_i32(t0, t0, sh);
1636         tcg_gen_extu_i32_tl(t1, t0);
1637         tcg_temp_free_i32(t0);
1638
1639         tcg_gen_andi_tl(t1, t1, mask);
1640         tcg_gen_andi_tl(t_ra, t_ra, ~mask);
1641         tcg_gen_or_tl(t_ra, t_ra, t1);
1642         tcg_temp_free(t1);
1643     }
1644     if (unlikely(Rc(ctx->opcode) != 0)) {
1645         gen_set_Rc0(ctx, t_ra);
1646     }
1647 }
1648
1649 /* rlwinm & rlwinm. */
1650 static void gen_rlwinm(DisasContext *ctx)
1651 {
1652     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1653     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1654     uint32_t sh = SH(ctx->opcode);
1655     uint32_t mb = MB(ctx->opcode);
1656     uint32_t me = ME(ctx->opcode);
1657
1658     if (mb == 0 && me == (31 - sh)) {
1659         tcg_gen_shli_tl(t_ra, t_rs, sh);
1660         tcg_gen_ext32u_tl(t_ra, t_ra);
1661     } else if (sh != 0 && me == 31 && sh == (32 - mb)) {
1662         tcg_gen_ext32u_tl(t_ra, t_rs);
1663         tcg_gen_shri_tl(t_ra, t_ra, mb);
1664     } else {
1665 #if defined(TARGET_PPC64)
1666         mb += 32;
1667         me += 32;
1668 #endif
1669         if (sh == 0) {
1670             tcg_gen_andi_tl(t_ra, t_rs, MASK(mb, me));
1671         } else {
1672             TCGv_i32 t0 = tcg_temp_new_i32();
1673
1674             tcg_gen_trunc_tl_i32(t0, t_rs);
1675             tcg_gen_rotli_i32(t0, t0, sh);
1676             tcg_gen_andi_i32(t0, t0, MASK(mb, me));
1677             tcg_gen_extu_i32_tl(t_ra, t0);
1678             tcg_temp_free_i32(t0);
1679         }
1680     }
1681     if (unlikely(Rc(ctx->opcode) != 0)) {
1682         gen_set_Rc0(ctx, t_ra);
1683     }
1684 }
1685
1686 /* rlwnm & rlwnm. */
1687 static void gen_rlwnm(DisasContext *ctx)
1688 {
1689     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1690     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1691     TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
1692     uint32_t mb = MB(ctx->opcode);
1693     uint32_t me = ME(ctx->opcode);
1694     TCGv_i32 t0, t1;
1695
1696 #if defined(TARGET_PPC64)
1697     mb += 32;
1698     me += 32;
1699 #endif
1700
1701     t0 = tcg_temp_new_i32();
1702     t1 = tcg_temp_new_i32();
1703     tcg_gen_trunc_tl_i32(t0, t_rb);
1704     tcg_gen_trunc_tl_i32(t1, t_rs);
1705     tcg_gen_andi_i32(t0, t0, 0x1f);
1706     tcg_gen_rotl_i32(t1, t1, t0);
1707     tcg_temp_free_i32(t0);
1708
1709     tcg_gen_andi_i32(t1, t1, MASK(mb, me));
1710     tcg_gen_extu_i32_tl(t_ra, t1);
1711     tcg_temp_free_i32(t1);
1712
1713     if (unlikely(Rc(ctx->opcode) != 0)) {
1714         gen_set_Rc0(ctx, t_ra);
1715     }
1716 }
1717
1718 #if defined(TARGET_PPC64)
1719 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
1720 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1721 {                                                                             \
1722     gen_##name(ctx, 0);                                                       \
1723 }                                                                             \
1724                                                                               \
1725 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1726 {                                                                             \
1727     gen_##name(ctx, 1);                                                       \
1728 }
1729 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
1730 static void glue(gen_, name##0)(DisasContext *ctx)                            \
1731 {                                                                             \
1732     gen_##name(ctx, 0, 0);                                                    \
1733 }                                                                             \
1734                                                                               \
1735 static void glue(gen_, name##1)(DisasContext *ctx)                            \
1736 {                                                                             \
1737     gen_##name(ctx, 0, 1);                                                    \
1738 }                                                                             \
1739                                                                               \
1740 static void glue(gen_, name##2)(DisasContext *ctx)                            \
1741 {                                                                             \
1742     gen_##name(ctx, 1, 0);                                                    \
1743 }                                                                             \
1744                                                                               \
1745 static void glue(gen_, name##3)(DisasContext *ctx)                            \
1746 {                                                                             \
1747     gen_##name(ctx, 1, 1);                                                    \
1748 }
1749
1750 static void gen_rldinm(DisasContext *ctx, int mb, int me, int sh)
1751 {
1752     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1753     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1754
1755     if (sh != 0 && mb == 0 && me == (63 - sh)) {
1756         tcg_gen_shli_tl(t_ra, t_rs, sh);
1757     } else if (sh != 0 && me == 63 && sh == (64 - mb)) {
1758         tcg_gen_shri_tl(t_ra, t_rs, mb);
1759     } else {
1760         tcg_gen_rotli_tl(t_ra, t_rs, sh);
1761         tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
1762     }
1763     if (unlikely(Rc(ctx->opcode) != 0)) {
1764         gen_set_Rc0(ctx, t_ra);
1765     }
1766 }
1767
1768 /* rldicl - rldicl. */
1769 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
1770 {
1771     uint32_t sh, mb;
1772
1773     sh = SH(ctx->opcode) | (shn << 5);
1774     mb = MB(ctx->opcode) | (mbn << 5);
1775     gen_rldinm(ctx, mb, 63, sh);
1776 }
1777 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1778
1779 /* rldicr - rldicr. */
1780 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
1781 {
1782     uint32_t sh, me;
1783
1784     sh = SH(ctx->opcode) | (shn << 5);
1785     me = MB(ctx->opcode) | (men << 5);
1786     gen_rldinm(ctx, 0, me, sh);
1787 }
1788 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1789
1790 /* rldic - rldic. */
1791 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
1792 {
1793     uint32_t sh, mb;
1794
1795     sh = SH(ctx->opcode) | (shn << 5);
1796     mb = MB(ctx->opcode) | (mbn << 5);
1797     gen_rldinm(ctx, mb, 63 - sh, sh);
1798 }
1799 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1800
1801 static void gen_rldnm(DisasContext *ctx, int mb, int me)
1802 {
1803     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1804     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1805     TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
1806     TCGv t0;
1807
1808     t0 = tcg_temp_new();
1809     tcg_gen_andi_tl(t0, t_rb, 0x3f);
1810     tcg_gen_rotl_tl(t_ra, t_rs, t0);
1811     tcg_temp_free(t0);
1812
1813     tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
1814     if (unlikely(Rc(ctx->opcode) != 0)) {
1815         gen_set_Rc0(ctx, t_ra);
1816     }
1817 }
1818
1819 /* rldcl - rldcl. */
1820 static inline void gen_rldcl(DisasContext *ctx, int mbn)
1821 {
1822     uint32_t mb;
1823
1824     mb = MB(ctx->opcode) | (mbn << 5);
1825     gen_rldnm(ctx, mb, 63);
1826 }
1827 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1828
1829 /* rldcr - rldcr. */
1830 static inline void gen_rldcr(DisasContext *ctx, int men)
1831 {
1832     uint32_t me;
1833
1834     me = MB(ctx->opcode) | (men << 5);
1835     gen_rldnm(ctx, 0, me);
1836 }
1837 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1838
1839 /* rldimi - rldimi. */
1840 static void gen_rldimi(DisasContext *ctx, int mbn, int shn)
1841 {
1842     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1843     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1844     uint32_t sh = SH(ctx->opcode) | (shn << 5);
1845     uint32_t mb = MB(ctx->opcode) | (mbn << 5);
1846     uint32_t me = 63 - sh;
1847
1848     if (mb <= me) {
1849         tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
1850     } else {
1851         target_ulong mask = MASK(mb, me);
1852         TCGv t1 = tcg_temp_new();
1853
1854         tcg_gen_rotli_tl(t1, t_rs, sh);
1855         tcg_gen_andi_tl(t1, t1, mask);
1856         tcg_gen_andi_tl(t_ra, t_ra, ~mask);
1857         tcg_gen_or_tl(t_ra, t_ra, t1);
1858         tcg_temp_free(t1);
1859     }
1860     if (unlikely(Rc(ctx->opcode) != 0)) {
1861         gen_set_Rc0(ctx, t_ra);
1862     }
1863 }
1864 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1865 #endif
1866
1867 /***                             Integer shift                             ***/
1868
1869 /* slw & slw. */
1870 static void gen_slw(DisasContext *ctx)
1871 {
1872     TCGv t0, t1;
1873
1874     t0 = tcg_temp_new();
1875     /* AND rS with a mask that is 0 when rB >= 0x20 */
1876 #if defined(TARGET_PPC64)
1877     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1878     tcg_gen_sari_tl(t0, t0, 0x3f);
1879 #else
1880     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1881     tcg_gen_sari_tl(t0, t0, 0x1f);
1882 #endif
1883     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1884     t1 = tcg_temp_new();
1885     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1886     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1887     tcg_temp_free(t1);
1888     tcg_temp_free(t0);
1889     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1890     if (unlikely(Rc(ctx->opcode) != 0))
1891         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1892 }
1893
1894 /* sraw & sraw. */
1895 static void gen_sraw(DisasContext *ctx)
1896 {
1897     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
1898                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1899     if (unlikely(Rc(ctx->opcode) != 0))
1900         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1901 }
1902
1903 /* srawi & srawi. */
1904 static void gen_srawi(DisasContext *ctx)
1905 {
1906     int sh = SH(ctx->opcode);
1907     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1908     TCGv src = cpu_gpr[rS(ctx->opcode)];
1909     if (sh == 0) {
1910         tcg_gen_ext32s_tl(dst, src);
1911         tcg_gen_movi_tl(cpu_ca, 0);
1912     } else {
1913         TCGv t0;
1914         tcg_gen_ext32s_tl(dst, src);
1915         tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
1916         t0 = tcg_temp_new();
1917         tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
1918         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1919         tcg_temp_free(t0);
1920         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1921         tcg_gen_sari_tl(dst, dst, sh);
1922     }
1923     if (unlikely(Rc(ctx->opcode) != 0)) {
1924         gen_set_Rc0(ctx, dst);
1925     }
1926 }
1927
1928 /* srw & srw. */
1929 static void gen_srw(DisasContext *ctx)
1930 {
1931     TCGv t0, t1;
1932
1933     t0 = tcg_temp_new();
1934     /* AND rS with a mask that is 0 when rB >= 0x20 */
1935 #if defined(TARGET_PPC64)
1936     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
1937     tcg_gen_sari_tl(t0, t0, 0x3f);
1938 #else
1939     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
1940     tcg_gen_sari_tl(t0, t0, 0x1f);
1941 #endif
1942     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1943     tcg_gen_ext32u_tl(t0, t0);
1944     t1 = tcg_temp_new();
1945     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
1946     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1947     tcg_temp_free(t1);
1948     tcg_temp_free(t0);
1949     if (unlikely(Rc(ctx->opcode) != 0))
1950         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1951 }
1952
1953 #if defined(TARGET_PPC64)
1954 /* sld & sld. */
1955 static void gen_sld(DisasContext *ctx)
1956 {
1957     TCGv t0, t1;
1958
1959     t0 = tcg_temp_new();
1960     /* AND rS with a mask that is 0 when rB >= 0x40 */
1961     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
1962     tcg_gen_sari_tl(t0, t0, 0x3f);
1963     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1964     t1 = tcg_temp_new();
1965     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
1966     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1967     tcg_temp_free(t1);
1968     tcg_temp_free(t0);
1969     if (unlikely(Rc(ctx->opcode) != 0))
1970         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1971 }
1972
1973 /* srad & srad. */
1974 static void gen_srad(DisasContext *ctx)
1975 {
1976     gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
1977                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1978     if (unlikely(Rc(ctx->opcode) != 0))
1979         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1980 }
1981 /* sradi & sradi. */
1982 static inline void gen_sradi(DisasContext *ctx, int n)
1983 {
1984     int sh = SH(ctx->opcode) + (n << 5);
1985     TCGv dst = cpu_gpr[rA(ctx->opcode)];
1986     TCGv src = cpu_gpr[rS(ctx->opcode)];
1987     if (sh == 0) {
1988         tcg_gen_mov_tl(dst, src);
1989         tcg_gen_movi_tl(cpu_ca, 0);
1990     } else {
1991         TCGv t0;
1992         tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
1993         t0 = tcg_temp_new();
1994         tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
1995         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
1996         tcg_temp_free(t0);
1997         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
1998         tcg_gen_sari_tl(dst, src, sh);
1999     }
2000     if (unlikely(Rc(ctx->opcode) != 0)) {
2001         gen_set_Rc0(ctx, dst);
2002     }
2003 }
2004
2005 static void gen_sradi0(DisasContext *ctx)
2006 {
2007     gen_sradi(ctx, 0);
2008 }
2009
2010 static void gen_sradi1(DisasContext *ctx)
2011 {
2012     gen_sradi(ctx, 1);
2013 }
2014
2015 /* srd & srd. */
2016 static void gen_srd(DisasContext *ctx)
2017 {
2018     TCGv t0, t1;
2019
2020     t0 = tcg_temp_new();
2021     /* AND rS with a mask that is 0 when rB >= 0x40 */
2022     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2023     tcg_gen_sari_tl(t0, t0, 0x3f);
2024     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2025     t1 = tcg_temp_new();
2026     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2027     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2028     tcg_temp_free(t1);
2029     tcg_temp_free(t0);
2030     if (unlikely(Rc(ctx->opcode) != 0))
2031         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2032 }
2033 #endif
2034
2035 #if defined(TARGET_PPC64)
2036 static void gen_set_cr1_from_fpscr(DisasContext *ctx)
2037 {
2038     TCGv_i32 tmp = tcg_temp_new_i32();
2039     tcg_gen_trunc_tl_i32(tmp, cpu_fpscr);
2040     tcg_gen_shri_i32(cpu_crf[1], tmp, 28);
2041     tcg_temp_free_i32(tmp);
2042 }
2043 #else
2044 static void gen_set_cr1_from_fpscr(DisasContext *ctx)
2045 {
2046     tcg_gen_shri_tl(cpu_crf[1], cpu_fpscr, 28);
2047 }
2048 #endif
2049
2050 /***                       Floating-Point arithmetic                       ***/
2051 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2052 static void gen_f##name(DisasContext *ctx)                                    \
2053 {                                                                             \
2054     if (unlikely(!ctx->fpu_enabled)) {                                        \
2055         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2056         return;                                                               \
2057     }                                                                         \
2058     /* NIP cannot be restored if the memory exception comes from an helper */ \
2059     gen_update_nip(ctx, ctx->nip - 4);                                        \
2060     gen_reset_fpstatus();                                                     \
2061     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2062                      cpu_fpr[rA(ctx->opcode)],                                \
2063                      cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2064     if (isfloat) {                                                            \
2065         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2066                         cpu_fpr[rD(ctx->opcode)]);                            \
2067     }                                                                         \
2068     if (set_fprf) {                                                           \
2069         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2070     }                                                                         \
2071     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2072         gen_set_cr1_from_fpscr(ctx);                                          \
2073     }                                                                         \
2074 }
2075
2076 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2077 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2078 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2079
2080 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2081 static void gen_f##name(DisasContext *ctx)                                    \
2082 {                                                                             \
2083     if (unlikely(!ctx->fpu_enabled)) {                                        \
2084         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2085         return;                                                               \
2086     }                                                                         \
2087     /* NIP cannot be restored if the memory exception comes from an helper */ \
2088     gen_update_nip(ctx, ctx->nip - 4);                                        \
2089     gen_reset_fpstatus();                                                     \
2090     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2091                      cpu_fpr[rA(ctx->opcode)],                                \
2092                      cpu_fpr[rB(ctx->opcode)]);                               \
2093     if (isfloat) {                                                            \
2094         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2095                         cpu_fpr[rD(ctx->opcode)]);                            \
2096     }                                                                         \
2097     if (set_fprf) {                                                           \
2098         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2099     }                                                                         \
2100     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2101         gen_set_cr1_from_fpscr(ctx);                                          \
2102     }                                                                         \
2103 }
2104 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2105 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2106 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2107
2108 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2109 static void gen_f##name(DisasContext *ctx)                                    \
2110 {                                                                             \
2111     if (unlikely(!ctx->fpu_enabled)) {                                        \
2112         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2113         return;                                                               \
2114     }                                                                         \
2115     /* NIP cannot be restored if the memory exception comes from an helper */ \
2116     gen_update_nip(ctx, ctx->nip - 4);                                        \
2117     gen_reset_fpstatus();                                                     \
2118     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_env,                       \
2119                      cpu_fpr[rA(ctx->opcode)],                                \
2120                      cpu_fpr[rC(ctx->opcode)]);                               \
2121     if (isfloat) {                                                            \
2122         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,                    \
2123                         cpu_fpr[rD(ctx->opcode)]);                            \
2124     }                                                                         \
2125     if (set_fprf) {                                                           \
2126         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2127     }                                                                         \
2128     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2129         gen_set_cr1_from_fpscr(ctx);                                          \
2130     }                                                                         \
2131 }
2132 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2133 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2134 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2135
2136 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2137 static void gen_f##name(DisasContext *ctx)                                    \
2138 {                                                                             \
2139     if (unlikely(!ctx->fpu_enabled)) {                                        \
2140         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2141         return;                                                               \
2142     }                                                                         \
2143     /* NIP cannot be restored if the memory exception comes from an helper */ \
2144     gen_update_nip(ctx, ctx->nip - 4);                                        \
2145     gen_reset_fpstatus();                                                     \
2146     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2147                        cpu_fpr[rB(ctx->opcode)]);                             \
2148     if (set_fprf) {                                                           \
2149         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2150     }                                                                         \
2151     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2152         gen_set_cr1_from_fpscr(ctx);                                          \
2153     }                                                                         \
2154 }
2155
2156 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2157 static void gen_f##name(DisasContext *ctx)                                    \
2158 {                                                                             \
2159     if (unlikely(!ctx->fpu_enabled)) {                                        \
2160         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2161         return;                                                               \
2162     }                                                                         \
2163     /* NIP cannot be restored if the memory exception comes from an helper */ \
2164     gen_update_nip(ctx, ctx->nip - 4);                                        \
2165     gen_reset_fpstatus();                                                     \
2166     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_env,                     \
2167                        cpu_fpr[rB(ctx->opcode)]);                             \
2168     if (set_fprf) {                                                           \
2169         gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);                           \
2170     }                                                                         \
2171     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
2172         gen_set_cr1_from_fpscr(ctx);                                          \
2173     }                                                                         \
2174 }
2175
2176 /* fadd - fadds */
2177 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2178 /* fdiv - fdivs */
2179 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2180 /* fmul - fmuls */
2181 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2182
2183 /* fre */
2184 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2185
2186 /* fres */
2187 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2188
2189 /* frsqrte */
2190 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2191
2192 /* frsqrtes */
2193 static void gen_frsqrtes(DisasContext *ctx)
2194 {
2195     if (unlikely(!ctx->fpu_enabled)) {
2196         gen_exception(ctx, POWERPC_EXCP_FPU);
2197         return;
2198     }
2199     /* NIP cannot be restored if the memory exception comes from an helper */
2200     gen_update_nip(ctx, ctx->nip - 4);
2201     gen_reset_fpstatus();
2202     gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_env,
2203                        cpu_fpr[rB(ctx->opcode)]);
2204     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2205                     cpu_fpr[rD(ctx->opcode)]);
2206     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);
2207     if (unlikely(Rc(ctx->opcode) != 0)) {
2208         gen_set_cr1_from_fpscr(ctx);
2209     }
2210 }
2211
2212 /* fsel */
2213 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2214 /* fsub - fsubs */
2215 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2216 /* Optional: */
2217
2218 /* fsqrt */
2219 static void gen_fsqrt(DisasContext *ctx)
2220 {
2221     if (unlikely(!ctx->fpu_enabled)) {
2222         gen_exception(ctx, POWERPC_EXCP_FPU);
2223         return;
2224     }
2225     /* NIP cannot be restored if the memory exception comes from an helper */
2226     gen_update_nip(ctx, ctx->nip - 4);
2227     gen_reset_fpstatus();
2228     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2229                      cpu_fpr[rB(ctx->opcode)]);
2230     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);
2231     if (unlikely(Rc(ctx->opcode) != 0)) {
2232         gen_set_cr1_from_fpscr(ctx);
2233     }
2234 }
2235
2236 static void gen_fsqrts(DisasContext *ctx)
2237 {
2238     if (unlikely(!ctx->fpu_enabled)) {
2239         gen_exception(ctx, POWERPC_EXCP_FPU);
2240         return;
2241     }
2242     /* NIP cannot be restored if the memory exception comes from an helper */
2243     gen_update_nip(ctx, ctx->nip - 4);
2244     gen_reset_fpstatus();
2245     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_env,
2246                      cpu_fpr[rB(ctx->opcode)]);
2247     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_env,
2248                     cpu_fpr[rD(ctx->opcode)]);
2249     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)]);
2250     if (unlikely(Rc(ctx->opcode) != 0)) {
2251         gen_set_cr1_from_fpscr(ctx);
2252     }
2253 }
2254
2255 /***                     Floating-Point multiply-and-add                   ***/
2256 /* fmadd - fmadds */
2257 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2258 /* fmsub - fmsubs */
2259 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2260 /* fnmadd - fnmadds */
2261 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2262 /* fnmsub - fnmsubs */
2263 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2264
2265 /***                     Floating-Point round & convert                    ***/
2266 /* fctiw */
2267 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2268 /* fctiwu */
2269 GEN_FLOAT_B(ctiwu, 0x0E, 0x04, 0, PPC2_FP_CVT_ISA206);
2270 /* fctiwz */
2271 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2272 /* fctiwuz */
2273 GEN_FLOAT_B(ctiwuz, 0x0F, 0x04, 0, PPC2_FP_CVT_ISA206);
2274 /* frsp */
2275 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2276 /* fcfid */
2277 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC2_FP_CVT_S64);
2278 /* fcfids */
2279 GEN_FLOAT_B(cfids, 0x0E, 0x1A, 0, PPC2_FP_CVT_ISA206);
2280 /* fcfidu */
2281 GEN_FLOAT_B(cfidu, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2282 /* fcfidus */
2283 GEN_FLOAT_B(cfidus, 0x0E, 0x1E, 0, PPC2_FP_CVT_ISA206);
2284 /* fctid */
2285 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC2_FP_CVT_S64);
2286 /* fctidu */
2287 GEN_FLOAT_B(ctidu, 0x0E, 0x1D, 0, PPC2_FP_CVT_ISA206);
2288 /* fctidz */
2289 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC2_FP_CVT_S64);
2290 /* fctidu */
2291 GEN_FLOAT_B(ctiduz, 0x0F, 0x1D, 0, PPC2_FP_CVT_ISA206);
2292
2293 /* frin */
2294 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2295 /* friz */
2296 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2297 /* frip */
2298 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2299 /* frim */
2300 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2301
2302 static void gen_ftdiv(DisasContext *ctx)
2303 {
2304     if (unlikely(!ctx->fpu_enabled)) {
2305         gen_exception(ctx, POWERPC_EXCP_FPU);
2306         return;
2307     }
2308     gen_helper_ftdiv(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2309                      cpu_fpr[rB(ctx->opcode)]);
2310 }
2311
2312 static void gen_ftsqrt(DisasContext *ctx)
2313 {
2314     if (unlikely(!ctx->fpu_enabled)) {
2315         gen_exception(ctx, POWERPC_EXCP_FPU);
2316         return;
2317     }
2318     gen_helper_ftsqrt(cpu_crf[crfD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2319 }
2320
2321
2322
2323 /***                         Floating-Point compare                        ***/
2324
2325 /* fcmpo */
2326 static void gen_fcmpo(DisasContext *ctx)
2327 {
2328     TCGv_i32 crf;
2329     if (unlikely(!ctx->fpu_enabled)) {
2330         gen_exception(ctx, POWERPC_EXCP_FPU);
2331         return;
2332     }
2333     /* NIP cannot be restored if the memory exception comes from an helper */
2334     gen_update_nip(ctx, ctx->nip - 4);
2335     gen_reset_fpstatus();
2336     crf = tcg_const_i32(crfD(ctx->opcode));
2337     gen_helper_fcmpo(cpu_env, cpu_fpr[rA(ctx->opcode)],
2338                      cpu_fpr[rB(ctx->opcode)], crf);
2339     tcg_temp_free_i32(crf);
2340     gen_helper_float_check_status(cpu_env);
2341 }
2342
2343 /* fcmpu */
2344 static void gen_fcmpu(DisasContext *ctx)
2345 {
2346     TCGv_i32 crf;
2347     if (unlikely(!ctx->fpu_enabled)) {
2348         gen_exception(ctx, POWERPC_EXCP_FPU);
2349         return;
2350     }
2351     /* NIP cannot be restored if the memory exception comes from an helper */
2352     gen_update_nip(ctx, ctx->nip - 4);
2353     gen_reset_fpstatus();
2354     crf = tcg_const_i32(crfD(ctx->opcode));
2355     gen_helper_fcmpu(cpu_env, cpu_fpr[rA(ctx->opcode)],
2356                      cpu_fpr[rB(ctx->opcode)], crf);
2357     tcg_temp_free_i32(crf);
2358     gen_helper_float_check_status(cpu_env);
2359 }
2360
2361 /***                         Floating-point move                           ***/
2362 /* fabs */
2363 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2364 static void gen_fabs(DisasContext *ctx)
2365 {
2366     if (unlikely(!ctx->fpu_enabled)) {
2367         gen_exception(ctx, POWERPC_EXCP_FPU);
2368         return;
2369     }
2370     tcg_gen_andi_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2371                      ~(1ULL << 63));
2372     if (unlikely(Rc(ctx->opcode))) {
2373         gen_set_cr1_from_fpscr(ctx);
2374     }
2375 }
2376
2377 /* fmr  - fmr. */
2378 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2379 static void gen_fmr(DisasContext *ctx)
2380 {
2381     if (unlikely(!ctx->fpu_enabled)) {
2382         gen_exception(ctx, POWERPC_EXCP_FPU);
2383         return;
2384     }
2385     tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2386     if (unlikely(Rc(ctx->opcode))) {
2387         gen_set_cr1_from_fpscr(ctx);
2388     }
2389 }
2390
2391 /* fnabs */
2392 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2393 static void gen_fnabs(DisasContext *ctx)
2394 {
2395     if (unlikely(!ctx->fpu_enabled)) {
2396         gen_exception(ctx, POWERPC_EXCP_FPU);
2397         return;
2398     }
2399     tcg_gen_ori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2400                     1ULL << 63);
2401     if (unlikely(Rc(ctx->opcode))) {
2402         gen_set_cr1_from_fpscr(ctx);
2403     }
2404 }
2405
2406 /* fneg */
2407 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2408 static void gen_fneg(DisasContext *ctx)
2409 {
2410     if (unlikely(!ctx->fpu_enabled)) {
2411         gen_exception(ctx, POWERPC_EXCP_FPU);
2412         return;
2413     }
2414     tcg_gen_xori_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)],
2415                      1ULL << 63);
2416     if (unlikely(Rc(ctx->opcode))) {
2417         gen_set_cr1_from_fpscr(ctx);
2418     }
2419 }
2420
2421 /* fcpsgn: PowerPC 2.05 specification */
2422 /* XXX: beware that fcpsgn never checks for NaNs nor update FPSCR */
2423 static void gen_fcpsgn(DisasContext *ctx)
2424 {
2425     if (unlikely(!ctx->fpu_enabled)) {
2426         gen_exception(ctx, POWERPC_EXCP_FPU);
2427         return;
2428     }
2429     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2430                         cpu_fpr[rB(ctx->opcode)], 0, 63);
2431     if (unlikely(Rc(ctx->opcode))) {
2432         gen_set_cr1_from_fpscr(ctx);
2433     }
2434 }
2435
2436 static void gen_fmrgew(DisasContext *ctx)
2437 {
2438     TCGv_i64 b0;
2439     if (unlikely(!ctx->fpu_enabled)) {
2440         gen_exception(ctx, POWERPC_EXCP_FPU);
2441         return;
2442     }
2443     b0 = tcg_temp_new_i64();
2444     tcg_gen_shri_i64(b0, cpu_fpr[rB(ctx->opcode)], 32);
2445     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],
2446                         b0, 0, 32);
2447     tcg_temp_free_i64(b0);
2448 }
2449
2450 static void gen_fmrgow(DisasContext *ctx)
2451 {
2452     if (unlikely(!ctx->fpu_enabled)) {
2453         gen_exception(ctx, POWERPC_EXCP_FPU);
2454         return;
2455     }
2456     tcg_gen_deposit_i64(cpu_fpr[rD(ctx->opcode)],
2457                         cpu_fpr[rB(ctx->opcode)],
2458                         cpu_fpr[rA(ctx->opcode)],
2459                         32, 32);
2460 }
2461
2462 /***                  Floating-Point status & ctrl register                ***/
2463
2464 /* mcrfs */
2465 static void gen_mcrfs(DisasContext *ctx)
2466 {
2467     TCGv tmp = tcg_temp_new();
2468     TCGv_i32 tmask;
2469     TCGv_i64 tnew_fpscr = tcg_temp_new_i64();
2470     int bfa;
2471     int nibble;
2472     int shift;
2473
2474     if (unlikely(!ctx->fpu_enabled)) {
2475         gen_exception(ctx, POWERPC_EXCP_FPU);
2476         return;
2477     }
2478     bfa = crfS(ctx->opcode);
2479     nibble = 7 - bfa;
2480     shift = 4 * nibble;
2481     tcg_gen_shri_tl(tmp, cpu_fpscr, shift);
2482     tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], tmp);
2483     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2484     tcg_temp_free(tmp);
2485     tcg_gen_extu_tl_i64(tnew_fpscr, cpu_fpscr);
2486     /* Only the exception bits (including FX) should be cleared if read */
2487     tcg_gen_andi_i64(tnew_fpscr, tnew_fpscr, ~((0xF << shift) & FP_EX_CLEAR_BITS));
2488     /* FEX and VX need to be updated, so don't set fpscr directly */
2489     tmask = tcg_const_i32(1 << nibble);
2490     gen_helper_store_fpscr(cpu_env, tnew_fpscr, tmask);
2491     tcg_temp_free_i32(tmask);
2492     tcg_temp_free_i64(tnew_fpscr);
2493 }
2494
2495 /* mffs */
2496 static void gen_mffs(DisasContext *ctx)
2497 {
2498     if (unlikely(!ctx->fpu_enabled)) {
2499         gen_exception(ctx, POWERPC_EXCP_FPU);
2500         return;
2501     }
2502     gen_reset_fpstatus();
2503     tcg_gen_extu_tl_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2504     if (unlikely(Rc(ctx->opcode))) {
2505         gen_set_cr1_from_fpscr(ctx);
2506     }
2507 }
2508
2509 /* mtfsb0 */
2510 static void gen_mtfsb0(DisasContext *ctx)
2511 {
2512     uint8_t crb;
2513
2514     if (unlikely(!ctx->fpu_enabled)) {
2515         gen_exception(ctx, POWERPC_EXCP_FPU);
2516         return;
2517     }
2518     crb = 31 - crbD(ctx->opcode);
2519     gen_reset_fpstatus();
2520     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2521         TCGv_i32 t0;
2522         /* NIP cannot be restored if the memory exception comes from an helper */
2523         gen_update_nip(ctx, ctx->nip - 4);
2524         t0 = tcg_const_i32(crb);
2525         gen_helper_fpscr_clrbit(cpu_env, t0);
2526         tcg_temp_free_i32(t0);
2527     }
2528     if (unlikely(Rc(ctx->opcode) != 0)) {
2529         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2530         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2531     }
2532 }
2533
2534 /* mtfsb1 */
2535 static void gen_mtfsb1(DisasContext *ctx)
2536 {
2537     uint8_t crb;
2538
2539     if (unlikely(!ctx->fpu_enabled)) {
2540         gen_exception(ctx, POWERPC_EXCP_FPU);
2541         return;
2542     }
2543     crb = 31 - crbD(ctx->opcode);
2544     gen_reset_fpstatus();
2545     /* XXX: we pretend we can only do IEEE floating-point computations */
2546     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2547         TCGv_i32 t0;
2548         /* NIP cannot be restored if the memory exception comes from an helper */
2549         gen_update_nip(ctx, ctx->nip - 4);
2550         t0 = tcg_const_i32(crb);
2551         gen_helper_fpscr_setbit(cpu_env, t0);
2552         tcg_temp_free_i32(t0);
2553     }
2554     if (unlikely(Rc(ctx->opcode) != 0)) {
2555         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2556         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2557     }
2558     /* We can raise a differed exception */
2559     gen_helper_float_check_status(cpu_env);
2560 }
2561
2562 /* mtfsf */
2563 static void gen_mtfsf(DisasContext *ctx)
2564 {
2565     TCGv_i32 t0;
2566     int flm, l, w;
2567
2568     if (unlikely(!ctx->fpu_enabled)) {
2569         gen_exception(ctx, POWERPC_EXCP_FPU);
2570         return;
2571     }
2572     flm = FPFLM(ctx->opcode);
2573     l = FPL(ctx->opcode);
2574     w = FPW(ctx->opcode);
2575     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2576         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2577         return;
2578     }
2579     /* NIP cannot be restored if the memory exception comes from an helper */
2580     gen_update_nip(ctx, ctx->nip - 4);
2581     gen_reset_fpstatus();
2582     if (l) {
2583         t0 = tcg_const_i32((ctx->insns_flags2 & PPC2_ISA205) ? 0xffff : 0xff);
2584     } else {
2585         t0 = tcg_const_i32(flm << (w * 8));
2586     }
2587     gen_helper_store_fpscr(cpu_env, cpu_fpr[rB(ctx->opcode)], t0);
2588     tcg_temp_free_i32(t0);
2589     if (unlikely(Rc(ctx->opcode) != 0)) {
2590         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2591         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2592     }
2593     /* We can raise a differed exception */
2594     gen_helper_float_check_status(cpu_env);
2595 }
2596
2597 /* mtfsfi */
2598 static void gen_mtfsfi(DisasContext *ctx)
2599 {
2600     int bf, sh, w;
2601     TCGv_i64 t0;
2602     TCGv_i32 t1;
2603
2604     if (unlikely(!ctx->fpu_enabled)) {
2605         gen_exception(ctx, POWERPC_EXCP_FPU);
2606         return;
2607     }
2608     w = FPW(ctx->opcode);
2609     bf = FPBF(ctx->opcode);
2610     if (unlikely(w & !(ctx->insns_flags2 & PPC2_ISA205))) {
2611         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2612         return;
2613     }
2614     sh = (8 * w) + 7 - bf;
2615     /* NIP cannot be restored if the memory exception comes from an helper */
2616     gen_update_nip(ctx, ctx->nip - 4);
2617     gen_reset_fpstatus();
2618     t0 = tcg_const_i64(((uint64_t)FPIMM(ctx->opcode)) << (4 * sh));
2619     t1 = tcg_const_i32(1 << sh);
2620     gen_helper_store_fpscr(cpu_env, t0, t1);
2621     tcg_temp_free_i64(t0);
2622     tcg_temp_free_i32(t1);
2623     if (unlikely(Rc(ctx->opcode) != 0)) {
2624         tcg_gen_trunc_tl_i32(cpu_crf[1], cpu_fpscr);
2625         tcg_gen_shri_i32(cpu_crf[1], cpu_crf[1], FPSCR_OX);
2626     }
2627     /* We can raise a differed exception */
2628     gen_helper_float_check_status(cpu_env);
2629 }
2630
2631 /***                           Addressing modes                            ***/
2632 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2633 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2634                                       target_long maskl)
2635 {
2636     target_long simm = SIMM(ctx->opcode);
2637
2638     simm &= ~maskl;
2639     if (rA(ctx->opcode) == 0) {
2640         if (NARROW_MODE(ctx)) {
2641             simm = (uint32_t)simm;
2642         }
2643         tcg_gen_movi_tl(EA, simm);
2644     } else if (likely(simm != 0)) {
2645         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2646         if (NARROW_MODE(ctx)) {
2647             tcg_gen_ext32u_tl(EA, EA);
2648         }
2649     } else {
2650         if (NARROW_MODE(ctx)) {
2651             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2652         } else {
2653             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2654         }
2655     }
2656 }
2657
2658 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2659 {
2660     if (rA(ctx->opcode) == 0) {
2661         if (NARROW_MODE(ctx)) {
2662             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2663         } else {
2664             tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2665         }
2666     } else {
2667         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2668         if (NARROW_MODE(ctx)) {
2669             tcg_gen_ext32u_tl(EA, EA);
2670         }
2671     }
2672 }
2673
2674 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2675 {
2676     if (rA(ctx->opcode) == 0) {
2677         tcg_gen_movi_tl(EA, 0);
2678     } else if (NARROW_MODE(ctx)) {
2679         tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2680     } else {
2681         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2682     }
2683 }
2684
2685 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2686                                 target_long val)
2687 {
2688     tcg_gen_addi_tl(ret, arg1, val);
2689     if (NARROW_MODE(ctx)) {
2690         tcg_gen_ext32u_tl(ret, ret);
2691     }
2692 }
2693
2694 static inline void gen_check_align(DisasContext *ctx, TCGv EA, int mask)
2695 {
2696     TCGLabel *l1 = gen_new_label();
2697     TCGv t0 = tcg_temp_new();
2698     TCGv_i32 t1, t2;
2699     /* NIP cannot be restored if the memory exception comes from an helper */
2700     gen_update_nip(ctx, ctx->nip - 4);
2701     tcg_gen_andi_tl(t0, EA, mask);
2702     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2703     t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2704     t2 = tcg_const_i32(0);
2705     gen_helper_raise_exception_err(cpu_env, t1, t2);
2706     tcg_temp_free_i32(t1);
2707     tcg_temp_free_i32(t2);
2708     gen_set_label(l1);
2709     tcg_temp_free(t0);
2710 }
2711
2712 /***                             Integer load                              ***/
2713 static inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2714 {
2715     tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2716 }
2717
2718 static inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2719 {
2720     TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2721     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2722 }
2723
2724 static inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2725 {
2726     TCGMemOp op = MO_SW | ctx->default_tcg_memop_mask;
2727     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2728 }
2729
2730 static inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2731 {
2732     TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2733     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2734 }
2735
2736 static void gen_qemu_ld32u_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2737 {
2738     TCGv tmp = tcg_temp_new();
2739     gen_qemu_ld32u(ctx, tmp, addr);
2740     tcg_gen_extu_tl_i64(val, tmp);
2741     tcg_temp_free(tmp);
2742 }
2743
2744 static inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2745 {
2746     TCGMemOp op = MO_SL | ctx->default_tcg_memop_mask;
2747     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
2748 }
2749
2750 static void gen_qemu_ld32s_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2751 {
2752     TCGv tmp = tcg_temp_new();
2753     gen_qemu_ld32s(ctx, tmp, addr);
2754     tcg_gen_ext_tl_i64(val, tmp);
2755     tcg_temp_free(tmp);
2756 }
2757
2758 static inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2759 {
2760     TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2761     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
2762 }
2763
2764 static inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2765 {
2766     tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2767 }
2768
2769 static inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2770 {
2771     TCGMemOp op = MO_UW | ctx->default_tcg_memop_mask;
2772     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
2773 }
2774
2775 static inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2776 {
2777     TCGMemOp op = MO_UL | ctx->default_tcg_memop_mask;
2778     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
2779 }
2780
2781 static void gen_qemu_st32_i64(DisasContext *ctx, TCGv_i64 val, TCGv addr)
2782 {
2783     TCGv tmp = tcg_temp_new();
2784     tcg_gen_trunc_i64_tl(tmp, val);
2785     gen_qemu_st32(ctx, tmp, addr);
2786     tcg_temp_free(tmp);
2787 }
2788
2789 static inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2790 {
2791     TCGMemOp op = MO_Q | ctx->default_tcg_memop_mask;
2792     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
2793 }
2794
2795 #define GEN_LD(name, ldop, opc, type)                                         \
2796 static void glue(gen_, name)(DisasContext *ctx)                                       \
2797 {                                                                             \
2798     TCGv EA;                                                                  \
2799     gen_set_access_type(ctx, ACCESS_INT);                                     \
2800     EA = tcg_temp_new();                                                      \
2801     gen_addr_imm_index(ctx, EA, 0);                                           \
2802     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2803     tcg_temp_free(EA);                                                        \
2804 }
2805
2806 #define GEN_LDU(name, ldop, opc, type)                                        \
2807 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
2808 {                                                                             \
2809     TCGv EA;                                                                  \
2810     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2811                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2812         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2813         return;                                                               \
2814     }                                                                         \
2815     gen_set_access_type(ctx, ACCESS_INT);                                     \
2816     EA = tcg_temp_new();                                                      \
2817     if (type == PPC_64B)                                                      \
2818         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2819     else                                                                      \
2820         gen_addr_imm_index(ctx, EA, 0);                                       \
2821     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2822     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2823     tcg_temp_free(EA);                                                        \
2824 }
2825
2826 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2827 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2828 {                                                                             \
2829     TCGv EA;                                                                  \
2830     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2831                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2832         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2833         return;                                                               \
2834     }                                                                         \
2835     gen_set_access_type(ctx, ACCESS_INT);                                     \
2836     EA = tcg_temp_new();                                                      \
2837     gen_addr_reg_index(ctx, EA);                                              \
2838     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2839     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2840     tcg_temp_free(EA);                                                        \
2841 }
2842
2843 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
2844 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2845 {                                                                             \
2846     TCGv EA;                                                                  \
2847     gen_set_access_type(ctx, ACCESS_INT);                                     \
2848     EA = tcg_temp_new();                                                      \
2849     gen_addr_reg_index(ctx, EA);                                              \
2850     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2851     tcg_temp_free(EA);                                                        \
2852 }
2853 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2854     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE)
2855
2856 #define GEN_LDS(name, ldop, op, type)                                         \
2857 GEN_LD(name, ldop, op | 0x20, type);                                          \
2858 GEN_LDU(name, ldop, op | 0x21, type);                                         \
2859 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2860 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2861
2862 /* lbz lbzu lbzux lbzx */
2863 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2864 /* lha lhau lhaux lhax */
2865 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2866 /* lhz lhzu lhzux lhzx */
2867 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2868 /* lwz lwzu lwzux lwzx */
2869 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2870 #if defined(TARGET_PPC64)
2871 /* lwaux */
2872 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2873 /* lwax */
2874 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2875 /* ldux */
2876 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2877 /* ldx */
2878 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2879
2880 static void gen_ld(DisasContext *ctx)
2881 {
2882     TCGv EA;
2883     if (Rc(ctx->opcode)) {
2884         if (unlikely(rA(ctx->opcode) == 0 ||
2885                      rA(ctx->opcode) == rD(ctx->opcode))) {
2886             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2887             return;
2888         }
2889     }
2890     gen_set_access_type(ctx, ACCESS_INT);
2891     EA = tcg_temp_new();
2892     gen_addr_imm_index(ctx, EA, 0x03);
2893     if (ctx->opcode & 0x02) {
2894         /* lwa (lwau is undefined) */
2895         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2896     } else {
2897         /* ld - ldu */
2898         gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2899     }
2900     if (Rc(ctx->opcode))
2901         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2902     tcg_temp_free(EA);
2903 }
2904
2905 /* lq */
2906 static void gen_lq(DisasContext *ctx)
2907 {
2908     int ra, rd;
2909     TCGv EA;
2910
2911     /* lq is a legal user mode instruction starting in ISA 2.07 */
2912     bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2913     bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2914
2915     if (!legal_in_user_mode && ctx->pr) {
2916         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2917         return;
2918     }
2919
2920     if (!le_is_supported && ctx->le_mode) {
2921         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2922         return;
2923     }
2924
2925     ra = rA(ctx->opcode);
2926     rd = rD(ctx->opcode);
2927     if (unlikely((rd & 1) || rd == ra)) {
2928         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2929         return;
2930     }
2931
2932     gen_set_access_type(ctx, ACCESS_INT);
2933     EA = tcg_temp_new();
2934     gen_addr_imm_index(ctx, EA, 0x0F);
2935
2936     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
2937        64-bit byteswap already. */
2938     if (unlikely(ctx->le_mode)) {
2939         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2940         gen_addr_add(ctx, EA, EA, 8);
2941         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2942     } else {
2943         gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2944         gen_addr_add(ctx, EA, EA, 8);
2945         gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2946     }
2947     tcg_temp_free(EA);
2948 }
2949 #endif
2950
2951 /***                              Integer store                            ***/
2952 #define GEN_ST(name, stop, opc, type)                                         \
2953 static void glue(gen_, name)(DisasContext *ctx)                                       \
2954 {                                                                             \
2955     TCGv EA;                                                                  \
2956     gen_set_access_type(ctx, ACCESS_INT);                                     \
2957     EA = tcg_temp_new();                                                      \
2958     gen_addr_imm_index(ctx, EA, 0);                                           \
2959     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2960     tcg_temp_free(EA);                                                        \
2961 }
2962
2963 #define GEN_STU(name, stop, opc, type)                                        \
2964 static void glue(gen_, stop##u)(DisasContext *ctx)                                    \
2965 {                                                                             \
2966     TCGv EA;                                                                  \
2967     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2968         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2969         return;                                                               \
2970     }                                                                         \
2971     gen_set_access_type(ctx, ACCESS_INT);                                     \
2972     EA = tcg_temp_new();                                                      \
2973     if (type == PPC_64B)                                                      \
2974         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2975     else                                                                      \
2976         gen_addr_imm_index(ctx, EA, 0);                                       \
2977     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2978     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2979     tcg_temp_free(EA);                                                        \
2980 }
2981
2982 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
2983 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
2984 {                                                                             \
2985     TCGv EA;                                                                  \
2986     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2987         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2988         return;                                                               \
2989     }                                                                         \
2990     gen_set_access_type(ctx, ACCESS_INT);                                     \
2991     EA = tcg_temp_new();                                                      \
2992     gen_addr_reg_index(ctx, EA);                                              \
2993     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2994     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2995     tcg_temp_free(EA);                                                        \
2996 }
2997
2998 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
2999 static void glue(gen_, name##x)(DisasContext *ctx)                            \
3000 {                                                                             \
3001     TCGv EA;                                                                  \
3002     gen_set_access_type(ctx, ACCESS_INT);                                     \
3003     EA = tcg_temp_new();                                                      \
3004     gen_addr_reg_index(ctx, EA);                                              \
3005     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
3006     tcg_temp_free(EA);                                                        \
3007 }
3008 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
3009     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE)
3010
3011 #define GEN_STS(name, stop, op, type)                                         \
3012 GEN_ST(name, stop, op | 0x20, type);                                          \
3013 GEN_STU(name, stop, op | 0x21, type);                                         \
3014 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
3015 GEN_STX(name, stop, 0x17, op | 0x00, type)
3016
3017 /* stb stbu stbux stbx */
3018 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
3019 /* sth sthu sthux sthx */
3020 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
3021 /* stw stwu stwux stwx */
3022 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
3023 #if defined(TARGET_PPC64)
3024 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
3025 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
3026
3027 static void gen_std(DisasContext *ctx)
3028 {
3029     int rs;
3030     TCGv EA;
3031
3032     rs = rS(ctx->opcode);
3033     if ((ctx->opcode & 0x3) == 0x2) { /* stq */
3034
3035         bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3036         bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
3037
3038         if (!legal_in_user_mode && ctx->pr) {
3039             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3040             return;
3041         }
3042
3043         if (!le_is_supported && ctx->le_mode) {
3044             gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3045             return;
3046         }
3047
3048         if (unlikely(rs & 1)) {
3049             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3050             return;
3051         }
3052         gen_set_access_type(ctx, ACCESS_INT);
3053         EA = tcg_temp_new();
3054         gen_addr_imm_index(ctx, EA, 0x03);
3055
3056         /* We only need to swap high and low halves. gen_qemu_st64 does
3057            necessary 64-bit byteswap already. */
3058         if (unlikely(ctx->le_mode)) {
3059             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3060             gen_addr_add(ctx, EA, EA, 8);
3061             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3062         } else {
3063             gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3064             gen_addr_add(ctx, EA, EA, 8);
3065             gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
3066         }
3067         tcg_temp_free(EA);
3068     } else {
3069         /* std / stdu*/
3070         if (Rc(ctx->opcode)) {
3071             if (unlikely(rA(ctx->opcode) == 0)) {
3072                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3073                 return;
3074             }
3075         }
3076         gen_set_access_type(ctx, ACCESS_INT);
3077         EA = tcg_temp_new();
3078         gen_addr_imm_index(ctx, EA, 0x03);
3079         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
3080         if (Rc(ctx->opcode))
3081             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3082         tcg_temp_free(EA);
3083     }
3084 }
3085 #endif
3086 /***                Integer load and store with byte reverse               ***/
3087
3088 /* lhbrx */
3089 static inline void gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3090 {
3091     TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3092     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
3093 }
3094 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3095
3096 /* lwbrx */
3097 static inline void gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3098 {
3099     TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3100     tcg_gen_qemu_ld_tl(arg1, arg2, ctx->mem_idx, op);
3101 }
3102 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3103
3104 #if defined(TARGET_PPC64)
3105 /* ldbrx */
3106 static inline void gen_qemu_ld64ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
3107 {
3108     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3109     tcg_gen_qemu_ld_i64(arg1, arg2, ctx->mem_idx, op);
3110 }
3111 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX);
3112 #endif  /* TARGET_PPC64 */
3113
3114 /* sthbrx */
3115 static inline void gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3116 {
3117     TCGMemOp op = MO_UW | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3118     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
3119 }
3120 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3121
3122 /* stwbrx */
3123 static inline void gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3124 {
3125     TCGMemOp op = MO_UL | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3126     tcg_gen_qemu_st_tl(arg1, arg2, ctx->mem_idx, op);
3127 }
3128 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3129
3130 #if defined(TARGET_PPC64)
3131 /* stdbrx */
3132 static inline void gen_qemu_st64r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3133 {
3134     TCGMemOp op = MO_Q | (ctx->default_tcg_memop_mask ^ MO_BSWAP);
3135     tcg_gen_qemu_st_i64(arg1, arg2, ctx->mem_idx, op);
3136 }
3137 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX);
3138 #endif  /* TARGET_PPC64 */
3139
3140 /***                    Integer load and store multiple                    ***/
3141
3142 /* lmw */
3143 static void gen_lmw(DisasContext *ctx)
3144 {
3145     TCGv t0;
3146     TCGv_i32 t1;
3147     gen_set_access_type(ctx, ACCESS_INT);
3148     /* NIP cannot be restored if the memory exception comes from an helper */
3149     gen_update_nip(ctx, ctx->nip - 4);
3150     t0 = tcg_temp_new();
3151     t1 = tcg_const_i32(rD(ctx->opcode));
3152     gen_addr_imm_index(ctx, t0, 0);
3153     gen_helper_lmw(cpu_env, t0, t1);
3154     tcg_temp_free(t0);
3155     tcg_temp_free_i32(t1);
3156 }
3157
3158 /* stmw */
3159 static void gen_stmw(DisasContext *ctx)
3160 {
3161     TCGv t0;
3162     TCGv_i32 t1;
3163     gen_set_access_type(ctx, ACCESS_INT);
3164     /* NIP cannot be restored if the memory exception comes from an helper */
3165     gen_update_nip(ctx, ctx->nip - 4);
3166     t0 = tcg_temp_new();
3167     t1 = tcg_const_i32(rS(ctx->opcode));
3168     gen_addr_imm_index(ctx, t0, 0);
3169     gen_helper_stmw(cpu_env, t0, t1);
3170     tcg_temp_free(t0);
3171     tcg_temp_free_i32(t1);
3172 }
3173
3174 /***                    Integer load and store strings                     ***/
3175
3176 /* lswi */
3177 /* PowerPC32 specification says we must generate an exception if
3178  * rA is in the range of registers to be loaded.
3179  * In an other hand, IBM says this is valid, but rA won't be loaded.
3180  * For now, I'll follow the spec...
3181  */
3182 static void gen_lswi(DisasContext *ctx)
3183 {
3184     TCGv t0;
3185     TCGv_i32 t1, t2;
3186     int nb = NB(ctx->opcode);
3187     int start = rD(ctx->opcode);
3188     int ra = rA(ctx->opcode);
3189     int nr;
3190
3191     if (nb == 0)
3192         nb = 32;
3193     nr = (nb + 3) / 4;
3194     if (unlikely(lsw_reg_in_range(start, nr, ra))) {
3195         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3196         return;
3197     }
3198     gen_set_access_type(ctx, ACCESS_INT);
3199     /* NIP cannot be restored if the memory exception comes from an helper */
3200     gen_update_nip(ctx, ctx->nip - 4);
3201     t0 = tcg_temp_new();
3202     gen_addr_register(ctx, t0);
3203     t1 = tcg_const_i32(nb);
3204     t2 = tcg_const_i32(start);
3205     gen_helper_lsw(cpu_env, t0, t1, t2);
3206     tcg_temp_free(t0);
3207     tcg_temp_free_i32(t1);
3208     tcg_temp_free_i32(t2);
3209 }
3210
3211 /* lswx */
3212 static void gen_lswx(DisasContext *ctx)
3213 {
3214     TCGv t0;
3215     TCGv_i32 t1, t2, t3;
3216     gen_set_access_type(ctx, ACCESS_INT);
3217     /* NIP cannot be restored if the memory exception comes from an helper */
3218     gen_update_nip(ctx, ctx->nip - 4);
3219     t0 = tcg_temp_new();
3220     gen_addr_reg_index(ctx, t0);
3221     t1 = tcg_const_i32(rD(ctx->opcode));
3222     t2 = tcg_const_i32(rA(ctx->opcode));
3223     t3 = tcg_const_i32(rB(ctx->opcode));
3224     gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3225     tcg_temp_free(t0);
3226     tcg_temp_free_i32(t1);
3227     tcg_temp_free_i32(t2);
3228     tcg_temp_free_i32(t3);
3229 }
3230
3231 /* stswi */
3232 static void gen_stswi(DisasContext *ctx)
3233 {
3234     TCGv t0;
3235     TCGv_i32 t1, t2;
3236     int nb = NB(ctx->opcode);
3237     gen_set_access_type(ctx, ACCESS_INT);
3238     /* NIP cannot be restored if the memory exception comes from an helper */
3239     gen_update_nip(ctx, ctx->nip - 4);
3240     t0 = tcg_temp_new();
3241     gen_addr_register(ctx, t0);
3242     if (nb == 0)
3243         nb = 32;
3244     t1 = tcg_const_i32(nb);
3245     t2 = tcg_const_i32(rS(ctx->opcode));
3246     gen_helper_stsw(cpu_env, t0, t1, t2);
3247     tcg_temp_free(t0);
3248     tcg_temp_free_i32(t1);
3249     tcg_temp_free_i32(t2);
3250 }
3251
3252 /* stswx */
3253 static void gen_stswx(DisasContext *ctx)
3254 {
3255     TCGv t0;
3256     TCGv_i32 t1, t2;
3257     gen_set_access_type(ctx, ACCESS_INT);
3258     /* NIP cannot be restored if the memory exception comes from an helper */
3259     gen_update_nip(ctx, ctx->nip - 4);
3260     t0 = tcg_temp_new();
3261     gen_addr_reg_index(ctx, t0);
3262     t1 = tcg_temp_new_i32();
3263     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3264     tcg_gen_andi_i32(t1, t1, 0x7F);
3265     t2 = tcg_const_i32(rS(ctx->opcode));
3266     gen_helper_stsw(cpu_env, t0, t1, t2);
3267     tcg_temp_free(t0);
3268     tcg_temp_free_i32(t1);
3269     tcg_temp_free_i32(t2);
3270 }
3271
3272 /***                        Memory synchronisation                         ***/
3273 /* eieio */
3274 static void gen_eieio(DisasContext *ctx)
3275 {
3276 }
3277
3278 #if !defined(CONFIG_USER_ONLY) && defined(TARGET_PPC64)
3279 static inline void gen_check_tlb_flush(DisasContext *ctx)
3280 {
3281     TCGv_i32 t = tcg_temp_new_i32();
3282     TCGLabel *l = gen_new_label();
3283
3284     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
3285     tcg_gen_brcondi_i32(TCG_COND_EQ, t, 0, l);
3286     gen_helper_check_tlb_flush(cpu_env);
3287     gen_set_label(l);
3288     tcg_temp_free_i32(t);
3289 }
3290 #else
3291 static inline void gen_check_tlb_flush(DisasContext *ctx) { }
3292 #endif
3293
3294 /* isync */
3295 static void gen_isync(DisasContext *ctx)
3296 {
3297     /*
3298      * We need to check for a pending TLB flush. This can only happen in
3299      * kernel mode however so check MSR_PR
3300      */
3301     if (!ctx->pr) {
3302         gen_check_tlb_flush(ctx);
3303     }
3304     gen_stop_exception(ctx);
3305 }
3306
3307 #define LARX(name, len, loadop)                                      \
3308 static void gen_##name(DisasContext *ctx)                            \
3309 {                                                                    \
3310     TCGv t0;                                                         \
3311     TCGv gpr = cpu_gpr[rD(ctx->opcode)];                             \
3312     gen_set_access_type(ctx, ACCESS_RES);                            \
3313     t0 = tcg_temp_local_new();                                       \
3314     gen_addr_reg_index(ctx, t0);                                     \
3315     if ((len) > 1) {                                                 \
3316         gen_check_align(ctx, t0, (len)-1);                           \
3317     }                                                                \
3318     gen_qemu_##loadop(ctx, gpr, t0);                                 \
3319     tcg_gen_mov_tl(cpu_reserve, t0);                                 \
3320     tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
3321     tcg_temp_free(t0);                                               \
3322 }
3323
3324 /* lwarx */
3325 LARX(lbarx, 1, ld8u);
3326 LARX(lharx, 2, ld16u);
3327 LARX(lwarx, 4, ld32u);
3328
3329
3330 #if defined(CONFIG_USER_ONLY)
3331 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3332                                   int reg, int size)
3333 {
3334     TCGv t0 = tcg_temp_new();
3335     uint32_t save_exception = ctx->exception;
3336
3337     tcg_gen_st_tl(EA, cpu_env, offsetof(CPUPPCState, reserve_ea));
3338     tcg_gen_movi_tl(t0, (size << 5) | reg);
3339     tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, reserve_info));
3340     tcg_temp_free(t0);
3341     gen_update_nip(ctx, ctx->nip-4);
3342     ctx->exception = POWERPC_EXCP_BRANCH;
3343     gen_exception(ctx, POWERPC_EXCP_STCX);
3344     ctx->exception = save_exception;
3345 }
3346 #else
3347 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
3348                                   int reg, int size)
3349 {
3350     TCGLabel *l1;
3351
3352     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3353     l1 = gen_new_label();
3354     tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
3355     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3356 #if defined(TARGET_PPC64)
3357     if (size == 8) {
3358         gen_qemu_st64(ctx, cpu_gpr[reg], EA);
3359     } else
3360 #endif
3361     if (size == 4) {
3362         gen_qemu_st32(ctx, cpu_gpr[reg], EA);
3363     } else if (size == 2) {
3364         gen_qemu_st16(ctx, cpu_gpr[reg], EA);
3365 #if defined(TARGET_PPC64)
3366     } else if (size == 16) {
3367         TCGv gpr1, gpr2 , EA8;
3368         if (unlikely(ctx->le_mode)) {
3369             gpr1 = cpu_gpr[reg+1];
3370             gpr2 = cpu_gpr[reg];
3371         } else {
3372             gpr1 = cpu_gpr[reg];
3373             gpr2 = cpu_gpr[reg+1];
3374         }
3375         gen_qemu_st64(ctx, gpr1, EA);
3376         EA8 = tcg_temp_local_new();
3377         gen_addr_add(ctx, EA8, EA, 8);
3378         gen_qemu_st64(ctx, gpr2, EA8);
3379         tcg_temp_free(EA8);
3380 #endif
3381     } else {
3382         gen_qemu_st8(ctx, cpu_gpr[reg], EA);
3383     }
3384     gen_set_label(l1);
3385     tcg_gen_movi_tl(cpu_reserve, -1);
3386 }
3387 #endif
3388
3389 #define STCX(name, len)                                   \
3390 static void gen_##name(DisasContext *ctx)                 \
3391 {                                                         \
3392     TCGv t0;                                              \
3393     if (unlikely((len == 16) && (rD(ctx->opcode) & 1))) { \
3394         gen_inval_exception(ctx,                          \
3395                             POWERPC_EXCP_INVAL_INVAL);    \
3396         return;                                           \
3397     }                                                     \
3398     gen_set_access_type(ctx, ACCESS_RES);                 \
3399     t0 = tcg_temp_local_new();                            \
3400     gen_addr_reg_index(ctx, t0);                          \
3401     if (len > 1) {                                        \
3402         gen_check_align(ctx, t0, (len)-1);                \
3403     }                                                     \
3404     gen_conditional_store(ctx, t0, rS(ctx->opcode), len); \
3405     tcg_temp_free(t0);                                    \
3406 }
3407
3408 STCX(stbcx_, 1);
3409 STCX(sthcx_, 2);
3410 STCX(stwcx_, 4);
3411
3412 #if defined(TARGET_PPC64)
3413 /* ldarx */
3414 LARX(ldarx, 8, ld64);
3415
3416 /* lqarx */
3417 static void gen_lqarx(DisasContext *ctx)
3418 {
3419     TCGv EA;
3420     int rd = rD(ctx->opcode);
3421     TCGv gpr1, gpr2;
3422
3423     if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3424                  (rd == rB(ctx->opcode)))) {
3425         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3426         return;
3427     }
3428
3429     gen_set_access_type(ctx, ACCESS_RES);
3430     EA = tcg_temp_local_new();
3431     gen_addr_reg_index(ctx, EA);
3432     gen_check_align(ctx, EA, 15);
3433     if (unlikely(ctx->le_mode)) {
3434         gpr1 = cpu_gpr[rd+1];
3435         gpr2 = cpu_gpr[rd];
3436     } else {
3437         gpr1 = cpu_gpr[rd];
3438         gpr2 = cpu_gpr[rd+1];
3439     }
3440     gen_qemu_ld64(ctx, gpr1, EA);
3441     tcg_gen_mov_tl(cpu_reserve, EA);
3442
3443     gen_addr_add(ctx, EA, EA, 8);
3444     gen_qemu_ld64(ctx, gpr2, EA);
3445
3446     tcg_gen_st_tl(gpr1, cpu_env, offsetof(CPUPPCState, reserve_val));
3447     tcg_gen_st_tl(gpr2, cpu_env, offsetof(CPUPPCState, reserve_val2));
3448
3449     tcg_temp_free(EA);
3450 }
3451
3452 /* stdcx. */
3453 STCX(stdcx_, 8);
3454 STCX(stqcx_, 16);
3455 #endif /* defined(TARGET_PPC64) */
3456
3457 /* sync */
3458 static void gen_sync(DisasContext *ctx)
3459 {
3460     uint32_t l = (ctx->opcode >> 21) & 3;
3461
3462     /*
3463      * For l == 2, it's a ptesync, We need to check for a pending TLB flush.
3464      * This can only happen in kernel mode however so check MSR_PR as well.
3465      */
3466     if (l == 2 && !ctx->pr) {
3467         gen_check_tlb_flush(ctx);
3468     }
3469 }
3470
3471 /* wait */
3472 static void gen_wait(DisasContext *ctx)
3473 {
3474     TCGv_i32 t0 = tcg_temp_new_i32();
3475     tcg_gen_st_i32(t0, cpu_env,
3476                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3477     tcg_temp_free_i32(t0);
3478     /* Stop translation, as the CPU is supposed to sleep from now */
3479     gen_exception_err(ctx, EXCP_HLT, 1);
3480 }
3481
3482 /***                         Floating-point load                           ***/
3483 #define GEN_LDF(name, ldop, opc, type)                                        \
3484 static void glue(gen_, name)(DisasContext *ctx)                                       \
3485 {                                                                             \
3486     TCGv EA;                                                                  \
3487     if (unlikely(!ctx->fpu_enabled)) {                                        \
3488         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3489         return;                                                               \
3490     }                                                                         \
3491     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3492     EA = tcg_temp_new();                                                      \
3493     gen_addr_imm_index(ctx, EA, 0);                                           \
3494     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3495     tcg_temp_free(EA);                                                        \
3496 }
3497
3498 #define GEN_LDUF(name, ldop, opc, type)                                       \
3499 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3500 {                                                                             \
3501     TCGv EA;                                                                  \
3502     if (unlikely(!ctx->fpu_enabled)) {                                        \
3503         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3504         return;                                                               \
3505     }                                                                         \
3506     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3507         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3508         return;                                                               \
3509     }                                                                         \
3510     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3511     EA = tcg_temp_new();                                                      \
3512     gen_addr_imm_index(ctx, EA, 0);                                           \
3513     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3514     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3515     tcg_temp_free(EA);                                                        \
3516 }
3517
3518 #define GEN_LDUXF(name, ldop, opc, type)                                      \
3519 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3520 {                                                                             \
3521     TCGv EA;                                                                  \
3522     if (unlikely(!ctx->fpu_enabled)) {                                        \
3523         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3524         return;                                                               \
3525     }                                                                         \
3526     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3527         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3528         return;                                                               \
3529     }                                                                         \
3530     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3531     EA = tcg_temp_new();                                                      \
3532     gen_addr_reg_index(ctx, EA);                                              \
3533     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3534     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3535     tcg_temp_free(EA);                                                        \
3536 }
3537
3538 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3539 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3540 {                                                                             \
3541     TCGv EA;                                                                  \
3542     if (unlikely(!ctx->fpu_enabled)) {                                        \
3543         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3544         return;                                                               \
3545     }                                                                         \
3546     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3547     EA = tcg_temp_new();                                                      \
3548     gen_addr_reg_index(ctx, EA);                                              \
3549     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3550     tcg_temp_free(EA);                                                        \
3551 }
3552
3553 #define GEN_LDFS(name, ldop, op, type)                                        \
3554 GEN_LDF(name, ldop, op | 0x20, type);                                         \
3555 GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3556 GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3557 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3558
3559 static inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3560 {
3561     TCGv t0 = tcg_temp_new();
3562     TCGv_i32 t1 = tcg_temp_new_i32();
3563     gen_qemu_ld32u(ctx, t0, arg2);
3564     tcg_gen_trunc_tl_i32(t1, t0);
3565     tcg_temp_free(t0);
3566     gen_helper_float32_to_float64(arg1, cpu_env, t1);
3567     tcg_temp_free_i32(t1);
3568 }
3569
3570  /* lfd lfdu lfdux lfdx */
3571 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3572  /* lfs lfsu lfsux lfsx */
3573 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3574
3575 /* lfdp */
3576 static void gen_lfdp(DisasContext *ctx)
3577 {
3578     TCGv EA;
3579     if (unlikely(!ctx->fpu_enabled)) {
3580         gen_exception(ctx, POWERPC_EXCP_FPU);
3581         return;
3582     }
3583     gen_set_access_type(ctx, ACCESS_FLOAT);
3584     EA = tcg_temp_new();
3585     gen_addr_imm_index(ctx, EA, 0);
3586     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3587        64-bit byteswap already. */
3588     if (unlikely(ctx->le_mode)) {
3589         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3590         tcg_gen_addi_tl(EA, EA, 8);
3591         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3592     } else {
3593         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3594         tcg_gen_addi_tl(EA, EA, 8);
3595         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3596     }
3597     tcg_temp_free(EA);
3598 }
3599
3600 /* lfdpx */
3601 static void gen_lfdpx(DisasContext *ctx)
3602 {
3603     TCGv EA;
3604     if (unlikely(!ctx->fpu_enabled)) {
3605         gen_exception(ctx, POWERPC_EXCP_FPU);
3606         return;
3607     }
3608     gen_set_access_type(ctx, ACCESS_FLOAT);
3609     EA = tcg_temp_new();
3610     gen_addr_reg_index(ctx, EA);
3611     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary
3612        64-bit byteswap already. */
3613     if (unlikely(ctx->le_mode)) {
3614         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3615         tcg_gen_addi_tl(EA, EA, 8);
3616         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3617     } else {
3618         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3619         tcg_gen_addi_tl(EA, EA, 8);
3620         gen_qemu_ld64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3621     }
3622     tcg_temp_free(EA);
3623 }
3624
3625 /* lfiwax */
3626 static void gen_lfiwax(DisasContext *ctx)
3627 {
3628     TCGv EA;
3629     TCGv t0;
3630     if (unlikely(!ctx->fpu_enabled)) {
3631         gen_exception(ctx, POWERPC_EXCP_FPU);
3632         return;
3633     }
3634     gen_set_access_type(ctx, ACCESS_FLOAT);
3635     EA = tcg_temp_new();
3636     t0 = tcg_temp_new();
3637     gen_addr_reg_index(ctx, EA);
3638     gen_qemu_ld32s(ctx, t0, EA);
3639     tcg_gen_ext_tl_i64(cpu_fpr[rD(ctx->opcode)], t0);
3640     tcg_temp_free(EA);
3641     tcg_temp_free(t0);
3642 }
3643
3644 /* lfiwzx */
3645 static void gen_lfiwzx(DisasContext *ctx)
3646 {
3647     TCGv EA;
3648     if (unlikely(!ctx->fpu_enabled)) {
3649         gen_exception(ctx, POWERPC_EXCP_FPU);
3650         return;
3651     }
3652     gen_set_access_type(ctx, ACCESS_FLOAT);
3653     EA = tcg_temp_new();
3654     gen_addr_reg_index(ctx, EA);
3655     gen_qemu_ld32u_i64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3656     tcg_temp_free(EA);
3657 }
3658 /***                         Floating-point store                          ***/
3659 #define GEN_STF(name, stop, opc, type)                                        \
3660 static void glue(gen_, name)(DisasContext *ctx)                                       \
3661 {                                                                             \
3662     TCGv EA;                                                                  \
3663     if (unlikely(!ctx->fpu_enabled)) {                                        \
3664         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3665         return;                                                               \
3666     }                                                                         \
3667     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3668     EA = tcg_temp_new();                                                      \
3669     gen_addr_imm_index(ctx, EA, 0);                                           \
3670     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3671     tcg_temp_free(EA);                                                        \
3672 }
3673
3674 #define GEN_STUF(name, stop, opc, type)                                       \
3675 static void glue(gen_, name##u)(DisasContext *ctx)                                    \
3676 {                                                                             \
3677     TCGv EA;                                                                  \
3678     if (unlikely(!ctx->fpu_enabled)) {                                        \
3679         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3680         return;                                                               \
3681     }                                                                         \
3682     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3683         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3684         return;                                                               \
3685     }                                                                         \
3686     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3687     EA = tcg_temp_new();                                                      \
3688     gen_addr_imm_index(ctx, EA, 0);                                           \
3689     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3690     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3691     tcg_temp_free(EA);                                                        \
3692 }
3693
3694 #define GEN_STUXF(name, stop, opc, type)                                      \
3695 static void glue(gen_, name##ux)(DisasContext *ctx)                                   \
3696 {                                                                             \
3697     TCGv EA;                                                                  \
3698     if (unlikely(!ctx->fpu_enabled)) {                                        \
3699         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3700         return;                                                               \
3701     }                                                                         \
3702     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3703         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3704         return;                                                               \
3705     }                                                                         \
3706     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3707     EA = tcg_temp_new();                                                      \
3708     gen_addr_reg_index(ctx, EA);                                              \
3709     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3710     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3711     tcg_temp_free(EA);                                                        \
3712 }
3713
3714 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
3715 static void glue(gen_, name##x)(DisasContext *ctx)                                    \
3716 {                                                                             \
3717     TCGv EA;                                                                  \
3718     if (unlikely(!ctx->fpu_enabled)) {                                        \
3719         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3720         return;                                                               \
3721     }                                                                         \
3722     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3723     EA = tcg_temp_new();                                                      \
3724     gen_addr_reg_index(ctx, EA);                                              \
3725     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3726     tcg_temp_free(EA);                                                        \
3727 }
3728
3729 #define GEN_STFS(name, stop, op, type)                                        \
3730 GEN_STF(name, stop, op | 0x20, type);                                         \
3731 GEN_STUF(name, stop, op | 0x21, type);                                        \
3732 GEN_STUXF(name, stop, op | 0x01, type);                                       \
3733 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3734
3735 static inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3736 {
3737     TCGv_i32 t0 = tcg_temp_new_i32();
3738     TCGv t1 = tcg_temp_new();
3739     gen_helper_float64_to_float32(t0, cpu_env, arg1);
3740     tcg_gen_extu_i32_tl(t1, t0);
3741     tcg_temp_free_i32(t0);
3742     gen_qemu_st32(ctx, t1, arg2);
3743     tcg_temp_free(t1);
3744 }
3745
3746 /* stfd stfdu stfdux stfdx */
3747 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3748 /* stfs stfsu stfsux stfsx */
3749 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3750
3751 /* stfdp */
3752 static void gen_stfdp(DisasContext *ctx)
3753 {
3754     TCGv EA;
3755     if (unlikely(!ctx->fpu_enabled)) {
3756         gen_exception(ctx, POWERPC_EXCP_FPU);
3757         return;
3758     }
3759     gen_set_access_type(ctx, ACCESS_FLOAT);
3760     EA = tcg_temp_new();
3761     gen_addr_imm_index(ctx, EA, 0);
3762     /* We only need to swap high and low halves. gen_qemu_st64 does necessary
3763        64-bit byteswap already. */
3764     if (unlikely(ctx->le_mode)) {
3765         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3766         tcg_gen_addi_tl(EA, EA, 8);
3767         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3768     } else {
3769         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3770         tcg_gen_addi_tl(EA, EA, 8);
3771         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3772     }
3773     tcg_temp_free(EA);
3774 }
3775
3776 /* stfdpx */
3777 static void gen_stfdpx(DisasContext *ctx)
3778 {
3779     TCGv EA;
3780     if (unlikely(!ctx->fpu_enabled)) {
3781         gen_exception(ctx, POWERPC_EXCP_FPU);
3782         return;
3783     }
3784     gen_set_access_type(ctx, ACCESS_FLOAT);
3785     EA = tcg_temp_new();
3786     gen_addr_reg_index(ctx, EA);
3787     /* We only need to swap high and low halves. gen_qemu_st64 does necessary
3788        64-bit byteswap already. */
3789     if (unlikely(ctx->le_mode)) {
3790         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3791         tcg_gen_addi_tl(EA, EA, 8);
3792         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3793     } else {
3794         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode)], EA);
3795         tcg_gen_addi_tl(EA, EA, 8);
3796         gen_qemu_st64(ctx, cpu_fpr[rD(ctx->opcode) + 1], EA);
3797     }
3798     tcg_temp_free(EA);
3799 }
3800
3801 /* Optional: */
3802 static inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3803 {
3804     TCGv t0 = tcg_temp_new();
3805     tcg_gen_trunc_i64_tl(t0, arg1),
3806     gen_qemu_st32(ctx, t0, arg2);
3807     tcg_temp_free(t0);
3808 }
3809 /* stfiwx */
3810 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3811
3812 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3813 {
3814 #if defined(TARGET_PPC64)
3815     if (ctx->has_cfar)
3816         tcg_gen_movi_tl(cpu_cfar, nip);
3817 #endif
3818 }
3819
3820 static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
3821 {
3822     if (unlikely(ctx->singlestep_enabled)) {
3823         return false;
3824     }
3825
3826 #ifndef CONFIG_USER_ONLY
3827     return (ctx->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
3828 #else
3829     return true;
3830 #endif
3831 }
3832
3833 /***                                Branch                                 ***/
3834 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
3835 {
3836     if (NARROW_MODE(ctx)) {
3837         dest = (uint32_t) dest;
3838     }
3839     if (use_goto_tb(ctx, dest)) {
3840         tcg_gen_goto_tb(n);
3841         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3842         tcg_gen_exit_tb((uintptr_t)ctx->tb + n);
3843     } else {
3844         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3845         if (unlikely(ctx->singlestep_enabled)) {
3846             if ((ctx->singlestep_enabled &
3847                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3848                 (ctx->exception == POWERPC_EXCP_BRANCH ||
3849                  ctx->exception == POWERPC_EXCP_TRACE)) {
3850                 target_ulong tmp = ctx->nip;
3851                 ctx->nip = dest;
3852                 gen_exception(ctx, POWERPC_EXCP_TRACE);
3853                 ctx->nip = tmp;
3854             }
3855             if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3856                 gen_debug_exception(ctx);
3857             }
3858         }
3859         tcg_gen_exit_tb(0);
3860     }
3861 }
3862
3863 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
3864 {
3865     if (NARROW_MODE(ctx)) {
3866         nip = (uint32_t)nip;
3867     }
3868     tcg_gen_movi_tl(cpu_lr, nip);
3869 }
3870
3871 /* b ba bl bla */
3872 static void gen_b(DisasContext *ctx)
3873 {
3874     target_ulong li, target;
3875
3876     ctx->exception = POWERPC_EXCP_BRANCH;
3877     /* sign extend LI */
3878     li = LI(ctx->opcode);
3879     li = (li ^ 0x02000000) - 0x02000000;
3880     if (likely(AA(ctx->opcode) == 0)) {
3881         target = ctx->nip + li - 4;
3882     } else {
3883         target = li;
3884     }
3885     if (LK(ctx->opcode)) {
3886         gen_setlr(ctx, ctx->nip);
3887     }
3888     gen_update_cfar(ctx, ctx->nip);
3889     gen_goto_tb(ctx, 0, target);
3890 }
3891
3892 #define BCOND_IM  0
3893 #define BCOND_LR  1
3894 #define BCOND_CTR 2
3895 #define BCOND_TAR 3
3896
3897 static inline void gen_bcond(DisasContext *ctx, int type)
3898 {
3899     uint32_t bo = BO(ctx->opcode);
3900     TCGLabel *l1;
3901     TCGv target;
3902
3903     ctx->exception = POWERPC_EXCP_BRANCH;
3904     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3905         target = tcg_temp_local_new();
3906         if (type == BCOND_CTR)
3907             tcg_gen_mov_tl(target, cpu_ctr);
3908         else if (type == BCOND_TAR)
3909             gen_load_spr(target, SPR_TAR);
3910         else
3911             tcg_gen_mov_tl(target, cpu_lr);
3912     } else {
3913         TCGV_UNUSED(target);
3914     }
3915     if (LK(ctx->opcode))
3916         gen_setlr(ctx, ctx->nip);
3917     l1 = gen_new_label();
3918     if ((bo & 0x4) == 0) {
3919         /* Decrement and test CTR */
3920         TCGv temp = tcg_temp_new();
3921         if (unlikely(type == BCOND_CTR)) {
3922             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3923             return;
3924         }
3925         tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3926         if (NARROW_MODE(ctx)) {
3927             tcg_gen_ext32u_tl(temp, cpu_ctr);
3928         } else {
3929             tcg_gen_mov_tl(temp, cpu_ctr);
3930         }
3931         if (bo & 0x2) {
3932             tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3933         } else {
3934             tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3935         }
3936         tcg_temp_free(temp);
3937     }
3938     if ((bo & 0x10) == 0) {
3939         /* Test CR */
3940         uint32_t bi = BI(ctx->opcode);
3941         uint32_t mask = 0x08 >> (bi & 0x03);
3942         TCGv_i32 temp = tcg_temp_new_i32();
3943
3944         if (bo & 0x8) {
3945             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3946             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3947         } else {
3948             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3949             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3950         }
3951         tcg_temp_free_i32(temp);
3952     }
3953     gen_update_cfar(ctx, ctx->nip);
3954     if (type == BCOND_IM) {
3955         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3956         if (likely(AA(ctx->opcode) == 0)) {
3957             gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3958         } else {
3959             gen_goto_tb(ctx, 0, li);
3960         }
3961         gen_set_label(l1);
3962         gen_goto_tb(ctx, 1, ctx->nip);
3963     } else {
3964         if (NARROW_MODE(ctx)) {
3965             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3966         } else {
3967             tcg_gen_andi_tl(cpu_nip, target, ~3);
3968         }
3969         tcg_gen_exit_tb(0);
3970         gen_set_label(l1);
3971         gen_update_nip(ctx, ctx->nip);
3972         tcg_gen_exit_tb(0);
3973     }
3974     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3975         tcg_temp_free(target);
3976     }
3977 }
3978
3979 static void gen_bc(DisasContext *ctx)
3980 {
3981     gen_bcond(ctx, BCOND_IM);
3982 }
3983
3984 static void gen_bcctr(DisasContext *ctx)
3985 {
3986     gen_bcond(ctx, BCOND_CTR);
3987 }
3988
3989 static void gen_bclr(DisasContext *ctx)
3990 {
3991     gen_bcond(ctx, BCOND_LR);
3992 }
3993
3994 static void gen_bctar(DisasContext *ctx)
3995 {
3996     gen_bcond(ctx, BCOND_TAR);
3997 }
3998
3999 /***                      Condition register logical                       ***/
4000 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
4001 static void glue(gen_, name)(DisasContext *ctx)                                       \
4002 {                                                                             \
4003     uint8_t bitmask;                                                          \
4004     int sh;                                                                   \
4005     TCGv_i32 t0, t1;                                                          \
4006     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
4007     t0 = tcg_temp_new_i32();                                                  \
4008     if (sh > 0)                                                               \
4009         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
4010     else if (sh < 0)                                                          \
4011         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
4012     else                                                                      \
4013         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
4014     t1 = tcg_temp_new_i32();                                                  \
4015     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
4016     if (sh > 0)                                                               \
4017         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
4018     else if (sh < 0)                                                          \
4019         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
4020     else                                                                      \
4021         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
4022     tcg_op(t0, t0, t1);                                                       \
4023     bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03);                             \
4024     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
4025     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
4026     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
4027     tcg_temp_free_i32(t0);                                                    \
4028     tcg_temp_free_i32(t1);                                                    \
4029 }
4030
4031 /* crand */
4032 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
4033 /* crandc */
4034 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
4035 /* creqv */
4036 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
4037 /* crnand */
4038 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
4039 /* crnor */
4040 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
4041 /* cror */
4042 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
4043 /* crorc */
4044 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
4045 /* crxor */
4046 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
4047
4048 /* mcrf */
4049 static void gen_mcrf(DisasContext *ctx)
4050 {
4051     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
4052 }
4053
4054 /***                           System linkage                              ***/
4055
4056 /* rfi (supervisor only) */
4057 static void gen_rfi(DisasContext *ctx)
4058 {
4059 #if defined(CONFIG_USER_ONLY)
4060     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4061 #else
4062     /* Restore CPU state */
4063     if (unlikely(ctx->pr)) {
4064         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4065         return;
4066     }
4067     gen_update_cfar(ctx, ctx->nip);
4068     gen_helper_rfi(cpu_env);
4069     gen_sync_exception(ctx);
4070 #endif
4071 }
4072
4073 #if defined(TARGET_PPC64)
4074 static void gen_rfid(DisasContext *ctx)
4075 {
4076 #if defined(CONFIG_USER_ONLY)
4077     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4078 #else
4079     /* Restore CPU state */
4080     if (unlikely(ctx->pr)) {
4081         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4082         return;
4083     }
4084     gen_update_cfar(ctx, ctx->nip);
4085     gen_helper_rfid(cpu_env);
4086     gen_sync_exception(ctx);
4087 #endif
4088 }
4089
4090 static void gen_hrfid(DisasContext *ctx)
4091 {
4092 #if defined(CONFIG_USER_ONLY)
4093     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4094 #else
4095     /* Restore CPU state */
4096     if (unlikely(!ctx->hv)) {
4097         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4098         return;
4099     }
4100     gen_helper_hrfid(cpu_env);
4101     gen_sync_exception(ctx);
4102 #endif
4103 }
4104 #endif
4105
4106 /* sc */
4107 #if defined(CONFIG_USER_ONLY)
4108 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4109 #else
4110 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4111 #endif
4112 static void gen_sc(DisasContext *ctx)
4113 {
4114     uint32_t lev;
4115
4116     lev = (ctx->opcode >> 5) & 0x7F;
4117     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4118 }
4119
4120 /***                                Trap                                   ***/
4121
4122 /* tw */
4123 static void gen_tw(DisasContext *ctx)
4124 {
4125     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4126     /* Update the nip since this might generate a trap exception */
4127     gen_update_nip(ctx, ctx->nip);
4128     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4129                   t0);
4130     tcg_temp_free_i32(t0);
4131 }
4132
4133 /* twi */
4134 static void gen_twi(DisasContext *ctx)
4135 {
4136     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4137     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4138     /* Update the nip since this might generate a trap exception */
4139     gen_update_nip(ctx, ctx->nip);
4140     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4141     tcg_temp_free(t0);
4142     tcg_temp_free_i32(t1);
4143 }
4144
4145 #if defined(TARGET_PPC64)
4146 /* td */
4147 static void gen_td(DisasContext *ctx)
4148 {
4149     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
4150     /* Update the nip since this might generate a trap exception */
4151     gen_update_nip(ctx, ctx->nip);
4152     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4153                   t0);
4154     tcg_temp_free_i32(t0);
4155 }
4156
4157 /* tdi */
4158 static void gen_tdi(DisasContext *ctx)
4159 {
4160     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
4161     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
4162     /* Update the nip since this might generate a trap exception */
4163     gen_update_nip(ctx, ctx->nip);
4164     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4165     tcg_temp_free(t0);
4166     tcg_temp_free_i32(t1);
4167 }
4168 #endif
4169
4170 /***                          Processor control                            ***/
4171
4172 static void gen_read_xer(TCGv dst)
4173 {
4174     TCGv t0 = tcg_temp_new();
4175     TCGv t1 = tcg_temp_new();
4176     TCGv t2 = tcg_temp_new();
4177     tcg_gen_mov_tl(dst, cpu_xer);
4178     tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4179     tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4180     tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4181     tcg_gen_or_tl(t0, t0, t1);
4182     tcg_gen_or_tl(dst, dst, t2);
4183     tcg_gen_or_tl(dst, dst, t0);
4184     tcg_temp_free(t0);
4185     tcg_temp_free(t1);
4186     tcg_temp_free(t2);
4187 }
4188
4189 static void gen_write_xer(TCGv src)
4190 {
4191     tcg_gen_andi_tl(cpu_xer, src,
4192                     ~((1u << XER_SO) | (1u << XER_OV) | (1u << XER_CA)));
4193     tcg_gen_shri_tl(cpu_so, src, XER_SO);
4194     tcg_gen_shri_tl(cpu_ov, src, XER_OV);
4195     tcg_gen_shri_tl(cpu_ca, src, XER_CA);
4196     tcg_gen_andi_tl(cpu_so, cpu_so, 1);
4197     tcg_gen_andi_tl(cpu_ov, cpu_ov, 1);
4198     tcg_gen_andi_tl(cpu_ca, cpu_ca, 1);
4199 }
4200
4201 /* mcrxr */
4202 static void gen_mcrxr(DisasContext *ctx)
4203 {
4204     TCGv_i32 t0 = tcg_temp_new_i32();
4205     TCGv_i32 t1 = tcg_temp_new_i32();
4206     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4207
4208     tcg_gen_trunc_tl_i32(t0, cpu_so);
4209     tcg_gen_trunc_tl_i32(t1, cpu_ov);
4210     tcg_gen_trunc_tl_i32(dst, cpu_ca);
4211     tcg_gen_shli_i32(t0, t0, 3);
4212     tcg_gen_shli_i32(t1, t1, 2);
4213     tcg_gen_shli_i32(dst, dst, 1);
4214     tcg_gen_or_i32(dst, dst, t0);
4215     tcg_gen_or_i32(dst, dst, t1);
4216     tcg_temp_free_i32(t0);
4217     tcg_temp_free_i32(t1);
4218
4219     tcg_gen_movi_tl(cpu_so, 0);
4220     tcg_gen_movi_tl(cpu_ov, 0);
4221     tcg_gen_movi_tl(cpu_ca, 0);
4222 }
4223
4224 /* mfcr mfocrf */
4225 static void gen_mfcr(DisasContext *ctx)
4226 {
4227     uint32_t crm, crn;
4228
4229     if (likely(ctx->opcode & 0x00100000)) {
4230         crm = CRM(ctx->opcode);
4231         if (likely(crm && ((crm & (crm - 1)) == 0))) {
4232             crn = ctz32 (crm);
4233             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4234             tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4235                             cpu_gpr[rD(ctx->opcode)], crn * 4);
4236         }
4237     } else {
4238         TCGv_i32 t0 = tcg_temp_new_i32();
4239         tcg_gen_mov_i32(t0, cpu_crf[0]);
4240         tcg_gen_shli_i32(t0, t0, 4);
4241         tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4242         tcg_gen_shli_i32(t0, t0, 4);
4243         tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4244         tcg_gen_shli_i32(t0, t0, 4);
4245         tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4246         tcg_gen_shli_i32(t0, t0, 4);
4247         tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4248         tcg_gen_shli_i32(t0, t0, 4);
4249         tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4250         tcg_gen_shli_i32(t0, t0, 4);
4251         tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4252         tcg_gen_shli_i32(t0, t0, 4);
4253         tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4254         tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4255         tcg_temp_free_i32(t0);
4256     }
4257 }
4258
4259 /* mfmsr */
4260 static void gen_mfmsr(DisasContext *ctx)
4261 {
4262 #if defined(CONFIG_USER_ONLY)
4263     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4264 #else
4265     if (unlikely(ctx->pr)) {
4266         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4267         return;
4268     }
4269     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4270 #endif
4271 }
4272
4273 static void spr_noaccess(DisasContext *ctx, int gprn, int sprn)
4274 {
4275 #if 0
4276     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4277     printf("ERROR: try to access SPR %d !\n", sprn);
4278 #endif
4279 }
4280 #define SPR_NOACCESS (&spr_noaccess)
4281
4282 /* mfspr */
4283 static inline void gen_op_mfspr(DisasContext *ctx)
4284 {
4285     void (*read_cb)(DisasContext *ctx, int gprn, int sprn);
4286     uint32_t sprn = SPR(ctx->opcode);
4287
4288 #if defined(CONFIG_USER_ONLY)
4289     read_cb = ctx->spr_cb[sprn].uea_read;
4290 #else
4291     if (ctx->pr) {
4292         read_cb = ctx->spr_cb[sprn].uea_read;
4293     } else if (ctx->hv) {
4294         read_cb = ctx->spr_cb[sprn].hea_read;
4295     } else {
4296         read_cb = ctx->spr_cb[sprn].oea_read;
4297     }
4298 #endif
4299     if (likely(read_cb != NULL)) {
4300         if (likely(read_cb != SPR_NOACCESS)) {
4301             (*read_cb)(ctx, rD(ctx->opcode), sprn);
4302         } else {
4303             /* Privilege exception */
4304             /* This is a hack to avoid warnings when running Linux:
4305              * this OS breaks the PowerPC virtualisation model,
4306              * allowing userland application to read the PVR
4307              */
4308             if (sprn != SPR_PVR) {
4309                 fprintf(stderr, "Trying to read privileged spr %d (0x%03x) at "
4310                         TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4311                 if (qemu_log_separate()) {
4312                     qemu_log("Trying to read privileged spr %d (0x%03x) at "
4313                              TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4314                 }
4315             }
4316             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4317         }
4318     } else {
4319         /* Not defined */
4320         fprintf(stderr, "Trying to read invalid spr %d (0x%03x) at "
4321                 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4322         if (qemu_log_separate()) {
4323             qemu_log("Trying to read invalid spr %d (0x%03x) at "
4324                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4325         }
4326         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4327     }
4328 }
4329
4330 static void gen_mfspr(DisasContext *ctx)
4331 {
4332     gen_op_mfspr(ctx);
4333 }
4334
4335 /* mftb */
4336 static void gen_mftb(DisasContext *ctx)
4337 {
4338     gen_op_mfspr(ctx);
4339 }
4340
4341 /* mtcrf mtocrf*/
4342 static void gen_mtcrf(DisasContext *ctx)
4343 {
4344     uint32_t crm, crn;
4345
4346     crm = CRM(ctx->opcode);
4347     if (likely((ctx->opcode & 0x00100000))) {
4348         if (crm && ((crm & (crm - 1)) == 0)) {
4349             TCGv_i32 temp = tcg_temp_new_i32();
4350             crn = ctz32 (crm);
4351             tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4352             tcg_gen_shri_i32(temp, temp, crn * 4);
4353             tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4354             tcg_temp_free_i32(temp);
4355         }
4356     } else {
4357         TCGv_i32 temp = tcg_temp_new_i32();
4358         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4359         for (crn = 0 ; crn < 8 ; crn++) {
4360             if (crm & (1 << crn)) {
4361                     tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4362                     tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4363             }
4364         }
4365         tcg_temp_free_i32(temp);
4366     }
4367 }
4368
4369 /* mtmsr */
4370 #if defined(TARGET_PPC64)
4371 static void gen_mtmsrd(DisasContext *ctx)
4372 {
4373 #if defined(CONFIG_USER_ONLY)
4374     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4375 #else
4376     if (unlikely(ctx->pr)) {
4377         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4378         return;
4379     }
4380     if (ctx->opcode & 0x00010000) {
4381         /* Special form that does not need any synchronisation */
4382         TCGv t0 = tcg_temp_new();
4383         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4384         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4385         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4386         tcg_temp_free(t0);
4387     } else {
4388         /* XXX: we need to update nip before the store
4389          *      if we enter power saving mode, we will exit the loop
4390          *      directly from ppc_store_msr
4391          */
4392         gen_update_nip(ctx, ctx->nip);
4393         gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4394         /* Must stop the translation as machine state (may have) changed */
4395         /* Note that mtmsr is not always defined as context-synchronizing */
4396         gen_stop_exception(ctx);
4397     }
4398 #endif
4399 }
4400 #endif
4401
4402 static void gen_mtmsr(DisasContext *ctx)
4403 {
4404 #if defined(CONFIG_USER_ONLY)
4405     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4406 #else
4407     if (unlikely(ctx->pr)) {
4408         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4409         return;
4410     }
4411     if (ctx->opcode & 0x00010000) {
4412         /* Special form that does not need any synchronisation */
4413         TCGv t0 = tcg_temp_new();
4414         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
4415         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
4416         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4417         tcg_temp_free(t0);
4418     } else {
4419         TCGv msr = tcg_temp_new();
4420
4421         /* XXX: we need to update nip before the store
4422          *      if we enter power saving mode, we will exit the loop
4423          *      directly from ppc_store_msr
4424          */
4425         gen_update_nip(ctx, ctx->nip);
4426 #if defined(TARGET_PPC64)
4427         tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4428 #else
4429         tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4430 #endif
4431         gen_helper_store_msr(cpu_env, msr);
4432         tcg_temp_free(msr);
4433         /* Must stop the translation as machine state (may have) changed */
4434         /* Note that mtmsr is not always defined as context-synchronizing */
4435         gen_stop_exception(ctx);
4436     }
4437 #endif
4438 }
4439
4440 /* mtspr */
4441 static void gen_mtspr(DisasContext *ctx)
4442 {
4443     void (*write_cb)(DisasContext *ctx, int sprn, int gprn);
4444     uint32_t sprn = SPR(ctx->opcode);
4445
4446 #if defined(CONFIG_USER_ONLY)
4447     write_cb = ctx->spr_cb[sprn].uea_write;
4448 #else
4449     if (ctx->pr) {
4450         write_cb = ctx->spr_cb[sprn].uea_write;
4451     } else if (ctx->hv) {
4452         write_cb = ctx->spr_cb[sprn].hea_write;
4453     } else {
4454         write_cb = ctx->spr_cb[sprn].oea_write;
4455     }
4456 #endif
4457     if (likely(write_cb != NULL)) {
4458         if (likely(write_cb != SPR_NOACCESS)) {
4459             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4460         } else {
4461             /* Privilege exception */
4462             fprintf(stderr, "Trying to write privileged spr %d (0x%03x) at "
4463                     TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4464             if (qemu_log_separate()) {
4465                 qemu_log("Trying to write privileged spr %d (0x%03x) at "
4466                          TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4467             }
4468             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4469         }
4470     } else {
4471         /* Not defined */
4472         if (qemu_log_separate()) {
4473             qemu_log("Trying to write invalid spr %d (0x%03x) at "
4474                      TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4475         }
4476         fprintf(stderr, "Trying to write invalid spr %d (0x%03x) at "
4477                 TARGET_FMT_lx "\n", sprn, sprn, ctx->nip - 4);
4478         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4479     }
4480 }
4481
4482 /***                         Cache management                              ***/
4483
4484 /* dcbf */
4485 static void gen_dcbf(DisasContext *ctx)
4486 {
4487     /* XXX: specification says this is treated as a load by the MMU */
4488     TCGv t0;
4489     gen_set_access_type(ctx, ACCESS_CACHE);
4490     t0 = tcg_temp_new();
4491     gen_addr_reg_index(ctx, t0);
4492     gen_qemu_ld8u(ctx, t0, t0);
4493     tcg_temp_free(t0);
4494 }
4495
4496 /* dcbi (Supervisor only) */
4497 static void gen_dcbi(DisasContext *ctx)
4498 {
4499 #if defined(CONFIG_USER_ONLY)
4500     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4501 #else
4502     TCGv EA, val;
4503     if (unlikely(ctx->pr)) {
4504         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4505         return;
4506     }
4507     EA = tcg_temp_new();
4508     gen_set_access_type(ctx, ACCESS_CACHE);
4509     gen_addr_reg_index(ctx, EA);
4510     val = tcg_temp_new();
4511     /* XXX: specification says this should be treated as a store by the MMU */
4512     gen_qemu_ld8u(ctx, val, EA);
4513     gen_qemu_st8(ctx, val, EA);
4514     tcg_temp_free(val);
4515     tcg_temp_free(EA);
4516 #endif
4517 }
4518
4519 /* dcdst */
4520 static void gen_dcbst(DisasContext *ctx)
4521 {
4522     /* XXX: specification say this is treated as a load by the MMU */
4523     TCGv t0;
4524     gen_set_access_type(ctx, ACCESS_CACHE);
4525     t0 = tcg_temp_new();
4526     gen_addr_reg_index(ctx, t0);
4527     gen_qemu_ld8u(ctx, t0, t0);
4528     tcg_temp_free(t0);
4529 }
4530
4531 /* dcbt */
4532 static void gen_dcbt(DisasContext *ctx)
4533 {
4534     /* interpreted as no-op */
4535     /* XXX: specification say this is treated as a load by the MMU
4536      *      but does not generate any exception
4537      */
4538 }
4539
4540 /* dcbtst */
4541 static void gen_dcbtst(DisasContext *ctx)
4542 {
4543     /* interpreted as no-op */
4544     /* XXX: specification say this is treated as a load by the MMU
4545      *      but does not generate any exception
4546      */
4547 }
4548
4549 /* dcbtls */
4550 static void gen_dcbtls(DisasContext *ctx)
4551 {
4552     /* Always fails locking the cache */
4553     TCGv t0 = tcg_temp_new();
4554     gen_load_spr(t0, SPR_Exxx_L1CSR0);
4555     tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
4556     gen_store_spr(SPR_Exxx_L1CSR0, t0);
4557     tcg_temp_free(t0);
4558 }
4559
4560 /* dcbz */
4561 static void gen_dcbz(DisasContext *ctx)
4562 {
4563     TCGv tcgv_addr;
4564     TCGv_i32 tcgv_is_dcbzl;
4565     int is_dcbzl = ctx->opcode & 0x00200000 ? 1 : 0;
4566
4567     gen_set_access_type(ctx, ACCESS_CACHE);
4568     /* NIP cannot be restored if the memory exception comes from an helper */
4569     gen_update_nip(ctx, ctx->nip - 4);
4570     tcgv_addr = tcg_temp_new();
4571     tcgv_is_dcbzl = tcg_const_i32(is_dcbzl);
4572
4573     gen_addr_reg_index(ctx, tcgv_addr);
4574     gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_is_dcbzl);
4575
4576     tcg_temp_free(tcgv_addr);
4577     tcg_temp_free_i32(tcgv_is_dcbzl);
4578 }
4579
4580 /* dst / dstt */
4581 static void gen_dst(DisasContext *ctx)
4582 {
4583     if (rA(ctx->opcode) == 0) {
4584         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4585     } else {
4586         /* interpreted as no-op */
4587     }
4588 }
4589
4590 /* dstst /dststt */
4591 static void gen_dstst(DisasContext *ctx)
4592 {
4593     if (rA(ctx->opcode) == 0) {
4594         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
4595     } else {
4596         /* interpreted as no-op */
4597     }
4598
4599 }
4600
4601 /* dss / dssall */
4602 static void gen_dss(DisasContext *ctx)
4603 {
4604     /* interpreted as no-op */
4605 }
4606
4607 /* icbi */
4608 static void gen_icbi(DisasContext *ctx)
4609 {
4610     TCGv t0;
4611     gen_set_access_type(ctx, ACCESS_CACHE);
4612     /* NIP cannot be restored if the memory exception comes from an helper */
4613     gen_update_nip(ctx, ctx->nip - 4);
4614     t0 = tcg_temp_new();
4615     gen_addr_reg_index(ctx, t0);
4616     gen_helper_icbi(cpu_env, t0);
4617     tcg_temp_free(t0);
4618 }
4619
4620 /* Optional: */
4621 /* dcba */
4622 static void gen_dcba(DisasContext *ctx)
4623 {
4624     /* interpreted as no-op */
4625     /* XXX: specification say this is treated as a store by the MMU
4626      *      but does not generate any exception
4627      */
4628 }
4629
4630 /***                    Segment register manipulation                      ***/
4631 /* Supervisor only: */
4632
4633 /* mfsr */
4634 static void gen_mfsr(DisasContext *ctx)
4635 {
4636 #if defined(CONFIG_USER_ONLY)
4637     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4638 #else
4639     TCGv t0;
4640     if (unlikely(ctx->pr)) {
4641         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4642         return;
4643     }
4644     t0 = tcg_const_tl(SR(ctx->opcode));
4645     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4646     tcg_temp_free(t0);
4647 #endif
4648 }
4649
4650 /* mfsrin */
4651 static void gen_mfsrin(DisasContext *ctx)
4652 {
4653 #if defined(CONFIG_USER_ONLY)
4654     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4655 #else
4656     TCGv t0;
4657     if (unlikely(ctx->pr)) {
4658         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4659         return;
4660     }
4661     t0 = tcg_temp_new();
4662     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4663     tcg_gen_andi_tl(t0, t0, 0xF);
4664     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4665     tcg_temp_free(t0);
4666 #endif
4667 }
4668
4669 /* mtsr */
4670 static void gen_mtsr(DisasContext *ctx)
4671 {
4672 #if defined(CONFIG_USER_ONLY)
4673     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4674 #else
4675     TCGv t0;
4676     if (unlikely(ctx->pr)) {
4677         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4678         return;
4679     }
4680     t0 = tcg_const_tl(SR(ctx->opcode));
4681     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4682     tcg_temp_free(t0);
4683 #endif
4684 }
4685
4686 /* mtsrin */
4687 static void gen_mtsrin(DisasContext *ctx)
4688 {
4689 #if defined(CONFIG_USER_ONLY)
4690     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4691 #else
4692     TCGv t0;
4693     if (unlikely(ctx->pr)) {
4694         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4695         return;
4696     }
4697     t0 = tcg_temp_new();
4698     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4699     tcg_gen_andi_tl(t0, t0, 0xF);
4700     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
4701     tcg_temp_free(t0);
4702 #endif
4703 }
4704
4705 #if defined(TARGET_PPC64)
4706 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4707
4708 /* mfsr */
4709 static void gen_mfsr_64b(DisasContext *ctx)
4710 {
4711 #if defined(CONFIG_USER_ONLY)
4712     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4713 #else
4714     TCGv t0;
4715     if (unlikely(ctx->pr)) {
4716         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4717         return;
4718     }
4719     t0 = tcg_const_tl(SR(ctx->opcode));
4720     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4721     tcg_temp_free(t0);
4722 #endif
4723 }
4724
4725 /* mfsrin */
4726 static void gen_mfsrin_64b(DisasContext *ctx)
4727 {
4728 #if defined(CONFIG_USER_ONLY)
4729     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4730 #else
4731     TCGv t0;
4732     if (unlikely(ctx->pr)) {
4733         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4734         return;
4735     }
4736     t0 = tcg_temp_new();
4737     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4738     tcg_gen_andi_tl(t0, t0, 0xF);
4739     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4740     tcg_temp_free(t0);
4741 #endif
4742 }
4743
4744 /* mtsr */
4745 static void gen_mtsr_64b(DisasContext *ctx)
4746 {
4747 #if defined(CONFIG_USER_ONLY)
4748     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4749 #else
4750     TCGv t0;
4751     if (unlikely(ctx->pr)) {
4752         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4753         return;
4754     }
4755     t0 = tcg_const_tl(SR(ctx->opcode));
4756     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4757     tcg_temp_free(t0);
4758 #endif
4759 }
4760
4761 /* mtsrin */
4762 static void gen_mtsrin_64b(DisasContext *ctx)
4763 {
4764 #if defined(CONFIG_USER_ONLY)
4765     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4766 #else
4767     TCGv t0;
4768     if (unlikely(ctx->pr)) {
4769         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4770         return;
4771     }
4772     t0 = tcg_temp_new();
4773     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4774     tcg_gen_andi_tl(t0, t0, 0xF);
4775     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4776     tcg_temp_free(t0);
4777 #endif
4778 }
4779
4780 /* slbmte */
4781 static void gen_slbmte(DisasContext *ctx)
4782 {
4783 #if defined(CONFIG_USER_ONLY)
4784     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4785 #else
4786     if (unlikely(ctx->pr)) {
4787         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4788         return;
4789     }
4790     gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4791                          cpu_gpr[rS(ctx->opcode)]);
4792 #endif
4793 }
4794
4795 static void gen_slbmfee(DisasContext *ctx)
4796 {
4797 #if defined(CONFIG_USER_ONLY)
4798     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4799 #else
4800     if (unlikely(ctx->pr)) {
4801         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4802         return;
4803     }
4804     gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4805                              cpu_gpr[rB(ctx->opcode)]);
4806 #endif
4807 }
4808
4809 static void gen_slbmfev(DisasContext *ctx)
4810 {
4811 #if defined(CONFIG_USER_ONLY)
4812     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4813 #else
4814     if (unlikely(ctx->pr)) {
4815         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4816         return;
4817     }
4818     gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4819                              cpu_gpr[rB(ctx->opcode)]);
4820 #endif
4821 }
4822 #endif /* defined(TARGET_PPC64) */
4823
4824 /***                      Lookaside buffer management                      ***/
4825 /* Optional & supervisor only: */
4826
4827 /* tlbia */
4828 static void gen_tlbia(DisasContext *ctx)
4829 {
4830 #if defined(CONFIG_USER_ONLY)
4831     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4832 #else
4833     if (unlikely(ctx->pr)) {
4834         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4835         return;
4836     }
4837     gen_helper_tlbia(cpu_env);
4838 #endif
4839 }
4840
4841 /* tlbiel */
4842 static void gen_tlbiel(DisasContext *ctx)
4843 {
4844 #if defined(CONFIG_USER_ONLY)
4845     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4846 #else
4847     if (unlikely(ctx->pr)) {
4848         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4849         return;
4850     }
4851     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4852 #endif
4853 }
4854
4855 /* tlbie */
4856 static void gen_tlbie(DisasContext *ctx)
4857 {
4858 #if defined(CONFIG_USER_ONLY)
4859     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4860 #else
4861     if (unlikely(ctx->pr || !ctx->hv)) {
4862         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4863         return;
4864     }
4865     if (NARROW_MODE(ctx)) {
4866         TCGv t0 = tcg_temp_new();
4867         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4868         gen_helper_tlbie(cpu_env, t0);
4869         tcg_temp_free(t0);
4870     } else {
4871         gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4872     }
4873 #endif
4874 }
4875
4876 /* tlbsync */
4877 static void gen_tlbsync(DisasContext *ctx)
4878 {
4879 #if defined(CONFIG_USER_ONLY)
4880     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4881 #else
4882     if (unlikely(ctx->pr || !ctx->hv)) {
4883         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4884         return;
4885     }
4886     /* tlbsync is a nop for server, ptesync handles delayed tlb flush,
4887      * embedded however needs to deal with tlbsync. We don't try to be
4888      * fancy and swallow the overhead of checking for both.
4889      */
4890     gen_check_tlb_flush(ctx);
4891 #endif
4892 }
4893
4894 #if defined(TARGET_PPC64)
4895 /* slbia */
4896 static void gen_slbia(DisasContext *ctx)
4897 {
4898 #if defined(CONFIG_USER_ONLY)
4899     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4900 #else
4901     if (unlikely(ctx->pr || !ctx->hv)) {
4902         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4903         return;
4904     }
4905     gen_helper_slbia(cpu_env);
4906 #endif
4907 }
4908
4909 /* slbie */
4910 static void gen_slbie(DisasContext *ctx)
4911 {
4912 #if defined(CONFIG_USER_ONLY)
4913     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4914 #else
4915     if (unlikely(ctx->pr)) {
4916         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4917         return;
4918     }
4919     gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4920 #endif
4921 }
4922 #endif
4923
4924 /***                              External control                         ***/
4925 /* Optional: */
4926
4927 /* eciwx */
4928 static void gen_eciwx(DisasContext *ctx)
4929 {
4930     TCGv t0;
4931     /* Should check EAR[E] ! */
4932     gen_set_access_type(ctx, ACCESS_EXT);
4933     t0 = tcg_temp_new();
4934     gen_addr_reg_index(ctx, t0);
4935     gen_check_align(ctx, t0, 0x03);
4936     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4937     tcg_temp_free(t0);
4938 }
4939
4940 /* ecowx */
4941 static void gen_ecowx(DisasContext *ctx)
4942 {
4943     TCGv t0;
4944     /* Should check EAR[E] ! */
4945     gen_set_access_type(ctx, ACCESS_EXT);
4946     t0 = tcg_temp_new();
4947     gen_addr_reg_index(ctx, t0);
4948     gen_check_align(ctx, t0, 0x03);
4949     gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4950     tcg_temp_free(t0);
4951 }
4952
4953 /* PowerPC 601 specific instructions */
4954
4955 /* abs - abs. */
4956 static void gen_abs(DisasContext *ctx)
4957 {
4958     TCGLabel *l1 = gen_new_label();
4959     TCGLabel *l2 = gen_new_label();
4960     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4961     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4962     tcg_gen_br(l2);
4963     gen_set_label(l1);
4964     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4965     gen_set_label(l2);
4966     if (unlikely(Rc(ctx->opcode) != 0))
4967         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4968 }
4969
4970 /* abso - abso. */
4971 static void gen_abso(DisasContext *ctx)
4972 {
4973     TCGLabel *l1 = gen_new_label();
4974     TCGLabel *l2 = gen_new_label();
4975     TCGLabel *l3 = gen_new_label();
4976     /* Start with XER OV disabled, the most likely case */
4977     tcg_gen_movi_tl(cpu_ov, 0);
4978     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4979     tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4980     tcg_gen_movi_tl(cpu_ov, 1);
4981     tcg_gen_movi_tl(cpu_so, 1);
4982     tcg_gen_br(l2);
4983     gen_set_label(l1);
4984     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4985     tcg_gen_br(l3);
4986     gen_set_label(l2);
4987     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4988     gen_set_label(l3);
4989     if (unlikely(Rc(ctx->opcode) != 0))
4990         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4991 }
4992
4993 /* clcs */
4994 static void gen_clcs(DisasContext *ctx)
4995 {
4996     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4997     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4998     tcg_temp_free_i32(t0);
4999     /* Rc=1 sets CR0 to an undefined state */
5000 }
5001
5002 /* div - div. */
5003 static void gen_div(DisasContext *ctx)
5004 {
5005     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5006                    cpu_gpr[rB(ctx->opcode)]);
5007     if (unlikely(Rc(ctx->opcode) != 0))
5008         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5009 }
5010
5011 /* divo - divo. */
5012 static void gen_divo(DisasContext *ctx)
5013 {
5014     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5015                     cpu_gpr[rB(ctx->opcode)]);
5016     if (unlikely(Rc(ctx->opcode) != 0))
5017         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5018 }
5019
5020 /* divs - divs. */
5021 static void gen_divs(DisasContext *ctx)
5022 {
5023     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5024                     cpu_gpr[rB(ctx->opcode)]);
5025     if (unlikely(Rc(ctx->opcode) != 0))
5026         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5027 }
5028
5029 /* divso - divso. */
5030 static void gen_divso(DisasContext *ctx)
5031 {
5032     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
5033                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5034     if (unlikely(Rc(ctx->opcode) != 0))
5035         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5036 }
5037
5038 /* doz - doz. */
5039 static void gen_doz(DisasContext *ctx)
5040 {
5041     TCGLabel *l1 = gen_new_label();
5042     TCGLabel *l2 = gen_new_label();
5043     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
5044     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5045     tcg_gen_br(l2);
5046     gen_set_label(l1);
5047     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5048     gen_set_label(l2);
5049     if (unlikely(Rc(ctx->opcode) != 0))
5050         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5051 }
5052
5053 /* dozo - dozo. */
5054 static void gen_dozo(DisasContext *ctx)
5055 {
5056     TCGLabel *l1 = gen_new_label();
5057     TCGLabel *l2 = gen_new_label();
5058     TCGv t0 = tcg_temp_new();
5059     TCGv t1 = tcg_temp_new();
5060     TCGv t2 = tcg_temp_new();
5061     /* Start with XER OV disabled, the most likely case */
5062     tcg_gen_movi_tl(cpu_ov, 0);
5063     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
5064     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5065     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5066     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
5067     tcg_gen_andc_tl(t1, t1, t2);
5068     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
5069     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5070     tcg_gen_movi_tl(cpu_ov, 1);
5071     tcg_gen_movi_tl(cpu_so, 1);
5072     tcg_gen_br(l2);
5073     gen_set_label(l1);
5074     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5075     gen_set_label(l2);
5076     tcg_temp_free(t0);
5077     tcg_temp_free(t1);
5078     tcg_temp_free(t2);
5079     if (unlikely(Rc(ctx->opcode) != 0))
5080         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5081 }
5082
5083 /* dozi */
5084 static void gen_dozi(DisasContext *ctx)
5085 {
5086     target_long simm = SIMM(ctx->opcode);
5087     TCGLabel *l1 = gen_new_label();
5088     TCGLabel *l2 = gen_new_label();
5089     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
5090     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5091     tcg_gen_br(l2);
5092     gen_set_label(l1);
5093     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5094     gen_set_label(l2);
5095     if (unlikely(Rc(ctx->opcode) != 0))
5096         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5097 }
5098
5099 /* lscbx - lscbx. */
5100 static void gen_lscbx(DisasContext *ctx)
5101 {
5102     TCGv t0 = tcg_temp_new();
5103     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5104     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5105     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
5106
5107     gen_addr_reg_index(ctx, t0);
5108     /* NIP cannot be restored if the memory exception comes from an helper */
5109     gen_update_nip(ctx, ctx->nip - 4);
5110     gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
5111     tcg_temp_free_i32(t1);
5112     tcg_temp_free_i32(t2);
5113     tcg_temp_free_i32(t3);
5114     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5115     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
5116     if (unlikely(Rc(ctx->opcode) != 0))
5117         gen_set_Rc0(ctx, t0);
5118     tcg_temp_free(t0);
5119 }
5120
5121 /* maskg - maskg. */
5122 static void gen_maskg(DisasContext *ctx)
5123 {
5124     TCGLabel *l1 = gen_new_label();
5125     TCGv t0 = tcg_temp_new();
5126     TCGv t1 = tcg_temp_new();
5127     TCGv t2 = tcg_temp_new();
5128     TCGv t3 = tcg_temp_new();
5129     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5130     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5131     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5132     tcg_gen_addi_tl(t2, t0, 1);
5133     tcg_gen_shr_tl(t2, t3, t2);
5134     tcg_gen_shr_tl(t3, t3, t1);
5135     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5136     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5137     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5138     gen_set_label(l1);
5139     tcg_temp_free(t0);
5140     tcg_temp_free(t1);
5141     tcg_temp_free(t2);
5142     tcg_temp_free(t3);
5143     if (unlikely(Rc(ctx->opcode) != 0))
5144         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5145 }
5146
5147 /* maskir - maskir. */
5148 static void gen_maskir(DisasContext *ctx)
5149 {
5150     TCGv t0 = tcg_temp_new();
5151     TCGv t1 = tcg_temp_new();
5152     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5153     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5154     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5155     tcg_temp_free(t0);
5156     tcg_temp_free(t1);
5157     if (unlikely(Rc(ctx->opcode) != 0))
5158         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5159 }
5160
5161 /* mul - mul. */
5162 static void gen_mul(DisasContext *ctx)
5163 {
5164     TCGv_i64 t0 = tcg_temp_new_i64();
5165     TCGv_i64 t1 = tcg_temp_new_i64();
5166     TCGv t2 = tcg_temp_new();
5167     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5168     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5169     tcg_gen_mul_i64(t0, t0, t1);
5170     tcg_gen_trunc_i64_tl(t2, t0);
5171     gen_store_spr(SPR_MQ, t2);
5172     tcg_gen_shri_i64(t1, t0, 32);
5173     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5174     tcg_temp_free_i64(t0);
5175     tcg_temp_free_i64(t1);
5176     tcg_temp_free(t2);
5177     if (unlikely(Rc(ctx->opcode) != 0))
5178         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5179 }
5180
5181 /* mulo - mulo. */
5182 static void gen_mulo(DisasContext *ctx)
5183 {
5184     TCGLabel *l1 = gen_new_label();
5185     TCGv_i64 t0 = tcg_temp_new_i64();
5186     TCGv_i64 t1 = tcg_temp_new_i64();
5187     TCGv t2 = tcg_temp_new();
5188     /* Start with XER OV disabled, the most likely case */
5189     tcg_gen_movi_tl(cpu_ov, 0);
5190     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5191     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5192     tcg_gen_mul_i64(t0, t0, t1);
5193     tcg_gen_trunc_i64_tl(t2, t0);
5194     gen_store_spr(SPR_MQ, t2);
5195     tcg_gen_shri_i64(t1, t0, 32);
5196     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5197     tcg_gen_ext32s_i64(t1, t0);
5198     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
5199     tcg_gen_movi_tl(cpu_ov, 1);
5200     tcg_gen_movi_tl(cpu_so, 1);
5201     gen_set_label(l1);
5202     tcg_temp_free_i64(t0);
5203     tcg_temp_free_i64(t1);
5204     tcg_temp_free(t2);
5205     if (unlikely(Rc(ctx->opcode) != 0))
5206         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5207 }
5208
5209 /* nabs - nabs. */
5210 static void gen_nabs(DisasContext *ctx)
5211 {
5212     TCGLabel *l1 = gen_new_label();
5213     TCGLabel *l2 = gen_new_label();
5214     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5215     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5216     tcg_gen_br(l2);
5217     gen_set_label(l1);
5218     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5219     gen_set_label(l2);
5220     if (unlikely(Rc(ctx->opcode) != 0))
5221         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5222 }
5223
5224 /* nabso - nabso. */
5225 static void gen_nabso(DisasContext *ctx)
5226 {
5227     TCGLabel *l1 = gen_new_label();
5228     TCGLabel *l2 = gen_new_label();
5229     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
5230     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5231     tcg_gen_br(l2);
5232     gen_set_label(l1);
5233     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5234     gen_set_label(l2);
5235     /* nabs never overflows */
5236     tcg_gen_movi_tl(cpu_ov, 0);
5237     if (unlikely(Rc(ctx->opcode) != 0))
5238         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5239 }
5240
5241 /* rlmi - rlmi. */
5242 static void gen_rlmi(DisasContext *ctx)
5243 {
5244     uint32_t mb = MB(ctx->opcode);
5245     uint32_t me = ME(ctx->opcode);
5246     TCGv t0 = tcg_temp_new();
5247     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5248     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5249     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5250     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
5251     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5252     tcg_temp_free(t0);
5253     if (unlikely(Rc(ctx->opcode) != 0))
5254         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5255 }
5256
5257 /* rrib - rrib. */
5258 static void gen_rrib(DisasContext *ctx)
5259 {
5260     TCGv t0 = tcg_temp_new();
5261     TCGv t1 = tcg_temp_new();
5262     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5263     tcg_gen_movi_tl(t1, 0x80000000);
5264     tcg_gen_shr_tl(t1, t1, t0);
5265     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5266     tcg_gen_and_tl(t0, t0, t1);
5267     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5268     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5269     tcg_temp_free(t0);
5270     tcg_temp_free(t1);
5271     if (unlikely(Rc(ctx->opcode) != 0))
5272         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5273 }
5274
5275 /* sle - sle. */
5276 static void gen_sle(DisasContext *ctx)
5277 {
5278     TCGv t0 = tcg_temp_new();
5279     TCGv t1 = tcg_temp_new();
5280     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5281     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5282     tcg_gen_subfi_tl(t1, 32, t1);
5283     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5284     tcg_gen_or_tl(t1, t0, t1);
5285     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5286     gen_store_spr(SPR_MQ, t1);
5287     tcg_temp_free(t0);
5288     tcg_temp_free(t1);
5289     if (unlikely(Rc(ctx->opcode) != 0))
5290         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5291 }
5292
5293 /* sleq - sleq. */
5294 static void gen_sleq(DisasContext *ctx)
5295 {
5296     TCGv t0 = tcg_temp_new();
5297     TCGv t1 = tcg_temp_new();
5298     TCGv t2 = tcg_temp_new();
5299     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5300     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5301     tcg_gen_shl_tl(t2, t2, t0);
5302     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5303     gen_load_spr(t1, SPR_MQ);
5304     gen_store_spr(SPR_MQ, t0);
5305     tcg_gen_and_tl(t0, t0, t2);
5306     tcg_gen_andc_tl(t1, t1, t2);
5307     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5308     tcg_temp_free(t0);
5309     tcg_temp_free(t1);
5310     tcg_temp_free(t2);
5311     if (unlikely(Rc(ctx->opcode) != 0))
5312         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5313 }
5314
5315 /* sliq - sliq. */
5316 static void gen_sliq(DisasContext *ctx)
5317 {
5318     int sh = SH(ctx->opcode);
5319     TCGv t0 = tcg_temp_new();
5320     TCGv t1 = tcg_temp_new();
5321     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5322     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5323     tcg_gen_or_tl(t1, t0, t1);
5324     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5325     gen_store_spr(SPR_MQ, t1);
5326     tcg_temp_free(t0);
5327     tcg_temp_free(t1);
5328     if (unlikely(Rc(ctx->opcode) != 0))
5329         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5330 }
5331
5332 /* slliq - slliq. */
5333 static void gen_slliq(DisasContext *ctx)
5334 {
5335     int sh = SH(ctx->opcode);
5336     TCGv t0 = tcg_temp_new();
5337     TCGv t1 = tcg_temp_new();
5338     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5339     gen_load_spr(t1, SPR_MQ);
5340     gen_store_spr(SPR_MQ, t0);
5341     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5342     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5343     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5344     tcg_temp_free(t0);
5345     tcg_temp_free(t1);
5346     if (unlikely(Rc(ctx->opcode) != 0))
5347         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5348 }
5349
5350 /* sllq - sllq. */
5351 static void gen_sllq(DisasContext *ctx)
5352 {
5353     TCGLabel *l1 = gen_new_label();
5354     TCGLabel *l2 = gen_new_label();
5355     TCGv t0 = tcg_temp_local_new();
5356     TCGv t1 = tcg_temp_local_new();
5357     TCGv t2 = tcg_temp_local_new();
5358     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5359     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5360     tcg_gen_shl_tl(t1, t1, t2);
5361     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5362     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5363     gen_load_spr(t0, SPR_MQ);
5364     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5365     tcg_gen_br(l2);
5366     gen_set_label(l1);
5367     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5368     gen_load_spr(t2, SPR_MQ);
5369     tcg_gen_andc_tl(t1, t2, t1);
5370     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5371     gen_set_label(l2);
5372     tcg_temp_free(t0);
5373     tcg_temp_free(t1);
5374     tcg_temp_free(t2);
5375     if (unlikely(Rc(ctx->opcode) != 0))
5376         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5377 }
5378
5379 /* slq - slq. */
5380 static void gen_slq(DisasContext *ctx)
5381 {
5382     TCGLabel *l1 = gen_new_label();
5383     TCGv t0 = tcg_temp_new();
5384     TCGv t1 = tcg_temp_new();
5385     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5386     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5387     tcg_gen_subfi_tl(t1, 32, t1);
5388     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5389     tcg_gen_or_tl(t1, t0, t1);
5390     gen_store_spr(SPR_MQ, t1);
5391     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5392     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5393     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5394     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5395     gen_set_label(l1);
5396     tcg_temp_free(t0);
5397     tcg_temp_free(t1);
5398     if (unlikely(Rc(ctx->opcode) != 0))
5399         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5400 }
5401
5402 /* sraiq - sraiq. */
5403 static void gen_sraiq(DisasContext *ctx)
5404 {
5405     int sh = SH(ctx->opcode);
5406     TCGLabel *l1 = gen_new_label();
5407     TCGv t0 = tcg_temp_new();
5408     TCGv t1 = tcg_temp_new();
5409     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5410     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5411     tcg_gen_or_tl(t0, t0, t1);
5412     gen_store_spr(SPR_MQ, t0);
5413     tcg_gen_movi_tl(cpu_ca, 0);
5414     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5415     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5416     tcg_gen_movi_tl(cpu_ca, 1);
5417     gen_set_label(l1);
5418     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5419     tcg_temp_free(t0);
5420     tcg_temp_free(t1);
5421     if (unlikely(Rc(ctx->opcode) != 0))
5422         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5423 }
5424
5425 /* sraq - sraq. */
5426 static void gen_sraq(DisasContext *ctx)
5427 {
5428     TCGLabel *l1 = gen_new_label();
5429     TCGLabel *l2 = gen_new_label();
5430     TCGv t0 = tcg_temp_new();
5431     TCGv t1 = tcg_temp_local_new();
5432     TCGv t2 = tcg_temp_local_new();
5433     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5434     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5435     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5436     tcg_gen_subfi_tl(t2, 32, t2);
5437     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5438     tcg_gen_or_tl(t0, t0, t2);
5439     gen_store_spr(SPR_MQ, t0);
5440     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5441     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5442     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5443     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5444     gen_set_label(l1);
5445     tcg_temp_free(t0);
5446     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5447     tcg_gen_movi_tl(cpu_ca, 0);
5448     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5449     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5450     tcg_gen_movi_tl(cpu_ca, 1);
5451     gen_set_label(l2);
5452     tcg_temp_free(t1);
5453     tcg_temp_free(t2);
5454     if (unlikely(Rc(ctx->opcode) != 0))
5455         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5456 }
5457
5458 /* sre - sre. */
5459 static void gen_sre(DisasContext *ctx)
5460 {
5461     TCGv t0 = tcg_temp_new();
5462     TCGv t1 = tcg_temp_new();
5463     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5464     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5465     tcg_gen_subfi_tl(t1, 32, t1);
5466     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5467     tcg_gen_or_tl(t1, t0, t1);
5468     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5469     gen_store_spr(SPR_MQ, t1);
5470     tcg_temp_free(t0);
5471     tcg_temp_free(t1);
5472     if (unlikely(Rc(ctx->opcode) != 0))
5473         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5474 }
5475
5476 /* srea - srea. */
5477 static void gen_srea(DisasContext *ctx)
5478 {
5479     TCGv t0 = tcg_temp_new();
5480     TCGv t1 = tcg_temp_new();
5481     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5482     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5483     gen_store_spr(SPR_MQ, t0);
5484     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5485     tcg_temp_free(t0);
5486     tcg_temp_free(t1);
5487     if (unlikely(Rc(ctx->opcode) != 0))
5488         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5489 }
5490
5491 /* sreq */
5492 static void gen_sreq(DisasContext *ctx)
5493 {
5494     TCGv t0 = tcg_temp_new();
5495     TCGv t1 = tcg_temp_new();
5496     TCGv t2 = tcg_temp_new();
5497     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5498     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5499     tcg_gen_shr_tl(t1, t1, t0);
5500     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5501     gen_load_spr(t2, SPR_MQ);
5502     gen_store_spr(SPR_MQ, t0);
5503     tcg_gen_and_tl(t0, t0, t1);
5504     tcg_gen_andc_tl(t2, t2, t1);
5505     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5506     tcg_temp_free(t0);
5507     tcg_temp_free(t1);
5508     tcg_temp_free(t2);
5509     if (unlikely(Rc(ctx->opcode) != 0))
5510         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5511 }
5512
5513 /* sriq */
5514 static void gen_sriq(DisasContext *ctx)
5515 {
5516     int sh = SH(ctx->opcode);
5517     TCGv t0 = tcg_temp_new();
5518     TCGv t1 = tcg_temp_new();
5519     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5520     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5521     tcg_gen_or_tl(t1, t0, t1);
5522     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5523     gen_store_spr(SPR_MQ, t1);
5524     tcg_temp_free(t0);
5525     tcg_temp_free(t1);
5526     if (unlikely(Rc(ctx->opcode) != 0))
5527         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5528 }
5529
5530 /* srliq */
5531 static void gen_srliq(DisasContext *ctx)
5532 {
5533     int sh = SH(ctx->opcode);
5534     TCGv t0 = tcg_temp_new();
5535     TCGv t1 = tcg_temp_new();
5536     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5537     gen_load_spr(t1, SPR_MQ);
5538     gen_store_spr(SPR_MQ, t0);
5539     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5540     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5541     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5542     tcg_temp_free(t0);
5543     tcg_temp_free(t1);
5544     if (unlikely(Rc(ctx->opcode) != 0))
5545         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5546 }
5547
5548 /* srlq */
5549 static void gen_srlq(DisasContext *ctx)
5550 {
5551     TCGLabel *l1 = gen_new_label();
5552     TCGLabel *l2 = gen_new_label();
5553     TCGv t0 = tcg_temp_local_new();
5554     TCGv t1 = tcg_temp_local_new();
5555     TCGv t2 = tcg_temp_local_new();
5556     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5557     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5558     tcg_gen_shr_tl(t2, t1, t2);
5559     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5560     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5561     gen_load_spr(t0, SPR_MQ);
5562     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5563     tcg_gen_br(l2);
5564     gen_set_label(l1);
5565     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5566     tcg_gen_and_tl(t0, t0, t2);
5567     gen_load_spr(t1, SPR_MQ);
5568     tcg_gen_andc_tl(t1, t1, t2);
5569     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5570     gen_set_label(l2);
5571     tcg_temp_free(t0);
5572     tcg_temp_free(t1);
5573     tcg_temp_free(t2);
5574     if (unlikely(Rc(ctx->opcode) != 0))
5575         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5576 }
5577
5578 /* srq */
5579 static void gen_srq(DisasContext *ctx)
5580 {
5581     TCGLabel *l1 = gen_new_label();
5582     TCGv t0 = tcg_temp_new();
5583     TCGv t1 = tcg_temp_new();
5584     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5585     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5586     tcg_gen_subfi_tl(t1, 32, t1);
5587     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5588     tcg_gen_or_tl(t1, t0, t1);
5589     gen_store_spr(SPR_MQ, t1);
5590     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5591     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5592     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5593     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5594     gen_set_label(l1);
5595     tcg_temp_free(t0);
5596     tcg_temp_free(t1);
5597     if (unlikely(Rc(ctx->opcode) != 0))
5598         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5599 }
5600
5601 /* PowerPC 602 specific instructions */
5602
5603 /* dsa  */
5604 static void gen_dsa(DisasContext *ctx)
5605 {
5606     /* XXX: TODO */
5607     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5608 }
5609
5610 /* esa */
5611 static void gen_esa(DisasContext *ctx)
5612 {
5613     /* XXX: TODO */
5614     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5615 }
5616
5617 /* mfrom */
5618 static void gen_mfrom(DisasContext *ctx)
5619 {
5620 #if defined(CONFIG_USER_ONLY)
5621     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5622 #else
5623     if (unlikely(ctx->pr)) {
5624         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5625         return;
5626     }
5627     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5628 #endif
5629 }
5630
5631 /* 602 - 603 - G2 TLB management */
5632
5633 /* tlbld */
5634 static void gen_tlbld_6xx(DisasContext *ctx)
5635 {
5636 #if defined(CONFIG_USER_ONLY)
5637     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5638 #else
5639     if (unlikely(ctx->pr)) {
5640         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5641         return;
5642     }
5643     gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5644 #endif
5645 }
5646
5647 /* tlbli */
5648 static void gen_tlbli_6xx(DisasContext *ctx)
5649 {
5650 #if defined(CONFIG_USER_ONLY)
5651     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5652 #else
5653     if (unlikely(ctx->pr)) {
5654         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5655         return;
5656     }
5657     gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5658 #endif
5659 }
5660
5661 /* 74xx TLB management */
5662
5663 /* tlbld */
5664 static void gen_tlbld_74xx(DisasContext *ctx)
5665 {
5666 #if defined(CONFIG_USER_ONLY)
5667     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5668 #else
5669     if (unlikely(ctx->pr)) {
5670         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5671         return;
5672     }
5673     gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5674 #endif
5675 }
5676
5677 /* tlbli */
5678 static void gen_tlbli_74xx(DisasContext *ctx)
5679 {
5680 #if defined(CONFIG_USER_ONLY)
5681     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5682 #else
5683     if (unlikely(ctx->pr)) {
5684         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5685         return;
5686     }
5687     gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5688 #endif
5689 }
5690
5691 /* POWER instructions not in PowerPC 601 */
5692
5693 /* clf */
5694 static void gen_clf(DisasContext *ctx)
5695 {
5696     /* Cache line flush: implemented as no-op */
5697 }
5698
5699 /* cli */
5700 static void gen_cli(DisasContext *ctx)
5701 {
5702     /* Cache line invalidate: privileged and treated as no-op */
5703 #if defined(CONFIG_USER_ONLY)
5704     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5705 #else
5706     if (unlikely(ctx->pr)) {
5707         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5708         return;
5709     }
5710 #endif
5711 }
5712
5713 /* dclst */
5714 static void gen_dclst(DisasContext *ctx)
5715 {
5716     /* Data cache line store: treated as no-op */
5717 }
5718
5719 static void gen_mfsri(DisasContext *ctx)
5720 {
5721 #if defined(CONFIG_USER_ONLY)
5722     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5723 #else
5724     int ra = rA(ctx->opcode);
5725     int rd = rD(ctx->opcode);
5726     TCGv t0;
5727     if (unlikely(ctx->pr)) {
5728         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5729         return;
5730     }
5731     t0 = tcg_temp_new();
5732     gen_addr_reg_index(ctx, t0);
5733     tcg_gen_shri_tl(t0, t0, 28);
5734     tcg_gen_andi_tl(t0, t0, 0xF);
5735     gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
5736     tcg_temp_free(t0);
5737     if (ra != 0 && ra != rd)
5738         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5739 #endif
5740 }
5741
5742 static void gen_rac(DisasContext *ctx)
5743 {
5744 #if defined(CONFIG_USER_ONLY)
5745     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5746 #else
5747     TCGv t0;
5748     if (unlikely(ctx->pr)) {
5749         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5750         return;
5751     }
5752     t0 = tcg_temp_new();
5753     gen_addr_reg_index(ctx, t0);
5754     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5755     tcg_temp_free(t0);
5756 #endif
5757 }
5758
5759 static void gen_rfsvc(DisasContext *ctx)
5760 {
5761 #if defined(CONFIG_USER_ONLY)
5762     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5763 #else
5764     if (unlikely(ctx->pr)) {
5765         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5766         return;
5767     }
5768     gen_helper_rfsvc(cpu_env);
5769     gen_sync_exception(ctx);
5770 #endif
5771 }
5772
5773 /* svc is not implemented for now */
5774
5775 /* POWER2 specific instructions */
5776 /* Quad manipulation (load/store two floats at a time) */
5777
5778 /* lfq */
5779 static void gen_lfq(DisasContext *ctx)
5780 {
5781     int rd = rD(ctx->opcode);
5782     TCGv t0;
5783     gen_set_access_type(ctx, ACCESS_FLOAT);
5784     t0 = tcg_temp_new();
5785     gen_addr_imm_index(ctx, t0, 0);
5786     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5787     gen_addr_add(ctx, t0, t0, 8);
5788     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5789     tcg_temp_free(t0);
5790 }
5791
5792 /* lfqu */
5793 static void gen_lfqu(DisasContext *ctx)
5794 {
5795     int ra = rA(ctx->opcode);
5796     int rd = rD(ctx->opcode);
5797     TCGv t0, t1;
5798     gen_set_access_type(ctx, ACCESS_FLOAT);
5799     t0 = tcg_temp_new();
5800     t1 = tcg_temp_new();
5801     gen_addr_imm_index(ctx, t0, 0);
5802     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5803     gen_addr_add(ctx, t1, t0, 8);
5804     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5805     if (ra != 0)
5806         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5807     tcg_temp_free(t0);
5808     tcg_temp_free(t1);
5809 }
5810
5811 /* lfqux */
5812 static void gen_lfqux(DisasContext *ctx)
5813 {
5814     int ra = rA(ctx->opcode);
5815     int rd = rD(ctx->opcode);
5816     gen_set_access_type(ctx, ACCESS_FLOAT);
5817     TCGv t0, t1;
5818     t0 = tcg_temp_new();
5819     gen_addr_reg_index(ctx, t0);
5820     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5821     t1 = tcg_temp_new();
5822     gen_addr_add(ctx, t1, t0, 8);
5823     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5824     tcg_temp_free(t1);
5825     if (ra != 0)
5826         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5827     tcg_temp_free(t0);
5828 }
5829
5830 /* lfqx */
5831 static void gen_lfqx(DisasContext *ctx)
5832 {
5833     int rd = rD(ctx->opcode);
5834     TCGv t0;
5835     gen_set_access_type(ctx, ACCESS_FLOAT);
5836     t0 = tcg_temp_new();
5837     gen_addr_reg_index(ctx, t0);
5838     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5839     gen_addr_add(ctx, t0, t0, 8);
5840     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5841     tcg_temp_free(t0);
5842 }
5843
5844 /* stfq */
5845 static void gen_stfq(DisasContext *ctx)
5846 {
5847     int rd = rD(ctx->opcode);
5848     TCGv t0;
5849     gen_set_access_type(ctx, ACCESS_FLOAT);
5850     t0 = tcg_temp_new();
5851     gen_addr_imm_index(ctx, t0, 0);
5852     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5853     gen_addr_add(ctx, t0, t0, 8);
5854     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5855     tcg_temp_free(t0);
5856 }
5857
5858 /* stfqu */
5859 static void gen_stfqu(DisasContext *ctx)
5860 {
5861     int ra = rA(ctx->opcode);
5862     int rd = rD(ctx->opcode);
5863     TCGv t0, t1;
5864     gen_set_access_type(ctx, ACCESS_FLOAT);
5865     t0 = tcg_temp_new();
5866     gen_addr_imm_index(ctx, t0, 0);
5867     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5868     t1 = tcg_temp_new();
5869     gen_addr_add(ctx, t1, t0, 8);
5870     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5871     tcg_temp_free(t1);
5872     if (ra != 0)
5873         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5874     tcg_temp_free(t0);
5875 }
5876
5877 /* stfqux */
5878 static void gen_stfqux(DisasContext *ctx)
5879 {
5880     int ra = rA(ctx->opcode);
5881     int rd = rD(ctx->opcode);
5882     TCGv t0, t1;
5883     gen_set_access_type(ctx, ACCESS_FLOAT);
5884     t0 = tcg_temp_new();
5885     gen_addr_reg_index(ctx, t0);
5886     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5887     t1 = tcg_temp_new();
5888     gen_addr_add(ctx, t1, t0, 8);
5889     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5890     tcg_temp_free(t1);
5891     if (ra != 0)
5892         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5893     tcg_temp_free(t0);
5894 }
5895
5896 /* stfqx */
5897 static void gen_stfqx(DisasContext *ctx)
5898 {
5899     int rd = rD(ctx->opcode);
5900     TCGv t0;
5901     gen_set_access_type(ctx, ACCESS_FLOAT);
5902     t0 = tcg_temp_new();
5903     gen_addr_reg_index(ctx, t0);
5904     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5905     gen_addr_add(ctx, t0, t0, 8);
5906     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5907     tcg_temp_free(t0);
5908 }
5909
5910 /* BookE specific instructions */
5911
5912 /* XXX: not implemented on 440 ? */
5913 static void gen_mfapidi(DisasContext *ctx)
5914 {
5915     /* XXX: TODO */
5916     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5917 }
5918
5919 /* XXX: not implemented on 440 ? */
5920 static void gen_tlbiva(DisasContext *ctx)
5921 {
5922 #if defined(CONFIG_USER_ONLY)
5923     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5924 #else
5925     TCGv t0;
5926     if (unlikely(ctx->pr)) {
5927         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5928         return;
5929     }
5930     t0 = tcg_temp_new();
5931     gen_addr_reg_index(ctx, t0);
5932     gen_helper_tlbiva(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5933     tcg_temp_free(t0);
5934 #endif
5935 }
5936
5937 /* All 405 MAC instructions are translated here */
5938 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5939                                         int ra, int rb, int rt, int Rc)
5940 {
5941     TCGv t0, t1;
5942
5943     t0 = tcg_temp_local_new();
5944     t1 = tcg_temp_local_new();
5945
5946     switch (opc3 & 0x0D) {
5947     case 0x05:
5948         /* macchw    - macchw.    - macchwo   - macchwo.   */
5949         /* macchws   - macchws.   - macchwso  - macchwso.  */
5950         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5951         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5952         /* mulchw - mulchw. */
5953         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5954         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5955         tcg_gen_ext16s_tl(t1, t1);
5956         break;
5957     case 0x04:
5958         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5959         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5960         /* mulchwu - mulchwu. */
5961         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5962         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5963         tcg_gen_ext16u_tl(t1, t1);
5964         break;
5965     case 0x01:
5966         /* machhw    - machhw.    - machhwo   - machhwo.   */
5967         /* machhws   - machhws.   - machhwso  - machhwso.  */
5968         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5969         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5970         /* mulhhw - mulhhw. */
5971         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5972         tcg_gen_ext16s_tl(t0, t0);
5973         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5974         tcg_gen_ext16s_tl(t1, t1);
5975         break;
5976     case 0x00:
5977         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5978         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5979         /* mulhhwu - mulhhwu. */
5980         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5981         tcg_gen_ext16u_tl(t0, t0);
5982         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5983         tcg_gen_ext16u_tl(t1, t1);
5984         break;
5985     case 0x0D:
5986         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5987         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5988         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5989         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5990         /* mullhw - mullhw. */
5991         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5992         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5993         break;
5994     case 0x0C:
5995         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5996         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5997         /* mullhwu - mullhwu. */
5998         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5999         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
6000         break;
6001     }
6002     if (opc2 & 0x04) {
6003         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
6004         tcg_gen_mul_tl(t1, t0, t1);
6005         if (opc2 & 0x02) {
6006             /* nmultiply-and-accumulate (0x0E) */
6007             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
6008         } else {
6009             /* multiply-and-accumulate (0x0C) */
6010             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
6011         }
6012
6013         if (opc3 & 0x12) {
6014             /* Check overflow and/or saturate */
6015             TCGLabel *l1 = gen_new_label();
6016
6017             if (opc3 & 0x10) {
6018                 /* Start with XER OV disabled, the most likely case */
6019                 tcg_gen_movi_tl(cpu_ov, 0);
6020             }
6021             if (opc3 & 0x01) {
6022                 /* Signed */
6023                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
6024                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
6025                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
6026                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
6027                 if (opc3 & 0x02) {
6028                     /* Saturate */
6029                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
6030                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
6031                 }
6032             } else {
6033                 /* Unsigned */
6034                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
6035                 if (opc3 & 0x02) {
6036                     /* Saturate */
6037                     tcg_gen_movi_tl(t0, UINT32_MAX);
6038                 }
6039             }
6040             if (opc3 & 0x10) {
6041                 /* Check overflow */
6042                 tcg_gen_movi_tl(cpu_ov, 1);
6043                 tcg_gen_movi_tl(cpu_so, 1);
6044             }
6045             gen_set_label(l1);
6046             tcg_gen_mov_tl(cpu_gpr[rt], t0);
6047         }
6048     } else {
6049         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
6050     }
6051     tcg_temp_free(t0);
6052     tcg_temp_free(t1);
6053     if (unlikely(Rc) != 0) {
6054         /* Update Rc0 */
6055         gen_set_Rc0(ctx, cpu_gpr[rt]);
6056     }
6057 }
6058
6059 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
6060 static void glue(gen_, name)(DisasContext *ctx)                               \
6061 {                                                                             \
6062     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
6063                          rD(ctx->opcode), Rc(ctx->opcode));                   \
6064 }
6065
6066 /* macchw    - macchw.    */
6067 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
6068 /* macchwo   - macchwo.   */
6069 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
6070 /* macchws   - macchws.   */
6071 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
6072 /* macchwso  - macchwso.  */
6073 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
6074 /* macchwsu  - macchwsu.  */
6075 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
6076 /* macchwsuo - macchwsuo. */
6077 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
6078 /* macchwu   - macchwu.   */
6079 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
6080 /* macchwuo  - macchwuo.  */
6081 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
6082 /* machhw    - machhw.    */
6083 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
6084 /* machhwo   - machhwo.   */
6085 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
6086 /* machhws   - machhws.   */
6087 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
6088 /* machhwso  - machhwso.  */
6089 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
6090 /* machhwsu  - machhwsu.  */
6091 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
6092 /* machhwsuo - machhwsuo. */
6093 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
6094 /* machhwu   - machhwu.   */
6095 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
6096 /* machhwuo  - machhwuo.  */
6097 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
6098 /* maclhw    - maclhw.    */
6099 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
6100 /* maclhwo   - maclhwo.   */
6101 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
6102 /* maclhws   - maclhws.   */
6103 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
6104 /* maclhwso  - maclhwso.  */
6105 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
6106 /* maclhwu   - maclhwu.   */
6107 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
6108 /* maclhwuo  - maclhwuo.  */
6109 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
6110 /* maclhwsu  - maclhwsu.  */
6111 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
6112 /* maclhwsuo - maclhwsuo. */
6113 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
6114 /* nmacchw   - nmacchw.   */
6115 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
6116 /* nmacchwo  - nmacchwo.  */
6117 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
6118 /* nmacchws  - nmacchws.  */
6119 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
6120 /* nmacchwso - nmacchwso. */
6121 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
6122 /* nmachhw   - nmachhw.   */
6123 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
6124 /* nmachhwo  - nmachhwo.  */
6125 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
6126 /* nmachhws  - nmachhws.  */
6127 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
6128 /* nmachhwso - nmachhwso. */
6129 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
6130 /* nmaclhw   - nmaclhw.   */
6131 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
6132 /* nmaclhwo  - nmaclhwo.  */
6133 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
6134 /* nmaclhws  - nmaclhws.  */
6135 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
6136 /* nmaclhwso - nmaclhwso. */
6137 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
6138
6139 /* mulchw  - mulchw.  */
6140 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
6141 /* mulchwu - mulchwu. */
6142 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
6143 /* mulhhw  - mulhhw.  */
6144 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
6145 /* mulhhwu - mulhhwu. */
6146 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
6147 /* mullhw  - mullhw.  */
6148 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
6149 /* mullhwu - mullhwu. */
6150 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
6151
6152 /* mfdcr */
6153 static void gen_mfdcr(DisasContext *ctx)
6154 {
6155 #if defined(CONFIG_USER_ONLY)
6156     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6157 #else
6158     TCGv dcrn;
6159     if (unlikely(ctx->pr)) {
6160         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6161         return;
6162     }
6163     /* NIP cannot be restored if the memory exception comes from an helper */
6164     gen_update_nip(ctx, ctx->nip - 4);
6165     dcrn = tcg_const_tl(SPR(ctx->opcode));
6166     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
6167     tcg_temp_free(dcrn);
6168 #endif
6169 }
6170
6171 /* mtdcr */
6172 static void gen_mtdcr(DisasContext *ctx)
6173 {
6174 #if defined(CONFIG_USER_ONLY)
6175     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6176 #else
6177     TCGv dcrn;
6178     if (unlikely(ctx->pr)) {
6179         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6180         return;
6181     }
6182     /* NIP cannot be restored if the memory exception comes from an helper */
6183     gen_update_nip(ctx, ctx->nip - 4);
6184     dcrn = tcg_const_tl(SPR(ctx->opcode));
6185     gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
6186     tcg_temp_free(dcrn);
6187 #endif
6188 }
6189
6190 /* mfdcrx */
6191 /* XXX: not implemented on 440 ? */
6192 static void gen_mfdcrx(DisasContext *ctx)
6193 {
6194 #if defined(CONFIG_USER_ONLY)
6195     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6196 #else
6197     if (unlikely(ctx->pr)) {
6198         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6199         return;
6200     }
6201     /* NIP cannot be restored if the memory exception comes from an helper */
6202     gen_update_nip(ctx, ctx->nip - 4);
6203     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6204                         cpu_gpr[rA(ctx->opcode)]);
6205     /* Note: Rc update flag set leads to undefined state of Rc0 */
6206 #endif
6207 }
6208
6209 /* mtdcrx */
6210 /* XXX: not implemented on 440 ? */
6211 static void gen_mtdcrx(DisasContext *ctx)
6212 {
6213 #if defined(CONFIG_USER_ONLY)
6214     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6215 #else
6216     if (unlikely(ctx->pr)) {
6217         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
6218         return;
6219     }
6220     /* NIP cannot be restored if the memory exception comes from an helper */
6221     gen_update_nip(ctx, ctx->nip - 4);
6222     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6223                          cpu_gpr[rS(ctx->opcode)]);
6224     /* Note: Rc update flag set leads to undefined state of Rc0 */
6225 #endif
6226 }
6227
6228 /* mfdcrux (PPC 460) : user-mode access to DCR */
6229 static void gen_mfdcrux(DisasContext *ctx)
6230 {
6231     /* NIP cannot be restored if the memory exception comes from an helper */
6232     gen_update_nip(ctx, ctx->nip - 4);
6233     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6234                         cpu_gpr[rA(ctx->opcode)]);
6235     /* Note: Rc update flag set leads to undefined state of Rc0 */
6236 }
6237
6238 /* mtdcrux (PPC 460) : user-mode access to DCR */
6239 static void gen_mtdcrux(DisasContext *ctx)
6240 {
6241     /* NIP cannot be restored if the memory exception comes from an helper */
6242     gen_update_nip(ctx, ctx->nip - 4);
6243     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6244                          cpu_gpr[rS(ctx->opcode)]);
6245     /* Note: Rc update flag set leads to undefined state of Rc0 */
6246 }
6247
6248 /* dccci */
6249 static void gen_dccci(DisasContext *ctx)
6250 {
6251 #if defined(CONFIG_USER_ONLY)
6252     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6253 #else
6254     if (unlikely(ctx->pr)) {
6255         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6256         return;
6257     }
6258     /* interpreted as no-op */
6259 #endif
6260 }
6261
6262 /* dcread */
6263 static void gen_dcread(DisasContext *ctx)
6264 {
6265 #if defined(CONFIG_USER_ONLY)
6266     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6267 #else
6268     TCGv EA, val;
6269     if (unlikely(ctx->pr)) {
6270         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6271         return;
6272     }
6273     gen_set_access_type(ctx, ACCESS_CACHE);
6274     EA = tcg_temp_new();
6275     gen_addr_reg_index(ctx, EA);
6276     val = tcg_temp_new();
6277     gen_qemu_ld32u(ctx, val, EA);
6278     tcg_temp_free(val);
6279     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6280     tcg_temp_free(EA);
6281 #endif
6282 }
6283
6284 /* icbt */
6285 static void gen_icbt_40x(DisasContext *ctx)
6286 {
6287     /* interpreted as no-op */
6288     /* XXX: specification say this is treated as a load by the MMU
6289      *      but does not generate any exception
6290      */
6291 }
6292
6293 /* iccci */
6294 static void gen_iccci(DisasContext *ctx)
6295 {
6296 #if defined(CONFIG_USER_ONLY)
6297     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6298 #else
6299     if (unlikely(ctx->pr)) {
6300         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6301         return;
6302     }
6303     /* interpreted as no-op */
6304 #endif
6305 }
6306
6307 /* icread */
6308 static void gen_icread(DisasContext *ctx)
6309 {
6310 #if defined(CONFIG_USER_ONLY)
6311     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6312 #else
6313     if (unlikely(ctx->pr)) {
6314         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6315         return;
6316     }
6317     /* interpreted as no-op */
6318 #endif
6319 }
6320
6321 /* rfci (supervisor only) */
6322 static void gen_rfci_40x(DisasContext *ctx)
6323 {
6324 #if defined(CONFIG_USER_ONLY)
6325     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6326 #else
6327     if (unlikely(ctx->pr)) {
6328         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6329         return;
6330     }
6331     /* Restore CPU state */
6332     gen_helper_40x_rfci(cpu_env);
6333     gen_sync_exception(ctx);
6334 #endif
6335 }
6336
6337 static void gen_rfci(DisasContext *ctx)
6338 {
6339 #if defined(CONFIG_USER_ONLY)
6340     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6341 #else
6342     if (unlikely(ctx->pr)) {
6343         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6344         return;
6345     }
6346     /* Restore CPU state */
6347     gen_helper_rfci(cpu_env);
6348     gen_sync_exception(ctx);
6349 #endif
6350 }
6351
6352 /* BookE specific */
6353
6354 /* XXX: not implemented on 440 ? */
6355 static void gen_rfdi(DisasContext *ctx)
6356 {
6357 #if defined(CONFIG_USER_ONLY)
6358     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6359 #else
6360     if (unlikely(ctx->pr)) {
6361         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6362         return;
6363     }
6364     /* Restore CPU state */
6365     gen_helper_rfdi(cpu_env);
6366     gen_sync_exception(ctx);
6367 #endif
6368 }
6369
6370 /* XXX: not implemented on 440 ? */
6371 static void gen_rfmci(DisasContext *ctx)
6372 {
6373 #if defined(CONFIG_USER_ONLY)
6374     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6375 #else
6376     if (unlikely(ctx->pr)) {
6377         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6378         return;
6379     }
6380     /* Restore CPU state */
6381     gen_helper_rfmci(cpu_env);
6382     gen_sync_exception(ctx);
6383 #endif
6384 }
6385
6386 /* TLB management - PowerPC 405 implementation */
6387
6388 /* tlbre */
6389 static void gen_tlbre_40x(DisasContext *ctx)
6390 {
6391 #if defined(CONFIG_USER_ONLY)
6392     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6393 #else
6394     if (unlikely(ctx->pr)) {
6395         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6396         return;
6397     }
6398     switch (rB(ctx->opcode)) {
6399     case 0:
6400         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6401                                 cpu_gpr[rA(ctx->opcode)]);
6402         break;
6403     case 1:
6404         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6405                                 cpu_gpr[rA(ctx->opcode)]);
6406         break;
6407     default:
6408         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6409         break;
6410     }
6411 #endif
6412 }
6413
6414 /* tlbsx - tlbsx. */
6415 static void gen_tlbsx_40x(DisasContext *ctx)
6416 {
6417 #if defined(CONFIG_USER_ONLY)
6418     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6419 #else
6420     TCGv t0;
6421     if (unlikely(ctx->pr)) {
6422         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6423         return;
6424     }
6425     t0 = tcg_temp_new();
6426     gen_addr_reg_index(ctx, t0);
6427     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6428     tcg_temp_free(t0);
6429     if (Rc(ctx->opcode)) {
6430         TCGLabel *l1 = gen_new_label();
6431         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6432         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6433         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6434         gen_set_label(l1);
6435     }
6436 #endif
6437 }
6438
6439 /* tlbwe */
6440 static void gen_tlbwe_40x(DisasContext *ctx)
6441 {
6442 #if defined(CONFIG_USER_ONLY)
6443     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6444 #else
6445     if (unlikely(ctx->pr)) {
6446         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6447         return;
6448     }
6449     switch (rB(ctx->opcode)) {
6450     case 0:
6451         gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6452                                 cpu_gpr[rS(ctx->opcode)]);
6453         break;
6454     case 1:
6455         gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6456                                 cpu_gpr[rS(ctx->opcode)]);
6457         break;
6458     default:
6459         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6460         break;
6461     }
6462 #endif
6463 }
6464
6465 /* TLB management - PowerPC 440 implementation */
6466
6467 /* tlbre */
6468 static void gen_tlbre_440(DisasContext *ctx)
6469 {
6470 #if defined(CONFIG_USER_ONLY)
6471     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6472 #else
6473     if (unlikely(ctx->pr)) {
6474         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6475         return;
6476     }
6477     switch (rB(ctx->opcode)) {
6478     case 0:
6479     case 1:
6480     case 2:
6481         {
6482             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6483             gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6484                                  t0, cpu_gpr[rA(ctx->opcode)]);
6485             tcg_temp_free_i32(t0);
6486         }
6487         break;
6488     default:
6489         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6490         break;
6491     }
6492 #endif
6493 }
6494
6495 /* tlbsx - tlbsx. */
6496 static void gen_tlbsx_440(DisasContext *ctx)
6497 {
6498 #if defined(CONFIG_USER_ONLY)
6499     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6500 #else
6501     TCGv t0;
6502     if (unlikely(ctx->pr)) {
6503         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6504         return;
6505     }
6506     t0 = tcg_temp_new();
6507     gen_addr_reg_index(ctx, t0);
6508     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6509     tcg_temp_free(t0);
6510     if (Rc(ctx->opcode)) {
6511         TCGLabel *l1 = gen_new_label();
6512         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6513         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6514         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6515         gen_set_label(l1);
6516     }
6517 #endif
6518 }
6519
6520 /* tlbwe */
6521 static void gen_tlbwe_440(DisasContext *ctx)
6522 {
6523 #if defined(CONFIG_USER_ONLY)
6524     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6525 #else
6526     if (unlikely(ctx->pr)) {
6527         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6528         return;
6529     }
6530     switch (rB(ctx->opcode)) {
6531     case 0:
6532     case 1:
6533     case 2:
6534         {
6535             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6536             gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6537                                  cpu_gpr[rS(ctx->opcode)]);
6538             tcg_temp_free_i32(t0);
6539         }
6540         break;
6541     default:
6542         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6543         break;
6544     }
6545 #endif
6546 }
6547
6548 /* TLB management - PowerPC BookE 2.06 implementation */
6549
6550 /* tlbre */
6551 static void gen_tlbre_booke206(DisasContext *ctx)
6552 {
6553 #if defined(CONFIG_USER_ONLY)
6554     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6555 #else
6556     if (unlikely(ctx->pr)) {
6557         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6558         return;
6559     }
6560
6561     gen_helper_booke206_tlbre(cpu_env);
6562 #endif
6563 }
6564
6565 /* tlbsx - tlbsx. */
6566 static void gen_tlbsx_booke206(DisasContext *ctx)
6567 {
6568 #if defined(CONFIG_USER_ONLY)
6569     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6570 #else
6571     TCGv t0;
6572     if (unlikely(ctx->pr)) {
6573         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6574         return;
6575     }
6576
6577     if (rA(ctx->opcode)) {
6578         t0 = tcg_temp_new();
6579         tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6580     } else {
6581         t0 = tcg_const_tl(0);
6582     }
6583
6584     tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6585     gen_helper_booke206_tlbsx(cpu_env, t0);
6586     tcg_temp_free(t0);
6587 #endif
6588 }
6589
6590 /* tlbwe */
6591 static void gen_tlbwe_booke206(DisasContext *ctx)
6592 {
6593 #if defined(CONFIG_USER_ONLY)
6594     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6595 #else
6596     if (unlikely(ctx->pr)) {
6597         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6598         return;
6599     }
6600     gen_update_nip(ctx, ctx->nip - 4);
6601     gen_helper_booke206_tlbwe(cpu_env);
6602 #endif
6603 }
6604
6605 static void gen_tlbivax_booke206(DisasContext *ctx)
6606 {
6607 #if defined(CONFIG_USER_ONLY)
6608     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6609 #else
6610     TCGv t0;
6611     if (unlikely(ctx->pr)) {
6612         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6613         return;
6614     }
6615
6616     t0 = tcg_temp_new();
6617     gen_addr_reg_index(ctx, t0);
6618
6619     gen_helper_booke206_tlbivax(cpu_env, t0);
6620     tcg_temp_free(t0);
6621 #endif
6622 }
6623
6624 static void gen_tlbilx_booke206(DisasContext *ctx)
6625 {
6626 #if defined(CONFIG_USER_ONLY)
6627     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6628 #else
6629     TCGv t0;
6630     if (unlikely(ctx->pr)) {
6631         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6632         return;
6633     }
6634
6635     t0 = tcg_temp_new();
6636     gen_addr_reg_index(ctx, t0);
6637
6638     switch((ctx->opcode >> 21) & 0x3) {
6639     case 0:
6640         gen_helper_booke206_tlbilx0(cpu_env, t0);
6641         break;
6642     case 1:
6643         gen_helper_booke206_tlbilx1(cpu_env, t0);
6644         break;
6645     case 3:
6646         gen_helper_booke206_tlbilx3(cpu_env, t0);
6647         break;
6648     default:
6649         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6650         break;
6651     }
6652
6653     tcg_temp_free(t0);
6654 #endif
6655 }
6656
6657
6658 /* wrtee */
6659 static void gen_wrtee(DisasContext *ctx)
6660 {
6661 #if defined(CONFIG_USER_ONLY)
6662     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6663 #else
6664     TCGv t0;
6665     if (unlikely(ctx->pr)) {
6666         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6667         return;
6668     }
6669     t0 = tcg_temp_new();
6670     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6671     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6672     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6673     tcg_temp_free(t0);
6674     /* Stop translation to have a chance to raise an exception
6675      * if we just set msr_ee to 1
6676      */
6677     gen_stop_exception(ctx);
6678 #endif
6679 }
6680
6681 /* wrteei */
6682 static void gen_wrteei(DisasContext *ctx)
6683 {
6684 #if defined(CONFIG_USER_ONLY)
6685     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6686 #else
6687     if (unlikely(ctx->pr)) {
6688         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6689         return;
6690     }
6691     if (ctx->opcode & 0x00008000) {
6692         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6693         /* Stop translation to have a chance to raise an exception */
6694         gen_stop_exception(ctx);
6695     } else {
6696         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6697     }
6698 #endif
6699 }
6700
6701 /* PowerPC 440 specific instructions */
6702
6703 /* dlmzb */
6704 static void gen_dlmzb(DisasContext *ctx)
6705 {
6706     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6707     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6708                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6709     tcg_temp_free_i32(t0);
6710 }
6711
6712 /* mbar replaces eieio on 440 */
6713 static void gen_mbar(DisasContext *ctx)
6714 {
6715     /* interpreted as no-op */
6716 }
6717
6718 /* msync replaces sync on 440 */
6719 static void gen_msync_4xx(DisasContext *ctx)
6720 {
6721     /* interpreted as no-op */
6722 }
6723
6724 /* icbt */
6725 static void gen_icbt_440(DisasContext *ctx)
6726 {
6727     /* interpreted as no-op */
6728     /* XXX: specification say this is treated as a load by the MMU
6729      *      but does not generate any exception
6730      */
6731 }
6732
6733 /* Embedded.Processor Control */
6734
6735 static void gen_msgclr(DisasContext *ctx)
6736 {
6737 #if defined(CONFIG_USER_ONLY)
6738     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6739 #else
6740     if (unlikely(ctx->pr)) {
6741         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6742         return;
6743     }
6744
6745     gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6746 #endif
6747 }
6748
6749 static void gen_msgsnd(DisasContext *ctx)
6750 {
6751 #if defined(CONFIG_USER_ONLY)
6752     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6753 #else
6754     if (unlikely(ctx->pr)) {
6755         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6756         return;
6757     }
6758
6759     gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6760 #endif
6761 }
6762
6763 /***                      Altivec vector extension                         ***/
6764 /* Altivec registers moves */
6765
6766 static inline TCGv_ptr gen_avr_ptr(int reg)
6767 {
6768     TCGv_ptr r = tcg_temp_new_ptr();
6769     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, avr[reg]));
6770     return r;
6771 }
6772
6773 #define GEN_VR_LDX(name, opc2, opc3)                                          \
6774 static void glue(gen_, name)(DisasContext *ctx)                                       \
6775 {                                                                             \
6776     TCGv EA;                                                                  \
6777     if (unlikely(!ctx->altivec_enabled)) {                                    \
6778         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6779         return;                                                               \
6780     }                                                                         \
6781     gen_set_access_type(ctx, ACCESS_INT);                                     \
6782     EA = tcg_temp_new();                                                      \
6783     gen_addr_reg_index(ctx, EA);                                              \
6784     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6785     /* We only need to swap high and low halves. gen_qemu_ld64 does necessary \
6786        64-bit byteswap already. */                                            \
6787     if (ctx->le_mode) {                                                       \
6788         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6789         tcg_gen_addi_tl(EA, EA, 8);                                           \
6790         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6791     } else {                                                                  \
6792         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6793         tcg_gen_addi_tl(EA, EA, 8);                                           \
6794         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6795     }                                                                         \
6796     tcg_temp_free(EA);                                                        \
6797 }
6798
6799 #define GEN_VR_STX(name, opc2, opc3)                                          \
6800 static void gen_st##name(DisasContext *ctx)                                   \
6801 {                                                                             \
6802     TCGv EA;                                                                  \
6803     if (unlikely(!ctx->altivec_enabled)) {                                    \
6804         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6805         return;                                                               \
6806     }                                                                         \
6807     gen_set_access_type(ctx, ACCESS_INT);                                     \
6808     EA = tcg_temp_new();                                                      \
6809     gen_addr_reg_index(ctx, EA);                                              \
6810     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6811     /* We only need to swap high and low halves. gen_qemu_st64 does necessary \
6812        64-bit byteswap already. */                                            \
6813     if (ctx->le_mode) {                                                       \
6814         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6815         tcg_gen_addi_tl(EA, EA, 8);                                           \
6816         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6817     } else {                                                                  \
6818         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6819         tcg_gen_addi_tl(EA, EA, 8);                                           \
6820         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6821     }                                                                         \
6822     tcg_temp_free(EA);                                                        \
6823 }
6824
6825 #define GEN_VR_LVE(name, opc2, opc3, size)                              \
6826 static void gen_lve##name(DisasContext *ctx)                            \
6827     {                                                                   \
6828         TCGv EA;                                                        \
6829         TCGv_ptr rs;                                                    \
6830         if (unlikely(!ctx->altivec_enabled)) {                          \
6831             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6832             return;                                                     \
6833         }                                                               \
6834         gen_set_access_type(ctx, ACCESS_INT);                           \
6835         EA = tcg_temp_new();                                            \
6836         gen_addr_reg_index(ctx, EA);                                    \
6837         if (size > 1) {                                                 \
6838             tcg_gen_andi_tl(EA, EA, ~(size - 1));                       \
6839         }                                                               \
6840         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6841         gen_helper_lve##name(cpu_env, rs, EA);                          \
6842         tcg_temp_free(EA);                                              \
6843         tcg_temp_free_ptr(rs);                                          \
6844     }
6845
6846 #define GEN_VR_STVE(name, opc2, opc3, size)                             \
6847 static void gen_stve##name(DisasContext *ctx)                           \
6848     {                                                                   \
6849         TCGv EA;                                                        \
6850         TCGv_ptr rs;                                                    \
6851         if (unlikely(!ctx->altivec_enabled)) {                          \
6852             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
6853             return;                                                     \
6854         }                                                               \
6855         gen_set_access_type(ctx, ACCESS_INT);                           \
6856         EA = tcg_temp_new();                                            \
6857         gen_addr_reg_index(ctx, EA);                                    \
6858         if (size > 1) {                                                 \
6859             tcg_gen_andi_tl(EA, EA, ~(size - 1));                       \
6860         }                                                               \
6861         rs = gen_avr_ptr(rS(ctx->opcode));                              \
6862         gen_helper_stve##name(cpu_env, rs, EA);                         \
6863         tcg_temp_free(EA);                                              \
6864         tcg_temp_free_ptr(rs);                                          \
6865     }
6866
6867 GEN_VR_LDX(lvx, 0x07, 0x03);
6868 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6869 GEN_VR_LDX(lvxl, 0x07, 0x0B);
6870
6871 GEN_VR_LVE(bx, 0x07, 0x00, 1);
6872 GEN_VR_LVE(hx, 0x07, 0x01, 2);
6873 GEN_VR_LVE(wx, 0x07, 0x02, 4);
6874
6875 GEN_VR_STX(svx, 0x07, 0x07);
6876 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6877 GEN_VR_STX(svxl, 0x07, 0x0F);
6878
6879 GEN_VR_STVE(bx, 0x07, 0x04, 1);
6880 GEN_VR_STVE(hx, 0x07, 0x05, 2);
6881 GEN_VR_STVE(wx, 0x07, 0x06, 4);
6882
6883 static void gen_lvsl(DisasContext *ctx)
6884 {
6885     TCGv_ptr rd;
6886     TCGv EA;
6887     if (unlikely(!ctx->altivec_enabled)) {
6888         gen_exception(ctx, POWERPC_EXCP_VPU);
6889         return;
6890     }
6891     EA = tcg_temp_new();
6892     gen_addr_reg_index(ctx, EA);
6893     rd = gen_avr_ptr(rD(ctx->opcode));
6894     gen_helper_lvsl(rd, EA);
6895     tcg_temp_free(EA);
6896     tcg_temp_free_ptr(rd);
6897 }
6898
6899 static void gen_lvsr(DisasContext *ctx)
6900 {
6901     TCGv_ptr rd;
6902     TCGv EA;
6903     if (unlikely(!ctx->altivec_enabled)) {
6904         gen_exception(ctx, POWERPC_EXCP_VPU);
6905         return;
6906     }
6907     EA = tcg_temp_new();
6908     gen_addr_reg_index(ctx, EA);
6909     rd = gen_avr_ptr(rD(ctx->opcode));
6910     gen_helper_lvsr(rd, EA);
6911     tcg_temp_free(EA);
6912     tcg_temp_free_ptr(rd);
6913 }
6914
6915 static void gen_mfvscr(DisasContext *ctx)
6916 {
6917     TCGv_i32 t;
6918     if (unlikely(!ctx->altivec_enabled)) {
6919         gen_exception(ctx, POWERPC_EXCP_VPU);
6920         return;
6921     }
6922     tcg_gen_movi_i64(cpu_avrh[rD(ctx->opcode)], 0);
6923     t = tcg_temp_new_i32();
6924     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, vscr));
6925     tcg_gen_extu_i32_i64(cpu_avrl[rD(ctx->opcode)], t);
6926     tcg_temp_free_i32(t);
6927 }
6928
6929 static void gen_mtvscr(DisasContext *ctx)
6930 {
6931     TCGv_ptr p;
6932     if (unlikely(!ctx->altivec_enabled)) {
6933         gen_exception(ctx, POWERPC_EXCP_VPU);
6934         return;
6935     }
6936     p = gen_avr_ptr(rB(ctx->opcode));
6937     gen_helper_mtvscr(cpu_env, p);
6938     tcg_temp_free_ptr(p);
6939 }
6940
6941 /* Logical operations */
6942 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
6943 static void glue(gen_, name)(DisasContext *ctx)                                 \
6944 {                                                                       \
6945     if (unlikely(!ctx->altivec_enabled)) {                              \
6946         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6947         return;                                                         \
6948     }                                                                   \
6949     tcg_op(cpu_avrh[rD(ctx->opcode)], cpu_avrh[rA(ctx->opcode)], cpu_avrh[rB(ctx->opcode)]); \
6950     tcg_op(cpu_avrl[rD(ctx->opcode)], cpu_avrl[rA(ctx->opcode)], cpu_avrl[rB(ctx->opcode)]); \
6951 }
6952
6953 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16);
6954 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17);
6955 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18);
6956 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19);
6957 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20);
6958 GEN_VX_LOGICAL(veqv, tcg_gen_eqv_i64, 2, 26);
6959 GEN_VX_LOGICAL(vnand, tcg_gen_nand_i64, 2, 22);
6960 GEN_VX_LOGICAL(vorc, tcg_gen_orc_i64, 2, 21);
6961
6962 #define GEN_VXFORM(name, opc2, opc3)                                    \
6963 static void glue(gen_, name)(DisasContext *ctx)                                 \
6964 {                                                                       \
6965     TCGv_ptr ra, rb, rd;                                                \
6966     if (unlikely(!ctx->altivec_enabled)) {                              \
6967         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6968         return;                                                         \
6969     }                                                                   \
6970     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6971     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6972     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6973     gen_helper_##name (rd, ra, rb);                                     \
6974     tcg_temp_free_ptr(ra);                                              \
6975     tcg_temp_free_ptr(rb);                                              \
6976     tcg_temp_free_ptr(rd);                                              \
6977 }
6978
6979 #define GEN_VXFORM_ENV(name, opc2, opc3)                                \
6980 static void glue(gen_, name)(DisasContext *ctx)                         \
6981 {                                                                       \
6982     TCGv_ptr ra, rb, rd;                                                \
6983     if (unlikely(!ctx->altivec_enabled)) {                              \
6984         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
6985         return;                                                         \
6986     }                                                                   \
6987     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
6988     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
6989     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
6990     gen_helper_##name(cpu_env, rd, ra, rb);                             \
6991     tcg_temp_free_ptr(ra);                                              \
6992     tcg_temp_free_ptr(rb);                                              \
6993     tcg_temp_free_ptr(rd);                                              \
6994 }
6995
6996 #define GEN_VXFORM3(name, opc2, opc3)                                   \
6997 static void glue(gen_, name)(DisasContext *ctx)                         \
6998 {                                                                       \
6999     TCGv_ptr ra, rb, rc, rd;                                            \
7000     if (unlikely(!ctx->altivec_enabled)) {                              \
7001         gen_exception(ctx, POWERPC_EXCP_VPU);                           \
7002         return;                                                         \
7003     }                                                                   \
7004     ra = gen_avr_ptr(rA(ctx->opcode));                                  \
7005     rb = gen_avr_ptr(rB(ctx->opcode));                                  \
7006     rc = gen_avr_ptr(rC(ctx->opcode));                                  \
7007     rd = gen_avr_ptr(rD(ctx->opcode));                                  \
7008     gen_helper_##name(rd, ra, rb, rc);                                  \
7009     tcg_temp_free_ptr(ra);                                              \
7010     tcg_temp_free_ptr(rb);                                              \
7011     tcg_temp_free_ptr(rc);                                              \
7012     tcg_temp_free_ptr(rd);                                              \
7013 }
7014
7015 /*
7016  * Support for Altivec instruction pairs that use bit 31 (Rc) as
7017  * an opcode bit.  In general, these pairs come from different
7018  * versions of the ISA, so we must also support a pair of flags for
7019  * each instruction.
7020  */
7021 #define GEN_VXFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)          \
7022 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
7023 {                                                                      \
7024     if ((Rc(ctx->opcode) == 0) &&                                      \
7025         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7026         gen_##name0(ctx);                                              \
7027     } else if ((Rc(ctx->opcode) == 1) &&                               \
7028         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7029         gen_##name1(ctx);                                              \
7030     } else {                                                           \
7031         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
7032     }                                                                  \
7033 }
7034
7035 GEN_VXFORM(vaddubm, 0, 0);
7036 GEN_VXFORM(vadduhm, 0, 1);
7037 GEN_VXFORM(vadduwm, 0, 2);
7038 GEN_VXFORM(vaddudm, 0, 3);
7039 GEN_VXFORM(vsububm, 0, 16);
7040 GEN_VXFORM(vsubuhm, 0, 17);
7041 GEN_VXFORM(vsubuwm, 0, 18);
7042 GEN_VXFORM(vsubudm, 0, 19);
7043 GEN_VXFORM(vmaxub, 1, 0);
7044 GEN_VXFORM(vmaxuh, 1, 1);
7045 GEN_VXFORM(vmaxuw, 1, 2);
7046 GEN_VXFORM(vmaxud, 1, 3);
7047 GEN_VXFORM(vmaxsb, 1, 4);
7048 GEN_VXFORM(vmaxsh, 1, 5);
7049 GEN_VXFORM(vmaxsw, 1, 6);
7050 GEN_VXFORM(vmaxsd, 1, 7);
7051 GEN_VXFORM(vminub, 1, 8);
7052 GEN_VXFORM(vminuh, 1, 9);
7053 GEN_VXFORM(vminuw, 1, 10);
7054 GEN_VXFORM(vminud, 1, 11);
7055 GEN_VXFORM(vminsb, 1, 12);
7056 GEN_VXFORM(vminsh, 1, 13);
7057 GEN_VXFORM(vminsw, 1, 14);
7058 GEN_VXFORM(vminsd, 1, 15);
7059 GEN_VXFORM(vavgub, 1, 16);
7060 GEN_VXFORM(vavguh, 1, 17);
7061 GEN_VXFORM(vavguw, 1, 18);
7062 GEN_VXFORM(vavgsb, 1, 20);
7063 GEN_VXFORM(vavgsh, 1, 21);
7064 GEN_VXFORM(vavgsw, 1, 22);
7065 GEN_VXFORM(vmrghb, 6, 0);
7066 GEN_VXFORM(vmrghh, 6, 1);
7067 GEN_VXFORM(vmrghw, 6, 2);
7068 GEN_VXFORM(vmrglb, 6, 4);
7069 GEN_VXFORM(vmrglh, 6, 5);
7070 GEN_VXFORM(vmrglw, 6, 6);
7071
7072 static void gen_vmrgew(DisasContext *ctx)
7073 {
7074     TCGv_i64 tmp;
7075     int VT, VA, VB;
7076     if (unlikely(!ctx->altivec_enabled)) {
7077         gen_exception(ctx, POWERPC_EXCP_VPU);
7078         return;
7079     }
7080     VT = rD(ctx->opcode);
7081     VA = rA(ctx->opcode);
7082     VB = rB(ctx->opcode);
7083     tmp = tcg_temp_new_i64();
7084     tcg_gen_shri_i64(tmp, cpu_avrh[VB], 32);
7085     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VA], tmp, 0, 32);
7086     tcg_gen_shri_i64(tmp, cpu_avrl[VB], 32);
7087     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VA], tmp, 0, 32);
7088     tcg_temp_free_i64(tmp);
7089 }
7090
7091 static void gen_vmrgow(DisasContext *ctx)
7092 {
7093     int VT, VA, VB;
7094     if (unlikely(!ctx->altivec_enabled)) {
7095         gen_exception(ctx, POWERPC_EXCP_VPU);
7096         return;
7097     }
7098     VT = rD(ctx->opcode);
7099     VA = rA(ctx->opcode);
7100     VB = rB(ctx->opcode);
7101
7102     tcg_gen_deposit_i64(cpu_avrh[VT], cpu_avrh[VB], cpu_avrh[VA], 32, 32);
7103     tcg_gen_deposit_i64(cpu_avrl[VT], cpu_avrl[VB], cpu_avrl[VA], 32, 32);
7104 }
7105
7106 GEN_VXFORM(vmuloub, 4, 0);
7107 GEN_VXFORM(vmulouh, 4, 1);
7108 GEN_VXFORM(vmulouw, 4, 2);
7109 GEN_VXFORM(vmuluwm, 4, 2);
7110 GEN_VXFORM_DUAL(vmulouw, PPC_ALTIVEC, PPC_NONE,
7111                 vmuluwm, PPC_NONE, PPC2_ALTIVEC_207)
7112 GEN_VXFORM(vmulosb, 4, 4);
7113 GEN_VXFORM(vmulosh, 4, 5);
7114 GEN_VXFORM(vmulosw, 4, 6);
7115 GEN_VXFORM(vmuleub, 4, 8);
7116 GEN_VXFORM(vmuleuh, 4, 9);
7117 GEN_VXFORM(vmuleuw, 4, 10);
7118 GEN_VXFORM(vmulesb, 4, 12);
7119 GEN_VXFORM(vmulesh, 4, 13);
7120 GEN_VXFORM(vmulesw, 4, 14);
7121 GEN_VXFORM(vslb, 2, 4);
7122 GEN_VXFORM(vslh, 2, 5);
7123 GEN_VXFORM(vslw, 2, 6);
7124 GEN_VXFORM(vsld, 2, 23);
7125 GEN_VXFORM(vsrb, 2, 8);
7126 GEN_VXFORM(vsrh, 2, 9);
7127 GEN_VXFORM(vsrw, 2, 10);
7128 GEN_VXFORM(vsrd, 2, 27);
7129 GEN_VXFORM(vsrab, 2, 12);
7130 GEN_VXFORM(vsrah, 2, 13);
7131 GEN_VXFORM(vsraw, 2, 14);
7132 GEN_VXFORM(vsrad, 2, 15);
7133 GEN_VXFORM(vslo, 6, 16);
7134 GEN_VXFORM(vsro, 6, 17);
7135 GEN_VXFORM(vaddcuw, 0, 6);
7136 GEN_VXFORM(vsubcuw, 0, 22);
7137 GEN_VXFORM_ENV(vaddubs, 0, 8);
7138 GEN_VXFORM_ENV(vadduhs, 0, 9);
7139 GEN_VXFORM_ENV(vadduws, 0, 10);
7140 GEN_VXFORM_ENV(vaddsbs, 0, 12);
7141 GEN_VXFORM_ENV(vaddshs, 0, 13);
7142 GEN_VXFORM_ENV(vaddsws, 0, 14);
7143 GEN_VXFORM_ENV(vsububs, 0, 24);
7144 GEN_VXFORM_ENV(vsubuhs, 0, 25);
7145 GEN_VXFORM_ENV(vsubuws, 0, 26);
7146 GEN_VXFORM_ENV(vsubsbs, 0, 28);
7147 GEN_VXFORM_ENV(vsubshs, 0, 29);
7148 GEN_VXFORM_ENV(vsubsws, 0, 30);
7149 GEN_VXFORM(vadduqm, 0, 4);
7150 GEN_VXFORM(vaddcuq, 0, 5);
7151 GEN_VXFORM3(vaddeuqm, 30, 0);
7152 GEN_VXFORM3(vaddecuq, 30, 0);
7153 GEN_VXFORM_DUAL(vaddeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7154             vaddecuq, PPC_NONE, PPC2_ALTIVEC_207)
7155 GEN_VXFORM(vsubuqm, 0, 20);
7156 GEN_VXFORM(vsubcuq, 0, 21);
7157 GEN_VXFORM3(vsubeuqm, 31, 0);
7158 GEN_VXFORM3(vsubecuq, 31, 0);
7159 GEN_VXFORM_DUAL(vsubeuqm, PPC_NONE, PPC2_ALTIVEC_207, \
7160             vsubecuq, PPC_NONE, PPC2_ALTIVEC_207)
7161 GEN_VXFORM(vrlb, 2, 0);
7162 GEN_VXFORM(vrlh, 2, 1);
7163 GEN_VXFORM(vrlw, 2, 2);
7164 GEN_VXFORM(vrld, 2, 3);
7165 GEN_VXFORM(vsl, 2, 7);
7166 GEN_VXFORM(vsr, 2, 11);
7167 GEN_VXFORM_ENV(vpkuhum, 7, 0);
7168 GEN_VXFORM_ENV(vpkuwum, 7, 1);
7169 GEN_VXFORM_ENV(vpkudum, 7, 17);
7170 GEN_VXFORM_ENV(vpkuhus, 7, 2);
7171 GEN_VXFORM_ENV(vpkuwus, 7, 3);
7172 GEN_VXFORM_ENV(vpkudus, 7, 19);
7173 GEN_VXFORM_ENV(vpkshus, 7, 4);
7174 GEN_VXFORM_ENV(vpkswus, 7, 5);
7175 GEN_VXFORM_ENV(vpksdus, 7, 21);
7176 GEN_VXFORM_ENV(vpkshss, 7, 6);
7177 GEN_VXFORM_ENV(vpkswss, 7, 7);
7178 GEN_VXFORM_ENV(vpksdss, 7, 23);
7179 GEN_VXFORM(vpkpx, 7, 12);
7180 GEN_VXFORM_ENV(vsum4ubs, 4, 24);
7181 GEN_VXFORM_ENV(vsum4sbs, 4, 28);
7182 GEN_VXFORM_ENV(vsum4shs, 4, 25);
7183 GEN_VXFORM_ENV(vsum2sws, 4, 26);
7184 GEN_VXFORM_ENV(vsumsws, 4, 30);
7185 GEN_VXFORM_ENV(vaddfp, 5, 0);
7186 GEN_VXFORM_ENV(vsubfp, 5, 1);
7187 GEN_VXFORM_ENV(vmaxfp, 5, 16);
7188 GEN_VXFORM_ENV(vminfp, 5, 17);
7189
7190 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
7191 static void glue(gen_, name)(DisasContext *ctx)                         \
7192     {                                                                   \
7193         TCGv_ptr ra, rb, rd;                                            \
7194         if (unlikely(!ctx->altivec_enabled)) {                          \
7195             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7196             return;                                                     \
7197         }                                                               \
7198         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7199         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7200         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7201         gen_helper_##opname(cpu_env, rd, ra, rb);                       \
7202         tcg_temp_free_ptr(ra);                                          \
7203         tcg_temp_free_ptr(rb);                                          \
7204         tcg_temp_free_ptr(rd);                                          \
7205     }
7206
7207 #define GEN_VXRFORM(name, opc2, opc3)                                \
7208     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
7209     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
7210
7211 /*
7212  * Support for Altivec instructions that use bit 31 (Rc) as an opcode
7213  * bit but also use bit 21 as an actual Rc bit.  In general, thse pairs
7214  * come from different versions of the ISA, so we must also support a
7215  * pair of flags for each instruction.
7216  */
7217 #define GEN_VXRFORM_DUAL(name0, flg0, flg2_0, name1, flg1, flg2_1)     \
7218 static void glue(gen_, name0##_##name1)(DisasContext *ctx)             \
7219 {                                                                      \
7220     if ((Rc(ctx->opcode) == 0) &&                                      \
7221         ((ctx->insns_flags & flg0) || (ctx->insns_flags2 & flg2_0))) { \
7222         if (Rc21(ctx->opcode) == 0) {                                  \
7223             gen_##name0(ctx);                                          \
7224         } else {                                                       \
7225             gen_##name0##_(ctx);                                       \
7226         }                                                              \
7227     } else if ((Rc(ctx->opcode) == 1) &&                               \
7228         ((ctx->insns_flags & flg1) || (ctx->insns_flags2 & flg2_1))) { \
7229         if (Rc21(ctx->opcode) == 0) {                                  \
7230             gen_##name1(ctx);                                          \
7231         } else {                                                       \
7232             gen_##name1##_(ctx);                                       \
7233         }                                                              \
7234     } else {                                                           \
7235         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);            \
7236     }                                                                  \
7237 }
7238
7239 GEN_VXRFORM(vcmpequb, 3, 0)
7240 GEN_VXRFORM(vcmpequh, 3, 1)
7241 GEN_VXRFORM(vcmpequw, 3, 2)
7242 GEN_VXRFORM(vcmpequd, 3, 3)
7243 GEN_VXRFORM(vcmpgtsb, 3, 12)
7244 GEN_VXRFORM(vcmpgtsh, 3, 13)
7245 GEN_VXRFORM(vcmpgtsw, 3, 14)
7246 GEN_VXRFORM(vcmpgtsd, 3, 15)
7247 GEN_VXRFORM(vcmpgtub, 3, 8)
7248 GEN_VXRFORM(vcmpgtuh, 3, 9)
7249 GEN_VXRFORM(vcmpgtuw, 3, 10)
7250 GEN_VXRFORM(vcmpgtud, 3, 11)
7251 GEN_VXRFORM(vcmpeqfp, 3, 3)
7252 GEN_VXRFORM(vcmpgefp, 3, 7)
7253 GEN_VXRFORM(vcmpgtfp, 3, 11)
7254 GEN_VXRFORM(vcmpbfp, 3, 15)
7255
7256 GEN_VXRFORM_DUAL(vcmpeqfp, PPC_ALTIVEC, PPC_NONE, \
7257                  vcmpequd, PPC_NONE, PPC2_ALTIVEC_207)
7258 GEN_VXRFORM_DUAL(vcmpbfp, PPC_ALTIVEC, PPC_NONE, \
7259                  vcmpgtsd, PPC_NONE, PPC2_ALTIVEC_207)
7260 GEN_VXRFORM_DUAL(vcmpgtfp, PPC_ALTIVEC, PPC_NONE, \
7261                  vcmpgtud, PPC_NONE, PPC2_ALTIVEC_207)
7262
7263 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7264 static void glue(gen_, name)(DisasContext *ctx)                         \
7265     {                                                                   \
7266         TCGv_ptr rd;                                                    \
7267         TCGv_i32 simm;                                                  \
7268         if (unlikely(!ctx->altivec_enabled)) {                          \
7269             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7270             return;                                                     \
7271         }                                                               \
7272         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7273         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7274         gen_helper_##name (rd, simm);                                   \
7275         tcg_temp_free_i32(simm);                                        \
7276         tcg_temp_free_ptr(rd);                                          \
7277     }
7278
7279 GEN_VXFORM_SIMM(vspltisb, 6, 12);
7280 GEN_VXFORM_SIMM(vspltish, 6, 13);
7281 GEN_VXFORM_SIMM(vspltisw, 6, 14);
7282
7283 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
7284 static void glue(gen_, name)(DisasContext *ctx)                                 \
7285     {                                                                   \
7286         TCGv_ptr rb, rd;                                                \
7287         if (unlikely(!ctx->altivec_enabled)) {                          \
7288             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7289             return;                                                     \
7290         }                                                               \
7291         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7292         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7293         gen_helper_##name (rd, rb);                                     \
7294         tcg_temp_free_ptr(rb);                                          \
7295         tcg_temp_free_ptr(rd);                                         \
7296     }
7297
7298 #define GEN_VXFORM_NOA_ENV(name, opc2, opc3)                            \
7299 static void glue(gen_, name)(DisasContext *ctx)                         \
7300     {                                                                   \
7301         TCGv_ptr rb, rd;                                                \
7302                                                                         \
7303         if (unlikely(!ctx->altivec_enabled)) {                          \
7304             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7305             return;                                                     \
7306         }                                                               \
7307         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7308         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7309         gen_helper_##name(cpu_env, rd, rb);                             \
7310         tcg_temp_free_ptr(rb);                                          \
7311         tcg_temp_free_ptr(rd);                                          \
7312     }
7313
7314 GEN_VXFORM_NOA(vupkhsb, 7, 8);
7315 GEN_VXFORM_NOA(vupkhsh, 7, 9);
7316 GEN_VXFORM_NOA(vupkhsw, 7, 25);
7317 GEN_VXFORM_NOA(vupklsb, 7, 10);
7318 GEN_VXFORM_NOA(vupklsh, 7, 11);
7319 GEN_VXFORM_NOA(vupklsw, 7, 27);
7320 GEN_VXFORM_NOA(vupkhpx, 7, 13);
7321 GEN_VXFORM_NOA(vupklpx, 7, 15);
7322 GEN_VXFORM_NOA_ENV(vrefp, 5, 4);
7323 GEN_VXFORM_NOA_ENV(vrsqrtefp, 5, 5);
7324 GEN_VXFORM_NOA_ENV(vexptefp, 5, 6);
7325 GEN_VXFORM_NOA_ENV(vlogefp, 5, 7);
7326 GEN_VXFORM_NOA_ENV(vrfim, 5, 11);
7327 GEN_VXFORM_NOA_ENV(vrfin, 5, 8);
7328 GEN_VXFORM_NOA_ENV(vrfip, 5, 10);
7329 GEN_VXFORM_NOA_ENV(vrfiz, 5, 9);
7330
7331 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
7332 static void glue(gen_, name)(DisasContext *ctx)                                 \
7333     {                                                                   \
7334         TCGv_ptr rd;                                                    \
7335         TCGv_i32 simm;                                                  \
7336         if (unlikely(!ctx->altivec_enabled)) {                          \
7337             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7338             return;                                                     \
7339         }                                                               \
7340         simm = tcg_const_i32(SIMM5(ctx->opcode));                       \
7341         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7342         gen_helper_##name (rd, simm);                                   \
7343         tcg_temp_free_i32(simm);                                        \
7344         tcg_temp_free_ptr(rd);                                          \
7345     }
7346
7347 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
7348 static void glue(gen_, name)(DisasContext *ctx)                                 \
7349     {                                                                   \
7350         TCGv_ptr rb, rd;                                                \
7351         TCGv_i32 uimm;                                                  \
7352         if (unlikely(!ctx->altivec_enabled)) {                          \
7353             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7354             return;                                                     \
7355         }                                                               \
7356         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7357         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7358         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7359         gen_helper_##name (rd, rb, uimm);                               \
7360         tcg_temp_free_i32(uimm);                                        \
7361         tcg_temp_free_ptr(rb);                                          \
7362         tcg_temp_free_ptr(rd);                                          \
7363     }
7364
7365 #define GEN_VXFORM_UIMM_ENV(name, opc2, opc3)                           \
7366 static void glue(gen_, name)(DisasContext *ctx)                         \
7367     {                                                                   \
7368         TCGv_ptr rb, rd;                                                \
7369         TCGv_i32 uimm;                                                  \
7370                                                                         \
7371         if (unlikely(!ctx->altivec_enabled)) {                          \
7372             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7373             return;                                                     \
7374         }                                                               \
7375         uimm = tcg_const_i32(UIMM5(ctx->opcode));                       \
7376         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7377         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7378         gen_helper_##name(cpu_env, rd, rb, uimm);                       \
7379         tcg_temp_free_i32(uimm);                                        \
7380         tcg_temp_free_ptr(rb);                                          \
7381         tcg_temp_free_ptr(rd);                                          \
7382     }
7383
7384 GEN_VXFORM_UIMM(vspltb, 6, 8);
7385 GEN_VXFORM_UIMM(vsplth, 6, 9);
7386 GEN_VXFORM_UIMM(vspltw, 6, 10);
7387 GEN_VXFORM_UIMM_ENV(vcfux, 5, 12);
7388 GEN_VXFORM_UIMM_ENV(vcfsx, 5, 13);
7389 GEN_VXFORM_UIMM_ENV(vctuxs, 5, 14);
7390 GEN_VXFORM_UIMM_ENV(vctsxs, 5, 15);
7391
7392 static void gen_vsldoi(DisasContext *ctx)
7393 {
7394     TCGv_ptr ra, rb, rd;
7395     TCGv_i32 sh;
7396     if (unlikely(!ctx->altivec_enabled)) {
7397         gen_exception(ctx, POWERPC_EXCP_VPU);
7398         return;
7399     }
7400     ra = gen_avr_ptr(rA(ctx->opcode));
7401     rb = gen_avr_ptr(rB(ctx->opcode));
7402     rd = gen_avr_ptr(rD(ctx->opcode));
7403     sh = tcg_const_i32(VSH(ctx->opcode));
7404     gen_helper_vsldoi (rd, ra, rb, sh);
7405     tcg_temp_free_ptr(ra);
7406     tcg_temp_free_ptr(rb);
7407     tcg_temp_free_ptr(rd);
7408     tcg_temp_free_i32(sh);
7409 }
7410
7411 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
7412 static void glue(gen_, name0##_##name1)(DisasContext *ctx)              \
7413     {                                                                   \
7414         TCGv_ptr ra, rb, rc, rd;                                        \
7415         if (unlikely(!ctx->altivec_enabled)) {                          \
7416             gen_exception(ctx, POWERPC_EXCP_VPU);                       \
7417             return;                                                     \
7418         }                                                               \
7419         ra = gen_avr_ptr(rA(ctx->opcode));                              \
7420         rb = gen_avr_ptr(rB(ctx->opcode));                              \
7421         rc = gen_avr_ptr(rC(ctx->opcode));                              \
7422         rd = gen_avr_ptr(rD(ctx->opcode));                              \
7423         if (Rc(ctx->opcode)) {                                          \
7424             gen_helper_##name1(cpu_env, rd, ra, rb, rc);                \
7425         } else {                                                        \
7426             gen_helper_##name0(cpu_env, rd, ra, rb, rc);                \
7427         }                                                               \
7428         tcg_temp_free_ptr(ra);                                          \
7429         tcg_temp_free_ptr(rb);                                          \
7430         tcg_temp_free_ptr(rc);                                          \
7431         tcg_temp_free_ptr(rd);                                          \
7432     }
7433
7434 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16)
7435
7436 static void gen_vmladduhm(DisasContext *ctx)
7437 {
7438     TCGv_ptr ra, rb, rc, rd;
7439     if (unlikely(!ctx->altivec_enabled)) {
7440         gen_exception(ctx, POWERPC_EXCP_VPU);
7441         return;
7442     }
7443     ra = gen_avr_ptr(rA(ctx->opcode));
7444     rb = gen_avr_ptr(rB(ctx->opcode));
7445     rc = gen_avr_ptr(rC(ctx->opcode));
7446     rd = gen_avr_ptr(rD(ctx->opcode));
7447     gen_helper_vmladduhm(rd, ra, rb, rc);
7448     tcg_temp_free_ptr(ra);
7449     tcg_temp_free_ptr(rb);
7450     tcg_temp_free_ptr(rc);
7451     tcg_temp_free_ptr(rd);
7452 }
7453
7454 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18)
7455 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19)
7456 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20)
7457 GEN_VAFORM_PAIRED(vsel, vperm, 21)
7458 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23)
7459
7460 GEN_VXFORM_NOA(vclzb, 1, 28)
7461 GEN_VXFORM_NOA(vclzh, 1, 29)
7462 GEN_VXFORM_NOA(vclzw, 1, 30)
7463 GEN_VXFORM_NOA(vclzd, 1, 31)
7464 GEN_VXFORM_NOA(vpopcntb, 1, 28)
7465 GEN_VXFORM_NOA(vpopcnth, 1, 29)
7466 GEN_VXFORM_NOA(vpopcntw, 1, 30)
7467 GEN_VXFORM_NOA(vpopcntd, 1, 31)
7468 GEN_VXFORM_DUAL(vclzb, PPC_NONE, PPC2_ALTIVEC_207, \
7469                 vpopcntb, PPC_NONE, PPC2_ALTIVEC_207)
7470 GEN_VXFORM_DUAL(vclzh, PPC_NONE, PPC2_ALTIVEC_207, \
7471                 vpopcnth, PPC_NONE, PPC2_ALTIVEC_207)
7472 GEN_VXFORM_DUAL(vclzw, PPC_NONE, PPC2_ALTIVEC_207, \
7473                 vpopcntw, PPC_NONE, PPC2_ALTIVEC_207)
7474 GEN_VXFORM_DUAL(vclzd, PPC_NONE, PPC2_ALTIVEC_207, \
7475                 vpopcntd, PPC_NONE, PPC2_ALTIVEC_207)
7476 GEN_VXFORM(vbpermq, 6, 21);
7477 GEN_VXFORM_NOA(vgbbd, 6, 20);
7478 GEN_VXFORM(vpmsumb, 4, 16)
7479 GEN_VXFORM(vpmsumh, 4, 17)
7480 GEN_VXFORM(vpmsumw, 4, 18)
7481 GEN_VXFORM(vpmsumd, 4, 19)
7482
7483 #define GEN_BCD(op)                                 \
7484 static void gen_##op(DisasContext *ctx)             \
7485 {                                                   \
7486     TCGv_ptr ra, rb, rd;                            \
7487     TCGv_i32 ps;                                    \
7488                                                     \
7489     if (unlikely(!ctx->altivec_enabled)) {          \
7490         gen_exception(ctx, POWERPC_EXCP_VPU);       \
7491         return;                                     \
7492     }                                               \
7493                                                     \
7494     ra = gen_avr_ptr(rA(ctx->opcode));              \
7495     rb = gen_avr_ptr(rB(ctx->opcode));              \
7496     rd = gen_avr_ptr(rD(ctx->opcode));              \
7497                                                     \
7498     ps = tcg_const_i32((ctx->opcode & 0x200) != 0); \
7499                                                     \
7500     gen_helper_##op(cpu_crf[6], rd, ra, rb, ps);    \
7501                                                     \
7502     tcg_temp_free_ptr(ra);                          \
7503     tcg_temp_free_ptr(rb);                          \
7504     tcg_temp_free_ptr(rd);                          \
7505     tcg_temp_free_i32(ps);                          \
7506 }
7507
7508 GEN_BCD(bcdadd)
7509 GEN_BCD(bcdsub)
7510
7511 GEN_VXFORM_DUAL(vsububm, PPC_ALTIVEC, PPC_NONE, \
7512                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7513 GEN_VXFORM_DUAL(vsububs, PPC_ALTIVEC, PPC_NONE, \
7514                 bcdadd, PPC_NONE, PPC2_ALTIVEC_207)
7515 GEN_VXFORM_DUAL(vsubuhm, PPC_ALTIVEC, PPC_NONE, \
7516                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7517 GEN_VXFORM_DUAL(vsubuhs, PPC_ALTIVEC, PPC_NONE, \
7518                 bcdsub, PPC_NONE, PPC2_ALTIVEC_207)
7519
7520 static void gen_vsbox(DisasContext *ctx)
7521 {
7522     TCGv_ptr ra, rd;
7523     if (unlikely(!ctx->altivec_enabled)) {
7524         gen_exception(ctx, POWERPC_EXCP_VPU);
7525         return;
7526     }
7527     ra = gen_avr_ptr(rA(ctx->opcode));
7528     rd = gen_avr_ptr(rD(ctx->opcode));
7529     gen_helper_vsbox(rd, ra);
7530     tcg_temp_free_ptr(ra);
7531     tcg_temp_free_ptr(rd);
7532 }
7533
7534 GEN_VXFORM(vcipher, 4, 20)
7535 GEN_VXFORM(vcipherlast, 4, 20)
7536 GEN_VXFORM(vncipher, 4, 21)
7537 GEN_VXFORM(vncipherlast, 4, 21)
7538
7539 GEN_VXFORM_DUAL(vcipher, PPC_NONE, PPC2_ALTIVEC_207,
7540                 vcipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7541 GEN_VXFORM_DUAL(vncipher, PPC_NONE, PPC2_ALTIVEC_207,
7542                 vncipherlast, PPC_NONE, PPC2_ALTIVEC_207)
7543
7544 #define VSHASIGMA(op)                         \
7545 static void gen_##op(DisasContext *ctx)       \
7546 {                                             \
7547     TCGv_ptr ra, rd;                          \
7548     TCGv_i32 st_six;                          \
7549     if (unlikely(!ctx->altivec_enabled)) {    \
7550         gen_exception(ctx, POWERPC_EXCP_VPU); \
7551         return;                               \
7552     }                                         \
7553     ra = gen_avr_ptr(rA(ctx->opcode));        \
7554     rd = gen_avr_ptr(rD(ctx->opcode));        \
7555     st_six = tcg_const_i32(rB(ctx->opcode));  \
7556     gen_helper_##op(rd, ra, st_six);          \
7557     tcg_temp_free_ptr(ra);                    \
7558     tcg_temp_free_ptr(rd);                    \
7559     tcg_temp_free_i32(st_six);                \
7560 }
7561
7562 VSHASIGMA(vshasigmaw)
7563 VSHASIGMA(vshasigmad)
7564
7565 GEN_VXFORM3(vpermxor, 22, 0xFF)
7566 GEN_VXFORM_DUAL(vsldoi, PPC_ALTIVEC, PPC_NONE,
7567                 vpermxor, PPC_NONE, PPC2_ALTIVEC_207)
7568
7569 /***                           VSX extension                               ***/
7570
7571 static inline TCGv_i64 cpu_vsrh(int n)
7572 {
7573     if (n < 32) {
7574         return cpu_fpr[n];
7575     } else {
7576         return cpu_avrh[n-32];
7577     }
7578 }
7579
7580 static inline TCGv_i64 cpu_vsrl(int n)
7581 {
7582     if (n < 32) {
7583         return cpu_vsr[n];
7584     } else {
7585         return cpu_avrl[n-32];
7586     }
7587 }
7588
7589 #define VSX_LOAD_SCALAR(name, operation)                      \
7590 static void gen_##name(DisasContext *ctx)                     \
7591 {                                                             \
7592     TCGv EA;                                                  \
7593     if (unlikely(!ctx->vsx_enabled)) {                        \
7594         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7595         return;                                               \
7596     }                                                         \
7597     gen_set_access_type(ctx, ACCESS_INT);                     \
7598     EA = tcg_temp_new();                                      \
7599     gen_addr_reg_index(ctx, EA);                              \
7600     gen_qemu_##operation(ctx, cpu_vsrh(xT(ctx->opcode)), EA); \
7601     /* NOTE: cpu_vsrl is undefined */                         \
7602     tcg_temp_free(EA);                                        \
7603 }
7604
7605 VSX_LOAD_SCALAR(lxsdx, ld64)
7606 VSX_LOAD_SCALAR(lxsiwax, ld32s_i64)
7607 VSX_LOAD_SCALAR(lxsiwzx, ld32u_i64)
7608 VSX_LOAD_SCALAR(lxsspx, ld32fs)
7609
7610 static void gen_lxvd2x(DisasContext *ctx)
7611 {
7612     TCGv EA;
7613     if (unlikely(!ctx->vsx_enabled)) {
7614         gen_exception(ctx, POWERPC_EXCP_VSXU);
7615         return;
7616     }
7617     gen_set_access_type(ctx, ACCESS_INT);
7618     EA = tcg_temp_new();
7619     gen_addr_reg_index(ctx, EA);
7620     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7621     tcg_gen_addi_tl(EA, EA, 8);
7622     gen_qemu_ld64(ctx, cpu_vsrl(xT(ctx->opcode)), EA);
7623     tcg_temp_free(EA);
7624 }
7625
7626 static void gen_lxvdsx(DisasContext *ctx)
7627 {
7628     TCGv EA;
7629     if (unlikely(!ctx->vsx_enabled)) {
7630         gen_exception(ctx, POWERPC_EXCP_VSXU);
7631         return;
7632     }
7633     gen_set_access_type(ctx, ACCESS_INT);
7634     EA = tcg_temp_new();
7635     gen_addr_reg_index(ctx, EA);
7636     gen_qemu_ld64(ctx, cpu_vsrh(xT(ctx->opcode)), EA);
7637     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
7638     tcg_temp_free(EA);
7639 }
7640
7641 static void gen_lxvw4x(DisasContext *ctx)
7642 {
7643     TCGv EA;
7644     TCGv_i64 tmp;
7645     TCGv_i64 xth = cpu_vsrh(xT(ctx->opcode));
7646     TCGv_i64 xtl = cpu_vsrl(xT(ctx->opcode));
7647     if (unlikely(!ctx->vsx_enabled)) {
7648         gen_exception(ctx, POWERPC_EXCP_VSXU);
7649         return;
7650     }
7651     gen_set_access_type(ctx, ACCESS_INT);
7652     EA = tcg_temp_new();
7653     tmp = tcg_temp_new_i64();
7654
7655     gen_addr_reg_index(ctx, EA);
7656     gen_qemu_ld32u_i64(ctx, tmp, EA);
7657     tcg_gen_addi_tl(EA, EA, 4);
7658     gen_qemu_ld32u_i64(ctx, xth, EA);
7659     tcg_gen_deposit_i64(xth, xth, tmp, 32, 32);
7660
7661     tcg_gen_addi_tl(EA, EA, 4);
7662     gen_qemu_ld32u_i64(ctx, tmp, EA);
7663     tcg_gen_addi_tl(EA, EA, 4);
7664     gen_qemu_ld32u_i64(ctx, xtl, EA);
7665     tcg_gen_deposit_i64(xtl, xtl, tmp, 32, 32);
7666
7667     tcg_temp_free(EA);
7668     tcg_temp_free_i64(tmp);
7669 }
7670
7671 #define VSX_STORE_SCALAR(name, operation)                     \
7672 static void gen_##name(DisasContext *ctx)                     \
7673 {                                                             \
7674     TCGv EA;                                                  \
7675     if (unlikely(!ctx->vsx_enabled)) {                        \
7676         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7677         return;                                               \
7678     }                                                         \
7679     gen_set_access_type(ctx, ACCESS_INT);                     \
7680     EA = tcg_temp_new();                                      \
7681     gen_addr_reg_index(ctx, EA);                              \
7682     gen_qemu_##operation(ctx, cpu_vsrh(xS(ctx->opcode)), EA); \
7683     tcg_temp_free(EA);                                        \
7684 }
7685
7686 VSX_STORE_SCALAR(stxsdx, st64)
7687 VSX_STORE_SCALAR(stxsiwx, st32_i64)
7688 VSX_STORE_SCALAR(stxsspx, st32fs)
7689
7690 static void gen_stxvd2x(DisasContext *ctx)
7691 {
7692     TCGv EA;
7693     if (unlikely(!ctx->vsx_enabled)) {
7694         gen_exception(ctx, POWERPC_EXCP_VSXU);
7695         return;
7696     }
7697     gen_set_access_type(ctx, ACCESS_INT);
7698     EA = tcg_temp_new();
7699     gen_addr_reg_index(ctx, EA);
7700     gen_qemu_st64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7701     tcg_gen_addi_tl(EA, EA, 8);
7702     gen_qemu_st64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7703     tcg_temp_free(EA);
7704 }
7705
7706 static void gen_stxvw4x(DisasContext *ctx)
7707 {
7708     TCGv_i64 tmp;
7709     TCGv EA;
7710     if (unlikely(!ctx->vsx_enabled)) {
7711         gen_exception(ctx, POWERPC_EXCP_VSXU);
7712         return;
7713     }
7714     gen_set_access_type(ctx, ACCESS_INT);
7715     EA = tcg_temp_new();
7716     gen_addr_reg_index(ctx, EA);
7717     tmp = tcg_temp_new_i64();
7718
7719     tcg_gen_shri_i64(tmp, cpu_vsrh(xS(ctx->opcode)), 32);
7720     gen_qemu_st32_i64(ctx, tmp, EA);
7721     tcg_gen_addi_tl(EA, EA, 4);
7722     gen_qemu_st32_i64(ctx, cpu_vsrh(xS(ctx->opcode)), EA);
7723
7724     tcg_gen_shri_i64(tmp, cpu_vsrl(xS(ctx->opcode)), 32);
7725     tcg_gen_addi_tl(EA, EA, 4);
7726     gen_qemu_st32_i64(ctx, tmp, EA);
7727     tcg_gen_addi_tl(EA, EA, 4);
7728     gen_qemu_st32_i64(ctx, cpu_vsrl(xS(ctx->opcode)), EA);
7729
7730     tcg_temp_free(EA);
7731     tcg_temp_free_i64(tmp);
7732 }
7733
7734 #define MV_VSRW(name, tcgop1, tcgop2, target, source)           \
7735 static void gen_##name(DisasContext *ctx)                       \
7736 {                                                               \
7737     if (xS(ctx->opcode) < 32) {                                 \
7738         if (unlikely(!ctx->fpu_enabled)) {                      \
7739             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7740             return;                                             \
7741         }                                                       \
7742     } else {                                                    \
7743         if (unlikely(!ctx->altivec_enabled)) {                  \
7744             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7745             return;                                             \
7746         }                                                       \
7747     }                                                           \
7748     TCGv_i64 tmp = tcg_temp_new_i64();                          \
7749     tcg_gen_##tcgop1(tmp, source);                              \
7750     tcg_gen_##tcgop2(target, tmp);                              \
7751     tcg_temp_free_i64(tmp);                                     \
7752 }
7753
7754
7755 MV_VSRW(mfvsrwz, ext32u_i64, trunc_i64_tl, cpu_gpr[rA(ctx->opcode)], \
7756         cpu_vsrh(xS(ctx->opcode)))
7757 MV_VSRW(mtvsrwa, extu_tl_i64, ext32s_i64, cpu_vsrh(xT(ctx->opcode)), \
7758         cpu_gpr[rA(ctx->opcode)])
7759 MV_VSRW(mtvsrwz, extu_tl_i64, ext32u_i64, cpu_vsrh(xT(ctx->opcode)), \
7760         cpu_gpr[rA(ctx->opcode)])
7761
7762 #if defined(TARGET_PPC64)
7763 #define MV_VSRD(name, target, source)                           \
7764 static void gen_##name(DisasContext *ctx)                       \
7765 {                                                               \
7766     if (xS(ctx->opcode) < 32) {                                 \
7767         if (unlikely(!ctx->fpu_enabled)) {                      \
7768             gen_exception(ctx, POWERPC_EXCP_FPU);               \
7769             return;                                             \
7770         }                                                       \
7771     } else {                                                    \
7772         if (unlikely(!ctx->altivec_enabled)) {                  \
7773             gen_exception(ctx, POWERPC_EXCP_VPU);               \
7774             return;                                             \
7775         }                                                       \
7776     }                                                           \
7777     tcg_gen_mov_i64(target, source);                            \
7778 }
7779
7780 MV_VSRD(mfvsrd, cpu_gpr[rA(ctx->opcode)], cpu_vsrh(xS(ctx->opcode)))
7781 MV_VSRD(mtvsrd, cpu_vsrh(xT(ctx->opcode)), cpu_gpr[rA(ctx->opcode)])
7782
7783 #endif
7784
7785 static void gen_xxpermdi(DisasContext *ctx)
7786 {
7787     if (unlikely(!ctx->vsx_enabled)) {
7788         gen_exception(ctx, POWERPC_EXCP_VSXU);
7789         return;
7790     }
7791
7792     if (unlikely((xT(ctx->opcode) == xA(ctx->opcode)) ||
7793                  (xT(ctx->opcode) == xB(ctx->opcode)))) {
7794         TCGv_i64 xh, xl;
7795
7796         xh = tcg_temp_new_i64();
7797         xl = tcg_temp_new_i64();
7798
7799         if ((DM(ctx->opcode) & 2) == 0) {
7800             tcg_gen_mov_i64(xh, cpu_vsrh(xA(ctx->opcode)));
7801         } else {
7802             tcg_gen_mov_i64(xh, cpu_vsrl(xA(ctx->opcode)));
7803         }
7804         if ((DM(ctx->opcode) & 1) == 0) {
7805             tcg_gen_mov_i64(xl, cpu_vsrh(xB(ctx->opcode)));
7806         } else {
7807             tcg_gen_mov_i64(xl, cpu_vsrl(xB(ctx->opcode)));
7808         }
7809
7810         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xh);
7811         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xl);
7812
7813         tcg_temp_free_i64(xh);
7814         tcg_temp_free_i64(xl);
7815     } else {
7816         if ((DM(ctx->opcode) & 2) == 0) {
7817             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)));
7818         } else {
7819             tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)));
7820         }
7821         if ((DM(ctx->opcode) & 1) == 0) {
7822             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xB(ctx->opcode)));
7823         } else {
7824             tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xB(ctx->opcode)));
7825         }
7826     }
7827 }
7828
7829 #define OP_ABS 1
7830 #define OP_NABS 2
7831 #define OP_NEG 3
7832 #define OP_CPSGN 4
7833 #define SGN_MASK_DP  0x8000000000000000ull
7834 #define SGN_MASK_SP 0x8000000080000000ull
7835
7836 #define VSX_SCALAR_MOVE(name, op, sgn_mask)                       \
7837 static void glue(gen_, name)(DisasContext * ctx)                  \
7838     {                                                             \
7839         TCGv_i64 xb, sgm;                                         \
7840         if (unlikely(!ctx->vsx_enabled)) {                        \
7841             gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7842             return;                                               \
7843         }                                                         \
7844         xb = tcg_temp_new_i64();                                  \
7845         sgm = tcg_temp_new_i64();                                 \
7846         tcg_gen_mov_i64(xb, cpu_vsrh(xB(ctx->opcode)));           \
7847         tcg_gen_movi_i64(sgm, sgn_mask);                          \
7848         switch (op) {                                             \
7849             case OP_ABS: {                                        \
7850                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7851                 break;                                            \
7852             }                                                     \
7853             case OP_NABS: {                                       \
7854                 tcg_gen_or_i64(xb, xb, sgm);                      \
7855                 break;                                            \
7856             }                                                     \
7857             case OP_NEG: {                                        \
7858                 tcg_gen_xor_i64(xb, xb, sgm);                     \
7859                 break;                                            \
7860             }                                                     \
7861             case OP_CPSGN: {                                      \
7862                 TCGv_i64 xa = tcg_temp_new_i64();                 \
7863                 tcg_gen_mov_i64(xa, cpu_vsrh(xA(ctx->opcode)));   \
7864                 tcg_gen_and_i64(xa, xa, sgm);                     \
7865                 tcg_gen_andc_i64(xb, xb, sgm);                    \
7866                 tcg_gen_or_i64(xb, xb, xa);                       \
7867                 tcg_temp_free_i64(xa);                            \
7868                 break;                                            \
7869             }                                                     \
7870         }                                                         \
7871         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xb);           \
7872         tcg_temp_free_i64(xb);                                    \
7873         tcg_temp_free_i64(sgm);                                   \
7874     }
7875
7876 VSX_SCALAR_MOVE(xsabsdp, OP_ABS, SGN_MASK_DP)
7877 VSX_SCALAR_MOVE(xsnabsdp, OP_NABS, SGN_MASK_DP)
7878 VSX_SCALAR_MOVE(xsnegdp, OP_NEG, SGN_MASK_DP)
7879 VSX_SCALAR_MOVE(xscpsgndp, OP_CPSGN, SGN_MASK_DP)
7880
7881 #define VSX_VECTOR_MOVE(name, op, sgn_mask)                      \
7882 static void glue(gen_, name)(DisasContext * ctx)                 \
7883     {                                                            \
7884         TCGv_i64 xbh, xbl, sgm;                                  \
7885         if (unlikely(!ctx->vsx_enabled)) {                       \
7886             gen_exception(ctx, POWERPC_EXCP_VSXU);               \
7887             return;                                              \
7888         }                                                        \
7889         xbh = tcg_temp_new_i64();                                \
7890         xbl = tcg_temp_new_i64();                                \
7891         sgm = tcg_temp_new_i64();                                \
7892         tcg_gen_mov_i64(xbh, cpu_vsrh(xB(ctx->opcode)));         \
7893         tcg_gen_mov_i64(xbl, cpu_vsrl(xB(ctx->opcode)));         \
7894         tcg_gen_movi_i64(sgm, sgn_mask);                         \
7895         switch (op) {                                            \
7896             case OP_ABS: {                                       \
7897                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7898                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7899                 break;                                           \
7900             }                                                    \
7901             case OP_NABS: {                                      \
7902                 tcg_gen_or_i64(xbh, xbh, sgm);                   \
7903                 tcg_gen_or_i64(xbl, xbl, sgm);                   \
7904                 break;                                           \
7905             }                                                    \
7906             case OP_NEG: {                                       \
7907                 tcg_gen_xor_i64(xbh, xbh, sgm);                  \
7908                 tcg_gen_xor_i64(xbl, xbl, sgm);                  \
7909                 break;                                           \
7910             }                                                    \
7911             case OP_CPSGN: {                                     \
7912                 TCGv_i64 xah = tcg_temp_new_i64();               \
7913                 TCGv_i64 xal = tcg_temp_new_i64();               \
7914                 tcg_gen_mov_i64(xah, cpu_vsrh(xA(ctx->opcode))); \
7915                 tcg_gen_mov_i64(xal, cpu_vsrl(xA(ctx->opcode))); \
7916                 tcg_gen_and_i64(xah, xah, sgm);                  \
7917                 tcg_gen_and_i64(xal, xal, sgm);                  \
7918                 tcg_gen_andc_i64(xbh, xbh, sgm);                 \
7919                 tcg_gen_andc_i64(xbl, xbl, sgm);                 \
7920                 tcg_gen_or_i64(xbh, xbh, xah);                   \
7921                 tcg_gen_or_i64(xbl, xbl, xal);                   \
7922                 tcg_temp_free_i64(xah);                          \
7923                 tcg_temp_free_i64(xal);                          \
7924                 break;                                           \
7925             }                                                    \
7926         }                                                        \
7927         tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xbh);         \
7928         tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xbl);         \
7929         tcg_temp_free_i64(xbh);                                  \
7930         tcg_temp_free_i64(xbl);                                  \
7931         tcg_temp_free_i64(sgm);                                  \
7932     }
7933
7934 VSX_VECTOR_MOVE(xvabsdp, OP_ABS, SGN_MASK_DP)
7935 VSX_VECTOR_MOVE(xvnabsdp, OP_NABS, SGN_MASK_DP)
7936 VSX_VECTOR_MOVE(xvnegdp, OP_NEG, SGN_MASK_DP)
7937 VSX_VECTOR_MOVE(xvcpsgndp, OP_CPSGN, SGN_MASK_DP)
7938 VSX_VECTOR_MOVE(xvabssp, OP_ABS, SGN_MASK_SP)
7939 VSX_VECTOR_MOVE(xvnabssp, OP_NABS, SGN_MASK_SP)
7940 VSX_VECTOR_MOVE(xvnegsp, OP_NEG, SGN_MASK_SP)
7941 VSX_VECTOR_MOVE(xvcpsgnsp, OP_CPSGN, SGN_MASK_SP)
7942
7943 #define GEN_VSX_HELPER_2(name, op1, op2, inval, type)                         \
7944 static void gen_##name(DisasContext * ctx)                                    \
7945 {                                                                             \
7946     TCGv_i32 opc;                                                             \
7947     if (unlikely(!ctx->vsx_enabled)) {                                        \
7948         gen_exception(ctx, POWERPC_EXCP_VSXU);                                \
7949         return;                                                               \
7950     }                                                                         \
7951     /* NIP cannot be restored if the memory exception comes from an helper */ \
7952     gen_update_nip(ctx, ctx->nip - 4);                                        \
7953     opc = tcg_const_i32(ctx->opcode);                                         \
7954     gen_helper_##name(cpu_env, opc);                                          \
7955     tcg_temp_free_i32(opc);                                                   \
7956 }
7957
7958 #define GEN_VSX_HELPER_XT_XB_ENV(name, op1, op2, inval, type) \
7959 static void gen_##name(DisasContext * ctx)                    \
7960 {                                                             \
7961     if (unlikely(!ctx->vsx_enabled)) {                        \
7962         gen_exception(ctx, POWERPC_EXCP_VSXU);                \
7963         return;                                               \
7964     }                                                         \
7965     /* NIP cannot be restored if the exception comes */       \
7966     /* from a helper. */                                      \
7967     gen_update_nip(ctx, ctx->nip - 4);                        \
7968                                                               \
7969     gen_helper_##name(cpu_vsrh(xT(ctx->opcode)), cpu_env,     \
7970                       cpu_vsrh(xB(ctx->opcode)));             \
7971 }
7972
7973 GEN_VSX_HELPER_2(xsadddp, 0x00, 0x04, 0, PPC2_VSX)
7974 GEN_VSX_HELPER_2(xssubdp, 0x00, 0x05, 0, PPC2_VSX)
7975 GEN_VSX_HELPER_2(xsmuldp, 0x00, 0x06, 0, PPC2_VSX)
7976 GEN_VSX_HELPER_2(xsdivdp, 0x00, 0x07, 0, PPC2_VSX)
7977 GEN_VSX_HELPER_2(xsredp, 0x14, 0x05, 0, PPC2_VSX)
7978 GEN_VSX_HELPER_2(xssqrtdp, 0x16, 0x04, 0, PPC2_VSX)
7979 GEN_VSX_HELPER_2(xsrsqrtedp, 0x14, 0x04, 0, PPC2_VSX)
7980 GEN_VSX_HELPER_2(xstdivdp, 0x14, 0x07, 0, PPC2_VSX)
7981 GEN_VSX_HELPER_2(xstsqrtdp, 0x14, 0x06, 0, PPC2_VSX)
7982 GEN_VSX_HELPER_2(xsmaddadp, 0x04, 0x04, 0, PPC2_VSX)
7983 GEN_VSX_HELPER_2(xsmaddmdp, 0x04, 0x05, 0, PPC2_VSX)
7984 GEN_VSX_HELPER_2(xsmsubadp, 0x04, 0x06, 0, PPC2_VSX)
7985 GEN_VSX_HELPER_2(xsmsubmdp, 0x04, 0x07, 0, PPC2_VSX)
7986 GEN_VSX_HELPER_2(xsnmaddadp, 0x04, 0x14, 0, PPC2_VSX)
7987 GEN_VSX_HELPER_2(xsnmaddmdp, 0x04, 0x15, 0, PPC2_VSX)
7988 GEN_VSX_HELPER_2(xsnmsubadp, 0x04, 0x16, 0, PPC2_VSX)
7989 GEN_VSX_HELPER_2(xsnmsubmdp, 0x04, 0x17, 0, PPC2_VSX)
7990 GEN_VSX_HELPER_2(xscmpodp, 0x0C, 0x05, 0, PPC2_VSX)
7991 GEN_VSX_HELPER_2(xscmpudp, 0x0C, 0x04, 0, PPC2_VSX)
7992 GEN_VSX_HELPER_2(xsmaxdp, 0x00, 0x14, 0, PPC2_VSX)
7993 GEN_VSX_HELPER_2(xsmindp, 0x00, 0x15, 0, PPC2_VSX)
7994 GEN_VSX_HELPER_2(xscvdpsp, 0x12, 0x10, 0, PPC2_VSX)
7995 GEN_VSX_HELPER_XT_XB_ENV(xscvdpspn, 0x16, 0x10, 0, PPC2_VSX207)
7996 GEN_VSX_HELPER_2(xscvspdp, 0x12, 0x14, 0, PPC2_VSX)
7997 GEN_VSX_HELPER_XT_XB_ENV(xscvspdpn, 0x16, 0x14, 0, PPC2_VSX207)
7998 GEN_VSX_HELPER_2(xscvdpsxds, 0x10, 0x15, 0, PPC2_VSX)
7999 GEN_VSX_HELPER_2(xscvdpsxws, 0x10, 0x05, 0, PPC2_VSX)
8000 GEN_VSX_HELPER_2(xscvdpuxds, 0x10, 0x14, 0, PPC2_VSX)
8001 GEN_VSX_HELPER_2(xscvdpuxws, 0x10, 0x04, 0, PPC2_VSX)
8002 GEN_VSX_HELPER_2(xscvsxddp, 0x10, 0x17, 0, PPC2_VSX)
8003 GEN_VSX_HELPER_2(xscvuxddp, 0x10, 0x16, 0, PPC2_VSX)
8004 GEN_VSX_HELPER_2(xsrdpi, 0x12, 0x04, 0, PPC2_VSX)
8005 GEN_VSX_HELPER_2(xsrdpic, 0x16, 0x06, 0, PPC2_VSX)
8006 GEN_VSX_HELPER_2(xsrdpim, 0x12, 0x07, 0, PPC2_VSX)
8007 GEN_VSX_HELPER_2(xsrdpip, 0x12, 0x06, 0, PPC2_VSX)
8008 GEN_VSX_HELPER_2(xsrdpiz, 0x12, 0x05, 0, PPC2_VSX)
8009 GEN_VSX_HELPER_XT_XB_ENV(xsrsp, 0x12, 0x11, 0, PPC2_VSX207)
8010
8011 GEN_VSX_HELPER_2(xsaddsp, 0x00, 0x00, 0, PPC2_VSX207)
8012 GEN_VSX_HELPER_2(xssubsp, 0x00, 0x01, 0, PPC2_VSX207)
8013 GEN_VSX_HELPER_2(xsmulsp, 0x00, 0x02, 0, PPC2_VSX207)
8014 GEN_VSX_HELPER_2(xsdivsp, 0x00, 0x03, 0, PPC2_VSX207)
8015 GEN_VSX_HELPER_2(xsresp, 0x14, 0x01, 0, PPC2_VSX207)
8016 GEN_VSX_HELPER_2(xssqrtsp, 0x16, 0x00, 0, PPC2_VSX207)
8017 GEN_VSX_HELPER_2(xsrsqrtesp, 0x14, 0x00, 0, PPC2_VSX207)
8018 GEN_VSX_HELPER_2(xsmaddasp, 0x04, 0x00, 0, PPC2_VSX207)
8019 GEN_VSX_HELPER_2(xsmaddmsp, 0x04, 0x01, 0, PPC2_VSX207)
8020 GEN_VSX_HELPER_2(xsmsubasp, 0x04, 0x02, 0, PPC2_VSX207)
8021 GEN_VSX_HELPER_2(xsmsubmsp, 0x04, 0x03, 0, PPC2_VSX207)
8022 GEN_VSX_HELPER_2(xsnmaddasp, 0x04, 0x10, 0, PPC2_VSX207)
8023 GEN_VSX_HELPER_2(xsnmaddmsp, 0x04, 0x11, 0, PPC2_VSX207)
8024 GEN_VSX_HELPER_2(xsnmsubasp, 0x04, 0x12, 0, PPC2_VSX207)
8025 GEN_VSX_HELPER_2(xsnmsubmsp, 0x04, 0x13, 0, PPC2_VSX207)
8026 GEN_VSX_HELPER_2(xscvsxdsp, 0x10, 0x13, 0, PPC2_VSX207)
8027 GEN_VSX_HELPER_2(xscvuxdsp, 0x10, 0x12, 0, PPC2_VSX207)
8028
8029 GEN_VSX_HELPER_2(xvadddp, 0x00, 0x0C, 0, PPC2_VSX)
8030 GEN_VSX_HELPER_2(xvsubdp, 0x00, 0x0D, 0, PPC2_VSX)
8031 GEN_VSX_HELPER_2(xvmuldp, 0x00, 0x0E, 0, PPC2_VSX)
8032 GEN_VSX_HELPER_2(xvdivdp, 0x00, 0x0F, 0, PPC2_VSX)
8033 GEN_VSX_HELPER_2(xvredp, 0x14, 0x0D, 0, PPC2_VSX)
8034 GEN_VSX_HELPER_2(xvsqrtdp, 0x16, 0x0C, 0, PPC2_VSX)
8035 GEN_VSX_HELPER_2(xvrsqrtedp, 0x14, 0x0C, 0, PPC2_VSX)
8036 GEN_VSX_HELPER_2(xvtdivdp, 0x14, 0x0F, 0, PPC2_VSX)
8037 GEN_VSX_HELPER_2(xvtsqrtdp, 0x14, 0x0E, 0, PPC2_VSX)
8038 GEN_VSX_HELPER_2(xvmaddadp, 0x04, 0x0C, 0, PPC2_VSX)
8039 GEN_VSX_HELPER_2(xvmaddmdp, 0x04, 0x0D, 0, PPC2_VSX)
8040 GEN_VSX_HELPER_2(xvmsubadp, 0x04, 0x0E, 0, PPC2_VSX)
8041 GEN_VSX_HELPER_2(xvmsubmdp, 0x04, 0x0F, 0, PPC2_VSX)
8042 GEN_VSX_HELPER_2(xvnmaddadp, 0x04, 0x1C, 0, PPC2_VSX)
8043 GEN_VSX_HELPER_2(xvnmaddmdp, 0x04, 0x1D, 0, PPC2_VSX)
8044 GEN_VSX_HELPER_2(xvnmsubadp, 0x04, 0x1E, 0, PPC2_VSX)
8045 GEN_VSX_HELPER_2(xvnmsubmdp, 0x04, 0x1F, 0, PPC2_VSX)
8046 GEN_VSX_HELPER_2(xvmaxdp, 0x00, 0x1C, 0, PPC2_VSX)
8047 GEN_VSX_HELPER_2(xvmindp, 0x00, 0x1D, 0, PPC2_VSX)
8048 GEN_VSX_HELPER_2(xvcmpeqdp, 0x0C, 0x0C, 0, PPC2_VSX)
8049 GEN_VSX_HELPER_2(xvcmpgtdp, 0x0C, 0x0D, 0, PPC2_VSX)
8050 GEN_VSX_HELPER_2(xvcmpgedp, 0x0C, 0x0E, 0, PPC2_VSX)
8051 GEN_VSX_HELPER_2(xvcvdpsp, 0x12, 0x18, 0, PPC2_VSX)
8052 GEN_VSX_HELPER_2(xvcvdpsxds, 0x10, 0x1D, 0, PPC2_VSX)
8053 GEN_VSX_HELPER_2(xvcvdpsxws, 0x10, 0x0D, 0, PPC2_VSX)
8054 GEN_VSX_HELPER_2(xvcvdpuxds, 0x10, 0x1C, 0, PPC2_VSX)
8055 GEN_VSX_HELPER_2(xvcvdpuxws, 0x10, 0x0C, 0, PPC2_VSX)
8056 GEN_VSX_HELPER_2(xvcvsxddp, 0x10, 0x1F, 0, PPC2_VSX)
8057 GEN_VSX_HELPER_2(xvcvuxddp, 0x10, 0x1E, 0, PPC2_VSX)
8058 GEN_VSX_HELPER_2(xvcvsxwdp, 0x10, 0x0F, 0, PPC2_VSX)
8059 GEN_VSX_HELPER_2(xvcvuxwdp, 0x10, 0x0E, 0, PPC2_VSX)
8060 GEN_VSX_HELPER_2(xvrdpi, 0x12, 0x0C, 0, PPC2_VSX)
8061 GEN_VSX_HELPER_2(xvrdpic, 0x16, 0x0E, 0, PPC2_VSX)
8062 GEN_VSX_HELPER_2(xvrdpim, 0x12, 0x0F, 0, PPC2_VSX)
8063 GEN_VSX_HELPER_2(xvrdpip, 0x12, 0x0E, 0, PPC2_VSX)
8064 GEN_VSX_HELPER_2(xvrdpiz, 0x12, 0x0D, 0, PPC2_VSX)
8065
8066 GEN_VSX_HELPER_2(xvaddsp, 0x00, 0x08, 0, PPC2_VSX)
8067 GEN_VSX_HELPER_2(xvsubsp, 0x00, 0x09, 0, PPC2_VSX)
8068 GEN_VSX_HELPER_2(xvmulsp, 0x00, 0x0A, 0, PPC2_VSX)
8069 GEN_VSX_HELPER_2(xvdivsp, 0x00, 0x0B, 0, PPC2_VSX)
8070 GEN_VSX_HELPER_2(xvresp, 0x14, 0x09, 0, PPC2_VSX)
8071 GEN_VSX_HELPER_2(xvsqrtsp, 0x16, 0x08, 0, PPC2_VSX)
8072 GEN_VSX_HELPER_2(xvrsqrtesp, 0x14, 0x08, 0, PPC2_VSX)
8073 GEN_VSX_HELPER_2(xvtdivsp, 0x14, 0x0B, 0, PPC2_VSX)
8074 GEN_VSX_HELPER_2(xvtsqrtsp, 0x14, 0x0A, 0, PPC2_VSX)
8075 GEN_VSX_HELPER_2(xvmaddasp, 0x04, 0x08, 0, PPC2_VSX)
8076 GEN_VSX_HELPER_2(xvmaddmsp, 0x04, 0x09, 0, PPC2_VSX)
8077 GEN_VSX_HELPER_2(xvmsubasp, 0x04, 0x0A, 0, PPC2_VSX)
8078 GEN_VSX_HELPER_2(xvmsubmsp, 0x04, 0x0B, 0, PPC2_VSX)
8079 GEN_VSX_HELPER_2(xvnmaddasp, 0x04, 0x18, 0, PPC2_VSX)
8080 GEN_VSX_HELPER_2(xvnmaddmsp, 0x04, 0x19, 0, PPC2_VSX)
8081 GEN_VSX_HELPER_2(xvnmsubasp, 0x04, 0x1A, 0, PPC2_VSX)
8082 GEN_VSX_HELPER_2(xvnmsubmsp, 0x04, 0x1B, 0, PPC2_VSX)
8083 GEN_VSX_HELPER_2(xvmaxsp, 0x00, 0x18, 0, PPC2_VSX)
8084 GEN_VSX_HELPER_2(xvminsp, 0x00, 0x19, 0, PPC2_VSX)
8085 GEN_VSX_HELPER_2(xvcmpeqsp, 0x0C, 0x08, 0, PPC2_VSX)
8086 GEN_VSX_HELPER_2(xvcmpgtsp, 0x0C, 0x09, 0, PPC2_VSX)
8087 GEN_VSX_HELPER_2(xvcmpgesp, 0x0C, 0x0A, 0, PPC2_VSX)
8088 GEN_VSX_HELPER_2(xvcvspdp, 0x12, 0x1C, 0, PPC2_VSX)
8089 GEN_VSX_HELPER_2(xvcvspsxds, 0x10, 0x19, 0, PPC2_VSX)
8090 GEN_VSX_HELPER_2(xvcvspsxws, 0x10, 0x09, 0, PPC2_VSX)
8091 GEN_VSX_HELPER_2(xvcvspuxds, 0x10, 0x18, 0, PPC2_VSX)
8092 GEN_VSX_HELPER_2(xvcvspuxws, 0x10, 0x08, 0, PPC2_VSX)
8093 GEN_VSX_HELPER_2(xvcvsxdsp, 0x10, 0x1B, 0, PPC2_VSX)
8094 GEN_VSX_HELPER_2(xvcvuxdsp, 0x10, 0x1A, 0, PPC2_VSX)
8095 GEN_VSX_HELPER_2(xvcvsxwsp, 0x10, 0x0B, 0, PPC2_VSX)
8096 GEN_VSX_HELPER_2(xvcvuxwsp, 0x10, 0x0A, 0, PPC2_VSX)
8097 GEN_VSX_HELPER_2(xvrspi, 0x12, 0x08, 0, PPC2_VSX)
8098 GEN_VSX_HELPER_2(xvrspic, 0x16, 0x0A, 0, PPC2_VSX)
8099 GEN_VSX_HELPER_2(xvrspim, 0x12, 0x0B, 0, PPC2_VSX)
8100 GEN_VSX_HELPER_2(xvrspip, 0x12, 0x0A, 0, PPC2_VSX)
8101 GEN_VSX_HELPER_2(xvrspiz, 0x12, 0x09, 0, PPC2_VSX)
8102
8103 #define VSX_LOGICAL(name, tcg_op)                                    \
8104 static void glue(gen_, name)(DisasContext * ctx)                     \
8105     {                                                                \
8106         if (unlikely(!ctx->vsx_enabled)) {                           \
8107             gen_exception(ctx, POWERPC_EXCP_VSXU);                   \
8108             return;                                                  \
8109         }                                                            \
8110         tcg_op(cpu_vsrh(xT(ctx->opcode)), cpu_vsrh(xA(ctx->opcode)), \
8111             cpu_vsrh(xB(ctx->opcode)));                              \
8112         tcg_op(cpu_vsrl(xT(ctx->opcode)), cpu_vsrl(xA(ctx->opcode)), \
8113             cpu_vsrl(xB(ctx->opcode)));                              \
8114     }
8115
8116 VSX_LOGICAL(xxland, tcg_gen_and_i64)
8117 VSX_LOGICAL(xxlandc, tcg_gen_andc_i64)
8118 VSX_LOGICAL(xxlor, tcg_gen_or_i64)
8119 VSX_LOGICAL(xxlxor, tcg_gen_xor_i64)
8120 VSX_LOGICAL(xxlnor, tcg_gen_nor_i64)
8121 VSX_LOGICAL(xxleqv, tcg_gen_eqv_i64)
8122 VSX_LOGICAL(xxlnand, tcg_gen_nand_i64)
8123 VSX_LOGICAL(xxlorc, tcg_gen_orc_i64)
8124
8125 #define VSX_XXMRG(name, high)                               \
8126 static void glue(gen_, name)(DisasContext * ctx)            \
8127     {                                                       \
8128         TCGv_i64 a0, a1, b0, b1;                            \
8129         if (unlikely(!ctx->vsx_enabled)) {                  \
8130             gen_exception(ctx, POWERPC_EXCP_VSXU);          \
8131             return;                                         \
8132         }                                                   \
8133         a0 = tcg_temp_new_i64();                            \
8134         a1 = tcg_temp_new_i64();                            \
8135         b0 = tcg_temp_new_i64();                            \
8136         b1 = tcg_temp_new_i64();                            \
8137         if (high) {                                         \
8138             tcg_gen_mov_i64(a0, cpu_vsrh(xA(ctx->opcode))); \
8139             tcg_gen_mov_i64(a1, cpu_vsrh(xA(ctx->opcode))); \
8140             tcg_gen_mov_i64(b0, cpu_vsrh(xB(ctx->opcode))); \
8141             tcg_gen_mov_i64(b1, cpu_vsrh(xB(ctx->opcode))); \
8142         } else {                                            \
8143             tcg_gen_mov_i64(a0, cpu_vsrl(xA(ctx->opcode))); \
8144             tcg_gen_mov_i64(a1, cpu_vsrl(xA(ctx->opcode))); \
8145             tcg_gen_mov_i64(b0, cpu_vsrl(xB(ctx->opcode))); \
8146             tcg_gen_mov_i64(b1, cpu_vsrl(xB(ctx->opcode))); \
8147         }                                                   \
8148         tcg_gen_shri_i64(a0, a0, 32);                       \
8149         tcg_gen_shri_i64(b0, b0, 32);                       \
8150         tcg_gen_deposit_i64(cpu_vsrh(xT(ctx->opcode)),      \
8151                             b0, a0, 32, 32);                \
8152         tcg_gen_deposit_i64(cpu_vsrl(xT(ctx->opcode)),      \
8153                             b1, a1, 32, 32);                \
8154         tcg_temp_free_i64(a0);                              \
8155         tcg_temp_free_i64(a1);                              \
8156         tcg_temp_free_i64(b0);                              \
8157         tcg_temp_free_i64(b1);                              \
8158     }
8159
8160 VSX_XXMRG(xxmrghw, 1)
8161 VSX_XXMRG(xxmrglw, 0)
8162
8163 static void gen_xxsel(DisasContext * ctx)
8164 {
8165     TCGv_i64 a, b, c;
8166     if (unlikely(!ctx->vsx_enabled)) {
8167         gen_exception(ctx, POWERPC_EXCP_VSXU);
8168         return;
8169     }
8170     a = tcg_temp_new_i64();
8171     b = tcg_temp_new_i64();
8172     c = tcg_temp_new_i64();
8173
8174     tcg_gen_mov_i64(a, cpu_vsrh(xA(ctx->opcode)));
8175     tcg_gen_mov_i64(b, cpu_vsrh(xB(ctx->opcode)));
8176     tcg_gen_mov_i64(c, cpu_vsrh(xC(ctx->opcode)));
8177
8178     tcg_gen_and_i64(b, b, c);
8179     tcg_gen_andc_i64(a, a, c);
8180     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), a, b);
8181
8182     tcg_gen_mov_i64(a, cpu_vsrl(xA(ctx->opcode)));
8183     tcg_gen_mov_i64(b, cpu_vsrl(xB(ctx->opcode)));
8184     tcg_gen_mov_i64(c, cpu_vsrl(xC(ctx->opcode)));
8185
8186     tcg_gen_and_i64(b, b, c);
8187     tcg_gen_andc_i64(a, a, c);
8188     tcg_gen_or_i64(cpu_vsrl(xT(ctx->opcode)), a, b);
8189
8190     tcg_temp_free_i64(a);
8191     tcg_temp_free_i64(b);
8192     tcg_temp_free_i64(c);
8193 }
8194
8195 static void gen_xxspltw(DisasContext *ctx)
8196 {
8197     TCGv_i64 b, b2;
8198     TCGv_i64 vsr = (UIM(ctx->opcode) & 2) ?
8199                    cpu_vsrl(xB(ctx->opcode)) :
8200                    cpu_vsrh(xB(ctx->opcode));
8201
8202     if (unlikely(!ctx->vsx_enabled)) {
8203         gen_exception(ctx, POWERPC_EXCP_VSXU);
8204         return;
8205     }
8206
8207     b = tcg_temp_new_i64();
8208     b2 = tcg_temp_new_i64();
8209
8210     if (UIM(ctx->opcode) & 1) {
8211         tcg_gen_ext32u_i64(b, vsr);
8212     } else {
8213         tcg_gen_shri_i64(b, vsr, 32);
8214     }
8215
8216     tcg_gen_shli_i64(b2, b, 32);
8217     tcg_gen_or_i64(cpu_vsrh(xT(ctx->opcode)), b, b2);
8218     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), cpu_vsrh(xT(ctx->opcode)));
8219
8220     tcg_temp_free_i64(b);
8221     tcg_temp_free_i64(b2);
8222 }
8223
8224 static void gen_xxsldwi(DisasContext *ctx)
8225 {
8226     TCGv_i64 xth, xtl;
8227     if (unlikely(!ctx->vsx_enabled)) {
8228         gen_exception(ctx, POWERPC_EXCP_VSXU);
8229         return;
8230     }
8231     xth = tcg_temp_new_i64();
8232     xtl = tcg_temp_new_i64();
8233
8234     switch (SHW(ctx->opcode)) {
8235         case 0: {
8236             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8237             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8238             break;
8239         }
8240         case 1: {
8241             TCGv_i64 t0 = tcg_temp_new_i64();
8242             tcg_gen_mov_i64(xth, cpu_vsrh(xA(ctx->opcode)));
8243             tcg_gen_shli_i64(xth, xth, 32);
8244             tcg_gen_mov_i64(t0, cpu_vsrl(xA(ctx->opcode)));
8245             tcg_gen_shri_i64(t0, t0, 32);
8246             tcg_gen_or_i64(xth, xth, t0);
8247             tcg_gen_mov_i64(xtl, cpu_vsrl(xA(ctx->opcode)));
8248             tcg_gen_shli_i64(xtl, xtl, 32);
8249             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8250             tcg_gen_shri_i64(t0, t0, 32);
8251             tcg_gen_or_i64(xtl, xtl, t0);
8252             tcg_temp_free_i64(t0);
8253             break;
8254         }
8255         case 2: {
8256             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8257             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8258             break;
8259         }
8260         case 3: {
8261             TCGv_i64 t0 = tcg_temp_new_i64();
8262             tcg_gen_mov_i64(xth, cpu_vsrl(xA(ctx->opcode)));
8263             tcg_gen_shli_i64(xth, xth, 32);
8264             tcg_gen_mov_i64(t0, cpu_vsrh(xB(ctx->opcode)));
8265             tcg_gen_shri_i64(t0, t0, 32);
8266             tcg_gen_or_i64(xth, xth, t0);
8267             tcg_gen_mov_i64(xtl, cpu_vsrh(xB(ctx->opcode)));
8268             tcg_gen_shli_i64(xtl, xtl, 32);
8269             tcg_gen_mov_i64(t0, cpu_vsrl(xB(ctx->opcode)));
8270             tcg_gen_shri_i64(t0, t0, 32);
8271             tcg_gen_or_i64(xtl, xtl, t0);
8272             tcg_temp_free_i64(t0);
8273             break;
8274         }
8275     }
8276
8277     tcg_gen_mov_i64(cpu_vsrh(xT(ctx->opcode)), xth);
8278     tcg_gen_mov_i64(cpu_vsrl(xT(ctx->opcode)), xtl);
8279
8280     tcg_temp_free_i64(xth);
8281     tcg_temp_free_i64(xtl);
8282 }
8283
8284 /*** Decimal Floating Point ***/
8285
8286 static inline TCGv_ptr gen_fprp_ptr(int reg)
8287 {
8288     TCGv_ptr r = tcg_temp_new_ptr();
8289     tcg_gen_addi_ptr(r, cpu_env, offsetof(CPUPPCState, fpr[reg]));
8290     return r;
8291 }
8292
8293 #define GEN_DFP_T_A_B_Rc(name)                   \
8294 static void gen_##name(DisasContext *ctx)        \
8295 {                                                \
8296     TCGv_ptr rd, ra, rb;                         \
8297     if (unlikely(!ctx->fpu_enabled)) {           \
8298         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8299         return;                                  \
8300     }                                            \
8301     gen_update_nip(ctx, ctx->nip - 4);           \
8302     rd = gen_fprp_ptr(rD(ctx->opcode));          \
8303     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8304     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8305     gen_helper_##name(cpu_env, rd, ra, rb);      \
8306     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8307         gen_set_cr1_from_fpscr(ctx);             \
8308     }                                            \
8309     tcg_temp_free_ptr(rd);                       \
8310     tcg_temp_free_ptr(ra);                       \
8311     tcg_temp_free_ptr(rb);                       \
8312 }
8313
8314 #define GEN_DFP_BF_A_B(name)                      \
8315 static void gen_##name(DisasContext *ctx)         \
8316 {                                                 \
8317     TCGv_ptr ra, rb;                              \
8318     if (unlikely(!ctx->fpu_enabled)) {            \
8319         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8320         return;                                   \
8321     }                                             \
8322     gen_update_nip(ctx, ctx->nip - 4);            \
8323     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8324     rb = gen_fprp_ptr(rB(ctx->opcode));           \
8325     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8326                       cpu_env, ra, rb);           \
8327     tcg_temp_free_ptr(ra);                        \
8328     tcg_temp_free_ptr(rb);                        \
8329 }
8330
8331 #define GEN_DFP_BF_A_DCM(name)                    \
8332 static void gen_##name(DisasContext *ctx)         \
8333 {                                                 \
8334     TCGv_ptr ra;                                  \
8335     TCGv_i32 dcm;                                 \
8336     if (unlikely(!ctx->fpu_enabled)) {            \
8337         gen_exception(ctx, POWERPC_EXCP_FPU);     \
8338         return;                                   \
8339     }                                             \
8340     gen_update_nip(ctx, ctx->nip - 4);            \
8341     ra = gen_fprp_ptr(rA(ctx->opcode));           \
8342     dcm = tcg_const_i32(DCM(ctx->opcode));        \
8343     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], \
8344                       cpu_env, ra, dcm);          \
8345     tcg_temp_free_ptr(ra);                        \
8346     tcg_temp_free_i32(dcm);                       \
8347 }
8348
8349 #define GEN_DFP_T_B_U32_U32_Rc(name, u32f1, u32f2)    \
8350 static void gen_##name(DisasContext *ctx)             \
8351 {                                                     \
8352     TCGv_ptr rt, rb;                                  \
8353     TCGv_i32 u32_1, u32_2;                            \
8354     if (unlikely(!ctx->fpu_enabled)) {                \
8355         gen_exception(ctx, POWERPC_EXCP_FPU);         \
8356         return;                                       \
8357     }                                                 \
8358     gen_update_nip(ctx, ctx->nip - 4);                \
8359     rt = gen_fprp_ptr(rD(ctx->opcode));               \
8360     rb = gen_fprp_ptr(rB(ctx->opcode));               \
8361     u32_1 = tcg_const_i32(u32f1(ctx->opcode));        \
8362     u32_2 = tcg_const_i32(u32f2(ctx->opcode));        \
8363     gen_helper_##name(cpu_env, rt, rb, u32_1, u32_2); \
8364     if (unlikely(Rc(ctx->opcode) != 0)) {             \
8365         gen_set_cr1_from_fpscr(ctx);                  \
8366     }                                                 \
8367     tcg_temp_free_ptr(rt);                            \
8368     tcg_temp_free_ptr(rb);                            \
8369     tcg_temp_free_i32(u32_1);                         \
8370     tcg_temp_free_i32(u32_2);                         \
8371 }
8372
8373 #define GEN_DFP_T_A_B_I32_Rc(name, i32fld)       \
8374 static void gen_##name(DisasContext *ctx)        \
8375 {                                                \
8376     TCGv_ptr rt, ra, rb;                         \
8377     TCGv_i32 i32;                                \
8378     if (unlikely(!ctx->fpu_enabled)) {           \
8379         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8380         return;                                  \
8381     }                                            \
8382     gen_update_nip(ctx, ctx->nip - 4);           \
8383     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8384     ra = gen_fprp_ptr(rA(ctx->opcode));          \
8385     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8386     i32 = tcg_const_i32(i32fld(ctx->opcode));    \
8387     gen_helper_##name(cpu_env, rt, ra, rb, i32); \
8388     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8389         gen_set_cr1_from_fpscr(ctx);             \
8390     }                                            \
8391     tcg_temp_free_ptr(rt);                       \
8392     tcg_temp_free_ptr(rb);                       \
8393     tcg_temp_free_ptr(ra);                       \
8394     tcg_temp_free_i32(i32);                      \
8395     }
8396
8397 #define GEN_DFP_T_B_Rc(name)                     \
8398 static void gen_##name(DisasContext *ctx)        \
8399 {                                                \
8400     TCGv_ptr rt, rb;                             \
8401     if (unlikely(!ctx->fpu_enabled)) {           \
8402         gen_exception(ctx, POWERPC_EXCP_FPU);    \
8403         return;                                  \
8404     }                                            \
8405     gen_update_nip(ctx, ctx->nip - 4);           \
8406     rt = gen_fprp_ptr(rD(ctx->opcode));          \
8407     rb = gen_fprp_ptr(rB(ctx->opcode));          \
8408     gen_helper_##name(cpu_env, rt, rb);          \
8409     if (unlikely(Rc(ctx->opcode) != 0)) {        \
8410         gen_set_cr1_from_fpscr(ctx);             \
8411     }                                            \
8412     tcg_temp_free_ptr(rt);                       \
8413     tcg_temp_free_ptr(rb);                       \
8414     }
8415
8416 #define GEN_DFP_T_FPR_I32_Rc(name, fprfld, i32fld) \
8417 static void gen_##name(DisasContext *ctx)          \
8418 {                                                  \
8419     TCGv_ptr rt, rs;                               \
8420     TCGv_i32 i32;                                  \
8421     if (unlikely(!ctx->fpu_enabled)) {             \
8422         gen_exception(ctx, POWERPC_EXCP_FPU);      \
8423         return;                                    \
8424     }                                              \
8425     gen_update_nip(ctx, ctx->nip - 4);             \
8426     rt = gen_fprp_ptr(rD(ctx->opcode));            \
8427     rs = gen_fprp_ptr(fprfld(ctx->opcode));        \
8428     i32 = tcg_const_i32(i32fld(ctx->opcode));      \
8429     gen_helper_##name(cpu_env, rt, rs, i32);       \
8430     if (unlikely(Rc(ctx->opcode) != 0)) {          \
8431         gen_set_cr1_from_fpscr(ctx);               \
8432     }                                              \
8433     tcg_temp_free_ptr(rt);                         \
8434     tcg_temp_free_ptr(rs);                         \
8435     tcg_temp_free_i32(i32);                        \
8436 }
8437
8438 GEN_DFP_T_A_B_Rc(dadd)
8439 GEN_DFP_T_A_B_Rc(daddq)
8440 GEN_DFP_T_A_B_Rc(dsub)
8441 GEN_DFP_T_A_B_Rc(dsubq)
8442 GEN_DFP_T_A_B_Rc(dmul)
8443 GEN_DFP_T_A_B_Rc(dmulq)
8444 GEN_DFP_T_A_B_Rc(ddiv)
8445 GEN_DFP_T_A_B_Rc(ddivq)
8446 GEN_DFP_BF_A_B(dcmpu)
8447 GEN_DFP_BF_A_B(dcmpuq)
8448 GEN_DFP_BF_A_B(dcmpo)
8449 GEN_DFP_BF_A_B(dcmpoq)
8450 GEN_DFP_BF_A_DCM(dtstdc)
8451 GEN_DFP_BF_A_DCM(dtstdcq)
8452 GEN_DFP_BF_A_DCM(dtstdg)
8453 GEN_DFP_BF_A_DCM(dtstdgq)
8454 GEN_DFP_BF_A_B(dtstex)
8455 GEN_DFP_BF_A_B(dtstexq)
8456 GEN_DFP_BF_A_B(dtstsf)
8457 GEN_DFP_BF_A_B(dtstsfq)
8458 GEN_DFP_T_B_U32_U32_Rc(dquai, SIMM5, RMC)
8459 GEN_DFP_T_B_U32_U32_Rc(dquaiq, SIMM5, RMC)
8460 GEN_DFP_T_A_B_I32_Rc(dqua, RMC)
8461 GEN_DFP_T_A_B_I32_Rc(dquaq, RMC)
8462 GEN_DFP_T_A_B_I32_Rc(drrnd, RMC)
8463 GEN_DFP_T_A_B_I32_Rc(drrndq, RMC)
8464 GEN_DFP_T_B_U32_U32_Rc(drintx, FPW, RMC)
8465 GEN_DFP_T_B_U32_U32_Rc(drintxq, FPW, RMC)
8466 GEN_DFP_T_B_U32_U32_Rc(drintn, FPW, RMC)
8467 GEN_DFP_T_B_U32_U32_Rc(drintnq, FPW, RMC)
8468 GEN_DFP_T_B_Rc(dctdp)
8469 GEN_DFP_T_B_Rc(dctqpq)
8470 GEN_DFP_T_B_Rc(drsp)
8471 GEN_DFP_T_B_Rc(drdpq)
8472 GEN_DFP_T_B_Rc(dcffix)
8473 GEN_DFP_T_B_Rc(dcffixq)
8474 GEN_DFP_T_B_Rc(dctfix)
8475 GEN_DFP_T_B_Rc(dctfixq)
8476 GEN_DFP_T_FPR_I32_Rc(ddedpd, rB, SP)
8477 GEN_DFP_T_FPR_I32_Rc(ddedpdq, rB, SP)
8478 GEN_DFP_T_FPR_I32_Rc(denbcd, rB, SP)
8479 GEN_DFP_T_FPR_I32_Rc(denbcdq, rB, SP)
8480 GEN_DFP_T_B_Rc(dxex)
8481 GEN_DFP_T_B_Rc(dxexq)
8482 GEN_DFP_T_A_B_Rc(diex)
8483 GEN_DFP_T_A_B_Rc(diexq)
8484 GEN_DFP_T_FPR_I32_Rc(dscli, rA, DCM)
8485 GEN_DFP_T_FPR_I32_Rc(dscliq, rA, DCM)
8486 GEN_DFP_T_FPR_I32_Rc(dscri, rA, DCM)
8487 GEN_DFP_T_FPR_I32_Rc(dscriq, rA, DCM)
8488
8489 /***                           SPE extension                               ***/
8490 /* Register moves */
8491
8492 static inline void gen_evmra(DisasContext *ctx)
8493 {
8494
8495     if (unlikely(!ctx->spe_enabled)) {
8496         gen_exception(ctx, POWERPC_EXCP_SPEU);
8497         return;
8498     }
8499
8500     TCGv_i64 tmp = tcg_temp_new_i64();
8501
8502     /* tmp := rA_lo + rA_hi << 32 */
8503     tcg_gen_concat_tl_i64(tmp, cpu_gpr[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8504
8505     /* spe_acc := tmp */
8506     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8507     tcg_temp_free_i64(tmp);
8508
8509     /* rD := rA */
8510     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8511     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8512 }
8513
8514 static inline void gen_load_gpr64(TCGv_i64 t, int reg)
8515 {
8516     tcg_gen_concat_tl_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
8517 }
8518
8519 static inline void gen_store_gpr64(int reg, TCGv_i64 t)
8520 {
8521     tcg_gen_extr_i64_tl(cpu_gpr[reg], cpu_gprh[reg], t);
8522 }
8523
8524 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type)         \
8525 static void glue(gen_, name0##_##name1)(DisasContext *ctx)                    \
8526 {                                                                             \
8527     if (Rc(ctx->opcode))                                                      \
8528         gen_##name1(ctx);                                                     \
8529     else                                                                      \
8530         gen_##name0(ctx);                                                     \
8531 }
8532
8533 /* Handler for undefined SPE opcodes */
8534 static inline void gen_speundef(DisasContext *ctx)
8535 {
8536     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
8537 }
8538
8539 /* SPE logic */
8540 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
8541 static inline void gen_##name(DisasContext *ctx)                              \
8542 {                                                                             \
8543     if (unlikely(!ctx->spe_enabled)) {                                        \
8544         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8545         return;                                                               \
8546     }                                                                         \
8547     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
8548            cpu_gpr[rB(ctx->opcode)]);                                         \
8549     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
8550            cpu_gprh[rB(ctx->opcode)]);                                        \
8551 }
8552
8553 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
8554 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
8555 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
8556 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
8557 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
8558 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
8559 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
8560 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
8561
8562 /* SPE logic immediate */
8563 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
8564 static inline void gen_##name(DisasContext *ctx)                              \
8565 {                                                                             \
8566     TCGv_i32 t0;                                                              \
8567     if (unlikely(!ctx->spe_enabled)) {                                        \
8568         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8569         return;                                                               \
8570     }                                                                         \
8571     t0 = tcg_temp_new_i32();                                                  \
8572                                                                               \
8573     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8574     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8575     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8576                                                                               \
8577     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8578     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
8579     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8580                                                                               \
8581     tcg_temp_free_i32(t0);                                                    \
8582 }
8583 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
8584 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
8585 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
8586 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
8587
8588 /* SPE arithmetic */
8589 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
8590 static inline void gen_##name(DisasContext *ctx)                              \
8591 {                                                                             \
8592     TCGv_i32 t0;                                                              \
8593     if (unlikely(!ctx->spe_enabled)) {                                        \
8594         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8595         return;                                                               \
8596     }                                                                         \
8597     t0 = tcg_temp_new_i32();                                                  \
8598                                                                               \
8599     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8600     tcg_op(t0, t0);                                                           \
8601     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8602                                                                               \
8603     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8604     tcg_op(t0, t0);                                                           \
8605     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8606                                                                               \
8607     tcg_temp_free_i32(t0);                                                    \
8608 }
8609
8610 static inline void gen_op_evabs(TCGv_i32 ret, TCGv_i32 arg1)
8611 {
8612     TCGLabel *l1 = gen_new_label();
8613     TCGLabel *l2 = gen_new_label();
8614
8615     tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
8616     tcg_gen_neg_i32(ret, arg1);
8617     tcg_gen_br(l2);
8618     gen_set_label(l1);
8619     tcg_gen_mov_i32(ret, arg1);
8620     gen_set_label(l2);
8621 }
8622 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
8623 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
8624 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
8625 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
8626 static inline void gen_op_evrndw(TCGv_i32 ret, TCGv_i32 arg1)
8627 {
8628     tcg_gen_addi_i32(ret, arg1, 0x8000);
8629     tcg_gen_ext16u_i32(ret, ret);
8630 }
8631 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
8632 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
8633 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
8634
8635 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
8636 static inline void gen_##name(DisasContext *ctx)                              \
8637 {                                                                             \
8638     TCGv_i32 t0, t1;                                                          \
8639     if (unlikely(!ctx->spe_enabled)) {                                        \
8640         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8641         return;                                                               \
8642     }                                                                         \
8643     t0 = tcg_temp_new_i32();                                                  \
8644     t1 = tcg_temp_new_i32();                                                  \
8645                                                                               \
8646     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
8647     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
8648     tcg_op(t0, t0, t1);                                                       \
8649     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8650                                                                               \
8651     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rA(ctx->opcode)]);                      \
8652     tcg_gen_trunc_tl_i32(t1, cpu_gprh[rB(ctx->opcode)]);                      \
8653     tcg_op(t0, t0, t1);                                                       \
8654     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8655                                                                               \
8656     tcg_temp_free_i32(t0);                                                    \
8657     tcg_temp_free_i32(t1);                                                    \
8658 }
8659
8660 static inline void gen_op_evsrwu(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8661 {
8662     TCGLabel *l1 = gen_new_label();
8663     TCGLabel *l2 = gen_new_label();
8664     TCGv_i32 t0 = tcg_temp_local_new_i32();
8665
8666     /* No error here: 6 bits are used */
8667     tcg_gen_andi_i32(t0, arg2, 0x3F);
8668     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8669     tcg_gen_shr_i32(ret, arg1, t0);
8670     tcg_gen_br(l2);
8671     gen_set_label(l1);
8672     tcg_gen_movi_i32(ret, 0);
8673     gen_set_label(l2);
8674     tcg_temp_free_i32(t0);
8675 }
8676 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
8677 static inline void gen_op_evsrws(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8678 {
8679     TCGLabel *l1 = gen_new_label();
8680     TCGLabel *l2 = gen_new_label();
8681     TCGv_i32 t0 = tcg_temp_local_new_i32();
8682
8683     /* No error here: 6 bits are used */
8684     tcg_gen_andi_i32(t0, arg2, 0x3F);
8685     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8686     tcg_gen_sar_i32(ret, arg1, t0);
8687     tcg_gen_br(l2);
8688     gen_set_label(l1);
8689     tcg_gen_movi_i32(ret, 0);
8690     gen_set_label(l2);
8691     tcg_temp_free_i32(t0);
8692 }
8693 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
8694 static inline void gen_op_evslw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8695 {
8696     TCGLabel *l1 = gen_new_label();
8697     TCGLabel *l2 = gen_new_label();
8698     TCGv_i32 t0 = tcg_temp_local_new_i32();
8699
8700     /* No error here: 6 bits are used */
8701     tcg_gen_andi_i32(t0, arg2, 0x3F);
8702     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
8703     tcg_gen_shl_i32(ret, arg1, t0);
8704     tcg_gen_br(l2);
8705     gen_set_label(l1);
8706     tcg_gen_movi_i32(ret, 0);
8707     gen_set_label(l2);
8708     tcg_temp_free_i32(t0);
8709 }
8710 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
8711 static inline void gen_op_evrlw(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8712 {
8713     TCGv_i32 t0 = tcg_temp_new_i32();
8714     tcg_gen_andi_i32(t0, arg2, 0x1F);
8715     tcg_gen_rotl_i32(ret, arg1, t0);
8716     tcg_temp_free_i32(t0);
8717 }
8718 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
8719 static inline void gen_evmergehi(DisasContext *ctx)
8720 {
8721     if (unlikely(!ctx->spe_enabled)) {
8722         gen_exception(ctx, POWERPC_EXCP_SPEU);
8723         return;
8724     }
8725     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8726     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8727 }
8728 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
8729 static inline void gen_op_evsubf(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
8730 {
8731     tcg_gen_sub_i32(ret, arg2, arg1);
8732 }
8733 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
8734
8735 /* SPE arithmetic immediate */
8736 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
8737 static inline void gen_##name(DisasContext *ctx)                              \
8738 {                                                                             \
8739     TCGv_i32 t0;                                                              \
8740     if (unlikely(!ctx->spe_enabled)) {                                        \
8741         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8742         return;                                                               \
8743     }                                                                         \
8744     t0 = tcg_temp_new_i32();                                                  \
8745                                                                               \
8746     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
8747     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8748     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
8749                                                                               \
8750     tcg_gen_trunc_tl_i32(t0, cpu_gprh[rB(ctx->opcode)]);                      \
8751     tcg_op(t0, t0, rA(ctx->opcode));                                          \
8752     tcg_gen_extu_i32_tl(cpu_gprh[rD(ctx->opcode)], t0);                       \
8753                                                                               \
8754     tcg_temp_free_i32(t0);                                                    \
8755 }
8756 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
8757 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
8758
8759 /* SPE comparison */
8760 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
8761 static inline void gen_##name(DisasContext *ctx)                              \
8762 {                                                                             \
8763     if (unlikely(!ctx->spe_enabled)) {                                        \
8764         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
8765         return;                                                               \
8766     }                                                                         \
8767     TCGLabel *l1 = gen_new_label();                                           \
8768     TCGLabel *l2 = gen_new_label();                                           \
8769     TCGLabel *l3 = gen_new_label();                                           \
8770     TCGLabel *l4 = gen_new_label();                                           \
8771                                                                               \
8772     tcg_gen_ext32s_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);    \
8773     tcg_gen_ext32s_tl(cpu_gpr[rB(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
8774     tcg_gen_ext32s_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);  \
8775     tcg_gen_ext32s_tl(cpu_gprh[rB(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);  \
8776                                                                               \
8777     tcg_gen_brcond_tl(tcg_cond, cpu_gpr[rA(ctx->opcode)],                     \
8778                        cpu_gpr[rB(ctx->opcode)], l1);                         \
8779     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
8780     tcg_gen_br(l2);                                                           \
8781     gen_set_label(l1);                                                        \
8782     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
8783                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
8784     gen_set_label(l2);                                                        \
8785     tcg_gen_brcond_tl(tcg_cond, cpu_gprh[rA(ctx->opcode)],                    \
8786                        cpu_gprh[rB(ctx->opcode)], l3);                        \
8787     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
8788                      ~(CRF_CH | CRF_CH_AND_CL));                              \
8789     tcg_gen_br(l4);                                                           \
8790     gen_set_label(l3);                                                        \
8791     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
8792                     CRF_CH | CRF_CH_OR_CL);                                   \
8793     gen_set_label(l4);                                                        \
8794 }
8795 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
8796 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
8797 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
8798 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
8799 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
8800
8801 /* SPE misc */
8802 static inline void gen_brinc(DisasContext *ctx)
8803 {
8804     /* Note: brinc is usable even if SPE is disabled */
8805     gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
8806                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8807 }
8808 static inline void gen_evmergelo(DisasContext *ctx)
8809 {
8810     if (unlikely(!ctx->spe_enabled)) {
8811         gen_exception(ctx, POWERPC_EXCP_SPEU);
8812         return;
8813     }
8814     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8815     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8816 }
8817 static inline void gen_evmergehilo(DisasContext *ctx)
8818 {
8819     if (unlikely(!ctx->spe_enabled)) {
8820         gen_exception(ctx, POWERPC_EXCP_SPEU);
8821         return;
8822     }
8823     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8824     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8825 }
8826 static inline void gen_evmergelohi(DisasContext *ctx)
8827 {
8828     if (unlikely(!ctx->spe_enabled)) {
8829         gen_exception(ctx, POWERPC_EXCP_SPEU);
8830         return;
8831     }
8832     if (rD(ctx->opcode) == rA(ctx->opcode)) {
8833         TCGv tmp = tcg_temp_new();
8834         tcg_gen_mov_tl(tmp, cpu_gpr[rA(ctx->opcode)]);
8835         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8836         tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], tmp);
8837         tcg_temp_free(tmp);
8838     } else {
8839         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8840         tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8841     }
8842 }
8843 static inline void gen_evsplati(DisasContext *ctx)
8844 {
8845     uint64_t imm = ((int32_t)(rA(ctx->opcode) << 27)) >> 27;
8846
8847     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], imm);
8848     tcg_gen_movi_tl(cpu_gprh[rD(ctx->opcode)], imm);
8849 }
8850 static inline void gen_evsplatfi(DisasContext *ctx)
8851 {
8852     uint64_t imm = rA(ctx->opcode) << 27;
8853
8854     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], imm);
8855     tcg_gen_movi_tl(cpu_gprh[rD(ctx->opcode)], imm);
8856 }
8857
8858 static inline void gen_evsel(DisasContext *ctx)
8859 {
8860     TCGLabel *l1 = gen_new_label();
8861     TCGLabel *l2 = gen_new_label();
8862     TCGLabel *l3 = gen_new_label();
8863     TCGLabel *l4 = gen_new_label();
8864     TCGv_i32 t0 = tcg_temp_local_new_i32();
8865
8866     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
8867     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
8868     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
8869     tcg_gen_br(l2);
8870     gen_set_label(l1);
8871     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
8872     gen_set_label(l2);
8873     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
8874     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
8875     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
8876     tcg_gen_br(l4);
8877     gen_set_label(l3);
8878     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
8879     gen_set_label(l4);
8880     tcg_temp_free_i32(t0);
8881 }
8882
8883 static void gen_evsel0(DisasContext *ctx)
8884 {
8885     gen_evsel(ctx);
8886 }
8887
8888 static void gen_evsel1(DisasContext *ctx)
8889 {
8890     gen_evsel(ctx);
8891 }
8892
8893 static void gen_evsel2(DisasContext *ctx)
8894 {
8895     gen_evsel(ctx);
8896 }
8897
8898 static void gen_evsel3(DisasContext *ctx)
8899 {
8900     gen_evsel(ctx);
8901 }
8902
8903 /* Multiply */
8904
8905 static inline void gen_evmwumi(DisasContext *ctx)
8906 {
8907     TCGv_i64 t0, t1;
8908
8909     if (unlikely(!ctx->spe_enabled)) {
8910         gen_exception(ctx, POWERPC_EXCP_SPEU);
8911         return;
8912     }
8913
8914     t0 = tcg_temp_new_i64();
8915     t1 = tcg_temp_new_i64();
8916
8917     /* t0 := rA; t1 := rB */
8918     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8919     tcg_gen_ext32u_i64(t0, t0);
8920     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
8921     tcg_gen_ext32u_i64(t1, t1);
8922
8923     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
8924
8925     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
8926
8927     tcg_temp_free_i64(t0);
8928     tcg_temp_free_i64(t1);
8929 }
8930
8931 static inline void gen_evmwumia(DisasContext *ctx)
8932 {
8933     TCGv_i64 tmp;
8934
8935     if (unlikely(!ctx->spe_enabled)) {
8936         gen_exception(ctx, POWERPC_EXCP_SPEU);
8937         return;
8938     }
8939
8940     gen_evmwumi(ctx);            /* rD := rA * rB */
8941
8942     tmp = tcg_temp_new_i64();
8943
8944     /* acc := rD */
8945     gen_load_gpr64(tmp, rD(ctx->opcode));
8946     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
8947     tcg_temp_free_i64(tmp);
8948 }
8949
8950 static inline void gen_evmwumiaa(DisasContext *ctx)
8951 {
8952     TCGv_i64 acc;
8953     TCGv_i64 tmp;
8954
8955     if (unlikely(!ctx->spe_enabled)) {
8956         gen_exception(ctx, POWERPC_EXCP_SPEU);
8957         return;
8958     }
8959
8960     gen_evmwumi(ctx);           /* rD := rA * rB */
8961
8962     acc = tcg_temp_new_i64();
8963     tmp = tcg_temp_new_i64();
8964
8965     /* tmp := rD */
8966     gen_load_gpr64(tmp, rD(ctx->opcode));
8967
8968     /* Load acc */
8969     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8970
8971     /* acc := tmp + acc */
8972     tcg_gen_add_i64(acc, acc, tmp);
8973
8974     /* Store acc */
8975     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
8976
8977     /* rD := acc */
8978     gen_store_gpr64(rD(ctx->opcode), acc);
8979
8980     tcg_temp_free_i64(acc);
8981     tcg_temp_free_i64(tmp);
8982 }
8983
8984 static inline void gen_evmwsmi(DisasContext *ctx)
8985 {
8986     TCGv_i64 t0, t1;
8987
8988     if (unlikely(!ctx->spe_enabled)) {
8989         gen_exception(ctx, POWERPC_EXCP_SPEU);
8990         return;
8991     }
8992
8993     t0 = tcg_temp_new_i64();
8994     t1 = tcg_temp_new_i64();
8995
8996     /* t0 := rA; t1 := rB */
8997     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
8998     tcg_gen_ext32s_i64(t0, t0);
8999     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
9000     tcg_gen_ext32s_i64(t1, t1);
9001
9002     tcg_gen_mul_i64(t0, t0, t1);  /* t0 := rA * rB */
9003
9004     gen_store_gpr64(rD(ctx->opcode), t0); /* rD := t0 */
9005
9006     tcg_temp_free_i64(t0);
9007     tcg_temp_free_i64(t1);
9008 }
9009
9010 static inline void gen_evmwsmia(DisasContext *ctx)
9011 {
9012     TCGv_i64 tmp;
9013
9014     gen_evmwsmi(ctx);            /* rD := rA * rB */
9015
9016     tmp = tcg_temp_new_i64();
9017
9018     /* acc := rD */
9019     gen_load_gpr64(tmp, rD(ctx->opcode));
9020     tcg_gen_st_i64(tmp, cpu_env, offsetof(CPUPPCState, spe_acc));
9021
9022     tcg_temp_free_i64(tmp);
9023 }
9024
9025 static inline void gen_evmwsmiaa(DisasContext *ctx)
9026 {
9027     TCGv_i64 acc = tcg_temp_new_i64();
9028     TCGv_i64 tmp = tcg_temp_new_i64();
9029
9030     gen_evmwsmi(ctx);           /* rD := rA * rB */
9031
9032     acc = tcg_temp_new_i64();
9033     tmp = tcg_temp_new_i64();
9034
9035     /* tmp := rD */
9036     gen_load_gpr64(tmp, rD(ctx->opcode));
9037
9038     /* Load acc */
9039     tcg_gen_ld_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9040
9041     /* acc := tmp + acc */
9042     tcg_gen_add_i64(acc, acc, tmp);
9043
9044     /* Store acc */
9045     tcg_gen_st_i64(acc, cpu_env, offsetof(CPUPPCState, spe_acc));
9046
9047     /* rD := acc */
9048     gen_store_gpr64(rD(ctx->opcode), acc);
9049
9050     tcg_temp_free_i64(acc);
9051     tcg_temp_free_i64(tmp);
9052 }
9053
9054 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9055 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9056 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9057 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9058 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9059 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9060 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE); ////
9061 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE); //
9062 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE);
9063 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9064 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9065 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9066 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9067 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9068 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9069 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9070 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE); ////
9071 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9072 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9073 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE);
9074 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE); ////
9075 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9076 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE); //
9077 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE);
9078 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9079 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE); ////
9080 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9081 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE); ////
9082 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE); ////
9083
9084 /* SPE load and stores */
9085 static inline void gen_addr_spe_imm_index(DisasContext *ctx, TCGv EA, int sh)
9086 {
9087     target_ulong uimm = rB(ctx->opcode);
9088
9089     if (rA(ctx->opcode) == 0) {
9090         tcg_gen_movi_tl(EA, uimm << sh);
9091     } else {
9092         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
9093         if (NARROW_MODE(ctx)) {
9094             tcg_gen_ext32u_tl(EA, EA);
9095         }
9096     }
9097 }
9098
9099 static inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
9100 {
9101     TCGv_i64 t0 = tcg_temp_new_i64();
9102     gen_qemu_ld64(ctx, t0, addr);
9103     gen_store_gpr64(rD(ctx->opcode), t0);
9104     tcg_temp_free_i64(t0);
9105 }
9106
9107 static inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
9108 {
9109     gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9110     gen_addr_add(ctx, addr, addr, 4);
9111     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9112 }
9113
9114 static inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
9115 {
9116     TCGv t0 = tcg_temp_new();
9117     gen_qemu_ld16u(ctx, t0, addr);
9118     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9119     gen_addr_add(ctx, addr, addr, 2);
9120     gen_qemu_ld16u(ctx, t0, addr);
9121     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9122     gen_addr_add(ctx, addr, addr, 2);
9123     gen_qemu_ld16u(ctx, t0, addr);
9124     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9125     gen_addr_add(ctx, addr, addr, 2);
9126     gen_qemu_ld16u(ctx, t0, addr);
9127     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
9128     tcg_temp_free(t0);
9129 }
9130
9131 static inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
9132 {
9133     TCGv t0 = tcg_temp_new();
9134     gen_qemu_ld16u(ctx, t0, addr);
9135     tcg_gen_shli_tl(t0, t0, 16);
9136     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9137     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9138     tcg_temp_free(t0);
9139 }
9140
9141 static inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
9142 {
9143     TCGv t0 = tcg_temp_new();
9144     gen_qemu_ld16u(ctx, t0, addr);
9145     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9146     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9147     tcg_temp_free(t0);
9148 }
9149
9150 static inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
9151 {
9152     TCGv t0 = tcg_temp_new();
9153     gen_qemu_ld16s(ctx, t0, addr);
9154     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9155     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9156     tcg_temp_free(t0);
9157 }
9158
9159 static inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
9160 {
9161     TCGv t0 = tcg_temp_new();
9162     gen_qemu_ld16u(ctx, t0, addr);
9163     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9164     gen_addr_add(ctx, addr, addr, 2);
9165     gen_qemu_ld16u(ctx, t0, addr);
9166     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9167     tcg_temp_free(t0);
9168 }
9169
9170 static inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
9171 {
9172     gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9173     gen_addr_add(ctx, addr, addr, 2);
9174     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9175 }
9176
9177 static inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
9178 {
9179     gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
9180     gen_addr_add(ctx, addr, addr, 2);
9181     gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
9182 }
9183
9184 static inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
9185 {
9186     TCGv t0 = tcg_temp_new();
9187     gen_qemu_ld32u(ctx, t0, addr);
9188     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
9189     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
9190     tcg_temp_free(t0);
9191 }
9192
9193 static inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
9194 {
9195     TCGv t0 = tcg_temp_new();
9196     gen_qemu_ld16u(ctx, t0, addr);
9197     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
9198     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9199     gen_addr_add(ctx, addr, addr, 2);
9200     gen_qemu_ld16u(ctx, t0, addr);
9201     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
9202     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
9203     tcg_temp_free(t0);
9204 }
9205
9206 static inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
9207 {
9208     TCGv_i64 t0 = tcg_temp_new_i64();
9209     gen_load_gpr64(t0, rS(ctx->opcode));
9210     gen_qemu_st64(ctx, t0, addr);
9211     tcg_temp_free_i64(t0);
9212 }
9213
9214 static inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
9215 {
9216     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9217     gen_addr_add(ctx, addr, addr, 4);
9218     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9219 }
9220
9221 static inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
9222 {
9223     TCGv t0 = tcg_temp_new();
9224     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9225     gen_qemu_st16(ctx, t0, addr);
9226     gen_addr_add(ctx, addr, addr, 2);
9227     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9228     gen_addr_add(ctx, addr, addr, 2);
9229     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9230     gen_qemu_st16(ctx, t0, addr);
9231     tcg_temp_free(t0);
9232     gen_addr_add(ctx, addr, addr, 2);
9233     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9234 }
9235
9236 static inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
9237 {
9238     TCGv t0 = tcg_temp_new();
9239     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
9240     gen_qemu_st16(ctx, t0, addr);
9241     gen_addr_add(ctx, addr, addr, 2);
9242     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
9243     gen_qemu_st16(ctx, t0, addr);
9244     tcg_temp_free(t0);
9245 }
9246
9247 static inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
9248 {
9249     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9250     gen_addr_add(ctx, addr, addr, 2);
9251     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9252 }
9253
9254 static inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
9255 {
9256     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
9257 }
9258
9259 static inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
9260 {
9261     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
9262 }
9263
9264 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
9265 static void glue(gen_, name)(DisasContext *ctx)                                       \
9266 {                                                                             \
9267     TCGv t0;                                                                  \
9268     if (unlikely(!ctx->spe_enabled)) {                                        \
9269         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9270         return;                                                               \
9271     }                                                                         \
9272     gen_set_access_type(ctx, ACCESS_INT);                                     \
9273     t0 = tcg_temp_new();                                                      \
9274     if (Rc(ctx->opcode)) {                                                    \
9275         gen_addr_spe_imm_index(ctx, t0, sh);                                  \
9276     } else {                                                                  \
9277         gen_addr_reg_index(ctx, t0);                                          \
9278     }                                                                         \
9279     gen_op_##name(ctx, t0);                                                   \
9280     tcg_temp_free(t0);                                                        \
9281 }
9282
9283 GEN_SPEOP_LDST(evldd, 0x00, 3);
9284 GEN_SPEOP_LDST(evldw, 0x01, 3);
9285 GEN_SPEOP_LDST(evldh, 0x02, 3);
9286 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
9287 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
9288 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
9289 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
9290 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
9291 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
9292 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
9293 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
9294
9295 GEN_SPEOP_LDST(evstdd, 0x10, 3);
9296 GEN_SPEOP_LDST(evstdw, 0x11, 3);
9297 GEN_SPEOP_LDST(evstdh, 0x12, 3);
9298 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
9299 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
9300 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
9301 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
9302
9303 /* Multiply and add - TODO */
9304 #if 0
9305 GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);//
9306 GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9307 GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9308 GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9309 GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9310 GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9311 GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9312 GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9313 GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9314 GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9315 GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, 0x00000000, PPC_SPE);
9316 GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9317
9318 GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9319 GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9320 GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9321 GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9322 GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9323 GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9324 GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9325 GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, 0xFFFFFFFF, PPC_SPE);
9326 GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, 0x00000000, PPC_SPE);
9327 GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9328 GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9329 GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9330
9331 GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9332 GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9333 GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9334 GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, 0x0000F800, PPC_SPE);
9335 GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, 0x00000000, PPC_SPE);
9336
9337 GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9338 GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9339 GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9340 GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9341 GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9342 GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9343 GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9344 GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9345 GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9346 GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9347 GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, 0x00000000, PPC_SPE);
9348 GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9349
9350 GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9351 GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, 0x00000000, PPC_SPE);
9352 GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9353 GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9354
9355 GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9356 GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9357 GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9358 GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9359 GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9360 GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9361 GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9362 GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9363 GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9364 GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9365 GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, 0x00000000, PPC_SPE);
9366 GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9367
9368 GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9369 GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9370 GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9371 GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, 0x00000000, PPC_SPE);
9372 GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0xFFFFFFFF, 0x00000000, PPC_SPE);
9373 #endif
9374
9375 /***                      SPE floating-point extension                     ***/
9376 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
9377 static inline void gen_##name(DisasContext *ctx)                              \
9378 {                                                                             \
9379     TCGv_i32 t0 = tcg_temp_new_i32();                                         \
9380     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
9381     gen_helper_##name(t0, cpu_env, t0);                                       \
9382     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
9383     tcg_temp_free_i32(t0);                                                    \
9384 }
9385 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
9386 static inline void gen_##name(DisasContext *ctx)                              \
9387 {                                                                             \
9388     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9389     TCGv_i32 t1 = tcg_temp_new_i32();                                         \
9390     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9391     gen_helper_##name(t1, cpu_env, t0);                                       \
9392     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);                        \
9393     tcg_temp_free_i64(t0);                                                    \
9394     tcg_temp_free_i32(t1);                                                    \
9395 }
9396 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
9397 static inline void gen_##name(DisasContext *ctx)                              \
9398 {                                                                             \
9399     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9400     TCGv_i32 t1 = tcg_temp_new_i32();                                         \
9401     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9402     gen_helper_##name(t0, cpu_env, t1);                                       \
9403     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9404     tcg_temp_free_i64(t0);                                                    \
9405     tcg_temp_free_i32(t1);                                                    \
9406 }
9407 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
9408 static inline void gen_##name(DisasContext *ctx)                              \
9409 {                                                                             \
9410     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
9411     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
9412     gen_helper_##name(t0, cpu_env, t0);                                       \
9413     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9414     tcg_temp_free_i64(t0);                                                    \
9415 }
9416 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
9417 static inline void gen_##name(DisasContext *ctx)                              \
9418 {                                                                             \
9419     TCGv_i32 t0, t1;                                                          \
9420     if (unlikely(!ctx->spe_enabled)) {                                        \
9421         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9422         return;                                                               \
9423     }                                                                         \
9424     t0 = tcg_temp_new_i32();                                                  \
9425     t1 = tcg_temp_new_i32();                                                  \
9426     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9427     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9428     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9429     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);                        \
9430                                                                               \
9431     tcg_temp_free_i32(t0);                                                    \
9432     tcg_temp_free_i32(t1);                                                    \
9433 }
9434 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
9435 static inline void gen_##name(DisasContext *ctx)                              \
9436 {                                                                             \
9437     TCGv_i64 t0, t1;                                                          \
9438     if (unlikely(!ctx->spe_enabled)) {                                        \
9439         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9440         return;                                                               \
9441     }                                                                         \
9442     t0 = tcg_temp_new_i64();                                                  \
9443     t1 = tcg_temp_new_i64();                                                  \
9444     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9445     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9446     gen_helper_##name(t0, cpu_env, t0, t1);                                   \
9447     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
9448     tcg_temp_free_i64(t0);                                                    \
9449     tcg_temp_free_i64(t1);                                                    \
9450 }
9451 #define GEN_SPEFPUOP_COMP_32(name)                                            \
9452 static inline void gen_##name(DisasContext *ctx)                              \
9453 {                                                                             \
9454     TCGv_i32 t0, t1;                                                          \
9455     if (unlikely(!ctx->spe_enabled)) {                                        \
9456         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9457         return;                                                               \
9458     }                                                                         \
9459     t0 = tcg_temp_new_i32();                                                  \
9460     t1 = tcg_temp_new_i32();                                                  \
9461                                                                               \
9462     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
9463     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
9464     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9465                                                                               \
9466     tcg_temp_free_i32(t0);                                                    \
9467     tcg_temp_free_i32(t1);                                                    \
9468 }
9469 #define GEN_SPEFPUOP_COMP_64(name)                                            \
9470 static inline void gen_##name(DisasContext *ctx)                              \
9471 {                                                                             \
9472     TCGv_i64 t0, t1;                                                          \
9473     if (unlikely(!ctx->spe_enabled)) {                                        \
9474         gen_exception(ctx, POWERPC_EXCP_SPEU);                                \
9475         return;                                                               \
9476     }                                                                         \
9477     t0 = tcg_temp_new_i64();                                                  \
9478     t1 = tcg_temp_new_i64();                                                  \
9479     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
9480     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
9481     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], cpu_env, t0, t1);           \
9482     tcg_temp_free_i64(t0);                                                    \
9483     tcg_temp_free_i64(t1);                                                    \
9484 }
9485
9486 /* Single precision floating-point vectors operations */
9487 /* Arithmetic */
9488 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
9489 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
9490 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
9491 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
9492 static inline void gen_evfsabs(DisasContext *ctx)
9493 {
9494     if (unlikely(!ctx->spe_enabled)) {
9495         gen_exception(ctx, POWERPC_EXCP_SPEU);
9496         return;
9497     }
9498     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9499                     ~0x80000000);
9500     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9501                     ~0x80000000);
9502 }
9503 static inline void gen_evfsnabs(DisasContext *ctx)
9504 {
9505     if (unlikely(!ctx->spe_enabled)) {
9506         gen_exception(ctx, POWERPC_EXCP_SPEU);
9507         return;
9508     }
9509     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9510                    0x80000000);
9511     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9512                    0x80000000);
9513 }
9514 static inline void gen_evfsneg(DisasContext *ctx)
9515 {
9516     if (unlikely(!ctx->spe_enabled)) {
9517         gen_exception(ctx, POWERPC_EXCP_SPEU);
9518         return;
9519     }
9520     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
9521                     0x80000000);
9522     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9523                     0x80000000);
9524 }
9525
9526 /* Conversion */
9527 GEN_SPEFPUOP_CONV_64_64(evfscfui);
9528 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
9529 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
9530 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
9531 GEN_SPEFPUOP_CONV_64_64(evfsctui);
9532 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
9533 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
9534 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
9535 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
9536 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
9537
9538 /* Comparison */
9539 GEN_SPEFPUOP_COMP_64(evfscmpgt);
9540 GEN_SPEFPUOP_COMP_64(evfscmplt);
9541 GEN_SPEFPUOP_COMP_64(evfscmpeq);
9542 GEN_SPEFPUOP_COMP_64(evfststgt);
9543 GEN_SPEFPUOP_COMP_64(evfststlt);
9544 GEN_SPEFPUOP_COMP_64(evfststeq);
9545
9546 /* Opcodes definitions */
9547 GEN_SPE(evfsadd,   evfssub,   0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9548 GEN_SPE(evfsabs,   evfsnabs,  0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9549 GEN_SPE(evfsneg,   speundef,  0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9550 GEN_SPE(evfsmul,   evfsdiv,   0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9551 GEN_SPE(evfscmpgt, evfscmplt, 0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9552 GEN_SPE(evfscmpeq, speundef,  0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9553 GEN_SPE(evfscfui,  evfscfsi,  0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9554 GEN_SPE(evfscfuf,  evfscfsf,  0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9555 GEN_SPE(evfsctui,  evfsctsi,  0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9556 GEN_SPE(evfsctuf,  evfsctsf,  0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9557 GEN_SPE(evfsctuiz, speundef,  0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9558 GEN_SPE(evfsctsiz, speundef,  0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9559 GEN_SPE(evfststgt, evfststlt, 0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9560 GEN_SPE(evfststeq, speundef,  0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9561
9562 /* Single precision floating-point operations */
9563 /* Arithmetic */
9564 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
9565 GEN_SPEFPUOP_ARITH2_32_32(efssub);
9566 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
9567 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
9568 static inline void gen_efsabs(DisasContext *ctx)
9569 {
9570     if (unlikely(!ctx->spe_enabled)) {
9571         gen_exception(ctx, POWERPC_EXCP_SPEU);
9572         return;
9573     }
9574     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
9575 }
9576 static inline void gen_efsnabs(DisasContext *ctx)
9577 {
9578     if (unlikely(!ctx->spe_enabled)) {
9579         gen_exception(ctx, POWERPC_EXCP_SPEU);
9580         return;
9581     }
9582     tcg_gen_ori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9583 }
9584 static inline void gen_efsneg(DisasContext *ctx)
9585 {
9586     if (unlikely(!ctx->spe_enabled)) {
9587         gen_exception(ctx, POWERPC_EXCP_SPEU);
9588         return;
9589     }
9590     tcg_gen_xori_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
9591 }
9592
9593 /* Conversion */
9594 GEN_SPEFPUOP_CONV_32_32(efscfui);
9595 GEN_SPEFPUOP_CONV_32_32(efscfsi);
9596 GEN_SPEFPUOP_CONV_32_32(efscfuf);
9597 GEN_SPEFPUOP_CONV_32_32(efscfsf);
9598 GEN_SPEFPUOP_CONV_32_32(efsctui);
9599 GEN_SPEFPUOP_CONV_32_32(efsctsi);
9600 GEN_SPEFPUOP_CONV_32_32(efsctuf);
9601 GEN_SPEFPUOP_CONV_32_32(efsctsf);
9602 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
9603 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
9604 GEN_SPEFPUOP_CONV_32_64(efscfd);
9605
9606 /* Comparison */
9607 GEN_SPEFPUOP_COMP_32(efscmpgt);
9608 GEN_SPEFPUOP_COMP_32(efscmplt);
9609 GEN_SPEFPUOP_COMP_32(efscmpeq);
9610 GEN_SPEFPUOP_COMP_32(efststgt);
9611 GEN_SPEFPUOP_COMP_32(efststlt);
9612 GEN_SPEFPUOP_COMP_32(efststeq);
9613
9614 /* Opcodes definitions */
9615 GEN_SPE(efsadd,   efssub,   0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9616 GEN_SPE(efsabs,   efsnabs,  0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE); //
9617 GEN_SPE(efsneg,   speundef, 0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9618 GEN_SPE(efsmul,   efsdiv,   0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE); //
9619 GEN_SPE(efscmpgt, efscmplt, 0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9620 GEN_SPE(efscmpeq, efscfd,   0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE); //
9621 GEN_SPE(efscfui,  efscfsi,  0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9622 GEN_SPE(efscfuf,  efscfsf,  0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9623 GEN_SPE(efsctui,  efsctsi,  0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9624 GEN_SPE(efsctuf,  efsctsf,  0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE); //
9625 GEN_SPE(efsctuiz, speundef, 0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9626 GEN_SPE(efsctsiz, speundef, 0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9627 GEN_SPE(efststgt, efststlt, 0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE); //
9628 GEN_SPE(efststeq, speundef, 0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE); //
9629
9630 /* Double precision floating-point operations */
9631 /* Arithmetic */
9632 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
9633 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
9634 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
9635 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
9636 static inline void gen_efdabs(DisasContext *ctx)
9637 {
9638     if (unlikely(!ctx->spe_enabled)) {
9639         gen_exception(ctx, POWERPC_EXCP_SPEU);
9640         return;
9641     }
9642     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9643     tcg_gen_andi_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9644                     ~0x80000000);
9645 }
9646 static inline void gen_efdnabs(DisasContext *ctx)
9647 {
9648     if (unlikely(!ctx->spe_enabled)) {
9649         gen_exception(ctx, POWERPC_EXCP_SPEU);
9650         return;
9651     }
9652     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9653     tcg_gen_ori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9654                    0x80000000);
9655 }
9656 static inline void gen_efdneg(DisasContext *ctx)
9657 {
9658     if (unlikely(!ctx->spe_enabled)) {
9659         gen_exception(ctx, POWERPC_EXCP_SPEU);
9660         return;
9661     }
9662     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
9663     tcg_gen_xori_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],
9664                     0x80000000);
9665 }
9666
9667 /* Conversion */
9668 GEN_SPEFPUOP_CONV_64_32(efdcfui);
9669 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
9670 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
9671 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
9672 GEN_SPEFPUOP_CONV_32_64(efdctui);
9673 GEN_SPEFPUOP_CONV_32_64(efdctsi);
9674 GEN_SPEFPUOP_CONV_32_64(efdctuf);
9675 GEN_SPEFPUOP_CONV_32_64(efdctsf);
9676 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
9677 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
9678 GEN_SPEFPUOP_CONV_64_32(efdcfs);
9679 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
9680 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
9681 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
9682 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
9683
9684 /* Comparison */
9685 GEN_SPEFPUOP_COMP_64(efdcmpgt);
9686 GEN_SPEFPUOP_COMP_64(efdcmplt);
9687 GEN_SPEFPUOP_COMP_64(efdcmpeq);
9688 GEN_SPEFPUOP_COMP_64(efdtstgt);
9689 GEN_SPEFPUOP_COMP_64(efdtstlt);
9690 GEN_SPEFPUOP_COMP_64(efdtsteq);
9691
9692 /* Opcodes definitions */
9693 GEN_SPE(efdadd,    efdsub,    0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9694 GEN_SPE(efdcfuid,  efdcfsid,  0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9695 GEN_SPE(efdabs,    efdnabs,   0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE); //
9696 GEN_SPE(efdneg,    speundef,  0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9697 GEN_SPE(efdmul,    efddiv,    0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE); //
9698 GEN_SPE(efdctuidz, efdctsidz, 0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9699 GEN_SPE(efdcmpgt,  efdcmplt,  0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9700 GEN_SPE(efdcmpeq,  efdcfs,    0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE); //
9701 GEN_SPE(efdcfui,   efdcfsi,   0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9702 GEN_SPE(efdcfuf,   efdcfsf,   0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9703 GEN_SPE(efdctui,   efdctsi,   0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9704 GEN_SPE(efdctuf,   efdctsf,   0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE); //
9705 GEN_SPE(efdctuiz,  speundef,  0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9706 GEN_SPE(efdctsiz,  speundef,  0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9707 GEN_SPE(efdtstgt,  efdtstlt,  0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE); //
9708 GEN_SPE(efdtsteq,  speundef,  0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE); //
9709
9710 static void gen_tbegin(DisasContext *ctx)
9711 {
9712     if (unlikely(!ctx->tm_enabled)) {
9713         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
9714         return;
9715     }
9716     gen_helper_tbegin(cpu_env);
9717 }
9718
9719 #define GEN_TM_NOOP(name)                                      \
9720 static inline void gen_##name(DisasContext *ctx)               \
9721 {                                                              \
9722     if (unlikely(!ctx->tm_enabled)) {                          \
9723         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
9724         return;                                                \
9725     }                                                          \
9726     /* Because tbegin always fails in QEMU, these user         \
9727      * space instructions all have a simple implementation:    \
9728      *                                                         \
9729      *     CR[0] = 0b0 || MSR[TS] || 0b0                       \
9730      *           = 0b0 || 0b00    || 0b0                       \
9731      */                                                        \
9732     tcg_gen_movi_i32(cpu_crf[0], 0);                           \
9733 }
9734
9735 GEN_TM_NOOP(tend);
9736 GEN_TM_NOOP(tabort);
9737 GEN_TM_NOOP(tabortwc);
9738 GEN_TM_NOOP(tabortwci);
9739 GEN_TM_NOOP(tabortdc);
9740 GEN_TM_NOOP(tabortdci);
9741 GEN_TM_NOOP(tsr);
9742
9743 static void gen_tcheck(DisasContext *ctx)
9744 {
9745     if (unlikely(!ctx->tm_enabled)) {
9746         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
9747         return;
9748     }
9749     /* Because tbegin always fails, the tcheck implementation
9750      * is simple:
9751      *
9752      * CR[CRF] = TDOOMED || MSR[TS] || 0b0
9753      *         = 0b1 || 0b00 || 0b0
9754      */
9755     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0x8);
9756 }
9757
9758 #if defined(CONFIG_USER_ONLY)
9759 #define GEN_TM_PRIV_NOOP(name)                                 \
9760 static inline void gen_##name(DisasContext *ctx)               \
9761 {                                                              \
9762     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);           \
9763 }
9764
9765 #else
9766
9767 #define GEN_TM_PRIV_NOOP(name)                                 \
9768 static inline void gen_##name(DisasContext *ctx)               \
9769 {                                                              \
9770     if (unlikely(ctx->pr)) {                                   \
9771         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);       \
9772         return;                                                \
9773     }                                                          \
9774     if (unlikely(!ctx->tm_enabled)) {                          \
9775         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
9776         return;                                                \
9777     }                                                          \
9778     /* Because tbegin always fails, the implementation is      \
9779      * simple:                                                 \
9780      *                                                         \
9781      *   CR[0] = 0b0 || MSR[TS] || 0b0                         \
9782      *         = 0b0 || 0b00 | 0b0                             \
9783      */                                                        \
9784     tcg_gen_movi_i32(cpu_crf[0], 0);                           \
9785 }
9786
9787 #endif
9788
9789 GEN_TM_PRIV_NOOP(treclaim);
9790 GEN_TM_PRIV_NOOP(trechkpt);
9791
9792 static opcode_t opcodes[] = {
9793 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
9794 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
9795 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9796 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER),
9797 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
9798 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
9799 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
9800 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9801 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9802 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9803 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9804 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
9805 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
9806 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
9807 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
9808 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9809 #if defined(TARGET_PPC64)
9810 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
9811 #endif
9812 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
9813 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
9814 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9815 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9816 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9817 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
9818 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
9819 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
9820 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9821 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9822 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9823 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9824 GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
9825 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
9826 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
9827 #if defined(TARGET_PPC64)
9828 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
9829 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
9830 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
9831 GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
9832 #endif
9833 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9834 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9835 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9836 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
9837 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
9838 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
9839 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
9840 #if defined(TARGET_PPC64)
9841 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
9842 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
9843 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
9844 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
9845 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
9846 #endif
9847 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES),
9848 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9849 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT),
9850 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT),
9851 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT),
9852 GEN_HANDLER(fabs, 0x3F, 0x08, 0x08, 0x001F0000, PPC_FLOAT),
9853 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT),
9854 GEN_HANDLER(fnabs, 0x3F, 0x08, 0x04, 0x001F0000, PPC_FLOAT),
9855 GEN_HANDLER(fneg, 0x3F, 0x08, 0x01, 0x001F0000, PPC_FLOAT),
9856 GEN_HANDLER_E(fcpsgn, 0x3F, 0x08, 0x00, 0x00000000, PPC_NONE, PPC2_ISA205),
9857 GEN_HANDLER_E(fmrgew, 0x3F, 0x06, 0x1E, 0x00000001, PPC_NONE, PPC2_VSX207),
9858 GEN_HANDLER_E(fmrgow, 0x3F, 0x06, 0x1A, 0x00000001, PPC_NONE, PPC2_VSX207),
9859 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT),
9860 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT),
9861 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT),
9862 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT),
9863 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x00000000, PPC_FLOAT),
9864 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006e0800, PPC_FLOAT),
9865 #if defined(TARGET_PPC64)
9866 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
9867 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
9868 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
9869 #endif
9870 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9871 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
9872 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
9873 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
9874 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
9875 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
9876 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO),
9877 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
9878 GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9879 GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9880 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
9881 GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9882 GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
9883 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
9884 #if defined(TARGET_PPC64)
9885 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
9886 GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
9887 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
9888 GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
9889 #endif
9890 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
9891 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
9892 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9893 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9894 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
9895 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
9896 GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0, PPC_NONE, PPC2_BCTAR_ISA207),
9897 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
9898 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
9899 #if defined(TARGET_PPC64)
9900 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
9901 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
9902 #endif
9903 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
9904 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
9905 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
9906 #if defined(TARGET_PPC64)
9907 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
9908 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
9909 #endif
9910 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
9911 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
9912 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
9913 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
9914 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
9915 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
9916 #if defined(TARGET_PPC64)
9917 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
9918 #endif
9919 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC),
9920 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000000, PPC_MISC),
9921 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
9922 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
9923 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
9924 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
9925 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
9926 GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
9927 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
9928 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
9929 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x02000001, PPC_ALTIVEC),
9930 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
9931 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
9932 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
9933 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
9934 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
9935 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
9936 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
9937 #if defined(TARGET_PPC64)
9938 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
9939 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
9940              PPC_SEGMENT_64B),
9941 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
9942 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
9943              PPC_SEGMENT_64B),
9944 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
9945 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
9946 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
9947 #endif
9948 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
9949 GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x03FF0001, PPC_MEM_TLBIE),
9950 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE),
9951 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
9952 #if defined(TARGET_PPC64)
9953 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI),
9954 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
9955 #endif
9956 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
9957 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
9958 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
9959 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
9960 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
9961 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
9962 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
9963 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
9964 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
9965 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
9966 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
9967 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9968 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
9969 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
9970 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
9971 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
9972 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
9973 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
9974 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
9975 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
9976 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
9977 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
9978 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
9979 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
9980 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
9981 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
9982 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
9983 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
9984 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
9985 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
9986 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
9987 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
9988 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
9989 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
9990 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
9991 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
9992 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
9993 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
9994 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
9995 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
9996 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
9997 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
9998 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
9999 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
10000 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
10001 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
10002 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
10003 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
10004 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
10005 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10006 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10007 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
10008 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
10009 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10010 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
10011 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
10012 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
10013 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
10014 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
10015 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
10016 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
10017 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
10018 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
10019 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
10020 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
10021 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
10022 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
10023 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
10024 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
10025 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
10026 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
10027 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
10028 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
10029 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
10030 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
10031 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
10032 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
10033 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
10034 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
10035 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
10036 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
10037                PPC_NONE, PPC2_BOOKE206),
10038 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
10039                PPC_NONE, PPC2_BOOKE206),
10040 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
10041                PPC_NONE, PPC2_BOOKE206),
10042 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
10043                PPC_NONE, PPC2_BOOKE206),
10044 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
10045                PPC_NONE, PPC2_BOOKE206),
10046 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
10047                PPC_NONE, PPC2_PRCNTL),
10048 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
10049                PPC_NONE, PPC2_PRCNTL),
10050 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
10051 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
10052 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
10053 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
10054               PPC_BOOKE, PPC2_BOOKE206),
10055 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE),
10056 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
10057                PPC_BOOKE, PPC2_BOOKE206),
10058 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
10059 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
10060 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
10061 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
10062 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
10063 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE),
10064 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE),
10065 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE),
10066 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE),
10067
10068 #undef GEN_INT_ARITH_ADD
10069 #undef GEN_INT_ARITH_ADD_CONST
10070 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
10071 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
10072 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
10073                                 add_ca, compute_ca, compute_ov)               \
10074 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
10075 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
10076 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
10077 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
10078 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
10079 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
10080 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
10081 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
10082 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
10083 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
10084 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
10085
10086 #undef GEN_INT_ARITH_DIVW
10087 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
10088 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
10089 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
10090 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
10091 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
10092 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
10093 GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10094 GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10095 GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10096 GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10097
10098 #if defined(TARGET_PPC64)
10099 #undef GEN_INT_ARITH_DIVD
10100 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
10101 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
10102 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
10103 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
10104 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
10105 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
10106
10107 GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10108 GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
10109 GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10110 GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
10111
10112 #undef GEN_INT_ARITH_MUL_HELPER
10113 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
10114 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
10115 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
10116 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
10117 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
10118 #endif
10119
10120 #undef GEN_INT_ARITH_SUBF
10121 #undef GEN_INT_ARITH_SUBF_CONST
10122 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
10123 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
10124 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
10125                                 add_ca, compute_ca, compute_ov)               \
10126 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
10127 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
10128 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
10129 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
10130 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
10131 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
10132 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
10133 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
10134 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
10135 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
10136 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
10137
10138 #undef GEN_LOGICAL1
10139 #undef GEN_LOGICAL2
10140 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
10141 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
10142 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
10143 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
10144 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
10145 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
10146 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
10147 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
10148 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
10149 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
10150 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
10151 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
10152 #if defined(TARGET_PPC64)
10153 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
10154 #endif
10155
10156 #if defined(TARGET_PPC64)
10157 #undef GEN_PPC64_R2
10158 #undef GEN_PPC64_R4
10159 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
10160 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10161 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10162              PPC_64B)
10163 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
10164 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
10165 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
10166              PPC_64B),                                                        \
10167 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
10168              PPC_64B),                                                        \
10169 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
10170              PPC_64B)
10171 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
10172 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
10173 GEN_PPC64_R4(rldic, 0x1E, 0x04),
10174 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
10175 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
10176 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
10177 #endif
10178
10179 #undef _GEN_FLOAT_ACB
10180 #undef GEN_FLOAT_ACB
10181 #undef _GEN_FLOAT_AB
10182 #undef GEN_FLOAT_AB
10183 #undef _GEN_FLOAT_AC
10184 #undef GEN_FLOAT_AC
10185 #undef GEN_FLOAT_B
10186 #undef GEN_FLOAT_BS
10187 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
10188 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)
10189 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
10190 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type),                     \
10191 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type)
10192 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10193 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10194 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
10195 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10196 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10197 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
10198 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)
10199 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
10200 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type),               \
10201 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type)
10202 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
10203 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)
10204 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
10205 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)
10206
10207 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT),
10208 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT),
10209 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT),
10210 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT),
10211 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES),
10212 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE),
10213 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL),
10214 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT),
10215 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT),
10216 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT),
10217 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT),
10218 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT),
10219 GEN_HANDLER_E(ftdiv, 0x3F, 0x00, 0x04, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10220 GEN_HANDLER_E(ftsqrt, 0x3F, 0x00, 0x05, 1, PPC_NONE, PPC2_FP_TST_ISA206),
10221 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT),
10222 GEN_HANDLER_E(fctiwu, 0x3F, 0x0E, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10223 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT),
10224 GEN_HANDLER_E(fctiwuz, 0x3F, 0x0F, 0x04, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10225 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT),
10226 GEN_HANDLER_E(fcfid, 0x3F, 0x0E, 0x1A, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10227 GEN_HANDLER_E(fcfids, 0x3B, 0x0E, 0x1A, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10228 GEN_HANDLER_E(fcfidu, 0x3F, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10229 GEN_HANDLER_E(fcfidus, 0x3B, 0x0E, 0x1E, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10230 GEN_HANDLER_E(fctid, 0x3F, 0x0E, 0x19, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10231 GEN_HANDLER_E(fctidu, 0x3F, 0x0E, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10232 GEN_HANDLER_E(fctidz, 0x3F, 0x0F, 0x19, 0x001F0000, PPC_NONE, PPC2_FP_CVT_S64),
10233 GEN_HANDLER_E(fctiduz, 0x3F, 0x0F, 0x1D, 0, PPC_NONE, PPC2_FP_CVT_ISA206),
10234 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT),
10235 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT),
10236 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT),
10237 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT),
10238
10239 #undef GEN_LD
10240 #undef GEN_LDU
10241 #undef GEN_LDUX
10242 #undef GEN_LDX_E
10243 #undef GEN_LDS
10244 #define GEN_LD(name, ldop, opc, type)                                         \
10245 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10246 #define GEN_LDU(name, ldop, opc, type)                                        \
10247 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10248 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
10249 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10250 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2)                        \
10251 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10252 #define GEN_LDS(name, ldop, op, type)                                         \
10253 GEN_LD(name, ldop, op | 0x20, type)                                           \
10254 GEN_LDU(name, ldop, op | 0x21, type)                                          \
10255 GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
10256 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
10257
10258 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
10259 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
10260 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
10261 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
10262 #if defined(TARGET_PPC64)
10263 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
10264 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
10265 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B)
10266 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B)
10267 GEN_LDX_E(ldbr, ld64ur, 0x14, 0x10, PPC_NONE, PPC2_DBRX)
10268 #endif
10269 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
10270 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
10271
10272 #undef GEN_ST
10273 #undef GEN_STU
10274 #undef GEN_STUX
10275 #undef GEN_STX_E
10276 #undef GEN_STS
10277 #define GEN_ST(name, stop, opc, type)                                         \
10278 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10279 #define GEN_STU(name, stop, opc, type)                                        \
10280 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
10281 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
10282 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
10283 #define GEN_STX_E(name, stop, opc2, opc3, type, type2)                        \
10284 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
10285 #define GEN_STS(name, stop, op, type)                                         \
10286 GEN_ST(name, stop, op | 0x20, type)                                           \
10287 GEN_STU(name, stop, op | 0x21, type)                                          \
10288 GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
10289 GEN_STX(name, stop, 0x17, op | 0x00, type)
10290
10291 GEN_STS(stb, st8, 0x06, PPC_INTEGER)
10292 GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
10293 GEN_STS(stw, st32, 0x04, PPC_INTEGER)
10294 #if defined(TARGET_PPC64)
10295 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B)
10296 GEN_STX(std, st64, 0x15, 0x04, PPC_64B)
10297 GEN_STX_E(stdbr, st64r, 0x14, 0x14, PPC_NONE, PPC2_DBRX)
10298 #endif
10299 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
10300 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
10301
10302 #undef GEN_LDF
10303 #undef GEN_LDUF
10304 #undef GEN_LDUXF
10305 #undef GEN_LDXF
10306 #undef GEN_LDFS
10307 #define GEN_LDF(name, ldop, opc, type)                                        \
10308 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10309 #define GEN_LDUF(name, ldop, opc, type)                                       \
10310 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10311 #define GEN_LDUXF(name, ldop, opc, type)                                      \
10312 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10313 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
10314 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10315 #define GEN_LDFS(name, ldop, op, type)                                        \
10316 GEN_LDF(name, ldop, op | 0x20, type)                                          \
10317 GEN_LDUF(name, ldop, op | 0x21, type)                                         \
10318 GEN_LDUXF(name, ldop, op | 0x01, type)                                        \
10319 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
10320
10321 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT)
10322 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT)
10323 GEN_HANDLER_E(lfiwax, 0x1f, 0x17, 0x1a, 0x00000001, PPC_NONE, PPC2_ISA205),
10324 GEN_HANDLER_E(lfiwzx, 0x1f, 0x17, 0x1b, 0x1, PPC_NONE, PPC2_FP_CVT_ISA206),
10325 GEN_HANDLER_E(lfdp, 0x39, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10326 GEN_HANDLER_E(lfdpx, 0x1F, 0x17, 0x18, 0x00200001, PPC_NONE, PPC2_ISA205),
10327
10328 #undef GEN_STF
10329 #undef GEN_STUF
10330 #undef GEN_STUXF
10331 #undef GEN_STXF
10332 #undef GEN_STFS
10333 #define GEN_STF(name, stop, opc, type)                                        \
10334 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
10335 #define GEN_STUF(name, stop, opc, type)                                       \
10336 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
10337 #define GEN_STUXF(name, stop, opc, type)                                      \
10338 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type),
10339 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
10340 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type),
10341 #define GEN_STFS(name, stop, op, type)                                        \
10342 GEN_STF(name, stop, op | 0x20, type)                                          \
10343 GEN_STUF(name, stop, op | 0x21, type)                                         \
10344 GEN_STUXF(name, stop, op | 0x01, type)                                        \
10345 GEN_STXF(name, stop, 0x17, op | 0x00, type)
10346
10347 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT)
10348 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT)
10349 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX)
10350 GEN_HANDLER_E(stfdp, 0x3D, 0xFF, 0xFF, 0x00200003, PPC_NONE, PPC2_ISA205),
10351 GEN_HANDLER_E(stfdpx, 0x1F, 0x17, 0x1C, 0x00200001, PPC_NONE, PPC2_ISA205),
10352
10353 #undef GEN_CRLOGIC
10354 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
10355 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
10356 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
10357 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
10358 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
10359 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
10360 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
10361 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
10362 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
10363 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
10364
10365 #undef GEN_MAC_HANDLER
10366 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
10367 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
10368 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
10369 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
10370 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
10371 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
10372 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
10373 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
10374 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
10375 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
10376 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
10377 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
10378 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
10379 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
10380 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
10381 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
10382 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
10383 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
10384 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
10385 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
10386 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
10387 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
10388 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
10389 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
10390 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
10391 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
10392 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
10393 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
10394 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
10395 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
10396 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
10397 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
10398 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
10399 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
10400 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
10401 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
10402 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
10403 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
10404 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
10405 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
10406 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
10407 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
10408 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
10409 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
10410
10411 #undef GEN_VR_LDX
10412 #undef GEN_VR_STX
10413 #undef GEN_VR_LVE
10414 #undef GEN_VR_STVE
10415 #define GEN_VR_LDX(name, opc2, opc3)                                          \
10416 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10417 #define GEN_VR_STX(name, opc2, opc3)                                          \
10418 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10419 #define GEN_VR_LVE(name, opc2, opc3)                                    \
10420     GEN_HANDLER(lve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10421 #define GEN_VR_STVE(name, opc2, opc3)                                   \
10422     GEN_HANDLER(stve##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)
10423 GEN_VR_LDX(lvx, 0x07, 0x03),
10424 GEN_VR_LDX(lvxl, 0x07, 0x0B),
10425 GEN_VR_LVE(bx, 0x07, 0x00),
10426 GEN_VR_LVE(hx, 0x07, 0x01),
10427 GEN_VR_LVE(wx, 0x07, 0x02),
10428 GEN_VR_STX(svx, 0x07, 0x07),
10429 GEN_VR_STX(svxl, 0x07, 0x0F),
10430 GEN_VR_STVE(bx, 0x07, 0x04),
10431 GEN_VR_STVE(hx, 0x07, 0x05),
10432 GEN_VR_STVE(wx, 0x07, 0x06),
10433
10434 #undef GEN_VX_LOGICAL
10435 #define GEN_VX_LOGICAL(name, tcg_op, opc2, opc3)                        \
10436 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10437
10438 #undef GEN_VX_LOGICAL_207
10439 #define GEN_VX_LOGICAL_207(name, tcg_op, opc2, opc3) \
10440 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10441
10442 GEN_VX_LOGICAL(vand, tcg_gen_and_i64, 2, 16),
10443 GEN_VX_LOGICAL(vandc, tcg_gen_andc_i64, 2, 17),
10444 GEN_VX_LOGICAL(vor, tcg_gen_or_i64, 2, 18),
10445 GEN_VX_LOGICAL(vxor, tcg_gen_xor_i64, 2, 19),
10446 GEN_VX_LOGICAL(vnor, tcg_gen_nor_i64, 2, 20),
10447 GEN_VX_LOGICAL_207(veqv, tcg_gen_eqv_i64, 2, 26),
10448 GEN_VX_LOGICAL_207(vnand, tcg_gen_nand_i64, 2, 22),
10449 GEN_VX_LOGICAL_207(vorc, tcg_gen_orc_i64, 2, 21),
10450
10451 #undef GEN_VXFORM
10452 #define GEN_VXFORM(name, opc2, opc3)                                    \
10453 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10454
10455 #undef GEN_VXFORM_207
10456 #define GEN_VXFORM_207(name, opc2, opc3) \
10457 GEN_HANDLER_E(name, 0x04, opc2, opc3, 0x00000000, PPC_NONE, PPC2_ALTIVEC_207)
10458
10459 #undef GEN_VXFORM_DUAL
10460 #define GEN_VXFORM_DUAL(name0, name1, opc2, opc3, type0, type1) \
10461 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, type0, type1)
10462
10463 #undef GEN_VXRFORM_DUAL
10464 #define GEN_VXRFORM_DUAL(name0, name1, opc2, opc3, tp0, tp1) \
10465 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, opc3, 0x00000000, tp0, tp1), \
10466 GEN_HANDLER_E(name0##_##name1, 0x4, opc2, (opc3 | 0x10), 0x00000000, tp0, tp1),
10467
10468 GEN_VXFORM(vaddubm, 0, 0),
10469 GEN_VXFORM(vadduhm, 0, 1),
10470 GEN_VXFORM(vadduwm, 0, 2),
10471 GEN_VXFORM_207(vaddudm, 0, 3),
10472 GEN_VXFORM_DUAL(vsububm, bcdadd, 0, 16, PPC_ALTIVEC, PPC_NONE),
10473 GEN_VXFORM_DUAL(vsubuhm, bcdsub, 0, 17, PPC_ALTIVEC, PPC_NONE),
10474 GEN_VXFORM(vsubuwm, 0, 18),
10475 GEN_VXFORM_207(vsubudm, 0, 19),
10476 GEN_VXFORM(vmaxub, 1, 0),
10477 GEN_VXFORM(vmaxuh, 1, 1),
10478 GEN_VXFORM(vmaxuw, 1, 2),
10479 GEN_VXFORM_207(vmaxud, 1, 3),
10480 GEN_VXFORM(vmaxsb, 1, 4),
10481 GEN_VXFORM(vmaxsh, 1, 5),
10482 GEN_VXFORM(vmaxsw, 1, 6),
10483 GEN_VXFORM_207(vmaxsd, 1, 7),
10484 GEN_VXFORM(vminub, 1, 8),
10485 GEN_VXFORM(vminuh, 1, 9),
10486 GEN_VXFORM(vminuw, 1, 10),
10487 GEN_VXFORM_207(vminud, 1, 11),
10488 GEN_VXFORM(vminsb, 1, 12),
10489 GEN_VXFORM(vminsh, 1, 13),
10490 GEN_VXFORM(vminsw, 1, 14),
10491 GEN_VXFORM_207(vminsd, 1, 15),
10492 GEN_VXFORM(vavgub, 1, 16),
10493 GEN_VXFORM(vavguh, 1, 17),
10494 GEN_VXFORM(vavguw, 1, 18),
10495 GEN_VXFORM(vavgsb, 1, 20),
10496 GEN_VXFORM(vavgsh, 1, 21),
10497 GEN_VXFORM(vavgsw, 1, 22),
10498 GEN_VXFORM(vmrghb, 6, 0),
10499 GEN_VXFORM(vmrghh, 6, 1),
10500 GEN_VXFORM(vmrghw, 6, 2),
10501 GEN_VXFORM(vmrglb, 6, 4),
10502 GEN_VXFORM(vmrglh, 6, 5),
10503 GEN_VXFORM(vmrglw, 6, 6),
10504 GEN_VXFORM_207(vmrgew, 6, 30),
10505 GEN_VXFORM_207(vmrgow, 6, 26),
10506 GEN_VXFORM(vmuloub, 4, 0),
10507 GEN_VXFORM(vmulouh, 4, 1),
10508 GEN_VXFORM_DUAL(vmulouw, vmuluwm, 4, 2, PPC_ALTIVEC, PPC_NONE),
10509 GEN_VXFORM(vmulosb, 4, 4),
10510 GEN_VXFORM(vmulosh, 4, 5),
10511 GEN_VXFORM_207(vmulosw, 4, 6),
10512 GEN_VXFORM(vmuleub, 4, 8),
10513 GEN_VXFORM(vmuleuh, 4, 9),
10514 GEN_VXFORM_207(vmuleuw, 4, 10),
10515 GEN_VXFORM(vmulesb, 4, 12),
10516 GEN_VXFORM(vmulesh, 4, 13),
10517 GEN_VXFORM_207(vmulesw, 4, 14),
10518 GEN_VXFORM(vslb, 2, 4),
10519 GEN_VXFORM(vslh, 2, 5),
10520 GEN_VXFORM(vslw, 2, 6),
10521 GEN_VXFORM_207(vsld, 2, 23),
10522 GEN_VXFORM(vsrb, 2, 8),
10523 GEN_VXFORM(vsrh, 2, 9),
10524 GEN_VXFORM(vsrw, 2, 10),
10525 GEN_VXFORM_207(vsrd, 2, 27),
10526 GEN_VXFORM(vsrab, 2, 12),
10527 GEN_VXFORM(vsrah, 2, 13),
10528 GEN_VXFORM(vsraw, 2, 14),
10529 GEN_VXFORM_207(vsrad, 2, 15),
10530 GEN_VXFORM(vslo, 6, 16),
10531 GEN_VXFORM(vsro, 6, 17),
10532 GEN_VXFORM(vaddcuw, 0, 6),
10533 GEN_VXFORM(vsubcuw, 0, 22),
10534 GEN_VXFORM(vaddubs, 0, 8),
10535 GEN_VXFORM(vadduhs, 0, 9),
10536 GEN_VXFORM(vadduws, 0, 10),
10537 GEN_VXFORM(vaddsbs, 0, 12),
10538 GEN_VXFORM(vaddshs, 0, 13),
10539 GEN_VXFORM(vaddsws, 0, 14),
10540 GEN_VXFORM_DUAL(vsububs, bcdadd, 0, 24, PPC_ALTIVEC, PPC_NONE),
10541 GEN_VXFORM_DUAL(vsubuhs, bcdsub, 0, 25, PPC_ALTIVEC, PPC_NONE),
10542 GEN_VXFORM(vsubuws, 0, 26),
10543 GEN_VXFORM(vsubsbs, 0, 28),
10544 GEN_VXFORM(vsubshs, 0, 29),
10545 GEN_VXFORM(vsubsws, 0, 30),
10546 GEN_VXFORM_207(vadduqm, 0, 4),
10547 GEN_VXFORM_207(vaddcuq, 0, 5),
10548 GEN_VXFORM_DUAL(vaddeuqm, vaddecuq, 30, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10549 GEN_VXFORM_207(vsubuqm, 0, 20),
10550 GEN_VXFORM_207(vsubcuq, 0, 21),
10551 GEN_VXFORM_DUAL(vsubeuqm, vsubecuq, 31, 0xFF, PPC_NONE, PPC2_ALTIVEC_207),
10552 GEN_VXFORM(vrlb, 2, 0),
10553 GEN_VXFORM(vrlh, 2, 1),
10554 GEN_VXFORM(vrlw, 2, 2),
10555 GEN_VXFORM_207(vrld, 2, 3),
10556 GEN_VXFORM(vsl, 2, 7),
10557 GEN_VXFORM(vsr, 2, 11),
10558 GEN_VXFORM(vpkuhum, 7, 0),
10559 GEN_VXFORM(vpkuwum, 7, 1),
10560 GEN_VXFORM_207(vpkudum, 7, 17),
10561 GEN_VXFORM(vpkuhus, 7, 2),
10562 GEN_VXFORM(vpkuwus, 7, 3),
10563 GEN_VXFORM_207(vpkudus, 7, 19),
10564 GEN_VXFORM(vpkshus, 7, 4),
10565 GEN_VXFORM(vpkswus, 7, 5),
10566 GEN_VXFORM_207(vpksdus, 7, 21),
10567 GEN_VXFORM(vpkshss, 7, 6),
10568 GEN_VXFORM(vpkswss, 7, 7),
10569 GEN_VXFORM_207(vpksdss, 7, 23),
10570 GEN_VXFORM(vpkpx, 7, 12),
10571 GEN_VXFORM(vsum4ubs, 4, 24),
10572 GEN_VXFORM(vsum4sbs, 4, 28),
10573 GEN_VXFORM(vsum4shs, 4, 25),
10574 GEN_VXFORM(vsum2sws, 4, 26),
10575 GEN_VXFORM(vsumsws, 4, 30),
10576 GEN_VXFORM(vaddfp, 5, 0),
10577 GEN_VXFORM(vsubfp, 5, 1),
10578 GEN_VXFORM(vmaxfp, 5, 16),
10579 GEN_VXFORM(vminfp, 5, 17),
10580
10581 #undef GEN_VXRFORM1
10582 #undef GEN_VXRFORM
10583 #define GEN_VXRFORM1(opname, name, str, opc2, opc3)                     \
10584     GEN_HANDLER2(name, str, 0x4, opc2, opc3, 0x00000000, PPC_ALTIVEC),
10585 #define GEN_VXRFORM(name, opc2, opc3)                                \
10586     GEN_VXRFORM1(name, name, #name, opc2, opc3)                      \
10587     GEN_VXRFORM1(name##_dot, name##_, #name ".", opc2, (opc3 | (0x1 << 4)))
10588 GEN_VXRFORM(vcmpequb, 3, 0)
10589 GEN_VXRFORM(vcmpequh, 3, 1)
10590 GEN_VXRFORM(vcmpequw, 3, 2)
10591 GEN_VXRFORM(vcmpgtsb, 3, 12)
10592 GEN_VXRFORM(vcmpgtsh, 3, 13)
10593 GEN_VXRFORM(vcmpgtsw, 3, 14)
10594 GEN_VXRFORM(vcmpgtub, 3, 8)
10595 GEN_VXRFORM(vcmpgtuh, 3, 9)
10596 GEN_VXRFORM(vcmpgtuw, 3, 10)
10597 GEN_VXRFORM_DUAL(vcmpeqfp, vcmpequd, 3, 3, PPC_ALTIVEC, PPC_NONE)
10598 GEN_VXRFORM(vcmpgefp, 3, 7)
10599 GEN_VXRFORM_DUAL(vcmpgtfp, vcmpgtud, 3, 11, PPC_ALTIVEC, PPC_NONE)
10600 GEN_VXRFORM_DUAL(vcmpbfp, vcmpgtsd, 3, 15, PPC_ALTIVEC, PPC_NONE)
10601
10602 #undef GEN_VXFORM_SIMM
10603 #define GEN_VXFORM_SIMM(name, opc2, opc3)                               \
10604     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10605 GEN_VXFORM_SIMM(vspltisb, 6, 12),
10606 GEN_VXFORM_SIMM(vspltish, 6, 13),
10607 GEN_VXFORM_SIMM(vspltisw, 6, 14),
10608
10609 #undef GEN_VXFORM_NOA
10610 #define GEN_VXFORM_NOA(name, opc2, opc3)                                \
10611     GEN_HANDLER(name, 0x04, opc2, opc3, 0x001f0000, PPC_ALTIVEC)
10612 GEN_VXFORM_NOA(vupkhsb, 7, 8),
10613 GEN_VXFORM_NOA(vupkhsh, 7, 9),
10614 GEN_VXFORM_207(vupkhsw, 7, 25),
10615 GEN_VXFORM_NOA(vupklsb, 7, 10),
10616 GEN_VXFORM_NOA(vupklsh, 7, 11),
10617 GEN_VXFORM_207(vupklsw, 7, 27),
10618 GEN_VXFORM_NOA(vupkhpx, 7, 13),
10619 GEN_VXFORM_NOA(vupklpx, 7, 15),
10620 GEN_VXFORM_NOA(vrefp, 5, 4),
10621 GEN_VXFORM_NOA(vrsqrtefp, 5, 5),
10622 GEN_VXFORM_NOA(vexptefp, 5, 6),
10623 GEN_VXFORM_NOA(vlogefp, 5, 7),
10624 GEN_VXFORM_NOA(vrfim, 5, 11),
10625 GEN_VXFORM_NOA(vrfin, 5, 8),
10626 GEN_VXFORM_NOA(vrfip, 5, 10),
10627 GEN_VXFORM_NOA(vrfiz, 5, 9),
10628
10629 #undef GEN_VXFORM_UIMM
10630 #define GEN_VXFORM_UIMM(name, opc2, opc3)                               \
10631     GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_ALTIVEC)
10632 GEN_VXFORM_UIMM(vspltb, 6, 8),
10633 GEN_VXFORM_UIMM(vsplth, 6, 9),
10634 GEN_VXFORM_UIMM(vspltw, 6, 10),
10635 GEN_VXFORM_UIMM(vcfux, 5, 12),
10636 GEN_VXFORM_UIMM(vcfsx, 5, 13),
10637 GEN_VXFORM_UIMM(vctuxs, 5, 14),
10638 GEN_VXFORM_UIMM(vctsxs, 5, 15),
10639
10640 #undef GEN_VAFORM_PAIRED
10641 #define GEN_VAFORM_PAIRED(name0, name1, opc2)                           \
10642     GEN_HANDLER(name0##_##name1, 0x04, opc2, 0xFF, 0x00000000, PPC_ALTIVEC)
10643 GEN_VAFORM_PAIRED(vmhaddshs, vmhraddshs, 16),
10644 GEN_VAFORM_PAIRED(vmsumubm, vmsummbm, 18),
10645 GEN_VAFORM_PAIRED(vmsumuhm, vmsumuhs, 19),
10646 GEN_VAFORM_PAIRED(vmsumshm, vmsumshs, 20),
10647 GEN_VAFORM_PAIRED(vsel, vperm, 21),
10648 GEN_VAFORM_PAIRED(vmaddfp, vnmsubfp, 23),
10649
10650 GEN_VXFORM_DUAL(vclzb, vpopcntb, 1, 28, PPC_NONE, PPC2_ALTIVEC_207),
10651 GEN_VXFORM_DUAL(vclzh, vpopcnth, 1, 29, PPC_NONE, PPC2_ALTIVEC_207),
10652 GEN_VXFORM_DUAL(vclzw, vpopcntw, 1, 30, PPC_NONE, PPC2_ALTIVEC_207),
10653 GEN_VXFORM_DUAL(vclzd, vpopcntd, 1, 31, PPC_NONE, PPC2_ALTIVEC_207),
10654
10655 GEN_VXFORM_207(vbpermq, 6, 21),
10656 GEN_VXFORM_207(vgbbd, 6, 20),
10657 GEN_VXFORM_207(vpmsumb, 4, 16),
10658 GEN_VXFORM_207(vpmsumh, 4, 17),
10659 GEN_VXFORM_207(vpmsumw, 4, 18),
10660 GEN_VXFORM_207(vpmsumd, 4, 19),
10661
10662 GEN_VXFORM_207(vsbox, 4, 23),
10663
10664 GEN_VXFORM_DUAL(vcipher, vcipherlast, 4, 20, PPC_NONE, PPC2_ALTIVEC_207),
10665 GEN_VXFORM_DUAL(vncipher, vncipherlast, 4, 21, PPC_NONE, PPC2_ALTIVEC_207),
10666
10667 GEN_VXFORM_207(vshasigmaw, 1, 26),
10668 GEN_VXFORM_207(vshasigmad, 1, 27),
10669
10670 GEN_VXFORM_DUAL(vsldoi, vpermxor, 22, 0xFF, PPC_ALTIVEC, PPC_NONE),
10671
10672 GEN_HANDLER_E(lxsdx, 0x1F, 0x0C, 0x12, 0, PPC_NONE, PPC2_VSX),
10673 GEN_HANDLER_E(lxsiwax, 0x1F, 0x0C, 0x02, 0, PPC_NONE, PPC2_VSX207),
10674 GEN_HANDLER_E(lxsiwzx, 0x1F, 0x0C, 0x00, 0, PPC_NONE, PPC2_VSX207),
10675 GEN_HANDLER_E(lxsspx, 0x1F, 0x0C, 0x10, 0, PPC_NONE, PPC2_VSX207),
10676 GEN_HANDLER_E(lxvd2x, 0x1F, 0x0C, 0x1A, 0, PPC_NONE, PPC2_VSX),
10677 GEN_HANDLER_E(lxvdsx, 0x1F, 0x0C, 0x0A, 0, PPC_NONE, PPC2_VSX),
10678 GEN_HANDLER_E(lxvw4x, 0x1F, 0x0C, 0x18, 0, PPC_NONE, PPC2_VSX),
10679
10680 GEN_HANDLER_E(stxsdx, 0x1F, 0xC, 0x16, 0, PPC_NONE, PPC2_VSX),
10681 GEN_HANDLER_E(stxsiwx, 0x1F, 0xC, 0x04, 0, PPC_NONE, PPC2_VSX207),
10682 GEN_HANDLER_E(stxsspx, 0x1F, 0xC, 0x14, 0, PPC_NONE, PPC2_VSX207),
10683 GEN_HANDLER_E(stxvd2x, 0x1F, 0xC, 0x1E, 0, PPC_NONE, PPC2_VSX),
10684 GEN_HANDLER_E(stxvw4x, 0x1F, 0xC, 0x1C, 0, PPC_NONE, PPC2_VSX),
10685
10686 GEN_HANDLER_E(mfvsrwz, 0x1F, 0x13, 0x03, 0x0000F800, PPC_NONE, PPC2_VSX207),
10687 GEN_HANDLER_E(mtvsrwa, 0x1F, 0x13, 0x06, 0x0000F800, PPC_NONE, PPC2_VSX207),
10688 GEN_HANDLER_E(mtvsrwz, 0x1F, 0x13, 0x07, 0x0000F800, PPC_NONE, PPC2_VSX207),
10689 #if defined(TARGET_PPC64)
10690 GEN_HANDLER_E(mfvsrd, 0x1F, 0x13, 0x01, 0x0000F800, PPC_NONE, PPC2_VSX207),
10691 GEN_HANDLER_E(mtvsrd, 0x1F, 0x13, 0x05, 0x0000F800, PPC_NONE, PPC2_VSX207),
10692 #endif
10693
10694 #undef GEN_XX2FORM
10695 #define GEN_XX2FORM(name, opc2, opc3, fl2)                           \
10696 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10697 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2)
10698
10699 #undef GEN_XX3FORM
10700 #define GEN_XX3FORM(name, opc2, opc3, fl2)                           \
10701 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 0, PPC_NONE, fl2), \
10702 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 0, PPC_NONE, fl2), \
10703 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 0, PPC_NONE, fl2), \
10704 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 0, PPC_NONE, fl2)
10705
10706 #undef GEN_XX2IFORM
10707 #define GEN_XX2IFORM(name, opc2, opc3, fl2)                           \
10708 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0, opc3, 1, PPC_NONE, fl2), \
10709 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 1, opc3, 1, PPC_NONE, fl2), \
10710 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 2, opc3, 1, PPC_NONE, fl2), \
10711 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 3, opc3, 1, PPC_NONE, fl2)
10712
10713 #undef GEN_XX3_RC_FORM
10714 #define GEN_XX3_RC_FORM(name, opc2, opc3, fl2)                          \
10715 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x00, 0, PPC_NONE, fl2), \
10716 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x00, 0, PPC_NONE, fl2), \
10717 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x00, 0, PPC_NONE, fl2), \
10718 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x00, 0, PPC_NONE, fl2), \
10719 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x00, opc3 | 0x10, 0, PPC_NONE, fl2), \
10720 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x01, opc3 | 0x10, 0, PPC_NONE, fl2), \
10721 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x02, opc3 | 0x10, 0, PPC_NONE, fl2), \
10722 GEN_HANDLER2_E(name, #name, 0x3C, opc2 | 0x03, opc3 | 0x10, 0, PPC_NONE, fl2)
10723
10724 #undef GEN_XX3FORM_DM
10725 #define GEN_XX3FORM_DM(name, opc2, opc3) \
10726 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10727 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10728 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10729 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x00, 0, PPC_NONE, PPC2_VSX),\
10730 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10731 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10732 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10733 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x04, 0, PPC_NONE, PPC2_VSX),\
10734 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10735 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10736 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10737 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x08, 0, PPC_NONE, PPC2_VSX),\
10738 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x00, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10739 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x01, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10740 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x02, opc3|0x0C, 0, PPC_NONE, PPC2_VSX),\
10741 GEN_HANDLER2_E(name, #name, 0x3C, opc2|0x03, opc3|0x0C, 0, PPC_NONE, PPC2_VSX)
10742
10743 GEN_XX2FORM(xsabsdp, 0x12, 0x15, PPC2_VSX),
10744 GEN_XX2FORM(xsnabsdp, 0x12, 0x16, PPC2_VSX),
10745 GEN_XX2FORM(xsnegdp, 0x12, 0x17, PPC2_VSX),
10746 GEN_XX3FORM(xscpsgndp, 0x00, 0x16, PPC2_VSX),
10747
10748 GEN_XX2FORM(xvabsdp, 0x12, 0x1D, PPC2_VSX),
10749 GEN_XX2FORM(xvnabsdp, 0x12, 0x1E, PPC2_VSX),
10750 GEN_XX2FORM(xvnegdp, 0x12, 0x1F, PPC2_VSX),
10751 GEN_XX3FORM(xvcpsgndp, 0x00, 0x1E, PPC2_VSX),
10752 GEN_XX2FORM(xvabssp, 0x12, 0x19, PPC2_VSX),
10753 GEN_XX2FORM(xvnabssp, 0x12, 0x1A, PPC2_VSX),
10754 GEN_XX2FORM(xvnegsp, 0x12, 0x1B, PPC2_VSX),
10755 GEN_XX3FORM(xvcpsgnsp, 0x00, 0x1A, PPC2_VSX),
10756
10757 GEN_XX3FORM(xsadddp, 0x00, 0x04, PPC2_VSX),
10758 GEN_XX3FORM(xssubdp, 0x00, 0x05, PPC2_VSX),
10759 GEN_XX3FORM(xsmuldp, 0x00, 0x06, PPC2_VSX),
10760 GEN_XX3FORM(xsdivdp, 0x00, 0x07, PPC2_VSX),
10761 GEN_XX2FORM(xsredp,  0x14, 0x05, PPC2_VSX),
10762 GEN_XX2FORM(xssqrtdp,  0x16, 0x04, PPC2_VSX),
10763 GEN_XX2FORM(xsrsqrtedp,  0x14, 0x04, PPC2_VSX),
10764 GEN_XX3FORM(xstdivdp,  0x14, 0x07, PPC2_VSX),
10765 GEN_XX2FORM(xstsqrtdp,  0x14, 0x06, PPC2_VSX),
10766 GEN_XX3FORM(xsmaddadp, 0x04, 0x04, PPC2_VSX),
10767 GEN_XX3FORM(xsmaddmdp, 0x04, 0x05, PPC2_VSX),
10768 GEN_XX3FORM(xsmsubadp, 0x04, 0x06, PPC2_VSX),
10769 GEN_XX3FORM(xsmsubmdp, 0x04, 0x07, PPC2_VSX),
10770 GEN_XX3FORM(xsnmaddadp, 0x04, 0x14, PPC2_VSX),
10771 GEN_XX3FORM(xsnmaddmdp, 0x04, 0x15, PPC2_VSX),
10772 GEN_XX3FORM(xsnmsubadp, 0x04, 0x16, PPC2_VSX),
10773 GEN_XX3FORM(xsnmsubmdp, 0x04, 0x17, PPC2_VSX),
10774 GEN_XX2IFORM(xscmpodp,  0x0C, 0x05, PPC2_VSX),
10775 GEN_XX2IFORM(xscmpudp,  0x0C, 0x04, PPC2_VSX),
10776 GEN_XX3FORM(xsmaxdp, 0x00, 0x14, PPC2_VSX),
10777 GEN_XX3FORM(xsmindp, 0x00, 0x15, PPC2_VSX),
10778 GEN_XX2FORM(xscvdpsp, 0x12, 0x10, PPC2_VSX),
10779 GEN_XX2FORM(xscvdpspn, 0x16, 0x10, PPC2_VSX207),
10780 GEN_XX2FORM(xscvspdp, 0x12, 0x14, PPC2_VSX),
10781 GEN_XX2FORM(xscvspdpn, 0x16, 0x14, PPC2_VSX207),
10782 GEN_XX2FORM(xscvdpsxds, 0x10, 0x15, PPC2_VSX),
10783 GEN_XX2FORM(xscvdpsxws, 0x10, 0x05, PPC2_VSX),
10784 GEN_XX2FORM(xscvdpuxds, 0x10, 0x14, PPC2_VSX),
10785 GEN_XX2FORM(xscvdpuxws, 0x10, 0x04, PPC2_VSX),
10786 GEN_XX2FORM(xscvsxddp, 0x10, 0x17, PPC2_VSX),
10787 GEN_XX2FORM(xscvuxddp, 0x10, 0x16, PPC2_VSX),
10788 GEN_XX2FORM(xsrdpi, 0x12, 0x04, PPC2_VSX),
10789 GEN_XX2FORM(xsrdpic, 0x16, 0x06, PPC2_VSX),
10790 GEN_XX2FORM(xsrdpim, 0x12, 0x07, PPC2_VSX),
10791 GEN_XX2FORM(xsrdpip, 0x12, 0x06, PPC2_VSX),
10792 GEN_XX2FORM(xsrdpiz, 0x12, 0x05, PPC2_VSX),
10793
10794 GEN_XX3FORM(xsaddsp, 0x00, 0x00, PPC2_VSX207),
10795 GEN_XX3FORM(xssubsp, 0x00, 0x01, PPC2_VSX207),
10796 GEN_XX3FORM(xsmulsp, 0x00, 0x02, PPC2_VSX207),
10797 GEN_XX3FORM(xsdivsp, 0x00, 0x03, PPC2_VSX207),
10798 GEN_XX2FORM(xsresp,  0x14, 0x01, PPC2_VSX207),
10799 GEN_XX2FORM(xsrsp, 0x12, 0x11, PPC2_VSX207),
10800 GEN_XX2FORM(xssqrtsp,  0x16, 0x00, PPC2_VSX207),
10801 GEN_XX2FORM(xsrsqrtesp,  0x14, 0x00, PPC2_VSX207),
10802 GEN_XX3FORM(xsmaddasp, 0x04, 0x00, PPC2_VSX207),
10803 GEN_XX3FORM(xsmaddmsp, 0x04, 0x01, PPC2_VSX207),
10804 GEN_XX3FORM(xsmsubasp, 0x04, 0x02, PPC2_VSX207),
10805 GEN_XX3FORM(xsmsubmsp, 0x04, 0x03, PPC2_VSX207),
10806 GEN_XX3FORM(xsnmaddasp, 0x04, 0x10, PPC2_VSX207),
10807 GEN_XX3FORM(xsnmaddmsp, 0x04, 0x11, PPC2_VSX207),
10808 GEN_XX3FORM(xsnmsubasp, 0x04, 0x12, PPC2_VSX207),
10809 GEN_XX3FORM(xsnmsubmsp, 0x04, 0x13, PPC2_VSX207),
10810 GEN_XX2FORM(xscvsxdsp, 0x10, 0x13, PPC2_VSX207),
10811 GEN_XX2FORM(xscvuxdsp, 0x10, 0x12, PPC2_VSX207),
10812
10813 GEN_XX3FORM(xvadddp, 0x00, 0x0C, PPC2_VSX),
10814 GEN_XX3FORM(xvsubdp, 0x00, 0x0D, PPC2_VSX),
10815 GEN_XX3FORM(xvmuldp, 0x00, 0x0E, PPC2_VSX),
10816 GEN_XX3FORM(xvdivdp, 0x00, 0x0F, PPC2_VSX),
10817 GEN_XX2FORM(xvredp,  0x14, 0x0D, PPC2_VSX),
10818 GEN_XX2FORM(xvsqrtdp,  0x16, 0x0C, PPC2_VSX),
10819 GEN_XX2FORM(xvrsqrtedp,  0x14, 0x0C, PPC2_VSX),
10820 GEN_XX3FORM(xvtdivdp, 0x14, 0x0F, PPC2_VSX),
10821 GEN_XX2FORM(xvtsqrtdp, 0x14, 0x0E, PPC2_VSX),
10822 GEN_XX3FORM(xvmaddadp, 0x04, 0x0C, PPC2_VSX),
10823 GEN_XX3FORM(xvmaddmdp, 0x04, 0x0D, PPC2_VSX),
10824 GEN_XX3FORM(xvmsubadp, 0x04, 0x0E, PPC2_VSX),
10825 GEN_XX3FORM(xvmsubmdp, 0x04, 0x0F, PPC2_VSX),
10826 GEN_XX3FORM(xvnmaddadp, 0x04, 0x1C, PPC2_VSX),
10827 GEN_XX3FORM(xvnmaddmdp, 0x04, 0x1D, PPC2_VSX),
10828 GEN_XX3FORM(xvnmsubadp, 0x04, 0x1E, PPC2_VSX),
10829 GEN_XX3FORM(xvnmsubmdp, 0x04, 0x1F, PPC2_VSX),
10830 GEN_XX3FORM(xvmaxdp, 0x00, 0x1C, PPC2_VSX),
10831 GEN_XX3FORM(xvmindp, 0x00, 0x1D, PPC2_VSX),
10832 GEN_XX3_RC_FORM(xvcmpeqdp, 0x0C, 0x0C, PPC2_VSX),
10833 GEN_XX3_RC_FORM(xvcmpgtdp, 0x0C, 0x0D, PPC2_VSX),
10834 GEN_XX3_RC_FORM(xvcmpgedp, 0x0C, 0x0E, PPC2_VSX),
10835 GEN_XX2FORM(xvcvdpsp, 0x12, 0x18, PPC2_VSX),
10836 GEN_XX2FORM(xvcvdpsxds, 0x10, 0x1D, PPC2_VSX),
10837 GEN_XX2FORM(xvcvdpsxws, 0x10, 0x0D, PPC2_VSX),
10838 GEN_XX2FORM(xvcvdpuxds, 0x10, 0x1C, PPC2_VSX),
10839 GEN_XX2FORM(xvcvdpuxws, 0x10, 0x0C, PPC2_VSX),
10840 GEN_XX2FORM(xvcvsxddp, 0x10, 0x1F, PPC2_VSX),
10841 GEN_XX2FORM(xvcvuxddp, 0x10, 0x1E, PPC2_VSX),
10842 GEN_XX2FORM(xvcvsxwdp, 0x10, 0x0F, PPC2_VSX),
10843 GEN_XX2FORM(xvcvuxwdp, 0x10, 0x0E, PPC2_VSX),
10844 GEN_XX2FORM(xvrdpi, 0x12, 0x0C, PPC2_VSX),
10845 GEN_XX2FORM(xvrdpic, 0x16, 0x0E, PPC2_VSX),
10846 GEN_XX2FORM(xvrdpim, 0x12, 0x0F, PPC2_VSX),
10847 GEN_XX2FORM(xvrdpip, 0x12, 0x0E, PPC2_VSX),
10848 GEN_XX2FORM(xvrdpiz, 0x12, 0x0D, PPC2_VSX),
10849
10850 GEN_XX3FORM(xvaddsp, 0x00, 0x08, PPC2_VSX),
10851 GEN_XX3FORM(xvsubsp, 0x00, 0x09, PPC2_VSX),
10852 GEN_XX3FORM(xvmulsp, 0x00, 0x0A, PPC2_VSX),
10853 GEN_XX3FORM(xvdivsp, 0x00, 0x0B, PPC2_VSX),
10854 GEN_XX2FORM(xvresp, 0x14, 0x09, PPC2_VSX),
10855 GEN_XX2FORM(xvsqrtsp, 0x16, 0x08, PPC2_VSX),
10856 GEN_XX2FORM(xvrsqrtesp, 0x14, 0x08, PPC2_VSX),
10857 GEN_XX3FORM(xvtdivsp, 0x14, 0x0B, PPC2_VSX),
10858 GEN_XX2FORM(xvtsqrtsp, 0x14, 0x0A, PPC2_VSX),
10859 GEN_XX3FORM(xvmaddasp, 0x04, 0x08, PPC2_VSX),
10860 GEN_XX3FORM(xvmaddmsp, 0x04, 0x09, PPC2_VSX),
10861 GEN_XX3FORM(xvmsubasp, 0x04, 0x0A, PPC2_VSX),
10862 GEN_XX3FORM(xvmsubmsp, 0x04, 0x0B, PPC2_VSX),
10863 GEN_XX3FORM(xvnmaddasp, 0x04, 0x18, PPC2_VSX),
10864 GEN_XX3FORM(xvnmaddmsp, 0x04, 0x19, PPC2_VSX),
10865 GEN_XX3FORM(xvnmsubasp, 0x04, 0x1A, PPC2_VSX),
10866 GEN_XX3FORM(xvnmsubmsp, 0x04, 0x1B, PPC2_VSX),
10867 GEN_XX3FORM(xvmaxsp, 0x00, 0x18, PPC2_VSX),
10868 GEN_XX3FORM(xvminsp, 0x00, 0x19, PPC2_VSX),
10869 GEN_XX3_RC_FORM(xvcmpeqsp, 0x0C, 0x08, PPC2_VSX),
10870 GEN_XX3_RC_FORM(xvcmpgtsp, 0x0C, 0x09, PPC2_VSX),
10871 GEN_XX3_RC_FORM(xvcmpgesp, 0x0C, 0x0A, PPC2_VSX),
10872 GEN_XX2FORM(xvcvspdp, 0x12, 0x1C, PPC2_VSX),
10873 GEN_XX2FORM(xvcvspsxds, 0x10, 0x19, PPC2_VSX),
10874 GEN_XX2FORM(xvcvspsxws, 0x10, 0x09, PPC2_VSX),
10875 GEN_XX2FORM(xvcvspuxds, 0x10, 0x18, PPC2_VSX),
10876 GEN_XX2FORM(xvcvspuxws, 0x10, 0x08, PPC2_VSX),
10877 GEN_XX2FORM(xvcvsxdsp, 0x10, 0x1B, PPC2_VSX),
10878 GEN_XX2FORM(xvcvuxdsp, 0x10, 0x1A, PPC2_VSX),
10879 GEN_XX2FORM(xvcvsxwsp, 0x10, 0x0B, PPC2_VSX),
10880 GEN_XX2FORM(xvcvuxwsp, 0x10, 0x0A, PPC2_VSX),
10881 GEN_XX2FORM(xvrspi, 0x12, 0x08, PPC2_VSX),
10882 GEN_XX2FORM(xvrspic, 0x16, 0x0A, PPC2_VSX),
10883 GEN_XX2FORM(xvrspim, 0x12, 0x0B, PPC2_VSX),
10884 GEN_XX2FORM(xvrspip, 0x12, 0x0A, PPC2_VSX),
10885 GEN_XX2FORM(xvrspiz, 0x12, 0x09, PPC2_VSX),
10886
10887 #undef VSX_LOGICAL
10888 #define VSX_LOGICAL(name, opc2, opc3, fl2) \
10889 GEN_XX3FORM(name, opc2, opc3, fl2)
10890
10891 VSX_LOGICAL(xxland, 0x8, 0x10, PPC2_VSX),
10892 VSX_LOGICAL(xxlandc, 0x8, 0x11, PPC2_VSX),
10893 VSX_LOGICAL(xxlor, 0x8, 0x12, PPC2_VSX),
10894 VSX_LOGICAL(xxlxor, 0x8, 0x13, PPC2_VSX),
10895 VSX_LOGICAL(xxlnor, 0x8, 0x14, PPC2_VSX),
10896 VSX_LOGICAL(xxleqv, 0x8, 0x17, PPC2_VSX207),
10897 VSX_LOGICAL(xxlnand, 0x8, 0x16, PPC2_VSX207),
10898 VSX_LOGICAL(xxlorc, 0x8, 0x15, PPC2_VSX207),
10899 GEN_XX3FORM(xxmrghw, 0x08, 0x02, PPC2_VSX),
10900 GEN_XX3FORM(xxmrglw, 0x08, 0x06, PPC2_VSX),
10901 GEN_XX2FORM(xxspltw, 0x08, 0x0A, PPC2_VSX),
10902 GEN_XX3FORM_DM(xxsldwi, 0x08, 0x00),
10903
10904 #define GEN_XXSEL_ROW(opc3) \
10905 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x18, opc3, 0, PPC_NONE, PPC2_VSX), \
10906 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x19, opc3, 0, PPC_NONE, PPC2_VSX), \
10907 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1A, opc3, 0, PPC_NONE, PPC2_VSX), \
10908 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1B, opc3, 0, PPC_NONE, PPC2_VSX), \
10909 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1C, opc3, 0, PPC_NONE, PPC2_VSX), \
10910 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1D, opc3, 0, PPC_NONE, PPC2_VSX), \
10911 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1E, opc3, 0, PPC_NONE, PPC2_VSX), \
10912 GEN_HANDLER2_E(xxsel, "xxsel", 0x3C, 0x1F, opc3, 0, PPC_NONE, PPC2_VSX), \
10913
10914 GEN_XXSEL_ROW(0x00)
10915 GEN_XXSEL_ROW(0x01)
10916 GEN_XXSEL_ROW(0x02)
10917 GEN_XXSEL_ROW(0x03)
10918 GEN_XXSEL_ROW(0x04)
10919 GEN_XXSEL_ROW(0x05)
10920 GEN_XXSEL_ROW(0x06)
10921 GEN_XXSEL_ROW(0x07)
10922 GEN_XXSEL_ROW(0x08)
10923 GEN_XXSEL_ROW(0x09)
10924 GEN_XXSEL_ROW(0x0A)
10925 GEN_XXSEL_ROW(0x0B)
10926 GEN_XXSEL_ROW(0x0C)
10927 GEN_XXSEL_ROW(0x0D)
10928 GEN_XXSEL_ROW(0x0E)
10929 GEN_XXSEL_ROW(0x0F)
10930 GEN_XXSEL_ROW(0x10)
10931 GEN_XXSEL_ROW(0x11)
10932 GEN_XXSEL_ROW(0x12)
10933 GEN_XXSEL_ROW(0x13)
10934 GEN_XXSEL_ROW(0x14)
10935 GEN_XXSEL_ROW(0x15)
10936 GEN_XXSEL_ROW(0x16)
10937 GEN_XXSEL_ROW(0x17)
10938 GEN_XXSEL_ROW(0x18)
10939 GEN_XXSEL_ROW(0x19)
10940 GEN_XXSEL_ROW(0x1A)
10941 GEN_XXSEL_ROW(0x1B)
10942 GEN_XXSEL_ROW(0x1C)
10943 GEN_XXSEL_ROW(0x1D)
10944 GEN_XXSEL_ROW(0x1E)
10945 GEN_XXSEL_ROW(0x1F)
10946
10947 GEN_XX3FORM_DM(xxpermdi, 0x08, 0x01),
10948
10949 #undef GEN_DFP_T_A_B_Rc
10950 #undef GEN_DFP_BF_A_B
10951 #undef GEN_DFP_BF_A_DCM
10952 #undef GEN_DFP_T_B_U32_U32_Rc
10953 #undef GEN_DFP_T_A_B_I32_Rc
10954 #undef GEN_DFP_T_B_Rc
10955 #undef GEN_DFP_T_FPR_I32_Rc
10956
10957 #define _GEN_DFP_LONG(name, op1, op2, mask) \
10958 GEN_HANDLER_E(name, 0x3B, op1, op2, mask, PPC_NONE, PPC2_DFP)
10959
10960 #define _GEN_DFP_LONGx2(name, op1, op2, mask) \
10961 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10962 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
10963
10964 #define _GEN_DFP_LONGx4(name, op1, op2, mask) \
10965 GEN_HANDLER_E(name, 0x3B, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10966 GEN_HANDLER_E(name, 0x3B, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
10967 GEN_HANDLER_E(name, 0x3B, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
10968 GEN_HANDLER_E(name, 0x3B, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
10969
10970 #define _GEN_DFP_QUAD(name, op1, op2, mask) \
10971 GEN_HANDLER_E(name, 0x3F, op1, op2, mask, PPC_NONE, PPC2_DFP)
10972
10973 #define _GEN_DFP_QUADx2(name, op1, op2, mask) \
10974 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10975 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP)
10976
10977 #define _GEN_DFP_QUADx4(name, op1, op2, mask)                         \
10978 GEN_HANDLER_E(name, 0x3F, op1, 0x00 | op2, mask, PPC_NONE, PPC2_DFP), \
10979 GEN_HANDLER_E(name, 0x3F, op1, 0x08 | op2, mask, PPC_NONE, PPC2_DFP), \
10980 GEN_HANDLER_E(name, 0x3F, op1, 0x10 | op2, mask, PPC_NONE, PPC2_DFP), \
10981 GEN_HANDLER_E(name, 0x3F, op1, 0x18 | op2, mask, PPC_NONE, PPC2_DFP)
10982
10983 #define GEN_DFP_T_A_B_Rc(name, op1, op2) \
10984 _GEN_DFP_LONG(name, op1, op2, 0x00000000)
10985
10986 #define GEN_DFP_Tp_Ap_Bp_Rc(name, op1, op2) \
10987 _GEN_DFP_QUAD(name, op1, op2, 0x00210800)
10988
10989 #define GEN_DFP_Tp_A_Bp_Rc(name, op1, op2) \
10990 _GEN_DFP_QUAD(name, op1, op2, 0x00200800)
10991
10992 #define GEN_DFP_T_B_Rc(name, op1, op2) \
10993 _GEN_DFP_LONG(name, op1, op2, 0x001F0000)
10994
10995 #define GEN_DFP_Tp_Bp_Rc(name, op1, op2) \
10996 _GEN_DFP_QUAD(name, op1, op2, 0x003F0800)
10997
10998 #define GEN_DFP_Tp_B_Rc(name, op1, op2) \
10999 _GEN_DFP_QUAD(name, op1, op2, 0x003F0000)
11000
11001 #define GEN_DFP_T_Bp_Rc(name, op1, op2) \
11002 _GEN_DFP_QUAD(name, op1, op2, 0x001F0800)
11003
11004 #define GEN_DFP_BF_A_B(name, op1, op2) \
11005 _GEN_DFP_LONG(name, op1, op2, 0x00000001)
11006
11007 #define GEN_DFP_BF_Ap_Bp(name, op1, op2) \
11008 _GEN_DFP_QUAD(name, op1, op2, 0x00610801)
11009
11010 #define GEN_DFP_BF_A_Bp(name, op1, op2) \
11011 _GEN_DFP_QUAD(name, op1, op2, 0x00600801)
11012
11013 #define GEN_DFP_BF_A_DCM(name, op1, op2) \
11014 _GEN_DFP_LONGx2(name, op1, op2, 0x00600001)
11015
11016 #define GEN_DFP_BF_Ap_DCM(name, op1, op2) \
11017 _GEN_DFP_QUADx2(name, op1, op2, 0x00610001)
11018
11019 #define GEN_DFP_T_A_B_RMC_Rc(name, op1, op2) \
11020 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
11021
11022 #define GEN_DFP_Tp_Ap_Bp_RMC_Rc(name, op1, op2) \
11023 _GEN_DFP_QUADx4(name, op1, op2, 0x02010800)
11024
11025 #define GEN_DFP_Tp_A_Bp_RMC_Rc(name, op1, op2) \
11026 _GEN_DFP_QUADx4(name, op1, op2, 0x02000800)
11027
11028 #define GEN_DFP_TE_T_B_RMC_Rc(name, op1, op2) \
11029 _GEN_DFP_LONGx4(name, op1, op2, 0x00000000)
11030
11031 #define GEN_DFP_TE_Tp_Bp_RMC_Rc(name, op1, op2) \
11032 _GEN_DFP_QUADx4(name, op1, op2, 0x00200800)
11033
11034 #define GEN_DFP_R_T_B_RMC_Rc(name, op1, op2) \
11035 _GEN_DFP_LONGx4(name, op1, op2, 0x001E0000)
11036
11037 #define GEN_DFP_R_Tp_Bp_RMC_Rc(name, op1, op2) \
11038 _GEN_DFP_QUADx4(name, op1, op2, 0x003E0800)
11039
11040 #define GEN_DFP_SP_T_B_Rc(name, op1, op2) \
11041 _GEN_DFP_LONG(name, op1, op2, 0x00070000)
11042
11043 #define GEN_DFP_SP_Tp_Bp_Rc(name, op1, op2) \
11044 _GEN_DFP_QUAD(name, op1, op2, 0x00270800)
11045
11046 #define GEN_DFP_S_T_B_Rc(name, op1, op2) \
11047 _GEN_DFP_LONG(name, op1, op2, 0x000F0000)
11048
11049 #define GEN_DFP_S_Tp_Bp_Rc(name, op1, op2) \
11050 _GEN_DFP_QUAD(name, op1, op2, 0x002F0800)
11051
11052 #define GEN_DFP_T_A_SH_Rc(name, op1, op2) \
11053 _GEN_DFP_LONGx2(name, op1, op2, 0x00000000)
11054
11055 #define GEN_DFP_Tp_Ap_SH_Rc(name, op1, op2) \
11056 _GEN_DFP_QUADx2(name, op1, op2, 0x00210000)
11057
11058 GEN_DFP_T_A_B_Rc(dadd, 0x02, 0x00),
11059 GEN_DFP_Tp_Ap_Bp_Rc(daddq, 0x02, 0x00),
11060 GEN_DFP_T_A_B_Rc(dsub, 0x02, 0x10),
11061 GEN_DFP_Tp_Ap_Bp_Rc(dsubq, 0x02, 0x10),
11062 GEN_DFP_T_A_B_Rc(dmul, 0x02, 0x01),
11063 GEN_DFP_Tp_Ap_Bp_Rc(dmulq, 0x02, 0x01),
11064 GEN_DFP_T_A_B_Rc(ddiv, 0x02, 0x11),
11065 GEN_DFP_Tp_Ap_Bp_Rc(ddivq, 0x02, 0x11),
11066 GEN_DFP_BF_A_B(dcmpu, 0x02, 0x14),
11067 GEN_DFP_BF_Ap_Bp(dcmpuq, 0x02, 0x14),
11068 GEN_DFP_BF_A_B(dcmpo, 0x02, 0x04),
11069 GEN_DFP_BF_Ap_Bp(dcmpoq, 0x02, 0x04),
11070 GEN_DFP_BF_A_DCM(dtstdc, 0x02, 0x06),
11071 GEN_DFP_BF_Ap_DCM(dtstdcq, 0x02, 0x06),
11072 GEN_DFP_BF_A_DCM(dtstdg, 0x02, 0x07),
11073 GEN_DFP_BF_Ap_DCM(dtstdgq, 0x02, 0x07),
11074 GEN_DFP_BF_A_B(dtstex, 0x02, 0x05),
11075 GEN_DFP_BF_Ap_Bp(dtstexq, 0x02, 0x05),
11076 GEN_DFP_BF_A_B(dtstsf, 0x02, 0x15),
11077 GEN_DFP_BF_A_Bp(dtstsfq, 0x02, 0x15),
11078 GEN_DFP_TE_T_B_RMC_Rc(dquai, 0x03, 0x02),
11079 GEN_DFP_TE_Tp_Bp_RMC_Rc(dquaiq, 0x03, 0x02),
11080 GEN_DFP_T_A_B_RMC_Rc(dqua, 0x03, 0x00),
11081 GEN_DFP_Tp_Ap_Bp_RMC_Rc(dquaq, 0x03, 0x00),
11082 GEN_DFP_T_A_B_RMC_Rc(drrnd, 0x03, 0x01),
11083 GEN_DFP_Tp_A_Bp_RMC_Rc(drrndq, 0x03, 0x01),
11084 GEN_DFP_R_T_B_RMC_Rc(drintx, 0x03, 0x03),
11085 GEN_DFP_R_Tp_Bp_RMC_Rc(drintxq, 0x03, 0x03),
11086 GEN_DFP_R_T_B_RMC_Rc(drintn, 0x03, 0x07),
11087 GEN_DFP_R_Tp_Bp_RMC_Rc(drintnq, 0x03, 0x07),
11088 GEN_DFP_T_B_Rc(dctdp, 0x02, 0x08),
11089 GEN_DFP_Tp_B_Rc(dctqpq, 0x02, 0x08),
11090 GEN_DFP_T_B_Rc(drsp, 0x02, 0x18),
11091 GEN_DFP_Tp_Bp_Rc(drdpq, 0x02, 0x18),
11092 GEN_DFP_T_B_Rc(dcffix, 0x02, 0x19),
11093 GEN_DFP_Tp_B_Rc(dcffixq, 0x02, 0x19),
11094 GEN_DFP_T_B_Rc(dctfix, 0x02, 0x09),
11095 GEN_DFP_T_Bp_Rc(dctfixq, 0x02, 0x09),
11096 GEN_DFP_SP_T_B_Rc(ddedpd, 0x02, 0x0a),
11097 GEN_DFP_SP_Tp_Bp_Rc(ddedpdq, 0x02, 0x0a),
11098 GEN_DFP_S_T_B_Rc(denbcd, 0x02, 0x1a),
11099 GEN_DFP_S_Tp_Bp_Rc(denbcdq, 0x02, 0x1a),
11100 GEN_DFP_T_B_Rc(dxex, 0x02, 0x0b),
11101 GEN_DFP_T_Bp_Rc(dxexq, 0x02, 0x0b),
11102 GEN_DFP_T_A_B_Rc(diex, 0x02, 0x1b),
11103 GEN_DFP_Tp_A_Bp_Rc(diexq, 0x02, 0x1b),
11104 GEN_DFP_T_A_SH_Rc(dscli, 0x02, 0x02),
11105 GEN_DFP_Tp_Ap_SH_Rc(dscliq, 0x02, 0x02),
11106 GEN_DFP_T_A_SH_Rc(dscri, 0x02, 0x03),
11107 GEN_DFP_Tp_Ap_SH_Rc(dscriq, 0x02, 0x03),
11108
11109 #undef GEN_SPE
11110 #define GEN_SPE(name0, name1, opc2, opc3, inval0, inval1, type) \
11111     GEN_OPCODE_DUAL(name0##_##name1, 0x04, opc2, opc3, inval0, inval1, type, PPC_NONE)
11112 GEN_SPE(evaddw,      speundef,    0x00, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11113 GEN_SPE(evaddiw,     speundef,    0x01, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11114 GEN_SPE(evsubfw,     speundef,    0x02, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11115 GEN_SPE(evsubifw,    speundef,    0x03, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11116 GEN_SPE(evabs,       evneg,       0x04, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11117 GEN_SPE(evextsb,     evextsh,     0x05, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11118 GEN_SPE(evrndw,      evcntlzw,    0x06, 0x08, 0x0000F800, 0x0000F800, PPC_SPE),
11119 GEN_SPE(evcntlsw,    brinc,       0x07, 0x08, 0x0000F800, 0x00000000, PPC_SPE),
11120 GEN_SPE(evmra,       speundef,    0x02, 0x13, 0x0000F800, 0xFFFFFFFF, PPC_SPE),
11121 GEN_SPE(speundef,    evand,       0x08, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11122 GEN_SPE(evandc,      speundef,    0x09, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11123 GEN_SPE(evxor,       evor,        0x0B, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11124 GEN_SPE(evnor,       eveqv,       0x0C, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11125 GEN_SPE(evmwumi,     evmwsmi,     0x0C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11126 GEN_SPE(evmwumia,    evmwsmia,    0x1C, 0x11, 0x00000000, 0x00000000, PPC_SPE),
11127 GEN_SPE(evmwumiaa,   evmwsmiaa,   0x0C, 0x15, 0x00000000, 0x00000000, PPC_SPE),
11128 GEN_SPE(speundef,    evorc,       0x0D, 0x08, 0xFFFFFFFF, 0x00000000, PPC_SPE),
11129 GEN_SPE(evnand,      speundef,    0x0F, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11130 GEN_SPE(evsrwu,      evsrws,      0x10, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11131 GEN_SPE(evsrwiu,     evsrwis,     0x11, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11132 GEN_SPE(evslw,       speundef,    0x12, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11133 GEN_SPE(evslwi,      speundef,    0x13, 0x08, 0x00000000, 0xFFFFFFFF, PPC_SPE),
11134 GEN_SPE(evrlw,       evsplati,    0x14, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11135 GEN_SPE(evrlwi,      evsplatfi,   0x15, 0x08, 0x00000000, 0x0000F800, PPC_SPE),
11136 GEN_SPE(evmergehi,   evmergelo,   0x16, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11137 GEN_SPE(evmergehilo, evmergelohi, 0x17, 0x08, 0x00000000, 0x00000000, PPC_SPE),
11138 GEN_SPE(evcmpgtu,    evcmpgts,    0x18, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11139 GEN_SPE(evcmpltu,    evcmplts,    0x19, 0x08, 0x00600000, 0x00600000, PPC_SPE),
11140 GEN_SPE(evcmpeq,     speundef,    0x1A, 0x08, 0x00600000, 0xFFFFFFFF, PPC_SPE),
11141
11142 GEN_SPE(evfsadd,     evfssub,     0x00, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11143 GEN_SPE(evfsabs,     evfsnabs,    0x02, 0x0A, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11144 GEN_SPE(evfsneg,     speundef,    0x03, 0x0A, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11145 GEN_SPE(evfsmul,     evfsdiv,     0x04, 0x0A, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11146 GEN_SPE(evfscmpgt,   evfscmplt,   0x06, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11147 GEN_SPE(evfscmpeq,   speundef,    0x07, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11148 GEN_SPE(evfscfui,    evfscfsi,    0x08, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11149 GEN_SPE(evfscfuf,    evfscfsf,    0x09, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11150 GEN_SPE(evfsctui,    evfsctsi,    0x0A, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11151 GEN_SPE(evfsctuf,    evfsctsf,    0x0B, 0x0A, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11152 GEN_SPE(evfsctuiz,   speundef,    0x0C, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11153 GEN_SPE(evfsctsiz,   speundef,    0x0D, 0x0A, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11154 GEN_SPE(evfststgt,   evfststlt,   0x0E, 0x0A, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11155 GEN_SPE(evfststeq,   speundef,    0x0F, 0x0A, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11156
11157 GEN_SPE(efsadd,      efssub,      0x00, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11158 GEN_SPE(efsabs,      efsnabs,     0x02, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_SINGLE),
11159 GEN_SPE(efsneg,      speundef,    0x03, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_SINGLE),
11160 GEN_SPE(efsmul,      efsdiv,      0x04, 0x0B, 0x00000000, 0x00000000, PPC_SPE_SINGLE),
11161 GEN_SPE(efscmpgt,    efscmplt,    0x06, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11162 GEN_SPE(efscmpeq,    efscfd,      0x07, 0x0B, 0x00600000, 0x00180000, PPC_SPE_SINGLE),
11163 GEN_SPE(efscfui,     efscfsi,     0x08, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11164 GEN_SPE(efscfuf,     efscfsf,     0x09, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11165 GEN_SPE(efsctui,     efsctsi,     0x0A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11166 GEN_SPE(efsctuf,     efsctsf,     0x0B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_SINGLE),
11167 GEN_SPE(efsctuiz,    speundef,    0x0C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11168 GEN_SPE(efsctsiz,    speundef,    0x0D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11169 GEN_SPE(efststgt,    efststlt,    0x0E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_SINGLE),
11170 GEN_SPE(efststeq,    speundef,    0x0F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_SINGLE),
11171
11172 GEN_SPE(efdadd,      efdsub,      0x10, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11173 GEN_SPE(efdcfuid,    efdcfsid,    0x11, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11174 GEN_SPE(efdabs,      efdnabs,     0x12, 0x0B, 0x0000F800, 0x0000F800, PPC_SPE_DOUBLE),
11175 GEN_SPE(efdneg,      speundef,    0x13, 0x0B, 0x0000F800, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11176 GEN_SPE(efdmul,      efddiv,      0x14, 0x0B, 0x00000000, 0x00000000, PPC_SPE_DOUBLE),
11177 GEN_SPE(efdctuidz,   efdctsidz,   0x15, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11178 GEN_SPE(efdcmpgt,    efdcmplt,    0x16, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11179 GEN_SPE(efdcmpeq,    efdcfs,      0x17, 0x0B, 0x00600000, 0x00180000, PPC_SPE_DOUBLE),
11180 GEN_SPE(efdcfui,     efdcfsi,     0x18, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11181 GEN_SPE(efdcfuf,     efdcfsf,     0x19, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11182 GEN_SPE(efdctui,     efdctsi,     0x1A, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11183 GEN_SPE(efdctuf,     efdctsf,     0x1B, 0x0B, 0x00180000, 0x00180000, PPC_SPE_DOUBLE),
11184 GEN_SPE(efdctuiz,    speundef,    0x1C, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11185 GEN_SPE(efdctsiz,    speundef,    0x1D, 0x0B, 0x00180000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11186 GEN_SPE(efdtstgt,    efdtstlt,    0x1E, 0x0B, 0x00600000, 0x00600000, PPC_SPE_DOUBLE),
11187 GEN_SPE(efdtsteq,    speundef,    0x1F, 0x0B, 0x00600000, 0xFFFFFFFF, PPC_SPE_DOUBLE),
11188
11189 #undef GEN_SPEOP_LDST
11190 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
11191 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)
11192 GEN_SPEOP_LDST(evldd, 0x00, 3),
11193 GEN_SPEOP_LDST(evldw, 0x01, 3),
11194 GEN_SPEOP_LDST(evldh, 0x02, 3),
11195 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1),
11196 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1),
11197 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1),
11198 GEN_SPEOP_LDST(evlwhe, 0x08, 2),
11199 GEN_SPEOP_LDST(evlwhou, 0x0A, 2),
11200 GEN_SPEOP_LDST(evlwhos, 0x0B, 2),
11201 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2),
11202 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2),
11203
11204 GEN_SPEOP_LDST(evstdd, 0x10, 3),
11205 GEN_SPEOP_LDST(evstdw, 0x11, 3),
11206 GEN_SPEOP_LDST(evstdh, 0x12, 3),
11207 GEN_SPEOP_LDST(evstwhe, 0x18, 2),
11208 GEN_SPEOP_LDST(evstwho, 0x1A, 2),
11209 GEN_SPEOP_LDST(evstwwe, 0x1C, 2),
11210 GEN_SPEOP_LDST(evstwwo, 0x1E, 2),
11211
11212 GEN_HANDLER2_E(tbegin, "tbegin", 0x1F, 0x0E, 0x14, 0x01DFF800, \
11213                PPC_NONE, PPC2_TM),
11214 GEN_HANDLER2_E(tend,   "tend",   0x1F, 0x0E, 0x15, 0x01FFF800, \
11215                PPC_NONE, PPC2_TM),
11216 GEN_HANDLER2_E(tabort, "tabort", 0x1F, 0x0E, 0x1C, 0x03E0F800, \
11217                PPC_NONE, PPC2_TM),
11218 GEN_HANDLER2_E(tabortwc, "tabortwc", 0x1F, 0x0E, 0x18, 0x00000000, \
11219                PPC_NONE, PPC2_TM),
11220 GEN_HANDLER2_E(tabortwci, "tabortwci", 0x1F, 0x0E, 0x1A, 0x00000000, \
11221                PPC_NONE, PPC2_TM),
11222 GEN_HANDLER2_E(tabortdc, "tabortdc", 0x1F, 0x0E, 0x19, 0x00000000, \
11223                PPC_NONE, PPC2_TM),
11224 GEN_HANDLER2_E(tabortdci, "tabortdci", 0x1F, 0x0E, 0x1B, 0x00000000, \
11225                PPC_NONE, PPC2_TM),
11226 GEN_HANDLER2_E(tsr, "tsr", 0x1F, 0x0E, 0x17, 0x03DFF800, \
11227                PPC_NONE, PPC2_TM),
11228 GEN_HANDLER2_E(tcheck, "tcheck", 0x1F, 0x0E, 0x16, 0x007FF800, \
11229                PPC_NONE, PPC2_TM),
11230 GEN_HANDLER2_E(treclaim, "treclaim", 0x1F, 0x0E, 0x1D, 0x03E0F800, \
11231                PPC_NONE, PPC2_TM),
11232 GEN_HANDLER2_E(trechkpt, "trechkpt", 0x1F, 0x0E, 0x1F, 0x03FFF800, \
11233                PPC_NONE, PPC2_TM),
11234 };
11235
11236 #include "helper_regs.h"
11237 #include "translate_init.c"
11238
11239 /*****************************************************************************/
11240 /* Misc PowerPC helpers */
11241 void ppc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
11242                         int flags)
11243 {
11244 #define RGPL  4
11245 #define RFPL  4
11246
11247     PowerPCCPU *cpu = POWERPC_CPU(cs);
11248     CPUPPCState *env = &cpu->env;
11249     int i;
11250
11251     cpu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
11252                 TARGET_FMT_lx " XER " TARGET_FMT_lx " CPU#%d\n",
11253                 env->nip, env->lr, env->ctr, cpu_read_xer(env),
11254                 cs->cpu_index);
11255     cpu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
11256                 TARGET_FMT_lx " iidx %d didx %d\n",
11257                 env->msr, env->spr[SPR_HID0],
11258                 env->hflags, env->immu_idx, env->dmmu_idx);
11259 #if !defined(NO_TIMER_DUMP)
11260     cpu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
11261 #if !defined(CONFIG_USER_ONLY)
11262                 " DECR %08" PRIu32
11263 #endif
11264                 "\n",
11265                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
11266 #if !defined(CONFIG_USER_ONLY)
11267                 , cpu_ppc_load_decr(env)
11268 #endif
11269                 );
11270 #endif
11271     for (i = 0; i < 32; i++) {
11272         if ((i & (RGPL - 1)) == 0)
11273             cpu_fprintf(f, "GPR%02d", i);
11274         cpu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
11275         if ((i & (RGPL - 1)) == (RGPL - 1))
11276             cpu_fprintf(f, "\n");
11277     }
11278     cpu_fprintf(f, "CR ");
11279     for (i = 0; i < 8; i++)
11280         cpu_fprintf(f, "%01x", env->crf[i]);
11281     cpu_fprintf(f, "  [");
11282     for (i = 0; i < 8; i++) {
11283         char a = '-';
11284         if (env->crf[i] & 0x08)
11285             a = 'L';
11286         else if (env->crf[i] & 0x04)
11287             a = 'G';
11288         else if (env->crf[i] & 0x02)
11289             a = 'E';
11290         cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
11291     }
11292     cpu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
11293                 env->reserve_addr);
11294     for (i = 0; i < 32; i++) {
11295         if ((i & (RFPL - 1)) == 0)
11296             cpu_fprintf(f, "FPR%02d", i);
11297         cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
11298         if ((i & (RFPL - 1)) == (RFPL - 1))
11299             cpu_fprintf(f, "\n");
11300     }
11301     cpu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
11302 #if !defined(CONFIG_USER_ONLY)
11303     cpu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
11304                    "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
11305                 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
11306                 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
11307
11308     cpu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
11309                    "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
11310                 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
11311                 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
11312
11313     cpu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
11314                    "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
11315                 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
11316                 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
11317
11318     if (env->excp_model == POWERPC_EXCP_BOOKE) {
11319         cpu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
11320                        " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
11321                     env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
11322                     env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
11323
11324         cpu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
11325                        "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
11326                     env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
11327                     env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
11328
11329         cpu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
11330                        "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
11331                     env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
11332                     env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
11333
11334         cpu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
11335                        "    EPR " TARGET_FMT_lx "\n",
11336                     env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
11337                     env->spr[SPR_BOOKE_EPR]);
11338
11339         /* FSL-specific */
11340         cpu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
11341                        "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
11342                     env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
11343                     env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
11344
11345         /*
11346          * IVORs are left out as they are large and do not change often --
11347          * they can be read with "p $ivor0", "p $ivor1", etc.
11348          */
11349     }
11350
11351 #if defined(TARGET_PPC64)
11352     if (env->flags & POWERPC_FLAG_CFAR) {
11353         cpu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
11354     }
11355 #endif
11356
11357     switch (env->mmu_model) {
11358     case POWERPC_MMU_32B:
11359     case POWERPC_MMU_601:
11360     case POWERPC_MMU_SOFT_6xx:
11361     case POWERPC_MMU_SOFT_74xx:
11362 #if defined(TARGET_PPC64)
11363     case POWERPC_MMU_64B:
11364     case POWERPC_MMU_2_03:
11365     case POWERPC_MMU_2_06:
11366     case POWERPC_MMU_2_06a:
11367     case POWERPC_MMU_2_07:
11368     case POWERPC_MMU_2_07a:
11369 #endif
11370         cpu_fprintf(f, " SDR1 " TARGET_FMT_lx "   DAR " TARGET_FMT_lx
11371                        "  DSISR " TARGET_FMT_lx "\n", env->spr[SPR_SDR1],
11372                     env->spr[SPR_DAR], env->spr[SPR_DSISR]);
11373         break;
11374     case POWERPC_MMU_BOOKE206:
11375         cpu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
11376                        "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
11377                     env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
11378                     env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
11379
11380         cpu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
11381                        "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
11382                     env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
11383                     env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
11384
11385         cpu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
11386                        " TLB1CFG " TARGET_FMT_lx "\n",
11387                     env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
11388                     env->spr[SPR_BOOKE_TLB1CFG]);
11389         break;
11390     default:
11391         break;
11392     }
11393 #endif
11394
11395 #undef RGPL
11396 #undef RFPL
11397 }
11398
11399 void ppc_cpu_dump_statistics(CPUState *cs, FILE*f,
11400                              fprintf_function cpu_fprintf, int flags)
11401 {
11402 #if defined(DO_PPC_STATISTICS)
11403     PowerPCCPU *cpu = POWERPC_CPU(cs);
11404     opc_handler_t **t1, **t2, **t3, *handler;
11405     int op1, op2, op3;
11406
11407     t1 = cpu->env.opcodes;
11408     for (op1 = 0; op1 < 64; op1++) {
11409         handler = t1[op1];
11410         if (is_indirect_opcode(handler)) {
11411             t2 = ind_table(handler);
11412             for (op2 = 0; op2 < 32; op2++) {
11413                 handler = t2[op2];
11414                 if (is_indirect_opcode(handler)) {
11415                     t3 = ind_table(handler);
11416                     for (op3 = 0; op3 < 32; op3++) {
11417                         handler = t3[op3];
11418                         if (handler->count == 0)
11419                             continue;
11420                         cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
11421                                     "%016" PRIx64 " %" PRId64 "\n",
11422                                     op1, op2, op3, op1, (op3 << 5) | op2,
11423                                     handler->oname,
11424                                     handler->count, handler->count);
11425                     }
11426                 } else {
11427                     if (handler->count == 0)
11428                         continue;
11429                     cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
11430                                 "%016" PRIx64 " %" PRId64 "\n",
11431                                 op1, op2, op1, op2, handler->oname,
11432                                 handler->count, handler->count);
11433                 }
11434             }
11435         } else {
11436             if (handler->count == 0)
11437                 continue;
11438             cpu_fprintf(f, "%02x       (%02x     ) %16s: %016" PRIx64
11439                         " %" PRId64 "\n",
11440                         op1, op1, handler->oname,
11441                         handler->count, handler->count);
11442         }
11443     }
11444 #endif
11445 }
11446
11447 /*****************************************************************************/
11448 void gen_intermediate_code(CPUPPCState *env, struct TranslationBlock *tb)
11449 {
11450     PowerPCCPU *cpu = ppc_env_get_cpu(env);
11451     CPUState *cs = CPU(cpu);
11452     DisasContext ctx, *ctxp = &ctx;
11453     opc_handler_t **table, *handler;
11454     target_ulong pc_start;
11455     int num_insns;
11456     int max_insns;
11457
11458     pc_start = tb->pc;
11459     ctx.nip = pc_start;
11460     ctx.tb = tb;
11461     ctx.exception = POWERPC_EXCP_NONE;
11462     ctx.spr_cb = env->spr_cb;
11463     ctx.pr = msr_pr;
11464     ctx.hv = !msr_pr && msr_hv;
11465     ctx.mem_idx = env->dmmu_idx;
11466     ctx.insns_flags = env->insns_flags;
11467     ctx.insns_flags2 = env->insns_flags2;
11468     ctx.access_type = -1;
11469     ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
11470     ctx.default_tcg_memop_mask = ctx.le_mode ? MO_LE : MO_BE;
11471 #if defined(TARGET_PPC64)
11472     ctx.sf_mode = msr_is_64bit(env, env->msr);
11473     ctx.has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
11474 #endif
11475     ctx.fpu_enabled = msr_fp;
11476     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
11477         ctx.spe_enabled = msr_spe;
11478     else
11479         ctx.spe_enabled = 0;
11480     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
11481         ctx.altivec_enabled = msr_vr;
11482     else
11483         ctx.altivec_enabled = 0;
11484     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
11485         ctx.vsx_enabled = msr_vsx;
11486     } else {
11487         ctx.vsx_enabled = 0;
11488     }
11489 #if defined(TARGET_PPC64)
11490     if ((env->flags & POWERPC_FLAG_TM) && msr_tm) {
11491         ctx.tm_enabled = msr_tm;
11492     } else {
11493         ctx.tm_enabled = 0;
11494     }
11495 #endif
11496     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
11497         ctx.singlestep_enabled = CPU_SINGLE_STEP;
11498     else
11499         ctx.singlestep_enabled = 0;
11500     if ((env->flags & POWERPC_FLAG_BE) && msr_be)
11501         ctx.singlestep_enabled |= CPU_BRANCH_STEP;
11502     if (unlikely(cs->singlestep_enabled)) {
11503         ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
11504     }
11505 #if defined (DO_SINGLE_STEP) && 0
11506     /* Single step trace mode */
11507     msr_se = 1;
11508 #endif
11509     num_insns = 0;
11510     max_insns = tb->cflags & CF_COUNT_MASK;
11511     if (max_insns == 0) {
11512         max_insns = CF_COUNT_MASK;
11513     }
11514     if (max_insns > TCG_MAX_INSNS) {
11515         max_insns = TCG_MAX_INSNS;
11516     }
11517
11518     gen_tb_start(tb);
11519     tcg_clear_temp_count();
11520     /* Set env in case of segfault during code fetch */
11521     while (ctx.exception == POWERPC_EXCP_NONE && !tcg_op_buf_full()) {
11522         tcg_gen_insn_start(ctx.nip);
11523         num_insns++;
11524
11525         if (unlikely(cpu_breakpoint_test(cs, ctx.nip, BP_ANY))) {
11526             gen_debug_exception(ctxp);
11527             /* The address covered by the breakpoint must be included in
11528                [tb->pc, tb->pc + tb->size) in order to for it to be
11529                properly cleared -- thus we increment the PC here so that
11530                the logic setting tb->size below does the right thing.  */
11531             ctx.nip += 4;
11532             break;
11533         }
11534
11535         LOG_DISAS("----------------\n");
11536         LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
11537                   ctx.nip, ctx.mem_idx, (int)msr_ir);
11538         if (num_insns == max_insns && (tb->cflags & CF_LAST_IO))
11539             gen_io_start();
11540         if (unlikely(need_byteswap(&ctx))) {
11541             ctx.opcode = bswap32(cpu_ldl_code(env, ctx.nip));
11542         } else {
11543             ctx.opcode = cpu_ldl_code(env, ctx.nip);
11544         }
11545         LOG_DISAS("translate opcode %08x (%02x %02x %02x) (%s)\n",
11546                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
11547                     opc3(ctx.opcode), ctx.le_mode ? "little" : "big");
11548         ctx.nip += 4;
11549         table = env->opcodes;
11550         handler = table[opc1(ctx.opcode)];
11551         if (is_indirect_opcode(handler)) {
11552             table = ind_table(handler);
11553             handler = table[opc2(ctx.opcode)];
11554             if (is_indirect_opcode(handler)) {
11555                 table = ind_table(handler);
11556                 handler = table[opc3(ctx.opcode)];
11557             }
11558         }
11559         /* Is opcode *REALLY* valid ? */
11560         if (unlikely(handler->handler == &gen_invalid)) {
11561             qemu_log_mask(LOG_GUEST_ERROR, "invalid/unsupported opcode: "
11562                           "%02x - %02x - %02x (%08x) " TARGET_FMT_lx " %d\n",
11563                           opc1(ctx.opcode), opc2(ctx.opcode),
11564                           opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
11565         } else {
11566             uint32_t inval;
11567
11568             if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE) && Rc(ctx.opcode))) {
11569                 inval = handler->inval2;
11570             } else {
11571                 inval = handler->inval1;
11572             }
11573
11574             if (unlikely((ctx.opcode & inval) != 0)) {
11575                 qemu_log_mask(LOG_GUEST_ERROR, "invalid bits: %08x for opcode: "
11576                               "%02x - %02x - %02x (%08x) " TARGET_FMT_lx "\n",
11577                               ctx.opcode & inval, opc1(ctx.opcode),
11578                               opc2(ctx.opcode), opc3(ctx.opcode),
11579                               ctx.opcode, ctx.nip - 4);
11580                 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
11581                 break;
11582             }
11583         }
11584         (*(handler->handler))(&ctx);
11585 #if defined(DO_PPC_STATISTICS)
11586         handler->count++;
11587 #endif
11588         /* Check trace mode exceptions */
11589         if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
11590                      (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
11591                      ctx.exception != POWERPC_SYSCALL &&
11592                      ctx.exception != POWERPC_EXCP_TRAP &&
11593                      ctx.exception != POWERPC_EXCP_BRANCH)) {
11594             gen_exception(ctxp, POWERPC_EXCP_TRACE);
11595         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
11596                             (cs->singlestep_enabled) ||
11597                             singlestep ||
11598                             num_insns >= max_insns)) {
11599             /* if we reach a page boundary or are single stepping, stop
11600              * generation
11601              */
11602             break;
11603         }
11604         if (tcg_check_temp_count()) {
11605             fprintf(stderr, "Opcode %02x %02x %02x (%08x) leaked temporaries\n",
11606                     opc1(ctx.opcode), opc2(ctx.opcode), opc3(ctx.opcode),
11607                     ctx.opcode);
11608             exit(1);
11609         }
11610     }
11611     if (tb->cflags & CF_LAST_IO)
11612         gen_io_end();
11613     if (ctx.exception == POWERPC_EXCP_NONE) {
11614         gen_goto_tb(&ctx, 0, ctx.nip);
11615     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
11616         if (unlikely(cs->singlestep_enabled)) {
11617             gen_debug_exception(ctxp);
11618         }
11619         /* Generate the return instruction */
11620         tcg_gen_exit_tb(0);
11621     }
11622     gen_tb_end(tb, num_insns);
11623
11624     tb->size = ctx.nip - pc_start;
11625     tb->icount = num_insns;
11626
11627 #if defined(DEBUG_DISAS)
11628     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
11629         int flags;
11630         flags = env->bfd_mach;
11631         flags |= ctx.le_mode << 16;
11632         qemu_log("IN: %s\n", lookup_symbol(pc_start));
11633         log_target_disas(cs, pc_start, ctx.nip - pc_start, flags);
11634         qemu_log("\n");
11635     }
11636 #endif
11637 }
11638
11639 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
11640                           target_ulong *data)
11641 {
11642     env->nip = data[0];
11643 }
This page took 0.715264 seconds and 4 git commands to generate.