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