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