ARMMCTargetDesc.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. //===-- ARMMCTargetDesc.cpp - ARM Target Descriptions ---------------------===//
  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. // This file provides ARM specific target descriptions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ARMMCTargetDesc.h"
  13. #include "ARMAddressingModes.h"
  14. #include "ARMBaseInfo.h"
  15. #include "ARMInstPrinter.h"
  16. #include "ARMMCAsmInfo.h"
  17. #include "TargetInfo/ARMTargetInfo.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/DebugInfo/CodeView/CodeView.h"
  20. #include "llvm/MC/MCAsmBackend.h"
  21. #include "llvm/MC/MCCodeEmitter.h"
  22. #include "llvm/MC/MCELFStreamer.h"
  23. #include "llvm/MC/MCInstrAnalysis.h"
  24. #include "llvm/MC/MCInstrInfo.h"
  25. #include "llvm/MC/MCObjectWriter.h"
  26. #include "llvm/MC/MCRegisterInfo.h"
  27. #include "llvm/MC/MCStreamer.h"
  28. #include "llvm/MC/MCSubtargetInfo.h"
  29. #include "llvm/MC/TargetRegistry.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/TargetParser.h"
  32. using namespace llvm;
  33. #define GET_REGINFO_MC_DESC
  34. #include "ARMGenRegisterInfo.inc"
  35. static bool getMCRDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
  36. std::string &Info) {
  37. if (STI.getFeatureBits()[llvm::ARM::HasV7Ops] &&
  38. (MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 15) &&
  39. (MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) &&
  40. // Checks for the deprecated CP15ISB encoding:
  41. // mcr p15, #0, rX, c7, c5, #4
  42. (MI.getOperand(3).isImm() && MI.getOperand(3).getImm() == 7)) {
  43. if ((MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 4)) {
  44. if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 5) {
  45. Info = "deprecated since v7, use 'isb'";
  46. return true;
  47. }
  48. // Checks for the deprecated CP15DSB encoding:
  49. // mcr p15, #0, rX, c7, c10, #4
  50. if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10) {
  51. Info = "deprecated since v7, use 'dsb'";
  52. return true;
  53. }
  54. }
  55. // Checks for the deprecated CP15DMB encoding:
  56. // mcr p15, #0, rX, c7, c10, #5
  57. if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10 &&
  58. (MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 5)) {
  59. Info = "deprecated since v7, use 'dmb'";
  60. return true;
  61. }
  62. }
  63. if (STI.getFeatureBits()[llvm::ARM::HasV7Ops] &&
  64. ((MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 10) ||
  65. (MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 11))) {
  66. Info = "since v7, cp10 and cp11 are reserved for advanced SIMD or floating "
  67. "point instructions";
  68. return true;
  69. }
  70. return false;
  71. }
  72. static bool getMRCDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
  73. std::string &Info) {
  74. if (STI.getFeatureBits()[llvm::ARM::HasV7Ops] &&
  75. ((MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 10) ||
  76. (MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 11))) {
  77. Info = "since v7, cp10 and cp11 are reserved for advanced SIMD or floating "
  78. "point instructions";
  79. return true;
  80. }
  81. return false;
  82. }
  83. static bool getITDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
  84. std::string &Info) {
  85. if (STI.getFeatureBits()[llvm::ARM::HasV8Ops] && MI.getOperand(1).isImm() &&
  86. MI.getOperand(1).getImm() != 8) {
  87. Info = "applying IT instruction to more than one subsequent instruction is "
  88. "deprecated";
  89. return true;
  90. }
  91. return false;
  92. }
  93. static bool getARMStoreDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
  94. std::string &Info) {
  95. assert(!STI.getFeatureBits()[llvm::ARM::ModeThumb] &&
  96. "cannot predicate thumb instructions");
  97. assert(MI.getNumOperands() >= 4 && "expected >= 4 arguments");
  98. for (unsigned OI = 4, OE = MI.getNumOperands(); OI < OE; ++OI) {
  99. assert(MI.getOperand(OI).isReg() && "expected register");
  100. if (MI.getOperand(OI).getReg() == ARM::PC) {
  101. Info = "use of PC in the list is deprecated";
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. static bool getARMLoadDeprecationInfo(MCInst &MI, const MCSubtargetInfo &STI,
  108. std::string &Info) {
  109. assert(!STI.getFeatureBits()[llvm::ARM::ModeThumb] &&
  110. "cannot predicate thumb instructions");
  111. assert(MI.getNumOperands() >= 4 && "expected >= 4 arguments");
  112. bool ListContainsPC = false, ListContainsLR = false;
  113. for (unsigned OI = 4, OE = MI.getNumOperands(); OI < OE; ++OI) {
  114. assert(MI.getOperand(OI).isReg() && "expected register");
  115. switch (MI.getOperand(OI).getReg()) {
  116. default:
  117. break;
  118. case ARM::LR:
  119. ListContainsLR = true;
  120. break;
  121. case ARM::PC:
  122. ListContainsPC = true;
  123. break;
  124. }
  125. }
  126. if (ListContainsPC && ListContainsLR) {
  127. Info = "use of LR and PC simultaneously in the list is deprecated";
  128. return true;
  129. }
  130. return false;
  131. }
  132. #define GET_INSTRINFO_MC_DESC
  133. #include "ARMGenInstrInfo.inc"
  134. #define GET_SUBTARGETINFO_MC_DESC
  135. #include "ARMGenSubtargetInfo.inc"
  136. std::string ARM_MC::ParseARMTriple(const Triple &TT, StringRef CPU) {
  137. std::string ARMArchFeature;
  138. ARM::ArchKind ArchID = ARM::parseArch(TT.getArchName());
  139. if (ArchID != ARM::ArchKind::INVALID && (CPU.empty() || CPU == "generic"))
  140. ARMArchFeature = (ARMArchFeature + "+" + ARM::getArchName(ArchID)).str();
  141. if (TT.isThumb()) {
  142. if (!ARMArchFeature.empty())
  143. ARMArchFeature += ",";
  144. ARMArchFeature += "+thumb-mode,+v4t";
  145. }
  146. if (TT.isOSNaCl()) {
  147. if (!ARMArchFeature.empty())
  148. ARMArchFeature += ",";
  149. ARMArchFeature += "+nacl-trap";
  150. }
  151. if (TT.isOSWindows()) {
  152. if (!ARMArchFeature.empty())
  153. ARMArchFeature += ",";
  154. ARMArchFeature += "+noarm";
  155. }
  156. return ARMArchFeature;
  157. }
  158. bool ARM_MC::isPredicated(const MCInst &MI, const MCInstrInfo *MCII) {
  159. const MCInstrDesc &Desc = MCII->get(MI.getOpcode());
  160. int PredOpIdx = Desc.findFirstPredOperandIdx();
  161. return PredOpIdx != -1 && MI.getOperand(PredOpIdx).getImm() != ARMCC::AL;
  162. }
  163. bool ARM_MC::isCPSRDefined(const MCInst &MI, const MCInstrInfo *MCII) {
  164. const MCInstrDesc &Desc = MCII->get(MI.getOpcode());
  165. for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
  166. const MCOperand &MO = MI.getOperand(I);
  167. if (MO.isReg() && MO.getReg() == ARM::CPSR &&
  168. Desc.OpInfo[I].isOptionalDef())
  169. return true;
  170. }
  171. return false;
  172. }
  173. uint64_t ARM_MC::evaluateBranchTarget(const MCInstrDesc &InstDesc,
  174. uint64_t Addr, int64_t Imm) {
  175. // For ARM instructions the PC offset is 8 bytes, for Thumb instructions it
  176. // is 4 bytes.
  177. uint64_t Offset =
  178. ((InstDesc.TSFlags & ARMII::FormMask) == ARMII::ThumbFrm) ? 4 : 8;
  179. // A Thumb instruction BLX(i) can be 16-bit aligned while targets Arm code
  180. // which is 32-bit aligned. The target address for the case is calculated as
  181. // targetAddress = Align(PC,4) + imm32;
  182. // where
  183. // Align(x, y) = y * (x DIV y);
  184. if (InstDesc.getOpcode() == ARM::tBLXi)
  185. Addr &= ~0x3;
  186. return Addr + Imm + Offset;
  187. }
  188. MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(const Triple &TT,
  189. StringRef CPU, StringRef FS) {
  190. std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
  191. if (!FS.empty()) {
  192. if (!ArchFS.empty())
  193. ArchFS = (Twine(ArchFS) + "," + FS).str();
  194. else
  195. ArchFS = std::string(FS);
  196. }
  197. return createARMMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, ArchFS);
  198. }
  199. static MCInstrInfo *createARMMCInstrInfo() {
  200. MCInstrInfo *X = new MCInstrInfo();
  201. InitARMMCInstrInfo(X);
  202. return X;
  203. }
  204. void ARM_MC::initLLVMToCVRegMapping(MCRegisterInfo *MRI) {
  205. // Mapping from CodeView to MC register id.
  206. static const struct {
  207. codeview::RegisterId CVReg;
  208. MCPhysReg Reg;
  209. } RegMap[] = {
  210. {codeview::RegisterId::ARM_R0, ARM::R0},
  211. {codeview::RegisterId::ARM_R1, ARM::R1},
  212. {codeview::RegisterId::ARM_R2, ARM::R2},
  213. {codeview::RegisterId::ARM_R3, ARM::R3},
  214. {codeview::RegisterId::ARM_R4, ARM::R4},
  215. {codeview::RegisterId::ARM_R5, ARM::R5},
  216. {codeview::RegisterId::ARM_R6, ARM::R6},
  217. {codeview::RegisterId::ARM_R7, ARM::R7},
  218. {codeview::RegisterId::ARM_R8, ARM::R8},
  219. {codeview::RegisterId::ARM_R9, ARM::R9},
  220. {codeview::RegisterId::ARM_R10, ARM::R10},
  221. {codeview::RegisterId::ARM_R11, ARM::R11},
  222. {codeview::RegisterId::ARM_R12, ARM::R12},
  223. {codeview::RegisterId::ARM_SP, ARM::SP},
  224. {codeview::RegisterId::ARM_LR, ARM::LR},
  225. {codeview::RegisterId::ARM_PC, ARM::PC},
  226. {codeview::RegisterId::ARM_CPSR, ARM::CPSR},
  227. {codeview::RegisterId::ARM_FPSCR, ARM::FPSCR},
  228. {codeview::RegisterId::ARM_FPEXC, ARM::FPEXC},
  229. {codeview::RegisterId::ARM_FS0, ARM::S0},
  230. {codeview::RegisterId::ARM_FS1, ARM::S1},
  231. {codeview::RegisterId::ARM_FS2, ARM::S2},
  232. {codeview::RegisterId::ARM_FS3, ARM::S3},
  233. {codeview::RegisterId::ARM_FS4, ARM::S4},
  234. {codeview::RegisterId::ARM_FS5, ARM::S5},
  235. {codeview::RegisterId::ARM_FS6, ARM::S6},
  236. {codeview::RegisterId::ARM_FS7, ARM::S7},
  237. {codeview::RegisterId::ARM_FS8, ARM::S8},
  238. {codeview::RegisterId::ARM_FS9, ARM::S9},
  239. {codeview::RegisterId::ARM_FS10, ARM::S10},
  240. {codeview::RegisterId::ARM_FS11, ARM::S11},
  241. {codeview::RegisterId::ARM_FS12, ARM::S12},
  242. {codeview::RegisterId::ARM_FS13, ARM::S13},
  243. {codeview::RegisterId::ARM_FS14, ARM::S14},
  244. {codeview::RegisterId::ARM_FS15, ARM::S15},
  245. {codeview::RegisterId::ARM_FS16, ARM::S16},
  246. {codeview::RegisterId::ARM_FS17, ARM::S17},
  247. {codeview::RegisterId::ARM_FS18, ARM::S18},
  248. {codeview::RegisterId::ARM_FS19, ARM::S19},
  249. {codeview::RegisterId::ARM_FS20, ARM::S20},
  250. {codeview::RegisterId::ARM_FS21, ARM::S21},
  251. {codeview::RegisterId::ARM_FS22, ARM::S22},
  252. {codeview::RegisterId::ARM_FS23, ARM::S23},
  253. {codeview::RegisterId::ARM_FS24, ARM::S24},
  254. {codeview::RegisterId::ARM_FS25, ARM::S25},
  255. {codeview::RegisterId::ARM_FS26, ARM::S26},
  256. {codeview::RegisterId::ARM_FS27, ARM::S27},
  257. {codeview::RegisterId::ARM_FS28, ARM::S28},
  258. {codeview::RegisterId::ARM_FS29, ARM::S29},
  259. {codeview::RegisterId::ARM_FS30, ARM::S30},
  260. {codeview::RegisterId::ARM_FS31, ARM::S31},
  261. {codeview::RegisterId::ARM_ND0, ARM::D0},
  262. {codeview::RegisterId::ARM_ND1, ARM::D1},
  263. {codeview::RegisterId::ARM_ND2, ARM::D2},
  264. {codeview::RegisterId::ARM_ND3, ARM::D3},
  265. {codeview::RegisterId::ARM_ND4, ARM::D4},
  266. {codeview::RegisterId::ARM_ND5, ARM::D5},
  267. {codeview::RegisterId::ARM_ND6, ARM::D6},
  268. {codeview::RegisterId::ARM_ND7, ARM::D7},
  269. {codeview::RegisterId::ARM_ND8, ARM::D8},
  270. {codeview::RegisterId::ARM_ND9, ARM::D9},
  271. {codeview::RegisterId::ARM_ND10, ARM::D10},
  272. {codeview::RegisterId::ARM_ND11, ARM::D11},
  273. {codeview::RegisterId::ARM_ND12, ARM::D12},
  274. {codeview::RegisterId::ARM_ND13, ARM::D13},
  275. {codeview::RegisterId::ARM_ND14, ARM::D14},
  276. {codeview::RegisterId::ARM_ND15, ARM::D15},
  277. {codeview::RegisterId::ARM_ND16, ARM::D16},
  278. {codeview::RegisterId::ARM_ND17, ARM::D17},
  279. {codeview::RegisterId::ARM_ND18, ARM::D18},
  280. {codeview::RegisterId::ARM_ND19, ARM::D19},
  281. {codeview::RegisterId::ARM_ND20, ARM::D20},
  282. {codeview::RegisterId::ARM_ND21, ARM::D21},
  283. {codeview::RegisterId::ARM_ND22, ARM::D22},
  284. {codeview::RegisterId::ARM_ND23, ARM::D23},
  285. {codeview::RegisterId::ARM_ND24, ARM::D24},
  286. {codeview::RegisterId::ARM_ND25, ARM::D25},
  287. {codeview::RegisterId::ARM_ND26, ARM::D26},
  288. {codeview::RegisterId::ARM_ND27, ARM::D27},
  289. {codeview::RegisterId::ARM_ND28, ARM::D28},
  290. {codeview::RegisterId::ARM_ND29, ARM::D29},
  291. {codeview::RegisterId::ARM_ND30, ARM::D30},
  292. {codeview::RegisterId::ARM_ND31, ARM::D31},
  293. {codeview::RegisterId::ARM_NQ0, ARM::Q0},
  294. {codeview::RegisterId::ARM_NQ1, ARM::Q1},
  295. {codeview::RegisterId::ARM_NQ2, ARM::Q2},
  296. {codeview::RegisterId::ARM_NQ3, ARM::Q3},
  297. {codeview::RegisterId::ARM_NQ4, ARM::Q4},
  298. {codeview::RegisterId::ARM_NQ5, ARM::Q5},
  299. {codeview::RegisterId::ARM_NQ6, ARM::Q6},
  300. {codeview::RegisterId::ARM_NQ7, ARM::Q7},
  301. {codeview::RegisterId::ARM_NQ8, ARM::Q8},
  302. {codeview::RegisterId::ARM_NQ9, ARM::Q9},
  303. {codeview::RegisterId::ARM_NQ10, ARM::Q10},
  304. {codeview::RegisterId::ARM_NQ11, ARM::Q11},
  305. {codeview::RegisterId::ARM_NQ12, ARM::Q12},
  306. {codeview::RegisterId::ARM_NQ13, ARM::Q13},
  307. {codeview::RegisterId::ARM_NQ14, ARM::Q14},
  308. {codeview::RegisterId::ARM_NQ15, ARM::Q15},
  309. };
  310. for (const auto &I : RegMap)
  311. MRI->mapLLVMRegToCVReg(I.Reg, static_cast<int>(I.CVReg));
  312. }
  313. static MCRegisterInfo *createARMMCRegisterInfo(const Triple &Triple) {
  314. MCRegisterInfo *X = new MCRegisterInfo();
  315. InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
  316. ARM_MC::initLLVMToCVRegMapping(X);
  317. return X;
  318. }
  319. static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI,
  320. const Triple &TheTriple,
  321. const MCTargetOptions &Options) {
  322. MCAsmInfo *MAI;
  323. if (TheTriple.isOSDarwin() || TheTriple.isOSBinFormatMachO())
  324. MAI = new ARMMCAsmInfoDarwin(TheTriple);
  325. else if (TheTriple.isWindowsMSVCEnvironment())
  326. MAI = new ARMCOFFMCAsmInfoMicrosoft();
  327. else if (TheTriple.isOSWindows())
  328. MAI = new ARMCOFFMCAsmInfoGNU();
  329. else
  330. MAI = new ARMELFMCAsmInfo(TheTriple);
  331. unsigned Reg = MRI.getDwarfRegNum(ARM::SP, true);
  332. MAI->addInitialFrameState(MCCFIInstruction::cfiDefCfa(nullptr, Reg, 0));
  333. return MAI;
  334. }
  335. static MCStreamer *createELFStreamer(const Triple &T, MCContext &Ctx,
  336. std::unique_ptr<MCAsmBackend> &&MAB,
  337. std::unique_ptr<MCObjectWriter> &&OW,
  338. std::unique_ptr<MCCodeEmitter> &&Emitter,
  339. bool RelaxAll) {
  340. return createARMELFStreamer(
  341. Ctx, std::move(MAB), std::move(OW), std::move(Emitter), false,
  342. (T.getArch() == Triple::thumb || T.getArch() == Triple::thumbeb),
  343. T.isAndroid());
  344. }
  345. static MCStreamer *
  346. createARMMachOStreamer(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&MAB,
  347. std::unique_ptr<MCObjectWriter> &&OW,
  348. std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
  349. bool DWARFMustBeAtTheEnd) {
  350. return createMachOStreamer(Ctx, std::move(MAB), std::move(OW),
  351. std::move(Emitter), false, DWARFMustBeAtTheEnd);
  352. }
  353. static MCInstPrinter *createARMMCInstPrinter(const Triple &T,
  354. unsigned SyntaxVariant,
  355. const MCAsmInfo &MAI,
  356. const MCInstrInfo &MII,
  357. const MCRegisterInfo &MRI) {
  358. if (SyntaxVariant == 0)
  359. return new ARMInstPrinter(MAI, MII, MRI);
  360. return nullptr;
  361. }
  362. static MCRelocationInfo *createARMMCRelocationInfo(const Triple &TT,
  363. MCContext &Ctx) {
  364. if (TT.isOSBinFormatMachO())
  365. return createARMMachORelocationInfo(Ctx);
  366. // Default to the stock relocation info.
  367. return llvm::createMCRelocationInfo(TT, Ctx);
  368. }
  369. namespace {
  370. class ARMMCInstrAnalysis : public MCInstrAnalysis {
  371. public:
  372. ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
  373. bool isUnconditionalBranch(const MCInst &Inst) const override {
  374. // BCCs with the "always" predicate are unconditional branches.
  375. if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
  376. return true;
  377. return MCInstrAnalysis::isUnconditionalBranch(Inst);
  378. }
  379. bool isConditionalBranch(const MCInst &Inst) const override {
  380. // BCCs with the "always" predicate are unconditional branches.
  381. if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
  382. return false;
  383. return MCInstrAnalysis::isConditionalBranch(Inst);
  384. }
  385. bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
  386. uint64_t &Target) const override {
  387. const MCInstrDesc &Desc = Info->get(Inst.getOpcode());
  388. // Find the PC-relative immediate operand in the instruction.
  389. for (unsigned OpNum = 0; OpNum < Desc.getNumOperands(); ++OpNum) {
  390. if (Inst.getOperand(OpNum).isImm() &&
  391. Desc.OpInfo[OpNum].OperandType == MCOI::OPERAND_PCREL) {
  392. int64_t Imm = Inst.getOperand(OpNum).getImm();
  393. Target = ARM_MC::evaluateBranchTarget(Desc, Addr, Imm);
  394. return true;
  395. }
  396. }
  397. return false;
  398. }
  399. Optional<uint64_t> evaluateMemoryOperandAddress(const MCInst &Inst,
  400. const MCSubtargetInfo *STI,
  401. uint64_t Addr,
  402. uint64_t Size) const override;
  403. };
  404. } // namespace
  405. static Optional<uint64_t>
  406. // NOLINTNEXTLINE(readability-identifier-naming)
  407. evaluateMemOpAddrForAddrMode_i12(const MCInst &Inst, const MCInstrDesc &Desc,
  408. unsigned MemOpIndex, uint64_t Addr) {
  409. if (MemOpIndex + 1 >= Desc.getNumOperands())
  410. return None;
  411. const MCOperand &MO1 = Inst.getOperand(MemOpIndex);
  412. const MCOperand &MO2 = Inst.getOperand(MemOpIndex + 1);
  413. if (!MO1.isReg() || MO1.getReg() != ARM::PC || !MO2.isImm())
  414. return None;
  415. int32_t OffImm = (int32_t)MO2.getImm();
  416. // Special value for #-0. All others are normal.
  417. if (OffImm == INT32_MIN)
  418. OffImm = 0;
  419. return Addr + OffImm;
  420. }
  421. static Optional<uint64_t> evaluateMemOpAddrForAddrMode3(const MCInst &Inst,
  422. const MCInstrDesc &Desc,
  423. unsigned MemOpIndex,
  424. uint64_t Addr) {
  425. if (MemOpIndex + 2 >= Desc.getNumOperands())
  426. return None;
  427. const MCOperand &MO1 = Inst.getOperand(MemOpIndex);
  428. const MCOperand &MO2 = Inst.getOperand(MemOpIndex + 1);
  429. const MCOperand &MO3 = Inst.getOperand(MemOpIndex + 2);
  430. if (!MO1.isReg() || MO1.getReg() != ARM::PC || MO2.getReg() || !MO3.isImm())
  431. return None;
  432. unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm());
  433. ARM_AM::AddrOpc Op = ARM_AM::getAM3Op(MO3.getImm());
  434. if (Op == ARM_AM::sub)
  435. return Addr - ImmOffs;
  436. return Addr + ImmOffs;
  437. }
  438. static Optional<uint64_t> evaluateMemOpAddrForAddrMode5(const MCInst &Inst,
  439. const MCInstrDesc &Desc,
  440. unsigned MemOpIndex,
  441. uint64_t Addr) {
  442. if (MemOpIndex + 1 >= Desc.getNumOperands())
  443. return None;
  444. const MCOperand &MO1 = Inst.getOperand(MemOpIndex);
  445. const MCOperand &MO2 = Inst.getOperand(MemOpIndex + 1);
  446. if (!MO1.isReg() || MO1.getReg() != ARM::PC || !MO2.isImm())
  447. return None;
  448. unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm());
  449. ARM_AM::AddrOpc Op = ARM_AM::getAM5Op(MO2.getImm());
  450. if (Op == ARM_AM::sub)
  451. return Addr - ImmOffs * 4;
  452. return Addr + ImmOffs * 4;
  453. }
  454. static Optional<uint64_t>
  455. evaluateMemOpAddrForAddrMode5FP16(const MCInst &Inst, const MCInstrDesc &Desc,
  456. unsigned MemOpIndex, uint64_t Addr) {
  457. if (MemOpIndex + 1 >= Desc.getNumOperands())
  458. return None;
  459. const MCOperand &MO1 = Inst.getOperand(MemOpIndex);
  460. const MCOperand &MO2 = Inst.getOperand(MemOpIndex + 1);
  461. if (!MO1.isReg() || MO1.getReg() != ARM::PC || !MO2.isImm())
  462. return None;
  463. unsigned ImmOffs = ARM_AM::getAM5FP16Offset(MO2.getImm());
  464. ARM_AM::AddrOpc Op = ARM_AM::getAM5FP16Op(MO2.getImm());
  465. if (Op == ARM_AM::sub)
  466. return Addr - ImmOffs * 2;
  467. return Addr + ImmOffs * 2;
  468. }
  469. static Optional<uint64_t>
  470. // NOLINTNEXTLINE(readability-identifier-naming)
  471. evaluateMemOpAddrForAddrModeT2_i8s4(const MCInst &Inst, const MCInstrDesc &Desc,
  472. unsigned MemOpIndex, uint64_t Addr) {
  473. if (MemOpIndex + 1 >= Desc.getNumOperands())
  474. return None;
  475. const MCOperand &MO1 = Inst.getOperand(MemOpIndex);
  476. const MCOperand &MO2 = Inst.getOperand(MemOpIndex + 1);
  477. if (!MO1.isReg() || MO1.getReg() != ARM::PC || !MO2.isImm())
  478. return None;
  479. int32_t OffImm = (int32_t)MO2.getImm();
  480. assert(((OffImm & 0x3) == 0) && "Not a valid immediate!");
  481. // Special value for #-0. All others are normal.
  482. if (OffImm == INT32_MIN)
  483. OffImm = 0;
  484. return Addr + OffImm;
  485. }
  486. static Optional<uint64_t>
  487. // NOLINTNEXTLINE(readability-identifier-naming)
  488. evaluateMemOpAddrForAddrModeT2_pc(const MCInst &Inst, const MCInstrDesc &Desc,
  489. unsigned MemOpIndex, uint64_t Addr) {
  490. const MCOperand &MO1 = Inst.getOperand(MemOpIndex);
  491. if (!MO1.isImm())
  492. return None;
  493. int32_t OffImm = (int32_t)MO1.getImm();
  494. // Special value for #-0. All others are normal.
  495. if (OffImm == INT32_MIN)
  496. OffImm = 0;
  497. return Addr + OffImm;
  498. }
  499. static Optional<uint64_t>
  500. // NOLINTNEXTLINE(readability-identifier-naming)
  501. evaluateMemOpAddrForAddrModeT1_s(const MCInst &Inst, const MCInstrDesc &Desc,
  502. unsigned MemOpIndex, uint64_t Addr) {
  503. return evaluateMemOpAddrForAddrModeT2_pc(Inst, Desc, MemOpIndex, Addr);
  504. }
  505. Optional<uint64_t> ARMMCInstrAnalysis::evaluateMemoryOperandAddress(
  506. const MCInst &Inst, const MCSubtargetInfo *STI, uint64_t Addr,
  507. uint64_t Size) const {
  508. const MCInstrDesc &Desc = Info->get(Inst.getOpcode());
  509. // Only load instructions can have PC-relative memory addressing.
  510. if (!Desc.mayLoad())
  511. return None;
  512. // PC-relative addressing does not update the base register.
  513. uint64_t TSFlags = Desc.TSFlags;
  514. unsigned IndexMode =
  515. (TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift;
  516. if (IndexMode != ARMII::IndexModeNone)
  517. return None;
  518. // Find the memory addressing operand in the instruction.
  519. unsigned OpIndex = Desc.NumDefs;
  520. while (OpIndex < Desc.getNumOperands() &&
  521. Desc.OpInfo[OpIndex].OperandType != MCOI::OPERAND_MEMORY)
  522. ++OpIndex;
  523. if (OpIndex == Desc.getNumOperands())
  524. return None;
  525. // Base address for PC-relative addressing is always 32-bit aligned.
  526. Addr &= ~0x3;
  527. // For ARM instructions the PC offset is 8 bytes, for Thumb instructions it
  528. // is 4 bytes.
  529. switch (Desc.TSFlags & ARMII::FormMask) {
  530. default:
  531. Addr += 8;
  532. break;
  533. case ARMII::ThumbFrm:
  534. Addr += 4;
  535. break;
  536. // VLDR* instructions share the same opcode (and thus the same form) for Arm
  537. // and Thumb. Use a bit longer route through STI in that case.
  538. case ARMII::VFPLdStFrm:
  539. Addr += STI->getFeatureBits()[ARM::ModeThumb] ? 4 : 8;
  540. break;
  541. }
  542. // Eveluate the address depending on the addressing mode
  543. unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
  544. switch (AddrMode) {
  545. default:
  546. return None;
  547. case ARMII::AddrMode_i12:
  548. return evaluateMemOpAddrForAddrMode_i12(Inst, Desc, OpIndex, Addr);
  549. case ARMII::AddrMode3:
  550. return evaluateMemOpAddrForAddrMode3(Inst, Desc, OpIndex, Addr);
  551. case ARMII::AddrMode5:
  552. return evaluateMemOpAddrForAddrMode5(Inst, Desc, OpIndex, Addr);
  553. case ARMII::AddrMode5FP16:
  554. return evaluateMemOpAddrForAddrMode5FP16(Inst, Desc, OpIndex, Addr);
  555. case ARMII::AddrModeT2_i8s4:
  556. return evaluateMemOpAddrForAddrModeT2_i8s4(Inst, Desc, OpIndex, Addr);
  557. case ARMII::AddrModeT2_pc:
  558. return evaluateMemOpAddrForAddrModeT2_pc(Inst, Desc, OpIndex, Addr);
  559. case ARMII::AddrModeT1_s:
  560. return evaluateMemOpAddrForAddrModeT1_s(Inst, Desc, OpIndex, Addr);
  561. }
  562. }
  563. static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
  564. return new ARMMCInstrAnalysis(Info);
  565. }
  566. bool ARM::isCDECoproc(size_t Coproc, const MCSubtargetInfo &STI) {
  567. // Unfortunately we don't have ARMTargetInfo in the disassembler, so we have
  568. // to rely on feature bits.
  569. if (Coproc >= 8)
  570. return false;
  571. return STI.getFeatureBits()[ARM::FeatureCoprocCDE0 + Coproc];
  572. }
  573. // Force static initialization.
  574. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMTargetMC() {
  575. for (Target *T : {&getTheARMLETarget(), &getTheARMBETarget(),
  576. &getTheThumbLETarget(), &getTheThumbBETarget()}) {
  577. // Register the MC asm info.
  578. RegisterMCAsmInfoFn X(*T, createARMMCAsmInfo);
  579. // Register the MC instruction info.
  580. TargetRegistry::RegisterMCInstrInfo(*T, createARMMCInstrInfo);
  581. // Register the MC register info.
  582. TargetRegistry::RegisterMCRegInfo(*T, createARMMCRegisterInfo);
  583. // Register the MC subtarget info.
  584. TargetRegistry::RegisterMCSubtargetInfo(*T,
  585. ARM_MC::createARMMCSubtargetInfo);
  586. TargetRegistry::RegisterELFStreamer(*T, createELFStreamer);
  587. TargetRegistry::RegisterCOFFStreamer(*T, createARMWinCOFFStreamer);
  588. TargetRegistry::RegisterMachOStreamer(*T, createARMMachOStreamer);
  589. // Register the obj target streamer.
  590. TargetRegistry::RegisterObjectTargetStreamer(*T,
  591. createARMObjectTargetStreamer);
  592. // Register the asm streamer.
  593. TargetRegistry::RegisterAsmTargetStreamer(*T, createARMTargetAsmStreamer);
  594. // Register the null TargetStreamer.
  595. TargetRegistry::RegisterNullTargetStreamer(*T, createARMNullTargetStreamer);
  596. // Register the MCInstPrinter.
  597. TargetRegistry::RegisterMCInstPrinter(*T, createARMMCInstPrinter);
  598. // Register the MC relocation info.
  599. TargetRegistry::RegisterMCRelocationInfo(*T, createARMMCRelocationInfo);
  600. }
  601. // Register the MC instruction analyzer.
  602. for (Target *T : {&getTheARMLETarget(), &getTheARMBETarget(),
  603. &getTheThumbLETarget(), &getTheThumbBETarget()})
  604. TargetRegistry::RegisterMCInstrAnalysis(*T, createARMMCInstrAnalysis);
  605. for (Target *T : {&getTheARMLETarget(), &getTheThumbLETarget()}) {
  606. TargetRegistry::RegisterMCCodeEmitter(*T, createARMLEMCCodeEmitter);
  607. TargetRegistry::RegisterMCAsmBackend(*T, createARMLEAsmBackend);
  608. }
  609. for (Target *T : {&getTheARMBETarget(), &getTheThumbBETarget()}) {
  610. TargetRegistry::RegisterMCCodeEmitter(*T, createARMBEMCCodeEmitter);
  611. TargetRegistry::RegisterMCAsmBackend(*T, createARMBEAsmBackend);
  612. }
  613. }