blob: a40210ca1e55dedc063cdf1ca1c0c8cb20b89fc9 [file] [log] [blame]
Taylor Simpson66d29a52021-02-07 23:46:03 -06001/*
Matheus Tavares Bernardino14edcf12023-05-08 10:37:23 -03002 * Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
Taylor Simpson66d29a52021-02-07 23:46:03 -06003 *
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#include "qemu/osdep.h"
Taylor Simpson66d29a52021-02-07 23:46:03 -060019#include "iclass.h"
20#include "attribs.h"
21#include "genptr.h"
22#include "decode.h"
23#include "insn.h"
24#include "printinsn.h"
Taylor Simpson60d11802021-02-07 12:42:57 -060025#include "mmvec/decode_ext_mmvec.h"
Taylor Simpson66d29a52021-02-07 23:46:03 -060026
27#define fZXTN(N, M, VAL) ((VAL) & ((1LL << (N)) - 1))
28
29enum {
30 EXT_IDX_noext = 0,
31 EXT_IDX_noext_AFTER = 4,
32 EXT_IDX_mmvec = 4,
33 EXT_IDX_mmvec_AFTER = 8,
34 XX_LAST_EXT_IDX
35};
36
37/*
38 * Certain operand types represent a non-contiguous set of values.
39 * For example, the compound compare-and-jump instruction can only access
40 * registers R0-R7 and R16-23.
41 * This table represents the mapping from the encoding to the actual values.
42 */
43
44#define DEF_REGMAP(NAME, ELEMENTS, ...) \
45 static const unsigned int DECODE_REGISTER_##NAME[ELEMENTS] = \
46 { __VA_ARGS__ };
47 /* Name Num Table */
48DEF_REGMAP(R_16, 16, 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23)
49DEF_REGMAP(R__8, 8, 0, 2, 4, 6, 16, 18, 20, 22)
Taylor Simpson61c9aab2020-01-23 13:03:33 -060050DEF_REGMAP(R_8, 8, 0, 1, 2, 3, 4, 5, 6, 7)
Taylor Simpson66d29a52021-02-07 23:46:03 -060051
Taylor Simpson1de468b2021-03-14 23:54:09 -050052#define DECODE_MAPPED_REG(OPNUM, NAME) \
53 insn->regno[OPNUM] = DECODE_REGISTER_##NAME[insn->regno[OPNUM]];
Taylor Simpson66d29a52021-02-07 23:46:03 -060054
Taylor Simpson1547a2d2024-01-15 15:14:41 -070055/* Helper functions for decode_*_generated.c.inc */
56#define DECODE_MAPPED(NAME) \
57static int decode_mapped_reg_##NAME(DisasContext *ctx, int x) \
58{ \
59 return DECODE_REGISTER_##NAME[x]; \
60}
61DECODE_MAPPED(R_16)
62DECODE_MAPPED(R_8)
Taylor Simpsonf6c01002024-01-15 15:14:42 -070063DECODE_MAPPED(R__8)
Taylor Simpson1547a2d2024-01-15 15:14:41 -070064
65/* Helper function for decodetree_trans_funcs_generated.c.inc */
66static int shift_left(DisasContext *ctx, int x, int n, int immno)
67{
68 int ret = x;
69 Insn *insn = ctx->insn;
70 if (!insn->extension_valid ||
71 insn->which_extended != immno) {
72 ret <<= n;
73 }
74 return ret;
75}
76
77/* Include the generated decoder for 32 bit insn */
78#include "decode_normal_generated.c.inc"
79#include "decode_hvx_generated.c.inc"
80
Taylor Simpsonf6c01002024-01-15 15:14:42 -070081/* Include the generated decoder for 16 bit insn */
82#include "decode_subinsn_a_generated.c.inc"
83#include "decode_subinsn_l1_generated.c.inc"
84#include "decode_subinsn_l2_generated.c.inc"
85#include "decode_subinsn_s1_generated.c.inc"
86#include "decode_subinsn_s2_generated.c.inc"
87
Taylor Simpson1547a2d2024-01-15 15:14:41 -070088/* Include the generated helpers for the decoder */
89#include "decodetree_trans_funcs_generated.c.inc"
90
Taylor Simpson66d29a52021-02-07 23:46:03 -060091void decode_send_insn_to(Packet *packet, int start, int newloc)
92{
93 Insn tmpinsn;
94 int direction;
95 int i;
96 if (start == newloc) {
97 return;
98 }
99 if (start < newloc) {
100 /* Move towards end */
101 direction = 1;
102 } else {
103 /* move towards beginning */
104 direction = -1;
105 }
106 for (i = start; i != newloc; i += direction) {
107 tmpinsn = packet->insn[i];
108 packet->insn[i] = packet->insn[i + direction];
109 packet->insn[i + direction] = tmpinsn;
110 }
111}
112
113/* Fill newvalue registers with the correct regno */
114static void
115decode_fill_newvalue_regno(Packet *packet)
116{
117 int i, use_regidx, offset, def_idx, dst_idx;
118 uint16_t def_opcode, use_opcode;
119 char *dststr;
120
121 for (i = 1; i < packet->num_insns; i++) {
122 if (GET_ATTRIB(packet->insn[i].opcode, A_DOTNEWVALUE) &&
123 !GET_ATTRIB(packet->insn[i].opcode, A_EXTENSION)) {
124 use_opcode = packet->insn[i].opcode;
125
126 /* It's a store, so we're adjusting the Nt field */
127 if (GET_ATTRIB(use_opcode, A_STORE)) {
128 use_regidx = strchr(opcode_reginfo[use_opcode], 't') -
129 opcode_reginfo[use_opcode];
130 } else { /* It's a Jump, so we're adjusting the Ns field */
131 use_regidx = strchr(opcode_reginfo[use_opcode], 's') -
132 opcode_reginfo[use_opcode];
133 }
134
135 /*
136 * What's encoded at the N-field is the offset to who's producing
137 * the value. Shift off the LSB which indicates odd/even register,
138 * then walk backwards and skip over the constant extenders.
139 */
140 offset = packet->insn[i].regno[use_regidx] >> 1;
141 def_idx = i - offset;
142 for (int j = 0; j < offset; j++) {
143 if (GET_ATTRIB(packet->insn[i - j - 1].opcode, A_IT_EXTENDER)) {
144 def_idx--;
145 }
146 }
147
148 /*
149 * Check for a badly encoded N-field which points to an instruction
150 * out-of-range
151 */
152 g_assert(!((def_idx < 0) || (def_idx > (packet->num_insns - 1))));
153
154 /*
155 * packet->insn[def_idx] is the producer
156 * Figure out which type of destination it produces
157 * and the corresponding index in the reginfo
158 */
159 def_opcode = packet->insn[def_idx].opcode;
160 dststr = strstr(opcode_wregs[def_opcode], "Rd");
161 if (dststr) {
162 dststr = strchr(opcode_reginfo[def_opcode], 'd');
163 } else {
164 dststr = strstr(opcode_wregs[def_opcode], "Rx");
165 if (dststr) {
166 dststr = strchr(opcode_reginfo[def_opcode], 'x');
167 } else {
168 dststr = strstr(opcode_wregs[def_opcode], "Re");
169 if (dststr) {
170 dststr = strchr(opcode_reginfo[def_opcode], 'e');
171 } else {
172 dststr = strstr(opcode_wregs[def_opcode], "Ry");
173 if (dststr) {
174 dststr = strchr(opcode_reginfo[def_opcode], 'y');
175 } else {
176 g_assert_not_reached();
177 }
178 }
179 }
180 }
181 g_assert(dststr != NULL);
182
183 /* Now patch up the consumer with the register number */
184 dst_idx = dststr - opcode_reginfo[def_opcode];
185 packet->insn[i].regno[use_regidx] =
186 packet->insn[def_idx].regno[dst_idx];
187 /*
188 * We need to remember who produces this value to later
189 * check if it was dynamically cancelled
190 */
191 packet->insn[i].new_value_producer_slot =
192 packet->insn[def_idx].slot;
193 }
194 }
195}
196
197/* Split CJ into a compare and a jump */
198static void decode_split_cmpjump(Packet *pkt)
199{
200 int last, i;
201 int numinsns = pkt->num_insns;
202
203 /*
204 * First, split all compare-jumps.
205 * The compare is sent to the end as a new instruction.
206 * Do it this way so we don't reorder dual jumps. Those need to stay in
207 * original order.
208 */
209 for (i = 0; i < numinsns; i++) {
210 /* It's a cmp-jump */
211 if (GET_ATTRIB(pkt->insn[i].opcode, A_NEWCMPJUMP)) {
212 last = pkt->num_insns;
213 pkt->insn[last] = pkt->insn[i]; /* copy the instruction */
Taylor Simpson92cfa252021-04-08 20:07:35 -0500214 pkt->insn[last].part1 = true; /* last insn does the CMP */
215 pkt->insn[i].part1 = false; /* existing insn does the JUMP */
Taylor Simpson66d29a52021-02-07 23:46:03 -0600216 pkt->num_insns++;
217 }
218 }
219
220 /* Now re-shuffle all the compares back to the beginning */
221 for (i = 0; i < pkt->num_insns; i++) {
222 if (pkt->insn[i].part1) {
223 decode_send_insn_to(pkt, i, 0);
224 }
225 }
226}
227
Taylor Simpson92cfa252021-04-08 20:07:35 -0500228static bool decode_opcode_can_jump(int opcode)
Taylor Simpson66d29a52021-02-07 23:46:03 -0600229{
230 if ((GET_ATTRIB(opcode, A_JUMP)) ||
231 (GET_ATTRIB(opcode, A_CALL)) ||
232 (opcode == J2_trap0) ||
233 (opcode == J2_pause)) {
234 /* Exception to A_JUMP attribute */
235 if (opcode == J4_hintjumpr) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500236 return false;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600237 }
Taylor Simpson92cfa252021-04-08 20:07:35 -0500238 return true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600239 }
240
Taylor Simpson92cfa252021-04-08 20:07:35 -0500241 return false;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600242}
243
Taylor Simpson92cfa252021-04-08 20:07:35 -0500244static bool decode_opcode_ends_loop(int opcode)
Taylor Simpson66d29a52021-02-07 23:46:03 -0600245{
246 return GET_ATTRIB(opcode, A_HWLOOP0_END) ||
247 GET_ATTRIB(opcode, A_HWLOOP1_END);
248}
249
250/* Set the is_* fields in each instruction */
251static void decode_set_insn_attr_fields(Packet *pkt)
252{
253 int i;
254 int numinsns = pkt->num_insns;
255 uint16_t opcode;
256
Taylor Simpson92cfa252021-04-08 20:07:35 -0500257 pkt->pkt_has_cof = false;
Taylor Simpsonfb67c2b2022-11-08 08:28:59 -0800258 pkt->pkt_has_multi_cof = false;
Taylor Simpson92cfa252021-04-08 20:07:35 -0500259 pkt->pkt_has_endloop = false;
260 pkt->pkt_has_dczeroa = false;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600261
262 for (i = 0; i < numinsns; i++) {
263 opcode = pkt->insn[i].opcode;
264 if (pkt->insn[i].part1) {
265 continue; /* Skip compare of cmp-jumps */
266 }
267
268 if (GET_ATTRIB(opcode, A_DCZEROA)) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500269 pkt->pkt_has_dczeroa = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600270 }
271
272 if (GET_ATTRIB(opcode, A_STORE)) {
Taylor Simpsone2be9a52022-09-20 01:07:46 -0700273 if (GET_ATTRIB(opcode, A_SCALAR_STORE) &&
274 !GET_ATTRIB(opcode, A_MEMSIZE_0B)) {
275 if (pkt->insn[i].slot == 0) {
276 pkt->pkt_has_store_s0 = true;
277 } else {
278 pkt->pkt_has_store_s1 = true;
279 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600280 }
281 }
282
Taylor Simpsonfb67c2b2022-11-08 08:28:59 -0800283 if (decode_opcode_can_jump(opcode)) {
284 if (pkt->pkt_has_cof) {
285 pkt->pkt_has_multi_cof = true;
286 }
287 pkt->pkt_has_cof = true;
288 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600289
290 pkt->insn[i].is_endloop = decode_opcode_ends_loop(opcode);
291
292 pkt->pkt_has_endloop |= pkt->insn[i].is_endloop;
293
Taylor Simpsonfb67c2b2022-11-08 08:28:59 -0800294 if (pkt->pkt_has_endloop) {
295 if (pkt->pkt_has_cof) {
296 pkt->pkt_has_multi_cof = true;
297 }
298 pkt->pkt_has_cof = true;
299 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600300 }
301}
302
303/*
304 * Shuffle for execution
305 * Move stores to end (in same order as encoding)
306 * Move compares to beginning (for use by .new insns)
307 */
308static void decode_shuffle_for_execution(Packet *packet)
309{
Taylor Simpson92cfa252021-04-08 20:07:35 -0500310 bool changed = false;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600311 int i;
Taylor Simpson92cfa252021-04-08 20:07:35 -0500312 bool flag; /* flag means we've seen a non-memory instruction */
Taylor Simpson66d29a52021-02-07 23:46:03 -0600313 int n_mems;
314 int last_insn = packet->num_insns - 1;
315
316 /*
317 * Skip end loops, somehow an end loop is getting in and messing
318 * up the order
319 */
320 if (decode_opcode_ends_loop(packet->insn[last_insn].opcode)) {
321 last_insn--;
322 }
323
324 do {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500325 changed = false;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600326 /*
327 * Stores go last, must not reorder.
328 * Cannot shuffle stores past loads, either.
329 * Iterate backwards. If we see a non-memory instruction,
330 * then a store, shuffle the store to the front. Don't shuffle
331 * stores wrt each other or a load.
332 */
Taylor Simpson92cfa252021-04-08 20:07:35 -0500333 for (flag = false, n_mems = 0, i = last_insn; i >= 0; i--) {
Taylor Simpson66d29a52021-02-07 23:46:03 -0600334 int opcode = packet->insn[i].opcode;
335
336 if (flag && GET_ATTRIB(opcode, A_STORE)) {
337 decode_send_insn_to(packet, i, last_insn - n_mems);
338 n_mems++;
Taylor Simpson92cfa252021-04-08 20:07:35 -0500339 changed = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600340 } else if (GET_ATTRIB(opcode, A_STORE)) {
341 n_mems++;
342 } else if (GET_ATTRIB(opcode, A_LOAD)) {
343 /*
344 * Don't set flag, since we don't want to shuffle a
345 * store past a load
346 */
347 n_mems++;
348 } else if (GET_ATTRIB(opcode, A_DOTNEWVALUE)) {
349 /*
350 * Don't set flag, since we don't want to shuffle past
351 * a .new value
352 */
353 } else {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500354 flag = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600355 }
356 }
357
358 if (changed) {
359 continue;
360 }
361 /* Compares go first, may be reordered wrt each other */
Taylor Simpson92cfa252021-04-08 20:07:35 -0500362 for (flag = false, i = 0; i < last_insn + 1; i++) {
Taylor Simpson66d29a52021-02-07 23:46:03 -0600363 int opcode = packet->insn[i].opcode;
364
365 if ((strstr(opcode_wregs[opcode], "Pd4") ||
366 strstr(opcode_wregs[opcode], "Pe4")) &&
367 GET_ATTRIB(opcode, A_STORE) == 0) {
368 /* This should be a compare (not a store conditional) */
369 if (flag) {
370 decode_send_insn_to(packet, i, 0);
Taylor Simpson92cfa252021-04-08 20:07:35 -0500371 changed = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600372 continue;
373 }
374 } else if (GET_ATTRIB(opcode, A_IMPLICIT_WRITES_P3) &&
375 !decode_opcode_ends_loop(packet->insn[i].opcode)) {
376 /*
377 * spNloop instruction
378 * Don't reorder endloops; they are not valid for .new uses,
379 * and we want to match HW
380 */
381 if (flag) {
382 decode_send_insn_to(packet, i, 0);
Taylor Simpson92cfa252021-04-08 20:07:35 -0500383 changed = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600384 continue;
385 }
386 } else if (GET_ATTRIB(opcode, A_IMPLICIT_WRITES_P0) &&
387 !GET_ATTRIB(opcode, A_NEWCMPJUMP)) {
388 if (flag) {
389 decode_send_insn_to(packet, i, 0);
Taylor Simpson92cfa252021-04-08 20:07:35 -0500390 changed = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600391 continue;
392 }
393 } else {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500394 flag = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600395 }
396 }
397 if (changed) {
398 continue;
399 }
400 } while (changed);
401
402 /*
403 * If we have a .new register compare/branch, move that to the very
404 * very end, past stores
405 */
406 for (i = 0; i < last_insn; i++) {
407 if (GET_ATTRIB(packet->insn[i].opcode, A_DOTNEWVALUE)) {
408 decode_send_insn_to(packet, i, last_insn);
409 break;
410 }
411 }
412}
413
414static void
415apply_extender(Packet *pkt, int i, uint32_t extender)
416{
417 int immed_num;
418 uint32_t base_immed;
419
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700420 immed_num = pkt->insn[i].which_extended;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600421 base_immed = pkt->insn[i].immed[immed_num];
422
423 pkt->insn[i].immed[immed_num] = extender | fZXTN(6, 32, base_immed);
424}
425
426static void decode_apply_extenders(Packet *packet)
427{
428 int i;
429 for (i = 0; i < packet->num_insns; i++) {
430 if (GET_ATTRIB(packet->insn[i].opcode, A_IT_EXTENDER)) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500431 packet->insn[i + 1].extension_valid = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600432 apply_extender(packet, i + 1, packet->insn[i].immed[0]);
433 }
434 }
435}
436
437static void decode_remove_extenders(Packet *packet)
438{
439 int i, j;
440 for (i = 0; i < packet->num_insns; i++) {
441 if (GET_ATTRIB(packet->insn[i].opcode, A_IT_EXTENDER)) {
442 /* Remove this one by moving the remaining instructions down */
443 for (j = i;
444 (j < packet->num_insns - 1) && (j < INSTRUCTIONS_MAX - 1);
445 j++) {
446 packet->insn[j] = packet->insn[j + 1];
447 }
448 packet->num_insns--;
449 }
450 }
451}
452
453static SlotMask get_valid_slots(const Packet *pkt, unsigned int slot)
454{
Taylor Simpson60d11802021-02-07 12:42:57 -0600455 if (GET_ATTRIB(pkt->insn[slot].opcode, A_EXTENSION)) {
456 return mmvec_ext_decode_find_iclass_slots(pkt->insn[slot].opcode);
457 } else {
458 return find_iclass_slots(pkt->insn[slot].opcode,
459 pkt->insn[slot].iclass);
460 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600461}
462
Taylor Simpsonf6c01002024-01-15 15:14:42 -0700463/*
464 * Section 10.3 of the Hexagon V73 Programmer's Reference Manual
465 *
466 * A duplex is encoded as a 32-bit instruction with bits [15:14] set to 00.
467 * The sub-instructions that comprise a duplex are encoded as 13-bit fields
468 * in the duplex.
469 *
470 * Per table 10-4, the 4-bit duplex iclass is encoded in bits 31:29, 13
471 */
472static uint32_t get_duplex_iclass(uint32_t encoding)
473{
474 uint32_t iclass = extract32(encoding, 13, 1);
475 iclass = deposit32(iclass, 1, 3, extract32(encoding, 29, 3));
476 return iclass;
477}
478
479/*
480 * Per table 10-5, the duplex ICLASS field values that specify the group of
481 * each sub-instruction in a duplex
482 *
483 * This table points to the decode instruction for each entry in the table
484 */
485typedef bool (*subinsn_decode_func)(DisasContext *ctx, uint16_t insn);
486typedef struct {
487 subinsn_decode_func decode_slot0_subinsn;
488 subinsn_decode_func decode_slot1_subinsn;
489} subinsn_decode_groups;
490
491static const subinsn_decode_groups decode_groups[16] = {
492 [0x0] = { decode_subinsn_l1, decode_subinsn_l1 },
493 [0x1] = { decode_subinsn_l2, decode_subinsn_l1 },
494 [0x2] = { decode_subinsn_l2, decode_subinsn_l2 },
495 [0x3] = { decode_subinsn_a, decode_subinsn_a },
496 [0x4] = { decode_subinsn_l1, decode_subinsn_a },
497 [0x5] = { decode_subinsn_l2, decode_subinsn_a },
498 [0x6] = { decode_subinsn_s1, decode_subinsn_a },
499 [0x7] = { decode_subinsn_s2, decode_subinsn_a },
500 [0x8] = { decode_subinsn_s1, decode_subinsn_l1 },
501 [0x9] = { decode_subinsn_s1, decode_subinsn_l2 },
502 [0xa] = { decode_subinsn_s1, decode_subinsn_s1 },
503 [0xb] = { decode_subinsn_s2, decode_subinsn_s1 },
504 [0xc] = { decode_subinsn_s2, decode_subinsn_l1 },
505 [0xd] = { decode_subinsn_s2, decode_subinsn_l2 },
506 [0xe] = { decode_subinsn_s2, decode_subinsn_s2 },
507 [0xf] = { NULL, NULL }, /* Reserved */
508};
509
510static uint16_t get_slot0_subinsn(uint32_t encoding)
511{
512 return extract32(encoding, 0, 13);
513}
514
515static uint16_t get_slot1_subinsn(uint32_t encoding)
516{
517 return extract32(encoding, 16, 13);
518}
519
Taylor Simpson66d29a52021-02-07 23:46:03 -0600520static unsigned int
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700521decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding)
Taylor Simpson66d29a52021-02-07 23:46:03 -0600522{
Taylor Simpson66d29a52021-02-07 23:46:03 -0600523 if (parse_bits(encoding) != 0) {
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700524 if (decode_normal(ctx, encoding) ||
525 decode_hvx(ctx, encoding)) {
526 insn->generate = opcode_genptr[insn->opcode];
527 insn->iclass = iclass_bits(encoding);
528 return 1;
529 }
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700530 g_assert_not_reached();
Taylor Simpson66d29a52021-02-07 23:46:03 -0600531 } else {
Taylor Simpsonf6c01002024-01-15 15:14:42 -0700532 uint32_t iclass = get_duplex_iclass(encoding);
533 unsigned int slot0_subinsn = get_slot0_subinsn(encoding);
534 unsigned int slot1_subinsn = get_slot1_subinsn(encoding);
535 subinsn_decode_func decode_slot0_subinsn =
536 decode_groups[iclass].decode_slot0_subinsn;
537 subinsn_decode_func decode_slot1_subinsn =
538 decode_groups[iclass].decode_slot1_subinsn;
539
540 /* The slot1 subinsn needs to be in the packet first */
541 if (decode_slot1_subinsn(ctx, slot1_subinsn)) {
542 insn->generate = opcode_genptr[insn->opcode];
543 insn->iclass = iclass_bits(encoding);
544 ctx->insn = ++insn;
545 if (decode_slot0_subinsn(ctx, slot0_subinsn)) {
546 insn->generate = opcode_genptr[insn->opcode];
547 insn->iclass = iclass_bits(encoding);
548 return 2;
549 }
550 }
Taylor Simpsonf6c01002024-01-15 15:14:42 -0700551 g_assert_not_reached();
Taylor Simpson66d29a52021-02-07 23:46:03 -0600552 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600553}
554
555static void decode_add_endloop_insn(Insn *insn, int loopnum)
556{
557 if (loopnum == 10) {
558 insn->opcode = J2_endloop01;
559 insn->generate = opcode_genptr[J2_endloop01];
560 } else if (loopnum == 1) {
561 insn->opcode = J2_endloop1;
562 insn->generate = opcode_genptr[J2_endloop1];
563 } else if (loopnum == 0) {
564 insn->opcode = J2_endloop0;
565 insn->generate = opcode_genptr[J2_endloop0];
566 } else {
567 g_assert_not_reached();
568 }
569}
570
Taylor Simpson92cfa252021-04-08 20:07:35 -0500571static bool decode_parsebits_is_loopend(uint32_t encoding32)
Taylor Simpson66d29a52021-02-07 23:46:03 -0600572{
573 uint32_t bits = parse_bits(encoding32);
574 return bits == 0x2;
575}
576
Matheus Tavares Bernardino14edcf12023-05-08 10:37:23 -0300577static bool has_valid_slot_assignment(Packet *pkt)
578{
579 int used_slots = 0;
580 for (int i = 0; i < pkt->num_insns; i++) {
581 int slot_mask;
582 Insn *insn = &pkt->insn[i];
583 if (decode_opcode_ends_loop(insn->opcode)) {
584 /* We overload slot 0 for endloop. */
585 continue;
586 }
587 slot_mask = 1 << insn->slot;
588 if (used_slots & slot_mask) {
589 return false;
590 }
591 used_slots |= slot_mask;
592 }
593 return true;
594}
595
596static bool
Taylor Simpson66d29a52021-02-07 23:46:03 -0600597decode_set_slot_number(Packet *pkt)
598{
599 int slot;
600 int i;
Taylor Simpson92cfa252021-04-08 20:07:35 -0500601 bool hit_mem_insn = false;
602 bool hit_duplex = false;
603 bool slot0_found = false;
604 bool slot1_found = false;
605 int slot1_iidx = 0;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600606
607 /*
608 * The slots are encoded in reverse order
609 * For each instruction, count down until you find a suitable slot
610 */
611 for (i = 0, slot = 3; i < pkt->num_insns; i++) {
612 SlotMask valid_slots = get_valid_slots(pkt, i);
613
614 while (!(valid_slots & (1 << slot))) {
615 slot--;
616 }
617 pkt->insn[i].slot = slot;
618 if (slot) {
619 /* I've assigned the slot, now decrement it for the next insn */
620 slot--;
621 }
622 }
623
624 /* Fix the exceptions - mem insns to slot 0,1 */
625 for (i = pkt->num_insns - 1; i >= 0; i--) {
626 /* First memory instruction always goes to slot 0 */
627 if ((GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE) ||
628 GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE_PACKET_RULES)) &&
629 !hit_mem_insn) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500630 hit_mem_insn = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600631 pkt->insn[i].slot = 0;
632 continue;
633 }
634
635 /* Next memory instruction always goes to slot 1 */
636 if ((GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE) ||
637 GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE_PACKET_RULES)) &&
638 hit_mem_insn) {
639 pkt->insn[i].slot = 1;
640 }
641 }
642
643 /* Fix the exceptions - duplex always slot 0,1 */
644 for (i = pkt->num_insns - 1; i >= 0; i--) {
645 /* First subinsn always goes to slot 0 */
646 if (GET_ATTRIB(pkt->insn[i].opcode, A_SUBINSN) && !hit_duplex) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500647 hit_duplex = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600648 pkt->insn[i].slot = 0;
649 continue;
650 }
651
652 /* Next subinsn always goes to slot 1 */
653 if (GET_ATTRIB(pkt->insn[i].opcode, A_SUBINSN) && hit_duplex) {
654 pkt->insn[i].slot = 1;
655 }
656 }
657
658 /* Fix the exceptions - slot 1 is never empty, always aligns to slot 0 */
Taylor Simpson66d29a52021-02-07 23:46:03 -0600659 for (i = pkt->num_insns - 1; i >= 0; i--) {
660 /* Is slot0 used? */
661 if (pkt->insn[i].slot == 0) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500662 bool is_endloop = (pkt->insn[i].opcode == J2_endloop01);
Taylor Simpson66d29a52021-02-07 23:46:03 -0600663 is_endloop |= (pkt->insn[i].opcode == J2_endloop0);
664 is_endloop |= (pkt->insn[i].opcode == J2_endloop1);
665
666 /*
667 * Make sure it's not endloop since, we're overloading
668 * slot0 for endloop
669 */
670 if (!is_endloop) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500671 slot0_found = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600672 }
673 }
674 /* Is slot1 used? */
675 if (pkt->insn[i].slot == 1) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500676 slot1_found = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600677 slot1_iidx = i;
678 }
679 }
680 /* Is slot0 empty and slot1 used? */
Taylor Simpson92cfa252021-04-08 20:07:35 -0500681 if ((!slot0_found) && slot1_found) {
Taylor Simpson66d29a52021-02-07 23:46:03 -0600682 /* Then push it to slot0 */
683 pkt->insn[slot1_iidx].slot = 0;
684 }
Matheus Tavares Bernardino14edcf12023-05-08 10:37:23 -0300685
686 return has_valid_slot_assignment(pkt);
Taylor Simpson66d29a52021-02-07 23:46:03 -0600687}
688
689/*
690 * decode_packet
691 * Decodes packet with given words
692 * Returns 0 on insufficient words,
693 * or number of words used on success
694 */
695
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700696int decode_packet(DisasContext *ctx, int max_words, const uint32_t *words,
697 Packet *pkt, bool disas_only)
Taylor Simpson66d29a52021-02-07 23:46:03 -0600698{
699 int num_insns = 0;
700 int words_read = 0;
Taylor Simpson92cfa252021-04-08 20:07:35 -0500701 bool end_of_packet = false;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600702 int new_insns = 0;
Taylor Simpson60d11802021-02-07 12:42:57 -0600703 int i;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600704 uint32_t encoding32;
705
706 /* Initialize */
707 memset(pkt, 0, sizeof(*pkt));
708 /* Try to build packet */
709 while (!end_of_packet && (words_read < max_words)) {
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700710 Insn *insn = &pkt->insn[num_insns];
711 ctx->insn = insn;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600712 encoding32 = words[words_read];
713 end_of_packet = is_packet_end(encoding32);
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700714 new_insns = decode_insns(ctx, insn, encoding32);
Taylor Simpson66d29a52021-02-07 23:46:03 -0600715 g_assert(new_insns > 0);
716 /*
717 * If we saw an extender, mark next word extended so immediate
718 * decode works
719 */
720 if (pkt->insn[num_insns].opcode == A4_ext) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500721 pkt->insn[num_insns + 1].extension_valid = true;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600722 }
723 num_insns += new_insns;
724 words_read++;
725 }
726
727 pkt->num_insns = num_insns;
728 if (!end_of_packet) {
729 /* Ran out of words! */
730 return 0;
731 }
732 pkt->encod_pkt_size_in_bytes = words_read * 4;
Taylor Simpson60d11802021-02-07 12:42:57 -0600733 pkt->pkt_has_hvx = false;
734 for (i = 0; i < num_insns; i++) {
735 pkt->pkt_has_hvx |=
736 GET_ATTRIB(pkt->insn[i].opcode, A_CVI);
737 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600738
739 /*
740 * Check for :endloop in the parse bits
741 * Section 10.6 of the Programmer's Reference describes the encoding
742 * The end of hardware loop 0 can be encoded with 2 words
743 * The end of hardware loop 1 needs 3 words
744 */
745 if ((words_read == 2) && (decode_parsebits_is_loopend(words[0]))) {
746 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 0);
747 }
748 if (words_read >= 3) {
Taylor Simpson92cfa252021-04-08 20:07:35 -0500749 bool has_loop0, has_loop1;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600750 has_loop0 = decode_parsebits_is_loopend(words[0]);
751 has_loop1 = decode_parsebits_is_loopend(words[1]);
752 if (has_loop0 && has_loop1) {
753 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 10);
754 } else if (has_loop1) {
755 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 1);
756 } else if (has_loop0) {
757 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 0);
758 }
759 }
760
761 decode_apply_extenders(pkt);
762 if (!disas_only) {
763 decode_remove_extenders(pkt);
Matheus Tavares Bernardino14edcf12023-05-08 10:37:23 -0300764 if (!decode_set_slot_number(pkt)) {
765 /* Invalid packet */
766 return 0;
767 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600768 }
Taylor Simpson66d29a52021-02-07 23:46:03 -0600769 decode_fill_newvalue_regno(pkt);
770
Taylor Simpson60d11802021-02-07 12:42:57 -0600771 if (pkt->pkt_has_hvx) {
772 mmvec_ext_decode_checks(pkt, disas_only);
773 }
774
Taylor Simpson66d29a52021-02-07 23:46:03 -0600775 if (!disas_only) {
776 decode_shuffle_for_execution(pkt);
777 decode_split_cmpjump(pkt);
778 decode_set_insn_attr_fields(pkt);
779 }
780
781 return words_read;
782}
783
784/* Used for "-d in_asm" logging */
785int disassemble_hexagon(uint32_t *words, int nwords, bfd_vma pc,
786 GString *buf)
787{
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700788 DisasContext ctx;
Taylor Simpson66d29a52021-02-07 23:46:03 -0600789 Packet pkt;
790
Taylor Simpson1547a2d2024-01-15 15:14:41 -0700791 memset(&ctx, 0, sizeof(DisasContext));
792 ctx.pkt = &pkt;
793
794 if (decode_packet(&ctx, nwords, words, &pkt, true) > 0) {
Taylor Simpson66d29a52021-02-07 23:46:03 -0600795 snprint_a_pkt_disas(buf, &pkt, words, pc);
796 return pkt.encod_pkt_size_in_bytes;
797 } else {
798 g_string_assign(buf, "<invalid>");
799 return 0;
800 }
801}