ARMTargetStreamer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //===- ARMTargetStreamer.cpp - ARMTargetStreamer class --*- C++ -*---------===//
  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 implements the ARMTargetStreamer class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MCTargetDesc/ARMMCTargetDesc.h"
  13. #include "llvm/MC/ConstantPools.h"
  14. #include "llvm/MC/MCAsmInfo.h"
  15. #include "llvm/MC/MCContext.h"
  16. #include "llvm/MC/MCExpr.h"
  17. #include "llvm/MC/MCStreamer.h"
  18. #include "llvm/MC/MCSubtargetInfo.h"
  19. #include "llvm/Support/ARMBuildAttributes.h"
  20. #include "llvm/Support/TargetParser.h"
  21. using namespace llvm;
  22. //
  23. // ARMTargetStreamer Implemenation
  24. //
  25. ARMTargetStreamer::ARMTargetStreamer(MCStreamer &S)
  26. : MCTargetStreamer(S), ConstantPools(new AssemblerConstantPools()) {}
  27. ARMTargetStreamer::~ARMTargetStreamer() = default;
  28. // The constant pool handling is shared by all ARMTargetStreamer
  29. // implementations.
  30. const MCExpr *ARMTargetStreamer::addConstantPoolEntry(const MCExpr *Expr, SMLoc Loc) {
  31. return ConstantPools->addEntry(Streamer, Expr, 4, Loc);
  32. }
  33. void ARMTargetStreamer::emitCurrentConstantPool() {
  34. ConstantPools->emitForCurrentSection(Streamer);
  35. ConstantPools->clearCacheForCurrentSection(Streamer);
  36. }
  37. // finish() - write out any non-empty assembler constant pools.
  38. void ARMTargetStreamer::emitConstantPools() {
  39. ConstantPools->emitAll(Streamer);
  40. }
  41. // reset() - Reset any state
  42. void ARMTargetStreamer::reset() {}
  43. void ARMTargetStreamer::emitInst(uint32_t Inst, char Suffix) {
  44. unsigned Size;
  45. char Buffer[4];
  46. const bool LittleEndian = getStreamer().getContext().getAsmInfo()->isLittleEndian();
  47. switch (Suffix) {
  48. case '\0':
  49. Size = 4;
  50. for (unsigned II = 0, IE = Size; II != IE; II++) {
  51. const unsigned I = LittleEndian ? (Size - II - 1) : II;
  52. Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
  53. }
  54. break;
  55. case 'n':
  56. case 'w':
  57. Size = (Suffix == 'n' ? 2 : 4);
  58. // Thumb wide instructions are emitted as a pair of 16-bit words of the
  59. // appropriate endianness.
  60. for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
  61. const unsigned I0 = LittleEndian ? II + 0 : II + 1;
  62. const unsigned I1 = LittleEndian ? II + 1 : II + 0;
  63. Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
  64. Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
  65. }
  66. break;
  67. default:
  68. llvm_unreachable("Invalid Suffix");
  69. }
  70. getStreamer().emitBytes(StringRef(Buffer, Size));
  71. }
  72. // The remaining callbacks should be handled separately by each
  73. // streamer.
  74. void ARMTargetStreamer::emitFnStart() {}
  75. void ARMTargetStreamer::emitFnEnd() {}
  76. void ARMTargetStreamer::emitCantUnwind() {}
  77. void ARMTargetStreamer::emitPersonality(const MCSymbol *Personality) {}
  78. void ARMTargetStreamer::emitPersonalityIndex(unsigned Index) {}
  79. void ARMTargetStreamer::emitHandlerData() {}
  80. void ARMTargetStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
  81. int64_t Offset) {}
  82. void ARMTargetStreamer::emitMovSP(unsigned Reg, int64_t Offset) {}
  83. void ARMTargetStreamer::emitPad(int64_t Offset) {}
  84. void ARMTargetStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
  85. bool isVector) {}
  86. void ARMTargetStreamer::emitUnwindRaw(int64_t StackOffset,
  87. const SmallVectorImpl<uint8_t> &Opcodes) {
  88. }
  89. void ARMTargetStreamer::switchVendor(StringRef Vendor) {}
  90. void ARMTargetStreamer::emitAttribute(unsigned Attribute, unsigned Value) {}
  91. void ARMTargetStreamer::emitTextAttribute(unsigned Attribute,
  92. StringRef String) {}
  93. void ARMTargetStreamer::emitIntTextAttribute(unsigned Attribute,
  94. unsigned IntValue,
  95. StringRef StringValue) {}
  96. void ARMTargetStreamer::emitArch(ARM::ArchKind Arch) {}
  97. void ARMTargetStreamer::emitArchExtension(uint64_t ArchExt) {}
  98. void ARMTargetStreamer::emitObjectArch(ARM::ArchKind Arch) {}
  99. void ARMTargetStreamer::emitFPU(unsigned FPU) {}
  100. void ARMTargetStreamer::finishAttributeSection() {}
  101. void
  102. ARMTargetStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) {}
  103. void ARMTargetStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {}
  104. static ARMBuildAttrs::CPUArch getArchForCPU(const MCSubtargetInfo &STI) {
  105. if (STI.getCPU() == "xscale")
  106. return ARMBuildAttrs::v5TEJ;
  107. if (STI.hasFeature(ARM::HasV8Ops)) {
  108. if (STI.hasFeature(ARM::FeatureRClass))
  109. return ARMBuildAttrs::v8_R;
  110. return ARMBuildAttrs::v8_A;
  111. } else if (STI.hasFeature(ARM::HasV8_1MMainlineOps))
  112. return ARMBuildAttrs::v8_1_M_Main;
  113. else if (STI.hasFeature(ARM::HasV8MMainlineOps))
  114. return ARMBuildAttrs::v8_M_Main;
  115. else if (STI.hasFeature(ARM::HasV7Ops)) {
  116. if (STI.hasFeature(ARM::FeatureMClass) && STI.hasFeature(ARM::FeatureDSP))
  117. return ARMBuildAttrs::v7E_M;
  118. return ARMBuildAttrs::v7;
  119. } else if (STI.hasFeature(ARM::HasV6T2Ops))
  120. return ARMBuildAttrs::v6T2;
  121. else if (STI.hasFeature(ARM::HasV8MBaselineOps))
  122. return ARMBuildAttrs::v8_M_Base;
  123. else if (STI.hasFeature(ARM::HasV6MOps))
  124. return ARMBuildAttrs::v6S_M;
  125. else if (STI.hasFeature(ARM::HasV6Ops))
  126. return ARMBuildAttrs::v6;
  127. else if (STI.hasFeature(ARM::HasV5TEOps))
  128. return ARMBuildAttrs::v5TE;
  129. else if (STI.hasFeature(ARM::HasV5TOps))
  130. return ARMBuildAttrs::v5T;
  131. else if (STI.hasFeature(ARM::HasV4TOps))
  132. return ARMBuildAttrs::v4T;
  133. else
  134. return ARMBuildAttrs::v4;
  135. }
  136. static bool isV8M(const MCSubtargetInfo &STI) {
  137. // Note that v8M Baseline is a subset of v6T2!
  138. return (STI.hasFeature(ARM::HasV8MBaselineOps) &&
  139. !STI.hasFeature(ARM::HasV6T2Ops)) ||
  140. STI.hasFeature(ARM::HasV8MMainlineOps);
  141. }
  142. /// Emit the build attributes that only depend on the hardware that we expect
  143. // /to be available, and not on the ABI, or any source-language choices.
  144. void ARMTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {
  145. switchVendor("aeabi");
  146. const StringRef CPUString = STI.getCPU();
  147. if (!CPUString.empty() && !CPUString.startswith("generic")) {
  148. // FIXME: remove krait check when GNU tools support krait cpu
  149. if (STI.hasFeature(ARM::ProcKrait)) {
  150. emitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a9");
  151. // We consider krait as a "cortex-a9" + hwdiv CPU
  152. // Enable hwdiv through ".arch_extension idiv"
  153. if (STI.hasFeature(ARM::FeatureHWDivThumb) ||
  154. STI.hasFeature(ARM::FeatureHWDivARM))
  155. emitArchExtension(ARM::AEK_HWDIVTHUMB | ARM::AEK_HWDIVARM);
  156. } else {
  157. emitTextAttribute(ARMBuildAttrs::CPU_name, CPUString);
  158. }
  159. }
  160. emitAttribute(ARMBuildAttrs::CPU_arch, getArchForCPU(STI));
  161. if (STI.hasFeature(ARM::FeatureAClass)) {
  162. emitAttribute(ARMBuildAttrs::CPU_arch_profile,
  163. ARMBuildAttrs::ApplicationProfile);
  164. } else if (STI.hasFeature(ARM::FeatureRClass)) {
  165. emitAttribute(ARMBuildAttrs::CPU_arch_profile,
  166. ARMBuildAttrs::RealTimeProfile);
  167. } else if (STI.hasFeature(ARM::FeatureMClass)) {
  168. emitAttribute(ARMBuildAttrs::CPU_arch_profile,
  169. ARMBuildAttrs::MicroControllerProfile);
  170. }
  171. emitAttribute(ARMBuildAttrs::ARM_ISA_use, STI.hasFeature(ARM::FeatureNoARM)
  172. ? ARMBuildAttrs::Not_Allowed
  173. : ARMBuildAttrs::Allowed);
  174. if (isV8M(STI)) {
  175. emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
  176. ARMBuildAttrs::AllowThumbDerived);
  177. } else if (STI.hasFeature(ARM::FeatureThumb2)) {
  178. emitAttribute(ARMBuildAttrs::THUMB_ISA_use,
  179. ARMBuildAttrs::AllowThumb32);
  180. } else if (STI.hasFeature(ARM::HasV4TOps)) {
  181. emitAttribute(ARMBuildAttrs::THUMB_ISA_use, ARMBuildAttrs::Allowed);
  182. }
  183. if (STI.hasFeature(ARM::FeatureNEON)) {
  184. /* NEON is not exactly a VFP architecture, but GAS emit one of
  185. * neon/neon-fp-armv8/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
  186. if (STI.hasFeature(ARM::FeatureFPARMv8)) {
  187. if (STI.hasFeature(ARM::FeatureCrypto))
  188. emitFPU(ARM::FK_CRYPTO_NEON_FP_ARMV8);
  189. else
  190. emitFPU(ARM::FK_NEON_FP_ARMV8);
  191. } else if (STI.hasFeature(ARM::FeatureVFP4))
  192. emitFPU(ARM::FK_NEON_VFPV4);
  193. else
  194. emitFPU(STI.hasFeature(ARM::FeatureFP16) ? ARM::FK_NEON_FP16
  195. : ARM::FK_NEON);
  196. // Emit Tag_Advanced_SIMD_arch for ARMv8 architecture
  197. if (STI.hasFeature(ARM::HasV8Ops))
  198. emitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
  199. STI.hasFeature(ARM::HasV8_1aOps)
  200. ? ARMBuildAttrs::AllowNeonARMv8_1a
  201. : ARMBuildAttrs::AllowNeonARMv8);
  202. } else {
  203. if (STI.hasFeature(ARM::FeatureFPARMv8_D16_SP))
  204. // FPv5 and FP-ARMv8 have the same instructions, so are modeled as one
  205. // FPU, but there are two different names for it depending on the CPU.
  206. emitFPU(STI.hasFeature(ARM::FeatureD32)
  207. ? ARM::FK_FP_ARMV8
  208. : (STI.hasFeature(ARM::FeatureFP64) ? ARM::FK_FPV5_D16
  209. : ARM::FK_FPV5_SP_D16));
  210. else if (STI.hasFeature(ARM::FeatureVFP4_D16_SP))
  211. emitFPU(STI.hasFeature(ARM::FeatureD32)
  212. ? ARM::FK_VFPV4
  213. : (STI.hasFeature(ARM::FeatureFP64) ? ARM::FK_VFPV4_D16
  214. : ARM::FK_FPV4_SP_D16));
  215. else if (STI.hasFeature(ARM::FeatureVFP3_D16_SP))
  216. emitFPU(
  217. STI.hasFeature(ARM::FeatureD32)
  218. // +d32
  219. ? (STI.hasFeature(ARM::FeatureFP16) ? ARM::FK_VFPV3_FP16
  220. : ARM::FK_VFPV3)
  221. // -d32
  222. : (STI.hasFeature(ARM::FeatureFP64)
  223. ? (STI.hasFeature(ARM::FeatureFP16)
  224. ? ARM::FK_VFPV3_D16_FP16
  225. : ARM::FK_VFPV3_D16)
  226. : (STI.hasFeature(ARM::FeatureFP16) ? ARM::FK_VFPV3XD_FP16
  227. : ARM::FK_VFPV3XD)));
  228. else if (STI.hasFeature(ARM::FeatureVFP2_SP))
  229. emitFPU(ARM::FK_VFPV2);
  230. }
  231. // ABI_HardFP_use attribute to indicate single precision FP.
  232. if (STI.hasFeature(ARM::FeatureVFP2_SP) && !STI.hasFeature(ARM::FeatureFP64))
  233. emitAttribute(ARMBuildAttrs::ABI_HardFP_use,
  234. ARMBuildAttrs::HardFPSinglePrecision);
  235. if (STI.hasFeature(ARM::FeatureFP16))
  236. emitAttribute(ARMBuildAttrs::FP_HP_extension, ARMBuildAttrs::AllowHPFP);
  237. if (STI.hasFeature(ARM::FeatureMP))
  238. emitAttribute(ARMBuildAttrs::MPextension_use, ARMBuildAttrs::AllowMP);
  239. if (STI.hasFeature(ARM::HasMVEFloatOps))
  240. emitAttribute(ARMBuildAttrs::MVE_arch, ARMBuildAttrs::AllowMVEIntegerAndFloat);
  241. else if (STI.hasFeature(ARM::HasMVEIntegerOps))
  242. emitAttribute(ARMBuildAttrs::MVE_arch, ARMBuildAttrs::AllowMVEInteger);
  243. // Hardware divide in ARM mode is part of base arch, starting from ARMv8.
  244. // If only Thumb hwdiv is present, it must also be in base arch (ARMv7-R/M).
  245. // It is not possible to produce DisallowDIV: if hwdiv is present in the base
  246. // arch, supplying -hwdiv downgrades the effective arch, via ClearImpliedBits.
  247. // AllowDIVExt is only emitted if hwdiv isn't available in the base arch;
  248. // otherwise, the default value (AllowDIVIfExists) applies.
  249. if (STI.hasFeature(ARM::FeatureHWDivARM) && !STI.hasFeature(ARM::HasV8Ops))
  250. emitAttribute(ARMBuildAttrs::DIV_use, ARMBuildAttrs::AllowDIVExt);
  251. if (STI.hasFeature(ARM::FeatureDSP) && isV8M(STI))
  252. emitAttribute(ARMBuildAttrs::DSP_extension, ARMBuildAttrs::Allowed);
  253. if (STI.hasFeature(ARM::FeatureStrictAlign))
  254. emitAttribute(ARMBuildAttrs::CPU_unaligned_access,
  255. ARMBuildAttrs::Not_Allowed);
  256. else
  257. emitAttribute(ARMBuildAttrs::CPU_unaligned_access,
  258. ARMBuildAttrs::Allowed);
  259. if (STI.hasFeature(ARM::FeatureTrustZone) &&
  260. STI.hasFeature(ARM::FeatureVirtualization))
  261. emitAttribute(ARMBuildAttrs::Virtualization_use,
  262. ARMBuildAttrs::AllowTZVirtualization);
  263. else if (STI.hasFeature(ARM::FeatureTrustZone))
  264. emitAttribute(ARMBuildAttrs::Virtualization_use, ARMBuildAttrs::AllowTZ);
  265. else if (STI.hasFeature(ARM::FeatureVirtualization))
  266. emitAttribute(ARMBuildAttrs::Virtualization_use,
  267. ARMBuildAttrs::AllowVirtualization);
  268. if (STI.hasFeature(ARM::FeaturePACBTI)) {
  269. emitAttribute(ARMBuildAttrs::PAC_extension, ARMBuildAttrs::AllowPAC);
  270. emitAttribute(ARMBuildAttrs::BTI_extension, ARMBuildAttrs::AllowBTI);
  271. }
  272. }