2 * Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
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.
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.
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/>.
18 #include "qemu/osdep.h"
25 #include "printinsn.h"
27 #define fZXTN(N, M, VAL) ((VAL) & ((1LL << (N)) - 1))
31 EXT_IDX_noext_AFTER
= 4,
33 EXT_IDX_mmvec_AFTER
= 8,
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.
44 #define DEF_REGMAP(NAME, ELEMENTS, ...) \
45 static const unsigned int DECODE_REGISTER_##NAME[ELEMENTS] = \
48 DEF_REGMAP(R_16
, 16, 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23)
49 DEF_REGMAP(R__8
, 8, 0, 2, 4, 6, 16, 18, 20, 22)
51 #define DECODE_MAPPED_REG(REGNO, NAME) \
52 insn->regno[REGNO] = DECODE_REGISTER_##NAME[insn->regno[REGNO]];
55 const struct DectreeTable
*table_link
;
56 const struct DectreeTable
*table_link_b
;
59 DECTREE_ENTRY_INVALID
,
67 typedef struct DectreeTable
{
68 unsigned int (*lookup_function
)(int startbit
, int width
, uint32_t opcode
);
70 unsigned int startbit
;
72 const DectreeEntry table
[];
75 #define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) \
76 static const DectreeTable dectree_table_##TAG;
77 #define TABLE_LINK(TABLE) /* NOTHING */
78 #define TERMINAL(TAG, ENC) /* NOTHING */
79 #define SUBINSNS(TAG, CLASSA, CLASSB, ENC) /* NOTHING */
80 #define EXTSPACE(TAG, ENC) /* NOTHING */
81 #define INVALID() /* NOTHING */
82 #define DECODE_END_TABLE(...) /* NOTHING */
83 #define DECODE_MATCH_INFO(...) /* NOTHING */
84 #define DECODE_LEGACY_MATCH_INFO(...) /* NOTHING */
85 #define DECODE_OPINFO(...) /* NOTHING */
87 #include "dectree_generated.h.inc"
90 #undef DECODE_MATCH_INFO
91 #undef DECODE_LEGACY_MATCH_INFO
92 #undef DECODE_END_TABLE
98 #undef DECODE_NEW_TABLE
99 #undef DECODE_SEPARATOR_BITS
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 = { \
105 .lookup_function = FN, \
109 #define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) \
110 DECODE_NEW_TABLE_HELPER(TAG, SIZE, WHATNOT)
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) \
118 .type = DECTREE_SUBINSNS, \
119 .table_link = &dectree_table_DECODE_SUBINSN_##CLASSA, \
120 .table_link_b = &dectree_table_DECODE_SUBINSN_##CLASSB \
122 #define EXTSPACE(TAG, ENC) { .type = DECTREE_EXTSPACE },
123 #define INVALID() { .type = DECTREE_ENTRY_INVALID, .opcode = XX_LAST_OPCODE },
125 #define DECODE_END_TABLE(...) } };
127 #define DECODE_MATCH_INFO(...) /* NOTHING */
128 #define DECODE_LEGACY_MATCH_INFO(...) /* NOTHING */
129 #define DECODE_OPINFO(...) /* NOTHING */
131 #include "dectree_generated.h.inc"
134 #undef DECODE_MATCH_INFO
135 #undef DECODE_LEGACY_MATCH_INFO
136 #undef DECODE_END_TABLE
142 #undef DECODE_NEW_TABLE
143 #undef DECODE_NEW_TABLE_HELPER
144 #undef DECODE_SEPARATOR_BITS
146 static const DectreeTable dectree_table_DECODE_EXT_EXT_noext
= {
147 .size
= 1, .lookup_function
= NULL
, .startbit
= 0, .width
= 0,
149 { .type
= DECTREE_ENTRY_INVALID
, .opcode
= XX_LAST_OPCODE
},
153 static const DectreeTable
*ext_trees
[XX_LAST_EXT_IDX
];
155 static void decode_ext_init(void)
158 for (i
= EXT_IDX_noext
; i
< EXT_IDX_noext_AFTER
; i
++) {
159 ext_trees
[i
] = &dectree_table_DECODE_EXT_EXT_noext
;
168 #define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) /* NOTHING */
169 #define TABLE_LINK(TABLE) /* NOTHING */
170 #define TERMINAL(TAG, ENC) /* NOTHING */
171 #define SUBINSNS(TAG, CLASSA, CLASSB, ENC) /* NOTHING */
172 #define EXTSPACE(TAG, ENC) /* NOTHING */
173 #define INVALID() /* NOTHING */
174 #define DECODE_END_TABLE(...) /* NOTHING */
175 #define DECODE_OPINFO(...) /* NOTHING */
177 #define DECODE_MATCH_INFO_NORMAL(TAG, MASK, MATCH) \
183 #define DECODE_MATCH_INFO_NULL(TAG, MASK, MATCH) \
184 [TAG] = { .match = ~0 },
186 #define DECODE_MATCH_INFO(...) DECODE_MATCH_INFO_NORMAL(__VA_ARGS__)
187 #define DECODE_LEGACY_MATCH_INFO(...) /* NOTHING */
189 static const DecodeITableEntry decode_itable
[XX_LAST_OPCODE
] = {
190 #include "dectree_generated.h.inc"
193 #undef DECODE_MATCH_INFO
194 #define DECODE_MATCH_INFO(...) DECODE_MATCH_INFO_NULL(__VA_ARGS__)
196 #undef DECODE_LEGACY_MATCH_INFO
197 #define DECODE_LEGACY_MATCH_INFO(...) DECODE_MATCH_INFO_NORMAL(__VA_ARGS__)
199 static const DecodeITableEntry decode_legacy_itable
[XX_LAST_OPCODE
] = {
200 #include "dectree_generated.h.inc"
204 #undef DECODE_MATCH_INFO
205 #undef DECODE_LEGACY_MATCH_INFO
206 #undef DECODE_END_TABLE
212 #undef DECODE_NEW_TABLE
213 #undef DECODE_SEPARATOR_BITS
215 void decode_init(void)
220 void decode_send_insn_to(Packet
*packet
, int start
, int newloc
)
225 if (start
== newloc
) {
228 if (start
< newloc
) {
229 /* Move towards end */
232 /* move towards beginning */
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
;
242 /* Fill newvalue registers with the correct regno */
244 decode_fill_newvalue_regno(Packet
*packet
)
246 int i
, use_regidx
, offset
, def_idx
, dst_idx
;
247 uint16_t def_opcode
, use_opcode
;
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
;
255 /* It's a store, so we're adjusting the Nt field */
256 if (GET_ATTRIB(use_opcode
, A_STORE
)) {
257 use_regidx
= strchr(opcode_reginfo
[use_opcode
], 't') -
258 opcode_reginfo
[use_opcode
];
259 } else { /* It's a Jump, so we're adjusting the Ns field */
260 use_regidx
= strchr(opcode_reginfo
[use_opcode
], 's') -
261 opcode_reginfo
[use_opcode
];
265 * What's encoded at the N-field is the offset to who's producing
266 * the value. Shift off the LSB which indicates odd/even register,
267 * then walk backwards and skip over the constant extenders.
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
)) {
278 * Check for a badly encoded N-field which points to an instruction
281 g_assert(!((def_idx
< 0) || (def_idx
> (packet
->num_insns
- 1))));
284 * packet->insn[def_idx] is the producer
285 * Figure out which type of destination it produces
286 * and the corresponding index in the reginfo
288 def_opcode
= packet
->insn
[def_idx
].opcode
;
289 dststr
= strstr(opcode_wregs
[def_opcode
], "Rd");
291 dststr
= strchr(opcode_reginfo
[def_opcode
], 'd');
293 dststr
= strstr(opcode_wregs
[def_opcode
], "Rx");
295 dststr
= strchr(opcode_reginfo
[def_opcode
], 'x');
297 dststr
= strstr(opcode_wregs
[def_opcode
], "Re");
299 dststr
= strchr(opcode_reginfo
[def_opcode
], 'e');
301 dststr
= strstr(opcode_wregs
[def_opcode
], "Ry");
303 dststr
= strchr(opcode_reginfo
[def_opcode
], 'y');
305 g_assert_not_reached();
310 g_assert(dststr
!= NULL
);
312 /* Now patch up the consumer with the register number */
313 dst_idx
= dststr
- opcode_reginfo
[def_opcode
];
314 packet
->insn
[i
].regno
[use_regidx
] =
315 packet
->insn
[def_idx
].regno
[dst_idx
];
317 * We need to remember who produces this value to later
318 * check if it was dynamically cancelled
320 packet
->insn
[i
].new_value_producer_slot
=
321 packet
->insn
[def_idx
].slot
;
326 /* Split CJ into a compare and a jump */
327 static void decode_split_cmpjump(Packet
*pkt
)
330 int numinsns
= pkt
->num_insns
;
333 * First, split all compare-jumps.
334 * The compare is sent to the end as a new instruction.
335 * Do it this way so we don't reorder dual jumps. Those need to stay in
338 for (i
= 0; i
< numinsns
; i
++) {
339 /* It's a cmp-jump */
340 if (GET_ATTRIB(pkt
->insn
[i
].opcode
, A_NEWCMPJUMP
)) {
341 last
= pkt
->num_insns
;
342 pkt
->insn
[last
] = pkt
->insn
[i
]; /* copy the instruction */
343 pkt
->insn
[last
].part1
= 1; /* last instruction does the CMP */
344 pkt
->insn
[i
].part1
= 0; /* existing instruction does the JUMP */
349 /* Now re-shuffle all the compares back to the beginning */
350 for (i
= 0; i
< pkt
->num_insns
; i
++) {
351 if (pkt
->insn
[i
].part1
) {
352 decode_send_insn_to(pkt
, i
, 0);
357 static inline int decode_opcode_can_jump(int opcode
)
359 if ((GET_ATTRIB(opcode
, A_JUMP
)) ||
360 (GET_ATTRIB(opcode
, A_CALL
)) ||
361 (opcode
== J2_trap0
) ||
362 (opcode
== J2_pause
)) {
363 /* Exception to A_JUMP attribute */
364 if (opcode
== J4_hintjumpr
) {
373 static inline int decode_opcode_ends_loop(int opcode
)
375 return GET_ATTRIB(opcode
, A_HWLOOP0_END
) ||
376 GET_ATTRIB(opcode
, A_HWLOOP1_END
);
379 /* Set the is_* fields in each instruction */
380 static void decode_set_insn_attr_fields(Packet
*pkt
)
383 int numinsns
= pkt
->num_insns
;
386 pkt
->pkt_has_cof
= 0;
387 pkt
->pkt_has_endloop
= 0;
388 pkt
->pkt_has_dczeroa
= 0;
390 for (i
= 0; i
< numinsns
; i
++) {
391 opcode
= pkt
->insn
[i
].opcode
;
392 if (pkt
->insn
[i
].part1
) {
393 continue; /* Skip compare of cmp-jumps */
396 if (GET_ATTRIB(opcode
, A_DCZEROA
)) {
397 pkt
->pkt_has_dczeroa
= 1;
400 if (GET_ATTRIB(opcode
, A_STORE
)) {
401 if (pkt
->insn
[i
].slot
== 0) {
402 pkt
->pkt_has_store_s0
= 1;
404 pkt
->pkt_has_store_s1
= 1;
408 pkt
->pkt_has_cof
|= decode_opcode_can_jump(opcode
);
410 pkt
->insn
[i
].is_endloop
= decode_opcode_ends_loop(opcode
);
412 pkt
->pkt_has_endloop
|= pkt
->insn
[i
].is_endloop
;
414 pkt
->pkt_has_cof
|= pkt
->pkt_has_endloop
;
419 * Shuffle for execution
420 * Move stores to end (in same order as encoding)
421 * Move compares to beginning (for use by .new insns)
423 static void decode_shuffle_for_execution(Packet
*packet
)
427 int flag
; /* flag means we've seen a non-memory instruction */
429 int last_insn
= packet
->num_insns
- 1;
432 * Skip end loops, somehow an end loop is getting in and messing
435 if (decode_opcode_ends_loop(packet
->insn
[last_insn
].opcode
)) {
442 * Stores go last, must not reorder.
443 * Cannot shuffle stores past loads, either.
444 * Iterate backwards. If we see a non-memory instruction,
445 * then a store, shuffle the store to the front. Don't shuffle
446 * stores wrt each other or a load.
448 for (flag
= n_mems
= 0, i
= last_insn
; i
>= 0; i
--) {
449 int opcode
= packet
->insn
[i
].opcode
;
451 if (flag
&& GET_ATTRIB(opcode
, A_STORE
)) {
452 decode_send_insn_to(packet
, i
, last_insn
- n_mems
);
455 } else if (GET_ATTRIB(opcode
, A_STORE
)) {
457 } else if (GET_ATTRIB(opcode
, A_LOAD
)) {
459 * Don't set flag, since we don't want to shuffle a
463 } else if (GET_ATTRIB(opcode
, A_DOTNEWVALUE
)) {
465 * Don't set flag, since we don't want to shuffle past
476 /* Compares go first, may be reordered wrt each other */
477 for (flag
= 0, i
= 0; i
< last_insn
+ 1; i
++) {
478 int opcode
= packet
->insn
[i
].opcode
;
480 if ((strstr(opcode_wregs
[opcode
], "Pd4") ||
481 strstr(opcode_wregs
[opcode
], "Pe4")) &&
482 GET_ATTRIB(opcode
, A_STORE
) == 0) {
483 /* This should be a compare (not a store conditional) */
485 decode_send_insn_to(packet
, i
, 0);
489 } else if (GET_ATTRIB(opcode
, A_IMPLICIT_WRITES_P3
) &&
490 !decode_opcode_ends_loop(packet
->insn
[i
].opcode
)) {
492 * spNloop instruction
493 * Don't reorder endloops; they are not valid for .new uses,
494 * and we want to match HW
497 decode_send_insn_to(packet
, i
, 0);
501 } else if (GET_ATTRIB(opcode
, A_IMPLICIT_WRITES_P0
) &&
502 !GET_ATTRIB(opcode
, A_NEWCMPJUMP
)) {
504 decode_send_insn_to(packet
, i
, 0);
518 * If we have a .new register compare/branch, move that to the very
519 * very end, past stores
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
);
530 apply_extender(Packet
*pkt
, int i
, uint32_t extender
)
535 immed_num
= opcode_which_immediate_is_extended(pkt
->insn
[i
].opcode
);
536 base_immed
= pkt
->insn
[i
].immed
[immed_num
];
538 pkt
->insn
[i
].immed
[immed_num
] = extender
| fZXTN(6, 32, base_immed
);
541 static void decode_apply_extenders(Packet
*packet
)
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]);
552 static void decode_remove_extenders(Packet
*packet
)
555 for (i
= 0; i
< packet
->num_insns
; i
++) {
556 if (GET_ATTRIB(packet
->insn
[i
].opcode
, A_IT_EXTENDER
)) {
557 /* Remove this one by moving the remaining instructions down */
559 (j
< packet
->num_insns
- 1) && (j
< INSTRUCTIONS_MAX
- 1);
561 packet
->insn
[j
] = packet
->insn
[j
+ 1];
568 static SlotMask
get_valid_slots(const Packet
*pkt
, unsigned int slot
)
570 return find_iclass_slots(pkt
->insn
[slot
].opcode
,
571 pkt
->insn
[slot
].iclass
);
574 #define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) /* NOTHING */
575 #define TABLE_LINK(TABLE) /* NOTHING */
576 #define TERMINAL(TAG, ENC) /* NOTHING */
577 #define SUBINSNS(TAG, CLASSA, CLASSB, ENC) /* NOTHING */
578 #define EXTSPACE(TAG, ENC) /* NOTHING */
579 #define INVALID() /* NOTHING */
580 #define DECODE_END_TABLE(...) /* NOTHING */
581 #define DECODE_MATCH_INFO(...) /* NOTHING */
582 #define DECODE_LEGACY_MATCH_INFO(...) /* NOTHING */
584 #define DECODE_REG(REGNO, WIDTH, STARTBIT) \
585 insn->regno[REGNO] = ((encoding >> STARTBIT) & ((1 << WIDTH) - 1));
587 #define DECODE_IMPL_REG(REGNO, VAL) \
588 insn->regno[REGNO] = VAL;
590 #define DECODE_IMM(IMMNO, WIDTH, STARTBIT, VALSTART) \
591 insn->immed[IMMNO] |= (((encoding >> STARTBIT) & ((1 << WIDTH) - 1))) << \
594 #define DECODE_IMM_SXT(IMMNO, WIDTH) \
595 insn->immed[IMMNO] = ((((int32_t)insn->immed[IMMNO]) << (32 - WIDTH)) >> \
598 #define DECODE_IMM_NEG(IMMNO, WIDTH) \
599 insn->immed[IMMNO] = -insn->immed[IMMNO];
601 #define DECODE_IMM_SHIFT(IMMNO, SHAMT) \
602 if ((!insn->extension_valid) || \
603 (insn->which_extended != IMMNO)) { \
604 insn->immed[IMMNO] <<= SHAMT; \
607 #define DECODE_OPINFO(TAG, BEH) \
613 * Fill in the operands of the instruction
614 * dectree_generated.h.inc has a DECODE_OPINFO entry for each opcode
616 * DECODE_OPINFO(A2_addi,
619 * DECODE_IMM(0,7,21,9)
620 * DECODE_IMM(0,9,5,0)
621 * DECODE_IMM_SXT(0,16)
622 * with the macros defined above, we'll fill in a switch statement
623 * where each case is an opcode tag.
626 decode_op(Insn
*insn
, Opcode tag
, uint32_t encoding
)
631 if (insn
->extension_valid
) {
632 insn
->which_extended
= opcode_which_immediate_is_extended(tag
);
636 #include "dectree_generated.h.inc"
641 insn
->generate
= opcode_genptr
[tag
];
643 insn
->iclass
= iclass_bits(encoding
);
647 #undef DECODE_IMPL_REG
649 #undef DECODE_IMM_SHIFT
651 #undef DECODE_MATCH_INFO
652 #undef DECODE_LEGACY_MATCH_INFO
653 #undef DECODE_END_TABLE
659 #undef DECODE_NEW_TABLE
660 #undef DECODE_SEPARATOR_BITS
663 decode_subinsn_tablewalk(Insn
*insn
, const DectreeTable
*table
,
668 if (table
->lookup_function
) {
669 i
= table
->lookup_function(table
->startbit
, table
->width
, encoding
);
671 i
= extract32(encoding
, table
->startbit
, table
->width
);
673 if (table
->table
[i
].type
== DECTREE_TABLE_LINK
) {
674 return decode_subinsn_tablewalk(insn
, table
->table
[i
].table_link
,
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
) {
681 decode_op(insn
, opc
, encoding
);
688 static unsigned int get_insn_a(uint32_t encoding
)
690 return extract32(encoding
, 0, 13);
693 static unsigned int get_insn_b(uint32_t encoding
)
695 return extract32(encoding
, 16, 13);
699 decode_insns_tablewalk(Insn
*insn
, const DectreeTable
*table
,
705 if (table
->lookup_function
) {
706 i
= table
->lookup_function(table
->startbit
, table
->width
, encoding
);
708 i
= extract32(encoding
, table
->startbit
, table
->width
);
710 if (table
->table
[i
].type
== DECTREE_TABLE_LINK
) {
711 return decode_insns_tablewalk(insn
, table
->table
[i
].table_link
,
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)) {
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
) {
730 decode_op(insn
, opc
, encoding
);
738 decode_insns(Insn
*insn
, uint32_t encoding
)
740 const DectreeTable
*table
;
741 if (parse_bits(encoding
) != 0) {
742 /* Start with PP table - 32 bit instructions */
743 table
= &dectree_table_DECODE_ROOT_32
;
745 /* start with EE table - duplex instructions */
746 table
= &dectree_table_DECODE_ROOT_EE
;
748 return decode_insns_tablewalk(insn
, table
, encoding
);
751 static void decode_add_endloop_insn(Insn
*insn
, int loopnum
)
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
];
763 g_assert_not_reached();
767 static inline int decode_parsebits_is_loopend(uint32_t encoding32
)
769 uint32_t bits
= parse_bits(encoding32
);
774 decode_set_slot_number(Packet
*pkt
)
778 int hit_mem_insn
= 0;
782 * The slots are encoded in reverse order
783 * For each instruction, count down until you find a suitable slot
785 for (i
= 0, slot
= 3; i
< pkt
->num_insns
; i
++) {
786 SlotMask valid_slots
= get_valid_slots(pkt
, i
);
788 while (!(valid_slots
& (1 << slot
))) {
791 pkt
->insn
[i
].slot
= slot
;
793 /* I've assigned the slot, now decrement it for the next insn */
798 /* Fix the exceptions - mem insns to slot 0,1 */
799 for (i
= pkt
->num_insns
- 1; i
>= 0; i
--) {
800 /* First memory instruction always goes to slot 0 */
801 if ((GET_ATTRIB(pkt
->insn
[i
].opcode
, A_MEMLIKE
) ||
802 GET_ATTRIB(pkt
->insn
[i
].opcode
, A_MEMLIKE_PACKET_RULES
)) &&
805 pkt
->insn
[i
].slot
= 0;
809 /* Next memory instruction always goes to slot 1 */
810 if ((GET_ATTRIB(pkt
->insn
[i
].opcode
, A_MEMLIKE
) ||
811 GET_ATTRIB(pkt
->insn
[i
].opcode
, A_MEMLIKE_PACKET_RULES
)) &&
813 pkt
->insn
[i
].slot
= 1;
817 /* Fix the exceptions - duplex always slot 0,1 */
818 for (i
= pkt
->num_insns
- 1; i
>= 0; i
--) {
819 /* First subinsn always goes to slot 0 */
820 if (GET_ATTRIB(pkt
->insn
[i
].opcode
, A_SUBINSN
) && !hit_duplex
) {
822 pkt
->insn
[i
].slot
= 0;
826 /* Next subinsn always goes to slot 1 */
827 if (GET_ATTRIB(pkt
->insn
[i
].opcode
, A_SUBINSN
) && hit_duplex
) {
828 pkt
->insn
[i
].slot
= 1;
832 /* Fix the exceptions - slot 1 is never empty, always aligns to slot 0 */
836 for (i
= pkt
->num_insns
- 1; i
>= 0; i
--) {
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
);
844 * Make sure it's not endloop since, we're overloading
852 if (pkt
->insn
[i
].slot
== 1) {
857 /* Is slot0 empty and slot1 used? */
858 if ((slot0_found
== 0) && (slot1_found
== 1)) {
859 /* Then push it to slot0 */
860 pkt
->insn
[slot1_iidx
].slot
= 0;
866 * Decodes packet with given words
867 * Returns 0 on insufficient words,
868 * or number of words used on success
871 int decode_packet(int max_words
, const uint32_t *words
, Packet
*pkt
,
876 int end_of_packet
= 0;
881 memset(pkt
, 0, sizeof(*pkt
));
882 /* Try to build packet */
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);
889 * If we saw an extender, mark next word extended so immediate
892 if (pkt
->insn
[num_insns
].opcode
== A4_ext
) {
893 pkt
->insn
[num_insns
+ 1].extension_valid
= 1;
895 num_insns
+= new_insns
;
899 pkt
->num_insns
= num_insns
;
900 if (!end_of_packet
) {
901 /* Ran out of words! */
904 pkt
->encod_pkt_size_in_bytes
= words_read
* 4;
907 * Check for :endloop in the parse bits
908 * Section 10.6 of the Programmer's Reference describes the encoding
909 * The end of hardware loop 0 can be encoded with 2 words
910 * The end of hardware loop 1 needs 3 words
912 if ((words_read
== 2) && (decode_parsebits_is_loopend(words
[0]))) {
913 decode_add_endloop_insn(&pkt
->insn
[pkt
->num_insns
++], 0);
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);
928 decode_apply_extenders(pkt
);
930 decode_remove_extenders(pkt
);
932 decode_set_slot_number(pkt
);
933 decode_fill_newvalue_regno(pkt
);
936 decode_shuffle_for_execution(pkt
);
937 decode_split_cmpjump(pkt
);
938 decode_set_insn_attr_fields(pkt
);
944 /* Used for "-d in_asm" logging */
945 int disassemble_hexagon(uint32_t *words
, int nwords
, bfd_vma pc
,
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
;
954 g_string_assign(buf
, "<invalid>");