Triple.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  1. //===--- Triple.cpp - Target triple helper class --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/TargetParser/Triple.h"
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/ADT/StringSwitch.h"
  12. #include "llvm/Support/ErrorHandling.h"
  13. #include "llvm/Support/SwapByteOrder.h"
  14. #include "llvm/Support/VersionTuple.h"
  15. #include "llvm/TargetParser/ARMTargetParser.h"
  16. #include "llvm/TargetParser/ARMTargetParserCommon.h"
  17. #include "llvm/TargetParser/Host.h"
  18. #include <cassert>
  19. #include <cstring>
  20. using namespace llvm;
  21. StringRef Triple::getArchTypeName(ArchType Kind) {
  22. switch (Kind) {
  23. case UnknownArch: return "unknown";
  24. case aarch64: return "aarch64";
  25. case aarch64_32: return "aarch64_32";
  26. case aarch64_be: return "aarch64_be";
  27. case amdgcn: return "amdgcn";
  28. case amdil64: return "amdil64";
  29. case amdil: return "amdil";
  30. case arc: return "arc";
  31. case arm: return "arm";
  32. case armeb: return "armeb";
  33. case avr: return "avr";
  34. case bpfeb: return "bpfeb";
  35. case bpfel: return "bpfel";
  36. case csky: return "csky";
  37. case dxil: return "dxil";
  38. case hexagon: return "hexagon";
  39. case hsail64: return "hsail64";
  40. case hsail: return "hsail";
  41. case kalimba: return "kalimba";
  42. case lanai: return "lanai";
  43. case le32: return "le32";
  44. case le64: return "le64";
  45. case loongarch32: return "loongarch32";
  46. case loongarch64: return "loongarch64";
  47. case m68k: return "m68k";
  48. case mips64: return "mips64";
  49. case mips64el: return "mips64el";
  50. case mips: return "mips";
  51. case mipsel: return "mipsel";
  52. case msp430: return "msp430";
  53. case nvptx64: return "nvptx64";
  54. case nvptx: return "nvptx";
  55. case ppc64: return "powerpc64";
  56. case ppc64le: return "powerpc64le";
  57. case ppc: return "powerpc";
  58. case ppcle: return "powerpcle";
  59. case r600: return "r600";
  60. case renderscript32: return "renderscript32";
  61. case renderscript64: return "renderscript64";
  62. case riscv32: return "riscv32";
  63. case riscv64: return "riscv64";
  64. case shave: return "shave";
  65. case sparc: return "sparc";
  66. case sparcel: return "sparcel";
  67. case sparcv9: return "sparcv9";
  68. case spir64: return "spir64";
  69. case spir: return "spir";
  70. case spirv32: return "spirv32";
  71. case spirv64: return "spirv64";
  72. case systemz: return "s390x";
  73. case tce: return "tce";
  74. case tcele: return "tcele";
  75. case thumb: return "thumb";
  76. case thumbeb: return "thumbeb";
  77. case ve: return "ve";
  78. case wasm32: return "wasm32";
  79. case wasm64: return "wasm64";
  80. case x86: return "i386";
  81. case x86_64: return "x86_64";
  82. case xcore: return "xcore";
  83. case xtensa: return "xtensa";
  84. }
  85. llvm_unreachable("Invalid ArchType!");
  86. }
  87. StringRef Triple::getArchTypePrefix(ArchType Kind) {
  88. switch (Kind) {
  89. default:
  90. return StringRef();
  91. case aarch64:
  92. case aarch64_be:
  93. case aarch64_32: return "aarch64";
  94. case arc: return "arc";
  95. case arm:
  96. case armeb:
  97. case thumb:
  98. case thumbeb: return "arm";
  99. case avr: return "avr";
  100. case ppc64:
  101. case ppc64le:
  102. case ppc:
  103. case ppcle: return "ppc";
  104. case m68k: return "m68k";
  105. case mips:
  106. case mipsel:
  107. case mips64:
  108. case mips64el: return "mips";
  109. case hexagon: return "hexagon";
  110. case amdgcn: return "amdgcn";
  111. case r600: return "r600";
  112. case bpfel:
  113. case bpfeb: return "bpf";
  114. case sparcv9:
  115. case sparcel:
  116. case sparc: return "sparc";
  117. case systemz: return "s390";
  118. case x86:
  119. case x86_64: return "x86";
  120. case xcore: return "xcore";
  121. // NVPTX intrinsics are namespaced under nvvm.
  122. case nvptx: return "nvvm";
  123. case nvptx64: return "nvvm";
  124. case le32: return "le32";
  125. case le64: return "le64";
  126. case amdil:
  127. case amdil64: return "amdil";
  128. case hsail:
  129. case hsail64: return "hsail";
  130. case spir:
  131. case spir64: return "spir";
  132. case spirv32:
  133. case spirv64: return "spirv";
  134. case kalimba: return "kalimba";
  135. case lanai: return "lanai";
  136. case shave: return "shave";
  137. case wasm32:
  138. case wasm64: return "wasm";
  139. case riscv32:
  140. case riscv64: return "riscv";
  141. case ve: return "ve";
  142. case csky: return "csky";
  143. case loongarch32:
  144. case loongarch64: return "loongarch";
  145. case dxil: return "dx";
  146. case xtensa: return "xtensa";
  147. }
  148. }
  149. StringRef Triple::getVendorTypeName(VendorType Kind) {
  150. switch (Kind) {
  151. case UnknownVendor: return "unknown";
  152. case AMD: return "amd";
  153. case Apple: return "apple";
  154. case CSR: return "csr";
  155. case Freescale: return "fsl";
  156. case IBM: return "ibm";
  157. case ImaginationTechnologies: return "img";
  158. case Mesa: return "mesa";
  159. case MipsTechnologies: return "mti";
  160. case Myriad: return "myriad";
  161. case NVIDIA: return "nvidia";
  162. case OpenEmbedded: return "oe";
  163. case PC: return "pc";
  164. case SCEI: return "scei";
  165. case SUSE: return "suse";
  166. }
  167. llvm_unreachable("Invalid VendorType!");
  168. }
  169. StringRef Triple::getOSTypeName(OSType Kind) {
  170. switch (Kind) {
  171. case UnknownOS: return "unknown";
  172. case AIX: return "aix";
  173. case AMDHSA: return "amdhsa";
  174. case AMDPAL: return "amdpal";
  175. case Ananas: return "ananas";
  176. case CUDA: return "cuda";
  177. case CloudABI: return "cloudabi";
  178. case Contiki: return "contiki";
  179. case Darwin: return "darwin";
  180. case DragonFly: return "dragonfly";
  181. case DriverKit: return "driverkit";
  182. case ELFIAMCU: return "elfiamcu";
  183. case Emscripten: return "emscripten";
  184. case FreeBSD: return "freebsd";
  185. case Fuchsia: return "fuchsia";
  186. case Haiku: return "haiku";
  187. case HermitCore: return "hermit";
  188. case Hurd: return "hurd";
  189. case IOS: return "ios";
  190. case KFreeBSD: return "kfreebsd";
  191. case Linux: return "linux";
  192. case Lv2: return "lv2";
  193. case MacOSX: return "macosx";
  194. case Mesa3D: return "mesa3d";
  195. case Minix: return "minix";
  196. case NVCL: return "nvcl";
  197. case NaCl: return "nacl";
  198. case NetBSD: return "netbsd";
  199. case OpenBSD: return "openbsd";
  200. case PS4: return "ps4";
  201. case PS5: return "ps5";
  202. case RTEMS: return "rtems";
  203. case Solaris: return "solaris";
  204. case TvOS: return "tvos";
  205. case WASI: return "wasi";
  206. case WatchOS: return "watchos";
  207. case Win32: return "windows";
  208. case ZOS: return "zos";
  209. case ShaderModel: return "shadermodel";
  210. }
  211. llvm_unreachable("Invalid OSType");
  212. }
  213. StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
  214. switch (Kind) {
  215. case UnknownEnvironment: return "unknown";
  216. case Android: return "android";
  217. case CODE16: return "code16";
  218. case CoreCLR: return "coreclr";
  219. case Cygnus: return "cygnus";
  220. case EABI: return "eabi";
  221. case EABIHF: return "eabihf";
  222. case GNU: return "gnu";
  223. case GNUABI64: return "gnuabi64";
  224. case GNUABIN32: return "gnuabin32";
  225. case GNUEABI: return "gnueabi";
  226. case GNUEABIHF: return "gnueabihf";
  227. case GNUF32: return "gnuf32";
  228. case GNUF64: return "gnuf64";
  229. case GNUSF: return "gnusf";
  230. case GNUX32: return "gnux32";
  231. case GNUILP32: return "gnu_ilp32";
  232. case Itanium: return "itanium";
  233. case MSVC: return "msvc";
  234. case MacABI: return "macabi";
  235. case Musl: return "musl";
  236. case MuslEABI: return "musleabi";
  237. case MuslEABIHF: return "musleabihf";
  238. case MuslX32: return "muslx32";
  239. case Simulator: return "simulator";
  240. case Pixel: return "pixel";
  241. case Vertex: return "vertex";
  242. case Geometry: return "geometry";
  243. case Hull: return "hull";
  244. case Domain: return "domain";
  245. case Compute: return "compute";
  246. case Library: return "library";
  247. case RayGeneration: return "raygeneration";
  248. case Intersection: return "intersection";
  249. case AnyHit: return "anyhit";
  250. case ClosestHit: return "closesthit";
  251. case Miss: return "miss";
  252. case Callable: return "callable";
  253. case Mesh: return "mesh";
  254. case Amplification: return "amplification";
  255. }
  256. llvm_unreachable("Invalid EnvironmentType!");
  257. }
  258. static Triple::ArchType parseBPFArch(StringRef ArchName) {
  259. if (ArchName.equals("bpf")) {
  260. if (sys::IsLittleEndianHost)
  261. return Triple::bpfel;
  262. else
  263. return Triple::bpfeb;
  264. } else if (ArchName.equals("bpf_be") || ArchName.equals("bpfeb")) {
  265. return Triple::bpfeb;
  266. } else if (ArchName.equals("bpf_le") || ArchName.equals("bpfel")) {
  267. return Triple::bpfel;
  268. } else {
  269. return Triple::UnknownArch;
  270. }
  271. }
  272. Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
  273. Triple::ArchType BPFArch(parseBPFArch(Name));
  274. return StringSwitch<Triple::ArchType>(Name)
  275. .Case("aarch64", aarch64)
  276. .Case("aarch64_be", aarch64_be)
  277. .Case("aarch64_32", aarch64_32)
  278. .Case("arc", arc)
  279. .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
  280. .Case("arm64_32", aarch64_32)
  281. .Case("arm", arm)
  282. .Case("armeb", armeb)
  283. .Case("avr", avr)
  284. .StartsWith("bpf", BPFArch)
  285. .Case("m68k", m68k)
  286. .Case("mips", mips)
  287. .Case("mipsel", mipsel)
  288. .Case("mips64", mips64)
  289. .Case("mips64el", mips64el)
  290. .Case("msp430", msp430)
  291. .Case("ppc64", ppc64)
  292. .Case("ppc32", ppc)
  293. .Case("ppc", ppc)
  294. .Case("ppc32le", ppcle)
  295. .Case("ppcle", ppcle)
  296. .Case("ppc64le", ppc64le)
  297. .Case("r600", r600)
  298. .Case("amdgcn", amdgcn)
  299. .Case("riscv32", riscv32)
  300. .Case("riscv64", riscv64)
  301. .Case("hexagon", hexagon)
  302. .Case("sparc", sparc)
  303. .Case("sparcel", sparcel)
  304. .Case("sparcv9", sparcv9)
  305. .Case("s390x", systemz)
  306. .Case("systemz", systemz)
  307. .Case("tce", tce)
  308. .Case("tcele", tcele)
  309. .Case("thumb", thumb)
  310. .Case("thumbeb", thumbeb)
  311. .Case("x86", x86)
  312. .Case("i386", x86)
  313. .Case("x86-64", x86_64)
  314. .Case("xcore", xcore)
  315. .Case("nvptx", nvptx)
  316. .Case("nvptx64", nvptx64)
  317. .Case("le32", le32)
  318. .Case("le64", le64)
  319. .Case("amdil", amdil)
  320. .Case("amdil64", amdil64)
  321. .Case("hsail", hsail)
  322. .Case("hsail64", hsail64)
  323. .Case("spir", spir)
  324. .Case("spir64", spir64)
  325. .Case("spirv32", spirv32)
  326. .Case("spirv64", spirv64)
  327. .Case("kalimba", kalimba)
  328. .Case("lanai", lanai)
  329. .Case("shave", shave)
  330. .Case("wasm32", wasm32)
  331. .Case("wasm64", wasm64)
  332. .Case("renderscript32", renderscript32)
  333. .Case("renderscript64", renderscript64)
  334. .Case("ve", ve)
  335. .Case("csky", csky)
  336. .Case("loongarch32", loongarch32)
  337. .Case("loongarch64", loongarch64)
  338. .Case("dxil", dxil)
  339. .Case("xtensa", xtensa)
  340. .Default(UnknownArch);
  341. }
  342. static Triple::ArchType parseARMArch(StringRef ArchName) {
  343. ARM::ISAKind ISA = ARM::parseArchISA(ArchName);
  344. ARM::EndianKind ENDIAN = ARM::parseArchEndian(ArchName);
  345. Triple::ArchType arch = Triple::UnknownArch;
  346. switch (ENDIAN) {
  347. case ARM::EndianKind::LITTLE: {
  348. switch (ISA) {
  349. case ARM::ISAKind::ARM:
  350. arch = Triple::arm;
  351. break;
  352. case ARM::ISAKind::THUMB:
  353. arch = Triple::thumb;
  354. break;
  355. case ARM::ISAKind::AARCH64:
  356. arch = Triple::aarch64;
  357. break;
  358. case ARM::ISAKind::INVALID:
  359. break;
  360. }
  361. break;
  362. }
  363. case ARM::EndianKind::BIG: {
  364. switch (ISA) {
  365. case ARM::ISAKind::ARM:
  366. arch = Triple::armeb;
  367. break;
  368. case ARM::ISAKind::THUMB:
  369. arch = Triple::thumbeb;
  370. break;
  371. case ARM::ISAKind::AARCH64:
  372. arch = Triple::aarch64_be;
  373. break;
  374. case ARM::ISAKind::INVALID:
  375. break;
  376. }
  377. break;
  378. }
  379. case ARM::EndianKind::INVALID: {
  380. break;
  381. }
  382. }
  383. ArchName = ARM::getCanonicalArchName(ArchName);
  384. if (ArchName.empty())
  385. return Triple::UnknownArch;
  386. // Thumb only exists in v4+
  387. if (ISA == ARM::ISAKind::THUMB &&
  388. (ArchName.startswith("v2") || ArchName.startswith("v3")))
  389. return Triple::UnknownArch;
  390. // Thumb only for v6m
  391. ARM::ProfileKind Profile = ARM::parseArchProfile(ArchName);
  392. unsigned Version = ARM::parseArchVersion(ArchName);
  393. if (Profile == ARM::ProfileKind::M && Version == 6) {
  394. if (ENDIAN == ARM::EndianKind::BIG)
  395. return Triple::thumbeb;
  396. else
  397. return Triple::thumb;
  398. }
  399. return arch;
  400. }
  401. static Triple::ArchType parseArch(StringRef ArchName) {
  402. auto AT = StringSwitch<Triple::ArchType>(ArchName)
  403. .Cases("i386", "i486", "i586", "i686", Triple::x86)
  404. // FIXME: Do we need to support these?
  405. .Cases("i786", "i886", "i986", Triple::x86)
  406. .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
  407. .Cases("powerpc", "powerpcspe", "ppc", "ppc32", Triple::ppc)
  408. .Cases("powerpcle", "ppcle", "ppc32le", Triple::ppcle)
  409. .Cases("powerpc64", "ppu", "ppc64", Triple::ppc64)
  410. .Cases("powerpc64le", "ppc64le", Triple::ppc64le)
  411. .Case("xscale", Triple::arm)
  412. .Case("xscaleeb", Triple::armeb)
  413. .Case("aarch64", Triple::aarch64)
  414. .Case("aarch64_be", Triple::aarch64_be)
  415. .Case("aarch64_32", Triple::aarch64_32)
  416. .Case("arc", Triple::arc)
  417. .Case("arm64", Triple::aarch64)
  418. .Case("arm64_32", Triple::aarch64_32)
  419. .Case("arm64e", Triple::aarch64)
  420. .Case("arm64ec", Triple::aarch64)
  421. .Case("arm", Triple::arm)
  422. .Case("armeb", Triple::armeb)
  423. .Case("thumb", Triple::thumb)
  424. .Case("thumbeb", Triple::thumbeb)
  425. .Case("avr", Triple::avr)
  426. .Case("m68k", Triple::m68k)
  427. .Case("msp430", Triple::msp430)
  428. .Cases("mips", "mipseb", "mipsallegrex", "mipsisa32r6",
  429. "mipsr6", Triple::mips)
  430. .Cases("mipsel", "mipsallegrexel", "mipsisa32r6el", "mipsr6el",
  431. Triple::mipsel)
  432. .Cases("mips64", "mips64eb", "mipsn32", "mipsisa64r6",
  433. "mips64r6", "mipsn32r6", Triple::mips64)
  434. .Cases("mips64el", "mipsn32el", "mipsisa64r6el", "mips64r6el",
  435. "mipsn32r6el", Triple::mips64el)
  436. .Case("r600", Triple::r600)
  437. .Case("amdgcn", Triple::amdgcn)
  438. .Case("riscv32", Triple::riscv32)
  439. .Case("riscv64", Triple::riscv64)
  440. .Case("hexagon", Triple::hexagon)
  441. .Cases("s390x", "systemz", Triple::systemz)
  442. .Case("sparc", Triple::sparc)
  443. .Case("sparcel", Triple::sparcel)
  444. .Cases("sparcv9", "sparc64", Triple::sparcv9)
  445. .Case("tce", Triple::tce)
  446. .Case("tcele", Triple::tcele)
  447. .Case("xcore", Triple::xcore)
  448. .Case("nvptx", Triple::nvptx)
  449. .Case("nvptx64", Triple::nvptx64)
  450. .Case("le32", Triple::le32)
  451. .Case("le64", Triple::le64)
  452. .Case("amdil", Triple::amdil)
  453. .Case("amdil64", Triple::amdil64)
  454. .Case("hsail", Triple::hsail)
  455. .Case("hsail64", Triple::hsail64)
  456. .Case("spir", Triple::spir)
  457. .Case("spir64", Triple::spir64)
  458. .Cases("spirv32", "spirv32v1.0", "spirv32v1.1", "spirv32v1.2",
  459. "spirv32v1.3", "spirv32v1.4", "spirv32v1.5", Triple::spirv32)
  460. .Cases("spirv64", "spirv64v1.0", "spirv64v1.1", "spirv64v1.2",
  461. "spirv64v1.3", "spirv64v1.4", "spirv64v1.5", Triple::spirv64)
  462. .StartsWith("kalimba", Triple::kalimba)
  463. .Case("lanai", Triple::lanai)
  464. .Case("renderscript32", Triple::renderscript32)
  465. .Case("renderscript64", Triple::renderscript64)
  466. .Case("shave", Triple::shave)
  467. .Case("ve", Triple::ve)
  468. .Case("wasm32", Triple::wasm32)
  469. .Case("wasm64", Triple::wasm64)
  470. .Case("csky", Triple::csky)
  471. .Case("loongarch32", Triple::loongarch32)
  472. .Case("loongarch64", Triple::loongarch64)
  473. .Case("dxil", Triple::dxil)
  474. .Case("xtensa", Triple::xtensa)
  475. .Default(Triple::UnknownArch);
  476. // Some architectures require special parsing logic just to compute the
  477. // ArchType result.
  478. if (AT == Triple::UnknownArch) {
  479. if (ArchName.startswith("arm") || ArchName.startswith("thumb") ||
  480. ArchName.startswith("aarch64"))
  481. return parseARMArch(ArchName);
  482. if (ArchName.startswith("bpf"))
  483. return parseBPFArch(ArchName);
  484. }
  485. return AT;
  486. }
  487. static Triple::VendorType parseVendor(StringRef VendorName) {
  488. return StringSwitch<Triple::VendorType>(VendorName)
  489. .Case("apple", Triple::Apple)
  490. .Case("pc", Triple::PC)
  491. .Case("scei", Triple::SCEI)
  492. .Case("sie", Triple::SCEI)
  493. .Case("fsl", Triple::Freescale)
  494. .Case("ibm", Triple::IBM)
  495. .Case("img", Triple::ImaginationTechnologies)
  496. .Case("mti", Triple::MipsTechnologies)
  497. .Case("nvidia", Triple::NVIDIA)
  498. .Case("csr", Triple::CSR)
  499. .Case("myriad", Triple::Myriad)
  500. .Case("amd", Triple::AMD)
  501. .Case("mesa", Triple::Mesa)
  502. .Case("suse", Triple::SUSE)
  503. .Case("oe", Triple::OpenEmbedded)
  504. .Default(Triple::UnknownVendor);
  505. }
  506. static Triple::OSType parseOS(StringRef OSName) {
  507. return StringSwitch<Triple::OSType>(OSName)
  508. .StartsWith("ananas", Triple::Ananas)
  509. .StartsWith("cloudabi", Triple::CloudABI)
  510. .StartsWith("darwin", Triple::Darwin)
  511. .StartsWith("dragonfly", Triple::DragonFly)
  512. .StartsWith("freebsd", Triple::FreeBSD)
  513. .StartsWith("fuchsia", Triple::Fuchsia)
  514. .StartsWith("ios", Triple::IOS)
  515. .StartsWith("kfreebsd", Triple::KFreeBSD)
  516. .StartsWith("linux", Triple::Linux)
  517. .StartsWith("lv2", Triple::Lv2)
  518. .StartsWith("macos", Triple::MacOSX)
  519. .StartsWith("netbsd", Triple::NetBSD)
  520. .StartsWith("openbsd", Triple::OpenBSD)
  521. .StartsWith("solaris", Triple::Solaris)
  522. .StartsWith("win32", Triple::Win32)
  523. .StartsWith("windows", Triple::Win32)
  524. .StartsWith("zos", Triple::ZOS)
  525. .StartsWith("haiku", Triple::Haiku)
  526. .StartsWith("minix", Triple::Minix)
  527. .StartsWith("rtems", Triple::RTEMS)
  528. .StartsWith("nacl", Triple::NaCl)
  529. .StartsWith("aix", Triple::AIX)
  530. .StartsWith("cuda", Triple::CUDA)
  531. .StartsWith("nvcl", Triple::NVCL)
  532. .StartsWith("amdhsa", Triple::AMDHSA)
  533. .StartsWith("ps4", Triple::PS4)
  534. .StartsWith("ps5", Triple::PS5)
  535. .StartsWith("elfiamcu", Triple::ELFIAMCU)
  536. .StartsWith("tvos", Triple::TvOS)
  537. .StartsWith("watchos", Triple::WatchOS)
  538. .StartsWith("driverkit", Triple::DriverKit)
  539. .StartsWith("mesa3d", Triple::Mesa3D)
  540. .StartsWith("contiki", Triple::Contiki)
  541. .StartsWith("amdpal", Triple::AMDPAL)
  542. .StartsWith("hermit", Triple::HermitCore)
  543. .StartsWith("hurd", Triple::Hurd)
  544. .StartsWith("wasi", Triple::WASI)
  545. .StartsWith("emscripten", Triple::Emscripten)
  546. .StartsWith("shadermodel", Triple::ShaderModel)
  547. .Default(Triple::UnknownOS);
  548. }
  549. static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
  550. return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
  551. .StartsWith("eabihf", Triple::EABIHF)
  552. .StartsWith("eabi", Triple::EABI)
  553. .StartsWith("gnuabin32", Triple::GNUABIN32)
  554. .StartsWith("gnuabi64", Triple::GNUABI64)
  555. .StartsWith("gnueabihf", Triple::GNUEABIHF)
  556. .StartsWith("gnueabi", Triple::GNUEABI)
  557. .StartsWith("gnuf32", Triple::GNUF32)
  558. .StartsWith("gnuf64", Triple::GNUF64)
  559. .StartsWith("gnusf", Triple::GNUSF)
  560. .StartsWith("gnux32", Triple::GNUX32)
  561. .StartsWith("gnu_ilp32", Triple::GNUILP32)
  562. .StartsWith("code16", Triple::CODE16)
  563. .StartsWith("gnu", Triple::GNU)
  564. .StartsWith("android", Triple::Android)
  565. .StartsWith("musleabihf", Triple::MuslEABIHF)
  566. .StartsWith("musleabi", Triple::MuslEABI)
  567. .StartsWith("muslx32", Triple::MuslX32)
  568. .StartsWith("musl", Triple::Musl)
  569. .StartsWith("msvc", Triple::MSVC)
  570. .StartsWith("itanium", Triple::Itanium)
  571. .StartsWith("cygnus", Triple::Cygnus)
  572. .StartsWith("coreclr", Triple::CoreCLR)
  573. .StartsWith("simulator", Triple::Simulator)
  574. .StartsWith("macabi", Triple::MacABI)
  575. .StartsWith("pixel", Triple::Pixel)
  576. .StartsWith("vertex", Triple::Vertex)
  577. .StartsWith("geometry", Triple::Geometry)
  578. .StartsWith("hull", Triple::Hull)
  579. .StartsWith("domain", Triple::Domain)
  580. .StartsWith("compute", Triple::Compute)
  581. .StartsWith("library", Triple::Library)
  582. .StartsWith("raygeneration", Triple::RayGeneration)
  583. .StartsWith("intersection", Triple::Intersection)
  584. .StartsWith("anyhit", Triple::AnyHit)
  585. .StartsWith("closesthit", Triple::ClosestHit)
  586. .StartsWith("miss", Triple::Miss)
  587. .StartsWith("callable", Triple::Callable)
  588. .StartsWith("mesh", Triple::Mesh)
  589. .StartsWith("amplification", Triple::Amplification)
  590. .Default(Triple::UnknownEnvironment);
  591. }
  592. static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
  593. return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
  594. // "xcoff" must come before "coff" because of the order-dependendent
  595. // pattern matching.
  596. .EndsWith("xcoff", Triple::XCOFF)
  597. .EndsWith("coff", Triple::COFF)
  598. .EndsWith("elf", Triple::ELF)
  599. .EndsWith("goff", Triple::GOFF)
  600. .EndsWith("macho", Triple::MachO)
  601. .EndsWith("wasm", Triple::Wasm)
  602. .EndsWith("spirv", Triple::SPIRV)
  603. .Default(Triple::UnknownObjectFormat);
  604. }
  605. static Triple::SubArchType parseSubArch(StringRef SubArchName) {
  606. if (SubArchName.startswith("mips") &&
  607. (SubArchName.endswith("r6el") || SubArchName.endswith("r6")))
  608. return Triple::MipsSubArch_r6;
  609. if (SubArchName == "powerpcspe")
  610. return Triple::PPCSubArch_spe;
  611. if (SubArchName == "arm64e")
  612. return Triple::AArch64SubArch_arm64e;
  613. if (SubArchName == "arm64ec")
  614. return Triple::AArch64SubArch_arm64ec;
  615. if (SubArchName.startswith("spirv"))
  616. return StringSwitch<Triple::SubArchType>(SubArchName)
  617. .EndsWith("v1.0", Triple::SPIRVSubArch_v10)
  618. .EndsWith("v1.1", Triple::SPIRVSubArch_v11)
  619. .EndsWith("v1.2", Triple::SPIRVSubArch_v12)
  620. .EndsWith("v1.3", Triple::SPIRVSubArch_v13)
  621. .EndsWith("v1.4", Triple::SPIRVSubArch_v14)
  622. .EndsWith("v1.5", Triple::SPIRVSubArch_v15)
  623. .Default(Triple::NoSubArch);
  624. StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
  625. // For now, this is the small part. Early return.
  626. if (ARMSubArch.empty())
  627. return StringSwitch<Triple::SubArchType>(SubArchName)
  628. .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
  629. .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
  630. .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
  631. .Default(Triple::NoSubArch);
  632. // ARM sub arch.
  633. switch(ARM::parseArch(ARMSubArch)) {
  634. case ARM::ArchKind::ARMV4:
  635. return Triple::NoSubArch;
  636. case ARM::ArchKind::ARMV4T:
  637. return Triple::ARMSubArch_v4t;
  638. case ARM::ArchKind::ARMV5T:
  639. return Triple::ARMSubArch_v5;
  640. case ARM::ArchKind::ARMV5TE:
  641. case ARM::ArchKind::IWMMXT:
  642. case ARM::ArchKind::IWMMXT2:
  643. case ARM::ArchKind::XSCALE:
  644. case ARM::ArchKind::ARMV5TEJ:
  645. return Triple::ARMSubArch_v5te;
  646. case ARM::ArchKind::ARMV6:
  647. return Triple::ARMSubArch_v6;
  648. case ARM::ArchKind::ARMV6K:
  649. case ARM::ArchKind::ARMV6KZ:
  650. return Triple::ARMSubArch_v6k;
  651. case ARM::ArchKind::ARMV6T2:
  652. return Triple::ARMSubArch_v6t2;
  653. case ARM::ArchKind::ARMV6M:
  654. return Triple::ARMSubArch_v6m;
  655. case ARM::ArchKind::ARMV7A:
  656. case ARM::ArchKind::ARMV7R:
  657. return Triple::ARMSubArch_v7;
  658. case ARM::ArchKind::ARMV7VE:
  659. return Triple::ARMSubArch_v7ve;
  660. case ARM::ArchKind::ARMV7K:
  661. return Triple::ARMSubArch_v7k;
  662. case ARM::ArchKind::ARMV7M:
  663. return Triple::ARMSubArch_v7m;
  664. case ARM::ArchKind::ARMV7S:
  665. return Triple::ARMSubArch_v7s;
  666. case ARM::ArchKind::ARMV7EM:
  667. return Triple::ARMSubArch_v7em;
  668. case ARM::ArchKind::ARMV8A:
  669. return Triple::ARMSubArch_v8;
  670. case ARM::ArchKind::ARMV8_1A:
  671. return Triple::ARMSubArch_v8_1a;
  672. case ARM::ArchKind::ARMV8_2A:
  673. return Triple::ARMSubArch_v8_2a;
  674. case ARM::ArchKind::ARMV8_3A:
  675. return Triple::ARMSubArch_v8_3a;
  676. case ARM::ArchKind::ARMV8_4A:
  677. return Triple::ARMSubArch_v8_4a;
  678. case ARM::ArchKind::ARMV8_5A:
  679. return Triple::ARMSubArch_v8_5a;
  680. case ARM::ArchKind::ARMV8_6A:
  681. return Triple::ARMSubArch_v8_6a;
  682. case ARM::ArchKind::ARMV8_7A:
  683. return Triple::ARMSubArch_v8_7a;
  684. case ARM::ArchKind::ARMV8_8A:
  685. return Triple::ARMSubArch_v8_8a;
  686. case ARM::ArchKind::ARMV8_9A:
  687. return Triple::ARMSubArch_v8_9a;
  688. case ARM::ArchKind::ARMV9A:
  689. return Triple::ARMSubArch_v9;
  690. case ARM::ArchKind::ARMV9_1A:
  691. return Triple::ARMSubArch_v9_1a;
  692. case ARM::ArchKind::ARMV9_2A:
  693. return Triple::ARMSubArch_v9_2a;
  694. case ARM::ArchKind::ARMV9_3A:
  695. return Triple::ARMSubArch_v9_3a;
  696. case ARM::ArchKind::ARMV9_4A:
  697. return Triple::ARMSubArch_v9_4a;
  698. case ARM::ArchKind::ARMV8R:
  699. return Triple::ARMSubArch_v8r;
  700. case ARM::ArchKind::ARMV8MBaseline:
  701. return Triple::ARMSubArch_v8m_baseline;
  702. case ARM::ArchKind::ARMV8MMainline:
  703. return Triple::ARMSubArch_v8m_mainline;
  704. case ARM::ArchKind::ARMV8_1MMainline:
  705. return Triple::ARMSubArch_v8_1m_mainline;
  706. default:
  707. return Triple::NoSubArch;
  708. }
  709. }
  710. static StringRef getObjectFormatTypeName(Triple::ObjectFormatType Kind) {
  711. switch (Kind) {
  712. case Triple::UnknownObjectFormat:
  713. return "";
  714. case Triple::COFF:
  715. return "coff";
  716. case Triple::ELF:
  717. return "elf";
  718. case Triple::GOFF:
  719. return "goff";
  720. case Triple::MachO:
  721. return "macho";
  722. case Triple::Wasm:
  723. return "wasm";
  724. case Triple::XCOFF:
  725. return "xcoff";
  726. case Triple::DXContainer:
  727. return "dxcontainer";
  728. case Triple::SPIRV:
  729. return "spirv";
  730. }
  731. llvm_unreachable("unknown object format type");
  732. }
  733. static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
  734. switch (T.getArch()) {
  735. case Triple::UnknownArch:
  736. case Triple::aarch64:
  737. case Triple::aarch64_32:
  738. case Triple::arm:
  739. case Triple::thumb:
  740. case Triple::x86:
  741. case Triple::x86_64:
  742. if (T.isOSDarwin())
  743. return Triple::MachO;
  744. else if (T.isOSWindows())
  745. return Triple::COFF;
  746. return Triple::ELF;
  747. case Triple::aarch64_be:
  748. case Triple::amdgcn:
  749. case Triple::amdil64:
  750. case Triple::amdil:
  751. case Triple::arc:
  752. case Triple::armeb:
  753. case Triple::avr:
  754. case Triple::bpfeb:
  755. case Triple::bpfel:
  756. case Triple::csky:
  757. case Triple::hexagon:
  758. case Triple::hsail64:
  759. case Triple::hsail:
  760. case Triple::kalimba:
  761. case Triple::lanai:
  762. case Triple::le32:
  763. case Triple::le64:
  764. case Triple::loongarch32:
  765. case Triple::loongarch64:
  766. case Triple::m68k:
  767. case Triple::mips64:
  768. case Triple::mips64el:
  769. case Triple::mips:
  770. case Triple::mipsel:
  771. case Triple::msp430:
  772. case Triple::nvptx64:
  773. case Triple::nvptx:
  774. case Triple::ppc64le:
  775. case Triple::ppcle:
  776. case Triple::r600:
  777. case Triple::renderscript32:
  778. case Triple::renderscript64:
  779. case Triple::riscv32:
  780. case Triple::riscv64:
  781. case Triple::shave:
  782. case Triple::sparc:
  783. case Triple::sparcel:
  784. case Triple::sparcv9:
  785. case Triple::spir64:
  786. case Triple::spir:
  787. case Triple::tce:
  788. case Triple::tcele:
  789. case Triple::thumbeb:
  790. case Triple::ve:
  791. case Triple::xcore:
  792. case Triple::xtensa:
  793. return Triple::ELF;
  794. case Triple::ppc64:
  795. case Triple::ppc:
  796. if (T.isOSAIX())
  797. return Triple::XCOFF;
  798. return Triple::ELF;
  799. case Triple::systemz:
  800. if (T.isOSzOS())
  801. return Triple::GOFF;
  802. return Triple::ELF;
  803. case Triple::wasm32:
  804. case Triple::wasm64:
  805. return Triple::Wasm;
  806. case Triple::spirv32:
  807. case Triple::spirv64:
  808. return Triple::SPIRV;
  809. case Triple::dxil:
  810. return Triple::DXContainer;
  811. }
  812. llvm_unreachable("unknown architecture");
  813. }
  814. /// Construct a triple from the string representation provided.
  815. ///
  816. /// This stores the string representation and parses the various pieces into
  817. /// enum members.
  818. Triple::Triple(const Twine &Str)
  819. : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
  820. Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
  821. ObjectFormat(UnknownObjectFormat) {
  822. // Do minimal parsing by hand here.
  823. SmallVector<StringRef, 4> Components;
  824. StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
  825. if (Components.size() > 0) {
  826. Arch = parseArch(Components[0]);
  827. SubArch = parseSubArch(Components[0]);
  828. if (Components.size() > 1) {
  829. Vendor = parseVendor(Components[1]);
  830. if (Components.size() > 2) {
  831. OS = parseOS(Components[2]);
  832. if (Components.size() > 3) {
  833. Environment = parseEnvironment(Components[3]);
  834. ObjectFormat = parseFormat(Components[3]);
  835. }
  836. }
  837. } else {
  838. Environment =
  839. StringSwitch<Triple::EnvironmentType>(Components[0])
  840. .StartsWith("mipsn32", Triple::GNUABIN32)
  841. .StartsWith("mips64", Triple::GNUABI64)
  842. .StartsWith("mipsisa64", Triple::GNUABI64)
  843. .StartsWith("mipsisa32", Triple::GNU)
  844. .Cases("mips", "mipsel", "mipsr6", "mipsr6el", Triple::GNU)
  845. .Default(UnknownEnvironment);
  846. }
  847. }
  848. if (ObjectFormat == UnknownObjectFormat)
  849. ObjectFormat = getDefaultFormat(*this);
  850. }
  851. /// Construct a triple from string representations of the architecture,
  852. /// vendor, and OS.
  853. ///
  854. /// This joins each argument into a canonical string representation and parses
  855. /// them into enum members. It leaves the environment unknown and omits it from
  856. /// the string representation.
  857. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
  858. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
  859. Arch(parseArch(ArchStr.str())),
  860. SubArch(parseSubArch(ArchStr.str())),
  861. Vendor(parseVendor(VendorStr.str())),
  862. OS(parseOS(OSStr.str())),
  863. Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
  864. ObjectFormat = getDefaultFormat(*this);
  865. }
  866. /// Construct a triple from string representations of the architecture,
  867. /// vendor, OS, and environment.
  868. ///
  869. /// This joins each argument into a canonical string representation and parses
  870. /// them into enum members.
  871. Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
  872. const Twine &EnvironmentStr)
  873. : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
  874. EnvironmentStr).str()),
  875. Arch(parseArch(ArchStr.str())),
  876. SubArch(parseSubArch(ArchStr.str())),
  877. Vendor(parseVendor(VendorStr.str())),
  878. OS(parseOS(OSStr.str())),
  879. Environment(parseEnvironment(EnvironmentStr.str())),
  880. ObjectFormat(parseFormat(EnvironmentStr.str())) {
  881. if (ObjectFormat == Triple::UnknownObjectFormat)
  882. ObjectFormat = getDefaultFormat(*this);
  883. }
  884. std::string Triple::normalize(StringRef Str) {
  885. bool IsMinGW32 = false;
  886. bool IsCygwin = false;
  887. // Parse into components.
  888. SmallVector<StringRef, 4> Components;
  889. Str.split(Components, '-');
  890. // If the first component corresponds to a known architecture, preferentially
  891. // use it for the architecture. If the second component corresponds to a
  892. // known vendor, preferentially use it for the vendor, etc. This avoids silly
  893. // component movement when a component parses as (eg) both a valid arch and a
  894. // valid os.
  895. ArchType Arch = UnknownArch;
  896. if (Components.size() > 0)
  897. Arch = parseArch(Components[0]);
  898. VendorType Vendor = UnknownVendor;
  899. if (Components.size() > 1)
  900. Vendor = parseVendor(Components[1]);
  901. OSType OS = UnknownOS;
  902. if (Components.size() > 2) {
  903. OS = parseOS(Components[2]);
  904. IsCygwin = Components[2].startswith("cygwin");
  905. IsMinGW32 = Components[2].startswith("mingw");
  906. }
  907. EnvironmentType Environment = UnknownEnvironment;
  908. if (Components.size() > 3)
  909. Environment = parseEnvironment(Components[3]);
  910. ObjectFormatType ObjectFormat = UnknownObjectFormat;
  911. if (Components.size() > 4)
  912. ObjectFormat = parseFormat(Components[4]);
  913. // Note which components are already in their final position. These will not
  914. // be moved.
  915. bool Found[4];
  916. Found[0] = Arch != UnknownArch;
  917. Found[1] = Vendor != UnknownVendor;
  918. Found[2] = OS != UnknownOS;
  919. Found[3] = Environment != UnknownEnvironment;
  920. // If they are not there already, permute the components into their canonical
  921. // positions by seeing if they parse as a valid architecture, and if so moving
  922. // the component to the architecture position etc.
  923. for (unsigned Pos = 0; Pos != std::size(Found); ++Pos) {
  924. if (Found[Pos])
  925. continue; // Already in the canonical position.
  926. for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
  927. // Do not reparse any components that already matched.
  928. if (Idx < std::size(Found) && Found[Idx])
  929. continue;
  930. // Does this component parse as valid for the target position?
  931. bool Valid = false;
  932. StringRef Comp = Components[Idx];
  933. switch (Pos) {
  934. default: llvm_unreachable("unexpected component type!");
  935. case 0:
  936. Arch = parseArch(Comp);
  937. Valid = Arch != UnknownArch;
  938. break;
  939. case 1:
  940. Vendor = parseVendor(Comp);
  941. Valid = Vendor != UnknownVendor;
  942. break;
  943. case 2:
  944. OS = parseOS(Comp);
  945. IsCygwin = Comp.startswith("cygwin");
  946. IsMinGW32 = Comp.startswith("mingw");
  947. Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
  948. break;
  949. case 3:
  950. Environment = parseEnvironment(Comp);
  951. Valid = Environment != UnknownEnvironment;
  952. if (!Valid) {
  953. ObjectFormat = parseFormat(Comp);
  954. Valid = ObjectFormat != UnknownObjectFormat;
  955. }
  956. break;
  957. }
  958. if (!Valid)
  959. continue; // Nope, try the next component.
  960. // Move the component to the target position, pushing any non-fixed
  961. // components that are in the way to the right. This tends to give
  962. // good results in the common cases of a forgotten vendor component
  963. // or a wrongly positioned environment.
  964. if (Pos < Idx) {
  965. // Insert left, pushing the existing components to the right. For
  966. // example, a-b-i386 -> i386-a-b when moving i386 to the front.
  967. StringRef CurrentComponent(""); // The empty component.
  968. // Replace the component we are moving with an empty component.
  969. std::swap(CurrentComponent, Components[Idx]);
  970. // Insert the component being moved at Pos, displacing any existing
  971. // components to the right.
  972. for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
  973. // Skip over any fixed components.
  974. while (i < std::size(Found) && Found[i])
  975. ++i;
  976. // Place the component at the new position, getting the component
  977. // that was at this position - it will be moved right.
  978. std::swap(CurrentComponent, Components[i]);
  979. }
  980. } else if (Pos > Idx) {
  981. // Push right by inserting empty components until the component at Idx
  982. // reaches the target position Pos. For example, pc-a -> -pc-a when
  983. // moving pc to the second position.
  984. do {
  985. // Insert one empty component at Idx.
  986. StringRef CurrentComponent(""); // The empty component.
  987. for (unsigned i = Idx; i < Components.size();) {
  988. // Place the component at the new position, getting the component
  989. // that was at this position - it will be moved right.
  990. std::swap(CurrentComponent, Components[i]);
  991. // If it was placed on top of an empty component then we are done.
  992. if (CurrentComponent.empty())
  993. break;
  994. // Advance to the next component, skipping any fixed components.
  995. while (++i < std::size(Found) && Found[i])
  996. ;
  997. }
  998. // The last component was pushed off the end - append it.
  999. if (!CurrentComponent.empty())
  1000. Components.push_back(CurrentComponent);
  1001. // Advance Idx to the component's new position.
  1002. while (++Idx < std::size(Found) && Found[Idx])
  1003. ;
  1004. } while (Idx < Pos); // Add more until the final position is reached.
  1005. }
  1006. assert(Pos < Components.size() && Components[Pos] == Comp &&
  1007. "Component moved wrong!");
  1008. Found[Pos] = true;
  1009. break;
  1010. }
  1011. }
  1012. // Replace empty components with "unknown" value.
  1013. for (StringRef &C : Components)
  1014. if (C.empty())
  1015. C = "unknown";
  1016. // Special case logic goes here. At this point Arch, Vendor and OS have the
  1017. // correct values for the computed components.
  1018. std::string NormalizedEnvironment;
  1019. if (Environment == Triple::Android && Components[3].startswith("androideabi")) {
  1020. StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
  1021. if (AndroidVersion.empty()) {
  1022. Components[3] = "android";
  1023. } else {
  1024. NormalizedEnvironment = Twine("android", AndroidVersion).str();
  1025. Components[3] = NormalizedEnvironment;
  1026. }
  1027. }
  1028. // SUSE uses "gnueabi" to mean "gnueabihf"
  1029. if (Vendor == Triple::SUSE && Environment == llvm::Triple::GNUEABI)
  1030. Components[3] = "gnueabihf";
  1031. if (OS == Triple::Win32) {
  1032. Components.resize(4);
  1033. Components[2] = "windows";
  1034. if (Environment == UnknownEnvironment) {
  1035. if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
  1036. Components[3] = "msvc";
  1037. else
  1038. Components[3] = getObjectFormatTypeName(ObjectFormat);
  1039. }
  1040. } else if (IsMinGW32) {
  1041. Components.resize(4);
  1042. Components[2] = "windows";
  1043. Components[3] = "gnu";
  1044. } else if (IsCygwin) {
  1045. Components.resize(4);
  1046. Components[2] = "windows";
  1047. Components[3] = "cygnus";
  1048. }
  1049. if (IsMinGW32 || IsCygwin ||
  1050. (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
  1051. if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
  1052. Components.resize(5);
  1053. Components[4] = getObjectFormatTypeName(ObjectFormat);
  1054. }
  1055. }
  1056. // Stick the corrected components back together to form the normalized string.
  1057. return join(Components, "-");
  1058. }
  1059. StringRef Triple::getArchName() const {
  1060. return StringRef(Data).split('-').first; // Isolate first component
  1061. }
  1062. StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) const {
  1063. switch (Kind) {
  1064. case Triple::mips:
  1065. if (SubArch == MipsSubArch_r6)
  1066. return "mipsisa32r6";
  1067. break;
  1068. case Triple::mipsel:
  1069. if (SubArch == MipsSubArch_r6)
  1070. return "mipsisa32r6el";
  1071. break;
  1072. case Triple::mips64:
  1073. if (SubArch == MipsSubArch_r6)
  1074. return "mipsisa64r6";
  1075. break;
  1076. case Triple::mips64el:
  1077. if (SubArch == MipsSubArch_r6)
  1078. return "mipsisa64r6el";
  1079. break;
  1080. case Triple::aarch64:
  1081. if (SubArch == AArch64SubArch_arm64ec)
  1082. return "arm64ec";
  1083. break;
  1084. default:
  1085. break;
  1086. }
  1087. return getArchTypeName(Kind);
  1088. }
  1089. StringRef Triple::getVendorName() const {
  1090. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  1091. return Tmp.split('-').first; // Isolate second component
  1092. }
  1093. StringRef Triple::getOSName() const {
  1094. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  1095. Tmp = Tmp.split('-').second; // Strip second component
  1096. return Tmp.split('-').first; // Isolate third component
  1097. }
  1098. StringRef Triple::getEnvironmentName() const {
  1099. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  1100. Tmp = Tmp.split('-').second; // Strip second component
  1101. return Tmp.split('-').second; // Strip third component
  1102. }
  1103. StringRef Triple::getOSAndEnvironmentName() const {
  1104. StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
  1105. return Tmp.split('-').second; // Strip second component
  1106. }
  1107. static VersionTuple parseVersionFromName(StringRef Name) {
  1108. VersionTuple Version;
  1109. Version.tryParse(Name);
  1110. return Version.withoutBuild();
  1111. }
  1112. VersionTuple Triple::getEnvironmentVersion() const {
  1113. StringRef EnvironmentName = getEnvironmentName();
  1114. StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
  1115. if (EnvironmentName.startswith(EnvironmentTypeName))
  1116. EnvironmentName = EnvironmentName.substr(EnvironmentTypeName.size());
  1117. return parseVersionFromName(EnvironmentName);
  1118. }
  1119. VersionTuple Triple::getOSVersion() const {
  1120. StringRef OSName = getOSName();
  1121. // Assume that the OS portion of the triple starts with the canonical name.
  1122. StringRef OSTypeName = getOSTypeName(getOS());
  1123. if (OSName.startswith(OSTypeName))
  1124. OSName = OSName.substr(OSTypeName.size());
  1125. else if (getOS() == MacOSX)
  1126. OSName.consume_front("macos");
  1127. return parseVersionFromName(OSName);
  1128. }
  1129. bool Triple::getMacOSXVersion(VersionTuple &Version) const {
  1130. Version = getOSVersion();
  1131. switch (getOS()) {
  1132. default: llvm_unreachable("unexpected OS for Darwin triple");
  1133. case Darwin:
  1134. // Default to darwin8, i.e., MacOSX 10.4.
  1135. if (Version.getMajor() == 0)
  1136. Version = VersionTuple(8);
  1137. // Darwin version numbers are skewed from OS X versions.
  1138. if (Version.getMajor() < 4) {
  1139. return false;
  1140. }
  1141. if (Version.getMajor() <= 19) {
  1142. Version = VersionTuple(10, Version.getMajor() - 4);
  1143. } else {
  1144. // darwin20+ corresponds to macOS 11+.
  1145. Version = VersionTuple(11 + Version.getMajor() - 20);
  1146. }
  1147. break;
  1148. case MacOSX:
  1149. // Default to 10.4.
  1150. if (Version.getMajor() == 0) {
  1151. Version = VersionTuple(10, 4);
  1152. } else if (Version.getMajor() < 10) {
  1153. return false;
  1154. }
  1155. break;
  1156. case IOS:
  1157. case TvOS:
  1158. case WatchOS:
  1159. // Ignore the version from the triple. This is only handled because the
  1160. // the clang driver combines OS X and IOS support into a common Darwin
  1161. // toolchain that wants to know the OS X version number even when targeting
  1162. // IOS.
  1163. Version = VersionTuple(10, 4);
  1164. break;
  1165. case DriverKit:
  1166. llvm_unreachable("OSX version isn't relevant for DriverKit");
  1167. }
  1168. return true;
  1169. }
  1170. VersionTuple Triple::getiOSVersion() const {
  1171. switch (getOS()) {
  1172. default: llvm_unreachable("unexpected OS for Darwin triple");
  1173. case Darwin:
  1174. case MacOSX:
  1175. // Ignore the version from the triple. This is only handled because the
  1176. // the clang driver combines OS X and IOS support into a common Darwin
  1177. // toolchain that wants to know the iOS version number even when targeting
  1178. // OS X.
  1179. return VersionTuple(5);
  1180. case IOS:
  1181. case TvOS: {
  1182. VersionTuple Version = getOSVersion();
  1183. // Default to 5.0 (or 7.0 for arm64).
  1184. if (Version.getMajor() == 0)
  1185. return (getArch() == aarch64) ? VersionTuple(7) : VersionTuple(5);
  1186. return Version;
  1187. }
  1188. case WatchOS:
  1189. llvm_unreachable("conflicting triple info");
  1190. case DriverKit:
  1191. llvm_unreachable("DriverKit doesn't have an iOS version");
  1192. }
  1193. }
  1194. VersionTuple Triple::getWatchOSVersion() const {
  1195. switch (getOS()) {
  1196. default: llvm_unreachable("unexpected OS for Darwin triple");
  1197. case Darwin:
  1198. case MacOSX:
  1199. // Ignore the version from the triple. This is only handled because the
  1200. // the clang driver combines OS X and IOS support into a common Darwin
  1201. // toolchain that wants to know the iOS version number even when targeting
  1202. // OS X.
  1203. return VersionTuple(2);
  1204. case WatchOS: {
  1205. VersionTuple Version = getOSVersion();
  1206. if (Version.getMajor() == 0)
  1207. return VersionTuple(2);
  1208. return Version;
  1209. }
  1210. case IOS:
  1211. llvm_unreachable("conflicting triple info");
  1212. case DriverKit:
  1213. llvm_unreachable("DriverKit doesn't have a WatchOS version");
  1214. }
  1215. }
  1216. VersionTuple Triple::getDriverKitVersion() const {
  1217. switch (getOS()) {
  1218. default:
  1219. llvm_unreachable("unexpected OS for Darwin triple");
  1220. case DriverKit:
  1221. VersionTuple Version = getOSVersion();
  1222. if (Version.getMajor() == 0)
  1223. return Version.withMajorReplaced(19);
  1224. return Version;
  1225. }
  1226. }
  1227. void Triple::setTriple(const Twine &Str) {
  1228. *this = Triple(Str);
  1229. }
  1230. void Triple::setArch(ArchType Kind, SubArchType SubArch) {
  1231. setArchName(getArchName(Kind, SubArch));
  1232. }
  1233. void Triple::setVendor(VendorType Kind) {
  1234. setVendorName(getVendorTypeName(Kind));
  1235. }
  1236. void Triple::setOS(OSType Kind) {
  1237. setOSName(getOSTypeName(Kind));
  1238. }
  1239. void Triple::setEnvironment(EnvironmentType Kind) {
  1240. if (ObjectFormat == getDefaultFormat(*this))
  1241. return setEnvironmentName(getEnvironmentTypeName(Kind));
  1242. setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
  1243. getObjectFormatTypeName(ObjectFormat)).str());
  1244. }
  1245. void Triple::setObjectFormat(ObjectFormatType Kind) {
  1246. if (Environment == UnknownEnvironment)
  1247. return setEnvironmentName(getObjectFormatTypeName(Kind));
  1248. setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
  1249. getObjectFormatTypeName(Kind)).str());
  1250. }
  1251. void Triple::setArchName(StringRef Str) {
  1252. // Work around a miscompilation bug for Twines in gcc 4.0.3.
  1253. SmallString<64> Triple;
  1254. Triple += Str;
  1255. Triple += "-";
  1256. Triple += getVendorName();
  1257. Triple += "-";
  1258. Triple += getOSAndEnvironmentName();
  1259. setTriple(Triple);
  1260. }
  1261. void Triple::setVendorName(StringRef Str) {
  1262. setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
  1263. }
  1264. void Triple::setOSName(StringRef Str) {
  1265. if (hasEnvironment())
  1266. setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
  1267. "-" + getEnvironmentName());
  1268. else
  1269. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  1270. }
  1271. void Triple::setEnvironmentName(StringRef Str) {
  1272. setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
  1273. "-" + Str);
  1274. }
  1275. void Triple::setOSAndEnvironmentName(StringRef Str) {
  1276. setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
  1277. }
  1278. static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
  1279. switch (Arch) {
  1280. case llvm::Triple::UnknownArch:
  1281. return 0;
  1282. case llvm::Triple::avr:
  1283. case llvm::Triple::msp430:
  1284. return 16;
  1285. case llvm::Triple::aarch64_32:
  1286. case llvm::Triple::amdil:
  1287. case llvm::Triple::arc:
  1288. case llvm::Triple::arm:
  1289. case llvm::Triple::armeb:
  1290. case llvm::Triple::csky:
  1291. case llvm::Triple::dxil:
  1292. case llvm::Triple::hexagon:
  1293. case llvm::Triple::hsail:
  1294. case llvm::Triple::kalimba:
  1295. case llvm::Triple::lanai:
  1296. case llvm::Triple::le32:
  1297. case llvm::Triple::loongarch32:
  1298. case llvm::Triple::m68k:
  1299. case llvm::Triple::mips:
  1300. case llvm::Triple::mipsel:
  1301. case llvm::Triple::nvptx:
  1302. case llvm::Triple::ppc:
  1303. case llvm::Triple::ppcle:
  1304. case llvm::Triple::r600:
  1305. case llvm::Triple::renderscript32:
  1306. case llvm::Triple::riscv32:
  1307. case llvm::Triple::shave:
  1308. case llvm::Triple::sparc:
  1309. case llvm::Triple::sparcel:
  1310. case llvm::Triple::spir:
  1311. case llvm::Triple::spirv32:
  1312. case llvm::Triple::tce:
  1313. case llvm::Triple::tcele:
  1314. case llvm::Triple::thumb:
  1315. case llvm::Triple::thumbeb:
  1316. case llvm::Triple::wasm32:
  1317. case llvm::Triple::x86:
  1318. case llvm::Triple::xcore:
  1319. case llvm::Triple::xtensa:
  1320. return 32;
  1321. case llvm::Triple::aarch64:
  1322. case llvm::Triple::aarch64_be:
  1323. case llvm::Triple::amdgcn:
  1324. case llvm::Triple::amdil64:
  1325. case llvm::Triple::bpfeb:
  1326. case llvm::Triple::bpfel:
  1327. case llvm::Triple::hsail64:
  1328. case llvm::Triple::le64:
  1329. case llvm::Triple::loongarch64:
  1330. case llvm::Triple::mips64:
  1331. case llvm::Triple::mips64el:
  1332. case llvm::Triple::nvptx64:
  1333. case llvm::Triple::ppc64:
  1334. case llvm::Triple::ppc64le:
  1335. case llvm::Triple::renderscript64:
  1336. case llvm::Triple::riscv64:
  1337. case llvm::Triple::sparcv9:
  1338. case llvm::Triple::spir64:
  1339. case llvm::Triple::spirv64:
  1340. case llvm::Triple::systemz:
  1341. case llvm::Triple::ve:
  1342. case llvm::Triple::wasm64:
  1343. case llvm::Triple::x86_64:
  1344. return 64;
  1345. }
  1346. llvm_unreachable("Invalid architecture value");
  1347. }
  1348. bool Triple::isArch64Bit() const {
  1349. return getArchPointerBitWidth(getArch()) == 64;
  1350. }
  1351. bool Triple::isArch32Bit() const {
  1352. return getArchPointerBitWidth(getArch()) == 32;
  1353. }
  1354. bool Triple::isArch16Bit() const {
  1355. return getArchPointerBitWidth(getArch()) == 16;
  1356. }
  1357. Triple Triple::get32BitArchVariant() const {
  1358. Triple T(*this);
  1359. switch (getArch()) {
  1360. case Triple::UnknownArch:
  1361. case Triple::amdgcn:
  1362. case Triple::avr:
  1363. case Triple::bpfeb:
  1364. case Triple::bpfel:
  1365. case Triple::msp430:
  1366. case Triple::systemz:
  1367. case Triple::ve:
  1368. T.setArch(UnknownArch);
  1369. break;
  1370. case Triple::aarch64_32:
  1371. case Triple::amdil:
  1372. case Triple::arc:
  1373. case Triple::arm:
  1374. case Triple::armeb:
  1375. case Triple::csky:
  1376. case Triple::dxil:
  1377. case Triple::hexagon:
  1378. case Triple::hsail:
  1379. case Triple::kalimba:
  1380. case Triple::lanai:
  1381. case Triple::le32:
  1382. case Triple::loongarch32:
  1383. case Triple::m68k:
  1384. case Triple::mips:
  1385. case Triple::mipsel:
  1386. case Triple::nvptx:
  1387. case Triple::ppc:
  1388. case Triple::ppcle:
  1389. case Triple::r600:
  1390. case Triple::renderscript32:
  1391. case Triple::riscv32:
  1392. case Triple::shave:
  1393. case Triple::sparc:
  1394. case Triple::sparcel:
  1395. case Triple::spir:
  1396. case Triple::spirv32:
  1397. case Triple::tce:
  1398. case Triple::tcele:
  1399. case Triple::thumb:
  1400. case Triple::thumbeb:
  1401. case Triple::wasm32:
  1402. case Triple::x86:
  1403. case Triple::xcore:
  1404. case Triple::xtensa:
  1405. // Already 32-bit.
  1406. break;
  1407. case Triple::aarch64: T.setArch(Triple::arm); break;
  1408. case Triple::aarch64_be: T.setArch(Triple::armeb); break;
  1409. case Triple::amdil64: T.setArch(Triple::amdil); break;
  1410. case Triple::hsail64: T.setArch(Triple::hsail); break;
  1411. case Triple::le64: T.setArch(Triple::le32); break;
  1412. case Triple::loongarch64: T.setArch(Triple::loongarch32); break;
  1413. case Triple::mips64:
  1414. T.setArch(Triple::mips, getSubArch());
  1415. break;
  1416. case Triple::mips64el:
  1417. T.setArch(Triple::mipsel, getSubArch());
  1418. break;
  1419. case Triple::nvptx64: T.setArch(Triple::nvptx); break;
  1420. case Triple::ppc64: T.setArch(Triple::ppc); break;
  1421. case Triple::ppc64le: T.setArch(Triple::ppcle); break;
  1422. case Triple::renderscript64: T.setArch(Triple::renderscript32); break;
  1423. case Triple::riscv64: T.setArch(Triple::riscv32); break;
  1424. case Triple::sparcv9: T.setArch(Triple::sparc); break;
  1425. case Triple::spir64: T.setArch(Triple::spir); break;
  1426. case Triple::spirv64:
  1427. T.setArch(Triple::spirv32, getSubArch());
  1428. break;
  1429. case Triple::wasm64: T.setArch(Triple::wasm32); break;
  1430. case Triple::x86_64: T.setArch(Triple::x86); break;
  1431. }
  1432. return T;
  1433. }
  1434. Triple Triple::get64BitArchVariant() const {
  1435. Triple T(*this);
  1436. switch (getArch()) {
  1437. case Triple::UnknownArch:
  1438. case Triple::arc:
  1439. case Triple::avr:
  1440. case Triple::csky:
  1441. case Triple::dxil:
  1442. case Triple::hexagon:
  1443. case Triple::kalimba:
  1444. case Triple::lanai:
  1445. case Triple::m68k:
  1446. case Triple::msp430:
  1447. case Triple::r600:
  1448. case Triple::shave:
  1449. case Triple::sparcel:
  1450. case Triple::tce:
  1451. case Triple::tcele:
  1452. case Triple::xcore:
  1453. case Triple::xtensa:
  1454. T.setArch(UnknownArch);
  1455. break;
  1456. case Triple::aarch64:
  1457. case Triple::aarch64_be:
  1458. case Triple::amdgcn:
  1459. case Triple::amdil64:
  1460. case Triple::bpfeb:
  1461. case Triple::bpfel:
  1462. case Triple::hsail64:
  1463. case Triple::le64:
  1464. case Triple::loongarch64:
  1465. case Triple::mips64:
  1466. case Triple::mips64el:
  1467. case Triple::nvptx64:
  1468. case Triple::ppc64:
  1469. case Triple::ppc64le:
  1470. case Triple::renderscript64:
  1471. case Triple::riscv64:
  1472. case Triple::sparcv9:
  1473. case Triple::spir64:
  1474. case Triple::spirv64:
  1475. case Triple::systemz:
  1476. case Triple::ve:
  1477. case Triple::wasm64:
  1478. case Triple::x86_64:
  1479. // Already 64-bit.
  1480. break;
  1481. case Triple::aarch64_32: T.setArch(Triple::aarch64); break;
  1482. case Triple::amdil: T.setArch(Triple::amdil64); break;
  1483. case Triple::arm: T.setArch(Triple::aarch64); break;
  1484. case Triple::armeb: T.setArch(Triple::aarch64_be); break;
  1485. case Triple::hsail: T.setArch(Triple::hsail64); break;
  1486. case Triple::le32: T.setArch(Triple::le64); break;
  1487. case Triple::loongarch32: T.setArch(Triple::loongarch64); break;
  1488. case Triple::mips:
  1489. T.setArch(Triple::mips64, getSubArch());
  1490. break;
  1491. case Triple::mipsel:
  1492. T.setArch(Triple::mips64el, getSubArch());
  1493. break;
  1494. case Triple::nvptx: T.setArch(Triple::nvptx64); break;
  1495. case Triple::ppc: T.setArch(Triple::ppc64); break;
  1496. case Triple::ppcle: T.setArch(Triple::ppc64le); break;
  1497. case Triple::renderscript32: T.setArch(Triple::renderscript64); break;
  1498. case Triple::riscv32: T.setArch(Triple::riscv64); break;
  1499. case Triple::sparc: T.setArch(Triple::sparcv9); break;
  1500. case Triple::spir: T.setArch(Triple::spir64); break;
  1501. case Triple::spirv32:
  1502. T.setArch(Triple::spirv64, getSubArch());
  1503. break;
  1504. case Triple::thumb: T.setArch(Triple::aarch64); break;
  1505. case Triple::thumbeb: T.setArch(Triple::aarch64_be); break;
  1506. case Triple::wasm32: T.setArch(Triple::wasm64); break;
  1507. case Triple::x86: T.setArch(Triple::x86_64); break;
  1508. }
  1509. return T;
  1510. }
  1511. Triple Triple::getBigEndianArchVariant() const {
  1512. Triple T(*this);
  1513. // Already big endian.
  1514. if (!isLittleEndian())
  1515. return T;
  1516. switch (getArch()) {
  1517. case Triple::UnknownArch:
  1518. case Triple::amdgcn:
  1519. case Triple::amdil64:
  1520. case Triple::amdil:
  1521. case Triple::avr:
  1522. case Triple::dxil:
  1523. case Triple::hexagon:
  1524. case Triple::hsail64:
  1525. case Triple::hsail:
  1526. case Triple::kalimba:
  1527. case Triple::le32:
  1528. case Triple::le64:
  1529. case Triple::loongarch32:
  1530. case Triple::loongarch64:
  1531. case Triple::msp430:
  1532. case Triple::nvptx64:
  1533. case Triple::nvptx:
  1534. case Triple::r600:
  1535. case Triple::renderscript32:
  1536. case Triple::renderscript64:
  1537. case Triple::riscv32:
  1538. case Triple::riscv64:
  1539. case Triple::shave:
  1540. case Triple::spir64:
  1541. case Triple::spir:
  1542. case Triple::spirv32:
  1543. case Triple::spirv64:
  1544. case Triple::wasm32:
  1545. case Triple::wasm64:
  1546. case Triple::x86:
  1547. case Triple::x86_64:
  1548. case Triple::xcore:
  1549. case Triple::ve:
  1550. case Triple::csky:
  1551. case Triple::xtensa:
  1552. // ARM is intentionally unsupported here, changing the architecture would
  1553. // drop any arch suffixes.
  1554. case Triple::arm:
  1555. case Triple::thumb:
  1556. T.setArch(UnknownArch);
  1557. break;
  1558. case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
  1559. case Triple::bpfel: T.setArch(Triple::bpfeb); break;
  1560. case Triple::mips64el:
  1561. T.setArch(Triple::mips64, getSubArch());
  1562. break;
  1563. case Triple::mipsel:
  1564. T.setArch(Triple::mips, getSubArch());
  1565. break;
  1566. case Triple::ppcle: T.setArch(Triple::ppc); break;
  1567. case Triple::ppc64le: T.setArch(Triple::ppc64); break;
  1568. case Triple::sparcel: T.setArch(Triple::sparc); break;
  1569. case Triple::tcele: T.setArch(Triple::tce); break;
  1570. default:
  1571. llvm_unreachable("getBigEndianArchVariant: unknown triple.");
  1572. }
  1573. return T;
  1574. }
  1575. Triple Triple::getLittleEndianArchVariant() const {
  1576. Triple T(*this);
  1577. if (isLittleEndian())
  1578. return T;
  1579. switch (getArch()) {
  1580. case Triple::UnknownArch:
  1581. case Triple::lanai:
  1582. case Triple::sparcv9:
  1583. case Triple::systemz:
  1584. case Triple::m68k:
  1585. // ARM is intentionally unsupported here, changing the architecture would
  1586. // drop any arch suffixes.
  1587. case Triple::armeb:
  1588. case Triple::thumbeb:
  1589. T.setArch(UnknownArch);
  1590. break;
  1591. case Triple::aarch64_be: T.setArch(Triple::aarch64); break;
  1592. case Triple::bpfeb: T.setArch(Triple::bpfel); break;
  1593. case Triple::mips64:
  1594. T.setArch(Triple::mips64el, getSubArch());
  1595. break;
  1596. case Triple::mips:
  1597. T.setArch(Triple::mipsel, getSubArch());
  1598. break;
  1599. case Triple::ppc: T.setArch(Triple::ppcle); break;
  1600. case Triple::ppc64: T.setArch(Triple::ppc64le); break;
  1601. case Triple::sparc: T.setArch(Triple::sparcel); break;
  1602. case Triple::tce: T.setArch(Triple::tcele); break;
  1603. default:
  1604. llvm_unreachable("getLittleEndianArchVariant: unknown triple.");
  1605. }
  1606. return T;
  1607. }
  1608. bool Triple::isLittleEndian() const {
  1609. switch (getArch()) {
  1610. case Triple::aarch64:
  1611. case Triple::aarch64_32:
  1612. case Triple::amdgcn:
  1613. case Triple::amdil64:
  1614. case Triple::amdil:
  1615. case Triple::arm:
  1616. case Triple::avr:
  1617. case Triple::bpfel:
  1618. case Triple::csky:
  1619. case Triple::dxil:
  1620. case Triple::hexagon:
  1621. case Triple::hsail64:
  1622. case Triple::hsail:
  1623. case Triple::kalimba:
  1624. case Triple::le32:
  1625. case Triple::le64:
  1626. case Triple::loongarch32:
  1627. case Triple::loongarch64:
  1628. case Triple::mips64el:
  1629. case Triple::mipsel:
  1630. case Triple::msp430:
  1631. case Triple::nvptx64:
  1632. case Triple::nvptx:
  1633. case Triple::ppcle:
  1634. case Triple::ppc64le:
  1635. case Triple::r600:
  1636. case Triple::renderscript32:
  1637. case Triple::renderscript64:
  1638. case Triple::riscv32:
  1639. case Triple::riscv64:
  1640. case Triple::shave:
  1641. case Triple::sparcel:
  1642. case Triple::spir64:
  1643. case Triple::spir:
  1644. case Triple::spirv32:
  1645. case Triple::spirv64:
  1646. case Triple::tcele:
  1647. case Triple::thumb:
  1648. case Triple::ve:
  1649. case Triple::wasm32:
  1650. case Triple::wasm64:
  1651. case Triple::x86:
  1652. case Triple::x86_64:
  1653. case Triple::xcore:
  1654. case Triple::xtensa:
  1655. return true;
  1656. default:
  1657. return false;
  1658. }
  1659. }
  1660. bool Triple::isCompatibleWith(const Triple &Other) const {
  1661. // ARM and Thumb triples are compatible, if subarch, vendor and OS match.
  1662. if ((getArch() == Triple::thumb && Other.getArch() == Triple::arm) ||
  1663. (getArch() == Triple::arm && Other.getArch() == Triple::thumb) ||
  1664. (getArch() == Triple::thumbeb && Other.getArch() == Triple::armeb) ||
  1665. (getArch() == Triple::armeb && Other.getArch() == Triple::thumbeb)) {
  1666. if (getVendor() == Triple::Apple)
  1667. return getSubArch() == Other.getSubArch() &&
  1668. getVendor() == Other.getVendor() && getOS() == Other.getOS();
  1669. else
  1670. return getSubArch() == Other.getSubArch() &&
  1671. getVendor() == Other.getVendor() && getOS() == Other.getOS() &&
  1672. getEnvironment() == Other.getEnvironment() &&
  1673. getObjectFormat() == Other.getObjectFormat();
  1674. }
  1675. // If vendor is apple, ignore the version number.
  1676. if (getVendor() == Triple::Apple)
  1677. return getArch() == Other.getArch() && getSubArch() == Other.getSubArch() &&
  1678. getVendor() == Other.getVendor() && getOS() == Other.getOS();
  1679. return *this == Other;
  1680. }
  1681. std::string Triple::merge(const Triple &Other) const {
  1682. // If vendor is apple, pick the triple with the larger version number.
  1683. if (getVendor() == Triple::Apple)
  1684. if (Other.isOSVersionLT(*this))
  1685. return str();
  1686. return Other.str();
  1687. }
  1688. bool Triple::isMacOSXVersionLT(unsigned Major, unsigned Minor,
  1689. unsigned Micro) const {
  1690. assert(isMacOSX() && "Not an OS X triple!");
  1691. // If this is OS X, expect a sane version number.
  1692. if (getOS() == Triple::MacOSX)
  1693. return isOSVersionLT(Major, Minor, Micro);
  1694. // Otherwise, compare to the "Darwin" number.
  1695. if (Major == 10) {
  1696. return isOSVersionLT(Minor + 4, Micro, 0);
  1697. } else {
  1698. assert(Major >= 11 && "Unexpected major version");
  1699. return isOSVersionLT(Major - 11 + 20, Minor, Micro);
  1700. }
  1701. }
  1702. VersionTuple Triple::getMinimumSupportedOSVersion() const {
  1703. if (getVendor() != Triple::Apple || getArch() != Triple::aarch64)
  1704. return VersionTuple();
  1705. switch (getOS()) {
  1706. case Triple::MacOSX:
  1707. // ARM64 slice is supported starting from macOS 11.0+.
  1708. return VersionTuple(11, 0, 0);
  1709. case Triple::IOS:
  1710. // ARM64 slice is supported starting from Mac Catalyst 14 (macOS 11).
  1711. // ARM64 simulators are supported for iOS 14+.
  1712. if (isMacCatalystEnvironment() || isSimulatorEnvironment())
  1713. return VersionTuple(14, 0, 0);
  1714. // ARM64e slice is supported starting from iOS 14.
  1715. if (isArm64e())
  1716. return VersionTuple(14, 0, 0);
  1717. break;
  1718. case Triple::TvOS:
  1719. // ARM64 simulators are supported for tvOS 14+.
  1720. if (isSimulatorEnvironment())
  1721. return VersionTuple(14, 0, 0);
  1722. break;
  1723. case Triple::WatchOS:
  1724. // ARM64 simulators are supported for watchOS 7+.
  1725. if (isSimulatorEnvironment())
  1726. return VersionTuple(7, 0, 0);
  1727. break;
  1728. case Triple::DriverKit:
  1729. return VersionTuple(20, 0, 0);
  1730. default:
  1731. break;
  1732. }
  1733. return VersionTuple();
  1734. }
  1735. VersionTuple Triple::getCanonicalVersionForOS(OSType OSKind,
  1736. const VersionTuple &Version) {
  1737. switch (OSKind) {
  1738. case MacOSX:
  1739. // macOS 10.16 is canonicalized to macOS 11.
  1740. if (Version == VersionTuple(10, 16))
  1741. return VersionTuple(11, 0);
  1742. [[fallthrough]];
  1743. default:
  1744. return Version;
  1745. }
  1746. }
  1747. // HLSL triple environment orders are relied on in the front end
  1748. static_assert(Triple::Vertex - Triple::Pixel == 1,
  1749. "incorrect HLSL stage order");
  1750. static_assert(Triple::Geometry - Triple::Pixel == 2,
  1751. "incorrect HLSL stage order");
  1752. static_assert(Triple::Hull - Triple::Pixel == 3,
  1753. "incorrect HLSL stage order");
  1754. static_assert(Triple::Domain - Triple::Pixel == 4,
  1755. "incorrect HLSL stage order");
  1756. static_assert(Triple::Compute - Triple::Pixel == 5,
  1757. "incorrect HLSL stage order");
  1758. static_assert(Triple::Library - Triple::Pixel == 6,
  1759. "incorrect HLSL stage order");
  1760. static_assert(Triple::RayGeneration - Triple::Pixel == 7,
  1761. "incorrect HLSL stage order");
  1762. static_assert(Triple::Intersection - Triple::Pixel == 8,
  1763. "incorrect HLSL stage order");
  1764. static_assert(Triple::AnyHit - Triple::Pixel == 9,
  1765. "incorrect HLSL stage order");
  1766. static_assert(Triple::ClosestHit - Triple::Pixel == 10,
  1767. "incorrect HLSL stage order");
  1768. static_assert(Triple::Miss - Triple::Pixel == 11,
  1769. "incorrect HLSL stage order");
  1770. static_assert(Triple::Callable - Triple::Pixel == 12,
  1771. "incorrect HLSL stage order");
  1772. static_assert(Triple::Mesh - Triple::Pixel == 13,
  1773. "incorrect HLSL stage order");
  1774. static_assert(Triple::Amplification - Triple::Pixel == 14,
  1775. "incorrect HLSL stage order");