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