TargetInfo.cpp 26 KB

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