xref: /openbmc/qemu/target/hexagon/translate.c (revision e8939e80)
18b453a2bSTaylor Simpson /*
2bd983f68STaylor Simpson  *  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
38b453a2bSTaylor Simpson  *
48b453a2bSTaylor Simpson  *  This program is free software; you can redistribute it and/or modify
58b453a2bSTaylor Simpson  *  it under the terms of the GNU General Public License as published by
68b453a2bSTaylor Simpson  *  the Free Software Foundation; either version 2 of the License, or
78b453a2bSTaylor Simpson  *  (at your option) any later version.
88b453a2bSTaylor Simpson  *
98b453a2bSTaylor Simpson  *  This program is distributed in the hope that it will be useful,
108b453a2bSTaylor Simpson  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
118b453a2bSTaylor Simpson  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
128b453a2bSTaylor Simpson  *  GNU General Public License for more details.
138b453a2bSTaylor Simpson  *
148b453a2bSTaylor Simpson  *  You should have received a copy of the GNU General Public License
158b453a2bSTaylor Simpson  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
168b453a2bSTaylor Simpson  */
178b453a2bSTaylor Simpson 
188b453a2bSTaylor Simpson #define QEMU_GENERATE
198b453a2bSTaylor Simpson #include "qemu/osdep.h"
208b453a2bSTaylor Simpson #include "cpu.h"
218b453a2bSTaylor Simpson #include "tcg/tcg-op.h"
22a82dd548STaylor Simpson #include "tcg/tcg-op-gvec.h"
2313e27d1fSRichard Henderson #include "exec/helper-gen.h"
24c213ee2dSRichard Henderson #include "exec/helper-proto.h"
25d654e928SRichard Henderson #include "exec/translation-block.h"
264597463bSPhilippe Mathieu-Daudé #include "exec/cpu_ldst.h"
278b453a2bSTaylor Simpson #include "exec/log.h"
288b453a2bSTaylor Simpson #include "internal.h"
298b453a2bSTaylor Simpson #include "attribs.h"
308b453a2bSTaylor Simpson #include "insn.h"
318b453a2bSTaylor Simpson #include "decode.h"
328b453a2bSTaylor Simpson #include "translate.h"
33d54c5615STaylor Simpson #include "genptr.h"
348b453a2bSTaylor Simpson #include "printinsn.h"
358b453a2bSTaylor Simpson 
36d53106c9SRichard Henderson #define HELPER_H "helper.h"
37d53106c9SRichard Henderson #include "exec/helper-info.c.inc"
38d53106c9SRichard Henderson #undef  HELPER_H
39d53106c9SRichard Henderson 
4010849c26STaylor Simpson #include "analyze_funcs_generated.c.inc"
4110849c26STaylor Simpson 
4210849c26STaylor Simpson typedef void (*AnalyzeInsn)(DisasContext *ctx);
4310849c26STaylor Simpson static const AnalyzeInsn opcode_analyze[XX_LAST_OPCODE] = {
4410849c26STaylor Simpson #define OPCODE(X)    [X] = analyze_##X
4510849c26STaylor Simpson #include "opcodes_def_generated.h.inc"
4610849c26STaylor Simpson #undef OPCODE
4710849c26STaylor Simpson };
4810849c26STaylor Simpson 
498b453a2bSTaylor Simpson TCGv hex_gpr[TOTAL_PER_THREAD_REGS];
508b453a2bSTaylor Simpson TCGv hex_pred[NUM_PREGS];
518b453a2bSTaylor Simpson TCGv hex_slot_cancelled;
526aa4f1d1STaylor Simpson TCGv hex_new_value_usr;
538b453a2bSTaylor Simpson TCGv hex_reg_written[TOTAL_PER_THREAD_REGS];
548b453a2bSTaylor Simpson TCGv hex_store_addr[STORES_MAX];
558b453a2bSTaylor Simpson TCGv hex_store_width[STORES_MAX];
568b453a2bSTaylor Simpson TCGv hex_store_val32[STORES_MAX];
578b453a2bSTaylor Simpson TCGv_i64 hex_store_val64[STORES_MAX];
588b453a2bSTaylor Simpson TCGv hex_llsc_addr;
598b453a2bSTaylor Simpson TCGv hex_llsc_val;
608b453a2bSTaylor Simpson TCGv_i64 hex_llsc_val_i64;
61a82dd548STaylor Simpson TCGv hex_vstore_addr[VSTORES_MAX];
62a82dd548STaylor Simpson TCGv hex_vstore_size[VSTORES_MAX];
63a82dd548STaylor Simpson TCGv hex_vstore_pending[VSTORES_MAX];
648b453a2bSTaylor Simpson 
658b453a2bSTaylor Simpson static const char * const hexagon_prednames[] = {
668b453a2bSTaylor Simpson   "p0", "p1", "p2", "p3"
678b453a2bSTaylor Simpson };
688b453a2bSTaylor Simpson 
ctx_future_vreg_off(DisasContext * ctx,int regnum,int num,bool alloc_ok)69a82dd548STaylor Simpson intptr_t ctx_future_vreg_off(DisasContext *ctx, int regnum,
70a82dd548STaylor Simpson                           int num, bool alloc_ok)
71a82dd548STaylor Simpson {
72a82dd548STaylor Simpson     intptr_t offset;
73a82dd548STaylor Simpson 
74b8552985STaylor Simpson     if (!ctx->need_commit) {
75b8552985STaylor Simpson         return offsetof(CPUHexagonState, VRegs[regnum]);
76b8552985STaylor Simpson     }
77b8552985STaylor Simpson 
78a82dd548STaylor Simpson     /* See if it is already allocated */
79a82dd548STaylor Simpson     for (int i = 0; i < ctx->future_vregs_idx; i++) {
80a82dd548STaylor Simpson         if (ctx->future_vregs_num[i] == regnum) {
81a82dd548STaylor Simpson             return offsetof(CPUHexagonState, future_VRegs[i]);
82a82dd548STaylor Simpson         }
83a82dd548STaylor Simpson     }
84a82dd548STaylor Simpson 
85a82dd548STaylor Simpson     g_assert(alloc_ok);
86a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, future_VRegs[ctx->future_vregs_idx]);
87a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
88a82dd548STaylor Simpson         ctx->future_vregs_num[ctx->future_vregs_idx + i] = regnum++;
89a82dd548STaylor Simpson     }
90a82dd548STaylor Simpson     ctx->future_vregs_idx += num;
91a82dd548STaylor Simpson     g_assert(ctx->future_vregs_idx <= VECTOR_TEMPS_MAX);
92a82dd548STaylor Simpson     return offset;
93a82dd548STaylor Simpson }
94a82dd548STaylor Simpson 
ctx_tmp_vreg_off(DisasContext * ctx,int regnum,int num,bool alloc_ok)95a82dd548STaylor Simpson intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,
96a82dd548STaylor Simpson                           int num, bool alloc_ok)
97a82dd548STaylor Simpson {
98a82dd548STaylor Simpson     intptr_t offset;
99a82dd548STaylor Simpson 
100a82dd548STaylor Simpson     /* See if it is already allocated */
101a82dd548STaylor Simpson     for (int i = 0; i < ctx->tmp_vregs_idx; i++) {
102a82dd548STaylor Simpson         if (ctx->tmp_vregs_num[i] == regnum) {
103a82dd548STaylor Simpson             return offsetof(CPUHexagonState, tmp_VRegs[i]);
104a82dd548STaylor Simpson         }
105a82dd548STaylor Simpson     }
106a82dd548STaylor Simpson 
107a82dd548STaylor Simpson     g_assert(alloc_ok);
108a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, tmp_VRegs[ctx->tmp_vregs_idx]);
109a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
110a82dd548STaylor Simpson         ctx->tmp_vregs_num[ctx->tmp_vregs_idx + i] = regnum++;
111a82dd548STaylor Simpson     }
112a82dd548STaylor Simpson     ctx->tmp_vregs_idx += num;
113a82dd548STaylor Simpson     g_assert(ctx->tmp_vregs_idx <= VECTOR_TEMPS_MAX);
114a82dd548STaylor Simpson     return offset;
115a82dd548STaylor Simpson }
116a82dd548STaylor Simpson 
gen_exception_raw(int excp)117743debbcSTaylor Simpson static void gen_exception_raw(int excp)
1188b453a2bSTaylor Simpson {
119ad75a51eSRichard Henderson     gen_helper_raise_exception(tcg_env, tcg_constant_i32(excp));
1208b453a2bSTaylor Simpson }
1218b453a2bSTaylor Simpson 
gen_exec_counters(DisasContext * ctx)122743debbcSTaylor Simpson static void gen_exec_counters(DisasContext *ctx)
1238b453a2bSTaylor Simpson {
124743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT],
125743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_PKT_CNT], ctx->num_packets);
126743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_INSN_CNT],
127743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns);
128a82dd548STaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT],
129a82dd548STaylor Simpson                     hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns);
130743debbcSTaylor Simpson }
131743debbcSTaylor Simpson 
use_goto_tb(DisasContext * ctx,target_ulong dest)1321b9a7f2aSTaylor Simpson static bool use_goto_tb(DisasContext *ctx, target_ulong dest)
1331b9a7f2aSTaylor Simpson {
1341b9a7f2aSTaylor Simpson     return translator_use_goto_tb(&ctx->base, dest);
1351b9a7f2aSTaylor Simpson }
1361b9a7f2aSTaylor Simpson 
gen_goto_tb(DisasContext * ctx,int idx,target_ulong dest,bool move_to_pc)137bee1fc56SMatheus Tavares Bernardino static void gen_goto_tb(DisasContext *ctx, int idx, target_ulong dest, bool
138bee1fc56SMatheus Tavares Bernardino                         move_to_pc)
1391b9a7f2aSTaylor Simpson {
1401b9a7f2aSTaylor Simpson     if (use_goto_tb(ctx, dest)) {
1411b9a7f2aSTaylor Simpson         tcg_gen_goto_tb(idx);
142bee1fc56SMatheus Tavares Bernardino         if (move_to_pc) {
1431b9a7f2aSTaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
144bee1fc56SMatheus Tavares Bernardino         }
1451b9a7f2aSTaylor Simpson         tcg_gen_exit_tb(ctx->base.tb, idx);
1461b9a7f2aSTaylor Simpson     } else {
147bee1fc56SMatheus Tavares Bernardino         if (move_to_pc) {
1481b9a7f2aSTaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
149bee1fc56SMatheus Tavares Bernardino         }
1501b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1511b9a7f2aSTaylor Simpson     }
1521b9a7f2aSTaylor Simpson }
1531b9a7f2aSTaylor Simpson 
gen_end_tb(DisasContext * ctx)154743debbcSTaylor Simpson static void gen_end_tb(DisasContext *ctx)
155743debbcSTaylor Simpson {
156564b2040STaylor Simpson     Packet *pkt = ctx->pkt;
157564b2040STaylor Simpson 
158743debbcSTaylor Simpson     gen_exec_counters(ctx);
1591b9a7f2aSTaylor Simpson 
1601b9a7f2aSTaylor Simpson     if (ctx->branch_cond != TCG_COND_NEVER) {
1611b9a7f2aSTaylor Simpson         if (ctx->branch_cond != TCG_COND_ALWAYS) {
1621b9a7f2aSTaylor Simpson             TCGLabel *skip = gen_new_label();
1630fc56c43STaylor Simpson             tcg_gen_brcondi_tl(ctx->branch_cond, ctx->branch_taken, 0, skip);
164bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 0, ctx->branch_dest, true);
1651b9a7f2aSTaylor Simpson             gen_set_label(skip);
166bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 1, ctx->next_PC, false);
1671b9a7f2aSTaylor Simpson         } else {
168bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 0, ctx->branch_dest, true);
1691b9a7f2aSTaylor Simpson         }
170564b2040STaylor Simpson     } else if (ctx->is_tight_loop &&
171564b2040STaylor Simpson                pkt->insn[pkt->num_insns - 1].opcode == J2_endloop0) {
172564b2040STaylor Simpson         /*
173564b2040STaylor Simpson          * When we're in a tight loop, we defer the endloop0 processing
174564b2040STaylor Simpson          * to take advantage of direct block chaining
175564b2040STaylor Simpson          */
176564b2040STaylor Simpson         TCGLabel *skip = gen_new_label();
177564b2040STaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC0], 1, skip);
178564b2040STaylor Simpson         tcg_gen_subi_tl(hex_gpr[HEX_REG_LC0], hex_gpr[HEX_REG_LC0], 1);
179bee1fc56SMatheus Tavares Bernardino         gen_goto_tb(ctx, 0, ctx->base.tb->pc, true);
180564b2040STaylor Simpson         gen_set_label(skip);
181bee1fc56SMatheus Tavares Bernardino         gen_goto_tb(ctx, 1, ctx->next_PC, false);
1821b9a7f2aSTaylor Simpson     } else {
1831b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1841b9a7f2aSTaylor Simpson     }
1851b9a7f2aSTaylor Simpson 
186743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
187743debbcSTaylor Simpson }
188743debbcSTaylor Simpson 
gen_exception_end_tb(DisasContext * ctx,int excp)189743debbcSTaylor Simpson static void gen_exception_end_tb(DisasContext *ctx, int excp)
190743debbcSTaylor Simpson {
191743debbcSTaylor Simpson     gen_exec_counters(ctx);
192613653e5STaylor Simpson     tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC);
193743debbcSTaylor Simpson     gen_exception_raw(excp);
194743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
195743debbcSTaylor Simpson 
1968b453a2bSTaylor Simpson }
1978b453a2bSTaylor Simpson 
1988b453a2bSTaylor Simpson #define PACKET_BUFFER_LEN              1028
print_pkt(Packet * pkt)1998b453a2bSTaylor Simpson static void print_pkt(Packet *pkt)
2008b453a2bSTaylor Simpson {
2018b453a2bSTaylor Simpson     GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
2028b453a2bSTaylor Simpson     snprint_a_pkt_debug(buf, pkt);
2038b453a2bSTaylor Simpson     HEX_DEBUG_LOG("%s", buf->str);
2048b453a2bSTaylor Simpson     g_string_free(buf, true);
2058b453a2bSTaylor Simpson }
20685580a65STaylor Simpson #define HEX_DEBUG_PRINT_PKT(pkt) \
20785580a65STaylor Simpson     do { \
20885580a65STaylor Simpson         if (HEX_DEBUG) { \
20985580a65STaylor Simpson             print_pkt(pkt); \
21085580a65STaylor Simpson         } \
21185580a65STaylor Simpson     } while (0)
2128b453a2bSTaylor Simpson 
read_packet_words(CPUHexagonState * env,DisasContext * ctx,uint32_t words[])2138b453a2bSTaylor Simpson static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
2148b453a2bSTaylor Simpson                              uint32_t words[])
2158b453a2bSTaylor Simpson {
2168b453a2bSTaylor Simpson     bool found_end = false;
2178b453a2bSTaylor Simpson     int nwords, max_words;
2188b453a2bSTaylor Simpson 
2198b453a2bSTaylor Simpson     memset(words, 0, PACKET_WORDS_MAX * sizeof(uint32_t));
2208b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
221a27c100cSTaylor Simpson         words[nwords] =
2224e116893SIlya Leoshkevich             translator_ldl(env, &ctx->base,
2234e116893SIlya Leoshkevich                            ctx->base.pc_next + nwords * sizeof(uint32_t));
2248b453a2bSTaylor Simpson         found_end = is_packet_end(words[nwords]);
2258b453a2bSTaylor Simpson     }
2268b453a2bSTaylor Simpson     if (!found_end) {
2278b453a2bSTaylor Simpson         /* Read too many words without finding the end */
2288b453a2bSTaylor Simpson         return 0;
2298b453a2bSTaylor Simpson     }
2308b453a2bSTaylor Simpson 
2318b453a2bSTaylor Simpson     /* Check for page boundary crossing */
2328b453a2bSTaylor Simpson     max_words = -(ctx->base.pc_next | TARGET_PAGE_MASK) / sizeof(uint32_t);
2338b453a2bSTaylor Simpson     if (nwords > max_words) {
2348b453a2bSTaylor Simpson         /* We can only cross a page boundary at the beginning of a TB */
2358b453a2bSTaylor Simpson         g_assert(ctx->base.num_insns == 1);
2368b453a2bSTaylor Simpson     }
2378b453a2bSTaylor Simpson 
23885c19af6SAnton Johansson     HEX_DEBUG_LOG("decode_packet: pc = 0x%" VADDR_PRIx "\n",
23985c19af6SAnton Johansson                   ctx->base.pc_next);
2408b453a2bSTaylor Simpson     HEX_DEBUG_LOG("    words = { ");
2418b453a2bSTaylor Simpson     for (int i = 0; i < nwords; i++) {
2428b453a2bSTaylor Simpson         HEX_DEBUG_LOG("0x%x, ", words[i]);
2438b453a2bSTaylor Simpson     }
2448b453a2bSTaylor Simpson     HEX_DEBUG_LOG("}\n");
2458b453a2bSTaylor Simpson 
2468b453a2bSTaylor Simpson     return nwords;
2478b453a2bSTaylor Simpson }
2488b453a2bSTaylor Simpson 
check_for_attrib(Packet * pkt,int attrib)249d54c5615STaylor Simpson static bool check_for_attrib(Packet *pkt, int attrib)
2508b453a2bSTaylor Simpson {
2518b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2528b453a2bSTaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, attrib)) {
2538b453a2bSTaylor Simpson             return true;
2548b453a2bSTaylor Simpson         }
2558b453a2bSTaylor Simpson     }
2568b453a2bSTaylor Simpson     return false;
2578b453a2bSTaylor Simpson }
2588b453a2bSTaylor Simpson 
need_slot_cancelled(Packet * pkt)2598b453a2bSTaylor Simpson static bool need_slot_cancelled(Packet *pkt)
2608b453a2bSTaylor Simpson {
261c2b33d0bSTaylor Simpson     /* We only need slot_cancelled for conditional store instructions */
2627b84fd04STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2637b84fd04STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
2647b84fd04STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) &&
265c2b33d0bSTaylor Simpson             GET_ATTRIB(opcode, A_SCALAR_STORE)) {
2667b84fd04STaylor Simpson             return true;
2677b84fd04STaylor Simpson         }
2687b84fd04STaylor Simpson     }
2697b84fd04STaylor Simpson     return false;
2708b453a2bSTaylor Simpson }
2718b453a2bSTaylor Simpson 
need_next_PC(DisasContext * ctx)272613653e5STaylor Simpson static bool need_next_PC(DisasContext *ctx)
273613653e5STaylor Simpson {
274613653e5STaylor Simpson     Packet *pkt = ctx->pkt;
275613653e5STaylor Simpson 
276613653e5STaylor Simpson     /* Check for conditional control flow or HW loop end */
277613653e5STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
278613653e5STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
279613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) && GET_ATTRIB(opcode, A_COF)) {
280613653e5STaylor Simpson             return true;
281613653e5STaylor Simpson         }
282613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_HWLOOP0_END) ||
283613653e5STaylor Simpson             GET_ATTRIB(opcode, A_HWLOOP1_END)) {
284613653e5STaylor Simpson             return true;
285613653e5STaylor Simpson         }
286613653e5STaylor Simpson     }
287613653e5STaylor Simpson     return false;
288613653e5STaylor Simpson }
289613653e5STaylor Simpson 
29010849c26STaylor Simpson /*
29110849c26STaylor Simpson  * The opcode_analyze functions mark most of the writes in a packet
29210849c26STaylor Simpson  * However, there are some implicit writes marked as attributes
29310849c26STaylor Simpson  * of the applicable instructions.
29410849c26STaylor Simpson  */
mark_implicit_reg_write(DisasContext * ctx,int attrib,int rnum)29510849c26STaylor Simpson static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
29610849c26STaylor Simpson {
29710849c26STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
29810849c26STaylor Simpson     if (GET_ATTRIB(opcode, attrib)) {
29910849c26STaylor Simpson         /*
30010849c26STaylor Simpson          * USR is used to set overflow and FP exceptions,
30110849c26STaylor Simpson          * so treat it as conditional
30210849c26STaylor Simpson          */
30310849c26STaylor Simpson         bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) ||
30410849c26STaylor Simpson                              rnum == HEX_REG_USR;
30510849c26STaylor Simpson 
30610849c26STaylor Simpson         /* LC0/LC1 is conditionally written by endloop instructions */
30710849c26STaylor Simpson         if ((rnum == HEX_REG_LC0 || rnum == HEX_REG_LC1) &&
30810849c26STaylor Simpson             (opcode == J2_endloop0 ||
30910849c26STaylor Simpson              opcode == J2_endloop1 ||
31010849c26STaylor Simpson              opcode == J2_endloop01)) {
31110849c26STaylor Simpson             is_predicated = true;
31210849c26STaylor Simpson         }
31310849c26STaylor Simpson 
31410849c26STaylor Simpson         ctx_log_reg_write(ctx, rnum, is_predicated);
31510849c26STaylor Simpson     }
31610849c26STaylor Simpson }
31710849c26STaylor Simpson 
mark_implicit_reg_writes(DisasContext * ctx)31810849c26STaylor Simpson static void mark_implicit_reg_writes(DisasContext *ctx)
31910849c26STaylor Simpson {
32010849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP,  HEX_REG_FP);
32110849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SP,  HEX_REG_SP);
32210849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LR,  HEX_REG_LR);
32310849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC0, HEX_REG_LC0);
32410849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0);
32510849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1);
32610849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1);
32710849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR);
32810849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR);
32910849c26STaylor Simpson }
33010849c26STaylor Simpson 
mark_implicit_pred_write(DisasContext * ctx,int attrib,int pnum)33110849c26STaylor Simpson static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum)
33210849c26STaylor Simpson {
33310849c26STaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
33410849c26STaylor Simpson         ctx_log_pred_write(ctx, pnum);
33510849c26STaylor Simpson     }
33610849c26STaylor Simpson }
33710849c26STaylor Simpson 
mark_implicit_pred_writes(DisasContext * ctx)33810849c26STaylor Simpson static void mark_implicit_pred_writes(DisasContext *ctx)
33910849c26STaylor Simpson {
34010849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P0, 0);
34110849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P1, 1);
34210849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P2, 2);
34310849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P3, 3);
34410849c26STaylor Simpson }
34510849c26STaylor Simpson 
pkt_raises_exception(Packet * pkt)346d54c5615STaylor Simpson static bool pkt_raises_exception(Packet *pkt)
347d54c5615STaylor Simpson {
348d54c5615STaylor Simpson     if (check_for_attrib(pkt, A_LOAD) ||
349d54c5615STaylor Simpson         check_for_attrib(pkt, A_STORE)) {
350d54c5615STaylor Simpson         return true;
351d54c5615STaylor Simpson     }
352d54c5615STaylor Simpson     return false;
353d54c5615STaylor Simpson }
354d54c5615STaylor Simpson 
need_commit(DisasContext * ctx)355d54c5615STaylor Simpson static bool need_commit(DisasContext *ctx)
356d54c5615STaylor Simpson {
357d54c5615STaylor Simpson     Packet *pkt = ctx->pkt;
358d54c5615STaylor Simpson 
359d54c5615STaylor Simpson     /*
360d54c5615STaylor Simpson      * If the short-circuit property is set to false, we'll always do the commit
361d54c5615STaylor Simpson      */
362d54c5615STaylor Simpson     if (!ctx->short_circuit) {
363d54c5615STaylor Simpson         return true;
364d54c5615STaylor Simpson     }
365d54c5615STaylor Simpson 
366d54c5615STaylor Simpson     if (pkt_raises_exception(pkt)) {
367d54c5615STaylor Simpson         return true;
368d54c5615STaylor Simpson     }
369d54c5615STaylor Simpson 
370d54c5615STaylor Simpson     /* Registers with immutability flags require new_value */
371d54c5615STaylor Simpson     for (int i = 0; i < ctx->reg_log_idx; i++) {
372d54c5615STaylor Simpson         int rnum = ctx->reg_log[i];
373d54c5615STaylor Simpson         if (reg_immut_masks[rnum]) {
374d54c5615STaylor Simpson             return true;
375d54c5615STaylor Simpson         }
376d54c5615STaylor Simpson     }
377d54c5615STaylor Simpson 
378d54c5615STaylor Simpson     /* Floating point instructions are hard-coded to use new_value */
379d54c5615STaylor Simpson     if (check_for_attrib(pkt, A_FPOP)) {
380d54c5615STaylor Simpson         return true;
381d54c5615STaylor Simpson     }
382d54c5615STaylor Simpson 
383763d2ce7STaylor Simpson     if (ctx->read_after_write || ctx->has_hvx_overlap) {
384d54c5615STaylor Simpson         return true;
385d54c5615STaylor Simpson     }
386455e169dSTaylor Simpson 
387d54c5615STaylor Simpson     return false;
388d54c5615STaylor Simpson }
389d54c5615STaylor Simpson 
mark_implicit_pred_read(DisasContext * ctx,int attrib,int pnum)390b9f0326bSTaylor Simpson static void mark_implicit_pred_read(DisasContext *ctx, int attrib, int pnum)
391b9f0326bSTaylor Simpson {
392b9f0326bSTaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
393b9f0326bSTaylor Simpson         ctx_log_pred_read(ctx, pnum);
394b9f0326bSTaylor Simpson     }
395b9f0326bSTaylor Simpson }
396b9f0326bSTaylor Simpson 
mark_implicit_pred_reads(DisasContext * ctx)397b9f0326bSTaylor Simpson static void mark_implicit_pred_reads(DisasContext *ctx)
398b9f0326bSTaylor Simpson {
399b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P0, 0);
400b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P1, 1);
401b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P3, 2);
402b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P3, 3);
403b9f0326bSTaylor Simpson }
404b9f0326bSTaylor Simpson 
analyze_packet(DisasContext * ctx)40510849c26STaylor Simpson static void analyze_packet(DisasContext *ctx)
40610849c26STaylor Simpson {
40710849c26STaylor Simpson     Packet *pkt = ctx->pkt;
408bd983f68STaylor Simpson     ctx->read_after_write = false;
409763d2ce7STaylor Simpson     ctx->has_hvx_overlap = false;
41010849c26STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
41110849c26STaylor Simpson         Insn *insn = &pkt->insn[i];
41210849c26STaylor Simpson         ctx->insn = insn;
41310849c26STaylor Simpson         if (opcode_analyze[insn->opcode]) {
41410849c26STaylor Simpson             opcode_analyze[insn->opcode](ctx);
41510849c26STaylor Simpson         }
41610849c26STaylor Simpson         mark_implicit_reg_writes(ctx);
41710849c26STaylor Simpson         mark_implicit_pred_writes(ctx);
418b9f0326bSTaylor Simpson         mark_implicit_pred_reads(ctx);
41910849c26STaylor Simpson     }
420d54c5615STaylor Simpson 
421d54c5615STaylor Simpson     ctx->need_commit = need_commit(ctx);
42210849c26STaylor Simpson }
42310849c26STaylor Simpson 
gen_start_packet(DisasContext * ctx)4241e536334STaylor Simpson static void gen_start_packet(DisasContext *ctx)
4258b453a2bSTaylor Simpson {
4261e536334STaylor Simpson     Packet *pkt = ctx->pkt;
4278b453a2bSTaylor Simpson     target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes;
4288b453a2bSTaylor Simpson     int i;
4298b453a2bSTaylor Simpson 
4308b453a2bSTaylor Simpson     /* Clear out the disassembly context */
431613653e5STaylor Simpson     ctx->next_PC = next_PC;
4328b453a2bSTaylor Simpson     ctx->reg_log_idx = 0;
4338b453a2bSTaylor Simpson     bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS);
43410849c26STaylor Simpson     bitmap_zero(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
4358b453a2bSTaylor Simpson     ctx->preg_log_idx = 0;
4366c677c60STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
437a82dd548STaylor Simpson     ctx->future_vregs_idx = 0;
438a82dd548STaylor Simpson     ctx->tmp_vregs_idx = 0;
439a82dd548STaylor Simpson     ctx->vreg_log_idx = 0;
440763d2ce7STaylor Simpson     bitmap_zero(ctx->vregs_written, NUM_VREGS);
441a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated_tmp, NUM_VREGS);
442a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated, NUM_VREGS);
443a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_select, NUM_VREGS);
4444d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_future_vregs, NUM_VREGS);
4454d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_tmp_vregs, NUM_VREGS);
446763d2ce7STaylor Simpson     bitmap_zero(ctx->qregs_written, NUM_QREGS);
447a82dd548STaylor Simpson     ctx->qreg_log_idx = 0;
4488b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
4498b453a2bSTaylor Simpson         ctx->store_width[i] = 0;
4508b453a2bSTaylor Simpson     }
45192cfa25fSTaylor Simpson     ctx->s1_store_processed = false;
452a82dd548STaylor Simpson     ctx->pre_commit = true;
4534ff56764STaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
4544ff56764STaylor Simpson         ctx->new_value[i] = NULL;
4554ff56764STaylor Simpson     }
456e22edc7cSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
457e22edc7cSTaylor Simpson         ctx->new_pred_value[i] = NULL;
458e22edc7cSTaylor Simpson     }
4598b453a2bSTaylor Simpson 
46010849c26STaylor Simpson     analyze_packet(ctx);
46110849c26STaylor Simpson 
46210849c26STaylor Simpson     /*
46310849c26STaylor Simpson      * pregs_written is used both in the analyze phase as well as the code
46410849c26STaylor Simpson      * gen phase, so clear it again.
46510849c26STaylor Simpson      */
46610849c26STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
46710849c26STaylor Simpson 
46885580a65STaylor Simpson     if (HEX_DEBUG) {
4698b453a2bSTaylor Simpson         /* Handy place to set a breakpoint before the packet executes */
470ad75a51eSRichard Henderson         gen_helper_debug_start_packet(tcg_env);
47185580a65STaylor Simpson     }
4728b453a2bSTaylor Simpson 
4738b453a2bSTaylor Simpson     /* Initialize the runtime state for packet semantics */
4748b453a2bSTaylor Simpson     if (need_slot_cancelled(pkt)) {
4758b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_slot_cancelled, 0);
4768b453a2bSTaylor Simpson     }
4770fc56c43STaylor Simpson     ctx->branch_taken = NULL;
4788b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
4790fc56c43STaylor Simpson         ctx->branch_taken = tcg_temp_new();
480fb67c2bfSTaylor Simpson         if (pkt->pkt_has_multi_cof) {
4810fc56c43STaylor Simpson             tcg_gen_movi_tl(ctx->branch_taken, 0);
482fb67c2bfSTaylor Simpson         }
483613653e5STaylor Simpson         if (need_next_PC(ctx)) {
484613653e5STaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], next_PC);
485613653e5STaylor Simpson         }
4868b453a2bSTaylor Simpson     }
48725e1d87dSTaylor Simpson     if (HEX_DEBUG) {
488842b206fSTaylor Simpson         ctx->pred_written = tcg_temp_new();
489842b206fSTaylor Simpson         tcg_gen_movi_tl(ctx->pred_written, 0);
4908b453a2bSTaylor Simpson     }
491a82dd548STaylor Simpson 
4926aa4f1d1STaylor Simpson     /* Preload the predicated registers into get_result_gpr(ctx, i) */
493d54c5615STaylor Simpson     if (ctx->need_commit &&
494d54c5615STaylor Simpson         !bitmap_empty(ctx->predicated_regs, TOTAL_PER_THREAD_REGS)) {
49520c34a92SBrian Cain         i = find_first_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
49610849c26STaylor Simpson         while (i < TOTAL_PER_THREAD_REGS) {
4976aa4f1d1STaylor Simpson             tcg_gen_mov_tl(get_result_gpr(ctx, i), hex_gpr[i]);
49810849c26STaylor Simpson             i = find_next_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS,
49910849c26STaylor Simpson                               i + 1);
50010849c26STaylor Simpson         }
50110849c26STaylor Simpson     }
50210849c26STaylor Simpson 
50325e1d87dSTaylor Simpson     /*
5042babbd93SMatheus Tavares Bernardino      * Preload the predicated pred registers into ctx->new_pred_value[pred_num]
50525e1d87dSTaylor Simpson      * Only endloop instructions conditionally write to pred registers
50625e1d87dSTaylor Simpson      */
507455e169dSTaylor Simpson     if (ctx->need_commit && pkt->pkt_has_endloop) {
50820c34a92SBrian Cain         for (i = 0; i < ctx->preg_log_idx; i++) {
50925e1d87dSTaylor Simpson             int pred_num = ctx->preg_log[i];
510e22edc7cSTaylor Simpson             ctx->new_pred_value[pred_num] = tcg_temp_new();
511e22edc7cSTaylor Simpson             tcg_gen_mov_tl(ctx->new_pred_value[pred_num], hex_pred[pred_num]);
51225e1d87dSTaylor Simpson         }
51325e1d87dSTaylor Simpson     }
51425e1d87dSTaylor Simpson 
5154d6f8420STaylor Simpson     /* Preload the predicated HVX registers into future_VRegs and tmp_VRegs */
5164d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_future_vregs, NUM_VREGS)) {
51720c34a92SBrian Cain         i = find_first_bit(ctx->predicated_future_vregs, NUM_VREGS);
5184d6f8420STaylor Simpson         while (i < NUM_VREGS) {
5194d6f8420STaylor Simpson             const intptr_t VdV_off =
5204d6f8420STaylor Simpson                 ctx_future_vreg_off(ctx, i, 1, true);
5214d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
5224d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
5234d6f8420STaylor Simpson                              src_off,
5244d6f8420STaylor Simpson                              sizeof(MMVector),
5254d6f8420STaylor Simpson                              sizeof(MMVector));
5264d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_future_vregs, NUM_VREGS, i + 1);
5274d6f8420STaylor Simpson         }
5284d6f8420STaylor Simpson     }
5294d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_tmp_vregs, NUM_VREGS)) {
53020c34a92SBrian Cain         i = find_first_bit(ctx->predicated_tmp_vregs, NUM_VREGS);
5314d6f8420STaylor Simpson         while (i < NUM_VREGS) {
5324d6f8420STaylor Simpson             const intptr_t VdV_off =
5334d6f8420STaylor Simpson                 ctx_tmp_vreg_off(ctx, i, 1, true);
5344d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
5354d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
5364d6f8420STaylor Simpson                              src_off,
5374d6f8420STaylor Simpson                              sizeof(MMVector),
5384d6f8420STaylor Simpson                              sizeof(MMVector));
5394d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_tmp_vregs, NUM_VREGS, i + 1);
5404d6f8420STaylor Simpson         }
5414d6f8420STaylor Simpson     }
542a82dd548STaylor Simpson }
543a82dd548STaylor Simpson 
is_gather_store_insn(DisasContext * ctx)5441e536334STaylor Simpson bool is_gather_store_insn(DisasContext *ctx)
545a82dd548STaylor Simpson {
5461e536334STaylor Simpson     Packet *pkt = ctx->pkt;
5471e536334STaylor Simpson     Insn *insn = ctx->insn;
548a82dd548STaylor Simpson     if (GET_ATTRIB(insn->opcode, A_CVI_NEW) &&
549a82dd548STaylor Simpson         insn->new_value_producer_slot == 1) {
550a82dd548STaylor Simpson         /* Look for gather instruction */
551a82dd548STaylor Simpson         for (int i = 0; i < pkt->num_insns; i++) {
552a82dd548STaylor Simpson             Insn *in = &pkt->insn[i];
553a82dd548STaylor Simpson             if (GET_ATTRIB(in->opcode, A_CVI_GATHER) && in->slot == 1) {
554a82dd548STaylor Simpson                 return true;
555a82dd548STaylor Simpson             }
556a82dd548STaylor Simpson         }
557a82dd548STaylor Simpson     }
558a82dd548STaylor Simpson     return false;
5598b453a2bSTaylor Simpson }
5608b453a2bSTaylor Simpson 
mark_store_width(DisasContext * ctx)5611e536334STaylor Simpson static void mark_store_width(DisasContext *ctx)
562661ad999STaylor Simpson {
5631e536334STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
5641e536334STaylor Simpson     uint32_t slot = ctx->insn->slot;
565661ad999STaylor Simpson     uint8_t width = 0;
566661ad999STaylor Simpson 
567661ad999STaylor Simpson     if (GET_ATTRIB(opcode, A_SCALAR_STORE)) {
568406c74f2STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_0B)) {
569406c74f2STaylor Simpson             return;
570406c74f2STaylor Simpson         }
571661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_1B)) {
572661ad999STaylor Simpson             width |= 1;
573661ad999STaylor Simpson         }
574661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_2B)) {
575661ad999STaylor Simpson             width |= 2;
576661ad999STaylor Simpson         }
577661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_4B)) {
578661ad999STaylor Simpson             width |= 4;
579661ad999STaylor Simpson         }
580661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_8B)) {
581661ad999STaylor Simpson             width |= 8;
582661ad999STaylor Simpson         }
583661ad999STaylor Simpson         tcg_debug_assert(is_power_of_2(width));
584661ad999STaylor Simpson         ctx->store_width[slot] = width;
585661ad999STaylor Simpson     }
586661ad999STaylor Simpson }
587661ad999STaylor Simpson 
gen_insn(DisasContext * ctx)5881e536334STaylor Simpson static void gen_insn(DisasContext *ctx)
5898b453a2bSTaylor Simpson {
5901e536334STaylor Simpson     if (ctx->insn->generate) {
5911e536334STaylor Simpson         ctx->insn->generate(ctx);
5921e536334STaylor Simpson         mark_store_width(ctx);
5938b453a2bSTaylor Simpson     } else {
594743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_OPCODE);
5958b453a2bSTaylor Simpson     }
5968b453a2bSTaylor Simpson }
5978b453a2bSTaylor Simpson 
5988b453a2bSTaylor Simpson /*
5998b453a2bSTaylor Simpson  * Helpers for generating the packet commit
6008b453a2bSTaylor Simpson  */
gen_reg_writes(DisasContext * ctx)6018b453a2bSTaylor Simpson static void gen_reg_writes(DisasContext *ctx)
6028b453a2bSTaylor Simpson {
6038b453a2bSTaylor Simpson     int i;
6048b453a2bSTaylor Simpson 
605d54c5615STaylor Simpson     /* Early exit if not needed */
606d54c5615STaylor Simpson     if (!ctx->need_commit) {
607d54c5615STaylor Simpson         return;
608d54c5615STaylor Simpson     }
609d54c5615STaylor Simpson 
6108b453a2bSTaylor Simpson     for (i = 0; i < ctx->reg_log_idx; i++) {
6118b453a2bSTaylor Simpson         int reg_num = ctx->reg_log[i];
6128b453a2bSTaylor Simpson 
6136aa4f1d1STaylor Simpson         tcg_gen_mov_tl(hex_gpr[reg_num], get_result_gpr(ctx, reg_num));
614564b2040STaylor Simpson 
615564b2040STaylor Simpson         /*
616564b2040STaylor Simpson          * ctx->is_tight_loop is set when SA0 points to the beginning of the TB.
617564b2040STaylor Simpson          * If we write to SA0, we have to turn off tight loop handling.
618564b2040STaylor Simpson          */
619564b2040STaylor Simpson         if (reg_num == HEX_REG_SA0) {
620564b2040STaylor Simpson             ctx->is_tight_loop = false;
621564b2040STaylor Simpson         }
6228b453a2bSTaylor Simpson     }
6238b453a2bSTaylor Simpson }
6248b453a2bSTaylor Simpson 
gen_pred_writes(DisasContext * ctx)6251e536334STaylor Simpson static void gen_pred_writes(DisasContext *ctx)
6268b453a2bSTaylor Simpson {
627455e169dSTaylor Simpson     /* Early exit if not needed or the log is empty */
628455e169dSTaylor Simpson     if (!ctx->need_commit || !ctx->preg_log_idx) {
6298b453a2bSTaylor Simpson         return;
6308b453a2bSTaylor Simpson     }
6318b453a2bSTaylor Simpson 
63225e1d87dSTaylor Simpson     for (int i = 0; i < ctx->preg_log_idx; i++) {
6338b453a2bSTaylor Simpson         int pred_num = ctx->preg_log[i];
634e22edc7cSTaylor Simpson         tcg_gen_mov_tl(hex_pred[pred_num], ctx->new_pred_value[pred_num]);
6358b453a2bSTaylor Simpson     }
6368b453a2bSTaylor Simpson }
6378b453a2bSTaylor Simpson 
gen_check_store_width(DisasContext * ctx,int slot_num)638a27c100cSTaylor Simpson static void gen_check_store_width(DisasContext *ctx, int slot_num)
6398b453a2bSTaylor Simpson {
64085580a65STaylor Simpson     if (HEX_DEBUG) {
64123803bbeSPhilippe Mathieu-Daudé         TCGv slot = tcg_constant_tl(slot_num);
64223803bbeSPhilippe Mathieu-Daudé         TCGv check = tcg_constant_tl(ctx->store_width[slot_num]);
643ad75a51eSRichard Henderson         gen_helper_debug_check_store_width(tcg_env, slot, check);
64485580a65STaylor Simpson     }
645a27c100cSTaylor Simpson }
6468b453a2bSTaylor Simpson 
slot_is_predicated(Packet * pkt,int slot_num)6478b453a2bSTaylor Simpson static bool slot_is_predicated(Packet *pkt, int slot_num)
6488b453a2bSTaylor Simpson {
6498b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
6508b453a2bSTaylor Simpson         if (pkt->insn[i].slot == slot_num) {
6518b453a2bSTaylor Simpson             return GET_ATTRIB(pkt->insn[i].opcode, A_CONDEXEC);
6528b453a2bSTaylor Simpson         }
6538b453a2bSTaylor Simpson     }
6548b453a2bSTaylor Simpson     /* If we get to here, we didn't find an instruction in the requested slot */
6558b453a2bSTaylor Simpson     g_assert_not_reached();
6568b453a2bSTaylor Simpson }
6578b453a2bSTaylor Simpson 
process_store(DisasContext * ctx,int slot_num)6581e536334STaylor Simpson void process_store(DisasContext *ctx, int slot_num)
6598b453a2bSTaylor Simpson {
6601e536334STaylor Simpson     bool is_predicated = slot_is_predicated(ctx->pkt, slot_num);
6618b453a2bSTaylor Simpson     TCGLabel *label_end = NULL;
6628b453a2bSTaylor Simpson 
6638b453a2bSTaylor Simpson     /*
6648b453a2bSTaylor Simpson      * We may have already processed this store
6658b453a2bSTaylor Simpson      * See CHECK_NOSHUF in macros.h
6668b453a2bSTaylor Simpson      */
6678b453a2bSTaylor Simpson     if (slot_num == 1 && ctx->s1_store_processed) {
6688b453a2bSTaylor Simpson         return;
6698b453a2bSTaylor Simpson     }
67092cfa25fSTaylor Simpson     ctx->s1_store_processed = true;
6718b453a2bSTaylor Simpson 
6728b453a2bSTaylor Simpson     if (is_predicated) {
6738b453a2bSTaylor Simpson         TCGv cancelled = tcg_temp_new();
6748b453a2bSTaylor Simpson         label_end = gen_new_label();
6758b453a2bSTaylor Simpson 
6768b453a2bSTaylor Simpson         /* Don't do anything if the slot was cancelled */
6778b453a2bSTaylor Simpson         tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
6788b453a2bSTaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
6798b453a2bSTaylor Simpson     }
6808b453a2bSTaylor Simpson     {
6817a819de8SRichard Henderson         TCGv address = tcg_temp_new();
6828b453a2bSTaylor Simpson         tcg_gen_mov_tl(address, hex_store_addr[slot_num]);
6838b453a2bSTaylor Simpson 
6848b453a2bSTaylor Simpson         /*
6858b453a2bSTaylor Simpson          * If we know the width from the DisasContext, we can
6868b453a2bSTaylor Simpson          * generate much cleaner code.
6878b453a2bSTaylor Simpson          * Unfortunately, not all instructions execute the fSTORE
6888b453a2bSTaylor Simpson          * macro during code generation.  Anything that uses the
6898b453a2bSTaylor Simpson          * generic helper will have this problem.  Instructions
6908b453a2bSTaylor Simpson          * that use fWRAP to generate proper TCG code will be OK.
6918b453a2bSTaylor Simpson          */
6928b453a2bSTaylor Simpson         switch (ctx->store_width[slot_num]) {
6938b453a2bSTaylor Simpson         case 1:
694a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
69553b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
6968b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
69753b26d25SRichard Henderson                                ctx->mem_idx, MO_UB);
6988b453a2bSTaylor Simpson             break;
6998b453a2bSTaylor Simpson         case 2:
700a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
70153b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7028b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
70353b26d25SRichard Henderson                                ctx->mem_idx, MO_TEUW);
7048b453a2bSTaylor Simpson             break;
7058b453a2bSTaylor Simpson         case 4:
706a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
70753b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7088b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
70953b26d25SRichard Henderson                                ctx->mem_idx, MO_TEUL);
7108b453a2bSTaylor Simpson             break;
7118b453a2bSTaylor Simpson         case 8:
712a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
71353b26d25SRichard Henderson             tcg_gen_qemu_st_i64(hex_store_val64[slot_num],
7148b453a2bSTaylor Simpson                                 hex_store_addr[slot_num],
71553b26d25SRichard Henderson                                 ctx->mem_idx, MO_TEUQ);
7168b453a2bSTaylor Simpson             break;
7178b453a2bSTaylor Simpson         default:
7188b453a2bSTaylor Simpson             {
7198b453a2bSTaylor Simpson                 /*
7208b453a2bSTaylor Simpson                  * If we get to here, we don't know the width at
7218b453a2bSTaylor Simpson                  * TCG generation time, we'll use a helper to
7228b453a2bSTaylor Simpson                  * avoid branching based on the width at runtime.
7238b453a2bSTaylor Simpson                  */
72423803bbeSPhilippe Mathieu-Daudé                 TCGv slot = tcg_constant_tl(slot_num);
725ad75a51eSRichard Henderson                 gen_helper_commit_store(tcg_env, slot);
7268b453a2bSTaylor Simpson             }
7278b453a2bSTaylor Simpson         }
7288b453a2bSTaylor Simpson     }
7298b453a2bSTaylor Simpson     if (is_predicated) {
7308b453a2bSTaylor Simpson         gen_set_label(label_end);
7318b453a2bSTaylor Simpson     }
7328b453a2bSTaylor Simpson }
7338b453a2bSTaylor Simpson 
process_store_log(DisasContext * ctx)7341e536334STaylor Simpson static void process_store_log(DisasContext *ctx)
7358b453a2bSTaylor Simpson {
7368b453a2bSTaylor Simpson     /*
7378b453a2bSTaylor Simpson      *  When a packet has two stores, the hardware processes
738c23b5764STaylor Simpson      *  slot 1 and then slot 0.  This will be important when
7398b453a2bSTaylor Simpson      *  the memory accesses overlap.
7408b453a2bSTaylor Simpson      */
7411e536334STaylor Simpson     Packet *pkt = ctx->pkt;
742e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s1) {
743e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
7441e536334STaylor Simpson         process_store(ctx, 1);
7458b453a2bSTaylor Simpson     }
746e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s0) {
747e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
7481e536334STaylor Simpson         process_store(ctx, 0);
7498b453a2bSTaylor Simpson     }
7508b453a2bSTaylor Simpson }
7518b453a2bSTaylor Simpson 
7528b453a2bSTaylor Simpson /* Zero out a 32-bit cache line */
process_dczeroa(DisasContext * ctx)7531e536334STaylor Simpson static void process_dczeroa(DisasContext *ctx)
7548b453a2bSTaylor Simpson {
7551e536334STaylor Simpson     if (ctx->pkt->pkt_has_dczeroa) {
7568b453a2bSTaylor Simpson         /* Store 32 bytes of zero starting at (addr & ~0x1f) */
7578b453a2bSTaylor Simpson         TCGv addr = tcg_temp_new();
75823803bbeSPhilippe Mathieu-Daudé         TCGv_i64 zero = tcg_constant_i64(0);
7598b453a2bSTaylor Simpson 
7600fc56c43STaylor Simpson         tcg_gen_andi_tl(addr, ctx->dczero_addr, ~0x1f);
76153b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
7628b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
76353b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
7648b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
76553b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
7668b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
76753b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
7688b453a2bSTaylor Simpson     }
7698b453a2bSTaylor Simpson }
7708b453a2bSTaylor Simpson 
pkt_has_hvx_store(Packet * pkt)771a82dd548STaylor Simpson static bool pkt_has_hvx_store(Packet *pkt)
772a82dd548STaylor Simpson {
773a82dd548STaylor Simpson     int i;
774a82dd548STaylor Simpson     for (i = 0; i < pkt->num_insns; i++) {
775a82dd548STaylor Simpson         int opcode = pkt->insn[i].opcode;
776a82dd548STaylor Simpson         if (GET_ATTRIB(opcode, A_CVI) && GET_ATTRIB(opcode, A_STORE)) {
777a82dd548STaylor Simpson             return true;
778a82dd548STaylor Simpson         }
779a82dd548STaylor Simpson     }
780a82dd548STaylor Simpson     return false;
781a82dd548STaylor Simpson }
782a82dd548STaylor Simpson 
gen_commit_hvx(DisasContext * ctx)7831e536334STaylor Simpson static void gen_commit_hvx(DisasContext *ctx)
784a82dd548STaylor Simpson {
785a82dd548STaylor Simpson     int i;
786a82dd548STaylor Simpson 
787b8552985STaylor Simpson     /* Early exit if not needed */
788b8552985STaylor Simpson     if (!ctx->need_commit) {
789b8552985STaylor Simpson         g_assert(!pkt_has_hvx_store(ctx->pkt));
790b8552985STaylor Simpson         return;
791b8552985STaylor Simpson     }
792b8552985STaylor Simpson 
793a82dd548STaylor Simpson     /*
794a82dd548STaylor Simpson      *    for (i = 0; i < ctx->vreg_log_idx; i++) {
795a82dd548STaylor Simpson      *        int rnum = ctx->vreg_log[i];
796a82dd548STaylor Simpson      *        env->VRegs[rnum] = env->future_VRegs[rnum];
797a82dd548STaylor Simpson      *    }
798a82dd548STaylor Simpson      */
799a82dd548STaylor Simpson     for (i = 0; i < ctx->vreg_log_idx; i++) {
800a82dd548STaylor Simpson         int rnum = ctx->vreg_log[i];
801a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, VRegs[rnum]);
802a82dd548STaylor Simpson         intptr_t srcoff = ctx_future_vreg_off(ctx, rnum, 1, false);
803a82dd548STaylor Simpson         size_t size = sizeof(MMVector);
804a82dd548STaylor Simpson 
805a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
806a82dd548STaylor Simpson     }
807a82dd548STaylor Simpson 
808a82dd548STaylor Simpson     /*
809a82dd548STaylor Simpson      *    for (i = 0; i < ctx->qreg_log_idx; i++) {
810a82dd548STaylor Simpson      *        int rnum = ctx->qreg_log[i];
811a82dd548STaylor Simpson      *        env->QRegs[rnum] = env->future_QRegs[rnum];
812a82dd548STaylor Simpson      *    }
813a82dd548STaylor Simpson      */
814a82dd548STaylor Simpson     for (i = 0; i < ctx->qreg_log_idx; i++) {
815a82dd548STaylor Simpson         int rnum = ctx->qreg_log[i];
816a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, QRegs[rnum]);
817a82dd548STaylor Simpson         intptr_t srcoff = offsetof(CPUHexagonState, future_QRegs[rnum]);
818a82dd548STaylor Simpson         size_t size = sizeof(MMQReg);
819a82dd548STaylor Simpson 
820a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
821a82dd548STaylor Simpson     }
822a82dd548STaylor Simpson 
8231e536334STaylor Simpson     if (pkt_has_hvx_store(ctx->pkt)) {
824ad75a51eSRichard Henderson         gen_helper_commit_hvx_stores(tcg_env);
825a82dd548STaylor Simpson     }
826a82dd548STaylor Simpson }
827a82dd548STaylor Simpson 
update_exec_counters(DisasContext * ctx)8281e536334STaylor Simpson static void update_exec_counters(DisasContext *ctx)
8298b453a2bSTaylor Simpson {
8301e536334STaylor Simpson     Packet *pkt = ctx->pkt;
8318b453a2bSTaylor Simpson     int num_insns = pkt->num_insns;
8328b453a2bSTaylor Simpson     int num_real_insns = 0;
833a82dd548STaylor Simpson     int num_hvx_insns = 0;
8348b453a2bSTaylor Simpson 
8358b453a2bSTaylor Simpson     for (int i = 0; i < num_insns; i++) {
8368b453a2bSTaylor Simpson         if (!pkt->insn[i].is_endloop &&
8378b453a2bSTaylor Simpson             !pkt->insn[i].part1 &&
8388b453a2bSTaylor Simpson             !GET_ATTRIB(pkt->insn[i].opcode, A_IT_NOP)) {
8398b453a2bSTaylor Simpson             num_real_insns++;
8408b453a2bSTaylor Simpson         }
841a82dd548STaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, A_CVI)) {
842a82dd548STaylor Simpson             num_hvx_insns++;
843a82dd548STaylor Simpson         }
8448b453a2bSTaylor Simpson     }
8458b453a2bSTaylor Simpson 
8468b453a2bSTaylor Simpson     ctx->num_packets++;
8478b453a2bSTaylor Simpson     ctx->num_insns += num_real_insns;
848a82dd548STaylor Simpson     ctx->num_hvx_insns += num_hvx_insns;
8498b453a2bSTaylor Simpson }
8508b453a2bSTaylor Simpson 
gen_commit_packet(DisasContext * ctx)8511e536334STaylor Simpson static void gen_commit_packet(DisasContext *ctx)
8528b453a2bSTaylor Simpson {
853c23b5764STaylor Simpson     /*
854c23b5764STaylor Simpson      * If there is more than one store in a packet, make sure they are all OK
855c23b5764STaylor Simpson      * before proceeding with the rest of the packet commit.
856c23b5764STaylor Simpson      *
857c23b5764STaylor Simpson      * dczeroa has to be the only store operation in the packet, so we go
858c23b5764STaylor Simpson      * ahead and process that first.
859c23b5764STaylor Simpson      *
860a82dd548STaylor Simpson      * When there is an HVX store, there can also be a scalar store in either
861a82dd548STaylor Simpson      * slot 0 or slot1, so we create a mask for the helper to indicate what
862a82dd548STaylor Simpson      * work to do.
863a82dd548STaylor Simpson      *
864c23b5764STaylor Simpson      * When there are two scalar stores, we probe the one in slot 0.
865c23b5764STaylor Simpson      *
866c23b5764STaylor Simpson      * Note that we don't call the probe helper for packets with only one
867c23b5764STaylor Simpson      * store.  Therefore, we call process_store_log before anything else
868c23b5764STaylor Simpson      * involved in committing the packet.
869c23b5764STaylor Simpson      */
8701e536334STaylor Simpson     Packet *pkt = ctx->pkt;
871c23b5764STaylor Simpson     bool has_store_s0 = pkt->pkt_has_store_s0;
872c23b5764STaylor Simpson     bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed);
873a82dd548STaylor Simpson     bool has_hvx_store = pkt_has_hvx_store(pkt);
874c23b5764STaylor Simpson     if (pkt->pkt_has_dczeroa) {
875c23b5764STaylor Simpson         /*
876c23b5764STaylor Simpson          * The dczeroa will be the store in slot 0, check that we don't have
877a82dd548STaylor Simpson          * a store in slot 1 or an HVX store.
878c23b5764STaylor Simpson          */
879e2be9a5cSTaylor Simpson         g_assert(!has_store_s1 && !has_hvx_store);
8801e536334STaylor Simpson         process_dczeroa(ctx);
881a82dd548STaylor Simpson     } else if (has_hvx_store) {
882a82dd548STaylor Simpson         if (!has_store_s0 && !has_store_s1) {
8832bda44e8STaylor Simpson             TCGv mem_idx = tcg_constant_tl(ctx->mem_idx);
884ad75a51eSRichard Henderson             gen_helper_probe_hvx_stores(tcg_env, mem_idx);
885a82dd548STaylor Simpson         } else {
886a82dd548STaylor Simpson             int mask = 0;
887a82dd548STaylor Simpson 
888a82dd548STaylor Simpson             if (has_store_s0) {
8897b84fd04STaylor Simpson                 mask =
8907b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST0, 1);
891a82dd548STaylor Simpson             }
892a82dd548STaylor Simpson             if (has_store_s1) {
8937b84fd04STaylor Simpson                 mask =
8947b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST1, 1);
895a82dd548STaylor Simpson             }
896a82dd548STaylor Simpson             if (has_hvx_store) {
8977b84fd04STaylor Simpson                 mask =
8987b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
8997b84fd04STaylor Simpson                                HAS_HVX_STORES, 1);
9007b84fd04STaylor Simpson             }
9017b84fd04STaylor Simpson             if (has_store_s0 && slot_is_predicated(pkt, 0)) {
9027b84fd04STaylor Simpson                 mask =
9037b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9047b84fd04STaylor Simpson                                S0_IS_PRED, 1);
9057b84fd04STaylor Simpson             }
9067b84fd04STaylor Simpson             if (has_store_s1 && slot_is_predicated(pkt, 1)) {
9077b84fd04STaylor Simpson                 mask =
9087b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9097b84fd04STaylor Simpson                                S1_IS_PRED, 1);
910a82dd548STaylor Simpson             }
9112bda44e8STaylor Simpson             mask = FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, MMU_IDX,
9122bda44e8STaylor Simpson                               ctx->mem_idx);
913ad75a51eSRichard Henderson             gen_helper_probe_pkt_scalar_hvx_stores(tcg_env,
9142bda44e8STaylor Simpson                                                    tcg_constant_tl(mask));
915a82dd548STaylor Simpson         }
916c23b5764STaylor Simpson     } else if (has_store_s0 && has_store_s1) {
917c23b5764STaylor Simpson         /*
918c23b5764STaylor Simpson          * process_store_log will execute the slot 1 store first,
919c23b5764STaylor Simpson          * so we only have to probe the store in slot 0
920c23b5764STaylor Simpson          */
9217b84fd04STaylor Simpson         int args = 0;
9227b84fd04STaylor Simpson         args =
9237b84fd04STaylor Simpson             FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, MMU_IDX, ctx->mem_idx);
9247b84fd04STaylor Simpson         if (slot_is_predicated(pkt, 0)) {
9257b84fd04STaylor Simpson             args =
9267b84fd04STaylor Simpson                 FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, IS_PREDICATED, 1);
9277b84fd04STaylor Simpson         }
9287b84fd04STaylor Simpson         TCGv args_tcgv = tcg_constant_tl(args);
929ad75a51eSRichard Henderson         gen_helper_probe_pkt_scalar_store_s0(tcg_env, args_tcgv);
930c23b5764STaylor Simpson     }
931c23b5764STaylor Simpson 
9321e536334STaylor Simpson     process_store_log(ctx);
933c23b5764STaylor Simpson 
9348b453a2bSTaylor Simpson     gen_reg_writes(ctx);
9351e536334STaylor Simpson     gen_pred_writes(ctx);
936a82dd548STaylor Simpson     if (pkt->pkt_has_hvx) {
9371e536334STaylor Simpson         gen_commit_hvx(ctx);
938a82dd548STaylor Simpson     }
9391e536334STaylor Simpson     update_exec_counters(ctx);
94085580a65STaylor Simpson     if (HEX_DEBUG) {
9418b453a2bSTaylor Simpson         TCGv has_st0 =
94223803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s0 && !pkt->pkt_has_dczeroa);
9438b453a2bSTaylor Simpson         TCGv has_st1 =
94423803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa);
9458b453a2bSTaylor Simpson 
9468b453a2bSTaylor Simpson         /* Handy place to set a breakpoint at the end of execution */
947ad75a51eSRichard Henderson         gen_helper_debug_commit_end(tcg_env, tcg_constant_tl(ctx->pkt->pc),
9480fc56c43STaylor Simpson                                     ctx->pred_written, has_st0, has_st1);
9498b453a2bSTaylor Simpson     }
9508b453a2bSTaylor Simpson 
951a82dd548STaylor Simpson     if (pkt->vhist_insn != NULL) {
952a82dd548STaylor Simpson         ctx->pre_commit = false;
9531e536334STaylor Simpson         ctx->insn = pkt->vhist_insn;
9541e536334STaylor Simpson         pkt->vhist_insn->generate(ctx);
955a82dd548STaylor Simpson     }
956a82dd548STaylor Simpson 
9578b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
958743debbcSTaylor Simpson         gen_end_tb(ctx);
9598b453a2bSTaylor Simpson     }
9608b453a2bSTaylor Simpson }
9618b453a2bSTaylor Simpson 
decode_and_translate_packet(CPUHexagonState * env,DisasContext * ctx)9628b453a2bSTaylor Simpson static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
9638b453a2bSTaylor Simpson {
9648b453a2bSTaylor Simpson     uint32_t words[PACKET_WORDS_MAX];
9658b453a2bSTaylor Simpson     int nwords;
9668b453a2bSTaylor Simpson     Packet pkt;
9678b453a2bSTaylor Simpson     int i;
9688b453a2bSTaylor Simpson 
9698b453a2bSTaylor Simpson     nwords = read_packet_words(env, ctx, words);
9708b453a2bSTaylor Simpson     if (!nwords) {
971743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
9728b453a2bSTaylor Simpson         return;
9738b453a2bSTaylor Simpson     }
9748b453a2bSTaylor Simpson 
9751547a2d3STaylor Simpson     ctx->pkt = &pkt;
9761547a2d3STaylor Simpson     if (decode_packet(ctx, nwords, words, &pkt, false) > 0) {
97740085901STaylor Simpson         pkt.pc = ctx->base.pc_next;
9788b453a2bSTaylor Simpson         HEX_DEBUG_PRINT_PKT(&pkt);
9791e536334STaylor Simpson         gen_start_packet(ctx);
9808b453a2bSTaylor Simpson         for (i = 0; i < pkt.num_insns; i++) {
9811e536334STaylor Simpson             ctx->insn = &pkt.insn[i];
9821e536334STaylor Simpson             gen_insn(ctx);
9838b453a2bSTaylor Simpson         }
9841e536334STaylor Simpson         gen_commit_packet(ctx);
9858b453a2bSTaylor Simpson         ctx->base.pc_next += pkt.encod_pkt_size_in_bytes;
9868b453a2bSTaylor Simpson     } else {
987743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
9888b453a2bSTaylor Simpson     }
9898b453a2bSTaylor Simpson }
9908b453a2bSTaylor Simpson 
hexagon_tr_init_disas_context(DisasContextBase * dcbase,CPUState * cs)9918b453a2bSTaylor Simpson static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
9928b453a2bSTaylor Simpson                                           CPUState *cs)
9938b453a2bSTaylor Simpson {
9948b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
995b77af26eSRichard Henderson     HexagonCPU *hex_cpu = env_archcpu(cpu_env(cs));
996564b2040STaylor Simpson     uint32_t hex_flags = dcbase->tb->flags;
9978b453a2bSTaylor Simpson 
9988b453a2bSTaylor Simpson     ctx->mem_idx = MMU_USER_IDX;
9998b453a2bSTaylor Simpson     ctx->num_packets = 0;
10008b453a2bSTaylor Simpson     ctx->num_insns = 0;
1001a82dd548STaylor Simpson     ctx->num_hvx_insns = 0;
10021b9a7f2aSTaylor Simpson     ctx->branch_cond = TCG_COND_NEVER;
1003564b2040STaylor Simpson     ctx->is_tight_loop = FIELD_EX32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP);
1004d54c5615STaylor Simpson     ctx->short_circuit = hex_cpu->short_circuit;
10058b453a2bSTaylor Simpson }
10068b453a2bSTaylor Simpson 
hexagon_tr_tb_start(DisasContextBase * db,CPUState * cpu)10078b453a2bSTaylor Simpson static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
10088b453a2bSTaylor Simpson {
10098b453a2bSTaylor Simpson }
10108b453a2bSTaylor Simpson 
hexagon_tr_insn_start(DisasContextBase * dcbase,CPUState * cpu)10118b453a2bSTaylor Simpson static void hexagon_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
10128b453a2bSTaylor Simpson {
10138b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
10148b453a2bSTaylor Simpson 
10158b453a2bSTaylor Simpson     tcg_gen_insn_start(ctx->base.pc_next);
10168b453a2bSTaylor Simpson }
10178b453a2bSTaylor Simpson 
pkt_crosses_page(CPUHexagonState * env,DisasContext * ctx)10188b453a2bSTaylor Simpson static bool pkt_crosses_page(CPUHexagonState *env, DisasContext *ctx)
10198b453a2bSTaylor Simpson {
10208b453a2bSTaylor Simpson     target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
10218b453a2bSTaylor Simpson     bool found_end = false;
10228b453a2bSTaylor Simpson     int nwords;
10238b453a2bSTaylor Simpson 
10248b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
1025*e8939e80SRichard Henderson         uint32_t word = translator_ldl(env, &ctx->base,
10268b453a2bSTaylor Simpson                             ctx->base.pc_next + nwords * sizeof(uint32_t));
10278b453a2bSTaylor Simpson         found_end = is_packet_end(word);
10288b453a2bSTaylor Simpson     }
10298b453a2bSTaylor Simpson     uint32_t next_ptr =  ctx->base.pc_next + nwords * sizeof(uint32_t);
10308b453a2bSTaylor Simpson     return found_end && next_ptr - page_start >= TARGET_PAGE_SIZE;
10318b453a2bSTaylor Simpson }
10328b453a2bSTaylor Simpson 
hexagon_tr_translate_packet(DisasContextBase * dcbase,CPUState * cpu)10338b453a2bSTaylor Simpson static void hexagon_tr_translate_packet(DisasContextBase *dcbase, CPUState *cpu)
10348b453a2bSTaylor Simpson {
10358b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1036b77af26eSRichard Henderson     CPUHexagonState *env = cpu_env(cpu);
10378b453a2bSTaylor Simpson 
10388b453a2bSTaylor Simpson     decode_and_translate_packet(env, ctx);
10398b453a2bSTaylor Simpson 
10408b453a2bSTaylor Simpson     if (ctx->base.is_jmp == DISAS_NEXT) {
10418b453a2bSTaylor Simpson         target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
10428b453a2bSTaylor Simpson         target_ulong bytes_max = PACKET_WORDS_MAX * sizeof(target_ulong);
10438b453a2bSTaylor Simpson 
10448b453a2bSTaylor Simpson         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE ||
10458b453a2bSTaylor Simpson             (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE - bytes_max &&
10468b453a2bSTaylor Simpson              pkt_crosses_page(env, ctx))) {
10478b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
10488b453a2bSTaylor Simpson         }
10498b453a2bSTaylor Simpson 
10508b453a2bSTaylor Simpson         /*
10518b453a2bSTaylor Simpson          * The CPU log is used to compare against LLDB single stepping,
10528b453a2bSTaylor Simpson          * so end the TLB after every packet.
10538b453a2bSTaylor Simpson          */
10547d9ab202STaylor Simpson         HexagonCPU *hex_cpu = env_archcpu(env);
10558b453a2bSTaylor Simpson         if (hex_cpu->lldb_compat && qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
10568b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
10578b453a2bSTaylor Simpson         }
10588b453a2bSTaylor Simpson     }
10598b453a2bSTaylor Simpson }
10608b453a2bSTaylor Simpson 
hexagon_tr_tb_stop(DisasContextBase * dcbase,CPUState * cpu)10618b453a2bSTaylor Simpson static void hexagon_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
10628b453a2bSTaylor Simpson {
10638b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
10648b453a2bSTaylor Simpson 
10658b453a2bSTaylor Simpson     switch (ctx->base.is_jmp) {
10668b453a2bSTaylor Simpson     case DISAS_TOO_MANY:
10678b453a2bSTaylor Simpson         gen_exec_counters(ctx);
10688b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next);
10698b453a2bSTaylor Simpson         tcg_gen_exit_tb(NULL, 0);
10708b453a2bSTaylor Simpson         break;
10718b453a2bSTaylor Simpson     case DISAS_NORETURN:
10728b453a2bSTaylor Simpson         break;
10738b453a2bSTaylor Simpson     default:
10748b453a2bSTaylor Simpson         g_assert_not_reached();
10758b453a2bSTaylor Simpson     }
10768b453a2bSTaylor Simpson }
10778b453a2bSTaylor Simpson 
10788b453a2bSTaylor Simpson static const TranslatorOps hexagon_tr_ops = {
10798b453a2bSTaylor Simpson     .init_disas_context = hexagon_tr_init_disas_context,
10808b453a2bSTaylor Simpson     .tb_start           = hexagon_tr_tb_start,
10818b453a2bSTaylor Simpson     .insn_start         = hexagon_tr_insn_start,
10828b453a2bSTaylor Simpson     .translate_insn     = hexagon_tr_translate_packet,
10838b453a2bSTaylor Simpson     .tb_stop            = hexagon_tr_tb_stop,
10848b453a2bSTaylor Simpson };
10858b453a2bSTaylor Simpson 
gen_intermediate_code(CPUState * cs,TranslationBlock * tb,int * max_insns,vaddr pc,void * host_pc)1086597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
108732f0c394SAnton Johansson                            vaddr pc, void *host_pc)
10888b453a2bSTaylor Simpson {
10898b453a2bSTaylor Simpson     DisasContext ctx;
10908b453a2bSTaylor Simpson 
1091306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc,
1092306c8721SRichard Henderson                     &hexagon_tr_ops, &ctx.base);
10938b453a2bSTaylor Simpson }
10948b453a2bSTaylor Simpson 
10958b453a2bSTaylor Simpson #define NAME_LEN               64
10968b453a2bSTaylor Simpson static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
10978b453a2bSTaylor Simpson static char store_addr_names[STORES_MAX][NAME_LEN];
10988b453a2bSTaylor Simpson static char store_width_names[STORES_MAX][NAME_LEN];
10998b453a2bSTaylor Simpson static char store_val32_names[STORES_MAX][NAME_LEN];
11008b453a2bSTaylor Simpson static char store_val64_names[STORES_MAX][NAME_LEN];
1101a82dd548STaylor Simpson static char vstore_addr_names[VSTORES_MAX][NAME_LEN];
1102a82dd548STaylor Simpson static char vstore_size_names[VSTORES_MAX][NAME_LEN];
1103a82dd548STaylor Simpson static char vstore_pending_names[VSTORES_MAX][NAME_LEN];
11048b453a2bSTaylor Simpson 
hexagon_translate_init(void)11058b453a2bSTaylor Simpson void hexagon_translate_init(void)
11068b453a2bSTaylor Simpson {
11078b453a2bSTaylor Simpson     int i;
11088b453a2bSTaylor Simpson 
11098b453a2bSTaylor Simpson     opcode_init();
11108b453a2bSTaylor Simpson 
11118b453a2bSTaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
1112ad75a51eSRichard Henderson         hex_gpr[i] = tcg_global_mem_new(tcg_env,
11138b453a2bSTaylor Simpson             offsetof(CPUHexagonState, gpr[i]),
11148b453a2bSTaylor Simpson             hexagon_regnames[i]);
11158b453a2bSTaylor Simpson 
111685580a65STaylor Simpson         if (HEX_DEBUG) {
11178b453a2bSTaylor Simpson             snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s",
11188b453a2bSTaylor Simpson                      hexagon_regnames[i]);
1119ad75a51eSRichard Henderson             hex_reg_written[i] = tcg_global_mem_new(tcg_env,
11208b453a2bSTaylor Simpson                 offsetof(CPUHexagonState, reg_written[i]),
11218b453a2bSTaylor Simpson                 reg_written_names[i]);
112285580a65STaylor Simpson         }
11238b453a2bSTaylor Simpson     }
1124ad75a51eSRichard Henderson     hex_new_value_usr = tcg_global_mem_new(tcg_env,
11256aa4f1d1STaylor Simpson         offsetof(CPUHexagonState, new_value_usr), "new_value_usr");
11266aa4f1d1STaylor Simpson 
11278b453a2bSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
1128ad75a51eSRichard Henderson         hex_pred[i] = tcg_global_mem_new(tcg_env,
11298b453a2bSTaylor Simpson             offsetof(CPUHexagonState, pred[i]),
11308b453a2bSTaylor Simpson             hexagon_prednames[i]);
11318b453a2bSTaylor Simpson     }
1132ad75a51eSRichard Henderson     hex_slot_cancelled = tcg_global_mem_new(tcg_env,
11338b453a2bSTaylor Simpson         offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled");
1134ad75a51eSRichard Henderson     hex_llsc_addr = tcg_global_mem_new(tcg_env,
11358b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_addr), "llsc_addr");
1136ad75a51eSRichard Henderson     hex_llsc_val = tcg_global_mem_new(tcg_env,
11378b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val), "llsc_val");
1138ad75a51eSRichard Henderson     hex_llsc_val_i64 = tcg_global_mem_new_i64(tcg_env,
11398b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
11408b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
11418b453a2bSTaylor Simpson         snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
1142ad75a51eSRichard Henderson         hex_store_addr[i] = tcg_global_mem_new(tcg_env,
11438b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].va),
11448b453a2bSTaylor Simpson             store_addr_names[i]);
11458b453a2bSTaylor Simpson 
11468b453a2bSTaylor Simpson         snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i);
1147ad75a51eSRichard Henderson         hex_store_width[i] = tcg_global_mem_new(tcg_env,
11488b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].width),
11498b453a2bSTaylor Simpson             store_width_names[i]);
11508b453a2bSTaylor Simpson 
11518b453a2bSTaylor Simpson         snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i);
1152ad75a51eSRichard Henderson         hex_store_val32[i] = tcg_global_mem_new(tcg_env,
11538b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data32),
11548b453a2bSTaylor Simpson             store_val32_names[i]);
11558b453a2bSTaylor Simpson 
11568b453a2bSTaylor Simpson         snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i);
1157ad75a51eSRichard Henderson         hex_store_val64[i] = tcg_global_mem_new_i64(tcg_env,
11588b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data64),
11598b453a2bSTaylor Simpson             store_val64_names[i]);
11608b453a2bSTaylor Simpson     }
116120c34a92SBrian Cain     for (i = 0; i < VSTORES_MAX; i++) {
1162a82dd548STaylor Simpson         snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i);
1163ad75a51eSRichard Henderson         hex_vstore_addr[i] = tcg_global_mem_new(tcg_env,
1164a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].va),
1165a82dd548STaylor Simpson             vstore_addr_names[i]);
1166a82dd548STaylor Simpson 
1167a82dd548STaylor Simpson         snprintf(vstore_size_names[i], NAME_LEN, "vstore_size_%d", i);
1168ad75a51eSRichard Henderson         hex_vstore_size[i] = tcg_global_mem_new(tcg_env,
1169a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].size),
1170a82dd548STaylor Simpson             vstore_size_names[i]);
1171a82dd548STaylor Simpson 
1172a82dd548STaylor Simpson         snprintf(vstore_pending_names[i], NAME_LEN, "vstore_pending_%d", i);
1173ad75a51eSRichard Henderson         hex_vstore_pending[i] = tcg_global_mem_new(tcg_env,
1174a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore_pending[i]),
1175a82dd548STaylor Simpson             vstore_pending_names[i]);
1176a82dd548STaylor Simpson     }
11778b453a2bSTaylor Simpson }
1178