xref: /openbmc/qemu/target/hexagon/translate.c (revision 20c34a92)
18b453a2bSTaylor Simpson /*
210849c26STaylor Simpson  *  Copyright(c) 2019-2023 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"
268b453a2bSTaylor Simpson #include "exec/log.h"
278b453a2bSTaylor Simpson #include "internal.h"
288b453a2bSTaylor Simpson #include "attribs.h"
298b453a2bSTaylor Simpson #include "insn.h"
308b453a2bSTaylor Simpson #include "decode.h"
318b453a2bSTaylor Simpson #include "translate.h"
32d54c5615STaylor Simpson #include "genptr.h"
338b453a2bSTaylor Simpson #include "printinsn.h"
348b453a2bSTaylor Simpson 
35d53106c9SRichard Henderson #define HELPER_H "helper.h"
36d53106c9SRichard Henderson #include "exec/helper-info.c.inc"
37d53106c9SRichard Henderson #undef  HELPER_H
38d53106c9SRichard Henderson 
3910849c26STaylor Simpson #include "analyze_funcs_generated.c.inc"
4010849c26STaylor Simpson 
4110849c26STaylor Simpson typedef void (*AnalyzeInsn)(DisasContext *ctx);
4210849c26STaylor Simpson static const AnalyzeInsn opcode_analyze[XX_LAST_OPCODE] = {
4310849c26STaylor Simpson #define OPCODE(X)    [X] = analyze_##X
4410849c26STaylor Simpson #include "opcodes_def_generated.h.inc"
4510849c26STaylor Simpson #undef OPCODE
4610849c26STaylor Simpson };
4710849c26STaylor Simpson 
488b453a2bSTaylor Simpson TCGv hex_gpr[TOTAL_PER_THREAD_REGS];
498b453a2bSTaylor Simpson TCGv hex_pred[NUM_PREGS];
508b453a2bSTaylor Simpson TCGv hex_slot_cancelled;
516aa4f1d1STaylor Simpson TCGv hex_new_value_usr;
528b453a2bSTaylor Simpson TCGv hex_reg_written[TOTAL_PER_THREAD_REGS];
538b453a2bSTaylor Simpson TCGv hex_store_addr[STORES_MAX];
548b453a2bSTaylor Simpson TCGv hex_store_width[STORES_MAX];
558b453a2bSTaylor Simpson TCGv hex_store_val32[STORES_MAX];
568b453a2bSTaylor Simpson TCGv_i64 hex_store_val64[STORES_MAX];
578b453a2bSTaylor Simpson TCGv hex_llsc_addr;
588b453a2bSTaylor Simpson TCGv hex_llsc_val;
598b453a2bSTaylor Simpson TCGv_i64 hex_llsc_val_i64;
60a82dd548STaylor Simpson TCGv hex_vstore_addr[VSTORES_MAX];
61a82dd548STaylor Simpson TCGv hex_vstore_size[VSTORES_MAX];
62a82dd548STaylor Simpson TCGv hex_vstore_pending[VSTORES_MAX];
638b453a2bSTaylor Simpson 
648b453a2bSTaylor Simpson static const char * const hexagon_prednames[] = {
658b453a2bSTaylor Simpson   "p0", "p1", "p2", "p3"
668b453a2bSTaylor Simpson };
678b453a2bSTaylor Simpson 
68a82dd548STaylor Simpson intptr_t ctx_future_vreg_off(DisasContext *ctx, int regnum,
69a82dd548STaylor Simpson                           int num, bool alloc_ok)
70a82dd548STaylor Simpson {
71a82dd548STaylor Simpson     intptr_t offset;
72a82dd548STaylor Simpson 
73b8552985STaylor Simpson     if (!ctx->need_commit) {
74b8552985STaylor Simpson         return offsetof(CPUHexagonState, VRegs[regnum]);
75b8552985STaylor Simpson     }
76b8552985STaylor Simpson 
77a82dd548STaylor Simpson     /* See if it is already allocated */
78a82dd548STaylor Simpson     for (int i = 0; i < ctx->future_vregs_idx; i++) {
79a82dd548STaylor Simpson         if (ctx->future_vregs_num[i] == regnum) {
80a82dd548STaylor Simpson             return offsetof(CPUHexagonState, future_VRegs[i]);
81a82dd548STaylor Simpson         }
82a82dd548STaylor Simpson     }
83a82dd548STaylor Simpson 
84a82dd548STaylor Simpson     g_assert(alloc_ok);
85a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, future_VRegs[ctx->future_vregs_idx]);
86a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
87a82dd548STaylor Simpson         ctx->future_vregs_num[ctx->future_vregs_idx + i] = regnum++;
88a82dd548STaylor Simpson     }
89a82dd548STaylor Simpson     ctx->future_vregs_idx += num;
90a82dd548STaylor Simpson     g_assert(ctx->future_vregs_idx <= VECTOR_TEMPS_MAX);
91a82dd548STaylor Simpson     return offset;
92a82dd548STaylor Simpson }
93a82dd548STaylor Simpson 
94a82dd548STaylor Simpson intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,
95a82dd548STaylor Simpson                           int num, bool alloc_ok)
96a82dd548STaylor Simpson {
97a82dd548STaylor Simpson     intptr_t offset;
98a82dd548STaylor Simpson 
99a82dd548STaylor Simpson     /* See if it is already allocated */
100a82dd548STaylor Simpson     for (int i = 0; i < ctx->tmp_vregs_idx; i++) {
101a82dd548STaylor Simpson         if (ctx->tmp_vregs_num[i] == regnum) {
102a82dd548STaylor Simpson             return offsetof(CPUHexagonState, tmp_VRegs[i]);
103a82dd548STaylor Simpson         }
104a82dd548STaylor Simpson     }
105a82dd548STaylor Simpson 
106a82dd548STaylor Simpson     g_assert(alloc_ok);
107a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, tmp_VRegs[ctx->tmp_vregs_idx]);
108a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
109a82dd548STaylor Simpson         ctx->tmp_vregs_num[ctx->tmp_vregs_idx + i] = regnum++;
110a82dd548STaylor Simpson     }
111a82dd548STaylor Simpson     ctx->tmp_vregs_idx += num;
112a82dd548STaylor Simpson     g_assert(ctx->tmp_vregs_idx <= VECTOR_TEMPS_MAX);
113a82dd548STaylor Simpson     return offset;
114a82dd548STaylor Simpson }
115a82dd548STaylor Simpson 
116743debbcSTaylor Simpson static void gen_exception_raw(int excp)
1178b453a2bSTaylor Simpson {
118ad75a51eSRichard Henderson     gen_helper_raise_exception(tcg_env, tcg_constant_i32(excp));
1198b453a2bSTaylor Simpson }
1208b453a2bSTaylor Simpson 
121743debbcSTaylor Simpson static void gen_exec_counters(DisasContext *ctx)
1228b453a2bSTaylor Simpson {
123743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT],
124743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_PKT_CNT], ctx->num_packets);
125743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_INSN_CNT],
126743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns);
127a82dd548STaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT],
128a82dd548STaylor Simpson                     hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns);
129743debbcSTaylor Simpson }
130743debbcSTaylor Simpson 
1311b9a7f2aSTaylor Simpson static bool use_goto_tb(DisasContext *ctx, target_ulong dest)
1321b9a7f2aSTaylor Simpson {
1331b9a7f2aSTaylor Simpson     return translator_use_goto_tb(&ctx->base, dest);
1341b9a7f2aSTaylor Simpson }
1351b9a7f2aSTaylor Simpson 
136bee1fc56SMatheus Tavares Bernardino static void gen_goto_tb(DisasContext *ctx, int idx, target_ulong dest, bool
137bee1fc56SMatheus Tavares Bernardino                         move_to_pc)
1381b9a7f2aSTaylor Simpson {
1391b9a7f2aSTaylor Simpson     if (use_goto_tb(ctx, dest)) {
1401b9a7f2aSTaylor Simpson         tcg_gen_goto_tb(idx);
141bee1fc56SMatheus Tavares Bernardino         if (move_to_pc) {
1421b9a7f2aSTaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
143bee1fc56SMatheus Tavares Bernardino         }
1441b9a7f2aSTaylor Simpson         tcg_gen_exit_tb(ctx->base.tb, idx);
1451b9a7f2aSTaylor Simpson     } else {
146bee1fc56SMatheus Tavares Bernardino         if (move_to_pc) {
1471b9a7f2aSTaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
148bee1fc56SMatheus Tavares Bernardino         }
1491b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1501b9a7f2aSTaylor Simpson     }
1511b9a7f2aSTaylor Simpson }
1521b9a7f2aSTaylor Simpson 
153743debbcSTaylor Simpson static void gen_end_tb(DisasContext *ctx)
154743debbcSTaylor Simpson {
155564b2040STaylor Simpson     Packet *pkt = ctx->pkt;
156564b2040STaylor Simpson 
157743debbcSTaylor Simpson     gen_exec_counters(ctx);
1581b9a7f2aSTaylor Simpson 
1591b9a7f2aSTaylor Simpson     if (ctx->branch_cond != TCG_COND_NEVER) {
1601b9a7f2aSTaylor Simpson         if (ctx->branch_cond != TCG_COND_ALWAYS) {
1611b9a7f2aSTaylor Simpson             TCGLabel *skip = gen_new_label();
1620fc56c43STaylor Simpson             tcg_gen_brcondi_tl(ctx->branch_cond, ctx->branch_taken, 0, skip);
163bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 0, ctx->branch_dest, true);
1641b9a7f2aSTaylor Simpson             gen_set_label(skip);
165bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 1, ctx->next_PC, false);
1661b9a7f2aSTaylor Simpson         } else {
167bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 0, ctx->branch_dest, true);
1681b9a7f2aSTaylor Simpson         }
169564b2040STaylor Simpson     } else if (ctx->is_tight_loop &&
170564b2040STaylor Simpson                pkt->insn[pkt->num_insns - 1].opcode == J2_endloop0) {
171564b2040STaylor Simpson         /*
172564b2040STaylor Simpson          * When we're in a tight loop, we defer the endloop0 processing
173564b2040STaylor Simpson          * to take advantage of direct block chaining
174564b2040STaylor Simpson          */
175564b2040STaylor Simpson         TCGLabel *skip = gen_new_label();
176564b2040STaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC0], 1, skip);
177564b2040STaylor Simpson         tcg_gen_subi_tl(hex_gpr[HEX_REG_LC0], hex_gpr[HEX_REG_LC0], 1);
178bee1fc56SMatheus Tavares Bernardino         gen_goto_tb(ctx, 0, ctx->base.tb->pc, true);
179564b2040STaylor Simpson         gen_set_label(skip);
180bee1fc56SMatheus Tavares Bernardino         gen_goto_tb(ctx, 1, ctx->next_PC, false);
1811b9a7f2aSTaylor Simpson     } else {
1821b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1831b9a7f2aSTaylor Simpson     }
1841b9a7f2aSTaylor Simpson 
185743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
186743debbcSTaylor Simpson }
187743debbcSTaylor Simpson 
188743debbcSTaylor Simpson static void gen_exception_end_tb(DisasContext *ctx, int excp)
189743debbcSTaylor Simpson {
190743debbcSTaylor Simpson     gen_exec_counters(ctx);
191613653e5STaylor Simpson     tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC);
192743debbcSTaylor Simpson     gen_exception_raw(excp);
193743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
194743debbcSTaylor Simpson 
1958b453a2bSTaylor Simpson }
1968b453a2bSTaylor Simpson 
1978b453a2bSTaylor Simpson #define PACKET_BUFFER_LEN              1028
1988b453a2bSTaylor Simpson static void print_pkt(Packet *pkt)
1998b453a2bSTaylor Simpson {
2008b453a2bSTaylor Simpson     GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
2018b453a2bSTaylor Simpson     snprint_a_pkt_debug(buf, pkt);
2028b453a2bSTaylor Simpson     HEX_DEBUG_LOG("%s", buf->str);
2038b453a2bSTaylor Simpson     g_string_free(buf, true);
2048b453a2bSTaylor Simpson }
20585580a65STaylor Simpson #define HEX_DEBUG_PRINT_PKT(pkt) \
20685580a65STaylor Simpson     do { \
20785580a65STaylor Simpson         if (HEX_DEBUG) { \
20885580a65STaylor Simpson             print_pkt(pkt); \
20985580a65STaylor Simpson         } \
21085580a65STaylor Simpson     } while (0)
2118b453a2bSTaylor Simpson 
2128b453a2bSTaylor Simpson static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
2138b453a2bSTaylor Simpson                              uint32_t words[])
2148b453a2bSTaylor Simpson {
2158b453a2bSTaylor Simpson     bool found_end = false;
2168b453a2bSTaylor Simpson     int nwords, max_words;
2178b453a2bSTaylor Simpson 
2188b453a2bSTaylor Simpson     memset(words, 0, PACKET_WORDS_MAX * sizeof(uint32_t));
2198b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
220a27c100cSTaylor Simpson         words[nwords] =
2214e116893SIlya Leoshkevich             translator_ldl(env, &ctx->base,
2224e116893SIlya Leoshkevich                            ctx->base.pc_next + nwords * sizeof(uint32_t));
2238b453a2bSTaylor Simpson         found_end = is_packet_end(words[nwords]);
2248b453a2bSTaylor Simpson     }
2258b453a2bSTaylor Simpson     if (!found_end) {
2268b453a2bSTaylor Simpson         /* Read too many words without finding the end */
2278b453a2bSTaylor Simpson         return 0;
2288b453a2bSTaylor Simpson     }
2298b453a2bSTaylor Simpson 
2308b453a2bSTaylor Simpson     /* Check for page boundary crossing */
2318b453a2bSTaylor Simpson     max_words = -(ctx->base.pc_next | TARGET_PAGE_MASK) / sizeof(uint32_t);
2328b453a2bSTaylor Simpson     if (nwords > max_words) {
2338b453a2bSTaylor Simpson         /* We can only cross a page boundary at the beginning of a TB */
2348b453a2bSTaylor Simpson         g_assert(ctx->base.num_insns == 1);
2358b453a2bSTaylor Simpson     }
2368b453a2bSTaylor Simpson 
2378b453a2bSTaylor Simpson     HEX_DEBUG_LOG("decode_packet: pc = 0x%x\n", ctx->base.pc_next);
2388b453a2bSTaylor Simpson     HEX_DEBUG_LOG("    words = { ");
2398b453a2bSTaylor Simpson     for (int i = 0; i < nwords; i++) {
2408b453a2bSTaylor Simpson         HEX_DEBUG_LOG("0x%x, ", words[i]);
2418b453a2bSTaylor Simpson     }
2428b453a2bSTaylor Simpson     HEX_DEBUG_LOG("}\n");
2438b453a2bSTaylor Simpson 
2448b453a2bSTaylor Simpson     return nwords;
2458b453a2bSTaylor Simpson }
2468b453a2bSTaylor Simpson 
247d54c5615STaylor Simpson static bool check_for_attrib(Packet *pkt, int attrib)
2488b453a2bSTaylor Simpson {
2498b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2508b453a2bSTaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, attrib)) {
2518b453a2bSTaylor Simpson             return true;
2528b453a2bSTaylor Simpson         }
2538b453a2bSTaylor Simpson     }
2548b453a2bSTaylor Simpson     return false;
2558b453a2bSTaylor Simpson }
2568b453a2bSTaylor Simpson 
2578b453a2bSTaylor Simpson static bool need_slot_cancelled(Packet *pkt)
2588b453a2bSTaylor Simpson {
259c2b33d0bSTaylor Simpson     /* We only need slot_cancelled for conditional store instructions */
2607b84fd04STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2617b84fd04STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
2627b84fd04STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) &&
263c2b33d0bSTaylor Simpson             GET_ATTRIB(opcode, A_SCALAR_STORE)) {
2647b84fd04STaylor Simpson             return true;
2657b84fd04STaylor Simpson         }
2667b84fd04STaylor Simpson     }
2677b84fd04STaylor Simpson     return false;
2688b453a2bSTaylor Simpson }
2698b453a2bSTaylor Simpson 
270613653e5STaylor Simpson static bool need_next_PC(DisasContext *ctx)
271613653e5STaylor Simpson {
272613653e5STaylor Simpson     Packet *pkt = ctx->pkt;
273613653e5STaylor Simpson 
274613653e5STaylor Simpson     /* Check for conditional control flow or HW loop end */
275613653e5STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
276613653e5STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
277613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) && GET_ATTRIB(opcode, A_COF)) {
278613653e5STaylor Simpson             return true;
279613653e5STaylor Simpson         }
280613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_HWLOOP0_END) ||
281613653e5STaylor Simpson             GET_ATTRIB(opcode, A_HWLOOP1_END)) {
282613653e5STaylor Simpson             return true;
283613653e5STaylor Simpson         }
284613653e5STaylor Simpson     }
285613653e5STaylor Simpson     return false;
286613653e5STaylor Simpson }
287613653e5STaylor Simpson 
28810849c26STaylor Simpson /*
28910849c26STaylor Simpson  * The opcode_analyze functions mark most of the writes in a packet
29010849c26STaylor Simpson  * However, there are some implicit writes marked as attributes
29110849c26STaylor Simpson  * of the applicable instructions.
29210849c26STaylor Simpson  */
29310849c26STaylor Simpson static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
29410849c26STaylor Simpson {
29510849c26STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
29610849c26STaylor Simpson     if (GET_ATTRIB(opcode, attrib)) {
29710849c26STaylor Simpson         /*
29810849c26STaylor Simpson          * USR is used to set overflow and FP exceptions,
29910849c26STaylor Simpson          * so treat it as conditional
30010849c26STaylor Simpson          */
30110849c26STaylor Simpson         bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) ||
30210849c26STaylor Simpson                              rnum == HEX_REG_USR;
30310849c26STaylor Simpson 
30410849c26STaylor Simpson         /* LC0/LC1 is conditionally written by endloop instructions */
30510849c26STaylor Simpson         if ((rnum == HEX_REG_LC0 || rnum == HEX_REG_LC1) &&
30610849c26STaylor Simpson             (opcode == J2_endloop0 ||
30710849c26STaylor Simpson              opcode == J2_endloop1 ||
30810849c26STaylor Simpson              opcode == J2_endloop01)) {
30910849c26STaylor Simpson             is_predicated = true;
31010849c26STaylor Simpson         }
31110849c26STaylor Simpson 
31210849c26STaylor Simpson         ctx_log_reg_write(ctx, rnum, is_predicated);
31310849c26STaylor Simpson     }
31410849c26STaylor Simpson }
31510849c26STaylor Simpson 
31610849c26STaylor Simpson static void mark_implicit_reg_writes(DisasContext *ctx)
31710849c26STaylor Simpson {
31810849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP,  HEX_REG_FP);
31910849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SP,  HEX_REG_SP);
32010849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LR,  HEX_REG_LR);
32110849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC0, HEX_REG_LC0);
32210849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0);
32310849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1);
32410849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1);
32510849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR);
32610849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR);
32710849c26STaylor Simpson }
32810849c26STaylor Simpson 
32910849c26STaylor Simpson static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum)
33010849c26STaylor Simpson {
33110849c26STaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
33210849c26STaylor Simpson         ctx_log_pred_write(ctx, pnum);
33310849c26STaylor Simpson     }
33410849c26STaylor Simpson }
33510849c26STaylor Simpson 
33610849c26STaylor Simpson static void mark_implicit_pred_writes(DisasContext *ctx)
33710849c26STaylor Simpson {
33810849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P0, 0);
33910849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P1, 1);
34010849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P2, 2);
34110849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P3, 3);
34210849c26STaylor Simpson }
34310849c26STaylor Simpson 
344d54c5615STaylor Simpson static bool pkt_raises_exception(Packet *pkt)
345d54c5615STaylor Simpson {
346d54c5615STaylor Simpson     if (check_for_attrib(pkt, A_LOAD) ||
347d54c5615STaylor Simpson         check_for_attrib(pkt, A_STORE)) {
348d54c5615STaylor Simpson         return true;
349d54c5615STaylor Simpson     }
350d54c5615STaylor Simpson     return false;
351d54c5615STaylor Simpson }
352d54c5615STaylor Simpson 
353d54c5615STaylor Simpson static bool need_commit(DisasContext *ctx)
354d54c5615STaylor Simpson {
355d54c5615STaylor Simpson     Packet *pkt = ctx->pkt;
356d54c5615STaylor Simpson 
357d54c5615STaylor Simpson     /*
358d54c5615STaylor Simpson      * If the short-circuit property is set to false, we'll always do the commit
359d54c5615STaylor Simpson      */
360d54c5615STaylor Simpson     if (!ctx->short_circuit) {
361d54c5615STaylor Simpson         return true;
362d54c5615STaylor Simpson     }
363d54c5615STaylor Simpson 
364d54c5615STaylor Simpson     if (pkt_raises_exception(pkt)) {
365d54c5615STaylor Simpson         return true;
366d54c5615STaylor Simpson     }
367d54c5615STaylor Simpson 
368d54c5615STaylor Simpson     /* Registers with immutability flags require new_value */
369d54c5615STaylor Simpson     for (int i = 0; i < ctx->reg_log_idx; i++) {
370d54c5615STaylor Simpson         int rnum = ctx->reg_log[i];
371d54c5615STaylor Simpson         if (reg_immut_masks[rnum]) {
372d54c5615STaylor Simpson             return true;
373d54c5615STaylor Simpson         }
374d54c5615STaylor Simpson     }
375d54c5615STaylor Simpson 
376d54c5615STaylor Simpson     /* Floating point instructions are hard-coded to use new_value */
377d54c5615STaylor Simpson     if (check_for_attrib(pkt, A_FPOP)) {
378d54c5615STaylor Simpson         return true;
379d54c5615STaylor Simpson     }
380d54c5615STaylor Simpson 
381d05d5eebSTaylor Simpson     if (pkt->num_insns == 1) {
382d05d5eebSTaylor Simpson         if (pkt->pkt_has_hvx) {
383d05d5eebSTaylor Simpson             /*
384d05d5eebSTaylor Simpson              * The HVX instructions with generated helpers use
385d05d5eebSTaylor Simpson              * pass-by-reference, so they need the read/write overlap
386d05d5eebSTaylor Simpson              * check below.
387d05d5eebSTaylor Simpson              * The HVX instructions with overrides are OK.
388d05d5eebSTaylor Simpson              */
389d05d5eebSTaylor Simpson             if (!ctx->has_hvx_helper) {
390d54c5615STaylor Simpson                 return false;
391d54c5615STaylor Simpson             }
392d05d5eebSTaylor Simpson         } else {
393d05d5eebSTaylor Simpson             return false;
394d05d5eebSTaylor Simpson         }
395d05d5eebSTaylor Simpson     }
396d54c5615STaylor Simpson 
397d54c5615STaylor Simpson     /* Check for overlap between register reads and writes */
398d54c5615STaylor Simpson     for (int i = 0; i < ctx->reg_log_idx; i++) {
399d54c5615STaylor Simpson         int rnum = ctx->reg_log[i];
400d54c5615STaylor Simpson         if (test_bit(rnum, ctx->regs_read)) {
401d54c5615STaylor Simpson             return true;
402d54c5615STaylor Simpson         }
403d54c5615STaylor Simpson     }
404d54c5615STaylor Simpson 
405455e169dSTaylor Simpson     /* Check for overlap between predicate reads and writes */
406455e169dSTaylor Simpson     for (int i = 0; i < ctx->preg_log_idx; i++) {
407455e169dSTaylor Simpson         int pnum = ctx->preg_log[i];
408455e169dSTaylor Simpson         if (test_bit(pnum, ctx->pregs_read)) {
409455e169dSTaylor Simpson             return true;
410455e169dSTaylor Simpson         }
411455e169dSTaylor Simpson     }
412455e169dSTaylor Simpson 
413b8552985STaylor Simpson     /* Check for overlap between HVX reads and writes */
414b8552985STaylor Simpson     for (int i = 0; i < ctx->vreg_log_idx; i++) {
415b8552985STaylor Simpson         int vnum = ctx->vreg_log[i];
416b8552985STaylor Simpson         if (test_bit(vnum, ctx->vregs_read)) {
417b8552985STaylor Simpson             return true;
418b8552985STaylor Simpson         }
419b8552985STaylor Simpson     }
420b8552985STaylor Simpson     if (!bitmap_empty(ctx->vregs_updated_tmp, NUM_VREGS)) {
421b8552985STaylor Simpson         int i = find_first_bit(ctx->vregs_updated_tmp, NUM_VREGS);
422b8552985STaylor Simpson         while (i < NUM_VREGS) {
423b8552985STaylor Simpson             if (test_bit(i, ctx->vregs_read)) {
424b8552985STaylor Simpson                 return true;
425b8552985STaylor Simpson             }
426b8552985STaylor Simpson             i = find_next_bit(ctx->vregs_updated_tmp, NUM_VREGS, i + 1);
427b8552985STaylor Simpson         }
428b8552985STaylor Simpson     }
429b8552985STaylor Simpson     if (!bitmap_empty(ctx->vregs_select, NUM_VREGS)) {
430b8552985STaylor Simpson         int i = find_first_bit(ctx->vregs_select, NUM_VREGS);
431b8552985STaylor Simpson         while (i < NUM_VREGS) {
432b8552985STaylor Simpson             if (test_bit(i, ctx->vregs_read)) {
433b8552985STaylor Simpson                 return true;
434b8552985STaylor Simpson             }
435b8552985STaylor Simpson             i = find_next_bit(ctx->vregs_select, NUM_VREGS, i + 1);
436b8552985STaylor Simpson         }
437b8552985STaylor Simpson     }
438b8552985STaylor Simpson 
439b8552985STaylor Simpson     /* Check for overlap between HVX predicate reads and writes */
440b8552985STaylor Simpson     for (int i = 0; i < ctx->qreg_log_idx; i++) {
441b8552985STaylor Simpson         int qnum = ctx->qreg_log[i];
442b8552985STaylor Simpson         if (test_bit(qnum, ctx->qregs_read)) {
443b8552985STaylor Simpson             return true;
444b8552985STaylor Simpson         }
445b8552985STaylor Simpson     }
446b8552985STaylor Simpson 
447d54c5615STaylor Simpson     return false;
448d54c5615STaylor Simpson }
449d54c5615STaylor Simpson 
450b9f0326bSTaylor Simpson static void mark_implicit_pred_read(DisasContext *ctx, int attrib, int pnum)
451b9f0326bSTaylor Simpson {
452b9f0326bSTaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
453b9f0326bSTaylor Simpson         ctx_log_pred_read(ctx, pnum);
454b9f0326bSTaylor Simpson     }
455b9f0326bSTaylor Simpson }
456b9f0326bSTaylor Simpson 
457b9f0326bSTaylor Simpson static void mark_implicit_pred_reads(DisasContext *ctx)
458b9f0326bSTaylor Simpson {
459b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P0, 0);
460b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P1, 1);
461b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P3, 2);
462b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P3, 3);
463b9f0326bSTaylor Simpson }
464b9f0326bSTaylor Simpson 
46510849c26STaylor Simpson static void analyze_packet(DisasContext *ctx)
46610849c26STaylor Simpson {
46710849c26STaylor Simpson     Packet *pkt = ctx->pkt;
468d05d5eebSTaylor Simpson     ctx->has_hvx_helper = false;
46910849c26STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
47010849c26STaylor Simpson         Insn *insn = &pkt->insn[i];
47110849c26STaylor Simpson         ctx->insn = insn;
47210849c26STaylor Simpson         if (opcode_analyze[insn->opcode]) {
47310849c26STaylor Simpson             opcode_analyze[insn->opcode](ctx);
47410849c26STaylor Simpson         }
47510849c26STaylor Simpson         mark_implicit_reg_writes(ctx);
47610849c26STaylor Simpson         mark_implicit_pred_writes(ctx);
477b9f0326bSTaylor Simpson         mark_implicit_pred_reads(ctx);
47810849c26STaylor Simpson     }
479d54c5615STaylor Simpson 
480d54c5615STaylor Simpson     ctx->need_commit = need_commit(ctx);
48110849c26STaylor Simpson }
48210849c26STaylor Simpson 
4831e536334STaylor Simpson static void gen_start_packet(DisasContext *ctx)
4848b453a2bSTaylor Simpson {
4851e536334STaylor Simpson     Packet *pkt = ctx->pkt;
4868b453a2bSTaylor Simpson     target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes;
4878b453a2bSTaylor Simpson     int i;
4888b453a2bSTaylor Simpson 
4898b453a2bSTaylor Simpson     /* Clear out the disassembly context */
490613653e5STaylor Simpson     ctx->next_PC = next_PC;
4918b453a2bSTaylor Simpson     ctx->reg_log_idx = 0;
4928b453a2bSTaylor Simpson     bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS);
493b9f0326bSTaylor Simpson     bitmap_zero(ctx->regs_read, TOTAL_PER_THREAD_REGS);
49410849c26STaylor Simpson     bitmap_zero(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
4958b453a2bSTaylor Simpson     ctx->preg_log_idx = 0;
4966c677c60STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
497b9f0326bSTaylor Simpson     bitmap_zero(ctx->pregs_read, NUM_PREGS);
498a82dd548STaylor Simpson     ctx->future_vregs_idx = 0;
499a82dd548STaylor Simpson     ctx->tmp_vregs_idx = 0;
500a82dd548STaylor Simpson     ctx->vreg_log_idx = 0;
501a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated_tmp, NUM_VREGS);
502a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated, NUM_VREGS);
503a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_select, NUM_VREGS);
5044d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_future_vregs, NUM_VREGS);
5054d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_tmp_vregs, NUM_VREGS);
506b9f0326bSTaylor Simpson     bitmap_zero(ctx->vregs_read, NUM_VREGS);
507b9f0326bSTaylor Simpson     bitmap_zero(ctx->qregs_read, NUM_QREGS);
508a82dd548STaylor Simpson     ctx->qreg_log_idx = 0;
5098b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
5108b453a2bSTaylor Simpson         ctx->store_width[i] = 0;
5118b453a2bSTaylor Simpson     }
51292cfa25fSTaylor Simpson     ctx->s1_store_processed = false;
513a82dd548STaylor Simpson     ctx->pre_commit = true;
5144ff56764STaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
5154ff56764STaylor Simpson         ctx->new_value[i] = NULL;
5164ff56764STaylor Simpson     }
517e22edc7cSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
518e22edc7cSTaylor Simpson         ctx->new_pred_value[i] = NULL;
519e22edc7cSTaylor Simpson     }
5208b453a2bSTaylor Simpson 
52110849c26STaylor Simpson     analyze_packet(ctx);
52210849c26STaylor Simpson 
52310849c26STaylor Simpson     /*
52410849c26STaylor Simpson      * pregs_written is used both in the analyze phase as well as the code
52510849c26STaylor Simpson      * gen phase, so clear it again.
52610849c26STaylor Simpson      */
52710849c26STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
52810849c26STaylor Simpson 
52985580a65STaylor Simpson     if (HEX_DEBUG) {
5308b453a2bSTaylor Simpson         /* Handy place to set a breakpoint before the packet executes */
531ad75a51eSRichard Henderson         gen_helper_debug_start_packet(tcg_env);
53285580a65STaylor Simpson     }
5338b453a2bSTaylor Simpson 
5348b453a2bSTaylor Simpson     /* Initialize the runtime state for packet semantics */
5358b453a2bSTaylor Simpson     if (need_slot_cancelled(pkt)) {
5368b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_slot_cancelled, 0);
5378b453a2bSTaylor Simpson     }
5380fc56c43STaylor Simpson     ctx->branch_taken = NULL;
5398b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
5400fc56c43STaylor Simpson         ctx->branch_taken = tcg_temp_new();
541fb67c2bfSTaylor Simpson         if (pkt->pkt_has_multi_cof) {
5420fc56c43STaylor Simpson             tcg_gen_movi_tl(ctx->branch_taken, 0);
543fb67c2bfSTaylor Simpson         }
544613653e5STaylor Simpson         if (need_next_PC(ctx)) {
545613653e5STaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], next_PC);
546613653e5STaylor Simpson         }
5478b453a2bSTaylor Simpson     }
54825e1d87dSTaylor Simpson     if (HEX_DEBUG) {
549842b206fSTaylor Simpson         ctx->pred_written = tcg_temp_new();
550842b206fSTaylor Simpson         tcg_gen_movi_tl(ctx->pred_written, 0);
5518b453a2bSTaylor Simpson     }
552a82dd548STaylor Simpson 
5536aa4f1d1STaylor Simpson     /* Preload the predicated registers into get_result_gpr(ctx, i) */
554d54c5615STaylor Simpson     if (ctx->need_commit &&
555d54c5615STaylor Simpson         !bitmap_empty(ctx->predicated_regs, TOTAL_PER_THREAD_REGS)) {
556*20c34a92SBrian Cain         i = find_first_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
55710849c26STaylor Simpson         while (i < TOTAL_PER_THREAD_REGS) {
5586aa4f1d1STaylor Simpson             tcg_gen_mov_tl(get_result_gpr(ctx, i), hex_gpr[i]);
55910849c26STaylor Simpson             i = find_next_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS,
56010849c26STaylor Simpson                               i + 1);
56110849c26STaylor Simpson         }
56210849c26STaylor Simpson     }
56310849c26STaylor Simpson 
56425e1d87dSTaylor Simpson     /*
5652babbd93SMatheus Tavares Bernardino      * Preload the predicated pred registers into ctx->new_pred_value[pred_num]
56625e1d87dSTaylor Simpson      * Only endloop instructions conditionally write to pred registers
56725e1d87dSTaylor Simpson      */
568455e169dSTaylor Simpson     if (ctx->need_commit && pkt->pkt_has_endloop) {
569*20c34a92SBrian Cain         for (i = 0; i < ctx->preg_log_idx; i++) {
57025e1d87dSTaylor Simpson             int pred_num = ctx->preg_log[i];
571e22edc7cSTaylor Simpson             ctx->new_pred_value[pred_num] = tcg_temp_new();
572e22edc7cSTaylor Simpson             tcg_gen_mov_tl(ctx->new_pred_value[pred_num], hex_pred[pred_num]);
57325e1d87dSTaylor Simpson         }
57425e1d87dSTaylor Simpson     }
57525e1d87dSTaylor Simpson 
5764d6f8420STaylor Simpson     /* Preload the predicated HVX registers into future_VRegs and tmp_VRegs */
5774d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_future_vregs, NUM_VREGS)) {
578*20c34a92SBrian Cain         i = find_first_bit(ctx->predicated_future_vregs, NUM_VREGS);
5794d6f8420STaylor Simpson         while (i < NUM_VREGS) {
5804d6f8420STaylor Simpson             const intptr_t VdV_off =
5814d6f8420STaylor Simpson                 ctx_future_vreg_off(ctx, i, 1, true);
5824d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
5834d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
5844d6f8420STaylor Simpson                              src_off,
5854d6f8420STaylor Simpson                              sizeof(MMVector),
5864d6f8420STaylor Simpson                              sizeof(MMVector));
5874d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_future_vregs, NUM_VREGS, i + 1);
5884d6f8420STaylor Simpson         }
5894d6f8420STaylor Simpson     }
5904d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_tmp_vregs, NUM_VREGS)) {
591*20c34a92SBrian Cain         i = find_first_bit(ctx->predicated_tmp_vregs, NUM_VREGS);
5924d6f8420STaylor Simpson         while (i < NUM_VREGS) {
5934d6f8420STaylor Simpson             const intptr_t VdV_off =
5944d6f8420STaylor Simpson                 ctx_tmp_vreg_off(ctx, i, 1, true);
5954d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
5964d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
5974d6f8420STaylor Simpson                              src_off,
5984d6f8420STaylor Simpson                              sizeof(MMVector),
5994d6f8420STaylor Simpson                              sizeof(MMVector));
6004d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_tmp_vregs, NUM_VREGS, i + 1);
6014d6f8420STaylor Simpson         }
6024d6f8420STaylor Simpson     }
603a82dd548STaylor Simpson }
604a82dd548STaylor Simpson 
6051e536334STaylor Simpson bool is_gather_store_insn(DisasContext *ctx)
606a82dd548STaylor Simpson {
6071e536334STaylor Simpson     Packet *pkt = ctx->pkt;
6081e536334STaylor Simpson     Insn *insn = ctx->insn;
609a82dd548STaylor Simpson     if (GET_ATTRIB(insn->opcode, A_CVI_NEW) &&
610a82dd548STaylor Simpson         insn->new_value_producer_slot == 1) {
611a82dd548STaylor Simpson         /* Look for gather instruction */
612a82dd548STaylor Simpson         for (int i = 0; i < pkt->num_insns; i++) {
613a82dd548STaylor Simpson             Insn *in = &pkt->insn[i];
614a82dd548STaylor Simpson             if (GET_ATTRIB(in->opcode, A_CVI_GATHER) && in->slot == 1) {
615a82dd548STaylor Simpson                 return true;
616a82dd548STaylor Simpson             }
617a82dd548STaylor Simpson         }
618a82dd548STaylor Simpson     }
619a82dd548STaylor Simpson     return false;
6208b453a2bSTaylor Simpson }
6218b453a2bSTaylor Simpson 
6221e536334STaylor Simpson static void mark_store_width(DisasContext *ctx)
623661ad999STaylor Simpson {
6241e536334STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
6251e536334STaylor Simpson     uint32_t slot = ctx->insn->slot;
626661ad999STaylor Simpson     uint8_t width = 0;
627661ad999STaylor Simpson 
628661ad999STaylor Simpson     if (GET_ATTRIB(opcode, A_SCALAR_STORE)) {
629406c74f2STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_0B)) {
630406c74f2STaylor Simpson             return;
631406c74f2STaylor Simpson         }
632661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_1B)) {
633661ad999STaylor Simpson             width |= 1;
634661ad999STaylor Simpson         }
635661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_2B)) {
636661ad999STaylor Simpson             width |= 2;
637661ad999STaylor Simpson         }
638661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_4B)) {
639661ad999STaylor Simpson             width |= 4;
640661ad999STaylor Simpson         }
641661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_8B)) {
642661ad999STaylor Simpson             width |= 8;
643661ad999STaylor Simpson         }
644661ad999STaylor Simpson         tcg_debug_assert(is_power_of_2(width));
645661ad999STaylor Simpson         ctx->store_width[slot] = width;
646661ad999STaylor Simpson     }
647661ad999STaylor Simpson }
648661ad999STaylor Simpson 
6491e536334STaylor Simpson static void gen_insn(DisasContext *ctx)
6508b453a2bSTaylor Simpson {
6511e536334STaylor Simpson     if (ctx->insn->generate) {
6521e536334STaylor Simpson         ctx->insn->generate(ctx);
6531e536334STaylor Simpson         mark_store_width(ctx);
6548b453a2bSTaylor Simpson     } else {
655743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_OPCODE);
6568b453a2bSTaylor Simpson     }
6578b453a2bSTaylor Simpson }
6588b453a2bSTaylor Simpson 
6598b453a2bSTaylor Simpson /*
6608b453a2bSTaylor Simpson  * Helpers for generating the packet commit
6618b453a2bSTaylor Simpson  */
6628b453a2bSTaylor Simpson static void gen_reg_writes(DisasContext *ctx)
6638b453a2bSTaylor Simpson {
6648b453a2bSTaylor Simpson     int i;
6658b453a2bSTaylor Simpson 
666d54c5615STaylor Simpson     /* Early exit if not needed */
667d54c5615STaylor Simpson     if (!ctx->need_commit) {
668d54c5615STaylor Simpson         return;
669d54c5615STaylor Simpson     }
670d54c5615STaylor Simpson 
6718b453a2bSTaylor Simpson     for (i = 0; i < ctx->reg_log_idx; i++) {
6728b453a2bSTaylor Simpson         int reg_num = ctx->reg_log[i];
6738b453a2bSTaylor Simpson 
6746aa4f1d1STaylor Simpson         tcg_gen_mov_tl(hex_gpr[reg_num], get_result_gpr(ctx, reg_num));
675564b2040STaylor Simpson 
676564b2040STaylor Simpson         /*
677564b2040STaylor Simpson          * ctx->is_tight_loop is set when SA0 points to the beginning of the TB.
678564b2040STaylor Simpson          * If we write to SA0, we have to turn off tight loop handling.
679564b2040STaylor Simpson          */
680564b2040STaylor Simpson         if (reg_num == HEX_REG_SA0) {
681564b2040STaylor Simpson             ctx->is_tight_loop = false;
682564b2040STaylor Simpson         }
6838b453a2bSTaylor Simpson     }
6848b453a2bSTaylor Simpson }
6858b453a2bSTaylor Simpson 
6861e536334STaylor Simpson static void gen_pred_writes(DisasContext *ctx)
6878b453a2bSTaylor Simpson {
688455e169dSTaylor Simpson     /* Early exit if not needed or the log is empty */
689455e169dSTaylor Simpson     if (!ctx->need_commit || !ctx->preg_log_idx) {
6908b453a2bSTaylor Simpson         return;
6918b453a2bSTaylor Simpson     }
6928b453a2bSTaylor Simpson 
69325e1d87dSTaylor Simpson     for (int i = 0; i < ctx->preg_log_idx; i++) {
6948b453a2bSTaylor Simpson         int pred_num = ctx->preg_log[i];
695e22edc7cSTaylor Simpson         tcg_gen_mov_tl(hex_pred[pred_num], ctx->new_pred_value[pred_num]);
6968b453a2bSTaylor Simpson     }
6978b453a2bSTaylor Simpson }
6988b453a2bSTaylor Simpson 
699a27c100cSTaylor Simpson static void gen_check_store_width(DisasContext *ctx, int slot_num)
7008b453a2bSTaylor Simpson {
70185580a65STaylor Simpson     if (HEX_DEBUG) {
70223803bbeSPhilippe Mathieu-Daudé         TCGv slot = tcg_constant_tl(slot_num);
70323803bbeSPhilippe Mathieu-Daudé         TCGv check = tcg_constant_tl(ctx->store_width[slot_num]);
704ad75a51eSRichard Henderson         gen_helper_debug_check_store_width(tcg_env, slot, check);
70585580a65STaylor Simpson     }
706a27c100cSTaylor Simpson }
7078b453a2bSTaylor Simpson 
7088b453a2bSTaylor Simpson static bool slot_is_predicated(Packet *pkt, int slot_num)
7098b453a2bSTaylor Simpson {
7108b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
7118b453a2bSTaylor Simpson         if (pkt->insn[i].slot == slot_num) {
7128b453a2bSTaylor Simpson             return GET_ATTRIB(pkt->insn[i].opcode, A_CONDEXEC);
7138b453a2bSTaylor Simpson         }
7148b453a2bSTaylor Simpson     }
7158b453a2bSTaylor Simpson     /* If we get to here, we didn't find an instruction in the requested slot */
7168b453a2bSTaylor Simpson     g_assert_not_reached();
7178b453a2bSTaylor Simpson }
7188b453a2bSTaylor Simpson 
7191e536334STaylor Simpson void process_store(DisasContext *ctx, int slot_num)
7208b453a2bSTaylor Simpson {
7211e536334STaylor Simpson     bool is_predicated = slot_is_predicated(ctx->pkt, slot_num);
7228b453a2bSTaylor Simpson     TCGLabel *label_end = NULL;
7238b453a2bSTaylor Simpson 
7248b453a2bSTaylor Simpson     /*
7258b453a2bSTaylor Simpson      * We may have already processed this store
7268b453a2bSTaylor Simpson      * See CHECK_NOSHUF in macros.h
7278b453a2bSTaylor Simpson      */
7288b453a2bSTaylor Simpson     if (slot_num == 1 && ctx->s1_store_processed) {
7298b453a2bSTaylor Simpson         return;
7308b453a2bSTaylor Simpson     }
73192cfa25fSTaylor Simpson     ctx->s1_store_processed = true;
7328b453a2bSTaylor Simpson 
7338b453a2bSTaylor Simpson     if (is_predicated) {
7348b453a2bSTaylor Simpson         TCGv cancelled = tcg_temp_new();
7358b453a2bSTaylor Simpson         label_end = gen_new_label();
7368b453a2bSTaylor Simpson 
7378b453a2bSTaylor Simpson         /* Don't do anything if the slot was cancelled */
7388b453a2bSTaylor Simpson         tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
7398b453a2bSTaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
7408b453a2bSTaylor Simpson     }
7418b453a2bSTaylor Simpson     {
7427a819de8SRichard Henderson         TCGv address = tcg_temp_new();
7438b453a2bSTaylor Simpson         tcg_gen_mov_tl(address, hex_store_addr[slot_num]);
7448b453a2bSTaylor Simpson 
7458b453a2bSTaylor Simpson         /*
7468b453a2bSTaylor Simpson          * If we know the width from the DisasContext, we can
7478b453a2bSTaylor Simpson          * generate much cleaner code.
7488b453a2bSTaylor Simpson          * Unfortunately, not all instructions execute the fSTORE
7498b453a2bSTaylor Simpson          * macro during code generation.  Anything that uses the
7508b453a2bSTaylor Simpson          * generic helper will have this problem.  Instructions
7518b453a2bSTaylor Simpson          * that use fWRAP to generate proper TCG code will be OK.
7528b453a2bSTaylor Simpson          */
7538b453a2bSTaylor Simpson         switch (ctx->store_width[slot_num]) {
7548b453a2bSTaylor Simpson         case 1:
755a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
75653b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7578b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
75853b26d25SRichard Henderson                                ctx->mem_idx, MO_UB);
7598b453a2bSTaylor Simpson             break;
7608b453a2bSTaylor Simpson         case 2:
761a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
76253b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7638b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
76453b26d25SRichard Henderson                                ctx->mem_idx, MO_TEUW);
7658b453a2bSTaylor Simpson             break;
7668b453a2bSTaylor Simpson         case 4:
767a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
76853b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7698b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
77053b26d25SRichard Henderson                                ctx->mem_idx, MO_TEUL);
7718b453a2bSTaylor Simpson             break;
7728b453a2bSTaylor Simpson         case 8:
773a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
77453b26d25SRichard Henderson             tcg_gen_qemu_st_i64(hex_store_val64[slot_num],
7758b453a2bSTaylor Simpson                                 hex_store_addr[slot_num],
77653b26d25SRichard Henderson                                 ctx->mem_idx, MO_TEUQ);
7778b453a2bSTaylor Simpson             break;
7788b453a2bSTaylor Simpson         default:
7798b453a2bSTaylor Simpson             {
7808b453a2bSTaylor Simpson                 /*
7818b453a2bSTaylor Simpson                  * If we get to here, we don't know the width at
7828b453a2bSTaylor Simpson                  * TCG generation time, we'll use a helper to
7838b453a2bSTaylor Simpson                  * avoid branching based on the width at runtime.
7848b453a2bSTaylor Simpson                  */
78523803bbeSPhilippe Mathieu-Daudé                 TCGv slot = tcg_constant_tl(slot_num);
786ad75a51eSRichard Henderson                 gen_helper_commit_store(tcg_env, slot);
7878b453a2bSTaylor Simpson             }
7888b453a2bSTaylor Simpson         }
7898b453a2bSTaylor Simpson     }
7908b453a2bSTaylor Simpson     if (is_predicated) {
7918b453a2bSTaylor Simpson         gen_set_label(label_end);
7928b453a2bSTaylor Simpson     }
7938b453a2bSTaylor Simpson }
7948b453a2bSTaylor Simpson 
7951e536334STaylor Simpson static void process_store_log(DisasContext *ctx)
7968b453a2bSTaylor Simpson {
7978b453a2bSTaylor Simpson     /*
7988b453a2bSTaylor Simpson      *  When a packet has two stores, the hardware processes
799c23b5764STaylor Simpson      *  slot 1 and then slot 0.  This will be important when
8008b453a2bSTaylor Simpson      *  the memory accesses overlap.
8018b453a2bSTaylor Simpson      */
8021e536334STaylor Simpson     Packet *pkt = ctx->pkt;
803e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s1) {
804e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
8051e536334STaylor Simpson         process_store(ctx, 1);
8068b453a2bSTaylor Simpson     }
807e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s0) {
808e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
8091e536334STaylor Simpson         process_store(ctx, 0);
8108b453a2bSTaylor Simpson     }
8118b453a2bSTaylor Simpson }
8128b453a2bSTaylor Simpson 
8138b453a2bSTaylor Simpson /* Zero out a 32-bit cache line */
8141e536334STaylor Simpson static void process_dczeroa(DisasContext *ctx)
8158b453a2bSTaylor Simpson {
8161e536334STaylor Simpson     if (ctx->pkt->pkt_has_dczeroa) {
8178b453a2bSTaylor Simpson         /* Store 32 bytes of zero starting at (addr & ~0x1f) */
8188b453a2bSTaylor Simpson         TCGv addr = tcg_temp_new();
81923803bbeSPhilippe Mathieu-Daudé         TCGv_i64 zero = tcg_constant_i64(0);
8208b453a2bSTaylor Simpson 
8210fc56c43STaylor Simpson         tcg_gen_andi_tl(addr, ctx->dczero_addr, ~0x1f);
82253b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
8238b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
82453b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
8258b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
82653b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
8278b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
82853b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
8298b453a2bSTaylor Simpson     }
8308b453a2bSTaylor Simpson }
8318b453a2bSTaylor Simpson 
832a82dd548STaylor Simpson static bool pkt_has_hvx_store(Packet *pkt)
833a82dd548STaylor Simpson {
834a82dd548STaylor Simpson     int i;
835a82dd548STaylor Simpson     for (i = 0; i < pkt->num_insns; i++) {
836a82dd548STaylor Simpson         int opcode = pkt->insn[i].opcode;
837a82dd548STaylor Simpson         if (GET_ATTRIB(opcode, A_CVI) && GET_ATTRIB(opcode, A_STORE)) {
838a82dd548STaylor Simpson             return true;
839a82dd548STaylor Simpson         }
840a82dd548STaylor Simpson     }
841a82dd548STaylor Simpson     return false;
842a82dd548STaylor Simpson }
843a82dd548STaylor Simpson 
8441e536334STaylor Simpson static void gen_commit_hvx(DisasContext *ctx)
845a82dd548STaylor Simpson {
846a82dd548STaylor Simpson     int i;
847a82dd548STaylor Simpson 
848b8552985STaylor Simpson     /* Early exit if not needed */
849b8552985STaylor Simpson     if (!ctx->need_commit) {
850b8552985STaylor Simpson         g_assert(!pkt_has_hvx_store(ctx->pkt));
851b8552985STaylor Simpson         return;
852b8552985STaylor Simpson     }
853b8552985STaylor Simpson 
854a82dd548STaylor Simpson     /*
855a82dd548STaylor Simpson      *    for (i = 0; i < ctx->vreg_log_idx; i++) {
856a82dd548STaylor Simpson      *        int rnum = ctx->vreg_log[i];
857a82dd548STaylor Simpson      *        env->VRegs[rnum] = env->future_VRegs[rnum];
858a82dd548STaylor Simpson      *    }
859a82dd548STaylor Simpson      */
860a82dd548STaylor Simpson     for (i = 0; i < ctx->vreg_log_idx; i++) {
861a82dd548STaylor Simpson         int rnum = ctx->vreg_log[i];
862a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, VRegs[rnum]);
863a82dd548STaylor Simpson         intptr_t srcoff = ctx_future_vreg_off(ctx, rnum, 1, false);
864a82dd548STaylor Simpson         size_t size = sizeof(MMVector);
865a82dd548STaylor Simpson 
866a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
867a82dd548STaylor Simpson     }
868a82dd548STaylor Simpson 
869a82dd548STaylor Simpson     /*
870a82dd548STaylor Simpson      *    for (i = 0; i < ctx->qreg_log_idx; i++) {
871a82dd548STaylor Simpson      *        int rnum = ctx->qreg_log[i];
872a82dd548STaylor Simpson      *        env->QRegs[rnum] = env->future_QRegs[rnum];
873a82dd548STaylor Simpson      *    }
874a82dd548STaylor Simpson      */
875a82dd548STaylor Simpson     for (i = 0; i < ctx->qreg_log_idx; i++) {
876a82dd548STaylor Simpson         int rnum = ctx->qreg_log[i];
877a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, QRegs[rnum]);
878a82dd548STaylor Simpson         intptr_t srcoff = offsetof(CPUHexagonState, future_QRegs[rnum]);
879a82dd548STaylor Simpson         size_t size = sizeof(MMQReg);
880a82dd548STaylor Simpson 
881a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
882a82dd548STaylor Simpson     }
883a82dd548STaylor Simpson 
8841e536334STaylor Simpson     if (pkt_has_hvx_store(ctx->pkt)) {
885ad75a51eSRichard Henderson         gen_helper_commit_hvx_stores(tcg_env);
886a82dd548STaylor Simpson     }
887a82dd548STaylor Simpson }
888a82dd548STaylor Simpson 
8891e536334STaylor Simpson static void update_exec_counters(DisasContext *ctx)
8908b453a2bSTaylor Simpson {
8911e536334STaylor Simpson     Packet *pkt = ctx->pkt;
8928b453a2bSTaylor Simpson     int num_insns = pkt->num_insns;
8938b453a2bSTaylor Simpson     int num_real_insns = 0;
894a82dd548STaylor Simpson     int num_hvx_insns = 0;
8958b453a2bSTaylor Simpson 
8968b453a2bSTaylor Simpson     for (int i = 0; i < num_insns; i++) {
8978b453a2bSTaylor Simpson         if (!pkt->insn[i].is_endloop &&
8988b453a2bSTaylor Simpson             !pkt->insn[i].part1 &&
8998b453a2bSTaylor Simpson             !GET_ATTRIB(pkt->insn[i].opcode, A_IT_NOP)) {
9008b453a2bSTaylor Simpson             num_real_insns++;
9018b453a2bSTaylor Simpson         }
902a82dd548STaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, A_CVI)) {
903a82dd548STaylor Simpson             num_hvx_insns++;
904a82dd548STaylor Simpson         }
9058b453a2bSTaylor Simpson     }
9068b453a2bSTaylor Simpson 
9078b453a2bSTaylor Simpson     ctx->num_packets++;
9088b453a2bSTaylor Simpson     ctx->num_insns += num_real_insns;
909a82dd548STaylor Simpson     ctx->num_hvx_insns += num_hvx_insns;
9108b453a2bSTaylor Simpson }
9118b453a2bSTaylor Simpson 
9121e536334STaylor Simpson static void gen_commit_packet(DisasContext *ctx)
9138b453a2bSTaylor Simpson {
914c23b5764STaylor Simpson     /*
915c23b5764STaylor Simpson      * If there is more than one store in a packet, make sure they are all OK
916c23b5764STaylor Simpson      * before proceeding with the rest of the packet commit.
917c23b5764STaylor Simpson      *
918c23b5764STaylor Simpson      * dczeroa has to be the only store operation in the packet, so we go
919c23b5764STaylor Simpson      * ahead and process that first.
920c23b5764STaylor Simpson      *
921a82dd548STaylor Simpson      * When there is an HVX store, there can also be a scalar store in either
922a82dd548STaylor Simpson      * slot 0 or slot1, so we create a mask for the helper to indicate what
923a82dd548STaylor Simpson      * work to do.
924a82dd548STaylor Simpson      *
925c23b5764STaylor Simpson      * When there are two scalar stores, we probe the one in slot 0.
926c23b5764STaylor Simpson      *
927c23b5764STaylor Simpson      * Note that we don't call the probe helper for packets with only one
928c23b5764STaylor Simpson      * store.  Therefore, we call process_store_log before anything else
929c23b5764STaylor Simpson      * involved in committing the packet.
930c23b5764STaylor Simpson      */
9311e536334STaylor Simpson     Packet *pkt = ctx->pkt;
932c23b5764STaylor Simpson     bool has_store_s0 = pkt->pkt_has_store_s0;
933c23b5764STaylor Simpson     bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed);
934a82dd548STaylor Simpson     bool has_hvx_store = pkt_has_hvx_store(pkt);
935c23b5764STaylor Simpson     if (pkt->pkt_has_dczeroa) {
936c23b5764STaylor Simpson         /*
937c23b5764STaylor Simpson          * The dczeroa will be the store in slot 0, check that we don't have
938a82dd548STaylor Simpson          * a store in slot 1 or an HVX store.
939c23b5764STaylor Simpson          */
940e2be9a5cSTaylor Simpson         g_assert(!has_store_s1 && !has_hvx_store);
9411e536334STaylor Simpson         process_dczeroa(ctx);
942a82dd548STaylor Simpson     } else if (has_hvx_store) {
943a82dd548STaylor Simpson         if (!has_store_s0 && !has_store_s1) {
9442bda44e8STaylor Simpson             TCGv mem_idx = tcg_constant_tl(ctx->mem_idx);
945ad75a51eSRichard Henderson             gen_helper_probe_hvx_stores(tcg_env, mem_idx);
946a82dd548STaylor Simpson         } else {
947a82dd548STaylor Simpson             int mask = 0;
948a82dd548STaylor Simpson 
949a82dd548STaylor Simpson             if (has_store_s0) {
9507b84fd04STaylor Simpson                 mask =
9517b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST0, 1);
952a82dd548STaylor Simpson             }
953a82dd548STaylor Simpson             if (has_store_s1) {
9547b84fd04STaylor Simpson                 mask =
9557b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST1, 1);
956a82dd548STaylor Simpson             }
957a82dd548STaylor Simpson             if (has_hvx_store) {
9587b84fd04STaylor Simpson                 mask =
9597b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9607b84fd04STaylor Simpson                                HAS_HVX_STORES, 1);
9617b84fd04STaylor Simpson             }
9627b84fd04STaylor Simpson             if (has_store_s0 && slot_is_predicated(pkt, 0)) {
9637b84fd04STaylor Simpson                 mask =
9647b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9657b84fd04STaylor Simpson                                S0_IS_PRED, 1);
9667b84fd04STaylor Simpson             }
9677b84fd04STaylor Simpson             if (has_store_s1 && slot_is_predicated(pkt, 1)) {
9687b84fd04STaylor Simpson                 mask =
9697b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9707b84fd04STaylor Simpson                                S1_IS_PRED, 1);
971a82dd548STaylor Simpson             }
9722bda44e8STaylor Simpson             mask = FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, MMU_IDX,
9732bda44e8STaylor Simpson                               ctx->mem_idx);
974ad75a51eSRichard Henderson             gen_helper_probe_pkt_scalar_hvx_stores(tcg_env,
9752bda44e8STaylor Simpson                                                    tcg_constant_tl(mask));
976a82dd548STaylor Simpson         }
977c23b5764STaylor Simpson     } else if (has_store_s0 && has_store_s1) {
978c23b5764STaylor Simpson         /*
979c23b5764STaylor Simpson          * process_store_log will execute the slot 1 store first,
980c23b5764STaylor Simpson          * so we only have to probe the store in slot 0
981c23b5764STaylor Simpson          */
9827b84fd04STaylor Simpson         int args = 0;
9837b84fd04STaylor Simpson         args =
9847b84fd04STaylor Simpson             FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, MMU_IDX, ctx->mem_idx);
9857b84fd04STaylor Simpson         if (slot_is_predicated(pkt, 0)) {
9867b84fd04STaylor Simpson             args =
9877b84fd04STaylor Simpson                 FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, IS_PREDICATED, 1);
9887b84fd04STaylor Simpson         }
9897b84fd04STaylor Simpson         TCGv args_tcgv = tcg_constant_tl(args);
990ad75a51eSRichard Henderson         gen_helper_probe_pkt_scalar_store_s0(tcg_env, args_tcgv);
991c23b5764STaylor Simpson     }
992c23b5764STaylor Simpson 
9931e536334STaylor Simpson     process_store_log(ctx);
994c23b5764STaylor Simpson 
9958b453a2bSTaylor Simpson     gen_reg_writes(ctx);
9961e536334STaylor Simpson     gen_pred_writes(ctx);
997a82dd548STaylor Simpson     if (pkt->pkt_has_hvx) {
9981e536334STaylor Simpson         gen_commit_hvx(ctx);
999a82dd548STaylor Simpson     }
10001e536334STaylor Simpson     update_exec_counters(ctx);
100185580a65STaylor Simpson     if (HEX_DEBUG) {
10028b453a2bSTaylor Simpson         TCGv has_st0 =
100323803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s0 && !pkt->pkt_has_dczeroa);
10048b453a2bSTaylor Simpson         TCGv has_st1 =
100523803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa);
10068b453a2bSTaylor Simpson 
10078b453a2bSTaylor Simpson         /* Handy place to set a breakpoint at the end of execution */
1008ad75a51eSRichard Henderson         gen_helper_debug_commit_end(tcg_env, tcg_constant_tl(ctx->pkt->pc),
10090fc56c43STaylor Simpson                                     ctx->pred_written, has_st0, has_st1);
10108b453a2bSTaylor Simpson     }
10118b453a2bSTaylor Simpson 
1012a82dd548STaylor Simpson     if (pkt->vhist_insn != NULL) {
1013a82dd548STaylor Simpson         ctx->pre_commit = false;
10141e536334STaylor Simpson         ctx->insn = pkt->vhist_insn;
10151e536334STaylor Simpson         pkt->vhist_insn->generate(ctx);
1016a82dd548STaylor Simpson     }
1017a82dd548STaylor Simpson 
10188b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
1019743debbcSTaylor Simpson         gen_end_tb(ctx);
10208b453a2bSTaylor Simpson     }
10218b453a2bSTaylor Simpson }
10228b453a2bSTaylor Simpson 
10238b453a2bSTaylor Simpson static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
10248b453a2bSTaylor Simpson {
10258b453a2bSTaylor Simpson     uint32_t words[PACKET_WORDS_MAX];
10268b453a2bSTaylor Simpson     int nwords;
10278b453a2bSTaylor Simpson     Packet pkt;
10288b453a2bSTaylor Simpson     int i;
10298b453a2bSTaylor Simpson 
10308b453a2bSTaylor Simpson     nwords = read_packet_words(env, ctx, words);
10318b453a2bSTaylor Simpson     if (!nwords) {
1032743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
10338b453a2bSTaylor Simpson         return;
10348b453a2bSTaylor Simpson     }
10358b453a2bSTaylor Simpson 
10368b453a2bSTaylor Simpson     if (decode_packet(nwords, words, &pkt, false) > 0) {
103740085901STaylor Simpson         pkt.pc = ctx->base.pc_next;
10388b453a2bSTaylor Simpson         HEX_DEBUG_PRINT_PKT(&pkt);
10391e536334STaylor Simpson         ctx->pkt = &pkt;
10401e536334STaylor Simpson         gen_start_packet(ctx);
10418b453a2bSTaylor Simpson         for (i = 0; i < pkt.num_insns; i++) {
10421e536334STaylor Simpson             ctx->insn = &pkt.insn[i];
10431e536334STaylor Simpson             gen_insn(ctx);
10448b453a2bSTaylor Simpson         }
10451e536334STaylor Simpson         gen_commit_packet(ctx);
10468b453a2bSTaylor Simpson         ctx->base.pc_next += pkt.encod_pkt_size_in_bytes;
10478b453a2bSTaylor Simpson     } else {
1048743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
10498b453a2bSTaylor Simpson     }
10508b453a2bSTaylor Simpson }
10518b453a2bSTaylor Simpson 
10528b453a2bSTaylor Simpson static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
10538b453a2bSTaylor Simpson                                           CPUState *cs)
10548b453a2bSTaylor Simpson {
10558b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1056b77af26eSRichard Henderson     HexagonCPU *hex_cpu = env_archcpu(cpu_env(cs));
1057564b2040STaylor Simpson     uint32_t hex_flags = dcbase->tb->flags;
10588b453a2bSTaylor Simpson 
10598b453a2bSTaylor Simpson     ctx->mem_idx = MMU_USER_IDX;
10608b453a2bSTaylor Simpson     ctx->num_packets = 0;
10618b453a2bSTaylor Simpson     ctx->num_insns = 0;
1062a82dd548STaylor Simpson     ctx->num_hvx_insns = 0;
10631b9a7f2aSTaylor Simpson     ctx->branch_cond = TCG_COND_NEVER;
1064564b2040STaylor Simpson     ctx->is_tight_loop = FIELD_EX32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP);
1065d54c5615STaylor Simpson     ctx->short_circuit = hex_cpu->short_circuit;
10668b453a2bSTaylor Simpson }
10678b453a2bSTaylor Simpson 
10688b453a2bSTaylor Simpson static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
10698b453a2bSTaylor Simpson {
10708b453a2bSTaylor Simpson }
10718b453a2bSTaylor Simpson 
10728b453a2bSTaylor Simpson static void hexagon_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
10738b453a2bSTaylor Simpson {
10748b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
10758b453a2bSTaylor Simpson 
10768b453a2bSTaylor Simpson     tcg_gen_insn_start(ctx->base.pc_next);
10778b453a2bSTaylor Simpson }
10788b453a2bSTaylor Simpson 
10798b453a2bSTaylor Simpson static bool pkt_crosses_page(CPUHexagonState *env, DisasContext *ctx)
10808b453a2bSTaylor Simpson {
10818b453a2bSTaylor Simpson     target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
10828b453a2bSTaylor Simpson     bool found_end = false;
10838b453a2bSTaylor Simpson     int nwords;
10848b453a2bSTaylor Simpson 
10858b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
10868b453a2bSTaylor Simpson         uint32_t word = cpu_ldl_code(env,
10878b453a2bSTaylor Simpson                             ctx->base.pc_next + nwords * sizeof(uint32_t));
10888b453a2bSTaylor Simpson         found_end = is_packet_end(word);
10898b453a2bSTaylor Simpson     }
10908b453a2bSTaylor Simpson     uint32_t next_ptr =  ctx->base.pc_next + nwords * sizeof(uint32_t);
10918b453a2bSTaylor Simpson     return found_end && next_ptr - page_start >= TARGET_PAGE_SIZE;
10928b453a2bSTaylor Simpson }
10938b453a2bSTaylor Simpson 
10948b453a2bSTaylor Simpson static void hexagon_tr_translate_packet(DisasContextBase *dcbase, CPUState *cpu)
10958b453a2bSTaylor Simpson {
10968b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1097b77af26eSRichard Henderson     CPUHexagonState *env = cpu_env(cpu);
10988b453a2bSTaylor Simpson 
10998b453a2bSTaylor Simpson     decode_and_translate_packet(env, ctx);
11008b453a2bSTaylor Simpson 
11018b453a2bSTaylor Simpson     if (ctx->base.is_jmp == DISAS_NEXT) {
11028b453a2bSTaylor Simpson         target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
11038b453a2bSTaylor Simpson         target_ulong bytes_max = PACKET_WORDS_MAX * sizeof(target_ulong);
11048b453a2bSTaylor Simpson 
11058b453a2bSTaylor Simpson         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE ||
11068b453a2bSTaylor Simpson             (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE - bytes_max &&
11078b453a2bSTaylor Simpson              pkt_crosses_page(env, ctx))) {
11088b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
11098b453a2bSTaylor Simpson         }
11108b453a2bSTaylor Simpson 
11118b453a2bSTaylor Simpson         /*
11128b453a2bSTaylor Simpson          * The CPU log is used to compare against LLDB single stepping,
11138b453a2bSTaylor Simpson          * so end the TLB after every packet.
11148b453a2bSTaylor Simpson          */
11157d9ab202STaylor Simpson         HexagonCPU *hex_cpu = env_archcpu(env);
11168b453a2bSTaylor Simpson         if (hex_cpu->lldb_compat && qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
11178b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
11188b453a2bSTaylor Simpson         }
11198b453a2bSTaylor Simpson     }
11208b453a2bSTaylor Simpson }
11218b453a2bSTaylor Simpson 
11228b453a2bSTaylor Simpson static void hexagon_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
11238b453a2bSTaylor Simpson {
11248b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
11258b453a2bSTaylor Simpson 
11268b453a2bSTaylor Simpson     switch (ctx->base.is_jmp) {
11278b453a2bSTaylor Simpson     case DISAS_TOO_MANY:
11288b453a2bSTaylor Simpson         gen_exec_counters(ctx);
11298b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next);
11308b453a2bSTaylor Simpson         tcg_gen_exit_tb(NULL, 0);
11318b453a2bSTaylor Simpson         break;
11328b453a2bSTaylor Simpson     case DISAS_NORETURN:
11338b453a2bSTaylor Simpson         break;
11348b453a2bSTaylor Simpson     default:
11358b453a2bSTaylor Simpson         g_assert_not_reached();
11368b453a2bSTaylor Simpson     }
11378b453a2bSTaylor Simpson }
11388b453a2bSTaylor Simpson 
11398eb806a7SRichard Henderson static void hexagon_tr_disas_log(const DisasContextBase *dcbase,
11408eb806a7SRichard Henderson                                  CPUState *cpu, FILE *logfile)
11418b453a2bSTaylor Simpson {
11428eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
11438eb806a7SRichard Henderson     target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
11448b453a2bSTaylor Simpson }
11458b453a2bSTaylor Simpson 
11468b453a2bSTaylor Simpson 
11478b453a2bSTaylor Simpson static const TranslatorOps hexagon_tr_ops = {
11488b453a2bSTaylor Simpson     .init_disas_context = hexagon_tr_init_disas_context,
11498b453a2bSTaylor Simpson     .tb_start           = hexagon_tr_tb_start,
11508b453a2bSTaylor Simpson     .insn_start         = hexagon_tr_insn_start,
11518b453a2bSTaylor Simpson     .translate_insn     = hexagon_tr_translate_packet,
11528b453a2bSTaylor Simpson     .tb_stop            = hexagon_tr_tb_stop,
11538b453a2bSTaylor Simpson     .disas_log          = hexagon_tr_disas_log,
11548b453a2bSTaylor Simpson };
11558b453a2bSTaylor Simpson 
1156597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
1157306c8721SRichard Henderson                            target_ulong pc, void *host_pc)
11588b453a2bSTaylor Simpson {
11598b453a2bSTaylor Simpson     DisasContext ctx;
11608b453a2bSTaylor Simpson 
1161306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc,
1162306c8721SRichard Henderson                     &hexagon_tr_ops, &ctx.base);
11638b453a2bSTaylor Simpson }
11648b453a2bSTaylor Simpson 
11658b453a2bSTaylor Simpson #define NAME_LEN               64
11668b453a2bSTaylor Simpson static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
11678b453a2bSTaylor Simpson static char store_addr_names[STORES_MAX][NAME_LEN];
11688b453a2bSTaylor Simpson static char store_width_names[STORES_MAX][NAME_LEN];
11698b453a2bSTaylor Simpson static char store_val32_names[STORES_MAX][NAME_LEN];
11708b453a2bSTaylor Simpson static char store_val64_names[STORES_MAX][NAME_LEN];
1171a82dd548STaylor Simpson static char vstore_addr_names[VSTORES_MAX][NAME_LEN];
1172a82dd548STaylor Simpson static char vstore_size_names[VSTORES_MAX][NAME_LEN];
1173a82dd548STaylor Simpson static char vstore_pending_names[VSTORES_MAX][NAME_LEN];
11748b453a2bSTaylor Simpson 
11758b453a2bSTaylor Simpson void hexagon_translate_init(void)
11768b453a2bSTaylor Simpson {
11778b453a2bSTaylor Simpson     int i;
11788b453a2bSTaylor Simpson 
11798b453a2bSTaylor Simpson     opcode_init();
11808b453a2bSTaylor Simpson 
11818b453a2bSTaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
1182ad75a51eSRichard Henderson         hex_gpr[i] = tcg_global_mem_new(tcg_env,
11838b453a2bSTaylor Simpson             offsetof(CPUHexagonState, gpr[i]),
11848b453a2bSTaylor Simpson             hexagon_regnames[i]);
11858b453a2bSTaylor Simpson 
118685580a65STaylor Simpson         if (HEX_DEBUG) {
11878b453a2bSTaylor Simpson             snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s",
11888b453a2bSTaylor Simpson                      hexagon_regnames[i]);
1189ad75a51eSRichard Henderson             hex_reg_written[i] = tcg_global_mem_new(tcg_env,
11908b453a2bSTaylor Simpson                 offsetof(CPUHexagonState, reg_written[i]),
11918b453a2bSTaylor Simpson                 reg_written_names[i]);
119285580a65STaylor Simpson         }
11938b453a2bSTaylor Simpson     }
1194ad75a51eSRichard Henderson     hex_new_value_usr = tcg_global_mem_new(tcg_env,
11956aa4f1d1STaylor Simpson         offsetof(CPUHexagonState, new_value_usr), "new_value_usr");
11966aa4f1d1STaylor Simpson 
11978b453a2bSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
1198ad75a51eSRichard Henderson         hex_pred[i] = tcg_global_mem_new(tcg_env,
11998b453a2bSTaylor Simpson             offsetof(CPUHexagonState, pred[i]),
12008b453a2bSTaylor Simpson             hexagon_prednames[i]);
12018b453a2bSTaylor Simpson     }
1202ad75a51eSRichard Henderson     hex_slot_cancelled = tcg_global_mem_new(tcg_env,
12038b453a2bSTaylor Simpson         offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled");
1204ad75a51eSRichard Henderson     hex_llsc_addr = tcg_global_mem_new(tcg_env,
12058b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_addr), "llsc_addr");
1206ad75a51eSRichard Henderson     hex_llsc_val = tcg_global_mem_new(tcg_env,
12078b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val), "llsc_val");
1208ad75a51eSRichard Henderson     hex_llsc_val_i64 = tcg_global_mem_new_i64(tcg_env,
12098b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
12108b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
12118b453a2bSTaylor Simpson         snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
1212ad75a51eSRichard Henderson         hex_store_addr[i] = tcg_global_mem_new(tcg_env,
12138b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].va),
12148b453a2bSTaylor Simpson             store_addr_names[i]);
12158b453a2bSTaylor Simpson 
12168b453a2bSTaylor Simpson         snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i);
1217ad75a51eSRichard Henderson         hex_store_width[i] = tcg_global_mem_new(tcg_env,
12188b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].width),
12198b453a2bSTaylor Simpson             store_width_names[i]);
12208b453a2bSTaylor Simpson 
12218b453a2bSTaylor Simpson         snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i);
1222ad75a51eSRichard Henderson         hex_store_val32[i] = tcg_global_mem_new(tcg_env,
12238b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data32),
12248b453a2bSTaylor Simpson             store_val32_names[i]);
12258b453a2bSTaylor Simpson 
12268b453a2bSTaylor Simpson         snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i);
1227ad75a51eSRichard Henderson         hex_store_val64[i] = tcg_global_mem_new_i64(tcg_env,
12288b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data64),
12298b453a2bSTaylor Simpson             store_val64_names[i]);
12308b453a2bSTaylor Simpson     }
1231*20c34a92SBrian Cain     for (i = 0; i < VSTORES_MAX; i++) {
1232a82dd548STaylor Simpson         snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i);
1233ad75a51eSRichard Henderson         hex_vstore_addr[i] = tcg_global_mem_new(tcg_env,
1234a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].va),
1235a82dd548STaylor Simpson             vstore_addr_names[i]);
1236a82dd548STaylor Simpson 
1237a82dd548STaylor Simpson         snprintf(vstore_size_names[i], NAME_LEN, "vstore_size_%d", i);
1238ad75a51eSRichard Henderson         hex_vstore_size[i] = tcg_global_mem_new(tcg_env,
1239a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].size),
1240a82dd548STaylor Simpson             vstore_size_names[i]);
1241a82dd548STaylor Simpson 
1242a82dd548STaylor Simpson         snprintf(vstore_pending_names[i], NAME_LEN, "vstore_pending_%d", i);
1243ad75a51eSRichard Henderson         hex_vstore_pending[i] = tcg_global_mem_new(tcg_env,
1244a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore_pending[i]),
1245a82dd548STaylor Simpson             vstore_pending_names[i]);
1246a82dd548STaylor Simpson     }
12478b453a2bSTaylor Simpson }
1248