blob: 732fca5c133336b967e2ab05322011afae34d30b [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"
Duncan Sands5754a452010-09-16 08:25:48 +000012#include "llvm/ADT/STLExtras.h"
Mikhail Glushenkov70748752009-04-02 01:11:37 +000013#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000014using namespace llvm;
15
Daniel Dunbar23e97b02009-04-01 21:53:23 +000016const char *Triple::getArchTypeName(ArchType Kind) {
17 switch (Kind) {
18 case InvalidArch: return "<invalid>";
19 case UnknownArch: return "unknown";
Jim Grosbache509aa92010-12-17 02:10:59 +000020
Daniel Dunbar6337f152009-07-26 04:23:03 +000021 case arm: return "arm";
22 case cellspu: return "cellspu";
Tony Linthicumb4b54152011-12-12 21:14:40 +000023 case hexagon: return "hexagon";
Daniel Dunbar6337f152009-07-26 04:23:03 +000024 case mips: return "mips";
25 case mipsel: return "mipsel";
Akira Hatanaka70303682011-09-20 18:09:37 +000026 case mips64: return "mips64";
27 case mips64el:return "mips64el";
Daniel Dunbar6337f152009-07-26 04:23:03 +000028 case msp430: return "msp430";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000029 case ppc64: return "powerpc64";
30 case ppc: return "powerpc";
Daniel Dunbar6337f152009-07-26 04:23:03 +000031 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000032 case sparcv9: return "sparcv9";
Eli Friedman74db89e2009-08-19 20:46:03 +000033 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000034 case thumb: return "thumb";
35 case x86: return "i386";
36 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000037 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000038 case mblaze: return "mblaze";
Justin Holewinskie1fee482011-04-20 15:37:17 +000039 case ptx32: return "ptx32";
40 case ptx64: return "ptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000041 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000042 case amdil: return "amdil";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000043 }
44
45 return "<invalid>";
46}
47
Daniel Dunbar688b55b2009-08-24 09:53:06 +000048const char *Triple::getArchTypePrefix(ArchType Kind) {
49 switch (Kind) {
50 default:
51 return 0;
52
Daniel Dunbar688b55b2009-08-24 09:53:06 +000053 case arm:
54 case thumb: return "arm";
55
Daniel Dunbar688b55b2009-08-24 09:53:06 +000056 case cellspu: return "spu";
57
58 case ppc64:
59 case ppc: return "ppc";
60
Wesley Pecka70f28c2010-02-23 19:15:24 +000061 case mblaze: return "mblaze";
62
Tony Linthicumb4b54152011-12-12 21:14:40 +000063 case hexagon: return "hexagon";
64
Chris Lattner87c06d62010-02-04 06:34:01 +000065 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000066 case sparc: return "sparc";
67
68 case x86:
69 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000070
Daniel Dunbar688b55b2009-08-24 09:53:06 +000071 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000072
Justin Holewinskie1fee482011-04-20 15:37:17 +000073 case ptx32: return "ptx";
74 case ptx64: return "ptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000075 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000076 case amdil: return "amdil";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000077 }
78}
79
Daniel Dunbar23e97b02009-04-01 21:53:23 +000080const char *Triple::getVendorTypeName(VendorType Kind) {
81 switch (Kind) {
82 case UnknownVendor: return "unknown";
83
84 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000085 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000086 case SCEI: return "scei";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000087 }
88
89 return "<invalid>";
90}
91
92const char *Triple::getOSTypeName(OSType Kind) {
93 switch (Kind) {
94 case UnknownOS: return "unknown";
95
Duncan Sands852cd112009-06-19 14:40:01 +000096 case AuroraUX: return "auroraux";
Daniel Dunbar6337f152009-07-26 04:23:03 +000097 case Cygwin: return "cygwin";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000098 case Darwin: return "darwin";
Daniel Dunbar7eaf0572009-05-22 02:24:11 +000099 case DragonFly: return "dragonfly";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000100 case FreeBSD: return "freebsd";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000101 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000102 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000103 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000104 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000105 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000106 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000107 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000108 case OpenBSD: return "openbsd";
Edward O'Callaghane0fb75d2009-11-15 10:18:17 +0000109 case Psp: return "psp";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000110 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000111 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000112 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000113 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000114 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000115 case NativeClient: return "nacl";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000116 }
117
118 return "<invalid>";
119}
120
Duncan Sands5754a452010-09-16 08:25:48 +0000121const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
122 switch (Kind) {
123 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000124 case GNU: return "gnu";
125 case GNUEABI: return "gnueabi";
126 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000127 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000128 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000129 }
130
131 return "<invalid>";
132}
133
Daniel Dunbar2928c832009-11-06 10:58:06 +0000134Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000135 if (Name == "arm")
136 return arm;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000137 if (Name == "cellspu")
138 return cellspu;
139 if (Name == "mips")
140 return mips;
141 if (Name == "mipsel")
142 return mipsel;
Akira Hatanaka70303682011-09-20 18:09:37 +0000143 if (Name == "mips64")
144 return mips64;
145 if (Name == "mips64el")
146 return mips64el;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000147 if (Name == "msp430")
148 return msp430;
149 if (Name == "ppc64")
150 return ppc64;
NAKAMURA Takumi12b27722011-07-22 04:02:22 +0000151 if (Name == "ppc32")
152 return ppc;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000153 if (Name == "ppc")
154 return ppc;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000155 if (Name == "mblaze")
156 return mblaze;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000157 if (Name == "hexagon")
158 return hexagon;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000159 if (Name == "sparc")
160 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000161 if (Name == "sparcv9")
162 return sparcv9;
Eli Friedman74db89e2009-08-19 20:46:03 +0000163 if (Name == "tce")
164 return tce;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000165 if (Name == "thumb")
166 return thumb;
167 if (Name == "x86")
168 return x86;
Chris Lattnerb796c4f2009-08-12 06:45:02 +0000169 if (Name == "x86-64")
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000170 return x86_64;
171 if (Name == "xcore")
172 return xcore;
Justin Holewinskie1fee482011-04-20 15:37:17 +0000173 if (Name == "ptx32")
174 return ptx32;
175 if (Name == "ptx64")
176 return ptx64;
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000177 if (Name == "le32")
178 return le32;
Tobias Grosser05d71382011-08-29 15:44:55 +0000179 if (Name == "amdil")
180 return amdil;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000181
182 return UnknownArch;
183}
184
Daniel Dunbar2928c832009-11-06 10:58:06 +0000185Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000186 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
187 // archs which Darwin doesn't use.
188
189 // The matching this routine does is fairly pointless, since it is neither the
190 // complete architecture list, nor a reasonable subset. The problem is that
191 // historically the driver driver accepts this and also ties its -march=
192 // handling to the architecture name, so we need to be careful before removing
193 // support for it.
194
Daniel Dunbared687882009-09-09 23:01:25 +0000195 // This code must be kept in sync with Clang's Darwin specific argument
196 // translation.
197
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000198 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
199 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
200 Str == "ppc7450" || Str == "ppc970")
201 return Triple::ppc;
202
203 if (Str == "ppc64")
204 return Triple::ppc64;
205
206 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
207 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
208 Str == "pentIIm5" || Str == "pentium4")
209 return Triple::x86;
210
211 if (Str == "x86_64")
212 return Triple::x86_64;
213
214 // This is derived from the driver driver.
215 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
Sean Callananefd79192011-07-30 01:29:54 +0000216 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
217 Str == "armv7s")
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000218 return Triple::arm;
219
Justin Holewinskie1fee482011-04-20 15:37:17 +0000220 if (Str == "ptx32")
221 return Triple::ptx32;
222 if (Str == "ptx64")
223 return Triple::ptx64;
Tobias Grosser05d71382011-08-29 15:44:55 +0000224 if (Str == "amdil")
225 return Triple::amdil;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000226
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000227 return Triple::UnknownArch;
228}
229
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000230// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000231const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000232 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000233 return NULL;
234
235 StringRef Str = getArchName();
236 if (Str == "i386")
237 return "i386";
238 if (Str == "x86_64")
239 return "x86_64";
240 if (Str == "powerpc")
241 return "ppc";
242 if (Str == "powerpc64")
243 return "ppc64";
Wesley Pecka70f28c2010-02-23 19:15:24 +0000244 if (Str == "mblaze" || Str == "microblaze")
245 return "mblaze";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000246 if (Str == "arm")
247 return "arm";
248 if (Str == "armv4t" || Str == "thumbv4t")
249 return "armv4t";
Jim Grosbache509aa92010-12-17 02:10:59 +0000250 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
251 || Str == "thumbv5e")
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000252 return "armv5";
253 if (Str == "armv6" || Str == "thumbv6")
254 return "armv6";
255 if (Str == "armv7" || Str == "thumbv7")
256 return "armv7";
Justin Holewinskie1fee482011-04-20 15:37:17 +0000257 if (Str == "ptx32")
258 return "ptx32";
259 if (Str == "ptx64")
260 return "ptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000261 if (Str == "le32")
262 return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +0000263 if (Str == "amdil")
264 return "amdil";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000265 return NULL;
266}
267
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000268//
269
Duncan Sands335db222010-08-12 11:31:39 +0000270Triple::ArchType Triple::ParseArch(StringRef ArchName) {
Jim Grosbache509aa92010-12-17 02:10:59 +0000271 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
272 ArchName[2] == '8' && ArchName[3] == '6' &&
Daniel Dunbar6337f152009-07-26 04:23:03 +0000273 ArchName[1] - '3' < 6) // i[3-9]86
Duncan Sands335db222010-08-12 11:31:39 +0000274 return x86;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000275 else if (ArchName == "amd64" || ArchName == "x86_64")
Duncan Sands335db222010-08-12 11:31:39 +0000276 return x86_64;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000277 else if (ArchName == "powerpc")
Duncan Sands335db222010-08-12 11:31:39 +0000278 return ppc;
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000279 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
Duncan Sands335db222010-08-12 11:31:39 +0000280 return ppc64;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000281 else if (ArchName == "mblaze")
Duncan Sands335db222010-08-12 11:31:39 +0000282 return mblaze;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000283 else if (ArchName == "arm" ||
Daniel Dunbar24473892009-08-18 04:51:26 +0000284 ArchName.startswith("armv") ||
285 ArchName == "xscale")
Duncan Sands335db222010-08-12 11:31:39 +0000286 return arm;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000287 else if (ArchName == "thumb" ||
288 ArchName.startswith("thumbv"))
Duncan Sands335db222010-08-12 11:31:39 +0000289 return thumb;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000290 else if (ArchName == "spu" || ArchName == "cellspu")
Duncan Sands335db222010-08-12 11:31:39 +0000291 return cellspu;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000292 else if (ArchName == "msp430")
Duncan Sands335db222010-08-12 11:31:39 +0000293 return msp430;
Joerg Sonnenbergerf86b76e2011-07-07 16:53:52 +0000294 else if (ArchName == "mips" || ArchName == "mipseb" ||
295 ArchName == "mipsallegrex")
Duncan Sands335db222010-08-12 11:31:39 +0000296 return mips;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000297 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
298 ArchName == "psp")
Duncan Sands335db222010-08-12 11:31:39 +0000299 return mipsel;
Akira Hatanaka70303682011-09-20 18:09:37 +0000300 else if (ArchName == "mips64" || ArchName == "mips64eb")
301 return mips64;
302 else if (ArchName == "mips64el")
303 return mips64el;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000304 else if (ArchName == "hexagon")
305 return hexagon;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000306 else if (ArchName == "sparc")
Duncan Sands335db222010-08-12 11:31:39 +0000307 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000308 else if (ArchName == "sparcv9")
Duncan Sands335db222010-08-12 11:31:39 +0000309 return sparcv9;
Eli Friedman74db89e2009-08-19 20:46:03 +0000310 else if (ArchName == "tce")
Duncan Sands335db222010-08-12 11:31:39 +0000311 return tce;
Richard Osborne768f1dd2009-08-31 21:51:36 +0000312 else if (ArchName == "xcore")
Duncan Sands335db222010-08-12 11:31:39 +0000313 return xcore;
Justin Holewinskie1fee482011-04-20 15:37:17 +0000314 else if (ArchName == "ptx32")
315 return ptx32;
316 else if (ArchName == "ptx64")
317 return ptx64;
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000318 else if (ArchName == "le32")
319 return le32;
Tobias Grosser05d71382011-08-29 15:44:55 +0000320 else if (ArchName == "amdil")
321 return amdil;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000322 else
Duncan Sands335db222010-08-12 11:31:39 +0000323 return UnknownArch;
324}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000325
Duncan Sands335db222010-08-12 11:31:39 +0000326Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
327 if (VendorName == "apple")
328 return Apple;
329 else if (VendorName == "pc")
330 return PC;
John Thompson6046cff2011-03-15 21:51:56 +0000331 else if (VendorName == "scei")
332 return SCEI;
Duncan Sands335db222010-08-12 11:31:39 +0000333 else
334 return UnknownVendor;
335}
336
337Triple::OSType Triple::ParseOS(StringRef OSName) {
338 if (OSName.startswith("auroraux"))
339 return AuroraUX;
340 else if (OSName.startswith("cygwin"))
341 return Cygwin;
342 else if (OSName.startswith("darwin"))
343 return Darwin;
344 else if (OSName.startswith("dragonfly"))
345 return DragonFly;
346 else if (OSName.startswith("freebsd"))
347 return FreeBSD;
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000348 else if (OSName.startswith("ios"))
349 return IOS;
Duncan Sands652b48b2011-07-26 15:30:04 +0000350 else if (OSName.startswith("kfreebsd"))
351 return KFreeBSD;
Duncan Sands335db222010-08-12 11:31:39 +0000352 else if (OSName.startswith("linux"))
353 return Linux;
354 else if (OSName.startswith("lv2"))
355 return Lv2;
Daniel Dunbar1af39472011-04-19 23:34:12 +0000356 else if (OSName.startswith("macosx"))
357 return MacOSX;
Duncan Sands335db222010-08-12 11:31:39 +0000358 else if (OSName.startswith("mingw32"))
359 return MinGW32;
Duncan Sands335db222010-08-12 11:31:39 +0000360 else if (OSName.startswith("netbsd"))
361 return NetBSD;
362 else if (OSName.startswith("openbsd"))
363 return OpenBSD;
364 else if (OSName.startswith("psp"))
365 return Psp;
366 else if (OSName.startswith("solaris"))
367 return Solaris;
368 else if (OSName.startswith("win32"))
369 return Win32;
370 else if (OSName.startswith("haiku"))
371 return Haiku;
372 else if (OSName.startswith("minix"))
373 return Minix;
Eli Friedmanb0e77a22011-07-06 20:56:26 +0000374 else if (OSName.startswith("rtems"))
375 return RTEMS;
Ivan Krasind8834532011-08-22 23:08:53 +0000376 else if (OSName.startswith("nacl"))
377 return NativeClient;
Duncan Sands335db222010-08-12 11:31:39 +0000378 else
379 return UnknownOS;
380}
381
Duncan Sands5754a452010-09-16 08:25:48 +0000382Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
Renato Golin859f8182011-01-21 18:25:47 +0000383 if (EnvironmentName.startswith("eabi"))
384 return EABI;
385 else if (EnvironmentName.startswith("gnueabi"))
386 return GNUEABI;
387 else if (EnvironmentName.startswith("gnu"))
388 return GNU;
Evan Cheng2bffee22011-02-01 01:14:13 +0000389 else if (EnvironmentName.startswith("macho"))
390 return MachO;
Chandler Carruthfd553c22012-01-10 19:46:00 +0000391 else if (EnvironmentName.startswith("androideabi"))
392 return ANDROIDEABI;
Renato Golin859f8182011-01-21 18:25:47 +0000393 else
394 return UnknownEnvironment;
Duncan Sands5754a452010-09-16 08:25:48 +0000395}
396
Duncan Sands335db222010-08-12 11:31:39 +0000397void Triple::Parse() const {
398 assert(!isInitialized() && "Invalid parse call.");
399
400 Arch = ParseArch(getArchName());
401 Vendor = ParseVendor(getVendorName());
402 OS = ParseOS(getOSName());
Duncan Sandsae200c62011-02-02 10:08:38 +0000403 Environment = ParseEnvironment(getEnvironmentName());
Daniel Dunbar651aa682009-08-18 19:26:55 +0000404
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000405 assert(isInitialized() && "Failed to initialize!");
406}
407
Duncan Sands335db222010-08-12 11:31:39 +0000408std::string Triple::normalize(StringRef Str) {
409 // Parse into components.
410 SmallVector<StringRef, 4> Components;
411 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
412 Last = Str.find('-', First);
413 Components.push_back(Str.slice(First, Last));
414 }
415
416 // If the first component corresponds to a known architecture, preferentially
417 // use it for the architecture. If the second component corresponds to a
418 // known vendor, preferentially use it for the vendor, etc. This avoids silly
419 // component movement when a component parses as (eg) both a valid arch and a
420 // valid os.
421 ArchType Arch = UnknownArch;
422 if (Components.size() > 0)
423 Arch = ParseArch(Components[0]);
424 VendorType Vendor = UnknownVendor;
425 if (Components.size() > 1)
426 Vendor = ParseVendor(Components[1]);
427 OSType OS = UnknownOS;
428 if (Components.size() > 2)
429 OS = ParseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000430 EnvironmentType Environment = UnknownEnvironment;
431 if (Components.size() > 3)
432 Environment = ParseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000433
434 // Note which components are already in their final position. These will not
435 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000436 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000437 Found[0] = Arch != UnknownArch;
438 Found[1] = Vendor != UnknownVendor;
439 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000440 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000441
442 // If they are not there already, permute the components into their canonical
443 // positions by seeing if they parse as a valid architecture, and if so moving
444 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000445 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000446 if (Found[Pos])
447 continue; // Already in the canonical position.
448
449 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
450 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000451 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000452 continue;
453
454 // Does this component parse as valid for the target position?
455 bool Valid = false;
456 StringRef Comp = Components[Idx];
457 switch (Pos) {
458 default:
459 assert(false && "unexpected component type!");
460 case 0:
461 Arch = ParseArch(Comp);
462 Valid = Arch != UnknownArch;
463 break;
464 case 1:
465 Vendor = ParseVendor(Comp);
466 Valid = Vendor != UnknownVendor;
467 break;
468 case 2:
469 OS = ParseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000470 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000471 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000472 case 3:
473 Environment = ParseEnvironment(Comp);
474 Valid = Environment != UnknownEnvironment;
475 break;
Duncan Sands335db222010-08-12 11:31:39 +0000476 }
477 if (!Valid)
478 continue; // Nope, try the next component.
479
480 // Move the component to the target position, pushing any non-fixed
481 // components that are in the way to the right. This tends to give
482 // good results in the common cases of a forgotten vendor component
483 // or a wrongly positioned environment.
484 if (Pos < Idx) {
485 // Insert left, pushing the existing components to the right. For
486 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
487 StringRef CurrentComponent(""); // The empty component.
488 // Replace the component we are moving with an empty component.
489 std::swap(CurrentComponent, Components[Idx]);
490 // Insert the component being moved at Pos, displacing any existing
491 // components to the right.
492 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
493 // Skip over any fixed components.
Duncan Sands5754a452010-09-16 08:25:48 +0000494 while (i < array_lengthof(Found) && Found[i]) ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000495 // Place the component at the new position, getting the component
496 // that was at this position - it will be moved right.
497 std::swap(CurrentComponent, Components[i]);
498 }
499 } else if (Pos > Idx) {
500 // Push right by inserting empty components until the component at Idx
501 // reaches the target position Pos. For example, pc-a -> -pc-a when
502 // moving pc to the second position.
503 do {
504 // Insert one empty component at Idx.
505 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000506 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000507 // Place the component at the new position, getting the component
508 // that was at this position - it will be moved right.
509 std::swap(CurrentComponent, Components[i]);
510 // If it was placed on top of an empty component then we are done.
511 if (CurrentComponent.empty())
512 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000513 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000514 while (++i < array_lengthof(Found) && Found[i])
515 ;
Duncan Sands335db222010-08-12 11:31:39 +0000516 }
517 // The last component was pushed off the end - append it.
518 if (!CurrentComponent.empty())
519 Components.push_back(CurrentComponent);
520
521 // Advance Idx to the component's new position.
Duncan Sands5754a452010-09-16 08:25:48 +0000522 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
Duncan Sands335db222010-08-12 11:31:39 +0000523 } while (Idx < Pos); // Add more until the final position is reached.
524 }
525 assert(Pos < Components.size() && Components[Pos] == Comp &&
526 "Component moved wrong!");
527 Found[Pos] = true;
528 break;
529 }
530 }
531
532 // Special case logic goes here. At this point Arch, Vendor and OS have the
533 // correct values for the computed components.
534
535 // Stick the corrected components back together to form the normalized string.
536 std::string Normalized;
537 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
538 if (i) Normalized += '-';
539 Normalized += Components[i];
540 }
541 return Normalized;
542}
543
Daniel Dunbara14d2252009-07-26 03:31:47 +0000544StringRef Triple::getArchName() const {
545 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000546}
547
Daniel Dunbara14d2252009-07-26 03:31:47 +0000548StringRef Triple::getVendorName() const {
549 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
550 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000551}
552
Daniel Dunbara14d2252009-07-26 03:31:47 +0000553StringRef Triple::getOSName() const {
554 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
555 Tmp = Tmp.split('-').second; // Strip second component
556 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000557}
558
Daniel Dunbara14d2252009-07-26 03:31:47 +0000559StringRef Triple::getEnvironmentName() const {
560 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
561 Tmp = Tmp.split('-').second; // Strip second component
562 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000563}
564
Daniel Dunbara14d2252009-07-26 03:31:47 +0000565StringRef Triple::getOSAndEnvironmentName() const {
566 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
567 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000568}
569
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000570static unsigned EatNumber(StringRef &Str) {
571 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000572 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000573
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000574 do {
575 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000576 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000577
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000578 // Eat the digit.
579 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000580 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000581
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000582 return Result;
583}
584
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000585void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
586 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000587 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000588
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000589 // Assume that the OS portion of the triple starts with the canonical name.
590 StringRef OSTypeName = getOSTypeName(getOS());
591 if (OSName.startswith(OSTypeName))
592 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000593
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000594 // Any unset version defaults to 0.
595 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000596
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000597 // Parse up to three components.
598 unsigned *Components[3] = { &Major, &Minor, &Micro };
599 for (unsigned i = 0; i != 3; ++i) {
600 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
601 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000602
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000603 // Consume the leading number.
604 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000605
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000606 // Consume the separator, if present.
607 if (OSName.startswith("."))
608 OSName = OSName.substr(1);
609 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000610}
611
Daniel Dunbara14d2252009-07-26 03:31:47 +0000612void Triple::setTriple(const Twine &Str) {
613 Data = Str.str();
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000614 Arch = InvalidArch;
615}
616
617void Triple::setArch(ArchType Kind) {
618 setArchName(getArchTypeName(Kind));
619}
620
621void Triple::setVendor(VendorType Kind) {
622 setVendorName(getVendorTypeName(Kind));
623}
624
625void Triple::setOS(OSType Kind) {
626 setOSName(getOSTypeName(Kind));
627}
628
Duncan Sands5754a452010-09-16 08:25:48 +0000629void Triple::setEnvironment(EnvironmentType Kind) {
630 setEnvironmentName(getEnvironmentTypeName(Kind));
631}
632
Daniel Dunbar2928c832009-11-06 10:58:06 +0000633void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000634 // Work around a miscompilation bug for Twines in gcc 4.0.3.
635 SmallString<64> Triple;
636 Triple += Str;
637 Triple += "-";
638 Triple += getVendorName();
639 Triple += "-";
640 Triple += getOSAndEnvironmentName();
641 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000642}
643
Daniel Dunbar2928c832009-11-06 10:58:06 +0000644void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000645 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
646}
647
Daniel Dunbar2928c832009-11-06 10:58:06 +0000648void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000649 if (hasEnvironment())
650 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
651 "-" + getEnvironmentName());
652 else
653 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
654}
655
Daniel Dunbar2928c832009-11-06 10:58:06 +0000656void Triple::setEnvironmentName(StringRef Str) {
657 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000658 "-" + Str);
659}
660
Daniel Dunbar2928c832009-11-06 10:58:06 +0000661void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000662 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
663}