2 * HPPA emulation cpu translation for qemu.
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.
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.
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/>.
20 #include "qemu/osdep.h"
22 #include "disas/disas.h"
23 #include "qemu/host-utils.h"
24 #include "exec/exec-all.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"
33 /* Since we have a distinction between register size and address size,
34 we need to redefine all of these. */
38 #undef tcg_global_reg_new
39 #undef tcg_global_mem_new
40 #undef tcg_temp_local_new
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
50 #define tcg_gen_extu_reg_tl tcg_gen_extu_i32_i64
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
59 #if TARGET_REGISTER_BITS == 64
60 #define TCGv_reg TCGv_i64
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
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
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
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 */
251 typedef struct DisasCond {
258 typedef struct DisasContext {
259 DisasContextBase base;
281 /* Note that ssm/rsm instructions number PSW_W and PSW_E differently. */
282 static int expand_sm_imm(int val)
284 if (val & PSW_SM_E) {
285 val = (val & ~PSW_SM_E) | PSW_E;
287 if (val & PSW_SM_W) {
288 val = (val & ~PSW_SM_W) | PSW_W;
293 /* Inverted space register indicates 0 means sr0 not inferred from base. */
294 static int expand_sr3x(int val)
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)
303 return val & 2 ? (val & 1 ? -1 : 1) : 0;
306 /* Convert the sign of the displacement to a pre or post-modify. */
307 static int pos_to_m(int val)
312 static int neg_to_m(int val)
317 /* Used for branch targets and fp memory ops. */
318 static int expand_shl2(int val)
323 /* Used for fp memory ops. */
324 static int expand_shl3(int val)
329 /* Used for assemble_21. */
330 static int expand_shl11(int val)
336 /* Include the auto-generated decoder. */
337 #include "decode.inc.c"
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
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
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
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;
365 #include "exec/gen-icount.h"
367 void hppa_translate_init(void)
369 #define DEF_VAR(V) { &cpu_##V, #V, offsetof(CPUHPPAState, V) }
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]) },
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"
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"
399 for (i = 1; i < 32; i++) {
400 cpu_gr[i] = tcg_global_mem_new(cpu_env,
401 offsetof(CPUHPPAState, gr[i]),
404 for (i = 0; i < 4; i++) {
405 cpu_sr[i] = tcg_global_mem_new_i64(cpu_env,
406 offsetof(CPUHPPAState, sr[i]),
409 cpu_srH = tcg_global_mem_new_i64(cpu_env,
410 offsetof(CPUHPPAState, sr[4]),
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);
418 cpu_iasq_f = tcg_global_mem_new_i64(cpu_env,
419 offsetof(CPUHPPAState, iasq_f),
421 cpu_iasq_b = tcg_global_mem_new_i64(cpu_env,
422 offsetof(CPUHPPAState, iasq_b),
426 static DisasCond cond_make_f(void)
435 static DisasCond cond_make_t(void)
438 .c = TCG_COND_ALWAYS,
444 static DisasCond cond_make_n(void)
455 static DisasCond cond_make_0_tmp(TCGCond c, TCGv_reg a0)
457 assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
459 .c = c, .a0 = a0, .a1_is_0 = true
463 static DisasCond cond_make_0(TCGCond c, TCGv_reg a0)
465 TCGv_reg tmp = tcg_temp_new();
466 tcg_gen_mov_reg(tmp, a0);
467 return cond_make_0_tmp(c, tmp);
470 static DisasCond cond_make(TCGCond c, TCGv_reg a0, TCGv_reg a1)
472 DisasCond r = { .c = c };
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);
483 static void cond_prep(DisasCond *cond)
486 cond->a1_is_0 = false;
487 cond->a1 = tcg_const_reg(0);
491 static void cond_free(DisasCond *cond)
495 if (!cond->a0_is_n) {
496 tcg_temp_free(cond->a0);
498 if (!cond->a1_is_0) {
499 tcg_temp_free(cond->a1);
501 cond->a0_is_n = false;
502 cond->a1_is_0 = false;
506 case TCG_COND_ALWAYS:
507 cond->c = TCG_COND_NEVER;
514 static TCGv_reg get_temp(DisasContext *ctx)
516 unsigned i = ctx->ntempr++;
517 g_assert(i < ARRAY_SIZE(ctx->tempr));
518 return ctx->tempr[i] = tcg_temp_new();
521 #ifndef CONFIG_USER_ONLY
522 static TCGv_tl get_temp_tl(DisasContext *ctx)
524 unsigned i = ctx->ntempl++;
525 g_assert(i < ARRAY_SIZE(ctx->templ));
526 return ctx->templ[i] = tcg_temp_new_tl();
530 static TCGv_reg load_const(DisasContext *ctx, target_sreg v)
532 TCGv_reg t = get_temp(ctx);
533 tcg_gen_movi_reg(t, v);
537 static TCGv_reg load_gpr(DisasContext *ctx, unsigned reg)
540 TCGv_reg t = get_temp(ctx);
541 tcg_gen_movi_reg(t, 0);
548 static TCGv_reg dest_gpr(DisasContext *ctx, unsigned reg)
550 if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
551 return get_temp(ctx);
557 static void save_or_nullify(DisasContext *ctx, TCGv_reg dest, TCGv_reg t)
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);
564 tcg_gen_mov_reg(dest, t);
568 static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_reg t)
571 save_or_nullify(ctx, cpu_gr[reg], t);
575 #ifdef HOST_WORDS_BIGENDIAN
583 static TCGv_i32 load_frw_i32(unsigned rt)
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));
592 static TCGv_i32 load_frw0_i32(unsigned rt)
595 return tcg_const_i32(0);
597 return load_frw_i32(rt);
601 static TCGv_i64 load_frw0_i64(unsigned rt)
604 return tcg_const_i64(0);
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));
614 static void save_frw_i32(unsigned rt, TCGv_i32 val)
616 tcg_gen_st_i32(val, cpu_env,
617 offsetof(CPUHPPAState, fr[rt & 31])
618 + (rt & 32 ? LO_OFS : HI_OFS));
624 static TCGv_i64 load_frd(unsigned rt)
626 TCGv_i64 ret = tcg_temp_new_i64();
627 tcg_gen_ld_i64(ret, cpu_env, offsetof(CPUHPPAState, fr[rt]));
631 static TCGv_i64 load_frd0(unsigned rt)
634 return tcg_const_i64(0);
640 static void save_frd(unsigned rt, TCGv_i64 val)
642 tcg_gen_st_i64(val, cpu_env, offsetof(CPUHPPAState, fr[rt]));
645 static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
647 #ifdef CONFIG_USER_ONLY
648 tcg_gen_movi_i64(dest, 0);
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);
655 tcg_gen_ld_i64(dest, cpu_env, offsetof(CPUHPPAState, sr[reg]));
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)
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);
668 ctx->null_lab = gen_new_label();
669 cond_prep(&ctx->null_cond);
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);
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);
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);
691 /* Save the current nullification state to PSW[N]. */
692 static void nullify_save(DisasContext *ctx)
694 if (ctx->null_cond.c == TCG_COND_NEVER) {
695 if (ctx->psw_n_nonzero) {
696 tcg_gen_movi_reg(cpu_psw_n, 0);
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;
706 cond_free(&ctx->null_cond);
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)
714 if (ctx->psw_n_nonzero || x) {
715 tcg_gen_movi_reg(cpu_psw_n, x);
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)
724 TCGLabel *null_lab = ctx->null_lab;
725 DisasJumpType status = ctx->base.is_jmp;
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);
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. */
737 ctx->null_lab = NULL;
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);
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. */
749 gen_set_label(null_lab);
750 ctx->null_cond = cond_make_n();
752 if (status == DISAS_NORETURN) {
753 ctx->base.is_jmp = DISAS_NEXT;
758 static void copy_iaoq_entry(TCGv_reg dest, target_ureg ival, TCGv_reg vval)
760 if (unlikely(ival == -1)) {
761 tcg_gen_mov_reg(dest, vval);
763 tcg_gen_movi_reg(dest, ival);
767 static inline target_ureg iaoq_dest(DisasContext *ctx, target_sreg disp)
769 return ctx->iaoq_f + disp + 8;
772 static void gen_excp_1(int exception)
774 TCGv_i32 t = tcg_const_i32(exception);
775 gen_helper_excp(cpu_env, t);
776 tcg_temp_free_i32(t);
779 static void gen_excp(DisasContext *ctx, int exception)
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);
784 gen_excp_1(exception);
785 ctx->base.is_jmp = DISAS_NORETURN;
788 static bool gen_excp_iir(DisasContext *ctx, int exc)
793 tmp = tcg_const_reg(ctx->insn);
794 tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[CR_IIR]));
797 return nullify_end(ctx);
800 static bool gen_illegal(DisasContext *ctx)
802 return gen_excp_iir(ctx, EXCP_ILL);
805 #ifdef CONFIG_USER_ONLY
806 #define CHECK_MOST_PRIVILEGED(EXCP) \
807 return gen_excp_iir(ctx, EXCP)
809 #define CHECK_MOST_PRIVILEGED(EXCP) \
811 if (ctx->privilege != 0) { \
812 return gen_excp_iir(ctx, EXCP); \
817 static bool use_goto_tb(DisasContext *ctx, target_ureg dest)
819 /* Suppress goto_tb for page crossing, IO, or single-steping. */
820 return !(((ctx->base.pc_first ^ dest) & TARGET_PAGE_MASK)
821 || (tb_cflags(ctx->base.tb) & CF_LAST_IO)
822 || ctx->base.singlestep_enabled);
825 /* If the next insn is to be nullified, and it's on the same page,
826 and we're not attempting to set a breakpoint on it, then we can
827 totally skip the nullified insn. This avoids creating and
828 executing a TB that merely branches to the next TB. */
829 static bool use_nullify_skip(DisasContext *ctx)
831 return (((ctx->iaoq_b ^ ctx->iaoq_f) & TARGET_PAGE_MASK) == 0
832 && !cpu_breakpoint_test(ctx->cs, ctx->iaoq_b, BP_ANY));
835 static void gen_goto_tb(DisasContext *ctx, int which,
836 target_ureg f, target_ureg b)
838 if (f != -1 && b != -1 && use_goto_tb(ctx, f)) {
839 tcg_gen_goto_tb(which);
840 tcg_gen_movi_reg(cpu_iaoq_f, f);
841 tcg_gen_movi_reg(cpu_iaoq_b, b);
842 tcg_gen_exit_tb(ctx->base.tb, which);
844 copy_iaoq_entry(cpu_iaoq_f, f, cpu_iaoq_b);
845 copy_iaoq_entry(cpu_iaoq_b, b, ctx->iaoq_n_var);
846 if (ctx->base.singlestep_enabled) {
847 gen_excp_1(EXCP_DEBUG);
849 tcg_gen_lookup_and_goto_ptr();
854 static bool cond_need_sv(int c)
856 return c == 2 || c == 3 || c == 6;
859 static bool cond_need_cb(int c)
861 return c == 4 || c == 5;
865 * Compute conditional for arithmetic. See Page 5-3, Table 5-1, of
866 * the Parisc 1.1 Architecture Reference Manual for details.
869 static DisasCond do_cond(unsigned cf, TCGv_reg res,
870 TCGv_reg cb_msb, TCGv_reg sv)
876 case 0: /* Never / TR (0 / 1) */
877 cond = cond_make_f();
879 case 1: /* = / <> (Z / !Z) */
880 cond = cond_make_0(TCG_COND_EQ, res);
882 case 2: /* < / >= (N ^ V / !(N ^ V) */
883 tmp = tcg_temp_new();
884 tcg_gen_xor_reg(tmp, res, sv);
885 cond = cond_make_0_tmp(TCG_COND_LT, tmp);
887 case 3: /* <= / > (N ^ V) | Z / !((N ^ V) | Z) */
891 * ((res < 0) ^ (sv < 0)) | !res
892 * ((res ^ sv) < 0) | !res
893 * (~(res ^ sv) >= 0) | !res
894 * !(~(res ^ sv) >> 31) | !res
895 * !(~(res ^ sv) >> 31 & res)
897 tmp = tcg_temp_new();
898 tcg_gen_eqv_reg(tmp, res, sv);
899 tcg_gen_sari_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
900 tcg_gen_and_reg(tmp, tmp, res);
901 cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
903 case 4: /* NUV / UV (!C / C) */
904 cond = cond_make_0(TCG_COND_EQ, cb_msb);
906 case 5: /* ZNV / VNZ (!C | Z / C & !Z) */
907 tmp = tcg_temp_new();
908 tcg_gen_neg_reg(tmp, cb_msb);
909 tcg_gen_and_reg(tmp, tmp, res);
910 cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
912 case 6: /* SV / NSV (V / !V) */
913 cond = cond_make_0(TCG_COND_LT, sv);
915 case 7: /* OD / EV */
916 tmp = tcg_temp_new();
917 tcg_gen_andi_reg(tmp, res, 1);
918 cond = cond_make_0_tmp(TCG_COND_NE, tmp);
921 g_assert_not_reached();
924 cond.c = tcg_invert_cond(cond.c);
930 /* Similar, but for the special case of subtraction without borrow, we
931 can use the inputs directly. This can allow other computation to be
932 deleted as unused. */
934 static DisasCond do_sub_cond(unsigned cf, TCGv_reg res,
935 TCGv_reg in1, TCGv_reg in2, TCGv_reg sv)
941 cond = cond_make(TCG_COND_EQ, in1, in2);
944 cond = cond_make(TCG_COND_LT, in1, in2);
947 cond = cond_make(TCG_COND_LE, in1, in2);
949 case 4: /* << / >>= */
950 cond = cond_make(TCG_COND_LTU, in1, in2);
952 case 5: /* <<= / >> */
953 cond = cond_make(TCG_COND_LEU, in1, in2);
956 return do_cond(cf, res, NULL, sv);
959 cond.c = tcg_invert_cond(cond.c);
966 * Similar, but for logicals, where the carry and overflow bits are not
967 * computed, and use of them is undefined.
969 * Undefined or not, hardware does not trap. It seems reasonable to
970 * assume hardware treats cases c={4,5,6} as if C=0 & V=0, since that's
971 * how cases c={2,3} are treated.
974 static DisasCond do_log_cond(unsigned cf, TCGv_reg res)
978 case 9: /* undef, C */
979 case 11: /* undef, C & !Z */
980 case 12: /* undef, V */
981 return cond_make_f();
984 case 8: /* undef, !C */
985 case 10: /* undef, !C | Z */
986 case 13: /* undef, !V */
987 return cond_make_t();
990 return cond_make_0(TCG_COND_EQ, res);
992 return cond_make_0(TCG_COND_NE, res);
994 return cond_make_0(TCG_COND_LT, res);
996 return cond_make_0(TCG_COND_GE, res);
998 return cond_make_0(TCG_COND_LE, res);
1000 return cond_make_0(TCG_COND_GT, res);
1004 return do_cond(cf, res, NULL, NULL);
1007 g_assert_not_reached();
1011 /* Similar, but for shift/extract/deposit conditions. */
1013 static DisasCond do_sed_cond(unsigned orig, TCGv_reg res)
1017 /* Convert the compressed condition codes to standard.
1018 0-2 are the same as logicals (nv,<,<=), while 3 is OD.
1019 4-7 are the reverse of 0-3. */
1026 return do_log_cond(c * 2 + f, res);
1029 /* Similar, but for unit conditions. */
1031 static DisasCond do_unit_cond(unsigned cf, TCGv_reg res,
1032 TCGv_reg in1, TCGv_reg in2)
1035 TCGv_reg tmp, cb = NULL;
1038 /* Since we want to test lots of carry-out bits all at once, do not
1039 * do our normal thing and compute carry-in of bit B+1 since that
1040 * leaves us with carry bits spread across two words.
1042 cb = tcg_temp_new();
1043 tmp = tcg_temp_new();
1044 tcg_gen_or_reg(cb, in1, in2);
1045 tcg_gen_and_reg(tmp, in1, in2);
1046 tcg_gen_andc_reg(cb, cb, res);
1047 tcg_gen_or_reg(cb, cb, tmp);
1052 case 0: /* never / TR */
1053 case 1: /* undefined */
1054 case 5: /* undefined */
1055 cond = cond_make_f();
1058 case 2: /* SBZ / NBZ */
1059 /* See hasless(v,1) from
1060 * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
1062 tmp = tcg_temp_new();
1063 tcg_gen_subi_reg(tmp, res, 0x01010101u);
1064 tcg_gen_andc_reg(tmp, tmp, res);
1065 tcg_gen_andi_reg(tmp, tmp, 0x80808080u);
1066 cond = cond_make_0(TCG_COND_NE, tmp);
1070 case 3: /* SHZ / NHZ */
1071 tmp = tcg_temp_new();
1072 tcg_gen_subi_reg(tmp, res, 0x00010001u);
1073 tcg_gen_andc_reg(tmp, tmp, res);
1074 tcg_gen_andi_reg(tmp, tmp, 0x80008000u);
1075 cond = cond_make_0(TCG_COND_NE, tmp);
1079 case 4: /* SDC / NDC */
1080 tcg_gen_andi_reg(cb, cb, 0x88888888u);
1081 cond = cond_make_0(TCG_COND_NE, cb);
1084 case 6: /* SBC / NBC */
1085 tcg_gen_andi_reg(cb, cb, 0x80808080u);
1086 cond = cond_make_0(TCG_COND_NE, cb);
1089 case 7: /* SHC / NHC */
1090 tcg_gen_andi_reg(cb, cb, 0x80008000u);
1091 cond = cond_make_0(TCG_COND_NE, cb);
1095 g_assert_not_reached();
1101 cond.c = tcg_invert_cond(cond.c);
1107 /* Compute signed overflow for addition. */
1108 static TCGv_reg do_add_sv(DisasContext *ctx, TCGv_reg res,
1109 TCGv_reg in1, TCGv_reg in2)
1111 TCGv_reg sv = get_temp(ctx);
1112 TCGv_reg tmp = tcg_temp_new();
1114 tcg_gen_xor_reg(sv, res, in1);
1115 tcg_gen_xor_reg(tmp, in1, in2);
1116 tcg_gen_andc_reg(sv, sv, tmp);
1122 /* Compute signed overflow for subtraction. */
1123 static TCGv_reg do_sub_sv(DisasContext *ctx, TCGv_reg res,
1124 TCGv_reg in1, TCGv_reg in2)
1126 TCGv_reg sv = get_temp(ctx);
1127 TCGv_reg tmp = tcg_temp_new();
1129 tcg_gen_xor_reg(sv, res, in1);
1130 tcg_gen_xor_reg(tmp, in1, in2);
1131 tcg_gen_and_reg(sv, sv, tmp);
1137 static void do_add(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1138 TCGv_reg in2, unsigned shift, bool is_l,
1139 bool is_tsv, bool is_tc, bool is_c, unsigned cf)
1141 TCGv_reg dest, cb, cb_msb, sv, tmp;
1142 unsigned c = cf >> 1;
1145 dest = tcg_temp_new();
1150 tmp = get_temp(ctx);
1151 tcg_gen_shli_reg(tmp, in1, shift);
1155 if (!is_l || cond_need_cb(c)) {
1156 TCGv_reg zero = tcg_const_reg(0);
1157 cb_msb = get_temp(ctx);
1158 tcg_gen_add2_reg(dest, cb_msb, in1, zero, in2, zero);
1160 tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cpu_psw_cb_msb, zero);
1162 tcg_temp_free(zero);
1165 tcg_gen_xor_reg(cb, in1, in2);
1166 tcg_gen_xor_reg(cb, cb, dest);
1169 tcg_gen_add_reg(dest, in1, in2);
1171 tcg_gen_add_reg(dest, dest, cpu_psw_cb_msb);
1175 /* Compute signed overflow if required. */
1177 if (is_tsv || cond_need_sv(c)) {
1178 sv = do_add_sv(ctx, dest, in1, in2);
1180 /* ??? Need to include overflow from shift. */
1181 gen_helper_tsv(cpu_env, sv);
1185 /* Emit any conditional trap before any writeback. */
1186 cond = do_cond(cf, dest, cb_msb, sv);
1189 tmp = tcg_temp_new();
1190 tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1191 gen_helper_tcond(cpu_env, tmp);
1195 /* Write back the result. */
1197 save_or_nullify(ctx, cpu_psw_cb, cb);
1198 save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1200 save_gpr(ctx, rt, dest);
1201 tcg_temp_free(dest);
1203 /* Install the new nullification. */
1204 cond_free(&ctx->null_cond);
1205 ctx->null_cond = cond;
1208 static bool do_add_reg(DisasContext *ctx, arg_rrr_cf_sh *a,
1209 bool is_l, bool is_tsv, bool is_tc, bool is_c)
1211 TCGv_reg tcg_r1, tcg_r2;
1216 tcg_r1 = load_gpr(ctx, a->r1);
1217 tcg_r2 = load_gpr(ctx, a->r2);
1218 do_add(ctx, a->t, tcg_r1, tcg_r2, a->sh, is_l, is_tsv, is_tc, is_c, a->cf);
1219 return nullify_end(ctx);
1222 static bool do_add_imm(DisasContext *ctx, arg_rri_cf *a,
1223 bool is_tsv, bool is_tc)
1225 TCGv_reg tcg_im, tcg_r2;
1230 tcg_im = load_const(ctx, a->i);
1231 tcg_r2 = load_gpr(ctx, a->r);
1232 do_add(ctx, a->t, tcg_im, tcg_r2, 0, 0, is_tsv, is_tc, 0, a->cf);
1233 return nullify_end(ctx);
1236 static void do_sub(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1237 TCGv_reg in2, bool is_tsv, bool is_b,
1238 bool is_tc, unsigned cf)
1240 TCGv_reg dest, sv, cb, cb_msb, zero, tmp;
1241 unsigned c = cf >> 1;
1244 dest = tcg_temp_new();
1245 cb = tcg_temp_new();
1246 cb_msb = tcg_temp_new();
1248 zero = tcg_const_reg(0);
1250 /* DEST,C = IN1 + ~IN2 + C. */
1251 tcg_gen_not_reg(cb, in2);
1252 tcg_gen_add2_reg(dest, cb_msb, in1, zero, cpu_psw_cb_msb, zero);
1253 tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cb, zero);
1254 tcg_gen_xor_reg(cb, cb, in1);
1255 tcg_gen_xor_reg(cb, cb, dest);
1257 /* DEST,C = IN1 + ~IN2 + 1. We can produce the same result in fewer
1258 operations by seeding the high word with 1 and subtracting. */
1259 tcg_gen_movi_reg(cb_msb, 1);
1260 tcg_gen_sub2_reg(dest, cb_msb, in1, cb_msb, in2, zero);
1261 tcg_gen_eqv_reg(cb, in1, in2);
1262 tcg_gen_xor_reg(cb, cb, dest);
1264 tcg_temp_free(zero);
1266 /* Compute signed overflow if required. */
1268 if (is_tsv || cond_need_sv(c)) {
1269 sv = do_sub_sv(ctx, dest, in1, in2);
1271 gen_helper_tsv(cpu_env, sv);
1275 /* Compute the condition. We cannot use the special case for borrow. */
1277 cond = do_sub_cond(cf, dest, in1, in2, sv);
1279 cond = do_cond(cf, dest, cb_msb, sv);
1282 /* Emit any conditional trap before any writeback. */
1285 tmp = tcg_temp_new();
1286 tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1287 gen_helper_tcond(cpu_env, tmp);
1291 /* Write back the result. */
1292 save_or_nullify(ctx, cpu_psw_cb, cb);
1293 save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1294 save_gpr(ctx, rt, dest);
1295 tcg_temp_free(dest);
1297 /* Install the new nullification. */
1298 cond_free(&ctx->null_cond);
1299 ctx->null_cond = cond;
1302 static bool do_sub_reg(DisasContext *ctx, arg_rrr_cf *a,
1303 bool is_tsv, bool is_b, bool is_tc)
1305 TCGv_reg tcg_r1, tcg_r2;
1310 tcg_r1 = load_gpr(ctx, a->r1);
1311 tcg_r2 = load_gpr(ctx, a->r2);
1312 do_sub(ctx, a->t, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, a->cf);
1313 return nullify_end(ctx);
1316 static bool do_sub_imm(DisasContext *ctx, arg_rri_cf *a, bool is_tsv)
1318 TCGv_reg tcg_im, tcg_r2;
1323 tcg_im = load_const(ctx, a->i);
1324 tcg_r2 = load_gpr(ctx, a->r);
1325 do_sub(ctx, a->t, tcg_im, tcg_r2, is_tsv, 0, 0, a->cf);
1326 return nullify_end(ctx);
1329 static void do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1330 TCGv_reg in2, unsigned cf)
1335 dest = tcg_temp_new();
1336 tcg_gen_sub_reg(dest, in1, in2);
1338 /* Compute signed overflow if required. */
1340 if (cond_need_sv(cf >> 1)) {
1341 sv = do_sub_sv(ctx, dest, in1, in2);
1344 /* Form the condition for the compare. */
1345 cond = do_sub_cond(cf, dest, in1, in2, sv);
1348 tcg_gen_movi_reg(dest, 0);
1349 save_gpr(ctx, rt, dest);
1350 tcg_temp_free(dest);
1352 /* Install the new nullification. */
1353 cond_free(&ctx->null_cond);
1354 ctx->null_cond = cond;
1357 static void do_log(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1358 TCGv_reg in2, unsigned cf,
1359 void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1361 TCGv_reg dest = dest_gpr(ctx, rt);
1363 /* Perform the operation, and writeback. */
1365 save_gpr(ctx, rt, dest);
1367 /* Install the new nullification. */
1368 cond_free(&ctx->null_cond);
1370 ctx->null_cond = do_log_cond(cf, dest);
1374 static bool do_log_reg(DisasContext *ctx, arg_rrr_cf *a,
1375 void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1377 TCGv_reg tcg_r1, tcg_r2;
1382 tcg_r1 = load_gpr(ctx, a->r1);
1383 tcg_r2 = load_gpr(ctx, a->r2);
1384 do_log(ctx, a->t, tcg_r1, tcg_r2, a->cf, fn);
1385 return nullify_end(ctx);
1388 static void do_unit(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1389 TCGv_reg in2, unsigned cf, bool is_tc,
1390 void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1396 dest = dest_gpr(ctx, rt);
1398 save_gpr(ctx, rt, dest);
1399 cond_free(&ctx->null_cond);
1401 dest = tcg_temp_new();
1404 cond = do_unit_cond(cf, dest, in1, in2);
1407 TCGv_reg tmp = tcg_temp_new();
1409 tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1410 gen_helper_tcond(cpu_env, tmp);
1413 save_gpr(ctx, rt, dest);
1415 cond_free(&ctx->null_cond);
1416 ctx->null_cond = cond;
1420 #ifndef CONFIG_USER_ONLY
1421 /* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
1422 from the top 2 bits of the base register. There are a few system
1423 instructions that have a 3-bit space specifier, for which SR0 is
1424 not special. To handle this, pass ~SP. */
1425 static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_reg base)
1435 spc = get_temp_tl(ctx);
1436 load_spr(ctx, spc, sp);
1439 if (ctx->tb_flags & TB_FLAG_SR_SAME) {
1443 ptr = tcg_temp_new_ptr();
1444 tmp = tcg_temp_new();
1445 spc = get_temp_tl(ctx);
1447 tcg_gen_shri_reg(tmp, base, TARGET_REGISTER_BITS - 5);
1448 tcg_gen_andi_reg(tmp, tmp, 030);
1449 tcg_gen_trunc_reg_ptr(ptr, tmp);
1452 tcg_gen_add_ptr(ptr, ptr, cpu_env);
1453 tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
1454 tcg_temp_free_ptr(ptr);
1460 static void form_gva(DisasContext *ctx, TCGv_tl *pgva, TCGv_reg *pofs,
1461 unsigned rb, unsigned rx, int scale, target_sreg disp,
1462 unsigned sp, int modify, bool is_phys)
1464 TCGv_reg base = load_gpr(ctx, rb);
1467 /* Note that RX is mutually exclusive with DISP. */
1469 ofs = get_temp(ctx);
1470 tcg_gen_shli_reg(ofs, cpu_gr[rx], scale);
1471 tcg_gen_add_reg(ofs, ofs, base);
1472 } else if (disp || modify) {
1473 ofs = get_temp(ctx);
1474 tcg_gen_addi_reg(ofs, base, disp);
1480 #ifdef CONFIG_USER_ONLY
1481 *pgva = (modify <= 0 ? ofs : base);
1483 TCGv_tl addr = get_temp_tl(ctx);
1484 tcg_gen_extu_reg_tl(addr, modify <= 0 ? ofs : base);
1485 if (ctx->tb_flags & PSW_W) {
1486 tcg_gen_andi_tl(addr, addr, 0x3fffffffffffffffull);
1489 tcg_gen_or_tl(addr, addr, space_select(ctx, sp, base));
1495 /* Emit a memory load. The modify parameter should be
1496 * < 0 for pre-modify,
1497 * > 0 for post-modify,
1498 * = 0 for no base register update.
1500 static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1501 unsigned rx, int scale, target_sreg disp,
1502 unsigned sp, int modify, TCGMemOp mop)
1507 /* Caller uses nullify_over/nullify_end. */
1508 assert(ctx->null_cond.c == TCG_COND_NEVER);
1510 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1511 ctx->mmu_idx == MMU_PHYS_IDX);
1512 tcg_gen_qemu_ld_reg(dest, addr, ctx->mmu_idx, mop);
1514 save_gpr(ctx, rb, ofs);
1518 static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1519 unsigned rx, int scale, target_sreg disp,
1520 unsigned sp, int modify, TCGMemOp mop)
1525 /* Caller uses nullify_over/nullify_end. */
1526 assert(ctx->null_cond.c == TCG_COND_NEVER);
1528 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1529 ctx->mmu_idx == MMU_PHYS_IDX);
1530 tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop);
1532 save_gpr(ctx, rb, ofs);
1536 static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1537 unsigned rx, int scale, target_sreg disp,
1538 unsigned sp, int modify, TCGMemOp mop)
1543 /* Caller uses nullify_over/nullify_end. */
1544 assert(ctx->null_cond.c == TCG_COND_NEVER);
1546 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1547 ctx->mmu_idx == MMU_PHYS_IDX);
1548 tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop);
1550 save_gpr(ctx, rb, ofs);
1554 static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1555 unsigned rx, int scale, target_sreg disp,
1556 unsigned sp, int modify, TCGMemOp mop)
1561 /* Caller uses nullify_over/nullify_end. */
1562 assert(ctx->null_cond.c == TCG_COND_NEVER);
1564 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1565 ctx->mmu_idx == MMU_PHYS_IDX);
1566 tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop);
1568 save_gpr(ctx, rb, ofs);
1572 #if TARGET_REGISTER_BITS == 64
1573 #define do_load_reg do_load_64
1574 #define do_store_reg do_store_64
1576 #define do_load_reg do_load_32
1577 #define do_store_reg do_store_32
1580 static bool do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1581 unsigned rx, int scale, target_sreg disp,
1582 unsigned sp, int modify, TCGMemOp mop)
1589 /* No base register update. */
1590 dest = dest_gpr(ctx, rt);
1592 /* Make sure if RT == RB, we see the result of the load. */
1593 dest = get_temp(ctx);
1595 do_load_reg(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
1596 save_gpr(ctx, rt, dest);
1598 return nullify_end(ctx);
1601 static bool do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1602 unsigned rx, int scale, target_sreg disp,
1603 unsigned sp, int modify)
1609 tmp = tcg_temp_new_i32();
1610 do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1611 save_frw_i32(rt, tmp);
1612 tcg_temp_free_i32(tmp);
1615 gen_helper_loaded_fr0(cpu_env);
1618 return nullify_end(ctx);
1621 static bool trans_fldw(DisasContext *ctx, arg_ldst *a)
1623 return do_floadw(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1624 a->disp, a->sp, a->m);
1627 static bool do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1628 unsigned rx, int scale, target_sreg disp,
1629 unsigned sp, int modify)
1635 tmp = tcg_temp_new_i64();
1636 do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEQ);
1638 tcg_temp_free_i64(tmp);
1641 gen_helper_loaded_fr0(cpu_env);
1644 return nullify_end(ctx);
1647 static bool trans_fldd(DisasContext *ctx, arg_ldst *a)
1649 return do_floadd(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1650 a->disp, a->sp, a->m);
1653 static bool do_store(DisasContext *ctx, unsigned rt, unsigned rb,
1654 target_sreg disp, unsigned sp,
1655 int modify, TCGMemOp mop)
1658 do_store_reg(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
1659 return nullify_end(ctx);
1662 static bool do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1663 unsigned rx, int scale, target_sreg disp,
1664 unsigned sp, int modify)
1670 tmp = load_frw_i32(rt);
1671 do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1672 tcg_temp_free_i32(tmp);
1674 return nullify_end(ctx);
1677 static bool trans_fstw(DisasContext *ctx, arg_ldst *a)
1679 return do_fstorew(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1680 a->disp, a->sp, a->m);
1683 static bool do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1684 unsigned rx, int scale, target_sreg disp,
1685 unsigned sp, int modify)
1692 do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEQ);
1693 tcg_temp_free_i64(tmp);
1695 return nullify_end(ctx);
1698 static bool trans_fstd(DisasContext *ctx, arg_ldst *a)
1700 return do_fstored(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1701 a->disp, a->sp, a->m);
1704 static bool do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
1705 void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1710 tmp = load_frw0_i32(ra);
1712 func(tmp, cpu_env, tmp);
1714 save_frw_i32(rt, tmp);
1715 tcg_temp_free_i32(tmp);
1716 return nullify_end(ctx);
1719 static bool do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
1720 void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1727 dst = tcg_temp_new_i32();
1729 func(dst, cpu_env, src);
1731 tcg_temp_free_i64(src);
1732 save_frw_i32(rt, dst);
1733 tcg_temp_free_i32(dst);
1734 return nullify_end(ctx);
1737 static bool do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
1738 void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1743 tmp = load_frd0(ra);
1745 func(tmp, cpu_env, tmp);
1748 tcg_temp_free_i64(tmp);
1749 return nullify_end(ctx);
1752 static bool do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
1753 void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1759 src = load_frw0_i32(ra);
1760 dst = tcg_temp_new_i64();
1762 func(dst, cpu_env, src);
1764 tcg_temp_free_i32(src);
1766 tcg_temp_free_i64(dst);
1767 return nullify_end(ctx);
1770 static bool do_fop_weww(DisasContext *ctx, unsigned rt,
1771 unsigned ra, unsigned rb,
1772 void (*func)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32))
1777 a = load_frw0_i32(ra);
1778 b = load_frw0_i32(rb);
1780 func(a, cpu_env, a, b);
1782 tcg_temp_free_i32(b);
1783 save_frw_i32(rt, a);
1784 tcg_temp_free_i32(a);
1785 return nullify_end(ctx);
1788 static bool do_fop_dedd(DisasContext *ctx, unsigned rt,
1789 unsigned ra, unsigned rb,
1790 void (*func)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1798 func(a, cpu_env, a, b);
1800 tcg_temp_free_i64(b);
1802 tcg_temp_free_i64(a);
1803 return nullify_end(ctx);
1806 /* Emit an unconditional branch to a direct target, which may or may not
1807 have already had nullification handled. */
1808 static bool do_dbranch(DisasContext *ctx, target_ureg dest,
1809 unsigned link, bool is_n)
1811 if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
1813 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1817 ctx->null_cond.c = TCG_COND_ALWAYS;
1823 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1826 if (is_n && use_nullify_skip(ctx)) {
1827 nullify_set(ctx, 0);
1828 gen_goto_tb(ctx, 0, dest, dest + 4);
1830 nullify_set(ctx, is_n);
1831 gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
1836 nullify_set(ctx, 0);
1837 gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
1838 ctx->base.is_jmp = DISAS_NORETURN;
1843 /* Emit a conditional branch to a direct target. If the branch itself
1844 is nullified, we should have already used nullify_over. */
1845 static bool do_cbranch(DisasContext *ctx, target_sreg disp, bool is_n,
1848 target_ureg dest = iaoq_dest(ctx, disp);
1849 TCGLabel *taken = NULL;
1850 TCGCond c = cond->c;
1853 assert(ctx->null_cond.c == TCG_COND_NEVER);
1855 /* Handle TRUE and NEVER as direct branches. */
1856 if (c == TCG_COND_ALWAYS) {
1857 return do_dbranch(ctx, dest, 0, is_n && disp >= 0);
1859 if (c == TCG_COND_NEVER) {
1860 return do_dbranch(ctx, ctx->iaoq_n, 0, is_n && disp < 0);
1863 taken = gen_new_label();
1865 tcg_gen_brcond_reg(c, cond->a0, cond->a1, taken);
1868 /* Not taken: Condition not satisfied; nullify on backward branches. */
1869 n = is_n && disp < 0;
1870 if (n && use_nullify_skip(ctx)) {
1871 nullify_set(ctx, 0);
1872 gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
1874 if (!n && ctx->null_lab) {
1875 gen_set_label(ctx->null_lab);
1876 ctx->null_lab = NULL;
1878 nullify_set(ctx, n);
1879 if (ctx->iaoq_n == -1) {
1880 /* The temporary iaoq_n_var died at the branch above.
1881 Regenerate it here instead of saving it. */
1882 tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
1884 gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
1887 gen_set_label(taken);
1889 /* Taken: Condition satisfied; nullify on forward branches. */
1890 n = is_n && disp >= 0;
1891 if (n && use_nullify_skip(ctx)) {
1892 nullify_set(ctx, 0);
1893 gen_goto_tb(ctx, 1, dest, dest + 4);
1895 nullify_set(ctx, n);
1896 gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
1899 /* Not taken: the branch itself was nullified. */
1900 if (ctx->null_lab) {
1901 gen_set_label(ctx->null_lab);
1902 ctx->null_lab = NULL;
1903 ctx->base.is_jmp = DISAS_IAQ_N_STALE;
1905 ctx->base.is_jmp = DISAS_NORETURN;
1910 /* Emit an unconditional branch to an indirect target. This handles
1911 nullification of the branch itself. */
1912 static bool do_ibranch(DisasContext *ctx, TCGv_reg dest,
1913 unsigned link, bool is_n)
1915 TCGv_reg a0, a1, next, tmp;
1918 assert(ctx->null_lab == NULL);
1920 if (ctx->null_cond.c == TCG_COND_NEVER) {
1922 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1924 next = get_temp(ctx);
1925 tcg_gen_mov_reg(next, dest);
1927 if (use_nullify_skip(ctx)) {
1928 tcg_gen_mov_reg(cpu_iaoq_f, next);
1929 tcg_gen_addi_reg(cpu_iaoq_b, next, 4);
1930 nullify_set(ctx, 0);
1931 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
1934 ctx->null_cond.c = TCG_COND_ALWAYS;
1937 ctx->iaoq_n_var = next;
1938 } else if (is_n && use_nullify_skip(ctx)) {
1939 /* The (conditional) branch, B, nullifies the next insn, N,
1940 and we're allowed to skip execution N (no single-step or
1941 tracepoint in effect). Since the goto_ptr that we must use
1942 for the indirect branch consumes no special resources, we
1943 can (conditionally) skip B and continue execution. */
1944 /* The use_nullify_skip test implies we have a known control path. */
1945 tcg_debug_assert(ctx->iaoq_b != -1);
1946 tcg_debug_assert(ctx->iaoq_n != -1);
1948 /* We do have to handle the non-local temporary, DEST, before
1949 branching. Since IOAQ_F is not really live at this point, we
1950 can simply store DEST optimistically. Similarly with IAOQ_B. */
1951 tcg_gen_mov_reg(cpu_iaoq_f, dest);
1952 tcg_gen_addi_reg(cpu_iaoq_b, dest, 4);
1956 tcg_gen_movi_reg(cpu_gr[link], ctx->iaoq_n);
1958 tcg_gen_lookup_and_goto_ptr();
1959 return nullify_end(ctx);
1961 cond_prep(&ctx->null_cond);
1962 c = ctx->null_cond.c;
1963 a0 = ctx->null_cond.a0;
1964 a1 = ctx->null_cond.a1;
1966 tmp = tcg_temp_new();
1967 next = get_temp(ctx);
1969 copy_iaoq_entry(tmp, ctx->iaoq_n, ctx->iaoq_n_var);
1970 tcg_gen_movcond_reg(c, next, a0, a1, tmp, dest);
1972 ctx->iaoq_n_var = next;
1975 tcg_gen_movcond_reg(c, cpu_gr[link], a0, a1, cpu_gr[link], tmp);
1979 /* The branch nullifies the next insn, which means the state of N
1980 after the branch is the inverse of the state of N that applied
1982 tcg_gen_setcond_reg(tcg_invert_cond(c), cpu_psw_n, a0, a1);
1983 cond_free(&ctx->null_cond);
1984 ctx->null_cond = cond_make_n();
1985 ctx->psw_n_nonzero = true;
1987 cond_free(&ctx->null_cond);
1994 * if (IAOQ_Front{30..31} < GR[b]{30..31})
1995 * IAOQ_Next{30..31} ← GR[b]{30..31};
1997 * IAOQ_Next{30..31} ← IAOQ_Front{30..31};
1998 * which keeps the privilege level from being increased.
2000 static TCGv_reg do_ibranch_priv(DisasContext *ctx, TCGv_reg offset)
2003 switch (ctx->privilege) {
2005 /* Privilege 0 is maximum and is allowed to decrease. */
2008 /* Privilege 3 is minimum and is never allowed to increase. */
2009 dest = get_temp(ctx);
2010 tcg_gen_ori_reg(dest, offset, 3);
2013 dest = get_temp(ctx);
2014 tcg_gen_andi_reg(dest, offset, -4);
2015 tcg_gen_ori_reg(dest, dest, ctx->privilege);
2016 tcg_gen_movcond_reg(TCG_COND_GTU, dest, dest, offset, dest, offset);
2022 #ifdef CONFIG_USER_ONLY
2023 /* On Linux, page zero is normally marked execute only + gateway.
2024 Therefore normal read or write is supposed to fail, but specific
2025 offsets have kernel code mapped to raise permissions to implement
2026 system calls. Handling this via an explicit check here, rather
2027 in than the "be disp(sr2,r0)" instruction that probably sent us
2028 here, is the easiest way to handle the branch delay slot on the
2029 aforementioned BE. */
2030 static void do_page_zero(DisasContext *ctx)
2032 /* If by some means we get here with PSW[N]=1, that implies that
2033 the B,GATE instruction would be skipped, and we'd fault on the
2034 next insn within the privilaged page. */
2035 switch (ctx->null_cond.c) {
2036 case TCG_COND_NEVER:
2038 case TCG_COND_ALWAYS:
2039 tcg_gen_movi_reg(cpu_psw_n, 0);
2042 /* Since this is always the first (and only) insn within the
2043 TB, we should know the state of PSW[N] from TB->FLAGS. */
2044 g_assert_not_reached();
2047 /* Check that we didn't arrive here via some means that allowed
2048 non-sequential instruction execution. Normally the PSW[B] bit
2049 detects this by disallowing the B,GATE instruction to execute
2050 under such conditions. */
2051 if (ctx->iaoq_b != ctx->iaoq_f + 4) {
2055 switch (ctx->iaoq_f & -4) {
2056 case 0x00: /* Null pointer call */
2057 gen_excp_1(EXCP_IMP);
2058 ctx->base.is_jmp = DISAS_NORETURN;
2061 case 0xb0: /* LWS */
2062 gen_excp_1(EXCP_SYSCALL_LWS);
2063 ctx->base.is_jmp = DISAS_NORETURN;
2066 case 0xe0: /* SET_THREAD_POINTER */
2067 tcg_gen_st_reg(cpu_gr[26], cpu_env, offsetof(CPUHPPAState, cr[27]));
2068 tcg_gen_ori_reg(cpu_iaoq_f, cpu_gr[31], 3);
2069 tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
2070 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
2073 case 0x100: /* SYSCALL */
2074 gen_excp_1(EXCP_SYSCALL);
2075 ctx->base.is_jmp = DISAS_NORETURN;
2080 gen_excp_1(EXCP_ILL);
2081 ctx->base.is_jmp = DISAS_NORETURN;
2087 static bool trans_nop(DisasContext *ctx, arg_nop *a)
2089 cond_free(&ctx->null_cond);
2093 static bool trans_break(DisasContext *ctx, arg_break *a)
2095 return gen_excp_iir(ctx, EXCP_BREAK);
2098 static bool trans_sync(DisasContext *ctx, arg_sync *a)
2100 /* No point in nullifying the memory barrier. */
2101 tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
2103 cond_free(&ctx->null_cond);
2107 static bool trans_mfia(DisasContext *ctx, arg_mfia *a)
2110 TCGv_reg tmp = dest_gpr(ctx, rt);
2111 tcg_gen_movi_reg(tmp, ctx->iaoq_f);
2112 save_gpr(ctx, rt, tmp);
2114 cond_free(&ctx->null_cond);
2118 static bool trans_mfsp(DisasContext *ctx, arg_mfsp *a)
2121 unsigned rs = a->sp;
2122 TCGv_i64 t0 = tcg_temp_new_i64();
2123 TCGv_reg t1 = tcg_temp_new();
2125 load_spr(ctx, t0, rs);
2126 tcg_gen_shri_i64(t0, t0, 32);
2127 tcg_gen_trunc_i64_reg(t1, t0);
2129 save_gpr(ctx, rt, t1);
2131 tcg_temp_free_i64(t0);
2133 cond_free(&ctx->null_cond);
2137 static bool trans_mfctl(DisasContext *ctx, arg_mfctl *a)
2140 unsigned ctl = a->r;
2145 #ifdef TARGET_HPPA64
2147 /* MFSAR without ,W masks low 5 bits. */
2148 tmp = dest_gpr(ctx, rt);
2149 tcg_gen_andi_reg(tmp, cpu_sar, 31);
2150 save_gpr(ctx, rt, tmp);
2154 save_gpr(ctx, rt, cpu_sar);
2156 case CR_IT: /* Interval Timer */
2157 /* FIXME: Respect PSW_S bit. */
2159 tmp = dest_gpr(ctx, rt);
2160 if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
2162 gen_helper_read_interval_timer(tmp);
2164 ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2166 gen_helper_read_interval_timer(tmp);
2168 save_gpr(ctx, rt, tmp);
2169 return nullify_end(ctx);
2174 /* All other control registers are privileged. */
2175 CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2179 tmp = get_temp(ctx);
2180 tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2181 save_gpr(ctx, rt, tmp);
2184 cond_free(&ctx->null_cond);
2188 static bool trans_mtsp(DisasContext *ctx, arg_mtsp *a)
2191 unsigned rs = a->sp;
2195 CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2199 t64 = tcg_temp_new_i64();
2200 tcg_gen_extu_reg_i64(t64, load_gpr(ctx, rr));
2201 tcg_gen_shli_i64(t64, t64, 32);
2204 tcg_gen_st_i64(t64, cpu_env, offsetof(CPUHPPAState, sr[rs]));
2205 ctx->tb_flags &= ~TB_FLAG_SR_SAME;
2207 tcg_gen_mov_i64(cpu_sr[rs], t64);
2209 tcg_temp_free_i64(t64);
2211 return nullify_end(ctx);
2214 static bool trans_mtctl(DisasContext *ctx, arg_mtctl *a)
2216 unsigned ctl = a->t;
2217 TCGv_reg reg = load_gpr(ctx, a->r);
2220 if (ctl == CR_SAR) {
2221 tmp = tcg_temp_new();
2222 tcg_gen_andi_reg(tmp, reg, TARGET_REGISTER_BITS - 1);
2223 save_or_nullify(ctx, cpu_sar, tmp);
2226 cond_free(&ctx->null_cond);
2230 /* All other control registers are privileged or read-only. */
2231 CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2233 #ifndef CONFIG_USER_ONLY
2237 gen_helper_write_interval_timer(cpu_env, reg);
2240 gen_helper_write_eirr(cpu_env, reg);
2243 gen_helper_write_eiem(cpu_env, reg);
2244 ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2249 /* FIXME: Respect PSW_Q bit */
2250 /* The write advances the queue and stores to the back element. */
2251 tmp = get_temp(ctx);
2252 tcg_gen_ld_reg(tmp, cpu_env,
2253 offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
2254 tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2255 tcg_gen_st_reg(reg, cpu_env,
2256 offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
2263 tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2264 #ifndef CONFIG_USER_ONLY
2265 gen_helper_change_prot_id(cpu_env);
2270 tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2273 return nullify_end(ctx);
2277 static bool trans_mtsarcm(DisasContext *ctx, arg_mtsarcm *a)
2279 TCGv_reg tmp = tcg_temp_new();
2281 tcg_gen_not_reg(tmp, load_gpr(ctx, a->r));
2282 tcg_gen_andi_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
2283 save_or_nullify(ctx, cpu_sar, tmp);
2286 cond_free(&ctx->null_cond);
2290 static bool trans_ldsid(DisasContext *ctx, arg_ldsid *a)
2292 TCGv_reg dest = dest_gpr(ctx, a->t);
2294 #ifdef CONFIG_USER_ONLY
2295 /* We don't implement space registers in user mode. */
2296 tcg_gen_movi_reg(dest, 0);
2298 TCGv_i64 t0 = tcg_temp_new_i64();
2300 tcg_gen_mov_i64(t0, space_select(ctx, a->sp, load_gpr(ctx, a->b)));
2301 tcg_gen_shri_i64(t0, t0, 32);
2302 tcg_gen_trunc_i64_reg(dest, t0);
2304 tcg_temp_free_i64(t0);
2306 save_gpr(ctx, a->t, dest);
2308 cond_free(&ctx->null_cond);
2312 static bool trans_rsm(DisasContext *ctx, arg_rsm *a)
2314 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2315 #ifndef CONFIG_USER_ONLY
2320 tmp = get_temp(ctx);
2321 tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2322 tcg_gen_andi_reg(tmp, tmp, ~a->i);
2323 gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2324 save_gpr(ctx, a->t, tmp);
2326 /* Exit the TB to recognize new interrupts, e.g. PSW_M. */
2327 ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2328 return nullify_end(ctx);
2332 static bool trans_ssm(DisasContext *ctx, arg_ssm *a)
2334 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2335 #ifndef CONFIG_USER_ONLY
2340 tmp = get_temp(ctx);
2341 tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2342 tcg_gen_ori_reg(tmp, tmp, a->i);
2343 gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2344 save_gpr(ctx, a->t, tmp);
2346 /* Exit the TB to recognize new interrupts, e.g. PSW_I. */
2347 ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2348 return nullify_end(ctx);
2352 static bool trans_mtsm(DisasContext *ctx, arg_mtsm *a)
2354 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2355 #ifndef CONFIG_USER_ONLY
2359 reg = load_gpr(ctx, a->r);
2360 tmp = get_temp(ctx);
2361 gen_helper_swap_system_mask(tmp, cpu_env, reg);
2363 /* Exit the TB to recognize new interrupts. */
2364 ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
2365 return nullify_end(ctx);
2369 static bool do_rfi(DisasContext *ctx, bool rfi_r)
2371 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2372 #ifndef CONFIG_USER_ONLY
2376 gen_helper_rfi_r(cpu_env);
2378 gen_helper_rfi(cpu_env);
2380 /* Exit the TB to recognize new interrupts. */
2381 if (ctx->base.singlestep_enabled) {
2382 gen_excp_1(EXCP_DEBUG);
2384 tcg_gen_exit_tb(NULL, 0);
2386 ctx->base.is_jmp = DISAS_NORETURN;
2388 return nullify_end(ctx);
2392 static bool trans_rfi(DisasContext *ctx, arg_rfi *a)
2394 return do_rfi(ctx, false);
2397 static bool trans_rfi_r(DisasContext *ctx, arg_rfi_r *a)
2399 return do_rfi(ctx, true);
2402 static bool trans_halt(DisasContext *ctx, arg_halt *a)
2404 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2405 #ifndef CONFIG_USER_ONLY
2407 gen_helper_halt(cpu_env);
2408 ctx->base.is_jmp = DISAS_NORETURN;
2409 return nullify_end(ctx);
2413 static bool trans_reset(DisasContext *ctx, arg_reset *a)
2415 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2416 #ifndef CONFIG_USER_ONLY
2418 gen_helper_reset(cpu_env);
2419 ctx->base.is_jmp = DISAS_NORETURN;
2420 return nullify_end(ctx);
2424 static bool trans_nop_addrx(DisasContext *ctx, arg_ldst *a)
2427 TCGv_reg dest = dest_gpr(ctx, a->b);
2428 TCGv_reg src1 = load_gpr(ctx, a->b);
2429 TCGv_reg src2 = load_gpr(ctx, a->x);
2431 /* The only thing we need to do is the base register modification. */
2432 tcg_gen_add_reg(dest, src1, src2);
2433 save_gpr(ctx, a->b, dest);
2435 cond_free(&ctx->null_cond);
2439 static bool trans_probe(DisasContext *ctx, arg_probe *a)
2442 TCGv_i32 level, want;
2447 dest = dest_gpr(ctx, a->t);
2448 form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2451 level = tcg_const_i32(a->ri);
2453 level = tcg_temp_new_i32();
2454 tcg_gen_trunc_reg_i32(level, load_gpr(ctx, a->ri));
2455 tcg_gen_andi_i32(level, level, 3);
2457 want = tcg_const_i32(a->write ? PAGE_WRITE : PAGE_READ);
2459 gen_helper_probe(dest, cpu_env, addr, level, want);
2461 tcg_temp_free_i32(want);
2462 tcg_temp_free_i32(level);
2464 save_gpr(ctx, a->t, dest);
2465 return nullify_end(ctx);
2468 static bool trans_ixtlbx(DisasContext *ctx, arg_ixtlbx *a)
2470 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2471 #ifndef CONFIG_USER_ONLY
2477 form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2478 reg = load_gpr(ctx, a->r);
2480 gen_helper_itlba(cpu_env, addr, reg);
2482 gen_helper_itlbp(cpu_env, addr, reg);
2485 /* Exit TB for TLB change if mmu is enabled. */
2486 if (ctx->tb_flags & PSW_C) {
2487 ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2489 return nullify_end(ctx);
2493 static bool trans_pxtlbx(DisasContext *ctx, arg_pxtlbx *a)
2495 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2496 #ifndef CONFIG_USER_ONLY
2502 form_gva(ctx, &addr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2504 save_gpr(ctx, a->b, ofs);
2507 gen_helper_ptlbe(cpu_env);
2509 gen_helper_ptlb(cpu_env, addr);
2512 /* Exit TB for TLB change if mmu is enabled. */
2513 if (ctx->tb_flags & PSW_C) {
2514 ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2516 return nullify_end(ctx);
2520 static bool trans_lpa(DisasContext *ctx, arg_ldst *a)
2522 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2523 #ifndef CONFIG_USER_ONLY
2525 TCGv_reg ofs, paddr;
2529 form_gva(ctx, &vaddr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2531 paddr = tcg_temp_new();
2532 gen_helper_lpa(paddr, cpu_env, vaddr);
2534 /* Note that physical address result overrides base modification. */
2536 save_gpr(ctx, a->b, ofs);
2538 save_gpr(ctx, a->t, paddr);
2539 tcg_temp_free(paddr);
2541 return nullify_end(ctx);
2545 static bool trans_lci(DisasContext *ctx, arg_lci *a)
2549 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2551 /* The Coherence Index is an implementation-defined function of the
2552 physical address. Two addresses with the same CI have a coherent
2553 view of the cache. Our implementation is to return 0 for all,
2554 since the entire address space is coherent. */
2555 ci = tcg_const_reg(0);
2556 save_gpr(ctx, a->t, ci);
2559 cond_free(&ctx->null_cond);
2563 static bool trans_add(DisasContext *ctx, arg_rrr_cf_sh *a)
2565 return do_add_reg(ctx, a, false, false, false, false);
2568 static bool trans_add_l(DisasContext *ctx, arg_rrr_cf_sh *a)
2570 return do_add_reg(ctx, a, true, false, false, false);
2573 static bool trans_add_tsv(DisasContext *ctx, arg_rrr_cf_sh *a)
2575 return do_add_reg(ctx, a, false, true, false, false);
2578 static bool trans_add_c(DisasContext *ctx, arg_rrr_cf_sh *a)
2580 return do_add_reg(ctx, a, false, false, false, true);
2583 static bool trans_add_c_tsv(DisasContext *ctx, arg_rrr_cf_sh *a)
2585 return do_add_reg(ctx, a, false, true, false, true);
2588 static bool trans_sub(DisasContext *ctx, arg_rrr_cf *a)
2590 return do_sub_reg(ctx, a, false, false, false);
2593 static bool trans_sub_tsv(DisasContext *ctx, arg_rrr_cf *a)
2595 return do_sub_reg(ctx, a, true, false, false);
2598 static bool trans_sub_tc(DisasContext *ctx, arg_rrr_cf *a)
2600 return do_sub_reg(ctx, a, false, false, true);
2603 static bool trans_sub_tsv_tc(DisasContext *ctx, arg_rrr_cf *a)
2605 return do_sub_reg(ctx, a, true, false, true);
2608 static bool trans_sub_b(DisasContext *ctx, arg_rrr_cf *a)
2610 return do_sub_reg(ctx, a, false, true, false);
2613 static bool trans_sub_b_tsv(DisasContext *ctx, arg_rrr_cf *a)
2615 return do_sub_reg(ctx, a, true, true, false);
2618 static bool trans_andcm(DisasContext *ctx, arg_rrr_cf *a)
2620 return do_log_reg(ctx, a, tcg_gen_andc_reg);
2623 static bool trans_and(DisasContext *ctx, arg_rrr_cf *a)
2625 return do_log_reg(ctx, a, tcg_gen_and_reg);
2628 static bool trans_or(DisasContext *ctx, arg_rrr_cf *a)
2631 unsigned r2 = a->r2;
2632 unsigned r1 = a->r1;
2635 if (rt == 0) { /* NOP */
2636 cond_free(&ctx->null_cond);
2639 if (r2 == 0) { /* COPY */
2641 TCGv_reg dest = dest_gpr(ctx, rt);
2642 tcg_gen_movi_reg(dest, 0);
2643 save_gpr(ctx, rt, dest);
2645 save_gpr(ctx, rt, cpu_gr[r1]);
2647 cond_free(&ctx->null_cond);
2650 #ifndef CONFIG_USER_ONLY
2651 /* These are QEMU extensions and are nops in the real architecture:
2653 * or %r10,%r10,%r10 -- idle loop; wait for interrupt
2654 * or %r31,%r31,%r31 -- death loop; offline cpu
2655 * currently implemented as idle.
2657 if ((rt == 10 || rt == 31) && r1 == rt && r2 == rt) { /* PAUSE */
2660 /* No need to check for supervisor, as userland can only pause
2661 until the next timer interrupt. */
2664 /* Advance the instruction queue. */
2665 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
2666 copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
2667 nullify_set(ctx, 0);
2669 /* Tell the qemu main loop to halt until this cpu has work. */
2670 tmp = tcg_const_i32(1);
2671 tcg_gen_st_i32(tmp, cpu_env, -offsetof(HPPACPU, env) +
2672 offsetof(CPUState, halted));
2673 tcg_temp_free_i32(tmp);
2674 gen_excp_1(EXCP_HALTED);
2675 ctx->base.is_jmp = DISAS_NORETURN;
2677 return nullify_end(ctx);
2681 return do_log_reg(ctx, a, tcg_gen_or_reg);
2684 static bool trans_xor(DisasContext *ctx, arg_rrr_cf *a)
2686 return do_log_reg(ctx, a, tcg_gen_xor_reg);
2689 static bool trans_cmpclr(DisasContext *ctx, arg_rrr_cf *a)
2691 TCGv_reg tcg_r1, tcg_r2;
2696 tcg_r1 = load_gpr(ctx, a->r1);
2697 tcg_r2 = load_gpr(ctx, a->r2);
2698 do_cmpclr(ctx, a->t, tcg_r1, tcg_r2, a->cf);
2699 return nullify_end(ctx);
2702 static bool trans_uxor(DisasContext *ctx, arg_rrr_cf *a)
2704 TCGv_reg tcg_r1, tcg_r2;
2709 tcg_r1 = load_gpr(ctx, a->r1);
2710 tcg_r2 = load_gpr(ctx, a->r2);
2711 do_unit(ctx, a->t, tcg_r1, tcg_r2, a->cf, false, tcg_gen_xor_reg);
2712 return nullify_end(ctx);
2715 static bool do_uaddcm(DisasContext *ctx, arg_rrr_cf *a, bool is_tc)
2717 TCGv_reg tcg_r1, tcg_r2, tmp;
2722 tcg_r1 = load_gpr(ctx, a->r1);
2723 tcg_r2 = load_gpr(ctx, a->r2);
2724 tmp = get_temp(ctx);
2725 tcg_gen_not_reg(tmp, tcg_r2);
2726 do_unit(ctx, a->t, tcg_r1, tmp, a->cf, is_tc, tcg_gen_add_reg);
2727 return nullify_end(ctx);
2730 static bool trans_uaddcm(DisasContext *ctx, arg_rrr_cf *a)
2732 return do_uaddcm(ctx, a, false);
2735 static bool trans_uaddcm_tc(DisasContext *ctx, arg_rrr_cf *a)
2737 return do_uaddcm(ctx, a, true);
2740 static bool do_dcor(DisasContext *ctx, arg_rr_cf *a, bool is_i)
2746 tmp = get_temp(ctx);
2747 tcg_gen_shri_reg(tmp, cpu_psw_cb, 3);
2749 tcg_gen_not_reg(tmp, tmp);
2751 tcg_gen_andi_reg(tmp, tmp, 0x11111111);
2752 tcg_gen_muli_reg(tmp, tmp, 6);
2753 do_unit(ctx, a->t, load_gpr(ctx, a->r), tmp, a->cf, false,
2754 is_i ? tcg_gen_add_reg : tcg_gen_sub_reg);
2755 return nullify_end(ctx);
2758 static bool trans_dcor(DisasContext *ctx, arg_rr_cf *a)
2760 return do_dcor(ctx, a, false);
2763 static bool trans_dcor_i(DisasContext *ctx, arg_rr_cf *a)
2765 return do_dcor(ctx, a, true);
2768 static bool trans_ds(DisasContext *ctx, arg_rrr_cf *a)
2770 TCGv_reg dest, add1, add2, addc, zero, in1, in2;
2774 in1 = load_gpr(ctx, a->r1);
2775 in2 = load_gpr(ctx, a->r2);
2777 add1 = tcg_temp_new();
2778 add2 = tcg_temp_new();
2779 addc = tcg_temp_new();
2780 dest = tcg_temp_new();
2781 zero = tcg_const_reg(0);
2783 /* Form R1 << 1 | PSW[CB]{8}. */
2784 tcg_gen_add_reg(add1, in1, in1);
2785 tcg_gen_add_reg(add1, add1, cpu_psw_cb_msb);
2787 /* Add or subtract R2, depending on PSW[V]. Proper computation of
2788 carry{8} requires that we subtract via + ~R2 + 1, as described in
2789 the manual. By extracting and masking V, we can produce the
2790 proper inputs to the addition without movcond. */
2791 tcg_gen_sari_reg(addc, cpu_psw_v, TARGET_REGISTER_BITS - 1);
2792 tcg_gen_xor_reg(add2, in2, addc);
2793 tcg_gen_andi_reg(addc, addc, 1);
2794 /* ??? This is only correct for 32-bit. */
2795 tcg_gen_add2_i32(dest, cpu_psw_cb_msb, add1, zero, add2, zero);
2796 tcg_gen_add2_i32(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb, addc, zero);
2798 tcg_temp_free(addc);
2799 tcg_temp_free(zero);
2801 /* Write back the result register. */
2802 save_gpr(ctx, a->t, dest);
2804 /* Write back PSW[CB]. */
2805 tcg_gen_xor_reg(cpu_psw_cb, add1, add2);
2806 tcg_gen_xor_reg(cpu_psw_cb, cpu_psw_cb, dest);
2808 /* Write back PSW[V] for the division step. */
2809 tcg_gen_neg_reg(cpu_psw_v, cpu_psw_cb_msb);
2810 tcg_gen_xor_reg(cpu_psw_v, cpu_psw_v, in2);
2812 /* Install the new nullification. */
2815 if (cond_need_sv(a->cf >> 1)) {
2816 /* ??? The lshift is supposed to contribute to overflow. */
2817 sv = do_add_sv(ctx, dest, add1, add2);
2819 ctx->null_cond = do_cond(a->cf, dest, cpu_psw_cb_msb, sv);
2822 tcg_temp_free(add1);
2823 tcg_temp_free(add2);
2824 tcg_temp_free(dest);
2826 return nullify_end(ctx);
2829 static bool trans_addi(DisasContext *ctx, arg_rri_cf *a)
2831 return do_add_imm(ctx, a, false, false);
2834 static bool trans_addi_tsv(DisasContext *ctx, arg_rri_cf *a)
2836 return do_add_imm(ctx, a, true, false);
2839 static bool trans_addi_tc(DisasContext *ctx, arg_rri_cf *a)
2841 return do_add_imm(ctx, a, false, true);
2844 static bool trans_addi_tc_tsv(DisasContext *ctx, arg_rri_cf *a)
2846 return do_add_imm(ctx, a, true, true);
2849 static bool trans_subi(DisasContext *ctx, arg_rri_cf *a)
2851 return do_sub_imm(ctx, a, false);
2854 static bool trans_subi_tsv(DisasContext *ctx, arg_rri_cf *a)
2856 return do_sub_imm(ctx, a, true);
2859 static bool trans_cmpiclr(DisasContext *ctx, arg_rri_cf *a)
2861 TCGv_reg tcg_im, tcg_r2;
2867 tcg_im = load_const(ctx, a->i);
2868 tcg_r2 = load_gpr(ctx, a->r);
2869 do_cmpclr(ctx, a->t, tcg_im, tcg_r2, a->cf);
2871 return nullify_end(ctx);
2874 static bool trans_ld(DisasContext *ctx, arg_ldst *a)
2876 return do_load(ctx, a->t, a->b, a->x, a->scale ? a->size : 0,
2877 a->disp, a->sp, a->m, a->size | MO_TE);
2880 static bool trans_st(DisasContext *ctx, arg_ldst *a)
2882 assert(a->x == 0 && a->scale == 0);
2883 return do_store(ctx, a->t, a->b, a->disp, a->sp, a->m, a->size | MO_TE);
2886 static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
2888 TCGMemOp mop = MO_TEUL | MO_ALIGN_16 | a->size;
2889 TCGv_reg zero, dest, ofs;
2895 /* Base register modification. Make sure if RT == RB,
2896 we see the result of the load. */
2897 dest = get_temp(ctx);
2899 dest = dest_gpr(ctx, a->t);
2902 form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? a->size : 0,
2903 a->disp, a->sp, a->m, ctx->mmu_idx == MMU_PHYS_IDX);
2904 zero = tcg_const_reg(0);
2905 tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
2907 save_gpr(ctx, a->b, ofs);
2909 save_gpr(ctx, a->t, dest);
2911 return nullify_end(ctx);
2914 static bool trans_stby(DisasContext *ctx, arg_stby *a)
2921 form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
2922 ctx->mmu_idx == MMU_PHYS_IDX);
2923 val = load_gpr(ctx, a->r);
2925 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2926 gen_helper_stby_e_parallel(cpu_env, addr, val);
2928 gen_helper_stby_e(cpu_env, addr, val);
2931 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2932 gen_helper_stby_b_parallel(cpu_env, addr, val);
2934 gen_helper_stby_b(cpu_env, addr, val);
2938 tcg_gen_andi_reg(ofs, ofs, ~3);
2939 save_gpr(ctx, a->b, ofs);
2942 return nullify_end(ctx);
2945 static bool trans_lda(DisasContext *ctx, arg_ldst *a)
2947 int hold_mmu_idx = ctx->mmu_idx;
2949 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2950 ctx->mmu_idx = MMU_PHYS_IDX;
2952 ctx->mmu_idx = hold_mmu_idx;
2956 static bool trans_sta(DisasContext *ctx, arg_ldst *a)
2958 int hold_mmu_idx = ctx->mmu_idx;
2960 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2961 ctx->mmu_idx = MMU_PHYS_IDX;
2963 ctx->mmu_idx = hold_mmu_idx;
2967 static bool trans_ldil(DisasContext *ctx, arg_ldil *a)
2969 TCGv_reg tcg_rt = dest_gpr(ctx, a->t);
2971 tcg_gen_movi_reg(tcg_rt, a->i);
2972 save_gpr(ctx, a->t, tcg_rt);
2973 cond_free(&ctx->null_cond);
2977 static bool trans_addil(DisasContext *ctx, arg_addil *a)
2979 TCGv_reg tcg_rt = load_gpr(ctx, a->r);
2980 TCGv_reg tcg_r1 = dest_gpr(ctx, 1);
2982 tcg_gen_addi_reg(tcg_r1, tcg_rt, a->i);
2983 save_gpr(ctx, 1, tcg_r1);
2984 cond_free(&ctx->null_cond);
2988 static bool trans_ldo(DisasContext *ctx, arg_ldo *a)
2990 TCGv_reg tcg_rt = dest_gpr(ctx, a->t);
2992 /* Special case rb == 0, for the LDI pseudo-op.
2993 The COPY pseudo-op is handled for free within tcg_gen_addi_tl. */
2995 tcg_gen_movi_reg(tcg_rt, a->i);
2997 tcg_gen_addi_reg(tcg_rt, cpu_gr[a->b], a->i);
2999 save_gpr(ctx, a->t, tcg_rt);
3000 cond_free(&ctx->null_cond);
3004 static bool do_cmpb(DisasContext *ctx, unsigned r, TCGv_reg in1,
3005 unsigned c, unsigned f, unsigned n, int disp)
3007 TCGv_reg dest, in2, sv;
3010 in2 = load_gpr(ctx, r);
3011 dest = get_temp(ctx);
3013 tcg_gen_sub_reg(dest, in1, in2);
3016 if (cond_need_sv(c)) {
3017 sv = do_sub_sv(ctx, dest, in1, in2);
3020 cond = do_sub_cond(c * 2 + f, dest, in1, in2, sv);
3021 return do_cbranch(ctx, disp, n, &cond);
3024 static bool trans_cmpb(DisasContext *ctx, arg_cmpb *a)
3027 return do_cmpb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
3030 static bool trans_cmpbi(DisasContext *ctx, arg_cmpbi *a)
3033 return do_cmpb(ctx, a->r, load_const(ctx, a->i), a->c, a->f, a->n, a->disp);
3036 static bool do_addb(DisasContext *ctx, unsigned r, TCGv_reg in1,
3037 unsigned c, unsigned f, unsigned n, int disp)
3039 TCGv_reg dest, in2, sv, cb_msb;
3042 in2 = load_gpr(ctx, r);
3043 dest = tcg_temp_new();
3047 if (cond_need_cb(c)) {
3048 cb_msb = get_temp(ctx);
3049 tcg_gen_movi_reg(cb_msb, 0);
3050 tcg_gen_add2_reg(dest, cb_msb, in1, cb_msb, in2, cb_msb);
3052 tcg_gen_add_reg(dest, in1, in2);
3054 if (cond_need_sv(c)) {
3055 sv = do_add_sv(ctx, dest, in1, in2);
3058 cond = do_cond(c * 2 + f, dest, cb_msb, sv);
3059 save_gpr(ctx, r, dest);
3060 tcg_temp_free(dest);
3061 return do_cbranch(ctx, disp, n, &cond);
3064 static bool trans_addb(DisasContext *ctx, arg_addb *a)
3067 return do_addb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
3070 static bool trans_addbi(DisasContext *ctx, arg_addbi *a)
3073 return do_addb(ctx, a->r, load_const(ctx, a->i), a->c, a->f, a->n, a->disp);
3076 static bool trans_bb_sar(DisasContext *ctx, arg_bb_sar *a)
3078 TCGv_reg tmp, tcg_r;
3083 tmp = tcg_temp_new();
3084 tcg_r = load_gpr(ctx, a->r);
3085 tcg_gen_shl_reg(tmp, tcg_r, cpu_sar);
3087 cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
3089 return do_cbranch(ctx, a->disp, a->n, &cond);
3092 static bool trans_bb_imm(DisasContext *ctx, arg_bb_imm *a)
3094 TCGv_reg tmp, tcg_r;
3099 tmp = tcg_temp_new();
3100 tcg_r = load_gpr(ctx, a->r);
3101 tcg_gen_shli_reg(tmp, tcg_r, a->p);
3103 cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
3105 return do_cbranch(ctx, a->disp, a->n, &cond);
3108 static bool trans_movb(DisasContext *ctx, arg_movb *a)
3115 dest = dest_gpr(ctx, a->r2);
3117 tcg_gen_movi_reg(dest, 0);
3119 tcg_gen_mov_reg(dest, cpu_gr[a->r1]);
3122 cond = do_sed_cond(a->c, dest);
3123 return do_cbranch(ctx, a->disp, a->n, &cond);
3126 static bool trans_movbi(DisasContext *ctx, arg_movbi *a)
3133 dest = dest_gpr(ctx, a->r);
3134 tcg_gen_movi_reg(dest, a->i);
3136 cond = do_sed_cond(a->c, dest);
3137 return do_cbranch(ctx, a->disp, a->n, &cond);
3140 static bool trans_shrpw_sar(DisasContext *ctx, arg_shrpw_sar *a)
3148 dest = dest_gpr(ctx, a->t);
3150 tcg_gen_ext32u_reg(dest, load_gpr(ctx, a->r2));
3151 tcg_gen_shr_reg(dest, dest, cpu_sar);
3152 } else if (a->r1 == a->r2) {
3153 TCGv_i32 t32 = tcg_temp_new_i32();
3154 tcg_gen_trunc_reg_i32(t32, load_gpr(ctx, a->r2));
3155 tcg_gen_rotr_i32(t32, t32, cpu_sar);
3156 tcg_gen_extu_i32_reg(dest, t32);
3157 tcg_temp_free_i32(t32);
3159 TCGv_i64 t = tcg_temp_new_i64();
3160 TCGv_i64 s = tcg_temp_new_i64();
3162 tcg_gen_concat_reg_i64(t, load_gpr(ctx, a->r2), load_gpr(ctx, a->r1));
3163 tcg_gen_extu_reg_i64(s, cpu_sar);
3164 tcg_gen_shr_i64(t, t, s);
3165 tcg_gen_trunc_i64_reg(dest, t);
3167 tcg_temp_free_i64(t);
3168 tcg_temp_free_i64(s);
3170 save_gpr(ctx, a->t, dest);
3172 /* Install the new nullification. */
3173 cond_free(&ctx->null_cond);
3175 ctx->null_cond = do_sed_cond(a->c, dest);
3177 return nullify_end(ctx);
3180 static bool trans_shrpw_imm(DisasContext *ctx, arg_shrpw_imm *a)
3182 unsigned sa = 31 - a->cpos;
3189 dest = dest_gpr(ctx, a->t);
3190 t2 = load_gpr(ctx, a->r2);
3191 if (a->r1 == a->r2) {
3192 TCGv_i32 t32 = tcg_temp_new_i32();
3193 tcg_gen_trunc_reg_i32(t32, t2);
3194 tcg_gen_rotri_i32(t32, t32, sa);
3195 tcg_gen_extu_i32_reg(dest, t32);
3196 tcg_temp_free_i32(t32);
3197 } else if (a->r1 == 0) {
3198 tcg_gen_extract_reg(dest, t2, sa, 32 - sa);
3200 TCGv_reg t0 = tcg_temp_new();
3201 tcg_gen_extract_reg(t0, t2, sa, 32 - sa);
3202 tcg_gen_deposit_reg(dest, t0, cpu_gr[a->r1], 32 - sa, sa);
3205 save_gpr(ctx, a->t, dest);
3207 /* Install the new nullification. */
3208 cond_free(&ctx->null_cond);
3210 ctx->null_cond = do_sed_cond(a->c, dest);
3212 return nullify_end(ctx);
3215 static bool trans_extrw_sar(DisasContext *ctx, arg_extrw_sar *a)
3217 unsigned len = 32 - a->clen;
3218 TCGv_reg dest, src, tmp;
3224 dest = dest_gpr(ctx, a->t);
3225 src = load_gpr(ctx, a->r);
3226 tmp = tcg_temp_new();
3228 /* Recall that SAR is using big-endian bit numbering. */
3229 tcg_gen_xori_reg(tmp, cpu_sar, TARGET_REGISTER_BITS - 1);
3231 tcg_gen_sar_reg(dest, src, tmp);
3232 tcg_gen_sextract_reg(dest, dest, 0, len);
3234 tcg_gen_shr_reg(dest, src, tmp);
3235 tcg_gen_extract_reg(dest, dest, 0, len);
3238 save_gpr(ctx, a->t, dest);
3240 /* Install the new nullification. */
3241 cond_free(&ctx->null_cond);
3243 ctx->null_cond = do_sed_cond(a->c, dest);
3245 return nullify_end(ctx);
3248 static bool trans_extrw_imm(DisasContext *ctx, arg_extrw_imm *a)
3250 unsigned len = 32 - a->clen;
3251 unsigned cpos = 31 - a->pos;
3258 dest = dest_gpr(ctx, a->t);
3259 src = load_gpr(ctx, a->r);
3261 tcg_gen_sextract_reg(dest, src, cpos, len);
3263 tcg_gen_extract_reg(dest, src, cpos, len);
3265 save_gpr(ctx, a->t, dest);
3267 /* Install the new nullification. */
3268 cond_free(&ctx->null_cond);
3270 ctx->null_cond = do_sed_cond(a->c, dest);
3272 return nullify_end(ctx);
3275 static bool trans_depwi_imm(DisasContext *ctx, arg_depwi_imm *a)
3277 unsigned len = 32 - a->clen;
3278 target_sreg mask0, mask1;
3284 if (a->cpos + len > 32) {
3288 dest = dest_gpr(ctx, a->t);
3289 mask0 = deposit64(0, a->cpos, len, a->i);
3290 mask1 = deposit64(-1, a->cpos, len, a->i);
3293 TCGv_reg src = load_gpr(ctx, a->t);
3295 tcg_gen_andi_reg(dest, src, mask1);
3298 tcg_gen_ori_reg(dest, src, mask0);
3300 tcg_gen_movi_reg(dest, mask0);
3302 save_gpr(ctx, a->t, dest);
3304 /* Install the new nullification. */
3305 cond_free(&ctx->null_cond);
3307 ctx->null_cond = do_sed_cond(a->c, dest);
3309 return nullify_end(ctx);
3312 static bool trans_depw_imm(DisasContext *ctx, arg_depw_imm *a)
3314 unsigned rs = a->nz ? a->t : 0;
3315 unsigned len = 32 - a->clen;
3321 if (a->cpos + len > 32) {
3325 dest = dest_gpr(ctx, a->t);
3326 val = load_gpr(ctx, a->r);
3328 tcg_gen_deposit_z_reg(dest, val, a->cpos, len);
3330 tcg_gen_deposit_reg(dest, cpu_gr[rs], val, a->cpos, len);
3332 save_gpr(ctx, a->t, dest);
3334 /* Install the new nullification. */
3335 cond_free(&ctx->null_cond);
3337 ctx->null_cond = do_sed_cond(a->c, dest);
3339 return nullify_end(ctx);
3342 static bool do_depw_sar(DisasContext *ctx, unsigned rt, unsigned c,
3343 unsigned nz, unsigned clen, TCGv_reg val)
3345 unsigned rs = nz ? rt : 0;
3346 unsigned len = 32 - clen;
3347 TCGv_reg mask, tmp, shift, dest;
3348 unsigned msb = 1U << (len - 1);
3354 dest = dest_gpr(ctx, rt);
3355 shift = tcg_temp_new();
3356 tmp = tcg_temp_new();
3358 /* Convert big-endian bit numbering in SAR to left-shift. */
3359 tcg_gen_xori_reg(shift, cpu_sar, TARGET_REGISTER_BITS - 1);
3361 mask = tcg_const_reg(msb + (msb - 1));
3362 tcg_gen_and_reg(tmp, val, mask);
3364 tcg_gen_shl_reg(mask, mask, shift);
3365 tcg_gen_shl_reg(tmp, tmp, shift);
3366 tcg_gen_andc_reg(dest, cpu_gr[rs], mask);
3367 tcg_gen_or_reg(dest, dest, tmp);
3369 tcg_gen_shl_reg(dest, tmp, shift);
3371 tcg_temp_free(shift);
3372 tcg_temp_free(mask);
3374 save_gpr(ctx, rt, dest);
3376 /* Install the new nullification. */
3377 cond_free(&ctx->null_cond);
3379 ctx->null_cond = do_sed_cond(c, dest);
3381 return nullify_end(ctx);
3384 static bool trans_depw_sar(DisasContext *ctx, arg_depw_sar *a)
3386 return do_depw_sar(ctx, a->t, a->c, a->nz, a->clen, load_gpr(ctx, a->r));
3389 static bool trans_depwi_sar(DisasContext *ctx, arg_depwi_sar *a)
3391 return do_depw_sar(ctx, a->t, a->c, a->nz, a->clen, load_const(ctx, a->i));
3394 static bool trans_be(DisasContext *ctx, arg_be *a)
3398 #ifdef CONFIG_USER_ONLY
3399 /* ??? It seems like there should be a good way of using
3400 "be disp(sr2, r0)", the canonical gateway entry mechanism
3401 to our advantage. But that appears to be inconvenient to
3402 manage along side branch delay slots. Therefore we handle
3403 entry into the gateway page via absolute address. */
3404 /* Since we don't implement spaces, just branch. Do notice the special
3405 case of "be disp(*,r0)" using a direct branch to disp, so that we can
3406 goto_tb to the TB containing the syscall. */
3408 return do_dbranch(ctx, a->disp, a->l, a->n);
3414 tmp = get_temp(ctx);
3415 tcg_gen_addi_reg(tmp, load_gpr(ctx, a->b), a->disp);
3416 tmp = do_ibranch_priv(ctx, tmp);
3418 #ifdef CONFIG_USER_ONLY
3419 return do_ibranch(ctx, tmp, a->l, a->n);
3421 TCGv_i64 new_spc = tcg_temp_new_i64();
3423 load_spr(ctx, new_spc, a->sp);
3425 copy_iaoq_entry(cpu_gr[31], ctx->iaoq_n, ctx->iaoq_n_var);
3426 tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_f);
3428 if (a->n && use_nullify_skip(ctx)) {
3429 tcg_gen_mov_reg(cpu_iaoq_f, tmp);
3430 tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
3431 tcg_gen_mov_i64(cpu_iasq_f, new_spc);
3432 tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
3434 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3435 if (ctx->iaoq_b == -1) {
3436 tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3438 tcg_gen_mov_reg(cpu_iaoq_b, tmp);
3439 tcg_gen_mov_i64(cpu_iasq_b, new_spc);
3440 nullify_set(ctx, a->n);
3442 tcg_temp_free_i64(new_spc);
3443 tcg_gen_lookup_and_goto_ptr();
3444 ctx->base.is_jmp = DISAS_NORETURN;
3445 return nullify_end(ctx);
3449 static bool trans_bl(DisasContext *ctx, arg_bl *a)
3451 return do_dbranch(ctx, iaoq_dest(ctx, a->disp), a->l, a->n);
3454 static bool trans_b_gate(DisasContext *ctx, arg_b_gate *a)
3456 target_ureg dest = iaoq_dest(ctx, a->disp);
3460 /* Make sure the caller hasn't done something weird with the queue.
3461 * ??? This is not quite the same as the PSW[B] bit, which would be
3462 * expensive to track. Real hardware will trap for
3464 * b gateway+4 (in delay slot of first branch)
3465 * However, checking for a non-sequential instruction queue *will*
3466 * diagnose the security hole
3469 * in which instructions at evil would run with increased privs.
3471 if (ctx->iaoq_b == -1 || ctx->iaoq_b != ctx->iaoq_f + 4) {
3472 return gen_illegal(ctx);
3475 #ifndef CONFIG_USER_ONLY
3476 if (ctx->tb_flags & PSW_C) {
3477 CPUHPPAState *env = ctx->cs->env_ptr;
3478 int type = hppa_artype_for_page(env, ctx->base.pc_next);
3479 /* If we could not find a TLB entry, then we need to generate an
3480 ITLB miss exception so the kernel will provide it.
3481 The resulting TLB fill operation will invalidate this TB and
3482 we will re-translate, at which point we *will* be able to find
3483 the TLB entry and determine if this is in fact a gateway page. */
3485 gen_excp(ctx, EXCP_ITLB_MISS);
3488 /* No change for non-gateway pages or for priv decrease. */
3489 if (type >= 4 && type - 4 < ctx->privilege) {
3490 dest = deposit32(dest, 0, 2, type - 4);
3493 dest &= -4; /* priv = 0 */
3498 TCGv_reg tmp = dest_gpr(ctx, a->l);
3499 if (ctx->privilege < 3) {
3500 tcg_gen_andi_reg(tmp, tmp, -4);
3502 tcg_gen_ori_reg(tmp, tmp, ctx->privilege);
3503 save_gpr(ctx, a->l, tmp);
3506 return do_dbranch(ctx, dest, 0, a->n);
3509 static bool trans_blr(DisasContext *ctx, arg_blr *a)
3512 TCGv_reg tmp = get_temp(ctx);
3513 tcg_gen_shli_reg(tmp, load_gpr(ctx, a->x), 3);
3514 tcg_gen_addi_reg(tmp, tmp, ctx->iaoq_f + 8);
3515 /* The computation here never changes privilege level. */
3516 return do_ibranch(ctx, tmp, a->l, a->n);
3518 /* BLR R0,RX is a good way to load PC+8 into RX. */
3519 return do_dbranch(ctx, ctx->iaoq_f + 8, a->l, a->n);
3523 static bool trans_bv(DisasContext *ctx, arg_bv *a)
3528 dest = load_gpr(ctx, a->b);
3530 dest = get_temp(ctx);
3531 tcg_gen_shli_reg(dest, load_gpr(ctx, a->x), 3);
3532 tcg_gen_add_reg(dest, dest, load_gpr(ctx, a->b));
3534 dest = do_ibranch_priv(ctx, dest);
3535 return do_ibranch(ctx, dest, 0, a->n);
3538 static bool trans_bve(DisasContext *ctx, arg_bve *a)
3542 #ifdef CONFIG_USER_ONLY
3543 dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
3544 return do_ibranch(ctx, dest, a->l, a->n);
3547 dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
3549 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3550 if (ctx->iaoq_b == -1) {
3551 tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3553 copy_iaoq_entry(cpu_iaoq_b, -1, dest);
3554 tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
3556 copy_iaoq_entry(cpu_gr[a->l], ctx->iaoq_n, ctx->iaoq_n_var);
3558 nullify_set(ctx, a->n);
3559 tcg_gen_lookup_and_goto_ptr();
3560 ctx->base.is_jmp = DISAS_NORETURN;
3561 return nullify_end(ctx);
3569 static void gen_fcpy_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3571 tcg_gen_mov_i32(dst, src);
3574 static bool trans_fcpy_f(DisasContext *ctx, arg_fclass01 *a)
3576 return do_fop_wew(ctx, a->t, a->r, gen_fcpy_f);
3579 static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3581 tcg_gen_mov_i64(dst, src);
3584 static bool trans_fcpy_d(DisasContext *ctx, arg_fclass01 *a)
3586 return do_fop_ded(ctx, a->t, a->r, gen_fcpy_d);
3589 static void gen_fabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3591 tcg_gen_andi_i32(dst, src, INT32_MAX);
3594 static bool trans_fabs_f(DisasContext *ctx, arg_fclass01 *a)
3596 return do_fop_wew(ctx, a->t, a->r, gen_fabs_f);
3599 static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3601 tcg_gen_andi_i64(dst, src, INT64_MAX);
3604 static bool trans_fabs_d(DisasContext *ctx, arg_fclass01 *a)
3606 return do_fop_ded(ctx, a->t, a->r, gen_fabs_d);
3609 static bool trans_fsqrt_f(DisasContext *ctx, arg_fclass01 *a)
3611 return do_fop_wew(ctx, a->t, a->r, gen_helper_fsqrt_s);
3614 static bool trans_fsqrt_d(DisasContext *ctx, arg_fclass01 *a)
3616 return do_fop_ded(ctx, a->t, a->r, gen_helper_fsqrt_d);
3619 static bool trans_frnd_f(DisasContext *ctx, arg_fclass01 *a)
3621 return do_fop_wew(ctx, a->t, a->r, gen_helper_frnd_s);
3624 static bool trans_frnd_d(DisasContext *ctx, arg_fclass01 *a)
3626 return do_fop_ded(ctx, a->t, a->r, gen_helper_frnd_d);
3629 static void gen_fneg_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3631 tcg_gen_xori_i32(dst, src, INT32_MIN);
3634 static bool trans_fneg_f(DisasContext *ctx, arg_fclass01 *a)
3636 return do_fop_wew(ctx, a->t, a->r, gen_fneg_f);
3639 static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3641 tcg_gen_xori_i64(dst, src, INT64_MIN);
3644 static bool trans_fneg_d(DisasContext *ctx, arg_fclass01 *a)
3646 return do_fop_ded(ctx, a->t, a->r, gen_fneg_d);
3649 static void gen_fnegabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3651 tcg_gen_ori_i32(dst, src, INT32_MIN);
3654 static bool trans_fnegabs_f(DisasContext *ctx, arg_fclass01 *a)
3656 return do_fop_wew(ctx, a->t, a->r, gen_fnegabs_f);
3659 static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3661 tcg_gen_ori_i64(dst, src, INT64_MIN);
3664 static bool trans_fnegabs_d(DisasContext *ctx, arg_fclass01 *a)
3666 return do_fop_ded(ctx, a->t, a->r, gen_fnegabs_d);
3673 static bool trans_fcnv_d_f(DisasContext *ctx, arg_fclass01 *a)
3675 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_s);
3678 static bool trans_fcnv_f_d(DisasContext *ctx, arg_fclass01 *a)
3680 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_d);
3683 static bool trans_fcnv_w_f(DisasContext *ctx, arg_fclass01 *a)
3685 return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_w_s);
3688 static bool trans_fcnv_q_f(DisasContext *ctx, arg_fclass01 *a)
3690 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_dw_s);
3693 static bool trans_fcnv_w_d(DisasContext *ctx, arg_fclass01 *a)
3695 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_w_d);
3698 static bool trans_fcnv_q_d(DisasContext *ctx, arg_fclass01 *a)
3700 return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_dw_d);
3703 static bool trans_fcnv_f_w(DisasContext *ctx, arg_fclass01 *a)
3705 return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_w);
3708 static bool trans_fcnv_d_w(DisasContext *ctx, arg_fclass01 *a)
3710 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_w);
3713 static bool trans_fcnv_f_q(DisasContext *ctx, arg_fclass01 *a)
3715 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_dw);
3718 static bool trans_fcnv_d_q(DisasContext *ctx, arg_fclass01 *a)
3720 return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_dw);
3723 static bool trans_fcnv_t_f_w(DisasContext *ctx, arg_fclass01 *a)
3725 return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_w);
3728 static bool trans_fcnv_t_d_w(DisasContext *ctx, arg_fclass01 *a)
3730 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_w);
3733 static bool trans_fcnv_t_f_q(DisasContext *ctx, arg_fclass01 *a)
3735 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_dw);
3738 static bool trans_fcnv_t_d_q(DisasContext *ctx, arg_fclass01 *a)
3740 return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_dw);
3743 static bool trans_fcnv_uw_f(DisasContext *ctx, arg_fclass01 *a)
3745 return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_uw_s);
3748 static bool trans_fcnv_uq_f(DisasContext *ctx, arg_fclass01 *a)
3750 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_udw_s);
3753 static bool trans_fcnv_uw_d(DisasContext *ctx, arg_fclass01 *a)
3755 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_uw_d);
3758 static bool trans_fcnv_uq_d(DisasContext *ctx, arg_fclass01 *a)
3760 return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_udw_d);
3763 static bool trans_fcnv_f_uw(DisasContext *ctx, arg_fclass01 *a)
3765 return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_uw);
3768 static bool trans_fcnv_d_uw(DisasContext *ctx, arg_fclass01 *a)
3770 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_uw);
3773 static bool trans_fcnv_f_uq(DisasContext *ctx, arg_fclass01 *a)
3775 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_udw);
3778 static bool trans_fcnv_d_uq(DisasContext *ctx, arg_fclass01 *a)
3780 return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_udw);
3783 static bool trans_fcnv_t_f_uw(DisasContext *ctx, arg_fclass01 *a)
3785 return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_uw);
3788 static bool trans_fcnv_t_d_uw(DisasContext *ctx, arg_fclass01 *a)
3790 return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_uw);
3793 static bool trans_fcnv_t_f_uq(DisasContext *ctx, arg_fclass01 *a)
3795 return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_udw);
3798 static bool trans_fcnv_t_d_uq(DisasContext *ctx, arg_fclass01 *a)
3800 return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_udw);
3807 static bool trans_fcmp_f(DisasContext *ctx, arg_fclass2 *a)
3809 TCGv_i32 ta, tb, tc, ty;
3813 ta = load_frw0_i32(a->r1);
3814 tb = load_frw0_i32(a->r2);
3815 ty = tcg_const_i32(a->y);
3816 tc = tcg_const_i32(a->c);
3818 gen_helper_fcmp_s(cpu_env, ta, tb, ty, tc);
3820 tcg_temp_free_i32(ta);
3821 tcg_temp_free_i32(tb);
3822 tcg_temp_free_i32(ty);
3823 tcg_temp_free_i32(tc);
3825 return nullify_end(ctx);
3828 static bool trans_fcmp_d(DisasContext *ctx, arg_fclass2 *a)
3835 ta = load_frd0(a->r1);
3836 tb = load_frd0(a->r2);
3837 ty = tcg_const_i32(a->y);
3838 tc = tcg_const_i32(a->c);
3840 gen_helper_fcmp_d(cpu_env, ta, tb, ty, tc);
3842 tcg_temp_free_i64(ta);
3843 tcg_temp_free_i64(tb);
3844 tcg_temp_free_i32(ty);
3845 tcg_temp_free_i32(tc);
3847 return nullify_end(ctx);
3850 static bool trans_ftest(DisasContext *ctx, arg_ftest *a)
3857 tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
3864 case 0: /* simple */
3865 tcg_gen_andi_reg(t, t, 0x4000000);
3866 ctx->null_cond = cond_make_0(TCG_COND_NE, t);
3894 TCGv_reg c = load_const(ctx, mask);
3895 tcg_gen_or_reg(t, t, c);
3896 ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
3898 tcg_gen_andi_reg(t, t, mask);
3899 ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
3902 unsigned cbit = (a->y ^ 1) - 1;
3904 tcg_gen_extract_reg(t, t, 21 - cbit, 1);
3905 ctx->null_cond = cond_make_0(TCG_COND_NE, t);
3910 return nullify_end(ctx);
3917 static bool trans_fadd_f(DisasContext *ctx, arg_fclass3 *a)
3919 return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fadd_s);
3922 static bool trans_fadd_d(DisasContext *ctx, arg_fclass3 *a)
3924 return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fadd_d);
3927 static bool trans_fsub_f(DisasContext *ctx, arg_fclass3 *a)
3929 return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fsub_s);
3932 static bool trans_fsub_d(DisasContext *ctx, arg_fclass3 *a)
3934 return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fsub_d);
3937 static bool trans_fmpy_f(DisasContext *ctx, arg_fclass3 *a)
3939 return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_s);
3942 static bool trans_fmpy_d(DisasContext *ctx, arg_fclass3 *a)
3944 return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_d);
3947 static bool trans_fdiv_f(DisasContext *ctx, arg_fclass3 *a)
3949 return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_s);
3952 static bool trans_fdiv_d(DisasContext *ctx, arg_fclass3 *a)
3954 return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_d);
3957 static bool trans_xmpyu(DisasContext *ctx, arg_xmpyu *a)
3963 x = load_frw0_i64(a->r1);
3964 y = load_frw0_i64(a->r2);
3965 tcg_gen_mul_i64(x, x, y);
3967 tcg_temp_free_i64(x);
3968 tcg_temp_free_i64(y);
3970 return nullify_end(ctx);
3973 /* Convert the fmpyadd single-precision register encodings to standard. */
3974 static inline int fmpyadd_s_reg(unsigned r)
3976 return (r & 16) * 2 + 16 + (r & 15);
3979 static bool do_fmpyadd_s(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
3981 int tm = fmpyadd_s_reg(a->tm);
3982 int ra = fmpyadd_s_reg(a->ra);
3983 int ta = fmpyadd_s_reg(a->ta);
3984 int rm2 = fmpyadd_s_reg(a->rm2);
3985 int rm1 = fmpyadd_s_reg(a->rm1);
3989 do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
3990 do_fop_weww(ctx, ta, ta, ra,
3991 is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
3993 return nullify_end(ctx);
3996 static bool trans_fmpyadd_f(DisasContext *ctx, arg_mpyadd *a)
3998 return do_fmpyadd_s(ctx, a, false);
4001 static bool trans_fmpysub_f(DisasContext *ctx, arg_mpyadd *a)
4003 return do_fmpyadd_s(ctx, a, true);
4006 static bool do_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4010 do_fop_dedd(ctx, a->tm, a->rm1, a->rm2, gen_helper_fmpy_d);
4011 do_fop_dedd(ctx, a->ta, a->ta, a->ra,
4012 is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
4014 return nullify_end(ctx);
4017 static bool trans_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a)
4019 return do_fmpyadd_d(ctx, a, false);
4022 static bool trans_fmpysub_d(DisasContext *ctx, arg_mpyadd *a)
4024 return do_fmpyadd_d(ctx, a, true);
4027 static bool trans_fmpyfadd_f(DisasContext *ctx, arg_fmpyfadd_f *a)
4032 x = load_frw0_i32(a->rm1);
4033 y = load_frw0_i32(a->rm2);
4034 z = load_frw0_i32(a->ra3);
4037 gen_helper_fmpynfadd_s(x, cpu_env, x, y, z);
4039 gen_helper_fmpyfadd_s(x, cpu_env, x, y, z);
4042 tcg_temp_free_i32(y);
4043 tcg_temp_free_i32(z);
4044 save_frw_i32(a->t, x);
4045 tcg_temp_free_i32(x);
4046 return nullify_end(ctx);
4049 static bool trans_fmpyfadd_d(DisasContext *ctx, arg_fmpyfadd_d *a)
4054 x = load_frd0(a->rm1);
4055 y = load_frd0(a->rm2);
4056 z = load_frd0(a->ra3);
4059 gen_helper_fmpynfadd_d(x, cpu_env, x, y, z);
4061 gen_helper_fmpyfadd_d(x, cpu_env, x, y, z);
4064 tcg_temp_free_i64(y);
4065 tcg_temp_free_i64(z);
4067 tcg_temp_free_i64(x);
4068 return nullify_end(ctx);
4071 static bool trans_diag(DisasContext *ctx, arg_diag *a)
4073 qemu_log_mask(LOG_UNIMP, "DIAG opcode ignored\n");
4074 cond_free(&ctx->null_cond);
4078 static void hppa_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
4080 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4084 ctx->tb_flags = ctx->base.tb->flags;
4086 #ifdef CONFIG_USER_ONLY
4087 ctx->privilege = MMU_USER_IDX;
4088 ctx->mmu_idx = MMU_USER_IDX;
4089 ctx->iaoq_f = ctx->base.pc_first | MMU_USER_IDX;
4090 ctx->iaoq_b = ctx->base.tb->cs_base | MMU_USER_IDX;
4092 ctx->privilege = (ctx->tb_flags >> TB_FLAG_PRIV_SHIFT) & 3;
4093 ctx->mmu_idx = (ctx->tb_flags & PSW_D ? ctx->privilege : MMU_PHYS_IDX);
4095 /* Recover the IAOQ values from the GVA + PRIV. */
4096 uint64_t cs_base = ctx->base.tb->cs_base;
4097 uint64_t iasq_f = cs_base & ~0xffffffffull;
4098 int32_t diff = cs_base;
4100 ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
4101 ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
4104 ctx->iaoq_n_var = NULL;
4106 /* Bound the number of instructions by those left on the page. */
4107 bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
4108 ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
4112 memset(ctx->tempr, 0, sizeof(ctx->tempr));
4113 memset(ctx->templ, 0, sizeof(ctx->templ));
4116 static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
4118 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4120 /* Seed the nullification status from PSW[N], as saved in TB->FLAGS. */
4121 ctx->null_cond = cond_make_f();
4122 ctx->psw_n_nonzero = false;
4123 if (ctx->tb_flags & PSW_N) {
4124 ctx->null_cond.c = TCG_COND_ALWAYS;
4125 ctx->psw_n_nonzero = true;
4127 ctx->null_lab = NULL;
4130 static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
4132 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4134 tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b);
4137 static bool hppa_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,
4138 const CPUBreakpoint *bp)
4140 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4142 gen_excp(ctx, EXCP_DEBUG);
4143 ctx->base.pc_next += 4;
4147 static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
4149 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4150 CPUHPPAState *env = cs->env_ptr;
4154 /* Execute one insn. */
4155 #ifdef CONFIG_USER_ONLY
4156 if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
4158 ret = ctx->base.is_jmp;
4159 assert(ret != DISAS_NEXT);
4163 /* Always fetch the insn, even if nullified, so that we check
4164 the page permissions for execute. */
4165 uint32_t insn = cpu_ldl_code(env, ctx->base.pc_next);
4167 /* Set up the IA queue for the next insn.
4168 This will be overwritten by a branch. */
4169 if (ctx->iaoq_b == -1) {
4171 ctx->iaoq_n_var = get_temp(ctx);
4172 tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
4174 ctx->iaoq_n = ctx->iaoq_b + 4;
4175 ctx->iaoq_n_var = NULL;
4178 if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
4179 ctx->null_cond.c = TCG_COND_NEVER;
4183 if (!decode(ctx, insn)) {
4186 ret = ctx->base.is_jmp;
4187 assert(ctx->null_lab == NULL);
4191 /* Free any temporaries allocated. */
4192 for (i = 0, n = ctx->ntempr; i < n; ++i) {
4193 tcg_temp_free(ctx->tempr[i]);
4194 ctx->tempr[i] = NULL;
4196 for (i = 0, n = ctx->ntempl; i < n; ++i) {
4197 tcg_temp_free_tl(ctx->templ[i]);
4198 ctx->templ[i] = NULL;
4203 /* Advance the insn queue. Note that this check also detects
4204 a priority change within the instruction queue. */
4205 if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
4206 if (ctx->iaoq_b != -1 && ctx->iaoq_n != -1
4207 && use_goto_tb(ctx, ctx->iaoq_b)
4208 && (ctx->null_cond.c == TCG_COND_NEVER
4209 || ctx->null_cond.c == TCG_COND_ALWAYS)) {
4210 nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
4211 gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
4212 ctx->base.is_jmp = ret = DISAS_NORETURN;
4214 ctx->base.is_jmp = ret = DISAS_IAQ_N_STALE;
4217 ctx->iaoq_f = ctx->iaoq_b;
4218 ctx->iaoq_b = ctx->iaoq_n;
4219 ctx->base.pc_next += 4;
4221 if (ret == DISAS_NORETURN || ret == DISAS_IAQ_N_UPDATED) {
4224 if (ctx->iaoq_f == -1) {
4225 tcg_gen_mov_reg(cpu_iaoq_f, cpu_iaoq_b);
4226 copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
4227 #ifndef CONFIG_USER_ONLY
4228 tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4231 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
4232 } else if (ctx->iaoq_b == -1) {
4233 tcg_gen_mov_reg(cpu_iaoq_b, ctx->iaoq_n_var);
4237 static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
4239 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4240 DisasJumpType is_jmp = ctx->base.is_jmp;
4243 case DISAS_NORETURN:
4245 case DISAS_TOO_MANY:
4246 case DISAS_IAQ_N_STALE:
4247 case DISAS_IAQ_N_STALE_EXIT:
4248 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
4249 copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
4252 case DISAS_IAQ_N_UPDATED:
4253 if (ctx->base.singlestep_enabled) {
4254 gen_excp_1(EXCP_DEBUG);
4255 } else if (is_jmp == DISAS_IAQ_N_STALE_EXIT) {
4256 tcg_gen_exit_tb(NULL, 0);
4258 tcg_gen_lookup_and_goto_ptr();
4262 g_assert_not_reached();
4266 static void hppa_tr_disas_log(const DisasContextBase *dcbase, CPUState *cs)
4268 target_ulong pc = dcbase->pc_first;
4270 #ifdef CONFIG_USER_ONLY
4273 qemu_log("IN:\n0x00000000: (null)\n");
4276 qemu_log("IN:\n0x000000b0: light-weight-syscall\n");
4279 qemu_log("IN:\n0x000000e0: set-thread-pointer-syscall\n");
4282 qemu_log("IN:\n0x00000100: syscall\n");
4287 qemu_log("IN: %s\n", lookup_symbol(pc));
4288 log_target_disas(cs, pc, dcbase->tb->size);
4291 static const TranslatorOps hppa_tr_ops = {
4292 .init_disas_context = hppa_tr_init_disas_context,
4293 .tb_start = hppa_tr_tb_start,
4294 .insn_start = hppa_tr_insn_start,
4295 .breakpoint_check = hppa_tr_breakpoint_check,
4296 .translate_insn = hppa_tr_translate_insn,
4297 .tb_stop = hppa_tr_tb_stop,
4298 .disas_log = hppa_tr_disas_log,
4301 void gen_intermediate_code(CPUState *cs, struct TranslationBlock *tb)
4305 translator_loop(&hppa_tr_ops, &ctx.base, cs, tb);
4308 void restore_state_to_opc(CPUHPPAState *env, TranslationBlock *tb,
4311 env->iaoq_f = data[0];
4312 if (data[1] != (target_ureg)-1) {
4313 env->iaoq_b = data[1];
4315 /* Since we were executing the instruction at IAOQ_F, and took some
4316 sort of action that provoked the cpu_restore_state, we can infer
4317 that the instruction was not nullified. */