Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 1 | //===--- 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 Yasskin | 0b22873 | 2009-10-06 21:45:26 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 06accda | 2012-02-12 09:27:38 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringSwitch.h" |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ErrorHandling.h" |
Mikhail Glushenkov | 7074875 | 2009-04-02 01:11:37 +0000 | [diff] [blame] | 15 | #include <cstring> |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 18 | const char *Triple::getArchTypeName(ArchType Kind) { |
| 19 | switch (Kind) { |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 20 | case UnknownArch: return "unknown"; |
Jim Grosbach | e509aa9 | 2010-12-17 02:10:59 +0000 | [diff] [blame] | 21 | |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 22 | case arm: return "arm"; |
| 23 | case cellspu: return "cellspu"; |
Tony Linthicum | b4b5415 | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 24 | case hexagon: return "hexagon"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 25 | case mips: return "mips"; |
| 26 | case mipsel: return "mipsel"; |
Akira Hatanaka | 7030368 | 2011-09-20 18:09:37 +0000 | [diff] [blame] | 27 | case mips64: return "mips64"; |
| 28 | case mips64el:return "mips64el"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 29 | case msp430: return "msp430"; |
Daniel Dunbar | 8c2f1d7 | 2009-07-26 04:52:45 +0000 | [diff] [blame] | 30 | case ppc64: return "powerpc64"; |
| 31 | case ppc: return "powerpc"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 32 | case sparc: return "sparc"; |
Chris Lattner | 87c06d6 | 2010-02-04 06:34:01 +0000 | [diff] [blame] | 33 | case sparcv9: return "sparcv9"; |
Eli Friedman | 74db89e | 2009-08-19 20:46:03 +0000 | [diff] [blame] | 34 | case tce: return "tce"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 35 | case thumb: return "thumb"; |
| 36 | case x86: return "i386"; |
| 37 | case x86_64: return "x86_64"; |
Daniel Dunbar | 8c2f1d7 | 2009-07-26 04:52:45 +0000 | [diff] [blame] | 38 | case xcore: return "xcore"; |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 39 | case mblaze: return "mblaze"; |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 40 | case ptx32: return "ptx32"; |
| 41 | case ptx64: return "ptx64"; |
Ivan Krasin | 38fb2db | 2011-08-23 16:59:00 +0000 | [diff] [blame] | 42 | case le32: return "le32"; |
Tobias Grosser | 05d7138 | 2011-08-29 15:44:55 +0000 | [diff] [blame] | 43 | case amdil: return "amdil"; |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 44 | } |
| 45 | |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 46 | llvm_unreachable("Invalid ArchType!"); |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Dunbar | 688b55b | 2009-08-24 09:53:06 +0000 | [diff] [blame] | 49 | const char *Triple::getArchTypePrefix(ArchType Kind) { |
| 50 | switch (Kind) { |
| 51 | default: |
| 52 | return 0; |
| 53 | |
Daniel Dunbar | 688b55b | 2009-08-24 09:53:06 +0000 | [diff] [blame] | 54 | case arm: |
| 55 | case thumb: return "arm"; |
| 56 | |
Daniel Dunbar | 688b55b | 2009-08-24 09:53:06 +0000 | [diff] [blame] | 57 | case cellspu: return "spu"; |
| 58 | |
| 59 | case ppc64: |
| 60 | case ppc: return "ppc"; |
| 61 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 62 | case mblaze: return "mblaze"; |
| 63 | |
Tony Linthicum | b4b5415 | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 64 | case hexagon: return "hexagon"; |
| 65 | |
Chris Lattner | 87c06d6 | 2010-02-04 06:34:01 +0000 | [diff] [blame] | 66 | case sparcv9: |
Daniel Dunbar | 688b55b | 2009-08-24 09:53:06 +0000 | [diff] [blame] | 67 | case sparc: return "sparc"; |
| 68 | |
| 69 | case x86: |
| 70 | case x86_64: return "x86"; |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | 688b55b | 2009-08-24 09:53:06 +0000 | [diff] [blame] | 72 | case xcore: return "xcore"; |
Nick Lewycky | f7a3c50 | 2010-09-07 18:14:24 +0000 | [diff] [blame] | 73 | |
Justin Holewinski | e1fee48 | 2011-04-20 15:37:17 +0000 | [diff] [blame] | 74 | case ptx32: return "ptx"; |
| 75 | case ptx64: return "ptx"; |
Ivan Krasin | 38fb2db | 2011-08-23 16:59:00 +0000 | [diff] [blame] | 76 | case le32: return "le32"; |
Tobias Grosser | 05d7138 | 2011-08-29 15:44:55 +0000 | [diff] [blame] | 77 | case amdil: return "amdil"; |
Daniel Dunbar | 688b55b | 2009-08-24 09:53:06 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 81 | const char *Triple::getVendorTypeName(VendorType Kind) { |
| 82 | switch (Kind) { |
| 83 | case UnknownVendor: return "unknown"; |
| 84 | |
| 85 | case Apple: return "apple"; |
Chris Lattner | 56ce0f4 | 2009-08-14 18:48:13 +0000 | [diff] [blame] | 86 | case PC: return "pc"; |
John Thompson | 6046cff | 2011-03-15 21:51:56 +0000 | [diff] [blame] | 87 | case SCEI: return "scei"; |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 88 | } |
| 89 | |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 90 | llvm_unreachable("Invalid VendorType!"); |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | const char *Triple::getOSTypeName(OSType Kind) { |
| 94 | switch (Kind) { |
| 95 | case UnknownOS: return "unknown"; |
| 96 | |
Duncan Sands | 852cd11 | 2009-06-19 14:40:01 +0000 | [diff] [blame] | 97 | case AuroraUX: return "auroraux"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 98 | case Cygwin: return "cygwin"; |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 99 | case Darwin: return "darwin"; |
Daniel Dunbar | 7eaf057 | 2009-05-22 02:24:11 +0000 | [diff] [blame] | 100 | case DragonFly: return "dragonfly"; |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 101 | case FreeBSD: return "freebsd"; |
Daniel Dunbar | 0dde4c0 | 2011-04-19 20:19:27 +0000 | [diff] [blame] | 102 | case IOS: return "ios"; |
Duncan Sands | 652b48b | 2011-07-26 15:30:04 +0000 | [diff] [blame] | 103 | case KFreeBSD: return "kfreebsd"; |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 104 | case Linux: return "linux"; |
Edward O'Callaghan | cc9fa81 | 2009-11-19 11:59:00 +0000 | [diff] [blame] | 105 | case Lv2: return "lv2"; |
Daniel Dunbar | 1af3947 | 2011-04-19 23:34:12 +0000 | [diff] [blame] | 106 | case MacOSX: return "macosx"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 107 | case MinGW32: return "mingw32"; |
Chris Lattner | b8ac841 | 2009-07-13 20:22:23 +0000 | [diff] [blame] | 108 | case NetBSD: return "netbsd"; |
Duncan Sands | cd1267d | 2009-06-29 13:36:13 +0000 | [diff] [blame] | 109 | case OpenBSD: return "openbsd"; |
Edward O'Callaghan | e0fb75d | 2009-11-15 10:18:17 +0000 | [diff] [blame] | 110 | case Psp: return "psp"; |
Daniel Dunbar | fdb0b7b | 2009-08-18 04:43:27 +0000 | [diff] [blame] | 111 | case Solaris: return "solaris"; |
Daniel Dunbar | 6337f15 | 2009-07-26 04:23:03 +0000 | [diff] [blame] | 112 | case Win32: return "win32"; |
Chris Lattner | a43fc34 | 2009-10-16 02:06:30 +0000 | [diff] [blame] | 113 | case Haiku: return "haiku"; |
Chris Lattner | 29269d0 | 2010-07-07 15:52:27 +0000 | [diff] [blame] | 114 | case Minix: return "minix"; |
Douglas Gregor | 6ced1d1 | 2011-07-01 22:41:06 +0000 | [diff] [blame] | 115 | case RTEMS: return "rtems"; |
Ivan Krasin | fb23462 | 2011-08-18 22:54:21 +0000 | [diff] [blame] | 116 | case NativeClient: return "nacl"; |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 119 | llvm_unreachable("Invalid OSType"); |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 122 | const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) { |
| 123 | switch (Kind) { |
| 124 | case UnknownEnvironment: return "unknown"; |
Renato Golin | 859f818 | 2011-01-21 18:25:47 +0000 | [diff] [blame] | 125 | case GNU: return "gnu"; |
Rafael Espindola | 8887a0f | 2012-01-18 23:35:29 +0000 | [diff] [blame] | 126 | case GNUEABIHF: return "gnueabihf"; |
Renato Golin | 859f818 | 2011-01-21 18:25:47 +0000 | [diff] [blame] | 127 | case GNUEABI: return "gnueabi"; |
| 128 | case EABI: return "eabi"; |
Evan Cheng | 2bffee2 | 2011-02-01 01:14:13 +0000 | [diff] [blame] | 129 | case MachO: return "macho"; |
Chandler Carruth | fd553c2 | 2012-01-10 19:46:00 +0000 | [diff] [blame] | 130 | case ANDROIDEABI: return "androideabi"; |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 133 | llvm_unreachable("Invalid EnvironmentType!"); |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 136 | Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) { |
Chandler Carruth | 06accda | 2012-02-12 09:27:38 +0000 | [diff] [blame] | 137 | 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 Dunbar | 3c2d4bf | 2009-08-03 04:03:51 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 164 | Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) { |
Daniel Dunbar | baf9b56 | 2009-09-08 23:32:51 +0000 | [diff] [blame] | 165 | // 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 Dunbar | ed68788 | 2009-09-09 23:01:25 +0000 | [diff] [blame] | 174 | // This code must be kept in sync with Clang's Darwin specific argument |
| 175 | // translation. |
| 176 | |
Chandler Carruth | 06accda | 2012-02-12 09:27:38 +0000 | [diff] [blame] | 177 | 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 Dunbar | baf9b56 | 2009-09-08 23:32:51 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Duncan Sands | bbdca3f | 2010-03-24 09:05:14 +0000 | [diff] [blame] | 194 | // Returns architecture name that is understood by the target assembler. |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 195 | const char *Triple::getArchNameForAssembler() { |
Daniel Dunbar | e1fe09f | 2011-04-19 21:12:05 +0000 | [diff] [blame] | 196 | if (!isOSDarwin() && getVendor() != Triple::Apple) |
Viktor Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 197 | return NULL; |
| 198 | |
Chandler Carruth | 06accda | 2012-02-12 09:27:38 +0000 | [diff] [blame] | 199 | 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 Kutuzov | 51cdac0 | 2009-11-17 18:48:27 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 217 | static 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 Carruth | 0a85771 | 2012-02-18 04:34:17 +0000 | [diff] [blame] | 227 | // FIXME: It would be good to replace these with explicit names for all the |
| 228 | // various suffixes supported. |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 229 | .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 Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 248 | } |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 249 | |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 250 | static 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 Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 258 | static 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 Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 283 | static 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 Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Chandler Carruth | cceb8f4 | 2012-02-20 00:02:47 +0000 | [diff] [blame] | 294 | /// \brief Construct a triple from the string representation provided. |
| 295 | /// |
Chandler Carruth | 0523f41 | 2012-02-21 08:31:18 +0000 | [diff] [blame] | 296 | /// This stores the string representation and parses the various pieces into |
| 297 | /// enum members. |
Chandler Carruth | 124e51c | 2012-02-21 03:39:36 +0000 | [diff] [blame] | 298 | Triple::Triple(const Twine &Str) |
| 299 | : Data(Str.str()), |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 300 | Arch(parseArch(getArchName())), |
| 301 | Vendor(parseVendor(getVendorName())), |
| 302 | OS(parseOS(getOSName())), |
| 303 | Environment(parseEnvironment(getEnvironmentName())) { |
Chandler Carruth | 124e51c | 2012-02-21 03:39:36 +0000 | [diff] [blame] | 304 | } |
Chandler Carruth | cceb8f4 | 2012-02-20 00:02:47 +0000 | [diff] [blame] | 305 | |
| 306 | /// \brief Construct a triple from string representations of the architecture, |
| 307 | /// vendor, and OS. |
| 308 | /// |
Chandler Carruth | 0523f41 | 2012-02-21 08:31:18 +0000 | [diff] [blame] | 309 | /// 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 Carruth | cceb8f4 | 2012-02-20 00:02:47 +0000 | [diff] [blame] | 312 | Triple::Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr) |
| 313 | : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()), |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 314 | Arch(parseArch(ArchStr.str())), |
| 315 | Vendor(parseVendor(VendorStr.str())), |
| 316 | OS(parseOS(OSStr.str())), |
Chandler Carruth | 124e51c | 2012-02-21 03:39:36 +0000 | [diff] [blame] | 317 | Environment() { |
Chandler Carruth | cceb8f4 | 2012-02-20 00:02:47 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /// \brief Construct a triple from string representations of the architecture, |
| 321 | /// vendor, OS, and environment. |
| 322 | /// |
Chandler Carruth | 0523f41 | 2012-02-21 08:31:18 +0000 | [diff] [blame] | 323 | /// This joins each argument into a canonical string representation and parses |
| 324 | /// them into enum members. |
Chandler Carruth | cceb8f4 | 2012-02-20 00:02:47 +0000 | [diff] [blame] | 325 | Triple::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 Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 329 | Arch(parseArch(ArchStr.str())), |
| 330 | Vendor(parseVendor(VendorStr.str())), |
| 331 | OS(parseOS(OSStr.str())), |
| 332 | Environment(parseEnvironment(EnvironmentStr.str())) { |
Chandler Carruth | cceb8f4 | 2012-02-20 00:02:47 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 335 | std::string Triple::normalize(StringRef Str) { |
| 336 | // Parse into components. |
| 337 | SmallVector<StringRef, 4> Components; |
| 338 | for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) { |
| 339 | Last = Str.find('-', First); |
| 340 | Components.push_back(Str.slice(First, Last)); |
| 341 | } |
| 342 | |
| 343 | // If the first component corresponds to a known architecture, preferentially |
| 344 | // use it for the architecture. If the second component corresponds to a |
| 345 | // known vendor, preferentially use it for the vendor, etc. This avoids silly |
| 346 | // component movement when a component parses as (eg) both a valid arch and a |
| 347 | // valid os. |
| 348 | ArchType Arch = UnknownArch; |
| 349 | if (Components.size() > 0) |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 350 | Arch = parseArch(Components[0]); |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 351 | VendorType Vendor = UnknownVendor; |
| 352 | if (Components.size() > 1) |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 353 | Vendor = parseVendor(Components[1]); |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 354 | OSType OS = UnknownOS; |
| 355 | if (Components.size() > 2) |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 356 | OS = parseOS(Components[2]); |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 357 | EnvironmentType Environment = UnknownEnvironment; |
| 358 | if (Components.size() > 3) |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 359 | Environment = parseEnvironment(Components[3]); |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 360 | |
| 361 | // Note which components are already in their final position. These will not |
| 362 | // be moved. |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 363 | bool Found[4]; |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 364 | Found[0] = Arch != UnknownArch; |
| 365 | Found[1] = Vendor != UnknownVendor; |
| 366 | Found[2] = OS != UnknownOS; |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 367 | Found[3] = Environment != UnknownEnvironment; |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 368 | |
| 369 | // If they are not there already, permute the components into their canonical |
| 370 | // positions by seeing if they parse as a valid architecture, and if so moving |
| 371 | // the component to the architecture position etc. |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 372 | for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) { |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 373 | if (Found[Pos]) |
| 374 | continue; // Already in the canonical position. |
| 375 | |
| 376 | for (unsigned Idx = 0; Idx != Components.size(); ++Idx) { |
| 377 | // Do not reparse any components that already matched. |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 378 | if (Idx < array_lengthof(Found) && Found[Idx]) |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 379 | continue; |
| 380 | |
| 381 | // Does this component parse as valid for the target position? |
| 382 | bool Valid = false; |
| 383 | StringRef Comp = Components[Idx]; |
| 384 | switch (Pos) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 385 | default: llvm_unreachable("unexpected component type!"); |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 386 | case 0: |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 387 | Arch = parseArch(Comp); |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 388 | Valid = Arch != UnknownArch; |
| 389 | break; |
| 390 | case 1: |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 391 | Vendor = parseVendor(Comp); |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 392 | Valid = Vendor != UnknownVendor; |
| 393 | break; |
| 394 | case 2: |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 395 | OS = parseOS(Comp); |
Duncan Sands | ae200c6 | 2011-02-02 10:08:38 +0000 | [diff] [blame] | 396 | Valid = OS != UnknownOS; |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 397 | break; |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 398 | case 3: |
Chandler Carruth | 4fbf658 | 2012-02-21 08:53:32 +0000 | [diff] [blame^] | 399 | Environment = parseEnvironment(Comp); |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 400 | Valid = Environment != UnknownEnvironment; |
| 401 | break; |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 402 | } |
| 403 | if (!Valid) |
| 404 | continue; // Nope, try the next component. |
| 405 | |
| 406 | // Move the component to the target position, pushing any non-fixed |
| 407 | // components that are in the way to the right. This tends to give |
| 408 | // good results in the common cases of a forgotten vendor component |
| 409 | // or a wrongly positioned environment. |
| 410 | if (Pos < Idx) { |
| 411 | // Insert left, pushing the existing components to the right. For |
| 412 | // example, a-b-i386 -> i386-a-b when moving i386 to the front. |
| 413 | StringRef CurrentComponent(""); // The empty component. |
| 414 | // Replace the component we are moving with an empty component. |
| 415 | std::swap(CurrentComponent, Components[Idx]); |
| 416 | // Insert the component being moved at Pos, displacing any existing |
| 417 | // components to the right. |
| 418 | for (unsigned i = Pos; !CurrentComponent.empty(); ++i) { |
| 419 | // Skip over any fixed components. |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 420 | while (i < array_lengthof(Found) && Found[i]) ++i; |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 421 | // Place the component at the new position, getting the component |
| 422 | // that was at this position - it will be moved right. |
| 423 | std::swap(CurrentComponent, Components[i]); |
| 424 | } |
| 425 | } else if (Pos > Idx) { |
| 426 | // Push right by inserting empty components until the component at Idx |
| 427 | // reaches the target position Pos. For example, pc-a -> -pc-a when |
| 428 | // moving pc to the second position. |
| 429 | do { |
| 430 | // Insert one empty component at Idx. |
| 431 | StringRef CurrentComponent(""); // The empty component. |
Duncan Sands | ae200c6 | 2011-02-02 10:08:38 +0000 | [diff] [blame] | 432 | for (unsigned i = Idx; i < Components.size();) { |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 433 | // Place the component at the new position, getting the component |
| 434 | // that was at this position - it will be moved right. |
| 435 | std::swap(CurrentComponent, Components[i]); |
| 436 | // If it was placed on top of an empty component then we are done. |
| 437 | if (CurrentComponent.empty()) |
| 438 | break; |
Duncan Sands | ae200c6 | 2011-02-02 10:08:38 +0000 | [diff] [blame] | 439 | // Advance to the next component, skipping any fixed components. |
Anders Carlsson | 15ec695 | 2011-02-05 18:19:35 +0000 | [diff] [blame] | 440 | while (++i < array_lengthof(Found) && Found[i]) |
| 441 | ; |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 442 | } |
| 443 | // The last component was pushed off the end - append it. |
| 444 | if (!CurrentComponent.empty()) |
| 445 | Components.push_back(CurrentComponent); |
| 446 | |
| 447 | // Advance Idx to the component's new position. |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 448 | while (++Idx < array_lengthof(Found) && Found[Idx]) {} |
Duncan Sands | 335db22 | 2010-08-12 11:31:39 +0000 | [diff] [blame] | 449 | } while (Idx < Pos); // Add more until the final position is reached. |
| 450 | } |
| 451 | assert(Pos < Components.size() && Components[Pos] == Comp && |
| 452 | "Component moved wrong!"); |
| 453 | Found[Pos] = true; |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Special case logic goes here. At this point Arch, Vendor and OS have the |
| 459 | // correct values for the computed components. |
| 460 | |
| 461 | // Stick the corrected components back together to form the normalized string. |
| 462 | std::string Normalized; |
| 463 | for (unsigned i = 0, e = Components.size(); i != e; ++i) { |
| 464 | if (i) Normalized += '-'; |
| 465 | Normalized += Components[i]; |
| 466 | } |
| 467 | return Normalized; |
| 468 | } |
| 469 | |
Daniel Dunbar | a14d225 | 2009-07-26 03:31:47 +0000 | [diff] [blame] | 470 | StringRef Triple::getArchName() const { |
| 471 | return StringRef(Data).split('-').first; // Isolate first component |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Daniel Dunbar | a14d225 | 2009-07-26 03:31:47 +0000 | [diff] [blame] | 474 | StringRef Triple::getVendorName() const { |
| 475 | StringRef Tmp = StringRef(Data).split('-').second; // Strip first component |
| 476 | return Tmp.split('-').first; // Isolate second component |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Daniel Dunbar | a14d225 | 2009-07-26 03:31:47 +0000 | [diff] [blame] | 479 | StringRef Triple::getOSName() const { |
| 480 | StringRef Tmp = StringRef(Data).split('-').second; // Strip first component |
| 481 | Tmp = Tmp.split('-').second; // Strip second component |
| 482 | return Tmp.split('-').first; // Isolate third component |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Daniel Dunbar | a14d225 | 2009-07-26 03:31:47 +0000 | [diff] [blame] | 485 | StringRef Triple::getEnvironmentName() const { |
| 486 | StringRef Tmp = StringRef(Data).split('-').second; // Strip first component |
| 487 | Tmp = Tmp.split('-').second; // Strip second component |
| 488 | return Tmp.split('-').second; // Strip third component |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Daniel Dunbar | a14d225 | 2009-07-26 03:31:47 +0000 | [diff] [blame] | 491 | StringRef Triple::getOSAndEnvironmentName() const { |
| 492 | StringRef Tmp = StringRef(Data).split('-').second; // Strip first component |
| 493 | return Tmp.split('-').second; // Strip second component |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 496 | static unsigned EatNumber(StringRef &Str) { |
| 497 | assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number"); |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 498 | unsigned Result = 0; |
Jim Grosbach | e509aa9 | 2010-12-17 02:10:59 +0000 | [diff] [blame] | 499 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 500 | do { |
| 501 | // Consume the leading digit. |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 502 | Result = Result*10 + (Str[0] - '0'); |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 503 | |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 504 | // Eat the digit. |
| 505 | Str = Str.substr(1); |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 506 | } while (!Str.empty() && Str[0] >= '0' && Str[0] <= '9'); |
Jim Grosbach | e509aa9 | 2010-12-17 02:10:59 +0000 | [diff] [blame] | 507 | |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 508 | return Result; |
| 509 | } |
| 510 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 511 | void Triple::getOSVersion(unsigned &Major, unsigned &Minor, |
| 512 | unsigned &Micro) const { |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 513 | StringRef OSName = getOSName(); |
Jim Grosbach | e509aa9 | 2010-12-17 02:10:59 +0000 | [diff] [blame] | 514 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 515 | // Assume that the OS portion of the triple starts with the canonical name. |
| 516 | StringRef OSTypeName = getOSTypeName(getOS()); |
| 517 | if (OSName.startswith(OSTypeName)) |
| 518 | OSName = OSName.substr(OSTypeName.size()); |
Jim Grosbach | e509aa9 | 2010-12-17 02:10:59 +0000 | [diff] [blame] | 519 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 520 | // Any unset version defaults to 0. |
| 521 | Major = Minor = Micro = 0; |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 522 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 523 | // Parse up to three components. |
| 524 | unsigned *Components[3] = { &Major, &Minor, &Micro }; |
| 525 | for (unsigned i = 0; i != 3; ++i) { |
| 526 | if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9') |
| 527 | break; |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 528 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 529 | // Consume the leading number. |
| 530 | *Components[i] = EatNumber(OSName); |
Jim Grosbach | e509aa9 | 2010-12-17 02:10:59 +0000 | [diff] [blame] | 531 | |
Daniel Dunbar | 087d6a5 | 2011-04-19 20:24:34 +0000 | [diff] [blame] | 532 | // Consume the separator, if present. |
| 533 | if (OSName.startswith(".")) |
| 534 | OSName = OSName.substr(1); |
| 535 | } |
Chris Lattner | dfc17f7 | 2009-08-12 06:19:40 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Bob Wilson | bda59fd | 2012-01-31 22:32:29 +0000 | [diff] [blame] | 538 | bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor, |
| 539 | unsigned &Micro) const { |
| 540 | getOSVersion(Major, Minor, Micro); |
| 541 | |
| 542 | switch (getOS()) { |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 543 | default: llvm_unreachable("unexpected OS for Darwin triple"); |
Bob Wilson | bda59fd | 2012-01-31 22:32:29 +0000 | [diff] [blame] | 544 | case Darwin: |
| 545 | // Default to darwin8, i.e., MacOSX 10.4. |
| 546 | if (Major == 0) |
| 547 | Major = 8; |
| 548 | // Darwin version numbers are skewed from OS X versions. |
| 549 | if (Major < 4) |
| 550 | return false; |
| 551 | Micro = 0; |
| 552 | Minor = Major - 4; |
| 553 | Major = 10; |
| 554 | break; |
| 555 | case MacOSX: |
| 556 | // Default to 10.4. |
| 557 | if (Major == 0) { |
| 558 | Major = 10; |
| 559 | Minor = 4; |
| 560 | } |
| 561 | if (Major != 10) |
| 562 | return false; |
| 563 | break; |
| 564 | case IOS: |
| 565 | // Ignore the version from the triple. This is only handled because the |
| 566 | // the clang driver combines OS X and IOS support into a common Darwin |
| 567 | // toolchain that wants to know the OS X version number even when targeting |
| 568 | // IOS. |
| 569 | Major = 10; |
| 570 | Minor = 4; |
| 571 | Micro = 0; |
| 572 | break; |
| 573 | } |
| 574 | return true; |
| 575 | } |
| 576 | |
Daniel Dunbar | a14d225 | 2009-07-26 03:31:47 +0000 | [diff] [blame] | 577 | void Triple::setTriple(const Twine &Str) { |
Chandler Carruth | 124e51c | 2012-02-21 03:39:36 +0000 | [diff] [blame] | 578 | *this = Triple(Str); |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void Triple::setArch(ArchType Kind) { |
| 582 | setArchName(getArchTypeName(Kind)); |
| 583 | } |
| 584 | |
| 585 | void Triple::setVendor(VendorType Kind) { |
| 586 | setVendorName(getVendorTypeName(Kind)); |
| 587 | } |
| 588 | |
| 589 | void Triple::setOS(OSType Kind) { |
| 590 | setOSName(getOSTypeName(Kind)); |
| 591 | } |
| 592 | |
Duncan Sands | 5754a45 | 2010-09-16 08:25:48 +0000 | [diff] [blame] | 593 | void Triple::setEnvironment(EnvironmentType Kind) { |
| 594 | setEnvironmentName(getEnvironmentTypeName(Kind)); |
| 595 | } |
| 596 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 597 | void Triple::setArchName(StringRef Str) { |
Jeffrey Yasskin | 0b22873 | 2009-10-06 21:45:26 +0000 | [diff] [blame] | 598 | // Work around a miscompilation bug for Twines in gcc 4.0.3. |
| 599 | SmallString<64> Triple; |
| 600 | Triple += Str; |
| 601 | Triple += "-"; |
| 602 | Triple += getVendorName(); |
| 603 | Triple += "-"; |
| 604 | Triple += getOSAndEnvironmentName(); |
| 605 | setTriple(Triple.str()); |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 608 | void Triple::setVendorName(StringRef Str) { |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 609 | setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName()); |
| 610 | } |
| 611 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 612 | void Triple::setOSName(StringRef Str) { |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 613 | if (hasEnvironment()) |
| 614 | setTriple(getArchName() + "-" + getVendorName() + "-" + Str + |
| 615 | "-" + getEnvironmentName()); |
| 616 | else |
| 617 | setTriple(getArchName() + "-" + getVendorName() + "-" + Str); |
| 618 | } |
| 619 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 620 | void Triple::setEnvironmentName(StringRef Str) { |
| 621 | setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 622 | "-" + Str); |
| 623 | } |
| 624 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 625 | void Triple::setOSAndEnvironmentName(StringRef Str) { |
Daniel Dunbar | 23e97b0 | 2009-04-01 21:53:23 +0000 | [diff] [blame] | 626 | setTriple(getArchName() + "-" + getVendorName() + "-" + Str); |
| 627 | } |
Chandler Carruth | 6f72ac4 | 2012-01-31 04:52:32 +0000 | [diff] [blame] | 628 | |
| 629 | static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) { |
| 630 | switch (Arch) { |
| 631 | case llvm::Triple::UnknownArch: |
Chandler Carruth | 6f72ac4 | 2012-01-31 04:52:32 +0000 | [diff] [blame] | 632 | return 0; |
| 633 | |
| 634 | case llvm::Triple::msp430: |
| 635 | return 16; |
| 636 | |
| 637 | case llvm::Triple::amdil: |
| 638 | case llvm::Triple::arm: |
| 639 | case llvm::Triple::cellspu: |
| 640 | case llvm::Triple::hexagon: |
| 641 | case llvm::Triple::le32: |
| 642 | case llvm::Triple::mblaze: |
| 643 | case llvm::Triple::mips: |
| 644 | case llvm::Triple::mipsel: |
| 645 | case llvm::Triple::ppc: |
| 646 | case llvm::Triple::ptx32: |
| 647 | case llvm::Triple::sparc: |
| 648 | case llvm::Triple::tce: |
| 649 | case llvm::Triple::thumb: |
| 650 | case llvm::Triple::x86: |
| 651 | case llvm::Triple::xcore: |
| 652 | return 32; |
| 653 | |
| 654 | case llvm::Triple::mips64: |
| 655 | case llvm::Triple::mips64el: |
| 656 | case llvm::Triple::ppc64: |
| 657 | case llvm::Triple::ptx64: |
| 658 | case llvm::Triple::sparcv9: |
| 659 | case llvm::Triple::x86_64: |
| 660 | return 64; |
| 661 | } |
| 662 | llvm_unreachable("Invalid architecture value"); |
| 663 | } |
| 664 | |
| 665 | bool Triple::isArch64Bit() const { |
| 666 | return getArchPointerBitWidth(getArch()) == 64; |
| 667 | } |
| 668 | |
| 669 | bool Triple::isArch32Bit() const { |
| 670 | return getArchPointerBitWidth(getArch()) == 32; |
| 671 | } |
| 672 | |
| 673 | bool Triple::isArch16Bit() const { |
| 674 | return getArchPointerBitWidth(getArch()) == 16; |
| 675 | } |
Chandler Carruth | 7d5a289 | 2012-02-06 20:46:33 +0000 | [diff] [blame] | 676 | |
| 677 | Triple Triple::get32BitArchVariant() const { |
| 678 | Triple T(*this); |
| 679 | switch (getArch()) { |
| 680 | case Triple::UnknownArch: |
Chandler Carruth | 7d5a289 | 2012-02-06 20:46:33 +0000 | [diff] [blame] | 681 | case Triple::msp430: |
| 682 | T.setArch(UnknownArch); |
| 683 | break; |
| 684 | |
| 685 | case Triple::amdil: |
| 686 | case Triple::arm: |
| 687 | case Triple::cellspu: |
| 688 | case Triple::hexagon: |
| 689 | case Triple::le32: |
| 690 | case Triple::mblaze: |
| 691 | case Triple::mips: |
| 692 | case Triple::mipsel: |
| 693 | case Triple::ppc: |
| 694 | case Triple::ptx32: |
| 695 | case Triple::sparc: |
| 696 | case Triple::tce: |
| 697 | case Triple::thumb: |
| 698 | case Triple::x86: |
| 699 | case Triple::xcore: |
| 700 | // Already 32-bit. |
| 701 | break; |
| 702 | |
| 703 | case Triple::mips64: T.setArch(Triple::mips); break; |
| 704 | case Triple::mips64el: T.setArch(Triple::mipsel); break; |
| 705 | case Triple::ppc64: T.setArch(Triple::ppc); break; |
| 706 | case Triple::ptx64: T.setArch(Triple::ptx32); break; |
| 707 | case Triple::sparcv9: T.setArch(Triple::sparc); break; |
| 708 | case Triple::x86_64: T.setArch(Triple::x86); break; |
| 709 | } |
| 710 | return T; |
| 711 | } |
| 712 | |
| 713 | Triple Triple::get64BitArchVariant() const { |
| 714 | Triple T(*this); |
| 715 | switch (getArch()) { |
Chandler Carruth | 7d5a289 | 2012-02-06 20:46:33 +0000 | [diff] [blame] | 716 | case Triple::UnknownArch: |
| 717 | case Triple::amdil: |
| 718 | case Triple::arm: |
| 719 | case Triple::cellspu: |
| 720 | case Triple::hexagon: |
| 721 | case Triple::le32: |
| 722 | case Triple::mblaze: |
| 723 | case Triple::msp430: |
| 724 | case Triple::tce: |
| 725 | case Triple::thumb: |
| 726 | case Triple::xcore: |
| 727 | T.setArch(UnknownArch); |
| 728 | break; |
| 729 | |
| 730 | case Triple::mips64: |
| 731 | case Triple::mips64el: |
| 732 | case Triple::ppc64: |
| 733 | case Triple::ptx64: |
| 734 | case Triple::sparcv9: |
| 735 | case Triple::x86_64: |
| 736 | // Already 64-bit. |
| 737 | break; |
| 738 | |
| 739 | case Triple::mips: T.setArch(Triple::mips64); break; |
| 740 | case Triple::mipsel: T.setArch(Triple::mips64el); break; |
| 741 | case Triple::ppc: T.setArch(Triple::ppc64); break; |
| 742 | case Triple::ptx32: T.setArch(Triple::ptx64); break; |
| 743 | case Triple::sparc: T.setArch(Triple::sparcv9); break; |
| 744 | case Triple::x86: T.setArch(Triple::x86_64); break; |
| 745 | } |
| 746 | return T; |
| 747 | } |