PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Support/Triple.cpp

https://github.com/carletes/llvm
C++ | 1004 lines | 816 code | 97 blank | 91 comment | 129 complexity | 2b42d31c9840a08e0a8cfa65b66d28af MD5 | raw file
  1. //===--- Triple.cpp - Target triple helper class --------------------------===//
  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. #include "llvm/ADT/Triple.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallString.h"
  12. #include "llvm/ADT/StringSwitch.h"
  13. #include "llvm/Support/ErrorHandling.h"
  14. #include <cstring>
  15. using namespace llvm;
  16. const char *Triple::getArchTypeName(ArchType Kind) {
  17. switch (Kind) {
  18. case UnknownArch: return "unknown";
  19. case aarch64: return "aarch64";
  20. case aarch64_be: return "aarch64_be";
  21. case arm: return "arm";
  22. case armeb: return "armeb";
  23. case hexagon: return "hexagon";
  24. case mips: return "mips";
  25. case mipsel: return "mipsel";
  26. case mips64: return "mips64";
  27. case mips64el: return "mips64el";
  28. case msp430: return "msp430";
  29. case ppc64: return "powerpc64";
  30. case ppc64le: return "powerpc64le";
  31. case ppc: return "powerpc";
  32. case r600: return "r600";
  33. case sparc: return "sparc";
  34. case sparcv9: return "sparcv9";
  35. case systemz: return "s390x";
  36. case tce: return "tce";
  37. case thumb: return "thumb";
  38. case thumbeb: return "thumbeb";
  39. case x86: return "i386";
  40. case x86_64: return "x86_64";
  41. case xcore: return "xcore";
  42. case nvptx: return "nvptx";
  43. case nvptx64: return "nvptx64";
  44. case le32: return "le32";
  45. case amdil: return "amdil";
  46. case spir: return "spir";
  47. case spir64: return "spir64";
  48. case kalimba: return "kalimba";
  49. }
  50. llvm_unreachable("Invalid ArchType!");
  51. }
  52. const char *Triple::getArchTypePrefix(ArchType Kind) {
  53. switch (Kind) {
  54. default:
  55. return nullptr;
  56. case aarch64:
  57. case aarch64_be: return "aarch64";
  58. case arm:
  59. case armeb:
  60. case thumb:
  61. case thumbeb: return "arm";
  62. case ppc64:
  63. case ppc64le:
  64. case ppc: return "ppc";
  65. case mips:
  66. case mipsel:
  67. case mips64:
  68. case mips64el: return "mips";
  69. case hexagon: return "hexagon";
  70. case r600: return "r600";
  71. case sparcv9:
  72. case sparc: return "sparc";
  73. case systemz: return "systemz";
  74. case x86:
  75. case x86_64: return "x86";
  76. case xcore: return "xcore";
  77. case nvptx: return "nvptx";
  78. case nvptx64: return "nvptx";
  79. case le32: return "le32";
  80. case amdil: return "amdil";
  81. case spir: return "spir";
  82. case spir64: return "spir";
  83. case kalimba: return "kalimba";
  84. }
  85. }
  86. const char *Triple::getVendorTypeName(VendorType Kind) {
  87. switch (Kind) {
  88. case UnknownVendor: return "unknown";
  89. case Apple: return "apple";
  90. case PC: return "pc";
  91. case SCEI: return "scei";
  92. case BGP: return "bgp";
  93. case BGQ: return "bgq";
  94. case Freescale: return "fsl";
  95. case IBM: return "ibm";
  96. case ImaginationTechnologies: return "img";
  97. case MipsTechnologies: return "mti";
  98. case NVIDIA: return "nvidia";
  99. case CSR: return "csr";
  100. }
  101. llvm_unreachable("Invalid VendorType!");
  102. }
  103. const char *Triple::getOSTypeName(OSType Kind) {
  104. switch (Kind) {
  105. case UnknownOS: return "unknown";
  106. case Darwin: return "darwin";
  107. case DragonFly: return "dragonfly";
  108. case FreeBSD: return "freebsd";
  109. case IOS: return "ios";
  110. case KFreeBSD: return "kfreebsd";
  111. case Linux: return "linux";
  112. case Lv2: return "lv2";
  113. case MacOSX: return "macosx";
  114. case NetBSD: return "netbsd";
  115. case OpenBSD: return "openbsd";
  116. case Solaris: return "solaris";
  117. case Win32: return "windows";
  118. case Haiku: return "haiku";
  119. case Minix: return "minix";
  120. case RTEMS: return "rtems";
  121. case NaCl: return "nacl";
  122. case CNK: return "cnk";
  123. case Bitrig: return "bitrig";
  124. case AIX: return "aix";
  125. case CUDA: return "cuda";
  126. case NVCL: return "nvcl";
  127. }
  128. llvm_unreachable("Invalid OSType");
  129. }
  130. const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
  131. switch (Kind) {
  132. case UnknownEnvironment: return "unknown";
  133. case GNU: return "gnu";
  134. case GNUEABIHF: return "gnueabihf";
  135. case GNUEABI: return "gnueabi";
  136. case GNUX32: return "gnux32";
  137. case CODE16: return "code16";
  138. case EABI: return "eabi";
  139. case EABIHF: return "eabihf";
  140. case Android: return "android";
  141. case MSVC: return "msvc";
  142. case Itanium: return "itanium";
  143. case Cygnus: return "cygnus";
  144. }
  145. llvm_unreachable("Invalid EnvironmentType!");
  146. }
  147. Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
  148. return StringSwitch<Triple::ArchType>(Name)
  149. .Case("aarch64", aarch64)
  150. .Case("aarch64_be", aarch64_be)
  151. .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
  152. .Case("arm", arm)
  153. .Case("armeb", armeb)
  154. .Case("mips", mips)
  155. .Case("mipsel", mipsel)
  156. .Case("mips64", mips64)
  157. .Case("mips64el", mips64el)
  158. .Case("msp430", msp430)
  159. .Case("ppc64", ppc64)
  160. .Case("ppc32", ppc)
  161. .Case("ppc", ppc)
  162. .Case("ppc64le", ppc64le)
  163. .Case("r600", r600)
  164. .Case("hexagon", hexagon)
  165. .Case("sparc", sparc)
  166. .Case("sparcv9", sparcv9)
  167. .Case("systemz", systemz)
  168. .Case("tce", tce)
  169. .Case("thumb", thumb)
  170. .Case("thumbeb", thumbeb)
  171. .Case("x86", x86)
  172. .Case("x86-64", x86_64)
  173. .Case("xcore", xcore)
  174. .Case("nvptx", nvptx)
  175. .Case("nvptx64", nvptx64)
  176. .Case("le32", le32)
  177. .Case("amdil", amdil)
  178. .Case("spir", spir)
  179. .Case("spir64", spir64)
  180. .Case("kalimba", kalimba)
  181. .Default(UnknownArch);
  182. }
  183. static Triple::ArchType parseArch(StringRef ArchName) {
  184. return StringSwitch<Triple::ArchType>(ArchName)
  185. .Cases("i386", "i486", "i586", "i686", Triple::x86)
  186. // FIXME: Do we need to support these?
  187. .Cases("i786", "i886", "i986", Triple::x86)
  188. .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
  189. .Case("powerpc", Triple::ppc)
  190. .Cases("powerpc64", "ppu", Triple::ppc64)
  191. .Case("powerpc64le", Triple::ppc64le)
  192. .Case("aarch64", Triple::aarch64)
  193. .Case("aarch64_be", Triple::aarch64_be)
  194. .Case("arm64", Triple::aarch64)
  195. .Cases("arm", "xscale", Triple::arm)
  196. // FIXME: It would be good to replace these with explicit names for all the
  197. // various suffixes supported.
  198. .StartsWith("armv", Triple::arm)
  199. .Case("armeb", Triple::armeb)
  200. .StartsWith("armebv", Triple::armeb)
  201. .Case("thumb", Triple::thumb)
  202. .StartsWith("thumbv", Triple::thumb)
  203. .Case("thumbeb", Triple::thumbeb)
  204. .StartsWith("thumbebv", Triple::thumbeb)
  205. .Case("msp430", Triple::msp430)
  206. .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
  207. .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
  208. .Cases("mips64", "mips64eb", Triple::mips64)
  209. .Case("mips64el", Triple::mips64el)
  210. .Case("r600", Triple::r600)
  211. .Case("hexagon", Triple::hexagon)
  212. .Case("s390x", Triple::systemz)
  213. .Case("sparc", Triple::sparc)
  214. .Cases("sparcv9", "sparc64", Triple::sparcv9)
  215. .Case("tce", Triple::tce)
  216. .Case("xcore", Triple::xcore)
  217. .Case("nvptx", Triple::nvptx)
  218. .Case("nvptx64", Triple::nvptx64)
  219. .Case("le32", Triple::le32)
  220. .Case("amdil", Triple::amdil)
  221. .Case("spir", Triple::spir)
  222. .Case("spir64", Triple::spir64)
  223. .StartsWith("kalimba", Triple::kalimba)
  224. .Default(Triple::UnknownArch);
  225. }
  226. static Triple::VendorType parseVendor(StringRef VendorName) {
  227. return StringSwitch<Triple::VendorType>(VendorName)
  228. .Case("apple", Triple::Apple)
  229. .Case("pc", Triple::PC)
  230. .Case("scei", Triple::SCEI)
  231. .Case("bgp", Triple::BGP)
  232. .Case("bgq", Triple::BGQ)
  233. .Case("fsl", Triple::Freescale)
  234. .Case("ibm", Triple::IBM)
  235. .Case("img", Triple::ImaginationTechnologies)
  236. .Case("mti", Triple::MipsTechnologies)
  237. .Case("nvidia", Triple::NVIDIA)
  238. .Case("csr", Triple::CSR)
  239. .Default(Triple::UnknownVendor);
  240. }
  241. static Triple::OSType parseOS(StringRef OSName) {
  242. return StringSwitch<Triple::OSType>(OSName)
  243. .StartsWith("darwin", Triple::Darwin)
  244. .StartsWith("dragonfly", Triple::DragonFly)
  245. .StartsWith("freebsd", Triple::FreeBSD)
  246. .StartsWith("ios", Triple::IOS)
  247. .StartsWith("kfreebsd", Triple::KFreeBSD)
  248. .StartsWith("linux", Triple::Linux)
  249. .StartsWith("lv2", Triple::Lv2)
  250. .StartsWith("macosx", Triple::MacOSX)
  251. .StartsWith("netbsd", Triple::NetBSD)
  252. .StartsWith("openbsd", Triple::OpenBSD)
  253. .StartsWith("solaris", Triple::Solaris)
  254. .StartsWith("win32", Triple::Win32)
  255. .StartsWith("windows", Triple::Win32)
  256. .StartsWith("haiku", Triple::Haiku)
  257. .StartsWith("minix", Triple::Minix)
  258. .StartsWith("rtems", Triple::RTEMS)
  259. .StartsWith("nacl", Triple::NaCl)
  260. .StartsWith("cnk", Triple::CNK)
  261. .StartsWith("bitrig", Triple::Bitrig)
  262. .StartsWith("aix", Triple::AIX)
  263. .StartsWith("cuda", Triple::CUDA)
  264. .StartsWith("nvcl", Triple::NVCL)
  265. .Default(Triple::UnknownOS);
  266. }
  267. static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
  268. return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
  269. .StartsWith("eabihf", Triple::EABIHF)
  270. .StartsWith("eabi", Triple::EABI)
  271. .StartsWith("gnueabihf", Triple::GNUEABIHF)
  272. .StartsWith("gnueabi", Triple::GNUEABI)
  273. .StartsWith("gnux32", Triple::GNUX32)
  274. .StartsWith("code16", Triple::CODE16)
  275. .StartsWith("gnu", Triple::GNU)
  276. .StartsWith("android", Triple::Android)
  277. .StartsWith("msvc", Triple::MSVC)
  278. .StartsWith("itanium", Triple::Itanium)
  279. .StartsWith("cygnus", Triple::Cygnus)
  280. .Default(Triple::UnknownEnvironment);
  281. }
  282. static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
  283. return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
  284. .EndsWith("coff", Triple::COFF)
  285. .EndsWith("elf", Triple::ELF)
  286. .EndsWith("macho", Triple::MachO)
  287. .Default(Triple::UnknownObjectFormat);
  288. }
  289. static Triple::SubArchType parseSubArch(StringRef SubArchName) {
  290. return StringSwitch<Triple::SubArchType>(SubArchName)
  291. .EndsWith("v8", Triple::ARMSubArch_v8)
  292. .EndsWith("v8a", Triple::ARMSubArch_v8)
  293. .EndsWith("v7", Triple::ARMSubArch_v7)
  294. .EndsWith("v7a", Triple::ARMSubArch_v7)
  295. .EndsWith("v7em", Triple::ARMSubArch_v7em)
  296. .EndsWith("v7l", Triple::ARMSubArch_v7)
  297. .EndsWith("v7m", Triple::ARMSubArch_v7m)
  298. .EndsWith("v7r", Triple::ARMSubArch_v7)
  299. .EndsWith("v7s", Triple::ARMSubArch_v7s)
  300. .EndsWith("v6", Triple::ARMSubArch_v6)
  301. .EndsWith("v6m", Triple::ARMSubArch_v6m)
  302. .EndsWith("v6t2", Triple::ARMSubArch_v6t2)
  303. .EndsWith("v5", Triple::ARMSubArch_v5)
  304. .EndsWith("v5e", Triple::ARMSubArch_v5)
  305. .EndsWith("v5t", Triple::ARMSubArch_v5)
  306. .EndsWith("v5te", Triple::ARMSubArch_v5te)
  307. .EndsWith("v4t", Triple::ARMSubArch_v4t)
  308. .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
  309. .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
  310. .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
  311. .Default(Triple::NoSubArch);
  312. }
  313. static const char *getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
  314. switch (Kind) {
  315. case Triple::UnknownObjectFormat: return "";
  316. case Triple::COFF: return "coff";
  317. case Triple::ELF: return "elf";
  318. case Triple::MachO: return "macho";
  319. }
  320. llvm_unreachable("unknown object format type");
  321. }
  322. static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
  323. if (T.isOSDarwin())
  324. return Triple::MachO;
  325. else if (T.isOSWindows())
  326. return Triple::COFF;
  327. return Triple::ELF;
  328. }
  329. /// \brief Construct a triple from the string representation provided.
  330. ///
  331. /// This stores the string representation and parses the various pieces into
  332. /// enum members.
  333. Triple::Triple(const Twine &Str)
  334. : Data(Str.str()),
  335. Arch(parseArch(getArchName())),
  336. SubArch(parseSubArch(getArchName())),
  337. Vendor(parseVendor(getVendorName())),
  338. OS(parseOS(getOSName())),
  339. Environment(parseEnvironment(getEnvironmentName())),
  340. ObjectFormat(parseFormat(getEnvironmentName())) {
  341. if (ObjectFormat == Triple::UnknownObjectFormat)
  342. ObjectFormat = getDefaultFormat(*this);
  343. }
  344. /// \brief Construct a triple from string representations of the architecture,
  345. /// vendor, and OS.
  346. ///
  347. /// This joins each argument into a canonical string representation and parses
  348. /// them into enum members. It leaves the environment unknown and omits it from
  349. /// the string representation.
  350. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
  351. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
  352. Arch(parseArch(ArchStr.str())),
  353. SubArch(parseSubArch(ArchStr.str())),
  354. Vendor(parseVendor(VendorStr.str())),
  355. OS(parseOS(OSStr.str())),
  356. Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
  357. ObjectFormat = getDefaultFormat(*this);
  358. }
  359. /// \brief Construct a triple from string representations of the architecture,
  360. /// vendor, OS, and environment.
  361. ///
  362. /// This joins each argument into a canonical string representation and parses
  363. /// them into enum members.
  364. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
  365. const Twine &EnvironmentStr)
  366. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
  367. EnvironmentStr).str()),
  368. Arch(parseArch(ArchStr.str())),
  369. SubArch(parseSubArch(ArchStr.str())),
  370. Vendor(parseVendor(VendorStr.str())),
  371. OS(parseOS(OSStr.str())),
  372. Environment(parseEnvironment(EnvironmentStr.str())),
  373. ObjectFormat(parseFormat(EnvironmentStr.str())) {
  374. if (ObjectFormat == Triple::UnknownObjectFormat)
  375. ObjectFormat = getDefaultFormat(*this);
  376. }
  377. std::string Triple::normalize(StringRef Str) {
  378. bool IsMinGW32 = false;
  379. bool IsCygwin = false;
  380. // Parse into components.
  381. SmallVector<StringRef, 4> Components;
  382. Str.split(Components, "-");
  383. // If the first component corresponds to a known architecture, preferentially
  384. // use it for the architecture. If the second component corresponds to a
  385. // known vendor, preferentially use it for the vendor, etc. This avoids silly
  386. // component movement when a component parses as (eg) both a valid arch and a
  387. // valid os.
  388. ArchType Arch = UnknownArch;
  389. if (Components.size() > 0)
  390. Arch = parseArch(Components[0]);
  391. VendorType Vendor = UnknownVendor;
  392. if (Components.size() > 1)
  393. Vendor = parseVendor(Components[1]);
  394. OSType OS = UnknownOS;
  395. if (Components.size() > 2) {
  396. OS = parseOS(Components[2]);
  397. IsCygwin = Components[2].startswith("cygwin");
  398. IsMinGW32 = Components[2].startswith("mingw");
  399. }
  400. EnvironmentType Environment = UnknownEnvironment;
  401. if (Components.size() > 3)
  402. Environment = parseEnvironment(Components[3]);
  403. ObjectFormatType ObjectFormat = UnknownObjectFormat;
  404. if (Components.size() > 4)
  405. ObjectFormat = parseFormat(Components[4]);
  406. // Note which components are already in their final position. These will not
  407. // be moved.
  408. bool Found[4];
  409. Found[0] = Arch != UnknownArch;
  410. Found[1] = Vendor != UnknownVendor;
  411. Found[2] = OS != UnknownOS;
  412. Found[3] = Environment != UnknownEnvironment;
  413. // If they are not there already, permute the components into their canonical
  414. // positions by seeing if they parse as a valid architecture, and if so moving
  415. // the component to the architecture position etc.
  416. for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
  417. if (Found[Pos])
  418. continue; // Already in the canonical position.
  419. for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
  420. // Do not reparse any components that already matched.
  421. if (Idx < array_lengthof(Found) && Found[Idx])
  422. continue;
  423. // Does this component parse as valid for the target position?
  424. bool Valid = false;
  425. StringRef Comp = Components[Idx];
  426. switch (Pos) {
  427. default: llvm_unreachable("unexpected component type!");
  428. case 0:
  429. Arch = parseArch(Comp);
  430. Valid = Arch != UnknownArch;
  431. break;
  432. case 1:
  433. Vendor = parseVendor(Comp);
  434. Valid = Vendor != UnknownVendor;
  435. break;
  436. case 2:
  437. OS = parseOS(Comp);
  438. IsCygwin = Comp.startswith("cygwin");
  439. IsMinGW32 = Comp.startswith("mingw");
  440. Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
  441. break;
  442. case 3:
  443. Environment = parseEnvironment(Comp);
  444. Valid = Environment != UnknownEnvironment;
  445. if (!Valid) {
  446. ObjectFormat = parseFormat(Comp);
  447. Valid = ObjectFormat != UnknownObjectFormat;
  448. }
  449. break;
  450. }
  451. if (!Valid)
  452. continue; // Nope, try the next component.
  453. // Move the component to the target position, pushing any non-fixed
  454. // components that are in the way to the right. This tends to give
  455. // good results in the common cases of a forgotten vendor component
  456. // or a wrongly positioned environment.
  457. if (Pos < Idx) {
  458. // Insert left, pushing the existing components to the right. For
  459. // example, a-b-i386 -> i386-a-b when moving i386 to the front.
  460. StringRef CurrentComponent(""); // The empty component.
  461. // Replace the component we are moving with an empty component.
  462. std::swap(CurrentComponent, Components[Idx]);
  463. // Insert the component being moved at Pos, displacing any existing
  464. // components to the right.
  465. for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
  466. // Skip over any fixed components.
  467. while (i < array_lengthof(Found) && Found[i])
  468. ++i;
  469. // Place the component at the new position, getting the component
  470. // that was at this position - it will be moved right.
  471. std::swap(CurrentComponent, Components[i]);
  472. }
  473. } else if (Pos > Idx) {
  474. // Push right by inserting empty components until the component at Idx
  475. // reaches the target position Pos. For example, pc-a -> -pc-a when
  476. // moving pc to the second position.
  477. do {
  478. // Insert one empty component at Idx.
  479. StringRef CurrentComponent(""); // The empty component.
  480. for (unsigned i = Idx; i < Components.size();) {
  481. // Place the component at the new position, getting the component
  482. // that was at this position - it will be moved right.
  483. std::swap(CurrentComponent, Components[i]);
  484. // If it was placed on top of an empty component then we are done.
  485. if (CurrentComponent.empty())
  486. break;
  487. // Advance to the next component, skipping any fixed components.
  488. while (++i < array_lengthof(Found) && Found[i])
  489. ;
  490. }
  491. // The last component was pushed off the end - append it.
  492. if (!CurrentComponent.empty())
  493. Components.push_back(CurrentComponent);
  494. // Advance Idx to the component's new position.
  495. while (++Idx < array_lengthof(Found) && Found[Idx])
  496. ;
  497. } while (Idx < Pos); // Add more until the final position is reached.
  498. }
  499. assert(Pos < Components.size() && Components[Pos] == Comp &&
  500. "Component moved wrong!");
  501. Found[Pos] = true;
  502. break;
  503. }
  504. }
  505. // Special case logic goes here. At this point Arch, Vendor and OS have the
  506. // correct values for the computed components.
  507. if (OS == Triple::Win32) {
  508. Components.resize(4);
  509. Components[2] = "windows";
  510. if (Environment == UnknownEnvironment) {
  511. if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
  512. Components[3] = "msvc";
  513. else
  514. Components[3] = getObjectFormatTypeName(ObjectFormat);
  515. }
  516. } else if (IsMinGW32) {
  517. Components.resize(4);
  518. Components[2] = "windows";
  519. Components[3] = "gnu";
  520. } else if (IsCygwin) {
  521. Components.resize(4);
  522. Components[2] = "windows";
  523. Components[3] = "cygnus";
  524. }
  525. if (IsMinGW32 || IsCygwin ||
  526. (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
  527. if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
  528. Components.resize(5);
  529. Components[4] = getObjectFormatTypeName(ObjectFormat);
  530. }
  531. }
  532. // Stick the corrected components back together to form the normalized string.
  533. std::string Normalized;
  534. for (unsigned i = 0, e = Components.size(); i != e; ++i) {
  535. if (i) Normalized += '-';
  536. Normalized += Components[i];
  537. }
  538. return Normalized;
  539. }
  540. StringRef Triple::getArchName() const {
  541. return StringRef(Data).split('-').first; // Isolate first component
  542. }
  543. StringRef Triple::getVendorName() const {
  544. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  545. return Tmp.split('-').first; // Isolate second component
  546. }
  547. StringRef Triple::getOSName() const {
  548. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  549. Tmp = Tmp.split('-').second; // Strip second component
  550. return Tmp.split('-').first; // Isolate third component
  551. }
  552. StringRef Triple::getEnvironmentName() const {
  553. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  554. Tmp = Tmp.split('-').second; // Strip second component
  555. return Tmp.split('-').second; // Strip third component
  556. }
  557. StringRef Triple::getOSAndEnvironmentName() const {
  558. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  559. return Tmp.split('-').second; // Strip second component
  560. }
  561. static unsigned EatNumber(StringRef &Str) {
  562. assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
  563. unsigned Result = 0;
  564. do {
  565. // Consume the leading digit.
  566. Result = Result*10 + (Str[0] - '0');
  567. // Eat the digit.
  568. Str = Str.substr(1);
  569. } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
  570. return Result;
  571. }
  572. void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
  573. unsigned &Micro) const {
  574. StringRef OSName = getOSName();
  575. // Assume that the OS portion of the triple starts with the canonical name.
  576. StringRef OSTypeName = getOSTypeName(getOS());
  577. if (OSName.startswith(OSTypeName))
  578. OSName = OSName.substr(OSTypeName.size());
  579. // Any unset version defaults to 0.
  580. Major = Minor = Micro = 0;
  581. // Parse up to three components.
  582. unsigned *Components[3] = { &Major, &Minor, &Micro };
  583. for (unsigned i = 0; i != 3; ++i) {
  584. if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
  585. break;
  586. // Consume the leading number.
  587. *Components[i] = EatNumber(OSName);
  588. // Consume the separator, if present.
  589. if (OSName.startswith("."))
  590. OSName = OSName.substr(1);
  591. }
  592. }
  593. bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
  594. unsigned &Micro) const {
  595. getOSVersion(Major, Minor, Micro);
  596. switch (getOS()) {
  597. default: llvm_unreachable("unexpected OS for Darwin triple");
  598. case Darwin:
  599. // Default to darwin8, i.e., MacOSX 10.4.
  600. if (Major == 0)
  601. Major = 8;
  602. // Darwin version numbers are skewed from OS X versions.
  603. if (Major < 4)
  604. return false;
  605. Micro = 0;
  606. Minor = Major - 4;
  607. Major = 10;
  608. break;
  609. case MacOSX:
  610. // Default to 10.4.
  611. if (Major == 0) {
  612. Major = 10;
  613. Minor = 4;
  614. }
  615. if (Major != 10)
  616. return false;
  617. break;
  618. case IOS:
  619. // Ignore the version from the triple. This is only handled because the
  620. // the clang driver combines OS X and IOS support into a common Darwin
  621. // toolchain that wants to know the OS X version number even when targeting
  622. // IOS.
  623. Major = 10;
  624. Minor = 4;
  625. Micro = 0;
  626. break;
  627. }
  628. return true;
  629. }
  630. void Triple::getiOSVersion(unsigned &Major, unsigned &Minor,
  631. unsigned &Micro) const {
  632. switch (getOS()) {
  633. default: llvm_unreachable("unexpected OS for Darwin triple");
  634. case Darwin:
  635. case MacOSX:
  636. // Ignore the version from the triple. This is only handled because the
  637. // the clang driver combines OS X and IOS support into a common Darwin
  638. // toolchain that wants to know the iOS version number even when targeting
  639. // OS X.
  640. Major = 5;
  641. Minor = 0;
  642. Micro = 0;
  643. break;
  644. case IOS:
  645. getOSVersion(Major, Minor, Micro);
  646. // Default to 5.0 (or 7.0 for arm64).
  647. if (Major == 0)
  648. Major = (getArch() == aarch64) ? 7 : 5;
  649. break;
  650. }
  651. }
  652. void Triple::setTriple(const Twine &Str) {
  653. *this = Triple(Str);
  654. }
  655. void Triple::setArch(ArchType Kind) {
  656. setArchName(getArchTypeName(Kind));
  657. }
  658. void Triple::setVendor(VendorType Kind) {
  659. setVendorName(getVendorTypeName(Kind));
  660. }
  661. void Triple::setOS(OSType Kind) {
  662. setOSName(getOSTypeName(Kind));
  663. }
  664. void Triple::setEnvironment(EnvironmentType Kind) {
  665. setEnvironmentName(getEnvironmentTypeName(Kind));
  666. }
  667. void Triple::setObjectFormat(ObjectFormatType Kind) {
  668. if (Environment == UnknownEnvironment)
  669. return setEnvironmentName(getObjectFormatTypeName(Kind));
  670. setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
  671. getObjectFormatTypeName(Kind)).str());
  672. }
  673. void Triple::setArchName(StringRef Str) {
  674. // Work around a miscompilation bug for Twines in gcc 4.0.3.
  675. SmallString<64> Triple;
  676. Triple += Str;
  677. Triple += "-";
  678. Triple += getVendorName();
  679. Triple += "-";
  680. Triple += getOSAndEnvironmentName();
  681. setTriple(Triple.str());
  682. }
  683. void Triple::setVendorName(StringRef Str) {
  684. setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
  685. }
  686. void Triple::setOSName(StringRef Str) {
  687. if (hasEnvironment())
  688. setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
  689. "-" + getEnvironmentName());
  690. else
  691. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  692. }
  693. void Triple::setEnvironmentName(StringRef Str) {
  694. setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
  695. "-" + Str);
  696. }
  697. void Triple::setOSAndEnvironmentName(StringRef Str) {
  698. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  699. }
  700. static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
  701. switch (Arch) {
  702. case llvm::Triple::UnknownArch:
  703. return 0;
  704. case llvm::Triple::msp430:
  705. return 16;
  706. case llvm::Triple::amdil:
  707. case llvm::Triple::arm:
  708. case llvm::Triple::armeb:
  709. case llvm::Triple::hexagon:
  710. case llvm::Triple::le32:
  711. case llvm::Triple::mips:
  712. case llvm::Triple::mipsel:
  713. case llvm::Triple::nvptx:
  714. case llvm::Triple::ppc:
  715. case llvm::Triple::r600:
  716. case llvm::Triple::sparc:
  717. case llvm::Triple::tce:
  718. case llvm::Triple::thumb:
  719. case llvm::Triple::thumbeb:
  720. case llvm::Triple::x86:
  721. case llvm::Triple::xcore:
  722. case llvm::Triple::spir:
  723. case llvm::Triple::kalimba:
  724. return 32;
  725. case llvm::Triple::aarch64:
  726. case llvm::Triple::aarch64_be:
  727. case llvm::Triple::mips64:
  728. case llvm::Triple::mips64el:
  729. case llvm::Triple::nvptx64:
  730. case llvm::Triple::ppc64:
  731. case llvm::Triple::ppc64le:
  732. case llvm::Triple::sparcv9:
  733. case llvm::Triple::systemz:
  734. case llvm::Triple::x86_64:
  735. case llvm::Triple::spir64:
  736. return 64;
  737. }
  738. llvm_unreachable("Invalid architecture value");
  739. }
  740. bool Triple::isArch64Bit() const {
  741. return getArchPointerBitWidth(getArch()) == 64;
  742. }
  743. bool Triple::isArch32Bit() const {
  744. return getArchPointerBitWidth(getArch()) == 32;
  745. }
  746. bool Triple::isArch16Bit() const {
  747. return getArchPointerBitWidth(getArch()) == 16;
  748. }
  749. Triple Triple::get32BitArchVariant() const {
  750. Triple T(*this);
  751. switch (getArch()) {
  752. case Triple::UnknownArch:
  753. case Triple::aarch64:
  754. case Triple::aarch64_be:
  755. case Triple::msp430:
  756. case Triple::systemz:
  757. case Triple::ppc64le:
  758. T.setArch(UnknownArch);
  759. break;
  760. case Triple::amdil:
  761. case Triple::spir:
  762. case Triple::arm:
  763. case Triple::armeb:
  764. case Triple::hexagon:
  765. case Triple::kalimba:
  766. case Triple::le32:
  767. case Triple::mips:
  768. case Triple::mipsel:
  769. case Triple::nvptx:
  770. case Triple::ppc:
  771. case Triple::r600:
  772. case Triple::sparc:
  773. case Triple::tce:
  774. case Triple::thumb:
  775. case Triple::thumbeb:
  776. case Triple::x86:
  777. case Triple::xcore:
  778. // Already 32-bit.
  779. break;
  780. case Triple::mips64: T.setArch(Triple::mips); break;
  781. case Triple::mips64el: T.setArch(Triple::mipsel); break;
  782. case Triple::nvptx64: T.setArch(Triple::nvptx); break;
  783. case Triple::ppc64: T.setArch(Triple::ppc); break;
  784. case Triple::sparcv9: T.setArch(Triple::sparc); break;
  785. case Triple::x86_64: T.setArch(Triple::x86); break;
  786. case Triple::spir64: T.setArch(Triple::spir); break;
  787. }
  788. return T;
  789. }
  790. Triple Triple::get64BitArchVariant() const {
  791. Triple T(*this);
  792. switch (getArch()) {
  793. case Triple::UnknownArch:
  794. case Triple::amdil:
  795. case Triple::arm:
  796. case Triple::armeb:
  797. case Triple::hexagon:
  798. case Triple::kalimba:
  799. case Triple::le32:
  800. case Triple::msp430:
  801. case Triple::r600:
  802. case Triple::tce:
  803. case Triple::thumb:
  804. case Triple::thumbeb:
  805. case Triple::xcore:
  806. T.setArch(UnknownArch);
  807. break;
  808. case Triple::aarch64:
  809. case Triple::aarch64_be:
  810. case Triple::spir64:
  811. case Triple::mips64:
  812. case Triple::mips64el:
  813. case Triple::nvptx64:
  814. case Triple::ppc64:
  815. case Triple::ppc64le:
  816. case Triple::sparcv9:
  817. case Triple::systemz:
  818. case Triple::x86_64:
  819. // Already 64-bit.
  820. break;
  821. case Triple::mips: T.setArch(Triple::mips64); break;
  822. case Triple::mipsel: T.setArch(Triple::mips64el); break;
  823. case Triple::nvptx: T.setArch(Triple::nvptx64); break;
  824. case Triple::ppc: T.setArch(Triple::ppc64); break;
  825. case Triple::sparc: T.setArch(Triple::sparcv9); break;
  826. case Triple::x86: T.setArch(Triple::x86_64); break;
  827. case Triple::spir: T.setArch(Triple::spir64); break;
  828. }
  829. return T;
  830. }
  831. // FIXME: tblgen this.
  832. const char *Triple::getARMCPUForArch(StringRef MArch) const {
  833. if (MArch.empty())
  834. MArch = getArchName();
  835. switch (getOS()) {
  836. case llvm::Triple::NetBSD:
  837. if (MArch == "armv6")
  838. return "arm1176jzf-s";
  839. break;
  840. case llvm::Triple::Win32:
  841. // FIXME: this is invalid for WindowsCE
  842. return "cortex-a9";
  843. default:
  844. break;
  845. }
  846. const char *result = nullptr;
  847. size_t offset = StringRef::npos;
  848. if (MArch.startswith("arm"))
  849. offset = 3;
  850. if (MArch.startswith("thumb"))
  851. offset = 5;
  852. if (offset != StringRef::npos && MArch.substr(offset, 2) == "eb")
  853. offset += 2;
  854. if (offset != StringRef::npos)
  855. result = llvm::StringSwitch<const char *>(MArch.substr(offset))
  856. .Cases("v2", "v2a", "arm2")
  857. .Case("v3", "arm6")
  858. .Case("v3m", "arm7m")
  859. .Case("v4", "strongarm")
  860. .Case("v4t", "arm7tdmi")
  861. .Cases("v5", "v5t", "arm10tdmi")
  862. .Cases("v5e", "v5te", "arm1022e")
  863. .Case("v5tej", "arm926ej-s")
  864. .Cases("v6", "v6k", "arm1136jf-s")
  865. .Case("v6j", "arm1136j-s")
  866. .Cases("v6z", "v6zk", "arm1176jzf-s")
  867. .Case("v6t2", "arm1156t2-s")
  868. .Cases("v6m", "v6-m", "cortex-m0")
  869. .Cases("v7", "v7a", "v7-a", "v7l", "v7-l", "cortex-a8")
  870. .Cases("v7s", "v7-s", "swift")
  871. .Cases("v7r", "v7-r", "cortex-r4")
  872. .Cases("v7m", "v7-m", "cortex-m3")
  873. .Cases("v7em", "v7e-m", "cortex-m4")
  874. .Cases("v8", "v8a", "v8-a", "cortex-a53")
  875. .Default(nullptr);
  876. else
  877. result = llvm::StringSwitch<const char *>(MArch)
  878. .Case("ep9312", "ep9312")
  879. .Case("iwmmxt", "iwmmxt")
  880. .Case("xscale", "xscale")
  881. .Default(nullptr);
  882. if (result)
  883. return result;
  884. // If all else failed, return the most base CPU with thumb interworking
  885. // supported by LLVM.
  886. // FIXME: Should warn once that we're falling back.
  887. switch (getOS()) {
  888. case llvm::Triple::NetBSD:
  889. switch (getEnvironment()) {
  890. case llvm::Triple::GNUEABIHF:
  891. case llvm::Triple::GNUEABI:
  892. case llvm::Triple::EABIHF:
  893. case llvm::Triple::EABI:
  894. return "arm926ej-s";
  895. default:
  896. return "strongarm";
  897. }
  898. default:
  899. switch (getEnvironment()) {
  900. case llvm::Triple::EABIHF:
  901. case llvm::Triple::GNUEABIHF:
  902. return "arm1176jzf-s";
  903. default:
  904. return "arm7tdmi";
  905. }
  906. }
  907. }