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