llvm-project

Форк
0
2119 строк · 67.5 Кб
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

9
#include "llvm/TargetParser/Triple.h"
10
#include "llvm/ADT/DenseMap.h"
11
#include "llvm/ADT/SmallString.h"
12
#include "llvm/ADT/StringExtras.h"
13
#include "llvm/ADT/StringSwitch.h"
14
#include "llvm/Support/ErrorHandling.h"
15
#include "llvm/Support/SwapByteOrder.h"
16
#include "llvm/Support/VersionTuple.h"
17
#include "llvm/TargetParser/ARMTargetParser.h"
18
#include "llvm/TargetParser/ARMTargetParserCommon.h"
19
#include "llvm/TargetParser/Host.h"
20
#include <cassert>
21
#include <cstring>
22
using namespace llvm;
23

24
StringRef Triple::getArchTypeName(ArchType Kind) {
25
  switch (Kind) {
26
  case UnknownArch:    return "unknown";
27

28
  case aarch64:        return "aarch64";
29
  case aarch64_32:     return "aarch64_32";
30
  case aarch64_be:     return "aarch64_be";
31
  case amdgcn:         return "amdgcn";
32
  case amdil64:        return "amdil64";
33
  case amdil:          return "amdil";
34
  case arc:            return "arc";
35
  case arm:            return "arm";
36
  case armeb:          return "armeb";
37
  case avr:            return "avr";
38
  case bpfeb:          return "bpfeb";
39
  case bpfel:          return "bpfel";
40
  case csky:           return "csky";
41
  case dxil:           return "dxil";
42
  case hexagon:        return "hexagon";
43
  case hsail64:        return "hsail64";
44
  case hsail:          return "hsail";
45
  case kalimba:        return "kalimba";
46
  case lanai:          return "lanai";
47
  case le32:           return "le32";
48
  case le64:           return "le64";
49
  case loongarch32:    return "loongarch32";
50
  case loongarch64:    return "loongarch64";
51
  case m68k:           return "m68k";
52
  case mips64:         return "mips64";
53
  case mips64el:       return "mips64el";
54
  case mips:           return "mips";
55
  case mipsel:         return "mipsel";
56
  case msp430:         return "msp430";
57
  case nvptx64:        return "nvptx64";
58
  case nvptx:          return "nvptx";
59
  case ppc64:          return "powerpc64";
60
  case ppc64le:        return "powerpc64le";
61
  case ppc:            return "powerpc";
62
  case ppcle:          return "powerpcle";
63
  case r600:           return "r600";
64
  case renderscript32: return "renderscript32";
65
  case renderscript64: return "renderscript64";
66
  case riscv32:        return "riscv32";
67
  case riscv64:        return "riscv64";
68
  case shave:          return "shave";
69
  case sparc:          return "sparc";
70
  case sparcel:        return "sparcel";
71
  case sparcv9:        return "sparcv9";
72
  case spir64:         return "spir64";
73
  case spir:           return "spir";
74
  case spirv:          return "spirv";
75
  case spirv32:        return "spirv32";
76
  case spirv64:        return "spirv64";
77
  case systemz:        return "s390x";
78
  case tce:            return "tce";
79
  case tcele:          return "tcele";
80
  case thumb:          return "thumb";
81
  case thumbeb:        return "thumbeb";
82
  case ve:             return "ve";
83
  case wasm32:         return "wasm32";
84
  case wasm64:         return "wasm64";
85
  case x86:            return "i386";
86
  case x86_64:         return "x86_64";
87
  case xcore:          return "xcore";
88
  case xtensa:         return "xtensa";
89
  }
90

91
  llvm_unreachable("Invalid ArchType!");
92
}
93

94
StringRef Triple::getArchName(ArchType Kind, SubArchType SubArch) {
95
  switch (Kind) {
96
  case Triple::mips:
97
    if (SubArch == MipsSubArch_r6)
98
      return "mipsisa32r6";
99
    break;
100
  case Triple::mipsel:
101
    if (SubArch == MipsSubArch_r6)
102
      return "mipsisa32r6el";
103
    break;
104
  case Triple::mips64:
105
    if (SubArch == MipsSubArch_r6)
106
      return "mipsisa64r6";
107
    break;
108
  case Triple::mips64el:
109
    if (SubArch == MipsSubArch_r6)
110
      return "mipsisa64r6el";
111
    break;
112
  case Triple::aarch64:
113
    if (SubArch == AArch64SubArch_arm64ec)
114
      return "arm64ec";
115
    if (SubArch == AArch64SubArch_arm64e)
116
      return "arm64e";
117
    break;
118
  case Triple::dxil:
119
    switch (SubArch) {
120
    case Triple::NoSubArch:
121
    case Triple::DXILSubArch_v1_0:
122
      return "dxilv1.0";
123
    case Triple::DXILSubArch_v1_1:
124
      return "dxilv1.1";
125
    case Triple::DXILSubArch_v1_2:
126
      return "dxilv1.2";
127
    case Triple::DXILSubArch_v1_3:
128
      return "dxilv1.3";
129
    case Triple::DXILSubArch_v1_4:
130
      return "dxilv1.4";
131
    case Triple::DXILSubArch_v1_5:
132
      return "dxilv1.5";
133
    case Triple::DXILSubArch_v1_6:
134
      return "dxilv1.6";
135
    case Triple::DXILSubArch_v1_7:
136
      return "dxilv1.7";
137
    case Triple::DXILSubArch_v1_8:
138
      return "dxilv1.8";
139
    default:
140
      break;
141
    }
142
    break;
143
  default:
144
    break;
145
  }
146
  return getArchTypeName(Kind);
147
}
148

149
StringRef Triple::getArchTypePrefix(ArchType Kind) {
150
  switch (Kind) {
151
  default:
152
    return StringRef();
153

154
  case aarch64:
155
  case aarch64_be:
156
  case aarch64_32:  return "aarch64";
157

158
  case arc:         return "arc";
159

160
  case arm:
161
  case armeb:
162
  case thumb:
163
  case thumbeb:     return "arm";
164

165
  case avr:         return "avr";
166

167
  case ppc64:
168
  case ppc64le:
169
  case ppc:
170
  case ppcle:       return "ppc";
171

172
  case m68k:        return "m68k";
173

174
  case mips:
175
  case mipsel:
176
  case mips64:
177
  case mips64el:    return "mips";
178

179
  case hexagon:     return "hexagon";
180

181
  case amdgcn:      return "amdgcn";
182
  case r600:        return "r600";
183

184
  case bpfel:
185
  case bpfeb:       return "bpf";
186

187
  case sparcv9:
188
  case sparcel:
189
  case sparc:       return "sparc";
190

191
  case systemz:     return "s390";
192

193
  case x86:
194
  case x86_64:      return "x86";
195

196
  case xcore:       return "xcore";
197

198
  // NVPTX intrinsics are namespaced under nvvm.
199
  case nvptx:       return "nvvm";
200
  case nvptx64:     return "nvvm";
201

202
  case le32:        return "le32";
203
  case le64:        return "le64";
204

205
  case amdil:
206
  case amdil64:     return "amdil";
207

208
  case hsail:
209
  case hsail64:     return "hsail";
210

211
  case spir:
212
  case spir64:      return "spir";
213

214
  case spirv:
215
  case spirv32:
216
  case spirv64:     return "spv";
217

218
  case kalimba:     return "kalimba";
219
  case lanai:       return "lanai";
220
  case shave:       return "shave";
221
  case wasm32:
222
  case wasm64:      return "wasm";
223

224
  case riscv32:
225
  case riscv64:     return "riscv";
226

227
  case ve:          return "ve";
228
  case csky:        return "csky";
229

230
  case loongarch32:
231
  case loongarch64: return "loongarch";
232

233
  case dxil:        return "dx";
234

235
  case xtensa:      return "xtensa";
236
  }
237
}
238

239
StringRef Triple::getVendorTypeName(VendorType Kind) {
240
  switch (Kind) {
241
  case UnknownVendor: return "unknown";
242

243
  case AMD: return "amd";
244
  case Apple: return "apple";
245
  case CSR: return "csr";
246
  case Freescale: return "fsl";
247
  case IBM: return "ibm";
248
  case ImaginationTechnologies: return "img";
249
  case Mesa: return "mesa";
250
  case MipsTechnologies: return "mti";
251
  case NVIDIA: return "nvidia";
252
  case OpenEmbedded: return "oe";
253
  case PC: return "pc";
254
  case SCEI: return "scei";
255
  case SUSE: return "suse";
256
  }
257

258
  llvm_unreachable("Invalid VendorType!");
259
}
260

261
StringRef Triple::getOSTypeName(OSType Kind) {
262
  switch (Kind) {
263
  case UnknownOS: return "unknown";
264

265
  case AIX: return "aix";
266
  case AMDHSA: return "amdhsa";
267
  case AMDPAL: return "amdpal";
268
  case BridgeOS: return "bridgeos";
269
  case CUDA: return "cuda";
270
  case Darwin: return "darwin";
271
  case DragonFly: return "dragonfly";
272
  case DriverKit: return "driverkit";
273
  case ELFIAMCU: return "elfiamcu";
274
  case Emscripten: return "emscripten";
275
  case FreeBSD: return "freebsd";
276
  case Fuchsia: return "fuchsia";
277
  case Haiku: return "haiku";
278
  case HermitCore: return "hermit";
279
  case Hurd: return "hurd";
280
  case IOS: return "ios";
281
  case KFreeBSD: return "kfreebsd";
282
  case Linux: return "linux";
283
  case Lv2: return "lv2";
284
  case MacOSX: return "macosx";
285
  case Mesa3D: return "mesa3d";
286
  case NVCL: return "nvcl";
287
  case NaCl: return "nacl";
288
  case NetBSD: return "netbsd";
289
  case OpenBSD: return "openbsd";
290
  case PS4: return "ps4";
291
  case PS5: return "ps5";
292
  case RTEMS: return "rtems";
293
  case Solaris: return "solaris";
294
  case Serenity: return "serenity";
295
  case TvOS: return "tvos";
296
  case UEFI: return "uefi";
297
  case WASI: return "wasi";
298
  case WatchOS: return "watchos";
299
  case Win32: return "windows";
300
  case ZOS: return "zos";
301
  case ShaderModel: return "shadermodel";
302
  case LiteOS: return "liteos";
303
  case XROS: return "xros";
304
  case Vulkan: return "vulkan";
305
  }
306

307
  llvm_unreachable("Invalid OSType");
308
}
309

310
StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
311
  switch (Kind) {
312
  case UnknownEnvironment: return "unknown";
313
  case Android: return "android";
314
  case CODE16: return "code16";
315
  case CoreCLR: return "coreclr";
316
  case Cygnus: return "cygnus";
317
  case EABI: return "eabi";
318
  case EABIHF: return "eabihf";
319
  case GNU: return "gnu";
320
  case GNUABI64: return "gnuabi64";
321
  case GNUABIN32: return "gnuabin32";
322
  case GNUEABI: return "gnueabi";
323
  case GNUEABIHF: return "gnueabihf";
324
  case GNUF32: return "gnuf32";
325
  case GNUF64: return "gnuf64";
326
  case GNUSF: return "gnusf";
327
  case GNUX32: return "gnux32";
328
  case GNUILP32: return "gnu_ilp32";
329
  case Itanium: return "itanium";
330
  case MSVC: return "msvc";
331
  case MacABI: return "macabi";
332
  case Musl: return "musl";
333
  case MuslEABI: return "musleabi";
334
  case MuslEABIHF: return "musleabihf";
335
  case MuslX32: return "muslx32";
336
  case Simulator: return "simulator";
337
  case Pixel: return "pixel";
338
  case Vertex: return "vertex";
339
  case Geometry: return "geometry";
340
  case Hull: return "hull";
341
  case Domain: return "domain";
342
  case Compute: return "compute";
343
  case Library: return "library";
344
  case RayGeneration: return "raygeneration";
345
  case Intersection: return "intersection";
346
  case AnyHit: return "anyhit";
347
  case ClosestHit: return "closesthit";
348
  case Miss: return "miss";
349
  case Callable: return "callable";
350
  case Mesh: return "mesh";
351
  case Amplification: return "amplification";
352
  case OpenCL:
353
    return "opencl";
354
  case OpenHOS: return "ohos";
355
  }
356

357
  llvm_unreachable("Invalid EnvironmentType!");
358
}
359

360
StringRef Triple::getObjectFormatTypeName(ObjectFormatType Kind) {
361
  switch (Kind) {
362
  case UnknownObjectFormat: return "";
363
  case COFF: return "coff";
364
  case ELF: return "elf";
365
  case GOFF: return "goff";
366
  case MachO: return "macho";
367
  case Wasm: return "wasm";
368
  case XCOFF: return "xcoff";
369
  case DXContainer: return "dxcontainer";
370
  case SPIRV: return "spirv";
371
  }
372
  llvm_unreachable("unknown object format type");
373
}
374

375
static Triple::ArchType parseBPFArch(StringRef ArchName) {
376
  if (ArchName == "bpf") {
377
    if (sys::IsLittleEndianHost)
378
      return Triple::bpfel;
379
    else
380
      return Triple::bpfeb;
381
  } else if (ArchName == "bpf_be" || ArchName == "bpfeb") {
382
    return Triple::bpfeb;
383
  } else if (ArchName == "bpf_le" || ArchName == "bpfel") {
384
    return Triple::bpfel;
385
  } else {
386
    return Triple::UnknownArch;
387
  }
388
}
389

390
Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
391
  Triple::ArchType BPFArch(parseBPFArch(Name));
392
  return StringSwitch<Triple::ArchType>(Name)
393
    .Case("aarch64", aarch64)
394
    .Case("aarch64_be", aarch64_be)
395
    .Case("aarch64_32", aarch64_32)
396
    .Case("arc", arc)
397
    .Case("arm64", aarch64) // "arm64" is an alias for "aarch64"
398
    .Case("arm64_32", aarch64_32)
399
    .Case("arm", arm)
400
    .Case("armeb", armeb)
401
    .Case("avr", avr)
402
    .StartsWith("bpf", BPFArch)
403
    .Case("m68k", m68k)
404
    .Case("mips", mips)
405
    .Case("mipsel", mipsel)
406
    .Case("mips64", mips64)
407
    .Case("mips64el", mips64el)
408
    .Case("msp430", msp430)
409
    .Case("ppc64", ppc64)
410
    .Case("ppc32", ppc)
411
    .Case("ppc", ppc)
412
    .Case("ppc32le", ppcle)
413
    .Case("ppcle", ppcle)
414
    .Case("ppc64le", ppc64le)
415
    .Case("r600", r600)
416
    .Case("amdgcn", amdgcn)
417
    .Case("riscv32", riscv32)
418
    .Case("riscv64", riscv64)
419
    .Case("hexagon", hexagon)
420
    .Case("sparc", sparc)
421
    .Case("sparcel", sparcel)
422
    .Case("sparcv9", sparcv9)
423
    .Case("s390x", systemz)
424
    .Case("systemz", systemz)
425
    .Case("tce", tce)
426
    .Case("tcele", tcele)
427
    .Case("thumb", thumb)
428
    .Case("thumbeb", thumbeb)
429
    .Case("x86", x86)
430
    .Case("i386", x86)
431
    .Case("x86-64", x86_64)
432
    .Case("xcore", xcore)
433
    .Case("nvptx", nvptx)
434
    .Case("nvptx64", nvptx64)
435
    .Case("le32", le32)
436
    .Case("le64", le64)
437
    .Case("amdil", amdil)
438
    .Case("amdil64", amdil64)
439
    .Case("hsail", hsail)
440
    .Case("hsail64", hsail64)
441
    .Case("spir", spir)
442
    .Case("spir64", spir64)
443
    .Case("spirv", spirv)
444
    .Case("spirv32", spirv32)
445
    .Case("spirv64", spirv64)
446
    .Case("kalimba", kalimba)
447
    .Case("lanai", lanai)
448
    .Case("shave", shave)
449
    .Case("wasm32", wasm32)
450
    .Case("wasm64", wasm64)
451
    .Case("renderscript32", renderscript32)
452
    .Case("renderscript64", renderscript64)
453
    .Case("ve", ve)
454
    .Case("csky", csky)
455
    .Case("loongarch32", loongarch32)
456
    .Case("loongarch64", loongarch64)
457
    .Case("dxil", dxil)
458
    .Case("xtensa", xtensa)
459
    .Default(UnknownArch);
460
}
461

462
static Triple::ArchType parseARMArch(StringRef ArchName) {
463
  ARM::ISAKind ISA = ARM::parseArchISA(ArchName);
464
  ARM::EndianKind ENDIAN = ARM::parseArchEndian(ArchName);
465

466
  Triple::ArchType arch = Triple::UnknownArch;
467
  switch (ENDIAN) {
468
  case ARM::EndianKind::LITTLE: {
469
    switch (ISA) {
470
    case ARM::ISAKind::ARM:
471
      arch = Triple::arm;
472
      break;
473
    case ARM::ISAKind::THUMB:
474
      arch = Triple::thumb;
475
      break;
476
    case ARM::ISAKind::AARCH64:
477
      arch = Triple::aarch64;
478
      break;
479
    case ARM::ISAKind::INVALID:
480
      break;
481
    }
482
    break;
483
  }
484
  case ARM::EndianKind::BIG: {
485
    switch (ISA) {
486
    case ARM::ISAKind::ARM:
487
      arch = Triple::armeb;
488
      break;
489
    case ARM::ISAKind::THUMB:
490
      arch = Triple::thumbeb;
491
      break;
492
    case ARM::ISAKind::AARCH64:
493
      arch = Triple::aarch64_be;
494
      break;
495
    case ARM::ISAKind::INVALID:
496
      break;
497
    }
498
    break;
499
  }
500
  case ARM::EndianKind::INVALID: {
501
    break;
502
  }
503
  }
504

505
  ArchName = ARM::getCanonicalArchName(ArchName);
506
  if (ArchName.empty())
507
    return Triple::UnknownArch;
508

509
  // Thumb only exists in v4+
510
  if (ISA == ARM::ISAKind::THUMB &&
511
      (ArchName.starts_with("v2") || ArchName.starts_with("v3")))
512
    return Triple::UnknownArch;
513

514
  // Thumb only for v6m
515
  ARM::ProfileKind Profile = ARM::parseArchProfile(ArchName);
516
  unsigned Version = ARM::parseArchVersion(ArchName);
517
  if (Profile == ARM::ProfileKind::M && Version == 6) {
518
    if (ENDIAN == ARM::EndianKind::BIG)
519
      return Triple::thumbeb;
520
    else
521
      return Triple::thumb;
522
  }
523

524
  return arch;
525
}
526

527
static Triple::ArchType parseArch(StringRef ArchName) {
528
  auto AT =
529
      StringSwitch<Triple::ArchType>(ArchName)
530
          .Cases("i386", "i486", "i586", "i686", Triple::x86)
531
          // FIXME: Do we need to support these?
532
          .Cases("i786", "i886", "i986", Triple::x86)
533
          .Cases("amd64", "x86_64", "x86_64h", Triple::x86_64)
534
          .Cases("powerpc", "powerpcspe", "ppc", "ppc32", Triple::ppc)
535
          .Cases("powerpcle", "ppcle", "ppc32le", Triple::ppcle)
536
          .Cases("powerpc64", "ppu", "ppc64", Triple::ppc64)
537
          .Cases("powerpc64le", "ppc64le", Triple::ppc64le)
538
          .Case("xscale", Triple::arm)
539
          .Case("xscaleeb", Triple::armeb)
540
          .Case("aarch64", Triple::aarch64)
541
          .Case("aarch64_be", Triple::aarch64_be)
542
          .Case("aarch64_32", Triple::aarch64_32)
543
          .Case("arc", Triple::arc)
544
          .Case("arm64", Triple::aarch64)
545
          .Case("arm64_32", Triple::aarch64_32)
546
          .Case("arm64e", Triple::aarch64)
547
          .Case("arm64ec", Triple::aarch64)
548
          .Case("arm", Triple::arm)
549
          .Case("armeb", Triple::armeb)
550
          .Case("thumb", Triple::thumb)
551
          .Case("thumbeb", Triple::thumbeb)
552
          .Case("avr", Triple::avr)
553
          .Case("m68k", Triple::m68k)
554
          .Case("msp430", Triple::msp430)
555
          .Cases("mips", "mipseb", "mipsallegrex", "mipsisa32r6", "mipsr6",
556
                 Triple::mips)
557
          .Cases("mipsel", "mipsallegrexel", "mipsisa32r6el", "mipsr6el",
558
                 Triple::mipsel)
559
          .Cases("mips64", "mips64eb", "mipsn32", "mipsisa64r6", "mips64r6",
560
                 "mipsn32r6", Triple::mips64)
561
          .Cases("mips64el", "mipsn32el", "mipsisa64r6el", "mips64r6el",
562
                 "mipsn32r6el", Triple::mips64el)
563
          .Case("r600", Triple::r600)
564
          .Case("amdgcn", Triple::amdgcn)
565
          .Case("riscv32", Triple::riscv32)
566
          .Case("riscv64", Triple::riscv64)
567
          .Case("hexagon", Triple::hexagon)
568
          .Cases("s390x", "systemz", Triple::systemz)
569
          .Case("sparc", Triple::sparc)
570
          .Case("sparcel", Triple::sparcel)
571
          .Cases("sparcv9", "sparc64", Triple::sparcv9)
572
          .Case("tce", Triple::tce)
573
          .Case("tcele", Triple::tcele)
574
          .Case("xcore", Triple::xcore)
575
          .Case("nvptx", Triple::nvptx)
576
          .Case("nvptx64", Triple::nvptx64)
577
          .Case("le32", Triple::le32)
578
          .Case("le64", Triple::le64)
579
          .Case("amdil", Triple::amdil)
580
          .Case("amdil64", Triple::amdil64)
581
          .Case("hsail", Triple::hsail)
582
          .Case("hsail64", Triple::hsail64)
583
          .Case("spir", Triple::spir)
584
          .Case("spir64", Triple::spir64)
585
          .Cases("spirv", "spirv1.5", "spirv1.6", Triple::spirv)
586
          .Cases("spirv32", "spirv32v1.0", "spirv32v1.1", "spirv32v1.2",
587
            "spirv32v1.3", "spirv32v1.4", "spirv32v1.5",
588
            "spirv32v1.6", Triple::spirv32)
589
          .Cases("spirv64", "spirv64v1.0", "spirv64v1.1", "spirv64v1.2",
590
            "spirv64v1.3", "spirv64v1.4", "spirv64v1.5",
591
            "spirv64v1.6", Triple::spirv64)
592
          .StartsWith("kalimba", Triple::kalimba)
593
          .Case("lanai", Triple::lanai)
594
          .Case("renderscript32", Triple::renderscript32)
595
          .Case("renderscript64", Triple::renderscript64)
596
          .Case("shave", Triple::shave)
597
          .Case("ve", Triple::ve)
598
          .Case("wasm32", Triple::wasm32)
599
          .Case("wasm64", Triple::wasm64)
600
          .Case("csky", Triple::csky)
601
          .Case("loongarch32", Triple::loongarch32)
602
          .Case("loongarch64", Triple::loongarch64)
603
          .Cases("dxil", "dxilv1.0", "dxilv1.1", "dxilv1.2", "dxilv1.3",
604
                 "dxilv1.4", "dxilv1.5", "dxilv1.6", "dxilv1.7", "dxilv1.8",
605
                 Triple::dxil)
606
          .Case("xtensa", Triple::xtensa)
607
          .Default(Triple::UnknownArch);
608

609
  // Some architectures require special parsing logic just to compute the
610
  // ArchType result.
611
  if (AT == Triple::UnknownArch) {
612
    if (ArchName.starts_with("arm") || ArchName.starts_with("thumb") ||
613
        ArchName.starts_with("aarch64"))
614
      return parseARMArch(ArchName);
615
    if (ArchName.starts_with("bpf"))
616
      return parseBPFArch(ArchName);
617
  }
618

619
  return AT;
620
}
621

622
static Triple::VendorType parseVendor(StringRef VendorName) {
623
  return StringSwitch<Triple::VendorType>(VendorName)
624
    .Case("apple", Triple::Apple)
625
    .Case("pc", Triple::PC)
626
    .Case("scei", Triple::SCEI)
627
    .Case("sie", Triple::SCEI)
628
    .Case("fsl", Triple::Freescale)
629
    .Case("ibm", Triple::IBM)
630
    .Case("img", Triple::ImaginationTechnologies)
631
    .Case("mti", Triple::MipsTechnologies)
632
    .Case("nvidia", Triple::NVIDIA)
633
    .Case("csr", Triple::CSR)
634
    .Case("amd", Triple::AMD)
635
    .Case("mesa", Triple::Mesa)
636
    .Case("suse", Triple::SUSE)
637
    .Case("oe", Triple::OpenEmbedded)
638
    .Default(Triple::UnknownVendor);
639
}
640

641
static Triple::OSType parseOS(StringRef OSName) {
642
  return StringSwitch<Triple::OSType>(OSName)
643
    .StartsWith("darwin", Triple::Darwin)
644
    .StartsWith("dragonfly", Triple::DragonFly)
645
    .StartsWith("freebsd", Triple::FreeBSD)
646
    .StartsWith("fuchsia", Triple::Fuchsia)
647
    .StartsWith("ios", Triple::IOS)
648
    .StartsWith("kfreebsd", Triple::KFreeBSD)
649
    .StartsWith("linux", Triple::Linux)
650
    .StartsWith("lv2", Triple::Lv2)
651
    .StartsWith("macos", Triple::MacOSX)
652
    .StartsWith("netbsd", Triple::NetBSD)
653
    .StartsWith("openbsd", Triple::OpenBSD)
654
    .StartsWith("solaris", Triple::Solaris)
655
    .StartsWith("uefi", Triple::UEFI)
656
    .StartsWith("win32", Triple::Win32)
657
    .StartsWith("windows", Triple::Win32)
658
    .StartsWith("zos", Triple::ZOS)
659
    .StartsWith("haiku", Triple::Haiku)
660
    .StartsWith("rtems", Triple::RTEMS)
661
    .StartsWith("nacl", Triple::NaCl)
662
    .StartsWith("aix", Triple::AIX)
663
    .StartsWith("cuda", Triple::CUDA)
664
    .StartsWith("nvcl", Triple::NVCL)
665
    .StartsWith("amdhsa", Triple::AMDHSA)
666
    .StartsWith("ps4", Triple::PS4)
667
    .StartsWith("ps5", Triple::PS5)
668
    .StartsWith("elfiamcu", Triple::ELFIAMCU)
669
    .StartsWith("tvos", Triple::TvOS)
670
    .StartsWith("watchos", Triple::WatchOS)
671
    .StartsWith("bridgeos", Triple::BridgeOS)
672
    .StartsWith("driverkit", Triple::DriverKit)
673
    .StartsWith("xros", Triple::XROS)
674
    .StartsWith("visionos", Triple::XROS)
675
    .StartsWith("mesa3d", Triple::Mesa3D)
676
    .StartsWith("amdpal", Triple::AMDPAL)
677
    .StartsWith("hermit", Triple::HermitCore)
678
    .StartsWith("hurd", Triple::Hurd)
679
    .StartsWith("wasi", Triple::WASI)
680
    .StartsWith("emscripten", Triple::Emscripten)
681
    .StartsWith("shadermodel", Triple::ShaderModel)
682
    .StartsWith("liteos", Triple::LiteOS)
683
    .StartsWith("serenity", Triple::Serenity)
684
    .StartsWith("vulkan", Triple::Vulkan)
685
    .Default(Triple::UnknownOS);
686
}
687

688
static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
689
  return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
690
      .StartsWith("eabihf", Triple::EABIHF)
691
      .StartsWith("eabi", Triple::EABI)
692
      .StartsWith("gnuabin32", Triple::GNUABIN32)
693
      .StartsWith("gnuabi64", Triple::GNUABI64)
694
      .StartsWith("gnueabihf", Triple::GNUEABIHF)
695
      .StartsWith("gnueabi", Triple::GNUEABI)
696
      .StartsWith("gnuf32", Triple::GNUF32)
697
      .StartsWith("gnuf64", Triple::GNUF64)
698
      .StartsWith("gnusf", Triple::GNUSF)
699
      .StartsWith("gnux32", Triple::GNUX32)
700
      .StartsWith("gnu_ilp32", Triple::GNUILP32)
701
      .StartsWith("code16", Triple::CODE16)
702
      .StartsWith("gnu", Triple::GNU)
703
      .StartsWith("android", Triple::Android)
704
      .StartsWith("musleabihf", Triple::MuslEABIHF)
705
      .StartsWith("musleabi", Triple::MuslEABI)
706
      .StartsWith("muslx32", Triple::MuslX32)
707
      .StartsWith("musl", Triple::Musl)
708
      .StartsWith("msvc", Triple::MSVC)
709
      .StartsWith("itanium", Triple::Itanium)
710
      .StartsWith("cygnus", Triple::Cygnus)
711
      .StartsWith("coreclr", Triple::CoreCLR)
712
      .StartsWith("simulator", Triple::Simulator)
713
      .StartsWith("macabi", Triple::MacABI)
714
      .StartsWith("pixel", Triple::Pixel)
715
      .StartsWith("vertex", Triple::Vertex)
716
      .StartsWith("geometry", Triple::Geometry)
717
      .StartsWith("hull", Triple::Hull)
718
      .StartsWith("domain", Triple::Domain)
719
      .StartsWith("compute", Triple::Compute)
720
      .StartsWith("library", Triple::Library)
721
      .StartsWith("raygeneration", Triple::RayGeneration)
722
      .StartsWith("intersection", Triple::Intersection)
723
      .StartsWith("anyhit", Triple::AnyHit)
724
      .StartsWith("closesthit", Triple::ClosestHit)
725
      .StartsWith("miss", Triple::Miss)
726
      .StartsWith("callable", Triple::Callable)
727
      .StartsWith("mesh", Triple::Mesh)
728
      .StartsWith("amplification", Triple::Amplification)
729
      .StartsWith("opencl", Triple::OpenCL)
730
      .StartsWith("ohos", Triple::OpenHOS)
731
      .Default(Triple::UnknownEnvironment);
732
}
733

734
static Triple::ObjectFormatType parseFormat(StringRef EnvironmentName) {
735
  return StringSwitch<Triple::ObjectFormatType>(EnvironmentName)
736
      // "xcoff" must come before "coff" because of the order-dependendent
737
      // pattern matching.
738
      .EndsWith("xcoff", Triple::XCOFF)
739
      .EndsWith("coff", Triple::COFF)
740
      .EndsWith("elf", Triple::ELF)
741
      .EndsWith("goff", Triple::GOFF)
742
      .EndsWith("macho", Triple::MachO)
743
      .EndsWith("wasm", Triple::Wasm)
744
      .EndsWith("spirv", Triple::SPIRV)
745
      .Default(Triple::UnknownObjectFormat);
746
}
747

748
static Triple::SubArchType parseSubArch(StringRef SubArchName) {
749
  if (SubArchName.starts_with("mips") &&
750
      (SubArchName.ends_with("r6el") || SubArchName.ends_with("r6")))
751
    return Triple::MipsSubArch_r6;
752

753
  if (SubArchName == "powerpcspe")
754
    return Triple::PPCSubArch_spe;
755

756
  if (SubArchName == "arm64e")
757
    return Triple::AArch64SubArch_arm64e;
758

759
  if (SubArchName == "arm64ec")
760
    return Triple::AArch64SubArch_arm64ec;
761

762
  if (SubArchName.starts_with("spirv"))
763
    return StringSwitch<Triple::SubArchType>(SubArchName)
764
        .EndsWith("v1.0", Triple::SPIRVSubArch_v10)
765
        .EndsWith("v1.1", Triple::SPIRVSubArch_v11)
766
        .EndsWith("v1.2", Triple::SPIRVSubArch_v12)
767
        .EndsWith("v1.3", Triple::SPIRVSubArch_v13)
768
        .EndsWith("v1.4", Triple::SPIRVSubArch_v14)
769
        .EndsWith("v1.5", Triple::SPIRVSubArch_v15)
770
        .EndsWith("v1.6", Triple::SPIRVSubArch_v16)
771
        .Default(Triple::NoSubArch);
772

773
  if (SubArchName.starts_with("dxil"))
774
    return StringSwitch<Triple::SubArchType>(SubArchName)
775
        .EndsWith("v1.0", Triple::DXILSubArch_v1_0)
776
        .EndsWith("v1.1", Triple::DXILSubArch_v1_1)
777
        .EndsWith("v1.2", Triple::DXILSubArch_v1_2)
778
        .EndsWith("v1.3", Triple::DXILSubArch_v1_3)
779
        .EndsWith("v1.4", Triple::DXILSubArch_v1_4)
780
        .EndsWith("v1.5", Triple::DXILSubArch_v1_5)
781
        .EndsWith("v1.6", Triple::DXILSubArch_v1_6)
782
        .EndsWith("v1.7", Triple::DXILSubArch_v1_7)
783
        .EndsWith("v1.8", Triple::DXILSubArch_v1_8)
784
        .Default(Triple::NoSubArch);
785

786
  StringRef ARMSubArch = ARM::getCanonicalArchName(SubArchName);
787

788
  // For now, this is the small part. Early return.
789
  if (ARMSubArch.empty())
790
    return StringSwitch<Triple::SubArchType>(SubArchName)
791
      .EndsWith("kalimba3", Triple::KalimbaSubArch_v3)
792
      .EndsWith("kalimba4", Triple::KalimbaSubArch_v4)
793
      .EndsWith("kalimba5", Triple::KalimbaSubArch_v5)
794
      .Default(Triple::NoSubArch);
795

796
  // ARM sub arch.
797
  switch(ARM::parseArch(ARMSubArch)) {
798
  case ARM::ArchKind::ARMV4:
799
    return Triple::NoSubArch;
800
  case ARM::ArchKind::ARMV4T:
801
    return Triple::ARMSubArch_v4t;
802
  case ARM::ArchKind::ARMV5T:
803
    return Triple::ARMSubArch_v5;
804
  case ARM::ArchKind::ARMV5TE:
805
  case ARM::ArchKind::IWMMXT:
806
  case ARM::ArchKind::IWMMXT2:
807
  case ARM::ArchKind::XSCALE:
808
  case ARM::ArchKind::ARMV5TEJ:
809
    return Triple::ARMSubArch_v5te;
810
  case ARM::ArchKind::ARMV6:
811
    return Triple::ARMSubArch_v6;
812
  case ARM::ArchKind::ARMV6K:
813
  case ARM::ArchKind::ARMV6KZ:
814
    return Triple::ARMSubArch_v6k;
815
  case ARM::ArchKind::ARMV6T2:
816
    return Triple::ARMSubArch_v6t2;
817
  case ARM::ArchKind::ARMV6M:
818
    return Triple::ARMSubArch_v6m;
819
  case ARM::ArchKind::ARMV7A:
820
  case ARM::ArchKind::ARMV7R:
821
    return Triple::ARMSubArch_v7;
822
  case ARM::ArchKind::ARMV7VE:
823
    return Triple::ARMSubArch_v7ve;
824
  case ARM::ArchKind::ARMV7K:
825
    return Triple::ARMSubArch_v7k;
826
  case ARM::ArchKind::ARMV7M:
827
    return Triple::ARMSubArch_v7m;
828
  case ARM::ArchKind::ARMV7S:
829
    return Triple::ARMSubArch_v7s;
830
  case ARM::ArchKind::ARMV7EM:
831
    return Triple::ARMSubArch_v7em;
832
  case ARM::ArchKind::ARMV8A:
833
    return Triple::ARMSubArch_v8;
834
  case ARM::ArchKind::ARMV8_1A:
835
    return Triple::ARMSubArch_v8_1a;
836
  case ARM::ArchKind::ARMV8_2A:
837
    return Triple::ARMSubArch_v8_2a;
838
  case ARM::ArchKind::ARMV8_3A:
839
    return Triple::ARMSubArch_v8_3a;
840
  case ARM::ArchKind::ARMV8_4A:
841
    return Triple::ARMSubArch_v8_4a;
842
  case ARM::ArchKind::ARMV8_5A:
843
    return Triple::ARMSubArch_v8_5a;
844
  case ARM::ArchKind::ARMV8_6A:
845
    return Triple::ARMSubArch_v8_6a;
846
  case ARM::ArchKind::ARMV8_7A:
847
    return Triple::ARMSubArch_v8_7a;
848
  case ARM::ArchKind::ARMV8_8A:
849
    return Triple::ARMSubArch_v8_8a;
850
  case ARM::ArchKind::ARMV8_9A:
851
    return Triple::ARMSubArch_v8_9a;
852
  case ARM::ArchKind::ARMV9A:
853
    return Triple::ARMSubArch_v9;
854
  case ARM::ArchKind::ARMV9_1A:
855
    return Triple::ARMSubArch_v9_1a;
856
  case ARM::ArchKind::ARMV9_2A:
857
    return Triple::ARMSubArch_v9_2a;
858
  case ARM::ArchKind::ARMV9_3A:
859
    return Triple::ARMSubArch_v9_3a;
860
  case ARM::ArchKind::ARMV9_4A:
861
    return Triple::ARMSubArch_v9_4a;
862
  case ARM::ArchKind::ARMV9_5A:
863
    return Triple::ARMSubArch_v9_5a;
864
  case ARM::ArchKind::ARMV8R:
865
    return Triple::ARMSubArch_v8r;
866
  case ARM::ArchKind::ARMV8MBaseline:
867
    return Triple::ARMSubArch_v8m_baseline;
868
  case ARM::ArchKind::ARMV8MMainline:
869
    return Triple::ARMSubArch_v8m_mainline;
870
  case ARM::ArchKind::ARMV8_1MMainline:
871
    return Triple::ARMSubArch_v8_1m_mainline;
872
  default:
873
    return Triple::NoSubArch;
874
  }
875
}
876

877
static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
878
  switch (T.getArch()) {
879
  case Triple::UnknownArch:
880
  case Triple::aarch64:
881
  case Triple::aarch64_32:
882
  case Triple::arm:
883
  case Triple::thumb:
884
  case Triple::x86:
885
  case Triple::x86_64:
886
    switch (T.getOS()) {
887
    case Triple::Win32:
888
    case Triple::UEFI:
889
      return Triple::COFF;
890
    default:
891
      return T.isOSDarwin() ? Triple::MachO : Triple::ELF;
892
    }
893
  case Triple::aarch64_be:
894
  case Triple::amdgcn:
895
  case Triple::amdil64:
896
  case Triple::amdil:
897
  case Triple::arc:
898
  case Triple::armeb:
899
  case Triple::avr:
900
  case Triple::bpfeb:
901
  case Triple::bpfel:
902
  case Triple::csky:
903
  case Triple::hexagon:
904
  case Triple::hsail64:
905
  case Triple::hsail:
906
  case Triple::kalimba:
907
  case Triple::lanai:
908
  case Triple::le32:
909
  case Triple::le64:
910
  case Triple::loongarch32:
911
  case Triple::loongarch64:
912
  case Triple::m68k:
913
  case Triple::mips64:
914
  case Triple::mips64el:
915
  case Triple::mips:
916
  case Triple::mipsel:
917
  case Triple::msp430:
918
  case Triple::nvptx64:
919
  case Triple::nvptx:
920
  case Triple::ppc64le:
921
  case Triple::ppcle:
922
  case Triple::r600:
923
  case Triple::renderscript32:
924
  case Triple::renderscript64:
925
  case Triple::riscv32:
926
  case Triple::riscv64:
927
  case Triple::shave:
928
  case Triple::sparc:
929
  case Triple::sparcel:
930
  case Triple::sparcv9:
931
  case Triple::spir64:
932
  case Triple::spir:
933
  case Triple::tce:
934
  case Triple::tcele:
935
  case Triple::thumbeb:
936
  case Triple::ve:
937
  case Triple::xcore:
938
  case Triple::xtensa:
939
    return Triple::ELF;
940

941
  case Triple::ppc64:
942
  case Triple::ppc:
943
    if (T.isOSAIX())
944
      return Triple::XCOFF;
945
    if (T.isOSDarwin())
946
      return Triple::MachO;
947
    return Triple::ELF;
948

949
  case Triple::systemz:
950
    if (T.isOSzOS())
951
      return Triple::GOFF;
952
    return Triple::ELF;
953

954
  case Triple::wasm32:
955
  case Triple::wasm64:
956
    return Triple::Wasm;
957

958
  case Triple::spirv:
959
  case Triple::spirv32:
960
  case Triple::spirv64:
961
    return Triple::SPIRV;
962

963
  case Triple::dxil:
964
    return Triple::DXContainer;
965
  }
966
  llvm_unreachable("unknown architecture");
967
}
968

969
/// Construct a triple from the string representation provided.
970
///
971
/// This stores the string representation and parses the various pieces into
972
/// enum members.
973
Triple::Triple(const Twine &Str)
974
    : Data(Str.str()), Arch(UnknownArch), SubArch(NoSubArch),
975
      Vendor(UnknownVendor), OS(UnknownOS), Environment(UnknownEnvironment),
976
      ObjectFormat(UnknownObjectFormat) {
977
  // Do minimal parsing by hand here.
978
  SmallVector<StringRef, 4> Components;
979
  StringRef(Data).split(Components, '-', /*MaxSplit*/ 3);
980
  if (Components.size() > 0) {
981
    Arch = parseArch(Components[0]);
982
    SubArch = parseSubArch(Components[0]);
983
    if (Components.size() > 1) {
984
      Vendor = parseVendor(Components[1]);
985
      if (Components.size() > 2) {
986
        OS = parseOS(Components[2]);
987
        if (Components.size() > 3) {
988
          Environment = parseEnvironment(Components[3]);
989
          ObjectFormat = parseFormat(Components[3]);
990
        }
991
      }
992
    } else {
993
      Environment =
994
          StringSwitch<Triple::EnvironmentType>(Components[0])
995
              .StartsWith("mipsn32", Triple::GNUABIN32)
996
              .StartsWith("mips64", Triple::GNUABI64)
997
              .StartsWith("mipsisa64", Triple::GNUABI64)
998
              .StartsWith("mipsisa32", Triple::GNU)
999
              .Cases("mips", "mipsel", "mipsr6", "mipsr6el", Triple::GNU)
1000
              .Default(UnknownEnvironment);
1001
    }
1002
  }
1003
  if (ObjectFormat == UnknownObjectFormat)
1004
    ObjectFormat = getDefaultFormat(*this);
1005
}
1006

1007
/// Construct a triple from string representations of the architecture,
1008
/// vendor, and OS.
1009
///
1010
/// This joins each argument into a canonical string representation and parses
1011
/// them into enum members. It leaves the environment unknown and omits it from
1012
/// the string representation.
1013
Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
1014
    : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
1015
      Arch(parseArch(ArchStr.str())),
1016
      SubArch(parseSubArch(ArchStr.str())),
1017
      Vendor(parseVendor(VendorStr.str())),
1018
      OS(parseOS(OSStr.str())),
1019
      Environment(), ObjectFormat(Triple::UnknownObjectFormat) {
1020
  ObjectFormat = getDefaultFormat(*this);
1021
}
1022

1023
/// Construct a triple from string representations of the architecture,
1024
/// vendor, OS, and environment.
1025
///
1026
/// This joins each argument into a canonical string representation and parses
1027
/// them into enum members.
1028
Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
1029
               const Twine &EnvironmentStr)
1030
    : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
1031
            EnvironmentStr).str()),
1032
      Arch(parseArch(ArchStr.str())),
1033
      SubArch(parseSubArch(ArchStr.str())),
1034
      Vendor(parseVendor(VendorStr.str())),
1035
      OS(parseOS(OSStr.str())),
1036
      Environment(parseEnvironment(EnvironmentStr.str())),
1037
      ObjectFormat(parseFormat(EnvironmentStr.str())) {
1038
  if (ObjectFormat == Triple::UnknownObjectFormat)
1039
    ObjectFormat = getDefaultFormat(*this);
1040
}
1041

1042
static VersionTuple parseVersionFromName(StringRef Name);
1043

1044
static StringRef getDXILArchNameFromShaderModel(StringRef ShaderModelStr) {
1045
  VersionTuple Ver =
1046
      parseVersionFromName(ShaderModelStr.drop_front(strlen("shadermodel")));
1047
  // Default DXIL minor version when Shader Model version is anything other
1048
  // than 6.[0...8] or 6.x (which translates to latest current SM version)
1049
  const unsigned SMMajor = 6;
1050
  if (!Ver.empty()) {
1051
    if (Ver.getMajor() == SMMajor) {
1052
      if (std::optional<unsigned> SMMinor = Ver.getMinor()) {
1053
        switch (*SMMinor) {
1054
        case 0:
1055
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_0);
1056
        case 1:
1057
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_1);
1058
        case 2:
1059
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_2);
1060
        case 3:
1061
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_3);
1062
        case 4:
1063
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_4);
1064
        case 5:
1065
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_5);
1066
        case 6:
1067
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_6);
1068
        case 7:
1069
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_7);
1070
        case 8:
1071
          return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_8);
1072
        default:
1073
          report_fatal_error("Unsupported Shader Model version", false);
1074
        }
1075
      }
1076
    }
1077
  } else {
1078
    // Special case: DXIL minor version is set to LatestCurrentDXILMinor for
1079
    // shadermodel6.x is
1080
    if (ShaderModelStr == "shadermodel6.x") {
1081
      return Triple::getArchName(Triple::dxil, Triple::LatestDXILSubArch);
1082
    }
1083
  }
1084
  // DXIL version corresponding to Shader Model version other than 6.Minor
1085
  // is 1.0
1086
  return Triple::getArchName(Triple::dxil, Triple::DXILSubArch_v1_0);
1087
}
1088

1089
std::string Triple::normalize(StringRef Str) {
1090
  bool IsMinGW32 = false;
1091
  bool IsCygwin = false;
1092

1093
  // Parse into components.
1094
  SmallVector<StringRef, 4> Components;
1095
  Str.split(Components, '-');
1096

1097
  // If the first component corresponds to a known architecture, preferentially
1098
  // use it for the architecture.  If the second component corresponds to a
1099
  // known vendor, preferentially use it for the vendor, etc.  This avoids silly
1100
  // component movement when a component parses as (eg) both a valid arch and a
1101
  // valid os.
1102
  ArchType Arch = UnknownArch;
1103
  if (Components.size() > 0)
1104
    Arch = parseArch(Components[0]);
1105
  VendorType Vendor = UnknownVendor;
1106
  if (Components.size() > 1)
1107
    Vendor = parseVendor(Components[1]);
1108
  OSType OS = UnknownOS;
1109
  if (Components.size() > 2) {
1110
    OS = parseOS(Components[2]);
1111
    IsCygwin = Components[2].starts_with("cygwin");
1112
    IsMinGW32 = Components[2].starts_with("mingw");
1113
  }
1114
  EnvironmentType Environment = UnknownEnvironment;
1115
  if (Components.size() > 3)
1116
    Environment = parseEnvironment(Components[3]);
1117
  ObjectFormatType ObjectFormat = UnknownObjectFormat;
1118
  if (Components.size() > 4)
1119
    ObjectFormat = parseFormat(Components[4]);
1120

1121
  // Note which components are already in their final position.  These will not
1122
  // be moved.
1123
  bool Found[4];
1124
  Found[0] = Arch != UnknownArch;
1125
  Found[1] = Vendor != UnknownVendor;
1126
  Found[2] = OS != UnknownOS;
1127
  Found[3] = Environment != UnknownEnvironment;
1128

1129
  // If they are not there already, permute the components into their canonical
1130
  // positions by seeing if they parse as a valid architecture, and if so moving
1131
  // the component to the architecture position etc.
1132
  for (unsigned Pos = 0; Pos != std::size(Found); ++Pos) {
1133
    if (Found[Pos])
1134
      continue; // Already in the canonical position.
1135

1136
    for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
1137
      // Do not reparse any components that already matched.
1138
      if (Idx < std::size(Found) && Found[Idx])
1139
        continue;
1140

1141
      // Does this component parse as valid for the target position?
1142
      bool Valid = false;
1143
      StringRef Comp = Components[Idx];
1144
      switch (Pos) {
1145
      default: llvm_unreachable("unexpected component type!");
1146
      case 0:
1147
        Arch = parseArch(Comp);
1148
        Valid = Arch != UnknownArch;
1149
        break;
1150
      case 1:
1151
        Vendor = parseVendor(Comp);
1152
        Valid = Vendor != UnknownVendor;
1153
        break;
1154
      case 2:
1155
        OS = parseOS(Comp);
1156
        IsCygwin = Comp.starts_with("cygwin");
1157
        IsMinGW32 = Comp.starts_with("mingw");
1158
        Valid = OS != UnknownOS || IsCygwin || IsMinGW32;
1159
        break;
1160
      case 3:
1161
        Environment = parseEnvironment(Comp);
1162
        Valid = Environment != UnknownEnvironment;
1163
        if (!Valid) {
1164
          ObjectFormat = parseFormat(Comp);
1165
          Valid = ObjectFormat != UnknownObjectFormat;
1166
        }
1167
        break;
1168
      }
1169
      if (!Valid)
1170
        continue; // Nope, try the next component.
1171

1172
      // Move the component to the target position, pushing any non-fixed
1173
      // components that are in the way to the right.  This tends to give
1174
      // good results in the common cases of a forgotten vendor component
1175
      // or a wrongly positioned environment.
1176
      if (Pos < Idx) {
1177
        // Insert left, pushing the existing components to the right.  For
1178
        // example, a-b-i386 -> i386-a-b when moving i386 to the front.
1179
        StringRef CurrentComponent(""); // The empty component.
1180
        // Replace the component we are moving with an empty component.
1181
        std::swap(CurrentComponent, Components[Idx]);
1182
        // Insert the component being moved at Pos, displacing any existing
1183
        // components to the right.
1184
        for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
1185
          // Skip over any fixed components.
1186
          while (i < std::size(Found) && Found[i])
1187
            ++i;
1188
          // Place the component at the new position, getting the component
1189
          // that was at this position - it will be moved right.
1190
          std::swap(CurrentComponent, Components[i]);
1191
        }
1192
      } else if (Pos > Idx) {
1193
        // Push right by inserting empty components until the component at Idx
1194
        // reaches the target position Pos.  For example, pc-a -> -pc-a when
1195
        // moving pc to the second position.
1196
        do {
1197
          // Insert one empty component at Idx.
1198
          StringRef CurrentComponent(""); // The empty component.
1199
          for (unsigned i = Idx; i < Components.size();) {
1200
            // Place the component at the new position, getting the component
1201
            // that was at this position - it will be moved right.
1202
            std::swap(CurrentComponent, Components[i]);
1203
            // If it was placed on top of an empty component then we are done.
1204
            if (CurrentComponent.empty())
1205
              break;
1206
            // Advance to the next component, skipping any fixed components.
1207
            while (++i < std::size(Found) && Found[i])
1208
              ;
1209
          }
1210
          // The last component was pushed off the end - append it.
1211
          if (!CurrentComponent.empty())
1212
            Components.push_back(CurrentComponent);
1213

1214
          // Advance Idx to the component's new position.
1215
          while (++Idx < std::size(Found) && Found[Idx])
1216
            ;
1217
        } while (Idx < Pos); // Add more until the final position is reached.
1218
      }
1219
      assert(Pos < Components.size() && Components[Pos] == Comp &&
1220
             "Component moved wrong!");
1221
      Found[Pos] = true;
1222
      break;
1223
    }
1224
  }
1225

1226
  // If "none" is in the middle component in a three-component triple, treat it
1227
  // as the OS (Components[2]) instead of the vendor (Components[1]).
1228
  if (Found[0] && !Found[1] && !Found[2] && Found[3] &&
1229
      Components[1] == "none" && Components[2].empty())
1230
    std::swap(Components[1], Components[2]);
1231

1232
  // Replace empty components with "unknown" value.
1233
  for (StringRef &C : Components)
1234
    if (C.empty())
1235
      C = "unknown";
1236

1237
  // Special case logic goes here.  At this point Arch, Vendor and OS have the
1238
  // correct values for the computed components.
1239
  std::string NormalizedEnvironment;
1240
  if (Environment == Triple::Android &&
1241
      Components[3].starts_with("androideabi")) {
1242
    StringRef AndroidVersion = Components[3].drop_front(strlen("androideabi"));
1243
    if (AndroidVersion.empty()) {
1244
      Components[3] = "android";
1245
    } else {
1246
      NormalizedEnvironment = Twine("android", AndroidVersion).str();
1247
      Components[3] = NormalizedEnvironment;
1248
    }
1249
  }
1250

1251
  // SUSE uses "gnueabi" to mean "gnueabihf"
1252
  if (Vendor == Triple::SUSE && Environment == llvm::Triple::GNUEABI)
1253
    Components[3] = "gnueabihf";
1254

1255
  if (OS == Triple::Win32) {
1256
    Components.resize(4);
1257
    Components[2] = "windows";
1258
    if (Environment == UnknownEnvironment) {
1259
      if (ObjectFormat == UnknownObjectFormat || ObjectFormat == Triple::COFF)
1260
        Components[3] = "msvc";
1261
      else
1262
        Components[3] = getObjectFormatTypeName(ObjectFormat);
1263
    }
1264
  } else if (IsMinGW32) {
1265
    Components.resize(4);
1266
    Components[2] = "windows";
1267
    Components[3] = "gnu";
1268
  } else if (IsCygwin) {
1269
    Components.resize(4);
1270
    Components[2] = "windows";
1271
    Components[3] = "cygnus";
1272
  }
1273
  if (IsMinGW32 || IsCygwin ||
1274
      (OS == Triple::Win32 && Environment != UnknownEnvironment)) {
1275
    if (ObjectFormat != UnknownObjectFormat && ObjectFormat != Triple::COFF) {
1276
      Components.resize(5);
1277
      Components[4] = getObjectFormatTypeName(ObjectFormat);
1278
    }
1279
  }
1280

1281
  // Normalize DXIL triple if it does not include DXIL version number.
1282
  // Determine DXIL version number using the minor version number of Shader
1283
  // Model version specified in target triple, if any. Prior to decoupling DXIL
1284
  // version numbering from that of Shader Model DXIL version 1.Y corresponds to
1285
  // SM 6.Y. E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
1286
  if (Components[0] == "dxil") {
1287
    if (Components.size() > 4) {
1288
      Components.resize(4);
1289
    }
1290
    // Add DXIL version only if shadermodel is specified in the triple
1291
    if (OS == Triple::ShaderModel) {
1292
      Components[0] = getDXILArchNameFromShaderModel(Components[2]);
1293
    }
1294
  }
1295
  // Stick the corrected components back together to form the normalized string.
1296
  return join(Components, "-");
1297
}
1298

1299
StringRef Triple::getArchName() const {
1300
  return StringRef(Data).split('-').first;           // Isolate first component
1301
}
1302

1303
StringRef Triple::getVendorName() const {
1304
  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1305
  return Tmp.split('-').first;                       // Isolate second component
1306
}
1307

1308
StringRef Triple::getOSName() const {
1309
  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1310
  Tmp = Tmp.split('-').second;                       // Strip second component
1311
  return Tmp.split('-').first;                       // Isolate third component
1312
}
1313

1314
StringRef Triple::getEnvironmentName() const {
1315
  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1316
  Tmp = Tmp.split('-').second;                       // Strip second component
1317
  return Tmp.split('-').second;                      // Strip third component
1318
}
1319

1320
StringRef Triple::getOSAndEnvironmentName() const {
1321
  StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
1322
  return Tmp.split('-').second;                      // Strip second component
1323
}
1324

1325
static VersionTuple parseVersionFromName(StringRef Name) {
1326
  VersionTuple Version;
1327
  Version.tryParse(Name);
1328
  return Version.withoutBuild();
1329
}
1330

1331
VersionTuple Triple::getEnvironmentVersion() const {
1332
  return parseVersionFromName(getEnvironmentVersionString());
1333
}
1334

1335
StringRef Triple::getEnvironmentVersionString() const {
1336
  StringRef EnvironmentName = getEnvironmentName();
1337

1338
  // none is a valid environment type - it basically amounts to a freestanding
1339
  // environment.
1340
  if (EnvironmentName == "none")
1341
    return "";
1342

1343
  StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
1344
  EnvironmentName.consume_front(EnvironmentTypeName);
1345

1346
  if (EnvironmentName.contains("-")) {
1347
    // -obj is the suffix
1348
    if (getObjectFormat() != Triple::UnknownObjectFormat) {
1349
      StringRef ObjectFormatTypeName =
1350
          getObjectFormatTypeName(getObjectFormat());
1351
      const std::string tmp = (Twine("-") + ObjectFormatTypeName).str();
1352
      EnvironmentName.consume_back(tmp);
1353
    }
1354
  }
1355
  return EnvironmentName;
1356
}
1357

1358
VersionTuple Triple::getOSVersion() const {
1359
  StringRef OSName = getOSName();
1360
  // Assume that the OS portion of the triple starts with the canonical name.
1361
  StringRef OSTypeName = getOSTypeName(getOS());
1362
  if (OSName.starts_with(OSTypeName))
1363
    OSName = OSName.substr(OSTypeName.size());
1364
  else if (getOS() == MacOSX)
1365
    OSName.consume_front("macos");
1366
  else if (OSName.starts_with("visionos"))
1367
    OSName.consume_front("visionos");
1368

1369
  return parseVersionFromName(OSName);
1370
}
1371

1372
bool Triple::getMacOSXVersion(VersionTuple &Version) const {
1373
  Version = getOSVersion();
1374

1375
  switch (getOS()) {
1376
  default: llvm_unreachable("unexpected OS for Darwin triple");
1377
  case Darwin:
1378
    // Default to darwin8, i.e., MacOSX 10.4.
1379
    if (Version.getMajor() == 0)
1380
      Version = VersionTuple(8);
1381
    // Darwin version numbers are skewed from OS X versions.
1382
    if (Version.getMajor() < 4) {
1383
      return false;
1384
    }
1385
    if (Version.getMajor() <= 19) {
1386
      Version = VersionTuple(10, Version.getMajor() - 4);
1387
    } else {
1388
      // darwin20+ corresponds to macOS 11+.
1389
      Version = VersionTuple(11 + Version.getMajor() - 20);
1390
    }
1391
    break;
1392
  case MacOSX:
1393
    // Default to 10.4.
1394
    if (Version.getMajor() == 0) {
1395
      Version = VersionTuple(10, 4);
1396
    } else if (Version.getMajor() < 10) {
1397
      return false;
1398
    }
1399
    break;
1400
  case IOS:
1401
  case TvOS:
1402
  case WatchOS:
1403
    // Ignore the version from the triple.  This is only handled because the
1404
    // the clang driver combines OS X and IOS support into a common Darwin
1405
    // toolchain that wants to know the OS X version number even when targeting
1406
    // IOS.
1407
    Version = VersionTuple(10, 4);
1408
    break;
1409
  case XROS:
1410
    llvm_unreachable("OSX version isn't relevant for xrOS");
1411
  case DriverKit:
1412
    llvm_unreachable("OSX version isn't relevant for DriverKit");
1413
  }
1414
  return true;
1415
}
1416

1417
VersionTuple Triple::getiOSVersion() const {
1418
  switch (getOS()) {
1419
  default: llvm_unreachable("unexpected OS for Darwin triple");
1420
  case Darwin:
1421
  case MacOSX:
1422
    // Ignore the version from the triple.  This is only handled because the
1423
    // the clang driver combines OS X and IOS support into a common Darwin
1424
    // toolchain that wants to know the iOS version number even when targeting
1425
    // OS X.
1426
    return VersionTuple(5);
1427
  case IOS:
1428
  case TvOS: {
1429
    VersionTuple Version = getOSVersion();
1430
    // Default to 5.0 (or 7.0 for arm64).
1431
    if (Version.getMajor() == 0)
1432
      return (getArch() == aarch64) ? VersionTuple(7) : VersionTuple(5);
1433
    return Version;
1434
  }
1435
  case XROS: {
1436
    // xrOS 1 is aligned with iOS 17.
1437
    VersionTuple Version = getOSVersion();
1438
    return Version.withMajorReplaced(Version.getMajor() + 16);
1439
  }
1440
  case WatchOS:
1441
    llvm_unreachable("conflicting triple info");
1442
  case DriverKit:
1443
    llvm_unreachable("DriverKit doesn't have an iOS version");
1444
  }
1445
}
1446

1447
VersionTuple Triple::getWatchOSVersion() const {
1448
  switch (getOS()) {
1449
  default: llvm_unreachable("unexpected OS for Darwin triple");
1450
  case Darwin:
1451
  case MacOSX:
1452
    // Ignore the version from the triple.  This is only handled because the
1453
    // the clang driver combines OS X and IOS support into a common Darwin
1454
    // toolchain that wants to know the iOS version number even when targeting
1455
    // OS X.
1456
    return VersionTuple(2);
1457
  case WatchOS: {
1458
    VersionTuple Version = getOSVersion();
1459
    if (Version.getMajor() == 0)
1460
      return VersionTuple(2);
1461
    return Version;
1462
  }
1463
  case IOS:
1464
    llvm_unreachable("conflicting triple info");
1465
  case XROS:
1466
    llvm_unreachable("watchOS version isn't relevant for xrOS");
1467
  case DriverKit:
1468
    llvm_unreachable("DriverKit doesn't have a WatchOS version");
1469
  }
1470
}
1471

1472
VersionTuple Triple::getDriverKitVersion() const {
1473
  switch (getOS()) {
1474
  default:
1475
    llvm_unreachable("unexpected OS for Darwin triple");
1476
  case DriverKit:
1477
    VersionTuple Version = getOSVersion();
1478
    if (Version.getMajor() == 0)
1479
      return Version.withMajorReplaced(19);
1480
    return Version;
1481
  }
1482
}
1483

1484
VersionTuple Triple::getVulkanVersion() const {
1485
  if (getArch() != spirv || getOS() != Vulkan)
1486
    llvm_unreachable("invalid Vulkan SPIR-V triple");
1487

1488
  VersionTuple VulkanVersion = getOSVersion();
1489
  SubArchType SpirvVersion = getSubArch();
1490

1491
  llvm::DenseMap<VersionTuple, SubArchType> ValidVersionMap = {
1492
      // Vulkan 1.2 -> SPIR-V 1.5.
1493
      {VersionTuple(1, 2), SPIRVSubArch_v15},
1494
      // Vulkan 1.3 -> SPIR-V 1.6.
1495
      {VersionTuple(1, 3), SPIRVSubArch_v16}};
1496

1497
  // If Vulkan version is unset, default to 1.2.
1498
  if (VulkanVersion == VersionTuple(0))
1499
    VulkanVersion = VersionTuple(1, 2);
1500

1501
  if (ValidVersionMap.contains(VulkanVersion) &&
1502
      (ValidVersionMap.lookup(VulkanVersion) == SpirvVersion ||
1503
       SpirvVersion == NoSubArch))
1504
    return VulkanVersion;
1505

1506
  return VersionTuple(0);
1507
}
1508

1509
VersionTuple Triple::getDXILVersion() const {
1510
  if (getArch() != dxil || getOS() != ShaderModel)
1511
    llvm_unreachable("invalid DXIL triple");
1512
  StringRef Arch = getArchName();
1513
  if (getSubArch() == NoSubArch)
1514
    Arch = getDXILArchNameFromShaderModel(getOSName());
1515
  Arch.consume_front("dxilv");
1516
  VersionTuple DXILVersion = parseVersionFromName(Arch);
1517
  // FIXME: validate DXIL version against Shader Model version.
1518
  // Tracked by https://github.com/llvm/llvm-project/issues/91388
1519
  return DXILVersion;
1520
}
1521

1522
void Triple::setTriple(const Twine &Str) {
1523
  *this = Triple(Str);
1524
}
1525

1526
void Triple::setArch(ArchType Kind, SubArchType SubArch) {
1527
  setArchName(getArchName(Kind, SubArch));
1528
}
1529

1530
void Triple::setVendor(VendorType Kind) {
1531
  setVendorName(getVendorTypeName(Kind));
1532
}
1533

1534
void Triple::setOS(OSType Kind) {
1535
  setOSName(getOSTypeName(Kind));
1536
}
1537

1538
void Triple::setEnvironment(EnvironmentType Kind) {
1539
  if (ObjectFormat == getDefaultFormat(*this))
1540
    return setEnvironmentName(getEnvironmentTypeName(Kind));
1541

1542
  setEnvironmentName((getEnvironmentTypeName(Kind) + Twine("-") +
1543
                      getObjectFormatTypeName(ObjectFormat)).str());
1544
}
1545

1546
void Triple::setObjectFormat(ObjectFormatType Kind) {
1547
  if (Environment == UnknownEnvironment)
1548
    return setEnvironmentName(getObjectFormatTypeName(Kind));
1549

1550
  setEnvironmentName((getEnvironmentTypeName(Environment) + Twine("-") +
1551
                      getObjectFormatTypeName(Kind)).str());
1552
}
1553

1554
void Triple::setArchName(StringRef Str) {
1555
  // Work around a miscompilation bug for Twines in gcc 4.0.3.
1556
  SmallString<64> Triple;
1557
  Triple += Str;
1558
  Triple += "-";
1559
  Triple += getVendorName();
1560
  Triple += "-";
1561
  Triple += getOSAndEnvironmentName();
1562
  setTriple(Triple);
1563
}
1564

1565
void Triple::setVendorName(StringRef Str) {
1566
  setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
1567
}
1568

1569
void Triple::setOSName(StringRef Str) {
1570
  if (hasEnvironment())
1571
    setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
1572
              "-" + getEnvironmentName());
1573
  else
1574
    setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1575
}
1576

1577
void Triple::setEnvironmentName(StringRef Str) {
1578
  setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
1579
            "-" + Str);
1580
}
1581

1582
void Triple::setOSAndEnvironmentName(StringRef Str) {
1583
  setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
1584
}
1585

1586
unsigned Triple::getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
1587
  switch (Arch) {
1588
  case llvm::Triple::UnknownArch:
1589
    return 0;
1590

1591
  case llvm::Triple::avr:
1592
  case llvm::Triple::msp430:
1593
    return 16;
1594

1595
  case llvm::Triple::aarch64_32:
1596
  case llvm::Triple::amdil:
1597
  case llvm::Triple::arc:
1598
  case llvm::Triple::arm:
1599
  case llvm::Triple::armeb:
1600
  case llvm::Triple::csky:
1601
  case llvm::Triple::dxil:
1602
  case llvm::Triple::hexagon:
1603
  case llvm::Triple::hsail:
1604
  case llvm::Triple::kalimba:
1605
  case llvm::Triple::lanai:
1606
  case llvm::Triple::le32:
1607
  case llvm::Triple::loongarch32:
1608
  case llvm::Triple::m68k:
1609
  case llvm::Triple::mips:
1610
  case llvm::Triple::mipsel:
1611
  case llvm::Triple::nvptx:
1612
  case llvm::Triple::ppc:
1613
  case llvm::Triple::ppcle:
1614
  case llvm::Triple::r600:
1615
  case llvm::Triple::renderscript32:
1616
  case llvm::Triple::riscv32:
1617
  case llvm::Triple::shave:
1618
  case llvm::Triple::sparc:
1619
  case llvm::Triple::sparcel:
1620
  case llvm::Triple::spir:
1621
  case llvm::Triple::spirv32:
1622
  case llvm::Triple::tce:
1623
  case llvm::Triple::tcele:
1624
  case llvm::Triple::thumb:
1625
  case llvm::Triple::thumbeb:
1626
  case llvm::Triple::wasm32:
1627
  case llvm::Triple::x86:
1628
  case llvm::Triple::xcore:
1629
  case llvm::Triple::xtensa:
1630
    return 32;
1631

1632
  case llvm::Triple::aarch64:
1633
  case llvm::Triple::aarch64_be:
1634
  case llvm::Triple::amdgcn:
1635
  case llvm::Triple::amdil64:
1636
  case llvm::Triple::bpfeb:
1637
  case llvm::Triple::bpfel:
1638
  case llvm::Triple::hsail64:
1639
  case llvm::Triple::le64:
1640
  case llvm::Triple::loongarch64:
1641
  case llvm::Triple::mips64:
1642
  case llvm::Triple::mips64el:
1643
  case llvm::Triple::nvptx64:
1644
  case llvm::Triple::ppc64:
1645
  case llvm::Triple::ppc64le:
1646
  case llvm::Triple::renderscript64:
1647
  case llvm::Triple::riscv64:
1648
  case llvm::Triple::sparcv9:
1649
  case llvm::Triple::spirv:
1650
  case llvm::Triple::spir64:
1651
  case llvm::Triple::spirv64:
1652
  case llvm::Triple::systemz:
1653
  case llvm::Triple::ve:
1654
  case llvm::Triple::wasm64:
1655
  case llvm::Triple::x86_64:
1656
    return 64;
1657
  }
1658
  llvm_unreachable("Invalid architecture value");
1659
}
1660

1661
bool Triple::isArch64Bit() const {
1662
  return getArchPointerBitWidth(getArch()) == 64;
1663
}
1664

1665
bool Triple::isArch32Bit() const {
1666
  return getArchPointerBitWidth(getArch()) == 32;
1667
}
1668

1669
bool Triple::isArch16Bit() const {
1670
  return getArchPointerBitWidth(getArch()) == 16;
1671
}
1672

1673
Triple Triple::get32BitArchVariant() const {
1674
  Triple T(*this);
1675
  switch (getArch()) {
1676
  case Triple::UnknownArch:
1677
  case Triple::amdgcn:
1678
  case Triple::avr:
1679
  case Triple::bpfeb:
1680
  case Triple::bpfel:
1681
  case Triple::msp430:
1682
  case Triple::systemz:
1683
  case Triple::ve:
1684
    T.setArch(UnknownArch);
1685
    break;
1686

1687
  case Triple::aarch64_32:
1688
  case Triple::amdil:
1689
  case Triple::arc:
1690
  case Triple::arm:
1691
  case Triple::armeb:
1692
  case Triple::csky:
1693
  case Triple::dxil:
1694
  case Triple::hexagon:
1695
  case Triple::hsail:
1696
  case Triple::kalimba:
1697
  case Triple::lanai:
1698
  case Triple::le32:
1699
  case Triple::loongarch32:
1700
  case Triple::m68k:
1701
  case Triple::mips:
1702
  case Triple::mipsel:
1703
  case Triple::nvptx:
1704
  case Triple::ppc:
1705
  case Triple::ppcle:
1706
  case Triple::r600:
1707
  case Triple::renderscript32:
1708
  case Triple::riscv32:
1709
  case Triple::shave:
1710
  case Triple::sparc:
1711
  case Triple::sparcel:
1712
  case Triple::spir:
1713
  case Triple::spirv32:
1714
  case Triple::tce:
1715
  case Triple::tcele:
1716
  case Triple::thumb:
1717
  case Triple::thumbeb:
1718
  case Triple::wasm32:
1719
  case Triple::x86:
1720
  case Triple::xcore:
1721
  case Triple::xtensa:
1722
    // Already 32-bit.
1723
    break;
1724

1725
  case Triple::aarch64:        T.setArch(Triple::arm);     break;
1726
  case Triple::aarch64_be:     T.setArch(Triple::armeb);   break;
1727
  case Triple::amdil64:        T.setArch(Triple::amdil);   break;
1728
  case Triple::hsail64:        T.setArch(Triple::hsail);   break;
1729
  case Triple::le64:           T.setArch(Triple::le32);    break;
1730
  case Triple::loongarch64:    T.setArch(Triple::loongarch32); break;
1731
  case Triple::mips64:
1732
    T.setArch(Triple::mips, getSubArch());
1733
    break;
1734
  case Triple::mips64el:
1735
    T.setArch(Triple::mipsel, getSubArch());
1736
    break;
1737
  case Triple::nvptx64:        T.setArch(Triple::nvptx);   break;
1738
  case Triple::ppc64:          T.setArch(Triple::ppc);     break;
1739
  case Triple::ppc64le:        T.setArch(Triple::ppcle);   break;
1740
  case Triple::renderscript64: T.setArch(Triple::renderscript32); break;
1741
  case Triple::riscv64:        T.setArch(Triple::riscv32); break;
1742
  case Triple::sparcv9:        T.setArch(Triple::sparc);   break;
1743
  case Triple::spir64:         T.setArch(Triple::spir);    break;
1744
  case Triple::spirv:
1745
  case Triple::spirv64:
1746
    T.setArch(Triple::spirv32, getSubArch());
1747
    break;
1748
  case Triple::wasm64:         T.setArch(Triple::wasm32);  break;
1749
  case Triple::x86_64:         T.setArch(Triple::x86);     break;
1750
  }
1751
  return T;
1752
}
1753

1754
Triple Triple::get64BitArchVariant() const {
1755
  Triple T(*this);
1756
  switch (getArch()) {
1757
  case Triple::UnknownArch:
1758
  case Triple::arc:
1759
  case Triple::avr:
1760
  case Triple::csky:
1761
  case Triple::dxil:
1762
  case Triple::hexagon:
1763
  case Triple::kalimba:
1764
  case Triple::lanai:
1765
  case Triple::m68k:
1766
  case Triple::msp430:
1767
  case Triple::r600:
1768
  case Triple::shave:
1769
  case Triple::sparcel:
1770
  case Triple::tce:
1771
  case Triple::tcele:
1772
  case Triple::xcore:
1773
  case Triple::xtensa:
1774
    T.setArch(UnknownArch);
1775
    break;
1776

1777
  case Triple::aarch64:
1778
  case Triple::aarch64_be:
1779
  case Triple::amdgcn:
1780
  case Triple::amdil64:
1781
  case Triple::bpfeb:
1782
  case Triple::bpfel:
1783
  case Triple::hsail64:
1784
  case Triple::le64:
1785
  case Triple::loongarch64:
1786
  case Triple::mips64:
1787
  case Triple::mips64el:
1788
  case Triple::nvptx64:
1789
  case Triple::ppc64:
1790
  case Triple::ppc64le:
1791
  case Triple::renderscript64:
1792
  case Triple::riscv64:
1793
  case Triple::sparcv9:
1794
  case Triple::spir64:
1795
  case Triple::spirv64:
1796
  case Triple::systemz:
1797
  case Triple::ve:
1798
  case Triple::wasm64:
1799
  case Triple::x86_64:
1800
    // Already 64-bit.
1801
    break;
1802

1803
  case Triple::aarch64_32:      T.setArch(Triple::aarch64);    break;
1804
  case Triple::amdil:           T.setArch(Triple::amdil64);    break;
1805
  case Triple::arm:             T.setArch(Triple::aarch64);    break;
1806
  case Triple::armeb:           T.setArch(Triple::aarch64_be); break;
1807
  case Triple::hsail:           T.setArch(Triple::hsail64);    break;
1808
  case Triple::le32:            T.setArch(Triple::le64);       break;
1809
  case Triple::loongarch32:     T.setArch(Triple::loongarch64);    break;
1810
  case Triple::mips:
1811
    T.setArch(Triple::mips64, getSubArch());
1812
    break;
1813
  case Triple::mipsel:
1814
    T.setArch(Triple::mips64el, getSubArch());
1815
    break;
1816
  case Triple::nvptx:           T.setArch(Triple::nvptx64);    break;
1817
  case Triple::ppc:             T.setArch(Triple::ppc64);      break;
1818
  case Triple::ppcle:           T.setArch(Triple::ppc64le);    break;
1819
  case Triple::renderscript32:  T.setArch(Triple::renderscript64);     break;
1820
  case Triple::riscv32:         T.setArch(Triple::riscv64);    break;
1821
  case Triple::sparc:           T.setArch(Triple::sparcv9);    break;
1822
  case Triple::spir:            T.setArch(Triple::spir64);     break;
1823
  case Triple::spirv:
1824
  case Triple::spirv32:
1825
    T.setArch(Triple::spirv64, getSubArch());
1826
    break;
1827
  case Triple::thumb:           T.setArch(Triple::aarch64);    break;
1828
  case Triple::thumbeb:         T.setArch(Triple::aarch64_be); break;
1829
  case Triple::wasm32:          T.setArch(Triple::wasm64);     break;
1830
  case Triple::x86:             T.setArch(Triple::x86_64);     break;
1831
  }
1832
  return T;
1833
}
1834

1835
Triple Triple::getBigEndianArchVariant() const {
1836
  Triple T(*this);
1837
  // Already big endian.
1838
  if (!isLittleEndian())
1839
    return T;
1840
  switch (getArch()) {
1841
  case Triple::UnknownArch:
1842
  case Triple::amdgcn:
1843
  case Triple::amdil64:
1844
  case Triple::amdil:
1845
  case Triple::avr:
1846
  case Triple::dxil:
1847
  case Triple::hexagon:
1848
  case Triple::hsail64:
1849
  case Triple::hsail:
1850
  case Triple::kalimba:
1851
  case Triple::le32:
1852
  case Triple::le64:
1853
  case Triple::loongarch32:
1854
  case Triple::loongarch64:
1855
  case Triple::msp430:
1856
  case Triple::nvptx64:
1857
  case Triple::nvptx:
1858
  case Triple::r600:
1859
  case Triple::renderscript32:
1860
  case Triple::renderscript64:
1861
  case Triple::riscv32:
1862
  case Triple::riscv64:
1863
  case Triple::shave:
1864
  case Triple::spir64:
1865
  case Triple::spir:
1866
  case Triple::spirv:
1867
  case Triple::spirv32:
1868
  case Triple::spirv64:
1869
  case Triple::wasm32:
1870
  case Triple::wasm64:
1871
  case Triple::x86:
1872
  case Triple::x86_64:
1873
  case Triple::xcore:
1874
  case Triple::ve:
1875
  case Triple::csky:
1876
  case Triple::xtensa:
1877

1878
  // ARM is intentionally unsupported here, changing the architecture would
1879
  // drop any arch suffixes.
1880
  case Triple::arm:
1881
  case Triple::thumb:
1882
    T.setArch(UnknownArch);
1883
    break;
1884

1885
  case Triple::aarch64: T.setArch(Triple::aarch64_be); break;
1886
  case Triple::bpfel:   T.setArch(Triple::bpfeb);      break;
1887
  case Triple::mips64el:
1888
    T.setArch(Triple::mips64, getSubArch());
1889
    break;
1890
  case Triple::mipsel:
1891
    T.setArch(Triple::mips, getSubArch());
1892
    break;
1893
  case Triple::ppcle:   T.setArch(Triple::ppc);        break;
1894
  case Triple::ppc64le: T.setArch(Triple::ppc64);      break;
1895
  case Triple::sparcel: T.setArch(Triple::sparc);      break;
1896
  case Triple::tcele:   T.setArch(Triple::tce);        break;
1897
  default:
1898
    llvm_unreachable("getBigEndianArchVariant: unknown triple.");
1899
  }
1900
  return T;
1901
}
1902

1903
Triple Triple::getLittleEndianArchVariant() const {
1904
  Triple T(*this);
1905
  if (isLittleEndian())
1906
    return T;
1907

1908
  switch (getArch()) {
1909
  case Triple::UnknownArch:
1910
  case Triple::lanai:
1911
  case Triple::sparcv9:
1912
  case Triple::systemz:
1913
  case Triple::m68k:
1914

1915
  // ARM is intentionally unsupported here, changing the architecture would
1916
  // drop any arch suffixes.
1917
  case Triple::armeb:
1918
  case Triple::thumbeb:
1919
    T.setArch(UnknownArch);
1920
    break;
1921

1922
  case Triple::aarch64_be: T.setArch(Triple::aarch64);  break;
1923
  case Triple::bpfeb:      T.setArch(Triple::bpfel);    break;
1924
  case Triple::mips64:
1925
    T.setArch(Triple::mips64el, getSubArch());
1926
    break;
1927
  case Triple::mips:
1928
    T.setArch(Triple::mipsel, getSubArch());
1929
    break;
1930
  case Triple::ppc:        T.setArch(Triple::ppcle);    break;
1931
  case Triple::ppc64:      T.setArch(Triple::ppc64le);  break;
1932
  case Triple::sparc:      T.setArch(Triple::sparcel);  break;
1933
  case Triple::tce:        T.setArch(Triple::tcele);    break;
1934
  default:
1935
    llvm_unreachable("getLittleEndianArchVariant: unknown triple.");
1936
  }
1937
  return T;
1938
}
1939

1940
bool Triple::isLittleEndian() const {
1941
  switch (getArch()) {
1942
  case Triple::aarch64:
1943
  case Triple::aarch64_32:
1944
  case Triple::amdgcn:
1945
  case Triple::amdil64:
1946
  case Triple::amdil:
1947
  case Triple::arm:
1948
  case Triple::avr:
1949
  case Triple::bpfel:
1950
  case Triple::csky:
1951
  case Triple::dxil:
1952
  case Triple::hexagon:
1953
  case Triple::hsail64:
1954
  case Triple::hsail:
1955
  case Triple::kalimba:
1956
  case Triple::le32:
1957
  case Triple::le64:
1958
  case Triple::loongarch32:
1959
  case Triple::loongarch64:
1960
  case Triple::mips64el:
1961
  case Triple::mipsel:
1962
  case Triple::msp430:
1963
  case Triple::nvptx64:
1964
  case Triple::nvptx:
1965
  case Triple::ppcle:
1966
  case Triple::ppc64le:
1967
  case Triple::r600:
1968
  case Triple::renderscript32:
1969
  case Triple::renderscript64:
1970
  case Triple::riscv32:
1971
  case Triple::riscv64:
1972
  case Triple::shave:
1973
  case Triple::sparcel:
1974
  case Triple::spir64:
1975
  case Triple::spir:
1976
  case Triple::spirv:
1977
  case Triple::spirv32:
1978
  case Triple::spirv64:
1979
  case Triple::tcele:
1980
  case Triple::thumb:
1981
  case Triple::ve:
1982
  case Triple::wasm32:
1983
  case Triple::wasm64:
1984
  case Triple::x86:
1985
  case Triple::x86_64:
1986
  case Triple::xcore:
1987
  case Triple::xtensa:
1988
    return true;
1989
  default:
1990
    return false;
1991
  }
1992
}
1993

1994
bool Triple::isCompatibleWith(const Triple &Other) const {
1995
  // ARM and Thumb triples are compatible, if subarch, vendor and OS match.
1996
  if ((getArch() == Triple::thumb && Other.getArch() == Triple::arm) ||
1997
      (getArch() == Triple::arm && Other.getArch() == Triple::thumb) ||
1998
      (getArch() == Triple::thumbeb && Other.getArch() == Triple::armeb) ||
1999
      (getArch() == Triple::armeb && Other.getArch() == Triple::thumbeb)) {
2000
    if (getVendor() == Triple::Apple)
2001
      return getSubArch() == Other.getSubArch() &&
2002
             getVendor() == Other.getVendor() && getOS() == Other.getOS();
2003
    else
2004
      return getSubArch() == Other.getSubArch() &&
2005
             getVendor() == Other.getVendor() && getOS() == Other.getOS() &&
2006
             getEnvironment() == Other.getEnvironment() &&
2007
             getObjectFormat() == Other.getObjectFormat();
2008
  }
2009

2010
  // If vendor is apple, ignore the version number.
2011
  if (getVendor() == Triple::Apple)
2012
    return getArch() == Other.getArch() && getSubArch() == Other.getSubArch() &&
2013
           getVendor() == Other.getVendor() && getOS() == Other.getOS();
2014

2015
  return *this == Other;
2016
}
2017

2018
std::string Triple::merge(const Triple &Other) const {
2019
  // If vendor is apple, pick the triple with the larger version number.
2020
  if (getVendor() == Triple::Apple)
2021
    if (Other.isOSVersionLT(*this))
2022
      return str();
2023

2024
  return Other.str();
2025
}
2026

2027
bool Triple::isMacOSXVersionLT(unsigned Major, unsigned Minor,
2028
                               unsigned Micro) const {
2029
  assert(isMacOSX() && "Not an OS X triple!");
2030

2031
  // If this is OS X, expect a sane version number.
2032
  if (getOS() == Triple::MacOSX)
2033
    return isOSVersionLT(Major, Minor, Micro);
2034

2035
  // Otherwise, compare to the "Darwin" number.
2036
  if (Major == 10) {
2037
    return isOSVersionLT(Minor + 4, Micro, 0);
2038
  } else {
2039
    assert(Major >= 11 && "Unexpected major version");
2040
    return isOSVersionLT(Major - 11 + 20, Minor, Micro);
2041
  }
2042
}
2043

2044
VersionTuple Triple::getMinimumSupportedOSVersion() const {
2045
  if (getVendor() != Triple::Apple || getArch() != Triple::aarch64)
2046
    return VersionTuple();
2047
  switch (getOS()) {
2048
  case Triple::MacOSX:
2049
    // ARM64 slice is supported starting from macOS 11.0+.
2050
    return VersionTuple(11, 0, 0);
2051
  case Triple::IOS:
2052
    // ARM64 slice is supported starting from Mac Catalyst 14 (macOS 11).
2053
    // ARM64 simulators are supported for iOS 14+.
2054
    if (isMacCatalystEnvironment() || isSimulatorEnvironment())
2055
      return VersionTuple(14, 0, 0);
2056
    // ARM64e slice is supported starting from iOS 14.
2057
    if (isArm64e())
2058
      return VersionTuple(14, 0, 0);
2059
    break;
2060
  case Triple::TvOS:
2061
    // ARM64 simulators are supported for tvOS 14+.
2062
    if (isSimulatorEnvironment())
2063
      return VersionTuple(14, 0, 0);
2064
    break;
2065
  case Triple::WatchOS:
2066
    // ARM64 simulators are supported for watchOS 7+.
2067
    if (isSimulatorEnvironment())
2068
      return VersionTuple(7, 0, 0);
2069
    break;
2070
  case Triple::DriverKit:
2071
    return VersionTuple(20, 0, 0);
2072
  default:
2073
    break;
2074
  }
2075
  return VersionTuple();
2076
}
2077

2078
VersionTuple Triple::getCanonicalVersionForOS(OSType OSKind,
2079
                                              const VersionTuple &Version) {
2080
  switch (OSKind) {
2081
  case MacOSX:
2082
    // macOS 10.16 is canonicalized to macOS 11.
2083
    if (Version == VersionTuple(10, 16))
2084
      return VersionTuple(11, 0);
2085
    [[fallthrough]];
2086
  default:
2087
    return Version;
2088
  }
2089
}
2090

2091
// HLSL triple environment orders are relied on in the front end
2092
static_assert(Triple::Vertex - Triple::Pixel == 1,
2093
              "incorrect HLSL stage order");
2094
static_assert(Triple::Geometry - Triple::Pixel == 2,
2095
              "incorrect HLSL stage order");
2096
static_assert(Triple::Hull - Triple::Pixel == 3,
2097
              "incorrect HLSL stage order");
2098
static_assert(Triple::Domain - Triple::Pixel == 4,
2099
              "incorrect HLSL stage order");
2100
static_assert(Triple::Compute - Triple::Pixel == 5,
2101
              "incorrect HLSL stage order");
2102
static_assert(Triple::Library - Triple::Pixel == 6,
2103
              "incorrect HLSL stage order");
2104
static_assert(Triple::RayGeneration - Triple::Pixel == 7,
2105
              "incorrect HLSL stage order");
2106
static_assert(Triple::Intersection - Triple::Pixel == 8,
2107
              "incorrect HLSL stage order");
2108
static_assert(Triple::AnyHit - Triple::Pixel == 9,
2109
              "incorrect HLSL stage order");
2110
static_assert(Triple::ClosestHit - Triple::Pixel == 10,
2111
              "incorrect HLSL stage order");
2112
static_assert(Triple::Miss - Triple::Pixel == 11,
2113
              "incorrect HLSL stage order");
2114
static_assert(Triple::Callable - Triple::Pixel == 12,
2115
              "incorrect HLSL stage order");
2116
static_assert(Triple::Mesh - Triple::Pixel == 13,
2117
              "incorrect HLSL stage order");
2118
static_assert(Triple::Amplification - Triple::Pixel == 14,
2119
              "incorrect HLSL stage order");
2120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.