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"
21 using namespace clang;
23 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25 // TargetInfo Constructor.
26 TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
27 // Set defaults. Defaults are set for a 32-bit RISC platform, like PPC or
28 // SPARC. These should be overridden by concrete targets as needed.
30 NoAsmVariants = false;
31 PointerWidth = PointerAlign = 32;
32 BoolWidth = BoolAlign = 8;
33 IntWidth = IntAlign = 32;
34 LongWidth = LongAlign = 32;
35 LongLongWidth = LongLongAlign = 64;
42 LargeArrayMinWidth = 0;
44 SizeType = UnsignedLong;
45 PtrDiffType = SignedLong;
46 IntMaxType = SignedLongLong;
47 UIntMaxType = UnsignedLongLong;
48 IntPtrType = SignedLong;
49 WCharType = SignedInt;
51 Char16Type = UnsignedShort;
52 Char32Type = UnsignedInt;
53 Int64Type = SignedLongLong;
54 SigAtomicType = SignedInt;
55 UseBitFieldTypeAlignment = true;
56 FloatFormat = &llvm::APFloat::IEEEsingle;
57 DoubleFormat = &llvm::APFloat::IEEEdouble;
58 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
59 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
60 "i64:64:64-f32:32:32-f64:64:64-n32";
61 UserLabelPrefix = "_";
62 MCountName = "mcount";
63 HasAlignMac68kSupport = false;
65 // Default to no types using fpret.
66 RealTypeUsesObjCFPRet = 0;
68 // Default to using the Itanium ABI.
69 CXXABI = CXXABI_Itanium;
71 // Default to an empty address space map.
72 AddrSpaceMap = &DefaultAddrSpaceMap;
74 // Default to an unknown platform name.
75 PlatformName = "unknown";
76 PlatformMinVersion = VersionTuple();
79 // Out of line virtual dtor for TargetInfo.
80 TargetInfo::~TargetInfo() {}
82 /// getTypeName - Return the user string for the specified integer type enum.
83 /// For example, SignedShort -> "short".
84 const char *TargetInfo::getTypeName(IntType T) {
86 default: assert(0 && "not an integer!");
87 case SignedShort: return "short";
88 case UnsignedShort: return "unsigned short";
89 case SignedInt: return "int";
90 case UnsignedInt: return "unsigned int";
91 case SignedLong: return "long int";
92 case UnsignedLong: return "long unsigned int";
93 case SignedLongLong: return "long long int";
94 case UnsignedLongLong: return "long long unsigned int";
98 /// getTypeConstantSuffix - Return the constant suffix for the specified
99 /// integer type enum. For example, SignedLong -> "L".
100 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
102 default: assert(0 && "not an integer!");
104 case SignedInt: return "";
105 case SignedLong: return "L";
106 case SignedLongLong: return "LL";
108 case UnsignedInt: return "U";
109 case UnsignedLong: return "UL";
110 case UnsignedLongLong: return "ULL";
114 /// getTypeWidth - Return the width (in bits) of the specified integer type
115 /// enum. For example, SignedInt -> getIntWidth().
116 unsigned TargetInfo::getTypeWidth(IntType T) const {
118 default: assert(0 && "not an integer!");
120 case UnsignedShort: return getShortWidth();
122 case UnsignedInt: return getIntWidth();
124 case UnsignedLong: return getLongWidth();
126 case UnsignedLongLong: return getLongLongWidth();
130 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
131 /// enum. For example, SignedInt -> getIntAlign().
132 unsigned TargetInfo::getTypeAlign(IntType T) const {
134 default: assert(0 && "not an integer!");
136 case UnsignedShort: return getShortAlign();
138 case UnsignedInt: return getIntAlign();
140 case UnsignedLong: return getLongAlign();
142 case UnsignedLongLong: return getLongLongAlign();
146 /// isTypeSigned - Return whether an integer types is signed. Returns true if
147 /// the type is signed; false otherwise.
148 bool TargetInfo::isTypeSigned(IntType T) {
150 default: assert(0 && "not an integer!");
159 case UnsignedLongLong:
164 /// setForcedLangOptions - Set forced language options.
165 /// Apply changes to the target information with respect to certain
166 /// language options which change the target configuration.
167 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
168 if (Opts.NoBitFieldTypeAlign)
169 UseBitFieldTypeAlignment = false;
171 WCharType = UnsignedShort;
174 //===----------------------------------------------------------------------===//
177 static llvm::StringRef removeGCCRegisterPrefix(llvm::StringRef Name) {
178 if (Name[0] == '%' || Name[0] == '#')
179 Name = Name.substr(1);
184 /// isValidClobber - Returns whether the passed in string is
185 /// a valid clobber in an inline asm statement. This is used by
187 bool TargetInfo::isValidClobber(llvm::StringRef Name) const {
188 return (isValidGCCRegisterName(Name) ||
189 Name == "memory" || Name == "cc");
192 /// isValidGCCRegisterName - Returns whether the passed in string
193 /// is a valid register name according to GCC. This is used by Sema for
194 /// inline asm statements.
195 bool TargetInfo::isValidGCCRegisterName(llvm::StringRef Name) const {
199 const char * const *Names;
202 // Get rid of any register prefix.
203 Name = removeGCCRegisterPrefix(Name);
205 getGCCRegNames(Names, NumNames);
207 // If we have a number it maps to an entry in the register name array.
208 if (isdigit(Name[0])) {
210 if (!Name.getAsInteger(0, n))
211 return n >= 0 && (unsigned)n < NumNames;
214 // Check register names.
215 for (unsigned i = 0; i < NumNames; i++) {
216 if (Name == Names[i])
220 // Check any additional names that we have.
221 const AddlRegName *AddlNames;
222 unsigned NumAddlNames;
223 getGCCAddlRegNames(AddlNames, NumAddlNames);
224 for (unsigned i = 0; i < NumAddlNames; i++)
225 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
226 if (!AddlNames[i].Names[j])
228 // Make sure the register that the additional name is for is within
229 // the bounds of the register names from above.
230 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
234 // Now check aliases.
235 const GCCRegAlias *Aliases;
238 getGCCRegAliases(Aliases, NumAliases);
239 for (unsigned i = 0; i < NumAliases; i++) {
240 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
241 if (!Aliases[i].Aliases[j])
243 if (Aliases[i].Aliases[j] == Name)
252 TargetInfo::getNormalizedGCCRegisterName(llvm::StringRef Name) const {
253 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
255 // Get rid of any register prefix.
256 Name = removeGCCRegisterPrefix(Name);
258 const char * const *Names;
261 getGCCRegNames(Names, NumNames);
263 // First, check if we have a number.
264 if (isdigit(Name[0])) {
266 if (!Name.getAsInteger(0, n)) {
267 assert(n >= 0 && (unsigned)n < NumNames &&
268 "Out of bounds register number!");
273 // Check any additional names that we have.
274 const AddlRegName *AddlNames;
275 unsigned NumAddlNames;
276 getGCCAddlRegNames(AddlNames, NumAddlNames);
277 for (unsigned i = 0; i < NumAddlNames; i++)
278 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
279 if (!AddlNames[i].Names[j])
281 // Make sure the register that the additional name is for is within
282 // the bounds of the register names from above.
283 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
287 // Now check aliases.
288 const GCCRegAlias *Aliases;
291 getGCCRegAliases(Aliases, NumAliases);
292 for (unsigned i = 0; i < NumAliases; i++) {
293 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
294 if (!Aliases[i].Aliases[j])
296 if (Aliases[i].Aliases[j] == Name)
297 return Aliases[i].Register;
304 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
305 const char *Name = Info.getConstraintStr().c_str();
306 // An output constraint must start with '=' or '+'
307 if (*Name != '=' && *Name != '+')
311 Info.setIsReadWrite();
317 if (!validateAsmConstraint(Name, Info)) {
318 // FIXME: We temporarily return false
319 // so we can add more constraints as we hit it.
320 // Eventually, an unknown constraint should just be treated as 'g'.
323 case '&': // early clobber.
325 case '%': // commutative.
326 // FIXME: Check that there is a another register after this one.
328 case 'r': // general register.
329 Info.setAllowsRegister();
331 case 'm': // memory operand.
332 case 'o': // offsetable memory operand.
333 case 'V': // non-offsetable memory operand.
334 case '<': // autodecrement memory operand.
335 case '>': // autoincrement memory operand.
336 Info.setAllowsMemory();
338 case 'g': // general register, memory operand or immediate integer.
339 case 'X': // any operand.
340 Info.setAllowsRegister();
341 Info.setAllowsMemory();
343 case ',': // multiple alternative constraint. Pass it.
344 // Handle additional optional '=' or '+' modifiers.
345 if (Name[1] == '=' || Name[1] == '+')
348 case '?': // Disparage slightly code.
349 case '!': // Disparage severely.
359 bool TargetInfo::resolveSymbolicName(const char *&Name,
360 ConstraintInfo *OutputConstraints,
362 unsigned &Index) const {
363 assert(*Name == '[' && "Symbolic name did not start with '['");
365 const char *Start = Name;
366 while (*Name && *Name != ']')
374 std::string SymbolicName(Start, Name - Start);
376 for (Index = 0; Index != NumOutputs; ++Index)
377 if (SymbolicName == OutputConstraints[Index].getName())
383 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
385 ConstraintInfo &Info) const {
386 const char *Name = Info.ConstraintStr.c_str();
391 // Check if we have a matching constraint
392 if (*Name >= '0' && *Name <= '9') {
393 unsigned i = *Name - '0';
395 // Check if matching constraint is out of bounds.
399 // A number must refer to an output only operand.
400 if (OutputConstraints[i].isReadWrite())
403 // If the constraint is already tied, it must be tied to the
404 // same operand referenced to by the number.
405 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
408 // The constraint should have the same info as the respective
409 // output constraint.
410 Info.setTiedOperand(i, OutputConstraints[i]);
411 } else if (!validateAsmConstraint(Name, Info)) {
412 // FIXME: This error return is in place temporarily so we can
413 // add more constraints as we hit it. Eventually, an unknown
414 // constraint should just be treated as 'g'.
420 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
423 // If the constraint is already tied, it must be tied to the
424 // same operand referenced to by the number.
425 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
428 Info.setTiedOperand(Index, OutputConstraints[Index]);
431 case '%': // commutative
432 // FIXME: Fail if % is used with the last operand.
434 case 'i': // immediate integer.
435 case 'n': // immediate integer with a known value.
437 case 'I': // Various constant constraints with target-specific meanings.
446 case 'r': // general register.
447 Info.setAllowsRegister();
449 case 'm': // memory operand.
450 case 'o': // offsettable memory operand.
451 case 'V': // non-offsettable memory operand.
452 case '<': // autodecrement memory operand.
453 case '>': // autoincrement memory operand.
454 Info.setAllowsMemory();
456 case 'g': // general register, memory operand or immediate integer.
457 case 'X': // any operand.
458 Info.setAllowsRegister();
459 Info.setAllowsMemory();
461 case 'E': // immediate floating point.
462 case 'F': // immediate floating point.
463 case 'p': // address operand.
465 case ',': // multiple alternative constraint. Ignore comma.
467 case '?': // Disparage slightly code.
468 case '!': // Disparage severely.