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