blob: fa4c0da9359a8e3435118f281fd0f0759023323f [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"
Jeffrey Yasskin0b228732009-10-06 21:45:26 +000011#include "llvm/ADT/SmallString.h"
Chandler Carruth06accda2012-02-12 09:27:38 +000012#include "llvm/ADT/StringSwitch.h"
Duncan Sands5754a452010-09-16 08:25:48 +000013#include "llvm/ADT/STLExtras.h"
David Blaikie4d6ccb52012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov70748752009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar23e97b02009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
20 case InvalidArch: return "<invalid>";
21 case UnknownArch: return "unknown";
Jim Grosbache509aa92010-12-17 02:10:59 +000022
Daniel Dunbar6337f152009-07-26 04:23:03 +000023 case arm: return "arm";
24 case cellspu: return "cellspu";
Tony Linthicumb4b54152011-12-12 21:14:40 +000025 case hexagon: return "hexagon";
Daniel Dunbar6337f152009-07-26 04:23:03 +000026 case mips: return "mips";
27 case mipsel: return "mipsel";
Akira Hatanaka70303682011-09-20 18:09:37 +000028 case mips64: return "mips64";
29 case mips64el:return "mips64el";
Daniel Dunbar6337f152009-07-26 04:23:03 +000030 case msp430: return "msp430";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000031 case ppc64: return "powerpc64";
32 case ppc: return "powerpc";
Daniel Dunbar6337f152009-07-26 04:23:03 +000033 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000034 case sparcv9: return "sparcv9";
Eli Friedman74db89e2009-08-19 20:46:03 +000035 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000036 case thumb: return "thumb";
37 case x86: return "i386";
38 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000039 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000040 case mblaze: return "mblaze";
Justin Holewinskie1fee482011-04-20 15:37:17 +000041 case ptx32: return "ptx32";
42 case ptx64: return "ptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000043 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000044 case amdil: return "amdil";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000045 }
46
David Blaikie4d6ccb52012-01-20 21:51:11 +000047 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000048}
49
Daniel Dunbar688b55b2009-08-24 09:53:06 +000050const char *Triple::getArchTypePrefix(ArchType Kind) {
51 switch (Kind) {
52 default:
53 return 0;
54
Daniel Dunbar688b55b2009-08-24 09:53:06 +000055 case arm:
56 case thumb: return "arm";
57
Daniel Dunbar688b55b2009-08-24 09:53:06 +000058 case cellspu: return "spu";
59
60 case ppc64:
61 case ppc: return "ppc";
62
Wesley Pecka70f28c2010-02-23 19:15:24 +000063 case mblaze: return "mblaze";
64
Tony Linthicumb4b54152011-12-12 21:14:40 +000065 case hexagon: return "hexagon";
66
Chris Lattner87c06d62010-02-04 06:34:01 +000067 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000068 case sparc: return "sparc";
69
70 case x86:
71 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000072
Daniel Dunbar688b55b2009-08-24 09:53:06 +000073 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000074
Justin Holewinskie1fee482011-04-20 15:37:17 +000075 case ptx32: return "ptx";
76 case ptx64: return "ptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000077 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000078 case amdil: return "amdil";
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";
John Thompson6046cff2011-03-15 21:51:56 +000088 case SCEI: return "scei";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000089 }
90
David Blaikie4d6ccb52012-01-20 21:51:11 +000091 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000092}
93
94const char *Triple::getOSTypeName(OSType Kind) {
95 switch (Kind) {
96 case UnknownOS: return "unknown";
97
Duncan Sands852cd112009-06-19 14:40:01 +000098 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +000099 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000100 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +0000101 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000102 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000103 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000104 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000105 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000106 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000107 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000108 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000109 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000110 case OpenBSD: return "openbsd";
Edward O'Callaghane0fb75d2009-11-15 10:18:17 +0000111 case Psp: return "psp";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000112 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000113 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000114 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000115 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000116 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000117 case NativeClient: return "nacl";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000118 }
119
David Blaikie4d6ccb52012-01-20 21:51:11 +0000120 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000121}
122
Duncan Sands5754a452010-09-16 08:25:48 +0000123const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
124 switch (Kind) {
125 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000126 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000127 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000128 case GNUEABI: return "gnueabi";
129 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000130 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000131 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000132 }
133
David Blaikie4d6ccb52012-01-20 21:51:11 +0000134 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000135}
136
Daniel Dunbar2928c832009-11-06 10:58:06 +0000137Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000138 return StringSwitch<Triple::ArchType>(Name)
139 .Case("arm", arm)
140 .Case("cellspu", cellspu)
141 .Case("mips", mips)
142 .Case("mipsel", mipsel)
143 .Case("mips64", mips64)
144 .Case("mips64el", mips64el)
145 .Case("msp430", msp430)
146 .Case("ppc64", ppc64)
147 .Case("ppc32", ppc)
148 .Case("ppc", ppc)
149 .Case("mblaze", mblaze)
150 .Case("hexagon", hexagon)
151 .Case("sparc", sparc)
152 .Case("sparcv9", sparcv9)
153 .Case("tce", tce)
154 .Case("thumb", thumb)
155 .Case("x86", x86)
156 .Case("x86-64", x86_64)
157 .Case("xcore", xcore)
158 .Case("ptx32", ptx32)
159 .Case("ptx64", ptx64)
160 .Case("le32", le32)
161 .Case("amdil", amdil)
162 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000163}
164
Daniel Dunbar2928c832009-11-06 10:58:06 +0000165Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000166 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
167 // archs which Darwin doesn't use.
168
169 // The matching this routine does is fairly pointless, since it is neither the
170 // complete architecture list, nor a reasonable subset. The problem is that
171 // historically the driver driver accepts this and also ties its -march=
172 // handling to the architecture name, so we need to be careful before removing
173 // support for it.
174
Daniel Dunbared687882009-09-09 23:01:25 +0000175 // This code must be kept in sync with Clang's Darwin specific argument
176 // translation.
177
Chandler Carruth06accda2012-02-12 09:27:38 +0000178 return StringSwitch<ArchType>(Str)
179 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
180 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
181 .Case("ppc64", Triple::ppc64)
182 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
183 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
184 Triple::x86)
185 .Case("x86_64", Triple::x86_64)
186 // This is derived from the driver driver.
187 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
188 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
189 .Case("ptx32", Triple::ptx32)
190 .Case("ptx64", Triple::ptx64)
191 .Case("amdil", Triple::amdil)
192 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000193}
194
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000195// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000196const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000197 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000198 return NULL;
199
Chandler Carruth06accda2012-02-12 09:27:38 +0000200 return StringSwitch<const char*>(getArchName())
201 .Case("i386", "i386")
202 .Case("x86_64", "x86_64")
203 .Case("powerpc", "ppc")
204 .Case("powerpc64", "ppc64")
205 .Cases("mblaze", "microblaze", "mblaze")
206 .Case("arm", "arm")
207 .Cases("armv4t", "thumbv4t", "armv4t")
208 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
209 .Cases("armv6", "thumbv6", "armv6")
210 .Cases("armv7", "thumbv7", "armv7")
211 .Case("ptx32", "ptx32")
212 .Case("ptx64", "ptx64")
213 .Case("le32", "le32")
214 .Case("amdil", "amdil")
215 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000216}
217
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000218//
219
Duncan Sands335db222010-08-12 11:31:39 +0000220Triple::ArchType Triple::ParseArch(StringRef ArchName) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000221 // Handle some unusual patterns.
222 // FIXME: It would be good to replace these with explicit names for all the
223 // various suffixes supported.
224 if (ArchName.startswith("armv")) return arm;
225 if (ArchName.startswith("thumbv")) return thumb;
226
227 return StringSwitch<ArchType>(ArchName)
228 .Cases("i386", "i486", "i586", "i686", x86)
229 .Cases("i786", "i886", "i986", x86) // FIXME: Do we need to support these?
230 .Cases("amd64", "x86_64", x86_64)
231 .Case("powerpc", ppc)
232 .Cases("powerpc64", "ppu", ppc64)
233 .Case("mblaze", mblaze)
234 .Cases("arm", "xscale", arm)
235 .Case("thumb", thumb)
236 .Cases("spu", "cellspu", cellspu)
237 .Case("msp430", msp430)
238 .Cases("mips", "mipseb", "mipsallegrex", mips)
239 .Cases("mipsel", "mipsallegrexel", "psp", mipsel)
240 .Cases("mips64", "mips64eb", mips64)
241 .Case("mips64el", mips64el)
242 .Case("hexagon", hexagon)
243 .Case("sparc", sparc)
244 .Case("sparcv9", sparcv9)
245 .Case("tce", tce)
246 .Case("xcore", xcore)
247 .Case("ptx32", ptx32)
248 .Case("ptx64", ptx64)
249 .Case("le32", le32)
250 .Case("amdil", amdil)
251 .Default(UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000252}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000253
Duncan Sands335db222010-08-12 11:31:39 +0000254Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000255 return StringSwitch<VendorType>(VendorName)
256 .Case("apple", Apple)
257 .Case("pc", PC)
258 .Case("scei", SCEI)
259 .Default(UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000260}
261
262Triple::OSType Triple::ParseOS(StringRef OSName) {
263 if (OSName.startswith("auroraux"))
264 return AuroraUX;
265 else if (OSName.startswith("cygwin"))
266 return Cygwin;
267 else if (OSName.startswith("darwin"))
268 return Darwin;
269 else if (OSName.startswith("dragonfly"))
270 return DragonFly;
271 else if (OSName.startswith("freebsd"))
272 return FreeBSD;
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000273 else if (OSName.startswith("ios"))
274 return IOS;
Duncan Sands652b48b2011-07-26 15:30:04 +0000275 else if (OSName.startswith("kfreebsd"))
276 return KFreeBSD;
Duncan Sands335db222010-08-12 11:31:39 +0000277 else if (OSName.startswith("linux"))
278 return Linux;
279 else if (OSName.startswith("lv2"))
280 return Lv2;
Daniel Dunbar1af39472011-04-19 23:34:12 +0000281 else if (OSName.startswith("macosx"))
282 return MacOSX;
Duncan Sands335db222010-08-12 11:31:39 +0000283 else if (OSName.startswith("mingw32"))
284 return MinGW32;
Duncan Sands335db222010-08-12 11:31:39 +0000285 else if (OSName.startswith("netbsd"))
286 return NetBSD;
287 else if (OSName.startswith("openbsd"))
288 return OpenBSD;
289 else if (OSName.startswith("psp"))
290 return Psp;
291 else if (OSName.startswith("solaris"))
292 return Solaris;
293 else if (OSName.startswith("win32"))
294 return Win32;
295 else if (OSName.startswith("haiku"))
296 return Haiku;
297 else if (OSName.startswith("minix"))
298 return Minix;
Eli Friedmanb0e77a22011-07-06 20:56:26 +0000299 else if (OSName.startswith("rtems"))
300 return RTEMS;
Ivan Krasind8834532011-08-22 23:08:53 +0000301 else if (OSName.startswith("nacl"))
302 return NativeClient;
Duncan Sands335db222010-08-12 11:31:39 +0000303 else
304 return UnknownOS;
305}
306
Duncan Sands5754a452010-09-16 08:25:48 +0000307Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
Renato Golin859f8182011-01-21 18:25:47 +0000308 if (EnvironmentName.startswith("eabi"))
309 return EABI;
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000310 else if (EnvironmentName.startswith("gnueabihf"))
311 return GNUEABIHF;
Renato Golin859f8182011-01-21 18:25:47 +0000312 else if (EnvironmentName.startswith("gnueabi"))
313 return GNUEABI;
314 else if (EnvironmentName.startswith("gnu"))
315 return GNU;
Evan Cheng2bffee22011-02-01 01:14:13 +0000316 else if (EnvironmentName.startswith("macho"))
317 return MachO;
Chandler Carruthfd553c22012-01-10 19:46:00 +0000318 else if (EnvironmentName.startswith("androideabi"))
319 return ANDROIDEABI;
Renato Golin859f8182011-01-21 18:25:47 +0000320 else
321 return UnknownEnvironment;
Duncan Sands5754a452010-09-16 08:25:48 +0000322}
323
Duncan Sands335db222010-08-12 11:31:39 +0000324void Triple::Parse() const {
325 assert(!isInitialized() && "Invalid parse call.");
326
327 Arch = ParseArch(getArchName());
328 Vendor = ParseVendor(getVendorName());
329 OS = ParseOS(getOSName());
Duncan Sandsae200c62011-02-02 10:08:38 +0000330 Environment = ParseEnvironment(getEnvironmentName());
Daniel Dunbar651aa682009-08-18 19:26:55 +0000331
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000332 assert(isInitialized() && "Failed to initialize!");
333}
334
Duncan Sands335db222010-08-12 11:31:39 +0000335std::string Triple::normalize(StringRef Str) {
336 // Parse into components.
337 SmallVector<StringRef, 4> Components;
338 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
339 Last = Str.find('-', First);
340 Components.push_back(Str.slice(First, Last));
341 }
342
343 // If the first component corresponds to a known architecture, preferentially
344 // use it for the architecture. If the second component corresponds to a
345 // known vendor, preferentially use it for the vendor, etc. This avoids silly
346 // component movement when a component parses as (eg) both a valid arch and a
347 // valid os.
348 ArchType Arch = UnknownArch;
349 if (Components.size() > 0)
350 Arch = ParseArch(Components[0]);
351 VendorType Vendor = UnknownVendor;
352 if (Components.size() > 1)
353 Vendor = ParseVendor(Components[1]);
354 OSType OS = UnknownOS;
355 if (Components.size() > 2)
356 OS = ParseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000357 EnvironmentType Environment = UnknownEnvironment;
358 if (Components.size() > 3)
359 Environment = ParseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000360
361 // Note which components are already in their final position. These will not
362 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000363 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000364 Found[0] = Arch != UnknownArch;
365 Found[1] = Vendor != UnknownVendor;
366 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000367 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000368
369 // If they are not there already, permute the components into their canonical
370 // positions by seeing if they parse as a valid architecture, and if so moving
371 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000372 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000373 if (Found[Pos])
374 continue; // Already in the canonical position.
375
376 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
377 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000378 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000379 continue;
380
381 // Does this component parse as valid for the target position?
382 bool Valid = false;
383 StringRef Comp = Components[Idx];
384 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000385 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000386 case 0:
387 Arch = ParseArch(Comp);
388 Valid = Arch != UnknownArch;
389 break;
390 case 1:
391 Vendor = ParseVendor(Comp);
392 Valid = Vendor != UnknownVendor;
393 break;
394 case 2:
395 OS = ParseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000396 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000397 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000398 case 3:
399 Environment = ParseEnvironment(Comp);
400 Valid = Environment != UnknownEnvironment;
401 break;
Duncan Sands335db222010-08-12 11:31:39 +0000402 }
403 if (!Valid)
404 continue; // Nope, try the next component.
405
406 // Move the component to the target position, pushing any non-fixed
407 // components that are in the way to the right. This tends to give
408 // good results in the common cases of a forgotten vendor component
409 // or a wrongly positioned environment.
410 if (Pos < Idx) {
411 // Insert left, pushing the existing components to the right. For
412 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
413 StringRef CurrentComponent(""); // The empty component.
414 // Replace the component we are moving with an empty component.
415 std::swap(CurrentComponent, Components[Idx]);
416 // Insert the component being moved at Pos, displacing any existing
417 // components to the right.
418 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
419 // Skip over any fixed components.
Duncan Sands5754a452010-09-16 08:25:48 +0000420 while (i < array_lengthof(Found) && Found[i]) ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000421 // Place the component at the new position, getting the component
422 // that was at this position - it will be moved right.
423 std::swap(CurrentComponent, Components[i]);
424 }
425 } else if (Pos > Idx) {
426 // Push right by inserting empty components until the component at Idx
427 // reaches the target position Pos. For example, pc-a -> -pc-a when
428 // moving pc to the second position.
429 do {
430 // Insert one empty component at Idx.
431 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000432 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000433 // Place the component at the new position, getting the component
434 // that was at this position - it will be moved right.
435 std::swap(CurrentComponent, Components[i]);
436 // If it was placed on top of an empty component then we are done.
437 if (CurrentComponent.empty())
438 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000439 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000440 while (++i < array_lengthof(Found) && Found[i])
441 ;
Duncan Sands335db222010-08-12 11:31:39 +0000442 }
443 // The last component was pushed off the end - append it.
444 if (!CurrentComponent.empty())
445 Components.push_back(CurrentComponent);
446
447 // Advance Idx to the component's new position.
Duncan Sands5754a452010-09-16 08:25:48 +0000448 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
Duncan Sands335db222010-08-12 11:31:39 +0000449 } while (Idx < Pos); // Add more until the final position is reached.
450 }
451 assert(Pos < Components.size() && Components[Pos] == Comp &&
452 "Component moved wrong!");
453 Found[Pos] = true;
454 break;
455 }
456 }
457
458 // Special case logic goes here. At this point Arch, Vendor and OS have the
459 // correct values for the computed components.
460
461 // Stick the corrected components back together to form the normalized string.
462 std::string Normalized;
463 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
464 if (i) Normalized += '-';
465 Normalized += Components[i];
466 }
467 return Normalized;
468}
469
Daniel Dunbara14d2252009-07-26 03:31:47 +0000470StringRef Triple::getArchName() const {
471 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000472}
473
Daniel Dunbara14d2252009-07-26 03:31:47 +0000474StringRef Triple::getVendorName() const {
475 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
476 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000477}
478
Daniel Dunbara14d2252009-07-26 03:31:47 +0000479StringRef Triple::getOSName() const {
480 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
481 Tmp = Tmp.split('-').second; // Strip second component
482 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000483}
484
Daniel Dunbara14d2252009-07-26 03:31:47 +0000485StringRef Triple::getEnvironmentName() const {
486 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
487 Tmp = Tmp.split('-').second; // Strip second component
488 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000489}
490
Daniel Dunbara14d2252009-07-26 03:31:47 +0000491StringRef Triple::getOSAndEnvironmentName() const {
492 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
493 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000494}
495
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000496static unsigned EatNumber(StringRef &Str) {
497 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000498 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000499
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000500 do {
501 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000502 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000503
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000504 // Eat the digit.
505 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000506 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000507
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000508 return Result;
509}
510
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000511void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
512 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000513 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000514
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000515 // Assume that the OS portion of the triple starts with the canonical name.
516 StringRef OSTypeName = getOSTypeName(getOS());
517 if (OSName.startswith(OSTypeName))
518 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000519
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000520 // Any unset version defaults to 0.
521 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000522
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000523 // Parse up to three components.
524 unsigned *Components[3] = { &Major, &Minor, &Micro };
525 for (unsigned i = 0; i != 3; ++i) {
526 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
527 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000528
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000529 // Consume the leading number.
530 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000531
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000532 // Consume the separator, if present.
533 if (OSName.startswith("."))
534 OSName = OSName.substr(1);
535 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000536}
537
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000538bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
539 unsigned &Micro) const {
540 getOSVersion(Major, Minor, Micro);
541
542 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000543 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000544 case Darwin:
545 // Default to darwin8, i.e., MacOSX 10.4.
546 if (Major == 0)
547 Major = 8;
548 // Darwin version numbers are skewed from OS X versions.
549 if (Major < 4)
550 return false;
551 Micro = 0;
552 Minor = Major - 4;
553 Major = 10;
554 break;
555 case MacOSX:
556 // Default to 10.4.
557 if (Major == 0) {
558 Major = 10;
559 Minor = 4;
560 }
561 if (Major != 10)
562 return false;
563 break;
564 case IOS:
565 // Ignore the version from the triple. This is only handled because the
566 // the clang driver combines OS X and IOS support into a common Darwin
567 // toolchain that wants to know the OS X version number even when targeting
568 // IOS.
569 Major = 10;
570 Minor = 4;
571 Micro = 0;
572 break;
573 }
574 return true;
575}
576
Daniel Dunbara14d2252009-07-26 03:31:47 +0000577void Triple::setTriple(const Twine &Str) {
578 Data = Str.str();
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000579 Arch = InvalidArch;
580}
581
582void Triple::setArch(ArchType Kind) {
583 setArchName(getArchTypeName(Kind));
584}
585
586void Triple::setVendor(VendorType Kind) {
587 setVendorName(getVendorTypeName(Kind));
588}
589
590void Triple::setOS(OSType Kind) {
591 setOSName(getOSTypeName(Kind));
592}
593
Duncan Sands5754a452010-09-16 08:25:48 +0000594void Triple::setEnvironment(EnvironmentType Kind) {
595 setEnvironmentName(getEnvironmentTypeName(Kind));
596}
597
Daniel Dunbar2928c832009-11-06 10:58:06 +0000598void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000599 // Work around a miscompilation bug for Twines in gcc 4.0.3.
600 SmallString<64> Triple;
601 Triple += Str;
602 Triple += "-";
603 Triple += getVendorName();
604 Triple += "-";
605 Triple += getOSAndEnvironmentName();
606 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000607}
608
Daniel Dunbar2928c832009-11-06 10:58:06 +0000609void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000610 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
611}
612
Daniel Dunbar2928c832009-11-06 10:58:06 +0000613void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000614 if (hasEnvironment())
615 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
616 "-" + getEnvironmentName());
617 else
618 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
619}
620
Daniel Dunbar2928c832009-11-06 10:58:06 +0000621void Triple::setEnvironmentName(StringRef Str) {
622 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000623 "-" + Str);
624}
625
Daniel Dunbar2928c832009-11-06 10:58:06 +0000626void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000627 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
628}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000629
630static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
631 switch (Arch) {
632 case llvm::Triple::UnknownArch:
633 case llvm::Triple::InvalidArch:
634 return 0;
635
636 case llvm::Triple::msp430:
637 return 16;
638
639 case llvm::Triple::amdil:
640 case llvm::Triple::arm:
641 case llvm::Triple::cellspu:
642 case llvm::Triple::hexagon:
643 case llvm::Triple::le32:
644 case llvm::Triple::mblaze:
645 case llvm::Triple::mips:
646 case llvm::Triple::mipsel:
647 case llvm::Triple::ppc:
648 case llvm::Triple::ptx32:
649 case llvm::Triple::sparc:
650 case llvm::Triple::tce:
651 case llvm::Triple::thumb:
652 case llvm::Triple::x86:
653 case llvm::Triple::xcore:
654 return 32;
655
656 case llvm::Triple::mips64:
657 case llvm::Triple::mips64el:
658 case llvm::Triple::ppc64:
659 case llvm::Triple::ptx64:
660 case llvm::Triple::sparcv9:
661 case llvm::Triple::x86_64:
662 return 64;
663 }
664 llvm_unreachable("Invalid architecture value");
665}
666
667bool Triple::isArch64Bit() const {
668 return getArchPointerBitWidth(getArch()) == 64;
669}
670
671bool Triple::isArch32Bit() const {
672 return getArchPointerBitWidth(getArch()) == 32;
673}
674
675bool Triple::isArch16Bit() const {
676 return getArchPointerBitWidth(getArch()) == 16;
677}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000678
679Triple Triple::get32BitArchVariant() const {
680 Triple T(*this);
681 switch (getArch()) {
682 case Triple::UnknownArch:
683 case Triple::InvalidArch:
684 case Triple::msp430:
685 T.setArch(UnknownArch);
686 break;
687
688 case Triple::amdil:
689 case Triple::arm:
690 case Triple::cellspu:
691 case Triple::hexagon:
692 case Triple::le32:
693 case Triple::mblaze:
694 case Triple::mips:
695 case Triple::mipsel:
696 case Triple::ppc:
697 case Triple::ptx32:
698 case Triple::sparc:
699 case Triple::tce:
700 case Triple::thumb:
701 case Triple::x86:
702 case Triple::xcore:
703 // Already 32-bit.
704 break;
705
706 case Triple::mips64: T.setArch(Triple::mips); break;
707 case Triple::mips64el: T.setArch(Triple::mipsel); break;
708 case Triple::ppc64: T.setArch(Triple::ppc); break;
709 case Triple::ptx64: T.setArch(Triple::ptx32); break;
710 case Triple::sparcv9: T.setArch(Triple::sparc); break;
711 case Triple::x86_64: T.setArch(Triple::x86); break;
712 }
713 return T;
714}
715
716Triple Triple::get64BitArchVariant() const {
717 Triple T(*this);
718 switch (getArch()) {
719 case Triple::InvalidArch:
720 case Triple::UnknownArch:
721 case Triple::amdil:
722 case Triple::arm:
723 case Triple::cellspu:
724 case Triple::hexagon:
725 case Triple::le32:
726 case Triple::mblaze:
727 case Triple::msp430:
728 case Triple::tce:
729 case Triple::thumb:
730 case Triple::xcore:
731 T.setArch(UnknownArch);
732 break;
733
734 case Triple::mips64:
735 case Triple::mips64el:
736 case Triple::ppc64:
737 case Triple::ptx64:
738 case Triple::sparcv9:
739 case Triple::x86_64:
740 // Already 64-bit.
741 break;
742
743 case Triple::mips: T.setArch(Triple::mips64); break;
744 case Triple::mipsel: T.setArch(Triple::mips64el); break;
745 case Triple::ppc: T.setArch(Triple::ppc64); break;
746 case Triple::ptx32: T.setArch(Triple::ptx64); break;
747 case Triple::sparc: T.setArch(Triple::sparcv9); break;
748 case Triple::x86: T.setArch(Triple::x86_64); break;
749 }
750 return T;
751}