1 /* aarch64-dis.c -- AArch64 disassembler. 2 Copyright (C) 2009-2014 Free Software Foundation, Inc. 3 Contributed by ARM Ltd. 4 5 This file is part of the GNU opcodes library. 6 7 This library is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 It is distributed in the hope that it will be useful, but WITHOUT 13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 15 License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; see the file COPYING3. If not, 19 see <http://www.gnu.org/licenses/>. */ 20 21 #include "sysdep.h" 22 #include "bfd_stdint.h" 23 #include "dis-asm.h" 24 #include "libiberty.h" 25 #include "opintl.h" 26 #include "aarch64-dis.h" 27 #include "elf-bfd.h" 28 29 #define ERR_OK 0 30 #define ERR_UND -1 31 #define ERR_UNP -3 32 #define ERR_NYI -5 33 34 #define INSNLEN 4 35 36 /* Cached mapping symbol state. */ 37 enum map_type 38 { 39 MAP_INSN, 40 MAP_DATA 41 }; 42 43 static enum map_type last_type; 44 static int last_mapping_sym = -1; 45 static bfd_vma last_mapping_addr = 0; 46 47 /* Other options */ 48 static int no_aliases = 0; /* If set disassemble as most general inst. */ 49 50 52 static void 53 set_default_aarch64_dis_options (struct disassemble_info *info ATTRIBUTE_UNUSED) 54 { 55 } 56 57 static void 58 parse_aarch64_dis_option (const char *option, unsigned int len ATTRIBUTE_UNUSED) 59 { 60 /* Try to match options that are simple flags */ 61 if (CONST_STRNEQ (option, "no-aliases")) 62 { 63 no_aliases = 1; 64 return; 65 } 66 67 if (CONST_STRNEQ (option, "aliases")) 68 { 69 no_aliases = 0; 70 return; 71 } 72 73 #ifdef DEBUG_AARCH64 74 if (CONST_STRNEQ (option, "debug_dump")) 75 { 76 debug_dump = 1; 77 return; 78 } 79 #endif /* DEBUG_AARCH64 */ 80 81 /* Invalid option. */ 82 fprintf (stderr, _("Unrecognised disassembler option: %s\n"), option); 83 } 84 85 static void 86 parse_aarch64_dis_options (const char *options) 87 { 88 const char *option_end; 89 90 if (options == NULL) 91 return; 92 93 while (*options != '\0') 94 { 95 /* Skip empty options. */ 96 if (*options == ',') 97 { 98 options++; 99 continue; 100 } 101 102 /* We know that *options is neither NUL or a comma. */ 103 option_end = options + 1; 104 while (*option_end != ',' && *option_end != '\0') 105 option_end++; 106 107 parse_aarch64_dis_option (options, option_end - options); 108 109 /* Go on to the next one. If option_end points to a comma, it 110 will be skipped above. */ 111 options = option_end; 112 } 113 } 114 115 /* Functions doing the instruction disassembling. */ 117 118 /* The unnamed arguments consist of the number of fields and information about 119 these fields where the VALUE will be extracted from CODE and returned. 120 MASK can be zero or the base mask of the opcode. 121 122 N.B. the fields are required to be in such an order than the most signficant 123 field for VALUE comes the first, e.g. the <index> in 124 SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>] 125 is encoded in H:L:M in some cases, the fields H:L:M should be passed in 126 the order of H, L, M. */ 127 128 static inline aarch64_insn 129 extract_fields (aarch64_insn code, aarch64_insn mask, ...) 130 { 131 uint32_t num; 132 const aarch64_field *field; 133 enum aarch64_field_kind kind; 134 va_list va; 135 136 va_start (va, mask); 137 num = va_arg (va, uint32_t); 138 assert (num <= 5); 139 aarch64_insn value = 0x0; 140 while (num--) 141 { 142 kind = va_arg (va, enum aarch64_field_kind); 143 field = &fields[kind]; 144 value <<= field->width; 145 value |= extract_field (kind, code, mask); 146 } 147 return value; 148 } 149 150 /* Sign-extend bit I of VALUE. */ 151 static inline int32_t 152 sign_extend (aarch64_insn value, unsigned i) 153 { 154 uint32_t ret = value; 155 156 assert (i < 32); 157 if ((value >> i) & 0x1) 158 { 159 uint32_t val = (uint32_t)(-1) << i; 160 ret = ret | val; 161 } 162 return (int32_t) ret; 163 } 164 165 /* N.B. the following inline helpfer functions create a dependency on the 166 order of operand qualifier enumerators. */ 167 168 /* Given VALUE, return qualifier for a general purpose register. */ 169 static inline enum aarch64_opnd_qualifier 170 get_greg_qualifier_from_value (aarch64_insn value) 171 { 172 enum aarch64_opnd_qualifier qualifier = AARCH64_OPND_QLF_W + value; 173 assert (value <= 0x1 174 && aarch64_get_qualifier_standard_value (qualifier) == value); 175 return qualifier; 176 } 177 178 /* Given VALUE, return qualifier for a vector register. */ 179 static inline enum aarch64_opnd_qualifier 180 get_vreg_qualifier_from_value (aarch64_insn value) 181 { 182 enum aarch64_opnd_qualifier qualifier = AARCH64_OPND_QLF_V_8B + value; 183 184 assert (value <= 0x8 185 && aarch64_get_qualifier_standard_value (qualifier) == value); 186 return qualifier; 187 } 188 189 /* Given VALUE, return qualifier for an FP or AdvSIMD scalar register. */ 190 static inline enum aarch64_opnd_qualifier 191 get_sreg_qualifier_from_value (aarch64_insn value) 192 { 193 enum aarch64_opnd_qualifier qualifier = AARCH64_OPND_QLF_S_B + value; 194 195 assert (value <= 0x4 196 && aarch64_get_qualifier_standard_value (qualifier) == value); 197 return qualifier; 198 } 199 200 /* Given the instruction in *INST which is probably half way through the 201 decoding and our caller wants to know the expected qualifier for operand 202 I. Return such a qualifier if we can establish it; otherwise return 203 AARCH64_OPND_QLF_NIL. */ 204 205 static aarch64_opnd_qualifier_t 206 get_expected_qualifier (const aarch64_inst *inst, int i) 207 { 208 aarch64_opnd_qualifier_seq_t qualifiers; 209 /* Should not be called if the qualifier is known. */ 210 assert (inst->operands[i].qualifier == AARCH64_OPND_QLF_NIL); 211 if (aarch64_find_best_match (inst, inst->opcode->qualifiers_list, 212 i, qualifiers)) 213 return qualifiers[i]; 214 else 215 return AARCH64_OPND_QLF_NIL; 216 } 217 218 /* Operand extractors. */ 219 220 int 221 aarch64_ext_regno (const aarch64_operand *self, aarch64_opnd_info *info, 222 const aarch64_insn code, 223 const aarch64_inst *inst ATTRIBUTE_UNUSED) 224 { 225 info->reg.regno = extract_field (self->fields[0], code, 0); 226 return 1; 227 } 228 229 int 230 aarch64_ext_regno_pair (const aarch64_operand *self ATTRIBUTE_UNUSED, aarch64_opnd_info *info, 231 const aarch64_insn code ATTRIBUTE_UNUSED, 232 const aarch64_inst *inst ATTRIBUTE_UNUSED) 233 { 234 assert (info->idx == 1 235 || info->idx ==3); 236 info->reg.regno = inst->operands[info->idx - 1].reg.regno + 1; 237 return 1; 238 } 239 240 /* e.g. IC <ic_op>{, <Xt>}. */ 241 int 242 aarch64_ext_regrt_sysins (const aarch64_operand *self, aarch64_opnd_info *info, 243 const aarch64_insn code, 244 const aarch64_inst *inst ATTRIBUTE_UNUSED) 245 { 246 info->reg.regno = extract_field (self->fields[0], code, 0); 247 assert (info->idx == 1 248 && (aarch64_get_operand_class (inst->operands[0].type) 249 == AARCH64_OPND_CLASS_SYSTEM)); 250 /* This will make the constraint checking happy and more importantly will 251 help the disassembler determine whether this operand is optional or 252 not. */ 253 info->present = inst->operands[0].sysins_op->has_xt; 254 255 return 1; 256 } 257 258 /* e.g. SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>]. */ 259 int 260 aarch64_ext_reglane (const aarch64_operand *self, aarch64_opnd_info *info, 261 const aarch64_insn code, 262 const aarch64_inst *inst ATTRIBUTE_UNUSED) 263 { 264 /* regno */ 265 info->reglane.regno = extract_field (self->fields[0], code, 266 inst->opcode->mask); 267 268 /* Index and/or type. */ 269 if (inst->opcode->iclass == asisdone 270 || inst->opcode->iclass == asimdins) 271 { 272 if (info->type == AARCH64_OPND_En 273 && inst->opcode->operands[0] == AARCH64_OPND_Ed) 274 { 275 unsigned shift; 276 /* index2 for e.g. INS <Vd>.<Ts>[<index1>], <Vn>.<Ts>[<index2>]. */ 277 assert (info->idx == 1); /* Vn */ 278 aarch64_insn value = extract_field (FLD_imm4, code, 0); 279 /* Depend on AARCH64_OPND_Ed to determine the qualifier. */ 280 info->qualifier = get_expected_qualifier (inst, info->idx); 281 shift = get_logsz (aarch64_get_qualifier_esize (info->qualifier)); 282 info->reglane.index = value >> shift; 283 } 284 else 285 { 286 /* index and type for e.g. DUP <V><d>, <Vn>.<T>[<index>]. 287 imm5<3:0> <V> 288 0000 RESERVED 289 xxx1 B 290 xx10 H 291 x100 S 292 1000 D */ 293 int pos = -1; 294 aarch64_insn value = extract_field (FLD_imm5, code, 0); 295 while (++pos <= 3 && (value & 0x1) == 0) 296 value >>= 1; 297 if (pos > 3) 298 return 0; 299 info->qualifier = get_sreg_qualifier_from_value (pos); 300 info->reglane.index = (unsigned) (value >> 1); 301 } 302 } 303 else 304 { 305 /* Index only for e.g. SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>] 306 or SQDMLAL <Va><d>, <Vb><n>, <Vm>.<Ts>[<index>]. */ 307 308 /* Need information in other operand(s) to help decoding. */ 309 info->qualifier = get_expected_qualifier (inst, info->idx); 310 switch (info->qualifier) 311 { 312 case AARCH64_OPND_QLF_S_H: 313 /* h:l:m */ 314 info->reglane.index = extract_fields (code, 0, 3, FLD_H, FLD_L, 315 FLD_M); 316 info->reglane.regno &= 0xf; 317 break; 318 case AARCH64_OPND_QLF_S_S: 319 /* h:l */ 320 info->reglane.index = extract_fields (code, 0, 2, FLD_H, FLD_L); 321 break; 322 case AARCH64_OPND_QLF_S_D: 323 /* H */ 324 info->reglane.index = extract_field (FLD_H, code, 0); 325 break; 326 default: 327 return 0; 328 } 329 } 330 331 return 1; 332 } 333 334 int 335 aarch64_ext_reglist (const aarch64_operand *self, aarch64_opnd_info *info, 336 const aarch64_insn code, 337 const aarch64_inst *inst ATTRIBUTE_UNUSED) 338 { 339 /* R */ 340 info->reglist.first_regno = extract_field (self->fields[0], code, 0); 341 /* len */ 342 info->reglist.num_regs = extract_field (FLD_len, code, 0) + 1; 343 return 1; 344 } 345 346 /* Decode Rt and opcode fields of Vt in AdvSIMD load/store instructions. */ 347 int 348 aarch64_ext_ldst_reglist (const aarch64_operand *self ATTRIBUTE_UNUSED, 349 aarch64_opnd_info *info, const aarch64_insn code, 350 const aarch64_inst *inst) 351 { 352 aarch64_insn value; 353 /* Number of elements in each structure to be loaded/stored. */ 354 unsigned expected_num = get_opcode_dependent_value (inst->opcode); 355 356 struct 357 { 358 unsigned is_reserved; 359 unsigned num_regs; 360 unsigned num_elements; 361 } data [] = 362 { {0, 4, 4}, 363 {1, 4, 4}, 364 {0, 4, 1}, 365 {0, 4, 2}, 366 {0, 3, 3}, 367 {1, 3, 3}, 368 {0, 3, 1}, 369 {0, 1, 1}, 370 {0, 2, 2}, 371 {1, 2, 2}, 372 {0, 2, 1}, 373 }; 374 375 /* Rt */ 376 info->reglist.first_regno = extract_field (FLD_Rt, code, 0); 377 /* opcode */ 378 value = extract_field (FLD_opcode, code, 0); 379 if (expected_num != data[value].num_elements || data[value].is_reserved) 380 return 0; 381 info->reglist.num_regs = data[value].num_regs; 382 383 return 1; 384 } 385 386 /* Decode Rt and S fields of Vt in AdvSIMD load single structure to all 387 lanes instructions. */ 388 int 389 aarch64_ext_ldst_reglist_r (const aarch64_operand *self ATTRIBUTE_UNUSED, 390 aarch64_opnd_info *info, const aarch64_insn code, 391 const aarch64_inst *inst) 392 { 393 aarch64_insn value; 394 395 /* Rt */ 396 info->reglist.first_regno = extract_field (FLD_Rt, code, 0); 397 /* S */ 398 value = extract_field (FLD_S, code, 0); 399 400 /* Number of registers is equal to the number of elements in 401 each structure to be loaded/stored. */ 402 info->reglist.num_regs = get_opcode_dependent_value (inst->opcode); 403 assert (info->reglist.num_regs >= 1 && info->reglist.num_regs <= 4); 404 405 /* Except when it is LD1R. */ 406 if (info->reglist.num_regs == 1 && value == (aarch64_insn) 1) 407 info->reglist.num_regs = 2; 408 409 return 1; 410 } 411 412 /* Decode Q, opcode<2:1>, S, size and Rt fields of Vt in AdvSIMD 413 load/store single element instructions. */ 414 int 415 aarch64_ext_ldst_elemlist (const aarch64_operand *self ATTRIBUTE_UNUSED, 416 aarch64_opnd_info *info, const aarch64_insn code, 417 const aarch64_inst *inst ATTRIBUTE_UNUSED) 418 { 419 aarch64_field field = {0, 0}; 420 aarch64_insn QSsize; /* fields Q:S:size. */ 421 aarch64_insn opcodeh2; /* opcode<2:1> */ 422 423 /* Rt */ 424 info->reglist.first_regno = extract_field (FLD_Rt, code, 0); 425 426 /* Decode the index, opcode<2:1> and size. */ 427 gen_sub_field (FLD_asisdlso_opcode, 1, 2, &field); 428 opcodeh2 = extract_field_2 (&field, code, 0); 429 QSsize = extract_fields (code, 0, 3, FLD_Q, FLD_S, FLD_vldst_size); 430 switch (opcodeh2) 431 { 432 case 0x0: 433 info->qualifier = AARCH64_OPND_QLF_S_B; 434 /* Index encoded in "Q:S:size". */ 435 info->reglist.index = QSsize; 436 break; 437 case 0x1: 438 if (QSsize & 0x1) 439 /* UND. */ 440 return 0; 441 info->qualifier = AARCH64_OPND_QLF_S_H; 442 /* Index encoded in "Q:S:size<1>". */ 443 info->reglist.index = QSsize >> 1; 444 break; 445 case 0x2: 446 if ((QSsize >> 1) & 0x1) 447 /* UND. */ 448 return 0; 449 if ((QSsize & 0x1) == 0) 450 { 451 info->qualifier = AARCH64_OPND_QLF_S_S; 452 /* Index encoded in "Q:S". */ 453 info->reglist.index = QSsize >> 2; 454 } 455 else 456 { 457 if (extract_field (FLD_S, code, 0)) 458 /* UND */ 459 return 0; 460 info->qualifier = AARCH64_OPND_QLF_S_D; 461 /* Index encoded in "Q". */ 462 info->reglist.index = QSsize >> 3; 463 } 464 break; 465 default: 466 return 0; 467 } 468 469 info->reglist.has_index = 1; 470 info->reglist.num_regs = 0; 471 /* Number of registers is equal to the number of elements in 472 each structure to be loaded/stored. */ 473 info->reglist.num_regs = get_opcode_dependent_value (inst->opcode); 474 assert (info->reglist.num_regs >= 1 && info->reglist.num_regs <= 4); 475 476 return 1; 477 } 478 479 /* Decode fields immh:immb and/or Q for e.g. 480 SSHR <Vd>.<T>, <Vn>.<T>, #<shift> 481 or SSHR <V><d>, <V><n>, #<shift>. */ 482 483 int 484 aarch64_ext_advsimd_imm_shift (const aarch64_operand *self ATTRIBUTE_UNUSED, 485 aarch64_opnd_info *info, const aarch64_insn code, 486 const aarch64_inst *inst) 487 { 488 int pos; 489 aarch64_insn Q, imm, immh; 490 enum aarch64_insn_class iclass = inst->opcode->iclass; 491 492 immh = extract_field (FLD_immh, code, 0); 493 if (immh == 0) 494 return 0; 495 imm = extract_fields (code, 0, 2, FLD_immh, FLD_immb); 496 pos = 4; 497 /* Get highest set bit in immh. */ 498 while (--pos >= 0 && (immh & 0x8) == 0) 499 immh <<= 1; 500 501 assert ((iclass == asimdshf || iclass == asisdshf) 502 && (info->type == AARCH64_OPND_IMM_VLSR 503 || info->type == AARCH64_OPND_IMM_VLSL)); 504 505 if (iclass == asimdshf) 506 { 507 Q = extract_field (FLD_Q, code, 0); 508 /* immh Q <T> 509 0000 x SEE AdvSIMD modified immediate 510 0001 0 8B 511 0001 1 16B 512 001x 0 4H 513 001x 1 8H 514 01xx 0 2S 515 01xx 1 4S 516 1xxx 0 RESERVED 517 1xxx 1 2D */ 518 info->qualifier = 519 get_vreg_qualifier_from_value ((pos << 1) | (int) Q); 520 } 521 else 522 info->qualifier = get_sreg_qualifier_from_value (pos); 523 524 if (info->type == AARCH64_OPND_IMM_VLSR) 525 /* immh <shift> 526 0000 SEE AdvSIMD modified immediate 527 0001 (16-UInt(immh:immb)) 528 001x (32-UInt(immh:immb)) 529 01xx (64-UInt(immh:immb)) 530 1xxx (128-UInt(immh:immb)) */ 531 info->imm.value = (16 << pos) - imm; 532 else 533 /* immh:immb 534 immh <shift> 535 0000 SEE AdvSIMD modified immediate 536 0001 (UInt(immh:immb)-8) 537 001x (UInt(immh:immb)-16) 538 01xx (UInt(immh:immb)-32) 539 1xxx (UInt(immh:immb)-64) */ 540 info->imm.value = imm - (8 << pos); 541 542 return 1; 543 } 544 545 /* Decode shift immediate for e.g. sshr (imm). */ 546 int 547 aarch64_ext_shll_imm (const aarch64_operand *self ATTRIBUTE_UNUSED, 548 aarch64_opnd_info *info, const aarch64_insn code, 549 const aarch64_inst *inst ATTRIBUTE_UNUSED) 550 { 551 int64_t imm; 552 aarch64_insn val; 553 val = extract_field (FLD_size, code, 0); 554 switch (val) 555 { 556 case 0: imm = 8; break; 557 case 1: imm = 16; break; 558 case 2: imm = 32; break; 559 default: return 0; 560 } 561 info->imm.value = imm; 562 return 1; 563 } 564 565 /* Decode imm for e.g. BFM <Wd>, <Wn>, #<immr>, #<imms>. 566 value in the field(s) will be extracted as unsigned immediate value. */ 567 int 568 aarch64_ext_imm (const aarch64_operand *self, aarch64_opnd_info *info, 569 const aarch64_insn code, 570 const aarch64_inst *inst ATTRIBUTE_UNUSED) 571 { 572 int64_t imm; 573 /* Maximum of two fields to extract. */ 574 assert (self->fields[2] == FLD_NIL); 575 576 if (self->fields[1] == FLD_NIL) 577 imm = extract_field (self->fields[0], code, 0); 578 else 579 /* e.g. TBZ b5:b40. */ 580 imm = extract_fields (code, 0, 2, self->fields[0], self->fields[1]); 581 582 if (info->type == AARCH64_OPND_FPIMM) 583 info->imm.is_fp = 1; 584 585 if (operand_need_sign_extension (self)) 586 imm = sign_extend (imm, get_operand_fields_width (self) - 1); 587 588 if (operand_need_shift_by_two (self)) 589 imm <<= 2; 590 591 if (info->type == AARCH64_OPND_ADDR_ADRP) 592 imm <<= 12; 593 594 info->imm.value = imm; 595 return 1; 596 } 597 598 /* Decode imm and its shifter for e.g. MOVZ <Wd>, #<imm16>{, LSL #<shift>}. */ 599 int 600 aarch64_ext_imm_half (const aarch64_operand *self, aarch64_opnd_info *info, 601 const aarch64_insn code, 602 const aarch64_inst *inst ATTRIBUTE_UNUSED) 603 { 604 aarch64_ext_imm (self, info, code, inst); 605 info->shifter.kind = AARCH64_MOD_LSL; 606 info->shifter.amount = extract_field (FLD_hw, code, 0) << 4; 607 return 1; 608 } 609 610 /* Decode cmode and "a:b:c:d:e:f:g:h" for e.g. 611 MOVI <Vd>.<T>, #<imm8> {, LSL #<amount>}. */ 612 int 613 aarch64_ext_advsimd_imm_modified (const aarch64_operand *self ATTRIBUTE_UNUSED, 614 aarch64_opnd_info *info, 615 const aarch64_insn code, 616 const aarch64_inst *inst ATTRIBUTE_UNUSED) 617 { 618 uint64_t imm; 619 enum aarch64_opnd_qualifier opnd0_qualifier = inst->operands[0].qualifier; 620 aarch64_field field = {0, 0}; 621 622 assert (info->idx == 1); 623 624 if (info->type == AARCH64_OPND_SIMD_FPIMM) 625 info->imm.is_fp = 1; 626 627 /* a:b:c:d:e:f:g:h */ 628 imm = extract_fields (code, 0, 2, FLD_abc, FLD_defgh); 629 if (!info->imm.is_fp && aarch64_get_qualifier_esize (opnd0_qualifier) == 8) 630 { 631 /* Either MOVI <Dd>, #<imm> 632 or MOVI <Vd>.2D, #<imm>. 633 <imm> is a 64-bit immediate 634 'aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffgggggggghhhhhhhh', 635 encoded in "a:b:c:d:e:f:g:h". */ 636 int i; 637 unsigned abcdefgh = imm; 638 for (imm = 0ull, i = 0; i < 8; i++) 639 if (((abcdefgh >> i) & 0x1) != 0) 640 imm |= 0xffull << (8 * i); 641 } 642 info->imm.value = imm; 643 644 /* cmode */ 645 info->qualifier = get_expected_qualifier (inst, info->idx); 646 switch (info->qualifier) 647 { 648 case AARCH64_OPND_QLF_NIL: 649 /* no shift */ 650 info->shifter.kind = AARCH64_MOD_NONE; 651 return 1; 652 case AARCH64_OPND_QLF_LSL: 653 /* shift zeros */ 654 info->shifter.kind = AARCH64_MOD_LSL; 655 switch (aarch64_get_qualifier_esize (opnd0_qualifier)) 656 { 657 case 4: gen_sub_field (FLD_cmode, 1, 2, &field); break; /* per word */ 658 case 2: gen_sub_field (FLD_cmode, 1, 1, &field); break; /* per half */ 659 case 1: gen_sub_field (FLD_cmode, 1, 0, &field); break; /* per byte */ 660 default: assert (0); return 0; 661 } 662 /* 00: 0; 01: 8; 10:16; 11:24. */ 663 info->shifter.amount = extract_field_2 (&field, code, 0) << 3; 664 break; 665 case AARCH64_OPND_QLF_MSL: 666 /* shift ones */ 667 info->shifter.kind = AARCH64_MOD_MSL; 668 gen_sub_field (FLD_cmode, 0, 1, &field); /* per word */ 669 info->shifter.amount = extract_field_2 (&field, code, 0) ? 16 : 8; 670 break; 671 default: 672 assert (0); 673 return 0; 674 } 675 676 return 1; 677 } 678 679 /* Decode scale for e.g. SCVTF <Dd>, <Wn>, #<fbits>. */ 680 int 681 aarch64_ext_fbits (const aarch64_operand *self ATTRIBUTE_UNUSED, 682 aarch64_opnd_info *info, const aarch64_insn code, 683 const aarch64_inst *inst ATTRIBUTE_UNUSED) 684 { 685 info->imm.value = 64- extract_field (FLD_scale, code, 0); 686 return 1; 687 } 688 689 /* Decode arithmetic immediate for e.g. 690 SUBS <Wd>, <Wn|WSP>, #<imm> {, <shift>}. */ 691 int 692 aarch64_ext_aimm (const aarch64_operand *self ATTRIBUTE_UNUSED, 693 aarch64_opnd_info *info, const aarch64_insn code, 694 const aarch64_inst *inst ATTRIBUTE_UNUSED) 695 { 696 aarch64_insn value; 697 698 info->shifter.kind = AARCH64_MOD_LSL; 699 /* shift */ 700 value = extract_field (FLD_shift, code, 0); 701 if (value >= 2) 702 return 0; 703 info->shifter.amount = value ? 12 : 0; 704 /* imm12 (unsigned) */ 705 info->imm.value = extract_field (FLD_imm12, code, 0); 706 707 return 1; 708 } 709 710 /* Decode logical immediate for e.g. ORR <Wd|WSP>, <Wn>, #<imm>. */ 711 712 int 713 aarch64_ext_limm (const aarch64_operand *self ATTRIBUTE_UNUSED, 714 aarch64_opnd_info *info, const aarch64_insn code, 715 const aarch64_inst *inst ATTRIBUTE_UNUSED) 716 { 717 uint64_t imm, mask; 718 uint32_t sf; 719 uint32_t N, R, S; 720 unsigned simd_size; 721 aarch64_insn value; 722 723 value = extract_fields (code, 0, 3, FLD_N, FLD_immr, FLD_imms); 724 assert (inst->operands[0].qualifier == AARCH64_OPND_QLF_W 725 || inst->operands[0].qualifier == AARCH64_OPND_QLF_X); 726 sf = aarch64_get_qualifier_esize (inst->operands[0].qualifier) != 4; 727 728 /* value is N:immr:imms. */ 729 S = value & 0x3f; 730 R = (value >> 6) & 0x3f; 731 N = (value >> 12) & 0x1; 732 733 if (sf == 0 && N == 1) 734 return 0; 735 736 /* The immediate value is S+1 bits to 1, left rotated by SIMDsize - R 737 (in other words, right rotated by R), then replicated. */ 738 if (N != 0) 739 { 740 simd_size = 64; 741 mask = 0xffffffffffffffffull; 742 } 743 else 744 { 745 switch (S) 746 { 747 case 0x00 ... 0x1f: /* 0xxxxx */ simd_size = 32; break; 748 case 0x20 ... 0x2f: /* 10xxxx */ simd_size = 16; S &= 0xf; break; 749 case 0x30 ... 0x37: /* 110xxx */ simd_size = 8; S &= 0x7; break; 750 case 0x38 ... 0x3b: /* 1110xx */ simd_size = 4; S &= 0x3; break; 751 case 0x3c ... 0x3d: /* 11110x */ simd_size = 2; S &= 0x1; break; 752 default: return 0; 753 } 754 mask = (1ull << simd_size) - 1; 755 /* Top bits are IGNORED. */ 756 R &= simd_size - 1; 757 } 758 /* NOTE: if S = simd_size - 1 we get 0xf..f which is rejected. */ 759 if (S == simd_size - 1) 760 return 0; 761 /* S+1 consecutive bits to 1. */ 762 /* NOTE: S can't be 63 due to detection above. */ 763 imm = (1ull << (S + 1)) - 1; 764 /* Rotate to the left by simd_size - R. */ 765 if (R != 0) 766 imm = ((imm << (simd_size - R)) & mask) | (imm >> R); 767 /* Replicate the value according to SIMD size. */ 768 switch (simd_size) 769 { 770 case 2: imm = (imm << 2) | imm; 771 case 4: imm = (imm << 4) | imm; 772 case 8: imm = (imm << 8) | imm; 773 case 16: imm = (imm << 16) | imm; 774 case 32: imm = (imm << 32) | imm; 775 case 64: break; 776 default: assert (0); return 0; 777 } 778 779 info->imm.value = sf ? imm : imm & 0xffffffff; 780 781 return 1; 782 } 783 784 /* Decode Ft for e.g. STR <Qt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}] 785 or LDP <Qt1>, <Qt2>, [<Xn|SP>], #<imm>. */ 786 int 787 aarch64_ext_ft (const aarch64_operand *self ATTRIBUTE_UNUSED, 788 aarch64_opnd_info *info, 789 const aarch64_insn code, const aarch64_inst *inst) 790 { 791 aarch64_insn value; 792 793 /* Rt */ 794 info->reg.regno = extract_field (FLD_Rt, code, 0); 795 796 /* size */ 797 value = extract_field (FLD_ldst_size, code, 0); 798 if (inst->opcode->iclass == ldstpair_indexed 799 || inst->opcode->iclass == ldstnapair_offs 800 || inst->opcode->iclass == ldstpair_off 801 || inst->opcode->iclass == loadlit) 802 { 803 enum aarch64_opnd_qualifier qualifier; 804 switch (value) 805 { 806 case 0: qualifier = AARCH64_OPND_QLF_S_S; break; 807 case 1: qualifier = AARCH64_OPND_QLF_S_D; break; 808 case 2: qualifier = AARCH64_OPND_QLF_S_Q; break; 809 default: return 0; 810 } 811 info->qualifier = qualifier; 812 } 813 else 814 { 815 /* opc1:size */ 816 value = extract_fields (code, 0, 2, FLD_opc1, FLD_ldst_size); 817 if (value > 0x4) 818 return 0; 819 info->qualifier = get_sreg_qualifier_from_value (value); 820 } 821 822 return 1; 823 } 824 825 /* Decode the address operand for e.g. STXRB <Ws>, <Wt>, [<Xn|SP>{,#0}]. */ 826 int 827 aarch64_ext_addr_simple (const aarch64_operand *self ATTRIBUTE_UNUSED, 828 aarch64_opnd_info *info, 829 aarch64_insn code, 830 const aarch64_inst *inst ATTRIBUTE_UNUSED) 831 { 832 /* Rn */ 833 info->addr.base_regno = extract_field (FLD_Rn, code, 0); 834 return 1; 835 } 836 837 /* Decode the address operand for e.g. 838 STR <Qt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]. */ 839 int 840 aarch64_ext_addr_regoff (const aarch64_operand *self ATTRIBUTE_UNUSED, 841 aarch64_opnd_info *info, 842 aarch64_insn code, const aarch64_inst *inst) 843 { 844 aarch64_insn S, value; 845 846 /* Rn */ 847 info->addr.base_regno = extract_field (FLD_Rn, code, 0); 848 /* Rm */ 849 info->addr.offset.regno = extract_field (FLD_Rm, code, 0); 850 /* option */ 851 value = extract_field (FLD_option, code, 0); 852 info->shifter.kind = 853 aarch64_get_operand_modifier_from_value (value, TRUE /* extend_p */); 854 /* Fix-up the shifter kind; although the table-driven approach is 855 efficient, it is slightly inflexible, thus needing this fix-up. */ 856 if (info->shifter.kind == AARCH64_MOD_UXTX) 857 info->shifter.kind = AARCH64_MOD_LSL; 858 /* S */ 859 S = extract_field (FLD_S, code, 0); 860 if (S == 0) 861 { 862 info->shifter.amount = 0; 863 info->shifter.amount_present = 0; 864 } 865 else 866 { 867 int size; 868 /* Need information in other operand(s) to help achieve the decoding 869 from 'S' field. */ 870 info->qualifier = get_expected_qualifier (inst, info->idx); 871 /* Get the size of the data element that is accessed, which may be 872 different from that of the source register size, e.g. in strb/ldrb. */ 873 size = aarch64_get_qualifier_esize (info->qualifier); 874 info->shifter.amount = get_logsz (size); 875 info->shifter.amount_present = 1; 876 } 877 878 return 1; 879 } 880 881 /* Decode the address operand for e.g. LDRSW <Xt>, [<Xn|SP>], #<simm>. */ 882 int 883 aarch64_ext_addr_simm (const aarch64_operand *self, aarch64_opnd_info *info, 884 aarch64_insn code, const aarch64_inst *inst) 885 { 886 aarch64_insn imm; 887 info->qualifier = get_expected_qualifier (inst, info->idx); 888 889 /* Rn */ 890 info->addr.base_regno = extract_field (FLD_Rn, code, 0); 891 /* simm (imm9 or imm7) */ 892 imm = extract_field (self->fields[0], code, 0); 893 info->addr.offset.imm = sign_extend (imm, fields[self->fields[0]].width - 1); 894 if (self->fields[0] == FLD_imm7) 895 /* scaled immediate in ld/st pair instructions. */ 896 info->addr.offset.imm *= aarch64_get_qualifier_esize (info->qualifier); 897 /* qualifier */ 898 if (inst->opcode->iclass == ldst_unscaled 899 || inst->opcode->iclass == ldstnapair_offs 900 || inst->opcode->iclass == ldstpair_off 901 || inst->opcode->iclass == ldst_unpriv) 902 info->addr.writeback = 0; 903 else 904 { 905 /* pre/post- index */ 906 info->addr.writeback = 1; 907 if (extract_field (self->fields[1], code, 0) == 1) 908 info->addr.preind = 1; 909 else 910 info->addr.postind = 1; 911 } 912 913 return 1; 914 } 915 916 /* Decode the address operand for e.g. LDRSW <Xt>, [<Xn|SP>{, #<simm>}]. */ 917 int 918 aarch64_ext_addr_uimm12 (const aarch64_operand *self, aarch64_opnd_info *info, 919 aarch64_insn code, 920 const aarch64_inst *inst ATTRIBUTE_UNUSED) 921 { 922 int shift; 923 info->qualifier = get_expected_qualifier (inst, info->idx); 924 shift = get_logsz (aarch64_get_qualifier_esize (info->qualifier)); 925 /* Rn */ 926 info->addr.base_regno = extract_field (self->fields[0], code, 0); 927 /* uimm12 */ 928 info->addr.offset.imm = extract_field (self->fields[1], code, 0) << shift; 929 return 1; 930 } 931 932 /* Decode the address operand for e.g. 933 LD1 {<Vt>.<T>, <Vt2>.<T>, <Vt3>.<T>}, [<Xn|SP>], <Xm|#<amount>>. */ 934 int 935 aarch64_ext_simd_addr_post (const aarch64_operand *self ATTRIBUTE_UNUSED, 936 aarch64_opnd_info *info, 937 aarch64_insn code, const aarch64_inst *inst) 938 { 939 /* The opcode dependent area stores the number of elements in 940 each structure to be loaded/stored. */ 941 int is_ld1r = get_opcode_dependent_value (inst->opcode) == 1; 942 943 /* Rn */ 944 info->addr.base_regno = extract_field (FLD_Rn, code, 0); 945 /* Rm | #<amount> */ 946 info->addr.offset.regno = extract_field (FLD_Rm, code, 0); 947 if (info->addr.offset.regno == 31) 948 { 949 if (inst->opcode->operands[0] == AARCH64_OPND_LVt_AL) 950 /* Special handling of loading single structure to all lane. */ 951 info->addr.offset.imm = (is_ld1r ? 1 952 : inst->operands[0].reglist.num_regs) 953 * aarch64_get_qualifier_esize (inst->operands[0].qualifier); 954 else 955 info->addr.offset.imm = inst->operands[0].reglist.num_regs 956 * aarch64_get_qualifier_esize (inst->operands[0].qualifier) 957 * aarch64_get_qualifier_nelem (inst->operands[0].qualifier); 958 } 959 else 960 info->addr.offset.is_reg = 1; 961 info->addr.writeback = 1; 962 963 return 1; 964 } 965 966 /* Decode the condition operand for e.g. CSEL <Xd>, <Xn>, <Xm>, <cond>. */ 967 int 968 aarch64_ext_cond (const aarch64_operand *self ATTRIBUTE_UNUSED, 969 aarch64_opnd_info *info, 970 aarch64_insn code, const aarch64_inst *inst ATTRIBUTE_UNUSED) 971 { 972 aarch64_insn value; 973 /* cond */ 974 value = extract_field (FLD_cond, code, 0); 975 info->cond = get_cond_from_value (value); 976 return 1; 977 } 978 979 /* Decode the system register operand for e.g. MRS <Xt>, <systemreg>. */ 980 int 981 aarch64_ext_sysreg (const aarch64_operand *self ATTRIBUTE_UNUSED, 982 aarch64_opnd_info *info, 983 aarch64_insn code, 984 const aarch64_inst *inst ATTRIBUTE_UNUSED) 985 { 986 /* op0:op1:CRn:CRm:op2 */ 987 info->sysreg = extract_fields (code, 0, 5, FLD_op0, FLD_op1, FLD_CRn, 988 FLD_CRm, FLD_op2); 989 return 1; 990 } 991 992 /* Decode the PSTATE field operand for e.g. MSR <pstatefield>, #<imm>. */ 993 int 994 aarch64_ext_pstatefield (const aarch64_operand *self ATTRIBUTE_UNUSED, 995 aarch64_opnd_info *info, aarch64_insn code, 996 const aarch64_inst *inst ATTRIBUTE_UNUSED) 997 { 998 int i; 999 /* op1:op2 */ 1000 info->pstatefield = extract_fields (code, 0, 2, FLD_op1, FLD_op2); 1001 for (i = 0; aarch64_pstatefields[i].name != NULL; ++i) 1002 if (aarch64_pstatefields[i].value == (aarch64_insn)info->pstatefield) 1003 return 1; 1004 /* Reserved value in <pstatefield>. */ 1005 return 0; 1006 } 1007 1008 /* Decode the system instruction op operand for e.g. AT <at_op>, <Xt>. */ 1009 int 1010 aarch64_ext_sysins_op (const aarch64_operand *self ATTRIBUTE_UNUSED, 1011 aarch64_opnd_info *info, 1012 aarch64_insn code, 1013 const aarch64_inst *inst ATTRIBUTE_UNUSED) 1014 { 1015 int i; 1016 aarch64_insn value; 1017 const aarch64_sys_ins_reg *sysins_ops; 1018 /* op0:op1:CRn:CRm:op2 */ 1019 value = extract_fields (code, 0, 5, 1020 FLD_op0, FLD_op1, FLD_CRn, 1021 FLD_CRm, FLD_op2); 1022 1023 switch (info->type) 1024 { 1025 case AARCH64_OPND_SYSREG_AT: sysins_ops = aarch64_sys_regs_at; break; 1026 case AARCH64_OPND_SYSREG_DC: sysins_ops = aarch64_sys_regs_dc; break; 1027 case AARCH64_OPND_SYSREG_IC: sysins_ops = aarch64_sys_regs_ic; break; 1028 case AARCH64_OPND_SYSREG_TLBI: sysins_ops = aarch64_sys_regs_tlbi; break; 1029 default: assert (0); return 0; 1030 } 1031 1032 for (i = 0; sysins_ops[i].template != NULL; ++i) 1033 if (sysins_ops[i].value == value) 1034 { 1035 info->sysins_op = sysins_ops + i; 1036 DEBUG_TRACE ("%s found value: %x, has_xt: %d, i: %d.", 1037 info->sysins_op->template, 1038 (unsigned)info->sysins_op->value, 1039 info->sysins_op->has_xt, i); 1040 return 1; 1041 } 1042 1043 return 0; 1044 } 1045 1046 /* Decode the memory barrier option operand for e.g. DMB <option>|#<imm>. */ 1047 1048 int 1049 aarch64_ext_barrier (const aarch64_operand *self ATTRIBUTE_UNUSED, 1050 aarch64_opnd_info *info, 1051 aarch64_insn code, 1052 const aarch64_inst *inst ATTRIBUTE_UNUSED) 1053 { 1054 /* CRm */ 1055 info->barrier = aarch64_barrier_options + extract_field (FLD_CRm, code, 0); 1056 return 1; 1057 } 1058 1059 /* Decode the prefetch operation option operand for e.g. 1060 PRFM <prfop>, [<Xn|SP>{, #<pimm>}]. */ 1061 1062 int 1063 aarch64_ext_prfop (const aarch64_operand *self ATTRIBUTE_UNUSED, 1064 aarch64_opnd_info *info, 1065 aarch64_insn code, const aarch64_inst *inst ATTRIBUTE_UNUSED) 1066 { 1067 /* prfop in Rt */ 1068 info->prfop = aarch64_prfops + extract_field (FLD_Rt, code, 0); 1069 return 1; 1070 } 1071 1072 /* Decode the extended register operand for e.g. 1073 STR <Qt>, [<Xn|SP>, <R><m>{, <extend> {<amount>}}]. */ 1074 int 1075 aarch64_ext_reg_extended (const aarch64_operand *self ATTRIBUTE_UNUSED, 1076 aarch64_opnd_info *info, 1077 aarch64_insn code, 1078 const aarch64_inst *inst ATTRIBUTE_UNUSED) 1079 { 1080 aarch64_insn value; 1081 1082 /* Rm */ 1083 info->reg.regno = extract_field (FLD_Rm, code, 0); 1084 /* option */ 1085 value = extract_field (FLD_option, code, 0); 1086 info->shifter.kind = 1087 aarch64_get_operand_modifier_from_value (value, TRUE /* extend_p */); 1088 /* imm3 */ 1089 info->shifter.amount = extract_field (FLD_imm3, code, 0); 1090 1091 /* This makes the constraint checking happy. */ 1092 info->shifter.operator_present = 1; 1093 1094 /* Assume inst->operands[0].qualifier has been resolved. */ 1095 assert (inst->operands[0].qualifier != AARCH64_OPND_QLF_NIL); 1096 info->qualifier = AARCH64_OPND_QLF_W; 1097 if (inst->operands[0].qualifier == AARCH64_OPND_QLF_X 1098 && (info->shifter.kind == AARCH64_MOD_UXTX 1099 || info->shifter.kind == AARCH64_MOD_SXTX)) 1100 info->qualifier = AARCH64_OPND_QLF_X; 1101 1102 return 1; 1103 } 1104 1105 /* Decode the shifted register operand for e.g. 1106 SUBS <Xd>, <Xn>, <Xm> {, <shift> #<amount>}. */ 1107 int 1108 aarch64_ext_reg_shifted (const aarch64_operand *self ATTRIBUTE_UNUSED, 1109 aarch64_opnd_info *info, 1110 aarch64_insn code, 1111 const aarch64_inst *inst ATTRIBUTE_UNUSED) 1112 { 1113 aarch64_insn value; 1114 1115 /* Rm */ 1116 info->reg.regno = extract_field (FLD_Rm, code, 0); 1117 /* shift */ 1118 value = extract_field (FLD_shift, code, 0); 1119 info->shifter.kind = 1120 aarch64_get_operand_modifier_from_value (value, FALSE /* extend_p */); 1121 if (info->shifter.kind == AARCH64_MOD_ROR 1122 && inst->opcode->iclass != log_shift) 1123 /* ROR is not available for the shifted register operand in arithmetic 1124 instructions. */ 1125 return 0; 1126 /* imm6 */ 1127 info->shifter.amount = extract_field (FLD_imm6, code, 0); 1128 1129 /* This makes the constraint checking happy. */ 1130 info->shifter.operator_present = 1; 1131 1132 return 1; 1133 } 1134 1135 /* Bitfields that are commonly used to encode certain operands' information 1137 may be partially used as part of the base opcode in some instructions. 1138 For example, the bit 1 of the field 'size' in 1139 FCVTXN <Vb><d>, <Va><n> 1140 is actually part of the base opcode, while only size<0> is available 1141 for encoding the register type. Another example is the AdvSIMD 1142 instruction ORR (register), in which the field 'size' is also used for 1143 the base opcode, leaving only the field 'Q' available to encode the 1144 vector register arrangement specifier '8B' or '16B'. 1145 1146 This function tries to deduce the qualifier from the value of partially 1147 constrained field(s). Given the VALUE of such a field or fields, the 1148 qualifiers CANDIDATES and the MASK (indicating which bits are valid for 1149 operand encoding), the function returns the matching qualifier or 1150 AARCH64_OPND_QLF_NIL if nothing matches. 1151 1152 N.B. CANDIDATES is a group of possible qualifiers that are valid for 1153 one operand; it has a maximum of AARCH64_MAX_QLF_SEQ_NUM qualifiers and 1154 may end with AARCH64_OPND_QLF_NIL. */ 1155 1156 static enum aarch64_opnd_qualifier 1157 get_qualifier_from_partial_encoding (aarch64_insn value, 1158 const enum aarch64_opnd_qualifier* \ 1159 candidates, 1160 aarch64_insn mask) 1161 { 1162 int i; 1163 DEBUG_TRACE ("enter with value: %d, mask: %d", (int)value, (int)mask); 1164 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i) 1165 { 1166 aarch64_insn standard_value; 1167 if (candidates[i] == AARCH64_OPND_QLF_NIL) 1168 break; 1169 standard_value = aarch64_get_qualifier_standard_value (candidates[i]); 1170 if ((standard_value & mask) == (value & mask)) 1171 return candidates[i]; 1172 } 1173 return AARCH64_OPND_QLF_NIL; 1174 } 1175 1176 /* Given a list of qualifier sequences, return all possible valid qualifiers 1177 for operand IDX in QUALIFIERS. 1178 Assume QUALIFIERS is an array whose length is large enough. */ 1179 1180 static void 1181 get_operand_possible_qualifiers (int idx, 1182 const aarch64_opnd_qualifier_seq_t *list, 1183 enum aarch64_opnd_qualifier *qualifiers) 1184 { 1185 int i; 1186 for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i) 1187 if ((qualifiers[i] = list[i][idx]) == AARCH64_OPND_QLF_NIL) 1188 break; 1189 } 1190 1191 /* Decode the size Q field for e.g. SHADD. 1192 We tag one operand with the qualifer according to the code; 1193 whether the qualifier is valid for this opcode or not, it is the 1194 duty of the semantic checking. */ 1195 1196 static int 1197 decode_sizeq (aarch64_inst *inst) 1198 { 1199 int idx; 1200 enum aarch64_opnd_qualifier qualifier; 1201 aarch64_insn code; 1202 aarch64_insn value, mask; 1203 enum aarch64_field_kind fld_sz; 1204 enum aarch64_opnd_qualifier candidates[AARCH64_MAX_QLF_SEQ_NUM]; 1205 1206 if (inst->opcode->iclass == asisdlse 1207 || inst->opcode->iclass == asisdlsep 1208 || inst->opcode->iclass == asisdlso 1209 || inst->opcode->iclass == asisdlsop) 1210 fld_sz = FLD_vldst_size; 1211 else 1212 fld_sz = FLD_size; 1213 1214 code = inst->value; 1215 value = extract_fields (code, inst->opcode->mask, 2, fld_sz, FLD_Q); 1216 /* Obtain the info that which bits of fields Q and size are actually 1217 available for operand encoding. Opcodes like FMAXNM and FMLA have 1218 size[1] unavailable. */ 1219 mask = extract_fields (~inst->opcode->mask, 0, 2, fld_sz, FLD_Q); 1220 1221 /* The index of the operand we are going to tag a qualifier and the qualifer 1222 itself are reasoned from the value of the size and Q fields and the 1223 possible valid qualifier lists. */ 1224 idx = aarch64_select_operand_for_sizeq_field_coding (inst->opcode); 1225 DEBUG_TRACE ("key idx: %d", idx); 1226 1227 /* For most related instruciton, size:Q are fully available for operand 1228 encoding. */ 1229 if (mask == 0x7) 1230 { 1231 inst->operands[idx].qualifier = get_vreg_qualifier_from_value (value); 1232 return 1; 1233 } 1234 1235 get_operand_possible_qualifiers (idx, inst->opcode->qualifiers_list, 1236 candidates); 1237 #ifdef DEBUG_AARCH64 1238 if (debug_dump) 1239 { 1240 int i; 1241 for (i = 0; candidates[i] != AARCH64_OPND_QLF_NIL 1242 && i < AARCH64_MAX_QLF_SEQ_NUM; ++i) 1243 DEBUG_TRACE ("qualifier %d: %s", i, 1244 aarch64_get_qualifier_name(candidates[i])); 1245 DEBUG_TRACE ("%d, %d", (int)value, (int)mask); 1246 } 1247 #endif /* DEBUG_AARCH64 */ 1248 1249 qualifier = get_qualifier_from_partial_encoding (value, candidates, mask); 1250 1251 if (qualifier == AARCH64_OPND_QLF_NIL) 1252 return 0; 1253 1254 inst->operands[idx].qualifier = qualifier; 1255 return 1; 1256 } 1257 1258 /* Decode size[0]:Q, i.e. bit 22 and bit 30, for 1259 e.g. FCVTN<Q> <Vd>.<Tb>, <Vn>.<Ta>. */ 1260 1261 static int 1262 decode_asimd_fcvt (aarch64_inst *inst) 1263 { 1264 aarch64_field field = {0, 0}; 1265 aarch64_insn value; 1266 enum aarch64_opnd_qualifier qualifier; 1267 1268 gen_sub_field (FLD_size, 0, 1, &field); 1269 value = extract_field_2 (&field, inst->value, 0); 1270 qualifier = value == 0 ? AARCH64_OPND_QLF_V_4S 1271 : AARCH64_OPND_QLF_V_2D; 1272 switch (inst->opcode->op) 1273 { 1274 case OP_FCVTN: 1275 case OP_FCVTN2: 1276 /* FCVTN<Q> <Vd>.<Tb>, <Vn>.<Ta>. */ 1277 inst->operands[1].qualifier = qualifier; 1278 break; 1279 case OP_FCVTL: 1280 case OP_FCVTL2: 1281 /* FCVTL<Q> <Vd>.<Ta>, <Vn>.<Tb>. */ 1282 inst->operands[0].qualifier = qualifier; 1283 break; 1284 default: 1285 assert (0); 1286 return 0; 1287 } 1288 1289 return 1; 1290 } 1291 1292 /* Decode size[0], i.e. bit 22, for 1293 e.g. FCVTXN <Vb><d>, <Va><n>. */ 1294 1295 static int 1296 decode_asisd_fcvtxn (aarch64_inst *inst) 1297 { 1298 aarch64_field field = {0, 0}; 1299 gen_sub_field (FLD_size, 0, 1, &field); 1300 if (!extract_field_2 (&field, inst->value, 0)) 1301 return 0; 1302 inst->operands[0].qualifier = AARCH64_OPND_QLF_S_S; 1303 return 1; 1304 } 1305 1306 /* Decode the 'opc' field for e.g. FCVT <Dd>, <Sn>. */ 1307 static int 1308 decode_fcvt (aarch64_inst *inst) 1309 { 1310 enum aarch64_opnd_qualifier qualifier; 1311 aarch64_insn value; 1312 const aarch64_field field = {15, 2}; 1313 1314 /* opc dstsize */ 1315 value = extract_field_2 (&field, inst->value, 0); 1316 switch (value) 1317 { 1318 case 0: qualifier = AARCH64_OPND_QLF_S_S; break; 1319 case 1: qualifier = AARCH64_OPND_QLF_S_D; break; 1320 case 3: qualifier = AARCH64_OPND_QLF_S_H; break; 1321 default: return 0; 1322 } 1323 inst->operands[0].qualifier = qualifier; 1324 1325 return 1; 1326 } 1327 1328 /* Do miscellaneous decodings that are not common enough to be driven by 1329 flags. */ 1330 1331 static int 1332 do_misc_decoding (aarch64_inst *inst) 1333 { 1334 switch (inst->opcode->op) 1335 { 1336 case OP_FCVT: 1337 return decode_fcvt (inst); 1338 case OP_FCVTN: 1339 case OP_FCVTN2: 1340 case OP_FCVTL: 1341 case OP_FCVTL2: 1342 return decode_asimd_fcvt (inst); 1343 case OP_FCVTXN_S: 1344 return decode_asisd_fcvtxn (inst); 1345 default: 1346 return 0; 1347 } 1348 } 1349 1350 /* Opcodes that have fields shared by multiple operands are usually flagged 1351 with flags. In this function, we detect such flags, decode the related 1352 field(s) and store the information in one of the related operands. The 1353 'one' operand is not any operand but one of the operands that can 1354 accommadate all the information that has been decoded. */ 1355 1356 static int 1357 do_special_decoding (aarch64_inst *inst) 1358 { 1359 int idx; 1360 aarch64_insn value; 1361 /* Condition for truly conditional executed instructions, e.g. b.cond. */ 1362 if (inst->opcode->flags & F_COND) 1363 { 1364 value = extract_field (FLD_cond2, inst->value, 0); 1365 inst->cond = get_cond_from_value (value); 1366 } 1367 /* 'sf' field. */ 1368 if (inst->opcode->flags & F_SF) 1369 { 1370 idx = select_operand_for_sf_field_coding (inst->opcode); 1371 value = extract_field (FLD_sf, inst->value, 0); 1372 inst->operands[idx].qualifier = get_greg_qualifier_from_value (value); 1373 if ((inst->opcode->flags & F_N) 1374 && extract_field (FLD_N, inst->value, 0) != value) 1375 return 0; 1376 } 1377 /* 'sf' field. */ 1378 if (inst->opcode->flags & F_LSE_SZ) 1379 { 1380 idx = select_operand_for_sf_field_coding (inst->opcode); 1381 value = extract_field (FLD_lse_sz, inst->value, 0); 1382 inst->operands[idx].qualifier = get_greg_qualifier_from_value (value); 1383 } 1384 /* size:Q fields. */ 1385 if (inst->opcode->flags & F_SIZEQ) 1386 return decode_sizeq (inst); 1387 1388 if (inst->opcode->flags & F_FPTYPE) 1389 { 1390 idx = select_operand_for_fptype_field_coding (inst->opcode); 1391 value = extract_field (FLD_type, inst->value, 0); 1392 switch (value) 1393 { 1394 case 0: inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_S; break; 1395 case 1: inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_D; break; 1396 case 3: inst->operands[idx].qualifier = AARCH64_OPND_QLF_S_H; break; 1397 default: return 0; 1398 } 1399 } 1400 1401 if (inst->opcode->flags & F_SSIZE) 1402 { 1403 /* N.B. some opcodes like FCMGT <V><d>, <V><n>, #0 have the size[1] as part 1404 of the base opcode. */ 1405 aarch64_insn mask; 1406 enum aarch64_opnd_qualifier candidates[AARCH64_MAX_QLF_SEQ_NUM]; 1407 idx = select_operand_for_scalar_size_field_coding (inst->opcode); 1408 value = extract_field (FLD_size, inst->value, inst->opcode->mask); 1409 mask = extract_field (FLD_size, ~inst->opcode->mask, 0); 1410 /* For most related instruciton, the 'size' field is fully available for 1411 operand encoding. */ 1412 if (mask == 0x3) 1413 inst->operands[idx].qualifier = get_sreg_qualifier_from_value (value); 1414 else 1415 { 1416 get_operand_possible_qualifiers (idx, inst->opcode->qualifiers_list, 1417 candidates); 1418 inst->operands[idx].qualifier 1419 = get_qualifier_from_partial_encoding (value, candidates, mask); 1420 } 1421 } 1422 1423 if (inst->opcode->flags & F_T) 1424 { 1425 /* Num of consecutive '0's on the right side of imm5<3:0>. */ 1426 int num = 0; 1427 unsigned val, Q; 1428 assert (aarch64_get_operand_class (inst->opcode->operands[0]) 1429 == AARCH64_OPND_CLASS_SIMD_REG); 1430 /* imm5<3:0> q <t> 1431 0000 x reserved 1432 xxx1 0 8b 1433 xxx1 1 16b 1434 xx10 0 4h 1435 xx10 1 8h 1436 x100 0 2s 1437 x100 1 4s 1438 1000 0 reserved 1439 1000 1 2d */ 1440 val = extract_field (FLD_imm5, inst->value, 0); 1441 while ((val & 0x1) == 0 && ++num <= 3) 1442 val >>= 1; 1443 if (num > 3) 1444 return 0; 1445 Q = (unsigned) extract_field (FLD_Q, inst->value, inst->opcode->mask); 1446 inst->operands[0].qualifier = 1447 get_vreg_qualifier_from_value ((num << 1) | Q); 1448 } 1449 1450 if (inst->opcode->flags & F_GPRSIZE_IN_Q) 1451 { 1452 /* Use Rt to encode in the case of e.g. 1453 STXP <Ws>, <Xt1>, <Xt2>, [<Xn|SP>{,#0}]. */ 1454 idx = aarch64_operand_index (inst->opcode->operands, AARCH64_OPND_Rt); 1455 if (idx == -1) 1456 { 1457 /* Otherwise use the result operand, which has to be a integer 1458 register. */ 1459 assert (aarch64_get_operand_class (inst->opcode->operands[0]) 1460 == AARCH64_OPND_CLASS_INT_REG); 1461 idx = 0; 1462 } 1463 assert (idx == 0 || idx == 1); 1464 value = extract_field (FLD_Q, inst->value, 0); 1465 inst->operands[idx].qualifier = get_greg_qualifier_from_value (value); 1466 } 1467 1468 if (inst->opcode->flags & F_LDS_SIZE) 1469 { 1470 aarch64_field field = {0, 0}; 1471 assert (aarch64_get_operand_class (inst->opcode->operands[0]) 1472 == AARCH64_OPND_CLASS_INT_REG); 1473 gen_sub_field (FLD_opc, 0, 1, &field); 1474 value = extract_field_2 (&field, inst->value, 0); 1475 inst->operands[0].qualifier 1476 = value ? AARCH64_OPND_QLF_W : AARCH64_OPND_QLF_X; 1477 } 1478 1479 /* Miscellaneous decoding; done as the last step. */ 1480 if (inst->opcode->flags & F_MISC) 1481 return do_misc_decoding (inst); 1482 1483 return 1; 1484 } 1485 1486 /* Converters converting a real opcode instruction to its alias form. */ 1487 1488 /* ROR <Wd>, <Ws>, #<shift> 1489 is equivalent to: 1490 EXTR <Wd>, <Ws>, <Ws>, #<shift>. */ 1491 static int 1492 convert_extr_to_ror (aarch64_inst *inst) 1493 { 1494 if (inst->operands[1].reg.regno == inst->operands[2].reg.regno) 1495 { 1496 copy_operand_info (inst, 2, 3); 1497 inst->operands[3].type = AARCH64_OPND_NIL; 1498 return 1; 1499 } 1500 return 0; 1501 } 1502 1503 /* UXTL<Q> <Vd>.<Ta>, <Vn>.<Tb> 1504 is equivalent to: 1505 USHLL<Q> <Vd>.<Ta>, <Vn>.<Tb>, #0. */ 1506 static int 1507 convert_shll_to_xtl (aarch64_inst *inst) 1508 { 1509 if (inst->operands[2].imm.value == 0) 1510 { 1511 inst->operands[2].type = AARCH64_OPND_NIL; 1512 return 1; 1513 } 1514 return 0; 1515 } 1516 1517 /* Convert 1518 UBFM <Xd>, <Xn>, #<shift>, #63. 1519 to 1520 LSR <Xd>, <Xn>, #<shift>. */ 1521 static int 1522 convert_bfm_to_sr (aarch64_inst *inst) 1523 { 1524 int64_t imms, val; 1525 1526 imms = inst->operands[3].imm.value; 1527 val = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 31 : 63; 1528 if (imms == val) 1529 { 1530 inst->operands[3].type = AARCH64_OPND_NIL; 1531 return 1; 1532 } 1533 1534 return 0; 1535 } 1536 1537 /* Convert MOV to ORR. */ 1538 static int 1539 convert_orr_to_mov (aarch64_inst *inst) 1540 { 1541 /* MOV <Vd>.<T>, <Vn>.<T> 1542 is equivalent to: 1543 ORR <Vd>.<T>, <Vn>.<T>, <Vn>.<T>. */ 1544 if (inst->operands[1].reg.regno == inst->operands[2].reg.regno) 1545 { 1546 inst->operands[2].type = AARCH64_OPND_NIL; 1547 return 1; 1548 } 1549 return 0; 1550 } 1551 1552 /* When <imms> >= <immr>, the instruction written: 1553 SBFX <Xd>, <Xn>, #<lsb>, #<width> 1554 is equivalent to: 1555 SBFM <Xd>, <Xn>, #<lsb>, #(<lsb>+<width>-1). */ 1556 1557 static int 1558 convert_bfm_to_bfx (aarch64_inst *inst) 1559 { 1560 int64_t immr, imms; 1561 1562 immr = inst->operands[2].imm.value; 1563 imms = inst->operands[3].imm.value; 1564 if (imms >= immr) 1565 { 1566 int64_t lsb = immr; 1567 inst->operands[2].imm.value = lsb; 1568 inst->operands[3].imm.value = imms + 1 - lsb; 1569 /* The two opcodes have different qualifiers for 1570 the immediate operands; reset to help the checking. */ 1571 reset_operand_qualifier (inst, 2); 1572 reset_operand_qualifier (inst, 3); 1573 return 1; 1574 } 1575 1576 return 0; 1577 } 1578 1579 /* When <imms> < <immr>, the instruction written: 1580 SBFIZ <Xd>, <Xn>, #<lsb>, #<width> 1581 is equivalent to: 1582 SBFM <Xd>, <Xn>, #((64-<lsb>)&0x3f), #(<width>-1). */ 1583 1584 static int 1585 convert_bfm_to_bfi (aarch64_inst *inst) 1586 { 1587 int64_t immr, imms, val; 1588 1589 immr = inst->operands[2].imm.value; 1590 imms = inst->operands[3].imm.value; 1591 val = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 32 : 64; 1592 if (imms < immr) 1593 { 1594 inst->operands[2].imm.value = (val - immr) & (val - 1); 1595 inst->operands[3].imm.value = imms + 1; 1596 /* The two opcodes have different qualifiers for 1597 the immediate operands; reset to help the checking. */ 1598 reset_operand_qualifier (inst, 2); 1599 reset_operand_qualifier (inst, 3); 1600 return 1; 1601 } 1602 1603 return 0; 1604 } 1605 1606 /* The instruction written: 1607 LSL <Xd>, <Xn>, #<shift> 1608 is equivalent to: 1609 UBFM <Xd>, <Xn>, #((64-<shift>)&0x3f), #(63-<shift>). */ 1610 1611 static int 1612 convert_ubfm_to_lsl (aarch64_inst *inst) 1613 { 1614 int64_t immr = inst->operands[2].imm.value; 1615 int64_t imms = inst->operands[3].imm.value; 1616 int64_t val 1617 = inst->operands[2].qualifier == AARCH64_OPND_QLF_imm_0_31 ? 31 : 63; 1618 1619 if ((immr == 0 && imms == val) || immr == imms + 1) 1620 { 1621 inst->operands[3].type = AARCH64_OPND_NIL; 1622 inst->operands[2].imm.value = val - imms; 1623 return 1; 1624 } 1625 1626 return 0; 1627 } 1628 1629 /* CINC <Wd>, <Wn>, <cond> 1630 is equivalent to: 1631 CSINC <Wd>, <Wn>, <Wn>, invert(<cond>) 1632 where <cond> is not AL or NV. */ 1633 1634 static int 1635 convert_from_csel (aarch64_inst *inst) 1636 { 1637 if (inst->operands[1].reg.regno == inst->operands[2].reg.regno 1638 && (inst->operands[3].cond->value & 0xe) != 0xe) 1639 { 1640 copy_operand_info (inst, 2, 3); 1641 inst->operands[2].cond = get_inverted_cond (inst->operands[3].cond); 1642 inst->operands[3].type = AARCH64_OPND_NIL; 1643 return 1; 1644 } 1645 return 0; 1646 } 1647 1648 /* CSET <Wd>, <cond> 1649 is equivalent to: 1650 CSINC <Wd>, WZR, WZR, invert(<cond>) 1651 where <cond> is not AL or NV. */ 1652 1653 static int 1654 convert_csinc_to_cset (aarch64_inst *inst) 1655 { 1656 if (inst->operands[1].reg.regno == 0x1f 1657 && inst->operands[2].reg.regno == 0x1f 1658 && (inst->operands[3].cond->value & 0xe) != 0xe) 1659 { 1660 copy_operand_info (inst, 1, 3); 1661 inst->operands[1].cond = get_inverted_cond (inst->operands[3].cond); 1662 inst->operands[3].type = AARCH64_OPND_NIL; 1663 inst->operands[2].type = AARCH64_OPND_NIL; 1664 return 1; 1665 } 1666 return 0; 1667 } 1668 1669 /* MOV <Wd>, #<imm> 1670 is equivalent to: 1671 MOVZ <Wd>, #<imm16>, LSL #<shift>. 1672 1673 A disassembler may output ORR, MOVZ and MOVN as a MOV mnemonic, except when 1674 ORR has an immediate that could be generated by a MOVZ or MOVN instruction, 1675 or where a MOVN has an immediate that could be encoded by MOVZ, or where 1676 MOVZ/MOVN #0 have a shift amount other than LSL #0, in which case the 1677 machine-instruction mnemonic must be used. */ 1678 1679 static int 1680 convert_movewide_to_mov (aarch64_inst *inst) 1681 { 1682 uint64_t value = inst->operands[1].imm.value; 1683 /* MOVZ/MOVN #0 have a shift amount other than LSL #0. */ 1684 if (value == 0 && inst->operands[1].shifter.amount != 0) 1685 return 0; 1686 inst->operands[1].type = AARCH64_OPND_IMM_MOV; 1687 inst->operands[1].shifter.kind = AARCH64_MOD_NONE; 1688 value <<= inst->operands[1].shifter.amount; 1689 /* As an alias convertor, it has to be clear that the INST->OPCODE 1690 is the opcode of the real instruction. */ 1691 if (inst->opcode->op == OP_MOVN) 1692 { 1693 int is32 = inst->operands[0].qualifier == AARCH64_OPND_QLF_W; 1694 value = ~value; 1695 /* A MOVN has an immediate that could be encoded by MOVZ. */ 1696 if (aarch64_wide_constant_p (value, is32, NULL) == TRUE) 1697 return 0; 1698 } 1699 inst->operands[1].imm.value = value; 1700 inst->operands[1].shifter.amount = 0; 1701 return 1; 1702 } 1703 1704 /* MOV <Wd>, #<imm> 1705 is equivalent to: 1706 ORR <Wd>, WZR, #<imm>. 1707 1708 A disassembler may output ORR, MOVZ and MOVN as a MOV mnemonic, except when 1709 ORR has an immediate that could be generated by a MOVZ or MOVN instruction, 1710 or where a MOVN has an immediate that could be encoded by MOVZ, or where 1711 MOVZ/MOVN #0 have a shift amount other than LSL #0, in which case the 1712 machine-instruction mnemonic must be used. */ 1713 1714 static int 1715 convert_movebitmask_to_mov (aarch64_inst *inst) 1716 { 1717 int is32; 1718 uint64_t value; 1719 1720 /* Should have been assured by the base opcode value. */ 1721 assert (inst->operands[1].reg.regno == 0x1f); 1722 copy_operand_info (inst, 1, 2); 1723 is32 = inst->operands[0].qualifier == AARCH64_OPND_QLF_W; 1724 inst->operands[1].type = AARCH64_OPND_IMM_MOV; 1725 value = inst->operands[1].imm.value; 1726 /* ORR has an immediate that could be generated by a MOVZ or MOVN 1727 instruction. */ 1728 if (inst->operands[0].reg.regno != 0x1f 1729 && (aarch64_wide_constant_p (value, is32, NULL) == TRUE 1730 || aarch64_wide_constant_p (~value, is32, NULL) == TRUE)) 1731 return 0; 1732 1733 inst->operands[2].type = AARCH64_OPND_NIL; 1734 return 1; 1735 } 1736 1737 /* Some alias opcodes are disassembled by being converted from their real-form. 1738 N.B. INST->OPCODE is the real opcode rather than the alias. */ 1739 1740 static int 1741 convert_to_alias (aarch64_inst *inst, const aarch64_opcode *alias) 1742 { 1743 switch (alias->op) 1744 { 1745 case OP_ASR_IMM: 1746 case OP_LSR_IMM: 1747 return convert_bfm_to_sr (inst); 1748 case OP_LSL_IMM: 1749 return convert_ubfm_to_lsl (inst); 1750 case OP_CINC: 1751 case OP_CINV: 1752 case OP_CNEG: 1753 return convert_from_csel (inst); 1754 case OP_CSET: 1755 case OP_CSETM: 1756 return convert_csinc_to_cset (inst); 1757 case OP_UBFX: 1758 case OP_BFXIL: 1759 case OP_SBFX: 1760 return convert_bfm_to_bfx (inst); 1761 case OP_SBFIZ: 1762 case OP_BFI: 1763 case OP_UBFIZ: 1764 return convert_bfm_to_bfi (inst); 1765 case OP_MOV_V: 1766 return convert_orr_to_mov (inst); 1767 case OP_MOV_IMM_WIDE: 1768 case OP_MOV_IMM_WIDEN: 1769 return convert_movewide_to_mov (inst); 1770 case OP_MOV_IMM_LOG: 1771 return convert_movebitmask_to_mov (inst); 1772 case OP_ROR_IMM: 1773 return convert_extr_to_ror (inst); 1774 case OP_SXTL: 1775 case OP_SXTL2: 1776 case OP_UXTL: 1777 case OP_UXTL2: 1778 return convert_shll_to_xtl (inst); 1779 default: 1780 return 0; 1781 } 1782 } 1783 1784 static int aarch64_opcode_decode (const aarch64_opcode *, const aarch64_insn, 1785 aarch64_inst *, int); 1786 1787 /* Given the instruction information in *INST, check if the instruction has 1788 any alias form that can be used to represent *INST. If the answer is yes, 1789 update *INST to be in the form of the determined alias. */ 1790 1791 /* In the opcode description table, the following flags are used in opcode 1792 entries to help establish the relations between the real and alias opcodes: 1793 1794 F_ALIAS: opcode is an alias 1795 F_HAS_ALIAS: opcode has alias(es) 1796 F_P1 1797 F_P2 1798 F_P3: Disassembly preference priority 1-3 (the larger the 1799 higher). If nothing is specified, it is the priority 1800 0 by default, i.e. the lowest priority. 1801 1802 Although the relation between the machine and the alias instructions are not 1803 explicitly described, it can be easily determined from the base opcode 1804 values, masks and the flags F_ALIAS and F_HAS_ALIAS in their opcode 1805 description entries: 1806 1807 The mask of an alias opcode must be equal to or a super-set (i.e. more 1808 constrained) of that of the aliased opcode; so is the base opcode value. 1809 1810 if (opcode_has_alias (real) && alias_opcode_p (opcode) 1811 && (opcode->mask & real->mask) == real->mask 1812 && (real->mask & opcode->opcode) == (real->mask & real->opcode)) 1813 then OPCODE is an alias of, and only of, the REAL instruction 1814 1815 The alias relationship is forced flat-structured to keep related algorithm 1816 simple; an opcode entry cannot be flagged with both F_ALIAS and F_HAS_ALIAS. 1817 1818 During the disassembling, the decoding decision tree (in 1819 opcodes/aarch64-dis-2.c) always returns an machine instruction opcode entry; 1820 if the decoding of such a machine instruction succeeds (and -Mno-aliases is 1821 not specified), the disassembler will check whether there is any alias 1822 instruction exists for this real instruction. If there is, the disassembler 1823 will try to disassemble the 32-bit binary again using the alias's rule, or 1824 try to convert the IR to the form of the alias. In the case of the multiple 1825 aliases, the aliases are tried one by one from the highest priority 1826 (currently the flag F_P3) to the lowest priority (no priority flag), and the 1827 first succeeds first adopted. 1828 1829 You may ask why there is a need for the conversion of IR from one form to 1830 another in handling certain aliases. This is because on one hand it avoids 1831 adding more operand code to handle unusual encoding/decoding; on other 1832 hand, during the disassembling, the conversion is an effective approach to 1833 check the condition of an alias (as an alias may be adopted only if certain 1834 conditions are met). 1835 1836 In order to speed up the alias opcode lookup, aarch64-gen has preprocessed 1837 aarch64_opcode_table and generated aarch64_find_alias_opcode and 1838 aarch64_find_next_alias_opcode (in opcodes/aarch64-dis-2.c) to help. */ 1839 1840 static void 1841 determine_disassembling_preference (struct aarch64_inst *inst) 1842 { 1843 const aarch64_opcode *opcode; 1844 const aarch64_opcode *alias; 1845 1846 opcode = inst->opcode; 1847 1848 /* This opcode does not have an alias, so use itself. */ 1849 if (opcode_has_alias (opcode) == FALSE) 1850 return; 1851 1852 alias = aarch64_find_alias_opcode (opcode); 1853 assert (alias); 1854 1855 #ifdef DEBUG_AARCH64 1856 if (debug_dump) 1857 { 1858 const aarch64_opcode *tmp = alias; 1859 printf ("#### LIST orderd: "); 1860 while (tmp) 1861 { 1862 printf ("%s, ", tmp->name); 1863 tmp = aarch64_find_next_alias_opcode (tmp); 1864 } 1865 printf ("\n"); 1866 } 1867 #endif /* DEBUG_AARCH64 */ 1868 1869 for (; alias; alias = aarch64_find_next_alias_opcode (alias)) 1870 { 1871 DEBUG_TRACE ("try %s", alias->name); 1872 assert (alias_opcode_p (alias)); 1873 1874 /* An alias can be a pseudo opcode which will never be used in the 1875 disassembly, e.g. BIC logical immediate is such a pseudo opcode 1876 aliasing AND. */ 1877 if (pseudo_opcode_p (alias)) 1878 { 1879 DEBUG_TRACE ("skip pseudo %s", alias->name); 1880 continue; 1881 } 1882 1883 if ((inst->value & alias->mask) != alias->opcode) 1884 { 1885 DEBUG_TRACE ("skip %s as base opcode not match", alias->name); 1886 continue; 1887 } 1888 /* No need to do any complicated transformation on operands, if the alias 1889 opcode does not have any operand. */ 1890 if (aarch64_num_of_operands (alias) == 0 && alias->opcode == inst->value) 1891 { 1892 DEBUG_TRACE ("succeed with 0-operand opcode %s", alias->name); 1893 aarch64_replace_opcode (inst, alias); 1894 return; 1895 } 1896 if (alias->flags & F_CONV) 1897 { 1898 aarch64_inst copy; 1899 memcpy (©, inst, sizeof (aarch64_inst)); 1900 /* ALIAS is the preference as long as the instruction can be 1901 successfully converted to the form of ALIAS. */ 1902 if (convert_to_alias (©, alias) == 1) 1903 { 1904 aarch64_replace_opcode (©, alias); 1905 assert (aarch64_match_operands_constraint (©, NULL)); 1906 DEBUG_TRACE ("succeed with %s via conversion", alias->name); 1907 memcpy (inst, ©, sizeof (aarch64_inst)); 1908 return; 1909 } 1910 } 1911 else 1912 { 1913 /* Directly decode the alias opcode. */ 1914 aarch64_inst temp; 1915 memset (&temp, '\0', sizeof (aarch64_inst)); 1916 if (aarch64_opcode_decode (alias, inst->value, &temp, 1) == 1) 1917 { 1918 DEBUG_TRACE ("succeed with %s via direct decoding", alias->name); 1919 memcpy (inst, &temp, sizeof (aarch64_inst)); 1920 return; 1921 } 1922 } 1923 } 1924 } 1925 1926 /* Decode the CODE according to OPCODE; fill INST. Return 0 if the decoding 1927 fails, which meanes that CODE is not an instruction of OPCODE; otherwise 1928 return 1. 1929 1930 If OPCODE has alias(es) and NOALIASES_P is 0, an alias opcode may be 1931 determined and used to disassemble CODE; this is done just before the 1932 return. */ 1933 1934 static int 1935 aarch64_opcode_decode (const aarch64_opcode *opcode, const aarch64_insn code, 1936 aarch64_inst *inst, int noaliases_p) 1937 { 1938 int i; 1939 1940 DEBUG_TRACE ("enter with %s", opcode->name); 1941 1942 assert (opcode && inst); 1943 1944 /* Check the base opcode. */ 1945 if ((code & opcode->mask) != (opcode->opcode & opcode->mask)) 1946 { 1947 DEBUG_TRACE ("base opcode match FAIL"); 1948 goto decode_fail; 1949 } 1950 1951 /* Clear inst. */ 1952 memset (inst, '\0', sizeof (aarch64_inst)); 1953 1954 inst->opcode = opcode; 1955 inst->value = code; 1956 1957 /* Assign operand codes and indexes. */ 1958 for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i) 1959 { 1960 if (opcode->operands[i] == AARCH64_OPND_NIL) 1961 break; 1962 inst->operands[i].type = opcode->operands[i]; 1963 inst->operands[i].idx = i; 1964 } 1965 1966 /* Call the opcode decoder indicated by flags. */ 1967 if (opcode_has_special_coder (opcode) && do_special_decoding (inst) == 0) 1968 { 1969 DEBUG_TRACE ("opcode flag-based decoder FAIL"); 1970 goto decode_fail; 1971 } 1972 1973 /* Call operand decoders. */ 1974 for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i) 1975 { 1976 const aarch64_operand *opnd; 1977 enum aarch64_opnd type; 1978 type = opcode->operands[i]; 1979 if (type == AARCH64_OPND_NIL) 1980 break; 1981 opnd = &aarch64_operands[type]; 1982 if (operand_has_extractor (opnd) 1983 && (! aarch64_extract_operand (opnd, &inst->operands[i], code, inst))) 1984 { 1985 DEBUG_TRACE ("operand decoder FAIL at operand %d", i); 1986 goto decode_fail; 1987 } 1988 } 1989 1990 /* Match the qualifiers. */ 1991 if (aarch64_match_operands_constraint (inst, NULL) == 1) 1992 { 1993 /* Arriving here, the CODE has been determined as a valid instruction 1994 of OPCODE and *INST has been filled with information of this OPCODE 1995 instruction. Before the return, check if the instruction has any 1996 alias and should be disassembled in the form of its alias instead. 1997 If the answer is yes, *INST will be updated. */ 1998 if (!noaliases_p) 1999 determine_disassembling_preference (inst); 2000 DEBUG_TRACE ("SUCCESS"); 2001 return 1; 2002 } 2003 else 2004 { 2005 DEBUG_TRACE ("constraint matching FAIL"); 2006 } 2007 2008 decode_fail: 2009 return 0; 2010 } 2011 2012 /* This does some user-friendly fix-up to *INST. It is currently focus on 2014 the adjustment of qualifiers to help the printed instruction 2015 recognized/understood more easily. */ 2016 2017 static void 2018 user_friendly_fixup (aarch64_inst *inst) 2019 { 2020 switch (inst->opcode->iclass) 2021 { 2022 case testbranch: 2023 /* TBNZ Xn|Wn, #uimm6, label 2024 Test and Branch Not Zero: conditionally jumps to label if bit number 2025 uimm6 in register Xn is not zero. The bit number implies the width of 2026 the register, which may be written and should be disassembled as Wn if 2027 uimm is less than 32. Limited to a branch offset range of +/- 32KiB. 2028 */ 2029 if (inst->operands[1].imm.value < 32) 2030 inst->operands[0].qualifier = AARCH64_OPND_QLF_W; 2031 break; 2032 default: break; 2033 } 2034 } 2035 2036 /* Decode INSN and fill in *INST the instruction information. */ 2037 2038 static int 2039 disas_aarch64_insn (uint64_t pc ATTRIBUTE_UNUSED, uint32_t insn, 2040 aarch64_inst *inst) 2041 { 2042 const aarch64_opcode *opcode = aarch64_opcode_lookup (insn); 2043 2044 #ifdef DEBUG_AARCH64 2045 if (debug_dump) 2046 { 2047 const aarch64_opcode *tmp = opcode; 2048 printf ("\n"); 2049 DEBUG_TRACE ("opcode lookup:"); 2050 while (tmp != NULL) 2051 { 2052 aarch64_verbose (" %s", tmp->name); 2053 tmp = aarch64_find_next_opcode (tmp); 2054 } 2055 } 2056 #endif /* DEBUG_AARCH64 */ 2057 2058 /* A list of opcodes may have been found, as aarch64_opcode_lookup cannot 2059 distinguish some opcodes, e.g. SSHR and MOVI, which almost share the same 2060 opcode field and value, apart from the difference that one of them has an 2061 extra field as part of the opcode, but such a field is used for operand 2062 encoding in other opcode(s) ('immh' in the case of the example). */ 2063 while (opcode != NULL) 2064 { 2065 /* But only one opcode can be decoded successfully for, as the 2066 decoding routine will check the constraint carefully. */ 2067 if (aarch64_opcode_decode (opcode, insn, inst, no_aliases) == 1) 2068 return ERR_OK; 2069 opcode = aarch64_find_next_opcode (opcode); 2070 } 2071 2072 return ERR_UND; 2073 } 2074 2075 /* Print operands. */ 2076 2077 static void 2078 print_operands (bfd_vma pc, const aarch64_opcode *opcode, 2079 const aarch64_opnd_info *opnds, struct disassemble_info *info) 2080 { 2081 int i, pcrel_p, num_printed; 2082 for (i = 0, num_printed = 0; i < AARCH64_MAX_OPND_NUM; ++i) 2083 { 2084 const size_t size = 128; 2085 char str[size]; 2086 /* We regard the opcode operand info more, however we also look into 2087 the inst->operands to support the disassembling of the optional 2088 operand. 2089 The two operand code should be the same in all cases, apart from 2090 when the operand can be optional. */ 2091 if (opcode->operands[i] == AARCH64_OPND_NIL 2092 || opnds[i].type == AARCH64_OPND_NIL) 2093 break; 2094 2095 /* Generate the operand string in STR. */ 2096 aarch64_print_operand (str, size, pc, opcode, opnds, i, &pcrel_p, 2097 &info->target); 2098 2099 /* Print the delimiter (taking account of omitted operand(s)). */ 2100 if (str[0] != '\0') 2101 (*info->fprintf_func) (info->stream, "%s", 2102 num_printed++ == 0 ? "\t" : ", "); 2103 2104 /* Print the operand. */ 2105 if (pcrel_p) 2106 (*info->print_address_func) (info->target, info); 2107 else 2108 (*info->fprintf_func) (info->stream, "%s", str); 2109 } 2110 } 2111 2112 /* Print the instruction mnemonic name. */ 2113 2114 static void 2115 print_mnemonic_name (const aarch64_inst *inst, struct disassemble_info *info) 2116 { 2117 if (inst->opcode->flags & F_COND) 2118 { 2119 /* For instructions that are truly conditionally executed, e.g. b.cond, 2120 prepare the full mnemonic name with the corresponding condition 2121 suffix. */ 2122 char name[8], *ptr; 2123 size_t len; 2124 2125 ptr = strchr (inst->opcode->name, '.'); 2126 assert (ptr && inst->cond); 2127 len = ptr - inst->opcode->name; 2128 assert (len < 8); 2129 strncpy (name, inst->opcode->name, len); 2130 name [len] = '\0'; 2131 (*info->fprintf_func) (info->stream, "%s.%s", name, inst->cond->names[0]); 2132 } 2133 else 2134 (*info->fprintf_func) (info->stream, "%s", inst->opcode->name); 2135 } 2136 2137 /* Print the instruction according to *INST. */ 2138 2139 static void 2140 print_aarch64_insn (bfd_vma pc, const aarch64_inst *inst, 2141 struct disassemble_info *info) 2142 { 2143 print_mnemonic_name (inst, info); 2144 print_operands (pc, inst->opcode, inst->operands, info); 2145 } 2146 2147 /* Entry-point of the instruction disassembler and printer. */ 2148 2149 static void 2150 print_insn_aarch64_word (bfd_vma pc, 2151 uint32_t word, 2152 struct disassemble_info *info) 2153 { 2154 static const char *err_msg[6] = 2155 { 2156 [ERR_OK] = "_", 2157 [-ERR_UND] = "undefined", 2158 [-ERR_UNP] = "unpredictable", 2159 [-ERR_NYI] = "NYI" 2160 }; 2161 2162 int ret; 2163 aarch64_inst inst; 2164 2165 info->insn_info_valid = 1; 2166 info->branch_delay_insns = 0; 2167 info->data_size = 0; 2168 info->target = 0; 2169 info->target2 = 0; 2170 2171 if (info->flags & INSN_HAS_RELOC) 2172 /* If the instruction has a reloc associated with it, then 2173 the offset field in the instruction will actually be the 2174 addend for the reloc. (If we are using REL type relocs). 2175 In such cases, we can ignore the pc when computing 2176 addresses, since the addend is not currently pc-relative. */ 2177 pc = 0; 2178 2179 ret = disas_aarch64_insn (pc, word, &inst); 2180 2181 if (((word >> 21) & 0x3ff) == 1) 2182 { 2183 /* RESERVED for ALES. */ 2184 assert (ret != ERR_OK); 2185 ret = ERR_NYI; 2186 } 2187 2188 switch (ret) 2189 { 2190 case ERR_UND: 2191 case ERR_UNP: 2192 case ERR_NYI: 2193 /* Handle undefined instructions. */ 2194 info->insn_type = dis_noninsn; 2195 (*info->fprintf_func) (info->stream,".inst\t0x%08x ; %s", 2196 word, err_msg[-ret]); 2197 break; 2198 case ERR_OK: 2199 user_friendly_fixup (&inst); 2200 print_aarch64_insn (pc, &inst, info); 2201 break; 2202 default: 2203 abort (); 2204 } 2205 } 2206 2207 /* Disallow mapping symbols ($x, $d etc) from 2208 being displayed in symbol relative addresses. */ 2209 2210 bfd_boolean 2211 aarch64_symbol_is_valid (asymbol * sym, 2212 struct disassemble_info * info ATTRIBUTE_UNUSED) 2213 { 2214 const char * name; 2215 2216 if (sym == NULL) 2217 return FALSE; 2218 2219 name = bfd_asymbol_name (sym); 2220 2221 return name 2222 && (name[0] != '$' 2223 || (name[1] != 'x' && name[1] != 'd') 2224 || (name[2] != '\0' && name[2] != '.')); 2225 } 2226 2227 /* Print data bytes on INFO->STREAM. */ 2228 2229 static void 2230 print_insn_data (bfd_vma pc ATTRIBUTE_UNUSED, 2231 uint32_t word, 2232 struct disassemble_info *info) 2233 { 2234 switch (info->bytes_per_chunk) 2235 { 2236 case 1: 2237 info->fprintf_func (info->stream, ".byte\t0x%02x", word); 2238 break; 2239 case 2: 2240 info->fprintf_func (info->stream, ".short\t0x%04x", word); 2241 break; 2242 case 4: 2243 info->fprintf_func (info->stream, ".word\t0x%08x", word); 2244 break; 2245 default: 2246 abort (); 2247 } 2248 } 2249 2250 /* Try to infer the code or data type from a symbol. 2251 Returns nonzero if *MAP_TYPE was set. */ 2252 2253 static int 2254 get_sym_code_type (struct disassemble_info *info, int n, 2255 enum map_type *map_type) 2256 { 2257 elf_symbol_type *es; 2258 unsigned int type; 2259 const char *name; 2260 2261 es = *(elf_symbol_type **)(info->symtab + n); 2262 type = ELF_ST_TYPE (es->internal_elf_sym.st_info); 2263 2264 /* If the symbol has function type then use that. */ 2265 if (type == STT_FUNC) 2266 { 2267 *map_type = MAP_INSN; 2268 return TRUE; 2269 } 2270 2271 /* Check for mapping symbols. */ 2272 name = bfd_asymbol_name(info->symtab[n]); 2273 if (name[0] == '$' 2274 && (name[1] == 'x' || name[1] == 'd') 2275 && (name[2] == '\0' || name[2] == '.')) 2276 { 2277 *map_type = (name[1] == 'x' ? MAP_INSN : MAP_DATA); 2278 return TRUE; 2279 } 2280 2281 return FALSE; 2282 } 2283 2284 /* Entry-point of the AArch64 disassembler. */ 2285 2286 int 2287 print_insn_aarch64 (bfd_vma pc, 2288 struct disassemble_info *info) 2289 { 2290 bfd_byte buffer[INSNLEN]; 2291 int status; 2292 void (*printer) (bfd_vma, uint32_t, struct disassemble_info *); 2293 bfd_boolean found = FALSE; 2294 unsigned int size = 4; 2295 unsigned long data; 2296 2297 if (info->disassembler_options) 2298 { 2299 set_default_aarch64_dis_options (info); 2300 2301 parse_aarch64_dis_options (info->disassembler_options); 2302 2303 /* To avoid repeated parsing of these options, we remove them here. */ 2304 info->disassembler_options = NULL; 2305 } 2306 2307 /* Aarch64 instructions are always little-endian */ 2308 info->endian_code = BFD_ENDIAN_LITTLE; 2309 2310 /* First check the full symtab for a mapping symbol, even if there 2311 are no usable non-mapping symbols for this address. */ 2312 if (info->symtab_size != 0 2313 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour) 2314 { 2315 enum map_type type = MAP_INSN; 2316 int last_sym = -1; 2317 bfd_vma addr; 2318 int n; 2319 2320 if (pc <= last_mapping_addr) 2321 last_mapping_sym = -1; 2322 2323 /* Start scanning at the start of the function, or wherever 2324 we finished last time. */ 2325 n = info->symtab_pos + 1; 2326 if (n < last_mapping_sym) 2327 n = last_mapping_sym; 2328 2329 /* Scan up to the location being disassembled. */ 2330 for (; n < info->symtab_size; n++) 2331 { 2332 addr = bfd_asymbol_value (info->symtab[n]); 2333 if (addr > pc) 2334 break; 2335 if ((info->section == NULL 2336 || info->section == info->symtab[n]->section) 2337 && get_sym_code_type (info, n, &type)) 2338 { 2339 last_sym = n; 2340 found = TRUE; 2341 } 2342 } 2343 2344 if (!found) 2345 { 2346 n = info->symtab_pos; 2347 if (n < last_mapping_sym) 2348 n = last_mapping_sym; 2349 2350 /* No mapping symbol found at this address. Look backwards 2351 for a preceeding one. */ 2352 for (; n >= 0; n--) 2353 { 2354 if (get_sym_code_type (info, n, &type)) 2355 { 2356 last_sym = n; 2357 found = TRUE; 2358 break; 2359 } 2360 } 2361 } 2362 2363 last_mapping_sym = last_sym; 2364 last_type = type; 2365 2366 /* Look a little bit ahead to see if we should print out 2367 less than four bytes of data. If there's a symbol, 2368 mapping or otherwise, after two bytes then don't 2369 print more. */ 2370 if (last_type == MAP_DATA) 2371 { 2372 size = 4 - (pc & 3); 2373 for (n = last_sym + 1; n < info->symtab_size; n++) 2374 { 2375 addr = bfd_asymbol_value (info->symtab[n]); 2376 if (addr > pc) 2377 { 2378 if (addr - pc < size) 2379 size = addr - pc; 2380 break; 2381 } 2382 } 2383 /* If the next symbol is after three bytes, we need to 2384 print only part of the data, so that we can use either 2385 .byte or .short. */ 2386 if (size == 3) 2387 size = (pc & 1) ? 1 : 2; 2388 } 2389 } 2390 2391 if (last_type == MAP_DATA) 2392 { 2393 /* size was set above. */ 2394 info->bytes_per_chunk = size; 2395 info->display_endian = info->endian; 2396 printer = print_insn_data; 2397 } 2398 else 2399 { 2400 info->bytes_per_chunk = size = INSNLEN; 2401 info->display_endian = info->endian_code; 2402 printer = print_insn_aarch64_word; 2403 } 2404 2405 status = (*info->read_memory_func) (pc, buffer, size, info); 2406 if (status != 0) 2407 { 2408 (*info->memory_error_func) (status, pc, info); 2409 return -1; 2410 } 2411 2412 data = bfd_get_bits (buffer, size * 8, 2413 info->display_endian == BFD_ENDIAN_BIG); 2414 2415 (*printer) (pc, data, info); 2416 2417 return size; 2418 } 2419 2420 void 2422 print_aarch64_disassembler_options (FILE *stream) 2423 { 2424 fprintf (stream, _("\n\ 2425 The following AARCH64 specific disassembler options are supported for use\n\ 2426 with the -M switch (multiple options should be separated by commas):\n")); 2427 2428 fprintf (stream, _("\n\ 2429 no-aliases Don't print instruction aliases.\n")); 2430 2431 fprintf (stream, _("\n\ 2432 aliases Do print instruction aliases.\n")); 2433 2434 #ifdef DEBUG_AARCH64 2435 fprintf (stream, _("\n\ 2436 debug_dump Temp switch for debug trace.\n")); 2437 #endif /* DEBUG_AARCH64 */ 2438 2439 fprintf (stream, _("\n")); 2440 } 2441