1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "qemu/osdep.h"
19#include "qemu/log.h"
20#include "iclass.h"
21#include "attribs.h"
22#include "genptr.h"
23#include "decode.h"
24#include "insn.h"
25#include "printinsn.h"
26
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
39
40
41
42
43
44#define DEF_REGMAP(NAME, ELEMENTS, ...) \
45 static const unsigned int DECODE_REGISTER_##NAME[ELEMENTS] = \
46 { __VA_ARGS__ };
47
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)
50
51#define DECODE_MAPPED_REG(REGNO, NAME) \
52 insn->regno[REGNO] = DECODE_REGISTER_##NAME[insn->regno[REGNO]];
53
54typedef struct {
55 const struct DectreeTable *table_link;
56 const struct DectreeTable *table_link_b;
57 Opcode opcode;
58 enum {
59 DECTREE_ENTRY_INVALID,
60 DECTREE_TABLE_LINK,
61 DECTREE_SUBINSNS,
62 DECTREE_EXTSPACE,
63 DECTREE_TERMINAL
64 } type;
65} DectreeEntry;
66
67typedef struct DectreeTable {
68 unsigned int (*lookup_function)(int startbit, int width, uint32_t opcode);
69 unsigned int size;
70 unsigned int startbit;
71 unsigned int width;
72 const DectreeEntry table[];
73} DectreeTable;
74
75#define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) \
76 static const DectreeTable dectree_table_##TAG;
77#define TABLE_LINK(TABLE)
78#define TERMINAL(TAG, ENC)
79#define SUBINSNS(TAG, CLASSA, CLASSB, ENC)
80#define EXTSPACE(TAG, ENC)
81#define INVALID()
82#define DECODE_END_TABLE(...)
83#define DECODE_MATCH_INFO(...)
84#define DECODE_LEGACY_MATCH_INFO(...)
85#define DECODE_OPINFO(...)
86
87#include "dectree_generated.h.inc"
88
89#undef DECODE_OPINFO
90#undef DECODE_MATCH_INFO
91#undef DECODE_LEGACY_MATCH_INFO
92#undef DECODE_END_TABLE
93#undef INVALID
94#undef TERMINAL
95#undef SUBINSNS
96#undef EXTSPACE
97#undef TABLE_LINK
98#undef DECODE_NEW_TABLE
99#undef DECODE_SEPARATOR_BITS
100
101#define DECODE_SEPARATOR_BITS(START, WIDTH) NULL, START, WIDTH
102#define DECODE_NEW_TABLE_HELPER(TAG, SIZE, FN, START, WIDTH) \
103 static const DectreeTable dectree_table_##TAG = { \
104 .size = SIZE, \
105 .lookup_function = FN, \
106 .startbit = START, \
107 .width = WIDTH, \
108 .table = {
109#define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) \
110 DECODE_NEW_TABLE_HELPER(TAG, SIZE, WHATNOT)
111
112#define TABLE_LINK(TABLE) \
113 { .type = DECTREE_TABLE_LINK, .table_link = &dectree_table_##TABLE },
114#define TERMINAL(TAG, ENC) \
115 { .type = DECTREE_TERMINAL, .opcode = TAG },
116#define SUBINSNS(TAG, CLASSA, CLASSB, ENC) \
117 { \
118 .type = DECTREE_SUBINSNS, \
119 .table_link = &dectree_table_DECODE_SUBINSN_##CLASSA, \
120 .table_link_b = &dectree_table_DECODE_SUBINSN_##CLASSB \
121 },
122#define EXTSPACE(TAG, ENC) { .type = DECTREE_EXTSPACE },
123#define INVALID() { .type = DECTREE_ENTRY_INVALID, .opcode = XX_LAST_OPCODE },
124
125#define DECODE_END_TABLE(...) } };
126
127#define DECODE_MATCH_INFO(...)
128#define DECODE_LEGACY_MATCH_INFO(...)
129#define DECODE_OPINFO(...)
130
131#include "dectree_generated.h.inc"
132
133#undef DECODE_OPINFO
134#undef DECODE_MATCH_INFO
135#undef DECODE_LEGACY_MATCH_INFO
136#undef DECODE_END_TABLE
137#undef INVALID
138#undef TERMINAL
139#undef SUBINSNS
140#undef EXTSPACE
141#undef TABLE_LINK
142#undef DECODE_NEW_TABLE
143#undef DECODE_NEW_TABLE_HELPER
144#undef DECODE_SEPARATOR_BITS
145
146static const DectreeTable dectree_table_DECODE_EXT_EXT_noext = {
147 .size = 1, .lookup_function = NULL, .startbit = 0, .width = 0,
148 .table = {
149 { .type = DECTREE_ENTRY_INVALID, .opcode = XX_LAST_OPCODE },
150 }
151};
152
153static const DectreeTable *ext_trees[XX_LAST_EXT_IDX];
154
155static void decode_ext_init(void)
156{
157 int i;
158 for (i = EXT_IDX_noext; i < EXT_IDX_noext_AFTER; i++) {
159 ext_trees[i] = &dectree_table_DECODE_EXT_EXT_noext;
160 }
161}
162
163typedef struct {
164 uint32_t mask;
165 uint32_t match;
166} DecodeITableEntry;
167
168#define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT)
169#define TABLE_LINK(TABLE)
170#define TERMINAL(TAG, ENC)
171#define SUBINSNS(TAG, CLASSA, CLASSB, ENC)
172#define EXTSPACE(TAG, ENC)
173#define INVALID()
174#define DECODE_END_TABLE(...)
175#define DECODE_OPINFO(...)
176
177#define DECODE_MATCH_INFO_NORMAL(TAG, MASK, MATCH) \
178 [TAG] = { \
179 .mask = MASK, \
180 .match = MATCH, \
181 },
182
183#define DECODE_MATCH_INFO_NULL(TAG, MASK, MATCH) \
184 [TAG] = { .match = ~0 },
185
186#define DECODE_MATCH_INFO(...) DECODE_MATCH_INFO_NORMAL(__VA_ARGS__)
187#define DECODE_LEGACY_MATCH_INFO(...)
188
189static const DecodeITableEntry decode_itable[XX_LAST_OPCODE] = {
190#include "dectree_generated.h.inc"
191};
192
193#undef DECODE_MATCH_INFO
194#define DECODE_MATCH_INFO(...) DECODE_MATCH_INFO_NULL(__VA_ARGS__)
195
196#undef DECODE_LEGACY_MATCH_INFO
197#define DECODE_LEGACY_MATCH_INFO(...) DECODE_MATCH_INFO_NORMAL(__VA_ARGS__)
198
199static const DecodeITableEntry decode_legacy_itable[XX_LAST_OPCODE] = {
200#include "dectree_generated.h.inc"
201};
202
203#undef DECODE_OPINFO
204#undef DECODE_MATCH_INFO
205#undef DECODE_LEGACY_MATCH_INFO
206#undef DECODE_END_TABLE
207#undef INVALID
208#undef TERMINAL
209#undef SUBINSNS
210#undef EXTSPACE
211#undef TABLE_LINK
212#undef DECODE_NEW_TABLE
213#undef DECODE_SEPARATOR_BITS
214
215void decode_init(void)
216{
217 decode_ext_init();
218}
219
220void decode_send_insn_to(Packet *packet, int start, int newloc)
221{
222 Insn tmpinsn;
223 int direction;
224 int i;
225 if (start == newloc) {
226 return;
227 }
228 if (start < newloc) {
229
230 direction = 1;
231 } else {
232
233 direction = -1;
234 }
235 for (i = start; i != newloc; i += direction) {
236 tmpinsn = packet->insn[i];
237 packet->insn[i] = packet->insn[i + direction];
238 packet->insn[i + direction] = tmpinsn;
239 }
240}
241
242
243static void
244decode_fill_newvalue_regno(Packet *packet)
245{
246 int i, use_regidx, offset, def_idx, dst_idx;
247 uint16_t def_opcode, use_opcode;
248 char *dststr;
249
250 for (i = 1; i < packet->num_insns; i++) {
251 if (GET_ATTRIB(packet->insn[i].opcode, A_DOTNEWVALUE) &&
252 !GET_ATTRIB(packet->insn[i].opcode, A_EXTENSION)) {
253 use_opcode = packet->insn[i].opcode;
254
255
256 if (GET_ATTRIB(use_opcode, A_STORE)) {
257 use_regidx = strchr(opcode_reginfo[use_opcode], 't') -
258 opcode_reginfo[use_opcode];
259 } else {
260 use_regidx = strchr(opcode_reginfo[use_opcode], 's') -
261 opcode_reginfo[use_opcode];
262 }
263
264
265
266
267
268
269 offset = packet->insn[i].regno[use_regidx] >> 1;
270 def_idx = i - offset;
271 for (int j = 0; j < offset; j++) {
272 if (GET_ATTRIB(packet->insn[i - j - 1].opcode, A_IT_EXTENDER)) {
273 def_idx--;
274 }
275 }
276
277
278
279
280
281 g_assert(!((def_idx < 0) || (def_idx > (packet->num_insns - 1))));
282
283
284
285
286
287
288 def_opcode = packet->insn[def_idx].opcode;
289 dststr = strstr(opcode_wregs[def_opcode], "Rd");
290 if (dststr) {
291 dststr = strchr(opcode_reginfo[def_opcode], 'd');
292 } else {
293 dststr = strstr(opcode_wregs[def_opcode], "Rx");
294 if (dststr) {
295 dststr = strchr(opcode_reginfo[def_opcode], 'x');
296 } else {
297 dststr = strstr(opcode_wregs[def_opcode], "Re");
298 if (dststr) {
299 dststr = strchr(opcode_reginfo[def_opcode], 'e');
300 } else {
301 dststr = strstr(opcode_wregs[def_opcode], "Ry");
302 if (dststr) {
303 dststr = strchr(opcode_reginfo[def_opcode], 'y');
304 } else {
305 g_assert_not_reached();
306 }
307 }
308 }
309 }
310 g_assert(dststr != NULL);
311
312
313 dst_idx = dststr - opcode_reginfo[def_opcode];
314 packet->insn[i].regno[use_regidx] =
315 packet->insn[def_idx].regno[dst_idx];
316
317
318
319
320 packet->insn[i].new_value_producer_slot =
321 packet->insn[def_idx].slot;
322 }
323 }
324}
325
326
327static void decode_split_cmpjump(Packet *pkt)
328{
329 int last, i;
330 int numinsns = pkt->num_insns;
331
332
333
334
335
336
337
338 for (i = 0; i < numinsns; i++) {
339
340 if (GET_ATTRIB(pkt->insn[i].opcode, A_NEWCMPJUMP)) {
341 last = pkt->num_insns;
342 pkt->insn[last] = pkt->insn[i];
343 pkt->insn[last].part1 = 1;
344 pkt->insn[i].part1 = 0;
345 pkt->num_insns++;
346 }
347 }
348
349
350 for (i = 0; i < pkt->num_insns; i++) {
351 if (pkt->insn[i].part1) {
352 decode_send_insn_to(pkt, i, 0);
353 }
354 }
355}
356
357static inline int decode_opcode_can_jump(int opcode)
358{
359 if ((GET_ATTRIB(opcode, A_JUMP)) ||
360 (GET_ATTRIB(opcode, A_CALL)) ||
361 (opcode == J2_trap0) ||
362 (opcode == J2_pause)) {
363
364 if (opcode == J4_hintjumpr) {
365 return 0;
366 }
367 return 1;
368 }
369
370 return 0;
371}
372
373static inline int decode_opcode_ends_loop(int opcode)
374{
375 return GET_ATTRIB(opcode, A_HWLOOP0_END) ||
376 GET_ATTRIB(opcode, A_HWLOOP1_END);
377}
378
379
380static void decode_set_insn_attr_fields(Packet *pkt)
381{
382 int i;
383 int numinsns = pkt->num_insns;
384 uint16_t opcode;
385
386 pkt->pkt_has_cof = 0;
387 pkt->pkt_has_endloop = 0;
388 pkt->pkt_has_dczeroa = 0;
389
390 for (i = 0; i < numinsns; i++) {
391 opcode = pkt->insn[i].opcode;
392 if (pkt->insn[i].part1) {
393 continue;
394 }
395
396 if (GET_ATTRIB(opcode, A_DCZEROA)) {
397 pkt->pkt_has_dczeroa = 1;
398 }
399
400 if (GET_ATTRIB(opcode, A_STORE)) {
401 if (pkt->insn[i].slot == 0) {
402 pkt->pkt_has_store_s0 = 1;
403 } else {
404 pkt->pkt_has_store_s1 = 1;
405 }
406 }
407
408 pkt->pkt_has_cof |= decode_opcode_can_jump(opcode);
409
410 pkt->insn[i].is_endloop = decode_opcode_ends_loop(opcode);
411
412 pkt->pkt_has_endloop |= pkt->insn[i].is_endloop;
413
414 pkt->pkt_has_cof |= pkt->pkt_has_endloop;
415 }
416}
417
418
419
420
421
422
423static void decode_shuffle_for_execution(Packet *packet)
424{
425 int changed = 0;
426 int i;
427 int flag;
428 int n_mems;
429 int last_insn = packet->num_insns - 1;
430
431
432
433
434
435 if (decode_opcode_ends_loop(packet->insn[last_insn].opcode)) {
436 last_insn--;
437 }
438
439 do {
440 changed = 0;
441
442
443
444
445
446
447
448 for (flag = n_mems = 0, i = last_insn; i >= 0; i--) {
449 int opcode = packet->insn[i].opcode;
450
451 if (flag && GET_ATTRIB(opcode, A_STORE)) {
452 decode_send_insn_to(packet, i, last_insn - n_mems);
453 n_mems++;
454 changed = 1;
455 } else if (GET_ATTRIB(opcode, A_STORE)) {
456 n_mems++;
457 } else if (GET_ATTRIB(opcode, A_LOAD)) {
458
459
460
461
462 n_mems++;
463 } else if (GET_ATTRIB(opcode, A_DOTNEWVALUE)) {
464
465
466
467
468 } else {
469 flag = 1;
470 }
471 }
472
473 if (changed) {
474 continue;
475 }
476
477 for (flag = 0, i = 0; i < last_insn + 1; i++) {
478 int opcode = packet->insn[i].opcode;
479
480 if ((strstr(opcode_wregs[opcode], "Pd4") ||
481 strstr(opcode_wregs[opcode], "Pe4")) &&
482 GET_ATTRIB(opcode, A_STORE) == 0) {
483
484 if (flag) {
485 decode_send_insn_to(packet, i, 0);
486 changed = 1;
487 continue;
488 }
489 } else if (GET_ATTRIB(opcode, A_IMPLICIT_WRITES_P3) &&
490 !decode_opcode_ends_loop(packet->insn[i].opcode)) {
491
492
493
494
495
496 if (flag) {
497 decode_send_insn_to(packet, i, 0);
498 changed = 1;
499 continue;
500 }
501 } else if (GET_ATTRIB(opcode, A_IMPLICIT_WRITES_P0) &&
502 !GET_ATTRIB(opcode, A_NEWCMPJUMP)) {
503 if (flag) {
504 decode_send_insn_to(packet, i, 0);
505 changed = 1;
506 continue;
507 }
508 } else {
509 flag = 1;
510 }
511 }
512 if (changed) {
513 continue;
514 }
515 } while (changed);
516
517
518
519
520
521 for (i = 0; i < last_insn; i++) {
522 if (GET_ATTRIB(packet->insn[i].opcode, A_DOTNEWVALUE)) {
523 decode_send_insn_to(packet, i, last_insn);
524 break;
525 }
526 }
527}
528
529static void
530apply_extender(Packet *pkt, int i, uint32_t extender)
531{
532 int immed_num;
533 uint32_t base_immed;
534
535 immed_num = opcode_which_immediate_is_extended(pkt->insn[i].opcode);
536 base_immed = pkt->insn[i].immed[immed_num];
537
538 pkt->insn[i].immed[immed_num] = extender | fZXTN(6, 32, base_immed);
539}
540
541static void decode_apply_extenders(Packet *packet)
542{
543 int i;
544 for (i = 0; i < packet->num_insns; i++) {
545 if (GET_ATTRIB(packet->insn[i].opcode, A_IT_EXTENDER)) {
546 packet->insn[i + 1].extension_valid = 1;
547 apply_extender(packet, i + 1, packet->insn[i].immed[0]);
548 }
549 }
550}
551
552static void decode_remove_extenders(Packet *packet)
553{
554 int i, j;
555 for (i = 0; i < packet->num_insns; i++) {
556 if (GET_ATTRIB(packet->insn[i].opcode, A_IT_EXTENDER)) {
557
558 for (j = i;
559 (j < packet->num_insns - 1) && (j < INSTRUCTIONS_MAX - 1);
560 j++) {
561 packet->insn[j] = packet->insn[j + 1];
562 }
563 packet->num_insns--;
564 }
565 }
566}
567
568static SlotMask get_valid_slots(const Packet *pkt, unsigned int slot)
569{
570 return find_iclass_slots(pkt->insn[slot].opcode,
571 pkt->insn[slot].iclass);
572}
573
574#define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT)
575#define TABLE_LINK(TABLE)
576#define TERMINAL(TAG, ENC)
577#define SUBINSNS(TAG, CLASSA, CLASSB, ENC)
578#define EXTSPACE(TAG, ENC)
579#define INVALID()
580#define DECODE_END_TABLE(...)
581#define DECODE_MATCH_INFO(...)
582#define DECODE_LEGACY_MATCH_INFO(...)
583
584#define DECODE_REG(REGNO, WIDTH, STARTBIT) \
585 insn->regno[REGNO] = ((encoding >> STARTBIT) & ((1 << WIDTH) - 1));
586
587#define DECODE_IMPL_REG(REGNO, VAL) \
588 insn->regno[REGNO] = VAL;
589
590#define DECODE_IMM(IMMNO, WIDTH, STARTBIT, VALSTART) \
591 insn->immed[IMMNO] |= (((encoding >> STARTBIT) & ((1 << WIDTH) - 1))) << \
592 (VALSTART);
593
594#define DECODE_IMM_SXT(IMMNO, WIDTH) \
595 insn->immed[IMMNO] = ((((int32_t)insn->immed[IMMNO]) << (32 - WIDTH)) >> \
596 (32 - WIDTH));
597
598#define DECODE_IMM_NEG(IMMNO, WIDTH) \
599 insn->immed[IMMNO] = -insn->immed[IMMNO];
600
601#define DECODE_IMM_SHIFT(IMMNO, SHAMT) \
602 if ((!insn->extension_valid) || \
603 (insn->which_extended != IMMNO)) { \
604 insn->immed[IMMNO] <<= SHAMT; \
605 }
606
607#define DECODE_OPINFO(TAG, BEH) \
608 case TAG: \
609 { BEH } \
610 break; \
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625static void
626decode_op(Insn *insn, Opcode tag, uint32_t encoding)
627{
628 insn->immed[0] = 0;
629 insn->immed[1] = 0;
630 insn->opcode = tag;
631 if (insn->extension_valid) {
632 insn->which_extended = opcode_which_immediate_is_extended(tag);
633 }
634
635 switch (tag) {
636#include "dectree_generated.h.inc"
637 default:
638 break;
639 }
640
641 insn->generate = opcode_genptr[tag];
642
643 insn->iclass = iclass_bits(encoding);
644}
645
646#undef DECODE_REG
647#undef DECODE_IMPL_REG
648#undef DECODE_IMM
649#undef DECODE_IMM_SHIFT
650#undef DECODE_OPINFO
651#undef DECODE_MATCH_INFO
652#undef DECODE_LEGACY_MATCH_INFO
653#undef DECODE_END_TABLE
654#undef INVALID
655#undef TERMINAL
656#undef SUBINSNS
657#undef EXTSPACE
658#undef TABLE_LINK
659#undef DECODE_NEW_TABLE
660#undef DECODE_SEPARATOR_BITS
661
662static unsigned int
663decode_subinsn_tablewalk(Insn *insn, const DectreeTable *table,
664 uint32_t encoding)
665{
666 unsigned int i;
667 Opcode opc;
668 if (table->lookup_function) {
669 i = table->lookup_function(table->startbit, table->width, encoding);
670 } else {
671 i = extract32(encoding, table->startbit, table->width);
672 }
673 if (table->table[i].type == DECTREE_TABLE_LINK) {
674 return decode_subinsn_tablewalk(insn, table->table[i].table_link,
675 encoding);
676 } else if (table->table[i].type == DECTREE_TERMINAL) {
677 opc = table->table[i].opcode;
678 if ((encoding & decode_itable[opc].mask) != decode_itable[opc].match) {
679 return 0;
680 }
681 decode_op(insn, opc, encoding);
682 return 1;
683 } else {
684 return 0;
685 }
686}
687
688static unsigned int get_insn_a(uint32_t encoding)
689{
690 return extract32(encoding, 0, 13);
691}
692
693static unsigned int get_insn_b(uint32_t encoding)
694{
695 return extract32(encoding, 16, 13);
696}
697
698static unsigned int
699decode_insns_tablewalk(Insn *insn, const DectreeTable *table,
700 uint32_t encoding)
701{
702 unsigned int i;
703 unsigned int a, b;
704 Opcode opc;
705 if (table->lookup_function) {
706 i = table->lookup_function(table->startbit, table->width, encoding);
707 } else {
708 i = extract32(encoding, table->startbit, table->width);
709 }
710 if (table->table[i].type == DECTREE_TABLE_LINK) {
711 return decode_insns_tablewalk(insn, table->table[i].table_link,
712 encoding);
713 } else if (table->table[i].type == DECTREE_SUBINSNS) {
714 a = get_insn_a(encoding);
715 b = get_insn_b(encoding);
716 b = decode_subinsn_tablewalk(insn, table->table[i].table_link_b, b);
717 a = decode_subinsn_tablewalk(insn + 1, table->table[i].table_link, a);
718 if ((a == 0) || (b == 0)) {
719 return 0;
720 }
721 return 2;
722 } else if (table->table[i].type == DECTREE_TERMINAL) {
723 opc = table->table[i].opcode;
724 if ((encoding & decode_itable[opc].mask) != decode_itable[opc].match) {
725 if ((encoding & decode_legacy_itable[opc].mask) !=
726 decode_legacy_itable[opc].match) {
727 return 0;
728 }
729 }
730 decode_op(insn, opc, encoding);
731 return 1;
732 } else {
733 return 0;
734 }
735}
736
737static unsigned int
738decode_insns(Insn *insn, uint32_t encoding)
739{
740 const DectreeTable *table;
741 if (parse_bits(encoding) != 0) {
742
743 table = &dectree_table_DECODE_ROOT_32;
744 } else {
745
746 table = &dectree_table_DECODE_ROOT_EE;
747 }
748 return decode_insns_tablewalk(insn, table, encoding);
749}
750
751static void decode_add_endloop_insn(Insn *insn, int loopnum)
752{
753 if (loopnum == 10) {
754 insn->opcode = J2_endloop01;
755 insn->generate = opcode_genptr[J2_endloop01];
756 } else if (loopnum == 1) {
757 insn->opcode = J2_endloop1;
758 insn->generate = opcode_genptr[J2_endloop1];
759 } else if (loopnum == 0) {
760 insn->opcode = J2_endloop0;
761 insn->generate = opcode_genptr[J2_endloop0];
762 } else {
763 g_assert_not_reached();
764 }
765}
766
767static inline int decode_parsebits_is_loopend(uint32_t encoding32)
768{
769 uint32_t bits = parse_bits(encoding32);
770 return bits == 0x2;
771}
772
773static void
774decode_set_slot_number(Packet *pkt)
775{
776 int slot;
777 int i;
778 int hit_mem_insn = 0;
779 int hit_duplex = 0;
780
781
782
783
784
785 for (i = 0, slot = 3; i < pkt->num_insns; i++) {
786 SlotMask valid_slots = get_valid_slots(pkt, i);
787
788 while (!(valid_slots & (1 << slot))) {
789 slot--;
790 }
791 pkt->insn[i].slot = slot;
792 if (slot) {
793
794 slot--;
795 }
796 }
797
798
799 for (i = pkt->num_insns - 1; i >= 0; i--) {
800
801 if ((GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE) ||
802 GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE_PACKET_RULES)) &&
803 !hit_mem_insn) {
804 hit_mem_insn = 1;
805 pkt->insn[i].slot = 0;
806 continue;
807 }
808
809
810 if ((GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE) ||
811 GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE_PACKET_RULES)) &&
812 hit_mem_insn) {
813 pkt->insn[i].slot = 1;
814 }
815 }
816
817
818 for (i = pkt->num_insns - 1; i >= 0; i--) {
819
820 if (GET_ATTRIB(pkt->insn[i].opcode, A_SUBINSN) && !hit_duplex) {
821 hit_duplex = 1;
822 pkt->insn[i].slot = 0;
823 continue;
824 }
825
826
827 if (GET_ATTRIB(pkt->insn[i].opcode, A_SUBINSN) && hit_duplex) {
828 pkt->insn[i].slot = 1;
829 }
830 }
831
832
833 int slot0_found = 0;
834 int slot1_found = 0;
835 int slot1_iidx = 0;
836 for (i = pkt->num_insns - 1; i >= 0; i--) {
837
838 if (pkt->insn[i].slot == 0) {
839 int is_endloop = (pkt->insn[i].opcode == J2_endloop01);
840 is_endloop |= (pkt->insn[i].opcode == J2_endloop0);
841 is_endloop |= (pkt->insn[i].opcode == J2_endloop1);
842
843
844
845
846
847 if (!is_endloop) {
848 slot0_found = 1;
849 }
850 }
851
852 if (pkt->insn[i].slot == 1) {
853 slot1_found = 1;
854 slot1_iidx = i;
855 }
856 }
857
858 if ((slot0_found == 0) && (slot1_found == 1)) {
859
860 pkt->insn[slot1_iidx].slot = 0;
861 }
862}
863
864
865
866
867
868
869
870
871int decode_packet(int max_words, const uint32_t *words, Packet *pkt,
872 bool disas_only)
873{
874 int num_insns = 0;
875 int words_read = 0;
876 int end_of_packet = 0;
877 int new_insns = 0;
878 uint32_t encoding32;
879
880
881 memset(pkt, 0, sizeof(*pkt));
882
883 while (!end_of_packet && (words_read < max_words)) {
884 encoding32 = words[words_read];
885 end_of_packet = is_packet_end(encoding32);
886 new_insns = decode_insns(&pkt->insn[num_insns], encoding32);
887 g_assert(new_insns > 0);
888
889
890
891
892 if (pkt->insn[num_insns].opcode == A4_ext) {
893 pkt->insn[num_insns + 1].extension_valid = 1;
894 }
895 num_insns += new_insns;
896 words_read++;
897 }
898
899 pkt->num_insns = num_insns;
900 if (!end_of_packet) {
901
902 return 0;
903 }
904 pkt->encod_pkt_size_in_bytes = words_read * 4;
905
906
907
908
909
910
911
912 if ((words_read == 2) && (decode_parsebits_is_loopend(words[0]))) {
913 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 0);
914 }
915 if (words_read >= 3) {
916 uint32_t has_loop0, has_loop1;
917 has_loop0 = decode_parsebits_is_loopend(words[0]);
918 has_loop1 = decode_parsebits_is_loopend(words[1]);
919 if (has_loop0 && has_loop1) {
920 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 10);
921 } else if (has_loop1) {
922 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 1);
923 } else if (has_loop0) {
924 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 0);
925 }
926 }
927
928 decode_apply_extenders(pkt);
929 if (!disas_only) {
930 decode_remove_extenders(pkt);
931 }
932 decode_set_slot_number(pkt);
933 decode_fill_newvalue_regno(pkt);
934
935 if (!disas_only) {
936 decode_shuffle_for_execution(pkt);
937 decode_split_cmpjump(pkt);
938 decode_set_insn_attr_fields(pkt);
939 }
940
941 return words_read;
942}
943
944
945int disassemble_hexagon(uint32_t *words, int nwords, bfd_vma pc,
946 GString *buf)
947{
948 Packet pkt;
949
950 if (decode_packet(nwords, words, &pkt, true) > 0) {
951 snprint_a_pkt_disas(buf, &pkt, words, pc);
952 return pkt.encod_pkt_size_in_bytes;
953 } else {
954 g_string_assign(buf, "<invalid>");
955 return 0;
956 }
957}
958