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