LLVM API Documentation
00001 //===--- Triple.cpp - Target triple helper class --------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/ADT/Triple.h" 00011 00012 #include "llvm/ADT/SmallString.h" 00013 #include "llvm/ADT/Twine.h" 00014 #include <cassert> 00015 #include <cstring> 00016 using namespace llvm; 00017 00018 // 00019 00020 const char *Triple::getArchTypeName(ArchType Kind) { 00021 switch (Kind) { 00022 case InvalidArch: return "<invalid>"; 00023 case UnknownArch: return "unknown"; 00024 00025 case alpha: return "alpha"; 00026 case arm: return "arm"; 00027 case bfin: return "bfin"; 00028 case cellspu: return "cellspu"; 00029 case mips: return "mips"; 00030 case mipsel: return "mipsel"; 00031 case msp430: return "msp430"; 00032 case pic16: return "pic16"; 00033 case ppc64: return "powerpc64"; 00034 case ppc: return "powerpc"; 00035 case sparc: return "sparc"; 00036 case systemz: return "s390x"; 00037 case tce: return "tce"; 00038 case thumb: return "thumb"; 00039 case x86: return "i386"; 00040 case x86_64: return "x86_64"; 00041 case xcore: return "xcore"; 00042 } 00043 00044 return "<invalid>"; 00045 } 00046 00047 const char *Triple::getVendorTypeName(VendorType Kind) { 00048 switch (Kind) { 00049 case UnknownVendor: return "unknown"; 00050 00051 case Apple: return "apple"; 00052 case PC: return "pc"; 00053 } 00054 00055 return "<invalid>"; 00056 } 00057 00058 const char *Triple::getOSTypeName(OSType Kind) { 00059 switch (Kind) { 00060 case UnknownOS: return "unknown"; 00061 00062 case AuroraUX: return "auroraux"; 00063 case Cygwin: return "cygwin"; 00064 case Darwin: return "darwin"; 00065 case DragonFly: return "dragonfly"; 00066 case FreeBSD: return "freebsd"; 00067 case Linux: return "linux"; 00068 case MinGW32: return "mingw32"; 00069 case MinGW64: return "mingw64"; 00070 case NetBSD: return "netbsd"; 00071 case OpenBSD: return "openbsd"; 00072 case Solaris: return "solaris"; 00073 case Win32: return "win32"; 00074 } 00075 00076 return "<invalid>"; 00077 } 00078 00079 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) { 00080 if (Name == "alpha") 00081 return alpha; 00082 if (Name == "arm") 00083 return arm; 00084 if (Name == "bfin") 00085 return bfin; 00086 if (Name == "cellspu") 00087 return cellspu; 00088 if (Name == "mips") 00089 return mips; 00090 if (Name == "mipsel") 00091 return mipsel; 00092 if (Name == "msp430") 00093 return msp430; 00094 if (Name == "pic16") 00095 return pic16; 00096 if (Name == "ppc64") 00097 return ppc64; 00098 if (Name == "ppc") 00099 return ppc; 00100 if (Name == "sparc") 00101 return sparc; 00102 if (Name == "systemz") 00103 return systemz; 00104 if (Name == "tce") 00105 return tce; 00106 if (Name == "thumb") 00107 return thumb; 00108 if (Name == "x86") 00109 return x86; 00110 if (Name == "x86-64") 00111 return x86_64; 00112 if (Name == "xcore") 00113 return xcore; 00114 00115 return UnknownArch; 00116 } 00117 00118 // 00119 00120 void Triple::Parse() const { 00121 assert(!isInitialized() && "Invalid parse call."); 00122 00123 StringRef ArchName = getArchName(); 00124 StringRef VendorName = getVendorName(); 00125 StringRef OSName = getOSName(); 00126 00127 if (ArchName.size() == 4 && ArchName[0] == 'i' && 00128 ArchName[2] == '8' && ArchName[3] == '6' && 00129 ArchName[1] - '3' < 6) // i[3-9]86 00130 Arch = x86; 00131 else if (ArchName == "amd64" || ArchName == "x86_64") 00132 Arch = x86_64; 00133 else if (ArchName == "bfin") 00134 Arch = bfin; 00135 else if (ArchName == "pic16") 00136 Arch = pic16; 00137 else if (ArchName == "powerpc") 00138 Arch = ppc; 00139 else if (ArchName == "powerpc64") 00140 Arch = ppc64; 00141 else if (ArchName == "arm" || 00142 ArchName.startswith("armv") || 00143 ArchName == "xscale") 00144 Arch = arm; 00145 else if (ArchName == "thumb" || 00146 ArchName.startswith("thumbv")) 00147 Arch = thumb; 00148 else if (ArchName.startswith("alpha")) 00149 Arch = alpha; 00150 else if (ArchName == "spu" || ArchName == "cellspu") 00151 Arch = cellspu; 00152 else if (ArchName == "msp430") 00153 Arch = msp430; 00154 else if (ArchName == "mips" || ArchName == "mipsallegrex") 00155 Arch = mips; 00156 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" || 00157 ArchName == "psp") 00158 Arch = mipsel; 00159 else if (ArchName == "sparc") 00160 Arch = sparc; 00161 else if (ArchName == "s390x") 00162 Arch = systemz; 00163 else if (ArchName == "tce") 00164 Arch = tce; 00165 else 00166 Arch = UnknownArch; 00167 00168 00169 // Handle some exceptional cases where the OS / environment components are 00170 // stuck into the vendor field. 00171 if (StringRef(getTriple()).count('-') == 1) { 00172 StringRef VendorName = getVendorName(); 00173 00174 if (VendorName.startswith("mingw32")) { // 'i386-mingw32', etc. 00175 Vendor = PC; 00176 OS = MinGW32; 00177 return; 00178 } 00179 00180 // arm-elf is another example, but we don't currently parse anything about 00181 // the environment. 00182 } 00183 00184 if (VendorName == "apple") 00185 Vendor = Apple; 00186 else if (VendorName == "pc") 00187 Vendor = PC; 00188 else 00189 Vendor = UnknownVendor; 00190 00191 if (OSName.startswith("auroraux")) 00192 OS = AuroraUX; 00193 else if (OSName.startswith("cygwin")) 00194 OS = Cygwin; 00195 else if (OSName.startswith("darwin")) 00196 OS = Darwin; 00197 else if (OSName.startswith("dragonfly")) 00198 OS = DragonFly; 00199 else if (OSName.startswith("freebsd")) 00200 OS = FreeBSD; 00201 else if (OSName.startswith("linux")) 00202 OS = Linux; 00203 else if (OSName.startswith("mingw32")) 00204 OS = MinGW32; 00205 else if (OSName.startswith("mingw64")) 00206 OS = MinGW64; 00207 else if (OSName.startswith("netbsd")) 00208 OS = NetBSD; 00209 else if (OSName.startswith("openbsd")) 00210 OS = OpenBSD; 00211 else if (OSName.startswith("solaris")) 00212 OS = Solaris; 00213 else if (OSName.startswith("win32")) 00214 OS = Win32; 00215 else 00216 OS = UnknownOS; 00217 00218 assert(isInitialized() && "Failed to initialize!"); 00219 } 00220 00221 StringRef Triple::getArchName() const { 00222 return StringRef(Data).split('-').first; // Isolate first component 00223 } 00224 00225 StringRef Triple::getVendorName() const { 00226 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 00227 return Tmp.split('-').first; // Isolate second component 00228 } 00229 00230 StringRef Triple::getOSName() const { 00231 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 00232 Tmp = Tmp.split('-').second; // Strip second component 00233 return Tmp.split('-').first; // Isolate third component 00234 } 00235 00236 StringRef Triple::getEnvironmentName() const { 00237 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 00238 Tmp = Tmp.split('-').second; // Strip second component 00239 return Tmp.split('-').second; // Strip third component 00240 } 00241 00242 StringRef Triple::getOSAndEnvironmentName() const { 00243 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component 00244 return Tmp.split('-').second; // Strip second component 00245 } 00246 00247 static unsigned EatNumber(StringRef &Str) { 00248 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number"); 00249 unsigned Result = Str[0]-'0'; 00250 00251 // Eat the digit. 00252 Str = Str.substr(1); 00253 00254 // Handle "darwin11". 00255 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') { 00256 Result = Result*10 + (Str[0] - '0'); 00257 // Eat the digit. 00258 Str = Str.substr(1); 00259 } 00260 00261 return Result; 00262 } 00263 00264 /// getDarwinNumber - Parse the 'darwin number' out of the specific target 00265 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is 00266 /// not defined, return 0's. This requires that the triple have an OSType of 00267 /// darwin before it is called. 00268 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min, 00269 unsigned &Revision) const { 00270 assert(getOS() == Darwin && "Not a darwin target triple!"); 00271 StringRef OSName = getOSName(); 00272 assert(OSName.startswith("darwin") && "Unknown darwin target triple!"); 00273 00274 // Strip off "darwin". 00275 OSName = OSName.substr(6); 00276 00277 Maj = Min = Revision = 0; 00278 00279 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9') 00280 return; 00281 00282 // The major version is the first digit. 00283 Maj = EatNumber(OSName); 00284 if (OSName.empty()) return; 00285 00286 // Handle minor version: 10.4.9 -> darwin8.9. 00287 if (OSName[0] != '.') 00288 return; 00289 00290 // Eat the '.'. 00291 OSName = OSName.substr(1); 00292 00293 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9') 00294 return; 00295 00296 Min = EatNumber(OSName); 00297 if (OSName.empty()) return; 00298 00299 // Handle revision darwin8.9.1 00300 if (OSName[0] != '.') 00301 return; 00302 00303 // Eat the '.'. 00304 OSName = OSName.substr(1); 00305 00306 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9') 00307 return; 00308 00309 Revision = EatNumber(OSName); 00310 } 00311 00312 void Triple::setTriple(const Twine &Str) { 00313 Data = Str.str(); 00314 Arch = InvalidArch; 00315 } 00316 00317 void Triple::setArch(ArchType Kind) { 00318 setArchName(getArchTypeName(Kind)); 00319 } 00320 00321 void Triple::setVendor(VendorType Kind) { 00322 setVendorName(getVendorTypeName(Kind)); 00323 } 00324 00325 void Triple::setOS(OSType Kind) { 00326 setOSName(getOSTypeName(Kind)); 00327 } 00328 00329 void Triple::setArchName(const StringRef &Str) { 00330 // Work around a miscompilation bug for Twines in gcc 4.0.3. 00331 SmallString<64> Triple; 00332 Triple += Str; 00333 Triple += "-"; 00334 Triple += getVendorName(); 00335 Triple += "-"; 00336 Triple += getOSAndEnvironmentName(); 00337 setTriple(Triple.str()); 00338 } 00339 00340 void Triple::setVendorName(const StringRef &Str) { 00341 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName()); 00342 } 00343 00344 void Triple::setOSName(const StringRef &Str) { 00345 if (hasEnvironment()) 00346 setTriple(getArchName() + "-" + getVendorName() + "-" + Str + 00347 "-" + getEnvironmentName()); 00348 else 00349 setTriple(getArchName() + "-" + getVendorName() + "-" + Str); 00350 } 00351 00352 void Triple::setEnvironmentName(const StringRef &Str) { 00353 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 00354 "-" + Str); 00355 } 00356 00357 void Triple::setOSAndEnvironmentName(const StringRef &Str) { 00358 setTriple(getArchName() + "-" + getVendorName() + "-" + Str); 00359 }