TargetInfo.cpp 26 KB

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