xref: /openbmc/qemu/target/hppa/translate.c (revision fe2d066a9e3dc3d902ef7d3860c077563146b35b)
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"
260843563fSRichard Henderson #include "tcg/tcg-op-gvec.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 
32d53106c9SRichard Henderson #define HELPER_H "helper.h"
33d53106c9SRichard Henderson #include "exec/helper-info.c.inc"
34d53106c9SRichard Henderson #undef  HELPER_H
35d53106c9SRichard Henderson 
36aac0f603SRichard Henderson /* Choose to use explicit sizes within this file. */
37aac0f603SRichard Henderson #undef tcg_temp_new
38d53106c9SRichard Henderson 
3961766fe9SRichard Henderson typedef struct DisasCond {
4061766fe9SRichard Henderson     TCGCond c;
416fd0c7bcSRichard Henderson     TCGv_i64 a0, a1;
4261766fe9SRichard Henderson } DisasCond;
4361766fe9SRichard Henderson 
4461766fe9SRichard Henderson typedef struct DisasContext {
45d01a3625SRichard Henderson     DisasContextBase base;
4661766fe9SRichard Henderson     CPUState *cs;
47f5b5c857SRichard Henderson     TCGOp *insn_start;
4861766fe9SRichard Henderson 
49c53e401eSRichard Henderson     uint64_t iaoq_f;
50c53e401eSRichard Henderson     uint64_t iaoq_b;
51c53e401eSRichard Henderson     uint64_t iaoq_n;
526fd0c7bcSRichard Henderson     TCGv_i64 iaoq_n_var;
5361766fe9SRichard Henderson 
5461766fe9SRichard Henderson     DisasCond null_cond;
5561766fe9SRichard Henderson     TCGLabel *null_lab;
5661766fe9SRichard Henderson 
57a4db4a78SRichard Henderson     TCGv_i64 zero;
58a4db4a78SRichard Henderson 
591a19da0dSRichard Henderson     uint32_t insn;
60494737b7SRichard Henderson     uint32_t tb_flags;
613d68ee7bSRichard Henderson     int mmu_idx;
623d68ee7bSRichard Henderson     int privilege;
6361766fe9SRichard Henderson     bool psw_n_nonzero;
64bd6243a3SRichard Henderson     bool is_pa20;
65217d1a5eSRichard Henderson 
66217d1a5eSRichard Henderson #ifdef CONFIG_USER_ONLY
67217d1a5eSRichard Henderson     MemOp unalign;
68217d1a5eSRichard Henderson #endif
6961766fe9SRichard Henderson } DisasContext;
7061766fe9SRichard Henderson 
71217d1a5eSRichard Henderson #ifdef CONFIG_USER_ONLY
72217d1a5eSRichard Henderson #define UNALIGN(C)       (C)->unalign
7317fe594cSRichard Henderson #define MMU_DISABLED(C)  false
74217d1a5eSRichard Henderson #else
752d4afb03SRichard Henderson #define UNALIGN(C)       MO_ALIGN
7617fe594cSRichard Henderson #define MMU_DISABLED(C)  MMU_IDX_MMU_DISABLED((C)->mmu_idx)
77217d1a5eSRichard Henderson #endif
78217d1a5eSRichard Henderson 
79e36f27efSRichard Henderson /* Note that ssm/rsm instructions number PSW_W and PSW_E differently.  */
80451e4ffdSRichard Henderson static int expand_sm_imm(DisasContext *ctx, int val)
81e36f27efSRichard Henderson {
82881d1073SHelge Deller     /* Keep unimplemented bits disabled -- see cpu_hppa_put_psw. */
83881d1073SHelge Deller     if (ctx->is_pa20) {
84e36f27efSRichard Henderson         if (val & PSW_SM_W) {
85881d1073SHelge Deller             val |= PSW_W;
86881d1073SHelge Deller         }
87881d1073SHelge Deller         val &= ~(PSW_SM_W | PSW_SM_E | PSW_G);
88881d1073SHelge Deller     } else {
89881d1073SHelge Deller         val &= ~(PSW_SM_W | PSW_SM_E | PSW_O);
90e36f27efSRichard Henderson     }
91e36f27efSRichard Henderson     return val;
92e36f27efSRichard Henderson }
93e36f27efSRichard Henderson 
94deee69a1SRichard Henderson /* Inverted space register indicates 0 means sr0 not inferred from base.  */
95451e4ffdSRichard Henderson static int expand_sr3x(DisasContext *ctx, int val)
96deee69a1SRichard Henderson {
97deee69a1SRichard Henderson     return ~val;
98deee69a1SRichard Henderson }
99deee69a1SRichard Henderson 
1001cd012a5SRichard Henderson /* Convert the M:A bits within a memory insn to the tri-state value
1011cd012a5SRichard Henderson    we use for the final M.  */
102451e4ffdSRichard Henderson static int ma_to_m(DisasContext *ctx, int val)
1031cd012a5SRichard Henderson {
1041cd012a5SRichard Henderson     return val & 2 ? (val & 1 ? -1 : 1) : 0;
1051cd012a5SRichard Henderson }
1061cd012a5SRichard Henderson 
107740038d7SRichard Henderson /* Convert the sign of the displacement to a pre or post-modify.  */
108451e4ffdSRichard Henderson static int pos_to_m(DisasContext *ctx, int val)
109740038d7SRichard Henderson {
110740038d7SRichard Henderson     return val ? 1 : -1;
111740038d7SRichard Henderson }
112740038d7SRichard Henderson 
113451e4ffdSRichard Henderson static int neg_to_m(DisasContext *ctx, int val)
114740038d7SRichard Henderson {
115740038d7SRichard Henderson     return val ? -1 : 1;
116740038d7SRichard Henderson }
117740038d7SRichard Henderson 
118740038d7SRichard Henderson /* Used for branch targets and fp memory ops.  */
119451e4ffdSRichard Henderson static int expand_shl2(DisasContext *ctx, int val)
12001afb7beSRichard Henderson {
12101afb7beSRichard Henderson     return val << 2;
12201afb7beSRichard Henderson }
12301afb7beSRichard Henderson 
1240588e061SRichard Henderson /* Used for assemble_21.  */
125451e4ffdSRichard Henderson static int expand_shl11(DisasContext *ctx, int val)
1260588e061SRichard Henderson {
1270588e061SRichard Henderson     return val << 11;
1280588e061SRichard Henderson }
1290588e061SRichard Henderson 
13072ae4f2bSRichard Henderson static int assemble_6(DisasContext *ctx, int val)
13172ae4f2bSRichard Henderson {
13272ae4f2bSRichard Henderson     /*
13372ae4f2bSRichard Henderson      * Officially, 32 * x + 32 - y.
13472ae4f2bSRichard Henderson      * Here, x is already in bit 5, and y is [4:0].
13572ae4f2bSRichard Henderson      * Since -y = ~y + 1, in 5 bits 32 - y => y ^ 31 + 1,
13672ae4f2bSRichard Henderson      * with the overflow from bit 4 summing with x.
13772ae4f2bSRichard Henderson      */
13872ae4f2bSRichard Henderson     return (val ^ 31) + 1;
13972ae4f2bSRichard Henderson }
14072ae4f2bSRichard Henderson 
1414768c28eSRichard Henderson /* Expander for assemble_16a(s,cat(im10a,0),i). */
1424768c28eSRichard Henderson static int expand_11a(DisasContext *ctx, int val)
1434768c28eSRichard Henderson {
1444768c28eSRichard Henderson     /*
1454768c28eSRichard Henderson      * @val is bit 0 and bits [4:15].
1464768c28eSRichard Henderson      * Swizzle thing around depending on PSW.W.
1474768c28eSRichard Henderson      */
1484768c28eSRichard Henderson     int im10a = extract32(val, 1, 10);
1494768c28eSRichard Henderson     int s = extract32(val, 11, 2);
1504768c28eSRichard Henderson     int i = (-(val & 1) << 13) | (im10a << 3);
1514768c28eSRichard Henderson 
1524768c28eSRichard Henderson     if (ctx->tb_flags & PSW_W) {
1534768c28eSRichard Henderson         i ^= s << 13;
1544768c28eSRichard Henderson     }
1554768c28eSRichard Henderson     return i;
1564768c28eSRichard Henderson }
1574768c28eSRichard Henderson 
15846174e14SRichard Henderson /* Expander for assemble_16a(s,im11a,i). */
15946174e14SRichard Henderson static int expand_12a(DisasContext *ctx, int val)
16046174e14SRichard Henderson {
16146174e14SRichard Henderson     /*
16246174e14SRichard Henderson      * @val is bit 0 and bits [3:15].
16346174e14SRichard Henderson      * Swizzle thing around depending on PSW.W.
16446174e14SRichard Henderson      */
16546174e14SRichard Henderson     int im11a = extract32(val, 1, 11);
16646174e14SRichard Henderson     int s = extract32(val, 12, 2);
16746174e14SRichard Henderson     int i = (-(val & 1) << 13) | (im11a << 2);
16846174e14SRichard Henderson 
16946174e14SRichard Henderson     if (ctx->tb_flags & PSW_W) {
17046174e14SRichard Henderson         i ^= s << 13;
17146174e14SRichard Henderson     }
17246174e14SRichard Henderson     return i;
17346174e14SRichard Henderson }
17446174e14SRichard Henderson 
17572bace2dSRichard Henderson /* Expander for assemble_16(s,im14). */
17672bace2dSRichard Henderson static int expand_16(DisasContext *ctx, int val)
17772bace2dSRichard Henderson {
17872bace2dSRichard Henderson     /*
17972bace2dSRichard Henderson      * @val is bits [0:15], containing both im14 and s.
18072bace2dSRichard Henderson      * Swizzle thing around depending on PSW.W.
18172bace2dSRichard Henderson      */
18272bace2dSRichard Henderson     int s = extract32(val, 14, 2);
18372bace2dSRichard Henderson     int i = (-(val & 1) << 13) | extract32(val, 1, 13);
18472bace2dSRichard Henderson 
18572bace2dSRichard Henderson     if (ctx->tb_flags & PSW_W) {
18672bace2dSRichard Henderson         i ^= s << 13;
18772bace2dSRichard Henderson     }
18872bace2dSRichard Henderson     return i;
18972bace2dSRichard Henderson }
19072bace2dSRichard Henderson 
19172bace2dSRichard Henderson /* The sp field is only present with !PSW_W. */
19272bace2dSRichard Henderson static int sp0_if_wide(DisasContext *ctx, int sp)
19372bace2dSRichard Henderson {
19472bace2dSRichard Henderson     return ctx->tb_flags & PSW_W ? 0 : sp;
19572bace2dSRichard Henderson }
19672bace2dSRichard Henderson 
197c65c3ee1SRichard Henderson /* Translate CMPI doubleword conditions to standard. */
198c65c3ee1SRichard Henderson static int cmpbid_c(DisasContext *ctx, int val)
199c65c3ee1SRichard Henderson {
200c65c3ee1SRichard Henderson     return val ? val : 4; /* 0 == "*<<" */
201c65c3ee1SRichard Henderson }
202c65c3ee1SRichard Henderson 
20382d0c831SRichard Henderson /*
20482d0c831SRichard Henderson  * In many places pa1.x did not decode the bit that later became
20582d0c831SRichard Henderson  * the pa2.0 D bit.  Suppress D unless the cpu is pa2.0.
20682d0c831SRichard Henderson  */
20782d0c831SRichard Henderson static int pa20_d(DisasContext *ctx, int val)
20882d0c831SRichard Henderson {
20982d0c831SRichard Henderson     return ctx->is_pa20 & val;
21082d0c831SRichard Henderson }
21101afb7beSRichard Henderson 
21240f9f908SRichard Henderson /* Include the auto-generated decoder.  */
213abff1abfSPaolo Bonzini #include "decode-insns.c.inc"
21440f9f908SRichard Henderson 
21561766fe9SRichard Henderson /* We are not using a goto_tb (for whatever reason), but have updated
21661766fe9SRichard Henderson    the iaq (for whatever reason), so don't do it again on exit.  */
217869051eaSRichard Henderson #define DISAS_IAQ_N_UPDATED  DISAS_TARGET_0
21861766fe9SRichard Henderson 
21961766fe9SRichard Henderson /* We are exiting the TB, but have neither emitted a goto_tb, nor
22061766fe9SRichard Henderson    updated the iaq for the next instruction to be executed.  */
221869051eaSRichard Henderson #define DISAS_IAQ_N_STALE    DISAS_TARGET_1
22261766fe9SRichard Henderson 
223e1b5a5edSRichard Henderson /* Similarly, but we want to return to the main loop immediately
224e1b5a5edSRichard Henderson    to recognize unmasked interrupts.  */
225e1b5a5edSRichard Henderson #define DISAS_IAQ_N_STALE_EXIT      DISAS_TARGET_2
226c5d0aec2SRichard Henderson #define DISAS_EXIT                  DISAS_TARGET_3
227e1b5a5edSRichard Henderson 
22861766fe9SRichard Henderson /* global register indexes */
2296fd0c7bcSRichard Henderson static TCGv_i64 cpu_gr[32];
23033423472SRichard Henderson static TCGv_i64 cpu_sr[4];
231494737b7SRichard Henderson static TCGv_i64 cpu_srH;
2326fd0c7bcSRichard Henderson static TCGv_i64 cpu_iaoq_f;
2336fd0c7bcSRichard Henderson static TCGv_i64 cpu_iaoq_b;
234c301f34eSRichard Henderson static TCGv_i64 cpu_iasq_f;
235c301f34eSRichard Henderson static TCGv_i64 cpu_iasq_b;
2366fd0c7bcSRichard Henderson static TCGv_i64 cpu_sar;
2376fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_n;
2386fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_v;
2396fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_cb;
2406fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_cb_msb;
24161766fe9SRichard Henderson 
24261766fe9SRichard Henderson void hppa_translate_init(void)
24361766fe9SRichard Henderson {
24461766fe9SRichard Henderson #define DEF_VAR(V)  { &cpu_##V, #V, offsetof(CPUHPPAState, V) }
24561766fe9SRichard Henderson 
2466fd0c7bcSRichard Henderson     typedef struct { TCGv_i64 *var; const char *name; int ofs; } GlobalVar;
24761766fe9SRichard Henderson     static const GlobalVar vars[] = {
24835136a77SRichard Henderson         { &cpu_sar, "sar", offsetof(CPUHPPAState, cr[CR_SAR]) },
24961766fe9SRichard Henderson         DEF_VAR(psw_n),
25061766fe9SRichard Henderson         DEF_VAR(psw_v),
25161766fe9SRichard Henderson         DEF_VAR(psw_cb),
25261766fe9SRichard Henderson         DEF_VAR(psw_cb_msb),
25361766fe9SRichard Henderson         DEF_VAR(iaoq_f),
25461766fe9SRichard Henderson         DEF_VAR(iaoq_b),
25561766fe9SRichard Henderson     };
25661766fe9SRichard Henderson 
25761766fe9SRichard Henderson #undef DEF_VAR
25861766fe9SRichard Henderson 
25961766fe9SRichard Henderson     /* Use the symbolic register names that match the disassembler.  */
26061766fe9SRichard Henderson     static const char gr_names[32][4] = {
26161766fe9SRichard Henderson         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
26261766fe9SRichard Henderson         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
26361766fe9SRichard Henderson         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
26461766fe9SRichard Henderson         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
26561766fe9SRichard Henderson     };
26633423472SRichard Henderson     /* SR[4-7] are not global registers so that we can index them.  */
267494737b7SRichard Henderson     static const char sr_names[5][4] = {
268494737b7SRichard Henderson         "sr0", "sr1", "sr2", "sr3", "srH"
26933423472SRichard Henderson     };
27061766fe9SRichard Henderson 
27161766fe9SRichard Henderson     int i;
27261766fe9SRichard Henderson 
273f764718dSRichard Henderson     cpu_gr[0] = NULL;
27461766fe9SRichard Henderson     for (i = 1; i < 32; i++) {
275ad75a51eSRichard Henderson         cpu_gr[i] = tcg_global_mem_new(tcg_env,
27661766fe9SRichard Henderson                                        offsetof(CPUHPPAState, gr[i]),
27761766fe9SRichard Henderson                                        gr_names[i]);
27861766fe9SRichard Henderson     }
27933423472SRichard Henderson     for (i = 0; i < 4; i++) {
280ad75a51eSRichard Henderson         cpu_sr[i] = tcg_global_mem_new_i64(tcg_env,
28133423472SRichard Henderson                                            offsetof(CPUHPPAState, sr[i]),
28233423472SRichard Henderson                                            sr_names[i]);
28333423472SRichard Henderson     }
284ad75a51eSRichard Henderson     cpu_srH = tcg_global_mem_new_i64(tcg_env,
285494737b7SRichard Henderson                                      offsetof(CPUHPPAState, sr[4]),
286494737b7SRichard Henderson                                      sr_names[4]);
28761766fe9SRichard Henderson 
28861766fe9SRichard Henderson     for (i = 0; i < ARRAY_SIZE(vars); ++i) {
28961766fe9SRichard Henderson         const GlobalVar *v = &vars[i];
290ad75a51eSRichard Henderson         *v->var = tcg_global_mem_new(tcg_env, v->ofs, v->name);
29161766fe9SRichard Henderson     }
292c301f34eSRichard Henderson 
293ad75a51eSRichard Henderson     cpu_iasq_f = tcg_global_mem_new_i64(tcg_env,
294c301f34eSRichard Henderson                                         offsetof(CPUHPPAState, iasq_f),
295c301f34eSRichard Henderson                                         "iasq_f");
296ad75a51eSRichard Henderson     cpu_iasq_b = tcg_global_mem_new_i64(tcg_env,
297c301f34eSRichard Henderson                                         offsetof(CPUHPPAState, iasq_b),
298c301f34eSRichard Henderson                                         "iasq_b");
29961766fe9SRichard Henderson }
30061766fe9SRichard Henderson 
301f5b5c857SRichard Henderson static void set_insn_breg(DisasContext *ctx, int breg)
302f5b5c857SRichard Henderson {
303f5b5c857SRichard Henderson     assert(ctx->insn_start != NULL);
304f5b5c857SRichard Henderson     tcg_set_insn_start_param(ctx->insn_start, 2, breg);
305f5b5c857SRichard Henderson     ctx->insn_start = NULL;
306f5b5c857SRichard Henderson }
307f5b5c857SRichard Henderson 
308129e9cc3SRichard Henderson static DisasCond cond_make_f(void)
309129e9cc3SRichard Henderson {
310f764718dSRichard Henderson     return (DisasCond){
311f764718dSRichard Henderson         .c = TCG_COND_NEVER,
312f764718dSRichard Henderson         .a0 = NULL,
313f764718dSRichard Henderson         .a1 = NULL,
314f764718dSRichard Henderson     };
315129e9cc3SRichard Henderson }
316129e9cc3SRichard Henderson 
317df0232feSRichard Henderson static DisasCond cond_make_t(void)
318df0232feSRichard Henderson {
319df0232feSRichard Henderson     return (DisasCond){
320df0232feSRichard Henderson         .c = TCG_COND_ALWAYS,
321df0232feSRichard Henderson         .a0 = NULL,
322df0232feSRichard Henderson         .a1 = NULL,
323df0232feSRichard Henderson     };
324df0232feSRichard Henderson }
325df0232feSRichard Henderson 
326129e9cc3SRichard Henderson static DisasCond cond_make_n(void)
327129e9cc3SRichard Henderson {
328f764718dSRichard Henderson     return (DisasCond){
329f764718dSRichard Henderson         .c = TCG_COND_NE,
330f764718dSRichard Henderson         .a0 = cpu_psw_n,
3316fd0c7bcSRichard Henderson         .a1 = tcg_constant_i64(0)
332f764718dSRichard Henderson     };
333129e9cc3SRichard Henderson }
334129e9cc3SRichard Henderson 
3356fd0c7bcSRichard Henderson static DisasCond cond_make_tmp(TCGCond c, TCGv_i64 a0, TCGv_i64 a1)
336b47a4a02SSven Schnelle {
337b47a4a02SSven Schnelle     assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
3384fe9533aSRichard Henderson     return (DisasCond){ .c = c, .a0 = a0, .a1 = a1 };
3394fe9533aSRichard Henderson }
3404fe9533aSRichard Henderson 
3416fd0c7bcSRichard Henderson static DisasCond cond_make_0_tmp(TCGCond c, TCGv_i64 a0)
3424fe9533aSRichard Henderson {
3436fd0c7bcSRichard Henderson     return cond_make_tmp(c, a0, tcg_constant_i64(0));
344b47a4a02SSven Schnelle }
345b47a4a02SSven Schnelle 
3466fd0c7bcSRichard Henderson static DisasCond cond_make_0(TCGCond c, TCGv_i64 a0)
347129e9cc3SRichard Henderson {
348aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3496fd0c7bcSRichard Henderson     tcg_gen_mov_i64(tmp, a0);
350b47a4a02SSven Schnelle     return cond_make_0_tmp(c, tmp);
351129e9cc3SRichard Henderson }
352129e9cc3SRichard Henderson 
3536fd0c7bcSRichard Henderson static DisasCond cond_make(TCGCond c, TCGv_i64 a0, TCGv_i64 a1)
354129e9cc3SRichard Henderson {
355aac0f603SRichard Henderson     TCGv_i64 t0 = tcg_temp_new_i64();
356aac0f603SRichard Henderson     TCGv_i64 t1 = tcg_temp_new_i64();
357129e9cc3SRichard Henderson 
3586fd0c7bcSRichard Henderson     tcg_gen_mov_i64(t0, a0);
3596fd0c7bcSRichard Henderson     tcg_gen_mov_i64(t1, a1);
3604fe9533aSRichard Henderson     return cond_make_tmp(c, t0, t1);
361129e9cc3SRichard Henderson }
362129e9cc3SRichard Henderson 
363129e9cc3SRichard Henderson static void cond_free(DisasCond *cond)
364129e9cc3SRichard Henderson {
365129e9cc3SRichard Henderson     switch (cond->c) {
366129e9cc3SRichard Henderson     default:
367f764718dSRichard Henderson         cond->a0 = NULL;
368f764718dSRichard Henderson         cond->a1 = NULL;
369129e9cc3SRichard Henderson         /* fallthru */
370129e9cc3SRichard Henderson     case TCG_COND_ALWAYS:
371129e9cc3SRichard Henderson         cond->c = TCG_COND_NEVER;
372129e9cc3SRichard Henderson         break;
373129e9cc3SRichard Henderson     case TCG_COND_NEVER:
374129e9cc3SRichard Henderson         break;
375129e9cc3SRichard Henderson     }
376129e9cc3SRichard Henderson }
377129e9cc3SRichard Henderson 
3786fd0c7bcSRichard Henderson static TCGv_i64 load_gpr(DisasContext *ctx, unsigned reg)
37961766fe9SRichard Henderson {
38061766fe9SRichard Henderson     if (reg == 0) {
381bc3da3cfSRichard Henderson         return ctx->zero;
38261766fe9SRichard Henderson     } else {
38361766fe9SRichard Henderson         return cpu_gr[reg];
38461766fe9SRichard Henderson     }
38561766fe9SRichard Henderson }
38661766fe9SRichard Henderson 
3876fd0c7bcSRichard Henderson static TCGv_i64 dest_gpr(DisasContext *ctx, unsigned reg)
38861766fe9SRichard Henderson {
389129e9cc3SRichard Henderson     if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
390aac0f603SRichard Henderson         return tcg_temp_new_i64();
39161766fe9SRichard Henderson     } else {
39261766fe9SRichard Henderson         return cpu_gr[reg];
39361766fe9SRichard Henderson     }
39461766fe9SRichard Henderson }
39561766fe9SRichard Henderson 
3966fd0c7bcSRichard Henderson static void save_or_nullify(DisasContext *ctx, TCGv_i64 dest, TCGv_i64 t)
397129e9cc3SRichard Henderson {
398129e9cc3SRichard Henderson     if (ctx->null_cond.c != TCG_COND_NEVER) {
3996fd0c7bcSRichard Henderson         tcg_gen_movcond_i64(ctx->null_cond.c, dest, ctx->null_cond.a0,
400129e9cc3SRichard Henderson                             ctx->null_cond.a1, dest, t);
401129e9cc3SRichard Henderson     } else {
4026fd0c7bcSRichard Henderson         tcg_gen_mov_i64(dest, t);
403129e9cc3SRichard Henderson     }
404129e9cc3SRichard Henderson }
405129e9cc3SRichard Henderson 
4066fd0c7bcSRichard Henderson static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_i64 t)
407129e9cc3SRichard Henderson {
408129e9cc3SRichard Henderson     if (reg != 0) {
409129e9cc3SRichard Henderson         save_or_nullify(ctx, cpu_gr[reg], t);
410129e9cc3SRichard Henderson     }
411129e9cc3SRichard Henderson }
412129e9cc3SRichard Henderson 
413e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
41496d6407fSRichard Henderson # define HI_OFS  0
41596d6407fSRichard Henderson # define LO_OFS  4
41696d6407fSRichard Henderson #else
41796d6407fSRichard Henderson # define HI_OFS  4
41896d6407fSRichard Henderson # define LO_OFS  0
41996d6407fSRichard Henderson #endif
42096d6407fSRichard Henderson 
42196d6407fSRichard Henderson static TCGv_i32 load_frw_i32(unsigned rt)
42296d6407fSRichard Henderson {
42396d6407fSRichard Henderson     TCGv_i32 ret = tcg_temp_new_i32();
424ad75a51eSRichard Henderson     tcg_gen_ld_i32(ret, tcg_env,
42596d6407fSRichard Henderson                    offsetof(CPUHPPAState, fr[rt & 31])
42696d6407fSRichard Henderson                    + (rt & 32 ? LO_OFS : HI_OFS));
42796d6407fSRichard Henderson     return ret;
42896d6407fSRichard Henderson }
42996d6407fSRichard Henderson 
430ebe9383cSRichard Henderson static TCGv_i32 load_frw0_i32(unsigned rt)
431ebe9383cSRichard Henderson {
432ebe9383cSRichard Henderson     if (rt == 0) {
4330992a930SRichard Henderson         TCGv_i32 ret = tcg_temp_new_i32();
4340992a930SRichard Henderson         tcg_gen_movi_i32(ret, 0);
4350992a930SRichard Henderson         return ret;
436ebe9383cSRichard Henderson     } else {
437ebe9383cSRichard Henderson         return load_frw_i32(rt);
438ebe9383cSRichard Henderson     }
439ebe9383cSRichard Henderson }
440ebe9383cSRichard Henderson 
441ebe9383cSRichard Henderson static TCGv_i64 load_frw0_i64(unsigned rt)
442ebe9383cSRichard Henderson {
443ebe9383cSRichard Henderson     TCGv_i64 ret = tcg_temp_new_i64();
4440992a930SRichard Henderson     if (rt == 0) {
4450992a930SRichard Henderson         tcg_gen_movi_i64(ret, 0);
4460992a930SRichard Henderson     } else {
447ad75a51eSRichard Henderson         tcg_gen_ld32u_i64(ret, tcg_env,
448ebe9383cSRichard Henderson                           offsetof(CPUHPPAState, fr[rt & 31])
449ebe9383cSRichard Henderson                           + (rt & 32 ? LO_OFS : HI_OFS));
450ebe9383cSRichard Henderson     }
4510992a930SRichard Henderson     return ret;
452ebe9383cSRichard Henderson }
453ebe9383cSRichard Henderson 
45496d6407fSRichard Henderson static void save_frw_i32(unsigned rt, TCGv_i32 val)
45596d6407fSRichard Henderson {
456ad75a51eSRichard Henderson     tcg_gen_st_i32(val, tcg_env,
45796d6407fSRichard Henderson                    offsetof(CPUHPPAState, fr[rt & 31])
45896d6407fSRichard Henderson                    + (rt & 32 ? LO_OFS : HI_OFS));
45996d6407fSRichard Henderson }
46096d6407fSRichard Henderson 
46196d6407fSRichard Henderson #undef HI_OFS
46296d6407fSRichard Henderson #undef LO_OFS
46396d6407fSRichard Henderson 
46496d6407fSRichard Henderson static TCGv_i64 load_frd(unsigned rt)
46596d6407fSRichard Henderson {
46696d6407fSRichard Henderson     TCGv_i64 ret = tcg_temp_new_i64();
467ad75a51eSRichard Henderson     tcg_gen_ld_i64(ret, tcg_env, offsetof(CPUHPPAState, fr[rt]));
46896d6407fSRichard Henderson     return ret;
46996d6407fSRichard Henderson }
47096d6407fSRichard Henderson 
471ebe9383cSRichard Henderson static TCGv_i64 load_frd0(unsigned rt)
472ebe9383cSRichard Henderson {
473ebe9383cSRichard Henderson     if (rt == 0) {
4740992a930SRichard Henderson         TCGv_i64 ret = tcg_temp_new_i64();
4750992a930SRichard Henderson         tcg_gen_movi_i64(ret, 0);
4760992a930SRichard Henderson         return ret;
477ebe9383cSRichard Henderson     } else {
478ebe9383cSRichard Henderson         return load_frd(rt);
479ebe9383cSRichard Henderson     }
480ebe9383cSRichard Henderson }
481ebe9383cSRichard Henderson 
48296d6407fSRichard Henderson static void save_frd(unsigned rt, TCGv_i64 val)
48396d6407fSRichard Henderson {
484ad75a51eSRichard Henderson     tcg_gen_st_i64(val, tcg_env, offsetof(CPUHPPAState, fr[rt]));
48596d6407fSRichard Henderson }
48696d6407fSRichard Henderson 
48733423472SRichard Henderson static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
48833423472SRichard Henderson {
48933423472SRichard Henderson #ifdef CONFIG_USER_ONLY
49033423472SRichard Henderson     tcg_gen_movi_i64(dest, 0);
49133423472SRichard Henderson #else
49233423472SRichard Henderson     if (reg < 4) {
49333423472SRichard Henderson         tcg_gen_mov_i64(dest, cpu_sr[reg]);
494494737b7SRichard Henderson     } else if (ctx->tb_flags & TB_FLAG_SR_SAME) {
495494737b7SRichard Henderson         tcg_gen_mov_i64(dest, cpu_srH);
49633423472SRichard Henderson     } else {
497ad75a51eSRichard Henderson         tcg_gen_ld_i64(dest, tcg_env, offsetof(CPUHPPAState, sr[reg]));
49833423472SRichard Henderson     }
49933423472SRichard Henderson #endif
50033423472SRichard Henderson }
50133423472SRichard Henderson 
502129e9cc3SRichard Henderson /* Skip over the implementation of an insn that has been nullified.
503129e9cc3SRichard Henderson    Use this when the insn is too complex for a conditional move.  */
504129e9cc3SRichard Henderson static void nullify_over(DisasContext *ctx)
505129e9cc3SRichard Henderson {
506129e9cc3SRichard Henderson     if (ctx->null_cond.c != TCG_COND_NEVER) {
507129e9cc3SRichard Henderson         /* The always condition should have been handled in the main loop.  */
508129e9cc3SRichard Henderson         assert(ctx->null_cond.c != TCG_COND_ALWAYS);
509129e9cc3SRichard Henderson 
510129e9cc3SRichard Henderson         ctx->null_lab = gen_new_label();
511129e9cc3SRichard Henderson 
512129e9cc3SRichard Henderson         /* If we're using PSW[N], copy it to a temp because... */
5136e94937aSRichard Henderson         if (ctx->null_cond.a0 == cpu_psw_n) {
514aac0f603SRichard Henderson             ctx->null_cond.a0 = tcg_temp_new_i64();
5156fd0c7bcSRichard Henderson             tcg_gen_mov_i64(ctx->null_cond.a0, cpu_psw_n);
516129e9cc3SRichard Henderson         }
517129e9cc3SRichard Henderson         /* ... we clear it before branching over the implementation,
518129e9cc3SRichard Henderson            so that (1) it's clear after nullifying this insn and
519129e9cc3SRichard Henderson            (2) if this insn nullifies the next, PSW[N] is valid.  */
520129e9cc3SRichard Henderson         if (ctx->psw_n_nonzero) {
521129e9cc3SRichard Henderson             ctx->psw_n_nonzero = false;
5226fd0c7bcSRichard Henderson             tcg_gen_movi_i64(cpu_psw_n, 0);
523129e9cc3SRichard Henderson         }
524129e9cc3SRichard Henderson 
5256fd0c7bcSRichard Henderson         tcg_gen_brcond_i64(ctx->null_cond.c, ctx->null_cond.a0,
526129e9cc3SRichard Henderson                            ctx->null_cond.a1, ctx->null_lab);
527129e9cc3SRichard Henderson         cond_free(&ctx->null_cond);
528129e9cc3SRichard Henderson     }
529129e9cc3SRichard Henderson }
530129e9cc3SRichard Henderson 
531129e9cc3SRichard Henderson /* Save the current nullification state to PSW[N].  */
532129e9cc3SRichard Henderson static void nullify_save(DisasContext *ctx)
533129e9cc3SRichard Henderson {
534129e9cc3SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER) {
535129e9cc3SRichard Henderson         if (ctx->psw_n_nonzero) {
5366fd0c7bcSRichard Henderson             tcg_gen_movi_i64(cpu_psw_n, 0);
537129e9cc3SRichard Henderson         }
538129e9cc3SRichard Henderson         return;
539129e9cc3SRichard Henderson     }
5406e94937aSRichard Henderson     if (ctx->null_cond.a0 != cpu_psw_n) {
5416fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(ctx->null_cond.c, cpu_psw_n,
542129e9cc3SRichard Henderson                             ctx->null_cond.a0, ctx->null_cond.a1);
543129e9cc3SRichard Henderson         ctx->psw_n_nonzero = true;
544129e9cc3SRichard Henderson     }
545129e9cc3SRichard Henderson     cond_free(&ctx->null_cond);
546129e9cc3SRichard Henderson }
547129e9cc3SRichard Henderson 
548129e9cc3SRichard Henderson /* Set a PSW[N] to X.  The intention is that this is used immediately
549129e9cc3SRichard Henderson    before a goto_tb/exit_tb, so that there is no fallthru path to other
550129e9cc3SRichard Henderson    code within the TB.  Therefore we do not update psw_n_nonzero.  */
551129e9cc3SRichard Henderson static void nullify_set(DisasContext *ctx, bool x)
552129e9cc3SRichard Henderson {
553129e9cc3SRichard Henderson     if (ctx->psw_n_nonzero || x) {
5546fd0c7bcSRichard Henderson         tcg_gen_movi_i64(cpu_psw_n, x);
555129e9cc3SRichard Henderson     }
556129e9cc3SRichard Henderson }
557129e9cc3SRichard Henderson 
558129e9cc3SRichard Henderson /* Mark the end of an instruction that may have been nullified.
55940f9f908SRichard Henderson    This is the pair to nullify_over.  Always returns true so that
56040f9f908SRichard Henderson    it may be tail-called from a translate function.  */
56131234768SRichard Henderson static bool nullify_end(DisasContext *ctx)
562129e9cc3SRichard Henderson {
563129e9cc3SRichard Henderson     TCGLabel *null_lab = ctx->null_lab;
56431234768SRichard Henderson     DisasJumpType status = ctx->base.is_jmp;
565129e9cc3SRichard Henderson 
566f49b3537SRichard Henderson     /* For NEXT, NORETURN, STALE, we can easily continue (or exit).
567f49b3537SRichard Henderson        For UPDATED, we cannot update on the nullified path.  */
568f49b3537SRichard Henderson     assert(status != DISAS_IAQ_N_UPDATED);
569f49b3537SRichard Henderson 
570129e9cc3SRichard Henderson     if (likely(null_lab == NULL)) {
571129e9cc3SRichard Henderson         /* The current insn wasn't conditional or handled the condition
572129e9cc3SRichard Henderson            applied to it without a branch, so the (new) setting of
573129e9cc3SRichard Henderson            NULL_COND can be applied directly to the next insn.  */
57431234768SRichard Henderson         return true;
575129e9cc3SRichard Henderson     }
576129e9cc3SRichard Henderson     ctx->null_lab = NULL;
577129e9cc3SRichard Henderson 
578129e9cc3SRichard Henderson     if (likely(ctx->null_cond.c == TCG_COND_NEVER)) {
579129e9cc3SRichard Henderson         /* The next instruction will be unconditional,
580129e9cc3SRichard Henderson            and NULL_COND already reflects that.  */
581129e9cc3SRichard Henderson         gen_set_label(null_lab);
582129e9cc3SRichard Henderson     } else {
583129e9cc3SRichard Henderson         /* The insn that we just executed is itself nullifying the next
584129e9cc3SRichard Henderson            instruction.  Store the condition in the PSW[N] global.
585129e9cc3SRichard Henderson            We asserted PSW[N] = 0 in nullify_over, so that after the
586129e9cc3SRichard Henderson            label we have the proper value in place.  */
587129e9cc3SRichard Henderson         nullify_save(ctx);
588129e9cc3SRichard Henderson         gen_set_label(null_lab);
589129e9cc3SRichard Henderson         ctx->null_cond = cond_make_n();
590129e9cc3SRichard Henderson     }
591869051eaSRichard Henderson     if (status == DISAS_NORETURN) {
59231234768SRichard Henderson         ctx->base.is_jmp = DISAS_NEXT;
593129e9cc3SRichard Henderson     }
59431234768SRichard Henderson     return true;
595129e9cc3SRichard Henderson }
596129e9cc3SRichard Henderson 
5976fd0c7bcSRichard Henderson static void copy_iaoq_entry(DisasContext *ctx, TCGv_i64 dest,
5986fd0c7bcSRichard Henderson                             uint64_t ival, TCGv_i64 vval)
59961766fe9SRichard Henderson {
6007d50b696SSven Schnelle     uint64_t mask = gva_offset_mask(ctx->tb_flags);
601f13bf343SRichard Henderson 
602f13bf343SRichard Henderson     if (ival != -1) {
6036fd0c7bcSRichard Henderson         tcg_gen_movi_i64(dest, ival & mask);
604f13bf343SRichard Henderson         return;
605f13bf343SRichard Henderson     }
606f13bf343SRichard Henderson     tcg_debug_assert(vval != NULL);
607f13bf343SRichard Henderson 
608f13bf343SRichard Henderson     /*
609f13bf343SRichard Henderson      * We know that the IAOQ is already properly masked.
610f13bf343SRichard Henderson      * This optimization is primarily for "iaoq_f = iaoq_b".
611f13bf343SRichard Henderson      */
612f13bf343SRichard Henderson     if (vval == cpu_iaoq_f || vval == cpu_iaoq_b) {
6136fd0c7bcSRichard Henderson         tcg_gen_mov_i64(dest, vval);
61461766fe9SRichard Henderson     } else {
6156fd0c7bcSRichard Henderson         tcg_gen_andi_i64(dest, vval, mask);
61661766fe9SRichard Henderson     }
61761766fe9SRichard Henderson }
61861766fe9SRichard Henderson 
619c53e401eSRichard Henderson static inline uint64_t iaoq_dest(DisasContext *ctx, int64_t disp)
62061766fe9SRichard Henderson {
62161766fe9SRichard Henderson     return ctx->iaoq_f + disp + 8;
62261766fe9SRichard Henderson }
62361766fe9SRichard Henderson 
62461766fe9SRichard Henderson static void gen_excp_1(int exception)
62561766fe9SRichard Henderson {
626ad75a51eSRichard Henderson     gen_helper_excp(tcg_env, tcg_constant_i32(exception));
62761766fe9SRichard Henderson }
62861766fe9SRichard Henderson 
62931234768SRichard Henderson static void gen_excp(DisasContext *ctx, int exception)
63061766fe9SRichard Henderson {
631741322f4SRichard Henderson     copy_iaoq_entry(ctx, cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
632741322f4SRichard Henderson     copy_iaoq_entry(ctx, cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
633129e9cc3SRichard Henderson     nullify_save(ctx);
63461766fe9SRichard Henderson     gen_excp_1(exception);
63531234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
63661766fe9SRichard Henderson }
63761766fe9SRichard Henderson 
63831234768SRichard Henderson static bool gen_excp_iir(DisasContext *ctx, int exc)
6391a19da0dSRichard Henderson {
64031234768SRichard Henderson     nullify_over(ctx);
6416fd0c7bcSRichard Henderson     tcg_gen_st_i64(tcg_constant_i64(ctx->insn),
642ad75a51eSRichard Henderson                    tcg_env, offsetof(CPUHPPAState, cr[CR_IIR]));
64331234768SRichard Henderson     gen_excp(ctx, exc);
64431234768SRichard Henderson     return nullify_end(ctx);
6451a19da0dSRichard Henderson }
6461a19da0dSRichard Henderson 
64731234768SRichard Henderson static bool gen_illegal(DisasContext *ctx)
64861766fe9SRichard Henderson {
64931234768SRichard Henderson     return gen_excp_iir(ctx, EXCP_ILL);
65061766fe9SRichard Henderson }
65161766fe9SRichard Henderson 
65240f9f908SRichard Henderson #ifdef CONFIG_USER_ONLY
65340f9f908SRichard Henderson #define CHECK_MOST_PRIVILEGED(EXCP) \
65440f9f908SRichard Henderson     return gen_excp_iir(ctx, EXCP)
65540f9f908SRichard Henderson #else
656e1b5a5edSRichard Henderson #define CHECK_MOST_PRIVILEGED(EXCP) \
657e1b5a5edSRichard Henderson     do {                                     \
658e1b5a5edSRichard Henderson         if (ctx->privilege != 0) {           \
65931234768SRichard Henderson             return gen_excp_iir(ctx, EXCP);  \
660e1b5a5edSRichard Henderson         }                                    \
661e1b5a5edSRichard Henderson     } while (0)
66240f9f908SRichard Henderson #endif
663e1b5a5edSRichard Henderson 
664c53e401eSRichard Henderson static bool use_goto_tb(DisasContext *ctx, uint64_t dest)
66561766fe9SRichard Henderson {
66657f91498SRichard Henderson     return translator_use_goto_tb(&ctx->base, dest);
66761766fe9SRichard Henderson }
66861766fe9SRichard Henderson 
669129e9cc3SRichard Henderson /* If the next insn is to be nullified, and it's on the same page,
670129e9cc3SRichard Henderson    and we're not attempting to set a breakpoint on it, then we can
671129e9cc3SRichard Henderson    totally skip the nullified insn.  This avoids creating and
672129e9cc3SRichard Henderson    executing a TB that merely branches to the next TB.  */
673129e9cc3SRichard Henderson static bool use_nullify_skip(DisasContext *ctx)
674129e9cc3SRichard Henderson {
675129e9cc3SRichard Henderson     return (((ctx->iaoq_b ^ ctx->iaoq_f) & TARGET_PAGE_MASK) == 0
676129e9cc3SRichard Henderson             && !cpu_breakpoint_test(ctx->cs, ctx->iaoq_b, BP_ANY));
677129e9cc3SRichard Henderson }
678129e9cc3SRichard Henderson 
67961766fe9SRichard Henderson static void gen_goto_tb(DisasContext *ctx, int which,
680c53e401eSRichard Henderson                         uint64_t f, uint64_t b)
68161766fe9SRichard Henderson {
68261766fe9SRichard Henderson     if (f != -1 && b != -1 && use_goto_tb(ctx, f)) {
68361766fe9SRichard Henderson         tcg_gen_goto_tb(which);
684a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, f, NULL);
685a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, b, NULL);
68607ea28b4SRichard Henderson         tcg_gen_exit_tb(ctx->base.tb, which);
68761766fe9SRichard Henderson     } else {
688741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, f, cpu_iaoq_b);
689741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, b, ctx->iaoq_n_var);
6907f11636dSEmilio G. Cota         tcg_gen_lookup_and_goto_ptr();
69161766fe9SRichard Henderson     }
69261766fe9SRichard Henderson }
69361766fe9SRichard Henderson 
694b47a4a02SSven Schnelle static bool cond_need_sv(int c)
695b47a4a02SSven Schnelle {
696b47a4a02SSven Schnelle     return c == 2 || c == 3 || c == 6;
697b47a4a02SSven Schnelle }
698b47a4a02SSven Schnelle 
699b47a4a02SSven Schnelle static bool cond_need_cb(int c)
700b47a4a02SSven Schnelle {
701b47a4a02SSven Schnelle     return c == 4 || c == 5;
702b47a4a02SSven Schnelle }
703b47a4a02SSven Schnelle 
704b47a4a02SSven Schnelle /*
705b47a4a02SSven Schnelle  * Compute conditional for arithmetic.  See Page 5-3, Table 5-1, of
706b47a4a02SSven Schnelle  * the Parisc 1.1 Architecture Reference Manual for details.
707b47a4a02SSven Schnelle  */
708b2167459SRichard Henderson 
709a751eb31SRichard Henderson static DisasCond do_cond(DisasContext *ctx, unsigned cf, bool d,
710*fe2d066aSRichard Henderson                          TCGv_i64 res, TCGv_i64 uv, TCGv_i64 sv)
711b2167459SRichard Henderson {
712b2167459SRichard Henderson     DisasCond cond;
7136fd0c7bcSRichard Henderson     TCGv_i64 tmp;
714b2167459SRichard Henderson 
715b2167459SRichard Henderson     switch (cf >> 1) {
716b47a4a02SSven Schnelle     case 0: /* Never / TR    (0 / 1) */
717b2167459SRichard Henderson         cond = cond_make_f();
718b2167459SRichard Henderson         break;
719b2167459SRichard Henderson     case 1: /* = / <>        (Z / !Z) */
72082d0c831SRichard Henderson         if (!d) {
721aac0f603SRichard Henderson             tmp = tcg_temp_new_i64();
7226fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, res);
723a751eb31SRichard Henderson             res = tmp;
724a751eb31SRichard Henderson         }
725b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_EQ, res);
726b2167459SRichard Henderson         break;
727b47a4a02SSven Schnelle     case 2: /* < / >=        (N ^ V / !(N ^ V) */
728aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
7296fd0c7bcSRichard Henderson         tcg_gen_xor_i64(tmp, res, sv);
73082d0c831SRichard Henderson         if (!d) {
7316fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(tmp, tmp);
732a751eb31SRichard Henderson         }
733b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_LT, tmp);
734b2167459SRichard Henderson         break;
735b47a4a02SSven Schnelle     case 3: /* <= / >        (N ^ V) | Z / !((N ^ V) | Z) */
736b47a4a02SSven Schnelle         /*
737b47a4a02SSven Schnelle          * Simplify:
738b47a4a02SSven Schnelle          *   (N ^ V) | Z
739b47a4a02SSven Schnelle          *   ((res < 0) ^ (sv < 0)) | !res
740b47a4a02SSven Schnelle          *   ((res ^ sv) < 0) | !res
741b47a4a02SSven Schnelle          *   (~(res ^ sv) >= 0) | !res
742b47a4a02SSven Schnelle          *   !(~(res ^ sv) >> 31) | !res
743b47a4a02SSven Schnelle          *   !(~(res ^ sv) >> 31 & res)
744b47a4a02SSven Schnelle          */
745aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
7466fd0c7bcSRichard Henderson         tcg_gen_eqv_i64(tmp, res, sv);
74782d0c831SRichard Henderson         if (!d) {
7486fd0c7bcSRichard Henderson             tcg_gen_sextract_i64(tmp, tmp, 31, 1);
7496fd0c7bcSRichard Henderson             tcg_gen_and_i64(tmp, tmp, res);
7506fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, tmp);
751a751eb31SRichard Henderson         } else {
7526fd0c7bcSRichard Henderson             tcg_gen_sari_i64(tmp, tmp, 63);
7536fd0c7bcSRichard Henderson             tcg_gen_and_i64(tmp, tmp, res);
754a751eb31SRichard Henderson         }
755b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
756b2167459SRichard Henderson         break;
757*fe2d066aSRichard Henderson     case 4: /* NUV / UV      (!UV / UV) */
758*fe2d066aSRichard Henderson         cond = cond_make_0(TCG_COND_EQ, uv);
759b2167459SRichard Henderson         break;
760*fe2d066aSRichard Henderson     case 5: /* ZNV / VNZ     (!UV | Z / UV & !Z) */
761aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
762*fe2d066aSRichard Henderson         tcg_gen_movcond_i64(TCG_COND_EQ, tmp, uv, ctx->zero, ctx->zero, res);
76382d0c831SRichard Henderson         if (!d) {
7646fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, tmp);
765a751eb31SRichard Henderson         }
766b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
767b2167459SRichard Henderson         break;
768b2167459SRichard Henderson     case 6: /* SV / NSV      (V / !V) */
76982d0c831SRichard Henderson         if (!d) {
770aac0f603SRichard Henderson             tmp = tcg_temp_new_i64();
7716fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(tmp, sv);
772a751eb31SRichard Henderson             sv = tmp;
773a751eb31SRichard Henderson         }
774b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_LT, sv);
775b2167459SRichard Henderson         break;
776b2167459SRichard Henderson     case 7: /* OD / EV */
777aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
7786fd0c7bcSRichard Henderson         tcg_gen_andi_i64(tmp, res, 1);
779b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_NE, tmp);
780b2167459SRichard Henderson         break;
781b2167459SRichard Henderson     default:
782b2167459SRichard Henderson         g_assert_not_reached();
783b2167459SRichard Henderson     }
784b2167459SRichard Henderson     if (cf & 1) {
785b2167459SRichard Henderson         cond.c = tcg_invert_cond(cond.c);
786b2167459SRichard Henderson     }
787b2167459SRichard Henderson 
788b2167459SRichard Henderson     return cond;
789b2167459SRichard Henderson }
790b2167459SRichard Henderson 
791b2167459SRichard Henderson /* Similar, but for the special case of subtraction without borrow, we
792b2167459SRichard Henderson    can use the inputs directly.  This can allow other computation to be
793b2167459SRichard Henderson    deleted as unused.  */
794b2167459SRichard Henderson 
7954fe9533aSRichard Henderson static DisasCond do_sub_cond(DisasContext *ctx, unsigned cf, bool d,
7966fd0c7bcSRichard Henderson                              TCGv_i64 res, TCGv_i64 in1,
7976fd0c7bcSRichard Henderson                              TCGv_i64 in2, TCGv_i64 sv)
798b2167459SRichard Henderson {
7994fe9533aSRichard Henderson     TCGCond tc;
8004fe9533aSRichard Henderson     bool ext_uns;
801b2167459SRichard Henderson 
802b2167459SRichard Henderson     switch (cf >> 1) {
803b2167459SRichard Henderson     case 1: /* = / <> */
8044fe9533aSRichard Henderson         tc = TCG_COND_EQ;
8054fe9533aSRichard Henderson         ext_uns = true;
806b2167459SRichard Henderson         break;
807b2167459SRichard Henderson     case 2: /* < / >= */
8084fe9533aSRichard Henderson         tc = TCG_COND_LT;
8094fe9533aSRichard Henderson         ext_uns = false;
810b2167459SRichard Henderson         break;
811b2167459SRichard Henderson     case 3: /* <= / > */
8124fe9533aSRichard Henderson         tc = TCG_COND_LE;
8134fe9533aSRichard Henderson         ext_uns = false;
814b2167459SRichard Henderson         break;
815b2167459SRichard Henderson     case 4: /* << / >>= */
8164fe9533aSRichard Henderson         tc = TCG_COND_LTU;
8174fe9533aSRichard Henderson         ext_uns = true;
818b2167459SRichard Henderson         break;
819b2167459SRichard Henderson     case 5: /* <<= / >> */
8204fe9533aSRichard Henderson         tc = TCG_COND_LEU;
8214fe9533aSRichard Henderson         ext_uns = true;
822b2167459SRichard Henderson         break;
823b2167459SRichard Henderson     default:
824a751eb31SRichard Henderson         return do_cond(ctx, cf, d, res, NULL, sv);
825b2167459SRichard Henderson     }
826b2167459SRichard Henderson 
8274fe9533aSRichard Henderson     if (cf & 1) {
8284fe9533aSRichard Henderson         tc = tcg_invert_cond(tc);
8294fe9533aSRichard Henderson     }
83082d0c831SRichard Henderson     if (!d) {
831aac0f603SRichard Henderson         TCGv_i64 t1 = tcg_temp_new_i64();
832aac0f603SRichard Henderson         TCGv_i64 t2 = tcg_temp_new_i64();
8334fe9533aSRichard Henderson 
8344fe9533aSRichard Henderson         if (ext_uns) {
8356fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(t1, in1);
8366fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(t2, in2);
8374fe9533aSRichard Henderson         } else {
8386fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(t1, in1);
8396fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(t2, in2);
8404fe9533aSRichard Henderson         }
8414fe9533aSRichard Henderson         return cond_make_tmp(tc, t1, t2);
8424fe9533aSRichard Henderson     }
8434fe9533aSRichard Henderson     return cond_make(tc, in1, in2);
844b2167459SRichard Henderson }
845b2167459SRichard Henderson 
846df0232feSRichard Henderson /*
847df0232feSRichard Henderson  * Similar, but for logicals, where the carry and overflow bits are not
848df0232feSRichard Henderson  * computed, and use of them is undefined.
849df0232feSRichard Henderson  *
850df0232feSRichard Henderson  * Undefined or not, hardware does not trap.  It seems reasonable to
851df0232feSRichard Henderson  * assume hardware treats cases c={4,5,6} as if C=0 & V=0, since that's
852df0232feSRichard Henderson  * how cases c={2,3} are treated.
853df0232feSRichard Henderson  */
854b2167459SRichard Henderson 
855b5af8423SRichard Henderson static DisasCond do_log_cond(DisasContext *ctx, unsigned cf, bool d,
8566fd0c7bcSRichard Henderson                              TCGv_i64 res)
857b2167459SRichard Henderson {
858b5af8423SRichard Henderson     TCGCond tc;
859b5af8423SRichard Henderson     bool ext_uns;
860a751eb31SRichard Henderson 
861df0232feSRichard Henderson     switch (cf) {
862df0232feSRichard Henderson     case 0:  /* never */
863df0232feSRichard Henderson     case 9:  /* undef, C */
864df0232feSRichard Henderson     case 11: /* undef, C & !Z */
865df0232feSRichard Henderson     case 12: /* undef, V */
866df0232feSRichard Henderson         return cond_make_f();
867df0232feSRichard Henderson 
868df0232feSRichard Henderson     case 1:  /* true */
869df0232feSRichard Henderson     case 8:  /* undef, !C */
870df0232feSRichard Henderson     case 10: /* undef, !C | Z */
871df0232feSRichard Henderson     case 13: /* undef, !V */
872df0232feSRichard Henderson         return cond_make_t();
873df0232feSRichard Henderson 
874df0232feSRichard Henderson     case 2:  /* == */
875b5af8423SRichard Henderson         tc = TCG_COND_EQ;
876b5af8423SRichard Henderson         ext_uns = true;
877b5af8423SRichard Henderson         break;
878df0232feSRichard Henderson     case 3:  /* <> */
879b5af8423SRichard Henderson         tc = TCG_COND_NE;
880b5af8423SRichard Henderson         ext_uns = true;
881b5af8423SRichard Henderson         break;
882df0232feSRichard Henderson     case 4:  /* < */
883b5af8423SRichard Henderson         tc = TCG_COND_LT;
884b5af8423SRichard Henderson         ext_uns = false;
885b5af8423SRichard Henderson         break;
886df0232feSRichard Henderson     case 5:  /* >= */
887b5af8423SRichard Henderson         tc = TCG_COND_GE;
888b5af8423SRichard Henderson         ext_uns = false;
889b5af8423SRichard Henderson         break;
890df0232feSRichard Henderson     case 6:  /* <= */
891b5af8423SRichard Henderson         tc = TCG_COND_LE;
892b5af8423SRichard Henderson         ext_uns = false;
893b5af8423SRichard Henderson         break;
894df0232feSRichard Henderson     case 7:  /* > */
895b5af8423SRichard Henderson         tc = TCG_COND_GT;
896b5af8423SRichard Henderson         ext_uns = false;
897b5af8423SRichard Henderson         break;
898df0232feSRichard Henderson 
899df0232feSRichard Henderson     case 14: /* OD */
900df0232feSRichard Henderson     case 15: /* EV */
901a751eb31SRichard Henderson         return do_cond(ctx, cf, d, res, NULL, NULL);
902df0232feSRichard Henderson 
903df0232feSRichard Henderson     default:
904df0232feSRichard Henderson         g_assert_not_reached();
905b2167459SRichard Henderson     }
906b5af8423SRichard Henderson 
90782d0c831SRichard Henderson     if (!d) {
908aac0f603SRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
909b5af8423SRichard Henderson 
910b5af8423SRichard Henderson         if (ext_uns) {
9116fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, res);
912b5af8423SRichard Henderson         } else {
9136fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(tmp, res);
914b5af8423SRichard Henderson         }
915b5af8423SRichard Henderson         return cond_make_0_tmp(tc, tmp);
916b5af8423SRichard Henderson     }
917b5af8423SRichard Henderson     return cond_make_0(tc, res);
918b2167459SRichard Henderson }
919b2167459SRichard Henderson 
92098cd9ca7SRichard Henderson /* Similar, but for shift/extract/deposit conditions.  */
92198cd9ca7SRichard Henderson 
9224fa52edfSRichard Henderson static DisasCond do_sed_cond(DisasContext *ctx, unsigned orig, bool d,
9236fd0c7bcSRichard Henderson                              TCGv_i64 res)
92498cd9ca7SRichard Henderson {
92598cd9ca7SRichard Henderson     unsigned c, f;
92698cd9ca7SRichard Henderson 
92798cd9ca7SRichard Henderson     /* Convert the compressed condition codes to standard.
92898cd9ca7SRichard Henderson        0-2 are the same as logicals (nv,<,<=), while 3 is OD.
92998cd9ca7SRichard Henderson        4-7 are the reverse of 0-3.  */
93098cd9ca7SRichard Henderson     c = orig & 3;
93198cd9ca7SRichard Henderson     if (c == 3) {
93298cd9ca7SRichard Henderson         c = 7;
93398cd9ca7SRichard Henderson     }
93498cd9ca7SRichard Henderson     f = (orig & 4) / 4;
93598cd9ca7SRichard Henderson 
936b5af8423SRichard Henderson     return do_log_cond(ctx, c * 2 + f, d, res);
93798cd9ca7SRichard Henderson }
93898cd9ca7SRichard Henderson 
93946bb3d46SRichard Henderson /* Similar, but for unit zero conditions.  */
94046bb3d46SRichard Henderson static DisasCond do_unit_zero_cond(unsigned cf, bool d, TCGv_i64 res)
941b2167459SRichard Henderson {
94246bb3d46SRichard Henderson     TCGv_i64 tmp;
943c53e401eSRichard Henderson     uint64_t d_repl = d ? 0x0000000100000001ull : 1;
94446bb3d46SRichard Henderson     uint64_t ones = 0, sgns = 0;
945b2167459SRichard Henderson 
946b2167459SRichard Henderson     switch (cf >> 1) {
947578b8132SSven Schnelle     case 1: /* SBW / NBW */
948578b8132SSven Schnelle         if (d) {
94946bb3d46SRichard Henderson             ones = d_repl;
95046bb3d46SRichard Henderson             sgns = d_repl << 31;
951578b8132SSven Schnelle         }
952578b8132SSven Schnelle         break;
953b2167459SRichard Henderson     case 2: /* SBZ / NBZ */
95446bb3d46SRichard Henderson         ones = d_repl * 0x01010101u;
95546bb3d46SRichard Henderson         sgns = ones << 7;
95646bb3d46SRichard Henderson         break;
95746bb3d46SRichard Henderson     case 3: /* SHZ / NHZ */
95846bb3d46SRichard Henderson         ones = d_repl * 0x00010001u;
95946bb3d46SRichard Henderson         sgns = ones << 15;
96046bb3d46SRichard Henderson         break;
96146bb3d46SRichard Henderson     }
96246bb3d46SRichard Henderson     if (ones == 0) {
96346bb3d46SRichard Henderson         /* Undefined, or 0/1 (never/always). */
96446bb3d46SRichard Henderson         return cf & 1 ? cond_make_t() : cond_make_f();
96546bb3d46SRichard Henderson     }
96646bb3d46SRichard Henderson 
96746bb3d46SRichard Henderson     /*
96846bb3d46SRichard Henderson      * See hasless(v,1) from
969b2167459SRichard Henderson      * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
970b2167459SRichard Henderson      */
971aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
97246bb3d46SRichard Henderson     tcg_gen_subi_i64(tmp, res, ones);
9736fd0c7bcSRichard Henderson     tcg_gen_andc_i64(tmp, tmp, res);
97446bb3d46SRichard Henderson     tcg_gen_andi_i64(tmp, tmp, sgns);
975b2167459SRichard Henderson 
97646bb3d46SRichard Henderson     return cond_make_0_tmp(cf & 1 ? TCG_COND_EQ : TCG_COND_NE, tmp);
977b2167459SRichard Henderson }
978b2167459SRichard Henderson 
9796fd0c7bcSRichard Henderson static TCGv_i64 get_carry(DisasContext *ctx, bool d,
9806fd0c7bcSRichard Henderson                           TCGv_i64 cb, TCGv_i64 cb_msb)
98172ca8753SRichard Henderson {
98282d0c831SRichard Henderson     if (!d) {
983aac0f603SRichard Henderson         TCGv_i64 t = tcg_temp_new_i64();
9846fd0c7bcSRichard Henderson         tcg_gen_extract_i64(t, cb, 32, 1);
98572ca8753SRichard Henderson         return t;
98672ca8753SRichard Henderson     }
98772ca8753SRichard Henderson     return cb_msb;
98872ca8753SRichard Henderson }
98972ca8753SRichard Henderson 
9906fd0c7bcSRichard Henderson static TCGv_i64 get_psw_carry(DisasContext *ctx, bool d)
99172ca8753SRichard Henderson {
99272ca8753SRichard Henderson     return get_carry(ctx, d, cpu_psw_cb, cpu_psw_cb_msb);
99372ca8753SRichard Henderson }
99472ca8753SRichard Henderson 
995b2167459SRichard Henderson /* Compute signed overflow for addition.  */
9966fd0c7bcSRichard Henderson static TCGv_i64 do_add_sv(DisasContext *ctx, TCGv_i64 res,
9976fd0c7bcSRichard Henderson                           TCGv_i64 in1, TCGv_i64 in2)
998b2167459SRichard Henderson {
999aac0f603SRichard Henderson     TCGv_i64 sv = tcg_temp_new_i64();
1000aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
1001b2167459SRichard Henderson 
10026fd0c7bcSRichard Henderson     tcg_gen_xor_i64(sv, res, in1);
10036fd0c7bcSRichard Henderson     tcg_gen_xor_i64(tmp, in1, in2);
10046fd0c7bcSRichard Henderson     tcg_gen_andc_i64(sv, sv, tmp);
1005b2167459SRichard Henderson 
1006b2167459SRichard Henderson     return sv;
1007b2167459SRichard Henderson }
1008b2167459SRichard Henderson 
1009b2167459SRichard Henderson /* Compute signed overflow for subtraction.  */
10106fd0c7bcSRichard Henderson static TCGv_i64 do_sub_sv(DisasContext *ctx, TCGv_i64 res,
10116fd0c7bcSRichard Henderson                           TCGv_i64 in1, TCGv_i64 in2)
1012b2167459SRichard Henderson {
1013aac0f603SRichard Henderson     TCGv_i64 sv = tcg_temp_new_i64();
1014aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
1015b2167459SRichard Henderson 
10166fd0c7bcSRichard Henderson     tcg_gen_xor_i64(sv, res, in1);
10176fd0c7bcSRichard Henderson     tcg_gen_xor_i64(tmp, in1, in2);
10186fd0c7bcSRichard Henderson     tcg_gen_and_i64(sv, sv, tmp);
1019b2167459SRichard Henderson 
1020b2167459SRichard Henderson     return sv;
1021b2167459SRichard Henderson }
1022b2167459SRichard Henderson 
10236fd0c7bcSRichard Henderson static void do_add(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
10246fd0c7bcSRichard Henderson                    TCGv_i64 in2, unsigned shift, bool is_l,
1025faf97ba1SRichard Henderson                    bool is_tsv, bool is_tc, bool is_c, unsigned cf, bool d)
1026b2167459SRichard Henderson {
10276fd0c7bcSRichard Henderson     TCGv_i64 dest, cb, cb_msb, cb_cond, sv, tmp;
1028b2167459SRichard Henderson     unsigned c = cf >> 1;
1029b2167459SRichard Henderson     DisasCond cond;
1030b2167459SRichard Henderson 
1031aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
1032f764718dSRichard Henderson     cb = NULL;
1033f764718dSRichard Henderson     cb_msb = NULL;
1034bdcccc17SRichard Henderson     cb_cond = NULL;
1035b2167459SRichard Henderson 
1036b2167459SRichard Henderson     if (shift) {
1037aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
10386fd0c7bcSRichard Henderson         tcg_gen_shli_i64(tmp, in1, shift);
1039b2167459SRichard Henderson         in1 = tmp;
1040b2167459SRichard Henderson     }
1041b2167459SRichard Henderson 
1042b47a4a02SSven Schnelle     if (!is_l || cond_need_cb(c)) {
1043aac0f603SRichard Henderson         cb_msb = tcg_temp_new_i64();
1044aac0f603SRichard Henderson         cb = tcg_temp_new_i64();
1045bdcccc17SRichard Henderson 
1046a4db4a78SRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, in1, ctx->zero, in2, ctx->zero);
1047b2167459SRichard Henderson         if (is_c) {
10486fd0c7bcSRichard Henderson             tcg_gen_add2_i64(dest, cb_msb, dest, cb_msb,
1049a4db4a78SRichard Henderson                              get_psw_carry(ctx, d), ctx->zero);
1050b2167459SRichard Henderson         }
10516fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, in1, in2);
10526fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
1053bdcccc17SRichard Henderson         if (cond_need_cb(c)) {
1054bdcccc17SRichard Henderson             cb_cond = get_carry(ctx, d, cb, cb_msb);
1055b2167459SRichard Henderson         }
1056b2167459SRichard Henderson     } else {
10576fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, in1, in2);
1058b2167459SRichard Henderson         if (is_c) {
10596fd0c7bcSRichard Henderson             tcg_gen_add_i64(dest, dest, get_psw_carry(ctx, d));
1060b2167459SRichard Henderson         }
1061b2167459SRichard Henderson     }
1062b2167459SRichard Henderson 
1063b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1064f764718dSRichard Henderson     sv = NULL;
1065b47a4a02SSven Schnelle     if (is_tsv || cond_need_sv(c)) {
1066b2167459SRichard Henderson         sv = do_add_sv(ctx, dest, in1, in2);
1067b2167459SRichard Henderson         if (is_tsv) {
1068bd1ad92cSSven Schnelle             if (!d) {
1069bd1ad92cSSven Schnelle                 tcg_gen_ext32s_i64(sv, sv);
1070bd1ad92cSSven Schnelle             }
1071b2167459SRichard Henderson             /* ??? Need to include overflow from shift.  */
1072ad75a51eSRichard Henderson             gen_helper_tsv(tcg_env, sv);
1073b2167459SRichard Henderson         }
1074b2167459SRichard Henderson     }
1075b2167459SRichard Henderson 
1076b2167459SRichard Henderson     /* Emit any conditional trap before any writeback.  */
1077a751eb31SRichard Henderson     cond = do_cond(ctx, cf, d, dest, cb_cond, sv);
1078b2167459SRichard Henderson     if (is_tc) {
1079aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
10806fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(cond.c, tmp, cond.a0, cond.a1);
1081ad75a51eSRichard Henderson         gen_helper_tcond(tcg_env, tmp);
1082b2167459SRichard Henderson     }
1083b2167459SRichard Henderson 
1084b2167459SRichard Henderson     /* Write back the result.  */
1085b2167459SRichard Henderson     if (!is_l) {
1086b2167459SRichard Henderson         save_or_nullify(ctx, cpu_psw_cb, cb);
1087b2167459SRichard Henderson         save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1088b2167459SRichard Henderson     }
1089b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1090b2167459SRichard Henderson 
1091b2167459SRichard Henderson     /* Install the new nullification.  */
1092b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1093b2167459SRichard Henderson     ctx->null_cond = cond;
1094b2167459SRichard Henderson }
1095b2167459SRichard Henderson 
1096faf97ba1SRichard Henderson static bool do_add_reg(DisasContext *ctx, arg_rrr_cf_d_sh *a,
10970c982a28SRichard Henderson                        bool is_l, bool is_tsv, bool is_tc, bool is_c)
10980c982a28SRichard Henderson {
10996fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
11000c982a28SRichard Henderson 
11010c982a28SRichard Henderson     if (a->cf) {
11020c982a28SRichard Henderson         nullify_over(ctx);
11030c982a28SRichard Henderson     }
11040c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
11050c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
1106faf97ba1SRichard Henderson     do_add(ctx, a->t, tcg_r1, tcg_r2, a->sh, is_l,
1107faf97ba1SRichard Henderson            is_tsv, is_tc, is_c, a->cf, a->d);
11080c982a28SRichard Henderson     return nullify_end(ctx);
11090c982a28SRichard Henderson }
11100c982a28SRichard Henderson 
11110588e061SRichard Henderson static bool do_add_imm(DisasContext *ctx, arg_rri_cf *a,
11120588e061SRichard Henderson                        bool is_tsv, bool is_tc)
11130588e061SRichard Henderson {
11146fd0c7bcSRichard Henderson     TCGv_i64 tcg_im, tcg_r2;
11150588e061SRichard Henderson 
11160588e061SRichard Henderson     if (a->cf) {
11170588e061SRichard Henderson         nullify_over(ctx);
11180588e061SRichard Henderson     }
11196fd0c7bcSRichard Henderson     tcg_im = tcg_constant_i64(a->i);
11200588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
1121faf97ba1SRichard Henderson     /* All ADDI conditions are 32-bit. */
1122faf97ba1SRichard Henderson     do_add(ctx, a->t, tcg_im, tcg_r2, 0, 0, is_tsv, is_tc, 0, a->cf, false);
11230588e061SRichard Henderson     return nullify_end(ctx);
11240588e061SRichard Henderson }
11250588e061SRichard Henderson 
11266fd0c7bcSRichard Henderson static void do_sub(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
11276fd0c7bcSRichard Henderson                    TCGv_i64 in2, bool is_tsv, bool is_b,
112863c427c6SRichard Henderson                    bool is_tc, unsigned cf, bool d)
1129b2167459SRichard Henderson {
1130a4db4a78SRichard Henderson     TCGv_i64 dest, sv, cb, cb_msb, tmp;
1131b2167459SRichard Henderson     unsigned c = cf >> 1;
1132b2167459SRichard Henderson     DisasCond cond;
1133b2167459SRichard Henderson 
1134aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
1135aac0f603SRichard Henderson     cb = tcg_temp_new_i64();
1136aac0f603SRichard Henderson     cb_msb = tcg_temp_new_i64();
1137b2167459SRichard Henderson 
1138b2167459SRichard Henderson     if (is_b) {
1139b2167459SRichard Henderson         /* DEST,C = IN1 + ~IN2 + C.  */
11406fd0c7bcSRichard Henderson         tcg_gen_not_i64(cb, in2);
1141a4db4a78SRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, in1, ctx->zero,
1142a4db4a78SRichard Henderson                          get_psw_carry(ctx, d), ctx->zero);
1143a4db4a78SRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, dest, cb_msb, cb, ctx->zero);
11446fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, in1);
11456fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
1146b2167459SRichard Henderson     } else {
1147bdcccc17SRichard Henderson         /*
1148bdcccc17SRichard Henderson          * DEST,C = IN1 + ~IN2 + 1.  We can produce the same result in fewer
1149bdcccc17SRichard Henderson          * operations by seeding the high word with 1 and subtracting.
1150bdcccc17SRichard Henderson          */
11516fd0c7bcSRichard Henderson         TCGv_i64 one = tcg_constant_i64(1);
1152a4db4a78SRichard Henderson         tcg_gen_sub2_i64(dest, cb_msb, in1, one, in2, ctx->zero);
11536fd0c7bcSRichard Henderson         tcg_gen_eqv_i64(cb, in1, in2);
11546fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
1155b2167459SRichard Henderson     }
1156b2167459SRichard Henderson 
1157b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1158f764718dSRichard Henderson     sv = NULL;
1159b47a4a02SSven Schnelle     if (is_tsv || cond_need_sv(c)) {
1160b2167459SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
1161b2167459SRichard Henderson         if (is_tsv) {
1162bd1ad92cSSven Schnelle             if (!d) {
1163bd1ad92cSSven Schnelle                 tcg_gen_ext32s_i64(sv, sv);
1164bd1ad92cSSven Schnelle             }
1165ad75a51eSRichard Henderson             gen_helper_tsv(tcg_env, sv);
1166b2167459SRichard Henderson         }
1167b2167459SRichard Henderson     }
1168b2167459SRichard Henderson 
1169b2167459SRichard Henderson     /* Compute the condition.  We cannot use the special case for borrow.  */
1170b2167459SRichard Henderson     if (!is_b) {
11714fe9533aSRichard Henderson         cond = do_sub_cond(ctx, cf, d, dest, in1, in2, sv);
1172b2167459SRichard Henderson     } else {
1173a751eb31SRichard Henderson         cond = do_cond(ctx, cf, d, dest, get_carry(ctx, d, cb, cb_msb), sv);
1174b2167459SRichard Henderson     }
1175b2167459SRichard Henderson 
1176b2167459SRichard Henderson     /* Emit any conditional trap before any writeback.  */
1177b2167459SRichard Henderson     if (is_tc) {
1178aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
11796fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(cond.c, tmp, cond.a0, cond.a1);
1180ad75a51eSRichard Henderson         gen_helper_tcond(tcg_env, tmp);
1181b2167459SRichard Henderson     }
1182b2167459SRichard Henderson 
1183b2167459SRichard Henderson     /* Write back the result.  */
1184b2167459SRichard Henderson     save_or_nullify(ctx, cpu_psw_cb, cb);
1185b2167459SRichard Henderson     save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1186b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1187b2167459SRichard Henderson 
1188b2167459SRichard Henderson     /* Install the new nullification.  */
1189b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1190b2167459SRichard Henderson     ctx->null_cond = cond;
1191b2167459SRichard Henderson }
1192b2167459SRichard Henderson 
119363c427c6SRichard Henderson static bool do_sub_reg(DisasContext *ctx, arg_rrr_cf_d *a,
11940c982a28SRichard Henderson                        bool is_tsv, bool is_b, bool is_tc)
11950c982a28SRichard Henderson {
11966fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
11970c982a28SRichard Henderson 
11980c982a28SRichard Henderson     if (a->cf) {
11990c982a28SRichard Henderson         nullify_over(ctx);
12000c982a28SRichard Henderson     }
12010c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
12020c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
120363c427c6SRichard Henderson     do_sub(ctx, a->t, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, a->cf, a->d);
12040c982a28SRichard Henderson     return nullify_end(ctx);
12050c982a28SRichard Henderson }
12060c982a28SRichard Henderson 
12070588e061SRichard Henderson static bool do_sub_imm(DisasContext *ctx, arg_rri_cf *a, bool is_tsv)
12080588e061SRichard Henderson {
12096fd0c7bcSRichard Henderson     TCGv_i64 tcg_im, tcg_r2;
12100588e061SRichard Henderson 
12110588e061SRichard Henderson     if (a->cf) {
12120588e061SRichard Henderson         nullify_over(ctx);
12130588e061SRichard Henderson     }
12146fd0c7bcSRichard Henderson     tcg_im = tcg_constant_i64(a->i);
12150588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
121663c427c6SRichard Henderson     /* All SUBI conditions are 32-bit. */
121763c427c6SRichard Henderson     do_sub(ctx, a->t, tcg_im, tcg_r2, is_tsv, 0, 0, a->cf, false);
12180588e061SRichard Henderson     return nullify_end(ctx);
12190588e061SRichard Henderson }
12200588e061SRichard Henderson 
12216fd0c7bcSRichard Henderson static void do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
12226fd0c7bcSRichard Henderson                       TCGv_i64 in2, unsigned cf, bool d)
1223b2167459SRichard Henderson {
12246fd0c7bcSRichard Henderson     TCGv_i64 dest, sv;
1225b2167459SRichard Henderson     DisasCond cond;
1226b2167459SRichard Henderson 
1227aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
12286fd0c7bcSRichard Henderson     tcg_gen_sub_i64(dest, in1, in2);
1229b2167459SRichard Henderson 
1230b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1231f764718dSRichard Henderson     sv = NULL;
1232b47a4a02SSven Schnelle     if (cond_need_sv(cf >> 1)) {
1233b2167459SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
1234b2167459SRichard Henderson     }
1235b2167459SRichard Henderson 
1236b2167459SRichard Henderson     /* Form the condition for the compare.  */
12374fe9533aSRichard Henderson     cond = do_sub_cond(ctx, cf, d, dest, in1, in2, sv);
1238b2167459SRichard Henderson 
1239b2167459SRichard Henderson     /* Clear.  */
12406fd0c7bcSRichard Henderson     tcg_gen_movi_i64(dest, 0);
1241b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1242b2167459SRichard Henderson 
1243b2167459SRichard Henderson     /* Install the new nullification.  */
1244b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1245b2167459SRichard Henderson     ctx->null_cond = cond;
1246b2167459SRichard Henderson }
1247b2167459SRichard Henderson 
12486fd0c7bcSRichard Henderson static void do_log(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
12496fd0c7bcSRichard Henderson                    TCGv_i64 in2, unsigned cf, bool d,
12506fd0c7bcSRichard Henderson                    void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
1251b2167459SRichard Henderson {
12526fd0c7bcSRichard Henderson     TCGv_i64 dest = dest_gpr(ctx, rt);
1253b2167459SRichard Henderson 
1254b2167459SRichard Henderson     /* Perform the operation, and writeback.  */
1255b2167459SRichard Henderson     fn(dest, in1, in2);
1256b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1257b2167459SRichard Henderson 
1258b2167459SRichard Henderson     /* Install the new nullification.  */
1259b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1260b2167459SRichard Henderson     if (cf) {
1261b5af8423SRichard Henderson         ctx->null_cond = do_log_cond(ctx, cf, d, dest);
1262b2167459SRichard Henderson     }
1263b2167459SRichard Henderson }
1264b2167459SRichard Henderson 
1265fa8e3bedSRichard Henderson static bool do_log_reg(DisasContext *ctx, arg_rrr_cf_d *a,
12666fd0c7bcSRichard Henderson                        void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
12670c982a28SRichard Henderson {
12686fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
12690c982a28SRichard Henderson 
12700c982a28SRichard Henderson     if (a->cf) {
12710c982a28SRichard Henderson         nullify_over(ctx);
12720c982a28SRichard Henderson     }
12730c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
12740c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
1275fa8e3bedSRichard Henderson     do_log(ctx, a->t, tcg_r1, tcg_r2, a->cf, a->d, fn);
12760c982a28SRichard Henderson     return nullify_end(ctx);
12770c982a28SRichard Henderson }
12780c982a28SRichard Henderson 
127946bb3d46SRichard Henderson static void do_unit_addsub(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
128046bb3d46SRichard Henderson                            TCGv_i64 in2, unsigned cf, bool d,
128146bb3d46SRichard Henderson                            bool is_tc, bool is_add)
1282b2167459SRichard Henderson {
128346bb3d46SRichard Henderson     TCGv_i64 dest = tcg_temp_new_i64();
128446bb3d46SRichard Henderson     uint64_t test_cb = 0;
1285b2167459SRichard Henderson     DisasCond cond;
1286b2167459SRichard Henderson 
128746bb3d46SRichard Henderson     /* Select which carry-out bits to test. */
128846bb3d46SRichard Henderson     switch (cf >> 1) {
128946bb3d46SRichard Henderson     case 4: /* NDC / SDC -- 4-bit carries */
129046bb3d46SRichard Henderson         test_cb = dup_const(MO_8, 0x88);
129146bb3d46SRichard Henderson         break;
129246bb3d46SRichard Henderson     case 5: /* NWC / SWC -- 32-bit carries */
129346bb3d46SRichard Henderson         if (d) {
129446bb3d46SRichard Henderson             test_cb = dup_const(MO_32, INT32_MIN);
1295b2167459SRichard Henderson         } else {
129646bb3d46SRichard Henderson             cf &= 1; /* undefined -- map to never/always */
129746bb3d46SRichard Henderson         }
129846bb3d46SRichard Henderson         break;
129946bb3d46SRichard Henderson     case 6: /* NBC / SBC -- 8-bit carries */
130046bb3d46SRichard Henderson         test_cb = dup_const(MO_8, INT8_MIN);
130146bb3d46SRichard Henderson         break;
130246bb3d46SRichard Henderson     case 7: /* NHC / SHC -- 16-bit carries */
130346bb3d46SRichard Henderson         test_cb = dup_const(MO_16, INT16_MIN);
130446bb3d46SRichard Henderson         break;
130546bb3d46SRichard Henderson     }
130646bb3d46SRichard Henderson     if (!d) {
130746bb3d46SRichard Henderson         test_cb = (uint32_t)test_cb;
130846bb3d46SRichard Henderson     }
1309b2167459SRichard Henderson 
131046bb3d46SRichard Henderson     if (!test_cb) {
131146bb3d46SRichard Henderson         /* No need to compute carries if we don't need to test them. */
131246bb3d46SRichard Henderson         if (is_add) {
131346bb3d46SRichard Henderson             tcg_gen_add_i64(dest, in1, in2);
131446bb3d46SRichard Henderson         } else {
131546bb3d46SRichard Henderson             tcg_gen_sub_i64(dest, in1, in2);
131646bb3d46SRichard Henderson         }
131746bb3d46SRichard Henderson         cond = do_unit_zero_cond(cf, d, dest);
131846bb3d46SRichard Henderson     } else {
131946bb3d46SRichard Henderson         TCGv_i64 cb = tcg_temp_new_i64();
132046bb3d46SRichard Henderson 
132146bb3d46SRichard Henderson         if (d) {
132246bb3d46SRichard Henderson             TCGv_i64 cb_msb = tcg_temp_new_i64();
132346bb3d46SRichard Henderson             if (is_add) {
132446bb3d46SRichard Henderson                 tcg_gen_add2_i64(dest, cb_msb, in1, ctx->zero, in2, ctx->zero);
132546bb3d46SRichard Henderson                 tcg_gen_xor_i64(cb, in1, in2);
132646bb3d46SRichard Henderson             } else {
132746bb3d46SRichard Henderson                 /* See do_sub, !is_b. */
132846bb3d46SRichard Henderson                 TCGv_i64 one = tcg_constant_i64(1);
132946bb3d46SRichard Henderson                 tcg_gen_sub2_i64(dest, cb_msb, in1, one, in2, ctx->zero);
133046bb3d46SRichard Henderson                 tcg_gen_eqv_i64(cb, in1, in2);
133146bb3d46SRichard Henderson             }
133246bb3d46SRichard Henderson             tcg_gen_xor_i64(cb, cb, dest);
133346bb3d46SRichard Henderson             tcg_gen_extract2_i64(cb, cb, cb_msb, 1);
133446bb3d46SRichard Henderson         } else {
133546bb3d46SRichard Henderson             if (is_add) {
133646bb3d46SRichard Henderson                 tcg_gen_add_i64(dest, in1, in2);
133746bb3d46SRichard Henderson                 tcg_gen_xor_i64(cb, in1, in2);
133846bb3d46SRichard Henderson             } else {
133946bb3d46SRichard Henderson                 tcg_gen_sub_i64(dest, in1, in2);
134046bb3d46SRichard Henderson                 tcg_gen_eqv_i64(cb, in1, in2);
134146bb3d46SRichard Henderson             }
134246bb3d46SRichard Henderson             tcg_gen_xor_i64(cb, cb, dest);
134346bb3d46SRichard Henderson             tcg_gen_shri_i64(cb, cb, 1);
134446bb3d46SRichard Henderson         }
134546bb3d46SRichard Henderson 
134646bb3d46SRichard Henderson         tcg_gen_andi_i64(cb, cb, test_cb);
134746bb3d46SRichard Henderson         cond = cond_make_0_tmp(cf & 1 ? TCG_COND_EQ : TCG_COND_NE, cb);
134846bb3d46SRichard Henderson     }
1349b2167459SRichard Henderson 
1350b2167459SRichard Henderson     if (is_tc) {
1351aac0f603SRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
13526fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(cond.c, tmp, cond.a0, cond.a1);
1353ad75a51eSRichard Henderson         gen_helper_tcond(tcg_env, tmp);
1354b2167459SRichard Henderson     }
1355b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1356b2167459SRichard Henderson 
1357b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1358b2167459SRichard Henderson     ctx->null_cond = cond;
1359b2167459SRichard Henderson }
1360b2167459SRichard Henderson 
136186f8d05fSRichard Henderson #ifndef CONFIG_USER_ONLY
13628d6ae7fbSRichard Henderson /* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
13638d6ae7fbSRichard Henderson    from the top 2 bits of the base register.  There are a few system
13648d6ae7fbSRichard Henderson    instructions that have a 3-bit space specifier, for which SR0 is
13658d6ae7fbSRichard Henderson    not special.  To handle this, pass ~SP.  */
13666fd0c7bcSRichard Henderson static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_i64 base)
136786f8d05fSRichard Henderson {
136886f8d05fSRichard Henderson     TCGv_ptr ptr;
13696fd0c7bcSRichard Henderson     TCGv_i64 tmp;
137086f8d05fSRichard Henderson     TCGv_i64 spc;
137186f8d05fSRichard Henderson 
137286f8d05fSRichard Henderson     if (sp != 0) {
13738d6ae7fbSRichard Henderson         if (sp < 0) {
13748d6ae7fbSRichard Henderson             sp = ~sp;
13758d6ae7fbSRichard Henderson         }
13766fd0c7bcSRichard Henderson         spc = tcg_temp_new_i64();
13778d6ae7fbSRichard Henderson         load_spr(ctx, spc, sp);
13788d6ae7fbSRichard Henderson         return spc;
137986f8d05fSRichard Henderson     }
1380494737b7SRichard Henderson     if (ctx->tb_flags & TB_FLAG_SR_SAME) {
1381494737b7SRichard Henderson         return cpu_srH;
1382494737b7SRichard Henderson     }
138386f8d05fSRichard Henderson 
138486f8d05fSRichard Henderson     ptr = tcg_temp_new_ptr();
1385aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
13866fd0c7bcSRichard Henderson     spc = tcg_temp_new_i64();
138786f8d05fSRichard Henderson 
1388698240d1SRichard Henderson     /* Extract top 2 bits of the address, shift left 3 for uint64_t index. */
13896fd0c7bcSRichard Henderson     tcg_gen_shri_i64(tmp, base, (ctx->tb_flags & PSW_W ? 64 : 32) - 5);
13906fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, 030);
13916fd0c7bcSRichard Henderson     tcg_gen_trunc_i64_ptr(ptr, tmp);
139286f8d05fSRichard Henderson 
1393ad75a51eSRichard Henderson     tcg_gen_add_ptr(ptr, ptr, tcg_env);
139486f8d05fSRichard Henderson     tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
139586f8d05fSRichard Henderson 
139686f8d05fSRichard Henderson     return spc;
139786f8d05fSRichard Henderson }
139886f8d05fSRichard Henderson #endif
139986f8d05fSRichard Henderson 
14006fd0c7bcSRichard Henderson static void form_gva(DisasContext *ctx, TCGv_i64 *pgva, TCGv_i64 *pofs,
1401c53e401eSRichard Henderson                      unsigned rb, unsigned rx, int scale, int64_t disp,
140286f8d05fSRichard Henderson                      unsigned sp, int modify, bool is_phys)
140386f8d05fSRichard Henderson {
14046fd0c7bcSRichard Henderson     TCGv_i64 base = load_gpr(ctx, rb);
14056fd0c7bcSRichard Henderson     TCGv_i64 ofs;
14066fd0c7bcSRichard Henderson     TCGv_i64 addr;
140786f8d05fSRichard Henderson 
1408f5b5c857SRichard Henderson     set_insn_breg(ctx, rb);
1409f5b5c857SRichard Henderson 
141086f8d05fSRichard Henderson     /* Note that RX is mutually exclusive with DISP.  */
141186f8d05fSRichard Henderson     if (rx) {
1412aac0f603SRichard Henderson         ofs = tcg_temp_new_i64();
14136fd0c7bcSRichard Henderson         tcg_gen_shli_i64(ofs, cpu_gr[rx], scale);
14146fd0c7bcSRichard Henderson         tcg_gen_add_i64(ofs, ofs, base);
141586f8d05fSRichard Henderson     } else if (disp || modify) {
1416aac0f603SRichard Henderson         ofs = tcg_temp_new_i64();
14176fd0c7bcSRichard Henderson         tcg_gen_addi_i64(ofs, base, disp);
141886f8d05fSRichard Henderson     } else {
141986f8d05fSRichard Henderson         ofs = base;
142086f8d05fSRichard Henderson     }
142186f8d05fSRichard Henderson 
142286f8d05fSRichard Henderson     *pofs = ofs;
14236fd0c7bcSRichard Henderson     *pgva = addr = tcg_temp_new_i64();
14247d50b696SSven Schnelle     tcg_gen_andi_i64(addr, modify <= 0 ? ofs : base,
14257d50b696SSven Schnelle                      gva_offset_mask(ctx->tb_flags));
1426698240d1SRichard Henderson #ifndef CONFIG_USER_ONLY
142786f8d05fSRichard Henderson     if (!is_phys) {
1428d265360fSRichard Henderson         tcg_gen_or_i64(addr, addr, space_select(ctx, sp, base));
142986f8d05fSRichard Henderson     }
143086f8d05fSRichard Henderson #endif
143186f8d05fSRichard Henderson }
143286f8d05fSRichard Henderson 
143396d6407fSRichard Henderson /* Emit a memory load.  The modify parameter should be
143496d6407fSRichard Henderson  * < 0 for pre-modify,
143596d6407fSRichard Henderson  * > 0 for post-modify,
143696d6407fSRichard Henderson  * = 0 for no base register update.
143796d6407fSRichard Henderson  */
143896d6407fSRichard Henderson static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1439c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
144014776ab5STony Nguyen                        unsigned sp, int modify, MemOp mop)
144196d6407fSRichard Henderson {
14426fd0c7bcSRichard Henderson     TCGv_i64 ofs;
14436fd0c7bcSRichard Henderson     TCGv_i64 addr;
144496d6407fSRichard Henderson 
144596d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
144696d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
144796d6407fSRichard Henderson 
144886f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
144917fe594cSRichard Henderson              MMU_DISABLED(ctx));
1450c1f55d97SRichard Henderson     tcg_gen_qemu_ld_i32(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
145186f8d05fSRichard Henderson     if (modify) {
145286f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
145396d6407fSRichard Henderson     }
145496d6407fSRichard Henderson }
145596d6407fSRichard Henderson 
145696d6407fSRichard Henderson static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1457c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
145814776ab5STony Nguyen                        unsigned sp, int modify, MemOp mop)
145996d6407fSRichard Henderson {
14606fd0c7bcSRichard Henderson     TCGv_i64 ofs;
14616fd0c7bcSRichard Henderson     TCGv_i64 addr;
146296d6407fSRichard Henderson 
146396d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
146496d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
146596d6407fSRichard Henderson 
146686f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
146717fe594cSRichard Henderson              MMU_DISABLED(ctx));
1468217d1a5eSRichard Henderson     tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
146986f8d05fSRichard Henderson     if (modify) {
147086f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
147196d6407fSRichard Henderson     }
147296d6407fSRichard Henderson }
147396d6407fSRichard Henderson 
147496d6407fSRichard Henderson static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1475c53e401eSRichard Henderson                         unsigned rx, int scale, int64_t disp,
147614776ab5STony Nguyen                         unsigned sp, int modify, MemOp mop)
147796d6407fSRichard Henderson {
14786fd0c7bcSRichard Henderson     TCGv_i64 ofs;
14796fd0c7bcSRichard Henderson     TCGv_i64 addr;
148096d6407fSRichard Henderson 
148196d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
148296d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
148396d6407fSRichard Henderson 
148486f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
148517fe594cSRichard Henderson              MMU_DISABLED(ctx));
1486217d1a5eSRichard Henderson     tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
148786f8d05fSRichard Henderson     if (modify) {
148886f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
148996d6407fSRichard Henderson     }
149096d6407fSRichard Henderson }
149196d6407fSRichard Henderson 
149296d6407fSRichard Henderson static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1493c53e401eSRichard Henderson                         unsigned rx, int scale, int64_t disp,
149414776ab5STony Nguyen                         unsigned sp, int modify, MemOp mop)
149596d6407fSRichard Henderson {
14966fd0c7bcSRichard Henderson     TCGv_i64 ofs;
14976fd0c7bcSRichard Henderson     TCGv_i64 addr;
149896d6407fSRichard Henderson 
149996d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
150096d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
150196d6407fSRichard Henderson 
150286f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
150317fe594cSRichard Henderson              MMU_DISABLED(ctx));
1504217d1a5eSRichard Henderson     tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
150586f8d05fSRichard Henderson     if (modify) {
150686f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
150796d6407fSRichard Henderson     }
150896d6407fSRichard Henderson }
150996d6407fSRichard Henderson 
15101cd012a5SRichard Henderson static bool do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1511c53e401eSRichard Henderson                     unsigned rx, int scale, int64_t disp,
151214776ab5STony Nguyen                     unsigned sp, int modify, MemOp mop)
151396d6407fSRichard Henderson {
15146fd0c7bcSRichard Henderson     TCGv_i64 dest;
151596d6407fSRichard Henderson 
151696d6407fSRichard Henderson     nullify_over(ctx);
151796d6407fSRichard Henderson 
151896d6407fSRichard Henderson     if (modify == 0) {
151996d6407fSRichard Henderson         /* No base register update.  */
152096d6407fSRichard Henderson         dest = dest_gpr(ctx, rt);
152196d6407fSRichard Henderson     } else {
152296d6407fSRichard Henderson         /* Make sure if RT == RB, we see the result of the load.  */
1523aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
152496d6407fSRichard Henderson     }
15256fd0c7bcSRichard Henderson     do_load_64(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
152696d6407fSRichard Henderson     save_gpr(ctx, rt, dest);
152796d6407fSRichard Henderson 
15281cd012a5SRichard Henderson     return nullify_end(ctx);
152996d6407fSRichard Henderson }
153096d6407fSRichard Henderson 
1531740038d7SRichard Henderson static bool do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1532c53e401eSRichard Henderson                       unsigned rx, int scale, int64_t disp,
153386f8d05fSRichard Henderson                       unsigned sp, int modify)
153496d6407fSRichard Henderson {
153596d6407fSRichard Henderson     TCGv_i32 tmp;
153696d6407fSRichard Henderson 
153796d6407fSRichard Henderson     nullify_over(ctx);
153896d6407fSRichard Henderson 
153996d6407fSRichard Henderson     tmp = tcg_temp_new_i32();
154086f8d05fSRichard Henderson     do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
154196d6407fSRichard Henderson     save_frw_i32(rt, tmp);
154296d6407fSRichard Henderson 
154396d6407fSRichard Henderson     if (rt == 0) {
1544ad75a51eSRichard Henderson         gen_helper_loaded_fr0(tcg_env);
154596d6407fSRichard Henderson     }
154696d6407fSRichard Henderson 
1547740038d7SRichard Henderson     return nullify_end(ctx);
154896d6407fSRichard Henderson }
154996d6407fSRichard Henderson 
1550740038d7SRichard Henderson static bool trans_fldw(DisasContext *ctx, arg_ldst *a)
1551740038d7SRichard Henderson {
1552740038d7SRichard Henderson     return do_floadw(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1553740038d7SRichard Henderson                      a->disp, a->sp, a->m);
1554740038d7SRichard Henderson }
1555740038d7SRichard Henderson 
1556740038d7SRichard Henderson static bool do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1557c53e401eSRichard Henderson                       unsigned rx, int scale, int64_t disp,
155886f8d05fSRichard Henderson                       unsigned sp, int modify)
155996d6407fSRichard Henderson {
156096d6407fSRichard Henderson     TCGv_i64 tmp;
156196d6407fSRichard Henderson 
156296d6407fSRichard Henderson     nullify_over(ctx);
156396d6407fSRichard Henderson 
156496d6407fSRichard Henderson     tmp = tcg_temp_new_i64();
1565fc313c64SFrédéric Pétrot     do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
156696d6407fSRichard Henderson     save_frd(rt, tmp);
156796d6407fSRichard Henderson 
156896d6407fSRichard Henderson     if (rt == 0) {
1569ad75a51eSRichard Henderson         gen_helper_loaded_fr0(tcg_env);
157096d6407fSRichard Henderson     }
157196d6407fSRichard Henderson 
1572740038d7SRichard Henderson     return nullify_end(ctx);
1573740038d7SRichard Henderson }
1574740038d7SRichard Henderson 
1575740038d7SRichard Henderson static bool trans_fldd(DisasContext *ctx, arg_ldst *a)
1576740038d7SRichard Henderson {
1577740038d7SRichard Henderson     return do_floadd(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1578740038d7SRichard Henderson                      a->disp, a->sp, a->m);
157996d6407fSRichard Henderson }
158096d6407fSRichard Henderson 
15811cd012a5SRichard Henderson static bool do_store(DisasContext *ctx, unsigned rt, unsigned rb,
1582c53e401eSRichard Henderson                      int64_t disp, unsigned sp,
158314776ab5STony Nguyen                      int modify, MemOp mop)
158496d6407fSRichard Henderson {
158596d6407fSRichard Henderson     nullify_over(ctx);
15866fd0c7bcSRichard Henderson     do_store_64(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
15871cd012a5SRichard Henderson     return nullify_end(ctx);
158896d6407fSRichard Henderson }
158996d6407fSRichard Henderson 
1590740038d7SRichard Henderson static bool do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1591c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
159286f8d05fSRichard Henderson                        unsigned sp, int modify)
159396d6407fSRichard Henderson {
159496d6407fSRichard Henderson     TCGv_i32 tmp;
159596d6407fSRichard Henderson 
159696d6407fSRichard Henderson     nullify_over(ctx);
159796d6407fSRichard Henderson 
159896d6407fSRichard Henderson     tmp = load_frw_i32(rt);
159986f8d05fSRichard Henderson     do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
160096d6407fSRichard Henderson 
1601740038d7SRichard Henderson     return nullify_end(ctx);
160296d6407fSRichard Henderson }
160396d6407fSRichard Henderson 
1604740038d7SRichard Henderson static bool trans_fstw(DisasContext *ctx, arg_ldst *a)
1605740038d7SRichard Henderson {
1606740038d7SRichard Henderson     return do_fstorew(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1607740038d7SRichard Henderson                       a->disp, a->sp, a->m);
1608740038d7SRichard Henderson }
1609740038d7SRichard Henderson 
1610740038d7SRichard Henderson static bool do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1611c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
161286f8d05fSRichard Henderson                        unsigned sp, int modify)
161396d6407fSRichard Henderson {
161496d6407fSRichard Henderson     TCGv_i64 tmp;
161596d6407fSRichard Henderson 
161696d6407fSRichard Henderson     nullify_over(ctx);
161796d6407fSRichard Henderson 
161896d6407fSRichard Henderson     tmp = load_frd(rt);
1619fc313c64SFrédéric Pétrot     do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
162096d6407fSRichard Henderson 
1621740038d7SRichard Henderson     return nullify_end(ctx);
1622740038d7SRichard Henderson }
1623740038d7SRichard Henderson 
1624740038d7SRichard Henderson static bool trans_fstd(DisasContext *ctx, arg_ldst *a)
1625740038d7SRichard Henderson {
1626740038d7SRichard Henderson     return do_fstored(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1627740038d7SRichard Henderson                       a->disp, a->sp, a->m);
162896d6407fSRichard Henderson }
162996d6407fSRichard Henderson 
16301ca74648SRichard Henderson static bool do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
1631ebe9383cSRichard Henderson                        void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1632ebe9383cSRichard Henderson {
1633ebe9383cSRichard Henderson     TCGv_i32 tmp;
1634ebe9383cSRichard Henderson 
1635ebe9383cSRichard Henderson     nullify_over(ctx);
1636ebe9383cSRichard Henderson     tmp = load_frw0_i32(ra);
1637ebe9383cSRichard Henderson 
1638ad75a51eSRichard Henderson     func(tmp, tcg_env, tmp);
1639ebe9383cSRichard Henderson 
1640ebe9383cSRichard Henderson     save_frw_i32(rt, tmp);
16411ca74648SRichard Henderson     return nullify_end(ctx);
1642ebe9383cSRichard Henderson }
1643ebe9383cSRichard Henderson 
16441ca74648SRichard Henderson static bool do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
1645ebe9383cSRichard Henderson                        void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1646ebe9383cSRichard Henderson {
1647ebe9383cSRichard Henderson     TCGv_i32 dst;
1648ebe9383cSRichard Henderson     TCGv_i64 src;
1649ebe9383cSRichard Henderson 
1650ebe9383cSRichard Henderson     nullify_over(ctx);
1651ebe9383cSRichard Henderson     src = load_frd(ra);
1652ebe9383cSRichard Henderson     dst = tcg_temp_new_i32();
1653ebe9383cSRichard Henderson 
1654ad75a51eSRichard Henderson     func(dst, tcg_env, src);
1655ebe9383cSRichard Henderson 
1656ebe9383cSRichard Henderson     save_frw_i32(rt, dst);
16571ca74648SRichard Henderson     return nullify_end(ctx);
1658ebe9383cSRichard Henderson }
1659ebe9383cSRichard Henderson 
16601ca74648SRichard Henderson static bool do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
1661ebe9383cSRichard Henderson                        void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1662ebe9383cSRichard Henderson {
1663ebe9383cSRichard Henderson     TCGv_i64 tmp;
1664ebe9383cSRichard Henderson 
1665ebe9383cSRichard Henderson     nullify_over(ctx);
1666ebe9383cSRichard Henderson     tmp = load_frd0(ra);
1667ebe9383cSRichard Henderson 
1668ad75a51eSRichard Henderson     func(tmp, tcg_env, tmp);
1669ebe9383cSRichard Henderson 
1670ebe9383cSRichard Henderson     save_frd(rt, tmp);
16711ca74648SRichard Henderson     return nullify_end(ctx);
1672ebe9383cSRichard Henderson }
1673ebe9383cSRichard Henderson 
16741ca74648SRichard Henderson static bool do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
1675ebe9383cSRichard Henderson                        void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1676ebe9383cSRichard Henderson {
1677ebe9383cSRichard Henderson     TCGv_i32 src;
1678ebe9383cSRichard Henderson     TCGv_i64 dst;
1679ebe9383cSRichard Henderson 
1680ebe9383cSRichard Henderson     nullify_over(ctx);
1681ebe9383cSRichard Henderson     src = load_frw0_i32(ra);
1682ebe9383cSRichard Henderson     dst = tcg_temp_new_i64();
1683ebe9383cSRichard Henderson 
1684ad75a51eSRichard Henderson     func(dst, tcg_env, src);
1685ebe9383cSRichard Henderson 
1686ebe9383cSRichard Henderson     save_frd(rt, dst);
16871ca74648SRichard Henderson     return nullify_end(ctx);
1688ebe9383cSRichard Henderson }
1689ebe9383cSRichard Henderson 
16901ca74648SRichard Henderson static bool do_fop_weww(DisasContext *ctx, unsigned rt,
1691ebe9383cSRichard Henderson                         unsigned ra, unsigned rb,
169231234768SRichard Henderson                         void (*func)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32))
1693ebe9383cSRichard Henderson {
1694ebe9383cSRichard Henderson     TCGv_i32 a, b;
1695ebe9383cSRichard Henderson 
1696ebe9383cSRichard Henderson     nullify_over(ctx);
1697ebe9383cSRichard Henderson     a = load_frw0_i32(ra);
1698ebe9383cSRichard Henderson     b = load_frw0_i32(rb);
1699ebe9383cSRichard Henderson 
1700ad75a51eSRichard Henderson     func(a, tcg_env, a, b);
1701ebe9383cSRichard Henderson 
1702ebe9383cSRichard Henderson     save_frw_i32(rt, a);
17031ca74648SRichard Henderson     return nullify_end(ctx);
1704ebe9383cSRichard Henderson }
1705ebe9383cSRichard Henderson 
17061ca74648SRichard Henderson static bool do_fop_dedd(DisasContext *ctx, unsigned rt,
1707ebe9383cSRichard Henderson                         unsigned ra, unsigned rb,
170831234768SRichard Henderson                         void (*func)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1709ebe9383cSRichard Henderson {
1710ebe9383cSRichard Henderson     TCGv_i64 a, b;
1711ebe9383cSRichard Henderson 
1712ebe9383cSRichard Henderson     nullify_over(ctx);
1713ebe9383cSRichard Henderson     a = load_frd0(ra);
1714ebe9383cSRichard Henderson     b = load_frd0(rb);
1715ebe9383cSRichard Henderson 
1716ad75a51eSRichard Henderson     func(a, tcg_env, a, b);
1717ebe9383cSRichard Henderson 
1718ebe9383cSRichard Henderson     save_frd(rt, a);
17191ca74648SRichard Henderson     return nullify_end(ctx);
1720ebe9383cSRichard Henderson }
1721ebe9383cSRichard Henderson 
172298cd9ca7SRichard Henderson /* Emit an unconditional branch to a direct target, which may or may not
172398cd9ca7SRichard Henderson    have already had nullification handled.  */
1724c53e401eSRichard Henderson static bool do_dbranch(DisasContext *ctx, uint64_t dest,
172598cd9ca7SRichard Henderson                        unsigned link, bool is_n)
172698cd9ca7SRichard Henderson {
172798cd9ca7SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
172898cd9ca7SRichard Henderson         if (link != 0) {
1729741322f4SRichard Henderson             copy_iaoq_entry(ctx, cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
173098cd9ca7SRichard Henderson         }
173198cd9ca7SRichard Henderson         ctx->iaoq_n = dest;
173298cd9ca7SRichard Henderson         if (is_n) {
173398cd9ca7SRichard Henderson             ctx->null_cond.c = TCG_COND_ALWAYS;
173498cd9ca7SRichard Henderson         }
173598cd9ca7SRichard Henderson     } else {
173698cd9ca7SRichard Henderson         nullify_over(ctx);
173798cd9ca7SRichard Henderson 
173898cd9ca7SRichard Henderson         if (link != 0) {
1739741322f4SRichard Henderson             copy_iaoq_entry(ctx, cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
174098cd9ca7SRichard Henderson         }
174198cd9ca7SRichard Henderson 
174298cd9ca7SRichard Henderson         if (is_n && use_nullify_skip(ctx)) {
174398cd9ca7SRichard Henderson             nullify_set(ctx, 0);
174498cd9ca7SRichard Henderson             gen_goto_tb(ctx, 0, dest, dest + 4);
174598cd9ca7SRichard Henderson         } else {
174698cd9ca7SRichard Henderson             nullify_set(ctx, is_n);
174798cd9ca7SRichard Henderson             gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
174898cd9ca7SRichard Henderson         }
174998cd9ca7SRichard Henderson 
175031234768SRichard Henderson         nullify_end(ctx);
175198cd9ca7SRichard Henderson 
175298cd9ca7SRichard Henderson         nullify_set(ctx, 0);
175398cd9ca7SRichard Henderson         gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
175431234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
175598cd9ca7SRichard Henderson     }
175601afb7beSRichard Henderson     return true;
175798cd9ca7SRichard Henderson }
175898cd9ca7SRichard Henderson 
175998cd9ca7SRichard Henderson /* Emit a conditional branch to a direct target.  If the branch itself
176098cd9ca7SRichard Henderson    is nullified, we should have already used nullify_over.  */
1761c53e401eSRichard Henderson static bool do_cbranch(DisasContext *ctx, int64_t disp, bool is_n,
176298cd9ca7SRichard Henderson                        DisasCond *cond)
176398cd9ca7SRichard Henderson {
1764c53e401eSRichard Henderson     uint64_t dest = iaoq_dest(ctx, disp);
176598cd9ca7SRichard Henderson     TCGLabel *taken = NULL;
176698cd9ca7SRichard Henderson     TCGCond c = cond->c;
176798cd9ca7SRichard Henderson     bool n;
176898cd9ca7SRichard Henderson 
176998cd9ca7SRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
177098cd9ca7SRichard Henderson 
177198cd9ca7SRichard Henderson     /* Handle TRUE and NEVER as direct branches.  */
177298cd9ca7SRichard Henderson     if (c == TCG_COND_ALWAYS) {
177301afb7beSRichard Henderson         return do_dbranch(ctx, dest, 0, is_n && disp >= 0);
177498cd9ca7SRichard Henderson     }
177598cd9ca7SRichard Henderson     if (c == TCG_COND_NEVER) {
177601afb7beSRichard Henderson         return do_dbranch(ctx, ctx->iaoq_n, 0, is_n && disp < 0);
177798cd9ca7SRichard Henderson     }
177898cd9ca7SRichard Henderson 
177998cd9ca7SRichard Henderson     taken = gen_new_label();
17806fd0c7bcSRichard Henderson     tcg_gen_brcond_i64(c, cond->a0, cond->a1, taken);
178198cd9ca7SRichard Henderson     cond_free(cond);
178298cd9ca7SRichard Henderson 
178398cd9ca7SRichard Henderson     /* Not taken: Condition not satisfied; nullify on backward branches. */
178498cd9ca7SRichard Henderson     n = is_n && disp < 0;
178598cd9ca7SRichard Henderson     if (n && use_nullify_skip(ctx)) {
178698cd9ca7SRichard Henderson         nullify_set(ctx, 0);
1787a881c8e7SRichard Henderson         gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
178898cd9ca7SRichard Henderson     } else {
178998cd9ca7SRichard Henderson         if (!n && ctx->null_lab) {
179098cd9ca7SRichard Henderson             gen_set_label(ctx->null_lab);
179198cd9ca7SRichard Henderson             ctx->null_lab = NULL;
179298cd9ca7SRichard Henderson         }
179398cd9ca7SRichard Henderson         nullify_set(ctx, n);
1794c301f34eSRichard Henderson         if (ctx->iaoq_n == -1) {
1795c301f34eSRichard Henderson             /* The temporary iaoq_n_var died at the branch above.
1796c301f34eSRichard Henderson                Regenerate it here instead of saving it.  */
17976fd0c7bcSRichard Henderson             tcg_gen_addi_i64(ctx->iaoq_n_var, cpu_iaoq_b, 4);
1798c301f34eSRichard Henderson         }
1799a881c8e7SRichard Henderson         gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
180098cd9ca7SRichard Henderson     }
180198cd9ca7SRichard Henderson 
180298cd9ca7SRichard Henderson     gen_set_label(taken);
180398cd9ca7SRichard Henderson 
180498cd9ca7SRichard Henderson     /* Taken: Condition satisfied; nullify on forward branches.  */
180598cd9ca7SRichard Henderson     n = is_n && disp >= 0;
180698cd9ca7SRichard Henderson     if (n && use_nullify_skip(ctx)) {
180798cd9ca7SRichard Henderson         nullify_set(ctx, 0);
1808a881c8e7SRichard Henderson         gen_goto_tb(ctx, 1, dest, dest + 4);
180998cd9ca7SRichard Henderson     } else {
181098cd9ca7SRichard Henderson         nullify_set(ctx, n);
1811a881c8e7SRichard Henderson         gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
181298cd9ca7SRichard Henderson     }
181398cd9ca7SRichard Henderson 
181498cd9ca7SRichard Henderson     /* Not taken: the branch itself was nullified.  */
181598cd9ca7SRichard Henderson     if (ctx->null_lab) {
181698cd9ca7SRichard Henderson         gen_set_label(ctx->null_lab);
181798cd9ca7SRichard Henderson         ctx->null_lab = NULL;
181831234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
181998cd9ca7SRichard Henderson     } else {
182031234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
182198cd9ca7SRichard Henderson     }
182201afb7beSRichard Henderson     return true;
182398cd9ca7SRichard Henderson }
182498cd9ca7SRichard Henderson 
182598cd9ca7SRichard Henderson /* Emit an unconditional branch to an indirect target.  This handles
182698cd9ca7SRichard Henderson    nullification of the branch itself.  */
18276fd0c7bcSRichard Henderson static bool do_ibranch(DisasContext *ctx, TCGv_i64 dest,
182898cd9ca7SRichard Henderson                        unsigned link, bool is_n)
182998cd9ca7SRichard Henderson {
18306fd0c7bcSRichard Henderson     TCGv_i64 a0, a1, next, tmp;
183198cd9ca7SRichard Henderson     TCGCond c;
183298cd9ca7SRichard Henderson 
183398cd9ca7SRichard Henderson     assert(ctx->null_lab == NULL);
183498cd9ca7SRichard Henderson 
183598cd9ca7SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER) {
183698cd9ca7SRichard Henderson         if (link != 0) {
1837741322f4SRichard Henderson             copy_iaoq_entry(ctx, cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
183898cd9ca7SRichard Henderson         }
1839aac0f603SRichard Henderson         next = tcg_temp_new_i64();
18406fd0c7bcSRichard Henderson         tcg_gen_mov_i64(next, dest);
184198cd9ca7SRichard Henderson         if (is_n) {
1842c301f34eSRichard Henderson             if (use_nullify_skip(ctx)) {
1843a0180973SRichard Henderson                 copy_iaoq_entry(ctx, cpu_iaoq_f, -1, next);
18446fd0c7bcSRichard Henderson                 tcg_gen_addi_i64(next, next, 4);
1845a0180973SRichard Henderson                 copy_iaoq_entry(ctx, cpu_iaoq_b, -1, next);
1846c301f34eSRichard Henderson                 nullify_set(ctx, 0);
184731234768SRichard Henderson                 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
184801afb7beSRichard Henderson                 return true;
1849c301f34eSRichard Henderson             }
185098cd9ca7SRichard Henderson             ctx->null_cond.c = TCG_COND_ALWAYS;
185198cd9ca7SRichard Henderson         }
1852c301f34eSRichard Henderson         ctx->iaoq_n = -1;
1853c301f34eSRichard Henderson         ctx->iaoq_n_var = next;
185498cd9ca7SRichard Henderson     } else if (is_n && use_nullify_skip(ctx)) {
185598cd9ca7SRichard Henderson         /* The (conditional) branch, B, nullifies the next insn, N,
185698cd9ca7SRichard Henderson            and we're allowed to skip execution N (no single-step or
18574137cb83SRichard Henderson            tracepoint in effect).  Since the goto_ptr that we must use
185898cd9ca7SRichard Henderson            for the indirect branch consumes no special resources, we
185998cd9ca7SRichard Henderson            can (conditionally) skip B and continue execution.  */
186098cd9ca7SRichard Henderson         /* The use_nullify_skip test implies we have a known control path.  */
186198cd9ca7SRichard Henderson         tcg_debug_assert(ctx->iaoq_b != -1);
186298cd9ca7SRichard Henderson         tcg_debug_assert(ctx->iaoq_n != -1);
186398cd9ca7SRichard Henderson 
186498cd9ca7SRichard Henderson         /* We do have to handle the non-local temporary, DEST, before
186598cd9ca7SRichard Henderson            branching.  Since IOAQ_F is not really live at this point, we
186698cd9ca7SRichard Henderson            can simply store DEST optimistically.  Similarly with IAOQ_B.  */
1867a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, -1, dest);
1868aac0f603SRichard Henderson         next = tcg_temp_new_i64();
18696fd0c7bcSRichard Henderson         tcg_gen_addi_i64(next, dest, 4);
1870a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, -1, next);
187198cd9ca7SRichard Henderson 
187298cd9ca7SRichard Henderson         nullify_over(ctx);
187398cd9ca7SRichard Henderson         if (link != 0) {
18749a91dd84SRichard Henderson             copy_iaoq_entry(ctx, cpu_gr[link], ctx->iaoq_n, ctx->iaoq_n_var);
187598cd9ca7SRichard Henderson         }
18767f11636dSEmilio G. Cota         tcg_gen_lookup_and_goto_ptr();
187701afb7beSRichard Henderson         return nullify_end(ctx);
187898cd9ca7SRichard Henderson     } else {
187998cd9ca7SRichard Henderson         c = ctx->null_cond.c;
188098cd9ca7SRichard Henderson         a0 = ctx->null_cond.a0;
188198cd9ca7SRichard Henderson         a1 = ctx->null_cond.a1;
188298cd9ca7SRichard Henderson 
1883aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
1884aac0f603SRichard Henderson         next = tcg_temp_new_i64();
188598cd9ca7SRichard Henderson 
1886741322f4SRichard Henderson         copy_iaoq_entry(ctx, tmp, ctx->iaoq_n, ctx->iaoq_n_var);
18876fd0c7bcSRichard Henderson         tcg_gen_movcond_i64(c, next, a0, a1, tmp, dest);
188898cd9ca7SRichard Henderson         ctx->iaoq_n = -1;
188998cd9ca7SRichard Henderson         ctx->iaoq_n_var = next;
189098cd9ca7SRichard Henderson 
189198cd9ca7SRichard Henderson         if (link != 0) {
18926fd0c7bcSRichard Henderson             tcg_gen_movcond_i64(c, cpu_gr[link], a0, a1, cpu_gr[link], tmp);
189398cd9ca7SRichard Henderson         }
189498cd9ca7SRichard Henderson 
189598cd9ca7SRichard Henderson         if (is_n) {
189698cd9ca7SRichard Henderson             /* The branch nullifies the next insn, which means the state of N
189798cd9ca7SRichard Henderson                after the branch is the inverse of the state of N that applied
189898cd9ca7SRichard Henderson                to the branch.  */
18996fd0c7bcSRichard Henderson             tcg_gen_setcond_i64(tcg_invert_cond(c), cpu_psw_n, a0, a1);
190098cd9ca7SRichard Henderson             cond_free(&ctx->null_cond);
190198cd9ca7SRichard Henderson             ctx->null_cond = cond_make_n();
190298cd9ca7SRichard Henderson             ctx->psw_n_nonzero = true;
190398cd9ca7SRichard Henderson         } else {
190498cd9ca7SRichard Henderson             cond_free(&ctx->null_cond);
190598cd9ca7SRichard Henderson         }
190698cd9ca7SRichard Henderson     }
190701afb7beSRichard Henderson     return true;
190898cd9ca7SRichard Henderson }
190998cd9ca7SRichard Henderson 
1910660eefe1SRichard Henderson /* Implement
1911660eefe1SRichard Henderson  *    if (IAOQ_Front{30..31} < GR[b]{30..31})
1912660eefe1SRichard Henderson  *      IAOQ_Next{30..31} ← GR[b]{30..31};
1913660eefe1SRichard Henderson  *    else
1914660eefe1SRichard Henderson  *      IAOQ_Next{30..31} ← IAOQ_Front{30..31};
1915660eefe1SRichard Henderson  * which keeps the privilege level from being increased.
1916660eefe1SRichard Henderson  */
19176fd0c7bcSRichard Henderson static TCGv_i64 do_ibranch_priv(DisasContext *ctx, TCGv_i64 offset)
1918660eefe1SRichard Henderson {
19196fd0c7bcSRichard Henderson     TCGv_i64 dest;
1920660eefe1SRichard Henderson     switch (ctx->privilege) {
1921660eefe1SRichard Henderson     case 0:
1922660eefe1SRichard Henderson         /* Privilege 0 is maximum and is allowed to decrease.  */
1923660eefe1SRichard Henderson         return offset;
1924660eefe1SRichard Henderson     case 3:
1925993119feSRichard Henderson         /* Privilege 3 is minimum and is never allowed to increase.  */
1926aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
19276fd0c7bcSRichard Henderson         tcg_gen_ori_i64(dest, offset, 3);
1928660eefe1SRichard Henderson         break;
1929660eefe1SRichard Henderson     default:
1930aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
19316fd0c7bcSRichard Henderson         tcg_gen_andi_i64(dest, offset, -4);
19326fd0c7bcSRichard Henderson         tcg_gen_ori_i64(dest, dest, ctx->privilege);
19336fd0c7bcSRichard Henderson         tcg_gen_movcond_i64(TCG_COND_GTU, dest, dest, offset, dest, offset);
1934660eefe1SRichard Henderson         break;
1935660eefe1SRichard Henderson     }
1936660eefe1SRichard Henderson     return dest;
1937660eefe1SRichard Henderson }
1938660eefe1SRichard Henderson 
1939ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
19407ad439dfSRichard Henderson /* On Linux, page zero is normally marked execute only + gateway.
19417ad439dfSRichard Henderson    Therefore normal read or write is supposed to fail, but specific
19427ad439dfSRichard Henderson    offsets have kernel code mapped to raise permissions to implement
19437ad439dfSRichard Henderson    system calls.  Handling this via an explicit check here, rather
19447ad439dfSRichard Henderson    in than the "be disp(sr2,r0)" instruction that probably sent us
19457ad439dfSRichard Henderson    here, is the easiest way to handle the branch delay slot on the
19467ad439dfSRichard Henderson    aforementioned BE.  */
194731234768SRichard Henderson static void do_page_zero(DisasContext *ctx)
19487ad439dfSRichard Henderson {
19496fd0c7bcSRichard Henderson     TCGv_i64 tmp;
1950a0180973SRichard Henderson 
19517ad439dfSRichard Henderson     /* If by some means we get here with PSW[N]=1, that implies that
19527ad439dfSRichard Henderson        the B,GATE instruction would be skipped, and we'd fault on the
19538b81968cSMichael Tokarev        next insn within the privileged page.  */
19547ad439dfSRichard Henderson     switch (ctx->null_cond.c) {
19557ad439dfSRichard Henderson     case TCG_COND_NEVER:
19567ad439dfSRichard Henderson         break;
19577ad439dfSRichard Henderson     case TCG_COND_ALWAYS:
19586fd0c7bcSRichard Henderson         tcg_gen_movi_i64(cpu_psw_n, 0);
19597ad439dfSRichard Henderson         goto do_sigill;
19607ad439dfSRichard Henderson     default:
19617ad439dfSRichard Henderson         /* Since this is always the first (and only) insn within the
19627ad439dfSRichard Henderson            TB, we should know the state of PSW[N] from TB->FLAGS.  */
19637ad439dfSRichard Henderson         g_assert_not_reached();
19647ad439dfSRichard Henderson     }
19657ad439dfSRichard Henderson 
19667ad439dfSRichard Henderson     /* Check that we didn't arrive here via some means that allowed
19677ad439dfSRichard Henderson        non-sequential instruction execution.  Normally the PSW[B] bit
19687ad439dfSRichard Henderson        detects this by disallowing the B,GATE instruction to execute
19697ad439dfSRichard Henderson        under such conditions.  */
19707ad439dfSRichard Henderson     if (ctx->iaoq_b != ctx->iaoq_f + 4) {
19717ad439dfSRichard Henderson         goto do_sigill;
19727ad439dfSRichard Henderson     }
19737ad439dfSRichard Henderson 
1974ebd0e151SRichard Henderson     switch (ctx->iaoq_f & -4) {
19757ad439dfSRichard Henderson     case 0x00: /* Null pointer call */
19762986721dSRichard Henderson         gen_excp_1(EXCP_IMP);
197731234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
197831234768SRichard Henderson         break;
19797ad439dfSRichard Henderson 
19807ad439dfSRichard Henderson     case 0xb0: /* LWS */
19817ad439dfSRichard Henderson         gen_excp_1(EXCP_SYSCALL_LWS);
198231234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
198331234768SRichard Henderson         break;
19847ad439dfSRichard Henderson 
19857ad439dfSRichard Henderson     case 0xe0: /* SET_THREAD_POINTER */
19866fd0c7bcSRichard Henderson         tcg_gen_st_i64(cpu_gr[26], tcg_env, offsetof(CPUHPPAState, cr[27]));
1987aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
19886fd0c7bcSRichard Henderson         tcg_gen_ori_i64(tmp, cpu_gr[31], 3);
1989a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, -1, tmp);
19906fd0c7bcSRichard Henderson         tcg_gen_addi_i64(tmp, tmp, 4);
1991a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, -1, tmp);
199231234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
199331234768SRichard Henderson         break;
19947ad439dfSRichard Henderson 
19957ad439dfSRichard Henderson     case 0x100: /* SYSCALL */
19967ad439dfSRichard Henderson         gen_excp_1(EXCP_SYSCALL);
199731234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
199831234768SRichard Henderson         break;
19997ad439dfSRichard Henderson 
20007ad439dfSRichard Henderson     default:
20017ad439dfSRichard Henderson     do_sigill:
20022986721dSRichard Henderson         gen_excp_1(EXCP_ILL);
200331234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
200431234768SRichard Henderson         break;
20057ad439dfSRichard Henderson     }
20067ad439dfSRichard Henderson }
2007ba1d0b44SRichard Henderson #endif
20087ad439dfSRichard Henderson 
2009deee69a1SRichard Henderson static bool trans_nop(DisasContext *ctx, arg_nop *a)
2010b2167459SRichard Henderson {
2011b2167459SRichard Henderson     cond_free(&ctx->null_cond);
201231234768SRichard Henderson     return true;
2013b2167459SRichard Henderson }
2014b2167459SRichard Henderson 
201540f9f908SRichard Henderson static bool trans_break(DisasContext *ctx, arg_break *a)
201698a9cb79SRichard Henderson {
201731234768SRichard Henderson     return gen_excp_iir(ctx, EXCP_BREAK);
201898a9cb79SRichard Henderson }
201998a9cb79SRichard Henderson 
2020e36f27efSRichard Henderson static bool trans_sync(DisasContext *ctx, arg_sync *a)
202198a9cb79SRichard Henderson {
202298a9cb79SRichard Henderson     /* No point in nullifying the memory barrier.  */
202398a9cb79SRichard Henderson     tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
202498a9cb79SRichard Henderson 
202598a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
202631234768SRichard Henderson     return true;
202798a9cb79SRichard Henderson }
202898a9cb79SRichard Henderson 
2029c603e14aSRichard Henderson static bool trans_mfia(DisasContext *ctx, arg_mfia *a)
203098a9cb79SRichard Henderson {
2031c603e14aSRichard Henderson     unsigned rt = a->t;
20326fd0c7bcSRichard Henderson     TCGv_i64 tmp = dest_gpr(ctx, rt);
2033b5e0b3a5SSven Schnelle     tcg_gen_movi_i64(tmp, ctx->iaoq_f & ~3ULL);
203498a9cb79SRichard Henderson     save_gpr(ctx, rt, tmp);
203598a9cb79SRichard Henderson 
203698a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
203731234768SRichard Henderson     return true;
203898a9cb79SRichard Henderson }
203998a9cb79SRichard Henderson 
2040c603e14aSRichard Henderson static bool trans_mfsp(DisasContext *ctx, arg_mfsp *a)
204198a9cb79SRichard Henderson {
2042c603e14aSRichard Henderson     unsigned rt = a->t;
2043c603e14aSRichard Henderson     unsigned rs = a->sp;
204433423472SRichard Henderson     TCGv_i64 t0 = tcg_temp_new_i64();
204598a9cb79SRichard Henderson 
204633423472SRichard Henderson     load_spr(ctx, t0, rs);
204733423472SRichard Henderson     tcg_gen_shri_i64(t0, t0, 32);
204833423472SRichard Henderson 
2049967662cdSRichard Henderson     save_gpr(ctx, rt, t0);
205098a9cb79SRichard Henderson 
205198a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
205231234768SRichard Henderson     return true;
205398a9cb79SRichard Henderson }
205498a9cb79SRichard Henderson 
2055c603e14aSRichard Henderson static bool trans_mfctl(DisasContext *ctx, arg_mfctl *a)
205698a9cb79SRichard Henderson {
2057c603e14aSRichard Henderson     unsigned rt = a->t;
2058c603e14aSRichard Henderson     unsigned ctl = a->r;
20596fd0c7bcSRichard Henderson     TCGv_i64 tmp;
206098a9cb79SRichard Henderson 
206198a9cb79SRichard Henderson     switch (ctl) {
206235136a77SRichard Henderson     case CR_SAR:
2063c603e14aSRichard Henderson         if (a->e == 0) {
206498a9cb79SRichard Henderson             /* MFSAR without ,W masks low 5 bits.  */
206598a9cb79SRichard Henderson             tmp = dest_gpr(ctx, rt);
20666fd0c7bcSRichard Henderson             tcg_gen_andi_i64(tmp, cpu_sar, 31);
206798a9cb79SRichard Henderson             save_gpr(ctx, rt, tmp);
206835136a77SRichard Henderson             goto done;
206998a9cb79SRichard Henderson         }
207098a9cb79SRichard Henderson         save_gpr(ctx, rt, cpu_sar);
207135136a77SRichard Henderson         goto done;
207235136a77SRichard Henderson     case CR_IT: /* Interval Timer */
207335136a77SRichard Henderson         /* FIXME: Respect PSW_S bit.  */
207435136a77SRichard Henderson         nullify_over(ctx);
207598a9cb79SRichard Henderson         tmp = dest_gpr(ctx, rt);
2076dfd1b812SRichard Henderson         if (translator_io_start(&ctx->base)) {
207731234768SRichard Henderson             ctx->base.is_jmp = DISAS_IAQ_N_STALE;
207849c29d6cSRichard Henderson         }
20790c58c1bcSRichard Henderson         gen_helper_read_interval_timer(tmp);
208098a9cb79SRichard Henderson         save_gpr(ctx, rt, tmp);
208131234768SRichard Henderson         return nullify_end(ctx);
208298a9cb79SRichard Henderson     case 26:
208398a9cb79SRichard Henderson     case 27:
208498a9cb79SRichard Henderson         break;
208598a9cb79SRichard Henderson     default:
208698a9cb79SRichard Henderson         /* All other control registers are privileged.  */
208735136a77SRichard Henderson         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
208835136a77SRichard Henderson         break;
208998a9cb79SRichard Henderson     }
209098a9cb79SRichard Henderson 
2091aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
20926fd0c7bcSRichard Henderson     tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
209335136a77SRichard Henderson     save_gpr(ctx, rt, tmp);
209435136a77SRichard Henderson 
209535136a77SRichard Henderson  done:
209698a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
209731234768SRichard Henderson     return true;
209898a9cb79SRichard Henderson }
209998a9cb79SRichard Henderson 
2100c603e14aSRichard Henderson static bool trans_mtsp(DisasContext *ctx, arg_mtsp *a)
210133423472SRichard Henderson {
2102c603e14aSRichard Henderson     unsigned rr = a->r;
2103c603e14aSRichard Henderson     unsigned rs = a->sp;
2104967662cdSRichard Henderson     TCGv_i64 tmp;
210533423472SRichard Henderson 
210633423472SRichard Henderson     if (rs >= 5) {
210733423472SRichard Henderson         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
210833423472SRichard Henderson     }
210933423472SRichard Henderson     nullify_over(ctx);
211033423472SRichard Henderson 
2111967662cdSRichard Henderson     tmp = tcg_temp_new_i64();
2112967662cdSRichard Henderson     tcg_gen_shli_i64(tmp, load_gpr(ctx, rr), 32);
211333423472SRichard Henderson 
211433423472SRichard Henderson     if (rs >= 4) {
2115967662cdSRichard Henderson         tcg_gen_st_i64(tmp, tcg_env, offsetof(CPUHPPAState, sr[rs]));
2116494737b7SRichard Henderson         ctx->tb_flags &= ~TB_FLAG_SR_SAME;
211733423472SRichard Henderson     } else {
2118967662cdSRichard Henderson         tcg_gen_mov_i64(cpu_sr[rs], tmp);
211933423472SRichard Henderson     }
212033423472SRichard Henderson 
212131234768SRichard Henderson     return nullify_end(ctx);
212233423472SRichard Henderson }
212333423472SRichard Henderson 
2124c603e14aSRichard Henderson static bool trans_mtctl(DisasContext *ctx, arg_mtctl *a)
212598a9cb79SRichard Henderson {
2126c603e14aSRichard Henderson     unsigned ctl = a->t;
21276fd0c7bcSRichard Henderson     TCGv_i64 reg;
21286fd0c7bcSRichard Henderson     TCGv_i64 tmp;
212998a9cb79SRichard Henderson 
213035136a77SRichard Henderson     if (ctl == CR_SAR) {
21314845f015SSven Schnelle         reg = load_gpr(ctx, a->r);
2132aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
21336fd0c7bcSRichard Henderson         tcg_gen_andi_i64(tmp, reg, ctx->is_pa20 ? 63 : 31);
213498a9cb79SRichard Henderson         save_or_nullify(ctx, cpu_sar, tmp);
213598a9cb79SRichard Henderson 
213698a9cb79SRichard Henderson         cond_free(&ctx->null_cond);
213731234768SRichard Henderson         return true;
213898a9cb79SRichard Henderson     }
213998a9cb79SRichard Henderson 
214035136a77SRichard Henderson     /* All other control registers are privileged or read-only.  */
214135136a77SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
214235136a77SRichard Henderson 
2143c603e14aSRichard Henderson #ifndef CONFIG_USER_ONLY
214435136a77SRichard Henderson     nullify_over(ctx);
21454c34bab0SHelge Deller 
21464c34bab0SHelge Deller     if (ctx->is_pa20) {
21474845f015SSven Schnelle         reg = load_gpr(ctx, a->r);
21484c34bab0SHelge Deller     } else {
21494c34bab0SHelge Deller         reg = tcg_temp_new_i64();
21504c34bab0SHelge Deller         tcg_gen_ext32u_i64(reg, load_gpr(ctx, a->r));
21514c34bab0SHelge Deller     }
21524845f015SSven Schnelle 
215335136a77SRichard Henderson     switch (ctl) {
215435136a77SRichard Henderson     case CR_IT:
2155104281c1SRichard Henderson         if (translator_io_start(&ctx->base)) {
2156104281c1SRichard Henderson             ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2157104281c1SRichard Henderson         }
2158ad75a51eSRichard Henderson         gen_helper_write_interval_timer(tcg_env, reg);
215935136a77SRichard Henderson         break;
21604f5f2548SRichard Henderson     case CR_EIRR:
21616ebebea7SRichard Henderson         /* Helper modifies interrupt lines and is therefore IO. */
21626ebebea7SRichard Henderson         translator_io_start(&ctx->base);
2163ad75a51eSRichard Henderson         gen_helper_write_eirr(tcg_env, reg);
21646ebebea7SRichard Henderson         /* Exit to re-evaluate interrupts in the main loop. */
216531234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
21664f5f2548SRichard Henderson         break;
21674f5f2548SRichard Henderson 
216835136a77SRichard Henderson     case CR_IIASQ:
216935136a77SRichard Henderson     case CR_IIAOQ:
217035136a77SRichard Henderson         /* FIXME: Respect PSW_Q bit */
217135136a77SRichard Henderson         /* The write advances the queue and stores to the back element.  */
2172aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
21736fd0c7bcSRichard Henderson         tcg_gen_ld_i64(tmp, tcg_env,
217435136a77SRichard Henderson                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
21756fd0c7bcSRichard Henderson         tcg_gen_st_i64(tmp, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
21766fd0c7bcSRichard Henderson         tcg_gen_st_i64(reg, tcg_env,
217735136a77SRichard Henderson                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
217835136a77SRichard Henderson         break;
217935136a77SRichard Henderson 
2180d5de20bdSSven Schnelle     case CR_PID1:
2181d5de20bdSSven Schnelle     case CR_PID2:
2182d5de20bdSSven Schnelle     case CR_PID3:
2183d5de20bdSSven Schnelle     case CR_PID4:
21846fd0c7bcSRichard Henderson         tcg_gen_st_i64(reg, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
2185d5de20bdSSven Schnelle #ifndef CONFIG_USER_ONLY
2186ad75a51eSRichard Henderson         gen_helper_change_prot_id(tcg_env);
2187d5de20bdSSven Schnelle #endif
2188d5de20bdSSven Schnelle         break;
2189d5de20bdSSven Schnelle 
21906ebebea7SRichard Henderson     case CR_EIEM:
21916ebebea7SRichard Henderson         /* Exit to re-evaluate interrupts in the main loop. */
21926ebebea7SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
21936ebebea7SRichard Henderson         /* FALLTHRU */
219435136a77SRichard Henderson     default:
21956fd0c7bcSRichard Henderson         tcg_gen_st_i64(reg, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
219635136a77SRichard Henderson         break;
219735136a77SRichard Henderson     }
219831234768SRichard Henderson     return nullify_end(ctx);
21994f5f2548SRichard Henderson #endif
220035136a77SRichard Henderson }
220135136a77SRichard Henderson 
2202c603e14aSRichard Henderson static bool trans_mtsarcm(DisasContext *ctx, arg_mtsarcm *a)
220398a9cb79SRichard Henderson {
2204aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
220598a9cb79SRichard Henderson 
22066fd0c7bcSRichard Henderson     tcg_gen_not_i64(tmp, load_gpr(ctx, a->r));
22076fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, ctx->is_pa20 ? 63 : 31);
220898a9cb79SRichard Henderson     save_or_nullify(ctx, cpu_sar, tmp);
220998a9cb79SRichard Henderson 
221098a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
221131234768SRichard Henderson     return true;
221298a9cb79SRichard Henderson }
221398a9cb79SRichard Henderson 
2214e36f27efSRichard Henderson static bool trans_ldsid(DisasContext *ctx, arg_ldsid *a)
221598a9cb79SRichard Henderson {
22166fd0c7bcSRichard Henderson     TCGv_i64 dest = dest_gpr(ctx, a->t);
221798a9cb79SRichard Henderson 
22182330504cSHelge Deller #ifdef CONFIG_USER_ONLY
22192330504cSHelge Deller     /* We don't implement space registers in user mode. */
22206fd0c7bcSRichard Henderson     tcg_gen_movi_i64(dest, 0);
22212330504cSHelge Deller #else
2222967662cdSRichard Henderson     tcg_gen_mov_i64(dest, space_select(ctx, a->sp, load_gpr(ctx, a->b)));
2223967662cdSRichard Henderson     tcg_gen_shri_i64(dest, dest, 32);
22242330504cSHelge Deller #endif
2225e36f27efSRichard Henderson     save_gpr(ctx, a->t, dest);
222698a9cb79SRichard Henderson 
222798a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
222831234768SRichard Henderson     return true;
222998a9cb79SRichard Henderson }
223098a9cb79SRichard Henderson 
2231e36f27efSRichard Henderson static bool trans_rsm(DisasContext *ctx, arg_rsm *a)
2232e36f27efSRichard Henderson {
22337b2d70a1SHelge Deller #ifdef CONFIG_USER_ONLY
2234e36f27efSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
22357b2d70a1SHelge Deller #else
22366fd0c7bcSRichard Henderson     TCGv_i64 tmp;
2237e1b5a5edSRichard Henderson 
22387b2d70a1SHelge Deller     /* HP-UX 11i and HP ODE use rsm for read-access to PSW */
22397b2d70a1SHelge Deller     if (a->i) {
22407b2d70a1SHelge Deller         CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
22417b2d70a1SHelge Deller     }
22427b2d70a1SHelge Deller 
2243e1b5a5edSRichard Henderson     nullify_over(ctx);
2244e1b5a5edSRichard Henderson 
2245aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
22466fd0c7bcSRichard Henderson     tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPUHPPAState, psw));
22476fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, ~a->i);
2248ad75a51eSRichard Henderson     gen_helper_swap_system_mask(tmp, tcg_env, tmp);
2249e36f27efSRichard Henderson     save_gpr(ctx, a->t, tmp);
2250e1b5a5edSRichard Henderson 
2251e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts, e.g. PSW_M.  */
225231234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
225331234768SRichard Henderson     return nullify_end(ctx);
2254e36f27efSRichard Henderson #endif
2255e1b5a5edSRichard Henderson }
2256e1b5a5edSRichard Henderson 
2257e36f27efSRichard Henderson static bool trans_ssm(DisasContext *ctx, arg_ssm *a)
2258e1b5a5edSRichard Henderson {
2259e36f27efSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2260e36f27efSRichard Henderson #ifndef CONFIG_USER_ONLY
22616fd0c7bcSRichard Henderson     TCGv_i64 tmp;
2262e1b5a5edSRichard Henderson 
2263e1b5a5edSRichard Henderson     nullify_over(ctx);
2264e1b5a5edSRichard Henderson 
2265aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
22666fd0c7bcSRichard Henderson     tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPUHPPAState, psw));
22676fd0c7bcSRichard Henderson     tcg_gen_ori_i64(tmp, tmp, a->i);
2268ad75a51eSRichard Henderson     gen_helper_swap_system_mask(tmp, tcg_env, tmp);
2269e36f27efSRichard Henderson     save_gpr(ctx, a->t, tmp);
2270e1b5a5edSRichard Henderson 
2271e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts, e.g. PSW_I.  */
227231234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
227331234768SRichard Henderson     return nullify_end(ctx);
2274e36f27efSRichard Henderson #endif
2275e1b5a5edSRichard Henderson }
2276e1b5a5edSRichard Henderson 
2277c603e14aSRichard Henderson static bool trans_mtsm(DisasContext *ctx, arg_mtsm *a)
2278e1b5a5edSRichard Henderson {
2279e1b5a5edSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2280c603e14aSRichard Henderson #ifndef CONFIG_USER_ONLY
22816fd0c7bcSRichard Henderson     TCGv_i64 tmp, reg;
2282e1b5a5edSRichard Henderson     nullify_over(ctx);
2283e1b5a5edSRichard Henderson 
2284c603e14aSRichard Henderson     reg = load_gpr(ctx, a->r);
2285aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
2286ad75a51eSRichard Henderson     gen_helper_swap_system_mask(tmp, tcg_env, reg);
2287e1b5a5edSRichard Henderson 
2288e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts.  */
228931234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
229031234768SRichard Henderson     return nullify_end(ctx);
2291c603e14aSRichard Henderson #endif
2292e1b5a5edSRichard Henderson }
2293f49b3537SRichard Henderson 
2294e36f27efSRichard Henderson static bool do_rfi(DisasContext *ctx, bool rfi_r)
2295f49b3537SRichard Henderson {
2296f49b3537SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2297e36f27efSRichard Henderson #ifndef CONFIG_USER_ONLY
2298f49b3537SRichard Henderson     nullify_over(ctx);
2299f49b3537SRichard Henderson 
2300e36f27efSRichard Henderson     if (rfi_r) {
2301ad75a51eSRichard Henderson         gen_helper_rfi_r(tcg_env);
2302f49b3537SRichard Henderson     } else {
2303ad75a51eSRichard Henderson         gen_helper_rfi(tcg_env);
2304f49b3537SRichard Henderson     }
230531234768SRichard Henderson     /* Exit the TB to recognize new interrupts.  */
230607ea28b4SRichard Henderson     tcg_gen_exit_tb(NULL, 0);
230731234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
2308f49b3537SRichard Henderson 
230931234768SRichard Henderson     return nullify_end(ctx);
2310e36f27efSRichard Henderson #endif
2311f49b3537SRichard Henderson }
23126210db05SHelge Deller 
2313e36f27efSRichard Henderson static bool trans_rfi(DisasContext *ctx, arg_rfi *a)
2314e36f27efSRichard Henderson {
2315e36f27efSRichard Henderson     return do_rfi(ctx, false);
2316e36f27efSRichard Henderson }
2317e36f27efSRichard Henderson 
2318e36f27efSRichard Henderson static bool trans_rfi_r(DisasContext *ctx, arg_rfi_r *a)
2319e36f27efSRichard Henderson {
2320e36f27efSRichard Henderson     return do_rfi(ctx, true);
2321e36f27efSRichard Henderson }
2322e36f27efSRichard Henderson 
232396927adbSRichard Henderson static bool trans_halt(DisasContext *ctx, arg_halt *a)
23246210db05SHelge Deller {
23256210db05SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
232696927adbSRichard Henderson #ifndef CONFIG_USER_ONLY
23276210db05SHelge Deller     nullify_over(ctx);
2328ad75a51eSRichard Henderson     gen_helper_halt(tcg_env);
232931234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
233031234768SRichard Henderson     return nullify_end(ctx);
233196927adbSRichard Henderson #endif
23326210db05SHelge Deller }
233396927adbSRichard Henderson 
233496927adbSRichard Henderson static bool trans_reset(DisasContext *ctx, arg_reset *a)
233596927adbSRichard Henderson {
233696927adbSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
233796927adbSRichard Henderson #ifndef CONFIG_USER_ONLY
233896927adbSRichard Henderson     nullify_over(ctx);
2339ad75a51eSRichard Henderson     gen_helper_reset(tcg_env);
234096927adbSRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
234196927adbSRichard Henderson     return nullify_end(ctx);
234296927adbSRichard Henderson #endif
234396927adbSRichard Henderson }
2344e1b5a5edSRichard Henderson 
23454a4554c6SHelge Deller static bool trans_getshadowregs(DisasContext *ctx, arg_getshadowregs *a)
23464a4554c6SHelge Deller {
23474a4554c6SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
23484a4554c6SHelge Deller #ifndef CONFIG_USER_ONLY
23494a4554c6SHelge Deller     nullify_over(ctx);
2350ad75a51eSRichard Henderson     gen_helper_getshadowregs(tcg_env);
23514a4554c6SHelge Deller     return nullify_end(ctx);
23524a4554c6SHelge Deller #endif
23534a4554c6SHelge Deller }
23544a4554c6SHelge Deller 
2355deee69a1SRichard Henderson static bool trans_nop_addrx(DisasContext *ctx, arg_ldst *a)
235698a9cb79SRichard Henderson {
2357deee69a1SRichard Henderson     if (a->m) {
23586fd0c7bcSRichard Henderson         TCGv_i64 dest = dest_gpr(ctx, a->b);
23596fd0c7bcSRichard Henderson         TCGv_i64 src1 = load_gpr(ctx, a->b);
23606fd0c7bcSRichard Henderson         TCGv_i64 src2 = load_gpr(ctx, a->x);
236198a9cb79SRichard Henderson 
236298a9cb79SRichard Henderson         /* The only thing we need to do is the base register modification.  */
23636fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, src1, src2);
2364deee69a1SRichard Henderson         save_gpr(ctx, a->b, dest);
2365deee69a1SRichard Henderson     }
236698a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
236731234768SRichard Henderson     return true;
236898a9cb79SRichard Henderson }
236998a9cb79SRichard Henderson 
2370ad1fdacdSSven Schnelle static bool trans_fic(DisasContext *ctx, arg_ldst *a)
2371ad1fdacdSSven Schnelle {
2372ad1fdacdSSven Schnelle     /* End TB for flush instruction cache, so we pick up new insns. */
2373ad1fdacdSSven Schnelle     ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2374ad1fdacdSSven Schnelle     return trans_nop_addrx(ctx, a);
2375ad1fdacdSSven Schnelle }
2376ad1fdacdSSven Schnelle 
2377deee69a1SRichard Henderson static bool trans_probe(DisasContext *ctx, arg_probe *a)
237898a9cb79SRichard Henderson {
23796fd0c7bcSRichard Henderson     TCGv_i64 dest, ofs;
2380eed14219SRichard Henderson     TCGv_i32 level, want;
23816fd0c7bcSRichard Henderson     TCGv_i64 addr;
238298a9cb79SRichard Henderson 
238398a9cb79SRichard Henderson     nullify_over(ctx);
238498a9cb79SRichard Henderson 
2385deee69a1SRichard Henderson     dest = dest_gpr(ctx, a->t);
2386deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2387eed14219SRichard Henderson 
2388deee69a1SRichard Henderson     if (a->imm) {
2389e5d487c9SRichard Henderson         level = tcg_constant_i32(a->ri & 3);
239098a9cb79SRichard Henderson     } else {
2391eed14219SRichard Henderson         level = tcg_temp_new_i32();
23926fd0c7bcSRichard Henderson         tcg_gen_extrl_i64_i32(level, load_gpr(ctx, a->ri));
2393eed14219SRichard Henderson         tcg_gen_andi_i32(level, level, 3);
239498a9cb79SRichard Henderson     }
239529dd6f64SRichard Henderson     want = tcg_constant_i32(a->write ? PAGE_WRITE : PAGE_READ);
2396eed14219SRichard Henderson 
2397ad75a51eSRichard Henderson     gen_helper_probe(dest, tcg_env, addr, level, want);
2398eed14219SRichard Henderson 
2399deee69a1SRichard Henderson     save_gpr(ctx, a->t, dest);
240031234768SRichard Henderson     return nullify_end(ctx);
240198a9cb79SRichard Henderson }
240298a9cb79SRichard Henderson 
2403deee69a1SRichard Henderson static bool trans_ixtlbx(DisasContext *ctx, arg_ixtlbx *a)
24048d6ae7fbSRichard Henderson {
24058577f354SRichard Henderson     if (ctx->is_pa20) {
24068577f354SRichard Henderson         return false;
24078577f354SRichard Henderson     }
2408deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2409deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
24106fd0c7bcSRichard Henderson     TCGv_i64 addr;
24116fd0c7bcSRichard Henderson     TCGv_i64 ofs, reg;
24128d6ae7fbSRichard Henderson 
24138d6ae7fbSRichard Henderson     nullify_over(ctx);
24148d6ae7fbSRichard Henderson 
2415deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2416deee69a1SRichard Henderson     reg = load_gpr(ctx, a->r);
2417deee69a1SRichard Henderson     if (a->addr) {
24188577f354SRichard Henderson         gen_helper_itlba_pa11(tcg_env, addr, reg);
24198d6ae7fbSRichard Henderson     } else {
24208577f354SRichard Henderson         gen_helper_itlbp_pa11(tcg_env, addr, reg);
24218d6ae7fbSRichard Henderson     }
24228d6ae7fbSRichard Henderson 
242332dc7569SSven Schnelle     /* Exit TB for TLB change if mmu is enabled.  */
242432dc7569SSven Schnelle     if (ctx->tb_flags & PSW_C) {
242531234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
242631234768SRichard Henderson     }
242731234768SRichard Henderson     return nullify_end(ctx);
2428deee69a1SRichard Henderson #endif
24298d6ae7fbSRichard Henderson }
243063300a00SRichard Henderson 
2431eb25d10fSHelge Deller static bool do_pxtlb(DisasContext *ctx, arg_ldst *a, bool local)
243263300a00SRichard Henderson {
2433deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2434deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
24356fd0c7bcSRichard Henderson     TCGv_i64 addr;
24366fd0c7bcSRichard Henderson     TCGv_i64 ofs;
243763300a00SRichard Henderson 
243863300a00SRichard Henderson     nullify_over(ctx);
243963300a00SRichard Henderson 
2440deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2441eb25d10fSHelge Deller 
2442eb25d10fSHelge Deller     /*
2443eb25d10fSHelge Deller      * Page align now, rather than later, so that we can add in the
2444eb25d10fSHelge Deller      * page_size field from pa2.0 from the low 4 bits of GR[b].
2445eb25d10fSHelge Deller      */
2446eb25d10fSHelge Deller     tcg_gen_andi_i64(addr, addr, TARGET_PAGE_MASK);
2447eb25d10fSHelge Deller     if (ctx->is_pa20) {
2448eb25d10fSHelge Deller         tcg_gen_deposit_i64(addr, addr, load_gpr(ctx, a->b), 0, 4);
244963300a00SRichard Henderson     }
2450eb25d10fSHelge Deller 
2451eb25d10fSHelge Deller     if (local) {
2452eb25d10fSHelge Deller         gen_helper_ptlb_l(tcg_env, addr);
245363300a00SRichard Henderson     } else {
2454ad75a51eSRichard Henderson         gen_helper_ptlb(tcg_env, addr);
245563300a00SRichard Henderson     }
245663300a00SRichard Henderson 
2457eb25d10fSHelge Deller     if (a->m) {
2458eb25d10fSHelge Deller         save_gpr(ctx, a->b, ofs);
2459eb25d10fSHelge Deller     }
2460eb25d10fSHelge Deller 
2461eb25d10fSHelge Deller     /* Exit TB for TLB change if mmu is enabled.  */
2462eb25d10fSHelge Deller     if (ctx->tb_flags & PSW_C) {
2463eb25d10fSHelge Deller         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2464eb25d10fSHelge Deller     }
2465eb25d10fSHelge Deller     return nullify_end(ctx);
2466eb25d10fSHelge Deller #endif
2467eb25d10fSHelge Deller }
2468eb25d10fSHelge Deller 
2469eb25d10fSHelge Deller static bool trans_pxtlb(DisasContext *ctx, arg_ldst *a)
2470eb25d10fSHelge Deller {
2471eb25d10fSHelge Deller     return do_pxtlb(ctx, a, false);
2472eb25d10fSHelge Deller }
2473eb25d10fSHelge Deller 
2474eb25d10fSHelge Deller static bool trans_pxtlb_l(DisasContext *ctx, arg_ldst *a)
2475eb25d10fSHelge Deller {
2476eb25d10fSHelge Deller     return ctx->is_pa20 && do_pxtlb(ctx, a, true);
2477eb25d10fSHelge Deller }
2478eb25d10fSHelge Deller 
2479eb25d10fSHelge Deller static bool trans_pxtlbe(DisasContext *ctx, arg_ldst *a)
2480eb25d10fSHelge Deller {
2481eb25d10fSHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2482eb25d10fSHelge Deller #ifndef CONFIG_USER_ONLY
2483eb25d10fSHelge Deller     nullify_over(ctx);
2484eb25d10fSHelge Deller 
2485eb25d10fSHelge Deller     trans_nop_addrx(ctx, a);
2486eb25d10fSHelge Deller     gen_helper_ptlbe(tcg_env);
2487eb25d10fSHelge Deller 
248863300a00SRichard Henderson     /* Exit TB for TLB change if mmu is enabled.  */
248932dc7569SSven Schnelle     if (ctx->tb_flags & PSW_C) {
249031234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
249131234768SRichard Henderson     }
249231234768SRichard Henderson     return nullify_end(ctx);
2493deee69a1SRichard Henderson #endif
249463300a00SRichard Henderson }
24952dfcca9fSRichard Henderson 
24966797c315SNick Hudson /*
24976797c315SNick Hudson  * Implement the pcxl and pcxl2 Fast TLB Insert instructions.
24986797c315SNick Hudson  * See
24996797c315SNick Hudson  *     https://parisc.wiki.kernel.org/images-parisc/a/a9/Pcxl2_ers.pdf
25006797c315SNick Hudson  *     page 13-9 (195/206)
25016797c315SNick Hudson  */
25026797c315SNick Hudson static bool trans_ixtlbxf(DisasContext *ctx, arg_ixtlbxf *a)
25036797c315SNick Hudson {
25048577f354SRichard Henderson     if (ctx->is_pa20) {
25058577f354SRichard Henderson         return false;
25068577f354SRichard Henderson     }
25076797c315SNick Hudson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
25086797c315SNick Hudson #ifndef CONFIG_USER_ONLY
25096fd0c7bcSRichard Henderson     TCGv_i64 addr, atl, stl;
25106fd0c7bcSRichard Henderson     TCGv_i64 reg;
25116797c315SNick Hudson 
25126797c315SNick Hudson     nullify_over(ctx);
25136797c315SNick Hudson 
25146797c315SNick Hudson     /*
25156797c315SNick Hudson      * FIXME:
25166797c315SNick Hudson      *  if (not (pcxl or pcxl2))
25176797c315SNick Hudson      *    return gen_illegal(ctx);
25186797c315SNick Hudson      */
25196797c315SNick Hudson 
25206fd0c7bcSRichard Henderson     atl = tcg_temp_new_i64();
25216fd0c7bcSRichard Henderson     stl = tcg_temp_new_i64();
25226fd0c7bcSRichard Henderson     addr = tcg_temp_new_i64();
25236797c315SNick Hudson 
2524ad75a51eSRichard Henderson     tcg_gen_ld32u_i64(stl, tcg_env,
25256797c315SNick Hudson                       a->data ? offsetof(CPUHPPAState, cr[CR_ISR])
25266797c315SNick Hudson                       : offsetof(CPUHPPAState, cr[CR_IIASQ]));
2527ad75a51eSRichard Henderson     tcg_gen_ld32u_i64(atl, tcg_env,
25286797c315SNick Hudson                       a->data ? offsetof(CPUHPPAState, cr[CR_IOR])
25296797c315SNick Hudson                       : offsetof(CPUHPPAState, cr[CR_IIAOQ]));
25306797c315SNick Hudson     tcg_gen_shli_i64(stl, stl, 32);
2531d265360fSRichard Henderson     tcg_gen_or_i64(addr, atl, stl);
25326797c315SNick Hudson 
25336797c315SNick Hudson     reg = load_gpr(ctx, a->r);
25346797c315SNick Hudson     if (a->addr) {
25358577f354SRichard Henderson         gen_helper_itlba_pa11(tcg_env, addr, reg);
25366797c315SNick Hudson     } else {
25378577f354SRichard Henderson         gen_helper_itlbp_pa11(tcg_env, addr, reg);
25386797c315SNick Hudson     }
25396797c315SNick Hudson 
25406797c315SNick Hudson     /* Exit TB for TLB change if mmu is enabled.  */
25416797c315SNick Hudson     if (ctx->tb_flags & PSW_C) {
25426797c315SNick Hudson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
25436797c315SNick Hudson     }
25446797c315SNick Hudson     return nullify_end(ctx);
25456797c315SNick Hudson #endif
25466797c315SNick Hudson }
25476797c315SNick Hudson 
25488577f354SRichard Henderson static bool trans_ixtlbt(DisasContext *ctx, arg_ixtlbt *a)
25498577f354SRichard Henderson {
25508577f354SRichard Henderson     if (!ctx->is_pa20) {
25518577f354SRichard Henderson         return false;
25528577f354SRichard Henderson     }
25538577f354SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
25548577f354SRichard Henderson #ifndef CONFIG_USER_ONLY
25558577f354SRichard Henderson     nullify_over(ctx);
25568577f354SRichard Henderson     {
25578577f354SRichard Henderson         TCGv_i64 src1 = load_gpr(ctx, a->r1);
25588577f354SRichard Henderson         TCGv_i64 src2 = load_gpr(ctx, a->r2);
25598577f354SRichard Henderson 
25608577f354SRichard Henderson         if (a->data) {
25618577f354SRichard Henderson             gen_helper_idtlbt_pa20(tcg_env, src1, src2);
25628577f354SRichard Henderson         } else {
25638577f354SRichard Henderson             gen_helper_iitlbt_pa20(tcg_env, src1, src2);
25648577f354SRichard Henderson         }
25658577f354SRichard Henderson     }
25668577f354SRichard Henderson     /* Exit TB for TLB change if mmu is enabled.  */
25678577f354SRichard Henderson     if (ctx->tb_flags & PSW_C) {
25688577f354SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
25698577f354SRichard Henderson     }
25708577f354SRichard Henderson     return nullify_end(ctx);
25718577f354SRichard Henderson #endif
25728577f354SRichard Henderson }
25738577f354SRichard Henderson 
2574deee69a1SRichard Henderson static bool trans_lpa(DisasContext *ctx, arg_ldst *a)
25752dfcca9fSRichard Henderson {
2576deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2577deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
25786fd0c7bcSRichard Henderson     TCGv_i64 vaddr;
25796fd0c7bcSRichard Henderson     TCGv_i64 ofs, paddr;
25802dfcca9fSRichard Henderson 
25812dfcca9fSRichard Henderson     nullify_over(ctx);
25822dfcca9fSRichard Henderson 
2583deee69a1SRichard Henderson     form_gva(ctx, &vaddr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
25842dfcca9fSRichard Henderson 
2585aac0f603SRichard Henderson     paddr = tcg_temp_new_i64();
2586ad75a51eSRichard Henderson     gen_helper_lpa(paddr, tcg_env, vaddr);
25872dfcca9fSRichard Henderson 
25882dfcca9fSRichard Henderson     /* Note that physical address result overrides base modification.  */
2589deee69a1SRichard Henderson     if (a->m) {
2590deee69a1SRichard Henderson         save_gpr(ctx, a->b, ofs);
25912dfcca9fSRichard Henderson     }
2592deee69a1SRichard Henderson     save_gpr(ctx, a->t, paddr);
25932dfcca9fSRichard Henderson 
259431234768SRichard Henderson     return nullify_end(ctx);
2595deee69a1SRichard Henderson #endif
25962dfcca9fSRichard Henderson }
259743a97b81SRichard Henderson 
2598deee69a1SRichard Henderson static bool trans_lci(DisasContext *ctx, arg_lci *a)
259943a97b81SRichard Henderson {
260043a97b81SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
260143a97b81SRichard Henderson 
260243a97b81SRichard Henderson     /* The Coherence Index is an implementation-defined function of the
260343a97b81SRichard Henderson        physical address.  Two addresses with the same CI have a coherent
260443a97b81SRichard Henderson        view of the cache.  Our implementation is to return 0 for all,
260543a97b81SRichard Henderson        since the entire address space is coherent.  */
2606a4db4a78SRichard Henderson     save_gpr(ctx, a->t, ctx->zero);
260743a97b81SRichard Henderson 
260831234768SRichard Henderson     cond_free(&ctx->null_cond);
260931234768SRichard Henderson     return true;
261043a97b81SRichard Henderson }
261198a9cb79SRichard Henderson 
2612faf97ba1SRichard Henderson static bool trans_add(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2613b2167459SRichard Henderson {
26140c982a28SRichard Henderson     return do_add_reg(ctx, a, false, false, false, false);
2615b2167459SRichard Henderson }
2616b2167459SRichard Henderson 
2617faf97ba1SRichard Henderson static bool trans_add_l(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2618b2167459SRichard Henderson {
26190c982a28SRichard Henderson     return do_add_reg(ctx, a, true, false, false, false);
2620b2167459SRichard Henderson }
2621b2167459SRichard Henderson 
2622faf97ba1SRichard Henderson static bool trans_add_tsv(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2623b2167459SRichard Henderson {
26240c982a28SRichard Henderson     return do_add_reg(ctx, a, false, true, false, false);
2625b2167459SRichard Henderson }
2626b2167459SRichard Henderson 
2627faf97ba1SRichard Henderson static bool trans_add_c(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2628b2167459SRichard Henderson {
26290c982a28SRichard Henderson     return do_add_reg(ctx, a, false, false, false, true);
26300c982a28SRichard Henderson }
2631b2167459SRichard Henderson 
2632faf97ba1SRichard Henderson static bool trans_add_c_tsv(DisasContext *ctx, arg_rrr_cf_d_sh *a)
26330c982a28SRichard Henderson {
26340c982a28SRichard Henderson     return do_add_reg(ctx, a, false, true, false, true);
26350c982a28SRichard Henderson }
26360c982a28SRichard Henderson 
263763c427c6SRichard Henderson static bool trans_sub(DisasContext *ctx, arg_rrr_cf_d *a)
26380c982a28SRichard Henderson {
26390c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, false, false);
26400c982a28SRichard Henderson }
26410c982a28SRichard Henderson 
264263c427c6SRichard Henderson static bool trans_sub_tsv(DisasContext *ctx, arg_rrr_cf_d *a)
26430c982a28SRichard Henderson {
26440c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, false, false);
26450c982a28SRichard Henderson }
26460c982a28SRichard Henderson 
264763c427c6SRichard Henderson static bool trans_sub_tc(DisasContext *ctx, arg_rrr_cf_d *a)
26480c982a28SRichard Henderson {
26490c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, false, true);
26500c982a28SRichard Henderson }
26510c982a28SRichard Henderson 
265263c427c6SRichard Henderson static bool trans_sub_tsv_tc(DisasContext *ctx, arg_rrr_cf_d *a)
26530c982a28SRichard Henderson {
26540c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, false, true);
26550c982a28SRichard Henderson }
26560c982a28SRichard Henderson 
265763c427c6SRichard Henderson static bool trans_sub_b(DisasContext *ctx, arg_rrr_cf_d *a)
26580c982a28SRichard Henderson {
26590c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, true, false);
26600c982a28SRichard Henderson }
26610c982a28SRichard Henderson 
266263c427c6SRichard Henderson static bool trans_sub_b_tsv(DisasContext *ctx, arg_rrr_cf_d *a)
26630c982a28SRichard Henderson {
26640c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, true, false);
26650c982a28SRichard Henderson }
26660c982a28SRichard Henderson 
2667fa8e3bedSRichard Henderson static bool trans_andcm(DisasContext *ctx, arg_rrr_cf_d *a)
26680c982a28SRichard Henderson {
26696fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_andc_i64);
26700c982a28SRichard Henderson }
26710c982a28SRichard Henderson 
2672fa8e3bedSRichard Henderson static bool trans_and(DisasContext *ctx, arg_rrr_cf_d *a)
26730c982a28SRichard Henderson {
26746fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_and_i64);
26750c982a28SRichard Henderson }
26760c982a28SRichard Henderson 
2677fa8e3bedSRichard Henderson static bool trans_or(DisasContext *ctx, arg_rrr_cf_d *a)
26780c982a28SRichard Henderson {
26790c982a28SRichard Henderson     if (a->cf == 0) {
26800c982a28SRichard Henderson         unsigned r2 = a->r2;
26810c982a28SRichard Henderson         unsigned r1 = a->r1;
26820c982a28SRichard Henderson         unsigned rt = a->t;
26830c982a28SRichard Henderson 
26847aee8189SRichard Henderson         if (rt == 0) { /* NOP */
26857aee8189SRichard Henderson             cond_free(&ctx->null_cond);
26867aee8189SRichard Henderson             return true;
26877aee8189SRichard Henderson         }
26887aee8189SRichard Henderson         if (r2 == 0) { /* COPY */
2689b2167459SRichard Henderson             if (r1 == 0) {
26906fd0c7bcSRichard Henderson                 TCGv_i64 dest = dest_gpr(ctx, rt);
26916fd0c7bcSRichard Henderson                 tcg_gen_movi_i64(dest, 0);
2692b2167459SRichard Henderson                 save_gpr(ctx, rt, dest);
2693b2167459SRichard Henderson             } else {
2694b2167459SRichard Henderson                 save_gpr(ctx, rt, cpu_gr[r1]);
2695b2167459SRichard Henderson             }
2696b2167459SRichard Henderson             cond_free(&ctx->null_cond);
269731234768SRichard Henderson             return true;
2698b2167459SRichard Henderson         }
26997aee8189SRichard Henderson #ifndef CONFIG_USER_ONLY
27007aee8189SRichard Henderson         /* These are QEMU extensions and are nops in the real architecture:
27017aee8189SRichard Henderson          *
27027aee8189SRichard Henderson          * or %r10,%r10,%r10 -- idle loop; wait for interrupt
27037aee8189SRichard Henderson          * or %r31,%r31,%r31 -- death loop; offline cpu
27047aee8189SRichard Henderson          *                      currently implemented as idle.
27057aee8189SRichard Henderson          */
27067aee8189SRichard Henderson         if ((rt == 10 || rt == 31) && r1 == rt && r2 == rt) { /* PAUSE */
27077aee8189SRichard Henderson             /* No need to check for supervisor, as userland can only pause
27087aee8189SRichard Henderson                until the next timer interrupt.  */
27097aee8189SRichard Henderson             nullify_over(ctx);
27107aee8189SRichard Henderson 
27117aee8189SRichard Henderson             /* Advance the instruction queue.  */
2712741322f4SRichard Henderson             copy_iaoq_entry(ctx, cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
2713741322f4SRichard Henderson             copy_iaoq_entry(ctx, cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
27147aee8189SRichard Henderson             nullify_set(ctx, 0);
27157aee8189SRichard Henderson 
27167aee8189SRichard Henderson             /* Tell the qemu main loop to halt until this cpu has work.  */
2717ad75a51eSRichard Henderson             tcg_gen_st_i32(tcg_constant_i32(1), tcg_env,
271829dd6f64SRichard Henderson                            offsetof(CPUState, halted) - offsetof(HPPACPU, env));
27197aee8189SRichard Henderson             gen_excp_1(EXCP_HALTED);
27207aee8189SRichard Henderson             ctx->base.is_jmp = DISAS_NORETURN;
27217aee8189SRichard Henderson 
27227aee8189SRichard Henderson             return nullify_end(ctx);
27237aee8189SRichard Henderson         }
27247aee8189SRichard Henderson #endif
27257aee8189SRichard Henderson     }
27266fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_or_i64);
27277aee8189SRichard Henderson }
2728b2167459SRichard Henderson 
2729fa8e3bedSRichard Henderson static bool trans_xor(DisasContext *ctx, arg_rrr_cf_d *a)
2730b2167459SRichard Henderson {
27316fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_xor_i64);
27320c982a28SRichard Henderson }
27330c982a28SRichard Henderson 
2734345aa35fSRichard Henderson static bool trans_cmpclr(DisasContext *ctx, arg_rrr_cf_d *a)
27350c982a28SRichard Henderson {
27366fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
2737b2167459SRichard Henderson 
27380c982a28SRichard Henderson     if (a->cf) {
2739b2167459SRichard Henderson         nullify_over(ctx);
2740b2167459SRichard Henderson     }
27410c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27420c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
2743345aa35fSRichard Henderson     do_cmpclr(ctx, a->t, tcg_r1, tcg_r2, a->cf, a->d);
274431234768SRichard Henderson     return nullify_end(ctx);
2745b2167459SRichard Henderson }
2746b2167459SRichard Henderson 
2747af240753SRichard Henderson static bool trans_uxor(DisasContext *ctx, arg_rrr_cf_d *a)
2748b2167459SRichard Henderson {
274946bb3d46SRichard Henderson     TCGv_i64 tcg_r1, tcg_r2, dest;
2750b2167459SRichard Henderson 
27510c982a28SRichard Henderson     if (a->cf) {
2752b2167459SRichard Henderson         nullify_over(ctx);
2753b2167459SRichard Henderson     }
275446bb3d46SRichard Henderson 
27550c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27560c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
275746bb3d46SRichard Henderson     dest = dest_gpr(ctx, a->t);
275846bb3d46SRichard Henderson 
275946bb3d46SRichard Henderson     tcg_gen_xor_i64(dest, tcg_r1, tcg_r2);
276046bb3d46SRichard Henderson     save_gpr(ctx, a->t, dest);
276146bb3d46SRichard Henderson 
276246bb3d46SRichard Henderson     cond_free(&ctx->null_cond);
276346bb3d46SRichard Henderson     if (a->cf) {
276446bb3d46SRichard Henderson         ctx->null_cond = do_unit_zero_cond(a->cf, a->d, dest);
276546bb3d46SRichard Henderson     }
276646bb3d46SRichard Henderson 
276731234768SRichard Henderson     return nullify_end(ctx);
2768b2167459SRichard Henderson }
2769b2167459SRichard Henderson 
2770af240753SRichard Henderson static bool do_uaddcm(DisasContext *ctx, arg_rrr_cf_d *a, bool is_tc)
2771b2167459SRichard Henderson {
27726fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2, tmp;
2773b2167459SRichard Henderson 
2774ababac16SRichard Henderson     if (a->cf == 0) {
2775ababac16SRichard Henderson         tcg_r2 = load_gpr(ctx, a->r2);
2776ababac16SRichard Henderson         tmp = dest_gpr(ctx, a->t);
2777ababac16SRichard Henderson 
2778ababac16SRichard Henderson         if (a->r1 == 0) {
2779ababac16SRichard Henderson             /* UADDCM r0,src,dst is the common idiom for dst = ~src. */
2780ababac16SRichard Henderson             tcg_gen_not_i64(tmp, tcg_r2);
2781ababac16SRichard Henderson         } else {
2782ababac16SRichard Henderson             /*
2783ababac16SRichard Henderson              * Recall that r1 - r2 == r1 + ~r2 + 1.
2784ababac16SRichard Henderson              * Thus r1 + ~r2 == r1 - r2 - 1,
2785ababac16SRichard Henderson              * which does not require an extra temporary.
2786ababac16SRichard Henderson              */
2787ababac16SRichard Henderson             tcg_r1 = load_gpr(ctx, a->r1);
2788ababac16SRichard Henderson             tcg_gen_sub_i64(tmp, tcg_r1, tcg_r2);
2789ababac16SRichard Henderson             tcg_gen_subi_i64(tmp, tmp, 1);
2790b2167459SRichard Henderson         }
2791ababac16SRichard Henderson         save_gpr(ctx, a->t, tmp);
2792ababac16SRichard Henderson         cond_free(&ctx->null_cond);
2793ababac16SRichard Henderson         return true;
2794ababac16SRichard Henderson     }
2795ababac16SRichard Henderson 
2796ababac16SRichard Henderson     nullify_over(ctx);
27970c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27980c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
2799aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
28006fd0c7bcSRichard Henderson     tcg_gen_not_i64(tmp, tcg_r2);
280146bb3d46SRichard Henderson     do_unit_addsub(ctx, a->t, tcg_r1, tmp, a->cf, a->d, is_tc, true);
280231234768SRichard Henderson     return nullify_end(ctx);
2803b2167459SRichard Henderson }
2804b2167459SRichard Henderson 
2805af240753SRichard Henderson static bool trans_uaddcm(DisasContext *ctx, arg_rrr_cf_d *a)
2806b2167459SRichard Henderson {
28070c982a28SRichard Henderson     return do_uaddcm(ctx, a, false);
28080c982a28SRichard Henderson }
28090c982a28SRichard Henderson 
2810af240753SRichard Henderson static bool trans_uaddcm_tc(DisasContext *ctx, arg_rrr_cf_d *a)
28110c982a28SRichard Henderson {
28120c982a28SRichard Henderson     return do_uaddcm(ctx, a, true);
28130c982a28SRichard Henderson }
28140c982a28SRichard Henderson 
2815af240753SRichard Henderson static bool do_dcor(DisasContext *ctx, arg_rr_cf_d *a, bool is_i)
28160c982a28SRichard Henderson {
28176fd0c7bcSRichard Henderson     TCGv_i64 tmp;
2818b2167459SRichard Henderson 
2819b2167459SRichard Henderson     nullify_over(ctx);
2820b2167459SRichard Henderson 
2821aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
2822d0ae87a2SRichard Henderson     tcg_gen_extract2_i64(tmp, cpu_psw_cb, cpu_psw_cb_msb, 4);
2823b2167459SRichard Henderson     if (!is_i) {
28246fd0c7bcSRichard Henderson         tcg_gen_not_i64(tmp, tmp);
2825b2167459SRichard Henderson     }
28266fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, (uint64_t)0x1111111111111111ull);
28276fd0c7bcSRichard Henderson     tcg_gen_muli_i64(tmp, tmp, 6);
282846bb3d46SRichard Henderson     do_unit_addsub(ctx, a->t, load_gpr(ctx, a->r), tmp,
282946bb3d46SRichard Henderson                    a->cf, a->d, false, is_i);
283031234768SRichard Henderson     return nullify_end(ctx);
2831b2167459SRichard Henderson }
2832b2167459SRichard Henderson 
2833af240753SRichard Henderson static bool trans_dcor(DisasContext *ctx, arg_rr_cf_d *a)
2834b2167459SRichard Henderson {
28350c982a28SRichard Henderson     return do_dcor(ctx, a, false);
28360c982a28SRichard Henderson }
28370c982a28SRichard Henderson 
2838af240753SRichard Henderson static bool trans_dcor_i(DisasContext *ctx, arg_rr_cf_d *a)
28390c982a28SRichard Henderson {
28400c982a28SRichard Henderson     return do_dcor(ctx, a, true);
28410c982a28SRichard Henderson }
28420c982a28SRichard Henderson 
28430c982a28SRichard Henderson static bool trans_ds(DisasContext *ctx, arg_rrr_cf *a)
28440c982a28SRichard Henderson {
2845a4db4a78SRichard Henderson     TCGv_i64 dest, add1, add2, addc, in1, in2;
28466fd0c7bcSRichard Henderson     TCGv_i64 cout;
2847b2167459SRichard Henderson 
2848b2167459SRichard Henderson     nullify_over(ctx);
2849b2167459SRichard Henderson 
28500c982a28SRichard Henderson     in1 = load_gpr(ctx, a->r1);
28510c982a28SRichard Henderson     in2 = load_gpr(ctx, a->r2);
2852b2167459SRichard Henderson 
2853aac0f603SRichard Henderson     add1 = tcg_temp_new_i64();
2854aac0f603SRichard Henderson     add2 = tcg_temp_new_i64();
2855aac0f603SRichard Henderson     addc = tcg_temp_new_i64();
2856aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
2857b2167459SRichard Henderson 
2858b2167459SRichard Henderson     /* Form R1 << 1 | PSW[CB]{8}.  */
28596fd0c7bcSRichard Henderson     tcg_gen_add_i64(add1, in1, in1);
28606fd0c7bcSRichard Henderson     tcg_gen_add_i64(add1, add1, get_psw_carry(ctx, false));
2861b2167459SRichard Henderson 
286272ca8753SRichard Henderson     /*
286372ca8753SRichard Henderson      * Add or subtract R2, depending on PSW[V].  Proper computation of
286472ca8753SRichard Henderson      * carry requires that we subtract via + ~R2 + 1, as described in
286572ca8753SRichard Henderson      * the manual.  By extracting and masking V, we can produce the
286672ca8753SRichard Henderson      * proper inputs to the addition without movcond.
286772ca8753SRichard Henderson      */
28686fd0c7bcSRichard Henderson     tcg_gen_sextract_i64(addc, cpu_psw_v, 31, 1);
28696fd0c7bcSRichard Henderson     tcg_gen_xor_i64(add2, in2, addc);
28706fd0c7bcSRichard Henderson     tcg_gen_andi_i64(addc, addc, 1);
287172ca8753SRichard Henderson 
2872a4db4a78SRichard Henderson     tcg_gen_add2_i64(dest, cpu_psw_cb_msb, add1, ctx->zero, add2, ctx->zero);
2873a4db4a78SRichard Henderson     tcg_gen_add2_i64(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb,
2874a4db4a78SRichard Henderson                      addc, ctx->zero);
2875b2167459SRichard Henderson 
2876b2167459SRichard Henderson     /* Write back the result register.  */
28770c982a28SRichard Henderson     save_gpr(ctx, a->t, dest);
2878b2167459SRichard Henderson 
2879b2167459SRichard Henderson     /* Write back PSW[CB].  */
28806fd0c7bcSRichard Henderson     tcg_gen_xor_i64(cpu_psw_cb, add1, add2);
28816fd0c7bcSRichard Henderson     tcg_gen_xor_i64(cpu_psw_cb, cpu_psw_cb, dest);
2882b2167459SRichard Henderson 
2883b2167459SRichard Henderson     /* Write back PSW[V] for the division step.  */
288472ca8753SRichard Henderson     cout = get_psw_carry(ctx, false);
28856fd0c7bcSRichard Henderson     tcg_gen_neg_i64(cpu_psw_v, cout);
28866fd0c7bcSRichard Henderson     tcg_gen_xor_i64(cpu_psw_v, cpu_psw_v, in2);
2887b2167459SRichard Henderson 
2888b2167459SRichard Henderson     /* Install the new nullification.  */
28890c982a28SRichard Henderson     if (a->cf) {
28906fd0c7bcSRichard Henderson         TCGv_i64 sv = NULL;
2891b47a4a02SSven Schnelle         if (cond_need_sv(a->cf >> 1)) {
2892b2167459SRichard Henderson             /* ??? The lshift is supposed to contribute to overflow.  */
2893b2167459SRichard Henderson             sv = do_add_sv(ctx, dest, add1, add2);
2894b2167459SRichard Henderson         }
2895a751eb31SRichard Henderson         ctx->null_cond = do_cond(ctx, a->cf, false, dest, cout, sv);
2896b2167459SRichard Henderson     }
2897b2167459SRichard Henderson 
289831234768SRichard Henderson     return nullify_end(ctx);
2899b2167459SRichard Henderson }
2900b2167459SRichard Henderson 
29010588e061SRichard Henderson static bool trans_addi(DisasContext *ctx, arg_rri_cf *a)
2902b2167459SRichard Henderson {
29030588e061SRichard Henderson     return do_add_imm(ctx, a, false, false);
29040588e061SRichard Henderson }
29050588e061SRichard Henderson 
29060588e061SRichard Henderson static bool trans_addi_tsv(DisasContext *ctx, arg_rri_cf *a)
29070588e061SRichard Henderson {
29080588e061SRichard Henderson     return do_add_imm(ctx, a, true, false);
29090588e061SRichard Henderson }
29100588e061SRichard Henderson 
29110588e061SRichard Henderson static bool trans_addi_tc(DisasContext *ctx, arg_rri_cf *a)
29120588e061SRichard Henderson {
29130588e061SRichard Henderson     return do_add_imm(ctx, a, false, true);
29140588e061SRichard Henderson }
29150588e061SRichard Henderson 
29160588e061SRichard Henderson static bool trans_addi_tc_tsv(DisasContext *ctx, arg_rri_cf *a)
29170588e061SRichard Henderson {
29180588e061SRichard Henderson     return do_add_imm(ctx, a, true, true);
29190588e061SRichard Henderson }
29200588e061SRichard Henderson 
29210588e061SRichard Henderson static bool trans_subi(DisasContext *ctx, arg_rri_cf *a)
29220588e061SRichard Henderson {
29230588e061SRichard Henderson     return do_sub_imm(ctx, a, false);
29240588e061SRichard Henderson }
29250588e061SRichard Henderson 
29260588e061SRichard Henderson static bool trans_subi_tsv(DisasContext *ctx, arg_rri_cf *a)
29270588e061SRichard Henderson {
29280588e061SRichard Henderson     return do_sub_imm(ctx, a, true);
29290588e061SRichard Henderson }
29300588e061SRichard Henderson 
2931345aa35fSRichard Henderson static bool trans_cmpiclr(DisasContext *ctx, arg_rri_cf_d *a)
29320588e061SRichard Henderson {
29336fd0c7bcSRichard Henderson     TCGv_i64 tcg_im, tcg_r2;
2934b2167459SRichard Henderson 
29350588e061SRichard Henderson     if (a->cf) {
2936b2167459SRichard Henderson         nullify_over(ctx);
2937b2167459SRichard Henderson     }
2938b2167459SRichard Henderson 
29396fd0c7bcSRichard Henderson     tcg_im = tcg_constant_i64(a->i);
29400588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
2941345aa35fSRichard Henderson     do_cmpclr(ctx, a->t, tcg_im, tcg_r2, a->cf, a->d);
2942b2167459SRichard Henderson 
294331234768SRichard Henderson     return nullify_end(ctx);
2944b2167459SRichard Henderson }
2945b2167459SRichard Henderson 
29460843563fSRichard Henderson static bool do_multimedia(DisasContext *ctx, arg_rrr *a,
29470843563fSRichard Henderson                           void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
29480843563fSRichard Henderson {
29490843563fSRichard Henderson     TCGv_i64 r1, r2, dest;
29500843563fSRichard Henderson 
29510843563fSRichard Henderson     if (!ctx->is_pa20) {
29520843563fSRichard Henderson         return false;
29530843563fSRichard Henderson     }
29540843563fSRichard Henderson 
29550843563fSRichard Henderson     nullify_over(ctx);
29560843563fSRichard Henderson 
29570843563fSRichard Henderson     r1 = load_gpr(ctx, a->r1);
29580843563fSRichard Henderson     r2 = load_gpr(ctx, a->r2);
29590843563fSRichard Henderson     dest = dest_gpr(ctx, a->t);
29600843563fSRichard Henderson 
29610843563fSRichard Henderson     fn(dest, r1, r2);
29620843563fSRichard Henderson     save_gpr(ctx, a->t, dest);
29630843563fSRichard Henderson 
29640843563fSRichard Henderson     return nullify_end(ctx);
29650843563fSRichard Henderson }
29660843563fSRichard Henderson 
2967151f309bSRichard Henderson static bool do_multimedia_sh(DisasContext *ctx, arg_rri *a,
2968151f309bSRichard Henderson                              void (*fn)(TCGv_i64, TCGv_i64, int64_t))
2969151f309bSRichard Henderson {
2970151f309bSRichard Henderson     TCGv_i64 r, dest;
2971151f309bSRichard Henderson 
2972151f309bSRichard Henderson     if (!ctx->is_pa20) {
2973151f309bSRichard Henderson         return false;
2974151f309bSRichard Henderson     }
2975151f309bSRichard Henderson 
2976151f309bSRichard Henderson     nullify_over(ctx);
2977151f309bSRichard Henderson 
2978151f309bSRichard Henderson     r = load_gpr(ctx, a->r);
2979151f309bSRichard Henderson     dest = dest_gpr(ctx, a->t);
2980151f309bSRichard Henderson 
2981151f309bSRichard Henderson     fn(dest, r, a->i);
2982151f309bSRichard Henderson     save_gpr(ctx, a->t, dest);
2983151f309bSRichard Henderson 
2984151f309bSRichard Henderson     return nullify_end(ctx);
2985151f309bSRichard Henderson }
2986151f309bSRichard Henderson 
29873bbb8e48SRichard Henderson static bool do_multimedia_shadd(DisasContext *ctx, arg_rrr_sh *a,
29883bbb8e48SRichard Henderson                                 void (*fn)(TCGv_i64, TCGv_i64,
29893bbb8e48SRichard Henderson                                            TCGv_i64, TCGv_i32))
29903bbb8e48SRichard Henderson {
29913bbb8e48SRichard Henderson     TCGv_i64 r1, r2, dest;
29923bbb8e48SRichard Henderson 
29933bbb8e48SRichard Henderson     if (!ctx->is_pa20) {
29943bbb8e48SRichard Henderson         return false;
29953bbb8e48SRichard Henderson     }
29963bbb8e48SRichard Henderson 
29973bbb8e48SRichard Henderson     nullify_over(ctx);
29983bbb8e48SRichard Henderson 
29993bbb8e48SRichard Henderson     r1 = load_gpr(ctx, a->r1);
30003bbb8e48SRichard Henderson     r2 = load_gpr(ctx, a->r2);
30013bbb8e48SRichard Henderson     dest = dest_gpr(ctx, a->t);
30023bbb8e48SRichard Henderson 
30033bbb8e48SRichard Henderson     fn(dest, r1, r2, tcg_constant_i32(a->sh));
30043bbb8e48SRichard Henderson     save_gpr(ctx, a->t, dest);
30053bbb8e48SRichard Henderson 
30063bbb8e48SRichard Henderson     return nullify_end(ctx);
30073bbb8e48SRichard Henderson }
30083bbb8e48SRichard Henderson 
30090843563fSRichard Henderson static bool trans_hadd(DisasContext *ctx, arg_rrr *a)
30100843563fSRichard Henderson {
30110843563fSRichard Henderson     return do_multimedia(ctx, a, tcg_gen_vec_add16_i64);
30120843563fSRichard Henderson }
30130843563fSRichard Henderson 
30140843563fSRichard Henderson static bool trans_hadd_ss(DisasContext *ctx, arg_rrr *a)
30150843563fSRichard Henderson {
30160843563fSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hadd_ss);
30170843563fSRichard Henderson }
30180843563fSRichard Henderson 
30190843563fSRichard Henderson static bool trans_hadd_us(DisasContext *ctx, arg_rrr *a)
30200843563fSRichard Henderson {
30210843563fSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hadd_us);
30220843563fSRichard Henderson }
30230843563fSRichard Henderson 
30241b3cb7c8SRichard Henderson static bool trans_havg(DisasContext *ctx, arg_rrr *a)
30251b3cb7c8SRichard Henderson {
30261b3cb7c8SRichard Henderson     return do_multimedia(ctx, a, gen_helper_havg);
30271b3cb7c8SRichard Henderson }
30281b3cb7c8SRichard Henderson 
3029151f309bSRichard Henderson static bool trans_hshl(DisasContext *ctx, arg_rri *a)
3030151f309bSRichard Henderson {
3031151f309bSRichard Henderson     return do_multimedia_sh(ctx, a, tcg_gen_vec_shl16i_i64);
3032151f309bSRichard Henderson }
3033151f309bSRichard Henderson 
3034151f309bSRichard Henderson static bool trans_hshr_s(DisasContext *ctx, arg_rri *a)
3035151f309bSRichard Henderson {
3036151f309bSRichard Henderson     return do_multimedia_sh(ctx, a, tcg_gen_vec_sar16i_i64);
3037151f309bSRichard Henderson }
3038151f309bSRichard Henderson 
3039151f309bSRichard Henderson static bool trans_hshr_u(DisasContext *ctx, arg_rri *a)
3040151f309bSRichard Henderson {
3041151f309bSRichard Henderson     return do_multimedia_sh(ctx, a, tcg_gen_vec_shr16i_i64);
3042151f309bSRichard Henderson }
3043151f309bSRichard Henderson 
30443bbb8e48SRichard Henderson static bool trans_hshladd(DisasContext *ctx, arg_rrr_sh *a)
30453bbb8e48SRichard Henderson {
30463bbb8e48SRichard Henderson     return do_multimedia_shadd(ctx, a, gen_helper_hshladd);
30473bbb8e48SRichard Henderson }
30483bbb8e48SRichard Henderson 
30493bbb8e48SRichard Henderson static bool trans_hshradd(DisasContext *ctx, arg_rrr_sh *a)
30503bbb8e48SRichard Henderson {
30513bbb8e48SRichard Henderson     return do_multimedia_shadd(ctx, a, gen_helper_hshradd);
30523bbb8e48SRichard Henderson }
30533bbb8e48SRichard Henderson 
305410c9e58dSRichard Henderson static bool trans_hsub(DisasContext *ctx, arg_rrr *a)
305510c9e58dSRichard Henderson {
305610c9e58dSRichard Henderson     return do_multimedia(ctx, a, tcg_gen_vec_sub16_i64);
305710c9e58dSRichard Henderson }
305810c9e58dSRichard Henderson 
305910c9e58dSRichard Henderson static bool trans_hsub_ss(DisasContext *ctx, arg_rrr *a)
306010c9e58dSRichard Henderson {
306110c9e58dSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hsub_ss);
306210c9e58dSRichard Henderson }
306310c9e58dSRichard Henderson 
306410c9e58dSRichard Henderson static bool trans_hsub_us(DisasContext *ctx, arg_rrr *a)
306510c9e58dSRichard Henderson {
306610c9e58dSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hsub_us);
306710c9e58dSRichard Henderson }
306810c9e58dSRichard Henderson 
3069c2a7ee3fSRichard Henderson static void gen_mixh_l(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3070c2a7ee3fSRichard Henderson {
3071c2a7ee3fSRichard Henderson     uint64_t mask = 0xffff0000ffff0000ull;
3072c2a7ee3fSRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3073c2a7ee3fSRichard Henderson 
3074c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(tmp, r2, mask);
3075c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(dst, r1, mask);
3076c2a7ee3fSRichard Henderson     tcg_gen_shri_i64(tmp, tmp, 16);
3077c2a7ee3fSRichard Henderson     tcg_gen_or_i64(dst, dst, tmp);
3078c2a7ee3fSRichard Henderson }
3079c2a7ee3fSRichard Henderson 
3080c2a7ee3fSRichard Henderson static bool trans_mixh_l(DisasContext *ctx, arg_rrr *a)
3081c2a7ee3fSRichard Henderson {
3082c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixh_l);
3083c2a7ee3fSRichard Henderson }
3084c2a7ee3fSRichard Henderson 
3085c2a7ee3fSRichard Henderson static void gen_mixh_r(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3086c2a7ee3fSRichard Henderson {
3087c2a7ee3fSRichard Henderson     uint64_t mask = 0x0000ffff0000ffffull;
3088c2a7ee3fSRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3089c2a7ee3fSRichard Henderson 
3090c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(tmp, r1, mask);
3091c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(dst, r2, mask);
3092c2a7ee3fSRichard Henderson     tcg_gen_shli_i64(tmp, tmp, 16);
3093c2a7ee3fSRichard Henderson     tcg_gen_or_i64(dst, dst, tmp);
3094c2a7ee3fSRichard Henderson }
3095c2a7ee3fSRichard Henderson 
3096c2a7ee3fSRichard Henderson static bool trans_mixh_r(DisasContext *ctx, arg_rrr *a)
3097c2a7ee3fSRichard Henderson {
3098c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixh_r);
3099c2a7ee3fSRichard Henderson }
3100c2a7ee3fSRichard Henderson 
3101c2a7ee3fSRichard Henderson static void gen_mixw_l(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3102c2a7ee3fSRichard Henderson {
3103c2a7ee3fSRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3104c2a7ee3fSRichard Henderson 
3105c2a7ee3fSRichard Henderson     tcg_gen_shri_i64(tmp, r2, 32);
3106c2a7ee3fSRichard Henderson     tcg_gen_deposit_i64(dst, r1, tmp, 0, 32);
3107c2a7ee3fSRichard Henderson }
3108c2a7ee3fSRichard Henderson 
3109c2a7ee3fSRichard Henderson static bool trans_mixw_l(DisasContext *ctx, arg_rrr *a)
3110c2a7ee3fSRichard Henderson {
3111c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixw_l);
3112c2a7ee3fSRichard Henderson }
3113c2a7ee3fSRichard Henderson 
3114c2a7ee3fSRichard Henderson static void gen_mixw_r(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3115c2a7ee3fSRichard Henderson {
3116c2a7ee3fSRichard Henderson     tcg_gen_deposit_i64(dst, r2, r1, 32, 32);
3117c2a7ee3fSRichard Henderson }
3118c2a7ee3fSRichard Henderson 
3119c2a7ee3fSRichard Henderson static bool trans_mixw_r(DisasContext *ctx, arg_rrr *a)
3120c2a7ee3fSRichard Henderson {
3121c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixw_r);
3122c2a7ee3fSRichard Henderson }
3123c2a7ee3fSRichard Henderson 
31244e7abdb1SRichard Henderson static bool trans_permh(DisasContext *ctx, arg_permh *a)
31254e7abdb1SRichard Henderson {
31264e7abdb1SRichard Henderson     TCGv_i64 r, t0, t1, t2, t3;
31274e7abdb1SRichard Henderson 
31284e7abdb1SRichard Henderson     if (!ctx->is_pa20) {
31294e7abdb1SRichard Henderson         return false;
31304e7abdb1SRichard Henderson     }
31314e7abdb1SRichard Henderson 
31324e7abdb1SRichard Henderson     nullify_over(ctx);
31334e7abdb1SRichard Henderson 
31344e7abdb1SRichard Henderson     r = load_gpr(ctx, a->r1);
31354e7abdb1SRichard Henderson     t0 = tcg_temp_new_i64();
31364e7abdb1SRichard Henderson     t1 = tcg_temp_new_i64();
31374e7abdb1SRichard Henderson     t2 = tcg_temp_new_i64();
31384e7abdb1SRichard Henderson     t3 = tcg_temp_new_i64();
31394e7abdb1SRichard Henderson 
31404e7abdb1SRichard Henderson     tcg_gen_extract_i64(t0, r, (3 - a->c0) * 16, 16);
31414e7abdb1SRichard Henderson     tcg_gen_extract_i64(t1, r, (3 - a->c1) * 16, 16);
31424e7abdb1SRichard Henderson     tcg_gen_extract_i64(t2, r, (3 - a->c2) * 16, 16);
31434e7abdb1SRichard Henderson     tcg_gen_extract_i64(t3, r, (3 - a->c3) * 16, 16);
31444e7abdb1SRichard Henderson 
31454e7abdb1SRichard Henderson     tcg_gen_deposit_i64(t0, t1, t0, 16, 48);
31464e7abdb1SRichard Henderson     tcg_gen_deposit_i64(t2, t3, t2, 16, 48);
31474e7abdb1SRichard Henderson     tcg_gen_deposit_i64(t0, t2, t0, 32, 32);
31484e7abdb1SRichard Henderson 
31494e7abdb1SRichard Henderson     save_gpr(ctx, a->t, t0);
31504e7abdb1SRichard Henderson     return nullify_end(ctx);
31514e7abdb1SRichard Henderson }
31524e7abdb1SRichard Henderson 
31531cd012a5SRichard Henderson static bool trans_ld(DisasContext *ctx, arg_ldst *a)
315496d6407fSRichard Henderson {
3155b5caa17cSRichard Henderson     if (ctx->is_pa20) {
3156b5caa17cSRichard Henderson        /*
3157b5caa17cSRichard Henderson         * With pa20, LDB, LDH, LDW, LDD to %g0 are prefetches.
3158b5caa17cSRichard Henderson         * Any base modification still occurs.
3159b5caa17cSRichard Henderson         */
3160b5caa17cSRichard Henderson         if (a->t == 0) {
3161b5caa17cSRichard Henderson             return trans_nop_addrx(ctx, a);
3162b5caa17cSRichard Henderson         }
3163b5caa17cSRichard Henderson     } else if (a->size > MO_32) {
31640786a3b6SHelge Deller         return gen_illegal(ctx);
3165c53e401eSRichard Henderson     }
31661cd012a5SRichard Henderson     return do_load(ctx, a->t, a->b, a->x, a->scale ? a->size : 0,
31671cd012a5SRichard Henderson                    a->disp, a->sp, a->m, a->size | MO_TE);
316896d6407fSRichard Henderson }
316996d6407fSRichard Henderson 
31701cd012a5SRichard Henderson static bool trans_st(DisasContext *ctx, arg_ldst *a)
317196d6407fSRichard Henderson {
31721cd012a5SRichard Henderson     assert(a->x == 0 && a->scale == 0);
3173c53e401eSRichard Henderson     if (!ctx->is_pa20 && a->size > MO_32) {
31740786a3b6SHelge Deller         return gen_illegal(ctx);
317596d6407fSRichard Henderson     }
3176c53e401eSRichard Henderson     return do_store(ctx, a->t, a->b, a->disp, a->sp, a->m, a->size | MO_TE);
31770786a3b6SHelge Deller }
317896d6407fSRichard Henderson 
31791cd012a5SRichard Henderson static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
318096d6407fSRichard Henderson {
3181b1af755cSRichard Henderson     MemOp mop = MO_TE | MO_ALIGN | a->size;
3182a4db4a78SRichard Henderson     TCGv_i64 dest, ofs;
31836fd0c7bcSRichard Henderson     TCGv_i64 addr;
318496d6407fSRichard Henderson 
3185c53e401eSRichard Henderson     if (!ctx->is_pa20 && a->size > MO_32) {
318651416c4eSRichard Henderson         return gen_illegal(ctx);
318751416c4eSRichard Henderson     }
318851416c4eSRichard Henderson 
318996d6407fSRichard Henderson     nullify_over(ctx);
319096d6407fSRichard Henderson 
31911cd012a5SRichard Henderson     if (a->m) {
319286f8d05fSRichard Henderson         /* Base register modification.  Make sure if RT == RB,
319386f8d05fSRichard Henderson            we see the result of the load.  */
3194aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
319596d6407fSRichard Henderson     } else {
31961cd012a5SRichard Henderson         dest = dest_gpr(ctx, a->t);
319796d6407fSRichard Henderson     }
319896d6407fSRichard Henderson 
3199c3ea1996SSven Schnelle     form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? 3 : 0,
320017fe594cSRichard Henderson              a->disp, a->sp, a->m, MMU_DISABLED(ctx));
3201b1af755cSRichard Henderson 
3202b1af755cSRichard Henderson     /*
3203b1af755cSRichard Henderson      * For hppa1.1, LDCW is undefined unless aligned mod 16.
3204b1af755cSRichard Henderson      * However actual hardware succeeds with aligned mod 4.
3205b1af755cSRichard Henderson      * Detect this case and log a GUEST_ERROR.
3206b1af755cSRichard Henderson      *
3207b1af755cSRichard Henderson      * TODO: HPPA64 relaxes the over-alignment requirement
3208b1af755cSRichard Henderson      * with the ,co completer.
3209b1af755cSRichard Henderson      */
3210b1af755cSRichard Henderson     gen_helper_ldc_check(addr);
3211b1af755cSRichard Henderson 
3212a4db4a78SRichard Henderson     tcg_gen_atomic_xchg_i64(dest, addr, ctx->zero, ctx->mmu_idx, mop);
3213b1af755cSRichard Henderson 
32141cd012a5SRichard Henderson     if (a->m) {
32151cd012a5SRichard Henderson         save_gpr(ctx, a->b, ofs);
321696d6407fSRichard Henderson     }
32171cd012a5SRichard Henderson     save_gpr(ctx, a->t, dest);
321896d6407fSRichard Henderson 
321931234768SRichard Henderson     return nullify_end(ctx);
322096d6407fSRichard Henderson }
322196d6407fSRichard Henderson 
32221cd012a5SRichard Henderson static bool trans_stby(DisasContext *ctx, arg_stby *a)
322396d6407fSRichard Henderson {
32246fd0c7bcSRichard Henderson     TCGv_i64 ofs, val;
32256fd0c7bcSRichard Henderson     TCGv_i64 addr;
322696d6407fSRichard Henderson 
322796d6407fSRichard Henderson     nullify_over(ctx);
322896d6407fSRichard Henderson 
32291cd012a5SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
323017fe594cSRichard Henderson              MMU_DISABLED(ctx));
32311cd012a5SRichard Henderson     val = load_gpr(ctx, a->r);
32321cd012a5SRichard Henderson     if (a->a) {
3233f9f46db4SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3234ad75a51eSRichard Henderson             gen_helper_stby_e_parallel(tcg_env, addr, val);
3235f9f46db4SEmilio G. Cota         } else {
3236ad75a51eSRichard Henderson             gen_helper_stby_e(tcg_env, addr, val);
3237f9f46db4SEmilio G. Cota         }
3238f9f46db4SEmilio G. Cota     } else {
3239f9f46db4SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3240ad75a51eSRichard Henderson             gen_helper_stby_b_parallel(tcg_env, addr, val);
324196d6407fSRichard Henderson         } else {
3242ad75a51eSRichard Henderson             gen_helper_stby_b(tcg_env, addr, val);
324396d6407fSRichard Henderson         }
3244f9f46db4SEmilio G. Cota     }
32451cd012a5SRichard Henderson     if (a->m) {
32466fd0c7bcSRichard Henderson         tcg_gen_andi_i64(ofs, ofs, ~3);
32471cd012a5SRichard Henderson         save_gpr(ctx, a->b, ofs);
324896d6407fSRichard Henderson     }
324996d6407fSRichard Henderson 
325031234768SRichard Henderson     return nullify_end(ctx);
325196d6407fSRichard Henderson }
325296d6407fSRichard Henderson 
325325460fc5SRichard Henderson static bool trans_stdby(DisasContext *ctx, arg_stby *a)
325425460fc5SRichard Henderson {
32556fd0c7bcSRichard Henderson     TCGv_i64 ofs, val;
32566fd0c7bcSRichard Henderson     TCGv_i64 addr;
325725460fc5SRichard Henderson 
325825460fc5SRichard Henderson     if (!ctx->is_pa20) {
325925460fc5SRichard Henderson         return false;
326025460fc5SRichard Henderson     }
326125460fc5SRichard Henderson     nullify_over(ctx);
326225460fc5SRichard Henderson 
326325460fc5SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
326417fe594cSRichard Henderson              MMU_DISABLED(ctx));
326525460fc5SRichard Henderson     val = load_gpr(ctx, a->r);
326625460fc5SRichard Henderson     if (a->a) {
326725460fc5SRichard Henderson         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
326825460fc5SRichard Henderson             gen_helper_stdby_e_parallel(tcg_env, addr, val);
326925460fc5SRichard Henderson         } else {
327025460fc5SRichard Henderson             gen_helper_stdby_e(tcg_env, addr, val);
327125460fc5SRichard Henderson         }
327225460fc5SRichard Henderson     } else {
327325460fc5SRichard Henderson         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
327425460fc5SRichard Henderson             gen_helper_stdby_b_parallel(tcg_env, addr, val);
327525460fc5SRichard Henderson         } else {
327625460fc5SRichard Henderson             gen_helper_stdby_b(tcg_env, addr, val);
327725460fc5SRichard Henderson         }
327825460fc5SRichard Henderson     }
327925460fc5SRichard Henderson     if (a->m) {
32806fd0c7bcSRichard Henderson         tcg_gen_andi_i64(ofs, ofs, ~7);
328125460fc5SRichard Henderson         save_gpr(ctx, a->b, ofs);
328225460fc5SRichard Henderson     }
328325460fc5SRichard Henderson 
328425460fc5SRichard Henderson     return nullify_end(ctx);
328525460fc5SRichard Henderson }
328625460fc5SRichard Henderson 
32871cd012a5SRichard Henderson static bool trans_lda(DisasContext *ctx, arg_ldst *a)
3288d0a851ccSRichard Henderson {
3289d0a851ccSRichard Henderson     int hold_mmu_idx = ctx->mmu_idx;
3290d0a851ccSRichard Henderson 
3291d0a851ccSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
3292451d993dSRichard Henderson     ctx->mmu_idx = ctx->tb_flags & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX;
32931cd012a5SRichard Henderson     trans_ld(ctx, a);
3294d0a851ccSRichard Henderson     ctx->mmu_idx = hold_mmu_idx;
329531234768SRichard Henderson     return true;
3296d0a851ccSRichard Henderson }
3297d0a851ccSRichard Henderson 
32981cd012a5SRichard Henderson static bool trans_sta(DisasContext *ctx, arg_ldst *a)
3299d0a851ccSRichard Henderson {
3300d0a851ccSRichard Henderson     int hold_mmu_idx = ctx->mmu_idx;
3301d0a851ccSRichard Henderson 
3302d0a851ccSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
3303451d993dSRichard Henderson     ctx->mmu_idx = ctx->tb_flags & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX;
33041cd012a5SRichard Henderson     trans_st(ctx, a);
3305d0a851ccSRichard Henderson     ctx->mmu_idx = hold_mmu_idx;
330631234768SRichard Henderson     return true;
3307d0a851ccSRichard Henderson }
330895412a61SRichard Henderson 
33090588e061SRichard Henderson static bool trans_ldil(DisasContext *ctx, arg_ldil *a)
3310b2167459SRichard Henderson {
33116fd0c7bcSRichard Henderson     TCGv_i64 tcg_rt = dest_gpr(ctx, a->t);
3312b2167459SRichard Henderson 
33136fd0c7bcSRichard Henderson     tcg_gen_movi_i64(tcg_rt, a->i);
33140588e061SRichard Henderson     save_gpr(ctx, a->t, tcg_rt);
3315b2167459SRichard Henderson     cond_free(&ctx->null_cond);
331631234768SRichard Henderson     return true;
3317b2167459SRichard Henderson }
3318b2167459SRichard Henderson 
33190588e061SRichard Henderson static bool trans_addil(DisasContext *ctx, arg_addil *a)
3320b2167459SRichard Henderson {
33216fd0c7bcSRichard Henderson     TCGv_i64 tcg_rt = load_gpr(ctx, a->r);
33226fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1 = dest_gpr(ctx, 1);
3323b2167459SRichard Henderson 
33246fd0c7bcSRichard Henderson     tcg_gen_addi_i64(tcg_r1, tcg_rt, a->i);
3325b2167459SRichard Henderson     save_gpr(ctx, 1, tcg_r1);
3326b2167459SRichard Henderson     cond_free(&ctx->null_cond);
332731234768SRichard Henderson     return true;
3328b2167459SRichard Henderson }
3329b2167459SRichard Henderson 
33300588e061SRichard Henderson static bool trans_ldo(DisasContext *ctx, arg_ldo *a)
3331b2167459SRichard Henderson {
33326fd0c7bcSRichard Henderson     TCGv_i64 tcg_rt = dest_gpr(ctx, a->t);
3333b2167459SRichard Henderson 
3334b2167459SRichard Henderson     /* Special case rb == 0, for the LDI pseudo-op.
3335d265360fSRichard Henderson        The COPY pseudo-op is handled for free within tcg_gen_addi_i64.  */
33360588e061SRichard Henderson     if (a->b == 0) {
33376fd0c7bcSRichard Henderson         tcg_gen_movi_i64(tcg_rt, a->i);
3338b2167459SRichard Henderson     } else {
33396fd0c7bcSRichard Henderson         tcg_gen_addi_i64(tcg_rt, cpu_gr[a->b], a->i);
3340b2167459SRichard Henderson     }
33410588e061SRichard Henderson     save_gpr(ctx, a->t, tcg_rt);
3342b2167459SRichard Henderson     cond_free(&ctx->null_cond);
334331234768SRichard Henderson     return true;
3344b2167459SRichard Henderson }
3345b2167459SRichard Henderson 
33466fd0c7bcSRichard Henderson static bool do_cmpb(DisasContext *ctx, unsigned r, TCGv_i64 in1,
3347e9efd4bcSRichard Henderson                     unsigned c, unsigned f, bool d, unsigned n, int disp)
334898cd9ca7SRichard Henderson {
33496fd0c7bcSRichard Henderson     TCGv_i64 dest, in2, sv;
335098cd9ca7SRichard Henderson     DisasCond cond;
335198cd9ca7SRichard Henderson 
335298cd9ca7SRichard Henderson     in2 = load_gpr(ctx, r);
3353aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
335498cd9ca7SRichard Henderson 
33556fd0c7bcSRichard Henderson     tcg_gen_sub_i64(dest, in1, in2);
335698cd9ca7SRichard Henderson 
3357f764718dSRichard Henderson     sv = NULL;
3358b47a4a02SSven Schnelle     if (cond_need_sv(c)) {
335998cd9ca7SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
336098cd9ca7SRichard Henderson     }
336198cd9ca7SRichard Henderson 
33624fe9533aSRichard Henderson     cond = do_sub_cond(ctx, c * 2 + f, d, dest, in1, in2, sv);
336301afb7beSRichard Henderson     return do_cbranch(ctx, disp, n, &cond);
336498cd9ca7SRichard Henderson }
336598cd9ca7SRichard Henderson 
336601afb7beSRichard Henderson static bool trans_cmpb(DisasContext *ctx, arg_cmpb *a)
336798cd9ca7SRichard Henderson {
3368e9efd4bcSRichard Henderson     if (!ctx->is_pa20 && a->d) {
3369e9efd4bcSRichard Henderson         return false;
3370e9efd4bcSRichard Henderson     }
337101afb7beSRichard Henderson     nullify_over(ctx);
3372e9efd4bcSRichard Henderson     return do_cmpb(ctx, a->r2, load_gpr(ctx, a->r1),
3373e9efd4bcSRichard Henderson                    a->c, a->f, a->d, a->n, a->disp);
337401afb7beSRichard Henderson }
337501afb7beSRichard Henderson 
337601afb7beSRichard Henderson static bool trans_cmpbi(DisasContext *ctx, arg_cmpbi *a)
337701afb7beSRichard Henderson {
3378c65c3ee1SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3379c65c3ee1SRichard Henderson         return false;
3380c65c3ee1SRichard Henderson     }
338101afb7beSRichard Henderson     nullify_over(ctx);
33826fd0c7bcSRichard Henderson     return do_cmpb(ctx, a->r, tcg_constant_i64(a->i),
3383c65c3ee1SRichard Henderson                    a->c, a->f, a->d, a->n, a->disp);
338401afb7beSRichard Henderson }
338501afb7beSRichard Henderson 
33866fd0c7bcSRichard Henderson static bool do_addb(DisasContext *ctx, unsigned r, TCGv_i64 in1,
338701afb7beSRichard Henderson                     unsigned c, unsigned f, unsigned n, int disp)
338801afb7beSRichard Henderson {
33896fd0c7bcSRichard Henderson     TCGv_i64 dest, in2, sv, cb_cond;
339098cd9ca7SRichard Henderson     DisasCond cond;
3391bdcccc17SRichard Henderson     bool d = false;
339298cd9ca7SRichard Henderson 
3393f25d3160SRichard Henderson     /*
3394f25d3160SRichard Henderson      * For hppa64, the ADDB conditions change with PSW.W,
3395f25d3160SRichard Henderson      * dropping ZNV, SV, OD in favor of double-word EQ, LT, LE.
3396f25d3160SRichard Henderson      */
3397f25d3160SRichard Henderson     if (ctx->tb_flags & PSW_W) {
3398f25d3160SRichard Henderson         d = c >= 5;
3399f25d3160SRichard Henderson         if (d) {
3400f25d3160SRichard Henderson             c &= 3;
3401f25d3160SRichard Henderson         }
3402f25d3160SRichard Henderson     }
3403f25d3160SRichard Henderson 
340498cd9ca7SRichard Henderson     in2 = load_gpr(ctx, r);
3405aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
3406f764718dSRichard Henderson     sv = NULL;
3407bdcccc17SRichard Henderson     cb_cond = NULL;
340898cd9ca7SRichard Henderson 
3409b47a4a02SSven Schnelle     if (cond_need_cb(c)) {
3410aac0f603SRichard Henderson         TCGv_i64 cb = tcg_temp_new_i64();
3411aac0f603SRichard Henderson         TCGv_i64 cb_msb = tcg_temp_new_i64();
3412bdcccc17SRichard Henderson 
34136fd0c7bcSRichard Henderson         tcg_gen_movi_i64(cb_msb, 0);
34146fd0c7bcSRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, in1, cb_msb, in2, cb_msb);
34156fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, in1, in2);
34166fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
3417bdcccc17SRichard Henderson         cb_cond = get_carry(ctx, d, cb, cb_msb);
3418b47a4a02SSven Schnelle     } else {
34196fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, in1, in2);
3420b47a4a02SSven Schnelle     }
3421b47a4a02SSven Schnelle     if (cond_need_sv(c)) {
342298cd9ca7SRichard Henderson         sv = do_add_sv(ctx, dest, in1, in2);
342398cd9ca7SRichard Henderson     }
342498cd9ca7SRichard Henderson 
3425a751eb31SRichard Henderson     cond = do_cond(ctx, c * 2 + f, d, dest, cb_cond, sv);
342643675d20SSven Schnelle     save_gpr(ctx, r, dest);
342701afb7beSRichard Henderson     return do_cbranch(ctx, disp, n, &cond);
342898cd9ca7SRichard Henderson }
342998cd9ca7SRichard Henderson 
343001afb7beSRichard Henderson static bool trans_addb(DisasContext *ctx, arg_addb *a)
343198cd9ca7SRichard Henderson {
343201afb7beSRichard Henderson     nullify_over(ctx);
343301afb7beSRichard Henderson     return do_addb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
343401afb7beSRichard Henderson }
343501afb7beSRichard Henderson 
343601afb7beSRichard Henderson static bool trans_addbi(DisasContext *ctx, arg_addbi *a)
343701afb7beSRichard Henderson {
343801afb7beSRichard Henderson     nullify_over(ctx);
34396fd0c7bcSRichard Henderson     return do_addb(ctx, a->r, tcg_constant_i64(a->i), a->c, a->f, a->n, a->disp);
344001afb7beSRichard Henderson }
344101afb7beSRichard Henderson 
344201afb7beSRichard Henderson static bool trans_bb_sar(DisasContext *ctx, arg_bb_sar *a)
344301afb7beSRichard Henderson {
34446fd0c7bcSRichard Henderson     TCGv_i64 tmp, tcg_r;
344598cd9ca7SRichard Henderson     DisasCond cond;
344698cd9ca7SRichard Henderson 
344798cd9ca7SRichard Henderson     nullify_over(ctx);
344898cd9ca7SRichard Henderson 
3449aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
345001afb7beSRichard Henderson     tcg_r = load_gpr(ctx, a->r);
345182d0c831SRichard Henderson     if (a->d) {
345282d0c831SRichard Henderson         tcg_gen_shl_i64(tmp, tcg_r, cpu_sar);
345382d0c831SRichard Henderson     } else {
34541e9ab9fbSRichard Henderson         /* Force shift into [32,63] */
34556fd0c7bcSRichard Henderson         tcg_gen_ori_i64(tmp, cpu_sar, 32);
34566fd0c7bcSRichard Henderson         tcg_gen_shl_i64(tmp, tcg_r, tmp);
34571e9ab9fbSRichard Henderson     }
345898cd9ca7SRichard Henderson 
34591e9ab9fbSRichard Henderson     cond = cond_make_0_tmp(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
346001afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
346198cd9ca7SRichard Henderson }
346298cd9ca7SRichard Henderson 
346301afb7beSRichard Henderson static bool trans_bb_imm(DisasContext *ctx, arg_bb_imm *a)
346498cd9ca7SRichard Henderson {
34656fd0c7bcSRichard Henderson     TCGv_i64 tmp, tcg_r;
346601afb7beSRichard Henderson     DisasCond cond;
34671e9ab9fbSRichard Henderson     int p;
346801afb7beSRichard Henderson 
346901afb7beSRichard Henderson     nullify_over(ctx);
347001afb7beSRichard Henderson 
3471aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
347201afb7beSRichard Henderson     tcg_r = load_gpr(ctx, a->r);
347382d0c831SRichard Henderson     p = a->p | (a->d ? 0 : 32);
34746fd0c7bcSRichard Henderson     tcg_gen_shli_i64(tmp, tcg_r, p);
347501afb7beSRichard Henderson 
347601afb7beSRichard Henderson     cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
347701afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
347801afb7beSRichard Henderson }
347901afb7beSRichard Henderson 
348001afb7beSRichard Henderson static bool trans_movb(DisasContext *ctx, arg_movb *a)
348101afb7beSRichard Henderson {
34826fd0c7bcSRichard Henderson     TCGv_i64 dest;
348398cd9ca7SRichard Henderson     DisasCond cond;
348498cd9ca7SRichard Henderson 
348598cd9ca7SRichard Henderson     nullify_over(ctx);
348698cd9ca7SRichard Henderson 
348701afb7beSRichard Henderson     dest = dest_gpr(ctx, a->r2);
348801afb7beSRichard Henderson     if (a->r1 == 0) {
34896fd0c7bcSRichard Henderson         tcg_gen_movi_i64(dest, 0);
349098cd9ca7SRichard Henderson     } else {
34916fd0c7bcSRichard Henderson         tcg_gen_mov_i64(dest, cpu_gr[a->r1]);
349298cd9ca7SRichard Henderson     }
349398cd9ca7SRichard Henderson 
34944fa52edfSRichard Henderson     /* All MOVB conditions are 32-bit. */
34954fa52edfSRichard Henderson     cond = do_sed_cond(ctx, a->c, false, dest);
349601afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
349701afb7beSRichard Henderson }
349801afb7beSRichard Henderson 
349901afb7beSRichard Henderson static bool trans_movbi(DisasContext *ctx, arg_movbi *a)
350001afb7beSRichard Henderson {
35016fd0c7bcSRichard Henderson     TCGv_i64 dest;
350201afb7beSRichard Henderson     DisasCond cond;
350301afb7beSRichard Henderson 
350401afb7beSRichard Henderson     nullify_over(ctx);
350501afb7beSRichard Henderson 
350601afb7beSRichard Henderson     dest = dest_gpr(ctx, a->r);
35076fd0c7bcSRichard Henderson     tcg_gen_movi_i64(dest, a->i);
350801afb7beSRichard Henderson 
35094fa52edfSRichard Henderson     /* All MOVBI conditions are 32-bit. */
35104fa52edfSRichard Henderson     cond = do_sed_cond(ctx, a->c, false, dest);
351101afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
351298cd9ca7SRichard Henderson }
351398cd9ca7SRichard Henderson 
3514f7b775a9SRichard Henderson static bool trans_shrp_sar(DisasContext *ctx, arg_shrp_sar *a)
35150b1347d2SRichard Henderson {
35166fd0c7bcSRichard Henderson     TCGv_i64 dest, src2;
35170b1347d2SRichard Henderson 
3518f7b775a9SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3519f7b775a9SRichard Henderson         return false;
3520f7b775a9SRichard Henderson     }
352130878590SRichard Henderson     if (a->c) {
35220b1347d2SRichard Henderson         nullify_over(ctx);
35230b1347d2SRichard Henderson     }
35240b1347d2SRichard Henderson 
352530878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
3526f7b775a9SRichard Henderson     src2 = load_gpr(ctx, a->r2);
352730878590SRichard Henderson     if (a->r1 == 0) {
3528f7b775a9SRichard Henderson         if (a->d) {
35296fd0c7bcSRichard Henderson             tcg_gen_shr_i64(dest, src2, cpu_sar);
3530f7b775a9SRichard Henderson         } else {
3531aac0f603SRichard Henderson             TCGv_i64 tmp = tcg_temp_new_i64();
3532f7b775a9SRichard Henderson 
35336fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(dest, src2);
35346fd0c7bcSRichard Henderson             tcg_gen_andi_i64(tmp, cpu_sar, 31);
35356fd0c7bcSRichard Henderson             tcg_gen_shr_i64(dest, dest, tmp);
3536f7b775a9SRichard Henderson         }
353730878590SRichard Henderson     } else if (a->r1 == a->r2) {
3538f7b775a9SRichard Henderson         if (a->d) {
35396fd0c7bcSRichard Henderson             tcg_gen_rotr_i64(dest, src2, cpu_sar);
3540f7b775a9SRichard Henderson         } else {
35410b1347d2SRichard Henderson             TCGv_i32 t32 = tcg_temp_new_i32();
3542e1d635e8SRichard Henderson             TCGv_i32 s32 = tcg_temp_new_i32();
3543e1d635e8SRichard Henderson 
35446fd0c7bcSRichard Henderson             tcg_gen_extrl_i64_i32(t32, src2);
35456fd0c7bcSRichard Henderson             tcg_gen_extrl_i64_i32(s32, cpu_sar);
3546f7b775a9SRichard Henderson             tcg_gen_andi_i32(s32, s32, 31);
3547e1d635e8SRichard Henderson             tcg_gen_rotr_i32(t32, t32, s32);
35486fd0c7bcSRichard Henderson             tcg_gen_extu_i32_i64(dest, t32);
3549f7b775a9SRichard Henderson         }
3550f7b775a9SRichard Henderson     } else {
35516fd0c7bcSRichard Henderson         TCGv_i64 src1 = load_gpr(ctx, a->r1);
3552f7b775a9SRichard Henderson 
3553f7b775a9SRichard Henderson         if (a->d) {
3554aac0f603SRichard Henderson             TCGv_i64 t = tcg_temp_new_i64();
3555aac0f603SRichard Henderson             TCGv_i64 n = tcg_temp_new_i64();
3556f7b775a9SRichard Henderson 
35576fd0c7bcSRichard Henderson             tcg_gen_xori_i64(n, cpu_sar, 63);
3558a01491a2SHelge Deller             tcg_gen_shl_i64(t, src1, n);
35596fd0c7bcSRichard Henderson             tcg_gen_shli_i64(t, t, 1);
3560a01491a2SHelge Deller             tcg_gen_shr_i64(dest, src2, cpu_sar);
35616fd0c7bcSRichard Henderson             tcg_gen_or_i64(dest, dest, t);
35620b1347d2SRichard Henderson         } else {
35630b1347d2SRichard Henderson             TCGv_i64 t = tcg_temp_new_i64();
35640b1347d2SRichard Henderson             TCGv_i64 s = tcg_temp_new_i64();
35650b1347d2SRichard Henderson 
35666fd0c7bcSRichard Henderson             tcg_gen_concat32_i64(t, src2, src1);
3567967662cdSRichard Henderson             tcg_gen_andi_i64(s, cpu_sar, 31);
3568967662cdSRichard Henderson             tcg_gen_shr_i64(dest, t, s);
35690b1347d2SRichard Henderson         }
3570f7b775a9SRichard Henderson     }
357130878590SRichard Henderson     save_gpr(ctx, a->t, dest);
35720b1347d2SRichard Henderson 
35730b1347d2SRichard Henderson     /* Install the new nullification.  */
35740b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
357530878590SRichard Henderson     if (a->c) {
3576d37fad0aSSven Schnelle         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
35770b1347d2SRichard Henderson     }
357831234768SRichard Henderson     return nullify_end(ctx);
35790b1347d2SRichard Henderson }
35800b1347d2SRichard Henderson 
3581f7b775a9SRichard Henderson static bool trans_shrp_imm(DisasContext *ctx, arg_shrp_imm *a)
35820b1347d2SRichard Henderson {
3583f7b775a9SRichard Henderson     unsigned width, sa;
35846fd0c7bcSRichard Henderson     TCGv_i64 dest, t2;
35850b1347d2SRichard Henderson 
3586f7b775a9SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3587f7b775a9SRichard Henderson         return false;
3588f7b775a9SRichard Henderson     }
358930878590SRichard Henderson     if (a->c) {
35900b1347d2SRichard Henderson         nullify_over(ctx);
35910b1347d2SRichard Henderson     }
35920b1347d2SRichard Henderson 
3593f7b775a9SRichard Henderson     width = a->d ? 64 : 32;
3594f7b775a9SRichard Henderson     sa = width - 1 - a->cpos;
3595f7b775a9SRichard Henderson 
359630878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
359730878590SRichard Henderson     t2 = load_gpr(ctx, a->r2);
359805bfd4dbSRichard Henderson     if (a->r1 == 0) {
35996fd0c7bcSRichard Henderson         tcg_gen_extract_i64(dest, t2, sa, width - sa);
3600c53e401eSRichard Henderson     } else if (width == TARGET_LONG_BITS) {
36016fd0c7bcSRichard Henderson         tcg_gen_extract2_i64(dest, t2, cpu_gr[a->r1], sa);
3602f7b775a9SRichard Henderson     } else {
3603f7b775a9SRichard Henderson         assert(!a->d);
3604f7b775a9SRichard Henderson         if (a->r1 == a->r2) {
36050b1347d2SRichard Henderson             TCGv_i32 t32 = tcg_temp_new_i32();
36066fd0c7bcSRichard Henderson             tcg_gen_extrl_i64_i32(t32, t2);
36070b1347d2SRichard Henderson             tcg_gen_rotri_i32(t32, t32, sa);
36086fd0c7bcSRichard Henderson             tcg_gen_extu_i32_i64(dest, t32);
36090b1347d2SRichard Henderson         } else {
3610967662cdSRichard Henderson             tcg_gen_concat32_i64(dest, t2, cpu_gr[a->r1]);
3611967662cdSRichard Henderson             tcg_gen_extract_i64(dest, dest, sa, 32);
36120b1347d2SRichard Henderson         }
3613f7b775a9SRichard Henderson     }
361430878590SRichard Henderson     save_gpr(ctx, a->t, dest);
36150b1347d2SRichard Henderson 
36160b1347d2SRichard Henderson     /* Install the new nullification.  */
36170b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
361830878590SRichard Henderson     if (a->c) {
3619d37fad0aSSven Schnelle         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
36200b1347d2SRichard Henderson     }
362131234768SRichard Henderson     return nullify_end(ctx);
36220b1347d2SRichard Henderson }
36230b1347d2SRichard Henderson 
3624bd792da3SRichard Henderson static bool trans_extr_sar(DisasContext *ctx, arg_extr_sar *a)
36250b1347d2SRichard Henderson {
3626bd792da3SRichard Henderson     unsigned widthm1 = a->d ? 63 : 31;
36276fd0c7bcSRichard Henderson     TCGv_i64 dest, src, tmp;
36280b1347d2SRichard Henderson 
3629bd792da3SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3630bd792da3SRichard Henderson         return false;
3631bd792da3SRichard Henderson     }
363230878590SRichard Henderson     if (a->c) {
36330b1347d2SRichard Henderson         nullify_over(ctx);
36340b1347d2SRichard Henderson     }
36350b1347d2SRichard Henderson 
363630878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
363730878590SRichard Henderson     src = load_gpr(ctx, a->r);
3638aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
36390b1347d2SRichard Henderson 
36400b1347d2SRichard Henderson     /* Recall that SAR is using big-endian bit numbering.  */
36416fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, cpu_sar, widthm1);
36426fd0c7bcSRichard Henderson     tcg_gen_xori_i64(tmp, tmp, widthm1);
3643d781cb77SRichard Henderson 
364430878590SRichard Henderson     if (a->se) {
3645bd792da3SRichard Henderson         if (!a->d) {
36466fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(dest, src);
3647bd792da3SRichard Henderson             src = dest;
3648bd792da3SRichard Henderson         }
36496fd0c7bcSRichard Henderson         tcg_gen_sar_i64(dest, src, tmp);
36506fd0c7bcSRichard Henderson         tcg_gen_sextract_i64(dest, dest, 0, a->len);
36510b1347d2SRichard Henderson     } else {
3652bd792da3SRichard Henderson         if (!a->d) {
36536fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(dest, src);
3654bd792da3SRichard Henderson             src = dest;
3655bd792da3SRichard Henderson         }
36566fd0c7bcSRichard Henderson         tcg_gen_shr_i64(dest, src, tmp);
36576fd0c7bcSRichard Henderson         tcg_gen_extract_i64(dest, dest, 0, a->len);
36580b1347d2SRichard Henderson     }
365930878590SRichard Henderson     save_gpr(ctx, a->t, dest);
36600b1347d2SRichard Henderson 
36610b1347d2SRichard Henderson     /* Install the new nullification.  */
36620b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
366330878590SRichard Henderson     if (a->c) {
3664bd792da3SRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
36650b1347d2SRichard Henderson     }
366631234768SRichard Henderson     return nullify_end(ctx);
36670b1347d2SRichard Henderson }
36680b1347d2SRichard Henderson 
3669bd792da3SRichard Henderson static bool trans_extr_imm(DisasContext *ctx, arg_extr_imm *a)
36700b1347d2SRichard Henderson {
3671bd792da3SRichard Henderson     unsigned len, cpos, width;
36726fd0c7bcSRichard Henderson     TCGv_i64 dest, src;
36730b1347d2SRichard Henderson 
3674bd792da3SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3675bd792da3SRichard Henderson         return false;
3676bd792da3SRichard Henderson     }
367730878590SRichard Henderson     if (a->c) {
36780b1347d2SRichard Henderson         nullify_over(ctx);
36790b1347d2SRichard Henderson     }
36800b1347d2SRichard Henderson 
3681bd792da3SRichard Henderson     len = a->len;
3682bd792da3SRichard Henderson     width = a->d ? 64 : 32;
3683bd792da3SRichard Henderson     cpos = width - 1 - a->pos;
3684bd792da3SRichard Henderson     if (cpos + len > width) {
3685bd792da3SRichard Henderson         len = width - cpos;
3686bd792da3SRichard Henderson     }
3687bd792da3SRichard Henderson 
368830878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
368930878590SRichard Henderson     src = load_gpr(ctx, a->r);
369030878590SRichard Henderson     if (a->se) {
36916fd0c7bcSRichard Henderson         tcg_gen_sextract_i64(dest, src, cpos, len);
36920b1347d2SRichard Henderson     } else {
36936fd0c7bcSRichard Henderson         tcg_gen_extract_i64(dest, src, cpos, len);
36940b1347d2SRichard Henderson     }
369530878590SRichard Henderson     save_gpr(ctx, a->t, dest);
36960b1347d2SRichard Henderson 
36970b1347d2SRichard Henderson     /* Install the new nullification.  */
36980b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
369930878590SRichard Henderson     if (a->c) {
3700bd792da3SRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
37010b1347d2SRichard Henderson     }
370231234768SRichard Henderson     return nullify_end(ctx);
37030b1347d2SRichard Henderson }
37040b1347d2SRichard Henderson 
370572ae4f2bSRichard Henderson static bool trans_depi_imm(DisasContext *ctx, arg_depi_imm *a)
37060b1347d2SRichard Henderson {
370772ae4f2bSRichard Henderson     unsigned len, width;
3708c53e401eSRichard Henderson     uint64_t mask0, mask1;
37096fd0c7bcSRichard Henderson     TCGv_i64 dest;
37100b1347d2SRichard Henderson 
371172ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
371272ae4f2bSRichard Henderson         return false;
371372ae4f2bSRichard Henderson     }
371430878590SRichard Henderson     if (a->c) {
37150b1347d2SRichard Henderson         nullify_over(ctx);
37160b1347d2SRichard Henderson     }
371772ae4f2bSRichard Henderson 
371872ae4f2bSRichard Henderson     len = a->len;
371972ae4f2bSRichard Henderson     width = a->d ? 64 : 32;
372072ae4f2bSRichard Henderson     if (a->cpos + len > width) {
372172ae4f2bSRichard Henderson         len = width - a->cpos;
37220b1347d2SRichard Henderson     }
37230b1347d2SRichard Henderson 
372430878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
372530878590SRichard Henderson     mask0 = deposit64(0, a->cpos, len, a->i);
372630878590SRichard Henderson     mask1 = deposit64(-1, a->cpos, len, a->i);
37270b1347d2SRichard Henderson 
372830878590SRichard Henderson     if (a->nz) {
37296fd0c7bcSRichard Henderson         TCGv_i64 src = load_gpr(ctx, a->t);
37306fd0c7bcSRichard Henderson         tcg_gen_andi_i64(dest, src, mask1);
37316fd0c7bcSRichard Henderson         tcg_gen_ori_i64(dest, dest, mask0);
37320b1347d2SRichard Henderson     } else {
37336fd0c7bcSRichard Henderson         tcg_gen_movi_i64(dest, mask0);
37340b1347d2SRichard Henderson     }
373530878590SRichard Henderson     save_gpr(ctx, a->t, dest);
37360b1347d2SRichard Henderson 
37370b1347d2SRichard Henderson     /* Install the new nullification.  */
37380b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
373930878590SRichard Henderson     if (a->c) {
374072ae4f2bSRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
37410b1347d2SRichard Henderson     }
374231234768SRichard Henderson     return nullify_end(ctx);
37430b1347d2SRichard Henderson }
37440b1347d2SRichard Henderson 
374572ae4f2bSRichard Henderson static bool trans_dep_imm(DisasContext *ctx, arg_dep_imm *a)
37460b1347d2SRichard Henderson {
374730878590SRichard Henderson     unsigned rs = a->nz ? a->t : 0;
374872ae4f2bSRichard Henderson     unsigned len, width;
37496fd0c7bcSRichard Henderson     TCGv_i64 dest, val;
37500b1347d2SRichard Henderson 
375172ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
375272ae4f2bSRichard Henderson         return false;
375372ae4f2bSRichard Henderson     }
375430878590SRichard Henderson     if (a->c) {
37550b1347d2SRichard Henderson         nullify_over(ctx);
37560b1347d2SRichard Henderson     }
375772ae4f2bSRichard Henderson 
375872ae4f2bSRichard Henderson     len = a->len;
375972ae4f2bSRichard Henderson     width = a->d ? 64 : 32;
376072ae4f2bSRichard Henderson     if (a->cpos + len > width) {
376172ae4f2bSRichard Henderson         len = width - a->cpos;
37620b1347d2SRichard Henderson     }
37630b1347d2SRichard Henderson 
376430878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
376530878590SRichard Henderson     val = load_gpr(ctx, a->r);
37660b1347d2SRichard Henderson     if (rs == 0) {
37676fd0c7bcSRichard Henderson         tcg_gen_deposit_z_i64(dest, val, a->cpos, len);
37680b1347d2SRichard Henderson     } else {
37696fd0c7bcSRichard Henderson         tcg_gen_deposit_i64(dest, cpu_gr[rs], val, a->cpos, len);
37700b1347d2SRichard Henderson     }
377130878590SRichard Henderson     save_gpr(ctx, a->t, dest);
37720b1347d2SRichard Henderson 
37730b1347d2SRichard Henderson     /* Install the new nullification.  */
37740b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
377530878590SRichard Henderson     if (a->c) {
377672ae4f2bSRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
37770b1347d2SRichard Henderson     }
377831234768SRichard Henderson     return nullify_end(ctx);
37790b1347d2SRichard Henderson }
37800b1347d2SRichard Henderson 
378172ae4f2bSRichard Henderson static bool do_dep_sar(DisasContext *ctx, unsigned rt, unsigned c,
37826fd0c7bcSRichard Henderson                        bool d, bool nz, unsigned len, TCGv_i64 val)
37830b1347d2SRichard Henderson {
37840b1347d2SRichard Henderson     unsigned rs = nz ? rt : 0;
378572ae4f2bSRichard Henderson     unsigned widthm1 = d ? 63 : 31;
37866fd0c7bcSRichard Henderson     TCGv_i64 mask, tmp, shift, dest;
3787c53e401eSRichard Henderson     uint64_t msb = 1ULL << (len - 1);
37880b1347d2SRichard Henderson 
37890b1347d2SRichard Henderson     dest = dest_gpr(ctx, rt);
3790aac0f603SRichard Henderson     shift = tcg_temp_new_i64();
3791aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
37920b1347d2SRichard Henderson 
37930b1347d2SRichard Henderson     /* Convert big-endian bit numbering in SAR to left-shift.  */
37946fd0c7bcSRichard Henderson     tcg_gen_andi_i64(shift, cpu_sar, widthm1);
37956fd0c7bcSRichard Henderson     tcg_gen_xori_i64(shift, shift, widthm1);
37960b1347d2SRichard Henderson 
3797aac0f603SRichard Henderson     mask = tcg_temp_new_i64();
37986fd0c7bcSRichard Henderson     tcg_gen_movi_i64(mask, msb + (msb - 1));
37996fd0c7bcSRichard Henderson     tcg_gen_and_i64(tmp, val, mask);
38000b1347d2SRichard Henderson     if (rs) {
38016fd0c7bcSRichard Henderson         tcg_gen_shl_i64(mask, mask, shift);
38026fd0c7bcSRichard Henderson         tcg_gen_shl_i64(tmp, tmp, shift);
38036fd0c7bcSRichard Henderson         tcg_gen_andc_i64(dest, cpu_gr[rs], mask);
38046fd0c7bcSRichard Henderson         tcg_gen_or_i64(dest, dest, tmp);
38050b1347d2SRichard Henderson     } else {
38066fd0c7bcSRichard Henderson         tcg_gen_shl_i64(dest, tmp, shift);
38070b1347d2SRichard Henderson     }
38080b1347d2SRichard Henderson     save_gpr(ctx, rt, dest);
38090b1347d2SRichard Henderson 
38100b1347d2SRichard Henderson     /* Install the new nullification.  */
38110b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
38120b1347d2SRichard Henderson     if (c) {
381372ae4f2bSRichard Henderson         ctx->null_cond = do_sed_cond(ctx, c, d, dest);
38140b1347d2SRichard Henderson     }
381531234768SRichard Henderson     return nullify_end(ctx);
38160b1347d2SRichard Henderson }
38170b1347d2SRichard Henderson 
381872ae4f2bSRichard Henderson static bool trans_dep_sar(DisasContext *ctx, arg_dep_sar *a)
381930878590SRichard Henderson {
382072ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
382172ae4f2bSRichard Henderson         return false;
382272ae4f2bSRichard Henderson     }
3823a6deecceSSven Schnelle     if (a->c) {
3824a6deecceSSven Schnelle         nullify_over(ctx);
3825a6deecceSSven Schnelle     }
382672ae4f2bSRichard Henderson     return do_dep_sar(ctx, a->t, a->c, a->d, a->nz, a->len,
382772ae4f2bSRichard Henderson                       load_gpr(ctx, a->r));
382830878590SRichard Henderson }
382930878590SRichard Henderson 
383072ae4f2bSRichard Henderson static bool trans_depi_sar(DisasContext *ctx, arg_depi_sar *a)
383130878590SRichard Henderson {
383272ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
383372ae4f2bSRichard Henderson         return false;
383472ae4f2bSRichard Henderson     }
3835a6deecceSSven Schnelle     if (a->c) {
3836a6deecceSSven Schnelle         nullify_over(ctx);
3837a6deecceSSven Schnelle     }
383872ae4f2bSRichard Henderson     return do_dep_sar(ctx, a->t, a->c, a->d, a->nz, a->len,
38396fd0c7bcSRichard Henderson                       tcg_constant_i64(a->i));
384030878590SRichard Henderson }
38410b1347d2SRichard Henderson 
38428340f534SRichard Henderson static bool trans_be(DisasContext *ctx, arg_be *a)
384398cd9ca7SRichard Henderson {
38446fd0c7bcSRichard Henderson     TCGv_i64 tmp;
384598cd9ca7SRichard Henderson 
3846c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
384798cd9ca7SRichard Henderson     /* ??? It seems like there should be a good way of using
384898cd9ca7SRichard Henderson        "be disp(sr2, r0)", the canonical gateway entry mechanism
384998cd9ca7SRichard Henderson        to our advantage.  But that appears to be inconvenient to
385098cd9ca7SRichard Henderson        manage along side branch delay slots.  Therefore we handle
385198cd9ca7SRichard Henderson        entry into the gateway page via absolute address.  */
385298cd9ca7SRichard Henderson     /* Since we don't implement spaces, just branch.  Do notice the special
385398cd9ca7SRichard Henderson        case of "be disp(*,r0)" using a direct branch to disp, so that we can
385498cd9ca7SRichard Henderson        goto_tb to the TB containing the syscall.  */
38558340f534SRichard Henderson     if (a->b == 0) {
38568340f534SRichard Henderson         return do_dbranch(ctx, a->disp, a->l, a->n);
385798cd9ca7SRichard Henderson     }
3858c301f34eSRichard Henderson #else
3859c301f34eSRichard Henderson     nullify_over(ctx);
3860660eefe1SRichard Henderson #endif
3861660eefe1SRichard Henderson 
3862aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
38636fd0c7bcSRichard Henderson     tcg_gen_addi_i64(tmp, load_gpr(ctx, a->b), a->disp);
3864660eefe1SRichard Henderson     tmp = do_ibranch_priv(ctx, tmp);
3865c301f34eSRichard Henderson 
3866c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
38678340f534SRichard Henderson     return do_ibranch(ctx, tmp, a->l, a->n);
3868c301f34eSRichard Henderson #else
3869c301f34eSRichard Henderson     TCGv_i64 new_spc = tcg_temp_new_i64();
3870c301f34eSRichard Henderson 
38718340f534SRichard Henderson     load_spr(ctx, new_spc, a->sp);
38728340f534SRichard Henderson     if (a->l) {
3873741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_gr[31], ctx->iaoq_n, ctx->iaoq_n_var);
38747fb7c9daSRichard Henderson         tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_b);
3875c301f34eSRichard Henderson     }
38768340f534SRichard Henderson     if (a->n && use_nullify_skip(ctx)) {
3877a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, -1, tmp);
38786fd0c7bcSRichard Henderson         tcg_gen_addi_i64(tmp, tmp, 4);
3879a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, -1, tmp);
3880c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_f, new_spc);
3881c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
3882c301f34eSRichard Henderson     } else {
3883741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3884c301f34eSRichard Henderson         if (ctx->iaoq_b == -1) {
3885c301f34eSRichard Henderson             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3886c301f34eSRichard Henderson         }
3887a0180973SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, -1, tmp);
3888c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_b, new_spc);
38898340f534SRichard Henderson         nullify_set(ctx, a->n);
3890c301f34eSRichard Henderson     }
3891c301f34eSRichard Henderson     tcg_gen_lookup_and_goto_ptr();
389231234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
389331234768SRichard Henderson     return nullify_end(ctx);
3894c301f34eSRichard Henderson #endif
389598cd9ca7SRichard Henderson }
389698cd9ca7SRichard Henderson 
38978340f534SRichard Henderson static bool trans_bl(DisasContext *ctx, arg_bl *a)
389898cd9ca7SRichard Henderson {
38998340f534SRichard Henderson     return do_dbranch(ctx, iaoq_dest(ctx, a->disp), a->l, a->n);
390098cd9ca7SRichard Henderson }
390198cd9ca7SRichard Henderson 
39028340f534SRichard Henderson static bool trans_b_gate(DisasContext *ctx, arg_b_gate *a)
390343e05652SRichard Henderson {
3904c53e401eSRichard Henderson     uint64_t dest = iaoq_dest(ctx, a->disp);
390543e05652SRichard Henderson 
39066e5f5300SSven Schnelle     nullify_over(ctx);
39076e5f5300SSven Schnelle 
390843e05652SRichard Henderson     /* Make sure the caller hasn't done something weird with the queue.
390943e05652SRichard Henderson      * ??? This is not quite the same as the PSW[B] bit, which would be
391043e05652SRichard Henderson      * expensive to track.  Real hardware will trap for
391143e05652SRichard Henderson      *    b  gateway
391243e05652SRichard Henderson      *    b  gateway+4  (in delay slot of first branch)
391343e05652SRichard Henderson      * However, checking for a non-sequential instruction queue *will*
391443e05652SRichard Henderson      * diagnose the security hole
391543e05652SRichard Henderson      *    b  gateway
391643e05652SRichard Henderson      *    b  evil
391743e05652SRichard Henderson      * in which instructions at evil would run with increased privs.
391843e05652SRichard Henderson      */
391943e05652SRichard Henderson     if (ctx->iaoq_b == -1 || ctx->iaoq_b != ctx->iaoq_f + 4) {
392043e05652SRichard Henderson         return gen_illegal(ctx);
392143e05652SRichard Henderson     }
392243e05652SRichard Henderson 
392343e05652SRichard Henderson #ifndef CONFIG_USER_ONLY
392443e05652SRichard Henderson     if (ctx->tb_flags & PSW_C) {
392594956d7bSPhilippe Mathieu-Daudé         int type = hppa_artype_for_page(cpu_env(ctx->cs), ctx->base.pc_next);
392643e05652SRichard Henderson         /* If we could not find a TLB entry, then we need to generate an
392743e05652SRichard Henderson            ITLB miss exception so the kernel will provide it.
392843e05652SRichard Henderson            The resulting TLB fill operation will invalidate this TB and
392943e05652SRichard Henderson            we will re-translate, at which point we *will* be able to find
393043e05652SRichard Henderson            the TLB entry and determine if this is in fact a gateway page.  */
393143e05652SRichard Henderson         if (type < 0) {
393231234768SRichard Henderson             gen_excp(ctx, EXCP_ITLB_MISS);
393331234768SRichard Henderson             return true;
393443e05652SRichard Henderson         }
393543e05652SRichard Henderson         /* No change for non-gateway pages or for priv decrease.  */
393643e05652SRichard Henderson         if (type >= 4 && type - 4 < ctx->privilege) {
39372f48ba7bSRichard Henderson             dest = deposit64(dest, 0, 2, type - 4);
393843e05652SRichard Henderson         }
393943e05652SRichard Henderson     } else {
394043e05652SRichard Henderson         dest &= -4;  /* priv = 0 */
394143e05652SRichard Henderson     }
394243e05652SRichard Henderson #endif
394343e05652SRichard Henderson 
39446e5f5300SSven Schnelle     if (a->l) {
39456fd0c7bcSRichard Henderson         TCGv_i64 tmp = dest_gpr(ctx, a->l);
39466e5f5300SSven Schnelle         if (ctx->privilege < 3) {
39476fd0c7bcSRichard Henderson             tcg_gen_andi_i64(tmp, tmp, -4);
39486e5f5300SSven Schnelle         }
39496fd0c7bcSRichard Henderson         tcg_gen_ori_i64(tmp, tmp, ctx->privilege);
39506e5f5300SSven Schnelle         save_gpr(ctx, a->l, tmp);
39516e5f5300SSven Schnelle     }
39526e5f5300SSven Schnelle 
39536e5f5300SSven Schnelle     return do_dbranch(ctx, dest, 0, a->n);
395443e05652SRichard Henderson }
395543e05652SRichard Henderson 
39568340f534SRichard Henderson static bool trans_blr(DisasContext *ctx, arg_blr *a)
395798cd9ca7SRichard Henderson {
3958b35aec85SRichard Henderson     if (a->x) {
3959aac0f603SRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
39606fd0c7bcSRichard Henderson         tcg_gen_shli_i64(tmp, load_gpr(ctx, a->x), 3);
39616fd0c7bcSRichard Henderson         tcg_gen_addi_i64(tmp, tmp, ctx->iaoq_f + 8);
3962660eefe1SRichard Henderson         /* The computation here never changes privilege level.  */
39638340f534SRichard Henderson         return do_ibranch(ctx, tmp, a->l, a->n);
3964b35aec85SRichard Henderson     } else {
3965b35aec85SRichard Henderson         /* BLR R0,RX is a good way to load PC+8 into RX.  */
3966b35aec85SRichard Henderson         return do_dbranch(ctx, ctx->iaoq_f + 8, a->l, a->n);
3967b35aec85SRichard Henderson     }
396898cd9ca7SRichard Henderson }
396998cd9ca7SRichard Henderson 
39708340f534SRichard Henderson static bool trans_bv(DisasContext *ctx, arg_bv *a)
397198cd9ca7SRichard Henderson {
39726fd0c7bcSRichard Henderson     TCGv_i64 dest;
397398cd9ca7SRichard Henderson 
39748340f534SRichard Henderson     if (a->x == 0) {
39758340f534SRichard Henderson         dest = load_gpr(ctx, a->b);
397698cd9ca7SRichard Henderson     } else {
3977aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
39786fd0c7bcSRichard Henderson         tcg_gen_shli_i64(dest, load_gpr(ctx, a->x), 3);
39796fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, dest, load_gpr(ctx, a->b));
398098cd9ca7SRichard Henderson     }
3981660eefe1SRichard Henderson     dest = do_ibranch_priv(ctx, dest);
39828340f534SRichard Henderson     return do_ibranch(ctx, dest, 0, a->n);
398398cd9ca7SRichard Henderson }
398498cd9ca7SRichard Henderson 
39858340f534SRichard Henderson static bool trans_bve(DisasContext *ctx, arg_bve *a)
398698cd9ca7SRichard Henderson {
39876fd0c7bcSRichard Henderson     TCGv_i64 dest;
398898cd9ca7SRichard Henderson 
3989c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
39908340f534SRichard Henderson     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
39918340f534SRichard Henderson     return do_ibranch(ctx, dest, a->l, a->n);
3992c301f34eSRichard Henderson #else
3993c301f34eSRichard Henderson     nullify_over(ctx);
39948340f534SRichard Henderson     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
3995c301f34eSRichard Henderson 
3996741322f4SRichard Henderson     copy_iaoq_entry(ctx, cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
3997c301f34eSRichard Henderson     if (ctx->iaoq_b == -1) {
3998c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3999c301f34eSRichard Henderson     }
4000741322f4SRichard Henderson     copy_iaoq_entry(ctx, cpu_iaoq_b, -1, dest);
4001c301f34eSRichard Henderson     tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
40028340f534SRichard Henderson     if (a->l) {
4003741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_gr[a->l], ctx->iaoq_n, ctx->iaoq_n_var);
4004c301f34eSRichard Henderson     }
40058340f534SRichard Henderson     nullify_set(ctx, a->n);
4006c301f34eSRichard Henderson     tcg_gen_lookup_and_goto_ptr();
400731234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
400831234768SRichard Henderson     return nullify_end(ctx);
4009c301f34eSRichard Henderson #endif
401098cd9ca7SRichard Henderson }
401198cd9ca7SRichard Henderson 
4012a8966ba7SRichard Henderson static bool trans_nopbts(DisasContext *ctx, arg_nopbts *a)
4013a8966ba7SRichard Henderson {
4014a8966ba7SRichard Henderson     /* All branch target stack instructions implement as nop. */
4015a8966ba7SRichard Henderson     return ctx->is_pa20;
4016a8966ba7SRichard Henderson }
4017a8966ba7SRichard Henderson 
40181ca74648SRichard Henderson /*
40191ca74648SRichard Henderson  * Float class 0
40201ca74648SRichard Henderson  */
4021ebe9383cSRichard Henderson 
40221ca74648SRichard Henderson static void gen_fcpy_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4023ebe9383cSRichard Henderson {
4024ebe9383cSRichard Henderson     tcg_gen_mov_i32(dst, src);
4025ebe9383cSRichard Henderson }
4026ebe9383cSRichard Henderson 
402759f8c04bSHelge Deller static bool trans_fid_f(DisasContext *ctx, arg_fid_f *a)
402859f8c04bSHelge Deller {
4029a300dad3SRichard Henderson     uint64_t ret;
4030a300dad3SRichard Henderson 
4031c53e401eSRichard Henderson     if (ctx->is_pa20) {
4032a300dad3SRichard Henderson         ret = 0x13080000000000ULL; /* PA8700 (PCX-W2) */
4033a300dad3SRichard Henderson     } else {
4034a300dad3SRichard Henderson         ret = 0x0f080000000000ULL; /* PA7300LC (PCX-L2) */
4035a300dad3SRichard Henderson     }
4036a300dad3SRichard Henderson 
403759f8c04bSHelge Deller     nullify_over(ctx);
4038a300dad3SRichard Henderson     save_frd(0, tcg_constant_i64(ret));
403959f8c04bSHelge Deller     return nullify_end(ctx);
404059f8c04bSHelge Deller }
404159f8c04bSHelge Deller 
40421ca74648SRichard Henderson static bool trans_fcpy_f(DisasContext *ctx, arg_fclass01 *a)
40431ca74648SRichard Henderson {
40441ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fcpy_f);
40451ca74648SRichard Henderson }
40461ca74648SRichard Henderson 
4047ebe9383cSRichard Henderson static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4048ebe9383cSRichard Henderson {
4049ebe9383cSRichard Henderson     tcg_gen_mov_i64(dst, src);
4050ebe9383cSRichard Henderson }
4051ebe9383cSRichard Henderson 
40521ca74648SRichard Henderson static bool trans_fcpy_d(DisasContext *ctx, arg_fclass01 *a)
40531ca74648SRichard Henderson {
40541ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fcpy_d);
40551ca74648SRichard Henderson }
40561ca74648SRichard Henderson 
40571ca74648SRichard Henderson static void gen_fabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4058ebe9383cSRichard Henderson {
4059ebe9383cSRichard Henderson     tcg_gen_andi_i32(dst, src, INT32_MAX);
4060ebe9383cSRichard Henderson }
4061ebe9383cSRichard Henderson 
40621ca74648SRichard Henderson static bool trans_fabs_f(DisasContext *ctx, arg_fclass01 *a)
40631ca74648SRichard Henderson {
40641ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fabs_f);
40651ca74648SRichard Henderson }
40661ca74648SRichard Henderson 
4067ebe9383cSRichard Henderson static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4068ebe9383cSRichard Henderson {
4069ebe9383cSRichard Henderson     tcg_gen_andi_i64(dst, src, INT64_MAX);
4070ebe9383cSRichard Henderson }
4071ebe9383cSRichard Henderson 
40721ca74648SRichard Henderson static bool trans_fabs_d(DisasContext *ctx, arg_fclass01 *a)
40731ca74648SRichard Henderson {
40741ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fabs_d);
40751ca74648SRichard Henderson }
40761ca74648SRichard Henderson 
40771ca74648SRichard Henderson static bool trans_fsqrt_f(DisasContext *ctx, arg_fclass01 *a)
40781ca74648SRichard Henderson {
40791ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fsqrt_s);
40801ca74648SRichard Henderson }
40811ca74648SRichard Henderson 
40821ca74648SRichard Henderson static bool trans_fsqrt_d(DisasContext *ctx, arg_fclass01 *a)
40831ca74648SRichard Henderson {
40841ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fsqrt_d);
40851ca74648SRichard Henderson }
40861ca74648SRichard Henderson 
40871ca74648SRichard Henderson static bool trans_frnd_f(DisasContext *ctx, arg_fclass01 *a)
40881ca74648SRichard Henderson {
40891ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_frnd_s);
40901ca74648SRichard Henderson }
40911ca74648SRichard Henderson 
40921ca74648SRichard Henderson static bool trans_frnd_d(DisasContext *ctx, arg_fclass01 *a)
40931ca74648SRichard Henderson {
40941ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_frnd_d);
40951ca74648SRichard Henderson }
40961ca74648SRichard Henderson 
40971ca74648SRichard Henderson static void gen_fneg_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4098ebe9383cSRichard Henderson {
4099ebe9383cSRichard Henderson     tcg_gen_xori_i32(dst, src, INT32_MIN);
4100ebe9383cSRichard Henderson }
4101ebe9383cSRichard Henderson 
41021ca74648SRichard Henderson static bool trans_fneg_f(DisasContext *ctx, arg_fclass01 *a)
41031ca74648SRichard Henderson {
41041ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fneg_f);
41051ca74648SRichard Henderson }
41061ca74648SRichard Henderson 
4107ebe9383cSRichard Henderson static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4108ebe9383cSRichard Henderson {
4109ebe9383cSRichard Henderson     tcg_gen_xori_i64(dst, src, INT64_MIN);
4110ebe9383cSRichard Henderson }
4111ebe9383cSRichard Henderson 
41121ca74648SRichard Henderson static bool trans_fneg_d(DisasContext *ctx, arg_fclass01 *a)
41131ca74648SRichard Henderson {
41141ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fneg_d);
41151ca74648SRichard Henderson }
41161ca74648SRichard Henderson 
41171ca74648SRichard Henderson static void gen_fnegabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4118ebe9383cSRichard Henderson {
4119ebe9383cSRichard Henderson     tcg_gen_ori_i32(dst, src, INT32_MIN);
4120ebe9383cSRichard Henderson }
4121ebe9383cSRichard Henderson 
41221ca74648SRichard Henderson static bool trans_fnegabs_f(DisasContext *ctx, arg_fclass01 *a)
41231ca74648SRichard Henderson {
41241ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fnegabs_f);
41251ca74648SRichard Henderson }
41261ca74648SRichard Henderson 
4127ebe9383cSRichard Henderson static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4128ebe9383cSRichard Henderson {
4129ebe9383cSRichard Henderson     tcg_gen_ori_i64(dst, src, INT64_MIN);
4130ebe9383cSRichard Henderson }
4131ebe9383cSRichard Henderson 
41321ca74648SRichard Henderson static bool trans_fnegabs_d(DisasContext *ctx, arg_fclass01 *a)
41331ca74648SRichard Henderson {
41341ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fnegabs_d);
41351ca74648SRichard Henderson }
41361ca74648SRichard Henderson 
41371ca74648SRichard Henderson /*
41381ca74648SRichard Henderson  * Float class 1
41391ca74648SRichard Henderson  */
41401ca74648SRichard Henderson 
41411ca74648SRichard Henderson static bool trans_fcnv_d_f(DisasContext *ctx, arg_fclass01 *a)
41421ca74648SRichard Henderson {
41431ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_s);
41441ca74648SRichard Henderson }
41451ca74648SRichard Henderson 
41461ca74648SRichard Henderson static bool trans_fcnv_f_d(DisasContext *ctx, arg_fclass01 *a)
41471ca74648SRichard Henderson {
41481ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_d);
41491ca74648SRichard Henderson }
41501ca74648SRichard Henderson 
41511ca74648SRichard Henderson static bool trans_fcnv_w_f(DisasContext *ctx, arg_fclass01 *a)
41521ca74648SRichard Henderson {
41531ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_w_s);
41541ca74648SRichard Henderson }
41551ca74648SRichard Henderson 
41561ca74648SRichard Henderson static bool trans_fcnv_q_f(DisasContext *ctx, arg_fclass01 *a)
41571ca74648SRichard Henderson {
41581ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_dw_s);
41591ca74648SRichard Henderson }
41601ca74648SRichard Henderson 
41611ca74648SRichard Henderson static bool trans_fcnv_w_d(DisasContext *ctx, arg_fclass01 *a)
41621ca74648SRichard Henderson {
41631ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_w_d);
41641ca74648SRichard Henderson }
41651ca74648SRichard Henderson 
41661ca74648SRichard Henderson static bool trans_fcnv_q_d(DisasContext *ctx, arg_fclass01 *a)
41671ca74648SRichard Henderson {
41681ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_dw_d);
41691ca74648SRichard Henderson }
41701ca74648SRichard Henderson 
41711ca74648SRichard Henderson static bool trans_fcnv_f_w(DisasContext *ctx, arg_fclass01 *a)
41721ca74648SRichard Henderson {
41731ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_w);
41741ca74648SRichard Henderson }
41751ca74648SRichard Henderson 
41761ca74648SRichard Henderson static bool trans_fcnv_d_w(DisasContext *ctx, arg_fclass01 *a)
41771ca74648SRichard Henderson {
41781ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_w);
41791ca74648SRichard Henderson }
41801ca74648SRichard Henderson 
41811ca74648SRichard Henderson static bool trans_fcnv_f_q(DisasContext *ctx, arg_fclass01 *a)
41821ca74648SRichard Henderson {
41831ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_dw);
41841ca74648SRichard Henderson }
41851ca74648SRichard Henderson 
41861ca74648SRichard Henderson static bool trans_fcnv_d_q(DisasContext *ctx, arg_fclass01 *a)
41871ca74648SRichard Henderson {
41881ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_dw);
41891ca74648SRichard Henderson }
41901ca74648SRichard Henderson 
41911ca74648SRichard Henderson static bool trans_fcnv_t_f_w(DisasContext *ctx, arg_fclass01 *a)
41921ca74648SRichard Henderson {
41931ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_w);
41941ca74648SRichard Henderson }
41951ca74648SRichard Henderson 
41961ca74648SRichard Henderson static bool trans_fcnv_t_d_w(DisasContext *ctx, arg_fclass01 *a)
41971ca74648SRichard Henderson {
41981ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_w);
41991ca74648SRichard Henderson }
42001ca74648SRichard Henderson 
42011ca74648SRichard Henderson static bool trans_fcnv_t_f_q(DisasContext *ctx, arg_fclass01 *a)
42021ca74648SRichard Henderson {
42031ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_dw);
42041ca74648SRichard Henderson }
42051ca74648SRichard Henderson 
42061ca74648SRichard Henderson static bool trans_fcnv_t_d_q(DisasContext *ctx, arg_fclass01 *a)
42071ca74648SRichard Henderson {
42081ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_dw);
42091ca74648SRichard Henderson }
42101ca74648SRichard Henderson 
42111ca74648SRichard Henderson static bool trans_fcnv_uw_f(DisasContext *ctx, arg_fclass01 *a)
42121ca74648SRichard Henderson {
42131ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_uw_s);
42141ca74648SRichard Henderson }
42151ca74648SRichard Henderson 
42161ca74648SRichard Henderson static bool trans_fcnv_uq_f(DisasContext *ctx, arg_fclass01 *a)
42171ca74648SRichard Henderson {
42181ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_udw_s);
42191ca74648SRichard Henderson }
42201ca74648SRichard Henderson 
42211ca74648SRichard Henderson static bool trans_fcnv_uw_d(DisasContext *ctx, arg_fclass01 *a)
42221ca74648SRichard Henderson {
42231ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_uw_d);
42241ca74648SRichard Henderson }
42251ca74648SRichard Henderson 
42261ca74648SRichard Henderson static bool trans_fcnv_uq_d(DisasContext *ctx, arg_fclass01 *a)
42271ca74648SRichard Henderson {
42281ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_udw_d);
42291ca74648SRichard Henderson }
42301ca74648SRichard Henderson 
42311ca74648SRichard Henderson static bool trans_fcnv_f_uw(DisasContext *ctx, arg_fclass01 *a)
42321ca74648SRichard Henderson {
42331ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_uw);
42341ca74648SRichard Henderson }
42351ca74648SRichard Henderson 
42361ca74648SRichard Henderson static bool trans_fcnv_d_uw(DisasContext *ctx, arg_fclass01 *a)
42371ca74648SRichard Henderson {
42381ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_uw);
42391ca74648SRichard Henderson }
42401ca74648SRichard Henderson 
42411ca74648SRichard Henderson static bool trans_fcnv_f_uq(DisasContext *ctx, arg_fclass01 *a)
42421ca74648SRichard Henderson {
42431ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_udw);
42441ca74648SRichard Henderson }
42451ca74648SRichard Henderson 
42461ca74648SRichard Henderson static bool trans_fcnv_d_uq(DisasContext *ctx, arg_fclass01 *a)
42471ca74648SRichard Henderson {
42481ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_udw);
42491ca74648SRichard Henderson }
42501ca74648SRichard Henderson 
42511ca74648SRichard Henderson static bool trans_fcnv_t_f_uw(DisasContext *ctx, arg_fclass01 *a)
42521ca74648SRichard Henderson {
42531ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_uw);
42541ca74648SRichard Henderson }
42551ca74648SRichard Henderson 
42561ca74648SRichard Henderson static bool trans_fcnv_t_d_uw(DisasContext *ctx, arg_fclass01 *a)
42571ca74648SRichard Henderson {
42581ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_uw);
42591ca74648SRichard Henderson }
42601ca74648SRichard Henderson 
42611ca74648SRichard Henderson static bool trans_fcnv_t_f_uq(DisasContext *ctx, arg_fclass01 *a)
42621ca74648SRichard Henderson {
42631ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_udw);
42641ca74648SRichard Henderson }
42651ca74648SRichard Henderson 
42661ca74648SRichard Henderson static bool trans_fcnv_t_d_uq(DisasContext *ctx, arg_fclass01 *a)
42671ca74648SRichard Henderson {
42681ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_udw);
42691ca74648SRichard Henderson }
42701ca74648SRichard Henderson 
42711ca74648SRichard Henderson /*
42721ca74648SRichard Henderson  * Float class 2
42731ca74648SRichard Henderson  */
42741ca74648SRichard Henderson 
42751ca74648SRichard Henderson static bool trans_fcmp_f(DisasContext *ctx, arg_fclass2 *a)
4276ebe9383cSRichard Henderson {
4277ebe9383cSRichard Henderson     TCGv_i32 ta, tb, tc, ty;
4278ebe9383cSRichard Henderson 
4279ebe9383cSRichard Henderson     nullify_over(ctx);
4280ebe9383cSRichard Henderson 
42811ca74648SRichard Henderson     ta = load_frw0_i32(a->r1);
42821ca74648SRichard Henderson     tb = load_frw0_i32(a->r2);
428329dd6f64SRichard Henderson     ty = tcg_constant_i32(a->y);
428429dd6f64SRichard Henderson     tc = tcg_constant_i32(a->c);
4285ebe9383cSRichard Henderson 
4286ad75a51eSRichard Henderson     gen_helper_fcmp_s(tcg_env, ta, tb, ty, tc);
4287ebe9383cSRichard Henderson 
42881ca74648SRichard Henderson     return nullify_end(ctx);
4289ebe9383cSRichard Henderson }
4290ebe9383cSRichard Henderson 
42911ca74648SRichard Henderson static bool trans_fcmp_d(DisasContext *ctx, arg_fclass2 *a)
4292ebe9383cSRichard Henderson {
4293ebe9383cSRichard Henderson     TCGv_i64 ta, tb;
4294ebe9383cSRichard Henderson     TCGv_i32 tc, ty;
4295ebe9383cSRichard Henderson 
4296ebe9383cSRichard Henderson     nullify_over(ctx);
4297ebe9383cSRichard Henderson 
42981ca74648SRichard Henderson     ta = load_frd0(a->r1);
42991ca74648SRichard Henderson     tb = load_frd0(a->r2);
430029dd6f64SRichard Henderson     ty = tcg_constant_i32(a->y);
430129dd6f64SRichard Henderson     tc = tcg_constant_i32(a->c);
4302ebe9383cSRichard Henderson 
4303ad75a51eSRichard Henderson     gen_helper_fcmp_d(tcg_env, ta, tb, ty, tc);
4304ebe9383cSRichard Henderson 
430531234768SRichard Henderson     return nullify_end(ctx);
4306ebe9383cSRichard Henderson }
4307ebe9383cSRichard Henderson 
43081ca74648SRichard Henderson static bool trans_ftest(DisasContext *ctx, arg_ftest *a)
4309ebe9383cSRichard Henderson {
43106fd0c7bcSRichard Henderson     TCGv_i64 t;
4311ebe9383cSRichard Henderson 
4312ebe9383cSRichard Henderson     nullify_over(ctx);
4313ebe9383cSRichard Henderson 
4314aac0f603SRichard Henderson     t = tcg_temp_new_i64();
43156fd0c7bcSRichard Henderson     tcg_gen_ld32u_i64(t, tcg_env, offsetof(CPUHPPAState, fr0_shadow));
4316ebe9383cSRichard Henderson 
43171ca74648SRichard Henderson     if (a->y == 1) {
4318ebe9383cSRichard Henderson         int mask;
4319ebe9383cSRichard Henderson         bool inv = false;
4320ebe9383cSRichard Henderson 
43211ca74648SRichard Henderson         switch (a->c) {
4322ebe9383cSRichard Henderson         case 0: /* simple */
43236fd0c7bcSRichard Henderson             tcg_gen_andi_i64(t, t, 0x4000000);
4324ebe9383cSRichard Henderson             ctx->null_cond = cond_make_0(TCG_COND_NE, t);
4325ebe9383cSRichard Henderson             goto done;
4326ebe9383cSRichard Henderson         case 2: /* rej */
4327ebe9383cSRichard Henderson             inv = true;
4328ebe9383cSRichard Henderson             /* fallthru */
4329ebe9383cSRichard Henderson         case 1: /* acc */
4330ebe9383cSRichard Henderson             mask = 0x43ff800;
4331ebe9383cSRichard Henderson             break;
4332ebe9383cSRichard Henderson         case 6: /* rej8 */
4333ebe9383cSRichard Henderson             inv = true;
4334ebe9383cSRichard Henderson             /* fallthru */
4335ebe9383cSRichard Henderson         case 5: /* acc8 */
4336ebe9383cSRichard Henderson             mask = 0x43f8000;
4337ebe9383cSRichard Henderson             break;
4338ebe9383cSRichard Henderson         case 9: /* acc6 */
4339ebe9383cSRichard Henderson             mask = 0x43e0000;
4340ebe9383cSRichard Henderson             break;
4341ebe9383cSRichard Henderson         case 13: /* acc4 */
4342ebe9383cSRichard Henderson             mask = 0x4380000;
4343ebe9383cSRichard Henderson             break;
4344ebe9383cSRichard Henderson         case 17: /* acc2 */
4345ebe9383cSRichard Henderson             mask = 0x4200000;
4346ebe9383cSRichard Henderson             break;
4347ebe9383cSRichard Henderson         default:
43481ca74648SRichard Henderson             gen_illegal(ctx);
43491ca74648SRichard Henderson             return true;
4350ebe9383cSRichard Henderson         }
4351ebe9383cSRichard Henderson         if (inv) {
43526fd0c7bcSRichard Henderson             TCGv_i64 c = tcg_constant_i64(mask);
43536fd0c7bcSRichard Henderson             tcg_gen_or_i64(t, t, c);
4354ebe9383cSRichard Henderson             ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
4355ebe9383cSRichard Henderson         } else {
43566fd0c7bcSRichard Henderson             tcg_gen_andi_i64(t, t, mask);
4357ebe9383cSRichard Henderson             ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
4358ebe9383cSRichard Henderson         }
43591ca74648SRichard Henderson     } else {
43601ca74648SRichard Henderson         unsigned cbit = (a->y ^ 1) - 1;
43611ca74648SRichard Henderson 
43626fd0c7bcSRichard Henderson         tcg_gen_extract_i64(t, t, 21 - cbit, 1);
43631ca74648SRichard Henderson         ctx->null_cond = cond_make_0(TCG_COND_NE, t);
43641ca74648SRichard Henderson     }
43651ca74648SRichard Henderson 
4366ebe9383cSRichard Henderson  done:
436731234768SRichard Henderson     return nullify_end(ctx);
4368ebe9383cSRichard Henderson }
4369ebe9383cSRichard Henderson 
43701ca74648SRichard Henderson /*
43711ca74648SRichard Henderson  * Float class 2
43721ca74648SRichard Henderson  */
43731ca74648SRichard Henderson 
43741ca74648SRichard Henderson static bool trans_fadd_f(DisasContext *ctx, arg_fclass3 *a)
4375ebe9383cSRichard Henderson {
43761ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fadd_s);
43771ca74648SRichard Henderson }
43781ca74648SRichard Henderson 
43791ca74648SRichard Henderson static bool trans_fadd_d(DisasContext *ctx, arg_fclass3 *a)
43801ca74648SRichard Henderson {
43811ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fadd_d);
43821ca74648SRichard Henderson }
43831ca74648SRichard Henderson 
43841ca74648SRichard Henderson static bool trans_fsub_f(DisasContext *ctx, arg_fclass3 *a)
43851ca74648SRichard Henderson {
43861ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fsub_s);
43871ca74648SRichard Henderson }
43881ca74648SRichard Henderson 
43891ca74648SRichard Henderson static bool trans_fsub_d(DisasContext *ctx, arg_fclass3 *a)
43901ca74648SRichard Henderson {
43911ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fsub_d);
43921ca74648SRichard Henderson }
43931ca74648SRichard Henderson 
43941ca74648SRichard Henderson static bool trans_fmpy_f(DisasContext *ctx, arg_fclass3 *a)
43951ca74648SRichard Henderson {
43961ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_s);
43971ca74648SRichard Henderson }
43981ca74648SRichard Henderson 
43991ca74648SRichard Henderson static bool trans_fmpy_d(DisasContext *ctx, arg_fclass3 *a)
44001ca74648SRichard Henderson {
44011ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_d);
44021ca74648SRichard Henderson }
44031ca74648SRichard Henderson 
44041ca74648SRichard Henderson static bool trans_fdiv_f(DisasContext *ctx, arg_fclass3 *a)
44051ca74648SRichard Henderson {
44061ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_s);
44071ca74648SRichard Henderson }
44081ca74648SRichard Henderson 
44091ca74648SRichard Henderson static bool trans_fdiv_d(DisasContext *ctx, arg_fclass3 *a)
44101ca74648SRichard Henderson {
44111ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_d);
44121ca74648SRichard Henderson }
44131ca74648SRichard Henderson 
44141ca74648SRichard Henderson static bool trans_xmpyu(DisasContext *ctx, arg_xmpyu *a)
44151ca74648SRichard Henderson {
44161ca74648SRichard Henderson     TCGv_i64 x, y;
4417ebe9383cSRichard Henderson 
4418ebe9383cSRichard Henderson     nullify_over(ctx);
4419ebe9383cSRichard Henderson 
44201ca74648SRichard Henderson     x = load_frw0_i64(a->r1);
44211ca74648SRichard Henderson     y = load_frw0_i64(a->r2);
44221ca74648SRichard Henderson     tcg_gen_mul_i64(x, x, y);
44231ca74648SRichard Henderson     save_frd(a->t, x);
4424ebe9383cSRichard Henderson 
442531234768SRichard Henderson     return nullify_end(ctx);
4426ebe9383cSRichard Henderson }
4427ebe9383cSRichard Henderson 
4428ebe9383cSRichard Henderson /* Convert the fmpyadd single-precision register encodings to standard.  */
4429ebe9383cSRichard Henderson static inline int fmpyadd_s_reg(unsigned r)
4430ebe9383cSRichard Henderson {
4431ebe9383cSRichard Henderson     return (r & 16) * 2 + 16 + (r & 15);
4432ebe9383cSRichard Henderson }
4433ebe9383cSRichard Henderson 
4434b1e2af57SRichard Henderson static bool do_fmpyadd_s(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4435ebe9383cSRichard Henderson {
4436b1e2af57SRichard Henderson     int tm = fmpyadd_s_reg(a->tm);
4437b1e2af57SRichard Henderson     int ra = fmpyadd_s_reg(a->ra);
4438b1e2af57SRichard Henderson     int ta = fmpyadd_s_reg(a->ta);
4439b1e2af57SRichard Henderson     int rm2 = fmpyadd_s_reg(a->rm2);
4440b1e2af57SRichard Henderson     int rm1 = fmpyadd_s_reg(a->rm1);
4441ebe9383cSRichard Henderson 
4442ebe9383cSRichard Henderson     nullify_over(ctx);
4443ebe9383cSRichard Henderson 
4444ebe9383cSRichard Henderson     do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
4445ebe9383cSRichard Henderson     do_fop_weww(ctx, ta, ta, ra,
4446ebe9383cSRichard Henderson                 is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
4447ebe9383cSRichard Henderson 
444831234768SRichard Henderson     return nullify_end(ctx);
4449ebe9383cSRichard Henderson }
4450ebe9383cSRichard Henderson 
4451b1e2af57SRichard Henderson static bool trans_fmpyadd_f(DisasContext *ctx, arg_mpyadd *a)
4452b1e2af57SRichard Henderson {
4453b1e2af57SRichard Henderson     return do_fmpyadd_s(ctx, a, false);
4454b1e2af57SRichard Henderson }
4455b1e2af57SRichard Henderson 
4456b1e2af57SRichard Henderson static bool trans_fmpysub_f(DisasContext *ctx, arg_mpyadd *a)
4457b1e2af57SRichard Henderson {
4458b1e2af57SRichard Henderson     return do_fmpyadd_s(ctx, a, true);
4459b1e2af57SRichard Henderson }
4460b1e2af57SRichard Henderson 
4461b1e2af57SRichard Henderson static bool do_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4462b1e2af57SRichard Henderson {
4463b1e2af57SRichard Henderson     nullify_over(ctx);
4464b1e2af57SRichard Henderson 
4465b1e2af57SRichard Henderson     do_fop_dedd(ctx, a->tm, a->rm1, a->rm2, gen_helper_fmpy_d);
4466b1e2af57SRichard Henderson     do_fop_dedd(ctx, a->ta, a->ta, a->ra,
4467b1e2af57SRichard Henderson                 is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
4468b1e2af57SRichard Henderson 
4469b1e2af57SRichard Henderson     return nullify_end(ctx);
4470b1e2af57SRichard Henderson }
4471b1e2af57SRichard Henderson 
4472b1e2af57SRichard Henderson static bool trans_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a)
4473b1e2af57SRichard Henderson {
4474b1e2af57SRichard Henderson     return do_fmpyadd_d(ctx, a, false);
4475b1e2af57SRichard Henderson }
4476b1e2af57SRichard Henderson 
4477b1e2af57SRichard Henderson static bool trans_fmpysub_d(DisasContext *ctx, arg_mpyadd *a)
4478b1e2af57SRichard Henderson {
4479b1e2af57SRichard Henderson     return do_fmpyadd_d(ctx, a, true);
4480b1e2af57SRichard Henderson }
4481b1e2af57SRichard Henderson 
4482c3bad4f8SRichard Henderson static bool trans_fmpyfadd_f(DisasContext *ctx, arg_fmpyfadd_f *a)
4483ebe9383cSRichard Henderson {
4484c3bad4f8SRichard Henderson     TCGv_i32 x, y, z;
4485ebe9383cSRichard Henderson 
4486ebe9383cSRichard Henderson     nullify_over(ctx);
4487c3bad4f8SRichard Henderson     x = load_frw0_i32(a->rm1);
4488c3bad4f8SRichard Henderson     y = load_frw0_i32(a->rm2);
4489c3bad4f8SRichard Henderson     z = load_frw0_i32(a->ra3);
4490ebe9383cSRichard Henderson 
4491c3bad4f8SRichard Henderson     if (a->neg) {
4492ad75a51eSRichard Henderson         gen_helper_fmpynfadd_s(x, tcg_env, x, y, z);
4493ebe9383cSRichard Henderson     } else {
4494ad75a51eSRichard Henderson         gen_helper_fmpyfadd_s(x, tcg_env, x, y, z);
4495ebe9383cSRichard Henderson     }
4496ebe9383cSRichard Henderson 
4497c3bad4f8SRichard Henderson     save_frw_i32(a->t, x);
449831234768SRichard Henderson     return nullify_end(ctx);
4499ebe9383cSRichard Henderson }
4500ebe9383cSRichard Henderson 
4501c3bad4f8SRichard Henderson static bool trans_fmpyfadd_d(DisasContext *ctx, arg_fmpyfadd_d *a)
4502ebe9383cSRichard Henderson {
4503c3bad4f8SRichard Henderson     TCGv_i64 x, y, z;
4504ebe9383cSRichard Henderson 
4505ebe9383cSRichard Henderson     nullify_over(ctx);
4506c3bad4f8SRichard Henderson     x = load_frd0(a->rm1);
4507c3bad4f8SRichard Henderson     y = load_frd0(a->rm2);
4508c3bad4f8SRichard Henderson     z = load_frd0(a->ra3);
4509ebe9383cSRichard Henderson 
4510c3bad4f8SRichard Henderson     if (a->neg) {
4511ad75a51eSRichard Henderson         gen_helper_fmpynfadd_d(x, tcg_env, x, y, z);
4512ebe9383cSRichard Henderson     } else {
4513ad75a51eSRichard Henderson         gen_helper_fmpyfadd_d(x, tcg_env, x, y, z);
4514ebe9383cSRichard Henderson     }
4515ebe9383cSRichard Henderson 
4516c3bad4f8SRichard Henderson     save_frd(a->t, x);
451731234768SRichard Henderson     return nullify_end(ctx);
4518ebe9383cSRichard Henderson }
4519ebe9383cSRichard Henderson 
452015da177bSSven Schnelle static bool trans_diag(DisasContext *ctx, arg_diag *a)
452115da177bSSven Schnelle {
4522cf6b28d4SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
4523cf6b28d4SHelge Deller #ifndef CONFIG_USER_ONLY
4524cf6b28d4SHelge Deller     if (a->i == 0x100) {
4525cf6b28d4SHelge Deller         /* emulate PDC BTLB, called by SeaBIOS-hppa */
4526ad75a51eSRichard Henderson         nullify_over(ctx);
4527ad75a51eSRichard Henderson         gen_helper_diag_btlb(tcg_env);
4528cf6b28d4SHelge Deller         return nullify_end(ctx);
452915da177bSSven Schnelle     }
4530dbca0835SHelge Deller     if (a->i == 0x101) {
4531dbca0835SHelge Deller         /* print char in %r26 to first serial console, used by SeaBIOS-hppa */
4532dbca0835SHelge Deller         nullify_over(ctx);
4533dbca0835SHelge Deller         gen_helper_diag_console_output(tcg_env);
4534dbca0835SHelge Deller         return nullify_end(ctx);
4535dbca0835SHelge Deller     }
4536ad75a51eSRichard Henderson #endif
4537ad75a51eSRichard Henderson     qemu_log_mask(LOG_UNIMP, "DIAG opcode 0x%04x ignored\n", a->i);
4538ad75a51eSRichard Henderson     return true;
4539ad75a51eSRichard Henderson }
454015da177bSSven Schnelle 
4541b542683dSEmilio G. Cota static void hppa_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
454261766fe9SRichard Henderson {
454351b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4544f764718dSRichard Henderson     int bound;
454561766fe9SRichard Henderson 
454651b061fbSRichard Henderson     ctx->cs = cs;
4547494737b7SRichard Henderson     ctx->tb_flags = ctx->base.tb->flags;
4548bd6243a3SRichard Henderson     ctx->is_pa20 = hppa_is_pa20(cpu_env(cs));
45493d68ee7bSRichard Henderson 
45503d68ee7bSRichard Henderson #ifdef CONFIG_USER_ONLY
4551c01e5dfbSHelge Deller     ctx->privilege = MMU_IDX_TO_PRIV(MMU_USER_IDX);
45523d68ee7bSRichard Henderson     ctx->mmu_idx = MMU_USER_IDX;
4553c01e5dfbSHelge Deller     ctx->iaoq_f = ctx->base.pc_first | ctx->privilege;
4554c01e5dfbSHelge Deller     ctx->iaoq_b = ctx->base.tb->cs_base | ctx->privilege;
4555217d1a5eSRichard Henderson     ctx->unalign = (ctx->tb_flags & TB_FLAG_UNALIGN ? MO_UNALN : MO_ALIGN);
4556c301f34eSRichard Henderson #else
4557494737b7SRichard Henderson     ctx->privilege = (ctx->tb_flags >> TB_FLAG_PRIV_SHIFT) & 3;
4558bb67ec32SRichard Henderson     ctx->mmu_idx = (ctx->tb_flags & PSW_D
4559bb67ec32SRichard Henderson                     ? PRIV_P_TO_MMU_IDX(ctx->privilege, ctx->tb_flags & PSW_P)
4560451d993dSRichard Henderson                     : ctx->tb_flags & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX);
45613d68ee7bSRichard Henderson 
4562c301f34eSRichard Henderson     /* Recover the IAOQ values from the GVA + PRIV.  */
4563c301f34eSRichard Henderson     uint64_t cs_base = ctx->base.tb->cs_base;
4564c301f34eSRichard Henderson     uint64_t iasq_f = cs_base & ~0xffffffffull;
4565c301f34eSRichard Henderson     int32_t diff = cs_base;
4566c301f34eSRichard Henderson 
4567c301f34eSRichard Henderson     ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
4568c301f34eSRichard Henderson     ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
4569c301f34eSRichard Henderson #endif
457051b061fbSRichard Henderson     ctx->iaoq_n = -1;
4571f764718dSRichard Henderson     ctx->iaoq_n_var = NULL;
457261766fe9SRichard Henderson 
4573a4db4a78SRichard Henderson     ctx->zero = tcg_constant_i64(0);
4574a4db4a78SRichard Henderson 
45753d68ee7bSRichard Henderson     /* Bound the number of instructions by those left on the page.  */
45763d68ee7bSRichard Henderson     bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
4577b542683dSEmilio G. Cota     ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
457861766fe9SRichard Henderson }
457961766fe9SRichard Henderson 
458051b061fbSRichard Henderson static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
458151b061fbSRichard Henderson {
458251b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
458361766fe9SRichard Henderson 
45843d68ee7bSRichard Henderson     /* Seed the nullification status from PSW[N], as saved in TB->FLAGS.  */
458551b061fbSRichard Henderson     ctx->null_cond = cond_make_f();
458651b061fbSRichard Henderson     ctx->psw_n_nonzero = false;
4587494737b7SRichard Henderson     if (ctx->tb_flags & PSW_N) {
458851b061fbSRichard Henderson         ctx->null_cond.c = TCG_COND_ALWAYS;
458951b061fbSRichard Henderson         ctx->psw_n_nonzero = true;
4590129e9cc3SRichard Henderson     }
459151b061fbSRichard Henderson     ctx->null_lab = NULL;
459261766fe9SRichard Henderson }
459361766fe9SRichard Henderson 
459451b061fbSRichard Henderson static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
459551b061fbSRichard Henderson {
459651b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
459751b061fbSRichard Henderson 
4598f5b5c857SRichard Henderson     tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b, 0);
4599f5b5c857SRichard Henderson     ctx->insn_start = tcg_last_op();
460051b061fbSRichard Henderson }
460151b061fbSRichard Henderson 
460251b061fbSRichard Henderson static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
460351b061fbSRichard Henderson {
460451b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4605b77af26eSRichard Henderson     CPUHPPAState *env = cpu_env(cs);
460651b061fbSRichard Henderson     DisasJumpType ret;
460751b061fbSRichard Henderson 
460851b061fbSRichard Henderson     /* Execute one insn.  */
4609ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
4610c301f34eSRichard Henderson     if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
461131234768SRichard Henderson         do_page_zero(ctx);
461231234768SRichard Henderson         ret = ctx->base.is_jmp;
4613869051eaSRichard Henderson         assert(ret != DISAS_NEXT);
4614ba1d0b44SRichard Henderson     } else
4615ba1d0b44SRichard Henderson #endif
4616ba1d0b44SRichard Henderson     {
461761766fe9SRichard Henderson         /* Always fetch the insn, even if nullified, so that we check
461861766fe9SRichard Henderson            the page permissions for execute.  */
46194e116893SIlya Leoshkevich         uint32_t insn = translator_ldl(env, &ctx->base, ctx->base.pc_next);
462061766fe9SRichard Henderson 
462161766fe9SRichard Henderson         /* Set up the IA queue for the next insn.
462261766fe9SRichard Henderson            This will be overwritten by a branch.  */
462351b061fbSRichard Henderson         if (ctx->iaoq_b == -1) {
462451b061fbSRichard Henderson             ctx->iaoq_n = -1;
4625aac0f603SRichard Henderson             ctx->iaoq_n_var = tcg_temp_new_i64();
46266fd0c7bcSRichard Henderson             tcg_gen_addi_i64(ctx->iaoq_n_var, cpu_iaoq_b, 4);
462761766fe9SRichard Henderson         } else {
462851b061fbSRichard Henderson             ctx->iaoq_n = ctx->iaoq_b + 4;
4629f764718dSRichard Henderson             ctx->iaoq_n_var = NULL;
463061766fe9SRichard Henderson         }
463161766fe9SRichard Henderson 
463251b061fbSRichard Henderson         if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
463351b061fbSRichard Henderson             ctx->null_cond.c = TCG_COND_NEVER;
4634869051eaSRichard Henderson             ret = DISAS_NEXT;
4635129e9cc3SRichard Henderson         } else {
46361a19da0dSRichard Henderson             ctx->insn = insn;
463731274b46SRichard Henderson             if (!decode(ctx, insn)) {
463831274b46SRichard Henderson                 gen_illegal(ctx);
463931274b46SRichard Henderson             }
464031234768SRichard Henderson             ret = ctx->base.is_jmp;
464151b061fbSRichard Henderson             assert(ctx->null_lab == NULL);
4642129e9cc3SRichard Henderson         }
464361766fe9SRichard Henderson     }
464461766fe9SRichard Henderson 
46453d68ee7bSRichard Henderson     /* Advance the insn queue.  Note that this check also detects
46463d68ee7bSRichard Henderson        a priority change within the instruction queue.  */
464751b061fbSRichard Henderson     if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
4648c301f34eSRichard Henderson         if (ctx->iaoq_b != -1 && ctx->iaoq_n != -1
4649c301f34eSRichard Henderson             && use_goto_tb(ctx, ctx->iaoq_b)
4650c301f34eSRichard Henderson             && (ctx->null_cond.c == TCG_COND_NEVER
4651c301f34eSRichard Henderson                 || ctx->null_cond.c == TCG_COND_ALWAYS)) {
465251b061fbSRichard Henderson             nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
465351b061fbSRichard Henderson             gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
465431234768SRichard Henderson             ctx->base.is_jmp = ret = DISAS_NORETURN;
4655129e9cc3SRichard Henderson         } else {
465631234768SRichard Henderson             ctx->base.is_jmp = ret = DISAS_IAQ_N_STALE;
465761766fe9SRichard Henderson         }
4658129e9cc3SRichard Henderson     }
465951b061fbSRichard Henderson     ctx->iaoq_f = ctx->iaoq_b;
466051b061fbSRichard Henderson     ctx->iaoq_b = ctx->iaoq_n;
4661c301f34eSRichard Henderson     ctx->base.pc_next += 4;
466261766fe9SRichard Henderson 
4663c5d0aec2SRichard Henderson     switch (ret) {
4664c5d0aec2SRichard Henderson     case DISAS_NORETURN:
4665c5d0aec2SRichard Henderson     case DISAS_IAQ_N_UPDATED:
4666c5d0aec2SRichard Henderson         break;
4667c5d0aec2SRichard Henderson 
4668c5d0aec2SRichard Henderson     case DISAS_NEXT:
4669c5d0aec2SRichard Henderson     case DISAS_IAQ_N_STALE:
4670c5d0aec2SRichard Henderson     case DISAS_IAQ_N_STALE_EXIT:
467151b061fbSRichard Henderson         if (ctx->iaoq_f == -1) {
4672a0180973SRichard Henderson             copy_iaoq_entry(ctx, cpu_iaoq_f, -1, cpu_iaoq_b);
4673741322f4SRichard Henderson             copy_iaoq_entry(ctx, cpu_iaoq_b, ctx->iaoq_n, ctx->iaoq_n_var);
4674c301f34eSRichard Henderson #ifndef CONFIG_USER_ONLY
4675c301f34eSRichard Henderson             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4676c301f34eSRichard Henderson #endif
467751b061fbSRichard Henderson             nullify_save(ctx);
4678c5d0aec2SRichard Henderson             ctx->base.is_jmp = (ret == DISAS_IAQ_N_STALE_EXIT
4679c5d0aec2SRichard Henderson                                 ? DISAS_EXIT
4680c5d0aec2SRichard Henderson                                 : DISAS_IAQ_N_UPDATED);
468151b061fbSRichard Henderson         } else if (ctx->iaoq_b == -1) {
4682a0180973SRichard Henderson             copy_iaoq_entry(ctx, cpu_iaoq_b, -1, ctx->iaoq_n_var);
468361766fe9SRichard Henderson         }
4684c5d0aec2SRichard Henderson         break;
4685c5d0aec2SRichard Henderson 
4686c5d0aec2SRichard Henderson     default:
4687c5d0aec2SRichard Henderson         g_assert_not_reached();
4688c5d0aec2SRichard Henderson     }
468961766fe9SRichard Henderson }
469061766fe9SRichard Henderson 
469151b061fbSRichard Henderson static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
469251b061fbSRichard Henderson {
469351b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4694e1b5a5edSRichard Henderson     DisasJumpType is_jmp = ctx->base.is_jmp;
469551b061fbSRichard Henderson 
4696e1b5a5edSRichard Henderson     switch (is_jmp) {
4697869051eaSRichard Henderson     case DISAS_NORETURN:
469861766fe9SRichard Henderson         break;
469951b061fbSRichard Henderson     case DISAS_TOO_MANY:
4700869051eaSRichard Henderson     case DISAS_IAQ_N_STALE:
4701e1b5a5edSRichard Henderson     case DISAS_IAQ_N_STALE_EXIT:
4702741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_f, ctx->iaoq_f, cpu_iaoq_f);
4703741322f4SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, ctx->iaoq_b, cpu_iaoq_b);
470451b061fbSRichard Henderson         nullify_save(ctx);
470561766fe9SRichard Henderson         /* FALLTHRU */
4706869051eaSRichard Henderson     case DISAS_IAQ_N_UPDATED:
47078532a14eSRichard Henderson         if (is_jmp != DISAS_IAQ_N_STALE_EXIT) {
47087f11636dSEmilio G. Cota             tcg_gen_lookup_and_goto_ptr();
47098532a14eSRichard Henderson             break;
471061766fe9SRichard Henderson         }
4711c5d0aec2SRichard Henderson         /* FALLTHRU */
4712c5d0aec2SRichard Henderson     case DISAS_EXIT:
4713c5d0aec2SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
471461766fe9SRichard Henderson         break;
471561766fe9SRichard Henderson     default:
471651b061fbSRichard Henderson         g_assert_not_reached();
471761766fe9SRichard Henderson     }
471851b061fbSRichard Henderson }
471961766fe9SRichard Henderson 
47208eb806a7SRichard Henderson static void hppa_tr_disas_log(const DisasContextBase *dcbase,
47218eb806a7SRichard Henderson                               CPUState *cs, FILE *logfile)
472251b061fbSRichard Henderson {
4723c301f34eSRichard Henderson     target_ulong pc = dcbase->pc_first;
472461766fe9SRichard Henderson 
4725ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
4726ba1d0b44SRichard Henderson     switch (pc) {
47277ad439dfSRichard Henderson     case 0x00:
47288eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x00000000:  (null)\n");
4729ba1d0b44SRichard Henderson         return;
47307ad439dfSRichard Henderson     case 0xb0:
47318eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x000000b0:  light-weight-syscall\n");
4732ba1d0b44SRichard Henderson         return;
47337ad439dfSRichard Henderson     case 0xe0:
47348eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x000000e0:  set-thread-pointer-syscall\n");
4735ba1d0b44SRichard Henderson         return;
47367ad439dfSRichard Henderson     case 0x100:
47378eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x00000100:  syscall\n");
4738ba1d0b44SRichard Henderson         return;
47397ad439dfSRichard Henderson     }
4740ba1d0b44SRichard Henderson #endif
4741ba1d0b44SRichard Henderson 
47428eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(pc));
47438eb806a7SRichard Henderson     target_disas(logfile, cs, pc, dcbase->tb->size);
474461766fe9SRichard Henderson }
474551b061fbSRichard Henderson 
474651b061fbSRichard Henderson static const TranslatorOps hppa_tr_ops = {
474751b061fbSRichard Henderson     .init_disas_context = hppa_tr_init_disas_context,
474851b061fbSRichard Henderson     .tb_start           = hppa_tr_tb_start,
474951b061fbSRichard Henderson     .insn_start         = hppa_tr_insn_start,
475051b061fbSRichard Henderson     .translate_insn     = hppa_tr_translate_insn,
475151b061fbSRichard Henderson     .tb_stop            = hppa_tr_tb_stop,
475251b061fbSRichard Henderson     .disas_log          = hppa_tr_disas_log,
475351b061fbSRichard Henderson };
475451b061fbSRichard Henderson 
4755597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
475632f0c394SAnton Johansson                            vaddr pc, void *host_pc)
475751b061fbSRichard Henderson {
475851b061fbSRichard Henderson     DisasContext ctx;
4759306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc, &hppa_tr_ops, &ctx.base);
476061766fe9SRichard Henderson }
4761