blob: 7806aec16c6d9a4f590eaeb038e4b916e2eedc56 [file] [log] [blame]
Daniel Dunbar23e97b02009-04-01 21:53:23 +00001//===--- 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"
Daniel Dunbara14d2252009-07-26 03:31:47 +000011
Jeffrey Yasskin0b228732009-10-06 21:45:26 +000012#include "llvm/ADT/SmallString.h"
Daniel Dunbara14d2252009-07-26 03:31:47 +000013#include "llvm/ADT/Twine.h"
Daniel Dunbar23e97b02009-04-01 21:53:23 +000014#include <cassert>
Mikhail Glushenkov70748752009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000016using namespace llvm;
17
18//
19
20const char *Triple::getArchTypeName(ArchType Kind) {
21 switch (Kind) {
22 case InvalidArch: return "<invalid>";
23 case UnknownArch: return "unknown";
Daniel Dunbar6337f152009-07-26 04:23:03 +000024
25 case alpha: return "alpha";
26 case arm: return "arm";
Jakob Stoklund Olesend9509412009-08-02 17:32:10 +000027 case bfin: return "bfin";
Daniel Dunbar6337f152009-07-26 04:23:03 +000028 case cellspu: return "cellspu";
29 case mips: return "mips";
30 case mipsel: return "mipsel";
31 case msp430: return "msp430";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +000032 case pic16: return "pic16";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000033 case ppc64: return "powerpc64";
34 case ppc: return "powerpc";
Daniel Dunbar6337f152009-07-26 04:23:03 +000035 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000036 case sparcv9: return "sparcv9";
Daniel Dunbar6337f152009-07-26 04:23:03 +000037 case systemz: return "s390x";
Eli Friedman74db89e2009-08-19 20:46:03 +000038 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000039 case thumb: return "thumb";
40 case x86: return "i386";
41 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000042 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000043 case mblaze: return "mblaze";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000044 }
45
46 return "<invalid>";
47}
48
Daniel Dunbar688b55b2009-08-24 09:53:06 +000049const char *Triple::getArchTypePrefix(ArchType Kind) {
50 switch (Kind) {
51 default:
52 return 0;
53
54 case alpha: return "alpha";
55
56 case arm:
57 case thumb: return "arm";
58
59 case bfin: return "bfin";
60
61 case cellspu: return "spu";
62
63 case ppc64:
64 case ppc: return "ppc";
65
Wesley Pecka70f28c2010-02-23 19:15:24 +000066 case mblaze: return "mblaze";
67
Chris Lattner87c06d62010-02-04 06:34:01 +000068 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000069 case sparc: return "sparc";
70
71 case x86:
72 case x86_64: return "x86";
73 case xcore: return "xcore";
74 }
75}
76
Daniel Dunbar23e97b02009-04-01 21:53:23 +000077const char *Triple::getVendorTypeName(VendorType Kind) {
78 switch (Kind) {
79 case UnknownVendor: return "unknown";
80
81 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000082 case PC: return "pc";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000083 }
84
85 return "<invalid>";
86}
87
88const char *Triple::getOSTypeName(OSType Kind) {
89 switch (Kind) {
90 case UnknownOS: return "unknown";
91
Duncan Sands852cd112009-06-19 14:40:01 +000092 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +000093 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000094 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +000095 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000096 case FreeBSD: return "freebsd";
97 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +000098 case Lv2: return "lv2";
Daniel Dunbar6337f152009-07-26 04:23:03 +000099 case MinGW32: return "mingw32";
Chris Lattner8e620832009-08-12 06:32:10 +0000100 case MinGW64: return "mingw64";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000101 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000102 case OpenBSD: return "openbsd";
Edward O'Callaghane0fb75d2009-11-15 10:18:17 +0000103 case Psp: return "psp";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000104 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000105 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000106 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000107 case Minix: return "minix";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000108 }
109
110 return "<invalid>";
111}
112
Daniel Dunbar2928c832009-11-06 10:58:06 +0000113Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000114 if (Name == "alpha")
115 return alpha;
116 if (Name == "arm")
117 return arm;
118 if (Name == "bfin")
119 return bfin;
120 if (Name == "cellspu")
121 return cellspu;
122 if (Name == "mips")
123 return mips;
124 if (Name == "mipsel")
125 return mipsel;
126 if (Name == "msp430")
127 return msp430;
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000128 if (Name == "pic16")
129 return pic16;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000130 if (Name == "ppc64")
131 return ppc64;
132 if (Name == "ppc")
133 return ppc;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000134 if (Name == "mblaze")
135 return mblaze;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000136 if (Name == "sparc")
137 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000138 if (Name == "sparcv9")
139 return sparcv9;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000140 if (Name == "systemz")
141 return systemz;
Eli Friedman74db89e2009-08-19 20:46:03 +0000142 if (Name == "tce")
143 return tce;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000144 if (Name == "thumb")
145 return thumb;
146 if (Name == "x86")
147 return x86;
Chris Lattnerb796c4f2009-08-12 06:45:02 +0000148 if (Name == "x86-64")
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000149 return x86_64;
150 if (Name == "xcore")
151 return xcore;
152
153 return UnknownArch;
154}
155
Daniel Dunbar2928c832009-11-06 10:58:06 +0000156Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000157 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
158 // archs which Darwin doesn't use.
159
160 // The matching this routine does is fairly pointless, since it is neither the
161 // complete architecture list, nor a reasonable subset. The problem is that
162 // historically the driver driver accepts this and also ties its -march=
163 // handling to the architecture name, so we need to be careful before removing
164 // support for it.
165
Daniel Dunbared687882009-09-09 23:01:25 +0000166 // This code must be kept in sync with Clang's Darwin specific argument
167 // translation.
168
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000169 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
170 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
171 Str == "ppc7450" || Str == "ppc970")
172 return Triple::ppc;
173
174 if (Str == "ppc64")
175 return Triple::ppc64;
176
177 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
178 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
179 Str == "pentIIm5" || Str == "pentium4")
180 return Triple::x86;
181
182 if (Str == "x86_64")
183 return Triple::x86_64;
184
185 // This is derived from the driver driver.
186 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
187 Str == "armv6" || Str == "armv7")
188 return Triple::arm;
189
190 return Triple::UnknownArch;
191}
192
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000193// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000194const char *Triple::getArchNameForAssembler() {
195 if (getOS() != Triple::Darwin && getVendor() != Triple::Apple)
196 return NULL;
197
198 StringRef Str = getArchName();
199 if (Str == "i386")
200 return "i386";
201 if (Str == "x86_64")
202 return "x86_64";
203 if (Str == "powerpc")
204 return "ppc";
205 if (Str == "powerpc64")
206 return "ppc64";
Wesley Pecka70f28c2010-02-23 19:15:24 +0000207 if (Str == "mblaze" || Str == "microblaze")
208 return "mblaze";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000209 if (Str == "arm")
210 return "arm";
211 if (Str == "armv4t" || Str == "thumbv4t")
212 return "armv4t";
213 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5" || Str == "thumbv5e")
214 return "armv5";
215 if (Str == "armv6" || Str == "thumbv6")
216 return "armv6";
217 if (Str == "armv7" || Str == "thumbv7")
218 return "armv7";
219 return NULL;
220}
221
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000222//
223
Duncan Sands335db222010-08-12 11:31:39 +0000224Triple::ArchType Triple::ParseArch(StringRef ArchName) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000225 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
Daniel Dunbar6337f152009-07-26 04:23:03 +0000226 ArchName[2] == '8' && ArchName[3] == '6' &&
227 ArchName[1] - '3' < 6) // i[3-9]86
Duncan Sands335db222010-08-12 11:31:39 +0000228 return x86;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000229 else if (ArchName == "amd64" || ArchName == "x86_64")
Duncan Sands335db222010-08-12 11:31:39 +0000230 return x86_64;
Daniel Dunbarb26bc422009-08-18 07:06:26 +0000231 else if (ArchName == "bfin")
Duncan Sands335db222010-08-12 11:31:39 +0000232 return bfin;
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000233 else if (ArchName == "pic16")
Duncan Sands335db222010-08-12 11:31:39 +0000234 return pic16;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000235 else if (ArchName == "powerpc")
Duncan Sands335db222010-08-12 11:31:39 +0000236 return ppc;
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000237 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
Duncan Sands335db222010-08-12 11:31:39 +0000238 return ppc64;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000239 else if (ArchName == "mblaze")
Duncan Sands335db222010-08-12 11:31:39 +0000240 return mblaze;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000241 else if (ArchName == "arm" ||
Daniel Dunbar24473892009-08-18 04:51:26 +0000242 ArchName.startswith("armv") ||
243 ArchName == "xscale")
Duncan Sands335db222010-08-12 11:31:39 +0000244 return arm;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000245 else if (ArchName == "thumb" ||
246 ArchName.startswith("thumbv"))
Duncan Sands335db222010-08-12 11:31:39 +0000247 return thumb;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000248 else if (ArchName.startswith("alpha"))
Duncan Sands335db222010-08-12 11:31:39 +0000249 return alpha;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000250 else if (ArchName == "spu" || ArchName == "cellspu")
Duncan Sands335db222010-08-12 11:31:39 +0000251 return cellspu;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000252 else if (ArchName == "msp430")
Duncan Sands335db222010-08-12 11:31:39 +0000253 return msp430;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000254 else if (ArchName == "mips" || ArchName == "mipsallegrex")
Duncan Sands335db222010-08-12 11:31:39 +0000255 return mips;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000256 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
257 ArchName == "psp")
Duncan Sands335db222010-08-12 11:31:39 +0000258 return mipsel;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000259 else if (ArchName == "sparc")
Duncan Sands335db222010-08-12 11:31:39 +0000260 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000261 else if (ArchName == "sparcv9")
Duncan Sands335db222010-08-12 11:31:39 +0000262 return sparcv9;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000263 else if (ArchName == "s390x")
Duncan Sands335db222010-08-12 11:31:39 +0000264 return systemz;
Eli Friedman74db89e2009-08-19 20:46:03 +0000265 else if (ArchName == "tce")
Duncan Sands335db222010-08-12 11:31:39 +0000266 return tce;
Richard Osborne768f1dd2009-08-31 21:51:36 +0000267 else if (ArchName == "xcore")
Duncan Sands335db222010-08-12 11:31:39 +0000268 return xcore;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000269 else
Duncan Sands335db222010-08-12 11:31:39 +0000270 return UnknownArch;
271}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000272
Duncan Sands335db222010-08-12 11:31:39 +0000273Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
274 if (VendorName == "apple")
275 return Apple;
276 else if (VendorName == "pc")
277 return PC;
278 else
279 return UnknownVendor;
280}
281
282Triple::OSType Triple::ParseOS(StringRef OSName) {
283 if (OSName.startswith("auroraux"))
284 return AuroraUX;
285 else if (OSName.startswith("cygwin"))
286 return Cygwin;
287 else if (OSName.startswith("darwin"))
288 return Darwin;
289 else if (OSName.startswith("dragonfly"))
290 return DragonFly;
291 else if (OSName.startswith("freebsd"))
292 return FreeBSD;
293 else if (OSName.startswith("linux"))
294 return Linux;
295 else if (OSName.startswith("lv2"))
296 return Lv2;
297 else if (OSName.startswith("mingw32"))
298 return MinGW32;
299 else if (OSName.startswith("mingw64"))
300 return MinGW64;
301 else if (OSName.startswith("netbsd"))
302 return NetBSD;
303 else if (OSName.startswith("openbsd"))
304 return OpenBSD;
305 else if (OSName.startswith("psp"))
306 return Psp;
307 else if (OSName.startswith("solaris"))
308 return Solaris;
309 else if (OSName.startswith("win32"))
310 return Win32;
311 else if (OSName.startswith("haiku"))
312 return Haiku;
313 else if (OSName.startswith("minix"))
314 return Minix;
315 else
316 return UnknownOS;
317}
318
319void Triple::Parse() const {
320 assert(!isInitialized() && "Invalid parse call.");
321
322 Arch = ParseArch(getArchName());
323 Vendor = ParseVendor(getVendorName());
324 OS = ParseOS(getOSName());
Daniel Dunbar651aa682009-08-18 19:26:55 +0000325
326 // Handle some exceptional cases where the OS / environment components are
327 // stuck into the vendor field.
Duncan Sands335db222010-08-12 11:31:39 +0000328 // TODO: Remove this logic and have places that need it use 'normalize'.
Daniel Dunbar651aa682009-08-18 19:26:55 +0000329 if (StringRef(getTriple()).count('-') == 1) {
330 StringRef VendorName = getVendorName();
331
332 if (VendorName.startswith("mingw32")) { // 'i386-mingw32', etc.
333 Vendor = PC;
334 OS = MinGW32;
335 return;
336 }
337
338 // arm-elf is another example, but we don't currently parse anything about
339 // the environment.
340 }
341
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000342 assert(isInitialized() && "Failed to initialize!");
343}
344
Duncan Sands335db222010-08-12 11:31:39 +0000345std::string Triple::normalize(StringRef Str) {
346 // Parse into components.
347 SmallVector<StringRef, 4> Components;
348 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
349 Last = Str.find('-', First);
350 Components.push_back(Str.slice(First, Last));
351 }
352
353 // If the first component corresponds to a known architecture, preferentially
354 // use it for the architecture. If the second component corresponds to a
355 // known vendor, preferentially use it for the vendor, etc. This avoids silly
356 // component movement when a component parses as (eg) both a valid arch and a
357 // valid os.
358 ArchType Arch = UnknownArch;
359 if (Components.size() > 0)
360 Arch = ParseArch(Components[0]);
361 VendorType Vendor = UnknownVendor;
362 if (Components.size() > 1)
363 Vendor = ParseVendor(Components[1]);
364 OSType OS = UnknownOS;
365 if (Components.size() > 2)
366 OS = ParseOS(Components[2]);
367
368 // Note which components are already in their final position. These will not
369 // be moved.
370 bool Found[3];
371 Found[0] = Arch != UnknownArch;
372 Found[1] = Vendor != UnknownVendor;
373 Found[2] = OS != UnknownOS;
374
375 // If they are not there already, permute the components into their canonical
376 // positions by seeing if they parse as a valid architecture, and if so moving
377 // the component to the architecture position etc.
378 for (unsigned Pos = 0; Pos != 3; ++Pos) {
379 if (Found[Pos])
380 continue; // Already in the canonical position.
381
382 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
383 // Do not reparse any components that already matched.
384 if (Idx < 3 && Found[Idx])
385 continue;
386
387 // Does this component parse as valid for the target position?
388 bool Valid = false;
389 StringRef Comp = Components[Idx];
390 switch (Pos) {
391 default:
392 assert(false && "unexpected component type!");
393 case 0:
394 Arch = ParseArch(Comp);
395 Valid = Arch != UnknownArch;
396 break;
397 case 1:
398 Vendor = ParseVendor(Comp);
399 Valid = Vendor != UnknownVendor;
400 break;
401 case 2:
402 OS = ParseOS(Comp);
403 Valid = OS != UnknownOS;
404 break;
405 }
406 if (!Valid)
407 continue; // Nope, try the next component.
408
409 // Move the component to the target position, pushing any non-fixed
410 // components that are in the way to the right. This tends to give
411 // good results in the common cases of a forgotten vendor component
412 // or a wrongly positioned environment.
413 if (Pos < Idx) {
414 // Insert left, pushing the existing components to the right. For
415 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
416 StringRef CurrentComponent(""); // The empty component.
417 // Replace the component we are moving with an empty component.
418 std::swap(CurrentComponent, Components[Idx]);
419 // Insert the component being moved at Pos, displacing any existing
420 // components to the right.
421 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
422 // Skip over any fixed components.
423 while (i < 3 && Found[i]) ++i;
424 // Place the component at the new position, getting the component
425 // that was at this position - it will be moved right.
426 std::swap(CurrentComponent, Components[i]);
427 }
428 } else if (Pos > Idx) {
429 // Push right by inserting empty components until the component at Idx
430 // reaches the target position Pos. For example, pc-a -> -pc-a when
431 // moving pc to the second position.
432 do {
433 // Insert one empty component at Idx.
434 StringRef CurrentComponent(""); // The empty component.
435 for (unsigned i = Idx; i < Components.size(); ++i) {
436 // Skip over any fixed components.
437 while (i < 3 && Found[i]) ++i;
438 // Place the component at the new position, getting the component
439 // that was at this position - it will be moved right.
440 std::swap(CurrentComponent, Components[i]);
441 // If it was placed on top of an empty component then we are done.
442 if (CurrentComponent.empty())
443 break;
444 }
445 // The last component was pushed off the end - append it.
446 if (!CurrentComponent.empty())
447 Components.push_back(CurrentComponent);
448
449 // Advance Idx to the component's new position.
450 while (++Idx < 3 && Found[Idx]) {}
451 } while (Idx < Pos); // Add more until the final position is reached.
452 }
453 assert(Pos < Components.size() && Components[Pos] == Comp &&
454 "Component moved wrong!");
455 Found[Pos] = true;
456 break;
457 }
458 }
459
460 // Special case logic goes here. At this point Arch, Vendor and OS have the
461 // correct values for the computed components.
462
463 // Stick the corrected components back together to form the normalized string.
464 std::string Normalized;
465 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
466 if (i) Normalized += '-';
467 Normalized += Components[i];
468 }
469 return Normalized;
470}
471
Daniel Dunbara14d2252009-07-26 03:31:47 +0000472StringRef Triple::getArchName() const {
473 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000474}
475
Daniel Dunbara14d2252009-07-26 03:31:47 +0000476StringRef Triple::getVendorName() const {
477 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
478 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000479}
480
Daniel Dunbara14d2252009-07-26 03:31:47 +0000481StringRef Triple::getOSName() const {
482 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
483 Tmp = Tmp.split('-').second; // Strip second component
484 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000485}
486
Daniel Dunbara14d2252009-07-26 03:31:47 +0000487StringRef Triple::getEnvironmentName() const {
488 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
489 Tmp = Tmp.split('-').second; // Strip second component
490 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000491}
492
Daniel Dunbara14d2252009-07-26 03:31:47 +0000493StringRef Triple::getOSAndEnvironmentName() const {
494 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
495 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000496}
497
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000498static unsigned EatNumber(StringRef &Str) {
499 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
500 unsigned Result = Str[0]-'0';
501
502 // Eat the digit.
503 Str = Str.substr(1);
504
505 // Handle "darwin11".
506 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
507 Result = Result*10 + (Str[0] - '0');
508 // Eat the digit.
509 Str = Str.substr(1);
510 }
511
512 return Result;
513}
514
515/// getDarwinNumber - Parse the 'darwin number' out of the specific target
516/// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
517/// not defined, return 0's. This requires that the triple have an OSType of
518/// darwin before it is called.
519void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
520 unsigned &Revision) const {
521 assert(getOS() == Darwin && "Not a darwin target triple!");
522 StringRef OSName = getOSName();
523 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
524
525 // Strip off "darwin".
526 OSName = OSName.substr(6);
527
528 Maj = Min = Revision = 0;
529
530 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
531 return;
532
533 // The major version is the first digit.
534 Maj = EatNumber(OSName);
535 if (OSName.empty()) return;
536
537 // Handle minor version: 10.4.9 -> darwin8.9.
538 if (OSName[0] != '.')
539 return;
540
541 // Eat the '.'.
542 OSName = OSName.substr(1);
543
544 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
545 return;
546
547 Min = EatNumber(OSName);
548 if (OSName.empty()) return;
549
550 // Handle revision darwin8.9.1
551 if (OSName[0] != '.')
552 return;
553
554 // Eat the '.'.
555 OSName = OSName.substr(1);
556
557 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
558 return;
559
560 Revision = EatNumber(OSName);
561}
562
Daniel Dunbara14d2252009-07-26 03:31:47 +0000563void Triple::setTriple(const Twine &Str) {
564 Data = Str.str();
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000565 Arch = InvalidArch;
566}
567
568void Triple::setArch(ArchType Kind) {
569 setArchName(getArchTypeName(Kind));
570}
571
572void Triple::setVendor(VendorType Kind) {
573 setVendorName(getVendorTypeName(Kind));
574}
575
576void Triple::setOS(OSType Kind) {
577 setOSName(getOSTypeName(Kind));
578}
579
Daniel Dunbar2928c832009-11-06 10:58:06 +0000580void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000581 // Work around a miscompilation bug for Twines in gcc 4.0.3.
582 SmallString<64> Triple;
583 Triple += Str;
584 Triple += "-";
585 Triple += getVendorName();
586 Triple += "-";
587 Triple += getOSAndEnvironmentName();
588 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000589}
590
Daniel Dunbar2928c832009-11-06 10:58:06 +0000591void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000592 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
593}
594
Daniel Dunbar2928c832009-11-06 10:58:06 +0000595void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000596 if (hasEnvironment())
597 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
598 "-" + getEnvironmentName());
599 else
600 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
601}
602
Daniel Dunbar2928c832009-11-06 10:58:06 +0000603void Triple::setEnvironmentName(StringRef Str) {
604 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000605 "-" + Str);
606}
607
Daniel Dunbar2928c832009-11-06 10:58:06 +0000608void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000609 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
610}