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