clang API Documentation
00001 //===--- TargetInfo.cpp - Information about Target machine ----------------===// 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 // This file implements the TargetInfo and TargetInfoImpl interfaces. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Basic/AddressSpaces.h" 00015 #include "clang/Basic/TargetInfo.h" 00016 #include "clang/Basic/LangOptions.h" 00017 #include "llvm/ADT/APFloat.h" 00018 #include "llvm/ADT/STLExtras.h" 00019 #include "llvm/Support/ErrorHandling.h" 00020 #include <cctype> 00021 #include <cstdlib> 00022 using namespace clang; 00023 00024 static const LangAS::Map DefaultAddrSpaceMap = { 0 }; 00025 00026 // TargetInfo Constructor. 00027 TargetInfo::TargetInfo(const std::string &T) : Triple(T) { 00028 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or 00029 // SPARC. These should be overridden by concrete targets as needed. 00030 BigEndian = true; 00031 TLSSupported = true; 00032 NoAsmVariants = false; 00033 PointerWidth = PointerAlign = 32; 00034 BoolWidth = BoolAlign = 8; 00035 IntWidth = IntAlign = 32; 00036 LongWidth = LongAlign = 32; 00037 LongLongWidth = LongLongAlign = 64; 00038 SuitableAlign = 64; 00039 HalfWidth = 16; 00040 HalfAlign = 16; 00041 FloatWidth = 32; 00042 FloatAlign = 32; 00043 DoubleWidth = 64; 00044 DoubleAlign = 64; 00045 LongDoubleWidth = 64; 00046 LongDoubleAlign = 64; 00047 LargeArrayMinWidth = 0; 00048 LargeArrayAlign = 0; 00049 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 00050 SizeType = UnsignedLong; 00051 PtrDiffType = SignedLong; 00052 IntMaxType = SignedLongLong; 00053 UIntMaxType = UnsignedLongLong; 00054 IntPtrType = SignedLong; 00055 WCharType = SignedInt; 00056 WIntType = SignedInt; 00057 Char16Type = UnsignedShort; 00058 Char32Type = UnsignedInt; 00059 Int64Type = SignedLongLong; 00060 SigAtomicType = SignedInt; 00061 UseSignedCharForObjCBool = true; 00062 UseBitFieldTypeAlignment = true; 00063 UseZeroLengthBitfieldAlignment = false; 00064 ZeroLengthBitfieldBoundary = 0; 00065 HalfFormat = &llvm::APFloat::IEEEhalf; 00066 FloatFormat = &llvm::APFloat::IEEEsingle; 00067 DoubleFormat = &llvm::APFloat::IEEEdouble; 00068 LongDoubleFormat = &llvm::APFloat::IEEEdouble; 00069 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" 00070 "i64:64:64-f32:32:32-f64:64:64-n32"; 00071 UserLabelPrefix = "_"; 00072 MCountName = "mcount"; 00073 RegParmMax = 0; 00074 SSERegParmMax = 0; 00075 HasAlignMac68kSupport = false; 00076 00077 // Default to no types using fpret. 00078 RealTypeUsesObjCFPRet = 0; 00079 00080 // Default to not using fp2ret for __Complex long double 00081 ComplexLongDoubleUsesFP2Ret = false; 00082 00083 // Default to using the Itanium ABI. 00084 CXXABI = CXXABI_Itanium; 00085 00086 // Default to an empty address space map. 00087 AddrSpaceMap = &DefaultAddrSpaceMap; 00088 00089 // Default to an unknown platform name. 00090 PlatformName = "unknown"; 00091 PlatformMinVersion = VersionTuple(); 00092 } 00093 00094 // Out of line virtual dtor for TargetInfo. 00095 TargetInfo::~TargetInfo() {} 00096 00097 /// getTypeName - Return the user string for the specified integer type enum. 00098 /// For example, SignedShort -> "short". 00099 const char *TargetInfo::getTypeName(IntType T) { 00100 switch (T) { 00101 default: llvm_unreachable("not an integer!"); 00102 case SignedShort: return "short"; 00103 case UnsignedShort: return "unsigned short"; 00104 case SignedInt: return "int"; 00105 case UnsignedInt: return "unsigned int"; 00106 case SignedLong: return "long int"; 00107 case UnsignedLong: return "long unsigned int"; 00108 case SignedLongLong: return "long long int"; 00109 case UnsignedLongLong: return "long long unsigned int"; 00110 } 00111 } 00112 00113 /// getTypeConstantSuffix - Return the constant suffix for the specified 00114 /// integer type enum. For example, SignedLong -> "L". 00115 const char *TargetInfo::getTypeConstantSuffix(IntType T) { 00116 switch (T) { 00117 default: llvm_unreachable("not an integer!"); 00118 case SignedShort: 00119 case SignedInt: return ""; 00120 case SignedLong: return "L"; 00121 case SignedLongLong: return "LL"; 00122 case UnsignedShort: 00123 case UnsignedInt: return "U"; 00124 case UnsignedLong: return "UL"; 00125 case UnsignedLongLong: return "ULL"; 00126 } 00127 } 00128 00129 /// getTypeWidth - Return the width (in bits) of the specified integer type 00130 /// enum. For example, SignedInt -> getIntWidth(). 00131 unsigned TargetInfo::getTypeWidth(IntType T) const { 00132 switch (T) { 00133 default: llvm_unreachable("not an integer!"); 00134 case SignedShort: 00135 case UnsignedShort: return getShortWidth(); 00136 case SignedInt: 00137 case UnsignedInt: return getIntWidth(); 00138 case SignedLong: 00139 case UnsignedLong: return getLongWidth(); 00140 case SignedLongLong: 00141 case UnsignedLongLong: return getLongLongWidth(); 00142 }; 00143 } 00144 00145 /// getTypeAlign - Return the alignment (in bits) of the specified integer type 00146 /// enum. For example, SignedInt -> getIntAlign(). 00147 unsigned TargetInfo::getTypeAlign(IntType T) const { 00148 switch (T) { 00149 default: llvm_unreachable("not an integer!"); 00150 case SignedShort: 00151 case UnsignedShort: return getShortAlign(); 00152 case SignedInt: 00153 case UnsignedInt: return getIntAlign(); 00154 case SignedLong: 00155 case UnsignedLong: return getLongAlign(); 00156 case SignedLongLong: 00157 case UnsignedLongLong: return getLongLongAlign(); 00158 }; 00159 } 00160 00161 /// isTypeSigned - Return whether an integer types is signed. Returns true if 00162 /// the type is signed; false otherwise. 00163 bool TargetInfo::isTypeSigned(IntType T) { 00164 switch (T) { 00165 default: llvm_unreachable("not an integer!"); 00166 case SignedShort: 00167 case SignedInt: 00168 case SignedLong: 00169 case SignedLongLong: 00170 return true; 00171 case UnsignedShort: 00172 case UnsignedInt: 00173 case UnsignedLong: 00174 case UnsignedLongLong: 00175 return false; 00176 }; 00177 } 00178 00179 /// setForcedLangOptions - Set forced language options. 00180 /// Apply changes to the target information with respect to certain 00181 /// language options which change the target configuration. 00182 void TargetInfo::setForcedLangOptions(LangOptions &Opts) { 00183 if (Opts.NoBitFieldTypeAlign) 00184 UseBitFieldTypeAlignment = false; 00185 if (Opts.ShortWChar) 00186 WCharType = UnsignedShort; 00187 } 00188 00189 //===----------------------------------------------------------------------===// 00190 00191 00192 static StringRef removeGCCRegisterPrefix(StringRef Name) { 00193 if (Name[0] == '%' || Name[0] == '#') 00194 Name = Name.substr(1); 00195 00196 return Name; 00197 } 00198 00199 /// isValidClobber - Returns whether the passed in string is 00200 /// a valid clobber in an inline asm statement. This is used by 00201 /// Sema. 00202 bool TargetInfo::isValidClobber(StringRef Name) const { 00203 return (isValidGCCRegisterName(Name) || 00204 Name == "memory" || Name == "cc"); 00205 } 00206 00207 /// isValidGCCRegisterName - Returns whether the passed in string 00208 /// is a valid register name according to GCC. This is used by Sema for 00209 /// inline asm statements. 00210 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 00211 if (Name.empty()) 00212 return false; 00213 00214 const char * const *Names; 00215 unsigned NumNames; 00216 00217 // Get rid of any register prefix. 00218 Name = removeGCCRegisterPrefix(Name); 00219 00220 getGCCRegNames(Names, NumNames); 00221 00222 // If we have a number it maps to an entry in the register name array. 00223 if (isdigit(Name[0])) { 00224 int n; 00225 if (!Name.getAsInteger(0, n)) 00226 return n >= 0 && (unsigned)n < NumNames; 00227 } 00228 00229 // Check register names. 00230 for (unsigned i = 0; i < NumNames; i++) { 00231 if (Name == Names[i]) 00232 return true; 00233 } 00234 00235 // Check any additional names that we have. 00236 const AddlRegName *AddlNames; 00237 unsigned NumAddlNames; 00238 getGCCAddlRegNames(AddlNames, NumAddlNames); 00239 for (unsigned i = 0; i < NumAddlNames; i++) 00240 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 00241 if (!AddlNames[i].Names[j]) 00242 break; 00243 // Make sure the register that the additional name is for is within 00244 // the bounds of the register names from above. 00245 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 00246 return true; 00247 } 00248 00249 // Now check aliases. 00250 const GCCRegAlias *Aliases; 00251 unsigned NumAliases; 00252 00253 getGCCRegAliases(Aliases, NumAliases); 00254 for (unsigned i = 0; i < NumAliases; i++) { 00255 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 00256 if (!Aliases[i].Aliases[j]) 00257 break; 00258 if (Aliases[i].Aliases[j] == Name) 00259 return true; 00260 } 00261 } 00262 00263 return false; 00264 } 00265 00266 StringRef 00267 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const { 00268 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 00269 00270 // Get rid of any register prefix. 00271 Name = removeGCCRegisterPrefix(Name); 00272 00273 const char * const *Names; 00274 unsigned NumNames; 00275 00276 getGCCRegNames(Names, NumNames); 00277 00278 // First, check if we have a number. 00279 if (isdigit(Name[0])) { 00280 int n; 00281 if (!Name.getAsInteger(0, n)) { 00282 assert(n >= 0 && (unsigned)n < NumNames && 00283 "Out of bounds register number!"); 00284 return Names[n]; 00285 } 00286 } 00287 00288 // Check any additional names that we have. 00289 const AddlRegName *AddlNames; 00290 unsigned NumAddlNames; 00291 getGCCAddlRegNames(AddlNames, NumAddlNames); 00292 for (unsigned i = 0; i < NumAddlNames; i++) 00293 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 00294 if (!AddlNames[i].Names[j]) 00295 break; 00296 // Make sure the register that the additional name is for is within 00297 // the bounds of the register names from above. 00298 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 00299 return Name; 00300 } 00301 00302 // Now check aliases. 00303 const GCCRegAlias *Aliases; 00304 unsigned NumAliases; 00305 00306 getGCCRegAliases(Aliases, NumAliases); 00307 for (unsigned i = 0; i < NumAliases; i++) { 00308 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 00309 if (!Aliases[i].Aliases[j]) 00310 break; 00311 if (Aliases[i].Aliases[j] == Name) 00312 return Aliases[i].Register; 00313 } 00314 } 00315 00316 return Name; 00317 } 00318 00319 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 00320 const char *Name = Info.getConstraintStr().c_str(); 00321 // An output constraint must start with '=' or '+' 00322 if (*Name != '=' && *Name != '+') 00323 return false; 00324 00325 if (*Name == '+') 00326 Info.setIsReadWrite(); 00327 00328 Name++; 00329 while (*Name) { 00330 switch (*Name) { 00331 default: 00332 if (!validateAsmConstraint(Name, Info)) { 00333 // FIXME: We temporarily return false 00334 // so we can add more constraints as we hit it. 00335 // Eventually, an unknown constraint should just be treated as 'g'. 00336 return false; 00337 } 00338 case '&': // early clobber. 00339 break; 00340 case '%': // commutative. 00341 // FIXME: Check that there is a another register after this one. 00342 break; 00343 case 'r': // general register. 00344 Info.setAllowsRegister(); 00345 break; 00346 case 'm': // memory operand. 00347 case 'o': // offsetable memory operand. 00348 case 'V': // non-offsetable memory operand. 00349 case '<': // autodecrement memory operand. 00350 case '>': // autoincrement memory operand. 00351 Info.setAllowsMemory(); 00352 break; 00353 case 'g': // general register, memory operand or immediate integer. 00354 case 'X': // any operand. 00355 Info.setAllowsRegister(); 00356 Info.setAllowsMemory(); 00357 break; 00358 case ',': // multiple alternative constraint. Pass it. 00359 // Handle additional optional '=' or '+' modifiers. 00360 if (Name[1] == '=' || Name[1] == '+') 00361 Name++; 00362 break; 00363 case '?': // Disparage slightly code. 00364 case '!': // Disparage severely. 00365 break; // Pass them. 00366 } 00367 00368 Name++; 00369 } 00370 00371 return true; 00372 } 00373 00374 bool TargetInfo::resolveSymbolicName(const char *&Name, 00375 ConstraintInfo *OutputConstraints, 00376 unsigned NumOutputs, 00377 unsigned &Index) const { 00378 assert(*Name == '[' && "Symbolic name did not start with '['"); 00379 Name++; 00380 const char *Start = Name; 00381 while (*Name && *Name != ']') 00382 Name++; 00383 00384 if (!*Name) { 00385 // Missing ']' 00386 return false; 00387 } 00388 00389 std::string SymbolicName(Start, Name - Start); 00390 00391 for (Index = 0; Index != NumOutputs; ++Index) 00392 if (SymbolicName == OutputConstraints[Index].getName()) 00393 return true; 00394 00395 return false; 00396 } 00397 00398 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints, 00399 unsigned NumOutputs, 00400 ConstraintInfo &Info) const { 00401 const char *Name = Info.ConstraintStr.c_str(); 00402 00403 while (*Name) { 00404 switch (*Name) { 00405 default: 00406 // Check if we have a matching constraint 00407 if (*Name >= '0' && *Name <= '9') { 00408 unsigned i = *Name - '0'; 00409 00410 // Check if matching constraint is out of bounds. 00411 if (i >= NumOutputs) 00412 return false; 00413 00414 // A number must refer to an output only operand. 00415 if (OutputConstraints[i].isReadWrite()) 00416 return false; 00417 00418 // If the constraint is already tied, it must be tied to the 00419 // same operand referenced to by the number. 00420 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 00421 return false; 00422 00423 // The constraint should have the same info as the respective 00424 // output constraint. 00425 Info.setTiedOperand(i, OutputConstraints[i]); 00426 } else if (!validateAsmConstraint(Name, Info)) { 00427 // FIXME: This error return is in place temporarily so we can 00428 // add more constraints as we hit it. Eventually, an unknown 00429 // constraint should just be treated as 'g'. 00430 return false; 00431 } 00432 break; 00433 case '[': { 00434 unsigned Index = 0; 00435 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index)) 00436 return false; 00437 00438 // If the constraint is already tied, it must be tied to the 00439 // same operand referenced to by the number. 00440 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 00441 return false; 00442 00443 Info.setTiedOperand(Index, OutputConstraints[Index]); 00444 break; 00445 } 00446 case '%': // commutative 00447 // FIXME: Fail if % is used with the last operand. 00448 break; 00449 case 'i': // immediate integer. 00450 case 'n': // immediate integer with a known value. 00451 break; 00452 case 'I': // Various constant constraints with target-specific meanings. 00453 case 'J': 00454 case 'K': 00455 case 'L': 00456 case 'M': 00457 case 'N': 00458 case 'O': 00459 case 'P': 00460 break; 00461 case 'r': // general register. 00462 Info.setAllowsRegister(); 00463 break; 00464 case 'm': // memory operand. 00465 case 'o': // offsettable memory operand. 00466 case 'V': // non-offsettable memory operand. 00467 case '<': // autodecrement memory operand. 00468 case '>': // autoincrement memory operand. 00469 Info.setAllowsMemory(); 00470 break; 00471 case 'g': // general register, memory operand or immediate integer. 00472 case 'X': // any operand. 00473 Info.setAllowsRegister(); 00474 Info.setAllowsMemory(); 00475 break; 00476 case 'E': // immediate floating point. 00477 case 'F': // immediate floating point. 00478 case 'p': // address operand. 00479 break; 00480 case ',': // multiple alternative constraint. Ignore comma. 00481 break; 00482 case '?': // Disparage slightly code. 00483 case '!': // Disparage severely. 00484 break; // Pass them. 00485 } 00486 00487 Name++; 00488 } 00489 00490 return true; 00491 }