TargetInfo.cpp revision e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2
1//===--- TargetInfo.cpp - Information about Target machine ----------------===// 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// This file implements the TargetInfo and TargetInfoImpl interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Basic/AddressSpaces.h" 15#include "clang/Basic/TargetInfo.h" 16#include "clang/Basic/LangOptions.h" 17#include "llvm/ADT/APFloat.h" 18#include "llvm/ADT/STLExtras.h" 19#include "llvm/Support/ErrorHandling.h" 20#include <cctype> 21#include <cstdlib> 22using namespace clang; 23 24static const LangAS::Map DefaultAddrSpaceMap = { 0 }; 25 26// TargetInfo Constructor. 27TargetInfo::TargetInfo(const std::string &T) : Triple(T) { 28 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or 29 // SPARC. These should be overridden by concrete targets as needed. 30 BigEndian = true; 31 TLSSupported = true; 32 NoAsmVariants = false; 33 PointerWidth = PointerAlign = 32; 34 BoolWidth = BoolAlign = 8; 35 IntWidth = IntAlign = 32; 36 LongWidth = LongAlign = 32; 37 LongLongWidth = LongLongAlign = 64; 38 SuitableAlign = 64; 39 HalfWidth = 16; 40 HalfAlign = 16; 41 FloatWidth = 32; 42 FloatAlign = 32; 43 DoubleWidth = 64; 44 DoubleAlign = 64; 45 LongDoubleWidth = 64; 46 LongDoubleAlign = 64; 47 LargeArrayMinWidth = 0; 48 LargeArrayAlign = 0; 49 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; 50 SizeType = UnsignedLong; 51 PtrDiffType = SignedLong; 52 IntMaxType = SignedLongLong; 53 UIntMaxType = UnsignedLongLong; 54 IntPtrType = SignedLong; 55 WCharType = SignedInt; 56 WIntType = SignedInt; 57 Char16Type = UnsignedShort; 58 Char32Type = UnsignedInt; 59 Int64Type = SignedLongLong; 60 SigAtomicType = SignedInt; 61 UseBitFieldTypeAlignment = true; 62 UseZeroLengthBitfieldAlignment = false; 63 ZeroLengthBitfieldBoundary = 0; 64 HalfFormat = &llvm::APFloat::IEEEhalf; 65 FloatFormat = &llvm::APFloat::IEEEsingle; 66 DoubleFormat = &llvm::APFloat::IEEEdouble; 67 LongDoubleFormat = &llvm::APFloat::IEEEdouble; 68 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" 69 "i64:64:64-f32:32:32-f64:64:64-n32"; 70 UserLabelPrefix = "_"; 71 MCountName = "mcount"; 72 RegParmMax = 0; 73 SSERegParmMax = 0; 74 HasAlignMac68kSupport = false; 75 76 // Default to no types using fpret. 77 RealTypeUsesObjCFPRet = 0; 78 79 // Default to not using fp2ret for __Complex long double 80 ComplexLongDoubleUsesFP2Ret = false; 81 82 // Default to using the Itanium ABI. 83 CXXABI = CXXABI_Itanium; 84 85 // Default to an empty address space map. 86 AddrSpaceMap = &DefaultAddrSpaceMap; 87 88 // Default to an unknown platform name. 89 PlatformName = "unknown"; 90 PlatformMinVersion = VersionTuple(); 91} 92 93// Out of line virtual dtor for TargetInfo. 94TargetInfo::~TargetInfo() {} 95 96/// getTypeName - Return the user string for the specified integer type enum. 97/// For example, SignedShort -> "short". 98const char *TargetInfo::getTypeName(IntType T) { 99 switch (T) { 100 default: llvm_unreachable("not an integer!"); 101 case SignedShort: return "short"; 102 case UnsignedShort: return "unsigned short"; 103 case SignedInt: return "int"; 104 case UnsignedInt: return "unsigned int"; 105 case SignedLong: return "long int"; 106 case UnsignedLong: return "long unsigned int"; 107 case SignedLongLong: return "long long int"; 108 case UnsignedLongLong: return "long long unsigned int"; 109 } 110} 111 112/// getTypeConstantSuffix - Return the constant suffix for the specified 113/// integer type enum. For example, SignedLong -> "L". 114const char *TargetInfo::getTypeConstantSuffix(IntType T) { 115 switch (T) { 116 default: llvm_unreachable("not an integer!"); 117 case SignedShort: 118 case SignedInt: return ""; 119 case SignedLong: return "L"; 120 case SignedLongLong: return "LL"; 121 case UnsignedShort: 122 case UnsignedInt: return "U"; 123 case UnsignedLong: return "UL"; 124 case UnsignedLongLong: return "ULL"; 125 } 126} 127 128/// getTypeWidth - Return the width (in bits) of the specified integer type 129/// enum. For example, SignedInt -> getIntWidth(). 130unsigned TargetInfo::getTypeWidth(IntType T) const { 131 switch (T) { 132 default: llvm_unreachable("not an integer!"); 133 case SignedShort: 134 case UnsignedShort: return getShortWidth(); 135 case SignedInt: 136 case UnsignedInt: return getIntWidth(); 137 case SignedLong: 138 case UnsignedLong: return getLongWidth(); 139 case SignedLongLong: 140 case UnsignedLongLong: return getLongLongWidth(); 141 }; 142} 143 144/// getTypeAlign - Return the alignment (in bits) of the specified integer type 145/// enum. For example, SignedInt -> getIntAlign(). 146unsigned TargetInfo::getTypeAlign(IntType T) const { 147 switch (T) { 148 default: llvm_unreachable("not an integer!"); 149 case SignedShort: 150 case UnsignedShort: return getShortAlign(); 151 case SignedInt: 152 case UnsignedInt: return getIntAlign(); 153 case SignedLong: 154 case UnsignedLong: return getLongAlign(); 155 case SignedLongLong: 156 case UnsignedLongLong: return getLongLongAlign(); 157 }; 158} 159 160/// isTypeSigned - Return whether an integer types is signed. Returns true if 161/// the type is signed; false otherwise. 162bool TargetInfo::isTypeSigned(IntType T) { 163 switch (T) { 164 default: llvm_unreachable("not an integer!"); 165 case SignedShort: 166 case SignedInt: 167 case SignedLong: 168 case SignedLongLong: 169 return true; 170 case UnsignedShort: 171 case UnsignedInt: 172 case UnsignedLong: 173 case UnsignedLongLong: 174 return false; 175 }; 176} 177 178/// setForcedLangOptions - Set forced language options. 179/// Apply changes to the target information with respect to certain 180/// language options which change the target configuration. 181void TargetInfo::setForcedLangOptions(LangOptions &Opts) { 182 if (Opts.NoBitFieldTypeAlign) 183 UseBitFieldTypeAlignment = false; 184 if (Opts.ShortWChar) 185 WCharType = UnsignedShort; 186} 187 188//===----------------------------------------------------------------------===// 189 190 191static StringRef removeGCCRegisterPrefix(StringRef Name) { 192 if (Name[0] == '%' || Name[0] == '#') 193 Name = Name.substr(1); 194 195 return Name; 196} 197 198/// isValidClobber - Returns whether the passed in string is 199/// a valid clobber in an inline asm statement. This is used by 200/// Sema. 201bool TargetInfo::isValidClobber(StringRef Name) const { 202 return (isValidGCCRegisterName(Name) || 203 Name == "memory" || Name == "cc"); 204} 205 206/// isValidGCCRegisterName - Returns whether the passed in string 207/// is a valid register name according to GCC. This is used by Sema for 208/// inline asm statements. 209bool TargetInfo::isValidGCCRegisterName(StringRef Name) const { 210 if (Name.empty()) 211 return false; 212 213 const char * const *Names; 214 unsigned NumNames; 215 216 // Get rid of any register prefix. 217 Name = removeGCCRegisterPrefix(Name); 218 219 getGCCRegNames(Names, NumNames); 220 221 // If we have a number it maps to an entry in the register name array. 222 if (isdigit(Name[0])) { 223 int n; 224 if (!Name.getAsInteger(0, n)) 225 return n >= 0 && (unsigned)n < NumNames; 226 } 227 228 // Check register names. 229 for (unsigned i = 0; i < NumNames; i++) { 230 if (Name == Names[i]) 231 return true; 232 } 233 234 // Check any additional names that we have. 235 const AddlRegName *AddlNames; 236 unsigned NumAddlNames; 237 getGCCAddlRegNames(AddlNames, NumAddlNames); 238 for (unsigned i = 0; i < NumAddlNames; i++) 239 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 240 if (!AddlNames[i].Names[j]) 241 break; 242 // Make sure the register that the additional name is for is within 243 // the bounds of the register names from above. 244 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 245 return true; 246 } 247 248 // Now check aliases. 249 const GCCRegAlias *Aliases; 250 unsigned NumAliases; 251 252 getGCCRegAliases(Aliases, NumAliases); 253 for (unsigned i = 0; i < NumAliases; i++) { 254 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 255 if (!Aliases[i].Aliases[j]) 256 break; 257 if (Aliases[i].Aliases[j] == Name) 258 return true; 259 } 260 } 261 262 return false; 263} 264 265StringRef 266TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const { 267 assert(isValidGCCRegisterName(Name) && "Invalid register passed in"); 268 269 // Get rid of any register prefix. 270 Name = removeGCCRegisterPrefix(Name); 271 272 const char * const *Names; 273 unsigned NumNames; 274 275 getGCCRegNames(Names, NumNames); 276 277 // First, check if we have a number. 278 if (isdigit(Name[0])) { 279 int n; 280 if (!Name.getAsInteger(0, n)) { 281 assert(n >= 0 && (unsigned)n < NumNames && 282 "Out of bounds register number!"); 283 return Names[n]; 284 } 285 } 286 287 // Check any additional names that we have. 288 const AddlRegName *AddlNames; 289 unsigned NumAddlNames; 290 getGCCAddlRegNames(AddlNames, NumAddlNames); 291 for (unsigned i = 0; i < NumAddlNames; i++) 292 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) { 293 if (!AddlNames[i].Names[j]) 294 break; 295 // Make sure the register that the additional name is for is within 296 // the bounds of the register names from above. 297 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames) 298 return Name; 299 } 300 301 // Now check aliases. 302 const GCCRegAlias *Aliases; 303 unsigned NumAliases; 304 305 getGCCRegAliases(Aliases, NumAliases); 306 for (unsigned i = 0; i < NumAliases; i++) { 307 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) { 308 if (!Aliases[i].Aliases[j]) 309 break; 310 if (Aliases[i].Aliases[j] == Name) 311 return Aliases[i].Register; 312 } 313 } 314 315 return Name; 316} 317 318bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const { 319 const char *Name = Info.getConstraintStr().c_str(); 320 // An output constraint must start with '=' or '+' 321 if (*Name != '=' && *Name != '+') 322 return false; 323 324 if (*Name == '+') 325 Info.setIsReadWrite(); 326 327 Name++; 328 while (*Name) { 329 switch (*Name) { 330 default: 331 if (!validateAsmConstraint(Name, Info)) { 332 // FIXME: We temporarily return false 333 // so we can add more constraints as we hit it. 334 // Eventually, an unknown constraint should just be treated as 'g'. 335 return false; 336 } 337 case '&': // early clobber. 338 break; 339 case '%': // commutative. 340 // FIXME: Check that there is a another register after this one. 341 break; 342 case 'r': // general register. 343 Info.setAllowsRegister(); 344 break; 345 case 'm': // memory operand. 346 case 'o': // offsetable memory operand. 347 case 'V': // non-offsetable memory operand. 348 case '<': // autodecrement memory operand. 349 case '>': // autoincrement memory operand. 350 Info.setAllowsMemory(); 351 break; 352 case 'g': // general register, memory operand or immediate integer. 353 case 'X': // any operand. 354 Info.setAllowsRegister(); 355 Info.setAllowsMemory(); 356 break; 357 case ',': // multiple alternative constraint. Pass it. 358 // Handle additional optional '=' or '+' modifiers. 359 if (Name[1] == '=' || Name[1] == '+') 360 Name++; 361 break; 362 case '?': // Disparage slightly code. 363 case '!': // Disparage severely. 364 break; // Pass them. 365 } 366 367 Name++; 368 } 369 370 return true; 371} 372 373bool TargetInfo::resolveSymbolicName(const char *&Name, 374 ConstraintInfo *OutputConstraints, 375 unsigned NumOutputs, 376 unsigned &Index) const { 377 assert(*Name == '[' && "Symbolic name did not start with '['"); 378 Name++; 379 const char *Start = Name; 380 while (*Name && *Name != ']') 381 Name++; 382 383 if (!*Name) { 384 // Missing ']' 385 return false; 386 } 387 388 std::string SymbolicName(Start, Name - Start); 389 390 for (Index = 0; Index != NumOutputs; ++Index) 391 if (SymbolicName == OutputConstraints[Index].getName()) 392 return true; 393 394 return false; 395} 396 397bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints, 398 unsigned NumOutputs, 399 ConstraintInfo &Info) const { 400 const char *Name = Info.ConstraintStr.c_str(); 401 402 while (*Name) { 403 switch (*Name) { 404 default: 405 // Check if we have a matching constraint 406 if (*Name >= '0' && *Name <= '9') { 407 unsigned i = *Name - '0'; 408 409 // Check if matching constraint is out of bounds. 410 if (i >= NumOutputs) 411 return false; 412 413 // A number must refer to an output only operand. 414 if (OutputConstraints[i].isReadWrite()) 415 return false; 416 417 // If the constraint is already tied, it must be tied to the 418 // same operand referenced to by the number. 419 if (Info.hasTiedOperand() && Info.getTiedOperand() != i) 420 return false; 421 422 // The constraint should have the same info as the respective 423 // output constraint. 424 Info.setTiedOperand(i, OutputConstraints[i]); 425 } else if (!validateAsmConstraint(Name, Info)) { 426 // FIXME: This error return is in place temporarily so we can 427 // add more constraints as we hit it. Eventually, an unknown 428 // constraint should just be treated as 'g'. 429 return false; 430 } 431 break; 432 case '[': { 433 unsigned Index = 0; 434 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index)) 435 return false; 436 437 // If the constraint is already tied, it must be tied to the 438 // same operand referenced to by the number. 439 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index) 440 return false; 441 442 Info.setTiedOperand(Index, OutputConstraints[Index]); 443 break; 444 } 445 case '%': // commutative 446 // FIXME: Fail if % is used with the last operand. 447 break; 448 case 'i': // immediate integer. 449 case 'n': // immediate integer with a known value. 450 break; 451 case 'I': // Various constant constraints with target-specific meanings. 452 case 'J': 453 case 'K': 454 case 'L': 455 case 'M': 456 case 'N': 457 case 'O': 458 case 'P': 459 break; 460 case 'r': // general register. 461 Info.setAllowsRegister(); 462 break; 463 case 'm': // memory operand. 464 case 'o': // offsettable memory operand. 465 case 'V': // non-offsettable memory operand. 466 case '<': // autodecrement memory operand. 467 case '>': // autoincrement memory operand. 468 Info.setAllowsMemory(); 469 break; 470 case 'g': // general register, memory operand or immediate integer. 471 case 'X': // any operand. 472 Info.setAllowsRegister(); 473 Info.setAllowsMemory(); 474 break; 475 case 'E': // immediate floating point. 476 case 'F': // immediate floating point. 477 case 'p': // address operand. 478 break; 479 case ',': // multiple alternative constraint. Ignore comma. 480 break; 481 case '?': // Disparage slightly code. 482 case '!': // Disparage severely. 483 break; // Pass them. 484 } 485 486 Name++; 487 } 488 489 return true; 490} 491