PageRenderTime 163ms CodeModel.GetById 3ms app.highlight 148ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/Support/Triple.cpp

https://bitbucket.org/HanyunseopEverspin/eversafe_ios_obfuscator_archive
C++ | 1608 lines | 1362 code | 137 blank | 109 comment | 220 complexity | f81b04ea2c717f0b3733785c9af27e3c MD5 | raw file

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

Large files files are truncated, but you can click here to view the full file