xref: /openbmc/qemu/target/hexagon/translate.c (revision c2b33d0b)
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"
308b453a2bSTaylor Simpson #include "printinsn.h"
318b453a2bSTaylor Simpson 
3210849c26STaylor Simpson #include "analyze_funcs_generated.c.inc"
3310849c26STaylor Simpson 
3410849c26STaylor Simpson typedef void (*AnalyzeInsn)(DisasContext *ctx);
3510849c26STaylor Simpson static const AnalyzeInsn opcode_analyze[XX_LAST_OPCODE] = {
3610849c26STaylor Simpson #define OPCODE(X)    [X] = analyze_##X
3710849c26STaylor Simpson #include "opcodes_def_generated.h.inc"
3810849c26STaylor Simpson #undef OPCODE
3910849c26STaylor Simpson };
4010849c26STaylor Simpson 
418b453a2bSTaylor Simpson TCGv hex_gpr[TOTAL_PER_THREAD_REGS];
428b453a2bSTaylor Simpson TCGv hex_pred[NUM_PREGS];
438b453a2bSTaylor Simpson TCGv hex_this_PC;
448b453a2bSTaylor Simpson TCGv hex_slot_cancelled;
458b453a2bSTaylor Simpson TCGv hex_branch_taken;
468b453a2bSTaylor Simpson TCGv hex_new_value[TOTAL_PER_THREAD_REGS];
478b453a2bSTaylor Simpson TCGv hex_reg_written[TOTAL_PER_THREAD_REGS];
488b453a2bSTaylor Simpson TCGv hex_new_pred_value[NUM_PREGS];
498b453a2bSTaylor Simpson TCGv hex_pred_written;
508b453a2bSTaylor Simpson TCGv hex_store_addr[STORES_MAX];
518b453a2bSTaylor Simpson TCGv hex_store_width[STORES_MAX];
528b453a2bSTaylor Simpson TCGv hex_store_val32[STORES_MAX];
538b453a2bSTaylor Simpson TCGv_i64 hex_store_val64[STORES_MAX];
548b453a2bSTaylor Simpson TCGv hex_pkt_has_store_s1;
558b453a2bSTaylor Simpson TCGv hex_dczero_addr;
568b453a2bSTaylor Simpson TCGv hex_llsc_addr;
578b453a2bSTaylor Simpson TCGv hex_llsc_val;
588b453a2bSTaylor Simpson TCGv_i64 hex_llsc_val_i64;
59a82dd548STaylor Simpson TCGv hex_vstore_addr[VSTORES_MAX];
60a82dd548STaylor Simpson TCGv hex_vstore_size[VSTORES_MAX];
61a82dd548STaylor Simpson TCGv hex_vstore_pending[VSTORES_MAX];
628b453a2bSTaylor Simpson 
638b453a2bSTaylor Simpson static const char * const hexagon_prednames[] = {
648b453a2bSTaylor Simpson   "p0", "p1", "p2", "p3"
658b453a2bSTaylor Simpson };
668b453a2bSTaylor Simpson 
67a82dd548STaylor Simpson intptr_t ctx_future_vreg_off(DisasContext *ctx, int regnum,
68a82dd548STaylor Simpson                           int num, bool alloc_ok)
69a82dd548STaylor Simpson {
70a82dd548STaylor Simpson     intptr_t offset;
71a82dd548STaylor Simpson 
72a82dd548STaylor Simpson     /* See if it is already allocated */
73a82dd548STaylor Simpson     for (int i = 0; i < ctx->future_vregs_idx; i++) {
74a82dd548STaylor Simpson         if (ctx->future_vregs_num[i] == regnum) {
75a82dd548STaylor Simpson             return offsetof(CPUHexagonState, future_VRegs[i]);
76a82dd548STaylor Simpson         }
77a82dd548STaylor Simpson     }
78a82dd548STaylor Simpson 
79a82dd548STaylor Simpson     g_assert(alloc_ok);
80a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, future_VRegs[ctx->future_vregs_idx]);
81a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
82a82dd548STaylor Simpson         ctx->future_vregs_num[ctx->future_vregs_idx + i] = regnum++;
83a82dd548STaylor Simpson     }
84a82dd548STaylor Simpson     ctx->future_vregs_idx += num;
85a82dd548STaylor Simpson     g_assert(ctx->future_vregs_idx <= VECTOR_TEMPS_MAX);
86a82dd548STaylor Simpson     return offset;
87a82dd548STaylor Simpson }
88a82dd548STaylor Simpson 
89a82dd548STaylor Simpson intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,
90a82dd548STaylor Simpson                           int num, bool alloc_ok)
91a82dd548STaylor Simpson {
92a82dd548STaylor Simpson     intptr_t offset;
93a82dd548STaylor Simpson 
94a82dd548STaylor Simpson     /* See if it is already allocated */
95a82dd548STaylor Simpson     for (int i = 0; i < ctx->tmp_vregs_idx; i++) {
96a82dd548STaylor Simpson         if (ctx->tmp_vregs_num[i] == regnum) {
97a82dd548STaylor Simpson             return offsetof(CPUHexagonState, tmp_VRegs[i]);
98a82dd548STaylor Simpson         }
99a82dd548STaylor Simpson     }
100a82dd548STaylor Simpson 
101a82dd548STaylor Simpson     g_assert(alloc_ok);
102a82dd548STaylor Simpson     offset = offsetof(CPUHexagonState, tmp_VRegs[ctx->tmp_vregs_idx]);
103a82dd548STaylor Simpson     for (int i = 0; i < num; i++) {
104a82dd548STaylor Simpson         ctx->tmp_vregs_num[ctx->tmp_vregs_idx + i] = regnum++;
105a82dd548STaylor Simpson     }
106a82dd548STaylor Simpson     ctx->tmp_vregs_idx += num;
107a82dd548STaylor Simpson     g_assert(ctx->tmp_vregs_idx <= VECTOR_TEMPS_MAX);
108a82dd548STaylor Simpson     return offset;
109a82dd548STaylor Simpson }
110a82dd548STaylor Simpson 
111743debbcSTaylor Simpson static void gen_exception_raw(int excp)
1128b453a2bSTaylor Simpson {
11323803bbeSPhilippe Mathieu-Daudé     gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
1148b453a2bSTaylor Simpson }
1158b453a2bSTaylor Simpson 
116743debbcSTaylor Simpson static void gen_exec_counters(DisasContext *ctx)
1178b453a2bSTaylor Simpson {
118743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT],
119743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_PKT_CNT], ctx->num_packets);
120743debbcSTaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_INSN_CNT],
121743debbcSTaylor Simpson                     hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns);
122a82dd548STaylor Simpson     tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT],
123a82dd548STaylor Simpson                     hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns);
124743debbcSTaylor Simpson }
125743debbcSTaylor Simpson 
1261b9a7f2aSTaylor Simpson static bool use_goto_tb(DisasContext *ctx, target_ulong dest)
1271b9a7f2aSTaylor Simpson {
1281b9a7f2aSTaylor Simpson     return translator_use_goto_tb(&ctx->base, dest);
1291b9a7f2aSTaylor Simpson }
1301b9a7f2aSTaylor Simpson 
1311b9a7f2aSTaylor Simpson static void gen_goto_tb(DisasContext *ctx, int idx, target_ulong dest)
1321b9a7f2aSTaylor Simpson {
1331b9a7f2aSTaylor Simpson     if (use_goto_tb(ctx, dest)) {
1341b9a7f2aSTaylor Simpson         tcg_gen_goto_tb(idx);
1351b9a7f2aSTaylor Simpson         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
1361b9a7f2aSTaylor Simpson         tcg_gen_exit_tb(ctx->base.tb, idx);
1371b9a7f2aSTaylor Simpson     } else {
1381b9a7f2aSTaylor Simpson         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], dest);
1391b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1401b9a7f2aSTaylor Simpson     }
1411b9a7f2aSTaylor Simpson }
1421b9a7f2aSTaylor Simpson 
143743debbcSTaylor Simpson static void gen_end_tb(DisasContext *ctx)
144743debbcSTaylor Simpson {
145564b2040STaylor Simpson     Packet *pkt = ctx->pkt;
146564b2040STaylor Simpson 
147743debbcSTaylor Simpson     gen_exec_counters(ctx);
1481b9a7f2aSTaylor Simpson 
1491b9a7f2aSTaylor Simpson     if (ctx->branch_cond != TCG_COND_NEVER) {
1501b9a7f2aSTaylor Simpson         if (ctx->branch_cond != TCG_COND_ALWAYS) {
1511b9a7f2aSTaylor Simpson             TCGLabel *skip = gen_new_label();
1521b9a7f2aSTaylor Simpson             tcg_gen_brcondi_tl(ctx->branch_cond, hex_branch_taken, 0, skip);
1531b9a7f2aSTaylor Simpson             gen_goto_tb(ctx, 0, ctx->branch_dest);
1541b9a7f2aSTaylor Simpson             gen_set_label(skip);
1551b9a7f2aSTaylor Simpson             gen_goto_tb(ctx, 1, ctx->next_PC);
1561b9a7f2aSTaylor Simpson         } else {
1571b9a7f2aSTaylor Simpson             gen_goto_tb(ctx, 0, ctx->branch_dest);
1581b9a7f2aSTaylor Simpson         }
159564b2040STaylor Simpson     } else if (ctx->is_tight_loop &&
160564b2040STaylor Simpson                pkt->insn[pkt->num_insns - 1].opcode == J2_endloop0) {
161564b2040STaylor Simpson         /*
162564b2040STaylor Simpson          * When we're in a tight loop, we defer the endloop0 processing
163564b2040STaylor Simpson          * to take advantage of direct block chaining
164564b2040STaylor Simpson          */
165564b2040STaylor Simpson         TCGLabel *skip = gen_new_label();
166564b2040STaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_LEU, hex_gpr[HEX_REG_LC0], 1, skip);
167564b2040STaylor Simpson         tcg_gen_subi_tl(hex_gpr[HEX_REG_LC0], hex_gpr[HEX_REG_LC0], 1);
168564b2040STaylor Simpson         gen_goto_tb(ctx, 0, ctx->base.tb->pc);
169564b2040STaylor Simpson         gen_set_label(skip);
170564b2040STaylor Simpson         gen_goto_tb(ctx, 1, ctx->next_PC);
1711b9a7f2aSTaylor Simpson     } else {
1721b9a7f2aSTaylor Simpson         tcg_gen_lookup_and_goto_ptr();
1731b9a7f2aSTaylor Simpson     }
1741b9a7f2aSTaylor Simpson 
175743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
176743debbcSTaylor Simpson }
177743debbcSTaylor Simpson 
178743debbcSTaylor Simpson static void gen_exception_end_tb(DisasContext *ctx, int excp)
179743debbcSTaylor Simpson {
180743debbcSTaylor Simpson     gen_exec_counters(ctx);
181613653e5STaylor Simpson     tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC);
182743debbcSTaylor Simpson     gen_exception_raw(excp);
183743debbcSTaylor Simpson     ctx->base.is_jmp = DISAS_NORETURN;
184743debbcSTaylor Simpson 
1858b453a2bSTaylor Simpson }
1868b453a2bSTaylor Simpson 
1878b453a2bSTaylor Simpson #define PACKET_BUFFER_LEN              1028
1888b453a2bSTaylor Simpson static void print_pkt(Packet *pkt)
1898b453a2bSTaylor Simpson {
1908b453a2bSTaylor Simpson     GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
1918b453a2bSTaylor Simpson     snprint_a_pkt_debug(buf, pkt);
1928b453a2bSTaylor Simpson     HEX_DEBUG_LOG("%s", buf->str);
1938b453a2bSTaylor Simpson     g_string_free(buf, true);
1948b453a2bSTaylor Simpson }
19585580a65STaylor Simpson #define HEX_DEBUG_PRINT_PKT(pkt) \
19685580a65STaylor Simpson     do { \
19785580a65STaylor Simpson         if (HEX_DEBUG) { \
19885580a65STaylor Simpson             print_pkt(pkt); \
19985580a65STaylor Simpson         } \
20085580a65STaylor Simpson     } while (0)
2018b453a2bSTaylor Simpson 
2028b453a2bSTaylor Simpson static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
2038b453a2bSTaylor Simpson                              uint32_t words[])
2048b453a2bSTaylor Simpson {
2058b453a2bSTaylor Simpson     bool found_end = false;
2068b453a2bSTaylor Simpson     int nwords, max_words;
2078b453a2bSTaylor Simpson 
2088b453a2bSTaylor Simpson     memset(words, 0, PACKET_WORDS_MAX * sizeof(uint32_t));
2098b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
210a27c100cSTaylor Simpson         words[nwords] =
2114e116893SIlya Leoshkevich             translator_ldl(env, &ctx->base,
2124e116893SIlya Leoshkevich                            ctx->base.pc_next + nwords * sizeof(uint32_t));
2138b453a2bSTaylor Simpson         found_end = is_packet_end(words[nwords]);
2148b453a2bSTaylor Simpson     }
2158b453a2bSTaylor Simpson     if (!found_end) {
2168b453a2bSTaylor Simpson         /* Read too many words without finding the end */
2178b453a2bSTaylor Simpson         return 0;
2188b453a2bSTaylor Simpson     }
2198b453a2bSTaylor Simpson 
2208b453a2bSTaylor Simpson     /* Check for page boundary crossing */
2218b453a2bSTaylor Simpson     max_words = -(ctx->base.pc_next | TARGET_PAGE_MASK) / sizeof(uint32_t);
2228b453a2bSTaylor Simpson     if (nwords > max_words) {
2238b453a2bSTaylor Simpson         /* We can only cross a page boundary at the beginning of a TB */
2248b453a2bSTaylor Simpson         g_assert(ctx->base.num_insns == 1);
2258b453a2bSTaylor Simpson     }
2268b453a2bSTaylor Simpson 
2278b453a2bSTaylor Simpson     HEX_DEBUG_LOG("decode_packet: pc = 0x%x\n", ctx->base.pc_next);
2288b453a2bSTaylor Simpson     HEX_DEBUG_LOG("    words = { ");
2298b453a2bSTaylor Simpson     for (int i = 0; i < nwords; i++) {
2308b453a2bSTaylor Simpson         HEX_DEBUG_LOG("0x%x, ", words[i]);
2318b453a2bSTaylor Simpson     }
2328b453a2bSTaylor Simpson     HEX_DEBUG_LOG("}\n");
2338b453a2bSTaylor Simpson 
2348b453a2bSTaylor Simpson     return nwords;
2358b453a2bSTaylor Simpson }
2368b453a2bSTaylor Simpson 
2378b453a2bSTaylor Simpson static bool check_for_attrib(Packet *pkt, int attrib)
2388b453a2bSTaylor Simpson {
2398b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2408b453a2bSTaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, attrib)) {
2418b453a2bSTaylor Simpson             return true;
2428b453a2bSTaylor Simpson         }
2438b453a2bSTaylor Simpson     }
2448b453a2bSTaylor Simpson     return false;
2458b453a2bSTaylor Simpson }
2468b453a2bSTaylor Simpson 
2478b453a2bSTaylor Simpson static bool need_slot_cancelled(Packet *pkt)
2488b453a2bSTaylor Simpson {
249*c2b33d0bSTaylor Simpson     /* We only need slot_cancelled for conditional store instructions */
2507b84fd04STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
2517b84fd04STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
2527b84fd04STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) &&
253*c2b33d0bSTaylor Simpson             GET_ATTRIB(opcode, A_SCALAR_STORE)) {
2547b84fd04STaylor Simpson             return true;
2557b84fd04STaylor Simpson         }
2567b84fd04STaylor Simpson     }
2577b84fd04STaylor Simpson     return false;
2588b453a2bSTaylor Simpson }
2598b453a2bSTaylor Simpson 
2608b453a2bSTaylor Simpson static bool need_pred_written(Packet *pkt)
2618b453a2bSTaylor Simpson {
2628b453a2bSTaylor Simpson     return check_for_attrib(pkt, A_WRITES_PRED_REG);
2638b453a2bSTaylor Simpson }
2648b453a2bSTaylor Simpson 
265613653e5STaylor Simpson static bool need_next_PC(DisasContext *ctx)
266613653e5STaylor Simpson {
267613653e5STaylor Simpson     Packet *pkt = ctx->pkt;
268613653e5STaylor Simpson 
269613653e5STaylor Simpson     /* Check for conditional control flow or HW loop end */
270613653e5STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
271613653e5STaylor Simpson         uint16_t opcode = pkt->insn[i].opcode;
272613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_CONDEXEC) && GET_ATTRIB(opcode, A_COF)) {
273613653e5STaylor Simpson             return true;
274613653e5STaylor Simpson         }
275613653e5STaylor Simpson         if (GET_ATTRIB(opcode, A_HWLOOP0_END) ||
276613653e5STaylor Simpson             GET_ATTRIB(opcode, A_HWLOOP1_END)) {
277613653e5STaylor Simpson             return true;
278613653e5STaylor Simpson         }
279613653e5STaylor Simpson     }
280613653e5STaylor Simpson     return false;
281613653e5STaylor Simpson }
282613653e5STaylor Simpson 
28310849c26STaylor Simpson /*
28410849c26STaylor Simpson  * The opcode_analyze functions mark most of the writes in a packet
28510849c26STaylor Simpson  * However, there are some implicit writes marked as attributes
28610849c26STaylor Simpson  * of the applicable instructions.
28710849c26STaylor Simpson  */
28810849c26STaylor Simpson static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
28910849c26STaylor Simpson {
29010849c26STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
29110849c26STaylor Simpson     if (GET_ATTRIB(opcode, attrib)) {
29210849c26STaylor Simpson         /*
29310849c26STaylor Simpson          * USR is used to set overflow and FP exceptions,
29410849c26STaylor Simpson          * so treat it as conditional
29510849c26STaylor Simpson          */
29610849c26STaylor Simpson         bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) ||
29710849c26STaylor Simpson                              rnum == HEX_REG_USR;
29810849c26STaylor Simpson 
29910849c26STaylor Simpson         /* LC0/LC1 is conditionally written by endloop instructions */
30010849c26STaylor Simpson         if ((rnum == HEX_REG_LC0 || rnum == HEX_REG_LC1) &&
30110849c26STaylor Simpson             (opcode == J2_endloop0 ||
30210849c26STaylor Simpson              opcode == J2_endloop1 ||
30310849c26STaylor Simpson              opcode == J2_endloop01)) {
30410849c26STaylor Simpson             is_predicated = true;
30510849c26STaylor Simpson         }
30610849c26STaylor Simpson 
30710849c26STaylor Simpson         ctx_log_reg_write(ctx, rnum, is_predicated);
30810849c26STaylor Simpson     }
30910849c26STaylor Simpson }
31010849c26STaylor Simpson 
31110849c26STaylor Simpson static void mark_implicit_reg_writes(DisasContext *ctx)
31210849c26STaylor Simpson {
31310849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP,  HEX_REG_FP);
31410849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SP,  HEX_REG_SP);
31510849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LR,  HEX_REG_LR);
31610849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC0, HEX_REG_LC0);
31710849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0);
31810849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1);
31910849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1);
32010849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR);
32110849c26STaylor Simpson     mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR);
32210849c26STaylor Simpson }
32310849c26STaylor Simpson 
32410849c26STaylor Simpson static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum)
32510849c26STaylor Simpson {
32610849c26STaylor Simpson     if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
32710849c26STaylor Simpson         ctx_log_pred_write(ctx, pnum);
32810849c26STaylor Simpson     }
32910849c26STaylor Simpson }
33010849c26STaylor Simpson 
33110849c26STaylor Simpson static void mark_implicit_pred_writes(DisasContext *ctx)
33210849c26STaylor Simpson {
33310849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P0, 0);
33410849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P1, 1);
33510849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P2, 2);
33610849c26STaylor Simpson     mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P3, 3);
33710849c26STaylor Simpson }
33810849c26STaylor Simpson 
33910849c26STaylor Simpson static void analyze_packet(DisasContext *ctx)
34010849c26STaylor Simpson {
34110849c26STaylor Simpson     Packet *pkt = ctx->pkt;
3424d13bb51STaylor Simpson     ctx->need_pkt_has_store_s1 = false;
34310849c26STaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
34410849c26STaylor Simpson         Insn *insn = &pkt->insn[i];
34510849c26STaylor Simpson         ctx->insn = insn;
34610849c26STaylor Simpson         if (opcode_analyze[insn->opcode]) {
34710849c26STaylor Simpson             opcode_analyze[insn->opcode](ctx);
34810849c26STaylor Simpson         }
34910849c26STaylor Simpson         mark_implicit_reg_writes(ctx);
35010849c26STaylor Simpson         mark_implicit_pred_writes(ctx);
35110849c26STaylor Simpson     }
35210849c26STaylor Simpson }
35310849c26STaylor Simpson 
3541e536334STaylor Simpson static void gen_start_packet(DisasContext *ctx)
3558b453a2bSTaylor Simpson {
3561e536334STaylor Simpson     Packet *pkt = ctx->pkt;
3578b453a2bSTaylor Simpson     target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes;
3588b453a2bSTaylor Simpson     int i;
3598b453a2bSTaylor Simpson 
3608b453a2bSTaylor Simpson     /* Clear out the disassembly context */
361613653e5STaylor Simpson     ctx->next_PC = next_PC;
3628b453a2bSTaylor Simpson     ctx->reg_log_idx = 0;
3638b453a2bSTaylor Simpson     bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS);
36410849c26STaylor Simpson     bitmap_zero(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
3658b453a2bSTaylor Simpson     ctx->preg_log_idx = 0;
3666c677c60STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
367a82dd548STaylor Simpson     ctx->future_vregs_idx = 0;
368a82dd548STaylor Simpson     ctx->tmp_vregs_idx = 0;
369a82dd548STaylor Simpson     ctx->vreg_log_idx = 0;
370a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated_tmp, NUM_VREGS);
371a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_updated, NUM_VREGS);
372a82dd548STaylor Simpson     bitmap_zero(ctx->vregs_select, NUM_VREGS);
3734d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_future_vregs, NUM_VREGS);
3744d6f8420STaylor Simpson     bitmap_zero(ctx->predicated_tmp_vregs, NUM_VREGS);
375a82dd548STaylor Simpson     ctx->qreg_log_idx = 0;
3768b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
3778b453a2bSTaylor Simpson         ctx->store_width[i] = 0;
3788b453a2bSTaylor Simpson     }
37992cfa25fSTaylor Simpson     ctx->s1_store_processed = false;
380a82dd548STaylor Simpson     ctx->pre_commit = true;
3818b453a2bSTaylor Simpson 
38210849c26STaylor Simpson     analyze_packet(ctx);
38310849c26STaylor Simpson 
3844d13bb51STaylor Simpson     if (ctx->need_pkt_has_store_s1) {
3854d13bb51STaylor Simpson         tcg_gen_movi_tl(hex_pkt_has_store_s1, pkt->pkt_has_store_s1);
3864d13bb51STaylor Simpson     }
3874d13bb51STaylor Simpson 
38810849c26STaylor Simpson     /*
38910849c26STaylor Simpson      * pregs_written is used both in the analyze phase as well as the code
39010849c26STaylor Simpson      * gen phase, so clear it again.
39110849c26STaylor Simpson      */
39210849c26STaylor Simpson     bitmap_zero(ctx->pregs_written, NUM_PREGS);
39310849c26STaylor Simpson 
39485580a65STaylor Simpson     if (HEX_DEBUG) {
3958b453a2bSTaylor Simpson         /* Handy place to set a breakpoint before the packet executes */
3968b453a2bSTaylor Simpson         gen_helper_debug_start_packet(cpu_env);
3978b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_this_PC, ctx->base.pc_next);
39885580a65STaylor Simpson     }
3998b453a2bSTaylor Simpson 
4008b453a2bSTaylor Simpson     /* Initialize the runtime state for packet semantics */
4018b453a2bSTaylor Simpson     if (need_slot_cancelled(pkt)) {
4028b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_slot_cancelled, 0);
4038b453a2bSTaylor Simpson     }
4048b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
405fb67c2bfSTaylor Simpson         if (pkt->pkt_has_multi_cof) {
4068b453a2bSTaylor Simpson             tcg_gen_movi_tl(hex_branch_taken, 0);
407fb67c2bfSTaylor Simpson         }
408613653e5STaylor Simpson         if (need_next_PC(ctx)) {
409613653e5STaylor Simpson             tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], next_PC);
410613653e5STaylor Simpson         }
4118b453a2bSTaylor Simpson     }
4128b453a2bSTaylor Simpson     if (need_pred_written(pkt)) {
4138b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_pred_written, 0);
4148b453a2bSTaylor Simpson     }
415a82dd548STaylor Simpson 
41610849c26STaylor Simpson     /* Preload the predicated registers into hex_new_value[i] */
41710849c26STaylor Simpson     if (!bitmap_empty(ctx->predicated_regs, TOTAL_PER_THREAD_REGS)) {
41810849c26STaylor Simpson         int i = find_first_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS);
41910849c26STaylor Simpson         while (i < TOTAL_PER_THREAD_REGS) {
42010849c26STaylor Simpson             tcg_gen_mov_tl(hex_new_value[i], hex_gpr[i]);
42110849c26STaylor Simpson             i = find_next_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS,
42210849c26STaylor Simpson                               i + 1);
42310849c26STaylor Simpson         }
42410849c26STaylor Simpson     }
42510849c26STaylor Simpson 
4264d6f8420STaylor Simpson     /* Preload the predicated HVX registers into future_VRegs and tmp_VRegs */
4274d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_future_vregs, NUM_VREGS)) {
4284d6f8420STaylor Simpson         int i = find_first_bit(ctx->predicated_future_vregs, NUM_VREGS);
4294d6f8420STaylor Simpson         while (i < NUM_VREGS) {
4304d6f8420STaylor Simpson             const intptr_t VdV_off =
4314d6f8420STaylor Simpson                 ctx_future_vreg_off(ctx, i, 1, true);
4324d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
4334d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
4344d6f8420STaylor Simpson                              src_off,
4354d6f8420STaylor Simpson                              sizeof(MMVector),
4364d6f8420STaylor Simpson                              sizeof(MMVector));
4374d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_future_vregs, NUM_VREGS, i + 1);
4384d6f8420STaylor Simpson         }
4394d6f8420STaylor Simpson     }
4404d6f8420STaylor Simpson     if (!bitmap_empty(ctx->predicated_tmp_vregs, NUM_VREGS)) {
4414d6f8420STaylor Simpson         int i = find_first_bit(ctx->predicated_tmp_vregs, NUM_VREGS);
4424d6f8420STaylor Simpson         while (i < NUM_VREGS) {
4434d6f8420STaylor Simpson             const intptr_t VdV_off =
4444d6f8420STaylor Simpson                 ctx_tmp_vreg_off(ctx, i, 1, true);
4454d6f8420STaylor Simpson             intptr_t src_off = offsetof(CPUHexagonState, VRegs[i]);
4464d6f8420STaylor Simpson             tcg_gen_gvec_mov(MO_64, VdV_off,
4474d6f8420STaylor Simpson                              src_off,
4484d6f8420STaylor Simpson                              sizeof(MMVector),
4494d6f8420STaylor Simpson                              sizeof(MMVector));
4504d6f8420STaylor Simpson             i = find_next_bit(ctx->predicated_tmp_vregs, NUM_VREGS, i + 1);
4514d6f8420STaylor Simpson         }
4524d6f8420STaylor Simpson     }
453a82dd548STaylor Simpson }
454a82dd548STaylor Simpson 
4551e536334STaylor Simpson bool is_gather_store_insn(DisasContext *ctx)
456a82dd548STaylor Simpson {
4571e536334STaylor Simpson     Packet *pkt = ctx->pkt;
4581e536334STaylor Simpson     Insn *insn = ctx->insn;
459a82dd548STaylor Simpson     if (GET_ATTRIB(insn->opcode, A_CVI_NEW) &&
460a82dd548STaylor Simpson         insn->new_value_producer_slot == 1) {
461a82dd548STaylor Simpson         /* Look for gather instruction */
462a82dd548STaylor Simpson         for (int i = 0; i < pkt->num_insns; i++) {
463a82dd548STaylor Simpson             Insn *in = &pkt->insn[i];
464a82dd548STaylor Simpson             if (GET_ATTRIB(in->opcode, A_CVI_GATHER) && in->slot == 1) {
465a82dd548STaylor Simpson                 return true;
466a82dd548STaylor Simpson             }
467a82dd548STaylor Simpson         }
468a82dd548STaylor Simpson     }
469a82dd548STaylor Simpson     return false;
4708b453a2bSTaylor Simpson }
4718b453a2bSTaylor Simpson 
4721e536334STaylor Simpson static void mark_store_width(DisasContext *ctx)
473661ad999STaylor Simpson {
4741e536334STaylor Simpson     uint16_t opcode = ctx->insn->opcode;
4751e536334STaylor Simpson     uint32_t slot = ctx->insn->slot;
476661ad999STaylor Simpson     uint8_t width = 0;
477661ad999STaylor Simpson 
478661ad999STaylor Simpson     if (GET_ATTRIB(opcode, A_SCALAR_STORE)) {
479661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_1B)) {
480661ad999STaylor Simpson             width |= 1;
481661ad999STaylor Simpson         }
482661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_2B)) {
483661ad999STaylor Simpson             width |= 2;
484661ad999STaylor Simpson         }
485661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_4B)) {
486661ad999STaylor Simpson             width |= 4;
487661ad999STaylor Simpson         }
488661ad999STaylor Simpson         if (GET_ATTRIB(opcode, A_MEMSIZE_8B)) {
489661ad999STaylor Simpson             width |= 8;
490661ad999STaylor Simpson         }
491661ad999STaylor Simpson         tcg_debug_assert(is_power_of_2(width));
492661ad999STaylor Simpson         ctx->store_width[slot] = width;
493661ad999STaylor Simpson     }
494661ad999STaylor Simpson }
495661ad999STaylor Simpson 
4961e536334STaylor Simpson static void gen_insn(DisasContext *ctx)
4978b453a2bSTaylor Simpson {
4981e536334STaylor Simpson     if (ctx->insn->generate) {
4991e536334STaylor Simpson         ctx->insn->generate(ctx);
5001e536334STaylor Simpson         mark_store_width(ctx);
5018b453a2bSTaylor Simpson     } else {
502743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_OPCODE);
5038b453a2bSTaylor Simpson     }
5048b453a2bSTaylor Simpson }
5058b453a2bSTaylor Simpson 
5068b453a2bSTaylor Simpson /*
5078b453a2bSTaylor Simpson  * Helpers for generating the packet commit
5088b453a2bSTaylor Simpson  */
5098b453a2bSTaylor Simpson static void gen_reg_writes(DisasContext *ctx)
5108b453a2bSTaylor Simpson {
5118b453a2bSTaylor Simpson     int i;
5128b453a2bSTaylor Simpson 
5138b453a2bSTaylor Simpson     for (i = 0; i < ctx->reg_log_idx; i++) {
5148b453a2bSTaylor Simpson         int reg_num = ctx->reg_log[i];
5158b453a2bSTaylor Simpson 
5168b453a2bSTaylor Simpson         tcg_gen_mov_tl(hex_gpr[reg_num], hex_new_value[reg_num]);
517564b2040STaylor Simpson 
518564b2040STaylor Simpson         /*
519564b2040STaylor Simpson          * ctx->is_tight_loop is set when SA0 points to the beginning of the TB.
520564b2040STaylor Simpson          * If we write to SA0, we have to turn off tight loop handling.
521564b2040STaylor Simpson          */
522564b2040STaylor Simpson         if (reg_num == HEX_REG_SA0) {
523564b2040STaylor Simpson             ctx->is_tight_loop = false;
524564b2040STaylor Simpson         }
5258b453a2bSTaylor Simpson     }
5268b453a2bSTaylor Simpson }
5278b453a2bSTaylor Simpson 
5281e536334STaylor Simpson static void gen_pred_writes(DisasContext *ctx)
5298b453a2bSTaylor Simpson {
5308b453a2bSTaylor Simpson     int i;
5318b453a2bSTaylor Simpson 
5328b453a2bSTaylor Simpson     /* Early exit if the log is empty */
5338b453a2bSTaylor Simpson     if (!ctx->preg_log_idx) {
5348b453a2bSTaylor Simpson         return;
5358b453a2bSTaylor Simpson     }
5368b453a2bSTaylor Simpson 
5378b453a2bSTaylor Simpson     /*
5388b453a2bSTaylor Simpson      * Only endloop instructions will conditionally
5398b453a2bSTaylor Simpson      * write a predicate.  If there are no endloop
5408b453a2bSTaylor Simpson      * instructions, we can use the non-conditional
5418b453a2bSTaylor Simpson      * write of the predicates.
5428b453a2bSTaylor Simpson      */
5431e536334STaylor Simpson     if (ctx->pkt->pkt_has_endloop) {
54423803bbeSPhilippe Mathieu-Daudé         TCGv zero = tcg_constant_tl(0);
5458b453a2bSTaylor Simpson         TCGv pred_written = tcg_temp_new();
5468b453a2bSTaylor Simpson         for (i = 0; i < ctx->preg_log_idx; i++) {
5478b453a2bSTaylor Simpson             int pred_num = ctx->preg_log[i];
5488b453a2bSTaylor Simpson 
5498b453a2bSTaylor Simpson             tcg_gen_andi_tl(pred_written, hex_pred_written, 1 << pred_num);
5508b453a2bSTaylor Simpson             tcg_gen_movcond_tl(TCG_COND_NE, hex_pred[pred_num],
5518b453a2bSTaylor Simpson                                pred_written, zero,
5528b453a2bSTaylor Simpson                                hex_new_pred_value[pred_num],
5538b453a2bSTaylor Simpson                                hex_pred[pred_num]);
5548b453a2bSTaylor Simpson         }
5558b453a2bSTaylor Simpson     } else {
5568b453a2bSTaylor Simpson         for (i = 0; i < ctx->preg_log_idx; i++) {
5578b453a2bSTaylor Simpson             int pred_num = ctx->preg_log[i];
5588b453a2bSTaylor Simpson             tcg_gen_mov_tl(hex_pred[pred_num], hex_new_pred_value[pred_num]);
55985580a65STaylor Simpson             if (HEX_DEBUG) {
5608b453a2bSTaylor Simpson                 /* Do this so HELPER(debug_commit_end) will know */
56185580a65STaylor Simpson                 tcg_gen_ori_tl(hex_pred_written, hex_pred_written,
56285580a65STaylor Simpson                                1 << pred_num);
56385580a65STaylor Simpson             }
5648b453a2bSTaylor Simpson         }
5658b453a2bSTaylor Simpson     }
5668b453a2bSTaylor Simpson }
5678b453a2bSTaylor Simpson 
568a27c100cSTaylor Simpson static void gen_check_store_width(DisasContext *ctx, int slot_num)
5698b453a2bSTaylor Simpson {
57085580a65STaylor Simpson     if (HEX_DEBUG) {
57123803bbeSPhilippe Mathieu-Daudé         TCGv slot = tcg_constant_tl(slot_num);
57223803bbeSPhilippe Mathieu-Daudé         TCGv check = tcg_constant_tl(ctx->store_width[slot_num]);
5738b453a2bSTaylor Simpson         gen_helper_debug_check_store_width(cpu_env, slot, check);
57485580a65STaylor Simpson     }
575a27c100cSTaylor Simpson }
5768b453a2bSTaylor Simpson 
5778b453a2bSTaylor Simpson static bool slot_is_predicated(Packet *pkt, int slot_num)
5788b453a2bSTaylor Simpson {
5798b453a2bSTaylor Simpson     for (int i = 0; i < pkt->num_insns; i++) {
5808b453a2bSTaylor Simpson         if (pkt->insn[i].slot == slot_num) {
5818b453a2bSTaylor Simpson             return GET_ATTRIB(pkt->insn[i].opcode, A_CONDEXEC);
5828b453a2bSTaylor Simpson         }
5838b453a2bSTaylor Simpson     }
5848b453a2bSTaylor Simpson     /* If we get to here, we didn't find an instruction in the requested slot */
5858b453a2bSTaylor Simpson     g_assert_not_reached();
5868b453a2bSTaylor Simpson }
5878b453a2bSTaylor Simpson 
5881e536334STaylor Simpson void process_store(DisasContext *ctx, int slot_num)
5898b453a2bSTaylor Simpson {
5901e536334STaylor Simpson     bool is_predicated = slot_is_predicated(ctx->pkt, slot_num);
5918b453a2bSTaylor Simpson     TCGLabel *label_end = NULL;
5928b453a2bSTaylor Simpson 
5938b453a2bSTaylor Simpson     /*
5948b453a2bSTaylor Simpson      * We may have already processed this store
5958b453a2bSTaylor Simpson      * See CHECK_NOSHUF in macros.h
5968b453a2bSTaylor Simpson      */
5978b453a2bSTaylor Simpson     if (slot_num == 1 && ctx->s1_store_processed) {
5988b453a2bSTaylor Simpson         return;
5998b453a2bSTaylor Simpson     }
60092cfa25fSTaylor Simpson     ctx->s1_store_processed = true;
6018b453a2bSTaylor Simpson 
6028b453a2bSTaylor Simpson     if (is_predicated) {
6038b453a2bSTaylor Simpson         TCGv cancelled = tcg_temp_new();
6048b453a2bSTaylor Simpson         label_end = gen_new_label();
6058b453a2bSTaylor Simpson 
6068b453a2bSTaylor Simpson         /* Don't do anything if the slot was cancelled */
6078b453a2bSTaylor Simpson         tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
6088b453a2bSTaylor Simpson         tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
6098b453a2bSTaylor Simpson     }
6108b453a2bSTaylor Simpson     {
6117a819de8SRichard Henderson         TCGv address = tcg_temp_new();
6128b453a2bSTaylor Simpson         tcg_gen_mov_tl(address, hex_store_addr[slot_num]);
6138b453a2bSTaylor Simpson 
6148b453a2bSTaylor Simpson         /*
6158b453a2bSTaylor Simpson          * If we know the width from the DisasContext, we can
6168b453a2bSTaylor Simpson          * generate much cleaner code.
6178b453a2bSTaylor Simpson          * Unfortunately, not all instructions execute the fSTORE
6188b453a2bSTaylor Simpson          * macro during code generation.  Anything that uses the
6198b453a2bSTaylor Simpson          * generic helper will have this problem.  Instructions
6208b453a2bSTaylor Simpson          * that use fWRAP to generate proper TCG code will be OK.
6218b453a2bSTaylor Simpson          */
6228b453a2bSTaylor Simpson         switch (ctx->store_width[slot_num]) {
6238b453a2bSTaylor Simpson         case 1:
624a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
6258b453a2bSTaylor Simpson             tcg_gen_qemu_st8(hex_store_val32[slot_num],
6268b453a2bSTaylor Simpson                              hex_store_addr[slot_num],
6278b453a2bSTaylor Simpson                              ctx->mem_idx);
6288b453a2bSTaylor Simpson             break;
6298b453a2bSTaylor Simpson         case 2:
630a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
6318b453a2bSTaylor Simpson             tcg_gen_qemu_st16(hex_store_val32[slot_num],
6328b453a2bSTaylor Simpson                               hex_store_addr[slot_num],
6338b453a2bSTaylor Simpson                               ctx->mem_idx);
6348b453a2bSTaylor Simpson             break;
6358b453a2bSTaylor Simpson         case 4:
636a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
6378b453a2bSTaylor Simpson             tcg_gen_qemu_st32(hex_store_val32[slot_num],
6388b453a2bSTaylor Simpson                               hex_store_addr[slot_num],
6398b453a2bSTaylor Simpson                               ctx->mem_idx);
6408b453a2bSTaylor Simpson             break;
6418b453a2bSTaylor Simpson         case 8:
642a27c100cSTaylor Simpson             gen_check_store_width(ctx, slot_num);
6438b453a2bSTaylor Simpson             tcg_gen_qemu_st64(hex_store_val64[slot_num],
6448b453a2bSTaylor Simpson                               hex_store_addr[slot_num],
6458b453a2bSTaylor Simpson                               ctx->mem_idx);
6468b453a2bSTaylor Simpson             break;
6478b453a2bSTaylor Simpson         default:
6488b453a2bSTaylor Simpson             {
6498b453a2bSTaylor Simpson                 /*
6508b453a2bSTaylor Simpson                  * If we get to here, we don't know the width at
6518b453a2bSTaylor Simpson                  * TCG generation time, we'll use a helper to
6528b453a2bSTaylor Simpson                  * avoid branching based on the width at runtime.
6538b453a2bSTaylor Simpson                  */
65423803bbeSPhilippe Mathieu-Daudé                 TCGv slot = tcg_constant_tl(slot_num);
6558b453a2bSTaylor Simpson                 gen_helper_commit_store(cpu_env, slot);
6568b453a2bSTaylor Simpson             }
6578b453a2bSTaylor Simpson         }
6588b453a2bSTaylor Simpson     }
6598b453a2bSTaylor Simpson     if (is_predicated) {
6608b453a2bSTaylor Simpson         gen_set_label(label_end);
6618b453a2bSTaylor Simpson     }
6628b453a2bSTaylor Simpson }
6638b453a2bSTaylor Simpson 
6641e536334STaylor Simpson static void process_store_log(DisasContext *ctx)
6658b453a2bSTaylor Simpson {
6668b453a2bSTaylor Simpson     /*
6678b453a2bSTaylor Simpson      *  When a packet has two stores, the hardware processes
668c23b5764STaylor Simpson      *  slot 1 and then slot 0.  This will be important when
6698b453a2bSTaylor Simpson      *  the memory accesses overlap.
6708b453a2bSTaylor Simpson      */
6711e536334STaylor Simpson     Packet *pkt = ctx->pkt;
672e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s1) {
673e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
6741e536334STaylor Simpson         process_store(ctx, 1);
6758b453a2bSTaylor Simpson     }
676e2be9a5cSTaylor Simpson     if (pkt->pkt_has_store_s0) {
677e2be9a5cSTaylor Simpson         g_assert(!pkt->pkt_has_dczeroa);
6781e536334STaylor Simpson         process_store(ctx, 0);
6798b453a2bSTaylor Simpson     }
6808b453a2bSTaylor Simpson }
6818b453a2bSTaylor Simpson 
6828b453a2bSTaylor Simpson /* Zero out a 32-bit cache line */
6831e536334STaylor Simpson static void process_dczeroa(DisasContext *ctx)
6848b453a2bSTaylor Simpson {
6851e536334STaylor Simpson     if (ctx->pkt->pkt_has_dczeroa) {
6868b453a2bSTaylor Simpson         /* Store 32 bytes of zero starting at (addr & ~0x1f) */
6878b453a2bSTaylor Simpson         TCGv addr = tcg_temp_new();
68823803bbeSPhilippe Mathieu-Daudé         TCGv_i64 zero = tcg_constant_i64(0);
6898b453a2bSTaylor Simpson 
6908b453a2bSTaylor Simpson         tcg_gen_andi_tl(addr, hex_dczero_addr, ~0x1f);
6918b453a2bSTaylor Simpson         tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
6928b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
6938b453a2bSTaylor Simpson         tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
6948b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
6958b453a2bSTaylor Simpson         tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
6968b453a2bSTaylor Simpson         tcg_gen_addi_tl(addr, addr, 8);
6978b453a2bSTaylor Simpson         tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
6988b453a2bSTaylor Simpson     }
6998b453a2bSTaylor Simpson }
7008b453a2bSTaylor Simpson 
701a82dd548STaylor Simpson static bool pkt_has_hvx_store(Packet *pkt)
702a82dd548STaylor Simpson {
703a82dd548STaylor Simpson     int i;
704a82dd548STaylor Simpson     for (i = 0; i < pkt->num_insns; i++) {
705a82dd548STaylor Simpson         int opcode = pkt->insn[i].opcode;
706a82dd548STaylor Simpson         if (GET_ATTRIB(opcode, A_CVI) && GET_ATTRIB(opcode, A_STORE)) {
707a82dd548STaylor Simpson             return true;
708a82dd548STaylor Simpson         }
709a82dd548STaylor Simpson     }
710a82dd548STaylor Simpson     return false;
711a82dd548STaylor Simpson }
712a82dd548STaylor Simpson 
7131e536334STaylor Simpson static void gen_commit_hvx(DisasContext *ctx)
714a82dd548STaylor Simpson {
715a82dd548STaylor Simpson     int i;
716a82dd548STaylor Simpson 
717a82dd548STaylor Simpson     /*
718a82dd548STaylor Simpson      *    for (i = 0; i < ctx->vreg_log_idx; i++) {
719a82dd548STaylor Simpson      *        int rnum = ctx->vreg_log[i];
720a82dd548STaylor Simpson      *        env->VRegs[rnum] = env->future_VRegs[rnum];
721a82dd548STaylor Simpson      *    }
722a82dd548STaylor Simpson      */
723a82dd548STaylor Simpson     for (i = 0; i < ctx->vreg_log_idx; i++) {
724a82dd548STaylor Simpson         int rnum = ctx->vreg_log[i];
725a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, VRegs[rnum]);
726a82dd548STaylor Simpson         intptr_t srcoff = ctx_future_vreg_off(ctx, rnum, 1, false);
727a82dd548STaylor Simpson         size_t size = sizeof(MMVector);
728a82dd548STaylor Simpson 
729a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
730a82dd548STaylor Simpson     }
731a82dd548STaylor Simpson 
732a82dd548STaylor Simpson     /*
733a82dd548STaylor Simpson      *    for (i = 0; i < ctx->qreg_log_idx; i++) {
734a82dd548STaylor Simpson      *        int rnum = ctx->qreg_log[i];
735a82dd548STaylor Simpson      *        env->QRegs[rnum] = env->future_QRegs[rnum];
736a82dd548STaylor Simpson      *    }
737a82dd548STaylor Simpson      */
738a82dd548STaylor Simpson     for (i = 0; i < ctx->qreg_log_idx; i++) {
739a82dd548STaylor Simpson         int rnum = ctx->qreg_log[i];
740a82dd548STaylor Simpson         intptr_t dstoff = offsetof(CPUHexagonState, QRegs[rnum]);
741a82dd548STaylor Simpson         intptr_t srcoff = offsetof(CPUHexagonState, future_QRegs[rnum]);
742a82dd548STaylor Simpson         size_t size = sizeof(MMQReg);
743a82dd548STaylor Simpson 
744a82dd548STaylor Simpson         tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
745a82dd548STaylor Simpson     }
746a82dd548STaylor Simpson 
7471e536334STaylor Simpson     if (pkt_has_hvx_store(ctx->pkt)) {
748a82dd548STaylor Simpson         gen_helper_commit_hvx_stores(cpu_env);
749a82dd548STaylor Simpson     }
750a82dd548STaylor Simpson }
751a82dd548STaylor Simpson 
7521e536334STaylor Simpson static void update_exec_counters(DisasContext *ctx)
7538b453a2bSTaylor Simpson {
7541e536334STaylor Simpson     Packet *pkt = ctx->pkt;
7558b453a2bSTaylor Simpson     int num_insns = pkt->num_insns;
7568b453a2bSTaylor Simpson     int num_real_insns = 0;
757a82dd548STaylor Simpson     int num_hvx_insns = 0;
7588b453a2bSTaylor Simpson 
7598b453a2bSTaylor Simpson     for (int i = 0; i < num_insns; i++) {
7608b453a2bSTaylor Simpson         if (!pkt->insn[i].is_endloop &&
7618b453a2bSTaylor Simpson             !pkt->insn[i].part1 &&
7628b453a2bSTaylor Simpson             !GET_ATTRIB(pkt->insn[i].opcode, A_IT_NOP)) {
7638b453a2bSTaylor Simpson             num_real_insns++;
7648b453a2bSTaylor Simpson         }
765a82dd548STaylor Simpson         if (GET_ATTRIB(pkt->insn[i].opcode, A_CVI)) {
766a82dd548STaylor Simpson             num_hvx_insns++;
767a82dd548STaylor Simpson         }
7688b453a2bSTaylor Simpson     }
7698b453a2bSTaylor Simpson 
7708b453a2bSTaylor Simpson     ctx->num_packets++;
7718b453a2bSTaylor Simpson     ctx->num_insns += num_real_insns;
772a82dd548STaylor Simpson     ctx->num_hvx_insns += num_hvx_insns;
7738b453a2bSTaylor Simpson }
7748b453a2bSTaylor Simpson 
7751e536334STaylor Simpson static void gen_commit_packet(DisasContext *ctx)
7768b453a2bSTaylor Simpson {
777c23b5764STaylor Simpson     /*
778c23b5764STaylor Simpson      * If there is more than one store in a packet, make sure they are all OK
779c23b5764STaylor Simpson      * before proceeding with the rest of the packet commit.
780c23b5764STaylor Simpson      *
781c23b5764STaylor Simpson      * dczeroa has to be the only store operation in the packet, so we go
782c23b5764STaylor Simpson      * ahead and process that first.
783c23b5764STaylor Simpson      *
784a82dd548STaylor Simpson      * When there is an HVX store, there can also be a scalar store in either
785a82dd548STaylor Simpson      * slot 0 or slot1, so we create a mask for the helper to indicate what
786a82dd548STaylor Simpson      * work to do.
787a82dd548STaylor Simpson      *
788c23b5764STaylor Simpson      * When there are two scalar stores, we probe the one in slot 0.
789c23b5764STaylor Simpson      *
790c23b5764STaylor Simpson      * Note that we don't call the probe helper for packets with only one
791c23b5764STaylor Simpson      * store.  Therefore, we call process_store_log before anything else
792c23b5764STaylor Simpson      * involved in committing the packet.
793c23b5764STaylor Simpson      */
7941e536334STaylor Simpson     Packet *pkt = ctx->pkt;
795c23b5764STaylor Simpson     bool has_store_s0 = pkt->pkt_has_store_s0;
796c23b5764STaylor Simpson     bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed);
797a82dd548STaylor Simpson     bool has_hvx_store = pkt_has_hvx_store(pkt);
798c23b5764STaylor Simpson     if (pkt->pkt_has_dczeroa) {
799c23b5764STaylor Simpson         /*
800c23b5764STaylor Simpson          * The dczeroa will be the store in slot 0, check that we don't have
801a82dd548STaylor Simpson          * a store in slot 1 or an HVX store.
802c23b5764STaylor Simpson          */
803e2be9a5cSTaylor Simpson         g_assert(!has_store_s1 && !has_hvx_store);
8041e536334STaylor Simpson         process_dczeroa(ctx);
805a82dd548STaylor Simpson     } else if (has_hvx_store) {
806a82dd548STaylor Simpson         TCGv mem_idx = tcg_constant_tl(ctx->mem_idx);
807a82dd548STaylor Simpson 
808a82dd548STaylor Simpson         if (!has_store_s0 && !has_store_s1) {
809a82dd548STaylor Simpson             gen_helper_probe_hvx_stores(cpu_env, mem_idx);
810a82dd548STaylor Simpson         } else {
811a82dd548STaylor Simpson             int mask = 0;
812a82dd548STaylor Simpson             TCGv mask_tcgv;
813a82dd548STaylor Simpson 
814a82dd548STaylor Simpson             if (has_store_s0) {
8157b84fd04STaylor Simpson                 mask =
8167b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST0, 1);
817a82dd548STaylor Simpson             }
818a82dd548STaylor Simpson             if (has_store_s1) {
8197b84fd04STaylor Simpson                 mask =
8207b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES, HAS_ST1, 1);
821a82dd548STaylor Simpson             }
822a82dd548STaylor Simpson             if (has_hvx_store) {
8237b84fd04STaylor Simpson                 mask =
8247b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
8257b84fd04STaylor Simpson                                HAS_HVX_STORES, 1);
8267b84fd04STaylor Simpson             }
8277b84fd04STaylor Simpson             if (has_store_s0 && slot_is_predicated(pkt, 0)) {
8287b84fd04STaylor Simpson                 mask =
8297b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
8307b84fd04STaylor Simpson                                S0_IS_PRED, 1);
8317b84fd04STaylor Simpson             }
8327b84fd04STaylor Simpson             if (has_store_s1 && slot_is_predicated(pkt, 1)) {
8337b84fd04STaylor Simpson                 mask =
8347b84fd04STaylor Simpson                     FIELD_DP32(mask, PROBE_PKT_SCALAR_HVX_STORES,
8357b84fd04STaylor Simpson                                S1_IS_PRED, 1);
836a82dd548STaylor Simpson             }
837a82dd548STaylor Simpson             mask_tcgv = tcg_constant_tl(mask);
838a82dd548STaylor Simpson             gen_helper_probe_pkt_scalar_hvx_stores(cpu_env, mask_tcgv, mem_idx);
839a82dd548STaylor Simpson         }
840c23b5764STaylor Simpson     } else if (has_store_s0 && has_store_s1) {
841c23b5764STaylor Simpson         /*
842c23b5764STaylor Simpson          * process_store_log will execute the slot 1 store first,
843c23b5764STaylor Simpson          * so we only have to probe the store in slot 0
844c23b5764STaylor Simpson          */
8457b84fd04STaylor Simpson         int args = 0;
8467b84fd04STaylor Simpson         args =
8477b84fd04STaylor Simpson             FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, MMU_IDX, ctx->mem_idx);
8487b84fd04STaylor Simpson         if (slot_is_predicated(pkt, 0)) {
8497b84fd04STaylor Simpson             args =
8507b84fd04STaylor Simpson                 FIELD_DP32(args, PROBE_PKT_SCALAR_STORE_S0, IS_PREDICATED, 1);
8517b84fd04STaylor Simpson         }
8527b84fd04STaylor Simpson         TCGv args_tcgv = tcg_constant_tl(args);
8537b84fd04STaylor Simpson         gen_helper_probe_pkt_scalar_store_s0(cpu_env, args_tcgv);
854c23b5764STaylor Simpson     }
855c23b5764STaylor Simpson 
8561e536334STaylor Simpson     process_store_log(ctx);
857c23b5764STaylor Simpson 
8588b453a2bSTaylor Simpson     gen_reg_writes(ctx);
8591e536334STaylor Simpson     gen_pred_writes(ctx);
860a82dd548STaylor Simpson     if (pkt->pkt_has_hvx) {
8611e536334STaylor Simpson         gen_commit_hvx(ctx);
862a82dd548STaylor Simpson     }
8631e536334STaylor Simpson     update_exec_counters(ctx);
86485580a65STaylor Simpson     if (HEX_DEBUG) {
8658b453a2bSTaylor Simpson         TCGv has_st0 =
86623803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s0 && !pkt->pkt_has_dczeroa);
8678b453a2bSTaylor Simpson         TCGv has_st1 =
86823803bbeSPhilippe Mathieu-Daudé             tcg_constant_tl(pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa);
8698b453a2bSTaylor Simpson 
8708b453a2bSTaylor Simpson         /* Handy place to set a breakpoint at the end of execution */
8718b453a2bSTaylor Simpson         gen_helper_debug_commit_end(cpu_env, has_st0, has_st1);
8728b453a2bSTaylor Simpson     }
8738b453a2bSTaylor Simpson 
874a82dd548STaylor Simpson     if (pkt->vhist_insn != NULL) {
875a82dd548STaylor Simpson         ctx->pre_commit = false;
8761e536334STaylor Simpson         ctx->insn = pkt->vhist_insn;
8771e536334STaylor Simpson         pkt->vhist_insn->generate(ctx);
878a82dd548STaylor Simpson     }
879a82dd548STaylor Simpson 
8808b453a2bSTaylor Simpson     if (pkt->pkt_has_cof) {
881743debbcSTaylor Simpson         gen_end_tb(ctx);
8828b453a2bSTaylor Simpson     }
8838b453a2bSTaylor Simpson }
8848b453a2bSTaylor Simpson 
8858b453a2bSTaylor Simpson static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
8868b453a2bSTaylor Simpson {
8878b453a2bSTaylor Simpson     uint32_t words[PACKET_WORDS_MAX];
8888b453a2bSTaylor Simpson     int nwords;
8898b453a2bSTaylor Simpson     Packet pkt;
8908b453a2bSTaylor Simpson     int i;
8918b453a2bSTaylor Simpson 
8928b453a2bSTaylor Simpson     nwords = read_packet_words(env, ctx, words);
8938b453a2bSTaylor Simpson     if (!nwords) {
894743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
8958b453a2bSTaylor Simpson         return;
8968b453a2bSTaylor Simpson     }
8978b453a2bSTaylor Simpson 
8988b453a2bSTaylor Simpson     if (decode_packet(nwords, words, &pkt, false) > 0) {
89940085901STaylor Simpson         pkt.pc = ctx->base.pc_next;
9008b453a2bSTaylor Simpson         HEX_DEBUG_PRINT_PKT(&pkt);
9011e536334STaylor Simpson         ctx->pkt = &pkt;
9021e536334STaylor Simpson         gen_start_packet(ctx);
9038b453a2bSTaylor Simpson         for (i = 0; i < pkt.num_insns; i++) {
9041e536334STaylor Simpson             ctx->insn = &pkt.insn[i];
9051e536334STaylor Simpson             gen_insn(ctx);
9068b453a2bSTaylor Simpson         }
9071e536334STaylor Simpson         gen_commit_packet(ctx);
9088b453a2bSTaylor Simpson         ctx->base.pc_next += pkt.encod_pkt_size_in_bytes;
9098b453a2bSTaylor Simpson     } else {
910743debbcSTaylor Simpson         gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
9118b453a2bSTaylor Simpson     }
9128b453a2bSTaylor Simpson }
9138b453a2bSTaylor Simpson 
9148b453a2bSTaylor Simpson static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
9158b453a2bSTaylor Simpson                                           CPUState *cs)
9168b453a2bSTaylor Simpson {
9178b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
918564b2040STaylor Simpson     uint32_t hex_flags = dcbase->tb->flags;
9198b453a2bSTaylor Simpson 
9208b453a2bSTaylor Simpson     ctx->mem_idx = MMU_USER_IDX;
9218b453a2bSTaylor Simpson     ctx->num_packets = 0;
9228b453a2bSTaylor Simpson     ctx->num_insns = 0;
923a82dd548STaylor Simpson     ctx->num_hvx_insns = 0;
9241b9a7f2aSTaylor Simpson     ctx->branch_cond = TCG_COND_NEVER;
925564b2040STaylor Simpson     ctx->is_tight_loop = FIELD_EX32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP);
9268b453a2bSTaylor Simpson }
9278b453a2bSTaylor Simpson 
9288b453a2bSTaylor Simpson static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
9298b453a2bSTaylor Simpson {
9308b453a2bSTaylor Simpson }
9318b453a2bSTaylor Simpson 
9328b453a2bSTaylor Simpson static void hexagon_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
9338b453a2bSTaylor Simpson {
9348b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
9358b453a2bSTaylor Simpson 
9368b453a2bSTaylor Simpson     tcg_gen_insn_start(ctx->base.pc_next);
9378b453a2bSTaylor Simpson }
9388b453a2bSTaylor Simpson 
9398b453a2bSTaylor Simpson static bool pkt_crosses_page(CPUHexagonState *env, DisasContext *ctx)
9408b453a2bSTaylor Simpson {
9418b453a2bSTaylor Simpson     target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
9428b453a2bSTaylor Simpson     bool found_end = false;
9438b453a2bSTaylor Simpson     int nwords;
9448b453a2bSTaylor Simpson 
9458b453a2bSTaylor Simpson     for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
9468b453a2bSTaylor Simpson         uint32_t word = cpu_ldl_code(env,
9478b453a2bSTaylor Simpson                             ctx->base.pc_next + nwords * sizeof(uint32_t));
9488b453a2bSTaylor Simpson         found_end = is_packet_end(word);
9498b453a2bSTaylor Simpson     }
9508b453a2bSTaylor Simpson     uint32_t next_ptr =  ctx->base.pc_next + nwords * sizeof(uint32_t);
9518b453a2bSTaylor Simpson     return found_end && next_ptr - page_start >= TARGET_PAGE_SIZE;
9528b453a2bSTaylor Simpson }
9538b453a2bSTaylor Simpson 
9548b453a2bSTaylor Simpson static void hexagon_tr_translate_packet(DisasContextBase *dcbase, CPUState *cpu)
9558b453a2bSTaylor Simpson {
9568b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
9578b453a2bSTaylor Simpson     CPUHexagonState *env = cpu->env_ptr;
9588b453a2bSTaylor Simpson 
9598b453a2bSTaylor Simpson     decode_and_translate_packet(env, ctx);
9608b453a2bSTaylor Simpson 
9618b453a2bSTaylor Simpson     if (ctx->base.is_jmp == DISAS_NEXT) {
9628b453a2bSTaylor Simpson         target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
9638b453a2bSTaylor Simpson         target_ulong bytes_max = PACKET_WORDS_MAX * sizeof(target_ulong);
9648b453a2bSTaylor Simpson 
9658b453a2bSTaylor Simpson         if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE ||
9668b453a2bSTaylor Simpson             (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE - bytes_max &&
9678b453a2bSTaylor Simpson              pkt_crosses_page(env, ctx))) {
9688b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
9698b453a2bSTaylor Simpson         }
9708b453a2bSTaylor Simpson 
9718b453a2bSTaylor Simpson         /*
9728b453a2bSTaylor Simpson          * The CPU log is used to compare against LLDB single stepping,
9738b453a2bSTaylor Simpson          * so end the TLB after every packet.
9748b453a2bSTaylor Simpson          */
9757d9ab202STaylor Simpson         HexagonCPU *hex_cpu = env_archcpu(env);
9768b453a2bSTaylor Simpson         if (hex_cpu->lldb_compat && qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
9778b453a2bSTaylor Simpson             ctx->base.is_jmp = DISAS_TOO_MANY;
9788b453a2bSTaylor Simpson         }
9798b453a2bSTaylor Simpson     }
9808b453a2bSTaylor Simpson }
9818b453a2bSTaylor Simpson 
9828b453a2bSTaylor Simpson static void hexagon_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
9838b453a2bSTaylor Simpson {
9848b453a2bSTaylor Simpson     DisasContext *ctx = container_of(dcbase, DisasContext, base);
9858b453a2bSTaylor Simpson 
9868b453a2bSTaylor Simpson     switch (ctx->base.is_jmp) {
9878b453a2bSTaylor Simpson     case DISAS_TOO_MANY:
9888b453a2bSTaylor Simpson         gen_exec_counters(ctx);
9898b453a2bSTaylor Simpson         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next);
9908b453a2bSTaylor Simpson         tcg_gen_exit_tb(NULL, 0);
9918b453a2bSTaylor Simpson         break;
9928b453a2bSTaylor Simpson     case DISAS_NORETURN:
9938b453a2bSTaylor Simpson         break;
9948b453a2bSTaylor Simpson     default:
9958b453a2bSTaylor Simpson         g_assert_not_reached();
9968b453a2bSTaylor Simpson     }
9978b453a2bSTaylor Simpson }
9988b453a2bSTaylor Simpson 
9998eb806a7SRichard Henderson static void hexagon_tr_disas_log(const DisasContextBase *dcbase,
10008eb806a7SRichard Henderson                                  CPUState *cpu, FILE *logfile)
10018b453a2bSTaylor Simpson {
10028eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
10038eb806a7SRichard Henderson     target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
10048b453a2bSTaylor Simpson }
10058b453a2bSTaylor Simpson 
10068b453a2bSTaylor Simpson 
10078b453a2bSTaylor Simpson static const TranslatorOps hexagon_tr_ops = {
10088b453a2bSTaylor Simpson     .init_disas_context = hexagon_tr_init_disas_context,
10098b453a2bSTaylor Simpson     .tb_start           = hexagon_tr_tb_start,
10108b453a2bSTaylor Simpson     .insn_start         = hexagon_tr_insn_start,
10118b453a2bSTaylor Simpson     .translate_insn     = hexagon_tr_translate_packet,
10128b453a2bSTaylor Simpson     .tb_stop            = hexagon_tr_tb_stop,
10138b453a2bSTaylor Simpson     .disas_log          = hexagon_tr_disas_log,
10148b453a2bSTaylor Simpson };
10158b453a2bSTaylor Simpson 
1016597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
1017306c8721SRichard Henderson                            target_ulong pc, void *host_pc)
10188b453a2bSTaylor Simpson {
10198b453a2bSTaylor Simpson     DisasContext ctx;
10208b453a2bSTaylor Simpson 
1021306c8721SRichard Henderson     translator_loop(cs, tb, max_insns, pc, host_pc,
1022306c8721SRichard Henderson                     &hexagon_tr_ops, &ctx.base);
10238b453a2bSTaylor Simpson }
10248b453a2bSTaylor Simpson 
10258b453a2bSTaylor Simpson #define NAME_LEN               64
10268b453a2bSTaylor Simpson static char new_value_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
10278b453a2bSTaylor Simpson static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
10288b453a2bSTaylor Simpson static char new_pred_value_names[NUM_PREGS][NAME_LEN];
10298b453a2bSTaylor Simpson static char store_addr_names[STORES_MAX][NAME_LEN];
10308b453a2bSTaylor Simpson static char store_width_names[STORES_MAX][NAME_LEN];
10318b453a2bSTaylor Simpson static char store_val32_names[STORES_MAX][NAME_LEN];
10328b453a2bSTaylor Simpson static char store_val64_names[STORES_MAX][NAME_LEN];
1033a82dd548STaylor Simpson static char vstore_addr_names[VSTORES_MAX][NAME_LEN];
1034a82dd548STaylor Simpson static char vstore_size_names[VSTORES_MAX][NAME_LEN];
1035a82dd548STaylor Simpson static char vstore_pending_names[VSTORES_MAX][NAME_LEN];
10368b453a2bSTaylor Simpson 
10378b453a2bSTaylor Simpson void hexagon_translate_init(void)
10388b453a2bSTaylor Simpson {
10398b453a2bSTaylor Simpson     int i;
10408b453a2bSTaylor Simpson 
10418b453a2bSTaylor Simpson     opcode_init();
10428b453a2bSTaylor Simpson 
10438b453a2bSTaylor Simpson     for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
10448b453a2bSTaylor Simpson         hex_gpr[i] = tcg_global_mem_new(cpu_env,
10458b453a2bSTaylor Simpson             offsetof(CPUHexagonState, gpr[i]),
10468b453a2bSTaylor Simpson             hexagon_regnames[i]);
10478b453a2bSTaylor Simpson 
10488b453a2bSTaylor Simpson         snprintf(new_value_names[i], NAME_LEN, "new_%s", hexagon_regnames[i]);
10498b453a2bSTaylor Simpson         hex_new_value[i] = tcg_global_mem_new(cpu_env,
10508b453a2bSTaylor Simpson             offsetof(CPUHexagonState, new_value[i]),
10518b453a2bSTaylor Simpson             new_value_names[i]);
10528b453a2bSTaylor Simpson 
105385580a65STaylor Simpson         if (HEX_DEBUG) {
10548b453a2bSTaylor Simpson             snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s",
10558b453a2bSTaylor Simpson                      hexagon_regnames[i]);
10568b453a2bSTaylor Simpson             hex_reg_written[i] = tcg_global_mem_new(cpu_env,
10578b453a2bSTaylor Simpson                 offsetof(CPUHexagonState, reg_written[i]),
10588b453a2bSTaylor Simpson                 reg_written_names[i]);
105985580a65STaylor Simpson         }
10608b453a2bSTaylor Simpson     }
10618b453a2bSTaylor Simpson     for (i = 0; i < NUM_PREGS; i++) {
10628b453a2bSTaylor Simpson         hex_pred[i] = tcg_global_mem_new(cpu_env,
10638b453a2bSTaylor Simpson             offsetof(CPUHexagonState, pred[i]),
10648b453a2bSTaylor Simpson             hexagon_prednames[i]);
10658b453a2bSTaylor Simpson 
10668b453a2bSTaylor Simpson         snprintf(new_pred_value_names[i], NAME_LEN, "new_pred_%s",
10678b453a2bSTaylor Simpson                  hexagon_prednames[i]);
10688b453a2bSTaylor Simpson         hex_new_pred_value[i] = tcg_global_mem_new(cpu_env,
10698b453a2bSTaylor Simpson             offsetof(CPUHexagonState, new_pred_value[i]),
10708b453a2bSTaylor Simpson             new_pred_value_names[i]);
10718b453a2bSTaylor Simpson     }
10728b453a2bSTaylor Simpson     hex_pred_written = tcg_global_mem_new(cpu_env,
10738b453a2bSTaylor Simpson         offsetof(CPUHexagonState, pred_written), "pred_written");
10748b453a2bSTaylor Simpson     hex_this_PC = tcg_global_mem_new(cpu_env,
10758b453a2bSTaylor Simpson         offsetof(CPUHexagonState, this_PC), "this_PC");
10768b453a2bSTaylor Simpson     hex_slot_cancelled = tcg_global_mem_new(cpu_env,
10778b453a2bSTaylor Simpson         offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled");
10788b453a2bSTaylor Simpson     hex_branch_taken = tcg_global_mem_new(cpu_env,
10798b453a2bSTaylor Simpson         offsetof(CPUHexagonState, branch_taken), "branch_taken");
10808b453a2bSTaylor Simpson     hex_pkt_has_store_s1 = tcg_global_mem_new(cpu_env,
10818b453a2bSTaylor Simpson         offsetof(CPUHexagonState, pkt_has_store_s1), "pkt_has_store_s1");
10828b453a2bSTaylor Simpson     hex_dczero_addr = tcg_global_mem_new(cpu_env,
10838b453a2bSTaylor Simpson         offsetof(CPUHexagonState, dczero_addr), "dczero_addr");
10848b453a2bSTaylor Simpson     hex_llsc_addr = tcg_global_mem_new(cpu_env,
10858b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_addr), "llsc_addr");
10868b453a2bSTaylor Simpson     hex_llsc_val = tcg_global_mem_new(cpu_env,
10878b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val), "llsc_val");
10888b453a2bSTaylor Simpson     hex_llsc_val_i64 = tcg_global_mem_new_i64(cpu_env,
10898b453a2bSTaylor Simpson         offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
10908b453a2bSTaylor Simpson     for (i = 0; i < STORES_MAX; i++) {
10918b453a2bSTaylor Simpson         snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
10928b453a2bSTaylor Simpson         hex_store_addr[i] = tcg_global_mem_new(cpu_env,
10938b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].va),
10948b453a2bSTaylor Simpson             store_addr_names[i]);
10958b453a2bSTaylor Simpson 
10968b453a2bSTaylor Simpson         snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i);
10978b453a2bSTaylor Simpson         hex_store_width[i] = tcg_global_mem_new(cpu_env,
10988b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].width),
10998b453a2bSTaylor Simpson             store_width_names[i]);
11008b453a2bSTaylor Simpson 
11018b453a2bSTaylor Simpson         snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i);
11028b453a2bSTaylor Simpson         hex_store_val32[i] = tcg_global_mem_new(cpu_env,
11038b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data32),
11048b453a2bSTaylor Simpson             store_val32_names[i]);
11058b453a2bSTaylor Simpson 
11068b453a2bSTaylor Simpson         snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i);
11078b453a2bSTaylor Simpson         hex_store_val64[i] = tcg_global_mem_new_i64(cpu_env,
11088b453a2bSTaylor Simpson             offsetof(CPUHexagonState, mem_log_stores[i].data64),
11098b453a2bSTaylor Simpson             store_val64_names[i]);
11108b453a2bSTaylor Simpson     }
1111a82dd548STaylor Simpson     for (int i = 0; i < VSTORES_MAX; i++) {
1112a82dd548STaylor Simpson         snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i);
1113a82dd548STaylor Simpson         hex_vstore_addr[i] = tcg_global_mem_new(cpu_env,
1114a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].va),
1115a82dd548STaylor Simpson             vstore_addr_names[i]);
1116a82dd548STaylor Simpson 
1117a82dd548STaylor Simpson         snprintf(vstore_size_names[i], NAME_LEN, "vstore_size_%d", i);
1118a82dd548STaylor Simpson         hex_vstore_size[i] = tcg_global_mem_new(cpu_env,
1119a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore[i].size),
1120a82dd548STaylor Simpson             vstore_size_names[i]);
1121a82dd548STaylor Simpson 
1122a82dd548STaylor Simpson         snprintf(vstore_pending_names[i], NAME_LEN, "vstore_pending_%d", i);
1123a82dd548STaylor Simpson         hex_vstore_pending[i] = tcg_global_mem_new(cpu_env,
1124a82dd548STaylor Simpson             offsetof(CPUHexagonState, vstore_pending[i]),
1125a82dd548STaylor Simpson             vstore_pending_names[i]);
1126a82dd548STaylor Simpson     }
11278b453a2bSTaylor Simpson }
1128