blob: e69d4385c6945461d596246c6fb4bf09682276db [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"
David Blaikie4d6ccb52012-01-20 21:51:11 +000013#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov70748752009-04-02 01:11:37 +000014#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000015using namespace llvm;
16
Daniel Dunbar23e97b02009-04-01 21:53:23 +000017const char *Triple::getArchTypeName(ArchType Kind) {
18 switch (Kind) {
19 case InvalidArch: return "<invalid>";
20 case UnknownArch: return "unknown";
Jim Grosbache509aa92010-12-17 02:10:59 +000021
Daniel Dunbar6337f152009-07-26 04:23:03 +000022 case arm: return "arm";
23 case cellspu: return "cellspu";
Tony Linthicumb4b54152011-12-12 21:14:40 +000024 case hexagon: return "hexagon";
Daniel Dunbar6337f152009-07-26 04:23:03 +000025 case mips: return "mips";
26 case mipsel: return "mipsel";
Akira Hatanaka70303682011-09-20 18:09:37 +000027 case mips64: return "mips64";
28 case mips64el:return "mips64el";
Daniel Dunbar6337f152009-07-26 04:23:03 +000029 case msp430: return "msp430";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000030 case ppc64: return "powerpc64";
31 case ppc: return "powerpc";
Daniel Dunbar6337f152009-07-26 04:23:03 +000032 case sparc: return "sparc";
Chris Lattner87c06d62010-02-04 06:34:01 +000033 case sparcv9: return "sparcv9";
Eli Friedman74db89e2009-08-19 20:46:03 +000034 case tce: return "tce";
Daniel Dunbar6337f152009-07-26 04:23:03 +000035 case thumb: return "thumb";
36 case x86: return "i386";
37 case x86_64: return "x86_64";
Daniel Dunbar8c2f1d72009-07-26 04:52:45 +000038 case xcore: return "xcore";
Wesley Pecka70f28c2010-02-23 19:15:24 +000039 case mblaze: return "mblaze";
Justin Holewinskie1fee482011-04-20 15:37:17 +000040 case ptx32: return "ptx32";
41 case ptx64: return "ptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000042 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000043 case amdil: return "amdil";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000044 }
45
David Blaikie4d6ccb52012-01-20 21:51:11 +000046 llvm_unreachable("Invalid ArchType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000047}
48
Daniel Dunbar688b55b2009-08-24 09:53:06 +000049const char *Triple::getArchTypePrefix(ArchType Kind) {
50 switch (Kind) {
51 default:
52 return 0;
53
Daniel Dunbar688b55b2009-08-24 09:53:06 +000054 case arm:
55 case thumb: return "arm";
56
Daniel Dunbar688b55b2009-08-24 09:53:06 +000057 case cellspu: return "spu";
58
59 case ppc64:
60 case ppc: return "ppc";
61
Wesley Pecka70f28c2010-02-23 19:15:24 +000062 case mblaze: return "mblaze";
63
Tony Linthicumb4b54152011-12-12 21:14:40 +000064 case hexagon: return "hexagon";
65
Chris Lattner87c06d62010-02-04 06:34:01 +000066 case sparcv9:
Daniel Dunbar688b55b2009-08-24 09:53:06 +000067 case sparc: return "sparc";
68
69 case x86:
70 case x86_64: return "x86";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000071
Daniel Dunbar688b55b2009-08-24 09:53:06 +000072 case xcore: return "xcore";
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000073
Justin Holewinskie1fee482011-04-20 15:37:17 +000074 case ptx32: return "ptx";
75 case ptx64: return "ptx";
Ivan Krasin38fb2db2011-08-23 16:59:00 +000076 case le32: return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +000077 case amdil: return "amdil";
Daniel Dunbar688b55b2009-08-24 09:53:06 +000078 }
79}
80
Daniel Dunbar23e97b02009-04-01 21:53:23 +000081const char *Triple::getVendorTypeName(VendorType Kind) {
82 switch (Kind) {
83 case UnknownVendor: return "unknown";
84
85 case Apple: return "apple";
Chris Lattner56ce0f42009-08-14 18:48:13 +000086 case PC: return "pc";
John Thompson6046cff2011-03-15 21:51:56 +000087 case SCEI: return "scei";
Daniel Dunbar23e97b02009-04-01 21:53:23 +000088 }
89
David Blaikie4d6ccb52012-01-20 21:51:11 +000090 llvm_unreachable("Invalid VendorType!");
Daniel Dunbar23e97b02009-04-01 21:53:23 +000091}
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";
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000102 case IOS: return "ios";
Duncan Sands652b48b2011-07-26 15:30:04 +0000103 case KFreeBSD: return "kfreebsd";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000104 case Linux: return "linux";
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000105 case Lv2: return "lv2";
Daniel Dunbar1af39472011-04-19 23:34:12 +0000106 case MacOSX: return "macosx";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000107 case MinGW32: return "mingw32";
Chris Lattnerb8ac8412009-07-13 20:22:23 +0000108 case NetBSD: return "netbsd";
Duncan Sandscd1267d2009-06-29 13:36:13 +0000109 case OpenBSD: return "openbsd";
Edward O'Callaghane0fb75d2009-11-15 10:18:17 +0000110 case Psp: return "psp";
Daniel Dunbarfdb0b7b2009-08-18 04:43:27 +0000111 case Solaris: return "solaris";
Daniel Dunbar6337f152009-07-26 04:23:03 +0000112 case Win32: return "win32";
Chris Lattnera43fc342009-10-16 02:06:30 +0000113 case Haiku: return "haiku";
Chris Lattner29269d02010-07-07 15:52:27 +0000114 case Minix: return "minix";
Douglas Gregor6ced1d12011-07-01 22:41:06 +0000115 case RTEMS: return "rtems";
Ivan Krasinfb234622011-08-18 22:54:21 +0000116 case NativeClient: return "nacl";
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000117 }
118
David Blaikie4d6ccb52012-01-20 21:51:11 +0000119 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000120}
121
Duncan Sands5754a452010-09-16 08:25:48 +0000122const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
123 switch (Kind) {
124 case UnknownEnvironment: return "unknown";
Renato Golin859f8182011-01-21 18:25:47 +0000125 case GNU: return "gnu";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000126 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000127 case GNUEABI: return "gnueabi";
128 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000129 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000130 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000131 }
132
David Blaikie4d6ccb52012-01-20 21:51:11 +0000133 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000134}
135
Daniel Dunbar2928c832009-11-06 10:58:06 +0000136Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000137 if (Name == "arm")
138 return arm;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000139 if (Name == "cellspu")
140 return cellspu;
141 if (Name == "mips")
142 return mips;
143 if (Name == "mipsel")
144 return mipsel;
Akira Hatanaka70303682011-09-20 18:09:37 +0000145 if (Name == "mips64")
146 return mips64;
147 if (Name == "mips64el")
148 return mips64el;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000149 if (Name == "msp430")
150 return msp430;
151 if (Name == "ppc64")
152 return ppc64;
NAKAMURA Takumi12b27722011-07-22 04:02:22 +0000153 if (Name == "ppc32")
154 return ppc;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000155 if (Name == "ppc")
156 return ppc;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000157 if (Name == "mblaze")
158 return mblaze;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000159 if (Name == "hexagon")
160 return hexagon;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000161 if (Name == "sparc")
162 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000163 if (Name == "sparcv9")
164 return sparcv9;
Eli Friedman74db89e2009-08-19 20:46:03 +0000165 if (Name == "tce")
166 return tce;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000167 if (Name == "thumb")
168 return thumb;
169 if (Name == "x86")
170 return x86;
Chris Lattnerb796c4f2009-08-12 06:45:02 +0000171 if (Name == "x86-64")
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000172 return x86_64;
173 if (Name == "xcore")
174 return xcore;
Justin Holewinskie1fee482011-04-20 15:37:17 +0000175 if (Name == "ptx32")
176 return ptx32;
177 if (Name == "ptx64")
178 return ptx64;
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000179 if (Name == "le32")
180 return le32;
Tobias Grosser05d71382011-08-29 15:44:55 +0000181 if (Name == "amdil")
182 return amdil;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000183
184 return UnknownArch;
185}
186
Daniel Dunbar2928c832009-11-06 10:58:06 +0000187Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000188 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
189 // archs which Darwin doesn't use.
190
191 // The matching this routine does is fairly pointless, since it is neither the
192 // complete architecture list, nor a reasonable subset. The problem is that
193 // historically the driver driver accepts this and also ties its -march=
194 // handling to the architecture name, so we need to be careful before removing
195 // support for it.
196
Daniel Dunbared687882009-09-09 23:01:25 +0000197 // This code must be kept in sync with Clang's Darwin specific argument
198 // translation.
199
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000200 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
201 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
202 Str == "ppc7450" || Str == "ppc970")
203 return Triple::ppc;
204
205 if (Str == "ppc64")
206 return Triple::ppc64;
207
208 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
209 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
210 Str == "pentIIm5" || Str == "pentium4")
211 return Triple::x86;
212
213 if (Str == "x86_64")
214 return Triple::x86_64;
215
216 // This is derived from the driver driver.
217 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
Sean Callananefd79192011-07-30 01:29:54 +0000218 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
219 Str == "armv7s")
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000220 return Triple::arm;
221
Justin Holewinskie1fee482011-04-20 15:37:17 +0000222 if (Str == "ptx32")
223 return Triple::ptx32;
224 if (Str == "ptx64")
225 return Triple::ptx64;
Tobias Grosser05d71382011-08-29 15:44:55 +0000226 if (Str == "amdil")
227 return Triple::amdil;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000228
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000229 return Triple::UnknownArch;
230}
231
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000232// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000233const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000234 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000235 return NULL;
236
237 StringRef Str = getArchName();
238 if (Str == "i386")
239 return "i386";
240 if (Str == "x86_64")
241 return "x86_64";
242 if (Str == "powerpc")
243 return "ppc";
244 if (Str == "powerpc64")
245 return "ppc64";
Wesley Pecka70f28c2010-02-23 19:15:24 +0000246 if (Str == "mblaze" || Str == "microblaze")
247 return "mblaze";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000248 if (Str == "arm")
249 return "arm";
250 if (Str == "armv4t" || Str == "thumbv4t")
251 return "armv4t";
Jim Grosbache509aa92010-12-17 02:10:59 +0000252 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
253 || Str == "thumbv5e")
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000254 return "armv5";
255 if (Str == "armv6" || Str == "thumbv6")
256 return "armv6";
257 if (Str == "armv7" || Str == "thumbv7")
258 return "armv7";
Justin Holewinskie1fee482011-04-20 15:37:17 +0000259 if (Str == "ptx32")
260 return "ptx32";
261 if (Str == "ptx64")
262 return "ptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000263 if (Str == "le32")
264 return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +0000265 if (Str == "amdil")
266 return "amdil";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000267 return NULL;
268}
269
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000270//
271
Duncan Sands335db222010-08-12 11:31:39 +0000272Triple::ArchType Triple::ParseArch(StringRef ArchName) {
Jim Grosbache509aa92010-12-17 02:10:59 +0000273 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
274 ArchName[2] == '8' && ArchName[3] == '6' &&
Daniel Dunbar6337f152009-07-26 04:23:03 +0000275 ArchName[1] - '3' < 6) // i[3-9]86
Duncan Sands335db222010-08-12 11:31:39 +0000276 return x86;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000277 else if (ArchName == "amd64" || ArchName == "x86_64")
Duncan Sands335db222010-08-12 11:31:39 +0000278 return x86_64;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000279 else if (ArchName == "powerpc")
Duncan Sands335db222010-08-12 11:31:39 +0000280 return ppc;
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000281 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
Duncan Sands335db222010-08-12 11:31:39 +0000282 return ppc64;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000283 else if (ArchName == "mblaze")
Duncan Sands335db222010-08-12 11:31:39 +0000284 return mblaze;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000285 else if (ArchName == "arm" ||
Daniel Dunbar24473892009-08-18 04:51:26 +0000286 ArchName.startswith("armv") ||
287 ArchName == "xscale")
Duncan Sands335db222010-08-12 11:31:39 +0000288 return arm;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000289 else if (ArchName == "thumb" ||
290 ArchName.startswith("thumbv"))
Duncan Sands335db222010-08-12 11:31:39 +0000291 return thumb;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000292 else if (ArchName == "spu" || ArchName == "cellspu")
Duncan Sands335db222010-08-12 11:31:39 +0000293 return cellspu;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000294 else if (ArchName == "msp430")
Duncan Sands335db222010-08-12 11:31:39 +0000295 return msp430;
Joerg Sonnenbergerf86b76e2011-07-07 16:53:52 +0000296 else if (ArchName == "mips" || ArchName == "mipseb" ||
297 ArchName == "mipsallegrex")
Duncan Sands335db222010-08-12 11:31:39 +0000298 return mips;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000299 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
300 ArchName == "psp")
Duncan Sands335db222010-08-12 11:31:39 +0000301 return mipsel;
Akira Hatanaka70303682011-09-20 18:09:37 +0000302 else if (ArchName == "mips64" || ArchName == "mips64eb")
303 return mips64;
304 else if (ArchName == "mips64el")
305 return mips64el;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000306 else if (ArchName == "hexagon")
307 return hexagon;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000308 else if (ArchName == "sparc")
Duncan Sands335db222010-08-12 11:31:39 +0000309 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000310 else if (ArchName == "sparcv9")
Duncan Sands335db222010-08-12 11:31:39 +0000311 return sparcv9;
Eli Friedman74db89e2009-08-19 20:46:03 +0000312 else if (ArchName == "tce")
Duncan Sands335db222010-08-12 11:31:39 +0000313 return tce;
Richard Osborne768f1dd2009-08-31 21:51:36 +0000314 else if (ArchName == "xcore")
Duncan Sands335db222010-08-12 11:31:39 +0000315 return xcore;
Justin Holewinskie1fee482011-04-20 15:37:17 +0000316 else if (ArchName == "ptx32")
317 return ptx32;
318 else if (ArchName == "ptx64")
319 return ptx64;
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000320 else if (ArchName == "le32")
321 return le32;
Tobias Grosser05d71382011-08-29 15:44:55 +0000322 else if (ArchName == "amdil")
323 return amdil;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000324 else
Duncan Sands335db222010-08-12 11:31:39 +0000325 return UnknownArch;
326}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000327
Duncan Sands335db222010-08-12 11:31:39 +0000328Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
329 if (VendorName == "apple")
330 return Apple;
331 else if (VendorName == "pc")
332 return PC;
John Thompson6046cff2011-03-15 21:51:56 +0000333 else if (VendorName == "scei")
334 return SCEI;
Duncan Sands335db222010-08-12 11:31:39 +0000335 else
336 return UnknownVendor;
337}
338
339Triple::OSType Triple::ParseOS(StringRef OSName) {
340 if (OSName.startswith("auroraux"))
341 return AuroraUX;
342 else if (OSName.startswith("cygwin"))
343 return Cygwin;
344 else if (OSName.startswith("darwin"))
345 return Darwin;
346 else if (OSName.startswith("dragonfly"))
347 return DragonFly;
348 else if (OSName.startswith("freebsd"))
349 return FreeBSD;
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000350 else if (OSName.startswith("ios"))
351 return IOS;
Duncan Sands652b48b2011-07-26 15:30:04 +0000352 else if (OSName.startswith("kfreebsd"))
353 return KFreeBSD;
Duncan Sands335db222010-08-12 11:31:39 +0000354 else if (OSName.startswith("linux"))
355 return Linux;
356 else if (OSName.startswith("lv2"))
357 return Lv2;
Daniel Dunbar1af39472011-04-19 23:34:12 +0000358 else if (OSName.startswith("macosx"))
359 return MacOSX;
Duncan Sands335db222010-08-12 11:31:39 +0000360 else if (OSName.startswith("mingw32"))
361 return MinGW32;
Duncan Sands335db222010-08-12 11:31:39 +0000362 else if (OSName.startswith("netbsd"))
363 return NetBSD;
364 else if (OSName.startswith("openbsd"))
365 return OpenBSD;
366 else if (OSName.startswith("psp"))
367 return Psp;
368 else if (OSName.startswith("solaris"))
369 return Solaris;
370 else if (OSName.startswith("win32"))
371 return Win32;
372 else if (OSName.startswith("haiku"))
373 return Haiku;
374 else if (OSName.startswith("minix"))
375 return Minix;
Eli Friedmanb0e77a22011-07-06 20:56:26 +0000376 else if (OSName.startswith("rtems"))
377 return RTEMS;
Ivan Krasind8834532011-08-22 23:08:53 +0000378 else if (OSName.startswith("nacl"))
379 return NativeClient;
Duncan Sands335db222010-08-12 11:31:39 +0000380 else
381 return UnknownOS;
382}
383
Duncan Sands5754a452010-09-16 08:25:48 +0000384Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
Renato Golin859f8182011-01-21 18:25:47 +0000385 if (EnvironmentName.startswith("eabi"))
386 return EABI;
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000387 else if (EnvironmentName.startswith("gnueabihf"))
388 return GNUEABIHF;
Renato Golin859f8182011-01-21 18:25:47 +0000389 else if (EnvironmentName.startswith("gnueabi"))
390 return GNUEABI;
391 else if (EnvironmentName.startswith("gnu"))
392 return GNU;
Evan Cheng2bffee22011-02-01 01:14:13 +0000393 else if (EnvironmentName.startswith("macho"))
394 return MachO;
Chandler Carruthfd553c22012-01-10 19:46:00 +0000395 else if (EnvironmentName.startswith("androideabi"))
396 return ANDROIDEABI;
Renato Golin859f8182011-01-21 18:25:47 +0000397 else
398 return UnknownEnvironment;
Duncan Sands5754a452010-09-16 08:25:48 +0000399}
400
Duncan Sands335db222010-08-12 11:31:39 +0000401void Triple::Parse() const {
402 assert(!isInitialized() && "Invalid parse call.");
403
404 Arch = ParseArch(getArchName());
405 Vendor = ParseVendor(getVendorName());
406 OS = ParseOS(getOSName());
Duncan Sandsae200c62011-02-02 10:08:38 +0000407 Environment = ParseEnvironment(getEnvironmentName());
Daniel Dunbar651aa682009-08-18 19:26:55 +0000408
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000409 assert(isInitialized() && "Failed to initialize!");
410}
411
Duncan Sands335db222010-08-12 11:31:39 +0000412std::string Triple::normalize(StringRef Str) {
413 // Parse into components.
414 SmallVector<StringRef, 4> Components;
415 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
416 Last = Str.find('-', First);
417 Components.push_back(Str.slice(First, Last));
418 }
419
420 // If the first component corresponds to a known architecture, preferentially
421 // use it for the architecture. If the second component corresponds to a
422 // known vendor, preferentially use it for the vendor, etc. This avoids silly
423 // component movement when a component parses as (eg) both a valid arch and a
424 // valid os.
425 ArchType Arch = UnknownArch;
426 if (Components.size() > 0)
427 Arch = ParseArch(Components[0]);
428 VendorType Vendor = UnknownVendor;
429 if (Components.size() > 1)
430 Vendor = ParseVendor(Components[1]);
431 OSType OS = UnknownOS;
432 if (Components.size() > 2)
433 OS = ParseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000434 EnvironmentType Environment = UnknownEnvironment;
435 if (Components.size() > 3)
436 Environment = ParseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000437
438 // Note which components are already in their final position. These will not
439 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000440 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000441 Found[0] = Arch != UnknownArch;
442 Found[1] = Vendor != UnknownVendor;
443 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000444 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000445
446 // If they are not there already, permute the components into their canonical
447 // positions by seeing if they parse as a valid architecture, and if so moving
448 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000449 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000450 if (Found[Pos])
451 continue; // Already in the canonical position.
452
453 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
454 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000455 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000456 continue;
457
458 // Does this component parse as valid for the target position?
459 bool Valid = false;
460 StringRef Comp = Components[Idx];
461 switch (Pos) {
462 default:
463 assert(false && "unexpected component type!");
464 case 0:
465 Arch = ParseArch(Comp);
466 Valid = Arch != UnknownArch;
467 break;
468 case 1:
469 Vendor = ParseVendor(Comp);
470 Valid = Vendor != UnknownVendor;
471 break;
472 case 2:
473 OS = ParseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000474 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000475 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000476 case 3:
477 Environment = ParseEnvironment(Comp);
478 Valid = Environment != UnknownEnvironment;
479 break;
Duncan Sands335db222010-08-12 11:31:39 +0000480 }
481 if (!Valid)
482 continue; // Nope, try the next component.
483
484 // Move the component to the target position, pushing any non-fixed
485 // components that are in the way to the right. This tends to give
486 // good results in the common cases of a forgotten vendor component
487 // or a wrongly positioned environment.
488 if (Pos < Idx) {
489 // Insert left, pushing the existing components to the right. For
490 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
491 StringRef CurrentComponent(""); // The empty component.
492 // Replace the component we are moving with an empty component.
493 std::swap(CurrentComponent, Components[Idx]);
494 // Insert the component being moved at Pos, displacing any existing
495 // components to the right.
496 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
497 // Skip over any fixed components.
Duncan Sands5754a452010-09-16 08:25:48 +0000498 while (i < array_lengthof(Found) && Found[i]) ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000499 // Place the component at the new position, getting the component
500 // that was at this position - it will be moved right.
501 std::swap(CurrentComponent, Components[i]);
502 }
503 } else if (Pos > Idx) {
504 // Push right by inserting empty components until the component at Idx
505 // reaches the target position Pos. For example, pc-a -> -pc-a when
506 // moving pc to the second position.
507 do {
508 // Insert one empty component at Idx.
509 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000510 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000511 // Place the component at the new position, getting the component
512 // that was at this position - it will be moved right.
513 std::swap(CurrentComponent, Components[i]);
514 // If it was placed on top of an empty component then we are done.
515 if (CurrentComponent.empty())
516 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000517 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000518 while (++i < array_lengthof(Found) && Found[i])
519 ;
Duncan Sands335db222010-08-12 11:31:39 +0000520 }
521 // The last component was pushed off the end - append it.
522 if (!CurrentComponent.empty())
523 Components.push_back(CurrentComponent);
524
525 // Advance Idx to the component's new position.
Duncan Sands5754a452010-09-16 08:25:48 +0000526 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
Duncan Sands335db222010-08-12 11:31:39 +0000527 } while (Idx < Pos); // Add more until the final position is reached.
528 }
529 assert(Pos < Components.size() && Components[Pos] == Comp &&
530 "Component moved wrong!");
531 Found[Pos] = true;
532 break;
533 }
534 }
535
536 // Special case logic goes here. At this point Arch, Vendor and OS have the
537 // correct values for the computed components.
538
539 // Stick the corrected components back together to form the normalized string.
540 std::string Normalized;
541 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
542 if (i) Normalized += '-';
543 Normalized += Components[i];
544 }
545 return Normalized;
546}
547
Daniel Dunbara14d2252009-07-26 03:31:47 +0000548StringRef Triple::getArchName() const {
549 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000550}
551
Daniel Dunbara14d2252009-07-26 03:31:47 +0000552StringRef Triple::getVendorName() const {
553 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
554 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000555}
556
Daniel Dunbara14d2252009-07-26 03:31:47 +0000557StringRef Triple::getOSName() const {
558 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
559 Tmp = Tmp.split('-').second; // Strip second component
560 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000561}
562
Daniel Dunbara14d2252009-07-26 03:31:47 +0000563StringRef Triple::getEnvironmentName() const {
564 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
565 Tmp = Tmp.split('-').second; // Strip second component
566 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000567}
568
Daniel Dunbara14d2252009-07-26 03:31:47 +0000569StringRef Triple::getOSAndEnvironmentName() const {
570 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
571 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000572}
573
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000574static unsigned EatNumber(StringRef &Str) {
575 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000576 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000577
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000578 do {
579 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000580 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000581
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000582 // Eat the digit.
583 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000584 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000585
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000586 return Result;
587}
588
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000589void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
590 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000591 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000592
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000593 // Assume that the OS portion of the triple starts with the canonical name.
594 StringRef OSTypeName = getOSTypeName(getOS());
595 if (OSName.startswith(OSTypeName))
596 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000597
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000598 // Any unset version defaults to 0.
599 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000600
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000601 // Parse up to three components.
602 unsigned *Components[3] = { &Major, &Minor, &Micro };
603 for (unsigned i = 0; i != 3; ++i) {
604 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
605 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000606
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000607 // Consume the leading number.
608 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000609
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000610 // Consume the separator, if present.
611 if (OSName.startswith("."))
612 OSName = OSName.substr(1);
613 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000614}
615
Daniel Dunbara14d2252009-07-26 03:31:47 +0000616void Triple::setTriple(const Twine &Str) {
617 Data = Str.str();
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000618 Arch = InvalidArch;
619}
620
621void Triple::setArch(ArchType Kind) {
622 setArchName(getArchTypeName(Kind));
623}
624
625void Triple::setVendor(VendorType Kind) {
626 setVendorName(getVendorTypeName(Kind));
627}
628
629void Triple::setOS(OSType Kind) {
630 setOSName(getOSTypeName(Kind));
631}
632
Duncan Sands5754a452010-09-16 08:25:48 +0000633void Triple::setEnvironment(EnvironmentType Kind) {
634 setEnvironmentName(getEnvironmentTypeName(Kind));
635}
636
Daniel Dunbar2928c832009-11-06 10:58:06 +0000637void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000638 // Work around a miscompilation bug for Twines in gcc 4.0.3.
639 SmallString<64> Triple;
640 Triple += Str;
641 Triple += "-";
642 Triple += getVendorName();
643 Triple += "-";
644 Triple += getOSAndEnvironmentName();
645 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000646}
647
Daniel Dunbar2928c832009-11-06 10:58:06 +0000648void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000649 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
650}
651
Daniel Dunbar2928c832009-11-06 10:58:06 +0000652void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000653 if (hasEnvironment())
654 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
655 "-" + getEnvironmentName());
656 else
657 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
658}
659
Daniel Dunbar2928c832009-11-06 10:58:06 +0000660void Triple::setEnvironmentName(StringRef Str) {
661 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000662 "-" + Str);
663}
664
Daniel Dunbar2928c832009-11-06 10:58:06 +0000665void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000666 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
667}