PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Support/Triple.cpp

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