blob: 24f83e2f6fb0fc41f61a9e659554c22c4fd9236f [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"
Duncan Sands5754a452010-09-16 08:25:48 +000013#include "llvm/ADT/STLExtras.h"
Daniel Dunbara14d2252009-07-26 03:31:47 +000014#include "llvm/ADT/Twine.h"
Daniel Dunbar23e97b02009-04-01 21:53:23 +000015#include <cassert>
Mikhail Glushenkov70748752009-04-02 01:11:37 +000016#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000017using namespace llvm;
18
19//
20
21const char *Triple::getArchTypeName(ArchType Kind) {
22 switch (Kind) {
23 case InvalidArch: return "<invalid>";
24 case UnknownArch: return "unknown";
Daniel Dunbar6337f152009-07-26 04:23:03 +000025
26 case alpha: return "alpha";
27 case arm: return "arm";
Jakob Stoklund Olesend9509412009-08-02 17:32:10 +000028 case bfin: return "bfin";
Daniel Dunbar6337f152009-07-26 04:23:03 +000029 case cellspu: return "cellspu";
30 case mips: return "mips";
31 case mipsel: return "mipsel";
32 case msp430: return "msp430";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +000033 case pic16: return "pic16";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000034 case ppc64: return "powerpc64";
35 case ppc: return "powerpc";
Daniel Dunbar6337f152009-07-26 04:23:03 +000036 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000037 case sparcv9: return "sparcv9";
Daniel Dunbar6337f152009-07-26 04:23:03 +000038 case systemz: return "s390x";
Eli Friedman74db89e2009-08-19 20:46:03 +000039 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000040 case thumb: return "thumb";
41 case x86: return "i386";
42 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000043 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000044 case mblaze: return "mblaze";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000045 case ptx: return "ptx";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000046 }
47
48 return "<invalid>";
49}
50
Daniel Dunbar688b55b2009-08-24 09:53:06 +000051const char *Triple::getArchTypePrefix(ArchType Kind) {
52 switch (Kind) {
53 default:
54 return 0;
55
56 case alpha: return "alpha";
57
58 case arm:
59 case thumb: return "arm";
60
61 case bfin: return "bfin";
62
63 case cellspu: return "spu";
64
65 case ppc64:
66 case ppc: return "ppc";
67
Wesley Pecka70f28c2010-02-23 19:15:24 +000068 case mblaze: return "mblaze";
69
Chris Lattner87c06d62010-02-04 06:34:01 +000070 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000071 case sparc: return "sparc";
72
73 case x86:
74 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000075
Daniel Dunbar688b55b2009-08-24 09:53:06 +000076 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000077
78 case ptx: return "ptx";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000079 }
80}
81
Daniel Dunbar23e97b02009-04-01 21:53:23 +000082const char *Triple::getVendorTypeName(VendorType Kind) {
83 switch (Kind) {
84 case UnknownVendor: return "unknown";
85
86 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000087 case PC: return "pc";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000088 }
89
90 return "<invalid>";
91}
92
93const char *Triple::getOSTypeName(OSType Kind) {
94 switch (Kind) {
95 case UnknownOS: return "unknown";
96
Duncan Sands852cd112009-06-19 14:40:01 +000097 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +000098 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000099 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000100 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000101 case FreeBSD: return "freebsd";
102 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000103 case Lv2: return "lv2";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000104 case MinGW32: return "mingw32";
Chris Lattner8e620832009-08-12 06:32:10 +0000105 case MinGW64: return "mingw64";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000106 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000107 case OpenBSD: return "openbsd";
Edward O'Callaghane0fb75d2009-11-15 10:18:17 +0000108 case Psp: return "psp";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000109 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000110 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000111 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000112 case Minix: return "minix";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000113 }
114
115 return "<invalid>";
116}
117
Duncan Sands5754a452010-09-16 08:25:48 +0000118const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
119 switch (Kind) {
120 case UnknownEnvironment: return "unknown";
121 }
122
123 return "<invalid>";
124}
125
Daniel Dunbar2928c832009-11-06 10:58:06 +0000126Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000127 if (Name == "alpha")
128 return alpha;
129 if (Name == "arm")
130 return arm;
131 if (Name == "bfin")
132 return bfin;
133 if (Name == "cellspu")
134 return cellspu;
135 if (Name == "mips")
136 return mips;
137 if (Name == "mipsel")
138 return mipsel;
139 if (Name == "msp430")
140 return msp430;
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000141 if (Name == "pic16")
142 return pic16;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000143 if (Name == "ppc64")
144 return ppc64;
145 if (Name == "ppc")
146 return ppc;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000147 if (Name == "mblaze")
148 return mblaze;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000149 if (Name == "sparc")
150 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000151 if (Name == "sparcv9")
152 return sparcv9;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000153 if (Name == "systemz")
154 return systemz;
Eli Friedman74db89e2009-08-19 20:46:03 +0000155 if (Name == "tce")
156 return tce;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000157 if (Name == "thumb")
158 return thumb;
159 if (Name == "x86")
160 return x86;
Chris Lattnerb796c4f2009-08-12 06:45:02 +0000161 if (Name == "x86-64")
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000162 return x86_64;
163 if (Name == "xcore")
164 return xcore;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000165 if (Name == "ptx")
166 return ptx;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000167
168 return UnknownArch;
169}
170
Daniel Dunbar2928c832009-11-06 10:58:06 +0000171Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000172 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
173 // archs which Darwin doesn't use.
174
175 // The matching this routine does is fairly pointless, since it is neither the
176 // complete architecture list, nor a reasonable subset. The problem is that
177 // historically the driver driver accepts this and also ties its -march=
178 // handling to the architecture name, so we need to be careful before removing
179 // support for it.
180
Daniel Dunbared687882009-09-09 23:01:25 +0000181 // This code must be kept in sync with Clang's Darwin specific argument
182 // translation.
183
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000184 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
185 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
186 Str == "ppc7450" || Str == "ppc970")
187 return Triple::ppc;
188
189 if (Str == "ppc64")
190 return Triple::ppc64;
191
192 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
193 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
194 Str == "pentIIm5" || Str == "pentium4")
195 return Triple::x86;
196
197 if (Str == "x86_64")
198 return Triple::x86_64;
199
200 // This is derived from the driver driver.
201 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
202 Str == "armv6" || Str == "armv7")
203 return Triple::arm;
204
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000205 if (Str == "ptx")
206 return Triple::ptx;
207
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000208 return Triple::UnknownArch;
209}
210
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000211// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000212const char *Triple::getArchNameForAssembler() {
213 if (getOS() != Triple::Darwin && getVendor() != Triple::Apple)
214 return NULL;
215
216 StringRef Str = getArchName();
217 if (Str == "i386")
218 return "i386";
219 if (Str == "x86_64")
220 return "x86_64";
221 if (Str == "powerpc")
222 return "ppc";
223 if (Str == "powerpc64")
224 return "ppc64";
Wesley Pecka70f28c2010-02-23 19:15:24 +0000225 if (Str == "mblaze" || Str == "microblaze")
226 return "mblaze";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000227 if (Str == "arm")
228 return "arm";
229 if (Str == "armv4t" || Str == "thumbv4t")
230 return "armv4t";
231 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5" || Str == "thumbv5e")
232 return "armv5";
233 if (Str == "armv6" || Str == "thumbv6")
234 return "armv6";
235 if (Str == "armv7" || Str == "thumbv7")
236 return "armv7";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000237 if (Str == "ptx")
238 return "ptx";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000239 return NULL;
240}
241
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000242//
243
Duncan Sands335db222010-08-12 11:31:39 +0000244Triple::ArchType Triple::ParseArch(StringRef ArchName) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000245 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
Daniel Dunbar6337f152009-07-26 04:23:03 +0000246 ArchName[2] == '8' && ArchName[3] == '6' &&
247 ArchName[1] - '3' < 6) // i[3-9]86
Duncan Sands335db222010-08-12 11:31:39 +0000248 return x86;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000249 else if (ArchName == "amd64" || ArchName == "x86_64")
Duncan Sands335db222010-08-12 11:31:39 +0000250 return x86_64;
Daniel Dunbarb26bc422009-08-18 07:06:26 +0000251 else if (ArchName == "bfin")
Duncan Sands335db222010-08-12 11:31:39 +0000252 return bfin;
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000253 else if (ArchName == "pic16")
Duncan Sands335db222010-08-12 11:31:39 +0000254 return pic16;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000255 else if (ArchName == "powerpc")
Duncan Sands335db222010-08-12 11:31:39 +0000256 return ppc;
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000257 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
Duncan Sands335db222010-08-12 11:31:39 +0000258 return ppc64;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000259 else if (ArchName == "mblaze")
Duncan Sands335db222010-08-12 11:31:39 +0000260 return mblaze;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000261 else if (ArchName == "arm" ||
Daniel Dunbar24473892009-08-18 04:51:26 +0000262 ArchName.startswith("armv") ||
263 ArchName == "xscale")
Duncan Sands335db222010-08-12 11:31:39 +0000264 return arm;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000265 else if (ArchName == "thumb" ||
266 ArchName.startswith("thumbv"))
Duncan Sands335db222010-08-12 11:31:39 +0000267 return thumb;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000268 else if (ArchName.startswith("alpha"))
Duncan Sands335db222010-08-12 11:31:39 +0000269 return alpha;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000270 else if (ArchName == "spu" || ArchName == "cellspu")
Duncan Sands335db222010-08-12 11:31:39 +0000271 return cellspu;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000272 else if (ArchName == "msp430")
Duncan Sands335db222010-08-12 11:31:39 +0000273 return msp430;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000274 else if (ArchName == "mips" || ArchName == "mipsallegrex")
Duncan Sands335db222010-08-12 11:31:39 +0000275 return mips;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000276 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
277 ArchName == "psp")
Duncan Sands335db222010-08-12 11:31:39 +0000278 return mipsel;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000279 else if (ArchName == "sparc")
Duncan Sands335db222010-08-12 11:31:39 +0000280 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000281 else if (ArchName == "sparcv9")
Duncan Sands335db222010-08-12 11:31:39 +0000282 return sparcv9;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000283 else if (ArchName == "s390x")
Duncan Sands335db222010-08-12 11:31:39 +0000284 return systemz;
Eli Friedman74db89e2009-08-19 20:46:03 +0000285 else if (ArchName == "tce")
Duncan Sands335db222010-08-12 11:31:39 +0000286 return tce;
Richard Osborne768f1dd2009-08-31 21:51:36 +0000287 else if (ArchName == "xcore")
Duncan Sands335db222010-08-12 11:31:39 +0000288 return xcore;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000289 else if (ArchName == "ptx")
290 return ptx;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000291 else
Duncan Sands335db222010-08-12 11:31:39 +0000292 return UnknownArch;
293}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000294
Duncan Sands335db222010-08-12 11:31:39 +0000295Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
296 if (VendorName == "apple")
297 return Apple;
298 else if (VendorName == "pc")
299 return PC;
300 else
301 return UnknownVendor;
302}
303
304Triple::OSType Triple::ParseOS(StringRef OSName) {
305 if (OSName.startswith("auroraux"))
306 return AuroraUX;
307 else if (OSName.startswith("cygwin"))
308 return Cygwin;
309 else if (OSName.startswith("darwin"))
310 return Darwin;
311 else if (OSName.startswith("dragonfly"))
312 return DragonFly;
313 else if (OSName.startswith("freebsd"))
314 return FreeBSD;
315 else if (OSName.startswith("linux"))
316 return Linux;
317 else if (OSName.startswith("lv2"))
318 return Lv2;
319 else if (OSName.startswith("mingw32"))
320 return MinGW32;
321 else if (OSName.startswith("mingw64"))
322 return MinGW64;
323 else if (OSName.startswith("netbsd"))
324 return NetBSD;
325 else if (OSName.startswith("openbsd"))
326 return OpenBSD;
327 else if (OSName.startswith("psp"))
328 return Psp;
329 else if (OSName.startswith("solaris"))
330 return Solaris;
331 else if (OSName.startswith("win32"))
332 return Win32;
333 else if (OSName.startswith("haiku"))
334 return Haiku;
335 else if (OSName.startswith("minix"))
336 return Minix;
337 else
338 return UnknownOS;
339}
340
Duncan Sands5754a452010-09-16 08:25:48 +0000341Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
342 return UnknownEnvironment;
343}
344
Duncan Sands335db222010-08-12 11:31:39 +0000345void Triple::Parse() const {
346 assert(!isInitialized() && "Invalid parse call.");
347
348 Arch = ParseArch(getArchName());
349 Vendor = ParseVendor(getVendorName());
350 OS = ParseOS(getOSName());
Duncan Sands5754a452010-09-16 08:25:48 +0000351 Environment = ParseEnvironment(getEnvironmentName());
Daniel Dunbar651aa682009-08-18 19:26:55 +0000352
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000353 assert(isInitialized() && "Failed to initialize!");
354}
355
Duncan Sands335db222010-08-12 11:31:39 +0000356std::string Triple::normalize(StringRef Str) {
357 // Parse into components.
358 SmallVector<StringRef, 4> Components;
359 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
360 Last = Str.find('-', First);
361 Components.push_back(Str.slice(First, Last));
362 }
363
364 // If the first component corresponds to a known architecture, preferentially
365 // use it for the architecture. If the second component corresponds to a
366 // known vendor, preferentially use it for the vendor, etc. This avoids silly
367 // component movement when a component parses as (eg) both a valid arch and a
368 // valid os.
369 ArchType Arch = UnknownArch;
370 if (Components.size() > 0)
371 Arch = ParseArch(Components[0]);
372 VendorType Vendor = UnknownVendor;
373 if (Components.size() > 1)
374 Vendor = ParseVendor(Components[1]);
375 OSType OS = UnknownOS;
376 if (Components.size() > 2)
377 OS = ParseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000378 EnvironmentType Environment = UnknownEnvironment;
379 if (Components.size() > 3)
380 Environment = ParseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000381
382 // Note which components are already in their final position. These will not
383 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000384 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000385 Found[0] = Arch != UnknownArch;
386 Found[1] = Vendor != UnknownVendor;
387 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000388 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000389
390 // If they are not there already, permute the components into their canonical
391 // positions by seeing if they parse as a valid architecture, and if so moving
392 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000393 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000394 if (Found[Pos])
395 continue; // Already in the canonical position.
396
397 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
398 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000399 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000400 continue;
401
402 // Does this component parse as valid for the target position?
403 bool Valid = false;
404 StringRef Comp = Components[Idx];
405 switch (Pos) {
406 default:
407 assert(false && "unexpected component type!");
408 case 0:
409 Arch = ParseArch(Comp);
410 Valid = Arch != UnknownArch;
411 break;
412 case 1:
413 Vendor = ParseVendor(Comp);
414 Valid = Vendor != UnknownVendor;
415 break;
416 case 2:
417 OS = ParseOS(Comp);
418 Valid = OS != UnknownOS;
419 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000420 case 3:
421 Environment = ParseEnvironment(Comp);
422 Valid = Environment != UnknownEnvironment;
423 break;
Duncan Sands335db222010-08-12 11:31:39 +0000424 }
425 if (!Valid)
426 continue; // Nope, try the next component.
427
428 // Move the component to the target position, pushing any non-fixed
429 // components that are in the way to the right. This tends to give
430 // good results in the common cases of a forgotten vendor component
431 // or a wrongly positioned environment.
432 if (Pos < Idx) {
433 // Insert left, pushing the existing components to the right. For
434 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
435 StringRef CurrentComponent(""); // The empty component.
436 // Replace the component we are moving with an empty component.
437 std::swap(CurrentComponent, Components[Idx]);
438 // Insert the component being moved at Pos, displacing any existing
439 // components to the right.
440 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
441 // Skip over any fixed components.
Duncan Sands5754a452010-09-16 08:25:48 +0000442 while (i < array_lengthof(Found) && Found[i]) ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000443 // Place the component at the new position, getting the component
444 // that was at this position - it will be moved right.
445 std::swap(CurrentComponent, Components[i]);
446 }
447 } else if (Pos > Idx) {
448 // Push right by inserting empty components until the component at Idx
449 // reaches the target position Pos. For example, pc-a -> -pc-a when
450 // moving pc to the second position.
451 do {
452 // Insert one empty component at Idx.
453 StringRef CurrentComponent(""); // The empty component.
454 for (unsigned i = Idx; i < Components.size(); ++i) {
455 // Skip over any fixed components.
Duncan Sands5754a452010-09-16 08:25:48 +0000456 while (i < array_lengthof(Found) && Found[i]) ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000457 // Place the component at the new position, getting the component
458 // that was at this position - it will be moved right.
459 std::swap(CurrentComponent, Components[i]);
460 // If it was placed on top of an empty component then we are done.
461 if (CurrentComponent.empty())
462 break;
463 }
464 // The last component was pushed off the end - append it.
465 if (!CurrentComponent.empty())
466 Components.push_back(CurrentComponent);
467
468 // Advance Idx to the component's new position.
Duncan Sands5754a452010-09-16 08:25:48 +0000469 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
Duncan Sands335db222010-08-12 11:31:39 +0000470 } while (Idx < Pos); // Add more until the final position is reached.
471 }
472 assert(Pos < Components.size() && Components[Pos] == Comp &&
473 "Component moved wrong!");
474 Found[Pos] = true;
475 break;
476 }
477 }
478
479 // Special case logic goes here. At this point Arch, Vendor and OS have the
480 // correct values for the computed components.
481
482 // Stick the corrected components back together to form the normalized string.
483 std::string Normalized;
484 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
485 if (i) Normalized += '-';
486 Normalized += Components[i];
487 }
488 return Normalized;
489}
490
Daniel Dunbara14d2252009-07-26 03:31:47 +0000491StringRef Triple::getArchName() const {
492 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000493}
494
Daniel Dunbara14d2252009-07-26 03:31:47 +0000495StringRef Triple::getVendorName() const {
496 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
497 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000498}
499
Daniel Dunbara14d2252009-07-26 03:31:47 +0000500StringRef Triple::getOSName() const {
501 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
502 Tmp = Tmp.split('-').second; // Strip second component
503 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000504}
505
Daniel Dunbara14d2252009-07-26 03:31:47 +0000506StringRef Triple::getEnvironmentName() const {
507 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
508 Tmp = Tmp.split('-').second; // Strip second component
509 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000510}
511
Daniel Dunbara14d2252009-07-26 03:31:47 +0000512StringRef Triple::getOSAndEnvironmentName() const {
513 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
514 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000515}
516
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000517static unsigned EatNumber(StringRef &Str) {
518 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
519 unsigned Result = Str[0]-'0';
520
521 // Eat the digit.
522 Str = Str.substr(1);
523
524 // Handle "darwin11".
525 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
526 Result = Result*10 + (Str[0] - '0');
527 // Eat the digit.
528 Str = Str.substr(1);
529 }
530
531 return Result;
532}
533
534/// getDarwinNumber - Parse the 'darwin number' out of the specific target
535/// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
536/// not defined, return 0's. This requires that the triple have an OSType of
537/// darwin before it is called.
538void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
539 unsigned &Revision) const {
540 assert(getOS() == Darwin && "Not a darwin target triple!");
541 StringRef OSName = getOSName();
542 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
543
544 // Strip off "darwin".
545 OSName = OSName.substr(6);
546
547 Maj = Min = Revision = 0;
548
549 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
550 return;
551
552 // The major version is the first digit.
553 Maj = EatNumber(OSName);
554 if (OSName.empty()) return;
555
556 // Handle minor version: 10.4.9 -> darwin8.9.
557 if (OSName[0] != '.')
558 return;
559
560 // Eat the '.'.
561 OSName = OSName.substr(1);
562
563 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
564 return;
565
566 Min = EatNumber(OSName);
567 if (OSName.empty()) return;
568
569 // Handle revision darwin8.9.1
570 if (OSName[0] != '.')
571 return;
572
573 // Eat the '.'.
574 OSName = OSName.substr(1);
575
576 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
577 return;
578
579 Revision = EatNumber(OSName);
580}
581
Daniel Dunbara14d2252009-07-26 03:31:47 +0000582void Triple::setTriple(const Twine &Str) {
583 Data = Str.str();
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000584 Arch = InvalidArch;
585}
586
587void Triple::setArch(ArchType Kind) {
588 setArchName(getArchTypeName(Kind));
589}
590
591void Triple::setVendor(VendorType Kind) {
592 setVendorName(getVendorTypeName(Kind));
593}
594
595void Triple::setOS(OSType Kind) {
596 setOSName(getOSTypeName(Kind));
597}
598
Duncan Sands5754a452010-09-16 08:25:48 +0000599void Triple::setEnvironment(EnvironmentType Kind) {
600 setEnvironmentName(getEnvironmentTypeName(Kind));
601}
602
Daniel Dunbar2928c832009-11-06 10:58:06 +0000603void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000604 // Work around a miscompilation bug for Twines in gcc 4.0.3.
605 SmallString<64> Triple;
606 Triple += Str;
607 Triple += "-";
608 Triple += getVendorName();
609 Triple += "-";
610 Triple += getOSAndEnvironmentName();
611 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000612}
613
Daniel Dunbar2928c832009-11-06 10:58:06 +0000614void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000615 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
616}
617
Daniel Dunbar2928c832009-11-06 10:58:06 +0000618void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000619 if (hasEnvironment())
620 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
621 "-" + getEnvironmentName());
622 else
623 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
624}
625
Daniel Dunbar2928c832009-11-06 10:58:06 +0000626void Triple::setEnvironmentName(StringRef Str) {
627 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000628 "-" + Str);
629}
630
Daniel Dunbar2928c832009-11-06 10:58:06 +0000631void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000632 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
633}