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;
48 LargeArrayMinWidth = 0;
50 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
52 SizeType = UnsignedLong;
53 PtrDiffType = SignedLong;
54 IntMaxType = SignedLongLong;
55 UIntMaxType = UnsignedLongLong;
56 IntPtrType = SignedLong;
57 WCharType = SignedInt;
59 Char16Type = UnsignedShort;
60 Char32Type = UnsignedInt;
61 Int64Type = SignedLongLong;
62 SigAtomicType = SignedInt;
63 ProcessIDType = SignedInt;
64 UseSignedCharForObjCBool = true;
65 UseBitFieldTypeAlignment = true;
66 UseZeroLengthBitfieldAlignment = false;
67 ZeroLengthBitfieldBoundary = 0;
68 HalfFormat = &llvm::APFloat::IEEEhalf;
69 FloatFormat = &llvm::APFloat::IEEEsingle;
70 DoubleFormat = &llvm::APFloat::IEEEdouble;
71 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
72 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
73 "i64:64:64-f32:32:32-f64:64:64-n32";
74 UserLabelPrefix = "_";
75 MCountName = "mcount";
78 HasAlignMac68kSupport = false;
80 // Default to no types using fpret.
81 RealTypeUsesObjCFPRet = 0;
83 // Default to not using fp2ret for __Complex long double
84 ComplexLongDoubleUsesFP2Ret = false;
86 // Default to using the Itanium ABI.
87 TheCXXABI.set(TargetCXXABI::GenericItanium);
89 // Default to an empty address space map.
90 AddrSpaceMap = &DefaultAddrSpaceMap;
92 // Default to an unknown platform name.
93 PlatformName = "unknown";
94 PlatformMinVersion = VersionTuple();
97 // Out of line virtual dtor for TargetInfo.
98 TargetInfo::~TargetInfo() {}
100 /// getTypeName - Return the user string for the specified integer type enum.
101 /// For example, SignedShort -> "short".
102 const char *TargetInfo::getTypeName(IntType T) {
104 default: llvm_unreachable("not an integer!");
105 case SignedShort: return "short";
106 case UnsignedShort: return "unsigned short";
107 case SignedInt: return "int";
108 case UnsignedInt: return "unsigned int";
109 case SignedLong: return "long int";
110 case UnsignedLong: return "long unsigned int";
111 case SignedLongLong: return "long long int";
112 case UnsignedLongLong: return "long long unsigned int";
116 /// getTypeConstantSuffix - Return the constant suffix for the specified
117 /// integer type enum. For example, SignedLong -> "L".
118 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
120 default: llvm_unreachable("not an integer!");
122 case SignedInt: return "";
123 case SignedLong: return "L";
124 case SignedLongLong: return "LL";
126 case UnsignedInt: return "U";
127 case UnsignedLong: return "UL";
128 case UnsignedLongLong: return "ULL";
132 /// getTypeWidth - Return the width (in bits) of the specified integer type
133 /// enum. For example, SignedInt -> getIntWidth().
134 unsigned TargetInfo::getTypeWidth(IntType T) const {
136 default: llvm_unreachable("not an integer!");
138 case UnsignedShort: return getShortWidth();
140 case UnsignedInt: return getIntWidth();
142 case UnsignedLong: return getLongWidth();
144 case UnsignedLongLong: return getLongLongWidth();
148 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
149 /// enum. For example, SignedInt -> getIntAlign().
150 unsigned TargetInfo::getTypeAlign(IntType T) const {
152 default: llvm_unreachable("not an integer!");
154 case UnsignedShort: return getShortAlign();
156 case UnsignedInt: return getIntAlign();
158 case UnsignedLong: return getLongAlign();
160 case UnsignedLongLong: return getLongLongAlign();
164 /// isTypeSigned - Return whether an integer types is signed. Returns true if
165 /// the type is signed; false otherwise.
166 bool TargetInfo::isTypeSigned(IntType T) {
168 default: llvm_unreachable("not an integer!");
177 case UnsignedLongLong:
182 /// setForcedLangOptions - Set forced language options.
183 /// Apply changes to the target information with respect to certain
184 /// language options which change the target configuration.
185 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
186 if (Opts.NoBitFieldTypeAlign)
187 UseBitFieldTypeAlignment = false;
189 WCharType = UnsignedShort;
192 //===----------------------------------------------------------------------===//
195 static StringRef removeGCCRegisterPrefix(StringRef Name) {
196 if (Name[0] == '%' || Name[0] == '#')
197 Name = Name.substr(1);
202 /// isValidClobber - Returns whether the passed in string is
203 /// a valid clobber in an inline asm statement. This is used by
205 bool TargetInfo::isValidClobber(StringRef Name) const {
206 return (isValidGCCRegisterName(Name) ||
207 Name == "memory" || Name == "cc");
210 /// isValidGCCRegisterName - Returns whether the passed in string
211 /// is a valid register name according to GCC. This is used by Sema for
212 /// inline asm statements.
213 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
217 const char * const *Names;
220 // Get rid of any register prefix.
221 Name = removeGCCRegisterPrefix(Name);
223 getGCCRegNames(Names, NumNames);
225 // If we have a number it maps to an entry in the register name array.
226 if (isDigit(Name[0])) {
228 if (!Name.getAsInteger(0, n))
229 return n >= 0 && (unsigned)n < NumNames;
232 // Check register names.
233 for (unsigned i = 0; i < NumNames; i++) {
234 if (Name == Names[i])
238 // Check any additional names that we have.
239 const AddlRegName *AddlNames;
240 unsigned NumAddlNames;
241 getGCCAddlRegNames(AddlNames, NumAddlNames);
242 for (unsigned i = 0; i < NumAddlNames; i++)
243 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
244 if (!AddlNames[i].Names[j])
246 // Make sure the register that the additional name is for is within
247 // the bounds of the register names from above.
248 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
252 // Now check aliases.
253 const GCCRegAlias *Aliases;
256 getGCCRegAliases(Aliases, NumAliases);
257 for (unsigned i = 0; i < NumAliases; i++) {
258 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
259 if (!Aliases[i].Aliases[j])
261 if (Aliases[i].Aliases[j] == Name)
270 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
271 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
273 // Get rid of any register prefix.
274 Name = removeGCCRegisterPrefix(Name);
276 const char * const *Names;
279 getGCCRegNames(Names, NumNames);
281 // First, check if we have a number.
282 if (isDigit(Name[0])) {
284 if (!Name.getAsInteger(0, n)) {
285 assert(n >= 0 && (unsigned)n < NumNames &&
286 "Out of bounds register number!");
291 // Check any additional names that we have.
292 const AddlRegName *AddlNames;
293 unsigned NumAddlNames;
294 getGCCAddlRegNames(AddlNames, NumAddlNames);
295 for (unsigned i = 0; i < NumAddlNames; i++)
296 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
297 if (!AddlNames[i].Names[j])
299 // Make sure the register that the additional name is for is within
300 // the bounds of the register names from above.
301 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
305 // Now check aliases.
306 const GCCRegAlias *Aliases;
309 getGCCRegAliases(Aliases, NumAliases);
310 for (unsigned i = 0; i < NumAliases; i++) {
311 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
312 if (!Aliases[i].Aliases[j])
314 if (Aliases[i].Aliases[j] == Name)
315 return Aliases[i].Register;
322 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
323 const char *Name = Info.getConstraintStr().c_str();
324 // An output constraint must start with '=' or '+'
325 if (*Name != '=' && *Name != '+')
329 Info.setIsReadWrite();
335 if (!validateAsmConstraint(Name, Info)) {
336 // FIXME: We temporarily return false
337 // so we can add more constraints as we hit it.
338 // Eventually, an unknown constraint should just be treated as 'g'.
341 case '&': // early clobber.
343 case '%': // commutative.
344 // FIXME: Check that there is a another register after this one.
346 case 'r': // general register.
347 Info.setAllowsRegister();
349 case 'm': // memory operand.
350 case 'o': // offsetable memory operand.
351 case 'V': // non-offsetable memory operand.
352 case '<': // autodecrement memory operand.
353 case '>': // autoincrement memory operand.
354 Info.setAllowsMemory();
356 case 'g': // general register, memory operand or immediate integer.
357 case 'X': // any operand.
358 Info.setAllowsRegister();
359 Info.setAllowsMemory();
361 case ',': // multiple alternative constraint. Pass it.
362 // Handle additional optional '=' or '+' modifiers.
363 if (Name[1] == '=' || Name[1] == '+')
366 case '?': // Disparage slightly code.
367 case '!': // Disparage severely.
368 case '#': // Ignore as constraint.
369 case '*': // Ignore for choosing register preferences.
379 bool TargetInfo::resolveSymbolicName(const char *&Name,
380 ConstraintInfo *OutputConstraints,
382 unsigned &Index) const {
383 assert(*Name == '[' && "Symbolic name did not start with '['");
385 const char *Start = Name;
386 while (*Name && *Name != ']')
394 std::string SymbolicName(Start, Name - Start);
396 for (Index = 0; Index != NumOutputs; ++Index)
397 if (SymbolicName == OutputConstraints[Index].getName())
403 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
405 ConstraintInfo &Info) const {
406 const char *Name = Info.ConstraintStr.c_str();
411 // Check if we have a matching constraint
412 if (*Name >= '0' && *Name <= '9') {
413 unsigned i = *Name - '0';
415 // Check if matching constraint is out of bounds.
419 // A number must refer to an output only operand.
420 if (OutputConstraints[i].isReadWrite())
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() != i)
428 // The constraint should have the same info as the respective
429 // output constraint.
430 Info.setTiedOperand(i, OutputConstraints[i]);
431 } else if (!validateAsmConstraint(Name, Info)) {
432 // FIXME: This error return is in place temporarily so we can
433 // add more constraints as we hit it. Eventually, an unknown
434 // constraint should just be treated as 'g'.
440 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
443 // If the constraint is already tied, it must be tied to the
444 // same operand referenced to by the number.
445 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
448 Info.setTiedOperand(Index, OutputConstraints[Index]);
451 case '%': // commutative
452 // FIXME: Fail if % is used with the last operand.
454 case 'i': // immediate integer.
455 case 'n': // immediate integer with a known value.
457 case 'I': // Various constant constraints with target-specific meanings.
466 case 'r': // general register.
467 Info.setAllowsRegister();
469 case 'm': // memory operand.
470 case 'o': // offsettable memory operand.
471 case 'V': // non-offsettable memory operand.
472 case '<': // autodecrement memory operand.
473 case '>': // autoincrement memory operand.
474 Info.setAllowsMemory();
476 case 'g': // general register, memory operand or immediate integer.
477 case 'X': // any operand.
478 Info.setAllowsRegister();
479 Info.setAllowsMemory();
481 case 'E': // immediate floating point.
482 case 'F': // immediate floating point.
483 case 'p': // address operand.
485 case ',': // multiple alternative constraint. Ignore comma.
487 case '?': // Disparage slightly code.
488 case '!': // Disparage severely.
489 case '#': // Ignore as constraint.
490 case '*': // Ignore for choosing register preferences.
500 bool TargetCXXABI::tryParse(llvm::StringRef name) {
501 const Kind unknown = static_cast<Kind>(-1);
502 Kind kind = llvm::StringSwitch<Kind>(name)
503 .Case("arm", GenericARM)
505 .Case("itanium", GenericItanium)
506 .Case("microsoft", Microsoft)
508 if (kind == unknown) return false;