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