AArch64Disassembler.cpp 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. //===- AArch64Disassembler.cpp - Disassembler for AArch64 -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "AArch64Disassembler.h"
  12. #include "AArch64ExternalSymbolizer.h"
  13. #include "MCTargetDesc/AArch64AddressingModes.h"
  14. #include "MCTargetDesc/AArch64MCTargetDesc.h"
  15. #include "TargetInfo/AArch64TargetInfo.h"
  16. #include "Utils/AArch64BaseInfo.h"
  17. #include "llvm-c/Disassembler.h"
  18. #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
  19. #include "llvm/MC/MCFixedLenDisassembler.h"
  20. #include "llvm/MC/MCInst.h"
  21. #include "llvm/MC/MCRegisterInfo.h"
  22. #include "llvm/MC/MCSubtargetInfo.h"
  23. #include "llvm/MC/TargetRegistry.h"
  24. #include "llvm/Support/Compiler.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include <algorithm>
  28. #include <memory>
  29. using namespace llvm;
  30. #define DEBUG_TYPE "aarch64-disassembler"
  31. // Pull DecodeStatus and its enum values into the global namespace.
  32. using DecodeStatus = MCDisassembler::DecodeStatus;
  33. // Forward declare these because the autogenerated code will reference them.
  34. // Definitions are further down.
  35. static DecodeStatus DecodeFPR128RegisterClass(MCInst &Inst,
  36. unsigned RegNo, uint64_t Address,
  37. const void *Decoder);
  38. static DecodeStatus DecodeFPR128_loRegisterClass(MCInst &Inst,
  39. unsigned RegNo,
  40. uint64_t Address,
  41. const void *Decoder);
  42. static DecodeStatus DecodeFPR64RegisterClass(MCInst &Inst, unsigned RegNo,
  43. uint64_t Address,
  44. const void *Decoder);
  45. static DecodeStatus DecodeFPR32RegisterClass(MCInst &Inst, unsigned RegNo,
  46. uint64_t Address,
  47. const void *Decoder);
  48. static DecodeStatus DecodeFPR16RegisterClass(MCInst &Inst, unsigned RegNo,
  49. uint64_t Address,
  50. const void *Decoder);
  51. static DecodeStatus DecodeFPR8RegisterClass(MCInst &Inst, unsigned RegNo,
  52. uint64_t Address,
  53. const void *Decoder);
  54. static DecodeStatus DecodeGPR64commonRegisterClass(MCInst &Inst, unsigned RegNo,
  55. uint64_t Address,
  56. const void *Decoder);
  57. static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst, unsigned RegNo,
  58. uint64_t Address,
  59. const void *Decoder);
  60. static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst &Inst,
  61. unsigned RegNo,
  62. uint64_t Address,
  63. const void *Decoder);
  64. static DecodeStatus DecodeGPR64spRegisterClass(MCInst &Inst,
  65. unsigned RegNo, uint64_t Address,
  66. const void *Decoder);
  67. static DecodeStatus DecodeMatrixIndexGPR32_12_15RegisterClass(MCInst &Inst,
  68. unsigned RegNo,
  69. uint64_t Address,
  70. const void *Decoder);
  71. static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo,
  72. uint64_t Address,
  73. const void *Decoder);
  74. static DecodeStatus DecodeGPR32spRegisterClass(MCInst &Inst,
  75. unsigned RegNo, uint64_t Address,
  76. const void *Decoder);
  77. static DecodeStatus DecodeQQRegisterClass(MCInst &Inst, unsigned RegNo,
  78. uint64_t Address,
  79. const void *Decoder);
  80. static DecodeStatus DecodeQQQRegisterClass(MCInst &Inst, unsigned RegNo,
  81. uint64_t Address,
  82. const void *Decoder);
  83. static DecodeStatus DecodeQQQQRegisterClass(MCInst &Inst, unsigned RegNo,
  84. uint64_t Address,
  85. const void *Decoder);
  86. static DecodeStatus DecodeDDRegisterClass(MCInst &Inst, unsigned RegNo,
  87. uint64_t Address,
  88. const void *Decoder);
  89. static DecodeStatus DecodeDDDRegisterClass(MCInst &Inst, unsigned RegNo,
  90. uint64_t Address,
  91. const void *Decoder);
  92. static DecodeStatus DecodeDDDDRegisterClass(MCInst &Inst, unsigned RegNo,
  93. uint64_t Address,
  94. const void *Decoder);
  95. static DecodeStatus DecodeZPRRegisterClass(MCInst &Inst, unsigned RegNo,
  96. uint64_t Address,
  97. const void *Decoder);
  98. static DecodeStatus DecodeZPR_4bRegisterClass(MCInst &Inst, unsigned RegNo,
  99. uint64_t Address,
  100. const void *Decoder);
  101. static DecodeStatus DecodeZPR_3bRegisterClass(MCInst &Inst, unsigned RegNo,
  102. uint64_t Address,
  103. const void *Decoder);
  104. static DecodeStatus DecodeZPR2RegisterClass(MCInst &Inst, unsigned RegNo,
  105. uint64_t Address,
  106. const void *Decoder);
  107. static DecodeStatus DecodeZPR3RegisterClass(MCInst &Inst, unsigned RegNo,
  108. uint64_t Address,
  109. const void *Decoder);
  110. static DecodeStatus DecodeZPR4RegisterClass(MCInst &Inst, unsigned RegNo,
  111. uint64_t Address,
  112. const void *Decoder);
  113. template <unsigned NumBitsForTile>
  114. static DecodeStatus DecodeMatrixTile(MCInst &Inst, unsigned RegNo,
  115. uint64_t Address, const void *Decoder);
  116. static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst &Inst,
  117. unsigned RegMask,
  118. uint64_t Address,
  119. const void *Decoder);
  120. static DecodeStatus DecodePPRRegisterClass(MCInst &Inst, unsigned RegNo,
  121. uint64_t Address,
  122. const void *Decoder);
  123. static DecodeStatus DecodePPR_3bRegisterClass(MCInst &Inst, unsigned RegNo,
  124. uint64_t Address,
  125. const void *Decoder);
  126. static DecodeStatus DecodeFixedPointScaleImm32(MCInst &Inst, unsigned Imm,
  127. uint64_t Address,
  128. const void *Decoder);
  129. static DecodeStatus DecodeFixedPointScaleImm64(MCInst &Inst, unsigned Imm,
  130. uint64_t Address,
  131. const void *Decoder);
  132. static DecodeStatus DecodePCRelLabel19(MCInst &Inst, unsigned Imm,
  133. uint64_t Address, const void *Decoder);
  134. static DecodeStatus DecodeMemExtend(MCInst &Inst, unsigned Imm,
  135. uint64_t Address, const void *Decoder);
  136. static DecodeStatus DecodeMRSSystemRegister(MCInst &Inst, unsigned Imm,
  137. uint64_t Address, const void *Decoder);
  138. static DecodeStatus DecodeMSRSystemRegister(MCInst &Inst, unsigned Imm,
  139. uint64_t Address, const void *Decoder);
  140. static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst &Inst, uint32_t insn,
  141. uint64_t Address,
  142. const void *Decoder);
  143. static DecodeStatus DecodeMoveImmInstruction(MCInst &Inst, uint32_t insn,
  144. uint64_t Address,
  145. const void *Decoder);
  146. static DecodeStatus DecodeUnsignedLdStInstruction(MCInst &Inst, uint32_t insn,
  147. uint64_t Address,
  148. const void *Decoder);
  149. static DecodeStatus DecodeSignedLdStInstruction(MCInst &Inst, uint32_t insn,
  150. uint64_t Address,
  151. const void *Decoder);
  152. static DecodeStatus DecodeExclusiveLdStInstruction(MCInst &Inst, uint32_t insn,
  153. uint64_t Address,
  154. const void *Decoder);
  155. static DecodeStatus DecodePairLdStInstruction(MCInst &Inst, uint32_t insn,
  156. uint64_t Address,
  157. const void *Decoder);
  158. static DecodeStatus DecodeAuthLoadInstruction(MCInst &Inst, uint32_t insn,
  159. uint64_t Address,
  160. const void *Decoder);
  161. static DecodeStatus DecodeAddSubERegInstruction(MCInst &Inst, uint32_t insn,
  162. uint64_t Address,
  163. const void *Decoder);
  164. static DecodeStatus DecodeLogicalImmInstruction(MCInst &Inst, uint32_t insn,
  165. uint64_t Address,
  166. const void *Decoder);
  167. static DecodeStatus DecodeModImmInstruction(MCInst &Inst, uint32_t insn,
  168. uint64_t Address,
  169. const void *Decoder);
  170. static DecodeStatus DecodeModImmTiedInstruction(MCInst &Inst, uint32_t insn,
  171. uint64_t Address,
  172. const void *Decoder);
  173. static DecodeStatus DecodeAdrInstruction(MCInst &Inst, uint32_t insn,
  174. uint64_t Address, const void *Decoder);
  175. static DecodeStatus DecodeAddSubImmShift(MCInst &Inst, uint32_t insn,
  176. uint64_t Address, const void *Decoder);
  177. static DecodeStatus DecodeUnconditionalBranch(MCInst &Inst, uint32_t insn,
  178. uint64_t Address,
  179. const void *Decoder);
  180. static DecodeStatus DecodeSystemPStateInstruction(MCInst &Inst, uint32_t insn,
  181. uint64_t Address,
  182. const void *Decoder);
  183. static DecodeStatus DecodeTestAndBranch(MCInst &Inst, uint32_t insn,
  184. uint64_t Address, const void *Decoder);
  185. static DecodeStatus DecodeFMOVLaneInstruction(MCInst &Inst, unsigned Insn,
  186. uint64_t Address,
  187. const void *Decoder);
  188. static DecodeStatus DecodeVecShiftR64Imm(MCInst &Inst, unsigned Imm,
  189. uint64_t Addr, const void *Decoder);
  190. static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst &Inst, unsigned Imm,
  191. uint64_t Addr,
  192. const void *Decoder);
  193. static DecodeStatus DecodeVecShiftR32Imm(MCInst &Inst, unsigned Imm,
  194. uint64_t Addr, const void *Decoder);
  195. static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst &Inst, unsigned Imm,
  196. uint64_t Addr,
  197. const void *Decoder);
  198. static DecodeStatus DecodeVecShiftR16Imm(MCInst &Inst, unsigned Imm,
  199. uint64_t Addr, const void *Decoder);
  200. static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst &Inst, unsigned Imm,
  201. uint64_t Addr,
  202. const void *Decoder);
  203. static DecodeStatus DecodeVecShiftR8Imm(MCInst &Inst, unsigned Imm,
  204. uint64_t Addr, const void *Decoder);
  205. static DecodeStatus DecodeVecShiftL64Imm(MCInst &Inst, unsigned Imm,
  206. uint64_t Addr, const void *Decoder);
  207. static DecodeStatus DecodeVecShiftL32Imm(MCInst &Inst, unsigned Imm,
  208. uint64_t Addr, const void *Decoder);
  209. static DecodeStatus DecodeVecShiftL16Imm(MCInst &Inst, unsigned Imm,
  210. uint64_t Addr, const void *Decoder);
  211. static DecodeStatus DecodeVecShiftL8Imm(MCInst &Inst, unsigned Imm,
  212. uint64_t Addr, const void *Decoder);
  213. static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst &Inst,
  214. unsigned RegNo,
  215. uint64_t Addr,
  216. const void *Decoder);
  217. static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst &Inst,
  218. unsigned RegNo,
  219. uint64_t Addr,
  220. const void *Decoder);
  221. static DecodeStatus DecodeSVELogicalImmInstruction(MCInst &Inst, uint32_t insn,
  222. uint64_t Address,
  223. const void *Decoder);
  224. template <int Bits>
  225. static DecodeStatus DecodeSImm(MCInst &Inst, uint64_t Imm, uint64_t Address,
  226. const void *Decoder);
  227. template <int ElementWidth>
  228. static DecodeStatus DecodeImm8OptLsl(MCInst &Inst, unsigned Imm,
  229. uint64_t Addr, const void *Decoder);
  230. static DecodeStatus DecodeSVEIncDecImm(MCInst &Inst, unsigned Imm,
  231. uint64_t Addr, const void *Decoder);
  232. static DecodeStatus DecodeSVCROp(MCInst &Inst, unsigned Imm, uint64_t Address,
  233. const void *Decoder);
  234. static DecodeStatus DecodeCPYMemOpInstruction(MCInst &Inst, uint32_t insn,
  235. uint64_t Addr,
  236. const void *Decoder);
  237. static DecodeStatus DecodeSETMemOpInstruction(MCInst &Inst, uint32_t insn,
  238. uint64_t Addr,
  239. const void *Decoder);
  240. static bool Check(DecodeStatus &Out, DecodeStatus In) {
  241. switch (In) {
  242. case MCDisassembler::Success:
  243. // Out stays the same.
  244. return true;
  245. case MCDisassembler::SoftFail:
  246. Out = In;
  247. return true;
  248. case MCDisassembler::Fail:
  249. Out = In;
  250. return false;
  251. }
  252. llvm_unreachable("Invalid DecodeStatus!");
  253. }
  254. #include "AArch64GenDisassemblerTables.inc"
  255. #include "AArch64GenInstrInfo.inc"
  256. #define Success MCDisassembler::Success
  257. #define Fail MCDisassembler::Fail
  258. #define SoftFail MCDisassembler::SoftFail
  259. static MCDisassembler *createAArch64Disassembler(const Target &T,
  260. const MCSubtargetInfo &STI,
  261. MCContext &Ctx) {
  262. return new AArch64Disassembler(STI, Ctx);
  263. }
  264. DecodeStatus AArch64Disassembler::getInstruction(MCInst &MI, uint64_t &Size,
  265. ArrayRef<uint8_t> Bytes,
  266. uint64_t Address,
  267. raw_ostream &CS) const {
  268. CommentStream = &CS;
  269. Size = 0;
  270. // We want to read exactly 4 bytes of data.
  271. if (Bytes.size() < 4)
  272. return Fail;
  273. Size = 4;
  274. // Encoded as a small-endian 32-bit word in the stream.
  275. uint32_t Insn =
  276. (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | (Bytes[0] << 0);
  277. const uint8_t *Tables[] = {DecoderTable32, DecoderTableFallback32};
  278. for (auto Table : Tables) {
  279. DecodeStatus Result =
  280. decodeInstruction(Table, MI, Insn, Address, this, STI);
  281. switch (MI.getOpcode()) {
  282. default:
  283. break;
  284. // For Scalable Matrix Extension (SME) instructions that have an implicit
  285. // operand for the accumulator (ZA) which isn't encoded, manually insert
  286. // operand.
  287. case AArch64::LDR_ZA:
  288. case AArch64::STR_ZA: {
  289. MI.insert(MI.begin(), MCOperand::createReg(AArch64::ZA));
  290. // Spill and fill instructions have a single immediate used for both the
  291. // vector select offset and optional memory offset. Replicate the decoded
  292. // immediate.
  293. const MCOperand &Imm4Op = MI.getOperand(2);
  294. assert(Imm4Op.isImm() && "Unexpected operand type!");
  295. MI.addOperand(Imm4Op);
  296. break;
  297. }
  298. case AArch64::LD1_MXIPXX_H_B:
  299. case AArch64::LD1_MXIPXX_V_B:
  300. case AArch64::ST1_MXIPXX_H_B:
  301. case AArch64::ST1_MXIPXX_V_B:
  302. case AArch64::INSERT_MXIPZ_H_B:
  303. case AArch64::INSERT_MXIPZ_V_B:
  304. // e.g.
  305. // MOVA ZA0<HV>.B[<Ws>, <imm>], <Pg>/M, <Zn>.B
  306. // ^ insert implicit 8-bit element tile
  307. MI.insert(MI.begin(), MCOperand::createReg(AArch64::ZAB0));
  308. break;
  309. case AArch64::EXTRACT_ZPMXI_H_B:
  310. case AArch64::EXTRACT_ZPMXI_V_B:
  311. // MOVA <Zd>.B, <Pg>/M, ZA0<HV>.B[<Ws>, <imm>]
  312. // ^ insert implicit 8-bit element tile
  313. MI.insert(MI.begin()+2, MCOperand::createReg(AArch64::ZAB0));
  314. break;
  315. case AArch64::LD1_MXIPXX_H_Q:
  316. case AArch64::LD1_MXIPXX_V_Q:
  317. case AArch64::ST1_MXIPXX_H_Q:
  318. case AArch64::ST1_MXIPXX_V_Q:
  319. // 128-bit load/store have implicit zero vector index.
  320. MI.insert(MI.begin()+2, MCOperand::createImm(0));
  321. break;
  322. // 128-bit mova have implicit zero vector index.
  323. case AArch64::INSERT_MXIPZ_H_Q:
  324. case AArch64::INSERT_MXIPZ_V_Q:
  325. MI.insert(MI.begin()+2, MCOperand::createImm(0));
  326. break;
  327. case AArch64::EXTRACT_ZPMXI_H_Q:
  328. case AArch64::EXTRACT_ZPMXI_V_Q:
  329. MI.addOperand(MCOperand::createImm(0));
  330. break;
  331. case AArch64::SMOVvi8to32_idx0:
  332. case AArch64::SMOVvi8to64_idx0:
  333. case AArch64::SMOVvi16to32_idx0:
  334. case AArch64::SMOVvi16to64_idx0:
  335. case AArch64::SMOVvi32to64_idx0:
  336. case AArch64::UMOVvi8_idx0:
  337. case AArch64::UMOVvi16_idx0:
  338. case AArch64::UMOVvi32_idx0:
  339. case AArch64::UMOVvi64_idx0:
  340. MI.addOperand(MCOperand::createImm(0));
  341. break;
  342. }
  343. if (Result != MCDisassembler::Fail)
  344. return Result;
  345. }
  346. return MCDisassembler::Fail;
  347. }
  348. static MCSymbolizer *
  349. createAArch64ExternalSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
  350. LLVMSymbolLookupCallback SymbolLookUp,
  351. void *DisInfo, MCContext *Ctx,
  352. std::unique_ptr<MCRelocationInfo> &&RelInfo) {
  353. return new AArch64ExternalSymbolizer(*Ctx, std::move(RelInfo), GetOpInfo,
  354. SymbolLookUp, DisInfo);
  355. }
  356. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64Disassembler() {
  357. TargetRegistry::RegisterMCDisassembler(getTheAArch64leTarget(),
  358. createAArch64Disassembler);
  359. TargetRegistry::RegisterMCDisassembler(getTheAArch64beTarget(),
  360. createAArch64Disassembler);
  361. TargetRegistry::RegisterMCSymbolizer(getTheAArch64leTarget(),
  362. createAArch64ExternalSymbolizer);
  363. TargetRegistry::RegisterMCSymbolizer(getTheAArch64beTarget(),
  364. createAArch64ExternalSymbolizer);
  365. TargetRegistry::RegisterMCDisassembler(getTheAArch64_32Target(),
  366. createAArch64Disassembler);
  367. TargetRegistry::RegisterMCSymbolizer(getTheAArch64_32Target(),
  368. createAArch64ExternalSymbolizer);
  369. TargetRegistry::RegisterMCDisassembler(getTheARM64Target(),
  370. createAArch64Disassembler);
  371. TargetRegistry::RegisterMCSymbolizer(getTheARM64Target(),
  372. createAArch64ExternalSymbolizer);
  373. TargetRegistry::RegisterMCDisassembler(getTheARM64_32Target(),
  374. createAArch64Disassembler);
  375. TargetRegistry::RegisterMCSymbolizer(getTheARM64_32Target(),
  376. createAArch64ExternalSymbolizer);
  377. }
  378. static DecodeStatus DecodeFPR128RegisterClass(MCInst &Inst, unsigned RegNo,
  379. uint64_t Addr,
  380. const void *Decoder) {
  381. if (RegNo > 31)
  382. return Fail;
  383. unsigned Register =
  384. AArch64MCRegisterClasses[AArch64::FPR128RegClassID].getRegister(RegNo);
  385. Inst.addOperand(MCOperand::createReg(Register));
  386. return Success;
  387. }
  388. static DecodeStatus DecodeFPR128_loRegisterClass(MCInst &Inst, unsigned RegNo,
  389. uint64_t Addr,
  390. const void *Decoder) {
  391. if (RegNo > 15)
  392. return Fail;
  393. return DecodeFPR128RegisterClass(Inst, RegNo, Addr, Decoder);
  394. }
  395. static DecodeStatus DecodeFPR64RegisterClass(MCInst &Inst, unsigned RegNo,
  396. uint64_t Addr,
  397. const void *Decoder) {
  398. if (RegNo > 31)
  399. return Fail;
  400. unsigned Register =
  401. AArch64MCRegisterClasses[AArch64::FPR64RegClassID].getRegister(RegNo);
  402. Inst.addOperand(MCOperand::createReg(Register));
  403. return Success;
  404. }
  405. static DecodeStatus DecodeFPR32RegisterClass(MCInst &Inst, unsigned RegNo,
  406. uint64_t Addr,
  407. const void *Decoder) {
  408. if (RegNo > 31)
  409. return Fail;
  410. unsigned Register =
  411. AArch64MCRegisterClasses[AArch64::FPR32RegClassID].getRegister(RegNo);
  412. Inst.addOperand(MCOperand::createReg(Register));
  413. return Success;
  414. }
  415. static DecodeStatus DecodeFPR16RegisterClass(MCInst &Inst, unsigned RegNo,
  416. uint64_t Addr,
  417. const void *Decoder) {
  418. if (RegNo > 31)
  419. return Fail;
  420. unsigned Register =
  421. AArch64MCRegisterClasses[AArch64::FPR16RegClassID].getRegister(RegNo);
  422. Inst.addOperand(MCOperand::createReg(Register));
  423. return Success;
  424. }
  425. static DecodeStatus DecodeFPR8RegisterClass(MCInst &Inst, unsigned RegNo,
  426. uint64_t Addr,
  427. const void *Decoder) {
  428. if (RegNo > 31)
  429. return Fail;
  430. unsigned Register =
  431. AArch64MCRegisterClasses[AArch64::FPR8RegClassID].getRegister(RegNo);
  432. Inst.addOperand(MCOperand::createReg(Register));
  433. return Success;
  434. }
  435. static DecodeStatus DecodeGPR64commonRegisterClass(MCInst &Inst, unsigned RegNo,
  436. uint64_t Addr,
  437. const void *Decoder) {
  438. if (RegNo > 30)
  439. return Fail;
  440. unsigned Register =
  441. AArch64MCRegisterClasses[AArch64::GPR64commonRegClassID].getRegister(
  442. RegNo);
  443. Inst.addOperand(MCOperand::createReg(Register));
  444. return Success;
  445. }
  446. static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst, unsigned RegNo,
  447. uint64_t Addr,
  448. const void *Decoder) {
  449. if (RegNo > 31)
  450. return Fail;
  451. unsigned Register =
  452. AArch64MCRegisterClasses[AArch64::GPR64RegClassID].getRegister(RegNo);
  453. Inst.addOperand(MCOperand::createReg(Register));
  454. return Success;
  455. }
  456. static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst &Inst,
  457. unsigned RegNo,
  458. uint64_t Address,
  459. const void *Decoder) {
  460. if (RegNo > 22)
  461. return Fail;
  462. if (RegNo & 1)
  463. return Fail;
  464. unsigned Register =
  465. AArch64MCRegisterClasses[AArch64::GPR64x8ClassRegClassID].getRegister(
  466. RegNo >> 1);
  467. Inst.addOperand(MCOperand::createReg(Register));
  468. return Success;
  469. }
  470. static DecodeStatus DecodeGPR64spRegisterClass(MCInst &Inst, unsigned RegNo,
  471. uint64_t Addr,
  472. const void *Decoder) {
  473. if (RegNo > 31)
  474. return Fail;
  475. unsigned Register =
  476. AArch64MCRegisterClasses[AArch64::GPR64spRegClassID].getRegister(RegNo);
  477. Inst.addOperand(MCOperand::createReg(Register));
  478. return Success;
  479. }
  480. static DecodeStatus DecodeMatrixIndexGPR32_12_15RegisterClass(MCInst &Inst,
  481. unsigned RegNo,
  482. uint64_t Addr,
  483. const void *Decoder) {
  484. if (RegNo > 3)
  485. return Fail;
  486. unsigned Register =
  487. AArch64MCRegisterClasses[AArch64::MatrixIndexGPR32_12_15RegClassID]
  488. .getRegister(RegNo);
  489. Inst.addOperand(MCOperand::createReg(Register));
  490. return Success;
  491. }
  492. static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, unsigned RegNo,
  493. uint64_t Addr,
  494. const void *Decoder) {
  495. if (RegNo > 31)
  496. return Fail;
  497. unsigned Register =
  498. AArch64MCRegisterClasses[AArch64::GPR32RegClassID].getRegister(RegNo);
  499. Inst.addOperand(MCOperand::createReg(Register));
  500. return Success;
  501. }
  502. static DecodeStatus DecodeGPR32spRegisterClass(MCInst &Inst, unsigned RegNo,
  503. uint64_t Addr,
  504. const void *Decoder) {
  505. if (RegNo > 31)
  506. return Fail;
  507. unsigned Register =
  508. AArch64MCRegisterClasses[AArch64::GPR32spRegClassID].getRegister(RegNo);
  509. Inst.addOperand(MCOperand::createReg(Register));
  510. return Success;
  511. }
  512. static DecodeStatus DecodeZPRRegisterClass(MCInst &Inst, unsigned RegNo,
  513. uint64_t Address,
  514. const void* Decoder) {
  515. if (RegNo > 31)
  516. return Fail;
  517. unsigned Register =
  518. AArch64MCRegisterClasses[AArch64::ZPRRegClassID].getRegister(RegNo);
  519. Inst.addOperand(MCOperand::createReg(Register));
  520. return Success;
  521. }
  522. static DecodeStatus DecodeZPR_4bRegisterClass(MCInst &Inst, unsigned RegNo,
  523. uint64_t Address,
  524. const void *Decoder) {
  525. if (RegNo > 15)
  526. return Fail;
  527. return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
  528. }
  529. static DecodeStatus DecodeZPR_3bRegisterClass(MCInst &Inst, unsigned RegNo,
  530. uint64_t Address,
  531. const void *Decoder) {
  532. if (RegNo > 7)
  533. return Fail;
  534. return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
  535. }
  536. static DecodeStatus DecodeZPR2RegisterClass(MCInst &Inst, unsigned RegNo,
  537. uint64_t Address,
  538. const void* Decoder) {
  539. if (RegNo > 31)
  540. return Fail;
  541. unsigned Register =
  542. AArch64MCRegisterClasses[AArch64::ZPR2RegClassID].getRegister(RegNo);
  543. Inst.addOperand(MCOperand::createReg(Register));
  544. return Success;
  545. }
  546. static DecodeStatus DecodeZPR3RegisterClass(MCInst &Inst, unsigned RegNo,
  547. uint64_t Address,
  548. const void* Decoder) {
  549. if (RegNo > 31)
  550. return Fail;
  551. unsigned Register =
  552. AArch64MCRegisterClasses[AArch64::ZPR3RegClassID].getRegister(RegNo);
  553. Inst.addOperand(MCOperand::createReg(Register));
  554. return Success;
  555. }
  556. static DecodeStatus DecodeZPR4RegisterClass(MCInst &Inst, unsigned RegNo,
  557. uint64_t Address,
  558. const void* Decoder) {
  559. if (RegNo > 31)
  560. return Fail;
  561. unsigned Register =
  562. AArch64MCRegisterClasses[AArch64::ZPR4RegClassID].getRegister(RegNo);
  563. Inst.addOperand(MCOperand::createReg(Register));
  564. return Success;
  565. }
  566. static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst &Inst,
  567. unsigned RegMask,
  568. uint64_t Address,
  569. const void *Decoder) {
  570. if (RegMask > 0xFF)
  571. return Fail;
  572. Inst.addOperand(MCOperand::createImm(RegMask));
  573. return Success;
  574. }
  575. static const SmallVector<SmallVector<unsigned, 16>, 5>
  576. MatrixZATileDecoderTable = {
  577. {AArch64::ZAB0},
  578. {AArch64::ZAH0, AArch64::ZAH1},
  579. {AArch64::ZAS0, AArch64::ZAS1, AArch64::ZAS2, AArch64::ZAS3},
  580. {AArch64::ZAD0, AArch64::ZAD1, AArch64::ZAD2, AArch64::ZAD3,
  581. AArch64::ZAD4, AArch64::ZAD5, AArch64::ZAD6, AArch64::ZAD7},
  582. {AArch64::ZAQ0, AArch64::ZAQ1, AArch64::ZAQ2, AArch64::ZAQ3,
  583. AArch64::ZAQ4, AArch64::ZAQ5, AArch64::ZAQ6, AArch64::ZAQ7,
  584. AArch64::ZAQ8, AArch64::ZAQ9, AArch64::ZAQ10, AArch64::ZAQ11,
  585. AArch64::ZAQ12, AArch64::ZAQ13, AArch64::ZAQ14, AArch64::ZAQ15}};
  586. template <unsigned NumBitsForTile>
  587. static DecodeStatus DecodeMatrixTile(MCInst &Inst, unsigned RegNo,
  588. uint64_t Address, const void *Decoder) {
  589. unsigned LastReg = (1 << NumBitsForTile) - 1;
  590. if (RegNo > LastReg)
  591. return Fail;
  592. Inst.addOperand(
  593. MCOperand::createReg(MatrixZATileDecoderTable[NumBitsForTile][RegNo]));
  594. return Success;
  595. }
  596. static DecodeStatus DecodePPRRegisterClass(MCInst &Inst, unsigned RegNo,
  597. uint64_t Addr, const void *Decoder) {
  598. if (RegNo > 15)
  599. return Fail;
  600. unsigned Register =
  601. AArch64MCRegisterClasses[AArch64::PPRRegClassID].getRegister(RegNo);
  602. Inst.addOperand(MCOperand::createReg(Register));
  603. return Success;
  604. }
  605. static DecodeStatus DecodePPR_3bRegisterClass(MCInst &Inst, unsigned RegNo,
  606. uint64_t Addr,
  607. const void* Decoder) {
  608. if (RegNo > 7)
  609. return Fail;
  610. // Just reuse the PPR decode table
  611. return DecodePPRRegisterClass(Inst, RegNo, Addr, Decoder);
  612. }
  613. static DecodeStatus DecodeQQRegisterClass(MCInst &Inst, unsigned RegNo,
  614. uint64_t Addr, const void *Decoder) {
  615. if (RegNo > 31)
  616. return Fail;
  617. unsigned Register =
  618. AArch64MCRegisterClasses[AArch64::QQRegClassID].getRegister(RegNo);
  619. Inst.addOperand(MCOperand::createReg(Register));
  620. return Success;
  621. }
  622. static DecodeStatus DecodeQQQRegisterClass(MCInst &Inst, unsigned RegNo,
  623. uint64_t Addr, const void *Decoder) {
  624. if (RegNo > 31)
  625. return Fail;
  626. unsigned Register =
  627. AArch64MCRegisterClasses[AArch64::QQQRegClassID].getRegister(RegNo);
  628. Inst.addOperand(MCOperand::createReg(Register));
  629. return Success;
  630. }
  631. static DecodeStatus DecodeQQQQRegisterClass(MCInst &Inst, unsigned RegNo,
  632. uint64_t Addr,
  633. const void *Decoder) {
  634. if (RegNo > 31)
  635. return Fail;
  636. unsigned Register =
  637. AArch64MCRegisterClasses[AArch64::QQQQRegClassID].getRegister(RegNo);
  638. Inst.addOperand(MCOperand::createReg(Register));
  639. return Success;
  640. }
  641. static DecodeStatus DecodeDDRegisterClass(MCInst &Inst, unsigned RegNo,
  642. uint64_t Addr, const void *Decoder) {
  643. if (RegNo > 31)
  644. return Fail;
  645. unsigned Register =
  646. AArch64MCRegisterClasses[AArch64::DDRegClassID].getRegister(RegNo);
  647. Inst.addOperand(MCOperand::createReg(Register));
  648. return Success;
  649. }
  650. static DecodeStatus DecodeDDDRegisterClass(MCInst &Inst, unsigned RegNo,
  651. uint64_t Addr, const void *Decoder) {
  652. if (RegNo > 31)
  653. return Fail;
  654. unsigned Register =
  655. AArch64MCRegisterClasses[AArch64::DDDRegClassID].getRegister(RegNo);
  656. Inst.addOperand(MCOperand::createReg(Register));
  657. return Success;
  658. }
  659. static DecodeStatus DecodeDDDDRegisterClass(MCInst &Inst, unsigned RegNo,
  660. uint64_t Addr,
  661. const void *Decoder) {
  662. if (RegNo > 31)
  663. return Fail;
  664. unsigned Register =
  665. AArch64MCRegisterClasses[AArch64::DDDDRegClassID].getRegister(RegNo);
  666. Inst.addOperand(MCOperand::createReg(Register));
  667. return Success;
  668. }
  669. static DecodeStatus DecodeFixedPointScaleImm32(MCInst &Inst, unsigned Imm,
  670. uint64_t Addr,
  671. const void *Decoder) {
  672. // scale{5} is asserted as 1 in tblgen.
  673. Imm |= 0x20;
  674. Inst.addOperand(MCOperand::createImm(64 - Imm));
  675. return Success;
  676. }
  677. static DecodeStatus DecodeFixedPointScaleImm64(MCInst &Inst, unsigned Imm,
  678. uint64_t Addr,
  679. const void *Decoder) {
  680. Inst.addOperand(MCOperand::createImm(64 - Imm));
  681. return Success;
  682. }
  683. static DecodeStatus DecodePCRelLabel19(MCInst &Inst, unsigned Imm,
  684. uint64_t Addr, const void *Decoder) {
  685. int64_t ImmVal = Imm;
  686. const AArch64Disassembler *Dis =
  687. static_cast<const AArch64Disassembler *>(Decoder);
  688. // Sign-extend 19-bit immediate.
  689. if (ImmVal & (1 << (19 - 1)))
  690. ImmVal |= ~((1LL << 19) - 1);
  691. if (!Dis->tryAddingSymbolicOperand(Inst, ImmVal * 4, Addr,
  692. Inst.getOpcode() != AArch64::LDRXl, 0, 4))
  693. Inst.addOperand(MCOperand::createImm(ImmVal));
  694. return Success;
  695. }
  696. static DecodeStatus DecodeMemExtend(MCInst &Inst, unsigned Imm,
  697. uint64_t Address, const void *Decoder) {
  698. Inst.addOperand(MCOperand::createImm((Imm >> 1) & 1));
  699. Inst.addOperand(MCOperand::createImm(Imm & 1));
  700. return Success;
  701. }
  702. static DecodeStatus DecodeMRSSystemRegister(MCInst &Inst, unsigned Imm,
  703. uint64_t Address,
  704. const void *Decoder) {
  705. Inst.addOperand(MCOperand::createImm(Imm));
  706. // Every system register in the encoding space is valid with the syntax
  707. // S<op0>_<op1>_<Cn>_<Cm>_<op2>, so decoding system registers always succeeds.
  708. return Success;
  709. }
  710. static DecodeStatus DecodeMSRSystemRegister(MCInst &Inst, unsigned Imm,
  711. uint64_t Address,
  712. const void *Decoder) {
  713. Inst.addOperand(MCOperand::createImm(Imm));
  714. return Success;
  715. }
  716. static DecodeStatus DecodeFMOVLaneInstruction(MCInst &Inst, unsigned Insn,
  717. uint64_t Address,
  718. const void *Decoder) {
  719. // This decoder exists to add the dummy Lane operand to the MCInst, which must
  720. // be 1 in assembly but has no other real manifestation.
  721. unsigned Rd = fieldFromInstruction(Insn, 0, 5);
  722. unsigned Rn = fieldFromInstruction(Insn, 5, 5);
  723. unsigned IsToVec = fieldFromInstruction(Insn, 16, 1);
  724. if (IsToVec) {
  725. DecodeFPR128RegisterClass(Inst, Rd, Address, Decoder);
  726. DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);
  727. } else {
  728. DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
  729. DecodeFPR128RegisterClass(Inst, Rn, Address, Decoder);
  730. }
  731. // Add the lane
  732. Inst.addOperand(MCOperand::createImm(1));
  733. return Success;
  734. }
  735. static DecodeStatus DecodeVecShiftRImm(MCInst &Inst, unsigned Imm,
  736. unsigned Add) {
  737. Inst.addOperand(MCOperand::createImm(Add - Imm));
  738. return Success;
  739. }
  740. static DecodeStatus DecodeVecShiftLImm(MCInst &Inst, unsigned Imm,
  741. unsigned Add) {
  742. Inst.addOperand(MCOperand::createImm((Imm + Add) & (Add - 1)));
  743. return Success;
  744. }
  745. static DecodeStatus DecodeVecShiftR64Imm(MCInst &Inst, unsigned Imm,
  746. uint64_t Addr, const void *Decoder) {
  747. return DecodeVecShiftRImm(Inst, Imm, 64);
  748. }
  749. static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst &Inst, unsigned Imm,
  750. uint64_t Addr,
  751. const void *Decoder) {
  752. return DecodeVecShiftRImm(Inst, Imm | 0x20, 64);
  753. }
  754. static DecodeStatus DecodeVecShiftR32Imm(MCInst &Inst, unsigned Imm,
  755. uint64_t Addr, const void *Decoder) {
  756. return DecodeVecShiftRImm(Inst, Imm, 32);
  757. }
  758. static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst &Inst, unsigned Imm,
  759. uint64_t Addr,
  760. const void *Decoder) {
  761. return DecodeVecShiftRImm(Inst, Imm | 0x10, 32);
  762. }
  763. static DecodeStatus DecodeVecShiftR16Imm(MCInst &Inst, unsigned Imm,
  764. uint64_t Addr, const void *Decoder) {
  765. return DecodeVecShiftRImm(Inst, Imm, 16);
  766. }
  767. static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst &Inst, unsigned Imm,
  768. uint64_t Addr,
  769. const void *Decoder) {
  770. return DecodeVecShiftRImm(Inst, Imm | 0x8, 16);
  771. }
  772. static DecodeStatus DecodeVecShiftR8Imm(MCInst &Inst, unsigned Imm,
  773. uint64_t Addr, const void *Decoder) {
  774. return DecodeVecShiftRImm(Inst, Imm, 8);
  775. }
  776. static DecodeStatus DecodeVecShiftL64Imm(MCInst &Inst, unsigned Imm,
  777. uint64_t Addr, const void *Decoder) {
  778. return DecodeVecShiftLImm(Inst, Imm, 64);
  779. }
  780. static DecodeStatus DecodeVecShiftL32Imm(MCInst &Inst, unsigned Imm,
  781. uint64_t Addr, const void *Decoder) {
  782. return DecodeVecShiftLImm(Inst, Imm, 32);
  783. }
  784. static DecodeStatus DecodeVecShiftL16Imm(MCInst &Inst, unsigned Imm,
  785. uint64_t Addr, const void *Decoder) {
  786. return DecodeVecShiftLImm(Inst, Imm, 16);
  787. }
  788. static DecodeStatus DecodeVecShiftL8Imm(MCInst &Inst, unsigned Imm,
  789. uint64_t Addr, const void *Decoder) {
  790. return DecodeVecShiftLImm(Inst, Imm, 8);
  791. }
  792. static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst &Inst, uint32_t insn,
  793. uint64_t Addr,
  794. const void *Decoder) {
  795. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  796. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  797. unsigned Rm = fieldFromInstruction(insn, 16, 5);
  798. unsigned shiftHi = fieldFromInstruction(insn, 22, 2);
  799. unsigned shiftLo = fieldFromInstruction(insn, 10, 6);
  800. unsigned shift = (shiftHi << 6) | shiftLo;
  801. switch (Inst.getOpcode()) {
  802. default:
  803. return Fail;
  804. case AArch64::ADDWrs:
  805. case AArch64::ADDSWrs:
  806. case AArch64::SUBWrs:
  807. case AArch64::SUBSWrs:
  808. // if shift == '11' then ReservedValue()
  809. if (shiftHi == 0x3)
  810. return Fail;
  811. LLVM_FALLTHROUGH;
  812. case AArch64::ANDWrs:
  813. case AArch64::ANDSWrs:
  814. case AArch64::BICWrs:
  815. case AArch64::BICSWrs:
  816. case AArch64::ORRWrs:
  817. case AArch64::ORNWrs:
  818. case AArch64::EORWrs:
  819. case AArch64::EONWrs: {
  820. // if sf == '0' and imm6<5> == '1' then ReservedValue()
  821. if (shiftLo >> 5 == 1)
  822. return Fail;
  823. DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
  824. DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
  825. DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
  826. break;
  827. }
  828. case AArch64::ADDXrs:
  829. case AArch64::ADDSXrs:
  830. case AArch64::SUBXrs:
  831. case AArch64::SUBSXrs:
  832. // if shift == '11' then ReservedValue()
  833. if (shiftHi == 0x3)
  834. return Fail;
  835. LLVM_FALLTHROUGH;
  836. case AArch64::ANDXrs:
  837. case AArch64::ANDSXrs:
  838. case AArch64::BICXrs:
  839. case AArch64::BICSXrs:
  840. case AArch64::ORRXrs:
  841. case AArch64::ORNXrs:
  842. case AArch64::EORXrs:
  843. case AArch64::EONXrs:
  844. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  845. DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
  846. DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
  847. break;
  848. }
  849. Inst.addOperand(MCOperand::createImm(shift));
  850. return Success;
  851. }
  852. static DecodeStatus DecodeMoveImmInstruction(MCInst &Inst, uint32_t insn,
  853. uint64_t Addr,
  854. const void *Decoder) {
  855. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  856. unsigned imm = fieldFromInstruction(insn, 5, 16);
  857. unsigned shift = fieldFromInstruction(insn, 21, 2);
  858. shift <<= 4;
  859. switch (Inst.getOpcode()) {
  860. default:
  861. return Fail;
  862. case AArch64::MOVZWi:
  863. case AArch64::MOVNWi:
  864. case AArch64::MOVKWi:
  865. if (shift & (1U << 5))
  866. return Fail;
  867. DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
  868. break;
  869. case AArch64::MOVZXi:
  870. case AArch64::MOVNXi:
  871. case AArch64::MOVKXi:
  872. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  873. break;
  874. }
  875. if (Inst.getOpcode() == AArch64::MOVKWi ||
  876. Inst.getOpcode() == AArch64::MOVKXi)
  877. Inst.addOperand(Inst.getOperand(0));
  878. Inst.addOperand(MCOperand::createImm(imm));
  879. Inst.addOperand(MCOperand::createImm(shift));
  880. return Success;
  881. }
  882. static DecodeStatus DecodeUnsignedLdStInstruction(MCInst &Inst, uint32_t insn,
  883. uint64_t Addr,
  884. const void *Decoder) {
  885. unsigned Rt = fieldFromInstruction(insn, 0, 5);
  886. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  887. unsigned offset = fieldFromInstruction(insn, 10, 12);
  888. const AArch64Disassembler *Dis =
  889. static_cast<const AArch64Disassembler *>(Decoder);
  890. switch (Inst.getOpcode()) {
  891. default:
  892. return Fail;
  893. case AArch64::PRFMui:
  894. // Rt is an immediate in prefetch.
  895. Inst.addOperand(MCOperand::createImm(Rt));
  896. break;
  897. case AArch64::STRBBui:
  898. case AArch64::LDRBBui:
  899. case AArch64::LDRSBWui:
  900. case AArch64::STRHHui:
  901. case AArch64::LDRHHui:
  902. case AArch64::LDRSHWui:
  903. case AArch64::STRWui:
  904. case AArch64::LDRWui:
  905. DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
  906. break;
  907. case AArch64::LDRSBXui:
  908. case AArch64::LDRSHXui:
  909. case AArch64::LDRSWui:
  910. case AArch64::STRXui:
  911. case AArch64::LDRXui:
  912. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  913. break;
  914. case AArch64::LDRQui:
  915. case AArch64::STRQui:
  916. DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
  917. break;
  918. case AArch64::LDRDui:
  919. case AArch64::STRDui:
  920. DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
  921. break;
  922. case AArch64::LDRSui:
  923. case AArch64::STRSui:
  924. DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
  925. break;
  926. case AArch64::LDRHui:
  927. case AArch64::STRHui:
  928. DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
  929. break;
  930. case AArch64::LDRBui:
  931. case AArch64::STRBui:
  932. DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
  933. break;
  934. }
  935. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  936. if (!Dis->tryAddingSymbolicOperand(Inst, offset, Addr, Fail, 0, 4))
  937. Inst.addOperand(MCOperand::createImm(offset));
  938. return Success;
  939. }
  940. static DecodeStatus DecodeSignedLdStInstruction(MCInst &Inst, uint32_t insn,
  941. uint64_t Addr,
  942. const void *Decoder) {
  943. unsigned Rt = fieldFromInstruction(insn, 0, 5);
  944. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  945. int64_t offset = fieldFromInstruction(insn, 12, 9);
  946. // offset is a 9-bit signed immediate, so sign extend it to
  947. // fill the unsigned.
  948. if (offset & (1 << (9 - 1)))
  949. offset |= ~((1LL << 9) - 1);
  950. // First operand is always the writeback to the address register, if needed.
  951. switch (Inst.getOpcode()) {
  952. default:
  953. break;
  954. case AArch64::LDRSBWpre:
  955. case AArch64::LDRSHWpre:
  956. case AArch64::STRBBpre:
  957. case AArch64::LDRBBpre:
  958. case AArch64::STRHHpre:
  959. case AArch64::LDRHHpre:
  960. case AArch64::STRWpre:
  961. case AArch64::LDRWpre:
  962. case AArch64::LDRSBWpost:
  963. case AArch64::LDRSHWpost:
  964. case AArch64::STRBBpost:
  965. case AArch64::LDRBBpost:
  966. case AArch64::STRHHpost:
  967. case AArch64::LDRHHpost:
  968. case AArch64::STRWpost:
  969. case AArch64::LDRWpost:
  970. case AArch64::LDRSBXpre:
  971. case AArch64::LDRSHXpre:
  972. case AArch64::STRXpre:
  973. case AArch64::LDRSWpre:
  974. case AArch64::LDRXpre:
  975. case AArch64::LDRSBXpost:
  976. case AArch64::LDRSHXpost:
  977. case AArch64::STRXpost:
  978. case AArch64::LDRSWpost:
  979. case AArch64::LDRXpost:
  980. case AArch64::LDRQpre:
  981. case AArch64::STRQpre:
  982. case AArch64::LDRQpost:
  983. case AArch64::STRQpost:
  984. case AArch64::LDRDpre:
  985. case AArch64::STRDpre:
  986. case AArch64::LDRDpost:
  987. case AArch64::STRDpost:
  988. case AArch64::LDRSpre:
  989. case AArch64::STRSpre:
  990. case AArch64::LDRSpost:
  991. case AArch64::STRSpost:
  992. case AArch64::LDRHpre:
  993. case AArch64::STRHpre:
  994. case AArch64::LDRHpost:
  995. case AArch64::STRHpost:
  996. case AArch64::LDRBpre:
  997. case AArch64::STRBpre:
  998. case AArch64::LDRBpost:
  999. case AArch64::STRBpost:
  1000. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1001. break;
  1002. }
  1003. switch (Inst.getOpcode()) {
  1004. default:
  1005. return Fail;
  1006. case AArch64::PRFUMi:
  1007. // Rt is an immediate in prefetch.
  1008. Inst.addOperand(MCOperand::createImm(Rt));
  1009. break;
  1010. case AArch64::STURBBi:
  1011. case AArch64::LDURBBi:
  1012. case AArch64::LDURSBWi:
  1013. case AArch64::STURHHi:
  1014. case AArch64::LDURHHi:
  1015. case AArch64::LDURSHWi:
  1016. case AArch64::STURWi:
  1017. case AArch64::LDURWi:
  1018. case AArch64::LDTRSBWi:
  1019. case AArch64::LDTRSHWi:
  1020. case AArch64::STTRWi:
  1021. case AArch64::LDTRWi:
  1022. case AArch64::STTRHi:
  1023. case AArch64::LDTRHi:
  1024. case AArch64::LDTRBi:
  1025. case AArch64::STTRBi:
  1026. case AArch64::LDRSBWpre:
  1027. case AArch64::LDRSHWpre:
  1028. case AArch64::STRBBpre:
  1029. case AArch64::LDRBBpre:
  1030. case AArch64::STRHHpre:
  1031. case AArch64::LDRHHpre:
  1032. case AArch64::STRWpre:
  1033. case AArch64::LDRWpre:
  1034. case AArch64::LDRSBWpost:
  1035. case AArch64::LDRSHWpost:
  1036. case AArch64::STRBBpost:
  1037. case AArch64::LDRBBpost:
  1038. case AArch64::STRHHpost:
  1039. case AArch64::LDRHHpost:
  1040. case AArch64::STRWpost:
  1041. case AArch64::LDRWpost:
  1042. case AArch64::STLURBi:
  1043. case AArch64::STLURHi:
  1044. case AArch64::STLURWi:
  1045. case AArch64::LDAPURBi:
  1046. case AArch64::LDAPURSBWi:
  1047. case AArch64::LDAPURHi:
  1048. case AArch64::LDAPURSHWi:
  1049. case AArch64::LDAPURi:
  1050. DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1051. break;
  1052. case AArch64::LDURSBXi:
  1053. case AArch64::LDURSHXi:
  1054. case AArch64::LDURSWi:
  1055. case AArch64::STURXi:
  1056. case AArch64::LDURXi:
  1057. case AArch64::LDTRSBXi:
  1058. case AArch64::LDTRSHXi:
  1059. case AArch64::LDTRSWi:
  1060. case AArch64::STTRXi:
  1061. case AArch64::LDTRXi:
  1062. case AArch64::LDRSBXpre:
  1063. case AArch64::LDRSHXpre:
  1064. case AArch64::STRXpre:
  1065. case AArch64::LDRSWpre:
  1066. case AArch64::LDRXpre:
  1067. case AArch64::LDRSBXpost:
  1068. case AArch64::LDRSHXpost:
  1069. case AArch64::STRXpost:
  1070. case AArch64::LDRSWpost:
  1071. case AArch64::LDRXpost:
  1072. case AArch64::LDAPURSWi:
  1073. case AArch64::LDAPURSHXi:
  1074. case AArch64::LDAPURSBXi:
  1075. case AArch64::STLURXi:
  1076. case AArch64::LDAPURXi:
  1077. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1078. break;
  1079. case AArch64::LDURQi:
  1080. case AArch64::STURQi:
  1081. case AArch64::LDRQpre:
  1082. case AArch64::STRQpre:
  1083. case AArch64::LDRQpost:
  1084. case AArch64::STRQpost:
  1085. DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
  1086. break;
  1087. case AArch64::LDURDi:
  1088. case AArch64::STURDi:
  1089. case AArch64::LDRDpre:
  1090. case AArch64::STRDpre:
  1091. case AArch64::LDRDpost:
  1092. case AArch64::STRDpost:
  1093. DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1094. break;
  1095. case AArch64::LDURSi:
  1096. case AArch64::STURSi:
  1097. case AArch64::LDRSpre:
  1098. case AArch64::STRSpre:
  1099. case AArch64::LDRSpost:
  1100. case AArch64::STRSpost:
  1101. DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1102. break;
  1103. case AArch64::LDURHi:
  1104. case AArch64::STURHi:
  1105. case AArch64::LDRHpre:
  1106. case AArch64::STRHpre:
  1107. case AArch64::LDRHpost:
  1108. case AArch64::STRHpost:
  1109. DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
  1110. break;
  1111. case AArch64::LDURBi:
  1112. case AArch64::STURBi:
  1113. case AArch64::LDRBpre:
  1114. case AArch64::STRBpre:
  1115. case AArch64::LDRBpost:
  1116. case AArch64::STRBpost:
  1117. DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
  1118. break;
  1119. }
  1120. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1121. Inst.addOperand(MCOperand::createImm(offset));
  1122. bool IsLoad = fieldFromInstruction(insn, 22, 1);
  1123. bool IsIndexed = fieldFromInstruction(insn, 10, 2) != 0;
  1124. bool IsFP = fieldFromInstruction(insn, 26, 1);
  1125. // Cannot write back to a transfer register (but xzr != sp).
  1126. if (IsLoad && IsIndexed && !IsFP && Rn != 31 && Rt == Rn)
  1127. return SoftFail;
  1128. return Success;
  1129. }
  1130. static DecodeStatus DecodeExclusiveLdStInstruction(MCInst &Inst, uint32_t insn,
  1131. uint64_t Addr,
  1132. const void *Decoder) {
  1133. unsigned Rt = fieldFromInstruction(insn, 0, 5);
  1134. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1135. unsigned Rt2 = fieldFromInstruction(insn, 10, 5);
  1136. unsigned Rs = fieldFromInstruction(insn, 16, 5);
  1137. unsigned Opcode = Inst.getOpcode();
  1138. switch (Opcode) {
  1139. default:
  1140. return Fail;
  1141. case AArch64::STLXRW:
  1142. case AArch64::STLXRB:
  1143. case AArch64::STLXRH:
  1144. case AArch64::STXRW:
  1145. case AArch64::STXRB:
  1146. case AArch64::STXRH:
  1147. DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
  1148. LLVM_FALLTHROUGH;
  1149. case AArch64::LDARW:
  1150. case AArch64::LDARB:
  1151. case AArch64::LDARH:
  1152. case AArch64::LDAXRW:
  1153. case AArch64::LDAXRB:
  1154. case AArch64::LDAXRH:
  1155. case AArch64::LDXRW:
  1156. case AArch64::LDXRB:
  1157. case AArch64::LDXRH:
  1158. case AArch64::STLRW:
  1159. case AArch64::STLRB:
  1160. case AArch64::STLRH:
  1161. case AArch64::STLLRW:
  1162. case AArch64::STLLRB:
  1163. case AArch64::STLLRH:
  1164. case AArch64::LDLARW:
  1165. case AArch64::LDLARB:
  1166. case AArch64::LDLARH:
  1167. DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1168. break;
  1169. case AArch64::STLXRX:
  1170. case AArch64::STXRX:
  1171. DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
  1172. LLVM_FALLTHROUGH;
  1173. case AArch64::LDARX:
  1174. case AArch64::LDAXRX:
  1175. case AArch64::LDXRX:
  1176. case AArch64::STLRX:
  1177. case AArch64::LDLARX:
  1178. case AArch64::STLLRX:
  1179. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1180. break;
  1181. case AArch64::STLXPW:
  1182. case AArch64::STXPW:
  1183. DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
  1184. LLVM_FALLTHROUGH;
  1185. case AArch64::LDAXPW:
  1186. case AArch64::LDXPW:
  1187. DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1188. DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
  1189. break;
  1190. case AArch64::STLXPX:
  1191. case AArch64::STXPX:
  1192. DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
  1193. LLVM_FALLTHROUGH;
  1194. case AArch64::LDAXPX:
  1195. case AArch64::LDXPX:
  1196. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1197. DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
  1198. break;
  1199. }
  1200. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1201. // You shouldn't load to the same register twice in an instruction...
  1202. if ((Opcode == AArch64::LDAXPW || Opcode == AArch64::LDXPW ||
  1203. Opcode == AArch64::LDAXPX || Opcode == AArch64::LDXPX) &&
  1204. Rt == Rt2)
  1205. return SoftFail;
  1206. return Success;
  1207. }
  1208. static DecodeStatus DecodePairLdStInstruction(MCInst &Inst, uint32_t insn,
  1209. uint64_t Addr,
  1210. const void *Decoder) {
  1211. unsigned Rt = fieldFromInstruction(insn, 0, 5);
  1212. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1213. unsigned Rt2 = fieldFromInstruction(insn, 10, 5);
  1214. int64_t offset = fieldFromInstruction(insn, 15, 7);
  1215. bool IsLoad = fieldFromInstruction(insn, 22, 1);
  1216. // offset is a 7-bit signed immediate, so sign extend it to
  1217. // fill the unsigned.
  1218. if (offset & (1 << (7 - 1)))
  1219. offset |= ~((1LL << 7) - 1);
  1220. unsigned Opcode = Inst.getOpcode();
  1221. bool NeedsDisjointWritebackTransfer = false;
  1222. // First operand is always writeback of base register.
  1223. switch (Opcode) {
  1224. default:
  1225. break;
  1226. case AArch64::LDPXpost:
  1227. case AArch64::STPXpost:
  1228. case AArch64::LDPSWpost:
  1229. case AArch64::LDPXpre:
  1230. case AArch64::STPXpre:
  1231. case AArch64::LDPSWpre:
  1232. case AArch64::LDPWpost:
  1233. case AArch64::STPWpost:
  1234. case AArch64::LDPWpre:
  1235. case AArch64::STPWpre:
  1236. case AArch64::LDPQpost:
  1237. case AArch64::STPQpost:
  1238. case AArch64::LDPQpre:
  1239. case AArch64::STPQpre:
  1240. case AArch64::LDPDpost:
  1241. case AArch64::STPDpost:
  1242. case AArch64::LDPDpre:
  1243. case AArch64::STPDpre:
  1244. case AArch64::LDPSpost:
  1245. case AArch64::STPSpost:
  1246. case AArch64::LDPSpre:
  1247. case AArch64::STPSpre:
  1248. case AArch64::STGPpre:
  1249. case AArch64::STGPpost:
  1250. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1251. break;
  1252. }
  1253. switch (Opcode) {
  1254. default:
  1255. return Fail;
  1256. case AArch64::LDPXpost:
  1257. case AArch64::STPXpost:
  1258. case AArch64::LDPSWpost:
  1259. case AArch64::LDPXpre:
  1260. case AArch64::STPXpre:
  1261. case AArch64::LDPSWpre:
  1262. case AArch64::STGPpre:
  1263. case AArch64::STGPpost:
  1264. NeedsDisjointWritebackTransfer = true;
  1265. LLVM_FALLTHROUGH;
  1266. case AArch64::LDNPXi:
  1267. case AArch64::STNPXi:
  1268. case AArch64::LDPXi:
  1269. case AArch64::STPXi:
  1270. case AArch64::LDPSWi:
  1271. case AArch64::STGPi:
  1272. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1273. DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
  1274. break;
  1275. case AArch64::LDPWpost:
  1276. case AArch64::STPWpost:
  1277. case AArch64::LDPWpre:
  1278. case AArch64::STPWpre:
  1279. NeedsDisjointWritebackTransfer = true;
  1280. LLVM_FALLTHROUGH;
  1281. case AArch64::LDNPWi:
  1282. case AArch64::STNPWi:
  1283. case AArch64::LDPWi:
  1284. case AArch64::STPWi:
  1285. DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1286. DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
  1287. break;
  1288. case AArch64::LDNPQi:
  1289. case AArch64::STNPQi:
  1290. case AArch64::LDPQpost:
  1291. case AArch64::STPQpost:
  1292. case AArch64::LDPQi:
  1293. case AArch64::STPQi:
  1294. case AArch64::LDPQpre:
  1295. case AArch64::STPQpre:
  1296. DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
  1297. DecodeFPR128RegisterClass(Inst, Rt2, Addr, Decoder);
  1298. break;
  1299. case AArch64::LDNPDi:
  1300. case AArch64::STNPDi:
  1301. case AArch64::LDPDpost:
  1302. case AArch64::STPDpost:
  1303. case AArch64::LDPDi:
  1304. case AArch64::STPDi:
  1305. case AArch64::LDPDpre:
  1306. case AArch64::STPDpre:
  1307. DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1308. DecodeFPR64RegisterClass(Inst, Rt2, Addr, Decoder);
  1309. break;
  1310. case AArch64::LDNPSi:
  1311. case AArch64::STNPSi:
  1312. case AArch64::LDPSpost:
  1313. case AArch64::STPSpost:
  1314. case AArch64::LDPSi:
  1315. case AArch64::STPSi:
  1316. case AArch64::LDPSpre:
  1317. case AArch64::STPSpre:
  1318. DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1319. DecodeFPR32RegisterClass(Inst, Rt2, Addr, Decoder);
  1320. break;
  1321. }
  1322. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1323. Inst.addOperand(MCOperand::createImm(offset));
  1324. // You shouldn't load to the same register twice in an instruction...
  1325. if (IsLoad && Rt == Rt2)
  1326. return SoftFail;
  1327. // ... or do any operation that writes-back to a transfer register. But note
  1328. // that "stp xzr, xzr, [sp], #4" is fine because xzr and sp are different.
  1329. if (NeedsDisjointWritebackTransfer && Rn != 31 && (Rt == Rn || Rt2 == Rn))
  1330. return SoftFail;
  1331. return Success;
  1332. }
  1333. static DecodeStatus DecodeAuthLoadInstruction(MCInst &Inst, uint32_t insn,
  1334. uint64_t Addr,
  1335. const void *Decoder) {
  1336. unsigned Rt = fieldFromInstruction(insn, 0, 5);
  1337. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1338. uint64_t offset = fieldFromInstruction(insn, 22, 1) << 9 |
  1339. fieldFromInstruction(insn, 12, 9);
  1340. unsigned writeback = fieldFromInstruction(insn, 11, 1);
  1341. switch (Inst.getOpcode()) {
  1342. default:
  1343. return Fail;
  1344. case AArch64::LDRAAwriteback:
  1345. case AArch64::LDRABwriteback:
  1346. DecodeGPR64spRegisterClass(Inst, Rn /* writeback register */, Addr,
  1347. Decoder);
  1348. break;
  1349. case AArch64::LDRAAindexed:
  1350. case AArch64::LDRABindexed:
  1351. break;
  1352. }
  1353. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1354. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1355. DecodeSImm<10>(Inst, offset, Addr, Decoder);
  1356. if (writeback && Rt == Rn && Rn != 31) {
  1357. return SoftFail;
  1358. }
  1359. return Success;
  1360. }
  1361. static DecodeStatus DecodeAddSubERegInstruction(MCInst &Inst, uint32_t insn,
  1362. uint64_t Addr,
  1363. const void *Decoder) {
  1364. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1365. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1366. unsigned Rm = fieldFromInstruction(insn, 16, 5);
  1367. unsigned extend = fieldFromInstruction(insn, 10, 6);
  1368. unsigned shift = extend & 0x7;
  1369. if (shift > 4)
  1370. return Fail;
  1371. switch (Inst.getOpcode()) {
  1372. default:
  1373. return Fail;
  1374. case AArch64::ADDWrx:
  1375. case AArch64::SUBWrx:
  1376. DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
  1377. DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
  1378. DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
  1379. break;
  1380. case AArch64::ADDSWrx:
  1381. case AArch64::SUBSWrx:
  1382. DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
  1383. DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
  1384. DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
  1385. break;
  1386. case AArch64::ADDXrx:
  1387. case AArch64::SUBXrx:
  1388. DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
  1389. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1390. DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
  1391. break;
  1392. case AArch64::ADDSXrx:
  1393. case AArch64::SUBSXrx:
  1394. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  1395. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1396. DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
  1397. break;
  1398. case AArch64::ADDXrx64:
  1399. case AArch64::SUBXrx64:
  1400. DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
  1401. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1402. DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
  1403. break;
  1404. case AArch64::SUBSXrx64:
  1405. case AArch64::ADDSXrx64:
  1406. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  1407. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1408. DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
  1409. break;
  1410. }
  1411. Inst.addOperand(MCOperand::createImm(extend));
  1412. return Success;
  1413. }
  1414. static DecodeStatus DecodeLogicalImmInstruction(MCInst &Inst, uint32_t insn,
  1415. uint64_t Addr,
  1416. const void *Decoder) {
  1417. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1418. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1419. unsigned Datasize = fieldFromInstruction(insn, 31, 1);
  1420. unsigned imm;
  1421. if (Datasize) {
  1422. if (Inst.getOpcode() == AArch64::ANDSXri)
  1423. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  1424. else
  1425. DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
  1426. DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
  1427. imm = fieldFromInstruction(insn, 10, 13);
  1428. if (!AArch64_AM::isValidDecodeLogicalImmediate(imm, 64))
  1429. return Fail;
  1430. } else {
  1431. if (Inst.getOpcode() == AArch64::ANDSWri)
  1432. DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
  1433. else
  1434. DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
  1435. DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
  1436. imm = fieldFromInstruction(insn, 10, 12);
  1437. if (!AArch64_AM::isValidDecodeLogicalImmediate(imm, 32))
  1438. return Fail;
  1439. }
  1440. Inst.addOperand(MCOperand::createImm(imm));
  1441. return Success;
  1442. }
  1443. static DecodeStatus DecodeModImmInstruction(MCInst &Inst, uint32_t insn,
  1444. uint64_t Addr,
  1445. const void *Decoder) {
  1446. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1447. unsigned cmode = fieldFromInstruction(insn, 12, 4);
  1448. unsigned imm = fieldFromInstruction(insn, 16, 3) << 5;
  1449. imm |= fieldFromInstruction(insn, 5, 5);
  1450. if (Inst.getOpcode() == AArch64::MOVID)
  1451. DecodeFPR64RegisterClass(Inst, Rd, Addr, Decoder);
  1452. else
  1453. DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
  1454. Inst.addOperand(MCOperand::createImm(imm));
  1455. switch (Inst.getOpcode()) {
  1456. default:
  1457. break;
  1458. case AArch64::MOVIv4i16:
  1459. case AArch64::MOVIv8i16:
  1460. case AArch64::MVNIv4i16:
  1461. case AArch64::MVNIv8i16:
  1462. case AArch64::MOVIv2i32:
  1463. case AArch64::MOVIv4i32:
  1464. case AArch64::MVNIv2i32:
  1465. case AArch64::MVNIv4i32:
  1466. Inst.addOperand(MCOperand::createImm((cmode & 6) << 2));
  1467. break;
  1468. case AArch64::MOVIv2s_msl:
  1469. case AArch64::MOVIv4s_msl:
  1470. case AArch64::MVNIv2s_msl:
  1471. case AArch64::MVNIv4s_msl:
  1472. Inst.addOperand(MCOperand::createImm((cmode & 1) ? 0x110 : 0x108));
  1473. break;
  1474. }
  1475. return Success;
  1476. }
  1477. static DecodeStatus DecodeModImmTiedInstruction(MCInst &Inst, uint32_t insn,
  1478. uint64_t Addr,
  1479. const void *Decoder) {
  1480. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1481. unsigned cmode = fieldFromInstruction(insn, 12, 4);
  1482. unsigned imm = fieldFromInstruction(insn, 16, 3) << 5;
  1483. imm |= fieldFromInstruction(insn, 5, 5);
  1484. // Tied operands added twice.
  1485. DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
  1486. DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
  1487. Inst.addOperand(MCOperand::createImm(imm));
  1488. Inst.addOperand(MCOperand::createImm((cmode & 6) << 2));
  1489. return Success;
  1490. }
  1491. static DecodeStatus DecodeAdrInstruction(MCInst &Inst, uint32_t insn,
  1492. uint64_t Addr, const void *Decoder) {
  1493. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1494. int64_t imm = fieldFromInstruction(insn, 5, 19) << 2;
  1495. imm |= fieldFromInstruction(insn, 29, 2);
  1496. const AArch64Disassembler *Dis =
  1497. static_cast<const AArch64Disassembler *>(Decoder);
  1498. // Sign-extend the 21-bit immediate.
  1499. if (imm & (1 << (21 - 1)))
  1500. imm |= ~((1LL << 21) - 1);
  1501. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  1502. if (!Dis->tryAddingSymbolicOperand(Inst, imm, Addr, Fail, 0, 4))
  1503. Inst.addOperand(MCOperand::createImm(imm));
  1504. return Success;
  1505. }
  1506. static DecodeStatus DecodeAddSubImmShift(MCInst &Inst, uint32_t insn,
  1507. uint64_t Addr, const void *Decoder) {
  1508. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1509. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1510. unsigned Imm = fieldFromInstruction(insn, 10, 14);
  1511. unsigned S = fieldFromInstruction(insn, 29, 1);
  1512. unsigned Datasize = fieldFromInstruction(insn, 31, 1);
  1513. unsigned ShifterVal = (Imm >> 12) & 3;
  1514. unsigned ImmVal = Imm & 0xFFF;
  1515. const AArch64Disassembler *Dis =
  1516. static_cast<const AArch64Disassembler *>(Decoder);
  1517. if (ShifterVal != 0 && ShifterVal != 1)
  1518. return Fail;
  1519. if (Datasize) {
  1520. if (Rd == 31 && !S)
  1521. DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
  1522. else
  1523. DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
  1524. DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
  1525. } else {
  1526. if (Rd == 31 && !S)
  1527. DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
  1528. else
  1529. DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
  1530. DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
  1531. }
  1532. if (!Dis->tryAddingSymbolicOperand(Inst, Imm, Addr, Fail, 0, 4))
  1533. Inst.addOperand(MCOperand::createImm(ImmVal));
  1534. Inst.addOperand(MCOperand::createImm(12 * ShifterVal));
  1535. return Success;
  1536. }
  1537. static DecodeStatus DecodeUnconditionalBranch(MCInst &Inst, uint32_t insn,
  1538. uint64_t Addr,
  1539. const void *Decoder) {
  1540. int64_t imm = fieldFromInstruction(insn, 0, 26);
  1541. const AArch64Disassembler *Dis =
  1542. static_cast<const AArch64Disassembler *>(Decoder);
  1543. // Sign-extend the 26-bit immediate.
  1544. if (imm & (1 << (26 - 1)))
  1545. imm |= ~((1LL << 26) - 1);
  1546. if (!Dis->tryAddingSymbolicOperand(Inst, imm * 4, Addr, true, 0, 4))
  1547. Inst.addOperand(MCOperand::createImm(imm));
  1548. return Success;
  1549. }
  1550. static DecodeStatus DecodeSystemPStateInstruction(MCInst &Inst, uint32_t insn,
  1551. uint64_t Addr,
  1552. const void *Decoder) {
  1553. uint64_t op1 = fieldFromInstruction(insn, 16, 3);
  1554. uint64_t op2 = fieldFromInstruction(insn, 5, 3);
  1555. uint64_t crm = fieldFromInstruction(insn, 8, 4);
  1556. uint64_t pstate_field = (op1 << 3) | op2;
  1557. switch (pstate_field) {
  1558. case 0x01: // XAFlag
  1559. case 0x02: // AXFlag
  1560. return Fail;
  1561. }
  1562. if ((pstate_field == AArch64PState::PAN ||
  1563. pstate_field == AArch64PState::UAO ||
  1564. pstate_field == AArch64PState::SSBS) && crm > 1)
  1565. return Fail;
  1566. Inst.addOperand(MCOperand::createImm(pstate_field));
  1567. Inst.addOperand(MCOperand::createImm(crm));
  1568. const AArch64Disassembler *Dis =
  1569. static_cast<const AArch64Disassembler *>(Decoder);
  1570. auto PState = AArch64PState::lookupPStateByEncoding(pstate_field);
  1571. if (PState && PState->haveFeatures(Dis->getSubtargetInfo().getFeatureBits()))
  1572. return Success;
  1573. return Fail;
  1574. }
  1575. static DecodeStatus DecodeTestAndBranch(MCInst &Inst, uint32_t insn,
  1576. uint64_t Addr, const void *Decoder) {
  1577. uint64_t Rt = fieldFromInstruction(insn, 0, 5);
  1578. uint64_t bit = fieldFromInstruction(insn, 31, 1) << 5;
  1579. bit |= fieldFromInstruction(insn, 19, 5);
  1580. int64_t dst = fieldFromInstruction(insn, 5, 14);
  1581. const AArch64Disassembler *Dis =
  1582. static_cast<const AArch64Disassembler *>(Decoder);
  1583. // Sign-extend 14-bit immediate.
  1584. if (dst & (1 << (14 - 1)))
  1585. dst |= ~((1LL << 14) - 1);
  1586. if (fieldFromInstruction(insn, 31, 1) == 0)
  1587. DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
  1588. else
  1589. DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
  1590. Inst.addOperand(MCOperand::createImm(bit));
  1591. if (!Dis->tryAddingSymbolicOperand(Inst, dst * 4, Addr, true, 0, 4))
  1592. Inst.addOperand(MCOperand::createImm(dst));
  1593. return Success;
  1594. }
  1595. static DecodeStatus DecodeGPRSeqPairsClassRegisterClass(MCInst &Inst,
  1596. unsigned RegClassID,
  1597. unsigned RegNo,
  1598. uint64_t Addr,
  1599. const void *Decoder) {
  1600. // Register number must be even (see CASP instruction)
  1601. if (RegNo & 0x1)
  1602. return Fail;
  1603. unsigned Reg = AArch64MCRegisterClasses[RegClassID].getRegister(RegNo / 2);
  1604. Inst.addOperand(MCOperand::createReg(Reg));
  1605. return Success;
  1606. }
  1607. static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst &Inst,
  1608. unsigned RegNo,
  1609. uint64_t Addr,
  1610. const void *Decoder) {
  1611. return DecodeGPRSeqPairsClassRegisterClass(Inst,
  1612. AArch64::WSeqPairsClassRegClassID,
  1613. RegNo, Addr, Decoder);
  1614. }
  1615. static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst &Inst,
  1616. unsigned RegNo,
  1617. uint64_t Addr,
  1618. const void *Decoder) {
  1619. return DecodeGPRSeqPairsClassRegisterClass(Inst,
  1620. AArch64::XSeqPairsClassRegClassID,
  1621. RegNo, Addr, Decoder);
  1622. }
  1623. static DecodeStatus DecodeSVELogicalImmInstruction(MCInst &Inst, uint32_t insn,
  1624. uint64_t Addr,
  1625. const void *Decoder) {
  1626. unsigned Zdn = fieldFromInstruction(insn, 0, 5);
  1627. unsigned imm = fieldFromInstruction(insn, 5, 13);
  1628. if (!AArch64_AM::isValidDecodeLogicalImmediate(imm, 64))
  1629. return Fail;
  1630. // The same (tied) operand is added twice to the instruction.
  1631. DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
  1632. if (Inst.getOpcode() != AArch64::DUPM_ZI)
  1633. DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
  1634. Inst.addOperand(MCOperand::createImm(imm));
  1635. return Success;
  1636. }
  1637. template <int Bits>
  1638. static DecodeStatus DecodeSImm(MCInst &Inst, uint64_t Imm, uint64_t Address,
  1639. const void *Decoder) {
  1640. if (Imm & ~((1LL << Bits) - 1))
  1641. return Fail;
  1642. // Imm is a signed immediate, so sign extend it.
  1643. if (Imm & (1 << (Bits - 1)))
  1644. Imm |= ~((1LL << Bits) - 1);
  1645. Inst.addOperand(MCOperand::createImm(Imm));
  1646. return Success;
  1647. }
  1648. // Decode 8-bit signed/unsigned immediate for a given element width.
  1649. template <int ElementWidth>
  1650. static DecodeStatus DecodeImm8OptLsl(MCInst &Inst, unsigned Imm,
  1651. uint64_t Addr, const void *Decoder) {
  1652. unsigned Val = (uint8_t)Imm;
  1653. unsigned Shift = (Imm & 0x100) ? 8 : 0;
  1654. if (ElementWidth == 8 && Shift)
  1655. return Fail;
  1656. Inst.addOperand(MCOperand::createImm(Val));
  1657. Inst.addOperand(MCOperand::createImm(Shift));
  1658. return Success;
  1659. }
  1660. // Decode uimm4 ranged from 1-16.
  1661. static DecodeStatus DecodeSVEIncDecImm(MCInst &Inst, unsigned Imm,
  1662. uint64_t Addr, const void *Decoder) {
  1663. Inst.addOperand(MCOperand::createImm(Imm + 1));
  1664. return Success;
  1665. }
  1666. static DecodeStatus DecodeSVCROp(MCInst &Inst, unsigned Imm, uint64_t Address,
  1667. const void *Decoder) {
  1668. if (AArch64SVCR::lookupSVCRByEncoding(Imm)) {
  1669. Inst.addOperand(MCOperand::createImm(Imm));
  1670. return Success;
  1671. }
  1672. return Fail;
  1673. }
  1674. static DecodeStatus DecodeCPYMemOpInstruction(MCInst &Inst, uint32_t insn,
  1675. uint64_t Addr,
  1676. const void *Decoder) {
  1677. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1678. unsigned Rs = fieldFromInstruction(insn, 16, 5);
  1679. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1680. // None of the registers may alias: if they do, then the instruction is not
  1681. // merely unpredictable but actually entirely unallocated.
  1682. if (Rd == Rs || Rs == Rn || Rd == Rn)
  1683. return MCDisassembler::Fail;
  1684. // All three register operands are written back, so they all appear
  1685. // twice in the operand list, once as outputs and once as inputs.
  1686. if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
  1687. !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
  1688. !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
  1689. !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
  1690. !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
  1691. !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder))
  1692. return MCDisassembler::Fail;
  1693. return MCDisassembler::Success;
  1694. }
  1695. static DecodeStatus DecodeSETMemOpInstruction(MCInst &Inst, uint32_t insn,
  1696. uint64_t Addr,
  1697. const void *Decoder) {
  1698. unsigned Rd = fieldFromInstruction(insn, 0, 5);
  1699. unsigned Rm = fieldFromInstruction(insn, 16, 5);
  1700. unsigned Rn = fieldFromInstruction(insn, 5, 5);
  1701. // None of the registers may alias: if they do, then the instruction is not
  1702. // merely unpredictable but actually entirely unallocated.
  1703. if (Rd == Rm || Rm == Rn || Rd == Rn)
  1704. return MCDisassembler::Fail;
  1705. // Rd and Rn (not Rm) register operands are written back, so they appear
  1706. // twice in the operand list, once as outputs and once as inputs.
  1707. if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
  1708. !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
  1709. !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
  1710. !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
  1711. !DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder))
  1712. return MCDisassembler::Fail;
  1713. return MCDisassembler::Success;
  1714. }