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.
32 NoAsmVariants = false;
33 PointerWidth = PointerAlign = 32;
34 BoolWidth = BoolAlign = 8;
35 IntWidth = IntAlign = 32;
36 LongWidth = LongAlign = 32;
37 LongLongWidth = LongLongAlign = 64;
47 LargeArrayMinWidth = 0;
49 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
50 SizeType = UnsignedLong;
51 PtrDiffType = SignedLong;
52 IntMaxType = SignedLongLong;
53 UIntMaxType = UnsignedLongLong;
54 IntPtrType = SignedLong;
55 WCharType = SignedInt;
57 Char16Type = UnsignedShort;
58 Char32Type = UnsignedInt;
59 Int64Type = SignedLongLong;
60 SigAtomicType = SignedInt;
61 UseSignedCharForObjCBool = true;
62 UseBitFieldTypeAlignment = true;
63 UseZeroLengthBitfieldAlignment = false;
64 ZeroLengthBitfieldBoundary = 0;
65 HalfFormat = &llvm::APFloat::IEEEhalf;
66 FloatFormat = &llvm::APFloat::IEEEsingle;
67 DoubleFormat = &llvm::APFloat::IEEEdouble;
68 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
69 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
70 "i64:64:64-f32:32:32-f64:64:64-n32";
71 UserLabelPrefix = "_";
72 MCountName = "mcount";
75 HasAlignMac68kSupport = false;
77 // Default to no types using fpret.
78 RealTypeUsesObjCFPRet = 0;
80 // Default to not using fp2ret for __Complex long double
81 ComplexLongDoubleUsesFP2Ret = false;
83 // Default to using the Itanium ABI.
84 CXXABI = CXXABI_Itanium;
86 // Default to an empty address space map.
87 AddrSpaceMap = &DefaultAddrSpaceMap;
89 // Default to an unknown platform name.
90 PlatformName = "unknown";
91 PlatformMinVersion = VersionTuple();
94 // Out of line virtual dtor for TargetInfo.
95 TargetInfo::~TargetInfo() {}
97 /// getTypeName - Return the user string for the specified integer type enum.
98 /// For example, SignedShort -> "short".
99 const char *TargetInfo::getTypeName(IntType T) {
101 default: llvm_unreachable("not an integer!");
102 case SignedShort: return "short";
103 case UnsignedShort: return "unsigned short";
104 case SignedInt: return "int";
105 case UnsignedInt: return "unsigned int";
106 case SignedLong: return "long int";
107 case UnsignedLong: return "long unsigned int";
108 case SignedLongLong: return "long long int";
109 case UnsignedLongLong: return "long long unsigned int";
113 /// getTypeConstantSuffix - Return the constant suffix for the specified
114 /// integer type enum. For example, SignedLong -> "L".
115 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
117 default: llvm_unreachable("not an integer!");
119 case SignedInt: return "";
120 case SignedLong: return "L";
121 case SignedLongLong: return "LL";
123 case UnsignedInt: return "U";
124 case UnsignedLong: return "UL";
125 case UnsignedLongLong: return "ULL";
129 /// getTypeWidth - Return the width (in bits) of the specified integer type
130 /// enum. For example, SignedInt -> getIntWidth().
131 unsigned TargetInfo::getTypeWidth(IntType T) const {
133 default: llvm_unreachable("not an integer!");
135 case UnsignedShort: return getShortWidth();
137 case UnsignedInt: return getIntWidth();
139 case UnsignedLong: return getLongWidth();
141 case UnsignedLongLong: return getLongLongWidth();
145 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
146 /// enum. For example, SignedInt -> getIntAlign().
147 unsigned TargetInfo::getTypeAlign(IntType T) const {
149 default: llvm_unreachable("not an integer!");
151 case UnsignedShort: return getShortAlign();
153 case UnsignedInt: return getIntAlign();
155 case UnsignedLong: return getLongAlign();
157 case UnsignedLongLong: return getLongLongAlign();
161 /// isTypeSigned - Return whether an integer types is signed. Returns true if
162 /// the type is signed; false otherwise.
163 bool TargetInfo::isTypeSigned(IntType T) {
165 default: llvm_unreachable("not an integer!");
174 case UnsignedLongLong:
179 /// setForcedLangOptions - Set forced language options.
180 /// Apply changes to the target information with respect to certain
181 /// language options which change the target configuration.
182 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
183 if (Opts.NoBitFieldTypeAlign)
184 UseBitFieldTypeAlignment = false;
186 WCharType = UnsignedShort;
189 //===----------------------------------------------------------------------===//
192 static StringRef removeGCCRegisterPrefix(StringRef Name) {
193 if (Name[0] == '%' || Name[0] == '#')
194 Name = Name.substr(1);
199 /// isValidClobber - Returns whether the passed in string is
200 /// a valid clobber in an inline asm statement. This is used by
202 bool TargetInfo::isValidClobber(StringRef Name) const {
203 return (isValidGCCRegisterName(Name) ||
204 Name == "memory" || Name == "cc");
207 /// isValidGCCRegisterName - Returns whether the passed in string
208 /// is a valid register name according to GCC. This is used by Sema for
209 /// inline asm statements.
210 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
214 const char * const *Names;
217 // Get rid of any register prefix.
218 Name = removeGCCRegisterPrefix(Name);
220 getGCCRegNames(Names, NumNames);
222 // If we have a number it maps to an entry in the register name array.
223 if (isdigit(Name[0])) {
225 if (!Name.getAsInteger(0, n))
226 return n >= 0 && (unsigned)n < NumNames;
229 // Check register names.
230 for (unsigned i = 0; i < NumNames; i++) {
231 if (Name == Names[i])
235 // Check any additional names that we have.
236 const AddlRegName *AddlNames;
237 unsigned NumAddlNames;
238 getGCCAddlRegNames(AddlNames, NumAddlNames);
239 for (unsigned i = 0; i < NumAddlNames; i++)
240 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
241 if (!AddlNames[i].Names[j])
243 // Make sure the register that the additional name is for is within
244 // the bounds of the register names from above.
245 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
249 // Now check aliases.
250 const GCCRegAlias *Aliases;
253 getGCCRegAliases(Aliases, NumAliases);
254 for (unsigned i = 0; i < NumAliases; i++) {
255 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
256 if (!Aliases[i].Aliases[j])
258 if (Aliases[i].Aliases[j] == Name)
267 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
268 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
270 // Get rid of any register prefix.
271 Name = removeGCCRegisterPrefix(Name);
273 const char * const *Names;
276 getGCCRegNames(Names, NumNames);
278 // First, check if we have a number.
279 if (isdigit(Name[0])) {
281 if (!Name.getAsInteger(0, n)) {
282 assert(n >= 0 && (unsigned)n < NumNames &&
283 "Out of bounds register number!");
288 // Check any additional names that we have.
289 const AddlRegName *AddlNames;
290 unsigned NumAddlNames;
291 getGCCAddlRegNames(AddlNames, NumAddlNames);
292 for (unsigned i = 0; i < NumAddlNames; i++)
293 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
294 if (!AddlNames[i].Names[j])
296 // Make sure the register that the additional name is for is within
297 // the bounds of the register names from above.
298 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
302 // Now check aliases.
303 const GCCRegAlias *Aliases;
306 getGCCRegAliases(Aliases, NumAliases);
307 for (unsigned i = 0; i < NumAliases; i++) {
308 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
309 if (!Aliases[i].Aliases[j])
311 if (Aliases[i].Aliases[j] == Name)
312 return Aliases[i].Register;
319 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
320 const char *Name = Info.getConstraintStr().c_str();
321 // An output constraint must start with '=' or '+'
322 if (*Name != '=' && *Name != '+')
326 Info.setIsReadWrite();
332 if (!validateAsmConstraint(Name, Info)) {
333 // FIXME: We temporarily return false
334 // so we can add more constraints as we hit it.
335 // Eventually, an unknown constraint should just be treated as 'g'.
338 case '&': // early clobber.
340 case '%': // commutative.
341 // FIXME: Check that there is a another register after this one.
343 case 'r': // general register.
344 Info.setAllowsRegister();
346 case 'm': // memory operand.
347 case 'o': // offsetable memory operand.
348 case 'V': // non-offsetable memory operand.
349 case '<': // autodecrement memory operand.
350 case '>': // autoincrement memory operand.
351 Info.setAllowsMemory();
353 case 'g': // general register, memory operand or immediate integer.
354 case 'X': // any operand.
355 Info.setAllowsRegister();
356 Info.setAllowsMemory();
358 case ',': // multiple alternative constraint. Pass it.
359 // Handle additional optional '=' or '+' modifiers.
360 if (Name[1] == '=' || Name[1] == '+')
363 case '?': // Disparage slightly code.
364 case '!': // Disparage severely.
374 bool TargetInfo::resolveSymbolicName(const char *&Name,
375 ConstraintInfo *OutputConstraints,
377 unsigned &Index) const {
378 assert(*Name == '[' && "Symbolic name did not start with '['");
380 const char *Start = Name;
381 while (*Name && *Name != ']')
389 std::string SymbolicName(Start, Name - Start);
391 for (Index = 0; Index != NumOutputs; ++Index)
392 if (SymbolicName == OutputConstraints[Index].getName())
398 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
400 ConstraintInfo &Info) const {
401 const char *Name = Info.ConstraintStr.c_str();
406 // Check if we have a matching constraint
407 if (*Name >= '0' && *Name <= '9') {
408 unsigned i = *Name - '0';
410 // Check if matching constraint is out of bounds.
414 // A number must refer to an output only operand.
415 if (OutputConstraints[i].isReadWrite())
418 // If the constraint is already tied, it must be tied to the
419 // same operand referenced to by the number.
420 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
423 // The constraint should have the same info as the respective
424 // output constraint.
425 Info.setTiedOperand(i, OutputConstraints[i]);
426 } else if (!validateAsmConstraint(Name, Info)) {
427 // FIXME: This error return is in place temporarily so we can
428 // add more constraints as we hit it. Eventually, an unknown
429 // constraint should just be treated as 'g'.
435 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
438 // If the constraint is already tied, it must be tied to the
439 // same operand referenced to by the number.
440 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
443 Info.setTiedOperand(Index, OutputConstraints[Index]);
446 case '%': // commutative
447 // FIXME: Fail if % is used with the last operand.
449 case 'i': // immediate integer.
450 case 'n': // immediate integer with a known value.
452 case 'I': // Various constant constraints with target-specific meanings.
461 case 'r': // general register.
462 Info.setAllowsRegister();
464 case 'm': // memory operand.
465 case 'o': // offsettable memory operand.
466 case 'V': // non-offsettable memory operand.
467 case '<': // autodecrement memory operand.
468 case '>': // autoincrement memory operand.
469 Info.setAllowsMemory();
471 case 'g': // general register, memory operand or immediate integer.
472 case 'X': // any operand.
473 Info.setAllowsRegister();
474 Info.setAllowsMemory();
476 case 'E': // immediate floating point.
477 case 'F': // immediate floating point.
478 case 'p': // address operand.
480 case ',': // multiple alternative constraint. Ignore comma.
482 case '?': // Disparage slightly code.
483 case '!': // Disparage severely.