xref: /openbmc/qemu/target/hppa/translate.c (revision 0786a3b6051ed081ddaa8dfe1c1e13ce0cfabc4a)
161766fe9SRichard Henderson /*
261766fe9SRichard Henderson  * HPPA emulation cpu translation for qemu.
361766fe9SRichard Henderson  *
461766fe9SRichard Henderson  * Copyright (c) 2016 Richard Henderson <rth@twiddle.net>
561766fe9SRichard Henderson  *
661766fe9SRichard Henderson  * This library is free software; you can redistribute it and/or
761766fe9SRichard Henderson  * modify it under the terms of the GNU Lesser General Public
861766fe9SRichard Henderson  * License as published by the Free Software Foundation; either
9d6ea4236SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
1061766fe9SRichard Henderson  *
1161766fe9SRichard Henderson  * This library is distributed in the hope that it will be useful,
1261766fe9SRichard Henderson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1361766fe9SRichard Henderson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1461766fe9SRichard Henderson  * Lesser General Public License for more details.
1561766fe9SRichard Henderson  *
1661766fe9SRichard Henderson  * You should have received a copy of the GNU Lesser General Public
1761766fe9SRichard Henderson  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1861766fe9SRichard Henderson  */
1961766fe9SRichard Henderson 
2061766fe9SRichard Henderson #include "qemu/osdep.h"
2161766fe9SRichard Henderson #include "cpu.h"
2261766fe9SRichard Henderson #include "disas/disas.h"
2361766fe9SRichard Henderson #include "qemu/host-utils.h"
2461766fe9SRichard Henderson #include "exec/exec-all.h"
25dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
2661766fe9SRichard Henderson #include "exec/cpu_ldst.h"
2761766fe9SRichard Henderson #include "exec/helper-proto.h"
2861766fe9SRichard Henderson #include "exec/helper-gen.h"
29869051eaSRichard Henderson #include "exec/translator.h"
3061766fe9SRichard Henderson #include "exec/log.h"
3161766fe9SRichard Henderson 
32eaa3783bSRichard Henderson /* Since we have a distinction between register size and address size,
33eaa3783bSRichard Henderson    we need to redefine all of these.  */
34eaa3783bSRichard Henderson 
35eaa3783bSRichard Henderson #undef TCGv
36eaa3783bSRichard Henderson #undef tcg_temp_new
37eaa3783bSRichard Henderson #undef tcg_global_mem_new
38eaa3783bSRichard Henderson #undef tcg_temp_local_new
39eaa3783bSRichard Henderson #undef tcg_temp_free
40eaa3783bSRichard Henderson 
41eaa3783bSRichard Henderson #if TARGET_LONG_BITS == 64
42eaa3783bSRichard Henderson #define TCGv_tl              TCGv_i64
43eaa3783bSRichard Henderson #define tcg_temp_new_tl      tcg_temp_new_i64
44eaa3783bSRichard Henderson #define tcg_temp_free_tl     tcg_temp_free_i64
45eaa3783bSRichard Henderson #if TARGET_REGISTER_BITS == 64
46eaa3783bSRichard Henderson #define tcg_gen_extu_reg_tl  tcg_gen_mov_i64
47eaa3783bSRichard Henderson #else
48eaa3783bSRichard Henderson #define tcg_gen_extu_reg_tl  tcg_gen_extu_i32_i64
49eaa3783bSRichard Henderson #endif
50eaa3783bSRichard Henderson #else
51eaa3783bSRichard Henderson #define TCGv_tl              TCGv_i32
52eaa3783bSRichard Henderson #define tcg_temp_new_tl      tcg_temp_new_i32
53eaa3783bSRichard Henderson #define tcg_temp_free_tl     tcg_temp_free_i32
54eaa3783bSRichard Henderson #define tcg_gen_extu_reg_tl  tcg_gen_mov_i32
55eaa3783bSRichard Henderson #endif
56eaa3783bSRichard Henderson 
57eaa3783bSRichard Henderson #if TARGET_REGISTER_BITS == 64
58eaa3783bSRichard Henderson #define TCGv_reg             TCGv_i64
59eaa3783bSRichard Henderson 
60eaa3783bSRichard Henderson #define tcg_temp_new         tcg_temp_new_i64
61eaa3783bSRichard Henderson #define tcg_global_mem_new   tcg_global_mem_new_i64
62eaa3783bSRichard Henderson #define tcg_temp_local_new   tcg_temp_local_new_i64
63eaa3783bSRichard Henderson #define tcg_temp_free        tcg_temp_free_i64
64eaa3783bSRichard Henderson 
65eaa3783bSRichard Henderson #define tcg_gen_movi_reg     tcg_gen_movi_i64
66eaa3783bSRichard Henderson #define tcg_gen_mov_reg      tcg_gen_mov_i64
67eaa3783bSRichard Henderson #define tcg_gen_ld8u_reg     tcg_gen_ld8u_i64
68eaa3783bSRichard Henderson #define tcg_gen_ld8s_reg     tcg_gen_ld8s_i64
69eaa3783bSRichard Henderson #define tcg_gen_ld16u_reg    tcg_gen_ld16u_i64
70eaa3783bSRichard Henderson #define tcg_gen_ld16s_reg    tcg_gen_ld16s_i64
71eaa3783bSRichard Henderson #define tcg_gen_ld32u_reg    tcg_gen_ld32u_i64
72eaa3783bSRichard Henderson #define tcg_gen_ld32s_reg    tcg_gen_ld32s_i64
73eaa3783bSRichard Henderson #define tcg_gen_ld_reg       tcg_gen_ld_i64
74eaa3783bSRichard Henderson #define tcg_gen_st8_reg      tcg_gen_st8_i64
75eaa3783bSRichard Henderson #define tcg_gen_st16_reg     tcg_gen_st16_i64
76eaa3783bSRichard Henderson #define tcg_gen_st32_reg     tcg_gen_st32_i64
77eaa3783bSRichard Henderson #define tcg_gen_st_reg       tcg_gen_st_i64
78eaa3783bSRichard Henderson #define tcg_gen_add_reg      tcg_gen_add_i64
79eaa3783bSRichard Henderson #define tcg_gen_addi_reg     tcg_gen_addi_i64
80eaa3783bSRichard Henderson #define tcg_gen_sub_reg      tcg_gen_sub_i64
81eaa3783bSRichard Henderson #define tcg_gen_neg_reg      tcg_gen_neg_i64
82eaa3783bSRichard Henderson #define tcg_gen_subfi_reg    tcg_gen_subfi_i64
83eaa3783bSRichard Henderson #define tcg_gen_subi_reg     tcg_gen_subi_i64
84eaa3783bSRichard Henderson #define tcg_gen_and_reg      tcg_gen_and_i64
85eaa3783bSRichard Henderson #define tcg_gen_andi_reg     tcg_gen_andi_i64
86eaa3783bSRichard Henderson #define tcg_gen_or_reg       tcg_gen_or_i64
87eaa3783bSRichard Henderson #define tcg_gen_ori_reg      tcg_gen_ori_i64
88eaa3783bSRichard Henderson #define tcg_gen_xor_reg      tcg_gen_xor_i64
89eaa3783bSRichard Henderson #define tcg_gen_xori_reg     tcg_gen_xori_i64
90eaa3783bSRichard Henderson #define tcg_gen_not_reg      tcg_gen_not_i64
91eaa3783bSRichard Henderson #define tcg_gen_shl_reg      tcg_gen_shl_i64
92eaa3783bSRichard Henderson #define tcg_gen_shli_reg     tcg_gen_shli_i64
93eaa3783bSRichard Henderson #define tcg_gen_shr_reg      tcg_gen_shr_i64
94eaa3783bSRichard Henderson #define tcg_gen_shri_reg     tcg_gen_shri_i64
95eaa3783bSRichard Henderson #define tcg_gen_sar_reg      tcg_gen_sar_i64
96eaa3783bSRichard Henderson #define tcg_gen_sari_reg     tcg_gen_sari_i64
97eaa3783bSRichard Henderson #define tcg_gen_brcond_reg   tcg_gen_brcond_i64
98eaa3783bSRichard Henderson #define tcg_gen_brcondi_reg  tcg_gen_brcondi_i64
99eaa3783bSRichard Henderson #define tcg_gen_setcond_reg  tcg_gen_setcond_i64
100eaa3783bSRichard Henderson #define tcg_gen_setcondi_reg tcg_gen_setcondi_i64
101eaa3783bSRichard Henderson #define tcg_gen_mul_reg      tcg_gen_mul_i64
102eaa3783bSRichard Henderson #define tcg_gen_muli_reg     tcg_gen_muli_i64
103eaa3783bSRichard Henderson #define tcg_gen_div_reg      tcg_gen_div_i64
104eaa3783bSRichard Henderson #define tcg_gen_rem_reg      tcg_gen_rem_i64
105eaa3783bSRichard Henderson #define tcg_gen_divu_reg     tcg_gen_divu_i64
106eaa3783bSRichard Henderson #define tcg_gen_remu_reg     tcg_gen_remu_i64
107eaa3783bSRichard Henderson #define tcg_gen_discard_reg  tcg_gen_discard_i64
108eaa3783bSRichard Henderson #define tcg_gen_trunc_reg_i32 tcg_gen_extrl_i64_i32
109eaa3783bSRichard Henderson #define tcg_gen_trunc_i64_reg tcg_gen_mov_i64
110eaa3783bSRichard Henderson #define tcg_gen_extu_i32_reg tcg_gen_extu_i32_i64
111eaa3783bSRichard Henderson #define tcg_gen_ext_i32_reg  tcg_gen_ext_i32_i64
112eaa3783bSRichard Henderson #define tcg_gen_extu_reg_i64 tcg_gen_mov_i64
113eaa3783bSRichard Henderson #define tcg_gen_ext_reg_i64  tcg_gen_mov_i64
114eaa3783bSRichard Henderson #define tcg_gen_ext8u_reg    tcg_gen_ext8u_i64
115eaa3783bSRichard Henderson #define tcg_gen_ext8s_reg    tcg_gen_ext8s_i64
116eaa3783bSRichard Henderson #define tcg_gen_ext16u_reg   tcg_gen_ext16u_i64
117eaa3783bSRichard Henderson #define tcg_gen_ext16s_reg   tcg_gen_ext16s_i64
118eaa3783bSRichard Henderson #define tcg_gen_ext32u_reg   tcg_gen_ext32u_i64
119eaa3783bSRichard Henderson #define tcg_gen_ext32s_reg   tcg_gen_ext32s_i64
120eaa3783bSRichard Henderson #define tcg_gen_bswap16_reg  tcg_gen_bswap16_i64
121eaa3783bSRichard Henderson #define tcg_gen_bswap32_reg  tcg_gen_bswap32_i64
122eaa3783bSRichard Henderson #define tcg_gen_bswap64_reg  tcg_gen_bswap64_i64
123eaa3783bSRichard Henderson #define tcg_gen_concat_reg_i64 tcg_gen_concat32_i64
124eaa3783bSRichard Henderson #define tcg_gen_andc_reg     tcg_gen_andc_i64
125eaa3783bSRichard Henderson #define tcg_gen_eqv_reg      tcg_gen_eqv_i64
126eaa3783bSRichard Henderson #define tcg_gen_nand_reg     tcg_gen_nand_i64
127eaa3783bSRichard Henderson #define tcg_gen_nor_reg      tcg_gen_nor_i64
128eaa3783bSRichard Henderson #define tcg_gen_orc_reg      tcg_gen_orc_i64
129eaa3783bSRichard Henderson #define tcg_gen_clz_reg      tcg_gen_clz_i64
130eaa3783bSRichard Henderson #define tcg_gen_ctz_reg      tcg_gen_ctz_i64
131eaa3783bSRichard Henderson #define tcg_gen_clzi_reg     tcg_gen_clzi_i64
132eaa3783bSRichard Henderson #define tcg_gen_ctzi_reg     tcg_gen_ctzi_i64
133eaa3783bSRichard Henderson #define tcg_gen_clrsb_reg    tcg_gen_clrsb_i64
134eaa3783bSRichard Henderson #define tcg_gen_ctpop_reg    tcg_gen_ctpop_i64
135eaa3783bSRichard Henderson #define tcg_gen_rotl_reg     tcg_gen_rotl_i64
136eaa3783bSRichard Henderson #define tcg_gen_rotli_reg    tcg_gen_rotli_i64
137eaa3783bSRichard Henderson #define tcg_gen_rotr_reg     tcg_gen_rotr_i64
138eaa3783bSRichard Henderson #define tcg_gen_rotri_reg    tcg_gen_rotri_i64
139eaa3783bSRichard Henderson #define tcg_gen_deposit_reg  tcg_gen_deposit_i64
140eaa3783bSRichard Henderson #define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i64
141eaa3783bSRichard Henderson #define tcg_gen_extract_reg  tcg_gen_extract_i64
142eaa3783bSRichard Henderson #define tcg_gen_sextract_reg tcg_gen_sextract_i64
14305bfd4dbSRichard Henderson #define tcg_gen_extract2_reg tcg_gen_extract2_i64
144eaa3783bSRichard Henderson #define tcg_const_reg        tcg_const_i64
145eaa3783bSRichard Henderson #define tcg_const_local_reg  tcg_const_local_i64
14629dd6f64SRichard Henderson #define tcg_constant_reg     tcg_constant_i64
147eaa3783bSRichard Henderson #define tcg_gen_movcond_reg  tcg_gen_movcond_i64
148eaa3783bSRichard Henderson #define tcg_gen_add2_reg     tcg_gen_add2_i64
149eaa3783bSRichard Henderson #define tcg_gen_sub2_reg     tcg_gen_sub2_i64
150eaa3783bSRichard Henderson #define tcg_gen_qemu_ld_reg  tcg_gen_qemu_ld_i64
151eaa3783bSRichard Henderson #define tcg_gen_qemu_st_reg  tcg_gen_qemu_st_i64
152eaa3783bSRichard Henderson #define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i64
1535bfa8034SRichard Henderson #define tcg_gen_trunc_reg_ptr   tcg_gen_trunc_i64_ptr
154eaa3783bSRichard Henderson #else
155eaa3783bSRichard Henderson #define TCGv_reg             TCGv_i32
156eaa3783bSRichard Henderson #define tcg_temp_new         tcg_temp_new_i32
157eaa3783bSRichard Henderson #define tcg_global_mem_new   tcg_global_mem_new_i32
158eaa3783bSRichard Henderson #define tcg_temp_local_new   tcg_temp_local_new_i32
159eaa3783bSRichard Henderson #define tcg_temp_free        tcg_temp_free_i32
160eaa3783bSRichard Henderson 
161eaa3783bSRichard Henderson #define tcg_gen_movi_reg     tcg_gen_movi_i32
162eaa3783bSRichard Henderson #define tcg_gen_mov_reg      tcg_gen_mov_i32
163eaa3783bSRichard Henderson #define tcg_gen_ld8u_reg     tcg_gen_ld8u_i32
164eaa3783bSRichard Henderson #define tcg_gen_ld8s_reg     tcg_gen_ld8s_i32
165eaa3783bSRichard Henderson #define tcg_gen_ld16u_reg    tcg_gen_ld16u_i32
166eaa3783bSRichard Henderson #define tcg_gen_ld16s_reg    tcg_gen_ld16s_i32
167eaa3783bSRichard Henderson #define tcg_gen_ld32u_reg    tcg_gen_ld_i32
168eaa3783bSRichard Henderson #define tcg_gen_ld32s_reg    tcg_gen_ld_i32
169eaa3783bSRichard Henderson #define tcg_gen_ld_reg       tcg_gen_ld_i32
170eaa3783bSRichard Henderson #define tcg_gen_st8_reg      tcg_gen_st8_i32
171eaa3783bSRichard Henderson #define tcg_gen_st16_reg     tcg_gen_st16_i32
172eaa3783bSRichard Henderson #define tcg_gen_st32_reg     tcg_gen_st32_i32
173eaa3783bSRichard Henderson #define tcg_gen_st_reg       tcg_gen_st_i32
174eaa3783bSRichard Henderson #define tcg_gen_add_reg      tcg_gen_add_i32
175eaa3783bSRichard Henderson #define tcg_gen_addi_reg     tcg_gen_addi_i32
176eaa3783bSRichard Henderson #define tcg_gen_sub_reg      tcg_gen_sub_i32
177eaa3783bSRichard Henderson #define tcg_gen_neg_reg      tcg_gen_neg_i32
178eaa3783bSRichard Henderson #define tcg_gen_subfi_reg    tcg_gen_subfi_i32
179eaa3783bSRichard Henderson #define tcg_gen_subi_reg     tcg_gen_subi_i32
180eaa3783bSRichard Henderson #define tcg_gen_and_reg      tcg_gen_and_i32
181eaa3783bSRichard Henderson #define tcg_gen_andi_reg     tcg_gen_andi_i32
182eaa3783bSRichard Henderson #define tcg_gen_or_reg       tcg_gen_or_i32
183eaa3783bSRichard Henderson #define tcg_gen_ori_reg      tcg_gen_ori_i32
184eaa3783bSRichard Henderson #define tcg_gen_xor_reg      tcg_gen_xor_i32
185eaa3783bSRichard Henderson #define tcg_gen_xori_reg     tcg_gen_xori_i32
186eaa3783bSRichard Henderson #define tcg_gen_not_reg      tcg_gen_not_i32
187eaa3783bSRichard Henderson #define tcg_gen_shl_reg      tcg_gen_shl_i32
188eaa3783bSRichard Henderson #define tcg_gen_shli_reg     tcg_gen_shli_i32
189eaa3783bSRichard Henderson #define tcg_gen_shr_reg      tcg_gen_shr_i32
190eaa3783bSRichard Henderson #define tcg_gen_shri_reg     tcg_gen_shri_i32
191eaa3783bSRichard Henderson #define tcg_gen_sar_reg      tcg_gen_sar_i32
192eaa3783bSRichard Henderson #define tcg_gen_sari_reg     tcg_gen_sari_i32
193eaa3783bSRichard Henderson #define tcg_gen_brcond_reg   tcg_gen_brcond_i32
194eaa3783bSRichard Henderson #define tcg_gen_brcondi_reg  tcg_gen_brcondi_i32
195eaa3783bSRichard Henderson #define tcg_gen_setcond_reg  tcg_gen_setcond_i32
196eaa3783bSRichard Henderson #define tcg_gen_setcondi_reg tcg_gen_setcondi_i32
197eaa3783bSRichard Henderson #define tcg_gen_mul_reg      tcg_gen_mul_i32
198eaa3783bSRichard Henderson #define tcg_gen_muli_reg     tcg_gen_muli_i32
199eaa3783bSRichard Henderson #define tcg_gen_div_reg      tcg_gen_div_i32
200eaa3783bSRichard Henderson #define tcg_gen_rem_reg      tcg_gen_rem_i32
201eaa3783bSRichard Henderson #define tcg_gen_divu_reg     tcg_gen_divu_i32
202eaa3783bSRichard Henderson #define tcg_gen_remu_reg     tcg_gen_remu_i32
203eaa3783bSRichard Henderson #define tcg_gen_discard_reg  tcg_gen_discard_i32
204eaa3783bSRichard Henderson #define tcg_gen_trunc_reg_i32 tcg_gen_mov_i32
205eaa3783bSRichard Henderson #define tcg_gen_trunc_i64_reg tcg_gen_extrl_i64_i32
206eaa3783bSRichard Henderson #define tcg_gen_extu_i32_reg tcg_gen_mov_i32
207eaa3783bSRichard Henderson #define tcg_gen_ext_i32_reg  tcg_gen_mov_i32
208eaa3783bSRichard Henderson #define tcg_gen_extu_reg_i64 tcg_gen_extu_i32_i64
209eaa3783bSRichard Henderson #define tcg_gen_ext_reg_i64  tcg_gen_ext_i32_i64
210eaa3783bSRichard Henderson #define tcg_gen_ext8u_reg    tcg_gen_ext8u_i32
211eaa3783bSRichard Henderson #define tcg_gen_ext8s_reg    tcg_gen_ext8s_i32
212eaa3783bSRichard Henderson #define tcg_gen_ext16u_reg   tcg_gen_ext16u_i32
213eaa3783bSRichard Henderson #define tcg_gen_ext16s_reg   tcg_gen_ext16s_i32
214eaa3783bSRichard Henderson #define tcg_gen_ext32u_reg   tcg_gen_mov_i32
215eaa3783bSRichard Henderson #define tcg_gen_ext32s_reg   tcg_gen_mov_i32
216eaa3783bSRichard Henderson #define tcg_gen_bswap16_reg  tcg_gen_bswap16_i32
217eaa3783bSRichard Henderson #define tcg_gen_bswap32_reg  tcg_gen_bswap32_i32
218eaa3783bSRichard Henderson #define tcg_gen_concat_reg_i64 tcg_gen_concat_i32_i64
219eaa3783bSRichard Henderson #define tcg_gen_andc_reg     tcg_gen_andc_i32
220eaa3783bSRichard Henderson #define tcg_gen_eqv_reg      tcg_gen_eqv_i32
221eaa3783bSRichard Henderson #define tcg_gen_nand_reg     tcg_gen_nand_i32
222eaa3783bSRichard Henderson #define tcg_gen_nor_reg      tcg_gen_nor_i32
223eaa3783bSRichard Henderson #define tcg_gen_orc_reg      tcg_gen_orc_i32
224eaa3783bSRichard Henderson #define tcg_gen_clz_reg      tcg_gen_clz_i32
225eaa3783bSRichard Henderson #define tcg_gen_ctz_reg      tcg_gen_ctz_i32
226eaa3783bSRichard Henderson #define tcg_gen_clzi_reg     tcg_gen_clzi_i32
227eaa3783bSRichard Henderson #define tcg_gen_ctzi_reg     tcg_gen_ctzi_i32
228eaa3783bSRichard Henderson #define tcg_gen_clrsb_reg    tcg_gen_clrsb_i32
229eaa3783bSRichard Henderson #define tcg_gen_ctpop_reg    tcg_gen_ctpop_i32
230eaa3783bSRichard Henderson #define tcg_gen_rotl_reg     tcg_gen_rotl_i32
231eaa3783bSRichard Henderson #define tcg_gen_rotli_reg    tcg_gen_rotli_i32
232eaa3783bSRichard Henderson #define tcg_gen_rotr_reg     tcg_gen_rotr_i32
233eaa3783bSRichard Henderson #define tcg_gen_rotri_reg    tcg_gen_rotri_i32
234eaa3783bSRichard Henderson #define tcg_gen_deposit_reg  tcg_gen_deposit_i32
235eaa3783bSRichard Henderson #define tcg_gen_deposit_z_reg tcg_gen_deposit_z_i32
236eaa3783bSRichard Henderson #define tcg_gen_extract_reg  tcg_gen_extract_i32
237eaa3783bSRichard Henderson #define tcg_gen_sextract_reg tcg_gen_sextract_i32
23805bfd4dbSRichard Henderson #define tcg_gen_extract2_reg tcg_gen_extract2_i32
239eaa3783bSRichard Henderson #define tcg_const_reg        tcg_const_i32
240eaa3783bSRichard Henderson #define tcg_const_local_reg  tcg_const_local_i32
24129dd6f64SRichard Henderson #define tcg_constant_reg     tcg_constant_i32
242eaa3783bSRichard Henderson #define tcg_gen_movcond_reg  tcg_gen_movcond_i32
243eaa3783bSRichard Henderson #define tcg_gen_add2_reg     tcg_gen_add2_i32
244eaa3783bSRichard Henderson #define tcg_gen_sub2_reg     tcg_gen_sub2_i32
245eaa3783bSRichard Henderson #define tcg_gen_qemu_ld_reg  tcg_gen_qemu_ld_i32
246eaa3783bSRichard Henderson #define tcg_gen_qemu_st_reg  tcg_gen_qemu_st_i32
247eaa3783bSRichard Henderson #define tcg_gen_atomic_xchg_reg tcg_gen_atomic_xchg_i32
2485bfa8034SRichard Henderson #define tcg_gen_trunc_reg_ptr   tcg_gen_ext_i32_ptr
249eaa3783bSRichard Henderson #endif /* TARGET_REGISTER_BITS */
250eaa3783bSRichard Henderson 
25161766fe9SRichard Henderson typedef struct DisasCond {
25261766fe9SRichard Henderson     TCGCond c;
253eaa3783bSRichard Henderson     TCGv_reg a0, a1;
25461766fe9SRichard Henderson } DisasCond;
25561766fe9SRichard Henderson 
25661766fe9SRichard Henderson typedef struct DisasContext {
257d01a3625SRichard Henderson     DisasContextBase base;
25861766fe9SRichard Henderson     CPUState *cs;
25961766fe9SRichard Henderson 
260eaa3783bSRichard Henderson     target_ureg iaoq_f;
261eaa3783bSRichard Henderson     target_ureg iaoq_b;
262eaa3783bSRichard Henderson     target_ureg iaoq_n;
263eaa3783bSRichard Henderson     TCGv_reg iaoq_n_var;
26461766fe9SRichard Henderson 
26586f8d05fSRichard Henderson     int ntempr, ntempl;
2665eecd37aSRichard Henderson     TCGv_reg tempr[8];
26786f8d05fSRichard Henderson     TCGv_tl  templ[4];
26861766fe9SRichard Henderson 
26961766fe9SRichard Henderson     DisasCond null_cond;
27061766fe9SRichard Henderson     TCGLabel *null_lab;
27161766fe9SRichard Henderson 
2721a19da0dSRichard Henderson     uint32_t insn;
273494737b7SRichard Henderson     uint32_t tb_flags;
2743d68ee7bSRichard Henderson     int mmu_idx;
2753d68ee7bSRichard Henderson     int privilege;
27661766fe9SRichard Henderson     bool psw_n_nonzero;
277217d1a5eSRichard Henderson 
278217d1a5eSRichard Henderson #ifdef CONFIG_USER_ONLY
279217d1a5eSRichard Henderson     MemOp unalign;
280217d1a5eSRichard Henderson #endif
28161766fe9SRichard Henderson } DisasContext;
28261766fe9SRichard Henderson 
283217d1a5eSRichard Henderson #ifdef CONFIG_USER_ONLY
284217d1a5eSRichard Henderson #define UNALIGN(C)  (C)->unalign
285217d1a5eSRichard Henderson #else
286217d1a5eSRichard Henderson #define UNALIGN(C)  0
287217d1a5eSRichard Henderson #endif
288217d1a5eSRichard Henderson 
289e36f27efSRichard Henderson /* Note that ssm/rsm instructions number PSW_W and PSW_E differently.  */
290451e4ffdSRichard Henderson static int expand_sm_imm(DisasContext *ctx, int val)
291e36f27efSRichard Henderson {
292e36f27efSRichard Henderson     if (val & PSW_SM_E) {
293e36f27efSRichard Henderson         val = (val & ~PSW_SM_E) | PSW_E;
294e36f27efSRichard Henderson     }
295e36f27efSRichard Henderson     if (val & PSW_SM_W) {
296e36f27efSRichard Henderson         val = (val & ~PSW_SM_W) | PSW_W;
297e36f27efSRichard Henderson     }
298e36f27efSRichard Henderson     return val;
299e36f27efSRichard Henderson }
300e36f27efSRichard Henderson 
301deee69a1SRichard Henderson /* Inverted space register indicates 0 means sr0 not inferred from base.  */
302451e4ffdSRichard Henderson static int expand_sr3x(DisasContext *ctx, int val)
303deee69a1SRichard Henderson {
304deee69a1SRichard Henderson     return ~val;
305deee69a1SRichard Henderson }
306deee69a1SRichard Henderson 
3071cd012a5SRichard Henderson /* Convert the M:A bits within a memory insn to the tri-state value
3081cd012a5SRichard Henderson    we use for the final M.  */
309451e4ffdSRichard Henderson static int ma_to_m(DisasContext *ctx, int val)
3101cd012a5SRichard Henderson {
3111cd012a5SRichard Henderson     return val & 2 ? (val & 1 ? -1 : 1) : 0;
3121cd012a5SRichard Henderson }
3131cd012a5SRichard Henderson 
314740038d7SRichard Henderson /* Convert the sign of the displacement to a pre or post-modify.  */
315451e4ffdSRichard Henderson static int pos_to_m(DisasContext *ctx, int val)
316740038d7SRichard Henderson {
317740038d7SRichard Henderson     return val ? 1 : -1;
318740038d7SRichard Henderson }
319740038d7SRichard Henderson 
320451e4ffdSRichard Henderson static int neg_to_m(DisasContext *ctx, int val)
321740038d7SRichard Henderson {
322740038d7SRichard Henderson     return val ? -1 : 1;
323740038d7SRichard Henderson }
324740038d7SRichard Henderson 
325740038d7SRichard Henderson /* Used for branch targets and fp memory ops.  */
326451e4ffdSRichard Henderson static int expand_shl2(DisasContext *ctx, int val)
32701afb7beSRichard Henderson {
32801afb7beSRichard Henderson     return val << 2;
32901afb7beSRichard Henderson }
33001afb7beSRichard Henderson 
331740038d7SRichard Henderson /* Used for fp memory ops.  */
332451e4ffdSRichard Henderson static int expand_shl3(DisasContext *ctx, int val)
333740038d7SRichard Henderson {
334740038d7SRichard Henderson     return val << 3;
335740038d7SRichard Henderson }
336740038d7SRichard Henderson 
3370588e061SRichard Henderson /* Used for assemble_21.  */
338451e4ffdSRichard Henderson static int expand_shl11(DisasContext *ctx, int val)
3390588e061SRichard Henderson {
3400588e061SRichard Henderson     return val << 11;
3410588e061SRichard Henderson }
3420588e061SRichard Henderson 
34301afb7beSRichard Henderson 
34440f9f908SRichard Henderson /* Include the auto-generated decoder.  */
345abff1abfSPaolo Bonzini #include "decode-insns.c.inc"
34640f9f908SRichard Henderson 
34761766fe9SRichard Henderson /* We are not using a goto_tb (for whatever reason), but have updated
34861766fe9SRichard Henderson    the iaq (for whatever reason), so don't do it again on exit.  */
349869051eaSRichard Henderson #define DISAS_IAQ_N_UPDATED  DISAS_TARGET_0
35061766fe9SRichard Henderson 
35161766fe9SRichard Henderson /* We are exiting the TB, but have neither emitted a goto_tb, nor
35261766fe9SRichard Henderson    updated the iaq for the next instruction to be executed.  */
353869051eaSRichard Henderson #define DISAS_IAQ_N_STALE    DISAS_TARGET_1
35461766fe9SRichard Henderson 
355e1b5a5edSRichard Henderson /* Similarly, but we want to return to the main loop immediately
356e1b5a5edSRichard Henderson    to recognize unmasked interrupts.  */
357e1b5a5edSRichard Henderson #define DISAS_IAQ_N_STALE_EXIT      DISAS_TARGET_2
358c5d0aec2SRichard Henderson #define DISAS_EXIT                  DISAS_TARGET_3
359e1b5a5edSRichard Henderson 
36061766fe9SRichard Henderson /* global register indexes */
361eaa3783bSRichard Henderson static TCGv_reg cpu_gr[32];
36233423472SRichard Henderson static TCGv_i64 cpu_sr[4];
363494737b7SRichard Henderson static TCGv_i64 cpu_srH;
364eaa3783bSRichard Henderson static TCGv_reg cpu_iaoq_f;
365eaa3783bSRichard Henderson static TCGv_reg cpu_iaoq_b;
366c301f34eSRichard Henderson static TCGv_i64 cpu_iasq_f;
367c301f34eSRichard Henderson static TCGv_i64 cpu_iasq_b;
368eaa3783bSRichard Henderson static TCGv_reg cpu_sar;
369eaa3783bSRichard Henderson static TCGv_reg cpu_psw_n;
370eaa3783bSRichard Henderson static TCGv_reg cpu_psw_v;
371eaa3783bSRichard Henderson static TCGv_reg cpu_psw_cb;
372eaa3783bSRichard Henderson static TCGv_reg cpu_psw_cb_msb;
37361766fe9SRichard Henderson 
37461766fe9SRichard Henderson #include "exec/gen-icount.h"
37561766fe9SRichard Henderson 
37661766fe9SRichard Henderson void hppa_translate_init(void)
37761766fe9SRichard Henderson {
37861766fe9SRichard Henderson #define DEF_VAR(V)  { &cpu_##V, #V, offsetof(CPUHPPAState, V) }
37961766fe9SRichard Henderson 
380eaa3783bSRichard Henderson     typedef struct { TCGv_reg *var; const char *name; int ofs; } GlobalVar;
38161766fe9SRichard Henderson     static const GlobalVar vars[] = {
38235136a77SRichard Henderson         { &cpu_sar, "sar", offsetof(CPUHPPAState, cr[CR_SAR]) },
38361766fe9SRichard Henderson         DEF_VAR(psw_n),
38461766fe9SRichard Henderson         DEF_VAR(psw_v),
38561766fe9SRichard Henderson         DEF_VAR(psw_cb),
38661766fe9SRichard Henderson         DEF_VAR(psw_cb_msb),
38761766fe9SRichard Henderson         DEF_VAR(iaoq_f),
38861766fe9SRichard Henderson         DEF_VAR(iaoq_b),
38961766fe9SRichard Henderson     };
39061766fe9SRichard Henderson 
39161766fe9SRichard Henderson #undef DEF_VAR
39261766fe9SRichard Henderson 
39361766fe9SRichard Henderson     /* Use the symbolic register names that match the disassembler.  */
39461766fe9SRichard Henderson     static const char gr_names[32][4] = {
39561766fe9SRichard Henderson         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
39661766fe9SRichard Henderson         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
39761766fe9SRichard Henderson         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
39861766fe9SRichard Henderson         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
39961766fe9SRichard Henderson     };
40033423472SRichard Henderson     /* SR[4-7] are not global registers so that we can index them.  */
401494737b7SRichard Henderson     static const char sr_names[5][4] = {
402494737b7SRichard Henderson         "sr0", "sr1", "sr2", "sr3", "srH"
40333423472SRichard Henderson     };
40461766fe9SRichard Henderson 
40561766fe9SRichard Henderson     int i;
40661766fe9SRichard Henderson 
407f764718dSRichard Henderson     cpu_gr[0] = NULL;
40861766fe9SRichard Henderson     for (i = 1; i < 32; i++) {
40961766fe9SRichard Henderson         cpu_gr[i] = tcg_global_mem_new(cpu_env,
41061766fe9SRichard Henderson                                        offsetof(CPUHPPAState, gr[i]),
41161766fe9SRichard Henderson                                        gr_names[i]);
41261766fe9SRichard Henderson     }
41333423472SRichard Henderson     for (i = 0; i < 4; i++) {
41433423472SRichard Henderson         cpu_sr[i] = tcg_global_mem_new_i64(cpu_env,
41533423472SRichard Henderson                                            offsetof(CPUHPPAState, sr[i]),
41633423472SRichard Henderson                                            sr_names[i]);
41733423472SRichard Henderson     }
418494737b7SRichard Henderson     cpu_srH = tcg_global_mem_new_i64(cpu_env,
419494737b7SRichard Henderson                                      offsetof(CPUHPPAState, sr[4]),
420494737b7SRichard Henderson                                      sr_names[4]);
42161766fe9SRichard Henderson 
42261766fe9SRichard Henderson     for (i = 0; i < ARRAY_SIZE(vars); ++i) {
42361766fe9SRichard Henderson         const GlobalVar *v = &vars[i];
42461766fe9SRichard Henderson         *v->var = tcg_global_mem_new(cpu_env, v->ofs, v->name);
42561766fe9SRichard Henderson     }
426c301f34eSRichard Henderson 
427c301f34eSRichard Henderson     cpu_iasq_f = tcg_global_mem_new_i64(cpu_env,
428c301f34eSRichard Henderson                                         offsetof(CPUHPPAState, iasq_f),
429c301f34eSRichard Henderson                                         "iasq_f");
430c301f34eSRichard Henderson     cpu_iasq_b = tcg_global_mem_new_i64(cpu_env,
431c301f34eSRichard Henderson                                         offsetof(CPUHPPAState, iasq_b),
432c301f34eSRichard Henderson                                         "iasq_b");
43361766fe9SRichard Henderson }
43461766fe9SRichard Henderson 
435129e9cc3SRichard Henderson static DisasCond cond_make_f(void)
436129e9cc3SRichard Henderson {
437f764718dSRichard Henderson     return (DisasCond){
438f764718dSRichard Henderson         .c = TCG_COND_NEVER,
439f764718dSRichard Henderson         .a0 = NULL,
440f764718dSRichard Henderson         .a1 = NULL,
441f764718dSRichard Henderson     };
442129e9cc3SRichard Henderson }
443129e9cc3SRichard Henderson 
444df0232feSRichard Henderson static DisasCond cond_make_t(void)
445df0232feSRichard Henderson {
446df0232feSRichard Henderson     return (DisasCond){
447df0232feSRichard Henderson         .c = TCG_COND_ALWAYS,
448df0232feSRichard Henderson         .a0 = NULL,
449df0232feSRichard Henderson         .a1 = NULL,
450df0232feSRichard Henderson     };
451df0232feSRichard Henderson }
452df0232feSRichard Henderson 
453129e9cc3SRichard Henderson static DisasCond cond_make_n(void)
454129e9cc3SRichard Henderson {
455f764718dSRichard Henderson     return (DisasCond){
456f764718dSRichard Henderson         .c = TCG_COND_NE,
457f764718dSRichard Henderson         .a0 = cpu_psw_n,
4586e94937aSRichard Henderson         .a1 = tcg_constant_reg(0)
459f764718dSRichard Henderson     };
460129e9cc3SRichard Henderson }
461129e9cc3SRichard Henderson 
462b47a4a02SSven Schnelle static DisasCond cond_make_0_tmp(TCGCond c, TCGv_reg a0)
463b47a4a02SSven Schnelle {
464b47a4a02SSven Schnelle     assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
465b47a4a02SSven Schnelle     return (DisasCond){
4666e94937aSRichard Henderson         .c = c, .a0 = a0, .a1 = tcg_constant_reg(0)
467b47a4a02SSven Schnelle     };
468b47a4a02SSven Schnelle }
469b47a4a02SSven Schnelle 
470eaa3783bSRichard Henderson static DisasCond cond_make_0(TCGCond c, TCGv_reg a0)
471129e9cc3SRichard Henderson {
472b47a4a02SSven Schnelle     TCGv_reg tmp = tcg_temp_new();
473b47a4a02SSven Schnelle     tcg_gen_mov_reg(tmp, a0);
474b47a4a02SSven Schnelle     return cond_make_0_tmp(c, tmp);
475129e9cc3SRichard Henderson }
476129e9cc3SRichard Henderson 
477eaa3783bSRichard Henderson static DisasCond cond_make(TCGCond c, TCGv_reg a0, TCGv_reg a1)
478129e9cc3SRichard Henderson {
479129e9cc3SRichard Henderson     DisasCond r = { .c = c };
480129e9cc3SRichard Henderson 
481129e9cc3SRichard Henderson     assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
482129e9cc3SRichard Henderson     r.a0 = tcg_temp_new();
483eaa3783bSRichard Henderson     tcg_gen_mov_reg(r.a0, a0);
484129e9cc3SRichard Henderson     r.a1 = tcg_temp_new();
485eaa3783bSRichard Henderson     tcg_gen_mov_reg(r.a1, a1);
486129e9cc3SRichard Henderson 
487129e9cc3SRichard Henderson     return r;
488129e9cc3SRichard Henderson }
489129e9cc3SRichard Henderson 
490129e9cc3SRichard Henderson static void cond_free(DisasCond *cond)
491129e9cc3SRichard Henderson {
492129e9cc3SRichard Henderson     switch (cond->c) {
493129e9cc3SRichard Henderson     default:
4946e94937aSRichard Henderson         if (cond->a0 != cpu_psw_n) {
495129e9cc3SRichard Henderson             tcg_temp_free(cond->a0);
496129e9cc3SRichard Henderson         }
497129e9cc3SRichard Henderson         tcg_temp_free(cond->a1);
498f764718dSRichard Henderson         cond->a0 = NULL;
499f764718dSRichard Henderson         cond->a1 = NULL;
500129e9cc3SRichard Henderson         /* fallthru */
501129e9cc3SRichard Henderson     case TCG_COND_ALWAYS:
502129e9cc3SRichard Henderson         cond->c = TCG_COND_NEVER;
503129e9cc3SRichard Henderson         break;
504129e9cc3SRichard Henderson     case TCG_COND_NEVER:
505129e9cc3SRichard Henderson         break;
506129e9cc3SRichard Henderson     }
507129e9cc3SRichard Henderson }
508129e9cc3SRichard Henderson 
509eaa3783bSRichard Henderson static TCGv_reg get_temp(DisasContext *ctx)
51061766fe9SRichard Henderson {
51186f8d05fSRichard Henderson     unsigned i = ctx->ntempr++;
51286f8d05fSRichard Henderson     g_assert(i < ARRAY_SIZE(ctx->tempr));
51386f8d05fSRichard Henderson     return ctx->tempr[i] = tcg_temp_new();
51461766fe9SRichard Henderson }
51561766fe9SRichard Henderson 
51686f8d05fSRichard Henderson #ifndef CONFIG_USER_ONLY
51786f8d05fSRichard Henderson static TCGv_tl get_temp_tl(DisasContext *ctx)
51886f8d05fSRichard Henderson {
51986f8d05fSRichard Henderson     unsigned i = ctx->ntempl++;
52086f8d05fSRichard Henderson     g_assert(i < ARRAY_SIZE(ctx->templ));
52186f8d05fSRichard Henderson     return ctx->templ[i] = tcg_temp_new_tl();
52286f8d05fSRichard Henderson }
52386f8d05fSRichard Henderson #endif
52486f8d05fSRichard Henderson 
525eaa3783bSRichard Henderson static TCGv_reg load_const(DisasContext *ctx, target_sreg v)
52661766fe9SRichard Henderson {
527eaa3783bSRichard Henderson     TCGv_reg t = get_temp(ctx);
528eaa3783bSRichard Henderson     tcg_gen_movi_reg(t, v);
52961766fe9SRichard Henderson     return t;
53061766fe9SRichard Henderson }
53161766fe9SRichard Henderson 
532eaa3783bSRichard Henderson static TCGv_reg load_gpr(DisasContext *ctx, unsigned reg)
53361766fe9SRichard Henderson {
53461766fe9SRichard Henderson     if (reg == 0) {
535eaa3783bSRichard Henderson         TCGv_reg t = get_temp(ctx);
536eaa3783bSRichard Henderson         tcg_gen_movi_reg(t, 0);
53761766fe9SRichard Henderson         return t;
53861766fe9SRichard Henderson     } else {
53961766fe9SRichard Henderson         return cpu_gr[reg];
54061766fe9SRichard Henderson     }
54161766fe9SRichard Henderson }
54261766fe9SRichard Henderson 
543eaa3783bSRichard Henderson static TCGv_reg dest_gpr(DisasContext *ctx, unsigned reg)
54461766fe9SRichard Henderson {
545129e9cc3SRichard Henderson     if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
54661766fe9SRichard Henderson         return get_temp(ctx);
54761766fe9SRichard Henderson     } else {
54861766fe9SRichard Henderson         return cpu_gr[reg];
54961766fe9SRichard Henderson     }
55061766fe9SRichard Henderson }
55161766fe9SRichard Henderson 
552eaa3783bSRichard Henderson static void save_or_nullify(DisasContext *ctx, TCGv_reg dest, TCGv_reg t)
553129e9cc3SRichard Henderson {
554129e9cc3SRichard Henderson     if (ctx->null_cond.c != TCG_COND_NEVER) {
555eaa3783bSRichard Henderson         tcg_gen_movcond_reg(ctx->null_cond.c, dest, ctx->null_cond.a0,
556129e9cc3SRichard Henderson                             ctx->null_cond.a1, dest, t);
557129e9cc3SRichard Henderson     } else {
558eaa3783bSRichard Henderson         tcg_gen_mov_reg(dest, t);
559129e9cc3SRichard Henderson     }
560129e9cc3SRichard Henderson }
561129e9cc3SRichard Henderson 
562eaa3783bSRichard Henderson static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_reg t)
563129e9cc3SRichard Henderson {
564129e9cc3SRichard Henderson     if (reg != 0) {
565129e9cc3SRichard Henderson         save_or_nullify(ctx, cpu_gr[reg], t);
566129e9cc3SRichard Henderson     }
567129e9cc3SRichard Henderson }
568129e9cc3SRichard Henderson 
569e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
57096d6407fSRichard Henderson # define HI_OFS  0
57196d6407fSRichard Henderson # define LO_OFS  4
57296d6407fSRichard Henderson #else
57396d6407fSRichard Henderson # define HI_OFS  4
57496d6407fSRichard Henderson # define LO_OFS  0
57596d6407fSRichard Henderson #endif
57696d6407fSRichard Henderson 
57796d6407fSRichard Henderson static TCGv_i32 load_frw_i32(unsigned rt)
57896d6407fSRichard Henderson {
57996d6407fSRichard Henderson     TCGv_i32 ret = tcg_temp_new_i32();
58096d6407fSRichard Henderson     tcg_gen_ld_i32(ret, cpu_env,
58196d6407fSRichard Henderson                    offsetof(CPUHPPAState, fr[rt & 31])
58296d6407fSRichard Henderson                    + (rt & 32 ? LO_OFS : HI_OFS));
58396d6407fSRichard Henderson     return ret;
58496d6407fSRichard Henderson }
58596d6407fSRichard Henderson 
586ebe9383cSRichard Henderson static TCGv_i32 load_frw0_i32(unsigned rt)
587ebe9383cSRichard Henderson {
588ebe9383cSRichard Henderson     if (rt == 0) {
589ebe9383cSRichard Henderson         return tcg_const_i32(0);
590ebe9383cSRichard Henderson     } else {
591ebe9383cSRichard Henderson         return load_frw_i32(rt);
592ebe9383cSRichard Henderson     }
593ebe9383cSRichard Henderson }
594ebe9383cSRichard Henderson 
595ebe9383cSRichard Henderson static TCGv_i64 load_frw0_i64(unsigned rt)
596ebe9383cSRichard Henderson {
597ebe9383cSRichard Henderson     if (rt == 0) {
598ebe9383cSRichard Henderson         return tcg_const_i64(0);
599ebe9383cSRichard Henderson     } else {
600ebe9383cSRichard Henderson         TCGv_i64 ret = tcg_temp_new_i64();
601ebe9383cSRichard Henderson         tcg_gen_ld32u_i64(ret, cpu_env,
602ebe9383cSRichard Henderson                           offsetof(CPUHPPAState, fr[rt & 31])
603ebe9383cSRichard Henderson                           + (rt & 32 ? LO_OFS : HI_OFS));
604ebe9383cSRichard Henderson         return ret;
605ebe9383cSRichard Henderson     }
606ebe9383cSRichard Henderson }
607ebe9383cSRichard Henderson 
60896d6407fSRichard Henderson static void save_frw_i32(unsigned rt, TCGv_i32 val)
60996d6407fSRichard Henderson {
61096d6407fSRichard Henderson     tcg_gen_st_i32(val, cpu_env,
61196d6407fSRichard Henderson                    offsetof(CPUHPPAState, fr[rt & 31])
61296d6407fSRichard Henderson                    + (rt & 32 ? LO_OFS : HI_OFS));
61396d6407fSRichard Henderson }
61496d6407fSRichard Henderson 
61596d6407fSRichard Henderson #undef HI_OFS
61696d6407fSRichard Henderson #undef LO_OFS
61796d6407fSRichard Henderson 
61896d6407fSRichard Henderson static TCGv_i64 load_frd(unsigned rt)
61996d6407fSRichard Henderson {
62096d6407fSRichard Henderson     TCGv_i64 ret = tcg_temp_new_i64();
62196d6407fSRichard Henderson     tcg_gen_ld_i64(ret, cpu_env, offsetof(CPUHPPAState, fr[rt]));
62296d6407fSRichard Henderson     return ret;
62396d6407fSRichard Henderson }
62496d6407fSRichard Henderson 
625ebe9383cSRichard Henderson static TCGv_i64 load_frd0(unsigned rt)
626ebe9383cSRichard Henderson {
627ebe9383cSRichard Henderson     if (rt == 0) {
628ebe9383cSRichard Henderson         return tcg_const_i64(0);
629ebe9383cSRichard Henderson     } else {
630ebe9383cSRichard Henderson         return load_frd(rt);
631ebe9383cSRichard Henderson     }
632ebe9383cSRichard Henderson }
633ebe9383cSRichard Henderson 
63496d6407fSRichard Henderson static void save_frd(unsigned rt, TCGv_i64 val)
63596d6407fSRichard Henderson {
63696d6407fSRichard Henderson     tcg_gen_st_i64(val, cpu_env, offsetof(CPUHPPAState, fr[rt]));
63796d6407fSRichard Henderson }
63896d6407fSRichard Henderson 
63933423472SRichard Henderson static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
64033423472SRichard Henderson {
64133423472SRichard Henderson #ifdef CONFIG_USER_ONLY
64233423472SRichard Henderson     tcg_gen_movi_i64(dest, 0);
64333423472SRichard Henderson #else
64433423472SRichard Henderson     if (reg < 4) {
64533423472SRichard Henderson         tcg_gen_mov_i64(dest, cpu_sr[reg]);
646494737b7SRichard Henderson     } else if (ctx->tb_flags & TB_FLAG_SR_SAME) {
647494737b7SRichard Henderson         tcg_gen_mov_i64(dest, cpu_srH);
64833423472SRichard Henderson     } else {
64933423472SRichard Henderson         tcg_gen_ld_i64(dest, cpu_env, offsetof(CPUHPPAState, sr[reg]));
65033423472SRichard Henderson     }
65133423472SRichard Henderson #endif
65233423472SRichard Henderson }
65333423472SRichard Henderson 
654129e9cc3SRichard Henderson /* Skip over the implementation of an insn that has been nullified.
655129e9cc3SRichard Henderson    Use this when the insn is too complex for a conditional move.  */
656129e9cc3SRichard Henderson static void nullify_over(DisasContext *ctx)
657129e9cc3SRichard Henderson {
658129e9cc3SRichard Henderson     if (ctx->null_cond.c != TCG_COND_NEVER) {
659129e9cc3SRichard Henderson         /* The always condition should have been handled in the main loop.  */
660129e9cc3SRichard Henderson         assert(ctx->null_cond.c != TCG_COND_ALWAYS);
661129e9cc3SRichard Henderson 
662129e9cc3SRichard Henderson         ctx->null_lab = gen_new_label();
663129e9cc3SRichard Henderson 
664129e9cc3SRichard Henderson         /* If we're using PSW[N], copy it to a temp because... */
6656e94937aSRichard Henderson         if (ctx->null_cond.a0 == cpu_psw_n) {
666129e9cc3SRichard Henderson             ctx->null_cond.a0 = tcg_temp_new();
667eaa3783bSRichard Henderson             tcg_gen_mov_reg(ctx->null_cond.a0, cpu_psw_n);
668129e9cc3SRichard Henderson         }
669129e9cc3SRichard Henderson         /* ... we clear it before branching over the implementation,
670129e9cc3SRichard Henderson            so that (1) it's clear after nullifying this insn and
671129e9cc3SRichard Henderson            (2) if this insn nullifies the next, PSW[N] is valid.  */
672129e9cc3SRichard Henderson         if (ctx->psw_n_nonzero) {
673129e9cc3SRichard Henderson             ctx->psw_n_nonzero = false;
674eaa3783bSRichard Henderson             tcg_gen_movi_reg(cpu_psw_n, 0);
675129e9cc3SRichard Henderson         }
676129e9cc3SRichard Henderson 
677eaa3783bSRichard Henderson         tcg_gen_brcond_reg(ctx->null_cond.c, ctx->null_cond.a0,
678129e9cc3SRichard Henderson                            ctx->null_cond.a1, ctx->null_lab);
679129e9cc3SRichard Henderson         cond_free(&ctx->null_cond);
680129e9cc3SRichard Henderson     }
681129e9cc3SRichard Henderson }
682129e9cc3SRichard Henderson 
683129e9cc3SRichard Henderson /* Save the current nullification state to PSW[N].  */
684129e9cc3SRichard Henderson static void nullify_save(DisasContext *ctx)
685129e9cc3SRichard Henderson {
686129e9cc3SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER) {
687129e9cc3SRichard Henderson         if (ctx->psw_n_nonzero) {
688eaa3783bSRichard Henderson             tcg_gen_movi_reg(cpu_psw_n, 0);
689129e9cc3SRichard Henderson         }
690129e9cc3SRichard Henderson         return;
691129e9cc3SRichard Henderson     }
6926e94937aSRichard Henderson     if (ctx->null_cond.a0 != cpu_psw_n) {
693eaa3783bSRichard Henderson         tcg_gen_setcond_reg(ctx->null_cond.c, cpu_psw_n,
694129e9cc3SRichard Henderson                             ctx->null_cond.a0, ctx->null_cond.a1);
695129e9cc3SRichard Henderson         ctx->psw_n_nonzero = true;
696129e9cc3SRichard Henderson     }
697129e9cc3SRichard Henderson     cond_free(&ctx->null_cond);
698129e9cc3SRichard Henderson }
699129e9cc3SRichard Henderson 
700129e9cc3SRichard Henderson /* Set a PSW[N] to X.  The intention is that this is used immediately
701129e9cc3SRichard Henderson    before a goto_tb/exit_tb, so that there is no fallthru path to other
702129e9cc3SRichard Henderson    code within the TB.  Therefore we do not update psw_n_nonzero.  */
703129e9cc3SRichard Henderson static void nullify_set(DisasContext *ctx, bool x)
704129e9cc3SRichard Henderson {
705129e9cc3SRichard Henderson     if (ctx->psw_n_nonzero || x) {
706eaa3783bSRichard Henderson         tcg_gen_movi_reg(cpu_psw_n, x);
707129e9cc3SRichard Henderson     }
708129e9cc3SRichard Henderson }
709129e9cc3SRichard Henderson 
710129e9cc3SRichard Henderson /* Mark the end of an instruction that may have been nullified.
71140f9f908SRichard Henderson    This is the pair to nullify_over.  Always returns true so that
71240f9f908SRichard Henderson    it may be tail-called from a translate function.  */
71331234768SRichard Henderson static bool nullify_end(DisasContext *ctx)
714129e9cc3SRichard Henderson {
715129e9cc3SRichard Henderson     TCGLabel *null_lab = ctx->null_lab;
71631234768SRichard Henderson     DisasJumpType status = ctx->base.is_jmp;
717129e9cc3SRichard Henderson 
718f49b3537SRichard Henderson     /* For NEXT, NORETURN, STALE, we can easily continue (or exit).
719f49b3537SRichard Henderson        For UPDATED, we cannot update on the nullified path.  */
720f49b3537SRichard Henderson     assert(status != DISAS_IAQ_N_UPDATED);
721f49b3537SRichard Henderson 
722129e9cc3SRichard Henderson     if (likely(null_lab == NULL)) {
723129e9cc3SRichard Henderson         /* The current insn wasn't conditional or handled the condition
724129e9cc3SRichard Henderson            applied to it without a branch, so the (new) setting of
725129e9cc3SRichard Henderson            NULL_COND can be applied directly to the next insn.  */
72631234768SRichard Henderson         return true;
727129e9cc3SRichard Henderson     }
728129e9cc3SRichard Henderson     ctx->null_lab = NULL;
729129e9cc3SRichard Henderson 
730129e9cc3SRichard Henderson     if (likely(ctx->null_cond.c == TCG_COND_NEVER)) {
731129e9cc3SRichard Henderson         /* The next instruction will be unconditional,
732129e9cc3SRichard Henderson            and NULL_COND already reflects that.  */
733129e9cc3SRichard Henderson         gen_set_label(null_lab);
734129e9cc3SRichard Henderson     } else {
735129e9cc3SRichard Henderson         /* The insn that we just executed is itself nullifying the next
736129e9cc3SRichard Henderson            instruction.  Store the condition in the PSW[N] global.
737129e9cc3SRichard Henderson            We asserted PSW[N] = 0 in nullify_over, so that after the
738129e9cc3SRichard Henderson            label we have the proper value in place.  */
739129e9cc3SRichard Henderson         nullify_save(ctx);
740129e9cc3SRichard Henderson         gen_set_label(null_lab);
741129e9cc3SRichard Henderson         ctx->null_cond = cond_make_n();
742129e9cc3SRichard Henderson     }
743869051eaSRichard Henderson     if (status == DISAS_NORETURN) {
74431234768SRichard Henderson         ctx->base.is_jmp = DISAS_NEXT;
745129e9cc3SRichard Henderson     }
74631234768SRichard Henderson     return true;
747129e9cc3SRichard Henderson }
748129e9cc3SRichard Henderson 
749eaa3783bSRichard Henderson static void copy_iaoq_entry(TCGv_reg dest, target_ureg ival, TCGv_reg vval)
75061766fe9SRichard Henderson {
75161766fe9SRichard Henderson     if (unlikely(ival == -1)) {
752eaa3783bSRichard Henderson         tcg_gen_mov_reg(dest, vval);
75361766fe9SRichard Henderson     } else {
754eaa3783bSRichard Henderson         tcg_gen_movi_reg(dest, ival);
75561766fe9SRichard Henderson     }
75661766fe9SRichard Henderson }
75761766fe9SRichard Henderson 
758eaa3783bSRichard Henderson static inline target_ureg iaoq_dest(DisasContext *ctx, target_sreg disp)
75961766fe9SRichard Henderson {
76061766fe9SRichard Henderson     return ctx->iaoq_f + disp + 8;
76161766fe9SRichard Henderson }
76261766fe9SRichard Henderson 
76361766fe9SRichard Henderson static void gen_excp_1(int exception)
76461766fe9SRichard Henderson {
76529dd6f64SRichard Henderson     gen_helper_excp(cpu_env, tcg_constant_i32(exception));
76661766fe9SRichard Henderson }
76761766fe9SRichard Henderson 
76831234768SRichard Henderson static void gen_excp(DisasContext *ctx, int exception)
76961766fe9SRichard Henderson {
77061766fe9SRichard Henderson     copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
77161766fe9SRichard Henderson     copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
772129e9cc3SRichard Henderson     nullify_save(ctx);
77361766fe9SRichard Henderson     gen_excp_1(exception);
77431234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
77561766fe9SRichard Henderson }
77661766fe9SRichard Henderson 
77731234768SRichard Henderson static bool gen_excp_iir(DisasContext *ctx, int exc)
7781a19da0dSRichard Henderson {
77931234768SRichard Henderson     nullify_over(ctx);
78029dd6f64SRichard Henderson     tcg_gen_st_reg(tcg_constant_reg(ctx->insn),
78129dd6f64SRichard Henderson                    cpu_env, offsetof(CPUHPPAState, cr[CR_IIR]));
78231234768SRichard Henderson     gen_excp(ctx, exc);
78331234768SRichard Henderson     return nullify_end(ctx);
7841a19da0dSRichard Henderson }
7851a19da0dSRichard Henderson 
78631234768SRichard Henderson static bool gen_illegal(DisasContext *ctx)
78761766fe9SRichard Henderson {
78831234768SRichard Henderson     return gen_excp_iir(ctx, EXCP_ILL);
78961766fe9SRichard Henderson }
79061766fe9SRichard Henderson 
79140f9f908SRichard Henderson #ifdef CONFIG_USER_ONLY
79240f9f908SRichard Henderson #define CHECK_MOST_PRIVILEGED(EXCP) \
79340f9f908SRichard Henderson     return gen_excp_iir(ctx, EXCP)
79440f9f908SRichard Henderson #else
795e1b5a5edSRichard Henderson #define CHECK_MOST_PRIVILEGED(EXCP) \
796e1b5a5edSRichard Henderson     do {                                     \
797e1b5a5edSRichard Henderson         if (ctx->privilege != 0) {           \
79831234768SRichard Henderson             return gen_excp_iir(ctx, EXCP);  \
799e1b5a5edSRichard Henderson         }                                    \
800e1b5a5edSRichard Henderson     } while (0)
80140f9f908SRichard Henderson #endif
802e1b5a5edSRichard Henderson 
803eaa3783bSRichard Henderson static bool use_goto_tb(DisasContext *ctx, target_ureg dest)
80461766fe9SRichard Henderson {
80557f91498SRichard Henderson     return translator_use_goto_tb(&ctx->base, dest);
80661766fe9SRichard Henderson }
80761766fe9SRichard Henderson 
808129e9cc3SRichard Henderson /* If the next insn is to be nullified, and it's on the same page,
809129e9cc3SRichard Henderson    and we're not attempting to set a breakpoint on it, then we can
810129e9cc3SRichard Henderson    totally skip the nullified insn.  This avoids creating and
811129e9cc3SRichard Henderson    executing a TB that merely branches to the next TB.  */
812129e9cc3SRichard Henderson static bool use_nullify_skip(DisasContext *ctx)
813129e9cc3SRichard Henderson {
814129e9cc3SRichard Henderson     return (((ctx->iaoq_b ^ ctx->iaoq_f) & TARGET_PAGE_MASK) == 0
815129e9cc3SRichard Henderson             && !cpu_breakpoint_test(ctx->cs, ctx->iaoq_b, BP_ANY));
816129e9cc3SRichard Henderson }
817129e9cc3SRichard Henderson 
81861766fe9SRichard Henderson static void gen_goto_tb(DisasContext *ctx, int which,
819eaa3783bSRichard Henderson                         target_ureg f, target_ureg b)
82061766fe9SRichard Henderson {
82161766fe9SRichard Henderson     if (f != -1 && b != -1 && use_goto_tb(ctx, f)) {
82261766fe9SRichard Henderson         tcg_gen_goto_tb(which);
823eaa3783bSRichard Henderson         tcg_gen_movi_reg(cpu_iaoq_f, f);
824eaa3783bSRichard Henderson         tcg_gen_movi_reg(cpu_iaoq_b, b);
82507ea28b4SRichard Henderson         tcg_gen_exit_tb(ctx->base.tb, which);
82661766fe9SRichard Henderson     } else {
82761766fe9SRichard Henderson         copy_iaoq_entry(cpu_iaoq_f, f, cpu_iaoq_b);
82861766fe9SRichard Henderson         copy_iaoq_entry(cpu_iaoq_b, b, ctx->iaoq_n_var);
8297f11636dSEmilio G. Cota         tcg_gen_lookup_and_goto_ptr();
83061766fe9SRichard Henderson     }
83161766fe9SRichard Henderson }
83261766fe9SRichard Henderson 
833b47a4a02SSven Schnelle static bool cond_need_sv(int c)
834b47a4a02SSven Schnelle {
835b47a4a02SSven Schnelle     return c == 2 || c == 3 || c == 6;
836b47a4a02SSven Schnelle }
837b47a4a02SSven Schnelle 
838b47a4a02SSven Schnelle static bool cond_need_cb(int c)
839b47a4a02SSven Schnelle {
840b47a4a02SSven Schnelle     return c == 4 || c == 5;
841b47a4a02SSven Schnelle }
842b47a4a02SSven Schnelle 
843b47a4a02SSven Schnelle /*
844b47a4a02SSven Schnelle  * Compute conditional for arithmetic.  See Page 5-3, Table 5-1, of
845b47a4a02SSven Schnelle  * the Parisc 1.1 Architecture Reference Manual for details.
846b47a4a02SSven Schnelle  */
847b2167459SRichard Henderson 
848eaa3783bSRichard Henderson static DisasCond do_cond(unsigned cf, TCGv_reg res,
849eaa3783bSRichard Henderson                          TCGv_reg cb_msb, TCGv_reg sv)
850b2167459SRichard Henderson {
851b2167459SRichard Henderson     DisasCond cond;
852eaa3783bSRichard Henderson     TCGv_reg tmp;
853b2167459SRichard Henderson 
854b2167459SRichard Henderson     switch (cf >> 1) {
855b47a4a02SSven Schnelle     case 0: /* Never / TR    (0 / 1) */
856b2167459SRichard Henderson         cond = cond_make_f();
857b2167459SRichard Henderson         break;
858b2167459SRichard Henderson     case 1: /* = / <>        (Z / !Z) */
859b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_EQ, res);
860b2167459SRichard Henderson         break;
861b47a4a02SSven Schnelle     case 2: /* < / >=        (N ^ V / !(N ^ V) */
862b47a4a02SSven Schnelle         tmp = tcg_temp_new();
863b47a4a02SSven Schnelle         tcg_gen_xor_reg(tmp, res, sv);
864b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_LT, tmp);
865b2167459SRichard Henderson         break;
866b47a4a02SSven Schnelle     case 3: /* <= / >        (N ^ V) | Z / !((N ^ V) | Z) */
867b47a4a02SSven Schnelle         /*
868b47a4a02SSven Schnelle          * Simplify:
869b47a4a02SSven Schnelle          *   (N ^ V) | Z
870b47a4a02SSven Schnelle          *   ((res < 0) ^ (sv < 0)) | !res
871b47a4a02SSven Schnelle          *   ((res ^ sv) < 0) | !res
872b47a4a02SSven Schnelle          *   (~(res ^ sv) >= 0) | !res
873b47a4a02SSven Schnelle          *   !(~(res ^ sv) >> 31) | !res
874b47a4a02SSven Schnelle          *   !(~(res ^ sv) >> 31 & res)
875b47a4a02SSven Schnelle          */
876b47a4a02SSven Schnelle         tmp = tcg_temp_new();
877b47a4a02SSven Schnelle         tcg_gen_eqv_reg(tmp, res, sv);
878b47a4a02SSven Schnelle         tcg_gen_sari_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
879b47a4a02SSven Schnelle         tcg_gen_and_reg(tmp, tmp, res);
880b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
881b2167459SRichard Henderson         break;
882b2167459SRichard Henderson     case 4: /* NUV / UV      (!C / C) */
883b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_EQ, cb_msb);
884b2167459SRichard Henderson         break;
885b2167459SRichard Henderson     case 5: /* ZNV / VNZ     (!C | Z / C & !Z) */
886b2167459SRichard Henderson         tmp = tcg_temp_new();
887eaa3783bSRichard Henderson         tcg_gen_neg_reg(tmp, cb_msb);
888eaa3783bSRichard Henderson         tcg_gen_and_reg(tmp, tmp, res);
889b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
890b2167459SRichard Henderson         break;
891b2167459SRichard Henderson     case 6: /* SV / NSV      (V / !V) */
892b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_LT, sv);
893b2167459SRichard Henderson         break;
894b2167459SRichard Henderson     case 7: /* OD / EV */
895b2167459SRichard Henderson         tmp = tcg_temp_new();
896eaa3783bSRichard Henderson         tcg_gen_andi_reg(tmp, res, 1);
897b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_NE, tmp);
898b2167459SRichard Henderson         break;
899b2167459SRichard Henderson     default:
900b2167459SRichard Henderson         g_assert_not_reached();
901b2167459SRichard Henderson     }
902b2167459SRichard Henderson     if (cf & 1) {
903b2167459SRichard Henderson         cond.c = tcg_invert_cond(cond.c);
904b2167459SRichard Henderson     }
905b2167459SRichard Henderson 
906b2167459SRichard Henderson     return cond;
907b2167459SRichard Henderson }
908b2167459SRichard Henderson 
909b2167459SRichard Henderson /* Similar, but for the special case of subtraction without borrow, we
910b2167459SRichard Henderson    can use the inputs directly.  This can allow other computation to be
911b2167459SRichard Henderson    deleted as unused.  */
912b2167459SRichard Henderson 
913eaa3783bSRichard Henderson static DisasCond do_sub_cond(unsigned cf, TCGv_reg res,
914eaa3783bSRichard Henderson                              TCGv_reg in1, TCGv_reg in2, TCGv_reg sv)
915b2167459SRichard Henderson {
916b2167459SRichard Henderson     DisasCond cond;
917b2167459SRichard Henderson 
918b2167459SRichard Henderson     switch (cf >> 1) {
919b2167459SRichard Henderson     case 1: /* = / <> */
920b2167459SRichard Henderson         cond = cond_make(TCG_COND_EQ, in1, in2);
921b2167459SRichard Henderson         break;
922b2167459SRichard Henderson     case 2: /* < / >= */
923b2167459SRichard Henderson         cond = cond_make(TCG_COND_LT, in1, in2);
924b2167459SRichard Henderson         break;
925b2167459SRichard Henderson     case 3: /* <= / > */
926b2167459SRichard Henderson         cond = cond_make(TCG_COND_LE, in1, in2);
927b2167459SRichard Henderson         break;
928b2167459SRichard Henderson     case 4: /* << / >>= */
929b2167459SRichard Henderson         cond = cond_make(TCG_COND_LTU, in1, in2);
930b2167459SRichard Henderson         break;
931b2167459SRichard Henderson     case 5: /* <<= / >> */
932b2167459SRichard Henderson         cond = cond_make(TCG_COND_LEU, in1, in2);
933b2167459SRichard Henderson         break;
934b2167459SRichard Henderson     default:
935b47a4a02SSven Schnelle         return do_cond(cf, res, NULL, sv);
936b2167459SRichard Henderson     }
937b2167459SRichard Henderson     if (cf & 1) {
938b2167459SRichard Henderson         cond.c = tcg_invert_cond(cond.c);
939b2167459SRichard Henderson     }
940b2167459SRichard Henderson 
941b2167459SRichard Henderson     return cond;
942b2167459SRichard Henderson }
943b2167459SRichard Henderson 
944df0232feSRichard Henderson /*
945df0232feSRichard Henderson  * Similar, but for logicals, where the carry and overflow bits are not
946df0232feSRichard Henderson  * computed, and use of them is undefined.
947df0232feSRichard Henderson  *
948df0232feSRichard Henderson  * Undefined or not, hardware does not trap.  It seems reasonable to
949df0232feSRichard Henderson  * assume hardware treats cases c={4,5,6} as if C=0 & V=0, since that's
950df0232feSRichard Henderson  * how cases c={2,3} are treated.
951df0232feSRichard Henderson  */
952b2167459SRichard Henderson 
953eaa3783bSRichard Henderson static DisasCond do_log_cond(unsigned cf, TCGv_reg res)
954b2167459SRichard Henderson {
955df0232feSRichard Henderson     switch (cf) {
956df0232feSRichard Henderson     case 0:  /* never */
957df0232feSRichard Henderson     case 9:  /* undef, C */
958df0232feSRichard Henderson     case 11: /* undef, C & !Z */
959df0232feSRichard Henderson     case 12: /* undef, V */
960df0232feSRichard Henderson         return cond_make_f();
961df0232feSRichard Henderson 
962df0232feSRichard Henderson     case 1:  /* true */
963df0232feSRichard Henderson     case 8:  /* undef, !C */
964df0232feSRichard Henderson     case 10: /* undef, !C | Z */
965df0232feSRichard Henderson     case 13: /* undef, !V */
966df0232feSRichard Henderson         return cond_make_t();
967df0232feSRichard Henderson 
968df0232feSRichard Henderson     case 2:  /* == */
969df0232feSRichard Henderson         return cond_make_0(TCG_COND_EQ, res);
970df0232feSRichard Henderson     case 3:  /* <> */
971df0232feSRichard Henderson         return cond_make_0(TCG_COND_NE, res);
972df0232feSRichard Henderson     case 4:  /* < */
973df0232feSRichard Henderson         return cond_make_0(TCG_COND_LT, res);
974df0232feSRichard Henderson     case 5:  /* >= */
975df0232feSRichard Henderson         return cond_make_0(TCG_COND_GE, res);
976df0232feSRichard Henderson     case 6:  /* <= */
977df0232feSRichard Henderson         return cond_make_0(TCG_COND_LE, res);
978df0232feSRichard Henderson     case 7:  /* > */
979df0232feSRichard Henderson         return cond_make_0(TCG_COND_GT, res);
980df0232feSRichard Henderson 
981df0232feSRichard Henderson     case 14: /* OD */
982df0232feSRichard Henderson     case 15: /* EV */
983df0232feSRichard Henderson         return do_cond(cf, res, NULL, NULL);
984df0232feSRichard Henderson 
985df0232feSRichard Henderson     default:
986df0232feSRichard Henderson         g_assert_not_reached();
987b2167459SRichard Henderson     }
988b2167459SRichard Henderson }
989b2167459SRichard Henderson 
99098cd9ca7SRichard Henderson /* Similar, but for shift/extract/deposit conditions.  */
99198cd9ca7SRichard Henderson 
992eaa3783bSRichard Henderson static DisasCond do_sed_cond(unsigned orig, TCGv_reg res)
99398cd9ca7SRichard Henderson {
99498cd9ca7SRichard Henderson     unsigned c, f;
99598cd9ca7SRichard Henderson 
99698cd9ca7SRichard Henderson     /* Convert the compressed condition codes to standard.
99798cd9ca7SRichard Henderson        0-2 are the same as logicals (nv,<,<=), while 3 is OD.
99898cd9ca7SRichard Henderson        4-7 are the reverse of 0-3.  */
99998cd9ca7SRichard Henderson     c = orig & 3;
100098cd9ca7SRichard Henderson     if (c == 3) {
100198cd9ca7SRichard Henderson         c = 7;
100298cd9ca7SRichard Henderson     }
100398cd9ca7SRichard Henderson     f = (orig & 4) / 4;
100498cd9ca7SRichard Henderson 
100598cd9ca7SRichard Henderson     return do_log_cond(c * 2 + f, res);
100698cd9ca7SRichard Henderson }
100798cd9ca7SRichard Henderson 
1008b2167459SRichard Henderson /* Similar, but for unit conditions.  */
1009b2167459SRichard Henderson 
1010eaa3783bSRichard Henderson static DisasCond do_unit_cond(unsigned cf, TCGv_reg res,
1011eaa3783bSRichard Henderson                               TCGv_reg in1, TCGv_reg in2)
1012b2167459SRichard Henderson {
1013b2167459SRichard Henderson     DisasCond cond;
1014eaa3783bSRichard Henderson     TCGv_reg tmp, cb = NULL;
1015b2167459SRichard Henderson 
1016b2167459SRichard Henderson     if (cf & 8) {
1017b2167459SRichard Henderson         /* Since we want to test lots of carry-out bits all at once, do not
1018b2167459SRichard Henderson          * do our normal thing and compute carry-in of bit B+1 since that
1019b2167459SRichard Henderson          * leaves us with carry bits spread across two words.
1020b2167459SRichard Henderson          */
1021b2167459SRichard Henderson         cb = tcg_temp_new();
1022b2167459SRichard Henderson         tmp = tcg_temp_new();
1023eaa3783bSRichard Henderson         tcg_gen_or_reg(cb, in1, in2);
1024eaa3783bSRichard Henderson         tcg_gen_and_reg(tmp, in1, in2);
1025eaa3783bSRichard Henderson         tcg_gen_andc_reg(cb, cb, res);
1026eaa3783bSRichard Henderson         tcg_gen_or_reg(cb, cb, tmp);
1027b2167459SRichard Henderson         tcg_temp_free(tmp);
1028b2167459SRichard Henderson     }
1029b2167459SRichard Henderson 
1030b2167459SRichard Henderson     switch (cf >> 1) {
1031b2167459SRichard Henderson     case 0: /* never / TR */
1032b2167459SRichard Henderson     case 1: /* undefined */
1033b2167459SRichard Henderson     case 5: /* undefined */
1034b2167459SRichard Henderson         cond = cond_make_f();
1035b2167459SRichard Henderson         break;
1036b2167459SRichard Henderson 
1037b2167459SRichard Henderson     case 2: /* SBZ / NBZ */
1038b2167459SRichard Henderson         /* See hasless(v,1) from
1039b2167459SRichard Henderson          * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
1040b2167459SRichard Henderson          */
1041b2167459SRichard Henderson         tmp = tcg_temp_new();
1042eaa3783bSRichard Henderson         tcg_gen_subi_reg(tmp, res, 0x01010101u);
1043eaa3783bSRichard Henderson         tcg_gen_andc_reg(tmp, tmp, res);
1044eaa3783bSRichard Henderson         tcg_gen_andi_reg(tmp, tmp, 0x80808080u);
1045b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_NE, tmp);
1046b2167459SRichard Henderson         tcg_temp_free(tmp);
1047b2167459SRichard Henderson         break;
1048b2167459SRichard Henderson 
1049b2167459SRichard Henderson     case 3: /* SHZ / NHZ */
1050b2167459SRichard Henderson         tmp = tcg_temp_new();
1051eaa3783bSRichard Henderson         tcg_gen_subi_reg(tmp, res, 0x00010001u);
1052eaa3783bSRichard Henderson         tcg_gen_andc_reg(tmp, tmp, res);
1053eaa3783bSRichard Henderson         tcg_gen_andi_reg(tmp, tmp, 0x80008000u);
1054b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_NE, tmp);
1055b2167459SRichard Henderson         tcg_temp_free(tmp);
1056b2167459SRichard Henderson         break;
1057b2167459SRichard Henderson 
1058b2167459SRichard Henderson     case 4: /* SDC / NDC */
1059eaa3783bSRichard Henderson         tcg_gen_andi_reg(cb, cb, 0x88888888u);
1060b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_NE, cb);
1061b2167459SRichard Henderson         break;
1062b2167459SRichard Henderson 
1063b2167459SRichard Henderson     case 6: /* SBC / NBC */
1064eaa3783bSRichard Henderson         tcg_gen_andi_reg(cb, cb, 0x80808080u);
1065b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_NE, cb);
1066b2167459SRichard Henderson         break;
1067b2167459SRichard Henderson 
1068b2167459SRichard Henderson     case 7: /* SHC / NHC */
1069eaa3783bSRichard Henderson         tcg_gen_andi_reg(cb, cb, 0x80008000u);
1070b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_NE, cb);
1071b2167459SRichard Henderson         break;
1072b2167459SRichard Henderson 
1073b2167459SRichard Henderson     default:
1074b2167459SRichard Henderson         g_assert_not_reached();
1075b2167459SRichard Henderson     }
1076b2167459SRichard Henderson     if (cf & 8) {
1077b2167459SRichard Henderson         tcg_temp_free(cb);
1078b2167459SRichard Henderson     }
1079b2167459SRichard Henderson     if (cf & 1) {
1080b2167459SRichard Henderson         cond.c = tcg_invert_cond(cond.c);
1081b2167459SRichard Henderson     }
1082b2167459SRichard Henderson 
1083b2167459SRichard Henderson     return cond;
1084b2167459SRichard Henderson }
1085b2167459SRichard Henderson 
1086b2167459SRichard Henderson /* Compute signed overflow for addition.  */
1087eaa3783bSRichard Henderson static TCGv_reg do_add_sv(DisasContext *ctx, TCGv_reg res,
1088eaa3783bSRichard Henderson                           TCGv_reg in1, TCGv_reg in2)
1089b2167459SRichard Henderson {
1090eaa3783bSRichard Henderson     TCGv_reg sv = get_temp(ctx);
1091eaa3783bSRichard Henderson     TCGv_reg tmp = tcg_temp_new();
1092b2167459SRichard Henderson 
1093eaa3783bSRichard Henderson     tcg_gen_xor_reg(sv, res, in1);
1094eaa3783bSRichard Henderson     tcg_gen_xor_reg(tmp, in1, in2);
1095eaa3783bSRichard Henderson     tcg_gen_andc_reg(sv, sv, tmp);
1096b2167459SRichard Henderson     tcg_temp_free(tmp);
1097b2167459SRichard Henderson 
1098b2167459SRichard Henderson     return sv;
1099b2167459SRichard Henderson }
1100b2167459SRichard Henderson 
1101b2167459SRichard Henderson /* Compute signed overflow for subtraction.  */
1102eaa3783bSRichard Henderson static TCGv_reg do_sub_sv(DisasContext *ctx, TCGv_reg res,
1103eaa3783bSRichard Henderson                           TCGv_reg in1, TCGv_reg in2)
1104b2167459SRichard Henderson {
1105eaa3783bSRichard Henderson     TCGv_reg sv = get_temp(ctx);
1106eaa3783bSRichard Henderson     TCGv_reg tmp = tcg_temp_new();
1107b2167459SRichard Henderson 
1108eaa3783bSRichard Henderson     tcg_gen_xor_reg(sv, res, in1);
1109eaa3783bSRichard Henderson     tcg_gen_xor_reg(tmp, in1, in2);
1110eaa3783bSRichard Henderson     tcg_gen_and_reg(sv, sv, tmp);
1111b2167459SRichard Henderson     tcg_temp_free(tmp);
1112b2167459SRichard Henderson 
1113b2167459SRichard Henderson     return sv;
1114b2167459SRichard Henderson }
1115b2167459SRichard Henderson 
111631234768SRichard Henderson static void do_add(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1117eaa3783bSRichard Henderson                    TCGv_reg in2, unsigned shift, bool is_l,
1118eaa3783bSRichard Henderson                    bool is_tsv, bool is_tc, bool is_c, unsigned cf)
1119b2167459SRichard Henderson {
1120eaa3783bSRichard Henderson     TCGv_reg dest, cb, cb_msb, sv, tmp;
1121b2167459SRichard Henderson     unsigned c = cf >> 1;
1122b2167459SRichard Henderson     DisasCond cond;
1123b2167459SRichard Henderson 
1124b2167459SRichard Henderson     dest = tcg_temp_new();
1125f764718dSRichard Henderson     cb = NULL;
1126f764718dSRichard Henderson     cb_msb = NULL;
1127b2167459SRichard Henderson 
1128b2167459SRichard Henderson     if (shift) {
1129b2167459SRichard Henderson         tmp = get_temp(ctx);
1130eaa3783bSRichard Henderson         tcg_gen_shli_reg(tmp, in1, shift);
1131b2167459SRichard Henderson         in1 = tmp;
1132b2167459SRichard Henderson     }
1133b2167459SRichard Henderson 
1134b47a4a02SSven Schnelle     if (!is_l || cond_need_cb(c)) {
113529dd6f64SRichard Henderson         TCGv_reg zero = tcg_constant_reg(0);
1136b2167459SRichard Henderson         cb_msb = get_temp(ctx);
1137eaa3783bSRichard Henderson         tcg_gen_add2_reg(dest, cb_msb, in1, zero, in2, zero);
1138b2167459SRichard Henderson         if (is_c) {
1139eaa3783bSRichard Henderson             tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cpu_psw_cb_msb, zero);
1140b2167459SRichard Henderson         }
1141b2167459SRichard Henderson         if (!is_l) {
1142b2167459SRichard Henderson             cb = get_temp(ctx);
1143eaa3783bSRichard Henderson             tcg_gen_xor_reg(cb, in1, in2);
1144eaa3783bSRichard Henderson             tcg_gen_xor_reg(cb, cb, dest);
1145b2167459SRichard Henderson         }
1146b2167459SRichard Henderson     } else {
1147eaa3783bSRichard Henderson         tcg_gen_add_reg(dest, in1, in2);
1148b2167459SRichard Henderson         if (is_c) {
1149eaa3783bSRichard Henderson             tcg_gen_add_reg(dest, dest, cpu_psw_cb_msb);
1150b2167459SRichard Henderson         }
1151b2167459SRichard Henderson     }
1152b2167459SRichard Henderson 
1153b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1154f764718dSRichard Henderson     sv = NULL;
1155b47a4a02SSven Schnelle     if (is_tsv || cond_need_sv(c)) {
1156b2167459SRichard Henderson         sv = do_add_sv(ctx, dest, in1, in2);
1157b2167459SRichard Henderson         if (is_tsv) {
1158b2167459SRichard Henderson             /* ??? Need to include overflow from shift.  */
1159b2167459SRichard Henderson             gen_helper_tsv(cpu_env, sv);
1160b2167459SRichard Henderson         }
1161b2167459SRichard Henderson     }
1162b2167459SRichard Henderson 
1163b2167459SRichard Henderson     /* Emit any conditional trap before any writeback.  */
1164b2167459SRichard Henderson     cond = do_cond(cf, dest, cb_msb, sv);
1165b2167459SRichard Henderson     if (is_tc) {
1166b2167459SRichard Henderson         tmp = tcg_temp_new();
1167eaa3783bSRichard Henderson         tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1168b2167459SRichard Henderson         gen_helper_tcond(cpu_env, tmp);
1169b2167459SRichard Henderson         tcg_temp_free(tmp);
1170b2167459SRichard Henderson     }
1171b2167459SRichard Henderson 
1172b2167459SRichard Henderson     /* Write back the result.  */
1173b2167459SRichard Henderson     if (!is_l) {
1174b2167459SRichard Henderson         save_or_nullify(ctx, cpu_psw_cb, cb);
1175b2167459SRichard Henderson         save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1176b2167459SRichard Henderson     }
1177b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1178b2167459SRichard Henderson     tcg_temp_free(dest);
1179b2167459SRichard Henderson 
1180b2167459SRichard Henderson     /* Install the new nullification.  */
1181b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1182b2167459SRichard Henderson     ctx->null_cond = cond;
1183b2167459SRichard Henderson }
1184b2167459SRichard Henderson 
11850c982a28SRichard Henderson static bool do_add_reg(DisasContext *ctx, arg_rrr_cf_sh *a,
11860c982a28SRichard Henderson                        bool is_l, bool is_tsv, bool is_tc, bool is_c)
11870c982a28SRichard Henderson {
11880c982a28SRichard Henderson     TCGv_reg tcg_r1, tcg_r2;
11890c982a28SRichard Henderson 
11900c982a28SRichard Henderson     if (a->cf) {
11910c982a28SRichard Henderson         nullify_over(ctx);
11920c982a28SRichard Henderson     }
11930c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
11940c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
11950c982a28SRichard Henderson     do_add(ctx, a->t, tcg_r1, tcg_r2, a->sh, is_l, is_tsv, is_tc, is_c, a->cf);
11960c982a28SRichard Henderson     return nullify_end(ctx);
11970c982a28SRichard Henderson }
11980c982a28SRichard Henderson 
11990588e061SRichard Henderson static bool do_add_imm(DisasContext *ctx, arg_rri_cf *a,
12000588e061SRichard Henderson                        bool is_tsv, bool is_tc)
12010588e061SRichard Henderson {
12020588e061SRichard Henderson     TCGv_reg tcg_im, tcg_r2;
12030588e061SRichard Henderson 
12040588e061SRichard Henderson     if (a->cf) {
12050588e061SRichard Henderson         nullify_over(ctx);
12060588e061SRichard Henderson     }
12070588e061SRichard Henderson     tcg_im = load_const(ctx, a->i);
12080588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
12090588e061SRichard Henderson     do_add(ctx, a->t, tcg_im, tcg_r2, 0, 0, is_tsv, is_tc, 0, a->cf);
12100588e061SRichard Henderson     return nullify_end(ctx);
12110588e061SRichard Henderson }
12120588e061SRichard Henderson 
121331234768SRichard Henderson static void do_sub(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1214eaa3783bSRichard Henderson                    TCGv_reg in2, bool is_tsv, bool is_b,
1215eaa3783bSRichard Henderson                    bool is_tc, unsigned cf)
1216b2167459SRichard Henderson {
1217eaa3783bSRichard Henderson     TCGv_reg dest, sv, cb, cb_msb, zero, tmp;
1218b2167459SRichard Henderson     unsigned c = cf >> 1;
1219b2167459SRichard Henderson     DisasCond cond;
1220b2167459SRichard Henderson 
1221b2167459SRichard Henderson     dest = tcg_temp_new();
1222b2167459SRichard Henderson     cb = tcg_temp_new();
1223b2167459SRichard Henderson     cb_msb = tcg_temp_new();
1224b2167459SRichard Henderson 
122529dd6f64SRichard Henderson     zero = tcg_constant_reg(0);
1226b2167459SRichard Henderson     if (is_b) {
1227b2167459SRichard Henderson         /* DEST,C = IN1 + ~IN2 + C.  */
1228eaa3783bSRichard Henderson         tcg_gen_not_reg(cb, in2);
1229eaa3783bSRichard Henderson         tcg_gen_add2_reg(dest, cb_msb, in1, zero, cpu_psw_cb_msb, zero);
1230eaa3783bSRichard Henderson         tcg_gen_add2_reg(dest, cb_msb, dest, cb_msb, cb, zero);
1231eaa3783bSRichard Henderson         tcg_gen_xor_reg(cb, cb, in1);
1232eaa3783bSRichard Henderson         tcg_gen_xor_reg(cb, cb, dest);
1233b2167459SRichard Henderson     } else {
1234b2167459SRichard Henderson         /* DEST,C = IN1 + ~IN2 + 1.  We can produce the same result in fewer
1235b2167459SRichard Henderson            operations by seeding the high word with 1 and subtracting.  */
1236eaa3783bSRichard Henderson         tcg_gen_movi_reg(cb_msb, 1);
1237eaa3783bSRichard Henderson         tcg_gen_sub2_reg(dest, cb_msb, in1, cb_msb, in2, zero);
1238eaa3783bSRichard Henderson         tcg_gen_eqv_reg(cb, in1, in2);
1239eaa3783bSRichard Henderson         tcg_gen_xor_reg(cb, cb, dest);
1240b2167459SRichard Henderson     }
1241b2167459SRichard Henderson 
1242b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1243f764718dSRichard Henderson     sv = NULL;
1244b47a4a02SSven Schnelle     if (is_tsv || cond_need_sv(c)) {
1245b2167459SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
1246b2167459SRichard Henderson         if (is_tsv) {
1247b2167459SRichard Henderson             gen_helper_tsv(cpu_env, sv);
1248b2167459SRichard Henderson         }
1249b2167459SRichard Henderson     }
1250b2167459SRichard Henderson 
1251b2167459SRichard Henderson     /* Compute the condition.  We cannot use the special case for borrow.  */
1252b2167459SRichard Henderson     if (!is_b) {
1253b2167459SRichard Henderson         cond = do_sub_cond(cf, dest, in1, in2, sv);
1254b2167459SRichard Henderson     } else {
1255b2167459SRichard Henderson         cond = do_cond(cf, dest, cb_msb, sv);
1256b2167459SRichard Henderson     }
1257b2167459SRichard Henderson 
1258b2167459SRichard Henderson     /* Emit any conditional trap before any writeback.  */
1259b2167459SRichard Henderson     if (is_tc) {
1260b2167459SRichard Henderson         tmp = tcg_temp_new();
1261eaa3783bSRichard Henderson         tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1262b2167459SRichard Henderson         gen_helper_tcond(cpu_env, tmp);
1263b2167459SRichard Henderson         tcg_temp_free(tmp);
1264b2167459SRichard Henderson     }
1265b2167459SRichard Henderson 
1266b2167459SRichard Henderson     /* Write back the result.  */
1267b2167459SRichard Henderson     save_or_nullify(ctx, cpu_psw_cb, cb);
1268b2167459SRichard Henderson     save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1269b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1270b2167459SRichard Henderson     tcg_temp_free(dest);
127179826f99SRichard Henderson     tcg_temp_free(cb);
127279826f99SRichard Henderson     tcg_temp_free(cb_msb);
1273b2167459SRichard Henderson 
1274b2167459SRichard Henderson     /* Install the new nullification.  */
1275b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1276b2167459SRichard Henderson     ctx->null_cond = cond;
1277b2167459SRichard Henderson }
1278b2167459SRichard Henderson 
12790c982a28SRichard Henderson static bool do_sub_reg(DisasContext *ctx, arg_rrr_cf *a,
12800c982a28SRichard Henderson                        bool is_tsv, bool is_b, bool is_tc)
12810c982a28SRichard Henderson {
12820c982a28SRichard Henderson     TCGv_reg tcg_r1, tcg_r2;
12830c982a28SRichard Henderson 
12840c982a28SRichard Henderson     if (a->cf) {
12850c982a28SRichard Henderson         nullify_over(ctx);
12860c982a28SRichard Henderson     }
12870c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
12880c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
12890c982a28SRichard Henderson     do_sub(ctx, a->t, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, a->cf);
12900c982a28SRichard Henderson     return nullify_end(ctx);
12910c982a28SRichard Henderson }
12920c982a28SRichard Henderson 
12930588e061SRichard Henderson static bool do_sub_imm(DisasContext *ctx, arg_rri_cf *a, bool is_tsv)
12940588e061SRichard Henderson {
12950588e061SRichard Henderson     TCGv_reg tcg_im, tcg_r2;
12960588e061SRichard Henderson 
12970588e061SRichard Henderson     if (a->cf) {
12980588e061SRichard Henderson         nullify_over(ctx);
12990588e061SRichard Henderson     }
13000588e061SRichard Henderson     tcg_im = load_const(ctx, a->i);
13010588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
13020588e061SRichard Henderson     do_sub(ctx, a->t, tcg_im, tcg_r2, is_tsv, 0, 0, a->cf);
13030588e061SRichard Henderson     return nullify_end(ctx);
13040588e061SRichard Henderson }
13050588e061SRichard Henderson 
130631234768SRichard Henderson static void do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1307eaa3783bSRichard Henderson                       TCGv_reg in2, unsigned cf)
1308b2167459SRichard Henderson {
1309eaa3783bSRichard Henderson     TCGv_reg dest, sv;
1310b2167459SRichard Henderson     DisasCond cond;
1311b2167459SRichard Henderson 
1312b2167459SRichard Henderson     dest = tcg_temp_new();
1313eaa3783bSRichard Henderson     tcg_gen_sub_reg(dest, in1, in2);
1314b2167459SRichard Henderson 
1315b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1316f764718dSRichard Henderson     sv = NULL;
1317b47a4a02SSven Schnelle     if (cond_need_sv(cf >> 1)) {
1318b2167459SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
1319b2167459SRichard Henderson     }
1320b2167459SRichard Henderson 
1321b2167459SRichard Henderson     /* Form the condition for the compare.  */
1322b2167459SRichard Henderson     cond = do_sub_cond(cf, dest, in1, in2, sv);
1323b2167459SRichard Henderson 
1324b2167459SRichard Henderson     /* Clear.  */
1325eaa3783bSRichard Henderson     tcg_gen_movi_reg(dest, 0);
1326b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1327b2167459SRichard Henderson     tcg_temp_free(dest);
1328b2167459SRichard Henderson 
1329b2167459SRichard Henderson     /* Install the new nullification.  */
1330b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1331b2167459SRichard Henderson     ctx->null_cond = cond;
1332b2167459SRichard Henderson }
1333b2167459SRichard Henderson 
133431234768SRichard Henderson static void do_log(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1335eaa3783bSRichard Henderson                    TCGv_reg in2, unsigned cf,
1336eaa3783bSRichard Henderson                    void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1337b2167459SRichard Henderson {
1338eaa3783bSRichard Henderson     TCGv_reg dest = dest_gpr(ctx, rt);
1339b2167459SRichard Henderson 
1340b2167459SRichard Henderson     /* Perform the operation, and writeback.  */
1341b2167459SRichard Henderson     fn(dest, in1, in2);
1342b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1343b2167459SRichard Henderson 
1344b2167459SRichard Henderson     /* Install the new nullification.  */
1345b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1346b2167459SRichard Henderson     if (cf) {
1347b2167459SRichard Henderson         ctx->null_cond = do_log_cond(cf, dest);
1348b2167459SRichard Henderson     }
1349b2167459SRichard Henderson }
1350b2167459SRichard Henderson 
13510c982a28SRichard Henderson static bool do_log_reg(DisasContext *ctx, arg_rrr_cf *a,
13520c982a28SRichard Henderson                        void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
13530c982a28SRichard Henderson {
13540c982a28SRichard Henderson     TCGv_reg tcg_r1, tcg_r2;
13550c982a28SRichard Henderson 
13560c982a28SRichard Henderson     if (a->cf) {
13570c982a28SRichard Henderson         nullify_over(ctx);
13580c982a28SRichard Henderson     }
13590c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
13600c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
13610c982a28SRichard Henderson     do_log(ctx, a->t, tcg_r1, tcg_r2, a->cf, fn);
13620c982a28SRichard Henderson     return nullify_end(ctx);
13630c982a28SRichard Henderson }
13640c982a28SRichard Henderson 
136531234768SRichard Henderson static void do_unit(DisasContext *ctx, unsigned rt, TCGv_reg in1,
1366eaa3783bSRichard Henderson                     TCGv_reg in2, unsigned cf, bool is_tc,
1367eaa3783bSRichard Henderson                     void (*fn)(TCGv_reg, TCGv_reg, TCGv_reg))
1368b2167459SRichard Henderson {
1369eaa3783bSRichard Henderson     TCGv_reg dest;
1370b2167459SRichard Henderson     DisasCond cond;
1371b2167459SRichard Henderson 
1372b2167459SRichard Henderson     if (cf == 0) {
1373b2167459SRichard Henderson         dest = dest_gpr(ctx, rt);
1374b2167459SRichard Henderson         fn(dest, in1, in2);
1375b2167459SRichard Henderson         save_gpr(ctx, rt, dest);
1376b2167459SRichard Henderson         cond_free(&ctx->null_cond);
1377b2167459SRichard Henderson     } else {
1378b2167459SRichard Henderson         dest = tcg_temp_new();
1379b2167459SRichard Henderson         fn(dest, in1, in2);
1380b2167459SRichard Henderson 
1381b2167459SRichard Henderson         cond = do_unit_cond(cf, dest, in1, in2);
1382b2167459SRichard Henderson 
1383b2167459SRichard Henderson         if (is_tc) {
1384eaa3783bSRichard Henderson             TCGv_reg tmp = tcg_temp_new();
1385eaa3783bSRichard Henderson             tcg_gen_setcond_reg(cond.c, tmp, cond.a0, cond.a1);
1386b2167459SRichard Henderson             gen_helper_tcond(cpu_env, tmp);
1387b2167459SRichard Henderson             tcg_temp_free(tmp);
1388b2167459SRichard Henderson         }
1389b2167459SRichard Henderson         save_gpr(ctx, rt, dest);
1390b2167459SRichard Henderson 
1391b2167459SRichard Henderson         cond_free(&ctx->null_cond);
1392b2167459SRichard Henderson         ctx->null_cond = cond;
1393b2167459SRichard Henderson     }
1394b2167459SRichard Henderson }
1395b2167459SRichard Henderson 
139686f8d05fSRichard Henderson #ifndef CONFIG_USER_ONLY
13978d6ae7fbSRichard Henderson /* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
13988d6ae7fbSRichard Henderson    from the top 2 bits of the base register.  There are a few system
13998d6ae7fbSRichard Henderson    instructions that have a 3-bit space specifier, for which SR0 is
14008d6ae7fbSRichard Henderson    not special.  To handle this, pass ~SP.  */
140186f8d05fSRichard Henderson static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_reg base)
140286f8d05fSRichard Henderson {
140386f8d05fSRichard Henderson     TCGv_ptr ptr;
140486f8d05fSRichard Henderson     TCGv_reg tmp;
140586f8d05fSRichard Henderson     TCGv_i64 spc;
140686f8d05fSRichard Henderson 
140786f8d05fSRichard Henderson     if (sp != 0) {
14088d6ae7fbSRichard Henderson         if (sp < 0) {
14098d6ae7fbSRichard Henderson             sp = ~sp;
14108d6ae7fbSRichard Henderson         }
14118d6ae7fbSRichard Henderson         spc = get_temp_tl(ctx);
14128d6ae7fbSRichard Henderson         load_spr(ctx, spc, sp);
14138d6ae7fbSRichard Henderson         return spc;
141486f8d05fSRichard Henderson     }
1415494737b7SRichard Henderson     if (ctx->tb_flags & TB_FLAG_SR_SAME) {
1416494737b7SRichard Henderson         return cpu_srH;
1417494737b7SRichard Henderson     }
141886f8d05fSRichard Henderson 
141986f8d05fSRichard Henderson     ptr = tcg_temp_new_ptr();
142086f8d05fSRichard Henderson     tmp = tcg_temp_new();
142186f8d05fSRichard Henderson     spc = get_temp_tl(ctx);
142286f8d05fSRichard Henderson 
142386f8d05fSRichard Henderson     tcg_gen_shri_reg(tmp, base, TARGET_REGISTER_BITS - 5);
142486f8d05fSRichard Henderson     tcg_gen_andi_reg(tmp, tmp, 030);
142586f8d05fSRichard Henderson     tcg_gen_trunc_reg_ptr(ptr, tmp);
142686f8d05fSRichard Henderson     tcg_temp_free(tmp);
142786f8d05fSRichard Henderson 
142886f8d05fSRichard Henderson     tcg_gen_add_ptr(ptr, ptr, cpu_env);
142986f8d05fSRichard Henderson     tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
143086f8d05fSRichard Henderson     tcg_temp_free_ptr(ptr);
143186f8d05fSRichard Henderson 
143286f8d05fSRichard Henderson     return spc;
143386f8d05fSRichard Henderson }
143486f8d05fSRichard Henderson #endif
143586f8d05fSRichard Henderson 
143686f8d05fSRichard Henderson static void form_gva(DisasContext *ctx, TCGv_tl *pgva, TCGv_reg *pofs,
143786f8d05fSRichard Henderson                      unsigned rb, unsigned rx, int scale, target_sreg disp,
143886f8d05fSRichard Henderson                      unsigned sp, int modify, bool is_phys)
143986f8d05fSRichard Henderson {
144086f8d05fSRichard Henderson     TCGv_reg base = load_gpr(ctx, rb);
144186f8d05fSRichard Henderson     TCGv_reg ofs;
144286f8d05fSRichard Henderson 
144386f8d05fSRichard Henderson     /* Note that RX is mutually exclusive with DISP.  */
144486f8d05fSRichard Henderson     if (rx) {
144586f8d05fSRichard Henderson         ofs = get_temp(ctx);
144686f8d05fSRichard Henderson         tcg_gen_shli_reg(ofs, cpu_gr[rx], scale);
144786f8d05fSRichard Henderson         tcg_gen_add_reg(ofs, ofs, base);
144886f8d05fSRichard Henderson     } else if (disp || modify) {
144986f8d05fSRichard Henderson         ofs = get_temp(ctx);
145086f8d05fSRichard Henderson         tcg_gen_addi_reg(ofs, base, disp);
145186f8d05fSRichard Henderson     } else {
145286f8d05fSRichard Henderson         ofs = base;
145386f8d05fSRichard Henderson     }
145486f8d05fSRichard Henderson 
145586f8d05fSRichard Henderson     *pofs = ofs;
145686f8d05fSRichard Henderson #ifdef CONFIG_USER_ONLY
145786f8d05fSRichard Henderson     *pgva = (modify <= 0 ? ofs : base);
145886f8d05fSRichard Henderson #else
145986f8d05fSRichard Henderson     TCGv_tl addr = get_temp_tl(ctx);
146086f8d05fSRichard Henderson     tcg_gen_extu_reg_tl(addr, modify <= 0 ? ofs : base);
1461494737b7SRichard Henderson     if (ctx->tb_flags & PSW_W) {
146286f8d05fSRichard Henderson         tcg_gen_andi_tl(addr, addr, 0x3fffffffffffffffull);
146386f8d05fSRichard Henderson     }
146486f8d05fSRichard Henderson     if (!is_phys) {
146586f8d05fSRichard Henderson         tcg_gen_or_tl(addr, addr, space_select(ctx, sp, base));
146686f8d05fSRichard Henderson     }
146786f8d05fSRichard Henderson     *pgva = addr;
146886f8d05fSRichard Henderson #endif
146986f8d05fSRichard Henderson }
147086f8d05fSRichard Henderson 
147196d6407fSRichard Henderson /* Emit a memory load.  The modify parameter should be
147296d6407fSRichard Henderson  * < 0 for pre-modify,
147396d6407fSRichard Henderson  * > 0 for post-modify,
147496d6407fSRichard Henderson  * = 0 for no base register update.
147596d6407fSRichard Henderson  */
147696d6407fSRichard Henderson static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1477eaa3783bSRichard Henderson                        unsigned rx, int scale, target_sreg disp,
147814776ab5STony Nguyen                        unsigned sp, int modify, MemOp mop)
147996d6407fSRichard Henderson {
148086f8d05fSRichard Henderson     TCGv_reg ofs;
148186f8d05fSRichard Henderson     TCGv_tl addr;
148296d6407fSRichard Henderson 
148396d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
148496d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
148596d6407fSRichard Henderson 
148686f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
148786f8d05fSRichard Henderson              ctx->mmu_idx == MMU_PHYS_IDX);
1488217d1a5eSRichard Henderson     tcg_gen_qemu_ld_reg(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
148986f8d05fSRichard Henderson     if (modify) {
149086f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
149196d6407fSRichard Henderson     }
149296d6407fSRichard Henderson }
149396d6407fSRichard Henderson 
149496d6407fSRichard Henderson static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1495eaa3783bSRichard Henderson                        unsigned rx, int scale, target_sreg disp,
149614776ab5STony Nguyen                        unsigned sp, int modify, MemOp mop)
149796d6407fSRichard Henderson {
149886f8d05fSRichard Henderson     TCGv_reg ofs;
149986f8d05fSRichard Henderson     TCGv_tl addr;
150096d6407fSRichard Henderson 
150196d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
150296d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
150396d6407fSRichard Henderson 
150486f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
150586f8d05fSRichard Henderson              ctx->mmu_idx == MMU_PHYS_IDX);
1506217d1a5eSRichard Henderson     tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
150786f8d05fSRichard Henderson     if (modify) {
150886f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
150996d6407fSRichard Henderson     }
151096d6407fSRichard Henderson }
151196d6407fSRichard Henderson 
151296d6407fSRichard Henderson static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1513eaa3783bSRichard Henderson                         unsigned rx, int scale, target_sreg disp,
151414776ab5STony Nguyen                         unsigned sp, int modify, MemOp mop)
151596d6407fSRichard Henderson {
151686f8d05fSRichard Henderson     TCGv_reg ofs;
151786f8d05fSRichard Henderson     TCGv_tl addr;
151896d6407fSRichard Henderson 
151996d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
152096d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
152196d6407fSRichard Henderson 
152286f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
152386f8d05fSRichard Henderson              ctx->mmu_idx == MMU_PHYS_IDX);
1524217d1a5eSRichard Henderson     tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
152586f8d05fSRichard Henderson     if (modify) {
152686f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
152796d6407fSRichard Henderson     }
152896d6407fSRichard Henderson }
152996d6407fSRichard Henderson 
153096d6407fSRichard Henderson static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1531eaa3783bSRichard Henderson                         unsigned rx, int scale, target_sreg disp,
153214776ab5STony Nguyen                         unsigned sp, int modify, MemOp mop)
153396d6407fSRichard Henderson {
153486f8d05fSRichard Henderson     TCGv_reg ofs;
153586f8d05fSRichard Henderson     TCGv_tl addr;
153696d6407fSRichard Henderson 
153796d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
153896d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
153996d6407fSRichard Henderson 
154086f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
154186f8d05fSRichard Henderson              ctx->mmu_idx == MMU_PHYS_IDX);
1542217d1a5eSRichard Henderson     tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
154386f8d05fSRichard Henderson     if (modify) {
154486f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
154596d6407fSRichard Henderson     }
154696d6407fSRichard Henderson }
154796d6407fSRichard Henderson 
1548eaa3783bSRichard Henderson #if TARGET_REGISTER_BITS == 64
1549eaa3783bSRichard Henderson #define do_load_reg   do_load_64
1550eaa3783bSRichard Henderson #define do_store_reg  do_store_64
155196d6407fSRichard Henderson #else
1552eaa3783bSRichard Henderson #define do_load_reg   do_load_32
1553eaa3783bSRichard Henderson #define do_store_reg  do_store_32
155496d6407fSRichard Henderson #endif
155596d6407fSRichard Henderson 
15561cd012a5SRichard Henderson static bool do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1557eaa3783bSRichard Henderson                     unsigned rx, int scale, target_sreg disp,
155814776ab5STony Nguyen                     unsigned sp, int modify, MemOp mop)
155996d6407fSRichard Henderson {
1560eaa3783bSRichard Henderson     TCGv_reg dest;
156196d6407fSRichard Henderson 
156296d6407fSRichard Henderson     nullify_over(ctx);
156396d6407fSRichard Henderson 
156496d6407fSRichard Henderson     if (modify == 0) {
156596d6407fSRichard Henderson         /* No base register update.  */
156696d6407fSRichard Henderson         dest = dest_gpr(ctx, rt);
156796d6407fSRichard Henderson     } else {
156896d6407fSRichard Henderson         /* Make sure if RT == RB, we see the result of the load.  */
156996d6407fSRichard Henderson         dest = get_temp(ctx);
157096d6407fSRichard Henderson     }
157186f8d05fSRichard Henderson     do_load_reg(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
157296d6407fSRichard Henderson     save_gpr(ctx, rt, dest);
157396d6407fSRichard Henderson 
15741cd012a5SRichard Henderson     return nullify_end(ctx);
157596d6407fSRichard Henderson }
157696d6407fSRichard Henderson 
1577740038d7SRichard Henderson static bool do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1578eaa3783bSRichard Henderson                       unsigned rx, int scale, target_sreg disp,
157986f8d05fSRichard Henderson                       unsigned sp, int modify)
158096d6407fSRichard Henderson {
158196d6407fSRichard Henderson     TCGv_i32 tmp;
158296d6407fSRichard Henderson 
158396d6407fSRichard Henderson     nullify_over(ctx);
158496d6407fSRichard Henderson 
158596d6407fSRichard Henderson     tmp = tcg_temp_new_i32();
158686f8d05fSRichard Henderson     do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
158796d6407fSRichard Henderson     save_frw_i32(rt, tmp);
158896d6407fSRichard Henderson     tcg_temp_free_i32(tmp);
158996d6407fSRichard Henderson 
159096d6407fSRichard Henderson     if (rt == 0) {
159196d6407fSRichard Henderson         gen_helper_loaded_fr0(cpu_env);
159296d6407fSRichard Henderson     }
159396d6407fSRichard Henderson 
1594740038d7SRichard Henderson     return nullify_end(ctx);
159596d6407fSRichard Henderson }
159696d6407fSRichard Henderson 
1597740038d7SRichard Henderson static bool trans_fldw(DisasContext *ctx, arg_ldst *a)
1598740038d7SRichard Henderson {
1599740038d7SRichard Henderson     return do_floadw(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1600740038d7SRichard Henderson                      a->disp, a->sp, a->m);
1601740038d7SRichard Henderson }
1602740038d7SRichard Henderson 
1603740038d7SRichard Henderson static bool do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1604eaa3783bSRichard Henderson                       unsigned rx, int scale, target_sreg disp,
160586f8d05fSRichard Henderson                       unsigned sp, int modify)
160696d6407fSRichard Henderson {
160796d6407fSRichard Henderson     TCGv_i64 tmp;
160896d6407fSRichard Henderson 
160996d6407fSRichard Henderson     nullify_over(ctx);
161096d6407fSRichard Henderson 
161196d6407fSRichard Henderson     tmp = tcg_temp_new_i64();
1612fc313c64SFrédéric Pétrot     do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
161396d6407fSRichard Henderson     save_frd(rt, tmp);
161496d6407fSRichard Henderson     tcg_temp_free_i64(tmp);
161596d6407fSRichard Henderson 
161696d6407fSRichard Henderson     if (rt == 0) {
161796d6407fSRichard Henderson         gen_helper_loaded_fr0(cpu_env);
161896d6407fSRichard Henderson     }
161996d6407fSRichard Henderson 
1620740038d7SRichard Henderson     return nullify_end(ctx);
1621740038d7SRichard Henderson }
1622740038d7SRichard Henderson 
1623740038d7SRichard Henderson static bool trans_fldd(DisasContext *ctx, arg_ldst *a)
1624740038d7SRichard Henderson {
1625740038d7SRichard Henderson     return do_floadd(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1626740038d7SRichard Henderson                      a->disp, a->sp, a->m);
162796d6407fSRichard Henderson }
162896d6407fSRichard Henderson 
16291cd012a5SRichard Henderson static bool do_store(DisasContext *ctx, unsigned rt, unsigned rb,
163086f8d05fSRichard Henderson                      target_sreg disp, unsigned sp,
163114776ab5STony Nguyen                      int modify, MemOp mop)
163296d6407fSRichard Henderson {
163396d6407fSRichard Henderson     nullify_over(ctx);
163486f8d05fSRichard Henderson     do_store_reg(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
16351cd012a5SRichard Henderson     return nullify_end(ctx);
163696d6407fSRichard Henderson }
163796d6407fSRichard Henderson 
1638740038d7SRichard Henderson static bool do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1639eaa3783bSRichard Henderson                        unsigned rx, int scale, target_sreg disp,
164086f8d05fSRichard Henderson                        unsigned sp, int modify)
164196d6407fSRichard Henderson {
164296d6407fSRichard Henderson     TCGv_i32 tmp;
164396d6407fSRichard Henderson 
164496d6407fSRichard Henderson     nullify_over(ctx);
164596d6407fSRichard Henderson 
164696d6407fSRichard Henderson     tmp = load_frw_i32(rt);
164786f8d05fSRichard Henderson     do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
164896d6407fSRichard Henderson     tcg_temp_free_i32(tmp);
164996d6407fSRichard Henderson 
1650740038d7SRichard Henderson     return nullify_end(ctx);
165196d6407fSRichard Henderson }
165296d6407fSRichard Henderson 
1653740038d7SRichard Henderson static bool trans_fstw(DisasContext *ctx, arg_ldst *a)
1654740038d7SRichard Henderson {
1655740038d7SRichard Henderson     return do_fstorew(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1656740038d7SRichard Henderson                       a->disp, a->sp, a->m);
1657740038d7SRichard Henderson }
1658740038d7SRichard Henderson 
1659740038d7SRichard Henderson static bool do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1660eaa3783bSRichard Henderson                        unsigned rx, int scale, target_sreg disp,
166186f8d05fSRichard Henderson                        unsigned sp, int modify)
166296d6407fSRichard Henderson {
166396d6407fSRichard Henderson     TCGv_i64 tmp;
166496d6407fSRichard Henderson 
166596d6407fSRichard Henderson     nullify_over(ctx);
166696d6407fSRichard Henderson 
166796d6407fSRichard Henderson     tmp = load_frd(rt);
1668fc313c64SFrédéric Pétrot     do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
166996d6407fSRichard Henderson     tcg_temp_free_i64(tmp);
167096d6407fSRichard Henderson 
1671740038d7SRichard Henderson     return nullify_end(ctx);
1672740038d7SRichard Henderson }
1673740038d7SRichard Henderson 
1674740038d7SRichard Henderson static bool trans_fstd(DisasContext *ctx, arg_ldst *a)
1675740038d7SRichard Henderson {
1676740038d7SRichard Henderson     return do_fstored(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1677740038d7SRichard Henderson                       a->disp, a->sp, a->m);
167896d6407fSRichard Henderson }
167996d6407fSRichard Henderson 
16801ca74648SRichard Henderson static bool do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
1681ebe9383cSRichard Henderson                        void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1682ebe9383cSRichard Henderson {
1683ebe9383cSRichard Henderson     TCGv_i32 tmp;
1684ebe9383cSRichard Henderson 
1685ebe9383cSRichard Henderson     nullify_over(ctx);
1686ebe9383cSRichard Henderson     tmp = load_frw0_i32(ra);
1687ebe9383cSRichard Henderson 
1688ebe9383cSRichard Henderson     func(tmp, cpu_env, tmp);
1689ebe9383cSRichard Henderson 
1690ebe9383cSRichard Henderson     save_frw_i32(rt, tmp);
1691ebe9383cSRichard Henderson     tcg_temp_free_i32(tmp);
16921ca74648SRichard Henderson     return nullify_end(ctx);
1693ebe9383cSRichard Henderson }
1694ebe9383cSRichard Henderson 
16951ca74648SRichard Henderson static bool do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
1696ebe9383cSRichard Henderson                        void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1697ebe9383cSRichard Henderson {
1698ebe9383cSRichard Henderson     TCGv_i32 dst;
1699ebe9383cSRichard Henderson     TCGv_i64 src;
1700ebe9383cSRichard Henderson 
1701ebe9383cSRichard Henderson     nullify_over(ctx);
1702ebe9383cSRichard Henderson     src = load_frd(ra);
1703ebe9383cSRichard Henderson     dst = tcg_temp_new_i32();
1704ebe9383cSRichard Henderson 
1705ebe9383cSRichard Henderson     func(dst, cpu_env, src);
1706ebe9383cSRichard Henderson 
1707ebe9383cSRichard Henderson     tcg_temp_free_i64(src);
1708ebe9383cSRichard Henderson     save_frw_i32(rt, dst);
1709ebe9383cSRichard Henderson     tcg_temp_free_i32(dst);
17101ca74648SRichard Henderson     return nullify_end(ctx);
1711ebe9383cSRichard Henderson }
1712ebe9383cSRichard Henderson 
17131ca74648SRichard Henderson static bool do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
1714ebe9383cSRichard Henderson                        void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1715ebe9383cSRichard Henderson {
1716ebe9383cSRichard Henderson     TCGv_i64 tmp;
1717ebe9383cSRichard Henderson 
1718ebe9383cSRichard Henderson     nullify_over(ctx);
1719ebe9383cSRichard Henderson     tmp = load_frd0(ra);
1720ebe9383cSRichard Henderson 
1721ebe9383cSRichard Henderson     func(tmp, cpu_env, tmp);
1722ebe9383cSRichard Henderson 
1723ebe9383cSRichard Henderson     save_frd(rt, tmp);
1724ebe9383cSRichard Henderson     tcg_temp_free_i64(tmp);
17251ca74648SRichard Henderson     return nullify_end(ctx);
1726ebe9383cSRichard Henderson }
1727ebe9383cSRichard Henderson 
17281ca74648SRichard Henderson static bool do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
1729ebe9383cSRichard Henderson                        void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1730ebe9383cSRichard Henderson {
1731ebe9383cSRichard Henderson     TCGv_i32 src;
1732ebe9383cSRichard Henderson     TCGv_i64 dst;
1733ebe9383cSRichard Henderson 
1734ebe9383cSRichard Henderson     nullify_over(ctx);
1735ebe9383cSRichard Henderson     src = load_frw0_i32(ra);
1736ebe9383cSRichard Henderson     dst = tcg_temp_new_i64();
1737ebe9383cSRichard Henderson 
1738ebe9383cSRichard Henderson     func(dst, cpu_env, src);
1739ebe9383cSRichard Henderson 
1740ebe9383cSRichard Henderson     tcg_temp_free_i32(src);
1741ebe9383cSRichard Henderson     save_frd(rt, dst);
1742ebe9383cSRichard Henderson     tcg_temp_free_i64(dst);
17431ca74648SRichard Henderson     return nullify_end(ctx);
1744ebe9383cSRichard Henderson }
1745ebe9383cSRichard Henderson 
17461ca74648SRichard Henderson static bool do_fop_weww(DisasContext *ctx, unsigned rt,
1747ebe9383cSRichard Henderson                         unsigned ra, unsigned rb,
174831234768SRichard Henderson                         void (*func)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32))
1749ebe9383cSRichard Henderson {
1750ebe9383cSRichard Henderson     TCGv_i32 a, b;
1751ebe9383cSRichard Henderson 
1752ebe9383cSRichard Henderson     nullify_over(ctx);
1753ebe9383cSRichard Henderson     a = load_frw0_i32(ra);
1754ebe9383cSRichard Henderson     b = load_frw0_i32(rb);
1755ebe9383cSRichard Henderson 
1756ebe9383cSRichard Henderson     func(a, cpu_env, a, b);
1757ebe9383cSRichard Henderson 
1758ebe9383cSRichard Henderson     tcg_temp_free_i32(b);
1759ebe9383cSRichard Henderson     save_frw_i32(rt, a);
1760ebe9383cSRichard Henderson     tcg_temp_free_i32(a);
17611ca74648SRichard Henderson     return nullify_end(ctx);
1762ebe9383cSRichard Henderson }
1763ebe9383cSRichard Henderson 
17641ca74648SRichard Henderson static bool do_fop_dedd(DisasContext *ctx, unsigned rt,
1765ebe9383cSRichard Henderson                         unsigned ra, unsigned rb,
176631234768SRichard Henderson                         void (*func)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1767ebe9383cSRichard Henderson {
1768ebe9383cSRichard Henderson     TCGv_i64 a, b;
1769ebe9383cSRichard Henderson 
1770ebe9383cSRichard Henderson     nullify_over(ctx);
1771ebe9383cSRichard Henderson     a = load_frd0(ra);
1772ebe9383cSRichard Henderson     b = load_frd0(rb);
1773ebe9383cSRichard Henderson 
1774ebe9383cSRichard Henderson     func(a, cpu_env, a, b);
1775ebe9383cSRichard Henderson 
1776ebe9383cSRichard Henderson     tcg_temp_free_i64(b);
1777ebe9383cSRichard Henderson     save_frd(rt, a);
1778ebe9383cSRichard Henderson     tcg_temp_free_i64(a);
17791ca74648SRichard Henderson     return nullify_end(ctx);
1780ebe9383cSRichard Henderson }
1781ebe9383cSRichard Henderson 
178298cd9ca7SRichard Henderson /* Emit an unconditional branch to a direct target, which may or may not
178398cd9ca7SRichard Henderson    have already had nullification handled.  */
178401afb7beSRichard Henderson static bool do_dbranch(DisasContext *ctx, target_ureg dest,
178598cd9ca7SRichard Henderson                        unsigned link, bool is_n)
178698cd9ca7SRichard Henderson {
178798cd9ca7SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
178898cd9ca7SRichard Henderson         if (link != 0) {
178998cd9ca7SRichard Henderson             copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
179098cd9ca7SRichard Henderson         }
179198cd9ca7SRichard Henderson         ctx->iaoq_n = dest;
179298cd9ca7SRichard Henderson         if (is_n) {
179398cd9ca7SRichard Henderson             ctx->null_cond.c = TCG_COND_ALWAYS;
179498cd9ca7SRichard Henderson         }
179598cd9ca7SRichard Henderson     } else {
179698cd9ca7SRichard Henderson         nullify_over(ctx);
179798cd9ca7SRichard Henderson 
179898cd9ca7SRichard Henderson         if (link != 0) {
179998cd9ca7SRichard Henderson             copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
180098cd9ca7SRichard Henderson         }
180198cd9ca7SRichard Henderson 
180298cd9ca7SRichard Henderson         if (is_n && use_nullify_skip(ctx)) {
180398cd9ca7SRichard Henderson             nullify_set(ctx, 0);
180498cd9ca7SRichard Henderson             gen_goto_tb(ctx, 0, dest, dest + 4);
180598cd9ca7SRichard Henderson         } else {
180698cd9ca7SRichard Henderson             nullify_set(ctx, is_n);
180798cd9ca7SRichard Henderson             gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
180898cd9ca7SRichard Henderson         }
180998cd9ca7SRichard Henderson 
181031234768SRichard Henderson         nullify_end(ctx);
181198cd9ca7SRichard Henderson 
181298cd9ca7SRichard Henderson         nullify_set(ctx, 0);
181398cd9ca7SRichard Henderson         gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
181431234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
181598cd9ca7SRichard Henderson     }
181601afb7beSRichard Henderson     return true;
181798cd9ca7SRichard Henderson }
181898cd9ca7SRichard Henderson 
181998cd9ca7SRichard Henderson /* Emit a conditional branch to a direct target.  If the branch itself
182098cd9ca7SRichard Henderson    is nullified, we should have already used nullify_over.  */
182101afb7beSRichard Henderson static bool do_cbranch(DisasContext *ctx, target_sreg disp, bool is_n,
182298cd9ca7SRichard Henderson                        DisasCond *cond)
182398cd9ca7SRichard Henderson {
1824eaa3783bSRichard Henderson     target_ureg dest = iaoq_dest(ctx, disp);
182598cd9ca7SRichard Henderson     TCGLabel *taken = NULL;
182698cd9ca7SRichard Henderson     TCGCond c = cond->c;
182798cd9ca7SRichard Henderson     bool n;
182898cd9ca7SRichard Henderson 
182998cd9ca7SRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
183098cd9ca7SRichard Henderson 
183198cd9ca7SRichard Henderson     /* Handle TRUE and NEVER as direct branches.  */
183298cd9ca7SRichard Henderson     if (c == TCG_COND_ALWAYS) {
183301afb7beSRichard Henderson         return do_dbranch(ctx, dest, 0, is_n && disp >= 0);
183498cd9ca7SRichard Henderson     }
183598cd9ca7SRichard Henderson     if (c == TCG_COND_NEVER) {
183601afb7beSRichard Henderson         return do_dbranch(ctx, ctx->iaoq_n, 0, is_n && disp < 0);
183798cd9ca7SRichard Henderson     }
183898cd9ca7SRichard Henderson 
183998cd9ca7SRichard Henderson     taken = gen_new_label();
1840eaa3783bSRichard Henderson     tcg_gen_brcond_reg(c, cond->a0, cond->a1, taken);
184198cd9ca7SRichard Henderson     cond_free(cond);
184298cd9ca7SRichard Henderson 
184398cd9ca7SRichard Henderson     /* Not taken: Condition not satisfied; nullify on backward branches. */
184498cd9ca7SRichard Henderson     n = is_n && disp < 0;
184598cd9ca7SRichard Henderson     if (n && use_nullify_skip(ctx)) {
184698cd9ca7SRichard Henderson         nullify_set(ctx, 0);
1847a881c8e7SRichard Henderson         gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
184898cd9ca7SRichard Henderson     } else {
184998cd9ca7SRichard Henderson         if (!n && ctx->null_lab) {
185098cd9ca7SRichard Henderson             gen_set_label(ctx->null_lab);
185198cd9ca7SRichard Henderson             ctx->null_lab = NULL;
185298cd9ca7SRichard Henderson         }
185398cd9ca7SRichard Henderson         nullify_set(ctx, n);
1854c301f34eSRichard Henderson         if (ctx->iaoq_n == -1) {
1855c301f34eSRichard Henderson             /* The temporary iaoq_n_var died at the branch above.
1856c301f34eSRichard Henderson                Regenerate it here instead of saving it.  */
1857c301f34eSRichard Henderson             tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
1858c301f34eSRichard Henderson         }
1859a881c8e7SRichard Henderson         gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
186098cd9ca7SRichard Henderson     }
186198cd9ca7SRichard Henderson 
186298cd9ca7SRichard Henderson     gen_set_label(taken);
186398cd9ca7SRichard Henderson 
186498cd9ca7SRichard Henderson     /* Taken: Condition satisfied; nullify on forward branches.  */
186598cd9ca7SRichard Henderson     n = is_n && disp >= 0;
186698cd9ca7SRichard Henderson     if (n && use_nullify_skip(ctx)) {
186798cd9ca7SRichard Henderson         nullify_set(ctx, 0);
1868a881c8e7SRichard Henderson         gen_goto_tb(ctx, 1, dest, dest + 4);
186998cd9ca7SRichard Henderson     } else {
187098cd9ca7SRichard Henderson         nullify_set(ctx, n);
1871a881c8e7SRichard Henderson         gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
187298cd9ca7SRichard Henderson     }
187398cd9ca7SRichard Henderson 
187498cd9ca7SRichard Henderson     /* Not taken: the branch itself was nullified.  */
187598cd9ca7SRichard Henderson     if (ctx->null_lab) {
187698cd9ca7SRichard Henderson         gen_set_label(ctx->null_lab);
187798cd9ca7SRichard Henderson         ctx->null_lab = NULL;
187831234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
187998cd9ca7SRichard Henderson     } else {
188031234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
188198cd9ca7SRichard Henderson     }
188201afb7beSRichard Henderson     return true;
188398cd9ca7SRichard Henderson }
188498cd9ca7SRichard Henderson 
188598cd9ca7SRichard Henderson /* Emit an unconditional branch to an indirect target.  This handles
188698cd9ca7SRichard Henderson    nullification of the branch itself.  */
188701afb7beSRichard Henderson static bool do_ibranch(DisasContext *ctx, TCGv_reg dest,
188898cd9ca7SRichard Henderson                        unsigned link, bool is_n)
188998cd9ca7SRichard Henderson {
1890eaa3783bSRichard Henderson     TCGv_reg a0, a1, next, tmp;
189198cd9ca7SRichard Henderson     TCGCond c;
189298cd9ca7SRichard Henderson 
189398cd9ca7SRichard Henderson     assert(ctx->null_lab == NULL);
189498cd9ca7SRichard Henderson 
189598cd9ca7SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER) {
189698cd9ca7SRichard Henderson         if (link != 0) {
189798cd9ca7SRichard Henderson             copy_iaoq_entry(cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
189898cd9ca7SRichard Henderson         }
189998cd9ca7SRichard Henderson         next = get_temp(ctx);
1900eaa3783bSRichard Henderson         tcg_gen_mov_reg(next, dest);
190198cd9ca7SRichard Henderson         if (is_n) {
1902c301f34eSRichard Henderson             if (use_nullify_skip(ctx)) {
1903c301f34eSRichard Henderson                 tcg_gen_mov_reg(cpu_iaoq_f, next);
1904c301f34eSRichard Henderson                 tcg_gen_addi_reg(cpu_iaoq_b, next, 4);
1905c301f34eSRichard Henderson                 nullify_set(ctx, 0);
190631234768SRichard Henderson                 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
190701afb7beSRichard Henderson                 return true;
1908c301f34eSRichard Henderson             }
190998cd9ca7SRichard Henderson             ctx->null_cond.c = TCG_COND_ALWAYS;
191098cd9ca7SRichard Henderson         }
1911c301f34eSRichard Henderson         ctx->iaoq_n = -1;
1912c301f34eSRichard Henderson         ctx->iaoq_n_var = next;
191398cd9ca7SRichard Henderson     } else if (is_n && use_nullify_skip(ctx)) {
191498cd9ca7SRichard Henderson         /* The (conditional) branch, B, nullifies the next insn, N,
191598cd9ca7SRichard Henderson            and we're allowed to skip execution N (no single-step or
19164137cb83SRichard Henderson            tracepoint in effect).  Since the goto_ptr that we must use
191798cd9ca7SRichard Henderson            for the indirect branch consumes no special resources, we
191898cd9ca7SRichard Henderson            can (conditionally) skip B and continue execution.  */
191998cd9ca7SRichard Henderson         /* The use_nullify_skip test implies we have a known control path.  */
192098cd9ca7SRichard Henderson         tcg_debug_assert(ctx->iaoq_b != -1);
192198cd9ca7SRichard Henderson         tcg_debug_assert(ctx->iaoq_n != -1);
192298cd9ca7SRichard Henderson 
192398cd9ca7SRichard Henderson         /* We do have to handle the non-local temporary, DEST, before
192498cd9ca7SRichard Henderson            branching.  Since IOAQ_F is not really live at this point, we
192598cd9ca7SRichard Henderson            can simply store DEST optimistically.  Similarly with IAOQ_B.  */
1926eaa3783bSRichard Henderson         tcg_gen_mov_reg(cpu_iaoq_f, dest);
1927eaa3783bSRichard Henderson         tcg_gen_addi_reg(cpu_iaoq_b, dest, 4);
192898cd9ca7SRichard Henderson 
192998cd9ca7SRichard Henderson         nullify_over(ctx);
193098cd9ca7SRichard Henderson         if (link != 0) {
1931eaa3783bSRichard Henderson             tcg_gen_movi_reg(cpu_gr[link], ctx->iaoq_n);
193298cd9ca7SRichard Henderson         }
19337f11636dSEmilio G. Cota         tcg_gen_lookup_and_goto_ptr();
193401afb7beSRichard Henderson         return nullify_end(ctx);
193598cd9ca7SRichard Henderson     } else {
193698cd9ca7SRichard Henderson         c = ctx->null_cond.c;
193798cd9ca7SRichard Henderson         a0 = ctx->null_cond.a0;
193898cd9ca7SRichard Henderson         a1 = ctx->null_cond.a1;
193998cd9ca7SRichard Henderson 
194098cd9ca7SRichard Henderson         tmp = tcg_temp_new();
194198cd9ca7SRichard Henderson         next = get_temp(ctx);
194298cd9ca7SRichard Henderson 
194398cd9ca7SRichard Henderson         copy_iaoq_entry(tmp, ctx->iaoq_n, ctx->iaoq_n_var);
1944eaa3783bSRichard Henderson         tcg_gen_movcond_reg(c, next, a0, a1, tmp, dest);
194598cd9ca7SRichard Henderson         ctx->iaoq_n = -1;
194698cd9ca7SRichard Henderson         ctx->iaoq_n_var = next;
194798cd9ca7SRichard Henderson 
194898cd9ca7SRichard Henderson         if (link != 0) {
1949eaa3783bSRichard Henderson             tcg_gen_movcond_reg(c, cpu_gr[link], a0, a1, cpu_gr[link], tmp);
195098cd9ca7SRichard Henderson         }
195198cd9ca7SRichard Henderson 
195298cd9ca7SRichard Henderson         if (is_n) {
195398cd9ca7SRichard Henderson             /* The branch nullifies the next insn, which means the state of N
195498cd9ca7SRichard Henderson                after the branch is the inverse of the state of N that applied
195598cd9ca7SRichard Henderson                to the branch.  */
1956eaa3783bSRichard Henderson             tcg_gen_setcond_reg(tcg_invert_cond(c), cpu_psw_n, a0, a1);
195798cd9ca7SRichard Henderson             cond_free(&ctx->null_cond);
195898cd9ca7SRichard Henderson             ctx->null_cond = cond_make_n();
195998cd9ca7SRichard Henderson             ctx->psw_n_nonzero = true;
196098cd9ca7SRichard Henderson         } else {
196198cd9ca7SRichard Henderson             cond_free(&ctx->null_cond);
196298cd9ca7SRichard Henderson         }
196398cd9ca7SRichard Henderson     }
196401afb7beSRichard Henderson     return true;
196598cd9ca7SRichard Henderson }
196698cd9ca7SRichard Henderson 
1967660eefe1SRichard Henderson /* Implement
1968660eefe1SRichard Henderson  *    if (IAOQ_Front{30..31} < GR[b]{30..31})
1969660eefe1SRichard Henderson  *      IAOQ_Next{30..31} ← GR[b]{30..31};
1970660eefe1SRichard Henderson  *    else
1971660eefe1SRichard Henderson  *      IAOQ_Next{30..31} ← IAOQ_Front{30..31};
1972660eefe1SRichard Henderson  * which keeps the privilege level from being increased.
1973660eefe1SRichard Henderson  */
1974660eefe1SRichard Henderson static TCGv_reg do_ibranch_priv(DisasContext *ctx, TCGv_reg offset)
1975660eefe1SRichard Henderson {
1976660eefe1SRichard Henderson     TCGv_reg dest;
1977660eefe1SRichard Henderson     switch (ctx->privilege) {
1978660eefe1SRichard Henderson     case 0:
1979660eefe1SRichard Henderson         /* Privilege 0 is maximum and is allowed to decrease.  */
1980660eefe1SRichard Henderson         return offset;
1981660eefe1SRichard Henderson     case 3:
1982993119feSRichard Henderson         /* Privilege 3 is minimum and is never allowed to increase.  */
1983660eefe1SRichard Henderson         dest = get_temp(ctx);
1984660eefe1SRichard Henderson         tcg_gen_ori_reg(dest, offset, 3);
1985660eefe1SRichard Henderson         break;
1986660eefe1SRichard Henderson     default:
1987993119feSRichard Henderson         dest = get_temp(ctx);
1988660eefe1SRichard Henderson         tcg_gen_andi_reg(dest, offset, -4);
1989660eefe1SRichard Henderson         tcg_gen_ori_reg(dest, dest, ctx->privilege);
1990660eefe1SRichard Henderson         tcg_gen_movcond_reg(TCG_COND_GTU, dest, dest, offset, dest, offset);
1991660eefe1SRichard Henderson         break;
1992660eefe1SRichard Henderson     }
1993660eefe1SRichard Henderson     return dest;
1994660eefe1SRichard Henderson }
1995660eefe1SRichard Henderson 
1996ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
19977ad439dfSRichard Henderson /* On Linux, page zero is normally marked execute only + gateway.
19987ad439dfSRichard Henderson    Therefore normal read or write is supposed to fail, but specific
19997ad439dfSRichard Henderson    offsets have kernel code mapped to raise permissions to implement
20007ad439dfSRichard Henderson    system calls.  Handling this via an explicit check here, rather
20017ad439dfSRichard Henderson    in than the "be disp(sr2,r0)" instruction that probably sent us
20027ad439dfSRichard Henderson    here, is the easiest way to handle the branch delay slot on the
20037ad439dfSRichard Henderson    aforementioned BE.  */
200431234768SRichard Henderson static void do_page_zero(DisasContext *ctx)
20057ad439dfSRichard Henderson {
20067ad439dfSRichard Henderson     /* If by some means we get here with PSW[N]=1, that implies that
20077ad439dfSRichard Henderson        the B,GATE instruction would be skipped, and we'd fault on the
20087ad439dfSRichard Henderson        next insn within the privilaged page.  */
20097ad439dfSRichard Henderson     switch (ctx->null_cond.c) {
20107ad439dfSRichard Henderson     case TCG_COND_NEVER:
20117ad439dfSRichard Henderson         break;
20127ad439dfSRichard Henderson     case TCG_COND_ALWAYS:
2013eaa3783bSRichard Henderson         tcg_gen_movi_reg(cpu_psw_n, 0);
20147ad439dfSRichard Henderson         goto do_sigill;
20157ad439dfSRichard Henderson     default:
20167ad439dfSRichard Henderson         /* Since this is always the first (and only) insn within the
20177ad439dfSRichard Henderson            TB, we should know the state of PSW[N] from TB->FLAGS.  */
20187ad439dfSRichard Henderson         g_assert_not_reached();
20197ad439dfSRichard Henderson     }
20207ad439dfSRichard Henderson 
20217ad439dfSRichard Henderson     /* Check that we didn't arrive here via some means that allowed
20227ad439dfSRichard Henderson        non-sequential instruction execution.  Normally the PSW[B] bit
20237ad439dfSRichard Henderson        detects this by disallowing the B,GATE instruction to execute
20247ad439dfSRichard Henderson        under such conditions.  */
20257ad439dfSRichard Henderson     if (ctx->iaoq_b != ctx->iaoq_f + 4) {
20267ad439dfSRichard Henderson         goto do_sigill;
20277ad439dfSRichard Henderson     }
20287ad439dfSRichard Henderson 
2029ebd0e151SRichard Henderson     switch (ctx->iaoq_f & -4) {
20307ad439dfSRichard Henderson     case 0x00: /* Null pointer call */
20312986721dSRichard Henderson         gen_excp_1(EXCP_IMP);
203231234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
203331234768SRichard Henderson         break;
20347ad439dfSRichard Henderson 
20357ad439dfSRichard Henderson     case 0xb0: /* LWS */
20367ad439dfSRichard Henderson         gen_excp_1(EXCP_SYSCALL_LWS);
203731234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
203831234768SRichard Henderson         break;
20397ad439dfSRichard Henderson 
20407ad439dfSRichard Henderson     case 0xe0: /* SET_THREAD_POINTER */
204135136a77SRichard Henderson         tcg_gen_st_reg(cpu_gr[26], cpu_env, offsetof(CPUHPPAState, cr[27]));
2042ebd0e151SRichard Henderson         tcg_gen_ori_reg(cpu_iaoq_f, cpu_gr[31], 3);
2043eaa3783bSRichard Henderson         tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
204431234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
204531234768SRichard Henderson         break;
20467ad439dfSRichard Henderson 
20477ad439dfSRichard Henderson     case 0x100: /* SYSCALL */
20487ad439dfSRichard Henderson         gen_excp_1(EXCP_SYSCALL);
204931234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
205031234768SRichard Henderson         break;
20517ad439dfSRichard Henderson 
20527ad439dfSRichard Henderson     default:
20537ad439dfSRichard Henderson     do_sigill:
20542986721dSRichard Henderson         gen_excp_1(EXCP_ILL);
205531234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
205631234768SRichard Henderson         break;
20577ad439dfSRichard Henderson     }
20587ad439dfSRichard Henderson }
2059ba1d0b44SRichard Henderson #endif
20607ad439dfSRichard Henderson 
2061deee69a1SRichard Henderson static bool trans_nop(DisasContext *ctx, arg_nop *a)
2062b2167459SRichard Henderson {
2063b2167459SRichard Henderson     cond_free(&ctx->null_cond);
206431234768SRichard Henderson     return true;
2065b2167459SRichard Henderson }
2066b2167459SRichard Henderson 
206740f9f908SRichard Henderson static bool trans_break(DisasContext *ctx, arg_break *a)
206898a9cb79SRichard Henderson {
206931234768SRichard Henderson     return gen_excp_iir(ctx, EXCP_BREAK);
207098a9cb79SRichard Henderson }
207198a9cb79SRichard Henderson 
2072e36f27efSRichard Henderson static bool trans_sync(DisasContext *ctx, arg_sync *a)
207398a9cb79SRichard Henderson {
207498a9cb79SRichard Henderson     /* No point in nullifying the memory barrier.  */
207598a9cb79SRichard Henderson     tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
207698a9cb79SRichard Henderson 
207798a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
207831234768SRichard Henderson     return true;
207998a9cb79SRichard Henderson }
208098a9cb79SRichard Henderson 
2081c603e14aSRichard Henderson static bool trans_mfia(DisasContext *ctx, arg_mfia *a)
208298a9cb79SRichard Henderson {
2083c603e14aSRichard Henderson     unsigned rt = a->t;
2084eaa3783bSRichard Henderson     TCGv_reg tmp = dest_gpr(ctx, rt);
2085eaa3783bSRichard Henderson     tcg_gen_movi_reg(tmp, ctx->iaoq_f);
208698a9cb79SRichard Henderson     save_gpr(ctx, rt, tmp);
208798a9cb79SRichard Henderson 
208898a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
208931234768SRichard Henderson     return true;
209098a9cb79SRichard Henderson }
209198a9cb79SRichard Henderson 
2092c603e14aSRichard Henderson static bool trans_mfsp(DisasContext *ctx, arg_mfsp *a)
209398a9cb79SRichard Henderson {
2094c603e14aSRichard Henderson     unsigned rt = a->t;
2095c603e14aSRichard Henderson     unsigned rs = a->sp;
209633423472SRichard Henderson     TCGv_i64 t0 = tcg_temp_new_i64();
209733423472SRichard Henderson     TCGv_reg t1 = tcg_temp_new();
209898a9cb79SRichard Henderson 
209933423472SRichard Henderson     load_spr(ctx, t0, rs);
210033423472SRichard Henderson     tcg_gen_shri_i64(t0, t0, 32);
210133423472SRichard Henderson     tcg_gen_trunc_i64_reg(t1, t0);
210233423472SRichard Henderson 
210333423472SRichard Henderson     save_gpr(ctx, rt, t1);
210433423472SRichard Henderson     tcg_temp_free(t1);
210533423472SRichard Henderson     tcg_temp_free_i64(t0);
210698a9cb79SRichard Henderson 
210798a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
210831234768SRichard Henderson     return true;
210998a9cb79SRichard Henderson }
211098a9cb79SRichard Henderson 
2111c603e14aSRichard Henderson static bool trans_mfctl(DisasContext *ctx, arg_mfctl *a)
211298a9cb79SRichard Henderson {
2113c603e14aSRichard Henderson     unsigned rt = a->t;
2114c603e14aSRichard Henderson     unsigned ctl = a->r;
2115eaa3783bSRichard Henderson     TCGv_reg tmp;
211698a9cb79SRichard Henderson 
211798a9cb79SRichard Henderson     switch (ctl) {
211835136a77SRichard Henderson     case CR_SAR:
211998a9cb79SRichard Henderson #ifdef TARGET_HPPA64
2120c603e14aSRichard Henderson         if (a->e == 0) {
212198a9cb79SRichard Henderson             /* MFSAR without ,W masks low 5 bits.  */
212298a9cb79SRichard Henderson             tmp = dest_gpr(ctx, rt);
2123eaa3783bSRichard Henderson             tcg_gen_andi_reg(tmp, cpu_sar, 31);
212498a9cb79SRichard Henderson             save_gpr(ctx, rt, tmp);
212535136a77SRichard Henderson             goto done;
212698a9cb79SRichard Henderson         }
212798a9cb79SRichard Henderson #endif
212898a9cb79SRichard Henderson         save_gpr(ctx, rt, cpu_sar);
212935136a77SRichard Henderson         goto done;
213035136a77SRichard Henderson     case CR_IT: /* Interval Timer */
213135136a77SRichard Henderson         /* FIXME: Respect PSW_S bit.  */
213235136a77SRichard Henderson         nullify_over(ctx);
213398a9cb79SRichard Henderson         tmp = dest_gpr(ctx, rt);
213484b41e65SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
213549c29d6cSRichard Henderson             gen_io_start();
213649c29d6cSRichard Henderson             gen_helper_read_interval_timer(tmp);
213731234768SRichard Henderson             ctx->base.is_jmp = DISAS_IAQ_N_STALE;
213849c29d6cSRichard Henderson         } else {
213949c29d6cSRichard Henderson             gen_helper_read_interval_timer(tmp);
214049c29d6cSRichard Henderson         }
214198a9cb79SRichard Henderson         save_gpr(ctx, rt, tmp);
214231234768SRichard Henderson         return nullify_end(ctx);
214398a9cb79SRichard Henderson     case 26:
214498a9cb79SRichard Henderson     case 27:
214598a9cb79SRichard Henderson         break;
214698a9cb79SRichard Henderson     default:
214798a9cb79SRichard Henderson         /* All other control registers are privileged.  */
214835136a77SRichard Henderson         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
214935136a77SRichard Henderson         break;
215098a9cb79SRichard Henderson     }
215198a9cb79SRichard Henderson 
215235136a77SRichard Henderson     tmp = get_temp(ctx);
215335136a77SRichard Henderson     tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
215435136a77SRichard Henderson     save_gpr(ctx, rt, tmp);
215535136a77SRichard Henderson 
215635136a77SRichard Henderson  done:
215798a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
215831234768SRichard Henderson     return true;
215998a9cb79SRichard Henderson }
216098a9cb79SRichard Henderson 
2161c603e14aSRichard Henderson static bool trans_mtsp(DisasContext *ctx, arg_mtsp *a)
216233423472SRichard Henderson {
2163c603e14aSRichard Henderson     unsigned rr = a->r;
2164c603e14aSRichard Henderson     unsigned rs = a->sp;
216533423472SRichard Henderson     TCGv_i64 t64;
216633423472SRichard Henderson 
216733423472SRichard Henderson     if (rs >= 5) {
216833423472SRichard Henderson         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
216933423472SRichard Henderson     }
217033423472SRichard Henderson     nullify_over(ctx);
217133423472SRichard Henderson 
217233423472SRichard Henderson     t64 = tcg_temp_new_i64();
217333423472SRichard Henderson     tcg_gen_extu_reg_i64(t64, load_gpr(ctx, rr));
217433423472SRichard Henderson     tcg_gen_shli_i64(t64, t64, 32);
217533423472SRichard Henderson 
217633423472SRichard Henderson     if (rs >= 4) {
217733423472SRichard Henderson         tcg_gen_st_i64(t64, cpu_env, offsetof(CPUHPPAState, sr[rs]));
2178494737b7SRichard Henderson         ctx->tb_flags &= ~TB_FLAG_SR_SAME;
217933423472SRichard Henderson     } else {
218033423472SRichard Henderson         tcg_gen_mov_i64(cpu_sr[rs], t64);
218133423472SRichard Henderson     }
218233423472SRichard Henderson     tcg_temp_free_i64(t64);
218333423472SRichard Henderson 
218431234768SRichard Henderson     return nullify_end(ctx);
218533423472SRichard Henderson }
218633423472SRichard Henderson 
2187c603e14aSRichard Henderson static bool trans_mtctl(DisasContext *ctx, arg_mtctl *a)
218898a9cb79SRichard Henderson {
2189c603e14aSRichard Henderson     unsigned ctl = a->t;
21904845f015SSven Schnelle     TCGv_reg reg;
2191eaa3783bSRichard Henderson     TCGv_reg tmp;
219298a9cb79SRichard Henderson 
219335136a77SRichard Henderson     if (ctl == CR_SAR) {
21944845f015SSven Schnelle         reg = load_gpr(ctx, a->r);
219598a9cb79SRichard Henderson         tmp = tcg_temp_new();
219635136a77SRichard Henderson         tcg_gen_andi_reg(tmp, reg, TARGET_REGISTER_BITS - 1);
219798a9cb79SRichard Henderson         save_or_nullify(ctx, cpu_sar, tmp);
219898a9cb79SRichard Henderson         tcg_temp_free(tmp);
219998a9cb79SRichard Henderson 
220098a9cb79SRichard Henderson         cond_free(&ctx->null_cond);
220131234768SRichard Henderson         return true;
220298a9cb79SRichard Henderson     }
220398a9cb79SRichard Henderson 
220435136a77SRichard Henderson     /* All other control registers are privileged or read-only.  */
220535136a77SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
220635136a77SRichard Henderson 
2207c603e14aSRichard Henderson #ifndef CONFIG_USER_ONLY
220835136a77SRichard Henderson     nullify_over(ctx);
22094845f015SSven Schnelle     reg = load_gpr(ctx, a->r);
22104845f015SSven Schnelle 
221135136a77SRichard Henderson     switch (ctl) {
221235136a77SRichard Henderson     case CR_IT:
221349c29d6cSRichard Henderson         gen_helper_write_interval_timer(cpu_env, reg);
221435136a77SRichard Henderson         break;
22154f5f2548SRichard Henderson     case CR_EIRR:
22164f5f2548SRichard Henderson         gen_helper_write_eirr(cpu_env, reg);
22174f5f2548SRichard Henderson         break;
22184f5f2548SRichard Henderson     case CR_EIEM:
22194f5f2548SRichard Henderson         gen_helper_write_eiem(cpu_env, reg);
222031234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
22214f5f2548SRichard Henderson         break;
22224f5f2548SRichard Henderson 
222335136a77SRichard Henderson     case CR_IIASQ:
222435136a77SRichard Henderson     case CR_IIAOQ:
222535136a77SRichard Henderson         /* FIXME: Respect PSW_Q bit */
222635136a77SRichard Henderson         /* The write advances the queue and stores to the back element.  */
222735136a77SRichard Henderson         tmp = get_temp(ctx);
222835136a77SRichard Henderson         tcg_gen_ld_reg(tmp, cpu_env,
222935136a77SRichard Henderson                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
223035136a77SRichard Henderson         tcg_gen_st_reg(tmp, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
223135136a77SRichard Henderson         tcg_gen_st_reg(reg, cpu_env,
223235136a77SRichard Henderson                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
223335136a77SRichard Henderson         break;
223435136a77SRichard Henderson 
2235d5de20bdSSven Schnelle     case CR_PID1:
2236d5de20bdSSven Schnelle     case CR_PID2:
2237d5de20bdSSven Schnelle     case CR_PID3:
2238d5de20bdSSven Schnelle     case CR_PID4:
2239d5de20bdSSven Schnelle         tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
2240d5de20bdSSven Schnelle #ifndef CONFIG_USER_ONLY
2241d5de20bdSSven Schnelle         gen_helper_change_prot_id(cpu_env);
2242d5de20bdSSven Schnelle #endif
2243d5de20bdSSven Schnelle         break;
2244d5de20bdSSven Schnelle 
224535136a77SRichard Henderson     default:
224635136a77SRichard Henderson         tcg_gen_st_reg(reg, cpu_env, offsetof(CPUHPPAState, cr[ctl]));
224735136a77SRichard Henderson         break;
224835136a77SRichard Henderson     }
224931234768SRichard Henderson     return nullify_end(ctx);
22504f5f2548SRichard Henderson #endif
225135136a77SRichard Henderson }
225235136a77SRichard Henderson 
2253c603e14aSRichard Henderson static bool trans_mtsarcm(DisasContext *ctx, arg_mtsarcm *a)
225498a9cb79SRichard Henderson {
2255eaa3783bSRichard Henderson     TCGv_reg tmp = tcg_temp_new();
225698a9cb79SRichard Henderson 
2257c603e14aSRichard Henderson     tcg_gen_not_reg(tmp, load_gpr(ctx, a->r));
2258eaa3783bSRichard Henderson     tcg_gen_andi_reg(tmp, tmp, TARGET_REGISTER_BITS - 1);
225998a9cb79SRichard Henderson     save_or_nullify(ctx, cpu_sar, tmp);
226098a9cb79SRichard Henderson     tcg_temp_free(tmp);
226198a9cb79SRichard Henderson 
226298a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
226331234768SRichard Henderson     return true;
226498a9cb79SRichard Henderson }
226598a9cb79SRichard Henderson 
2266e36f27efSRichard Henderson static bool trans_ldsid(DisasContext *ctx, arg_ldsid *a)
226798a9cb79SRichard Henderson {
2268e36f27efSRichard Henderson     TCGv_reg dest = dest_gpr(ctx, a->t);
226998a9cb79SRichard Henderson 
22702330504cSHelge Deller #ifdef CONFIG_USER_ONLY
22712330504cSHelge Deller     /* We don't implement space registers in user mode. */
2272eaa3783bSRichard Henderson     tcg_gen_movi_reg(dest, 0);
22732330504cSHelge Deller #else
22742330504cSHelge Deller     TCGv_i64 t0 = tcg_temp_new_i64();
22752330504cSHelge Deller 
2276e36f27efSRichard Henderson     tcg_gen_mov_i64(t0, space_select(ctx, a->sp, load_gpr(ctx, a->b)));
22772330504cSHelge Deller     tcg_gen_shri_i64(t0, t0, 32);
22782330504cSHelge Deller     tcg_gen_trunc_i64_reg(dest, t0);
22792330504cSHelge Deller 
22802330504cSHelge Deller     tcg_temp_free_i64(t0);
22812330504cSHelge Deller #endif
2282e36f27efSRichard Henderson     save_gpr(ctx, a->t, dest);
228398a9cb79SRichard Henderson 
228498a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
228531234768SRichard Henderson     return true;
228698a9cb79SRichard Henderson }
228798a9cb79SRichard Henderson 
2288e36f27efSRichard Henderson static bool trans_rsm(DisasContext *ctx, arg_rsm *a)
2289e36f27efSRichard Henderson {
2290e36f27efSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2291e1b5a5edSRichard Henderson #ifndef CONFIG_USER_ONLY
2292e1b5a5edSRichard Henderson     TCGv_reg tmp;
2293e1b5a5edSRichard Henderson 
2294e1b5a5edSRichard Henderson     nullify_over(ctx);
2295e1b5a5edSRichard Henderson 
2296e1b5a5edSRichard Henderson     tmp = get_temp(ctx);
2297e1b5a5edSRichard Henderson     tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2298e36f27efSRichard Henderson     tcg_gen_andi_reg(tmp, tmp, ~a->i);
2299e1b5a5edSRichard Henderson     gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2300e36f27efSRichard Henderson     save_gpr(ctx, a->t, tmp);
2301e1b5a5edSRichard Henderson 
2302e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts, e.g. PSW_M.  */
230331234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
230431234768SRichard Henderson     return nullify_end(ctx);
2305e36f27efSRichard Henderson #endif
2306e1b5a5edSRichard Henderson }
2307e1b5a5edSRichard Henderson 
2308e36f27efSRichard Henderson static bool trans_ssm(DisasContext *ctx, arg_ssm *a)
2309e1b5a5edSRichard Henderson {
2310e36f27efSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2311e36f27efSRichard Henderson #ifndef CONFIG_USER_ONLY
2312e1b5a5edSRichard Henderson     TCGv_reg tmp;
2313e1b5a5edSRichard Henderson 
2314e1b5a5edSRichard Henderson     nullify_over(ctx);
2315e1b5a5edSRichard Henderson 
2316e1b5a5edSRichard Henderson     tmp = get_temp(ctx);
2317e1b5a5edSRichard Henderson     tcg_gen_ld_reg(tmp, cpu_env, offsetof(CPUHPPAState, psw));
2318e36f27efSRichard Henderson     tcg_gen_ori_reg(tmp, tmp, a->i);
2319e1b5a5edSRichard Henderson     gen_helper_swap_system_mask(tmp, cpu_env, tmp);
2320e36f27efSRichard Henderson     save_gpr(ctx, a->t, tmp);
2321e1b5a5edSRichard Henderson 
2322e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts, e.g. PSW_I.  */
232331234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
232431234768SRichard Henderson     return nullify_end(ctx);
2325e36f27efSRichard Henderson #endif
2326e1b5a5edSRichard Henderson }
2327e1b5a5edSRichard Henderson 
2328c603e14aSRichard Henderson static bool trans_mtsm(DisasContext *ctx, arg_mtsm *a)
2329e1b5a5edSRichard Henderson {
2330e1b5a5edSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2331c603e14aSRichard Henderson #ifndef CONFIG_USER_ONLY
2332c603e14aSRichard Henderson     TCGv_reg tmp, reg;
2333e1b5a5edSRichard Henderson     nullify_over(ctx);
2334e1b5a5edSRichard Henderson 
2335c603e14aSRichard Henderson     reg = load_gpr(ctx, a->r);
2336e1b5a5edSRichard Henderson     tmp = get_temp(ctx);
2337e1b5a5edSRichard Henderson     gen_helper_swap_system_mask(tmp, cpu_env, reg);
2338e1b5a5edSRichard Henderson 
2339e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts.  */
234031234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
234131234768SRichard Henderson     return nullify_end(ctx);
2342c603e14aSRichard Henderson #endif
2343e1b5a5edSRichard Henderson }
2344f49b3537SRichard Henderson 
2345e36f27efSRichard Henderson static bool do_rfi(DisasContext *ctx, bool rfi_r)
2346f49b3537SRichard Henderson {
2347f49b3537SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2348e36f27efSRichard Henderson #ifndef CONFIG_USER_ONLY
2349f49b3537SRichard Henderson     nullify_over(ctx);
2350f49b3537SRichard Henderson 
2351e36f27efSRichard Henderson     if (rfi_r) {
2352f49b3537SRichard Henderson         gen_helper_rfi_r(cpu_env);
2353f49b3537SRichard Henderson     } else {
2354f49b3537SRichard Henderson         gen_helper_rfi(cpu_env);
2355f49b3537SRichard Henderson     }
235631234768SRichard Henderson     /* Exit the TB to recognize new interrupts.  */
235707ea28b4SRichard Henderson     tcg_gen_exit_tb(NULL, 0);
235831234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
2359f49b3537SRichard Henderson 
236031234768SRichard Henderson     return nullify_end(ctx);
2361e36f27efSRichard Henderson #endif
2362f49b3537SRichard Henderson }
23636210db05SHelge Deller 
2364e36f27efSRichard Henderson static bool trans_rfi(DisasContext *ctx, arg_rfi *a)
2365e36f27efSRichard Henderson {
2366e36f27efSRichard Henderson     return do_rfi(ctx, false);
2367e36f27efSRichard Henderson }
2368e36f27efSRichard Henderson 
2369e36f27efSRichard Henderson static bool trans_rfi_r(DisasContext *ctx, arg_rfi_r *a)
2370e36f27efSRichard Henderson {
2371e36f27efSRichard Henderson     return do_rfi(ctx, true);
2372e36f27efSRichard Henderson }
2373e36f27efSRichard Henderson 
237496927adbSRichard Henderson static bool trans_halt(DisasContext *ctx, arg_halt *a)
23756210db05SHelge Deller {
23766210db05SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
237796927adbSRichard Henderson #ifndef CONFIG_USER_ONLY
23786210db05SHelge Deller     nullify_over(ctx);
23796210db05SHelge Deller     gen_helper_halt(cpu_env);
238031234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
238131234768SRichard Henderson     return nullify_end(ctx);
238296927adbSRichard Henderson #endif
23836210db05SHelge Deller }
238496927adbSRichard Henderson 
238596927adbSRichard Henderson static bool trans_reset(DisasContext *ctx, arg_reset *a)
238696927adbSRichard Henderson {
238796927adbSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
238896927adbSRichard Henderson #ifndef CONFIG_USER_ONLY
238996927adbSRichard Henderson     nullify_over(ctx);
239096927adbSRichard Henderson     gen_helper_reset(cpu_env);
239196927adbSRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
239296927adbSRichard Henderson     return nullify_end(ctx);
239396927adbSRichard Henderson #endif
239496927adbSRichard Henderson }
2395e1b5a5edSRichard Henderson 
23964a4554c6SHelge Deller static bool trans_getshadowregs(DisasContext *ctx, arg_getshadowregs *a)
23974a4554c6SHelge Deller {
23984a4554c6SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
23994a4554c6SHelge Deller #ifndef CONFIG_USER_ONLY
24004a4554c6SHelge Deller     nullify_over(ctx);
24014a4554c6SHelge Deller     gen_helper_getshadowregs(cpu_env);
24024a4554c6SHelge Deller     return nullify_end(ctx);
24034a4554c6SHelge Deller #endif
24044a4554c6SHelge Deller }
24054a4554c6SHelge Deller 
2406deee69a1SRichard Henderson static bool trans_nop_addrx(DisasContext *ctx, arg_ldst *a)
240798a9cb79SRichard Henderson {
2408deee69a1SRichard Henderson     if (a->m) {
2409deee69a1SRichard Henderson         TCGv_reg dest = dest_gpr(ctx, a->b);
2410deee69a1SRichard Henderson         TCGv_reg src1 = load_gpr(ctx, a->b);
2411deee69a1SRichard Henderson         TCGv_reg src2 = load_gpr(ctx, a->x);
241298a9cb79SRichard Henderson 
241398a9cb79SRichard Henderson         /* The only thing we need to do is the base register modification.  */
2414eaa3783bSRichard Henderson         tcg_gen_add_reg(dest, src1, src2);
2415deee69a1SRichard Henderson         save_gpr(ctx, a->b, dest);
2416deee69a1SRichard Henderson     }
241798a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
241831234768SRichard Henderson     return true;
241998a9cb79SRichard Henderson }
242098a9cb79SRichard Henderson 
2421deee69a1SRichard Henderson static bool trans_probe(DisasContext *ctx, arg_probe *a)
242298a9cb79SRichard Henderson {
242386f8d05fSRichard Henderson     TCGv_reg dest, ofs;
2424eed14219SRichard Henderson     TCGv_i32 level, want;
242586f8d05fSRichard Henderson     TCGv_tl addr;
242698a9cb79SRichard Henderson 
242798a9cb79SRichard Henderson     nullify_over(ctx);
242898a9cb79SRichard Henderson 
2429deee69a1SRichard Henderson     dest = dest_gpr(ctx, a->t);
2430deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2431eed14219SRichard Henderson 
2432deee69a1SRichard Henderson     if (a->imm) {
243329dd6f64SRichard Henderson         level = tcg_constant_i32(a->ri);
243498a9cb79SRichard Henderson     } else {
2435eed14219SRichard Henderson         level = tcg_temp_new_i32();
2436deee69a1SRichard Henderson         tcg_gen_trunc_reg_i32(level, load_gpr(ctx, a->ri));
2437eed14219SRichard Henderson         tcg_gen_andi_i32(level, level, 3);
243898a9cb79SRichard Henderson     }
243929dd6f64SRichard Henderson     want = tcg_constant_i32(a->write ? PAGE_WRITE : PAGE_READ);
2440eed14219SRichard Henderson 
2441eed14219SRichard Henderson     gen_helper_probe(dest, cpu_env, addr, level, want);
2442eed14219SRichard Henderson 
2443eed14219SRichard Henderson     tcg_temp_free_i32(level);
2444eed14219SRichard Henderson 
2445deee69a1SRichard Henderson     save_gpr(ctx, a->t, dest);
244631234768SRichard Henderson     return nullify_end(ctx);
244798a9cb79SRichard Henderson }
244898a9cb79SRichard Henderson 
2449deee69a1SRichard Henderson static bool trans_ixtlbx(DisasContext *ctx, arg_ixtlbx *a)
24508d6ae7fbSRichard Henderson {
2451deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2452deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
24538d6ae7fbSRichard Henderson     TCGv_tl addr;
24548d6ae7fbSRichard Henderson     TCGv_reg ofs, reg;
24558d6ae7fbSRichard Henderson 
24568d6ae7fbSRichard Henderson     nullify_over(ctx);
24578d6ae7fbSRichard Henderson 
2458deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2459deee69a1SRichard Henderson     reg = load_gpr(ctx, a->r);
2460deee69a1SRichard Henderson     if (a->addr) {
24618d6ae7fbSRichard Henderson         gen_helper_itlba(cpu_env, addr, reg);
24628d6ae7fbSRichard Henderson     } else {
24638d6ae7fbSRichard Henderson         gen_helper_itlbp(cpu_env, addr, reg);
24648d6ae7fbSRichard Henderson     }
24658d6ae7fbSRichard Henderson 
246632dc7569SSven Schnelle     /* Exit TB for TLB change if mmu is enabled.  */
246732dc7569SSven Schnelle     if (ctx->tb_flags & PSW_C) {
246831234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
246931234768SRichard Henderson     }
247031234768SRichard Henderson     return nullify_end(ctx);
2471deee69a1SRichard Henderson #endif
24728d6ae7fbSRichard Henderson }
247363300a00SRichard Henderson 
2474deee69a1SRichard Henderson static bool trans_pxtlbx(DisasContext *ctx, arg_pxtlbx *a)
247563300a00SRichard Henderson {
2476deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2477deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
247863300a00SRichard Henderson     TCGv_tl addr;
247963300a00SRichard Henderson     TCGv_reg ofs;
248063300a00SRichard Henderson 
248163300a00SRichard Henderson     nullify_over(ctx);
248263300a00SRichard Henderson 
2483deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2484deee69a1SRichard Henderson     if (a->m) {
2485deee69a1SRichard Henderson         save_gpr(ctx, a->b, ofs);
248663300a00SRichard Henderson     }
2487deee69a1SRichard Henderson     if (a->local) {
248863300a00SRichard Henderson         gen_helper_ptlbe(cpu_env);
248963300a00SRichard Henderson     } else {
249063300a00SRichard Henderson         gen_helper_ptlb(cpu_env, addr);
249163300a00SRichard Henderson     }
249263300a00SRichard Henderson 
249363300a00SRichard Henderson     /* Exit TB for TLB change if mmu is enabled.  */
249432dc7569SSven Schnelle     if (ctx->tb_flags & PSW_C) {
249531234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
249631234768SRichard Henderson     }
249731234768SRichard Henderson     return nullify_end(ctx);
2498deee69a1SRichard Henderson #endif
249963300a00SRichard Henderson }
25002dfcca9fSRichard Henderson 
25016797c315SNick Hudson /*
25026797c315SNick Hudson  * Implement the pcxl and pcxl2 Fast TLB Insert instructions.
25036797c315SNick Hudson  * See
25046797c315SNick Hudson  *     https://parisc.wiki.kernel.org/images-parisc/a/a9/Pcxl2_ers.pdf
25056797c315SNick Hudson  *     page 13-9 (195/206)
25066797c315SNick Hudson  */
25076797c315SNick Hudson static bool trans_ixtlbxf(DisasContext *ctx, arg_ixtlbxf *a)
25086797c315SNick Hudson {
25096797c315SNick Hudson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
25106797c315SNick Hudson #ifndef CONFIG_USER_ONLY
25116797c315SNick Hudson     TCGv_tl addr, atl, stl;
25126797c315SNick Hudson     TCGv_reg reg;
25136797c315SNick Hudson 
25146797c315SNick Hudson     nullify_over(ctx);
25156797c315SNick Hudson 
25166797c315SNick Hudson     /*
25176797c315SNick Hudson      * FIXME:
25186797c315SNick Hudson      *  if (not (pcxl or pcxl2))
25196797c315SNick Hudson      *    return gen_illegal(ctx);
25206797c315SNick Hudson      *
25216797c315SNick Hudson      * Note for future: these are 32-bit systems; no hppa64.
25226797c315SNick Hudson      */
25236797c315SNick Hudson 
25246797c315SNick Hudson     atl = tcg_temp_new_tl();
25256797c315SNick Hudson     stl = tcg_temp_new_tl();
25266797c315SNick Hudson     addr = tcg_temp_new_tl();
25276797c315SNick Hudson 
25286797c315SNick Hudson     tcg_gen_ld32u_i64(stl, cpu_env,
25296797c315SNick Hudson                       a->data ? offsetof(CPUHPPAState, cr[CR_ISR])
25306797c315SNick Hudson                       : offsetof(CPUHPPAState, cr[CR_IIASQ]));
25316797c315SNick Hudson     tcg_gen_ld32u_i64(atl, cpu_env,
25326797c315SNick Hudson                       a->data ? offsetof(CPUHPPAState, cr[CR_IOR])
25336797c315SNick Hudson                       : offsetof(CPUHPPAState, cr[CR_IIAOQ]));
25346797c315SNick Hudson     tcg_gen_shli_i64(stl, stl, 32);
25356797c315SNick Hudson     tcg_gen_or_tl(addr, atl, stl);
25366797c315SNick Hudson     tcg_temp_free_tl(atl);
25376797c315SNick Hudson     tcg_temp_free_tl(stl);
25386797c315SNick Hudson 
25396797c315SNick Hudson     reg = load_gpr(ctx, a->r);
25406797c315SNick Hudson     if (a->addr) {
25416797c315SNick Hudson         gen_helper_itlba(cpu_env, addr, reg);
25426797c315SNick Hudson     } else {
25436797c315SNick Hudson         gen_helper_itlbp(cpu_env, addr, reg);
25446797c315SNick Hudson     }
25456797c315SNick Hudson     tcg_temp_free_tl(addr);
25466797c315SNick Hudson 
25476797c315SNick Hudson     /* Exit TB for TLB change if mmu is enabled.  */
25486797c315SNick Hudson     if (ctx->tb_flags & PSW_C) {
25496797c315SNick Hudson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
25506797c315SNick Hudson     }
25516797c315SNick Hudson     return nullify_end(ctx);
25526797c315SNick Hudson #endif
25536797c315SNick Hudson }
25546797c315SNick Hudson 
2555deee69a1SRichard Henderson static bool trans_lpa(DisasContext *ctx, arg_ldst *a)
25562dfcca9fSRichard Henderson {
2557deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2558deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
25592dfcca9fSRichard Henderson     TCGv_tl vaddr;
25602dfcca9fSRichard Henderson     TCGv_reg ofs, paddr;
25612dfcca9fSRichard Henderson 
25622dfcca9fSRichard Henderson     nullify_over(ctx);
25632dfcca9fSRichard Henderson 
2564deee69a1SRichard Henderson     form_gva(ctx, &vaddr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
25652dfcca9fSRichard Henderson 
25662dfcca9fSRichard Henderson     paddr = tcg_temp_new();
25672dfcca9fSRichard Henderson     gen_helper_lpa(paddr, cpu_env, vaddr);
25682dfcca9fSRichard Henderson 
25692dfcca9fSRichard Henderson     /* Note that physical address result overrides base modification.  */
2570deee69a1SRichard Henderson     if (a->m) {
2571deee69a1SRichard Henderson         save_gpr(ctx, a->b, ofs);
25722dfcca9fSRichard Henderson     }
2573deee69a1SRichard Henderson     save_gpr(ctx, a->t, paddr);
25742dfcca9fSRichard Henderson     tcg_temp_free(paddr);
25752dfcca9fSRichard Henderson 
257631234768SRichard Henderson     return nullify_end(ctx);
2577deee69a1SRichard Henderson #endif
25782dfcca9fSRichard Henderson }
257943a97b81SRichard Henderson 
2580deee69a1SRichard Henderson static bool trans_lci(DisasContext *ctx, arg_lci *a)
258143a97b81SRichard Henderson {
258243a97b81SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
258343a97b81SRichard Henderson 
258443a97b81SRichard Henderson     /* The Coherence Index is an implementation-defined function of the
258543a97b81SRichard Henderson        physical address.  Two addresses with the same CI have a coherent
258643a97b81SRichard Henderson        view of the cache.  Our implementation is to return 0 for all,
258743a97b81SRichard Henderson        since the entire address space is coherent.  */
258829dd6f64SRichard Henderson     save_gpr(ctx, a->t, tcg_constant_reg(0));
258943a97b81SRichard Henderson 
259031234768SRichard Henderson     cond_free(&ctx->null_cond);
259131234768SRichard Henderson     return true;
259243a97b81SRichard Henderson }
259398a9cb79SRichard Henderson 
25940c982a28SRichard Henderson static bool trans_add(DisasContext *ctx, arg_rrr_cf_sh *a)
2595b2167459SRichard Henderson {
25960c982a28SRichard Henderson     return do_add_reg(ctx, a, false, false, false, false);
2597b2167459SRichard Henderson }
2598b2167459SRichard Henderson 
25990c982a28SRichard Henderson static bool trans_add_l(DisasContext *ctx, arg_rrr_cf_sh *a)
2600b2167459SRichard Henderson {
26010c982a28SRichard Henderson     return do_add_reg(ctx, a, true, false, false, false);
2602b2167459SRichard Henderson }
2603b2167459SRichard Henderson 
26040c982a28SRichard Henderson static bool trans_add_tsv(DisasContext *ctx, arg_rrr_cf_sh *a)
2605b2167459SRichard Henderson {
26060c982a28SRichard Henderson     return do_add_reg(ctx, a, false, true, false, false);
2607b2167459SRichard Henderson }
2608b2167459SRichard Henderson 
26090c982a28SRichard Henderson static bool trans_add_c(DisasContext *ctx, arg_rrr_cf_sh *a)
2610b2167459SRichard Henderson {
26110c982a28SRichard Henderson     return do_add_reg(ctx, a, false, false, false, true);
26120c982a28SRichard Henderson }
2613b2167459SRichard Henderson 
26140c982a28SRichard Henderson static bool trans_add_c_tsv(DisasContext *ctx, arg_rrr_cf_sh *a)
26150c982a28SRichard Henderson {
26160c982a28SRichard Henderson     return do_add_reg(ctx, a, false, true, false, true);
26170c982a28SRichard Henderson }
26180c982a28SRichard Henderson 
26190c982a28SRichard Henderson static bool trans_sub(DisasContext *ctx, arg_rrr_cf *a)
26200c982a28SRichard Henderson {
26210c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, false, false);
26220c982a28SRichard Henderson }
26230c982a28SRichard Henderson 
26240c982a28SRichard Henderson static bool trans_sub_tsv(DisasContext *ctx, arg_rrr_cf *a)
26250c982a28SRichard Henderson {
26260c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, false, false);
26270c982a28SRichard Henderson }
26280c982a28SRichard Henderson 
26290c982a28SRichard Henderson static bool trans_sub_tc(DisasContext *ctx, arg_rrr_cf *a)
26300c982a28SRichard Henderson {
26310c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, false, true);
26320c982a28SRichard Henderson }
26330c982a28SRichard Henderson 
26340c982a28SRichard Henderson static bool trans_sub_tsv_tc(DisasContext *ctx, arg_rrr_cf *a)
26350c982a28SRichard Henderson {
26360c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, false, true);
26370c982a28SRichard Henderson }
26380c982a28SRichard Henderson 
26390c982a28SRichard Henderson static bool trans_sub_b(DisasContext *ctx, arg_rrr_cf *a)
26400c982a28SRichard Henderson {
26410c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, true, false);
26420c982a28SRichard Henderson }
26430c982a28SRichard Henderson 
26440c982a28SRichard Henderson static bool trans_sub_b_tsv(DisasContext *ctx, arg_rrr_cf *a)
26450c982a28SRichard Henderson {
26460c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, true, false);
26470c982a28SRichard Henderson }
26480c982a28SRichard Henderson 
26490c982a28SRichard Henderson static bool trans_andcm(DisasContext *ctx, arg_rrr_cf *a)
26500c982a28SRichard Henderson {
26510c982a28SRichard Henderson     return do_log_reg(ctx, a, tcg_gen_andc_reg);
26520c982a28SRichard Henderson }
26530c982a28SRichard Henderson 
26540c982a28SRichard Henderson static bool trans_and(DisasContext *ctx, arg_rrr_cf *a)
26550c982a28SRichard Henderson {
26560c982a28SRichard Henderson     return do_log_reg(ctx, a, tcg_gen_and_reg);
26570c982a28SRichard Henderson }
26580c982a28SRichard Henderson 
26590c982a28SRichard Henderson static bool trans_or(DisasContext *ctx, arg_rrr_cf *a)
26600c982a28SRichard Henderson {
26610c982a28SRichard Henderson     if (a->cf == 0) {
26620c982a28SRichard Henderson         unsigned r2 = a->r2;
26630c982a28SRichard Henderson         unsigned r1 = a->r1;
26640c982a28SRichard Henderson         unsigned rt = a->t;
26650c982a28SRichard Henderson 
26667aee8189SRichard Henderson         if (rt == 0) { /* NOP */
26677aee8189SRichard Henderson             cond_free(&ctx->null_cond);
26687aee8189SRichard Henderson             return true;
26697aee8189SRichard Henderson         }
26707aee8189SRichard Henderson         if (r2 == 0) { /* COPY */
2671b2167459SRichard Henderson             if (r1 == 0) {
2672eaa3783bSRichard Henderson                 TCGv_reg dest = dest_gpr(ctx, rt);
2673eaa3783bSRichard Henderson                 tcg_gen_movi_reg(dest, 0);
2674b2167459SRichard Henderson                 save_gpr(ctx, rt, dest);
2675b2167459SRichard Henderson             } else {
2676b2167459SRichard Henderson                 save_gpr(ctx, rt, cpu_gr[r1]);
2677b2167459SRichard Henderson             }
2678b2167459SRichard Henderson             cond_free(&ctx->null_cond);
267931234768SRichard Henderson             return true;
2680b2167459SRichard Henderson         }
26817aee8189SRichard Henderson #ifndef CONFIG_USER_ONLY
26827aee8189SRichard Henderson         /* These are QEMU extensions and are nops in the real architecture:
26837aee8189SRichard Henderson          *
26847aee8189SRichard Henderson          * or %r10,%r10,%r10 -- idle loop; wait for interrupt
26857aee8189SRichard Henderson          * or %r31,%r31,%r31 -- death loop; offline cpu
26867aee8189SRichard Henderson          *                      currently implemented as idle.
26877aee8189SRichard Henderson          */
26887aee8189SRichard Henderson         if ((rt == 10 || rt == 31) && r1 == rt && r2 == rt) { /* PAUSE */
26897aee8189SRichard Henderson             /* No need to check for supervisor, as userland can only pause
26907aee8189SRichard Henderson                until the next timer interrupt.  */
26917aee8189SRichard Henderson             nullify_over(ctx);
26927aee8189SRichard Henderson 
26937aee8189SRichard Henderson             /* Advance the instruction queue.  */
26947aee8189SRichard Henderson             copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
26957aee8189SRichard Henderson             copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
26967aee8189SRichard Henderson             nullify_set(ctx, 0);
26977aee8189SRichard Henderson 
26987aee8189SRichard Henderson             /* Tell the qemu main loop to halt until this cpu has work.  */
269929dd6f64SRichard Henderson             tcg_gen_st_i32(tcg_constant_i32(1), cpu_env,
270029dd6f64SRichard Henderson                            offsetof(CPUState, halted) - offsetof(HPPACPU, env));
27017aee8189SRichard Henderson             gen_excp_1(EXCP_HALTED);
27027aee8189SRichard Henderson             ctx->base.is_jmp = DISAS_NORETURN;
27037aee8189SRichard Henderson 
27047aee8189SRichard Henderson             return nullify_end(ctx);
27057aee8189SRichard Henderson         }
27067aee8189SRichard Henderson #endif
27077aee8189SRichard Henderson     }
27080c982a28SRichard Henderson     return do_log_reg(ctx, a, tcg_gen_or_reg);
27097aee8189SRichard Henderson }
2710b2167459SRichard Henderson 
27110c982a28SRichard Henderson static bool trans_xor(DisasContext *ctx, arg_rrr_cf *a)
2712b2167459SRichard Henderson {
27130c982a28SRichard Henderson     return do_log_reg(ctx, a, tcg_gen_xor_reg);
27140c982a28SRichard Henderson }
27150c982a28SRichard Henderson 
27160c982a28SRichard Henderson static bool trans_cmpclr(DisasContext *ctx, arg_rrr_cf *a)
27170c982a28SRichard Henderson {
2718eaa3783bSRichard Henderson     TCGv_reg tcg_r1, tcg_r2;
2719b2167459SRichard Henderson 
27200c982a28SRichard Henderson     if (a->cf) {
2721b2167459SRichard Henderson         nullify_over(ctx);
2722b2167459SRichard Henderson     }
27230c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27240c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
27250c982a28SRichard Henderson     do_cmpclr(ctx, a->t, tcg_r1, tcg_r2, a->cf);
272631234768SRichard Henderson     return nullify_end(ctx);
2727b2167459SRichard Henderson }
2728b2167459SRichard Henderson 
27290c982a28SRichard Henderson static bool trans_uxor(DisasContext *ctx, arg_rrr_cf *a)
2730b2167459SRichard Henderson {
2731eaa3783bSRichard Henderson     TCGv_reg tcg_r1, tcg_r2;
2732b2167459SRichard Henderson 
27330c982a28SRichard Henderson     if (a->cf) {
2734b2167459SRichard Henderson         nullify_over(ctx);
2735b2167459SRichard Henderson     }
27360c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27370c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
27380c982a28SRichard Henderson     do_unit(ctx, a->t, tcg_r1, tcg_r2, a->cf, false, tcg_gen_xor_reg);
273931234768SRichard Henderson     return nullify_end(ctx);
2740b2167459SRichard Henderson }
2741b2167459SRichard Henderson 
27420c982a28SRichard Henderson static bool do_uaddcm(DisasContext *ctx, arg_rrr_cf *a, bool is_tc)
2743b2167459SRichard Henderson {
2744eaa3783bSRichard Henderson     TCGv_reg tcg_r1, tcg_r2, tmp;
2745b2167459SRichard Henderson 
27460c982a28SRichard Henderson     if (a->cf) {
2747b2167459SRichard Henderson         nullify_over(ctx);
2748b2167459SRichard Henderson     }
27490c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27500c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
2751b2167459SRichard Henderson     tmp = get_temp(ctx);
2752eaa3783bSRichard Henderson     tcg_gen_not_reg(tmp, tcg_r2);
27530c982a28SRichard Henderson     do_unit(ctx, a->t, tcg_r1, tmp, a->cf, is_tc, tcg_gen_add_reg);
275431234768SRichard Henderson     return nullify_end(ctx);
2755b2167459SRichard Henderson }
2756b2167459SRichard Henderson 
27570c982a28SRichard Henderson static bool trans_uaddcm(DisasContext *ctx, arg_rrr_cf *a)
2758b2167459SRichard Henderson {
27590c982a28SRichard Henderson     return do_uaddcm(ctx, a, false);
27600c982a28SRichard Henderson }
27610c982a28SRichard Henderson 
27620c982a28SRichard Henderson static bool trans_uaddcm_tc(DisasContext *ctx, arg_rrr_cf *a)
27630c982a28SRichard Henderson {
27640c982a28SRichard Henderson     return do_uaddcm(ctx, a, true);
27650c982a28SRichard Henderson }
27660c982a28SRichard Henderson 
27670c982a28SRichard Henderson static bool do_dcor(DisasContext *ctx, arg_rr_cf *a, bool is_i)
27680c982a28SRichard Henderson {
2769eaa3783bSRichard Henderson     TCGv_reg tmp;
2770b2167459SRichard Henderson 
2771b2167459SRichard Henderson     nullify_over(ctx);
2772b2167459SRichard Henderson 
2773b2167459SRichard Henderson     tmp = get_temp(ctx);
2774eaa3783bSRichard Henderson     tcg_gen_shri_reg(tmp, cpu_psw_cb, 3);
2775b2167459SRichard Henderson     if (!is_i) {
2776eaa3783bSRichard Henderson         tcg_gen_not_reg(tmp, tmp);
2777b2167459SRichard Henderson     }
2778eaa3783bSRichard Henderson     tcg_gen_andi_reg(tmp, tmp, 0x11111111);
2779eaa3783bSRichard Henderson     tcg_gen_muli_reg(tmp, tmp, 6);
278060e29463SSven Schnelle     do_unit(ctx, a->t, load_gpr(ctx, a->r), tmp, a->cf, false,
2781eaa3783bSRichard Henderson             is_i ? tcg_gen_add_reg : tcg_gen_sub_reg);
278231234768SRichard Henderson     return nullify_end(ctx);
2783b2167459SRichard Henderson }
2784b2167459SRichard Henderson 
27850c982a28SRichard Henderson static bool trans_dcor(DisasContext *ctx, arg_rr_cf *a)
2786b2167459SRichard Henderson {
27870c982a28SRichard Henderson     return do_dcor(ctx, a, false);
27880c982a28SRichard Henderson }
27890c982a28SRichard Henderson 
27900c982a28SRichard Henderson static bool trans_dcor_i(DisasContext *ctx, arg_rr_cf *a)
27910c982a28SRichard Henderson {
27920c982a28SRichard Henderson     return do_dcor(ctx, a, true);
27930c982a28SRichard Henderson }
27940c982a28SRichard Henderson 
27950c982a28SRichard Henderson static bool trans_ds(DisasContext *ctx, arg_rrr_cf *a)
27960c982a28SRichard Henderson {
2797eaa3783bSRichard Henderson     TCGv_reg dest, add1, add2, addc, zero, in1, in2;
2798b2167459SRichard Henderson 
2799b2167459SRichard Henderson     nullify_over(ctx);
2800b2167459SRichard Henderson 
28010c982a28SRichard Henderson     in1 = load_gpr(ctx, a->r1);
28020c982a28SRichard Henderson     in2 = load_gpr(ctx, a->r2);
2803b2167459SRichard Henderson 
2804b2167459SRichard Henderson     add1 = tcg_temp_new();
2805b2167459SRichard Henderson     add2 = tcg_temp_new();
2806b2167459SRichard Henderson     addc = tcg_temp_new();
2807b2167459SRichard Henderson     dest = tcg_temp_new();
280829dd6f64SRichard Henderson     zero = tcg_constant_reg(0);
2809b2167459SRichard Henderson 
2810b2167459SRichard Henderson     /* Form R1 << 1 | PSW[CB]{8}.  */
2811eaa3783bSRichard Henderson     tcg_gen_add_reg(add1, in1, in1);
2812eaa3783bSRichard Henderson     tcg_gen_add_reg(add1, add1, cpu_psw_cb_msb);
2813b2167459SRichard Henderson 
2814b2167459SRichard Henderson     /* Add or subtract R2, depending on PSW[V].  Proper computation of
2815b2167459SRichard Henderson        carry{8} requires that we subtract via + ~R2 + 1, as described in
2816b2167459SRichard Henderson        the manual.  By extracting and masking V, we can produce the
2817b2167459SRichard Henderson        proper inputs to the addition without movcond.  */
2818eaa3783bSRichard Henderson     tcg_gen_sari_reg(addc, cpu_psw_v, TARGET_REGISTER_BITS - 1);
2819eaa3783bSRichard Henderson     tcg_gen_xor_reg(add2, in2, addc);
2820eaa3783bSRichard Henderson     tcg_gen_andi_reg(addc, addc, 1);
2821b2167459SRichard Henderson     /* ??? This is only correct for 32-bit.  */
2822b2167459SRichard Henderson     tcg_gen_add2_i32(dest, cpu_psw_cb_msb, add1, zero, add2, zero);
2823b2167459SRichard Henderson     tcg_gen_add2_i32(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb, addc, zero);
2824b2167459SRichard Henderson 
2825b2167459SRichard Henderson     tcg_temp_free(addc);
2826b2167459SRichard Henderson 
2827b2167459SRichard Henderson     /* Write back the result register.  */
28280c982a28SRichard Henderson     save_gpr(ctx, a->t, dest);
2829b2167459SRichard Henderson 
2830b2167459SRichard Henderson     /* Write back PSW[CB].  */
2831eaa3783bSRichard Henderson     tcg_gen_xor_reg(cpu_psw_cb, add1, add2);
2832eaa3783bSRichard Henderson     tcg_gen_xor_reg(cpu_psw_cb, cpu_psw_cb, dest);
2833b2167459SRichard Henderson 
2834b2167459SRichard Henderson     /* Write back PSW[V] for the division step.  */
2835eaa3783bSRichard Henderson     tcg_gen_neg_reg(cpu_psw_v, cpu_psw_cb_msb);
2836eaa3783bSRichard Henderson     tcg_gen_xor_reg(cpu_psw_v, cpu_psw_v, in2);
2837b2167459SRichard Henderson 
2838b2167459SRichard Henderson     /* Install the new nullification.  */
28390c982a28SRichard Henderson     if (a->cf) {
2840eaa3783bSRichard Henderson         TCGv_reg sv = NULL;
2841b47a4a02SSven Schnelle         if (cond_need_sv(a->cf >> 1)) {
2842b2167459SRichard Henderson             /* ??? The lshift is supposed to contribute to overflow.  */
2843b2167459SRichard Henderson             sv = do_add_sv(ctx, dest, add1, add2);
2844b2167459SRichard Henderson         }
28450c982a28SRichard Henderson         ctx->null_cond = do_cond(a->cf, dest, cpu_psw_cb_msb, sv);
2846b2167459SRichard Henderson     }
2847b2167459SRichard Henderson 
2848b2167459SRichard Henderson     tcg_temp_free(add1);
2849b2167459SRichard Henderson     tcg_temp_free(add2);
2850b2167459SRichard Henderson     tcg_temp_free(dest);
2851b2167459SRichard Henderson 
285231234768SRichard Henderson     return nullify_end(ctx);
2853b2167459SRichard Henderson }
2854b2167459SRichard Henderson 
28550588e061SRichard Henderson static bool trans_addi(DisasContext *ctx, arg_rri_cf *a)
2856b2167459SRichard Henderson {
28570588e061SRichard Henderson     return do_add_imm(ctx, a, false, false);
28580588e061SRichard Henderson }
28590588e061SRichard Henderson 
28600588e061SRichard Henderson static bool trans_addi_tsv(DisasContext *ctx, arg_rri_cf *a)
28610588e061SRichard Henderson {
28620588e061SRichard Henderson     return do_add_imm(ctx, a, true, false);
28630588e061SRichard Henderson }
28640588e061SRichard Henderson 
28650588e061SRichard Henderson static bool trans_addi_tc(DisasContext *ctx, arg_rri_cf *a)
28660588e061SRichard Henderson {
28670588e061SRichard Henderson     return do_add_imm(ctx, a, false, true);
28680588e061SRichard Henderson }
28690588e061SRichard Henderson 
28700588e061SRichard Henderson static bool trans_addi_tc_tsv(DisasContext *ctx, arg_rri_cf *a)
28710588e061SRichard Henderson {
28720588e061SRichard Henderson     return do_add_imm(ctx, a, true, true);
28730588e061SRichard Henderson }
28740588e061SRichard Henderson 
28750588e061SRichard Henderson static bool trans_subi(DisasContext *ctx, arg_rri_cf *a)
28760588e061SRichard Henderson {
28770588e061SRichard Henderson     return do_sub_imm(ctx, a, false);
28780588e061SRichard Henderson }
28790588e061SRichard Henderson 
28800588e061SRichard Henderson static bool trans_subi_tsv(DisasContext *ctx, arg_rri_cf *a)
28810588e061SRichard Henderson {
28820588e061SRichard Henderson     return do_sub_imm(ctx, a, true);
28830588e061SRichard Henderson }
28840588e061SRichard Henderson 
28850588e061SRichard Henderson static bool trans_cmpiclr(DisasContext *ctx, arg_rri_cf *a)
28860588e061SRichard Henderson {
2887eaa3783bSRichard Henderson     TCGv_reg tcg_im, tcg_r2;
2888b2167459SRichard Henderson 
28890588e061SRichard Henderson     if (a->cf) {
2890b2167459SRichard Henderson         nullify_over(ctx);
2891b2167459SRichard Henderson     }
2892b2167459SRichard Henderson 
28930588e061SRichard Henderson     tcg_im = load_const(ctx, a->i);
28940588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
28950588e061SRichard Henderson     do_cmpclr(ctx, a->t, tcg_im, tcg_r2, a->cf);
2896b2167459SRichard Henderson 
289731234768SRichard Henderson     return nullify_end(ctx);
2898b2167459SRichard Henderson }
2899b2167459SRichard Henderson 
29001cd012a5SRichard Henderson static bool trans_ld(DisasContext *ctx, arg_ldst *a)
290196d6407fSRichard Henderson {
2902*0786a3b6SHelge Deller     if (unlikely(TARGET_REGISTER_BITS == 32 && a->size > MO_32)) {
2903*0786a3b6SHelge Deller         return gen_illegal(ctx);
2904*0786a3b6SHelge Deller     } else {
29051cd012a5SRichard Henderson         return do_load(ctx, a->t, a->b, a->x, a->scale ? a->size : 0,
29061cd012a5SRichard Henderson                    a->disp, a->sp, a->m, a->size | MO_TE);
290796d6407fSRichard Henderson     }
2908*0786a3b6SHelge Deller }
290996d6407fSRichard Henderson 
29101cd012a5SRichard Henderson static bool trans_st(DisasContext *ctx, arg_ldst *a)
291196d6407fSRichard Henderson {
29121cd012a5SRichard Henderson     assert(a->x == 0 && a->scale == 0);
2913*0786a3b6SHelge Deller     if (unlikely(TARGET_REGISTER_BITS == 32 && a->size > MO_32)) {
2914*0786a3b6SHelge Deller         return gen_illegal(ctx);
2915*0786a3b6SHelge Deller     } else {
29161cd012a5SRichard Henderson         return do_store(ctx, a->t, a->b, a->disp, a->sp, a->m, a->size | MO_TE);
291796d6407fSRichard Henderson     }
2918*0786a3b6SHelge Deller }
291996d6407fSRichard Henderson 
29201cd012a5SRichard Henderson static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
292196d6407fSRichard Henderson {
2922b1af755cSRichard Henderson     MemOp mop = MO_TE | MO_ALIGN | a->size;
292386f8d05fSRichard Henderson     TCGv_reg zero, dest, ofs;
292486f8d05fSRichard Henderson     TCGv_tl addr;
292596d6407fSRichard Henderson 
292696d6407fSRichard Henderson     nullify_over(ctx);
292796d6407fSRichard Henderson 
29281cd012a5SRichard Henderson     if (a->m) {
292986f8d05fSRichard Henderson         /* Base register modification.  Make sure if RT == RB,
293086f8d05fSRichard Henderson            we see the result of the load.  */
293196d6407fSRichard Henderson         dest = get_temp(ctx);
293296d6407fSRichard Henderson     } else {
29331cd012a5SRichard Henderson         dest = dest_gpr(ctx, a->t);
293496d6407fSRichard Henderson     }
293596d6407fSRichard Henderson 
29361cd012a5SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? a->size : 0,
29371cd012a5SRichard Henderson              a->disp, a->sp, a->m, ctx->mmu_idx == MMU_PHYS_IDX);
2938b1af755cSRichard Henderson 
2939b1af755cSRichard Henderson     /*
2940b1af755cSRichard Henderson      * For hppa1.1, LDCW is undefined unless aligned mod 16.
2941b1af755cSRichard Henderson      * However actual hardware succeeds with aligned mod 4.
2942b1af755cSRichard Henderson      * Detect this case and log a GUEST_ERROR.
2943b1af755cSRichard Henderson      *
2944b1af755cSRichard Henderson      * TODO: HPPA64 relaxes the over-alignment requirement
2945b1af755cSRichard Henderson      * with the ,co completer.
2946b1af755cSRichard Henderson      */
2947b1af755cSRichard Henderson     gen_helper_ldc_check(addr);
2948b1af755cSRichard Henderson 
294929dd6f64SRichard Henderson     zero = tcg_constant_reg(0);
295086f8d05fSRichard Henderson     tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
2951b1af755cSRichard Henderson 
29521cd012a5SRichard Henderson     if (a->m) {
29531cd012a5SRichard Henderson         save_gpr(ctx, a->b, ofs);
295496d6407fSRichard Henderson     }
29551cd012a5SRichard Henderson     save_gpr(ctx, a->t, dest);
295696d6407fSRichard Henderson 
295731234768SRichard Henderson     return nullify_end(ctx);
295896d6407fSRichard Henderson }
295996d6407fSRichard Henderson 
29601cd012a5SRichard Henderson static bool trans_stby(DisasContext *ctx, arg_stby *a)
296196d6407fSRichard Henderson {
296286f8d05fSRichard Henderson     TCGv_reg ofs, val;
296386f8d05fSRichard Henderson     TCGv_tl addr;
296496d6407fSRichard Henderson 
296596d6407fSRichard Henderson     nullify_over(ctx);
296696d6407fSRichard Henderson 
29671cd012a5SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
296886f8d05fSRichard Henderson              ctx->mmu_idx == MMU_PHYS_IDX);
29691cd012a5SRichard Henderson     val = load_gpr(ctx, a->r);
29701cd012a5SRichard Henderson     if (a->a) {
2971f9f46db4SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2972f9f46db4SEmilio G. Cota             gen_helper_stby_e_parallel(cpu_env, addr, val);
2973f9f46db4SEmilio G. Cota         } else {
297496d6407fSRichard Henderson             gen_helper_stby_e(cpu_env, addr, val);
2975f9f46db4SEmilio G. Cota         }
2976f9f46db4SEmilio G. Cota     } else {
2977f9f46db4SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2978f9f46db4SEmilio G. Cota             gen_helper_stby_b_parallel(cpu_env, addr, val);
297996d6407fSRichard Henderson         } else {
298096d6407fSRichard Henderson             gen_helper_stby_b(cpu_env, addr, val);
298196d6407fSRichard Henderson         }
2982f9f46db4SEmilio G. Cota     }
29831cd012a5SRichard Henderson     if (a->m) {
298486f8d05fSRichard Henderson         tcg_gen_andi_reg(ofs, ofs, ~3);
29851cd012a5SRichard Henderson         save_gpr(ctx, a->b, ofs);
298696d6407fSRichard Henderson     }
298796d6407fSRichard Henderson 
298831234768SRichard Henderson     return nullify_end(ctx);
298996d6407fSRichard Henderson }
299096d6407fSRichard Henderson 
29911cd012a5SRichard Henderson static bool trans_lda(DisasContext *ctx, arg_ldst *a)
2992d0a851ccSRichard Henderson {
2993d0a851ccSRichard Henderson     int hold_mmu_idx = ctx->mmu_idx;
2994d0a851ccSRichard Henderson 
2995d0a851ccSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2996d0a851ccSRichard Henderson     ctx->mmu_idx = MMU_PHYS_IDX;
29971cd012a5SRichard Henderson     trans_ld(ctx, a);
2998d0a851ccSRichard Henderson     ctx->mmu_idx = hold_mmu_idx;
299931234768SRichard Henderson     return true;
3000d0a851ccSRichard Henderson }
3001d0a851ccSRichard Henderson 
30021cd012a5SRichard Henderson static bool trans_sta(DisasContext *ctx, arg_ldst *a)
3003d0a851ccSRichard Henderson {
3004d0a851ccSRichard Henderson     int hold_mmu_idx = ctx->mmu_idx;
3005d0a851ccSRichard Henderson 
3006d0a851ccSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
3007d0a851ccSRichard Henderson     ctx->mmu_idx = MMU_PHYS_IDX;
30081cd012a5SRichard Henderson     trans_st(ctx, a);
3009d0a851ccSRichard Henderson     ctx->mmu_idx = hold_mmu_idx;
301031234768SRichard Henderson     return true;
3011d0a851ccSRichard Henderson }
301295412a61SRichard Henderson 
30130588e061SRichard Henderson static bool trans_ldil(DisasContext *ctx, arg_ldil *a)
3014b2167459SRichard Henderson {
30150588e061SRichard Henderson     TCGv_reg tcg_rt = dest_gpr(ctx, a->t);
3016b2167459SRichard Henderson 
30170588e061SRichard Henderson     tcg_gen_movi_reg(tcg_rt, a->i);
30180588e061SRichard Henderson     save_gpr(ctx, a->t, tcg_rt);
3019b2167459SRichard Henderson     cond_free(&ctx->null_cond);
302031234768SRichard Henderson     return true;
3021b2167459SRichard Henderson }
3022b2167459SRichard Henderson 
30230588e061SRichard Henderson static bool trans_addil(DisasContext *ctx, arg_addil *a)
3024b2167459SRichard Henderson {
30250588e061SRichard Henderson     TCGv_reg tcg_rt = load_gpr(ctx, a->r);
3026eaa3783bSRichard Henderson     TCGv_reg tcg_r1 = dest_gpr(ctx, 1);
3027b2167459SRichard Henderson 
30280588e061SRichard Henderson     tcg_gen_addi_reg(tcg_r1, tcg_rt, a->i);
3029b2167459SRichard Henderson     save_gpr(ctx, 1, tcg_r1);
3030b2167459SRichard Henderson     cond_free(&ctx->null_cond);
303131234768SRichard Henderson     return true;
3032b2167459SRichard Henderson }
3033b2167459SRichard Henderson 
30340588e061SRichard Henderson static bool trans_ldo(DisasContext *ctx, arg_ldo *a)
3035b2167459SRichard Henderson {
30360588e061SRichard Henderson     TCGv_reg tcg_rt = dest_gpr(ctx, a->t);
3037b2167459SRichard Henderson 
3038b2167459SRichard Henderson     /* Special case rb == 0, for the LDI pseudo-op.
3039b2167459SRichard Henderson        The COPY pseudo-op is handled for free within tcg_gen_addi_tl.  */
30400588e061SRichard Henderson     if (a->b == 0) {
30410588e061SRichard Henderson         tcg_gen_movi_reg(tcg_rt, a->i);
3042b2167459SRichard Henderson     } else {
30430588e061SRichard Henderson         tcg_gen_addi_reg(tcg_rt, cpu_gr[a->b], a->i);
3044b2167459SRichard Henderson     }
30450588e061SRichard Henderson     save_gpr(ctx, a->t, tcg_rt);
3046b2167459SRichard Henderson     cond_free(&ctx->null_cond);
304731234768SRichard Henderson     return true;
3048b2167459SRichard Henderson }
3049b2167459SRichard Henderson 
305001afb7beSRichard Henderson static bool do_cmpb(DisasContext *ctx, unsigned r, TCGv_reg in1,
305101afb7beSRichard Henderson                     unsigned c, unsigned f, unsigned n, int disp)
305298cd9ca7SRichard Henderson {
305301afb7beSRichard Henderson     TCGv_reg dest, in2, sv;
305498cd9ca7SRichard Henderson     DisasCond cond;
305598cd9ca7SRichard Henderson 
305698cd9ca7SRichard Henderson     in2 = load_gpr(ctx, r);
305798cd9ca7SRichard Henderson     dest = get_temp(ctx);
305898cd9ca7SRichard Henderson 
3059eaa3783bSRichard Henderson     tcg_gen_sub_reg(dest, in1, in2);
306098cd9ca7SRichard Henderson 
3061f764718dSRichard Henderson     sv = NULL;
3062b47a4a02SSven Schnelle     if (cond_need_sv(c)) {
306398cd9ca7SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
306498cd9ca7SRichard Henderson     }
306598cd9ca7SRichard Henderson 
306601afb7beSRichard Henderson     cond = do_sub_cond(c * 2 + f, dest, in1, in2, sv);
306701afb7beSRichard Henderson     return do_cbranch(ctx, disp, n, &cond);
306898cd9ca7SRichard Henderson }
306998cd9ca7SRichard Henderson 
307001afb7beSRichard Henderson static bool trans_cmpb(DisasContext *ctx, arg_cmpb *a)
307198cd9ca7SRichard Henderson {
307201afb7beSRichard Henderson     nullify_over(ctx);
307301afb7beSRichard Henderson     return do_cmpb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
307401afb7beSRichard Henderson }
307501afb7beSRichard Henderson 
307601afb7beSRichard Henderson static bool trans_cmpbi(DisasContext *ctx, arg_cmpbi *a)
307701afb7beSRichard Henderson {
307801afb7beSRichard Henderson     nullify_over(ctx);
307901afb7beSRichard Henderson     return do_cmpb(ctx, a->r, load_const(ctx, a->i), a->c, a->f, a->n, a->disp);
308001afb7beSRichard Henderson }
308101afb7beSRichard Henderson 
308201afb7beSRichard Henderson static bool do_addb(DisasContext *ctx, unsigned r, TCGv_reg in1,
308301afb7beSRichard Henderson                     unsigned c, unsigned f, unsigned n, int disp)
308401afb7beSRichard Henderson {
308501afb7beSRichard Henderson     TCGv_reg dest, in2, sv, cb_msb;
308698cd9ca7SRichard Henderson     DisasCond cond;
308798cd9ca7SRichard Henderson 
308898cd9ca7SRichard Henderson     in2 = load_gpr(ctx, r);
308943675d20SSven Schnelle     dest = tcg_temp_new();
3090f764718dSRichard Henderson     sv = NULL;
3091f764718dSRichard Henderson     cb_msb = NULL;
309298cd9ca7SRichard Henderson 
3093b47a4a02SSven Schnelle     if (cond_need_cb(c)) {
309498cd9ca7SRichard Henderson         cb_msb = get_temp(ctx);
3095eaa3783bSRichard Henderson         tcg_gen_movi_reg(cb_msb, 0);
3096eaa3783bSRichard Henderson         tcg_gen_add2_reg(dest, cb_msb, in1, cb_msb, in2, cb_msb);
3097b47a4a02SSven Schnelle     } else {
3098eaa3783bSRichard Henderson         tcg_gen_add_reg(dest, in1, in2);
3099b47a4a02SSven Schnelle     }
3100b47a4a02SSven Schnelle     if (cond_need_sv(c)) {
310198cd9ca7SRichard Henderson         sv = do_add_sv(ctx, dest, in1, in2);
310298cd9ca7SRichard Henderson     }
310398cd9ca7SRichard Henderson 
310401afb7beSRichard Henderson     cond = do_cond(c * 2 + f, dest, cb_msb, sv);
310543675d20SSven Schnelle     save_gpr(ctx, r, dest);
310643675d20SSven Schnelle     tcg_temp_free(dest);
310701afb7beSRichard Henderson     return do_cbranch(ctx, disp, n, &cond);
310898cd9ca7SRichard Henderson }
310998cd9ca7SRichard Henderson 
311001afb7beSRichard Henderson static bool trans_addb(DisasContext *ctx, arg_addb *a)
311198cd9ca7SRichard Henderson {
311201afb7beSRichard Henderson     nullify_over(ctx);
311301afb7beSRichard Henderson     return do_addb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
311401afb7beSRichard Henderson }
311501afb7beSRichard Henderson 
311601afb7beSRichard Henderson static bool trans_addbi(DisasContext *ctx, arg_addbi *a)
311701afb7beSRichard Henderson {
311801afb7beSRichard Henderson     nullify_over(ctx);
311901afb7beSRichard Henderson     return do_addb(ctx, a->r, load_const(ctx, a->i), a->c, a->f, a->n, a->disp);
312001afb7beSRichard Henderson }
312101afb7beSRichard Henderson 
312201afb7beSRichard Henderson static bool trans_bb_sar(DisasContext *ctx, arg_bb_sar *a)
312301afb7beSRichard Henderson {
3124eaa3783bSRichard Henderson     TCGv_reg tmp, tcg_r;
312598cd9ca7SRichard Henderson     DisasCond cond;
312698cd9ca7SRichard Henderson 
312798cd9ca7SRichard Henderson     nullify_over(ctx);
312898cd9ca7SRichard Henderson 
312998cd9ca7SRichard Henderson     tmp = tcg_temp_new();
313001afb7beSRichard Henderson     tcg_r = load_gpr(ctx, a->r);
3131eaa3783bSRichard Henderson     tcg_gen_shl_reg(tmp, tcg_r, cpu_sar);
313298cd9ca7SRichard Henderson 
313301afb7beSRichard Henderson     cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
313498cd9ca7SRichard Henderson     tcg_temp_free(tmp);
313501afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
313698cd9ca7SRichard Henderson }
313798cd9ca7SRichard Henderson 
313801afb7beSRichard Henderson static bool trans_bb_imm(DisasContext *ctx, arg_bb_imm *a)
313998cd9ca7SRichard Henderson {
314001afb7beSRichard Henderson     TCGv_reg tmp, tcg_r;
314101afb7beSRichard Henderson     DisasCond cond;
314201afb7beSRichard Henderson 
314301afb7beSRichard Henderson     nullify_over(ctx);
314401afb7beSRichard Henderson 
314501afb7beSRichard Henderson     tmp = tcg_temp_new();
314601afb7beSRichard Henderson     tcg_r = load_gpr(ctx, a->r);
314701afb7beSRichard Henderson     tcg_gen_shli_reg(tmp, tcg_r, a->p);
314801afb7beSRichard Henderson 
314901afb7beSRichard Henderson     cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
315001afb7beSRichard Henderson     tcg_temp_free(tmp);
315101afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
315201afb7beSRichard Henderson }
315301afb7beSRichard Henderson 
315401afb7beSRichard Henderson static bool trans_movb(DisasContext *ctx, arg_movb *a)
315501afb7beSRichard Henderson {
3156eaa3783bSRichard Henderson     TCGv_reg dest;
315798cd9ca7SRichard Henderson     DisasCond cond;
315898cd9ca7SRichard Henderson 
315998cd9ca7SRichard Henderson     nullify_over(ctx);
316098cd9ca7SRichard Henderson 
316101afb7beSRichard Henderson     dest = dest_gpr(ctx, a->r2);
316201afb7beSRichard Henderson     if (a->r1 == 0) {
3163eaa3783bSRichard Henderson         tcg_gen_movi_reg(dest, 0);
316498cd9ca7SRichard Henderson     } else {
316501afb7beSRichard Henderson         tcg_gen_mov_reg(dest, cpu_gr[a->r1]);
316698cd9ca7SRichard Henderson     }
316798cd9ca7SRichard Henderson 
316801afb7beSRichard Henderson     cond = do_sed_cond(a->c, dest);
316901afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
317001afb7beSRichard Henderson }
317101afb7beSRichard Henderson 
317201afb7beSRichard Henderson static bool trans_movbi(DisasContext *ctx, arg_movbi *a)
317301afb7beSRichard Henderson {
317401afb7beSRichard Henderson     TCGv_reg dest;
317501afb7beSRichard Henderson     DisasCond cond;
317601afb7beSRichard Henderson 
317701afb7beSRichard Henderson     nullify_over(ctx);
317801afb7beSRichard Henderson 
317901afb7beSRichard Henderson     dest = dest_gpr(ctx, a->r);
318001afb7beSRichard Henderson     tcg_gen_movi_reg(dest, a->i);
318101afb7beSRichard Henderson 
318201afb7beSRichard Henderson     cond = do_sed_cond(a->c, dest);
318301afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
318498cd9ca7SRichard Henderson }
318598cd9ca7SRichard Henderson 
318630878590SRichard Henderson static bool trans_shrpw_sar(DisasContext *ctx, arg_shrpw_sar *a)
31870b1347d2SRichard Henderson {
3188eaa3783bSRichard Henderson     TCGv_reg dest;
31890b1347d2SRichard Henderson 
319030878590SRichard Henderson     if (a->c) {
31910b1347d2SRichard Henderson         nullify_over(ctx);
31920b1347d2SRichard Henderson     }
31930b1347d2SRichard Henderson 
319430878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
319530878590SRichard Henderson     if (a->r1 == 0) {
319630878590SRichard Henderson         tcg_gen_ext32u_reg(dest, load_gpr(ctx, a->r2));
3197eaa3783bSRichard Henderson         tcg_gen_shr_reg(dest, dest, cpu_sar);
319830878590SRichard Henderson     } else if (a->r1 == a->r2) {
31990b1347d2SRichard Henderson         TCGv_i32 t32 = tcg_temp_new_i32();
320030878590SRichard Henderson         tcg_gen_trunc_reg_i32(t32, load_gpr(ctx, a->r2));
32010b1347d2SRichard Henderson         tcg_gen_rotr_i32(t32, t32, cpu_sar);
3202eaa3783bSRichard Henderson         tcg_gen_extu_i32_reg(dest, t32);
32030b1347d2SRichard Henderson         tcg_temp_free_i32(t32);
32040b1347d2SRichard Henderson     } else {
32050b1347d2SRichard Henderson         TCGv_i64 t = tcg_temp_new_i64();
32060b1347d2SRichard Henderson         TCGv_i64 s = tcg_temp_new_i64();
32070b1347d2SRichard Henderson 
320830878590SRichard Henderson         tcg_gen_concat_reg_i64(t, load_gpr(ctx, a->r2), load_gpr(ctx, a->r1));
3209eaa3783bSRichard Henderson         tcg_gen_extu_reg_i64(s, cpu_sar);
32100b1347d2SRichard Henderson         tcg_gen_shr_i64(t, t, s);
3211eaa3783bSRichard Henderson         tcg_gen_trunc_i64_reg(dest, t);
32120b1347d2SRichard Henderson 
32130b1347d2SRichard Henderson         tcg_temp_free_i64(t);
32140b1347d2SRichard Henderson         tcg_temp_free_i64(s);
32150b1347d2SRichard Henderson     }
321630878590SRichard Henderson     save_gpr(ctx, a->t, dest);
32170b1347d2SRichard Henderson 
32180b1347d2SRichard Henderson     /* Install the new nullification.  */
32190b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
322030878590SRichard Henderson     if (a->c) {
322130878590SRichard Henderson         ctx->null_cond = do_sed_cond(a->c, dest);
32220b1347d2SRichard Henderson     }
322331234768SRichard Henderson     return nullify_end(ctx);
32240b1347d2SRichard Henderson }
32250b1347d2SRichard Henderson 
322630878590SRichard Henderson static bool trans_shrpw_imm(DisasContext *ctx, arg_shrpw_imm *a)
32270b1347d2SRichard Henderson {
322830878590SRichard Henderson     unsigned sa = 31 - a->cpos;
3229eaa3783bSRichard Henderson     TCGv_reg dest, t2;
32300b1347d2SRichard Henderson 
323130878590SRichard Henderson     if (a->c) {
32320b1347d2SRichard Henderson         nullify_over(ctx);
32330b1347d2SRichard Henderson     }
32340b1347d2SRichard Henderson 
323530878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
323630878590SRichard Henderson     t2 = load_gpr(ctx, a->r2);
323705bfd4dbSRichard Henderson     if (a->r1 == 0) {
323805bfd4dbSRichard Henderson         tcg_gen_extract_reg(dest, t2, sa, 32 - sa);
323905bfd4dbSRichard Henderson     } else if (TARGET_REGISTER_BITS == 32) {
324005bfd4dbSRichard Henderson         tcg_gen_extract2_reg(dest, t2, cpu_gr[a->r1], sa);
324105bfd4dbSRichard Henderson     } else if (a->r1 == a->r2) {
32420b1347d2SRichard Henderson         TCGv_i32 t32 = tcg_temp_new_i32();
3243eaa3783bSRichard Henderson         tcg_gen_trunc_reg_i32(t32, t2);
32440b1347d2SRichard Henderson         tcg_gen_rotri_i32(t32, t32, sa);
3245eaa3783bSRichard Henderson         tcg_gen_extu_i32_reg(dest, t32);
32460b1347d2SRichard Henderson         tcg_temp_free_i32(t32);
32470b1347d2SRichard Henderson     } else {
324805bfd4dbSRichard Henderson         TCGv_i64 t64 = tcg_temp_new_i64();
324905bfd4dbSRichard Henderson         tcg_gen_concat_reg_i64(t64, t2, cpu_gr[a->r1]);
325005bfd4dbSRichard Henderson         tcg_gen_shri_i64(t64, t64, sa);
325105bfd4dbSRichard Henderson         tcg_gen_trunc_i64_reg(dest, t64);
325205bfd4dbSRichard Henderson         tcg_temp_free_i64(t64);
32530b1347d2SRichard Henderson     }
325430878590SRichard Henderson     save_gpr(ctx, a->t, dest);
32550b1347d2SRichard Henderson 
32560b1347d2SRichard Henderson     /* Install the new nullification.  */
32570b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
325830878590SRichard Henderson     if (a->c) {
325930878590SRichard Henderson         ctx->null_cond = do_sed_cond(a->c, dest);
32600b1347d2SRichard Henderson     }
326131234768SRichard Henderson     return nullify_end(ctx);
32620b1347d2SRichard Henderson }
32630b1347d2SRichard Henderson 
326430878590SRichard Henderson static bool trans_extrw_sar(DisasContext *ctx, arg_extrw_sar *a)
32650b1347d2SRichard Henderson {
326630878590SRichard Henderson     unsigned len = 32 - a->clen;
3267eaa3783bSRichard Henderson     TCGv_reg dest, src, tmp;
32680b1347d2SRichard Henderson 
326930878590SRichard Henderson     if (a->c) {
32700b1347d2SRichard Henderson         nullify_over(ctx);
32710b1347d2SRichard Henderson     }
32720b1347d2SRichard Henderson 
327330878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
327430878590SRichard Henderson     src = load_gpr(ctx, a->r);
32750b1347d2SRichard Henderson     tmp = tcg_temp_new();
32760b1347d2SRichard Henderson 
32770b1347d2SRichard Henderson     /* Recall that SAR is using big-endian bit numbering.  */
3278eaa3783bSRichard Henderson     tcg_gen_xori_reg(tmp, cpu_sar, TARGET_REGISTER_BITS - 1);
327930878590SRichard Henderson     if (a->se) {
3280eaa3783bSRichard Henderson         tcg_gen_sar_reg(dest, src, tmp);
3281eaa3783bSRichard Henderson         tcg_gen_sextract_reg(dest, dest, 0, len);
32820b1347d2SRichard Henderson     } else {
3283eaa3783bSRichard Henderson         tcg_gen_shr_reg(dest, src, tmp);
3284eaa3783bSRichard Henderson         tcg_gen_extract_reg(dest, dest, 0, len);
32850b1347d2SRichard Henderson     }
32860b1347d2SRichard Henderson     tcg_temp_free(tmp);
328730878590SRichard Henderson     save_gpr(ctx, a->t, dest);
32880b1347d2SRichard Henderson 
32890b1347d2SRichard Henderson     /* Install the new nullification.  */
32900b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
329130878590SRichard Henderson     if (a->c) {
329230878590SRichard Henderson         ctx->null_cond = do_sed_cond(a->c, dest);
32930b1347d2SRichard Henderson     }
329431234768SRichard Henderson     return nullify_end(ctx);
32950b1347d2SRichard Henderson }
32960b1347d2SRichard Henderson 
329730878590SRichard Henderson static bool trans_extrw_imm(DisasContext *ctx, arg_extrw_imm *a)
32980b1347d2SRichard Henderson {
329930878590SRichard Henderson     unsigned len = 32 - a->clen;
330030878590SRichard Henderson     unsigned cpos = 31 - a->pos;
3301eaa3783bSRichard Henderson     TCGv_reg dest, src;
33020b1347d2SRichard Henderson 
330330878590SRichard Henderson     if (a->c) {
33040b1347d2SRichard Henderson         nullify_over(ctx);
33050b1347d2SRichard Henderson     }
33060b1347d2SRichard Henderson 
330730878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
330830878590SRichard Henderson     src = load_gpr(ctx, a->r);
330930878590SRichard Henderson     if (a->se) {
3310eaa3783bSRichard Henderson         tcg_gen_sextract_reg(dest, src, cpos, len);
33110b1347d2SRichard Henderson     } else {
3312eaa3783bSRichard Henderson         tcg_gen_extract_reg(dest, src, cpos, len);
33130b1347d2SRichard Henderson     }
331430878590SRichard Henderson     save_gpr(ctx, a->t, dest);
33150b1347d2SRichard Henderson 
33160b1347d2SRichard Henderson     /* Install the new nullification.  */
33170b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
331830878590SRichard Henderson     if (a->c) {
331930878590SRichard Henderson         ctx->null_cond = do_sed_cond(a->c, dest);
33200b1347d2SRichard Henderson     }
332131234768SRichard Henderson     return nullify_end(ctx);
33220b1347d2SRichard Henderson }
33230b1347d2SRichard Henderson 
332430878590SRichard Henderson static bool trans_depwi_imm(DisasContext *ctx, arg_depwi_imm *a)
33250b1347d2SRichard Henderson {
332630878590SRichard Henderson     unsigned len = 32 - a->clen;
3327eaa3783bSRichard Henderson     target_sreg mask0, mask1;
3328eaa3783bSRichard Henderson     TCGv_reg dest;
33290b1347d2SRichard Henderson 
333030878590SRichard Henderson     if (a->c) {
33310b1347d2SRichard Henderson         nullify_over(ctx);
33320b1347d2SRichard Henderson     }
333330878590SRichard Henderson     if (a->cpos + len > 32) {
333430878590SRichard Henderson         len = 32 - a->cpos;
33350b1347d2SRichard Henderson     }
33360b1347d2SRichard Henderson 
333730878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
333830878590SRichard Henderson     mask0 = deposit64(0, a->cpos, len, a->i);
333930878590SRichard Henderson     mask1 = deposit64(-1, a->cpos, len, a->i);
33400b1347d2SRichard Henderson 
334130878590SRichard Henderson     if (a->nz) {
334230878590SRichard Henderson         TCGv_reg src = load_gpr(ctx, a->t);
33430b1347d2SRichard Henderson         if (mask1 != -1) {
3344eaa3783bSRichard Henderson             tcg_gen_andi_reg(dest, src, mask1);
33450b1347d2SRichard Henderson             src = dest;
33460b1347d2SRichard Henderson         }
3347eaa3783bSRichard Henderson         tcg_gen_ori_reg(dest, src, mask0);
33480b1347d2SRichard Henderson     } else {
3349eaa3783bSRichard Henderson         tcg_gen_movi_reg(dest, mask0);
33500b1347d2SRichard Henderson     }
335130878590SRichard Henderson     save_gpr(ctx, a->t, dest);
33520b1347d2SRichard Henderson 
33530b1347d2SRichard Henderson     /* Install the new nullification.  */
33540b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
335530878590SRichard Henderson     if (a->c) {
335630878590SRichard Henderson         ctx->null_cond = do_sed_cond(a->c, dest);
33570b1347d2SRichard Henderson     }
335831234768SRichard Henderson     return nullify_end(ctx);
33590b1347d2SRichard Henderson }
33600b1347d2SRichard Henderson 
336130878590SRichard Henderson static bool trans_depw_imm(DisasContext *ctx, arg_depw_imm *a)
33620b1347d2SRichard Henderson {
336330878590SRichard Henderson     unsigned rs = a->nz ? a->t : 0;
336430878590SRichard Henderson     unsigned len = 32 - a->clen;
3365eaa3783bSRichard Henderson     TCGv_reg dest, val;
33660b1347d2SRichard Henderson 
336730878590SRichard Henderson     if (a->c) {
33680b1347d2SRichard Henderson         nullify_over(ctx);
33690b1347d2SRichard Henderson     }
337030878590SRichard Henderson     if (a->cpos + len > 32) {
337130878590SRichard Henderson         len = 32 - a->cpos;
33720b1347d2SRichard Henderson     }
33730b1347d2SRichard Henderson 
337430878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
337530878590SRichard Henderson     val = load_gpr(ctx, a->r);
33760b1347d2SRichard Henderson     if (rs == 0) {
337730878590SRichard Henderson         tcg_gen_deposit_z_reg(dest, val, a->cpos, len);
33780b1347d2SRichard Henderson     } else {
337930878590SRichard Henderson         tcg_gen_deposit_reg(dest, cpu_gr[rs], val, a->cpos, len);
33800b1347d2SRichard Henderson     }
338130878590SRichard Henderson     save_gpr(ctx, a->t, dest);
33820b1347d2SRichard Henderson 
33830b1347d2SRichard Henderson     /* Install the new nullification.  */
33840b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
338530878590SRichard Henderson     if (a->c) {
338630878590SRichard Henderson         ctx->null_cond = do_sed_cond(a->c, dest);
33870b1347d2SRichard Henderson     }
338831234768SRichard Henderson     return nullify_end(ctx);
33890b1347d2SRichard Henderson }
33900b1347d2SRichard Henderson 
339130878590SRichard Henderson static bool do_depw_sar(DisasContext *ctx, unsigned rt, unsigned c,
339230878590SRichard Henderson                         unsigned nz, unsigned clen, TCGv_reg val)
33930b1347d2SRichard Henderson {
33940b1347d2SRichard Henderson     unsigned rs = nz ? rt : 0;
33950b1347d2SRichard Henderson     unsigned len = 32 - clen;
339630878590SRichard Henderson     TCGv_reg mask, tmp, shift, dest;
33970b1347d2SRichard Henderson     unsigned msb = 1U << (len - 1);
33980b1347d2SRichard Henderson 
33990b1347d2SRichard Henderson     dest = dest_gpr(ctx, rt);
34000b1347d2SRichard Henderson     shift = tcg_temp_new();
34010b1347d2SRichard Henderson     tmp = tcg_temp_new();
34020b1347d2SRichard Henderson 
34030b1347d2SRichard Henderson     /* Convert big-endian bit numbering in SAR to left-shift.  */
3404eaa3783bSRichard Henderson     tcg_gen_xori_reg(shift, cpu_sar, TARGET_REGISTER_BITS - 1);
34050b1347d2SRichard Henderson 
3406eaa3783bSRichard Henderson     mask = tcg_const_reg(msb + (msb - 1));
3407eaa3783bSRichard Henderson     tcg_gen_and_reg(tmp, val, mask);
34080b1347d2SRichard Henderson     if (rs) {
3409eaa3783bSRichard Henderson         tcg_gen_shl_reg(mask, mask, shift);
3410eaa3783bSRichard Henderson         tcg_gen_shl_reg(tmp, tmp, shift);
3411eaa3783bSRichard Henderson         tcg_gen_andc_reg(dest, cpu_gr[rs], mask);
3412eaa3783bSRichard Henderson         tcg_gen_or_reg(dest, dest, tmp);
34130b1347d2SRichard Henderson     } else {
3414eaa3783bSRichard Henderson         tcg_gen_shl_reg(dest, tmp, shift);
34150b1347d2SRichard Henderson     }
34160b1347d2SRichard Henderson     tcg_temp_free(shift);
34170b1347d2SRichard Henderson     tcg_temp_free(mask);
34180b1347d2SRichard Henderson     tcg_temp_free(tmp);
34190b1347d2SRichard Henderson     save_gpr(ctx, rt, dest);
34200b1347d2SRichard Henderson 
34210b1347d2SRichard Henderson     /* Install the new nullification.  */
34220b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
34230b1347d2SRichard Henderson     if (c) {
34240b1347d2SRichard Henderson         ctx->null_cond = do_sed_cond(c, dest);
34250b1347d2SRichard Henderson     }
342631234768SRichard Henderson     return nullify_end(ctx);
34270b1347d2SRichard Henderson }
34280b1347d2SRichard Henderson 
342930878590SRichard Henderson static bool trans_depw_sar(DisasContext *ctx, arg_depw_sar *a)
343030878590SRichard Henderson {
3431a6deecceSSven Schnelle     if (a->c) {
3432a6deecceSSven Schnelle         nullify_over(ctx);
3433a6deecceSSven Schnelle     }
343430878590SRichard Henderson     return do_depw_sar(ctx, a->t, a->c, a->nz, a->clen, load_gpr(ctx, a->r));
343530878590SRichard Henderson }
343630878590SRichard Henderson 
343730878590SRichard Henderson static bool trans_depwi_sar(DisasContext *ctx, arg_depwi_sar *a)
343830878590SRichard Henderson {
3439a6deecceSSven Schnelle     if (a->c) {
3440a6deecceSSven Schnelle         nullify_over(ctx);
3441a6deecceSSven Schnelle     }
344230878590SRichard Henderson     return do_depw_sar(ctx, a->t, a->c, a->nz, a->clen, load_const(ctx, a->i));
344330878590SRichard Henderson }
34440b1347d2SRichard Henderson 
34458340f534SRichard Henderson static bool trans_be(DisasContext *ctx, arg_be *a)
344698cd9ca7SRichard Henderson {
3447660eefe1SRichard Henderson     TCGv_reg tmp;
344898cd9ca7SRichard Henderson 
3449c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
345098cd9ca7SRichard Henderson     /* ??? It seems like there should be a good way of using
345198cd9ca7SRichard Henderson        "be disp(sr2, r0)", the canonical gateway entry mechanism
345298cd9ca7SRichard Henderson        to our advantage.  But that appears to be inconvenient to
345398cd9ca7SRichard Henderson        manage along side branch delay slots.  Therefore we handle
345498cd9ca7SRichard Henderson        entry into the gateway page via absolute address.  */
345598cd9ca7SRichard Henderson     /* Since we don't implement spaces, just branch.  Do notice the special
345698cd9ca7SRichard Henderson        case of "be disp(*,r0)" using a direct branch to disp, so that we can
345798cd9ca7SRichard Henderson        goto_tb to the TB containing the syscall.  */
34588340f534SRichard Henderson     if (a->b == 0) {
34598340f534SRichard Henderson         return do_dbranch(ctx, a->disp, a->l, a->n);
346098cd9ca7SRichard Henderson     }
3461c301f34eSRichard Henderson #else
3462c301f34eSRichard Henderson     nullify_over(ctx);
3463660eefe1SRichard Henderson #endif
3464660eefe1SRichard Henderson 
3465660eefe1SRichard Henderson     tmp = get_temp(ctx);
34668340f534SRichard Henderson     tcg_gen_addi_reg(tmp, load_gpr(ctx, a->b), a->disp);
3467660eefe1SRichard Henderson     tmp = do_ibranch_priv(ctx, tmp);
3468c301f34eSRichard Henderson 
3469c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
34708340f534SRichard Henderson     return do_ibranch(ctx, tmp, a->l, a->n);
3471c301f34eSRichard Henderson #else
3472c301f34eSRichard Henderson     TCGv_i64 new_spc = tcg_temp_new_i64();
3473c301f34eSRichard Henderson 
34748340f534SRichard Henderson     load_spr(ctx, new_spc, a->sp);
34758340f534SRichard Henderson     if (a->l) {
3476c301f34eSRichard Henderson         copy_iaoq_entry(cpu_gr[31], ctx->iaoq_n, ctx->iaoq_n_var);
3477c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_f);
3478c301f34eSRichard Henderson     }
34798340f534SRichard Henderson     if (a->n && use_nullify_skip(ctx)) {
3480c301f34eSRichard Henderson         tcg_gen_mov_reg(cpu_iaoq_f, tmp);
3481c301f34eSRichard Henderson         tcg_gen_addi_reg(cpu_iaoq_b, cpu_iaoq_f, 4);
3482c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_f, new_spc);
3483c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
3484c301f34eSRichard Henderson     } else {
3485c301f34eSRichard Henderson         copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3486c301f34eSRichard Henderson         if (ctx->iaoq_b == -1) {
3487c301f34eSRichard Henderson             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3488c301f34eSRichard Henderson         }
3489c301f34eSRichard Henderson         tcg_gen_mov_reg(cpu_iaoq_b, tmp);
3490c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_b, new_spc);
34918340f534SRichard Henderson         nullify_set(ctx, a->n);
3492c301f34eSRichard Henderson     }
3493c301f34eSRichard Henderson     tcg_temp_free_i64(new_spc);
3494c301f34eSRichard Henderson     tcg_gen_lookup_and_goto_ptr();
349531234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
349631234768SRichard Henderson     return nullify_end(ctx);
3497c301f34eSRichard Henderson #endif
349898cd9ca7SRichard Henderson }
349998cd9ca7SRichard Henderson 
35008340f534SRichard Henderson static bool trans_bl(DisasContext *ctx, arg_bl *a)
350198cd9ca7SRichard Henderson {
35028340f534SRichard Henderson     return do_dbranch(ctx, iaoq_dest(ctx, a->disp), a->l, a->n);
350398cd9ca7SRichard Henderson }
350498cd9ca7SRichard Henderson 
35058340f534SRichard Henderson static bool trans_b_gate(DisasContext *ctx, arg_b_gate *a)
350643e05652SRichard Henderson {
35078340f534SRichard Henderson     target_ureg dest = iaoq_dest(ctx, a->disp);
350843e05652SRichard Henderson 
35096e5f5300SSven Schnelle     nullify_over(ctx);
35106e5f5300SSven Schnelle 
351143e05652SRichard Henderson     /* Make sure the caller hasn't done something weird with the queue.
351243e05652SRichard Henderson      * ??? This is not quite the same as the PSW[B] bit, which would be
351343e05652SRichard Henderson      * expensive to track.  Real hardware will trap for
351443e05652SRichard Henderson      *    b  gateway
351543e05652SRichard Henderson      *    b  gateway+4  (in delay slot of first branch)
351643e05652SRichard Henderson      * However, checking for a non-sequential instruction queue *will*
351743e05652SRichard Henderson      * diagnose the security hole
351843e05652SRichard Henderson      *    b  gateway
351943e05652SRichard Henderson      *    b  evil
352043e05652SRichard Henderson      * in which instructions at evil would run with increased privs.
352143e05652SRichard Henderson      */
352243e05652SRichard Henderson     if (ctx->iaoq_b == -1 || ctx->iaoq_b != ctx->iaoq_f + 4) {
352343e05652SRichard Henderson         return gen_illegal(ctx);
352443e05652SRichard Henderson     }
352543e05652SRichard Henderson 
352643e05652SRichard Henderson #ifndef CONFIG_USER_ONLY
352743e05652SRichard Henderson     if (ctx->tb_flags & PSW_C) {
352843e05652SRichard Henderson         CPUHPPAState *env = ctx->cs->env_ptr;
352943e05652SRichard Henderson         int type = hppa_artype_for_page(env, ctx->base.pc_next);
353043e05652SRichard Henderson         /* If we could not find a TLB entry, then we need to generate an
353143e05652SRichard Henderson            ITLB miss exception so the kernel will provide it.
353243e05652SRichard Henderson            The resulting TLB fill operation will invalidate this TB and
353343e05652SRichard Henderson            we will re-translate, at which point we *will* be able to find
353443e05652SRichard Henderson            the TLB entry and determine if this is in fact a gateway page.  */
353543e05652SRichard Henderson         if (type < 0) {
353631234768SRichard Henderson             gen_excp(ctx, EXCP_ITLB_MISS);
353731234768SRichard Henderson             return true;
353843e05652SRichard Henderson         }
353943e05652SRichard Henderson         /* No change for non-gateway pages or for priv decrease.  */
354043e05652SRichard Henderson         if (type >= 4 && type - 4 < ctx->privilege) {
354143e05652SRichard Henderson             dest = deposit32(dest, 0, 2, type - 4);
354243e05652SRichard Henderson         }
354343e05652SRichard Henderson     } else {
354443e05652SRichard Henderson         dest &= -4;  /* priv = 0 */
354543e05652SRichard Henderson     }
354643e05652SRichard Henderson #endif
354743e05652SRichard Henderson 
35486e5f5300SSven Schnelle     if (a->l) {
35496e5f5300SSven Schnelle         TCGv_reg tmp = dest_gpr(ctx, a->l);
35506e5f5300SSven Schnelle         if (ctx->privilege < 3) {
35516e5f5300SSven Schnelle             tcg_gen_andi_reg(tmp, tmp, -4);
35526e5f5300SSven Schnelle         }
35536e5f5300SSven Schnelle         tcg_gen_ori_reg(tmp, tmp, ctx->privilege);
35546e5f5300SSven Schnelle         save_gpr(ctx, a->l, tmp);
35556e5f5300SSven Schnelle     }
35566e5f5300SSven Schnelle 
35576e5f5300SSven Schnelle     return do_dbranch(ctx, dest, 0, a->n);
355843e05652SRichard Henderson }
355943e05652SRichard Henderson 
35608340f534SRichard Henderson static bool trans_blr(DisasContext *ctx, arg_blr *a)
356198cd9ca7SRichard Henderson {
3562b35aec85SRichard Henderson     if (a->x) {
3563eaa3783bSRichard Henderson         TCGv_reg tmp = get_temp(ctx);
35648340f534SRichard Henderson         tcg_gen_shli_reg(tmp, load_gpr(ctx, a->x), 3);
3565eaa3783bSRichard Henderson         tcg_gen_addi_reg(tmp, tmp, ctx->iaoq_f + 8);
3566660eefe1SRichard Henderson         /* The computation here never changes privilege level.  */
35678340f534SRichard Henderson         return do_ibranch(ctx, tmp, a->l, a->n);
3568b35aec85SRichard Henderson     } else {
3569b35aec85SRichard Henderson         /* BLR R0,RX is a good way to load PC+8 into RX.  */
3570b35aec85SRichard Henderson         return do_dbranch(ctx, ctx->iaoq_f + 8, a->l, a->n);
3571b35aec85SRichard Henderson     }
357298cd9ca7SRichard Henderson }
357398cd9ca7SRichard Henderson 
35748340f534SRichard Henderson static bool trans_bv(DisasContext *ctx, arg_bv *a)
357598cd9ca7SRichard Henderson {
3576eaa3783bSRichard Henderson     TCGv_reg dest;
357798cd9ca7SRichard Henderson 
35788340f534SRichard Henderson     if (a->x == 0) {
35798340f534SRichard Henderson         dest = load_gpr(ctx, a->b);
358098cd9ca7SRichard Henderson     } else {
358198cd9ca7SRichard Henderson         dest = get_temp(ctx);
35828340f534SRichard Henderson         tcg_gen_shli_reg(dest, load_gpr(ctx, a->x), 3);
35838340f534SRichard Henderson         tcg_gen_add_reg(dest, dest, load_gpr(ctx, a->b));
358498cd9ca7SRichard Henderson     }
3585660eefe1SRichard Henderson     dest = do_ibranch_priv(ctx, dest);
35868340f534SRichard Henderson     return do_ibranch(ctx, dest, 0, a->n);
358798cd9ca7SRichard Henderson }
358898cd9ca7SRichard Henderson 
35898340f534SRichard Henderson static bool trans_bve(DisasContext *ctx, arg_bve *a)
359098cd9ca7SRichard Henderson {
3591660eefe1SRichard Henderson     TCGv_reg dest;
359298cd9ca7SRichard Henderson 
3593c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
35948340f534SRichard Henderson     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
35958340f534SRichard Henderson     return do_ibranch(ctx, dest, a->l, a->n);
3596c301f34eSRichard Henderson #else
3597c301f34eSRichard Henderson     nullify_over(ctx);
35988340f534SRichard Henderson     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
3599c301f34eSRichard Henderson 
3600c301f34eSRichard Henderson     copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3601c301f34eSRichard Henderson     if (ctx->iaoq_b == -1) {
3602c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3603c301f34eSRichard Henderson     }
3604c301f34eSRichard Henderson     copy_iaoq_entry(cpu_iaoq_b, -1, dest);
3605c301f34eSRichard Henderson     tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
36068340f534SRichard Henderson     if (a->l) {
36078340f534SRichard Henderson         copy_iaoq_entry(cpu_gr[a->l], ctx->iaoq_n, ctx->iaoq_n_var);
3608c301f34eSRichard Henderson     }
36098340f534SRichard Henderson     nullify_set(ctx, a->n);
3610c301f34eSRichard Henderson     tcg_gen_lookup_and_goto_ptr();
361131234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
361231234768SRichard Henderson     return nullify_end(ctx);
3613c301f34eSRichard Henderson #endif
361498cd9ca7SRichard Henderson }
361598cd9ca7SRichard Henderson 
36161ca74648SRichard Henderson /*
36171ca74648SRichard Henderson  * Float class 0
36181ca74648SRichard Henderson  */
3619ebe9383cSRichard Henderson 
36201ca74648SRichard Henderson static void gen_fcpy_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3621ebe9383cSRichard Henderson {
3622ebe9383cSRichard Henderson     tcg_gen_mov_i32(dst, src);
3623ebe9383cSRichard Henderson }
3624ebe9383cSRichard Henderson 
36251ca74648SRichard Henderson static bool trans_fcpy_f(DisasContext *ctx, arg_fclass01 *a)
36261ca74648SRichard Henderson {
36271ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fcpy_f);
36281ca74648SRichard Henderson }
36291ca74648SRichard Henderson 
3630ebe9383cSRichard Henderson static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3631ebe9383cSRichard Henderson {
3632ebe9383cSRichard Henderson     tcg_gen_mov_i64(dst, src);
3633ebe9383cSRichard Henderson }
3634ebe9383cSRichard Henderson 
36351ca74648SRichard Henderson static bool trans_fcpy_d(DisasContext *ctx, arg_fclass01 *a)
36361ca74648SRichard Henderson {
36371ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fcpy_d);
36381ca74648SRichard Henderson }
36391ca74648SRichard Henderson 
36401ca74648SRichard Henderson static void gen_fabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3641ebe9383cSRichard Henderson {
3642ebe9383cSRichard Henderson     tcg_gen_andi_i32(dst, src, INT32_MAX);
3643ebe9383cSRichard Henderson }
3644ebe9383cSRichard Henderson 
36451ca74648SRichard Henderson static bool trans_fabs_f(DisasContext *ctx, arg_fclass01 *a)
36461ca74648SRichard Henderson {
36471ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fabs_f);
36481ca74648SRichard Henderson }
36491ca74648SRichard Henderson 
3650ebe9383cSRichard Henderson static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3651ebe9383cSRichard Henderson {
3652ebe9383cSRichard Henderson     tcg_gen_andi_i64(dst, src, INT64_MAX);
3653ebe9383cSRichard Henderson }
3654ebe9383cSRichard Henderson 
36551ca74648SRichard Henderson static bool trans_fabs_d(DisasContext *ctx, arg_fclass01 *a)
36561ca74648SRichard Henderson {
36571ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fabs_d);
36581ca74648SRichard Henderson }
36591ca74648SRichard Henderson 
36601ca74648SRichard Henderson static bool trans_fsqrt_f(DisasContext *ctx, arg_fclass01 *a)
36611ca74648SRichard Henderson {
36621ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fsqrt_s);
36631ca74648SRichard Henderson }
36641ca74648SRichard Henderson 
36651ca74648SRichard Henderson static bool trans_fsqrt_d(DisasContext *ctx, arg_fclass01 *a)
36661ca74648SRichard Henderson {
36671ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fsqrt_d);
36681ca74648SRichard Henderson }
36691ca74648SRichard Henderson 
36701ca74648SRichard Henderson static bool trans_frnd_f(DisasContext *ctx, arg_fclass01 *a)
36711ca74648SRichard Henderson {
36721ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_frnd_s);
36731ca74648SRichard Henderson }
36741ca74648SRichard Henderson 
36751ca74648SRichard Henderson static bool trans_frnd_d(DisasContext *ctx, arg_fclass01 *a)
36761ca74648SRichard Henderson {
36771ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_frnd_d);
36781ca74648SRichard Henderson }
36791ca74648SRichard Henderson 
36801ca74648SRichard Henderson static void gen_fneg_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3681ebe9383cSRichard Henderson {
3682ebe9383cSRichard Henderson     tcg_gen_xori_i32(dst, src, INT32_MIN);
3683ebe9383cSRichard Henderson }
3684ebe9383cSRichard Henderson 
36851ca74648SRichard Henderson static bool trans_fneg_f(DisasContext *ctx, arg_fclass01 *a)
36861ca74648SRichard Henderson {
36871ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fneg_f);
36881ca74648SRichard Henderson }
36891ca74648SRichard Henderson 
3690ebe9383cSRichard Henderson static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3691ebe9383cSRichard Henderson {
3692ebe9383cSRichard Henderson     tcg_gen_xori_i64(dst, src, INT64_MIN);
3693ebe9383cSRichard Henderson }
3694ebe9383cSRichard Henderson 
36951ca74648SRichard Henderson static bool trans_fneg_d(DisasContext *ctx, arg_fclass01 *a)
36961ca74648SRichard Henderson {
36971ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fneg_d);
36981ca74648SRichard Henderson }
36991ca74648SRichard Henderson 
37001ca74648SRichard Henderson static void gen_fnegabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
3701ebe9383cSRichard Henderson {
3702ebe9383cSRichard Henderson     tcg_gen_ori_i32(dst, src, INT32_MIN);
3703ebe9383cSRichard Henderson }
3704ebe9383cSRichard Henderson 
37051ca74648SRichard Henderson static bool trans_fnegabs_f(DisasContext *ctx, arg_fclass01 *a)
37061ca74648SRichard Henderson {
37071ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fnegabs_f);
37081ca74648SRichard Henderson }
37091ca74648SRichard Henderson 
3710ebe9383cSRichard Henderson static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
3711ebe9383cSRichard Henderson {
3712ebe9383cSRichard Henderson     tcg_gen_ori_i64(dst, src, INT64_MIN);
3713ebe9383cSRichard Henderson }
3714ebe9383cSRichard Henderson 
37151ca74648SRichard Henderson static bool trans_fnegabs_d(DisasContext *ctx, arg_fclass01 *a)
37161ca74648SRichard Henderson {
37171ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fnegabs_d);
37181ca74648SRichard Henderson }
37191ca74648SRichard Henderson 
37201ca74648SRichard Henderson /*
37211ca74648SRichard Henderson  * Float class 1
37221ca74648SRichard Henderson  */
37231ca74648SRichard Henderson 
37241ca74648SRichard Henderson static bool trans_fcnv_d_f(DisasContext *ctx, arg_fclass01 *a)
37251ca74648SRichard Henderson {
37261ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_s);
37271ca74648SRichard Henderson }
37281ca74648SRichard Henderson 
37291ca74648SRichard Henderson static bool trans_fcnv_f_d(DisasContext *ctx, arg_fclass01 *a)
37301ca74648SRichard Henderson {
37311ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_d);
37321ca74648SRichard Henderson }
37331ca74648SRichard Henderson 
37341ca74648SRichard Henderson static bool trans_fcnv_w_f(DisasContext *ctx, arg_fclass01 *a)
37351ca74648SRichard Henderson {
37361ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_w_s);
37371ca74648SRichard Henderson }
37381ca74648SRichard Henderson 
37391ca74648SRichard Henderson static bool trans_fcnv_q_f(DisasContext *ctx, arg_fclass01 *a)
37401ca74648SRichard Henderson {
37411ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_dw_s);
37421ca74648SRichard Henderson }
37431ca74648SRichard Henderson 
37441ca74648SRichard Henderson static bool trans_fcnv_w_d(DisasContext *ctx, arg_fclass01 *a)
37451ca74648SRichard Henderson {
37461ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_w_d);
37471ca74648SRichard Henderson }
37481ca74648SRichard Henderson 
37491ca74648SRichard Henderson static bool trans_fcnv_q_d(DisasContext *ctx, arg_fclass01 *a)
37501ca74648SRichard Henderson {
37511ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_dw_d);
37521ca74648SRichard Henderson }
37531ca74648SRichard Henderson 
37541ca74648SRichard Henderson static bool trans_fcnv_f_w(DisasContext *ctx, arg_fclass01 *a)
37551ca74648SRichard Henderson {
37561ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_w);
37571ca74648SRichard Henderson }
37581ca74648SRichard Henderson 
37591ca74648SRichard Henderson static bool trans_fcnv_d_w(DisasContext *ctx, arg_fclass01 *a)
37601ca74648SRichard Henderson {
37611ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_w);
37621ca74648SRichard Henderson }
37631ca74648SRichard Henderson 
37641ca74648SRichard Henderson static bool trans_fcnv_f_q(DisasContext *ctx, arg_fclass01 *a)
37651ca74648SRichard Henderson {
37661ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_dw);
37671ca74648SRichard Henderson }
37681ca74648SRichard Henderson 
37691ca74648SRichard Henderson static bool trans_fcnv_d_q(DisasContext *ctx, arg_fclass01 *a)
37701ca74648SRichard Henderson {
37711ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_dw);
37721ca74648SRichard Henderson }
37731ca74648SRichard Henderson 
37741ca74648SRichard Henderson static bool trans_fcnv_t_f_w(DisasContext *ctx, arg_fclass01 *a)
37751ca74648SRichard Henderson {
37761ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_w);
37771ca74648SRichard Henderson }
37781ca74648SRichard Henderson 
37791ca74648SRichard Henderson static bool trans_fcnv_t_d_w(DisasContext *ctx, arg_fclass01 *a)
37801ca74648SRichard Henderson {
37811ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_w);
37821ca74648SRichard Henderson }
37831ca74648SRichard Henderson 
37841ca74648SRichard Henderson static bool trans_fcnv_t_f_q(DisasContext *ctx, arg_fclass01 *a)
37851ca74648SRichard Henderson {
37861ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_dw);
37871ca74648SRichard Henderson }
37881ca74648SRichard Henderson 
37891ca74648SRichard Henderson static bool trans_fcnv_t_d_q(DisasContext *ctx, arg_fclass01 *a)
37901ca74648SRichard Henderson {
37911ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_dw);
37921ca74648SRichard Henderson }
37931ca74648SRichard Henderson 
37941ca74648SRichard Henderson static bool trans_fcnv_uw_f(DisasContext *ctx, arg_fclass01 *a)
37951ca74648SRichard Henderson {
37961ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_uw_s);
37971ca74648SRichard Henderson }
37981ca74648SRichard Henderson 
37991ca74648SRichard Henderson static bool trans_fcnv_uq_f(DisasContext *ctx, arg_fclass01 *a)
38001ca74648SRichard Henderson {
38011ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_udw_s);
38021ca74648SRichard Henderson }
38031ca74648SRichard Henderson 
38041ca74648SRichard Henderson static bool trans_fcnv_uw_d(DisasContext *ctx, arg_fclass01 *a)
38051ca74648SRichard Henderson {
38061ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_uw_d);
38071ca74648SRichard Henderson }
38081ca74648SRichard Henderson 
38091ca74648SRichard Henderson static bool trans_fcnv_uq_d(DisasContext *ctx, arg_fclass01 *a)
38101ca74648SRichard Henderson {
38111ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_udw_d);
38121ca74648SRichard Henderson }
38131ca74648SRichard Henderson 
38141ca74648SRichard Henderson static bool trans_fcnv_f_uw(DisasContext *ctx, arg_fclass01 *a)
38151ca74648SRichard Henderson {
38161ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_uw);
38171ca74648SRichard Henderson }
38181ca74648SRichard Henderson 
38191ca74648SRichard Henderson static bool trans_fcnv_d_uw(DisasContext *ctx, arg_fclass01 *a)
38201ca74648SRichard Henderson {
38211ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_uw);
38221ca74648SRichard Henderson }
38231ca74648SRichard Henderson 
38241ca74648SRichard Henderson static bool trans_fcnv_f_uq(DisasContext *ctx, arg_fclass01 *a)
38251ca74648SRichard Henderson {
38261ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_udw);
38271ca74648SRichard Henderson }
38281ca74648SRichard Henderson 
38291ca74648SRichard Henderson static bool trans_fcnv_d_uq(DisasContext *ctx, arg_fclass01 *a)
38301ca74648SRichard Henderson {
38311ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_udw);
38321ca74648SRichard Henderson }
38331ca74648SRichard Henderson 
38341ca74648SRichard Henderson static bool trans_fcnv_t_f_uw(DisasContext *ctx, arg_fclass01 *a)
38351ca74648SRichard Henderson {
38361ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_uw);
38371ca74648SRichard Henderson }
38381ca74648SRichard Henderson 
38391ca74648SRichard Henderson static bool trans_fcnv_t_d_uw(DisasContext *ctx, arg_fclass01 *a)
38401ca74648SRichard Henderson {
38411ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_uw);
38421ca74648SRichard Henderson }
38431ca74648SRichard Henderson 
38441ca74648SRichard Henderson static bool trans_fcnv_t_f_uq(DisasContext *ctx, arg_fclass01 *a)
38451ca74648SRichard Henderson {
38461ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_udw);
38471ca74648SRichard Henderson }
38481ca74648SRichard Henderson 
38491ca74648SRichard Henderson static bool trans_fcnv_t_d_uq(DisasContext *ctx, arg_fclass01 *a)
38501ca74648SRichard Henderson {
38511ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_udw);
38521ca74648SRichard Henderson }
38531ca74648SRichard Henderson 
38541ca74648SRichard Henderson /*
38551ca74648SRichard Henderson  * Float class 2
38561ca74648SRichard Henderson  */
38571ca74648SRichard Henderson 
38581ca74648SRichard Henderson static bool trans_fcmp_f(DisasContext *ctx, arg_fclass2 *a)
3859ebe9383cSRichard Henderson {
3860ebe9383cSRichard Henderson     TCGv_i32 ta, tb, tc, ty;
3861ebe9383cSRichard Henderson 
3862ebe9383cSRichard Henderson     nullify_over(ctx);
3863ebe9383cSRichard Henderson 
38641ca74648SRichard Henderson     ta = load_frw0_i32(a->r1);
38651ca74648SRichard Henderson     tb = load_frw0_i32(a->r2);
386629dd6f64SRichard Henderson     ty = tcg_constant_i32(a->y);
386729dd6f64SRichard Henderson     tc = tcg_constant_i32(a->c);
3868ebe9383cSRichard Henderson 
3869ebe9383cSRichard Henderson     gen_helper_fcmp_s(cpu_env, ta, tb, ty, tc);
3870ebe9383cSRichard Henderson 
3871ebe9383cSRichard Henderson     tcg_temp_free_i32(ta);
3872ebe9383cSRichard Henderson     tcg_temp_free_i32(tb);
3873ebe9383cSRichard Henderson 
38741ca74648SRichard Henderson     return nullify_end(ctx);
3875ebe9383cSRichard Henderson }
3876ebe9383cSRichard Henderson 
38771ca74648SRichard Henderson static bool trans_fcmp_d(DisasContext *ctx, arg_fclass2 *a)
3878ebe9383cSRichard Henderson {
3879ebe9383cSRichard Henderson     TCGv_i64 ta, tb;
3880ebe9383cSRichard Henderson     TCGv_i32 tc, ty;
3881ebe9383cSRichard Henderson 
3882ebe9383cSRichard Henderson     nullify_over(ctx);
3883ebe9383cSRichard Henderson 
38841ca74648SRichard Henderson     ta = load_frd0(a->r1);
38851ca74648SRichard Henderson     tb = load_frd0(a->r2);
388629dd6f64SRichard Henderson     ty = tcg_constant_i32(a->y);
388729dd6f64SRichard Henderson     tc = tcg_constant_i32(a->c);
3888ebe9383cSRichard Henderson 
3889ebe9383cSRichard Henderson     gen_helper_fcmp_d(cpu_env, ta, tb, ty, tc);
3890ebe9383cSRichard Henderson 
3891ebe9383cSRichard Henderson     tcg_temp_free_i64(ta);
3892ebe9383cSRichard Henderson     tcg_temp_free_i64(tb);
3893ebe9383cSRichard Henderson 
389431234768SRichard Henderson     return nullify_end(ctx);
3895ebe9383cSRichard Henderson }
3896ebe9383cSRichard Henderson 
38971ca74648SRichard Henderson static bool trans_ftest(DisasContext *ctx, arg_ftest *a)
3898ebe9383cSRichard Henderson {
3899eaa3783bSRichard Henderson     TCGv_reg t;
3900ebe9383cSRichard Henderson 
3901ebe9383cSRichard Henderson     nullify_over(ctx);
3902ebe9383cSRichard Henderson 
39031ca74648SRichard Henderson     t = get_temp(ctx);
3904eaa3783bSRichard Henderson     tcg_gen_ld32u_reg(t, cpu_env, offsetof(CPUHPPAState, fr0_shadow));
3905ebe9383cSRichard Henderson 
39061ca74648SRichard Henderson     if (a->y == 1) {
3907ebe9383cSRichard Henderson         int mask;
3908ebe9383cSRichard Henderson         bool inv = false;
3909ebe9383cSRichard Henderson 
39101ca74648SRichard Henderson         switch (a->c) {
3911ebe9383cSRichard Henderson         case 0: /* simple */
3912eaa3783bSRichard Henderson             tcg_gen_andi_reg(t, t, 0x4000000);
3913ebe9383cSRichard Henderson             ctx->null_cond = cond_make_0(TCG_COND_NE, t);
3914ebe9383cSRichard Henderson             goto done;
3915ebe9383cSRichard Henderson         case 2: /* rej */
3916ebe9383cSRichard Henderson             inv = true;
3917ebe9383cSRichard Henderson             /* fallthru */
3918ebe9383cSRichard Henderson         case 1: /* acc */
3919ebe9383cSRichard Henderson             mask = 0x43ff800;
3920ebe9383cSRichard Henderson             break;
3921ebe9383cSRichard Henderson         case 6: /* rej8 */
3922ebe9383cSRichard Henderson             inv = true;
3923ebe9383cSRichard Henderson             /* fallthru */
3924ebe9383cSRichard Henderson         case 5: /* acc8 */
3925ebe9383cSRichard Henderson             mask = 0x43f8000;
3926ebe9383cSRichard Henderson             break;
3927ebe9383cSRichard Henderson         case 9: /* acc6 */
3928ebe9383cSRichard Henderson             mask = 0x43e0000;
3929ebe9383cSRichard Henderson             break;
3930ebe9383cSRichard Henderson         case 13: /* acc4 */
3931ebe9383cSRichard Henderson             mask = 0x4380000;
3932ebe9383cSRichard Henderson             break;
3933ebe9383cSRichard Henderson         case 17: /* acc2 */
3934ebe9383cSRichard Henderson             mask = 0x4200000;
3935ebe9383cSRichard Henderson             break;
3936ebe9383cSRichard Henderson         default:
39371ca74648SRichard Henderson             gen_illegal(ctx);
39381ca74648SRichard Henderson             return true;
3939ebe9383cSRichard Henderson         }
3940ebe9383cSRichard Henderson         if (inv) {
3941eaa3783bSRichard Henderson             TCGv_reg c = load_const(ctx, mask);
3942eaa3783bSRichard Henderson             tcg_gen_or_reg(t, t, c);
3943ebe9383cSRichard Henderson             ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
3944ebe9383cSRichard Henderson         } else {
3945eaa3783bSRichard Henderson             tcg_gen_andi_reg(t, t, mask);
3946ebe9383cSRichard Henderson             ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
3947ebe9383cSRichard Henderson         }
39481ca74648SRichard Henderson     } else {
39491ca74648SRichard Henderson         unsigned cbit = (a->y ^ 1) - 1;
39501ca74648SRichard Henderson 
39511ca74648SRichard Henderson         tcg_gen_extract_reg(t, t, 21 - cbit, 1);
39521ca74648SRichard Henderson         ctx->null_cond = cond_make_0(TCG_COND_NE, t);
39531ca74648SRichard Henderson         tcg_temp_free(t);
39541ca74648SRichard Henderson     }
39551ca74648SRichard Henderson 
3956ebe9383cSRichard Henderson  done:
395731234768SRichard Henderson     return nullify_end(ctx);
3958ebe9383cSRichard Henderson }
3959ebe9383cSRichard Henderson 
39601ca74648SRichard Henderson /*
39611ca74648SRichard Henderson  * Float class 2
39621ca74648SRichard Henderson  */
39631ca74648SRichard Henderson 
39641ca74648SRichard Henderson static bool trans_fadd_f(DisasContext *ctx, arg_fclass3 *a)
3965ebe9383cSRichard Henderson {
39661ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fadd_s);
39671ca74648SRichard Henderson }
39681ca74648SRichard Henderson 
39691ca74648SRichard Henderson static bool trans_fadd_d(DisasContext *ctx, arg_fclass3 *a)
39701ca74648SRichard Henderson {
39711ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fadd_d);
39721ca74648SRichard Henderson }
39731ca74648SRichard Henderson 
39741ca74648SRichard Henderson static bool trans_fsub_f(DisasContext *ctx, arg_fclass3 *a)
39751ca74648SRichard Henderson {
39761ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fsub_s);
39771ca74648SRichard Henderson }
39781ca74648SRichard Henderson 
39791ca74648SRichard Henderson static bool trans_fsub_d(DisasContext *ctx, arg_fclass3 *a)
39801ca74648SRichard Henderson {
39811ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fsub_d);
39821ca74648SRichard Henderson }
39831ca74648SRichard Henderson 
39841ca74648SRichard Henderson static bool trans_fmpy_f(DisasContext *ctx, arg_fclass3 *a)
39851ca74648SRichard Henderson {
39861ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_s);
39871ca74648SRichard Henderson }
39881ca74648SRichard Henderson 
39891ca74648SRichard Henderson static bool trans_fmpy_d(DisasContext *ctx, arg_fclass3 *a)
39901ca74648SRichard Henderson {
39911ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_d);
39921ca74648SRichard Henderson }
39931ca74648SRichard Henderson 
39941ca74648SRichard Henderson static bool trans_fdiv_f(DisasContext *ctx, arg_fclass3 *a)
39951ca74648SRichard Henderson {
39961ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_s);
39971ca74648SRichard Henderson }
39981ca74648SRichard Henderson 
39991ca74648SRichard Henderson static bool trans_fdiv_d(DisasContext *ctx, arg_fclass3 *a)
40001ca74648SRichard Henderson {
40011ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_d);
40021ca74648SRichard Henderson }
40031ca74648SRichard Henderson 
40041ca74648SRichard Henderson static bool trans_xmpyu(DisasContext *ctx, arg_xmpyu *a)
40051ca74648SRichard Henderson {
40061ca74648SRichard Henderson     TCGv_i64 x, y;
4007ebe9383cSRichard Henderson 
4008ebe9383cSRichard Henderson     nullify_over(ctx);
4009ebe9383cSRichard Henderson 
40101ca74648SRichard Henderson     x = load_frw0_i64(a->r1);
40111ca74648SRichard Henderson     y = load_frw0_i64(a->r2);
40121ca74648SRichard Henderson     tcg_gen_mul_i64(x, x, y);
40131ca74648SRichard Henderson     save_frd(a->t, x);
40141ca74648SRichard Henderson     tcg_temp_free_i64(x);
40151ca74648SRichard Henderson     tcg_temp_free_i64(y);
4016ebe9383cSRichard Henderson 
401731234768SRichard Henderson     return nullify_end(ctx);
4018ebe9383cSRichard Henderson }
4019ebe9383cSRichard Henderson 
4020ebe9383cSRichard Henderson /* Convert the fmpyadd single-precision register encodings to standard.  */
4021ebe9383cSRichard Henderson static inline int fmpyadd_s_reg(unsigned r)
4022ebe9383cSRichard Henderson {
4023ebe9383cSRichard Henderson     return (r & 16) * 2 + 16 + (r & 15);
4024ebe9383cSRichard Henderson }
4025ebe9383cSRichard Henderson 
4026b1e2af57SRichard Henderson static bool do_fmpyadd_s(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4027ebe9383cSRichard Henderson {
4028b1e2af57SRichard Henderson     int tm = fmpyadd_s_reg(a->tm);
4029b1e2af57SRichard Henderson     int ra = fmpyadd_s_reg(a->ra);
4030b1e2af57SRichard Henderson     int ta = fmpyadd_s_reg(a->ta);
4031b1e2af57SRichard Henderson     int rm2 = fmpyadd_s_reg(a->rm2);
4032b1e2af57SRichard Henderson     int rm1 = fmpyadd_s_reg(a->rm1);
4033ebe9383cSRichard Henderson 
4034ebe9383cSRichard Henderson     nullify_over(ctx);
4035ebe9383cSRichard Henderson 
4036ebe9383cSRichard Henderson     do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
4037ebe9383cSRichard Henderson     do_fop_weww(ctx, ta, ta, ra,
4038ebe9383cSRichard Henderson                 is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
4039ebe9383cSRichard Henderson 
404031234768SRichard Henderson     return nullify_end(ctx);
4041ebe9383cSRichard Henderson }
4042ebe9383cSRichard Henderson 
4043b1e2af57SRichard Henderson static bool trans_fmpyadd_f(DisasContext *ctx, arg_mpyadd *a)
4044b1e2af57SRichard Henderson {
4045b1e2af57SRichard Henderson     return do_fmpyadd_s(ctx, a, false);
4046b1e2af57SRichard Henderson }
4047b1e2af57SRichard Henderson 
4048b1e2af57SRichard Henderson static bool trans_fmpysub_f(DisasContext *ctx, arg_mpyadd *a)
4049b1e2af57SRichard Henderson {
4050b1e2af57SRichard Henderson     return do_fmpyadd_s(ctx, a, true);
4051b1e2af57SRichard Henderson }
4052b1e2af57SRichard Henderson 
4053b1e2af57SRichard Henderson static bool do_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4054b1e2af57SRichard Henderson {
4055b1e2af57SRichard Henderson     nullify_over(ctx);
4056b1e2af57SRichard Henderson 
4057b1e2af57SRichard Henderson     do_fop_dedd(ctx, a->tm, a->rm1, a->rm2, gen_helper_fmpy_d);
4058b1e2af57SRichard Henderson     do_fop_dedd(ctx, a->ta, a->ta, a->ra,
4059b1e2af57SRichard Henderson                 is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
4060b1e2af57SRichard Henderson 
4061b1e2af57SRichard Henderson     return nullify_end(ctx);
4062b1e2af57SRichard Henderson }
4063b1e2af57SRichard Henderson 
4064b1e2af57SRichard Henderson static bool trans_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a)
4065b1e2af57SRichard Henderson {
4066b1e2af57SRichard Henderson     return do_fmpyadd_d(ctx, a, false);
4067b1e2af57SRichard Henderson }
4068b1e2af57SRichard Henderson 
4069b1e2af57SRichard Henderson static bool trans_fmpysub_d(DisasContext *ctx, arg_mpyadd *a)
4070b1e2af57SRichard Henderson {
4071b1e2af57SRichard Henderson     return do_fmpyadd_d(ctx, a, true);
4072b1e2af57SRichard Henderson }
4073b1e2af57SRichard Henderson 
4074c3bad4f8SRichard Henderson static bool trans_fmpyfadd_f(DisasContext *ctx, arg_fmpyfadd_f *a)
4075ebe9383cSRichard Henderson {
4076c3bad4f8SRichard Henderson     TCGv_i32 x, y, z;
4077ebe9383cSRichard Henderson 
4078ebe9383cSRichard Henderson     nullify_over(ctx);
4079c3bad4f8SRichard Henderson     x = load_frw0_i32(a->rm1);
4080c3bad4f8SRichard Henderson     y = load_frw0_i32(a->rm2);
4081c3bad4f8SRichard Henderson     z = load_frw0_i32(a->ra3);
4082ebe9383cSRichard Henderson 
4083c3bad4f8SRichard Henderson     if (a->neg) {
4084c3bad4f8SRichard Henderson         gen_helper_fmpynfadd_s(x, cpu_env, x, y, z);
4085ebe9383cSRichard Henderson     } else {
4086c3bad4f8SRichard Henderson         gen_helper_fmpyfadd_s(x, cpu_env, x, y, z);
4087ebe9383cSRichard Henderson     }
4088ebe9383cSRichard Henderson 
4089c3bad4f8SRichard Henderson     tcg_temp_free_i32(y);
4090c3bad4f8SRichard Henderson     tcg_temp_free_i32(z);
4091c3bad4f8SRichard Henderson     save_frw_i32(a->t, x);
4092c3bad4f8SRichard Henderson     tcg_temp_free_i32(x);
409331234768SRichard Henderson     return nullify_end(ctx);
4094ebe9383cSRichard Henderson }
4095ebe9383cSRichard Henderson 
4096c3bad4f8SRichard Henderson static bool trans_fmpyfadd_d(DisasContext *ctx, arg_fmpyfadd_d *a)
4097ebe9383cSRichard Henderson {
4098c3bad4f8SRichard Henderson     TCGv_i64 x, y, z;
4099ebe9383cSRichard Henderson 
4100ebe9383cSRichard Henderson     nullify_over(ctx);
4101c3bad4f8SRichard Henderson     x = load_frd0(a->rm1);
4102c3bad4f8SRichard Henderson     y = load_frd0(a->rm2);
4103c3bad4f8SRichard Henderson     z = load_frd0(a->ra3);
4104ebe9383cSRichard Henderson 
4105c3bad4f8SRichard Henderson     if (a->neg) {
4106c3bad4f8SRichard Henderson         gen_helper_fmpynfadd_d(x, cpu_env, x, y, z);
4107ebe9383cSRichard Henderson     } else {
4108c3bad4f8SRichard Henderson         gen_helper_fmpyfadd_d(x, cpu_env, x, y, z);
4109ebe9383cSRichard Henderson     }
4110ebe9383cSRichard Henderson 
4111c3bad4f8SRichard Henderson     tcg_temp_free_i64(y);
4112c3bad4f8SRichard Henderson     tcg_temp_free_i64(z);
4113c3bad4f8SRichard Henderson     save_frd(a->t, x);
4114c3bad4f8SRichard Henderson     tcg_temp_free_i64(x);
411531234768SRichard Henderson     return nullify_end(ctx);
4116ebe9383cSRichard Henderson }
4117ebe9383cSRichard Henderson 
411815da177bSSven Schnelle static bool trans_diag(DisasContext *ctx, arg_diag *a)
411915da177bSSven Schnelle {
412015da177bSSven Schnelle     qemu_log_mask(LOG_UNIMP, "DIAG opcode ignored\n");
412115da177bSSven Schnelle     cond_free(&ctx->null_cond);
412215da177bSSven Schnelle     return true;
412315da177bSSven Schnelle }
412415da177bSSven Schnelle 
4125b542683dSEmilio G. Cota static void hppa_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
412661766fe9SRichard Henderson {
412751b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4128f764718dSRichard Henderson     int bound;
412961766fe9SRichard Henderson 
413051b061fbSRichard Henderson     ctx->cs = cs;
4131494737b7SRichard Henderson     ctx->tb_flags = ctx->base.tb->flags;
41323d68ee7bSRichard Henderson 
41333d68ee7bSRichard Henderson #ifdef CONFIG_USER_ONLY
41343d68ee7bSRichard Henderson     ctx->privilege = MMU_USER_IDX;
41353d68ee7bSRichard Henderson     ctx->mmu_idx = MMU_USER_IDX;
4136ebd0e151SRichard Henderson     ctx->iaoq_f = ctx->base.pc_first | MMU_USER_IDX;
4137ebd0e151SRichard Henderson     ctx->iaoq_b = ctx->base.tb->cs_base | MMU_USER_IDX;
4138217d1a5eSRichard Henderson     ctx->unalign = (ctx->tb_flags & TB_FLAG_UNALIGN ? MO_UNALN : MO_ALIGN);
4139c301f34eSRichard Henderson #else
4140494737b7SRichard Henderson     ctx->privilege = (ctx->tb_flags >> TB_FLAG_PRIV_SHIFT) & 3;
4141494737b7SRichard Henderson     ctx->mmu_idx = (ctx->tb_flags & PSW_D ? ctx->privilege : MMU_PHYS_IDX);
41423d68ee7bSRichard Henderson 
4143c301f34eSRichard Henderson     /* Recover the IAOQ values from the GVA + PRIV.  */
4144c301f34eSRichard Henderson     uint64_t cs_base = ctx->base.tb->cs_base;
4145c301f34eSRichard Henderson     uint64_t iasq_f = cs_base & ~0xffffffffull;
4146c301f34eSRichard Henderson     int32_t diff = cs_base;
4147c301f34eSRichard Henderson 
4148c301f34eSRichard Henderson     ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
4149c301f34eSRichard Henderson     ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
4150c301f34eSRichard Henderson #endif
415151b061fbSRichard Henderson     ctx->iaoq_n = -1;
4152f764718dSRichard Henderson     ctx->iaoq_n_var = NULL;
415361766fe9SRichard Henderson 
41543d68ee7bSRichard Henderson     /* Bound the number of instructions by those left on the page.  */
41553d68ee7bSRichard Henderson     bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
4156b542683dSEmilio G. Cota     ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
41573d68ee7bSRichard Henderson 
415886f8d05fSRichard Henderson     ctx->ntempr = 0;
415986f8d05fSRichard Henderson     ctx->ntempl = 0;
416086f8d05fSRichard Henderson     memset(ctx->tempr, 0, sizeof(ctx->tempr));
416186f8d05fSRichard Henderson     memset(ctx->templ, 0, sizeof(ctx->templ));
416261766fe9SRichard Henderson }
416361766fe9SRichard Henderson 
416451b061fbSRichard Henderson static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
416551b061fbSRichard Henderson {
416651b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
416761766fe9SRichard Henderson 
41683d68ee7bSRichard Henderson     /* Seed the nullification status from PSW[N], as saved in TB->FLAGS.  */
416951b061fbSRichard Henderson     ctx->null_cond = cond_make_f();
417051b061fbSRichard Henderson     ctx->psw_n_nonzero = false;
4171494737b7SRichard Henderson     if (ctx->tb_flags & PSW_N) {
417251b061fbSRichard Henderson         ctx->null_cond.c = TCG_COND_ALWAYS;
417351b061fbSRichard Henderson         ctx->psw_n_nonzero = true;
4174129e9cc3SRichard Henderson     }
417551b061fbSRichard Henderson     ctx->null_lab = NULL;
417661766fe9SRichard Henderson }
417761766fe9SRichard Henderson 
417851b061fbSRichard Henderson static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
417951b061fbSRichard Henderson {
418051b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
418151b061fbSRichard Henderson 
418251b061fbSRichard Henderson     tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b);
418351b061fbSRichard Henderson }
418451b061fbSRichard Henderson 
418551b061fbSRichard Henderson static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
418651b061fbSRichard Henderson {
418751b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
418851b061fbSRichard Henderson     CPUHPPAState *env = cs->env_ptr;
418951b061fbSRichard Henderson     DisasJumpType ret;
419051b061fbSRichard Henderson     int i, n;
419151b061fbSRichard Henderson 
419251b061fbSRichard Henderson     /* Execute one insn.  */
4193ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
4194c301f34eSRichard Henderson     if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
419531234768SRichard Henderson         do_page_zero(ctx);
419631234768SRichard Henderson         ret = ctx->base.is_jmp;
4197869051eaSRichard Henderson         assert(ret != DISAS_NEXT);
4198ba1d0b44SRichard Henderson     } else
4199ba1d0b44SRichard Henderson #endif
4200ba1d0b44SRichard Henderson     {
420161766fe9SRichard Henderson         /* Always fetch the insn, even if nullified, so that we check
420261766fe9SRichard Henderson            the page permissions for execute.  */
42034e116893SIlya Leoshkevich         uint32_t insn = translator_ldl(env, &ctx->base, ctx->base.pc_next);
420461766fe9SRichard Henderson 
420561766fe9SRichard Henderson         /* Set up the IA queue for the next insn.
420661766fe9SRichard Henderson            This will be overwritten by a branch.  */
420751b061fbSRichard Henderson         if (ctx->iaoq_b == -1) {
420851b061fbSRichard Henderson             ctx->iaoq_n = -1;
420951b061fbSRichard Henderson             ctx->iaoq_n_var = get_temp(ctx);
4210eaa3783bSRichard Henderson             tcg_gen_addi_reg(ctx->iaoq_n_var, cpu_iaoq_b, 4);
421161766fe9SRichard Henderson         } else {
421251b061fbSRichard Henderson             ctx->iaoq_n = ctx->iaoq_b + 4;
4213f764718dSRichard Henderson             ctx->iaoq_n_var = NULL;
421461766fe9SRichard Henderson         }
421561766fe9SRichard Henderson 
421651b061fbSRichard Henderson         if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
421751b061fbSRichard Henderson             ctx->null_cond.c = TCG_COND_NEVER;
4218869051eaSRichard Henderson             ret = DISAS_NEXT;
4219129e9cc3SRichard Henderson         } else {
42201a19da0dSRichard Henderson             ctx->insn = insn;
422131274b46SRichard Henderson             if (!decode(ctx, insn)) {
422231274b46SRichard Henderson                 gen_illegal(ctx);
422331274b46SRichard Henderson             }
422431234768SRichard Henderson             ret = ctx->base.is_jmp;
422551b061fbSRichard Henderson             assert(ctx->null_lab == NULL);
4226129e9cc3SRichard Henderson         }
422761766fe9SRichard Henderson     }
422861766fe9SRichard Henderson 
422951b061fbSRichard Henderson     /* Free any temporaries allocated.  */
423086f8d05fSRichard Henderson     for (i = 0, n = ctx->ntempr; i < n; ++i) {
423186f8d05fSRichard Henderson         tcg_temp_free(ctx->tempr[i]);
423286f8d05fSRichard Henderson         ctx->tempr[i] = NULL;
423361766fe9SRichard Henderson     }
423486f8d05fSRichard Henderson     for (i = 0, n = ctx->ntempl; i < n; ++i) {
423586f8d05fSRichard Henderson         tcg_temp_free_tl(ctx->templ[i]);
423686f8d05fSRichard Henderson         ctx->templ[i] = NULL;
423786f8d05fSRichard Henderson     }
423886f8d05fSRichard Henderson     ctx->ntempr = 0;
423986f8d05fSRichard Henderson     ctx->ntempl = 0;
424061766fe9SRichard Henderson 
42413d68ee7bSRichard Henderson     /* Advance the insn queue.  Note that this check also detects
42423d68ee7bSRichard Henderson        a priority change within the instruction queue.  */
424351b061fbSRichard Henderson     if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
4244c301f34eSRichard Henderson         if (ctx->iaoq_b != -1 && ctx->iaoq_n != -1
4245c301f34eSRichard Henderson             && use_goto_tb(ctx, ctx->iaoq_b)
4246c301f34eSRichard Henderson             && (ctx->null_cond.c == TCG_COND_NEVER
4247c301f34eSRichard Henderson                 || ctx->null_cond.c == TCG_COND_ALWAYS)) {
424851b061fbSRichard Henderson             nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
424951b061fbSRichard Henderson             gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
425031234768SRichard Henderson             ctx->base.is_jmp = ret = DISAS_NORETURN;
4251129e9cc3SRichard Henderson         } else {
425231234768SRichard Henderson             ctx->base.is_jmp = ret = DISAS_IAQ_N_STALE;
425361766fe9SRichard Henderson         }
4254129e9cc3SRichard Henderson     }
425551b061fbSRichard Henderson     ctx->iaoq_f = ctx->iaoq_b;
425651b061fbSRichard Henderson     ctx->iaoq_b = ctx->iaoq_n;
4257c301f34eSRichard Henderson     ctx->base.pc_next += 4;
425861766fe9SRichard Henderson 
4259c5d0aec2SRichard Henderson     switch (ret) {
4260c5d0aec2SRichard Henderson     case DISAS_NORETURN:
4261c5d0aec2SRichard Henderson     case DISAS_IAQ_N_UPDATED:
4262c5d0aec2SRichard Henderson         break;
4263c5d0aec2SRichard Henderson 
4264c5d0aec2SRichard Henderson     case DISAS_NEXT:
4265c5d0aec2SRichard Henderson     case DISAS_IAQ_N_STALE:
4266c5d0aec2SRichard Henderson     case DISAS_IAQ_N_STALE_EXIT:
426751b061fbSRichard Henderson         if (ctx->iaoq_f == -1) {
4268eaa3783bSRichard Henderson             tcg_gen_mov_reg(cpu_iaoq_f, cpu_iaoq_b);
426951b061fbSRichard Henderson             copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
4270c301f34eSRichard Henderson #ifndef CONFIG_USER_ONLY
4271c301f34eSRichard Henderson             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4272c301f34eSRichard Henderson #endif
427351b061fbSRichard Henderson             nullify_save(ctx);
4274c5d0aec2SRichard Henderson             ctx->base.is_jmp = (ret == DISAS_IAQ_N_STALE_EXIT
4275c5d0aec2SRichard Henderson                                 ? DISAS_EXIT
4276c5d0aec2SRichard Henderson                                 : DISAS_IAQ_N_UPDATED);
427751b061fbSRichard Henderson         } else if (ctx->iaoq_b == -1) {
4278eaa3783bSRichard Henderson             tcg_gen_mov_reg(cpu_iaoq_b, ctx->iaoq_n_var);
427961766fe9SRichard Henderson         }
4280c5d0aec2SRichard Henderson         break;
4281c5d0aec2SRichard Henderson 
4282c5d0aec2SRichard Henderson     default:
4283c5d0aec2SRichard Henderson         g_assert_not_reached();
4284c5d0aec2SRichard Henderson     }
428561766fe9SRichard Henderson }
428661766fe9SRichard Henderson 
428751b061fbSRichard Henderson static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
428851b061fbSRichard Henderson {
428951b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4290e1b5a5edSRichard Henderson     DisasJumpType is_jmp = ctx->base.is_jmp;
429151b061fbSRichard Henderson 
4292e1b5a5edSRichard Henderson     switch (is_jmp) {
4293869051eaSRichard Henderson     case DISAS_NORETURN:
429461766fe9SRichard Henderson         break;
429551b061fbSRichard Henderson     case DISAS_TOO_MANY:
4296869051eaSRichard Henderson     case DISAS_IAQ_N_STALE:
4297e1b5a5edSRichard Henderson     case DISAS_IAQ_N_STALE_EXIT:
429851b061fbSRichard Henderson         copy_iaoq_entry(cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
429951b061fbSRichard Henderson         copy_iaoq_entry(cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
430051b061fbSRichard Henderson         nullify_save(ctx);
430161766fe9SRichard Henderson         /* FALLTHRU */
4302869051eaSRichard Henderson     case DISAS_IAQ_N_UPDATED:
43038532a14eSRichard Henderson         if (is_jmp != DISAS_IAQ_N_STALE_EXIT) {
43047f11636dSEmilio G. Cota             tcg_gen_lookup_and_goto_ptr();
43058532a14eSRichard Henderson             break;
430661766fe9SRichard Henderson         }
4307c5d0aec2SRichard Henderson         /* FALLTHRU */
4308c5d0aec2SRichard Henderson     case DISAS_EXIT:
4309c5d0aec2SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
431061766fe9SRichard Henderson         break;
431161766fe9SRichard Henderson     default:
431251b061fbSRichard Henderson         g_assert_not_reached();
431361766fe9SRichard Henderson     }
431451b061fbSRichard Henderson }
431561766fe9SRichard Henderson 
43168eb806a7SRichard Henderson static void hppa_tr_disas_log(const DisasContextBase *dcbase,
43178eb806a7SRichard Henderson                               CPUState *cs, FILE *logfile)
431851b061fbSRichard Henderson {
4319c301f34eSRichard Henderson     target_ulong pc = dcbase->pc_first;
432061766fe9SRichard Henderson 
4321ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
4322ba1d0b44SRichard Henderson     switch (pc) {
43237ad439dfSRichard Henderson     case 0x00:
43248eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x00000000:  (null)\n");
4325ba1d0b44SRichard Henderson         return;
43267ad439dfSRichard Henderson     case 0xb0:
43278eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x000000b0:  light-weight-syscall\n");
4328ba1d0b44SRichard Henderson         return;
43297ad439dfSRichard Henderson     case 0xe0:
43308eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x000000e0:  set-thread-pointer-syscall\n");
4331ba1d0b44SRichard Henderson         return;
43327ad439dfSRichard Henderson     case 0x100:
43338eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x00000100:  syscall\n");
4334ba1d0b44SRichard Henderson         return;
43357ad439dfSRichard Henderson     }
4336ba1d0b44SRichard Henderson #endif
4337ba1d0b44SRichard Henderson 
43388eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(pc));
43398eb806a7SRichard Henderson     target_disas(logfile, cs, pc, dcbase->tb->size);
434061766fe9SRichard Henderson }
434151b061fbSRichard Henderson 
434251b061fbSRichard Henderson static const TranslatorOps hppa_tr_ops = {
434351b061fbSRichard Henderson     .init_disas_context = hppa_tr_init_disas_context,
434451b061fbSRichard Henderson     .tb_start           = hppa_tr_tb_start,
434551b061fbSRichard Henderson     .insn_start         = hppa_tr_insn_start,
434651b061fbSRichard Henderson     .translate_insn     = hppa_tr_translate_insn,
434751b061fbSRichard Henderson     .tb_stop            = hppa_tr_tb_stop,
434851b061fbSRichard Henderson     .disas_log          = hppa_tr_disas_log,
434951b061fbSRichard Henderson };
435051b061fbSRichard Henderson 
4351306c8721SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
4352306c8721SRichard Henderson                            target_ulong pc, void *host_pc)
435351b061fbSRichard Henderson {
435451b061fbSRichard Henderson     DisasContext ctx;
4355306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc, &hppa_tr_ops, &ctx.base);
435661766fe9SRichard Henderson }
4357