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