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