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