xref: /openbmc/qemu/target/hexagon/translate.c (revision 842b206f)
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"
238b453a2bSTaylor Simpson #include "exec/cpu_ldst.h"
248b453a2bSTaylor Simpson #include "exec/log.h"
258b453a2bSTaylor Simpson #include "internal.h"
268b453a2bSTaylor Simpson #include "attribs.h"
278b453a2bSTaylor Simpson #include "insn.h"
288b453a2bSTaylor Simpson #include "decode.h"
298b453a2bSTaylor Simpson #include "translate.h"
30d54c5615STaylor Simpson #include "genptr.h"
318b453a2bSTaylor Simpson #include "printinsn.h"
328b453a2bSTaylor Simpson 
3310849c26STaylor Simpson #include "analyze_funcs_generated.c.inc"
3410849c26STaylor Simpson 
3510849c26STaylor Simpson typedef void (*AnalyzeInsn)(DisasContext *ctx);
3610849c26STaylor Simpson static const AnalyzeInsn opcode_analyze[XX_LAST_OPCODE] = {
3710849c26STaylor Simpson #define OPCODE(X)    [X] = analyze_##X
3810849c26STaylor Simpson #include "opcodes_def_generated.h.inc"
3910849c26STaylor Simpson #undef OPCODE
4010849c26STaylor Simpson };
4110849c26STaylor Simpson 
428b453a2bSTaylor Simpson TCGv hex_gpr[TOTAL_PER_THREAD_REGS];
438b453a2bSTaylor Simpson TCGv hex_pred[NUM_PREGS];
448b453a2bSTaylor Simpson TCGv hex_this_PC;
458b453a2bSTaylor Simpson TCGv hex_slot_cancelled;
468b453a2bSTaylor Simpson TCGv hex_branch_taken;
476aa4f1d1STaylor Simpson TCGv hex_new_value_usr;
488b453a2bSTaylor Simpson TCGv hex_reg_written[TOTAL_PER_THREAD_REGS];
498b453a2bSTaylor Simpson TCGv hex_store_addr[STORES_MAX];
508b453a2bSTaylor Simpson TCGv hex_store_width[STORES_MAX];
518b453a2bSTaylor Simpson TCGv hex_store_val32[STORES_MAX];
528b453a2bSTaylor Simpson TCGv_i64 hex_store_val64[STORES_MAX];
538b453a2bSTaylor Simpson TCGv hex_pkt_has_store_s1;
548b453a2bSTaylor Simpson TCGv hex_dczero_addr;
558b453a2bSTaylor Simpson TCGv hex_llsc_addr;
568b453a2bSTaylor Simpson TCGv hex_llsc_val;
578b453a2bSTaylor Simpson TCGv_i64 hex_llsc_val_i64;
58a82dd548STaylor Simpson TCGv hex_vstore_addr[VSTORES_MAX];
59a82dd548STaylor Simpson TCGv hex_vstore_size[VSTORES_MAX];
60a82dd548STaylor Simpson TCGv hex_vstore_pending[VSTORES_MAX];
618b453a2bSTaylor Simpson 
628b453a2bSTaylor Simpson static const char * const hexagon_prednames[] = {
638b453a2bSTaylor Simpson   "p0", "p1", "p2", "p3"
648b453a2bSTaylor Simpson };
658b453a2bSTaylor Simpson 
66a82dd548STaylor Simpson intptr_t ctx_future_vreg_off(DisasContext *ctx, int regnum,
67a82dd548STaylor Simpson                           int num, bool alloc_ok)
68a82dd548STaylor Simpson {
69a82dd548STaylor Simpson     intptr_t offset;
70a82dd548STaylor Simpson 
71b8552985STaylor Simpson     if (!ctx->need_commit) {
72b8552985STaylor Simpson         return offsetof(CPUHexagonState, VRegs[regnum]);
73b8552985STaylor Simpson     }
74b8552985STaylor Simpson 
75a82dd548STaylor Simpson     /* See if it is already allocated */
76a82dd548STaylor Simpson     for (int i = 0; i < ctx->future_vregs_idx; i++) {
77a82dd548STaylor Simpson         if (ctx->future_vregs_num[i] == regnum) {
78a82dd548STaylor Simpson             return offsetof(CPUHexagonState, future_VRegs[i]);
79a82dd548STaylor Simpson         }
80a82dd548STaylor Simpson     }
81a82dd548STaylor Simpson 
82a82dd548STaylor Simpson     g_assert(alloc_ok);
83a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, future_VRegs[ctx->future_vregs_idx]);
84a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
85a82dd548STaylor Simpson         ctx->future_vregs_num[ctx->future_vregs_idx + i] = regnum++;
86a82dd548STaylor Simpson     }
87a82dd548STaylor Simpson     ctx->future_vregs_idx += num;
88a82dd548STaylor Simpson     g_assert(ctx->future_vregs_idx <= VECTOR_TEMPS_MAX);
89a82dd548STaylor Simpson     return offset;
90a82dd548STaylor Simpson }
91a82dd548STaylor Simpson 
92a82dd548STaylor Simpson intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,
93a82dd548STaylor Simpson                           int num, bool alloc_ok)
94a82dd548STaylor Simpson {
95a82dd548STaylor Simpson     intptr_t offset;
96a82dd548STaylor Simpson 
97a82dd548STaylor Simpson     /* See if it is already allocated */
98a82dd548STaylor Simpson     for (int i = 0; i < ctx->tmp_vregs_idx; i++) {
99a82dd548STaylor Simpson         if (ctx->tmp_vregs_num[i] == regnum) {
100a82dd548STaylor Simpson             return offsetof(CPUHexagonState, tmp_VRegs[i]);
101a82dd548STaylor Simpson         }
102a82dd548STaylor Simpson     }
103a82dd548STaylor Simpson 
104a82dd548STaylor Simpson     g_assert(alloc_ok);
105a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, tmp_VRegs[ctx->tmp_vregs_idx]);
106a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
107a82dd548STaylor Simpson         ctx->tmp_vregs_num[ctx->tmp_vregs_idx + i] = regnum++;
108a82dd548STaylor Simpson     }
109a82dd548STaylor Simpson     ctx->tmp_vregs_idx += num;
110a82dd548STaylor Simpson     g_assert(ctx->tmp_vregs_idx <= VECTOR_TEMPS_MAX);
111a82dd548STaylor Simpson     return offset;
112a82dd548STaylor Simpson }
113a82dd548STaylor Simpson 
114743debbcSTaylor Simpson static void gen_exception_raw(int excp)
1158b453a2bSTaylor Simpson {
11623803bbeSPhilippe Mathieu-Daudé     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
1178b453a2bSTaylor Simpson }
1188b453a2bSTaylor Simpson 
119743debbcSTaylor Simpson static void gen_exec_counters(DisasContext *ctx)
1208b453a2bSTaylor Simpson {
121743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT],
122743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_PKT_CNT], ctx->num_packets);
123743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_INSN_CNT],
124743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns);
125a82dd548STaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT],
126a82dd548STaylor Simpson                     hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns);
127743debbcSTaylor Simpson }
128743debbcSTaylor Simpson 
1291b9a7f2aSTaylor Simpson static bool use_goto_tb(DisasContext *ctx, target_ulong dest)
1301b9a7f2aSTaylor Simpson {
1311b9a7f2aSTaylor Simpson     return translator_use_goto_tb(&ctx->base, dest);
1321b9a7f2aSTaylor Simpson }
1331b9a7f2aSTaylor Simpson 
134bee1fc56SMatheus Tavares Bernardino static void gen_goto_tb(DisasContext *ctx, int idx, target_ulong dest, bool
135bee1fc56SMatheus Tavares Bernardino                         move_to_pc)
1361b9a7f2aSTaylor Simpson {
1371b9a7f2aSTaylor Simpson     if (use_goto_tb(ctx, dest)) {
1381b9a7f2aSTaylor Simpson         tcg_gen_goto_tb(idx);
139bee1fc56SMatheus Tavares Bernardino         if (move_to_pc) {
1401b9a7f2aSTaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
141bee1fc56SMatheus Tavares Bernardino         }
1421b9a7f2aSTaylor Simpson         tcg_gen_exit_tb(ctx->base.tb, idx);
1431b9a7f2aSTaylor Simpson     } else {
144bee1fc56SMatheus Tavares Bernardino         if (move_to_pc) {
1451b9a7f2aSTaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
146bee1fc56SMatheus Tavares Bernardino         }
1471b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1481b9a7f2aSTaylor Simpson     }
1491b9a7f2aSTaylor Simpson }
1501b9a7f2aSTaylor Simpson 
151743debbcSTaylor Simpson static void gen_end_tb(DisasContext *ctx)
152743debbcSTaylor Simpson {
153564b2040STaylor Simpson     Packet *pkt = ctx->pkt;
154564b2040STaylor Simpson 
155743debbcSTaylor Simpson     gen_exec_counters(ctx);
1561b9a7f2aSTaylor Simpson 
1571b9a7f2aSTaylor Simpson     if (ctx->branch_cond != TCG_COND_NEVER) {
1581b9a7f2aSTaylor Simpson         if (ctx->branch_cond != TCG_COND_ALWAYS) {
1591b9a7f2aSTaylor Simpson             TCGLabel *skip = gen_new_label();
1601b9a7f2aSTaylor Simpson             tcg_gen_brcondi_tl(ctx->branch_cond, hex_branch_taken, 0, skip);
161bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 0, ctx->branch_dest, true);
1621b9a7f2aSTaylor Simpson             gen_set_label(skip);
163bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 1, ctx->next_PC, false);
1641b9a7f2aSTaylor Simpson         } else {
165bee1fc56SMatheus Tavares Bernardino             gen_goto_tb(ctx, 0, ctx->branch_dest, true);
1661b9a7f2aSTaylor Simpson         }
167564b2040STaylor Simpson     } else if (ctx->is_tight_loop &&
168564b2040STaylor Simpson                pkt->insn[pkt->num_insns - 1].opcode == J2_endloop0) {
169564b2040STaylor Simpson         /*
170564b2040STaylor Simpson          * When we're in a tight loop, we defer the endloop0 processing
171564b2040STaylor Simpson          * to take advantage of direct block chaining
172564b2040STaylor Simpson          */
173564b2040STaylor Simpson         TCGLabel *skip = gen_new_label();
174564b2040STaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC0], 1, skip);
175564b2040STaylor Simpson         tcg_gen_subi_tl(hex_gpr[HEX_REG_LC0], hex_gpr[HEX_REG_LC0], 1);
176bee1fc56SMatheus Tavares Bernardino         gen_goto_tb(ctx, 0, ctx->base.tb->pc, true);
177564b2040STaylor Simpson         gen_set_label(skip);
178bee1fc56SMatheus Tavares Bernardino         gen_goto_tb(ctx, 1, ctx->next_PC, false);
1791b9a7f2aSTaylor Simpson     } else {
1801b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1811b9a7f2aSTaylor Simpson     }
1821b9a7f2aSTaylor Simpson 
183743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
184743debbcSTaylor Simpson }
185743debbcSTaylor Simpson 
186743debbcSTaylor Simpson static void gen_exception_end_tb(DisasContext *ctx, int excp)
187743debbcSTaylor Simpson {
188743debbcSTaylor Simpson     gen_exec_counters(ctx);
189613653e5STaylor Simpson     tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC);
190743debbcSTaylor Simpson     gen_exception_raw(excp);
191743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
192743debbcSTaylor Simpson 
1938b453a2bSTaylor Simpson }
1948b453a2bSTaylor Simpson 
1958b453a2bSTaylor Simpson #define PACKET_BUFFER_LEN              1028
1968b453a2bSTaylor Simpson static void print_pkt(Packet *pkt)
1978b453a2bSTaylor Simpson {
1988b453a2bSTaylor Simpson     GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
1998b453a2bSTaylor Simpson     snprint_a_pkt_debug(buf, pkt);
2008b453a2bSTaylor Simpson     HEX_DEBUG_LOG("%s", buf->str);
2018b453a2bSTaylor Simpson     g_string_free(buf, true);
2028b453a2bSTaylor Simpson }
20385580a65STaylor Simpson #define HEX_DEBUG_PRINT_PKT(pkt) \
20485580a65STaylor Simpson     do { \
20585580a65STaylor Simpson         if (HEX_DEBUG) { \
20685580a65STaylor Simpson             print_pkt(pkt); \
20785580a65STaylor Simpson         } \
20885580a65STaylor Simpson     } while (0)
2098b453a2bSTaylor Simpson 
2108b453a2bSTaylor Simpson static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
2118b453a2bSTaylor Simpson                              uint32_t words[])
2128b453a2bSTaylor Simpson {
2138b453a2bSTaylor Simpson     bool found_end = false;
2148b453a2bSTaylor Simpson     int nwords, max_words;
2158b453a2bSTaylor Simpson 
2168b453a2bSTaylor Simpson     memset(words, 0, PACKET_WORDS_MAX * sizeof(uint32_t));
2178b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
218a27c100cSTaylor Simpson         words[nwords] =
2194e116893SIlya Leoshkevich             translator_ldl(env, &ctx->base,
2204e116893SIlya Leoshkevich                            ctx->base.pc_next + nwords * sizeof(uint32_t));
2218b453a2bSTaylor Simpson         found_end = is_packet_end(words[nwords]);
2228b453a2bSTaylor Simpson     }
2238b453a2bSTaylor Simpson     if (!found_end) {
2248b453a2bSTaylor Simpson         /* Read too many words without finding the end */
2258b453a2bSTaylor Simpson         return 0;
2268b453a2bSTaylor Simpson     }
2278b453a2bSTaylor Simpson 
2288b453a2bSTaylor Simpson     /* Check for page boundary crossing */
2298b453a2bSTaylor Simpson     max_words = -(ctx->base.pc_next | TARGET_PAGE_MASK) / sizeof(uint32_t);
2308b453a2bSTaylor Simpson     if (nwords > max_words) {
2318b453a2bSTaylor Simpson         /* We can only cross a page boundary at the beginning of a TB */
2328b453a2bSTaylor Simpson         g_assert(ctx->base.num_insns == 1);
2338b453a2bSTaylor Simpson     }
2348b453a2bSTaylor Simpson 
2358b453a2bSTaylor Simpson     HEX_DEBUG_LOG("decode_packet: pc = 0x%x\n", ctx->base.pc_next);
2368b453a2bSTaylor Simpson     HEX_DEBUG_LOG("    words = { ");
2378b453a2bSTaylor Simpson     for (int i = 0; i < nwords; i++) {
2388b453a2bSTaylor Simpson         HEX_DEBUG_LOG("0x%x, ", words[i]);
2398b453a2bSTaylor Simpson     }
2408b453a2bSTaylor Simpson     HEX_DEBUG_LOG("}\n");
2418b453a2bSTaylor Simpson 
2428b453a2bSTaylor Simpson     return nwords;
2438b453a2bSTaylor Simpson }
2448b453a2bSTaylor Simpson 
245d54c5615STaylor Simpson static bool check_for_attrib(Packet *pkt, int attrib)
2468b453a2bSTaylor Simpson {
2478b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2488b453a2bSTaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, attrib)) {
2498b453a2bSTaylor Simpson             return true;
2508b453a2bSTaylor Simpson         }
2518b453a2bSTaylor Simpson     }
2528b453a2bSTaylor Simpson     return false;
2538b453a2bSTaylor Simpson }
2548b453a2bSTaylor Simpson 
2558b453a2bSTaylor Simpson static bool need_slot_cancelled(Packet *pkt)
2568b453a2bSTaylor Simpson {
257c2b33d0bSTaylor Simpson     /* We only need slot_cancelled for conditional store instructions */
2587b84fd04STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2597b84fd04STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
2607b84fd04STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) &&
261c2b33d0bSTaylor Simpson             GET_ATTRIB(opcode, A_SCALAR_STORE)) {
2627b84fd04STaylor Simpson             return true;
2637b84fd04STaylor Simpson         }
2647b84fd04STaylor Simpson     }
2657b84fd04STaylor Simpson     return false;
2668b453a2bSTaylor Simpson }
2678b453a2bSTaylor Simpson 
268613653e5STaylor Simpson static bool need_next_PC(DisasContext *ctx)
269613653e5STaylor Simpson {
270613653e5STaylor Simpson     Packet *pkt = ctx->pkt;
271613653e5STaylor Simpson 
272613653e5STaylor Simpson     /* Check for conditional control flow or HW loop end */
273613653e5STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
274613653e5STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
275613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) && GET_ATTRIB(opcode, A_COF)) {
276613653e5STaylor Simpson             return true;
277613653e5STaylor Simpson         }
278613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_HWLOOP0_END) ||
279613653e5STaylor Simpson             GET_ATTRIB(opcode, A_HWLOOP1_END)) {
280613653e5STaylor Simpson             return true;
281613653e5STaylor Simpson         }
282613653e5STaylor Simpson     }
283613653e5STaylor Simpson     return false;
284613653e5STaylor Simpson }
285613653e5STaylor Simpson 
28610849c26STaylor Simpson /*
28710849c26STaylor Simpson  * The opcode_analyze functions mark most of the writes in a packet
28810849c26STaylor Simpson  * However, there are some implicit writes marked as attributes
28910849c26STaylor Simpson  * of the applicable instructions.
29010849c26STaylor Simpson  */
29110849c26STaylor Simpson static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
29210849c26STaylor Simpson {
29310849c26STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
29410849c26STaylor Simpson     if (GET_ATTRIB(opcode, attrib)) {
29510849c26STaylor Simpson         /*
29610849c26STaylor Simpson          * USR is used to set overflow and FP exceptions,
29710849c26STaylor Simpson          * so treat it as conditional
29810849c26STaylor Simpson          */
29910849c26STaylor Simpson         bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) ||
30010849c26STaylor Simpson                              rnum == HEX_REG_USR;
30110849c26STaylor Simpson 
30210849c26STaylor Simpson         /* LC0/LC1 is conditionally written by endloop instructions */
30310849c26STaylor Simpson         if ((rnum == HEX_REG_LC0 || rnum == HEX_REG_LC1) &&
30410849c26STaylor Simpson             (opcode == J2_endloop0 ||
30510849c26STaylor Simpson              opcode == J2_endloop1 ||
30610849c26STaylor Simpson              opcode == J2_endloop01)) {
30710849c26STaylor Simpson             is_predicated = true;
30810849c26STaylor Simpson         }
30910849c26STaylor Simpson 
31010849c26STaylor Simpson         ctx_log_reg_write(ctx, rnum, is_predicated);
31110849c26STaylor Simpson     }
31210849c26STaylor Simpson }
31310849c26STaylor Simpson 
31410849c26STaylor Simpson static void mark_implicit_reg_writes(DisasContext *ctx)
31510849c26STaylor Simpson {
31610849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP,  HEX_REG_FP);
31710849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SP,  HEX_REG_SP);
31810849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LR,  HEX_REG_LR);
31910849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC0, HEX_REG_LC0);
32010849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0);
32110849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1);
32210849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1);
32310849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR);
32410849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR);
32510849c26STaylor Simpson }
32610849c26STaylor Simpson 
32710849c26STaylor Simpson static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum)
32810849c26STaylor Simpson {
32910849c26STaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
33010849c26STaylor Simpson         ctx_log_pred_write(ctx, pnum);
33110849c26STaylor Simpson     }
33210849c26STaylor Simpson }
33310849c26STaylor Simpson 
33410849c26STaylor Simpson static void mark_implicit_pred_writes(DisasContext *ctx)
33510849c26STaylor Simpson {
33610849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P0, 0);
33710849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P1, 1);
33810849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P2, 2);
33910849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P3, 3);
34010849c26STaylor Simpson }
34110849c26STaylor Simpson 
342d54c5615STaylor Simpson static bool pkt_raises_exception(Packet *pkt)
343d54c5615STaylor Simpson {
344d54c5615STaylor Simpson     if (check_for_attrib(pkt, A_LOAD) ||
345d54c5615STaylor Simpson         check_for_attrib(pkt, A_STORE)) {
346d54c5615STaylor Simpson         return true;
347d54c5615STaylor Simpson     }
348d54c5615STaylor Simpson     return false;
349d54c5615STaylor Simpson }
350d54c5615STaylor Simpson 
351d54c5615STaylor Simpson static bool need_commit(DisasContext *ctx)
352d54c5615STaylor Simpson {
353d54c5615STaylor Simpson     Packet *pkt = ctx->pkt;
354d54c5615STaylor Simpson 
355d54c5615STaylor Simpson     /*
356d54c5615STaylor Simpson      * If the short-circuit property is set to false, we'll always do the commit
357d54c5615STaylor Simpson      */
358d54c5615STaylor Simpson     if (!ctx->short_circuit) {
359d54c5615STaylor Simpson         return true;
360d54c5615STaylor Simpson     }
361d54c5615STaylor Simpson 
362d54c5615STaylor Simpson     if (pkt_raises_exception(pkt)) {
363d54c5615STaylor Simpson         return true;
364d54c5615STaylor Simpson     }
365d54c5615STaylor Simpson 
366d54c5615STaylor Simpson     /* Registers with immutability flags require new_value */
367d54c5615STaylor Simpson     for (int i = 0; i < ctx->reg_log_idx; i++) {
368d54c5615STaylor Simpson         int rnum = ctx->reg_log[i];
369d54c5615STaylor Simpson         if (reg_immut_masks[rnum]) {
370d54c5615STaylor Simpson             return true;
371d54c5615STaylor Simpson         }
372d54c5615STaylor Simpson     }
373d54c5615STaylor Simpson 
374d54c5615STaylor Simpson     /* Floating point instructions are hard-coded to use new_value */
375d54c5615STaylor Simpson     if (check_for_attrib(pkt, A_FPOP)) {
376d54c5615STaylor Simpson         return true;
377d54c5615STaylor Simpson     }
378d54c5615STaylor Simpson 
379d05d5eebSTaylor Simpson     if (pkt->num_insns == 1) {
380d05d5eebSTaylor Simpson         if (pkt->pkt_has_hvx) {
381d05d5eebSTaylor Simpson             /*
382d05d5eebSTaylor Simpson              * The HVX instructions with generated helpers use
383d05d5eebSTaylor Simpson              * pass-by-reference, so they need the read/write overlap
384d05d5eebSTaylor Simpson              * check below.
385d05d5eebSTaylor Simpson              * The HVX instructions with overrides are OK.
386d05d5eebSTaylor Simpson              */
387d05d5eebSTaylor Simpson             if (!ctx->has_hvx_helper) {
388d54c5615STaylor Simpson                 return false;
389d54c5615STaylor Simpson             }
390d05d5eebSTaylor Simpson         } else {
391d05d5eebSTaylor Simpson             return false;
392d05d5eebSTaylor Simpson         }
393d05d5eebSTaylor Simpson     }
394d54c5615STaylor Simpson 
395d54c5615STaylor Simpson     /* Check for overlap between register reads and writes */
396d54c5615STaylor Simpson     for (int i = 0; i < ctx->reg_log_idx; i++) {
397d54c5615STaylor Simpson         int rnum = ctx->reg_log[i];
398d54c5615STaylor Simpson         if (test_bit(rnum, ctx->regs_read)) {
399d54c5615STaylor Simpson             return true;
400d54c5615STaylor Simpson         }
401d54c5615STaylor Simpson     }
402d54c5615STaylor Simpson 
403455e169dSTaylor Simpson     /* Check for overlap between predicate reads and writes */
404455e169dSTaylor Simpson     for (int i = 0; i < ctx->preg_log_idx; i++) {
405455e169dSTaylor Simpson         int pnum = ctx->preg_log[i];
406455e169dSTaylor Simpson         if (test_bit(pnum, ctx->pregs_read)) {
407455e169dSTaylor Simpson             return true;
408455e169dSTaylor Simpson         }
409455e169dSTaylor Simpson     }
410455e169dSTaylor Simpson 
411b8552985STaylor Simpson     /* Check for overlap between HVX reads and writes */
412b8552985STaylor Simpson     for (int i = 0; i < ctx->vreg_log_idx; i++) {
413b8552985STaylor Simpson         int vnum = ctx->vreg_log[i];
414b8552985STaylor Simpson         if (test_bit(vnum, ctx->vregs_read)) {
415b8552985STaylor Simpson             return true;
416b8552985STaylor Simpson         }
417b8552985STaylor Simpson     }
418b8552985STaylor Simpson     if (!bitmap_empty(ctx->vregs_updated_tmp, NUM_VREGS)) {
419b8552985STaylor Simpson         int i = find_first_bit(ctx->vregs_updated_tmp, NUM_VREGS);
420b8552985STaylor Simpson         while (i < NUM_VREGS) {
421b8552985STaylor Simpson             if (test_bit(i, ctx->vregs_read)) {
422b8552985STaylor Simpson                 return true;
423b8552985STaylor Simpson             }
424b8552985STaylor Simpson             i = find_next_bit(ctx->vregs_updated_tmp, NUM_VREGS, i + 1);
425b8552985STaylor Simpson         }
426b8552985STaylor Simpson     }
427b8552985STaylor Simpson     if (!bitmap_empty(ctx->vregs_select, NUM_VREGS)) {
428b8552985STaylor Simpson         int i = find_first_bit(ctx->vregs_select, NUM_VREGS);
429b8552985STaylor Simpson         while (i < NUM_VREGS) {
430b8552985STaylor Simpson             if (test_bit(i, ctx->vregs_read)) {
431b8552985STaylor Simpson                 return true;
432b8552985STaylor Simpson             }
433b8552985STaylor Simpson             i = find_next_bit(ctx->vregs_select, NUM_VREGS, i + 1);
434b8552985STaylor Simpson         }
435b8552985STaylor Simpson     }
436b8552985STaylor Simpson 
437b8552985STaylor Simpson     /* Check for overlap between HVX predicate reads and writes */
438b8552985STaylor Simpson     for (int i = 0; i < ctx->qreg_log_idx; i++) {
439b8552985STaylor Simpson         int qnum = ctx->qreg_log[i];
440b8552985STaylor Simpson         if (test_bit(qnum, ctx->qregs_read)) {
441b8552985STaylor Simpson             return true;
442b8552985STaylor Simpson         }
443b8552985STaylor Simpson     }
444b8552985STaylor Simpson 
445d54c5615STaylor Simpson     return false;
446d54c5615STaylor Simpson }
447d54c5615STaylor Simpson 
448b9f0326bSTaylor Simpson static void mark_implicit_pred_read(DisasContext *ctx, int attrib, int pnum)
449b9f0326bSTaylor Simpson {
450b9f0326bSTaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
451b9f0326bSTaylor Simpson         ctx_log_pred_read(ctx, pnum);
452b9f0326bSTaylor Simpson     }
453b9f0326bSTaylor Simpson }
454b9f0326bSTaylor Simpson 
455b9f0326bSTaylor Simpson static void mark_implicit_pred_reads(DisasContext *ctx)
456b9f0326bSTaylor Simpson {
457b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P0, 0);
458b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P1, 1);
459b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P3, 2);
460b9f0326bSTaylor Simpson     mark_implicit_pred_read(ctx, A_IMPLICIT_READS_P3, 3);
461b9f0326bSTaylor Simpson }
462b9f0326bSTaylor Simpson 
46310849c26STaylor Simpson static void analyze_packet(DisasContext *ctx)
46410849c26STaylor Simpson {
46510849c26STaylor Simpson     Packet *pkt = ctx->pkt;
4664d13bb51STaylor Simpson     ctx->need_pkt_has_store_s1 = false;
467d05d5eebSTaylor Simpson     ctx->has_hvx_helper = false;
46810849c26STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
46910849c26STaylor Simpson         Insn *insn = &pkt->insn[i];
47010849c26STaylor Simpson         ctx->insn = insn;
47110849c26STaylor Simpson         if (opcode_analyze[insn->opcode]) {
47210849c26STaylor Simpson             opcode_analyze[insn->opcode](ctx);
47310849c26STaylor Simpson         }
47410849c26STaylor Simpson         mark_implicit_reg_writes(ctx);
47510849c26STaylor Simpson         mark_implicit_pred_writes(ctx);
476b9f0326bSTaylor Simpson         mark_implicit_pred_reads(ctx);
47710849c26STaylor Simpson     }
478d54c5615STaylor Simpson 
479d54c5615STaylor Simpson     ctx->need_commit = need_commit(ctx);
48010849c26STaylor Simpson }
48110849c26STaylor Simpson 
4821e536334STaylor Simpson static void gen_start_packet(DisasContext *ctx)
4838b453a2bSTaylor Simpson {
4841e536334STaylor Simpson     Packet *pkt = ctx->pkt;
4858b453a2bSTaylor Simpson     target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes;
4868b453a2bSTaylor Simpson     int i;
4878b453a2bSTaylor Simpson 
4888b453a2bSTaylor Simpson     /* Clear out the disassembly context */
489613653e5STaylor Simpson     ctx->next_PC = next_PC;
4908b453a2bSTaylor Simpson     ctx->reg_log_idx = 0;
4918b453a2bSTaylor Simpson     bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS);
492b9f0326bSTaylor Simpson     bitmap_zero(ctx->regs_read, TOTAL_PER_THREAD_REGS);
49310849c26STaylor Simpson     bitmap_zero(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
4948b453a2bSTaylor Simpson     ctx->preg_log_idx = 0;
4956c677c60STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
496b9f0326bSTaylor Simpson     bitmap_zero(ctx->pregs_read, NUM_PREGS);
497a82dd548STaylor Simpson     ctx->future_vregs_idx = 0;
498a82dd548STaylor Simpson     ctx->tmp_vregs_idx = 0;
499a82dd548STaylor Simpson     ctx->vreg_log_idx = 0;
500a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated_tmp, NUM_VREGS);
501a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated, NUM_VREGS);
502a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_select, NUM_VREGS);
5034d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_future_vregs, NUM_VREGS);
5044d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_tmp_vregs, NUM_VREGS);
505b9f0326bSTaylor Simpson     bitmap_zero(ctx->vregs_read, NUM_VREGS);
506b9f0326bSTaylor Simpson     bitmap_zero(ctx->qregs_read, NUM_QREGS);
507a82dd548STaylor Simpson     ctx->qreg_log_idx = 0;
5088b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
5098b453a2bSTaylor Simpson         ctx->store_width[i] = 0;
5108b453a2bSTaylor Simpson     }
51192cfa25fSTaylor Simpson     ctx->s1_store_processed = false;
512a82dd548STaylor Simpson     ctx->pre_commit = true;
5134ff56764STaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
5144ff56764STaylor Simpson         ctx->new_value[i] = NULL;
5154ff56764STaylor Simpson     }
516e22edc7cSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
517e22edc7cSTaylor Simpson         ctx->new_pred_value[i] = NULL;
518e22edc7cSTaylor Simpson     }
5198b453a2bSTaylor Simpson 
52010849c26STaylor Simpson     analyze_packet(ctx);
52110849c26STaylor Simpson 
5224d13bb51STaylor Simpson     if (ctx->need_pkt_has_store_s1) {
5234d13bb51STaylor Simpson         tcg_gen_movi_tl(hex_pkt_has_store_s1, pkt->pkt_has_store_s1);
5244d13bb51STaylor Simpson     }
5254d13bb51STaylor Simpson 
52610849c26STaylor Simpson     /*
52710849c26STaylor Simpson      * pregs_written is used both in the analyze phase as well as the code
52810849c26STaylor Simpson      * gen phase, so clear it again.
52910849c26STaylor Simpson      */
53010849c26STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
53110849c26STaylor Simpson 
53285580a65STaylor Simpson     if (HEX_DEBUG) {
5338b453a2bSTaylor Simpson         /* Handy place to set a breakpoint before the packet executes */
5348b453a2bSTaylor Simpson         gen_helper_debug_start_packet(cpu_env);
5358b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_this_PC, ctx->base.pc_next);
53685580a65STaylor Simpson     }
5378b453a2bSTaylor Simpson 
5388b453a2bSTaylor Simpson     /* Initialize the runtime state for packet semantics */
5398b453a2bSTaylor Simpson     if (need_slot_cancelled(pkt)) {
5408b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_slot_cancelled, 0);
5418b453a2bSTaylor Simpson     }
5428b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
543fb67c2bfSTaylor Simpson         if (pkt->pkt_has_multi_cof) {
5448b453a2bSTaylor Simpson             tcg_gen_movi_tl(hex_branch_taken, 0);
545fb67c2bfSTaylor Simpson         }
546613653e5STaylor Simpson         if (need_next_PC(ctx)) {
547613653e5STaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], next_PC);
548613653e5STaylor Simpson         }
5498b453a2bSTaylor Simpson     }
55025e1d87dSTaylor Simpson     if (HEX_DEBUG) {
551*842b206fSTaylor Simpson         ctx->pred_written = tcg_temp_new();
552*842b206fSTaylor Simpson         tcg_gen_movi_tl(ctx->pred_written, 0);
5538b453a2bSTaylor Simpson     }
554a82dd548STaylor Simpson 
5556aa4f1d1STaylor Simpson     /* Preload the predicated registers into get_result_gpr(ctx, i) */
556d54c5615STaylor Simpson     if (ctx->need_commit &&
557d54c5615STaylor Simpson         !bitmap_empty(ctx->predicated_regs, TOTAL_PER_THREAD_REGS)) {
55810849c26STaylor Simpson         int i = find_first_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
55910849c26STaylor Simpson         while (i < TOTAL_PER_THREAD_REGS) {
5606aa4f1d1STaylor Simpson             tcg_gen_mov_tl(get_result_gpr(ctx, i), hex_gpr[i]);
56110849c26STaylor Simpson             i = find_next_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS,
56210849c26STaylor Simpson                               i + 1);
56310849c26STaylor Simpson         }
56410849c26STaylor Simpson     }
56510849c26STaylor Simpson 
56625e1d87dSTaylor Simpson     /*
56725e1d87dSTaylor Simpson      * Preload the predicated pred registers into hex_new_pred_value[pred_num]
56825e1d87dSTaylor Simpson      * Only endloop instructions conditionally write to pred registers
56925e1d87dSTaylor Simpson      */
570455e169dSTaylor Simpson     if (ctx->need_commit && pkt->pkt_has_endloop) {
57125e1d87dSTaylor Simpson         for (int i = 0; i < ctx->preg_log_idx; i++) {
57225e1d87dSTaylor Simpson             int pred_num = ctx->preg_log[i];
573e22edc7cSTaylor Simpson             ctx->new_pred_value[pred_num] = tcg_temp_new();
574e22edc7cSTaylor Simpson             tcg_gen_mov_tl(ctx->new_pred_value[pred_num], hex_pred[pred_num]);
57525e1d87dSTaylor Simpson         }
57625e1d87dSTaylor Simpson     }
57725e1d87dSTaylor Simpson 
5784d6f8420STaylor Simpson     /* Preload the predicated HVX registers into future_VRegs and tmp_VRegs */
5794d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_future_vregs, NUM_VREGS)) {
5804d6f8420STaylor Simpson         int i = find_first_bit(ctx->predicated_future_vregs, NUM_VREGS);
5814d6f8420STaylor Simpson         while (i < NUM_VREGS) {
5824d6f8420STaylor Simpson             const intptr_t VdV_off =
5834d6f8420STaylor Simpson                 ctx_future_vreg_off(ctx, i, 1, true);
5844d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
5854d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
5864d6f8420STaylor Simpson                              src_off,
5874d6f8420STaylor Simpson                              sizeof(MMVector),
5884d6f8420STaylor Simpson                              sizeof(MMVector));
5894d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_future_vregs, NUM_VREGS, i + 1);
5904d6f8420STaylor Simpson         }
5914d6f8420STaylor Simpson     }
5924d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_tmp_vregs, NUM_VREGS)) {
5934d6f8420STaylor Simpson         int i = find_first_bit(ctx->predicated_tmp_vregs, NUM_VREGS);
5944d6f8420STaylor Simpson         while (i < NUM_VREGS) {
5954d6f8420STaylor Simpson             const intptr_t VdV_off =
5964d6f8420STaylor Simpson                 ctx_tmp_vreg_off(ctx, i, 1, true);
5974d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
5984d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
5994d6f8420STaylor Simpson                              src_off,
6004d6f8420STaylor Simpson                              sizeof(MMVector),
6014d6f8420STaylor Simpson                              sizeof(MMVector));
6024d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_tmp_vregs, NUM_VREGS, i + 1);
6034d6f8420STaylor Simpson         }
6044d6f8420STaylor Simpson     }
605a82dd548STaylor Simpson }
606a82dd548STaylor Simpson 
6071e536334STaylor Simpson bool is_gather_store_insn(DisasContext *ctx)
608a82dd548STaylor Simpson {
6091e536334STaylor Simpson     Packet *pkt = ctx->pkt;
6101e536334STaylor Simpson     Insn *insn = ctx->insn;
611a82dd548STaylor Simpson     if (GET_ATTRIB(insn->opcode, A_CVI_NEW) &&
612a82dd548STaylor Simpson         insn->new_value_producer_slot == 1) {
613a82dd548STaylor Simpson         /* Look for gather instruction */
614a82dd548STaylor Simpson         for (int i = 0; i < pkt->num_insns; i++) {
615a82dd548STaylor Simpson             Insn *in = &pkt->insn[i];
616a82dd548STaylor Simpson             if (GET_ATTRIB(in->opcode, A_CVI_GATHER) && in->slot == 1) {
617a82dd548STaylor Simpson                 return true;
618a82dd548STaylor Simpson             }
619a82dd548STaylor Simpson         }
620a82dd548STaylor Simpson     }
621a82dd548STaylor Simpson     return false;
6228b453a2bSTaylor Simpson }
6238b453a2bSTaylor Simpson 
6241e536334STaylor Simpson static void mark_store_width(DisasContext *ctx)
625661ad999STaylor Simpson {
6261e536334STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
6271e536334STaylor Simpson     uint32_t slot = ctx->insn->slot;
628661ad999STaylor Simpson     uint8_t width = 0;
629661ad999STaylor Simpson 
630661ad999STaylor Simpson     if (GET_ATTRIB(opcode, A_SCALAR_STORE)) {
631406c74f2STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_0B)) {
632406c74f2STaylor Simpson             return;
633406c74f2STaylor Simpson         }
634661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_1B)) {
635661ad999STaylor Simpson             width |= 1;
636661ad999STaylor Simpson         }
637661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_2B)) {
638661ad999STaylor Simpson             width |= 2;
639661ad999STaylor Simpson         }
640661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_4B)) {
641661ad999STaylor Simpson             width |= 4;
642661ad999STaylor Simpson         }
643661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_8B)) {
644661ad999STaylor Simpson             width |= 8;
645661ad999STaylor Simpson         }
646661ad999STaylor Simpson         tcg_debug_assert(is_power_of_2(width));
647661ad999STaylor Simpson         ctx->store_width[slot] = width;
648661ad999STaylor Simpson     }
649661ad999STaylor Simpson }
650661ad999STaylor Simpson 
6511e536334STaylor Simpson static void gen_insn(DisasContext *ctx)
6528b453a2bSTaylor Simpson {
6531e536334STaylor Simpson     if (ctx->insn->generate) {
6541e536334STaylor Simpson         ctx->insn->generate(ctx);
6551e536334STaylor Simpson         mark_store_width(ctx);
6568b453a2bSTaylor Simpson     } else {
657743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_OPCODE);
6588b453a2bSTaylor Simpson     }
6598b453a2bSTaylor Simpson }
6608b453a2bSTaylor Simpson 
6618b453a2bSTaylor Simpson /*
6628b453a2bSTaylor Simpson  * Helpers for generating the packet commit
6638b453a2bSTaylor Simpson  */
6648b453a2bSTaylor Simpson static void gen_reg_writes(DisasContext *ctx)
6658b453a2bSTaylor Simpson {
6668b453a2bSTaylor Simpson     int i;
6678b453a2bSTaylor Simpson 
668d54c5615STaylor Simpson     /* Early exit if not needed */
669d54c5615STaylor Simpson     if (!ctx->need_commit) {
670d54c5615STaylor Simpson         return;
671d54c5615STaylor Simpson     }
672d54c5615STaylor Simpson 
6738b453a2bSTaylor Simpson     for (i = 0; i < ctx->reg_log_idx; i++) {
6748b453a2bSTaylor Simpson         int reg_num = ctx->reg_log[i];
6758b453a2bSTaylor Simpson 
6766aa4f1d1STaylor Simpson         tcg_gen_mov_tl(hex_gpr[reg_num], get_result_gpr(ctx, reg_num));
677564b2040STaylor Simpson 
678564b2040STaylor Simpson         /*
679564b2040STaylor Simpson          * ctx->is_tight_loop is set when SA0 points to the beginning of the TB.
680564b2040STaylor Simpson          * If we write to SA0, we have to turn off tight loop handling.
681564b2040STaylor Simpson          */
682564b2040STaylor Simpson         if (reg_num == HEX_REG_SA0) {
683564b2040STaylor Simpson             ctx->is_tight_loop = false;
684564b2040STaylor Simpson         }
6858b453a2bSTaylor Simpson     }
6868b453a2bSTaylor Simpson }
6878b453a2bSTaylor Simpson 
6881e536334STaylor Simpson static void gen_pred_writes(DisasContext *ctx)
6898b453a2bSTaylor Simpson {
690455e169dSTaylor Simpson     /* Early exit if not needed or the log is empty */
691455e169dSTaylor Simpson     if (!ctx->need_commit || !ctx->preg_log_idx) {
6928b453a2bSTaylor Simpson         return;
6938b453a2bSTaylor Simpson     }
6948b453a2bSTaylor Simpson 
69525e1d87dSTaylor Simpson     for (int i = 0; i < ctx->preg_log_idx; i++) {
6968b453a2bSTaylor Simpson         int pred_num = ctx->preg_log[i];
697e22edc7cSTaylor Simpson         tcg_gen_mov_tl(hex_pred[pred_num], ctx->new_pred_value[pred_num]);
6988b453a2bSTaylor Simpson     }
6998b453a2bSTaylor Simpson }
7008b453a2bSTaylor Simpson 
701a27c100cSTaylor Simpson static void gen_check_store_width(DisasContext *ctx, int slot_num)
7028b453a2bSTaylor Simpson {
70385580a65STaylor Simpson     if (HEX_DEBUG) {
70423803bbeSPhilippe Mathieu-Daudé         TCGv slot = tcg_constant_tl(slot_num);
70523803bbeSPhilippe Mathieu-Daudé         TCGv check = tcg_constant_tl(ctx->store_width[slot_num]);
7068b453a2bSTaylor Simpson         gen_helper_debug_check_store_width(cpu_env, slot, check);
70785580a65STaylor Simpson     }
708a27c100cSTaylor Simpson }
7098b453a2bSTaylor Simpson 
7108b453a2bSTaylor Simpson static bool slot_is_predicated(Packet *pkt, int slot_num)
7118b453a2bSTaylor Simpson {
7128b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
7138b453a2bSTaylor Simpson         if (pkt->insn[i].slot == slot_num) {
7148b453a2bSTaylor Simpson             return GET_ATTRIB(pkt->insn[i].opcode, A_CONDEXEC);
7158b453a2bSTaylor Simpson         }
7168b453a2bSTaylor Simpson     }
7178b453a2bSTaylor Simpson     /* If we get to here, we didn't find an instruction in the requested slot */
7188b453a2bSTaylor Simpson     g_assert_not_reached();
7198b453a2bSTaylor Simpson }
7208b453a2bSTaylor Simpson 
7211e536334STaylor Simpson void process_store(DisasContext *ctx, int slot_num)
7228b453a2bSTaylor Simpson {
7231e536334STaylor Simpson     bool is_predicated = slot_is_predicated(ctx->pkt, slot_num);
7248b453a2bSTaylor Simpson     TCGLabel *label_end = NULL;
7258b453a2bSTaylor Simpson 
7268b453a2bSTaylor Simpson     /*
7278b453a2bSTaylor Simpson      * We may have already processed this store
7288b453a2bSTaylor Simpson      * See CHECK_NOSHUF in macros.h
7298b453a2bSTaylor Simpson      */
7308b453a2bSTaylor Simpson     if (slot_num == 1 && ctx->s1_store_processed) {
7318b453a2bSTaylor Simpson         return;
7328b453a2bSTaylor Simpson     }
73392cfa25fSTaylor Simpson     ctx->s1_store_processed = true;
7348b453a2bSTaylor Simpson 
7358b453a2bSTaylor Simpson     if (is_predicated) {
7368b453a2bSTaylor Simpson         TCGv cancelled = tcg_temp_new();
7378b453a2bSTaylor Simpson         label_end = gen_new_label();
7388b453a2bSTaylor Simpson 
7398b453a2bSTaylor Simpson         /* Don't do anything if the slot was cancelled */
7408b453a2bSTaylor Simpson         tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
7418b453a2bSTaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
7428b453a2bSTaylor Simpson     }
7438b453a2bSTaylor Simpson     {
7447a819de8SRichard Henderson         TCGv address = tcg_temp_new();
7458b453a2bSTaylor Simpson         tcg_gen_mov_tl(address, hex_store_addr[slot_num]);
7468b453a2bSTaylor Simpson 
7478b453a2bSTaylor Simpson         /*
7488b453a2bSTaylor Simpson          * If we know the width from the DisasContext, we can
7498b453a2bSTaylor Simpson          * generate much cleaner code.
7508b453a2bSTaylor Simpson          * Unfortunately, not all instructions execute the fSTORE
7518b453a2bSTaylor Simpson          * macro during code generation.  Anything that uses the
7528b453a2bSTaylor Simpson          * generic helper will have this problem.  Instructions
7538b453a2bSTaylor Simpson          * that use fWRAP to generate proper TCG code will be OK.
7548b453a2bSTaylor Simpson          */
7558b453a2bSTaylor Simpson         switch (ctx->store_width[slot_num]) {
7568b453a2bSTaylor Simpson         case 1:
757a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
75853b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7598b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
76053b26d25SRichard Henderson                                ctx->mem_idx, MO_UB);
7618b453a2bSTaylor Simpson             break;
7628b453a2bSTaylor Simpson         case 2:
763a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
76453b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7658b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
76653b26d25SRichard Henderson                                ctx->mem_idx, MO_TEUW);
7678b453a2bSTaylor Simpson             break;
7688b453a2bSTaylor Simpson         case 4:
769a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
77053b26d25SRichard Henderson             tcg_gen_qemu_st_tl(hex_store_val32[slot_num],
7718b453a2bSTaylor Simpson                                hex_store_addr[slot_num],
77253b26d25SRichard Henderson                                ctx->mem_idx, MO_TEUL);
7738b453a2bSTaylor Simpson             break;
7748b453a2bSTaylor Simpson         case 8:
775a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
77653b26d25SRichard Henderson             tcg_gen_qemu_st_i64(hex_store_val64[slot_num],
7778b453a2bSTaylor Simpson                                 hex_store_addr[slot_num],
77853b26d25SRichard Henderson                                 ctx->mem_idx, MO_TEUQ);
7798b453a2bSTaylor Simpson             break;
7808b453a2bSTaylor Simpson         default:
7818b453a2bSTaylor Simpson             {
7828b453a2bSTaylor Simpson                 /*
7838b453a2bSTaylor Simpson                  * If we get to here, we don't know the width at
7848b453a2bSTaylor Simpson                  * TCG generation time, we'll use a helper to
7858b453a2bSTaylor Simpson                  * avoid branching based on the width at runtime.
7868b453a2bSTaylor Simpson                  */
78723803bbeSPhilippe Mathieu-Daudé                 TCGv slot = tcg_constant_tl(slot_num);
7888b453a2bSTaylor Simpson                 gen_helper_commit_store(cpu_env, slot);
7898b453a2bSTaylor Simpson             }
7908b453a2bSTaylor Simpson         }
7918b453a2bSTaylor Simpson     }
7928b453a2bSTaylor Simpson     if (is_predicated) {
7938b453a2bSTaylor Simpson         gen_set_label(label_end);
7948b453a2bSTaylor Simpson     }
7958b453a2bSTaylor Simpson }
7968b453a2bSTaylor Simpson 
7971e536334STaylor Simpson static void process_store_log(DisasContext *ctx)
7988b453a2bSTaylor Simpson {
7998b453a2bSTaylor Simpson     /*
8008b453a2bSTaylor Simpson      *  When a packet has two stores, the hardware processes
801c23b5764STaylor Simpson      *  slot 1 and then slot 0.  This will be important when
8028b453a2bSTaylor Simpson      *  the memory accesses overlap.
8038b453a2bSTaylor Simpson      */
8041e536334STaylor Simpson     Packet *pkt = ctx->pkt;
805e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s1) {
806e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
8071e536334STaylor Simpson         process_store(ctx, 1);
8088b453a2bSTaylor Simpson     }
809e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s0) {
810e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
8111e536334STaylor Simpson         process_store(ctx, 0);
8128b453a2bSTaylor Simpson     }
8138b453a2bSTaylor Simpson }
8148b453a2bSTaylor Simpson 
8158b453a2bSTaylor Simpson /* Zero out a 32-bit cache line */
8161e536334STaylor Simpson static void process_dczeroa(DisasContext *ctx)
8178b453a2bSTaylor Simpson {
8181e536334STaylor Simpson     if (ctx->pkt->pkt_has_dczeroa) {
8198b453a2bSTaylor Simpson         /* Store 32 bytes of zero starting at (addr & ~0x1f) */
8208b453a2bSTaylor Simpson         TCGv addr = tcg_temp_new();
82123803bbeSPhilippe Mathieu-Daudé         TCGv_i64 zero = tcg_constant_i64(0);
8228b453a2bSTaylor Simpson 
8238b453a2bSTaylor Simpson         tcg_gen_andi_tl(addr, hex_dczero_addr, ~0x1f);
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         tcg_gen_addi_tl(addr, addr, 8);
83053b26d25SRichard Henderson         tcg_gen_qemu_st_i64(zero, addr, ctx->mem_idx, MO_UQ);
8318b453a2bSTaylor Simpson     }
8328b453a2bSTaylor Simpson }
8338b453a2bSTaylor Simpson 
834a82dd548STaylor Simpson static bool pkt_has_hvx_store(Packet *pkt)
835a82dd548STaylor Simpson {
836a82dd548STaylor Simpson     int i;
837a82dd548STaylor Simpson     for (i = 0; i < pkt->num_insns; i++) {
838a82dd548STaylor Simpson         int opcode = pkt->insn[i].opcode;
839a82dd548STaylor Simpson         if (GET_ATTRIB(opcode, A_CVI) && GET_ATTRIB(opcode, A_STORE)) {
840a82dd548STaylor Simpson             return true;
841a82dd548STaylor Simpson         }
842a82dd548STaylor Simpson     }
843a82dd548STaylor Simpson     return false;
844a82dd548STaylor Simpson }
845a82dd548STaylor Simpson 
8461e536334STaylor Simpson static void gen_commit_hvx(DisasContext *ctx)
847a82dd548STaylor Simpson {
848a82dd548STaylor Simpson     int i;
849a82dd548STaylor Simpson 
850b8552985STaylor Simpson     /* Early exit if not needed */
851b8552985STaylor Simpson     if (!ctx->need_commit) {
852b8552985STaylor Simpson         g_assert(!pkt_has_hvx_store(ctx->pkt));
853b8552985STaylor Simpson         return;
854b8552985STaylor Simpson     }
855b8552985STaylor Simpson 
856a82dd548STaylor Simpson     /*
857a82dd548STaylor Simpson      *    for (i = 0; i < ctx->vreg_log_idx; i++) {
858a82dd548STaylor Simpson      *        int rnum = ctx->vreg_log[i];
859a82dd548STaylor Simpson      *        env->VRegs[rnum] = env->future_VRegs[rnum];
860a82dd548STaylor Simpson      *    }
861a82dd548STaylor Simpson      */
862a82dd548STaylor Simpson     for (i = 0; i < ctx->vreg_log_idx; i++) {
863a82dd548STaylor Simpson         int rnum = ctx->vreg_log[i];
864a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, VRegs[rnum]);
865a82dd548STaylor Simpson         intptr_t srcoff = ctx_future_vreg_off(ctx, rnum, 1, false);
866a82dd548STaylor Simpson         size_t size = sizeof(MMVector);
867a82dd548STaylor Simpson 
868a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
869a82dd548STaylor Simpson     }
870a82dd548STaylor Simpson 
871a82dd548STaylor Simpson     /*
872a82dd548STaylor Simpson      *    for (i = 0; i < ctx->qreg_log_idx; i++) {
873a82dd548STaylor Simpson      *        int rnum = ctx->qreg_log[i];
874a82dd548STaylor Simpson      *        env->QRegs[rnum] = env->future_QRegs[rnum];
875a82dd548STaylor Simpson      *    }
876a82dd548STaylor Simpson      */
877a82dd548STaylor Simpson     for (i = 0; i < ctx->qreg_log_idx; i++) {
878a82dd548STaylor Simpson         int rnum = ctx->qreg_log[i];
879a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, QRegs[rnum]);
880a82dd548STaylor Simpson         intptr_t srcoff = offsetof(CPUHexagonState, future_QRegs[rnum]);
881a82dd548STaylor Simpson         size_t size = sizeof(MMQReg);
882a82dd548STaylor Simpson 
883a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
884a82dd548STaylor Simpson     }
885a82dd548STaylor Simpson 
8861e536334STaylor Simpson     if (pkt_has_hvx_store(ctx->pkt)) {
887a82dd548STaylor Simpson         gen_helper_commit_hvx_stores(cpu_env);
888a82dd548STaylor Simpson     }
889a82dd548STaylor Simpson }
890a82dd548STaylor Simpson 
8911e536334STaylor Simpson static void update_exec_counters(DisasContext *ctx)
8928b453a2bSTaylor Simpson {
8931e536334STaylor Simpson     Packet *pkt = ctx->pkt;
8948b453a2bSTaylor Simpson     int num_insns = pkt->num_insns;
8958b453a2bSTaylor Simpson     int num_real_insns = 0;
896a82dd548STaylor Simpson     int num_hvx_insns = 0;
8978b453a2bSTaylor Simpson 
8988b453a2bSTaylor Simpson     for (int i = 0; i < num_insns; i++) {
8998b453a2bSTaylor Simpson         if (!pkt->insn[i].is_endloop &&
9008b453a2bSTaylor Simpson             !pkt->insn[i].part1 &&
9018b453a2bSTaylor Simpson             !GET_ATTRIB(pkt->insn[i].opcode, A_IT_NOP)) {
9028b453a2bSTaylor Simpson             num_real_insns++;
9038b453a2bSTaylor Simpson         }
904a82dd548STaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, A_CVI)) {
905a82dd548STaylor Simpson             num_hvx_insns++;
906a82dd548STaylor Simpson         }
9078b453a2bSTaylor Simpson     }
9088b453a2bSTaylor Simpson 
9098b453a2bSTaylor Simpson     ctx->num_packets++;
9108b453a2bSTaylor Simpson     ctx->num_insns += num_real_insns;
911a82dd548STaylor Simpson     ctx->num_hvx_insns += num_hvx_insns;
9128b453a2bSTaylor Simpson }
9138b453a2bSTaylor Simpson 
9141e536334STaylor Simpson static void gen_commit_packet(DisasContext *ctx)
9158b453a2bSTaylor Simpson {
916c23b5764STaylor Simpson     /*
917c23b5764STaylor Simpson      * If there is more than one store in a packet, make sure they are all OK
918c23b5764STaylor Simpson      * before proceeding with the rest of the packet commit.
919c23b5764STaylor Simpson      *
920c23b5764STaylor Simpson      * dczeroa has to be the only store operation in the packet, so we go
921c23b5764STaylor Simpson      * ahead and process that first.
922c23b5764STaylor Simpson      *
923a82dd548STaylor Simpson      * When there is an HVX store, there can also be a scalar store in either
924a82dd548STaylor Simpson      * slot 0 or slot1, so we create a mask for the helper to indicate what
925a82dd548STaylor Simpson      * work to do.
926a82dd548STaylor Simpson      *
927c23b5764STaylor Simpson      * When there are two scalar stores, we probe the one in slot 0.
928c23b5764STaylor Simpson      *
929c23b5764STaylor Simpson      * Note that we don't call the probe helper for packets with only one
930c23b5764STaylor Simpson      * store.  Therefore, we call process_store_log before anything else
931c23b5764STaylor Simpson      * involved in committing the packet.
932c23b5764STaylor Simpson      */
9331e536334STaylor Simpson     Packet *pkt = ctx->pkt;
934c23b5764STaylor Simpson     bool has_store_s0 = pkt->pkt_has_store_s0;
935c23b5764STaylor Simpson     bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed);
936a82dd548STaylor Simpson     bool has_hvx_store = pkt_has_hvx_store(pkt);
937c23b5764STaylor Simpson     if (pkt->pkt_has_dczeroa) {
938c23b5764STaylor Simpson         /*
939c23b5764STaylor Simpson          * The dczeroa will be the store in slot 0, check that we don't have
940a82dd548STaylor Simpson          * a store in slot 1 or an HVX store.
941c23b5764STaylor Simpson          */
942e2be9a5cSTaylor Simpson         g_assert(!has_store_s1 && !has_hvx_store);
9431e536334STaylor Simpson         process_dczeroa(ctx);
944a82dd548STaylor Simpson     } else if (has_hvx_store) {
945a82dd548STaylor Simpson         if (!has_store_s0 && !has_store_s1) {
9462bda44e8STaylor Simpson             TCGv mem_idx = tcg_constant_tl(ctx->mem_idx);
947a82dd548STaylor Simpson             gen_helper_probe_hvx_stores(cpu_env, mem_idx);
948a82dd548STaylor Simpson         } else {
949a82dd548STaylor Simpson             int mask = 0;
950a82dd548STaylor Simpson 
951a82dd548STaylor Simpson             if (has_store_s0) {
9527b84fd04STaylor Simpson                 mask =
9537b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST0, 1);
954a82dd548STaylor Simpson             }
955a82dd548STaylor Simpson             if (has_store_s1) {
9567b84fd04STaylor Simpson                 mask =
9577b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST1, 1);
958a82dd548STaylor Simpson             }
959a82dd548STaylor Simpson             if (has_hvx_store) {
9607b84fd04STaylor Simpson                 mask =
9617b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9627b84fd04STaylor Simpson                                HAS_HVX_STORES, 1);
9637b84fd04STaylor Simpson             }
9647b84fd04STaylor Simpson             if (has_store_s0 && slot_is_predicated(pkt, 0)) {
9657b84fd04STaylor Simpson                 mask =
9667b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9677b84fd04STaylor Simpson                                S0_IS_PRED, 1);
9687b84fd04STaylor Simpson             }
9697b84fd04STaylor Simpson             if (has_store_s1 && slot_is_predicated(pkt, 1)) {
9707b84fd04STaylor Simpson                 mask =
9717b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
9727b84fd04STaylor Simpson                                S1_IS_PRED, 1);
973a82dd548STaylor Simpson             }
9742bda44e8STaylor Simpson             mask = FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, MMU_IDX,
9752bda44e8STaylor Simpson                               ctx->mem_idx);
9762bda44e8STaylor Simpson             gen_helper_probe_pkt_scalar_hvx_stores(cpu_env,
9772bda44e8STaylor Simpson                                                    tcg_constant_tl(mask));
978a82dd548STaylor Simpson         }
979c23b5764STaylor Simpson     } else if (has_store_s0 && has_store_s1) {
980c23b5764STaylor Simpson         /*
981c23b5764STaylor Simpson          * process_store_log will execute the slot 1 store first,
982c23b5764STaylor Simpson          * so we only have to probe the store in slot 0
983c23b5764STaylor Simpson          */
9847b84fd04STaylor Simpson         int args = 0;
9857b84fd04STaylor Simpson         args =
9867b84fd04STaylor Simpson             FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, MMU_IDX, ctx->mem_idx);
9877b84fd04STaylor Simpson         if (slot_is_predicated(pkt, 0)) {
9887b84fd04STaylor Simpson             args =
9897b84fd04STaylor Simpson                 FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, IS_PREDICATED, 1);
9907b84fd04STaylor Simpson         }
9917b84fd04STaylor Simpson         TCGv args_tcgv = tcg_constant_tl(args);
9927b84fd04STaylor Simpson         gen_helper_probe_pkt_scalar_store_s0(cpu_env, args_tcgv);
993c23b5764STaylor Simpson     }
994c23b5764STaylor Simpson 
9951e536334STaylor Simpson     process_store_log(ctx);
996c23b5764STaylor Simpson 
9978b453a2bSTaylor Simpson     gen_reg_writes(ctx);
9981e536334STaylor Simpson     gen_pred_writes(ctx);
999a82dd548STaylor Simpson     if (pkt->pkt_has_hvx) {
10001e536334STaylor Simpson         gen_commit_hvx(ctx);
1001a82dd548STaylor Simpson     }
10021e536334STaylor Simpson     update_exec_counters(ctx);
100385580a65STaylor Simpson     if (HEX_DEBUG) {
10048b453a2bSTaylor Simpson         TCGv has_st0 =
100523803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s0 && !pkt->pkt_has_dczeroa);
10068b453a2bSTaylor Simpson         TCGv has_st1 =
100723803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa);
10088b453a2bSTaylor Simpson 
10098b453a2bSTaylor Simpson         /* Handy place to set a breakpoint at the end of execution */
1010*842b206fSTaylor Simpson         gen_helper_debug_commit_end(cpu_env, ctx->pred_written,
1011*842b206fSTaylor Simpson                                     has_st0, has_st1);
10128b453a2bSTaylor Simpson     }
10138b453a2bSTaylor Simpson 
1014a82dd548STaylor Simpson     if (pkt->vhist_insn != NULL) {
1015a82dd548STaylor Simpson         ctx->pre_commit = false;
10161e536334STaylor Simpson         ctx->insn = pkt->vhist_insn;
10171e536334STaylor Simpson         pkt->vhist_insn->generate(ctx);
1018a82dd548STaylor Simpson     }
1019a82dd548STaylor Simpson 
10208b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
1021743debbcSTaylor Simpson         gen_end_tb(ctx);
10228b453a2bSTaylor Simpson     }
10238b453a2bSTaylor Simpson }
10248b453a2bSTaylor Simpson 
10258b453a2bSTaylor Simpson static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
10268b453a2bSTaylor Simpson {
10278b453a2bSTaylor Simpson     uint32_t words[PACKET_WORDS_MAX];
10288b453a2bSTaylor Simpson     int nwords;
10298b453a2bSTaylor Simpson     Packet pkt;
10308b453a2bSTaylor Simpson     int i;
10318b453a2bSTaylor Simpson 
10328b453a2bSTaylor Simpson     nwords = read_packet_words(env, ctx, words);
10338b453a2bSTaylor Simpson     if (!nwords) {
1034743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
10358b453a2bSTaylor Simpson         return;
10368b453a2bSTaylor Simpson     }
10378b453a2bSTaylor Simpson 
10388b453a2bSTaylor Simpson     if (decode_packet(nwords, words, &pkt, false) > 0) {
103940085901STaylor Simpson         pkt.pc = ctx->base.pc_next;
10408b453a2bSTaylor Simpson         HEX_DEBUG_PRINT_PKT(&pkt);
10411e536334STaylor Simpson         ctx->pkt = &pkt;
10421e536334STaylor Simpson         gen_start_packet(ctx);
10438b453a2bSTaylor Simpson         for (i = 0; i < pkt.num_insns; i++) {
10441e536334STaylor Simpson             ctx->insn = &pkt.insn[i];
10451e536334STaylor Simpson             gen_insn(ctx);
10468b453a2bSTaylor Simpson         }
10471e536334STaylor Simpson         gen_commit_packet(ctx);
10488b453a2bSTaylor Simpson         ctx->base.pc_next += pkt.encod_pkt_size_in_bytes;
10498b453a2bSTaylor Simpson     } else {
1050743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
10518b453a2bSTaylor Simpson     }
10528b453a2bSTaylor Simpson }
10538b453a2bSTaylor Simpson 
10548b453a2bSTaylor Simpson static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
10558b453a2bSTaylor Simpson                                           CPUState *cs)
10568b453a2bSTaylor Simpson {
10578b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
1058d54c5615STaylor Simpson     HexagonCPU *hex_cpu = env_archcpu(cs->env_ptr);
1059564b2040STaylor Simpson     uint32_t hex_flags = dcbase->tb->flags;
10608b453a2bSTaylor Simpson 
10618b453a2bSTaylor Simpson     ctx->mem_idx = MMU_USER_IDX;
10628b453a2bSTaylor Simpson     ctx->num_packets = 0;
10638b453a2bSTaylor Simpson     ctx->num_insns = 0;
1064a82dd548STaylor Simpson     ctx->num_hvx_insns = 0;
10651b9a7f2aSTaylor Simpson     ctx->branch_cond = TCG_COND_NEVER;
1066564b2040STaylor Simpson     ctx->is_tight_loop = FIELD_EX32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP);
1067d54c5615STaylor Simpson     ctx->short_circuit = hex_cpu->short_circuit;
10688b453a2bSTaylor Simpson }
10698b453a2bSTaylor Simpson 
10708b453a2bSTaylor Simpson static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
10718b453a2bSTaylor Simpson {
10728b453a2bSTaylor Simpson }
10738b453a2bSTaylor Simpson 
10748b453a2bSTaylor Simpson static void hexagon_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
10758b453a2bSTaylor Simpson {
10768b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
10778b453a2bSTaylor Simpson 
10788b453a2bSTaylor Simpson     tcg_gen_insn_start(ctx->base.pc_next);
10798b453a2bSTaylor Simpson }
10808b453a2bSTaylor Simpson 
10818b453a2bSTaylor Simpson static bool pkt_crosses_page(CPUHexagonState *env, DisasContext *ctx)
10828b453a2bSTaylor Simpson {
10838b453a2bSTaylor Simpson     target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
10848b453a2bSTaylor Simpson     bool found_end = false;
10858b453a2bSTaylor Simpson     int nwords;
10868b453a2bSTaylor Simpson 
10878b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
10888b453a2bSTaylor Simpson         uint32_t word = cpu_ldl_code(env,
10898b453a2bSTaylor Simpson                             ctx->base.pc_next + nwords * sizeof(uint32_t));
10908b453a2bSTaylor Simpson         found_end = is_packet_end(word);
10918b453a2bSTaylor Simpson     }
10928b453a2bSTaylor Simpson     uint32_t next_ptr =  ctx->base.pc_next + nwords * sizeof(uint32_t);
10938b453a2bSTaylor Simpson     return found_end && next_ptr - page_start >= TARGET_PAGE_SIZE;
10948b453a2bSTaylor Simpson }
10958b453a2bSTaylor Simpson 
10968b453a2bSTaylor Simpson static void hexagon_tr_translate_packet(DisasContextBase *dcbase, CPUState *cpu)
10978b453a2bSTaylor Simpson {
10988b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
10998b453a2bSTaylor Simpson     CPUHexagonState *env = cpu->env_ptr;
11008b453a2bSTaylor Simpson 
11018b453a2bSTaylor Simpson     decode_and_translate_packet(env, ctx);
11028b453a2bSTaylor Simpson 
11038b453a2bSTaylor Simpson     if (ctx->base.is_jmp == DISAS_NEXT) {
11048b453a2bSTaylor Simpson         target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
11058b453a2bSTaylor Simpson         target_ulong bytes_max = PACKET_WORDS_MAX * sizeof(target_ulong);
11068b453a2bSTaylor Simpson 
11078b453a2bSTaylor Simpson         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE ||
11088b453a2bSTaylor Simpson             (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE - bytes_max &&
11098b453a2bSTaylor Simpson              pkt_crosses_page(env, ctx))) {
11108b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
11118b453a2bSTaylor Simpson         }
11128b453a2bSTaylor Simpson 
11138b453a2bSTaylor Simpson         /*
11148b453a2bSTaylor Simpson          * The CPU log is used to compare against LLDB single stepping,
11158b453a2bSTaylor Simpson          * so end the TLB after every packet.
11168b453a2bSTaylor Simpson          */
11177d9ab202STaylor Simpson         HexagonCPU *hex_cpu = env_archcpu(env);
11188b453a2bSTaylor Simpson         if (hex_cpu->lldb_compat && qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
11198b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
11208b453a2bSTaylor Simpson         }
11218b453a2bSTaylor Simpson     }
11228b453a2bSTaylor Simpson }
11238b453a2bSTaylor Simpson 
11248b453a2bSTaylor Simpson static void hexagon_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
11258b453a2bSTaylor Simpson {
11268b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
11278b453a2bSTaylor Simpson 
11288b453a2bSTaylor Simpson     switch (ctx->base.is_jmp) {
11298b453a2bSTaylor Simpson     case DISAS_TOO_MANY:
11308b453a2bSTaylor Simpson         gen_exec_counters(ctx);
11318b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next);
11328b453a2bSTaylor Simpson         tcg_gen_exit_tb(NULL, 0);
11338b453a2bSTaylor Simpson         break;
11348b453a2bSTaylor Simpson     case DISAS_NORETURN:
11358b453a2bSTaylor Simpson         break;
11368b453a2bSTaylor Simpson     default:
11378b453a2bSTaylor Simpson         g_assert_not_reached();
11388b453a2bSTaylor Simpson     }
11398b453a2bSTaylor Simpson }
11408b453a2bSTaylor Simpson 
11418eb806a7SRichard Henderson static void hexagon_tr_disas_log(const DisasContextBase *dcbase,
11428eb806a7SRichard Henderson                                  CPUState *cpu, FILE *logfile)
11438b453a2bSTaylor Simpson {
11448eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
11458eb806a7SRichard Henderson     target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
11468b453a2bSTaylor Simpson }
11478b453a2bSTaylor Simpson 
11488b453a2bSTaylor Simpson 
11498b453a2bSTaylor Simpson static const TranslatorOps hexagon_tr_ops = {
11508b453a2bSTaylor Simpson     .init_disas_context = hexagon_tr_init_disas_context,
11518b453a2bSTaylor Simpson     .tb_start           = hexagon_tr_tb_start,
11528b453a2bSTaylor Simpson     .insn_start         = hexagon_tr_insn_start,
11538b453a2bSTaylor Simpson     .translate_insn     = hexagon_tr_translate_packet,
11548b453a2bSTaylor Simpson     .tb_stop            = hexagon_tr_tb_stop,
11558b453a2bSTaylor Simpson     .disas_log          = hexagon_tr_disas_log,
11568b453a2bSTaylor Simpson };
11578b453a2bSTaylor Simpson 
1158597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
1159306c8721SRichard Henderson                            target_ulong pc, void *host_pc)
11608b453a2bSTaylor Simpson {
11618b453a2bSTaylor Simpson     DisasContext ctx;
11628b453a2bSTaylor Simpson 
1163306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc,
1164306c8721SRichard Henderson                     &hexagon_tr_ops, &ctx.base);
11658b453a2bSTaylor Simpson }
11668b453a2bSTaylor Simpson 
11678b453a2bSTaylor Simpson #define NAME_LEN               64
11688b453a2bSTaylor Simpson static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
11698b453a2bSTaylor Simpson static char store_addr_names[STORES_MAX][NAME_LEN];
11708b453a2bSTaylor Simpson static char store_width_names[STORES_MAX][NAME_LEN];
11718b453a2bSTaylor Simpson static char store_val32_names[STORES_MAX][NAME_LEN];
11728b453a2bSTaylor Simpson static char store_val64_names[STORES_MAX][NAME_LEN];
1173a82dd548STaylor Simpson static char vstore_addr_names[VSTORES_MAX][NAME_LEN];
1174a82dd548STaylor Simpson static char vstore_size_names[VSTORES_MAX][NAME_LEN];
1175a82dd548STaylor Simpson static char vstore_pending_names[VSTORES_MAX][NAME_LEN];
11768b453a2bSTaylor Simpson 
11778b453a2bSTaylor Simpson void hexagon_translate_init(void)
11788b453a2bSTaylor Simpson {
11798b453a2bSTaylor Simpson     int i;
11808b453a2bSTaylor Simpson 
11818b453a2bSTaylor Simpson     opcode_init();
11828b453a2bSTaylor Simpson 
11838b453a2bSTaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
11848b453a2bSTaylor Simpson         hex_gpr[i] = tcg_global_mem_new(cpu_env,
11858b453a2bSTaylor Simpson             offsetof(CPUHexagonState, gpr[i]),
11868b453a2bSTaylor Simpson             hexagon_regnames[i]);
11878b453a2bSTaylor Simpson 
118885580a65STaylor Simpson         if (HEX_DEBUG) {
11898b453a2bSTaylor Simpson             snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s",
11908b453a2bSTaylor Simpson                      hexagon_regnames[i]);
11918b453a2bSTaylor Simpson             hex_reg_written[i] = tcg_global_mem_new(cpu_env,
11928b453a2bSTaylor Simpson                 offsetof(CPUHexagonState, reg_written[i]),
11938b453a2bSTaylor Simpson                 reg_written_names[i]);
119485580a65STaylor Simpson         }
11958b453a2bSTaylor Simpson     }
11966aa4f1d1STaylor Simpson     hex_new_value_usr = tcg_global_mem_new(cpu_env,
11976aa4f1d1STaylor Simpson         offsetof(CPUHexagonState, new_value_usr), "new_value_usr");
11986aa4f1d1STaylor Simpson 
11998b453a2bSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
12008b453a2bSTaylor Simpson         hex_pred[i] = tcg_global_mem_new(cpu_env,
12018b453a2bSTaylor Simpson             offsetof(CPUHexagonState, pred[i]),
12028b453a2bSTaylor Simpson             hexagon_prednames[i]);
12038b453a2bSTaylor Simpson     }
12048b453a2bSTaylor Simpson     hex_this_PC = tcg_global_mem_new(cpu_env,
12058b453a2bSTaylor Simpson         offsetof(CPUHexagonState, this_PC), "this_PC");
12068b453a2bSTaylor Simpson     hex_slot_cancelled = tcg_global_mem_new(cpu_env,
12078b453a2bSTaylor Simpson         offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled");
12088b453a2bSTaylor Simpson     hex_branch_taken = tcg_global_mem_new(cpu_env,
12098b453a2bSTaylor Simpson         offsetof(CPUHexagonState, branch_taken), "branch_taken");
12108b453a2bSTaylor Simpson     hex_pkt_has_store_s1 = tcg_global_mem_new(cpu_env,
12118b453a2bSTaylor Simpson         offsetof(CPUHexagonState, pkt_has_store_s1), "pkt_has_store_s1");
12128b453a2bSTaylor Simpson     hex_dczero_addr = tcg_global_mem_new(cpu_env,
12138b453a2bSTaylor Simpson         offsetof(CPUHexagonState, dczero_addr), "dczero_addr");
12148b453a2bSTaylor Simpson     hex_llsc_addr = tcg_global_mem_new(cpu_env,
12158b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_addr), "llsc_addr");
12168b453a2bSTaylor Simpson     hex_llsc_val = tcg_global_mem_new(cpu_env,
12178b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val), "llsc_val");
12188b453a2bSTaylor Simpson     hex_llsc_val_i64 = tcg_global_mem_new_i64(cpu_env,
12198b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
12208b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
12218b453a2bSTaylor Simpson         snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
12228b453a2bSTaylor Simpson         hex_store_addr[i] = tcg_global_mem_new(cpu_env,
12238b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].va),
12248b453a2bSTaylor Simpson             store_addr_names[i]);
12258b453a2bSTaylor Simpson 
12268b453a2bSTaylor Simpson         snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i);
12278b453a2bSTaylor Simpson         hex_store_width[i] = tcg_global_mem_new(cpu_env,
12288b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].width),
12298b453a2bSTaylor Simpson             store_width_names[i]);
12308b453a2bSTaylor Simpson 
12318b453a2bSTaylor Simpson         snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i);
12328b453a2bSTaylor Simpson         hex_store_val32[i] = tcg_global_mem_new(cpu_env,
12338b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data32),
12348b453a2bSTaylor Simpson             store_val32_names[i]);
12358b453a2bSTaylor Simpson 
12368b453a2bSTaylor Simpson         snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i);
12378b453a2bSTaylor Simpson         hex_store_val64[i] = tcg_global_mem_new_i64(cpu_env,
12388b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data64),
12398b453a2bSTaylor Simpson             store_val64_names[i]);
12408b453a2bSTaylor Simpson     }
1241a82dd548STaylor Simpson     for (int i = 0; i < VSTORES_MAX; i++) {
1242a82dd548STaylor Simpson         snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i);
1243a82dd548STaylor Simpson         hex_vstore_addr[i] = tcg_global_mem_new(cpu_env,
1244a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].va),
1245a82dd548STaylor Simpson             vstore_addr_names[i]);
1246a82dd548STaylor Simpson 
1247a82dd548STaylor Simpson         snprintf(vstore_size_names[i], NAME_LEN, "vstore_size_%d", i);
1248a82dd548STaylor Simpson         hex_vstore_size[i] = tcg_global_mem_new(cpu_env,
1249a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].size),
1250a82dd548STaylor Simpson             vstore_size_names[i]);
1251a82dd548STaylor Simpson 
1252a82dd548STaylor Simpson         snprintf(vstore_pending_names[i], NAME_LEN, "vstore_pending_%d", i);
1253a82dd548STaylor Simpson         hex_vstore_pending[i] = tcg_global_mem_new(cpu_env,
1254a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore_pending[i]),
1255a82dd548STaylor Simpson             vstore_pending_names[i]);
1256a82dd548STaylor Simpson     }
12578b453a2bSTaylor Simpson }
1258