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