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/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"
22 using namespace clang;
24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
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.
31 NoAsmVariants = false;
32 PointerWidth = PointerAlign = 32;
33 BoolWidth = BoolAlign = 8;
34 IntWidth = IntAlign = 32;
35 LongWidth = LongAlign = 32;
36 LongLongWidth = LongLongAlign = 64;
45 LargeArrayMinWidth = 0;
47 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
48 SizeType = UnsignedLong;
49 PtrDiffType = SignedLong;
50 IntMaxType = SignedLongLong;
51 UIntMaxType = UnsignedLongLong;
52 IntPtrType = SignedLong;
53 WCharType = 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";
72 HasAlignMac68kSupport = false;
74 // Default to no types using fpret.
75 RealTypeUsesObjCFPRet = 0;
77 // Default to using the Itanium ABI.
78 CXXABI = CXXABI_Itanium;
80 // Default to an empty address space map.
81 AddrSpaceMap = &DefaultAddrSpaceMap;
83 // Default to an unknown platform name.
84 PlatformName = "unknown";
85 PlatformMinVersion = VersionTuple();
88 // Out of line virtual dtor for TargetInfo.
89 TargetInfo::~TargetInfo() {}
91 /// getTypeName - Return the user string for the specified integer type enum.
92 /// For example, SignedShort -> "short".
93 const char *TargetInfo::getTypeName(IntType T) {
95 default: llvm_unreachable("not an integer!");
96 case SignedShort: return "short";
97 case UnsignedShort: return "unsigned short";
98 case SignedInt: return "int";
99 case UnsignedInt: return "unsigned int";
100 case SignedLong: return "long int";
101 case UnsignedLong: return "long unsigned int";
102 case SignedLongLong: return "long long int";
103 case UnsignedLongLong: return "long long unsigned int";
107 /// getTypeConstantSuffix - Return the constant suffix for the specified
108 /// integer type enum. For example, SignedLong -> "L".
109 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
111 default: llvm_unreachable("not an integer!");
113 case SignedInt: return "";
114 case SignedLong: return "L";
115 case SignedLongLong: return "LL";
117 case UnsignedInt: return "U";
118 case UnsignedLong: return "UL";
119 case UnsignedLongLong: return "ULL";
123 /// getTypeWidth - Return the width (in bits) of the specified integer type
124 /// enum. For example, SignedInt -> getIntWidth().
125 unsigned TargetInfo::getTypeWidth(IntType T) const {
127 default: llvm_unreachable("not an integer!");
129 case UnsignedShort: return getShortWidth();
131 case UnsignedInt: return getIntWidth();
133 case UnsignedLong: return getLongWidth();
135 case UnsignedLongLong: return getLongLongWidth();
139 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
140 /// enum. For example, SignedInt -> getIntAlign().
141 unsigned TargetInfo::getTypeAlign(IntType T) const {
143 default: llvm_unreachable("not an integer!");
145 case UnsignedShort: return getShortAlign();
147 case UnsignedInt: return getIntAlign();
149 case UnsignedLong: return getLongAlign();
151 case UnsignedLongLong: return getLongLongAlign();
155 /// isTypeSigned - Return whether an integer types is signed. Returns true if
156 /// the type is signed; false otherwise.
157 bool TargetInfo::isTypeSigned(IntType T) {
159 default: llvm_unreachable("not an integer!");
168 case UnsignedLongLong:
173 /// setForcedLangOptions - Set forced language options.
174 /// Apply changes to the target information with respect to certain
175 /// language options which change the target configuration.
176 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
177 if (Opts.NoBitFieldTypeAlign)
178 UseBitFieldTypeAlignment = false;
180 WCharType = UnsignedShort;
183 //===----------------------------------------------------------------------===//
186 static StringRef removeGCCRegisterPrefix(StringRef Name) {
187 if (Name[0] == '%' || Name[0] == '#')
188 Name = Name.substr(1);
193 /// isValidClobber - Returns whether the passed in string is
194 /// a valid clobber in an inline asm statement. This is used by
196 bool TargetInfo::isValidClobber(StringRef Name) const {
197 return (isValidGCCRegisterName(Name) ||
198 Name == "memory" || Name == "cc");
201 /// isValidGCCRegisterName - Returns whether the passed in string
202 /// is a valid register name according to GCC. This is used by Sema for
203 /// inline asm statements.
204 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
208 const char * const *Names;
211 // Get rid of any register prefix.
212 Name = removeGCCRegisterPrefix(Name);
214 getGCCRegNames(Names, NumNames);
216 // If we have a number it maps to an entry in the register name array.
217 if (isdigit(Name[0])) {
219 if (!Name.getAsInteger(0, n))
220 return n >= 0 && (unsigned)n < NumNames;
223 // Check register names.
224 for (unsigned i = 0; i < NumNames; i++) {
225 if (Name == Names[i])
229 // Check any additional names that we have.
230 const AddlRegName *AddlNames;
231 unsigned NumAddlNames;
232 getGCCAddlRegNames(AddlNames, NumAddlNames);
233 for (unsigned i = 0; i < NumAddlNames; i++)
234 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
235 if (!AddlNames[i].Names[j])
237 // Make sure the register that the additional name is for is within
238 // the bounds of the register names from above.
239 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
243 // Now check aliases.
244 const GCCRegAlias *Aliases;
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])
252 if (Aliases[i].Aliases[j] == Name)
261 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
262 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
264 // Get rid of any register prefix.
265 Name = removeGCCRegisterPrefix(Name);
267 const char * const *Names;
270 getGCCRegNames(Names, NumNames);
272 // First, check if we have a number.
273 if (isdigit(Name[0])) {
275 if (!Name.getAsInteger(0, n)) {
276 assert(n >= 0 && (unsigned)n < NumNames &&
277 "Out of bounds register number!");
282 // Check any additional names that we have.
283 const AddlRegName *AddlNames;
284 unsigned NumAddlNames;
285 getGCCAddlRegNames(AddlNames, NumAddlNames);
286 for (unsigned i = 0; i < NumAddlNames; i++)
287 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
288 if (!AddlNames[i].Names[j])
290 // Make sure the register that the additional name is for is within
291 // the bounds of the register names from above.
292 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
296 // Now check aliases.
297 const GCCRegAlias *Aliases;
300 getGCCRegAliases(Aliases, NumAliases);
301 for (unsigned i = 0; i < NumAliases; i++) {
302 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
303 if (!Aliases[i].Aliases[j])
305 if (Aliases[i].Aliases[j] == Name)
306 return Aliases[i].Register;
313 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
314 const char *Name = Info.getConstraintStr().c_str();
315 // An output constraint must start with '=' or '+'
316 if (*Name != '=' && *Name != '+')
320 Info.setIsReadWrite();
326 if (!validateAsmConstraint(Name, Info)) {
327 // FIXME: We temporarily return false
328 // so we can add more constraints as we hit it.
329 // Eventually, an unknown constraint should just be treated as 'g'.
332 case '&': // early clobber.
334 case '%': // commutative.
335 // FIXME: Check that there is a another register after this one.
337 case 'r': // general register.
338 Info.setAllowsRegister();
340 case 'm': // memory operand.
341 case 'o': // offsetable memory operand.
342 case 'V': // non-offsetable memory operand.
343 case '<': // autodecrement memory operand.
344 case '>': // autoincrement memory operand.
345 Info.setAllowsMemory();
347 case 'g': // general register, memory operand or immediate integer.
348 case 'X': // any operand.
349 Info.setAllowsRegister();
350 Info.setAllowsMemory();
352 case ',': // multiple alternative constraint. Pass it.
353 // Handle additional optional '=' or '+' modifiers.
354 if (Name[1] == '=' || Name[1] == '+')
357 case '?': // Disparage slightly code.
358 case '!': // Disparage severely.
368 bool TargetInfo::resolveSymbolicName(const char *&Name,
369 ConstraintInfo *OutputConstraints,
371 unsigned &Index) const {
372 assert(*Name == '[' && "Symbolic name did not start with '['");
374 const char *Start = Name;
375 while (*Name && *Name != ']')
383 std::string SymbolicName(Start, Name - Start);
385 for (Index = 0; Index != NumOutputs; ++Index)
386 if (SymbolicName == OutputConstraints[Index].getName())
392 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
394 ConstraintInfo &Info) const {
395 const char *Name = Info.ConstraintStr.c_str();
400 // Check if we have a matching constraint
401 if (*Name >= '0' && *Name <= '9') {
402 unsigned i = *Name - '0';
404 // Check if matching constraint is out of bounds.
408 // A number must refer to an output only operand.
409 if (OutputConstraints[i].isReadWrite())
412 // If the constraint is already tied, it must be tied to the
413 // same operand referenced to by the number.
414 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
417 // The constraint should have the same info as the respective
418 // output constraint.
419 Info.setTiedOperand(i, OutputConstraints[i]);
420 } else if (!validateAsmConstraint(Name, Info)) {
421 // FIXME: This error return is in place temporarily so we can
422 // add more constraints as we hit it. Eventually, an unknown
423 // constraint should just be treated as 'g'.
429 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
432 // If the constraint is already tied, it must be tied to the
433 // same operand referenced to by the number.
434 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
437 Info.setTiedOperand(Index, OutputConstraints[Index]);
440 case '%': // commutative
441 // FIXME: Fail if % is used with the last operand.
443 case 'i': // immediate integer.
444 case 'n': // immediate integer with a known value.
446 case 'I': // Various constant constraints with target-specific meanings.
455 case 'r': // general register.
456 Info.setAllowsRegister();
458 case 'm': // memory operand.
459 case 'o': // offsettable memory operand.
460 case 'V': // non-offsettable memory operand.
461 case '<': // autodecrement memory operand.
462 case '>': // autoincrement memory operand.
463 Info.setAllowsMemory();
465 case 'g': // general register, memory operand or immediate integer.
466 case 'X': // any operand.
467 Info.setAllowsRegister();
468 Info.setAllowsMemory();
470 case 'E': // immediate floating point.
471 case 'F': // immediate floating point.
472 case 'p': // address operand.
474 case ',': // multiple alternative constraint. Ignore comma.
476 case '?': // Disparage slightly code.
477 case '!': // Disparage severely.