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