blob: 8daedf9ffe2560d1df8f4ca9074c5d8b89efa801 [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";
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000125 case GNUEABIHF: return "gnueabihf";
Renato Golin859f8182011-01-21 18:25:47 +0000126 case GNUEABI: return "gnueabi";
127 case EABI: return "eabi";
Evan Cheng2bffee22011-02-01 01:14:13 +0000128 case MachO: return "macho";
Chandler Carruthfd553c22012-01-10 19:46:00 +0000129 case ANDROIDEABI: return "androideabi";
Duncan Sands5754a452010-09-16 08:25:48 +0000130 }
131
132 return "<invalid>";
133}
134
Daniel Dunbar2928c832009-11-06 10:58:06 +0000135Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000136 if (Name == "arm")
137 return arm;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000138 if (Name == "cellspu")
139 return cellspu;
140 if (Name == "mips")
141 return mips;
142 if (Name == "mipsel")
143 return mipsel;
Akira Hatanaka70303682011-09-20 18:09:37 +0000144 if (Name == "mips64")
145 return mips64;
146 if (Name == "mips64el")
147 return mips64el;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000148 if (Name == "msp430")
149 return msp430;
150 if (Name == "ppc64")
151 return ppc64;
NAKAMURA Takumi12b27722011-07-22 04:02:22 +0000152 if (Name == "ppc32")
153 return ppc;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000154 if (Name == "ppc")
155 return ppc;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000156 if (Name == "mblaze")
157 return mblaze;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000158 if (Name == "hexagon")
159 return hexagon;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000160 if (Name == "sparc")
161 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000162 if (Name == "sparcv9")
163 return sparcv9;
Eli Friedman74db89e2009-08-19 20:46:03 +0000164 if (Name == "tce")
165 return tce;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000166 if (Name == "thumb")
167 return thumb;
168 if (Name == "x86")
169 return x86;
Chris Lattnerb796c4f2009-08-12 06:45:02 +0000170 if (Name == "x86-64")
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000171 return x86_64;
172 if (Name == "xcore")
173 return xcore;
Justin Holewinskie1fee482011-04-20 15:37:17 +0000174 if (Name == "ptx32")
175 return ptx32;
176 if (Name == "ptx64")
177 return ptx64;
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000178 if (Name == "le32")
179 return le32;
Tobias Grosser05d71382011-08-29 15:44:55 +0000180 if (Name == "amdil")
181 return amdil;
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000182
183 return UnknownArch;
184}
185
Daniel Dunbar2928c832009-11-06 10:58:06 +0000186Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000187 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
188 // archs which Darwin doesn't use.
189
190 // The matching this routine does is fairly pointless, since it is neither the
191 // complete architecture list, nor a reasonable subset. The problem is that
192 // historically the driver driver accepts this and also ties its -march=
193 // handling to the architecture name, so we need to be careful before removing
194 // support for it.
195
Daniel Dunbared687882009-09-09 23:01:25 +0000196 // This code must be kept in sync with Clang's Darwin specific argument
197 // translation.
198
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000199 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
200 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
201 Str == "ppc7450" || Str == "ppc970")
202 return Triple::ppc;
203
204 if (Str == "ppc64")
205 return Triple::ppc64;
206
207 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
208 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
209 Str == "pentIIm5" || Str == "pentium4")
210 return Triple::x86;
211
212 if (Str == "x86_64")
213 return Triple::x86_64;
214
215 // This is derived from the driver driver.
216 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
Sean Callananefd79192011-07-30 01:29:54 +0000217 Str == "armv6" || Str == "armv7" || Str == "armv7f" || Str == "armv7k" ||
218 Str == "armv7s")
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000219 return Triple::arm;
220
Justin Holewinskie1fee482011-04-20 15:37:17 +0000221 if (Str == "ptx32")
222 return Triple::ptx32;
223 if (Str == "ptx64")
224 return Triple::ptx64;
Tobias Grosser05d71382011-08-29 15:44:55 +0000225 if (Str == "amdil")
226 return Triple::amdil;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000227
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000228 return Triple::UnknownArch;
229}
230
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000231// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000232const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000233 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000234 return NULL;
235
236 StringRef Str = getArchName();
237 if (Str == "i386")
238 return "i386";
239 if (Str == "x86_64")
240 return "x86_64";
241 if (Str == "powerpc")
242 return "ppc";
243 if (Str == "powerpc64")
244 return "ppc64";
Wesley Pecka70f28c2010-02-23 19:15:24 +0000245 if (Str == "mblaze" || Str == "microblaze")
246 return "mblaze";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000247 if (Str == "arm")
248 return "arm";
249 if (Str == "armv4t" || Str == "thumbv4t")
250 return "armv4t";
Jim Grosbache509aa92010-12-17 02:10:59 +0000251 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5"
252 || Str == "thumbv5e")
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000253 return "armv5";
254 if (Str == "armv6" || Str == "thumbv6")
255 return "armv6";
256 if (Str == "armv7" || Str == "thumbv7")
257 return "armv7";
Justin Holewinskie1fee482011-04-20 15:37:17 +0000258 if (Str == "ptx32")
259 return "ptx32";
260 if (Str == "ptx64")
261 return "ptx64";
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000262 if (Str == "le32")
263 return "le32";
Tobias Grosser05d71382011-08-29 15:44:55 +0000264 if (Str == "amdil")
265 return "amdil";
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000266 return NULL;
267}
268
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000269//
270
Duncan Sands335db222010-08-12 11:31:39 +0000271Triple::ArchType Triple::ParseArch(StringRef ArchName) {
Jim Grosbache509aa92010-12-17 02:10:59 +0000272 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
273 ArchName[2] == '8' && ArchName[3] == '6' &&
Daniel Dunbar6337f152009-07-26 04:23:03 +0000274 ArchName[1] - '3' < 6) // i[3-9]86
Duncan Sands335db222010-08-12 11:31:39 +0000275 return x86;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000276 else if (ArchName == "amd64" || ArchName == "x86_64")
Duncan Sands335db222010-08-12 11:31:39 +0000277 return x86_64;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000278 else if (ArchName == "powerpc")
Duncan Sands335db222010-08-12 11:31:39 +0000279 return ppc;
Edward O'Callaghancc9fa812009-11-19 11:59:00 +0000280 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
Duncan Sands335db222010-08-12 11:31:39 +0000281 return ppc64;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000282 else if (ArchName == "mblaze")
Duncan Sands335db222010-08-12 11:31:39 +0000283 return mblaze;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000284 else if (ArchName == "arm" ||
Daniel Dunbar24473892009-08-18 04:51:26 +0000285 ArchName.startswith("armv") ||
286 ArchName == "xscale")
Duncan Sands335db222010-08-12 11:31:39 +0000287 return arm;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000288 else if (ArchName == "thumb" ||
289 ArchName.startswith("thumbv"))
Duncan Sands335db222010-08-12 11:31:39 +0000290 return thumb;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000291 else if (ArchName == "spu" || ArchName == "cellspu")
Duncan Sands335db222010-08-12 11:31:39 +0000292 return cellspu;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000293 else if (ArchName == "msp430")
Duncan Sands335db222010-08-12 11:31:39 +0000294 return msp430;
Joerg Sonnenbergerf86b76e2011-07-07 16:53:52 +0000295 else if (ArchName == "mips" || ArchName == "mipseb" ||
296 ArchName == "mipsallegrex")
Duncan Sands335db222010-08-12 11:31:39 +0000297 return mips;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000298 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
299 ArchName == "psp")
Duncan Sands335db222010-08-12 11:31:39 +0000300 return mipsel;
Akira Hatanaka70303682011-09-20 18:09:37 +0000301 else if (ArchName == "mips64" || ArchName == "mips64eb")
302 return mips64;
303 else if (ArchName == "mips64el")
304 return mips64el;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000305 else if (ArchName == "hexagon")
306 return hexagon;
Daniel Dunbar6337f152009-07-26 04:23:03 +0000307 else if (ArchName == "sparc")
Duncan Sands335db222010-08-12 11:31:39 +0000308 return sparc;
Chris Lattner87c06d62010-02-04 06:34:01 +0000309 else if (ArchName == "sparcv9")
Duncan Sands335db222010-08-12 11:31:39 +0000310 return sparcv9;
Eli Friedman74db89e2009-08-19 20:46:03 +0000311 else if (ArchName == "tce")
Duncan Sands335db222010-08-12 11:31:39 +0000312 return tce;
Richard Osborne768f1dd2009-08-31 21:51:36 +0000313 else if (ArchName == "xcore")
Duncan Sands335db222010-08-12 11:31:39 +0000314 return xcore;
Justin Holewinskie1fee482011-04-20 15:37:17 +0000315 else if (ArchName == "ptx32")
316 return ptx32;
317 else if (ArchName == "ptx64")
318 return ptx64;
Ivan Krasin38fb2db2011-08-23 16:59:00 +0000319 else if (ArchName == "le32")
320 return le32;
Tobias Grosser05d71382011-08-29 15:44:55 +0000321 else if (ArchName == "amdil")
322 return amdil;
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000323 else
Duncan Sands335db222010-08-12 11:31:39 +0000324 return UnknownArch;
325}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000326
Duncan Sands335db222010-08-12 11:31:39 +0000327Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
328 if (VendorName == "apple")
329 return Apple;
330 else if (VendorName == "pc")
331 return PC;
John Thompson6046cff2011-03-15 21:51:56 +0000332 else if (VendorName == "scei")
333 return SCEI;
Duncan Sands335db222010-08-12 11:31:39 +0000334 else
335 return UnknownVendor;
336}
337
338Triple::OSType Triple::ParseOS(StringRef OSName) {
339 if (OSName.startswith("auroraux"))
340 return AuroraUX;
341 else if (OSName.startswith("cygwin"))
342 return Cygwin;
343 else if (OSName.startswith("darwin"))
344 return Darwin;
345 else if (OSName.startswith("dragonfly"))
346 return DragonFly;
347 else if (OSName.startswith("freebsd"))
348 return FreeBSD;
Daniel Dunbar0dde4c02011-04-19 20:19:27 +0000349 else if (OSName.startswith("ios"))
350 return IOS;
Duncan Sands652b48b2011-07-26 15:30:04 +0000351 else if (OSName.startswith("kfreebsd"))
352 return KFreeBSD;
Duncan Sands335db222010-08-12 11:31:39 +0000353 else if (OSName.startswith("linux"))
354 return Linux;
355 else if (OSName.startswith("lv2"))
356 return Lv2;
Daniel Dunbar1af39472011-04-19 23:34:12 +0000357 else if (OSName.startswith("macosx"))
358 return MacOSX;
Duncan Sands335db222010-08-12 11:31:39 +0000359 else if (OSName.startswith("mingw32"))
360 return MinGW32;
Duncan Sands335db222010-08-12 11:31:39 +0000361 else if (OSName.startswith("netbsd"))
362 return NetBSD;
363 else if (OSName.startswith("openbsd"))
364 return OpenBSD;
365 else if (OSName.startswith("psp"))
366 return Psp;
367 else if (OSName.startswith("solaris"))
368 return Solaris;
369 else if (OSName.startswith("win32"))
370 return Win32;
371 else if (OSName.startswith("haiku"))
372 return Haiku;
373 else if (OSName.startswith("minix"))
374 return Minix;
Eli Friedmanb0e77a22011-07-06 20:56:26 +0000375 else if (OSName.startswith("rtems"))
376 return RTEMS;
Ivan Krasind8834532011-08-22 23:08:53 +0000377 else if (OSName.startswith("nacl"))
378 return NativeClient;
Duncan Sands335db222010-08-12 11:31:39 +0000379 else
380 return UnknownOS;
381}
382
Duncan Sands5754a452010-09-16 08:25:48 +0000383Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
Renato Golin859f8182011-01-21 18:25:47 +0000384 if (EnvironmentName.startswith("eabi"))
385 return EABI;
Rafael Espindola8887a0f2012-01-18 23:35:29 +0000386 else if (EnvironmentName.startswith("gnueabihf"))
387 return GNUEABIHF;
Renato Golin859f8182011-01-21 18:25:47 +0000388 else if (EnvironmentName.startswith("gnueabi"))
389 return GNUEABI;
390 else if (EnvironmentName.startswith("gnu"))
391 return GNU;
Evan Cheng2bffee22011-02-01 01:14:13 +0000392 else if (EnvironmentName.startswith("macho"))
393 return MachO;
Chandler Carruthfd553c22012-01-10 19:46:00 +0000394 else if (EnvironmentName.startswith("androideabi"))
395 return ANDROIDEABI;
Renato Golin859f8182011-01-21 18:25:47 +0000396 else
397 return UnknownEnvironment;
Duncan Sands5754a452010-09-16 08:25:48 +0000398}
399
Duncan Sands335db222010-08-12 11:31:39 +0000400void Triple::Parse() const {
401 assert(!isInitialized() && "Invalid parse call.");
402
403 Arch = ParseArch(getArchName());
404 Vendor = ParseVendor(getVendorName());
405 OS = ParseOS(getOSName());
Duncan Sandsae200c62011-02-02 10:08:38 +0000406 Environment = ParseEnvironment(getEnvironmentName());
Daniel Dunbar651aa682009-08-18 19:26:55 +0000407
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000408 assert(isInitialized() && "Failed to initialize!");
409}
410
Duncan Sands335db222010-08-12 11:31:39 +0000411std::string Triple::normalize(StringRef Str) {
412 // Parse into components.
413 SmallVector<StringRef, 4> Components;
414 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
415 Last = Str.find('-', First);
416 Components.push_back(Str.slice(First, Last));
417 }
418
419 // If the first component corresponds to a known architecture, preferentially
420 // use it for the architecture. If the second component corresponds to a
421 // known vendor, preferentially use it for the vendor, etc. This avoids silly
422 // component movement when a component parses as (eg) both a valid arch and a
423 // valid os.
424 ArchType Arch = UnknownArch;
425 if (Components.size() > 0)
426 Arch = ParseArch(Components[0]);
427 VendorType Vendor = UnknownVendor;
428 if (Components.size() > 1)
429 Vendor = ParseVendor(Components[1]);
430 OSType OS = UnknownOS;
431 if (Components.size() > 2)
432 OS = ParseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000433 EnvironmentType Environment = UnknownEnvironment;
434 if (Components.size() > 3)
435 Environment = ParseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000436
437 // Note which components are already in their final position. These will not
438 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000439 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000440 Found[0] = Arch != UnknownArch;
441 Found[1] = Vendor != UnknownVendor;
442 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000443 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000444
445 // If they are not there already, permute the components into their canonical
446 // positions by seeing if they parse as a valid architecture, and if so moving
447 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000448 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000449 if (Found[Pos])
450 continue; // Already in the canonical position.
451
452 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
453 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000454 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000455 continue;
456
457 // Does this component parse as valid for the target position?
458 bool Valid = false;
459 StringRef Comp = Components[Idx];
460 switch (Pos) {
461 default:
462 assert(false && "unexpected component type!");
463 case 0:
464 Arch = ParseArch(Comp);
465 Valid = Arch != UnknownArch;
466 break;
467 case 1:
468 Vendor = ParseVendor(Comp);
469 Valid = Vendor != UnknownVendor;
470 break;
471 case 2:
472 OS = ParseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000473 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000474 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000475 case 3:
476 Environment = ParseEnvironment(Comp);
477 Valid = Environment != UnknownEnvironment;
478 break;
Duncan Sands335db222010-08-12 11:31:39 +0000479 }
480 if (!Valid)
481 continue; // Nope, try the next component.
482
483 // Move the component to the target position, pushing any non-fixed
484 // components that are in the way to the right. This tends to give
485 // good results in the common cases of a forgotten vendor component
486 // or a wrongly positioned environment.
487 if (Pos < Idx) {
488 // Insert left, pushing the existing components to the right. For
489 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
490 StringRef CurrentComponent(""); // The empty component.
491 // Replace the component we are moving with an empty component.
492 std::swap(CurrentComponent, Components[Idx]);
493 // Insert the component being moved at Pos, displacing any existing
494 // components to the right.
495 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
496 // Skip over any fixed components.
Duncan Sands5754a452010-09-16 08:25:48 +0000497 while (i < array_lengthof(Found) && Found[i]) ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000498 // Place the component at the new position, getting the component
499 // that was at this position - it will be moved right.
500 std::swap(CurrentComponent, Components[i]);
501 }
502 } else if (Pos > Idx) {
503 // Push right by inserting empty components until the component at Idx
504 // reaches the target position Pos. For example, pc-a -> -pc-a when
505 // moving pc to the second position.
506 do {
507 // Insert one empty component at Idx.
508 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000509 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000510 // Place the component at the new position, getting the component
511 // that was at this position - it will be moved right.
512 std::swap(CurrentComponent, Components[i]);
513 // If it was placed on top of an empty component then we are done.
514 if (CurrentComponent.empty())
515 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000516 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000517 while (++i < array_lengthof(Found) && Found[i])
518 ;
Duncan Sands335db222010-08-12 11:31:39 +0000519 }
520 // The last component was pushed off the end - append it.
521 if (!CurrentComponent.empty())
522 Components.push_back(CurrentComponent);
523
524 // Advance Idx to the component's new position.
Duncan Sands5754a452010-09-16 08:25:48 +0000525 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
Duncan Sands335db222010-08-12 11:31:39 +0000526 } while (Idx < Pos); // Add more until the final position is reached.
527 }
528 assert(Pos < Components.size() && Components[Pos] == Comp &&
529 "Component moved wrong!");
530 Found[Pos] = true;
531 break;
532 }
533 }
534
535 // Special case logic goes here. At this point Arch, Vendor and OS have the
536 // correct values for the computed components.
537
538 // Stick the corrected components back together to form the normalized string.
539 std::string Normalized;
540 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
541 if (i) Normalized += '-';
542 Normalized += Components[i];
543 }
544 return Normalized;
545}
546
Daniel Dunbara14d2252009-07-26 03:31:47 +0000547StringRef Triple::getArchName() const {
548 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000549}
550
Daniel Dunbara14d2252009-07-26 03:31:47 +0000551StringRef Triple::getVendorName() const {
552 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
553 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000554}
555
Daniel Dunbara14d2252009-07-26 03:31:47 +0000556StringRef Triple::getOSName() const {
557 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
558 Tmp = Tmp.split('-').second; // Strip second component
559 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000560}
561
Daniel Dunbara14d2252009-07-26 03:31:47 +0000562StringRef Triple::getEnvironmentName() const {
563 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
564 Tmp = Tmp.split('-').second; // Strip second component
565 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000566}
567
Daniel Dunbara14d2252009-07-26 03:31:47 +0000568StringRef Triple::getOSAndEnvironmentName() const {
569 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
570 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000571}
572
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000573static unsigned EatNumber(StringRef &Str) {
574 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000575 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000576
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000577 do {
578 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000579 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000580
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000581 // Eat the digit.
582 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000583 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000584
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000585 return Result;
586}
587
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000588void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
589 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000590 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000591
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000592 // Assume that the OS portion of the triple starts with the canonical name.
593 StringRef OSTypeName = getOSTypeName(getOS());
594 if (OSName.startswith(OSTypeName))
595 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000596
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000597 // Any unset version defaults to 0.
598 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000599
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000600 // Parse up to three components.
601 unsigned *Components[3] = { &Major, &Minor, &Micro };
602 for (unsigned i = 0; i != 3; ++i) {
603 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
604 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000605
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000606 // Consume the leading number.
607 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000608
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000609 // Consume the separator, if present.
610 if (OSName.startswith("."))
611 OSName = OSName.substr(1);
612 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000613}
614
Daniel Dunbara14d2252009-07-26 03:31:47 +0000615void Triple::setTriple(const Twine &Str) {
616 Data = Str.str();
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000617 Arch = InvalidArch;
618}
619
620void Triple::setArch(ArchType Kind) {
621 setArchName(getArchTypeName(Kind));
622}
623
624void Triple::setVendor(VendorType Kind) {
625 setVendorName(getVendorTypeName(Kind));
626}
627
628void Triple::setOS(OSType Kind) {
629 setOSName(getOSTypeName(Kind));
630}
631
Duncan Sands5754a452010-09-16 08:25:48 +0000632void Triple::setEnvironment(EnvironmentType Kind) {
633 setEnvironmentName(getEnvironmentTypeName(Kind));
634}
635
Daniel Dunbar2928c832009-11-06 10:58:06 +0000636void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000637 // Work around a miscompilation bug for Twines in gcc 4.0.3.
638 SmallString<64> Triple;
639 Triple += Str;
640 Triple += "-";
641 Triple += getVendorName();
642 Triple += "-";
643 Triple += getOSAndEnvironmentName();
644 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000645}
646
Daniel Dunbar2928c832009-11-06 10:58:06 +0000647void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000648 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
649}
650
Daniel Dunbar2928c832009-11-06 10:58:06 +0000651void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000652 if (hasEnvironment())
653 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
654 "-" + getEnvironmentName());
655 else
656 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
657}
658
Daniel Dunbar2928c832009-11-06 10:58:06 +0000659void Triple::setEnvironmentName(StringRef Str) {
660 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000661 "-" + Str);
662}
663
Daniel Dunbar2928c832009-11-06 10:58:06 +0000664void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000665 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
666}