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 "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 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(OPNUM, NAME) \ 52 insn->regno[OPNUM] = DECODE_REGISTER_##NAME[insn->regno[OPNUM]]; 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 = true; /* last insn does the CMP */ 344 pkt->insn[i].part1 = false; /* existing insn 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 bool 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 false; 366 } 367 return true; 368 } 369 370 return false; 371 } 372 373 static bool 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 = false; 387 pkt->pkt_has_endloop = false; 388 pkt->pkt_has_dczeroa = false; 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 = true; 398 } 399 400 if (GET_ATTRIB(opcode, A_STORE)) { 401 if (pkt->insn[i].slot == 0) { 402 pkt->pkt_has_store_s0 = true; 403 } else { 404 pkt->pkt_has_store_s1 = true; 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 bool changed = false; 426 int i; 427 bool 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 = false; 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 = false, 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 = true; 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 = true; 470 } 471 } 472 473 if (changed) { 474 continue; 475 } 476 /* Compares go first, may be reordered wrt each other */ 477 for (flag = false, 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 = true; 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 = true; 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 = true; 506 continue; 507 } 508 } else { 509 flag = true; 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 = true; 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 if (GET_ATTRIB(pkt->insn[slot].opcode, A_EXTENSION)) { 571 return mmvec_ext_decode_find_iclass_slots(pkt->insn[slot].opcode); 572 } else { 573 return find_iclass_slots(pkt->insn[slot].opcode, 574 pkt->insn[slot].iclass); 575 } 576 } 577 578 #define DECODE_NEW_TABLE(TAG, SIZE, WHATNOT) /* NOTHING */ 579 #define TABLE_LINK(TABLE) /* NOTHING */ 580 #define TERMINAL(TAG, ENC) /* NOTHING */ 581 #define SUBINSNS(TAG, CLASSA, CLASSB, ENC) /* NOTHING */ 582 #define EXTSPACE(TAG, ENC) /* NOTHING */ 583 #define INVALID() /* NOTHING */ 584 #define DECODE_END_TABLE(...) /* NOTHING */ 585 #define DECODE_MATCH_INFO(...) /* NOTHING */ 586 #define DECODE_LEGACY_MATCH_INFO(...) /* NOTHING */ 587 588 #define DECODE_REG(REGNO, WIDTH, STARTBIT) \ 589 insn->regno[REGNO] = ((encoding >> STARTBIT) & ((1 << WIDTH) - 1)); 590 591 #define DECODE_IMPL_REG(REGNO, VAL) \ 592 insn->regno[REGNO] = VAL; 593 594 #define DECODE_IMM(IMMNO, WIDTH, STARTBIT, VALSTART) \ 595 insn->immed[IMMNO] |= (((encoding >> STARTBIT) & ((1 << WIDTH) - 1))) << \ 596 (VALSTART); 597 598 #define DECODE_IMM_SXT(IMMNO, WIDTH) \ 599 insn->immed[IMMNO] = ((((int32_t)insn->immed[IMMNO]) << (32 - WIDTH)) >> \ 600 (32 - WIDTH)); 601 602 #define DECODE_IMM_NEG(IMMNO, WIDTH) \ 603 insn->immed[IMMNO] = -insn->immed[IMMNO]; 604 605 #define DECODE_IMM_SHIFT(IMMNO, SHAMT) \ 606 if ((!insn->extension_valid) || \ 607 (insn->which_extended != IMMNO)) { \ 608 insn->immed[IMMNO] <<= SHAMT; \ 609 } 610 611 #define DECODE_OPINFO(TAG, BEH) \ 612 case TAG: \ 613 { BEH } \ 614 break; \ 615 616 /* 617 * Fill in the operands of the instruction 618 * dectree_generated.h.inc has a DECODE_OPINFO entry for each opcode 619 * For example, 620 * DECODE_OPINFO(A2_addi, 621 * DECODE_REG(0,5,0) 622 * DECODE_REG(1,5,16) 623 * DECODE_IMM(0,7,21,9) 624 * DECODE_IMM(0,9,5,0) 625 * DECODE_IMM_SXT(0,16) 626 * with the macros defined above, we'll fill in a switch statement 627 * where each case is an opcode tag. 628 */ 629 static void 630 decode_op(Insn *insn, Opcode tag, uint32_t encoding) 631 { 632 insn->immed[0] = 0; 633 insn->immed[1] = 0; 634 insn->opcode = tag; 635 if (insn->extension_valid) { 636 insn->which_extended = opcode_which_immediate_is_extended(tag); 637 } 638 639 switch (tag) { 640 #include "dectree_generated.h.inc" 641 default: 642 break; 643 } 644 645 insn->generate = opcode_genptr[tag]; 646 647 insn->iclass = iclass_bits(encoding); 648 } 649 650 #undef DECODE_REG 651 #undef DECODE_IMPL_REG 652 #undef DECODE_IMM 653 #undef DECODE_IMM_SHIFT 654 #undef DECODE_OPINFO 655 #undef DECODE_MATCH_INFO 656 #undef DECODE_LEGACY_MATCH_INFO 657 #undef DECODE_END_TABLE 658 #undef INVALID 659 #undef TERMINAL 660 #undef SUBINSNS 661 #undef EXTSPACE 662 #undef TABLE_LINK 663 #undef DECODE_NEW_TABLE 664 #undef DECODE_SEPARATOR_BITS 665 666 static unsigned int 667 decode_subinsn_tablewalk(Insn *insn, const DectreeTable *table, 668 uint32_t encoding) 669 { 670 unsigned int i; 671 Opcode opc; 672 if (table->lookup_function) { 673 i = table->lookup_function(table->startbit, table->width, encoding); 674 } else { 675 i = extract32(encoding, table->startbit, table->width); 676 } 677 if (table->table[i].type == DECTREE_TABLE_LINK) { 678 return decode_subinsn_tablewalk(insn, table->table[i].table_link, 679 encoding); 680 } else if (table->table[i].type == DECTREE_TERMINAL) { 681 opc = table->table[i].opcode; 682 if ((encoding & decode_itable[opc].mask) != decode_itable[opc].match) { 683 return 0; 684 } 685 decode_op(insn, opc, encoding); 686 return 1; 687 } else { 688 return 0; 689 } 690 } 691 692 static unsigned int get_insn_a(uint32_t encoding) 693 { 694 return extract32(encoding, 0, 13); 695 } 696 697 static unsigned int get_insn_b(uint32_t encoding) 698 { 699 return extract32(encoding, 16, 13); 700 } 701 702 static unsigned int 703 decode_insns_tablewalk(Insn *insn, const DectreeTable *table, 704 uint32_t encoding) 705 { 706 unsigned int i; 707 unsigned int a, b; 708 Opcode opc; 709 if (table->lookup_function) { 710 i = table->lookup_function(table->startbit, table->width, encoding); 711 } else { 712 i = extract32(encoding, table->startbit, table->width); 713 } 714 if (table->table[i].type == DECTREE_TABLE_LINK) { 715 return decode_insns_tablewalk(insn, table->table[i].table_link, 716 encoding); 717 } else if (table->table[i].type == DECTREE_SUBINSNS) { 718 a = get_insn_a(encoding); 719 b = get_insn_b(encoding); 720 b = decode_subinsn_tablewalk(insn, table->table[i].table_link_b, b); 721 a = decode_subinsn_tablewalk(insn + 1, table->table[i].table_link, a); 722 if ((a == 0) || (b == 0)) { 723 return 0; 724 } 725 return 2; 726 } else if (table->table[i].type == DECTREE_TERMINAL) { 727 opc = table->table[i].opcode; 728 if ((encoding & decode_itable[opc].mask) != decode_itable[opc].match) { 729 if ((encoding & decode_legacy_itable[opc].mask) != 730 decode_legacy_itable[opc].match) { 731 return 0; 732 } 733 } 734 decode_op(insn, opc, encoding); 735 return 1; 736 } else if (table->table[i].type == DECTREE_EXTSPACE) { 737 /* 738 * For now, HVX will be the only coproc 739 */ 740 return decode_insns_tablewalk(insn, ext_trees[EXT_IDX_mmvec], encoding); 741 } else { 742 return 0; 743 } 744 } 745 746 static unsigned int 747 decode_insns(Insn *insn, uint32_t encoding) 748 { 749 const DectreeTable *table; 750 if (parse_bits(encoding) != 0) { 751 /* Start with PP table - 32 bit instructions */ 752 table = &dectree_table_DECODE_ROOT_32; 753 } else { 754 /* start with EE table - duplex instructions */ 755 table = &dectree_table_DECODE_ROOT_EE; 756 } 757 return decode_insns_tablewalk(insn, table, encoding); 758 } 759 760 static void decode_add_endloop_insn(Insn *insn, int loopnum) 761 { 762 if (loopnum == 10) { 763 insn->opcode = J2_endloop01; 764 insn->generate = opcode_genptr[J2_endloop01]; 765 } else if (loopnum == 1) { 766 insn->opcode = J2_endloop1; 767 insn->generate = opcode_genptr[J2_endloop1]; 768 } else if (loopnum == 0) { 769 insn->opcode = J2_endloop0; 770 insn->generate = opcode_genptr[J2_endloop0]; 771 } else { 772 g_assert_not_reached(); 773 } 774 } 775 776 static bool decode_parsebits_is_loopend(uint32_t encoding32) 777 { 778 uint32_t bits = parse_bits(encoding32); 779 return bits == 0x2; 780 } 781 782 static void 783 decode_set_slot_number(Packet *pkt) 784 { 785 int slot; 786 int i; 787 bool hit_mem_insn = false; 788 bool hit_duplex = false; 789 bool slot0_found = false; 790 bool slot1_found = false; 791 int slot1_iidx = 0; 792 793 /* 794 * The slots are encoded in reverse order 795 * For each instruction, count down until you find a suitable slot 796 */ 797 for (i = 0, slot = 3; i < pkt->num_insns; i++) { 798 SlotMask valid_slots = get_valid_slots(pkt, i); 799 800 while (!(valid_slots & (1 << slot))) { 801 slot--; 802 } 803 pkt->insn[i].slot = slot; 804 if (slot) { 805 /* I've assigned the slot, now decrement it for the next insn */ 806 slot--; 807 } 808 } 809 810 /* Fix the exceptions - mem insns to slot 0,1 */ 811 for (i = pkt->num_insns - 1; i >= 0; i--) { 812 /* First memory instruction always goes to slot 0 */ 813 if ((GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE) || 814 GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE_PACKET_RULES)) && 815 !hit_mem_insn) { 816 hit_mem_insn = true; 817 pkt->insn[i].slot = 0; 818 continue; 819 } 820 821 /* Next memory instruction always goes to slot 1 */ 822 if ((GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE) || 823 GET_ATTRIB(pkt->insn[i].opcode, A_MEMLIKE_PACKET_RULES)) && 824 hit_mem_insn) { 825 pkt->insn[i].slot = 1; 826 } 827 } 828 829 /* Fix the exceptions - duplex always slot 0,1 */ 830 for (i = pkt->num_insns - 1; i >= 0; i--) { 831 /* First subinsn always goes to slot 0 */ 832 if (GET_ATTRIB(pkt->insn[i].opcode, A_SUBINSN) && !hit_duplex) { 833 hit_duplex = true; 834 pkt->insn[i].slot = 0; 835 continue; 836 } 837 838 /* Next subinsn always goes to slot 1 */ 839 if (GET_ATTRIB(pkt->insn[i].opcode, A_SUBINSN) && hit_duplex) { 840 pkt->insn[i].slot = 1; 841 } 842 } 843 844 /* Fix the exceptions - slot 1 is never empty, always aligns to slot 0 */ 845 for (i = pkt->num_insns - 1; i >= 0; i--) { 846 /* Is slot0 used? */ 847 if (pkt->insn[i].slot == 0) { 848 bool is_endloop = (pkt->insn[i].opcode == J2_endloop01); 849 is_endloop |= (pkt->insn[i].opcode == J2_endloop0); 850 is_endloop |= (pkt->insn[i].opcode == J2_endloop1); 851 852 /* 853 * Make sure it's not endloop since, we're overloading 854 * slot0 for endloop 855 */ 856 if (!is_endloop) { 857 slot0_found = true; 858 } 859 } 860 /* Is slot1 used? */ 861 if (pkt->insn[i].slot == 1) { 862 slot1_found = true; 863 slot1_iidx = i; 864 } 865 } 866 /* Is slot0 empty and slot1 used? */ 867 if ((!slot0_found) && slot1_found) { 868 /* Then push it to slot0 */ 869 pkt->insn[slot1_iidx].slot = 0; 870 } 871 } 872 873 /* 874 * decode_packet 875 * Decodes packet with given words 876 * Returns 0 on insufficient words, 877 * or number of words used on success 878 */ 879 880 int decode_packet(int max_words, const uint32_t *words, Packet *pkt, 881 bool disas_only) 882 { 883 int num_insns = 0; 884 int words_read = 0; 885 bool end_of_packet = false; 886 int new_insns = 0; 887 int i; 888 uint32_t encoding32; 889 890 /* Initialize */ 891 memset(pkt, 0, sizeof(*pkt)); 892 /* Try to build packet */ 893 while (!end_of_packet && (words_read < max_words)) { 894 encoding32 = words[words_read]; 895 end_of_packet = is_packet_end(encoding32); 896 new_insns = decode_insns(&pkt->insn[num_insns], encoding32); 897 g_assert(new_insns > 0); 898 /* 899 * If we saw an extender, mark next word extended so immediate 900 * decode works 901 */ 902 if (pkt->insn[num_insns].opcode == A4_ext) { 903 pkt->insn[num_insns + 1].extension_valid = true; 904 } 905 num_insns += new_insns; 906 words_read++; 907 } 908 909 pkt->num_insns = num_insns; 910 if (!end_of_packet) { 911 /* Ran out of words! */ 912 return 0; 913 } 914 pkt->encod_pkt_size_in_bytes = words_read * 4; 915 pkt->pkt_has_hvx = false; 916 for (i = 0; i < num_insns; i++) { 917 pkt->pkt_has_hvx |= 918 GET_ATTRIB(pkt->insn[i].opcode, A_CVI); 919 } 920 921 /* 922 * Check for :endloop in the parse bits 923 * Section 10.6 of the Programmer's Reference describes the encoding 924 * The end of hardware loop 0 can be encoded with 2 words 925 * The end of hardware loop 1 needs 3 words 926 */ 927 if ((words_read == 2) && (decode_parsebits_is_loopend(words[0]))) { 928 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 0); 929 } 930 if (words_read >= 3) { 931 bool has_loop0, has_loop1; 932 has_loop0 = decode_parsebits_is_loopend(words[0]); 933 has_loop1 = decode_parsebits_is_loopend(words[1]); 934 if (has_loop0 && has_loop1) { 935 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 10); 936 } else if (has_loop1) { 937 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 1); 938 } else if (has_loop0) { 939 decode_add_endloop_insn(&pkt->insn[pkt->num_insns++], 0); 940 } 941 } 942 943 decode_apply_extenders(pkt); 944 if (!disas_only) { 945 decode_remove_extenders(pkt); 946 } 947 decode_set_slot_number(pkt); 948 decode_fill_newvalue_regno(pkt); 949 950 if (pkt->pkt_has_hvx) { 951 mmvec_ext_decode_checks(pkt, disas_only); 952 } 953 954 if (!disas_only) { 955 decode_shuffle_for_execution(pkt); 956 decode_split_cmpjump(pkt); 957 decode_set_insn_attr_fields(pkt); 958 } 959 960 return words_read; 961 } 962 963 /* Used for "-d in_asm" logging */ 964 int disassemble_hexagon(uint32_t *words, int nwords, bfd_vma pc, 965 GString *buf) 966 { 967 Packet pkt; 968 969 if (decode_packet(nwords, words, &pkt, true) > 0) { 970 snprint_a_pkt_disas(buf, &pkt, words, pc); 971 return pkt.encod_pkt_size_in_bytes; 972 } else { 973 g_string_assign(buf, "<invalid>"); 974 return 0; 975 } 976 } 977