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