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 llvm::Triple &T) : TargetOpts(), 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;
48 LargeArrayMinWidth = 0;
50 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
52 SizeType = UnsignedLong;
53 PtrDiffType = SignedLong;
54 IntMaxType = SignedLongLong;
55 IntPtrType = SignedLong;
56 WCharType = SignedInt;
58 Char16Type = UnsignedShort;
59 Char32Type = UnsignedInt;
60 Int64Type = SignedLongLong;
61 SigAtomicType = SignedInt;
62 ProcessIDType = SignedInt;
63 UseSignedCharForObjCBool = true;
64 UseBitFieldTypeAlignment = true;
65 UseZeroLengthBitfieldAlignment = false;
66 ZeroLengthBitfieldBoundary = 0;
67 HalfFormat = &llvm::APFloat::IEEEhalf;
68 FloatFormat = &llvm::APFloat::IEEEsingle;
69 DoubleFormat = &llvm::APFloat::IEEEdouble;
70 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
71 DescriptionString = nullptr;
72 UserLabelPrefix = "_";
73 MCountName = "mcount";
76 HasAlignMac68kSupport = false;
78 // Default to no types using fpret.
79 RealTypeUsesObjCFPRet = 0;
81 // Default to not using fp2ret for __Complex long double
82 ComplexLongDoubleUsesFP2Ret = false;
84 // Set the C++ ABI based on the triple.
85 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
86 ? TargetCXXABI::Microsoft
87 : TargetCXXABI::GenericItanium);
89 // Default to an empty address space map.
90 AddrSpaceMap = &DefaultAddrSpaceMap;
91 UseAddrSpaceMapMangling = false;
93 // Default to an unknown platform name.
94 PlatformName = "unknown";
95 PlatformMinVersion = VersionTuple();
98 // Out of line virtual dtor for TargetInfo.
99 TargetInfo::~TargetInfo() {}
101 /// getTypeName - Return the user string for the specified integer type enum.
102 /// For example, SignedShort -> "short".
103 const char *TargetInfo::getTypeName(IntType T) {
105 default: llvm_unreachable("not an integer!");
106 case SignedChar: return "signed char";
107 case UnsignedChar: return "unsigned char";
108 case SignedShort: return "short";
109 case UnsignedShort: return "unsigned short";
110 case SignedInt: return "int";
111 case UnsignedInt: return "unsigned int";
112 case SignedLong: return "long int";
113 case UnsignedLong: return "long unsigned int";
114 case SignedLongLong: return "long long int";
115 case UnsignedLongLong: return "long long unsigned int";
119 /// getTypeConstantSuffix - Return the constant suffix for the specified
120 /// integer type enum. For example, SignedLong -> "L".
121 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
123 default: llvm_unreachable("not an integer!");
126 case SignedInt: return "";
127 case SignedLong: return "L";
128 case SignedLongLong: return "LL";
130 if (getCharWidth() < getIntWidth())
133 if (getShortWidth() < getIntWidth())
135 case UnsignedInt: return "U";
136 case UnsignedLong: return "UL";
137 case UnsignedLongLong: return "ULL";
141 /// getTypeFormatModifier - Return the printf format modifier for the
142 /// specified integer type enum. For example, SignedLong -> "l".
144 const char *TargetInfo::getTypeFormatModifier(IntType T) {
146 default: llvm_unreachable("not an integer!");
148 case UnsignedChar: return "hh";
150 case UnsignedShort: return "h";
152 case UnsignedInt: return "";
154 case UnsignedLong: return "l";
156 case UnsignedLongLong: return "ll";
160 /// getTypeWidth - Return the width (in bits) of the specified integer type
161 /// enum. For example, SignedInt -> getIntWidth().
162 unsigned TargetInfo::getTypeWidth(IntType T) const {
164 default: llvm_unreachable("not an integer!");
166 case UnsignedChar: return getCharWidth();
168 case UnsignedShort: return getShortWidth();
170 case UnsignedInt: return getIntWidth();
172 case UnsignedLong: return getLongWidth();
174 case UnsignedLongLong: return getLongLongWidth();
178 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
179 unsigned BitWidth, bool IsSigned) const {
180 if (getCharWidth() == BitWidth)
181 return IsSigned ? SignedChar : UnsignedChar;
182 if (getShortWidth() == BitWidth)
183 return IsSigned ? SignedShort : UnsignedShort;
184 if (getIntWidth() == BitWidth)
185 return IsSigned ? SignedInt : UnsignedInt;
186 if (getLongWidth() == BitWidth)
187 return IsSigned ? SignedLong : UnsignedLong;
188 if (getLongLongWidth() == BitWidth)
189 return IsSigned ? SignedLongLong : UnsignedLongLong;
193 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
194 bool IsSigned) const {
195 if (getCharWidth() >= BitWidth)
196 return IsSigned ? SignedChar : UnsignedChar;
197 if (getShortWidth() >= BitWidth)
198 return IsSigned ? SignedShort : UnsignedShort;
199 if (getIntWidth() >= BitWidth)
200 return IsSigned ? SignedInt : UnsignedInt;
201 if (getLongWidth() >= BitWidth)
202 return IsSigned ? SignedLong : UnsignedLong;
203 if (getLongLongWidth() >= BitWidth)
204 return IsSigned ? SignedLongLong : UnsignedLongLong;
208 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
209 if (getFloatWidth() == BitWidth)
211 if (getDoubleWidth() == BitWidth)
216 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
220 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
221 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
229 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
230 /// enum. For example, SignedInt -> getIntAlign().
231 unsigned TargetInfo::getTypeAlign(IntType T) const {
233 default: llvm_unreachable("not an integer!");
235 case UnsignedChar: return getCharAlign();
237 case UnsignedShort: return getShortAlign();
239 case UnsignedInt: return getIntAlign();
241 case UnsignedLong: return getLongAlign();
243 case UnsignedLongLong: return getLongLongAlign();
247 /// isTypeSigned - Return whether an integer types is signed. Returns true if
248 /// the type is signed; false otherwise.
249 bool TargetInfo::isTypeSigned(IntType T) {
251 default: llvm_unreachable("not an integer!");
262 case UnsignedLongLong:
267 /// adjust - Set forced language options.
268 /// Apply changes to the target information with respect to certain
269 /// language options which change the target configuration.
270 void TargetInfo::adjust(const LangOptions &Opts) {
271 if (Opts.NoBitFieldTypeAlign)
272 UseBitFieldTypeAlignment = false;
274 WCharType = UnsignedShort;
277 // OpenCL C requires specific widths for types, irrespective of
278 // what these normally are for the target.
279 // We also define long long and long double here, although the
280 // OpenCL standard only mentions these as "reserved".
281 IntWidth = IntAlign = 32;
282 LongWidth = LongAlign = 64;
283 LongLongWidth = LongLongAlign = 128;
284 HalfWidth = HalfAlign = 16;
285 FloatWidth = FloatAlign = 32;
287 // Embedded 32-bit targets (OpenCL EP) might have double C type
288 // defined as float. Let's not override this as it might lead
289 // to generating illegal code that uses 64bit doubles.
290 if (DoubleWidth != FloatWidth) {
291 DoubleWidth = DoubleAlign = 64;
292 DoubleFormat = &llvm::APFloat::IEEEdouble;
294 LongDoubleWidth = LongDoubleAlign = 128;
296 assert(PointerWidth == 32 || PointerWidth == 64);
297 bool Is32BitArch = PointerWidth == 32;
298 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
299 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
300 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
302 IntMaxType = SignedLongLong;
303 Int64Type = SignedLong;
305 HalfFormat = &llvm::APFloat::IEEEhalf;
306 FloatFormat = &llvm::APFloat::IEEEsingle;
307 LongDoubleFormat = &llvm::APFloat::IEEEquad;
311 //===----------------------------------------------------------------------===//
314 static StringRef removeGCCRegisterPrefix(StringRef Name) {
315 if (Name[0] == '%' || Name[0] == '#')
316 Name = Name.substr(1);
321 /// isValidClobber - Returns whether the passed in string is
322 /// a valid clobber in an inline asm statement. This is used by
324 bool TargetInfo::isValidClobber(StringRef Name) const {
325 return (isValidGCCRegisterName(Name) ||
326 Name == "memory" || Name == "cc");
329 /// isValidGCCRegisterName - Returns whether the passed in string
330 /// is a valid register name according to GCC. This is used by Sema for
331 /// inline asm statements.
332 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
336 const char * const *Names;
339 // Get rid of any register prefix.
340 Name = removeGCCRegisterPrefix(Name);
342 getGCCRegNames(Names, NumNames);
344 // If we have a number it maps to an entry in the register name array.
345 if (isDigit(Name[0])) {
347 if (!Name.getAsInteger(0, n))
348 return n >= 0 && (unsigned)n < NumNames;
351 // Check register names.
352 for (unsigned i = 0; i < NumNames; i++) {
353 if (Name == Names[i])
357 // Check any additional names that we have.
358 const AddlRegName *AddlNames;
359 unsigned NumAddlNames;
360 getGCCAddlRegNames(AddlNames, NumAddlNames);
361 for (unsigned i = 0; i < NumAddlNames; i++)
362 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
363 if (!AddlNames[i].Names[j])
365 // Make sure the register that the additional name is for is within
366 // the bounds of the register names from above.
367 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
371 // Now check aliases.
372 const GCCRegAlias *Aliases;
375 getGCCRegAliases(Aliases, NumAliases);
376 for (unsigned i = 0; i < NumAliases; i++) {
377 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
378 if (!Aliases[i].Aliases[j])
380 if (Aliases[i].Aliases[j] == Name)
389 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
390 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
392 // Get rid of any register prefix.
393 Name = removeGCCRegisterPrefix(Name);
395 const char * const *Names;
398 getGCCRegNames(Names, NumNames);
400 // First, check if we have a number.
401 if (isDigit(Name[0])) {
403 if (!Name.getAsInteger(0, n)) {
404 assert(n >= 0 && (unsigned)n < NumNames &&
405 "Out of bounds register number!");
410 // Check any additional names that we have.
411 const AddlRegName *AddlNames;
412 unsigned NumAddlNames;
413 getGCCAddlRegNames(AddlNames, NumAddlNames);
414 for (unsigned i = 0; i < NumAddlNames; i++)
415 for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
416 if (!AddlNames[i].Names[j])
418 // Make sure the register that the additional name is for is within
419 // the bounds of the register names from above.
420 if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
424 // Now check aliases.
425 const GCCRegAlias *Aliases;
428 getGCCRegAliases(Aliases, NumAliases);
429 for (unsigned i = 0; i < NumAliases; i++) {
430 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
431 if (!Aliases[i].Aliases[j])
433 if (Aliases[i].Aliases[j] == Name)
434 return Aliases[i].Register;
441 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
442 const char *Name = Info.getConstraintStr().c_str();
443 // An output constraint must start with '=' or '+'
444 if (*Name != '=' && *Name != '+')
448 Info.setIsReadWrite();
454 if (!validateAsmConstraint(Name, Info)) {
455 // FIXME: We temporarily return false
456 // so we can add more constraints as we hit it.
457 // Eventually, an unknown constraint should just be treated as 'g'.
460 case '&': // early clobber.
462 case '%': // commutative.
463 // FIXME: Check that there is a another register after this one.
465 case 'r': // general register.
466 Info.setAllowsRegister();
468 case 'm': // memory operand.
469 case 'o': // offsetable memory operand.
470 case 'V': // non-offsetable memory operand.
471 case '<': // autodecrement memory operand.
472 case '>': // autoincrement memory operand.
473 Info.setAllowsMemory();
475 case 'g': // general register, memory operand or immediate integer.
476 case 'X': // any operand.
477 Info.setAllowsRegister();
478 Info.setAllowsMemory();
480 case ',': // multiple alternative constraint. Pass it.
481 // Handle additional optional '=' or '+' modifiers.
482 if (Name[1] == '=' || Name[1] == '+')
485 case '?': // Disparage slightly code.
486 case '!': // Disparage severely.
487 case '#': // Ignore as constraint.
488 case '*': // Ignore for choosing register preferences.
495 // If a constraint allows neither memory nor register operands it contains
496 // only modifiers. Reject it.
497 return Info.allowsMemory() || Info.allowsRegister();
500 bool TargetInfo::resolveSymbolicName(const char *&Name,
501 ConstraintInfo *OutputConstraints,
503 unsigned &Index) const {
504 assert(*Name == '[' && "Symbolic name did not start with '['");
506 const char *Start = Name;
507 while (*Name && *Name != ']')
515 std::string SymbolicName(Start, Name - Start);
517 for (Index = 0; Index != NumOutputs; ++Index)
518 if (SymbolicName == OutputConstraints[Index].getName())
524 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
526 ConstraintInfo &Info) const {
527 const char *Name = Info.ConstraintStr.c_str();
535 // Check if we have a matching constraint
536 if (*Name >= '0' && *Name <= '9') {
537 unsigned i = *Name - '0';
539 // Check if matching constraint is out of bounds.
543 // A number must refer to an output only operand.
544 if (OutputConstraints[i].isReadWrite())
547 // If the constraint is already tied, it must be tied to the
548 // same operand referenced to by the number.
549 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
552 // The constraint should have the same info as the respective
553 // output constraint.
554 Info.setTiedOperand(i, OutputConstraints[i]);
555 } else if (!validateAsmConstraint(Name, Info)) {
556 // FIXME: This error return is in place temporarily so we can
557 // add more constraints as we hit it. Eventually, an unknown
558 // constraint should just be treated as 'g'.
564 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
567 // If the constraint is already tied, it must be tied to the
568 // same operand referenced to by the number.
569 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
572 Info.setTiedOperand(Index, OutputConstraints[Index]);
575 case '%': // commutative
576 // FIXME: Fail if % is used with the last operand.
578 case 'i': // immediate integer.
579 case 'n': // immediate integer with a known value.
581 case 'I': // Various constant constraints with target-specific meanings.
590 case 'r': // general register.
591 Info.setAllowsRegister();
593 case 'm': // memory operand.
594 case 'o': // offsettable memory operand.
595 case 'V': // non-offsettable memory operand.
596 case '<': // autodecrement memory operand.
597 case '>': // autoincrement memory operand.
598 Info.setAllowsMemory();
600 case 'g': // general register, memory operand or immediate integer.
601 case 'X': // any operand.
602 Info.setAllowsRegister();
603 Info.setAllowsMemory();
605 case 'E': // immediate floating point.
606 case 'F': // immediate floating point.
607 case 'p': // address operand.
609 case ',': // multiple alternative constraint. Ignore comma.
611 case '?': // Disparage slightly code.
612 case '!': // Disparage severely.
613 case '#': // Ignore as constraint.
614 case '*': // Ignore for choosing register preferences.
624 bool TargetCXXABI::tryParse(llvm::StringRef name) {
625 const Kind unknown = static_cast<Kind>(-1);
626 Kind kind = llvm::StringSwitch<Kind>(name)
627 .Case("arm", GenericARM)
629 .Case("itanium", GenericItanium)
630 .Case("microsoft", Microsoft)
632 if (kind == unknown) return false;