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