xref: /openbmc/qemu/target/hppa/translate.c (revision 2a196de1)
1 /*
2  * HPPA emulation cpu translation for qemu.
3  *
4  * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "disas/disas.h"
23 #include "qemu/host-utils.h"
24 #include "exec/exec-all.h"
25 #include "tcg/tcg-op.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/helper-proto.h"
28 #include "exec/helper-gen.h"
29 #include "exec/translator.h"
30 #include "exec/log.h"
31 
32 /* Since we have a distinction between register size and address size,
33    we need to redefine all of these.  */
34 
35 #undef TCGv
36 #undef tcg_temp_new
37 #undef tcg_global_mem_new
38 #undef tcg_temp_free
39 
40 #if TARGET_LONG_BITS == 64
41 #define TCGv_tl              TCGv_i64
42 #define tcg_temp_new_tl      tcg_temp_new_i64
43 #define tcg_temp_free_tl     tcg_temp_free_i64
44 #if TARGET_REGISTER_BITS == 64
45 #define tcg_gen_extu_reg_tl  tcg_gen_mov_i64
46 #else
47 #define tcg_gen_extu_reg_tl  tcg_gen_extu_i32_i64
48 #endif
49 #else
50 #define TCGv_tl              TCGv_i32
51 #define tcg_temp_new_tl      tcg_temp_new_i32
52 #define tcg_temp_free_tl     tcg_temp_free_i32
53 #define tcg_gen_extu_reg_tl  tcg_gen_mov_i32
54 #endif
55 
56 #if TARGET_REGISTER_BITS == 64
57 #define TCGv_reg             TCGv_i64
58 
59 #define tcg_temp_new         tcg_temp_new_i64
60 #define tcg_global_mem_new   tcg_global_mem_new_i64
61 #define tcg_temp_free        tcg_temp_free_i64
62 
63 #define tcg_gen_movi_reg     tcg_gen_movi_i64
64 #define tcg_gen_mov_reg      tcg_gen_mov_i64
65 #define tcg_gen_ld8u_reg     tcg_gen_ld8u_i64
66 #define tcg_gen_ld8s_reg     tcg_gen_ld8s_i64
67 #define tcg_gen_ld16u_reg    tcg_gen_ld16u_i64
68 #define tcg_gen_ld16s_reg    tcg_gen_ld16s_i64
69 #define tcg_gen_ld32u_reg    tcg_gen_ld32u_i64
70 #define tcg_gen_ld32s_reg    tcg_gen_ld32s_i64
71 #define tcg_gen_ld_reg       tcg_gen_ld_i64
72 #define tcg_gen_st8_reg      tcg_gen_st8_i64
73 #define tcg_gen_st16_reg     tcg_gen_st16_i64
74 #define tcg_gen_st32_reg     tcg_gen_st32_i64
75 #define tcg_gen_st_reg       tcg_gen_st_i64
76 #define tcg_gen_add_reg      tcg_gen_add_i64
77 #define tcg_gen_addi_reg     tcg_gen_addi_i64
78 #define tcg_gen_sub_reg      tcg_gen_sub_i64
79 #define tcg_gen_neg_reg      tcg_gen_neg_i64
80 #define tcg_gen_subfi_reg    tcg_gen_subfi_i64
81 #define tcg_gen_subi_reg     tcg_gen_subi_i64
82 #define tcg_gen_and_reg      tcg_gen_and_i64
83 #define tcg_gen_andi_reg     tcg_gen_andi_i64
84 #define tcg_gen_or_reg       tcg_gen_or_i64
85 #define tcg_gen_ori_reg      tcg_gen_ori_i64
86 #define tcg_gen_xor_reg      tcg_gen_xor_i64
87 #define tcg_gen_xori_reg     tcg_gen_xori_i64
88 #define tcg_gen_not_reg      tcg_gen_not_i64
89 #define tcg_gen_shl_reg      tcg_gen_shl_i64
90 #define tcg_gen_shli_reg     tcg_gen_shli_i64
91 #define tcg_gen_shr_reg      tcg_gen_shr_i64
92 #define tcg_gen_shri_reg     tcg_gen_shri_i64
93 #define tcg_gen_sar_reg      tcg_gen_sar_i64
94 #define tcg_gen_sari_reg     tcg_gen_sari_i64
95 #define tcg_gen_brcond_reg   tcg_gen_brcond_i64
96 #define tcg_gen_brcondi_reg  tcg_gen_brcondi_i64
97 #define tcg_gen_setcond_reg  tcg_gen_setcond_i64
98 #define tcg_gen_setcondi_reg tcg_gen_setcondi_i64
99 #define tcg_gen_mul_reg      tcg_gen_mul_i64
100 #define tcg_gen_muli_reg     tcg_gen_muli_i64
101 #define tcg_gen_div_reg      tcg_gen_div_i64
102 #define tcg_gen_rem_reg      tcg_gen_rem_i64
103 #define tcg_gen_divu_reg     tcg_gen_divu_i64
104 #define tcg_gen_remu_reg     tcg_gen_remu_i64
105 #define tcg_gen_discard_reg  tcg_gen_discard_i64
106 #define tcg_gen_trunc_reg_i32 tcg_gen_extrl_i64_i32
107 #define tcg_gen_trunc_i64_reg tcg_gen_mov_i64
108 #define tcg_gen_extu_i32_reg tcg_gen_extu_i32_i64
109 #define tcg_gen_ext_i32_reg  tcg_gen_ext_i32_i64
110 #define tcg_gen_extu_reg_i64 tcg_gen_mov_i64
111 #define tcg_gen_ext_reg_i64  tcg_gen_mov_i64
112 #define tcg_gen_ext8u_reg    tcg_gen_ext8u_i64
113 #define tcg_gen_ext8s_reg    tcg_gen_ext8s_i64
114 #define tcg_gen_ext16u_reg   tcg_gen_ext16u_i64
115 #define tcg_gen_ext16s_reg   tcg_gen_ext16s_i64
116 #define tcg_gen_ext32u_reg   tcg_gen_ext32u_i64
117 #define tcg_gen_ext32s_reg   tcg_gen_ext32s_i64
118 #define tcg_gen_bswap16_reg  tcg_gen_bswap16_i64
119 #define tcg_gen_bswap32_reg  tcg_gen_bswap32_i64
120 #define tcg_gen_bswap64_reg  tcg_gen_bswap64_i64
121 #define tcg_gen_concat_reg_i64 tcg_gen_concat32_i64
122 #define tcg_gen_andc_reg     tcg_gen_andc_i64
123 #define tcg_gen_eqv_reg      tcg_gen_eqv_i64
124 #define tcg_gen_nand_reg     tcg_gen_nand_i64
125 #define tcg_gen_nor_reg      tcg_gen_nor_i64
126 #define tcg_gen_orc_reg      tcg_gen_orc_i64
127 #define tcg_gen_clz_reg      tcg_gen_clz_i64
128 #define tcg_gen_ctz_reg      tcg_gen_ctz_i64
129 #define tcg_gen_clzi_reg     tcg_gen_clzi_i64
130 #define tcg_gen_ctzi_reg     tcg_gen_ctzi_i64
131 #define tcg_gen_clrsb_reg    tcg_gen_clrsb_i64
132 #define tcg_gen_ctpop_reg    tcg_gen_ctpop_i64
133 #define tcg_gen_rotl_reg     tcg_gen_rotl_i64
134 #define tcg_gen_rotli_reg    tcg_gen_rotli_i64
135 #define tcg_gen_rotr_reg     tcg_gen_rotr_i64
136 #define tcg_gen_rotri_reg    tcg_gen_rotri_i64
137 #define tcg_gen_deposit_reg  tcg_gen_deposit_i64
138 #define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i64
139 #define tcg_gen_extract_reg  tcg_gen_extract_i64
140 #define tcg_gen_sextract_reg tcg_gen_sextract_i64
141 #define tcg_gen_extract2_reg tcg_gen_extract2_i64
142 #define tcg_const_reg        tcg_const_i64
143 #define tcg_const_local_reg  tcg_const_local_i64
144 #define tcg_constant_reg     tcg_constant_i64
145 #define tcg_gen_movcond_reg  tcg_gen_movcond_i64
146 #define tcg_gen_add2_reg     tcg_gen_add2_i64
147 #define tcg_gen_sub2_reg     tcg_gen_sub2_i64
148 #define tcg_gen_qemu_ld_reg  tcg_gen_qemu_ld_i64
149 #define tcg_gen_qemu_st_reg  tcg_gen_qemu_st_i64
150 #define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i64
151 #define tcg_gen_trunc_reg_ptr   tcg_gen_trunc_i64_ptr
152 #else
153 #define TCGv_reg             TCGv_i32
154 #define tcg_temp_new         tcg_temp_new_i32
155 #define tcg_global_mem_new   tcg_global_mem_new_i32
156 #define tcg_temp_free        tcg_temp_free_i32
157 
158 #define tcg_gen_movi_reg     tcg_gen_movi_i32
159 #define tcg_gen_mov_reg      tcg_gen_mov_i32
160 #define tcg_gen_ld8u_reg     tcg_gen_ld8u_i32
161 #define tcg_gen_ld8s_reg     tcg_gen_ld8s_i32
162 #define tcg_gen_ld16u_reg    tcg_gen_ld16u_i32
163 #define tcg_gen_ld16s_reg    tcg_gen_ld16s_i32
164 #define tcg_gen_ld32u_reg    tcg_gen_ld_i32
165 #define tcg_gen_ld32s_reg    tcg_gen_ld_i32
166 #define tcg_gen_ld_reg       tcg_gen_ld_i32
167 #define tcg_gen_st8_reg      tcg_gen_st8_i32
168 #define tcg_gen_st16_reg     tcg_gen_st16_i32
169 #define tcg_gen_st32_reg     tcg_gen_st32_i32
170 #define tcg_gen_st_reg       tcg_gen_st_i32
171 #define tcg_gen_add_reg      tcg_gen_add_i32
172 #define tcg_gen_addi_reg     tcg_gen_addi_i32
173 #define tcg_gen_sub_reg      tcg_gen_sub_i32
174 #define tcg_gen_neg_reg      tcg_gen_neg_i32
175 #define tcg_gen_subfi_reg    tcg_gen_subfi_i32
176 #define tcg_gen_subi_reg     tcg_gen_subi_i32
177 #define tcg_gen_and_reg      tcg_gen_and_i32
178 #define tcg_gen_andi_reg     tcg_gen_andi_i32
179 #define tcg_gen_or_reg       tcg_gen_or_i32
180 #define tcg_gen_ori_reg      tcg_gen_ori_i32
181 #define tcg_gen_xor_reg      tcg_gen_xor_i32
182 #define tcg_gen_xori_reg     tcg_gen_xori_i32
183 #define tcg_gen_not_reg      tcg_gen_not_i32
184 #define tcg_gen_shl_reg      tcg_gen_shl_i32
185 #define tcg_gen_shli_reg     tcg_gen_shli_i32
186 #define tcg_gen_shr_reg      tcg_gen_shr_i32
187 #define tcg_gen_shri_reg     tcg_gen_shri_i32
188 #define tcg_gen_sar_reg      tcg_gen_sar_i32
189 #define tcg_gen_sari_reg     tcg_gen_sari_i32
190 #define tcg_gen_brcond_reg   tcg_gen_brcond_i32
191 #define tcg_gen_brcondi_reg  tcg_gen_brcondi_i32
192 #define tcg_gen_setcond_reg  tcg_gen_setcond_i32
193 #define tcg_gen_setcondi_reg tcg_gen_setcondi_i32
194 #define tcg_gen_mul_reg      tcg_gen_mul_i32
195 #define tcg_gen_muli_reg     tcg_gen_muli_i32
196 #define tcg_gen_div_reg      tcg_gen_div_i32
197 #define tcg_gen_rem_reg      tcg_gen_rem_i32
198 #define tcg_gen_divu_reg     tcg_gen_divu_i32
199 #define tcg_gen_remu_reg     tcg_gen_remu_i32
200 #define tcg_gen_discard_reg  tcg_gen_discard_i32
201 #define tcg_gen_trunc_reg_i32 tcg_gen_mov_i32
202 #define tcg_gen_trunc_i64_reg tcg_gen_extrl_i64_i32
203 #define tcg_gen_extu_i32_reg tcg_gen_mov_i32
204 #define tcg_gen_ext_i32_reg  tcg_gen_mov_i32
205 #define tcg_gen_extu_reg_i64 tcg_gen_extu_i32_i64
206 #define tcg_gen_ext_reg_i64  tcg_gen_ext_i32_i64
207 #define tcg_gen_ext8u_reg    tcg_gen_ext8u_i32
208 #define tcg_gen_ext8s_reg    tcg_gen_ext8s_i32
209 #define tcg_gen_ext16u_reg   tcg_gen_ext16u_i32
210 #define tcg_gen_ext16s_reg   tcg_gen_ext16s_i32
211 #define tcg_gen_ext32u_reg   tcg_gen_mov_i32
212 #define tcg_gen_ext32s_reg   tcg_gen_mov_i32
213 #define tcg_gen_bswap16_reg  tcg_gen_bswap16_i32
214 #define tcg_gen_bswap32_reg  tcg_gen_bswap32_i32
215 #define tcg_gen_concat_reg_i64 tcg_gen_concat_i32_i64
216 #define tcg_gen_andc_reg     tcg_gen_andc_i32
217 #define tcg_gen_eqv_reg      tcg_gen_eqv_i32
218 #define tcg_gen_nand_reg     tcg_gen_nand_i32
219 #define tcg_gen_nor_reg      tcg_gen_nor_i32
220 #define tcg_gen_orc_reg      tcg_gen_orc_i32
221 #define tcg_gen_clz_reg      tcg_gen_clz_i32
222 #define tcg_gen_ctz_reg      tcg_gen_ctz_i32
223 #define tcg_gen_clzi_reg     tcg_gen_clzi_i32
224 #define tcg_gen_ctzi_reg     tcg_gen_ctzi_i32
225 #define tcg_gen_clrsb_reg    tcg_gen_clrsb_i32
226 #define tcg_gen_ctpop_reg    tcg_gen_ctpop_i32
227 #define tcg_gen_rotl_reg     tcg_gen_rotl_i32
228 #define tcg_gen_rotli_reg    tcg_gen_rotli_i32
229 #define tcg_gen_rotr_reg     tcg_gen_rotr_i32
230 #define tcg_gen_rotri_reg    tcg_gen_rotri_i32
231 #define tcg_gen_deposit_reg  tcg_gen_deposit_i32
232 #define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i32
233 #define tcg_gen_extract_reg  tcg_gen_extract_i32
234 #define tcg_gen_sextract_reg tcg_gen_sextract_i32
235 #define tcg_gen_extract2_reg tcg_gen_extract2_i32
236 #define tcg_const_reg        tcg_const_i32
237 #define tcg_const_local_reg  tcg_const_local_i32
238 #define tcg_constant_reg     tcg_constant_i32
239 #define tcg_gen_movcond_reg  tcg_gen_movcond_i32
240 #define tcg_gen_add2_reg     tcg_gen_add2_i32
241 #define tcg_gen_sub2_reg     tcg_gen_sub2_i32
242 #define tcg_gen_qemu_ld_reg  tcg_gen_qemu_ld_i32
243 #define tcg_gen_qemu_st_reg  tcg_gen_qemu_st_i32
244 #define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i32
245 #define tcg_gen_trunc_reg_ptr   tcg_gen_ext_i32_ptr
246 #endif /* TARGET_REGISTER_BITS */
247 
248 typedef struct DisasCond {
249     TCGCond c;
250     TCGv_reg a0, a1;
251 } DisasCond;
252 
253 typedef struct DisasContext {
254     DisasContextBase base;
255     CPUState *cs;
256 
257     target_ureg iaoq_f;
258     target_ureg iaoq_b;
259     target_ureg iaoq_n;
260     TCGv_reg iaoq_n_var;
261 
262     int ntempr, ntempl;
263     TCGv_reg tempr[8];
264     TCGv_tl  templ[4];
265 
266     DisasCond null_cond;
267     TCGLabel *null_lab;
268 
269     uint32_t insn;
270     uint32_t tb_flags;
271     int mmu_idx;
272     int privilege;
273     bool psw_n_nonzero;
274 
275 #ifdef CONFIG_USER_ONLY
276     MemOp unalign;
277 #endif
278 } DisasContext;
279 
280 #ifdef CONFIG_USER_ONLY
281 #define UNALIGN(C)  (C)->unalign
282 #else
283 #define UNALIGN(C)  0
284 #endif
285 
286 /* Note that ssm/rsm instructions number PSW_W and PSW_E differently.  */
287 static int expand_sm_imm(DisasContext *ctx, int val)
288 {
289     if (val & PSW_SM_E) {
290         val = (val & ~PSW_SM_E) | PSW_E;
291     }
292     if (val & PSW_SM_W) {
293         val = (val & ~PSW_SM_W) | PSW_W;
294     }
295     return val;
296 }
297 
298 /* Inverted space register indicates 0 means sr0 not inferred from base.  */
299 static int expand_sr3x(DisasContext *ctx, int val)
300 {
301     return ~val;
302 }
303 
304 /* Convert the M:A bits within a memory insn to the tri-state value
305    we use for the final M.  */
306 static int ma_to_m(DisasContext *ctx, int val)
307 {
308     return val & 2 ? (val & 1 ? -1 : 1) : 0;
309 }
310 
311 /* Convert the sign of the displacement to a pre or post-modify.  */
312 static int pos_to_m(DisasContext *ctx, int val)
313 {
314     return val ? 1 : -1;
315 }
316 
317 static int neg_to_m(DisasContext *ctx, int val)
318 {
319     return val ? -1 : 1;
320 }
321 
322 /* Used for branch targets and fp memory ops.  */
323 static int expand_shl2(DisasContext *ctx, int val)
324 {
325     return val << 2;
326 }
327 
328 /* Used for fp memory ops.  */
329 static int expand_shl3(DisasContext *ctx, int val)
330 {
331     return val << 3;
332 }
333 
334 /* Used for assemble_21.  */
335 static int expand_shl11(DisasContext *ctx, int val)
336 {
337     return val << 11;
338 }
339 
340 
341 /* Include the auto-generated decoder.  */
342 #include "decode-insns.c.inc"
343 
344 /* We are not using a goto_tb (for whatever reason), but have updated
345    the iaq (for whatever reason), so don't do it again on exit.  */
346 #define DISAS_IAQ_N_UPDATED  DISAS_TARGET_0
347 
348 /* We are exiting the TB, but have neither emitted a goto_tb, nor
349    updated the iaq for the next instruction to be executed.  */
350 #define DISAS_IAQ_N_STALE    DISAS_TARGET_1
351 
352 /* Similarly, but we want to return to the main loop immediately
353    to recognize unmasked interrupts.  */
354 #define DISAS_IAQ_N_STALE_EXIT      DISAS_TARGET_2
355 #define DISAS_EXIT                  DISAS_TARGET_3
356 
357 /* global register indexes */
358 static TCGv_reg cpu_gr[32];
359 static TCGv_i64 cpu_sr[4];
360 static TCGv_i64 cpu_srH;
361 static TCGv_reg cpu_iaoq_f;
362 static TCGv_reg cpu_iaoq_b;
363 static TCGv_i64 cpu_iasq_f;
364 static TCGv_i64 cpu_iasq_b;
365 static TCGv_reg cpu_sar;
366 static TCGv_reg cpu_psw_n;
367 static TCGv_reg cpu_psw_v;
368 static TCGv_reg cpu_psw_cb;
369 static TCGv_reg cpu_psw_cb_msb;
370 
371 #include "exec/gen-icount.h"
372 
373 void hppa_translate_init(void)
374 {
375 #define DEF_VAR(V)  { &cpu_##V, #V, offsetof(CPUHPPAState, V) }
376 
377     typedef struct { TCGv_reg *var; const char *name; int ofs; } GlobalVar;
378     static const GlobalVar vars[] = {
379         { &cpu_sar, "sar", offsetof(CPUHPPAState, cr[CR_SAR]) },
380         DEF_VAR(psw_n),
381         DEF_VAR(psw_v),
382         DEF_VAR(psw_cb),
383         DEF_VAR(psw_cb_msb),
384         DEF_VAR(iaoq_f),
385         DEF_VAR(iaoq_b),
386     };
387 
388 #undef DEF_VAR
389 
390     /* Use the symbolic register names that match the disassembler.  */
391     static const char gr_names[32][4] = {
392         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
393         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
394         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
395         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
396     };
397     /* SR[4-7] are not global registers so that we can index them.  */
398     static const char sr_names[5][4] = {
399         "sr0", "sr1", "sr2", "sr3", "srH"
400     };
401 
402     int i;
403 
404     cpu_gr[0] = NULL;
405     for (i = 1; i < 32; i++) {
406         cpu_gr[i] = tcg_global_mem_new(cpu_env,
407                                        offsetof(CPUHPPAState, gr[i]),
408                                        gr_names[i]);
409     }
410     for (i = 0; i < 4; i++) {
411         cpu_sr[i] = tcg_global_mem_new_i64(cpu_env,
412                                            offsetof(CPUHPPAState, sr[i]),
413                                            sr_names[i]);
414     }
415     cpu_srH = tcg_global_mem_new_i64(cpu_env,
416                                      offsetof(CPUHPPAState, sr[4]),
417                                      sr_names[4]);
418 
419     for (i = 0; i < ARRAY_SIZE(vars); ++i) {
420         const GlobalVar *v = &vars[i];
421         *v->var = tcg_global_mem_new(cpu_env, v->ofs, v->name);
422     }
423 
424     cpu_iasq_f = tcg_global_mem_new_i64(cpu_env,
425                                         offsetof(CPUHPPAState, iasq_f),
426                                         "iasq_f");
427     cpu_iasq_b = tcg_global_mem_new_i64(cpu_env,
428                                         offsetof(CPUHPPAState, iasq_b),
429                                         "iasq_b");
430 }
431 
432 static DisasCond cond_make_f(void)
433 {
434     return (DisasCond){
435         .c = TCG_COND_NEVER,
436         .a0 = NULL,
437         .a1 = NULL,
438     };
439 }
440 
441 static DisasCond cond_make_t(void)
442 {
443     return (DisasCond){
444         .c = TCG_COND_ALWAYS,
445         .a0 = NULL,
446         .a1 = NULL,
447     };
448 }
449 
450 static DisasCond cond_make_n(void)
451 {
452     return (DisasCond){
453         .c = TCG_COND_NE,
454         .a0 = cpu_psw_n,
455         .a1 = tcg_constant_reg(0)
456     };
457 }
458 
459 static DisasCond cond_make_0_tmp(TCGCond c, TCGv_reg a0)
460 {
461     assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
462     return (DisasCond){
463         .c = c, .a0 = a0, .a1 = tcg_constant_reg(0)
464     };
465 }
466 
467 static DisasCond cond_make_0(TCGCond c, TCGv_reg a0)
468 {
469     TCGv_reg tmp = tcg_temp_new();
470     tcg_gen_mov_reg(tmp, a0);
471     return cond_make_0_tmp(c, tmp);
472 }
473 
474 static DisasCond cond_make(TCGCond c, TCGv_reg a0, TCGv_reg a1)
475 {
476     DisasCond r = { .c = c };
477 
478     assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
479     r.a0 = tcg_temp_new();
480     tcg_gen_mov_reg(r.a0, a0);
481     r.a1 = tcg_temp_new();
482     tcg_gen_mov_reg(r.a1, a1);
483 
484     return r;
485 }
486 
487 static void cond_free(DisasCond *cond)
488 {
489     switch (cond->c) {
490     default:
491         if (cond->a0 != cpu_psw_n) {
492             tcg_temp_free(cond->a0);
493         }
494         tcg_temp_free(cond->a1);
495         cond->a0 = NULL;
496         cond->a1 = NULL;
497         /* fallthru */
498     case TCG_COND_ALWAYS:
499         cond->c = TCG_COND_NEVER;
500         break;
501     case TCG_COND_NEVER:
502         break;
503     }
504 }
505 
506 static TCGv_reg get_temp(DisasContext *ctx)
507 {
508     unsigned i = ctx->ntempr++;
509     g_assert(i < ARRAY_SIZE(ctx->tempr));
510     return ctx->tempr[i] = tcg_temp_new();
511 }
512 
513 #ifndef CONFIG_USER_ONLY
514 static TCGv_tl get_temp_tl(DisasContext *ctx)
515 {
516     unsigned i = ctx->ntempl++;
517     g_assert(i < ARRAY_SIZE(ctx->templ));
518     return ctx->templ[i] = tcg_temp_new_tl();
519 }
520 #endif
521 
522 static TCGv_reg load_const(DisasContext *ctx, target_sreg v)
523 {
524     TCGv_reg t = get_temp(ctx);
525     tcg_gen_movi_reg(t, v);
526     return t;
527 }
528 
529 static TCGv_reg load_gpr(DisasContext *ctx, unsigned reg)
530 {
531     if (reg == 0) {
532         TCGv_reg t = get_temp(ctx);
533         tcg_gen_movi_reg(t, 0);
534         return t;
535     } else {
536         return cpu_gr[reg];
537     }
538 }
539 
540 static TCGv_reg dest_gpr(DisasContext *ctx, unsigned reg)
541 {
542     if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
543         return get_temp(ctx);
544     } else {
545         return cpu_gr[reg];
546     }
547 }
548 
549 static void save_or_nullify(DisasContext *ctx, TCGv_reg dest, TCGv_reg t)
550 {
551     if (ctx->null_cond.c != TCG_COND_NEVER) {
552         tcg_gen_movcond_reg(ctx->null_cond.c, dest, ctx->null_cond.a0,
553                             ctx->null_cond.a1, dest, t);
554     } else {
555         tcg_gen_mov_reg(dest, t);
556     }
557 }
558 
559 static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_reg t)
560 {
561     if (reg != 0) {
562         save_or_nullify(ctx, cpu_gr[reg], t);
563     }
564 }
565 
566 #if HOST_BIG_ENDIAN
567 # define HI_OFS  0
568 # define LO_OFS  4
569 #else
570 # define HI_OFS  4
571 # define LO_OFS  0
572 #endif
573 
574 static TCGv_i32 load_frw_i32(unsigned rt)
575 {
576     TCGv_i32 ret = tcg_temp_new_i32();
577     tcg_gen_ld_i32(ret, cpu_env,
578                    offsetof(CPUHPPAState, fr[rt & 31])
579                    + (rt & 32 ? LO_OFS : HI_OFS));
580     return ret;
581 }
582 
583 static TCGv_i32 load_frw0_i32(unsigned rt)
584 {
585     if (rt == 0) {
586         return tcg_const_i32(0);
587     } else {
588         return load_frw_i32(rt);
589     }
590 }
591 
592 static TCGv_i64 load_frw0_i64(unsigned rt)
593 {
594     if (rt == 0) {
595         return tcg_const_i64(0);
596     } else {
597         TCGv_i64 ret = tcg_temp_new_i64();
598         tcg_gen_ld32u_i64(ret, cpu_env,
599                           offsetof(CPUHPPAState, fr[rt & 31])
600                           + (rt & 32 ? LO_OFS : HI_OFS));
601         return ret;
602     }
603 }
604 
605 static void save_frw_i32(unsigned rt, TCGv_i32 val)
606 {
607     tcg_gen_st_i32(val, cpu_env,
608                    offsetof(CPUHPPAState, fr[rt & 31])
609                    + (rt & 32 ? LO_OFS : HI_OFS));
610 }
611 
612 #undef HI_OFS
613 #undef LO_OFS
614 
615 static TCGv_i64 load_frd(unsigned rt)
616 {
617     TCGv_i64 ret = tcg_temp_new_i64();
618     tcg_gen_ld_i64(ret, cpu_env, offsetof(CPUHPPAState, fr[rt]));
619     return ret;
620 }
621 
622 static TCGv_i64 load_frd0(unsigned rt)
623 {
624     if (rt == 0) {
625         return tcg_const_i64(0);
626     } else {
627         return load_frd(rt);
628     }
629 }
630 
631 static void save_frd(unsigned rt, TCGv_i64 val)
632 {
633     tcg_gen_st_i64(val, cpu_env, offsetof(CPUHPPAState, fr[rt]));
634 }
635 
636 static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
637 {
638 #ifdef CONFIG_USER_ONLY
639     tcg_gen_movi_i64(dest, 0);
640 #else
641     if (reg < 4) {
642         tcg_gen_mov_i64(dest, cpu_sr[reg]);
643     } else if (ctx->tb_flags & TB_FLAG_SR_SAME) {
644         tcg_gen_mov_i64(dest, cpu_srH);
645     } else {
646         tcg_gen_ld_i64(dest, cpu_env, offsetof(CPUHPPAState, sr[reg]));
647     }
648 #endif
649 }
650 
651 /* Skip over the implementation of an insn that has been nullified.
652    Use this when the insn is too complex for a conditional move.  */
653 static void nullify_over(DisasContext *ctx)
654 {
655     if (ctx->null_cond.c != TCG_COND_NEVER) {
656         /* The always condition should have been handled in the main loop.  */
657         assert(ctx->null_cond.c != TCG_COND_ALWAYS);
658 
659         ctx->null_lab = gen_new_label();
660 
661         /* If we're using PSW[N], copy it to a temp because... */
662         if (ctx->null_cond.a0 == cpu_psw_n) {
663             ctx->null_cond.a0 = tcg_temp_new();
664             tcg_gen_mov_reg(ctx->null_cond.a0, cpu_psw_n);
665         }
666         /* ... we clear it before branching over the implementation,
667            so that (1) it's clear after nullifying this insn and
668            (2) if this insn nullifies the next, PSW[N] is valid.  */
669         if (ctx->psw_n_nonzero) {
670             ctx->psw_n_nonzero = false;
671             tcg_gen_movi_reg(cpu_psw_n, 0);
672         }
673 
674         tcg_gen_brcond_reg(ctx->null_cond.c, ctx->null_cond.a0,
675                            ctx->null_cond.a1, ctx->null_lab);
676         cond_free(&ctx->null_cond);
677     }
678 }
679 
680 /* Save the current nullification state to PSW[N].  */
681 static void nullify_save(DisasContext *ctx)
682 {
683     if (ctx->null_cond.c == TCG_COND_NEVER) {
684         if (ctx->psw_n_nonzero) {
685             tcg_gen_movi_reg(cpu_psw_n, 0);
686         }
687         return;
688     }
689     if (ctx->null_cond.a0 != cpu_psw_n) {
690         tcg_gen_setcond_reg(ctx->null_cond.c, cpu_psw_n,
691                             ctx->null_cond.a0, ctx->null_cond.a1);
692         ctx->psw_n_nonzero = true;
693     }
694     cond_free(&ctx->null_cond);
695 }
696 
697 /* Set a PSW[N] to X.  The intention is that this is used immediately
698    before a goto_tb/exit_tb, so that there is no fallthru path to other
699    code within the TB.  Therefore we do not update psw_n_nonzero.  */
700 static void nullify_set(DisasContext *ctx, bool x)
701 {
702     if (ctx->psw_n_nonzero || x) {
703         tcg_gen_movi_reg(cpu_psw_n, x);
704     }
705 }
706 
707 /* Mark the end of an instruction that may have been nullified.
708    This is the pair to nullify_over.  Always returns true so that
709    it may be tail-called from a translate function.  */
710 static bool nullify_end(DisasContext *ctx)
711 {
712     TCGLabel *null_lab = ctx->null_lab;
713     DisasJumpType status = ctx->base.is_jmp;
714 
715     /* For NEXT, NORETURN, STALE, we can easily continue (or exit).
716        For UPDATED, we cannot update on the nullified path.  */
717     assert(status != DISAS_IAQ_N_UPDATED);
718 
719     if (likely(null_lab == NULL)) {
720         /* The current insn wasn't conditional or handled the condition
721            applied to it without a branch, so the (new) setting of
722            NULL_COND can be applied directly to the next insn.  */
723         return true;
724     }
725     ctx->null_lab = NULL;
726 
727     if (likely(ctx->null_cond.c == TCG_COND_NEVER)) {
728         /* The next instruction will be unconditional,
729            and NULL_COND already reflects that.  */
730         gen_set_label(null_lab);
731     } else {
732         /* The insn that we just executed is itself nullifying the next
733            instruction.  Store the condition in the PSW[N] global.
734            We asserted PSW[N] = 0 in nullify_over, so that after the
735            label we have the proper value in place.  */
736         nullify_save(ctx);
737         gen_set_label(null_lab);
738         ctx->null_cond = cond_make_n();
739     }
740     if (status == DISAS_NORETURN) {
741         ctx->base.is_jmp = DISAS_NEXT;
742     }
743     return true;
744 }
745 
746 static void copy_iaoq_entry(TCGv_reg dest, target_ureg ival, TCGv_reg vval)
747 {
748     if (unlikely(ival == -1)) {
749         tcg_gen_mov_reg(dest, vval);
750     } else {
751         tcg_gen_movi_reg(dest, ival);
752     }
753 }
754 
755 static inline target_ureg iaoq_dest(DisasContext *ctx, target_sreg disp)
756 {
757     return ctx->iaoq_f + disp + 8;
758 }
759 
760 static void gen_excp_1(int exception)
761 {
762     gen_helper_excp(cpu_env, tcg_constant_i32(exception));
763 }
764 
765 static void gen_excp(DisasContext *ctx, int exception)
766 {
767     copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
768     copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
769     nullify_save(ctx);
770     gen_excp_1(exception);
771     ctx->base.is_jmp = DISAS_NORETURN;
772 }
773 
774 static bool gen_excp_iir(DisasContext *ctx, int exc)
775 {
776     nullify_over(ctx);
777     tcg_gen_st_reg(tcg_constant_reg(ctx->insn),
778                    cpu_env, offsetof(CPUHPPAState, cr[CR_IIR]));
779     gen_excp(ctx, exc);
780     return nullify_end(ctx);
781 }
782 
783 static bool gen_illegal(DisasContext *ctx)
784 {
785     return gen_excp_iir(ctx, EXCP_ILL);
786 }
787 
788 #ifdef CONFIG_USER_ONLY
789 #define CHECK_MOST_PRIVILEGED(EXCP) \
790     return gen_excp_iir(ctx, EXCP)
791 #else
792 #define CHECK_MOST_PRIVILEGED(EXCP) \
793     do {                                     \
794         if (ctx->privilege != 0) {           \
795             return gen_excp_iir(ctx, EXCP);  \
796         }                                    \
797     } while (0)
798 #endif
799 
800 static bool use_goto_tb(DisasContext *ctx, target_ureg dest)
801 {
802     return translator_use_goto_tb(&ctx->base, dest);
803 }
804 
805 /* If the next insn is to be nullified, and it's on the same page,
806    and we're not attempting to set a breakpoint on it, then we can
807    totally skip the nullified insn.  This avoids creating and
808    executing a TB that merely branches to the next TB.  */
809 static bool use_nullify_skip(DisasContext *ctx)
810 {
811     return (((ctx->iaoq_b ^ ctx->iaoq_f) & TARGET_PAGE_MASK) == 0
812             && !cpu_breakpoint_test(ctx->cs, ctx->iaoq_b, BP_ANY));
813 }
814 
815 static void gen_goto_tb(DisasContext *ctx, int which,
816                         target_ureg f, target_ureg b)
817 {
818     if (f != -1 && b != -1 && use_goto_tb(ctx, f)) {
819         tcg_gen_goto_tb(which);
820         tcg_gen_movi_reg(cpu_iaoq_f, f);
821         tcg_gen_movi_reg(cpu_iaoq_b, b);
822         tcg_gen_exit_tb(ctx->base.tb, which);
823     } else {
824         copy_iaoq_entry(cpu_iaoq_f, f, cpu_iaoq_b);
825         copy_iaoq_entry(cpu_iaoq_b, b, ctx->iaoq_n_var);
826         tcg_gen_lookup_and_goto_ptr();
827     }
828 }
829 
830 static bool cond_need_sv(int c)
831 {
832     return c == 2 || c == 3 || c == 6;
833 }
834 
835 static bool cond_need_cb(int c)
836 {
837     return c == 4 || c == 5;
838 }
839 
840 /*
841  * Compute conditional for arithmetic.  See Page 5-3, Table 5-1, of
842  * the Parisc 1.1 Architecture Reference Manual for details.
843  */
844 
845 static DisasCond do_cond(unsigned cf, TCGv_reg res,
846                          TCGv_reg cb_msb, TCGv_reg sv)
847 {
848     DisasCond cond;
849     TCGv_reg tmp;
850 
851     switch (cf >> 1) {
852     case 0: /* Never / TR    (0 / 1) */
853         cond = cond_make_f();
854         break;
855     case 1: /* = / <>        (Z / !Z) */
856         cond = cond_make_0(TCG_COND_EQ, res);
857         break;
858     case 2: /* < / >=        (N ^ V / !(N ^ V) */
859         tmp = tcg_temp_new();
860         tcg_gen_xor_reg(tmp, res, sv);
861         cond = cond_make_0_tmp(TCG_COND_LT, tmp);
862         break;
863     case 3: /* <= / >        (N ^ V) | Z / !((N ^ V) | Z) */
864         /*
865          * Simplify:
866          *   (N ^ V) | Z
867          *   ((res < 0) ^ (sv < 0)) | !res
868          *   ((res ^ sv) < 0) | !res
869          *   (~(res ^ sv) >= 0) | !res
870          *   !(~(res ^ sv) >> 31) | !res
871          *   !(~(res ^ sv) >> 31 & res)
872          */
873         tmp = tcg_temp_new();
874         tcg_gen_eqv_reg(tmp, res, sv);
875         tcg_gen_sari_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
876         tcg_gen_and_reg(tmp, tmp, res);
877         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
878         break;
879     case 4: /* NUV / UV      (!C / C) */
880         cond = cond_make_0(TCG_COND_EQ, cb_msb);
881         break;
882     case 5: /* ZNV / VNZ     (!C | Z / C & !Z) */
883         tmp = tcg_temp_new();
884         tcg_gen_neg_reg(tmp, cb_msb);
885         tcg_gen_and_reg(tmp, tmp, res);
886         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
887         break;
888     case 6: /* SV / NSV      (V / !V) */
889         cond = cond_make_0(TCG_COND_LT, sv);
890         break;
891     case 7: /* OD / EV */
892         tmp = tcg_temp_new();
893         tcg_gen_andi_reg(tmp, res, 1);
894         cond = cond_make_0_tmp(TCG_COND_NE, tmp);
895         break;
896     default:
897         g_assert_not_reached();
898     }
899     if (cf & 1) {
900         cond.c = tcg_invert_cond(cond.c);
901     }
902 
903     return cond;
904 }
905 
906 /* Similar, but for the special case of subtraction without borrow, we
907    can use the inputs directly.  This can allow other computation to be
908    deleted as unused.  */
909 
910 static DisasCond do_sub_cond(unsigned cf, TCGv_reg res,
911                              TCGv_reg in1, TCGv_reg in2, TCGv_reg sv)
912 {
913     DisasCond cond;
914 
915     switch (cf >> 1) {
916     case 1: /* = / <> */
917         cond = cond_make(TCG_COND_EQ, in1, in2);
918         break;
919     case 2: /* < / >= */
920         cond = cond_make(TCG_COND_LT, in1, in2);
921         break;
922     case 3: /* <= / > */
923         cond = cond_make(TCG_COND_LE, in1, in2);
924         break;
925     case 4: /* << / >>= */
926         cond = cond_make(TCG_COND_LTU, in1, in2);
927         break;
928     case 5: /* <<= / >> */
929         cond = cond_make(TCG_COND_LEU, in1, in2);
930         break;
931     default:
932         return do_cond(cf, res, NULL, sv);
933     }
934     if (cf & 1) {
935         cond.c = tcg_invert_cond(cond.c);
936     }
937 
938     return cond;
939 }
940 
941 /*
942  * Similar, but for logicals, where the carry and overflow bits are not
943  * computed, and use of them is undefined.
944  *
945  * Undefined or not, hardware does not trap.  It seems reasonable to
946  * assume hardware treats cases c={4,5,6} as if C=0 & V=0, since that's
947  * how cases c={2,3} are treated.
948  */
949 
950 static DisasCond do_log_cond(unsigned cf, TCGv_reg res)
951 {
952     switch (cf) {
953     case 0:  /* never */
954     case 9:  /* undef, C */
955     case 11: /* undef, C & !Z */
956     case 12: /* undef, V */
957         return cond_make_f();
958 
959     case 1:  /* true */
960     case 8:  /* undef, !C */
961     case 10: /* undef, !C | Z */
962     case 13: /* undef, !V */
963         return cond_make_t();
964 
965     case 2:  /* == */
966         return cond_make_0(TCG_COND_EQ, res);
967     case 3:  /* <> */
968         return cond_make_0(TCG_COND_NE, res);
969     case 4:  /* < */
970         return cond_make_0(TCG_COND_LT, res);
971     case 5:  /* >= */
972         return cond_make_0(TCG_COND_GE, res);
973     case 6:  /* <= */
974         return cond_make_0(TCG_COND_LE, res);
975     case 7:  /* > */
976         return cond_make_0(TCG_COND_GT, res);
977 
978     case 14: /* OD */
979     case 15: /* EV */
980         return do_cond(cf, res, NULL, NULL);
981 
982     default:
983         g_assert_not_reached();
984     }
985 }
986 
987 /* Similar, but for shift/extract/deposit conditions.  */
988 
989 static DisasCond do_sed_cond(unsigned orig, TCGv_reg res)
990 {
991     unsigned c, f;
992 
993     /* Convert the compressed condition codes to standard.
994        0-2 are the same as logicals (nv,<,<=), while 3 is OD.
995        4-7 are the reverse of 0-3.  */
996     c = orig & 3;
997     if (c == 3) {
998         c = 7;
999     }
1000     f = (orig & 4) / 4;
1001 
1002     return do_log_cond(c * 2 + f, res);
1003 }
1004 
1005 /* Similar, but for unit conditions.  */
1006 
1007 static DisasCond do_unit_cond(unsigned cf, TCGv_reg res,
1008                               TCGv_reg in1, TCGv_reg in2)
1009 {
1010     DisasCond cond;
1011     TCGv_reg tmp, cb = NULL;
1012 
1013     if (cf & 8) {
1014         /* Since we want to test lots of carry-out bits all at once, do not
1015          * do our normal thing and compute carry-in of bit B+1 since that
1016          * leaves us with carry bits spread across two words.
1017          */
1018         cb = tcg_temp_new();
1019         tmp = tcg_temp_new();
1020         tcg_gen_or_reg(cb, in1, in2);
1021         tcg_gen_and_reg(tmp, in1, in2);
1022         tcg_gen_andc_reg(cb, cb, res);
1023         tcg_gen_or_reg(cb, cb, tmp);
1024         tcg_temp_free(tmp);
1025     }
1026 
1027     switch (cf >> 1) {
1028     case 0: /* never / TR */
1029     case 1: /* undefined */
1030     case 5: /* undefined */
1031         cond = cond_make_f();
1032         break;
1033 
1034     case 2: /* SBZ / NBZ */
1035         /* See hasless(v,1) from
1036          * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
1037          */
1038         tmp = tcg_temp_new();
1039         tcg_gen_subi_reg(tmp, res, 0x01010101u);
1040         tcg_gen_andc_reg(tmp, tmp, res);
1041         tcg_gen_andi_reg(tmp, tmp, 0x80808080u);
1042         cond = cond_make_0(TCG_COND_NE, tmp);
1043         tcg_temp_free(tmp);
1044         break;
1045 
1046     case 3: /* SHZ / NHZ */
1047         tmp = tcg_temp_new();
1048         tcg_gen_subi_reg(tmp, res, 0x00010001u);
1049         tcg_gen_andc_reg(tmp, tmp, res);
1050         tcg_gen_andi_reg(tmp, tmp, 0x80008000u);
1051         cond = cond_make_0(TCG_COND_NE, tmp);
1052         tcg_temp_free(tmp);
1053         break;
1054 
1055     case 4: /* SDC / NDC */
1056         tcg_gen_andi_reg(cb, cb, 0x88888888u);
1057         cond = cond_make_0(TCG_COND_NE, cb);
1058         break;
1059 
1060     case 6: /* SBC / NBC */
1061         tcg_gen_andi_reg(cb, cb, 0x80808080u);
1062         cond = cond_make_0(TCG_COND_NE, cb);
1063         break;
1064 
1065     case 7: /* SHC / NHC */
1066         tcg_gen_andi_reg(cb, cb, 0x80008000u);
1067         cond = cond_make_0(TCG_COND_NE, cb);
1068         break;
1069 
1070     default:
1071         g_assert_not_reached();
1072     }
1073     if (cf & 8) {
1074         tcg_temp_free(cb);
1075     }
1076     if (cf & 1) {
1077         cond.c = tcg_invert_cond(cond.c);
1078     }
1079 
1080     return cond;
1081 }
1082 
1083 /* Compute signed overflow for addition.  */
1084 static TCGv_reg do_add_sv(DisasContext *ctx, TCGv_reg res,
1085                           TCGv_reg in1, TCGv_reg in2)
1086 {
1087     TCGv_reg sv = get_temp(ctx);
1088     TCGv_reg tmp = tcg_temp_new();
1089 
1090     tcg_gen_xor_reg(sv, res, in1);
1091     tcg_gen_xor_reg(tmp, in1, in2);
1092     tcg_gen_andc_reg(sv, sv, tmp);
1093     tcg_temp_free(tmp);
1094 
1095     return sv;
1096 }
1097 
1098 /* Compute signed overflow for subtraction.  */
1099 static TCGv_reg do_sub_sv(DisasContext *ctx, TCGv_reg res,
1100                           TCGv_reg in1, TCGv_reg in2)
1101 {
1102     TCGv_reg sv = get_temp(ctx);
1103     TCGv_reg tmp = tcg_temp_new();
1104 
1105     tcg_gen_xor_reg(sv, res, in1);
1106     tcg_gen_xor_reg(tmp, in1, in2);
1107     tcg_gen_and_reg(sv, sv, tmp);
1108     tcg_temp_free(tmp);
1109 
1110     return sv;
1111 }
1112 
1113 static void do_add(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1114                    TCGv_reg in2, unsigned shift, bool is_l,
1115                    bool is_tsv, bool is_tc, bool is_c, unsigned cf)
1116 {
1117     TCGv_reg dest, cb, cb_msb, sv, tmp;
1118     unsigned c = cf >> 1;
1119     DisasCond cond;
1120 
1121     dest = tcg_temp_new();
1122     cb = NULL;
1123     cb_msb = NULL;
1124 
1125     if (shift) {
1126         tmp = get_temp(ctx);
1127         tcg_gen_shli_reg(tmp, in1, shift);
1128         in1 = tmp;
1129     }
1130 
1131     if (!is_l || cond_need_cb(c)) {
1132         TCGv_reg zero = tcg_constant_reg(0);
1133         cb_msb = get_temp(ctx);
1134         tcg_gen_add2_reg(dest, cb_msb, in1, zero, in2, zero);
1135         if (is_c) {
1136             tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cpu_psw_cb_msb, zero);
1137         }
1138         if (!is_l) {
1139             cb = get_temp(ctx);
1140             tcg_gen_xor_reg(cb, in1, in2);
1141             tcg_gen_xor_reg(cb, cb, dest);
1142         }
1143     } else {
1144         tcg_gen_add_reg(dest, in1, in2);
1145         if (is_c) {
1146             tcg_gen_add_reg(dest, dest, cpu_psw_cb_msb);
1147         }
1148     }
1149 
1150     /* Compute signed overflow if required.  */
1151     sv = NULL;
1152     if (is_tsv || cond_need_sv(c)) {
1153         sv = do_add_sv(ctx, dest, in1, in2);
1154         if (is_tsv) {
1155             /* ??? Need to include overflow from shift.  */
1156             gen_helper_tsv(cpu_env, sv);
1157         }
1158     }
1159 
1160     /* Emit any conditional trap before any writeback.  */
1161     cond = do_cond(cf, dest, cb_msb, sv);
1162     if (is_tc) {
1163         tmp = tcg_temp_new();
1164         tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1165         gen_helper_tcond(cpu_env, tmp);
1166         tcg_temp_free(tmp);
1167     }
1168 
1169     /* Write back the result.  */
1170     if (!is_l) {
1171         save_or_nullify(ctx, cpu_psw_cb, cb);
1172         save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1173     }
1174     save_gpr(ctx, rt, dest);
1175     tcg_temp_free(dest);
1176 
1177     /* Install the new nullification.  */
1178     cond_free(&ctx->null_cond);
1179     ctx->null_cond = cond;
1180 }
1181 
1182 static bool do_add_reg(DisasContext *ctx, arg_rrr_cf_sh *a,
1183                        bool is_l, bool is_tsv, bool is_tc, bool is_c)
1184 {
1185     TCGv_reg tcg_r1, tcg_r2;
1186 
1187     if (a->cf) {
1188         nullify_over(ctx);
1189     }
1190     tcg_r1 = load_gpr(ctx, a->r1);
1191     tcg_r2 = load_gpr(ctx, a->r2);
1192     do_add(ctx, a->t, tcg_r1, tcg_r2, a->sh, is_l, is_tsv, is_tc, is_c, a->cf);
1193     return nullify_end(ctx);
1194 }
1195 
1196 static bool do_add_imm(DisasContext *ctx, arg_rri_cf *a,
1197                        bool is_tsv, bool is_tc)
1198 {
1199     TCGv_reg tcg_im, tcg_r2;
1200 
1201     if (a->cf) {
1202         nullify_over(ctx);
1203     }
1204     tcg_im = load_const(ctx, a->i);
1205     tcg_r2 = load_gpr(ctx, a->r);
1206     do_add(ctx, a->t, tcg_im, tcg_r2, 0, 0, is_tsv, is_tc, 0, a->cf);
1207     return nullify_end(ctx);
1208 }
1209 
1210 static void do_sub(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1211                    TCGv_reg in2, bool is_tsv, bool is_b,
1212                    bool is_tc, unsigned cf)
1213 {
1214     TCGv_reg dest, sv, cb, cb_msb, zero, tmp;
1215     unsigned c = cf >> 1;
1216     DisasCond cond;
1217 
1218     dest = tcg_temp_new();
1219     cb = tcg_temp_new();
1220     cb_msb = tcg_temp_new();
1221 
1222     zero = tcg_constant_reg(0);
1223     if (is_b) {
1224         /* DEST,C = IN1 + ~IN2 + C.  */
1225         tcg_gen_not_reg(cb, in2);
1226         tcg_gen_add2_reg(dest, cb_msb, in1, zero, cpu_psw_cb_msb, zero);
1227         tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cb, zero);
1228         tcg_gen_xor_reg(cb, cb, in1);
1229         tcg_gen_xor_reg(cb, cb, dest);
1230     } else {
1231         /* DEST,C = IN1 + ~IN2 + 1.  We can produce the same result in fewer
1232            operations by seeding the high word with 1 and subtracting.  */
1233         tcg_gen_movi_reg(cb_msb, 1);
1234         tcg_gen_sub2_reg(dest, cb_msb, in1, cb_msb, in2, zero);
1235         tcg_gen_eqv_reg(cb, in1, in2);
1236         tcg_gen_xor_reg(cb, cb, dest);
1237     }
1238 
1239     /* Compute signed overflow if required.  */
1240     sv = NULL;
1241     if (is_tsv || cond_need_sv(c)) {
1242         sv = do_sub_sv(ctx, dest, in1, in2);
1243         if (is_tsv) {
1244             gen_helper_tsv(cpu_env, sv);
1245         }
1246     }
1247 
1248     /* Compute the condition.  We cannot use the special case for borrow.  */
1249     if (!is_b) {
1250         cond = do_sub_cond(cf, dest, in1, in2, sv);
1251     } else {
1252         cond = do_cond(cf, dest, cb_msb, sv);
1253     }
1254 
1255     /* Emit any conditional trap before any writeback.  */
1256     if (is_tc) {
1257         tmp = tcg_temp_new();
1258         tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1259         gen_helper_tcond(cpu_env, tmp);
1260         tcg_temp_free(tmp);
1261     }
1262 
1263     /* Write back the result.  */
1264     save_or_nullify(ctx, cpu_psw_cb, cb);
1265     save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1266     save_gpr(ctx, rt, dest);
1267     tcg_temp_free(dest);
1268     tcg_temp_free(cb);
1269     tcg_temp_free(cb_msb);
1270 
1271     /* Install the new nullification.  */
1272     cond_free(&ctx->null_cond);
1273     ctx->null_cond = cond;
1274 }
1275 
1276 static bool do_sub_reg(DisasContext *ctx, arg_rrr_cf *a,
1277                        bool is_tsv, bool is_b, bool is_tc)
1278 {
1279     TCGv_reg tcg_r1, tcg_r2;
1280 
1281     if (a->cf) {
1282         nullify_over(ctx);
1283     }
1284     tcg_r1 = load_gpr(ctx, a->r1);
1285     tcg_r2 = load_gpr(ctx, a->r2);
1286     do_sub(ctx, a->t, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, a->cf);
1287     return nullify_end(ctx);
1288 }
1289 
1290 static bool do_sub_imm(DisasContext *ctx, arg_rri_cf *a, bool is_tsv)
1291 {
1292     TCGv_reg tcg_im, tcg_r2;
1293 
1294     if (a->cf) {
1295         nullify_over(ctx);
1296     }
1297     tcg_im = load_const(ctx, a->i);
1298     tcg_r2 = load_gpr(ctx, a->r);
1299     do_sub(ctx, a->t, tcg_im, tcg_r2, is_tsv, 0, 0, a->cf);
1300     return nullify_end(ctx);
1301 }
1302 
1303 static void do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1304                       TCGv_reg in2, unsigned cf)
1305 {
1306     TCGv_reg dest, sv;
1307     DisasCond cond;
1308 
1309     dest = tcg_temp_new();
1310     tcg_gen_sub_reg(dest, in1, in2);
1311 
1312     /* Compute signed overflow if required.  */
1313     sv = NULL;
1314     if (cond_need_sv(cf >> 1)) {
1315         sv = do_sub_sv(ctx, dest, in1, in2);
1316     }
1317 
1318     /* Form the condition for the compare.  */
1319     cond = do_sub_cond(cf, dest, in1, in2, sv);
1320 
1321     /* Clear.  */
1322     tcg_gen_movi_reg(dest, 0);
1323     save_gpr(ctx, rt, dest);
1324     tcg_temp_free(dest);
1325 
1326     /* Install the new nullification.  */
1327     cond_free(&ctx->null_cond);
1328     ctx->null_cond = cond;
1329 }
1330 
1331 static void do_log(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1332                    TCGv_reg in2, unsigned cf,
1333                    void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1334 {
1335     TCGv_reg dest = dest_gpr(ctx, rt);
1336 
1337     /* Perform the operation, and writeback.  */
1338     fn(dest, in1, in2);
1339     save_gpr(ctx, rt, dest);
1340 
1341     /* Install the new nullification.  */
1342     cond_free(&ctx->null_cond);
1343     if (cf) {
1344         ctx->null_cond = do_log_cond(cf, dest);
1345     }
1346 }
1347 
1348 static bool do_log_reg(DisasContext *ctx, arg_rrr_cf *a,
1349                        void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1350 {
1351     TCGv_reg tcg_r1, tcg_r2;
1352 
1353     if (a->cf) {
1354         nullify_over(ctx);
1355     }
1356     tcg_r1 = load_gpr(ctx, a->r1);
1357     tcg_r2 = load_gpr(ctx, a->r2);
1358     do_log(ctx, a->t, tcg_r1, tcg_r2, a->cf, fn);
1359     return nullify_end(ctx);
1360 }
1361 
1362 static void do_unit(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1363                     TCGv_reg in2, unsigned cf, bool is_tc,
1364                     void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1365 {
1366     TCGv_reg dest;
1367     DisasCond cond;
1368 
1369     if (cf == 0) {
1370         dest = dest_gpr(ctx, rt);
1371         fn(dest, in1, in2);
1372         save_gpr(ctx, rt, dest);
1373         cond_free(&ctx->null_cond);
1374     } else {
1375         dest = tcg_temp_new();
1376         fn(dest, in1, in2);
1377 
1378         cond = do_unit_cond(cf, dest, in1, in2);
1379 
1380         if (is_tc) {
1381             TCGv_reg tmp = tcg_temp_new();
1382             tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1383             gen_helper_tcond(cpu_env, tmp);
1384             tcg_temp_free(tmp);
1385         }
1386         save_gpr(ctx, rt, dest);
1387 
1388         cond_free(&ctx->null_cond);
1389         ctx->null_cond = cond;
1390     }
1391 }
1392 
1393 #ifndef CONFIG_USER_ONLY
1394 /* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
1395    from the top 2 bits of the base register.  There are a few system
1396    instructions that have a 3-bit space specifier, for which SR0 is
1397    not special.  To handle this, pass ~SP.  */
1398 static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_reg base)
1399 {
1400     TCGv_ptr ptr;
1401     TCGv_reg tmp;
1402     TCGv_i64 spc;
1403 
1404     if (sp != 0) {
1405         if (sp < 0) {
1406             sp = ~sp;
1407         }
1408         spc = get_temp_tl(ctx);
1409         load_spr(ctx, spc, sp);
1410         return spc;
1411     }
1412     if (ctx->tb_flags & TB_FLAG_SR_SAME) {
1413         return cpu_srH;
1414     }
1415 
1416     ptr = tcg_temp_new_ptr();
1417     tmp = tcg_temp_new();
1418     spc = get_temp_tl(ctx);
1419 
1420     tcg_gen_shri_reg(tmp, base, TARGET_REGISTER_BITS - 5);
1421     tcg_gen_andi_reg(tmp, tmp, 030);
1422     tcg_gen_trunc_reg_ptr(ptr, tmp);
1423     tcg_temp_free(tmp);
1424 
1425     tcg_gen_add_ptr(ptr, ptr, cpu_env);
1426     tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
1427     tcg_temp_free_ptr(ptr);
1428 
1429     return spc;
1430 }
1431 #endif
1432 
1433 static void form_gva(DisasContext *ctx, TCGv_tl *pgva, TCGv_reg *pofs,
1434                      unsigned rb, unsigned rx, int scale, target_sreg disp,
1435                      unsigned sp, int modify, bool is_phys)
1436 {
1437     TCGv_reg base = load_gpr(ctx, rb);
1438     TCGv_reg ofs;
1439 
1440     /* Note that RX is mutually exclusive with DISP.  */
1441     if (rx) {
1442         ofs = get_temp(ctx);
1443         tcg_gen_shli_reg(ofs, cpu_gr[rx], scale);
1444         tcg_gen_add_reg(ofs, ofs, base);
1445     } else if (disp || modify) {
1446         ofs = get_temp(ctx);
1447         tcg_gen_addi_reg(ofs, base, disp);
1448     } else {
1449         ofs = base;
1450     }
1451 
1452     *pofs = ofs;
1453 #ifdef CONFIG_USER_ONLY
1454     *pgva = (modify <= 0 ? ofs : base);
1455 #else
1456     TCGv_tl addr = get_temp_tl(ctx);
1457     tcg_gen_extu_reg_tl(addr, modify <= 0 ? ofs : base);
1458     if (ctx->tb_flags & PSW_W) {
1459         tcg_gen_andi_tl(addr, addr, 0x3fffffffffffffffull);
1460     }
1461     if (!is_phys) {
1462         tcg_gen_or_tl(addr, addr, space_select(ctx, sp, base));
1463     }
1464     *pgva = addr;
1465 #endif
1466 }
1467 
1468 /* Emit a memory load.  The modify parameter should be
1469  * < 0 for pre-modify,
1470  * > 0 for post-modify,
1471  * = 0 for no base register update.
1472  */
1473 static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1474                        unsigned rx, int scale, target_sreg disp,
1475                        unsigned sp, int modify, MemOp mop)
1476 {
1477     TCGv_reg ofs;
1478     TCGv_tl addr;
1479 
1480     /* Caller uses nullify_over/nullify_end.  */
1481     assert(ctx->null_cond.c == TCG_COND_NEVER);
1482 
1483     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1484              ctx->mmu_idx == MMU_PHYS_IDX);
1485     tcg_gen_qemu_ld_reg(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
1486     if (modify) {
1487         save_gpr(ctx, rb, ofs);
1488     }
1489 }
1490 
1491 static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1492                        unsigned rx, int scale, target_sreg disp,
1493                        unsigned sp, int modify, MemOp mop)
1494 {
1495     TCGv_reg ofs;
1496     TCGv_tl addr;
1497 
1498     /* Caller uses nullify_over/nullify_end.  */
1499     assert(ctx->null_cond.c == TCG_COND_NEVER);
1500 
1501     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1502              ctx->mmu_idx == MMU_PHYS_IDX);
1503     tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
1504     if (modify) {
1505         save_gpr(ctx, rb, ofs);
1506     }
1507 }
1508 
1509 static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1510                         unsigned rx, int scale, target_sreg disp,
1511                         unsigned sp, int modify, MemOp mop)
1512 {
1513     TCGv_reg ofs;
1514     TCGv_tl addr;
1515 
1516     /* Caller uses nullify_over/nullify_end.  */
1517     assert(ctx->null_cond.c == TCG_COND_NEVER);
1518 
1519     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1520              ctx->mmu_idx == MMU_PHYS_IDX);
1521     tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
1522     if (modify) {
1523         save_gpr(ctx, rb, ofs);
1524     }
1525 }
1526 
1527 static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1528                         unsigned rx, int scale, target_sreg disp,
1529                         unsigned sp, int modify, MemOp mop)
1530 {
1531     TCGv_reg ofs;
1532     TCGv_tl addr;
1533 
1534     /* Caller uses nullify_over/nullify_end.  */
1535     assert(ctx->null_cond.c == TCG_COND_NEVER);
1536 
1537     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1538              ctx->mmu_idx == MMU_PHYS_IDX);
1539     tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
1540     if (modify) {
1541         save_gpr(ctx, rb, ofs);
1542     }
1543 }
1544 
1545 #if TARGET_REGISTER_BITS == 64
1546 #define do_load_reg   do_load_64
1547 #define do_store_reg  do_store_64
1548 #else
1549 #define do_load_reg   do_load_32
1550 #define do_store_reg  do_store_32
1551 #endif
1552 
1553 static bool do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1554                     unsigned rx, int scale, target_sreg disp,
1555                     unsigned sp, int modify, MemOp mop)
1556 {
1557     TCGv_reg dest;
1558 
1559     nullify_over(ctx);
1560 
1561     if (modify == 0) {
1562         /* No base register update.  */
1563         dest = dest_gpr(ctx, rt);
1564     } else {
1565         /* Make sure if RT == RB, we see the result of the load.  */
1566         dest = get_temp(ctx);
1567     }
1568     do_load_reg(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
1569     save_gpr(ctx, rt, dest);
1570 
1571     return nullify_end(ctx);
1572 }
1573 
1574 static bool do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1575                       unsigned rx, int scale, target_sreg disp,
1576                       unsigned sp, int modify)
1577 {
1578     TCGv_i32 tmp;
1579 
1580     nullify_over(ctx);
1581 
1582     tmp = tcg_temp_new_i32();
1583     do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1584     save_frw_i32(rt, tmp);
1585     tcg_temp_free_i32(tmp);
1586 
1587     if (rt == 0) {
1588         gen_helper_loaded_fr0(cpu_env);
1589     }
1590 
1591     return nullify_end(ctx);
1592 }
1593 
1594 static bool trans_fldw(DisasContext *ctx, arg_ldst *a)
1595 {
1596     return do_floadw(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1597                      a->disp, a->sp, a->m);
1598 }
1599 
1600 static bool do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1601                       unsigned rx, int scale, target_sreg disp,
1602                       unsigned sp, int modify)
1603 {
1604     TCGv_i64 tmp;
1605 
1606     nullify_over(ctx);
1607 
1608     tmp = tcg_temp_new_i64();
1609     do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
1610     save_frd(rt, tmp);
1611     tcg_temp_free_i64(tmp);
1612 
1613     if (rt == 0) {
1614         gen_helper_loaded_fr0(cpu_env);
1615     }
1616 
1617     return nullify_end(ctx);
1618 }
1619 
1620 static bool trans_fldd(DisasContext *ctx, arg_ldst *a)
1621 {
1622     return do_floadd(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1623                      a->disp, a->sp, a->m);
1624 }
1625 
1626 static bool do_store(DisasContext *ctx, unsigned rt, unsigned rb,
1627                      target_sreg disp, unsigned sp,
1628                      int modify, MemOp mop)
1629 {
1630     nullify_over(ctx);
1631     do_store_reg(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
1632     return nullify_end(ctx);
1633 }
1634 
1635 static bool do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1636                        unsigned rx, int scale, target_sreg disp,
1637                        unsigned sp, int modify)
1638 {
1639     TCGv_i32 tmp;
1640 
1641     nullify_over(ctx);
1642 
1643     tmp = load_frw_i32(rt);
1644     do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1645     tcg_temp_free_i32(tmp);
1646 
1647     return nullify_end(ctx);
1648 }
1649 
1650 static bool trans_fstw(DisasContext *ctx, arg_ldst *a)
1651 {
1652     return do_fstorew(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1653                       a->disp, a->sp, a->m);
1654 }
1655 
1656 static bool do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1657                        unsigned rx, int scale, target_sreg disp,
1658                        unsigned sp, int modify)
1659 {
1660     TCGv_i64 tmp;
1661 
1662     nullify_over(ctx);
1663 
1664     tmp = load_frd(rt);
1665     do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
1666     tcg_temp_free_i64(tmp);
1667 
1668     return nullify_end(ctx);
1669 }
1670 
1671 static bool trans_fstd(DisasContext *ctx, arg_ldst *a)
1672 {
1673     return do_fstored(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1674                       a->disp, a->sp, a->m);
1675 }
1676 
1677 static bool do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
1678                        void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1679 {
1680     TCGv_i32 tmp;
1681 
1682     nullify_over(ctx);
1683     tmp = load_frw0_i32(ra);
1684 
1685     func(tmp, cpu_env, tmp);
1686 
1687     save_frw_i32(rt, tmp);
1688     tcg_temp_free_i32(tmp);
1689     return nullify_end(ctx);
1690 }
1691 
1692 static bool do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
1693                        void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1694 {
1695     TCGv_i32 dst;
1696     TCGv_i64 src;
1697 
1698     nullify_over(ctx);
1699     src = load_frd(ra);
1700     dst = tcg_temp_new_i32();
1701 
1702     func(dst, cpu_env, src);
1703 
1704     tcg_temp_free_i64(src);
1705     save_frw_i32(rt, dst);
1706     tcg_temp_free_i32(dst);
1707     return nullify_end(ctx);
1708 }
1709 
1710 static bool do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
1711                        void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1712 {
1713     TCGv_i64 tmp;
1714 
1715     nullify_over(ctx);
1716     tmp = load_frd0(ra);
1717 
1718     func(tmp, cpu_env, tmp);
1719 
1720     save_frd(rt, tmp);
1721     tcg_temp_free_i64(tmp);
1722     return nullify_end(ctx);
1723 }
1724 
1725 static bool do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
1726                        void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1727 {
1728     TCGv_i32 src;
1729     TCGv_i64 dst;
1730 
1731     nullify_over(ctx);
1732     src = load_frw0_i32(ra);
1733     dst = tcg_temp_new_i64();
1734 
1735     func(dst, cpu_env, src);
1736 
1737     tcg_temp_free_i32(src);
1738     save_frd(rt, dst);
1739     tcg_temp_free_i64(dst);
1740     return nullify_end(ctx);
1741 }
1742 
1743 static bool do_fop_weww(DisasContext *ctx, unsigned rt,
1744                         unsigned ra, unsigned rb,
1745                         void (*func)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32))
1746 {
1747     TCGv_i32 a, b;
1748 
1749     nullify_over(ctx);
1750     a = load_frw0_i32(ra);
1751     b = load_frw0_i32(rb);
1752 
1753     func(a, cpu_env, a, b);
1754 
1755     tcg_temp_free_i32(b);
1756     save_frw_i32(rt, a);
1757     tcg_temp_free_i32(a);
1758     return nullify_end(ctx);
1759 }
1760 
1761 static bool do_fop_dedd(DisasContext *ctx, unsigned rt,
1762                         unsigned ra, unsigned rb,
1763                         void (*func)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1764 {
1765     TCGv_i64 a, b;
1766 
1767     nullify_over(ctx);
1768     a = load_frd0(ra);
1769     b = load_frd0(rb);
1770 
1771     func(a, cpu_env, a, b);
1772 
1773     tcg_temp_free_i64(b);
1774     save_frd(rt, a);
1775     tcg_temp_free_i64(a);
1776     return nullify_end(ctx);
1777 }
1778 
1779 /* Emit an unconditional branch to a direct target, which may or may not
1780    have already had nullification handled.  */
1781 static bool do_dbranch(DisasContext *ctx, target_ureg dest,
1782                        unsigned link, bool is_n)
1783 {
1784     if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
1785         if (link != 0) {
1786             copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1787         }
1788         ctx->iaoq_n = dest;
1789         if (is_n) {
1790             ctx->null_cond.c = TCG_COND_ALWAYS;
1791         }
1792     } else {
1793         nullify_over(ctx);
1794 
1795         if (link != 0) {
1796             copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1797         }
1798 
1799         if (is_n && use_nullify_skip(ctx)) {
1800             nullify_set(ctx, 0);
1801             gen_goto_tb(ctx, 0, dest, dest + 4);
1802         } else {
1803             nullify_set(ctx, is_n);
1804             gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
1805         }
1806 
1807         nullify_end(ctx);
1808 
1809         nullify_set(ctx, 0);
1810         gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
1811         ctx->base.is_jmp = DISAS_NORETURN;
1812     }
1813     return true;
1814 }
1815 
1816 /* Emit a conditional branch to a direct target.  If the branch itself
1817    is nullified, we should have already used nullify_over.  */
1818 static bool do_cbranch(DisasContext *ctx, target_sreg disp, bool is_n,
1819                        DisasCond *cond)
1820 {
1821     target_ureg dest = iaoq_dest(ctx, disp);
1822     TCGLabel *taken = NULL;
1823     TCGCond c = cond->c;
1824     bool n;
1825 
1826     assert(ctx->null_cond.c == TCG_COND_NEVER);
1827 
1828     /* Handle TRUE and NEVER as direct branches.  */
1829     if (c == TCG_COND_ALWAYS) {
1830         return do_dbranch(ctx, dest, 0, is_n && disp >= 0);
1831     }
1832     if (c == TCG_COND_NEVER) {
1833         return do_dbranch(ctx, ctx->iaoq_n, 0, is_n && disp < 0);
1834     }
1835 
1836     taken = gen_new_label();
1837     tcg_gen_brcond_reg(c, cond->a0, cond->a1, taken);
1838     cond_free(cond);
1839 
1840     /* Not taken: Condition not satisfied; nullify on backward branches. */
1841     n = is_n && disp < 0;
1842     if (n && use_nullify_skip(ctx)) {
1843         nullify_set(ctx, 0);
1844         gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
1845     } else {
1846         if (!n && ctx->null_lab) {
1847             gen_set_label(ctx->null_lab);
1848             ctx->null_lab = NULL;
1849         }
1850         nullify_set(ctx, n);
1851         if (ctx->iaoq_n == -1) {
1852             /* The temporary iaoq_n_var died at the branch above.
1853                Regenerate it here instead of saving it.  */
1854             tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
1855         }
1856         gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
1857     }
1858 
1859     gen_set_label(taken);
1860 
1861     /* Taken: Condition satisfied; nullify on forward branches.  */
1862     n = is_n && disp >= 0;
1863     if (n && use_nullify_skip(ctx)) {
1864         nullify_set(ctx, 0);
1865         gen_goto_tb(ctx, 1, dest, dest + 4);
1866     } else {
1867         nullify_set(ctx, n);
1868         gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
1869     }
1870 
1871     /* Not taken: the branch itself was nullified.  */
1872     if (ctx->null_lab) {
1873         gen_set_label(ctx->null_lab);
1874         ctx->null_lab = NULL;
1875         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
1876     } else {
1877         ctx->base.is_jmp = DISAS_NORETURN;
1878     }
1879     return true;
1880 }
1881 
1882 /* Emit an unconditional branch to an indirect target.  This handles
1883    nullification of the branch itself.  */
1884 static bool do_ibranch(DisasContext *ctx, TCGv_reg dest,
1885                        unsigned link, bool is_n)
1886 {
1887     TCGv_reg a0, a1, next, tmp;
1888     TCGCond c;
1889 
1890     assert(ctx->null_lab == NULL);
1891 
1892     if (ctx->null_cond.c == TCG_COND_NEVER) {
1893         if (link != 0) {
1894             copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1895         }
1896         next = get_temp(ctx);
1897         tcg_gen_mov_reg(next, dest);
1898         if (is_n) {
1899             if (use_nullify_skip(ctx)) {
1900                 tcg_gen_mov_reg(cpu_iaoq_f, next);
1901                 tcg_gen_addi_reg(cpu_iaoq_b, next, 4);
1902                 nullify_set(ctx, 0);
1903                 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
1904                 return true;
1905             }
1906             ctx->null_cond.c = TCG_COND_ALWAYS;
1907         }
1908         ctx->iaoq_n = -1;
1909         ctx->iaoq_n_var = next;
1910     } else if (is_n && use_nullify_skip(ctx)) {
1911         /* The (conditional) branch, B, nullifies the next insn, N,
1912            and we're allowed to skip execution N (no single-step or
1913            tracepoint in effect).  Since the goto_ptr that we must use
1914            for the indirect branch consumes no special resources, we
1915            can (conditionally) skip B and continue execution.  */
1916         /* The use_nullify_skip test implies we have a known control path.  */
1917         tcg_debug_assert(ctx->iaoq_b != -1);
1918         tcg_debug_assert(ctx->iaoq_n != -1);
1919 
1920         /* We do have to handle the non-local temporary, DEST, before
1921            branching.  Since IOAQ_F is not really live at this point, we
1922            can simply store DEST optimistically.  Similarly with IAOQ_B.  */
1923         tcg_gen_mov_reg(cpu_iaoq_f, dest);
1924         tcg_gen_addi_reg(cpu_iaoq_b, dest, 4);
1925 
1926         nullify_over(ctx);
1927         if (link != 0) {
1928             tcg_gen_movi_reg(cpu_gr[link], ctx->iaoq_n);
1929         }
1930         tcg_gen_lookup_and_goto_ptr();
1931         return nullify_end(ctx);
1932     } else {
1933         c = ctx->null_cond.c;
1934         a0 = ctx->null_cond.a0;
1935         a1 = ctx->null_cond.a1;
1936 
1937         tmp = tcg_temp_new();
1938         next = get_temp(ctx);
1939 
1940         copy_iaoq_entry(tmp, ctx->iaoq_n, ctx->iaoq_n_var);
1941         tcg_gen_movcond_reg(c, next, a0, a1, tmp, dest);
1942         ctx->iaoq_n = -1;
1943         ctx->iaoq_n_var = next;
1944 
1945         if (link != 0) {
1946             tcg_gen_movcond_reg(c, cpu_gr[link], a0, a1, cpu_gr[link], tmp);
1947         }
1948 
1949         if (is_n) {
1950             /* The branch nullifies the next insn, which means the state of N
1951                after the branch is the inverse of the state of N that applied
1952                to the branch.  */
1953             tcg_gen_setcond_reg(tcg_invert_cond(c), cpu_psw_n, a0, a1);
1954             cond_free(&ctx->null_cond);
1955             ctx->null_cond = cond_make_n();
1956             ctx->psw_n_nonzero = true;
1957         } else {
1958             cond_free(&ctx->null_cond);
1959         }
1960     }
1961     return true;
1962 }
1963 
1964 /* Implement
1965  *    if (IAOQ_Front{30..31} < GR[b]{30..31})
1966  *      IAOQ_Next{30..31} ← GR[b]{30..31};
1967  *    else
1968  *      IAOQ_Next{30..31} ← IAOQ_Front{30..31};
1969  * which keeps the privilege level from being increased.
1970  */
1971 static TCGv_reg do_ibranch_priv(DisasContext *ctx, TCGv_reg offset)
1972 {
1973     TCGv_reg dest;
1974     switch (ctx->privilege) {
1975     case 0:
1976         /* Privilege 0 is maximum and is allowed to decrease.  */
1977         return offset;
1978     case 3:
1979         /* Privilege 3 is minimum and is never allowed to increase.  */
1980         dest = get_temp(ctx);
1981         tcg_gen_ori_reg(dest, offset, 3);
1982         break;
1983     default:
1984         dest = get_temp(ctx);
1985         tcg_gen_andi_reg(dest, offset, -4);
1986         tcg_gen_ori_reg(dest, dest, ctx->privilege);
1987         tcg_gen_movcond_reg(TCG_COND_GTU, dest, dest, offset, dest, offset);
1988         break;
1989     }
1990     return dest;
1991 }
1992 
1993 #ifdef CONFIG_USER_ONLY
1994 /* On Linux, page zero is normally marked execute only + gateway.
1995    Therefore normal read or write is supposed to fail, but specific
1996    offsets have kernel code mapped to raise permissions to implement
1997    system calls.  Handling this via an explicit check here, rather
1998    in than the "be disp(sr2,r0)" instruction that probably sent us
1999    here, is the easiest way to handle the branch delay slot on the
2000    aforementioned BE.  */
2001 static void do_page_zero(DisasContext *ctx)
2002 {
2003     /* If by some means we get here with PSW[N]=1, that implies that
2004        the B,GATE instruction would be skipped, and we'd fault on the
2005        next insn within the privilaged page.  */
2006     switch (ctx->null_cond.c) {
2007     case TCG_COND_NEVER:
2008         break;
2009     case TCG_COND_ALWAYS:
2010         tcg_gen_movi_reg(cpu_psw_n, 0);
2011         goto do_sigill;
2012     default:
2013         /* Since this is always the first (and only) insn within the
2014            TB, we should know the state of PSW[N] from TB->FLAGS.  */
2015         g_assert_not_reached();
2016     }
2017 
2018     /* Check that we didn't arrive here via some means that allowed
2019        non-sequential instruction execution.  Normally the PSW[B] bit
2020        detects this by disallowing the B,GATE instruction to execute
2021        under such conditions.  */
2022     if (ctx->iaoq_b != ctx->iaoq_f + 4) {
2023         goto do_sigill;
2024     }
2025 
2026     switch (ctx->iaoq_f & -4) {
2027     case 0x00: /* Null pointer call */
2028         gen_excp_1(EXCP_IMP);
2029         ctx->base.is_jmp = DISAS_NORETURN;
2030         break;
2031 
2032     case 0xb0: /* LWS */
2033         gen_excp_1(EXCP_SYSCALL_LWS);
2034         ctx->base.is_jmp = DISAS_NORETURN;
2035         break;
2036 
2037     case 0xe0: /* SET_THREAD_POINTER */
2038         tcg_gen_st_reg(cpu_gr[26], cpu_env, offsetof(CPUHPPAState, cr[27]));
2039         tcg_gen_ori_reg(cpu_iaoq_f, cpu_gr[31], 3);
2040         tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
2041         ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
2042         break;
2043 
2044     case 0x100: /* SYSCALL */
2045         gen_excp_1(EXCP_SYSCALL);
2046         ctx->base.is_jmp = DISAS_NORETURN;
2047         break;
2048 
2049     default:
2050     do_sigill:
2051         gen_excp_1(EXCP_ILL);
2052         ctx->base.is_jmp = DISAS_NORETURN;
2053         break;
2054     }
2055 }
2056 #endif
2057 
2058 static bool trans_nop(DisasContext *ctx, arg_nop *a)
2059 {
2060     cond_free(&ctx->null_cond);
2061     return true;
2062 }
2063 
2064 static bool trans_break(DisasContext *ctx, arg_break *a)
2065 {
2066     return gen_excp_iir(ctx, EXCP_BREAK);
2067 }
2068 
2069 static bool trans_sync(DisasContext *ctx, arg_sync *a)
2070 {
2071     /* No point in nullifying the memory barrier.  */
2072     tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
2073 
2074     cond_free(&ctx->null_cond);
2075     return true;
2076 }
2077 
2078 static bool trans_mfia(DisasContext *ctx, arg_mfia *a)
2079 {
2080     unsigned rt = a->t;
2081     TCGv_reg tmp = dest_gpr(ctx, rt);
2082     tcg_gen_movi_reg(tmp, ctx->iaoq_f);
2083     save_gpr(ctx, rt, tmp);
2084 
2085     cond_free(&ctx->null_cond);
2086     return true;
2087 }
2088 
2089 static bool trans_mfsp(DisasContext *ctx, arg_mfsp *a)
2090 {
2091     unsigned rt = a->t;
2092     unsigned rs = a->sp;
2093     TCGv_i64 t0 = tcg_temp_new_i64();
2094     TCGv_reg t1 = tcg_temp_new();
2095 
2096     load_spr(ctx, t0, rs);
2097     tcg_gen_shri_i64(t0, t0, 32);
2098     tcg_gen_trunc_i64_reg(t1, t0);
2099 
2100     save_gpr(ctx, rt, t1);
2101     tcg_temp_free(t1);
2102     tcg_temp_free_i64(t0);
2103 
2104     cond_free(&ctx->null_cond);
2105     return true;
2106 }
2107 
2108 static bool trans_mfctl(DisasContext *ctx, arg_mfctl *a)
2109 {
2110     unsigned rt = a->t;
2111     unsigned ctl = a->r;
2112     TCGv_reg tmp;
2113 
2114     switch (ctl) {
2115     case CR_SAR:
2116 #ifdef TARGET_HPPA64
2117         if (a->e == 0) {
2118             /* MFSAR without ,W masks low 5 bits.  */
2119             tmp = dest_gpr(ctx, rt);
2120             tcg_gen_andi_reg(tmp, cpu_sar, 31);
2121             save_gpr(ctx, rt, tmp);
2122             goto done;
2123         }
2124 #endif
2125         save_gpr(ctx, rt, cpu_sar);
2126         goto done;
2127     case CR_IT: /* Interval Timer */
2128         /* FIXME: Respect PSW_S bit.  */
2129         nullify_over(ctx);
2130         tmp = dest_gpr(ctx, rt);
2131         if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
2132             gen_io_start();
2133             gen_helper_read_interval_timer(tmp);
2134             ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2135         } else {
2136             gen_helper_read_interval_timer(tmp);
2137         }
2138         save_gpr(ctx, rt, tmp);
2139         return nullify_end(ctx);
2140     case 26:
2141     case 27:
2142         break;
2143     default:
2144         /* All other control registers are privileged.  */
2145         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2146         break;
2147     }
2148 
2149     tmp = get_temp(ctx);
2150     tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2151     save_gpr(ctx, rt, tmp);
2152 
2153  done:
2154     cond_free(&ctx->null_cond);
2155     return true;
2156 }
2157 
2158 static bool trans_mtsp(DisasContext *ctx, arg_mtsp *a)
2159 {
2160     unsigned rr = a->r;
2161     unsigned rs = a->sp;
2162     TCGv_i64 t64;
2163 
2164     if (rs >= 5) {
2165         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2166     }
2167     nullify_over(ctx);
2168 
2169     t64 = tcg_temp_new_i64();
2170     tcg_gen_extu_reg_i64(t64, load_gpr(ctx, rr));
2171     tcg_gen_shli_i64(t64, t64, 32);
2172 
2173     if (rs >= 4) {
2174         tcg_gen_st_i64(t64, cpu_env, offsetof(CPUHPPAState, sr[rs]));
2175         ctx->tb_flags &= ~TB_FLAG_SR_SAME;
2176     } else {
2177         tcg_gen_mov_i64(cpu_sr[rs], t64);
2178     }
2179     tcg_temp_free_i64(t64);
2180 
2181     return nullify_end(ctx);
2182 }
2183 
2184 static bool trans_mtctl(DisasContext *ctx, arg_mtctl *a)
2185 {
2186     unsigned ctl = a->t;
2187     TCGv_reg reg;
2188     TCGv_reg tmp;
2189 
2190     if (ctl == CR_SAR) {
2191         reg = load_gpr(ctx, a->r);
2192         tmp = tcg_temp_new();
2193         tcg_gen_andi_reg(tmp, reg, TARGET_REGISTER_BITS - 1);
2194         save_or_nullify(ctx, cpu_sar, tmp);
2195         tcg_temp_free(tmp);
2196 
2197         cond_free(&ctx->null_cond);
2198         return true;
2199     }
2200 
2201     /* All other control registers are privileged or read-only.  */
2202     CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2203 
2204 #ifndef CONFIG_USER_ONLY
2205     nullify_over(ctx);
2206     reg = load_gpr(ctx, a->r);
2207 
2208     switch (ctl) {
2209     case CR_IT:
2210         gen_helper_write_interval_timer(cpu_env, reg);
2211         break;
2212     case CR_EIRR:
2213         gen_helper_write_eirr(cpu_env, reg);
2214         break;
2215     case CR_EIEM:
2216         gen_helper_write_eiem(cpu_env, reg);
2217         ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2218         break;
2219 
2220     case CR_IIASQ:
2221     case CR_IIAOQ:
2222         /* FIXME: Respect PSW_Q bit */
2223         /* The write advances the queue and stores to the back element.  */
2224         tmp = get_temp(ctx);
2225         tcg_gen_ld_reg(tmp, cpu_env,
2226                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
2227         tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2228         tcg_gen_st_reg(reg, cpu_env,
2229                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
2230         break;
2231 
2232     case CR_PID1:
2233     case CR_PID2:
2234     case CR_PID3:
2235     case CR_PID4:
2236         tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2237 #ifndef CONFIG_USER_ONLY
2238         gen_helper_change_prot_id(cpu_env);
2239 #endif
2240         break;
2241 
2242     default:
2243         tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2244         break;
2245     }
2246     return nullify_end(ctx);
2247 #endif
2248 }
2249 
2250 static bool trans_mtsarcm(DisasContext *ctx, arg_mtsarcm *a)
2251 {
2252     TCGv_reg tmp = tcg_temp_new();
2253 
2254     tcg_gen_not_reg(tmp, load_gpr(ctx, a->r));
2255     tcg_gen_andi_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
2256     save_or_nullify(ctx, cpu_sar, tmp);
2257     tcg_temp_free(tmp);
2258 
2259     cond_free(&ctx->null_cond);
2260     return true;
2261 }
2262 
2263 static bool trans_ldsid(DisasContext *ctx, arg_ldsid *a)
2264 {
2265     TCGv_reg dest = dest_gpr(ctx, a->t);
2266 
2267 #ifdef CONFIG_USER_ONLY
2268     /* We don't implement space registers in user mode. */
2269     tcg_gen_movi_reg(dest, 0);
2270 #else
2271     TCGv_i64 t0 = tcg_temp_new_i64();
2272 
2273     tcg_gen_mov_i64(t0, space_select(ctx, a->sp, load_gpr(ctx, a->b)));
2274     tcg_gen_shri_i64(t0, t0, 32);
2275     tcg_gen_trunc_i64_reg(dest, t0);
2276 
2277     tcg_temp_free_i64(t0);
2278 #endif
2279     save_gpr(ctx, a->t, dest);
2280 
2281     cond_free(&ctx->null_cond);
2282     return true;
2283 }
2284 
2285 static bool trans_rsm(DisasContext *ctx, arg_rsm *a)
2286 {
2287     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2288 #ifndef CONFIG_USER_ONLY
2289     TCGv_reg tmp;
2290 
2291     nullify_over(ctx);
2292 
2293     tmp = get_temp(ctx);
2294     tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2295     tcg_gen_andi_reg(tmp, tmp, ~a->i);
2296     gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2297     save_gpr(ctx, a->t, tmp);
2298 
2299     /* Exit the TB to recognize new interrupts, e.g. PSW_M.  */
2300     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2301     return nullify_end(ctx);
2302 #endif
2303 }
2304 
2305 static bool trans_ssm(DisasContext *ctx, arg_ssm *a)
2306 {
2307     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2308 #ifndef CONFIG_USER_ONLY
2309     TCGv_reg tmp;
2310 
2311     nullify_over(ctx);
2312 
2313     tmp = get_temp(ctx);
2314     tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2315     tcg_gen_ori_reg(tmp, tmp, a->i);
2316     gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2317     save_gpr(ctx, a->t, tmp);
2318 
2319     /* Exit the TB to recognize new interrupts, e.g. PSW_I.  */
2320     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2321     return nullify_end(ctx);
2322 #endif
2323 }
2324 
2325 static bool trans_mtsm(DisasContext *ctx, arg_mtsm *a)
2326 {
2327     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2328 #ifndef CONFIG_USER_ONLY
2329     TCGv_reg tmp, reg;
2330     nullify_over(ctx);
2331 
2332     reg = load_gpr(ctx, a->r);
2333     tmp = get_temp(ctx);
2334     gen_helper_swap_system_mask(tmp, cpu_env, reg);
2335 
2336     /* Exit the TB to recognize new interrupts.  */
2337     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2338     return nullify_end(ctx);
2339 #endif
2340 }
2341 
2342 static bool do_rfi(DisasContext *ctx, bool rfi_r)
2343 {
2344     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2345 #ifndef CONFIG_USER_ONLY
2346     nullify_over(ctx);
2347 
2348     if (rfi_r) {
2349         gen_helper_rfi_r(cpu_env);
2350     } else {
2351         gen_helper_rfi(cpu_env);
2352     }
2353     /* Exit the TB to recognize new interrupts.  */
2354     tcg_gen_exit_tb(NULL, 0);
2355     ctx->base.is_jmp = DISAS_NORETURN;
2356 
2357     return nullify_end(ctx);
2358 #endif
2359 }
2360 
2361 static bool trans_rfi(DisasContext *ctx, arg_rfi *a)
2362 {
2363     return do_rfi(ctx, false);
2364 }
2365 
2366 static bool trans_rfi_r(DisasContext *ctx, arg_rfi_r *a)
2367 {
2368     return do_rfi(ctx, true);
2369 }
2370 
2371 static bool trans_halt(DisasContext *ctx, arg_halt *a)
2372 {
2373     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2374 #ifndef CONFIG_USER_ONLY
2375     nullify_over(ctx);
2376     gen_helper_halt(cpu_env);
2377     ctx->base.is_jmp = DISAS_NORETURN;
2378     return nullify_end(ctx);
2379 #endif
2380 }
2381 
2382 static bool trans_reset(DisasContext *ctx, arg_reset *a)
2383 {
2384     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2385 #ifndef CONFIG_USER_ONLY
2386     nullify_over(ctx);
2387     gen_helper_reset(cpu_env);
2388     ctx->base.is_jmp = DISAS_NORETURN;
2389     return nullify_end(ctx);
2390 #endif
2391 }
2392 
2393 static bool trans_getshadowregs(DisasContext *ctx, arg_getshadowregs *a)
2394 {
2395     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2396 #ifndef CONFIG_USER_ONLY
2397     nullify_over(ctx);
2398     gen_helper_getshadowregs(cpu_env);
2399     return nullify_end(ctx);
2400 #endif
2401 }
2402 
2403 static bool trans_nop_addrx(DisasContext *ctx, arg_ldst *a)
2404 {
2405     if (a->m) {
2406         TCGv_reg dest = dest_gpr(ctx, a->b);
2407         TCGv_reg src1 = load_gpr(ctx, a->b);
2408         TCGv_reg src2 = load_gpr(ctx, a->x);
2409 
2410         /* The only thing we need to do is the base register modification.  */
2411         tcg_gen_add_reg(dest, src1, src2);
2412         save_gpr(ctx, a->b, dest);
2413     }
2414     cond_free(&ctx->null_cond);
2415     return true;
2416 }
2417 
2418 static bool trans_probe(DisasContext *ctx, arg_probe *a)
2419 {
2420     TCGv_reg dest, ofs;
2421     TCGv_i32 level, want;
2422     TCGv_tl addr;
2423 
2424     nullify_over(ctx);
2425 
2426     dest = dest_gpr(ctx, a->t);
2427     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2428 
2429     if (a->imm) {
2430         level = tcg_constant_i32(a->ri);
2431     } else {
2432         level = tcg_temp_new_i32();
2433         tcg_gen_trunc_reg_i32(level, load_gpr(ctx, a->ri));
2434         tcg_gen_andi_i32(level, level, 3);
2435     }
2436     want = tcg_constant_i32(a->write ? PAGE_WRITE : PAGE_READ);
2437 
2438     gen_helper_probe(dest, cpu_env, addr, level, want);
2439 
2440     tcg_temp_free_i32(level);
2441 
2442     save_gpr(ctx, a->t, dest);
2443     return nullify_end(ctx);
2444 }
2445 
2446 static bool trans_ixtlbx(DisasContext *ctx, arg_ixtlbx *a)
2447 {
2448     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2449 #ifndef CONFIG_USER_ONLY
2450     TCGv_tl addr;
2451     TCGv_reg ofs, reg;
2452 
2453     nullify_over(ctx);
2454 
2455     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2456     reg = load_gpr(ctx, a->r);
2457     if (a->addr) {
2458         gen_helper_itlba(cpu_env, addr, reg);
2459     } else {
2460         gen_helper_itlbp(cpu_env, addr, reg);
2461     }
2462 
2463     /* Exit TB for TLB change if mmu is enabled.  */
2464     if (ctx->tb_flags & PSW_C) {
2465         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2466     }
2467     return nullify_end(ctx);
2468 #endif
2469 }
2470 
2471 static bool trans_pxtlbx(DisasContext *ctx, arg_pxtlbx *a)
2472 {
2473     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2474 #ifndef CONFIG_USER_ONLY
2475     TCGv_tl addr;
2476     TCGv_reg ofs;
2477 
2478     nullify_over(ctx);
2479 
2480     form_gva(ctx, &addr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2481     if (a->m) {
2482         save_gpr(ctx, a->b, ofs);
2483     }
2484     if (a->local) {
2485         gen_helper_ptlbe(cpu_env);
2486     } else {
2487         gen_helper_ptlb(cpu_env, addr);
2488     }
2489 
2490     /* Exit TB for TLB change if mmu is enabled.  */
2491     if (ctx->tb_flags & PSW_C) {
2492         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2493     }
2494     return nullify_end(ctx);
2495 #endif
2496 }
2497 
2498 /*
2499  * Implement the pcxl and pcxl2 Fast TLB Insert instructions.
2500  * See
2501  *     https://parisc.wiki.kernel.org/images-parisc/a/a9/Pcxl2_ers.pdf
2502  *     page 13-9 (195/206)
2503  */
2504 static bool trans_ixtlbxf(DisasContext *ctx, arg_ixtlbxf *a)
2505 {
2506     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2507 #ifndef CONFIG_USER_ONLY
2508     TCGv_tl addr, atl, stl;
2509     TCGv_reg reg;
2510 
2511     nullify_over(ctx);
2512 
2513     /*
2514      * FIXME:
2515      *  if (not (pcxl or pcxl2))
2516      *    return gen_illegal(ctx);
2517      *
2518      * Note for future: these are 32-bit systems; no hppa64.
2519      */
2520 
2521     atl = tcg_temp_new_tl();
2522     stl = tcg_temp_new_tl();
2523     addr = tcg_temp_new_tl();
2524 
2525     tcg_gen_ld32u_i64(stl, cpu_env,
2526                       a->data ? offsetof(CPUHPPAState, cr[CR_ISR])
2527                       : offsetof(CPUHPPAState, cr[CR_IIASQ]));
2528     tcg_gen_ld32u_i64(atl, cpu_env,
2529                       a->data ? offsetof(CPUHPPAState, cr[CR_IOR])
2530                       : offsetof(CPUHPPAState, cr[CR_IIAOQ]));
2531     tcg_gen_shli_i64(stl, stl, 32);
2532     tcg_gen_or_tl(addr, atl, stl);
2533     tcg_temp_free_tl(atl);
2534     tcg_temp_free_tl(stl);
2535 
2536     reg = load_gpr(ctx, a->r);
2537     if (a->addr) {
2538         gen_helper_itlba(cpu_env, addr, reg);
2539     } else {
2540         gen_helper_itlbp(cpu_env, addr, reg);
2541     }
2542     tcg_temp_free_tl(addr);
2543 
2544     /* Exit TB for TLB change if mmu is enabled.  */
2545     if (ctx->tb_flags & PSW_C) {
2546         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2547     }
2548     return nullify_end(ctx);
2549 #endif
2550 }
2551 
2552 static bool trans_lpa(DisasContext *ctx, arg_ldst *a)
2553 {
2554     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2555 #ifndef CONFIG_USER_ONLY
2556     TCGv_tl vaddr;
2557     TCGv_reg ofs, paddr;
2558 
2559     nullify_over(ctx);
2560 
2561     form_gva(ctx, &vaddr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2562 
2563     paddr = tcg_temp_new();
2564     gen_helper_lpa(paddr, cpu_env, vaddr);
2565 
2566     /* Note that physical address result overrides base modification.  */
2567     if (a->m) {
2568         save_gpr(ctx, a->b, ofs);
2569     }
2570     save_gpr(ctx, a->t, paddr);
2571     tcg_temp_free(paddr);
2572 
2573     return nullify_end(ctx);
2574 #endif
2575 }
2576 
2577 static bool trans_lci(DisasContext *ctx, arg_lci *a)
2578 {
2579     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2580 
2581     /* The Coherence Index is an implementation-defined function of the
2582        physical address.  Two addresses with the same CI have a coherent
2583        view of the cache.  Our implementation is to return 0 for all,
2584        since the entire address space is coherent.  */
2585     save_gpr(ctx, a->t, tcg_constant_reg(0));
2586 
2587     cond_free(&ctx->null_cond);
2588     return true;
2589 }
2590 
2591 static bool trans_add(DisasContext *ctx, arg_rrr_cf_sh *a)
2592 {
2593     return do_add_reg(ctx, a, false, false, false, false);
2594 }
2595 
2596 static bool trans_add_l(DisasContext *ctx, arg_rrr_cf_sh *a)
2597 {
2598     return do_add_reg(ctx, a, true, false, false, false);
2599 }
2600 
2601 static bool trans_add_tsv(DisasContext *ctx, arg_rrr_cf_sh *a)
2602 {
2603     return do_add_reg(ctx, a, false, true, false, false);
2604 }
2605 
2606 static bool trans_add_c(DisasContext *ctx, arg_rrr_cf_sh *a)
2607 {
2608     return do_add_reg(ctx, a, false, false, false, true);
2609 }
2610 
2611 static bool trans_add_c_tsv(DisasContext *ctx, arg_rrr_cf_sh *a)
2612 {
2613     return do_add_reg(ctx, a, false, true, false, true);
2614 }
2615 
2616 static bool trans_sub(DisasContext *ctx, arg_rrr_cf *a)
2617 {
2618     return do_sub_reg(ctx, a, false, false, false);
2619 }
2620 
2621 static bool trans_sub_tsv(DisasContext *ctx, arg_rrr_cf *a)
2622 {
2623     return do_sub_reg(ctx, a, true, false, false);
2624 }
2625 
2626 static bool trans_sub_tc(DisasContext *ctx, arg_rrr_cf *a)
2627 {
2628     return do_sub_reg(ctx, a, false, false, true);
2629 }
2630 
2631 static bool trans_sub_tsv_tc(DisasContext *ctx, arg_rrr_cf *a)
2632 {
2633     return do_sub_reg(ctx, a, true, false, true);
2634 }
2635 
2636 static bool trans_sub_b(DisasContext *ctx, arg_rrr_cf *a)
2637 {
2638     return do_sub_reg(ctx, a, false, true, false);
2639 }
2640 
2641 static bool trans_sub_b_tsv(DisasContext *ctx, arg_rrr_cf *a)
2642 {
2643     return do_sub_reg(ctx, a, true, true, false);
2644 }
2645 
2646 static bool trans_andcm(DisasContext *ctx, arg_rrr_cf *a)
2647 {
2648     return do_log_reg(ctx, a, tcg_gen_andc_reg);
2649 }
2650 
2651 static bool trans_and(DisasContext *ctx, arg_rrr_cf *a)
2652 {
2653     return do_log_reg(ctx, a, tcg_gen_and_reg);
2654 }
2655 
2656 static bool trans_or(DisasContext *ctx, arg_rrr_cf *a)
2657 {
2658     if (a->cf == 0) {
2659         unsigned r2 = a->r2;
2660         unsigned r1 = a->r1;
2661         unsigned rt = a->t;
2662 
2663         if (rt == 0) { /* NOP */
2664             cond_free(&ctx->null_cond);
2665             return true;
2666         }
2667         if (r2 == 0) { /* COPY */
2668             if (r1 == 0) {
2669                 TCGv_reg dest = dest_gpr(ctx, rt);
2670                 tcg_gen_movi_reg(dest, 0);
2671                 save_gpr(ctx, rt, dest);
2672             } else {
2673                 save_gpr(ctx, rt, cpu_gr[r1]);
2674             }
2675             cond_free(&ctx->null_cond);
2676             return true;
2677         }
2678 #ifndef CONFIG_USER_ONLY
2679         /* These are QEMU extensions and are nops in the real architecture:
2680          *
2681          * or %r10,%r10,%r10 -- idle loop; wait for interrupt
2682          * or %r31,%r31,%r31 -- death loop; offline cpu
2683          *                      currently implemented as idle.
2684          */
2685         if ((rt == 10 || rt == 31) && r1 == rt && r2 == rt) { /* PAUSE */
2686             /* No need to check for supervisor, as userland can only pause
2687                until the next timer interrupt.  */
2688             nullify_over(ctx);
2689 
2690             /* Advance the instruction queue.  */
2691             copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
2692             copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
2693             nullify_set(ctx, 0);
2694 
2695             /* Tell the qemu main loop to halt until this cpu has work.  */
2696             tcg_gen_st_i32(tcg_constant_i32(1), cpu_env,
2697                            offsetof(CPUState, halted) - offsetof(HPPACPU, env));
2698             gen_excp_1(EXCP_HALTED);
2699             ctx->base.is_jmp = DISAS_NORETURN;
2700 
2701             return nullify_end(ctx);
2702         }
2703 #endif
2704     }
2705     return do_log_reg(ctx, a, tcg_gen_or_reg);
2706 }
2707 
2708 static bool trans_xor(DisasContext *ctx, arg_rrr_cf *a)
2709 {
2710     return do_log_reg(ctx, a, tcg_gen_xor_reg);
2711 }
2712 
2713 static bool trans_cmpclr(DisasContext *ctx, arg_rrr_cf *a)
2714 {
2715     TCGv_reg tcg_r1, tcg_r2;
2716 
2717     if (a->cf) {
2718         nullify_over(ctx);
2719     }
2720     tcg_r1 = load_gpr(ctx, a->r1);
2721     tcg_r2 = load_gpr(ctx, a->r2);
2722     do_cmpclr(ctx, a->t, tcg_r1, tcg_r2, a->cf);
2723     return nullify_end(ctx);
2724 }
2725 
2726 static bool trans_uxor(DisasContext *ctx, arg_rrr_cf *a)
2727 {
2728     TCGv_reg tcg_r1, tcg_r2;
2729 
2730     if (a->cf) {
2731         nullify_over(ctx);
2732     }
2733     tcg_r1 = load_gpr(ctx, a->r1);
2734     tcg_r2 = load_gpr(ctx, a->r2);
2735     do_unit(ctx, a->t, tcg_r1, tcg_r2, a->cf, false, tcg_gen_xor_reg);
2736     return nullify_end(ctx);
2737 }
2738 
2739 static bool do_uaddcm(DisasContext *ctx, arg_rrr_cf *a, bool is_tc)
2740 {
2741     TCGv_reg tcg_r1, tcg_r2, tmp;
2742 
2743     if (a->cf) {
2744         nullify_over(ctx);
2745     }
2746     tcg_r1 = load_gpr(ctx, a->r1);
2747     tcg_r2 = load_gpr(ctx, a->r2);
2748     tmp = get_temp(ctx);
2749     tcg_gen_not_reg(tmp, tcg_r2);
2750     do_unit(ctx, a->t, tcg_r1, tmp, a->cf, is_tc, tcg_gen_add_reg);
2751     return nullify_end(ctx);
2752 }
2753 
2754 static bool trans_uaddcm(DisasContext *ctx, arg_rrr_cf *a)
2755 {
2756     return do_uaddcm(ctx, a, false);
2757 }
2758 
2759 static bool trans_uaddcm_tc(DisasContext *ctx, arg_rrr_cf *a)
2760 {
2761     return do_uaddcm(ctx, a, true);
2762 }
2763 
2764 static bool do_dcor(DisasContext *ctx, arg_rr_cf *a, bool is_i)
2765 {
2766     TCGv_reg tmp;
2767 
2768     nullify_over(ctx);
2769 
2770     tmp = get_temp(ctx);
2771     tcg_gen_shri_reg(tmp, cpu_psw_cb, 3);
2772     if (!is_i) {
2773         tcg_gen_not_reg(tmp, tmp);
2774     }
2775     tcg_gen_andi_reg(tmp, tmp, 0x11111111);
2776     tcg_gen_muli_reg(tmp, tmp, 6);
2777     do_unit(ctx, a->t, load_gpr(ctx, a->r), tmp, a->cf, false,
2778             is_i ? tcg_gen_add_reg : tcg_gen_sub_reg);
2779     return nullify_end(ctx);
2780 }
2781 
2782 static bool trans_dcor(DisasContext *ctx, arg_rr_cf *a)
2783 {
2784     return do_dcor(ctx, a, false);
2785 }
2786 
2787 static bool trans_dcor_i(DisasContext *ctx, arg_rr_cf *a)
2788 {
2789     return do_dcor(ctx, a, true);
2790 }
2791 
2792 static bool trans_ds(DisasContext *ctx, arg_rrr_cf *a)
2793 {
2794     TCGv_reg dest, add1, add2, addc, zero, in1, in2;
2795 
2796     nullify_over(ctx);
2797 
2798     in1 = load_gpr(ctx, a->r1);
2799     in2 = load_gpr(ctx, a->r2);
2800 
2801     add1 = tcg_temp_new();
2802     add2 = tcg_temp_new();
2803     addc = tcg_temp_new();
2804     dest = tcg_temp_new();
2805     zero = tcg_constant_reg(0);
2806 
2807     /* Form R1 << 1 | PSW[CB]{8}.  */
2808     tcg_gen_add_reg(add1, in1, in1);
2809     tcg_gen_add_reg(add1, add1, cpu_psw_cb_msb);
2810 
2811     /* Add or subtract R2, depending on PSW[V].  Proper computation of
2812        carry{8} requires that we subtract via + ~R2 + 1, as described in
2813        the manual.  By extracting and masking V, we can produce the
2814        proper inputs to the addition without movcond.  */
2815     tcg_gen_sari_reg(addc, cpu_psw_v, TARGET_REGISTER_BITS - 1);
2816     tcg_gen_xor_reg(add2, in2, addc);
2817     tcg_gen_andi_reg(addc, addc, 1);
2818     /* ??? This is only correct for 32-bit.  */
2819     tcg_gen_add2_i32(dest, cpu_psw_cb_msb, add1, zero, add2, zero);
2820     tcg_gen_add2_i32(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb, addc, zero);
2821 
2822     tcg_temp_free(addc);
2823 
2824     /* Write back the result register.  */
2825     save_gpr(ctx, a->t, dest);
2826 
2827     /* Write back PSW[CB].  */
2828     tcg_gen_xor_reg(cpu_psw_cb, add1, add2);
2829     tcg_gen_xor_reg(cpu_psw_cb, cpu_psw_cb, dest);
2830 
2831     /* Write back PSW[V] for the division step.  */
2832     tcg_gen_neg_reg(cpu_psw_v, cpu_psw_cb_msb);
2833     tcg_gen_xor_reg(cpu_psw_v, cpu_psw_v, in2);
2834 
2835     /* Install the new nullification.  */
2836     if (a->cf) {
2837         TCGv_reg sv = NULL;
2838         if (cond_need_sv(a->cf >> 1)) {
2839             /* ??? The lshift is supposed to contribute to overflow.  */
2840             sv = do_add_sv(ctx, dest, add1, add2);
2841         }
2842         ctx->null_cond = do_cond(a->cf, dest, cpu_psw_cb_msb, sv);
2843     }
2844 
2845     tcg_temp_free(add1);
2846     tcg_temp_free(add2);
2847     tcg_temp_free(dest);
2848 
2849     return nullify_end(ctx);
2850 }
2851 
2852 static bool trans_addi(DisasContext *ctx, arg_rri_cf *a)
2853 {
2854     return do_add_imm(ctx, a, false, false);
2855 }
2856 
2857 static bool trans_addi_tsv(DisasContext *ctx, arg_rri_cf *a)
2858 {
2859     return do_add_imm(ctx, a, true, false);
2860 }
2861 
2862 static bool trans_addi_tc(DisasContext *ctx, arg_rri_cf *a)
2863 {
2864     return do_add_imm(ctx, a, false, true);
2865 }
2866 
2867 static bool trans_addi_tc_tsv(DisasContext *ctx, arg_rri_cf *a)
2868 {
2869     return do_add_imm(ctx, a, true, true);
2870 }
2871 
2872 static bool trans_subi(DisasContext *ctx, arg_rri_cf *a)
2873 {
2874     return do_sub_imm(ctx, a, false);
2875 }
2876 
2877 static bool trans_subi_tsv(DisasContext *ctx, arg_rri_cf *a)
2878 {
2879     return do_sub_imm(ctx, a, true);
2880 }
2881 
2882 static bool trans_cmpiclr(DisasContext *ctx, arg_rri_cf *a)
2883 {
2884     TCGv_reg tcg_im, tcg_r2;
2885 
2886     if (a->cf) {
2887         nullify_over(ctx);
2888     }
2889 
2890     tcg_im = load_const(ctx, a->i);
2891     tcg_r2 = load_gpr(ctx, a->r);
2892     do_cmpclr(ctx, a->t, tcg_im, tcg_r2, a->cf);
2893 
2894     return nullify_end(ctx);
2895 }
2896 
2897 static bool trans_ld(DisasContext *ctx, arg_ldst *a)
2898 {
2899     if (unlikely(TARGET_REGISTER_BITS == 32 && a->size > MO_32)) {
2900         return gen_illegal(ctx);
2901     } else {
2902         return do_load(ctx, a->t, a->b, a->x, a->scale ? a->size : 0,
2903                    a->disp, a->sp, a->m, a->size | MO_TE);
2904     }
2905 }
2906 
2907 static bool trans_st(DisasContext *ctx, arg_ldst *a)
2908 {
2909     assert(a->x == 0 && a->scale == 0);
2910     if (unlikely(TARGET_REGISTER_BITS == 32 && a->size > MO_32)) {
2911         return gen_illegal(ctx);
2912     } else {
2913         return do_store(ctx, a->t, a->b, a->disp, a->sp, a->m, a->size | MO_TE);
2914     }
2915 }
2916 
2917 static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
2918 {
2919     MemOp mop = MO_TE | MO_ALIGN | a->size;
2920     TCGv_reg zero, dest, ofs;
2921     TCGv_tl addr;
2922 
2923     nullify_over(ctx);
2924 
2925     if (a->m) {
2926         /* Base register modification.  Make sure if RT == RB,
2927            we see the result of the load.  */
2928         dest = get_temp(ctx);
2929     } else {
2930         dest = dest_gpr(ctx, a->t);
2931     }
2932 
2933     form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? a->size : 0,
2934              a->disp, a->sp, a->m, ctx->mmu_idx == MMU_PHYS_IDX);
2935 
2936     /*
2937      * For hppa1.1, LDCW is undefined unless aligned mod 16.
2938      * However actual hardware succeeds with aligned mod 4.
2939      * Detect this case and log a GUEST_ERROR.
2940      *
2941      * TODO: HPPA64 relaxes the over-alignment requirement
2942      * with the ,co completer.
2943      */
2944     gen_helper_ldc_check(addr);
2945 
2946     zero = tcg_constant_reg(0);
2947     tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
2948 
2949     if (a->m) {
2950         save_gpr(ctx, a->b, ofs);
2951     }
2952     save_gpr(ctx, a->t, dest);
2953 
2954     return nullify_end(ctx);
2955 }
2956 
2957 static bool trans_stby(DisasContext *ctx, arg_stby *a)
2958 {
2959     TCGv_reg ofs, val;
2960     TCGv_tl addr;
2961 
2962     nullify_over(ctx);
2963 
2964     form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
2965              ctx->mmu_idx == MMU_PHYS_IDX);
2966     val = load_gpr(ctx, a->r);
2967     if (a->a) {
2968         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2969             gen_helper_stby_e_parallel(cpu_env, addr, val);
2970         } else {
2971             gen_helper_stby_e(cpu_env, addr, val);
2972         }
2973     } else {
2974         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2975             gen_helper_stby_b_parallel(cpu_env, addr, val);
2976         } else {
2977             gen_helper_stby_b(cpu_env, addr, val);
2978         }
2979     }
2980     if (a->m) {
2981         tcg_gen_andi_reg(ofs, ofs, ~3);
2982         save_gpr(ctx, a->b, ofs);
2983     }
2984 
2985     return nullify_end(ctx);
2986 }
2987 
2988 static bool trans_lda(DisasContext *ctx, arg_ldst *a)
2989 {
2990     int hold_mmu_idx = ctx->mmu_idx;
2991 
2992     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2993     ctx->mmu_idx = MMU_PHYS_IDX;
2994     trans_ld(ctx, a);
2995     ctx->mmu_idx = hold_mmu_idx;
2996     return true;
2997 }
2998 
2999 static bool trans_sta(DisasContext *ctx, arg_ldst *a)
3000 {
3001     int hold_mmu_idx = ctx->mmu_idx;
3002 
3003     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
3004     ctx->mmu_idx = MMU_PHYS_IDX;
3005     trans_st(ctx, a);
3006     ctx->mmu_idx = hold_mmu_idx;
3007     return true;
3008 }
3009 
3010 static bool trans_ldil(DisasContext *ctx, arg_ldil *a)
3011 {
3012     TCGv_reg tcg_rt = dest_gpr(ctx, a->t);
3013 
3014     tcg_gen_movi_reg(tcg_rt, a->i);
3015     save_gpr(ctx, a->t, tcg_rt);
3016     cond_free(&ctx->null_cond);
3017     return true;
3018 }
3019 
3020 static bool trans_addil(DisasContext *ctx, arg_addil *a)
3021 {
3022     TCGv_reg tcg_rt = load_gpr(ctx, a->r);
3023     TCGv_reg tcg_r1 = dest_gpr(ctx, 1);
3024 
3025     tcg_gen_addi_reg(tcg_r1, tcg_rt, a->i);
3026     save_gpr(ctx, 1, tcg_r1);
3027     cond_free(&ctx->null_cond);
3028     return true;
3029 }
3030 
3031 static bool trans_ldo(DisasContext *ctx, arg_ldo *a)
3032 {
3033     TCGv_reg tcg_rt = dest_gpr(ctx, a->t);
3034 
3035     /* Special case rb == 0, for the LDI pseudo-op.
3036        The COPY pseudo-op is handled for free within tcg_gen_addi_tl.  */
3037     if (a->b == 0) {
3038         tcg_gen_movi_reg(tcg_rt, a->i);
3039     } else {
3040         tcg_gen_addi_reg(tcg_rt, cpu_gr[a->b], a->i);
3041     }
3042     save_gpr(ctx, a->t, tcg_rt);
3043     cond_free(&ctx->null_cond);
3044     return true;
3045 }
3046 
3047 static bool do_cmpb(DisasContext *ctx, unsigned r, TCGv_reg in1,
3048                     unsigned c, unsigned f, unsigned n, int disp)
3049 {
3050     TCGv_reg dest, in2, sv;
3051     DisasCond cond;
3052 
3053     in2 = load_gpr(ctx, r);
3054     dest = get_temp(ctx);
3055 
3056     tcg_gen_sub_reg(dest, in1, in2);
3057 
3058     sv = NULL;
3059     if (cond_need_sv(c)) {
3060         sv = do_sub_sv(ctx, dest, in1, in2);
3061     }
3062 
3063     cond = do_sub_cond(c * 2 + f, dest, in1, in2, sv);
3064     return do_cbranch(ctx, disp, n, &cond);
3065 }
3066 
3067 static bool trans_cmpb(DisasContext *ctx, arg_cmpb *a)
3068 {
3069     nullify_over(ctx);
3070     return do_cmpb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
3071 }
3072 
3073 static bool trans_cmpbi(DisasContext *ctx, arg_cmpbi *a)
3074 {
3075     nullify_over(ctx);
3076     return do_cmpb(ctx, a->r, load_const(ctx, a->i), a->c, a->f, a->n, a->disp);
3077 }
3078 
3079 static bool do_addb(DisasContext *ctx, unsigned r, TCGv_reg in1,
3080                     unsigned c, unsigned f, unsigned n, int disp)
3081 {
3082     TCGv_reg dest, in2, sv, cb_msb;
3083     DisasCond cond;
3084 
3085     in2 = load_gpr(ctx, r);
3086     dest = tcg_temp_new();
3087     sv = NULL;
3088     cb_msb = NULL;
3089 
3090     if (cond_need_cb(c)) {
3091         cb_msb = get_temp(ctx);
3092         tcg_gen_movi_reg(cb_msb, 0);
3093         tcg_gen_add2_reg(dest, cb_msb, in1, cb_msb, in2, cb_msb);
3094     } else {
3095         tcg_gen_add_reg(dest, in1, in2);
3096     }
3097     if (cond_need_sv(c)) {
3098         sv = do_add_sv(ctx, dest, in1, in2);
3099     }
3100 
3101     cond = do_cond(c * 2 + f, dest, cb_msb, sv);
3102     save_gpr(ctx, r, dest);
3103     tcg_temp_free(dest);
3104     return do_cbranch(ctx, disp, n, &cond);
3105 }
3106 
3107 static bool trans_addb(DisasContext *ctx, arg_addb *a)
3108 {
3109     nullify_over(ctx);
3110     return do_addb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
3111 }
3112 
3113 static bool trans_addbi(DisasContext *ctx, arg_addbi *a)
3114 {
3115     nullify_over(ctx);
3116     return do_addb(ctx, a->r, load_const(ctx, a->i), a->c, a->f, a->n, a->disp);
3117 }
3118 
3119 static bool trans_bb_sar(DisasContext *ctx, arg_bb_sar *a)
3120 {
3121     TCGv_reg tmp, tcg_r;
3122     DisasCond cond;
3123 
3124     nullify_over(ctx);
3125 
3126     tmp = tcg_temp_new();
3127     tcg_r = load_gpr(ctx, a->r);
3128     tcg_gen_shl_reg(tmp, tcg_r, cpu_sar);
3129 
3130     cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
3131     tcg_temp_free(tmp);
3132     return do_cbranch(ctx, a->disp, a->n, &cond);
3133 }
3134 
3135 static bool trans_bb_imm(DisasContext *ctx, arg_bb_imm *a)
3136 {
3137     TCGv_reg tmp, tcg_r;
3138     DisasCond cond;
3139 
3140     nullify_over(ctx);
3141 
3142     tmp = tcg_temp_new();
3143     tcg_r = load_gpr(ctx, a->r);
3144     tcg_gen_shli_reg(tmp, tcg_r, a->p);
3145 
3146     cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
3147     tcg_temp_free(tmp);
3148     return do_cbranch(ctx, a->disp, a->n, &cond);
3149 }
3150 
3151 static bool trans_movb(DisasContext *ctx, arg_movb *a)
3152 {
3153     TCGv_reg dest;
3154     DisasCond cond;
3155 
3156     nullify_over(ctx);
3157 
3158     dest = dest_gpr(ctx, a->r2);
3159     if (a->r1 == 0) {
3160         tcg_gen_movi_reg(dest, 0);
3161     } else {
3162         tcg_gen_mov_reg(dest, cpu_gr[a->r1]);
3163     }
3164 
3165     cond = do_sed_cond(a->c, dest);
3166     return do_cbranch(ctx, a->disp, a->n, &cond);
3167 }
3168 
3169 static bool trans_movbi(DisasContext *ctx, arg_movbi *a)
3170 {
3171     TCGv_reg dest;
3172     DisasCond cond;
3173 
3174     nullify_over(ctx);
3175 
3176     dest = dest_gpr(ctx, a->r);
3177     tcg_gen_movi_reg(dest, a->i);
3178 
3179     cond = do_sed_cond(a->c, dest);
3180     return do_cbranch(ctx, a->disp, a->n, &cond);
3181 }
3182 
3183 static bool trans_shrpw_sar(DisasContext *ctx, arg_shrpw_sar *a)
3184 {
3185     TCGv_reg dest;
3186 
3187     if (a->c) {
3188         nullify_over(ctx);
3189     }
3190 
3191     dest = dest_gpr(ctx, a->t);
3192     if (a->r1 == 0) {
3193         tcg_gen_ext32u_reg(dest, load_gpr(ctx, a->r2));
3194         tcg_gen_shr_reg(dest, dest, cpu_sar);
3195     } else if (a->r1 == a->r2) {
3196         TCGv_i32 t32 = tcg_temp_new_i32();
3197         tcg_gen_trunc_reg_i32(t32, load_gpr(ctx, a->r2));
3198         tcg_gen_rotr_i32(t32, t32, cpu_sar);
3199         tcg_gen_extu_i32_reg(dest, t32);
3200         tcg_temp_free_i32(t32);
3201     } else {
3202         TCGv_i64 t = tcg_temp_new_i64();
3203         TCGv_i64 s = tcg_temp_new_i64();
3204 
3205         tcg_gen_concat_reg_i64(t, load_gpr(ctx, a->r2), load_gpr(ctx, a->r1));
3206         tcg_gen_extu_reg_i64(s, cpu_sar);
3207         tcg_gen_shr_i64(t, t, s);
3208         tcg_gen_trunc_i64_reg(dest, t);
3209 
3210         tcg_temp_free_i64(t);
3211         tcg_temp_free_i64(s);
3212     }
3213     save_gpr(ctx, a->t, dest);
3214 
3215     /* Install the new nullification.  */
3216     cond_free(&ctx->null_cond);
3217     if (a->c) {
3218         ctx->null_cond = do_sed_cond(a->c, dest);
3219     }
3220     return nullify_end(ctx);
3221 }
3222 
3223 static bool trans_shrpw_imm(DisasContext *ctx, arg_shrpw_imm *a)
3224 {
3225     unsigned sa = 31 - a->cpos;
3226     TCGv_reg dest, t2;
3227 
3228     if (a->c) {
3229         nullify_over(ctx);
3230     }
3231 
3232     dest = dest_gpr(ctx, a->t);
3233     t2 = load_gpr(ctx, a->r2);
3234     if (a->r1 == 0) {
3235         tcg_gen_extract_reg(dest, t2, sa, 32 - sa);
3236     } else if (TARGET_REGISTER_BITS == 32) {
3237         tcg_gen_extract2_reg(dest, t2, cpu_gr[a->r1], sa);
3238     } else if (a->r1 == a->r2) {
3239         TCGv_i32 t32 = tcg_temp_new_i32();
3240         tcg_gen_trunc_reg_i32(t32, t2);
3241         tcg_gen_rotri_i32(t32, t32, sa);
3242         tcg_gen_extu_i32_reg(dest, t32);
3243         tcg_temp_free_i32(t32);
3244     } else {
3245         TCGv_i64 t64 = tcg_temp_new_i64();
3246         tcg_gen_concat_reg_i64(t64, t2, cpu_gr[a->r1]);
3247         tcg_gen_shri_i64(t64, t64, sa);
3248         tcg_gen_trunc_i64_reg(dest, t64);
3249         tcg_temp_free_i64(t64);
3250     }
3251     save_gpr(ctx, a->t, dest);
3252 
3253     /* Install the new nullification.  */
3254     cond_free(&ctx->null_cond);
3255     if (a->c) {
3256         ctx->null_cond = do_sed_cond(a->c, dest);
3257     }
3258     return nullify_end(ctx);
3259 }
3260 
3261 static bool trans_extrw_sar(DisasContext *ctx, arg_extrw_sar *a)
3262 {
3263     unsigned len = 32 - a->clen;
3264     TCGv_reg dest, src, tmp;
3265 
3266     if (a->c) {
3267         nullify_over(ctx);
3268     }
3269 
3270     dest = dest_gpr(ctx, a->t);
3271     src = load_gpr(ctx, a->r);
3272     tmp = tcg_temp_new();
3273 
3274     /* Recall that SAR is using big-endian bit numbering.  */
3275     tcg_gen_xori_reg(tmp, cpu_sar, TARGET_REGISTER_BITS - 1);
3276     if (a->se) {
3277         tcg_gen_sar_reg(dest, src, tmp);
3278         tcg_gen_sextract_reg(dest, dest, 0, len);
3279     } else {
3280         tcg_gen_shr_reg(dest, src, tmp);
3281         tcg_gen_extract_reg(dest, dest, 0, len);
3282     }
3283     tcg_temp_free(tmp);
3284     save_gpr(ctx, a->t, dest);
3285 
3286     /* Install the new nullification.  */
3287     cond_free(&ctx->null_cond);
3288     if (a->c) {
3289         ctx->null_cond = do_sed_cond(a->c, dest);
3290     }
3291     return nullify_end(ctx);
3292 }
3293 
3294 static bool trans_extrw_imm(DisasContext *ctx, arg_extrw_imm *a)
3295 {
3296     unsigned len = 32 - a->clen;
3297     unsigned cpos = 31 - a->pos;
3298     TCGv_reg dest, src;
3299 
3300     if (a->c) {
3301         nullify_over(ctx);
3302     }
3303 
3304     dest = dest_gpr(ctx, a->t);
3305     src = load_gpr(ctx, a->r);
3306     if (a->se) {
3307         tcg_gen_sextract_reg(dest, src, cpos, len);
3308     } else {
3309         tcg_gen_extract_reg(dest, src, cpos, len);
3310     }
3311     save_gpr(ctx, a->t, dest);
3312 
3313     /* Install the new nullification.  */
3314     cond_free(&ctx->null_cond);
3315     if (a->c) {
3316         ctx->null_cond = do_sed_cond(a->c, dest);
3317     }
3318     return nullify_end(ctx);
3319 }
3320 
3321 static bool trans_depwi_imm(DisasContext *ctx, arg_depwi_imm *a)
3322 {
3323     unsigned len = 32 - a->clen;
3324     target_sreg mask0, mask1;
3325     TCGv_reg dest;
3326 
3327     if (a->c) {
3328         nullify_over(ctx);
3329     }
3330     if (a->cpos + len > 32) {
3331         len = 32 - a->cpos;
3332     }
3333 
3334     dest = dest_gpr(ctx, a->t);
3335     mask0 = deposit64(0, a->cpos, len, a->i);
3336     mask1 = deposit64(-1, a->cpos, len, a->i);
3337 
3338     if (a->nz) {
3339         TCGv_reg src = load_gpr(ctx, a->t);
3340         if (mask1 != -1) {
3341             tcg_gen_andi_reg(dest, src, mask1);
3342             src = dest;
3343         }
3344         tcg_gen_ori_reg(dest, src, mask0);
3345     } else {
3346         tcg_gen_movi_reg(dest, mask0);
3347     }
3348     save_gpr(ctx, a->t, dest);
3349 
3350     /* Install the new nullification.  */
3351     cond_free(&ctx->null_cond);
3352     if (a->c) {
3353         ctx->null_cond = do_sed_cond(a->c, dest);
3354     }
3355     return nullify_end(ctx);
3356 }
3357 
3358 static bool trans_depw_imm(DisasContext *ctx, arg_depw_imm *a)
3359 {
3360     unsigned rs = a->nz ? a->t : 0;
3361     unsigned len = 32 - a->clen;
3362     TCGv_reg dest, val;
3363 
3364     if (a->c) {
3365         nullify_over(ctx);
3366     }
3367     if (a->cpos + len > 32) {
3368         len = 32 - a->cpos;
3369     }
3370 
3371     dest = dest_gpr(ctx, a->t);
3372     val = load_gpr(ctx, a->r);
3373     if (rs == 0) {
3374         tcg_gen_deposit_z_reg(dest, val, a->cpos, len);
3375     } else {
3376         tcg_gen_deposit_reg(dest, cpu_gr[rs], val, a->cpos, len);
3377     }
3378     save_gpr(ctx, a->t, dest);
3379 
3380     /* Install the new nullification.  */
3381     cond_free(&ctx->null_cond);
3382     if (a->c) {
3383         ctx->null_cond = do_sed_cond(a->c, dest);
3384     }
3385     return nullify_end(ctx);
3386 }
3387 
3388 static bool do_depw_sar(DisasContext *ctx, unsigned rt, unsigned c,
3389                         unsigned nz, unsigned clen, TCGv_reg val)
3390 {
3391     unsigned rs = nz ? rt : 0;
3392     unsigned len = 32 - clen;
3393     TCGv_reg mask, tmp, shift, dest;
3394     unsigned msb = 1U << (len - 1);
3395 
3396     dest = dest_gpr(ctx, rt);
3397     shift = tcg_temp_new();
3398     tmp = tcg_temp_new();
3399 
3400     /* Convert big-endian bit numbering in SAR to left-shift.  */
3401     tcg_gen_xori_reg(shift, cpu_sar, TARGET_REGISTER_BITS - 1);
3402 
3403     mask = tcg_const_reg(msb + (msb - 1));
3404     tcg_gen_and_reg(tmp, val, mask);
3405     if (rs) {
3406         tcg_gen_shl_reg(mask, mask, shift);
3407         tcg_gen_shl_reg(tmp, tmp, shift);
3408         tcg_gen_andc_reg(dest, cpu_gr[rs], mask);
3409         tcg_gen_or_reg(dest, dest, tmp);
3410     } else {
3411         tcg_gen_shl_reg(dest, tmp, shift);
3412     }
3413     tcg_temp_free(shift);
3414     tcg_temp_free(mask);
3415     tcg_temp_free(tmp);
3416     save_gpr(ctx, rt, dest);
3417 
3418     /* Install the new nullification.  */
3419     cond_free(&ctx->null_cond);
3420     if (c) {
3421         ctx->null_cond = do_sed_cond(c, dest);
3422     }
3423     return nullify_end(ctx);
3424 }
3425 
3426 static bool trans_depw_sar(DisasContext *ctx, arg_depw_sar *a)
3427 {
3428     if (a->c) {
3429         nullify_over(ctx);
3430     }
3431     return do_depw_sar(ctx, a->t, a->c, a->nz, a->clen, load_gpr(ctx, a->r));
3432 }
3433 
3434 static bool trans_depwi_sar(DisasContext *ctx, arg_depwi_sar *a)
3435 {
3436     if (a->c) {
3437         nullify_over(ctx);
3438     }
3439     return do_depw_sar(ctx, a->t, a->c, a->nz, a->clen, load_const(ctx, a->i));
3440 }
3441 
3442 static bool trans_be(DisasContext *ctx, arg_be *a)
3443 {
3444     TCGv_reg tmp;
3445 
3446 #ifdef CONFIG_USER_ONLY
3447     /* ??? It seems like there should be a good way of using
3448        "be disp(sr2, r0)", the canonical gateway entry mechanism
3449        to our advantage.  But that appears to be inconvenient to
3450        manage along side branch delay slots.  Therefore we handle
3451        entry into the gateway page via absolute address.  */
3452     /* Since we don't implement spaces, just branch.  Do notice the special
3453        case of "be disp(*,r0)" using a direct branch to disp, so that we can
3454        goto_tb to the TB containing the syscall.  */
3455     if (a->b == 0) {
3456         return do_dbranch(ctx, a->disp, a->l, a->n);
3457     }
3458 #else
3459     nullify_over(ctx);
3460 #endif
3461 
3462     tmp = get_temp(ctx);
3463     tcg_gen_addi_reg(tmp, load_gpr(ctx, a->b), a->disp);
3464     tmp = do_ibranch_priv(ctx, tmp);
3465 
3466 #ifdef CONFIG_USER_ONLY
3467     return do_ibranch(ctx, tmp, a->l, a->n);
3468 #else
3469     TCGv_i64 new_spc = tcg_temp_new_i64();
3470 
3471     load_spr(ctx, new_spc, a->sp);
3472     if (a->l) {
3473         copy_iaoq_entry(cpu_gr[31], ctx->iaoq_n, ctx->iaoq_n_var);
3474         tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_f);
3475     }
3476     if (a->n && use_nullify_skip(ctx)) {
3477         tcg_gen_mov_reg(cpu_iaoq_f, tmp);
3478         tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
3479         tcg_gen_mov_i64(cpu_iasq_f, new_spc);
3480         tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
3481     } else {
3482         copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3483         if (ctx->iaoq_b == -1) {
3484             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3485         }
3486         tcg_gen_mov_reg(cpu_iaoq_b, tmp);
3487         tcg_gen_mov_i64(cpu_iasq_b, new_spc);
3488         nullify_set(ctx, a->n);
3489     }
3490     tcg_temp_free_i64(new_spc);
3491     tcg_gen_lookup_and_goto_ptr();
3492     ctx->base.is_jmp = DISAS_NORETURN;
3493     return nullify_end(ctx);
3494 #endif
3495 }
3496 
3497 static bool trans_bl(DisasContext *ctx, arg_bl *a)
3498 {
3499     return do_dbranch(ctx, iaoq_dest(ctx, a->disp), a->l, a->n);
3500 }
3501 
3502 static bool trans_b_gate(DisasContext *ctx, arg_b_gate *a)
3503 {
3504     target_ureg dest = iaoq_dest(ctx, a->disp);
3505 
3506     nullify_over(ctx);
3507 
3508     /* Make sure the caller hasn't done something weird with the queue.
3509      * ??? This is not quite the same as the PSW[B] bit, which would be
3510      * expensive to track.  Real hardware will trap for
3511      *    b  gateway
3512      *    b  gateway+4  (in delay slot of first branch)
3513      * However, checking for a non-sequential instruction queue *will*
3514      * diagnose the security hole
3515      *    b  gateway
3516      *    b  evil
3517      * in which instructions at evil would run with increased privs.
3518      */
3519     if (ctx->iaoq_b == -1 || ctx->iaoq_b != ctx->iaoq_f + 4) {
3520         return gen_illegal(ctx);
3521     }
3522 
3523 #ifndef CONFIG_USER_ONLY
3524     if (ctx->tb_flags & PSW_C) {
3525         CPUHPPAState *env = ctx->cs->env_ptr;
3526         int type = hppa_artype_for_page(env, ctx->base.pc_next);
3527         /* If we could not find a TLB entry, then we need to generate an
3528            ITLB miss exception so the kernel will provide it.
3529            The resulting TLB fill operation will invalidate this TB and
3530            we will re-translate, at which point we *will* be able to find
3531            the TLB entry and determine if this is in fact a gateway page.  */
3532         if (type < 0) {
3533             gen_excp(ctx, EXCP_ITLB_MISS);
3534             return true;
3535         }
3536         /* No change for non-gateway pages or for priv decrease.  */
3537         if (type >= 4 && type - 4 < ctx->privilege) {
3538             dest = deposit32(dest, 0, 2, type - 4);
3539         }
3540     } else {
3541         dest &= -4;  /* priv = 0 */
3542     }
3543 #endif
3544 
3545     if (a->l) {
3546         TCGv_reg tmp = dest_gpr(ctx, a->l);
3547         if (ctx->privilege < 3) {
3548             tcg_gen_andi_reg(tmp, tmp, -4);
3549         }
3550         tcg_gen_ori_reg(tmp, tmp, ctx->privilege);
3551         save_gpr(ctx, a->l, tmp);
3552     }
3553 
3554     return do_dbranch(ctx, dest, 0, a->n);
3555 }
3556 
3557 static bool trans_blr(DisasContext *ctx, arg_blr *a)
3558 {
3559     if (a->x) {
3560         TCGv_reg tmp = get_temp(ctx);
3561         tcg_gen_shli_reg(tmp, load_gpr(ctx, a->x), 3);
3562         tcg_gen_addi_reg(tmp, tmp, ctx->iaoq_f + 8);
3563         /* The computation here never changes privilege level.  */
3564         return do_ibranch(ctx, tmp, a->l, a->n);
3565     } else {
3566         /* BLR R0,RX is a good way to load PC+8 into RX.  */
3567         return do_dbranch(ctx, ctx->iaoq_f + 8, a->l, a->n);
3568     }
3569 }
3570 
3571 static bool trans_bv(DisasContext *ctx, arg_bv *a)
3572 {
3573     TCGv_reg dest;
3574 
3575     if (a->x == 0) {
3576         dest = load_gpr(ctx, a->b);
3577     } else {
3578         dest = get_temp(ctx);
3579         tcg_gen_shli_reg(dest, load_gpr(ctx, a->x), 3);
3580         tcg_gen_add_reg(dest, dest, load_gpr(ctx, a->b));
3581     }
3582     dest = do_ibranch_priv(ctx, dest);
3583     return do_ibranch(ctx, dest, 0, a->n);
3584 }
3585 
3586 static bool trans_bve(DisasContext *ctx, arg_bve *a)
3587 {
3588     TCGv_reg dest;
3589 
3590 #ifdef CONFIG_USER_ONLY
3591     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
3592     return do_ibranch(ctx, dest, a->l, a->n);
3593 #else
3594     nullify_over(ctx);
3595     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
3596 
3597     copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3598     if (ctx->iaoq_b == -1) {
3599         tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3600     }
3601     copy_iaoq_entry(cpu_iaoq_b, -1, dest);
3602     tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
3603     if (a->l) {
3604         copy_iaoq_entry(cpu_gr[a->l], ctx->iaoq_n, ctx->iaoq_n_var);
3605     }
3606     nullify_set(ctx, a->n);
3607     tcg_gen_lookup_and_goto_ptr();
3608     ctx->base.is_jmp = DISAS_NORETURN;
3609     return nullify_end(ctx);
3610 #endif
3611 }
3612 
3613 /*
3614  * Float class 0
3615  */
3616 
3617 static void gen_fcpy_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3618 {
3619     tcg_gen_mov_i32(dst, src);
3620 }
3621 
3622 static bool trans_fid_f(DisasContext *ctx, arg_fid_f *a)
3623 {
3624     nullify_over(ctx);
3625 #if TARGET_REGISTER_BITS == 64
3626     save_frd(0, tcg_const_i64(0x13080000000000ULL)); /* PA8700 (PCX-W2) */
3627 #else
3628     save_frd(0, tcg_const_i64(0x0f080000000000ULL)); /* PA7300LC (PCX-L2) */
3629 #endif
3630     return nullify_end(ctx);
3631 }
3632 
3633 static bool trans_fcpy_f(DisasContext *ctx, arg_fclass01 *a)
3634 {
3635     return do_fop_wew(ctx, a->t, a->r, gen_fcpy_f);
3636 }
3637 
3638 static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3639 {
3640     tcg_gen_mov_i64(dst, src);
3641 }
3642 
3643 static bool trans_fcpy_d(DisasContext *ctx, arg_fclass01 *a)
3644 {
3645     return do_fop_ded(ctx, a->t, a->r, gen_fcpy_d);
3646 }
3647 
3648 static void gen_fabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3649 {
3650     tcg_gen_andi_i32(dst, src, INT32_MAX);
3651 }
3652 
3653 static bool trans_fabs_f(DisasContext *ctx, arg_fclass01 *a)
3654 {
3655     return do_fop_wew(ctx, a->t, a->r, gen_fabs_f);
3656 }
3657 
3658 static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3659 {
3660     tcg_gen_andi_i64(dst, src, INT64_MAX);
3661 }
3662 
3663 static bool trans_fabs_d(DisasContext *ctx, arg_fclass01 *a)
3664 {
3665     return do_fop_ded(ctx, a->t, a->r, gen_fabs_d);
3666 }
3667 
3668 static bool trans_fsqrt_f(DisasContext *ctx, arg_fclass01 *a)
3669 {
3670     return do_fop_wew(ctx, a->t, a->r, gen_helper_fsqrt_s);
3671 }
3672 
3673 static bool trans_fsqrt_d(DisasContext *ctx, arg_fclass01 *a)
3674 {
3675     return do_fop_ded(ctx, a->t, a->r, gen_helper_fsqrt_d);
3676 }
3677 
3678 static bool trans_frnd_f(DisasContext *ctx, arg_fclass01 *a)
3679 {
3680     return do_fop_wew(ctx, a->t, a->r, gen_helper_frnd_s);
3681 }
3682 
3683 static bool trans_frnd_d(DisasContext *ctx, arg_fclass01 *a)
3684 {
3685     return do_fop_ded(ctx, a->t, a->r, gen_helper_frnd_d);
3686 }
3687 
3688 static void gen_fneg_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3689 {
3690     tcg_gen_xori_i32(dst, src, INT32_MIN);
3691 }
3692 
3693 static bool trans_fneg_f(DisasContext *ctx, arg_fclass01 *a)
3694 {
3695     return do_fop_wew(ctx, a->t, a->r, gen_fneg_f);
3696 }
3697 
3698 static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3699 {
3700     tcg_gen_xori_i64(dst, src, INT64_MIN);
3701 }
3702 
3703 static bool trans_fneg_d(DisasContext *ctx, arg_fclass01 *a)
3704 {
3705     return do_fop_ded(ctx, a->t, a->r, gen_fneg_d);
3706 }
3707 
3708 static void gen_fnegabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3709 {
3710     tcg_gen_ori_i32(dst, src, INT32_MIN);
3711 }
3712 
3713 static bool trans_fnegabs_f(DisasContext *ctx, arg_fclass01 *a)
3714 {
3715     return do_fop_wew(ctx, a->t, a->r, gen_fnegabs_f);
3716 }
3717 
3718 static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3719 {
3720     tcg_gen_ori_i64(dst, src, INT64_MIN);
3721 }
3722 
3723 static bool trans_fnegabs_d(DisasContext *ctx, arg_fclass01 *a)
3724 {
3725     return do_fop_ded(ctx, a->t, a->r, gen_fnegabs_d);
3726 }
3727 
3728 /*
3729  * Float class 1
3730  */
3731 
3732 static bool trans_fcnv_d_f(DisasContext *ctx, arg_fclass01 *a)
3733 {
3734     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_s);
3735 }
3736 
3737 static bool trans_fcnv_f_d(DisasContext *ctx, arg_fclass01 *a)
3738 {
3739     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_d);
3740 }
3741 
3742 static bool trans_fcnv_w_f(DisasContext *ctx, arg_fclass01 *a)
3743 {
3744     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_w_s);
3745 }
3746 
3747 static bool trans_fcnv_q_f(DisasContext *ctx, arg_fclass01 *a)
3748 {
3749     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_dw_s);
3750 }
3751 
3752 static bool trans_fcnv_w_d(DisasContext *ctx, arg_fclass01 *a)
3753 {
3754     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_w_d);
3755 }
3756 
3757 static bool trans_fcnv_q_d(DisasContext *ctx, arg_fclass01 *a)
3758 {
3759     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_dw_d);
3760 }
3761 
3762 static bool trans_fcnv_f_w(DisasContext *ctx, arg_fclass01 *a)
3763 {
3764     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_w);
3765 }
3766 
3767 static bool trans_fcnv_d_w(DisasContext *ctx, arg_fclass01 *a)
3768 {
3769     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_w);
3770 }
3771 
3772 static bool trans_fcnv_f_q(DisasContext *ctx, arg_fclass01 *a)
3773 {
3774     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_dw);
3775 }
3776 
3777 static bool trans_fcnv_d_q(DisasContext *ctx, arg_fclass01 *a)
3778 {
3779     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_dw);
3780 }
3781 
3782 static bool trans_fcnv_t_f_w(DisasContext *ctx, arg_fclass01 *a)
3783 {
3784     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_w);
3785 }
3786 
3787 static bool trans_fcnv_t_d_w(DisasContext *ctx, arg_fclass01 *a)
3788 {
3789     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_w);
3790 }
3791 
3792 static bool trans_fcnv_t_f_q(DisasContext *ctx, arg_fclass01 *a)
3793 {
3794     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_dw);
3795 }
3796 
3797 static bool trans_fcnv_t_d_q(DisasContext *ctx, arg_fclass01 *a)
3798 {
3799     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_dw);
3800 }
3801 
3802 static bool trans_fcnv_uw_f(DisasContext *ctx, arg_fclass01 *a)
3803 {
3804     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_uw_s);
3805 }
3806 
3807 static bool trans_fcnv_uq_f(DisasContext *ctx, arg_fclass01 *a)
3808 {
3809     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_udw_s);
3810 }
3811 
3812 static bool trans_fcnv_uw_d(DisasContext *ctx, arg_fclass01 *a)
3813 {
3814     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_uw_d);
3815 }
3816 
3817 static bool trans_fcnv_uq_d(DisasContext *ctx, arg_fclass01 *a)
3818 {
3819     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_udw_d);
3820 }
3821 
3822 static bool trans_fcnv_f_uw(DisasContext *ctx, arg_fclass01 *a)
3823 {
3824     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_uw);
3825 }
3826 
3827 static bool trans_fcnv_d_uw(DisasContext *ctx, arg_fclass01 *a)
3828 {
3829     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_uw);
3830 }
3831 
3832 static bool trans_fcnv_f_uq(DisasContext *ctx, arg_fclass01 *a)
3833 {
3834     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_udw);
3835 }
3836 
3837 static bool trans_fcnv_d_uq(DisasContext *ctx, arg_fclass01 *a)
3838 {
3839     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_udw);
3840 }
3841 
3842 static bool trans_fcnv_t_f_uw(DisasContext *ctx, arg_fclass01 *a)
3843 {
3844     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_uw);
3845 }
3846 
3847 static bool trans_fcnv_t_d_uw(DisasContext *ctx, arg_fclass01 *a)
3848 {
3849     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_uw);
3850 }
3851 
3852 static bool trans_fcnv_t_f_uq(DisasContext *ctx, arg_fclass01 *a)
3853 {
3854     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_udw);
3855 }
3856 
3857 static bool trans_fcnv_t_d_uq(DisasContext *ctx, arg_fclass01 *a)
3858 {
3859     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_udw);
3860 }
3861 
3862 /*
3863  * Float class 2
3864  */
3865 
3866 static bool trans_fcmp_f(DisasContext *ctx, arg_fclass2 *a)
3867 {
3868     TCGv_i32 ta, tb, tc, ty;
3869 
3870     nullify_over(ctx);
3871 
3872     ta = load_frw0_i32(a->r1);
3873     tb = load_frw0_i32(a->r2);
3874     ty = tcg_constant_i32(a->y);
3875     tc = tcg_constant_i32(a->c);
3876 
3877     gen_helper_fcmp_s(cpu_env, ta, tb, ty, tc);
3878 
3879     tcg_temp_free_i32(ta);
3880     tcg_temp_free_i32(tb);
3881 
3882     return nullify_end(ctx);
3883 }
3884 
3885 static bool trans_fcmp_d(DisasContext *ctx, arg_fclass2 *a)
3886 {
3887     TCGv_i64 ta, tb;
3888     TCGv_i32 tc, ty;
3889 
3890     nullify_over(ctx);
3891 
3892     ta = load_frd0(a->r1);
3893     tb = load_frd0(a->r2);
3894     ty = tcg_constant_i32(a->y);
3895     tc = tcg_constant_i32(a->c);
3896 
3897     gen_helper_fcmp_d(cpu_env, ta, tb, ty, tc);
3898 
3899     tcg_temp_free_i64(ta);
3900     tcg_temp_free_i64(tb);
3901 
3902     return nullify_end(ctx);
3903 }
3904 
3905 static bool trans_ftest(DisasContext *ctx, arg_ftest *a)
3906 {
3907     TCGv_reg t;
3908 
3909     nullify_over(ctx);
3910 
3911     t = get_temp(ctx);
3912     tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
3913 
3914     if (a->y == 1) {
3915         int mask;
3916         bool inv = false;
3917 
3918         switch (a->c) {
3919         case 0: /* simple */
3920             tcg_gen_andi_reg(t, t, 0x4000000);
3921             ctx->null_cond = cond_make_0(TCG_COND_NE, t);
3922             goto done;
3923         case 2: /* rej */
3924             inv = true;
3925             /* fallthru */
3926         case 1: /* acc */
3927             mask = 0x43ff800;
3928             break;
3929         case 6: /* rej8 */
3930             inv = true;
3931             /* fallthru */
3932         case 5: /* acc8 */
3933             mask = 0x43f8000;
3934             break;
3935         case 9: /* acc6 */
3936             mask = 0x43e0000;
3937             break;
3938         case 13: /* acc4 */
3939             mask = 0x4380000;
3940             break;
3941         case 17: /* acc2 */
3942             mask = 0x4200000;
3943             break;
3944         default:
3945             gen_illegal(ctx);
3946             return true;
3947         }
3948         if (inv) {
3949             TCGv_reg c = load_const(ctx, mask);
3950             tcg_gen_or_reg(t, t, c);
3951             ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
3952         } else {
3953             tcg_gen_andi_reg(t, t, mask);
3954             ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
3955         }
3956     } else {
3957         unsigned cbit = (a->y ^ 1) - 1;
3958 
3959         tcg_gen_extract_reg(t, t, 21 - cbit, 1);
3960         ctx->null_cond = cond_make_0(TCG_COND_NE, t);
3961         tcg_temp_free(t);
3962     }
3963 
3964  done:
3965     return nullify_end(ctx);
3966 }
3967 
3968 /*
3969  * Float class 2
3970  */
3971 
3972 static bool trans_fadd_f(DisasContext *ctx, arg_fclass3 *a)
3973 {
3974     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fadd_s);
3975 }
3976 
3977 static bool trans_fadd_d(DisasContext *ctx, arg_fclass3 *a)
3978 {
3979     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fadd_d);
3980 }
3981 
3982 static bool trans_fsub_f(DisasContext *ctx, arg_fclass3 *a)
3983 {
3984     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fsub_s);
3985 }
3986 
3987 static bool trans_fsub_d(DisasContext *ctx, arg_fclass3 *a)
3988 {
3989     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fsub_d);
3990 }
3991 
3992 static bool trans_fmpy_f(DisasContext *ctx, arg_fclass3 *a)
3993 {
3994     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_s);
3995 }
3996 
3997 static bool trans_fmpy_d(DisasContext *ctx, arg_fclass3 *a)
3998 {
3999     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_d);
4000 }
4001 
4002 static bool trans_fdiv_f(DisasContext *ctx, arg_fclass3 *a)
4003 {
4004     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_s);
4005 }
4006 
4007 static bool trans_fdiv_d(DisasContext *ctx, arg_fclass3 *a)
4008 {
4009     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_d);
4010 }
4011 
4012 static bool trans_xmpyu(DisasContext *ctx, arg_xmpyu *a)
4013 {
4014     TCGv_i64 x, y;
4015 
4016     nullify_over(ctx);
4017 
4018     x = load_frw0_i64(a->r1);
4019     y = load_frw0_i64(a->r2);
4020     tcg_gen_mul_i64(x, x, y);
4021     save_frd(a->t, x);
4022     tcg_temp_free_i64(x);
4023     tcg_temp_free_i64(y);
4024 
4025     return nullify_end(ctx);
4026 }
4027 
4028 /* Convert the fmpyadd single-precision register encodings to standard.  */
4029 static inline int fmpyadd_s_reg(unsigned r)
4030 {
4031     return (r & 16) * 2 + 16 + (r & 15);
4032 }
4033 
4034 static bool do_fmpyadd_s(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4035 {
4036     int tm = fmpyadd_s_reg(a->tm);
4037     int ra = fmpyadd_s_reg(a->ra);
4038     int ta = fmpyadd_s_reg(a->ta);
4039     int rm2 = fmpyadd_s_reg(a->rm2);
4040     int rm1 = fmpyadd_s_reg(a->rm1);
4041 
4042     nullify_over(ctx);
4043 
4044     do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
4045     do_fop_weww(ctx, ta, ta, ra,
4046                 is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
4047 
4048     return nullify_end(ctx);
4049 }
4050 
4051 static bool trans_fmpyadd_f(DisasContext *ctx, arg_mpyadd *a)
4052 {
4053     return do_fmpyadd_s(ctx, a, false);
4054 }
4055 
4056 static bool trans_fmpysub_f(DisasContext *ctx, arg_mpyadd *a)
4057 {
4058     return do_fmpyadd_s(ctx, a, true);
4059 }
4060 
4061 static bool do_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4062 {
4063     nullify_over(ctx);
4064 
4065     do_fop_dedd(ctx, a->tm, a->rm1, a->rm2, gen_helper_fmpy_d);
4066     do_fop_dedd(ctx, a->ta, a->ta, a->ra,
4067                 is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
4068 
4069     return nullify_end(ctx);
4070 }
4071 
4072 static bool trans_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a)
4073 {
4074     return do_fmpyadd_d(ctx, a, false);
4075 }
4076 
4077 static bool trans_fmpysub_d(DisasContext *ctx, arg_mpyadd *a)
4078 {
4079     return do_fmpyadd_d(ctx, a, true);
4080 }
4081 
4082 static bool trans_fmpyfadd_f(DisasContext *ctx, arg_fmpyfadd_f *a)
4083 {
4084     TCGv_i32 x, y, z;
4085 
4086     nullify_over(ctx);
4087     x = load_frw0_i32(a->rm1);
4088     y = load_frw0_i32(a->rm2);
4089     z = load_frw0_i32(a->ra3);
4090 
4091     if (a->neg) {
4092         gen_helper_fmpynfadd_s(x, cpu_env, x, y, z);
4093     } else {
4094         gen_helper_fmpyfadd_s(x, cpu_env, x, y, z);
4095     }
4096 
4097     tcg_temp_free_i32(y);
4098     tcg_temp_free_i32(z);
4099     save_frw_i32(a->t, x);
4100     tcg_temp_free_i32(x);
4101     return nullify_end(ctx);
4102 }
4103 
4104 static bool trans_fmpyfadd_d(DisasContext *ctx, arg_fmpyfadd_d *a)
4105 {
4106     TCGv_i64 x, y, z;
4107 
4108     nullify_over(ctx);
4109     x = load_frd0(a->rm1);
4110     y = load_frd0(a->rm2);
4111     z = load_frd0(a->ra3);
4112 
4113     if (a->neg) {
4114         gen_helper_fmpynfadd_d(x, cpu_env, x, y, z);
4115     } else {
4116         gen_helper_fmpyfadd_d(x, cpu_env, x, y, z);
4117     }
4118 
4119     tcg_temp_free_i64(y);
4120     tcg_temp_free_i64(z);
4121     save_frd(a->t, x);
4122     tcg_temp_free_i64(x);
4123     return nullify_end(ctx);
4124 }
4125 
4126 static bool trans_diag(DisasContext *ctx, arg_diag *a)
4127 {
4128     qemu_log_mask(LOG_UNIMP, "DIAG opcode ignored\n");
4129     cond_free(&ctx->null_cond);
4130     return true;
4131 }
4132 
4133 static void hppa_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
4134 {
4135     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4136     int bound;
4137 
4138     ctx->cs = cs;
4139     ctx->tb_flags = ctx->base.tb->flags;
4140 
4141 #ifdef CONFIG_USER_ONLY
4142     ctx->privilege = MMU_USER_IDX;
4143     ctx->mmu_idx = MMU_USER_IDX;
4144     ctx->iaoq_f = ctx->base.pc_first | MMU_USER_IDX;
4145     ctx->iaoq_b = ctx->base.tb->cs_base | MMU_USER_IDX;
4146     ctx->unalign = (ctx->tb_flags & TB_FLAG_UNALIGN ? MO_UNALN : MO_ALIGN);
4147 #else
4148     ctx->privilege = (ctx->tb_flags >> TB_FLAG_PRIV_SHIFT) & 3;
4149     ctx->mmu_idx = (ctx->tb_flags & PSW_D ? ctx->privilege : MMU_PHYS_IDX);
4150 
4151     /* Recover the IAOQ values from the GVA + PRIV.  */
4152     uint64_t cs_base = ctx->base.tb->cs_base;
4153     uint64_t iasq_f = cs_base & ~0xffffffffull;
4154     int32_t diff = cs_base;
4155 
4156     ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
4157     ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
4158 #endif
4159     ctx->iaoq_n = -1;
4160     ctx->iaoq_n_var = NULL;
4161 
4162     /* Bound the number of instructions by those left on the page.  */
4163     bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
4164     ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
4165 
4166     ctx->ntempr = 0;
4167     ctx->ntempl = 0;
4168     memset(ctx->tempr, 0, sizeof(ctx->tempr));
4169     memset(ctx->templ, 0, sizeof(ctx->templ));
4170 }
4171 
4172 static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
4173 {
4174     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4175 
4176     /* Seed the nullification status from PSW[N], as saved in TB->FLAGS.  */
4177     ctx->null_cond = cond_make_f();
4178     ctx->psw_n_nonzero = false;
4179     if (ctx->tb_flags & PSW_N) {
4180         ctx->null_cond.c = TCG_COND_ALWAYS;
4181         ctx->psw_n_nonzero = true;
4182     }
4183     ctx->null_lab = NULL;
4184 }
4185 
4186 static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
4187 {
4188     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4189 
4190     tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b);
4191 }
4192 
4193 static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
4194 {
4195     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4196     CPUHPPAState *env = cs->env_ptr;
4197     DisasJumpType ret;
4198     int i, n;
4199 
4200     /* Execute one insn.  */
4201 #ifdef CONFIG_USER_ONLY
4202     if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
4203         do_page_zero(ctx);
4204         ret = ctx->base.is_jmp;
4205         assert(ret != DISAS_NEXT);
4206     } else
4207 #endif
4208     {
4209         /* Always fetch the insn, even if nullified, so that we check
4210            the page permissions for execute.  */
4211         uint32_t insn = translator_ldl(env, &ctx->base, ctx->base.pc_next);
4212 
4213         /* Set up the IA queue for the next insn.
4214            This will be overwritten by a branch.  */
4215         if (ctx->iaoq_b == -1) {
4216             ctx->iaoq_n = -1;
4217             ctx->iaoq_n_var = get_temp(ctx);
4218             tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
4219         } else {
4220             ctx->iaoq_n = ctx->iaoq_b + 4;
4221             ctx->iaoq_n_var = NULL;
4222         }
4223 
4224         if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
4225             ctx->null_cond.c = TCG_COND_NEVER;
4226             ret = DISAS_NEXT;
4227         } else {
4228             ctx->insn = insn;
4229             if (!decode(ctx, insn)) {
4230                 gen_illegal(ctx);
4231             }
4232             ret = ctx->base.is_jmp;
4233             assert(ctx->null_lab == NULL);
4234         }
4235     }
4236 
4237     /* Free any temporaries allocated.  */
4238     for (i = 0, n = ctx->ntempr; i < n; ++i) {
4239         tcg_temp_free(ctx->tempr[i]);
4240         ctx->tempr[i] = NULL;
4241     }
4242     for (i = 0, n = ctx->ntempl; i < n; ++i) {
4243         tcg_temp_free_tl(ctx->templ[i]);
4244         ctx->templ[i] = NULL;
4245     }
4246     ctx->ntempr = 0;
4247     ctx->ntempl = 0;
4248 
4249     /* Advance the insn queue.  Note that this check also detects
4250        a priority change within the instruction queue.  */
4251     if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
4252         if (ctx->iaoq_b != -1 && ctx->iaoq_n != -1
4253             && use_goto_tb(ctx, ctx->iaoq_b)
4254             && (ctx->null_cond.c == TCG_COND_NEVER
4255                 || ctx->null_cond.c == TCG_COND_ALWAYS)) {
4256             nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
4257             gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
4258             ctx->base.is_jmp = ret = DISAS_NORETURN;
4259         } else {
4260             ctx->base.is_jmp = ret = DISAS_IAQ_N_STALE;
4261         }
4262     }
4263     ctx->iaoq_f = ctx->iaoq_b;
4264     ctx->iaoq_b = ctx->iaoq_n;
4265     ctx->base.pc_next += 4;
4266 
4267     switch (ret) {
4268     case DISAS_NORETURN:
4269     case DISAS_IAQ_N_UPDATED:
4270         break;
4271 
4272     case DISAS_NEXT:
4273     case DISAS_IAQ_N_STALE:
4274     case DISAS_IAQ_N_STALE_EXIT:
4275         if (ctx->iaoq_f == -1) {
4276             tcg_gen_mov_reg(cpu_iaoq_f, cpu_iaoq_b);
4277             copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
4278 #ifndef CONFIG_USER_ONLY
4279             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4280 #endif
4281             nullify_save(ctx);
4282             ctx->base.is_jmp = (ret == DISAS_IAQ_N_STALE_EXIT
4283                                 ? DISAS_EXIT
4284                                 : DISAS_IAQ_N_UPDATED);
4285         } else if (ctx->iaoq_b == -1) {
4286             tcg_gen_mov_reg(cpu_iaoq_b, ctx->iaoq_n_var);
4287         }
4288         break;
4289 
4290     default:
4291         g_assert_not_reached();
4292     }
4293 }
4294 
4295 static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
4296 {
4297     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4298     DisasJumpType is_jmp = ctx->base.is_jmp;
4299 
4300     switch (is_jmp) {
4301     case DISAS_NORETURN:
4302         break;
4303     case DISAS_TOO_MANY:
4304     case DISAS_IAQ_N_STALE:
4305     case DISAS_IAQ_N_STALE_EXIT:
4306         copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
4307         copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
4308         nullify_save(ctx);
4309         /* FALLTHRU */
4310     case DISAS_IAQ_N_UPDATED:
4311         if (is_jmp != DISAS_IAQ_N_STALE_EXIT) {
4312             tcg_gen_lookup_and_goto_ptr();
4313             break;
4314         }
4315         /* FALLTHRU */
4316     case DISAS_EXIT:
4317         tcg_gen_exit_tb(NULL, 0);
4318         break;
4319     default:
4320         g_assert_not_reached();
4321     }
4322 }
4323 
4324 static void hppa_tr_disas_log(const DisasContextBase *dcbase,
4325                               CPUState *cs, FILE *logfile)
4326 {
4327     target_ulong pc = dcbase->pc_first;
4328 
4329 #ifdef CONFIG_USER_ONLY
4330     switch (pc) {
4331     case 0x00:
4332         fprintf(logfile, "IN:\n0x00000000:  (null)\n");
4333         return;
4334     case 0xb0:
4335         fprintf(logfile, "IN:\n0x000000b0:  light-weight-syscall\n");
4336         return;
4337     case 0xe0:
4338         fprintf(logfile, "IN:\n0x000000e0:  set-thread-pointer-syscall\n");
4339         return;
4340     case 0x100:
4341         fprintf(logfile, "IN:\n0x00000100:  syscall\n");
4342         return;
4343     }
4344 #endif
4345 
4346     fprintf(logfile, "IN: %s\n", lookup_symbol(pc));
4347     target_disas(logfile, cs, pc, dcbase->tb->size);
4348 }
4349 
4350 static const TranslatorOps hppa_tr_ops = {
4351     .init_disas_context = hppa_tr_init_disas_context,
4352     .tb_start           = hppa_tr_tb_start,
4353     .insn_start         = hppa_tr_insn_start,
4354     .translate_insn     = hppa_tr_translate_insn,
4355     .tb_stop            = hppa_tr_tb_stop,
4356     .disas_log          = hppa_tr_disas_log,
4357 };
4358 
4359 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
4360                            target_ulong pc, void *host_pc)
4361 {
4362     DisasContext ctx;
4363     translator_loop(cs, tb, max_insns, pc, host_pc, &hppa_tr_ops, &ctx.base);
4364 }
4365