target/hppa: Implement LDWA
[qemu/ar7.git] / target / hppa / translate.c
blob3d441ef4acc24eee27c9878f9a5a451fb63f2481
1 /*
2 * HPPA emulation cpu translation for qemu.
4 * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
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"
21 #include "cpu.h"
22 #include "disas/disas.h"
23 #include "qemu/host-utils.h"
24 #include "exec/exec-all.h"
25 #include "tcg-op.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/helper-proto.h"
28 #include "exec/helper-gen.h"
29 #include "exec/translator.h"
30 #include "trace-tcg.h"
31 #include "exec/log.h"
33 /* Since we have a distinction between register size and address size,
34 we need to redefine all of these. */
36 #undef TCGv
37 #undef tcg_temp_new
38 #undef tcg_global_reg_new
39 #undef tcg_global_mem_new
40 #undef tcg_temp_local_new
41 #undef tcg_temp_free
43 #if TARGET_LONG_BITS == 64
44 #define TCGv_tl TCGv_i64
45 #define tcg_temp_new_tl tcg_temp_new_i64
46 #define tcg_temp_free_tl tcg_temp_free_i64
47 #if TARGET_REGISTER_BITS == 64
48 #define tcg_gen_extu_reg_tl tcg_gen_mov_i64
49 #else
50 #define tcg_gen_extu_reg_tl tcg_gen_extu_i32_i64
51 #endif
52 #else
53 #define TCGv_tl TCGv_i32
54 #define tcg_temp_new_tl tcg_temp_new_i32
55 #define tcg_temp_free_tl tcg_temp_free_i32
56 #define tcg_gen_extu_reg_tl tcg_gen_mov_i32
57 #endif
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 #if UINTPTR_MAX == UINT32_MAX
155 # define tcg_gen_trunc_reg_ptr(p, r) \
156 tcg_gen_trunc_i64_i32(TCGV_PTR_TO_NAT(p), r)
157 #else
158 # define tcg_gen_trunc_reg_ptr(p, r) \
159 tcg_gen_mov_i64(TCGV_PTR_TO_NAT(p), r)
160 #endif
161 #else
162 #define TCGv_reg TCGv_i32
163 #define tcg_temp_new tcg_temp_new_i32
164 #define tcg_global_reg_new tcg_global_reg_new_i32
165 #define tcg_global_mem_new tcg_global_mem_new_i32
166 #define tcg_temp_local_new tcg_temp_local_new_i32
167 #define tcg_temp_free tcg_temp_free_i32
169 #define tcg_gen_movi_reg tcg_gen_movi_i32
170 #define tcg_gen_mov_reg tcg_gen_mov_i32
171 #define tcg_gen_ld8u_reg tcg_gen_ld8u_i32
172 #define tcg_gen_ld8s_reg tcg_gen_ld8s_i32
173 #define tcg_gen_ld16u_reg tcg_gen_ld16u_i32
174 #define tcg_gen_ld16s_reg tcg_gen_ld16s_i32
175 #define tcg_gen_ld32u_reg tcg_gen_ld_i32
176 #define tcg_gen_ld32s_reg tcg_gen_ld_i32
177 #define tcg_gen_ld_reg tcg_gen_ld_i32
178 #define tcg_gen_st8_reg tcg_gen_st8_i32
179 #define tcg_gen_st16_reg tcg_gen_st16_i32
180 #define tcg_gen_st32_reg tcg_gen_st32_i32
181 #define tcg_gen_st_reg tcg_gen_st_i32
182 #define tcg_gen_add_reg tcg_gen_add_i32
183 #define tcg_gen_addi_reg tcg_gen_addi_i32
184 #define tcg_gen_sub_reg tcg_gen_sub_i32
185 #define tcg_gen_neg_reg tcg_gen_neg_i32
186 #define tcg_gen_subfi_reg tcg_gen_subfi_i32
187 #define tcg_gen_subi_reg tcg_gen_subi_i32
188 #define tcg_gen_and_reg tcg_gen_and_i32
189 #define tcg_gen_andi_reg tcg_gen_andi_i32
190 #define tcg_gen_or_reg tcg_gen_or_i32
191 #define tcg_gen_ori_reg tcg_gen_ori_i32
192 #define tcg_gen_xor_reg tcg_gen_xor_i32
193 #define tcg_gen_xori_reg tcg_gen_xori_i32
194 #define tcg_gen_not_reg tcg_gen_not_i32
195 #define tcg_gen_shl_reg tcg_gen_shl_i32
196 #define tcg_gen_shli_reg tcg_gen_shli_i32
197 #define tcg_gen_shr_reg tcg_gen_shr_i32
198 #define tcg_gen_shri_reg tcg_gen_shri_i32
199 #define tcg_gen_sar_reg tcg_gen_sar_i32
200 #define tcg_gen_sari_reg tcg_gen_sari_i32
201 #define tcg_gen_brcond_reg tcg_gen_brcond_i32
202 #define tcg_gen_brcondi_reg tcg_gen_brcondi_i32
203 #define tcg_gen_setcond_reg tcg_gen_setcond_i32
204 #define tcg_gen_setcondi_reg tcg_gen_setcondi_i32
205 #define tcg_gen_mul_reg tcg_gen_mul_i32
206 #define tcg_gen_muli_reg tcg_gen_muli_i32
207 #define tcg_gen_div_reg tcg_gen_div_i32
208 #define tcg_gen_rem_reg tcg_gen_rem_i32
209 #define tcg_gen_divu_reg tcg_gen_divu_i32
210 #define tcg_gen_remu_reg tcg_gen_remu_i32
211 #define tcg_gen_discard_reg tcg_gen_discard_i32
212 #define tcg_gen_trunc_reg_i32 tcg_gen_mov_i32
213 #define tcg_gen_trunc_i64_reg tcg_gen_extrl_i64_i32
214 #define tcg_gen_extu_i32_reg tcg_gen_mov_i32
215 #define tcg_gen_ext_i32_reg tcg_gen_mov_i32
216 #define tcg_gen_extu_reg_i64 tcg_gen_extu_i32_i64
217 #define tcg_gen_ext_reg_i64 tcg_gen_ext_i32_i64
218 #define tcg_gen_ext8u_reg tcg_gen_ext8u_i32
219 #define tcg_gen_ext8s_reg tcg_gen_ext8s_i32
220 #define tcg_gen_ext16u_reg tcg_gen_ext16u_i32
221 #define tcg_gen_ext16s_reg tcg_gen_ext16s_i32
222 #define tcg_gen_ext32u_reg tcg_gen_mov_i32
223 #define tcg_gen_ext32s_reg tcg_gen_mov_i32
224 #define tcg_gen_bswap16_reg tcg_gen_bswap16_i32
225 #define tcg_gen_bswap32_reg tcg_gen_bswap32_i32
226 #define tcg_gen_concat_reg_i64 tcg_gen_concat_i32_i64
227 #define tcg_gen_andc_reg tcg_gen_andc_i32
228 #define tcg_gen_eqv_reg tcg_gen_eqv_i32
229 #define tcg_gen_nand_reg tcg_gen_nand_i32
230 #define tcg_gen_nor_reg tcg_gen_nor_i32
231 #define tcg_gen_orc_reg tcg_gen_orc_i32
232 #define tcg_gen_clz_reg tcg_gen_clz_i32
233 #define tcg_gen_ctz_reg tcg_gen_ctz_i32
234 #define tcg_gen_clzi_reg tcg_gen_clzi_i32
235 #define tcg_gen_ctzi_reg tcg_gen_ctzi_i32
236 #define tcg_gen_clrsb_reg tcg_gen_clrsb_i32
237 #define tcg_gen_ctpop_reg tcg_gen_ctpop_i32
238 #define tcg_gen_rotl_reg tcg_gen_rotl_i32
239 #define tcg_gen_rotli_reg tcg_gen_rotli_i32
240 #define tcg_gen_rotr_reg tcg_gen_rotr_i32
241 #define tcg_gen_rotri_reg tcg_gen_rotri_i32
242 #define tcg_gen_deposit_reg tcg_gen_deposit_i32
243 #define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i32
244 #define tcg_gen_extract_reg tcg_gen_extract_i32
245 #define tcg_gen_sextract_reg tcg_gen_sextract_i32
246 #define tcg_const_reg tcg_const_i32
247 #define tcg_const_local_reg tcg_const_local_i32
248 #define tcg_gen_movcond_reg tcg_gen_movcond_i32
249 #define tcg_gen_add2_reg tcg_gen_add2_i32
250 #define tcg_gen_sub2_reg tcg_gen_sub2_i32
251 #define tcg_gen_qemu_ld_reg tcg_gen_qemu_ld_i32
252 #define tcg_gen_qemu_st_reg tcg_gen_qemu_st_i32
253 #define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i32
254 #if UINTPTR_MAX == UINT32_MAX
255 # define tcg_gen_trunc_reg_ptr(p, r) \
256 tcg_gen_mov_i32(TCGV_PTR_TO_NAT(p), r)
257 #else
258 # define tcg_gen_trunc_reg_ptr(p, r) \
259 tcg_gen_extu_i32_i64(TCGV_PTR_TO_NAT(p), r)
260 #endif
261 #endif /* TARGET_REGISTER_BITS */
263 typedef struct DisasCond {
264 TCGCond c;
265 TCGv_reg a0, a1;
266 bool a0_is_n;
267 bool a1_is_0;
268 } DisasCond;
270 typedef struct DisasContext {
271 DisasContextBase base;
272 CPUState *cs;
274 target_ureg iaoq_f;
275 target_ureg iaoq_b;
276 target_ureg iaoq_n;
277 TCGv_reg iaoq_n_var;
279 int ntempr, ntempl;
280 TCGv_reg tempr[4];
281 TCGv_tl templ[4];
283 DisasCond null_cond;
284 TCGLabel *null_lab;
286 uint32_t insn;
287 int mmu_idx;
288 int privilege;
289 bool psw_n_nonzero;
290 } DisasContext;
292 /* Target-specific return values from translate_one, indicating the
293 state of the TB. Note that DISAS_NEXT indicates that we are not
294 exiting the TB. */
296 /* We are not using a goto_tb (for whatever reason), but have updated
297 the iaq (for whatever reason), so don't do it again on exit. */
298 #define DISAS_IAQ_N_UPDATED DISAS_TARGET_0
300 /* We are exiting the TB, but have neither emitted a goto_tb, nor
301 updated the iaq for the next instruction to be executed. */
302 #define DISAS_IAQ_N_STALE DISAS_TARGET_1
304 /* Similarly, but we want to return to the main loop immediately
305 to recognize unmasked interrupts. */
306 #define DISAS_IAQ_N_STALE_EXIT DISAS_TARGET_2
308 typedef struct DisasInsn {
309 uint32_t insn, mask;
310 DisasJumpType (*trans)(DisasContext *ctx, uint32_t insn,
311 const struct DisasInsn *f);
312 union {
313 void (*ttt)(TCGv_reg, TCGv_reg, TCGv_reg);
314 void (*weww)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32);
315 void (*dedd)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64);
316 void (*wew)(TCGv_i32, TCGv_env, TCGv_i32);
317 void (*ded)(TCGv_i64, TCGv_env, TCGv_i64);
318 void (*wed)(TCGv_i32, TCGv_env, TCGv_i64);
319 void (*dew)(TCGv_i64, TCGv_env, TCGv_i32);
320 } f;
321 } DisasInsn;
323 /* global register indexes */
324 static TCGv_reg cpu_gr[32];
325 static TCGv_i64 cpu_sr[4];
326 static TCGv_reg cpu_iaoq_f;
327 static TCGv_reg cpu_iaoq_b;
328 static TCGv_i64 cpu_iasq_f;
329 static TCGv_i64 cpu_iasq_b;
330 static TCGv_reg cpu_sar;
331 static TCGv_reg cpu_psw_n;
332 static TCGv_reg cpu_psw_v;
333 static TCGv_reg cpu_psw_cb;
334 static TCGv_reg cpu_psw_cb_msb;
336 #include "exec/gen-icount.h"
338 void hppa_translate_init(void)
340 #define DEF_VAR(V) { &cpu_##V, #V, offsetof(CPUHPPAState, V) }
342 typedef struct { TCGv_reg *var; const char *name; int ofs; } GlobalVar;
343 static const GlobalVar vars[] = {
344 { &cpu_sar, "sar", offsetof(CPUHPPAState, cr[CR_SAR]) },
345 DEF_VAR(psw_n),
346 DEF_VAR(psw_v),
347 DEF_VAR(psw_cb),
348 DEF_VAR(psw_cb_msb),
349 DEF_VAR(iaoq_f),
350 DEF_VAR(iaoq_b),
353 #undef DEF_VAR
355 /* Use the symbolic register names that match the disassembler. */
356 static const char gr_names[32][4] = {
357 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
358 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
359 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
360 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
362 /* SR[4-7] are not global registers so that we can index them. */
363 static const char sr_names[4][4] = {
364 "sr0", "sr1", "sr2", "sr3"
367 int i;
369 cpu_gr[0] = NULL;
370 for (i = 1; i < 32; i++) {
371 cpu_gr[i] = tcg_global_mem_new(cpu_env,
372 offsetof(CPUHPPAState, gr[i]),
373 gr_names[i]);
375 for (i = 0; i < 4; i++) {
376 cpu_sr[i] = tcg_global_mem_new_i64(cpu_env,
377 offsetof(CPUHPPAState, sr[i]),
378 sr_names[i]);
381 for (i = 0; i < ARRAY_SIZE(vars); ++i) {
382 const GlobalVar *v = &vars[i];
383 *v->var = tcg_global_mem_new(cpu_env, v->ofs, v->name);
386 cpu_iasq_f = tcg_global_mem_new_i64(cpu_env,
387 offsetof(CPUHPPAState, iasq_f),
388 "iasq_f");
389 cpu_iasq_b = tcg_global_mem_new_i64(cpu_env,
390 offsetof(CPUHPPAState, iasq_b),
391 "iasq_b");
394 static DisasCond cond_make_f(void)
396 return (DisasCond){
397 .c = TCG_COND_NEVER,
398 .a0 = NULL,
399 .a1 = NULL,
403 static DisasCond cond_make_n(void)
405 return (DisasCond){
406 .c = TCG_COND_NE,
407 .a0 = cpu_psw_n,
408 .a0_is_n = true,
409 .a1 = NULL,
410 .a1_is_0 = true
414 static DisasCond cond_make_0(TCGCond c, TCGv_reg a0)
416 DisasCond r = { .c = c, .a1 = NULL, .a1_is_0 = true };
418 assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
419 r.a0 = tcg_temp_new();
420 tcg_gen_mov_reg(r.a0, a0);
422 return r;
425 static DisasCond cond_make(TCGCond c, TCGv_reg a0, TCGv_reg a1)
427 DisasCond r = { .c = c };
429 assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
430 r.a0 = tcg_temp_new();
431 tcg_gen_mov_reg(r.a0, a0);
432 r.a1 = tcg_temp_new();
433 tcg_gen_mov_reg(r.a1, a1);
435 return r;
438 static void cond_prep(DisasCond *cond)
440 if (cond->a1_is_0) {
441 cond->a1_is_0 = false;
442 cond->a1 = tcg_const_reg(0);
446 static void cond_free(DisasCond *cond)
448 switch (cond->c) {
449 default:
450 if (!cond->a0_is_n) {
451 tcg_temp_free(cond->a0);
453 if (!cond->a1_is_0) {
454 tcg_temp_free(cond->a1);
456 cond->a0_is_n = false;
457 cond->a1_is_0 = false;
458 cond->a0 = NULL;
459 cond->a1 = NULL;
460 /* fallthru */
461 case TCG_COND_ALWAYS:
462 cond->c = TCG_COND_NEVER;
463 break;
464 case TCG_COND_NEVER:
465 break;
469 static TCGv_reg get_temp(DisasContext *ctx)
471 unsigned i = ctx->ntempr++;
472 g_assert(i < ARRAY_SIZE(ctx->tempr));
473 return ctx->tempr[i] = tcg_temp_new();
476 #ifndef CONFIG_USER_ONLY
477 static TCGv_tl get_temp_tl(DisasContext *ctx)
479 unsigned i = ctx->ntempl++;
480 g_assert(i < ARRAY_SIZE(ctx->templ));
481 return ctx->templ[i] = tcg_temp_new_tl();
483 #endif
485 static TCGv_reg load_const(DisasContext *ctx, target_sreg v)
487 TCGv_reg t = get_temp(ctx);
488 tcg_gen_movi_reg(t, v);
489 return t;
492 static TCGv_reg load_gpr(DisasContext *ctx, unsigned reg)
494 if (reg == 0) {
495 TCGv_reg t = get_temp(ctx);
496 tcg_gen_movi_reg(t, 0);
497 return t;
498 } else {
499 return cpu_gr[reg];
503 static TCGv_reg dest_gpr(DisasContext *ctx, unsigned reg)
505 if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
506 return get_temp(ctx);
507 } else {
508 return cpu_gr[reg];
512 static void save_or_nullify(DisasContext *ctx, TCGv_reg dest, TCGv_reg t)
514 if (ctx->null_cond.c != TCG_COND_NEVER) {
515 cond_prep(&ctx->null_cond);
516 tcg_gen_movcond_reg(ctx->null_cond.c, dest, ctx->null_cond.a0,
517 ctx->null_cond.a1, dest, t);
518 } else {
519 tcg_gen_mov_reg(dest, t);
523 static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_reg t)
525 if (reg != 0) {
526 save_or_nullify(ctx, cpu_gr[reg], t);
530 #ifdef HOST_WORDS_BIGENDIAN
531 # define HI_OFS 0
532 # define LO_OFS 4
533 #else
534 # define HI_OFS 4
535 # define LO_OFS 0
536 #endif
538 static TCGv_i32 load_frw_i32(unsigned rt)
540 TCGv_i32 ret = tcg_temp_new_i32();
541 tcg_gen_ld_i32(ret, cpu_env,
542 offsetof(CPUHPPAState, fr[rt & 31])
543 + (rt & 32 ? LO_OFS : HI_OFS));
544 return ret;
547 static TCGv_i32 load_frw0_i32(unsigned rt)
549 if (rt == 0) {
550 return tcg_const_i32(0);
551 } else {
552 return load_frw_i32(rt);
556 static TCGv_i64 load_frw0_i64(unsigned rt)
558 if (rt == 0) {
559 return tcg_const_i64(0);
560 } else {
561 TCGv_i64 ret = tcg_temp_new_i64();
562 tcg_gen_ld32u_i64(ret, cpu_env,
563 offsetof(CPUHPPAState, fr[rt & 31])
564 + (rt & 32 ? LO_OFS : HI_OFS));
565 return ret;
569 static void save_frw_i32(unsigned rt, TCGv_i32 val)
571 tcg_gen_st_i32(val, cpu_env,
572 offsetof(CPUHPPAState, fr[rt & 31])
573 + (rt & 32 ? LO_OFS : HI_OFS));
576 #undef HI_OFS
577 #undef LO_OFS
579 static TCGv_i64 load_frd(unsigned rt)
581 TCGv_i64 ret = tcg_temp_new_i64();
582 tcg_gen_ld_i64(ret, cpu_env, offsetof(CPUHPPAState, fr[rt]));
583 return ret;
586 static TCGv_i64 load_frd0(unsigned rt)
588 if (rt == 0) {
589 return tcg_const_i64(0);
590 } else {
591 return load_frd(rt);
595 static void save_frd(unsigned rt, TCGv_i64 val)
597 tcg_gen_st_i64(val, cpu_env, offsetof(CPUHPPAState, fr[rt]));
600 static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
602 #ifdef CONFIG_USER_ONLY
603 tcg_gen_movi_i64(dest, 0);
604 #else
605 if (reg < 4) {
606 tcg_gen_mov_i64(dest, cpu_sr[reg]);
607 } else {
608 tcg_gen_ld_i64(dest, cpu_env, offsetof(CPUHPPAState, sr[reg]));
610 #endif
613 /* Skip over the implementation of an insn that has been nullified.
614 Use this when the insn is too complex for a conditional move. */
615 static void nullify_over(DisasContext *ctx)
617 if (ctx->null_cond.c != TCG_COND_NEVER) {
618 /* The always condition should have been handled in the main loop. */
619 assert(ctx->null_cond.c != TCG_COND_ALWAYS);
621 ctx->null_lab = gen_new_label();
622 cond_prep(&ctx->null_cond);
624 /* If we're using PSW[N], copy it to a temp because... */
625 if (ctx->null_cond.a0_is_n) {
626 ctx->null_cond.a0_is_n = false;
627 ctx->null_cond.a0 = tcg_temp_new();
628 tcg_gen_mov_reg(ctx->null_cond.a0, cpu_psw_n);
630 /* ... we clear it before branching over the implementation,
631 so that (1) it's clear after nullifying this insn and
632 (2) if this insn nullifies the next, PSW[N] is valid. */
633 if (ctx->psw_n_nonzero) {
634 ctx->psw_n_nonzero = false;
635 tcg_gen_movi_reg(cpu_psw_n, 0);
638 tcg_gen_brcond_reg(ctx->null_cond.c, ctx->null_cond.a0,
639 ctx->null_cond.a1, ctx->null_lab);
640 cond_free(&ctx->null_cond);
644 /* Save the current nullification state to PSW[N]. */
645 static void nullify_save(DisasContext *ctx)
647 if (ctx->null_cond.c == TCG_COND_NEVER) {
648 if (ctx->psw_n_nonzero) {
649 tcg_gen_movi_reg(cpu_psw_n, 0);
651 return;
653 if (!ctx->null_cond.a0_is_n) {
654 cond_prep(&ctx->null_cond);
655 tcg_gen_setcond_reg(ctx->null_cond.c, cpu_psw_n,
656 ctx->null_cond.a0, ctx->null_cond.a1);
657 ctx->psw_n_nonzero = true;
659 cond_free(&ctx->null_cond);
662 /* Set a PSW[N] to X. The intention is that this is used immediately
663 before a goto_tb/exit_tb, so that there is no fallthru path to other
664 code within the TB. Therefore we do not update psw_n_nonzero. */
665 static void nullify_set(DisasContext *ctx, bool x)
667 if (ctx->psw_n_nonzero || x) {
668 tcg_gen_movi_reg(cpu_psw_n, x);
672 /* Mark the end of an instruction that may have been nullified.
673 This is the pair to nullify_over. */
674 static DisasJumpType nullify_end(DisasContext *ctx, DisasJumpType status)
676 TCGLabel *null_lab = ctx->null_lab;
678 /* For NEXT, NORETURN, STALE, we can easily continue (or exit).
679 For UPDATED, we cannot update on the nullified path. */
680 assert(status != DISAS_IAQ_N_UPDATED);
682 if (likely(null_lab == NULL)) {
683 /* The current insn wasn't conditional or handled the condition
684 applied to it without a branch, so the (new) setting of
685 NULL_COND can be applied directly to the next insn. */
686 return status;
688 ctx->null_lab = NULL;
690 if (likely(ctx->null_cond.c == TCG_COND_NEVER)) {
691 /* The next instruction will be unconditional,
692 and NULL_COND already reflects that. */
693 gen_set_label(null_lab);
694 } else {
695 /* The insn that we just executed is itself nullifying the next
696 instruction. Store the condition in the PSW[N] global.
697 We asserted PSW[N] = 0 in nullify_over, so that after the
698 label we have the proper value in place. */
699 nullify_save(ctx);
700 gen_set_label(null_lab);
701 ctx->null_cond = cond_make_n();
703 if (status == DISAS_NORETURN) {
704 status = DISAS_NEXT;
706 return status;
709 static void copy_iaoq_entry(TCGv_reg dest, target_ureg ival, TCGv_reg vval)
711 if (unlikely(ival == -1)) {
712 tcg_gen_mov_reg(dest, vval);
713 } else {
714 tcg_gen_movi_reg(dest, ival);
718 static inline target_ureg iaoq_dest(DisasContext *ctx, target_sreg disp)
720 return ctx->iaoq_f + disp + 8;
723 static void gen_excp_1(int exception)
725 TCGv_i32 t = tcg_const_i32(exception);
726 gen_helper_excp(cpu_env, t);
727 tcg_temp_free_i32(t);
730 static DisasJumpType gen_excp(DisasContext *ctx, int exception)
732 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
733 copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
734 nullify_save(ctx);
735 gen_excp_1(exception);
736 return DISAS_NORETURN;
739 static DisasJumpType gen_excp_iir(DisasContext *ctx, int exc)
741 TCGv_reg tmp = tcg_const_reg(ctx->insn);
742 tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[CR_IIR]));
743 tcg_temp_free(tmp);
744 return gen_excp(ctx, exc);
747 static DisasJumpType gen_illegal(DisasContext *ctx)
749 nullify_over(ctx);
750 return nullify_end(ctx, gen_excp_iir(ctx, EXCP_ILL));
753 #define CHECK_MOST_PRIVILEGED(EXCP) \
754 do { \
755 if (ctx->privilege != 0) { \
756 nullify_over(ctx); \
757 return nullify_end(ctx, gen_excp_iir(ctx, EXCP)); \
759 } while (0)
761 static bool use_goto_tb(DisasContext *ctx, target_ureg dest)
763 /* Suppress goto_tb in the case of single-steping and IO. */
764 if ((tb_cflags(ctx->base.tb) & CF_LAST_IO) || ctx->base.singlestep_enabled) {
765 return false;
767 return true;
770 /* If the next insn is to be nullified, and it's on the same page,
771 and we're not attempting to set a breakpoint on it, then we can
772 totally skip the nullified insn. This avoids creating and
773 executing a TB that merely branches to the next TB. */
774 static bool use_nullify_skip(DisasContext *ctx)
776 return (((ctx->iaoq_b ^ ctx->iaoq_f) & TARGET_PAGE_MASK) == 0
777 && !cpu_breakpoint_test(ctx->cs, ctx->iaoq_b, BP_ANY));
780 static void gen_goto_tb(DisasContext *ctx, int which,
781 target_ureg f, target_ureg b)
783 if (f != -1 && b != -1 && use_goto_tb(ctx, f)) {
784 tcg_gen_goto_tb(which);
785 tcg_gen_movi_reg(cpu_iaoq_f, f);
786 tcg_gen_movi_reg(cpu_iaoq_b, b);
787 tcg_gen_exit_tb((uintptr_t)ctx->base.tb + which);
788 } else {
789 copy_iaoq_entry(cpu_iaoq_f, f, cpu_iaoq_b);
790 copy_iaoq_entry(cpu_iaoq_b, b, ctx->iaoq_n_var);
791 if (ctx->base.singlestep_enabled) {
792 gen_excp_1(EXCP_DEBUG);
793 } else {
794 tcg_gen_lookup_and_goto_ptr();
799 /* PA has a habit of taking the LSB of a field and using that as the sign,
800 with the rest of the field becoming the least significant bits. */
801 static target_sreg low_sextract(uint32_t val, int pos, int len)
803 target_ureg x = -(target_ureg)extract32(val, pos, 1);
804 x = (x << (len - 1)) | extract32(val, pos + 1, len - 1);
805 return x;
808 static unsigned assemble_rt64(uint32_t insn)
810 unsigned r1 = extract32(insn, 6, 1);
811 unsigned r0 = extract32(insn, 0, 5);
812 return r1 * 32 + r0;
815 static unsigned assemble_ra64(uint32_t insn)
817 unsigned r1 = extract32(insn, 7, 1);
818 unsigned r0 = extract32(insn, 21, 5);
819 return r1 * 32 + r0;
822 static unsigned assemble_rb64(uint32_t insn)
824 unsigned r1 = extract32(insn, 12, 1);
825 unsigned r0 = extract32(insn, 16, 5);
826 return r1 * 32 + r0;
829 static unsigned assemble_rc64(uint32_t insn)
831 unsigned r2 = extract32(insn, 8, 1);
832 unsigned r1 = extract32(insn, 13, 3);
833 unsigned r0 = extract32(insn, 9, 2);
834 return r2 * 32 + r1 * 4 + r0;
837 static unsigned assemble_sr3(uint32_t insn)
839 unsigned s2 = extract32(insn, 13, 1);
840 unsigned s0 = extract32(insn, 14, 2);
841 return s2 * 4 + s0;
844 static target_sreg assemble_12(uint32_t insn)
846 target_ureg x = -(target_ureg)(insn & 1);
847 x = (x << 1) | extract32(insn, 2, 1);
848 x = (x << 10) | extract32(insn, 3, 10);
849 return x;
852 static target_sreg assemble_16(uint32_t insn)
854 /* Take the name from PA2.0, which produces a 16-bit number
855 only with wide mode; otherwise a 14-bit number. Since we don't
856 implement wide mode, this is always the 14-bit number. */
857 return low_sextract(insn, 0, 14);
860 static target_sreg assemble_16a(uint32_t insn)
862 /* Take the name from PA2.0, which produces a 14-bit shifted number
863 only with wide mode; otherwise a 12-bit shifted number. Since we
864 don't implement wide mode, this is always the 12-bit number. */
865 target_ureg x = -(target_ureg)(insn & 1);
866 x = (x << 11) | extract32(insn, 2, 11);
867 return x << 2;
870 static target_sreg assemble_17(uint32_t insn)
872 target_ureg x = -(target_ureg)(insn & 1);
873 x = (x << 5) | extract32(insn, 16, 5);
874 x = (x << 1) | extract32(insn, 2, 1);
875 x = (x << 10) | extract32(insn, 3, 10);
876 return x << 2;
879 static target_sreg assemble_21(uint32_t insn)
881 target_ureg x = -(target_ureg)(insn & 1);
882 x = (x << 11) | extract32(insn, 1, 11);
883 x = (x << 2) | extract32(insn, 14, 2);
884 x = (x << 5) | extract32(insn, 16, 5);
885 x = (x << 2) | extract32(insn, 12, 2);
886 return x << 11;
889 static target_sreg assemble_22(uint32_t insn)
891 target_ureg x = -(target_ureg)(insn & 1);
892 x = (x << 10) | extract32(insn, 16, 10);
893 x = (x << 1) | extract32(insn, 2, 1);
894 x = (x << 10) | extract32(insn, 3, 10);
895 return x << 2;
898 /* The parisc documentation describes only the general interpretation of
899 the conditions, without describing their exact implementation. The
900 interpretations do not stand up well when considering ADD,C and SUB,B.
901 However, considering the Addition, Subtraction and Logical conditions
902 as a whole it would appear that these relations are similar to what
903 a traditional NZCV set of flags would produce. */
905 static DisasCond do_cond(unsigned cf, TCGv_reg res,
906 TCGv_reg cb_msb, TCGv_reg sv)
908 DisasCond cond;
909 TCGv_reg tmp;
911 switch (cf >> 1) {
912 case 0: /* Never / TR */
913 cond = cond_make_f();
914 break;
915 case 1: /* = / <> (Z / !Z) */
916 cond = cond_make_0(TCG_COND_EQ, res);
917 break;
918 case 2: /* < / >= (N / !N) */
919 cond = cond_make_0(TCG_COND_LT, res);
920 break;
921 case 3: /* <= / > (N | Z / !N & !Z) */
922 cond = cond_make_0(TCG_COND_LE, res);
923 break;
924 case 4: /* NUV / UV (!C / C) */
925 cond = cond_make_0(TCG_COND_EQ, cb_msb);
926 break;
927 case 5: /* ZNV / VNZ (!C | Z / C & !Z) */
928 tmp = tcg_temp_new();
929 tcg_gen_neg_reg(tmp, cb_msb);
930 tcg_gen_and_reg(tmp, tmp, res);
931 cond = cond_make_0(TCG_COND_EQ, tmp);
932 tcg_temp_free(tmp);
933 break;
934 case 6: /* SV / NSV (V / !V) */
935 cond = cond_make_0(TCG_COND_LT, sv);
936 break;
937 case 7: /* OD / EV */
938 tmp = tcg_temp_new();
939 tcg_gen_andi_reg(tmp, res, 1);
940 cond = cond_make_0(TCG_COND_NE, tmp);
941 tcg_temp_free(tmp);
942 break;
943 default:
944 g_assert_not_reached();
946 if (cf & 1) {
947 cond.c = tcg_invert_cond(cond.c);
950 return cond;
953 /* Similar, but for the special case of subtraction without borrow, we
954 can use the inputs directly. This can allow other computation to be
955 deleted as unused. */
957 static DisasCond do_sub_cond(unsigned cf, TCGv_reg res,
958 TCGv_reg in1, TCGv_reg in2, TCGv_reg sv)
960 DisasCond cond;
962 switch (cf >> 1) {
963 case 1: /* = / <> */
964 cond = cond_make(TCG_COND_EQ, in1, in2);
965 break;
966 case 2: /* < / >= */
967 cond = cond_make(TCG_COND_LT, in1, in2);
968 break;
969 case 3: /* <= / > */
970 cond = cond_make(TCG_COND_LE, in1, in2);
971 break;
972 case 4: /* << / >>= */
973 cond = cond_make(TCG_COND_LTU, in1, in2);
974 break;
975 case 5: /* <<= / >> */
976 cond = cond_make(TCG_COND_LEU, in1, in2);
977 break;
978 default:
979 return do_cond(cf, res, sv, sv);
981 if (cf & 1) {
982 cond.c = tcg_invert_cond(cond.c);
985 return cond;
988 /* Similar, but for logicals, where the carry and overflow bits are not
989 computed, and use of them is undefined. */
991 static DisasCond do_log_cond(unsigned cf, TCGv_reg res)
993 switch (cf >> 1) {
994 case 4: case 5: case 6:
995 cf &= 1;
996 break;
998 return do_cond(cf, res, res, res);
1001 /* Similar, but for shift/extract/deposit conditions. */
1003 static DisasCond do_sed_cond(unsigned orig, TCGv_reg res)
1005 unsigned c, f;
1007 /* Convert the compressed condition codes to standard.
1008 0-2 are the same as logicals (nv,<,<=), while 3 is OD.
1009 4-7 are the reverse of 0-3. */
1010 c = orig & 3;
1011 if (c == 3) {
1012 c = 7;
1014 f = (orig & 4) / 4;
1016 return do_log_cond(c * 2 + f, res);
1019 /* Similar, but for unit conditions. */
1021 static DisasCond do_unit_cond(unsigned cf, TCGv_reg res,
1022 TCGv_reg in1, TCGv_reg in2)
1024 DisasCond cond;
1025 TCGv_reg tmp, cb = NULL;
1027 if (cf & 8) {
1028 /* Since we want to test lots of carry-out bits all at once, do not
1029 * do our normal thing and compute carry-in of bit B+1 since that
1030 * leaves us with carry bits spread across two words.
1032 cb = tcg_temp_new();
1033 tmp = tcg_temp_new();
1034 tcg_gen_or_reg(cb, in1, in2);
1035 tcg_gen_and_reg(tmp, in1, in2);
1036 tcg_gen_andc_reg(cb, cb, res);
1037 tcg_gen_or_reg(cb, cb, tmp);
1038 tcg_temp_free(tmp);
1041 switch (cf >> 1) {
1042 case 0: /* never / TR */
1043 case 1: /* undefined */
1044 case 5: /* undefined */
1045 cond = cond_make_f();
1046 break;
1048 case 2: /* SBZ / NBZ */
1049 /* See hasless(v,1) from
1050 * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
1052 tmp = tcg_temp_new();
1053 tcg_gen_subi_reg(tmp, res, 0x01010101u);
1054 tcg_gen_andc_reg(tmp, tmp, res);
1055 tcg_gen_andi_reg(tmp, tmp, 0x80808080u);
1056 cond = cond_make_0(TCG_COND_NE, tmp);
1057 tcg_temp_free(tmp);
1058 break;
1060 case 3: /* SHZ / NHZ */
1061 tmp = tcg_temp_new();
1062 tcg_gen_subi_reg(tmp, res, 0x00010001u);
1063 tcg_gen_andc_reg(tmp, tmp, res);
1064 tcg_gen_andi_reg(tmp, tmp, 0x80008000u);
1065 cond = cond_make_0(TCG_COND_NE, tmp);
1066 tcg_temp_free(tmp);
1067 break;
1069 case 4: /* SDC / NDC */
1070 tcg_gen_andi_reg(cb, cb, 0x88888888u);
1071 cond = cond_make_0(TCG_COND_NE, cb);
1072 break;
1074 case 6: /* SBC / NBC */
1075 tcg_gen_andi_reg(cb, cb, 0x80808080u);
1076 cond = cond_make_0(TCG_COND_NE, cb);
1077 break;
1079 case 7: /* SHC / NHC */
1080 tcg_gen_andi_reg(cb, cb, 0x80008000u);
1081 cond = cond_make_0(TCG_COND_NE, cb);
1082 break;
1084 default:
1085 g_assert_not_reached();
1087 if (cf & 8) {
1088 tcg_temp_free(cb);
1090 if (cf & 1) {
1091 cond.c = tcg_invert_cond(cond.c);
1094 return cond;
1097 /* Compute signed overflow for addition. */
1098 static TCGv_reg do_add_sv(DisasContext *ctx, TCGv_reg res,
1099 TCGv_reg in1, TCGv_reg in2)
1101 TCGv_reg sv = get_temp(ctx);
1102 TCGv_reg tmp = tcg_temp_new();
1104 tcg_gen_xor_reg(sv, res, in1);
1105 tcg_gen_xor_reg(tmp, in1, in2);
1106 tcg_gen_andc_reg(sv, sv, tmp);
1107 tcg_temp_free(tmp);
1109 return sv;
1112 /* Compute signed overflow for subtraction. */
1113 static TCGv_reg do_sub_sv(DisasContext *ctx, TCGv_reg res,
1114 TCGv_reg in1, TCGv_reg in2)
1116 TCGv_reg sv = get_temp(ctx);
1117 TCGv_reg tmp = tcg_temp_new();
1119 tcg_gen_xor_reg(sv, res, in1);
1120 tcg_gen_xor_reg(tmp, in1, in2);
1121 tcg_gen_and_reg(sv, sv, tmp);
1122 tcg_temp_free(tmp);
1124 return sv;
1127 static DisasJumpType do_add(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1128 TCGv_reg in2, unsigned shift, bool is_l,
1129 bool is_tsv, bool is_tc, bool is_c, unsigned cf)
1131 TCGv_reg dest, cb, cb_msb, sv, tmp;
1132 unsigned c = cf >> 1;
1133 DisasCond cond;
1135 dest = tcg_temp_new();
1136 cb = NULL;
1137 cb_msb = NULL;
1139 if (shift) {
1140 tmp = get_temp(ctx);
1141 tcg_gen_shli_reg(tmp, in1, shift);
1142 in1 = tmp;
1145 if (!is_l || c == 4 || c == 5) {
1146 TCGv_reg zero = tcg_const_reg(0);
1147 cb_msb = get_temp(ctx);
1148 tcg_gen_add2_reg(dest, cb_msb, in1, zero, in2, zero);
1149 if (is_c) {
1150 tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cpu_psw_cb_msb, zero);
1152 tcg_temp_free(zero);
1153 if (!is_l) {
1154 cb = get_temp(ctx);
1155 tcg_gen_xor_reg(cb, in1, in2);
1156 tcg_gen_xor_reg(cb, cb, dest);
1158 } else {
1159 tcg_gen_add_reg(dest, in1, in2);
1160 if (is_c) {
1161 tcg_gen_add_reg(dest, dest, cpu_psw_cb_msb);
1165 /* Compute signed overflow if required. */
1166 sv = NULL;
1167 if (is_tsv || c == 6) {
1168 sv = do_add_sv(ctx, dest, in1, in2);
1169 if (is_tsv) {
1170 /* ??? Need to include overflow from shift. */
1171 gen_helper_tsv(cpu_env, sv);
1175 /* Emit any conditional trap before any writeback. */
1176 cond = do_cond(cf, dest, cb_msb, sv);
1177 if (is_tc) {
1178 cond_prep(&cond);
1179 tmp = tcg_temp_new();
1180 tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1181 gen_helper_tcond(cpu_env, tmp);
1182 tcg_temp_free(tmp);
1185 /* Write back the result. */
1186 if (!is_l) {
1187 save_or_nullify(ctx, cpu_psw_cb, cb);
1188 save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1190 save_gpr(ctx, rt, dest);
1191 tcg_temp_free(dest);
1193 /* Install the new nullification. */
1194 cond_free(&ctx->null_cond);
1195 ctx->null_cond = cond;
1196 return DISAS_NEXT;
1199 static DisasJumpType do_sub(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1200 TCGv_reg in2, bool is_tsv, bool is_b,
1201 bool is_tc, unsigned cf)
1203 TCGv_reg dest, sv, cb, cb_msb, zero, tmp;
1204 unsigned c = cf >> 1;
1205 DisasCond cond;
1207 dest = tcg_temp_new();
1208 cb = tcg_temp_new();
1209 cb_msb = tcg_temp_new();
1211 zero = tcg_const_reg(0);
1212 if (is_b) {
1213 /* DEST,C = IN1 + ~IN2 + C. */
1214 tcg_gen_not_reg(cb, in2);
1215 tcg_gen_add2_reg(dest, cb_msb, in1, zero, cpu_psw_cb_msb, zero);
1216 tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cb, zero);
1217 tcg_gen_xor_reg(cb, cb, in1);
1218 tcg_gen_xor_reg(cb, cb, dest);
1219 } else {
1220 /* DEST,C = IN1 + ~IN2 + 1. We can produce the same result in fewer
1221 operations by seeding the high word with 1 and subtracting. */
1222 tcg_gen_movi_reg(cb_msb, 1);
1223 tcg_gen_sub2_reg(dest, cb_msb, in1, cb_msb, in2, zero);
1224 tcg_gen_eqv_reg(cb, in1, in2);
1225 tcg_gen_xor_reg(cb, cb, dest);
1227 tcg_temp_free(zero);
1229 /* Compute signed overflow if required. */
1230 sv = NULL;
1231 if (is_tsv || c == 6) {
1232 sv = do_sub_sv(ctx, dest, in1, in2);
1233 if (is_tsv) {
1234 gen_helper_tsv(cpu_env, sv);
1238 /* Compute the condition. We cannot use the special case for borrow. */
1239 if (!is_b) {
1240 cond = do_sub_cond(cf, dest, in1, in2, sv);
1241 } else {
1242 cond = do_cond(cf, dest, cb_msb, sv);
1245 /* Emit any conditional trap before any writeback. */
1246 if (is_tc) {
1247 cond_prep(&cond);
1248 tmp = tcg_temp_new();
1249 tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1250 gen_helper_tcond(cpu_env, tmp);
1251 tcg_temp_free(tmp);
1254 /* Write back the result. */
1255 save_or_nullify(ctx, cpu_psw_cb, cb);
1256 save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1257 save_gpr(ctx, rt, dest);
1258 tcg_temp_free(dest);
1260 /* Install the new nullification. */
1261 cond_free(&ctx->null_cond);
1262 ctx->null_cond = cond;
1263 return DISAS_NEXT;
1266 static DisasJumpType do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1267 TCGv_reg in2, unsigned cf)
1269 TCGv_reg dest, sv;
1270 DisasCond cond;
1272 dest = tcg_temp_new();
1273 tcg_gen_sub_reg(dest, in1, in2);
1275 /* Compute signed overflow if required. */
1276 sv = NULL;
1277 if ((cf >> 1) == 6) {
1278 sv = do_sub_sv(ctx, dest, in1, in2);
1281 /* Form the condition for the compare. */
1282 cond = do_sub_cond(cf, dest, in1, in2, sv);
1284 /* Clear. */
1285 tcg_gen_movi_reg(dest, 0);
1286 save_gpr(ctx, rt, dest);
1287 tcg_temp_free(dest);
1289 /* Install the new nullification. */
1290 cond_free(&ctx->null_cond);
1291 ctx->null_cond = cond;
1292 return DISAS_NEXT;
1295 static DisasJumpType do_log(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1296 TCGv_reg in2, unsigned cf,
1297 void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1299 TCGv_reg dest = dest_gpr(ctx, rt);
1301 /* Perform the operation, and writeback. */
1302 fn(dest, in1, in2);
1303 save_gpr(ctx, rt, dest);
1305 /* Install the new nullification. */
1306 cond_free(&ctx->null_cond);
1307 if (cf) {
1308 ctx->null_cond = do_log_cond(cf, dest);
1310 return DISAS_NEXT;
1313 static DisasJumpType do_unit(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1314 TCGv_reg in2, unsigned cf, bool is_tc,
1315 void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1317 TCGv_reg dest;
1318 DisasCond cond;
1320 if (cf == 0) {
1321 dest = dest_gpr(ctx, rt);
1322 fn(dest, in1, in2);
1323 save_gpr(ctx, rt, dest);
1324 cond_free(&ctx->null_cond);
1325 } else {
1326 dest = tcg_temp_new();
1327 fn(dest, in1, in2);
1329 cond = do_unit_cond(cf, dest, in1, in2);
1331 if (is_tc) {
1332 TCGv_reg tmp = tcg_temp_new();
1333 cond_prep(&cond);
1334 tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1335 gen_helper_tcond(cpu_env, tmp);
1336 tcg_temp_free(tmp);
1338 save_gpr(ctx, rt, dest);
1340 cond_free(&ctx->null_cond);
1341 ctx->null_cond = cond;
1343 return DISAS_NEXT;
1346 #ifndef CONFIG_USER_ONLY
1347 /* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
1348 from the top 2 bits of the base register. There are a few system
1349 instructions that have a 3-bit space specifier, for which SR0 is
1350 not special. To handle this, pass ~SP. */
1351 static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_reg base)
1353 TCGv_ptr ptr;
1354 TCGv_reg tmp;
1355 TCGv_i64 spc;
1357 if (sp != 0) {
1358 if (sp < 0) {
1359 sp = ~sp;
1361 spc = get_temp_tl(ctx);
1362 load_spr(ctx, spc, sp);
1363 return spc;
1366 ptr = tcg_temp_new_ptr();
1367 tmp = tcg_temp_new();
1368 spc = get_temp_tl(ctx);
1370 tcg_gen_shri_reg(tmp, base, TARGET_REGISTER_BITS - 5);
1371 tcg_gen_andi_reg(tmp, tmp, 030);
1372 tcg_gen_trunc_reg_ptr(ptr, tmp);
1373 tcg_temp_free(tmp);
1375 tcg_gen_add_ptr(ptr, ptr, cpu_env);
1376 tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
1377 tcg_temp_free_ptr(ptr);
1379 return spc;
1381 #endif
1383 static void form_gva(DisasContext *ctx, TCGv_tl *pgva, TCGv_reg *pofs,
1384 unsigned rb, unsigned rx, int scale, target_sreg disp,
1385 unsigned sp, int modify, bool is_phys)
1387 TCGv_reg base = load_gpr(ctx, rb);
1388 TCGv_reg ofs;
1390 /* Note that RX is mutually exclusive with DISP. */
1391 if (rx) {
1392 ofs = get_temp(ctx);
1393 tcg_gen_shli_reg(ofs, cpu_gr[rx], scale);
1394 tcg_gen_add_reg(ofs, ofs, base);
1395 } else if (disp || modify) {
1396 ofs = get_temp(ctx);
1397 tcg_gen_addi_reg(ofs, base, disp);
1398 } else {
1399 ofs = base;
1402 *pofs = ofs;
1403 #ifdef CONFIG_USER_ONLY
1404 *pgva = (modify <= 0 ? ofs : base);
1405 #else
1406 TCGv_tl addr = get_temp_tl(ctx);
1407 tcg_gen_extu_reg_tl(addr, modify <= 0 ? ofs : base);
1408 if (ctx->base.tb->flags & PSW_W) {
1409 tcg_gen_andi_tl(addr, addr, 0x3fffffffffffffffull);
1411 if (!is_phys) {
1412 tcg_gen_or_tl(addr, addr, space_select(ctx, sp, base));
1414 *pgva = addr;
1415 #endif
1418 /* Emit a memory load. The modify parameter should be
1419 * < 0 for pre-modify,
1420 * > 0 for post-modify,
1421 * = 0 for no base register update.
1423 static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1424 unsigned rx, int scale, target_sreg disp,
1425 unsigned sp, int modify, TCGMemOp mop)
1427 TCGv_reg ofs;
1428 TCGv_tl addr;
1430 /* Caller uses nullify_over/nullify_end. */
1431 assert(ctx->null_cond.c == TCG_COND_NEVER);
1433 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1434 ctx->mmu_idx == MMU_PHYS_IDX);
1435 tcg_gen_qemu_ld_reg(dest, addr, ctx->mmu_idx, mop);
1436 if (modify) {
1437 save_gpr(ctx, rb, ofs);
1441 static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1442 unsigned rx, int scale, target_sreg disp,
1443 unsigned sp, int modify, TCGMemOp mop)
1445 TCGv_reg ofs;
1446 TCGv_tl addr;
1448 /* Caller uses nullify_over/nullify_end. */
1449 assert(ctx->null_cond.c == TCG_COND_NEVER);
1451 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1452 ctx->mmu_idx == MMU_PHYS_IDX);
1453 tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop);
1454 if (modify) {
1455 save_gpr(ctx, rb, ofs);
1459 static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1460 unsigned rx, int scale, target_sreg disp,
1461 unsigned sp, int modify, TCGMemOp mop)
1463 TCGv_reg ofs;
1464 TCGv_tl addr;
1466 /* Caller uses nullify_over/nullify_end. */
1467 assert(ctx->null_cond.c == TCG_COND_NEVER);
1469 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1470 ctx->mmu_idx == MMU_PHYS_IDX);
1471 tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop);
1472 if (modify) {
1473 save_gpr(ctx, rb, ofs);
1477 static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1478 unsigned rx, int scale, target_sreg disp,
1479 unsigned sp, int modify, TCGMemOp mop)
1481 TCGv_reg ofs;
1482 TCGv_tl addr;
1484 /* Caller uses nullify_over/nullify_end. */
1485 assert(ctx->null_cond.c == TCG_COND_NEVER);
1487 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
1488 ctx->mmu_idx == MMU_PHYS_IDX);
1489 tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop);
1490 if (modify) {
1491 save_gpr(ctx, rb, ofs);
1495 #if TARGET_REGISTER_BITS == 64
1496 #define do_load_reg do_load_64
1497 #define do_store_reg do_store_64
1498 #else
1499 #define do_load_reg do_load_32
1500 #define do_store_reg do_store_32
1501 #endif
1503 static DisasJumpType do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1504 unsigned rx, int scale, target_sreg disp,
1505 unsigned sp, int modify, TCGMemOp mop)
1507 TCGv_reg dest;
1509 nullify_over(ctx);
1511 if (modify == 0) {
1512 /* No base register update. */
1513 dest = dest_gpr(ctx, rt);
1514 } else {
1515 /* Make sure if RT == RB, we see the result of the load. */
1516 dest = get_temp(ctx);
1518 do_load_reg(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
1519 save_gpr(ctx, rt, dest);
1521 return nullify_end(ctx, DISAS_NEXT);
1524 static DisasJumpType do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1525 unsigned rx, int scale, target_sreg disp,
1526 unsigned sp, int modify)
1528 TCGv_i32 tmp;
1530 nullify_over(ctx);
1532 tmp = tcg_temp_new_i32();
1533 do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1534 save_frw_i32(rt, tmp);
1535 tcg_temp_free_i32(tmp);
1537 if (rt == 0) {
1538 gen_helper_loaded_fr0(cpu_env);
1541 return nullify_end(ctx, DISAS_NEXT);
1544 static DisasJumpType do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1545 unsigned rx, int scale, target_sreg disp,
1546 unsigned sp, int modify)
1548 TCGv_i64 tmp;
1550 nullify_over(ctx);
1552 tmp = tcg_temp_new_i64();
1553 do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEQ);
1554 save_frd(rt, tmp);
1555 tcg_temp_free_i64(tmp);
1557 if (rt == 0) {
1558 gen_helper_loaded_fr0(cpu_env);
1561 return nullify_end(ctx, DISAS_NEXT);
1564 static DisasJumpType do_store(DisasContext *ctx, unsigned rt, unsigned rb,
1565 target_sreg disp, unsigned sp,
1566 int modify, TCGMemOp mop)
1568 nullify_over(ctx);
1569 do_store_reg(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
1570 return nullify_end(ctx, DISAS_NEXT);
1573 static DisasJumpType do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1574 unsigned rx, int scale, target_sreg disp,
1575 unsigned sp, int modify)
1577 TCGv_i32 tmp;
1579 nullify_over(ctx);
1581 tmp = load_frw_i32(rt);
1582 do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
1583 tcg_temp_free_i32(tmp);
1585 return nullify_end(ctx, DISAS_NEXT);
1588 static DisasJumpType do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1589 unsigned rx, int scale, target_sreg disp,
1590 unsigned sp, int modify)
1592 TCGv_i64 tmp;
1594 nullify_over(ctx);
1596 tmp = load_frd(rt);
1597 do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEQ);
1598 tcg_temp_free_i64(tmp);
1600 return nullify_end(ctx, DISAS_NEXT);
1603 static DisasJumpType do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
1604 void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1606 TCGv_i32 tmp;
1608 nullify_over(ctx);
1609 tmp = load_frw0_i32(ra);
1611 func(tmp, cpu_env, tmp);
1613 save_frw_i32(rt, tmp);
1614 tcg_temp_free_i32(tmp);
1615 return nullify_end(ctx, DISAS_NEXT);
1618 static DisasJumpType do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
1619 void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1621 TCGv_i32 dst;
1622 TCGv_i64 src;
1624 nullify_over(ctx);
1625 src = load_frd(ra);
1626 dst = tcg_temp_new_i32();
1628 func(dst, cpu_env, src);
1630 tcg_temp_free_i64(src);
1631 save_frw_i32(rt, dst);
1632 tcg_temp_free_i32(dst);
1633 return nullify_end(ctx, DISAS_NEXT);
1636 static DisasJumpType do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
1637 void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1639 TCGv_i64 tmp;
1641 nullify_over(ctx);
1642 tmp = load_frd0(ra);
1644 func(tmp, cpu_env, tmp);
1646 save_frd(rt, tmp);
1647 tcg_temp_free_i64(tmp);
1648 return nullify_end(ctx, DISAS_NEXT);
1651 static DisasJumpType do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
1652 void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1654 TCGv_i32 src;
1655 TCGv_i64 dst;
1657 nullify_over(ctx);
1658 src = load_frw0_i32(ra);
1659 dst = tcg_temp_new_i64();
1661 func(dst, cpu_env, src);
1663 tcg_temp_free_i32(src);
1664 save_frd(rt, dst);
1665 tcg_temp_free_i64(dst);
1666 return nullify_end(ctx, DISAS_NEXT);
1669 static DisasJumpType do_fop_weww(DisasContext *ctx, unsigned rt,
1670 unsigned ra, unsigned rb,
1671 void (*func)(TCGv_i32, TCGv_env,
1672 TCGv_i32, TCGv_i32))
1674 TCGv_i32 a, b;
1676 nullify_over(ctx);
1677 a = load_frw0_i32(ra);
1678 b = load_frw0_i32(rb);
1680 func(a, cpu_env, a, b);
1682 tcg_temp_free_i32(b);
1683 save_frw_i32(rt, a);
1684 tcg_temp_free_i32(a);
1685 return nullify_end(ctx, DISAS_NEXT);
1688 static DisasJumpType do_fop_dedd(DisasContext *ctx, unsigned rt,
1689 unsigned ra, unsigned rb,
1690 void (*func)(TCGv_i64, TCGv_env,
1691 TCGv_i64, TCGv_i64))
1693 TCGv_i64 a, b;
1695 nullify_over(ctx);
1696 a = load_frd0(ra);
1697 b = load_frd0(rb);
1699 func(a, cpu_env, a, b);
1701 tcg_temp_free_i64(b);
1702 save_frd(rt, a);
1703 tcg_temp_free_i64(a);
1704 return nullify_end(ctx, DISAS_NEXT);
1707 /* Emit an unconditional branch to a direct target, which may or may not
1708 have already had nullification handled. */
1709 static DisasJumpType do_dbranch(DisasContext *ctx, target_ureg dest,
1710 unsigned link, bool is_n)
1712 if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
1713 if (link != 0) {
1714 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1716 ctx->iaoq_n = dest;
1717 if (is_n) {
1718 ctx->null_cond.c = TCG_COND_ALWAYS;
1720 return DISAS_NEXT;
1721 } else {
1722 nullify_over(ctx);
1724 if (link != 0) {
1725 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1728 if (is_n && use_nullify_skip(ctx)) {
1729 nullify_set(ctx, 0);
1730 gen_goto_tb(ctx, 0, dest, dest + 4);
1731 } else {
1732 nullify_set(ctx, is_n);
1733 gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
1736 nullify_end(ctx, DISAS_NEXT);
1738 nullify_set(ctx, 0);
1739 gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
1740 return DISAS_NORETURN;
1744 /* Emit a conditional branch to a direct target. If the branch itself
1745 is nullified, we should have already used nullify_over. */
1746 static DisasJumpType do_cbranch(DisasContext *ctx, target_sreg disp, bool is_n,
1747 DisasCond *cond)
1749 target_ureg dest = iaoq_dest(ctx, disp);
1750 TCGLabel *taken = NULL;
1751 TCGCond c = cond->c;
1752 bool n;
1754 assert(ctx->null_cond.c == TCG_COND_NEVER);
1756 /* Handle TRUE and NEVER as direct branches. */
1757 if (c == TCG_COND_ALWAYS) {
1758 return do_dbranch(ctx, dest, 0, is_n && disp >= 0);
1760 if (c == TCG_COND_NEVER) {
1761 return do_dbranch(ctx, ctx->iaoq_n, 0, is_n && disp < 0);
1764 taken = gen_new_label();
1765 cond_prep(cond);
1766 tcg_gen_brcond_reg(c, cond->a0, cond->a1, taken);
1767 cond_free(cond);
1769 /* Not taken: Condition not satisfied; nullify on backward branches. */
1770 n = is_n && disp < 0;
1771 if (n && use_nullify_skip(ctx)) {
1772 nullify_set(ctx, 0);
1773 gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
1774 } else {
1775 if (!n && ctx->null_lab) {
1776 gen_set_label(ctx->null_lab);
1777 ctx->null_lab = NULL;
1779 nullify_set(ctx, n);
1780 if (ctx->iaoq_n == -1) {
1781 /* The temporary iaoq_n_var died at the branch above.
1782 Regenerate it here instead of saving it. */
1783 tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
1785 gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
1788 gen_set_label(taken);
1790 /* Taken: Condition satisfied; nullify on forward branches. */
1791 n = is_n && disp >= 0;
1792 if (n && use_nullify_skip(ctx)) {
1793 nullify_set(ctx, 0);
1794 gen_goto_tb(ctx, 1, dest, dest + 4);
1795 } else {
1796 nullify_set(ctx, n);
1797 gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
1800 /* Not taken: the branch itself was nullified. */
1801 if (ctx->null_lab) {
1802 gen_set_label(ctx->null_lab);
1803 ctx->null_lab = NULL;
1804 return DISAS_IAQ_N_STALE;
1805 } else {
1806 return DISAS_NORETURN;
1810 /* Emit an unconditional branch to an indirect target. This handles
1811 nullification of the branch itself. */
1812 static DisasJumpType do_ibranch(DisasContext *ctx, TCGv_reg dest,
1813 unsigned link, bool is_n)
1815 TCGv_reg a0, a1, next, tmp;
1816 TCGCond c;
1818 assert(ctx->null_lab == NULL);
1820 if (ctx->null_cond.c == TCG_COND_NEVER) {
1821 if (link != 0) {
1822 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
1824 next = get_temp(ctx);
1825 tcg_gen_mov_reg(next, dest);
1826 if (is_n) {
1827 if (use_nullify_skip(ctx)) {
1828 tcg_gen_mov_reg(cpu_iaoq_f, next);
1829 tcg_gen_addi_reg(cpu_iaoq_b, next, 4);
1830 nullify_set(ctx, 0);
1831 return DISAS_IAQ_N_UPDATED;
1833 ctx->null_cond.c = TCG_COND_ALWAYS;
1835 ctx->iaoq_n = -1;
1836 ctx->iaoq_n_var = next;
1837 } else if (is_n && use_nullify_skip(ctx)) {
1838 /* The (conditional) branch, B, nullifies the next insn, N,
1839 and we're allowed to skip execution N (no single-step or
1840 tracepoint in effect). Since the goto_ptr that we must use
1841 for the indirect branch consumes no special resources, we
1842 can (conditionally) skip B and continue execution. */
1843 /* The use_nullify_skip test implies we have a known control path. */
1844 tcg_debug_assert(ctx->iaoq_b != -1);
1845 tcg_debug_assert(ctx->iaoq_n != -1);
1847 /* We do have to handle the non-local temporary, DEST, before
1848 branching. Since IOAQ_F is not really live at this point, we
1849 can simply store DEST optimistically. Similarly with IAOQ_B. */
1850 tcg_gen_mov_reg(cpu_iaoq_f, dest);
1851 tcg_gen_addi_reg(cpu_iaoq_b, dest, 4);
1853 nullify_over(ctx);
1854 if (link != 0) {
1855 tcg_gen_movi_reg(cpu_gr[link], ctx->iaoq_n);
1857 tcg_gen_lookup_and_goto_ptr();
1858 return nullify_end(ctx, DISAS_NEXT);
1859 } else {
1860 cond_prep(&ctx->null_cond);
1861 c = ctx->null_cond.c;
1862 a0 = ctx->null_cond.a0;
1863 a1 = ctx->null_cond.a1;
1865 tmp = tcg_temp_new();
1866 next = get_temp(ctx);
1868 copy_iaoq_entry(tmp, ctx->iaoq_n, ctx->iaoq_n_var);
1869 tcg_gen_movcond_reg(c, next, a0, a1, tmp, dest);
1870 ctx->iaoq_n = -1;
1871 ctx->iaoq_n_var = next;
1873 if (link != 0) {
1874 tcg_gen_movcond_reg(c, cpu_gr[link], a0, a1, cpu_gr[link], tmp);
1877 if (is_n) {
1878 /* The branch nullifies the next insn, which means the state of N
1879 after the branch is the inverse of the state of N that applied
1880 to the branch. */
1881 tcg_gen_setcond_reg(tcg_invert_cond(c), cpu_psw_n, a0, a1);
1882 cond_free(&ctx->null_cond);
1883 ctx->null_cond = cond_make_n();
1884 ctx->psw_n_nonzero = true;
1885 } else {
1886 cond_free(&ctx->null_cond);
1890 return DISAS_NEXT;
1893 /* Implement
1894 * if (IAOQ_Front{30..31} < GR[b]{30..31})
1895 * IAOQ_Next{30..31} ← GR[b]{30..31};
1896 * else
1897 * IAOQ_Next{30..31} ← IAOQ_Front{30..31};
1898 * which keeps the privilege level from being increased.
1900 static TCGv_reg do_ibranch_priv(DisasContext *ctx, TCGv_reg offset)
1902 #ifdef CONFIG_USER_ONLY
1903 return offset;
1904 #else
1905 TCGv_reg dest;
1906 switch (ctx->privilege) {
1907 case 0:
1908 /* Privilege 0 is maximum and is allowed to decrease. */
1909 return offset;
1910 case 3:
1911 /* Privilege 3 is minimum and is never allowed increase. */
1912 dest = get_temp(ctx);
1913 tcg_gen_ori_reg(dest, offset, 3);
1914 break;
1915 default:
1916 dest = tcg_temp_new();
1917 tcg_gen_andi_reg(dest, offset, -4);
1918 tcg_gen_ori_reg(dest, dest, ctx->privilege);
1919 tcg_gen_movcond_reg(TCG_COND_GTU, dest, dest, offset, dest, offset);
1920 tcg_temp_free(dest);
1921 break;
1923 return dest;
1924 #endif
1927 #ifdef CONFIG_USER_ONLY
1928 /* On Linux, page zero is normally marked execute only + gateway.
1929 Therefore normal read or write is supposed to fail, but specific
1930 offsets have kernel code mapped to raise permissions to implement
1931 system calls. Handling this via an explicit check here, rather
1932 in than the "be disp(sr2,r0)" instruction that probably sent us
1933 here, is the easiest way to handle the branch delay slot on the
1934 aforementioned BE. */
1935 static DisasJumpType do_page_zero(DisasContext *ctx)
1937 /* If by some means we get here with PSW[N]=1, that implies that
1938 the B,GATE instruction would be skipped, and we'd fault on the
1939 next insn within the privilaged page. */
1940 switch (ctx->null_cond.c) {
1941 case TCG_COND_NEVER:
1942 break;
1943 case TCG_COND_ALWAYS:
1944 tcg_gen_movi_reg(cpu_psw_n, 0);
1945 goto do_sigill;
1946 default:
1947 /* Since this is always the first (and only) insn within the
1948 TB, we should know the state of PSW[N] from TB->FLAGS. */
1949 g_assert_not_reached();
1952 /* Check that we didn't arrive here via some means that allowed
1953 non-sequential instruction execution. Normally the PSW[B] bit
1954 detects this by disallowing the B,GATE instruction to execute
1955 under such conditions. */
1956 if (ctx->iaoq_b != ctx->iaoq_f + 4) {
1957 goto do_sigill;
1960 switch (ctx->iaoq_f) {
1961 case 0x00: /* Null pointer call */
1962 gen_excp_1(EXCP_IMP);
1963 return DISAS_NORETURN;
1965 case 0xb0: /* LWS */
1966 gen_excp_1(EXCP_SYSCALL_LWS);
1967 return DISAS_NORETURN;
1969 case 0xe0: /* SET_THREAD_POINTER */
1970 tcg_gen_st_reg(cpu_gr[26], cpu_env, offsetof(CPUHPPAState, cr[27]));
1971 tcg_gen_mov_reg(cpu_iaoq_f, cpu_gr[31]);
1972 tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
1973 return DISAS_IAQ_N_UPDATED;
1975 case 0x100: /* SYSCALL */
1976 gen_excp_1(EXCP_SYSCALL);
1977 return DISAS_NORETURN;
1979 default:
1980 do_sigill:
1981 gen_excp_1(EXCP_ILL);
1982 return DISAS_NORETURN;
1985 #endif
1987 static DisasJumpType trans_nop(DisasContext *ctx, uint32_t insn,
1988 const DisasInsn *di)
1990 cond_free(&ctx->null_cond);
1991 return DISAS_NEXT;
1994 static DisasJumpType trans_break(DisasContext *ctx, uint32_t insn,
1995 const DisasInsn *di)
1997 nullify_over(ctx);
1998 return nullify_end(ctx, gen_excp_iir(ctx, EXCP_BREAK));
2001 static DisasJumpType trans_sync(DisasContext *ctx, uint32_t insn,
2002 const DisasInsn *di)
2004 /* No point in nullifying the memory barrier. */
2005 tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
2007 cond_free(&ctx->null_cond);
2008 return DISAS_NEXT;
2011 static DisasJumpType trans_mfia(DisasContext *ctx, uint32_t insn,
2012 const DisasInsn *di)
2014 unsigned rt = extract32(insn, 0, 5);
2015 TCGv_reg tmp = dest_gpr(ctx, rt);
2016 tcg_gen_movi_reg(tmp, ctx->iaoq_f);
2017 save_gpr(ctx, rt, tmp);
2019 cond_free(&ctx->null_cond);
2020 return DISAS_NEXT;
2023 static DisasJumpType trans_mfsp(DisasContext *ctx, uint32_t insn,
2024 const DisasInsn *di)
2026 unsigned rt = extract32(insn, 0, 5);
2027 unsigned rs = assemble_sr3(insn);
2028 TCGv_i64 t0 = tcg_temp_new_i64();
2029 TCGv_reg t1 = tcg_temp_new();
2031 load_spr(ctx, t0, rs);
2032 tcg_gen_shri_i64(t0, t0, 32);
2033 tcg_gen_trunc_i64_reg(t1, t0);
2035 save_gpr(ctx, rt, t1);
2036 tcg_temp_free(t1);
2037 tcg_temp_free_i64(t0);
2039 cond_free(&ctx->null_cond);
2040 return DISAS_NEXT;
2043 static DisasJumpType trans_mfctl(DisasContext *ctx, uint32_t insn,
2044 const DisasInsn *di)
2046 unsigned rt = extract32(insn, 0, 5);
2047 unsigned ctl = extract32(insn, 21, 5);
2048 TCGv_reg tmp;
2049 DisasJumpType ret;
2051 switch (ctl) {
2052 case CR_SAR:
2053 #ifdef TARGET_HPPA64
2054 if (extract32(insn, 14, 1) == 0) {
2055 /* MFSAR without ,W masks low 5 bits. */
2056 tmp = dest_gpr(ctx, rt);
2057 tcg_gen_andi_reg(tmp, cpu_sar, 31);
2058 save_gpr(ctx, rt, tmp);
2059 goto done;
2061 #endif
2062 save_gpr(ctx, rt, cpu_sar);
2063 goto done;
2064 case CR_IT: /* Interval Timer */
2065 /* FIXME: Respect PSW_S bit. */
2066 nullify_over(ctx);
2067 tmp = dest_gpr(ctx, rt);
2068 if (ctx->base.tb->cflags & CF_USE_ICOUNT) {
2069 gen_io_start();
2070 gen_helper_read_interval_timer(tmp);
2071 gen_io_end();
2072 ret = DISAS_IAQ_N_STALE;
2073 } else {
2074 gen_helper_read_interval_timer(tmp);
2075 ret = DISAS_NEXT;
2077 save_gpr(ctx, rt, tmp);
2078 return nullify_end(ctx, ret);
2079 case 26:
2080 case 27:
2081 break;
2082 default:
2083 /* All other control registers are privileged. */
2084 CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2085 break;
2088 tmp = get_temp(ctx);
2089 tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2090 save_gpr(ctx, rt, tmp);
2092 done:
2093 cond_free(&ctx->null_cond);
2094 return DISAS_NEXT;
2097 static DisasJumpType trans_mtsp(DisasContext *ctx, uint32_t insn,
2098 const DisasInsn *di)
2100 unsigned rr = extract32(insn, 16, 5);
2101 unsigned rs = assemble_sr3(insn);
2102 TCGv_i64 t64;
2104 if (rs >= 5) {
2105 CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2107 nullify_over(ctx);
2109 t64 = tcg_temp_new_i64();
2110 tcg_gen_extu_reg_i64(t64, load_gpr(ctx, rr));
2111 tcg_gen_shli_i64(t64, t64, 32);
2113 if (rs >= 4) {
2114 tcg_gen_st_i64(t64, cpu_env, offsetof(CPUHPPAState, sr[rs]));
2115 } else {
2116 tcg_gen_mov_i64(cpu_sr[rs], t64);
2118 tcg_temp_free_i64(t64);
2120 return nullify_end(ctx, DISAS_NEXT);
2123 static DisasJumpType trans_mtctl(DisasContext *ctx, uint32_t insn,
2124 const DisasInsn *di)
2126 unsigned rin = extract32(insn, 16, 5);
2127 unsigned ctl = extract32(insn, 21, 5);
2128 TCGv_reg reg = load_gpr(ctx, rin);
2129 TCGv_reg tmp;
2131 if (ctl == CR_SAR) {
2132 tmp = tcg_temp_new();
2133 tcg_gen_andi_reg(tmp, reg, TARGET_REGISTER_BITS - 1);
2134 save_or_nullify(ctx, cpu_sar, tmp);
2135 tcg_temp_free(tmp);
2137 cond_free(&ctx->null_cond);
2138 return DISAS_NEXT;
2141 /* All other control registers are privileged or read-only. */
2142 CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
2144 #ifdef CONFIG_USER_ONLY
2145 g_assert_not_reached();
2146 #else
2147 DisasJumpType ret = DISAS_NEXT;
2149 nullify_over(ctx);
2150 switch (ctl) {
2151 case CR_IT:
2152 gen_helper_write_interval_timer(cpu_env, reg);
2153 break;
2154 case CR_EIRR:
2155 gen_helper_write_eirr(cpu_env, reg);
2156 break;
2157 case CR_EIEM:
2158 gen_helper_write_eiem(cpu_env, reg);
2159 ret = DISAS_IAQ_N_STALE_EXIT;
2160 break;
2162 case CR_IIASQ:
2163 case CR_IIAOQ:
2164 /* FIXME: Respect PSW_Q bit */
2165 /* The write advances the queue and stores to the back element. */
2166 tmp = get_temp(ctx);
2167 tcg_gen_ld_reg(tmp, cpu_env,
2168 offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
2169 tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2170 tcg_gen_st_reg(reg, cpu_env,
2171 offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
2172 break;
2174 default:
2175 tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2176 break;
2178 return nullify_end(ctx, ret);
2179 #endif
2182 static DisasJumpType trans_mtsarcm(DisasContext *ctx, uint32_t insn,
2183 const DisasInsn *di)
2185 unsigned rin = extract32(insn, 16, 5);
2186 TCGv_reg tmp = tcg_temp_new();
2188 tcg_gen_not_reg(tmp, load_gpr(ctx, rin));
2189 tcg_gen_andi_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
2190 save_or_nullify(ctx, cpu_sar, tmp);
2191 tcg_temp_free(tmp);
2193 cond_free(&ctx->null_cond);
2194 return DISAS_NEXT;
2197 static DisasJumpType trans_ldsid(DisasContext *ctx, uint32_t insn,
2198 const DisasInsn *di)
2200 unsigned rt = extract32(insn, 0, 5);
2201 TCGv_reg dest = dest_gpr(ctx, rt);
2203 /* Since we don't implement space registers, this returns zero. */
2204 tcg_gen_movi_reg(dest, 0);
2205 save_gpr(ctx, rt, dest);
2207 cond_free(&ctx->null_cond);
2208 return DISAS_NEXT;
2211 #ifndef CONFIG_USER_ONLY
2212 /* Note that ssm/rsm instructions number PSW_W and PSW_E differently. */
2213 static target_ureg extract_sm_imm(uint32_t insn)
2215 target_ureg val = extract32(insn, 16, 10);
2217 if (val & PSW_SM_E) {
2218 val = (val & ~PSW_SM_E) | PSW_E;
2220 if (val & PSW_SM_W) {
2221 val = (val & ~PSW_SM_W) | PSW_W;
2223 return val;
2226 static DisasJumpType trans_rsm(DisasContext *ctx, uint32_t insn,
2227 const DisasInsn *di)
2229 unsigned rt = extract32(insn, 0, 5);
2230 target_ureg sm = extract_sm_imm(insn);
2231 TCGv_reg tmp;
2233 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2234 nullify_over(ctx);
2236 tmp = get_temp(ctx);
2237 tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2238 tcg_gen_andi_reg(tmp, tmp, ~sm);
2239 gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2240 save_gpr(ctx, rt, tmp);
2242 /* Exit the TB to recognize new interrupts, e.g. PSW_M. */
2243 return nullify_end(ctx, DISAS_IAQ_N_STALE_EXIT);
2246 static DisasJumpType trans_ssm(DisasContext *ctx, uint32_t insn,
2247 const DisasInsn *di)
2249 unsigned rt = extract32(insn, 0, 5);
2250 target_ureg sm = extract_sm_imm(insn);
2251 TCGv_reg tmp;
2253 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2254 nullify_over(ctx);
2256 tmp = get_temp(ctx);
2257 tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2258 tcg_gen_ori_reg(tmp, tmp, sm);
2259 gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2260 save_gpr(ctx, rt, tmp);
2262 /* Exit the TB to recognize new interrupts, e.g. PSW_I. */
2263 return nullify_end(ctx, DISAS_IAQ_N_STALE_EXIT);
2266 static DisasJumpType trans_mtsm(DisasContext *ctx, uint32_t insn,
2267 const DisasInsn *di)
2269 unsigned rr = extract32(insn, 16, 5);
2270 TCGv_reg tmp, reg;
2272 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2273 nullify_over(ctx);
2275 reg = load_gpr(ctx, rr);
2276 tmp = get_temp(ctx);
2277 gen_helper_swap_system_mask(tmp, cpu_env, reg);
2279 /* Exit the TB to recognize new interrupts. */
2280 return nullify_end(ctx, DISAS_IAQ_N_STALE_EXIT);
2283 static DisasJumpType trans_rfi(DisasContext *ctx, uint32_t insn,
2284 const DisasInsn *di)
2286 unsigned comp = extract32(insn, 5, 4);
2288 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2289 nullify_over(ctx);
2291 if (comp == 5) {
2292 gen_helper_rfi_r(cpu_env);
2293 } else {
2294 gen_helper_rfi(cpu_env);
2296 if (ctx->base.singlestep_enabled) {
2297 gen_excp_1(EXCP_DEBUG);
2298 } else {
2299 tcg_gen_exit_tb(0);
2302 /* Exit the TB to recognize new interrupts. */
2303 return nullify_end(ctx, DISAS_NORETURN);
2305 #endif /* !CONFIG_USER_ONLY */
2307 static const DisasInsn table_system[] = {
2308 { 0x00000000u, 0xfc001fe0u, trans_break },
2309 { 0x00001820u, 0xffe01fffu, trans_mtsp },
2310 { 0x00001840u, 0xfc00ffffu, trans_mtctl },
2311 { 0x016018c0u, 0xffe0ffffu, trans_mtsarcm },
2312 { 0x000014a0u, 0xffffffe0u, trans_mfia },
2313 { 0x000004a0u, 0xffff1fe0u, trans_mfsp },
2314 { 0x000008a0u, 0xfc1fbfe0u, trans_mfctl },
2315 { 0x00000400u, 0xffffffffu, trans_sync },
2316 { 0x000010a0u, 0xfc1f3fe0u, trans_ldsid },
2317 #ifndef CONFIG_USER_ONLY
2318 { 0x00000e60u, 0xfc00ffe0u, trans_rsm },
2319 { 0x00000d60u, 0xfc00ffe0u, trans_ssm },
2320 { 0x00001860u, 0xffe0ffffu, trans_mtsm },
2321 { 0x00000c00u, 0xfffffe1fu, trans_rfi },
2322 #endif
2325 static DisasJumpType trans_base_idx_mod(DisasContext *ctx, uint32_t insn,
2326 const DisasInsn *di)
2328 unsigned rb = extract32(insn, 21, 5);
2329 unsigned rx = extract32(insn, 16, 5);
2330 TCGv_reg dest = dest_gpr(ctx, rb);
2331 TCGv_reg src1 = load_gpr(ctx, rb);
2332 TCGv_reg src2 = load_gpr(ctx, rx);
2334 /* The only thing we need to do is the base register modification. */
2335 tcg_gen_add_reg(dest, src1, src2);
2336 save_gpr(ctx, rb, dest);
2338 cond_free(&ctx->null_cond);
2339 return DISAS_NEXT;
2342 static DisasJumpType trans_probe(DisasContext *ctx, uint32_t insn,
2343 const DisasInsn *di)
2345 unsigned rt = extract32(insn, 0, 5);
2346 unsigned sp = extract32(insn, 14, 2);
2347 unsigned rb = extract32(insn, 21, 5);
2348 unsigned is_write = extract32(insn, 6, 1);
2349 TCGv_reg dest, ofs;
2350 TCGv_tl addr;
2352 nullify_over(ctx);
2354 /* ??? Do something with priv level operand. */
2355 dest = dest_gpr(ctx, rt);
2356 form_gva(ctx, &addr, &ofs, rb, 0, 0, 0, sp, 0, false);
2357 if (is_write) {
2358 gen_helper_probe_w(dest, addr);
2359 } else {
2360 gen_helper_probe_r(dest, addr);
2362 save_gpr(ctx, rt, dest);
2363 return nullify_end(ctx, DISAS_NEXT);
2366 #ifndef CONFIG_USER_ONLY
2367 static DisasJumpType trans_ixtlbx(DisasContext *ctx, uint32_t insn,
2368 const DisasInsn *di)
2370 unsigned sp;
2371 unsigned rr = extract32(insn, 16, 5);
2372 unsigned rb = extract32(insn, 21, 5);
2373 unsigned is_data = insn & 0x1000;
2374 unsigned is_addr = insn & 0x40;
2375 TCGv_tl addr;
2376 TCGv_reg ofs, reg;
2378 if (is_data) {
2379 sp = extract32(insn, 14, 2);
2380 } else {
2381 sp = ~assemble_sr3(insn);
2384 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2385 nullify_over(ctx);
2387 form_gva(ctx, &addr, &ofs, rb, 0, 0, 0, sp, 0, false);
2388 reg = load_gpr(ctx, rr);
2389 if (is_addr) {
2390 gen_helper_itlba(cpu_env, addr, reg);
2391 } else {
2392 gen_helper_itlbp(cpu_env, addr, reg);
2395 /* Exit TB for ITLB change if mmu is enabled. This *should* not be
2396 the case, since the OS TLB fill handler runs with mmu disabled. */
2397 return nullify_end(ctx, !is_data && (ctx->base.tb->flags & PSW_C)
2398 ? DISAS_IAQ_N_STALE : DISAS_NEXT);
2401 static DisasJumpType trans_pxtlbx(DisasContext *ctx, uint32_t insn,
2402 const DisasInsn *di)
2404 unsigned m = extract32(insn, 5, 1);
2405 unsigned sp;
2406 unsigned rx = extract32(insn, 16, 5);
2407 unsigned rb = extract32(insn, 21, 5);
2408 unsigned is_data = insn & 0x1000;
2409 unsigned is_local = insn & 0x40;
2410 TCGv_tl addr;
2411 TCGv_reg ofs;
2413 if (is_data) {
2414 sp = extract32(insn, 14, 2);
2415 } else {
2416 sp = ~assemble_sr3(insn);
2419 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2420 nullify_over(ctx);
2422 form_gva(ctx, &addr, &ofs, rb, rx, 0, 0, sp, m, false);
2423 if (m) {
2424 save_gpr(ctx, rb, ofs);
2426 if (is_local) {
2427 gen_helper_ptlbe(cpu_env);
2428 } else {
2429 gen_helper_ptlb(cpu_env, addr);
2432 /* Exit TB for TLB change if mmu is enabled. */
2433 return nullify_end(ctx, !is_data && (ctx->base.tb->flags & PSW_C)
2434 ? DISAS_IAQ_N_STALE : DISAS_NEXT);
2436 #endif /* !CONFIG_USER_ONLY */
2438 static const DisasInsn table_mem_mgmt[] = {
2439 { 0x04003280u, 0xfc003fffu, trans_nop }, /* fdc, disp */
2440 { 0x04001280u, 0xfc003fffu, trans_nop }, /* fdc, index */
2441 { 0x040012a0u, 0xfc003fffu, trans_base_idx_mod }, /* fdc, index, base mod */
2442 { 0x040012c0u, 0xfc003fffu, trans_nop }, /* fdce */
2443 { 0x040012e0u, 0xfc003fffu, trans_base_idx_mod }, /* fdce, base mod */
2444 { 0x04000280u, 0xfc001fffu, trans_nop }, /* fic 0a */
2445 { 0x040002a0u, 0xfc001fffu, trans_base_idx_mod }, /* fic 0a, base mod */
2446 { 0x040013c0u, 0xfc003fffu, trans_nop }, /* fic 4f */
2447 { 0x040013e0u, 0xfc003fffu, trans_base_idx_mod }, /* fic 4f, base mod */
2448 { 0x040002c0u, 0xfc001fffu, trans_nop }, /* fice */
2449 { 0x040002e0u, 0xfc001fffu, trans_base_idx_mod }, /* fice, base mod */
2450 { 0x04002700u, 0xfc003fffu, trans_nop }, /* pdc */
2451 { 0x04002720u, 0xfc003fffu, trans_base_idx_mod }, /* pdc, base mod */
2452 { 0x04001180u, 0xfc003fa0u, trans_probe }, /* probe */
2453 { 0x04003180u, 0xfc003fa0u, trans_probe }, /* probei */
2454 #ifndef CONFIG_USER_ONLY
2455 { 0x04000000u, 0xfc001fffu, trans_ixtlbx }, /* iitlbp */
2456 { 0x04000040u, 0xfc001fffu, trans_ixtlbx }, /* iitlba */
2457 { 0x04001000u, 0xfc001fffu, trans_ixtlbx }, /* idtlbp */
2458 { 0x04001040u, 0xfc001fffu, trans_ixtlbx }, /* idtlba */
2459 { 0x04000200u, 0xfc001fdfu, trans_pxtlbx }, /* pitlb */
2460 { 0x04000240u, 0xfc001fdfu, trans_pxtlbx }, /* pitlbe */
2461 { 0x04001200u, 0xfc001fdfu, trans_pxtlbx }, /* pdtlb */
2462 { 0x04001240u, 0xfc001fdfu, trans_pxtlbx }, /* pdtlbe */
2463 #endif
2466 static DisasJumpType trans_add(DisasContext *ctx, uint32_t insn,
2467 const DisasInsn *di)
2469 unsigned r2 = extract32(insn, 21, 5);
2470 unsigned r1 = extract32(insn, 16, 5);
2471 unsigned cf = extract32(insn, 12, 4);
2472 unsigned ext = extract32(insn, 8, 4);
2473 unsigned shift = extract32(insn, 6, 2);
2474 unsigned rt = extract32(insn, 0, 5);
2475 TCGv_reg tcg_r1, tcg_r2;
2476 bool is_c = false;
2477 bool is_l = false;
2478 bool is_tc = false;
2479 bool is_tsv = false;
2480 DisasJumpType ret;
2482 switch (ext) {
2483 case 0x6: /* ADD, SHLADD */
2484 break;
2485 case 0xa: /* ADD,L, SHLADD,L */
2486 is_l = true;
2487 break;
2488 case 0xe: /* ADD,TSV, SHLADD,TSV (1) */
2489 is_tsv = true;
2490 break;
2491 case 0x7: /* ADD,C */
2492 is_c = true;
2493 break;
2494 case 0xf: /* ADD,C,TSV */
2495 is_c = is_tsv = true;
2496 break;
2497 default:
2498 return gen_illegal(ctx);
2501 if (cf) {
2502 nullify_over(ctx);
2504 tcg_r1 = load_gpr(ctx, r1);
2505 tcg_r2 = load_gpr(ctx, r2);
2506 ret = do_add(ctx, rt, tcg_r1, tcg_r2, shift, is_l, is_tsv, is_tc, is_c, cf);
2507 return nullify_end(ctx, ret);
2510 static DisasJumpType trans_sub(DisasContext *ctx, uint32_t insn,
2511 const DisasInsn *di)
2513 unsigned r2 = extract32(insn, 21, 5);
2514 unsigned r1 = extract32(insn, 16, 5);
2515 unsigned cf = extract32(insn, 12, 4);
2516 unsigned ext = extract32(insn, 6, 6);
2517 unsigned rt = extract32(insn, 0, 5);
2518 TCGv_reg tcg_r1, tcg_r2;
2519 bool is_b = false;
2520 bool is_tc = false;
2521 bool is_tsv = false;
2522 DisasJumpType ret;
2524 switch (ext) {
2525 case 0x10: /* SUB */
2526 break;
2527 case 0x30: /* SUB,TSV */
2528 is_tsv = true;
2529 break;
2530 case 0x14: /* SUB,B */
2531 is_b = true;
2532 break;
2533 case 0x34: /* SUB,B,TSV */
2534 is_b = is_tsv = true;
2535 break;
2536 case 0x13: /* SUB,TC */
2537 is_tc = true;
2538 break;
2539 case 0x33: /* SUB,TSV,TC */
2540 is_tc = is_tsv = true;
2541 break;
2542 default:
2543 return gen_illegal(ctx);
2546 if (cf) {
2547 nullify_over(ctx);
2549 tcg_r1 = load_gpr(ctx, r1);
2550 tcg_r2 = load_gpr(ctx, r2);
2551 ret = do_sub(ctx, rt, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, cf);
2552 return nullify_end(ctx, ret);
2555 static DisasJumpType trans_log(DisasContext *ctx, uint32_t insn,
2556 const DisasInsn *di)
2558 unsigned r2 = extract32(insn, 21, 5);
2559 unsigned r1 = extract32(insn, 16, 5);
2560 unsigned cf = extract32(insn, 12, 4);
2561 unsigned rt = extract32(insn, 0, 5);
2562 TCGv_reg tcg_r1, tcg_r2;
2563 DisasJumpType ret;
2565 if (cf) {
2566 nullify_over(ctx);
2568 tcg_r1 = load_gpr(ctx, r1);
2569 tcg_r2 = load_gpr(ctx, r2);
2570 ret = do_log(ctx, rt, tcg_r1, tcg_r2, cf, di->f.ttt);
2571 return nullify_end(ctx, ret);
2574 /* OR r,0,t -> COPY (according to gas) */
2575 static DisasJumpType trans_copy(DisasContext *ctx, uint32_t insn,
2576 const DisasInsn *di)
2578 unsigned r1 = extract32(insn, 16, 5);
2579 unsigned rt = extract32(insn, 0, 5);
2581 if (r1 == 0) {
2582 TCGv_reg dest = dest_gpr(ctx, rt);
2583 tcg_gen_movi_reg(dest, 0);
2584 save_gpr(ctx, rt, dest);
2585 } else {
2586 save_gpr(ctx, rt, cpu_gr[r1]);
2588 cond_free(&ctx->null_cond);
2589 return DISAS_NEXT;
2592 static DisasJumpType trans_cmpclr(DisasContext *ctx, uint32_t insn,
2593 const DisasInsn *di)
2595 unsigned r2 = extract32(insn, 21, 5);
2596 unsigned r1 = extract32(insn, 16, 5);
2597 unsigned cf = extract32(insn, 12, 4);
2598 unsigned rt = extract32(insn, 0, 5);
2599 TCGv_reg tcg_r1, tcg_r2;
2600 DisasJumpType ret;
2602 if (cf) {
2603 nullify_over(ctx);
2605 tcg_r1 = load_gpr(ctx, r1);
2606 tcg_r2 = load_gpr(ctx, r2);
2607 ret = do_cmpclr(ctx, rt, tcg_r1, tcg_r2, cf);
2608 return nullify_end(ctx, ret);
2611 static DisasJumpType trans_uxor(DisasContext *ctx, uint32_t insn,
2612 const DisasInsn *di)
2614 unsigned r2 = extract32(insn, 21, 5);
2615 unsigned r1 = extract32(insn, 16, 5);
2616 unsigned cf = extract32(insn, 12, 4);
2617 unsigned rt = extract32(insn, 0, 5);
2618 TCGv_reg tcg_r1, tcg_r2;
2619 DisasJumpType ret;
2621 if (cf) {
2622 nullify_over(ctx);
2624 tcg_r1 = load_gpr(ctx, r1);
2625 tcg_r2 = load_gpr(ctx, r2);
2626 ret = do_unit(ctx, rt, tcg_r1, tcg_r2, cf, false, tcg_gen_xor_reg);
2627 return nullify_end(ctx, ret);
2630 static DisasJumpType trans_uaddcm(DisasContext *ctx, uint32_t insn,
2631 const DisasInsn *di)
2633 unsigned r2 = extract32(insn, 21, 5);
2634 unsigned r1 = extract32(insn, 16, 5);
2635 unsigned cf = extract32(insn, 12, 4);
2636 unsigned is_tc = extract32(insn, 6, 1);
2637 unsigned rt = extract32(insn, 0, 5);
2638 TCGv_reg tcg_r1, tcg_r2, tmp;
2639 DisasJumpType ret;
2641 if (cf) {
2642 nullify_over(ctx);
2644 tcg_r1 = load_gpr(ctx, r1);
2645 tcg_r2 = load_gpr(ctx, r2);
2646 tmp = get_temp(ctx);
2647 tcg_gen_not_reg(tmp, tcg_r2);
2648 ret = do_unit(ctx, rt, tcg_r1, tmp, cf, is_tc, tcg_gen_add_reg);
2649 return nullify_end(ctx, ret);
2652 static DisasJumpType trans_dcor(DisasContext *ctx, uint32_t insn,
2653 const DisasInsn *di)
2655 unsigned r2 = extract32(insn, 21, 5);
2656 unsigned cf = extract32(insn, 12, 4);
2657 unsigned is_i = extract32(insn, 6, 1);
2658 unsigned rt = extract32(insn, 0, 5);
2659 TCGv_reg tmp;
2660 DisasJumpType ret;
2662 nullify_over(ctx);
2664 tmp = get_temp(ctx);
2665 tcg_gen_shri_reg(tmp, cpu_psw_cb, 3);
2666 if (!is_i) {
2667 tcg_gen_not_reg(tmp, tmp);
2669 tcg_gen_andi_reg(tmp, tmp, 0x11111111);
2670 tcg_gen_muli_reg(tmp, tmp, 6);
2671 ret = do_unit(ctx, rt, tmp, load_gpr(ctx, r2), cf, false,
2672 is_i ? tcg_gen_add_reg : tcg_gen_sub_reg);
2674 return nullify_end(ctx, ret);
2677 static DisasJumpType trans_ds(DisasContext *ctx, uint32_t insn,
2678 const DisasInsn *di)
2680 unsigned r2 = extract32(insn, 21, 5);
2681 unsigned r1 = extract32(insn, 16, 5);
2682 unsigned cf = extract32(insn, 12, 4);
2683 unsigned rt = extract32(insn, 0, 5);
2684 TCGv_reg dest, add1, add2, addc, zero, in1, in2;
2686 nullify_over(ctx);
2688 in1 = load_gpr(ctx, r1);
2689 in2 = load_gpr(ctx, r2);
2691 add1 = tcg_temp_new();
2692 add2 = tcg_temp_new();
2693 addc = tcg_temp_new();
2694 dest = tcg_temp_new();
2695 zero = tcg_const_reg(0);
2697 /* Form R1 << 1 | PSW[CB]{8}. */
2698 tcg_gen_add_reg(add1, in1, in1);
2699 tcg_gen_add_reg(add1, add1, cpu_psw_cb_msb);
2701 /* Add or subtract R2, depending on PSW[V]. Proper computation of
2702 carry{8} requires that we subtract via + ~R2 + 1, as described in
2703 the manual. By extracting and masking V, we can produce the
2704 proper inputs to the addition without movcond. */
2705 tcg_gen_sari_reg(addc, cpu_psw_v, TARGET_REGISTER_BITS - 1);
2706 tcg_gen_xor_reg(add2, in2, addc);
2707 tcg_gen_andi_reg(addc, addc, 1);
2708 /* ??? This is only correct for 32-bit. */
2709 tcg_gen_add2_i32(dest, cpu_psw_cb_msb, add1, zero, add2, zero);
2710 tcg_gen_add2_i32(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb, addc, zero);
2712 tcg_temp_free(addc);
2713 tcg_temp_free(zero);
2715 /* Write back the result register. */
2716 save_gpr(ctx, rt, dest);
2718 /* Write back PSW[CB]. */
2719 tcg_gen_xor_reg(cpu_psw_cb, add1, add2);
2720 tcg_gen_xor_reg(cpu_psw_cb, cpu_psw_cb, dest);
2722 /* Write back PSW[V] for the division step. */
2723 tcg_gen_neg_reg(cpu_psw_v, cpu_psw_cb_msb);
2724 tcg_gen_xor_reg(cpu_psw_v, cpu_psw_v, in2);
2726 /* Install the new nullification. */
2727 if (cf) {
2728 TCGv_reg sv = NULL;
2729 if (cf >> 1 == 6) {
2730 /* ??? The lshift is supposed to contribute to overflow. */
2731 sv = do_add_sv(ctx, dest, add1, add2);
2733 ctx->null_cond = do_cond(cf, dest, cpu_psw_cb_msb, sv);
2736 tcg_temp_free(add1);
2737 tcg_temp_free(add2);
2738 tcg_temp_free(dest);
2740 return nullify_end(ctx, DISAS_NEXT);
2743 static const DisasInsn table_arith_log[] = {
2744 { 0x08000240u, 0xfc00ffffu, trans_nop }, /* or x,y,0 */
2745 { 0x08000240u, 0xffe0ffe0u, trans_copy }, /* or x,0,t */
2746 { 0x08000000u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_andc_reg },
2747 { 0x08000200u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_and_reg },
2748 { 0x08000240u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_or_reg },
2749 { 0x08000280u, 0xfc000fe0u, trans_log, .f.ttt = tcg_gen_xor_reg },
2750 { 0x08000880u, 0xfc000fe0u, trans_cmpclr },
2751 { 0x08000380u, 0xfc000fe0u, trans_uxor },
2752 { 0x08000980u, 0xfc000fa0u, trans_uaddcm },
2753 { 0x08000b80u, 0xfc1f0fa0u, trans_dcor },
2754 { 0x08000440u, 0xfc000fe0u, trans_ds },
2755 { 0x08000700u, 0xfc0007e0u, trans_add }, /* add */
2756 { 0x08000400u, 0xfc0006e0u, trans_sub }, /* sub; sub,b; sub,tsv */
2757 { 0x080004c0u, 0xfc0007e0u, trans_sub }, /* sub,tc; sub,tsv,tc */
2758 { 0x08000200u, 0xfc000320u, trans_add }, /* shladd */
2761 static DisasJumpType trans_addi(DisasContext *ctx, uint32_t insn)
2763 target_sreg im = low_sextract(insn, 0, 11);
2764 unsigned e1 = extract32(insn, 11, 1);
2765 unsigned cf = extract32(insn, 12, 4);
2766 unsigned rt = extract32(insn, 16, 5);
2767 unsigned r2 = extract32(insn, 21, 5);
2768 unsigned o1 = extract32(insn, 26, 1);
2769 TCGv_reg tcg_im, tcg_r2;
2770 DisasJumpType ret;
2772 if (cf) {
2773 nullify_over(ctx);
2776 tcg_im = load_const(ctx, im);
2777 tcg_r2 = load_gpr(ctx, r2);
2778 ret = do_add(ctx, rt, tcg_im, tcg_r2, 0, false, e1, !o1, false, cf);
2780 return nullify_end(ctx, ret);
2783 static DisasJumpType trans_subi(DisasContext *ctx, uint32_t insn)
2785 target_sreg im = low_sextract(insn, 0, 11);
2786 unsigned e1 = extract32(insn, 11, 1);
2787 unsigned cf = extract32(insn, 12, 4);
2788 unsigned rt = extract32(insn, 16, 5);
2789 unsigned r2 = extract32(insn, 21, 5);
2790 TCGv_reg tcg_im, tcg_r2;
2791 DisasJumpType ret;
2793 if (cf) {
2794 nullify_over(ctx);
2797 tcg_im = load_const(ctx, im);
2798 tcg_r2 = load_gpr(ctx, r2);
2799 ret = do_sub(ctx, rt, tcg_im, tcg_r2, e1, false, false, cf);
2801 return nullify_end(ctx, ret);
2804 static DisasJumpType trans_cmpiclr(DisasContext *ctx, uint32_t insn)
2806 target_sreg im = low_sextract(insn, 0, 11);
2807 unsigned cf = extract32(insn, 12, 4);
2808 unsigned rt = extract32(insn, 16, 5);
2809 unsigned r2 = extract32(insn, 21, 5);
2810 TCGv_reg tcg_im, tcg_r2;
2811 DisasJumpType ret;
2813 if (cf) {
2814 nullify_over(ctx);
2817 tcg_im = load_const(ctx, im);
2818 tcg_r2 = load_gpr(ctx, r2);
2819 ret = do_cmpclr(ctx, rt, tcg_im, tcg_r2, cf);
2821 return nullify_end(ctx, ret);
2824 static DisasJumpType trans_ld_idx_i(DisasContext *ctx, uint32_t insn,
2825 const DisasInsn *di)
2827 unsigned rt = extract32(insn, 0, 5);
2828 unsigned m = extract32(insn, 5, 1);
2829 unsigned sz = extract32(insn, 6, 2);
2830 unsigned a = extract32(insn, 13, 1);
2831 unsigned sp = extract32(insn, 14, 2);
2832 int disp = low_sextract(insn, 16, 5);
2833 unsigned rb = extract32(insn, 21, 5);
2834 int modify = (m ? (a ? -1 : 1) : 0);
2835 TCGMemOp mop = MO_TE | sz;
2837 return do_load(ctx, rt, rb, 0, 0, disp, sp, modify, mop);
2840 static DisasJumpType trans_ld_idx_x(DisasContext *ctx, uint32_t insn,
2841 const DisasInsn *di)
2843 unsigned rt = extract32(insn, 0, 5);
2844 unsigned m = extract32(insn, 5, 1);
2845 unsigned sz = extract32(insn, 6, 2);
2846 unsigned u = extract32(insn, 13, 1);
2847 unsigned sp = extract32(insn, 14, 2);
2848 unsigned rx = extract32(insn, 16, 5);
2849 unsigned rb = extract32(insn, 21, 5);
2850 TCGMemOp mop = MO_TE | sz;
2852 return do_load(ctx, rt, rb, rx, u ? sz : 0, 0, sp, m, mop);
2855 static DisasJumpType trans_st_idx_i(DisasContext *ctx, uint32_t insn,
2856 const DisasInsn *di)
2858 int disp = low_sextract(insn, 0, 5);
2859 unsigned m = extract32(insn, 5, 1);
2860 unsigned sz = extract32(insn, 6, 2);
2861 unsigned a = extract32(insn, 13, 1);
2862 unsigned sp = extract32(insn, 14, 2);
2863 unsigned rr = extract32(insn, 16, 5);
2864 unsigned rb = extract32(insn, 21, 5);
2865 int modify = (m ? (a ? -1 : 1) : 0);
2866 TCGMemOp mop = MO_TE | sz;
2868 return do_store(ctx, rr, rb, disp, sp, modify, mop);
2871 static DisasJumpType trans_ldcw(DisasContext *ctx, uint32_t insn,
2872 const DisasInsn *di)
2874 unsigned rt = extract32(insn, 0, 5);
2875 unsigned m = extract32(insn, 5, 1);
2876 unsigned i = extract32(insn, 12, 1);
2877 unsigned au = extract32(insn, 13, 1);
2878 unsigned sp = extract32(insn, 14, 2);
2879 unsigned rx = extract32(insn, 16, 5);
2880 unsigned rb = extract32(insn, 21, 5);
2881 TCGMemOp mop = MO_TEUL | MO_ALIGN_16;
2882 TCGv_reg zero, dest, ofs;
2883 TCGv_tl addr;
2884 int modify, disp = 0, scale = 0;
2886 nullify_over(ctx);
2888 if (i) {
2889 modify = (m ? (au ? -1 : 1) : 0);
2890 disp = low_sextract(rx, 0, 5);
2891 rx = 0;
2892 } else {
2893 modify = m;
2894 if (au) {
2895 scale = mop & MO_SIZE;
2898 if (modify) {
2899 /* Base register modification. Make sure if RT == RB,
2900 we see the result of the load. */
2901 dest = get_temp(ctx);
2902 } else {
2903 dest = dest_gpr(ctx, rt);
2906 form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
2907 ctx->mmu_idx == MMU_PHYS_IDX);
2908 zero = tcg_const_reg(0);
2909 tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
2910 if (modify) {
2911 save_gpr(ctx, rb, ofs);
2913 save_gpr(ctx, rt, dest);
2915 return nullify_end(ctx, DISAS_NEXT);
2918 static DisasJumpType trans_stby(DisasContext *ctx, uint32_t insn,
2919 const DisasInsn *di)
2921 target_sreg disp = low_sextract(insn, 0, 5);
2922 unsigned m = extract32(insn, 5, 1);
2923 unsigned a = extract32(insn, 13, 1);
2924 unsigned sp = extract32(insn, 14, 2);
2925 unsigned rt = extract32(insn, 16, 5);
2926 unsigned rb = extract32(insn, 21, 5);
2927 TCGv_reg ofs, val;
2928 TCGv_tl addr;
2930 nullify_over(ctx);
2932 form_gva(ctx, &addr, &ofs, rb, 0, 0, disp, sp, m,
2933 ctx->mmu_idx == MMU_PHYS_IDX);
2934 val = load_gpr(ctx, rt);
2935 if (a) {
2936 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2937 gen_helper_stby_e_parallel(cpu_env, addr, val);
2938 } else {
2939 gen_helper_stby_e(cpu_env, addr, val);
2941 } else {
2942 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2943 gen_helper_stby_b_parallel(cpu_env, addr, val);
2944 } else {
2945 gen_helper_stby_b(cpu_env, addr, val);
2949 if (m) {
2950 tcg_gen_andi_reg(ofs, ofs, ~3);
2951 save_gpr(ctx, rb, ofs);
2954 return nullify_end(ctx, DISAS_NEXT);
2957 #ifndef CONFIG_USER_ONLY
2958 static DisasJumpType trans_ldwa_idx_i(DisasContext *ctx, uint32_t insn,
2959 const DisasInsn *di)
2961 int hold_mmu_idx = ctx->mmu_idx;
2962 DisasJumpType ret;
2964 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2966 /* ??? needs fixing for hppa64 -- ldda does not follow the same
2967 format wrt the sub-opcode in bits 6:9. */
2968 ctx->mmu_idx = MMU_PHYS_IDX;
2969 ret = trans_ld_idx_i(ctx, insn, di);
2970 ctx->mmu_idx = hold_mmu_idx;
2971 return ret;
2974 static DisasJumpType trans_ldwa_idx_x(DisasContext *ctx, uint32_t insn,
2975 const DisasInsn *di)
2977 int hold_mmu_idx = ctx->mmu_idx;
2978 DisasJumpType ret;
2980 CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2982 /* ??? needs fixing for hppa64 -- ldda does not follow the same
2983 format wrt the sub-opcode in bits 6:9. */
2984 ctx->mmu_idx = MMU_PHYS_IDX;
2985 ret = trans_ld_idx_x(ctx, insn, di);
2986 ctx->mmu_idx = hold_mmu_idx;
2987 return ret;
2989 #endif
2991 static const DisasInsn table_index_mem[] = {
2992 { 0x0c001000u, 0xfc001300, trans_ld_idx_i }, /* LD[BHWD], im */
2993 { 0x0c000000u, 0xfc001300, trans_ld_idx_x }, /* LD[BHWD], rx */
2994 { 0x0c001200u, 0xfc001300, trans_st_idx_i }, /* ST[BHWD] */
2995 { 0x0c0001c0u, 0xfc0003c0, trans_ldcw },
2996 { 0x0c001300u, 0xfc0013c0, trans_stby },
2997 #ifndef CONFIG_USER_ONLY
2998 { 0x0c001180u, 0xfc00d3c0, trans_ldwa_idx_i }, /* LDWA, im */
2999 { 0x0c000180u, 0xfc00d3c0, trans_ldwa_idx_x }, /* LDWA, rx */
3000 #endif
3003 static DisasJumpType trans_ldil(DisasContext *ctx, uint32_t insn)
3005 unsigned rt = extract32(insn, 21, 5);
3006 target_sreg i = assemble_21(insn);
3007 TCGv_reg tcg_rt = dest_gpr(ctx, rt);
3009 tcg_gen_movi_reg(tcg_rt, i);
3010 save_gpr(ctx, rt, tcg_rt);
3011 cond_free(&ctx->null_cond);
3013 return DISAS_NEXT;
3016 static DisasJumpType trans_addil(DisasContext *ctx, uint32_t insn)
3018 unsigned rt = extract32(insn, 21, 5);
3019 target_sreg i = assemble_21(insn);
3020 TCGv_reg tcg_rt = load_gpr(ctx, rt);
3021 TCGv_reg tcg_r1 = dest_gpr(ctx, 1);
3023 tcg_gen_addi_reg(tcg_r1, tcg_rt, i);
3024 save_gpr(ctx, 1, tcg_r1);
3025 cond_free(&ctx->null_cond);
3027 return DISAS_NEXT;
3030 static DisasJumpType trans_ldo(DisasContext *ctx, uint32_t insn)
3032 unsigned rb = extract32(insn, 21, 5);
3033 unsigned rt = extract32(insn, 16, 5);
3034 target_sreg i = assemble_16(insn);
3035 TCGv_reg tcg_rt = dest_gpr(ctx, rt);
3037 /* Special case rb == 0, for the LDI pseudo-op.
3038 The COPY pseudo-op is handled for free within tcg_gen_addi_tl. */
3039 if (rb == 0) {
3040 tcg_gen_movi_reg(tcg_rt, i);
3041 } else {
3042 tcg_gen_addi_reg(tcg_rt, cpu_gr[rb], i);
3044 save_gpr(ctx, rt, tcg_rt);
3045 cond_free(&ctx->null_cond);
3047 return DISAS_NEXT;
3050 static DisasJumpType trans_load(DisasContext *ctx, uint32_t insn,
3051 bool is_mod, TCGMemOp mop)
3053 unsigned rb = extract32(insn, 21, 5);
3054 unsigned rt = extract32(insn, 16, 5);
3055 unsigned sp = extract32(insn, 14, 2);
3056 target_sreg i = assemble_16(insn);
3058 return do_load(ctx, rt, rb, 0, 0, i, sp,
3059 is_mod ? (i < 0 ? -1 : 1) : 0, mop);
3062 static DisasJumpType trans_load_w(DisasContext *ctx, uint32_t insn)
3064 unsigned rb = extract32(insn, 21, 5);
3065 unsigned rt = extract32(insn, 16, 5);
3066 unsigned sp = extract32(insn, 14, 2);
3067 target_sreg i = assemble_16a(insn);
3068 unsigned ext2 = extract32(insn, 1, 2);
3070 switch (ext2) {
3071 case 0:
3072 case 1:
3073 /* FLDW without modification. */
3074 return do_floadw(ctx, ext2 * 32 + rt, rb, 0, 0, i, sp, 0);
3075 case 2:
3076 /* LDW with modification. Note that the sign of I selects
3077 post-dec vs pre-inc. */
3078 return do_load(ctx, rt, rb, 0, 0, i, sp, (i < 0 ? 1 : -1), MO_TEUL);
3079 default:
3080 return gen_illegal(ctx);
3084 static DisasJumpType trans_fload_mod(DisasContext *ctx, uint32_t insn)
3086 target_sreg i = assemble_16a(insn);
3087 unsigned t1 = extract32(insn, 1, 1);
3088 unsigned a = extract32(insn, 2, 1);
3089 unsigned sp = extract32(insn, 14, 2);
3090 unsigned t0 = extract32(insn, 16, 5);
3091 unsigned rb = extract32(insn, 21, 5);
3093 /* FLDW with modification. */
3094 return do_floadw(ctx, t1 * 32 + t0, rb, 0, 0, i, sp, (a ? -1 : 1));
3097 static DisasJumpType trans_store(DisasContext *ctx, uint32_t insn,
3098 bool is_mod, TCGMemOp mop)
3100 unsigned rb = extract32(insn, 21, 5);
3101 unsigned rt = extract32(insn, 16, 5);
3102 unsigned sp = extract32(insn, 14, 2);
3103 target_sreg i = assemble_16(insn);
3105 return do_store(ctx, rt, rb, i, sp, is_mod ? (i < 0 ? -1 : 1) : 0, mop);
3108 static DisasJumpType trans_store_w(DisasContext *ctx, uint32_t insn)
3110 unsigned rb = extract32(insn, 21, 5);
3111 unsigned rt = extract32(insn, 16, 5);
3112 unsigned sp = extract32(insn, 14, 2);
3113 target_sreg i = assemble_16a(insn);
3114 unsigned ext2 = extract32(insn, 1, 2);
3116 switch (ext2) {
3117 case 0:
3118 case 1:
3119 /* FSTW without modification. */
3120 return do_fstorew(ctx, ext2 * 32 + rt, rb, 0, 0, i, sp, 0);
3121 case 2:
3122 /* LDW with modification. */
3123 return do_store(ctx, rt, rb, i, sp, (i < 0 ? 1 : -1), MO_TEUL);
3124 default:
3125 return gen_illegal(ctx);
3129 static DisasJumpType trans_fstore_mod(DisasContext *ctx, uint32_t insn)
3131 target_sreg i = assemble_16a(insn);
3132 unsigned t1 = extract32(insn, 1, 1);
3133 unsigned a = extract32(insn, 2, 1);
3134 unsigned sp = extract32(insn, 14, 2);
3135 unsigned t0 = extract32(insn, 16, 5);
3136 unsigned rb = extract32(insn, 21, 5);
3138 /* FSTW with modification. */
3139 return do_fstorew(ctx, t1 * 32 + t0, rb, 0, 0, i, sp, (a ? -1 : 1));
3142 static DisasJumpType trans_copr_w(DisasContext *ctx, uint32_t insn)
3144 unsigned t0 = extract32(insn, 0, 5);
3145 unsigned m = extract32(insn, 5, 1);
3146 unsigned t1 = extract32(insn, 6, 1);
3147 unsigned ext3 = extract32(insn, 7, 3);
3148 /* unsigned cc = extract32(insn, 10, 2); */
3149 unsigned i = extract32(insn, 12, 1);
3150 unsigned ua = extract32(insn, 13, 1);
3151 unsigned sp = extract32(insn, 14, 2);
3152 unsigned rx = extract32(insn, 16, 5);
3153 unsigned rb = extract32(insn, 21, 5);
3154 unsigned rt = t1 * 32 + t0;
3155 int modify = (m ? (ua ? -1 : 1) : 0);
3156 int disp, scale;
3158 if (i == 0) {
3159 scale = (ua ? 2 : 0);
3160 disp = 0;
3161 modify = m;
3162 } else {
3163 disp = low_sextract(rx, 0, 5);
3164 scale = 0;
3165 rx = 0;
3166 modify = (m ? (ua ? -1 : 1) : 0);
3169 switch (ext3) {
3170 case 0: /* FLDW */
3171 return do_floadw(ctx, rt, rb, rx, scale, disp, sp, modify);
3172 case 4: /* FSTW */
3173 return do_fstorew(ctx, rt, rb, rx, scale, disp, sp, modify);
3175 return gen_illegal(ctx);
3178 static DisasJumpType trans_copr_dw(DisasContext *ctx, uint32_t insn)
3180 unsigned rt = extract32(insn, 0, 5);
3181 unsigned m = extract32(insn, 5, 1);
3182 unsigned ext4 = extract32(insn, 6, 4);
3183 /* unsigned cc = extract32(insn, 10, 2); */
3184 unsigned i = extract32(insn, 12, 1);
3185 unsigned ua = extract32(insn, 13, 1);
3186 unsigned sp = extract32(insn, 14, 2);
3187 unsigned rx = extract32(insn, 16, 5);
3188 unsigned rb = extract32(insn, 21, 5);
3189 int modify = (m ? (ua ? -1 : 1) : 0);
3190 int disp, scale;
3192 if (i == 0) {
3193 scale = (ua ? 3 : 0);
3194 disp = 0;
3195 modify = m;
3196 } else {
3197 disp = low_sextract(rx, 0, 5);
3198 scale = 0;
3199 rx = 0;
3200 modify = (m ? (ua ? -1 : 1) : 0);
3203 switch (ext4) {
3204 case 0: /* FLDD */
3205 return do_floadd(ctx, rt, rb, rx, scale, disp, sp, modify);
3206 case 8: /* FSTD */
3207 return do_fstored(ctx, rt, rb, rx, scale, disp, sp, modify);
3208 default:
3209 return gen_illegal(ctx);
3213 static DisasJumpType trans_cmpb(DisasContext *ctx, uint32_t insn,
3214 bool is_true, bool is_imm, bool is_dw)
3216 target_sreg disp = assemble_12(insn) * 4;
3217 unsigned n = extract32(insn, 1, 1);
3218 unsigned c = extract32(insn, 13, 3);
3219 unsigned r = extract32(insn, 21, 5);
3220 unsigned cf = c * 2 + !is_true;
3221 TCGv_reg dest, in1, in2, sv;
3222 DisasCond cond;
3224 nullify_over(ctx);
3226 if (is_imm) {
3227 in1 = load_const(ctx, low_sextract(insn, 16, 5));
3228 } else {
3229 in1 = load_gpr(ctx, extract32(insn, 16, 5));
3231 in2 = load_gpr(ctx, r);
3232 dest = get_temp(ctx);
3234 tcg_gen_sub_reg(dest, in1, in2);
3236 sv = NULL;
3237 if (c == 6) {
3238 sv = do_sub_sv(ctx, dest, in1, in2);
3241 cond = do_sub_cond(cf, dest, in1, in2, sv);
3242 return do_cbranch(ctx, disp, n, &cond);
3245 static DisasJumpType trans_addb(DisasContext *ctx, uint32_t insn,
3246 bool is_true, bool is_imm)
3248 target_sreg disp = assemble_12(insn) * 4;
3249 unsigned n = extract32(insn, 1, 1);
3250 unsigned c = extract32(insn, 13, 3);
3251 unsigned r = extract32(insn, 21, 5);
3252 unsigned cf = c * 2 + !is_true;
3253 TCGv_reg dest, in1, in2, sv, cb_msb;
3254 DisasCond cond;
3256 nullify_over(ctx);
3258 if (is_imm) {
3259 in1 = load_const(ctx, low_sextract(insn, 16, 5));
3260 } else {
3261 in1 = load_gpr(ctx, extract32(insn, 16, 5));
3263 in2 = load_gpr(ctx, r);
3264 dest = dest_gpr(ctx, r);
3265 sv = NULL;
3266 cb_msb = NULL;
3268 switch (c) {
3269 default:
3270 tcg_gen_add_reg(dest, in1, in2);
3271 break;
3272 case 4: case 5:
3273 cb_msb = get_temp(ctx);
3274 tcg_gen_movi_reg(cb_msb, 0);
3275 tcg_gen_add2_reg(dest, cb_msb, in1, cb_msb, in2, cb_msb);
3276 break;
3277 case 6:
3278 tcg_gen_add_reg(dest, in1, in2);
3279 sv = do_add_sv(ctx, dest, in1, in2);
3280 break;
3283 cond = do_cond(cf, dest, cb_msb, sv);
3284 return do_cbranch(ctx, disp, n, &cond);
3287 static DisasJumpType trans_bb(DisasContext *ctx, uint32_t insn)
3289 target_sreg disp = assemble_12(insn) * 4;
3290 unsigned n = extract32(insn, 1, 1);
3291 unsigned c = extract32(insn, 15, 1);
3292 unsigned r = extract32(insn, 16, 5);
3293 unsigned p = extract32(insn, 21, 5);
3294 unsigned i = extract32(insn, 26, 1);
3295 TCGv_reg tmp, tcg_r;
3296 DisasCond cond;
3298 nullify_over(ctx);
3300 tmp = tcg_temp_new();
3301 tcg_r = load_gpr(ctx, r);
3302 if (i) {
3303 tcg_gen_shli_reg(tmp, tcg_r, p);
3304 } else {
3305 tcg_gen_shl_reg(tmp, tcg_r, cpu_sar);
3308 cond = cond_make_0(c ? TCG_COND_GE : TCG_COND_LT, tmp);
3309 tcg_temp_free(tmp);
3310 return do_cbranch(ctx, disp, n, &cond);
3313 static DisasJumpType trans_movb(DisasContext *ctx, uint32_t insn, bool is_imm)
3315 target_sreg disp = assemble_12(insn) * 4;
3316 unsigned n = extract32(insn, 1, 1);
3317 unsigned c = extract32(insn, 13, 3);
3318 unsigned t = extract32(insn, 16, 5);
3319 unsigned r = extract32(insn, 21, 5);
3320 TCGv_reg dest;
3321 DisasCond cond;
3323 nullify_over(ctx);
3325 dest = dest_gpr(ctx, r);
3326 if (is_imm) {
3327 tcg_gen_movi_reg(dest, low_sextract(t, 0, 5));
3328 } else if (t == 0) {
3329 tcg_gen_movi_reg(dest, 0);
3330 } else {
3331 tcg_gen_mov_reg(dest, cpu_gr[t]);
3334 cond = do_sed_cond(c, dest);
3335 return do_cbranch(ctx, disp, n, &cond);
3338 static DisasJumpType trans_shrpw_sar(DisasContext *ctx, uint32_t insn,
3339 const DisasInsn *di)
3341 unsigned rt = extract32(insn, 0, 5);
3342 unsigned c = extract32(insn, 13, 3);
3343 unsigned r1 = extract32(insn, 16, 5);
3344 unsigned r2 = extract32(insn, 21, 5);
3345 TCGv_reg dest;
3347 if (c) {
3348 nullify_over(ctx);
3351 dest = dest_gpr(ctx, rt);
3352 if (r1 == 0) {
3353 tcg_gen_ext32u_reg(dest, load_gpr(ctx, r2));
3354 tcg_gen_shr_reg(dest, dest, cpu_sar);
3355 } else if (r1 == r2) {
3356 TCGv_i32 t32 = tcg_temp_new_i32();
3357 tcg_gen_trunc_reg_i32(t32, load_gpr(ctx, r2));
3358 tcg_gen_rotr_i32(t32, t32, cpu_sar);
3359 tcg_gen_extu_i32_reg(dest, t32);
3360 tcg_temp_free_i32(t32);
3361 } else {
3362 TCGv_i64 t = tcg_temp_new_i64();
3363 TCGv_i64 s = tcg_temp_new_i64();
3365 tcg_gen_concat_reg_i64(t, load_gpr(ctx, r2), load_gpr(ctx, r1));
3366 tcg_gen_extu_reg_i64(s, cpu_sar);
3367 tcg_gen_shr_i64(t, t, s);
3368 tcg_gen_trunc_i64_reg(dest, t);
3370 tcg_temp_free_i64(t);
3371 tcg_temp_free_i64(s);
3373 save_gpr(ctx, rt, dest);
3375 /* Install the new nullification. */
3376 cond_free(&ctx->null_cond);
3377 if (c) {
3378 ctx->null_cond = do_sed_cond(c, dest);
3380 return nullify_end(ctx, DISAS_NEXT);
3383 static DisasJumpType trans_shrpw_imm(DisasContext *ctx, uint32_t insn,
3384 const DisasInsn *di)
3386 unsigned rt = extract32(insn, 0, 5);
3387 unsigned cpos = extract32(insn, 5, 5);
3388 unsigned c = extract32(insn, 13, 3);
3389 unsigned r1 = extract32(insn, 16, 5);
3390 unsigned r2 = extract32(insn, 21, 5);
3391 unsigned sa = 31 - cpos;
3392 TCGv_reg dest, t2;
3394 if (c) {
3395 nullify_over(ctx);
3398 dest = dest_gpr(ctx, rt);
3399 t2 = load_gpr(ctx, r2);
3400 if (r1 == r2) {
3401 TCGv_i32 t32 = tcg_temp_new_i32();
3402 tcg_gen_trunc_reg_i32(t32, t2);
3403 tcg_gen_rotri_i32(t32, t32, sa);
3404 tcg_gen_extu_i32_reg(dest, t32);
3405 tcg_temp_free_i32(t32);
3406 } else if (r1 == 0) {
3407 tcg_gen_extract_reg(dest, t2, sa, 32 - sa);
3408 } else {
3409 TCGv_reg t0 = tcg_temp_new();
3410 tcg_gen_extract_reg(t0, t2, sa, 32 - sa);
3411 tcg_gen_deposit_reg(dest, t0, cpu_gr[r1], 32 - sa, sa);
3412 tcg_temp_free(t0);
3414 save_gpr(ctx, rt, dest);
3416 /* Install the new nullification. */
3417 cond_free(&ctx->null_cond);
3418 if (c) {
3419 ctx->null_cond = do_sed_cond(c, dest);
3421 return nullify_end(ctx, DISAS_NEXT);
3424 static DisasJumpType trans_extrw_sar(DisasContext *ctx, uint32_t insn,
3425 const DisasInsn *di)
3427 unsigned clen = extract32(insn, 0, 5);
3428 unsigned is_se = extract32(insn, 10, 1);
3429 unsigned c = extract32(insn, 13, 3);
3430 unsigned rt = extract32(insn, 16, 5);
3431 unsigned rr = extract32(insn, 21, 5);
3432 unsigned len = 32 - clen;
3433 TCGv_reg dest, src, tmp;
3435 if (c) {
3436 nullify_over(ctx);
3439 dest = dest_gpr(ctx, rt);
3440 src = load_gpr(ctx, rr);
3441 tmp = tcg_temp_new();
3443 /* Recall that SAR is using big-endian bit numbering. */
3444 tcg_gen_xori_reg(tmp, cpu_sar, TARGET_REGISTER_BITS - 1);
3445 if (is_se) {
3446 tcg_gen_sar_reg(dest, src, tmp);
3447 tcg_gen_sextract_reg(dest, dest, 0, len);
3448 } else {
3449 tcg_gen_shr_reg(dest, src, tmp);
3450 tcg_gen_extract_reg(dest, dest, 0, len);
3452 tcg_temp_free(tmp);
3453 save_gpr(ctx, rt, dest);
3455 /* Install the new nullification. */
3456 cond_free(&ctx->null_cond);
3457 if (c) {
3458 ctx->null_cond = do_sed_cond(c, dest);
3460 return nullify_end(ctx, DISAS_NEXT);
3463 static DisasJumpType trans_extrw_imm(DisasContext *ctx, uint32_t insn,
3464 const DisasInsn *di)
3466 unsigned clen = extract32(insn, 0, 5);
3467 unsigned pos = extract32(insn, 5, 5);
3468 unsigned is_se = extract32(insn, 10, 1);
3469 unsigned c = extract32(insn, 13, 3);
3470 unsigned rt = extract32(insn, 16, 5);
3471 unsigned rr = extract32(insn, 21, 5);
3472 unsigned len = 32 - clen;
3473 unsigned cpos = 31 - pos;
3474 TCGv_reg dest, src;
3476 if (c) {
3477 nullify_over(ctx);
3480 dest = dest_gpr(ctx, rt);
3481 src = load_gpr(ctx, rr);
3482 if (is_se) {
3483 tcg_gen_sextract_reg(dest, src, cpos, len);
3484 } else {
3485 tcg_gen_extract_reg(dest, src, cpos, len);
3487 save_gpr(ctx, rt, dest);
3489 /* Install the new nullification. */
3490 cond_free(&ctx->null_cond);
3491 if (c) {
3492 ctx->null_cond = do_sed_cond(c, dest);
3494 return nullify_end(ctx, DISAS_NEXT);
3497 static const DisasInsn table_sh_ex[] = {
3498 { 0xd0000000u, 0xfc001fe0u, trans_shrpw_sar },
3499 { 0xd0000800u, 0xfc001c00u, trans_shrpw_imm },
3500 { 0xd0001000u, 0xfc001be0u, trans_extrw_sar },
3501 { 0xd0001800u, 0xfc001800u, trans_extrw_imm },
3504 static DisasJumpType trans_depw_imm_c(DisasContext *ctx, uint32_t insn,
3505 const DisasInsn *di)
3507 unsigned clen = extract32(insn, 0, 5);
3508 unsigned cpos = extract32(insn, 5, 5);
3509 unsigned nz = extract32(insn, 10, 1);
3510 unsigned c = extract32(insn, 13, 3);
3511 target_sreg val = low_sextract(insn, 16, 5);
3512 unsigned rt = extract32(insn, 21, 5);
3513 unsigned len = 32 - clen;
3514 target_sreg mask0, mask1;
3515 TCGv_reg dest;
3517 if (c) {
3518 nullify_over(ctx);
3520 if (cpos + len > 32) {
3521 len = 32 - cpos;
3524 dest = dest_gpr(ctx, rt);
3525 mask0 = deposit64(0, cpos, len, val);
3526 mask1 = deposit64(-1, cpos, len, val);
3528 if (nz) {
3529 TCGv_reg src = load_gpr(ctx, rt);
3530 if (mask1 != -1) {
3531 tcg_gen_andi_reg(dest, src, mask1);
3532 src = dest;
3534 tcg_gen_ori_reg(dest, src, mask0);
3535 } else {
3536 tcg_gen_movi_reg(dest, mask0);
3538 save_gpr(ctx, rt, dest);
3540 /* Install the new nullification. */
3541 cond_free(&ctx->null_cond);
3542 if (c) {
3543 ctx->null_cond = do_sed_cond(c, dest);
3545 return nullify_end(ctx, DISAS_NEXT);
3548 static DisasJumpType trans_depw_imm(DisasContext *ctx, uint32_t insn,
3549 const DisasInsn *di)
3551 unsigned clen = extract32(insn, 0, 5);
3552 unsigned cpos = extract32(insn, 5, 5);
3553 unsigned nz = extract32(insn, 10, 1);
3554 unsigned c = extract32(insn, 13, 3);
3555 unsigned rr = extract32(insn, 16, 5);
3556 unsigned rt = extract32(insn, 21, 5);
3557 unsigned rs = nz ? rt : 0;
3558 unsigned len = 32 - clen;
3559 TCGv_reg dest, val;
3561 if (c) {
3562 nullify_over(ctx);
3564 if (cpos + len > 32) {
3565 len = 32 - cpos;
3568 dest = dest_gpr(ctx, rt);
3569 val = load_gpr(ctx, rr);
3570 if (rs == 0) {
3571 tcg_gen_deposit_z_reg(dest, val, cpos, len);
3572 } else {
3573 tcg_gen_deposit_reg(dest, cpu_gr[rs], val, cpos, len);
3575 save_gpr(ctx, rt, dest);
3577 /* Install the new nullification. */
3578 cond_free(&ctx->null_cond);
3579 if (c) {
3580 ctx->null_cond = do_sed_cond(c, dest);
3582 return nullify_end(ctx, DISAS_NEXT);
3585 static DisasJumpType trans_depw_sar(DisasContext *ctx, uint32_t insn,
3586 const DisasInsn *di)
3588 unsigned clen = extract32(insn, 0, 5);
3589 unsigned nz = extract32(insn, 10, 1);
3590 unsigned i = extract32(insn, 12, 1);
3591 unsigned c = extract32(insn, 13, 3);
3592 unsigned rt = extract32(insn, 21, 5);
3593 unsigned rs = nz ? rt : 0;
3594 unsigned len = 32 - clen;
3595 TCGv_reg val, mask, tmp, shift, dest;
3596 unsigned msb = 1U << (len - 1);
3598 if (c) {
3599 nullify_over(ctx);
3602 if (i) {
3603 val = load_const(ctx, low_sextract(insn, 16, 5));
3604 } else {
3605 val = load_gpr(ctx, extract32(insn, 16, 5));
3607 dest = dest_gpr(ctx, rt);
3608 shift = tcg_temp_new();
3609 tmp = tcg_temp_new();
3611 /* Convert big-endian bit numbering in SAR to left-shift. */
3612 tcg_gen_xori_reg(shift, cpu_sar, TARGET_REGISTER_BITS - 1);
3614 mask = tcg_const_reg(msb + (msb - 1));
3615 tcg_gen_and_reg(tmp, val, mask);
3616 if (rs) {
3617 tcg_gen_shl_reg(mask, mask, shift);
3618 tcg_gen_shl_reg(tmp, tmp, shift);
3619 tcg_gen_andc_reg(dest, cpu_gr[rs], mask);
3620 tcg_gen_or_reg(dest, dest, tmp);
3621 } else {
3622 tcg_gen_shl_reg(dest, tmp, shift);
3624 tcg_temp_free(shift);
3625 tcg_temp_free(mask);
3626 tcg_temp_free(tmp);
3627 save_gpr(ctx, rt, dest);
3629 /* Install the new nullification. */
3630 cond_free(&ctx->null_cond);
3631 if (c) {
3632 ctx->null_cond = do_sed_cond(c, dest);
3634 return nullify_end(ctx, DISAS_NEXT);
3637 static const DisasInsn table_depw[] = {
3638 { 0xd4000000u, 0xfc000be0u, trans_depw_sar },
3639 { 0xd4000800u, 0xfc001800u, trans_depw_imm },
3640 { 0xd4001800u, 0xfc001800u, trans_depw_imm_c },
3643 static DisasJumpType trans_be(DisasContext *ctx, uint32_t insn, bool is_l)
3645 unsigned n = extract32(insn, 1, 1);
3646 unsigned b = extract32(insn, 21, 5);
3647 target_sreg disp = assemble_17(insn);
3648 TCGv_reg tmp;
3650 #ifdef CONFIG_USER_ONLY
3651 /* ??? It seems like there should be a good way of using
3652 "be disp(sr2, r0)", the canonical gateway entry mechanism
3653 to our advantage. But that appears to be inconvenient to
3654 manage along side branch delay slots. Therefore we handle
3655 entry into the gateway page via absolute address. */
3656 /* Since we don't implement spaces, just branch. Do notice the special
3657 case of "be disp(*,r0)" using a direct branch to disp, so that we can
3658 goto_tb to the TB containing the syscall. */
3659 if (b == 0) {
3660 return do_dbranch(ctx, disp, is_l ? 31 : 0, n);
3662 #else
3663 int sp = assemble_sr3(insn);
3664 nullify_over(ctx);
3665 #endif
3667 tmp = get_temp(ctx);
3668 tcg_gen_addi_reg(tmp, load_gpr(ctx, b), disp);
3669 tmp = do_ibranch_priv(ctx, tmp);
3671 #ifdef CONFIG_USER_ONLY
3672 return do_ibranch(ctx, tmp, is_l ? 31 : 0, n);
3673 #else
3674 TCGv_i64 new_spc = tcg_temp_new_i64();
3676 load_spr(ctx, new_spc, sp);
3677 if (is_l) {
3678 copy_iaoq_entry(cpu_gr[31], ctx->iaoq_n, ctx->iaoq_n_var);
3679 tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_f);
3681 if (n && use_nullify_skip(ctx)) {
3682 tcg_gen_mov_reg(cpu_iaoq_f, tmp);
3683 tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
3684 tcg_gen_mov_i64(cpu_iasq_f, new_spc);
3685 tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
3686 } else {
3687 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3688 if (ctx->iaoq_b == -1) {
3689 tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3691 tcg_gen_mov_reg(cpu_iaoq_b, tmp);
3692 tcg_gen_mov_i64(cpu_iasq_b, new_spc);
3693 nullify_set(ctx, n);
3695 tcg_temp_free_i64(new_spc);
3696 tcg_gen_lookup_and_goto_ptr();
3697 return nullify_end(ctx, DISAS_NORETURN);
3698 #endif
3701 static DisasJumpType trans_bl(DisasContext *ctx, uint32_t insn,
3702 const DisasInsn *di)
3704 unsigned n = extract32(insn, 1, 1);
3705 unsigned link = extract32(insn, 21, 5);
3706 target_sreg disp = assemble_17(insn);
3708 return do_dbranch(ctx, iaoq_dest(ctx, disp), link, n);
3711 static DisasJumpType trans_bl_long(DisasContext *ctx, uint32_t insn,
3712 const DisasInsn *di)
3714 unsigned n = extract32(insn, 1, 1);
3715 target_sreg disp = assemble_22(insn);
3717 return do_dbranch(ctx, iaoq_dest(ctx, disp), 2, n);
3720 static DisasJumpType trans_blr(DisasContext *ctx, uint32_t insn,
3721 const DisasInsn *di)
3723 unsigned n = extract32(insn, 1, 1);
3724 unsigned rx = extract32(insn, 16, 5);
3725 unsigned link = extract32(insn, 21, 5);
3726 TCGv_reg tmp = get_temp(ctx);
3728 tcg_gen_shli_reg(tmp, load_gpr(ctx, rx), 3);
3729 tcg_gen_addi_reg(tmp, tmp, ctx->iaoq_f + 8);
3730 /* The computation here never changes privilege level. */
3731 return do_ibranch(ctx, tmp, link, n);
3734 static DisasJumpType trans_bv(DisasContext *ctx, uint32_t insn,
3735 const DisasInsn *di)
3737 unsigned n = extract32(insn, 1, 1);
3738 unsigned rx = extract32(insn, 16, 5);
3739 unsigned rb = extract32(insn, 21, 5);
3740 TCGv_reg dest;
3742 if (rx == 0) {
3743 dest = load_gpr(ctx, rb);
3744 } else {
3745 dest = get_temp(ctx);
3746 tcg_gen_shli_reg(dest, load_gpr(ctx, rx), 3);
3747 tcg_gen_add_reg(dest, dest, load_gpr(ctx, rb));
3749 dest = do_ibranch_priv(ctx, dest);
3750 return do_ibranch(ctx, dest, 0, n);
3753 static DisasJumpType trans_bve(DisasContext *ctx, uint32_t insn,
3754 const DisasInsn *di)
3756 unsigned n = extract32(insn, 1, 1);
3757 unsigned rb = extract32(insn, 21, 5);
3758 unsigned link = extract32(insn, 13, 1) ? 2 : 0;
3759 TCGv_reg dest;
3761 #ifdef CONFIG_USER_ONLY
3762 dest = do_ibranch_priv(ctx, load_gpr(ctx, rb));
3763 return do_ibranch(ctx, dest, link, n);
3764 #else
3765 nullify_over(ctx);
3766 dest = do_ibranch_priv(ctx, load_gpr(ctx, rb));
3768 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3769 if (ctx->iaoq_b == -1) {
3770 tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3772 copy_iaoq_entry(cpu_iaoq_b, -1, dest);
3773 tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
3774 if (link) {
3775 copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
3777 nullify_set(ctx, n);
3778 tcg_gen_lookup_and_goto_ptr();
3779 return nullify_end(ctx, DISAS_NORETURN);
3780 #endif
3783 static const DisasInsn table_branch[] = {
3784 { 0xe8000000u, 0xfc006000u, trans_bl }, /* B,L and B,L,PUSH */
3785 { 0xe800a000u, 0xfc00e000u, trans_bl_long },
3786 { 0xe8004000u, 0xfc00fffdu, trans_blr },
3787 { 0xe800c000u, 0xfc00fffdu, trans_bv },
3788 { 0xe800d000u, 0xfc00dffcu, trans_bve },
3791 static DisasJumpType trans_fop_wew_0c(DisasContext *ctx, uint32_t insn,
3792 const DisasInsn *di)
3794 unsigned rt = extract32(insn, 0, 5);
3795 unsigned ra = extract32(insn, 21, 5);
3796 return do_fop_wew(ctx, rt, ra, di->f.wew);
3799 static DisasJumpType trans_fop_wew_0e(DisasContext *ctx, uint32_t insn,
3800 const DisasInsn *di)
3802 unsigned rt = assemble_rt64(insn);
3803 unsigned ra = assemble_ra64(insn);
3804 return do_fop_wew(ctx, rt, ra, di->f.wew);
3807 static DisasJumpType trans_fop_ded(DisasContext *ctx, uint32_t insn,
3808 const DisasInsn *di)
3810 unsigned rt = extract32(insn, 0, 5);
3811 unsigned ra = extract32(insn, 21, 5);
3812 return do_fop_ded(ctx, rt, ra, di->f.ded);
3815 static DisasJumpType trans_fop_wed_0c(DisasContext *ctx, uint32_t insn,
3816 const DisasInsn *di)
3818 unsigned rt = extract32(insn, 0, 5);
3819 unsigned ra = extract32(insn, 21, 5);
3820 return do_fop_wed(ctx, rt, ra, di->f.wed);
3823 static DisasJumpType trans_fop_wed_0e(DisasContext *ctx, uint32_t insn,
3824 const DisasInsn *di)
3826 unsigned rt = assemble_rt64(insn);
3827 unsigned ra = extract32(insn, 21, 5);
3828 return do_fop_wed(ctx, rt, ra, di->f.wed);
3831 static DisasJumpType trans_fop_dew_0c(DisasContext *ctx, uint32_t insn,
3832 const DisasInsn *di)
3834 unsigned rt = extract32(insn, 0, 5);
3835 unsigned ra = extract32(insn, 21, 5);
3836 return do_fop_dew(ctx, rt, ra, di->f.dew);
3839 static DisasJumpType trans_fop_dew_0e(DisasContext *ctx, uint32_t insn,
3840 const DisasInsn *di)
3842 unsigned rt = extract32(insn, 0, 5);
3843 unsigned ra = assemble_ra64(insn);
3844 return do_fop_dew(ctx, rt, ra, di->f.dew);
3847 static DisasJumpType trans_fop_weww_0c(DisasContext *ctx, uint32_t insn,
3848 const DisasInsn *di)
3850 unsigned rt = extract32(insn, 0, 5);
3851 unsigned rb = extract32(insn, 16, 5);
3852 unsigned ra = extract32(insn, 21, 5);
3853 return do_fop_weww(ctx, rt, ra, rb, di->f.weww);
3856 static DisasJumpType trans_fop_weww_0e(DisasContext *ctx, uint32_t insn,
3857 const DisasInsn *di)
3859 unsigned rt = assemble_rt64(insn);
3860 unsigned rb = assemble_rb64(insn);
3861 unsigned ra = assemble_ra64(insn);
3862 return do_fop_weww(ctx, rt, ra, rb, di->f.weww);
3865 static DisasJumpType trans_fop_dedd(DisasContext *ctx, uint32_t insn,
3866 const DisasInsn *di)
3868 unsigned rt = extract32(insn, 0, 5);
3869 unsigned rb = extract32(insn, 16, 5);
3870 unsigned ra = extract32(insn, 21, 5);
3871 return do_fop_dedd(ctx, rt, ra, rb, di->f.dedd);
3874 static void gen_fcpy_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3876 tcg_gen_mov_i32(dst, src);
3879 static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3881 tcg_gen_mov_i64(dst, src);
3884 static void gen_fabs_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3886 tcg_gen_andi_i32(dst, src, INT32_MAX);
3889 static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3891 tcg_gen_andi_i64(dst, src, INT64_MAX);
3894 static void gen_fneg_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3896 tcg_gen_xori_i32(dst, src, INT32_MIN);
3899 static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3901 tcg_gen_xori_i64(dst, src, INT64_MIN);
3904 static void gen_fnegabs_s(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3906 tcg_gen_ori_i32(dst, src, INT32_MIN);
3909 static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3911 tcg_gen_ori_i64(dst, src, INT64_MIN);
3914 static DisasJumpType do_fcmp_s(DisasContext *ctx, unsigned ra, unsigned rb,
3915 unsigned y, unsigned c)
3917 TCGv_i32 ta, tb, tc, ty;
3919 nullify_over(ctx);
3921 ta = load_frw0_i32(ra);
3922 tb = load_frw0_i32(rb);
3923 ty = tcg_const_i32(y);
3924 tc = tcg_const_i32(c);
3926 gen_helper_fcmp_s(cpu_env, ta, tb, ty, tc);
3928 tcg_temp_free_i32(ta);
3929 tcg_temp_free_i32(tb);
3930 tcg_temp_free_i32(ty);
3931 tcg_temp_free_i32(tc);
3933 return nullify_end(ctx, DISAS_NEXT);
3936 static DisasJumpType trans_fcmp_s_0c(DisasContext *ctx, uint32_t insn,
3937 const DisasInsn *di)
3939 unsigned c = extract32(insn, 0, 5);
3940 unsigned y = extract32(insn, 13, 3);
3941 unsigned rb = extract32(insn, 16, 5);
3942 unsigned ra = extract32(insn, 21, 5);
3943 return do_fcmp_s(ctx, ra, rb, y, c);
3946 static DisasJumpType trans_fcmp_s_0e(DisasContext *ctx, uint32_t insn,
3947 const DisasInsn *di)
3949 unsigned c = extract32(insn, 0, 5);
3950 unsigned y = extract32(insn, 13, 3);
3951 unsigned rb = assemble_rb64(insn);
3952 unsigned ra = assemble_ra64(insn);
3953 return do_fcmp_s(ctx, ra, rb, y, c);
3956 static DisasJumpType trans_fcmp_d(DisasContext *ctx, uint32_t insn,
3957 const DisasInsn *di)
3959 unsigned c = extract32(insn, 0, 5);
3960 unsigned y = extract32(insn, 13, 3);
3961 unsigned rb = extract32(insn, 16, 5);
3962 unsigned ra = extract32(insn, 21, 5);
3963 TCGv_i64 ta, tb;
3964 TCGv_i32 tc, ty;
3966 nullify_over(ctx);
3968 ta = load_frd0(ra);
3969 tb = load_frd0(rb);
3970 ty = tcg_const_i32(y);
3971 tc = tcg_const_i32(c);
3973 gen_helper_fcmp_d(cpu_env, ta, tb, ty, tc);
3975 tcg_temp_free_i64(ta);
3976 tcg_temp_free_i64(tb);
3977 tcg_temp_free_i32(ty);
3978 tcg_temp_free_i32(tc);
3980 return nullify_end(ctx, DISAS_NEXT);
3983 static DisasJumpType trans_ftest_t(DisasContext *ctx, uint32_t insn,
3984 const DisasInsn *di)
3986 unsigned y = extract32(insn, 13, 3);
3987 unsigned cbit = (y ^ 1) - 1;
3988 TCGv_reg t;
3990 nullify_over(ctx);
3992 t = tcg_temp_new();
3993 tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
3994 tcg_gen_extract_reg(t, t, 21 - cbit, 1);
3995 ctx->null_cond = cond_make_0(TCG_COND_NE, t);
3996 tcg_temp_free(t);
3998 return nullify_end(ctx, DISAS_NEXT);
4001 static DisasJumpType trans_ftest_q(DisasContext *ctx, uint32_t insn,
4002 const DisasInsn *di)
4004 unsigned c = extract32(insn, 0, 5);
4005 int mask;
4006 bool inv = false;
4007 TCGv_reg t;
4009 nullify_over(ctx);
4011 t = tcg_temp_new();
4012 tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
4014 switch (c) {
4015 case 0: /* simple */
4016 tcg_gen_andi_reg(t, t, 0x4000000);
4017 ctx->null_cond = cond_make_0(TCG_COND_NE, t);
4018 goto done;
4019 case 2: /* rej */
4020 inv = true;
4021 /* fallthru */
4022 case 1: /* acc */
4023 mask = 0x43ff800;
4024 break;
4025 case 6: /* rej8 */
4026 inv = true;
4027 /* fallthru */
4028 case 5: /* acc8 */
4029 mask = 0x43f8000;
4030 break;
4031 case 9: /* acc6 */
4032 mask = 0x43e0000;
4033 break;
4034 case 13: /* acc4 */
4035 mask = 0x4380000;
4036 break;
4037 case 17: /* acc2 */
4038 mask = 0x4200000;
4039 break;
4040 default:
4041 return gen_illegal(ctx);
4043 if (inv) {
4044 TCGv_reg c = load_const(ctx, mask);
4045 tcg_gen_or_reg(t, t, c);
4046 ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
4047 } else {
4048 tcg_gen_andi_reg(t, t, mask);
4049 ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
4051 done:
4052 return nullify_end(ctx, DISAS_NEXT);
4055 static DisasJumpType trans_xmpyu(DisasContext *ctx, uint32_t insn,
4056 const DisasInsn *di)
4058 unsigned rt = extract32(insn, 0, 5);
4059 unsigned rb = assemble_rb64(insn);
4060 unsigned ra = assemble_ra64(insn);
4061 TCGv_i64 a, b;
4063 nullify_over(ctx);
4065 a = load_frw0_i64(ra);
4066 b = load_frw0_i64(rb);
4067 tcg_gen_mul_i64(a, a, b);
4068 save_frd(rt, a);
4069 tcg_temp_free_i64(a);
4070 tcg_temp_free_i64(b);
4072 return nullify_end(ctx, DISAS_NEXT);
4075 #define FOP_DED trans_fop_ded, .f.ded
4076 #define FOP_DEDD trans_fop_dedd, .f.dedd
4078 #define FOP_WEW trans_fop_wew_0c, .f.wew
4079 #define FOP_DEW trans_fop_dew_0c, .f.dew
4080 #define FOP_WED trans_fop_wed_0c, .f.wed
4081 #define FOP_WEWW trans_fop_weww_0c, .f.weww
4083 static const DisasInsn table_float_0c[] = {
4084 /* floating point class zero */
4085 { 0x30004000, 0xfc1fffe0, FOP_WEW = gen_fcpy_s },
4086 { 0x30006000, 0xfc1fffe0, FOP_WEW = gen_fabs_s },
4087 { 0x30008000, 0xfc1fffe0, FOP_WEW = gen_helper_fsqrt_s },
4088 { 0x3000a000, 0xfc1fffe0, FOP_WEW = gen_helper_frnd_s },
4089 { 0x3000c000, 0xfc1fffe0, FOP_WEW = gen_fneg_s },
4090 { 0x3000e000, 0xfc1fffe0, FOP_WEW = gen_fnegabs_s },
4092 { 0x30004800, 0xfc1fffe0, FOP_DED = gen_fcpy_d },
4093 { 0x30006800, 0xfc1fffe0, FOP_DED = gen_fabs_d },
4094 { 0x30008800, 0xfc1fffe0, FOP_DED = gen_helper_fsqrt_d },
4095 { 0x3000a800, 0xfc1fffe0, FOP_DED = gen_helper_frnd_d },
4096 { 0x3000c800, 0xfc1fffe0, FOP_DED = gen_fneg_d },
4097 { 0x3000e800, 0xfc1fffe0, FOP_DED = gen_fnegabs_d },
4099 /* floating point class three */
4100 { 0x30000600, 0xfc00ffe0, FOP_WEWW = gen_helper_fadd_s },
4101 { 0x30002600, 0xfc00ffe0, FOP_WEWW = gen_helper_fsub_s },
4102 { 0x30004600, 0xfc00ffe0, FOP_WEWW = gen_helper_fmpy_s },
4103 { 0x30006600, 0xfc00ffe0, FOP_WEWW = gen_helper_fdiv_s },
4105 { 0x30000e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fadd_d },
4106 { 0x30002e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fsub_d },
4107 { 0x30004e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fmpy_d },
4108 { 0x30006e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fdiv_d },
4110 /* floating point class one */
4111 /* float/float */
4112 { 0x30000a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_d_s },
4113 { 0x30002200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_s_d },
4114 /* int/float */
4115 { 0x30008200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_w_s },
4116 { 0x30008a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_dw_s },
4117 { 0x3000a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_w_d },
4118 { 0x3000aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_dw_d },
4119 /* float/int */
4120 { 0x30010200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_s_w },
4121 { 0x30010a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_d_w },
4122 { 0x30012200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_s_dw },
4123 { 0x30012a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_dw },
4124 /* float/int truncate */
4125 { 0x30018200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_t_s_w },
4126 { 0x30018a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_t_d_w },
4127 { 0x3001a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_t_s_dw },
4128 { 0x3001aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_dw },
4129 /* uint/float */
4130 { 0x30028200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_uw_s },
4131 { 0x30028a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_udw_s },
4132 { 0x3002a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_uw_d },
4133 { 0x3002aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_udw_d },
4134 /* float/uint */
4135 { 0x30030200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_s_uw },
4136 { 0x30030a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_d_uw },
4137 { 0x30032200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_s_udw },
4138 { 0x30032a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_udw },
4139 /* float/uint truncate */
4140 { 0x30038200, 0xfc1fffe0, FOP_WEW = gen_helper_fcnv_t_s_uw },
4141 { 0x30038a00, 0xfc1fffe0, FOP_WED = gen_helper_fcnv_t_d_uw },
4142 { 0x3003a200, 0xfc1fffe0, FOP_DEW = gen_helper_fcnv_t_s_udw },
4143 { 0x3003aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_udw },
4145 /* floating point class two */
4146 { 0x30000400, 0xfc001fe0, trans_fcmp_s_0c },
4147 { 0x30000c00, 0xfc001fe0, trans_fcmp_d },
4148 { 0x30002420, 0xffffffe0, trans_ftest_q },
4149 { 0x30000420, 0xffff1fff, trans_ftest_t },
4151 /* FID. Note that ra == rt == 0, which via fcpy puts 0 into fr0.
4152 This is machine/revision == 0, which is reserved for simulator. */
4153 { 0x30000000, 0xffffffff, FOP_WEW = gen_fcpy_s },
4156 #undef FOP_WEW
4157 #undef FOP_DEW
4158 #undef FOP_WED
4159 #undef FOP_WEWW
4160 #define FOP_WEW trans_fop_wew_0e, .f.wew
4161 #define FOP_DEW trans_fop_dew_0e, .f.dew
4162 #define FOP_WED trans_fop_wed_0e, .f.wed
4163 #define FOP_WEWW trans_fop_weww_0e, .f.weww
4165 static const DisasInsn table_float_0e[] = {
4166 /* floating point class zero */
4167 { 0x38004000, 0xfc1fff20, FOP_WEW = gen_fcpy_s },
4168 { 0x38006000, 0xfc1fff20, FOP_WEW = gen_fabs_s },
4169 { 0x38008000, 0xfc1fff20, FOP_WEW = gen_helper_fsqrt_s },
4170 { 0x3800a000, 0xfc1fff20, FOP_WEW = gen_helper_frnd_s },
4171 { 0x3800c000, 0xfc1fff20, FOP_WEW = gen_fneg_s },
4172 { 0x3800e000, 0xfc1fff20, FOP_WEW = gen_fnegabs_s },
4174 { 0x38004800, 0xfc1fffe0, FOP_DED = gen_fcpy_d },
4175 { 0x38006800, 0xfc1fffe0, FOP_DED = gen_fabs_d },
4176 { 0x38008800, 0xfc1fffe0, FOP_DED = gen_helper_fsqrt_d },
4177 { 0x3800a800, 0xfc1fffe0, FOP_DED = gen_helper_frnd_d },
4178 { 0x3800c800, 0xfc1fffe0, FOP_DED = gen_fneg_d },
4179 { 0x3800e800, 0xfc1fffe0, FOP_DED = gen_fnegabs_d },
4181 /* floating point class three */
4182 { 0x38000600, 0xfc00ef20, FOP_WEWW = gen_helper_fadd_s },
4183 { 0x38002600, 0xfc00ef20, FOP_WEWW = gen_helper_fsub_s },
4184 { 0x38004600, 0xfc00ef20, FOP_WEWW = gen_helper_fmpy_s },
4185 { 0x38006600, 0xfc00ef20, FOP_WEWW = gen_helper_fdiv_s },
4187 { 0x38000e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fadd_d },
4188 { 0x38002e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fsub_d },
4189 { 0x38004e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fmpy_d },
4190 { 0x38006e00, 0xfc00ffe0, FOP_DEDD = gen_helper_fdiv_d },
4192 { 0x38004700, 0xfc00ef60, trans_xmpyu },
4194 /* floating point class one */
4195 /* float/float */
4196 { 0x38000a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_d_s },
4197 { 0x38002200, 0xfc1fffc0, FOP_DEW = gen_helper_fcnv_s_d },
4198 /* int/float */
4199 { 0x38008200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_w_s },
4200 { 0x38008a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_dw_s },
4201 { 0x3800a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_w_d },
4202 { 0x3800aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_dw_d },
4203 /* float/int */
4204 { 0x38010200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_s_w },
4205 { 0x38010a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_d_w },
4206 { 0x38012200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_s_dw },
4207 { 0x38012a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_dw },
4208 /* float/int truncate */
4209 { 0x38018200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_t_s_w },
4210 { 0x38018a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_t_d_w },
4211 { 0x3801a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_t_s_dw },
4212 { 0x3801aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_dw },
4213 /* uint/float */
4214 { 0x38028200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_uw_s },
4215 { 0x38028a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_udw_s },
4216 { 0x3802a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_uw_d },
4217 { 0x3802aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_udw_d },
4218 /* float/uint */
4219 { 0x38030200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_s_uw },
4220 { 0x38030a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_d_uw },
4221 { 0x38032200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_s_udw },
4222 { 0x38032a00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_d_udw },
4223 /* float/uint truncate */
4224 { 0x38038200, 0xfc1ffe60, FOP_WEW = gen_helper_fcnv_t_s_uw },
4225 { 0x38038a00, 0xfc1fffa0, FOP_WED = gen_helper_fcnv_t_d_uw },
4226 { 0x3803a200, 0xfc1fff60, FOP_DEW = gen_helper_fcnv_t_s_udw },
4227 { 0x3803aa00, 0xfc1fffe0, FOP_DED = gen_helper_fcnv_t_d_udw },
4229 /* floating point class two */
4230 { 0x38000400, 0xfc000f60, trans_fcmp_s_0e },
4231 { 0x38000c00, 0xfc001fe0, trans_fcmp_d },
4234 #undef FOP_WEW
4235 #undef FOP_DEW
4236 #undef FOP_WED
4237 #undef FOP_WEWW
4238 #undef FOP_DED
4239 #undef FOP_DEDD
4241 /* Convert the fmpyadd single-precision register encodings to standard. */
4242 static inline int fmpyadd_s_reg(unsigned r)
4244 return (r & 16) * 2 + 16 + (r & 15);
4247 static DisasJumpType trans_fmpyadd(DisasContext *ctx,
4248 uint32_t insn, bool is_sub)
4250 unsigned tm = extract32(insn, 0, 5);
4251 unsigned f = extract32(insn, 5, 1);
4252 unsigned ra = extract32(insn, 6, 5);
4253 unsigned ta = extract32(insn, 11, 5);
4254 unsigned rm2 = extract32(insn, 16, 5);
4255 unsigned rm1 = extract32(insn, 21, 5);
4257 nullify_over(ctx);
4259 /* Independent multiply & add/sub, with undefined behaviour
4260 if outputs overlap inputs. */
4261 if (f == 0) {
4262 tm = fmpyadd_s_reg(tm);
4263 ra = fmpyadd_s_reg(ra);
4264 ta = fmpyadd_s_reg(ta);
4265 rm2 = fmpyadd_s_reg(rm2);
4266 rm1 = fmpyadd_s_reg(rm1);
4267 do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
4268 do_fop_weww(ctx, ta, ta, ra,
4269 is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
4270 } else {
4271 do_fop_dedd(ctx, tm, rm1, rm2, gen_helper_fmpy_d);
4272 do_fop_dedd(ctx, ta, ta, ra,
4273 is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
4276 return nullify_end(ctx, DISAS_NEXT);
4279 static DisasJumpType trans_fmpyfadd_s(DisasContext *ctx, uint32_t insn,
4280 const DisasInsn *di)
4282 unsigned rt = assemble_rt64(insn);
4283 unsigned neg = extract32(insn, 5, 1);
4284 unsigned rm1 = assemble_ra64(insn);
4285 unsigned rm2 = assemble_rb64(insn);
4286 unsigned ra3 = assemble_rc64(insn);
4287 TCGv_i32 a, b, c;
4289 nullify_over(ctx);
4290 a = load_frw0_i32(rm1);
4291 b = load_frw0_i32(rm2);
4292 c = load_frw0_i32(ra3);
4294 if (neg) {
4295 gen_helper_fmpynfadd_s(a, cpu_env, a, b, c);
4296 } else {
4297 gen_helper_fmpyfadd_s(a, cpu_env, a, b, c);
4300 tcg_temp_free_i32(b);
4301 tcg_temp_free_i32(c);
4302 save_frw_i32(rt, a);
4303 tcg_temp_free_i32(a);
4304 return nullify_end(ctx, DISAS_NEXT);
4307 static DisasJumpType trans_fmpyfadd_d(DisasContext *ctx, uint32_t insn,
4308 const DisasInsn *di)
4310 unsigned rt = extract32(insn, 0, 5);
4311 unsigned neg = extract32(insn, 5, 1);
4312 unsigned rm1 = extract32(insn, 21, 5);
4313 unsigned rm2 = extract32(insn, 16, 5);
4314 unsigned ra3 = assemble_rc64(insn);
4315 TCGv_i64 a, b, c;
4317 nullify_over(ctx);
4318 a = load_frd0(rm1);
4319 b = load_frd0(rm2);
4320 c = load_frd0(ra3);
4322 if (neg) {
4323 gen_helper_fmpynfadd_d(a, cpu_env, a, b, c);
4324 } else {
4325 gen_helper_fmpyfadd_d(a, cpu_env, a, b, c);
4328 tcg_temp_free_i64(b);
4329 tcg_temp_free_i64(c);
4330 save_frd(rt, a);
4331 tcg_temp_free_i64(a);
4332 return nullify_end(ctx, DISAS_NEXT);
4335 static const DisasInsn table_fp_fused[] = {
4336 { 0xb8000000u, 0xfc000800u, trans_fmpyfadd_s },
4337 { 0xb8000800u, 0xfc0019c0u, trans_fmpyfadd_d }
4340 static DisasJumpType translate_table_int(DisasContext *ctx, uint32_t insn,
4341 const DisasInsn table[], size_t n)
4343 size_t i;
4344 for (i = 0; i < n; ++i) {
4345 if ((insn & table[i].mask) == table[i].insn) {
4346 return table[i].trans(ctx, insn, &table[i]);
4349 qemu_log_mask(LOG_UNIMP, "UNIMP insn %08x @ " TARGET_FMT_lx "\n",
4350 insn, ctx->base.pc_next);
4351 return gen_illegal(ctx);
4354 #define translate_table(ctx, insn, table) \
4355 translate_table_int(ctx, insn, table, ARRAY_SIZE(table))
4357 static DisasJumpType translate_one(DisasContext *ctx, uint32_t insn)
4359 uint32_t opc = extract32(insn, 26, 6);
4361 switch (opc) {
4362 case 0x00: /* system op */
4363 return translate_table(ctx, insn, table_system);
4364 case 0x01:
4365 return translate_table(ctx, insn, table_mem_mgmt);
4366 case 0x02:
4367 return translate_table(ctx, insn, table_arith_log);
4368 case 0x03:
4369 return translate_table(ctx, insn, table_index_mem);
4370 case 0x06:
4371 return trans_fmpyadd(ctx, insn, false);
4372 case 0x08:
4373 return trans_ldil(ctx, insn);
4374 case 0x09:
4375 return trans_copr_w(ctx, insn);
4376 case 0x0A:
4377 return trans_addil(ctx, insn);
4378 case 0x0B:
4379 return trans_copr_dw(ctx, insn);
4380 case 0x0C:
4381 return translate_table(ctx, insn, table_float_0c);
4382 case 0x0D:
4383 return trans_ldo(ctx, insn);
4384 case 0x0E:
4385 return translate_table(ctx, insn, table_float_0e);
4387 case 0x10:
4388 return trans_load(ctx, insn, false, MO_UB);
4389 case 0x11:
4390 return trans_load(ctx, insn, false, MO_TEUW);
4391 case 0x12:
4392 return trans_load(ctx, insn, false, MO_TEUL);
4393 case 0x13:
4394 return trans_load(ctx, insn, true, MO_TEUL);
4395 case 0x16:
4396 return trans_fload_mod(ctx, insn);
4397 case 0x17:
4398 return trans_load_w(ctx, insn);
4399 case 0x18:
4400 return trans_store(ctx, insn, false, MO_UB);
4401 case 0x19:
4402 return trans_store(ctx, insn, false, MO_TEUW);
4403 case 0x1A:
4404 return trans_store(ctx, insn, false, MO_TEUL);
4405 case 0x1B:
4406 return trans_store(ctx, insn, true, MO_TEUL);
4407 case 0x1E:
4408 return trans_fstore_mod(ctx, insn);
4409 case 0x1F:
4410 return trans_store_w(ctx, insn);
4412 case 0x20:
4413 return trans_cmpb(ctx, insn, true, false, false);
4414 case 0x21:
4415 return trans_cmpb(ctx, insn, true, true, false);
4416 case 0x22:
4417 return trans_cmpb(ctx, insn, false, false, false);
4418 case 0x23:
4419 return trans_cmpb(ctx, insn, false, true, false);
4420 case 0x24:
4421 return trans_cmpiclr(ctx, insn);
4422 case 0x25:
4423 return trans_subi(ctx, insn);
4424 case 0x26:
4425 return trans_fmpyadd(ctx, insn, true);
4426 case 0x27:
4427 return trans_cmpb(ctx, insn, true, false, true);
4428 case 0x28:
4429 return trans_addb(ctx, insn, true, false);
4430 case 0x29:
4431 return trans_addb(ctx, insn, true, true);
4432 case 0x2A:
4433 return trans_addb(ctx, insn, false, false);
4434 case 0x2B:
4435 return trans_addb(ctx, insn, false, true);
4436 case 0x2C:
4437 case 0x2D:
4438 return trans_addi(ctx, insn);
4439 case 0x2E:
4440 return translate_table(ctx, insn, table_fp_fused);
4441 case 0x2F:
4442 return trans_cmpb(ctx, insn, false, false, true);
4444 case 0x30:
4445 case 0x31:
4446 return trans_bb(ctx, insn);
4447 case 0x32:
4448 return trans_movb(ctx, insn, false);
4449 case 0x33:
4450 return trans_movb(ctx, insn, true);
4451 case 0x34:
4452 return translate_table(ctx, insn, table_sh_ex);
4453 case 0x35:
4454 return translate_table(ctx, insn, table_depw);
4455 case 0x38:
4456 return trans_be(ctx, insn, false);
4457 case 0x39:
4458 return trans_be(ctx, insn, true);
4459 case 0x3A:
4460 return translate_table(ctx, insn, table_branch);
4462 case 0x04: /* spopn */
4463 case 0x05: /* diag */
4464 case 0x0F: /* product specific */
4465 break;
4467 case 0x07: /* unassigned */
4468 case 0x15: /* unassigned */
4469 case 0x1D: /* unassigned */
4470 case 0x37: /* unassigned */
4471 case 0x3F: /* unassigned */
4472 default:
4473 break;
4475 return gen_illegal(ctx);
4478 static int hppa_tr_init_disas_context(DisasContextBase *dcbase,
4479 CPUState *cs, int max_insns)
4481 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4482 int bound;
4484 ctx->cs = cs;
4486 #ifdef CONFIG_USER_ONLY
4487 ctx->privilege = MMU_USER_IDX;
4488 ctx->mmu_idx = MMU_USER_IDX;
4489 ctx->iaoq_f = ctx->base.pc_first;
4490 ctx->iaoq_b = ctx->base.tb->cs_base;
4491 #else
4492 ctx->privilege = (ctx->base.tb->flags >> TB_FLAG_PRIV_SHIFT) & 3;
4493 ctx->mmu_idx = (ctx->base.tb->flags & PSW_D
4494 ? ctx->privilege : MMU_PHYS_IDX);
4496 /* Recover the IAOQ values from the GVA + PRIV. */
4497 uint64_t cs_base = ctx->base.tb->cs_base;
4498 uint64_t iasq_f = cs_base & ~0xffffffffull;
4499 int32_t diff = cs_base;
4501 ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
4502 ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
4503 #endif
4504 ctx->iaoq_n = -1;
4505 ctx->iaoq_n_var = NULL;
4507 /* Bound the number of instructions by those left on the page. */
4508 bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
4509 bound = MIN(max_insns, bound);
4511 ctx->ntempr = 0;
4512 ctx->ntempl = 0;
4513 memset(ctx->tempr, 0, sizeof(ctx->tempr));
4514 memset(ctx->templ, 0, sizeof(ctx->templ));
4516 return bound;
4519 static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
4521 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4523 /* Seed the nullification status from PSW[N], as saved in TB->FLAGS. */
4524 ctx->null_cond = cond_make_f();
4525 ctx->psw_n_nonzero = false;
4526 if (ctx->base.tb->flags & PSW_N) {
4527 ctx->null_cond.c = TCG_COND_ALWAYS;
4528 ctx->psw_n_nonzero = true;
4530 ctx->null_lab = NULL;
4533 static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
4535 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4537 tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b);
4540 static bool hppa_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,
4541 const CPUBreakpoint *bp)
4543 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4545 ctx->base.is_jmp = gen_excp(ctx, EXCP_DEBUG);
4546 ctx->base.pc_next += 4;
4547 return true;
4550 static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
4552 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4553 CPUHPPAState *env = cs->env_ptr;
4554 DisasJumpType ret;
4555 int i, n;
4557 /* Execute one insn. */
4558 #ifdef CONFIG_USER_ONLY
4559 if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
4560 ret = do_page_zero(ctx);
4561 assert(ret != DISAS_NEXT);
4562 } else
4563 #endif
4565 /* Always fetch the insn, even if nullified, so that we check
4566 the page permissions for execute. */
4567 uint32_t insn = cpu_ldl_code(env, ctx->base.pc_next);
4569 /* Set up the IA queue for the next insn.
4570 This will be overwritten by a branch. */
4571 if (ctx->iaoq_b == -1) {
4572 ctx->iaoq_n = -1;
4573 ctx->iaoq_n_var = get_temp(ctx);
4574 tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
4575 } else {
4576 ctx->iaoq_n = ctx->iaoq_b + 4;
4577 ctx->iaoq_n_var = NULL;
4580 if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
4581 ctx->null_cond.c = TCG_COND_NEVER;
4582 ret = DISAS_NEXT;
4583 } else {
4584 ctx->insn = insn;
4585 ret = translate_one(ctx, insn);
4586 assert(ctx->null_lab == NULL);
4590 /* Free any temporaries allocated. */
4591 for (i = 0, n = ctx->ntempr; i < n; ++i) {
4592 tcg_temp_free(ctx->tempr[i]);
4593 ctx->tempr[i] = NULL;
4595 for (i = 0, n = ctx->ntempl; i < n; ++i) {
4596 tcg_temp_free_tl(ctx->templ[i]);
4597 ctx->templ[i] = NULL;
4599 ctx->ntempr = 0;
4600 ctx->ntempl = 0;
4602 /* Advance the insn queue. Note that this check also detects
4603 a priority change within the instruction queue. */
4604 if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
4605 if (ctx->iaoq_b != -1 && ctx->iaoq_n != -1
4606 && use_goto_tb(ctx, ctx->iaoq_b)
4607 && (ctx->null_cond.c == TCG_COND_NEVER
4608 || ctx->null_cond.c == TCG_COND_ALWAYS)) {
4609 nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
4610 gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
4611 ret = DISAS_NORETURN;
4612 } else {
4613 ret = DISAS_IAQ_N_STALE;
4616 ctx->iaoq_f = ctx->iaoq_b;
4617 ctx->iaoq_b = ctx->iaoq_n;
4618 ctx->base.is_jmp = ret;
4619 ctx->base.pc_next += 4;
4621 if (ret == DISAS_NORETURN || ret == DISAS_IAQ_N_UPDATED) {
4622 return;
4624 if (ctx->iaoq_f == -1) {
4625 tcg_gen_mov_reg(cpu_iaoq_f, cpu_iaoq_b);
4626 copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
4627 #ifndef CONFIG_USER_ONLY
4628 tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4629 #endif
4630 nullify_save(ctx);
4631 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
4632 } else if (ctx->iaoq_b == -1) {
4633 tcg_gen_mov_reg(cpu_iaoq_b, ctx->iaoq_n_var);
4637 static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
4639 DisasContext *ctx = container_of(dcbase, DisasContext, base);
4640 DisasJumpType is_jmp = ctx->base.is_jmp;
4642 switch (is_jmp) {
4643 case DISAS_NORETURN:
4644 break;
4645 case DISAS_TOO_MANY:
4646 case DISAS_IAQ_N_STALE:
4647 case DISAS_IAQ_N_STALE_EXIT:
4648 copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
4649 copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
4650 nullify_save(ctx);
4651 /* FALLTHRU */
4652 case DISAS_IAQ_N_UPDATED:
4653 if (ctx->base.singlestep_enabled) {
4654 gen_excp_1(EXCP_DEBUG);
4655 } else if (is_jmp == DISAS_IAQ_N_STALE_EXIT) {
4656 tcg_gen_exit_tb(0);
4657 } else {
4658 tcg_gen_lookup_and_goto_ptr();
4660 break;
4661 default:
4662 g_assert_not_reached();
4666 static void hppa_tr_disas_log(const DisasContextBase *dcbase, CPUState *cs)
4668 target_ulong pc = dcbase->pc_first;
4670 #ifdef CONFIG_USER_ONLY
4671 switch (pc) {
4672 case 0x00:
4673 qemu_log("IN:\n0x00000000: (null)\n");
4674 return;
4675 case 0xb0:
4676 qemu_log("IN:\n0x000000b0: light-weight-syscall\n");
4677 return;
4678 case 0xe0:
4679 qemu_log("IN:\n0x000000e0: set-thread-pointer-syscall\n");
4680 return;
4681 case 0x100:
4682 qemu_log("IN:\n0x00000100: syscall\n");
4683 return;
4685 #endif
4687 qemu_log("IN: %s\n", lookup_symbol(pc));
4688 log_target_disas(cs, pc, dcbase->tb->size);
4691 static const TranslatorOps hppa_tr_ops = {
4692 .init_disas_context = hppa_tr_init_disas_context,
4693 .tb_start = hppa_tr_tb_start,
4694 .insn_start = hppa_tr_insn_start,
4695 .breakpoint_check = hppa_tr_breakpoint_check,
4696 .translate_insn = hppa_tr_translate_insn,
4697 .tb_stop = hppa_tr_tb_stop,
4698 .disas_log = hppa_tr_disas_log,
4701 void gen_intermediate_code(CPUState *cs, struct TranslationBlock *tb)
4704 DisasContext ctx;
4705 translator_loop(&hppa_tr_ops, &ctx.base, cs, tb);
4708 void restore_state_to_opc(CPUHPPAState *env, TranslationBlock *tb,
4709 target_ulong *data)
4711 env->iaoq_f = data[0];
4712 if (data[1] != (target_ureg)-1) {
4713 env->iaoq_b = data[1];
4715 /* Since we were executing the instruction at IAOQ_F, and took some
4716 sort of action that provoked the cpu_restore_state, we can infer
4717 that the instruction was not nullified. */
4718 env->psw_n = 0;