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 UseBitFieldTypeAlignment = true;
62 UseZeroLengthBitfieldAlignment = false;
63 ZeroLengthBitfieldBoundary = 0;
64 HalfFormat = &llvm::APFloat::IEEEhalf;
65 FloatFormat = &llvm::APFloat::IEEEsingle;
66 DoubleFormat = &llvm::APFloat::IEEEdouble;
67 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
68 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
69 "i64:64:64-f32:32:32-f64:64:64-n32";
70 UserLabelPrefix = "_";
71 MCountName = "mcount";
74 HasAlignMac68kSupport = false;
76 // Default to no types using fpret.
77 RealTypeUsesObjCFPRet = 0;
79 // Default to not using fp2ret for __Complex long double
80 ComplexLongDoubleUsesFP2Ret = false;
82 // Default to using the Itanium ABI.
83 CXXABI = CXXABI_Itanium;
85 // Default to an empty address space map.
86 AddrSpaceMap = &DefaultAddrSpaceMap;
88 // Default to an unknown platform name.
89 PlatformName = "unknown";
90 PlatformMinVersion = VersionTuple();
93 // Out of line virtual dtor for TargetInfo.
94 TargetInfo::~TargetInfo() {}
96 /// getTypeName - Return the user string for the specified integer type enum.
97 /// For example, SignedShort -> "short".
98 const char *TargetInfo::getTypeName(IntType T) {
100 default: llvm_unreachable("not an integer!");
101 case SignedShort: return "short";
102 case UnsignedShort: return "unsigned short";
103 case SignedInt: return "int";
104 case UnsignedInt: return "unsigned int";
105 case SignedLong: return "long int";
106 case UnsignedLong: return "long unsigned int";
107 case SignedLongLong: return "long long int";
108 case UnsignedLongLong: return "long long unsigned int";
112 /// getTypeConstantSuffix - Return the constant suffix for the specified
113 /// integer type enum. For example, SignedLong -> "L".
114 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
116 default: llvm_unreachable("not an integer!");
118 case SignedInt: return "";
119 case SignedLong: return "L";
120 case SignedLongLong: return "LL";
122 case UnsignedInt: return "U";
123 case UnsignedLong: return "UL";
124 case UnsignedLongLong: return "ULL";
128 /// getTypeWidth - Return the width (in bits) of the specified integer type
129 /// enum. For example, SignedInt -> getIntWidth().
130 unsigned TargetInfo::getTypeWidth(IntType T) const {
132 default: llvm_unreachable("not an integer!");
134 case UnsignedShort: return getShortWidth();
136 case UnsignedInt: return getIntWidth();
138 case UnsignedLong: return getLongWidth();
140 case UnsignedLongLong: return getLongLongWidth();
144 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
145 /// enum. For example, SignedInt -> getIntAlign().
146 unsigned TargetInfo::getTypeAlign(IntType T) const {
148 default: llvm_unreachable("not an integer!");
150 case UnsignedShort: return getShortAlign();
152 case UnsignedInt: return getIntAlign();
154 case UnsignedLong: return getLongAlign();
156 case UnsignedLongLong: return getLongLongAlign();
160 /// isTypeSigned - Return whether an integer types is signed. Returns true if
161 /// the type is signed; false otherwise.
162 bool TargetInfo::isTypeSigned(IntType T) {
164 default: llvm_unreachable("not an integer!");
173 case UnsignedLongLong:
178 /// setForcedLangOptions - Set forced language options.
179 /// Apply changes to the target information with respect to certain
180 /// language options which change the target configuration.
181 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
182 if (Opts.NoBitFieldTypeAlign)
183 UseBitFieldTypeAlignment = false;
185 WCharType = UnsignedShort;
188 //===----------------------------------------------------------------------===//
191 static StringRef removeGCCRegisterPrefix(StringRef Name) {
192 if (Name[0] == '%' || Name[0] == '#')
193 Name = Name.substr(1);
198 /// isValidClobber - Returns whether the passed in string is
199 /// a valid clobber in an inline asm statement. This is used by
201 bool TargetInfo::isValidClobber(StringRef Name) const {
202 return (isValidGCCRegisterName(Name) ||
203 Name == "memory" || Name == "cc");
206 /// isValidGCCRegisterName - Returns whether the passed in string
207 /// is a valid register name according to GCC. This is used by Sema for
208 /// inline asm statements.
209 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
213 const char * const *Names;
216 // Get rid of any register prefix.
217 Name = removeGCCRegisterPrefix(Name);
219 getGCCRegNames(Names, NumNames);
221 // If we have a number it maps to an entry in the register name array.
222 if (isdigit(Name[0])) {
224 if (!Name.getAsInteger(0, n))
225 return n >= 0 && (unsigned)n < NumNames;
228 // Check register names.
229 for (unsigned i = 0; i < NumNames; i++) {
230 if (Name == Names[i])
234 // Check any additional names that we have.
235 const AddlRegName *AddlNames;
236 unsigned NumAddlNames;
237 getGCCAddlRegNames(AddlNames, NumAddlNames);
238 for (unsigned i = 0; i < NumAddlNames; i++)
239 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
240 if (!AddlNames[i].Names[j])
242 // Make sure the register that the additional name is for is within
243 // the bounds of the register names from above.
244 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
248 // Now check aliases.
249 const GCCRegAlias *Aliases;
252 getGCCRegAliases(Aliases, NumAliases);
253 for (unsigned i = 0; i < NumAliases; i++) {
254 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
255 if (!Aliases[i].Aliases[j])
257 if (Aliases[i].Aliases[j] == Name)
266 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
267 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
269 // Get rid of any register prefix.
270 Name = removeGCCRegisterPrefix(Name);
272 const char * const *Names;
275 getGCCRegNames(Names, NumNames);
277 // First, check if we have a number.
278 if (isdigit(Name[0])) {
280 if (!Name.getAsInteger(0, n)) {
281 assert(n >= 0 && (unsigned)n < NumNames &&
282 "Out of bounds register number!");
287 // Check any additional names that we have.
288 const AddlRegName *AddlNames;
289 unsigned NumAddlNames;
290 getGCCAddlRegNames(AddlNames, NumAddlNames);
291 for (unsigned i = 0; i < NumAddlNames; i++)
292 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
293 if (!AddlNames[i].Names[j])
295 // Make sure the register that the additional name is for is within
296 // the bounds of the register names from above.
297 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
301 // Now check aliases.
302 const GCCRegAlias *Aliases;
305 getGCCRegAliases(Aliases, NumAliases);
306 for (unsigned i = 0; i < NumAliases; i++) {
307 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
308 if (!Aliases[i].Aliases[j])
310 if (Aliases[i].Aliases[j] == Name)
311 return Aliases[i].Register;
318 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
319 const char *Name = Info.getConstraintStr().c_str();
320 // An output constraint must start with '=' or '+'
321 if (*Name != '=' && *Name != '+')
325 Info.setIsReadWrite();
331 if (!validateAsmConstraint(Name, Info)) {
332 // FIXME: We temporarily return false
333 // so we can add more constraints as we hit it.
334 // Eventually, an unknown constraint should just be treated as 'g'.
337 case '&': // early clobber.
339 case '%': // commutative.
340 // FIXME: Check that there is a another register after this one.
342 case 'r': // general register.
343 Info.setAllowsRegister();
345 case 'm': // memory operand.
346 case 'o': // offsetable memory operand.
347 case 'V': // non-offsetable memory operand.
348 case '<': // autodecrement memory operand.
349 case '>': // autoincrement memory operand.
350 Info.setAllowsMemory();
352 case 'g': // general register, memory operand or immediate integer.
353 case 'X': // any operand.
354 Info.setAllowsRegister();
355 Info.setAllowsMemory();
357 case ',': // multiple alternative constraint. Pass it.
358 // Handle additional optional '=' or '+' modifiers.
359 if (Name[1] == '=' || Name[1] == '+')
362 case '?': // Disparage slightly code.
363 case '!': // Disparage severely.
373 bool TargetInfo::resolveSymbolicName(const char *&Name,
374 ConstraintInfo *OutputConstraints,
376 unsigned &Index) const {
377 assert(*Name == '[' && "Symbolic name did not start with '['");
379 const char *Start = Name;
380 while (*Name && *Name != ']')
388 std::string SymbolicName(Start, Name - Start);
390 for (Index = 0; Index != NumOutputs; ++Index)
391 if (SymbolicName == OutputConstraints[Index].getName())
397 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
399 ConstraintInfo &Info) const {
400 const char *Name = Info.ConstraintStr.c_str();
405 // Check if we have a matching constraint
406 if (*Name >= '0' && *Name <= '9') {
407 unsigned i = *Name - '0';
409 // Check if matching constraint is out of bounds.
413 // A number must refer to an output only operand.
414 if (OutputConstraints[i].isReadWrite())
417 // If the constraint is already tied, it must be tied to the
418 // same operand referenced to by the number.
419 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
422 // The constraint should have the same info as the respective
423 // output constraint.
424 Info.setTiedOperand(i, OutputConstraints[i]);
425 } else if (!validateAsmConstraint(Name, Info)) {
426 // FIXME: This error return is in place temporarily so we can
427 // add more constraints as we hit it. Eventually, an unknown
428 // constraint should just be treated as 'g'.
434 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
437 // If the constraint is already tied, it must be tied to the
438 // same operand referenced to by the number.
439 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
442 Info.setTiedOperand(Index, OutputConstraints[Index]);
445 case '%': // commutative
446 // FIXME: Fail if % is used with the last operand.
448 case 'i': // immediate integer.
449 case 'n': // immediate integer with a known value.
451 case 'I': // Various constant constraints with target-specific meanings.
460 case 'r': // general register.
461 Info.setAllowsRegister();
463 case 'm': // memory operand.
464 case 'o': // offsettable memory operand.
465 case 'V': // non-offsettable memory operand.
466 case '<': // autodecrement memory operand.
467 case '>': // autoincrement memory operand.
468 Info.setAllowsMemory();
470 case 'g': // general register, memory operand or immediate integer.
471 case 'X': // any operand.
472 Info.setAllowsRegister();
473 Info.setAllowsMemory();
475 case 'E': // immediate floating point.
476 case 'F': // immediate floating point.
477 case 'p': // address operand.
479 case ',': // multiple alternative constraint. Ignore comma.
481 case '?': // Disparage slightly code.
482 case '!': // Disparage severely.