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/TargetInfo.h" 00015 #include "clang/Basic/AddressSpaces.h" 00016 #include "clang/Basic/CharInfo.h" 00017 #include "clang/Basic/LangOptions.h" 00018 #include "llvm/ADT/APFloat.h" 00019 #include "llvm/ADT/STLExtras.h" 00020 #include "llvm/Support/ErrorHandling.h" 00021 #include <cstdlib> 00022 using namespace clang; 00023 00024 static const LangAS::Map DefaultAddrSpaceMap = { 0 }; 00025 00026 // TargetInfo Constructor. 00027 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), 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 MinGlobalAlign = 0; 00040 HalfWidth = 16; 00041 HalfAlign = 16; 00042 FloatWidth = 32; 00043 FloatAlign = 32; 00044 DoubleWidth = 64; 00045 DoubleAlign = 64; 00046 LongDoubleWidth = 64; 00047 LongDoubleAlign = 64; 00048 LargeArrayMinWidth = 0; 00049 LargeArrayAlign = 0; 00050 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 00051 MaxVectorAlign = 0; 00052 SizeType = UnsignedLong; 00053 PtrDiffType = SignedLong; 00054 IntMaxType = SignedLongLong; 00055 IntPtrType = SignedLong; 00056 WCharType = SignedInt; 00057 WIntType = SignedInt; 00058 Char16Type = UnsignedShort; 00059 Char32Type = UnsignedInt; 00060 Int64Type = SignedLongLong; 00061 SigAtomicType = SignedInt; 00062 ProcessIDType = SignedInt; 00063 UseSignedCharForObjCBool = true; 00064 UseBitFieldTypeAlignment = true; 00065 UseZeroLengthBitfieldAlignment = false; 00066 ZeroLengthBitfieldBoundary = 0; 00067 HalfFormat = &llvm::APFloat::IEEEhalf; 00068 FloatFormat = &llvm::APFloat::IEEEsingle; 00069 DoubleFormat = &llvm::APFloat::IEEEdouble; 00070 LongDoubleFormat = &llvm::APFloat::IEEEdouble; 00071 DescriptionString = nullptr; 00072 UserLabelPrefix = "_"; 00073 MCountName = "mcount"; 00074 RegParmMax = 0; 00075 SSERegParmMax = 0; 00076 HasAlignMac68kSupport = false; 00077 00078 // Default to no types using fpret. 00079 RealTypeUsesObjCFPRet = 0; 00080 00081 // Default to not using fp2ret for __Complex long double 00082 ComplexLongDoubleUsesFP2Ret = false; 00083 00084 // Set the C++ ABI based on the triple. 00085 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment() 00086 ? TargetCXXABI::Microsoft 00087 : TargetCXXABI::GenericItanium); 00088 00089 // Default to an empty address space map. 00090 AddrSpaceMap = &DefaultAddrSpaceMap; 00091 UseAddrSpaceMapMangling = false; 00092 00093 // Default to an unknown platform name. 00094 PlatformName = "unknown"; 00095 PlatformMinVersion = VersionTuple(); 00096 } 00097 00098 // Out of line virtual dtor for TargetInfo. 00099 TargetInfo::~TargetInfo() {} 00100 00101 /// getTypeName - Return the user string for the specified integer type enum. 00102 /// For example, SignedShort -> "short". 00103 const char *TargetInfo::getTypeName(IntType T) { 00104 switch (T) { 00105 default: llvm_unreachable("not an integer!"); 00106 case SignedChar: return "signed char"; 00107 case UnsignedChar: return "unsigned char"; 00108 case SignedShort: return "short"; 00109 case UnsignedShort: return "unsigned short"; 00110 case SignedInt: return "int"; 00111 case UnsignedInt: return "unsigned int"; 00112 case SignedLong: return "long int"; 00113 case UnsignedLong: return "long unsigned int"; 00114 case SignedLongLong: return "long long int"; 00115 case UnsignedLongLong: return "long long unsigned int"; 00116 } 00117 } 00118 00119 /// getTypeConstantSuffix - Return the constant suffix for the specified 00120 /// integer type enum. For example, SignedLong -> "L". 00121 const char *TargetInfo::getTypeConstantSuffix(IntType T) const { 00122 switch (T) { 00123 default: llvm_unreachable("not an integer!"); 00124 case SignedChar: 00125 case SignedShort: 00126 case SignedInt: return ""; 00127 case SignedLong: return "L"; 00128 case SignedLongLong: return "LL"; 00129 case UnsignedChar: 00130 if (getCharWidth() < getIntWidth()) 00131 return ""; 00132 case UnsignedShort: 00133 if (getShortWidth() < getIntWidth()) 00134 return ""; 00135 case UnsignedInt: return "U"; 00136 case UnsignedLong: return "UL"; 00137 case UnsignedLongLong: return "ULL"; 00138 } 00139 } 00140 00141 /// getTypeFormatModifier - Return the printf format modifier for the 00142 /// specified integer type enum. For example, SignedLong -> "l". 00143 00144 const char *TargetInfo::getTypeFormatModifier(IntType T) { 00145 switch (T) { 00146 default: llvm_unreachable("not an integer!"); 00147 case SignedChar: 00148 case UnsignedChar: return "hh"; 00149 case SignedShort: 00150 case UnsignedShort: return "h"; 00151 case SignedInt: 00152 case UnsignedInt: return ""; 00153 case SignedLong: 00154 case UnsignedLong: return "l"; 00155 case SignedLongLong: 00156 case UnsignedLongLong: return "ll"; 00157 } 00158 } 00159 00160 /// getTypeWidth - Return the width (in bits) of the specified integer type 00161 /// enum. For example, SignedInt -> getIntWidth(). 00162 unsigned TargetInfo::getTypeWidth(IntType T) const { 00163 switch (T) { 00164 default: llvm_unreachable("not an integer!"); 00165 case SignedChar: 00166 case UnsignedChar: return getCharWidth(); 00167 case SignedShort: 00168 case UnsignedShort: return getShortWidth(); 00169 case SignedInt: 00170 case UnsignedInt: return getIntWidth(); 00171 case SignedLong: 00172 case UnsignedLong: return getLongWidth(); 00173 case SignedLongLong: 00174 case UnsignedLongLong: return getLongLongWidth(); 00175 }; 00176 } 00177 00178 TargetInfo::IntType TargetInfo::getIntTypeByWidth( 00179 unsigned BitWidth, bool IsSigned) const { 00180 if (getCharWidth() == BitWidth) 00181 return IsSigned ? SignedChar : UnsignedChar; 00182 if (getShortWidth() == BitWidth) 00183 return IsSigned ? SignedShort : UnsignedShort; 00184 if (getIntWidth() == BitWidth) 00185 return IsSigned ? SignedInt : UnsignedInt; 00186 if (getLongWidth() == BitWidth) 00187 return IsSigned ? SignedLong : UnsignedLong; 00188 if (getLongLongWidth() == BitWidth) 00189 return IsSigned ? SignedLongLong : UnsignedLongLong; 00190 return NoInt; 00191 } 00192 00193 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth, 00194 bool IsSigned) const { 00195 if (getCharWidth() >= BitWidth) 00196 return IsSigned ? SignedChar : UnsignedChar; 00197 if (getShortWidth() >= BitWidth) 00198 return IsSigned ? SignedShort : UnsignedShort; 00199 if (getIntWidth() >= BitWidth) 00200 return IsSigned ? SignedInt : UnsignedInt; 00201 if (getLongWidth() >= BitWidth) 00202 return IsSigned ? SignedLong : UnsignedLong; 00203 if (getLongLongWidth() >= BitWidth) 00204 return IsSigned ? SignedLongLong : UnsignedLongLong; 00205 return NoInt; 00206 } 00207 00208 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const { 00209 if (getFloatWidth() == BitWidth) 00210 return Float; 00211 if (getDoubleWidth() == BitWidth) 00212 return Double; 00213 00214 switch (BitWidth) { 00215 case 96: 00216 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended) 00217 return LongDouble; 00218 break; 00219 case 128: 00220 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble || 00221 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad) 00222 return LongDouble; 00223 break; 00224 } 00225 00226 return NoFloat; 00227 } 00228 00229 /// getTypeAlign - Return the alignment (in bits) of the specified integer type 00230 /// enum. For example, SignedInt -> getIntAlign(). 00231 unsigned TargetInfo::getTypeAlign(IntType T) const { 00232 switch (T) { 00233 default: llvm_unreachable("not an integer!"); 00234 case SignedChar: 00235 case UnsignedChar: return getCharAlign(); 00236 case SignedShort: 00237 case UnsignedShort: return getShortAlign(); 00238 case SignedInt: 00239 case UnsignedInt: return getIntAlign(); 00240 case SignedLong: 00241 case UnsignedLong: return getLongAlign(); 00242 case SignedLongLong: 00243 case UnsignedLongLong: return getLongLongAlign(); 00244 }; 00245 } 00246 00247 /// isTypeSigned - Return whether an integer types is signed. Returns true if 00248 /// the type is signed; false otherwise. 00249 bool TargetInfo::isTypeSigned(IntType T) { 00250 switch (T) { 00251 default: llvm_unreachable("not an integer!"); 00252 case SignedChar: 00253 case SignedShort: 00254 case SignedInt: 00255 case SignedLong: 00256 case SignedLongLong: 00257 return true; 00258 case UnsignedChar: 00259 case UnsignedShort: 00260 case UnsignedInt: 00261 case UnsignedLong: 00262 case UnsignedLongLong: 00263 return false; 00264 }; 00265 } 00266 00267 /// adjust - Set forced language options. 00268 /// Apply changes to the target information with respect to certain 00269 /// language options which change the target configuration. 00270 void TargetInfo::adjust(const LangOptions &Opts) { 00271 if (Opts.NoBitFieldTypeAlign) 00272 UseBitFieldTypeAlignment = false; 00273 if (Opts.ShortWChar) 00274 WCharType = UnsignedShort; 00275 00276 if (Opts.OpenCL) { 00277 // OpenCL C requires specific widths for types, irrespective of 00278 // what these normally are for the target. 00279 // We also define long long and long double here, although the 00280 // OpenCL standard only mentions these as "reserved". 00281 IntWidth = IntAlign = 32; 00282 LongWidth = LongAlign = 64; 00283 LongLongWidth = LongLongAlign = 128; 00284 HalfWidth = HalfAlign = 16; 00285 FloatWidth = FloatAlign = 32; 00286 00287 // Embedded 32-bit targets (OpenCL EP) might have double C type 00288 // defined as float. Let's not override this as it might lead 00289 // to generating illegal code that uses 64bit doubles. 00290 if (DoubleWidth != FloatWidth) { 00291 DoubleWidth = DoubleAlign = 64; 00292 DoubleFormat = &llvm::APFloat::IEEEdouble; 00293 } 00294 LongDoubleWidth = LongDoubleAlign = 128; 00295 00296 assert(PointerWidth == 32 || PointerWidth == 64); 00297 bool Is32BitArch = PointerWidth == 32; 00298 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong; 00299 PtrDiffType = Is32BitArch ? SignedInt : SignedLong; 00300 IntPtrType = Is32BitArch ? SignedInt : SignedLong; 00301 00302 IntMaxType = SignedLongLong; 00303 Int64Type = SignedLong; 00304 00305 HalfFormat = &llvm::APFloat::IEEEhalf; 00306 FloatFormat = &llvm::APFloat::IEEEsingle; 00307 LongDoubleFormat = &llvm::APFloat::IEEEquad; 00308 } 00309 } 00310 00311 //===----------------------------------------------------------------------===// 00312 00313 00314 static StringRef removeGCCRegisterPrefix(StringRef Name) { 00315 if (Name[0] == '%' || Name[0] == '#') 00316 Name = Name.substr(1); 00317 00318 return Name; 00319 } 00320 00321 /// isValidClobber - Returns whether the passed in string is 00322 /// a valid clobber in an inline asm statement. This is used by 00323 /// Sema. 00324 bool TargetInfo::isValidClobber(StringRef Name) const { 00325 return (isValidGCCRegisterName(Name) || 00326 Name == "memory" || Name == "cc"); 00327 } 00328 00329 /// isValidGCCRegisterName - Returns whether the passed in string 00330 /// is a valid register name according to GCC. This is used by Sema for 00331 /// inline asm statements. 00332 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 00333 if (Name.empty()) 00334 return false; 00335 00336 const char * const *Names; 00337 unsigned NumNames; 00338 00339 // Get rid of any register prefix. 00340 Name = removeGCCRegisterPrefix(Name); 00341 if (Name.empty()) 00342 return false; 00343 00344 getGCCRegNames(Names, NumNames); 00345 00346 // If we have a number it maps to an entry in the register name array. 00347 if (isDigit(Name[0])) { 00348 int n; 00349 if (!Name.getAsInteger(0, n)) 00350 return n >= 0 && (unsigned)n < NumNames; 00351 } 00352 00353 // Check register names. 00354 for (unsigned i = 0; i < NumNames; i++) { 00355 if (Name == Names[i]) 00356 return true; 00357 } 00358 00359 // Check any additional names that we have. 00360 const AddlRegName *AddlNames; 00361 unsigned NumAddlNames; 00362 getGCCAddlRegNames(AddlNames, NumAddlNames); 00363 for (unsigned i = 0; i < NumAddlNames; i++) 00364 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 00365 if (!AddlNames[i].Names[j]) 00366 break; 00367 // Make sure the register that the additional name is for is within 00368 // the bounds of the register names from above. 00369 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 00370 return true; 00371 } 00372 00373 // Now check aliases. 00374 const GCCRegAlias *Aliases; 00375 unsigned NumAliases; 00376 00377 getGCCRegAliases(Aliases, NumAliases); 00378 for (unsigned i = 0; i < NumAliases; i++) { 00379 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 00380 if (!Aliases[i].Aliases[j]) 00381 break; 00382 if (Aliases[i].Aliases[j] == Name) 00383 return true; 00384 } 00385 } 00386 00387 return false; 00388 } 00389 00390 StringRef 00391 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const { 00392 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 00393 00394 // Get rid of any register prefix. 00395 Name = removeGCCRegisterPrefix(Name); 00396 00397 const char * const *Names; 00398 unsigned NumNames; 00399 00400 getGCCRegNames(Names, NumNames); 00401 00402 // First, check if we have a number. 00403 if (isDigit(Name[0])) { 00404 int n; 00405 if (!Name.getAsInteger(0, n)) { 00406 assert(n >= 0 && (unsigned)n < NumNames && 00407 "Out of bounds register number!"); 00408 return Names[n]; 00409 } 00410 } 00411 00412 // Check any additional names that we have. 00413 const AddlRegName *AddlNames; 00414 unsigned NumAddlNames; 00415 getGCCAddlRegNames(AddlNames, NumAddlNames); 00416 for (unsigned i = 0; i < NumAddlNames; i++) 00417 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 00418 if (!AddlNames[i].Names[j]) 00419 break; 00420 // Make sure the register that the additional name is for is within 00421 // the bounds of the register names from above. 00422 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 00423 return Name; 00424 } 00425 00426 // Now check aliases. 00427 const GCCRegAlias *Aliases; 00428 unsigned NumAliases; 00429 00430 getGCCRegAliases(Aliases, NumAliases); 00431 for (unsigned i = 0; i < NumAliases; i++) { 00432 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 00433 if (!Aliases[i].Aliases[j]) 00434 break; 00435 if (Aliases[i].Aliases[j] == Name) 00436 return Aliases[i].Register; 00437 } 00438 } 00439 00440 return Name; 00441 } 00442 00443 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 00444 const char *Name = Info.getConstraintStr().c_str(); 00445 // An output constraint must start with '=' or '+' 00446 if (*Name != '=' && *Name != '+') 00447 return false; 00448 00449 if (*Name == '+') 00450 Info.setIsReadWrite(); 00451 00452 Name++; 00453 while (*Name) { 00454 switch (*Name) { 00455 default: 00456 if (!validateAsmConstraint(Name, Info)) { 00457 // FIXME: We temporarily return false 00458 // so we can add more constraints as we hit it. 00459 // Eventually, an unknown constraint should just be treated as 'g'. 00460 return false; 00461 } 00462 case '&': // early clobber. 00463 break; 00464 case '%': // commutative. 00465 // FIXME: Check that there is a another register after this one. 00466 break; 00467 case 'r': // general register. 00468 Info.setAllowsRegister(); 00469 break; 00470 case 'm': // memory operand. 00471 case 'o': // offsetable memory operand. 00472 case 'V': // non-offsetable memory operand. 00473 case '<': // autodecrement memory operand. 00474 case '>': // autoincrement memory operand. 00475 Info.setAllowsMemory(); 00476 break; 00477 case 'g': // general register, memory operand or immediate integer. 00478 case 'X': // any operand. 00479 Info.setAllowsRegister(); 00480 Info.setAllowsMemory(); 00481 break; 00482 case ',': // multiple alternative constraint. Pass it. 00483 // Handle additional optional '=' or '+' modifiers. 00484 if (Name[1] == '=' || Name[1] == '+') 00485 Name++; 00486 break; 00487 case '?': // Disparage slightly code. 00488 case '!': // Disparage severely. 00489 case '#': // Ignore as constraint. 00490 case '*': // Ignore for choosing register preferences. 00491 break; // Pass them. 00492 } 00493 00494 Name++; 00495 } 00496 00497 // If a constraint allows neither memory nor register operands it contains 00498 // only modifiers. Reject it. 00499 return Info.allowsMemory() || Info.allowsRegister(); 00500 } 00501 00502 bool TargetInfo::resolveSymbolicName(const char *&Name, 00503 ConstraintInfo *OutputConstraints, 00504 unsigned NumOutputs, 00505 unsigned &Index) const { 00506 assert(*Name == '[' && "Symbolic name did not start with '['"); 00507 Name++; 00508 const char *Start = Name; 00509 while (*Name && *Name != ']') 00510 Name++; 00511 00512 if (!*Name) { 00513 // Missing ']' 00514 return false; 00515 } 00516 00517 std::string SymbolicName(Start, Name - Start); 00518 00519 for (Index = 0; Index != NumOutputs; ++Index) 00520 if (SymbolicName == OutputConstraints[Index].getName()) 00521 return true; 00522 00523 return false; 00524 } 00525 00526 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints, 00527 unsigned NumOutputs, 00528 ConstraintInfo &Info) const { 00529 const char *Name = Info.ConstraintStr.c_str(); 00530 00531 if (!*Name) 00532 return false; 00533 00534 while (*Name) { 00535 switch (*Name) { 00536 default: 00537 // Check if we have a matching constraint 00538 if (*Name >= '0' && *Name <= '9') { 00539 unsigned i = *Name - '0'; 00540 00541 // Check if matching constraint is out of bounds. 00542 if (i >= NumOutputs) 00543 return false; 00544 00545 // A number must refer to an output only operand. 00546 if (OutputConstraints[i].isReadWrite()) 00547 return false; 00548 00549 // If the constraint is already tied, it must be tied to the 00550 // same operand referenced to by the number. 00551 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 00552 return false; 00553 00554 // The constraint should have the same info as the respective 00555 // output constraint. 00556 Info.setTiedOperand(i, OutputConstraints[i]); 00557 } else if (!validateAsmConstraint(Name, Info)) { 00558 // FIXME: This error return is in place temporarily so we can 00559 // add more constraints as we hit it. Eventually, an unknown 00560 // constraint should just be treated as 'g'. 00561 return false; 00562 } 00563 break; 00564 case '[': { 00565 unsigned Index = 0; 00566 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index)) 00567 return false; 00568 00569 // If the constraint is already tied, it must be tied to the 00570 // same operand referenced to by the number. 00571 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 00572 return false; 00573 00574 Info.setTiedOperand(Index, OutputConstraints[Index]); 00575 break; 00576 } 00577 case '%': // commutative 00578 // FIXME: Fail if % is used with the last operand. 00579 break; 00580 case 'i': // immediate integer. 00581 case 'n': // immediate integer with a known value. 00582 break; 00583 case 'I': // Various constant constraints with target-specific meanings. 00584 case 'J': 00585 case 'K': 00586 case 'L': 00587 case 'M': 00588 case 'N': 00589 case 'O': 00590 case 'P': 00591 break; 00592 case 'r': // general register. 00593 Info.setAllowsRegister(); 00594 break; 00595 case 'm': // memory operand. 00596 case 'o': // offsettable memory operand. 00597 case 'V': // non-offsettable memory operand. 00598 case '<': // autodecrement memory operand. 00599 case '>': // autoincrement memory operand. 00600 Info.setAllowsMemory(); 00601 break; 00602 case 'g': // general register, memory operand or immediate integer. 00603 case 'X': // any operand. 00604 Info.setAllowsRegister(); 00605 Info.setAllowsMemory(); 00606 break; 00607 case 'E': // immediate floating point. 00608 case 'F': // immediate floating point. 00609 case 'p': // address operand. 00610 break; 00611 case ',': // multiple alternative constraint. Ignore comma. 00612 break; 00613 case '?': // Disparage slightly code. 00614 case '!': // Disparage severely. 00615 case '#': // Ignore as constraint. 00616 case '*': // Ignore for choosing register preferences. 00617 break; // Pass them. 00618 } 00619 00620 Name++; 00621 } 00622 00623 return true; 00624 } 00625 00626 bool TargetCXXABI::tryParse(llvm::StringRef name) { 00627 const Kind unknown = static_cast<Kind>(-1); 00628 Kind kind = llvm::StringSwitch<Kind>(name) 00629 .Case("arm", GenericARM) 00630 .Case("ios", iOS) 00631 .Case("itanium", GenericItanium) 00632 .Case("microsoft", Microsoft) 00633 .Default(unknown); 00634 if (kind == unknown) return false; 00635 00636 set(kind); 00637 return true; 00638 }