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