]> Git Repo - qemu.git/blob - target/s390x/translate.c
tcg: Pass generic CPUState to gen_intermediate_code()
[qemu.git] / target / s390x / translate.c
1 /*
2  *  S/390 translation
3  *
4  *  Copyright (c) 2009 Ulrich Hecht
5  *  Copyright (c) 2010 Alexander Graf
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /* #define DEBUG_INLINE_BRANCHES */
22 #define S390X_DEBUG_DISAS
23 /* #define S390X_DEBUG_DISAS_VERBOSE */
24
25 #ifdef S390X_DEBUG_DISAS_VERBOSE
26 #  define LOG_DISAS(...) qemu_log(__VA_ARGS__)
27 #else
28 #  define LOG_DISAS(...) do { } while (0)
29 #endif
30
31 #include "qemu/osdep.h"
32 #include "cpu.h"
33 #include "disas/disas.h"
34 #include "exec/exec-all.h"
35 #include "tcg-op.h"
36 #include "qemu/log.h"
37 #include "qemu/host-utils.h"
38 #include "exec/cpu_ldst.h"
39
40 /* global register indexes */
41 static TCGv_env cpu_env;
42
43 #include "exec/gen-icount.h"
44 #include "exec/helper-proto.h"
45 #include "exec/helper-gen.h"
46
47 #include "trace-tcg.h"
48 #include "exec/log.h"
49
50
51 /* Information that (most) every instruction needs to manipulate.  */
52 typedef struct DisasContext DisasContext;
53 typedef struct DisasInsn DisasInsn;
54 typedef struct DisasFields DisasFields;
55
56 struct DisasContext {
57     struct TranslationBlock *tb;
58     const DisasInsn *insn;
59     DisasFields *fields;
60     uint64_t ex_value;
61     uint64_t pc, next_pc;
62     uint32_t ilen;
63     enum cc_op cc_op;
64     bool singlestep_enabled;
65 };
66
67 /* Information carried about a condition to be evaluated.  */
68 typedef struct {
69     TCGCond cond:8;
70     bool is_64;
71     bool g1;
72     bool g2;
73     union {
74         struct { TCGv_i64 a, b; } s64;
75         struct { TCGv_i32 a, b; } s32;
76     } u;
77 } DisasCompare;
78
79 #define DISAS_EXCP 4
80
81 #ifdef DEBUG_INLINE_BRANCHES
82 static uint64_t inline_branch_hit[CC_OP_MAX];
83 static uint64_t inline_branch_miss[CC_OP_MAX];
84 #endif
85
86 static uint64_t pc_to_link_info(DisasContext *s, uint64_t pc)
87 {
88     if (!(s->tb->flags & FLAG_MASK_64)) {
89         if (s->tb->flags & FLAG_MASK_32) {
90             return pc | 0x80000000;
91         }
92     }
93     return pc;
94 }
95
96 void s390_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
97                          int flags)
98 {
99     S390CPU *cpu = S390_CPU(cs);
100     CPUS390XState *env = &cpu->env;
101     int i;
102
103     if (env->cc_op > 3) {
104         cpu_fprintf(f, "PSW=mask %016" PRIx64 " addr %016" PRIx64 " cc %15s\n",
105                     env->psw.mask, env->psw.addr, cc_name(env->cc_op));
106     } else {
107         cpu_fprintf(f, "PSW=mask %016" PRIx64 " addr %016" PRIx64 " cc %02x\n",
108                     env->psw.mask, env->psw.addr, env->cc_op);
109     }
110
111     for (i = 0; i < 16; i++) {
112         cpu_fprintf(f, "R%02d=%016" PRIx64, i, env->regs[i]);
113         if ((i % 4) == 3) {
114             cpu_fprintf(f, "\n");
115         } else {
116             cpu_fprintf(f, " ");
117         }
118     }
119
120     for (i = 0; i < 16; i++) {
121         cpu_fprintf(f, "F%02d=%016" PRIx64, i, get_freg(env, i)->ll);
122         if ((i % 4) == 3) {
123             cpu_fprintf(f, "\n");
124         } else {
125             cpu_fprintf(f, " ");
126         }
127     }
128
129     for (i = 0; i < 32; i++) {
130         cpu_fprintf(f, "V%02d=%016" PRIx64 "%016" PRIx64, i,
131                     env->vregs[i][0].ll, env->vregs[i][1].ll);
132         cpu_fprintf(f, (i % 2) ? "\n" : " ");
133     }
134
135 #ifndef CONFIG_USER_ONLY
136     for (i = 0; i < 16; i++) {
137         cpu_fprintf(f, "C%02d=%016" PRIx64, i, env->cregs[i]);
138         if ((i % 4) == 3) {
139             cpu_fprintf(f, "\n");
140         } else {
141             cpu_fprintf(f, " ");
142         }
143     }
144 #endif
145
146 #ifdef DEBUG_INLINE_BRANCHES
147     for (i = 0; i < CC_OP_MAX; i++) {
148         cpu_fprintf(f, "  %15s = %10ld\t%10ld\n", cc_name(i),
149                     inline_branch_miss[i], inline_branch_hit[i]);
150     }
151 #endif
152
153     cpu_fprintf(f, "\n");
154 }
155
156 static TCGv_i64 psw_addr;
157 static TCGv_i64 psw_mask;
158 static TCGv_i64 gbea;
159
160 static TCGv_i32 cc_op;
161 static TCGv_i64 cc_src;
162 static TCGv_i64 cc_dst;
163 static TCGv_i64 cc_vr;
164
165 static char cpu_reg_names[32][4];
166 static TCGv_i64 regs[16];
167 static TCGv_i64 fregs[16];
168
169 void s390x_translate_init(void)
170 {
171     int i;
172
173     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
174     tcg_ctx.tcg_env = cpu_env;
175     psw_addr = tcg_global_mem_new_i64(cpu_env,
176                                       offsetof(CPUS390XState, psw.addr),
177                                       "psw_addr");
178     psw_mask = tcg_global_mem_new_i64(cpu_env,
179                                       offsetof(CPUS390XState, psw.mask),
180                                       "psw_mask");
181     gbea = tcg_global_mem_new_i64(cpu_env,
182                                   offsetof(CPUS390XState, gbea),
183                                   "gbea");
184
185     cc_op = tcg_global_mem_new_i32(cpu_env, offsetof(CPUS390XState, cc_op),
186                                    "cc_op");
187     cc_src = tcg_global_mem_new_i64(cpu_env, offsetof(CPUS390XState, cc_src),
188                                     "cc_src");
189     cc_dst = tcg_global_mem_new_i64(cpu_env, offsetof(CPUS390XState, cc_dst),
190                                     "cc_dst");
191     cc_vr = tcg_global_mem_new_i64(cpu_env, offsetof(CPUS390XState, cc_vr),
192                                    "cc_vr");
193
194     for (i = 0; i < 16; i++) {
195         snprintf(cpu_reg_names[i], sizeof(cpu_reg_names[0]), "r%d", i);
196         regs[i] = tcg_global_mem_new(cpu_env,
197                                      offsetof(CPUS390XState, regs[i]),
198                                      cpu_reg_names[i]);
199     }
200
201     for (i = 0; i < 16; i++) {
202         snprintf(cpu_reg_names[i + 16], sizeof(cpu_reg_names[0]), "f%d", i);
203         fregs[i] = tcg_global_mem_new(cpu_env,
204                                       offsetof(CPUS390XState, vregs[i][0].d),
205                                       cpu_reg_names[i + 16]);
206     }
207 }
208
209 static TCGv_i64 load_reg(int reg)
210 {
211     TCGv_i64 r = tcg_temp_new_i64();
212     tcg_gen_mov_i64(r, regs[reg]);
213     return r;
214 }
215
216 static TCGv_i64 load_freg32_i64(int reg)
217 {
218     TCGv_i64 r = tcg_temp_new_i64();
219     tcg_gen_shri_i64(r, fregs[reg], 32);
220     return r;
221 }
222
223 static void store_reg(int reg, TCGv_i64 v)
224 {
225     tcg_gen_mov_i64(regs[reg], v);
226 }
227
228 static void store_freg(int reg, TCGv_i64 v)
229 {
230     tcg_gen_mov_i64(fregs[reg], v);
231 }
232
233 static void store_reg32_i64(int reg, TCGv_i64 v)
234 {
235     /* 32 bit register writes keep the upper half */
236     tcg_gen_deposit_i64(regs[reg], regs[reg], v, 0, 32);
237 }
238
239 static void store_reg32h_i64(int reg, TCGv_i64 v)
240 {
241     tcg_gen_deposit_i64(regs[reg], regs[reg], v, 32, 32);
242 }
243
244 static void store_freg32_i64(int reg, TCGv_i64 v)
245 {
246     tcg_gen_deposit_i64(fregs[reg], fregs[reg], v, 32, 32);
247 }
248
249 static void return_low128(TCGv_i64 dest)
250 {
251     tcg_gen_ld_i64(dest, cpu_env, offsetof(CPUS390XState, retxl));
252 }
253
254 static void update_psw_addr(DisasContext *s)
255 {
256     /* psw.addr */
257     tcg_gen_movi_i64(psw_addr, s->pc);
258 }
259
260 static void per_branch(DisasContext *s, bool to_next)
261 {
262 #ifndef CONFIG_USER_ONLY
263     tcg_gen_movi_i64(gbea, s->pc);
264
265     if (s->tb->flags & FLAG_MASK_PER) {
266         TCGv_i64 next_pc = to_next ? tcg_const_i64(s->next_pc) : psw_addr;
267         gen_helper_per_branch(cpu_env, gbea, next_pc);
268         if (to_next) {
269             tcg_temp_free_i64(next_pc);
270         }
271     }
272 #endif
273 }
274
275 static void per_branch_cond(DisasContext *s, TCGCond cond,
276                             TCGv_i64 arg1, TCGv_i64 arg2)
277 {
278 #ifndef CONFIG_USER_ONLY
279     if (s->tb->flags & FLAG_MASK_PER) {
280         TCGLabel *lab = gen_new_label();
281         tcg_gen_brcond_i64(tcg_invert_cond(cond), arg1, arg2, lab);
282
283         tcg_gen_movi_i64(gbea, s->pc);
284         gen_helper_per_branch(cpu_env, gbea, psw_addr);
285
286         gen_set_label(lab);
287     } else {
288         TCGv_i64 pc = tcg_const_i64(s->pc);
289         tcg_gen_movcond_i64(cond, gbea, arg1, arg2, gbea, pc);
290         tcg_temp_free_i64(pc);
291     }
292 #endif
293 }
294
295 static void per_breaking_event(DisasContext *s)
296 {
297     tcg_gen_movi_i64(gbea, s->pc);
298 }
299
300 static void update_cc_op(DisasContext *s)
301 {
302     if (s->cc_op != CC_OP_DYNAMIC && s->cc_op != CC_OP_STATIC) {
303         tcg_gen_movi_i32(cc_op, s->cc_op);
304     }
305 }
306
307 static void potential_page_fault(DisasContext *s)
308 {
309     update_psw_addr(s);
310     update_cc_op(s);
311 }
312
313 static inline uint64_t ld_code2(CPUS390XState *env, uint64_t pc)
314 {
315     return (uint64_t)cpu_lduw_code(env, pc);
316 }
317
318 static inline uint64_t ld_code4(CPUS390XState *env, uint64_t pc)
319 {
320     return (uint64_t)(uint32_t)cpu_ldl_code(env, pc);
321 }
322
323 static int get_mem_index(DisasContext *s)
324 {
325     switch (s->tb->flags & FLAG_MASK_ASC) {
326     case PSW_ASC_PRIMARY >> FLAG_MASK_PSW_SHIFT:
327         return 0;
328     case PSW_ASC_SECONDARY >> FLAG_MASK_PSW_SHIFT:
329         return 1;
330     case PSW_ASC_HOME >> FLAG_MASK_PSW_SHIFT:
331         return 2;
332     default:
333         tcg_abort();
334         break;
335     }
336 }
337
338 static void gen_exception(int excp)
339 {
340     TCGv_i32 tmp = tcg_const_i32(excp);
341     gen_helper_exception(cpu_env, tmp);
342     tcg_temp_free_i32(tmp);
343 }
344
345 static void gen_program_exception(DisasContext *s, int code)
346 {
347     TCGv_i32 tmp;
348
349     /* Remember what pgm exeption this was.  */
350     tmp = tcg_const_i32(code);
351     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUS390XState, int_pgm_code));
352     tcg_temp_free_i32(tmp);
353
354     tmp = tcg_const_i32(s->ilen);
355     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUS390XState, int_pgm_ilen));
356     tcg_temp_free_i32(tmp);
357
358     /* update the psw */
359     update_psw_addr(s);
360
361     /* Save off cc.  */
362     update_cc_op(s);
363
364     /* Trigger exception.  */
365     gen_exception(EXCP_PGM);
366 }
367
368 static inline void gen_illegal_opcode(DisasContext *s)
369 {
370     gen_program_exception(s, PGM_OPERATION);
371 }
372
373 static inline void gen_trap(DisasContext *s)
374 {
375     TCGv_i32 t;
376
377     /* Set DXC to 0xff.  */
378     t = tcg_temp_new_i32();
379     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUS390XState, fpc));
380     tcg_gen_ori_i32(t, t, 0xff00);
381     tcg_gen_st_i32(t, cpu_env, offsetof(CPUS390XState, fpc));
382     tcg_temp_free_i32(t);
383
384     gen_program_exception(s, PGM_DATA);
385 }
386
387 #ifndef CONFIG_USER_ONLY
388 static void check_privileged(DisasContext *s)
389 {
390     if (s->tb->flags & FLAG_MASK_PSTATE) {
391         gen_program_exception(s, PGM_PRIVILEGED);
392     }
393 }
394 #endif
395
396 static TCGv_i64 get_address(DisasContext *s, int x2, int b2, int d2)
397 {
398     TCGv_i64 tmp = tcg_temp_new_i64();
399     bool need_31 = !(s->tb->flags & FLAG_MASK_64);
400
401     /* Note that d2 is limited to 20 bits, signed.  If we crop negative
402        displacements early we create larger immedate addends.  */
403
404     /* Note that addi optimizes the imm==0 case.  */
405     if (b2 && x2) {
406         tcg_gen_add_i64(tmp, regs[b2], regs[x2]);
407         tcg_gen_addi_i64(tmp, tmp, d2);
408     } else if (b2) {
409         tcg_gen_addi_i64(tmp, regs[b2], d2);
410     } else if (x2) {
411         tcg_gen_addi_i64(tmp, regs[x2], d2);
412     } else {
413         if (need_31) {
414             d2 &= 0x7fffffff;
415             need_31 = false;
416         }
417         tcg_gen_movi_i64(tmp, d2);
418     }
419     if (need_31) {
420         tcg_gen_andi_i64(tmp, tmp, 0x7fffffff);
421     }
422
423     return tmp;
424 }
425
426 static inline bool live_cc_data(DisasContext *s)
427 {
428     return (s->cc_op != CC_OP_DYNAMIC
429             && s->cc_op != CC_OP_STATIC
430             && s->cc_op > 3);
431 }
432
433 static inline void gen_op_movi_cc(DisasContext *s, uint32_t val)
434 {
435     if (live_cc_data(s)) {
436         tcg_gen_discard_i64(cc_src);
437         tcg_gen_discard_i64(cc_dst);
438         tcg_gen_discard_i64(cc_vr);
439     }
440     s->cc_op = CC_OP_CONST0 + val;
441 }
442
443 static void gen_op_update1_cc_i64(DisasContext *s, enum cc_op op, TCGv_i64 dst)
444 {
445     if (live_cc_data(s)) {
446         tcg_gen_discard_i64(cc_src);
447         tcg_gen_discard_i64(cc_vr);
448     }
449     tcg_gen_mov_i64(cc_dst, dst);
450     s->cc_op = op;
451 }
452
453 static void gen_op_update2_cc_i64(DisasContext *s, enum cc_op op, TCGv_i64 src,
454                                   TCGv_i64 dst)
455 {
456     if (live_cc_data(s)) {
457         tcg_gen_discard_i64(cc_vr);
458     }
459     tcg_gen_mov_i64(cc_src, src);
460     tcg_gen_mov_i64(cc_dst, dst);
461     s->cc_op = op;
462 }
463
464 static void gen_op_update3_cc_i64(DisasContext *s, enum cc_op op, TCGv_i64 src,
465                                   TCGv_i64 dst, TCGv_i64 vr)
466 {
467     tcg_gen_mov_i64(cc_src, src);
468     tcg_gen_mov_i64(cc_dst, dst);
469     tcg_gen_mov_i64(cc_vr, vr);
470     s->cc_op = op;
471 }
472
473 static void set_cc_nz_u64(DisasContext *s, TCGv_i64 val)
474 {
475     gen_op_update1_cc_i64(s, CC_OP_NZ, val);
476 }
477
478 static void gen_set_cc_nz_f32(DisasContext *s, TCGv_i64 val)
479 {
480     gen_op_update1_cc_i64(s, CC_OP_NZ_F32, val);
481 }
482
483 static void gen_set_cc_nz_f64(DisasContext *s, TCGv_i64 val)
484 {
485     gen_op_update1_cc_i64(s, CC_OP_NZ_F64, val);
486 }
487
488 static void gen_set_cc_nz_f128(DisasContext *s, TCGv_i64 vh, TCGv_i64 vl)
489 {
490     gen_op_update2_cc_i64(s, CC_OP_NZ_F128, vh, vl);
491 }
492
493 /* CC value is in env->cc_op */
494 static void set_cc_static(DisasContext *s)
495 {
496     if (live_cc_data(s)) {
497         tcg_gen_discard_i64(cc_src);
498         tcg_gen_discard_i64(cc_dst);
499         tcg_gen_discard_i64(cc_vr);
500     }
501     s->cc_op = CC_OP_STATIC;
502 }
503
504 /* calculates cc into cc_op */
505 static void gen_op_calc_cc(DisasContext *s)
506 {
507     TCGv_i32 local_cc_op;
508     TCGv_i64 dummy;
509
510     TCGV_UNUSED_I32(local_cc_op);
511     TCGV_UNUSED_I64(dummy);
512     switch (s->cc_op) {
513     default:
514         dummy = tcg_const_i64(0);
515         /* FALLTHRU */
516     case CC_OP_ADD_64:
517     case CC_OP_ADDU_64:
518     case CC_OP_ADDC_64:
519     case CC_OP_SUB_64:
520     case CC_OP_SUBU_64:
521     case CC_OP_SUBB_64:
522     case CC_OP_ADD_32:
523     case CC_OP_ADDU_32:
524     case CC_OP_ADDC_32:
525     case CC_OP_SUB_32:
526     case CC_OP_SUBU_32:
527     case CC_OP_SUBB_32:
528         local_cc_op = tcg_const_i32(s->cc_op);
529         break;
530     case CC_OP_CONST0:
531     case CC_OP_CONST1:
532     case CC_OP_CONST2:
533     case CC_OP_CONST3:
534     case CC_OP_STATIC:
535     case CC_OP_DYNAMIC:
536         break;
537     }
538
539     switch (s->cc_op) {
540     case CC_OP_CONST0:
541     case CC_OP_CONST1:
542     case CC_OP_CONST2:
543     case CC_OP_CONST3:
544         /* s->cc_op is the cc value */
545         tcg_gen_movi_i32(cc_op, s->cc_op - CC_OP_CONST0);
546         break;
547     case CC_OP_STATIC:
548         /* env->cc_op already is the cc value */
549         break;
550     case CC_OP_NZ:
551     case CC_OP_ABS_64:
552     case CC_OP_NABS_64:
553     case CC_OP_ABS_32:
554     case CC_OP_NABS_32:
555     case CC_OP_LTGT0_32:
556     case CC_OP_LTGT0_64:
557     case CC_OP_COMP_32:
558     case CC_OP_COMP_64:
559     case CC_OP_NZ_F32:
560     case CC_OP_NZ_F64:
561     case CC_OP_FLOGR:
562         /* 1 argument */
563         gen_helper_calc_cc(cc_op, cpu_env, local_cc_op, dummy, cc_dst, dummy);
564         break;
565     case CC_OP_ICM:
566     case CC_OP_LTGT_32:
567     case CC_OP_LTGT_64:
568     case CC_OP_LTUGTU_32:
569     case CC_OP_LTUGTU_64:
570     case CC_OP_TM_32:
571     case CC_OP_TM_64:
572     case CC_OP_SLA_32:
573     case CC_OP_SLA_64:
574     case CC_OP_NZ_F128:
575         /* 2 arguments */
576         gen_helper_calc_cc(cc_op, cpu_env, local_cc_op, cc_src, cc_dst, dummy);
577         break;
578     case CC_OP_ADD_64:
579     case CC_OP_ADDU_64:
580     case CC_OP_ADDC_64:
581     case CC_OP_SUB_64:
582     case CC_OP_SUBU_64:
583     case CC_OP_SUBB_64:
584     case CC_OP_ADD_32:
585     case CC_OP_ADDU_32:
586     case CC_OP_ADDC_32:
587     case CC_OP_SUB_32:
588     case CC_OP_SUBU_32:
589     case CC_OP_SUBB_32:
590         /* 3 arguments */
591         gen_helper_calc_cc(cc_op, cpu_env, local_cc_op, cc_src, cc_dst, cc_vr);
592         break;
593     case CC_OP_DYNAMIC:
594         /* unknown operation - assume 3 arguments and cc_op in env */
595         gen_helper_calc_cc(cc_op, cpu_env, cc_op, cc_src, cc_dst, cc_vr);
596         break;
597     default:
598         tcg_abort();
599     }
600
601     if (!TCGV_IS_UNUSED_I32(local_cc_op)) {
602         tcg_temp_free_i32(local_cc_op);
603     }
604     if (!TCGV_IS_UNUSED_I64(dummy)) {
605         tcg_temp_free_i64(dummy);
606     }
607
608     /* We now have cc in cc_op as constant */
609     set_cc_static(s);
610 }
611
612 static bool use_exit_tb(DisasContext *s)
613 {
614     return (s->singlestep_enabled ||
615             (s->tb->cflags & CF_LAST_IO) ||
616             (s->tb->flags & FLAG_MASK_PER));
617 }
618
619 static bool use_goto_tb(DisasContext *s, uint64_t dest)
620 {
621     if (unlikely(use_exit_tb(s))) {
622         return false;
623     }
624 #ifndef CONFIG_USER_ONLY
625     return (dest & TARGET_PAGE_MASK) == (s->tb->pc & TARGET_PAGE_MASK) ||
626            (dest & TARGET_PAGE_MASK) == (s->pc & TARGET_PAGE_MASK);
627 #else
628     return true;
629 #endif
630 }
631
632 static void account_noninline_branch(DisasContext *s, int cc_op)
633 {
634 #ifdef DEBUG_INLINE_BRANCHES
635     inline_branch_miss[cc_op]++;
636 #endif
637 }
638
639 static void account_inline_branch(DisasContext *s, int cc_op)
640 {
641 #ifdef DEBUG_INLINE_BRANCHES
642     inline_branch_hit[cc_op]++;
643 #endif
644 }
645
646 /* Table of mask values to comparison codes, given a comparison as input.
647    For such, CC=3 should not be possible.  */
648 static const TCGCond ltgt_cond[16] = {
649     TCG_COND_NEVER,  TCG_COND_NEVER,     /*    |    |    | x */
650     TCG_COND_GT,     TCG_COND_GT,        /*    |    | GT | x */
651     TCG_COND_LT,     TCG_COND_LT,        /*    | LT |    | x */
652     TCG_COND_NE,     TCG_COND_NE,        /*    | LT | GT | x */
653     TCG_COND_EQ,     TCG_COND_EQ,        /* EQ |    |    | x */
654     TCG_COND_GE,     TCG_COND_GE,        /* EQ |    | GT | x */
655     TCG_COND_LE,     TCG_COND_LE,        /* EQ | LT |    | x */
656     TCG_COND_ALWAYS, TCG_COND_ALWAYS,    /* EQ | LT | GT | x */
657 };
658
659 /* Table of mask values to comparison codes, given a logic op as input.
660    For such, only CC=0 and CC=1 should be possible.  */
661 static const TCGCond nz_cond[16] = {
662     TCG_COND_NEVER, TCG_COND_NEVER,      /*    |    | x | x */
663     TCG_COND_NEVER, TCG_COND_NEVER,
664     TCG_COND_NE, TCG_COND_NE,            /*    | NE | x | x */
665     TCG_COND_NE, TCG_COND_NE,
666     TCG_COND_EQ, TCG_COND_EQ,            /* EQ |    | x | x */
667     TCG_COND_EQ, TCG_COND_EQ,
668     TCG_COND_ALWAYS, TCG_COND_ALWAYS,    /* EQ | NE | x | x */
669     TCG_COND_ALWAYS, TCG_COND_ALWAYS,
670 };
671
672 /* Interpret MASK in terms of S->CC_OP, and fill in C with all the
673    details required to generate a TCG comparison.  */
674 static void disas_jcc(DisasContext *s, DisasCompare *c, uint32_t mask)
675 {
676     TCGCond cond;
677     enum cc_op old_cc_op = s->cc_op;
678
679     if (mask == 15 || mask == 0) {
680         c->cond = (mask ? TCG_COND_ALWAYS : TCG_COND_NEVER);
681         c->u.s32.a = cc_op;
682         c->u.s32.b = cc_op;
683         c->g1 = c->g2 = true;
684         c->is_64 = false;
685         return;
686     }
687
688     /* Find the TCG condition for the mask + cc op.  */
689     switch (old_cc_op) {
690     case CC_OP_LTGT0_32:
691     case CC_OP_LTGT0_64:
692     case CC_OP_LTGT_32:
693     case CC_OP_LTGT_64:
694         cond = ltgt_cond[mask];
695         if (cond == TCG_COND_NEVER) {
696             goto do_dynamic;
697         }
698         account_inline_branch(s, old_cc_op);
699         break;
700
701     case CC_OP_LTUGTU_32:
702     case CC_OP_LTUGTU_64:
703         cond = tcg_unsigned_cond(ltgt_cond[mask]);
704         if (cond == TCG_COND_NEVER) {
705             goto do_dynamic;
706         }
707         account_inline_branch(s, old_cc_op);
708         break;
709
710     case CC_OP_NZ:
711         cond = nz_cond[mask];
712         if (cond == TCG_COND_NEVER) {
713             goto do_dynamic;
714         }
715         account_inline_branch(s, old_cc_op);
716         break;
717
718     case CC_OP_TM_32:
719     case CC_OP_TM_64:
720         switch (mask) {
721         case 8:
722             cond = TCG_COND_EQ;
723             break;
724         case 4 | 2 | 1:
725             cond = TCG_COND_NE;
726             break;
727         default:
728             goto do_dynamic;
729         }
730         account_inline_branch(s, old_cc_op);
731         break;
732
733     case CC_OP_ICM:
734         switch (mask) {
735         case 8:
736             cond = TCG_COND_EQ;
737             break;
738         case 4 | 2 | 1:
739         case 4 | 2:
740             cond = TCG_COND_NE;
741             break;
742         default:
743             goto do_dynamic;
744         }
745         account_inline_branch(s, old_cc_op);
746         break;
747
748     case CC_OP_FLOGR:
749         switch (mask & 0xa) {
750         case 8: /* src == 0 -> no one bit found */
751             cond = TCG_COND_EQ;
752             break;
753         case 2: /* src != 0 -> one bit found */
754             cond = TCG_COND_NE;
755             break;
756         default:
757             goto do_dynamic;
758         }
759         account_inline_branch(s, old_cc_op);
760         break;
761
762     case CC_OP_ADDU_32:
763     case CC_OP_ADDU_64:
764         switch (mask) {
765         case 8 | 2: /* vr == 0 */
766             cond = TCG_COND_EQ;
767             break;
768         case 4 | 1: /* vr != 0 */
769             cond = TCG_COND_NE;
770             break;
771         case 8 | 4: /* no carry -> vr >= src */
772             cond = TCG_COND_GEU;
773             break;
774         case 2 | 1: /* carry -> vr < src */
775             cond = TCG_COND_LTU;
776             break;
777         default:
778             goto do_dynamic;
779         }
780         account_inline_branch(s, old_cc_op);
781         break;
782
783     case CC_OP_SUBU_32:
784     case CC_OP_SUBU_64:
785         /* Note that CC=0 is impossible; treat it as dont-care.  */
786         switch (mask & 7) {
787         case 2: /* zero -> op1 == op2 */
788             cond = TCG_COND_EQ;
789             break;
790         case 4 | 1: /* !zero -> op1 != op2 */
791             cond = TCG_COND_NE;
792             break;
793         case 4: /* borrow (!carry) -> op1 < op2 */
794             cond = TCG_COND_LTU;
795             break;
796         case 2 | 1: /* !borrow (carry) -> op1 >= op2 */
797             cond = TCG_COND_GEU;
798             break;
799         default:
800             goto do_dynamic;
801         }
802         account_inline_branch(s, old_cc_op);
803         break;
804
805     default:
806     do_dynamic:
807         /* Calculate cc value.  */
808         gen_op_calc_cc(s);
809         /* FALLTHRU */
810
811     case CC_OP_STATIC:
812         /* Jump based on CC.  We'll load up the real cond below;
813            the assignment here merely avoids a compiler warning.  */
814         account_noninline_branch(s, old_cc_op);
815         old_cc_op = CC_OP_STATIC;
816         cond = TCG_COND_NEVER;
817         break;
818     }
819
820     /* Load up the arguments of the comparison.  */
821     c->is_64 = true;
822     c->g1 = c->g2 = false;
823     switch (old_cc_op) {
824     case CC_OP_LTGT0_32:
825         c->is_64 = false;
826         c->u.s32.a = tcg_temp_new_i32();
827         tcg_gen_extrl_i64_i32(c->u.s32.a, cc_dst);
828         c->u.s32.b = tcg_const_i32(0);
829         break;
830     case CC_OP_LTGT_32:
831     case CC_OP_LTUGTU_32:
832     case CC_OP_SUBU_32:
833         c->is_64 = false;
834         c->u.s32.a = tcg_temp_new_i32();
835         tcg_gen_extrl_i64_i32(c->u.s32.a, cc_src);
836         c->u.s32.b = tcg_temp_new_i32();
837         tcg_gen_extrl_i64_i32(c->u.s32.b, cc_dst);
838         break;
839
840     case CC_OP_LTGT0_64:
841     case CC_OP_NZ:
842     case CC_OP_FLOGR:
843         c->u.s64.a = cc_dst;
844         c->u.s64.b = tcg_const_i64(0);
845         c->g1 = true;
846         break;
847     case CC_OP_LTGT_64:
848     case CC_OP_LTUGTU_64:
849     case CC_OP_SUBU_64:
850         c->u.s64.a = cc_src;
851         c->u.s64.b = cc_dst;
852         c->g1 = c->g2 = true;
853         break;
854
855     case CC_OP_TM_32:
856     case CC_OP_TM_64:
857     case CC_OP_ICM:
858         c->u.s64.a = tcg_temp_new_i64();
859         c->u.s64.b = tcg_const_i64(0);
860         tcg_gen_and_i64(c->u.s64.a, cc_src, cc_dst);
861         break;
862
863     case CC_OP_ADDU_32:
864         c->is_64 = false;
865         c->u.s32.a = tcg_temp_new_i32();
866         c->u.s32.b = tcg_temp_new_i32();
867         tcg_gen_extrl_i64_i32(c->u.s32.a, cc_vr);
868         if (cond == TCG_COND_EQ || cond == TCG_COND_NE) {
869             tcg_gen_movi_i32(c->u.s32.b, 0);
870         } else {
871             tcg_gen_extrl_i64_i32(c->u.s32.b, cc_src);
872         }
873         break;
874
875     case CC_OP_ADDU_64:
876         c->u.s64.a = cc_vr;
877         c->g1 = true;
878         if (cond == TCG_COND_EQ || cond == TCG_COND_NE) {
879             c->u.s64.b = tcg_const_i64(0);
880         } else {
881             c->u.s64.b = cc_src;
882             c->g2 = true;
883         }
884         break;
885
886     case CC_OP_STATIC:
887         c->is_64 = false;
888         c->u.s32.a = cc_op;
889         c->g1 = true;
890         switch (mask) {
891         case 0x8 | 0x4 | 0x2: /* cc != 3 */
892             cond = TCG_COND_NE;
893             c->u.s32.b = tcg_const_i32(3);
894             break;
895         case 0x8 | 0x4 | 0x1: /* cc != 2 */
896             cond = TCG_COND_NE;
897             c->u.s32.b = tcg_const_i32(2);
898             break;
899         case 0x8 | 0x2 | 0x1: /* cc != 1 */
900             cond = TCG_COND_NE;
901             c->u.s32.b = tcg_const_i32(1);
902             break;
903         case 0x8 | 0x2: /* cc == 0 || cc == 2 => (cc & 1) == 0 */
904             cond = TCG_COND_EQ;
905             c->g1 = false;
906             c->u.s32.a = tcg_temp_new_i32();
907             c->u.s32.b = tcg_const_i32(0);
908             tcg_gen_andi_i32(c->u.s32.a, cc_op, 1);
909             break;
910         case 0x8 | 0x4: /* cc < 2 */
911             cond = TCG_COND_LTU;
912             c->u.s32.b = tcg_const_i32(2);
913             break;
914         case 0x8: /* cc == 0 */
915             cond = TCG_COND_EQ;
916             c->u.s32.b = tcg_const_i32(0);
917             break;
918         case 0x4 | 0x2 | 0x1: /* cc != 0 */
919             cond = TCG_COND_NE;
920             c->u.s32.b = tcg_const_i32(0);
921             break;
922         case 0x4 | 0x1: /* cc == 1 || cc == 3 => (cc & 1) != 0 */
923             cond = TCG_COND_NE;
924             c->g1 = false;
925             c->u.s32.a = tcg_temp_new_i32();
926             c->u.s32.b = tcg_const_i32(0);
927             tcg_gen_andi_i32(c->u.s32.a, cc_op, 1);
928             break;
929         case 0x4: /* cc == 1 */
930             cond = TCG_COND_EQ;
931             c->u.s32.b = tcg_const_i32(1);
932             break;
933         case 0x2 | 0x1: /* cc > 1 */
934             cond = TCG_COND_GTU;
935             c->u.s32.b = tcg_const_i32(1);
936             break;
937         case 0x2: /* cc == 2 */
938             cond = TCG_COND_EQ;
939             c->u.s32.b = tcg_const_i32(2);
940             break;
941         case 0x1: /* cc == 3 */
942             cond = TCG_COND_EQ;
943             c->u.s32.b = tcg_const_i32(3);
944             break;
945         default:
946             /* CC is masked by something else: (8 >> cc) & mask.  */
947             cond = TCG_COND_NE;
948             c->g1 = false;
949             c->u.s32.a = tcg_const_i32(8);
950             c->u.s32.b = tcg_const_i32(0);
951             tcg_gen_shr_i32(c->u.s32.a, c->u.s32.a, cc_op);
952             tcg_gen_andi_i32(c->u.s32.a, c->u.s32.a, mask);
953             break;
954         }
955         break;
956
957     default:
958         abort();
959     }
960     c->cond = cond;
961 }
962
963 static void free_compare(DisasCompare *c)
964 {
965     if (!c->g1) {
966         if (c->is_64) {
967             tcg_temp_free_i64(c->u.s64.a);
968         } else {
969             tcg_temp_free_i32(c->u.s32.a);
970         }
971     }
972     if (!c->g2) {
973         if (c->is_64) {
974             tcg_temp_free_i64(c->u.s64.b);
975         } else {
976             tcg_temp_free_i32(c->u.s32.b);
977         }
978     }
979 }
980
981 /* ====================================================================== */
982 /* Define the insn format enumeration.  */
983 #define F0(N)                         FMT_##N,
984 #define F1(N, X1)                     F0(N)
985 #define F2(N, X1, X2)                 F0(N)
986 #define F3(N, X1, X2, X3)             F0(N)
987 #define F4(N, X1, X2, X3, X4)         F0(N)
988 #define F5(N, X1, X2, X3, X4, X5)     F0(N)
989
990 typedef enum {
991 #include "insn-format.def"
992 } DisasFormat;
993
994 #undef F0
995 #undef F1
996 #undef F2
997 #undef F3
998 #undef F4
999 #undef F5
1000
1001 /* Define a structure to hold the decoded fields.  We'll store each inside
1002    an array indexed by an enum.  In order to conserve memory, we'll arrange
1003    for fields that do not exist at the same time to overlap, thus the "C"
1004    for compact.  For checking purposes there is an "O" for original index
1005    as well that will be applied to availability bitmaps.  */
1006
1007 enum DisasFieldIndexO {
1008     FLD_O_r1,
1009     FLD_O_r2,
1010     FLD_O_r3,
1011     FLD_O_m1,
1012     FLD_O_m3,
1013     FLD_O_m4,
1014     FLD_O_b1,
1015     FLD_O_b2,
1016     FLD_O_b4,
1017     FLD_O_d1,
1018     FLD_O_d2,
1019     FLD_O_d4,
1020     FLD_O_x2,
1021     FLD_O_l1,
1022     FLD_O_l2,
1023     FLD_O_i1,
1024     FLD_O_i2,
1025     FLD_O_i3,
1026     FLD_O_i4,
1027     FLD_O_i5
1028 };
1029
1030 enum DisasFieldIndexC {
1031     FLD_C_r1 = 0,
1032     FLD_C_m1 = 0,
1033     FLD_C_b1 = 0,
1034     FLD_C_i1 = 0,
1035
1036     FLD_C_r2 = 1,
1037     FLD_C_b2 = 1,
1038     FLD_C_i2 = 1,
1039
1040     FLD_C_r3 = 2,
1041     FLD_C_m3 = 2,
1042     FLD_C_i3 = 2,
1043
1044     FLD_C_m4 = 3,
1045     FLD_C_b4 = 3,
1046     FLD_C_i4 = 3,
1047     FLD_C_l1 = 3,
1048
1049     FLD_C_i5 = 4,
1050     FLD_C_d1 = 4,
1051
1052     FLD_C_d2 = 5,
1053
1054     FLD_C_d4 = 6,
1055     FLD_C_x2 = 6,
1056     FLD_C_l2 = 6,
1057
1058     NUM_C_FIELD = 7
1059 };
1060
1061 struct DisasFields {
1062     uint64_t raw_insn;
1063     unsigned op:8;
1064     unsigned op2:8;
1065     unsigned presentC:16;
1066     unsigned int presentO;
1067     int c[NUM_C_FIELD];
1068 };
1069
1070 /* This is the way fields are to be accessed out of DisasFields.  */
1071 #define have_field(S, F)  have_field1((S), FLD_O_##F)
1072 #define get_field(S, F)   get_field1((S), FLD_O_##F, FLD_C_##F)
1073
1074 static bool have_field1(const DisasFields *f, enum DisasFieldIndexO c)
1075 {
1076     return (f->presentO >> c) & 1;
1077 }
1078
1079 static int get_field1(const DisasFields *f, enum DisasFieldIndexO o,
1080                       enum DisasFieldIndexC c)
1081 {
1082     assert(have_field1(f, o));
1083     return f->c[c];
1084 }
1085
1086 /* Describe the layout of each field in each format.  */
1087 typedef struct DisasField {
1088     unsigned int beg:8;
1089     unsigned int size:8;
1090     unsigned int type:2;
1091     unsigned int indexC:6;
1092     enum DisasFieldIndexO indexO:8;
1093 } DisasField;
1094
1095 typedef struct DisasFormatInfo {
1096     DisasField op[NUM_C_FIELD];
1097 } DisasFormatInfo;
1098
1099 #define R(N, B)       {  B,  4, 0, FLD_C_r##N, FLD_O_r##N }
1100 #define M(N, B)       {  B,  4, 0, FLD_C_m##N, FLD_O_m##N }
1101 #define BD(N, BB, BD) { BB,  4, 0, FLD_C_b##N, FLD_O_b##N }, \
1102                       { BD, 12, 0, FLD_C_d##N, FLD_O_d##N }
1103 #define BXD(N)        { 16,  4, 0, FLD_C_b##N, FLD_O_b##N }, \
1104                       { 12,  4, 0, FLD_C_x##N, FLD_O_x##N }, \
1105                       { 20, 12, 0, FLD_C_d##N, FLD_O_d##N }
1106 #define BDL(N)        { 16,  4, 0, FLD_C_b##N, FLD_O_b##N }, \
1107                       { 20, 20, 2, FLD_C_d##N, FLD_O_d##N }
1108 #define BXDL(N)       { 16,  4, 0, FLD_C_b##N, FLD_O_b##N }, \
1109                       { 12,  4, 0, FLD_C_x##N, FLD_O_x##N }, \
1110                       { 20, 20, 2, FLD_C_d##N, FLD_O_d##N }
1111 #define I(N, B, S)    {  B,  S, 1, FLD_C_i##N, FLD_O_i##N }
1112 #define L(N, B, S)    {  B,  S, 0, FLD_C_l##N, FLD_O_l##N }
1113
1114 #define F0(N)                     { { } },
1115 #define F1(N, X1)                 { { X1 } },
1116 #define F2(N, X1, X2)             { { X1, X2 } },
1117 #define F3(N, X1, X2, X3)         { { X1, X2, X3 } },
1118 #define F4(N, X1, X2, X3, X4)     { { X1, X2, X3, X4 } },
1119 #define F5(N, X1, X2, X3, X4, X5) { { X1, X2, X3, X4, X5 } },
1120
1121 static const DisasFormatInfo format_info[] = {
1122 #include "insn-format.def"
1123 };
1124
1125 #undef F0
1126 #undef F1
1127 #undef F2
1128 #undef F3
1129 #undef F4
1130 #undef F5
1131 #undef R
1132 #undef M
1133 #undef BD
1134 #undef BXD
1135 #undef BDL
1136 #undef BXDL
1137 #undef I
1138 #undef L
1139
1140 /* Generally, we'll extract operands into this structures, operate upon
1141    them, and store them back.  See the "in1", "in2", "prep", "wout" sets
1142    of routines below for more details.  */
1143 typedef struct {
1144     bool g_out, g_out2, g_in1, g_in2;
1145     TCGv_i64 out, out2, in1, in2;
1146     TCGv_i64 addr1;
1147 } DisasOps;
1148
1149 /* Instructions can place constraints on their operands, raising specification
1150    exceptions if they are violated.  To make this easy to automate, each "in1",
1151    "in2", "prep", "wout" helper will have a SPEC_<name> define that equals one
1152    of the following, or 0.  To make this easy to document, we'll put the
1153    SPEC_<name> defines next to <name>.  */
1154
1155 #define SPEC_r1_even    1
1156 #define SPEC_r2_even    2
1157 #define SPEC_r3_even    4
1158 #define SPEC_r1_f128    8
1159 #define SPEC_r2_f128    16
1160
1161 /* Return values from translate_one, indicating the state of the TB.  */
1162 typedef enum {
1163     /* Continue the TB.  */
1164     NO_EXIT,
1165     /* We have emitted one or more goto_tb.  No fixup required.  */
1166     EXIT_GOTO_TB,
1167     /* We are not using a goto_tb (for whatever reason), but have updated
1168        the PC (for whatever reason), so there's no need to do it again on
1169        exiting the TB.  */
1170     EXIT_PC_UPDATED,
1171     /* We have updated the PC and CC values.  */
1172     EXIT_PC_CC_UPDATED,
1173     /* We are exiting the TB, but have neither emitted a goto_tb, nor
1174        updated the PC for the next instruction to be executed.  */
1175     EXIT_PC_STALE,
1176     /* We are exiting the TB to the main loop.  */
1177     EXIT_PC_STALE_NOCHAIN,
1178     /* We are ending the TB with a noreturn function call, e.g. longjmp.
1179        No following code will be executed.  */
1180     EXIT_NORETURN,
1181 } ExitStatus;
1182
1183 struct DisasInsn {
1184     unsigned opc:16;
1185     DisasFormat fmt:8;
1186     unsigned fac:8;
1187     unsigned spec:8;
1188
1189     const char *name;
1190
1191     void (*help_in1)(DisasContext *, DisasFields *, DisasOps *);
1192     void (*help_in2)(DisasContext *, DisasFields *, DisasOps *);
1193     void (*help_prep)(DisasContext *, DisasFields *, DisasOps *);
1194     void (*help_wout)(DisasContext *, DisasFields *, DisasOps *);
1195     void (*help_cout)(DisasContext *, DisasOps *);
1196     ExitStatus (*help_op)(DisasContext *, DisasOps *);
1197
1198     uint64_t data;
1199 };
1200
1201 /* ====================================================================== */
1202 /* Miscellaneous helpers, used by several operations.  */
1203
1204 static void help_l2_shift(DisasContext *s, DisasFields *f,
1205                           DisasOps *o, int mask)
1206 {
1207     int b2 = get_field(f, b2);
1208     int d2 = get_field(f, d2);
1209
1210     if (b2 == 0) {
1211         o->in2 = tcg_const_i64(d2 & mask);
1212     } else {
1213         o->in2 = get_address(s, 0, b2, d2);
1214         tcg_gen_andi_i64(o->in2, o->in2, mask);
1215     }
1216 }
1217
1218 static ExitStatus help_goto_direct(DisasContext *s, uint64_t dest)
1219 {
1220     if (dest == s->next_pc) {
1221         per_branch(s, true);
1222         return NO_EXIT;
1223     }
1224     if (use_goto_tb(s, dest)) {
1225         update_cc_op(s);
1226         per_breaking_event(s);
1227         tcg_gen_goto_tb(0);
1228         tcg_gen_movi_i64(psw_addr, dest);
1229         tcg_gen_exit_tb((uintptr_t)s->tb);
1230         return EXIT_GOTO_TB;
1231     } else {
1232         tcg_gen_movi_i64(psw_addr, dest);
1233         per_branch(s, false);
1234         return EXIT_PC_UPDATED;
1235     }
1236 }
1237
1238 static ExitStatus help_branch(DisasContext *s, DisasCompare *c,
1239                               bool is_imm, int imm, TCGv_i64 cdest)
1240 {
1241     ExitStatus ret;
1242     uint64_t dest = s->pc + 2 * imm;
1243     TCGLabel *lab;
1244
1245     /* Take care of the special cases first.  */
1246     if (c->cond == TCG_COND_NEVER) {
1247         ret = NO_EXIT;
1248         goto egress;
1249     }
1250     if (is_imm) {
1251         if (dest == s->next_pc) {
1252             /* Branch to next.  */
1253             per_branch(s, true);
1254             ret = NO_EXIT;
1255             goto egress;
1256         }
1257         if (c->cond == TCG_COND_ALWAYS) {
1258             ret = help_goto_direct(s, dest);
1259             goto egress;
1260         }
1261     } else {
1262         if (TCGV_IS_UNUSED_I64(cdest)) {
1263             /* E.g. bcr %r0 -> no branch.  */
1264             ret = NO_EXIT;
1265             goto egress;
1266         }
1267         if (c->cond == TCG_COND_ALWAYS) {
1268             tcg_gen_mov_i64(psw_addr, cdest);
1269             per_branch(s, false);
1270             ret = EXIT_PC_UPDATED;
1271             goto egress;
1272         }
1273     }
1274
1275     if (use_goto_tb(s, s->next_pc)) {
1276         if (is_imm && use_goto_tb(s, dest)) {
1277             /* Both exits can use goto_tb.  */
1278             update_cc_op(s);
1279
1280             lab = gen_new_label();
1281             if (c->is_64) {
1282                 tcg_gen_brcond_i64(c->cond, c->u.s64.a, c->u.s64.b, lab);
1283             } else {
1284                 tcg_gen_brcond_i32(c->cond, c->u.s32.a, c->u.s32.b, lab);
1285             }
1286
1287             /* Branch not taken.  */
1288             tcg_gen_goto_tb(0);
1289             tcg_gen_movi_i64(psw_addr, s->next_pc);
1290             tcg_gen_exit_tb((uintptr_t)s->tb + 0);
1291
1292             /* Branch taken.  */
1293             gen_set_label(lab);
1294             per_breaking_event(s);
1295             tcg_gen_goto_tb(1);
1296             tcg_gen_movi_i64(psw_addr, dest);
1297             tcg_gen_exit_tb((uintptr_t)s->tb + 1);
1298
1299             ret = EXIT_GOTO_TB;
1300         } else {
1301             /* Fallthru can use goto_tb, but taken branch cannot.  */
1302             /* Store taken branch destination before the brcond.  This
1303                avoids having to allocate a new local temp to hold it.
1304                We'll overwrite this in the not taken case anyway.  */
1305             if (!is_imm) {
1306                 tcg_gen_mov_i64(psw_addr, cdest);
1307             }
1308
1309             lab = gen_new_label();
1310             if (c->is_64) {
1311                 tcg_gen_brcond_i64(c->cond, c->u.s64.a, c->u.s64.b, lab);
1312             } else {
1313                 tcg_gen_brcond_i32(c->cond, c->u.s32.a, c->u.s32.b, lab);
1314             }
1315
1316             /* Branch not taken.  */
1317             update_cc_op(s);
1318             tcg_gen_goto_tb(0);
1319             tcg_gen_movi_i64(psw_addr, s->next_pc);
1320             tcg_gen_exit_tb((uintptr_t)s->tb + 0);
1321
1322             gen_set_label(lab);
1323             if (is_imm) {
1324                 tcg_gen_movi_i64(psw_addr, dest);
1325             }
1326             per_breaking_event(s);
1327             ret = EXIT_PC_UPDATED;
1328         }
1329     } else {
1330         /* Fallthru cannot use goto_tb.  This by itself is vanishingly rare.
1331            Most commonly we're single-stepping or some other condition that
1332            disables all use of goto_tb.  Just update the PC and exit.  */
1333
1334         TCGv_i64 next = tcg_const_i64(s->next_pc);
1335         if (is_imm) {
1336             cdest = tcg_const_i64(dest);
1337         }
1338
1339         if (c->is_64) {
1340             tcg_gen_movcond_i64(c->cond, psw_addr, c->u.s64.a, c->u.s64.b,
1341                                 cdest, next);
1342             per_branch_cond(s, c->cond, c->u.s64.a, c->u.s64.b);
1343         } else {
1344             TCGv_i32 t0 = tcg_temp_new_i32();
1345             TCGv_i64 t1 = tcg_temp_new_i64();
1346             TCGv_i64 z = tcg_const_i64(0);
1347             tcg_gen_setcond_i32(c->cond, t0, c->u.s32.a, c->u.s32.b);
1348             tcg_gen_extu_i32_i64(t1, t0);
1349             tcg_temp_free_i32(t0);
1350             tcg_gen_movcond_i64(TCG_COND_NE, psw_addr, t1, z, cdest, next);
1351             per_branch_cond(s, TCG_COND_NE, t1, z);
1352             tcg_temp_free_i64(t1);
1353             tcg_temp_free_i64(z);
1354         }
1355
1356         if (is_imm) {
1357             tcg_temp_free_i64(cdest);
1358         }
1359         tcg_temp_free_i64(next);
1360
1361         ret = EXIT_PC_UPDATED;
1362     }
1363
1364  egress:
1365     free_compare(c);
1366     return ret;
1367 }
1368
1369 /* ====================================================================== */
1370 /* The operations.  These perform the bulk of the work for any insn,
1371    usually after the operands have been loaded and output initialized.  */
1372
1373 static ExitStatus op_abs(DisasContext *s, DisasOps *o)
1374 {
1375     TCGv_i64 z, n;
1376     z = tcg_const_i64(0);
1377     n = tcg_temp_new_i64();
1378     tcg_gen_neg_i64(n, o->in2);
1379     tcg_gen_movcond_i64(TCG_COND_LT, o->out, o->in2, z, n, o->in2);
1380     tcg_temp_free_i64(n);
1381     tcg_temp_free_i64(z);
1382     return NO_EXIT;
1383 }
1384
1385 static ExitStatus op_absf32(DisasContext *s, DisasOps *o)
1386 {
1387     tcg_gen_andi_i64(o->out, o->in2, 0x7fffffffull);
1388     return NO_EXIT;
1389 }
1390
1391 static ExitStatus op_absf64(DisasContext *s, DisasOps *o)
1392 {
1393     tcg_gen_andi_i64(o->out, o->in2, 0x7fffffffffffffffull);
1394     return NO_EXIT;
1395 }
1396
1397 static ExitStatus op_absf128(DisasContext *s, DisasOps *o)
1398 {
1399     tcg_gen_andi_i64(o->out, o->in1, 0x7fffffffffffffffull);
1400     tcg_gen_mov_i64(o->out2, o->in2);
1401     return NO_EXIT;
1402 }
1403
1404 static ExitStatus op_add(DisasContext *s, DisasOps *o)
1405 {
1406     tcg_gen_add_i64(o->out, o->in1, o->in2);
1407     return NO_EXIT;
1408 }
1409
1410 static ExitStatus op_addc(DisasContext *s, DisasOps *o)
1411 {
1412     DisasCompare cmp;
1413     TCGv_i64 carry;
1414
1415     tcg_gen_add_i64(o->out, o->in1, o->in2);
1416
1417     /* The carry flag is the msb of CC, therefore the branch mask that would
1418        create that comparison is 3.  Feeding the generated comparison to
1419        setcond produces the carry flag that we desire.  */
1420     disas_jcc(s, &cmp, 3);
1421     carry = tcg_temp_new_i64();
1422     if (cmp.is_64) {
1423         tcg_gen_setcond_i64(cmp.cond, carry, cmp.u.s64.a, cmp.u.s64.b);
1424     } else {
1425         TCGv_i32 t = tcg_temp_new_i32();
1426         tcg_gen_setcond_i32(cmp.cond, t, cmp.u.s32.a, cmp.u.s32.b);
1427         tcg_gen_extu_i32_i64(carry, t);
1428         tcg_temp_free_i32(t);
1429     }
1430     free_compare(&cmp);
1431
1432     tcg_gen_add_i64(o->out, o->out, carry);
1433     tcg_temp_free_i64(carry);
1434     return NO_EXIT;
1435 }
1436
1437 static ExitStatus op_aeb(DisasContext *s, DisasOps *o)
1438 {
1439     gen_helper_aeb(o->out, cpu_env, o->in1, o->in2);
1440     return NO_EXIT;
1441 }
1442
1443 static ExitStatus op_adb(DisasContext *s, DisasOps *o)
1444 {
1445     gen_helper_adb(o->out, cpu_env, o->in1, o->in2);
1446     return NO_EXIT;
1447 }
1448
1449 static ExitStatus op_axb(DisasContext *s, DisasOps *o)
1450 {
1451     gen_helper_axb(o->out, cpu_env, o->out, o->out2, o->in1, o->in2);
1452     return_low128(o->out2);
1453     return NO_EXIT;
1454 }
1455
1456 static ExitStatus op_and(DisasContext *s, DisasOps *o)
1457 {
1458     tcg_gen_and_i64(o->out, o->in1, o->in2);
1459     return NO_EXIT;
1460 }
1461
1462 static ExitStatus op_andi(DisasContext *s, DisasOps *o)
1463 {
1464     int shift = s->insn->data & 0xff;
1465     int size = s->insn->data >> 8;
1466     uint64_t mask = ((1ull << size) - 1) << shift;
1467
1468     assert(!o->g_in2);
1469     tcg_gen_shli_i64(o->in2, o->in2, shift);
1470     tcg_gen_ori_i64(o->in2, o->in2, ~mask);
1471     tcg_gen_and_i64(o->out, o->in1, o->in2);
1472
1473     /* Produce the CC from only the bits manipulated.  */
1474     tcg_gen_andi_i64(cc_dst, o->out, mask);
1475     set_cc_nz_u64(s, cc_dst);
1476     return NO_EXIT;
1477 }
1478
1479 static ExitStatus op_bas(DisasContext *s, DisasOps *o)
1480 {
1481     tcg_gen_movi_i64(o->out, pc_to_link_info(s, s->next_pc));
1482     if (!TCGV_IS_UNUSED_I64(o->in2)) {
1483         tcg_gen_mov_i64(psw_addr, o->in2);
1484         per_branch(s, false);
1485         return EXIT_PC_UPDATED;
1486     } else {
1487         return NO_EXIT;
1488     }
1489 }
1490
1491 static ExitStatus op_basi(DisasContext *s, DisasOps *o)
1492 {
1493     tcg_gen_movi_i64(o->out, pc_to_link_info(s, s->next_pc));
1494     return help_goto_direct(s, s->pc + 2 * get_field(s->fields, i2));
1495 }
1496
1497 static ExitStatus op_bc(DisasContext *s, DisasOps *o)
1498 {
1499     int m1 = get_field(s->fields, m1);
1500     bool is_imm = have_field(s->fields, i2);
1501     int imm = is_imm ? get_field(s->fields, i2) : 0;
1502     DisasCompare c;
1503
1504     /* BCR with R2 = 0 causes no branching */
1505     if (have_field(s->fields, r2) && get_field(s->fields, r2) == 0) {
1506         if (m1 == 14) {
1507             /* Perform serialization */
1508             /* FIXME: check for fast-BCR-serialization facility */
1509             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1510         }
1511         if (m1 == 15) {
1512             /* Perform serialization */
1513             /* FIXME: perform checkpoint-synchronisation */
1514             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1515         }
1516         return NO_EXIT;
1517     }
1518
1519     disas_jcc(s, &c, m1);
1520     return help_branch(s, &c, is_imm, imm, o->in2);
1521 }
1522
1523 static ExitStatus op_bct32(DisasContext *s, DisasOps *o)
1524 {
1525     int r1 = get_field(s->fields, r1);
1526     bool is_imm = have_field(s->fields, i2);
1527     int imm = is_imm ? get_field(s->fields, i2) : 0;
1528     DisasCompare c;
1529     TCGv_i64 t;
1530
1531     c.cond = TCG_COND_NE;
1532     c.is_64 = false;
1533     c.g1 = false;
1534     c.g2 = false;
1535
1536     t = tcg_temp_new_i64();
1537     tcg_gen_subi_i64(t, regs[r1], 1);
1538     store_reg32_i64(r1, t);
1539     c.u.s32.a = tcg_temp_new_i32();
1540     c.u.s32.b = tcg_const_i32(0);
1541     tcg_gen_extrl_i64_i32(c.u.s32.a, t);
1542     tcg_temp_free_i64(t);
1543
1544     return help_branch(s, &c, is_imm, imm, o->in2);
1545 }
1546
1547 static ExitStatus op_bcth(DisasContext *s, DisasOps *o)
1548 {
1549     int r1 = get_field(s->fields, r1);
1550     int imm = get_field(s->fields, i2);
1551     DisasCompare c;
1552     TCGv_i64 t;
1553
1554     c.cond = TCG_COND_NE;
1555     c.is_64 = false;
1556     c.g1 = false;
1557     c.g2 = false;
1558
1559     t = tcg_temp_new_i64();
1560     tcg_gen_shri_i64(t, regs[r1], 32);
1561     tcg_gen_subi_i64(t, t, 1);
1562     store_reg32h_i64(r1, t);
1563     c.u.s32.a = tcg_temp_new_i32();
1564     c.u.s32.b = tcg_const_i32(0);
1565     tcg_gen_extrl_i64_i32(c.u.s32.a, t);
1566     tcg_temp_free_i64(t);
1567
1568     return help_branch(s, &c, 1, imm, o->in2);
1569 }
1570
1571 static ExitStatus op_bct64(DisasContext *s, DisasOps *o)
1572 {
1573     int r1 = get_field(s->fields, r1);
1574     bool is_imm = have_field(s->fields, i2);
1575     int imm = is_imm ? get_field(s->fields, i2) : 0;
1576     DisasCompare c;
1577
1578     c.cond = TCG_COND_NE;
1579     c.is_64 = true;
1580     c.g1 = true;
1581     c.g2 = false;
1582
1583     tcg_gen_subi_i64(regs[r1], regs[r1], 1);
1584     c.u.s64.a = regs[r1];
1585     c.u.s64.b = tcg_const_i64(0);
1586
1587     return help_branch(s, &c, is_imm, imm, o->in2);
1588 }
1589
1590 static ExitStatus op_bx32(DisasContext *s, DisasOps *o)
1591 {
1592     int r1 = get_field(s->fields, r1);
1593     int r3 = get_field(s->fields, r3);
1594     bool is_imm = have_field(s->fields, i2);
1595     int imm = is_imm ? get_field(s->fields, i2) : 0;
1596     DisasCompare c;
1597     TCGv_i64 t;
1598
1599     c.cond = (s->insn->data ? TCG_COND_LE : TCG_COND_GT);
1600     c.is_64 = false;
1601     c.g1 = false;
1602     c.g2 = false;
1603
1604     t = tcg_temp_new_i64();
1605     tcg_gen_add_i64(t, regs[r1], regs[r3]);
1606     c.u.s32.a = tcg_temp_new_i32();
1607     c.u.s32.b = tcg_temp_new_i32();
1608     tcg_gen_extrl_i64_i32(c.u.s32.a, t);
1609     tcg_gen_extrl_i64_i32(c.u.s32.b, regs[r3 | 1]);
1610     store_reg32_i64(r1, t);
1611     tcg_temp_free_i64(t);
1612
1613     return help_branch(s, &c, is_imm, imm, o->in2);
1614 }
1615
1616 static ExitStatus op_bx64(DisasContext *s, DisasOps *o)
1617 {
1618     int r1 = get_field(s->fields, r1);
1619     int r3 = get_field(s->fields, r3);
1620     bool is_imm = have_field(s->fields, i2);
1621     int imm = is_imm ? get_field(s->fields, i2) : 0;
1622     DisasCompare c;
1623
1624     c.cond = (s->insn->data ? TCG_COND_LE : TCG_COND_GT);
1625     c.is_64 = true;
1626
1627     if (r1 == (r3 | 1)) {
1628         c.u.s64.b = load_reg(r3 | 1);
1629         c.g2 = false;
1630     } else {
1631         c.u.s64.b = regs[r3 | 1];
1632         c.g2 = true;
1633     }
1634
1635     tcg_gen_add_i64(regs[r1], regs[r1], regs[r3]);
1636     c.u.s64.a = regs[r1];
1637     c.g1 = true;
1638
1639     return help_branch(s, &c, is_imm, imm, o->in2);
1640 }
1641
1642 static ExitStatus op_cj(DisasContext *s, DisasOps *o)
1643 {
1644     int imm, m3 = get_field(s->fields, m3);
1645     bool is_imm;
1646     DisasCompare c;
1647
1648     c.cond = ltgt_cond[m3];
1649     if (s->insn->data) {
1650         c.cond = tcg_unsigned_cond(c.cond);
1651     }
1652     c.is_64 = c.g1 = c.g2 = true;
1653     c.u.s64.a = o->in1;
1654     c.u.s64.b = o->in2;
1655
1656     is_imm = have_field(s->fields, i4);
1657     if (is_imm) {
1658         imm = get_field(s->fields, i4);
1659     } else {
1660         imm = 0;
1661         o->out = get_address(s, 0, get_field(s->fields, b4),
1662                              get_field(s->fields, d4));
1663     }
1664
1665     return help_branch(s, &c, is_imm, imm, o->out);
1666 }
1667
1668 static ExitStatus op_ceb(DisasContext *s, DisasOps *o)
1669 {
1670     gen_helper_ceb(cc_op, cpu_env, o->in1, o->in2);
1671     set_cc_static(s);
1672     return NO_EXIT;
1673 }
1674
1675 static ExitStatus op_cdb(DisasContext *s, DisasOps *o)
1676 {
1677     gen_helper_cdb(cc_op, cpu_env, o->in1, o->in2);
1678     set_cc_static(s);
1679     return NO_EXIT;
1680 }
1681
1682 static ExitStatus op_cxb(DisasContext *s, DisasOps *o)
1683 {
1684     gen_helper_cxb(cc_op, cpu_env, o->out, o->out2, o->in1, o->in2);
1685     set_cc_static(s);
1686     return NO_EXIT;
1687 }
1688
1689 static ExitStatus op_cfeb(DisasContext *s, DisasOps *o)
1690 {
1691     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1692     gen_helper_cfeb(o->out, cpu_env, o->in2, m3);
1693     tcg_temp_free_i32(m3);
1694     gen_set_cc_nz_f32(s, o->in2);
1695     return NO_EXIT;
1696 }
1697
1698 static ExitStatus op_cfdb(DisasContext *s, DisasOps *o)
1699 {
1700     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1701     gen_helper_cfdb(o->out, cpu_env, o->in2, m3);
1702     tcg_temp_free_i32(m3);
1703     gen_set_cc_nz_f64(s, o->in2);
1704     return NO_EXIT;
1705 }
1706
1707 static ExitStatus op_cfxb(DisasContext *s, DisasOps *o)
1708 {
1709     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1710     gen_helper_cfxb(o->out, cpu_env, o->in1, o->in2, m3);
1711     tcg_temp_free_i32(m3);
1712     gen_set_cc_nz_f128(s, o->in1, o->in2);
1713     return NO_EXIT;
1714 }
1715
1716 static ExitStatus op_cgeb(DisasContext *s, DisasOps *o)
1717 {
1718     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1719     gen_helper_cgeb(o->out, cpu_env, o->in2, m3);
1720     tcg_temp_free_i32(m3);
1721     gen_set_cc_nz_f32(s, o->in2);
1722     return NO_EXIT;
1723 }
1724
1725 static ExitStatus op_cgdb(DisasContext *s, DisasOps *o)
1726 {
1727     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1728     gen_helper_cgdb(o->out, cpu_env, o->in2, m3);
1729     tcg_temp_free_i32(m3);
1730     gen_set_cc_nz_f64(s, o->in2);
1731     return NO_EXIT;
1732 }
1733
1734 static ExitStatus op_cgxb(DisasContext *s, DisasOps *o)
1735 {
1736     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1737     gen_helper_cgxb(o->out, cpu_env, o->in1, o->in2, m3);
1738     tcg_temp_free_i32(m3);
1739     gen_set_cc_nz_f128(s, o->in1, o->in2);
1740     return NO_EXIT;
1741 }
1742
1743 static ExitStatus op_clfeb(DisasContext *s, DisasOps *o)
1744 {
1745     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1746     gen_helper_clfeb(o->out, cpu_env, o->in2, m3);
1747     tcg_temp_free_i32(m3);
1748     gen_set_cc_nz_f32(s, o->in2);
1749     return NO_EXIT;
1750 }
1751
1752 static ExitStatus op_clfdb(DisasContext *s, DisasOps *o)
1753 {
1754     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1755     gen_helper_clfdb(o->out, cpu_env, o->in2, m3);
1756     tcg_temp_free_i32(m3);
1757     gen_set_cc_nz_f64(s, o->in2);
1758     return NO_EXIT;
1759 }
1760
1761 static ExitStatus op_clfxb(DisasContext *s, DisasOps *o)
1762 {
1763     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1764     gen_helper_clfxb(o->out, cpu_env, o->in1, o->in2, m3);
1765     tcg_temp_free_i32(m3);
1766     gen_set_cc_nz_f128(s, o->in1, o->in2);
1767     return NO_EXIT;
1768 }
1769
1770 static ExitStatus op_clgeb(DisasContext *s, DisasOps *o)
1771 {
1772     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1773     gen_helper_clgeb(o->out, cpu_env, o->in2, m3);
1774     tcg_temp_free_i32(m3);
1775     gen_set_cc_nz_f32(s, o->in2);
1776     return NO_EXIT;
1777 }
1778
1779 static ExitStatus op_clgdb(DisasContext *s, DisasOps *o)
1780 {
1781     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1782     gen_helper_clgdb(o->out, cpu_env, o->in2, m3);
1783     tcg_temp_free_i32(m3);
1784     gen_set_cc_nz_f64(s, o->in2);
1785     return NO_EXIT;
1786 }
1787
1788 static ExitStatus op_clgxb(DisasContext *s, DisasOps *o)
1789 {
1790     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1791     gen_helper_clgxb(o->out, cpu_env, o->in1, o->in2, m3);
1792     tcg_temp_free_i32(m3);
1793     gen_set_cc_nz_f128(s, o->in1, o->in2);
1794     return NO_EXIT;
1795 }
1796
1797 static ExitStatus op_cegb(DisasContext *s, DisasOps *o)
1798 {
1799     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1800     gen_helper_cegb(o->out, cpu_env, o->in2, m3);
1801     tcg_temp_free_i32(m3);
1802     return NO_EXIT;
1803 }
1804
1805 static ExitStatus op_cdgb(DisasContext *s, DisasOps *o)
1806 {
1807     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1808     gen_helper_cdgb(o->out, cpu_env, o->in2, m3);
1809     tcg_temp_free_i32(m3);
1810     return NO_EXIT;
1811 }
1812
1813 static ExitStatus op_cxgb(DisasContext *s, DisasOps *o)
1814 {
1815     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1816     gen_helper_cxgb(o->out, cpu_env, o->in2, m3);
1817     tcg_temp_free_i32(m3);
1818     return_low128(o->out2);
1819     return NO_EXIT;
1820 }
1821
1822 static ExitStatus op_celgb(DisasContext *s, DisasOps *o)
1823 {
1824     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1825     gen_helper_celgb(o->out, cpu_env, o->in2, m3);
1826     tcg_temp_free_i32(m3);
1827     return NO_EXIT;
1828 }
1829
1830 static ExitStatus op_cdlgb(DisasContext *s, DisasOps *o)
1831 {
1832     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1833     gen_helper_cdlgb(o->out, cpu_env, o->in2, m3);
1834     tcg_temp_free_i32(m3);
1835     return NO_EXIT;
1836 }
1837
1838 static ExitStatus op_cxlgb(DisasContext *s, DisasOps *o)
1839 {
1840     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1841     gen_helper_cxlgb(o->out, cpu_env, o->in2, m3);
1842     tcg_temp_free_i32(m3);
1843     return_low128(o->out2);
1844     return NO_EXIT;
1845 }
1846
1847 static ExitStatus op_cksm(DisasContext *s, DisasOps *o)
1848 {
1849     int r2 = get_field(s->fields, r2);
1850     TCGv_i64 len = tcg_temp_new_i64();
1851
1852     gen_helper_cksm(len, cpu_env, o->in1, o->in2, regs[r2 + 1]);
1853     set_cc_static(s);
1854     return_low128(o->out);
1855
1856     tcg_gen_add_i64(regs[r2], regs[r2], len);
1857     tcg_gen_sub_i64(regs[r2 + 1], regs[r2 + 1], len);
1858     tcg_temp_free_i64(len);
1859
1860     return NO_EXIT;
1861 }
1862
1863 static ExitStatus op_clc(DisasContext *s, DisasOps *o)
1864 {
1865     int l = get_field(s->fields, l1);
1866     TCGv_i32 vl;
1867
1868     switch (l + 1) {
1869     case 1:
1870         tcg_gen_qemu_ld8u(cc_src, o->addr1, get_mem_index(s));
1871         tcg_gen_qemu_ld8u(cc_dst, o->in2, get_mem_index(s));
1872         break;
1873     case 2:
1874         tcg_gen_qemu_ld16u(cc_src, o->addr1, get_mem_index(s));
1875         tcg_gen_qemu_ld16u(cc_dst, o->in2, get_mem_index(s));
1876         break;
1877     case 4:
1878         tcg_gen_qemu_ld32u(cc_src, o->addr1, get_mem_index(s));
1879         tcg_gen_qemu_ld32u(cc_dst, o->in2, get_mem_index(s));
1880         break;
1881     case 8:
1882         tcg_gen_qemu_ld64(cc_src, o->addr1, get_mem_index(s));
1883         tcg_gen_qemu_ld64(cc_dst, o->in2, get_mem_index(s));
1884         break;
1885     default:
1886         vl = tcg_const_i32(l);
1887         gen_helper_clc(cc_op, cpu_env, vl, o->addr1, o->in2);
1888         tcg_temp_free_i32(vl);
1889         set_cc_static(s);
1890         return NO_EXIT;
1891     }
1892     gen_op_update2_cc_i64(s, CC_OP_LTUGTU_64, cc_src, cc_dst);
1893     return NO_EXIT;
1894 }
1895
1896 static ExitStatus op_clcl(DisasContext *s, DisasOps *o)
1897 {
1898     int r1 = get_field(s->fields, r1);
1899     int r2 = get_field(s->fields, r2);
1900     TCGv_i32 t1, t2;
1901
1902     /* r1 and r2 must be even.  */
1903     if (r1 & 1 || r2 & 1) {
1904         gen_program_exception(s, PGM_SPECIFICATION);
1905         return EXIT_NORETURN;
1906     }
1907
1908     t1 = tcg_const_i32(r1);
1909     t2 = tcg_const_i32(r2);
1910     gen_helper_clcl(cc_op, cpu_env, t1, t2);
1911     tcg_temp_free_i32(t1);
1912     tcg_temp_free_i32(t2);
1913     set_cc_static(s);
1914     return NO_EXIT;
1915 }
1916
1917 static ExitStatus op_clcle(DisasContext *s, DisasOps *o)
1918 {
1919     int r1 = get_field(s->fields, r1);
1920     int r3 = get_field(s->fields, r3);
1921     TCGv_i32 t1, t3;
1922
1923     /* r1 and r3 must be even.  */
1924     if (r1 & 1 || r3 & 1) {
1925         gen_program_exception(s, PGM_SPECIFICATION);
1926         return EXIT_NORETURN;
1927     }
1928
1929     t1 = tcg_const_i32(r1);
1930     t3 = tcg_const_i32(r3);
1931     gen_helper_clcle(cc_op, cpu_env, t1, o->in2, t3);
1932     tcg_temp_free_i32(t1);
1933     tcg_temp_free_i32(t3);
1934     set_cc_static(s);
1935     return NO_EXIT;
1936 }
1937
1938 static ExitStatus op_clclu(DisasContext *s, DisasOps *o)
1939 {
1940     int r1 = get_field(s->fields, r1);
1941     int r3 = get_field(s->fields, r3);
1942     TCGv_i32 t1, t3;
1943
1944     /* r1 and r3 must be even.  */
1945     if (r1 & 1 || r3 & 1) {
1946         gen_program_exception(s, PGM_SPECIFICATION);
1947         return EXIT_NORETURN;
1948     }
1949
1950     t1 = tcg_const_i32(r1);
1951     t3 = tcg_const_i32(r3);
1952     gen_helper_clclu(cc_op, cpu_env, t1, o->in2, t3);
1953     tcg_temp_free_i32(t1);
1954     tcg_temp_free_i32(t3);
1955     set_cc_static(s);
1956     return NO_EXIT;
1957 }
1958
1959 static ExitStatus op_clm(DisasContext *s, DisasOps *o)
1960 {
1961     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
1962     TCGv_i32 t1 = tcg_temp_new_i32();
1963     tcg_gen_extrl_i64_i32(t1, o->in1);
1964     gen_helper_clm(cc_op, cpu_env, t1, m3, o->in2);
1965     set_cc_static(s);
1966     tcg_temp_free_i32(t1);
1967     tcg_temp_free_i32(m3);
1968     return NO_EXIT;
1969 }
1970
1971 static ExitStatus op_clst(DisasContext *s, DisasOps *o)
1972 {
1973     gen_helper_clst(o->in1, cpu_env, regs[0], o->in1, o->in2);
1974     set_cc_static(s);
1975     return_low128(o->in2);
1976     return NO_EXIT;
1977 }
1978
1979 static ExitStatus op_cps(DisasContext *s, DisasOps *o)
1980 {
1981     TCGv_i64 t = tcg_temp_new_i64();
1982     tcg_gen_andi_i64(t, o->in1, 0x8000000000000000ull);
1983     tcg_gen_andi_i64(o->out, o->in2, 0x7fffffffffffffffull);
1984     tcg_gen_or_i64(o->out, o->out, t);
1985     tcg_temp_free_i64(t);
1986     return NO_EXIT;
1987 }
1988
1989 static ExitStatus op_cs(DisasContext *s, DisasOps *o)
1990 {
1991     int d2 = get_field(s->fields, d2);
1992     int b2 = get_field(s->fields, b2);
1993     TCGv_i64 addr, cc;
1994
1995     /* Note that in1 = R3 (new value) and
1996        in2 = (zero-extended) R1 (expected value).  */
1997
1998     addr = get_address(s, 0, b2, d2);
1999     tcg_gen_atomic_cmpxchg_i64(o->out, addr, o->in2, o->in1,
2000                                get_mem_index(s), s->insn->data | MO_ALIGN);
2001     tcg_temp_free_i64(addr);
2002
2003     /* Are the memory and expected values (un)equal?  Note that this setcond
2004        produces the output CC value, thus the NE sense of the test.  */
2005     cc = tcg_temp_new_i64();
2006     tcg_gen_setcond_i64(TCG_COND_NE, cc, o->in2, o->out);
2007     tcg_gen_extrl_i64_i32(cc_op, cc);
2008     tcg_temp_free_i64(cc);
2009     set_cc_static(s);
2010
2011     return NO_EXIT;
2012 }
2013
2014 static ExitStatus op_cdsg(DisasContext *s, DisasOps *o)
2015 {
2016     int r1 = get_field(s->fields, r1);
2017     int r3 = get_field(s->fields, r3);
2018     int d2 = get_field(s->fields, d2);
2019     int b2 = get_field(s->fields, b2);
2020     TCGv_i64 addr;
2021     TCGv_i32 t_r1, t_r3;
2022
2023     /* Note that R1:R1+1 = expected value and R3:R3+1 = new value.  */
2024     addr = get_address(s, 0, b2, d2);
2025     t_r1 = tcg_const_i32(r1);
2026     t_r3 = tcg_const_i32(r3);
2027     gen_helper_cdsg(cpu_env, addr, t_r1, t_r3);
2028     tcg_temp_free_i64(addr);
2029     tcg_temp_free_i32(t_r1);
2030     tcg_temp_free_i32(t_r3);
2031
2032     set_cc_static(s);
2033     return NO_EXIT;
2034 }
2035
2036 static ExitStatus op_csst(DisasContext *s, DisasOps *o)
2037 {
2038     int r3 = get_field(s->fields, r3);
2039     TCGv_i32 t_r3 = tcg_const_i32(r3);
2040
2041     gen_helper_csst(cc_op, cpu_env, t_r3, o->in1, o->in2);
2042     tcg_temp_free_i32(t_r3);
2043
2044     set_cc_static(s);
2045     return NO_EXIT;
2046 }
2047
2048 #ifndef CONFIG_USER_ONLY
2049 static ExitStatus op_csp(DisasContext *s, DisasOps *o)
2050 {
2051     TCGMemOp mop = s->insn->data;
2052     TCGv_i64 addr, old, cc;
2053     TCGLabel *lab = gen_new_label();
2054
2055     /* Note that in1 = R1 (zero-extended expected value),
2056        out = R1 (original reg), out2 = R1+1 (new value).  */
2057
2058     check_privileged(s);
2059     addr = tcg_temp_new_i64();
2060     old = tcg_temp_new_i64();
2061     tcg_gen_andi_i64(addr, o->in2, -1ULL << (mop & MO_SIZE));
2062     tcg_gen_atomic_cmpxchg_i64(old, addr, o->in1, o->out2,
2063                                get_mem_index(s), mop | MO_ALIGN);
2064     tcg_temp_free_i64(addr);
2065
2066     /* Are the memory and expected values (un)equal?  */
2067     cc = tcg_temp_new_i64();
2068     tcg_gen_setcond_i64(TCG_COND_NE, cc, o->in1, old);
2069     tcg_gen_extrl_i64_i32(cc_op, cc);
2070
2071     /* Write back the output now, so that it happens before the
2072        following branch, so that we don't need local temps.  */
2073     if ((mop & MO_SIZE) == MO_32) {
2074         tcg_gen_deposit_i64(o->out, o->out, old, 0, 32);
2075     } else {
2076         tcg_gen_mov_i64(o->out, old);
2077     }
2078     tcg_temp_free_i64(old);
2079
2080     /* If the comparison was equal, and the LSB of R2 was set,
2081        then we need to flush the TLB (for all cpus).  */
2082     tcg_gen_xori_i64(cc, cc, 1);
2083     tcg_gen_and_i64(cc, cc, o->in2);
2084     tcg_gen_brcondi_i64(TCG_COND_EQ, cc, 0, lab);
2085     tcg_temp_free_i64(cc);
2086
2087     gen_helper_purge(cpu_env);
2088     gen_set_label(lab);
2089
2090     return NO_EXIT;
2091 }
2092 #endif
2093
2094 static ExitStatus op_cvd(DisasContext *s, DisasOps *o)
2095 {
2096     TCGv_i64 t1 = tcg_temp_new_i64();
2097     TCGv_i32 t2 = tcg_temp_new_i32();
2098     tcg_gen_extrl_i64_i32(t2, o->in1);
2099     gen_helper_cvd(t1, t2);
2100     tcg_temp_free_i32(t2);
2101     tcg_gen_qemu_st64(t1, o->in2, get_mem_index(s));
2102     tcg_temp_free_i64(t1);
2103     return NO_EXIT;
2104 }
2105
2106 static ExitStatus op_ct(DisasContext *s, DisasOps *o)
2107 {
2108     int m3 = get_field(s->fields, m3);
2109     TCGLabel *lab = gen_new_label();
2110     TCGCond c;
2111
2112     c = tcg_invert_cond(ltgt_cond[m3]);
2113     if (s->insn->data) {
2114         c = tcg_unsigned_cond(c);
2115     }
2116     tcg_gen_brcond_i64(c, o->in1, o->in2, lab);
2117
2118     /* Trap.  */
2119     gen_trap(s);
2120
2121     gen_set_label(lab);
2122     return NO_EXIT;
2123 }
2124
2125 static ExitStatus op_cuXX(DisasContext *s, DisasOps *o)
2126 {
2127     int m3 = get_field(s->fields, m3);
2128     int r1 = get_field(s->fields, r1);
2129     int r2 = get_field(s->fields, r2);
2130     TCGv_i32 tr1, tr2, chk;
2131
2132     /* R1 and R2 must both be even.  */
2133     if ((r1 | r2) & 1) {
2134         gen_program_exception(s, PGM_SPECIFICATION);
2135         return EXIT_NORETURN;
2136     }
2137     if (!s390_has_feat(S390_FEAT_ETF3_ENH)) {
2138         m3 = 0;
2139     }
2140
2141     tr1 = tcg_const_i32(r1);
2142     tr2 = tcg_const_i32(r2);
2143     chk = tcg_const_i32(m3);
2144
2145     switch (s->insn->data) {
2146     case 12:
2147         gen_helper_cu12(cc_op, cpu_env, tr1, tr2, chk);
2148         break;
2149     case 14:
2150         gen_helper_cu14(cc_op, cpu_env, tr1, tr2, chk);
2151         break;
2152     case 21:
2153         gen_helper_cu21(cc_op, cpu_env, tr1, tr2, chk);
2154         break;
2155     case 24:
2156         gen_helper_cu24(cc_op, cpu_env, tr1, tr2, chk);
2157         break;
2158     case 41:
2159         gen_helper_cu41(cc_op, cpu_env, tr1, tr2, chk);
2160         break;
2161     case 42:
2162         gen_helper_cu42(cc_op, cpu_env, tr1, tr2, chk);
2163         break;
2164     default:
2165         g_assert_not_reached();
2166     }
2167
2168     tcg_temp_free_i32(tr1);
2169     tcg_temp_free_i32(tr2);
2170     tcg_temp_free_i32(chk);
2171     set_cc_static(s);
2172     return NO_EXIT;
2173 }
2174
2175 #ifndef CONFIG_USER_ONLY
2176 static ExitStatus op_diag(DisasContext *s, DisasOps *o)
2177 {
2178     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
2179     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
2180     TCGv_i32 func_code = tcg_const_i32(get_field(s->fields, i2));
2181
2182     check_privileged(s);
2183     update_psw_addr(s);
2184     gen_op_calc_cc(s);
2185
2186     gen_helper_diag(cpu_env, r1, r3, func_code);
2187
2188     tcg_temp_free_i32(func_code);
2189     tcg_temp_free_i32(r3);
2190     tcg_temp_free_i32(r1);
2191     return NO_EXIT;
2192 }
2193 #endif
2194
2195 static ExitStatus op_divs32(DisasContext *s, DisasOps *o)
2196 {
2197     gen_helper_divs32(o->out2, cpu_env, o->in1, o->in2);
2198     return_low128(o->out);
2199     return NO_EXIT;
2200 }
2201
2202 static ExitStatus op_divu32(DisasContext *s, DisasOps *o)
2203 {
2204     gen_helper_divu32(o->out2, cpu_env, o->in1, o->in2);
2205     return_low128(o->out);
2206     return NO_EXIT;
2207 }
2208
2209 static ExitStatus op_divs64(DisasContext *s, DisasOps *o)
2210 {
2211     gen_helper_divs64(o->out2, cpu_env, o->in1, o->in2);
2212     return_low128(o->out);
2213     return NO_EXIT;
2214 }
2215
2216 static ExitStatus op_divu64(DisasContext *s, DisasOps *o)
2217 {
2218     gen_helper_divu64(o->out2, cpu_env, o->out, o->out2, o->in2);
2219     return_low128(o->out);
2220     return NO_EXIT;
2221 }
2222
2223 static ExitStatus op_deb(DisasContext *s, DisasOps *o)
2224 {
2225     gen_helper_deb(o->out, cpu_env, o->in1, o->in2);
2226     return NO_EXIT;
2227 }
2228
2229 static ExitStatus op_ddb(DisasContext *s, DisasOps *o)
2230 {
2231     gen_helper_ddb(o->out, cpu_env, o->in1, o->in2);
2232     return NO_EXIT;
2233 }
2234
2235 static ExitStatus op_dxb(DisasContext *s, DisasOps *o)
2236 {
2237     gen_helper_dxb(o->out, cpu_env, o->out, o->out2, o->in1, o->in2);
2238     return_low128(o->out2);
2239     return NO_EXIT;
2240 }
2241
2242 static ExitStatus op_ear(DisasContext *s, DisasOps *o)
2243 {
2244     int r2 = get_field(s->fields, r2);
2245     tcg_gen_ld32u_i64(o->out, cpu_env, offsetof(CPUS390XState, aregs[r2]));
2246     return NO_EXIT;
2247 }
2248
2249 static ExitStatus op_ecag(DisasContext *s, DisasOps *o)
2250 {
2251     /* No cache information provided.  */
2252     tcg_gen_movi_i64(o->out, -1);
2253     return NO_EXIT;
2254 }
2255
2256 static ExitStatus op_efpc(DisasContext *s, DisasOps *o)
2257 {
2258     tcg_gen_ld32u_i64(o->out, cpu_env, offsetof(CPUS390XState, fpc));
2259     return NO_EXIT;
2260 }
2261
2262 static ExitStatus op_epsw(DisasContext *s, DisasOps *o)
2263 {
2264     int r1 = get_field(s->fields, r1);
2265     int r2 = get_field(s->fields, r2);
2266     TCGv_i64 t = tcg_temp_new_i64();
2267
2268     /* Note the "subsequently" in the PoO, which implies a defined result
2269        if r1 == r2.  Thus we cannot defer these writes to an output hook.  */
2270     tcg_gen_shri_i64(t, psw_mask, 32);
2271     store_reg32_i64(r1, t);
2272     if (r2 != 0) {
2273         store_reg32_i64(r2, psw_mask);
2274     }
2275
2276     tcg_temp_free_i64(t);
2277     return NO_EXIT;
2278 }
2279
2280 static ExitStatus op_ex(DisasContext *s, DisasOps *o)
2281 {
2282     int r1 = get_field(s->fields, r1);
2283     TCGv_i32 ilen;
2284     TCGv_i64 v1;
2285
2286     /* Nested EXECUTE is not allowed.  */
2287     if (unlikely(s->ex_value)) {
2288         gen_program_exception(s, PGM_EXECUTE);
2289         return EXIT_NORETURN;
2290     }
2291
2292     update_psw_addr(s);
2293     update_cc_op(s);
2294
2295     if (r1 == 0) {
2296         v1 = tcg_const_i64(0);
2297     } else {
2298         v1 = regs[r1];
2299     }
2300
2301     ilen = tcg_const_i32(s->ilen);
2302     gen_helper_ex(cpu_env, ilen, v1, o->in2);
2303     tcg_temp_free_i32(ilen);
2304
2305     if (r1 == 0) {
2306         tcg_temp_free_i64(v1);
2307     }
2308
2309     return EXIT_PC_CC_UPDATED;
2310 }
2311
2312 static ExitStatus op_fieb(DisasContext *s, DisasOps *o)
2313 {
2314     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
2315     gen_helper_fieb(o->out, cpu_env, o->in2, m3);
2316     tcg_temp_free_i32(m3);
2317     return NO_EXIT;
2318 }
2319
2320 static ExitStatus op_fidb(DisasContext *s, DisasOps *o)
2321 {
2322     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
2323     gen_helper_fidb(o->out, cpu_env, o->in2, m3);
2324     tcg_temp_free_i32(m3);
2325     return NO_EXIT;
2326 }
2327
2328 static ExitStatus op_fixb(DisasContext *s, DisasOps *o)
2329 {
2330     TCGv_i32 m3 = tcg_const_i32(get_field(s->fields, m3));
2331     gen_helper_fixb(o->out, cpu_env, o->in1, o->in2, m3);
2332     return_low128(o->out2);
2333     tcg_temp_free_i32(m3);
2334     return NO_EXIT;
2335 }
2336
2337 static ExitStatus op_flogr(DisasContext *s, DisasOps *o)
2338 {
2339     /* We'll use the original input for cc computation, since we get to
2340        compare that against 0, which ought to be better than comparing
2341        the real output against 64.  It also lets cc_dst be a convenient
2342        temporary during our computation.  */
2343     gen_op_update1_cc_i64(s, CC_OP_FLOGR, o->in2);
2344
2345     /* R1 = IN ? CLZ(IN) : 64.  */
2346     tcg_gen_clzi_i64(o->out, o->in2, 64);
2347
2348     /* R1+1 = IN & ~(found bit).  Note that we may attempt to shift this
2349        value by 64, which is undefined.  But since the shift is 64 iff the
2350        input is zero, we still get the correct result after and'ing.  */
2351     tcg_gen_movi_i64(o->out2, 0x8000000000000000ull);
2352     tcg_gen_shr_i64(o->out2, o->out2, o->out);
2353     tcg_gen_andc_i64(o->out2, cc_dst, o->out2);
2354     return NO_EXIT;
2355 }
2356
2357 static ExitStatus op_icm(DisasContext *s, DisasOps *o)
2358 {
2359     int m3 = get_field(s->fields, m3);
2360     int pos, len, base = s->insn->data;
2361     TCGv_i64 tmp = tcg_temp_new_i64();
2362     uint64_t ccm;
2363
2364     switch (m3) {
2365     case 0xf:
2366         /* Effectively a 32-bit load.  */
2367         tcg_gen_qemu_ld32u(tmp, o->in2, get_mem_index(s));
2368         len = 32;
2369         goto one_insert;
2370
2371     case 0xc:
2372     case 0x6:
2373     case 0x3:
2374         /* Effectively a 16-bit load.  */
2375         tcg_gen_qemu_ld16u(tmp, o->in2, get_mem_index(s));
2376         len = 16;
2377         goto one_insert;
2378
2379     case 0x8:
2380     case 0x4:
2381     case 0x2:
2382     case 0x1:
2383         /* Effectively an 8-bit load.  */
2384         tcg_gen_qemu_ld8u(tmp, o->in2, get_mem_index(s));
2385         len = 8;
2386         goto one_insert;
2387
2388     one_insert:
2389         pos = base + ctz32(m3) * 8;
2390         tcg_gen_deposit_i64(o->out, o->out, tmp, pos, len);
2391         ccm = ((1ull << len) - 1) << pos;
2392         break;
2393
2394     default:
2395         /* This is going to be a sequence of loads and inserts.  */
2396         pos = base + 32 - 8;
2397         ccm = 0;
2398         while (m3) {
2399             if (m3 & 0x8) {
2400                 tcg_gen_qemu_ld8u(tmp, o->in2, get_mem_index(s));
2401                 tcg_gen_addi_i64(o->in2, o->in2, 1);
2402                 tcg_gen_deposit_i64(o->out, o->out, tmp, pos, 8);
2403                 ccm |= 0xff << pos;
2404             }
2405             m3 = (m3 << 1) & 0xf;
2406             pos -= 8;
2407         }
2408         break;
2409     }
2410
2411     tcg_gen_movi_i64(tmp, ccm);
2412     gen_op_update2_cc_i64(s, CC_OP_ICM, tmp, o->out);
2413     tcg_temp_free_i64(tmp);
2414     return NO_EXIT;
2415 }
2416
2417 static ExitStatus op_insi(DisasContext *s, DisasOps *o)
2418 {
2419     int shift = s->insn->data & 0xff;
2420     int size = s->insn->data >> 8;
2421     tcg_gen_deposit_i64(o->out, o->in1, o->in2, shift, size);
2422     return NO_EXIT;
2423 }
2424
2425 static ExitStatus op_ipm(DisasContext *s, DisasOps *o)
2426 {
2427     TCGv_i64 t1;
2428
2429     gen_op_calc_cc(s);
2430     tcg_gen_andi_i64(o->out, o->out, ~0xff000000ull);
2431
2432     t1 = tcg_temp_new_i64();
2433     tcg_gen_shli_i64(t1, psw_mask, 20);
2434     tcg_gen_shri_i64(t1, t1, 36);
2435     tcg_gen_or_i64(o->out, o->out, t1);
2436
2437     tcg_gen_extu_i32_i64(t1, cc_op);
2438     tcg_gen_shli_i64(t1, t1, 28);
2439     tcg_gen_or_i64(o->out, o->out, t1);
2440     tcg_temp_free_i64(t1);
2441     return NO_EXIT;
2442 }
2443
2444 #ifndef CONFIG_USER_ONLY
2445 static ExitStatus op_idte(DisasContext *s, DisasOps *o)
2446 {
2447     TCGv_i32 m4;
2448
2449     check_privileged(s);
2450     if (s390_has_feat(S390_FEAT_LOCAL_TLB_CLEARING)) {
2451         m4 = tcg_const_i32(get_field(s->fields, m4));
2452     } else {
2453         m4 = tcg_const_i32(0);
2454     }
2455     gen_helper_idte(cpu_env, o->in1, o->in2, m4);
2456     tcg_temp_free_i32(m4);
2457     return NO_EXIT;
2458 }
2459
2460 static ExitStatus op_ipte(DisasContext *s, DisasOps *o)
2461 {
2462     TCGv_i32 m4;
2463
2464     check_privileged(s);
2465     if (s390_has_feat(S390_FEAT_LOCAL_TLB_CLEARING)) {
2466         m4 = tcg_const_i32(get_field(s->fields, m4));
2467     } else {
2468         m4 = tcg_const_i32(0);
2469     }
2470     gen_helper_ipte(cpu_env, o->in1, o->in2, m4);
2471     tcg_temp_free_i32(m4);
2472     return NO_EXIT;
2473 }
2474
2475 static ExitStatus op_iske(DisasContext *s, DisasOps *o)
2476 {
2477     check_privileged(s);
2478     gen_helper_iske(o->out, cpu_env, o->in2);
2479     return NO_EXIT;
2480 }
2481 #endif
2482
2483 static ExitStatus op_keb(DisasContext *s, DisasOps *o)
2484 {
2485     gen_helper_keb(cc_op, cpu_env, o->in1, o->in2);
2486     set_cc_static(s);
2487     return NO_EXIT;
2488 }
2489
2490 static ExitStatus op_kdb(DisasContext *s, DisasOps *o)
2491 {
2492     gen_helper_kdb(cc_op, cpu_env, o->in1, o->in2);
2493     set_cc_static(s);
2494     return NO_EXIT;
2495 }
2496
2497 static ExitStatus op_kxb(DisasContext *s, DisasOps *o)
2498 {
2499     gen_helper_kxb(cc_op, cpu_env, o->out, o->out2, o->in1, o->in2);
2500     set_cc_static(s);
2501     return NO_EXIT;
2502 }
2503
2504 static ExitStatus op_laa(DisasContext *s, DisasOps *o)
2505 {
2506     /* The real output is indeed the original value in memory;
2507        recompute the addition for the computation of CC.  */
2508     tcg_gen_atomic_fetch_add_i64(o->in2, o->in2, o->in1, get_mem_index(s),
2509                                  s->insn->data | MO_ALIGN);
2510     /* However, we need to recompute the addition for setting CC.  */
2511     tcg_gen_add_i64(o->out, o->in1, o->in2);
2512     return NO_EXIT;
2513 }
2514
2515 static ExitStatus op_lan(DisasContext *s, DisasOps *o)
2516 {
2517     /* The real output is indeed the original value in memory;
2518        recompute the addition for the computation of CC.  */
2519     tcg_gen_atomic_fetch_and_i64(o->in2, o->in2, o->in1, get_mem_index(s),
2520                                  s->insn->data | MO_ALIGN);
2521     /* However, we need to recompute the operation for setting CC.  */
2522     tcg_gen_and_i64(o->out, o->in1, o->in2);
2523     return NO_EXIT;
2524 }
2525
2526 static ExitStatus op_lao(DisasContext *s, DisasOps *o)
2527 {
2528     /* The real output is indeed the original value in memory;
2529        recompute the addition for the computation of CC.  */
2530     tcg_gen_atomic_fetch_or_i64(o->in2, o->in2, o->in1, get_mem_index(s),
2531                                 s->insn->data | MO_ALIGN);
2532     /* However, we need to recompute the operation for setting CC.  */
2533     tcg_gen_or_i64(o->out, o->in1, o->in2);
2534     return NO_EXIT;
2535 }
2536
2537 static ExitStatus op_lax(DisasContext *s, DisasOps *o)
2538 {
2539     /* The real output is indeed the original value in memory;
2540        recompute the addition for the computation of CC.  */
2541     tcg_gen_atomic_fetch_xor_i64(o->in2, o->in2, o->in1, get_mem_index(s),
2542                                  s->insn->data | MO_ALIGN);
2543     /* However, we need to recompute the operation for setting CC.  */
2544     tcg_gen_xor_i64(o->out, o->in1, o->in2);
2545     return NO_EXIT;
2546 }
2547
2548 static ExitStatus op_ldeb(DisasContext *s, DisasOps *o)
2549 {
2550     gen_helper_ldeb(o->out, cpu_env, o->in2);
2551     return NO_EXIT;
2552 }
2553
2554 static ExitStatus op_ledb(DisasContext *s, DisasOps *o)
2555 {
2556     gen_helper_ledb(o->out, cpu_env, o->in2);
2557     return NO_EXIT;
2558 }
2559
2560 static ExitStatus op_ldxb(DisasContext *s, DisasOps *o)
2561 {
2562     gen_helper_ldxb(o->out, cpu_env, o->in1, o->in2);
2563     return NO_EXIT;
2564 }
2565
2566 static ExitStatus op_lexb(DisasContext *s, DisasOps *o)
2567 {
2568     gen_helper_lexb(o->out, cpu_env, o->in1, o->in2);
2569     return NO_EXIT;
2570 }
2571
2572 static ExitStatus op_lxdb(DisasContext *s, DisasOps *o)
2573 {
2574     gen_helper_lxdb(o->out, cpu_env, o->in2);
2575     return_low128(o->out2);
2576     return NO_EXIT;
2577 }
2578
2579 static ExitStatus op_lxeb(DisasContext *s, DisasOps *o)
2580 {
2581     gen_helper_lxeb(o->out, cpu_env, o->in2);
2582     return_low128(o->out2);
2583     return NO_EXIT;
2584 }
2585
2586 static ExitStatus op_llgt(DisasContext *s, DisasOps *o)
2587 {
2588     tcg_gen_andi_i64(o->out, o->in2, 0x7fffffff);
2589     return NO_EXIT;
2590 }
2591
2592 static ExitStatus op_ld8s(DisasContext *s, DisasOps *o)
2593 {
2594     tcg_gen_qemu_ld8s(o->out, o->in2, get_mem_index(s));
2595     return NO_EXIT;
2596 }
2597
2598 static ExitStatus op_ld8u(DisasContext *s, DisasOps *o)
2599 {
2600     tcg_gen_qemu_ld8u(o->out, o->in2, get_mem_index(s));
2601     return NO_EXIT;
2602 }
2603
2604 static ExitStatus op_ld16s(DisasContext *s, DisasOps *o)
2605 {
2606     tcg_gen_qemu_ld16s(o->out, o->in2, get_mem_index(s));
2607     return NO_EXIT;
2608 }
2609
2610 static ExitStatus op_ld16u(DisasContext *s, DisasOps *o)
2611 {
2612     tcg_gen_qemu_ld16u(o->out, o->in2, get_mem_index(s));
2613     return NO_EXIT;
2614 }
2615
2616 static ExitStatus op_ld32s(DisasContext *s, DisasOps *o)
2617 {
2618     tcg_gen_qemu_ld32s(o->out, o->in2, get_mem_index(s));
2619     return NO_EXIT;
2620 }
2621
2622 static ExitStatus op_ld32u(DisasContext *s, DisasOps *o)
2623 {
2624     tcg_gen_qemu_ld32u(o->out, o->in2, get_mem_index(s));
2625     return NO_EXIT;
2626 }
2627
2628 static ExitStatus op_ld64(DisasContext *s, DisasOps *o)
2629 {
2630     tcg_gen_qemu_ld64(o->out, o->in2, get_mem_index(s));
2631     return NO_EXIT;
2632 }
2633
2634 static ExitStatus op_lat(DisasContext *s, DisasOps *o)
2635 {
2636     TCGLabel *lab = gen_new_label();
2637     store_reg32_i64(get_field(s->fields, r1), o->in2);
2638     /* The value is stored even in case of trap. */
2639     tcg_gen_brcondi_i64(TCG_COND_NE, o->in2, 0, lab);
2640     gen_trap(s);
2641     gen_set_label(lab);
2642     return NO_EXIT;
2643 }
2644
2645 static ExitStatus op_lgat(DisasContext *s, DisasOps *o)
2646 {
2647     TCGLabel *lab = gen_new_label();
2648     tcg_gen_qemu_ld64(o->out, o->in2, get_mem_index(s));
2649     /* The value is stored even in case of trap. */
2650     tcg_gen_brcondi_i64(TCG_COND_NE, o->out, 0, lab);
2651     gen_trap(s);
2652     gen_set_label(lab);
2653     return NO_EXIT;
2654 }
2655
2656 static ExitStatus op_lfhat(DisasContext *s, DisasOps *o)
2657 {
2658     TCGLabel *lab = gen_new_label();
2659     store_reg32h_i64(get_field(s->fields, r1), o->in2);
2660     /* The value is stored even in case of trap. */
2661     tcg_gen_brcondi_i64(TCG_COND_NE, o->in2, 0, lab);
2662     gen_trap(s);
2663     gen_set_label(lab);
2664     return NO_EXIT;
2665 }
2666
2667 static ExitStatus op_llgfat(DisasContext *s, DisasOps *o)
2668 {
2669     TCGLabel *lab = gen_new_label();
2670     tcg_gen_qemu_ld32u(o->out, o->in2, get_mem_index(s));
2671     /* The value is stored even in case of trap. */
2672     tcg_gen_brcondi_i64(TCG_COND_NE, o->out, 0, lab);
2673     gen_trap(s);
2674     gen_set_label(lab);
2675     return NO_EXIT;
2676 }
2677
2678 static ExitStatus op_llgtat(DisasContext *s, DisasOps *o)
2679 {
2680     TCGLabel *lab = gen_new_label();
2681     tcg_gen_andi_i64(o->out, o->in2, 0x7fffffff);
2682     /* The value is stored even in case of trap. */
2683     tcg_gen_brcondi_i64(TCG_COND_NE, o->out, 0, lab);
2684     gen_trap(s);
2685     gen_set_label(lab);
2686     return NO_EXIT;
2687 }
2688
2689 static ExitStatus op_loc(DisasContext *s, DisasOps *o)
2690 {
2691     DisasCompare c;
2692
2693     disas_jcc(s, &c, get_field(s->fields, m3));
2694
2695     if (c.is_64) {
2696         tcg_gen_movcond_i64(c.cond, o->out, c.u.s64.a, c.u.s64.b,
2697                             o->in2, o->in1);
2698         free_compare(&c);
2699     } else {
2700         TCGv_i32 t32 = tcg_temp_new_i32();
2701         TCGv_i64 t, z;
2702
2703         tcg_gen_setcond_i32(c.cond, t32, c.u.s32.a, c.u.s32.b);
2704         free_compare(&c);
2705
2706         t = tcg_temp_new_i64();
2707         tcg_gen_extu_i32_i64(t, t32);
2708         tcg_temp_free_i32(t32);
2709
2710         z = tcg_const_i64(0);
2711         tcg_gen_movcond_i64(TCG_COND_NE, o->out, t, z, o->in2, o->in1);
2712         tcg_temp_free_i64(t);
2713         tcg_temp_free_i64(z);
2714     }
2715
2716     return NO_EXIT;
2717 }
2718
2719 #ifndef CONFIG_USER_ONLY
2720 static ExitStatus op_lctl(DisasContext *s, DisasOps *o)
2721 {
2722     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
2723     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
2724     check_privileged(s);
2725     gen_helper_lctl(cpu_env, r1, o->in2, r3);
2726     tcg_temp_free_i32(r1);
2727     tcg_temp_free_i32(r3);
2728     return NO_EXIT;
2729 }
2730
2731 static ExitStatus op_lctlg(DisasContext *s, DisasOps *o)
2732 {
2733     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
2734     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
2735     check_privileged(s);
2736     gen_helper_lctlg(cpu_env, r1, o->in2, r3);
2737     tcg_temp_free_i32(r1);
2738     tcg_temp_free_i32(r3);
2739     return NO_EXIT;
2740 }
2741
2742 static ExitStatus op_lra(DisasContext *s, DisasOps *o)
2743 {
2744     check_privileged(s);
2745     gen_helper_lra(o->out, cpu_env, o->in2);
2746     set_cc_static(s);
2747     return NO_EXIT;
2748 }
2749
2750 static ExitStatus op_lpp(DisasContext *s, DisasOps *o)
2751 {
2752     check_privileged(s);
2753
2754     tcg_gen_st_i64(o->in2, cpu_env, offsetof(CPUS390XState, pp));
2755     return NO_EXIT;
2756 }
2757
2758 static ExitStatus op_lpsw(DisasContext *s, DisasOps *o)
2759 {
2760     TCGv_i64 t1, t2;
2761
2762     check_privileged(s);
2763     per_breaking_event(s);
2764
2765     t1 = tcg_temp_new_i64();
2766     t2 = tcg_temp_new_i64();
2767     tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2768     tcg_gen_addi_i64(o->in2, o->in2, 4);
2769     tcg_gen_qemu_ld32u(t2, o->in2, get_mem_index(s));
2770     /* Convert the 32-bit PSW_MASK into the 64-bit PSW_MASK.  */
2771     tcg_gen_shli_i64(t1, t1, 32);
2772     gen_helper_load_psw(cpu_env, t1, t2);
2773     tcg_temp_free_i64(t1);
2774     tcg_temp_free_i64(t2);
2775     return EXIT_NORETURN;
2776 }
2777
2778 static ExitStatus op_lpswe(DisasContext *s, DisasOps *o)
2779 {
2780     TCGv_i64 t1, t2;
2781
2782     check_privileged(s);
2783     per_breaking_event(s);
2784
2785     t1 = tcg_temp_new_i64();
2786     t2 = tcg_temp_new_i64();
2787     tcg_gen_qemu_ld64(t1, o->in2, get_mem_index(s));
2788     tcg_gen_addi_i64(o->in2, o->in2, 8);
2789     tcg_gen_qemu_ld64(t2, o->in2, get_mem_index(s));
2790     gen_helper_load_psw(cpu_env, t1, t2);
2791     tcg_temp_free_i64(t1);
2792     tcg_temp_free_i64(t2);
2793     return EXIT_NORETURN;
2794 }
2795 #endif
2796
2797 static ExitStatus op_lam(DisasContext *s, DisasOps *o)
2798 {
2799     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
2800     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
2801     gen_helper_lam(cpu_env, r1, o->in2, r3);
2802     tcg_temp_free_i32(r1);
2803     tcg_temp_free_i32(r3);
2804     return NO_EXIT;
2805 }
2806
2807 static ExitStatus op_lm32(DisasContext *s, DisasOps *o)
2808 {
2809     int r1 = get_field(s->fields, r1);
2810     int r3 = get_field(s->fields, r3);
2811     TCGv_i64 t1, t2;
2812
2813     /* Only one register to read. */
2814     t1 = tcg_temp_new_i64();
2815     if (unlikely(r1 == r3)) {
2816         tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2817         store_reg32_i64(r1, t1);
2818         tcg_temp_free(t1);
2819         return NO_EXIT;
2820     }
2821
2822     /* First load the values of the first and last registers to trigger
2823        possible page faults. */
2824     t2 = tcg_temp_new_i64();
2825     tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2826     tcg_gen_addi_i64(t2, o->in2, 4 * ((r3 - r1) & 15));
2827     tcg_gen_qemu_ld32u(t2, t2, get_mem_index(s));
2828     store_reg32_i64(r1, t1);
2829     store_reg32_i64(r3, t2);
2830
2831     /* Only two registers to read. */
2832     if (((r1 + 1) & 15) == r3) {
2833         tcg_temp_free(t2);
2834         tcg_temp_free(t1);
2835         return NO_EXIT;
2836     }
2837
2838     /* Then load the remaining registers. Page fault can't occur. */
2839     r3 = (r3 - 1) & 15;
2840     tcg_gen_movi_i64(t2, 4);
2841     while (r1 != r3) {
2842         r1 = (r1 + 1) & 15;
2843         tcg_gen_add_i64(o->in2, o->in2, t2);
2844         tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2845         store_reg32_i64(r1, t1);
2846     }
2847     tcg_temp_free(t2);
2848     tcg_temp_free(t1);
2849
2850     return NO_EXIT;
2851 }
2852
2853 static ExitStatus op_lmh(DisasContext *s, DisasOps *o)
2854 {
2855     int r1 = get_field(s->fields, r1);
2856     int r3 = get_field(s->fields, r3);
2857     TCGv_i64 t1, t2;
2858
2859     /* Only one register to read. */
2860     t1 = tcg_temp_new_i64();
2861     if (unlikely(r1 == r3)) {
2862         tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2863         store_reg32h_i64(r1, t1);
2864         tcg_temp_free(t1);
2865         return NO_EXIT;
2866     }
2867
2868     /* First load the values of the first and last registers to trigger
2869        possible page faults. */
2870     t2 = tcg_temp_new_i64();
2871     tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2872     tcg_gen_addi_i64(t2, o->in2, 4 * ((r3 - r1) & 15));
2873     tcg_gen_qemu_ld32u(t2, t2, get_mem_index(s));
2874     store_reg32h_i64(r1, t1);
2875     store_reg32h_i64(r3, t2);
2876
2877     /* Only two registers to read. */
2878     if (((r1 + 1) & 15) == r3) {
2879         tcg_temp_free(t2);
2880         tcg_temp_free(t1);
2881         return NO_EXIT;
2882     }
2883
2884     /* Then load the remaining registers. Page fault can't occur. */
2885     r3 = (r3 - 1) & 15;
2886     tcg_gen_movi_i64(t2, 4);
2887     while (r1 != r3) {
2888         r1 = (r1 + 1) & 15;
2889         tcg_gen_add_i64(o->in2, o->in2, t2);
2890         tcg_gen_qemu_ld32u(t1, o->in2, get_mem_index(s));
2891         store_reg32h_i64(r1, t1);
2892     }
2893     tcg_temp_free(t2);
2894     tcg_temp_free(t1);
2895
2896     return NO_EXIT;
2897 }
2898
2899 static ExitStatus op_lm64(DisasContext *s, DisasOps *o)
2900 {
2901     int r1 = get_field(s->fields, r1);
2902     int r3 = get_field(s->fields, r3);
2903     TCGv_i64 t1, t2;
2904
2905     /* Only one register to read. */
2906     if (unlikely(r1 == r3)) {
2907         tcg_gen_qemu_ld64(regs[r1], o->in2, get_mem_index(s));
2908         return NO_EXIT;
2909     }
2910
2911     /* First load the values of the first and last registers to trigger
2912        possible page faults. */
2913     t1 = tcg_temp_new_i64();
2914     t2 = tcg_temp_new_i64();
2915     tcg_gen_qemu_ld64(t1, o->in2, get_mem_index(s));
2916     tcg_gen_addi_i64(t2, o->in2, 8 * ((r3 - r1) & 15));
2917     tcg_gen_qemu_ld64(regs[r3], t2, get_mem_index(s));
2918     tcg_gen_mov_i64(regs[r1], t1);
2919     tcg_temp_free(t2);
2920
2921     /* Only two registers to read. */
2922     if (((r1 + 1) & 15) == r3) {
2923         tcg_temp_free(t1);
2924         return NO_EXIT;
2925     }
2926
2927     /* Then load the remaining registers. Page fault can't occur. */
2928     r3 = (r3 - 1) & 15;
2929     tcg_gen_movi_i64(t1, 8);
2930     while (r1 != r3) {
2931         r1 = (r1 + 1) & 15;
2932         tcg_gen_add_i64(o->in2, o->in2, t1);
2933         tcg_gen_qemu_ld64(regs[r1], o->in2, get_mem_index(s));
2934     }
2935     tcg_temp_free(t1);
2936
2937     return NO_EXIT;
2938 }
2939
2940 static ExitStatus op_lpd(DisasContext *s, DisasOps *o)
2941 {
2942     TCGv_i64 a1, a2;
2943     TCGMemOp mop = s->insn->data;
2944
2945     /* In a parallel context, stop the world and single step.  */
2946     if (parallel_cpus) {
2947         potential_page_fault(s);
2948         gen_exception(EXCP_ATOMIC);
2949         return EXIT_NORETURN;
2950     }
2951
2952     /* In a serial context, perform the two loads ... */
2953     a1 = get_address(s, 0, get_field(s->fields, b1), get_field(s->fields, d1));
2954     a2 = get_address(s, 0, get_field(s->fields, b2), get_field(s->fields, d2));
2955     tcg_gen_qemu_ld_i64(o->out, a1, get_mem_index(s), mop | MO_ALIGN);
2956     tcg_gen_qemu_ld_i64(o->out2, a2, get_mem_index(s), mop | MO_ALIGN);
2957     tcg_temp_free_i64(a1);
2958     tcg_temp_free_i64(a2);
2959
2960     /* ... and indicate that we performed them while interlocked.  */
2961     gen_op_movi_cc(s, 0);
2962     return NO_EXIT;
2963 }
2964
2965 static ExitStatus op_lpq(DisasContext *s, DisasOps *o)
2966 {
2967     gen_helper_lpq(o->out, cpu_env, o->in2);
2968     return_low128(o->out2);
2969     return NO_EXIT;
2970 }
2971
2972 #ifndef CONFIG_USER_ONLY
2973 static ExitStatus op_lura(DisasContext *s, DisasOps *o)
2974 {
2975     check_privileged(s);
2976     potential_page_fault(s);
2977     gen_helper_lura(o->out, cpu_env, o->in2);
2978     return NO_EXIT;
2979 }
2980
2981 static ExitStatus op_lurag(DisasContext *s, DisasOps *o)
2982 {
2983     check_privileged(s);
2984     potential_page_fault(s);
2985     gen_helper_lurag(o->out, cpu_env, o->in2);
2986     return NO_EXIT;
2987 }
2988 #endif
2989
2990 static ExitStatus op_lzrb(DisasContext *s, DisasOps *o)
2991 {
2992     tcg_gen_andi_i64(o->out, o->in2, -256);
2993     return NO_EXIT;
2994 }
2995
2996 static ExitStatus op_mov2(DisasContext *s, DisasOps *o)
2997 {
2998     o->out = o->in2;
2999     o->g_out = o->g_in2;
3000     TCGV_UNUSED_I64(o->in2);
3001     o->g_in2 = false;
3002     return NO_EXIT;
3003 }
3004
3005 static ExitStatus op_mov2e(DisasContext *s, DisasOps *o)
3006 {
3007     int b2 = get_field(s->fields, b2);
3008     TCGv ar1 = tcg_temp_new_i64();
3009
3010     o->out = o->in2;
3011     o->g_out = o->g_in2;
3012     TCGV_UNUSED_I64(o->in2);
3013     o->g_in2 = false;
3014
3015     switch (s->tb->flags & FLAG_MASK_ASC) {
3016     case PSW_ASC_PRIMARY >> FLAG_MASK_PSW_SHIFT:
3017         tcg_gen_movi_i64(ar1, 0);
3018         break;
3019     case PSW_ASC_ACCREG >> FLAG_MASK_PSW_SHIFT:
3020         tcg_gen_movi_i64(ar1, 1);
3021         break;
3022     case PSW_ASC_SECONDARY >> FLAG_MASK_PSW_SHIFT:
3023         if (b2) {
3024             tcg_gen_ld32u_i64(ar1, cpu_env, offsetof(CPUS390XState, aregs[b2]));
3025         } else {
3026             tcg_gen_movi_i64(ar1, 0);
3027         }
3028         break;
3029     case PSW_ASC_HOME >> FLAG_MASK_PSW_SHIFT:
3030         tcg_gen_movi_i64(ar1, 2);
3031         break;
3032     }
3033
3034     tcg_gen_st32_i64(ar1, cpu_env, offsetof(CPUS390XState, aregs[1]));
3035     tcg_temp_free_i64(ar1);
3036
3037     return NO_EXIT;
3038 }
3039
3040 static ExitStatus op_movx(DisasContext *s, DisasOps *o)
3041 {
3042     o->out = o->in1;
3043     o->out2 = o->in2;
3044     o->g_out = o->g_in1;
3045     o->g_out2 = o->g_in2;
3046     TCGV_UNUSED_I64(o->in1);
3047     TCGV_UNUSED_I64(o->in2);
3048     o->g_in1 = o->g_in2 = false;
3049     return NO_EXIT;
3050 }
3051
3052 static ExitStatus op_mvc(DisasContext *s, DisasOps *o)
3053 {
3054     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3055     gen_helper_mvc(cpu_env, l, o->addr1, o->in2);
3056     tcg_temp_free_i32(l);
3057     return NO_EXIT;
3058 }
3059
3060 static ExitStatus op_mvcin(DisasContext *s, DisasOps *o)
3061 {
3062     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3063     gen_helper_mvcin(cpu_env, l, o->addr1, o->in2);
3064     tcg_temp_free_i32(l);
3065     return NO_EXIT;
3066 }
3067
3068 static ExitStatus op_mvcl(DisasContext *s, DisasOps *o)
3069 {
3070     int r1 = get_field(s->fields, r1);
3071     int r2 = get_field(s->fields, r2);
3072     TCGv_i32 t1, t2;
3073
3074     /* r1 and r2 must be even.  */
3075     if (r1 & 1 || r2 & 1) {
3076         gen_program_exception(s, PGM_SPECIFICATION);
3077         return EXIT_NORETURN;
3078     }
3079
3080     t1 = tcg_const_i32(r1);
3081     t2 = tcg_const_i32(r2);
3082     gen_helper_mvcl(cc_op, cpu_env, t1, t2);
3083     tcg_temp_free_i32(t1);
3084     tcg_temp_free_i32(t2);
3085     set_cc_static(s);
3086     return NO_EXIT;
3087 }
3088
3089 static ExitStatus op_mvcle(DisasContext *s, DisasOps *o)
3090 {
3091     int r1 = get_field(s->fields, r1);
3092     int r3 = get_field(s->fields, r3);
3093     TCGv_i32 t1, t3;
3094
3095     /* r1 and r3 must be even.  */
3096     if (r1 & 1 || r3 & 1) {
3097         gen_program_exception(s, PGM_SPECIFICATION);
3098         return EXIT_NORETURN;
3099     }
3100
3101     t1 = tcg_const_i32(r1);
3102     t3 = tcg_const_i32(r3);
3103     gen_helper_mvcle(cc_op, cpu_env, t1, o->in2, t3);
3104     tcg_temp_free_i32(t1);
3105     tcg_temp_free_i32(t3);
3106     set_cc_static(s);
3107     return NO_EXIT;
3108 }
3109
3110 static ExitStatus op_mvclu(DisasContext *s, DisasOps *o)
3111 {
3112     int r1 = get_field(s->fields, r1);
3113     int r3 = get_field(s->fields, r3);
3114     TCGv_i32 t1, t3;
3115
3116     /* r1 and r3 must be even.  */
3117     if (r1 & 1 || r3 & 1) {
3118         gen_program_exception(s, PGM_SPECIFICATION);
3119         return EXIT_NORETURN;
3120     }
3121
3122     t1 = tcg_const_i32(r1);
3123     t3 = tcg_const_i32(r3);
3124     gen_helper_mvclu(cc_op, cpu_env, t1, o->in2, t3);
3125     tcg_temp_free_i32(t1);
3126     tcg_temp_free_i32(t3);
3127     set_cc_static(s);
3128     return NO_EXIT;
3129 }
3130
3131 static ExitStatus op_mvcos(DisasContext *s, DisasOps *o)
3132 {
3133     int r3 = get_field(s->fields, r3);
3134     gen_helper_mvcos(cc_op, cpu_env, o->addr1, o->in2, regs[r3]);
3135     set_cc_static(s);
3136     return NO_EXIT;
3137 }
3138
3139 #ifndef CONFIG_USER_ONLY
3140 static ExitStatus op_mvcp(DisasContext *s, DisasOps *o)
3141 {
3142     int r1 = get_field(s->fields, l1);
3143     check_privileged(s);
3144     gen_helper_mvcp(cc_op, cpu_env, regs[r1], o->addr1, o->in2);
3145     set_cc_static(s);
3146     return NO_EXIT;
3147 }
3148
3149 static ExitStatus op_mvcs(DisasContext *s, DisasOps *o)
3150 {
3151     int r1 = get_field(s->fields, l1);
3152     check_privileged(s);
3153     gen_helper_mvcs(cc_op, cpu_env, regs[r1], o->addr1, o->in2);
3154     set_cc_static(s);
3155     return NO_EXIT;
3156 }
3157 #endif
3158
3159 static ExitStatus op_mvn(DisasContext *s, DisasOps *o)
3160 {
3161     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3162     gen_helper_mvn(cpu_env, l, o->addr1, o->in2);
3163     tcg_temp_free_i32(l);
3164     return NO_EXIT;
3165 }
3166
3167 static ExitStatus op_mvo(DisasContext *s, DisasOps *o)
3168 {
3169     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3170     gen_helper_mvo(cpu_env, l, o->addr1, o->in2);
3171     tcg_temp_free_i32(l);
3172     return NO_EXIT;
3173 }
3174
3175 static ExitStatus op_mvpg(DisasContext *s, DisasOps *o)
3176 {
3177     gen_helper_mvpg(cc_op, cpu_env, regs[0], o->in1, o->in2);
3178     set_cc_static(s);
3179     return NO_EXIT;
3180 }
3181
3182 static ExitStatus op_mvst(DisasContext *s, DisasOps *o)
3183 {
3184     gen_helper_mvst(o->in1, cpu_env, regs[0], o->in1, o->in2);
3185     set_cc_static(s);
3186     return_low128(o->in2);
3187     return NO_EXIT;
3188 }
3189
3190 static ExitStatus op_mvz(DisasContext *s, DisasOps *o)
3191 {
3192     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3193     gen_helper_mvz(cpu_env, l, o->addr1, o->in2);
3194     tcg_temp_free_i32(l);
3195     return NO_EXIT;
3196 }
3197
3198 static ExitStatus op_mul(DisasContext *s, DisasOps *o)
3199 {
3200     tcg_gen_mul_i64(o->out, o->in1, o->in2);
3201     return NO_EXIT;
3202 }
3203
3204 static ExitStatus op_mul128(DisasContext *s, DisasOps *o)
3205 {
3206     tcg_gen_mulu2_i64(o->out2, o->out, o->in1, o->in2);
3207     return NO_EXIT;
3208 }
3209
3210 static ExitStatus op_meeb(DisasContext *s, DisasOps *o)
3211 {
3212     gen_helper_meeb(o->out, cpu_env, o->in1, o->in2);
3213     return NO_EXIT;
3214 }
3215
3216 static ExitStatus op_mdeb(DisasContext *s, DisasOps *o)
3217 {
3218     gen_helper_mdeb(o->out, cpu_env, o->in1, o->in2);
3219     return NO_EXIT;
3220 }
3221
3222 static ExitStatus op_mdb(DisasContext *s, DisasOps *o)
3223 {
3224     gen_helper_mdb(o->out, cpu_env, o->in1, o->in2);
3225     return NO_EXIT;
3226 }
3227
3228 static ExitStatus op_mxb(DisasContext *s, DisasOps *o)
3229 {
3230     gen_helper_mxb(o->out, cpu_env, o->out, o->out2, o->in1, o->in2);
3231     return_low128(o->out2);
3232     return NO_EXIT;
3233 }
3234
3235 static ExitStatus op_mxdb(DisasContext *s, DisasOps *o)
3236 {
3237     gen_helper_mxdb(o->out, cpu_env, o->out, o->out2, o->in2);
3238     return_low128(o->out2);
3239     return NO_EXIT;
3240 }
3241
3242 static ExitStatus op_maeb(DisasContext *s, DisasOps *o)
3243 {
3244     TCGv_i64 r3 = load_freg32_i64(get_field(s->fields, r3));
3245     gen_helper_maeb(o->out, cpu_env, o->in1, o->in2, r3);
3246     tcg_temp_free_i64(r3);
3247     return NO_EXIT;
3248 }
3249
3250 static ExitStatus op_madb(DisasContext *s, DisasOps *o)
3251 {
3252     int r3 = get_field(s->fields, r3);
3253     gen_helper_madb(o->out, cpu_env, o->in1, o->in2, fregs[r3]);
3254     return NO_EXIT;
3255 }
3256
3257 static ExitStatus op_mseb(DisasContext *s, DisasOps *o)
3258 {
3259     TCGv_i64 r3 = load_freg32_i64(get_field(s->fields, r3));
3260     gen_helper_mseb(o->out, cpu_env, o->in1, o->in2, r3);
3261     tcg_temp_free_i64(r3);
3262     return NO_EXIT;
3263 }
3264
3265 static ExitStatus op_msdb(DisasContext *s, DisasOps *o)
3266 {
3267     int r3 = get_field(s->fields, r3);
3268     gen_helper_msdb(o->out, cpu_env, o->in1, o->in2, fregs[r3]);
3269     return NO_EXIT;
3270 }
3271
3272 static ExitStatus op_nabs(DisasContext *s, DisasOps *o)
3273 {
3274     TCGv_i64 z, n;
3275     z = tcg_const_i64(0);
3276     n = tcg_temp_new_i64();
3277     tcg_gen_neg_i64(n, o->in2);
3278     tcg_gen_movcond_i64(TCG_COND_GE, o->out, o->in2, z, n, o->in2);
3279     tcg_temp_free_i64(n);
3280     tcg_temp_free_i64(z);
3281     return NO_EXIT;
3282 }
3283
3284 static ExitStatus op_nabsf32(DisasContext *s, DisasOps *o)
3285 {
3286     tcg_gen_ori_i64(o->out, o->in2, 0x80000000ull);
3287     return NO_EXIT;
3288 }
3289
3290 static ExitStatus op_nabsf64(DisasContext *s, DisasOps *o)
3291 {
3292     tcg_gen_ori_i64(o->out, o->in2, 0x8000000000000000ull);
3293     return NO_EXIT;
3294 }
3295
3296 static ExitStatus op_nabsf128(DisasContext *s, DisasOps *o)
3297 {
3298     tcg_gen_ori_i64(o->out, o->in1, 0x8000000000000000ull);
3299     tcg_gen_mov_i64(o->out2, o->in2);
3300     return NO_EXIT;
3301 }
3302
3303 static ExitStatus op_nc(DisasContext *s, DisasOps *o)
3304 {
3305     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3306     gen_helper_nc(cc_op, cpu_env, l, o->addr1, o->in2);
3307     tcg_temp_free_i32(l);
3308     set_cc_static(s);
3309     return NO_EXIT;
3310 }
3311
3312 static ExitStatus op_neg(DisasContext *s, DisasOps *o)
3313 {
3314     tcg_gen_neg_i64(o->out, o->in2);
3315     return NO_EXIT;
3316 }
3317
3318 static ExitStatus op_negf32(DisasContext *s, DisasOps *o)
3319 {
3320     tcg_gen_xori_i64(o->out, o->in2, 0x80000000ull);
3321     return NO_EXIT;
3322 }
3323
3324 static ExitStatus op_negf64(DisasContext *s, DisasOps *o)
3325 {
3326     tcg_gen_xori_i64(o->out, o->in2, 0x8000000000000000ull);
3327     return NO_EXIT;
3328 }
3329
3330 static ExitStatus op_negf128(DisasContext *s, DisasOps *o)
3331 {
3332     tcg_gen_xori_i64(o->out, o->in1, 0x8000000000000000ull);
3333     tcg_gen_mov_i64(o->out2, o->in2);
3334     return NO_EXIT;
3335 }
3336
3337 static ExitStatus op_oc(DisasContext *s, DisasOps *o)
3338 {
3339     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3340     gen_helper_oc(cc_op, cpu_env, l, o->addr1, o->in2);
3341     tcg_temp_free_i32(l);
3342     set_cc_static(s);
3343     return NO_EXIT;
3344 }
3345
3346 static ExitStatus op_or(DisasContext *s, DisasOps *o)
3347 {
3348     tcg_gen_or_i64(o->out, o->in1, o->in2);
3349     return NO_EXIT;
3350 }
3351
3352 static ExitStatus op_ori(DisasContext *s, DisasOps *o)
3353 {
3354     int shift = s->insn->data & 0xff;
3355     int size = s->insn->data >> 8;
3356     uint64_t mask = ((1ull << size) - 1) << shift;
3357
3358     assert(!o->g_in2);
3359     tcg_gen_shli_i64(o->in2, o->in2, shift);
3360     tcg_gen_or_i64(o->out, o->in1, o->in2);
3361
3362     /* Produce the CC from only the bits manipulated.  */
3363     tcg_gen_andi_i64(cc_dst, o->out, mask);
3364     set_cc_nz_u64(s, cc_dst);
3365     return NO_EXIT;
3366 }
3367
3368 static ExitStatus op_pack(DisasContext *s, DisasOps *o)
3369 {
3370     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
3371     gen_helper_pack(cpu_env, l, o->addr1, o->in2);
3372     tcg_temp_free_i32(l);
3373     return NO_EXIT;
3374 }
3375
3376 static ExitStatus op_pka(DisasContext *s, DisasOps *o)
3377 {
3378     int l2 = get_field(s->fields, l2) + 1;
3379     TCGv_i32 l;
3380
3381     /* The length must not exceed 32 bytes.  */
3382     if (l2 > 32) {
3383         gen_program_exception(s, PGM_SPECIFICATION);
3384         return EXIT_NORETURN;
3385     }
3386     l = tcg_const_i32(l2);
3387     gen_helper_pka(cpu_env, o->addr1, o->in2, l);
3388     tcg_temp_free_i32(l);
3389     return NO_EXIT;
3390 }
3391
3392 static ExitStatus op_pku(DisasContext *s, DisasOps *o)
3393 {
3394     int l2 = get_field(s->fields, l2) + 1;
3395     TCGv_i32 l;
3396
3397     /* The length must be even and should not exceed 64 bytes.  */
3398     if ((l2 & 1) || (l2 > 64)) {
3399         gen_program_exception(s, PGM_SPECIFICATION);
3400         return EXIT_NORETURN;
3401     }
3402     l = tcg_const_i32(l2);
3403     gen_helper_pku(cpu_env, o->addr1, o->in2, l);
3404     tcg_temp_free_i32(l);
3405     return NO_EXIT;
3406 }
3407
3408 static ExitStatus op_popcnt(DisasContext *s, DisasOps *o)
3409 {
3410     gen_helper_popcnt(o->out, o->in2);
3411     return NO_EXIT;
3412 }
3413
3414 #ifndef CONFIG_USER_ONLY
3415 static ExitStatus op_ptlb(DisasContext *s, DisasOps *o)
3416 {
3417     check_privileged(s);
3418     gen_helper_ptlb(cpu_env);
3419     return NO_EXIT;
3420 }
3421 #endif
3422
3423 static ExitStatus op_risbg(DisasContext *s, DisasOps *o)
3424 {
3425     int i3 = get_field(s->fields, i3);
3426     int i4 = get_field(s->fields, i4);
3427     int i5 = get_field(s->fields, i5);
3428     int do_zero = i4 & 0x80;
3429     uint64_t mask, imask, pmask;
3430     int pos, len, rot;
3431
3432     /* Adjust the arguments for the specific insn.  */
3433     switch (s->fields->op2) {
3434     case 0x55: /* risbg */
3435         i3 &= 63;
3436         i4 &= 63;
3437         pmask = ~0;
3438         break;
3439     case 0x5d: /* risbhg */
3440         i3 &= 31;
3441         i4 &= 31;
3442         pmask = 0xffffffff00000000ull;
3443         break;
3444     case 0x51: /* risblg */
3445         i3 &= 31;
3446         i4 &= 31;
3447         pmask = 0x00000000ffffffffull;
3448         break;
3449     default:
3450         abort();
3451     }
3452
3453     /* MASK is the set of bits to be inserted from R2.
3454        Take care for I3/I4 wraparound.  */
3455     mask = pmask >> i3;
3456     if (i3 <= i4) {
3457         mask ^= pmask >> i4 >> 1;
3458     } else {
3459         mask |= ~(pmask >> i4 >> 1);
3460     }
3461     mask &= pmask;
3462
3463     /* IMASK is the set of bits to be kept from R1.  In the case of the high/low
3464        insns, we need to keep the other half of the register.  */
3465     imask = ~mask | ~pmask;
3466     if (do_zero) {
3467         if (s->fields->op2 == 0x55) {
3468             imask = 0;
3469         } else {
3470             imask = ~pmask;
3471         }
3472     }
3473
3474     len = i4 - i3 + 1;
3475     pos = 63 - i4;
3476     rot = i5 & 63;
3477     if (s->fields->op2 == 0x5d) {
3478         pos += 32;
3479     }
3480
3481     /* In some cases we can implement this with extract.  */
3482     if (imask == 0 && pos == 0 && len > 0 && len <= rot) {
3483         tcg_gen_extract_i64(o->out, o->in2, 64 - rot, len);
3484         return NO_EXIT;
3485     }
3486
3487     /* In some cases we can implement this with deposit.  */
3488     if (len > 0 && (imask == 0 || ~mask == imask)) {
3489         /* Note that we rotate the bits to be inserted to the lsb, not to
3490            the position as described in the PoO.  */
3491         rot = (rot - pos) & 63;
3492     } else {
3493         pos = -1;
3494     }
3495
3496     /* Rotate the input as necessary.  */
3497     tcg_gen_rotli_i64(o->in2, o->in2, rot);
3498
3499     /* Insert the selected bits into the output.  */
3500     if (pos >= 0) {
3501         if (imask == 0) {
3502             tcg_gen_deposit_z_i64(o->out, o->in2, pos, len);
3503         } else {
3504             tcg_gen_deposit_i64(o->out, o->out, o->in2, pos, len);
3505         }
3506     } else if (imask == 0) {
3507         tcg_gen_andi_i64(o->out, o->in2, mask);
3508     } else {
3509         tcg_gen_andi_i64(o->in2, o->in2, mask);
3510         tcg_gen_andi_i64(o->out, o->out, imask);
3511         tcg_gen_or_i64(o->out, o->out, o->in2);
3512     }
3513     return NO_EXIT;
3514 }
3515
3516 static ExitStatus op_rosbg(DisasContext *s, DisasOps *o)
3517 {
3518     int i3 = get_field(s->fields, i3);
3519     int i4 = get_field(s->fields, i4);
3520     int i5 = get_field(s->fields, i5);
3521     uint64_t mask;
3522
3523     /* If this is a test-only form, arrange to discard the result.  */
3524     if (i3 & 0x80) {
3525         o->out = tcg_temp_new_i64();
3526         o->g_out = false;
3527     }
3528
3529     i3 &= 63;
3530     i4 &= 63;
3531     i5 &= 63;
3532
3533     /* MASK is the set of bits to be operated on from R2.
3534        Take care for I3/I4 wraparound.  */
3535     mask = ~0ull >> i3;
3536     if (i3 <= i4) {
3537         mask ^= ~0ull >> i4 >> 1;
3538     } else {
3539         mask |= ~(~0ull >> i4 >> 1);
3540     }
3541
3542     /* Rotate the input as necessary.  */
3543     tcg_gen_rotli_i64(o->in2, o->in2, i5);
3544
3545     /* Operate.  */
3546     switch (s->fields->op2) {
3547     case 0x55: /* AND */
3548         tcg_gen_ori_i64(o->in2, o->in2, ~mask);
3549         tcg_gen_and_i64(o->out, o->out, o->in2);
3550         break;
3551     case 0x56: /* OR */
3552         tcg_gen_andi_i64(o->in2, o->in2, mask);
3553         tcg_gen_or_i64(o->out, o->out, o->in2);
3554         break;
3555     case 0x57: /* XOR */
3556         tcg_gen_andi_i64(o->in2, o->in2, mask);
3557         tcg_gen_xor_i64(o->out, o->out, o->in2);
3558         break;
3559     default:
3560         abort();
3561     }
3562
3563     /* Set the CC.  */
3564     tcg_gen_andi_i64(cc_dst, o->out, mask);
3565     set_cc_nz_u64(s, cc_dst);
3566     return NO_EXIT;
3567 }
3568
3569 static ExitStatus op_rev16(DisasContext *s, DisasOps *o)
3570 {
3571     tcg_gen_bswap16_i64(o->out, o->in2);
3572     return NO_EXIT;
3573 }
3574
3575 static ExitStatus op_rev32(DisasContext *s, DisasOps *o)
3576 {
3577     tcg_gen_bswap32_i64(o->out, o->in2);
3578     return NO_EXIT;
3579 }
3580
3581 static ExitStatus op_rev64(DisasContext *s, DisasOps *o)
3582 {
3583     tcg_gen_bswap64_i64(o->out, o->in2);
3584     return NO_EXIT;
3585 }
3586
3587 static ExitStatus op_rll32(DisasContext *s, DisasOps *o)
3588 {
3589     TCGv_i32 t1 = tcg_temp_new_i32();
3590     TCGv_i32 t2 = tcg_temp_new_i32();
3591     TCGv_i32 to = tcg_temp_new_i32();
3592     tcg_gen_extrl_i64_i32(t1, o->in1);
3593     tcg_gen_extrl_i64_i32(t2, o->in2);
3594     tcg_gen_rotl_i32(to, t1, t2);
3595     tcg_gen_extu_i32_i64(o->out, to);
3596     tcg_temp_free_i32(t1);
3597     tcg_temp_free_i32(t2);
3598     tcg_temp_free_i32(to);
3599     return NO_EXIT;
3600 }
3601
3602 static ExitStatus op_rll64(DisasContext *s, DisasOps *o)
3603 {
3604     tcg_gen_rotl_i64(o->out, o->in1, o->in2);
3605     return NO_EXIT;
3606 }
3607
3608 #ifndef CONFIG_USER_ONLY
3609 static ExitStatus op_rrbe(DisasContext *s, DisasOps *o)
3610 {
3611     check_privileged(s);
3612     gen_helper_rrbe(cc_op, cpu_env, o->in2);
3613     set_cc_static(s);
3614     return NO_EXIT;
3615 }
3616
3617 static ExitStatus op_sacf(DisasContext *s, DisasOps *o)
3618 {
3619     check_privileged(s);
3620     gen_helper_sacf(cpu_env, o->in2);
3621     /* Addressing mode has changed, so end the block.  */
3622     return EXIT_PC_STALE;
3623 }
3624 #endif
3625
3626 static ExitStatus op_sam(DisasContext *s, DisasOps *o)
3627 {
3628     int sam = s->insn->data;
3629     TCGv_i64 tsam;
3630     uint64_t mask;
3631
3632     switch (sam) {
3633     case 0:
3634         mask = 0xffffff;
3635         break;
3636     case 1:
3637         mask = 0x7fffffff;
3638         break;
3639     default:
3640         mask = -1;
3641         break;
3642     }
3643
3644     /* Bizarre but true, we check the address of the current insn for the
3645        specification exception, not the next to be executed.  Thus the PoO
3646        documents that Bad Things Happen two bytes before the end.  */
3647     if (s->pc & ~mask) {
3648         gen_program_exception(s, PGM_SPECIFICATION);
3649         return EXIT_NORETURN;
3650     }
3651     s->next_pc &= mask;
3652
3653     tsam = tcg_const_i64(sam);
3654     tcg_gen_deposit_i64(psw_mask, psw_mask, tsam, 31, 2);
3655     tcg_temp_free_i64(tsam);
3656
3657     /* Always exit the TB, since we (may have) changed execution mode.  */
3658     return EXIT_PC_STALE;
3659 }
3660
3661 static ExitStatus op_sar(DisasContext *s, DisasOps *o)
3662 {
3663     int r1 = get_field(s->fields, r1);
3664     tcg_gen_st32_i64(o->in2, cpu_env, offsetof(CPUS390XState, aregs[r1]));
3665     return NO_EXIT;
3666 }
3667
3668 static ExitStatus op_seb(DisasContext *s, DisasOps *o)
3669 {
3670     gen_helper_seb(o->out, cpu_env, o->in1, o->in2);
3671     return NO_EXIT;
3672 }
3673
3674 static ExitStatus op_sdb(DisasContext *s, DisasOps *o)
3675 {
3676     gen_helper_sdb(o->out, cpu_env, o->in1, o->in2);
3677     return NO_EXIT;
3678 }
3679
3680 static ExitStatus op_sxb(DisasContext *s, DisasOps *o)
3681 {
3682     gen_helper_sxb(o->out, cpu_env, o->out, o->out2, o->in1, o->in2);
3683     return_low128(o->out2);
3684     return NO_EXIT;
3685 }
3686
3687 static ExitStatus op_sqeb(DisasContext *s, DisasOps *o)
3688 {
3689     gen_helper_sqeb(o->out, cpu_env, o->in2);
3690     return NO_EXIT;
3691 }
3692
3693 static ExitStatus op_sqdb(DisasContext *s, DisasOps *o)
3694 {
3695     gen_helper_sqdb(o->out, cpu_env, o->in2);
3696     return NO_EXIT;
3697 }
3698
3699 static ExitStatus op_sqxb(DisasContext *s, DisasOps *o)
3700 {
3701     gen_helper_sqxb(o->out, cpu_env, o->in1, o->in2);
3702     return_low128(o->out2);
3703     return NO_EXIT;
3704 }
3705
3706 #ifndef CONFIG_USER_ONLY
3707 static ExitStatus op_servc(DisasContext *s, DisasOps *o)
3708 {
3709     check_privileged(s);
3710     potential_page_fault(s);
3711     gen_helper_servc(cc_op, cpu_env, o->in2, o->in1);
3712     set_cc_static(s);
3713     return NO_EXIT;
3714 }
3715
3716 static ExitStatus op_sigp(DisasContext *s, DisasOps *o)
3717 {
3718     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
3719     check_privileged(s);
3720     potential_page_fault(s);
3721     gen_helper_sigp(cc_op, cpu_env, o->in2, r1, o->in1);
3722     set_cc_static(s);
3723     tcg_temp_free_i32(r1);
3724     return NO_EXIT;
3725 }
3726 #endif
3727
3728 static ExitStatus op_soc(DisasContext *s, DisasOps *o)
3729 {
3730     DisasCompare c;
3731     TCGv_i64 a, h;
3732     TCGLabel *lab;
3733     int r1;
3734
3735     disas_jcc(s, &c, get_field(s->fields, m3));
3736
3737     /* We want to store when the condition is fulfilled, so branch
3738        out when it's not */
3739     c.cond = tcg_invert_cond(c.cond);
3740
3741     lab = gen_new_label();
3742     if (c.is_64) {
3743         tcg_gen_brcond_i64(c.cond, c.u.s64.a, c.u.s64.b, lab);
3744     } else {
3745         tcg_gen_brcond_i32(c.cond, c.u.s32.a, c.u.s32.b, lab);
3746     }
3747     free_compare(&c);
3748
3749     r1 = get_field(s->fields, r1);
3750     a = get_address(s, 0, get_field(s->fields, b2), get_field(s->fields, d2));
3751     switch (s->insn->data) {
3752     case 1: /* STOCG */
3753         tcg_gen_qemu_st64(regs[r1], a, get_mem_index(s));
3754         break;
3755     case 0: /* STOC */
3756         tcg_gen_qemu_st32(regs[r1], a, get_mem_index(s));
3757         break;
3758     case 2: /* STOCFH */
3759         h = tcg_temp_new_i64();
3760         tcg_gen_shri_i64(h, regs[r1], 32);
3761         tcg_gen_qemu_st32(h, a, get_mem_index(s));
3762         tcg_temp_free_i64(h);
3763         break;
3764     default:
3765         g_assert_not_reached();
3766     }
3767     tcg_temp_free_i64(a);
3768
3769     gen_set_label(lab);
3770     return NO_EXIT;
3771 }
3772
3773 static ExitStatus op_sla(DisasContext *s, DisasOps *o)
3774 {
3775     uint64_t sign = 1ull << s->insn->data;
3776     enum cc_op cco = s->insn->data == 31 ? CC_OP_SLA_32 : CC_OP_SLA_64;
3777     gen_op_update2_cc_i64(s, cco, o->in1, o->in2);
3778     tcg_gen_shl_i64(o->out, o->in1, o->in2);
3779     /* The arithmetic left shift is curious in that it does not affect
3780        the sign bit.  Copy that over from the source unchanged.  */
3781     tcg_gen_andi_i64(o->out, o->out, ~sign);
3782     tcg_gen_andi_i64(o->in1, o->in1, sign);
3783     tcg_gen_or_i64(o->out, o->out, o->in1);
3784     return NO_EXIT;
3785 }
3786
3787 static ExitStatus op_sll(DisasContext *s, DisasOps *o)
3788 {
3789     tcg_gen_shl_i64(o->out, o->in1, o->in2);
3790     return NO_EXIT;
3791 }
3792
3793 static ExitStatus op_sra(DisasContext *s, DisasOps *o)
3794 {
3795     tcg_gen_sar_i64(o->out, o->in1, o->in2);
3796     return NO_EXIT;
3797 }
3798
3799 static ExitStatus op_srl(DisasContext *s, DisasOps *o)
3800 {
3801     tcg_gen_shr_i64(o->out, o->in1, o->in2);
3802     return NO_EXIT;
3803 }
3804
3805 static ExitStatus op_sfpc(DisasContext *s, DisasOps *o)
3806 {
3807     gen_helper_sfpc(cpu_env, o->in2);
3808     return NO_EXIT;
3809 }
3810
3811 static ExitStatus op_sfas(DisasContext *s, DisasOps *o)
3812 {
3813     gen_helper_sfas(cpu_env, o->in2);
3814     return NO_EXIT;
3815 }
3816
3817 static ExitStatus op_srnm(DisasContext *s, DisasOps *o)
3818 {
3819     int b2 = get_field(s->fields, b2);
3820     int d2 = get_field(s->fields, d2);
3821     TCGv_i64 t1 = tcg_temp_new_i64();
3822     TCGv_i64 t2 = tcg_temp_new_i64();
3823     int mask, pos, len;
3824
3825     switch (s->fields->op2) {
3826     case 0x99: /* SRNM */
3827         pos = 0, len = 2;
3828         break;
3829     case 0xb8: /* SRNMB */
3830         pos = 0, len = 3;
3831         break;
3832     case 0xb9: /* SRNMT */
3833         pos = 4, len = 3;
3834         break;
3835     default:
3836         tcg_abort();
3837     }
3838     mask = (1 << len) - 1;
3839
3840     /* Insert the value into the appropriate field of the FPC.  */
3841     if (b2 == 0) {
3842         tcg_gen_movi_i64(t1, d2 & mask);
3843     } else {
3844         tcg_gen_addi_i64(t1, regs[b2], d2);
3845         tcg_gen_andi_i64(t1, t1, mask);
3846     }
3847     tcg_gen_ld32u_i64(t2, cpu_env, offsetof(CPUS390XState, fpc));
3848     tcg_gen_deposit_i64(t2, t2, t1, pos, len);
3849     tcg_temp_free_i64(t1);
3850
3851     /* Then install the new FPC to set the rounding mode in fpu_status.  */
3852     gen_helper_sfpc(cpu_env, t2);
3853     tcg_temp_free_i64(t2);
3854     return NO_EXIT;
3855 }
3856
3857 #ifndef CONFIG_USER_ONLY
3858 static ExitStatus op_spka(DisasContext *s, DisasOps *o)
3859 {
3860     check_privileged(s);
3861     tcg_gen_shri_i64(o->in2, o->in2, 4);
3862     tcg_gen_deposit_i64(psw_mask, psw_mask, o->in2, PSW_SHIFT_KEY, 4);
3863     return NO_EXIT;
3864 }
3865
3866 static ExitStatus op_sske(DisasContext *s, DisasOps *o)
3867 {
3868     check_privileged(s);
3869     gen_helper_sske(cpu_env, o->in1, o->in2);
3870     return NO_EXIT;
3871 }
3872
3873 static ExitStatus op_ssm(DisasContext *s, DisasOps *o)
3874 {
3875     check_privileged(s);
3876     tcg_gen_deposit_i64(psw_mask, psw_mask, o->in2, 56, 8);
3877     /* Exit to main loop to reevaluate s390_cpu_exec_interrupt.  */
3878     return EXIT_PC_STALE_NOCHAIN;
3879 }
3880
3881 static ExitStatus op_stap(DisasContext *s, DisasOps *o)
3882 {
3883     check_privileged(s);
3884     /* ??? Surely cpu address != cpu number.  In any case the previous
3885        version of this stored more than the required half-word, so it
3886        is unlikely this has ever been tested.  */
3887     tcg_gen_ld32u_i64(o->out, cpu_env, offsetof(CPUS390XState, cpu_num));
3888     return NO_EXIT;
3889 }
3890
3891 static ExitStatus op_stck(DisasContext *s, DisasOps *o)
3892 {
3893     gen_helper_stck(o->out, cpu_env);
3894     /* ??? We don't implement clock states.  */
3895     gen_op_movi_cc(s, 0);
3896     return NO_EXIT;
3897 }
3898
3899 static ExitStatus op_stcke(DisasContext *s, DisasOps *o)
3900 {
3901     TCGv_i64 c1 = tcg_temp_new_i64();
3902     TCGv_i64 c2 = tcg_temp_new_i64();
3903     gen_helper_stck(c1, cpu_env);
3904     /* Shift the 64-bit value into its place as a zero-extended
3905        104-bit value.  Note that "bit positions 64-103 are always
3906        non-zero so that they compare differently to STCK"; we set
3907        the least significant bit to 1.  */
3908     tcg_gen_shli_i64(c2, c1, 56);
3909     tcg_gen_shri_i64(c1, c1, 8);
3910     tcg_gen_ori_i64(c2, c2, 0x10000);
3911     tcg_gen_qemu_st64(c1, o->in2, get_mem_index(s));
3912     tcg_gen_addi_i64(o->in2, o->in2, 8);
3913     tcg_gen_qemu_st64(c2, o->in2, get_mem_index(s));
3914     tcg_temp_free_i64(c1);
3915     tcg_temp_free_i64(c2);
3916     /* ??? We don't implement clock states.  */
3917     gen_op_movi_cc(s, 0);
3918     return NO_EXIT;
3919 }
3920
3921 static ExitStatus op_sckc(DisasContext *s, DisasOps *o)
3922 {
3923     check_privileged(s);
3924     gen_helper_sckc(cpu_env, o->in2);
3925     return NO_EXIT;
3926 }
3927
3928 static ExitStatus op_stckc(DisasContext *s, DisasOps *o)
3929 {
3930     check_privileged(s);
3931     gen_helper_stckc(o->out, cpu_env);
3932     return NO_EXIT;
3933 }
3934
3935 static ExitStatus op_stctg(DisasContext *s, DisasOps *o)
3936 {
3937     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
3938     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
3939     check_privileged(s);
3940     gen_helper_stctg(cpu_env, r1, o->in2, r3);
3941     tcg_temp_free_i32(r1);
3942     tcg_temp_free_i32(r3);
3943     return NO_EXIT;
3944 }
3945
3946 static ExitStatus op_stctl(DisasContext *s, DisasOps *o)
3947 {
3948     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
3949     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
3950     check_privileged(s);
3951     gen_helper_stctl(cpu_env, r1, o->in2, r3);
3952     tcg_temp_free_i32(r1);
3953     tcg_temp_free_i32(r3);
3954     return NO_EXIT;
3955 }
3956
3957 static ExitStatus op_stidp(DisasContext *s, DisasOps *o)
3958 {
3959     check_privileged(s);
3960     tcg_gen_ld_i64(o->out, cpu_env, offsetof(CPUS390XState, cpuid));
3961     tcg_gen_qemu_st_i64(o->out, o->addr1, get_mem_index(s), MO_TEQ | MO_ALIGN);
3962     return NO_EXIT;
3963 }
3964
3965 static ExitStatus op_spt(DisasContext *s, DisasOps *o)
3966 {
3967     check_privileged(s);
3968     gen_helper_spt(cpu_env, o->in2);
3969     return NO_EXIT;
3970 }
3971
3972 static ExitStatus op_stfl(DisasContext *s, DisasOps *o)
3973 {
3974     check_privileged(s);
3975     gen_helper_stfl(cpu_env);
3976     return NO_EXIT;
3977 }
3978
3979 static ExitStatus op_stpt(DisasContext *s, DisasOps *o)
3980 {
3981     check_privileged(s);
3982     gen_helper_stpt(o->out, cpu_env);
3983     return NO_EXIT;
3984 }
3985
3986 static ExitStatus op_stsi(DisasContext *s, DisasOps *o)
3987 {
3988     check_privileged(s);
3989     potential_page_fault(s);
3990     gen_helper_stsi(cc_op, cpu_env, o->in2, regs[0], regs[1]);
3991     set_cc_static(s);
3992     return NO_EXIT;
3993 }
3994
3995 static ExitStatus op_spx(DisasContext *s, DisasOps *o)
3996 {
3997     check_privileged(s);
3998     gen_helper_spx(cpu_env, o->in2);
3999     return NO_EXIT;
4000 }
4001
4002 static ExitStatus op_xsch(DisasContext *s, DisasOps *o)
4003 {
4004     check_privileged(s);
4005     potential_page_fault(s);
4006     gen_helper_xsch(cpu_env, regs[1]);
4007     set_cc_static(s);
4008     return NO_EXIT;
4009 }
4010
4011 static ExitStatus op_csch(DisasContext *s, DisasOps *o)
4012 {
4013     check_privileged(s);
4014     potential_page_fault(s);
4015     gen_helper_csch(cpu_env, regs[1]);
4016     set_cc_static(s);
4017     return NO_EXIT;
4018 }
4019
4020 static ExitStatus op_hsch(DisasContext *s, DisasOps *o)
4021 {
4022     check_privileged(s);
4023     potential_page_fault(s);
4024     gen_helper_hsch(cpu_env, regs[1]);
4025     set_cc_static(s);
4026     return NO_EXIT;
4027 }
4028
4029 static ExitStatus op_msch(DisasContext *s, DisasOps *o)
4030 {
4031     check_privileged(s);
4032     potential_page_fault(s);
4033     gen_helper_msch(cpu_env, regs[1], o->in2);
4034     set_cc_static(s);
4035     return NO_EXIT;
4036 }
4037
4038 static ExitStatus op_rchp(DisasContext *s, DisasOps *o)
4039 {
4040     check_privileged(s);
4041     potential_page_fault(s);
4042     gen_helper_rchp(cpu_env, regs[1]);
4043     set_cc_static(s);
4044     return NO_EXIT;
4045 }
4046
4047 static ExitStatus op_rsch(DisasContext *s, DisasOps *o)
4048 {
4049     check_privileged(s);
4050     potential_page_fault(s);
4051     gen_helper_rsch(cpu_env, regs[1]);
4052     set_cc_static(s);
4053     return NO_EXIT;
4054 }
4055
4056 static ExitStatus op_ssch(DisasContext *s, DisasOps *o)
4057 {
4058     check_privileged(s);
4059     potential_page_fault(s);
4060     gen_helper_ssch(cpu_env, regs[1], o->in2);
4061     set_cc_static(s);
4062     return NO_EXIT;
4063 }
4064
4065 static ExitStatus op_stsch(DisasContext *s, DisasOps *o)
4066 {
4067     check_privileged(s);
4068     potential_page_fault(s);
4069     gen_helper_stsch(cpu_env, regs[1], o->in2);
4070     set_cc_static(s);
4071     return NO_EXIT;
4072 }
4073
4074 static ExitStatus op_tsch(DisasContext *s, DisasOps *o)
4075 {
4076     check_privileged(s);
4077     potential_page_fault(s);
4078     gen_helper_tsch(cpu_env, regs[1], o->in2);
4079     set_cc_static(s);
4080     return NO_EXIT;
4081 }
4082
4083 static ExitStatus op_chsc(DisasContext *s, DisasOps *o)
4084 {
4085     check_privileged(s);
4086     potential_page_fault(s);
4087     gen_helper_chsc(cpu_env, o->in2);
4088     set_cc_static(s);
4089     return NO_EXIT;
4090 }
4091
4092 static ExitStatus op_stpx(DisasContext *s, DisasOps *o)
4093 {
4094     check_privileged(s);
4095     tcg_gen_ld_i64(o->out, cpu_env, offsetof(CPUS390XState, psa));
4096     tcg_gen_andi_i64(o->out, o->out, 0x7fffe000);
4097     return NO_EXIT;
4098 }
4099
4100 static ExitStatus op_stnosm(DisasContext *s, DisasOps *o)
4101 {
4102     uint64_t i2 = get_field(s->fields, i2);
4103     TCGv_i64 t;
4104
4105     check_privileged(s);
4106
4107     /* It is important to do what the instruction name says: STORE THEN.
4108        If we let the output hook perform the store then if we fault and
4109        restart, we'll have the wrong SYSTEM MASK in place.  */
4110     t = tcg_temp_new_i64();
4111     tcg_gen_shri_i64(t, psw_mask, 56);
4112     tcg_gen_qemu_st8(t, o->addr1, get_mem_index(s));
4113     tcg_temp_free_i64(t);
4114
4115     if (s->fields->op == 0xac) {
4116         tcg_gen_andi_i64(psw_mask, psw_mask,
4117                          (i2 << 56) | 0x00ffffffffffffffull);
4118     } else {
4119         tcg_gen_ori_i64(psw_mask, psw_mask, i2 << 56);
4120     }
4121
4122     /* Exit to main loop to reevaluate s390_cpu_exec_interrupt.  */
4123     return EXIT_PC_STALE_NOCHAIN;
4124 }
4125
4126 static ExitStatus op_stura(DisasContext *s, DisasOps *o)
4127 {
4128     check_privileged(s);
4129     potential_page_fault(s);
4130     gen_helper_stura(cpu_env, o->in2, o->in1);
4131     return NO_EXIT;
4132 }
4133
4134 static ExitStatus op_sturg(DisasContext *s, DisasOps *o)
4135 {
4136     check_privileged(s);
4137     potential_page_fault(s);
4138     gen_helper_sturg(cpu_env, o->in2, o->in1);
4139     return NO_EXIT;
4140 }
4141 #endif
4142
4143 static ExitStatus op_stfle(DisasContext *s, DisasOps *o)
4144 {
4145     potential_page_fault(s);
4146     gen_helper_stfle(cc_op, cpu_env, o->in2);
4147     set_cc_static(s);
4148     return NO_EXIT;
4149 }
4150
4151 static ExitStatus op_st8(DisasContext *s, DisasOps *o)
4152 {
4153     tcg_gen_qemu_st8(o->in1, o->in2, get_mem_index(s));
4154     return NO_EXIT;
4155 }
4156
4157 static ExitStatus op_st16(DisasContext *s, DisasOps *o)
4158 {
4159     tcg_gen_qemu_st16(o->in1, o->in2, get_mem_index(s));
4160     return NO_EXIT;
4161 }
4162
4163 static ExitStatus op_st32(DisasContext *s, DisasOps *o)
4164 {
4165     tcg_gen_qemu_st32(o->in1, o->in2, get_mem_index(s));
4166     return NO_EXIT;
4167 }
4168
4169 static ExitStatus op_st64(DisasContext *s, DisasOps *o)
4170 {
4171     tcg_gen_qemu_st64(o->in1, o->in2, get_mem_index(s));
4172     return NO_EXIT;
4173 }
4174
4175 static ExitStatus op_stam(DisasContext *s, DisasOps *o)
4176 {
4177     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
4178     TCGv_i32 r3 = tcg_const_i32(get_field(s->fields, r3));
4179     gen_helper_stam(cpu_env, r1, o->in2, r3);
4180     tcg_temp_free_i32(r1);
4181     tcg_temp_free_i32(r3);
4182     return NO_EXIT;
4183 }
4184
4185 static ExitStatus op_stcm(DisasContext *s, DisasOps *o)
4186 {
4187     int m3 = get_field(s->fields, m3);
4188     int pos, base = s->insn->data;
4189     TCGv_i64 tmp = tcg_temp_new_i64();
4190
4191     pos = base + ctz32(m3) * 8;
4192     switch (m3) {
4193     case 0xf:
4194         /* Effectively a 32-bit store.  */
4195         tcg_gen_shri_i64(tmp, o->in1, pos);
4196         tcg_gen_qemu_st32(tmp, o->in2, get_mem_index(s));
4197         break;
4198
4199     case 0xc:
4200     case 0x6:
4201     case 0x3:
4202         /* Effectively a 16-bit store.  */
4203         tcg_gen_shri_i64(tmp, o->in1, pos);
4204         tcg_gen_qemu_st16(tmp, o->in2, get_mem_index(s));
4205         break;
4206
4207     case 0x8:
4208     case 0x4:
4209     case 0x2:
4210     case 0x1:
4211         /* Effectively an 8-bit store.  */
4212         tcg_gen_shri_i64(tmp, o->in1, pos);
4213         tcg_gen_qemu_st8(tmp, o->in2, get_mem_index(s));
4214         break;
4215
4216     default:
4217         /* This is going to be a sequence of shifts and stores.  */
4218         pos = base + 32 - 8;
4219         while (m3) {
4220             if (m3 & 0x8) {
4221                 tcg_gen_shri_i64(tmp, o->in1, pos);
4222                 tcg_gen_qemu_st8(tmp, o->in2, get_mem_index(s));
4223                 tcg_gen_addi_i64(o->in2, o->in2, 1);
4224             }
4225             m3 = (m3 << 1) & 0xf;
4226             pos -= 8;
4227         }
4228         break;
4229     }
4230     tcg_temp_free_i64(tmp);
4231     return NO_EXIT;
4232 }
4233
4234 static ExitStatus op_stm(DisasContext *s, DisasOps *o)
4235 {
4236     int r1 = get_field(s->fields, r1);
4237     int r3 = get_field(s->fields, r3);
4238     int size = s->insn->data;
4239     TCGv_i64 tsize = tcg_const_i64(size);
4240
4241     while (1) {
4242         if (size == 8) {
4243             tcg_gen_qemu_st64(regs[r1], o->in2, get_mem_index(s));
4244         } else {
4245             tcg_gen_qemu_st32(regs[r1], o->in2, get_mem_index(s));
4246         }
4247         if (r1 == r3) {
4248             break;
4249         }
4250         tcg_gen_add_i64(o->in2, o->in2, tsize);
4251         r1 = (r1 + 1) & 15;
4252     }
4253
4254     tcg_temp_free_i64(tsize);
4255     return NO_EXIT;
4256 }
4257
4258 static ExitStatus op_stmh(DisasContext *s, DisasOps *o)
4259 {
4260     int r1 = get_field(s->fields, r1);
4261     int r3 = get_field(s->fields, r3);
4262     TCGv_i64 t = tcg_temp_new_i64();
4263     TCGv_i64 t4 = tcg_const_i64(4);
4264     TCGv_i64 t32 = tcg_const_i64(32);
4265
4266     while (1) {
4267         tcg_gen_shl_i64(t, regs[r1], t32);
4268         tcg_gen_qemu_st32(t, o->in2, get_mem_index(s));
4269         if (r1 == r3) {
4270             break;
4271         }
4272         tcg_gen_add_i64(o->in2, o->in2, t4);
4273         r1 = (r1 + 1) & 15;
4274     }
4275
4276     tcg_temp_free_i64(t);
4277     tcg_temp_free_i64(t4);
4278     tcg_temp_free_i64(t32);
4279     return NO_EXIT;
4280 }
4281
4282 static ExitStatus op_stpq(DisasContext *s, DisasOps *o)
4283 {
4284     gen_helper_stpq(cpu_env, o->in2, o->out2, o->out);
4285     return NO_EXIT;
4286 }
4287
4288 static ExitStatus op_srst(DisasContext *s, DisasOps *o)
4289 {
4290     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
4291     TCGv_i32 r2 = tcg_const_i32(get_field(s->fields, r2));
4292
4293     gen_helper_srst(cpu_env, r1, r2);
4294
4295     tcg_temp_free_i32(r1);
4296     tcg_temp_free_i32(r2);
4297     set_cc_static(s);
4298     return NO_EXIT;
4299 }
4300
4301 static ExitStatus op_srstu(DisasContext *s, DisasOps *o)
4302 {
4303     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
4304     TCGv_i32 r2 = tcg_const_i32(get_field(s->fields, r2));
4305
4306     gen_helper_srstu(cpu_env, r1, r2);
4307
4308     tcg_temp_free_i32(r1);
4309     tcg_temp_free_i32(r2);
4310     set_cc_static(s);
4311     return NO_EXIT;
4312 }
4313
4314 static ExitStatus op_sub(DisasContext *s, DisasOps *o)
4315 {
4316     tcg_gen_sub_i64(o->out, o->in1, o->in2);
4317     return NO_EXIT;
4318 }
4319
4320 static ExitStatus op_subb(DisasContext *s, DisasOps *o)
4321 {
4322     DisasCompare cmp;
4323     TCGv_i64 borrow;
4324
4325     tcg_gen_sub_i64(o->out, o->in1, o->in2);
4326
4327     /* The !borrow flag is the msb of CC.  Since we want the inverse of
4328        that, we ask for a comparison of CC=0 | CC=1 -> mask of 8 | 4.  */
4329     disas_jcc(s, &cmp, 8 | 4);
4330     borrow = tcg_temp_new_i64();
4331     if (cmp.is_64) {
4332         tcg_gen_setcond_i64(cmp.cond, borrow, cmp.u.s64.a, cmp.u.s64.b);
4333     } else {
4334         TCGv_i32 t = tcg_temp_new_i32();
4335         tcg_gen_setcond_i32(cmp.cond, t, cmp.u.s32.a, cmp.u.s32.b);
4336         tcg_gen_extu_i32_i64(borrow, t);
4337         tcg_temp_free_i32(t);
4338     }
4339     free_compare(&cmp);
4340
4341     tcg_gen_sub_i64(o->out, o->out, borrow);
4342     tcg_temp_free_i64(borrow);
4343     return NO_EXIT;
4344 }
4345
4346 static ExitStatus op_svc(DisasContext *s, DisasOps *o)
4347 {
4348     TCGv_i32 t;
4349
4350     update_psw_addr(s);
4351     update_cc_op(s);
4352
4353     t = tcg_const_i32(get_field(s->fields, i1) & 0xff);
4354     tcg_gen_st_i32(t, cpu_env, offsetof(CPUS390XState, int_svc_code));
4355     tcg_temp_free_i32(t);
4356
4357     t = tcg_const_i32(s->ilen);
4358     tcg_gen_st_i32(t, cpu_env, offsetof(CPUS390XState, int_svc_ilen));
4359     tcg_temp_free_i32(t);
4360
4361     gen_exception(EXCP_SVC);
4362     return EXIT_NORETURN;
4363 }
4364
4365 static ExitStatus op_tam(DisasContext *s, DisasOps *o)
4366 {
4367     int cc = 0;
4368
4369     cc |= (s->tb->flags & FLAG_MASK_64) ? 2 : 0;
4370     cc |= (s->tb->flags & FLAG_MASK_32) ? 1 : 0;
4371     gen_op_movi_cc(s, cc);
4372     return NO_EXIT;
4373 }
4374
4375 static ExitStatus op_tceb(DisasContext *s, DisasOps *o)
4376 {
4377     gen_helper_tceb(cc_op, cpu_env, o->in1, o->in2);
4378     set_cc_static(s);
4379     return NO_EXIT;
4380 }
4381
4382 static ExitStatus op_tcdb(DisasContext *s, DisasOps *o)
4383 {
4384     gen_helper_tcdb(cc_op, cpu_env, o->in1, o->in2);
4385     set_cc_static(s);
4386     return NO_EXIT;
4387 }
4388
4389 static ExitStatus op_tcxb(DisasContext *s, DisasOps *o)
4390 {
4391     gen_helper_tcxb(cc_op, cpu_env, o->out, o->out2, o->in2);
4392     set_cc_static(s);
4393     return NO_EXIT;
4394 }
4395
4396 #ifndef CONFIG_USER_ONLY
4397
4398 static ExitStatus op_testblock(DisasContext *s, DisasOps *o)
4399 {
4400     check_privileged(s);
4401     gen_helper_testblock(cc_op, cpu_env, o->in2);
4402     set_cc_static(s);
4403     return NO_EXIT;
4404 }
4405
4406 static ExitStatus op_tprot(DisasContext *s, DisasOps *o)
4407 {
4408     gen_helper_tprot(cc_op, o->addr1, o->in2);
4409     set_cc_static(s);
4410     return NO_EXIT;
4411 }
4412
4413 #endif
4414
4415 static ExitStatus op_tp(DisasContext *s, DisasOps *o)
4416 {
4417     TCGv_i32 l1 = tcg_const_i32(get_field(s->fields, l1) + 1);
4418     gen_helper_tp(cc_op, cpu_env, o->addr1, l1);
4419     tcg_temp_free_i32(l1);
4420     set_cc_static(s);
4421     return NO_EXIT;
4422 }
4423
4424 static ExitStatus op_tr(DisasContext *s, DisasOps *o)
4425 {
4426     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
4427     gen_helper_tr(cpu_env, l, o->addr1, o->in2);
4428     tcg_temp_free_i32(l);
4429     set_cc_static(s);
4430     return NO_EXIT;
4431 }
4432
4433 static ExitStatus op_tre(DisasContext *s, DisasOps *o)
4434 {
4435     gen_helper_tre(o->out, cpu_env, o->out, o->out2, o->in2);
4436     return_low128(o->out2);
4437     set_cc_static(s);
4438     return NO_EXIT;
4439 }
4440
4441 static ExitStatus op_trt(DisasContext *s, DisasOps *o)
4442 {
4443     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
4444     gen_helper_trt(cc_op, cpu_env, l, o->addr1, o->in2);
4445     tcg_temp_free_i32(l);
4446     set_cc_static(s);
4447     return NO_EXIT;
4448 }
4449
4450 static ExitStatus op_trtr(DisasContext *s, DisasOps *o)
4451 {
4452     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
4453     gen_helper_trtr(cc_op, cpu_env, l, o->addr1, o->in2);
4454     tcg_temp_free_i32(l);
4455     set_cc_static(s);
4456     return NO_EXIT;
4457 }
4458
4459 static ExitStatus op_trXX(DisasContext *s, DisasOps *o)
4460 {
4461     TCGv_i32 r1 = tcg_const_i32(get_field(s->fields, r1));
4462     TCGv_i32 r2 = tcg_const_i32(get_field(s->fields, r2));
4463     TCGv_i32 sizes = tcg_const_i32(s->insn->opc & 3);
4464     TCGv_i32 tst = tcg_temp_new_i32();
4465     int m3 = get_field(s->fields, m3);
4466
4467     if (!s390_has_feat(S390_FEAT_ETF2_ENH)) {
4468         m3 = 0;
4469     }
4470     if (m3 & 1) {
4471         tcg_gen_movi_i32(tst, -1);
4472     } else {
4473         tcg_gen_extrl_i64_i32(tst, regs[0]);
4474         if (s->insn->opc & 3) {
4475             tcg_gen_ext8u_i32(tst, tst);
4476         } else {
4477             tcg_gen_ext16u_i32(tst, tst);
4478         }
4479     }
4480     gen_helper_trXX(cc_op, cpu_env, r1, r2, tst, sizes);
4481
4482     tcg_temp_free_i32(r1);
4483     tcg_temp_free_i32(r2);
4484     tcg_temp_free_i32(sizes);
4485     tcg_temp_free_i32(tst);
4486     set_cc_static(s);
4487     return NO_EXIT;
4488 }
4489
4490 static ExitStatus op_ts(DisasContext *s, DisasOps *o)
4491 {
4492     TCGv_i32 t1 = tcg_const_i32(0xff);
4493     tcg_gen_atomic_xchg_i32(t1, o->in2, t1, get_mem_index(s), MO_UB);
4494     tcg_gen_extract_i32(cc_op, t1, 7, 1);
4495     tcg_temp_free_i32(t1);
4496     set_cc_static(s);
4497     return NO_EXIT;
4498 }
4499
4500 static ExitStatus op_unpk(DisasContext *s, DisasOps *o)
4501 {
4502     TCGv_i32 l = tcg_const_i32(get_field(s->fields, l1));
4503     gen_helper_unpk(cpu_env, l, o->addr1, o->in2);
4504     tcg_temp_free_i32(l);
4505     return NO_EXIT;
4506 }
4507
4508 static ExitStatus op_unpka(DisasContext *s, DisasOps *o)
4509 {
4510     int l1 = get_field(s->fields, l1) + 1;
4511     TCGv_i32 l;
4512
4513     /* The length must not exceed 32 bytes.  */
4514     if (l1 > 32) {
4515         gen_program_exception(s, PGM_SPECIFICATION);
4516         return EXIT_NORETURN;
4517     }
4518     l = tcg_const_i32(l1);
4519     gen_helper_unpka(cc_op, cpu_env, o->addr1, l, o->in2);
4520     tcg_temp_free_i32(l);
4521     set_cc_static(s);
4522     return NO_EXIT;
4523 }
4524
4525 static ExitStatus op_unpku(DisasContext *s, DisasOps *o)
4526 {
4527     int l1 = get_field(s->fields, l1) + 1;
4528     TCGv_i32 l;
4529
4530     /* The length must be even and should not exceed 64 bytes.  */
4531     if ((l1 & 1) || (l1 > 64)) {
4532         gen_program_exception(s, PGM_SPECIFICATION);
4533         return EXIT_NORETURN;
4534     }
4535     l = tcg_const_i32(l1);
4536     gen_helper_unpku(cc_op, cpu_env, o->addr1, l, o->in2);
4537     tcg_temp_free_i32(l);
4538     set_cc_static(s);
4539     return NO_EXIT;
4540 }
4541
4542
4543 static ExitStatus op_xc(DisasContext *s, DisasOps *o)
4544 {
4545     int d1 = get_field(s->fields, d1);
4546     int d2 = get_field(s->fields, d2);
4547     int b1 = get_field(s->fields, b1);
4548     int b2 = get_field(s->fields, b2);
4549     int l = get_field(s->fields, l1);
4550     TCGv_i32 t32;
4551
4552     o->addr1 = get_address(s, 0, b1, d1);
4553
4554     /* If the addresses are identical, this is a store/memset of zero.  */
4555     if (b1 == b2 && d1 == d2 && (l + 1) <= 32) {
4556         o->in2 = tcg_const_i64(0);
4557
4558         l++;
4559         while (l >= 8) {
4560             tcg_gen_qemu_st64(o->in2, o->addr1, get_mem_index(s));
4561             l -= 8;
4562             if (l > 0) {
4563                 tcg_gen_addi_i64(o->addr1, o->addr1, 8);
4564             }
4565         }
4566         if (l >= 4) {
4567             tcg_gen_qemu_st32(o->in2, o->addr1, get_mem_index(s));
4568             l -= 4;
4569             if (l > 0) {
4570                 tcg_gen_addi_i64(o->addr1, o->addr1, 4);
4571             }
4572         }
4573         if (l >= 2) {
4574             tcg_gen_qemu_st16(o->in2, o->addr1, get_mem_index(s));
4575             l -= 2;
4576             if (l > 0) {
4577                 tcg_gen_addi_i64(o->addr1, o->addr1, 2);
4578             }
4579         }
4580         if (l) {
4581             tcg_gen_qemu_st8(o->in2, o->addr1, get_mem_index(s));
4582         }
4583         gen_op_movi_cc(s, 0);
4584         return NO_EXIT;
4585     }
4586
4587     /* But in general we'll defer to a helper.  */
4588     o->in2 = get_address(s, 0, b2, d2);
4589     t32 = tcg_const_i32(l);
4590     gen_helper_xc(cc_op, cpu_env, t32, o->addr1, o->in2);
4591     tcg_temp_free_i32(t32);
4592     set_cc_static(s);
4593     return NO_EXIT;
4594 }
4595
4596 static ExitStatus op_xor(DisasContext *s, DisasOps *o)
4597 {
4598     tcg_gen_xor_i64(o->out, o->in1, o->in2);
4599     return NO_EXIT;
4600 }
4601
4602 static ExitStatus op_xori(DisasContext *s, DisasOps *o)
4603 {
4604     int shift = s->insn->data & 0xff;
4605     int size = s->insn->data >> 8;
4606     uint64_t mask = ((1ull << size) - 1) << shift;
4607
4608     assert(!o->g_in2);
4609     tcg_gen_shli_i64(o->in2, o->in2, shift);
4610     tcg_gen_xor_i64(o->out, o->in1, o->in2);
4611
4612     /* Produce the CC from only the bits manipulated.  */
4613     tcg_gen_andi_i64(cc_dst, o->out, mask);
4614     set_cc_nz_u64(s, cc_dst);
4615     return NO_EXIT;
4616 }
4617
4618 static ExitStatus op_zero(DisasContext *s, DisasOps *o)
4619 {
4620     o->out = tcg_const_i64(0);
4621     return NO_EXIT;
4622 }
4623
4624 static ExitStatus op_zero2(DisasContext *s, DisasOps *o)
4625 {
4626     o->out = tcg_const_i64(0);
4627     o->out2 = o->out;
4628     o->g_out2 = true;
4629     return NO_EXIT;
4630 }
4631
4632 /* ====================================================================== */
4633 /* The "Cc OUTput" generators.  Given the generated output (and in some cases
4634    the original inputs), update the various cc data structures in order to
4635    be able to compute the new condition code.  */
4636
4637 static void cout_abs32(DisasContext *s, DisasOps *o)
4638 {
4639     gen_op_update1_cc_i64(s, CC_OP_ABS_32, o->out);
4640 }
4641
4642 static void cout_abs64(DisasContext *s, DisasOps *o)
4643 {
4644     gen_op_update1_cc_i64(s, CC_OP_ABS_64, o->out);
4645 }
4646
4647 static void cout_adds32(DisasContext *s, DisasOps *o)
4648 {
4649     gen_op_update3_cc_i64(s, CC_OP_ADD_32, o->in1, o->in2, o->out);
4650 }
4651
4652 static void cout_adds64(DisasContext *s, DisasOps *o)
4653 {
4654     gen_op_update3_cc_i64(s, CC_OP_ADD_64, o->in1, o->in2, o->out);
4655 }
4656
4657 static void cout_addu32(DisasContext *s, DisasOps *o)
4658 {
4659     gen_op_update3_cc_i64(s, CC_OP_ADDU_32, o->in1, o->in2, o->out);
4660 }
4661
4662 static void cout_addu64(DisasContext *s, DisasOps *o)
4663 {
4664     gen_op_update3_cc_i64(s, CC_OP_ADDU_64, o->in1, o->in2, o->out);
4665 }
4666
4667 static void cout_addc32(DisasContext *s, DisasOps *o)
4668 {
4669     gen_op_update3_cc_i64(s, CC_OP_ADDC_32, o->in1, o->in2, o->out);
4670 }
4671
4672 static void cout_addc64(DisasContext *s, DisasOps *o)
4673 {
4674     gen_op_update3_cc_i64(s, CC_OP_ADDC_64, o->in1, o->in2, o->out);
4675 }
4676
4677 static void cout_cmps32(DisasContext *s, DisasOps *o)
4678 {
4679     gen_op_update2_cc_i64(s, CC_OP_LTGT_32, o->in1, o->in2);
4680 }
4681
4682 static void cout_cmps64(DisasContext *s, DisasOps *o)
4683 {
4684     gen_op_update2_cc_i64(s, CC_OP_LTGT_64, o->in1, o->in2);
4685 }
4686
4687 static void cout_cmpu32(DisasContext *s, DisasOps *o)
4688 {
4689     gen_op_update2_cc_i64(s, CC_OP_LTUGTU_32, o->in1, o->in2);
4690 }
4691
4692 static void cout_cmpu64(DisasContext *s, DisasOps *o)
4693 {
4694     gen_op_update2_cc_i64(s, CC_OP_LTUGTU_64, o->in1, o->in2);
4695 }
4696
4697 static void cout_f32(DisasContext *s, DisasOps *o)
4698 {
4699     gen_op_update1_cc_i64(s, CC_OP_NZ_F32, o->out);
4700 }
4701
4702 static void cout_f64(DisasContext *s, DisasOps *o)
4703 {
4704     gen_op_update1_cc_i64(s, CC_OP_NZ_F64, o->out);
4705 }
4706
4707 static void cout_f128(DisasContext *s, DisasOps *o)
4708 {
4709     gen_op_update2_cc_i64(s, CC_OP_NZ_F128, o->out, o->out2);
4710 }
4711
4712 static void cout_nabs32(DisasContext *s, DisasOps *o)
4713 {
4714     gen_op_update1_cc_i64(s, CC_OP_NABS_32, o->out);
4715 }
4716
4717 static void cout_nabs64(DisasContext *s, DisasOps *o)
4718 {
4719     gen_op_update1_cc_i64(s, CC_OP_NABS_64, o->out);
4720 }
4721
4722 static void cout_neg32(DisasContext *s, DisasOps *o)
4723 {
4724     gen_op_update1_cc_i64(s, CC_OP_COMP_32, o->out);
4725 }
4726
4727 static void cout_neg64(DisasContext *s, DisasOps *o)
4728 {
4729     gen_op_update1_cc_i64(s, CC_OP_COMP_64, o->out);
4730 }
4731
4732 static void cout_nz32(DisasContext *s, DisasOps *o)
4733 {
4734     tcg_gen_ext32u_i64(cc_dst, o->out);
4735     gen_op_update1_cc_i64(s, CC_OP_NZ, cc_dst);
4736 }
4737
4738 static void cout_nz64(DisasContext *s, DisasOps *o)
4739 {
4740     gen_op_update1_cc_i64(s, CC_OP_NZ, o->out);
4741 }
4742
4743 static void cout_s32(DisasContext *s, DisasOps *o)
4744 {
4745     gen_op_update1_cc_i64(s, CC_OP_LTGT0_32, o->out);
4746 }
4747
4748 static void cout_s64(DisasContext *s, DisasOps *o)
4749 {
4750     gen_op_update1_cc_i64(s, CC_OP_LTGT0_64, o->out);
4751 }
4752
4753 static void cout_subs32(DisasContext *s, DisasOps *o)
4754 {
4755     gen_op_update3_cc_i64(s, CC_OP_SUB_32, o->in1, o->in2, o->out);
4756 }
4757
4758 static void cout_subs64(DisasContext *s, DisasOps *o)
4759 {
4760     gen_op_update3_cc_i64(s, CC_OP_SUB_64, o->in1, o->in2, o->out);
4761 }
4762
4763 static void cout_subu32(DisasContext *s, DisasOps *o)
4764 {
4765     gen_op_update3_cc_i64(s, CC_OP_SUBU_32, o->in1, o->in2, o->out);
4766 }
4767
4768 static void cout_subu64(DisasContext *s, DisasOps *o)
4769 {
4770     gen_op_update3_cc_i64(s, CC_OP_SUBU_64, o->in1, o->in2, o->out);
4771 }
4772
4773 static void cout_subb32(DisasContext *s, DisasOps *o)
4774 {
4775     gen_op_update3_cc_i64(s, CC_OP_SUBB_32, o->in1, o->in2, o->out);
4776 }
4777
4778 static void cout_subb64(DisasContext *s, DisasOps *o)
4779 {
4780     gen_op_update3_cc_i64(s, CC_OP_SUBB_64, o->in1, o->in2, o->out);
4781 }
4782
4783 static void cout_tm32(DisasContext *s, DisasOps *o)
4784 {
4785     gen_op_update2_cc_i64(s, CC_OP_TM_32, o->in1, o->in2);
4786 }
4787
4788 static void cout_tm64(DisasContext *s, DisasOps *o)
4789 {
4790     gen_op_update2_cc_i64(s, CC_OP_TM_64, o->in1, o->in2);
4791 }
4792
4793 /* ====================================================================== */
4794 /* The "PREParation" generators.  These initialize the DisasOps.OUT fields
4795    with the TCG register to which we will write.  Used in combination with
4796    the "wout" generators, in some cases we need a new temporary, and in
4797    some cases we can write to a TCG global.  */
4798
4799 static void prep_new(DisasContext *s, DisasFields *f, DisasOps *o)
4800 {
4801     o->out = tcg_temp_new_i64();
4802 }
4803 #define SPEC_prep_new 0
4804
4805 static void prep_new_P(DisasContext *s, DisasFields *f, DisasOps *o)
4806 {
4807     o->out = tcg_temp_new_i64();
4808     o->out2 = tcg_temp_new_i64();
4809 }
4810 #define SPEC_prep_new_P 0
4811
4812 static void prep_r1(DisasContext *s, DisasFields *f, DisasOps *o)
4813 {
4814     o->out = regs[get_field(f, r1)];
4815     o->g_out = true;
4816 }
4817 #define SPEC_prep_r1 0
4818
4819 static void prep_r1_P(DisasContext *s, DisasFields *f, DisasOps *o)
4820 {
4821     int r1 = get_field(f, r1);
4822     o->out = regs[r1];
4823     o->out2 = regs[r1 + 1];
4824     o->g_out = o->g_out2 = true;
4825 }
4826 #define SPEC_prep_r1_P SPEC_r1_even
4827
4828 static void prep_f1(DisasContext *s, DisasFields *f, DisasOps *o)
4829 {
4830     o->out = fregs[get_field(f, r1)];
4831     o->g_out = true;
4832 }
4833 #define SPEC_prep_f1 0
4834
4835 static void prep_x1(DisasContext *s, DisasFields *f, DisasOps *o)
4836 {
4837     int r1 = get_field(f, r1);
4838     o->out = fregs[r1];
4839     o->out2 = fregs[r1 + 2];
4840     o->g_out = o->g_out2 = true;
4841 }
4842 #define SPEC_prep_x1 SPEC_r1_f128
4843
4844 /* ====================================================================== */
4845 /* The "Write OUTput" generators.  These generally perform some non-trivial
4846    copy of data to TCG globals, or to main memory.  The trivial cases are
4847    generally handled by having a "prep" generator install the TCG global
4848    as the destination of the operation.  */
4849
4850 static void wout_r1(DisasContext *s, DisasFields *f, DisasOps *o)
4851 {
4852     store_reg(get_field(f, r1), o->out);
4853 }
4854 #define SPEC_wout_r1 0
4855
4856 static void wout_r1_8(DisasContext *s, DisasFields *f, DisasOps *o)
4857 {
4858     int r1 = get_field(f, r1);
4859     tcg_gen_deposit_i64(regs[r1], regs[r1], o->out, 0, 8);
4860 }
4861 #define SPEC_wout_r1_8 0
4862
4863 static void wout_r1_16(DisasContext *s, DisasFields *f, DisasOps *o)
4864 {
4865     int r1 = get_field(f, r1);
4866     tcg_gen_deposit_i64(regs[r1], regs[r1], o->out, 0, 16);
4867 }
4868 #define SPEC_wout_r1_16 0
4869
4870 static void wout_r1_32(DisasContext *s, DisasFields *f, DisasOps *o)
4871 {
4872     store_reg32_i64(get_field(f, r1), o->out);
4873 }
4874 #define SPEC_wout_r1_32 0
4875
4876 static void wout_r1_32h(DisasContext *s, DisasFields *f, DisasOps *o)
4877 {
4878     store_reg32h_i64(get_field(f, r1), o->out);
4879 }
4880 #define SPEC_wout_r1_32h 0
4881
4882 static void wout_r1_P32(DisasContext *s, DisasFields *f, DisasOps *o)
4883 {
4884     int r1 = get_field(f, r1);
4885     store_reg32_i64(r1, o->out);
4886     store_reg32_i64(r1 + 1, o->out2);
4887 }
4888 #define SPEC_wout_r1_P32 SPEC_r1_even
4889
4890 static void wout_r1_D32(DisasContext *s, DisasFields *f, DisasOps *o)
4891 {
4892     int r1 = get_field(f, r1);
4893     store_reg32_i64(r1 + 1, o->out);
4894     tcg_gen_shri_i64(o->out, o->out, 32);
4895     store_reg32_i64(r1, o->out);
4896 }
4897 #define SPEC_wout_r1_D32 SPEC_r1_even
4898
4899 static void wout_r3_P32(DisasContext *s, DisasFields *f, DisasOps *o)
4900 {
4901     int r3 = get_field(f, r3);
4902     store_reg32_i64(r3, o->out);
4903     store_reg32_i64(r3 + 1, o->out2);
4904 }
4905 #define SPEC_wout_r3_P32 SPEC_r3_even
4906
4907 static void wout_r3_P64(DisasContext *s, DisasFields *f, DisasOps *o)
4908 {
4909     int r3 = get_field(f, r3);
4910     store_reg(r3, o->out);
4911     store_reg(r3 + 1, o->out2);
4912 }
4913 #define SPEC_wout_r3_P64 SPEC_r3_even
4914
4915 static void wout_e1(DisasContext *s, DisasFields *f, DisasOps *o)
4916 {
4917     store_freg32_i64(get_field(f, r1), o->out);
4918 }
4919 #define SPEC_wout_e1 0
4920
4921 static void wout_f1(DisasContext *s, DisasFields *f, DisasOps *o)
4922 {
4923     store_freg(get_field(f, r1), o->out);
4924 }
4925 #define SPEC_wout_f1 0
4926
4927 static void wout_x1(DisasContext *s, DisasFields *f, DisasOps *o)
4928 {
4929     int f1 = get_field(s->fields, r1);
4930     store_freg(f1, o->out);
4931     store_freg(f1 + 2, o->out2);
4932 }
4933 #define SPEC_wout_x1 SPEC_r1_f128
4934
4935 static void wout_cond_r1r2_32(DisasContext *s, DisasFields *f, DisasOps *o)
4936 {
4937     if (get_field(f, r1) != get_field(f, r2)) {
4938         store_reg32_i64(get_field(f, r1), o->out);
4939     }
4940 }
4941 #define SPEC_wout_cond_r1r2_32 0
4942
4943 static void wout_cond_e1e2(DisasContext *s, DisasFields *f, DisasOps *o)
4944 {
4945     if (get_field(f, r1) != get_field(f, r2)) {
4946         store_freg32_i64(get_field(f, r1), o->out);
4947     }
4948 }
4949 #define SPEC_wout_cond_e1e2 0
4950
4951 static void wout_m1_8(DisasContext *s, DisasFields *f, DisasOps *o)
4952 {
4953     tcg_gen_qemu_st8(o->out, o->addr1, get_mem_index(s));
4954 }
4955 #define SPEC_wout_m1_8 0
4956
4957 static void wout_m1_16(DisasContext *s, DisasFields *f, DisasOps *o)
4958 {
4959     tcg_gen_qemu_st16(o->out, o->addr1, get_mem_index(s));
4960 }
4961 #define SPEC_wout_m1_16 0
4962
4963 static void wout_m1_32(DisasContext *s, DisasFields *f, DisasOps *o)
4964 {
4965     tcg_gen_qemu_st32(o->out, o->addr1, get_mem_index(s));
4966 }
4967 #define SPEC_wout_m1_32 0
4968
4969 static void wout_m1_64(DisasContext *s, DisasFields *f, DisasOps *o)
4970 {
4971     tcg_gen_qemu_st64(o->out, o->addr1, get_mem_index(s));
4972 }
4973 #define SPEC_wout_m1_64 0
4974
4975 static void wout_m2_32(DisasContext *s, DisasFields *f, DisasOps *o)
4976 {
4977     tcg_gen_qemu_st32(o->out, o->in2, get_mem_index(s));
4978 }
4979 #define SPEC_wout_m2_32 0
4980
4981 static void wout_in2_r1(DisasContext *s, DisasFields *f, DisasOps *o)
4982 {
4983     store_reg(get_field(f, r1), o->in2);
4984 }
4985 #define SPEC_wout_in2_r1 0
4986
4987 static void wout_in2_r1_32(DisasContext *s, DisasFields *f, DisasOps *o)
4988 {
4989     store_reg32_i64(get_field(f, r1), o->in2);
4990 }
4991 #define SPEC_wout_in2_r1_32 0
4992
4993 /* ====================================================================== */
4994 /* The "INput 1" generators.  These load the first operand to an insn.  */
4995
4996 static void in1_r1(DisasContext *s, DisasFields *f, DisasOps *o)
4997 {
4998     o->in1 = load_reg(get_field(f, r1));
4999 }
5000 #define SPEC_in1_r1 0
5001
5002 static void in1_r1_o(DisasContext *s, DisasFields *f, DisasOps *o)
5003 {
5004     o->in1 = regs[get_field(f, r1)];
5005     o->g_in1 = true;
5006 }
5007 #define SPEC_in1_r1_o 0
5008
5009 static void in1_r1_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5010 {
5011     o->in1 = tcg_temp_new_i64();
5012     tcg_gen_ext32s_i64(o->in1, regs[get_field(f, r1)]);
5013 }
5014 #define SPEC_in1_r1_32s 0
5015
5016 static void in1_r1_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5017 {
5018     o->in1 = tcg_temp_new_i64();
5019     tcg_gen_ext32u_i64(o->in1, regs[get_field(f, r1)]);
5020 }
5021 #define SPEC_in1_r1_32u 0
5022
5023 static void in1_r1_sr32(DisasContext *s, DisasFields *f, DisasOps *o)
5024 {
5025     o->in1 = tcg_temp_new_i64();
5026     tcg_gen_shri_i64(o->in1, regs[get_field(f, r1)], 32);
5027 }
5028 #define SPEC_in1_r1_sr32 0
5029
5030 static void in1_r1p1(DisasContext *s, DisasFields *f, DisasOps *o)
5031 {
5032     o->in1 = load_reg(get_field(f, r1) + 1);
5033 }
5034 #define SPEC_in1_r1p1 SPEC_r1_even
5035
5036 static void in1_r1p1_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5037 {
5038     o->in1 = tcg_temp_new_i64();
5039     tcg_gen_ext32s_i64(o->in1, regs[get_field(f, r1) + 1]);
5040 }
5041 #define SPEC_in1_r1p1_32s SPEC_r1_even
5042
5043 static void in1_r1p1_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5044 {
5045     o->in1 = tcg_temp_new_i64();
5046     tcg_gen_ext32u_i64(o->in1, regs[get_field(f, r1) + 1]);
5047 }
5048 #define SPEC_in1_r1p1_32u SPEC_r1_even
5049
5050 static void in1_r1_D32(DisasContext *s, DisasFields *f, DisasOps *o)
5051 {
5052     int r1 = get_field(f, r1);
5053     o->in1 = tcg_temp_new_i64();
5054     tcg_gen_concat32_i64(o->in1, regs[r1 + 1], regs[r1]);
5055 }
5056 #define SPEC_in1_r1_D32 SPEC_r1_even
5057
5058 static void in1_r2(DisasContext *s, DisasFields *f, DisasOps *o)
5059 {
5060     o->in1 = load_reg(get_field(f, r2));
5061 }
5062 #define SPEC_in1_r2 0
5063
5064 static void in1_r2_sr32(DisasContext *s, DisasFields *f, DisasOps *o)
5065 {
5066     o->in1 = tcg_temp_new_i64();
5067     tcg_gen_shri_i64(o->in1, regs[get_field(f, r2)], 32);
5068 }
5069 #define SPEC_in1_r2_sr32 0
5070
5071 static void in1_r3(DisasContext *s, DisasFields *f, DisasOps *o)
5072 {
5073     o->in1 = load_reg(get_field(f, r3));
5074 }
5075 #define SPEC_in1_r3 0
5076
5077 static void in1_r3_o(DisasContext *s, DisasFields *f, DisasOps *o)
5078 {
5079     o->in1 = regs[get_field(f, r3)];
5080     o->g_in1 = true;
5081 }
5082 #define SPEC_in1_r3_o 0
5083
5084 static void in1_r3_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5085 {
5086     o->in1 = tcg_temp_new_i64();
5087     tcg_gen_ext32s_i64(o->in1, regs[get_field(f, r3)]);
5088 }
5089 #define SPEC_in1_r3_32s 0
5090
5091 static void in1_r3_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5092 {
5093     o->in1 = tcg_temp_new_i64();
5094     tcg_gen_ext32u_i64(o->in1, regs[get_field(f, r3)]);
5095 }
5096 #define SPEC_in1_r3_32u 0
5097
5098 static void in1_r3_D32(DisasContext *s, DisasFields *f, DisasOps *o)
5099 {
5100     int r3 = get_field(f, r3);
5101     o->in1 = tcg_temp_new_i64();
5102     tcg_gen_concat32_i64(o->in1, regs[r3 + 1], regs[r3]);
5103 }
5104 #define SPEC_in1_r3_D32 SPEC_r3_even
5105
5106 static void in1_e1(DisasContext *s, DisasFields *f, DisasOps *o)
5107 {
5108     o->in1 = load_freg32_i64(get_field(f, r1));
5109 }
5110 #define SPEC_in1_e1 0
5111
5112 static void in1_f1_o(DisasContext *s, DisasFields *f, DisasOps *o)
5113 {
5114     o->in1 = fregs[get_field(f, r1)];
5115     o->g_in1 = true;
5116 }
5117 #define SPEC_in1_f1_o 0
5118
5119 static void in1_x1_o(DisasContext *s, DisasFields *f, DisasOps *o)
5120 {
5121     int r1 = get_field(f, r1);
5122     o->out = fregs[r1];
5123     o->out2 = fregs[r1 + 2];
5124     o->g_out = o->g_out2 = true;
5125 }
5126 #define SPEC_in1_x1_o SPEC_r1_f128
5127
5128 static void in1_f3_o(DisasContext *s, DisasFields *f, DisasOps *o)
5129 {
5130     o->in1 = fregs[get_field(f, r3)];
5131     o->g_in1 = true;
5132 }
5133 #define SPEC_in1_f3_o 0
5134
5135 static void in1_la1(DisasContext *s, DisasFields *f, DisasOps *o)
5136 {
5137     o->addr1 = get_address(s, 0, get_field(f, b1), get_field(f, d1));
5138 }
5139 #define SPEC_in1_la1 0
5140
5141 static void in1_la2(DisasContext *s, DisasFields *f, DisasOps *o)
5142 {
5143     int x2 = have_field(f, x2) ? get_field(f, x2) : 0;
5144     o->addr1 = get_address(s, x2, get_field(f, b2), get_field(f, d2));
5145 }
5146 #define SPEC_in1_la2 0
5147
5148 static void in1_m1_8u(DisasContext *s, DisasFields *f, DisasOps *o)
5149 {
5150     in1_la1(s, f, o);
5151     o->in1 = tcg_temp_new_i64();
5152     tcg_gen_qemu_ld8u(o->in1, o->addr1, get_mem_index(s));
5153 }
5154 #define SPEC_in1_m1_8u 0
5155
5156 static void in1_m1_16s(DisasContext *s, DisasFields *f, DisasOps *o)
5157 {
5158     in1_la1(s, f, o);
5159     o->in1 = tcg_temp_new_i64();
5160     tcg_gen_qemu_ld16s(o->in1, o->addr1, get_mem_index(s));
5161 }
5162 #define SPEC_in1_m1_16s 0
5163
5164 static void in1_m1_16u(DisasContext *s, DisasFields *f, DisasOps *o)
5165 {
5166     in1_la1(s, f, o);
5167     o->in1 = tcg_temp_new_i64();
5168     tcg_gen_qemu_ld16u(o->in1, o->addr1, get_mem_index(s));
5169 }
5170 #define SPEC_in1_m1_16u 0
5171
5172 static void in1_m1_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5173 {
5174     in1_la1(s, f, o);
5175     o->in1 = tcg_temp_new_i64();
5176     tcg_gen_qemu_ld32s(o->in1, o->addr1, get_mem_index(s));
5177 }
5178 #define SPEC_in1_m1_32s 0
5179
5180 static void in1_m1_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5181 {
5182     in1_la1(s, f, o);
5183     o->in1 = tcg_temp_new_i64();
5184     tcg_gen_qemu_ld32u(o->in1, o->addr1, get_mem_index(s));
5185 }
5186 #define SPEC_in1_m1_32u 0
5187
5188 static void in1_m1_64(DisasContext *s, DisasFields *f, DisasOps *o)
5189 {
5190     in1_la1(s, f, o);
5191     o->in1 = tcg_temp_new_i64();
5192     tcg_gen_qemu_ld64(o->in1, o->addr1, get_mem_index(s));
5193 }
5194 #define SPEC_in1_m1_64 0
5195
5196 /* ====================================================================== */
5197 /* The "INput 2" generators.  These load the second operand to an insn.  */
5198
5199 static void in2_r1_o(DisasContext *s, DisasFields *f, DisasOps *o)
5200 {
5201     o->in2 = regs[get_field(f, r1)];
5202     o->g_in2 = true;
5203 }
5204 #define SPEC_in2_r1_o 0
5205
5206 static void in2_r1_16u(DisasContext *s, DisasFields *f, DisasOps *o)
5207 {
5208     o->in2 = tcg_temp_new_i64();
5209     tcg_gen_ext16u_i64(o->in2, regs[get_field(f, r1)]);
5210 }
5211 #define SPEC_in2_r1_16u 0
5212
5213 static void in2_r1_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5214 {
5215     o->in2 = tcg_temp_new_i64();
5216     tcg_gen_ext32u_i64(o->in2, regs[get_field(f, r1)]);
5217 }
5218 #define SPEC_in2_r1_32u 0
5219
5220 static void in2_r1_D32(DisasContext *s, DisasFields *f, DisasOps *o)
5221 {
5222     int r1 = get_field(f, r1);
5223     o->in2 = tcg_temp_new_i64();
5224     tcg_gen_concat32_i64(o->in2, regs[r1 + 1], regs[r1]);
5225 }
5226 #define SPEC_in2_r1_D32 SPEC_r1_even
5227
5228 static void in2_r2(DisasContext *s, DisasFields *f, DisasOps *o)
5229 {
5230     o->in2 = load_reg(get_field(f, r2));
5231 }
5232 #define SPEC_in2_r2 0
5233
5234 static void in2_r2_o(DisasContext *s, DisasFields *f, DisasOps *o)
5235 {
5236     o->in2 = regs[get_field(f, r2)];
5237     o->g_in2 = true;
5238 }
5239 #define SPEC_in2_r2_o 0
5240
5241 static void in2_r2_nz(DisasContext *s, DisasFields *f, DisasOps *o)
5242 {
5243     int r2 = get_field(f, r2);
5244     if (r2 != 0) {
5245         o->in2 = load_reg(r2);
5246     }
5247 }
5248 #define SPEC_in2_r2_nz 0
5249
5250 static void in2_r2_8s(DisasContext *s, DisasFields *f, DisasOps *o)
5251 {
5252     o->in2 = tcg_temp_new_i64();
5253     tcg_gen_ext8s_i64(o->in2, regs[get_field(f, r2)]);
5254 }
5255 #define SPEC_in2_r2_8s 0
5256
5257 static void in2_r2_8u(DisasContext *s, DisasFields *f, DisasOps *o)
5258 {
5259     o->in2 = tcg_temp_new_i64();
5260     tcg_gen_ext8u_i64(o->in2, regs[get_field(f, r2)]);
5261 }
5262 #define SPEC_in2_r2_8u 0
5263
5264 static void in2_r2_16s(DisasContext *s, DisasFields *f, DisasOps *o)
5265 {
5266     o->in2 = tcg_temp_new_i64();
5267     tcg_gen_ext16s_i64(o->in2, regs[get_field(f, r2)]);
5268 }
5269 #define SPEC_in2_r2_16s 0
5270
5271 static void in2_r2_16u(DisasContext *s, DisasFields *f, DisasOps *o)
5272 {
5273     o->in2 = tcg_temp_new_i64();
5274     tcg_gen_ext16u_i64(o->in2, regs[get_field(f, r2)]);
5275 }
5276 #define SPEC_in2_r2_16u 0
5277
5278 static void in2_r3(DisasContext *s, DisasFields *f, DisasOps *o)
5279 {
5280     o->in2 = load_reg(get_field(f, r3));
5281 }
5282 #define SPEC_in2_r3 0
5283
5284 static void in2_r3_sr32(DisasContext *s, DisasFields *f, DisasOps *o)
5285 {
5286     o->in2 = tcg_temp_new_i64();
5287     tcg_gen_shri_i64(o->in2, regs[get_field(f, r3)], 32);
5288 }
5289 #define SPEC_in2_r3_sr32 0
5290
5291 static void in2_r2_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5292 {
5293     o->in2 = tcg_temp_new_i64();
5294     tcg_gen_ext32s_i64(o->in2, regs[get_field(f, r2)]);
5295 }
5296 #define SPEC_in2_r2_32s 0
5297
5298 static void in2_r2_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5299 {
5300     o->in2 = tcg_temp_new_i64();
5301     tcg_gen_ext32u_i64(o->in2, regs[get_field(f, r2)]);
5302 }
5303 #define SPEC_in2_r2_32u 0
5304
5305 static void in2_r2_sr32(DisasContext *s, DisasFields *f, DisasOps *o)
5306 {
5307     o->in2 = tcg_temp_new_i64();
5308     tcg_gen_shri_i64(o->in2, regs[get_field(f, r2)], 32);
5309 }
5310 #define SPEC_in2_r2_sr32 0
5311
5312 static void in2_e2(DisasContext *s, DisasFields *f, DisasOps *o)
5313 {
5314     o->in2 = load_freg32_i64(get_field(f, r2));
5315 }
5316 #define SPEC_in2_e2 0
5317
5318 static void in2_f2_o(DisasContext *s, DisasFields *f, DisasOps *o)
5319 {
5320     o->in2 = fregs[get_field(f, r2)];
5321     o->g_in2 = true;
5322 }
5323 #define SPEC_in2_f2_o 0
5324
5325 static void in2_x2_o(DisasContext *s, DisasFields *f, DisasOps *o)
5326 {
5327     int r2 = get_field(f, r2);
5328     o->in1 = fregs[r2];
5329     o->in2 = fregs[r2 + 2];
5330     o->g_in1 = o->g_in2 = true;
5331 }
5332 #define SPEC_in2_x2_o SPEC_r2_f128
5333
5334 static void in2_ra2(DisasContext *s, DisasFields *f, DisasOps *o)
5335 {
5336     o->in2 = get_address(s, 0, get_field(f, r2), 0);
5337 }
5338 #define SPEC_in2_ra2 0
5339
5340 static void in2_a2(DisasContext *s, DisasFields *f, DisasOps *o)
5341 {
5342     int x2 = have_field(f, x2) ? get_field(f, x2) : 0;
5343     o->in2 = get_address(s, x2, get_field(f, b2), get_field(f, d2));
5344 }
5345 #define SPEC_in2_a2 0
5346
5347 static void in2_ri2(DisasContext *s, DisasFields *f, DisasOps *o)
5348 {
5349     o->in2 = tcg_const_i64(s->pc + (int64_t)get_field(f, i2) * 2);
5350 }
5351 #define SPEC_in2_ri2 0
5352
5353 static void in2_sh32(DisasContext *s, DisasFields *f, DisasOps *o)
5354 {
5355     help_l2_shift(s, f, o, 31);
5356 }
5357 #define SPEC_in2_sh32 0
5358
5359 static void in2_sh64(DisasContext *s, DisasFields *f, DisasOps *o)
5360 {
5361     help_l2_shift(s, f, o, 63);
5362 }
5363 #define SPEC_in2_sh64 0
5364
5365 static void in2_m2_8u(DisasContext *s, DisasFields *f, DisasOps *o)
5366 {
5367     in2_a2(s, f, o);
5368     tcg_gen_qemu_ld8u(o->in2, o->in2, get_mem_index(s));
5369 }
5370 #define SPEC_in2_m2_8u 0
5371
5372 static void in2_m2_16s(DisasContext *s, DisasFields *f, DisasOps *o)
5373 {
5374     in2_a2(s, f, o);
5375     tcg_gen_qemu_ld16s(o->in2, o->in2, get_mem_index(s));
5376 }
5377 #define SPEC_in2_m2_16s 0
5378
5379 static void in2_m2_16u(DisasContext *s, DisasFields *f, DisasOps *o)
5380 {
5381     in2_a2(s, f, o);
5382     tcg_gen_qemu_ld16u(o->in2, o->in2, get_mem_index(s));
5383 }
5384 #define SPEC_in2_m2_16u 0
5385
5386 static void in2_m2_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5387 {
5388     in2_a2(s, f, o);
5389     tcg_gen_qemu_ld32s(o->in2, o->in2, get_mem_index(s));
5390 }
5391 #define SPEC_in2_m2_32s 0
5392
5393 static void in2_m2_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5394 {
5395     in2_a2(s, f, o);
5396     tcg_gen_qemu_ld32u(o->in2, o->in2, get_mem_index(s));
5397 }
5398 #define SPEC_in2_m2_32u 0
5399
5400 static void in2_m2_64(DisasContext *s, DisasFields *f, DisasOps *o)
5401 {
5402     in2_a2(s, f, o);
5403     tcg_gen_qemu_ld64(o->in2, o->in2, get_mem_index(s));
5404 }
5405 #define SPEC_in2_m2_64 0
5406
5407 static void in2_mri2_16u(DisasContext *s, DisasFields *f, DisasOps *o)
5408 {
5409     in2_ri2(s, f, o);
5410     tcg_gen_qemu_ld16u(o->in2, o->in2, get_mem_index(s));
5411 }
5412 #define SPEC_in2_mri2_16u 0
5413
5414 static void in2_mri2_32s(DisasContext *s, DisasFields *f, DisasOps *o)
5415 {
5416     in2_ri2(s, f, o);
5417     tcg_gen_qemu_ld32s(o->in2, o->in2, get_mem_index(s));
5418 }
5419 #define SPEC_in2_mri2_32s 0
5420
5421 static void in2_mri2_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5422 {
5423     in2_ri2(s, f, o);
5424     tcg_gen_qemu_ld32u(o->in2, o->in2, get_mem_index(s));
5425 }
5426 #define SPEC_in2_mri2_32u 0
5427
5428 static void in2_mri2_64(DisasContext *s, DisasFields *f, DisasOps *o)
5429 {
5430     in2_ri2(s, f, o);
5431     tcg_gen_qemu_ld64(o->in2, o->in2, get_mem_index(s));
5432 }
5433 #define SPEC_in2_mri2_64 0
5434
5435 static void in2_i2(DisasContext *s, DisasFields *f, DisasOps *o)
5436 {
5437     o->in2 = tcg_const_i64(get_field(f, i2));
5438 }
5439 #define SPEC_in2_i2 0
5440
5441 static void in2_i2_8u(DisasContext *s, DisasFields *f, DisasOps *o)
5442 {
5443     o->in2 = tcg_const_i64((uint8_t)get_field(f, i2));
5444 }
5445 #define SPEC_in2_i2_8u 0
5446
5447 static void in2_i2_16u(DisasContext *s, DisasFields *f, DisasOps *o)
5448 {
5449     o->in2 = tcg_const_i64((uint16_t)get_field(f, i2));
5450 }
5451 #define SPEC_in2_i2_16u 0
5452
5453 static void in2_i2_32u(DisasContext *s, DisasFields *f, DisasOps *o)
5454 {
5455     o->in2 = tcg_const_i64((uint32_t)get_field(f, i2));
5456 }
5457 #define SPEC_in2_i2_32u 0
5458
5459 static void in2_i2_16u_shl(DisasContext *s, DisasFields *f, DisasOps *o)
5460 {
5461     uint64_t i2 = (uint16_t)get_field(f, i2);
5462     o->in2 = tcg_const_i64(i2 << s->insn->data);
5463 }
5464 #define SPEC_in2_i2_16u_shl 0
5465
5466 static void in2_i2_32u_shl(DisasContext *s, DisasFields *f, DisasOps *o)
5467 {
5468     uint64_t i2 = (uint32_t)get_field(f, i2);
5469     o->in2 = tcg_const_i64(i2 << s->insn->data);
5470 }
5471 #define SPEC_in2_i2_32u_shl 0
5472
5473 #ifndef CONFIG_USER_ONLY
5474 static void in2_insn(DisasContext *s, DisasFields *f, DisasOps *o)
5475 {
5476     o->in2 = tcg_const_i64(s->fields->raw_insn);
5477 }
5478 #define SPEC_in2_insn 0
5479 #endif
5480
5481 /* ====================================================================== */
5482
5483 /* Find opc within the table of insns.  This is formulated as a switch
5484    statement so that (1) we get compile-time notice of cut-paste errors
5485    for duplicated opcodes, and (2) the compiler generates the binary
5486    search tree, rather than us having to post-process the table.  */
5487
5488 #define C(OPC, NM, FT, FC, I1, I2, P, W, OP, CC) \
5489     D(OPC, NM, FT, FC, I1, I2, P, W, OP, CC, 0)
5490
5491 #define D(OPC, NM, FT, FC, I1, I2, P, W, OP, CC, D) insn_ ## NM,
5492
5493 enum DisasInsnEnum {
5494 #include "insn-data.def"
5495 };
5496
5497 #undef D
5498 #define D(OPC, NM, FT, FC, I1, I2, P, W, OP, CC, D) {                       \
5499     .opc = OPC,                                                             \
5500     .fmt = FMT_##FT,                                                        \
5501     .fac = FAC_##FC,                                                        \
5502     .spec = SPEC_in1_##I1 | SPEC_in2_##I2 | SPEC_prep_##P | SPEC_wout_##W,  \
5503     .name = #NM,                                                            \
5504     .help_in1 = in1_##I1,                                                   \
5505     .help_in2 = in2_##I2,                                                   \
5506     .help_prep = prep_##P,                                                  \
5507     .help_wout = wout_##W,                                                  \
5508     .help_cout = cout_##CC,                                                 \
5509     .help_op = op_##OP,                                                     \
5510     .data = D                                                               \
5511  },
5512
5513 /* Allow 0 to be used for NULL in the table below.  */
5514 #define in1_0  NULL
5515 #define in2_0  NULL
5516 #define prep_0  NULL
5517 #define wout_0  NULL
5518 #define cout_0  NULL
5519 #define op_0  NULL
5520
5521 #define SPEC_in1_0 0
5522 #define SPEC_in2_0 0
5523 #define SPEC_prep_0 0
5524 #define SPEC_wout_0 0
5525
5526 /* Give smaller names to the various facilities.  */
5527 #define FAC_Z           S390_FEAT_ZARCH
5528 #define FAC_CASS        S390_FEAT_COMPARE_AND_SWAP_AND_STORE
5529 #define FAC_DFP         S390_FEAT_DFP
5530 #define FAC_DFPR        S390_FEAT_FLOATING_POINT_SUPPPORT_ENH /* DFP-rounding */
5531 #define FAC_DO          S390_FEAT_STFLE_45 /* distinct-operands */
5532 #define FAC_EE          S390_FEAT_EXECUTE_EXT
5533 #define FAC_EI          S390_FEAT_EXTENDED_IMMEDIATE
5534 #define FAC_FPE         S390_FEAT_FLOATING_POINT_EXT
5535 #define FAC_FPSSH       S390_FEAT_FLOATING_POINT_SUPPPORT_ENH /* FPS-sign-handling */
5536 #define FAC_FPRGR       S390_FEAT_FLOATING_POINT_SUPPPORT_ENH /* FPR-GR-transfer */
5537 #define FAC_GIE         S390_FEAT_GENERAL_INSTRUCTIONS_EXT
5538 #define FAC_HFP_MA      S390_FEAT_HFP_MADDSUB
5539 #define FAC_HW          S390_FEAT_STFLE_45 /* high-word */
5540 #define FAC_IEEEE_SIM   S390_FEAT_FLOATING_POINT_SUPPPORT_ENH /* IEEE-exception-simulation */
5541 #define FAC_MIE         S390_FEAT_STFLE_49 /* misc-instruction-extensions */
5542 #define FAC_LAT         S390_FEAT_STFLE_49 /* load-and-trap */
5543 #define FAC_LOC         S390_FEAT_STFLE_45 /* load/store on condition 1 */
5544 #define FAC_LOC2        S390_FEAT_STFLE_53 /* load/store on condition 2 */
5545 #define FAC_LD          S390_FEAT_LONG_DISPLACEMENT
5546 #define FAC_PC          S390_FEAT_STFLE_45 /* population count */
5547 #define FAC_SCF         S390_FEAT_STORE_CLOCK_FAST
5548 #define FAC_SFLE        S390_FEAT_STFLE
5549 #define FAC_ILA         S390_FEAT_STFLE_45 /* interlocked-access-facility 1 */
5550 #define FAC_MVCOS       S390_FEAT_MOVE_WITH_OPTIONAL_SPEC
5551 #define FAC_LPP         S390_FEAT_SET_PROGRAM_PARAMETERS /* load-program-parameter */
5552 #define FAC_DAT_ENH     S390_FEAT_DAT_ENH
5553 #define FAC_E2          S390_FEAT_EXTENDED_TRANSLATION_2
5554 #define FAC_EH          S390_FEAT_STFLE_49 /* execution-hint */
5555 #define FAC_PPA         S390_FEAT_STFLE_49 /* processor-assist */
5556 #define FAC_LZRB        S390_FEAT_STFLE_53 /* load-and-zero-rightmost-byte */
5557 #define FAC_ETF3        S390_FEAT_EXTENDED_TRANSLATION_3
5558
5559 static const DisasInsn insn_info[] = {
5560 #include "insn-data.def"
5561 };
5562
5563 #undef D
5564 #define D(OPC, NM, FT, FC, I1, I2, P, W, OP, CC, D) \
5565     case OPC: return &insn_info[insn_ ## NM];
5566
5567 static const DisasInsn *lookup_opc(uint16_t opc)
5568 {
5569     switch (opc) {
5570 #include "insn-data.def"
5571     default:
5572         return NULL;
5573     }
5574 }
5575
5576 #undef D
5577 #undef C
5578
5579 /* Extract a field from the insn.  The INSN should be left-aligned in
5580    the uint64_t so that we can more easily utilize the big-bit-endian
5581    definitions we extract from the Principals of Operation.  */
5582
5583 static void extract_field(DisasFields *o, const DisasField *f, uint64_t insn)
5584 {
5585     uint32_t r, m;
5586
5587     if (f->size == 0) {
5588         return;
5589     }
5590
5591     /* Zero extract the field from the insn.  */
5592     r = (insn << f->beg) >> (64 - f->size);
5593
5594     /* Sign-extend, or un-swap the field as necessary.  */
5595     switch (f->type) {
5596     case 0: /* unsigned */
5597         break;
5598     case 1: /* signed */
5599         assert(f->size <= 32);
5600         m = 1u << (f->size - 1);
5601         r = (r ^ m) - m;
5602         break;
5603     case 2: /* dl+dh split, signed 20 bit. */
5604         r = ((int8_t)r << 12) | (r >> 8);
5605         break;
5606     default:
5607         abort();
5608     }
5609
5610     /* Validate that the "compressed" encoding we selected above is valid.
5611        I.e. we havn't make two different original fields overlap.  */
5612     assert(((o->presentC >> f->indexC) & 1) == 0);
5613     o->presentC |= 1 << f->indexC;
5614     o->presentO |= 1 << f->indexO;
5615
5616     o->c[f->indexC] = r;
5617 }
5618
5619 /* Lookup the insn at the current PC, extracting the operands into O and
5620    returning the info struct for the insn.  Returns NULL for invalid insn.  */
5621
5622 static const DisasInsn *extract_insn(CPUS390XState *env, DisasContext *s,
5623                                      DisasFields *f)
5624 {
5625     uint64_t insn, pc = s->pc;
5626     int op, op2, ilen;
5627     const DisasInsn *info;
5628
5629     if (unlikely(s->ex_value)) {
5630         /* Drop the EX data now, so that it's clear on exception paths.  */
5631         TCGv_i64 zero = tcg_const_i64(0);
5632         tcg_gen_st_i64(zero, cpu_env, offsetof(CPUS390XState, ex_value));
5633         tcg_temp_free_i64(zero);
5634
5635         /* Extract the values saved by EXECUTE.  */
5636         insn = s->ex_value & 0xffffffffffff0000ull;
5637         ilen = s->ex_value & 0xf;
5638         op = insn >> 56;
5639     } else {
5640         insn = ld_code2(env, pc);
5641         op = (insn >> 8) & 0xff;
5642         ilen = get_ilen(op);
5643         switch (ilen) {
5644         case 2:
5645             insn = insn << 48;
5646             break;
5647         case 4:
5648             insn = ld_code4(env, pc) << 32;
5649             break;
5650         case 6:
5651             insn = (insn << 48) | (ld_code4(env, pc + 2) << 16);
5652             break;
5653         default:
5654             g_assert_not_reached();
5655         }
5656     }
5657     s->next_pc = s->pc + ilen;
5658     s->ilen = ilen;
5659
5660     /* We can't actually determine the insn format until we've looked up
5661        the full insn opcode.  Which we can't do without locating the
5662        secondary opcode.  Assume by default that OP2 is at bit 40; for
5663        those smaller insns that don't actually have a secondary opcode
5664        this will correctly result in OP2 = 0. */
5665     switch (op) {
5666     case 0x01: /* E */
5667     case 0x80: /* S */
5668     case 0x82: /* S */
5669     case 0x93: /* S */
5670     case 0xb2: /* S, RRF, RRE, IE */
5671     case 0xb3: /* RRE, RRD, RRF */
5672     case 0xb9: /* RRE, RRF */
5673     case 0xe5: /* SSE, SIL */
5674         op2 = (insn << 8) >> 56;
5675         break;
5676     case 0xa5: /* RI */
5677     case 0xa7: /* RI */
5678     case 0xc0: /* RIL */
5679     case 0xc2: /* RIL */
5680     case 0xc4: /* RIL */
5681     case 0xc6: /* RIL */
5682     case 0xc8: /* SSF */
5683     case 0xcc: /* RIL */
5684         op2 = (insn << 12) >> 60;
5685         break;
5686     case 0xc5: /* MII */
5687     case 0xc7: /* SMI */
5688     case 0xd0 ... 0xdf: /* SS */
5689     case 0xe1: /* SS */
5690     case 0xe2: /* SS */
5691     case 0xe8: /* SS */
5692     case 0xe9: /* SS */
5693     case 0xea: /* SS */
5694     case 0xee ... 0xf3: /* SS */
5695     case 0xf8 ... 0xfd: /* SS */
5696         op2 = 0;
5697         break;
5698     default:
5699         op2 = (insn << 40) >> 56;
5700         break;
5701     }
5702
5703     memset(f, 0, sizeof(*f));
5704     f->raw_insn = insn;
5705     f->op = op;
5706     f->op2 = op2;
5707
5708     /* Lookup the instruction.  */
5709     info = lookup_opc(op << 8 | op2);
5710
5711     /* If we found it, extract the operands.  */
5712     if (info != NULL) {
5713         DisasFormat fmt = info->fmt;
5714         int i;
5715
5716         for (i = 0; i < NUM_C_FIELD; ++i) {
5717             extract_field(f, &format_info[fmt].op[i], insn);
5718         }
5719     }
5720     return info;
5721 }
5722
5723 static ExitStatus translate_one(CPUS390XState *env, DisasContext *s)
5724 {
5725     const DisasInsn *insn;
5726     ExitStatus ret = NO_EXIT;
5727     DisasFields f;
5728     DisasOps o;
5729
5730     /* Search for the insn in the table.  */
5731     insn = extract_insn(env, s, &f);
5732
5733     /* Not found means unimplemented/illegal opcode.  */
5734     if (insn == NULL) {
5735         qemu_log_mask(LOG_UNIMP, "unimplemented opcode 0x%02x%02x\n",
5736                       f.op, f.op2);
5737         gen_illegal_opcode(s);
5738         return EXIT_NORETURN;
5739     }
5740
5741 #ifndef CONFIG_USER_ONLY
5742     if (s->tb->flags & FLAG_MASK_PER) {
5743         TCGv_i64 addr = tcg_const_i64(s->pc);
5744         gen_helper_per_ifetch(cpu_env, addr);
5745         tcg_temp_free_i64(addr);
5746     }
5747 #endif
5748
5749     /* Check for insn specification exceptions.  */
5750     if (insn->spec) {
5751         int spec = insn->spec, excp = 0, r;
5752
5753         if (spec & SPEC_r1_even) {
5754             r = get_field(&f, r1);
5755             if (r & 1) {
5756                 excp = PGM_SPECIFICATION;
5757             }
5758         }
5759         if (spec & SPEC_r2_even) {
5760             r = get_field(&f, r2);
5761             if (r & 1) {
5762                 excp = PGM_SPECIFICATION;
5763             }
5764         }
5765         if (spec & SPEC_r3_even) {
5766             r = get_field(&f, r3);
5767             if (r & 1) {
5768                 excp = PGM_SPECIFICATION;
5769             }
5770         }
5771         if (spec & SPEC_r1_f128) {
5772             r = get_field(&f, r1);
5773             if (r > 13) {
5774                 excp = PGM_SPECIFICATION;
5775             }
5776         }
5777         if (spec & SPEC_r2_f128) {
5778             r = get_field(&f, r2);
5779             if (r > 13) {
5780                 excp = PGM_SPECIFICATION;
5781             }
5782         }
5783         if (excp) {
5784             gen_program_exception(s, excp);
5785             return EXIT_NORETURN;
5786         }
5787     }
5788
5789     /* Set up the strutures we use to communicate with the helpers. */
5790     s->insn = insn;
5791     s->fields = &f;
5792     o.g_out = o.g_out2 = o.g_in1 = o.g_in2 = false;
5793     TCGV_UNUSED_I64(o.out);
5794     TCGV_UNUSED_I64(o.out2);
5795     TCGV_UNUSED_I64(o.in1);
5796     TCGV_UNUSED_I64(o.in2);
5797     TCGV_UNUSED_I64(o.addr1);
5798
5799     /* Implement the instruction.  */
5800     if (insn->help_in1) {
5801         insn->help_in1(s, &f, &o);
5802     }
5803     if (insn->help_in2) {
5804         insn->help_in2(s, &f, &o);
5805     }
5806     if (insn->help_prep) {
5807         insn->help_prep(s, &f, &o);
5808     }
5809     if (insn->help_op) {
5810         ret = insn->help_op(s, &o);
5811     }
5812     if (insn->help_wout) {
5813         insn->help_wout(s, &f, &o);
5814     }
5815     if (insn->help_cout) {
5816         insn->help_cout(s, &o);
5817     }
5818
5819     /* Free any temporaries created by the helpers.  */
5820     if (!TCGV_IS_UNUSED_I64(o.out) && !o.g_out) {
5821         tcg_temp_free_i64(o.out);
5822     }
5823     if (!TCGV_IS_UNUSED_I64(o.out2) && !o.g_out2) {
5824         tcg_temp_free_i64(o.out2);
5825     }
5826     if (!TCGV_IS_UNUSED_I64(o.in1) && !o.g_in1) {
5827         tcg_temp_free_i64(o.in1);
5828     }
5829     if (!TCGV_IS_UNUSED_I64(o.in2) && !o.g_in2) {
5830         tcg_temp_free_i64(o.in2);
5831     }
5832     if (!TCGV_IS_UNUSED_I64(o.addr1)) {
5833         tcg_temp_free_i64(o.addr1);
5834     }
5835
5836 #ifndef CONFIG_USER_ONLY
5837     if (s->tb->flags & FLAG_MASK_PER) {
5838         /* An exception might be triggered, save PSW if not already done.  */
5839         if (ret == NO_EXIT || ret == EXIT_PC_STALE) {
5840             tcg_gen_movi_i64(psw_addr, s->next_pc);
5841         }
5842
5843         /* Save off cc.  */
5844         update_cc_op(s);
5845
5846         /* Call the helper to check for a possible PER exception.  */
5847         gen_helper_per_check_exception(cpu_env);
5848     }
5849 #endif
5850
5851     /* Advance to the next instruction.  */
5852     s->pc = s->next_pc;
5853     return ret;
5854 }
5855
5856 void gen_intermediate_code(CPUState *cs, struct TranslationBlock *tb)
5857 {
5858     CPUS390XState *env = cs->env_ptr;
5859     DisasContext dc;
5860     target_ulong pc_start;
5861     uint64_t next_page_start;
5862     int num_insns, max_insns;
5863     ExitStatus status;
5864     bool do_debug;
5865
5866     pc_start = tb->pc;
5867
5868     /* 31-bit mode */
5869     if (!(tb->flags & FLAG_MASK_64)) {
5870         pc_start &= 0x7fffffff;
5871     }
5872
5873     dc.tb = tb;
5874     dc.pc = pc_start;
5875     dc.cc_op = CC_OP_DYNAMIC;
5876     dc.ex_value = tb->cs_base;
5877     do_debug = dc.singlestep_enabled = cs->singlestep_enabled;
5878
5879     next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
5880
5881     num_insns = 0;
5882     max_insns = tb->cflags & CF_COUNT_MASK;
5883     if (max_insns == 0) {
5884         max_insns = CF_COUNT_MASK;
5885     }
5886     if (max_insns > TCG_MAX_INSNS) {
5887         max_insns = TCG_MAX_INSNS;
5888     }
5889
5890     gen_tb_start(tb);
5891
5892     do {
5893         tcg_gen_insn_start(dc.pc, dc.cc_op);
5894         num_insns++;
5895
5896         if (unlikely(cpu_breakpoint_test(cs, dc.pc, BP_ANY))) {
5897             status = EXIT_PC_STALE;
5898             do_debug = true;
5899             /* The address covered by the breakpoint must be included in
5900                [tb->pc, tb->pc + tb->size) in order to for it to be
5901                properly cleared -- thus we increment the PC here so that
5902                the logic setting tb->size below does the right thing.  */
5903             dc.pc += 2;
5904             break;
5905         }
5906
5907         if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
5908             gen_io_start();
5909         }
5910
5911         status = translate_one(env, &dc);
5912
5913         /* If we reach a page boundary, are single stepping,
5914            or exhaust instruction count, stop generation.  */
5915         if (status == NO_EXIT
5916             && (dc.pc >= next_page_start
5917                 || tcg_op_buf_full()
5918                 || num_insns >= max_insns
5919                 || singlestep
5920                 || cs->singlestep_enabled
5921                 || dc.ex_value)) {
5922             status = EXIT_PC_STALE;
5923         }
5924     } while (status == NO_EXIT);
5925
5926     if (tb->cflags & CF_LAST_IO) {
5927         gen_io_end();
5928     }
5929
5930     switch (status) {
5931     case EXIT_GOTO_TB:
5932     case EXIT_NORETURN:
5933         break;
5934     case EXIT_PC_STALE:
5935     case EXIT_PC_STALE_NOCHAIN:
5936         update_psw_addr(&dc);
5937         /* FALLTHRU */
5938     case EXIT_PC_UPDATED:
5939         /* Next TB starts off with CC_OP_DYNAMIC, so make sure the
5940            cc op type is in env */
5941         update_cc_op(&dc);
5942         /* FALLTHRU */
5943     case EXIT_PC_CC_UPDATED:
5944         /* Exit the TB, either by raising a debug exception or by return.  */
5945         if (do_debug) {
5946             gen_exception(EXCP_DEBUG);
5947         } else if (use_exit_tb(&dc) || status == EXIT_PC_STALE_NOCHAIN) {
5948             tcg_gen_exit_tb(0);
5949         } else {
5950             tcg_gen_lookup_and_goto_ptr(psw_addr);
5951         }
5952         break;
5953     default:
5954         g_assert_not_reached();
5955     }
5956
5957     gen_tb_end(tb, num_insns);
5958
5959     tb->size = dc.pc - pc_start;
5960     tb->icount = num_insns;
5961
5962 #if defined(S390X_DEBUG_DISAS)
5963     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
5964         && qemu_log_in_addr_range(pc_start)) {
5965         qemu_log_lock();
5966         if (unlikely(dc.ex_value)) {
5967             /* ??? Unfortunately log_target_disas can't use host memory.  */
5968             qemu_log("IN: EXECUTE %016" PRIx64 "\n", dc.ex_value);
5969         } else {
5970             qemu_log("IN: %s\n", lookup_symbol(pc_start));
5971             log_target_disas(cs, pc_start, dc.pc - pc_start, 1);
5972             qemu_log("\n");
5973         }
5974         qemu_log_unlock();
5975     }
5976 #endif
5977 }
5978
5979 void restore_state_to_opc(CPUS390XState *env, TranslationBlock *tb,
5980                           target_ulong *data)
5981 {
5982     int cc_op = data[1];
5983     env->psw.addr = data[0];
5984     if ((cc_op != CC_OP_DYNAMIC) && (cc_op != CC_OP_STATIC)) {
5985         env->cc_op = cc_op;
5986     }
5987 }
This page took 0.371397 seconds and 4 git commands to generate.