blob: ae69c6073145942a9dd46e9a6cf0c804c449df4e [file] [log] [blame]
Daniel Dunbar23e97b02009-04-01 21:53:23 +00001//===--- Triple.cpp - Target triple helper class --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/Triple.h"
Jeffrey Yasskin0b228732009-10-06 21:45:26 +000011#include "llvm/ADT/SmallString.h"
Chandler Carruth06accda2012-02-12 09:27:38 +000012#include "llvm/ADT/StringSwitch.h"
Duncan Sands5754a452010-09-16 08:25:48 +000013#include "llvm/ADT/STLExtras.h"
David Blaikie4d6ccb52012-01-20 21:51:11 +000014#include "llvm/Support/ErrorHandling.h"
Mikhail Glushenkov70748752009-04-02 01:11:37 +000015#include <cstring>
Daniel Dunbar23e97b02009-04-01 21:53:23 +000016using namespace llvm;
17
Daniel Dunbar23e97b02009-04-01 21:53:23 +000018const char *Triple::getArchTypeName(ArchType Kind) {
19 switch (Kind) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +000020 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) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000137 return StringSwitch<Triple::ArchType>(Name)
138 .Case("arm", arm)
139 .Case("cellspu", cellspu)
140 .Case("mips", mips)
141 .Case("mipsel", mipsel)
142 .Case("mips64", mips64)
143 .Case("mips64el", mips64el)
144 .Case("msp430", msp430)
145 .Case("ppc64", ppc64)
146 .Case("ppc32", ppc)
147 .Case("ppc", ppc)
148 .Case("mblaze", mblaze)
149 .Case("hexagon", hexagon)
150 .Case("sparc", sparc)
151 .Case("sparcv9", sparcv9)
152 .Case("tce", tce)
153 .Case("thumb", thumb)
154 .Case("x86", x86)
155 .Case("x86-64", x86_64)
156 .Case("xcore", xcore)
157 .Case("ptx32", ptx32)
158 .Case("ptx64", ptx64)
159 .Case("le32", le32)
160 .Case("amdil", amdil)
161 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000162}
163
Daniel Dunbar2928c832009-11-06 10:58:06 +0000164Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000165 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
166 // archs which Darwin doesn't use.
167
168 // The matching this routine does is fairly pointless, since it is neither the
169 // complete architecture list, nor a reasonable subset. The problem is that
170 // historically the driver driver accepts this and also ties its -march=
171 // handling to the architecture name, so we need to be careful before removing
172 // support for it.
173
Daniel Dunbared687882009-09-09 23:01:25 +0000174 // This code must be kept in sync with Clang's Darwin specific argument
175 // translation.
176
Chandler Carruth06accda2012-02-12 09:27:38 +0000177 return StringSwitch<ArchType>(Str)
178 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
179 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
180 .Case("ppc64", Triple::ppc64)
181 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
182 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
183 Triple::x86)
184 .Case("x86_64", Triple::x86_64)
185 // This is derived from the driver driver.
186 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
187 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
188 .Case("ptx32", Triple::ptx32)
189 .Case("ptx64", Triple::ptx64)
190 .Case("amdil", Triple::amdil)
191 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000192}
193
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000194// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000195const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000196 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000197 return NULL;
198
Chandler Carruth06accda2012-02-12 09:27:38 +0000199 return StringSwitch<const char*>(getArchName())
200 .Case("i386", "i386")
201 .Case("x86_64", "x86_64")
202 .Case("powerpc", "ppc")
203 .Case("powerpc64", "ppc64")
204 .Cases("mblaze", "microblaze", "mblaze")
205 .Case("arm", "arm")
206 .Cases("armv4t", "thumbv4t", "armv4t")
207 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
208 .Cases("armv6", "thumbv6", "armv6")
209 .Cases("armv7", "thumbv7", "armv7")
210 .Case("ptx32", "ptx32")
211 .Case("ptx64", "ptx64")
212 .Case("le32", "le32")
213 .Case("amdil", "amdil")
214 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000215}
216
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000217static Triple::ArchType parseArch(StringRef ArchName) {
218 return StringSwitch<Triple::ArchType>(ArchName)
219 .Cases("i386", "i486", "i586", "i686", Triple::x86)
220 // FIXME: Do we need to support these?
221 .Cases("i786", "i886", "i986", Triple::x86)
222 .Cases("amd64", "x86_64", Triple::x86_64)
223 .Case("powerpc", Triple::ppc)
224 .Cases("powerpc64", "ppu", Triple::ppc64)
225 .Case("mblaze", Triple::mblaze)
226 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000227 // FIXME: It would be good to replace these with explicit names for all the
228 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000229 .StartsWith("armv", Triple::arm)
230 .Case("thumb", Triple::thumb)
231 .StartsWith("thumbv", Triple::thumb)
232 .Cases("spu", "cellspu", Triple::cellspu)
233 .Case("msp430", Triple::msp430)
234 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
235 .Cases("mipsel", "mipsallegrexel", "psp", Triple::mipsel)
236 .Cases("mips64", "mips64eb", Triple::mips64)
237 .Case("mips64el", Triple::mips64el)
238 .Case("hexagon", Triple::hexagon)
239 .Case("sparc", Triple::sparc)
240 .Case("sparcv9", Triple::sparcv9)
241 .Case("tce", Triple::tce)
242 .Case("xcore", Triple::xcore)
243 .Case("ptx32", Triple::ptx32)
244 .Case("ptx64", Triple::ptx64)
245 .Case("le32", Triple::le32)
246 .Case("amdil", Triple::amdil)
247 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000248}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000249
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000250static Triple::VendorType parseVendor(StringRef VendorName) {
251 return StringSwitch<Triple::VendorType>(VendorName)
252 .Case("apple", Triple::Apple)
253 .Case("pc", Triple::PC)
254 .Case("scei", Triple::SCEI)
255 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000256}
257
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000258static Triple::OSType parseOS(StringRef OSName) {
259 return StringSwitch<Triple::OSType>(OSName)
260 .StartsWith("auroraux", Triple::AuroraUX)
261 .StartsWith("cygwin", Triple::Cygwin)
262 .StartsWith("darwin", Triple::Darwin)
263 .StartsWith("dragonfly", Triple::DragonFly)
264 .StartsWith("freebsd", Triple::FreeBSD)
265 .StartsWith("ios", Triple::IOS)
266 .StartsWith("kfreebsd", Triple::KFreeBSD)
267 .StartsWith("linux", Triple::Linux)
268 .StartsWith("lv2", Triple::Lv2)
269 .StartsWith("macosx", Triple::MacOSX)
270 .StartsWith("mingw32", Triple::MinGW32)
271 .StartsWith("netbsd", Triple::NetBSD)
272 .StartsWith("openbsd", Triple::OpenBSD)
273 .StartsWith("psp", Triple::Psp)
274 .StartsWith("solaris", Triple::Solaris)
275 .StartsWith("win32", Triple::Win32)
276 .StartsWith("haiku", Triple::Haiku)
277 .StartsWith("minix", Triple::Minix)
278 .StartsWith("rtems", Triple::RTEMS)
279 .StartsWith("nacl", Triple::NativeClient)
280 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000281}
282
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000283static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
284 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
285 .StartsWith("eabi", Triple::EABI)
286 .StartsWith("gnueabihf", Triple::GNUEABIHF)
287 .StartsWith("gnueabi", Triple::GNUEABI)
288 .StartsWith("gnu", Triple::GNU)
289 .StartsWith("macho", Triple::MachO)
290 .StartsWith("androideabi", Triple::ANDROIDEABI)
291 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000292}
293
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000294/// \brief Construct a triple from the string representation provided.
295///
Chandler Carruth0523f412012-02-21 08:31:18 +0000296/// This stores the string representation and parses the various pieces into
297/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000298Triple::Triple(const Twine &Str)
299 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000300 Arch(parseArch(getArchName())),
301 Vendor(parseVendor(getVendorName())),
302 OS(parseOS(getOSName())),
303 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000304}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000305
306/// \brief Construct a triple from string representations of the architecture,
307/// vendor, and OS.
308///
Chandler Carruth0523f412012-02-21 08:31:18 +0000309/// This joins each argument into a canonical string representation and parses
310/// them into enum members. It leaves the environment unknown and omits it from
311/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000312Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
313 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000314 Arch(parseArch(ArchStr.str())),
315 Vendor(parseVendor(VendorStr.str())),
316 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000317 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000318}
319
320/// \brief Construct a triple from string representations of the architecture,
321/// vendor, OS, and environment.
322///
Chandler Carruth0523f412012-02-21 08:31:18 +0000323/// This joins each argument into a canonical string representation and parses
324/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000325Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
326 const Twine &EnvironmentStr)
327 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
328 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000329 Arch(parseArch(ArchStr.str())),
330 Vendor(parseVendor(VendorStr.str())),
331 OS(parseOS(OSStr.str())),
332 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000333}
334
Duncan Sands335db222010-08-12 11:31:39 +0000335std::string Triple::normalize(StringRef Str) {
336 // Parse into components.
337 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000338 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000339
340 // If the first component corresponds to a known architecture, preferentially
341 // use it for the architecture. If the second component corresponds to a
342 // known vendor, preferentially use it for the vendor, etc. This avoids silly
343 // component movement when a component parses as (eg) both a valid arch and a
344 // valid os.
345 ArchType Arch = UnknownArch;
346 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000347 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000348 VendorType Vendor = UnknownVendor;
349 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000350 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000351 OSType OS = UnknownOS;
352 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000353 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000354 EnvironmentType Environment = UnknownEnvironment;
355 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000356 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000357
358 // Note which components are already in their final position. These will not
359 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000360 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000361 Found[0] = Arch != UnknownArch;
362 Found[1] = Vendor != UnknownVendor;
363 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000364 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000365
366 // If they are not there already, permute the components into their canonical
367 // positions by seeing if they parse as a valid architecture, and if so moving
368 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000369 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000370 if (Found[Pos])
371 continue; // Already in the canonical position.
372
373 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
374 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000375 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000376 continue;
377
378 // Does this component parse as valid for the target position?
379 bool Valid = false;
380 StringRef Comp = Components[Idx];
381 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000382 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000383 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000384 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000385 Valid = Arch != UnknownArch;
386 break;
387 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000388 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000389 Valid = Vendor != UnknownVendor;
390 break;
391 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000392 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000393 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000394 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000395 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000396 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000397 Valid = Environment != UnknownEnvironment;
398 break;
Duncan Sands335db222010-08-12 11:31:39 +0000399 }
400 if (!Valid)
401 continue; // Nope, try the next component.
402
403 // Move the component to the target position, pushing any non-fixed
404 // components that are in the way to the right. This tends to give
405 // good results in the common cases of a forgotten vendor component
406 // or a wrongly positioned environment.
407 if (Pos < Idx) {
408 // Insert left, pushing the existing components to the right. For
409 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
410 StringRef CurrentComponent(""); // The empty component.
411 // Replace the component we are moving with an empty component.
412 std::swap(CurrentComponent, Components[Idx]);
413 // Insert the component being moved at Pos, displacing any existing
414 // components to the right.
415 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
416 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000417 while (i < array_lengthof(Found) && Found[i])
418 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000419 // Place the component at the new position, getting the component
420 // that was at this position - it will be moved right.
421 std::swap(CurrentComponent, Components[i]);
422 }
423 } else if (Pos > Idx) {
424 // Push right by inserting empty components until the component at Idx
425 // reaches the target position Pos. For example, pc-a -> -pc-a when
426 // moving pc to the second position.
427 do {
428 // Insert one empty component at Idx.
429 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000430 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000431 // Place the component at the new position, getting the component
432 // that was at this position - it will be moved right.
433 std::swap(CurrentComponent, Components[i]);
434 // If it was placed on top of an empty component then we are done.
435 if (CurrentComponent.empty())
436 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000437 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000438 while (++i < array_lengthof(Found) && Found[i])
439 ;
Duncan Sands335db222010-08-12 11:31:39 +0000440 }
441 // The last component was pushed off the end - append it.
442 if (!CurrentComponent.empty())
443 Components.push_back(CurrentComponent);
444
445 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000446 while (++Idx < array_lengthof(Found) && Found[Idx])
447 ;
Duncan Sands335db222010-08-12 11:31:39 +0000448 } while (Idx < Pos); // Add more until the final position is reached.
449 }
450 assert(Pos < Components.size() && Components[Pos] == Comp &&
451 "Component moved wrong!");
452 Found[Pos] = true;
453 break;
454 }
455 }
456
457 // Special case logic goes here. At this point Arch, Vendor and OS have the
458 // correct values for the computed components.
459
460 // Stick the corrected components back together to form the normalized string.
461 std::string Normalized;
462 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
463 if (i) Normalized += '-';
464 Normalized += Components[i];
465 }
466 return Normalized;
467}
468
Daniel Dunbara14d2252009-07-26 03:31:47 +0000469StringRef Triple::getArchName() const {
470 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000471}
472
Daniel Dunbara14d2252009-07-26 03:31:47 +0000473StringRef Triple::getVendorName() const {
474 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
475 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000476}
477
Daniel Dunbara14d2252009-07-26 03:31:47 +0000478StringRef Triple::getOSName() const {
479 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
480 Tmp = Tmp.split('-').second; // Strip second component
481 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000482}
483
Daniel Dunbara14d2252009-07-26 03:31:47 +0000484StringRef Triple::getEnvironmentName() const {
485 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
486 Tmp = Tmp.split('-').second; // Strip second component
487 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000488}
489
Daniel Dunbara14d2252009-07-26 03:31:47 +0000490StringRef Triple::getOSAndEnvironmentName() const {
491 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
492 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000493}
494
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000495static unsigned EatNumber(StringRef &Str) {
496 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000497 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000498
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000499 do {
500 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000501 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000502
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000503 // Eat the digit.
504 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000505 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000506
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000507 return Result;
508}
509
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000510void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
511 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000512 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000513
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000514 // Assume that the OS portion of the triple starts with the canonical name.
515 StringRef OSTypeName = getOSTypeName(getOS());
516 if (OSName.startswith(OSTypeName))
517 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000518
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000519 // Any unset version defaults to 0.
520 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000521
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000522 // Parse up to three components.
523 unsigned *Components[3] = { &Major, &Minor, &Micro };
524 for (unsigned i = 0; i != 3; ++i) {
525 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
526 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000527
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000528 // Consume the leading number.
529 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000530
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000531 // Consume the separator, if present.
532 if (OSName.startswith("."))
533 OSName = OSName.substr(1);
534 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000535}
536
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000537bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
538 unsigned &Micro) const {
539 getOSVersion(Major, Minor, Micro);
540
541 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000542 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000543 case Darwin:
544 // Default to darwin8, i.e., MacOSX 10.4.
545 if (Major == 0)
546 Major = 8;
547 // Darwin version numbers are skewed from OS X versions.
548 if (Major < 4)
549 return false;
550 Micro = 0;
551 Minor = Major - 4;
552 Major = 10;
553 break;
554 case MacOSX:
555 // Default to 10.4.
556 if (Major == 0) {
557 Major = 10;
558 Minor = 4;
559 }
560 if (Major != 10)
561 return false;
562 break;
563 case IOS:
564 // Ignore the version from the triple. This is only handled because the
565 // the clang driver combines OS X and IOS support into a common Darwin
566 // toolchain that wants to know the OS X version number even when targeting
567 // IOS.
568 Major = 10;
569 Minor = 4;
570 Micro = 0;
571 break;
572 }
573 return true;
574}
575
Daniel Dunbara14d2252009-07-26 03:31:47 +0000576void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000577 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000578}
579
580void Triple::setArch(ArchType Kind) {
581 setArchName(getArchTypeName(Kind));
582}
583
584void Triple::setVendor(VendorType Kind) {
585 setVendorName(getVendorTypeName(Kind));
586}
587
588void Triple::setOS(OSType Kind) {
589 setOSName(getOSTypeName(Kind));
590}
591
Duncan Sands5754a452010-09-16 08:25:48 +0000592void Triple::setEnvironment(EnvironmentType Kind) {
593 setEnvironmentName(getEnvironmentTypeName(Kind));
594}
595
Daniel Dunbar2928c832009-11-06 10:58:06 +0000596void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000597 // Work around a miscompilation bug for Twines in gcc 4.0.3.
598 SmallString<64> Triple;
599 Triple += Str;
600 Triple += "-";
601 Triple += getVendorName();
602 Triple += "-";
603 Triple += getOSAndEnvironmentName();
604 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000605}
606
Daniel Dunbar2928c832009-11-06 10:58:06 +0000607void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000608 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
609}
610
Daniel Dunbar2928c832009-11-06 10:58:06 +0000611void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000612 if (hasEnvironment())
613 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
614 "-" + getEnvironmentName());
615 else
616 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
617}
618
Daniel Dunbar2928c832009-11-06 10:58:06 +0000619void Triple::setEnvironmentName(StringRef Str) {
620 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000621 "-" + Str);
622}
623
Daniel Dunbar2928c832009-11-06 10:58:06 +0000624void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000625 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
626}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000627
628static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
629 switch (Arch) {
630 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000631 return 0;
632
633 case llvm::Triple::msp430:
634 return 16;
635
636 case llvm::Triple::amdil:
637 case llvm::Triple::arm:
638 case llvm::Triple::cellspu:
639 case llvm::Triple::hexagon:
640 case llvm::Triple::le32:
641 case llvm::Triple::mblaze:
642 case llvm::Triple::mips:
643 case llvm::Triple::mipsel:
644 case llvm::Triple::ppc:
645 case llvm::Triple::ptx32:
646 case llvm::Triple::sparc:
647 case llvm::Triple::tce:
648 case llvm::Triple::thumb:
649 case llvm::Triple::x86:
650 case llvm::Triple::xcore:
651 return 32;
652
653 case llvm::Triple::mips64:
654 case llvm::Triple::mips64el:
655 case llvm::Triple::ppc64:
656 case llvm::Triple::ptx64:
657 case llvm::Triple::sparcv9:
658 case llvm::Triple::x86_64:
659 return 64;
660 }
661 llvm_unreachable("Invalid architecture value");
662}
663
664bool Triple::isArch64Bit() const {
665 return getArchPointerBitWidth(getArch()) == 64;
666}
667
668bool Triple::isArch32Bit() const {
669 return getArchPointerBitWidth(getArch()) == 32;
670}
671
672bool Triple::isArch16Bit() const {
673 return getArchPointerBitWidth(getArch()) == 16;
674}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000675
676Triple Triple::get32BitArchVariant() const {
677 Triple T(*this);
678 switch (getArch()) {
679 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000680 case Triple::msp430:
681 T.setArch(UnknownArch);
682 break;
683
684 case Triple::amdil:
685 case Triple::arm:
686 case Triple::cellspu:
687 case Triple::hexagon:
688 case Triple::le32:
689 case Triple::mblaze:
690 case Triple::mips:
691 case Triple::mipsel:
692 case Triple::ppc:
693 case Triple::ptx32:
694 case Triple::sparc:
695 case Triple::tce:
696 case Triple::thumb:
697 case Triple::x86:
698 case Triple::xcore:
699 // Already 32-bit.
700 break;
701
702 case Triple::mips64: T.setArch(Triple::mips); break;
703 case Triple::mips64el: T.setArch(Triple::mipsel); break;
704 case Triple::ppc64: T.setArch(Triple::ppc); break;
705 case Triple::ptx64: T.setArch(Triple::ptx32); break;
706 case Triple::sparcv9: T.setArch(Triple::sparc); break;
707 case Triple::x86_64: T.setArch(Triple::x86); break;
708 }
709 return T;
710}
711
712Triple Triple::get64BitArchVariant() const {
713 Triple T(*this);
714 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000715 case Triple::UnknownArch:
716 case Triple::amdil:
717 case Triple::arm:
718 case Triple::cellspu:
719 case Triple::hexagon:
720 case Triple::le32:
721 case Triple::mblaze:
722 case Triple::msp430:
723 case Triple::tce:
724 case Triple::thumb:
725 case Triple::xcore:
726 T.setArch(UnknownArch);
727 break;
728
729 case Triple::mips64:
730 case Triple::mips64el:
731 case Triple::ppc64:
732 case Triple::ptx64:
733 case Triple::sparcv9:
734 case Triple::x86_64:
735 // Already 64-bit.
736 break;
737
738 case Triple::mips: T.setArch(Triple::mips64); break;
739 case Triple::mipsel: T.setArch(Triple::mips64el); break;
740 case Triple::ppc: T.setArch(Triple::ppc64); break;
741 case Triple::ptx32: T.setArch(Triple::ptx64); break;
742 case Triple::sparc: T.setArch(Triple::sparcv9); break;
743 case Triple::x86: T.setArch(Triple::x86_64); break;
744 }
745 return T;
746}