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