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