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