1 //===--- TargetInfo.cpp - Information about Target machine ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TargetInfo and TargetInfoImpl interfaces.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/TargetInfo.h"
15 #include "clang/Basic/AddressSpaces.h"
16 #include "clang/Basic/CharInfo.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
22 using namespace clang;
24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
26 // TargetInfo Constructor.
27 TargetInfo::TargetInfo(const std::string &T) : TargetOpts(), Triple(T)
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.
33 NoAsmVariants = false;
34 PointerWidth = PointerAlign = 32;
35 BoolWidth = BoolAlign = 8;
36 IntWidth = IntAlign = 32;
37 LongWidth = LongAlign = 32;
38 LongLongWidth = LongLongAlign = 64;
49 LargeArrayMinWidth = 0;
51 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
53 SizeType = UnsignedLong;
54 PtrDiffType = SignedLong;
55 IntMaxType = SignedLongLong;
56 UIntMaxType = UnsignedLongLong;
57 IntPtrType = SignedLong;
58 WCharType = SignedInt;
60 Char16Type = UnsignedShort;
61 Char32Type = UnsignedInt;
62 Int64Type = SignedLongLong;
63 SigAtomicType = SignedInt;
64 ProcessIDType = SignedInt;
65 UseSignedCharForObjCBool = true;
66 UseBitFieldTypeAlignment = true;
67 UseZeroLengthBitfieldAlignment = false;
68 ZeroLengthBitfieldBoundary = 0;
69 HalfFormat = &llvm::APFloat::IEEEhalf;
70 FloatFormat = &llvm::APFloat::IEEEsingle;
71 DoubleFormat = &llvm::APFloat::IEEEdouble;
72 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
73 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
74 "i64:64:64-f32:32:32-f64:64:64-n32";
75 UserLabelPrefix = "_";
76 MCountName = "mcount";
79 HasAlignMac68kSupport = false;
81 // Default to no types using fpret.
82 RealTypeUsesObjCFPRet = 0;
84 // Default to not using fp2ret for __Complex long double
85 ComplexLongDoubleUsesFP2Ret = false;
87 // Default to using the Itanium ABI.
88 TheCXXABI.set(TargetCXXABI::GenericItanium);
90 // Default to an empty address space map.
91 AddrSpaceMap = &DefaultAddrSpaceMap;
93 // Default to an unknown platform name.
94 PlatformName = "unknown";
95 PlatformMinVersion = VersionTuple();
98 // Out of line virtual dtor for TargetInfo.
99 TargetInfo::~TargetInfo() {}
101 /// getTypeName - Return the user string for the specified integer type enum.
102 /// For example, SignedShort -> "short".
103 const char *TargetInfo::getTypeName(IntType T) {
105 default: llvm_unreachable("not an integer!");
106 case SignedShort: return "short";
107 case UnsignedShort: return "unsigned short";
108 case SignedInt: return "int";
109 case UnsignedInt: return "unsigned int";
110 case SignedLong: return "long int";
111 case UnsignedLong: return "long unsigned int";
112 case SignedLongLong: return "long long int";
113 case UnsignedLongLong: return "long long unsigned int";
117 /// getTypeConstantSuffix - Return the constant suffix for the specified
118 /// integer type enum. For example, SignedLong -> "L".
119 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
121 default: llvm_unreachable("not an integer!");
123 case SignedInt: return "";
124 case SignedLong: return "L";
125 case SignedLongLong: return "LL";
127 case UnsignedInt: return "U";
128 case UnsignedLong: return "UL";
129 case UnsignedLongLong: return "ULL";
133 /// getTypeWidth - Return the width (in bits) of the specified integer type
134 /// enum. For example, SignedInt -> getIntWidth().
135 unsigned TargetInfo::getTypeWidth(IntType T) const {
137 default: llvm_unreachable("not an integer!");
139 case UnsignedShort: return getShortWidth();
141 case UnsignedInt: return getIntWidth();
143 case UnsignedLong: return getLongWidth();
145 case UnsignedLongLong: return getLongLongWidth();
149 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
150 /// enum. For example, SignedInt -> getIntAlign().
151 unsigned TargetInfo::getTypeAlign(IntType T) const {
153 default: llvm_unreachable("not an integer!");
155 case UnsignedShort: return getShortAlign();
157 case UnsignedInt: return getIntAlign();
159 case UnsignedLong: return getLongAlign();
161 case UnsignedLongLong: return getLongLongAlign();
165 /// isTypeSigned - Return whether an integer types is signed. Returns true if
166 /// the type is signed; false otherwise.
167 bool TargetInfo::isTypeSigned(IntType T) {
169 default: llvm_unreachable("not an integer!");
178 case UnsignedLongLong:
183 /// setForcedLangOptions - Set forced language options.
184 /// Apply changes to the target information with respect to certain
185 /// language options which change the target configuration.
186 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
187 if (Opts.NoBitFieldTypeAlign)
188 UseBitFieldTypeAlignment = false;
190 WCharType = UnsignedShort;
193 //===----------------------------------------------------------------------===//
196 static StringRef removeGCCRegisterPrefix(StringRef Name) {
197 if (Name[0] == '%' || Name[0] == '#')
198 Name = Name.substr(1);
203 /// isValidClobber - Returns whether the passed in string is
204 /// a valid clobber in an inline asm statement. This is used by
206 bool TargetInfo::isValidClobber(StringRef Name) const {
207 return (isValidGCCRegisterName(Name) ||
208 Name == "memory" || Name == "cc");
211 /// isValidGCCRegisterName - Returns whether the passed in string
212 /// is a valid register name according to GCC. This is used by Sema for
213 /// inline asm statements.
214 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
218 const char * const *Names;
221 // Get rid of any register prefix.
222 Name = removeGCCRegisterPrefix(Name);
224 getGCCRegNames(Names, NumNames);
226 // If we have a number it maps to an entry in the register name array.
227 if (isDigit(Name[0])) {
229 if (!Name.getAsInteger(0, n))
230 return n >= 0 && (unsigned)n < NumNames;
233 // Check register names.
234 for (unsigned i = 0; i < NumNames; i++) {
235 if (Name == Names[i])
239 // Check any additional names that we have.
240 const AddlRegName *AddlNames;
241 unsigned NumAddlNames;
242 getGCCAddlRegNames(AddlNames, NumAddlNames);
243 for (unsigned i = 0; i < NumAddlNames; i++)
244 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
245 if (!AddlNames[i].Names[j])
247 // Make sure the register that the additional name is for is within
248 // the bounds of the register names from above.
249 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
253 // Now check aliases.
254 const GCCRegAlias *Aliases;
257 getGCCRegAliases(Aliases, NumAliases);
258 for (unsigned i = 0; i < NumAliases; i++) {
259 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
260 if (!Aliases[i].Aliases[j])
262 if (Aliases[i].Aliases[j] == Name)
271 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
272 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
274 // Get rid of any register prefix.
275 Name = removeGCCRegisterPrefix(Name);
277 const char * const *Names;
280 getGCCRegNames(Names, NumNames);
282 // First, check if we have a number.
283 if (isDigit(Name[0])) {
285 if (!Name.getAsInteger(0, n)) {
286 assert(n >= 0 && (unsigned)n < NumNames &&
287 "Out of bounds register number!");
292 // Check any additional names that we have.
293 const AddlRegName *AddlNames;
294 unsigned NumAddlNames;
295 getGCCAddlRegNames(AddlNames, NumAddlNames);
296 for (unsigned i = 0; i < NumAddlNames; i++)
297 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
298 if (!AddlNames[i].Names[j])
300 // Make sure the register that the additional name is for is within
301 // the bounds of the register names from above.
302 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
306 // Now check aliases.
307 const GCCRegAlias *Aliases;
310 getGCCRegAliases(Aliases, NumAliases);
311 for (unsigned i = 0; i < NumAliases; i++) {
312 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
313 if (!Aliases[i].Aliases[j])
315 if (Aliases[i].Aliases[j] == Name)
316 return Aliases[i].Register;
323 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
324 const char *Name = Info.getConstraintStr().c_str();
325 // An output constraint must start with '=' or '+'
326 if (*Name != '=' && *Name != '+')
330 Info.setIsReadWrite();
336 if (!validateAsmConstraint(Name, Info)) {
337 // FIXME: We temporarily return false
338 // so we can add more constraints as we hit it.
339 // Eventually, an unknown constraint should just be treated as 'g'.
342 case '&': // early clobber.
344 case '%': // commutative.
345 // FIXME: Check that there is a another register after this one.
347 case 'r': // general register.
348 Info.setAllowsRegister();
350 case 'm': // memory operand.
351 case 'o': // offsetable memory operand.
352 case 'V': // non-offsetable memory operand.
353 case '<': // autodecrement memory operand.
354 case '>': // autoincrement memory operand.
355 Info.setAllowsMemory();
357 case 'g': // general register, memory operand or immediate integer.
358 case 'X': // any operand.
359 Info.setAllowsRegister();
360 Info.setAllowsMemory();
362 case ',': // multiple alternative constraint. Pass it.
363 // Handle additional optional '=' or '+' modifiers.
364 if (Name[1] == '=' || Name[1] == '+')
367 case '?': // Disparage slightly code.
368 case '!': // Disparage severely.
369 case '#': // Ignore as constraint.
370 case '*': // Ignore for choosing register preferences.
377 // If a constraint allows neither memory nor register operands it contains
378 // only modifiers. Reject it.
379 return Info.allowsMemory() || Info.allowsRegister();
382 bool TargetInfo::resolveSymbolicName(const char *&Name,
383 ConstraintInfo *OutputConstraints,
385 unsigned &Index) const {
386 assert(*Name == '[' && "Symbolic name did not start with '['");
388 const char *Start = Name;
389 while (*Name && *Name != ']')
397 std::string SymbolicName(Start, Name - Start);
399 for (Index = 0; Index != NumOutputs; ++Index)
400 if (SymbolicName == OutputConstraints[Index].getName())
406 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
408 ConstraintInfo &Info) const {
409 const char *Name = Info.ConstraintStr.c_str();
414 // Check if we have a matching constraint
415 if (*Name >= '0' && *Name <= '9') {
416 unsigned i = *Name - '0';
418 // Check if matching constraint is out of bounds.
422 // A number must refer to an output only operand.
423 if (OutputConstraints[i].isReadWrite())
426 // If the constraint is already tied, it must be tied to the
427 // same operand referenced to by the number.
428 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
431 // The constraint should have the same info as the respective
432 // output constraint.
433 Info.setTiedOperand(i, OutputConstraints[i]);
434 } else if (!validateAsmConstraint(Name, Info)) {
435 // FIXME: This error return is in place temporarily so we can
436 // add more constraints as we hit it. Eventually, an unknown
437 // constraint should just be treated as 'g'.
443 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
446 // If the constraint is already tied, it must be tied to the
447 // same operand referenced to by the number.
448 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
451 Info.setTiedOperand(Index, OutputConstraints[Index]);
454 case '%': // commutative
455 // FIXME: Fail if % is used with the last operand.
457 case 'i': // immediate integer.
458 case 'n': // immediate integer with a known value.
460 case 'I': // Various constant constraints with target-specific meanings.
469 case 'r': // general register.
470 Info.setAllowsRegister();
472 case 'm': // memory operand.
473 case 'o': // offsettable memory operand.
474 case 'V': // non-offsettable memory operand.
475 case '<': // autodecrement memory operand.
476 case '>': // autoincrement memory operand.
477 Info.setAllowsMemory();
479 case 'g': // general register, memory operand or immediate integer.
480 case 'X': // any operand.
481 Info.setAllowsRegister();
482 Info.setAllowsMemory();
484 case 'E': // immediate floating point.
485 case 'F': // immediate floating point.
486 case 'p': // address operand.
488 case ',': // multiple alternative constraint. Ignore comma.
490 case '?': // Disparage slightly code.
491 case '!': // Disparage severely.
492 case '#': // Ignore as constraint.
493 case '*': // Ignore for choosing register preferences.
503 bool TargetCXXABI::tryParse(llvm::StringRef name) {
504 const Kind unknown = static_cast<Kind>(-1);
505 Kind kind = llvm::StringSwitch<Kind>(name)
506 .Case("arm", GenericARM)
508 .Case("itanium", GenericItanium)
509 .Case("microsoft", Microsoft)
511 if (kind == unknown) return false;