xref: /openbmc/qemu/target/hppa/translate.c (revision 43541db0c40dad6291ce1dcb76b7729f55704893)
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"
2574781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
26dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
270843563fSRichard Henderson #include "tcg/tcg-op-gvec.h"
2861766fe9SRichard Henderson #include "exec/helper-proto.h"
2961766fe9SRichard Henderson #include "exec/helper-gen.h"
30869051eaSRichard Henderson #include "exec/translator.h"
3161766fe9SRichard Henderson #include "exec/log.h"
3261766fe9SRichard Henderson 
33d53106c9SRichard Henderson #define HELPER_H "helper.h"
34d53106c9SRichard Henderson #include "exec/helper-info.c.inc"
35d53106c9SRichard Henderson #undef  HELPER_H
36d53106c9SRichard Henderson 
37aac0f603SRichard Henderson /* Choose to use explicit sizes within this file. */
38aac0f603SRichard Henderson #undef tcg_temp_new
39d53106c9SRichard Henderson 
4061766fe9SRichard Henderson typedef struct DisasCond {
4161766fe9SRichard Henderson     TCGCond c;
426fd0c7bcSRichard Henderson     TCGv_i64 a0, a1;
4361766fe9SRichard Henderson } DisasCond;
4461766fe9SRichard Henderson 
4561766fe9SRichard Henderson typedef struct DisasContext {
46d01a3625SRichard Henderson     DisasContextBase base;
4761766fe9SRichard Henderson     CPUState *cs;
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;
6524638bd1SRichard Henderson     bool insn_start_updated;
66217d1a5eSRichard Henderson 
67217d1a5eSRichard Henderson #ifdef CONFIG_USER_ONLY
68217d1a5eSRichard Henderson     MemOp unalign;
69217d1a5eSRichard Henderson #endif
7061766fe9SRichard Henderson } DisasContext;
7161766fe9SRichard Henderson 
72217d1a5eSRichard Henderson #ifdef CONFIG_USER_ONLY
73217d1a5eSRichard Henderson #define UNALIGN(C)       (C)->unalign
7417fe594cSRichard Henderson #define MMU_DISABLED(C)  false
75217d1a5eSRichard Henderson #else
762d4afb03SRichard Henderson #define UNALIGN(C)       MO_ALIGN
7717fe594cSRichard Henderson #define MMU_DISABLED(C)  MMU_IDX_MMU_DISABLED((C)->mmu_idx)
78217d1a5eSRichard Henderson #endif
79217d1a5eSRichard Henderson 
80e36f27efSRichard Henderson /* Note that ssm/rsm instructions number PSW_W and PSW_E differently.  */
81451e4ffdSRichard Henderson static int expand_sm_imm(DisasContext *ctx, int val)
82e36f27efSRichard Henderson {
83881d1073SHelge Deller     /* Keep unimplemented bits disabled -- see cpu_hppa_put_psw. */
84881d1073SHelge Deller     if (ctx->is_pa20) {
85e36f27efSRichard Henderson         if (val & PSW_SM_W) {
86881d1073SHelge Deller             val |= PSW_W;
87881d1073SHelge Deller         }
88881d1073SHelge Deller         val &= ~(PSW_SM_W | PSW_SM_E | PSW_G);
89881d1073SHelge Deller     } else {
90881d1073SHelge Deller         val &= ~(PSW_SM_W | PSW_SM_E | PSW_O);
91e36f27efSRichard Henderson     }
92e36f27efSRichard Henderson     return val;
93e36f27efSRichard Henderson }
94e36f27efSRichard Henderson 
95deee69a1SRichard Henderson /* Inverted space register indicates 0 means sr0 not inferred from base.  */
96451e4ffdSRichard Henderson static int expand_sr3x(DisasContext *ctx, int val)
97deee69a1SRichard Henderson {
98deee69a1SRichard Henderson     return ~val;
99deee69a1SRichard Henderson }
100deee69a1SRichard Henderson 
1011cd012a5SRichard Henderson /* Convert the M:A bits within a memory insn to the tri-state value
1021cd012a5SRichard Henderson    we use for the final M.  */
103451e4ffdSRichard Henderson static int ma_to_m(DisasContext *ctx, int val)
1041cd012a5SRichard Henderson {
1051cd012a5SRichard Henderson     return val & 2 ? (val & 1 ? -1 : 1) : 0;
1061cd012a5SRichard Henderson }
1071cd012a5SRichard Henderson 
108740038d7SRichard Henderson /* Convert the sign of the displacement to a pre or post-modify.  */
109451e4ffdSRichard Henderson static int pos_to_m(DisasContext *ctx, int val)
110740038d7SRichard Henderson {
111740038d7SRichard Henderson     return val ? 1 : -1;
112740038d7SRichard Henderson }
113740038d7SRichard Henderson 
114451e4ffdSRichard Henderson static int neg_to_m(DisasContext *ctx, int val)
115740038d7SRichard Henderson {
116740038d7SRichard Henderson     return val ? -1 : 1;
117740038d7SRichard Henderson }
118740038d7SRichard Henderson 
119740038d7SRichard Henderson /* Used for branch targets and fp memory ops.  */
120451e4ffdSRichard Henderson static int expand_shl2(DisasContext *ctx, int val)
12101afb7beSRichard Henderson {
12201afb7beSRichard Henderson     return val << 2;
12301afb7beSRichard Henderson }
12401afb7beSRichard Henderson 
1250588e061SRichard Henderson /* Used for assemble_21.  */
126451e4ffdSRichard Henderson static int expand_shl11(DisasContext *ctx, int val)
1270588e061SRichard Henderson {
1280588e061SRichard Henderson     return val << 11;
1290588e061SRichard Henderson }
1300588e061SRichard Henderson 
13172ae4f2bSRichard Henderson static int assemble_6(DisasContext *ctx, int val)
13272ae4f2bSRichard Henderson {
13372ae4f2bSRichard Henderson     /*
13472ae4f2bSRichard Henderson      * Officially, 32 * x + 32 - y.
13572ae4f2bSRichard Henderson      * Here, x is already in bit 5, and y is [4:0].
13672ae4f2bSRichard Henderson      * Since -y = ~y + 1, in 5 bits 32 - y => y ^ 31 + 1,
13772ae4f2bSRichard Henderson      * with the overflow from bit 4 summing with x.
13872ae4f2bSRichard Henderson      */
13972ae4f2bSRichard Henderson     return (val ^ 31) + 1;
14072ae4f2bSRichard Henderson }
14172ae4f2bSRichard Henderson 
1424768c28eSRichard Henderson /* Expander for assemble_16a(s,cat(im10a,0),i). */
1434768c28eSRichard Henderson static int expand_11a(DisasContext *ctx, int val)
1444768c28eSRichard Henderson {
1454768c28eSRichard Henderson     /*
1464768c28eSRichard Henderson      * @val is bit 0 and bits [4:15].
1474768c28eSRichard Henderson      * Swizzle thing around depending on PSW.W.
1484768c28eSRichard Henderson      */
1494768c28eSRichard Henderson     int im10a = extract32(val, 1, 10);
1504768c28eSRichard Henderson     int s = extract32(val, 11, 2);
1514768c28eSRichard Henderson     int i = (-(val & 1) << 13) | (im10a << 3);
1524768c28eSRichard Henderson 
1534768c28eSRichard Henderson     if (ctx->tb_flags & PSW_W) {
1544768c28eSRichard Henderson         i ^= s << 13;
1554768c28eSRichard Henderson     }
1564768c28eSRichard Henderson     return i;
1574768c28eSRichard Henderson }
1584768c28eSRichard Henderson 
15946174e14SRichard Henderson /* Expander for assemble_16a(s,im11a,i). */
16046174e14SRichard Henderson static int expand_12a(DisasContext *ctx, int val)
16146174e14SRichard Henderson {
16246174e14SRichard Henderson     /*
16346174e14SRichard Henderson      * @val is bit 0 and bits [3:15].
16446174e14SRichard Henderson      * Swizzle thing around depending on PSW.W.
16546174e14SRichard Henderson      */
16646174e14SRichard Henderson     int im11a = extract32(val, 1, 11);
16746174e14SRichard Henderson     int s = extract32(val, 12, 2);
16846174e14SRichard Henderson     int i = (-(val & 1) << 13) | (im11a << 2);
16946174e14SRichard Henderson 
17046174e14SRichard Henderson     if (ctx->tb_flags & PSW_W) {
17146174e14SRichard Henderson         i ^= s << 13;
17246174e14SRichard Henderson     }
17346174e14SRichard Henderson     return i;
17446174e14SRichard Henderson }
17546174e14SRichard Henderson 
17672bace2dSRichard Henderson /* Expander for assemble_16(s,im14). */
17772bace2dSRichard Henderson static int expand_16(DisasContext *ctx, int val)
17872bace2dSRichard Henderson {
17972bace2dSRichard Henderson     /*
18072bace2dSRichard Henderson      * @val is bits [0:15], containing both im14 and s.
18172bace2dSRichard Henderson      * Swizzle thing around depending on PSW.W.
18272bace2dSRichard Henderson      */
18372bace2dSRichard Henderson     int s = extract32(val, 14, 2);
18472bace2dSRichard Henderson     int i = (-(val & 1) << 13) | extract32(val, 1, 13);
18572bace2dSRichard Henderson 
18672bace2dSRichard Henderson     if (ctx->tb_flags & PSW_W) {
18772bace2dSRichard Henderson         i ^= s << 13;
18872bace2dSRichard Henderson     }
18972bace2dSRichard Henderson     return i;
19072bace2dSRichard Henderson }
19172bace2dSRichard Henderson 
19272bace2dSRichard Henderson /* The sp field is only present with !PSW_W. */
19372bace2dSRichard Henderson static int sp0_if_wide(DisasContext *ctx, int sp)
19472bace2dSRichard Henderson {
19572bace2dSRichard Henderson     return ctx->tb_flags & PSW_W ? 0 : sp;
19672bace2dSRichard Henderson }
19772bace2dSRichard Henderson 
198c65c3ee1SRichard Henderson /* Translate CMPI doubleword conditions to standard. */
199c65c3ee1SRichard Henderson static int cmpbid_c(DisasContext *ctx, int val)
200c65c3ee1SRichard Henderson {
201c65c3ee1SRichard Henderson     return val ? val : 4; /* 0 == "*<<" */
202c65c3ee1SRichard Henderson }
203c65c3ee1SRichard Henderson 
20482d0c831SRichard Henderson /*
20582d0c831SRichard Henderson  * In many places pa1.x did not decode the bit that later became
20682d0c831SRichard Henderson  * the pa2.0 D bit.  Suppress D unless the cpu is pa2.0.
20782d0c831SRichard Henderson  */
20882d0c831SRichard Henderson static int pa20_d(DisasContext *ctx, int val)
20982d0c831SRichard Henderson {
21082d0c831SRichard Henderson     return ctx->is_pa20 & val;
21182d0c831SRichard Henderson }
21201afb7beSRichard Henderson 
21340f9f908SRichard Henderson /* Include the auto-generated decoder.  */
214abff1abfSPaolo Bonzini #include "decode-insns.c.inc"
21540f9f908SRichard Henderson 
21661766fe9SRichard Henderson /* We are not using a goto_tb (for whatever reason), but have updated
21761766fe9SRichard Henderson    the iaq (for whatever reason), so don't do it again on exit.  */
218869051eaSRichard Henderson #define DISAS_IAQ_N_UPDATED  DISAS_TARGET_0
21961766fe9SRichard Henderson 
22061766fe9SRichard Henderson /* We are exiting the TB, but have neither emitted a goto_tb, nor
22161766fe9SRichard Henderson    updated the iaq for the next instruction to be executed.  */
222869051eaSRichard Henderson #define DISAS_IAQ_N_STALE    DISAS_TARGET_1
22361766fe9SRichard Henderson 
224e1b5a5edSRichard Henderson /* Similarly, but we want to return to the main loop immediately
225e1b5a5edSRichard Henderson    to recognize unmasked interrupts.  */
226e1b5a5edSRichard Henderson #define DISAS_IAQ_N_STALE_EXIT      DISAS_TARGET_2
227c5d0aec2SRichard Henderson #define DISAS_EXIT                  DISAS_TARGET_3
228e1b5a5edSRichard Henderson 
22961766fe9SRichard Henderson /* global register indexes */
2306fd0c7bcSRichard Henderson static TCGv_i64 cpu_gr[32];
23133423472SRichard Henderson static TCGv_i64 cpu_sr[4];
232494737b7SRichard Henderson static TCGv_i64 cpu_srH;
2336fd0c7bcSRichard Henderson static TCGv_i64 cpu_iaoq_f;
2346fd0c7bcSRichard Henderson static TCGv_i64 cpu_iaoq_b;
235c301f34eSRichard Henderson static TCGv_i64 cpu_iasq_f;
236c301f34eSRichard Henderson static TCGv_i64 cpu_iasq_b;
2376fd0c7bcSRichard Henderson static TCGv_i64 cpu_sar;
2386fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_n;
2396fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_v;
2406fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_cb;
2416fd0c7bcSRichard Henderson static TCGv_i64 cpu_psw_cb_msb;
24261766fe9SRichard Henderson 
24361766fe9SRichard Henderson void hppa_translate_init(void)
24461766fe9SRichard Henderson {
24561766fe9SRichard Henderson #define DEF_VAR(V)  { &cpu_##V, #V, offsetof(CPUHPPAState, V) }
24661766fe9SRichard Henderson 
2476fd0c7bcSRichard Henderson     typedef struct { TCGv_i64 *var; const char *name; int ofs; } GlobalVar;
24861766fe9SRichard Henderson     static const GlobalVar vars[] = {
24935136a77SRichard Henderson         { &cpu_sar, "sar", offsetof(CPUHPPAState, cr[CR_SAR]) },
25061766fe9SRichard Henderson         DEF_VAR(psw_n),
25161766fe9SRichard Henderson         DEF_VAR(psw_v),
25261766fe9SRichard Henderson         DEF_VAR(psw_cb),
25361766fe9SRichard Henderson         DEF_VAR(psw_cb_msb),
25461766fe9SRichard Henderson         DEF_VAR(iaoq_f),
25561766fe9SRichard Henderson         DEF_VAR(iaoq_b),
25661766fe9SRichard Henderson     };
25761766fe9SRichard Henderson 
25861766fe9SRichard Henderson #undef DEF_VAR
25961766fe9SRichard Henderson 
26061766fe9SRichard Henderson     /* Use the symbolic register names that match the disassembler.  */
26161766fe9SRichard Henderson     static const char gr_names[32][4] = {
26261766fe9SRichard Henderson         "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
26361766fe9SRichard Henderson         "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
26461766fe9SRichard Henderson         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
26561766fe9SRichard Henderson         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
26661766fe9SRichard Henderson     };
26733423472SRichard Henderson     /* SR[4-7] are not global registers so that we can index them.  */
268494737b7SRichard Henderson     static const char sr_names[5][4] = {
269494737b7SRichard Henderson         "sr0", "sr1", "sr2", "sr3", "srH"
27033423472SRichard Henderson     };
27161766fe9SRichard Henderson 
27261766fe9SRichard Henderson     int i;
27361766fe9SRichard Henderson 
274f764718dSRichard Henderson     cpu_gr[0] = NULL;
27561766fe9SRichard Henderson     for (i = 1; i < 32; i++) {
276ad75a51eSRichard Henderson         cpu_gr[i] = tcg_global_mem_new(tcg_env,
27761766fe9SRichard Henderson                                        offsetof(CPUHPPAState, gr[i]),
27861766fe9SRichard Henderson                                        gr_names[i]);
27961766fe9SRichard Henderson     }
28033423472SRichard Henderson     for (i = 0; i < 4; i++) {
281ad75a51eSRichard Henderson         cpu_sr[i] = tcg_global_mem_new_i64(tcg_env,
28233423472SRichard Henderson                                            offsetof(CPUHPPAState, sr[i]),
28333423472SRichard Henderson                                            sr_names[i]);
28433423472SRichard Henderson     }
285ad75a51eSRichard Henderson     cpu_srH = tcg_global_mem_new_i64(tcg_env,
286494737b7SRichard Henderson                                      offsetof(CPUHPPAState, sr[4]),
287494737b7SRichard Henderson                                      sr_names[4]);
28861766fe9SRichard Henderson 
28961766fe9SRichard Henderson     for (i = 0; i < ARRAY_SIZE(vars); ++i) {
29061766fe9SRichard Henderson         const GlobalVar *v = &vars[i];
291ad75a51eSRichard Henderson         *v->var = tcg_global_mem_new(tcg_env, v->ofs, v->name);
29261766fe9SRichard Henderson     }
293c301f34eSRichard Henderson 
294ad75a51eSRichard Henderson     cpu_iasq_f = tcg_global_mem_new_i64(tcg_env,
295c301f34eSRichard Henderson                                         offsetof(CPUHPPAState, iasq_f),
296c301f34eSRichard Henderson                                         "iasq_f");
297ad75a51eSRichard Henderson     cpu_iasq_b = tcg_global_mem_new_i64(tcg_env,
298c301f34eSRichard Henderson                                         offsetof(CPUHPPAState, iasq_b),
299c301f34eSRichard Henderson                                         "iasq_b");
30061766fe9SRichard Henderson }
30161766fe9SRichard Henderson 
302f5b5c857SRichard Henderson static void set_insn_breg(DisasContext *ctx, int breg)
303f5b5c857SRichard Henderson {
30424638bd1SRichard Henderson     assert(!ctx->insn_start_updated);
30524638bd1SRichard Henderson     ctx->insn_start_updated = true;
30624638bd1SRichard Henderson     tcg_set_insn_start_param(ctx->base.insn_start, 2, breg);
307f5b5c857SRichard Henderson }
308f5b5c857SRichard Henderson 
309129e9cc3SRichard Henderson static DisasCond cond_make_f(void)
310129e9cc3SRichard Henderson {
311f764718dSRichard Henderson     return (DisasCond){
312f764718dSRichard Henderson         .c = TCG_COND_NEVER,
313f764718dSRichard Henderson         .a0 = NULL,
314f764718dSRichard Henderson         .a1 = NULL,
315f764718dSRichard Henderson     };
316129e9cc3SRichard Henderson }
317129e9cc3SRichard Henderson 
318df0232feSRichard Henderson static DisasCond cond_make_t(void)
319df0232feSRichard Henderson {
320df0232feSRichard Henderson     return (DisasCond){
321df0232feSRichard Henderson         .c = TCG_COND_ALWAYS,
322df0232feSRichard Henderson         .a0 = NULL,
323df0232feSRichard Henderson         .a1 = NULL,
324df0232feSRichard Henderson     };
325df0232feSRichard Henderson }
326df0232feSRichard Henderson 
327129e9cc3SRichard Henderson static DisasCond cond_make_n(void)
328129e9cc3SRichard Henderson {
329f764718dSRichard Henderson     return (DisasCond){
330f764718dSRichard Henderson         .c = TCG_COND_NE,
331f764718dSRichard Henderson         .a0 = cpu_psw_n,
3326fd0c7bcSRichard Henderson         .a1 = tcg_constant_i64(0)
333f764718dSRichard Henderson     };
334129e9cc3SRichard Henderson }
335129e9cc3SRichard Henderson 
3366fd0c7bcSRichard Henderson static DisasCond cond_make_tmp(TCGCond c, TCGv_i64 a0, TCGv_i64 a1)
337b47a4a02SSven Schnelle {
338b47a4a02SSven Schnelle     assert (c != TCG_COND_NEVER && c != TCG_COND_ALWAYS);
3394fe9533aSRichard Henderson     return (DisasCond){ .c = c, .a0 = a0, .a1 = a1 };
3404fe9533aSRichard Henderson }
3414fe9533aSRichard Henderson 
3426fd0c7bcSRichard Henderson static DisasCond cond_make_0_tmp(TCGCond c, TCGv_i64 a0)
3434fe9533aSRichard Henderson {
3446fd0c7bcSRichard Henderson     return cond_make_tmp(c, a0, tcg_constant_i64(0));
345b47a4a02SSven Schnelle }
346b47a4a02SSven Schnelle 
3476fd0c7bcSRichard Henderson static DisasCond cond_make_0(TCGCond c, TCGv_i64 a0)
348129e9cc3SRichard Henderson {
349aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3506fd0c7bcSRichard Henderson     tcg_gen_mov_i64(tmp, a0);
351b47a4a02SSven Schnelle     return cond_make_0_tmp(c, tmp);
352129e9cc3SRichard Henderson }
353129e9cc3SRichard Henderson 
3546fd0c7bcSRichard Henderson static DisasCond cond_make(TCGCond c, TCGv_i64 a0, TCGv_i64 a1)
355129e9cc3SRichard Henderson {
356aac0f603SRichard Henderson     TCGv_i64 t0 = tcg_temp_new_i64();
357aac0f603SRichard Henderson     TCGv_i64 t1 = tcg_temp_new_i64();
358129e9cc3SRichard Henderson 
3596fd0c7bcSRichard Henderson     tcg_gen_mov_i64(t0, a0);
3606fd0c7bcSRichard Henderson     tcg_gen_mov_i64(t1, a1);
3614fe9533aSRichard Henderson     return cond_make_tmp(c, t0, t1);
362129e9cc3SRichard Henderson }
363129e9cc3SRichard Henderson 
364129e9cc3SRichard Henderson static void cond_free(DisasCond *cond)
365129e9cc3SRichard Henderson {
366129e9cc3SRichard Henderson     switch (cond->c) {
367129e9cc3SRichard Henderson     default:
368f764718dSRichard Henderson         cond->a0 = NULL;
369f764718dSRichard Henderson         cond->a1 = NULL;
370129e9cc3SRichard Henderson         /* fallthru */
371129e9cc3SRichard Henderson     case TCG_COND_ALWAYS:
372129e9cc3SRichard Henderson         cond->c = TCG_COND_NEVER;
373129e9cc3SRichard Henderson         break;
374129e9cc3SRichard Henderson     case TCG_COND_NEVER:
375129e9cc3SRichard Henderson         break;
376129e9cc3SRichard Henderson     }
377129e9cc3SRichard Henderson }
378129e9cc3SRichard Henderson 
3796fd0c7bcSRichard Henderson static TCGv_i64 load_gpr(DisasContext *ctx, unsigned reg)
38061766fe9SRichard Henderson {
38161766fe9SRichard Henderson     if (reg == 0) {
382bc3da3cfSRichard Henderson         return ctx->zero;
38361766fe9SRichard Henderson     } else {
38461766fe9SRichard Henderson         return cpu_gr[reg];
38561766fe9SRichard Henderson     }
38661766fe9SRichard Henderson }
38761766fe9SRichard Henderson 
3886fd0c7bcSRichard Henderson static TCGv_i64 dest_gpr(DisasContext *ctx, unsigned reg)
38961766fe9SRichard Henderson {
390129e9cc3SRichard Henderson     if (reg == 0 || ctx->null_cond.c != TCG_COND_NEVER) {
391aac0f603SRichard Henderson         return tcg_temp_new_i64();
39261766fe9SRichard Henderson     } else {
39361766fe9SRichard Henderson         return cpu_gr[reg];
39461766fe9SRichard Henderson     }
39561766fe9SRichard Henderson }
39661766fe9SRichard Henderson 
3976fd0c7bcSRichard Henderson static void save_or_nullify(DisasContext *ctx, TCGv_i64 dest, TCGv_i64 t)
398129e9cc3SRichard Henderson {
399129e9cc3SRichard Henderson     if (ctx->null_cond.c != TCG_COND_NEVER) {
4006fd0c7bcSRichard Henderson         tcg_gen_movcond_i64(ctx->null_cond.c, dest, ctx->null_cond.a0,
401129e9cc3SRichard Henderson                             ctx->null_cond.a1, dest, t);
402129e9cc3SRichard Henderson     } else {
4036fd0c7bcSRichard Henderson         tcg_gen_mov_i64(dest, t);
404129e9cc3SRichard Henderson     }
405129e9cc3SRichard Henderson }
406129e9cc3SRichard Henderson 
4076fd0c7bcSRichard Henderson static void save_gpr(DisasContext *ctx, unsigned reg, TCGv_i64 t)
408129e9cc3SRichard Henderson {
409129e9cc3SRichard Henderson     if (reg != 0) {
410129e9cc3SRichard Henderson         save_or_nullify(ctx, cpu_gr[reg], t);
411129e9cc3SRichard Henderson     }
412129e9cc3SRichard Henderson }
413129e9cc3SRichard Henderson 
414e03b5686SMarc-André Lureau #if HOST_BIG_ENDIAN
41596d6407fSRichard Henderson # define HI_OFS  0
41696d6407fSRichard Henderson # define LO_OFS  4
41796d6407fSRichard Henderson #else
41896d6407fSRichard Henderson # define HI_OFS  4
41996d6407fSRichard Henderson # define LO_OFS  0
42096d6407fSRichard Henderson #endif
42196d6407fSRichard Henderson 
42296d6407fSRichard Henderson static TCGv_i32 load_frw_i32(unsigned rt)
42396d6407fSRichard Henderson {
42496d6407fSRichard Henderson     TCGv_i32 ret = tcg_temp_new_i32();
425ad75a51eSRichard Henderson     tcg_gen_ld_i32(ret, tcg_env,
42696d6407fSRichard Henderson                    offsetof(CPUHPPAState, fr[rt & 31])
42796d6407fSRichard Henderson                    + (rt & 32 ? LO_OFS : HI_OFS));
42896d6407fSRichard Henderson     return ret;
42996d6407fSRichard Henderson }
43096d6407fSRichard Henderson 
431ebe9383cSRichard Henderson static TCGv_i32 load_frw0_i32(unsigned rt)
432ebe9383cSRichard Henderson {
433ebe9383cSRichard Henderson     if (rt == 0) {
4340992a930SRichard Henderson         TCGv_i32 ret = tcg_temp_new_i32();
4350992a930SRichard Henderson         tcg_gen_movi_i32(ret, 0);
4360992a930SRichard Henderson         return ret;
437ebe9383cSRichard Henderson     } else {
438ebe9383cSRichard Henderson         return load_frw_i32(rt);
439ebe9383cSRichard Henderson     }
440ebe9383cSRichard Henderson }
441ebe9383cSRichard Henderson 
442ebe9383cSRichard Henderson static TCGv_i64 load_frw0_i64(unsigned rt)
443ebe9383cSRichard Henderson {
444ebe9383cSRichard Henderson     TCGv_i64 ret = tcg_temp_new_i64();
4450992a930SRichard Henderson     if (rt == 0) {
4460992a930SRichard Henderson         tcg_gen_movi_i64(ret, 0);
4470992a930SRichard Henderson     } else {
448ad75a51eSRichard Henderson         tcg_gen_ld32u_i64(ret, tcg_env,
449ebe9383cSRichard Henderson                           offsetof(CPUHPPAState, fr[rt & 31])
450ebe9383cSRichard Henderson                           + (rt & 32 ? LO_OFS : HI_OFS));
451ebe9383cSRichard Henderson     }
4520992a930SRichard Henderson     return ret;
453ebe9383cSRichard Henderson }
454ebe9383cSRichard Henderson 
45596d6407fSRichard Henderson static void save_frw_i32(unsigned rt, TCGv_i32 val)
45696d6407fSRichard Henderson {
457ad75a51eSRichard Henderson     tcg_gen_st_i32(val, tcg_env,
45896d6407fSRichard Henderson                    offsetof(CPUHPPAState, fr[rt & 31])
45996d6407fSRichard Henderson                    + (rt & 32 ? LO_OFS : HI_OFS));
46096d6407fSRichard Henderson }
46196d6407fSRichard Henderson 
46296d6407fSRichard Henderson #undef HI_OFS
46396d6407fSRichard Henderson #undef LO_OFS
46496d6407fSRichard Henderson 
46596d6407fSRichard Henderson static TCGv_i64 load_frd(unsigned rt)
46696d6407fSRichard Henderson {
46796d6407fSRichard Henderson     TCGv_i64 ret = tcg_temp_new_i64();
468ad75a51eSRichard Henderson     tcg_gen_ld_i64(ret, tcg_env, offsetof(CPUHPPAState, fr[rt]));
46996d6407fSRichard Henderson     return ret;
47096d6407fSRichard Henderson }
47196d6407fSRichard Henderson 
472ebe9383cSRichard Henderson static TCGv_i64 load_frd0(unsigned rt)
473ebe9383cSRichard Henderson {
474ebe9383cSRichard Henderson     if (rt == 0) {
4750992a930SRichard Henderson         TCGv_i64 ret = tcg_temp_new_i64();
4760992a930SRichard Henderson         tcg_gen_movi_i64(ret, 0);
4770992a930SRichard Henderson         return ret;
478ebe9383cSRichard Henderson     } else {
479ebe9383cSRichard Henderson         return load_frd(rt);
480ebe9383cSRichard Henderson     }
481ebe9383cSRichard Henderson }
482ebe9383cSRichard Henderson 
48396d6407fSRichard Henderson static void save_frd(unsigned rt, TCGv_i64 val)
48496d6407fSRichard Henderson {
485ad75a51eSRichard Henderson     tcg_gen_st_i64(val, tcg_env, offsetof(CPUHPPAState, fr[rt]));
48696d6407fSRichard Henderson }
48796d6407fSRichard Henderson 
48833423472SRichard Henderson static void load_spr(DisasContext *ctx, TCGv_i64 dest, unsigned reg)
48933423472SRichard Henderson {
49033423472SRichard Henderson #ifdef CONFIG_USER_ONLY
49133423472SRichard Henderson     tcg_gen_movi_i64(dest, 0);
49233423472SRichard Henderson #else
49333423472SRichard Henderson     if (reg < 4) {
49433423472SRichard Henderson         tcg_gen_mov_i64(dest, cpu_sr[reg]);
495494737b7SRichard Henderson     } else if (ctx->tb_flags & TB_FLAG_SR_SAME) {
496494737b7SRichard Henderson         tcg_gen_mov_i64(dest, cpu_srH);
49733423472SRichard Henderson     } else {
498ad75a51eSRichard Henderson         tcg_gen_ld_i64(dest, tcg_env, offsetof(CPUHPPAState, sr[reg]));
49933423472SRichard Henderson     }
50033423472SRichard Henderson #endif
50133423472SRichard Henderson }
50233423472SRichard Henderson 
503129e9cc3SRichard Henderson /* Skip over the implementation of an insn that has been nullified.
504129e9cc3SRichard Henderson    Use this when the insn is too complex for a conditional move.  */
505129e9cc3SRichard Henderson static void nullify_over(DisasContext *ctx)
506129e9cc3SRichard Henderson {
507129e9cc3SRichard Henderson     if (ctx->null_cond.c != TCG_COND_NEVER) {
508129e9cc3SRichard Henderson         /* The always condition should have been handled in the main loop.  */
509129e9cc3SRichard Henderson         assert(ctx->null_cond.c != TCG_COND_ALWAYS);
510129e9cc3SRichard Henderson 
511129e9cc3SRichard Henderson         ctx->null_lab = gen_new_label();
512129e9cc3SRichard Henderson 
513129e9cc3SRichard Henderson         /* If we're using PSW[N], copy it to a temp because... */
5146e94937aSRichard Henderson         if (ctx->null_cond.a0 == cpu_psw_n) {
515aac0f603SRichard Henderson             ctx->null_cond.a0 = tcg_temp_new_i64();
5166fd0c7bcSRichard Henderson             tcg_gen_mov_i64(ctx->null_cond.a0, cpu_psw_n);
517129e9cc3SRichard Henderson         }
518129e9cc3SRichard Henderson         /* ... we clear it before branching over the implementation,
519129e9cc3SRichard Henderson            so that (1) it's clear after nullifying this insn and
520129e9cc3SRichard Henderson            (2) if this insn nullifies the next, PSW[N] is valid.  */
521129e9cc3SRichard Henderson         if (ctx->psw_n_nonzero) {
522129e9cc3SRichard Henderson             ctx->psw_n_nonzero = false;
5236fd0c7bcSRichard Henderson             tcg_gen_movi_i64(cpu_psw_n, 0);
524129e9cc3SRichard Henderson         }
525129e9cc3SRichard Henderson 
5266fd0c7bcSRichard Henderson         tcg_gen_brcond_i64(ctx->null_cond.c, ctx->null_cond.a0,
527129e9cc3SRichard Henderson                            ctx->null_cond.a1, ctx->null_lab);
528129e9cc3SRichard Henderson         cond_free(&ctx->null_cond);
529129e9cc3SRichard Henderson     }
530129e9cc3SRichard Henderson }
531129e9cc3SRichard Henderson 
532129e9cc3SRichard Henderson /* Save the current nullification state to PSW[N].  */
533129e9cc3SRichard Henderson static void nullify_save(DisasContext *ctx)
534129e9cc3SRichard Henderson {
535129e9cc3SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER) {
536129e9cc3SRichard Henderson         if (ctx->psw_n_nonzero) {
5376fd0c7bcSRichard Henderson             tcg_gen_movi_i64(cpu_psw_n, 0);
538129e9cc3SRichard Henderson         }
539129e9cc3SRichard Henderson         return;
540129e9cc3SRichard Henderson     }
5416e94937aSRichard Henderson     if (ctx->null_cond.a0 != cpu_psw_n) {
5426fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(ctx->null_cond.c, cpu_psw_n,
543129e9cc3SRichard Henderson                             ctx->null_cond.a0, ctx->null_cond.a1);
544129e9cc3SRichard Henderson         ctx->psw_n_nonzero = true;
545129e9cc3SRichard Henderson     }
546129e9cc3SRichard Henderson     cond_free(&ctx->null_cond);
547129e9cc3SRichard Henderson }
548129e9cc3SRichard Henderson 
549129e9cc3SRichard Henderson /* Set a PSW[N] to X.  The intention is that this is used immediately
550129e9cc3SRichard Henderson    before a goto_tb/exit_tb, so that there is no fallthru path to other
551129e9cc3SRichard Henderson    code within the TB.  Therefore we do not update psw_n_nonzero.  */
552129e9cc3SRichard Henderson static void nullify_set(DisasContext *ctx, bool x)
553129e9cc3SRichard Henderson {
554129e9cc3SRichard Henderson     if (ctx->psw_n_nonzero || x) {
5556fd0c7bcSRichard Henderson         tcg_gen_movi_i64(cpu_psw_n, x);
556129e9cc3SRichard Henderson     }
557129e9cc3SRichard Henderson }
558129e9cc3SRichard Henderson 
559129e9cc3SRichard Henderson /* Mark the end of an instruction that may have been nullified.
56040f9f908SRichard Henderson    This is the pair to nullify_over.  Always returns true so that
56140f9f908SRichard Henderson    it may be tail-called from a translate function.  */
56231234768SRichard Henderson static bool nullify_end(DisasContext *ctx)
563129e9cc3SRichard Henderson {
564129e9cc3SRichard Henderson     TCGLabel *null_lab = ctx->null_lab;
56531234768SRichard Henderson     DisasJumpType status = ctx->base.is_jmp;
566129e9cc3SRichard Henderson 
567f49b3537SRichard Henderson     /* For NEXT, NORETURN, STALE, we can easily continue (or exit).
568f49b3537SRichard Henderson        For UPDATED, we cannot update on the nullified path.  */
569f49b3537SRichard Henderson     assert(status != DISAS_IAQ_N_UPDATED);
570f49b3537SRichard Henderson 
571129e9cc3SRichard Henderson     if (likely(null_lab == NULL)) {
572129e9cc3SRichard Henderson         /* The current insn wasn't conditional or handled the condition
573129e9cc3SRichard Henderson            applied to it without a branch, so the (new) setting of
574129e9cc3SRichard Henderson            NULL_COND can be applied directly to the next insn.  */
57531234768SRichard Henderson         return true;
576129e9cc3SRichard Henderson     }
577129e9cc3SRichard Henderson     ctx->null_lab = NULL;
578129e9cc3SRichard Henderson 
579129e9cc3SRichard Henderson     if (likely(ctx->null_cond.c == TCG_COND_NEVER)) {
580129e9cc3SRichard Henderson         /* The next instruction will be unconditional,
581129e9cc3SRichard Henderson            and NULL_COND already reflects that.  */
582129e9cc3SRichard Henderson         gen_set_label(null_lab);
583129e9cc3SRichard Henderson     } else {
584129e9cc3SRichard Henderson         /* The insn that we just executed is itself nullifying the next
585129e9cc3SRichard Henderson            instruction.  Store the condition in the PSW[N] global.
586129e9cc3SRichard Henderson            We asserted PSW[N] = 0 in nullify_over, so that after the
587129e9cc3SRichard Henderson            label we have the proper value in place.  */
588129e9cc3SRichard Henderson         nullify_save(ctx);
589129e9cc3SRichard Henderson         gen_set_label(null_lab);
590129e9cc3SRichard Henderson         ctx->null_cond = cond_make_n();
591129e9cc3SRichard Henderson     }
592869051eaSRichard Henderson     if (status == DISAS_NORETURN) {
59331234768SRichard Henderson         ctx->base.is_jmp = DISAS_NEXT;
594129e9cc3SRichard Henderson     }
59531234768SRichard Henderson     return true;
596129e9cc3SRichard Henderson }
597129e9cc3SRichard Henderson 
5986fd0c7bcSRichard Henderson static void copy_iaoq_entry(DisasContext *ctx, TCGv_i64 dest,
5996fd0c7bcSRichard Henderson                             uint64_t ival, TCGv_i64 vval)
60061766fe9SRichard Henderson {
6017d50b696SSven Schnelle     uint64_t mask = gva_offset_mask(ctx->tb_flags);
602f13bf343SRichard Henderson 
603f13bf343SRichard Henderson     if (ival != -1) {
6046fd0c7bcSRichard Henderson         tcg_gen_movi_i64(dest, ival & mask);
605f13bf343SRichard Henderson         return;
606f13bf343SRichard Henderson     }
607f13bf343SRichard Henderson     tcg_debug_assert(vval != NULL);
608f13bf343SRichard Henderson 
609f13bf343SRichard Henderson     /*
610f13bf343SRichard Henderson      * We know that the IAOQ is already properly masked.
611f13bf343SRichard Henderson      * This optimization is primarily for "iaoq_f = iaoq_b".
612f13bf343SRichard Henderson      */
613f13bf343SRichard Henderson     if (vval == cpu_iaoq_f || vval == cpu_iaoq_b) {
6146fd0c7bcSRichard Henderson         tcg_gen_mov_i64(dest, vval);
61561766fe9SRichard Henderson     } else {
6166fd0c7bcSRichard Henderson         tcg_gen_andi_i64(dest, vval, mask);
61761766fe9SRichard Henderson     }
61861766fe9SRichard Henderson }
61961766fe9SRichard Henderson 
62085e6cda0SRichard Henderson static void install_iaq_entries(DisasContext *ctx, uint64_t bi, TCGv_i64 bv,
62185e6cda0SRichard Henderson                                 uint64_t ni, TCGv_i64 nv)
62285e6cda0SRichard Henderson {
62385e6cda0SRichard Henderson     copy_iaoq_entry(ctx, cpu_iaoq_f, bi, bv);
62485e6cda0SRichard Henderson 
62585e6cda0SRichard Henderson     /* Allow ni variable, with nv null, to indicate a trivial advance. */
62685e6cda0SRichard Henderson     if (ni != -1 || nv) {
62785e6cda0SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, ni, nv);
62885e6cda0SRichard Henderson     } else if (bi != -1) {
62985e6cda0SRichard Henderson         copy_iaoq_entry(ctx, cpu_iaoq_b, bi + 4, NULL);
63085e6cda0SRichard Henderson     } else {
63185e6cda0SRichard Henderson         tcg_gen_addi_i64(cpu_iaoq_b, cpu_iaoq_f, 4);
63285e6cda0SRichard Henderson         tcg_gen_andi_i64(cpu_iaoq_b, cpu_iaoq_b,
63385e6cda0SRichard Henderson                          gva_offset_mask(ctx->tb_flags));
63485e6cda0SRichard Henderson     }
63585e6cda0SRichard Henderson }
63685e6cda0SRichard Henderson 
637*43541db0SRichard Henderson static void install_link(DisasContext *ctx, unsigned link, bool with_sr0)
638*43541db0SRichard Henderson {
639*43541db0SRichard Henderson     tcg_debug_assert(ctx->null_cond.c == TCG_COND_NEVER);
640*43541db0SRichard Henderson     if (!link) {
641*43541db0SRichard Henderson         return;
642*43541db0SRichard Henderson     }
643*43541db0SRichard Henderson     if (ctx->iaoq_b == -1) {
644*43541db0SRichard Henderson         tcg_gen_addi_i64(cpu_gr[link], cpu_iaoq_b, 4);
645*43541db0SRichard Henderson     } else {
646*43541db0SRichard Henderson         tcg_gen_movi_i64(cpu_gr[link], ctx->iaoq_b + 4);
647*43541db0SRichard Henderson     }
648*43541db0SRichard Henderson #ifndef CONFIG_USER_ONLY
649*43541db0SRichard Henderson     if (with_sr0) {
650*43541db0SRichard Henderson         tcg_gen_mov_i64(cpu_sr[0], cpu_iasq_b);
651*43541db0SRichard Henderson     }
652*43541db0SRichard Henderson #endif
653*43541db0SRichard Henderson }
654*43541db0SRichard Henderson 
655c53e401eSRichard Henderson static inline uint64_t iaoq_dest(DisasContext *ctx, int64_t disp)
65661766fe9SRichard Henderson {
65761766fe9SRichard Henderson     return ctx->iaoq_f + disp + 8;
65861766fe9SRichard Henderson }
65961766fe9SRichard Henderson 
66061766fe9SRichard Henderson static void gen_excp_1(int exception)
66161766fe9SRichard Henderson {
662ad75a51eSRichard Henderson     gen_helper_excp(tcg_env, tcg_constant_i32(exception));
66361766fe9SRichard Henderson }
66461766fe9SRichard Henderson 
66531234768SRichard Henderson static void gen_excp(DisasContext *ctx, int exception)
66661766fe9SRichard Henderson {
66785e6cda0SRichard Henderson     install_iaq_entries(ctx, ctx->iaoq_f, cpu_iaoq_f, ctx->iaoq_b, cpu_iaoq_b);
668129e9cc3SRichard Henderson     nullify_save(ctx);
66961766fe9SRichard Henderson     gen_excp_1(exception);
67031234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
67161766fe9SRichard Henderson }
67261766fe9SRichard Henderson 
67331234768SRichard Henderson static bool gen_excp_iir(DisasContext *ctx, int exc)
6741a19da0dSRichard Henderson {
67531234768SRichard Henderson     nullify_over(ctx);
6766fd0c7bcSRichard Henderson     tcg_gen_st_i64(tcg_constant_i64(ctx->insn),
677ad75a51eSRichard Henderson                    tcg_env, offsetof(CPUHPPAState, cr[CR_IIR]));
67831234768SRichard Henderson     gen_excp(ctx, exc);
67931234768SRichard Henderson     return nullify_end(ctx);
6801a19da0dSRichard Henderson }
6811a19da0dSRichard Henderson 
68231234768SRichard Henderson static bool gen_illegal(DisasContext *ctx)
68361766fe9SRichard Henderson {
68431234768SRichard Henderson     return gen_excp_iir(ctx, EXCP_ILL);
68561766fe9SRichard Henderson }
68661766fe9SRichard Henderson 
68740f9f908SRichard Henderson #ifdef CONFIG_USER_ONLY
68840f9f908SRichard Henderson #define CHECK_MOST_PRIVILEGED(EXCP) \
68940f9f908SRichard Henderson     return gen_excp_iir(ctx, EXCP)
69040f9f908SRichard Henderson #else
691e1b5a5edSRichard Henderson #define CHECK_MOST_PRIVILEGED(EXCP) \
692e1b5a5edSRichard Henderson     do {                                     \
693e1b5a5edSRichard Henderson         if (ctx->privilege != 0) {           \
69431234768SRichard Henderson             return gen_excp_iir(ctx, EXCP);  \
695e1b5a5edSRichard Henderson         }                                    \
696e1b5a5edSRichard Henderson     } while (0)
69740f9f908SRichard Henderson #endif
698e1b5a5edSRichard Henderson 
6994e31e68bSRichard Henderson static bool use_goto_tb(DisasContext *ctx, uint64_t bofs, uint64_t nofs)
70061766fe9SRichard Henderson {
7014e31e68bSRichard Henderson     return (bofs != -1 && nofs != -1 &&
7024e31e68bSRichard Henderson             translator_use_goto_tb(&ctx->base, bofs));
70361766fe9SRichard Henderson }
70461766fe9SRichard Henderson 
705129e9cc3SRichard Henderson /* If the next insn is to be nullified, and it's on the same page,
706129e9cc3SRichard Henderson    and we're not attempting to set a breakpoint on it, then we can
707129e9cc3SRichard Henderson    totally skip the nullified insn.  This avoids creating and
708129e9cc3SRichard Henderson    executing a TB that merely branches to the next TB.  */
709129e9cc3SRichard Henderson static bool use_nullify_skip(DisasContext *ctx)
710129e9cc3SRichard Henderson {
711f9b11bc2SRichard Henderson     return (!(tb_cflags(ctx->base.tb) & CF_BP_PAGE)
712f9b11bc2SRichard Henderson             && ctx->iaoq_b != -1
713f9b11bc2SRichard Henderson             && is_same_page(&ctx->base, ctx->iaoq_b));
714129e9cc3SRichard Henderson }
715129e9cc3SRichard Henderson 
71661766fe9SRichard Henderson static void gen_goto_tb(DisasContext *ctx, int which,
7174e31e68bSRichard Henderson                         uint64_t b, uint64_t n)
71861766fe9SRichard Henderson {
7194e31e68bSRichard Henderson     if (use_goto_tb(ctx, b, n)) {
72061766fe9SRichard Henderson         tcg_gen_goto_tb(which);
72185e6cda0SRichard Henderson         install_iaq_entries(ctx, b, NULL, n, NULL);
72207ea28b4SRichard Henderson         tcg_gen_exit_tb(ctx->base.tb, which);
72361766fe9SRichard Henderson     } else {
72485e6cda0SRichard Henderson         install_iaq_entries(ctx, b, cpu_iaoq_b, n, ctx->iaoq_n_var);
7257f11636dSEmilio G. Cota         tcg_gen_lookup_and_goto_ptr();
72661766fe9SRichard Henderson     }
72761766fe9SRichard Henderson }
72861766fe9SRichard Henderson 
729b47a4a02SSven Schnelle static bool cond_need_sv(int c)
730b47a4a02SSven Schnelle {
731b47a4a02SSven Schnelle     return c == 2 || c == 3 || c == 6;
732b47a4a02SSven Schnelle }
733b47a4a02SSven Schnelle 
734b47a4a02SSven Schnelle static bool cond_need_cb(int c)
735b47a4a02SSven Schnelle {
736b47a4a02SSven Schnelle     return c == 4 || c == 5;
737b47a4a02SSven Schnelle }
738b47a4a02SSven Schnelle 
739b47a4a02SSven Schnelle /*
740b47a4a02SSven Schnelle  * Compute conditional for arithmetic.  See Page 5-3, Table 5-1, of
741b47a4a02SSven Schnelle  * the Parisc 1.1 Architecture Reference Manual for details.
742b47a4a02SSven Schnelle  */
743b2167459SRichard Henderson 
744a751eb31SRichard Henderson static DisasCond do_cond(DisasContext *ctx, unsigned cf, bool d,
745fe2d066aSRichard Henderson                          TCGv_i64 res, TCGv_i64 uv, TCGv_i64 sv)
746b2167459SRichard Henderson {
747b2167459SRichard Henderson     DisasCond cond;
7486fd0c7bcSRichard Henderson     TCGv_i64 tmp;
749b2167459SRichard Henderson 
750b2167459SRichard Henderson     switch (cf >> 1) {
751b47a4a02SSven Schnelle     case 0: /* Never / TR    (0 / 1) */
752b2167459SRichard Henderson         cond = cond_make_f();
753b2167459SRichard Henderson         break;
754b2167459SRichard Henderson     case 1: /* = / <>        (Z / !Z) */
75582d0c831SRichard Henderson         if (!d) {
756aac0f603SRichard Henderson             tmp = tcg_temp_new_i64();
7576fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, res);
758a751eb31SRichard Henderson             res = tmp;
759a751eb31SRichard Henderson         }
760b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_EQ, res);
761b2167459SRichard Henderson         break;
762b47a4a02SSven Schnelle     case 2: /* < / >=        (N ^ V / !(N ^ V) */
763aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
7646fd0c7bcSRichard Henderson         tcg_gen_xor_i64(tmp, res, sv);
76582d0c831SRichard Henderson         if (!d) {
7666fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(tmp, tmp);
767a751eb31SRichard Henderson         }
768b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_LT, tmp);
769b2167459SRichard Henderson         break;
770b47a4a02SSven Schnelle     case 3: /* <= / >        (N ^ V) | Z / !((N ^ V) | Z) */
771b47a4a02SSven Schnelle         /*
772b47a4a02SSven Schnelle          * Simplify:
773b47a4a02SSven Schnelle          *   (N ^ V) | Z
774b47a4a02SSven Schnelle          *   ((res < 0) ^ (sv < 0)) | !res
775b47a4a02SSven Schnelle          *   ((res ^ sv) < 0) | !res
776b47a4a02SSven Schnelle          *   (~(res ^ sv) >= 0) | !res
777b47a4a02SSven Schnelle          *   !(~(res ^ sv) >> 31) | !res
778b47a4a02SSven Schnelle          *   !(~(res ^ sv) >> 31 & res)
779b47a4a02SSven Schnelle          */
780aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
7816fd0c7bcSRichard Henderson         tcg_gen_eqv_i64(tmp, res, sv);
78282d0c831SRichard Henderson         if (!d) {
7836fd0c7bcSRichard Henderson             tcg_gen_sextract_i64(tmp, tmp, 31, 1);
7846fd0c7bcSRichard Henderson             tcg_gen_and_i64(tmp, tmp, res);
7856fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, tmp);
786a751eb31SRichard Henderson         } else {
7876fd0c7bcSRichard Henderson             tcg_gen_sari_i64(tmp, tmp, 63);
7886fd0c7bcSRichard Henderson             tcg_gen_and_i64(tmp, tmp, res);
789a751eb31SRichard Henderson         }
790b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
791b2167459SRichard Henderson         break;
792fe2d066aSRichard Henderson     case 4: /* NUV / UV      (!UV / UV) */
793fe2d066aSRichard Henderson         cond = cond_make_0(TCG_COND_EQ, uv);
794b2167459SRichard Henderson         break;
795fe2d066aSRichard Henderson     case 5: /* ZNV / VNZ     (!UV | Z / UV & !Z) */
796aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
797fe2d066aSRichard Henderson         tcg_gen_movcond_i64(TCG_COND_EQ, tmp, uv, ctx->zero, ctx->zero, res);
79882d0c831SRichard Henderson         if (!d) {
7996fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, tmp);
800a751eb31SRichard Henderson         }
801b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_EQ, tmp);
802b2167459SRichard Henderson         break;
803b2167459SRichard Henderson     case 6: /* SV / NSV      (V / !V) */
80482d0c831SRichard Henderson         if (!d) {
805aac0f603SRichard Henderson             tmp = tcg_temp_new_i64();
8066fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(tmp, sv);
807a751eb31SRichard Henderson             sv = tmp;
808a751eb31SRichard Henderson         }
809b2167459SRichard Henderson         cond = cond_make_0(TCG_COND_LT, sv);
810b2167459SRichard Henderson         break;
811b2167459SRichard Henderson     case 7: /* OD / EV */
812aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
8136fd0c7bcSRichard Henderson         tcg_gen_andi_i64(tmp, res, 1);
814b47a4a02SSven Schnelle         cond = cond_make_0_tmp(TCG_COND_NE, tmp);
815b2167459SRichard Henderson         break;
816b2167459SRichard Henderson     default:
817b2167459SRichard Henderson         g_assert_not_reached();
818b2167459SRichard Henderson     }
819b2167459SRichard Henderson     if (cf & 1) {
820b2167459SRichard Henderson         cond.c = tcg_invert_cond(cond.c);
821b2167459SRichard Henderson     }
822b2167459SRichard Henderson 
823b2167459SRichard Henderson     return cond;
824b2167459SRichard Henderson }
825b2167459SRichard Henderson 
826b2167459SRichard Henderson /* Similar, but for the special case of subtraction without borrow, we
827b2167459SRichard Henderson    can use the inputs directly.  This can allow other computation to be
828b2167459SRichard Henderson    deleted as unused.  */
829b2167459SRichard Henderson 
8304fe9533aSRichard Henderson static DisasCond do_sub_cond(DisasContext *ctx, unsigned cf, bool d,
8316fd0c7bcSRichard Henderson                              TCGv_i64 res, TCGv_i64 in1,
8326fd0c7bcSRichard Henderson                              TCGv_i64 in2, TCGv_i64 sv)
833b2167459SRichard Henderson {
8344fe9533aSRichard Henderson     TCGCond tc;
8354fe9533aSRichard Henderson     bool ext_uns;
836b2167459SRichard Henderson 
837b2167459SRichard Henderson     switch (cf >> 1) {
838b2167459SRichard Henderson     case 1: /* = / <> */
8394fe9533aSRichard Henderson         tc = TCG_COND_EQ;
8404fe9533aSRichard Henderson         ext_uns = true;
841b2167459SRichard Henderson         break;
842b2167459SRichard Henderson     case 2: /* < / >= */
8434fe9533aSRichard Henderson         tc = TCG_COND_LT;
8444fe9533aSRichard Henderson         ext_uns = false;
845b2167459SRichard Henderson         break;
846b2167459SRichard Henderson     case 3: /* <= / > */
8474fe9533aSRichard Henderson         tc = TCG_COND_LE;
8484fe9533aSRichard Henderson         ext_uns = false;
849b2167459SRichard Henderson         break;
850b2167459SRichard Henderson     case 4: /* << / >>= */
8514fe9533aSRichard Henderson         tc = TCG_COND_LTU;
8524fe9533aSRichard Henderson         ext_uns = true;
853b2167459SRichard Henderson         break;
854b2167459SRichard Henderson     case 5: /* <<= / >> */
8554fe9533aSRichard Henderson         tc = TCG_COND_LEU;
8564fe9533aSRichard Henderson         ext_uns = true;
857b2167459SRichard Henderson         break;
858b2167459SRichard Henderson     default:
859a751eb31SRichard Henderson         return do_cond(ctx, cf, d, res, NULL, sv);
860b2167459SRichard Henderson     }
861b2167459SRichard Henderson 
8624fe9533aSRichard Henderson     if (cf & 1) {
8634fe9533aSRichard Henderson         tc = tcg_invert_cond(tc);
8644fe9533aSRichard Henderson     }
86582d0c831SRichard Henderson     if (!d) {
866aac0f603SRichard Henderson         TCGv_i64 t1 = tcg_temp_new_i64();
867aac0f603SRichard Henderson         TCGv_i64 t2 = tcg_temp_new_i64();
8684fe9533aSRichard Henderson 
8694fe9533aSRichard Henderson         if (ext_uns) {
8706fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(t1, in1);
8716fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(t2, in2);
8724fe9533aSRichard Henderson         } else {
8736fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(t1, in1);
8746fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(t2, in2);
8754fe9533aSRichard Henderson         }
8764fe9533aSRichard Henderson         return cond_make_tmp(tc, t1, t2);
8774fe9533aSRichard Henderson     }
8784fe9533aSRichard Henderson     return cond_make(tc, in1, in2);
879b2167459SRichard Henderson }
880b2167459SRichard Henderson 
881df0232feSRichard Henderson /*
882df0232feSRichard Henderson  * Similar, but for logicals, where the carry and overflow bits are not
883df0232feSRichard Henderson  * computed, and use of them is undefined.
884df0232feSRichard Henderson  *
885df0232feSRichard Henderson  * Undefined or not, hardware does not trap.  It seems reasonable to
886df0232feSRichard Henderson  * assume hardware treats cases c={4,5,6} as if C=0 & V=0, since that's
887df0232feSRichard Henderson  * how cases c={2,3} are treated.
888df0232feSRichard Henderson  */
889b2167459SRichard Henderson 
890b5af8423SRichard Henderson static DisasCond do_log_cond(DisasContext *ctx, unsigned cf, bool d,
8916fd0c7bcSRichard Henderson                              TCGv_i64 res)
892b2167459SRichard Henderson {
893b5af8423SRichard Henderson     TCGCond tc;
894b5af8423SRichard Henderson     bool ext_uns;
895a751eb31SRichard Henderson 
896df0232feSRichard Henderson     switch (cf) {
897df0232feSRichard Henderson     case 0:  /* never */
898df0232feSRichard Henderson     case 9:  /* undef, C */
899df0232feSRichard Henderson     case 11: /* undef, C & !Z */
900df0232feSRichard Henderson     case 12: /* undef, V */
901df0232feSRichard Henderson         return cond_make_f();
902df0232feSRichard Henderson 
903df0232feSRichard Henderson     case 1:  /* true */
904df0232feSRichard Henderson     case 8:  /* undef, !C */
905df0232feSRichard Henderson     case 10: /* undef, !C | Z */
906df0232feSRichard Henderson     case 13: /* undef, !V */
907df0232feSRichard Henderson         return cond_make_t();
908df0232feSRichard Henderson 
909df0232feSRichard Henderson     case 2:  /* == */
910b5af8423SRichard Henderson         tc = TCG_COND_EQ;
911b5af8423SRichard Henderson         ext_uns = true;
912b5af8423SRichard Henderson         break;
913df0232feSRichard Henderson     case 3:  /* <> */
914b5af8423SRichard Henderson         tc = TCG_COND_NE;
915b5af8423SRichard Henderson         ext_uns = true;
916b5af8423SRichard Henderson         break;
917df0232feSRichard Henderson     case 4:  /* < */
918b5af8423SRichard Henderson         tc = TCG_COND_LT;
919b5af8423SRichard Henderson         ext_uns = false;
920b5af8423SRichard Henderson         break;
921df0232feSRichard Henderson     case 5:  /* >= */
922b5af8423SRichard Henderson         tc = TCG_COND_GE;
923b5af8423SRichard Henderson         ext_uns = false;
924b5af8423SRichard Henderson         break;
925df0232feSRichard Henderson     case 6:  /* <= */
926b5af8423SRichard Henderson         tc = TCG_COND_LE;
927b5af8423SRichard Henderson         ext_uns = false;
928b5af8423SRichard Henderson         break;
929df0232feSRichard Henderson     case 7:  /* > */
930b5af8423SRichard Henderson         tc = TCG_COND_GT;
931b5af8423SRichard Henderson         ext_uns = false;
932b5af8423SRichard Henderson         break;
933df0232feSRichard Henderson 
934df0232feSRichard Henderson     case 14: /* OD */
935df0232feSRichard Henderson     case 15: /* EV */
936a751eb31SRichard Henderson         return do_cond(ctx, cf, d, res, NULL, NULL);
937df0232feSRichard Henderson 
938df0232feSRichard Henderson     default:
939df0232feSRichard Henderson         g_assert_not_reached();
940b2167459SRichard Henderson     }
941b5af8423SRichard Henderson 
94282d0c831SRichard Henderson     if (!d) {
943aac0f603SRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
944b5af8423SRichard Henderson 
945b5af8423SRichard Henderson         if (ext_uns) {
9466fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(tmp, res);
947b5af8423SRichard Henderson         } else {
9486fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(tmp, res);
949b5af8423SRichard Henderson         }
950b5af8423SRichard Henderson         return cond_make_0_tmp(tc, tmp);
951b5af8423SRichard Henderson     }
952b5af8423SRichard Henderson     return cond_make_0(tc, res);
953b2167459SRichard Henderson }
954b2167459SRichard Henderson 
95598cd9ca7SRichard Henderson /* Similar, but for shift/extract/deposit conditions.  */
95698cd9ca7SRichard Henderson 
9574fa52edfSRichard Henderson static DisasCond do_sed_cond(DisasContext *ctx, unsigned orig, bool d,
9586fd0c7bcSRichard Henderson                              TCGv_i64 res)
95998cd9ca7SRichard Henderson {
96098cd9ca7SRichard Henderson     unsigned c, f;
96198cd9ca7SRichard Henderson 
96298cd9ca7SRichard Henderson     /* Convert the compressed condition codes to standard.
96398cd9ca7SRichard Henderson        0-2 are the same as logicals (nv,<,<=), while 3 is OD.
96498cd9ca7SRichard Henderson        4-7 are the reverse of 0-3.  */
96598cd9ca7SRichard Henderson     c = orig & 3;
96698cd9ca7SRichard Henderson     if (c == 3) {
96798cd9ca7SRichard Henderson         c = 7;
96898cd9ca7SRichard Henderson     }
96998cd9ca7SRichard Henderson     f = (orig & 4) / 4;
97098cd9ca7SRichard Henderson 
971b5af8423SRichard Henderson     return do_log_cond(ctx, c * 2 + f, d, res);
97298cd9ca7SRichard Henderson }
97398cd9ca7SRichard Henderson 
97446bb3d46SRichard Henderson /* Similar, but for unit zero conditions.  */
97546bb3d46SRichard Henderson static DisasCond do_unit_zero_cond(unsigned cf, bool d, TCGv_i64 res)
976b2167459SRichard Henderson {
97746bb3d46SRichard Henderson     TCGv_i64 tmp;
978c53e401eSRichard Henderson     uint64_t d_repl = d ? 0x0000000100000001ull : 1;
97946bb3d46SRichard Henderson     uint64_t ones = 0, sgns = 0;
980b2167459SRichard Henderson 
981b2167459SRichard Henderson     switch (cf >> 1) {
982578b8132SSven Schnelle     case 1: /* SBW / NBW */
983578b8132SSven Schnelle         if (d) {
98446bb3d46SRichard Henderson             ones = d_repl;
98546bb3d46SRichard Henderson             sgns = d_repl << 31;
986578b8132SSven Schnelle         }
987578b8132SSven Schnelle         break;
988b2167459SRichard Henderson     case 2: /* SBZ / NBZ */
98946bb3d46SRichard Henderson         ones = d_repl * 0x01010101u;
99046bb3d46SRichard Henderson         sgns = ones << 7;
99146bb3d46SRichard Henderson         break;
99246bb3d46SRichard Henderson     case 3: /* SHZ / NHZ */
99346bb3d46SRichard Henderson         ones = d_repl * 0x00010001u;
99446bb3d46SRichard Henderson         sgns = ones << 15;
99546bb3d46SRichard Henderson         break;
99646bb3d46SRichard Henderson     }
99746bb3d46SRichard Henderson     if (ones == 0) {
99846bb3d46SRichard Henderson         /* Undefined, or 0/1 (never/always). */
99946bb3d46SRichard Henderson         return cf & 1 ? cond_make_t() : cond_make_f();
100046bb3d46SRichard Henderson     }
100146bb3d46SRichard Henderson 
100246bb3d46SRichard Henderson     /*
100346bb3d46SRichard Henderson      * See hasless(v,1) from
1004b2167459SRichard Henderson      * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
1005b2167459SRichard Henderson      */
1006aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
100746bb3d46SRichard Henderson     tcg_gen_subi_i64(tmp, res, ones);
10086fd0c7bcSRichard Henderson     tcg_gen_andc_i64(tmp, tmp, res);
100946bb3d46SRichard Henderson     tcg_gen_andi_i64(tmp, tmp, sgns);
1010b2167459SRichard Henderson 
101146bb3d46SRichard Henderson     return cond_make_0_tmp(cf & 1 ? TCG_COND_EQ : TCG_COND_NE, tmp);
1012b2167459SRichard Henderson }
1013b2167459SRichard Henderson 
10146fd0c7bcSRichard Henderson static TCGv_i64 get_carry(DisasContext *ctx, bool d,
10156fd0c7bcSRichard Henderson                           TCGv_i64 cb, TCGv_i64 cb_msb)
101672ca8753SRichard Henderson {
101782d0c831SRichard Henderson     if (!d) {
1018aac0f603SRichard Henderson         TCGv_i64 t = tcg_temp_new_i64();
10196fd0c7bcSRichard Henderson         tcg_gen_extract_i64(t, cb, 32, 1);
102072ca8753SRichard Henderson         return t;
102172ca8753SRichard Henderson     }
102272ca8753SRichard Henderson     return cb_msb;
102372ca8753SRichard Henderson }
102472ca8753SRichard Henderson 
10256fd0c7bcSRichard Henderson static TCGv_i64 get_psw_carry(DisasContext *ctx, bool d)
102672ca8753SRichard Henderson {
102772ca8753SRichard Henderson     return get_carry(ctx, d, cpu_psw_cb, cpu_psw_cb_msb);
102872ca8753SRichard Henderson }
102972ca8753SRichard Henderson 
1030b2167459SRichard Henderson /* Compute signed overflow for addition.  */
10316fd0c7bcSRichard Henderson static TCGv_i64 do_add_sv(DisasContext *ctx, TCGv_i64 res,
1032f8f5986eSRichard Henderson                           TCGv_i64 in1, TCGv_i64 in2,
1033f8f5986eSRichard Henderson                           TCGv_i64 orig_in1, int shift, bool d)
1034b2167459SRichard Henderson {
1035aac0f603SRichard Henderson     TCGv_i64 sv = tcg_temp_new_i64();
1036aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
1037b2167459SRichard Henderson 
10386fd0c7bcSRichard Henderson     tcg_gen_xor_i64(sv, res, in1);
10396fd0c7bcSRichard Henderson     tcg_gen_xor_i64(tmp, in1, in2);
10406fd0c7bcSRichard Henderson     tcg_gen_andc_i64(sv, sv, tmp);
1041b2167459SRichard Henderson 
1042f8f5986eSRichard Henderson     switch (shift) {
1043f8f5986eSRichard Henderson     case 0:
1044f8f5986eSRichard Henderson         break;
1045f8f5986eSRichard Henderson     case 1:
1046f8f5986eSRichard Henderson         /* Shift left by one and compare the sign. */
1047f8f5986eSRichard Henderson         tcg_gen_add_i64(tmp, orig_in1, orig_in1);
1048f8f5986eSRichard Henderson         tcg_gen_xor_i64(tmp, tmp, orig_in1);
1049f8f5986eSRichard Henderson         /* Incorporate into the overflow. */
1050f8f5986eSRichard Henderson         tcg_gen_or_i64(sv, sv, tmp);
1051f8f5986eSRichard Henderson         break;
1052f8f5986eSRichard Henderson     default:
1053f8f5986eSRichard Henderson         {
1054f8f5986eSRichard Henderson             int sign_bit = d ? 63 : 31;
1055f8f5986eSRichard Henderson 
1056f8f5986eSRichard Henderson             /* Compare the sign against all lower bits. */
1057f8f5986eSRichard Henderson             tcg_gen_sextract_i64(tmp, orig_in1, sign_bit, 1);
1058f8f5986eSRichard Henderson             tcg_gen_xor_i64(tmp, tmp, orig_in1);
1059f8f5986eSRichard Henderson             /*
1060f8f5986eSRichard Henderson              * If one of the bits shifting into or through the sign
1061f8f5986eSRichard Henderson              * differs, then we have overflow.
1062f8f5986eSRichard Henderson              */
1063f8f5986eSRichard Henderson             tcg_gen_extract_i64(tmp, tmp, sign_bit - shift, shift);
1064f8f5986eSRichard Henderson             tcg_gen_movcond_i64(TCG_COND_NE, sv, tmp, ctx->zero,
1065f8f5986eSRichard Henderson                                 tcg_constant_i64(-1), sv);
1066f8f5986eSRichard Henderson         }
1067f8f5986eSRichard Henderson     }
1068b2167459SRichard Henderson     return sv;
1069b2167459SRichard Henderson }
1070b2167459SRichard Henderson 
1071f8f5986eSRichard Henderson /* Compute unsigned overflow for addition.  */
1072f8f5986eSRichard Henderson static TCGv_i64 do_add_uv(DisasContext *ctx, TCGv_i64 cb, TCGv_i64 cb_msb,
1073f8f5986eSRichard Henderson                           TCGv_i64 in1, int shift, bool d)
1074f8f5986eSRichard Henderson {
1075f8f5986eSRichard Henderson     if (shift == 0) {
1076f8f5986eSRichard Henderson         return get_carry(ctx, d, cb, cb_msb);
1077f8f5986eSRichard Henderson     } else {
1078f8f5986eSRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
1079f8f5986eSRichard Henderson         tcg_gen_extract_i64(tmp, in1, (d ? 63 : 31) - shift, shift);
1080f8f5986eSRichard Henderson         tcg_gen_or_i64(tmp, tmp, get_carry(ctx, d, cb, cb_msb));
1081f8f5986eSRichard Henderson         return tmp;
1082f8f5986eSRichard Henderson     }
1083f8f5986eSRichard Henderson }
1084f8f5986eSRichard Henderson 
1085b2167459SRichard Henderson /* Compute signed overflow for subtraction.  */
10866fd0c7bcSRichard Henderson static TCGv_i64 do_sub_sv(DisasContext *ctx, TCGv_i64 res,
10876fd0c7bcSRichard Henderson                           TCGv_i64 in1, TCGv_i64 in2)
1088b2167459SRichard Henderson {
1089aac0f603SRichard Henderson     TCGv_i64 sv = tcg_temp_new_i64();
1090aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
1091b2167459SRichard Henderson 
10926fd0c7bcSRichard Henderson     tcg_gen_xor_i64(sv, res, in1);
10936fd0c7bcSRichard Henderson     tcg_gen_xor_i64(tmp, in1, in2);
10946fd0c7bcSRichard Henderson     tcg_gen_and_i64(sv, sv, tmp);
1095b2167459SRichard Henderson 
1096b2167459SRichard Henderson     return sv;
1097b2167459SRichard Henderson }
1098b2167459SRichard Henderson 
1099f8f5986eSRichard Henderson static void do_add(DisasContext *ctx, unsigned rt, TCGv_i64 orig_in1,
11006fd0c7bcSRichard Henderson                    TCGv_i64 in2, unsigned shift, bool is_l,
1101faf97ba1SRichard Henderson                    bool is_tsv, bool is_tc, bool is_c, unsigned cf, bool d)
1102b2167459SRichard Henderson {
1103f8f5986eSRichard Henderson     TCGv_i64 dest, cb, cb_msb, in1, uv, sv, tmp;
1104b2167459SRichard Henderson     unsigned c = cf >> 1;
1105b2167459SRichard Henderson     DisasCond cond;
1106b2167459SRichard Henderson 
1107aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
1108f764718dSRichard Henderson     cb = NULL;
1109f764718dSRichard Henderson     cb_msb = NULL;
1110b2167459SRichard Henderson 
1111f8f5986eSRichard Henderson     in1 = orig_in1;
1112b2167459SRichard Henderson     if (shift) {
1113aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
11146fd0c7bcSRichard Henderson         tcg_gen_shli_i64(tmp, in1, shift);
1115b2167459SRichard Henderson         in1 = tmp;
1116b2167459SRichard Henderson     }
1117b2167459SRichard Henderson 
1118b47a4a02SSven Schnelle     if (!is_l || cond_need_cb(c)) {
1119aac0f603SRichard Henderson         cb_msb = tcg_temp_new_i64();
1120aac0f603SRichard Henderson         cb = tcg_temp_new_i64();
1121bdcccc17SRichard Henderson 
1122a4db4a78SRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, in1, ctx->zero, in2, ctx->zero);
1123b2167459SRichard Henderson         if (is_c) {
11246fd0c7bcSRichard Henderson             tcg_gen_add2_i64(dest, cb_msb, dest, cb_msb,
1125a4db4a78SRichard Henderson                              get_psw_carry(ctx, d), ctx->zero);
1126b2167459SRichard Henderson         }
11276fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, in1, in2);
11286fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
1129b2167459SRichard Henderson     } else {
11306fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, in1, in2);
1131b2167459SRichard Henderson         if (is_c) {
11326fd0c7bcSRichard Henderson             tcg_gen_add_i64(dest, dest, get_psw_carry(ctx, d));
1133b2167459SRichard Henderson         }
1134b2167459SRichard Henderson     }
1135b2167459SRichard Henderson 
1136b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1137f764718dSRichard Henderson     sv = NULL;
1138b47a4a02SSven Schnelle     if (is_tsv || cond_need_sv(c)) {
1139f8f5986eSRichard Henderson         sv = do_add_sv(ctx, dest, in1, in2, orig_in1, shift, d);
1140b2167459SRichard Henderson         if (is_tsv) {
1141bd1ad92cSSven Schnelle             if (!d) {
1142bd1ad92cSSven Schnelle                 tcg_gen_ext32s_i64(sv, sv);
1143bd1ad92cSSven Schnelle             }
1144ad75a51eSRichard Henderson             gen_helper_tsv(tcg_env, sv);
1145b2167459SRichard Henderson         }
1146b2167459SRichard Henderson     }
1147b2167459SRichard Henderson 
1148f8f5986eSRichard Henderson     /* Compute unsigned overflow if required.  */
1149f8f5986eSRichard Henderson     uv = NULL;
1150f8f5986eSRichard Henderson     if (cond_need_cb(c)) {
1151f8f5986eSRichard Henderson         uv = do_add_uv(ctx, cb, cb_msb, orig_in1, shift, d);
1152f8f5986eSRichard Henderson     }
1153f8f5986eSRichard Henderson 
1154b2167459SRichard Henderson     /* Emit any conditional trap before any writeback.  */
1155f8f5986eSRichard Henderson     cond = do_cond(ctx, cf, d, dest, uv, sv);
1156b2167459SRichard Henderson     if (is_tc) {
1157aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
11586fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(cond.c, tmp, cond.a0, cond.a1);
1159ad75a51eSRichard Henderson         gen_helper_tcond(tcg_env, tmp);
1160b2167459SRichard Henderson     }
1161b2167459SRichard Henderson 
1162b2167459SRichard Henderson     /* Write back the result.  */
1163b2167459SRichard Henderson     if (!is_l) {
1164b2167459SRichard Henderson         save_or_nullify(ctx, cpu_psw_cb, cb);
1165b2167459SRichard Henderson         save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1166b2167459SRichard Henderson     }
1167b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1168b2167459SRichard Henderson 
1169b2167459SRichard Henderson     /* Install the new nullification.  */
1170b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1171b2167459SRichard Henderson     ctx->null_cond = cond;
1172b2167459SRichard Henderson }
1173b2167459SRichard Henderson 
1174faf97ba1SRichard Henderson static bool do_add_reg(DisasContext *ctx, arg_rrr_cf_d_sh *a,
11750c982a28SRichard Henderson                        bool is_l, bool is_tsv, bool is_tc, bool is_c)
11760c982a28SRichard Henderson {
11776fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
11780c982a28SRichard Henderson 
11790c982a28SRichard Henderson     if (a->cf) {
11800c982a28SRichard Henderson         nullify_over(ctx);
11810c982a28SRichard Henderson     }
11820c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
11830c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
1184faf97ba1SRichard Henderson     do_add(ctx, a->t, tcg_r1, tcg_r2, a->sh, is_l,
1185faf97ba1SRichard Henderson            is_tsv, is_tc, is_c, a->cf, a->d);
11860c982a28SRichard Henderson     return nullify_end(ctx);
11870c982a28SRichard Henderson }
11880c982a28SRichard Henderson 
11890588e061SRichard Henderson static bool do_add_imm(DisasContext *ctx, arg_rri_cf *a,
11900588e061SRichard Henderson                        bool is_tsv, bool is_tc)
11910588e061SRichard Henderson {
11926fd0c7bcSRichard Henderson     TCGv_i64 tcg_im, tcg_r2;
11930588e061SRichard Henderson 
11940588e061SRichard Henderson     if (a->cf) {
11950588e061SRichard Henderson         nullify_over(ctx);
11960588e061SRichard Henderson     }
11976fd0c7bcSRichard Henderson     tcg_im = tcg_constant_i64(a->i);
11980588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
1199faf97ba1SRichard Henderson     /* All ADDI conditions are 32-bit. */
1200faf97ba1SRichard Henderson     do_add(ctx, a->t, tcg_im, tcg_r2, 0, 0, is_tsv, is_tc, 0, a->cf, false);
12010588e061SRichard Henderson     return nullify_end(ctx);
12020588e061SRichard Henderson }
12030588e061SRichard Henderson 
12046fd0c7bcSRichard Henderson static void do_sub(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
12056fd0c7bcSRichard Henderson                    TCGv_i64 in2, bool is_tsv, bool is_b,
120663c427c6SRichard Henderson                    bool is_tc, unsigned cf, bool d)
1207b2167459SRichard Henderson {
1208a4db4a78SRichard Henderson     TCGv_i64 dest, sv, cb, cb_msb, tmp;
1209b2167459SRichard Henderson     unsigned c = cf >> 1;
1210b2167459SRichard Henderson     DisasCond cond;
1211b2167459SRichard Henderson 
1212aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
1213aac0f603SRichard Henderson     cb = tcg_temp_new_i64();
1214aac0f603SRichard Henderson     cb_msb = tcg_temp_new_i64();
1215b2167459SRichard Henderson 
1216b2167459SRichard Henderson     if (is_b) {
1217b2167459SRichard Henderson         /* DEST,C = IN1 + ~IN2 + C.  */
12186fd0c7bcSRichard Henderson         tcg_gen_not_i64(cb, in2);
1219a4db4a78SRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, in1, ctx->zero,
1220a4db4a78SRichard Henderson                          get_psw_carry(ctx, d), ctx->zero);
1221a4db4a78SRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, dest, cb_msb, cb, ctx->zero);
12226fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, in1);
12236fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
1224b2167459SRichard Henderson     } else {
1225bdcccc17SRichard Henderson         /*
1226bdcccc17SRichard Henderson          * DEST,C = IN1 + ~IN2 + 1.  We can produce the same result in fewer
1227bdcccc17SRichard Henderson          * operations by seeding the high word with 1 and subtracting.
1228bdcccc17SRichard Henderson          */
12296fd0c7bcSRichard Henderson         TCGv_i64 one = tcg_constant_i64(1);
1230a4db4a78SRichard Henderson         tcg_gen_sub2_i64(dest, cb_msb, in1, one, in2, ctx->zero);
12316fd0c7bcSRichard Henderson         tcg_gen_eqv_i64(cb, in1, in2);
12326fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
1233b2167459SRichard Henderson     }
1234b2167459SRichard Henderson 
1235b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1236f764718dSRichard Henderson     sv = NULL;
1237b47a4a02SSven Schnelle     if (is_tsv || cond_need_sv(c)) {
1238b2167459SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
1239b2167459SRichard Henderson         if (is_tsv) {
1240bd1ad92cSSven Schnelle             if (!d) {
1241bd1ad92cSSven Schnelle                 tcg_gen_ext32s_i64(sv, sv);
1242bd1ad92cSSven Schnelle             }
1243ad75a51eSRichard Henderson             gen_helper_tsv(tcg_env, sv);
1244b2167459SRichard Henderson         }
1245b2167459SRichard Henderson     }
1246b2167459SRichard Henderson 
1247b2167459SRichard Henderson     /* Compute the condition.  We cannot use the special case for borrow.  */
1248b2167459SRichard Henderson     if (!is_b) {
12494fe9533aSRichard Henderson         cond = do_sub_cond(ctx, cf, d, dest, in1, in2, sv);
1250b2167459SRichard Henderson     } else {
1251a751eb31SRichard Henderson         cond = do_cond(ctx, cf, d, dest, get_carry(ctx, d, cb, cb_msb), sv);
1252b2167459SRichard Henderson     }
1253b2167459SRichard Henderson 
1254b2167459SRichard Henderson     /* Emit any conditional trap before any writeback.  */
1255b2167459SRichard Henderson     if (is_tc) {
1256aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
12576fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(cond.c, tmp, cond.a0, cond.a1);
1258ad75a51eSRichard Henderson         gen_helper_tcond(tcg_env, tmp);
1259b2167459SRichard Henderson     }
1260b2167459SRichard Henderson 
1261b2167459SRichard Henderson     /* Write back the result.  */
1262b2167459SRichard Henderson     save_or_nullify(ctx, cpu_psw_cb, cb);
1263b2167459SRichard Henderson     save_or_nullify(ctx, cpu_psw_cb_msb, cb_msb);
1264b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1265b2167459SRichard Henderson 
1266b2167459SRichard Henderson     /* Install the new nullification.  */
1267b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1268b2167459SRichard Henderson     ctx->null_cond = cond;
1269b2167459SRichard Henderson }
1270b2167459SRichard Henderson 
127163c427c6SRichard Henderson static bool do_sub_reg(DisasContext *ctx, arg_rrr_cf_d *a,
12720c982a28SRichard Henderson                        bool is_tsv, bool is_b, bool is_tc)
12730c982a28SRichard Henderson {
12746fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
12750c982a28SRichard Henderson 
12760c982a28SRichard Henderson     if (a->cf) {
12770c982a28SRichard Henderson         nullify_over(ctx);
12780c982a28SRichard Henderson     }
12790c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
12800c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
128163c427c6SRichard Henderson     do_sub(ctx, a->t, tcg_r1, tcg_r2, is_tsv, is_b, is_tc, a->cf, a->d);
12820c982a28SRichard Henderson     return nullify_end(ctx);
12830c982a28SRichard Henderson }
12840c982a28SRichard Henderson 
12850588e061SRichard Henderson static bool do_sub_imm(DisasContext *ctx, arg_rri_cf *a, bool is_tsv)
12860588e061SRichard Henderson {
12876fd0c7bcSRichard Henderson     TCGv_i64 tcg_im, tcg_r2;
12880588e061SRichard Henderson 
12890588e061SRichard Henderson     if (a->cf) {
12900588e061SRichard Henderson         nullify_over(ctx);
12910588e061SRichard Henderson     }
12926fd0c7bcSRichard Henderson     tcg_im = tcg_constant_i64(a->i);
12930588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
129463c427c6SRichard Henderson     /* All SUBI conditions are 32-bit. */
129563c427c6SRichard Henderson     do_sub(ctx, a->t, tcg_im, tcg_r2, is_tsv, 0, 0, a->cf, false);
12960588e061SRichard Henderson     return nullify_end(ctx);
12970588e061SRichard Henderson }
12980588e061SRichard Henderson 
12996fd0c7bcSRichard Henderson static void do_cmpclr(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
13006fd0c7bcSRichard Henderson                       TCGv_i64 in2, unsigned cf, bool d)
1301b2167459SRichard Henderson {
13026fd0c7bcSRichard Henderson     TCGv_i64 dest, sv;
1303b2167459SRichard Henderson     DisasCond cond;
1304b2167459SRichard Henderson 
1305aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
13066fd0c7bcSRichard Henderson     tcg_gen_sub_i64(dest, in1, in2);
1307b2167459SRichard Henderson 
1308b2167459SRichard Henderson     /* Compute signed overflow if required.  */
1309f764718dSRichard Henderson     sv = NULL;
1310b47a4a02SSven Schnelle     if (cond_need_sv(cf >> 1)) {
1311b2167459SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
1312b2167459SRichard Henderson     }
1313b2167459SRichard Henderson 
1314b2167459SRichard Henderson     /* Form the condition for the compare.  */
13154fe9533aSRichard Henderson     cond = do_sub_cond(ctx, cf, d, dest, in1, in2, sv);
1316b2167459SRichard Henderson 
1317b2167459SRichard Henderson     /* Clear.  */
13186fd0c7bcSRichard Henderson     tcg_gen_movi_i64(dest, 0);
1319b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1320b2167459SRichard Henderson 
1321b2167459SRichard Henderson     /* Install the new nullification.  */
1322b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1323b2167459SRichard Henderson     ctx->null_cond = cond;
1324b2167459SRichard Henderson }
1325b2167459SRichard Henderson 
13266fd0c7bcSRichard Henderson static void do_log(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
13276fd0c7bcSRichard Henderson                    TCGv_i64 in2, unsigned cf, bool d,
13286fd0c7bcSRichard Henderson                    void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
1329b2167459SRichard Henderson {
13306fd0c7bcSRichard Henderson     TCGv_i64 dest = dest_gpr(ctx, rt);
1331b2167459SRichard Henderson 
1332b2167459SRichard Henderson     /* Perform the operation, and writeback.  */
1333b2167459SRichard Henderson     fn(dest, in1, in2);
1334b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1335b2167459SRichard Henderson 
1336b2167459SRichard Henderson     /* Install the new nullification.  */
1337b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1338b2167459SRichard Henderson     if (cf) {
1339b5af8423SRichard Henderson         ctx->null_cond = do_log_cond(ctx, cf, d, dest);
1340b2167459SRichard Henderson     }
1341b2167459SRichard Henderson }
1342b2167459SRichard Henderson 
1343fa8e3bedSRichard Henderson static bool do_log_reg(DisasContext *ctx, arg_rrr_cf_d *a,
13446fd0c7bcSRichard Henderson                        void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
13450c982a28SRichard Henderson {
13466fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
13470c982a28SRichard Henderson 
13480c982a28SRichard Henderson     if (a->cf) {
13490c982a28SRichard Henderson         nullify_over(ctx);
13500c982a28SRichard Henderson     }
13510c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
13520c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
1353fa8e3bedSRichard Henderson     do_log(ctx, a->t, tcg_r1, tcg_r2, a->cf, a->d, fn);
13540c982a28SRichard Henderson     return nullify_end(ctx);
13550c982a28SRichard Henderson }
13560c982a28SRichard Henderson 
135746bb3d46SRichard Henderson static void do_unit_addsub(DisasContext *ctx, unsigned rt, TCGv_i64 in1,
135846bb3d46SRichard Henderson                            TCGv_i64 in2, unsigned cf, bool d,
135946bb3d46SRichard Henderson                            bool is_tc, bool is_add)
1360b2167459SRichard Henderson {
136146bb3d46SRichard Henderson     TCGv_i64 dest = tcg_temp_new_i64();
136246bb3d46SRichard Henderson     uint64_t test_cb = 0;
1363b2167459SRichard Henderson     DisasCond cond;
1364b2167459SRichard Henderson 
136546bb3d46SRichard Henderson     /* Select which carry-out bits to test. */
136646bb3d46SRichard Henderson     switch (cf >> 1) {
136746bb3d46SRichard Henderson     case 4: /* NDC / SDC -- 4-bit carries */
136846bb3d46SRichard Henderson         test_cb = dup_const(MO_8, 0x88);
136946bb3d46SRichard Henderson         break;
137046bb3d46SRichard Henderson     case 5: /* NWC / SWC -- 32-bit carries */
137146bb3d46SRichard Henderson         if (d) {
137246bb3d46SRichard Henderson             test_cb = dup_const(MO_32, INT32_MIN);
1373b2167459SRichard Henderson         } else {
137446bb3d46SRichard Henderson             cf &= 1; /* undefined -- map to never/always */
137546bb3d46SRichard Henderson         }
137646bb3d46SRichard Henderson         break;
137746bb3d46SRichard Henderson     case 6: /* NBC / SBC -- 8-bit carries */
137846bb3d46SRichard Henderson         test_cb = dup_const(MO_8, INT8_MIN);
137946bb3d46SRichard Henderson         break;
138046bb3d46SRichard Henderson     case 7: /* NHC / SHC -- 16-bit carries */
138146bb3d46SRichard Henderson         test_cb = dup_const(MO_16, INT16_MIN);
138246bb3d46SRichard Henderson         break;
138346bb3d46SRichard Henderson     }
138446bb3d46SRichard Henderson     if (!d) {
138546bb3d46SRichard Henderson         test_cb = (uint32_t)test_cb;
138646bb3d46SRichard Henderson     }
1387b2167459SRichard Henderson 
138846bb3d46SRichard Henderson     if (!test_cb) {
138946bb3d46SRichard Henderson         /* No need to compute carries if we don't need to test them. */
139046bb3d46SRichard Henderson         if (is_add) {
139146bb3d46SRichard Henderson             tcg_gen_add_i64(dest, in1, in2);
139246bb3d46SRichard Henderson         } else {
139346bb3d46SRichard Henderson             tcg_gen_sub_i64(dest, in1, in2);
139446bb3d46SRichard Henderson         }
139546bb3d46SRichard Henderson         cond = do_unit_zero_cond(cf, d, dest);
139646bb3d46SRichard Henderson     } else {
139746bb3d46SRichard Henderson         TCGv_i64 cb = tcg_temp_new_i64();
139846bb3d46SRichard Henderson 
139946bb3d46SRichard Henderson         if (d) {
140046bb3d46SRichard Henderson             TCGv_i64 cb_msb = tcg_temp_new_i64();
140146bb3d46SRichard Henderson             if (is_add) {
140246bb3d46SRichard Henderson                 tcg_gen_add2_i64(dest, cb_msb, in1, ctx->zero, in2, ctx->zero);
140346bb3d46SRichard Henderson                 tcg_gen_xor_i64(cb, in1, in2);
140446bb3d46SRichard Henderson             } else {
140546bb3d46SRichard Henderson                 /* See do_sub, !is_b. */
140646bb3d46SRichard Henderson                 TCGv_i64 one = tcg_constant_i64(1);
140746bb3d46SRichard Henderson                 tcg_gen_sub2_i64(dest, cb_msb, in1, one, in2, ctx->zero);
140846bb3d46SRichard Henderson                 tcg_gen_eqv_i64(cb, in1, in2);
140946bb3d46SRichard Henderson             }
141046bb3d46SRichard Henderson             tcg_gen_xor_i64(cb, cb, dest);
141146bb3d46SRichard Henderson             tcg_gen_extract2_i64(cb, cb, cb_msb, 1);
141246bb3d46SRichard Henderson         } else {
141346bb3d46SRichard Henderson             if (is_add) {
141446bb3d46SRichard Henderson                 tcg_gen_add_i64(dest, in1, in2);
141546bb3d46SRichard Henderson                 tcg_gen_xor_i64(cb, in1, in2);
141646bb3d46SRichard Henderson             } else {
141746bb3d46SRichard Henderson                 tcg_gen_sub_i64(dest, in1, in2);
141846bb3d46SRichard Henderson                 tcg_gen_eqv_i64(cb, in1, in2);
141946bb3d46SRichard Henderson             }
142046bb3d46SRichard Henderson             tcg_gen_xor_i64(cb, cb, dest);
142146bb3d46SRichard Henderson             tcg_gen_shri_i64(cb, cb, 1);
142246bb3d46SRichard Henderson         }
142346bb3d46SRichard Henderson 
142446bb3d46SRichard Henderson         tcg_gen_andi_i64(cb, cb, test_cb);
142546bb3d46SRichard Henderson         cond = cond_make_0_tmp(cf & 1 ? TCG_COND_EQ : TCG_COND_NE, cb);
142646bb3d46SRichard Henderson     }
1427b2167459SRichard Henderson 
1428b2167459SRichard Henderson     if (is_tc) {
1429aac0f603SRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
14306fd0c7bcSRichard Henderson         tcg_gen_setcond_i64(cond.c, tmp, cond.a0, cond.a1);
1431ad75a51eSRichard Henderson         gen_helper_tcond(tcg_env, tmp);
1432b2167459SRichard Henderson     }
1433b2167459SRichard Henderson     save_gpr(ctx, rt, dest);
1434b2167459SRichard Henderson 
1435b2167459SRichard Henderson     cond_free(&ctx->null_cond);
1436b2167459SRichard Henderson     ctx->null_cond = cond;
1437b2167459SRichard Henderson }
1438b2167459SRichard Henderson 
143986f8d05fSRichard Henderson #ifndef CONFIG_USER_ONLY
14408d6ae7fbSRichard Henderson /* The "normal" usage is SP >= 0, wherein SP == 0 selects the space
14418d6ae7fbSRichard Henderson    from the top 2 bits of the base register.  There are a few system
14428d6ae7fbSRichard Henderson    instructions that have a 3-bit space specifier, for which SR0 is
14438d6ae7fbSRichard Henderson    not special.  To handle this, pass ~SP.  */
14446fd0c7bcSRichard Henderson static TCGv_i64 space_select(DisasContext *ctx, int sp, TCGv_i64 base)
144586f8d05fSRichard Henderson {
144686f8d05fSRichard Henderson     TCGv_ptr ptr;
14476fd0c7bcSRichard Henderson     TCGv_i64 tmp;
144886f8d05fSRichard Henderson     TCGv_i64 spc;
144986f8d05fSRichard Henderson 
145086f8d05fSRichard Henderson     if (sp != 0) {
14518d6ae7fbSRichard Henderson         if (sp < 0) {
14528d6ae7fbSRichard Henderson             sp = ~sp;
14538d6ae7fbSRichard Henderson         }
14546fd0c7bcSRichard Henderson         spc = tcg_temp_new_i64();
14558d6ae7fbSRichard Henderson         load_spr(ctx, spc, sp);
14568d6ae7fbSRichard Henderson         return spc;
145786f8d05fSRichard Henderson     }
1458494737b7SRichard Henderson     if (ctx->tb_flags & TB_FLAG_SR_SAME) {
1459494737b7SRichard Henderson         return cpu_srH;
1460494737b7SRichard Henderson     }
146186f8d05fSRichard Henderson 
146286f8d05fSRichard Henderson     ptr = tcg_temp_new_ptr();
1463aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
14646fd0c7bcSRichard Henderson     spc = tcg_temp_new_i64();
146586f8d05fSRichard Henderson 
1466698240d1SRichard Henderson     /* Extract top 2 bits of the address, shift left 3 for uint64_t index. */
14676fd0c7bcSRichard Henderson     tcg_gen_shri_i64(tmp, base, (ctx->tb_flags & PSW_W ? 64 : 32) - 5);
14686fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, 030);
14696fd0c7bcSRichard Henderson     tcg_gen_trunc_i64_ptr(ptr, tmp);
147086f8d05fSRichard Henderson 
1471ad75a51eSRichard Henderson     tcg_gen_add_ptr(ptr, ptr, tcg_env);
147286f8d05fSRichard Henderson     tcg_gen_ld_i64(spc, ptr, offsetof(CPUHPPAState, sr[4]));
147386f8d05fSRichard Henderson 
147486f8d05fSRichard Henderson     return spc;
147586f8d05fSRichard Henderson }
147686f8d05fSRichard Henderson #endif
147786f8d05fSRichard Henderson 
14786fd0c7bcSRichard Henderson static void form_gva(DisasContext *ctx, TCGv_i64 *pgva, TCGv_i64 *pofs,
1479c53e401eSRichard Henderson                      unsigned rb, unsigned rx, int scale, int64_t disp,
148086f8d05fSRichard Henderson                      unsigned sp, int modify, bool is_phys)
148186f8d05fSRichard Henderson {
14826fd0c7bcSRichard Henderson     TCGv_i64 base = load_gpr(ctx, rb);
14836fd0c7bcSRichard Henderson     TCGv_i64 ofs;
14846fd0c7bcSRichard Henderson     TCGv_i64 addr;
148586f8d05fSRichard Henderson 
1486f5b5c857SRichard Henderson     set_insn_breg(ctx, rb);
1487f5b5c857SRichard Henderson 
148886f8d05fSRichard Henderson     /* Note that RX is mutually exclusive with DISP.  */
148986f8d05fSRichard Henderson     if (rx) {
1490aac0f603SRichard Henderson         ofs = tcg_temp_new_i64();
14916fd0c7bcSRichard Henderson         tcg_gen_shli_i64(ofs, cpu_gr[rx], scale);
14926fd0c7bcSRichard Henderson         tcg_gen_add_i64(ofs, ofs, base);
149386f8d05fSRichard Henderson     } else if (disp || modify) {
1494aac0f603SRichard Henderson         ofs = tcg_temp_new_i64();
14956fd0c7bcSRichard Henderson         tcg_gen_addi_i64(ofs, base, disp);
149686f8d05fSRichard Henderson     } else {
149786f8d05fSRichard Henderson         ofs = base;
149886f8d05fSRichard Henderson     }
149986f8d05fSRichard Henderson 
150086f8d05fSRichard Henderson     *pofs = ofs;
15016fd0c7bcSRichard Henderson     *pgva = addr = tcg_temp_new_i64();
15027d50b696SSven Schnelle     tcg_gen_andi_i64(addr, modify <= 0 ? ofs : base,
15037d50b696SSven Schnelle                      gva_offset_mask(ctx->tb_flags));
1504698240d1SRichard Henderson #ifndef CONFIG_USER_ONLY
150586f8d05fSRichard Henderson     if (!is_phys) {
1506d265360fSRichard Henderson         tcg_gen_or_i64(addr, addr, space_select(ctx, sp, base));
150786f8d05fSRichard Henderson     }
150886f8d05fSRichard Henderson #endif
150986f8d05fSRichard Henderson }
151086f8d05fSRichard Henderson 
151196d6407fSRichard Henderson /* Emit a memory load.  The modify parameter should be
151296d6407fSRichard Henderson  * < 0 for pre-modify,
151396d6407fSRichard Henderson  * > 0 for post-modify,
151496d6407fSRichard Henderson  * = 0 for no base register update.
151596d6407fSRichard Henderson  */
151696d6407fSRichard Henderson static void do_load_32(DisasContext *ctx, TCGv_i32 dest, unsigned rb,
1517c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
151814776ab5STony Nguyen                        unsigned sp, int modify, MemOp mop)
151996d6407fSRichard Henderson {
15206fd0c7bcSRichard Henderson     TCGv_i64 ofs;
15216fd0c7bcSRichard Henderson     TCGv_i64 addr;
152296d6407fSRichard Henderson 
152396d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
152496d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
152596d6407fSRichard Henderson 
152686f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
152717fe594cSRichard Henderson              MMU_DISABLED(ctx));
1528c1f55d97SRichard Henderson     tcg_gen_qemu_ld_i32(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
152986f8d05fSRichard Henderson     if (modify) {
153086f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
153196d6407fSRichard Henderson     }
153296d6407fSRichard Henderson }
153396d6407fSRichard Henderson 
153496d6407fSRichard Henderson static void do_load_64(DisasContext *ctx, TCGv_i64 dest, unsigned rb,
1535c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
153614776ab5STony Nguyen                        unsigned sp, int modify, MemOp mop)
153796d6407fSRichard Henderson {
15386fd0c7bcSRichard Henderson     TCGv_i64 ofs;
15396fd0c7bcSRichard Henderson     TCGv_i64 addr;
154096d6407fSRichard Henderson 
154196d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
154296d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
154396d6407fSRichard Henderson 
154486f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
154517fe594cSRichard Henderson              MMU_DISABLED(ctx));
1546217d1a5eSRichard Henderson     tcg_gen_qemu_ld_i64(dest, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
154786f8d05fSRichard Henderson     if (modify) {
154886f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
154996d6407fSRichard Henderson     }
155096d6407fSRichard Henderson }
155196d6407fSRichard Henderson 
155296d6407fSRichard Henderson static void do_store_32(DisasContext *ctx, TCGv_i32 src, unsigned rb,
1553c53e401eSRichard Henderson                         unsigned rx, int scale, int64_t disp,
155414776ab5STony Nguyen                         unsigned sp, int modify, MemOp mop)
155596d6407fSRichard Henderson {
15566fd0c7bcSRichard Henderson     TCGv_i64 ofs;
15576fd0c7bcSRichard Henderson     TCGv_i64 addr;
155896d6407fSRichard Henderson 
155996d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
156096d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
156196d6407fSRichard Henderson 
156286f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
156317fe594cSRichard Henderson              MMU_DISABLED(ctx));
1564217d1a5eSRichard Henderson     tcg_gen_qemu_st_i32(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
156586f8d05fSRichard Henderson     if (modify) {
156686f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
156796d6407fSRichard Henderson     }
156896d6407fSRichard Henderson }
156996d6407fSRichard Henderson 
157096d6407fSRichard Henderson static void do_store_64(DisasContext *ctx, TCGv_i64 src, unsigned rb,
1571c53e401eSRichard Henderson                         unsigned rx, int scale, int64_t disp,
157214776ab5STony Nguyen                         unsigned sp, int modify, MemOp mop)
157396d6407fSRichard Henderson {
15746fd0c7bcSRichard Henderson     TCGv_i64 ofs;
15756fd0c7bcSRichard Henderson     TCGv_i64 addr;
157696d6407fSRichard Henderson 
157796d6407fSRichard Henderson     /* Caller uses nullify_over/nullify_end.  */
157896d6407fSRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
157996d6407fSRichard Henderson 
158086f8d05fSRichard Henderson     form_gva(ctx, &addr, &ofs, rb, rx, scale, disp, sp, modify,
158117fe594cSRichard Henderson              MMU_DISABLED(ctx));
1582217d1a5eSRichard Henderson     tcg_gen_qemu_st_i64(src, addr, ctx->mmu_idx, mop | UNALIGN(ctx));
158386f8d05fSRichard Henderson     if (modify) {
158486f8d05fSRichard Henderson         save_gpr(ctx, rb, ofs);
158596d6407fSRichard Henderson     }
158696d6407fSRichard Henderson }
158796d6407fSRichard Henderson 
15881cd012a5SRichard Henderson static bool do_load(DisasContext *ctx, unsigned rt, unsigned rb,
1589c53e401eSRichard Henderson                     unsigned rx, int scale, int64_t disp,
159014776ab5STony Nguyen                     unsigned sp, int modify, MemOp mop)
159196d6407fSRichard Henderson {
15926fd0c7bcSRichard Henderson     TCGv_i64 dest;
159396d6407fSRichard Henderson 
159496d6407fSRichard Henderson     nullify_over(ctx);
159596d6407fSRichard Henderson 
159696d6407fSRichard Henderson     if (modify == 0) {
159796d6407fSRichard Henderson         /* No base register update.  */
159896d6407fSRichard Henderson         dest = dest_gpr(ctx, rt);
159996d6407fSRichard Henderson     } else {
160096d6407fSRichard Henderson         /* Make sure if RT == RB, we see the result of the load.  */
1601aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
160296d6407fSRichard Henderson     }
16036fd0c7bcSRichard Henderson     do_load_64(ctx, dest, rb, rx, scale, disp, sp, modify, mop);
160496d6407fSRichard Henderson     save_gpr(ctx, rt, dest);
160596d6407fSRichard Henderson 
16061cd012a5SRichard Henderson     return nullify_end(ctx);
160796d6407fSRichard Henderson }
160896d6407fSRichard Henderson 
1609740038d7SRichard Henderson static bool do_floadw(DisasContext *ctx, unsigned rt, unsigned rb,
1610c53e401eSRichard Henderson                       unsigned rx, int scale, int64_t disp,
161186f8d05fSRichard Henderson                       unsigned sp, int modify)
161296d6407fSRichard Henderson {
161396d6407fSRichard Henderson     TCGv_i32 tmp;
161496d6407fSRichard Henderson 
161596d6407fSRichard Henderson     nullify_over(ctx);
161696d6407fSRichard Henderson 
161796d6407fSRichard Henderson     tmp = tcg_temp_new_i32();
161886f8d05fSRichard Henderson     do_load_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
161996d6407fSRichard Henderson     save_frw_i32(rt, tmp);
162096d6407fSRichard Henderson 
162196d6407fSRichard Henderson     if (rt == 0) {
1622ad75a51eSRichard Henderson         gen_helper_loaded_fr0(tcg_env);
162396d6407fSRichard Henderson     }
162496d6407fSRichard Henderson 
1625740038d7SRichard Henderson     return nullify_end(ctx);
162696d6407fSRichard Henderson }
162796d6407fSRichard Henderson 
1628740038d7SRichard Henderson static bool trans_fldw(DisasContext *ctx, arg_ldst *a)
1629740038d7SRichard Henderson {
1630740038d7SRichard Henderson     return do_floadw(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1631740038d7SRichard Henderson                      a->disp, a->sp, a->m);
1632740038d7SRichard Henderson }
1633740038d7SRichard Henderson 
1634740038d7SRichard Henderson static bool do_floadd(DisasContext *ctx, unsigned rt, unsigned rb,
1635c53e401eSRichard Henderson                       unsigned rx, int scale, int64_t disp,
163686f8d05fSRichard Henderson                       unsigned sp, int modify)
163796d6407fSRichard Henderson {
163896d6407fSRichard Henderson     TCGv_i64 tmp;
163996d6407fSRichard Henderson 
164096d6407fSRichard Henderson     nullify_over(ctx);
164196d6407fSRichard Henderson 
164296d6407fSRichard Henderson     tmp = tcg_temp_new_i64();
1643fc313c64SFrédéric Pétrot     do_load_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
164496d6407fSRichard Henderson     save_frd(rt, tmp);
164596d6407fSRichard Henderson 
164696d6407fSRichard Henderson     if (rt == 0) {
1647ad75a51eSRichard Henderson         gen_helper_loaded_fr0(tcg_env);
164896d6407fSRichard Henderson     }
164996d6407fSRichard Henderson 
1650740038d7SRichard Henderson     return nullify_end(ctx);
1651740038d7SRichard Henderson }
1652740038d7SRichard Henderson 
1653740038d7SRichard Henderson static bool trans_fldd(DisasContext *ctx, arg_ldst *a)
1654740038d7SRichard Henderson {
1655740038d7SRichard Henderson     return do_floadd(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1656740038d7SRichard Henderson                      a->disp, a->sp, a->m);
165796d6407fSRichard Henderson }
165896d6407fSRichard Henderson 
16591cd012a5SRichard Henderson static bool do_store(DisasContext *ctx, unsigned rt, unsigned rb,
1660c53e401eSRichard Henderson                      int64_t disp, unsigned sp,
166114776ab5STony Nguyen                      int modify, MemOp mop)
166296d6407fSRichard Henderson {
166396d6407fSRichard Henderson     nullify_over(ctx);
16646fd0c7bcSRichard Henderson     do_store_64(ctx, load_gpr(ctx, rt), rb, 0, 0, disp, sp, modify, mop);
16651cd012a5SRichard Henderson     return nullify_end(ctx);
166696d6407fSRichard Henderson }
166796d6407fSRichard Henderson 
1668740038d7SRichard Henderson static bool do_fstorew(DisasContext *ctx, unsigned rt, unsigned rb,
1669c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
167086f8d05fSRichard Henderson                        unsigned sp, int modify)
167196d6407fSRichard Henderson {
167296d6407fSRichard Henderson     TCGv_i32 tmp;
167396d6407fSRichard Henderson 
167496d6407fSRichard Henderson     nullify_over(ctx);
167596d6407fSRichard Henderson 
167696d6407fSRichard Henderson     tmp = load_frw_i32(rt);
167786f8d05fSRichard Henderson     do_store_32(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUL);
167896d6407fSRichard Henderson 
1679740038d7SRichard Henderson     return nullify_end(ctx);
168096d6407fSRichard Henderson }
168196d6407fSRichard Henderson 
1682740038d7SRichard Henderson static bool trans_fstw(DisasContext *ctx, arg_ldst *a)
1683740038d7SRichard Henderson {
1684740038d7SRichard Henderson     return do_fstorew(ctx, a->t, a->b, a->x, a->scale ? 2 : 0,
1685740038d7SRichard Henderson                       a->disp, a->sp, a->m);
1686740038d7SRichard Henderson }
1687740038d7SRichard Henderson 
1688740038d7SRichard Henderson static bool do_fstored(DisasContext *ctx, unsigned rt, unsigned rb,
1689c53e401eSRichard Henderson                        unsigned rx, int scale, int64_t disp,
169086f8d05fSRichard Henderson                        unsigned sp, int modify)
169196d6407fSRichard Henderson {
169296d6407fSRichard Henderson     TCGv_i64 tmp;
169396d6407fSRichard Henderson 
169496d6407fSRichard Henderson     nullify_over(ctx);
169596d6407fSRichard Henderson 
169696d6407fSRichard Henderson     tmp = load_frd(rt);
1697fc313c64SFrédéric Pétrot     do_store_64(ctx, tmp, rb, rx, scale, disp, sp, modify, MO_TEUQ);
169896d6407fSRichard Henderson 
1699740038d7SRichard Henderson     return nullify_end(ctx);
1700740038d7SRichard Henderson }
1701740038d7SRichard Henderson 
1702740038d7SRichard Henderson static bool trans_fstd(DisasContext *ctx, arg_ldst *a)
1703740038d7SRichard Henderson {
1704740038d7SRichard Henderson     return do_fstored(ctx, a->t, a->b, a->x, a->scale ? 3 : 0,
1705740038d7SRichard Henderson                       a->disp, a->sp, a->m);
170696d6407fSRichard Henderson }
170796d6407fSRichard Henderson 
17081ca74648SRichard Henderson static bool do_fop_wew(DisasContext *ctx, unsigned rt, unsigned ra,
1709ebe9383cSRichard Henderson                        void (*func)(TCGv_i32, TCGv_env, TCGv_i32))
1710ebe9383cSRichard Henderson {
1711ebe9383cSRichard Henderson     TCGv_i32 tmp;
1712ebe9383cSRichard Henderson 
1713ebe9383cSRichard Henderson     nullify_over(ctx);
1714ebe9383cSRichard Henderson     tmp = load_frw0_i32(ra);
1715ebe9383cSRichard Henderson 
1716ad75a51eSRichard Henderson     func(tmp, tcg_env, tmp);
1717ebe9383cSRichard Henderson 
1718ebe9383cSRichard Henderson     save_frw_i32(rt, tmp);
17191ca74648SRichard Henderson     return nullify_end(ctx);
1720ebe9383cSRichard Henderson }
1721ebe9383cSRichard Henderson 
17221ca74648SRichard Henderson static bool do_fop_wed(DisasContext *ctx, unsigned rt, unsigned ra,
1723ebe9383cSRichard Henderson                        void (*func)(TCGv_i32, TCGv_env, TCGv_i64))
1724ebe9383cSRichard Henderson {
1725ebe9383cSRichard Henderson     TCGv_i32 dst;
1726ebe9383cSRichard Henderson     TCGv_i64 src;
1727ebe9383cSRichard Henderson 
1728ebe9383cSRichard Henderson     nullify_over(ctx);
1729ebe9383cSRichard Henderson     src = load_frd(ra);
1730ebe9383cSRichard Henderson     dst = tcg_temp_new_i32();
1731ebe9383cSRichard Henderson 
1732ad75a51eSRichard Henderson     func(dst, tcg_env, src);
1733ebe9383cSRichard Henderson 
1734ebe9383cSRichard Henderson     save_frw_i32(rt, dst);
17351ca74648SRichard Henderson     return nullify_end(ctx);
1736ebe9383cSRichard Henderson }
1737ebe9383cSRichard Henderson 
17381ca74648SRichard Henderson static bool do_fop_ded(DisasContext *ctx, unsigned rt, unsigned ra,
1739ebe9383cSRichard Henderson                        void (*func)(TCGv_i64, TCGv_env, TCGv_i64))
1740ebe9383cSRichard Henderson {
1741ebe9383cSRichard Henderson     TCGv_i64 tmp;
1742ebe9383cSRichard Henderson 
1743ebe9383cSRichard Henderson     nullify_over(ctx);
1744ebe9383cSRichard Henderson     tmp = load_frd0(ra);
1745ebe9383cSRichard Henderson 
1746ad75a51eSRichard Henderson     func(tmp, tcg_env, tmp);
1747ebe9383cSRichard Henderson 
1748ebe9383cSRichard Henderson     save_frd(rt, tmp);
17491ca74648SRichard Henderson     return nullify_end(ctx);
1750ebe9383cSRichard Henderson }
1751ebe9383cSRichard Henderson 
17521ca74648SRichard Henderson static bool do_fop_dew(DisasContext *ctx, unsigned rt, unsigned ra,
1753ebe9383cSRichard Henderson                        void (*func)(TCGv_i64, TCGv_env, TCGv_i32))
1754ebe9383cSRichard Henderson {
1755ebe9383cSRichard Henderson     TCGv_i32 src;
1756ebe9383cSRichard Henderson     TCGv_i64 dst;
1757ebe9383cSRichard Henderson 
1758ebe9383cSRichard Henderson     nullify_over(ctx);
1759ebe9383cSRichard Henderson     src = load_frw0_i32(ra);
1760ebe9383cSRichard Henderson     dst = tcg_temp_new_i64();
1761ebe9383cSRichard Henderson 
1762ad75a51eSRichard Henderson     func(dst, tcg_env, src);
1763ebe9383cSRichard Henderson 
1764ebe9383cSRichard Henderson     save_frd(rt, dst);
17651ca74648SRichard Henderson     return nullify_end(ctx);
1766ebe9383cSRichard Henderson }
1767ebe9383cSRichard Henderson 
17681ca74648SRichard Henderson static bool do_fop_weww(DisasContext *ctx, unsigned rt,
1769ebe9383cSRichard Henderson                         unsigned ra, unsigned rb,
177031234768SRichard Henderson                         void (*func)(TCGv_i32, TCGv_env, TCGv_i32, TCGv_i32))
1771ebe9383cSRichard Henderson {
1772ebe9383cSRichard Henderson     TCGv_i32 a, b;
1773ebe9383cSRichard Henderson 
1774ebe9383cSRichard Henderson     nullify_over(ctx);
1775ebe9383cSRichard Henderson     a = load_frw0_i32(ra);
1776ebe9383cSRichard Henderson     b = load_frw0_i32(rb);
1777ebe9383cSRichard Henderson 
1778ad75a51eSRichard Henderson     func(a, tcg_env, a, b);
1779ebe9383cSRichard Henderson 
1780ebe9383cSRichard Henderson     save_frw_i32(rt, a);
17811ca74648SRichard Henderson     return nullify_end(ctx);
1782ebe9383cSRichard Henderson }
1783ebe9383cSRichard Henderson 
17841ca74648SRichard Henderson static bool do_fop_dedd(DisasContext *ctx, unsigned rt,
1785ebe9383cSRichard Henderson                         unsigned ra, unsigned rb,
178631234768SRichard Henderson                         void (*func)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1787ebe9383cSRichard Henderson {
1788ebe9383cSRichard Henderson     TCGv_i64 a, b;
1789ebe9383cSRichard Henderson 
1790ebe9383cSRichard Henderson     nullify_over(ctx);
1791ebe9383cSRichard Henderson     a = load_frd0(ra);
1792ebe9383cSRichard Henderson     b = load_frd0(rb);
1793ebe9383cSRichard Henderson 
1794ad75a51eSRichard Henderson     func(a, tcg_env, a, b);
1795ebe9383cSRichard Henderson 
1796ebe9383cSRichard Henderson     save_frd(rt, a);
17971ca74648SRichard Henderson     return nullify_end(ctx);
1798ebe9383cSRichard Henderson }
1799ebe9383cSRichard Henderson 
180098cd9ca7SRichard Henderson /* Emit an unconditional branch to a direct target, which may or may not
180198cd9ca7SRichard Henderson    have already had nullification handled.  */
18022644f80bSRichard Henderson static bool do_dbranch(DisasContext *ctx, int64_t disp,
180398cd9ca7SRichard Henderson                        unsigned link, bool is_n)
180498cd9ca7SRichard Henderson {
18052644f80bSRichard Henderson     uint64_t dest = iaoq_dest(ctx, disp);
18062644f80bSRichard Henderson 
180798cd9ca7SRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
1808*43541db0SRichard Henderson         install_link(ctx, link, false);
180998cd9ca7SRichard Henderson         ctx->iaoq_n = dest;
181098cd9ca7SRichard Henderson         if (is_n) {
181198cd9ca7SRichard Henderson             ctx->null_cond.c = TCG_COND_ALWAYS;
181298cd9ca7SRichard Henderson         }
181398cd9ca7SRichard Henderson     } else {
181498cd9ca7SRichard Henderson         nullify_over(ctx);
181598cd9ca7SRichard Henderson 
1816*43541db0SRichard Henderson         install_link(ctx, link, false);
181798cd9ca7SRichard Henderson         if (is_n && use_nullify_skip(ctx)) {
181898cd9ca7SRichard Henderson             nullify_set(ctx, 0);
181998cd9ca7SRichard Henderson             gen_goto_tb(ctx, 0, dest, dest + 4);
182098cd9ca7SRichard Henderson         } else {
182198cd9ca7SRichard Henderson             nullify_set(ctx, is_n);
182298cd9ca7SRichard Henderson             gen_goto_tb(ctx, 0, ctx->iaoq_b, dest);
182398cd9ca7SRichard Henderson         }
182498cd9ca7SRichard Henderson 
182531234768SRichard Henderson         nullify_end(ctx);
182698cd9ca7SRichard Henderson 
182798cd9ca7SRichard Henderson         nullify_set(ctx, 0);
182898cd9ca7SRichard Henderson         gen_goto_tb(ctx, 1, ctx->iaoq_b, ctx->iaoq_n);
182931234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
183098cd9ca7SRichard Henderson     }
183101afb7beSRichard Henderson     return true;
183298cd9ca7SRichard Henderson }
183398cd9ca7SRichard Henderson 
183498cd9ca7SRichard Henderson /* Emit a conditional branch to a direct target.  If the branch itself
183598cd9ca7SRichard Henderson    is nullified, we should have already used nullify_over.  */
1836c53e401eSRichard Henderson static bool do_cbranch(DisasContext *ctx, int64_t disp, bool is_n,
183798cd9ca7SRichard Henderson                        DisasCond *cond)
183898cd9ca7SRichard Henderson {
1839c53e401eSRichard Henderson     uint64_t dest = iaoq_dest(ctx, disp);
184098cd9ca7SRichard Henderson     TCGLabel *taken = NULL;
184198cd9ca7SRichard Henderson     TCGCond c = cond->c;
184298cd9ca7SRichard Henderson     bool n;
184398cd9ca7SRichard Henderson 
184498cd9ca7SRichard Henderson     assert(ctx->null_cond.c == TCG_COND_NEVER);
184598cd9ca7SRichard Henderson 
184698cd9ca7SRichard Henderson     /* Handle TRUE and NEVER as direct branches.  */
184798cd9ca7SRichard Henderson     if (c == TCG_COND_ALWAYS) {
18482644f80bSRichard Henderson         return do_dbranch(ctx, disp, 0, is_n && disp >= 0);
184998cd9ca7SRichard Henderson     }
185098cd9ca7SRichard Henderson 
185198cd9ca7SRichard Henderson     taken = gen_new_label();
18526fd0c7bcSRichard Henderson     tcg_gen_brcond_i64(c, cond->a0, cond->a1, taken);
185398cd9ca7SRichard Henderson     cond_free(cond);
185498cd9ca7SRichard Henderson 
185598cd9ca7SRichard Henderson     /* Not taken: Condition not satisfied; nullify on backward branches. */
185698cd9ca7SRichard Henderson     n = is_n && disp < 0;
185798cd9ca7SRichard Henderson     if (n && use_nullify_skip(ctx)) {
185898cd9ca7SRichard Henderson         nullify_set(ctx, 0);
1859a881c8e7SRichard Henderson         gen_goto_tb(ctx, 0, ctx->iaoq_n, ctx->iaoq_n + 4);
186098cd9ca7SRichard Henderson     } else {
186198cd9ca7SRichard Henderson         if (!n && ctx->null_lab) {
186298cd9ca7SRichard Henderson             gen_set_label(ctx->null_lab);
186398cd9ca7SRichard Henderson             ctx->null_lab = NULL;
186498cd9ca7SRichard Henderson         }
186598cd9ca7SRichard Henderson         nullify_set(ctx, n);
1866c301f34eSRichard Henderson         if (ctx->iaoq_n == -1) {
1867c301f34eSRichard Henderson             /* The temporary iaoq_n_var died at the branch above.
1868c301f34eSRichard Henderson                Regenerate it here instead of saving it.  */
18696fd0c7bcSRichard Henderson             tcg_gen_addi_i64(ctx->iaoq_n_var, cpu_iaoq_b, 4);
1870c301f34eSRichard Henderson         }
1871a881c8e7SRichard Henderson         gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
187298cd9ca7SRichard Henderson     }
187398cd9ca7SRichard Henderson 
187498cd9ca7SRichard Henderson     gen_set_label(taken);
187598cd9ca7SRichard Henderson 
187698cd9ca7SRichard Henderson     /* Taken: Condition satisfied; nullify on forward branches.  */
187798cd9ca7SRichard Henderson     n = is_n && disp >= 0;
187898cd9ca7SRichard Henderson     if (n && use_nullify_skip(ctx)) {
187998cd9ca7SRichard Henderson         nullify_set(ctx, 0);
1880a881c8e7SRichard Henderson         gen_goto_tb(ctx, 1, dest, dest + 4);
188198cd9ca7SRichard Henderson     } else {
188298cd9ca7SRichard Henderson         nullify_set(ctx, n);
1883a881c8e7SRichard Henderson         gen_goto_tb(ctx, 1, ctx->iaoq_b, dest);
188498cd9ca7SRichard Henderson     }
188598cd9ca7SRichard Henderson 
188698cd9ca7SRichard Henderson     /* Not taken: the branch itself was nullified.  */
188798cd9ca7SRichard Henderson     if (ctx->null_lab) {
188898cd9ca7SRichard Henderson         gen_set_label(ctx->null_lab);
188998cd9ca7SRichard Henderson         ctx->null_lab = NULL;
189031234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
189198cd9ca7SRichard Henderson     } else {
189231234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
189398cd9ca7SRichard Henderson     }
189401afb7beSRichard Henderson     return true;
189598cd9ca7SRichard Henderson }
189698cd9ca7SRichard Henderson 
189798cd9ca7SRichard Henderson /* Emit an unconditional branch to an indirect target.  This handles
189898cd9ca7SRichard Henderson    nullification of the branch itself.  */
18996fd0c7bcSRichard Henderson static bool do_ibranch(DisasContext *ctx, TCGv_i64 dest,
190098cd9ca7SRichard Henderson                        unsigned link, bool is_n)
190198cd9ca7SRichard Henderson {
1902d582c1faSRichard Henderson     TCGv_i64 next;
190398cd9ca7SRichard Henderson 
1904d582c1faSRichard Henderson     if (ctx->null_cond.c == TCG_COND_NEVER && ctx->null_lab == NULL) {
1905d582c1faSRichard Henderson         next = tcg_temp_new_i64();
1906d582c1faSRichard Henderson         tcg_gen_mov_i64(next, dest);
190798cd9ca7SRichard Henderson 
1908*43541db0SRichard Henderson         install_link(ctx, link, false);
190998cd9ca7SRichard Henderson         if (is_n) {
1910c301f34eSRichard Henderson             if (use_nullify_skip(ctx)) {
191185e6cda0SRichard Henderson                 install_iaq_entries(ctx, -1, next, -1, NULL);
1912c301f34eSRichard Henderson                 nullify_set(ctx, 0);
191331234768SRichard Henderson                 ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
191401afb7beSRichard Henderson                 return true;
1915c301f34eSRichard Henderson             }
191698cd9ca7SRichard Henderson             ctx->null_cond.c = TCG_COND_ALWAYS;
191798cd9ca7SRichard Henderson         }
1918c301f34eSRichard Henderson         ctx->iaoq_n = -1;
1919c301f34eSRichard Henderson         ctx->iaoq_n_var = next;
1920d582c1faSRichard Henderson         return true;
1921d582c1faSRichard Henderson     }
192298cd9ca7SRichard Henderson 
1923d582c1faSRichard Henderson     nullify_over(ctx);
1924d582c1faSRichard Henderson 
1925*43541db0SRichard Henderson     next = tcg_temp_new_i64();
1926*43541db0SRichard Henderson     tcg_gen_mov_i64(next, dest);
1927*43541db0SRichard Henderson 
1928*43541db0SRichard Henderson     install_link(ctx, link, false);
1929d582c1faSRichard Henderson     if (is_n && use_nullify_skip(ctx)) {
1930*43541db0SRichard Henderson         install_iaq_entries(ctx, -1, next, -1, NULL);
1931d582c1faSRichard Henderson         nullify_set(ctx, 0);
1932d582c1faSRichard Henderson     } else {
1933*43541db0SRichard Henderson         install_iaq_entries(ctx, ctx->iaoq_b, cpu_iaoq_b, -1, next);
1934d582c1faSRichard Henderson         nullify_set(ctx, is_n);
1935d582c1faSRichard Henderson     }
1936d582c1faSRichard Henderson 
19377f11636dSEmilio G. Cota     tcg_gen_lookup_and_goto_ptr();
1938d582c1faSRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
193901afb7beSRichard Henderson     return nullify_end(ctx);
194098cd9ca7SRichard Henderson }
194198cd9ca7SRichard Henderson 
1942660eefe1SRichard Henderson /* Implement
1943660eefe1SRichard Henderson  *    if (IAOQ_Front{30..31} < GR[b]{30..31})
1944660eefe1SRichard Henderson  *      IAOQ_Next{30..31} ← GR[b]{30..31};
1945660eefe1SRichard Henderson  *    else
1946660eefe1SRichard Henderson  *      IAOQ_Next{30..31} ← IAOQ_Front{30..31};
1947660eefe1SRichard Henderson  * which keeps the privilege level from being increased.
1948660eefe1SRichard Henderson  */
19496fd0c7bcSRichard Henderson static TCGv_i64 do_ibranch_priv(DisasContext *ctx, TCGv_i64 offset)
1950660eefe1SRichard Henderson {
19516fd0c7bcSRichard Henderson     TCGv_i64 dest;
1952660eefe1SRichard Henderson     switch (ctx->privilege) {
1953660eefe1SRichard Henderson     case 0:
1954660eefe1SRichard Henderson         /* Privilege 0 is maximum and is allowed to decrease.  */
1955660eefe1SRichard Henderson         return offset;
1956660eefe1SRichard Henderson     case 3:
1957993119feSRichard Henderson         /* Privilege 3 is minimum and is never allowed to increase.  */
1958aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
19596fd0c7bcSRichard Henderson         tcg_gen_ori_i64(dest, offset, 3);
1960660eefe1SRichard Henderson         break;
1961660eefe1SRichard Henderson     default:
1962aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
19636fd0c7bcSRichard Henderson         tcg_gen_andi_i64(dest, offset, -4);
19646fd0c7bcSRichard Henderson         tcg_gen_ori_i64(dest, dest, ctx->privilege);
19656fd0c7bcSRichard Henderson         tcg_gen_movcond_i64(TCG_COND_GTU, dest, dest, offset, dest, offset);
1966660eefe1SRichard Henderson         break;
1967660eefe1SRichard Henderson     }
1968660eefe1SRichard Henderson     return dest;
1969660eefe1SRichard Henderson }
1970660eefe1SRichard Henderson 
1971ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
19727ad439dfSRichard Henderson /* On Linux, page zero is normally marked execute only + gateway.
19737ad439dfSRichard Henderson    Therefore normal read or write is supposed to fail, but specific
19747ad439dfSRichard Henderson    offsets have kernel code mapped to raise permissions to implement
19757ad439dfSRichard Henderson    system calls.  Handling this via an explicit check here, rather
19767ad439dfSRichard Henderson    in than the "be disp(sr2,r0)" instruction that probably sent us
19777ad439dfSRichard Henderson    here, is the easiest way to handle the branch delay slot on the
19787ad439dfSRichard Henderson    aforementioned BE.  */
197931234768SRichard Henderson static void do_page_zero(DisasContext *ctx)
19807ad439dfSRichard Henderson {
19816fd0c7bcSRichard Henderson     TCGv_i64 tmp;
1982a0180973SRichard Henderson 
19837ad439dfSRichard Henderson     /* If by some means we get here with PSW[N]=1, that implies that
19847ad439dfSRichard Henderson        the B,GATE instruction would be skipped, and we'd fault on the
19858b81968cSMichael Tokarev        next insn within the privileged page.  */
19867ad439dfSRichard Henderson     switch (ctx->null_cond.c) {
19877ad439dfSRichard Henderson     case TCG_COND_NEVER:
19887ad439dfSRichard Henderson         break;
19897ad439dfSRichard Henderson     case TCG_COND_ALWAYS:
19906fd0c7bcSRichard Henderson         tcg_gen_movi_i64(cpu_psw_n, 0);
19917ad439dfSRichard Henderson         goto do_sigill;
19927ad439dfSRichard Henderson     default:
19937ad439dfSRichard Henderson         /* Since this is always the first (and only) insn within the
19947ad439dfSRichard Henderson            TB, we should know the state of PSW[N] from TB->FLAGS.  */
19957ad439dfSRichard Henderson         g_assert_not_reached();
19967ad439dfSRichard Henderson     }
19977ad439dfSRichard Henderson 
19987ad439dfSRichard Henderson     /* Check that we didn't arrive here via some means that allowed
19997ad439dfSRichard Henderson        non-sequential instruction execution.  Normally the PSW[B] bit
20007ad439dfSRichard Henderson        detects this by disallowing the B,GATE instruction to execute
20017ad439dfSRichard Henderson        under such conditions.  */
20027ad439dfSRichard Henderson     if (ctx->iaoq_b != ctx->iaoq_f + 4) {
20037ad439dfSRichard Henderson         goto do_sigill;
20047ad439dfSRichard Henderson     }
20057ad439dfSRichard Henderson 
2006ebd0e151SRichard Henderson     switch (ctx->iaoq_f & -4) {
20077ad439dfSRichard Henderson     case 0x00: /* Null pointer call */
20082986721dSRichard Henderson         gen_excp_1(EXCP_IMP);
200931234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
201031234768SRichard Henderson         break;
20117ad439dfSRichard Henderson 
20127ad439dfSRichard Henderson     case 0xb0: /* LWS */
20137ad439dfSRichard Henderson         gen_excp_1(EXCP_SYSCALL_LWS);
201431234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
201531234768SRichard Henderson         break;
20167ad439dfSRichard Henderson 
20177ad439dfSRichard Henderson     case 0xe0: /* SET_THREAD_POINTER */
20186fd0c7bcSRichard Henderson         tcg_gen_st_i64(cpu_gr[26], tcg_env, offsetof(CPUHPPAState, cr[27]));
2019aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
20206fd0c7bcSRichard Henderson         tcg_gen_ori_i64(tmp, cpu_gr[31], 3);
202185e6cda0SRichard Henderson         install_iaq_entries(ctx, -1, tmp, -1, NULL);
202231234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_UPDATED;
202331234768SRichard Henderson         break;
20247ad439dfSRichard Henderson 
20257ad439dfSRichard Henderson     case 0x100: /* SYSCALL */
20267ad439dfSRichard Henderson         gen_excp_1(EXCP_SYSCALL);
202731234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
202831234768SRichard Henderson         break;
20297ad439dfSRichard Henderson 
20307ad439dfSRichard Henderson     default:
20317ad439dfSRichard Henderson     do_sigill:
20322986721dSRichard Henderson         gen_excp_1(EXCP_ILL);
203331234768SRichard Henderson         ctx->base.is_jmp = DISAS_NORETURN;
203431234768SRichard Henderson         break;
20357ad439dfSRichard Henderson     }
20367ad439dfSRichard Henderson }
2037ba1d0b44SRichard Henderson #endif
20387ad439dfSRichard Henderson 
2039deee69a1SRichard Henderson static bool trans_nop(DisasContext *ctx, arg_nop *a)
2040b2167459SRichard Henderson {
2041b2167459SRichard Henderson     cond_free(&ctx->null_cond);
204231234768SRichard Henderson     return true;
2043b2167459SRichard Henderson }
2044b2167459SRichard Henderson 
204540f9f908SRichard Henderson static bool trans_break(DisasContext *ctx, arg_break *a)
204698a9cb79SRichard Henderson {
204731234768SRichard Henderson     return gen_excp_iir(ctx, EXCP_BREAK);
204898a9cb79SRichard Henderson }
204998a9cb79SRichard Henderson 
2050e36f27efSRichard Henderson static bool trans_sync(DisasContext *ctx, arg_sync *a)
205198a9cb79SRichard Henderson {
205298a9cb79SRichard Henderson     /* No point in nullifying the memory barrier.  */
205398a9cb79SRichard Henderson     tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
205498a9cb79SRichard Henderson 
205598a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
205631234768SRichard Henderson     return true;
205798a9cb79SRichard Henderson }
205898a9cb79SRichard Henderson 
2059c603e14aSRichard Henderson static bool trans_mfia(DisasContext *ctx, arg_mfia *a)
206098a9cb79SRichard Henderson {
2061c603e14aSRichard Henderson     unsigned rt = a->t;
20626fd0c7bcSRichard Henderson     TCGv_i64 tmp = dest_gpr(ctx, rt);
2063b5e0b3a5SSven Schnelle     tcg_gen_movi_i64(tmp, ctx->iaoq_f & ~3ULL);
206498a9cb79SRichard Henderson     save_gpr(ctx, rt, tmp);
206598a9cb79SRichard Henderson 
206698a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
206731234768SRichard Henderson     return true;
206898a9cb79SRichard Henderson }
206998a9cb79SRichard Henderson 
2070c603e14aSRichard Henderson static bool trans_mfsp(DisasContext *ctx, arg_mfsp *a)
207198a9cb79SRichard Henderson {
2072c603e14aSRichard Henderson     unsigned rt = a->t;
2073c603e14aSRichard Henderson     unsigned rs = a->sp;
207433423472SRichard Henderson     TCGv_i64 t0 = tcg_temp_new_i64();
207598a9cb79SRichard Henderson 
207633423472SRichard Henderson     load_spr(ctx, t0, rs);
207733423472SRichard Henderson     tcg_gen_shri_i64(t0, t0, 32);
207833423472SRichard Henderson 
2079967662cdSRichard Henderson     save_gpr(ctx, rt, t0);
208098a9cb79SRichard Henderson 
208198a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
208231234768SRichard Henderson     return true;
208398a9cb79SRichard Henderson }
208498a9cb79SRichard Henderson 
2085c603e14aSRichard Henderson static bool trans_mfctl(DisasContext *ctx, arg_mfctl *a)
208698a9cb79SRichard Henderson {
2087c603e14aSRichard Henderson     unsigned rt = a->t;
2088c603e14aSRichard Henderson     unsigned ctl = a->r;
20896fd0c7bcSRichard Henderson     TCGv_i64 tmp;
209098a9cb79SRichard Henderson 
209198a9cb79SRichard Henderson     switch (ctl) {
209235136a77SRichard Henderson     case CR_SAR:
2093c603e14aSRichard Henderson         if (a->e == 0) {
209498a9cb79SRichard Henderson             /* MFSAR without ,W masks low 5 bits.  */
209598a9cb79SRichard Henderson             tmp = dest_gpr(ctx, rt);
20966fd0c7bcSRichard Henderson             tcg_gen_andi_i64(tmp, cpu_sar, 31);
209798a9cb79SRichard Henderson             save_gpr(ctx, rt, tmp);
209835136a77SRichard Henderson             goto done;
209998a9cb79SRichard Henderson         }
210098a9cb79SRichard Henderson         save_gpr(ctx, rt, cpu_sar);
210135136a77SRichard Henderson         goto done;
210235136a77SRichard Henderson     case CR_IT: /* Interval Timer */
210335136a77SRichard Henderson         /* FIXME: Respect PSW_S bit.  */
210435136a77SRichard Henderson         nullify_over(ctx);
210598a9cb79SRichard Henderson         tmp = dest_gpr(ctx, rt);
2106dfd1b812SRichard Henderson         if (translator_io_start(&ctx->base)) {
210731234768SRichard Henderson             ctx->base.is_jmp = DISAS_IAQ_N_STALE;
210849c29d6cSRichard Henderson         }
21090c58c1bcSRichard Henderson         gen_helper_read_interval_timer(tmp);
211098a9cb79SRichard Henderson         save_gpr(ctx, rt, tmp);
211131234768SRichard Henderson         return nullify_end(ctx);
211298a9cb79SRichard Henderson     case 26:
211398a9cb79SRichard Henderson     case 27:
211498a9cb79SRichard Henderson         break;
211598a9cb79SRichard Henderson     default:
211698a9cb79SRichard Henderson         /* All other control registers are privileged.  */
211735136a77SRichard Henderson         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
211835136a77SRichard Henderson         break;
211998a9cb79SRichard Henderson     }
212098a9cb79SRichard Henderson 
2121aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
21226fd0c7bcSRichard Henderson     tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
212335136a77SRichard Henderson     save_gpr(ctx, rt, tmp);
212435136a77SRichard Henderson 
212535136a77SRichard Henderson  done:
212698a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
212731234768SRichard Henderson     return true;
212898a9cb79SRichard Henderson }
212998a9cb79SRichard Henderson 
2130c603e14aSRichard Henderson static bool trans_mtsp(DisasContext *ctx, arg_mtsp *a)
213133423472SRichard Henderson {
2132c603e14aSRichard Henderson     unsigned rr = a->r;
2133c603e14aSRichard Henderson     unsigned rs = a->sp;
2134967662cdSRichard Henderson     TCGv_i64 tmp;
213533423472SRichard Henderson 
213633423472SRichard Henderson     if (rs >= 5) {
213733423472SRichard Henderson         CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
213833423472SRichard Henderson     }
213933423472SRichard Henderson     nullify_over(ctx);
214033423472SRichard Henderson 
2141967662cdSRichard Henderson     tmp = tcg_temp_new_i64();
2142967662cdSRichard Henderson     tcg_gen_shli_i64(tmp, load_gpr(ctx, rr), 32);
214333423472SRichard Henderson 
214433423472SRichard Henderson     if (rs >= 4) {
2145967662cdSRichard Henderson         tcg_gen_st_i64(tmp, tcg_env, offsetof(CPUHPPAState, sr[rs]));
2146494737b7SRichard Henderson         ctx->tb_flags &= ~TB_FLAG_SR_SAME;
214733423472SRichard Henderson     } else {
2148967662cdSRichard Henderson         tcg_gen_mov_i64(cpu_sr[rs], tmp);
214933423472SRichard Henderson     }
215033423472SRichard Henderson 
215131234768SRichard Henderson     return nullify_end(ctx);
215233423472SRichard Henderson }
215333423472SRichard Henderson 
2154c603e14aSRichard Henderson static bool trans_mtctl(DisasContext *ctx, arg_mtctl *a)
215598a9cb79SRichard Henderson {
2156c603e14aSRichard Henderson     unsigned ctl = a->t;
21576fd0c7bcSRichard Henderson     TCGv_i64 reg;
21586fd0c7bcSRichard Henderson     TCGv_i64 tmp;
215998a9cb79SRichard Henderson 
216035136a77SRichard Henderson     if (ctl == CR_SAR) {
21614845f015SSven Schnelle         reg = load_gpr(ctx, a->r);
2162aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
21636fd0c7bcSRichard Henderson         tcg_gen_andi_i64(tmp, reg, ctx->is_pa20 ? 63 : 31);
216498a9cb79SRichard Henderson         save_or_nullify(ctx, cpu_sar, tmp);
216598a9cb79SRichard Henderson 
216698a9cb79SRichard Henderson         cond_free(&ctx->null_cond);
216731234768SRichard Henderson         return true;
216898a9cb79SRichard Henderson     }
216998a9cb79SRichard Henderson 
217035136a77SRichard Henderson     /* All other control registers are privileged or read-only.  */
217135136a77SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_REG);
217235136a77SRichard Henderson 
2173c603e14aSRichard Henderson #ifndef CONFIG_USER_ONLY
217435136a77SRichard Henderson     nullify_over(ctx);
21754c34bab0SHelge Deller 
21764c34bab0SHelge Deller     if (ctx->is_pa20) {
21774845f015SSven Schnelle         reg = load_gpr(ctx, a->r);
21784c34bab0SHelge Deller     } else {
21794c34bab0SHelge Deller         reg = tcg_temp_new_i64();
21804c34bab0SHelge Deller         tcg_gen_ext32u_i64(reg, load_gpr(ctx, a->r));
21814c34bab0SHelge Deller     }
21824845f015SSven Schnelle 
218335136a77SRichard Henderson     switch (ctl) {
218435136a77SRichard Henderson     case CR_IT:
2185104281c1SRichard Henderson         if (translator_io_start(&ctx->base)) {
2186104281c1SRichard Henderson             ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2187104281c1SRichard Henderson         }
2188ad75a51eSRichard Henderson         gen_helper_write_interval_timer(tcg_env, reg);
218935136a77SRichard Henderson         break;
21904f5f2548SRichard Henderson     case CR_EIRR:
21916ebebea7SRichard Henderson         /* Helper modifies interrupt lines and is therefore IO. */
21926ebebea7SRichard Henderson         translator_io_start(&ctx->base);
2193ad75a51eSRichard Henderson         gen_helper_write_eirr(tcg_env, reg);
21946ebebea7SRichard Henderson         /* Exit to re-evaluate interrupts in the main loop. */
219531234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
21964f5f2548SRichard Henderson         break;
21974f5f2548SRichard Henderson 
219835136a77SRichard Henderson     case CR_IIASQ:
219935136a77SRichard Henderson     case CR_IIAOQ:
220035136a77SRichard Henderson         /* FIXME: Respect PSW_Q bit */
220135136a77SRichard Henderson         /* The write advances the queue and stores to the back element.  */
2202aac0f603SRichard Henderson         tmp = tcg_temp_new_i64();
22036fd0c7bcSRichard Henderson         tcg_gen_ld_i64(tmp, tcg_env,
220435136a77SRichard Henderson                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
22056fd0c7bcSRichard Henderson         tcg_gen_st_i64(tmp, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
22066fd0c7bcSRichard Henderson         tcg_gen_st_i64(reg, tcg_env,
220735136a77SRichard Henderson                        offsetof(CPUHPPAState, cr_back[ctl - CR_IIASQ]));
220835136a77SRichard Henderson         break;
220935136a77SRichard Henderson 
2210d5de20bdSSven Schnelle     case CR_PID1:
2211d5de20bdSSven Schnelle     case CR_PID2:
2212d5de20bdSSven Schnelle     case CR_PID3:
2213d5de20bdSSven Schnelle     case CR_PID4:
22146fd0c7bcSRichard Henderson         tcg_gen_st_i64(reg, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
2215d5de20bdSSven Schnelle #ifndef CONFIG_USER_ONLY
2216ad75a51eSRichard Henderson         gen_helper_change_prot_id(tcg_env);
2217d5de20bdSSven Schnelle #endif
2218d5de20bdSSven Schnelle         break;
2219d5de20bdSSven Schnelle 
22206ebebea7SRichard Henderson     case CR_EIEM:
22216ebebea7SRichard Henderson         /* Exit to re-evaluate interrupts in the main loop. */
22226ebebea7SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
22236ebebea7SRichard Henderson         /* FALLTHRU */
222435136a77SRichard Henderson     default:
22256fd0c7bcSRichard Henderson         tcg_gen_st_i64(reg, tcg_env, offsetof(CPUHPPAState, cr[ctl]));
222635136a77SRichard Henderson         break;
222735136a77SRichard Henderson     }
222831234768SRichard Henderson     return nullify_end(ctx);
22294f5f2548SRichard Henderson #endif
223035136a77SRichard Henderson }
223135136a77SRichard Henderson 
2232c603e14aSRichard Henderson static bool trans_mtsarcm(DisasContext *ctx, arg_mtsarcm *a)
223398a9cb79SRichard Henderson {
2234aac0f603SRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
223598a9cb79SRichard Henderson 
22366fd0c7bcSRichard Henderson     tcg_gen_not_i64(tmp, load_gpr(ctx, a->r));
22376fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, ctx->is_pa20 ? 63 : 31);
223898a9cb79SRichard Henderson     save_or_nullify(ctx, cpu_sar, tmp);
223998a9cb79SRichard Henderson 
224098a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
224131234768SRichard Henderson     return true;
224298a9cb79SRichard Henderson }
224398a9cb79SRichard Henderson 
2244e36f27efSRichard Henderson static bool trans_ldsid(DisasContext *ctx, arg_ldsid *a)
224598a9cb79SRichard Henderson {
22466fd0c7bcSRichard Henderson     TCGv_i64 dest = dest_gpr(ctx, a->t);
224798a9cb79SRichard Henderson 
22482330504cSHelge Deller #ifdef CONFIG_USER_ONLY
22492330504cSHelge Deller     /* We don't implement space registers in user mode. */
22506fd0c7bcSRichard Henderson     tcg_gen_movi_i64(dest, 0);
22512330504cSHelge Deller #else
2252967662cdSRichard Henderson     tcg_gen_mov_i64(dest, space_select(ctx, a->sp, load_gpr(ctx, a->b)));
2253967662cdSRichard Henderson     tcg_gen_shri_i64(dest, dest, 32);
22542330504cSHelge Deller #endif
2255e36f27efSRichard Henderson     save_gpr(ctx, a->t, dest);
225698a9cb79SRichard Henderson 
225798a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
225831234768SRichard Henderson     return true;
225998a9cb79SRichard Henderson }
226098a9cb79SRichard Henderson 
2261e36f27efSRichard Henderson static bool trans_rsm(DisasContext *ctx, arg_rsm *a)
2262e36f27efSRichard Henderson {
22637b2d70a1SHelge Deller #ifdef CONFIG_USER_ONLY
2264e36f27efSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
22657b2d70a1SHelge Deller #else
22666fd0c7bcSRichard Henderson     TCGv_i64 tmp;
2267e1b5a5edSRichard Henderson 
22687b2d70a1SHelge Deller     /* HP-UX 11i and HP ODE use rsm for read-access to PSW */
22697b2d70a1SHelge Deller     if (a->i) {
22707b2d70a1SHelge Deller         CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
22717b2d70a1SHelge Deller     }
22727b2d70a1SHelge Deller 
2273e1b5a5edSRichard Henderson     nullify_over(ctx);
2274e1b5a5edSRichard Henderson 
2275aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
22766fd0c7bcSRichard Henderson     tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPUHPPAState, psw));
22776fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, ~a->i);
2278ad75a51eSRichard Henderson     gen_helper_swap_system_mask(tmp, tcg_env, tmp);
2279e36f27efSRichard Henderson     save_gpr(ctx, a->t, tmp);
2280e1b5a5edSRichard Henderson 
2281e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts, e.g. PSW_M.  */
228231234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
228331234768SRichard Henderson     return nullify_end(ctx);
2284e36f27efSRichard Henderson #endif
2285e1b5a5edSRichard Henderson }
2286e1b5a5edSRichard Henderson 
2287e36f27efSRichard Henderson static bool trans_ssm(DisasContext *ctx, arg_ssm *a)
2288e1b5a5edSRichard Henderson {
2289e36f27efSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2290e36f27efSRichard Henderson #ifndef CONFIG_USER_ONLY
22916fd0c7bcSRichard Henderson     TCGv_i64 tmp;
2292e1b5a5edSRichard Henderson 
2293e1b5a5edSRichard Henderson     nullify_over(ctx);
2294e1b5a5edSRichard Henderson 
2295aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
22966fd0c7bcSRichard Henderson     tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPUHPPAState, psw));
22976fd0c7bcSRichard Henderson     tcg_gen_ori_i64(tmp, tmp, a->i);
2298ad75a51eSRichard Henderson     gen_helper_swap_system_mask(tmp, tcg_env, tmp);
2299e36f27efSRichard Henderson     save_gpr(ctx, a->t, tmp);
2300e1b5a5edSRichard Henderson 
2301e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts, e.g. PSW_I.  */
230231234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
230331234768SRichard Henderson     return nullify_end(ctx);
2304e36f27efSRichard Henderson #endif
2305e1b5a5edSRichard Henderson }
2306e1b5a5edSRichard Henderson 
2307c603e14aSRichard Henderson static bool trans_mtsm(DisasContext *ctx, arg_mtsm *a)
2308e1b5a5edSRichard Henderson {
2309e1b5a5edSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2310c603e14aSRichard Henderson #ifndef CONFIG_USER_ONLY
23116fd0c7bcSRichard Henderson     TCGv_i64 tmp, reg;
2312e1b5a5edSRichard Henderson     nullify_over(ctx);
2313e1b5a5edSRichard Henderson 
2314c603e14aSRichard Henderson     reg = load_gpr(ctx, a->r);
2315aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
2316ad75a51eSRichard Henderson     gen_helper_swap_system_mask(tmp, tcg_env, reg);
2317e1b5a5edSRichard Henderson 
2318e1b5a5edSRichard Henderson     /* Exit the TB to recognize new interrupts.  */
231931234768SRichard Henderson     ctx->base.is_jmp = DISAS_IAQ_N_STALE_EXIT;
232031234768SRichard Henderson     return nullify_end(ctx);
2321c603e14aSRichard Henderson #endif
2322e1b5a5edSRichard Henderson }
2323f49b3537SRichard Henderson 
2324e36f27efSRichard Henderson static bool do_rfi(DisasContext *ctx, bool rfi_r)
2325f49b3537SRichard Henderson {
2326f49b3537SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2327e36f27efSRichard Henderson #ifndef CONFIG_USER_ONLY
2328f49b3537SRichard Henderson     nullify_over(ctx);
2329f49b3537SRichard Henderson 
2330e36f27efSRichard Henderson     if (rfi_r) {
2331ad75a51eSRichard Henderson         gen_helper_rfi_r(tcg_env);
2332f49b3537SRichard Henderson     } else {
2333ad75a51eSRichard Henderson         gen_helper_rfi(tcg_env);
2334f49b3537SRichard Henderson     }
233531234768SRichard Henderson     /* Exit the TB to recognize new interrupts.  */
233607ea28b4SRichard Henderson     tcg_gen_exit_tb(NULL, 0);
233731234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
2338f49b3537SRichard Henderson 
233931234768SRichard Henderson     return nullify_end(ctx);
2340e36f27efSRichard Henderson #endif
2341f49b3537SRichard Henderson }
23426210db05SHelge Deller 
2343e36f27efSRichard Henderson static bool trans_rfi(DisasContext *ctx, arg_rfi *a)
2344e36f27efSRichard Henderson {
2345e36f27efSRichard Henderson     return do_rfi(ctx, false);
2346e36f27efSRichard Henderson }
2347e36f27efSRichard Henderson 
2348e36f27efSRichard Henderson static bool trans_rfi_r(DisasContext *ctx, arg_rfi_r *a)
2349e36f27efSRichard Henderson {
2350e36f27efSRichard Henderson     return do_rfi(ctx, true);
2351e36f27efSRichard Henderson }
2352e36f27efSRichard Henderson 
235396927adbSRichard Henderson static bool trans_halt(DisasContext *ctx, arg_halt *a)
23546210db05SHelge Deller {
23556210db05SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
235696927adbSRichard Henderson #ifndef CONFIG_USER_ONLY
23576210db05SHelge Deller     nullify_over(ctx);
2358ad75a51eSRichard Henderson     gen_helper_halt(tcg_env);
235931234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
236031234768SRichard Henderson     return nullify_end(ctx);
236196927adbSRichard Henderson #endif
23626210db05SHelge Deller }
236396927adbSRichard Henderson 
236496927adbSRichard Henderson static bool trans_reset(DisasContext *ctx, arg_reset *a)
236596927adbSRichard Henderson {
236696927adbSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
236796927adbSRichard Henderson #ifndef CONFIG_USER_ONLY
236896927adbSRichard Henderson     nullify_over(ctx);
2369ad75a51eSRichard Henderson     gen_helper_reset(tcg_env);
237096927adbSRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
237196927adbSRichard Henderson     return nullify_end(ctx);
237296927adbSRichard Henderson #endif
237396927adbSRichard Henderson }
2374e1b5a5edSRichard Henderson 
2375558c09beSRichard Henderson static bool do_getshadowregs(DisasContext *ctx)
23764a4554c6SHelge Deller {
23774a4554c6SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
23784a4554c6SHelge Deller     nullify_over(ctx);
2379558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[1], tcg_env, offsetof(CPUHPPAState, shadow[0]));
2380558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[8], tcg_env, offsetof(CPUHPPAState, shadow[1]));
2381558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[9], tcg_env, offsetof(CPUHPPAState, shadow[2]));
2382558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[16], tcg_env, offsetof(CPUHPPAState, shadow[3]));
2383558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[17], tcg_env, offsetof(CPUHPPAState, shadow[4]));
2384558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[24], tcg_env, offsetof(CPUHPPAState, shadow[5]));
2385558c09beSRichard Henderson     tcg_gen_ld_i64(cpu_gr[25], tcg_env, offsetof(CPUHPPAState, shadow[6]));
23864a4554c6SHelge Deller     return nullify_end(ctx);
2387558c09beSRichard Henderson }
2388558c09beSRichard Henderson 
23893bdf2081SHelge Deller static bool do_putshadowregs(DisasContext *ctx)
23903bdf2081SHelge Deller {
23913bdf2081SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
23923bdf2081SHelge Deller     nullify_over(ctx);
23933bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[1], tcg_env, offsetof(CPUHPPAState, shadow[0]));
23943bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[8], tcg_env, offsetof(CPUHPPAState, shadow[1]));
23953bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[9], tcg_env, offsetof(CPUHPPAState, shadow[2]));
23963bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[16], tcg_env, offsetof(CPUHPPAState, shadow[3]));
23973bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[17], tcg_env, offsetof(CPUHPPAState, shadow[4]));
23983bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[24], tcg_env, offsetof(CPUHPPAState, shadow[5]));
23993bdf2081SHelge Deller     tcg_gen_st_i64(cpu_gr[25], tcg_env, offsetof(CPUHPPAState, shadow[6]));
24003bdf2081SHelge Deller     return nullify_end(ctx);
24013bdf2081SHelge Deller }
24023bdf2081SHelge Deller 
2403558c09beSRichard Henderson static bool trans_getshadowregs(DisasContext *ctx, arg_getshadowregs *a)
2404558c09beSRichard Henderson {
2405558c09beSRichard Henderson     return do_getshadowregs(ctx);
24064a4554c6SHelge Deller }
24074a4554c6SHelge Deller 
2408deee69a1SRichard Henderson static bool trans_nop_addrx(DisasContext *ctx, arg_ldst *a)
240998a9cb79SRichard Henderson {
2410deee69a1SRichard Henderson     if (a->m) {
24116fd0c7bcSRichard Henderson         TCGv_i64 dest = dest_gpr(ctx, a->b);
24126fd0c7bcSRichard Henderson         TCGv_i64 src1 = load_gpr(ctx, a->b);
24136fd0c7bcSRichard Henderson         TCGv_i64 src2 = load_gpr(ctx, a->x);
241498a9cb79SRichard Henderson 
241598a9cb79SRichard Henderson         /* The only thing we need to do is the base register modification.  */
24166fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, src1, src2);
2417deee69a1SRichard Henderson         save_gpr(ctx, a->b, dest);
2418deee69a1SRichard Henderson     }
241998a9cb79SRichard Henderson     cond_free(&ctx->null_cond);
242031234768SRichard Henderson     return true;
242198a9cb79SRichard Henderson }
242298a9cb79SRichard Henderson 
2423ad1fdacdSSven Schnelle static bool trans_fic(DisasContext *ctx, arg_ldst *a)
2424ad1fdacdSSven Schnelle {
2425ad1fdacdSSven Schnelle     /* End TB for flush instruction cache, so we pick up new insns. */
2426ad1fdacdSSven Schnelle     ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2427ad1fdacdSSven Schnelle     return trans_nop_addrx(ctx, a);
2428ad1fdacdSSven Schnelle }
2429ad1fdacdSSven Schnelle 
2430deee69a1SRichard Henderson static bool trans_probe(DisasContext *ctx, arg_probe *a)
243198a9cb79SRichard Henderson {
24326fd0c7bcSRichard Henderson     TCGv_i64 dest, ofs;
2433eed14219SRichard Henderson     TCGv_i32 level, want;
24346fd0c7bcSRichard Henderson     TCGv_i64 addr;
243598a9cb79SRichard Henderson 
243698a9cb79SRichard Henderson     nullify_over(ctx);
243798a9cb79SRichard Henderson 
2438deee69a1SRichard Henderson     dest = dest_gpr(ctx, a->t);
2439deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2440eed14219SRichard Henderson 
2441deee69a1SRichard Henderson     if (a->imm) {
2442e5d487c9SRichard Henderson         level = tcg_constant_i32(a->ri & 3);
244398a9cb79SRichard Henderson     } else {
2444eed14219SRichard Henderson         level = tcg_temp_new_i32();
24456fd0c7bcSRichard Henderson         tcg_gen_extrl_i64_i32(level, load_gpr(ctx, a->ri));
2446eed14219SRichard Henderson         tcg_gen_andi_i32(level, level, 3);
244798a9cb79SRichard Henderson     }
244829dd6f64SRichard Henderson     want = tcg_constant_i32(a->write ? PAGE_WRITE : PAGE_READ);
2449eed14219SRichard Henderson 
2450ad75a51eSRichard Henderson     gen_helper_probe(dest, tcg_env, addr, level, want);
2451eed14219SRichard Henderson 
2452deee69a1SRichard Henderson     save_gpr(ctx, a->t, dest);
245331234768SRichard Henderson     return nullify_end(ctx);
245498a9cb79SRichard Henderson }
245598a9cb79SRichard Henderson 
2456deee69a1SRichard Henderson static bool trans_ixtlbx(DisasContext *ctx, arg_ixtlbx *a)
24578d6ae7fbSRichard Henderson {
24588577f354SRichard Henderson     if (ctx->is_pa20) {
24598577f354SRichard Henderson         return false;
24608577f354SRichard Henderson     }
2461deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2462deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
24636fd0c7bcSRichard Henderson     TCGv_i64 addr;
24646fd0c7bcSRichard Henderson     TCGv_i64 ofs, reg;
24658d6ae7fbSRichard Henderson 
24668d6ae7fbSRichard Henderson     nullify_over(ctx);
24678d6ae7fbSRichard Henderson 
2468deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, 0, a->sp, 0, false);
2469deee69a1SRichard Henderson     reg = load_gpr(ctx, a->r);
2470deee69a1SRichard Henderson     if (a->addr) {
24718577f354SRichard Henderson         gen_helper_itlba_pa11(tcg_env, addr, reg);
24728d6ae7fbSRichard Henderson     } else {
24738577f354SRichard Henderson         gen_helper_itlbp_pa11(tcg_env, addr, reg);
24748d6ae7fbSRichard Henderson     }
24758d6ae7fbSRichard Henderson 
247632dc7569SSven Schnelle     /* Exit TB for TLB change if mmu is enabled.  */
247732dc7569SSven Schnelle     if (ctx->tb_flags & PSW_C) {
247831234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
247931234768SRichard Henderson     }
248031234768SRichard Henderson     return nullify_end(ctx);
2481deee69a1SRichard Henderson #endif
24828d6ae7fbSRichard Henderson }
248363300a00SRichard Henderson 
2484eb25d10fSHelge Deller static bool do_pxtlb(DisasContext *ctx, arg_ldst *a, bool local)
248563300a00SRichard Henderson {
2486deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2487deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
24886fd0c7bcSRichard Henderson     TCGv_i64 addr;
24896fd0c7bcSRichard Henderson     TCGv_i64 ofs;
249063300a00SRichard Henderson 
249163300a00SRichard Henderson     nullify_over(ctx);
249263300a00SRichard Henderson 
2493deee69a1SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
2494eb25d10fSHelge Deller 
2495eb25d10fSHelge Deller     /*
2496eb25d10fSHelge Deller      * Page align now, rather than later, so that we can add in the
2497eb25d10fSHelge Deller      * page_size field from pa2.0 from the low 4 bits of GR[b].
2498eb25d10fSHelge Deller      */
2499eb25d10fSHelge Deller     tcg_gen_andi_i64(addr, addr, TARGET_PAGE_MASK);
2500eb25d10fSHelge Deller     if (ctx->is_pa20) {
2501eb25d10fSHelge Deller         tcg_gen_deposit_i64(addr, addr, load_gpr(ctx, a->b), 0, 4);
250263300a00SRichard Henderson     }
2503eb25d10fSHelge Deller 
2504eb25d10fSHelge Deller     if (local) {
2505eb25d10fSHelge Deller         gen_helper_ptlb_l(tcg_env, addr);
250663300a00SRichard Henderson     } else {
2507ad75a51eSRichard Henderson         gen_helper_ptlb(tcg_env, addr);
250863300a00SRichard Henderson     }
250963300a00SRichard Henderson 
2510eb25d10fSHelge Deller     if (a->m) {
2511eb25d10fSHelge Deller         save_gpr(ctx, a->b, ofs);
2512eb25d10fSHelge Deller     }
2513eb25d10fSHelge Deller 
2514eb25d10fSHelge Deller     /* Exit TB for TLB change if mmu is enabled.  */
2515eb25d10fSHelge Deller     if (ctx->tb_flags & PSW_C) {
2516eb25d10fSHelge Deller         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
2517eb25d10fSHelge Deller     }
2518eb25d10fSHelge Deller     return nullify_end(ctx);
2519eb25d10fSHelge Deller #endif
2520eb25d10fSHelge Deller }
2521eb25d10fSHelge Deller 
2522eb25d10fSHelge Deller static bool trans_pxtlb(DisasContext *ctx, arg_ldst *a)
2523eb25d10fSHelge Deller {
2524eb25d10fSHelge Deller     return do_pxtlb(ctx, a, false);
2525eb25d10fSHelge Deller }
2526eb25d10fSHelge Deller 
2527eb25d10fSHelge Deller static bool trans_pxtlb_l(DisasContext *ctx, arg_ldst *a)
2528eb25d10fSHelge Deller {
2529eb25d10fSHelge Deller     return ctx->is_pa20 && do_pxtlb(ctx, a, true);
2530eb25d10fSHelge Deller }
2531eb25d10fSHelge Deller 
2532eb25d10fSHelge Deller static bool trans_pxtlbe(DisasContext *ctx, arg_ldst *a)
2533eb25d10fSHelge Deller {
2534eb25d10fSHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2535eb25d10fSHelge Deller #ifndef CONFIG_USER_ONLY
2536eb25d10fSHelge Deller     nullify_over(ctx);
2537eb25d10fSHelge Deller 
2538eb25d10fSHelge Deller     trans_nop_addrx(ctx, a);
2539eb25d10fSHelge Deller     gen_helper_ptlbe(tcg_env);
2540eb25d10fSHelge Deller 
254163300a00SRichard Henderson     /* Exit TB for TLB change if mmu is enabled.  */
254232dc7569SSven Schnelle     if (ctx->tb_flags & PSW_C) {
254331234768SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
254431234768SRichard Henderson     }
254531234768SRichard Henderson     return nullify_end(ctx);
2546deee69a1SRichard Henderson #endif
254763300a00SRichard Henderson }
25482dfcca9fSRichard Henderson 
25496797c315SNick Hudson /*
25506797c315SNick Hudson  * Implement the pcxl and pcxl2 Fast TLB Insert instructions.
25516797c315SNick Hudson  * See
25526797c315SNick Hudson  *     https://parisc.wiki.kernel.org/images-parisc/a/a9/Pcxl2_ers.pdf
25536797c315SNick Hudson  *     page 13-9 (195/206)
25546797c315SNick Hudson  */
25556797c315SNick Hudson static bool trans_ixtlbxf(DisasContext *ctx, arg_ixtlbxf *a)
25566797c315SNick Hudson {
25578577f354SRichard Henderson     if (ctx->is_pa20) {
25588577f354SRichard Henderson         return false;
25598577f354SRichard Henderson     }
25606797c315SNick Hudson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
25616797c315SNick Hudson #ifndef CONFIG_USER_ONLY
25626fd0c7bcSRichard Henderson     TCGv_i64 addr, atl, stl;
25636fd0c7bcSRichard Henderson     TCGv_i64 reg;
25646797c315SNick Hudson 
25656797c315SNick Hudson     nullify_over(ctx);
25666797c315SNick Hudson 
25676797c315SNick Hudson     /*
25686797c315SNick Hudson      * FIXME:
25696797c315SNick Hudson      *  if (not (pcxl or pcxl2))
25706797c315SNick Hudson      *    return gen_illegal(ctx);
25716797c315SNick Hudson      */
25726797c315SNick Hudson 
25736fd0c7bcSRichard Henderson     atl = tcg_temp_new_i64();
25746fd0c7bcSRichard Henderson     stl = tcg_temp_new_i64();
25756fd0c7bcSRichard Henderson     addr = tcg_temp_new_i64();
25766797c315SNick Hudson 
2577ad75a51eSRichard Henderson     tcg_gen_ld32u_i64(stl, tcg_env,
25786797c315SNick Hudson                       a->data ? offsetof(CPUHPPAState, cr[CR_ISR])
25796797c315SNick Hudson                       : offsetof(CPUHPPAState, cr[CR_IIASQ]));
2580ad75a51eSRichard Henderson     tcg_gen_ld32u_i64(atl, tcg_env,
25816797c315SNick Hudson                       a->data ? offsetof(CPUHPPAState, cr[CR_IOR])
25826797c315SNick Hudson                       : offsetof(CPUHPPAState, cr[CR_IIAOQ]));
25836797c315SNick Hudson     tcg_gen_shli_i64(stl, stl, 32);
2584d265360fSRichard Henderson     tcg_gen_or_i64(addr, atl, stl);
25856797c315SNick Hudson 
25866797c315SNick Hudson     reg = load_gpr(ctx, a->r);
25876797c315SNick Hudson     if (a->addr) {
25888577f354SRichard Henderson         gen_helper_itlba_pa11(tcg_env, addr, reg);
25896797c315SNick Hudson     } else {
25908577f354SRichard Henderson         gen_helper_itlbp_pa11(tcg_env, addr, reg);
25916797c315SNick Hudson     }
25926797c315SNick Hudson 
25936797c315SNick Hudson     /* Exit TB for TLB change if mmu is enabled.  */
25946797c315SNick Hudson     if (ctx->tb_flags & PSW_C) {
25956797c315SNick Hudson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
25966797c315SNick Hudson     }
25976797c315SNick Hudson     return nullify_end(ctx);
25986797c315SNick Hudson #endif
25996797c315SNick Hudson }
26006797c315SNick Hudson 
26018577f354SRichard Henderson static bool trans_ixtlbt(DisasContext *ctx, arg_ixtlbt *a)
26028577f354SRichard Henderson {
26038577f354SRichard Henderson     if (!ctx->is_pa20) {
26048577f354SRichard Henderson         return false;
26058577f354SRichard Henderson     }
26068577f354SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
26078577f354SRichard Henderson #ifndef CONFIG_USER_ONLY
26088577f354SRichard Henderson     nullify_over(ctx);
26098577f354SRichard Henderson     {
26108577f354SRichard Henderson         TCGv_i64 src1 = load_gpr(ctx, a->r1);
26118577f354SRichard Henderson         TCGv_i64 src2 = load_gpr(ctx, a->r2);
26128577f354SRichard Henderson 
26138577f354SRichard Henderson         if (a->data) {
26148577f354SRichard Henderson             gen_helper_idtlbt_pa20(tcg_env, src1, src2);
26158577f354SRichard Henderson         } else {
26168577f354SRichard Henderson             gen_helper_iitlbt_pa20(tcg_env, src1, src2);
26178577f354SRichard Henderson         }
26188577f354SRichard Henderson     }
26198577f354SRichard Henderson     /* Exit TB for TLB change if mmu is enabled.  */
26208577f354SRichard Henderson     if (ctx->tb_flags & PSW_C) {
26218577f354SRichard Henderson         ctx->base.is_jmp = DISAS_IAQ_N_STALE;
26228577f354SRichard Henderson     }
26238577f354SRichard Henderson     return nullify_end(ctx);
26248577f354SRichard Henderson #endif
26258577f354SRichard Henderson }
26268577f354SRichard Henderson 
2627deee69a1SRichard Henderson static bool trans_lpa(DisasContext *ctx, arg_ldst *a)
26282dfcca9fSRichard Henderson {
2629deee69a1SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
2630deee69a1SRichard Henderson #ifndef CONFIG_USER_ONLY
26316fd0c7bcSRichard Henderson     TCGv_i64 vaddr;
26326fd0c7bcSRichard Henderson     TCGv_i64 ofs, paddr;
26332dfcca9fSRichard Henderson 
26342dfcca9fSRichard Henderson     nullify_over(ctx);
26352dfcca9fSRichard Henderson 
2636deee69a1SRichard Henderson     form_gva(ctx, &vaddr, &ofs, a->b, a->x, 0, 0, a->sp, a->m, false);
26372dfcca9fSRichard Henderson 
2638aac0f603SRichard Henderson     paddr = tcg_temp_new_i64();
2639ad75a51eSRichard Henderson     gen_helper_lpa(paddr, tcg_env, vaddr);
26402dfcca9fSRichard Henderson 
26412dfcca9fSRichard Henderson     /* Note that physical address result overrides base modification.  */
2642deee69a1SRichard Henderson     if (a->m) {
2643deee69a1SRichard Henderson         save_gpr(ctx, a->b, ofs);
26442dfcca9fSRichard Henderson     }
2645deee69a1SRichard Henderson     save_gpr(ctx, a->t, paddr);
26462dfcca9fSRichard Henderson 
264731234768SRichard Henderson     return nullify_end(ctx);
2648deee69a1SRichard Henderson #endif
26492dfcca9fSRichard Henderson }
265043a97b81SRichard Henderson 
2651deee69a1SRichard Henderson static bool trans_lci(DisasContext *ctx, arg_lci *a)
265243a97b81SRichard Henderson {
265343a97b81SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
265443a97b81SRichard Henderson 
265543a97b81SRichard Henderson     /* The Coherence Index is an implementation-defined function of the
265643a97b81SRichard Henderson        physical address.  Two addresses with the same CI have a coherent
265743a97b81SRichard Henderson        view of the cache.  Our implementation is to return 0 for all,
265843a97b81SRichard Henderson        since the entire address space is coherent.  */
2659a4db4a78SRichard Henderson     save_gpr(ctx, a->t, ctx->zero);
266043a97b81SRichard Henderson 
266131234768SRichard Henderson     cond_free(&ctx->null_cond);
266231234768SRichard Henderson     return true;
266343a97b81SRichard Henderson }
266498a9cb79SRichard Henderson 
2665faf97ba1SRichard Henderson static bool trans_add(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2666b2167459SRichard Henderson {
26670c982a28SRichard Henderson     return do_add_reg(ctx, a, false, false, false, false);
2668b2167459SRichard Henderson }
2669b2167459SRichard Henderson 
2670faf97ba1SRichard Henderson static bool trans_add_l(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2671b2167459SRichard Henderson {
26720c982a28SRichard Henderson     return do_add_reg(ctx, a, true, false, false, false);
2673b2167459SRichard Henderson }
2674b2167459SRichard Henderson 
2675faf97ba1SRichard Henderson static bool trans_add_tsv(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2676b2167459SRichard Henderson {
26770c982a28SRichard Henderson     return do_add_reg(ctx, a, false, true, false, false);
2678b2167459SRichard Henderson }
2679b2167459SRichard Henderson 
2680faf97ba1SRichard Henderson static bool trans_add_c(DisasContext *ctx, arg_rrr_cf_d_sh *a)
2681b2167459SRichard Henderson {
26820c982a28SRichard Henderson     return do_add_reg(ctx, a, false, false, false, true);
26830c982a28SRichard Henderson }
2684b2167459SRichard Henderson 
2685faf97ba1SRichard Henderson static bool trans_add_c_tsv(DisasContext *ctx, arg_rrr_cf_d_sh *a)
26860c982a28SRichard Henderson {
26870c982a28SRichard Henderson     return do_add_reg(ctx, a, false, true, false, true);
26880c982a28SRichard Henderson }
26890c982a28SRichard Henderson 
269063c427c6SRichard Henderson static bool trans_sub(DisasContext *ctx, arg_rrr_cf_d *a)
26910c982a28SRichard Henderson {
26920c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, false, false);
26930c982a28SRichard Henderson }
26940c982a28SRichard Henderson 
269563c427c6SRichard Henderson static bool trans_sub_tsv(DisasContext *ctx, arg_rrr_cf_d *a)
26960c982a28SRichard Henderson {
26970c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, false, false);
26980c982a28SRichard Henderson }
26990c982a28SRichard Henderson 
270063c427c6SRichard Henderson static bool trans_sub_tc(DisasContext *ctx, arg_rrr_cf_d *a)
27010c982a28SRichard Henderson {
27020c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, false, true);
27030c982a28SRichard Henderson }
27040c982a28SRichard Henderson 
270563c427c6SRichard Henderson static bool trans_sub_tsv_tc(DisasContext *ctx, arg_rrr_cf_d *a)
27060c982a28SRichard Henderson {
27070c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, false, true);
27080c982a28SRichard Henderson }
27090c982a28SRichard Henderson 
271063c427c6SRichard Henderson static bool trans_sub_b(DisasContext *ctx, arg_rrr_cf_d *a)
27110c982a28SRichard Henderson {
27120c982a28SRichard Henderson     return do_sub_reg(ctx, a, false, true, false);
27130c982a28SRichard Henderson }
27140c982a28SRichard Henderson 
271563c427c6SRichard Henderson static bool trans_sub_b_tsv(DisasContext *ctx, arg_rrr_cf_d *a)
27160c982a28SRichard Henderson {
27170c982a28SRichard Henderson     return do_sub_reg(ctx, a, true, true, false);
27180c982a28SRichard Henderson }
27190c982a28SRichard Henderson 
2720fa8e3bedSRichard Henderson static bool trans_andcm(DisasContext *ctx, arg_rrr_cf_d *a)
27210c982a28SRichard Henderson {
27226fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_andc_i64);
27230c982a28SRichard Henderson }
27240c982a28SRichard Henderson 
2725fa8e3bedSRichard Henderson static bool trans_and(DisasContext *ctx, arg_rrr_cf_d *a)
27260c982a28SRichard Henderson {
27276fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_and_i64);
27280c982a28SRichard Henderson }
27290c982a28SRichard Henderson 
2730fa8e3bedSRichard Henderson static bool trans_or(DisasContext *ctx, arg_rrr_cf_d *a)
27310c982a28SRichard Henderson {
27320c982a28SRichard Henderson     if (a->cf == 0) {
27330c982a28SRichard Henderson         unsigned r2 = a->r2;
27340c982a28SRichard Henderson         unsigned r1 = a->r1;
27350c982a28SRichard Henderson         unsigned rt = a->t;
27360c982a28SRichard Henderson 
27377aee8189SRichard Henderson         if (rt == 0) { /* NOP */
27387aee8189SRichard Henderson             cond_free(&ctx->null_cond);
27397aee8189SRichard Henderson             return true;
27407aee8189SRichard Henderson         }
27417aee8189SRichard Henderson         if (r2 == 0) { /* COPY */
2742b2167459SRichard Henderson             if (r1 == 0) {
27436fd0c7bcSRichard Henderson                 TCGv_i64 dest = dest_gpr(ctx, rt);
27446fd0c7bcSRichard Henderson                 tcg_gen_movi_i64(dest, 0);
2745b2167459SRichard Henderson                 save_gpr(ctx, rt, dest);
2746b2167459SRichard Henderson             } else {
2747b2167459SRichard Henderson                 save_gpr(ctx, rt, cpu_gr[r1]);
2748b2167459SRichard Henderson             }
2749b2167459SRichard Henderson             cond_free(&ctx->null_cond);
275031234768SRichard Henderson             return true;
2751b2167459SRichard Henderson         }
27527aee8189SRichard Henderson #ifndef CONFIG_USER_ONLY
27537aee8189SRichard Henderson         /* These are QEMU extensions and are nops in the real architecture:
27547aee8189SRichard Henderson          *
27557aee8189SRichard Henderson          * or %r10,%r10,%r10 -- idle loop; wait for interrupt
27567aee8189SRichard Henderson          * or %r31,%r31,%r31 -- death loop; offline cpu
27577aee8189SRichard Henderson          *                      currently implemented as idle.
27587aee8189SRichard Henderson          */
27597aee8189SRichard Henderson         if ((rt == 10 || rt == 31) && r1 == rt && r2 == rt) { /* PAUSE */
27607aee8189SRichard Henderson             /* No need to check for supervisor, as userland can only pause
27617aee8189SRichard Henderson                until the next timer interrupt.  */
27627aee8189SRichard Henderson             nullify_over(ctx);
27637aee8189SRichard Henderson 
27647aee8189SRichard Henderson             /* Advance the instruction queue.  */
276585e6cda0SRichard Henderson             install_iaq_entries(ctx, ctx->iaoq_b, cpu_iaoq_b,
276685e6cda0SRichard Henderson                                 ctx->iaoq_n, ctx->iaoq_n_var);
27677aee8189SRichard Henderson             nullify_set(ctx, 0);
27687aee8189SRichard Henderson 
27697aee8189SRichard Henderson             /* Tell the qemu main loop to halt until this cpu has work.  */
2770ad75a51eSRichard Henderson             tcg_gen_st_i32(tcg_constant_i32(1), tcg_env,
277129dd6f64SRichard Henderson                            offsetof(CPUState, halted) - offsetof(HPPACPU, env));
27727aee8189SRichard Henderson             gen_excp_1(EXCP_HALTED);
27737aee8189SRichard Henderson             ctx->base.is_jmp = DISAS_NORETURN;
27747aee8189SRichard Henderson 
27757aee8189SRichard Henderson             return nullify_end(ctx);
27767aee8189SRichard Henderson         }
27777aee8189SRichard Henderson #endif
27787aee8189SRichard Henderson     }
27796fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_or_i64);
27807aee8189SRichard Henderson }
2781b2167459SRichard Henderson 
2782fa8e3bedSRichard Henderson static bool trans_xor(DisasContext *ctx, arg_rrr_cf_d *a)
2783b2167459SRichard Henderson {
27846fd0c7bcSRichard Henderson     return do_log_reg(ctx, a, tcg_gen_xor_i64);
27850c982a28SRichard Henderson }
27860c982a28SRichard Henderson 
2787345aa35fSRichard Henderson static bool trans_cmpclr(DisasContext *ctx, arg_rrr_cf_d *a)
27880c982a28SRichard Henderson {
27896fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2;
2790b2167459SRichard Henderson 
27910c982a28SRichard Henderson     if (a->cf) {
2792b2167459SRichard Henderson         nullify_over(ctx);
2793b2167459SRichard Henderson     }
27940c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
27950c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
2796345aa35fSRichard Henderson     do_cmpclr(ctx, a->t, tcg_r1, tcg_r2, a->cf, a->d);
279731234768SRichard Henderson     return nullify_end(ctx);
2798b2167459SRichard Henderson }
2799b2167459SRichard Henderson 
2800af240753SRichard Henderson static bool trans_uxor(DisasContext *ctx, arg_rrr_cf_d *a)
2801b2167459SRichard Henderson {
280246bb3d46SRichard Henderson     TCGv_i64 tcg_r1, tcg_r2, dest;
2803b2167459SRichard Henderson 
28040c982a28SRichard Henderson     if (a->cf) {
2805b2167459SRichard Henderson         nullify_over(ctx);
2806b2167459SRichard Henderson     }
280746bb3d46SRichard Henderson 
28080c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
28090c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
281046bb3d46SRichard Henderson     dest = dest_gpr(ctx, a->t);
281146bb3d46SRichard Henderson 
281246bb3d46SRichard Henderson     tcg_gen_xor_i64(dest, tcg_r1, tcg_r2);
281346bb3d46SRichard Henderson     save_gpr(ctx, a->t, dest);
281446bb3d46SRichard Henderson 
281546bb3d46SRichard Henderson     cond_free(&ctx->null_cond);
281646bb3d46SRichard Henderson     if (a->cf) {
281746bb3d46SRichard Henderson         ctx->null_cond = do_unit_zero_cond(a->cf, a->d, dest);
281846bb3d46SRichard Henderson     }
281946bb3d46SRichard Henderson 
282031234768SRichard Henderson     return nullify_end(ctx);
2821b2167459SRichard Henderson }
2822b2167459SRichard Henderson 
2823af240753SRichard Henderson static bool do_uaddcm(DisasContext *ctx, arg_rrr_cf_d *a, bool is_tc)
2824b2167459SRichard Henderson {
28256fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1, tcg_r2, tmp;
2826b2167459SRichard Henderson 
2827ababac16SRichard Henderson     if (a->cf == 0) {
2828ababac16SRichard Henderson         tcg_r2 = load_gpr(ctx, a->r2);
2829ababac16SRichard Henderson         tmp = dest_gpr(ctx, a->t);
2830ababac16SRichard Henderson 
2831ababac16SRichard Henderson         if (a->r1 == 0) {
2832ababac16SRichard Henderson             /* UADDCM r0,src,dst is the common idiom for dst = ~src. */
2833ababac16SRichard Henderson             tcg_gen_not_i64(tmp, tcg_r2);
2834ababac16SRichard Henderson         } else {
2835ababac16SRichard Henderson             /*
2836ababac16SRichard Henderson              * Recall that r1 - r2 == r1 + ~r2 + 1.
2837ababac16SRichard Henderson              * Thus r1 + ~r2 == r1 - r2 - 1,
2838ababac16SRichard Henderson              * which does not require an extra temporary.
2839ababac16SRichard Henderson              */
2840ababac16SRichard Henderson             tcg_r1 = load_gpr(ctx, a->r1);
2841ababac16SRichard Henderson             tcg_gen_sub_i64(tmp, tcg_r1, tcg_r2);
2842ababac16SRichard Henderson             tcg_gen_subi_i64(tmp, tmp, 1);
2843b2167459SRichard Henderson         }
2844ababac16SRichard Henderson         save_gpr(ctx, a->t, tmp);
2845ababac16SRichard Henderson         cond_free(&ctx->null_cond);
2846ababac16SRichard Henderson         return true;
2847ababac16SRichard Henderson     }
2848ababac16SRichard Henderson 
2849ababac16SRichard Henderson     nullify_over(ctx);
28500c982a28SRichard Henderson     tcg_r1 = load_gpr(ctx, a->r1);
28510c982a28SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r2);
2852aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
28536fd0c7bcSRichard Henderson     tcg_gen_not_i64(tmp, tcg_r2);
285446bb3d46SRichard Henderson     do_unit_addsub(ctx, a->t, tcg_r1, tmp, a->cf, a->d, is_tc, true);
285531234768SRichard Henderson     return nullify_end(ctx);
2856b2167459SRichard Henderson }
2857b2167459SRichard Henderson 
2858af240753SRichard Henderson static bool trans_uaddcm(DisasContext *ctx, arg_rrr_cf_d *a)
2859b2167459SRichard Henderson {
28600c982a28SRichard Henderson     return do_uaddcm(ctx, a, false);
28610c982a28SRichard Henderson }
28620c982a28SRichard Henderson 
2863af240753SRichard Henderson static bool trans_uaddcm_tc(DisasContext *ctx, arg_rrr_cf_d *a)
28640c982a28SRichard Henderson {
28650c982a28SRichard Henderson     return do_uaddcm(ctx, a, true);
28660c982a28SRichard Henderson }
28670c982a28SRichard Henderson 
2868af240753SRichard Henderson static bool do_dcor(DisasContext *ctx, arg_rr_cf_d *a, bool is_i)
28690c982a28SRichard Henderson {
28706fd0c7bcSRichard Henderson     TCGv_i64 tmp;
2871b2167459SRichard Henderson 
2872b2167459SRichard Henderson     nullify_over(ctx);
2873b2167459SRichard Henderson 
2874aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
2875d0ae87a2SRichard Henderson     tcg_gen_extract2_i64(tmp, cpu_psw_cb, cpu_psw_cb_msb, 4);
2876b2167459SRichard Henderson     if (!is_i) {
28776fd0c7bcSRichard Henderson         tcg_gen_not_i64(tmp, tmp);
2878b2167459SRichard Henderson     }
28796fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, tmp, (uint64_t)0x1111111111111111ull);
28806fd0c7bcSRichard Henderson     tcg_gen_muli_i64(tmp, tmp, 6);
288146bb3d46SRichard Henderson     do_unit_addsub(ctx, a->t, load_gpr(ctx, a->r), tmp,
288246bb3d46SRichard Henderson                    a->cf, a->d, false, is_i);
288331234768SRichard Henderson     return nullify_end(ctx);
2884b2167459SRichard Henderson }
2885b2167459SRichard Henderson 
2886af240753SRichard Henderson static bool trans_dcor(DisasContext *ctx, arg_rr_cf_d *a)
2887b2167459SRichard Henderson {
28880c982a28SRichard Henderson     return do_dcor(ctx, a, false);
28890c982a28SRichard Henderson }
28900c982a28SRichard Henderson 
2891af240753SRichard Henderson static bool trans_dcor_i(DisasContext *ctx, arg_rr_cf_d *a)
28920c982a28SRichard Henderson {
28930c982a28SRichard Henderson     return do_dcor(ctx, a, true);
28940c982a28SRichard Henderson }
28950c982a28SRichard Henderson 
28960c982a28SRichard Henderson static bool trans_ds(DisasContext *ctx, arg_rrr_cf *a)
28970c982a28SRichard Henderson {
2898a4db4a78SRichard Henderson     TCGv_i64 dest, add1, add2, addc, in1, in2;
2899b2167459SRichard Henderson 
2900b2167459SRichard Henderson     nullify_over(ctx);
2901b2167459SRichard Henderson 
29020c982a28SRichard Henderson     in1 = load_gpr(ctx, a->r1);
29030c982a28SRichard Henderson     in2 = load_gpr(ctx, a->r2);
2904b2167459SRichard Henderson 
2905aac0f603SRichard Henderson     add1 = tcg_temp_new_i64();
2906aac0f603SRichard Henderson     add2 = tcg_temp_new_i64();
2907aac0f603SRichard Henderson     addc = tcg_temp_new_i64();
2908aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
2909b2167459SRichard Henderson 
2910b2167459SRichard Henderson     /* Form R1 << 1 | PSW[CB]{8}.  */
29116fd0c7bcSRichard Henderson     tcg_gen_add_i64(add1, in1, in1);
29126fd0c7bcSRichard Henderson     tcg_gen_add_i64(add1, add1, get_psw_carry(ctx, false));
2913b2167459SRichard Henderson 
291472ca8753SRichard Henderson     /*
291572ca8753SRichard Henderson      * Add or subtract R2, depending on PSW[V].  Proper computation of
291672ca8753SRichard Henderson      * carry requires that we subtract via + ~R2 + 1, as described in
291772ca8753SRichard Henderson      * the manual.  By extracting and masking V, we can produce the
291872ca8753SRichard Henderson      * proper inputs to the addition without movcond.
291972ca8753SRichard Henderson      */
29206fd0c7bcSRichard Henderson     tcg_gen_sextract_i64(addc, cpu_psw_v, 31, 1);
29216fd0c7bcSRichard Henderson     tcg_gen_xor_i64(add2, in2, addc);
29226fd0c7bcSRichard Henderson     tcg_gen_andi_i64(addc, addc, 1);
292372ca8753SRichard Henderson 
2924a4db4a78SRichard Henderson     tcg_gen_add2_i64(dest, cpu_psw_cb_msb, add1, ctx->zero, add2, ctx->zero);
2925a4db4a78SRichard Henderson     tcg_gen_add2_i64(dest, cpu_psw_cb_msb, dest, cpu_psw_cb_msb,
2926a4db4a78SRichard Henderson                      addc, ctx->zero);
2927b2167459SRichard Henderson 
2928b2167459SRichard Henderson     /* Write back the result register.  */
29290c982a28SRichard Henderson     save_gpr(ctx, a->t, dest);
2930b2167459SRichard Henderson 
2931b2167459SRichard Henderson     /* Write back PSW[CB].  */
29326fd0c7bcSRichard Henderson     tcg_gen_xor_i64(cpu_psw_cb, add1, add2);
29336fd0c7bcSRichard Henderson     tcg_gen_xor_i64(cpu_psw_cb, cpu_psw_cb, dest);
2934b2167459SRichard Henderson 
2935f8f5986eSRichard Henderson     /*
2936f8f5986eSRichard Henderson      * Write back PSW[V] for the division step.
2937f8f5986eSRichard Henderson      * Shift cb{8} from where it lives in bit 32 to bit 31,
2938f8f5986eSRichard Henderson      * so that it overlaps r2{32} in bit 31.
2939f8f5986eSRichard Henderson      */
2940f8f5986eSRichard Henderson     tcg_gen_shri_i64(cpu_psw_v, cpu_psw_cb, 1);
29416fd0c7bcSRichard Henderson     tcg_gen_xor_i64(cpu_psw_v, cpu_psw_v, in2);
2942b2167459SRichard Henderson 
2943b2167459SRichard Henderson     /* Install the new nullification.  */
29440c982a28SRichard Henderson     if (a->cf) {
2945f8f5986eSRichard Henderson         TCGv_i64 sv = NULL, uv = NULL;
2946b47a4a02SSven Schnelle         if (cond_need_sv(a->cf >> 1)) {
2947f8f5986eSRichard Henderson             sv = do_add_sv(ctx, dest, add1, add2, in1, 1, false);
2948f8f5986eSRichard Henderson         } else if (cond_need_cb(a->cf >> 1)) {
2949f8f5986eSRichard Henderson             uv = do_add_uv(ctx, cpu_psw_cb, NULL, in1, 1, false);
2950b2167459SRichard Henderson         }
2951f8f5986eSRichard Henderson         ctx->null_cond = do_cond(ctx, a->cf, false, dest, uv, sv);
2952b2167459SRichard Henderson     }
2953b2167459SRichard Henderson 
295431234768SRichard Henderson     return nullify_end(ctx);
2955b2167459SRichard Henderson }
2956b2167459SRichard Henderson 
29570588e061SRichard Henderson static bool trans_addi(DisasContext *ctx, arg_rri_cf *a)
2958b2167459SRichard Henderson {
29590588e061SRichard Henderson     return do_add_imm(ctx, a, false, false);
29600588e061SRichard Henderson }
29610588e061SRichard Henderson 
29620588e061SRichard Henderson static bool trans_addi_tsv(DisasContext *ctx, arg_rri_cf *a)
29630588e061SRichard Henderson {
29640588e061SRichard Henderson     return do_add_imm(ctx, a, true, false);
29650588e061SRichard Henderson }
29660588e061SRichard Henderson 
29670588e061SRichard Henderson static bool trans_addi_tc(DisasContext *ctx, arg_rri_cf *a)
29680588e061SRichard Henderson {
29690588e061SRichard Henderson     return do_add_imm(ctx, a, false, true);
29700588e061SRichard Henderson }
29710588e061SRichard Henderson 
29720588e061SRichard Henderson static bool trans_addi_tc_tsv(DisasContext *ctx, arg_rri_cf *a)
29730588e061SRichard Henderson {
29740588e061SRichard Henderson     return do_add_imm(ctx, a, true, true);
29750588e061SRichard Henderson }
29760588e061SRichard Henderson 
29770588e061SRichard Henderson static bool trans_subi(DisasContext *ctx, arg_rri_cf *a)
29780588e061SRichard Henderson {
29790588e061SRichard Henderson     return do_sub_imm(ctx, a, false);
29800588e061SRichard Henderson }
29810588e061SRichard Henderson 
29820588e061SRichard Henderson static bool trans_subi_tsv(DisasContext *ctx, arg_rri_cf *a)
29830588e061SRichard Henderson {
29840588e061SRichard Henderson     return do_sub_imm(ctx, a, true);
29850588e061SRichard Henderson }
29860588e061SRichard Henderson 
2987345aa35fSRichard Henderson static bool trans_cmpiclr(DisasContext *ctx, arg_rri_cf_d *a)
29880588e061SRichard Henderson {
29896fd0c7bcSRichard Henderson     TCGv_i64 tcg_im, tcg_r2;
2990b2167459SRichard Henderson 
29910588e061SRichard Henderson     if (a->cf) {
2992b2167459SRichard Henderson         nullify_over(ctx);
2993b2167459SRichard Henderson     }
2994b2167459SRichard Henderson 
29956fd0c7bcSRichard Henderson     tcg_im = tcg_constant_i64(a->i);
29960588e061SRichard Henderson     tcg_r2 = load_gpr(ctx, a->r);
2997345aa35fSRichard Henderson     do_cmpclr(ctx, a->t, tcg_im, tcg_r2, a->cf, a->d);
2998b2167459SRichard Henderson 
299931234768SRichard Henderson     return nullify_end(ctx);
3000b2167459SRichard Henderson }
3001b2167459SRichard Henderson 
30020843563fSRichard Henderson static bool do_multimedia(DisasContext *ctx, arg_rrr *a,
30030843563fSRichard Henderson                           void (*fn)(TCGv_i64, TCGv_i64, TCGv_i64))
30040843563fSRichard Henderson {
30050843563fSRichard Henderson     TCGv_i64 r1, r2, dest;
30060843563fSRichard Henderson 
30070843563fSRichard Henderson     if (!ctx->is_pa20) {
30080843563fSRichard Henderson         return false;
30090843563fSRichard Henderson     }
30100843563fSRichard Henderson 
30110843563fSRichard Henderson     nullify_over(ctx);
30120843563fSRichard Henderson 
30130843563fSRichard Henderson     r1 = load_gpr(ctx, a->r1);
30140843563fSRichard Henderson     r2 = load_gpr(ctx, a->r2);
30150843563fSRichard Henderson     dest = dest_gpr(ctx, a->t);
30160843563fSRichard Henderson 
30170843563fSRichard Henderson     fn(dest, r1, r2);
30180843563fSRichard Henderson     save_gpr(ctx, a->t, dest);
30190843563fSRichard Henderson 
30200843563fSRichard Henderson     return nullify_end(ctx);
30210843563fSRichard Henderson }
30220843563fSRichard Henderson 
3023151f309bSRichard Henderson static bool do_multimedia_sh(DisasContext *ctx, arg_rri *a,
3024151f309bSRichard Henderson                              void (*fn)(TCGv_i64, TCGv_i64, int64_t))
3025151f309bSRichard Henderson {
3026151f309bSRichard Henderson     TCGv_i64 r, dest;
3027151f309bSRichard Henderson 
3028151f309bSRichard Henderson     if (!ctx->is_pa20) {
3029151f309bSRichard Henderson         return false;
3030151f309bSRichard Henderson     }
3031151f309bSRichard Henderson 
3032151f309bSRichard Henderson     nullify_over(ctx);
3033151f309bSRichard Henderson 
3034151f309bSRichard Henderson     r = load_gpr(ctx, a->r);
3035151f309bSRichard Henderson     dest = dest_gpr(ctx, a->t);
3036151f309bSRichard Henderson 
3037151f309bSRichard Henderson     fn(dest, r, a->i);
3038151f309bSRichard Henderson     save_gpr(ctx, a->t, dest);
3039151f309bSRichard Henderson 
3040151f309bSRichard Henderson     return nullify_end(ctx);
3041151f309bSRichard Henderson }
3042151f309bSRichard Henderson 
30433bbb8e48SRichard Henderson static bool do_multimedia_shadd(DisasContext *ctx, arg_rrr_sh *a,
30443bbb8e48SRichard Henderson                                 void (*fn)(TCGv_i64, TCGv_i64,
30453bbb8e48SRichard Henderson                                            TCGv_i64, TCGv_i32))
30463bbb8e48SRichard Henderson {
30473bbb8e48SRichard Henderson     TCGv_i64 r1, r2, dest;
30483bbb8e48SRichard Henderson 
30493bbb8e48SRichard Henderson     if (!ctx->is_pa20) {
30503bbb8e48SRichard Henderson         return false;
30513bbb8e48SRichard Henderson     }
30523bbb8e48SRichard Henderson 
30533bbb8e48SRichard Henderson     nullify_over(ctx);
30543bbb8e48SRichard Henderson 
30553bbb8e48SRichard Henderson     r1 = load_gpr(ctx, a->r1);
30563bbb8e48SRichard Henderson     r2 = load_gpr(ctx, a->r2);
30573bbb8e48SRichard Henderson     dest = dest_gpr(ctx, a->t);
30583bbb8e48SRichard Henderson 
30593bbb8e48SRichard Henderson     fn(dest, r1, r2, tcg_constant_i32(a->sh));
30603bbb8e48SRichard Henderson     save_gpr(ctx, a->t, dest);
30613bbb8e48SRichard Henderson 
30623bbb8e48SRichard Henderson     return nullify_end(ctx);
30633bbb8e48SRichard Henderson }
30643bbb8e48SRichard Henderson 
30650843563fSRichard Henderson static bool trans_hadd(DisasContext *ctx, arg_rrr *a)
30660843563fSRichard Henderson {
30670843563fSRichard Henderson     return do_multimedia(ctx, a, tcg_gen_vec_add16_i64);
30680843563fSRichard Henderson }
30690843563fSRichard Henderson 
30700843563fSRichard Henderson static bool trans_hadd_ss(DisasContext *ctx, arg_rrr *a)
30710843563fSRichard Henderson {
30720843563fSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hadd_ss);
30730843563fSRichard Henderson }
30740843563fSRichard Henderson 
30750843563fSRichard Henderson static bool trans_hadd_us(DisasContext *ctx, arg_rrr *a)
30760843563fSRichard Henderson {
30770843563fSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hadd_us);
30780843563fSRichard Henderson }
30790843563fSRichard Henderson 
30801b3cb7c8SRichard Henderson static bool trans_havg(DisasContext *ctx, arg_rrr *a)
30811b3cb7c8SRichard Henderson {
30821b3cb7c8SRichard Henderson     return do_multimedia(ctx, a, gen_helper_havg);
30831b3cb7c8SRichard Henderson }
30841b3cb7c8SRichard Henderson 
3085151f309bSRichard Henderson static bool trans_hshl(DisasContext *ctx, arg_rri *a)
3086151f309bSRichard Henderson {
3087151f309bSRichard Henderson     return do_multimedia_sh(ctx, a, tcg_gen_vec_shl16i_i64);
3088151f309bSRichard Henderson }
3089151f309bSRichard Henderson 
3090151f309bSRichard Henderson static bool trans_hshr_s(DisasContext *ctx, arg_rri *a)
3091151f309bSRichard Henderson {
3092151f309bSRichard Henderson     return do_multimedia_sh(ctx, a, tcg_gen_vec_sar16i_i64);
3093151f309bSRichard Henderson }
3094151f309bSRichard Henderson 
3095151f309bSRichard Henderson static bool trans_hshr_u(DisasContext *ctx, arg_rri *a)
3096151f309bSRichard Henderson {
3097151f309bSRichard Henderson     return do_multimedia_sh(ctx, a, tcg_gen_vec_shr16i_i64);
3098151f309bSRichard Henderson }
3099151f309bSRichard Henderson 
31003bbb8e48SRichard Henderson static bool trans_hshladd(DisasContext *ctx, arg_rrr_sh *a)
31013bbb8e48SRichard Henderson {
31023bbb8e48SRichard Henderson     return do_multimedia_shadd(ctx, a, gen_helper_hshladd);
31033bbb8e48SRichard Henderson }
31043bbb8e48SRichard Henderson 
31053bbb8e48SRichard Henderson static bool trans_hshradd(DisasContext *ctx, arg_rrr_sh *a)
31063bbb8e48SRichard Henderson {
31073bbb8e48SRichard Henderson     return do_multimedia_shadd(ctx, a, gen_helper_hshradd);
31083bbb8e48SRichard Henderson }
31093bbb8e48SRichard Henderson 
311010c9e58dSRichard Henderson static bool trans_hsub(DisasContext *ctx, arg_rrr *a)
311110c9e58dSRichard Henderson {
311210c9e58dSRichard Henderson     return do_multimedia(ctx, a, tcg_gen_vec_sub16_i64);
311310c9e58dSRichard Henderson }
311410c9e58dSRichard Henderson 
311510c9e58dSRichard Henderson static bool trans_hsub_ss(DisasContext *ctx, arg_rrr *a)
311610c9e58dSRichard Henderson {
311710c9e58dSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hsub_ss);
311810c9e58dSRichard Henderson }
311910c9e58dSRichard Henderson 
312010c9e58dSRichard Henderson static bool trans_hsub_us(DisasContext *ctx, arg_rrr *a)
312110c9e58dSRichard Henderson {
312210c9e58dSRichard Henderson     return do_multimedia(ctx, a, gen_helper_hsub_us);
312310c9e58dSRichard Henderson }
312410c9e58dSRichard Henderson 
3125c2a7ee3fSRichard Henderson static void gen_mixh_l(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3126c2a7ee3fSRichard Henderson {
3127c2a7ee3fSRichard Henderson     uint64_t mask = 0xffff0000ffff0000ull;
3128c2a7ee3fSRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3129c2a7ee3fSRichard Henderson 
3130c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(tmp, r2, mask);
3131c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(dst, r1, mask);
3132c2a7ee3fSRichard Henderson     tcg_gen_shri_i64(tmp, tmp, 16);
3133c2a7ee3fSRichard Henderson     tcg_gen_or_i64(dst, dst, tmp);
3134c2a7ee3fSRichard Henderson }
3135c2a7ee3fSRichard Henderson 
3136c2a7ee3fSRichard Henderson static bool trans_mixh_l(DisasContext *ctx, arg_rrr *a)
3137c2a7ee3fSRichard Henderson {
3138c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixh_l);
3139c2a7ee3fSRichard Henderson }
3140c2a7ee3fSRichard Henderson 
3141c2a7ee3fSRichard Henderson static void gen_mixh_r(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3142c2a7ee3fSRichard Henderson {
3143c2a7ee3fSRichard Henderson     uint64_t mask = 0x0000ffff0000ffffull;
3144c2a7ee3fSRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3145c2a7ee3fSRichard Henderson 
3146c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(tmp, r1, mask);
3147c2a7ee3fSRichard Henderson     tcg_gen_andi_i64(dst, r2, mask);
3148c2a7ee3fSRichard Henderson     tcg_gen_shli_i64(tmp, tmp, 16);
3149c2a7ee3fSRichard Henderson     tcg_gen_or_i64(dst, dst, tmp);
3150c2a7ee3fSRichard Henderson }
3151c2a7ee3fSRichard Henderson 
3152c2a7ee3fSRichard Henderson static bool trans_mixh_r(DisasContext *ctx, arg_rrr *a)
3153c2a7ee3fSRichard Henderson {
3154c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixh_r);
3155c2a7ee3fSRichard Henderson }
3156c2a7ee3fSRichard Henderson 
3157c2a7ee3fSRichard Henderson static void gen_mixw_l(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3158c2a7ee3fSRichard Henderson {
3159c2a7ee3fSRichard Henderson     TCGv_i64 tmp = tcg_temp_new_i64();
3160c2a7ee3fSRichard Henderson 
3161c2a7ee3fSRichard Henderson     tcg_gen_shri_i64(tmp, r2, 32);
3162c2a7ee3fSRichard Henderson     tcg_gen_deposit_i64(dst, r1, tmp, 0, 32);
3163c2a7ee3fSRichard Henderson }
3164c2a7ee3fSRichard Henderson 
3165c2a7ee3fSRichard Henderson static bool trans_mixw_l(DisasContext *ctx, arg_rrr *a)
3166c2a7ee3fSRichard Henderson {
3167c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixw_l);
3168c2a7ee3fSRichard Henderson }
3169c2a7ee3fSRichard Henderson 
3170c2a7ee3fSRichard Henderson static void gen_mixw_r(TCGv_i64 dst, TCGv_i64 r1, TCGv_i64 r2)
3171c2a7ee3fSRichard Henderson {
3172c2a7ee3fSRichard Henderson     tcg_gen_deposit_i64(dst, r2, r1, 32, 32);
3173c2a7ee3fSRichard Henderson }
3174c2a7ee3fSRichard Henderson 
3175c2a7ee3fSRichard Henderson static bool trans_mixw_r(DisasContext *ctx, arg_rrr *a)
3176c2a7ee3fSRichard Henderson {
3177c2a7ee3fSRichard Henderson     return do_multimedia(ctx, a, gen_mixw_r);
3178c2a7ee3fSRichard Henderson }
3179c2a7ee3fSRichard Henderson 
31804e7abdb1SRichard Henderson static bool trans_permh(DisasContext *ctx, arg_permh *a)
31814e7abdb1SRichard Henderson {
31824e7abdb1SRichard Henderson     TCGv_i64 r, t0, t1, t2, t3;
31834e7abdb1SRichard Henderson 
31844e7abdb1SRichard Henderson     if (!ctx->is_pa20) {
31854e7abdb1SRichard Henderson         return false;
31864e7abdb1SRichard Henderson     }
31874e7abdb1SRichard Henderson 
31884e7abdb1SRichard Henderson     nullify_over(ctx);
31894e7abdb1SRichard Henderson 
31904e7abdb1SRichard Henderson     r = load_gpr(ctx, a->r1);
31914e7abdb1SRichard Henderson     t0 = tcg_temp_new_i64();
31924e7abdb1SRichard Henderson     t1 = tcg_temp_new_i64();
31934e7abdb1SRichard Henderson     t2 = tcg_temp_new_i64();
31944e7abdb1SRichard Henderson     t3 = tcg_temp_new_i64();
31954e7abdb1SRichard Henderson 
31964e7abdb1SRichard Henderson     tcg_gen_extract_i64(t0, r, (3 - a->c0) * 16, 16);
31974e7abdb1SRichard Henderson     tcg_gen_extract_i64(t1, r, (3 - a->c1) * 16, 16);
31984e7abdb1SRichard Henderson     tcg_gen_extract_i64(t2, r, (3 - a->c2) * 16, 16);
31994e7abdb1SRichard Henderson     tcg_gen_extract_i64(t3, r, (3 - a->c3) * 16, 16);
32004e7abdb1SRichard Henderson 
32014e7abdb1SRichard Henderson     tcg_gen_deposit_i64(t0, t1, t0, 16, 48);
32024e7abdb1SRichard Henderson     tcg_gen_deposit_i64(t2, t3, t2, 16, 48);
32034e7abdb1SRichard Henderson     tcg_gen_deposit_i64(t0, t2, t0, 32, 32);
32044e7abdb1SRichard Henderson 
32054e7abdb1SRichard Henderson     save_gpr(ctx, a->t, t0);
32064e7abdb1SRichard Henderson     return nullify_end(ctx);
32074e7abdb1SRichard Henderson }
32084e7abdb1SRichard Henderson 
32091cd012a5SRichard Henderson static bool trans_ld(DisasContext *ctx, arg_ldst *a)
321096d6407fSRichard Henderson {
3211b5caa17cSRichard Henderson     if (ctx->is_pa20) {
3212b5caa17cSRichard Henderson        /*
3213b5caa17cSRichard Henderson         * With pa20, LDB, LDH, LDW, LDD to %g0 are prefetches.
3214b5caa17cSRichard Henderson         * Any base modification still occurs.
3215b5caa17cSRichard Henderson         */
3216b5caa17cSRichard Henderson         if (a->t == 0) {
3217b5caa17cSRichard Henderson             return trans_nop_addrx(ctx, a);
3218b5caa17cSRichard Henderson         }
3219b5caa17cSRichard Henderson     } else if (a->size > MO_32) {
32200786a3b6SHelge Deller         return gen_illegal(ctx);
3221c53e401eSRichard Henderson     }
32221cd012a5SRichard Henderson     return do_load(ctx, a->t, a->b, a->x, a->scale ? a->size : 0,
32231cd012a5SRichard Henderson                    a->disp, a->sp, a->m, a->size | MO_TE);
322496d6407fSRichard Henderson }
322596d6407fSRichard Henderson 
32261cd012a5SRichard Henderson static bool trans_st(DisasContext *ctx, arg_ldst *a)
322796d6407fSRichard Henderson {
32281cd012a5SRichard Henderson     assert(a->x == 0 && a->scale == 0);
3229c53e401eSRichard Henderson     if (!ctx->is_pa20 && a->size > MO_32) {
32300786a3b6SHelge Deller         return gen_illegal(ctx);
323196d6407fSRichard Henderson     }
3232c53e401eSRichard Henderson     return do_store(ctx, a->t, a->b, a->disp, a->sp, a->m, a->size | MO_TE);
32330786a3b6SHelge Deller }
323496d6407fSRichard Henderson 
32351cd012a5SRichard Henderson static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
323696d6407fSRichard Henderson {
3237b1af755cSRichard Henderson     MemOp mop = MO_TE | MO_ALIGN | a->size;
3238a4db4a78SRichard Henderson     TCGv_i64 dest, ofs;
32396fd0c7bcSRichard Henderson     TCGv_i64 addr;
324096d6407fSRichard Henderson 
3241c53e401eSRichard Henderson     if (!ctx->is_pa20 && a->size > MO_32) {
324251416c4eSRichard Henderson         return gen_illegal(ctx);
324351416c4eSRichard Henderson     }
324451416c4eSRichard Henderson 
324596d6407fSRichard Henderson     nullify_over(ctx);
324696d6407fSRichard Henderson 
32471cd012a5SRichard Henderson     if (a->m) {
324886f8d05fSRichard Henderson         /* Base register modification.  Make sure if RT == RB,
324986f8d05fSRichard Henderson            we see the result of the load.  */
3250aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
325196d6407fSRichard Henderson     } else {
32521cd012a5SRichard Henderson         dest = dest_gpr(ctx, a->t);
325396d6407fSRichard Henderson     }
325496d6407fSRichard Henderson 
3255c3ea1996SSven Schnelle     form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? 3 : 0,
325617fe594cSRichard Henderson              a->disp, a->sp, a->m, MMU_DISABLED(ctx));
3257b1af755cSRichard Henderson 
3258b1af755cSRichard Henderson     /*
3259b1af755cSRichard Henderson      * For hppa1.1, LDCW is undefined unless aligned mod 16.
3260b1af755cSRichard Henderson      * However actual hardware succeeds with aligned mod 4.
3261b1af755cSRichard Henderson      * Detect this case and log a GUEST_ERROR.
3262b1af755cSRichard Henderson      *
3263b1af755cSRichard Henderson      * TODO: HPPA64 relaxes the over-alignment requirement
3264b1af755cSRichard Henderson      * with the ,co completer.
3265b1af755cSRichard Henderson      */
3266b1af755cSRichard Henderson     gen_helper_ldc_check(addr);
3267b1af755cSRichard Henderson 
3268a4db4a78SRichard Henderson     tcg_gen_atomic_xchg_i64(dest, addr, ctx->zero, ctx->mmu_idx, mop);
3269b1af755cSRichard Henderson 
32701cd012a5SRichard Henderson     if (a->m) {
32711cd012a5SRichard Henderson         save_gpr(ctx, a->b, ofs);
327296d6407fSRichard Henderson     }
32731cd012a5SRichard Henderson     save_gpr(ctx, a->t, dest);
327496d6407fSRichard Henderson 
327531234768SRichard Henderson     return nullify_end(ctx);
327696d6407fSRichard Henderson }
327796d6407fSRichard Henderson 
32781cd012a5SRichard Henderson static bool trans_stby(DisasContext *ctx, arg_stby *a)
327996d6407fSRichard Henderson {
32806fd0c7bcSRichard Henderson     TCGv_i64 ofs, val;
32816fd0c7bcSRichard Henderson     TCGv_i64 addr;
328296d6407fSRichard Henderson 
328396d6407fSRichard Henderson     nullify_over(ctx);
328496d6407fSRichard Henderson 
32851cd012a5SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
328617fe594cSRichard Henderson              MMU_DISABLED(ctx));
32871cd012a5SRichard Henderson     val = load_gpr(ctx, a->r);
32881cd012a5SRichard Henderson     if (a->a) {
3289f9f46db4SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3290ad75a51eSRichard Henderson             gen_helper_stby_e_parallel(tcg_env, addr, val);
3291f9f46db4SEmilio G. Cota         } else {
3292ad75a51eSRichard Henderson             gen_helper_stby_e(tcg_env, addr, val);
3293f9f46db4SEmilio G. Cota         }
3294f9f46db4SEmilio G. Cota     } else {
3295f9f46db4SEmilio G. Cota         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3296ad75a51eSRichard Henderson             gen_helper_stby_b_parallel(tcg_env, addr, val);
329796d6407fSRichard Henderson         } else {
3298ad75a51eSRichard Henderson             gen_helper_stby_b(tcg_env, addr, val);
329996d6407fSRichard Henderson         }
3300f9f46db4SEmilio G. Cota     }
33011cd012a5SRichard Henderson     if (a->m) {
33026fd0c7bcSRichard Henderson         tcg_gen_andi_i64(ofs, ofs, ~3);
33031cd012a5SRichard Henderson         save_gpr(ctx, a->b, ofs);
330496d6407fSRichard Henderson     }
330596d6407fSRichard Henderson 
330631234768SRichard Henderson     return nullify_end(ctx);
330796d6407fSRichard Henderson }
330896d6407fSRichard Henderson 
330925460fc5SRichard Henderson static bool trans_stdby(DisasContext *ctx, arg_stby *a)
331025460fc5SRichard Henderson {
33116fd0c7bcSRichard Henderson     TCGv_i64 ofs, val;
33126fd0c7bcSRichard Henderson     TCGv_i64 addr;
331325460fc5SRichard Henderson 
331425460fc5SRichard Henderson     if (!ctx->is_pa20) {
331525460fc5SRichard Henderson         return false;
331625460fc5SRichard Henderson     }
331725460fc5SRichard Henderson     nullify_over(ctx);
331825460fc5SRichard Henderson 
331925460fc5SRichard Henderson     form_gva(ctx, &addr, &ofs, a->b, 0, 0, a->disp, a->sp, a->m,
332017fe594cSRichard Henderson              MMU_DISABLED(ctx));
332125460fc5SRichard Henderson     val = load_gpr(ctx, a->r);
332225460fc5SRichard Henderson     if (a->a) {
332325460fc5SRichard Henderson         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
332425460fc5SRichard Henderson             gen_helper_stdby_e_parallel(tcg_env, addr, val);
332525460fc5SRichard Henderson         } else {
332625460fc5SRichard Henderson             gen_helper_stdby_e(tcg_env, addr, val);
332725460fc5SRichard Henderson         }
332825460fc5SRichard Henderson     } else {
332925460fc5SRichard Henderson         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
333025460fc5SRichard Henderson             gen_helper_stdby_b_parallel(tcg_env, addr, val);
333125460fc5SRichard Henderson         } else {
333225460fc5SRichard Henderson             gen_helper_stdby_b(tcg_env, addr, val);
333325460fc5SRichard Henderson         }
333425460fc5SRichard Henderson     }
333525460fc5SRichard Henderson     if (a->m) {
33366fd0c7bcSRichard Henderson         tcg_gen_andi_i64(ofs, ofs, ~7);
333725460fc5SRichard Henderson         save_gpr(ctx, a->b, ofs);
333825460fc5SRichard Henderson     }
333925460fc5SRichard Henderson 
334025460fc5SRichard Henderson     return nullify_end(ctx);
334125460fc5SRichard Henderson }
334225460fc5SRichard Henderson 
33431cd012a5SRichard Henderson static bool trans_lda(DisasContext *ctx, arg_ldst *a)
3344d0a851ccSRichard Henderson {
3345d0a851ccSRichard Henderson     int hold_mmu_idx = ctx->mmu_idx;
3346d0a851ccSRichard Henderson 
3347d0a851ccSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
3348451d993dSRichard Henderson     ctx->mmu_idx = ctx->tb_flags & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX;
33491cd012a5SRichard Henderson     trans_ld(ctx, a);
3350d0a851ccSRichard Henderson     ctx->mmu_idx = hold_mmu_idx;
335131234768SRichard Henderson     return true;
3352d0a851ccSRichard Henderson }
3353d0a851ccSRichard Henderson 
33541cd012a5SRichard Henderson static bool trans_sta(DisasContext *ctx, arg_ldst *a)
3355d0a851ccSRichard Henderson {
3356d0a851ccSRichard Henderson     int hold_mmu_idx = ctx->mmu_idx;
3357d0a851ccSRichard Henderson 
3358d0a851ccSRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
3359451d993dSRichard Henderson     ctx->mmu_idx = ctx->tb_flags & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX;
33601cd012a5SRichard Henderson     trans_st(ctx, a);
3361d0a851ccSRichard Henderson     ctx->mmu_idx = hold_mmu_idx;
336231234768SRichard Henderson     return true;
3363d0a851ccSRichard Henderson }
336495412a61SRichard Henderson 
33650588e061SRichard Henderson static bool trans_ldil(DisasContext *ctx, arg_ldil *a)
3366b2167459SRichard Henderson {
33676fd0c7bcSRichard Henderson     TCGv_i64 tcg_rt = dest_gpr(ctx, a->t);
3368b2167459SRichard Henderson 
33696fd0c7bcSRichard Henderson     tcg_gen_movi_i64(tcg_rt, a->i);
33700588e061SRichard Henderson     save_gpr(ctx, a->t, tcg_rt);
3371b2167459SRichard Henderson     cond_free(&ctx->null_cond);
337231234768SRichard Henderson     return true;
3373b2167459SRichard Henderson }
3374b2167459SRichard Henderson 
33750588e061SRichard Henderson static bool trans_addil(DisasContext *ctx, arg_addil *a)
3376b2167459SRichard Henderson {
33776fd0c7bcSRichard Henderson     TCGv_i64 tcg_rt = load_gpr(ctx, a->r);
33786fd0c7bcSRichard Henderson     TCGv_i64 tcg_r1 = dest_gpr(ctx, 1);
3379b2167459SRichard Henderson 
33806fd0c7bcSRichard Henderson     tcg_gen_addi_i64(tcg_r1, tcg_rt, a->i);
3381b2167459SRichard Henderson     save_gpr(ctx, 1, tcg_r1);
3382b2167459SRichard Henderson     cond_free(&ctx->null_cond);
338331234768SRichard Henderson     return true;
3384b2167459SRichard Henderson }
3385b2167459SRichard Henderson 
33860588e061SRichard Henderson static bool trans_ldo(DisasContext *ctx, arg_ldo *a)
3387b2167459SRichard Henderson {
33886fd0c7bcSRichard Henderson     TCGv_i64 tcg_rt = dest_gpr(ctx, a->t);
3389b2167459SRichard Henderson 
3390b2167459SRichard Henderson     /* Special case rb == 0, for the LDI pseudo-op.
3391d265360fSRichard Henderson        The COPY pseudo-op is handled for free within tcg_gen_addi_i64.  */
33920588e061SRichard Henderson     if (a->b == 0) {
33936fd0c7bcSRichard Henderson         tcg_gen_movi_i64(tcg_rt, a->i);
3394b2167459SRichard Henderson     } else {
33956fd0c7bcSRichard Henderson         tcg_gen_addi_i64(tcg_rt, cpu_gr[a->b], a->i);
3396b2167459SRichard Henderson     }
33970588e061SRichard Henderson     save_gpr(ctx, a->t, tcg_rt);
3398b2167459SRichard Henderson     cond_free(&ctx->null_cond);
339931234768SRichard Henderson     return true;
3400b2167459SRichard Henderson }
3401b2167459SRichard Henderson 
34026fd0c7bcSRichard Henderson static bool do_cmpb(DisasContext *ctx, unsigned r, TCGv_i64 in1,
3403e9efd4bcSRichard Henderson                     unsigned c, unsigned f, bool d, unsigned n, int disp)
340498cd9ca7SRichard Henderson {
34056fd0c7bcSRichard Henderson     TCGv_i64 dest, in2, sv;
340698cd9ca7SRichard Henderson     DisasCond cond;
340798cd9ca7SRichard Henderson 
340898cd9ca7SRichard Henderson     in2 = load_gpr(ctx, r);
3409aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
341098cd9ca7SRichard Henderson 
34116fd0c7bcSRichard Henderson     tcg_gen_sub_i64(dest, in1, in2);
341298cd9ca7SRichard Henderson 
3413f764718dSRichard Henderson     sv = NULL;
3414b47a4a02SSven Schnelle     if (cond_need_sv(c)) {
341598cd9ca7SRichard Henderson         sv = do_sub_sv(ctx, dest, in1, in2);
341698cd9ca7SRichard Henderson     }
341798cd9ca7SRichard Henderson 
34184fe9533aSRichard Henderson     cond = do_sub_cond(ctx, c * 2 + f, d, dest, in1, in2, sv);
341901afb7beSRichard Henderson     return do_cbranch(ctx, disp, n, &cond);
342098cd9ca7SRichard Henderson }
342198cd9ca7SRichard Henderson 
342201afb7beSRichard Henderson static bool trans_cmpb(DisasContext *ctx, arg_cmpb *a)
342398cd9ca7SRichard Henderson {
3424e9efd4bcSRichard Henderson     if (!ctx->is_pa20 && a->d) {
3425e9efd4bcSRichard Henderson         return false;
3426e9efd4bcSRichard Henderson     }
342701afb7beSRichard Henderson     nullify_over(ctx);
3428e9efd4bcSRichard Henderson     return do_cmpb(ctx, a->r2, load_gpr(ctx, a->r1),
3429e9efd4bcSRichard Henderson                    a->c, a->f, a->d, a->n, a->disp);
343001afb7beSRichard Henderson }
343101afb7beSRichard Henderson 
343201afb7beSRichard Henderson static bool trans_cmpbi(DisasContext *ctx, arg_cmpbi *a)
343301afb7beSRichard Henderson {
3434c65c3ee1SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3435c65c3ee1SRichard Henderson         return false;
3436c65c3ee1SRichard Henderson     }
343701afb7beSRichard Henderson     nullify_over(ctx);
34386fd0c7bcSRichard Henderson     return do_cmpb(ctx, a->r, tcg_constant_i64(a->i),
3439c65c3ee1SRichard Henderson                    a->c, a->f, a->d, a->n, a->disp);
344001afb7beSRichard Henderson }
344101afb7beSRichard Henderson 
34426fd0c7bcSRichard Henderson static bool do_addb(DisasContext *ctx, unsigned r, TCGv_i64 in1,
344301afb7beSRichard Henderson                     unsigned c, unsigned f, unsigned n, int disp)
344401afb7beSRichard Henderson {
34456fd0c7bcSRichard Henderson     TCGv_i64 dest, in2, sv, cb_cond;
344698cd9ca7SRichard Henderson     DisasCond cond;
3447bdcccc17SRichard Henderson     bool d = false;
344898cd9ca7SRichard Henderson 
3449f25d3160SRichard Henderson     /*
3450f25d3160SRichard Henderson      * For hppa64, the ADDB conditions change with PSW.W,
3451f25d3160SRichard Henderson      * dropping ZNV, SV, OD in favor of double-word EQ, LT, LE.
3452f25d3160SRichard Henderson      */
3453f25d3160SRichard Henderson     if (ctx->tb_flags & PSW_W) {
3454f25d3160SRichard Henderson         d = c >= 5;
3455f25d3160SRichard Henderson         if (d) {
3456f25d3160SRichard Henderson             c &= 3;
3457f25d3160SRichard Henderson         }
3458f25d3160SRichard Henderson     }
3459f25d3160SRichard Henderson 
346098cd9ca7SRichard Henderson     in2 = load_gpr(ctx, r);
3461aac0f603SRichard Henderson     dest = tcg_temp_new_i64();
3462f764718dSRichard Henderson     sv = NULL;
3463bdcccc17SRichard Henderson     cb_cond = NULL;
346498cd9ca7SRichard Henderson 
3465b47a4a02SSven Schnelle     if (cond_need_cb(c)) {
3466aac0f603SRichard Henderson         TCGv_i64 cb = tcg_temp_new_i64();
3467aac0f603SRichard Henderson         TCGv_i64 cb_msb = tcg_temp_new_i64();
3468bdcccc17SRichard Henderson 
34696fd0c7bcSRichard Henderson         tcg_gen_movi_i64(cb_msb, 0);
34706fd0c7bcSRichard Henderson         tcg_gen_add2_i64(dest, cb_msb, in1, cb_msb, in2, cb_msb);
34716fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, in1, in2);
34726fd0c7bcSRichard Henderson         tcg_gen_xor_i64(cb, cb, dest);
3473bdcccc17SRichard Henderson         cb_cond = get_carry(ctx, d, cb, cb_msb);
3474b47a4a02SSven Schnelle     } else {
34756fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, in1, in2);
3476b47a4a02SSven Schnelle     }
3477b47a4a02SSven Schnelle     if (cond_need_sv(c)) {
3478f8f5986eSRichard Henderson         sv = do_add_sv(ctx, dest, in1, in2, in1, 0, d);
347998cd9ca7SRichard Henderson     }
348098cd9ca7SRichard Henderson 
3481a751eb31SRichard Henderson     cond = do_cond(ctx, c * 2 + f, d, dest, cb_cond, sv);
348243675d20SSven Schnelle     save_gpr(ctx, r, dest);
348301afb7beSRichard Henderson     return do_cbranch(ctx, disp, n, &cond);
348498cd9ca7SRichard Henderson }
348598cd9ca7SRichard Henderson 
348601afb7beSRichard Henderson static bool trans_addb(DisasContext *ctx, arg_addb *a)
348798cd9ca7SRichard Henderson {
348801afb7beSRichard Henderson     nullify_over(ctx);
348901afb7beSRichard Henderson     return do_addb(ctx, a->r2, load_gpr(ctx, a->r1), a->c, a->f, a->n, a->disp);
349001afb7beSRichard Henderson }
349101afb7beSRichard Henderson 
349201afb7beSRichard Henderson static bool trans_addbi(DisasContext *ctx, arg_addbi *a)
349301afb7beSRichard Henderson {
349401afb7beSRichard Henderson     nullify_over(ctx);
34956fd0c7bcSRichard Henderson     return do_addb(ctx, a->r, tcg_constant_i64(a->i), a->c, a->f, a->n, a->disp);
349601afb7beSRichard Henderson }
349701afb7beSRichard Henderson 
349801afb7beSRichard Henderson static bool trans_bb_sar(DisasContext *ctx, arg_bb_sar *a)
349901afb7beSRichard Henderson {
35006fd0c7bcSRichard Henderson     TCGv_i64 tmp, tcg_r;
350198cd9ca7SRichard Henderson     DisasCond cond;
350298cd9ca7SRichard Henderson 
350398cd9ca7SRichard Henderson     nullify_over(ctx);
350498cd9ca7SRichard Henderson 
3505aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
350601afb7beSRichard Henderson     tcg_r = load_gpr(ctx, a->r);
350782d0c831SRichard Henderson     if (a->d) {
350882d0c831SRichard Henderson         tcg_gen_shl_i64(tmp, tcg_r, cpu_sar);
350982d0c831SRichard Henderson     } else {
35101e9ab9fbSRichard Henderson         /* Force shift into [32,63] */
35116fd0c7bcSRichard Henderson         tcg_gen_ori_i64(tmp, cpu_sar, 32);
35126fd0c7bcSRichard Henderson         tcg_gen_shl_i64(tmp, tcg_r, tmp);
35131e9ab9fbSRichard Henderson     }
351498cd9ca7SRichard Henderson 
35151e9ab9fbSRichard Henderson     cond = cond_make_0_tmp(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
351601afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
351798cd9ca7SRichard Henderson }
351898cd9ca7SRichard Henderson 
351901afb7beSRichard Henderson static bool trans_bb_imm(DisasContext *ctx, arg_bb_imm *a)
352098cd9ca7SRichard Henderson {
35216fd0c7bcSRichard Henderson     TCGv_i64 tmp, tcg_r;
352201afb7beSRichard Henderson     DisasCond cond;
35231e9ab9fbSRichard Henderson     int p;
352401afb7beSRichard Henderson 
352501afb7beSRichard Henderson     nullify_over(ctx);
352601afb7beSRichard Henderson 
3527aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
352801afb7beSRichard Henderson     tcg_r = load_gpr(ctx, a->r);
352982d0c831SRichard Henderson     p = a->p | (a->d ? 0 : 32);
35306fd0c7bcSRichard Henderson     tcg_gen_shli_i64(tmp, tcg_r, p);
353101afb7beSRichard Henderson 
353201afb7beSRichard Henderson     cond = cond_make_0(a->c ? TCG_COND_GE : TCG_COND_LT, tmp);
353301afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
353401afb7beSRichard Henderson }
353501afb7beSRichard Henderson 
353601afb7beSRichard Henderson static bool trans_movb(DisasContext *ctx, arg_movb *a)
353701afb7beSRichard Henderson {
35386fd0c7bcSRichard Henderson     TCGv_i64 dest;
353998cd9ca7SRichard Henderson     DisasCond cond;
354098cd9ca7SRichard Henderson 
354198cd9ca7SRichard Henderson     nullify_over(ctx);
354298cd9ca7SRichard Henderson 
354301afb7beSRichard Henderson     dest = dest_gpr(ctx, a->r2);
354401afb7beSRichard Henderson     if (a->r1 == 0) {
35456fd0c7bcSRichard Henderson         tcg_gen_movi_i64(dest, 0);
354698cd9ca7SRichard Henderson     } else {
35476fd0c7bcSRichard Henderson         tcg_gen_mov_i64(dest, cpu_gr[a->r1]);
354898cd9ca7SRichard Henderson     }
354998cd9ca7SRichard Henderson 
35504fa52edfSRichard Henderson     /* All MOVB conditions are 32-bit. */
35514fa52edfSRichard Henderson     cond = do_sed_cond(ctx, a->c, false, dest);
355201afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
355301afb7beSRichard Henderson }
355401afb7beSRichard Henderson 
355501afb7beSRichard Henderson static bool trans_movbi(DisasContext *ctx, arg_movbi *a)
355601afb7beSRichard Henderson {
35576fd0c7bcSRichard Henderson     TCGv_i64 dest;
355801afb7beSRichard Henderson     DisasCond cond;
355901afb7beSRichard Henderson 
356001afb7beSRichard Henderson     nullify_over(ctx);
356101afb7beSRichard Henderson 
356201afb7beSRichard Henderson     dest = dest_gpr(ctx, a->r);
35636fd0c7bcSRichard Henderson     tcg_gen_movi_i64(dest, a->i);
356401afb7beSRichard Henderson 
35654fa52edfSRichard Henderson     /* All MOVBI conditions are 32-bit. */
35664fa52edfSRichard Henderson     cond = do_sed_cond(ctx, a->c, false, dest);
356701afb7beSRichard Henderson     return do_cbranch(ctx, a->disp, a->n, &cond);
356898cd9ca7SRichard Henderson }
356998cd9ca7SRichard Henderson 
3570f7b775a9SRichard Henderson static bool trans_shrp_sar(DisasContext *ctx, arg_shrp_sar *a)
35710b1347d2SRichard Henderson {
35726fd0c7bcSRichard Henderson     TCGv_i64 dest, src2;
35730b1347d2SRichard Henderson 
3574f7b775a9SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3575f7b775a9SRichard Henderson         return false;
3576f7b775a9SRichard Henderson     }
357730878590SRichard Henderson     if (a->c) {
35780b1347d2SRichard Henderson         nullify_over(ctx);
35790b1347d2SRichard Henderson     }
35800b1347d2SRichard Henderson 
358130878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
3582f7b775a9SRichard Henderson     src2 = load_gpr(ctx, a->r2);
358330878590SRichard Henderson     if (a->r1 == 0) {
3584f7b775a9SRichard Henderson         if (a->d) {
35856fd0c7bcSRichard Henderson             tcg_gen_shr_i64(dest, src2, cpu_sar);
3586f7b775a9SRichard Henderson         } else {
3587aac0f603SRichard Henderson             TCGv_i64 tmp = tcg_temp_new_i64();
3588f7b775a9SRichard Henderson 
35896fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(dest, src2);
35906fd0c7bcSRichard Henderson             tcg_gen_andi_i64(tmp, cpu_sar, 31);
35916fd0c7bcSRichard Henderson             tcg_gen_shr_i64(dest, dest, tmp);
3592f7b775a9SRichard Henderson         }
359330878590SRichard Henderson     } else if (a->r1 == a->r2) {
3594f7b775a9SRichard Henderson         if (a->d) {
35956fd0c7bcSRichard Henderson             tcg_gen_rotr_i64(dest, src2, cpu_sar);
3596f7b775a9SRichard Henderson         } else {
35970b1347d2SRichard Henderson             TCGv_i32 t32 = tcg_temp_new_i32();
3598e1d635e8SRichard Henderson             TCGv_i32 s32 = tcg_temp_new_i32();
3599e1d635e8SRichard Henderson 
36006fd0c7bcSRichard Henderson             tcg_gen_extrl_i64_i32(t32, src2);
36016fd0c7bcSRichard Henderson             tcg_gen_extrl_i64_i32(s32, cpu_sar);
3602f7b775a9SRichard Henderson             tcg_gen_andi_i32(s32, s32, 31);
3603e1d635e8SRichard Henderson             tcg_gen_rotr_i32(t32, t32, s32);
36046fd0c7bcSRichard Henderson             tcg_gen_extu_i32_i64(dest, t32);
3605f7b775a9SRichard Henderson         }
3606f7b775a9SRichard Henderson     } else {
36076fd0c7bcSRichard Henderson         TCGv_i64 src1 = load_gpr(ctx, a->r1);
3608f7b775a9SRichard Henderson 
3609f7b775a9SRichard Henderson         if (a->d) {
3610aac0f603SRichard Henderson             TCGv_i64 t = tcg_temp_new_i64();
3611aac0f603SRichard Henderson             TCGv_i64 n = tcg_temp_new_i64();
3612f7b775a9SRichard Henderson 
36136fd0c7bcSRichard Henderson             tcg_gen_xori_i64(n, cpu_sar, 63);
3614a01491a2SHelge Deller             tcg_gen_shl_i64(t, src1, n);
36156fd0c7bcSRichard Henderson             tcg_gen_shli_i64(t, t, 1);
3616a01491a2SHelge Deller             tcg_gen_shr_i64(dest, src2, cpu_sar);
36176fd0c7bcSRichard Henderson             tcg_gen_or_i64(dest, dest, t);
36180b1347d2SRichard Henderson         } else {
36190b1347d2SRichard Henderson             TCGv_i64 t = tcg_temp_new_i64();
36200b1347d2SRichard Henderson             TCGv_i64 s = tcg_temp_new_i64();
36210b1347d2SRichard Henderson 
36226fd0c7bcSRichard Henderson             tcg_gen_concat32_i64(t, src2, src1);
3623967662cdSRichard Henderson             tcg_gen_andi_i64(s, cpu_sar, 31);
3624967662cdSRichard Henderson             tcg_gen_shr_i64(dest, t, s);
36250b1347d2SRichard Henderson         }
3626f7b775a9SRichard Henderson     }
362730878590SRichard Henderson     save_gpr(ctx, a->t, dest);
36280b1347d2SRichard Henderson 
36290b1347d2SRichard Henderson     /* Install the new nullification.  */
36300b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
363130878590SRichard Henderson     if (a->c) {
3632d37fad0aSSven Schnelle         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
36330b1347d2SRichard Henderson     }
363431234768SRichard Henderson     return nullify_end(ctx);
36350b1347d2SRichard Henderson }
36360b1347d2SRichard Henderson 
3637f7b775a9SRichard Henderson static bool trans_shrp_imm(DisasContext *ctx, arg_shrp_imm *a)
36380b1347d2SRichard Henderson {
3639f7b775a9SRichard Henderson     unsigned width, sa;
36406fd0c7bcSRichard Henderson     TCGv_i64 dest, t2;
36410b1347d2SRichard Henderson 
3642f7b775a9SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3643f7b775a9SRichard Henderson         return false;
3644f7b775a9SRichard Henderson     }
364530878590SRichard Henderson     if (a->c) {
36460b1347d2SRichard Henderson         nullify_over(ctx);
36470b1347d2SRichard Henderson     }
36480b1347d2SRichard Henderson 
3649f7b775a9SRichard Henderson     width = a->d ? 64 : 32;
3650f7b775a9SRichard Henderson     sa = width - 1 - a->cpos;
3651f7b775a9SRichard Henderson 
365230878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
365330878590SRichard Henderson     t2 = load_gpr(ctx, a->r2);
365405bfd4dbSRichard Henderson     if (a->r1 == 0) {
36556fd0c7bcSRichard Henderson         tcg_gen_extract_i64(dest, t2, sa, width - sa);
3656c53e401eSRichard Henderson     } else if (width == TARGET_LONG_BITS) {
36576fd0c7bcSRichard Henderson         tcg_gen_extract2_i64(dest, t2, cpu_gr[a->r1], sa);
3658f7b775a9SRichard Henderson     } else {
3659f7b775a9SRichard Henderson         assert(!a->d);
3660f7b775a9SRichard Henderson         if (a->r1 == a->r2) {
36610b1347d2SRichard Henderson             TCGv_i32 t32 = tcg_temp_new_i32();
36626fd0c7bcSRichard Henderson             tcg_gen_extrl_i64_i32(t32, t2);
36630b1347d2SRichard Henderson             tcg_gen_rotri_i32(t32, t32, sa);
36646fd0c7bcSRichard Henderson             tcg_gen_extu_i32_i64(dest, t32);
36650b1347d2SRichard Henderson         } else {
3666967662cdSRichard Henderson             tcg_gen_concat32_i64(dest, t2, cpu_gr[a->r1]);
3667967662cdSRichard Henderson             tcg_gen_extract_i64(dest, dest, sa, 32);
36680b1347d2SRichard Henderson         }
3669f7b775a9SRichard Henderson     }
367030878590SRichard Henderson     save_gpr(ctx, a->t, dest);
36710b1347d2SRichard Henderson 
36720b1347d2SRichard Henderson     /* Install the new nullification.  */
36730b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
367430878590SRichard Henderson     if (a->c) {
3675d37fad0aSSven Schnelle         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
36760b1347d2SRichard Henderson     }
367731234768SRichard Henderson     return nullify_end(ctx);
36780b1347d2SRichard Henderson }
36790b1347d2SRichard Henderson 
3680bd792da3SRichard Henderson static bool trans_extr_sar(DisasContext *ctx, arg_extr_sar *a)
36810b1347d2SRichard Henderson {
3682bd792da3SRichard Henderson     unsigned widthm1 = a->d ? 63 : 31;
36836fd0c7bcSRichard Henderson     TCGv_i64 dest, src, tmp;
36840b1347d2SRichard Henderson 
3685bd792da3SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3686bd792da3SRichard Henderson         return false;
3687bd792da3SRichard Henderson     }
368830878590SRichard Henderson     if (a->c) {
36890b1347d2SRichard Henderson         nullify_over(ctx);
36900b1347d2SRichard Henderson     }
36910b1347d2SRichard Henderson 
369230878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
369330878590SRichard Henderson     src = load_gpr(ctx, a->r);
3694aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
36950b1347d2SRichard Henderson 
36960b1347d2SRichard Henderson     /* Recall that SAR is using big-endian bit numbering.  */
36976fd0c7bcSRichard Henderson     tcg_gen_andi_i64(tmp, cpu_sar, widthm1);
36986fd0c7bcSRichard Henderson     tcg_gen_xori_i64(tmp, tmp, widthm1);
3699d781cb77SRichard Henderson 
370030878590SRichard Henderson     if (a->se) {
3701bd792da3SRichard Henderson         if (!a->d) {
37026fd0c7bcSRichard Henderson             tcg_gen_ext32s_i64(dest, src);
3703bd792da3SRichard Henderson             src = dest;
3704bd792da3SRichard Henderson         }
37056fd0c7bcSRichard Henderson         tcg_gen_sar_i64(dest, src, tmp);
37066fd0c7bcSRichard Henderson         tcg_gen_sextract_i64(dest, dest, 0, a->len);
37070b1347d2SRichard Henderson     } else {
3708bd792da3SRichard Henderson         if (!a->d) {
37096fd0c7bcSRichard Henderson             tcg_gen_ext32u_i64(dest, src);
3710bd792da3SRichard Henderson             src = dest;
3711bd792da3SRichard Henderson         }
37126fd0c7bcSRichard Henderson         tcg_gen_shr_i64(dest, src, tmp);
37136fd0c7bcSRichard Henderson         tcg_gen_extract_i64(dest, dest, 0, a->len);
37140b1347d2SRichard Henderson     }
371530878590SRichard Henderson     save_gpr(ctx, a->t, dest);
37160b1347d2SRichard Henderson 
37170b1347d2SRichard Henderson     /* Install the new nullification.  */
37180b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
371930878590SRichard Henderson     if (a->c) {
3720bd792da3SRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
37210b1347d2SRichard Henderson     }
372231234768SRichard Henderson     return nullify_end(ctx);
37230b1347d2SRichard Henderson }
37240b1347d2SRichard Henderson 
3725bd792da3SRichard Henderson static bool trans_extr_imm(DisasContext *ctx, arg_extr_imm *a)
37260b1347d2SRichard Henderson {
3727bd792da3SRichard Henderson     unsigned len, cpos, width;
37286fd0c7bcSRichard Henderson     TCGv_i64 dest, src;
37290b1347d2SRichard Henderson 
3730bd792da3SRichard Henderson     if (!ctx->is_pa20 && a->d) {
3731bd792da3SRichard Henderson         return false;
3732bd792da3SRichard Henderson     }
373330878590SRichard Henderson     if (a->c) {
37340b1347d2SRichard Henderson         nullify_over(ctx);
37350b1347d2SRichard Henderson     }
37360b1347d2SRichard Henderson 
3737bd792da3SRichard Henderson     len = a->len;
3738bd792da3SRichard Henderson     width = a->d ? 64 : 32;
3739bd792da3SRichard Henderson     cpos = width - 1 - a->pos;
3740bd792da3SRichard Henderson     if (cpos + len > width) {
3741bd792da3SRichard Henderson         len = width - cpos;
3742bd792da3SRichard Henderson     }
3743bd792da3SRichard Henderson 
374430878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
374530878590SRichard Henderson     src = load_gpr(ctx, a->r);
374630878590SRichard Henderson     if (a->se) {
37476fd0c7bcSRichard Henderson         tcg_gen_sextract_i64(dest, src, cpos, len);
37480b1347d2SRichard Henderson     } else {
37496fd0c7bcSRichard Henderson         tcg_gen_extract_i64(dest, src, cpos, len);
37500b1347d2SRichard Henderson     }
375130878590SRichard Henderson     save_gpr(ctx, a->t, dest);
37520b1347d2SRichard Henderson 
37530b1347d2SRichard Henderson     /* Install the new nullification.  */
37540b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
375530878590SRichard Henderson     if (a->c) {
3756bd792da3SRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
37570b1347d2SRichard Henderson     }
375831234768SRichard Henderson     return nullify_end(ctx);
37590b1347d2SRichard Henderson }
37600b1347d2SRichard Henderson 
376172ae4f2bSRichard Henderson static bool trans_depi_imm(DisasContext *ctx, arg_depi_imm *a)
37620b1347d2SRichard Henderson {
376372ae4f2bSRichard Henderson     unsigned len, width;
3764c53e401eSRichard Henderson     uint64_t mask0, mask1;
37656fd0c7bcSRichard Henderson     TCGv_i64 dest;
37660b1347d2SRichard Henderson 
376772ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
376872ae4f2bSRichard Henderson         return false;
376972ae4f2bSRichard Henderson     }
377030878590SRichard Henderson     if (a->c) {
37710b1347d2SRichard Henderson         nullify_over(ctx);
37720b1347d2SRichard Henderson     }
377372ae4f2bSRichard Henderson 
377472ae4f2bSRichard Henderson     len = a->len;
377572ae4f2bSRichard Henderson     width = a->d ? 64 : 32;
377672ae4f2bSRichard Henderson     if (a->cpos + len > width) {
377772ae4f2bSRichard Henderson         len = width - a->cpos;
37780b1347d2SRichard Henderson     }
37790b1347d2SRichard Henderson 
378030878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
378130878590SRichard Henderson     mask0 = deposit64(0, a->cpos, len, a->i);
378230878590SRichard Henderson     mask1 = deposit64(-1, a->cpos, len, a->i);
37830b1347d2SRichard Henderson 
378430878590SRichard Henderson     if (a->nz) {
37856fd0c7bcSRichard Henderson         TCGv_i64 src = load_gpr(ctx, a->t);
37866fd0c7bcSRichard Henderson         tcg_gen_andi_i64(dest, src, mask1);
37876fd0c7bcSRichard Henderson         tcg_gen_ori_i64(dest, dest, mask0);
37880b1347d2SRichard Henderson     } else {
37896fd0c7bcSRichard Henderson         tcg_gen_movi_i64(dest, mask0);
37900b1347d2SRichard Henderson     }
379130878590SRichard Henderson     save_gpr(ctx, a->t, dest);
37920b1347d2SRichard Henderson 
37930b1347d2SRichard Henderson     /* Install the new nullification.  */
37940b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
379530878590SRichard Henderson     if (a->c) {
379672ae4f2bSRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
37970b1347d2SRichard Henderson     }
379831234768SRichard Henderson     return nullify_end(ctx);
37990b1347d2SRichard Henderson }
38000b1347d2SRichard Henderson 
380172ae4f2bSRichard Henderson static bool trans_dep_imm(DisasContext *ctx, arg_dep_imm *a)
38020b1347d2SRichard Henderson {
380330878590SRichard Henderson     unsigned rs = a->nz ? a->t : 0;
380472ae4f2bSRichard Henderson     unsigned len, width;
38056fd0c7bcSRichard Henderson     TCGv_i64 dest, val;
38060b1347d2SRichard Henderson 
380772ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
380872ae4f2bSRichard Henderson         return false;
380972ae4f2bSRichard Henderson     }
381030878590SRichard Henderson     if (a->c) {
38110b1347d2SRichard Henderson         nullify_over(ctx);
38120b1347d2SRichard Henderson     }
381372ae4f2bSRichard Henderson 
381472ae4f2bSRichard Henderson     len = a->len;
381572ae4f2bSRichard Henderson     width = a->d ? 64 : 32;
381672ae4f2bSRichard Henderson     if (a->cpos + len > width) {
381772ae4f2bSRichard Henderson         len = width - a->cpos;
38180b1347d2SRichard Henderson     }
38190b1347d2SRichard Henderson 
382030878590SRichard Henderson     dest = dest_gpr(ctx, a->t);
382130878590SRichard Henderson     val = load_gpr(ctx, a->r);
38220b1347d2SRichard Henderson     if (rs == 0) {
38236fd0c7bcSRichard Henderson         tcg_gen_deposit_z_i64(dest, val, a->cpos, len);
38240b1347d2SRichard Henderson     } else {
38256fd0c7bcSRichard Henderson         tcg_gen_deposit_i64(dest, cpu_gr[rs], val, a->cpos, len);
38260b1347d2SRichard Henderson     }
382730878590SRichard Henderson     save_gpr(ctx, a->t, dest);
38280b1347d2SRichard Henderson 
38290b1347d2SRichard Henderson     /* Install the new nullification.  */
38300b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
383130878590SRichard Henderson     if (a->c) {
383272ae4f2bSRichard Henderson         ctx->null_cond = do_sed_cond(ctx, a->c, a->d, dest);
38330b1347d2SRichard Henderson     }
383431234768SRichard Henderson     return nullify_end(ctx);
38350b1347d2SRichard Henderson }
38360b1347d2SRichard Henderson 
383772ae4f2bSRichard Henderson static bool do_dep_sar(DisasContext *ctx, unsigned rt, unsigned c,
38386fd0c7bcSRichard Henderson                        bool d, bool nz, unsigned len, TCGv_i64 val)
38390b1347d2SRichard Henderson {
38400b1347d2SRichard Henderson     unsigned rs = nz ? rt : 0;
384172ae4f2bSRichard Henderson     unsigned widthm1 = d ? 63 : 31;
38426fd0c7bcSRichard Henderson     TCGv_i64 mask, tmp, shift, dest;
3843c53e401eSRichard Henderson     uint64_t msb = 1ULL << (len - 1);
38440b1347d2SRichard Henderson 
38450b1347d2SRichard Henderson     dest = dest_gpr(ctx, rt);
3846aac0f603SRichard Henderson     shift = tcg_temp_new_i64();
3847aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
38480b1347d2SRichard Henderson 
38490b1347d2SRichard Henderson     /* Convert big-endian bit numbering in SAR to left-shift.  */
38506fd0c7bcSRichard Henderson     tcg_gen_andi_i64(shift, cpu_sar, widthm1);
38516fd0c7bcSRichard Henderson     tcg_gen_xori_i64(shift, shift, widthm1);
38520b1347d2SRichard Henderson 
3853aac0f603SRichard Henderson     mask = tcg_temp_new_i64();
38546fd0c7bcSRichard Henderson     tcg_gen_movi_i64(mask, msb + (msb - 1));
38556fd0c7bcSRichard Henderson     tcg_gen_and_i64(tmp, val, mask);
38560b1347d2SRichard Henderson     if (rs) {
38576fd0c7bcSRichard Henderson         tcg_gen_shl_i64(mask, mask, shift);
38586fd0c7bcSRichard Henderson         tcg_gen_shl_i64(tmp, tmp, shift);
38596fd0c7bcSRichard Henderson         tcg_gen_andc_i64(dest, cpu_gr[rs], mask);
38606fd0c7bcSRichard Henderson         tcg_gen_or_i64(dest, dest, tmp);
38610b1347d2SRichard Henderson     } else {
38626fd0c7bcSRichard Henderson         tcg_gen_shl_i64(dest, tmp, shift);
38630b1347d2SRichard Henderson     }
38640b1347d2SRichard Henderson     save_gpr(ctx, rt, dest);
38650b1347d2SRichard Henderson 
38660b1347d2SRichard Henderson     /* Install the new nullification.  */
38670b1347d2SRichard Henderson     cond_free(&ctx->null_cond);
38680b1347d2SRichard Henderson     if (c) {
386972ae4f2bSRichard Henderson         ctx->null_cond = do_sed_cond(ctx, c, d, dest);
38700b1347d2SRichard Henderson     }
387131234768SRichard Henderson     return nullify_end(ctx);
38720b1347d2SRichard Henderson }
38730b1347d2SRichard Henderson 
387472ae4f2bSRichard Henderson static bool trans_dep_sar(DisasContext *ctx, arg_dep_sar *a)
387530878590SRichard Henderson {
387672ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
387772ae4f2bSRichard Henderson         return false;
387872ae4f2bSRichard Henderson     }
3879a6deecceSSven Schnelle     if (a->c) {
3880a6deecceSSven Schnelle         nullify_over(ctx);
3881a6deecceSSven Schnelle     }
388272ae4f2bSRichard Henderson     return do_dep_sar(ctx, a->t, a->c, a->d, a->nz, a->len,
388372ae4f2bSRichard Henderson                       load_gpr(ctx, a->r));
388430878590SRichard Henderson }
388530878590SRichard Henderson 
388672ae4f2bSRichard Henderson static bool trans_depi_sar(DisasContext *ctx, arg_depi_sar *a)
388730878590SRichard Henderson {
388872ae4f2bSRichard Henderson     if (!ctx->is_pa20 && a->d) {
388972ae4f2bSRichard Henderson         return false;
389072ae4f2bSRichard Henderson     }
3891a6deecceSSven Schnelle     if (a->c) {
3892a6deecceSSven Schnelle         nullify_over(ctx);
3893a6deecceSSven Schnelle     }
389472ae4f2bSRichard Henderson     return do_dep_sar(ctx, a->t, a->c, a->d, a->nz, a->len,
38956fd0c7bcSRichard Henderson                       tcg_constant_i64(a->i));
389630878590SRichard Henderson }
38970b1347d2SRichard Henderson 
38988340f534SRichard Henderson static bool trans_be(DisasContext *ctx, arg_be *a)
389998cd9ca7SRichard Henderson {
39006fd0c7bcSRichard Henderson     TCGv_i64 tmp;
390198cd9ca7SRichard Henderson 
3902aac0f603SRichard Henderson     tmp = tcg_temp_new_i64();
39036fd0c7bcSRichard Henderson     tcg_gen_addi_i64(tmp, load_gpr(ctx, a->b), a->disp);
3904660eefe1SRichard Henderson     tmp = do_ibranch_priv(ctx, tmp);
3905c301f34eSRichard Henderson 
3906c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
39078340f534SRichard Henderson     return do_ibranch(ctx, tmp, a->l, a->n);
3908c301f34eSRichard Henderson #else
3909c301f34eSRichard Henderson     TCGv_i64 new_spc = tcg_temp_new_i64();
3910c301f34eSRichard Henderson 
39112644f80bSRichard Henderson     nullify_over(ctx);
39122644f80bSRichard Henderson 
39138340f534SRichard Henderson     load_spr(ctx, new_spc, a->sp);
3914*43541db0SRichard Henderson     install_link(ctx, a->l, true);
39158340f534SRichard Henderson     if (a->n && use_nullify_skip(ctx)) {
391685e6cda0SRichard Henderson         install_iaq_entries(ctx, -1, tmp, -1, NULL);
3917c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_f, new_spc);
3918c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_b, cpu_iasq_f);
39194a3aa11eSRichard Henderson         nullify_set(ctx, 0);
3920c301f34eSRichard Henderson     } else {
392185e6cda0SRichard Henderson         install_iaq_entries(ctx, ctx->iaoq_b, cpu_iaoq_b, -1, tmp);
3922c301f34eSRichard Henderson         if (ctx->iaoq_b == -1) {
3923c301f34eSRichard Henderson             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
3924c301f34eSRichard Henderson         }
3925c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_b, new_spc);
39268340f534SRichard Henderson         nullify_set(ctx, a->n);
3927c301f34eSRichard Henderson     }
3928c301f34eSRichard Henderson     tcg_gen_lookup_and_goto_ptr();
392931234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
393031234768SRichard Henderson     return nullify_end(ctx);
3931c301f34eSRichard Henderson #endif
393298cd9ca7SRichard Henderson }
393398cd9ca7SRichard Henderson 
39348340f534SRichard Henderson static bool trans_bl(DisasContext *ctx, arg_bl *a)
393598cd9ca7SRichard Henderson {
39362644f80bSRichard Henderson     return do_dbranch(ctx, a->disp, a->l, a->n);
393798cd9ca7SRichard Henderson }
393898cd9ca7SRichard Henderson 
39398340f534SRichard Henderson static bool trans_b_gate(DisasContext *ctx, arg_b_gate *a)
394043e05652SRichard Henderson {
3941c53e401eSRichard Henderson     uint64_t dest = iaoq_dest(ctx, a->disp);
394243e05652SRichard Henderson 
39436e5f5300SSven Schnelle     nullify_over(ctx);
39446e5f5300SSven Schnelle 
394543e05652SRichard Henderson     /* Make sure the caller hasn't done something weird with the queue.
394643e05652SRichard Henderson      * ??? This is not quite the same as the PSW[B] bit, which would be
394743e05652SRichard Henderson      * expensive to track.  Real hardware will trap for
394843e05652SRichard Henderson      *    b  gateway
394943e05652SRichard Henderson      *    b  gateway+4  (in delay slot of first branch)
395043e05652SRichard Henderson      * However, checking for a non-sequential instruction queue *will*
395143e05652SRichard Henderson      * diagnose the security hole
395243e05652SRichard Henderson      *    b  gateway
395343e05652SRichard Henderson      *    b  evil
395443e05652SRichard Henderson      * in which instructions at evil would run with increased privs.
395543e05652SRichard Henderson      */
395643e05652SRichard Henderson     if (ctx->iaoq_b == -1 || ctx->iaoq_b != ctx->iaoq_f + 4) {
395743e05652SRichard Henderson         return gen_illegal(ctx);
395843e05652SRichard Henderson     }
395943e05652SRichard Henderson 
396043e05652SRichard Henderson #ifndef CONFIG_USER_ONLY
396143e05652SRichard Henderson     if (ctx->tb_flags & PSW_C) {
396294956d7bSPhilippe Mathieu-Daudé         int type = hppa_artype_for_page(cpu_env(ctx->cs), ctx->base.pc_next);
396343e05652SRichard Henderson         /* If we could not find a TLB entry, then we need to generate an
396443e05652SRichard Henderson            ITLB miss exception so the kernel will provide it.
396543e05652SRichard Henderson            The resulting TLB fill operation will invalidate this TB and
396643e05652SRichard Henderson            we will re-translate, at which point we *will* be able to find
396743e05652SRichard Henderson            the TLB entry and determine if this is in fact a gateway page.  */
396843e05652SRichard Henderson         if (type < 0) {
396931234768SRichard Henderson             gen_excp(ctx, EXCP_ITLB_MISS);
397031234768SRichard Henderson             return true;
397143e05652SRichard Henderson         }
397243e05652SRichard Henderson         /* No change for non-gateway pages or for priv decrease.  */
397343e05652SRichard Henderson         if (type >= 4 && type - 4 < ctx->privilege) {
39742f48ba7bSRichard Henderson             dest = deposit64(dest, 0, 2, type - 4);
397543e05652SRichard Henderson         }
397643e05652SRichard Henderson     } else {
397743e05652SRichard Henderson         dest &= -4;  /* priv = 0 */
397843e05652SRichard Henderson     }
397943e05652SRichard Henderson #endif
398043e05652SRichard Henderson 
39816e5f5300SSven Schnelle     if (a->l) {
39826fd0c7bcSRichard Henderson         TCGv_i64 tmp = dest_gpr(ctx, a->l);
39836e5f5300SSven Schnelle         if (ctx->privilege < 3) {
39846fd0c7bcSRichard Henderson             tcg_gen_andi_i64(tmp, tmp, -4);
39856e5f5300SSven Schnelle         }
39866fd0c7bcSRichard Henderson         tcg_gen_ori_i64(tmp, tmp, ctx->privilege);
39876e5f5300SSven Schnelle         save_gpr(ctx, a->l, tmp);
39886e5f5300SSven Schnelle     }
39896e5f5300SSven Schnelle 
39902644f80bSRichard Henderson     return do_dbranch(ctx, dest - iaoq_dest(ctx, 0), 0, a->n);
399143e05652SRichard Henderson }
399243e05652SRichard Henderson 
39938340f534SRichard Henderson static bool trans_blr(DisasContext *ctx, arg_blr *a)
399498cd9ca7SRichard Henderson {
3995b35aec85SRichard Henderson     if (a->x) {
3996aac0f603SRichard Henderson         TCGv_i64 tmp = tcg_temp_new_i64();
39976fd0c7bcSRichard Henderson         tcg_gen_shli_i64(tmp, load_gpr(ctx, a->x), 3);
39986fd0c7bcSRichard Henderson         tcg_gen_addi_i64(tmp, tmp, ctx->iaoq_f + 8);
3999660eefe1SRichard Henderson         /* The computation here never changes privilege level.  */
40008340f534SRichard Henderson         return do_ibranch(ctx, tmp, a->l, a->n);
4001b35aec85SRichard Henderson     } else {
4002b35aec85SRichard Henderson         /* BLR R0,RX is a good way to load PC+8 into RX.  */
40032644f80bSRichard Henderson         return do_dbranch(ctx, 0, a->l, a->n);
4004b35aec85SRichard Henderson     }
400598cd9ca7SRichard Henderson }
400698cd9ca7SRichard Henderson 
40078340f534SRichard Henderson static bool trans_bv(DisasContext *ctx, arg_bv *a)
400898cd9ca7SRichard Henderson {
40096fd0c7bcSRichard Henderson     TCGv_i64 dest;
401098cd9ca7SRichard Henderson 
40118340f534SRichard Henderson     if (a->x == 0) {
40128340f534SRichard Henderson         dest = load_gpr(ctx, a->b);
401398cd9ca7SRichard Henderson     } else {
4014aac0f603SRichard Henderson         dest = tcg_temp_new_i64();
40156fd0c7bcSRichard Henderson         tcg_gen_shli_i64(dest, load_gpr(ctx, a->x), 3);
40166fd0c7bcSRichard Henderson         tcg_gen_add_i64(dest, dest, load_gpr(ctx, a->b));
401798cd9ca7SRichard Henderson     }
4018660eefe1SRichard Henderson     dest = do_ibranch_priv(ctx, dest);
40198340f534SRichard Henderson     return do_ibranch(ctx, dest, 0, a->n);
402098cd9ca7SRichard Henderson }
402198cd9ca7SRichard Henderson 
40228340f534SRichard Henderson static bool trans_bve(DisasContext *ctx, arg_bve *a)
402398cd9ca7SRichard Henderson {
40246fd0c7bcSRichard Henderson     TCGv_i64 dest;
402598cd9ca7SRichard Henderson 
4026c301f34eSRichard Henderson #ifdef CONFIG_USER_ONLY
40278340f534SRichard Henderson     dest = do_ibranch_priv(ctx, load_gpr(ctx, a->b));
40288340f534SRichard Henderson     return do_ibranch(ctx, dest, a->l, a->n);
4029c301f34eSRichard Henderson #else
4030c301f34eSRichard Henderson     nullify_over(ctx);
4031*43541db0SRichard Henderson     dest = tcg_temp_new_i64();
4032*43541db0SRichard Henderson     tcg_gen_mov_i64(dest, load_gpr(ctx, a->b));
4033*43541db0SRichard Henderson     dest = do_ibranch_priv(ctx, dest);
4034c301f34eSRichard Henderson 
4035*43541db0SRichard Henderson     install_link(ctx, a->l, false);
403685e6cda0SRichard Henderson     install_iaq_entries(ctx, ctx->iaoq_b, cpu_iaoq_b, -1, dest);
4037c301f34eSRichard Henderson     if (ctx->iaoq_b == -1) {
4038c301f34eSRichard Henderson         tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4039c301f34eSRichard Henderson     }
4040c301f34eSRichard Henderson     tcg_gen_mov_i64(cpu_iasq_b, space_select(ctx, 0, dest));
40418340f534SRichard Henderson     nullify_set(ctx, a->n);
4042c301f34eSRichard Henderson     tcg_gen_lookup_and_goto_ptr();
404331234768SRichard Henderson     ctx->base.is_jmp = DISAS_NORETURN;
404431234768SRichard Henderson     return nullify_end(ctx);
4045c301f34eSRichard Henderson #endif
404698cd9ca7SRichard Henderson }
404798cd9ca7SRichard Henderson 
4048a8966ba7SRichard Henderson static bool trans_nopbts(DisasContext *ctx, arg_nopbts *a)
4049a8966ba7SRichard Henderson {
4050a8966ba7SRichard Henderson     /* All branch target stack instructions implement as nop. */
4051a8966ba7SRichard Henderson     return ctx->is_pa20;
4052a8966ba7SRichard Henderson }
4053a8966ba7SRichard Henderson 
40541ca74648SRichard Henderson /*
40551ca74648SRichard Henderson  * Float class 0
40561ca74648SRichard Henderson  */
4057ebe9383cSRichard Henderson 
40581ca74648SRichard Henderson static void gen_fcpy_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4059ebe9383cSRichard Henderson {
4060ebe9383cSRichard Henderson     tcg_gen_mov_i32(dst, src);
4061ebe9383cSRichard Henderson }
4062ebe9383cSRichard Henderson 
406359f8c04bSHelge Deller static bool trans_fid_f(DisasContext *ctx, arg_fid_f *a)
406459f8c04bSHelge Deller {
4065a300dad3SRichard Henderson     uint64_t ret;
4066a300dad3SRichard Henderson 
4067c53e401eSRichard Henderson     if (ctx->is_pa20) {
4068a300dad3SRichard Henderson         ret = 0x13080000000000ULL; /* PA8700 (PCX-W2) */
4069a300dad3SRichard Henderson     } else {
4070a300dad3SRichard Henderson         ret = 0x0f080000000000ULL; /* PA7300LC (PCX-L2) */
4071a300dad3SRichard Henderson     }
4072a300dad3SRichard Henderson 
407359f8c04bSHelge Deller     nullify_over(ctx);
4074a300dad3SRichard Henderson     save_frd(0, tcg_constant_i64(ret));
407559f8c04bSHelge Deller     return nullify_end(ctx);
407659f8c04bSHelge Deller }
407759f8c04bSHelge Deller 
40781ca74648SRichard Henderson static bool trans_fcpy_f(DisasContext *ctx, arg_fclass01 *a)
40791ca74648SRichard Henderson {
40801ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fcpy_f);
40811ca74648SRichard Henderson }
40821ca74648SRichard Henderson 
4083ebe9383cSRichard Henderson static void gen_fcpy_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4084ebe9383cSRichard Henderson {
4085ebe9383cSRichard Henderson     tcg_gen_mov_i64(dst, src);
4086ebe9383cSRichard Henderson }
4087ebe9383cSRichard Henderson 
40881ca74648SRichard Henderson static bool trans_fcpy_d(DisasContext *ctx, arg_fclass01 *a)
40891ca74648SRichard Henderson {
40901ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fcpy_d);
40911ca74648SRichard Henderson }
40921ca74648SRichard Henderson 
40931ca74648SRichard Henderson static void gen_fabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4094ebe9383cSRichard Henderson {
4095ebe9383cSRichard Henderson     tcg_gen_andi_i32(dst, src, INT32_MAX);
4096ebe9383cSRichard Henderson }
4097ebe9383cSRichard Henderson 
40981ca74648SRichard Henderson static bool trans_fabs_f(DisasContext *ctx, arg_fclass01 *a)
40991ca74648SRichard Henderson {
41001ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fabs_f);
41011ca74648SRichard Henderson }
41021ca74648SRichard Henderson 
4103ebe9383cSRichard Henderson static void gen_fabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4104ebe9383cSRichard Henderson {
4105ebe9383cSRichard Henderson     tcg_gen_andi_i64(dst, src, INT64_MAX);
4106ebe9383cSRichard Henderson }
4107ebe9383cSRichard Henderson 
41081ca74648SRichard Henderson static bool trans_fabs_d(DisasContext *ctx, arg_fclass01 *a)
41091ca74648SRichard Henderson {
41101ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fabs_d);
41111ca74648SRichard Henderson }
41121ca74648SRichard Henderson 
41131ca74648SRichard Henderson static bool trans_fsqrt_f(DisasContext *ctx, arg_fclass01 *a)
41141ca74648SRichard Henderson {
41151ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fsqrt_s);
41161ca74648SRichard Henderson }
41171ca74648SRichard Henderson 
41181ca74648SRichard Henderson static bool trans_fsqrt_d(DisasContext *ctx, arg_fclass01 *a)
41191ca74648SRichard Henderson {
41201ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fsqrt_d);
41211ca74648SRichard Henderson }
41221ca74648SRichard Henderson 
41231ca74648SRichard Henderson static bool trans_frnd_f(DisasContext *ctx, arg_fclass01 *a)
41241ca74648SRichard Henderson {
41251ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_frnd_s);
41261ca74648SRichard Henderson }
41271ca74648SRichard Henderson 
41281ca74648SRichard Henderson static bool trans_frnd_d(DisasContext *ctx, arg_fclass01 *a)
41291ca74648SRichard Henderson {
41301ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_frnd_d);
41311ca74648SRichard Henderson }
41321ca74648SRichard Henderson 
41331ca74648SRichard Henderson static void gen_fneg_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4134ebe9383cSRichard Henderson {
4135ebe9383cSRichard Henderson     tcg_gen_xori_i32(dst, src, INT32_MIN);
4136ebe9383cSRichard Henderson }
4137ebe9383cSRichard Henderson 
41381ca74648SRichard Henderson static bool trans_fneg_f(DisasContext *ctx, arg_fclass01 *a)
41391ca74648SRichard Henderson {
41401ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fneg_f);
41411ca74648SRichard Henderson }
41421ca74648SRichard Henderson 
4143ebe9383cSRichard Henderson static void gen_fneg_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4144ebe9383cSRichard Henderson {
4145ebe9383cSRichard Henderson     tcg_gen_xori_i64(dst, src, INT64_MIN);
4146ebe9383cSRichard Henderson }
4147ebe9383cSRichard Henderson 
41481ca74648SRichard Henderson static bool trans_fneg_d(DisasContext *ctx, arg_fclass01 *a)
41491ca74648SRichard Henderson {
41501ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fneg_d);
41511ca74648SRichard Henderson }
41521ca74648SRichard Henderson 
41531ca74648SRichard Henderson static void gen_fnegabs_f(TCGv_i32 dst, TCGv_env unused, TCGv_i32 src)
4154ebe9383cSRichard Henderson {
4155ebe9383cSRichard Henderson     tcg_gen_ori_i32(dst, src, INT32_MIN);
4156ebe9383cSRichard Henderson }
4157ebe9383cSRichard Henderson 
41581ca74648SRichard Henderson static bool trans_fnegabs_f(DisasContext *ctx, arg_fclass01 *a)
41591ca74648SRichard Henderson {
41601ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_fnegabs_f);
41611ca74648SRichard Henderson }
41621ca74648SRichard Henderson 
4163ebe9383cSRichard Henderson static void gen_fnegabs_d(TCGv_i64 dst, TCGv_env unused, TCGv_i64 src)
4164ebe9383cSRichard Henderson {
4165ebe9383cSRichard Henderson     tcg_gen_ori_i64(dst, src, INT64_MIN);
4166ebe9383cSRichard Henderson }
4167ebe9383cSRichard Henderson 
41681ca74648SRichard Henderson static bool trans_fnegabs_d(DisasContext *ctx, arg_fclass01 *a)
41691ca74648SRichard Henderson {
41701ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_fnegabs_d);
41711ca74648SRichard Henderson }
41721ca74648SRichard Henderson 
41731ca74648SRichard Henderson /*
41741ca74648SRichard Henderson  * Float class 1
41751ca74648SRichard Henderson  */
41761ca74648SRichard Henderson 
41771ca74648SRichard Henderson static bool trans_fcnv_d_f(DisasContext *ctx, arg_fclass01 *a)
41781ca74648SRichard Henderson {
41791ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_s);
41801ca74648SRichard Henderson }
41811ca74648SRichard Henderson 
41821ca74648SRichard Henderson static bool trans_fcnv_f_d(DisasContext *ctx, arg_fclass01 *a)
41831ca74648SRichard Henderson {
41841ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_d);
41851ca74648SRichard Henderson }
41861ca74648SRichard Henderson 
41871ca74648SRichard Henderson static bool trans_fcnv_w_f(DisasContext *ctx, arg_fclass01 *a)
41881ca74648SRichard Henderson {
41891ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_w_s);
41901ca74648SRichard Henderson }
41911ca74648SRichard Henderson 
41921ca74648SRichard Henderson static bool trans_fcnv_q_f(DisasContext *ctx, arg_fclass01 *a)
41931ca74648SRichard Henderson {
41941ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_dw_s);
41951ca74648SRichard Henderson }
41961ca74648SRichard Henderson 
41971ca74648SRichard Henderson static bool trans_fcnv_w_d(DisasContext *ctx, arg_fclass01 *a)
41981ca74648SRichard Henderson {
41991ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_w_d);
42001ca74648SRichard Henderson }
42011ca74648SRichard Henderson 
42021ca74648SRichard Henderson static bool trans_fcnv_q_d(DisasContext *ctx, arg_fclass01 *a)
42031ca74648SRichard Henderson {
42041ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_dw_d);
42051ca74648SRichard Henderson }
42061ca74648SRichard Henderson 
42071ca74648SRichard Henderson static bool trans_fcnv_f_w(DisasContext *ctx, arg_fclass01 *a)
42081ca74648SRichard Henderson {
42091ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_w);
42101ca74648SRichard Henderson }
42111ca74648SRichard Henderson 
42121ca74648SRichard Henderson static bool trans_fcnv_d_w(DisasContext *ctx, arg_fclass01 *a)
42131ca74648SRichard Henderson {
42141ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_w);
42151ca74648SRichard Henderson }
42161ca74648SRichard Henderson 
42171ca74648SRichard Henderson static bool trans_fcnv_f_q(DisasContext *ctx, arg_fclass01 *a)
42181ca74648SRichard Henderson {
42191ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_dw);
42201ca74648SRichard Henderson }
42211ca74648SRichard Henderson 
42221ca74648SRichard Henderson static bool trans_fcnv_d_q(DisasContext *ctx, arg_fclass01 *a)
42231ca74648SRichard Henderson {
42241ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_dw);
42251ca74648SRichard Henderson }
42261ca74648SRichard Henderson 
42271ca74648SRichard Henderson static bool trans_fcnv_t_f_w(DisasContext *ctx, arg_fclass01 *a)
42281ca74648SRichard Henderson {
42291ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_w);
42301ca74648SRichard Henderson }
42311ca74648SRichard Henderson 
42321ca74648SRichard Henderson static bool trans_fcnv_t_d_w(DisasContext *ctx, arg_fclass01 *a)
42331ca74648SRichard Henderson {
42341ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_w);
42351ca74648SRichard Henderson }
42361ca74648SRichard Henderson 
42371ca74648SRichard Henderson static bool trans_fcnv_t_f_q(DisasContext *ctx, arg_fclass01 *a)
42381ca74648SRichard Henderson {
42391ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_dw);
42401ca74648SRichard Henderson }
42411ca74648SRichard Henderson 
42421ca74648SRichard Henderson static bool trans_fcnv_t_d_q(DisasContext *ctx, arg_fclass01 *a)
42431ca74648SRichard Henderson {
42441ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_dw);
42451ca74648SRichard Henderson }
42461ca74648SRichard Henderson 
42471ca74648SRichard Henderson static bool trans_fcnv_uw_f(DisasContext *ctx, arg_fclass01 *a)
42481ca74648SRichard Henderson {
42491ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_uw_s);
42501ca74648SRichard Henderson }
42511ca74648SRichard Henderson 
42521ca74648SRichard Henderson static bool trans_fcnv_uq_f(DisasContext *ctx, arg_fclass01 *a)
42531ca74648SRichard Henderson {
42541ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_udw_s);
42551ca74648SRichard Henderson }
42561ca74648SRichard Henderson 
42571ca74648SRichard Henderson static bool trans_fcnv_uw_d(DisasContext *ctx, arg_fclass01 *a)
42581ca74648SRichard Henderson {
42591ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_uw_d);
42601ca74648SRichard Henderson }
42611ca74648SRichard Henderson 
42621ca74648SRichard Henderson static bool trans_fcnv_uq_d(DisasContext *ctx, arg_fclass01 *a)
42631ca74648SRichard Henderson {
42641ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_udw_d);
42651ca74648SRichard Henderson }
42661ca74648SRichard Henderson 
42671ca74648SRichard Henderson static bool trans_fcnv_f_uw(DisasContext *ctx, arg_fclass01 *a)
42681ca74648SRichard Henderson {
42691ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_s_uw);
42701ca74648SRichard Henderson }
42711ca74648SRichard Henderson 
42721ca74648SRichard Henderson static bool trans_fcnv_d_uw(DisasContext *ctx, arg_fclass01 *a)
42731ca74648SRichard Henderson {
42741ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_d_uw);
42751ca74648SRichard Henderson }
42761ca74648SRichard Henderson 
42771ca74648SRichard Henderson static bool trans_fcnv_f_uq(DisasContext *ctx, arg_fclass01 *a)
42781ca74648SRichard Henderson {
42791ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_s_udw);
42801ca74648SRichard Henderson }
42811ca74648SRichard Henderson 
42821ca74648SRichard Henderson static bool trans_fcnv_d_uq(DisasContext *ctx, arg_fclass01 *a)
42831ca74648SRichard Henderson {
42841ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_d_udw);
42851ca74648SRichard Henderson }
42861ca74648SRichard Henderson 
42871ca74648SRichard Henderson static bool trans_fcnv_t_f_uw(DisasContext *ctx, arg_fclass01 *a)
42881ca74648SRichard Henderson {
42891ca74648SRichard Henderson     return do_fop_wew(ctx, a->t, a->r, gen_helper_fcnv_t_s_uw);
42901ca74648SRichard Henderson }
42911ca74648SRichard Henderson 
42921ca74648SRichard Henderson static bool trans_fcnv_t_d_uw(DisasContext *ctx, arg_fclass01 *a)
42931ca74648SRichard Henderson {
42941ca74648SRichard Henderson     return do_fop_wed(ctx, a->t, a->r, gen_helper_fcnv_t_d_uw);
42951ca74648SRichard Henderson }
42961ca74648SRichard Henderson 
42971ca74648SRichard Henderson static bool trans_fcnv_t_f_uq(DisasContext *ctx, arg_fclass01 *a)
42981ca74648SRichard Henderson {
42991ca74648SRichard Henderson     return do_fop_dew(ctx, a->t, a->r, gen_helper_fcnv_t_s_udw);
43001ca74648SRichard Henderson }
43011ca74648SRichard Henderson 
43021ca74648SRichard Henderson static bool trans_fcnv_t_d_uq(DisasContext *ctx, arg_fclass01 *a)
43031ca74648SRichard Henderson {
43041ca74648SRichard Henderson     return do_fop_ded(ctx, a->t, a->r, gen_helper_fcnv_t_d_udw);
43051ca74648SRichard Henderson }
43061ca74648SRichard Henderson 
43071ca74648SRichard Henderson /*
43081ca74648SRichard Henderson  * Float class 2
43091ca74648SRichard Henderson  */
43101ca74648SRichard Henderson 
43111ca74648SRichard Henderson static bool trans_fcmp_f(DisasContext *ctx, arg_fclass2 *a)
4312ebe9383cSRichard Henderson {
4313ebe9383cSRichard Henderson     TCGv_i32 ta, tb, tc, ty;
4314ebe9383cSRichard Henderson 
4315ebe9383cSRichard Henderson     nullify_over(ctx);
4316ebe9383cSRichard Henderson 
43171ca74648SRichard Henderson     ta = load_frw0_i32(a->r1);
43181ca74648SRichard Henderson     tb = load_frw0_i32(a->r2);
431929dd6f64SRichard Henderson     ty = tcg_constant_i32(a->y);
432029dd6f64SRichard Henderson     tc = tcg_constant_i32(a->c);
4321ebe9383cSRichard Henderson 
4322ad75a51eSRichard Henderson     gen_helper_fcmp_s(tcg_env, ta, tb, ty, tc);
4323ebe9383cSRichard Henderson 
43241ca74648SRichard Henderson     return nullify_end(ctx);
4325ebe9383cSRichard Henderson }
4326ebe9383cSRichard Henderson 
43271ca74648SRichard Henderson static bool trans_fcmp_d(DisasContext *ctx, arg_fclass2 *a)
4328ebe9383cSRichard Henderson {
4329ebe9383cSRichard Henderson     TCGv_i64 ta, tb;
4330ebe9383cSRichard Henderson     TCGv_i32 tc, ty;
4331ebe9383cSRichard Henderson 
4332ebe9383cSRichard Henderson     nullify_over(ctx);
4333ebe9383cSRichard Henderson 
43341ca74648SRichard Henderson     ta = load_frd0(a->r1);
43351ca74648SRichard Henderson     tb = load_frd0(a->r2);
433629dd6f64SRichard Henderson     ty = tcg_constant_i32(a->y);
433729dd6f64SRichard Henderson     tc = tcg_constant_i32(a->c);
4338ebe9383cSRichard Henderson 
4339ad75a51eSRichard Henderson     gen_helper_fcmp_d(tcg_env, ta, tb, ty, tc);
4340ebe9383cSRichard Henderson 
434131234768SRichard Henderson     return nullify_end(ctx);
4342ebe9383cSRichard Henderson }
4343ebe9383cSRichard Henderson 
43441ca74648SRichard Henderson static bool trans_ftest(DisasContext *ctx, arg_ftest *a)
4345ebe9383cSRichard Henderson {
43466fd0c7bcSRichard Henderson     TCGv_i64 t;
4347ebe9383cSRichard Henderson 
4348ebe9383cSRichard Henderson     nullify_over(ctx);
4349ebe9383cSRichard Henderson 
4350aac0f603SRichard Henderson     t = tcg_temp_new_i64();
43516fd0c7bcSRichard Henderson     tcg_gen_ld32u_i64(t, tcg_env, offsetof(CPUHPPAState, fr0_shadow));
4352ebe9383cSRichard Henderson 
43531ca74648SRichard Henderson     if (a->y == 1) {
4354ebe9383cSRichard Henderson         int mask;
4355ebe9383cSRichard Henderson         bool inv = false;
4356ebe9383cSRichard Henderson 
43571ca74648SRichard Henderson         switch (a->c) {
4358ebe9383cSRichard Henderson         case 0: /* simple */
43596fd0c7bcSRichard Henderson             tcg_gen_andi_i64(t, t, 0x4000000);
4360ebe9383cSRichard Henderson             ctx->null_cond = cond_make_0(TCG_COND_NE, t);
4361ebe9383cSRichard Henderson             goto done;
4362ebe9383cSRichard Henderson         case 2: /* rej */
4363ebe9383cSRichard Henderson             inv = true;
4364ebe9383cSRichard Henderson             /* fallthru */
4365ebe9383cSRichard Henderson         case 1: /* acc */
4366ebe9383cSRichard Henderson             mask = 0x43ff800;
4367ebe9383cSRichard Henderson             break;
4368ebe9383cSRichard Henderson         case 6: /* rej8 */
4369ebe9383cSRichard Henderson             inv = true;
4370ebe9383cSRichard Henderson             /* fallthru */
4371ebe9383cSRichard Henderson         case 5: /* acc8 */
4372ebe9383cSRichard Henderson             mask = 0x43f8000;
4373ebe9383cSRichard Henderson             break;
4374ebe9383cSRichard Henderson         case 9: /* acc6 */
4375ebe9383cSRichard Henderson             mask = 0x43e0000;
4376ebe9383cSRichard Henderson             break;
4377ebe9383cSRichard Henderson         case 13: /* acc4 */
4378ebe9383cSRichard Henderson             mask = 0x4380000;
4379ebe9383cSRichard Henderson             break;
4380ebe9383cSRichard Henderson         case 17: /* acc2 */
4381ebe9383cSRichard Henderson             mask = 0x4200000;
4382ebe9383cSRichard Henderson             break;
4383ebe9383cSRichard Henderson         default:
43841ca74648SRichard Henderson             gen_illegal(ctx);
43851ca74648SRichard Henderson             return true;
4386ebe9383cSRichard Henderson         }
4387ebe9383cSRichard Henderson         if (inv) {
43886fd0c7bcSRichard Henderson             TCGv_i64 c = tcg_constant_i64(mask);
43896fd0c7bcSRichard Henderson             tcg_gen_or_i64(t, t, c);
4390ebe9383cSRichard Henderson             ctx->null_cond = cond_make(TCG_COND_EQ, t, c);
4391ebe9383cSRichard Henderson         } else {
43926fd0c7bcSRichard Henderson             tcg_gen_andi_i64(t, t, mask);
4393ebe9383cSRichard Henderson             ctx->null_cond = cond_make_0(TCG_COND_EQ, t);
4394ebe9383cSRichard Henderson         }
43951ca74648SRichard Henderson     } else {
43961ca74648SRichard Henderson         unsigned cbit = (a->y ^ 1) - 1;
43971ca74648SRichard Henderson 
43986fd0c7bcSRichard Henderson         tcg_gen_extract_i64(t, t, 21 - cbit, 1);
43991ca74648SRichard Henderson         ctx->null_cond = cond_make_0(TCG_COND_NE, t);
44001ca74648SRichard Henderson     }
44011ca74648SRichard Henderson 
4402ebe9383cSRichard Henderson  done:
440331234768SRichard Henderson     return nullify_end(ctx);
4404ebe9383cSRichard Henderson }
4405ebe9383cSRichard Henderson 
44061ca74648SRichard Henderson /*
44071ca74648SRichard Henderson  * Float class 2
44081ca74648SRichard Henderson  */
44091ca74648SRichard Henderson 
44101ca74648SRichard Henderson static bool trans_fadd_f(DisasContext *ctx, arg_fclass3 *a)
4411ebe9383cSRichard Henderson {
44121ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fadd_s);
44131ca74648SRichard Henderson }
44141ca74648SRichard Henderson 
44151ca74648SRichard Henderson static bool trans_fadd_d(DisasContext *ctx, arg_fclass3 *a)
44161ca74648SRichard Henderson {
44171ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fadd_d);
44181ca74648SRichard Henderson }
44191ca74648SRichard Henderson 
44201ca74648SRichard Henderson static bool trans_fsub_f(DisasContext *ctx, arg_fclass3 *a)
44211ca74648SRichard Henderson {
44221ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fsub_s);
44231ca74648SRichard Henderson }
44241ca74648SRichard Henderson 
44251ca74648SRichard Henderson static bool trans_fsub_d(DisasContext *ctx, arg_fclass3 *a)
44261ca74648SRichard Henderson {
44271ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fsub_d);
44281ca74648SRichard Henderson }
44291ca74648SRichard Henderson 
44301ca74648SRichard Henderson static bool trans_fmpy_f(DisasContext *ctx, arg_fclass3 *a)
44311ca74648SRichard Henderson {
44321ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_s);
44331ca74648SRichard Henderson }
44341ca74648SRichard Henderson 
44351ca74648SRichard Henderson static bool trans_fmpy_d(DisasContext *ctx, arg_fclass3 *a)
44361ca74648SRichard Henderson {
44371ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fmpy_d);
44381ca74648SRichard Henderson }
44391ca74648SRichard Henderson 
44401ca74648SRichard Henderson static bool trans_fdiv_f(DisasContext *ctx, arg_fclass3 *a)
44411ca74648SRichard Henderson {
44421ca74648SRichard Henderson     return do_fop_weww(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_s);
44431ca74648SRichard Henderson }
44441ca74648SRichard Henderson 
44451ca74648SRichard Henderson static bool trans_fdiv_d(DisasContext *ctx, arg_fclass3 *a)
44461ca74648SRichard Henderson {
44471ca74648SRichard Henderson     return do_fop_dedd(ctx, a->t, a->r1, a->r2, gen_helper_fdiv_d);
44481ca74648SRichard Henderson }
44491ca74648SRichard Henderson 
44501ca74648SRichard Henderson static bool trans_xmpyu(DisasContext *ctx, arg_xmpyu *a)
44511ca74648SRichard Henderson {
44521ca74648SRichard Henderson     TCGv_i64 x, y;
4453ebe9383cSRichard Henderson 
4454ebe9383cSRichard Henderson     nullify_over(ctx);
4455ebe9383cSRichard Henderson 
44561ca74648SRichard Henderson     x = load_frw0_i64(a->r1);
44571ca74648SRichard Henderson     y = load_frw0_i64(a->r2);
44581ca74648SRichard Henderson     tcg_gen_mul_i64(x, x, y);
44591ca74648SRichard Henderson     save_frd(a->t, x);
4460ebe9383cSRichard Henderson 
446131234768SRichard Henderson     return nullify_end(ctx);
4462ebe9383cSRichard Henderson }
4463ebe9383cSRichard Henderson 
4464ebe9383cSRichard Henderson /* Convert the fmpyadd single-precision register encodings to standard.  */
4465ebe9383cSRichard Henderson static inline int fmpyadd_s_reg(unsigned r)
4466ebe9383cSRichard Henderson {
4467ebe9383cSRichard Henderson     return (r & 16) * 2 + 16 + (r & 15);
4468ebe9383cSRichard Henderson }
4469ebe9383cSRichard Henderson 
4470b1e2af57SRichard Henderson static bool do_fmpyadd_s(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4471ebe9383cSRichard Henderson {
4472b1e2af57SRichard Henderson     int tm = fmpyadd_s_reg(a->tm);
4473b1e2af57SRichard Henderson     int ra = fmpyadd_s_reg(a->ra);
4474b1e2af57SRichard Henderson     int ta = fmpyadd_s_reg(a->ta);
4475b1e2af57SRichard Henderson     int rm2 = fmpyadd_s_reg(a->rm2);
4476b1e2af57SRichard Henderson     int rm1 = fmpyadd_s_reg(a->rm1);
4477ebe9383cSRichard Henderson 
4478ebe9383cSRichard Henderson     nullify_over(ctx);
4479ebe9383cSRichard Henderson 
4480ebe9383cSRichard Henderson     do_fop_weww(ctx, tm, rm1, rm2, gen_helper_fmpy_s);
4481ebe9383cSRichard Henderson     do_fop_weww(ctx, ta, ta, ra,
4482ebe9383cSRichard Henderson                 is_sub ? gen_helper_fsub_s : gen_helper_fadd_s);
4483ebe9383cSRichard Henderson 
448431234768SRichard Henderson     return nullify_end(ctx);
4485ebe9383cSRichard Henderson }
4486ebe9383cSRichard Henderson 
4487b1e2af57SRichard Henderson static bool trans_fmpyadd_f(DisasContext *ctx, arg_mpyadd *a)
4488b1e2af57SRichard Henderson {
4489b1e2af57SRichard Henderson     return do_fmpyadd_s(ctx, a, false);
4490b1e2af57SRichard Henderson }
4491b1e2af57SRichard Henderson 
4492b1e2af57SRichard Henderson static bool trans_fmpysub_f(DisasContext *ctx, arg_mpyadd *a)
4493b1e2af57SRichard Henderson {
4494b1e2af57SRichard Henderson     return do_fmpyadd_s(ctx, a, true);
4495b1e2af57SRichard Henderson }
4496b1e2af57SRichard Henderson 
4497b1e2af57SRichard Henderson static bool do_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a, bool is_sub)
4498b1e2af57SRichard Henderson {
4499b1e2af57SRichard Henderson     nullify_over(ctx);
4500b1e2af57SRichard Henderson 
4501b1e2af57SRichard Henderson     do_fop_dedd(ctx, a->tm, a->rm1, a->rm2, gen_helper_fmpy_d);
4502b1e2af57SRichard Henderson     do_fop_dedd(ctx, a->ta, a->ta, a->ra,
4503b1e2af57SRichard Henderson                 is_sub ? gen_helper_fsub_d : gen_helper_fadd_d);
4504b1e2af57SRichard Henderson 
4505b1e2af57SRichard Henderson     return nullify_end(ctx);
4506b1e2af57SRichard Henderson }
4507b1e2af57SRichard Henderson 
4508b1e2af57SRichard Henderson static bool trans_fmpyadd_d(DisasContext *ctx, arg_mpyadd *a)
4509b1e2af57SRichard Henderson {
4510b1e2af57SRichard Henderson     return do_fmpyadd_d(ctx, a, false);
4511b1e2af57SRichard Henderson }
4512b1e2af57SRichard Henderson 
4513b1e2af57SRichard Henderson static bool trans_fmpysub_d(DisasContext *ctx, arg_mpyadd *a)
4514b1e2af57SRichard Henderson {
4515b1e2af57SRichard Henderson     return do_fmpyadd_d(ctx, a, true);
4516b1e2af57SRichard Henderson }
4517b1e2af57SRichard Henderson 
4518c3bad4f8SRichard Henderson static bool trans_fmpyfadd_f(DisasContext *ctx, arg_fmpyfadd_f *a)
4519ebe9383cSRichard Henderson {
4520c3bad4f8SRichard Henderson     TCGv_i32 x, y, z;
4521ebe9383cSRichard Henderson 
4522ebe9383cSRichard Henderson     nullify_over(ctx);
4523c3bad4f8SRichard Henderson     x = load_frw0_i32(a->rm1);
4524c3bad4f8SRichard Henderson     y = load_frw0_i32(a->rm2);
4525c3bad4f8SRichard Henderson     z = load_frw0_i32(a->ra3);
4526ebe9383cSRichard Henderson 
4527c3bad4f8SRichard Henderson     if (a->neg) {
4528ad75a51eSRichard Henderson         gen_helper_fmpynfadd_s(x, tcg_env, x, y, z);
4529ebe9383cSRichard Henderson     } else {
4530ad75a51eSRichard Henderson         gen_helper_fmpyfadd_s(x, tcg_env, x, y, z);
4531ebe9383cSRichard Henderson     }
4532ebe9383cSRichard Henderson 
4533c3bad4f8SRichard Henderson     save_frw_i32(a->t, x);
453431234768SRichard Henderson     return nullify_end(ctx);
4535ebe9383cSRichard Henderson }
4536ebe9383cSRichard Henderson 
4537c3bad4f8SRichard Henderson static bool trans_fmpyfadd_d(DisasContext *ctx, arg_fmpyfadd_d *a)
4538ebe9383cSRichard Henderson {
4539c3bad4f8SRichard Henderson     TCGv_i64 x, y, z;
4540ebe9383cSRichard Henderson 
4541ebe9383cSRichard Henderson     nullify_over(ctx);
4542c3bad4f8SRichard Henderson     x = load_frd0(a->rm1);
4543c3bad4f8SRichard Henderson     y = load_frd0(a->rm2);
4544c3bad4f8SRichard Henderson     z = load_frd0(a->ra3);
4545ebe9383cSRichard Henderson 
4546c3bad4f8SRichard Henderson     if (a->neg) {
4547ad75a51eSRichard Henderson         gen_helper_fmpynfadd_d(x, tcg_env, x, y, z);
4548ebe9383cSRichard Henderson     } else {
4549ad75a51eSRichard Henderson         gen_helper_fmpyfadd_d(x, tcg_env, x, y, z);
4550ebe9383cSRichard Henderson     }
4551ebe9383cSRichard Henderson 
4552c3bad4f8SRichard Henderson     save_frd(a->t, x);
455331234768SRichard Henderson     return nullify_end(ctx);
4554ebe9383cSRichard Henderson }
4555ebe9383cSRichard Henderson 
455638193127SRichard Henderson /* Emulate PDC BTLB, called by SeaBIOS-hppa */
455738193127SRichard Henderson static bool trans_diag_btlb(DisasContext *ctx, arg_diag_btlb *a)
455815da177bSSven Schnelle {
4559cf6b28d4SHelge Deller     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
4560cf6b28d4SHelge Deller #ifndef CONFIG_USER_ONLY
4561ad75a51eSRichard Henderson     nullify_over(ctx);
4562ad75a51eSRichard Henderson     gen_helper_diag_btlb(tcg_env);
4563cf6b28d4SHelge Deller     return nullify_end(ctx);
456438193127SRichard Henderson #endif
456515da177bSSven Schnelle }
456638193127SRichard Henderson 
456738193127SRichard Henderson /* Print char in %r26 to first serial console, used by SeaBIOS-hppa */
456838193127SRichard Henderson static bool trans_diag_cout(DisasContext *ctx, arg_diag_cout *a)
456938193127SRichard Henderson {
457038193127SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
457138193127SRichard Henderson #ifndef CONFIG_USER_ONLY
4572dbca0835SHelge Deller     nullify_over(ctx);
4573dbca0835SHelge Deller     gen_helper_diag_console_output(tcg_env);
4574dbca0835SHelge Deller     return nullify_end(ctx);
4575ad75a51eSRichard Henderson #endif
457638193127SRichard Henderson }
457738193127SRichard Henderson 
45783bdf2081SHelge Deller static bool trans_diag_getshadowregs_pa1(DisasContext *ctx, arg_empty *a)
45793bdf2081SHelge Deller {
45803bdf2081SHelge Deller     return !ctx->is_pa20 && do_getshadowregs(ctx);
45813bdf2081SHelge Deller }
45823bdf2081SHelge Deller 
45833bdf2081SHelge Deller static bool trans_diag_getshadowregs_pa2(DisasContext *ctx, arg_empty *a)
45843bdf2081SHelge Deller {
45853bdf2081SHelge Deller     return ctx->is_pa20 && do_getshadowregs(ctx);
45863bdf2081SHelge Deller }
45873bdf2081SHelge Deller 
45883bdf2081SHelge Deller static bool trans_diag_putshadowregs_pa1(DisasContext *ctx, arg_empty *a)
45893bdf2081SHelge Deller {
45903bdf2081SHelge Deller     return !ctx->is_pa20 && do_putshadowregs(ctx);
45913bdf2081SHelge Deller }
45923bdf2081SHelge Deller 
45933bdf2081SHelge Deller static bool trans_diag_putshadowregs_pa2(DisasContext *ctx, arg_empty *a)
45943bdf2081SHelge Deller {
45953bdf2081SHelge Deller     return ctx->is_pa20 && do_putshadowregs(ctx);
45963bdf2081SHelge Deller }
45973bdf2081SHelge Deller 
459838193127SRichard Henderson static bool trans_diag_unimp(DisasContext *ctx, arg_diag_unimp *a)
459938193127SRichard Henderson {
460038193127SRichard Henderson     CHECK_MOST_PRIVILEGED(EXCP_PRIV_OPR);
4601ad75a51eSRichard Henderson     qemu_log_mask(LOG_UNIMP, "DIAG opcode 0x%04x ignored\n", a->i);
4602ad75a51eSRichard Henderson     return true;
4603ad75a51eSRichard Henderson }
460415da177bSSven Schnelle 
4605b542683dSEmilio G. Cota static void hppa_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
460661766fe9SRichard Henderson {
460751b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4608f764718dSRichard Henderson     int bound;
460961766fe9SRichard Henderson 
461051b061fbSRichard Henderson     ctx->cs = cs;
4611494737b7SRichard Henderson     ctx->tb_flags = ctx->base.tb->flags;
4612bd6243a3SRichard Henderson     ctx->is_pa20 = hppa_is_pa20(cpu_env(cs));
46133d68ee7bSRichard Henderson 
46143d68ee7bSRichard Henderson #ifdef CONFIG_USER_ONLY
4615c01e5dfbSHelge Deller     ctx->privilege = MMU_IDX_TO_PRIV(MMU_USER_IDX);
46163d68ee7bSRichard Henderson     ctx->mmu_idx = MMU_USER_IDX;
4617c01e5dfbSHelge Deller     ctx->iaoq_f = ctx->base.pc_first | ctx->privilege;
4618c01e5dfbSHelge Deller     ctx->iaoq_b = ctx->base.tb->cs_base | ctx->privilege;
4619217d1a5eSRichard Henderson     ctx->unalign = (ctx->tb_flags & TB_FLAG_UNALIGN ? MO_UNALN : MO_ALIGN);
4620c301f34eSRichard Henderson #else
4621494737b7SRichard Henderson     ctx->privilege = (ctx->tb_flags >> TB_FLAG_PRIV_SHIFT) & 3;
4622bb67ec32SRichard Henderson     ctx->mmu_idx = (ctx->tb_flags & PSW_D
4623bb67ec32SRichard Henderson                     ? PRIV_P_TO_MMU_IDX(ctx->privilege, ctx->tb_flags & PSW_P)
4624451d993dSRichard Henderson                     : ctx->tb_flags & PSW_W ? MMU_ABS_W_IDX : MMU_ABS_IDX);
46253d68ee7bSRichard Henderson 
4626c301f34eSRichard Henderson     /* Recover the IAOQ values from the GVA + PRIV.  */
4627c301f34eSRichard Henderson     uint64_t cs_base = ctx->base.tb->cs_base;
4628c301f34eSRichard Henderson     uint64_t iasq_f = cs_base & ~0xffffffffull;
4629c301f34eSRichard Henderson     int32_t diff = cs_base;
4630c301f34eSRichard Henderson 
4631c301f34eSRichard Henderson     ctx->iaoq_f = (ctx->base.pc_first & ~iasq_f) + ctx->privilege;
4632c301f34eSRichard Henderson     ctx->iaoq_b = (diff ? ctx->iaoq_f + diff : -1);
4633c301f34eSRichard Henderson #endif
463451b061fbSRichard Henderson     ctx->iaoq_n = -1;
4635f764718dSRichard Henderson     ctx->iaoq_n_var = NULL;
463661766fe9SRichard Henderson 
4637a4db4a78SRichard Henderson     ctx->zero = tcg_constant_i64(0);
4638a4db4a78SRichard Henderson 
46393d68ee7bSRichard Henderson     /* Bound the number of instructions by those left on the page.  */
46403d68ee7bSRichard Henderson     bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
4641b542683dSEmilio G. Cota     ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
464261766fe9SRichard Henderson }
464361766fe9SRichard Henderson 
464451b061fbSRichard Henderson static void hppa_tr_tb_start(DisasContextBase *dcbase, CPUState *cs)
464551b061fbSRichard Henderson {
464651b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
464761766fe9SRichard Henderson 
46483d68ee7bSRichard Henderson     /* Seed the nullification status from PSW[N], as saved in TB->FLAGS.  */
464951b061fbSRichard Henderson     ctx->null_cond = cond_make_f();
465051b061fbSRichard Henderson     ctx->psw_n_nonzero = false;
4651494737b7SRichard Henderson     if (ctx->tb_flags & PSW_N) {
465251b061fbSRichard Henderson         ctx->null_cond.c = TCG_COND_ALWAYS;
465351b061fbSRichard Henderson         ctx->psw_n_nonzero = true;
4654129e9cc3SRichard Henderson     }
465551b061fbSRichard Henderson     ctx->null_lab = NULL;
465661766fe9SRichard Henderson }
465761766fe9SRichard Henderson 
465851b061fbSRichard Henderson static void hppa_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
465951b061fbSRichard Henderson {
466051b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
466151b061fbSRichard Henderson 
4662f5b5c857SRichard Henderson     tcg_gen_insn_start(ctx->iaoq_f, ctx->iaoq_b, 0);
466324638bd1SRichard Henderson     ctx->insn_start_updated = false;
466451b061fbSRichard Henderson }
466551b061fbSRichard Henderson 
466651b061fbSRichard Henderson static void hppa_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
466751b061fbSRichard Henderson {
466851b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4669b77af26eSRichard Henderson     CPUHPPAState *env = cpu_env(cs);
467051b061fbSRichard Henderson     DisasJumpType ret;
467151b061fbSRichard Henderson 
467251b061fbSRichard Henderson     /* Execute one insn.  */
4673ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
4674c301f34eSRichard Henderson     if (ctx->base.pc_next < TARGET_PAGE_SIZE) {
467531234768SRichard Henderson         do_page_zero(ctx);
467631234768SRichard Henderson         ret = ctx->base.is_jmp;
4677869051eaSRichard Henderson         assert(ret != DISAS_NEXT);
4678ba1d0b44SRichard Henderson     } else
4679ba1d0b44SRichard Henderson #endif
4680ba1d0b44SRichard Henderson     {
468161766fe9SRichard Henderson         /* Always fetch the insn, even if nullified, so that we check
468261766fe9SRichard Henderson            the page permissions for execute.  */
46834e116893SIlya Leoshkevich         uint32_t insn = translator_ldl(env, &ctx->base, ctx->base.pc_next);
468461766fe9SRichard Henderson 
468561766fe9SRichard Henderson         /* Set up the IA queue for the next insn.
468661766fe9SRichard Henderson            This will be overwritten by a branch.  */
468751b061fbSRichard Henderson         if (ctx->iaoq_b == -1) {
468851b061fbSRichard Henderson             ctx->iaoq_n = -1;
4689aac0f603SRichard Henderson             ctx->iaoq_n_var = tcg_temp_new_i64();
46906fd0c7bcSRichard Henderson             tcg_gen_addi_i64(ctx->iaoq_n_var, cpu_iaoq_b, 4);
469161766fe9SRichard Henderson         } else {
469251b061fbSRichard Henderson             ctx->iaoq_n = ctx->iaoq_b + 4;
4693f764718dSRichard Henderson             ctx->iaoq_n_var = NULL;
469461766fe9SRichard Henderson         }
469561766fe9SRichard Henderson 
469651b061fbSRichard Henderson         if (unlikely(ctx->null_cond.c == TCG_COND_ALWAYS)) {
469751b061fbSRichard Henderson             ctx->null_cond.c = TCG_COND_NEVER;
4698869051eaSRichard Henderson             ret = DISAS_NEXT;
4699129e9cc3SRichard Henderson         } else {
47001a19da0dSRichard Henderson             ctx->insn = insn;
470131274b46SRichard Henderson             if (!decode(ctx, insn)) {
470231274b46SRichard Henderson                 gen_illegal(ctx);
470331274b46SRichard Henderson             }
470431234768SRichard Henderson             ret = ctx->base.is_jmp;
470551b061fbSRichard Henderson             assert(ctx->null_lab == NULL);
4706129e9cc3SRichard Henderson         }
470761766fe9SRichard Henderson     }
470861766fe9SRichard Henderson 
47093d68ee7bSRichard Henderson     /* Advance the insn queue.  Note that this check also detects
47103d68ee7bSRichard Henderson        a priority change within the instruction queue.  */
471151b061fbSRichard Henderson     if (ret == DISAS_NEXT && ctx->iaoq_b != ctx->iaoq_f + 4) {
47124e31e68bSRichard Henderson         if (use_goto_tb(ctx, ctx->iaoq_b, ctx->iaoq_n)
4713c301f34eSRichard Henderson             && (ctx->null_cond.c == TCG_COND_NEVER
4714c301f34eSRichard Henderson                 || ctx->null_cond.c == TCG_COND_ALWAYS)) {
471551b061fbSRichard Henderson             nullify_set(ctx, ctx->null_cond.c == TCG_COND_ALWAYS);
471651b061fbSRichard Henderson             gen_goto_tb(ctx, 0, ctx->iaoq_b, ctx->iaoq_n);
471731234768SRichard Henderson             ctx->base.is_jmp = ret = DISAS_NORETURN;
4718129e9cc3SRichard Henderson         } else {
471931234768SRichard Henderson             ctx->base.is_jmp = ret = DISAS_IAQ_N_STALE;
472061766fe9SRichard Henderson         }
4721129e9cc3SRichard Henderson     }
472251b061fbSRichard Henderson     ctx->iaoq_f = ctx->iaoq_b;
472351b061fbSRichard Henderson     ctx->iaoq_b = ctx->iaoq_n;
4724c301f34eSRichard Henderson     ctx->base.pc_next += 4;
472561766fe9SRichard Henderson 
4726c5d0aec2SRichard Henderson     switch (ret) {
4727c5d0aec2SRichard Henderson     case DISAS_NORETURN:
4728c5d0aec2SRichard Henderson     case DISAS_IAQ_N_UPDATED:
4729c5d0aec2SRichard Henderson         break;
4730c5d0aec2SRichard Henderson 
4731c5d0aec2SRichard Henderson     case DISAS_NEXT:
4732c5d0aec2SRichard Henderson     case DISAS_IAQ_N_STALE:
4733c5d0aec2SRichard Henderson     case DISAS_IAQ_N_STALE_EXIT:
473451b061fbSRichard Henderson         if (ctx->iaoq_f == -1) {
473585e6cda0SRichard Henderson             install_iaq_entries(ctx, -1, cpu_iaoq_b,
473685e6cda0SRichard Henderson                                 ctx->iaoq_n, ctx->iaoq_n_var);
4737c301f34eSRichard Henderson #ifndef CONFIG_USER_ONLY
4738c301f34eSRichard Henderson             tcg_gen_mov_i64(cpu_iasq_f, cpu_iasq_b);
4739c301f34eSRichard Henderson #endif
474051b061fbSRichard Henderson             nullify_save(ctx);
4741c5d0aec2SRichard Henderson             ctx->base.is_jmp = (ret == DISAS_IAQ_N_STALE_EXIT
4742c5d0aec2SRichard Henderson                                 ? DISAS_EXIT
4743c5d0aec2SRichard Henderson                                 : DISAS_IAQ_N_UPDATED);
474451b061fbSRichard Henderson         } else if (ctx->iaoq_b == -1) {
4745a0180973SRichard Henderson             copy_iaoq_entry(ctx, cpu_iaoq_b, -1, ctx->iaoq_n_var);
474661766fe9SRichard Henderson         }
4747c5d0aec2SRichard Henderson         break;
4748c5d0aec2SRichard Henderson 
4749c5d0aec2SRichard Henderson     default:
4750c5d0aec2SRichard Henderson         g_assert_not_reached();
4751c5d0aec2SRichard Henderson     }
475261766fe9SRichard Henderson }
475361766fe9SRichard Henderson 
475451b061fbSRichard Henderson static void hppa_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
475551b061fbSRichard Henderson {
475651b061fbSRichard Henderson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
4757e1b5a5edSRichard Henderson     DisasJumpType is_jmp = ctx->base.is_jmp;
475851b061fbSRichard Henderson 
4759e1b5a5edSRichard Henderson     switch (is_jmp) {
4760869051eaSRichard Henderson     case DISAS_NORETURN:
476161766fe9SRichard Henderson         break;
476251b061fbSRichard Henderson     case DISAS_TOO_MANY:
4763869051eaSRichard Henderson     case DISAS_IAQ_N_STALE:
4764e1b5a5edSRichard Henderson     case DISAS_IAQ_N_STALE_EXIT:
476585e6cda0SRichard Henderson         install_iaq_entries(ctx, ctx->iaoq_f, cpu_iaoq_f,
476685e6cda0SRichard Henderson                             ctx->iaoq_b, cpu_iaoq_b);
476751b061fbSRichard Henderson         nullify_save(ctx);
476861766fe9SRichard Henderson         /* FALLTHRU */
4769869051eaSRichard Henderson     case DISAS_IAQ_N_UPDATED:
47708532a14eSRichard Henderson         if (is_jmp != DISAS_IAQ_N_STALE_EXIT) {
47717f11636dSEmilio G. Cota             tcg_gen_lookup_and_goto_ptr();
47728532a14eSRichard Henderson             break;
477361766fe9SRichard Henderson         }
4774c5d0aec2SRichard Henderson         /* FALLTHRU */
4775c5d0aec2SRichard Henderson     case DISAS_EXIT:
4776c5d0aec2SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
477761766fe9SRichard Henderson         break;
477861766fe9SRichard Henderson     default:
477951b061fbSRichard Henderson         g_assert_not_reached();
478061766fe9SRichard Henderson     }
478151b061fbSRichard Henderson }
478261766fe9SRichard Henderson 
47838eb806a7SRichard Henderson static void hppa_tr_disas_log(const DisasContextBase *dcbase,
47848eb806a7SRichard Henderson                               CPUState *cs, FILE *logfile)
478551b061fbSRichard Henderson {
4786c301f34eSRichard Henderson     target_ulong pc = dcbase->pc_first;
478761766fe9SRichard Henderson 
4788ba1d0b44SRichard Henderson #ifdef CONFIG_USER_ONLY
4789ba1d0b44SRichard Henderson     switch (pc) {
47907ad439dfSRichard Henderson     case 0x00:
47918eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x00000000:  (null)\n");
4792ba1d0b44SRichard Henderson         return;
47937ad439dfSRichard Henderson     case 0xb0:
47948eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x000000b0:  light-weight-syscall\n");
4795ba1d0b44SRichard Henderson         return;
47967ad439dfSRichard Henderson     case 0xe0:
47978eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x000000e0:  set-thread-pointer-syscall\n");
4798ba1d0b44SRichard Henderson         return;
47997ad439dfSRichard Henderson     case 0x100:
48008eb806a7SRichard Henderson         fprintf(logfile, "IN:\n0x00000100:  syscall\n");
4801ba1d0b44SRichard Henderson         return;
48027ad439dfSRichard Henderson     }
4803ba1d0b44SRichard Henderson #endif
4804ba1d0b44SRichard Henderson 
48058eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(pc));
48068eb806a7SRichard Henderson     target_disas(logfile, cs, pc, dcbase->tb->size);
480761766fe9SRichard Henderson }
480851b061fbSRichard Henderson 
480951b061fbSRichard Henderson static const TranslatorOps hppa_tr_ops = {
481051b061fbSRichard Henderson     .init_disas_context = hppa_tr_init_disas_context,
481151b061fbSRichard Henderson     .tb_start           = hppa_tr_tb_start,
481251b061fbSRichard Henderson     .insn_start         = hppa_tr_insn_start,
481351b061fbSRichard Henderson     .translate_insn     = hppa_tr_translate_insn,
481451b061fbSRichard Henderson     .tb_stop            = hppa_tr_tb_stop,
481551b061fbSRichard Henderson     .disas_log          = hppa_tr_disas_log,
481651b061fbSRichard Henderson };
481751b061fbSRichard Henderson 
4818597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
481932f0c394SAnton Johansson                            vaddr pc, void *host_pc)
482051b061fbSRichard Henderson {
482151b061fbSRichard Henderson     DisasContext ctx;
4822306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc, &hppa_tr_ops, &ctx.base);
482361766fe9SRichard Henderson }
4824