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