target-ppc: fix mtfsb0 and mtfsb1
[qemu] / target-ppc / translate.c
1 /*
2  *  PowerPC emulation for qemu: main translation routines.
3  *
4  *  Copyright (c) 2003-2007 Jocelyn Mayer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25
26 #include "cpu.h"
27 #include "exec-all.h"
28 #include "disas.h"
29 #include "tcg-op.h"
30 #include "qemu-common.h"
31
32 #include "helper.h"
33 #define GEN_HELPER 1
34 #include "helper.h"
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 DO_SINGLE_STEP
42 //#define PPC_DEBUG_DISAS
43 //#define DO_PPC_STATISTICS
44 //#define OPTIMIZE_FPRF_UPDATE
45
46 /*****************************************************************************/
47 /* Code translation helpers                                                  */
48
49 /* global register indexes */
50 static TCGv_ptr cpu_env;
51 static char cpu_reg_names[10*3 + 22*4 /* GPR */
52 #if !defined(TARGET_PPC64)
53     + 10*4 + 22*5 /* SPE GPRh */
54 #endif
55     + 10*4 + 22*5 /* FPR */
56     + 2*(10*6 + 22*7) /* AVRh, AVRl */
57     + 8*5 /* CRF */];
58 static TCGv cpu_gpr[32];
59 #if !defined(TARGET_PPC64)
60 static TCGv cpu_gprh[32];
61 #endif
62 static TCGv_i64 cpu_fpr[32];
63 static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64 static TCGv_i32 cpu_crf[8];
65 static TCGv cpu_nip;
66 static TCGv cpu_msr;
67 static TCGv cpu_ctr;
68 static TCGv cpu_lr;
69 static TCGv cpu_xer;
70 static TCGv cpu_reserve;
71 static TCGv_i32 cpu_fpscr;
72 static TCGv_i32 cpu_access_type;
73
74 #include "gen-icount.h"
75
76 void ppc_translate_init(void)
77 {
78     int i;
79     char* p;
80     static int done_init = 0;
81
82     if (done_init)
83         return;
84
85     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
86
87     p = cpu_reg_names;
88
89     for (i = 0; i < 8; i++) {
90         sprintf(p, "crf%d", i);
91         cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
92                                             offsetof(CPUState, crf[i]), p);
93         p += 5;
94     }
95
96     for (i = 0; i < 32; i++) {
97         sprintf(p, "r%d", i);
98         cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
99                                         offsetof(CPUState, gpr[i]), p);
100         p += (i < 10) ? 3 : 4;
101 #if !defined(TARGET_PPC64)
102         sprintf(p, "r%dH", i);
103         cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
104                                              offsetof(CPUState, gprh[i]), p);
105         p += (i < 10) ? 4 : 5;
106 #endif
107
108         sprintf(p, "fp%d", i);
109         cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
110                                             offsetof(CPUState, fpr[i]), p);
111         p += (i < 10) ? 4 : 5;
112
113         sprintf(p, "avr%dH", i);
114 #ifdef WORDS_BIGENDIAN
115         cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
116                                              offsetof(CPUState, avr[i].u64[0]), p);
117 #else
118         cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
119                                              offsetof(CPUState, avr[i].u64[1]), p);
120 #endif
121         p += (i < 10) ? 6 : 7;
122
123         sprintf(p, "avr%dL", i);
124 #ifdef WORDS_BIGENDIAN
125         cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
126                                              offsetof(CPUState, avr[i].u64[1]), p);
127 #else
128         cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
129                                              offsetof(CPUState, avr[i].u64[0]), p);
130 #endif
131         p += (i < 10) ? 6 : 7;
132     }
133
134     cpu_nip = tcg_global_mem_new(TCG_AREG0,
135                                  offsetof(CPUState, nip), "nip");
136
137     cpu_msr = tcg_global_mem_new(TCG_AREG0,
138                                  offsetof(CPUState, msr), "msr");
139
140     cpu_ctr = tcg_global_mem_new(TCG_AREG0,
141                                  offsetof(CPUState, ctr), "ctr");
142
143     cpu_lr = tcg_global_mem_new(TCG_AREG0,
144                                 offsetof(CPUState, lr), "lr");
145
146     cpu_xer = tcg_global_mem_new(TCG_AREG0,
147                                  offsetof(CPUState, xer), "xer");
148
149     cpu_reserve = tcg_global_mem_new(TCG_AREG0,
150                                      offsetof(CPUState, reserve), "reserve");
151
152     cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
153                                        offsetof(CPUState, fpscr), "fpscr");
154
155     cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
156                                              offsetof(CPUState, access_type), "access_type");
157
158     /* register helpers */
159 #define GEN_HELPER 2
160 #include "helper.h"
161
162     done_init = 1;
163 }
164
165 #if defined(OPTIMIZE_FPRF_UPDATE)
166 static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
167 static uint16_t **gen_fprf_ptr;
168 #endif
169
170 /* internal defines */
171 typedef struct DisasContext {
172     struct TranslationBlock *tb;
173     target_ulong nip;
174     uint32_t opcode;
175     uint32_t exception;
176     /* Routine used to access memory */
177     int mem_idx;
178     int access_type;
179     /* Translation flags */
180     int le_mode;
181 #if defined(TARGET_PPC64)
182     int sf_mode;
183 #endif
184     int fpu_enabled;
185     int altivec_enabled;
186     int spe_enabled;
187     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
188     int singlestep_enabled;
189 } DisasContext;
190
191 struct opc_handler_t {
192     /* invalid bits */
193     uint32_t inval;
194     /* instruction type */
195     uint64_t type;
196     /* handler */
197     void (*handler)(DisasContext *ctx);
198 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
199     const char *oname;
200 #endif
201 #if defined(DO_PPC_STATISTICS)
202     uint64_t count;
203 #endif
204 };
205
206 static always_inline void gen_reset_fpstatus (void)
207 {
208 #ifdef CONFIG_SOFTFLOAT
209     gen_helper_reset_fpstatus();
210 #endif
211 }
212
213 static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
214 {
215     TCGv_i32 t0 = tcg_temp_new_i32();
216
217     if (set_fprf != 0) {
218         /* This case might be optimized later */
219 #if defined(OPTIMIZE_FPRF_UPDATE)
220         *gen_fprf_ptr++ = gen_opc_ptr;
221 #endif
222         tcg_gen_movi_i32(t0, 1);
223         gen_helper_compute_fprf(t0, arg, t0);
224         if (unlikely(set_rc)) {
225             tcg_gen_mov_i32(cpu_crf[1], t0);
226         }
227         gen_helper_float_check_status();
228     } else if (unlikely(set_rc)) {
229         /* We always need to compute fpcc */
230         tcg_gen_movi_i32(t0, 0);
231         gen_helper_compute_fprf(t0, arg, t0);
232         tcg_gen_mov_i32(cpu_crf[1], t0);
233     }
234
235     tcg_temp_free_i32(t0);
236 }
237
238 static always_inline void gen_optimize_fprf (void)
239 {
240 #if defined(OPTIMIZE_FPRF_UPDATE)
241     uint16_t **ptr;
242
243     for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
244         *ptr = INDEX_op_nop1;
245     gen_fprf_ptr = gen_fprf_buf;
246 #endif
247 }
248
249 static always_inline void gen_set_access_type (DisasContext *ctx, int access_type)
250 {
251     if (ctx->access_type != access_type) {
252         tcg_gen_movi_i32(cpu_access_type, access_type);
253         ctx->access_type = access_type;
254     }
255 }
256
257 static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
258 {
259 #if defined(TARGET_PPC64)
260     if (ctx->sf_mode)
261         tcg_gen_movi_tl(cpu_nip, nip);
262     else
263 #endif
264         tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
265 }
266
267 static always_inline void gen_exception_err (DisasContext *ctx, uint32_t excp, uint32_t error)
268 {
269     TCGv_i32 t0, t1;
270     if (ctx->exception == POWERPC_EXCP_NONE) {
271         gen_update_nip(ctx, ctx->nip);
272     }
273     t0 = tcg_const_i32(excp);
274     t1 = tcg_const_i32(error);
275     gen_helper_raise_exception_err(t0, t1);
276     tcg_temp_free_i32(t0);
277     tcg_temp_free_i32(t1);
278     ctx->exception = (excp);
279 }
280
281 static always_inline void gen_exception (DisasContext *ctx, uint32_t excp)
282 {
283     TCGv_i32 t0;
284     if (ctx->exception == POWERPC_EXCP_NONE) {
285         gen_update_nip(ctx, ctx->nip);
286     }
287     t0 = tcg_const_i32(excp);
288     gen_helper_raise_exception(t0);
289     tcg_temp_free_i32(t0);
290     ctx->exception = (excp);
291 }
292
293 static always_inline void gen_debug_exception (DisasContext *ctx)
294 {
295     TCGv_i32 t0;
296     gen_update_nip(ctx, ctx->nip);
297     t0 = tcg_const_i32(EXCP_DEBUG);
298     gen_helper_raise_exception(t0);
299     tcg_temp_free_i32(t0);
300 }
301
302 static always_inline void gen_inval_exception (DisasContext *ctx, uint32_t error)
303 {
304     gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_INVAL | error);
305 }
306
307 /* Stop translation */
308 static always_inline void gen_stop_exception (DisasContext *ctx)
309 {
310     gen_update_nip(ctx, ctx->nip);
311     ctx->exception = POWERPC_EXCP_STOP;
312 }
313
314 /* No need to update nip here, as execution flow will change */
315 static always_inline void gen_sync_exception (DisasContext *ctx)
316 {
317     ctx->exception = POWERPC_EXCP_SYNC;
318 }
319
320 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
321 static void gen_##name (DisasContext *ctx);                                   \
322 GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
323 static void gen_##name (DisasContext *ctx)
324
325 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
326 static void gen_##name (DisasContext *ctx);                                   \
327 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
328 static void gen_##name (DisasContext *ctx)
329
330 typedef struct opcode_t {
331     unsigned char opc1, opc2, opc3;
332 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
333     unsigned char pad[5];
334 #else
335     unsigned char pad[1];
336 #endif
337     opc_handler_t handler;
338     const char *oname;
339 } opcode_t;
340
341 /*****************************************************************************/
342 /***                           Instruction decoding                        ***/
343 #define EXTRACT_HELPER(name, shift, nb)                                       \
344 static always_inline uint32_t name (uint32_t opcode)                          \
345 {                                                                             \
346     return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
347 }
348
349 #define EXTRACT_SHELPER(name, shift, nb)                                      \
350 static always_inline int32_t name (uint32_t opcode)                           \
351 {                                                                             \
352     return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
353 }
354
355 /* Opcode part 1 */
356 EXTRACT_HELPER(opc1, 26, 6);
357 /* Opcode part 2 */
358 EXTRACT_HELPER(opc2, 1, 5);
359 /* Opcode part 3 */
360 EXTRACT_HELPER(opc3, 6, 5);
361 /* Update Cr0 flags */
362 EXTRACT_HELPER(Rc, 0, 1);
363 /* Destination */
364 EXTRACT_HELPER(rD, 21, 5);
365 /* Source */
366 EXTRACT_HELPER(rS, 21, 5);
367 /* First operand */
368 EXTRACT_HELPER(rA, 16, 5);
369 /* Second operand */
370 EXTRACT_HELPER(rB, 11, 5);
371 /* Third operand */
372 EXTRACT_HELPER(rC, 6, 5);
373 /***                               Get CRn                                 ***/
374 EXTRACT_HELPER(crfD, 23, 3);
375 EXTRACT_HELPER(crfS, 18, 3);
376 EXTRACT_HELPER(crbD, 21, 5);
377 EXTRACT_HELPER(crbA, 16, 5);
378 EXTRACT_HELPER(crbB, 11, 5);
379 /* SPR / TBL */
380 EXTRACT_HELPER(_SPR, 11, 10);
381 static always_inline uint32_t SPR (uint32_t opcode)
382 {
383     uint32_t sprn = _SPR(opcode);
384
385     return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
386 }
387 /***                              Get constants                            ***/
388 EXTRACT_HELPER(IMM, 12, 8);
389 /* 16 bits signed immediate value */
390 EXTRACT_SHELPER(SIMM, 0, 16);
391 /* 16 bits unsigned immediate value */
392 EXTRACT_HELPER(UIMM, 0, 16);
393 /* Bit count */
394 EXTRACT_HELPER(NB, 11, 5);
395 /* Shift count */
396 EXTRACT_HELPER(SH, 11, 5);
397 /* Mask start */
398 EXTRACT_HELPER(MB, 6, 5);
399 /* Mask end */
400 EXTRACT_HELPER(ME, 1, 5);
401 /* Trap operand */
402 EXTRACT_HELPER(TO, 21, 5);
403
404 EXTRACT_HELPER(CRM, 12, 8);
405 EXTRACT_HELPER(FM, 17, 8);
406 EXTRACT_HELPER(SR, 16, 4);
407 EXTRACT_HELPER(FPIMM, 12, 4);
408
409 /***                            Jump target decoding                       ***/
410 /* Displacement */
411 EXTRACT_SHELPER(d, 0, 16);
412 /* Immediate address */
413 static always_inline target_ulong LI (uint32_t opcode)
414 {
415     return (opcode >> 0) & 0x03FFFFFC;
416 }
417
418 static always_inline uint32_t BD (uint32_t opcode)
419 {
420     return (opcode >> 0) & 0xFFFC;
421 }
422
423 EXTRACT_HELPER(BO, 21, 5);
424 EXTRACT_HELPER(BI, 16, 5);
425 /* Absolute/relative address */
426 EXTRACT_HELPER(AA, 1, 1);
427 /* Link */
428 EXTRACT_HELPER(LK, 0, 1);
429
430 /* Create a mask between <start> and <end> bits */
431 static always_inline target_ulong MASK (uint32_t start, uint32_t end)
432 {
433     target_ulong ret;
434
435 #if defined(TARGET_PPC64)
436     if (likely(start == 0)) {
437         ret = UINT64_MAX << (63 - end);
438     } else if (likely(end == 63)) {
439         ret = UINT64_MAX >> start;
440     }
441 #else
442     if (likely(start == 0)) {
443         ret = UINT32_MAX << (31  - end);
444     } else if (likely(end == 31)) {
445         ret = UINT32_MAX >> start;
446     }
447 #endif
448     else {
449         ret = (((target_ulong)(-1ULL)) >> (start)) ^
450             (((target_ulong)(-1ULL) >> (end)) >> 1);
451         if (unlikely(start > end))
452             return ~ret;
453     }
454
455     return ret;
456 }
457
458 /*****************************************************************************/
459 /* PowerPC Instructions types definitions                                    */
460 enum {
461     PPC_NONE           = 0x0000000000000000ULL,
462     /* PowerPC base instructions set                                         */
463     PPC_INSNS_BASE     = 0x0000000000000001ULL,
464     /*   integer operations instructions                                     */
465 #define PPC_INTEGER PPC_INSNS_BASE
466     /*   flow control instructions                                           */
467 #define PPC_FLOW    PPC_INSNS_BASE
468     /*   virtual memory instructions                                         */
469 #define PPC_MEM     PPC_INSNS_BASE
470     /*   ld/st with reservation instructions                                 */
471 #define PPC_RES     PPC_INSNS_BASE
472     /*   spr/msr access instructions                                         */
473 #define PPC_MISC    PPC_INSNS_BASE
474     /* Deprecated instruction sets                                           */
475     /*   Original POWER instruction set                                      */
476     PPC_POWER          = 0x0000000000000002ULL,
477     /*   POWER2 instruction set extension                                    */
478     PPC_POWER2         = 0x0000000000000004ULL,
479     /*   Power RTC support                                                   */
480     PPC_POWER_RTC      = 0x0000000000000008ULL,
481     /*   Power-to-PowerPC bridge (601)                                       */
482     PPC_POWER_BR       = 0x0000000000000010ULL,
483     /* 64 bits PowerPC instruction set                                       */
484     PPC_64B            = 0x0000000000000020ULL,
485     /*   New 64 bits extensions (PowerPC 2.0x)                               */
486     PPC_64BX           = 0x0000000000000040ULL,
487     /*   64 bits hypervisor extensions                                       */
488     PPC_64H            = 0x0000000000000080ULL,
489     /*   New wait instruction (PowerPC 2.0x)                                 */
490     PPC_WAIT           = 0x0000000000000100ULL,
491     /*   Time base mftb instruction                                          */
492     PPC_MFTB           = 0x0000000000000200ULL,
493
494     /* Fixed-point unit extensions                                           */
495     /*   PowerPC 602 specific                                                */
496     PPC_602_SPEC       = 0x0000000000000400ULL,
497     /*   isel instruction                                                    */
498     PPC_ISEL           = 0x0000000000000800ULL,
499     /*   popcntb instruction                                                 */
500     PPC_POPCNTB        = 0x0000000000001000ULL,
501     /*   string load / store                                                 */
502     PPC_STRING         = 0x0000000000002000ULL,
503
504     /* Floating-point unit extensions                                        */
505     /*   Optional floating point instructions                                */
506     PPC_FLOAT          = 0x0000000000010000ULL,
507     /* New floating-point extensions (PowerPC 2.0x)                          */
508     PPC_FLOAT_EXT      = 0x0000000000020000ULL,
509     PPC_FLOAT_FSQRT    = 0x0000000000040000ULL,
510     PPC_FLOAT_FRES     = 0x0000000000080000ULL,
511     PPC_FLOAT_FRSQRTE  = 0x0000000000100000ULL,
512     PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
513     PPC_FLOAT_FSEL     = 0x0000000000400000ULL,
514     PPC_FLOAT_STFIWX   = 0x0000000000800000ULL,
515
516     /* Vector/SIMD extensions                                                */
517     /*   Altivec support                                                     */
518     PPC_ALTIVEC        = 0x0000000001000000ULL,
519     /*   PowerPC 2.03 SPE extension                                          */
520     PPC_SPE            = 0x0000000002000000ULL,
521     /*   PowerPC 2.03 SPE floating-point extension                           */
522     PPC_SPEFPU         = 0x0000000004000000ULL,
523
524     /* Optional memory control instructions                                  */
525     PPC_MEM_TLBIA      = 0x0000000010000000ULL,
526     PPC_MEM_TLBIE      = 0x0000000020000000ULL,
527     PPC_MEM_TLBSYNC    = 0x0000000040000000ULL,
528     /*   sync instruction                                                    */
529     PPC_MEM_SYNC       = 0x0000000080000000ULL,
530     /*   eieio instruction                                                   */
531     PPC_MEM_EIEIO      = 0x0000000100000000ULL,
532
533     /* Cache control instructions                                            */
534     PPC_CACHE          = 0x0000000200000000ULL,
535     /*   icbi instruction                                                    */
536     PPC_CACHE_ICBI     = 0x0000000400000000ULL,
537     /*   dcbz instruction with fixed cache line size                         */
538     PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
539     /*   dcbz instruction with tunable cache line size                       */
540     PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
541     /*   dcba instruction                                                    */
542     PPC_CACHE_DCBA     = 0x0000002000000000ULL,
543     /*   Freescale cache locking instructions                                */
544     PPC_CACHE_LOCK     = 0x0000004000000000ULL,
545
546     /* MMU related extensions                                                */
547     /*   external control instructions                                       */
548     PPC_EXTERN         = 0x0000010000000000ULL,
549     /*   segment register access instructions                                */
550     PPC_SEGMENT        = 0x0000020000000000ULL,
551     /*   PowerPC 6xx TLB management instructions                             */
552     PPC_6xx_TLB        = 0x0000040000000000ULL,
553     /* PowerPC 74xx TLB management instructions                              */
554     PPC_74xx_TLB       = 0x0000080000000000ULL,
555     /*   PowerPC 40x TLB management instructions                             */
556     PPC_40x_TLB        = 0x0000100000000000ULL,
557     /*   segment register access instructions for PowerPC 64 "bridge"        */
558     PPC_SEGMENT_64B    = 0x0000200000000000ULL,
559     /*   SLB management                                                      */
560     PPC_SLBI           = 0x0000400000000000ULL,
561
562     /* Embedded PowerPC dedicated instructions                               */
563     PPC_WRTEE          = 0x0001000000000000ULL,
564     /* PowerPC 40x exception model                                           */
565     PPC_40x_EXCP       = 0x0002000000000000ULL,
566     /* PowerPC 405 Mac instructions                                          */
567     PPC_405_MAC        = 0x0004000000000000ULL,
568     /* PowerPC 440 specific instructions                                     */
569     PPC_440_SPEC       = 0x0008000000000000ULL,
570     /* BookE (embedded) PowerPC specification                                */
571     PPC_BOOKE          = 0x0010000000000000ULL,
572     /* mfapidi instruction                                                   */
573     PPC_MFAPIDI        = 0x0020000000000000ULL,
574     /* tlbiva instruction                                                    */
575     PPC_TLBIVA         = 0x0040000000000000ULL,
576     /* tlbivax instruction                                                   */
577     PPC_TLBIVAX        = 0x0080000000000000ULL,
578     /* PowerPC 4xx dedicated instructions                                    */
579     PPC_4xx_COMMON     = 0x0100000000000000ULL,
580     /* PowerPC 40x ibct instructions                                         */
581     PPC_40x_ICBT       = 0x0200000000000000ULL,
582     /* rfmci is not implemented in all BookE PowerPC                         */
583     PPC_RFMCI          = 0x0400000000000000ULL,
584     /* rfdi instruction                                                      */
585     PPC_RFDI           = 0x0800000000000000ULL,
586     /* DCR accesses                                                          */
587     PPC_DCR            = 0x1000000000000000ULL,
588     /* DCR extended accesse                                                  */
589     PPC_DCRX           = 0x2000000000000000ULL,
590     /* user-mode DCR access, implemented in PowerPC 460                      */
591     PPC_DCRUX          = 0x4000000000000000ULL,
592 };
593
594 /*****************************************************************************/
595 /* PowerPC instructions table                                                */
596 #if HOST_LONG_BITS == 64
597 #define OPC_ALIGN 8
598 #else
599 #define OPC_ALIGN 4
600 #endif
601 #if defined(__APPLE__)
602 #define OPCODES_SECTION                                                       \
603     __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
604 #else
605 #define OPCODES_SECTION                                                       \
606     __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
607 #endif
608
609 #if defined(DO_PPC_STATISTICS)
610 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
611 OPCODES_SECTION opcode_t opc_##name = {                                       \
612     .opc1 = op1,                                                              \
613     .opc2 = op2,                                                              \
614     .opc3 = op3,                                                              \
615     .pad  = { 0, },                                                           \
616     .handler = {                                                              \
617         .inval   = invl,                                                      \
618         .type = _typ,                                                         \
619         .handler = &gen_##name,                                               \
620         .oname = stringify(name),                                             \
621     },                                                                        \
622     .oname = stringify(name),                                                 \
623 }
624 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
625 OPCODES_SECTION opcode_t opc_##name = {                                       \
626     .opc1 = op1,                                                              \
627     .opc2 = op2,                                                              \
628     .opc3 = op3,                                                              \
629     .pad  = { 0, },                                                           \
630     .handler = {                                                              \
631         .inval   = invl,                                                      \
632         .type = _typ,                                                         \
633         .handler = &gen_##name,                                               \
634         .oname = onam,                                                        \
635     },                                                                        \
636     .oname = onam,                                                            \
637 }
638 #else
639 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
640 OPCODES_SECTION opcode_t opc_##name = {                                       \
641     .opc1 = op1,                                                              \
642     .opc2 = op2,                                                              \
643     .opc3 = op3,                                                              \
644     .pad  = { 0, },                                                           \
645     .handler = {                                                              \
646         .inval   = invl,                                                      \
647         .type = _typ,                                                         \
648         .handler = &gen_##name,                                               \
649     },                                                                        \
650     .oname = stringify(name),                                                 \
651 }
652 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
653 OPCODES_SECTION opcode_t opc_##name = {                                       \
654     .opc1 = op1,                                                              \
655     .opc2 = op2,                                                              \
656     .opc3 = op3,                                                              \
657     .pad  = { 0, },                                                           \
658     .handler = {                                                              \
659         .inval   = invl,                                                      \
660         .type = _typ,                                                         \
661         .handler = &gen_##name,                                               \
662     },                                                                        \
663     .oname = onam,                                                            \
664 }
665 #endif
666
667 #define GEN_OPCODE_MARK(name)                                                 \
668 OPCODES_SECTION opcode_t opc_##name = {                                       \
669     .opc1 = 0xFF,                                                             \
670     .opc2 = 0xFF,                                                             \
671     .opc3 = 0xFF,                                                             \
672     .pad  = { 0, },                                                           \
673     .handler = {                                                              \
674         .inval   = 0x00000000,                                                \
675         .type = 0x00,                                                         \
676         .handler = NULL,                                                      \
677     },                                                                        \
678     .oname = stringify(name),                                                 \
679 }
680
681 /* SPR load/store helpers */
682 static always_inline void gen_load_spr(TCGv t, int reg)
683 {
684     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
685 }
686
687 static always_inline void gen_store_spr(int reg, TCGv t)
688 {
689     tcg_gen_st_tl(t, cpu_env, offsetof(CPUState, spr[reg]));
690 }
691
692 /* Start opcode list */
693 GEN_OPCODE_MARK(start);
694
695 /* Invalid instruction */
696 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
697 {
698     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
699 }
700
701 static opc_handler_t invalid_handler = {
702     .inval   = 0xFFFFFFFF,
703     .type    = PPC_NONE,
704     .handler = gen_invalid,
705 };
706
707 /***                           Integer comparison                          ***/
708
709 static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
710 {
711     int l1, l2, l3;
712
713     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
714     tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
715     tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
716
717     l1 = gen_new_label();
718     l2 = gen_new_label();
719     l3 = gen_new_label();
720     if (s) {
721         tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
722         tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
723     } else {
724         tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
725         tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
726     }
727     tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
728     tcg_gen_br(l3);
729     gen_set_label(l1);
730     tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
731     tcg_gen_br(l3);
732     gen_set_label(l2);
733     tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
734     gen_set_label(l3);
735 }
736
737 static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
738 {
739     TCGv t0 = tcg_const_local_tl(arg1);
740     gen_op_cmp(arg0, t0, s, crf);
741     tcg_temp_free(t0);
742 }
743
744 #if defined(TARGET_PPC64)
745 static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
746 {
747     TCGv t0, t1;
748     t0 = tcg_temp_local_new();
749     t1 = tcg_temp_local_new();
750     if (s) {
751         tcg_gen_ext32s_tl(t0, arg0);
752         tcg_gen_ext32s_tl(t1, arg1);
753     } else {
754         tcg_gen_ext32u_tl(t0, arg0);
755         tcg_gen_ext32u_tl(t1, arg1);
756     }
757     gen_op_cmp(t0, t1, s, crf);
758     tcg_temp_free(t1);
759     tcg_temp_free(t0);
760 }
761
762 static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
763 {
764     TCGv t0 = tcg_const_local_tl(arg1);
765     gen_op_cmp32(arg0, t0, s, crf);
766     tcg_temp_free(t0);
767 }
768 #endif
769
770 static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
771 {
772 #if defined(TARGET_PPC64)
773     if (!(ctx->sf_mode))
774         gen_op_cmpi32(reg, 0, 1, 0);
775     else
776 #endif
777         gen_op_cmpi(reg, 0, 1, 0);
778 }
779
780 /* cmp */
781 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
782 {
783 #if defined(TARGET_PPC64)
784     if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
785         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
786                      1, crfD(ctx->opcode));
787     else
788 #endif
789         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
790                    1, crfD(ctx->opcode));
791 }
792
793 /* cmpi */
794 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
795 {
796 #if defined(TARGET_PPC64)
797     if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
798         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
799                       1, crfD(ctx->opcode));
800     else
801 #endif
802         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
803                     1, crfD(ctx->opcode));
804 }
805
806 /* cmpl */
807 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
808 {
809 #if defined(TARGET_PPC64)
810     if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
811         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
812                      0, crfD(ctx->opcode));
813     else
814 #endif
815         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
816                    0, crfD(ctx->opcode));
817 }
818
819 /* cmpli */
820 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
821 {
822 #if defined(TARGET_PPC64)
823     if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
824         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
825                       0, crfD(ctx->opcode));
826     else
827 #endif
828         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
829                     0, crfD(ctx->opcode));
830 }
831
832 /* isel (PowerPC 2.03 specification) */
833 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
834 {
835     int l1, l2;
836     uint32_t bi = rC(ctx->opcode);
837     uint32_t mask;
838     TCGv_i32 t0;
839
840     l1 = gen_new_label();
841     l2 = gen_new_label();
842
843     mask = 1 << (3 - (bi & 0x03));
844     t0 = tcg_temp_new_i32();
845     tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
846     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
847     if (rA(ctx->opcode) == 0)
848         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
849     else
850         tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
851     tcg_gen_br(l2);
852     gen_set_label(l1);
853     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
854     gen_set_label(l2);
855     tcg_temp_free_i32(t0);
856 }
857
858 /***                           Integer arithmetic                          ***/
859
860 static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
861 {
862     int l1;
863     TCGv t0;
864
865     l1 = gen_new_label();
866     /* Start with XER OV disabled, the most likely case */
867     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
868     t0 = tcg_temp_local_new();
869     tcg_gen_xor_tl(t0, arg0, arg1);
870 #if defined(TARGET_PPC64)
871     if (!ctx->sf_mode)
872         tcg_gen_ext32s_tl(t0, t0);
873 #endif
874     if (sub)
875         tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
876     else
877         tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
878     tcg_gen_xor_tl(t0, arg1, arg2);
879 #if defined(TARGET_PPC64)
880     if (!ctx->sf_mode)
881         tcg_gen_ext32s_tl(t0, t0);
882 #endif
883     if (sub)
884         tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
885     else
886         tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
887     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
888     gen_set_label(l1);
889     tcg_temp_free(t0);
890 }
891
892 static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
893 {
894     int l1 = gen_new_label();
895
896 #if defined(TARGET_PPC64)
897     if (!(ctx->sf_mode)) {
898         TCGv t0, t1;
899         t0 = tcg_temp_new();
900         t1 = tcg_temp_new();
901
902         tcg_gen_ext32u_tl(t0, arg1);
903         tcg_gen_ext32u_tl(t1, arg2);
904         if (sub) {
905             tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
906         } else {
907             tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
908         }
909         tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
910         gen_set_label(l1);
911         tcg_temp_free(t0);
912         tcg_temp_free(t1);
913     } else
914 #endif
915     {
916         if (sub) {
917             tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
918         } else {
919             tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
920         }
921         tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
922         gen_set_label(l1);
923     }
924 }
925
926 /* Common add function */
927 static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
928                                            int add_ca, int compute_ca, int compute_ov)
929 {
930     TCGv t0, t1;
931
932     if ((!compute_ca && !compute_ov) ||
933         (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
934         t0 = ret;
935     } else {
936         t0 = tcg_temp_local_new();
937     }
938
939     if (add_ca) {
940         t1 = tcg_temp_local_new();
941         tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
942         tcg_gen_shri_tl(t1, t1, XER_CA);
943     }
944
945     if (compute_ca && compute_ov) {
946         /* Start with XER CA and OV disabled, the most likely case */
947         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
948     } else if (compute_ca) {
949         /* Start with XER CA disabled, the most likely case */
950         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
951     } else if (compute_ov) {
952         /* Start with XER OV disabled, the most likely case */
953         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
954     }
955
956     tcg_gen_add_tl(t0, arg1, arg2);
957
958     if (compute_ca) {
959         gen_op_arith_compute_ca(ctx, t0, arg1, 0);
960     }
961     if (add_ca) {
962         tcg_gen_add_tl(t0, t0, t1);
963         gen_op_arith_compute_ca(ctx, t0, t1, 0);
964         tcg_temp_free(t1);
965     }
966     if (compute_ov) {
967         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
968     }
969
970     if (unlikely(Rc(ctx->opcode) != 0))
971         gen_set_Rc0(ctx, t0);
972
973     if (!TCGV_EQUAL(t0, ret)) {
974         tcg_gen_mov_tl(ret, t0);
975         tcg_temp_free(t0);
976     }
977 }
978 /* Add functions with two operands */
979 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
980 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER)                  \
981 {                                                                             \
982     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
983                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
984                      add_ca, compute_ca, compute_ov);                         \
985 }
986 /* Add functions with one operand and one immediate */
987 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
988                                 add_ca, compute_ca, compute_ov)               \
989 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER)                  \
990 {                                                                             \
991     TCGv t0 = tcg_const_local_tl(const_val);                                  \
992     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
993                      cpu_gpr[rA(ctx->opcode)], t0,                            \
994                      add_ca, compute_ca, compute_ov);                         \
995     tcg_temp_free(t0);                                                        \
996 }
997
998 /* add  add.  addo  addo. */
999 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
1000 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
1001 /* addc  addc.  addco  addco. */
1002 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
1003 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
1004 /* adde  adde.  addeo  addeo. */
1005 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1006 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1007 /* addme  addme.  addmeo  addmeo.  */
1008 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1009 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1010 /* addze  addze.  addzeo  addzeo.*/
1011 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1012 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1013 /* addi */
1014 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1015 {
1016     target_long simm = SIMM(ctx->opcode);
1017
1018     if (rA(ctx->opcode) == 0) {
1019         /* li case */
1020         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1021     } else {
1022         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1023     }
1024 }
1025 /* addic  addic.*/
1026 static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1027                                         int compute_Rc0)
1028 {
1029     target_long simm = SIMM(ctx->opcode);
1030
1031     /* Start with XER CA and OV disabled, the most likely case */
1032     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1033
1034     if (likely(simm != 0)) {
1035         TCGv t0 = tcg_temp_local_new();
1036         tcg_gen_addi_tl(t0, arg1, simm);
1037         gen_op_arith_compute_ca(ctx, t0, arg1, 0);
1038         tcg_gen_mov_tl(ret, t0);
1039         tcg_temp_free(t0);
1040     } else {
1041         tcg_gen_mov_tl(ret, arg1);
1042     }
1043     if (compute_Rc0) {
1044         gen_set_Rc0(ctx, ret);
1045     }
1046 }
1047 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1048 {
1049     gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1050 }
1051 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1052 {
1053     gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1054 }
1055 /* addis */
1056 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1057 {
1058     target_long simm = SIMM(ctx->opcode);
1059
1060     if (rA(ctx->opcode) == 0) {
1061         /* lis case */
1062         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1063     } else {
1064         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1065     }
1066 }
1067
1068 static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1069                                              int sign, int compute_ov)
1070 {
1071     int l1 = gen_new_label();
1072     int l2 = gen_new_label();
1073     TCGv_i32 t0 = tcg_temp_local_new_i32();
1074     TCGv_i32 t1 = tcg_temp_local_new_i32();
1075
1076     tcg_gen_trunc_tl_i32(t0, arg1);
1077     tcg_gen_trunc_tl_i32(t1, arg2);
1078     tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1079     if (sign) {
1080         int l3 = gen_new_label();
1081         tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1082         tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1083         gen_set_label(l3);
1084         tcg_gen_div_i32(t0, t0, t1);
1085     } else {
1086         tcg_gen_divu_i32(t0, t0, t1);
1087     }
1088     if (compute_ov) {
1089         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1090     }
1091     tcg_gen_br(l2);
1092     gen_set_label(l1);
1093     if (sign) {
1094         tcg_gen_sari_i32(t0, t0, 31);
1095     } else {
1096         tcg_gen_movi_i32(t0, 0);
1097     }
1098     if (compute_ov) {
1099         tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1100     }
1101     gen_set_label(l2);
1102     tcg_gen_extu_i32_tl(ret, t0);
1103     tcg_temp_free_i32(t0);
1104     tcg_temp_free_i32(t1);
1105     if (unlikely(Rc(ctx->opcode) != 0))
1106         gen_set_Rc0(ctx, ret);
1107 }
1108 /* Div functions */
1109 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
1110 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)                  \
1111 {                                                                             \
1112     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1113                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1114                      sign, compute_ov);                                       \
1115 }
1116 /* divwu  divwu.  divwuo  divwuo.   */
1117 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1118 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1119 /* divw  divw.  divwo  divwo.   */
1120 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1121 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1122 #if defined(TARGET_PPC64)
1123 static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1124                                              int sign, int compute_ov)
1125 {
1126     int l1 = gen_new_label();
1127     int l2 = gen_new_label();
1128
1129     tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1130     if (sign) {
1131         int l3 = gen_new_label();
1132         tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1133         tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1134         gen_set_label(l3);
1135         tcg_gen_div_i64(ret, arg1, arg2);
1136     } else {
1137         tcg_gen_divu_i64(ret, arg1, arg2);
1138     }
1139     if (compute_ov) {
1140         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1141     }
1142     tcg_gen_br(l2);
1143     gen_set_label(l1);
1144     if (sign) {
1145         tcg_gen_sari_i64(ret, arg1, 63);
1146     } else {
1147         tcg_gen_movi_i64(ret, 0);
1148     }
1149     if (compute_ov) {
1150         tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1151     }
1152     gen_set_label(l2);
1153     if (unlikely(Rc(ctx->opcode) != 0))
1154         gen_set_Rc0(ctx, ret);
1155 }
1156 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1157 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
1158 {                                                                             \
1159     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1160                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1161                       sign, compute_ov);                                      \
1162 }
1163 /* divwu  divwu.  divwuo  divwuo.   */
1164 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1165 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1166 /* divw  divw.  divwo  divwo.   */
1167 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1168 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1169 #endif
1170
1171 /* mulhw  mulhw. */
1172 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1173 {
1174     TCGv_i64 t0, t1;
1175
1176     t0 = tcg_temp_new_i64();
1177     t1 = tcg_temp_new_i64();
1178 #if defined(TARGET_PPC64)
1179     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1180     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1181     tcg_gen_mul_i64(t0, t0, t1);
1182     tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1183 #else
1184     tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1185     tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1186     tcg_gen_mul_i64(t0, t0, t1);
1187     tcg_gen_shri_i64(t0, t0, 32);
1188     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1189 #endif
1190     tcg_temp_free_i64(t0);
1191     tcg_temp_free_i64(t1);
1192     if (unlikely(Rc(ctx->opcode) != 0))
1193         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1194 }
1195 /* mulhwu  mulhwu.  */
1196 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1197 {
1198     TCGv_i64 t0, t1;
1199
1200     t0 = tcg_temp_new_i64();
1201     t1 = tcg_temp_new_i64();
1202 #if defined(TARGET_PPC64)
1203     tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1204     tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1205     tcg_gen_mul_i64(t0, t0, t1);
1206     tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1207 #else
1208     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1209     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1210     tcg_gen_mul_i64(t0, t0, t1);
1211     tcg_gen_shri_i64(t0, t0, 32);
1212     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1213 #endif
1214     tcg_temp_free_i64(t0);
1215     tcg_temp_free_i64(t1);
1216     if (unlikely(Rc(ctx->opcode) != 0))
1217         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1218 }
1219 /* mullw  mullw. */
1220 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1221 {
1222     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1223                    cpu_gpr[rB(ctx->opcode)]);
1224     tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1225     if (unlikely(Rc(ctx->opcode) != 0))
1226         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1227 }
1228 /* mullwo  mullwo. */
1229 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1230 {
1231     int l1;
1232     TCGv_i64 t0, t1;
1233
1234     t0 = tcg_temp_new_i64();
1235     t1 = tcg_temp_new_i64();
1236     l1 = gen_new_label();
1237     /* Start with XER OV disabled, the most likely case */
1238     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1239 #if defined(TARGET_PPC64)
1240     tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1241     tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1242 #else
1243     tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1244     tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1245 #endif
1246     tcg_gen_mul_i64(t0, t0, t1);
1247 #if defined(TARGET_PPC64)
1248     tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
1249     tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
1250 #else
1251     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1252     tcg_gen_ext32s_i64(t1, t0);
1253     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
1254 #endif
1255     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1256     gen_set_label(l1);
1257     tcg_temp_free_i64(t0);
1258     tcg_temp_free_i64(t1);
1259     if (unlikely(Rc(ctx->opcode) != 0))
1260         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1261 }
1262 /* mulli */
1263 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1264 {
1265     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1266                     SIMM(ctx->opcode));
1267 }
1268 #if defined(TARGET_PPC64)
1269 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
1270 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
1271 {                                                                             \
1272     gen_helper_##name (cpu_gpr[rD(ctx->opcode)],                              \
1273                        cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);   \
1274     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1275         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1276 }
1277 /* mulhd  mulhd. */
1278 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
1279 /* mulhdu  mulhdu. */
1280 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
1281 /* mulld  mulld. */
1282 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
1283 {
1284     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1285                    cpu_gpr[rB(ctx->opcode)]);
1286     if (unlikely(Rc(ctx->opcode) != 0))
1287         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1288 }
1289 /* mulldo  mulldo. */
1290 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1291 #endif
1292
1293 /* neg neg. nego nego. */
1294 static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1295 {
1296     int l1 = gen_new_label();
1297     int l2 = gen_new_label();
1298     TCGv t0 = tcg_temp_local_new();
1299 #if defined(TARGET_PPC64)
1300     if (ctx->sf_mode) {
1301         tcg_gen_mov_tl(t0, arg1);
1302         tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
1303     } else
1304 #endif
1305     {
1306         tcg_gen_ext32s_tl(t0, arg1);
1307         tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
1308     }
1309     tcg_gen_neg_tl(ret, arg1);
1310     if (ov_check) {
1311         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1312     }
1313     tcg_gen_br(l2);
1314     gen_set_label(l1);
1315     tcg_gen_mov_tl(ret, t0);
1316     if (ov_check) {
1317         tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1318     }
1319     gen_set_label(l2);
1320     tcg_temp_free(t0);
1321     if (unlikely(Rc(ctx->opcode) != 0))
1322         gen_set_Rc0(ctx, ret);
1323 }
1324 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1325 {
1326     gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1327 }
1328 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
1329 {
1330     gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1331 }
1332
1333 /* Common subf function */
1334 static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1335                                             int add_ca, int compute_ca, int compute_ov)
1336 {
1337     TCGv t0, t1;
1338
1339     if ((!compute_ca && !compute_ov) ||
1340         (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1341         t0 = ret;
1342     } else {
1343         t0 = tcg_temp_local_new();
1344     }
1345
1346     if (add_ca) {
1347         t1 = tcg_temp_local_new();
1348         tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1349         tcg_gen_shri_tl(t1, t1, XER_CA);
1350     }
1351
1352     if (compute_ca && compute_ov) {
1353         /* Start with XER CA and OV disabled, the most likely case */
1354         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1355     } else if (compute_ca) {
1356         /* Start with XER CA disabled, the most likely case */
1357         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1358     } else if (compute_ov) {
1359         /* Start with XER OV disabled, the most likely case */
1360         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1361     }
1362
1363     if (add_ca) {
1364         tcg_gen_not_tl(t0, arg1);
1365         tcg_gen_add_tl(t0, t0, arg2);
1366         gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1367         tcg_gen_add_tl(t0, t0, t1);
1368         gen_op_arith_compute_ca(ctx, t0, t1, 0);
1369         tcg_temp_free(t1);
1370     } else {
1371         tcg_gen_sub_tl(t0, arg2, arg1);
1372         if (compute_ca) {
1373             gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1374         }
1375     }
1376     if (compute_ov) {
1377         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1378     }
1379
1380     if (unlikely(Rc(ctx->opcode) != 0))
1381         gen_set_Rc0(ctx, t0);
1382
1383     if (!TCGV_EQUAL(t0, ret)) {
1384         tcg_gen_mov_tl(ret, t0);
1385         tcg_temp_free(t0);
1386     }
1387 }
1388 /* Sub functions with Two operands functions */
1389 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1390 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER)                  \
1391 {                                                                             \
1392     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1393                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1394                       add_ca, compute_ca, compute_ov);                        \
1395 }
1396 /* Sub functions with one operand and one immediate */
1397 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1398                                 add_ca, compute_ca, compute_ov)               \
1399 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER)                  \
1400 {                                                                             \
1401     TCGv t0 = tcg_const_local_tl(const_val);                                  \
1402     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1403                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1404                       add_ca, compute_ca, compute_ov);                        \
1405     tcg_temp_free(t0);                                                        \
1406 }
1407 /* subf  subf.  subfo  subfo. */
1408 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1409 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1410 /* subfc  subfc.  subfco  subfco. */
1411 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1412 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1413 /* subfe  subfe.  subfeo  subfo. */
1414 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1415 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1416 /* subfme  subfme.  subfmeo  subfmeo.  */
1417 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1418 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1419 /* subfze  subfze.  subfzeo  subfzeo.*/
1420 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1421 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1422 /* subfic */
1423 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1424 {
1425     /* Start with XER CA and OV disabled, the most likely case */
1426     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1427     TCGv t0 = tcg_temp_local_new();
1428     TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
1429     tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
1430     gen_op_arith_compute_ca(ctx, t0, t1, 1);
1431     tcg_temp_free(t1);
1432     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
1433     tcg_temp_free(t0);
1434 }
1435
1436 /***                            Integer logical                            ***/
1437 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1438 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
1439 {                                                                             \
1440     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1441        cpu_gpr[rB(ctx->opcode)]);                                             \
1442     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1443         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1444 }
1445
1446 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1447 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
1448 {                                                                             \
1449     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1450     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1451         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1452 }
1453
1454 /* and & and. */
1455 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1456 /* andc & andc. */
1457 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1458 /* andi. */
1459 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1460 {
1461     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1462     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1463 }
1464 /* andis. */
1465 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1466 {
1467     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1468     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1469 }
1470 /* cntlzw */
1471 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
1472 {
1473     gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1474     if (unlikely(Rc(ctx->opcode) != 0))
1475         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1476 }
1477 /* eqv & eqv. */
1478 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1479 /* extsb & extsb. */
1480 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1481 /* extsh & extsh. */
1482 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1483 /* nand & nand. */
1484 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1485 /* nor & nor. */
1486 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1487 /* or & or. */
1488 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1489 {
1490     int rs, ra, rb;
1491
1492     rs = rS(ctx->opcode);
1493     ra = rA(ctx->opcode);
1494     rb = rB(ctx->opcode);
1495     /* Optimisation for mr. ri case */
1496     if (rs != ra || rs != rb) {
1497         if (rs != rb)
1498             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1499         else
1500             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1501         if (unlikely(Rc(ctx->opcode) != 0))
1502             gen_set_Rc0(ctx, cpu_gpr[ra]);
1503     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1504         gen_set_Rc0(ctx, cpu_gpr[rs]);
1505 #if defined(TARGET_PPC64)
1506     } else {
1507         int prio = 0;
1508
1509         switch (rs) {
1510         case 1:
1511             /* Set process priority to low */
1512             prio = 2;
1513             break;
1514         case 6:
1515             /* Set process priority to medium-low */
1516             prio = 3;
1517             break;
1518         case 2:
1519             /* Set process priority to normal */
1520             prio = 4;
1521             break;
1522 #if !defined(CONFIG_USER_ONLY)
1523         case 31:
1524             if (ctx->mem_idx > 0) {
1525                 /* Set process priority to very low */
1526                 prio = 1;
1527             }
1528             break;
1529         case 5:
1530             if (ctx->mem_idx > 0) {
1531                 /* Set process priority to medium-hight */
1532                 prio = 5;
1533             }
1534             break;
1535         case 3:
1536             if (ctx->mem_idx > 0) {
1537                 /* Set process priority to high */
1538                 prio = 6;
1539             }
1540             break;
1541         case 7:
1542             if (ctx->mem_idx > 1) {
1543                 /* Set process priority to very high */
1544                 prio = 7;
1545             }
1546             break;
1547 #endif
1548         default:
1549             /* nop */
1550             break;
1551         }
1552         if (prio) {
1553             TCGv t0 = tcg_temp_new();
1554             gen_load_spr(t0, SPR_PPR);
1555             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1556             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1557             gen_store_spr(SPR_PPR, t0);
1558             tcg_temp_free(t0);
1559         }
1560 #endif
1561     }
1562 }
1563 /* orc & orc. */
1564 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1565 /* xor & xor. */
1566 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1567 {
1568     /* Optimisation for "set to zero" case */
1569     if (rS(ctx->opcode) != rB(ctx->opcode))
1570         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1571     else
1572         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1573     if (unlikely(Rc(ctx->opcode) != 0))
1574         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1575 }
1576 /* ori */
1577 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1578 {
1579     target_ulong uimm = UIMM(ctx->opcode);
1580
1581     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1582         /* NOP */
1583         /* XXX: should handle special NOPs for POWER series */
1584         return;
1585     }
1586     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1587 }
1588 /* oris */
1589 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1590 {
1591     target_ulong uimm = UIMM(ctx->opcode);
1592
1593     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1594         /* NOP */
1595         return;
1596     }
1597     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1598 }
1599 /* xori */
1600 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1601 {
1602     target_ulong uimm = UIMM(ctx->opcode);
1603
1604     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1605         /* NOP */
1606         return;
1607     }
1608     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1609 }
1610 /* xoris */
1611 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1612 {
1613     target_ulong uimm = UIMM(ctx->opcode);
1614
1615     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1616         /* NOP */
1617         return;
1618     }
1619     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1620 }
1621 /* popcntb : PowerPC 2.03 specification */
1622 GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1623 {
1624 #if defined(TARGET_PPC64)
1625     if (ctx->sf_mode)
1626         gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1627     else
1628 #endif
1629         gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1630 }
1631
1632 #if defined(TARGET_PPC64)
1633 /* extsw & extsw. */
1634 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1635 /* cntlzd */
1636 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1637 {
1638     gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1639     if (unlikely(Rc(ctx->opcode) != 0))
1640         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1641 }
1642 #endif
1643
1644 /***                             Integer rotate                            ***/
1645 /* rlwimi & rlwimi. */
1646 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1647 {
1648     uint32_t mb, me, sh;
1649
1650     mb = MB(ctx->opcode);
1651     me = ME(ctx->opcode);
1652     sh = SH(ctx->opcode);
1653     if (likely(sh == 0 && mb == 0 && me == 31)) {
1654         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1655     } else {
1656         target_ulong mask;
1657         TCGv t1;
1658         TCGv t0 = tcg_temp_new();
1659 #if defined(TARGET_PPC64)
1660         TCGv_i32 t2 = tcg_temp_new_i32();
1661         tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1662         tcg_gen_rotli_i32(t2, t2, sh);
1663         tcg_gen_extu_i32_i64(t0, t2);
1664         tcg_temp_free_i32(t2);
1665 #else
1666         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1667 #endif
1668 #if defined(TARGET_PPC64)
1669         mb += 32;
1670         me += 32;
1671 #endif
1672         mask = MASK(mb, me);
1673         t1 = tcg_temp_new();
1674         tcg_gen_andi_tl(t0, t0, mask);
1675         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1676         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1677         tcg_temp_free(t0);
1678         tcg_temp_free(t1);
1679     }
1680     if (unlikely(Rc(ctx->opcode) != 0))
1681         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1682 }
1683 /* rlwinm & rlwinm. */
1684 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1685 {
1686     uint32_t mb, me, sh;
1687
1688     sh = SH(ctx->opcode);
1689     mb = MB(ctx->opcode);
1690     me = ME(ctx->opcode);
1691
1692     if (likely(mb == 0 && me == (31 - sh))) {
1693         if (likely(sh == 0)) {
1694             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1695         } else {
1696             TCGv t0 = tcg_temp_new();
1697             tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1698             tcg_gen_shli_tl(t0, t0, sh);
1699             tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1700             tcg_temp_free(t0);
1701         }
1702     } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1703         TCGv t0 = tcg_temp_new();
1704         tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1705         tcg_gen_shri_tl(t0, t0, mb);
1706         tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1707         tcg_temp_free(t0);
1708     } else {
1709         TCGv t0 = tcg_temp_new();
1710 #if defined(TARGET_PPC64)
1711         TCGv_i32 t1 = tcg_temp_new_i32();
1712         tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1713         tcg_gen_rotli_i32(t1, t1, sh);
1714         tcg_gen_extu_i32_i64(t0, t1);
1715         tcg_temp_free_i32(t1);
1716 #else
1717         tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1718 #endif
1719 #if defined(TARGET_PPC64)
1720         mb += 32;
1721         me += 32;
1722 #endif
1723         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1724         tcg_temp_free(t0);
1725     }
1726     if (unlikely(Rc(ctx->opcode) != 0))
1727         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1728 }
1729 /* rlwnm & rlwnm. */
1730 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1731 {
1732     uint32_t mb, me;
1733     TCGv t0;
1734 #if defined(TARGET_PPC64)
1735     TCGv_i32 t1, t2;
1736 #endif
1737
1738     mb = MB(ctx->opcode);
1739     me = ME(ctx->opcode);
1740     t0 = tcg_temp_new();
1741     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1742 #if defined(TARGET_PPC64)
1743     t1 = tcg_temp_new_i32();
1744     t2 = tcg_temp_new_i32();
1745     tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1746     tcg_gen_trunc_i64_i32(t2, t0);
1747     tcg_gen_rotl_i32(t1, t1, t2);
1748     tcg_gen_extu_i32_i64(t0, t1);
1749     tcg_temp_free_i32(t1);
1750     tcg_temp_free_i32(t2);
1751 #else
1752     tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1753 #endif
1754     if (unlikely(mb != 0 || me != 31)) {
1755 #if defined(TARGET_PPC64)
1756         mb += 32;
1757         me += 32;
1758 #endif
1759         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1760     } else {
1761         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1762     }
1763     tcg_temp_free(t0);
1764     if (unlikely(Rc(ctx->opcode) != 0))
1765         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1766 }
1767
1768 #if defined(TARGET_PPC64)
1769 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
1770 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1771 {                                                                             \
1772     gen_##name(ctx, 0);                                                       \
1773 }                                                                             \
1774 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1775              PPC_64B)                                                         \
1776 {                                                                             \
1777     gen_##name(ctx, 1);                                                       \
1778 }
1779 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
1780 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1781 {                                                                             \
1782     gen_##name(ctx, 0, 0);                                                    \
1783 }                                                                             \
1784 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
1785              PPC_64B)                                                         \
1786 {                                                                             \
1787     gen_##name(ctx, 0, 1);                                                    \
1788 }                                                                             \
1789 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1790              PPC_64B)                                                         \
1791 {                                                                             \
1792     gen_##name(ctx, 1, 0);                                                    \
1793 }                                                                             \
1794 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
1795              PPC_64B)                                                         \
1796 {                                                                             \
1797     gen_##name(ctx, 1, 1);                                                    \
1798 }
1799
1800 static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1801                                       uint32_t me, uint32_t sh)
1802 {
1803     if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1804         tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1805     } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1806         tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1807     } else {
1808         TCGv t0 = tcg_temp_new();
1809         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1810         if (likely(mb == 0 && me == 63)) {
1811             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1812         } else {
1813             tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1814         }
1815         tcg_temp_free(t0);
1816     }
1817     if (unlikely(Rc(ctx->opcode) != 0))
1818         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1819 }
1820 /* rldicl - rldicl. */
1821 static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1822 {
1823     uint32_t sh, mb;
1824
1825     sh = SH(ctx->opcode) | (shn << 5);
1826     mb = MB(ctx->opcode) | (mbn << 5);
1827     gen_rldinm(ctx, mb, 63, sh);
1828 }
1829 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1830 /* rldicr - rldicr. */
1831 static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1832 {
1833     uint32_t sh, me;
1834
1835     sh = SH(ctx->opcode) | (shn << 5);
1836     me = MB(ctx->opcode) | (men << 5);
1837     gen_rldinm(ctx, 0, me, sh);
1838 }
1839 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1840 /* rldic - rldic. */
1841 static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1842 {
1843     uint32_t sh, mb;
1844
1845     sh = SH(ctx->opcode) | (shn << 5);
1846     mb = MB(ctx->opcode) | (mbn << 5);
1847     gen_rldinm(ctx, mb, 63 - sh, sh);
1848 }
1849 GEN_PPC64_R4(rldic, 0x1E, 0x04);
1850
1851 static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1852                                      uint32_t me)
1853 {
1854     TCGv t0;
1855
1856     mb = MB(ctx->opcode);
1857     me = ME(ctx->opcode);
1858     t0 = tcg_temp_new();
1859     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1860     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1861     if (unlikely(mb != 0 || me != 63)) {
1862         tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1863     } else {
1864         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1865     }
1866     tcg_temp_free(t0);
1867     if (unlikely(Rc(ctx->opcode) != 0))
1868         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1869 }
1870
1871 /* rldcl - rldcl. */
1872 static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1873 {
1874     uint32_t mb;
1875
1876     mb = MB(ctx->opcode) | (mbn << 5);
1877     gen_rldnm(ctx, mb, 63);
1878 }
1879 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1880 /* rldcr - rldcr. */
1881 static always_inline void gen_rldcr (DisasContext *ctx, int men)
1882 {
1883     uint32_t me;
1884
1885     me = MB(ctx->opcode) | (men << 5);
1886     gen_rldnm(ctx, 0, me);
1887 }
1888 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1889 /* rldimi - rldimi. */
1890 static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1891 {
1892     uint32_t sh, mb, me;
1893
1894     sh = SH(ctx->opcode) | (shn << 5);
1895     mb = MB(ctx->opcode) | (mbn << 5);
1896     me = 63 - sh;
1897     if (unlikely(sh == 0 && mb == 0)) {
1898         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1899     } else {
1900         TCGv t0, t1;
1901         target_ulong mask;
1902
1903         t0 = tcg_temp_new();
1904         tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1905         t1 = tcg_temp_new();
1906         mask = MASK(mb, me);
1907         tcg_gen_andi_tl(t0, t0, mask);
1908         tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1909         tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1910         tcg_temp_free(t0);
1911         tcg_temp_free(t1);
1912     }
1913     if (unlikely(Rc(ctx->opcode) != 0))
1914         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1915 }
1916 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1917 #endif
1918
1919 /***                             Integer shift                             ***/
1920 /* slw & slw. */
1921 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1922 {
1923     TCGv t0;
1924     int l1, l2;
1925     l1 = gen_new_label();
1926     l2 = gen_new_label();
1927
1928     t0 = tcg_temp_local_new();
1929     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1930     tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1931     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1932     tcg_gen_br(l2);
1933     gen_set_label(l1);
1934     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
1935     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1936     gen_set_label(l2);
1937     tcg_temp_free(t0);
1938     if (unlikely(Rc(ctx->opcode) != 0))
1939         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1940 }
1941 /* sraw & sraw. */
1942 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
1943 {
1944     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
1945                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1946     if (unlikely(Rc(ctx->opcode) != 0))
1947         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1948 }
1949 /* srawi & srawi. */
1950 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1951 {
1952     int sh = SH(ctx->opcode);
1953     if (sh != 0) {
1954         int l1, l2;
1955         TCGv t0;
1956         l1 = gen_new_label();
1957         l2 = gen_new_label();
1958         t0 = tcg_temp_local_new();
1959         tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1960         tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
1961         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
1962         tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1963         tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
1964         tcg_gen_br(l2);
1965         gen_set_label(l1);
1966         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1967         gen_set_label(l2);
1968         tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1969         tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
1970         tcg_temp_free(t0);
1971     } else {
1972         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1973         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1974     }
1975     if (unlikely(Rc(ctx->opcode) != 0))
1976         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1977 }
1978 /* srw & srw. */
1979 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
1980 {
1981     TCGv t0, t1;
1982     int l1, l2;
1983     l1 = gen_new_label();
1984     l2 = gen_new_label();
1985
1986     t0 = tcg_temp_local_new();
1987     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1988     tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1989     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1990     tcg_gen_br(l2);
1991     gen_set_label(l1);
1992     t1 = tcg_temp_new();
1993     tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
1994     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
1995     tcg_temp_free(t1);
1996     gen_set_label(l2);
1997     tcg_temp_free(t0);
1998     if (unlikely(Rc(ctx->opcode) != 0))
1999         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2000 }
2001 #if defined(TARGET_PPC64)
2002 /* sld & sld. */
2003 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
2004 {
2005     TCGv t0;
2006     int l1, l2;
2007     l1 = gen_new_label();
2008     l2 = gen_new_label();
2009
2010     t0 = tcg_temp_local_new();
2011     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2012     tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2013     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2014     tcg_gen_br(l2);
2015     gen_set_label(l1);
2016     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2017     gen_set_label(l2);
2018     tcg_temp_free(t0);
2019     if (unlikely(Rc(ctx->opcode) != 0))
2020         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2021 }
2022 /* srad & srad. */
2023 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
2024 {
2025     gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
2026                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2027     if (unlikely(Rc(ctx->opcode) != 0))
2028         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2029 }
2030 /* sradi & sradi. */
2031 static always_inline void gen_sradi (DisasContext *ctx, int n)
2032 {
2033     int sh = SH(ctx->opcode) + (n << 5);
2034     if (sh != 0) {
2035         int l1, l2;
2036         TCGv t0;
2037         l1 = gen_new_label();
2038         l2 = gen_new_label();
2039         t0 = tcg_temp_local_new();
2040         tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
2041         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
2042         tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2043         tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
2044         tcg_gen_br(l2);
2045         gen_set_label(l1);
2046         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2047         gen_set_label(l2);
2048         tcg_temp_free(t0);
2049         tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
2050     } else {
2051         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2052         tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2053     }
2054     if (unlikely(Rc(ctx->opcode) != 0))
2055         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2056 }
2057 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
2058 {
2059     gen_sradi(ctx, 0);
2060 }
2061 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
2062 {
2063     gen_sradi(ctx, 1);
2064 }
2065 /* srd & srd. */
2066 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
2067 {
2068     TCGv t0;
2069     int l1, l2;
2070     l1 = gen_new_label();
2071     l2 = gen_new_label();
2072
2073     t0 = tcg_temp_local_new();
2074     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2075     tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2076     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2077     tcg_gen_br(l2);
2078     gen_set_label(l1);
2079     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2080     gen_set_label(l2);
2081     tcg_temp_free(t0);
2082     if (unlikely(Rc(ctx->opcode) != 0))
2083         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2084 }
2085 #endif
2086
2087 /***                       Floating-Point arithmetic                       ***/
2088 #define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2089 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2090 {                                                                             \
2091     if (unlikely(!ctx->fpu_enabled)) {                                        \
2092         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2093         return;                                                               \
2094     }                                                                         \
2095     gen_reset_fpstatus();                                                     \
2096     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2097                      cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2098     if (isfloat) {                                                            \
2099         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2100     }                                                                         \
2101     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
2102                      Rc(ctx->opcode) != 0);                                   \
2103 }
2104
2105 #define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2106 _GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2107 _GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2108
2109 #define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2110 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2111 {                                                                             \
2112     if (unlikely(!ctx->fpu_enabled)) {                                        \
2113         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2114         return;                                                               \
2115     }                                                                         \
2116     gen_reset_fpstatus();                                                     \
2117     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2118                      cpu_fpr[rB(ctx->opcode)]);                               \
2119     if (isfloat) {                                                            \
2120         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2121     }                                                                         \
2122     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2123                      set_fprf, Rc(ctx->opcode) != 0);                         \
2124 }
2125 #define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2126 _GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2127 _GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2128
2129 #define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2130 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2131 {                                                                             \
2132     if (unlikely(!ctx->fpu_enabled)) {                                        \
2133         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2134         return;                                                               \
2135     }                                                                         \
2136     gen_reset_fpstatus();                                                     \
2137     gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2138                        cpu_fpr[rC(ctx->opcode)]);                             \
2139     if (isfloat) {                                                            \
2140         gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2141     }                                                                         \
2142     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2143                      set_fprf, Rc(ctx->opcode) != 0);                         \
2144 }
2145 #define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2146 _GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2147 _GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2148
2149 #define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2150 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2151 {                                                                             \
2152     if (unlikely(!ctx->fpu_enabled)) {                                        \
2153         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2154         return;                                                               \
2155     }                                                                         \
2156     gen_reset_fpstatus();                                                     \
2157     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2158     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2159                      set_fprf, Rc(ctx->opcode) != 0);                         \
2160 }
2161
2162 #define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2163 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2164 {                                                                             \
2165     if (unlikely(!ctx->fpu_enabled)) {                                        \
2166         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
2167         return;                                                               \
2168     }                                                                         \
2169     gen_reset_fpstatus();                                                     \
2170     gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2171     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2172                      set_fprf, Rc(ctx->opcode) != 0);                         \
2173 }
2174
2175 /* fadd - fadds */
2176 GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2177 /* fdiv - fdivs */
2178 GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2179 /* fmul - fmuls */
2180 GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2181
2182 /* fre */
2183 GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2184
2185 /* fres */
2186 GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2187
2188 /* frsqrte */
2189 GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2190
2191 /* frsqrtes */
2192 GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2193 {
2194     if (unlikely(!ctx->fpu_enabled)) {
2195         gen_exception(ctx, POWERPC_EXCP_FPU);
2196         return;
2197     }
2198     gen_reset_fpstatus();
2199     gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2200     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2201     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2202 }
2203
2204 /* fsel */
2205 _GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2206 /* fsub - fsubs */
2207 GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2208 /* Optional: */
2209 /* fsqrt */
2210 GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2211 {
2212     if (unlikely(!ctx->fpu_enabled)) {
2213         gen_exception(ctx, POWERPC_EXCP_FPU);
2214         return;
2215     }
2216     gen_reset_fpstatus();
2217     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2218     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2219 }
2220
2221 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2222 {
2223     if (unlikely(!ctx->fpu_enabled)) {
2224         gen_exception(ctx, POWERPC_EXCP_FPU);
2225         return;
2226     }
2227     gen_reset_fpstatus();
2228     gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2229     gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2230     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2231 }
2232
2233 /***                     Floating-Point multiply-and-add                   ***/
2234 /* fmadd - fmadds */
2235 GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2236 /* fmsub - fmsubs */
2237 GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2238 /* fnmadd - fnmadds */
2239 GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2240 /* fnmsub - fnmsubs */
2241 GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2242
2243 /***                     Floating-Point round & convert                    ***/
2244 /* fctiw */
2245 GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2246 /* fctiwz */
2247 GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2248 /* frsp */
2249 GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2250 #if defined(TARGET_PPC64)
2251 /* fcfid */
2252 GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2253 /* fctid */
2254 GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2255 /* fctidz */
2256 GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2257 #endif
2258
2259 /* frin */
2260 GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2261 /* friz */
2262 GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2263 /* frip */
2264 GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2265 /* frim */
2266 GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2267
2268 /***                         Floating-Point compare                        ***/
2269 /* fcmpo */
2270 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
2271 {
2272     if (unlikely(!ctx->fpu_enabled)) {
2273         gen_exception(ctx, POWERPC_EXCP_FPU);
2274         return;
2275     }
2276     gen_reset_fpstatus();
2277     gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2278                      cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2279     gen_helper_float_check_status();
2280 }
2281
2282 /* fcmpu */
2283 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
2284 {
2285     if (unlikely(!ctx->fpu_enabled)) {
2286         gen_exception(ctx, POWERPC_EXCP_FPU);
2287         return;
2288     }
2289     gen_reset_fpstatus();
2290     gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2291                      cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2292     gen_helper_float_check_status();
2293 }
2294
2295 /***                         Floating-point move                           ***/
2296 /* fabs */
2297 /* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2298 GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2299
2300 /* fmr  - fmr. */
2301 /* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2302 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2303 {
2304     if (unlikely(!ctx->fpu_enabled)) {
2305         gen_exception(ctx, POWERPC_EXCP_FPU);
2306         return;
2307     }
2308     tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2309     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2310 }
2311
2312 /* fnabs */
2313 /* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2314 GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2315 /* fneg */
2316 /* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2317 GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2318
2319 /***                  Floating-Point status & ctrl register                ***/
2320 /* mcrfs */
2321 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2322 {
2323     int bfa;
2324
2325     if (unlikely(!ctx->fpu_enabled)) {
2326         gen_exception(ctx, POWERPC_EXCP_FPU);
2327         return;
2328     }
2329     gen_optimize_fprf();
2330     bfa = 4 * (7 - crfS(ctx->opcode));
2331     tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2332     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2333     tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2334 }
2335
2336 /* mffs */
2337 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2338 {
2339     if (unlikely(!ctx->fpu_enabled)) {
2340         gen_exception(ctx, POWERPC_EXCP_FPU);
2341         return;
2342     }
2343     gen_optimize_fprf();
2344     gen_reset_fpstatus();
2345     tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2346     gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2347 }
2348
2349 /* mtfsb0 */
2350 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2351 {
2352     uint8_t crb;
2353
2354     if (unlikely(!ctx->fpu_enabled)) {
2355         gen_exception(ctx, POWERPC_EXCP_FPU);
2356         return;
2357     }
2358     crb = 31 - crbD(ctx->opcode);
2359     gen_optimize_fprf();
2360     gen_reset_fpstatus();
2361     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX)) {
2362         TCGv_i32 t0 = tcg_const_i32(crb);
2363         gen_helper_fpscr_clrbit(t0);
2364         tcg_temp_free_i32(t0);
2365     }
2366     if (unlikely(Rc(ctx->opcode) != 0)) {
2367         tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2368     }
2369 }
2370
2371 /* mtfsb1 */
2372 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2373 {
2374     uint8_t crb;
2375
2376     if (unlikely(!ctx->fpu_enabled)) {
2377         gen_exception(ctx, POWERPC_EXCP_FPU);
2378         return;
2379     }
2380     crb = 31 - crbD(ctx->opcode);
2381     gen_optimize_fprf();
2382     gen_reset_fpstatus();
2383     /* XXX: we pretend we can only do IEEE floating-point computations */
2384     if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2385         TCGv_i32 t0 = tcg_const_i32(crb);
2386         gen_helper_fpscr_setbit(t0);
2387         tcg_temp_free_i32(t0);
2388     }
2389     if (unlikely(Rc(ctx->opcode) != 0)) {
2390         tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2391     }
2392     /* We can raise a differed exception */
2393     gen_helper_float_check_status();
2394 }
2395
2396 /* mtfsf */
2397 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2398 {
2399     TCGv_i32 t0;
2400
2401     if (unlikely(!ctx->fpu_enabled)) {
2402         gen_exception(ctx, POWERPC_EXCP_FPU);
2403         return;
2404     }
2405     gen_optimize_fprf();
2406     gen_reset_fpstatus();
2407     t0 = tcg_const_i32(FM(ctx->opcode));
2408     gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2409     tcg_temp_free_i32(t0);
2410     if (unlikely(Rc(ctx->opcode) != 0)) {
2411         tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2412     }
2413     /* We can raise a differed exception */
2414     gen_helper_float_check_status();
2415 }
2416
2417 /* mtfsfi */
2418 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2419 {
2420     int bf, sh;
2421     TCGv_i64 t0;
2422     TCGv_i32 t1;
2423
2424     if (unlikely(!ctx->fpu_enabled)) {
2425         gen_exception(ctx, POWERPC_EXCP_FPU);
2426         return;
2427     }
2428     bf = crbD(ctx->opcode) >> 2;
2429     sh = 7 - bf;
2430     gen_optimize_fprf();
2431     gen_reset_fpstatus();
2432     t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
2433     t1 = tcg_const_i32(1 << sh);
2434     gen_helper_store_fpscr(t0, t1);
2435     tcg_temp_free_i64(t0);
2436     tcg_temp_free_i32(t1);
2437     if (unlikely(Rc(ctx->opcode) != 0)) {
2438         tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2439     }
2440     /* We can raise a differed exception */
2441     gen_helper_float_check_status();
2442 }
2443
2444 /***                           Addressing modes                            ***/
2445 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2446 static always_inline void gen_addr_imm_index (DisasContext *ctx, TCGv EA, target_long maskl)
2447 {
2448     target_long simm = SIMM(ctx->opcode);
2449
2450     simm &= ~maskl;
2451     if (rA(ctx->opcode) == 0) {
2452 #if defined(TARGET_PPC64)
2453         if (!ctx->sf_mode) {
2454             tcg_gen_movi_tl(EA, (uint32_t)simm);
2455         } else
2456 #endif
2457         tcg_gen_movi_tl(EA, simm);
2458     } else if (likely(simm != 0)) {
2459         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2460 #if defined(TARGET_PPC64)
2461         if (!ctx->sf_mode) {
2462             tcg_gen_ext32u_tl(EA, EA);
2463         }
2464 #endif
2465     } else {
2466 #if defined(TARGET_PPC64)
2467         if (!ctx->sf_mode) {
2468             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2469         } else
2470 #endif
2471         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2472     }
2473 }
2474
2475 static always_inline void gen_addr_reg_index (DisasContext *ctx, TCGv EA)
2476 {
2477     if (rA(ctx->opcode) == 0) {
2478 #if defined(TARGET_PPC64)
2479         if (!ctx->sf_mode) {
2480             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2481         } else
2482 #endif
2483         tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2484     } else {
2485         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2486 #if defined(TARGET_PPC64)
2487         if (!ctx->sf_mode) {
2488             tcg_gen_ext32u_tl(EA, EA);
2489         }
2490 #endif
2491     }
2492 }
2493
2494 static always_inline void gen_addr_register (DisasContext *ctx, TCGv EA)
2495 {
2496     if (rA(ctx->opcode) == 0) {
2497         tcg_gen_movi_tl(EA, 0);
2498     } else {
2499 #if defined(TARGET_PPC64)
2500         if (!ctx->sf_mode) {
2501             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2502         } else
2503 #endif
2504             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2505     }
2506 }
2507
2508 static always_inline void gen_addr_add (DisasContext *ctx, TCGv ret, TCGv arg1, target_long val)
2509 {
2510     tcg_gen_addi_tl(ret, arg1, val);
2511 #if defined(TARGET_PPC64)
2512     if (!ctx->sf_mode) {
2513         tcg_gen_ext32u_tl(ret, ret);
2514     }
2515 #endif
2516 }
2517
2518 static always_inline void gen_check_align (DisasContext *ctx, TCGv EA, int mask)
2519 {
2520     int l1 = gen_new_label();
2521     TCGv t0 = tcg_temp_new();
2522     TCGv_i32 t1, t2;
2523     /* NIP cannot be restored if the memory exception comes from an helper */
2524     gen_update_nip(ctx, ctx->nip - 4);
2525     tcg_gen_andi_tl(t0, EA, mask);
2526     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2527     t1 = tcg_const_i32(POWERPC_EXCP_ALIGN);
2528     t2 = tcg_const_i32(0);
2529     gen_helper_raise_exception_err(t1, t2);
2530     tcg_temp_free_i32(t1);
2531     tcg_temp_free_i32(t2);
2532     gen_set_label(l1);
2533     tcg_temp_free(t0);
2534 }
2535
2536 /***                             Integer load                              ***/
2537 static always_inline void gen_qemu_ld8u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2538 {
2539     tcg_gen_qemu_ld8u(arg1, arg2, ctx->mem_idx);
2540 }
2541
2542 static always_inline void gen_qemu_ld8s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2543 {
2544     tcg_gen_qemu_ld8s(arg1, arg2, ctx->mem_idx);
2545 }
2546
2547 static always_inline void gen_qemu_ld16u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2548 {
2549     tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2550     if (unlikely(ctx->le_mode)) {
2551 #if defined(TARGET_PPC64)
2552         TCGv_i32 t0 = tcg_temp_new_i32();
2553         tcg_gen_trunc_tl_i32(t0, arg1);
2554         tcg_gen_bswap16_i32(t0, t0);
2555         tcg_gen_extu_i32_tl(arg1, t0);
2556         tcg_temp_free_i32(t0);
2557 #else
2558         tcg_gen_bswap16_i32(arg1, arg1);
2559 #endif
2560     }
2561 }
2562
2563 static always_inline void gen_qemu_ld16s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2564 {
2565     if (unlikely(ctx->le_mode)) {
2566 #if defined(TARGET_PPC64)
2567         TCGv_i32 t0;
2568         tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2569         t0 = tcg_temp_new_i32();
2570         tcg_gen_trunc_tl_i32(t0, arg1);
2571         tcg_gen_bswap16_i32(t0, t0);
2572         tcg_gen_extu_i32_tl(arg1, t0);
2573         tcg_gen_ext16s_tl(arg1, arg1);
2574         tcg_temp_free_i32(t0);
2575 #else
2576         tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2577         tcg_gen_bswap16_i32(arg1, arg1);
2578         tcg_gen_ext16s_i32(arg1, arg1);
2579 #endif
2580     } else {
2581         tcg_gen_qemu_ld16s(arg1, arg2, ctx->mem_idx);
2582     }
2583 }
2584
2585 static always_inline void gen_qemu_ld32u(DisasContext *ctx, TCGv arg1, TCGv arg2)
2586 {
2587     tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2588     if (unlikely(ctx->le_mode)) {
2589 #if defined(TARGET_PPC64)
2590         TCGv_i32 t0 = tcg_temp_new_i32();
2591         tcg_gen_trunc_tl_i32(t0, arg1);
2592         tcg_gen_bswap_i32(t0, t0);
2593         tcg_gen_extu_i32_tl(arg1, t0);
2594         tcg_temp_free_i32(t0);
2595 #else
2596         tcg_gen_bswap_i32(arg1, arg1);
2597 #endif
2598     }
2599 }
2600
2601 #if defined(TARGET_PPC64)
2602 static always_inline void gen_qemu_ld32s(DisasContext *ctx, TCGv arg1, TCGv arg2)
2603 {
2604     if (unlikely(ctx->mem_idx)) {
2605         TCGv_i32 t0;
2606         tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2607         t0 = tcg_temp_new_i32();
2608         tcg_gen_trunc_tl_i32(t0, arg1);
2609         tcg_gen_bswap_i32(t0, t0);
2610         tcg_gen_ext_i32_tl(arg1, t0);
2611         tcg_temp_free_i32(t0);
2612     } else
2613         tcg_gen_qemu_ld32s(arg1, arg2, ctx->mem_idx);
2614 }
2615 #endif
2616
2617 static always_inline void gen_qemu_ld64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2618 {
2619     tcg_gen_qemu_ld64(arg1, arg2, ctx->mem_idx);
2620     if (unlikely(ctx->le_mode)) {
2621         tcg_gen_bswap_i64(arg1, arg1);
2622     }
2623 }
2624
2625 static always_inline void gen_qemu_st8(DisasContext *ctx, TCGv arg1, TCGv arg2)
2626 {
2627     tcg_gen_qemu_st8(arg1, arg2, ctx->mem_idx);
2628 }
2629
2630 static always_inline void gen_qemu_st16(DisasContext *ctx, TCGv arg1, TCGv arg2)
2631 {
2632     if (unlikely(ctx->le_mode)) {
2633 #if defined(TARGET_PPC64)
2634         TCGv_i32 t0;
2635         TCGv t1;
2636         t0 = tcg_temp_new_i32();
2637         tcg_gen_trunc_tl_i32(t0, arg1);
2638         tcg_gen_ext16u_i32(t0, t0);
2639         tcg_gen_bswap16_i32(t0, t0);
2640         t1 = tcg_temp_new();
2641         tcg_gen_extu_i32_tl(t1, t0);
2642         tcg_temp_free_i32(t0);
2643         tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
2644         tcg_temp_free(t1);
2645 #else
2646         TCGv t0 = tcg_temp_new();
2647         tcg_gen_ext16u_tl(t0, arg1);
2648         tcg_gen_bswap16_i32(t0, t0);
2649         tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
2650         tcg_temp_free(t0);
2651 #endif
2652     } else {
2653         tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
2654     }
2655 }
2656
2657 static always_inline void gen_qemu_st32(DisasContext *ctx, TCGv arg1, TCGv arg2)
2658 {
2659     if (unlikely(ctx->le_mode)) {
2660 #if defined(TARGET_PPC64)
2661         TCGv_i32 t0;
2662         TCGv t1;
2663         t0 = tcg_temp_new_i32();
2664         tcg_gen_trunc_tl_i32(t0, arg1);
2665         tcg_gen_bswap_i32(t0, t0);
2666         t1 = tcg_temp_new();
2667         tcg_gen_extu_i32_tl(t1, t0);
2668         tcg_temp_free_i32(t0);
2669         tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
2670         tcg_temp_free(t1);
2671 #else
2672         TCGv t0 = tcg_temp_new_i32();
2673         tcg_gen_bswap_i32(t0, arg1);
2674         tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
2675         tcg_temp_free(t0);
2676 #endif
2677     } else {
2678         tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
2679     }
2680 }
2681
2682 static always_inline void gen_qemu_st64(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
2683 {
2684     if (unlikely(ctx->le_mode)) {
2685         TCGv_i64 t0 = tcg_temp_new_i64();
2686         tcg_gen_bswap_i64(t0, arg1);
2687         tcg_gen_qemu_st64(t0, arg2, ctx->mem_idx);
2688         tcg_temp_free_i64(t0);
2689     } else
2690         tcg_gen_qemu_st64(arg1, arg2, ctx->mem_idx);
2691 }
2692
2693 #define GEN_LD(name, ldop, opc, type)                                         \
2694 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2695 {                                                                             \
2696     TCGv EA;                                                                  \
2697     gen_set_access_type(ctx, ACCESS_INT);                                     \
2698     EA = tcg_temp_new();                                                      \
2699     gen_addr_imm_index(ctx, EA, 0);                                           \
2700     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2701     tcg_temp_free(EA);                                                        \
2702 }
2703
2704 #define GEN_LDU(name, ldop, opc, type)                                        \
2705 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2706 {                                                                             \
2707     TCGv EA;                                                                  \
2708     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2709                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2710         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2711         return;                                                               \
2712     }                                                                         \
2713     gen_set_access_type(ctx, ACCESS_INT);                                     \
2714     EA = tcg_temp_new();                                                      \
2715     if (type == PPC_64B)                                                      \
2716         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2717     else                                                                      \
2718         gen_addr_imm_index(ctx, EA, 0);                                       \
2719     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2720     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2721     tcg_temp_free(EA);                                                        \
2722 }
2723
2724 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2725 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2726 {                                                                             \
2727     TCGv EA;                                                                  \
2728     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2729                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2730         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2731         return;                                                               \
2732     }                                                                         \
2733     gen_set_access_type(ctx, ACCESS_INT);                                     \
2734     EA = tcg_temp_new();                                                      \
2735     gen_addr_reg_index(ctx, EA);                                              \
2736     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2737     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2738     tcg_temp_free(EA);                                                        \
2739 }
2740
2741 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2742 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2743 {                                                                             \
2744     TCGv EA;                                                                  \
2745     gen_set_access_type(ctx, ACCESS_INT);                                     \
2746     EA = tcg_temp_new();                                                      \
2747     gen_addr_reg_index(ctx, EA);                                              \
2748     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2749     tcg_temp_free(EA);                                                        \
2750 }
2751
2752 #define GEN_LDS(name, ldop, op, type)                                         \
2753 GEN_LD(name, ldop, op | 0x20, type);                                          \
2754 GEN_LDU(name, ldop, op | 0x21, type);                                         \
2755 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2756 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2757
2758 /* lbz lbzu lbzux lbzx */
2759 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2760 /* lha lhau lhaux lhax */
2761 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2762 /* lhz lhzu lhzux lhzx */
2763 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2764 /* lwz lwzu lwzux lwzx */
2765 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2766 #if defined(TARGET_PPC64)
2767 /* lwaux */
2768 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2769 /* lwax */
2770 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2771 /* ldux */
2772 GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2773 /* ldx */
2774 GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2775 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2776 {
2777     TCGv EA;
2778     if (Rc(ctx->opcode)) {
2779         if (unlikely(rA(ctx->opcode) == 0 ||
2780                      rA(ctx->opcode) == rD(ctx->opcode))) {
2781             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2782             return;
2783         }
2784     }
2785     gen_set_access_type(ctx, ACCESS_INT);
2786     EA = tcg_temp_new();
2787     gen_addr_imm_index(ctx, EA, 0x03);
2788     if (ctx->opcode & 0x02) {
2789         /* lwa (lwau is undefined) */
2790         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2791     } else {
2792         /* ld - ldu */
2793         gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2794     }
2795     if (Rc(ctx->opcode))
2796         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2797     tcg_temp_free(EA);
2798 }
2799 /* lq */
2800 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2801 {
2802 #if defined(CONFIG_USER_ONLY)
2803     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2804 #else
2805     int ra, rd;
2806     TCGv EA;
2807
2808     /* Restore CPU state */
2809     if (unlikely(ctx->mem_idx == 0)) {
2810         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2811         return;
2812     }
2813     ra = rA(ctx->opcode);
2814     rd = rD(ctx->opcode);
2815     if (unlikely((rd & 1) || rd == ra)) {
2816         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2817         return;
2818     }
2819     if (unlikely(ctx->le_mode)) {
2820         /* Little-endian mode is not handled */
2821         gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2822         return;
2823     }
2824     gen_set_access_type(ctx, ACCESS_INT);
2825     EA = tcg_temp_new();
2826     gen_addr_imm_index(ctx, EA, 0x0F);
2827     gen_qemu_ld64(ctx, cpu_gpr[rd], EA);
2828     gen_addr_add(ctx, EA, EA, 8);
2829     gen_qemu_ld64(ctx, cpu_gpr[rd+1], EA);
2830     tcg_temp_free(EA);
2831 #endif
2832 }
2833 #endif
2834
2835 /***                              Integer store                            ***/
2836 #define GEN_ST(name, stop, opc, type)                                         \
2837 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2838 {                                                                             \
2839     TCGv EA;                                                                  \
2840     gen_set_access_type(ctx, ACCESS_INT);                                     \
2841     EA = tcg_temp_new();                                                      \
2842     gen_addr_imm_index(ctx, EA, 0);                                           \
2843     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2844     tcg_temp_free(EA);                                                        \
2845 }
2846
2847 #define GEN_STU(name, stop, opc, type)                                        \
2848 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2849 {                                                                             \
2850     TCGv EA;                                                                  \
2851     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2852         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2853         return;                                                               \
2854     }                                                                         \
2855     gen_set_access_type(ctx, ACCESS_INT);                                     \
2856     EA = tcg_temp_new();                                                      \
2857     if (type == PPC_64B)                                                      \
2858         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2859     else                                                                      \
2860         gen_addr_imm_index(ctx, EA, 0);                                       \
2861     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2862     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2863     tcg_temp_free(EA);                                                        \
2864 }
2865
2866 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
2867 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2868 {                                                                             \
2869     TCGv EA;                                                                  \
2870     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2871         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2872         return;                                                               \
2873     }                                                                         \
2874     gen_set_access_type(ctx, ACCESS_INT);                                     \
2875     EA = tcg_temp_new();                                                      \
2876     gen_addr_reg_index(ctx, EA);                                              \
2877     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2878     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2879     tcg_temp_free(EA);                                                        \
2880 }
2881
2882 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
2883 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2884 {                                                                             \
2885     TCGv EA;                                                                  \
2886     gen_set_access_type(ctx, ACCESS_INT);                                     \
2887     EA = tcg_temp_new();                                                      \
2888     gen_addr_reg_index(ctx, EA);                                              \
2889     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2890     tcg_temp_free(EA);                                                        \
2891 }
2892
2893 #define GEN_STS(name, stop, op, type)                                         \
2894 GEN_ST(name, stop, op | 0x20, type);                                          \
2895 GEN_STU(name, stop, op | 0x21, type);                                         \
2896 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2897 GEN_STX(name, stop, 0x17, op | 0x00, type)
2898
2899 /* stb stbu stbux stbx */
2900 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2901 /* sth sthu sthux sthx */
2902 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2903 /* stw stwu stwux stwx */
2904 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2905 #if defined(TARGET_PPC64)
2906 GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
2907 GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
2908 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2909 {
2910     int rs;
2911     TCGv EA;
2912
2913     rs = rS(ctx->opcode);
2914     if ((ctx->opcode & 0x3) == 0x2) {
2915 #if defined(CONFIG_USER_ONLY)
2916         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2917 #else
2918         /* stq */
2919         if (unlikely(ctx->mem_idx == 0)) {
2920             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2921             return;
2922         }
2923         if (unlikely(rs & 1)) {
2924             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2925             return;
2926         }
2927         if (unlikely(ctx->le_mode)) {
2928             /* Little-endian mode is not handled */
2929             gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2930             return;
2931         }
2932         gen_set_access_type(ctx, ACCESS_INT);
2933         EA = tcg_temp_new();
2934         gen_addr_imm_index(ctx, EA, 0x03);
2935         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2936         gen_addr_add(ctx, EA, EA, 8);
2937         gen_qemu_st64(ctx, cpu_gpr[rs+1], EA);
2938         tcg_temp_free(EA);
2939 #endif
2940     } else {
2941         /* std / stdu */
2942         if (Rc(ctx->opcode)) {
2943             if (unlikely(rA(ctx->opcode) == 0)) {
2944                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2945                 return;
2946             }
2947         }
2948         gen_set_access_type(ctx, ACCESS_INT);
2949         EA = tcg_temp_new();
2950         gen_addr_imm_index(ctx, EA, 0x03);
2951         gen_qemu_st64(ctx, cpu_gpr[rs], EA);
2952         if (Rc(ctx->opcode))
2953             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2954         tcg_temp_free(EA);
2955     }
2956 }
2957 #endif
2958 /***                Integer load and store with byte reverse               ***/
2959 /* lhbrx */
2960 static void always_inline gen_qemu_ld16ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2961 {
2962     tcg_gen_qemu_ld16u(arg1, arg2, ctx->mem_idx);
2963     if (likely(!ctx->le_mode)) {
2964 #if defined(TARGET_PPC64)
2965         TCGv_i32 t0 = tcg_temp_new_i32();
2966         tcg_gen_trunc_tl_i32(t0, arg1);
2967         tcg_gen_bswap16_i32(t0, t0);
2968         tcg_gen_extu_i32_tl(arg1, t0);
2969         tcg_temp_free_i32(t0);
2970 #else
2971         tcg_gen_bswap16_i32(arg1, arg1);
2972 #endif
2973     }
2974 }
2975 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
2976
2977 /* lwbrx */
2978 static void always_inline gen_qemu_ld32ur(DisasContext *ctx, TCGv arg1, TCGv arg2)
2979 {
2980     tcg_gen_qemu_ld32u(arg1, arg2, ctx->mem_idx);
2981     if (likely(!ctx->le_mode)) {
2982 #if defined(TARGET_PPC64)
2983         TCGv_i32 t0 = tcg_temp_new_i32();
2984         tcg_gen_trunc_tl_i32(t0, arg1);
2985         tcg_gen_bswap_i32(t0, t0);
2986         tcg_gen_extu_i32_tl(arg1, t0);
2987         tcg_temp_free_i32(t0);
2988 #else
2989         tcg_gen_bswap_i32(arg1, arg1);
2990 #endif
2991     }
2992 }
2993 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
2994
2995 /* sthbrx */
2996 static void always_inline gen_qemu_st16r(DisasContext *ctx, TCGv arg1, TCGv arg2)
2997 {
2998     if (likely(!ctx->le_mode)) {
2999 #if defined(TARGET_PPC64)
3000         TCGv_i32 t0;
3001         TCGv t1;
3002         t0 = tcg_temp_new_i32();
3003         tcg_gen_trunc_tl_i32(t0, arg1);
3004         tcg_gen_ext16u_i32(t0, t0);
3005         tcg_gen_bswap16_i32(t0, t0);
3006         t1 = tcg_temp_new();
3007         tcg_gen_extu_i32_tl(t1, t0);
3008         tcg_temp_free_i32(t0);
3009         tcg_gen_qemu_st16(t1, arg2, ctx->mem_idx);
3010         tcg_temp_free(t1);
3011 #else
3012         TCGv t0 = tcg_temp_new();
3013         tcg_gen_ext16u_tl(t0, arg1);
3014         tcg_gen_bswap16_i32(t0, t0);
3015         tcg_gen_qemu_st16(t0, arg2, ctx->mem_idx);
3016         tcg_temp_free(t0);
3017 #endif
3018     } else {
3019         tcg_gen_qemu_st16(arg1, arg2, ctx->mem_idx);
3020     }
3021 }
3022 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3023
3024 /* stwbrx */
3025 static void always_inline gen_qemu_st32r(DisasContext *ctx, TCGv arg1, TCGv arg2)
3026 {
3027     if (likely(!ctx->le_mode)) {
3028 #if defined(TARGET_PPC64)
3029         TCGv_i32 t0;
3030         TCGv t1;
3031         t0 = tcg_temp_new_i32();
3032         tcg_gen_trunc_tl_i32(t0, arg1);
3033         tcg_gen_bswap_i32(t0, t0);
3034         t1 = tcg_temp_new();
3035         tcg_gen_extu_i32_tl(t1, t0);
3036         tcg_temp_free_i32(t0);
3037         tcg_gen_qemu_st32(t1, arg2, ctx->mem_idx);
3038         tcg_temp_free(t1);
3039 #else
3040         TCGv t0 = tcg_temp_new_i32();
3041         tcg_gen_bswap_i32(t0, arg1);
3042         tcg_gen_qemu_st32(t0, arg2, ctx->mem_idx);
3043         tcg_temp_free(t0);
3044 #endif
3045     } else {
3046         tcg_gen_qemu_st32(arg1, arg2, ctx->mem_idx);
3047     }
3048 }
3049 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3050
3051 /***                    Integer load and store multiple                    ***/
3052 /* lmw */
3053 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3054 {
3055     TCGv t0;
3056     TCGv_i32 t1;
3057     gen_set_access_type(ctx, ACCESS_INT);
3058     /* NIP cannot be restored if the memory exception comes from an helper */
3059     gen_update_nip(ctx, ctx->nip - 4);
3060     t0 = tcg_temp_new();
3061     t1 = tcg_const_i32(rD(ctx->opcode));
3062     gen_addr_imm_index(ctx, t0, 0);
3063     gen_helper_lmw(t0, t1);
3064     tcg_temp_free(t0);
3065     tcg_temp_free_i32(t1);
3066 }
3067
3068 /* stmw */
3069 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3070 {
3071     TCGv t0;
3072     TCGv_i32 t1;
3073     gen_set_access_type(ctx, ACCESS_INT);
3074     /* NIP cannot be restored if the memory exception comes from an helper */
3075     gen_update_nip(ctx, ctx->nip - 4);
3076     t0 = tcg_temp_new();
3077     t1 = tcg_const_i32(rS(ctx->opcode));
3078     gen_addr_imm_index(ctx, t0, 0);
3079     gen_helper_stmw(t0, t1);
3080     tcg_temp_free(t0);
3081     tcg_temp_free_i32(t1);
3082 }
3083
3084 /***                    Integer load and store strings                     ***/
3085 /* lswi */
3086 /* PowerPC32 specification says we must generate an exception if
3087  * rA is in the range of registers to be loaded.
3088  * In an other hand, IBM says this is valid, but rA won't be loaded.
3089  * For now, I'll follow the spec...
3090  */
3091 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
3092 {
3093     TCGv t0;
3094     TCGv_i32 t1, t2;
3095     int nb = NB(ctx->opcode);
3096     int start = rD(ctx->opcode);
3097     int ra = rA(ctx->opcode);
3098     int nr;
3099
3100     if (nb == 0)
3101         nb = 32;
3102     nr = nb / 4;
3103     if (unlikely(((start + nr) > 32  &&
3104                   start <= ra && (start + nr - 32) > ra) ||
3105                  ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3106         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3107         return;
3108     }
3109     gen_set_access_type(ctx, ACCESS_INT);
3110     /* NIP cannot be restored if the memory exception comes from an helper */
3111     gen_update_nip(ctx, ctx->nip - 4);
3112     t0 = tcg_temp_new();
3113     gen_addr_register(ctx, t0);
3114     t1 = tcg_const_i32(nb);
3115     t2 = tcg_const_i32(start);
3116     gen_helper_lsw(t0, t1, t2);
3117     tcg_temp_free(t0);
3118     tcg_temp_free_i32(t1);
3119     tcg_temp_free_i32(t2);
3120 }
3121
3122 /* lswx */
3123 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
3124 {
3125     TCGv t0;
3126     TCGv_i32 t1, t2, t3;
3127     gen_set_access_type(ctx, ACCESS_INT);
3128     /* NIP cannot be restored if the memory exception comes from an helper */
3129     gen_update_nip(ctx, ctx->nip - 4);
3130     t0 = tcg_temp_new();
3131     gen_addr_reg_index(ctx, t0);
3132     t1 = tcg_const_i32(rD(ctx->opcode));
3133     t2 = tcg_const_i32(rA(ctx->opcode));
3134     t3 = tcg_const_i32(rB(ctx->opcode));
3135     gen_helper_lswx(t0, t1, t2, t3);
3136     tcg_temp_free(t0);
3137     tcg_temp_free_i32(t1);
3138     tcg_temp_free_i32(t2);
3139     tcg_temp_free_i32(t3);
3140 }
3141
3142 /* stswi */
3143 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
3144 {
3145     TCGv t0;
3146     TCGv_i32 t1, t2;
3147     int nb = NB(ctx->opcode);
3148     gen_set_access_type(ctx, ACCESS_INT);
3149     /* NIP cannot be restored if the memory exception comes from an helper */
3150     gen_update_nip(ctx, ctx->nip - 4);
3151     t0 = tcg_temp_new();
3152     gen_addr_register(ctx, t0);
3153     if (nb == 0)
3154         nb = 32;
3155     t1 = tcg_const_i32(nb);
3156     t2 = tcg_const_i32(rS(ctx->opcode));
3157     gen_helper_stsw(t0, t1, t2);
3158     tcg_temp_free(t0);
3159     tcg_temp_free_i32(t1);
3160     tcg_temp_free_i32(t2);
3161 }
3162
3163 /* stswx */
3164 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
3165 {
3166     TCGv t0;
3167     TCGv_i32 t1, t2;
3168     gen_set_access_type(ctx, ACCESS_INT);
3169     /* NIP cannot be restored if the memory exception comes from an helper */
3170     gen_update_nip(ctx, ctx->nip - 4);
3171     t0 = tcg_temp_new();
3172     gen_addr_reg_index(ctx, t0);
3173     t1 = tcg_temp_new_i32();
3174     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3175     tcg_gen_andi_i32(t1, t1, 0x7F);
3176     t2 = tcg_const_i32(rS(ctx->opcode));
3177     gen_helper_stsw(t0, t1, t2);
3178     tcg_temp_free(t0);
3179     tcg_temp_free_i32(t1);
3180     tcg_temp_free_i32(t2);
3181 }
3182
3183 /***                        Memory synchronisation                         ***/
3184 /* eieio */
3185 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
3186 {
3187 }
3188
3189 /* isync */
3190 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
3191 {
3192     gen_stop_exception(ctx);
3193 }
3194
3195 /* lwarx */
3196 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
3197 {
3198     TCGv t0;
3199     gen_set_access_type(ctx, ACCESS_RES);
3200     t0 = tcg_temp_local_new();
3201     gen_addr_reg_index(ctx, t0);
3202     gen_check_align(ctx, t0, 0x03);
3203     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3204     tcg_gen_mov_tl(cpu_reserve, t0);
3205     tcg_temp_free(t0);
3206 }
3207
3208 /* stwcx. */
3209 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
3210 {
3211     int l1;
3212     TCGv t0;
3213     gen_set_access_type(ctx, ACCESS_RES);
3214     t0 = tcg_temp_local_new();
3215     gen_addr_reg_index(ctx, t0);
3216     gen_check_align(ctx, t0, 0x03);
3217     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3218     tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3219     tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3220     l1 = gen_new_label();
3221     tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3222     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3223     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3224     gen_set_label(l1);
3225     tcg_gen_movi_tl(cpu_reserve, -1);
3226     tcg_temp_free(t0);
3227 }
3228
3229 #if defined(TARGET_PPC64)
3230 /* ldarx */
3231 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
3232 {
3233     TCGv t0;
3234     gen_set_access_type(ctx, ACCESS_RES);
3235     t0 = tcg_temp_local_new();
3236     gen_addr_reg_index(ctx, t0);
3237     gen_check_align(ctx, t0, 0x07);
3238     gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], t0);
3239     tcg_gen_mov_tl(cpu_reserve, t0);
3240     tcg_temp_free(t0);
3241 }
3242
3243 /* stdcx. */
3244 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
3245 {
3246     int l1;
3247     TCGv t0;
3248     gen_set_access_type(ctx, ACCESS_RES);
3249     t0 = tcg_temp_local_new();
3250     gen_addr_reg_index(ctx, t0);
3251     gen_check_align(ctx, t0, 0x07);
3252     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
3253     tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
3254     tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
3255     l1 = gen_new_label();
3256     tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3257     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 1 << CRF_EQ);
3258     gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], t0);
3259     gen_set_label(l1);
3260     tcg_gen_movi_tl(cpu_reserve, -1);
3261     tcg_temp_free(t0);
3262 }
3263 #endif /* defined(TARGET_PPC64) */
3264
3265 /* sync */
3266 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
3267 {
3268 }
3269
3270 /* wait */
3271 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3272 {
3273     TCGv_i32 t0 = tcg_temp_new_i32();
3274     tcg_gen_st_i32(t0, cpu_env, offsetof(CPUState, halted));
3275     tcg_temp_free_i32(t0);
3276     /* Stop translation, as the CPU is supposed to sleep from now */
3277     gen_exception_err(ctx, EXCP_HLT, 1);
3278 }
3279
3280 /***                         Floating-point load                           ***/
3281 #define GEN_LDF(name, ldop, opc, type)                                        \
3282 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3283 {                                                                             \
3284     TCGv EA;                                                                  \
3285     if (unlikely(!ctx->fpu_enabled)) {                                        \
3286         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3287         return;                                                               \
3288     }                                                                         \
3289     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3290     EA = tcg_temp_new();                                                      \
3291     gen_addr_imm_index(ctx, EA, 0);                                           \
3292     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3293     tcg_temp_free(EA);                                                        \
3294 }
3295
3296 #define GEN_LDUF(name, ldop, opc, type)                                       \
3297 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3298 {                                                                             \
3299     TCGv EA;                                                                  \
3300     if (unlikely(!ctx->fpu_enabled)) {                                        \
3301         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3302         return;                                                               \
3303     }                                                                         \
3304     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3305         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3306         return;                                                               \
3307     }                                                                         \
3308     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3309     EA = tcg_temp_new();                                                      \
3310     gen_addr_imm_index(ctx, EA, 0);                                           \
3311     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3312     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3313     tcg_temp_free(EA);                                                        \
3314 }
3315
3316 #define GEN_LDUXF(name, ldop, opc, type)                                      \
3317 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3318 {                                                                             \
3319     TCGv EA;                                                                  \
3320     if (unlikely(!ctx->fpu_enabled)) {                                        \
3321         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3322         return;                                                               \
3323     }                                                                         \
3324     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3325         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3326         return;                                                               \
3327     }                                                                         \
3328     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3329     EA = tcg_temp_new();                                                      \
3330     gen_addr_reg_index(ctx, EA);                                              \
3331     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3332     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3333     tcg_temp_free(EA);                                                        \
3334 }
3335
3336 #define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3337 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3338 {                                                                             \
3339     TCGv EA;                                                                  \
3340     if (unlikely(!ctx->fpu_enabled)) {                                        \
3341         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3342         return;                                                               \
3343     }                                                                         \
3344     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3345     EA = tcg_temp_new();                                                      \
3346     gen_addr_reg_index(ctx, EA);                                              \
3347     gen_qemu_##ldop(ctx, cpu_fpr[rD(ctx->opcode)], EA);                       \
3348     tcg_temp_free(EA);                                                        \
3349 }
3350
3351 #define GEN_LDFS(name, ldop, op, type)                                        \
3352 GEN_LDF(name, ldop, op | 0x20, type);                                         \
3353 GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3354 GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3355 GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3356
3357 static always_inline void gen_qemu_ld32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3358 {
3359     TCGv t0 = tcg_temp_new();
3360     TCGv_i32 t1 = tcg_temp_new_i32();
3361     gen_qemu_ld32u(ctx, t0, arg2);
3362     tcg_gen_trunc_tl_i32(t1, t0);
3363     tcg_temp_free(t0);
3364     gen_helper_float32_to_float64(arg1, t1);
3365     tcg_temp_free_i32(t1);
3366 }
3367
3368  /* lfd lfdu lfdux lfdx */
3369 GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3370  /* lfs lfsu lfsux lfsx */
3371 GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3372
3373 /***                         Floating-point store                          ***/
3374 #define GEN_STF(name, stop, opc, type)                                        \
3375 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3376 {                                                                             \
3377     TCGv EA;                                                                  \
3378     if (unlikely(!ctx->fpu_enabled)) {                                        \
3379         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3380         return;                                                               \
3381     }                                                                         \
3382     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3383     EA = tcg_temp_new();                                                      \
3384     gen_addr_imm_index(ctx, EA, 0);                                           \
3385     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3386     tcg_temp_free(EA);                                                        \
3387 }
3388
3389 #define GEN_STUF(name, stop, opc, type)                                       \
3390 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3391 {                                                                             \
3392     TCGv EA;                                                                  \
3393     if (unlikely(!ctx->fpu_enabled)) {                                        \
3394         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3395         return;                                                               \
3396     }                                                                         \
3397     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3398         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3399         return;                                                               \
3400     }                                                                         \
3401     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3402     EA = tcg_temp_new();                                                      \
3403     gen_addr_imm_index(ctx, EA, 0);                                           \
3404     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3405     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3406     tcg_temp_free(EA);                                                        \
3407 }
3408
3409 #define GEN_STUXF(name, stop, opc, type)                                      \
3410 GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3411 {                                                                             \
3412     TCGv EA;                                                                  \
3413     if (unlikely(!ctx->fpu_enabled)) {                                        \
3414         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3415         return;                                                               \
3416     }                                                                         \
3417     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3418         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
3419         return;                                                               \
3420     }                                                                         \
3421     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3422     EA = tcg_temp_new();                                                      \
3423     gen_addr_reg_index(ctx, EA);                                              \
3424     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3425     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3426     tcg_temp_free(EA);                                                        \
3427 }
3428
3429 #define GEN_STXF(name, stop, opc2, opc3, type)                                \
3430 GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3431 {                                                                             \
3432     TCGv EA;                                                                  \
3433     if (unlikely(!ctx->fpu_enabled)) {                                        \
3434         gen_exception(ctx, POWERPC_EXCP_FPU);                                 \
3435         return;                                                               \
3436     }                                                                         \
3437     gen_set_access_type(ctx, ACCESS_FLOAT);                                   \
3438     EA = tcg_temp_new();                                                      \
3439     gen_addr_reg_index(ctx, EA);                                              \
3440     gen_qemu_##stop(ctx, cpu_fpr[rS(ctx->opcode)], EA);                       \
3441     tcg_temp_free(EA);                                                        \
3442 }
3443
3444 #define GEN_STFS(name, stop, op, type)                                        \
3445 GEN_STF(name, stop, op | 0x20, type);                                         \
3446 GEN_STUF(name, stop, op | 0x21, type);                                        \
3447 GEN_STUXF(name, stop, op | 0x01, type);                                       \
3448 GEN_STXF(name, stop, 0x17, op | 0x00, type)
3449
3450 static always_inline void gen_qemu_st32fs(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3451 {
3452     TCGv_i32 t0 = tcg_temp_new_i32();
3453     TCGv t1 = tcg_temp_new();
3454     gen_helper_float64_to_float32(t0, arg1);
3455     tcg_gen_extu_i32_tl(t1, t0);
3456     tcg_temp_free_i32(t0);
3457     gen_qemu_st32(ctx, t1, arg2);
3458     tcg_temp_free(t1);
3459 }
3460
3461 /* stfd stfdu stfdux stfdx */
3462 GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3463 /* stfs stfsu stfsux stfsx */
3464 GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3465
3466 /* Optional: */
3467 static always_inline void gen_qemu_st32fiw(DisasContext *ctx, TCGv_i64 arg1, TCGv arg2)
3468 {
3469     TCGv t0 = tcg_temp_new();
3470     tcg_gen_trunc_i64_tl(t0, arg1),
3471     gen_qemu_st32(ctx, t0, arg2);
3472     tcg_temp_free(t0);
3473 }
3474 /* stfiwx */
3475 GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3476
3477 /***                                Branch                                 ***/
3478 static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3479                                        target_ulong dest)
3480 {
3481     TranslationBlock *tb;
3482     tb = ctx->tb;
3483 #if defined(TARGET_PPC64)
3484     if (!ctx->sf_mode)
3485         dest = (uint32_t) dest;
3486 #endif
3487     if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3488         likely(!ctx->singlestep_enabled)) {
3489         tcg_gen_goto_tb(n);
3490         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3491         tcg_gen_exit_tb((long)tb + n);
3492     } else {
3493         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3494         if (unlikely(ctx->singlestep_enabled)) {
3495             if ((ctx->singlestep_enabled &
3496                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3497                 ctx->exception == POWERPC_EXCP_BRANCH) {
3498                 target_ulong tmp = ctx->nip;
3499                 ctx->nip = dest;
3500                 gen_exception(ctx, POWERPC_EXCP_TRACE);
3501                 ctx->nip = tmp;
3502             }
3503             if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3504                 gen_debug_exception(ctx);
3505             }
3506         }
3507         tcg_gen_exit_tb(0);
3508     }
3509 }
3510
3511 static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3512 {
3513 #if defined(TARGET_PPC64)
3514     if (ctx->sf_mode == 0)
3515         tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3516     else
3517 #endif
3518         tcg_gen_movi_tl(cpu_lr, nip);
3519 }
3520
3521 /* b ba bl bla */
3522 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3523 {
3524     target_ulong li, target;
3525
3526     ctx->exception = POWERPC_EXCP_BRANCH;
3527     /* sign extend LI */
3528 #if defined(TARGET_PPC64)
3529     if (ctx->sf_mode)
3530         li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3531     else
3532 #endif
3533         li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3534     if (likely(AA(ctx->opcode) == 0))
3535         target = ctx->nip + li - 4;
3536     else
3537         target = li;
3538     if (LK(ctx->opcode))
3539         gen_setlr(ctx, ctx->nip);
3540     gen_goto_tb(ctx, 0, target);
3541 }
3542
3543 #define BCOND_IM  0
3544 #define BCOND_LR  1
3545 #define BCOND_CTR 2
3546
3547 static always_inline void gen_bcond (DisasContext *ctx, int type)
3548 {
3549     uint32_t bo = BO(ctx->opcode);
3550     int l1 = gen_new_label();
3551     TCGv target;
3552
3553     ctx->exception = POWERPC_EXCP_BRANCH;
3554     if (type == BCOND_LR || type == BCOND_CTR) {
3555         target = tcg_temp_local_new();
3556         if (type == BCOND_CTR)
3557             tcg_gen_mov_tl(target, cpu_ctr);
3558         else
3559             tcg_gen_mov_tl(target, cpu_lr);
3560     }
3561     if (LK(ctx->opcode))
3562         gen_setlr(ctx, ctx->nip);
3563     l1 = gen_new_label();
3564     if ((bo & 0x4) == 0) {
3565         /* Decrement and test CTR */
3566         TCGv temp = tcg_temp_new();
3567         if (unlikely(type == BCOND_CTR)) {
3568             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3569             return;
3570         }
3571         tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3572 #if defined(TARGET_PPC64)
3573         if (!ctx->sf_mode)
3574             tcg_gen_ext32u_tl(temp, cpu_ctr);
3575         else
3576 #endif
3577             tcg_gen_mov_tl(temp, cpu_ctr);
3578         if (bo & 0x2) {
3579             tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3580         } else {
3581             tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3582         }
3583         tcg_temp_free(temp);
3584     }
3585     if ((bo & 0x10) == 0) {
3586         /* Test CR */
3587         uint32_t bi = BI(ctx->opcode);
3588         uint32_t mask = 1 << (3 - (bi & 0x03));
3589         TCGv_i32 temp = tcg_temp_new_i32();
3590
3591         if (bo & 0x8) {
3592             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3593             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3594         } else {
3595             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3596             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3597         }
3598         tcg_temp_free_i32(temp);
3599     }
3600     if (type == BCOND_IM) {
3601         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3602         if (likely(AA(ctx->opcode) == 0)) {
3603             gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3604         } else {
3605             gen_goto_tb(ctx, 0, li);
3606         }
3607         gen_set_label(l1);
3608         gen_goto_tb(ctx, 1, ctx->nip);
3609     } else {
3610 #if defined(TARGET_PPC64)
3611         if (!(ctx->sf_mode))
3612             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3613         else
3614 #endif
3615             tcg_gen_andi_tl(cpu_nip, target, ~3);
3616         tcg_gen_exit_tb(0);
3617         gen_set_label(l1);
3618 #if defined(TARGET_PPC64)
3619         if (!(ctx->sf_mode))
3620             tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3621         else
3622 #endif
3623             tcg_gen_movi_tl(cpu_nip, ctx->nip);
3624         tcg_gen_exit_tb(0);
3625     }
3626 }
3627
3628 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3629 {
3630     gen_bcond(ctx, BCOND_IM);
3631 }
3632
3633 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3634 {
3635     gen_bcond(ctx, BCOND_CTR);
3636 }
3637
3638 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3639 {
3640     gen_bcond(ctx, BCOND_LR);
3641 }
3642
3643 /***                      Condition register logical                       ***/
3644 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3645 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
3646 {                                                                             \
3647     uint8_t bitmask;                                                          \
3648     int sh;                                                                   \
3649     TCGv_i32 t0, t1;                                                          \
3650     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3651     t0 = tcg_temp_new_i32();                                                  \
3652     if (sh > 0)                                                               \
3653         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3654     else if (sh < 0)                                                          \
3655         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3656     else                                                                      \
3657         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3658     t1 = tcg_temp_new_i32();                                                  \
3659     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3660     if (sh > 0)                                                               \
3661         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3662     else if (sh < 0)                                                          \
3663         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3664     else                                                                      \
3665         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3666     tcg_op(t0, t0, t1);                                                       \
3667     bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3668     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3669     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3670     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3671     tcg_temp_free_i32(t0);                                                    \
3672     tcg_temp_free_i32(t1);                                                    \
3673 }
3674
3675 /* crand */
3676 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3677 /* crandc */
3678 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3679 /* creqv */
3680 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3681 /* crnand */
3682 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3683 /* crnor */
3684 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3685 /* cror */
3686 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3687 /* crorc */
3688 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3689 /* crxor */
3690 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3691 /* mcrf */
3692 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3693 {
3694     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3695 }
3696
3697 /***                           System linkage                              ***/
3698 /* rfi (mem_idx only) */
3699 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3700 {
3701 #if defined(CONFIG_USER_ONLY)
3702     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3703 #else
3704     /* Restore CPU state */
3705     if (unlikely(!ctx->mem_idx)) {
3706         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3707         return;
3708     }
3709     gen_helper_rfi();
3710     gen_sync_exception(ctx);
3711 #endif
3712 }
3713
3714 #if defined(TARGET_PPC64)
3715 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3716 {
3717 #if defined(CONFIG_USER_ONLY)
3718     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3719 #else
3720     /* Restore CPU state */
3721     if (unlikely(!ctx->mem_idx)) {
3722         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3723         return;
3724     }
3725     gen_helper_rfid();
3726     gen_sync_exception(ctx);
3727 #endif
3728 }
3729
3730 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3731 {
3732 #if defined(CONFIG_USER_ONLY)
3733     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3734 #else
3735     /* Restore CPU state */
3736     if (unlikely(ctx->mem_idx <= 1)) {
3737         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
3738         return;
3739     }
3740     gen_helper_hrfid();
3741     gen_sync_exception(ctx);
3742 #endif
3743 }
3744 #endif
3745
3746 /* sc */
3747 #if defined(CONFIG_USER_ONLY)
3748 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3749 #else
3750 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3751 #endif
3752 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3753 {
3754     uint32_t lev;
3755
3756     lev = (ctx->opcode >> 5) & 0x7F;
3757     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
3758 }
3759
3760 /***                                Trap                                   ***/
3761 /* tw */
3762 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3763 {
3764     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3765     /* Update the nip since this might generate a trap exception */
3766     gen_update_nip(ctx, ctx->nip);
3767     gen_helper_tw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3768     tcg_temp_free_i32(t0);
3769 }
3770
3771 /* twi */
3772 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3773 {
3774     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3775     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3776     /* Update the nip since this might generate a trap exception */
3777     gen_update_nip(ctx, ctx->nip);
3778     gen_helper_tw(cpu_gpr[rA(ctx->opcode)], t0, t1);
3779     tcg_temp_free(t0);
3780     tcg_temp_free_i32(t1);
3781 }
3782
3783 #if defined(TARGET_PPC64)
3784 /* td */
3785 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3786 {
3787     TCGv_i32 t0 = tcg_const_i32(TO(ctx->opcode));
3788     /* Update the nip since this might generate a trap exception */
3789     gen_update_nip(ctx, ctx->nip);
3790     gen_helper_td(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
3791     tcg_temp_free_i32(t0);
3792 }
3793
3794 /* tdi */
3795 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3796 {
3797     TCGv t0 = tcg_const_tl(SIMM(ctx->opcode));
3798     TCGv_i32 t1 = tcg_const_i32(TO(ctx->opcode));
3799     /* Update the nip since this might generate a trap exception */
3800     gen_update_nip(ctx, ctx->nip);
3801     gen_helper_td(cpu_gpr[rA(ctx->opcode)], t0, t1);
3802     tcg_temp_free(t0);
3803     tcg_temp_free_i32(t1);
3804 }
3805 #endif
3806
3807 /***                          Processor control                            ***/
3808 /* mcrxr */
3809 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3810 {
3811     tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3812     tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3813     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
3814 }
3815
3816 /* mfcr */
3817 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3818 {
3819     uint32_t crm, crn;
3820
3821     if (likely(ctx->opcode & 0x00100000)) {
3822         crm = CRM(ctx->opcode);
3823         if (likely((crm ^ (crm - 1)) == 0)) {
3824             crn = ffs(crm);
3825             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3826         }
3827     } else {
3828         gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3829     }
3830 }
3831
3832 /* mfmsr */
3833 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3834 {
3835 #if defined(CONFIG_USER_ONLY)
3836     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3837 #else
3838     if (unlikely(!ctx->mem_idx)) {
3839         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3840         return;
3841     }
3842     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
3843 #endif
3844 }
3845
3846 #if 1
3847 #define SPR_NOACCESS ((void *)(-1UL))
3848 #else
3849 static void spr_noaccess (void *opaque, int sprn)
3850 {
3851     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3852     printf("ERROR: try to access SPR %d !\n", sprn);
3853 }
3854 #define SPR_NOACCESS (&spr_noaccess)
3855 #endif
3856
3857 /* mfspr */
3858 static always_inline void gen_op_mfspr (DisasContext *ctx)
3859 {
3860     void (*read_cb)(void *opaque, int gprn, int sprn);
3861     uint32_t sprn = SPR(ctx->opcode);
3862
3863 #if !defined(CONFIG_USER_ONLY)
3864     if (ctx->mem_idx == 2)
3865         read_cb = ctx->spr_cb[sprn].hea_read;
3866     else if (ctx->mem_idx)
3867         read_cb = ctx->spr_cb[sprn].oea_read;
3868     else
3869 #endif
3870         read_cb = ctx->spr_cb[sprn].uea_read;
3871     if (likely(read_cb != NULL)) {
3872         if (likely(read_cb != SPR_NOACCESS)) {
3873             (*read_cb)(ctx, rD(ctx->opcode), sprn);
3874         } else {
3875             /* Privilege exception */
3876             /* This is a hack to avoid warnings when running Linux:
3877              * this OS breaks the PowerPC virtualisation model,
3878              * allowing userland application to read the PVR
3879              */
3880             if (sprn != SPR_PVR) {
3881                 if (loglevel != 0) {
3882                     fprintf(logfile, "Trying to read privileged spr %d %03x at "
3883                             ADDRX "\n", sprn, sprn, ctx->nip);
3884                 }
3885                 printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3886                        sprn, sprn, ctx->nip);
3887             }
3888             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3889         }
3890     } else {
3891         /* Not defined */
3892         if (loglevel != 0) {
3893             fprintf(logfile, "Trying to read invalid spr %d %03x at "
3894                     ADDRX "\n", sprn, sprn, ctx->nip);
3895         }
3896         printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3897                sprn, sprn, ctx->nip);
3898         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
3899     }
3900 }
3901
3902 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3903 {
3904     gen_op_mfspr(ctx);
3905 }
3906
3907 /* mftb */
3908 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3909 {
3910     gen_op_mfspr(ctx);
3911 }
3912
3913 /* mtcrf */
3914 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3915 {
3916     uint32_t crm, crn;
3917
3918     crm = CRM(ctx->opcode);
3919     if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3920         TCGv_i32 temp = tcg_temp_new_i32();
3921         crn = ffs(crm);
3922         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3923         tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3924         tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3925         tcg_temp_free_i32(temp);
3926     } else {
3927         TCGv_i32 temp = tcg_const_i32(crm);
3928         gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3929         tcg_temp_free_i32(temp);
3930     }
3931 }
3932
3933 /* mtmsr */
3934 #if defined(TARGET_PPC64)
3935 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3936 {
3937 #if defined(CONFIG_USER_ONLY)
3938     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3939 #else
3940     if (unlikely(!ctx->mem_idx)) {
3941         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3942         return;
3943     }
3944     if (ctx->opcode & 0x00010000) {
3945         /* Special form that does not need any synchronisation */
3946         TCGv t0 = tcg_temp_new();
3947         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3948         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3949         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3950         tcg_temp_free(t0);
3951     } else {
3952         /* XXX: we need to update nip before the store
3953          *      if we enter power saving mode, we will exit the loop
3954          *      directly from ppc_store_msr
3955          */
3956         gen_update_nip(ctx, ctx->nip);
3957         gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
3958         /* Must stop the translation as machine state (may have) changed */
3959         /* Note that mtmsr is not always defined as context-synchronizing */
3960         gen_stop_exception(ctx);
3961     }
3962 #endif
3963 }
3964 #endif
3965
3966 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
3967 {
3968 #if defined(CONFIG_USER_ONLY)
3969     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3970 #else
3971     if (unlikely(!ctx->mem_idx)) {
3972         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
3973         return;
3974     }
3975     if (ctx->opcode & 0x00010000) {
3976         /* Special form that does not need any synchronisation */
3977         TCGv t0 = tcg_temp_new();
3978         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1 << MSR_RI) | (1 << MSR_EE));
3979         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~((1 << MSR_RI) | (1 << MSR_EE)));
3980         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
3981         tcg_temp_free(t0);
3982     } else {
3983         /* XXX: we need to update nip before the store
3984          *      if we enter power saving mode, we will exit the loop
3985          *      directly from ppc_store_msr
3986          */
3987         gen_update_nip(ctx, ctx->nip);
3988 #if defined(TARGET_PPC64)
3989         if (!ctx->sf_mode) {
3990             TCGv t0 = tcg_temp_new();
3991             TCGv t1 = tcg_temp_new();
3992             tcg_gen_andi_tl(t0, cpu_msr, 0xFFFFFFFF00000000ULL);
3993             tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
3994             tcg_gen_or_tl(t0, t0, t1);
3995             tcg_temp_free(t1);
3996             gen_helper_store_msr(t0);
3997             tcg_temp_free(t0);
3998         } else
3999 #endif
4000             gen_helper_store_msr(cpu_gpr[rS(ctx->opcode)]);
4001         /* Must stop the translation as machine state (may have) changed */
4002         /* Note that mtmsr is not always defined as context-synchronizing */
4003         gen_stop_exception(ctx);
4004     }
4005 #endif
4006 }
4007
4008 /* mtspr */
4009 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4010 {
4011     void (*write_cb)(void *opaque, int sprn, int gprn);
4012     uint32_t sprn = SPR(ctx->opcode);
4013
4014 #if !defined(CONFIG_USER_ONLY)
4015     if (ctx->mem_idx == 2)
4016         write_cb = ctx->spr_cb[sprn].hea_write;
4017     else if (ctx->mem_idx)
4018         write_cb = ctx->spr_cb[sprn].oea_write;
4019     else
4020 #endif
4021         write_cb = ctx->spr_cb[sprn].uea_write;
4022     if (likely(write_cb != NULL)) {
4023         if (likely(write_cb != SPR_NOACCESS)) {
4024             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4025         } else {
4026             /* Privilege exception */
4027             if (loglevel != 0) {
4028                 fprintf(logfile, "Trying to write privileged spr %d %03x at "
4029                         ADDRX "\n", sprn, sprn, ctx->nip);
4030             }
4031             printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4032                    sprn, sprn, ctx->nip);
4033             gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4034         }
4035     } else {
4036         /* Not defined */
4037         if (loglevel != 0) {
4038             fprintf(logfile, "Trying to write invalid spr %d %03x at "
4039                     ADDRX "\n", sprn, sprn, ctx->nip);
4040         }
4041         printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4042                sprn, sprn, ctx->nip);
4043         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4044     }
4045 }
4046
4047 /***                         Cache management                              ***/
4048 /* dcbf */
4049 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
4050 {
4051     /* XXX: specification says this is treated as a load by the MMU */
4052     TCGv t0;
4053     gen_set_access_type(ctx, ACCESS_CACHE);
4054     t0 = tcg_temp_new();
4055     gen_addr_reg_index(ctx, t0);
4056     gen_qemu_ld8u(ctx, t0, t0);
4057     tcg_temp_free(t0);
4058 }
4059
4060 /* dcbi (Supervisor only) */
4061 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
4062 {
4063 #if defined(CONFIG_USER_ONLY)
4064     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4065 #else
4066     TCGv EA, val;
4067     if (unlikely(!ctx->mem_idx)) {
4068         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4069         return;
4070     }
4071     EA = tcg_temp_new();
4072     gen_set_access_type(ctx, ACCESS_CACHE);
4073     gen_addr_reg_index(ctx, EA);
4074     val = tcg_temp_new();
4075     /* XXX: specification says this should be treated as a store by the MMU */
4076     gen_qemu_ld8u(ctx, val, EA);
4077     gen_qemu_st8(ctx, val, EA);
4078     tcg_temp_free(val);
4079     tcg_temp_free(EA);
4080 #endif
4081 }
4082
4083 /* dcdst */
4084 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
4085 {
4086     /* XXX: specification say this is treated as a load by the MMU */
4087     TCGv t0;
4088     gen_set_access_type(ctx, ACCESS_CACHE);
4089     t0 = tcg_temp_new();
4090     gen_addr_reg_index(ctx, t0);
4091     gen_qemu_ld8u(ctx, t0, t0);
4092     tcg_temp_free(t0);
4093 }
4094
4095 /* dcbt */
4096 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
4097 {
4098     /* interpreted as no-op */
4099     /* XXX: specification say this is treated as a load by the MMU
4100      *      but does not generate any exception
4101      */
4102 }
4103
4104 /* dcbtst */
4105 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
4106 {
4107     /* interpreted as no-op */
4108     /* XXX: specification say this is treated as a load by the MMU
4109      *      but does not generate any exception
4110      */
4111 }
4112
4113 /* dcbz */
4114 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
4115 {
4116     TCGv t0;
4117     gen_set_access_type(ctx, ACCESS_CACHE);
4118     /* NIP cannot be restored if the memory exception comes from an helper */
4119     gen_update_nip(ctx, ctx->nip - 4);
4120     t0 = tcg_temp_new();
4121     gen_addr_reg_index(ctx, t0);
4122     gen_helper_dcbz(t0);
4123     tcg_temp_free(t0);
4124 }
4125
4126 GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4127 {
4128     TCGv t0;
4129     gen_set_access_type(ctx, ACCESS_CACHE);
4130     /* NIP cannot be restored if the memory exception comes from an helper */
4131     gen_update_nip(ctx, ctx->nip - 4);
4132     t0 = tcg_temp_new();
4133     gen_addr_reg_index(ctx, t0);
4134     if (ctx->opcode & 0x00200000)
4135         gen_helper_dcbz(t0);
4136     else
4137         gen_helper_dcbz_970(t0);
4138     tcg_temp_free(t0);
4139 }
4140
4141 /* icbi */
4142 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
4143 {
4144     TCGv t0;
4145     gen_set_access_type(ctx, ACCESS_CACHE);
4146     /* NIP cannot be restored if the memory exception comes from an helper */
4147     gen_update_nip(ctx, ctx->nip - 4);
4148     t0 = tcg_temp_new();
4149     gen_addr_reg_index(ctx, t0);
4150     gen_helper_icbi(t0);
4151     tcg_temp_free(t0);
4152 }
4153
4154 /* Optional: */
4155 /* dcba */
4156 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
4157 {
4158     /* interpreted as no-op */
4159     /* XXX: specification say this is treated as a store by the MMU
4160      *      but does not generate any exception
4161      */
4162 }
4163
4164 /***                    Segment register manipulation                      ***/
4165 /* Supervisor only: */
4166 /* mfsr */
4167 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4168 {
4169 #if defined(CONFIG_USER_ONLY)
4170     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4171 #else
4172     TCGv t0;
4173     if (unlikely(!ctx->mem_idx)) {
4174         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4175         return;
4176     }
4177     t0 = tcg_const_tl(SR(ctx->opcode));
4178     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4179     tcg_temp_free(t0);
4180 #endif
4181 }
4182
4183 /* mfsrin */
4184 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4185 {
4186 #if defined(CONFIG_USER_ONLY)
4187     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4188 #else
4189     TCGv t0;
4190     if (unlikely(!ctx->mem_idx)) {
4191         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4192         return;
4193     }
4194     t0 = tcg_temp_new();
4195     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4196     tcg_gen_andi_tl(t0, t0, 0xF);
4197     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], t0);
4198     tcg_temp_free(t0);
4199 #endif
4200 }
4201
4202 /* mtsr */
4203 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4204 {
4205 #if defined(CONFIG_USER_ONLY)
4206     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4207 #else
4208     TCGv t0;
4209     if (unlikely(!ctx->mem_idx)) {
4210         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4211         return;
4212     }
4213     t0 = tcg_const_tl(SR(ctx->opcode));
4214     gen_helper_store_sr(t0, cpu_gpr[rS(ctx->opcode)]);
4215     tcg_temp_free(t0);
4216 #endif
4217 }
4218
4219 /* mtsrin */
4220 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4221 {
4222 #if defined(CONFIG_USER_ONLY)
4223     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4224 #else
4225     TCGv t0;
4226     if (unlikely(!ctx->mem_idx)) {
4227         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4228         return;
4229     }
4230     t0 = tcg_temp_new();
4231     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4232     tcg_gen_andi_tl(t0, t0, 0xF);
4233     gen_helper_store_sr(t0, cpu_gpr[rD(ctx->opcode)]);
4234     tcg_temp_free(t0);
4235 #endif
4236 }
4237
4238 #if defined(TARGET_PPC64)
4239 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4240 /* mfsr */
4241 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4242 {
4243 #if defined(CONFIG_USER_ONLY)
4244     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4245 #else
4246     TCGv t0;
4247     if (unlikely(!ctx->mem_idx)) {
4248         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4249         return;
4250     }
4251     t0 = tcg_const_tl(SR(ctx->opcode));
4252     gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
4253     tcg_temp_free(t0);
4254 #endif
4255 }
4256
4257 /* mfsrin */
4258 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4259              PPC_SEGMENT_64B)
4260 {
4261 #if defined(CONFIG_USER_ONLY)
4262     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4263 #else
4264     TCGv t0;
4265     if (unlikely(!ctx->mem_idx)) {
4266         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4267         return;
4268     }
4269     t0 = tcg_temp_new();
4270     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4271     tcg_gen_andi_tl(t0, t0, 0xF);
4272     gen_helper_load_slb(cpu_gpr[rD(ctx->opcode)], t0);
4273     tcg_temp_free(t0);
4274 #endif
4275 }
4276
4277 /* mtsr */
4278 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4279 {
4280 #if defined(CONFIG_USER_ONLY)
4281     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4282 #else
4283     TCGv t0;
4284     if (unlikely(!ctx->mem_idx)) {
4285         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4286         return;
4287     }
4288     t0 = tcg_const_tl(SR(ctx->opcode));
4289     gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
4290     tcg_temp_free(t0);
4291 #endif
4292 }
4293
4294 /* mtsrin */
4295 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4296              PPC_SEGMENT_64B)
4297 {
4298 #if defined(CONFIG_USER_ONLY)
4299     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4300 #else
4301     TCGv t0;
4302     if (unlikely(!ctx->mem_idx)) {
4303         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4304         return;
4305     }
4306     t0 = tcg_temp_new();
4307     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 28);
4308     tcg_gen_andi_tl(t0, t0, 0xF);
4309     gen_helper_store_slb(t0, cpu_gpr[rS(ctx->opcode)]);
4310     tcg_temp_free(t0);
4311 #endif
4312 }
4313 #endif /* defined(TARGET_PPC64) */
4314
4315 /***                      Lookaside buffer management                      ***/
4316 /* Optional & mem_idx only: */
4317 /* tlbia */
4318 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4319 {
4320 #if defined(CONFIG_USER_ONLY)
4321     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4322 #else
4323     if (unlikely(!ctx->mem_idx)) {
4324         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4325         return;
4326     }
4327     gen_helper_tlbia();
4328 #endif
4329 }
4330
4331 /* tlbie */
4332 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4333 {
4334 #if defined(CONFIG_USER_ONLY)
4335     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4336 #else
4337     if (unlikely(!ctx->mem_idx)) {
4338         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4339         return;
4340     }
4341 #if defined(TARGET_PPC64)
4342     if (!ctx->sf_mode) {
4343         TCGv t0 = tcg_temp_new();
4344         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4345         gen_helper_tlbie(t0);
4346         tcg_temp_free(t0);
4347     } else
4348 #endif
4349         gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
4350 #endif
4351 }
4352
4353 /* tlbsync */
4354 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4355 {
4356 #if defined(CONFIG_USER_ONLY)
4357     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4358 #else
4359     if (unlikely(!ctx->mem_idx)) {
4360         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4361         return;
4362     }
4363     /* This has no effect: it should ensure that all previous
4364      * tlbie have completed
4365      */
4366     gen_stop_exception(ctx);
4367 #endif
4368 }
4369
4370 #if defined(TARGET_PPC64)
4371 /* slbia */
4372 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4373 {
4374 #if defined(CONFIG_USER_ONLY)
4375     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4376 #else
4377     if (unlikely(!ctx->mem_idx)) {
4378         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4379         return;
4380     }
4381     gen_helper_slbia();
4382 #endif
4383 }
4384
4385 /* slbie */
4386 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4387 {
4388 #if defined(CONFIG_USER_ONLY)
4389     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4390 #else
4391     if (unlikely(!ctx->mem_idx)) {
4392         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
4393         return;
4394     }
4395     gen_helper_slbie(cpu_gpr[rB(ctx->opcode)]);
4396 #endif
4397 }
4398 #endif
4399
4400 /***                              External control                         ***/
4401 /* Optional: */
4402 /* eciwx */
4403 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4404 {
4405     TCGv t0;
4406     /* Should check EAR[E] ! */
4407     gen_set_access_type(ctx, ACCESS_EXT);
4408     t0 = tcg_temp_new();
4409     gen_addr_reg_index(ctx, t0);
4410     gen_check_align(ctx, t0, 0x03);
4411     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4412     tcg_temp_free(t0);
4413 }
4414
4415 /* ecowx */
4416 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4417 {
4418     TCGv t0;
4419     /* Should check EAR[E] ! */
4420     gen_set_access_type(ctx, ACCESS_EXT);
4421     t0 = tcg_temp_new();
4422     gen_addr_reg_index(ctx, t0);
4423     gen_check_align(ctx, t0, 0x03);
4424     gen_qemu_st32(ctx, cpu_gpr[rD(ctx->opcode)], t0);
4425     tcg_temp_free(t0);
4426 }
4427
4428 /* PowerPC 601 specific instructions */
4429 /* abs - abs. */
4430 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4431 {
4432     int l1 = gen_new_label();
4433     int l2 = gen_new_label();
4434     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l1);
4435     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4436     tcg_gen_br(l2);
4437     gen_set_label(l1);
4438     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4439     gen_set_label(l2);
4440     if (unlikely(Rc(ctx->opcode) != 0))
4441         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4442 }
4443
4444 /* abso - abso. */
4445 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4446 {
4447     int l1 = gen_new_label();
4448     int l2 = gen_new_label();
4449     int l3 = gen_new_label();
4450     /* Start with XER OV disabled, the most likely case */
4451     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4452     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rA(ctx->opcode)], 0, l2);
4453     tcg_gen_brcondi_tl(TCG_COND_NE, cpu_gpr[rA(ctx->opcode)], 0x80000000, l1);
4454     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4455     tcg_gen_br(l2);
4456     gen_set_label(l1);
4457     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4458     tcg_gen_br(l3);
4459     gen_set_label(l2);
4460     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4461     gen_set_label(l3);
4462     if (unlikely(Rc(ctx->opcode) != 0))
4463         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4464 }
4465
4466 /* clcs */
4467 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4468 {
4469     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
4470     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], t0);
4471     tcg_temp_free_i32(t0);
4472     /* Rc=1 sets CR0 to an undefined state */
4473 }
4474
4475 /* div - div. */
4476 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4477 {
4478     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4479     if (unlikely(Rc(ctx->opcode) != 0))
4480         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4481 }
4482
4483 /* divo - divo. */
4484 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4485 {
4486     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4487     if (unlikely(Rc(ctx->opcode) != 0))
4488         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4489 }
4490
4491 /* divs - divs. */
4492 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4493 {
4494     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4495     if (unlikely(Rc(ctx->opcode) != 0))
4496         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4497 }
4498
4499 /* divso - divso. */
4500 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4501 {
4502     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4503     if (unlikely(Rc(ctx->opcode) != 0))
4504         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4505 }
4506
4507 /* doz - doz. */
4508 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4509 {
4510     int l1 = gen_new_label();
4511     int l2 = gen_new_label();
4512     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4513     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4514     tcg_gen_br(l2);
4515     gen_set_label(l1);
4516     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4517     gen_set_label(l2);
4518     if (unlikely(Rc(ctx->opcode) != 0))
4519         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4520 }
4521
4522 /* dozo - dozo. */
4523 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4524 {
4525     int l1 = gen_new_label();
4526     int l2 = gen_new_label();
4527     TCGv t0 = tcg_temp_new();
4528     TCGv t1 = tcg_temp_new();
4529     TCGv t2 = tcg_temp_new();
4530     /* Start with XER OV disabled, the most likely case */
4531     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4532     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], l1);
4533     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4534     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4535     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
4536     tcg_gen_andc_tl(t1, t1, t2);
4537     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
4538     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4539     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4540     tcg_gen_br(l2);
4541     gen_set_label(l1);
4542     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4543     gen_set_label(l2);
4544     tcg_temp_free(t0);
4545     tcg_temp_free(t1);
4546     tcg_temp_free(t2);
4547     if (unlikely(Rc(ctx->opcode) != 0))
4548         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4549 }
4550
4551 /* dozi */
4552 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4553 {
4554     target_long simm = SIMM(ctx->opcode);
4555     int l1 = gen_new_label();
4556     int l2 = gen_new_label();
4557     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
4558     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
4559     tcg_gen_br(l2);
4560     gen_set_label(l1);
4561     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
4562     gen_set_label(l2);
4563     if (unlikely(Rc(ctx->opcode) != 0))
4564         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4565 }
4566
4567 /* lscbx - lscbx. */
4568 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
4569 {
4570     TCGv t0 = tcg_temp_new();
4571     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
4572     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
4573     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
4574
4575     gen_addr_reg_index(ctx, t0);
4576     /* NIP cannot be restored if the memory exception comes from an helper */
4577     gen_update_nip(ctx, ctx->nip - 4);
4578     gen_helper_lscbx(t0, t0, t1, t2, t3);
4579     tcg_temp_free_i32(t1);
4580     tcg_temp_free_i32(t2);
4581     tcg_temp_free_i32(t3);
4582     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4583     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
4584     if (unlikely(Rc(ctx->opcode) != 0))
4585         gen_set_Rc0(ctx, t0);
4586     tcg_temp_free(t0);
4587 }
4588
4589 /* maskg - maskg. */
4590 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4591 {
4592     int l1 = gen_new_label();
4593     TCGv t0 = tcg_temp_new();
4594     TCGv t1 = tcg_temp_new();
4595     TCGv t2 = tcg_temp_new();
4596     TCGv t3 = tcg_temp_new();
4597     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
4598     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4599     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
4600     tcg_gen_addi_tl(t2, t0, 1);
4601     tcg_gen_shr_tl(t2, t3, t2);
4602     tcg_gen_shr_tl(t3, t3, t1);
4603     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
4604     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
4605     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4606     gen_set_label(l1);
4607     tcg_temp_free(t0);
4608     tcg_temp_free(t1);
4609     tcg_temp_free(t2);
4610     tcg_temp_free(t3);
4611     if (unlikely(Rc(ctx->opcode) != 0))
4612         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4613 }
4614
4615 /* maskir - maskir. */
4616 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4617 {
4618     TCGv t0 = tcg_temp_new();
4619     TCGv t1 = tcg_temp_new();
4620     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4621     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
4622     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4623     tcg_temp_free(t0);
4624     tcg_temp_free(t1);
4625     if (unlikely(Rc(ctx->opcode) != 0))
4626         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4627 }
4628
4629 /* mul - mul. */
4630 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4631 {
4632     TCGv_i64 t0 = tcg_temp_new_i64();
4633     TCGv_i64 t1 = tcg_temp_new_i64();
4634     TCGv t2 = tcg_temp_new();
4635     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4636     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4637     tcg_gen_mul_i64(t0, t0, t1);
4638     tcg_gen_trunc_i64_tl(t2, t0);
4639     gen_store_spr(SPR_MQ, t2);
4640     tcg_gen_shri_i64(t1, t0, 32);
4641     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4642     tcg_temp_free_i64(t0);
4643     tcg_temp_free_i64(t1);
4644     tcg_temp_free(t2);
4645     if (unlikely(Rc(ctx->opcode) != 0))
4646         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4647 }
4648
4649 /* mulo - mulo. */
4650 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4651 {
4652     int l1 = gen_new_label();
4653     TCGv_i64 t0 = tcg_temp_new_i64();
4654     TCGv_i64 t1 = tcg_temp_new_i64();
4655     TCGv t2 = tcg_temp_new();
4656     /* Start with XER OV disabled, the most likely case */
4657     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4658     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
4659     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
4660     tcg_gen_mul_i64(t0, t0, t1);
4661     tcg_gen_trunc_i64_tl(t2, t0);
4662     gen_store_spr(SPR_MQ, t2);
4663     tcg_gen_shri_i64(t1, t0, 32);
4664     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
4665     tcg_gen_ext32s_i64(t1, t0);
4666     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
4667     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
4668     gen_set_label(l1);
4669     tcg_temp_free_i64(t0);
4670     tcg_temp_free_i64(t1);
4671     tcg_temp_free(t2);
4672     if (unlikely(Rc(ctx->opcode) != 0))
4673         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4674 }
4675
4676 /* nabs - nabs. */
4677 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4678 {
4679     int l1 = gen_new_label();
4680     int l2 = gen_new_label();
4681     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4682     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4683     tcg_gen_br(l2);
4684     gen_set_label(l1);
4685     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4686     gen_set_label(l2);
4687     if (unlikely(Rc(ctx->opcode) != 0))
4688         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4689 }
4690
4691 /* nabso - nabso. */
4692 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4693 {
4694     int l1 = gen_new_label();
4695     int l2 = gen_new_label();
4696     tcg_gen_brcondi_tl(TCG_COND_GT, cpu_gpr[rA(ctx->opcode)], 0, l1);
4697     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4698     tcg_gen_br(l2);
4699     gen_set_label(l1);
4700     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
4701     gen_set_label(l2);
4702     /* nabs never overflows */
4703     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
4704     if (unlikely(Rc(ctx->opcode) != 0))
4705         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
4706 }
4707
4708 /* rlmi - rlmi. */
4709 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4710 {
4711     uint32_t mb = MB(ctx->opcode);
4712     uint32_t me = ME(ctx->opcode);
4713     TCGv t0 = tcg_temp_new();
4714     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4715     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4716     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
4717     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~MASK(mb, me));
4718     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
4719     tcg_temp_free(t0);
4720     if (unlikely(Rc(ctx->opcode) != 0))
4721         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4722 }
4723
4724 /* rrib - rrib. */
4725 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4726 {
4727     TCGv t0 = tcg_temp_new();
4728     TCGv t1 = tcg_temp_new();
4729     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4730     tcg_gen_movi_tl(t1, 0x80000000);
4731     tcg_gen_shr_tl(t1, t1, t0);
4732     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4733     tcg_gen_and_tl(t0, t0, t1);
4734     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
4735     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4736     tcg_temp_free(t0);
4737     tcg_temp_free(t1);
4738     if (unlikely(Rc(ctx->opcode) != 0))
4739         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4740 }
4741
4742 /* sle - sle. */
4743 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4744 {
4745     TCGv t0 = tcg_temp_new();
4746     TCGv t1 = tcg_temp_new();
4747     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4748     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4749     tcg_gen_subfi_tl(t1, 32, t1);
4750     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4751     tcg_gen_or_tl(t1, t0, t1);
4752     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4753     gen_store_spr(SPR_MQ, t1);
4754     tcg_temp_free(t0);
4755     tcg_temp_free(t1);
4756     if (unlikely(Rc(ctx->opcode) != 0))
4757         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4758 }
4759
4760 /* sleq - sleq. */
4761 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4762 {
4763     TCGv t0 = tcg_temp_new();
4764     TCGv t1 = tcg_temp_new();
4765     TCGv t2 = tcg_temp_new();
4766     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4767     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
4768     tcg_gen_shl_tl(t2, t2, t0);
4769     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4770     gen_load_spr(t1, SPR_MQ);
4771     gen_store_spr(SPR_MQ, t0);
4772     tcg_gen_and_tl(t0, t0, t2);
4773     tcg_gen_andc_tl(t1, t1, t2);
4774     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4775     tcg_temp_free(t0);
4776     tcg_temp_free(t1);
4777     tcg_temp_free(t2);
4778     if (unlikely(Rc(ctx->opcode) != 0))
4779         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4780 }
4781
4782 /* sliq - sliq. */
4783 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4784 {
4785     int sh = SH(ctx->opcode);
4786     TCGv t0 = tcg_temp_new();
4787     TCGv t1 = tcg_temp_new();
4788     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4789     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4790     tcg_gen_or_tl(t1, t0, t1);
4791     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4792     gen_store_spr(SPR_MQ, t1);
4793     tcg_temp_free(t0);
4794     tcg_temp_free(t1);
4795     if (unlikely(Rc(ctx->opcode) != 0))
4796         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4797 }
4798
4799 /* slliq - slliq. */
4800 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4801 {
4802     int sh = SH(ctx->opcode);
4803     TCGv t0 = tcg_temp_new();
4804     TCGv t1 = tcg_temp_new();
4805     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4806     gen_load_spr(t1, SPR_MQ);
4807     gen_store_spr(SPR_MQ, t0);
4808     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
4809     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
4810     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4811     tcg_temp_free(t0);
4812     tcg_temp_free(t1);
4813     if (unlikely(Rc(ctx->opcode) != 0))
4814         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4815 }
4816
4817 /* sllq - sllq. */
4818 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4819 {
4820     int l1 = gen_new_label();
4821     int l2 = gen_new_label();
4822     TCGv t0 = tcg_temp_local_new();
4823     TCGv t1 = tcg_temp_local_new();
4824     TCGv t2 = tcg_temp_local_new();
4825     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4826     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4827     tcg_gen_shl_tl(t1, t1, t2);
4828     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4829     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
4830     gen_load_spr(t0, SPR_MQ);
4831     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4832     tcg_gen_br(l2);
4833     gen_set_label(l1);
4834     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4835     gen_load_spr(t2, SPR_MQ);
4836     tcg_gen_andc_tl(t1, t2, t1);
4837     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
4838     gen_set_label(l2);
4839     tcg_temp_free(t0);
4840     tcg_temp_free(t1);
4841     tcg_temp_free(t2);
4842     if (unlikely(Rc(ctx->opcode) != 0))
4843         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4844 }
4845
4846 /* slq - slq. */
4847 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4848 {
4849     int l1 = gen_new_label();
4850     TCGv t0 = tcg_temp_new();
4851     TCGv t1 = tcg_temp_new();
4852     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4853     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4854     tcg_gen_subfi_tl(t1, 32, t1);
4855     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4856     tcg_gen_or_tl(t1, t0, t1);
4857     gen_store_spr(SPR_MQ, t1);
4858     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
4859     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4860     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4861     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
4862     gen_set_label(l1);
4863     tcg_temp_free(t0);
4864     tcg_temp_free(t1);
4865     if (unlikely(Rc(ctx->opcode) != 0))
4866         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4867 }
4868
4869 /* sraiq - sraiq. */
4870 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4871 {
4872     int sh = SH(ctx->opcode);
4873     int l1 = gen_new_label();
4874     TCGv t0 = tcg_temp_new();
4875     TCGv t1 = tcg_temp_new();
4876     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4877     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4878     tcg_gen_or_tl(t0, t0, t1);
4879     gen_store_spr(SPR_MQ, t0);
4880     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4881     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
4882     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
4883     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4884     gen_set_label(l1);
4885     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
4886     tcg_temp_free(t0);
4887     tcg_temp_free(t1);
4888     if (unlikely(Rc(ctx->opcode) != 0))
4889         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4890 }
4891
4892 /* sraq - sraq. */
4893 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4894 {
4895     int l1 = gen_new_label();
4896     int l2 = gen_new_label();
4897     TCGv t0 = tcg_temp_new();
4898     TCGv t1 = tcg_temp_local_new();
4899     TCGv t2 = tcg_temp_local_new();
4900     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
4901     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
4902     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
4903     tcg_gen_subfi_tl(t2, 32, t2);
4904     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
4905     tcg_gen_or_tl(t0, t0, t2);
4906     gen_store_spr(SPR_MQ, t0);
4907     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
4908     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
4909     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
4910     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
4911     gen_set_label(l1);
4912     tcg_temp_free(t0);
4913     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
4914     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
4915     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
4916     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
4917     tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_CA));
4918     gen_set_label(l2);
4919     tcg_temp_free(t1);
4920     tcg_temp_free(t2);
4921     if (unlikely(Rc(ctx->opcode) != 0))
4922         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4923 }
4924
4925 /* sre - sre. */
4926 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4927 {
4928     TCGv t0 = tcg_temp_new();
4929     TCGv t1 = tcg_temp_new();
4930     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4931     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4932     tcg_gen_subfi_tl(t1, 32, t1);
4933     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
4934     tcg_gen_or_tl(t1, t0, t1);
4935     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4936     gen_store_spr(SPR_MQ, t1);
4937     tcg_temp_free(t0);
4938     tcg_temp_free(t1);
4939     if (unlikely(Rc(ctx->opcode) != 0))
4940         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4941 }
4942
4943 /* srea - srea. */
4944 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4945 {
4946     TCGv t0 = tcg_temp_new();
4947     TCGv t1 = tcg_temp_new();
4948     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
4949     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
4950     gen_store_spr(SPR_MQ, t0);
4951     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
4952     tcg_temp_free(t0);
4953     tcg_temp_free(t1);
4954     if (unlikely(Rc(ctx->opcode) != 0))
4955         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4956 }
4957
4958 /* sreq */
4959 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4960 {
4961     TCGv t0 = tcg_temp_new();
4962     TCGv t1 = tcg_temp_new();
4963     TCGv t2 = tcg_temp_new();
4964     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
4965     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
4966     tcg_gen_shr_tl(t1, t1, t0);
4967     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
4968     gen_load_spr(t2, SPR_MQ);
4969     gen_store_spr(SPR_MQ, t0);
4970     tcg_gen_and_tl(t0, t0, t1);
4971     tcg_gen_andc_tl(t2, t2, t1);
4972     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
4973     tcg_temp_free(t0);
4974     tcg_temp_free(t1);
4975     tcg_temp_free(t2);
4976     if (unlikely(Rc(ctx->opcode) != 0))
4977         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4978 }
4979
4980 /* sriq */
4981 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4982 {
4983     int sh = SH(ctx->opcode);
4984     TCGv t0 = tcg_temp_new();
4985     TCGv t1 = tcg_temp_new();
4986     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
4987     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
4988     tcg_gen_or_tl(t1, t0, t1);
4989     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
4990     gen_store_spr(SPR_MQ, t1);
4991     tcg_temp_free(t0);
4992     tcg_temp_free(t1);
4993     if (unlikely(Rc(ctx->opcode) != 0))
4994         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
4995 }
4996
4997 /* srliq */
4998 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4999 {
5000     int sh = SH(ctx->opcode);
5001     TCGv t0 = tcg_temp_new();
5002     TCGv t1 = tcg_temp_new();
5003     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5004     gen_load_spr(t1, SPR_MQ);
5005     gen_store_spr(SPR_MQ, t0);
5006     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5007     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5008     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5009     tcg_temp_free(t0);
5010     tcg_temp_free(t1);
5011     if (unlikely(Rc(ctx->opcode) != 0))
5012         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5013 }
5014
5015 /* srlq */
5016 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
5017 {
5018     int l1 = gen_new_label();
5019     int l2 = gen_new_label();
5020     TCGv t0 = tcg_temp_local_new();
5021     TCGv t1 = tcg_temp_local_new();
5022     TCGv t2 = tcg_temp_local_new();
5023     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5024     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5025     tcg_gen_shr_tl(t2, t1, t2);
5026     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5027     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5028     gen_load_spr(t0, SPR_MQ);
5029     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5030     tcg_gen_br(l2);
5031     gen_set_label(l1);
5032     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5033     tcg_gen_and_tl(t0, t0, t2);
5034     gen_load_spr(t1, SPR_MQ);
5035     tcg_gen_andc_tl(t1, t1, t2);
5036     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5037     gen_set_label(l2);
5038     tcg_temp_free(t0);
5039     tcg_temp_free(t1);
5040     tcg_temp_free(t2);
5041     if (unlikely(Rc(ctx->opcode) != 0))
5042         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5043 }
5044
5045 /* srq */
5046 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
5047 {
5048     int l1 = gen_new_label();
5049     TCGv t0 = tcg_temp_new();
5050     TCGv t1 = tcg_temp_new();
5051     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5052     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5053     tcg_gen_subfi_tl(t1, 32, t1);
5054     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5055     tcg_gen_or_tl(t1, t0, t1);
5056     gen_store_spr(SPR_MQ, t1);
5057     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5058     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5059     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5060     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5061     gen_set_label(l1);
5062     tcg_temp_free(t0);
5063     tcg_temp_free(t1);
5064     if (unlikely(Rc(ctx->opcode) != 0))
5065         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5066 }
5067
5068 /* PowerPC 602 specific instructions */
5069 /* dsa  */
5070 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
5071 {
5072     /* XXX: TODO */
5073     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5074 }
5075
5076 /* esa */
5077 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
5078 {
5079     /* XXX: TODO */
5080     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5081 }
5082
5083 /* mfrom */
5084 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
5085 {
5086 #if defined(CONFIG_USER_ONLY)
5087     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5088 #else
5089     if (unlikely(!ctx->mem_idx)) {
5090         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5091         return;
5092     }
5093     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5094 #endif
5095 }
5096
5097 /* 602 - 603 - G2 TLB management */
5098 /* tlbld */
5099 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
5100 {
5101 #if defined(CONFIG_USER_ONLY)
5102     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5103 #else
5104     if (unlikely(!ctx->mem_idx)) {
5105         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5106         return;
5107     }
5108     gen_helper_6xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5109 #endif
5110 }
5111
5112 /* tlbli */
5113 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
5114 {
5115 #if defined(CONFIG_USER_ONLY)
5116     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5117 #else
5118     if (unlikely(!ctx->mem_idx)) {
5119         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5120         return;
5121     }
5122     gen_helper_6xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5123 #endif
5124 }
5125
5126 /* 74xx TLB management */
5127 /* tlbld */
5128 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5129 {
5130 #if defined(CONFIG_USER_ONLY)
5131     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5132 #else
5133     if (unlikely(!ctx->mem_idx)) {
5134         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5135         return;
5136     }
5137     gen_helper_74xx_tlbd(cpu_gpr[rB(ctx->opcode)]);
5138 #endif
5139 }
5140
5141 /* tlbli */
5142 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5143 {
5144 #if defined(CONFIG_USER_ONLY)
5145     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5146 #else
5147     if (unlikely(!ctx->mem_idx)) {
5148         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5149         return;
5150     }
5151     gen_helper_74xx_tlbi(cpu_gpr[rB(ctx->opcode)]);
5152 #endif
5153 }
5154
5155 /* POWER instructions not in PowerPC 601 */
5156 /* clf */
5157 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
5158 {
5159     /* Cache line flush: implemented as no-op */
5160 }
5161
5162 /* cli */
5163 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
5164 {
5165     /* Cache line invalidate: privileged and treated as no-op */
5166 #if defined(CONFIG_USER_ONLY)
5167     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5168 #else
5169     if (unlikely(!ctx->mem_idx)) {
5170         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5171         return;
5172     }
5173 #endif
5174 }
5175
5176 /* dclst */
5177 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
5178 {
5179     /* Data cache line store: treated as no-op */
5180 }
5181
5182 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
5183 {
5184 #if defined(CONFIG_USER_ONLY)
5185     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5186 #else
5187     int ra = rA(ctx->opcode);
5188     int rd = rD(ctx->opcode);
5189     TCGv t0;
5190     if (unlikely(!ctx->mem_idx)) {
5191         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5192         return;
5193     }
5194     t0 = tcg_temp_new();
5195     gen_addr_reg_index(ctx, t0);
5196     tcg_gen_shri_tl(t0, t0, 28);
5197     tcg_gen_andi_tl(t0, t0, 0xF);
5198     gen_helper_load_sr(cpu_gpr[rd], t0);
5199     tcg_temp_free(t0);
5200     if (ra != 0 && ra != rd)
5201         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5202 #endif
5203 }
5204
5205 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
5206 {
5207 #if defined(CONFIG_USER_ONLY)
5208     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5209 #else
5210     TCGv t0;
5211     if (unlikely(!ctx->mem_idx)) {
5212         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5213         return;
5214     }
5215     t0 = tcg_temp_new();
5216     gen_addr_reg_index(ctx, t0);
5217     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], t0);
5218     tcg_temp_free(t0);
5219 #endif
5220 }
5221
5222 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
5223 {
5224 #if defined(CONFIG_USER_ONLY)
5225     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5226 #else
5227     if (unlikely(!ctx->mem_idx)) {
5228         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5229         return;
5230     }
5231     gen_helper_rfsvc();
5232     gen_sync_exception(ctx);
5233 #endif
5234 }
5235
5236 /* svc is not implemented for now */
5237
5238 /* POWER2 specific instructions */
5239 /* Quad manipulation (load/store two floats at a time) */
5240
5241 /* lfq */
5242 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5243 {
5244     int rd = rD(ctx->opcode);
5245     TCGv t0;
5246     gen_set_access_type(ctx, ACCESS_FLOAT);
5247     t0 = tcg_temp_new();
5248     gen_addr_imm_index(ctx, t0, 0);
5249     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5250     gen_addr_add(ctx, t0, t0, 8);
5251     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5252     tcg_temp_free(t0);
5253 }
5254
5255 /* lfqu */
5256 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5257 {
5258     int ra = rA(ctx->opcode);
5259     int rd = rD(ctx->opcode);
5260     TCGv t0, t1;
5261     gen_set_access_type(ctx, ACCESS_FLOAT);
5262     t0 = tcg_temp_new();
5263     t1 = tcg_temp_new();
5264     gen_addr_imm_index(ctx, t0, 0);
5265     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5266     gen_addr_add(ctx, t1, t0, 8);
5267     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5268     if (ra != 0)
5269         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5270     tcg_temp_free(t0);
5271     tcg_temp_free(t1);
5272 }
5273
5274 /* lfqux */
5275 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
5276 {
5277     int ra = rA(ctx->opcode);
5278     int rd = rD(ctx->opcode);
5279     gen_set_access_type(ctx, ACCESS_FLOAT);
5280     TCGv t0, t1;
5281     t0 = tcg_temp_new();
5282     gen_addr_reg_index(ctx, t0);
5283     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5284     t1 = tcg_temp_new();
5285     gen_addr_add(ctx, t1, t0, 8);
5286     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5287     tcg_temp_free(t1);
5288     if (ra != 0)
5289         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5290     tcg_temp_free(t0);
5291 }
5292
5293 /* lfqx */
5294 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5295 {
5296     int rd = rD(ctx->opcode);
5297     TCGv t0;
5298     gen_set_access_type(ctx, ACCESS_FLOAT);
5299     t0 = tcg_temp_new();
5300     gen_addr_reg_index(ctx, t0);
5301     gen_qemu_ld64(ctx, cpu_fpr[rd], t0);
5302     gen_addr_add(ctx, t0, t0, 8);
5303     gen_qemu_ld64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5304     tcg_temp_free(t0);
5305 }
5306
5307 /* stfq */
5308 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5309 {
5310     int rd = rD(ctx->opcode);
5311     TCGv t0;
5312     gen_set_access_type(ctx, ACCESS_FLOAT);
5313     t0 = tcg_temp_new();
5314     gen_addr_imm_index(ctx, t0, 0);
5315     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5316     gen_addr_add(ctx, t0, t0, 8);
5317     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5318     tcg_temp_free(t0);
5319 }
5320
5321 /* stfqu */
5322 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5323 {
5324     int ra = rA(ctx->opcode);
5325     int rd = rD(ctx->opcode);
5326     TCGv t0, t1;
5327     gen_set_access_type(ctx, ACCESS_FLOAT);
5328     t0 = tcg_temp_new();
5329     gen_addr_imm_index(ctx, t0, 0);
5330     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5331     t1 = tcg_temp_new();
5332     gen_addr_add(ctx, t1, t0, 8);
5333     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5334     tcg_temp_free(t1);
5335     if (ra != 0)
5336         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5337     tcg_temp_free(t0);
5338 }
5339
5340 /* stfqux */
5341 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
5342 {
5343     int ra = rA(ctx->opcode);
5344     int rd = rD(ctx->opcode);
5345     TCGv t0, t1;
5346     gen_set_access_type(ctx, ACCESS_FLOAT);
5347     t0 = tcg_temp_new();
5348     gen_addr_reg_index(ctx, t0);
5349     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5350     t1 = tcg_temp_new();
5351     gen_addr_add(ctx, t1, t0, 8);
5352     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t1);
5353     tcg_temp_free(t1);
5354     if (ra != 0)
5355         tcg_gen_mov_tl(cpu_gpr[ra], t0);
5356     tcg_temp_free(t0);
5357 }
5358
5359 /* stfqx */
5360 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5361 {
5362     int rd = rD(ctx->opcode);
5363     TCGv t0;
5364     gen_set_access_type(ctx, ACCESS_FLOAT);
5365     t0 = tcg_temp_new();
5366     gen_addr_reg_index(ctx, t0);
5367     gen_qemu_st64(ctx, cpu_fpr[rd], t0);
5368     gen_addr_add(ctx, t0, t0, 8);
5369     gen_qemu_st64(ctx, cpu_fpr[(rd + 1) % 32], t0);
5370     tcg_temp_free(t0);
5371 }
5372
5373 /* BookE specific instructions */
5374 /* XXX: not implemented on 440 ? */
5375 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5376 {
5377     /* XXX: TODO */
5378     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5379 }
5380
5381 /* XXX: not implemented on 440 ? */
5382 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5383 {
5384 #if defined(CONFIG_USER_ONLY)
5385     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5386 #else
5387     TCGv t0;
5388     if (unlikely(!ctx->mem_idx)) {
5389         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5390         return;
5391     }
5392     t0 = tcg_temp_new();
5393     gen_addr_reg_index(ctx, t0);
5394     gen_helper_tlbie(cpu_gpr[rB(ctx->opcode)]);
5395     tcg_temp_free(t0);
5396 #endif
5397 }
5398
5399 /* All 405 MAC instructions are translated here */
5400 static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5401                                                 int opc2, int opc3,
5402                                                 int ra, int rb, int rt, int Rc)
5403 {
5404     TCGv t0, t1;
5405
5406     t0 = tcg_temp_local_new();
5407     t1 = tcg_temp_local_new();
5408
5409     switch (opc3 & 0x0D) {
5410     case 0x05:
5411         /* macchw    - macchw.    - macchwo   - macchwo.   */
5412         /* macchws   - macchws.   - macchwso  - macchwso.  */
5413         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5414         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5415         /* mulchw - mulchw. */
5416         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5417         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5418         tcg_gen_ext16s_tl(t1, t1);
5419         break;
5420     case 0x04:
5421         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5422         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5423         /* mulchwu - mulchwu. */
5424         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5425         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5426         tcg_gen_ext16u_tl(t1, t1);
5427         break;
5428     case 0x01:
5429         /* machhw    - machhw.    - machhwo   - machhwo.   */
5430         /* machhws   - machhws.   - machhwso  - machhwso.  */
5431         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5432         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5433         /* mulhhw - mulhhw. */
5434         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5435         tcg_gen_ext16s_tl(t0, t0);
5436         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5437         tcg_gen_ext16s_tl(t1, t1);
5438         break;
5439     case 0x00:
5440         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5441         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5442         /* mulhhwu - mulhhwu. */
5443         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5444         tcg_gen_ext16u_tl(t0, t0);
5445         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5446         tcg_gen_ext16u_tl(t1, t1);
5447         break;
5448     case 0x0D:
5449         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5450         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5451         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5452         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5453         /* mullhw - mullhw. */
5454         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5455         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5456         break;
5457     case 0x0C:
5458         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5459         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5460         /* mullhwu - mullhwu. */
5461         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5462         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5463         break;
5464     }
5465     if (opc2 & 0x04) {
5466         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5467         tcg_gen_mul_tl(t1, t0, t1);
5468         if (opc2 & 0x02) {
5469             /* nmultiply-and-accumulate (0x0E) */
5470             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5471         } else {
5472             /* multiply-and-accumulate (0x0C) */
5473             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5474         }
5475
5476         if (opc3 & 0x12) {
5477             /* Check overflow and/or saturate */
5478             int l1 = gen_new_label();
5479
5480             if (opc3 & 0x10) {
5481                 /* Start with XER OV disabled, the most likely case */
5482                 tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5483             }
5484             if (opc3 & 0x01) {
5485                 /* Signed */
5486                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5487                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5488                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5489                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5490                 if (opc3 & 0x02) {
5491                     /* Saturate */
5492                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5493                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5494                 }
5495             } else {
5496                 /* Unsigned */
5497                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5498                 if (opc3 & 0x02) {
5499                     /* Saturate */
5500                     tcg_gen_movi_tl(t0, UINT32_MAX);
5501                 }
5502             }
5503             if (opc3 & 0x10) {
5504                 /* Check overflow */
5505                 tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5506             }
5507             gen_set_label(l1);
5508             tcg_gen_mov_tl(cpu_gpr[rt], t0);
5509         }
5510     } else {
5511         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5512     }
5513     tcg_temp_free(t0);
5514     tcg_temp_free(t1);
5515     if (unlikely(Rc) != 0) {
5516         /* Update Rc0 */
5517         gen_set_Rc0(ctx, cpu_gpr[rt]);
5518     }
5519 }
5520
5521 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5522 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5523 {                                                                             \
5524     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5525                          rD(ctx->opcode), Rc(ctx->opcode));                   \
5526 }
5527
5528 /* macchw    - macchw.    */
5529 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5530 /* macchwo   - macchwo.   */
5531 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5532 /* macchws   - macchws.   */
5533 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5534 /* macchwso  - macchwso.  */
5535 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5536 /* macchwsu  - macchwsu.  */
5537 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5538 /* macchwsuo - macchwsuo. */
5539 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5540 /* macchwu   - macchwu.   */
5541 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5542 /* macchwuo  - macchwuo.  */
5543 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5544 /* machhw    - machhw.    */
5545 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5546 /* machhwo   - machhwo.   */
5547 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5548 /* machhws   - machhws.   */
5549 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5550 /* machhwso  - machhwso.  */
5551 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5552 /* machhwsu  - machhwsu.  */
5553 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5554 /* machhwsuo - machhwsuo. */
5555 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5556 /* machhwu   - machhwu.   */
5557 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5558 /* machhwuo  - machhwuo.  */
5559 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5560 /* maclhw    - maclhw.    */
5561 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5562 /* maclhwo   - maclhwo.   */
5563 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5564 /* maclhws   - maclhws.   */
5565 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5566 /* maclhwso  - maclhwso.  */
5567 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5568 /* maclhwu   - maclhwu.   */
5569 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5570 /* maclhwuo  - maclhwuo.  */
5571 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5572 /* maclhwsu  - maclhwsu.  */
5573 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5574 /* maclhwsuo - maclhwsuo. */
5575 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5576 /* nmacchw   - nmacchw.   */
5577 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5578 /* nmacchwo  - nmacchwo.  */
5579 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5580 /* nmacchws  - nmacchws.  */
5581 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5582 /* nmacchwso - nmacchwso. */
5583 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5584 /* nmachhw   - nmachhw.   */
5585 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5586 /* nmachhwo  - nmachhwo.  */
5587 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5588 /* nmachhws  - nmachhws.  */
5589 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5590 /* nmachhwso - nmachhwso. */
5591 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5592 /* nmaclhw   - nmaclhw.   */
5593 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5594 /* nmaclhwo  - nmaclhwo.  */
5595 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5596 /* nmaclhws  - nmaclhws.  */
5597 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5598 /* nmaclhwso - nmaclhwso. */
5599 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5600
5601 /* mulchw  - mulchw.  */
5602 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5603 /* mulchwu - mulchwu. */
5604 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5605 /* mulhhw  - mulhhw.  */
5606 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5607 /* mulhhwu - mulhhwu. */
5608 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5609 /* mullhw  - mullhw.  */
5610 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5611 /* mullhwu - mullhwu. */
5612 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5613
5614 /* mfdcr */
5615 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5616 {
5617 #if defined(CONFIG_USER_ONLY)
5618     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5619 #else
5620     TCGv dcrn;
5621     if (unlikely(!ctx->mem_idx)) {
5622         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5623         return;
5624     }
5625     /* NIP cannot be restored if the memory exception comes from an helper */
5626     gen_update_nip(ctx, ctx->nip - 4);
5627     dcrn = tcg_const_tl(SPR(ctx->opcode));
5628     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], dcrn);
5629     tcg_temp_free(dcrn);
5630 #endif
5631 }
5632
5633 /* mtdcr */
5634 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5635 {
5636 #if defined(CONFIG_USER_ONLY)
5637     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5638 #else
5639     TCGv dcrn;
5640     if (unlikely(!ctx->mem_idx)) {
5641         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5642         return;
5643     }
5644     /* NIP cannot be restored if the memory exception comes from an helper */
5645     gen_update_nip(ctx, ctx->nip - 4);
5646     dcrn = tcg_const_tl(SPR(ctx->opcode));
5647     gen_helper_store_dcr(dcrn, cpu_gpr[rS(ctx->opcode)]);
5648     tcg_temp_free(dcrn);
5649 #endif
5650 }
5651
5652 /* mfdcrx */
5653 /* XXX: not implemented on 440 ? */
5654 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5655 {
5656 #if defined(CONFIG_USER_ONLY)
5657     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5658 #else
5659     if (unlikely(!ctx->mem_idx)) {
5660         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5661         return;
5662     }
5663     /* NIP cannot be restored if the memory exception comes from an helper */
5664     gen_update_nip(ctx, ctx->nip - 4);
5665     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5666     /* Note: Rc update flag set leads to undefined state of Rc0 */
5667 #endif
5668 }
5669
5670 /* mtdcrx */
5671 /* XXX: not implemented on 440 ? */
5672 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5673 {
5674 #if defined(CONFIG_USER_ONLY)
5675     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5676 #else
5677     if (unlikely(!ctx->mem_idx)) {
5678         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
5679         return;
5680     }
5681     /* NIP cannot be restored if the memory exception comes from an helper */
5682     gen_update_nip(ctx, ctx->nip - 4);
5683     gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5684     /* Note: Rc update flag set leads to undefined state of Rc0 */
5685 #endif
5686 }
5687
5688 /* mfdcrux (PPC 460) : user-mode access to DCR */
5689 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5690 {
5691     /* NIP cannot be restored if the memory exception comes from an helper */
5692     gen_update_nip(ctx, ctx->nip - 4);
5693     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5694     /* Note: Rc update flag set leads to undefined state of Rc0 */
5695 }
5696
5697 /* mtdcrux (PPC 460) : user-mode access to DCR */
5698 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5699 {
5700     /* NIP cannot be restored if the memory exception comes from an helper */
5701     gen_update_nip(ctx, ctx->nip - 4);
5702     gen_helper_store_dcr(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5703     /* Note: Rc update flag set leads to undefined state of Rc0 */
5704 }
5705
5706 /* dccci */
5707 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5708 {
5709 #if defined(CONFIG_USER_ONLY)
5710     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5711 #else
5712     if (unlikely(!ctx->mem_idx)) {
5713         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5714         return;
5715     }
5716     /* interpreted as no-op */
5717 #endif
5718 }
5719
5720 /* dcread */
5721 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5722 {
5723 #if defined(CONFIG_USER_ONLY)
5724     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5725 #else
5726     TCGv EA, val;
5727     if (unlikely(!ctx->mem_idx)) {
5728         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5729         return;
5730     }
5731     gen_set_access_type(ctx, ACCESS_CACHE);
5732     EA = tcg_temp_new();
5733     gen_addr_reg_index(ctx, EA);
5734     val = tcg_temp_new();
5735     gen_qemu_ld32u(ctx, val, EA);
5736     tcg_temp_free(val);
5737     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5738     tcg_temp_free(EA);
5739 #endif
5740 }
5741
5742 /* icbt */
5743 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5744 {
5745     /* interpreted as no-op */
5746     /* XXX: specification say this is treated as a load by the MMU
5747      *      but does not generate any exception
5748      */
5749 }
5750
5751 /* iccci */
5752 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5753 {
5754 #if defined(CONFIG_USER_ONLY)
5755     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5756 #else
5757     if (unlikely(!ctx->mem_idx)) {
5758         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5759         return;
5760     }
5761     /* interpreted as no-op */
5762 #endif
5763 }
5764
5765 /* icread */
5766 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5767 {
5768 #if defined(CONFIG_USER_ONLY)
5769     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5770 #else
5771     if (unlikely(!ctx->mem_idx)) {
5772         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5773         return;
5774     }
5775     /* interpreted as no-op */
5776 #endif
5777 }
5778
5779 /* rfci (mem_idx only) */
5780 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5781 {
5782 #if defined(CONFIG_USER_ONLY)
5783     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5784 #else
5785     if (unlikely(!ctx->mem_idx)) {
5786         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5787         return;
5788     }
5789     /* Restore CPU state */
5790     gen_helper_40x_rfci();
5791     gen_sync_exception(ctx);
5792 #endif
5793 }
5794
5795 GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5796 {
5797 #if defined(CONFIG_USER_ONLY)
5798     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5799 #else
5800     if (unlikely(!ctx->mem_idx)) {
5801         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5802         return;
5803     }
5804     /* Restore CPU state */
5805     gen_helper_rfci();
5806     gen_sync_exception(ctx);
5807 #endif
5808 }
5809
5810 /* BookE specific */
5811 /* XXX: not implemented on 440 ? */
5812 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5813 {
5814 #if defined(CONFIG_USER_ONLY)
5815     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5816 #else
5817     if (unlikely(!ctx->mem_idx)) {
5818         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5819         return;
5820     }
5821     /* Restore CPU state */
5822     gen_helper_rfdi();
5823     gen_sync_exception(ctx);
5824 #endif
5825 }
5826
5827 /* XXX: not implemented on 440 ? */
5828 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5829 {
5830 #if defined(CONFIG_USER_ONLY)
5831     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5832 #else
5833     if (unlikely(!ctx->mem_idx)) {
5834         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5835         return;
5836     }
5837     /* Restore CPU state */
5838     gen_helper_rfmci();
5839     gen_sync_exception(ctx);
5840 #endif
5841 }
5842
5843 /* TLB management - PowerPC 405 implementation */
5844 /* tlbre */
5845 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5846 {
5847 #if defined(CONFIG_USER_ONLY)
5848     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5849 #else
5850     if (unlikely(!ctx->mem_idx)) {
5851         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5852         return;
5853     }
5854     switch (rB(ctx->opcode)) {
5855     case 0:
5856         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5857         break;
5858     case 1:
5859         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5860         break;
5861     default:
5862         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5863         break;
5864     }
5865 #endif
5866 }
5867
5868 /* tlbsx - tlbsx. */
5869 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5870 {
5871 #if defined(CONFIG_USER_ONLY)
5872     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5873 #else
5874     TCGv t0;
5875     if (unlikely(!ctx->mem_idx)) {
5876         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5877         return;
5878     }
5879     t0 = tcg_temp_new();
5880     gen_addr_reg_index(ctx, t0);
5881     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5882     tcg_temp_free(t0);
5883     if (Rc(ctx->opcode)) {
5884         int l1 = gen_new_label();
5885         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5886         tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5887         tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5888         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5889         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5890         gen_set_label(l1);
5891     }
5892 #endif
5893 }
5894
5895 /* tlbwe */
5896 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5897 {
5898 #if defined(CONFIG_USER_ONLY)
5899     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5900 #else
5901     if (unlikely(!ctx->mem_idx)) {
5902         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5903         return;
5904     }
5905     switch (rB(ctx->opcode)) {
5906     case 0:
5907         gen_helper_4xx_tlbwe_hi(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5908         break;
5909     case 1:
5910         gen_helper_4xx_tlbwe_lo(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5911         break;
5912     default:
5913         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5914         break;
5915     }
5916 #endif
5917 }
5918
5919 /* TLB management - PowerPC 440 implementation */
5920 /* tlbre */
5921 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5922 {
5923 #if defined(CONFIG_USER_ONLY)
5924     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5925 #else
5926     if (unlikely(!ctx->mem_idx)) {
5927         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5928         return;
5929     }
5930     switch (rB(ctx->opcode)) {
5931     case 0:
5932     case 1:
5933     case 2:
5934         {
5935             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5936             gen_helper_440_tlbwe(t0, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5937             tcg_temp_free_i32(t0);
5938         }
5939         break;
5940     default:
5941         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5942         break;
5943     }
5944 #endif
5945 }
5946
5947 /* tlbsx - tlbsx. */
5948 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5949 {
5950 #if defined(CONFIG_USER_ONLY)
5951     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5952 #else
5953     TCGv t0;
5954     if (unlikely(!ctx->mem_idx)) {
5955         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5956         return;
5957     }
5958     t0 = tcg_temp_new();
5959     gen_addr_reg_index(ctx, t0);
5960     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], t0);
5961     tcg_temp_free(t0);
5962     if (Rc(ctx->opcode)) {
5963         int l1 = gen_new_label();
5964         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_xer);
5965         tcg_gen_shri_i32(cpu_crf[0], cpu_crf[0], XER_SO);
5966         tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 1);
5967         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5968         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5969         gen_set_label(l1);
5970     }
5971 #endif
5972 }
5973
5974 /* tlbwe */
5975 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5976 {
5977 #if defined(CONFIG_USER_ONLY)
5978     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5979 #else
5980     if (unlikely(!ctx->mem_idx)) {
5981         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5982         return;
5983     }
5984     switch (rB(ctx->opcode)) {
5985     case 0:
5986     case 1:
5987     case 2:
5988         {
5989             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
5990             gen_helper_440_tlbwe(t0, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
5991             tcg_temp_free_i32(t0);
5992         }
5993         break;
5994     default:
5995         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5996         break;
5997     }
5998 #endif
5999 }
6000
6001 /* wrtee */
6002 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
6003 {
6004 #if defined(CONFIG_USER_ONLY)
6005     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6006 #else
6007     TCGv t0;
6008     if (unlikely(!ctx->mem_idx)) {
6009         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6010         return;
6011     }
6012     t0 = tcg_temp_new();
6013     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6014     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6015     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6016     tcg_temp_free(t0);
6017     /* Stop translation to have a chance to raise an exception
6018      * if we just set msr_ee to 1
6019      */
6020     gen_stop_exception(ctx);
6021 #endif
6022 }
6023
6024 /* wrteei */
6025 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
6026 {
6027 #if defined(CONFIG_USER_ONLY)
6028     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6029 #else
6030     if (unlikely(!ctx->mem_idx)) {
6031         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_OPC);
6032         return;
6033     }
6034     if (ctx->opcode & 0x00010000) {
6035         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6036         /* Stop translation to have a chance to raise an exception */
6037         gen_stop_exception(ctx);
6038     } else {
6039         tcg_gen_andi_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6040     }
6041 #endif
6042 }
6043
6044 /* PowerPC 440 specific instructions */
6045 /* dlmzb */
6046 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
6047 {
6048     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6049     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
6050                      cpu_gpr[rB(ctx->opcode)], t0);
6051     tcg_temp_free_i32(t0);
6052 }
6053
6054 /* mbar replaces eieio on 440 */
6055 GEN_HANDLER(mbar, 0x1F, 0x16, 0x1a, 0x001FF801, PPC_BOOKE)
6056 {
6057     /* interpreted as no-op */
6058 }
6059
6060 /* msync replaces sync on 440 */
6061 GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
6062 {
6063     /* interpreted as no-op */
6064 }
6065
6066 /* icbt */
6067 GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
6068 {
6069     /* interpreted as no-op */
6070     /* XXX: specification say this is treated as a load by the MMU
6071      *      but does not generate any exception
6072      */
6073 }
6074
6075 /***                      Altivec vector extension                         ***/
6076 /* Altivec registers moves */
6077
6078 #define GEN_VR_LDX(name, opc2, opc3)                                          \
6079 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
6080 {                                                                             \
6081     TCGv EA;                                                                  \
6082     if (unlikely(!ctx->altivec_enabled)) {                                    \
6083         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6084         return;                                                               \
6085     }                                                                         \
6086     gen_set_access_type(ctx, ACCESS_INT);                                     \
6087     EA = tcg_temp_new();                                                      \
6088     gen_addr_reg_index(ctx, EA);                                              \
6089     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6090     if (ctx->le_mode) {                                                       \
6091         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6092         tcg_gen_addi_tl(EA, EA, 8);                                           \
6093         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6094     } else {                                                                  \
6095         gen_qemu_ld64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6096         tcg_gen_addi_tl(EA, EA, 8);                                           \
6097         gen_qemu_ld64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6098     }                                                                         \
6099     tcg_temp_free(EA);                                                        \
6100 }
6101
6102 #define GEN_VR_STX(name, opc2, opc3)                                          \
6103 GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
6104 {                                                                             \
6105     TCGv EA;                                                                  \
6106     if (unlikely(!ctx->altivec_enabled)) {                                    \
6107         gen_exception(ctx, POWERPC_EXCP_VPU);                                 \
6108         return;                                                               \
6109     }                                                                         \
6110     gen_set_access_type(ctx, ACCESS_INT);                                     \
6111     EA = tcg_temp_new();                                                      \
6112     gen_addr_reg_index(ctx, EA);                                              \
6113     tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
6114     if (ctx->le_mode) {                                                       \
6115         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6116         tcg_gen_addi_tl(EA, EA, 8);                                           \
6117         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6118     } else {                                                                  \
6119         gen_qemu_st64(ctx, cpu_avrh[rD(ctx->opcode)], EA);                    \
6120         tcg_gen_addi_tl(EA, EA, 8);                                           \
6121         gen_qemu_st64(ctx, cpu_avrl[rD(ctx->opcode)], EA);                    \
6122     }                                                                         \
6123     tcg_temp_free(EA);                                                        \
6124 }
6125
6126 GEN_VR_LDX(lvx, 0x07, 0x03);
6127 /* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
6128 GEN_VR_LDX(lvxl, 0x07, 0x0B);
6129
6130 GEN_VR_STX(svx, 0x07, 0x07);
6131 /* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
6132 GEN_VR_STX(svxl, 0x07, 0x0F);
6133
6134 /***                           SPE extension                               ***/
6135 /* Register moves */
6136
6137 static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
6138 #if defined(TARGET_PPC64)
6139     tcg_gen_mov_i64(t, cpu_gpr[reg]);
6140 #else
6141     tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6142 #endif
6143 }
6144
6145 static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
6146 #if defined(TARGET_PPC64)
6147     tcg_gen_mov_i64(cpu_gpr[reg], t);
6148 #else
6149     TCGv_i64 tmp = tcg_temp_new_i64();
6150     tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
6151     tcg_gen_shri_i64(tmp, t, 32);
6152     tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
6153     tcg_temp_free_i64(tmp);
6154 #endif
6155 }
6156
6157 #define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
6158 GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
6159 {                                                                             \
6160     if (Rc(ctx->opcode))                                                      \
6161         gen_##name1(ctx);                                                     \
6162     else                                                                      \
6163         gen_##name0(ctx);                                                     \
6164 }
6165
6166 /* Handler for undefined SPE opcodes */
6167 static always_inline void gen_speundef (DisasContext *ctx)
6168 {
6169     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6170 }
6171
6172 /* SPE logic */
6173 #if defined(TARGET_PPC64)
6174 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6175 static always_inline void gen_##name (DisasContext *ctx)                      \
6176 {                                                                             \
6177     if (unlikely(!ctx->spe_enabled)) {                                        \
6178         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6179         return;                                                               \
6180     }                                                                         \
6181     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6182            cpu_gpr[rB(ctx->opcode)]);                                         \
6183 }
6184 #else
6185 #define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6186 static always_inline void gen_##name (DisasContext *ctx)                      \
6187 {                                                                             \
6188     if (unlikely(!ctx->spe_enabled)) {                                        \
6189         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6190         return;                                                               \
6191     }                                                                         \
6192     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6193            cpu_gpr[rB(ctx->opcode)]);                                         \
6194     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6195            cpu_gprh[rB(ctx->opcode)]);                                        \
6196 }
6197 #endif
6198
6199 GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6200 GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6201 GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6202 GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6203 GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6204 GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6205 GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6206 GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6207
6208 /* SPE logic immediate */
6209 #if defined(TARGET_PPC64)
6210 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6211 static always_inline void gen_##name (DisasContext *ctx)                      \
6212 {                                                                             \
6213     if (unlikely(!ctx->spe_enabled)) {                                        \
6214         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6215         return;                                                               \
6216     }                                                                         \
6217     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6218     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6219     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6220     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6221     tcg_opi(t0, t0, rB(ctx->opcode));                                         \
6222     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6223     tcg_gen_trunc_i64_i32(t1, t2);                                            \
6224     tcg_temp_free_i64(t2);                                                    \
6225     tcg_opi(t1, t1, rB(ctx->opcode));                                         \
6226     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6227     tcg_temp_free_i32(t0);                                                    \
6228     tcg_temp_free_i32(t1);                                                    \
6229 }
6230 #else
6231 #define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6232 static always_inline void gen_##name (DisasContext *ctx)                      \
6233 {                                                                             \
6234     if (unlikely(!ctx->spe_enabled)) {                                        \
6235         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6236         return;                                                               \
6237     }                                                                         \
6238     tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
6239             rB(ctx->opcode));                                                 \
6240     tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
6241             rB(ctx->opcode));                                                 \
6242 }
6243 #endif
6244 GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6245 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6246 GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6247 GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6248
6249 /* SPE arithmetic */
6250 #if defined(TARGET_PPC64)
6251 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6252 static always_inline void gen_##name (DisasContext *ctx)                      \
6253 {                                                                             \
6254     if (unlikely(!ctx->spe_enabled)) {                                        \
6255         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6256         return;                                                               \
6257     }                                                                         \
6258     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6259     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6260     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6261     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6262     tcg_op(t0, t0);                                                           \
6263     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6264     tcg_gen_trunc_i64_i32(t1, t2);                                            \
6265     tcg_temp_free_i64(t2);                                                    \
6266     tcg_op(t1, t1);                                                           \
6267     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6268     tcg_temp_free_i32(t0);                                                    \
6269     tcg_temp_free_i32(t1);                                                    \
6270 }
6271 #else
6272 #define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6273 static always_inline void gen_##name (DisasContext *ctx)                      \
6274 {                                                                             \
6275     if (unlikely(!ctx->spe_enabled)) {                                        \
6276         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6277         return;                                                               \
6278     }                                                                         \
6279     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
6280     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
6281 }
6282 #endif
6283
6284 static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6285 {
6286     int l1 = gen_new_label();
6287     int l2 = gen_new_label();
6288
6289     tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6290     tcg_gen_neg_i32(ret, arg1);
6291     tcg_gen_br(l2);
6292     gen_set_label(l1);
6293     tcg_gen_mov_i32(ret, arg1);
6294     gen_set_label(l2);
6295 }
6296 GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6297 GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6298 GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6299 GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
6300 static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6301 {
6302     tcg_gen_addi_i32(ret, arg1, 0x8000);
6303     tcg_gen_ext16u_i32(ret, ret);
6304 }
6305 GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
6306 GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6307 GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6308
6309 #if defined(TARGET_PPC64)
6310 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6311 static always_inline void gen_##name (DisasContext *ctx)                      \
6312 {                                                                             \
6313     if (unlikely(!ctx->spe_enabled)) {                                        \
6314         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6315         return;                                                               \
6316     }                                                                         \
6317     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6318     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6319     TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
6320     TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64);                           \
6321     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6322     tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
6323     tcg_op(t0, t0, t2);                                                       \
6324     tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
6325     tcg_gen_trunc_i64_i32(t1, t3);                                            \
6326     tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
6327     tcg_gen_trunc_i64_i32(t2, t3);                                            \
6328     tcg_temp_free_i64(t3);                                                    \
6329     tcg_op(t1, t1, t2);                                                       \
6330     tcg_temp_free_i32(t2);                                                    \
6331     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6332     tcg_temp_free_i32(t0);                                                    \
6333     tcg_temp_free_i32(t1);                                                    \
6334 }
6335 #else
6336 #define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6337 static always_inline void gen_##name (DisasContext *ctx)                      \
6338 {                                                                             \
6339     if (unlikely(!ctx->spe_enabled)) {                                        \
6340         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6341         return;                                                               \
6342     }                                                                         \
6343     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6344            cpu_gpr[rB(ctx->opcode)]);                                         \
6345     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6346            cpu_gprh[rB(ctx->opcode)]);                                        \
6347 }
6348 #endif
6349
6350 static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6351 {
6352     TCGv_i32 t0;
6353     int l1, l2;
6354
6355     l1 = gen_new_label();
6356     l2 = gen_new_label();
6357     t0 = tcg_temp_local_new_i32();
6358     /* No error here: 6 bits are used */
6359     tcg_gen_andi_i32(t0, arg2, 0x3F);
6360     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6361     tcg_gen_shr_i32(ret, arg1, t0);
6362     tcg_gen_br(l2);
6363     gen_set_label(l1);
6364     tcg_gen_movi_i32(ret, 0);
6365     tcg_gen_br(l2);
6366     tcg_temp_free_i32(t0);
6367 }
6368 GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
6369 static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6370 {
6371     TCGv_i32 t0;
6372     int l1, l2;
6373
6374     l1 = gen_new_label();
6375     l2 = gen_new_label();
6376     t0 = tcg_temp_local_new_i32();
6377     /* No error here: 6 bits are used */
6378     tcg_gen_andi_i32(t0, arg2, 0x3F);
6379     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6380     tcg_gen_sar_i32(ret, arg1, t0);
6381     tcg_gen_br(l2);
6382     gen_set_label(l1);
6383     tcg_gen_movi_i32(ret, 0);
6384     tcg_gen_br(l2);
6385     tcg_temp_free_i32(t0);
6386 }
6387 GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
6388 static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6389 {
6390     TCGv_i32 t0;
6391     int l1, l2;
6392
6393     l1 = gen_new_label();
6394     l2 = gen_new_label();
6395     t0 = tcg_temp_local_new_i32();
6396     /* No error here: 6 bits are used */
6397     tcg_gen_andi_i32(t0, arg2, 0x3F);
6398     tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6399     tcg_gen_shl_i32(ret, arg1, t0);
6400     tcg_gen_br(l2);
6401     gen_set_label(l1);
6402     tcg_gen_movi_i32(ret, 0);
6403     tcg_gen_br(l2);
6404     tcg_temp_free_i32(t0);
6405 }
6406 GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
6407 static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6408 {
6409     TCGv_i32 t0 = tcg_temp_new_i32();
6410     tcg_gen_andi_i32(t0, arg2, 0x1F);
6411     tcg_gen_rotl_i32(ret, arg1, t0);
6412     tcg_temp_free_i32(t0);
6413 }
6414 GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6415 static always_inline void gen_evmergehi (DisasContext *ctx)
6416 {
6417     if (unlikely(!ctx->spe_enabled)) {
6418         gen_exception(ctx, POWERPC_EXCP_APU);
6419         return;
6420     }
6421 #if defined(TARGET_PPC64)
6422     TCGv t0 = tcg_temp_new();
6423     TCGv t1 = tcg_temp_new();
6424     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6425     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6426     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6427     tcg_temp_free(t0);
6428     tcg_temp_free(t1);
6429 #else
6430     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6431     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6432 #endif
6433 }
6434 GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
6435 static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6436 {
6437     tcg_gen_sub_i32(ret, arg2, arg1);
6438 }
6439 GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6440
6441 /* SPE arithmetic immediate */
6442 #if defined(TARGET_PPC64)
6443 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6444 static always_inline void gen_##name (DisasContext *ctx)                      \
6445 {                                                                             \
6446     if (unlikely(!ctx->spe_enabled)) {                                        \
6447         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6448         return;                                                               \
6449     }                                                                         \
6450     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6451     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6452     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6453     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
6454     tcg_op(t0, t0, rA(ctx->opcode));                                          \
6455     tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6456     tcg_gen_trunc_i64_i32(t1, t2);                                            \
6457     tcg_temp_free_i64(t2);                                                    \
6458     tcg_op(t1, t1, rA(ctx->opcode));                                          \
6459     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6460     tcg_temp_free_i32(t0);                                                    \
6461     tcg_temp_free_i32(t1);                                                    \
6462 }
6463 #else
6464 #define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6465 static always_inline void gen_##name (DisasContext *ctx)                      \
6466 {                                                                             \
6467     if (unlikely(!ctx->spe_enabled)) {                                        \
6468         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6469         return;                                                               \
6470     }                                                                         \
6471     tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
6472            rA(ctx->opcode));                                                  \
6473     tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
6474            rA(ctx->opcode));                                                  \
6475 }
6476 #endif
6477 GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6478 GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6479
6480 /* SPE comparison */
6481 #if defined(TARGET_PPC64)
6482 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6483 static always_inline void gen_##name (DisasContext *ctx)                      \
6484 {                                                                             \
6485     if (unlikely(!ctx->spe_enabled)) {                                        \
6486         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6487         return;                                                               \
6488     }                                                                         \
6489     int l1 = gen_new_label();                                                 \
6490     int l2 = gen_new_label();                                                 \
6491     int l3 = gen_new_label();                                                 \
6492     int l4 = gen_new_label();                                                 \
6493     TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6494     TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6495     TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6496     tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6497     tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
6498     tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
6499     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6500     tcg_gen_br(l2);                                                           \
6501     gen_set_label(l1);                                                        \
6502     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6503                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6504     gen_set_label(l2);                                                        \
6505     tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6506     tcg_gen_trunc_i64_i32(t0, t2);                                            \
6507     tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6508     tcg_gen_trunc_i64_i32(t1, t2);                                            \
6509     tcg_temp_free_i64(t2);                                                    \
6510     tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
6511     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6512                      ~(CRF_CH | CRF_CH_AND_CL));                              \
6513     tcg_gen_br(l4);                                                           \
6514     gen_set_label(l3);                                                        \
6515     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6516                     CRF_CH | CRF_CH_OR_CL);                                   \
6517     gen_set_label(l4);                                                        \
6518     tcg_temp_free_i32(t0);                                                    \
6519     tcg_temp_free_i32(t1);                                                    \
6520 }
6521 #else
6522 #define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6523 static always_inline void gen_##name (DisasContext *ctx)                      \
6524 {                                                                             \
6525     if (unlikely(!ctx->spe_enabled)) {                                        \
6526         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
6527         return;                                                               \
6528     }                                                                         \
6529     int l1 = gen_new_label();                                                 \
6530     int l2 = gen_new_label();                                                 \
6531     int l3 = gen_new_label();                                                 \
6532     int l4 = gen_new_label();                                                 \
6533                                                                               \
6534     tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
6535                        cpu_gpr[rB(ctx->opcode)], l1);                         \
6536     tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
6537     tcg_gen_br(l2);                                                           \
6538     gen_set_label(l1);                                                        \
6539     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6540                      CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6541     gen_set_label(l2);                                                        \
6542     tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
6543                        cpu_gprh[rB(ctx->opcode)], l3);                        \
6544     tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6545                      ~(CRF_CH | CRF_CH_AND_CL));                              \
6546     tcg_gen_br(l4);                                                           \
6547     gen_set_label(l3);                                                        \
6548     tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6549                     CRF_CH | CRF_CH_OR_CL);                                   \
6550     gen_set_label(l4);                                                        \
6551 }
6552 #endif
6553 GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6554 GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6555 GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6556 GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6557 GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6558
6559 /* SPE misc */
6560 static always_inline void gen_brinc (DisasContext *ctx)
6561 {
6562     /* Note: brinc is usable even if SPE is disabled */
6563     gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6564                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6565 }
6566 static always_inline void gen_evmergelo (DisasContext *ctx)
6567 {
6568     if (unlikely(!ctx->spe_enabled)) {
6569         gen_exception(ctx, POWERPC_EXCP_APU);
6570         return;
6571     }
6572 #if defined(TARGET_PPC64)
6573     TCGv t0 = tcg_temp_new();
6574     TCGv t1 = tcg_temp_new();
6575     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6576     tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6577     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6578     tcg_temp_free(t0);
6579     tcg_temp_free(t1);
6580 #else
6581     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6582     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6583 #endif
6584 }
6585 static always_inline void gen_evmergehilo (DisasContext *ctx)
6586 {
6587     if (unlikely(!ctx->spe_enabled)) {
6588         gen_exception(ctx, POWERPC_EXCP_APU);
6589         return;
6590     }
6591 #if defined(TARGET_PPC64)
6592     TCGv t0 = tcg_temp_new();
6593     TCGv t1 = tcg_temp_new();
6594     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6595     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6596     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6597     tcg_temp_free(t0);
6598     tcg_temp_free(t1);
6599 #else
6600     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6601     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6602 #endif
6603 }
6604 static always_inline void gen_evmergelohi (DisasContext *ctx)
6605 {
6606     if (unlikely(!ctx->spe_enabled)) {
6607         gen_exception(ctx, POWERPC_EXCP_APU);
6608         return;
6609     }
6610 #if defined(TARGET_PPC64)
6611     TCGv t0 = tcg_temp_new();
6612     TCGv t1 = tcg_temp_new();
6613     tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6614     tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6615     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6616     tcg_temp_free(t0);
6617     tcg_temp_free(t1);
6618 #else
6619     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6620     tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6621 #endif
6622 }
6623 static always_inline void gen_evsplati (DisasContext *ctx)
6624 {
6625     uint64_t imm = ((int32_t)(rA(ctx->opcode) << 11)) >> 27;
6626
6627 #if defined(TARGET_PPC64)
6628     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6629 #else
6630     tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6631     tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6632 #endif
6633 }
6634 static always_inline void gen_evsplatfi (DisasContext *ctx)
6635 {
6636     uint64_t imm = rA(ctx->opcode) << 11;
6637
6638 #if defined(TARGET_PPC64)
6639     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], (imm << 32) | imm);
6640 #else
6641     tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6642     tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6643 #endif
6644 }
6645
6646 static always_inline void gen_evsel (DisasContext *ctx)
6647 {
6648     int l1 = gen_new_label();
6649     int l2 = gen_new_label();
6650     int l3 = gen_new_label();
6651     int l4 = gen_new_label();
6652     TCGv_i32 t0 = tcg_temp_local_new_i32();
6653 #if defined(TARGET_PPC64)
6654     TCGv t1 = tcg_temp_local_new();
6655     TCGv t2 = tcg_temp_local_new();
6656 #endif
6657     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6658     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6659 #if defined(TARGET_PPC64)
6660     tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6661 #else
6662     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6663 #endif
6664     tcg_gen_br(l2);
6665     gen_set_label(l1);
6666 #if defined(TARGET_PPC64)
6667     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6668 #else
6669     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6670 #endif
6671     gen_set_label(l2);
6672     tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6673     tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6674 #if defined(TARGET_PPC64)
6675     tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6676 #else
6677     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6678 #endif
6679     tcg_gen_br(l4);
6680     gen_set_label(l3);
6681 #if defined(TARGET_PPC64)
6682     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6683 #else
6684     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6685 #endif
6686     gen_set_label(l4);
6687     tcg_temp_free_i32(t0);
6688 #if defined(TARGET_PPC64)
6689     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6690     tcg_temp_free(t1);
6691     tcg_temp_free(t2);
6692 #endif
6693 }
6694 GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6695 {
6696     gen_evsel(ctx);
6697 }
6698 GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6699 {
6700     gen_evsel(ctx);
6701 }
6702 GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6703 {
6704     gen_evsel(ctx);
6705 }
6706 GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6707 {
6708     gen_evsel(ctx);
6709 }
6710
6711 GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
6712 GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
6713 GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
6714 GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
6715 GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
6716 GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
6717 GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
6718 GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
6719 GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
6720 GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
6721 GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
6722 GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
6723 GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
6724 GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
6725 GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
6726 GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
6727 GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
6728 GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
6729 GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
6730 GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
6731 GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
6732 GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
6733 GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
6734 GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
6735 GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
6736
6737 /* SPE load and stores */
6738 static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, TCGv EA, int sh)
6739 {
6740     target_ulong uimm = rB(ctx->opcode);
6741
6742     if (rA(ctx->opcode) == 0) {
6743         tcg_gen_movi_tl(EA, uimm << sh);
6744     } else {
6745         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], uimm << sh);
6746 #if defined(TARGET_PPC64)
6747         if (!ctx->sf_mode) {
6748             tcg_gen_ext32u_tl(EA, EA);
6749         }
6750 #endif
6751     }
6752 }
6753
6754 static always_inline void gen_op_evldd(DisasContext *ctx, TCGv addr)
6755 {
6756 #if defined(TARGET_PPC64)
6757     gen_qemu_ld64(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6758 #else
6759     TCGv_i64 t0 = tcg_temp_new_i64();
6760     gen_qemu_ld64(ctx, t0, addr);
6761     tcg_gen_trunc_i64_i32(cpu_gpr[rD(ctx->opcode)], t0);
6762     tcg_gen_shri_i64(t0, t0, 32);
6763     tcg_gen_trunc_i64_i32(cpu_gprh[rD(ctx->opcode)], t0);
6764     tcg_temp_free_i64(t0);
6765 #endif
6766 }
6767
6768 static always_inline void gen_op_evldw(DisasContext *ctx, TCGv addr)
6769 {
6770 #if defined(TARGET_PPC64)
6771     TCGv t0 = tcg_temp_new();
6772     gen_qemu_ld32u(ctx, t0, addr);
6773     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6774     gen_addr_add(ctx, addr, addr, 4);
6775     gen_qemu_ld32u(ctx, t0, addr);
6776     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6777     tcg_temp_free(t0);
6778 #else
6779     gen_qemu_ld32u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6780     gen_addr_add(ctx, addr, addr, 4);
6781     gen_qemu_ld32u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6782 #endif
6783 }
6784
6785 static always_inline void gen_op_evldh(DisasContext *ctx, TCGv addr)
6786 {
6787     TCGv t0 = tcg_temp_new();
6788 #if defined(TARGET_PPC64)
6789     gen_qemu_ld16u(ctx, t0, addr);
6790     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6791     gen_addr_add(ctx, addr, addr, 2);
6792     gen_qemu_ld16u(ctx, t0, addr);
6793     tcg_gen_shli_tl(t0, t0, 32);
6794     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6795     gen_addr_add(ctx, addr, addr, 2);
6796     gen_qemu_ld16u(ctx, t0, addr);
6797     tcg_gen_shli_tl(t0, t0, 16);
6798     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6799     gen_addr_add(ctx, addr, addr, 2);
6800     gen_qemu_ld16u(ctx, t0, addr);
6801     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6802 #else
6803     gen_qemu_ld16u(ctx, t0, addr);
6804     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6805     gen_addr_add(ctx, addr, addr, 2);
6806     gen_qemu_ld16u(ctx, t0, addr);
6807     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6808     gen_addr_add(ctx, addr, addr, 2);
6809     gen_qemu_ld16u(ctx, t0, addr);
6810     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6811     gen_addr_add(ctx, addr, addr, 2);
6812     gen_qemu_ld16u(ctx, t0, addr);
6813     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6814 #endif
6815     tcg_temp_free(t0);
6816 }
6817
6818 static always_inline void gen_op_evlhhesplat(DisasContext *ctx, TCGv addr)
6819 {
6820     TCGv t0 = tcg_temp_new();
6821     gen_qemu_ld16u(ctx, t0, addr);
6822 #if defined(TARGET_PPC64)
6823     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6824     tcg_gen_shli_tl(t0, t0, 16);
6825     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6826 #else
6827     tcg_gen_shli_tl(t0, t0, 16);
6828     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6829     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6830 #endif
6831     tcg_temp_free(t0);
6832 }
6833
6834 static always_inline void gen_op_evlhhousplat(DisasContext *ctx, TCGv addr)
6835 {
6836     TCGv t0 = tcg_temp_new();
6837     gen_qemu_ld16u(ctx, t0, addr);
6838 #if defined(TARGET_PPC64)
6839     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6840     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6841 #else
6842     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6843     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6844 #endif
6845     tcg_temp_free(t0);
6846 }
6847
6848 static always_inline void gen_op_evlhhossplat(DisasContext *ctx, TCGv addr)
6849 {
6850     TCGv t0 = tcg_temp_new();
6851     gen_qemu_ld16s(ctx, t0, addr);
6852 #if defined(TARGET_PPC64)
6853     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6854     tcg_gen_ext32u_tl(t0, t0);
6855     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6856 #else
6857     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6858     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6859 #endif
6860     tcg_temp_free(t0);
6861 }
6862
6863 static always_inline void gen_op_evlwhe(DisasContext *ctx, TCGv addr)
6864 {
6865     TCGv t0 = tcg_temp_new();
6866 #if defined(TARGET_PPC64)
6867     gen_qemu_ld16u(ctx, t0, addr);
6868     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6869     gen_addr_add(ctx, addr, addr, 2);
6870     gen_qemu_ld16u(ctx, t0, addr);
6871     tcg_gen_shli_tl(t0, t0, 16);
6872     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6873 #else
6874     gen_qemu_ld16u(ctx, t0, addr);
6875     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6876     gen_addr_add(ctx, addr, addr, 2);
6877     gen_qemu_ld16u(ctx, t0, addr);
6878     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6879 #endif
6880     tcg_temp_free(t0);
6881 }
6882
6883 static always_inline void gen_op_evlwhou(DisasContext *ctx, TCGv addr)
6884 {
6885 #if defined(TARGET_PPC64)
6886     TCGv t0 = tcg_temp_new();
6887     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6888     gen_addr_add(ctx, addr, addr, 2);
6889     gen_qemu_ld16u(ctx, t0, addr);
6890     tcg_gen_shli_tl(t0, t0, 32);
6891     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6892     tcg_temp_free(t0);
6893 #else
6894     gen_qemu_ld16u(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6895     gen_addr_add(ctx, addr, addr, 2);
6896     gen_qemu_ld16u(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6897 #endif
6898 }
6899
6900 static always_inline void gen_op_evlwhos(DisasContext *ctx, TCGv addr)
6901 {
6902 #if defined(TARGET_PPC64)
6903     TCGv t0 = tcg_temp_new();
6904     gen_qemu_ld16s(ctx, t0, addr);
6905     tcg_gen_ext32u_tl(cpu_gpr[rD(ctx->opcode)], t0);
6906     gen_addr_add(ctx, addr, addr, 2);
6907     gen_qemu_ld16s(ctx, t0, addr);
6908     tcg_gen_shli_tl(t0, t0, 32);
6909     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6910     tcg_temp_free(t0);
6911 #else
6912     gen_qemu_ld16s(ctx, cpu_gprh[rD(ctx->opcode)], addr);
6913     gen_addr_add(ctx, addr, addr, 2);
6914     gen_qemu_ld16s(ctx, cpu_gpr[rD(ctx->opcode)], addr);
6915 #endif
6916 }
6917
6918 static always_inline void gen_op_evlwwsplat(DisasContext *ctx, TCGv addr)
6919 {
6920     TCGv t0 = tcg_temp_new();
6921     gen_qemu_ld32u(ctx, t0, addr);
6922 #if defined(TARGET_PPC64)
6923     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 32);
6924     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6925 #else
6926     tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], t0);
6927     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
6928 #endif
6929     tcg_temp_free(t0);
6930 }
6931
6932 static always_inline void gen_op_evlwhsplat(DisasContext *ctx, TCGv addr)
6933 {
6934     TCGv t0 = tcg_temp_new();
6935 #if defined(TARGET_PPC64)
6936     gen_qemu_ld16u(ctx, t0, addr);
6937     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 48);
6938     tcg_gen_shli_tl(t0, t0, 32);
6939     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6940     gen_addr_add(ctx, addr, addr, 2);
6941     gen_qemu_ld16u(ctx, t0, addr);
6942     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6943     tcg_gen_shli_tl(t0, t0, 16);
6944     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t0);
6945 #else
6946     gen_qemu_ld16u(ctx, t0, addr);
6947     tcg_gen_shli_tl(cpu_gprh[rD(ctx->opcode)], t0, 16);
6948     tcg_gen_or_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6949     gen_addr_add(ctx, addr, addr, 2);
6950     gen_qemu_ld16u(ctx, t0, addr);
6951     tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)], t0, 16);
6952     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rD(ctx->opcode)], t0);
6953 #endif
6954     tcg_temp_free(t0);
6955 }
6956
6957 static always_inline void gen_op_evstdd(DisasContext *ctx, TCGv addr)
6958 {
6959 #if defined(TARGET_PPC64)
6960     gen_qemu_st64(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6961 #else
6962     TCGv_i64 t0 = tcg_temp_new_i64();
6963     tcg_gen_concat_i32_i64(t0, cpu_gpr[rS(ctx->opcode)], cpu_gprh[rS(ctx->opcode)]);
6964     gen_qemu_st64(ctx, t0, addr);
6965     tcg_temp_free_i64(t0);
6966 #endif
6967 }
6968
6969 static always_inline void gen_op_evstdw(DisasContext *ctx, TCGv addr)
6970 {
6971 #if defined(TARGET_PPC64)
6972     TCGv t0 = tcg_temp_new();
6973     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6974     gen_qemu_st32(ctx, t0, addr);
6975     tcg_temp_free(t0);
6976 #else
6977     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6978 #endif
6979     gen_addr_add(ctx, addr, addr, 4);
6980     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
6981 }
6982
6983 static always_inline void gen_op_evstdh(DisasContext *ctx, TCGv addr)
6984 {
6985     TCGv t0 = tcg_temp_new();
6986 #if defined(TARGET_PPC64)
6987     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
6988 #else
6989     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
6990 #endif
6991     gen_qemu_st16(ctx, t0, addr);
6992     gen_addr_add(ctx, addr, addr, 2);
6993 #if defined(TARGET_PPC64)
6994     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
6995     gen_qemu_st16(ctx, t0, addr);
6996 #else
6997     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
6998 #endif
6999     gen_addr_add(ctx, addr, addr, 2);
7000     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
7001     gen_qemu_st16(ctx, t0, addr);
7002     tcg_temp_free(t0);
7003     gen_addr_add(ctx, addr, addr, 2);
7004     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7005 }
7006
7007 static always_inline void gen_op_evstwhe(DisasContext *ctx, TCGv addr)
7008 {
7009     TCGv t0 = tcg_temp_new();
7010 #if defined(TARGET_PPC64)
7011     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 48);
7012 #else
7013     tcg_gen_shri_tl(t0, cpu_gprh[rS(ctx->opcode)], 16);
7014 #endif
7015     gen_qemu_st16(ctx, t0, addr);
7016     gen_addr_add(ctx, addr, addr, 2);
7017     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 16);
7018     gen_qemu_st16(ctx, t0, addr);
7019     tcg_temp_free(t0);
7020 }
7021
7022 static always_inline void gen_op_evstwho(DisasContext *ctx, TCGv addr)
7023 {
7024 #if defined(TARGET_PPC64)
7025     TCGv t0 = tcg_temp_new();
7026     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
7027     gen_qemu_st16(ctx, t0, addr);
7028     tcg_temp_free(t0);
7029 #else
7030     gen_qemu_st16(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7031 #endif
7032     gen_addr_add(ctx, addr, addr, 2);
7033     gen_qemu_st16(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7034 }
7035
7036 static always_inline void gen_op_evstwwe(DisasContext *ctx, TCGv addr)
7037 {
7038 #if defined(TARGET_PPC64)
7039     TCGv t0 = tcg_temp_new();
7040     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], 32);
7041     gen_qemu_st32(ctx, t0, addr);
7042     tcg_temp_free(t0);
7043 #else
7044     gen_qemu_st32(ctx, cpu_gprh[rS(ctx->opcode)], addr);
7045 #endif
7046 }
7047
7048 static always_inline void gen_op_evstwwo(DisasContext *ctx, TCGv addr)
7049 {
7050     gen_qemu_st32(ctx, cpu_gpr[rS(ctx->opcode)], addr);
7051 }
7052
7053 #define GEN_SPEOP_LDST(name, opc2, sh)                                        \
7054 GEN_HANDLER(name, 0x04, opc2, 0x0C, 0x00000000, PPC_SPE)                      \
7055 {                                                                             \
7056     TCGv t0;                                                                  \
7057     if (unlikely(!ctx->spe_enabled)) {                                        \
7058         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7059         return;                                                               \
7060     }                                                                         \
7061     gen_set_access_type(ctx, ACCESS_INT);                                     \
7062     t0 = tcg_temp_new();                                                      \
7063     if (Rc(ctx->opcode)) {                                                    \
7064         gen_addr_spe_imm_index(ctx, t0, sh);                                  \
7065     } else {                                                                  \
7066         gen_addr_reg_index(ctx, t0);                                          \
7067     }                                                                         \
7068     gen_op_##name(ctx, t0);                                                   \
7069     tcg_temp_free(t0);                                                        \
7070 }
7071
7072 GEN_SPEOP_LDST(evldd, 0x00, 3);
7073 GEN_SPEOP_LDST(evldw, 0x01, 3);
7074 GEN_SPEOP_LDST(evldh, 0x02, 3);
7075 GEN_SPEOP_LDST(evlhhesplat, 0x04, 1);
7076 GEN_SPEOP_LDST(evlhhousplat, 0x06, 1);
7077 GEN_SPEOP_LDST(evlhhossplat, 0x07, 1);
7078 GEN_SPEOP_LDST(evlwhe, 0x08, 2);
7079 GEN_SPEOP_LDST(evlwhou, 0x0A, 2);
7080 GEN_SPEOP_LDST(evlwhos, 0x0B, 2);
7081 GEN_SPEOP_LDST(evlwwsplat, 0x0C, 2);
7082 GEN_SPEOP_LDST(evlwhsplat, 0x0E, 2);
7083
7084 GEN_SPEOP_LDST(evstdd, 0x10, 3);
7085 GEN_SPEOP_LDST(evstdw, 0x11, 3);
7086 GEN_SPEOP_LDST(evstdh, 0x12, 3);
7087 GEN_SPEOP_LDST(evstwhe, 0x18, 2);
7088 GEN_SPEOP_LDST(evstwho, 0x1A, 2);
7089 GEN_SPEOP_LDST(evstwwe, 0x1C, 2);
7090 GEN_SPEOP_LDST(evstwwo, 0x1E, 2);
7091
7092 /* Multiply and add - TODO */
7093 #if 0
7094 GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
7095 GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
7096 GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
7097 GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
7098 GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
7099 GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
7100 GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
7101 GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
7102 GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
7103 GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
7104 GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
7105 GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
7106
7107 GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
7108 GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
7109 GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
7110 GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
7111 GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
7112 GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
7113 GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
7114 GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
7115 GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
7116 GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
7117 GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
7118 GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
7119 GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
7120 GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
7121
7122 GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, PPC_SPE);
7123 GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, PPC_SPE);
7124 GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, PPC_SPE);
7125 GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, PPC_SPE);
7126 GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, PPC_SPE);
7127 GEN_SPE(evmra,          speundef,      0x07, 0x13, 0x0000F800, PPC_SPE);
7128
7129 GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
7130 GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
7131 GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
7132 GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
7133 GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
7134 GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
7135 GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
7136 GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
7137 GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
7138 GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
7139 GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
7140 GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
7141
7142 GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
7143 GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
7144 GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
7145 GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
7146 GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
7147
7148 GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
7149 GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
7150 GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
7151 GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
7152 GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
7153 GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
7154 GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
7155 GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
7156 GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
7157 GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
7158 GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
7159 GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
7160
7161 GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
7162 GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
7163 GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
7164 GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
7165 GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
7166 #endif
7167
7168 /***                      SPE floating-point extension                     ***/
7169 #if defined(TARGET_PPC64)
7170 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7171 static always_inline void gen_##name (DisasContext *ctx)                      \
7172 {                                                                             \
7173     TCGv_i32 t0;                                                              \
7174     TCGv t1;                                                                  \
7175     t0 = tcg_temp_new_i32();                                                  \
7176     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
7177     gen_helper_##name(t0, t0);                                                \
7178     t1 = tcg_temp_new();                                                      \
7179     tcg_gen_extu_i32_tl(t1, t0);                                              \
7180     tcg_temp_free_i32(t0);                                                    \
7181     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7182                     0xFFFFFFFF00000000ULL);                                   \
7183     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
7184     tcg_temp_free(t1);                                                        \
7185 }
7186 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7187 static always_inline void gen_##name (DisasContext *ctx)                      \
7188 {                                                                             \
7189     TCGv_i32 t0;                                                              \
7190     TCGv t1;                                                                  \
7191     t0 = tcg_temp_new_i32();                                                  \
7192     gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7193     t1 = tcg_temp_new();                                                      \
7194     tcg_gen_extu_i32_tl(t1, t0);                                              \
7195     tcg_temp_free_i32(t0);                                                    \
7196     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7197                     0xFFFFFFFF00000000ULL);                                   \
7198     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
7199     tcg_temp_free(t1);                                                        \
7200 }
7201 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7202 static always_inline void gen_##name (DisasContext *ctx)                      \
7203 {                                                                             \
7204     TCGv_i32 t0 = tcg_temp_new_i32();                                         \
7205     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
7206     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7207     tcg_temp_free_i32(t0);                                                    \
7208 }
7209 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7210 static always_inline void gen_##name (DisasContext *ctx)                      \
7211 {                                                                             \
7212     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7213 }
7214 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7215 static always_inline void gen_##name (DisasContext *ctx)                      \
7216 {                                                                             \
7217     TCGv_i32 t0, t1;                                                          \
7218     TCGv_i64 t2;                                                              \
7219     if (unlikely(!ctx->spe_enabled)) {                                        \
7220         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7221         return;                                                               \
7222     }                                                                         \
7223     t0 = tcg_temp_new_i32();                                                  \
7224     t1 = tcg_temp_new_i32();                                                  \
7225     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7226     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7227     gen_helper_##name(t0, t0, t1);                                            \
7228     tcg_temp_free_i32(t1);                                                    \
7229     t2 = tcg_temp_new();                                                      \
7230     tcg_gen_extu_i32_tl(t2, t0);                                              \
7231     tcg_temp_free_i32(t0);                                                    \
7232     tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7233                     0xFFFFFFFF00000000ULL);                                   \
7234     tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
7235     tcg_temp_free(t2);                                                        \
7236 }
7237 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7238 static always_inline void gen_##name (DisasContext *ctx)                      \
7239 {                                                                             \
7240     if (unlikely(!ctx->spe_enabled)) {                                        \
7241         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7242         return;                                                               \
7243     }                                                                         \
7244     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
7245                       cpu_gpr[rB(ctx->opcode)]);                              \
7246 }
7247 #define GEN_SPEFPUOP_COMP_32(name)                                            \
7248 static always_inline void gen_##name (DisasContext *ctx)                      \
7249 {                                                                             \
7250     TCGv_i32 t0, t1;                                                          \
7251     if (unlikely(!ctx->spe_enabled)) {                                        \
7252         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7253         return;                                                               \
7254     }                                                                         \
7255     t0 = tcg_temp_new_i32();                                                  \
7256     t1 = tcg_temp_new_i32();                                                  \
7257     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7258     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7259     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7260     tcg_temp_free_i32(t0);                                                    \
7261     tcg_temp_free_i32(t1);                                                    \
7262 }
7263 #define GEN_SPEFPUOP_COMP_64(name)                                            \
7264 static always_inline void gen_##name (DisasContext *ctx)                      \
7265 {                                                                             \
7266     if (unlikely(!ctx->spe_enabled)) {                                        \
7267         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7268         return;                                                               \
7269     }                                                                         \
7270     gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7271                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7272 }
7273 #else
7274 #define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7275 static always_inline void gen_##name (DisasContext *ctx)                      \
7276 {                                                                             \
7277     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7278 }
7279 #define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7280 static always_inline void gen_##name (DisasContext *ctx)                      \
7281 {                                                                             \
7282     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7283     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7284     gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7285     tcg_temp_free_i64(t0);                                                    \
7286 }
7287 #define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7288 static always_inline void gen_##name (DisasContext *ctx)                      \
7289 {                                                                             \
7290     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7291     gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7292     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7293     tcg_temp_free_i64(t0);                                                    \
7294 }
7295 #define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7296 static always_inline void gen_##name (DisasContext *ctx)                      \
7297 {                                                                             \
7298     TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7299     gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7300     gen_helper_##name(t0, t0);                                                \
7301     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7302     tcg_temp_free_i64(t0);                                                    \
7303 }
7304 #define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7305 static always_inline void gen_##name (DisasContext *ctx)                      \
7306 {                                                                             \
7307     if (unlikely(!ctx->spe_enabled)) {                                        \
7308         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7309         return;                                                               \
7310     }                                                                         \
7311     gen_helper_##name(cpu_gpr[rD(ctx->opcode)],                               \
7312                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7313 }
7314 #define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7315 static always_inline void gen_##name (DisasContext *ctx)                      \
7316 {                                                                             \
7317     TCGv_i64 t0, t1;                                                          \
7318     if (unlikely(!ctx->spe_enabled)) {                                        \
7319         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7320         return;                                                               \
7321     }                                                                         \
7322     t0 = tcg_temp_new_i64();                                                  \
7323     t1 = tcg_temp_new_i64();                                                  \
7324     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7325     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7326     gen_helper_##name(t0, t0, t1);                                            \
7327     gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7328     tcg_temp_free_i64(t0);                                                    \
7329     tcg_temp_free_i64(t1);                                                    \
7330 }
7331 #define GEN_SPEFPUOP_COMP_32(name)                                            \
7332 static always_inline void gen_##name (DisasContext *ctx)                      \
7333 {                                                                             \
7334     if (unlikely(!ctx->spe_enabled)) {                                        \
7335         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7336         return;                                                               \
7337     }                                                                         \
7338     gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7339                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7340 }
7341 #define GEN_SPEFPUOP_COMP_64(name)                                            \
7342 static always_inline void gen_##name (DisasContext *ctx)                      \
7343 {                                                                             \
7344     TCGv_i64 t0, t1;                                                          \
7345     if (unlikely(!ctx->spe_enabled)) {                                        \
7346         gen_exception(ctx, POWERPC_EXCP_APU);                                 \
7347         return;                                                               \
7348     }                                                                         \
7349     t0 = tcg_temp_new_i64();                                                  \
7350     t1 = tcg_temp_new_i64();                                                  \
7351     gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7352     gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7353     gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7354     tcg_temp_free_i64(t0);                                                    \
7355     tcg_temp_free_i64(t1);                                                    \
7356 }
7357 #endif
7358
7359 /* Single precision floating-point vectors operations */
7360 /* Arithmetic */
7361 GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7362 GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7363 GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7364 GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7365 static always_inline void gen_evfsabs (DisasContext *ctx)
7366 {
7367     if (unlikely(!ctx->spe_enabled)) {
7368         gen_exception(ctx, POWERPC_EXCP_APU);
7369         return;
7370     }
7371 #if defined(TARGET_PPC64)
7372     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7373 #else
7374     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7375     tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7376 #endif
7377 }
7378 static always_inline void gen_evfsnabs (DisasContext *ctx)
7379 {
7380     if (unlikely(!ctx->spe_enabled)) {
7381         gen_exception(ctx, POWERPC_EXCP_APU);
7382         return;
7383     }
7384 #if defined(TARGET_PPC64)
7385     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7386 #else
7387     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7388     tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7389 #endif
7390 }
7391 static always_inline void gen_evfsneg (DisasContext *ctx)
7392 {
7393     if (unlikely(!ctx->spe_enabled)) {
7394         gen_exception(ctx, POWERPC_EXCP_APU);
7395         return;
7396     }
7397 #if defined(TARGET_PPC64)
7398     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7399 #else
7400     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7401     tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7402 #endif
7403 }
7404
7405 /* Conversion */
7406 GEN_SPEFPUOP_CONV_64_64(evfscfui);
7407 GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7408 GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7409 GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7410 GEN_SPEFPUOP_CONV_64_64(evfsctui);
7411 GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7412 GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7413 GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7414 GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7415 GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7416
7417 /* Comparison */
7418 GEN_SPEFPUOP_COMP_64(evfscmpgt);
7419 GEN_SPEFPUOP_COMP_64(evfscmplt);
7420 GEN_SPEFPUOP_COMP_64(evfscmpeq);
7421 GEN_SPEFPUOP_COMP_64(evfststgt);
7422 GEN_SPEFPUOP_COMP_64(evfststlt);
7423 GEN_SPEFPUOP_COMP_64(evfststeq);
7424
7425 /* Opcodes definitions */
7426 GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7427 GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7428 GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7429 GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7430 GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7431 GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7432 GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7433 GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7434 GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7435 GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7436 GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7437 GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7438 GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7439 GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7440
7441 /* Single precision floating-point operations */
7442 /* Arithmetic */
7443 GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7444 GEN_SPEFPUOP_ARITH2_32_32(efssub);
7445 GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7446 GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7447 static always_inline void gen_efsabs (DisasContext *ctx)
7448 {
7449     if (unlikely(!ctx->spe_enabled)) {
7450         gen_exception(ctx, POWERPC_EXCP_APU);
7451         return;
7452     }
7453     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7454 }
7455 static always_inline void gen_efsnabs (DisasContext *ctx)
7456 {
7457     if (unlikely(!ctx->spe_enabled)) {
7458         gen_exception(ctx, POWERPC_EXCP_APU);
7459         return;
7460     }
7461     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7462 }
7463 static always_inline void gen_efsneg (DisasContext *ctx)
7464 {
7465     if (unlikely(!ctx->spe_enabled)) {
7466         gen_exception(ctx, POWERPC_EXCP_APU);
7467         return;
7468     }
7469     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7470 }
7471
7472 /* Conversion */
7473 GEN_SPEFPUOP_CONV_32_32(efscfui);
7474 GEN_SPEFPUOP_CONV_32_32(efscfsi);
7475 GEN_SPEFPUOP_CONV_32_32(efscfuf);
7476 GEN_SPEFPUOP_CONV_32_32(efscfsf);
7477 GEN_SPEFPUOP_CONV_32_32(efsctui);
7478 GEN_SPEFPUOP_CONV_32_32(efsctsi);
7479 GEN_SPEFPUOP_CONV_32_32(efsctuf);
7480 GEN_SPEFPUOP_CONV_32_32(efsctsf);
7481 GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7482 GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7483 GEN_SPEFPUOP_CONV_32_64(efscfd);
7484
7485 /* Comparison */
7486 GEN_SPEFPUOP_COMP_32(efscmpgt);
7487 GEN_SPEFPUOP_COMP_32(efscmplt);
7488 GEN_SPEFPUOP_COMP_32(efscmpeq);
7489 GEN_SPEFPUOP_COMP_32(efststgt);
7490 GEN_SPEFPUOP_COMP_32(efststlt);
7491 GEN_SPEFPUOP_COMP_32(efststeq);
7492
7493 /* Opcodes definitions */
7494 GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7495 GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7496 GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7497 GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7498 GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7499 GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7500 GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7501 GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7502 GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7503 GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
7504 GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7505 GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7506 GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7507 GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7508
7509 /* Double precision floating-point operations */
7510 /* Arithmetic */
7511 GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7512 GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7513 GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7514 GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7515 static always_inline void gen_efdabs (DisasContext *ctx)
7516 {
7517     if (unlikely(!ctx->spe_enabled)) {
7518         gen_exception(ctx, POWERPC_EXCP_APU);
7519         return;
7520     }
7521 #if defined(TARGET_PPC64)
7522     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7523 #else
7524     tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7525 #endif
7526 }
7527 static always_inline void gen_efdnabs (DisasContext *ctx)
7528 {
7529     if (unlikely(!ctx->spe_enabled)) {
7530         gen_exception(ctx, POWERPC_EXCP_APU);
7531         return;
7532     }
7533 #if defined(TARGET_PPC64)
7534     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7535 #else
7536     tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7537 #endif
7538 }
7539 static always_inline void gen_efdneg (DisasContext *ctx)
7540 {
7541     if (unlikely(!ctx->spe_enabled)) {
7542         gen_exception(ctx, POWERPC_EXCP_APU);
7543         return;
7544     }
7545 #if defined(TARGET_PPC64)
7546     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7547 #else
7548     tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7549 #endif
7550 }
7551
7552 /* Conversion */
7553 GEN_SPEFPUOP_CONV_64_32(efdcfui);
7554 GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7555 GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7556 GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7557 GEN_SPEFPUOP_CONV_32_64(efdctui);
7558 GEN_SPEFPUOP_CONV_32_64(efdctsi);
7559 GEN_SPEFPUOP_CONV_32_64(efdctuf);
7560 GEN_SPEFPUOP_CONV_32_64(efdctsf);
7561 GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7562 GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7563 GEN_SPEFPUOP_CONV_64_32(efdcfs);
7564 GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7565 GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7566 GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7567 GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7568
7569 /* Comparison */
7570 GEN_SPEFPUOP_COMP_64(efdcmpgt);
7571 GEN_SPEFPUOP_COMP_64(efdcmplt);
7572 GEN_SPEFPUOP_COMP_64(efdcmpeq);
7573 GEN_SPEFPUOP_COMP_64(efdtstgt);
7574 GEN_SPEFPUOP_COMP_64(efdtstlt);
7575 GEN_SPEFPUOP_COMP_64(efdtsteq);
7576
7577 /* Opcodes definitions */
7578 GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7579 GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7580 GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7581 GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7582 GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7583 GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7584 GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7585 GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7586 GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7587 GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7588 GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7589 GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7590 GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7591 GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7592 GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7593 GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
7594
7595 /* End opcode list */
7596 GEN_OPCODE_MARK(end);
7597
7598 #include "translate_init.c"
7599 #include "helper_regs.h"
7600
7601 /*****************************************************************************/
7602 /* Misc PowerPC helpers */
7603 void cpu_dump_state (CPUState *env, FILE *f,
7604                      int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7605                      int flags)
7606 {
7607 #define RGPL  4
7608 #define RFPL  4
7609
7610     int i;
7611
7612     cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
7613                 env->nip, env->lr, env->ctr, env->xer);
7614     cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
7615                 env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7616 #if !defined(NO_TIMER_DUMP)
7617     cpu_fprintf(f, "TB %08x %08x "
7618 #if !defined(CONFIG_USER_ONLY)
7619                 "DECR %08x"
7620 #endif
7621                 "\n",
7622                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7623 #if !defined(CONFIG_USER_ONLY)
7624                 , cpu_ppc_load_decr(env)
7625 #endif
7626                 );
7627 #endif
7628     for (i = 0; i < 32; i++) {
7629         if ((i & (RGPL - 1)) == 0)
7630             cpu_fprintf(f, "GPR%02d", i);
7631         cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7632         if ((i & (RGPL - 1)) == (RGPL - 1))
7633             cpu_fprintf(f, "\n");
7634     }
7635     cpu_fprintf(f, "CR ");
7636     for (i = 0; i < 8; i++)
7637         cpu_fprintf(f, "%01x", env->crf[i]);
7638     cpu_fprintf(f, "  [");
7639     for (i = 0; i < 8; i++) {
7640         char a = '-';
7641         if (env->crf[i] & 0x08)
7642             a = 'L';
7643         else if (env->crf[i] & 0x04)
7644             a = 'G';
7645         else if (env->crf[i] & 0x02)
7646             a = 'E';
7647         cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7648     }
7649     cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
7650     for (i = 0; i < 32; i++) {
7651         if ((i & (RFPL - 1)) == 0)
7652             cpu_fprintf(f, "FPR%02d", i);
7653         cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7654         if ((i & (RFPL - 1)) == (RFPL - 1))
7655             cpu_fprintf(f, "\n");
7656     }
7657     cpu_fprintf(f, "FPSCR %08x\n", env->fpscr);
7658 #if !defined(CONFIG_USER_ONLY)
7659     cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7660                 env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7661 #endif
7662
7663 #undef RGPL
7664 #undef RFPL
7665 }
7666
7667 void cpu_dump_statistics (CPUState *env, FILE*f,
7668                           int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7669                           int flags)
7670 {
7671 #if defined(DO_PPC_STATISTICS)
7672     opc_handler_t **t1, **t2, **t3, *handler;
7673     int op1, op2, op3;
7674
7675     t1 = env->opcodes;
7676     for (op1 = 0; op1 < 64; op1++) {
7677         handler = t1[op1];
7678         if (is_indirect_opcode(handler)) {
7679             t2 = ind_table(handler);
7680             for (op2 = 0; op2 < 32; op2++) {
7681                 handler = t2[op2];
7682                 if (is_indirect_opcode(handler)) {
7683                     t3 = ind_table(handler);
7684                     for (op3 = 0; op3 < 32; op3++) {
7685                         handler = t3[op3];
7686                         if (handler->count == 0)
7687                             continue;
7688                         cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7689                                     "%016llx %lld\n",
7690                                     op1, op2, op3, op1, (op3 << 5) | op2,
7691                                     handler->oname,
7692                                     handler->count, handler->count);
7693                     }
7694                 } else {
7695                     if (handler->count == 0)
7696                         continue;
7697                     cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
7698                                 "%016llx %lld\n",
7699                                 op1, op2, op1, op2, handler->oname,
7700                                 handler->count, handler->count);
7701                 }
7702             }
7703         } else {
7704             if (handler->count == 0)
7705                 continue;
7706             cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
7707                         op1, op1, handler->oname,
7708                         handler->count, handler->count);
7709         }
7710     }
7711 #endif
7712 }
7713
7714 /*****************************************************************************/
7715 static always_inline void gen_intermediate_code_internal (CPUState *env,
7716                                                           TranslationBlock *tb,
7717                                                           int search_pc)
7718 {
7719     DisasContext ctx, *ctxp = &ctx;
7720     opc_handler_t **table, *handler;
7721     target_ulong pc_start;
7722     uint16_t *gen_opc_end;
7723     CPUBreakpoint *bp;
7724     int j, lj = -1;
7725     int num_insns;
7726     int max_insns;
7727
7728     pc_start = tb->pc;
7729     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7730 #if defined(OPTIMIZE_FPRF_UPDATE)
7731     gen_fprf_ptr = gen_fprf_buf;
7732 #endif
7733     ctx.nip = pc_start;
7734     ctx.tb = tb;
7735     ctx.exception = POWERPC_EXCP_NONE;
7736     ctx.spr_cb = env->spr_cb;
7737     ctx.mem_idx = env->mmu_idx;
7738     ctx.access_type = -1;
7739     ctx.le_mode = env->hflags & (1 << MSR_LE) ? 1 : 0;
7740 #if defined(TARGET_PPC64)
7741     ctx.sf_mode = msr_sf;
7742 #endif
7743     ctx.fpu_enabled = msr_fp;
7744     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7745         ctx.spe_enabled = msr_spe;
7746     else
7747         ctx.spe_enabled = 0;
7748     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7749         ctx.altivec_enabled = msr_vr;
7750     else
7751         ctx.altivec_enabled = 0;
7752     if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7753         ctx.singlestep_enabled = CPU_SINGLE_STEP;
7754     else
7755         ctx.singlestep_enabled = 0;
7756     if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7757         ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7758     if (unlikely(env->singlestep_enabled))
7759         ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7760 #if defined (DO_SINGLE_STEP) && 0
7761     /* Single step trace mode */
7762     msr_se = 1;
7763 #endif
7764     num_insns = 0;
7765     max_insns = tb->cflags & CF_COUNT_MASK;
7766     if (max_insns == 0)
7767         max_insns = CF_COUNT_MASK;
7768
7769     gen_icount_start();
7770     /* Set env in case of segfault during code fetch */
7771     while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7772         if (unlikely(!TAILQ_EMPTY(&env->breakpoints))) {
7773             TAILQ_FOREACH(bp, &env->breakpoints, entry) {
7774                 if (bp->pc == ctx.nip) {
7775                     gen_debug_exception(ctxp);
7776                     break;
7777                 }
7778             }
7779         }
7780         if (unlikely(search_pc)) {
7781             j = gen_opc_ptr - gen_opc_buf;
7782             if (lj < j) {
7783                 lj++;
7784                 while (lj < j)
7785                     gen_opc_instr_start[lj++] = 0;
7786                 gen_opc_pc[lj] = ctx.nip;
7787                 gen_opc_instr_start[lj] = 1;
7788                 gen_opc_icount[lj] = num_insns;
7789             }
7790         }
7791 #if defined PPC_DEBUG_DISAS
7792         if (loglevel & CPU_LOG_TB_IN_ASM) {
7793             fprintf(logfile, "----------------\n");
7794             fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
7795                     ctx.nip, ctx.mem_idx, (int)msr_ir);
7796         }
7797 #endif
7798         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7799             gen_io_start();
7800         if (unlikely(ctx.le_mode)) {
7801             ctx.opcode = bswap32(ldl_code(ctx.nip));
7802         } else {
7803             ctx.opcode = ldl_code(ctx.nip);
7804         }
7805 #if defined PPC_DEBUG_DISAS
7806         if (loglevel & CPU_LOG_TB_IN_ASM) {
7807             fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7808                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7809                     opc3(ctx.opcode), little_endian ? "little" : "big");
7810         }
7811 #endif
7812         ctx.nip += 4;
7813         table = env->opcodes;
7814         num_insns++;
7815         handler = table[opc1(ctx.opcode)];
7816         if (is_indirect_opcode(handler)) {
7817             table = ind_table(handler);
7818             handler = table[opc2(ctx.opcode)];
7819             if (is_indirect_opcode(handler)) {
7820                 table = ind_table(handler);
7821                 handler = table[opc3(ctx.opcode)];
7822             }
7823         }
7824         /* Is opcode *REALLY* valid ? */
7825         if (unlikely(handler->handler == &gen_invalid)) {
7826             if (loglevel != 0) {
7827                 fprintf(logfile, "invalid/unsupported opcode: "
7828                         "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7829                         opc1(ctx.opcode), opc2(ctx.opcode),
7830                         opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7831             } else {
7832                 printf("invalid/unsupported opcode: "
7833                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7834                        opc1(ctx.opcode), opc2(ctx.opcode),
7835                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7836             }
7837         } else {
7838             if (unlikely((ctx.opcode & handler->inval) != 0)) {
7839                 if (loglevel != 0) {
7840                     fprintf(logfile, "invalid bits: %08x for opcode: "
7841                             "%02x - %02x - %02x (%08x) " ADDRX "\n",
7842                             ctx.opcode & handler->inval, opc1(ctx.opcode),
7843                             opc2(ctx.opcode), opc3(ctx.opcode),
7844                             ctx.opcode, ctx.nip - 4);
7845                 } else {
7846                     printf("invalid bits: %08x for opcode: "
7847                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
7848                            ctx.opcode & handler->inval, opc1(ctx.opcode),
7849                            opc2(ctx.opcode), opc3(ctx.opcode),
7850                            ctx.opcode, ctx.nip - 4);
7851                 }
7852                 gen_inval_exception(ctxp, POWERPC_EXCP_INVAL_INVAL);
7853                 break;
7854             }
7855         }
7856         (*(handler->handler))(&ctx);
7857 #if defined(DO_PPC_STATISTICS)
7858         handler->count++;
7859 #endif
7860         /* Check trace mode exceptions */
7861         if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7862                      (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7863                      ctx.exception != POWERPC_SYSCALL &&
7864                      ctx.exception != POWERPC_EXCP_TRAP &&
7865                      ctx.exception != POWERPC_EXCP_BRANCH)) {
7866             gen_exception(ctxp, POWERPC_EXCP_TRACE);
7867         } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7868                             (env->singlestep_enabled) ||
7869                             num_insns >= max_insns)) {
7870             /* if we reach a page boundary or are single stepping, stop
7871              * generation
7872              */
7873             break;
7874         }
7875 #if defined (DO_SINGLE_STEP)
7876         break;
7877 #endif
7878     }
7879     if (tb->cflags & CF_LAST_IO)
7880         gen_io_end();
7881     if (ctx.exception == POWERPC_EXCP_NONE) {
7882         gen_goto_tb(&ctx, 0, ctx.nip);
7883     } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7884         if (unlikely(env->singlestep_enabled)) {
7885             gen_debug_exception(ctxp);
7886         }
7887         /* Generate the return instruction */
7888         tcg_gen_exit_tb(0);
7889     }
7890     gen_icount_end(tb, num_insns);
7891     *gen_opc_ptr = INDEX_op_end;
7892     if (unlikely(search_pc)) {
7893         j = gen_opc_ptr - gen_opc_buf;
7894         lj++;
7895         while (lj <= j)
7896             gen_opc_instr_start[lj++] = 0;
7897     } else {
7898         tb->size = ctx.nip - pc_start;
7899         tb->icount = num_insns;
7900     }
7901 #if defined(DEBUG_DISAS)
7902     if (loglevel & CPU_LOG_TB_CPU) {
7903         fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7904         cpu_dump_state(env, logfile, fprintf, 0);
7905     }
7906     if (loglevel & CPU_LOG_TB_IN_ASM) {
7907         int flags;
7908         flags = env->bfd_mach;
7909         flags |= ctx.le_mode << 16;
7910         fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7911         target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7912         fprintf(logfile, "\n");
7913     }
7914 #endif
7915 }
7916
7917 void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7918 {
7919     gen_intermediate_code_internal(env, tb, 0);
7920 }
7921
7922 void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7923 {
7924     gen_intermediate_code_internal(env, tb, 1);
7925 }
7926
7927 void gen_pc_load(CPUState *env, TranslationBlock *tb,
7928                 unsigned long searched_pc, int pc_pos, void *puc)
7929 {
7930     env->nip = gen_opc_pc[pc_pos];
7931 }