xref: /openbmc/qemu/target/hexagon/decode.c (revision 6f03770d)
1 /*
2  *  Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #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 
29 enum {
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 */
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)
50 
51 #define DECODE_MAPPED_REG(REGNO, NAME) \
52     insn->regno[REGNO] = DECODE_REGISTER_##NAME[insn->regno[REGNO]];
53 
54 typedef 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 
67 typedef 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)                     /* 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 */
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(...)                /* NOTHING */
128 #define DECODE_LEGACY_MATCH_INFO(...)         /* NOTHING */
129 #define DECODE_OPINFO(...)                    /* NOTHING */
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 
146 static 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 
153 static const DectreeTable *ext_trees[XX_LAST_EXT_IDX];
154 
155 static 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 
163 typedef struct {
164     uint32_t mask;
165     uint32_t match;
166 } DecodeITableEntry;
167 
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 */
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(...) /* NOTHING */
188 
189 static 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 
199 static 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 
215 void decode_init(void)
216 {
217     decode_ext_init();
218 }
219 
220 void 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         /* Move towards end */
230         direction = 1;
231     } else {
232         /* move towards beginning */
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 /* Fill newvalue registers with the correct regno */
243 static void
244 decode_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             /* 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];
262             }
263 
264             /*
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.
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              * Check for a badly encoded N-field which points to an instruction
279              * out-of-range
280              */
281             g_assert(!((def_idx < 0) || (def_idx > (packet->num_insns - 1))));
282 
283             /*
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
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             /* 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];
316             /*
317              * We need to remember who produces this value to later
318              * check if it was dynamically cancelled
319              */
320             packet->insn[i].new_value_producer_slot =
321                 packet->insn[def_idx].slot;
322         }
323     }
324 }
325 
326 /* Split CJ into a compare and a jump */
327 static void decode_split_cmpjump(Packet *pkt)
328 {
329     int last, i;
330     int numinsns = pkt->num_insns;
331 
332     /*
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
336      * original order.
337      */
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 */
345             pkt->num_insns++;
346         }
347     }
348 
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);
353         }
354     }
355 }
356 
357 static 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         /* Exception to A_JUMP attribute */
364         if (opcode == J4_hintjumpr) {
365             return 0;
366         }
367         return 1;
368     }
369 
370     return 0;
371 }
372 
373 static 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 /* Set the is_* fields in each instruction */
380 static 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;    /* Skip compare of cmp-jumps */
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  * Shuffle for execution
420  * Move stores to end (in same order as encoding)
421  * Move compares to beginning (for use by .new insns)
422  */
423 static void decode_shuffle_for_execution(Packet *packet)
424 {
425     int changed = 0;
426     int i;
427     int flag;    /* flag means we've seen a non-memory instruction */
428     int n_mems;
429     int last_insn = packet->num_insns - 1;
430 
431     /*
432      * Skip end loops, somehow an end loop is getting in and messing
433      * up the order
434      */
435     if (decode_opcode_ends_loop(packet->insn[last_insn].opcode)) {
436         last_insn--;
437     }
438 
439     do {
440         changed = 0;
441         /*
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.
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                  * Don't set flag, since we don't want to shuffle a
460                  * store past a load
461                  */
462                 n_mems++;
463             } else if (GET_ATTRIB(opcode, A_DOTNEWVALUE)) {
464                 /*
465                  * Don't set flag, since we don't want to shuffle past
466                  * a .new value
467                  */
468             } else {
469                 flag = 1;
470             }
471         }
472 
473         if (changed) {
474             continue;
475         }
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;
479 
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) */
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                  * spNloop instruction
493                  * Don't reorder endloops; they are not valid for .new uses,
494                  * and we want to match HW
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      * If we have a .new register compare/branch, move that to the very
519      * very end, past stores
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 
529 static void
530 apply_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 
541 static 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 
552 static 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             /* Remove this one by moving the remaining instructions down */
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 
568 static 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)     /* 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 */
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  * Fill in the operands of the instruction
614  * dectree_generated.h.inc has a DECODE_OPINFO entry for each opcode
615  * For example,
616  *     DECODE_OPINFO(A2_addi,
617  *          DECODE_REG(0,5,0)
618  *          DECODE_REG(1,5,16)
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.
624  */
625 static void
626 decode_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 
662 static unsigned int
663 decode_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 
688 static unsigned int get_insn_a(uint32_t encoding)
689 {
690     return extract32(encoding, 0, 13);
691 }
692 
693 static unsigned int get_insn_b(uint32_t encoding)
694 {
695     return extract32(encoding, 16, 13);
696 }
697 
698 static unsigned int
699 decode_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 
737 static unsigned int
738 decode_insns(Insn *insn, uint32_t encoding)
739 {
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;
744     } else {
745         /* start with EE table - duplex instructions */
746         table = &dectree_table_DECODE_ROOT_EE;
747     }
748     return decode_insns_tablewalk(insn, table, encoding);
749 }
750 
751 static 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 
767 static inline int decode_parsebits_is_loopend(uint32_t encoding32)
768 {
769     uint32_t bits = parse_bits(encoding32);
770     return bits == 0x2;
771 }
772 
773 static void
774 decode_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      * The slots are encoded in reverse order
783      * For each instruction, count down until you find a suitable slot
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             /* I've assigned the slot, now decrement it for the next insn */
794             slot--;
795         }
796     }
797 
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)) &&
803             !hit_mem_insn) {
804             hit_mem_insn = 1;
805             pkt->insn[i].slot = 0;
806             continue;
807         }
808 
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)) &&
812             hit_mem_insn) {
813             pkt->insn[i].slot = 1;
814         }
815     }
816 
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) {
821             hit_duplex = 1;
822             pkt->insn[i].slot = 0;
823             continue;
824         }
825 
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;
829         }
830     }
831 
832     /* Fix the exceptions - slot 1 is never empty, always aligns to slot 0 */
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         /* Is slot0 used? */
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              * Make sure it's not endloop since, we're overloading
845              * slot0 for endloop
846              */
847             if (!is_endloop) {
848                 slot0_found = 1;
849             }
850         }
851         /* Is slot1 used? */
852         if (pkt->insn[i].slot == 1) {
853             slot1_found = 1;
854             slot1_iidx = i;
855         }
856     }
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;
861     }
862 }
863 
864 /*
865  * decode_packet
866  * Decodes packet with given words
867  * Returns 0 on insufficient words,
868  * or number of words used on success
869  */
870 
871 int 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     /* Initialize */
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);
888         /*
889          * If we saw an extender, mark next word extended so immediate
890          * decode works
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         /* Ran out of words! */
902         return 0;
903     }
904     pkt->encod_pkt_size_in_bytes = words_read * 4;
905 
906     /*
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
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 /* Used for "-d in_asm" logging */
945 int 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