blob: 94333a3a9924d45825c197fc8017c7c071416b45 [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";
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
David Blaikie4d6ccb52012-01-20 21:51:11 +0000118 llvm_unreachable("Invalid OSType");
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000119}
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
David Blaikie4d6ccb52012-01-20 21:51:11 +0000132 llvm_unreachable("Invalid EnvironmentType!");
Duncan Sands5754a452010-09-16 08:25:48 +0000133}
134
Daniel Dunbar2928c832009-11-06 10:58:06 +0000135Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
Chandler Carruth06accda2012-02-12 09:27:38 +0000136 return StringSwitch<Triple::ArchType>(Name)
137 .Case("arm", arm)
138 .Case("cellspu", cellspu)
139 .Case("mips", mips)
140 .Case("mipsel", mipsel)
141 .Case("mips64", mips64)
142 .Case("mips64el", mips64el)
143 .Case("msp430", msp430)
144 .Case("ppc64", ppc64)
145 .Case("ppc32", ppc)
146 .Case("ppc", ppc)
147 .Case("mblaze", mblaze)
148 .Case("hexagon", hexagon)
149 .Case("sparc", sparc)
150 .Case("sparcv9", sparcv9)
151 .Case("tce", tce)
152 .Case("thumb", thumb)
153 .Case("x86", x86)
154 .Case("x86-64", x86_64)
155 .Case("xcore", xcore)
156 .Case("ptx32", ptx32)
157 .Case("ptx64", ptx64)
158 .Case("le32", le32)
159 .Case("amdil", amdil)
160 .Default(UnknownArch);
Daniel Dunbar3c2d4bf2009-08-03 04:03:51 +0000161}
162
Daniel Dunbar2928c832009-11-06 10:58:06 +0000163Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000164 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
165 // archs which Darwin doesn't use.
166
167 // The matching this routine does is fairly pointless, since it is neither the
168 // complete architecture list, nor a reasonable subset. The problem is that
169 // historically the driver driver accepts this and also ties its -march=
170 // handling to the architecture name, so we need to be careful before removing
171 // support for it.
172
Daniel Dunbared687882009-09-09 23:01:25 +0000173 // This code must be kept in sync with Clang's Darwin specific argument
174 // translation.
175
Chandler Carruth06accda2012-02-12 09:27:38 +0000176 return StringSwitch<ArchType>(Str)
177 .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", Triple::ppc)
178 .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", Triple::ppc)
179 .Case("ppc64", Triple::ppc64)
180 .Cases("i386", "i486", "i486SX", "i586", "i686", Triple::x86)
181 .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
182 Triple::x86)
183 .Case("x86_64", Triple::x86_64)
184 // This is derived from the driver driver.
185 .Cases("arm", "armv4t", "armv5", "armv6", Triple::arm)
186 .Cases("armv7", "armv7f", "armv7k", "armv7s", "xscale", Triple::arm)
187 .Case("ptx32", Triple::ptx32)
188 .Case("ptx64", Triple::ptx64)
189 .Case("amdil", Triple::amdil)
190 .Default(Triple::UnknownArch);
Daniel Dunbarbaf9b562009-09-08 23:32:51 +0000191}
192
Duncan Sandsbbdca3f2010-03-24 09:05:14 +0000193// Returns architecture name that is understood by the target assembler.
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000194const char *Triple::getArchNameForAssembler() {
Daniel Dunbare1fe09f2011-04-19 21:12:05 +0000195 if (!isOSDarwin() && getVendor() != Triple::Apple)
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000196 return NULL;
197
Chandler Carruth06accda2012-02-12 09:27:38 +0000198 return StringSwitch<const char*>(getArchName())
199 .Case("i386", "i386")
200 .Case("x86_64", "x86_64")
201 .Case("powerpc", "ppc")
202 .Case("powerpc64", "ppc64")
203 .Cases("mblaze", "microblaze", "mblaze")
204 .Case("arm", "arm")
205 .Cases("armv4t", "thumbv4t", "armv4t")
206 .Cases("armv5", "armv5e", "thumbv5", "thumbv5e", "armv5")
207 .Cases("armv6", "thumbv6", "armv6")
208 .Cases("armv7", "thumbv7", "armv7")
209 .Case("ptx32", "ptx32")
210 .Case("ptx64", "ptx64")
211 .Case("le32", "le32")
212 .Case("amdil", "amdil")
213 .Default(NULL);
Viktor Kutuzov51cdac02009-11-17 18:48:27 +0000214}
215
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000216static Triple::ArchType parseArch(StringRef ArchName) {
217 return StringSwitch<Triple::ArchType>(ArchName)
218 .Cases("i386", "i486", "i586", "i686", Triple::x86)
219 // FIXME: Do we need to support these?
220 .Cases("i786", "i886", "i986", Triple::x86)
221 .Cases("amd64", "x86_64", Triple::x86_64)
222 .Case("powerpc", Triple::ppc)
223 .Cases("powerpc64", "ppu", Triple::ppc64)
224 .Case("mblaze", Triple::mblaze)
225 .Cases("arm", "xscale", Triple::arm)
Chandler Carruth0a857712012-02-18 04:34:17 +0000226 // FIXME: It would be good to replace these with explicit names for all the
227 // various suffixes supported.
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000228 .StartsWith("armv", Triple::arm)
229 .Case("thumb", Triple::thumb)
230 .StartsWith("thumbv", Triple::thumb)
231 .Cases("spu", "cellspu", Triple::cellspu)
232 .Case("msp430", Triple::msp430)
233 .Cases("mips", "mipseb", "mipsallegrex", Triple::mips)
Chandler Carruthfdf0dc92012-02-22 11:32:54 +0000234 .Cases("mipsel", "mipsallegrexel", Triple::mipsel)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000235 .Cases("mips64", "mips64eb", Triple::mips64)
236 .Case("mips64el", Triple::mips64el)
237 .Case("hexagon", Triple::hexagon)
238 .Case("sparc", Triple::sparc)
239 .Case("sparcv9", Triple::sparcv9)
240 .Case("tce", Triple::tce)
241 .Case("xcore", Triple::xcore)
242 .Case("ptx32", Triple::ptx32)
243 .Case("ptx64", Triple::ptx64)
244 .Case("le32", Triple::le32)
245 .Case("amdil", Triple::amdil)
246 .Default(Triple::UnknownArch);
Duncan Sands335db222010-08-12 11:31:39 +0000247}
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000248
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000249static Triple::VendorType parseVendor(StringRef VendorName) {
250 return StringSwitch<Triple::VendorType>(VendorName)
251 .Case("apple", Triple::Apple)
252 .Case("pc", Triple::PC)
253 .Case("scei", Triple::SCEI)
254 .Default(Triple::UnknownVendor);
Duncan Sands335db222010-08-12 11:31:39 +0000255}
256
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000257static Triple::OSType parseOS(StringRef OSName) {
258 return StringSwitch<Triple::OSType>(OSName)
259 .StartsWith("auroraux", Triple::AuroraUX)
260 .StartsWith("cygwin", Triple::Cygwin)
261 .StartsWith("darwin", Triple::Darwin)
262 .StartsWith("dragonfly", Triple::DragonFly)
263 .StartsWith("freebsd", Triple::FreeBSD)
264 .StartsWith("ios", Triple::IOS)
265 .StartsWith("kfreebsd", Triple::KFreeBSD)
266 .StartsWith("linux", Triple::Linux)
267 .StartsWith("lv2", Triple::Lv2)
268 .StartsWith("macosx", Triple::MacOSX)
269 .StartsWith("mingw32", Triple::MinGW32)
270 .StartsWith("netbsd", Triple::NetBSD)
271 .StartsWith("openbsd", Triple::OpenBSD)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000272 .StartsWith("solaris", Triple::Solaris)
273 .StartsWith("win32", Triple::Win32)
274 .StartsWith("haiku", Triple::Haiku)
275 .StartsWith("minix", Triple::Minix)
276 .StartsWith("rtems", Triple::RTEMS)
277 .StartsWith("nacl", Triple::NativeClient)
278 .Default(Triple::UnknownOS);
Duncan Sands335db222010-08-12 11:31:39 +0000279}
280
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000281static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
282 return StringSwitch<Triple::EnvironmentType>(EnvironmentName)
283 .StartsWith("eabi", Triple::EABI)
284 .StartsWith("gnueabihf", Triple::GNUEABIHF)
285 .StartsWith("gnueabi", Triple::GNUEABI)
286 .StartsWith("gnu", Triple::GNU)
287 .StartsWith("macho", Triple::MachO)
288 .StartsWith("androideabi", Triple::ANDROIDEABI)
289 .Default(Triple::UnknownEnvironment);
Duncan Sands5754a452010-09-16 08:25:48 +0000290}
291
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000292/// \brief Construct a triple from the string representation provided.
293///
Chandler Carruth0523f412012-02-21 08:31:18 +0000294/// This stores the string representation and parses the various pieces into
295/// enum members.
Chandler Carruth124e51c2012-02-21 03:39:36 +0000296Triple::Triple(const Twine &Str)
297 : Data(Str.str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000298 Arch(parseArch(getArchName())),
299 Vendor(parseVendor(getVendorName())),
300 OS(parseOS(getOSName())),
301 Environment(parseEnvironment(getEnvironmentName())) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000302}
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000303
304/// \brief Construct a triple from string representations of the architecture,
305/// vendor, and OS.
306///
Chandler Carruth0523f412012-02-21 08:31:18 +0000307/// This joins each argument into a canonical string representation and parses
308/// them into enum members. It leaves the environment unknown and omits it from
309/// the string representation.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000310Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
311 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000312 Arch(parseArch(ArchStr.str())),
313 Vendor(parseVendor(VendorStr.str())),
314 OS(parseOS(OSStr.str())),
Chandler Carruth124e51c2012-02-21 03:39:36 +0000315 Environment() {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000316}
317
318/// \brief Construct a triple from string representations of the architecture,
319/// vendor, OS, and environment.
320///
Chandler Carruth0523f412012-02-21 08:31:18 +0000321/// This joins each argument into a canonical string representation and parses
322/// them into enum members.
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000323Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
324 const Twine &EnvironmentStr)
325 : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
326 EnvironmentStr).str()),
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000327 Arch(parseArch(ArchStr.str())),
328 Vendor(parseVendor(VendorStr.str())),
329 OS(parseOS(OSStr.str())),
330 Environment(parseEnvironment(EnvironmentStr.str())) {
Chandler Carruthcceb8f42012-02-20 00:02:47 +0000331}
332
Duncan Sands335db222010-08-12 11:31:39 +0000333std::string Triple::normalize(StringRef Str) {
334 // Parse into components.
335 SmallVector<StringRef, 4> Components;
Chandler Carruthdac3d362012-02-21 09:12:48 +0000336 Str.split(Components, "-");
Duncan Sands335db222010-08-12 11:31:39 +0000337
338 // If the first component corresponds to a known architecture, preferentially
339 // use it for the architecture. If the second component corresponds to a
340 // known vendor, preferentially use it for the vendor, etc. This avoids silly
341 // component movement when a component parses as (eg) both a valid arch and a
342 // valid os.
343 ArchType Arch = UnknownArch;
344 if (Components.size() > 0)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000345 Arch = parseArch(Components[0]);
Duncan Sands335db222010-08-12 11:31:39 +0000346 VendorType Vendor = UnknownVendor;
347 if (Components.size() > 1)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000348 Vendor = parseVendor(Components[1]);
Duncan Sands335db222010-08-12 11:31:39 +0000349 OSType OS = UnknownOS;
350 if (Components.size() > 2)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000351 OS = parseOS(Components[2]);
Duncan Sands5754a452010-09-16 08:25:48 +0000352 EnvironmentType Environment = UnknownEnvironment;
353 if (Components.size() > 3)
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000354 Environment = parseEnvironment(Components[3]);
Duncan Sands335db222010-08-12 11:31:39 +0000355
356 // Note which components are already in their final position. These will not
357 // be moved.
Duncan Sands5754a452010-09-16 08:25:48 +0000358 bool Found[4];
Duncan Sands335db222010-08-12 11:31:39 +0000359 Found[0] = Arch != UnknownArch;
360 Found[1] = Vendor != UnknownVendor;
361 Found[2] = OS != UnknownOS;
Duncan Sands5754a452010-09-16 08:25:48 +0000362 Found[3] = Environment != UnknownEnvironment;
Duncan Sands335db222010-08-12 11:31:39 +0000363
364 // If they are not there already, permute the components into their canonical
365 // positions by seeing if they parse as a valid architecture, and if so moving
366 // the component to the architecture position etc.
Duncan Sands5754a452010-09-16 08:25:48 +0000367 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
Duncan Sands335db222010-08-12 11:31:39 +0000368 if (Found[Pos])
369 continue; // Already in the canonical position.
370
371 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
372 // Do not reparse any components that already matched.
Duncan Sands5754a452010-09-16 08:25:48 +0000373 if (Idx < array_lengthof(Found) && Found[Idx])
Duncan Sands335db222010-08-12 11:31:39 +0000374 continue;
375
376 // Does this component parse as valid for the target position?
377 bool Valid = false;
378 StringRef Comp = Components[Idx];
379 switch (Pos) {
Craig Topper85814382012-02-07 05:05:23 +0000380 default: llvm_unreachable("unexpected component type!");
Duncan Sands335db222010-08-12 11:31:39 +0000381 case 0:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000382 Arch = parseArch(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000383 Valid = Arch != UnknownArch;
384 break;
385 case 1:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000386 Vendor = parseVendor(Comp);
Duncan Sands335db222010-08-12 11:31:39 +0000387 Valid = Vendor != UnknownVendor;
388 break;
389 case 2:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000390 OS = parseOS(Comp);
Duncan Sandsae200c62011-02-02 10:08:38 +0000391 Valid = OS != UnknownOS;
Duncan Sands335db222010-08-12 11:31:39 +0000392 break;
Duncan Sands5754a452010-09-16 08:25:48 +0000393 case 3:
Chandler Carruth4fbf6582012-02-21 08:53:32 +0000394 Environment = parseEnvironment(Comp);
Duncan Sands5754a452010-09-16 08:25:48 +0000395 Valid = Environment != UnknownEnvironment;
396 break;
Duncan Sands335db222010-08-12 11:31:39 +0000397 }
398 if (!Valid)
399 continue; // Nope, try the next component.
400
401 // Move the component to the target position, pushing any non-fixed
402 // components that are in the way to the right. This tends to give
403 // good results in the common cases of a forgotten vendor component
404 // or a wrongly positioned environment.
405 if (Pos < Idx) {
406 // Insert left, pushing the existing components to the right. For
407 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
408 StringRef CurrentComponent(""); // The empty component.
409 // Replace the component we are moving with an empty component.
410 std::swap(CurrentComponent, Components[Idx]);
411 // Insert the component being moved at Pos, displacing any existing
412 // components to the right.
413 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
414 // Skip over any fixed components.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000415 while (i < array_lengthof(Found) && Found[i])
416 ++i;
Duncan Sands335db222010-08-12 11:31:39 +0000417 // Place the component at the new position, getting the component
418 // that was at this position - it will be moved right.
419 std::swap(CurrentComponent, Components[i]);
420 }
421 } else if (Pos > Idx) {
422 // Push right by inserting empty components until the component at Idx
423 // reaches the target position Pos. For example, pc-a -> -pc-a when
424 // moving pc to the second position.
425 do {
426 // Insert one empty component at Idx.
427 StringRef CurrentComponent(""); // The empty component.
Duncan Sandsae200c62011-02-02 10:08:38 +0000428 for (unsigned i = Idx; i < Components.size();) {
Duncan Sands335db222010-08-12 11:31:39 +0000429 // Place the component at the new position, getting the component
430 // that was at this position - it will be moved right.
431 std::swap(CurrentComponent, Components[i]);
432 // If it was placed on top of an empty component then we are done.
433 if (CurrentComponent.empty())
434 break;
Duncan Sandsae200c62011-02-02 10:08:38 +0000435 // Advance to the next component, skipping any fixed components.
Anders Carlsson15ec6952011-02-05 18:19:35 +0000436 while (++i < array_lengthof(Found) && Found[i])
437 ;
Duncan Sands335db222010-08-12 11:31:39 +0000438 }
439 // The last component was pushed off the end - append it.
440 if (!CurrentComponent.empty())
441 Components.push_back(CurrentComponent);
442
443 // Advance Idx to the component's new position.
Chandler Carruthc5f18d32012-02-21 09:29:14 +0000444 while (++Idx < array_lengthof(Found) && Found[Idx])
445 ;
Duncan Sands335db222010-08-12 11:31:39 +0000446 } while (Idx < Pos); // Add more until the final position is reached.
447 }
448 assert(Pos < Components.size() && Components[Pos] == Comp &&
449 "Component moved wrong!");
450 Found[Pos] = true;
451 break;
452 }
453 }
454
455 // Special case logic goes here. At this point Arch, Vendor and OS have the
456 // correct values for the computed components.
457
458 // Stick the corrected components back together to form the normalized string.
459 std::string Normalized;
460 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
461 if (i) Normalized += '-';
462 Normalized += Components[i];
463 }
464 return Normalized;
465}
466
Daniel Dunbara14d2252009-07-26 03:31:47 +0000467StringRef Triple::getArchName() const {
468 return StringRef(Data).split('-').first; // Isolate first component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000469}
470
Daniel Dunbara14d2252009-07-26 03:31:47 +0000471StringRef Triple::getVendorName() const {
472 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
473 return Tmp.split('-').first; // Isolate second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000474}
475
Daniel Dunbara14d2252009-07-26 03:31:47 +0000476StringRef Triple::getOSName() const {
477 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
478 Tmp = Tmp.split('-').second; // Strip second component
479 return Tmp.split('-').first; // Isolate third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000480}
481
Daniel Dunbara14d2252009-07-26 03:31:47 +0000482StringRef Triple::getEnvironmentName() const {
483 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
484 Tmp = Tmp.split('-').second; // Strip second component
485 return Tmp.split('-').second; // Strip third component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000486}
487
Daniel Dunbara14d2252009-07-26 03:31:47 +0000488StringRef Triple::getOSAndEnvironmentName() const {
489 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
490 return Tmp.split('-').second; // Strip second component
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000491}
492
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000493static unsigned EatNumber(StringRef &Str) {
494 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000495 unsigned Result = 0;
Jim Grosbache509aa92010-12-17 02:10:59 +0000496
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000497 do {
498 // Consume the leading digit.
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000499 Result = Result*10 + (Str[0] - '0');
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000500
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000501 // Eat the digit.
502 Str = Str.substr(1);
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000503 } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9');
Jim Grosbache509aa92010-12-17 02:10:59 +0000504
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000505 return Result;
506}
507
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000508void Triple::getOSVersion(unsigned &Major, unsigned &Minor,
509 unsigned &Micro) const {
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000510 StringRef OSName = getOSName();
Jim Grosbache509aa92010-12-17 02:10:59 +0000511
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000512 // Assume that the OS portion of the triple starts with the canonical name.
513 StringRef OSTypeName = getOSTypeName(getOS());
514 if (OSName.startswith(OSTypeName))
515 OSName = OSName.substr(OSTypeName.size());
Jim Grosbache509aa92010-12-17 02:10:59 +0000516
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000517 // Any unset version defaults to 0.
518 Major = Minor = Micro = 0;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000519
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000520 // Parse up to three components.
521 unsigned *Components[3] = { &Major, &Minor, &Micro };
522 for (unsigned i = 0; i != 3; ++i) {
523 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
524 break;
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000525
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000526 // Consume the leading number.
527 *Components[i] = EatNumber(OSName);
Jim Grosbache509aa92010-12-17 02:10:59 +0000528
Daniel Dunbar087d6a52011-04-19 20:24:34 +0000529 // Consume the separator, if present.
530 if (OSName.startswith("."))
531 OSName = OSName.substr(1);
532 }
Chris Lattnerdfc17f72009-08-12 06:19:40 +0000533}
534
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000535bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
536 unsigned &Micro) const {
537 getOSVersion(Major, Minor, Micro);
538
539 switch (getOS()) {
Craig Topper85814382012-02-07 05:05:23 +0000540 default: llvm_unreachable("unexpected OS for Darwin triple");
Bob Wilsonbda59fd2012-01-31 22:32:29 +0000541 case Darwin:
542 // Default to darwin8, i.e., MacOSX 10.4.
543 if (Major == 0)
544 Major = 8;
545 // Darwin version numbers are skewed from OS X versions.
546 if (Major < 4)
547 return false;
548 Micro = 0;
549 Minor = Major - 4;
550 Major = 10;
551 break;
552 case MacOSX:
553 // Default to 10.4.
554 if (Major == 0) {
555 Major = 10;
556 Minor = 4;
557 }
558 if (Major != 10)
559 return false;
560 break;
561 case IOS:
562 // Ignore the version from the triple. This is only handled because the
563 // the clang driver combines OS X and IOS support into a common Darwin
564 // toolchain that wants to know the OS X version number even when targeting
565 // IOS.
566 Major = 10;
567 Minor = 4;
568 Micro = 0;
569 break;
570 }
571 return true;
572}
573
Daniel Dunbara14d2252009-07-26 03:31:47 +0000574void Triple::setTriple(const Twine &Str) {
Chandler Carruth124e51c2012-02-21 03:39:36 +0000575 *this = Triple(Str);
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000576}
577
578void Triple::setArch(ArchType Kind) {
579 setArchName(getArchTypeName(Kind));
580}
581
582void Triple::setVendor(VendorType Kind) {
583 setVendorName(getVendorTypeName(Kind));
584}
585
586void Triple::setOS(OSType Kind) {
587 setOSName(getOSTypeName(Kind));
588}
589
Duncan Sands5754a452010-09-16 08:25:48 +0000590void Triple::setEnvironment(EnvironmentType Kind) {
591 setEnvironmentName(getEnvironmentTypeName(Kind));
592}
593
Daniel Dunbar2928c832009-11-06 10:58:06 +0000594void Triple::setArchName(StringRef Str) {
Jeffrey Yasskin0b228732009-10-06 21:45:26 +0000595 // Work around a miscompilation bug for Twines in gcc 4.0.3.
596 SmallString<64> Triple;
597 Triple += Str;
598 Triple += "-";
599 Triple += getVendorName();
600 Triple += "-";
601 Triple += getOSAndEnvironmentName();
602 setTriple(Triple.str());
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000603}
604
Daniel Dunbar2928c832009-11-06 10:58:06 +0000605void Triple::setVendorName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000606 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
607}
608
Daniel Dunbar2928c832009-11-06 10:58:06 +0000609void Triple::setOSName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000610 if (hasEnvironment())
611 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
612 "-" + getEnvironmentName());
613 else
614 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
615}
616
Daniel Dunbar2928c832009-11-06 10:58:06 +0000617void Triple::setEnvironmentName(StringRef Str) {
618 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000619 "-" + Str);
620}
621
Daniel Dunbar2928c832009-11-06 10:58:06 +0000622void Triple::setOSAndEnvironmentName(StringRef Str) {
Daniel Dunbar23e97b02009-04-01 21:53:23 +0000623 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
624}
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000625
626static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
627 switch (Arch) {
628 case llvm::Triple::UnknownArch:
Chandler Carruth6f72ac42012-01-31 04:52:32 +0000629 return 0;
630
631 case llvm::Triple::msp430:
632 return 16;
633
634 case llvm::Triple::amdil:
635 case llvm::Triple::arm:
636 case llvm::Triple::cellspu:
637 case llvm::Triple::hexagon:
638 case llvm::Triple::le32:
639 case llvm::Triple::mblaze:
640 case llvm::Triple::mips:
641 case llvm::Triple::mipsel:
642 case llvm::Triple::ppc:
643 case llvm::Triple::ptx32:
644 case llvm::Triple::sparc:
645 case llvm::Triple::tce:
646 case llvm::Triple::thumb:
647 case llvm::Triple::x86:
648 case llvm::Triple::xcore:
649 return 32;
650
651 case llvm::Triple::mips64:
652 case llvm::Triple::mips64el:
653 case llvm::Triple::ppc64:
654 case llvm::Triple::ptx64:
655 case llvm::Triple::sparcv9:
656 case llvm::Triple::x86_64:
657 return 64;
658 }
659 llvm_unreachable("Invalid architecture value");
660}
661
662bool Triple::isArch64Bit() const {
663 return getArchPointerBitWidth(getArch()) == 64;
664}
665
666bool Triple::isArch32Bit() const {
667 return getArchPointerBitWidth(getArch()) == 32;
668}
669
670bool Triple::isArch16Bit() const {
671 return getArchPointerBitWidth(getArch()) == 16;
672}
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000673
674Triple Triple::get32BitArchVariant() const {
675 Triple T(*this);
676 switch (getArch()) {
677 case Triple::UnknownArch:
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000678 case Triple::msp430:
679 T.setArch(UnknownArch);
680 break;
681
682 case Triple::amdil:
683 case Triple::arm:
684 case Triple::cellspu:
685 case Triple::hexagon:
686 case Triple::le32:
687 case Triple::mblaze:
688 case Triple::mips:
689 case Triple::mipsel:
690 case Triple::ppc:
691 case Triple::ptx32:
692 case Triple::sparc:
693 case Triple::tce:
694 case Triple::thumb:
695 case Triple::x86:
696 case Triple::xcore:
697 // Already 32-bit.
698 break;
699
700 case Triple::mips64: T.setArch(Triple::mips); break;
701 case Triple::mips64el: T.setArch(Triple::mipsel); break;
702 case Triple::ppc64: T.setArch(Triple::ppc); break;
703 case Triple::ptx64: T.setArch(Triple::ptx32); break;
704 case Triple::sparcv9: T.setArch(Triple::sparc); break;
705 case Triple::x86_64: T.setArch(Triple::x86); break;
706 }
707 return T;
708}
709
710Triple Triple::get64BitArchVariant() const {
711 Triple T(*this);
712 switch (getArch()) {
Chandler Carruth7d5a2892012-02-06 20:46:33 +0000713 case Triple::UnknownArch:
714 case Triple::amdil:
715 case Triple::arm:
716 case Triple::cellspu:
717 case Triple::hexagon:
718 case Triple::le32:
719 case Triple::mblaze:
720 case Triple::msp430:
721 case Triple::tce:
722 case Triple::thumb:
723 case Triple::xcore:
724 T.setArch(UnknownArch);
725 break;
726
727 case Triple::mips64:
728 case Triple::mips64el:
729 case Triple::ppc64:
730 case Triple::ptx64:
731 case Triple::sparcv9:
732 case Triple::x86_64:
733 // Already 64-bit.
734 break;
735
736 case Triple::mips: T.setArch(Triple::mips64); break;
737 case Triple::mipsel: T.setArch(Triple::mips64el); break;
738 case Triple::ppc: T.setArch(Triple::ppc64); break;
739 case Triple::ptx32: T.setArch(Triple::ptx64); break;
740 case Triple::sparc: T.setArch(Triple::sparcv9); break;
741 case Triple::x86: T.setArch(Triple::x86_64); break;
742 }
743 return T;
744}