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