TargetInfo.cpp 26 KB

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