]> git.proxmox.com Git - mirror_qemu.git/blame - target/hexagon/translate.c
Hexagon (target/hexagon) Only use branch_taken when packet has multi cof
[mirror_qemu.git] / target / hexagon / translate.c
CommitLineData
8b453a2b 1/*
e2be9a5c 2 * Copyright(c) 2019-2022 Qualcomm Innovation Center, Inc. All Rights Reserved.
8b453a2b
TS
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define QEMU_GENERATE
19#include "qemu/osdep.h"
20#include "cpu.h"
21#include "tcg/tcg-op.h"
a82dd548 22#include "tcg/tcg-op-gvec.h"
8b453a2b
TS
23#include "exec/cpu_ldst.h"
24#include "exec/log.h"
25#include "internal.h"
26#include "attribs.h"
27#include "insn.h"
28#include "decode.h"
29#include "translate.h"
30#include "printinsn.h"
31
32TCGv hex_gpr[TOTAL_PER_THREAD_REGS];
33TCGv hex_pred[NUM_PREGS];
34TCGv hex_next_PC;
35TCGv hex_this_PC;
36TCGv hex_slot_cancelled;
37TCGv hex_branch_taken;
38TCGv hex_new_value[TOTAL_PER_THREAD_REGS];
8b453a2b 39TCGv hex_reg_written[TOTAL_PER_THREAD_REGS];
8b453a2b
TS
40TCGv hex_new_pred_value[NUM_PREGS];
41TCGv hex_pred_written;
42TCGv hex_store_addr[STORES_MAX];
43TCGv hex_store_width[STORES_MAX];
44TCGv hex_store_val32[STORES_MAX];
45TCGv_i64 hex_store_val64[STORES_MAX];
46TCGv hex_pkt_has_store_s1;
47TCGv hex_dczero_addr;
48TCGv hex_llsc_addr;
49TCGv hex_llsc_val;
50TCGv_i64 hex_llsc_val_i64;
a82dd548
TS
51TCGv hex_VRegs_updated;
52TCGv hex_QRegs_updated;
53TCGv hex_vstore_addr[VSTORES_MAX];
54TCGv hex_vstore_size[VSTORES_MAX];
55TCGv hex_vstore_pending[VSTORES_MAX];
8b453a2b
TS
56
57static const char * const hexagon_prednames[] = {
58 "p0", "p1", "p2", "p3"
59};
60
a82dd548
TS
61intptr_t ctx_future_vreg_off(DisasContext *ctx, int regnum,
62 int num, bool alloc_ok)
63{
64 intptr_t offset;
65
66 /* See if it is already allocated */
67 for (int i = 0; i < ctx->future_vregs_idx; i++) {
68 if (ctx->future_vregs_num[i] == regnum) {
69 return offsetof(CPUHexagonState, future_VRegs[i]);
70 }
71 }
72
73 g_assert(alloc_ok);
74 offset = offsetof(CPUHexagonState, future_VRegs[ctx->future_vregs_idx]);
75 for (int i = 0; i < num; i++) {
76 ctx->future_vregs_num[ctx->future_vregs_idx + i] = regnum++;
77 }
78 ctx->future_vregs_idx += num;
79 g_assert(ctx->future_vregs_idx <= VECTOR_TEMPS_MAX);
80 return offset;
81}
82
83intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,
84 int num, bool alloc_ok)
85{
86 intptr_t offset;
87
88 /* See if it is already allocated */
89 for (int i = 0; i < ctx->tmp_vregs_idx; i++) {
90 if (ctx->tmp_vregs_num[i] == regnum) {
91 return offsetof(CPUHexagonState, tmp_VRegs[i]);
92 }
93 }
94
95 g_assert(alloc_ok);
96 offset = offsetof(CPUHexagonState, tmp_VRegs[ctx->tmp_vregs_idx]);
97 for (int i = 0; i < num; i++) {
98 ctx->tmp_vregs_num[ctx->tmp_vregs_idx + i] = regnum++;
99 }
100 ctx->tmp_vregs_idx += num;
101 g_assert(ctx->tmp_vregs_idx <= VECTOR_TEMPS_MAX);
102 return offset;
103}
104
743debbc 105static void gen_exception_raw(int excp)
8b453a2b 106{
23803bbe 107 gen_helper_raise_exception(cpu_env, tcg_constant_i32(excp));
8b453a2b
TS
108}
109
743debbc
TS
110static void gen_exec_counters(DisasContext *ctx)
111{
112 tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT],
113 hex_gpr[HEX_REG_QEMU_PKT_CNT], ctx->num_packets);
114 tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_INSN_CNT],
115 hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns);
a82dd548
TS
116 tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT],
117 hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns);
743debbc
TS
118}
119
120static void gen_end_tb(DisasContext *ctx)
8b453a2b 121{
743debbc
TS
122 gen_exec_counters(ctx);
123 tcg_gen_mov_tl(hex_gpr[HEX_REG_PC], hex_next_PC);
db07bd02 124 tcg_gen_exit_tb(NULL, 0);
743debbc
TS
125 ctx->base.is_jmp = DISAS_NORETURN;
126}
127
128static void gen_exception_end_tb(DisasContext *ctx, int excp)
129{
130 gen_exec_counters(ctx);
131 tcg_gen_mov_tl(hex_gpr[HEX_REG_PC], hex_next_PC);
132 gen_exception_raw(excp);
133 ctx->base.is_jmp = DISAS_NORETURN;
134
8b453a2b
TS
135}
136
8b453a2b
TS
137#define PACKET_BUFFER_LEN 1028
138static void print_pkt(Packet *pkt)
139{
140 GString *buf = g_string_sized_new(PACKET_BUFFER_LEN);
141 snprint_a_pkt_debug(buf, pkt);
142 HEX_DEBUG_LOG("%s", buf->str);
143 g_string_free(buf, true);
144}
85580a65
TS
145#define HEX_DEBUG_PRINT_PKT(pkt) \
146 do { \
147 if (HEX_DEBUG) { \
148 print_pkt(pkt); \
149 } \
150 } while (0)
8b453a2b
TS
151
152static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
153 uint32_t words[])
154{
155 bool found_end = false;
156 int nwords, max_words;
157
158 memset(words, 0, PACKET_WORDS_MAX * sizeof(uint32_t));
159 for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
a27c100c 160 words[nwords] =
4e116893
IL
161 translator_ldl(env, &ctx->base,
162 ctx->base.pc_next + nwords * sizeof(uint32_t));
8b453a2b
TS
163 found_end = is_packet_end(words[nwords]);
164 }
165 if (!found_end) {
166 /* Read too many words without finding the end */
167 return 0;
168 }
169
170 /* Check for page boundary crossing */
171 max_words = -(ctx->base.pc_next | TARGET_PAGE_MASK) / sizeof(uint32_t);
172 if (nwords > max_words) {
173 /* We can only cross a page boundary at the beginning of a TB */
174 g_assert(ctx->base.num_insns == 1);
175 }
176
177 HEX_DEBUG_LOG("decode_packet: pc = 0x%x\n", ctx->base.pc_next);
178 HEX_DEBUG_LOG(" words = { ");
179 for (int i = 0; i < nwords; i++) {
180 HEX_DEBUG_LOG("0x%x, ", words[i]);
181 }
182 HEX_DEBUG_LOG("}\n");
183
184 return nwords;
185}
186
187static bool check_for_attrib(Packet *pkt, int attrib)
188{
189 for (int i = 0; i < pkt->num_insns; i++) {
190 if (GET_ATTRIB(pkt->insn[i].opcode, attrib)) {
191 return true;
192 }
193 }
194 return false;
195}
196
197static bool need_pc(Packet *pkt)
198{
199 return check_for_attrib(pkt, A_IMPLICIT_READS_PC);
200}
201
202static bool need_slot_cancelled(Packet *pkt)
203{
204 return check_for_attrib(pkt, A_CONDEXEC);
205}
206
207static bool need_pred_written(Packet *pkt)
208{
209 return check_for_attrib(pkt, A_WRITES_PRED_REG);
210}
211
1e536334 212static void gen_start_packet(DisasContext *ctx)
8b453a2b 213{
1e536334 214 Packet *pkt = ctx->pkt;
8b453a2b
TS
215 target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes;
216 int i;
217
218 /* Clear out the disassembly context */
219 ctx->reg_log_idx = 0;
220 bitmap_zero(ctx->regs_written, TOTAL_PER_THREAD_REGS);
221 ctx->preg_log_idx = 0;
6c677c60 222 bitmap_zero(ctx->pregs_written, NUM_PREGS);
a82dd548
TS
223 ctx->future_vregs_idx = 0;
224 ctx->tmp_vregs_idx = 0;
225 ctx->vreg_log_idx = 0;
226 bitmap_zero(ctx->vregs_updated_tmp, NUM_VREGS);
227 bitmap_zero(ctx->vregs_updated, NUM_VREGS);
228 bitmap_zero(ctx->vregs_select, NUM_VREGS);
229 ctx->qreg_log_idx = 0;
8b453a2b
TS
230 for (i = 0; i < STORES_MAX; i++) {
231 ctx->store_width[i] = 0;
232 }
233 tcg_gen_movi_tl(hex_pkt_has_store_s1, pkt->pkt_has_store_s1);
92cfa25f 234 ctx->s1_store_processed = false;
a82dd548 235 ctx->pre_commit = true;
8b453a2b 236
85580a65
TS
237 if (HEX_DEBUG) {
238 /* Handy place to set a breakpoint before the packet executes */
239 gen_helper_debug_start_packet(cpu_env);
240 tcg_gen_movi_tl(hex_this_PC, ctx->base.pc_next);
241 }
8b453a2b
TS
242
243 /* Initialize the runtime state for packet semantics */
244 if (need_pc(pkt)) {
245 tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next);
246 }
247 if (need_slot_cancelled(pkt)) {
248 tcg_gen_movi_tl(hex_slot_cancelled, 0);
249 }
250 if (pkt->pkt_has_cof) {
fb67c2bf
TS
251 if (pkt->pkt_has_multi_cof) {
252 tcg_gen_movi_tl(hex_branch_taken, 0);
253 }
8b453a2b
TS
254 tcg_gen_movi_tl(hex_next_PC, next_PC);
255 }
256 if (need_pred_written(pkt)) {
257 tcg_gen_movi_tl(hex_pred_written, 0);
258 }
a82dd548
TS
259
260 if (pkt->pkt_has_hvx) {
261 tcg_gen_movi_tl(hex_VRegs_updated, 0);
262 tcg_gen_movi_tl(hex_QRegs_updated, 0);
263 }
264}
265
1e536334 266bool is_gather_store_insn(DisasContext *ctx)
a82dd548 267{
1e536334
TS
268 Packet *pkt = ctx->pkt;
269 Insn *insn = ctx->insn;
a82dd548
TS
270 if (GET_ATTRIB(insn->opcode, A_CVI_NEW) &&
271 insn->new_value_producer_slot == 1) {
272 /* Look for gather instruction */
273 for (int i = 0; i < pkt->num_insns; i++) {
274 Insn *in = &pkt->insn[i];
275 if (GET_ATTRIB(in->opcode, A_CVI_GATHER) && in->slot == 1) {
276 return true;
277 }
278 }
279 }
280 return false;
8b453a2b
TS
281}
282
283/*
284 * The LOG_*_WRITE macros mark most of the writes in a packet
285 * However, there are some implicit writes marked as attributes
286 * of the applicable instructions.
287 */
1e536334 288static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
8b453a2b 289{
1e536334
TS
290 uint16_t opcode = ctx->insn->opcode;
291 if (GET_ATTRIB(opcode, attrib)) {
b9dd6ff9
TS
292 /*
293 * USR is used to set overflow and FP exceptions,
294 * so treat it as conditional
295 */
1e536334 296 bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) ||
b9dd6ff9 297 rnum == HEX_REG_USR;
8b453a2b
TS
298 if (is_predicated && !is_preloaded(ctx, rnum)) {
299 tcg_gen_mov_tl(hex_new_value[rnum], hex_gpr[rnum]);
300 }
301
302 ctx_log_reg_write(ctx, rnum);
303 }
304}
305
1e536334 306static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum)
8b453a2b 307{
1e536334 308 if (GET_ATTRIB(ctx->insn->opcode, attrib)) {
8b453a2b
TS
309 ctx_log_pred_write(ctx, pnum);
310 }
311}
312
1e536334 313static void mark_implicit_reg_writes(DisasContext *ctx)
8b453a2b 314{
1e536334
TS
315 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP, HEX_REG_FP);
316 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SP, HEX_REG_SP);
317 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LR, HEX_REG_LR);
318 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC0, HEX_REG_LC0);
319 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0);
320 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1);
321 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1);
322 mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR);
323 mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR);
6c677c60 324}
8b453a2b 325
1e536334 326static void mark_implicit_pred_writes(DisasContext *ctx)
6c677c60 327{
1e536334
TS
328 mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P0, 0);
329 mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P1, 1);
330 mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P2, 2);
331 mark_implicit_pred_write(ctx, A_IMPLICIT_WRITES_P3, 3);
8b453a2b
TS
332}
333
1e536334 334static void mark_store_width(DisasContext *ctx)
661ad999 335{
1e536334
TS
336 uint16_t opcode = ctx->insn->opcode;
337 uint32_t slot = ctx->insn->slot;
661ad999
TS
338 uint8_t width = 0;
339
340 if (GET_ATTRIB(opcode, A_SCALAR_STORE)) {
341 if (GET_ATTRIB(opcode, A_MEMSIZE_1B)) {
342 width |= 1;
343 }
344 if (GET_ATTRIB(opcode, A_MEMSIZE_2B)) {
345 width |= 2;
346 }
347 if (GET_ATTRIB(opcode, A_MEMSIZE_4B)) {
348 width |= 4;
349 }
350 if (GET_ATTRIB(opcode, A_MEMSIZE_8B)) {
351 width |= 8;
352 }
353 tcg_debug_assert(is_power_of_2(width));
354 ctx->store_width[slot] = width;
355 }
356}
357
1e536334 358static void gen_insn(DisasContext *ctx)
8b453a2b 359{
1e536334
TS
360 if (ctx->insn->generate) {
361 mark_implicit_reg_writes(ctx);
362 ctx->insn->generate(ctx);
363 mark_implicit_pred_writes(ctx);
364 mark_store_width(ctx);
8b453a2b 365 } else {
743debbc 366 gen_exception_end_tb(ctx, HEX_EXCP_INVALID_OPCODE);
8b453a2b
TS
367 }
368}
369
370/*
371 * Helpers for generating the packet commit
372 */
373static void gen_reg_writes(DisasContext *ctx)
374{
375 int i;
376
377 for (i = 0; i < ctx->reg_log_idx; i++) {
378 int reg_num = ctx->reg_log[i];
379
380 tcg_gen_mov_tl(hex_gpr[reg_num], hex_new_value[reg_num]);
381 }
382}
383
1e536334 384static void gen_pred_writes(DisasContext *ctx)
8b453a2b 385{
8b453a2b
TS
386 int i;
387
388 /* Early exit if the log is empty */
389 if (!ctx->preg_log_idx) {
390 return;
391 }
392
8b453a2b
TS
393 /*
394 * Only endloop instructions will conditionally
395 * write a predicate. If there are no endloop
396 * instructions, we can use the non-conditional
397 * write of the predicates.
398 */
1e536334 399 if (ctx->pkt->pkt_has_endloop) {
23803bbe 400 TCGv zero = tcg_constant_tl(0);
8b453a2b
TS
401 TCGv pred_written = tcg_temp_new();
402 for (i = 0; i < ctx->preg_log_idx; i++) {
403 int pred_num = ctx->preg_log[i];
404
405 tcg_gen_andi_tl(pred_written, hex_pred_written, 1 << pred_num);
406 tcg_gen_movcond_tl(TCG_COND_NE, hex_pred[pred_num],
407 pred_written, zero,
408 hex_new_pred_value[pred_num],
409 hex_pred[pred_num]);
410 }
411 tcg_temp_free(pred_written);
412 } else {
413 for (i = 0; i < ctx->preg_log_idx; i++) {
414 int pred_num = ctx->preg_log[i];
415 tcg_gen_mov_tl(hex_pred[pred_num], hex_new_pred_value[pred_num]);
85580a65
TS
416 if (HEX_DEBUG) {
417 /* Do this so HELPER(debug_commit_end) will know */
418 tcg_gen_ori_tl(hex_pred_written, hex_pred_written,
419 1 << pred_num);
420 }
8b453a2b
TS
421 }
422 }
8b453a2b
TS
423}
424
a27c100c 425static void gen_check_store_width(DisasContext *ctx, int slot_num)
8b453a2b 426{
85580a65 427 if (HEX_DEBUG) {
23803bbe
PMD
428 TCGv slot = tcg_constant_tl(slot_num);
429 TCGv check = tcg_constant_tl(ctx->store_width[slot_num]);
85580a65 430 gen_helper_debug_check_store_width(cpu_env, slot, check);
85580a65 431 }
a27c100c 432}
8b453a2b
TS
433
434static bool slot_is_predicated(Packet *pkt, int slot_num)
435{
436 for (int i = 0; i < pkt->num_insns; i++) {
437 if (pkt->insn[i].slot == slot_num) {
438 return GET_ATTRIB(pkt->insn[i].opcode, A_CONDEXEC);
439 }
440 }
441 /* If we get to here, we didn't find an instruction in the requested slot */
442 g_assert_not_reached();
443}
444
1e536334 445void process_store(DisasContext *ctx, int slot_num)
8b453a2b 446{
1e536334 447 bool is_predicated = slot_is_predicated(ctx->pkt, slot_num);
8b453a2b
TS
448 TCGLabel *label_end = NULL;
449
450 /*
451 * We may have already processed this store
452 * See CHECK_NOSHUF in macros.h
453 */
454 if (slot_num == 1 && ctx->s1_store_processed) {
455 return;
456 }
92cfa25f 457 ctx->s1_store_processed = true;
8b453a2b
TS
458
459 if (is_predicated) {
460 TCGv cancelled = tcg_temp_new();
461 label_end = gen_new_label();
462
463 /* Don't do anything if the slot was cancelled */
464 tcg_gen_extract_tl(cancelled, hex_slot_cancelled, slot_num, 1);
465 tcg_gen_brcondi_tl(TCG_COND_NE, cancelled, 0, label_end);
466 tcg_temp_free(cancelled);
467 }
468 {
469 TCGv address = tcg_temp_local_new();
470 tcg_gen_mov_tl(address, hex_store_addr[slot_num]);
471
472 /*
473 * If we know the width from the DisasContext, we can
474 * generate much cleaner code.
475 * Unfortunately, not all instructions execute the fSTORE
476 * macro during code generation. Anything that uses the
477 * generic helper will have this problem. Instructions
478 * that use fWRAP to generate proper TCG code will be OK.
479 */
480 switch (ctx->store_width[slot_num]) {
481 case 1:
a27c100c 482 gen_check_store_width(ctx, slot_num);
8b453a2b
TS
483 tcg_gen_qemu_st8(hex_store_val32[slot_num],
484 hex_store_addr[slot_num],
485 ctx->mem_idx);
486 break;
487 case 2:
a27c100c 488 gen_check_store_width(ctx, slot_num);
8b453a2b
TS
489 tcg_gen_qemu_st16(hex_store_val32[slot_num],
490 hex_store_addr[slot_num],
491 ctx->mem_idx);
492 break;
493 case 4:
a27c100c 494 gen_check_store_width(ctx, slot_num);
8b453a2b
TS
495 tcg_gen_qemu_st32(hex_store_val32[slot_num],
496 hex_store_addr[slot_num],
497 ctx->mem_idx);
498 break;
499 case 8:
a27c100c 500 gen_check_store_width(ctx, slot_num);
8b453a2b
TS
501 tcg_gen_qemu_st64(hex_store_val64[slot_num],
502 hex_store_addr[slot_num],
503 ctx->mem_idx);
504 break;
505 default:
506 {
507 /*
508 * If we get to here, we don't know the width at
509 * TCG generation time, we'll use a helper to
510 * avoid branching based on the width at runtime.
511 */
23803bbe 512 TCGv slot = tcg_constant_tl(slot_num);
8b453a2b 513 gen_helper_commit_store(cpu_env, slot);
8b453a2b
TS
514 }
515 }
516 tcg_temp_free(address);
517 }
518 if (is_predicated) {
519 gen_set_label(label_end);
520 }
521}
522
1e536334 523static void process_store_log(DisasContext *ctx)
8b453a2b
TS
524{
525 /*
526 * When a packet has two stores, the hardware processes
c23b5764 527 * slot 1 and then slot 0. This will be important when
8b453a2b
TS
528 * the memory accesses overlap.
529 */
1e536334 530 Packet *pkt = ctx->pkt;
e2be9a5c
TS
531 if (pkt->pkt_has_store_s1) {
532 g_assert(!pkt->pkt_has_dczeroa);
1e536334 533 process_store(ctx, 1);
8b453a2b 534 }
e2be9a5c
TS
535 if (pkt->pkt_has_store_s0) {
536 g_assert(!pkt->pkt_has_dczeroa);
1e536334 537 process_store(ctx, 0);
8b453a2b
TS
538 }
539}
540
541/* Zero out a 32-bit cache line */
1e536334 542static void process_dczeroa(DisasContext *ctx)
8b453a2b 543{
1e536334 544 if (ctx->pkt->pkt_has_dczeroa) {
8b453a2b
TS
545 /* Store 32 bytes of zero starting at (addr & ~0x1f) */
546 TCGv addr = tcg_temp_new();
23803bbe 547 TCGv_i64 zero = tcg_constant_i64(0);
8b453a2b
TS
548
549 tcg_gen_andi_tl(addr, hex_dczero_addr, ~0x1f);
550 tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
551 tcg_gen_addi_tl(addr, addr, 8);
552 tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
553 tcg_gen_addi_tl(addr, addr, 8);
554 tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
555 tcg_gen_addi_tl(addr, addr, 8);
556 tcg_gen_qemu_st64(zero, addr, ctx->mem_idx);
557
558 tcg_temp_free(addr);
8b453a2b
TS
559 }
560}
561
a82dd548
TS
562static bool pkt_has_hvx_store(Packet *pkt)
563{
564 int i;
565 for (i = 0; i < pkt->num_insns; i++) {
566 int opcode = pkt->insn[i].opcode;
567 if (GET_ATTRIB(opcode, A_CVI) && GET_ATTRIB(opcode, A_STORE)) {
568 return true;
569 }
570 }
571 return false;
572}
573
1e536334 574static void gen_commit_hvx(DisasContext *ctx)
a82dd548
TS
575{
576 int i;
577
578 /*
579 * for (i = 0; i < ctx->vreg_log_idx; i++) {
580 * int rnum = ctx->vreg_log[i];
581 * if (ctx->vreg_is_predicated[i]) {
582 * if (env->VRegs_updated & (1 << rnum)) {
583 * env->VRegs[rnum] = env->future_VRegs[rnum];
584 * }
585 * } else {
586 * env->VRegs[rnum] = env->future_VRegs[rnum];
587 * }
588 * }
589 */
590 for (i = 0; i < ctx->vreg_log_idx; i++) {
591 int rnum = ctx->vreg_log[i];
592 bool is_predicated = ctx->vreg_is_predicated[i];
593 intptr_t dstoff = offsetof(CPUHexagonState, VRegs[rnum]);
594 intptr_t srcoff = ctx_future_vreg_off(ctx, rnum, 1, false);
595 size_t size = sizeof(MMVector);
596
597 if (is_predicated) {
598 TCGv cmp = tcg_temp_new();
599 TCGLabel *label_skip = gen_new_label();
600
601 tcg_gen_andi_tl(cmp, hex_VRegs_updated, 1 << rnum);
602 tcg_gen_brcondi_tl(TCG_COND_EQ, cmp, 0, label_skip);
603 tcg_temp_free(cmp);
604 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
605 gen_set_label(label_skip);
606 } else {
607 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
608 }
609 }
610
611 /*
612 * for (i = 0; i < ctx->qreg_log_idx; i++) {
613 * int rnum = ctx->qreg_log[i];
614 * if (ctx->qreg_is_predicated[i]) {
615 * if (env->QRegs_updated) & (1 << rnum)) {
616 * env->QRegs[rnum] = env->future_QRegs[rnum];
617 * }
618 * } else {
619 * env->QRegs[rnum] = env->future_QRegs[rnum];
620 * }
621 * }
622 */
623 for (i = 0; i < ctx->qreg_log_idx; i++) {
624 int rnum = ctx->qreg_log[i];
625 bool is_predicated = ctx->qreg_is_predicated[i];
626 intptr_t dstoff = offsetof(CPUHexagonState, QRegs[rnum]);
627 intptr_t srcoff = offsetof(CPUHexagonState, future_QRegs[rnum]);
628 size_t size = sizeof(MMQReg);
629
630 if (is_predicated) {
631 TCGv cmp = tcg_temp_new();
632 TCGLabel *label_skip = gen_new_label();
633
634 tcg_gen_andi_tl(cmp, hex_QRegs_updated, 1 << rnum);
635 tcg_gen_brcondi_tl(TCG_COND_EQ, cmp, 0, label_skip);
636 tcg_temp_free(cmp);
637 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
638 gen_set_label(label_skip);
639 } else {
640 tcg_gen_gvec_mov(MO_64, dstoff, srcoff, size, size);
641 }
642 }
643
1e536334 644 if (pkt_has_hvx_store(ctx->pkt)) {
a82dd548
TS
645 gen_helper_commit_hvx_stores(cpu_env);
646 }
647}
648
1e536334 649static void update_exec_counters(DisasContext *ctx)
8b453a2b 650{
1e536334 651 Packet *pkt = ctx->pkt;
8b453a2b
TS
652 int num_insns = pkt->num_insns;
653 int num_real_insns = 0;
a82dd548 654 int num_hvx_insns = 0;
8b453a2b
TS
655
656 for (int i = 0; i < num_insns; i++) {
657 if (!pkt->insn[i].is_endloop &&
658 !pkt->insn[i].part1 &&
659 !GET_ATTRIB(pkt->insn[i].opcode, A_IT_NOP)) {
660 num_real_insns++;
661 }
a82dd548
TS
662 if (GET_ATTRIB(pkt->insn[i].opcode, A_CVI)) {
663 num_hvx_insns++;
664 }
8b453a2b
TS
665 }
666
667 ctx->num_packets++;
668 ctx->num_insns += num_real_insns;
a82dd548 669 ctx->num_hvx_insns += num_hvx_insns;
8b453a2b
TS
670}
671
1e536334 672static void gen_commit_packet(DisasContext *ctx)
8b453a2b 673{
c23b5764
TS
674 /*
675 * If there is more than one store in a packet, make sure they are all OK
676 * before proceeding with the rest of the packet commit.
677 *
678 * dczeroa has to be the only store operation in the packet, so we go
679 * ahead and process that first.
680 *
a82dd548
TS
681 * When there is an HVX store, there can also be a scalar store in either
682 * slot 0 or slot1, so we create a mask for the helper to indicate what
683 * work to do.
684 *
c23b5764
TS
685 * When there are two scalar stores, we probe the one in slot 0.
686 *
687 * Note that we don't call the probe helper for packets with only one
688 * store. Therefore, we call process_store_log before anything else
689 * involved in committing the packet.
690 */
1e536334 691 Packet *pkt = ctx->pkt;
c23b5764
TS
692 bool has_store_s0 = pkt->pkt_has_store_s0;
693 bool has_store_s1 = (pkt->pkt_has_store_s1 && !ctx->s1_store_processed);
a82dd548 694 bool has_hvx_store = pkt_has_hvx_store(pkt);
c23b5764
TS
695 if (pkt->pkt_has_dczeroa) {
696 /*
697 * The dczeroa will be the store in slot 0, check that we don't have
a82dd548 698 * a store in slot 1 or an HVX store.
c23b5764 699 */
e2be9a5c 700 g_assert(!has_store_s1 && !has_hvx_store);
1e536334 701 process_dczeroa(ctx);
a82dd548
TS
702 } else if (has_hvx_store) {
703 TCGv mem_idx = tcg_constant_tl(ctx->mem_idx);
704
705 if (!has_store_s0 && !has_store_s1) {
706 gen_helper_probe_hvx_stores(cpu_env, mem_idx);
707 } else {
708 int mask = 0;
709 TCGv mask_tcgv;
710
711 if (has_store_s0) {
712 mask |= (1 << 0);
713 }
714 if (has_store_s1) {
715 mask |= (1 << 1);
716 }
717 if (has_hvx_store) {
718 mask |= (1 << 2);
719 }
720 mask_tcgv = tcg_constant_tl(mask);
721 gen_helper_probe_pkt_scalar_hvx_stores(cpu_env, mask_tcgv, mem_idx);
722 }
c23b5764
TS
723 } else if (has_store_s0 && has_store_s1) {
724 /*
725 * process_store_log will execute the slot 1 store first,
726 * so we only have to probe the store in slot 0
727 */
f448397a 728 TCGv mem_idx = tcg_constant_tl(ctx->mem_idx);
c23b5764 729 gen_helper_probe_pkt_scalar_store_s0(cpu_env, mem_idx);
c23b5764
TS
730 }
731
1e536334 732 process_store_log(ctx);
c23b5764 733
8b453a2b 734 gen_reg_writes(ctx);
1e536334 735 gen_pred_writes(ctx);
a82dd548 736 if (pkt->pkt_has_hvx) {
1e536334 737 gen_commit_hvx(ctx);
a82dd548 738 }
1e536334 739 update_exec_counters(ctx);
85580a65 740 if (HEX_DEBUG) {
8b453a2b 741 TCGv has_st0 =
23803bbe 742 tcg_constant_tl(pkt->pkt_has_store_s0 && !pkt->pkt_has_dczeroa);
8b453a2b 743 TCGv has_st1 =
23803bbe 744 tcg_constant_tl(pkt->pkt_has_store_s1 && !pkt->pkt_has_dczeroa);
8b453a2b
TS
745
746 /* Handy place to set a breakpoint at the end of execution */
747 gen_helper_debug_commit_end(cpu_env, has_st0, has_st1);
8b453a2b 748 }
8b453a2b 749
a82dd548
TS
750 if (pkt->vhist_insn != NULL) {
751 ctx->pre_commit = false;
1e536334
TS
752 ctx->insn = pkt->vhist_insn;
753 pkt->vhist_insn->generate(ctx);
a82dd548
TS
754 }
755
8b453a2b 756 if (pkt->pkt_has_cof) {
743debbc 757 gen_end_tb(ctx);
8b453a2b
TS
758 }
759}
760
761static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
762{
763 uint32_t words[PACKET_WORDS_MAX];
764 int nwords;
765 Packet pkt;
766 int i;
767
768 nwords = read_packet_words(env, ctx, words);
769 if (!nwords) {
743debbc 770 gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
8b453a2b
TS
771 return;
772 }
773
774 if (decode_packet(nwords, words, &pkt, false) > 0) {
775 HEX_DEBUG_PRINT_PKT(&pkt);
1e536334
TS
776 ctx->pkt = &pkt;
777 gen_start_packet(ctx);
8b453a2b 778 for (i = 0; i < pkt.num_insns; i++) {
1e536334
TS
779 ctx->insn = &pkt.insn[i];
780 gen_insn(ctx);
8b453a2b 781 }
1e536334 782 gen_commit_packet(ctx);
8b453a2b
TS
783 ctx->base.pc_next += pkt.encod_pkt_size_in_bytes;
784 } else {
743debbc 785 gen_exception_end_tb(ctx, HEX_EXCP_INVALID_PACKET);
8b453a2b
TS
786 }
787}
788
789static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
790 CPUState *cs)
791{
792 DisasContext *ctx = container_of(dcbase, DisasContext, base);
793
794 ctx->mem_idx = MMU_USER_IDX;
795 ctx->num_packets = 0;
796 ctx->num_insns = 0;
a82dd548 797 ctx->num_hvx_insns = 0;
8b453a2b
TS
798}
799
800static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
801{
802}
803
804static void hexagon_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
805{
806 DisasContext *ctx = container_of(dcbase, DisasContext, base);
807
808 tcg_gen_insn_start(ctx->base.pc_next);
809}
810
8b453a2b
TS
811static bool pkt_crosses_page(CPUHexagonState *env, DisasContext *ctx)
812{
813 target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
814 bool found_end = false;
815 int nwords;
816
817 for (nwords = 0; !found_end && nwords < PACKET_WORDS_MAX; nwords++) {
818 uint32_t word = cpu_ldl_code(env,
819 ctx->base.pc_next + nwords * sizeof(uint32_t));
820 found_end = is_packet_end(word);
821 }
822 uint32_t next_ptr = ctx->base.pc_next + nwords * sizeof(uint32_t);
823 return found_end && next_ptr - page_start >= TARGET_PAGE_SIZE;
824}
825
826static void hexagon_tr_translate_packet(DisasContextBase *dcbase, CPUState *cpu)
827{
828 DisasContext *ctx = container_of(dcbase, DisasContext, base);
829 CPUHexagonState *env = cpu->env_ptr;
830
831 decode_and_translate_packet(env, ctx);
832
833 if (ctx->base.is_jmp == DISAS_NEXT) {
834 target_ulong page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
835 target_ulong bytes_max = PACKET_WORDS_MAX * sizeof(target_ulong);
836
837 if (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE ||
838 (ctx->base.pc_next - page_start >= TARGET_PAGE_SIZE - bytes_max &&
839 pkt_crosses_page(env, ctx))) {
840 ctx->base.is_jmp = DISAS_TOO_MANY;
841 }
842
843 /*
844 * The CPU log is used to compare against LLDB single stepping,
845 * so end the TLB after every packet.
846 */
7d9ab202 847 HexagonCPU *hex_cpu = env_archcpu(env);
8b453a2b
TS
848 if (hex_cpu->lldb_compat && qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
849 ctx->base.is_jmp = DISAS_TOO_MANY;
850 }
8b453a2b
TS
851 }
852}
853
854static void hexagon_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
855{
856 DisasContext *ctx = container_of(dcbase, DisasContext, base);
857
858 switch (ctx->base.is_jmp) {
859 case DISAS_TOO_MANY:
860 gen_exec_counters(ctx);
861 tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->base.pc_next);
db07bd02 862 tcg_gen_exit_tb(NULL, 0);
8b453a2b
TS
863 break;
864 case DISAS_NORETURN:
8b453a2b
TS
865 break;
866 default:
867 g_assert_not_reached();
868 }
869}
870
8eb806a7
RH
871static void hexagon_tr_disas_log(const DisasContextBase *dcbase,
872 CPUState *cpu, FILE *logfile)
8b453a2b 873{
8eb806a7
RH
874 fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
875 target_disas(logfile, cpu, dcbase->pc_first, dcbase->tb->size);
8b453a2b
TS
876}
877
878
879static const TranslatorOps hexagon_tr_ops = {
880 .init_disas_context = hexagon_tr_init_disas_context,
881 .tb_start = hexagon_tr_tb_start,
882 .insn_start = hexagon_tr_insn_start,
8b453a2b
TS
883 .translate_insn = hexagon_tr_translate_packet,
884 .tb_stop = hexagon_tr_tb_stop,
885 .disas_log = hexagon_tr_disas_log,
886};
887
306c8721
RH
888void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns,
889 target_ulong pc, void *host_pc)
8b453a2b
TS
890{
891 DisasContext ctx;
892
306c8721
RH
893 translator_loop(cs, tb, max_insns, pc, host_pc,
894 &hexagon_tr_ops, &ctx.base);
8b453a2b
TS
895}
896
897#define NAME_LEN 64
898static char new_value_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
8b453a2b 899static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN];
8b453a2b
TS
900static char new_pred_value_names[NUM_PREGS][NAME_LEN];
901static char store_addr_names[STORES_MAX][NAME_LEN];
902static char store_width_names[STORES_MAX][NAME_LEN];
903static char store_val32_names[STORES_MAX][NAME_LEN];
904static char store_val64_names[STORES_MAX][NAME_LEN];
a82dd548
TS
905static char vstore_addr_names[VSTORES_MAX][NAME_LEN];
906static char vstore_size_names[VSTORES_MAX][NAME_LEN];
907static char vstore_pending_names[VSTORES_MAX][NAME_LEN];
8b453a2b
TS
908
909void hexagon_translate_init(void)
910{
911 int i;
912
913 opcode_init();
914
8b453a2b
TS
915 for (i = 0; i < TOTAL_PER_THREAD_REGS; i++) {
916 hex_gpr[i] = tcg_global_mem_new(cpu_env,
917 offsetof(CPUHexagonState, gpr[i]),
918 hexagon_regnames[i]);
919
920 snprintf(new_value_names[i], NAME_LEN, "new_%s", hexagon_regnames[i]);
921 hex_new_value[i] = tcg_global_mem_new(cpu_env,
922 offsetof(CPUHexagonState, new_value[i]),
923 new_value_names[i]);
924
85580a65
TS
925 if (HEX_DEBUG) {
926 snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s",
927 hexagon_regnames[i]);
928 hex_reg_written[i] = tcg_global_mem_new(cpu_env,
929 offsetof(CPUHexagonState, reg_written[i]),
930 reg_written_names[i]);
931 }
8b453a2b
TS
932 }
933 for (i = 0; i < NUM_PREGS; i++) {
934 hex_pred[i] = tcg_global_mem_new(cpu_env,
935 offsetof(CPUHexagonState, pred[i]),
936 hexagon_prednames[i]);
937
938 snprintf(new_pred_value_names[i], NAME_LEN, "new_pred_%s",
939 hexagon_prednames[i]);
940 hex_new_pred_value[i] = tcg_global_mem_new(cpu_env,
941 offsetof(CPUHexagonState, new_pred_value[i]),
942 new_pred_value_names[i]);
943 }
944 hex_pred_written = tcg_global_mem_new(cpu_env,
945 offsetof(CPUHexagonState, pred_written), "pred_written");
946 hex_next_PC = tcg_global_mem_new(cpu_env,
947 offsetof(CPUHexagonState, next_PC), "next_PC");
948 hex_this_PC = tcg_global_mem_new(cpu_env,
949 offsetof(CPUHexagonState, this_PC), "this_PC");
950 hex_slot_cancelled = tcg_global_mem_new(cpu_env,
951 offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled");
952 hex_branch_taken = tcg_global_mem_new(cpu_env,
953 offsetof(CPUHexagonState, branch_taken), "branch_taken");
954 hex_pkt_has_store_s1 = tcg_global_mem_new(cpu_env,
955 offsetof(CPUHexagonState, pkt_has_store_s1), "pkt_has_store_s1");
956 hex_dczero_addr = tcg_global_mem_new(cpu_env,
957 offsetof(CPUHexagonState, dczero_addr), "dczero_addr");
958 hex_llsc_addr = tcg_global_mem_new(cpu_env,
959 offsetof(CPUHexagonState, llsc_addr), "llsc_addr");
960 hex_llsc_val = tcg_global_mem_new(cpu_env,
961 offsetof(CPUHexagonState, llsc_val), "llsc_val");
962 hex_llsc_val_i64 = tcg_global_mem_new_i64(cpu_env,
963 offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
a82dd548
TS
964 hex_VRegs_updated = tcg_global_mem_new(cpu_env,
965 offsetof(CPUHexagonState, VRegs_updated), "VRegs_updated");
966 hex_QRegs_updated = tcg_global_mem_new(cpu_env,
967 offsetof(CPUHexagonState, QRegs_updated), "QRegs_updated");
8b453a2b
TS
968 for (i = 0; i < STORES_MAX; i++) {
969 snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
970 hex_store_addr[i] = tcg_global_mem_new(cpu_env,
971 offsetof(CPUHexagonState, mem_log_stores[i].va),
972 store_addr_names[i]);
973
974 snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i);
975 hex_store_width[i] = tcg_global_mem_new(cpu_env,
976 offsetof(CPUHexagonState, mem_log_stores[i].width),
977 store_width_names[i]);
978
979 snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i);
980 hex_store_val32[i] = tcg_global_mem_new(cpu_env,
981 offsetof(CPUHexagonState, mem_log_stores[i].data32),
982 store_val32_names[i]);
983
984 snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i);
985 hex_store_val64[i] = tcg_global_mem_new_i64(cpu_env,
986 offsetof(CPUHexagonState, mem_log_stores[i].data64),
987 store_val64_names[i]);
988 }
a82dd548
TS
989 for (int i = 0; i < VSTORES_MAX; i++) {
990 snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i);
991 hex_vstore_addr[i] = tcg_global_mem_new(cpu_env,
992 offsetof(CPUHexagonState, vstore[i].va),
993 vstore_addr_names[i]);
994
995 snprintf(vstore_size_names[i], NAME_LEN, "vstore_size_%d", i);
996 hex_vstore_size[i] = tcg_global_mem_new(cpu_env,
997 offsetof(CPUHexagonState, vstore[i].size),
998 vstore_size_names[i]);
999
1000 snprintf(vstore_pending_names[i], NAME_LEN, "vstore_pending_%d", i);
1001 hex_vstore_pending[i] = tcg_global_mem_new(cpu_env,
1002 offsetof(CPUHexagonState, vstore_pending[i]),
1003 vstore_pending_names[i]);
1004 }
8b453a2b 1005}