TargetInfo.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. //===--- TargetInfo.cpp - Information about Target machine ----------------===//
  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 TargetInfo and TargetInfoImpl interfaces.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/TargetInfo.h"
  13. #include "clang/Basic/AddressSpaces.h"
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/LangOptions.h"
  17. #include "llvm/ADT/APFloat.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/TargetParser.h"
  21. #include <cstdlib>
  22. using namespace clang;
  23. static const LangASMap DefaultAddrSpaceMap = {0};
  24. // TargetInfo Constructor.
  25. TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) {
  26. // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
  27. // SPARC. These should be overridden by concrete targets as needed.
  28. BigEndian = !T.isLittleEndian();
  29. TLSSupported = true;
  30. VLASupported = true;
  31. NoAsmVariants = false;
  32. HasLegalHalfType = false;
  33. HasFloat128 = false;
  34. HasIbm128 = false;
  35. HasFloat16 = false;
  36. HasBFloat16 = false;
  37. HasLongDouble = true;
  38. HasFPReturn = true;
  39. HasStrictFP = false;
  40. PointerWidth = PointerAlign = 32;
  41. BoolWidth = BoolAlign = 8;
  42. IntWidth = IntAlign = 32;
  43. LongWidth = LongAlign = 32;
  44. LongLongWidth = LongLongAlign = 64;
  45. // Fixed point default bit widths
  46. ShortAccumWidth = ShortAccumAlign = 16;
  47. AccumWidth = AccumAlign = 32;
  48. LongAccumWidth = LongAccumAlign = 64;
  49. ShortFractWidth = ShortFractAlign = 8;
  50. FractWidth = FractAlign = 16;
  51. LongFractWidth = LongFractAlign = 32;
  52. // Fixed point default integral and fractional bit sizes
  53. // We give the _Accum 1 fewer fractional bits than their corresponding _Fract
  54. // types by default to have the same number of fractional bits between _Accum
  55. // and _Fract types.
  56. PaddingOnUnsignedFixedPoint = false;
  57. ShortAccumScale = 7;
  58. AccumScale = 15;
  59. LongAccumScale = 31;
  60. SuitableAlign = 64;
  61. DefaultAlignForAttributeAligned = 128;
  62. MinGlobalAlign = 0;
  63. // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
  64. // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
  65. // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html.
  66. // This alignment guarantee also applies to Windows and Android. On Darwin
  67. // and OpenBSD, the alignment is 16 bytes on both 64-bit and 32-bit systems.
  68. if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid())
  69. NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
  70. else if (T.isOSDarwin() || T.isOSOpenBSD())
  71. NewAlign = 128;
  72. else
  73. NewAlign = 0; // Infer from basic type alignment.
  74. HalfWidth = 16;
  75. HalfAlign = 16;
  76. FloatWidth = 32;
  77. FloatAlign = 32;
  78. DoubleWidth = 64;
  79. DoubleAlign = 64;
  80. LongDoubleWidth = 64;
  81. LongDoubleAlign = 64;
  82. Float128Align = 128;
  83. Ibm128Align = 128;
  84. LargeArrayMinWidth = 0;
  85. LargeArrayAlign = 0;
  86. MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
  87. MaxVectorAlign = 0;
  88. MaxTLSAlign = 0;
  89. SimdDefaultAlign = 0;
  90. SizeType = UnsignedLong;
  91. PtrDiffType = SignedLong;
  92. IntMaxType = SignedLongLong;
  93. IntPtrType = SignedLong;
  94. WCharType = SignedInt;
  95. WIntType = SignedInt;
  96. Char16Type = UnsignedShort;
  97. Char32Type = UnsignedInt;
  98. Int64Type = SignedLongLong;
  99. Int16Type = SignedShort;
  100. SigAtomicType = SignedInt;
  101. ProcessIDType = SignedInt;
  102. UseSignedCharForObjCBool = true;
  103. UseBitFieldTypeAlignment = true;
  104. UseZeroLengthBitfieldAlignment = false;
  105. UseLeadingZeroLengthBitfield = true;
  106. UseExplicitBitFieldAlignment = true;
  107. ZeroLengthBitfieldBoundary = 0;
  108. MaxAlignedAttribute = 0;
  109. HalfFormat = &llvm::APFloat::IEEEhalf();
  110. FloatFormat = &llvm::APFloat::IEEEsingle();
  111. DoubleFormat = &llvm::APFloat::IEEEdouble();
  112. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  113. Float128Format = &llvm::APFloat::IEEEquad();
  114. Ibm128Format = &llvm::APFloat::PPCDoubleDouble();
  115. MCountName = "mcount";
  116. UserLabelPrefix = "_";
  117. RegParmMax = 0;
  118. SSERegParmMax = 0;
  119. HasAlignMac68kSupport = false;
  120. HasBuiltinMSVaList = false;
  121. IsRenderScriptTarget = false;
  122. HasAArch64SVETypes = false;
  123. HasRISCVVTypes = false;
  124. AllowAMDGPUUnsafeFPAtomics = false;
  125. ARMCDECoprocMask = 0;
  126. // Default to no types using fpret.
  127. RealTypeUsesObjCFPRet = 0;
  128. // Default to not using fp2ret for __Complex long double
  129. ComplexLongDoubleUsesFP2Ret = false;
  130. // Set the C++ ABI based on the triple.
  131. TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
  132. ? TargetCXXABI::Microsoft
  133. : TargetCXXABI::GenericItanium);
  134. // Default to an empty address space map.
  135. AddrSpaceMap = &DefaultAddrSpaceMap;
  136. UseAddrSpaceMapMangling = false;
  137. // Default to an unknown platform name.
  138. PlatformName = "unknown";
  139. PlatformMinVersion = VersionTuple();
  140. MaxOpenCLWorkGroupSize = 1024;
  141. ProgramAddrSpace = 0;
  142. }
  143. // Out of line virtual dtor for TargetInfo.
  144. TargetInfo::~TargetInfo() {}
  145. void TargetInfo::resetDataLayout(StringRef DL, const char *ULP) {
  146. DataLayoutString = DL.str();
  147. UserLabelPrefix = ULP;
  148. }
  149. bool
  150. TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
  151. Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch";
  152. return false;
  153. }
  154. bool
  155. TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const {
  156. Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return";
  157. return false;
  158. }
  159. /// getTypeName - Return the user string for the specified integer type enum.
  160. /// For example, SignedShort -> "short".
  161. const char *TargetInfo::getTypeName(IntType T) {
  162. switch (T) {
  163. default: llvm_unreachable("not an integer!");
  164. case SignedChar: return "signed char";
  165. case UnsignedChar: return "unsigned char";
  166. case SignedShort: return "short";
  167. case UnsignedShort: return "unsigned short";
  168. case SignedInt: return "int";
  169. case UnsignedInt: return "unsigned int";
  170. case SignedLong: return "long int";
  171. case UnsignedLong: return "long unsigned int";
  172. case SignedLongLong: return "long long int";
  173. case UnsignedLongLong: return "long long unsigned int";
  174. }
  175. }
  176. /// getTypeConstantSuffix - Return the constant suffix for the specified
  177. /// integer type enum. For example, SignedLong -> "L".
  178. const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
  179. switch (T) {
  180. default: llvm_unreachable("not an integer!");
  181. case SignedChar:
  182. case SignedShort:
  183. case SignedInt: return "";
  184. case SignedLong: return "L";
  185. case SignedLongLong: return "LL";
  186. case UnsignedChar:
  187. if (getCharWidth() < getIntWidth())
  188. return "";
  189. LLVM_FALLTHROUGH;
  190. case UnsignedShort:
  191. if (getShortWidth() < getIntWidth())
  192. return "";
  193. LLVM_FALLTHROUGH;
  194. case UnsignedInt: return "U";
  195. case UnsignedLong: return "UL";
  196. case UnsignedLongLong: return "ULL";
  197. }
  198. }
  199. /// getTypeFormatModifier - Return the printf format modifier for the
  200. /// specified integer type enum. For example, SignedLong -> "l".
  201. const char *TargetInfo::getTypeFormatModifier(IntType T) {
  202. switch (T) {
  203. default: llvm_unreachable("not an integer!");
  204. case SignedChar:
  205. case UnsignedChar: return "hh";
  206. case SignedShort:
  207. case UnsignedShort: return "h";
  208. case SignedInt:
  209. case UnsignedInt: return "";
  210. case SignedLong:
  211. case UnsignedLong: return "l";
  212. case SignedLongLong:
  213. case UnsignedLongLong: return "ll";
  214. }
  215. }
  216. /// getTypeWidth - Return the width (in bits) of the specified integer type
  217. /// enum. For example, SignedInt -> getIntWidth().
  218. unsigned TargetInfo::getTypeWidth(IntType T) const {
  219. switch (T) {
  220. default: llvm_unreachable("not an integer!");
  221. case SignedChar:
  222. case UnsignedChar: return getCharWidth();
  223. case SignedShort:
  224. case UnsignedShort: return getShortWidth();
  225. case SignedInt:
  226. case UnsignedInt: return getIntWidth();
  227. case SignedLong:
  228. case UnsignedLong: return getLongWidth();
  229. case SignedLongLong:
  230. case UnsignedLongLong: return getLongLongWidth();
  231. };
  232. }
  233. TargetInfo::IntType TargetInfo::getIntTypeByWidth(
  234. unsigned BitWidth, bool IsSigned) const {
  235. if (getCharWidth() == BitWidth)
  236. return IsSigned ? SignedChar : UnsignedChar;
  237. if (getShortWidth() == BitWidth)
  238. return IsSigned ? SignedShort : UnsignedShort;
  239. if (getIntWidth() == BitWidth)
  240. return IsSigned ? SignedInt : UnsignedInt;
  241. if (getLongWidth() == BitWidth)
  242. return IsSigned ? SignedLong : UnsignedLong;
  243. if (getLongLongWidth() == BitWidth)
  244. return IsSigned ? SignedLongLong : UnsignedLongLong;
  245. return NoInt;
  246. }
  247. TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
  248. bool IsSigned) const {
  249. if (getCharWidth() >= BitWidth)
  250. return IsSigned ? SignedChar : UnsignedChar;
  251. if (getShortWidth() >= BitWidth)
  252. return IsSigned ? SignedShort : UnsignedShort;
  253. if (getIntWidth() >= BitWidth)
  254. return IsSigned ? SignedInt : UnsignedInt;
  255. if (getLongWidth() >= BitWidth)
  256. return IsSigned ? SignedLong : UnsignedLong;
  257. if (getLongLongWidth() >= BitWidth)
  258. return IsSigned ? SignedLongLong : UnsignedLongLong;
  259. return NoInt;
  260. }
  261. FloatModeKind TargetInfo::getRealTypeByWidth(unsigned BitWidth,
  262. FloatModeKind ExplicitType) const {
  263. if (getFloatWidth() == BitWidth)
  264. return FloatModeKind::Float;
  265. if (getDoubleWidth() == BitWidth)
  266. return FloatModeKind::Double;
  267. switch (BitWidth) {
  268. case 96:
  269. if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
  270. return FloatModeKind::LongDouble;
  271. break;
  272. case 128:
  273. // The caller explicitly asked for an IEEE compliant type but we still
  274. // have to check if the target supports it.
  275. if (ExplicitType == FloatModeKind::Float128)
  276. return hasFloat128Type() ? FloatModeKind::Float128
  277. : FloatModeKind::NoFloat;
  278. if (ExplicitType == FloatModeKind::Ibm128)
  279. return hasIbm128Type() ? FloatModeKind::Ibm128
  280. : FloatModeKind::NoFloat;
  281. if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
  282. &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
  283. return FloatModeKind::LongDouble;
  284. if (hasFloat128Type())
  285. return FloatModeKind::Float128;
  286. break;
  287. }
  288. return FloatModeKind::NoFloat;
  289. }
  290. /// getTypeAlign - Return the alignment (in bits) of the specified integer type
  291. /// enum. For example, SignedInt -> getIntAlign().
  292. unsigned TargetInfo::getTypeAlign(IntType T) const {
  293. switch (T) {
  294. default: llvm_unreachable("not an integer!");
  295. case SignedChar:
  296. case UnsignedChar: return getCharAlign();
  297. case SignedShort:
  298. case UnsignedShort: return getShortAlign();
  299. case SignedInt:
  300. case UnsignedInt: return getIntAlign();
  301. case SignedLong:
  302. case UnsignedLong: return getLongAlign();
  303. case SignedLongLong:
  304. case UnsignedLongLong: return getLongLongAlign();
  305. };
  306. }
  307. /// isTypeSigned - Return whether an integer types is signed. Returns true if
  308. /// the type is signed; false otherwise.
  309. bool TargetInfo::isTypeSigned(IntType T) {
  310. switch (T) {
  311. default: llvm_unreachable("not an integer!");
  312. case SignedChar:
  313. case SignedShort:
  314. case SignedInt:
  315. case SignedLong:
  316. case SignedLongLong:
  317. return true;
  318. case UnsignedChar:
  319. case UnsignedShort:
  320. case UnsignedInt:
  321. case UnsignedLong:
  322. case UnsignedLongLong:
  323. return false;
  324. };
  325. }
  326. /// adjust - Set forced language options.
  327. /// Apply changes to the target information with respect to certain
  328. /// language options which change the target configuration and adjust
  329. /// the language based on the target options where applicable.
  330. void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
  331. if (Opts.NoBitFieldTypeAlign)
  332. UseBitFieldTypeAlignment = false;
  333. switch (Opts.WCharSize) {
  334. default: llvm_unreachable("invalid wchar_t width");
  335. case 0: break;
  336. case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
  337. case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
  338. case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
  339. }
  340. if (Opts.AlignDouble) {
  341. DoubleAlign = LongLongAlign = 64;
  342. LongDoubleAlign = 64;
  343. }
  344. if (Opts.OpenCL) {
  345. // OpenCL C requires specific widths for types, irrespective of
  346. // what these normally are for the target.
  347. // We also define long long and long double here, although the
  348. // OpenCL standard only mentions these as "reserved".
  349. IntWidth = IntAlign = 32;
  350. LongWidth = LongAlign = 64;
  351. LongLongWidth = LongLongAlign = 128;
  352. HalfWidth = HalfAlign = 16;
  353. FloatWidth = FloatAlign = 32;
  354. // Embedded 32-bit targets (OpenCL EP) might have double C type
  355. // defined as float. Let's not override this as it might lead
  356. // to generating illegal code that uses 64bit doubles.
  357. if (DoubleWidth != FloatWidth) {
  358. DoubleWidth = DoubleAlign = 64;
  359. DoubleFormat = &llvm::APFloat::IEEEdouble();
  360. }
  361. LongDoubleWidth = LongDoubleAlign = 128;
  362. unsigned MaxPointerWidth = getMaxPointerWidth();
  363. assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
  364. bool Is32BitArch = MaxPointerWidth == 32;
  365. SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
  366. PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
  367. IntPtrType = Is32BitArch ? SignedInt : SignedLong;
  368. IntMaxType = SignedLongLong;
  369. Int64Type = SignedLong;
  370. HalfFormat = &llvm::APFloat::IEEEhalf();
  371. FloatFormat = &llvm::APFloat::IEEEsingle();
  372. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  373. // OpenCL C v3.0 s6.7.5 - The generic address space requires support for
  374. // OpenCL C 2.0 or OpenCL C 3.0 with the __opencl_c_generic_address_space
  375. // feature
  376. // OpenCL C v3.0 s6.2.1 - OpenCL pipes require support of OpenCL C 2.0
  377. // or later and __opencl_c_pipes feature
  378. // FIXME: These language options are also defined in setLangDefaults()
  379. // for OpenCL C 2.0 but with no access to target capabilities. Target
  380. // should be immutable once created and thus these language options need
  381. // to be defined only once.
  382. if (Opts.getOpenCLCompatibleVersion() == 300) {
  383. const auto &OpenCLFeaturesMap = getSupportedOpenCLOpts();
  384. Opts.OpenCLGenericAddressSpace = hasFeatureEnabled(
  385. OpenCLFeaturesMap, "__opencl_c_generic_address_space");
  386. Opts.OpenCLPipes =
  387. hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_pipes");
  388. Opts.Blocks =
  389. hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_device_enqueue");
  390. }
  391. }
  392. if (Opts.DoubleSize) {
  393. if (Opts.DoubleSize == 32) {
  394. DoubleWidth = 32;
  395. LongDoubleWidth = 32;
  396. DoubleFormat = &llvm::APFloat::IEEEsingle();
  397. LongDoubleFormat = &llvm::APFloat::IEEEsingle();
  398. } else if (Opts.DoubleSize == 64) {
  399. DoubleWidth = 64;
  400. LongDoubleWidth = 64;
  401. DoubleFormat = &llvm::APFloat::IEEEdouble();
  402. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  403. }
  404. }
  405. if (Opts.LongDoubleSize) {
  406. if (Opts.LongDoubleSize == DoubleWidth) {
  407. LongDoubleWidth = DoubleWidth;
  408. LongDoubleAlign = DoubleAlign;
  409. LongDoubleFormat = DoubleFormat;
  410. } else if (Opts.LongDoubleSize == 128) {
  411. LongDoubleWidth = LongDoubleAlign = 128;
  412. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  413. }
  414. }
  415. if (Opts.NewAlignOverride)
  416. NewAlign = Opts.NewAlignOverride * getCharWidth();
  417. // Each unsigned fixed point type has the same number of fractional bits as
  418. // its corresponding signed type.
  419. PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint;
  420. CheckFixedPointBits();
  421. if (Opts.ProtectParens && !checkArithmeticFenceSupported()) {
  422. Diags.Report(diag::err_opt_not_valid_on_target) << "-fprotect-parens";
  423. Opts.ProtectParens = false;
  424. }
  425. }
  426. bool TargetInfo::initFeatureMap(
  427. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  428. const std::vector<std::string> &FeatureVec) const {
  429. for (const auto &F : FeatureVec) {
  430. StringRef Name = F;
  431. // Apply the feature via the target.
  432. bool Enabled = Name[0] == '+';
  433. setFeatureEnabled(Features, Name.substr(1), Enabled);
  434. }
  435. return true;
  436. }
  437. TargetInfo::CallingConvKind
  438. TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
  439. if (getCXXABI() != TargetCXXABI::Microsoft &&
  440. (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4))
  441. return CCK_ClangABI4OrPS4;
  442. return CCK_Default;
  443. }
  444. LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
  445. switch (TK) {
  446. case OCLTK_Image:
  447. case OCLTK_Pipe:
  448. return LangAS::opencl_global;
  449. case OCLTK_Sampler:
  450. return LangAS::opencl_constant;
  451. default:
  452. return LangAS::Default;
  453. }
  454. }
  455. //===----------------------------------------------------------------------===//
  456. static StringRef removeGCCRegisterPrefix(StringRef Name) {
  457. if (Name[0] == '%' || Name[0] == '#')
  458. Name = Name.substr(1);
  459. return Name;
  460. }
  461. /// isValidClobber - Returns whether the passed in string is
  462. /// a valid clobber in an inline asm statement. This is used by
  463. /// Sema.
  464. bool TargetInfo::isValidClobber(StringRef Name) const {
  465. return (isValidGCCRegisterName(Name) || Name == "memory" || Name == "cc" ||
  466. Name == "unwind");
  467. }
  468. /// isValidGCCRegisterName - Returns whether the passed in string
  469. /// is a valid register name according to GCC. This is used by Sema for
  470. /// inline asm statements.
  471. bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
  472. if (Name.empty())
  473. return false;
  474. // Get rid of any register prefix.
  475. Name = removeGCCRegisterPrefix(Name);
  476. if (Name.empty())
  477. return false;
  478. ArrayRef<const char *> Names = getGCCRegNames();
  479. // If we have a number it maps to an entry in the register name array.
  480. if (isDigit(Name[0])) {
  481. unsigned n;
  482. if (!Name.getAsInteger(0, n))
  483. return n < Names.size();
  484. }
  485. // Check register names.
  486. if (llvm::is_contained(Names, Name))
  487. return true;
  488. // Check any additional names that we have.
  489. for (const AddlRegName &ARN : getGCCAddlRegNames())
  490. for (const char *AN : ARN.Names) {
  491. if (!AN)
  492. break;
  493. // Make sure the register that the additional name is for is within
  494. // the bounds of the register names from above.
  495. if (AN == Name && ARN.RegNum < Names.size())
  496. return true;
  497. }
  498. // Now check aliases.
  499. for (const GCCRegAlias &GRA : getGCCRegAliases())
  500. for (const char *A : GRA.Aliases) {
  501. if (!A)
  502. break;
  503. if (A == Name)
  504. return true;
  505. }
  506. return false;
  507. }
  508. StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
  509. bool ReturnCanonical) const {
  510. assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
  511. // Get rid of any register prefix.
  512. Name = removeGCCRegisterPrefix(Name);
  513. ArrayRef<const char *> Names = getGCCRegNames();
  514. // First, check if we have a number.
  515. if (isDigit(Name[0])) {
  516. unsigned n;
  517. if (!Name.getAsInteger(0, n)) {
  518. assert(n < Names.size() && "Out of bounds register number!");
  519. return Names[n];
  520. }
  521. }
  522. // Check any additional names that we have.
  523. for (const AddlRegName &ARN : getGCCAddlRegNames())
  524. for (const char *AN : ARN.Names) {
  525. if (!AN)
  526. break;
  527. // Make sure the register that the additional name is for is within
  528. // the bounds of the register names from above.
  529. if (AN == Name && ARN.RegNum < Names.size())
  530. return ReturnCanonical ? Names[ARN.RegNum] : Name;
  531. }
  532. // Now check aliases.
  533. for (const GCCRegAlias &RA : getGCCRegAliases())
  534. for (const char *A : RA.Aliases) {
  535. if (!A)
  536. break;
  537. if (A == Name)
  538. return RA.Register;
  539. }
  540. return Name;
  541. }
  542. bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
  543. const char *Name = Info.getConstraintStr().c_str();
  544. // An output constraint must start with '=' or '+'
  545. if (*Name != '=' && *Name != '+')
  546. return false;
  547. if (*Name == '+')
  548. Info.setIsReadWrite();
  549. Name++;
  550. while (*Name) {
  551. switch (*Name) {
  552. default:
  553. if (!validateAsmConstraint(Name, Info)) {
  554. // FIXME: We temporarily return false
  555. // so we can add more constraints as we hit it.
  556. // Eventually, an unknown constraint should just be treated as 'g'.
  557. return false;
  558. }
  559. break;
  560. case '&': // early clobber.
  561. Info.setEarlyClobber();
  562. break;
  563. case '%': // commutative.
  564. // FIXME: Check that there is a another register after this one.
  565. break;
  566. case 'r': // general register.
  567. Info.setAllowsRegister();
  568. break;
  569. case 'm': // memory operand.
  570. case 'o': // offsetable memory operand.
  571. case 'V': // non-offsetable memory operand.
  572. case '<': // autodecrement memory operand.
  573. case '>': // autoincrement memory operand.
  574. Info.setAllowsMemory();
  575. break;
  576. case 'g': // general register, memory operand or immediate integer.
  577. case 'X': // any operand.
  578. Info.setAllowsRegister();
  579. Info.setAllowsMemory();
  580. break;
  581. case ',': // multiple alternative constraint. Pass it.
  582. // Handle additional optional '=' or '+' modifiers.
  583. if (Name[1] == '=' || Name[1] == '+')
  584. Name++;
  585. break;
  586. case '#': // Ignore as constraint.
  587. while (Name[1] && Name[1] != ',')
  588. Name++;
  589. break;
  590. case '?': // Disparage slightly code.
  591. case '!': // Disparage severely.
  592. case '*': // Ignore for choosing register preferences.
  593. case 'i': // Ignore i,n,E,F as output constraints (match from the other
  594. // chars)
  595. case 'n':
  596. case 'E':
  597. case 'F':
  598. break; // Pass them.
  599. }
  600. Name++;
  601. }
  602. // Early clobber with a read-write constraint which doesn't permit registers
  603. // is invalid.
  604. if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
  605. return false;
  606. // If a constraint allows neither memory nor register operands it contains
  607. // only modifiers. Reject it.
  608. return Info.allowsMemory() || Info.allowsRegister();
  609. }
  610. bool TargetInfo::resolveSymbolicName(const char *&Name,
  611. ArrayRef<ConstraintInfo> OutputConstraints,
  612. unsigned &Index) const {
  613. assert(*Name == '[' && "Symbolic name did not start with '['");
  614. Name++;
  615. const char *Start = Name;
  616. while (*Name && *Name != ']')
  617. Name++;
  618. if (!*Name) {
  619. // Missing ']'
  620. return false;
  621. }
  622. std::string SymbolicName(Start, Name - Start);
  623. for (Index = 0; Index != OutputConstraints.size(); ++Index)
  624. if (SymbolicName == OutputConstraints[Index].getName())
  625. return true;
  626. return false;
  627. }
  628. bool TargetInfo::validateInputConstraint(
  629. MutableArrayRef<ConstraintInfo> OutputConstraints,
  630. ConstraintInfo &Info) const {
  631. const char *Name = Info.ConstraintStr.c_str();
  632. if (!*Name)
  633. return false;
  634. while (*Name) {
  635. switch (*Name) {
  636. default:
  637. // Check if we have a matching constraint
  638. if (*Name >= '0' && *Name <= '9') {
  639. const char *DigitStart = Name;
  640. while (Name[1] >= '0' && Name[1] <= '9')
  641. Name++;
  642. const char *DigitEnd = Name;
  643. unsigned i;
  644. if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
  645. .getAsInteger(10, i))
  646. return false;
  647. // Check if matching constraint is out of bounds.
  648. if (i >= OutputConstraints.size()) return false;
  649. // A number must refer to an output only operand.
  650. if (OutputConstraints[i].isReadWrite())
  651. return false;
  652. // If the constraint is already tied, it must be tied to the
  653. // same operand referenced to by the number.
  654. if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
  655. return false;
  656. // The constraint should have the same info as the respective
  657. // output constraint.
  658. Info.setTiedOperand(i, OutputConstraints[i]);
  659. } else if (!validateAsmConstraint(Name, Info)) {
  660. // FIXME: This error return is in place temporarily so we can
  661. // add more constraints as we hit it. Eventually, an unknown
  662. // constraint should just be treated as 'g'.
  663. return false;
  664. }
  665. break;
  666. case '[': {
  667. unsigned Index = 0;
  668. if (!resolveSymbolicName(Name, OutputConstraints, Index))
  669. return false;
  670. // If the constraint is already tied, it must be tied to the
  671. // same operand referenced to by the number.
  672. if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
  673. return false;
  674. // A number must refer to an output only operand.
  675. if (OutputConstraints[Index].isReadWrite())
  676. return false;
  677. Info.setTiedOperand(Index, OutputConstraints[Index]);
  678. break;
  679. }
  680. case '%': // commutative
  681. // FIXME: Fail if % is used with the last operand.
  682. break;
  683. case 'i': // immediate integer.
  684. break;
  685. case 'n': // immediate integer with a known value.
  686. Info.setRequiresImmediate();
  687. break;
  688. case 'I': // Various constant constraints with target-specific meanings.
  689. case 'J':
  690. case 'K':
  691. case 'L':
  692. case 'M':
  693. case 'N':
  694. case 'O':
  695. case 'P':
  696. if (!validateAsmConstraint(Name, Info))
  697. return false;
  698. break;
  699. case 'r': // general register.
  700. Info.setAllowsRegister();
  701. break;
  702. case 'm': // memory operand.
  703. case 'o': // offsettable memory operand.
  704. case 'V': // non-offsettable memory operand.
  705. case '<': // autodecrement memory operand.
  706. case '>': // autoincrement memory operand.
  707. Info.setAllowsMemory();
  708. break;
  709. case 'g': // general register, memory operand or immediate integer.
  710. case 'X': // any operand.
  711. Info.setAllowsRegister();
  712. Info.setAllowsMemory();
  713. break;
  714. case 'E': // immediate floating point.
  715. case 'F': // immediate floating point.
  716. case 'p': // address operand.
  717. break;
  718. case ',': // multiple alternative constraint. Ignore comma.
  719. break;
  720. case '#': // Ignore as constraint.
  721. while (Name[1] && Name[1] != ',')
  722. Name++;
  723. break;
  724. case '?': // Disparage slightly code.
  725. case '!': // Disparage severely.
  726. case '*': // Ignore for choosing register preferences.
  727. break; // Pass them.
  728. }
  729. Name++;
  730. }
  731. return true;
  732. }
  733. void TargetInfo::CheckFixedPointBits() const {
  734. // Check that the number of fractional and integral bits (and maybe sign) can
  735. // fit into the bits given for a fixed point type.
  736. assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth);
  737. assert(AccumScale + getAccumIBits() + 1 <= AccumWidth);
  738. assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth);
  739. assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <=
  740. ShortAccumWidth);
  741. assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth);
  742. assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <=
  743. LongAccumWidth);
  744. assert(getShortFractScale() + 1 <= ShortFractWidth);
  745. assert(getFractScale() + 1 <= FractWidth);
  746. assert(getLongFractScale() + 1 <= LongFractWidth);
  747. assert(getUnsignedShortFractScale() <= ShortFractWidth);
  748. assert(getUnsignedFractScale() <= FractWidth);
  749. assert(getUnsignedLongFractScale() <= LongFractWidth);
  750. // Each unsigned fract type has either the same number of fractional bits
  751. // as, or one more fractional bit than, its corresponding signed fract type.
  752. assert(getShortFractScale() == getUnsignedShortFractScale() ||
  753. getShortFractScale() == getUnsignedShortFractScale() - 1);
  754. assert(getFractScale() == getUnsignedFractScale() ||
  755. getFractScale() == getUnsignedFractScale() - 1);
  756. assert(getLongFractScale() == getUnsignedLongFractScale() ||
  757. getLongFractScale() == getUnsignedLongFractScale() - 1);
  758. // When arranged in order of increasing rank (see 6.3.1.3a), the number of
  759. // fractional bits is nondecreasing for each of the following sets of
  760. // fixed-point types:
  761. // - signed fract types
  762. // - unsigned fract types
  763. // - signed accum types
  764. // - unsigned accum types.
  765. assert(getLongFractScale() >= getFractScale() &&
  766. getFractScale() >= getShortFractScale());
  767. assert(getUnsignedLongFractScale() >= getUnsignedFractScale() &&
  768. getUnsignedFractScale() >= getUnsignedShortFractScale());
  769. assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale);
  770. assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() &&
  771. getUnsignedAccumScale() >= getUnsignedShortAccumScale());
  772. // When arranged in order of increasing rank (see 6.3.1.3a), the number of
  773. // integral bits is nondecreasing for each of the following sets of
  774. // fixed-point types:
  775. // - signed accum types
  776. // - unsigned accum types
  777. assert(getLongAccumIBits() >= getAccumIBits() &&
  778. getAccumIBits() >= getShortAccumIBits());
  779. assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() &&
  780. getUnsignedAccumIBits() >= getUnsignedShortAccumIBits());
  781. // Each signed accum type has at least as many integral bits as its
  782. // corresponding unsigned accum type.
  783. assert(getShortAccumIBits() >= getUnsignedShortAccumIBits());
  784. assert(getAccumIBits() >= getUnsignedAccumIBits());
  785. assert(getLongAccumIBits() >= getUnsignedLongAccumIBits());
  786. }
  787. void TargetInfo::copyAuxTarget(const TargetInfo *Aux) {
  788. auto *Target = static_cast<TransferrableTargetInfo*>(this);
  789. auto *Src = static_cast<const TransferrableTargetInfo*>(Aux);
  790. *Target = *Src;
  791. }