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