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;
39 DefaultAlignForAttributeAligned = 128;
49 LargeArrayMinWidth = 0;
51 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
55 SizeType = UnsignedLong;
56 PtrDiffType = SignedLong;
57 IntMaxType = SignedLongLong;
58 IntPtrType = SignedLong;
59 WCharType = SignedInt;
61 Char16Type = UnsignedShort;
62 Char32Type = UnsignedInt;
63 Int64Type = SignedLongLong;
64 SigAtomicType = SignedInt;
65 ProcessIDType = SignedInt;
66 UseSignedCharForObjCBool = true;
67 UseBitFieldTypeAlignment = true;
68 UseZeroLengthBitfieldAlignment = false;
69 ZeroLengthBitfieldBoundary = 0;
70 HalfFormat = &llvm::APFloat::IEEEhalf;
71 FloatFormat = &llvm::APFloat::IEEEsingle;
72 DoubleFormat = &llvm::APFloat::IEEEdouble;
73 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
74 DataLayoutString = nullptr;
75 UserLabelPrefix = "_";
76 MCountName = "mcount";
79 HasAlignMac68kSupport = false;
80 HasBuiltinMSVaList = false;
82 // Default to no types using fpret.
83 RealTypeUsesObjCFPRet = 0;
85 // Default to not using fp2ret for __Complex long double
86 ComplexLongDoubleUsesFP2Ret = false;
88 // Set the C++ ABI based on the triple.
89 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
90 ? TargetCXXABI::Microsoft
91 : TargetCXXABI::GenericItanium);
93 // Default to an empty address space map.
94 AddrSpaceMap = &DefaultAddrSpaceMap;
95 UseAddrSpaceMapMangling = false;
97 // Default to an unknown platform name.
98 PlatformName = "unknown";
99 PlatformMinVersion = VersionTuple();
102 // Out of line virtual dtor for TargetInfo.
103 TargetInfo::~TargetInfo() {}
105 /// getTypeName - Return the user string for the specified integer type enum.
106 /// For example, SignedShort -> "short".
107 const char *TargetInfo::getTypeName(IntType T) {
109 default: llvm_unreachable("not an integer!");
110 case SignedChar: return "signed char";
111 case UnsignedChar: return "unsigned char";
112 case SignedShort: return "short";
113 case UnsignedShort: return "unsigned short";
114 case SignedInt: return "int";
115 case UnsignedInt: return "unsigned int";
116 case SignedLong: return "long int";
117 case UnsignedLong: return "long unsigned int";
118 case SignedLongLong: return "long long int";
119 case UnsignedLongLong: return "long long unsigned int";
123 /// getTypeConstantSuffix - Return the constant suffix for the specified
124 /// integer type enum. For example, SignedLong -> "L".
125 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
127 default: llvm_unreachable("not an integer!");
130 case SignedInt: return "";
131 case SignedLong: return "L";
132 case SignedLongLong: return "LL";
134 if (getCharWidth() < getIntWidth())
137 if (getShortWidth() < getIntWidth())
139 case UnsignedInt: return "U";
140 case UnsignedLong: return "UL";
141 case UnsignedLongLong: return "ULL";
145 /// getTypeFormatModifier - Return the printf format modifier for the
146 /// specified integer type enum. For example, SignedLong -> "l".
148 const char *TargetInfo::getTypeFormatModifier(IntType T) {
150 default: llvm_unreachable("not an integer!");
152 case UnsignedChar: return "hh";
154 case UnsignedShort: return "h";
156 case UnsignedInt: return "";
158 case UnsignedLong: return "l";
160 case UnsignedLongLong: return "ll";
164 /// getTypeWidth - Return the width (in bits) of the specified integer type
165 /// enum. For example, SignedInt -> getIntWidth().
166 unsigned TargetInfo::getTypeWidth(IntType T) const {
168 default: llvm_unreachable("not an integer!");
170 case UnsignedChar: return getCharWidth();
172 case UnsignedShort: return getShortWidth();
174 case UnsignedInt: return getIntWidth();
176 case UnsignedLong: return getLongWidth();
178 case UnsignedLongLong: return getLongLongWidth();
182 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
183 unsigned BitWidth, bool IsSigned) const {
184 if (getCharWidth() == BitWidth)
185 return IsSigned ? SignedChar : UnsignedChar;
186 if (getShortWidth() == BitWidth)
187 return IsSigned ? SignedShort : UnsignedShort;
188 if (getIntWidth() == BitWidth)
189 return IsSigned ? SignedInt : UnsignedInt;
190 if (getLongWidth() == BitWidth)
191 return IsSigned ? SignedLong : UnsignedLong;
192 if (getLongLongWidth() == BitWidth)
193 return IsSigned ? SignedLongLong : UnsignedLongLong;
197 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
198 bool IsSigned) const {
199 if (getCharWidth() >= BitWidth)
200 return IsSigned ? SignedChar : UnsignedChar;
201 if (getShortWidth() >= BitWidth)
202 return IsSigned ? SignedShort : UnsignedShort;
203 if (getIntWidth() >= BitWidth)
204 return IsSigned ? SignedInt : UnsignedInt;
205 if (getLongWidth() >= BitWidth)
206 return IsSigned ? SignedLong : UnsignedLong;
207 if (getLongLongWidth() >= BitWidth)
208 return IsSigned ? SignedLongLong : UnsignedLongLong;
212 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
213 if (getFloatWidth() == BitWidth)
215 if (getDoubleWidth() == BitWidth)
220 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
224 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
225 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
233 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
234 /// enum. For example, SignedInt -> getIntAlign().
235 unsigned TargetInfo::getTypeAlign(IntType T) const {
237 default: llvm_unreachable("not an integer!");
239 case UnsignedChar: return getCharAlign();
241 case UnsignedShort: return getShortAlign();
243 case UnsignedInt: return getIntAlign();
245 case UnsignedLong: return getLongAlign();
247 case UnsignedLongLong: return getLongLongAlign();
251 /// isTypeSigned - Return whether an integer types is signed. Returns true if
252 /// the type is signed; false otherwise.
253 bool TargetInfo::isTypeSigned(IntType T) {
255 default: llvm_unreachable("not an integer!");
266 case UnsignedLongLong:
271 /// adjust - Set forced language options.
272 /// Apply changes to the target information with respect to certain
273 /// language options which change the target configuration.
274 void TargetInfo::adjust(const LangOptions &Opts) {
275 if (Opts.NoBitFieldTypeAlign)
276 UseBitFieldTypeAlignment = false;
278 WCharType = UnsignedShort;
281 // OpenCL C requires specific widths for types, irrespective of
282 // what these normally are for the target.
283 // We also define long long and long double here, although the
284 // OpenCL standard only mentions these as "reserved".
285 IntWidth = IntAlign = 32;
286 LongWidth = LongAlign = 64;
287 LongLongWidth = LongLongAlign = 128;
288 HalfWidth = HalfAlign = 16;
289 FloatWidth = FloatAlign = 32;
291 // Embedded 32-bit targets (OpenCL EP) might have double C type
292 // defined as float. Let's not override this as it might lead
293 // to generating illegal code that uses 64bit doubles.
294 if (DoubleWidth != FloatWidth) {
295 DoubleWidth = DoubleAlign = 64;
296 DoubleFormat = &llvm::APFloat::IEEEdouble;
298 LongDoubleWidth = LongDoubleAlign = 128;
300 assert(PointerWidth == 32 || PointerWidth == 64);
301 bool Is32BitArch = PointerWidth == 32;
302 SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
303 PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
304 IntPtrType = Is32BitArch ? SignedInt : SignedLong;
306 IntMaxType = SignedLongLong;
307 Int64Type = SignedLong;
309 HalfFormat = &llvm::APFloat::IEEEhalf;
310 FloatFormat = &llvm::APFloat::IEEEsingle;
311 LongDoubleFormat = &llvm::APFloat::IEEEquad;
315 bool TargetInfo::initFeatureMap(
316 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
317 const std::vector<std::string> &FeatureVec) const {
318 for (const auto &F : FeatureVec) {
320 // Apply the feature via the target.
321 bool Enabled = Name[0] == '+';
322 setFeatureEnabled(Features, Name.substr(1), Enabled);
327 //===----------------------------------------------------------------------===//
330 static StringRef removeGCCRegisterPrefix(StringRef Name) {
331 if (Name[0] == '%' || Name[0] == '#')
332 Name = Name.substr(1);
337 /// isValidClobber - Returns whether the passed in string is
338 /// a valid clobber in an inline asm statement. This is used by
340 bool TargetInfo::isValidClobber(StringRef Name) const {
341 return (isValidGCCRegisterName(Name) ||
342 Name == "memory" || Name == "cc");
345 /// isValidGCCRegisterName - Returns whether the passed in string
346 /// is a valid register name according to GCC. This is used by Sema for
347 /// inline asm statements.
348 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
352 // Get rid of any register prefix.
353 Name = removeGCCRegisterPrefix(Name);
357 ArrayRef<const char *> Names = getGCCRegNames();
359 // If we have a number it maps to an entry in the register name array.
360 if (isDigit(Name[0])) {
362 if (!Name.getAsInteger(0, n))
363 return n < Names.size();
366 // Check register names.
367 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
370 // Check any additional names that we have.
371 for (const AddlRegName &ARN : getGCCAddlRegNames())
372 for (const char *AN : ARN.Names) {
375 // Make sure the register that the additional name is for is within
376 // the bounds of the register names from above.
377 if (AN == Name && ARN.RegNum < Names.size())
381 // Now check aliases.
382 for (const GCCRegAlias &GRA : getGCCRegAliases())
383 for (const char *A : GRA.Aliases) {
394 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
395 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
397 // Get rid of any register prefix.
398 Name = removeGCCRegisterPrefix(Name);
400 ArrayRef<const char *> Names = getGCCRegNames();
402 // First, check if we have a number.
403 if (isDigit(Name[0])) {
405 if (!Name.getAsInteger(0, n)) {
406 assert(n < Names.size() && "Out of bounds register number!");
411 // Check any additional names that we have.
412 for (const AddlRegName &ARN : getGCCAddlRegNames())
413 for (const char *AN : ARN.Names) {
416 // Make sure the register that the additional name is for is within
417 // the bounds of the register names from above.
418 if (AN == Name && ARN.RegNum < Names.size())
422 // Now check aliases.
423 for (const GCCRegAlias &RA : getGCCRegAliases())
424 for (const char *A : RA.Aliases) {
434 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
435 const char *Name = Info.getConstraintStr().c_str();
436 // An output constraint must start with '=' or '+'
437 if (*Name != '=' && *Name != '+')
441 Info.setIsReadWrite();
447 if (!validateAsmConstraint(Name, Info)) {
448 // FIXME: We temporarily return false
449 // so we can add more constraints as we hit it.
450 // Eventually, an unknown constraint should just be treated as 'g'.
454 case '&': // early clobber.
455 Info.setEarlyClobber();
457 case '%': // commutative.
458 // FIXME: Check that there is a another register after this one.
460 case 'r': // general register.
461 Info.setAllowsRegister();
463 case 'm': // memory operand.
464 case 'o': // offsetable memory operand.
465 case 'V': // non-offsetable 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 ',': // multiple alternative constraint. Pass it.
476 // Handle additional optional '=' or '+' modifiers.
477 if (Name[1] == '=' || Name[1] == '+')
480 case '#': // Ignore as constraint.
481 while (Name[1] && Name[1] != ',')
484 case '?': // Disparage slightly code.
485 case '!': // Disparage severely.
486 case '*': // Ignore for choosing register preferences.
493 // Early clobber with a read-write constraint which doesn't permit registers
495 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
498 // If a constraint allows neither memory nor register operands it contains
499 // only modifiers. Reject it.
500 return Info.allowsMemory() || Info.allowsRegister();
503 bool TargetInfo::resolveSymbolicName(const char *&Name,
504 ArrayRef<ConstraintInfo> OutputConstraints,
505 unsigned &Index) const {
506 assert(*Name == '[' && "Symbolic name did not start with '['");
508 const char *Start = Name;
509 while (*Name && *Name != ']')
517 std::string SymbolicName(Start, Name - Start);
519 for (Index = 0; Index != OutputConstraints.size(); ++Index)
520 if (SymbolicName == OutputConstraints[Index].getName())
526 bool TargetInfo::validateInputConstraint(
527 MutableArrayRef<ConstraintInfo> OutputConstraints,
528 ConstraintInfo &Info) const {
529 const char *Name = Info.ConstraintStr.c_str();
537 // Check if we have a matching constraint
538 if (*Name >= '0' && *Name <= '9') {
539 const char *DigitStart = Name;
540 while (Name[1] >= '0' && Name[1] <= '9')
542 const char *DigitEnd = Name;
544 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
545 .getAsInteger(10, i))
548 // Check if matching constraint is out of bounds.
549 if (i >= OutputConstraints.size()) return false;
551 // A number must refer to an output only operand.
552 if (OutputConstraints[i].isReadWrite())
555 // If the constraint is already tied, it must be tied to the
556 // same operand referenced to by the number.
557 if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
560 // The constraint should have the same info as the respective
561 // output constraint.
562 Info.setTiedOperand(i, OutputConstraints[i]);
563 } else if (!validateAsmConstraint(Name, Info)) {
564 // FIXME: This error return is in place temporarily so we can
565 // add more constraints as we hit it. Eventually, an unknown
566 // constraint should just be treated as 'g'.
572 if (!resolveSymbolicName(Name, OutputConstraints, Index))
575 // If the constraint is already tied, it must be tied to the
576 // same operand referenced to by the number.
577 if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
580 // A number must refer to an output only operand.
581 if (OutputConstraints[Index].isReadWrite())
584 Info.setTiedOperand(Index, OutputConstraints[Index]);
587 case '%': // commutative
588 // FIXME: Fail if % is used with the last operand.
590 case 'i': // immediate integer.
591 case 'n': // immediate integer with a known value.
593 case 'I': // Various constant constraints with target-specific meanings.
601 if (!validateAsmConstraint(Name, Info))
604 case 'r': // general register.
605 Info.setAllowsRegister();
607 case 'm': // memory operand.
608 case 'o': // offsettable memory operand.
609 case 'V': // non-offsettable memory operand.
610 case '<': // autodecrement memory operand.
611 case '>': // autoincrement memory operand.
612 Info.setAllowsMemory();
614 case 'g': // general register, memory operand or immediate integer.
615 case 'X': // any operand.
616 Info.setAllowsRegister();
617 Info.setAllowsMemory();
619 case 'E': // immediate floating point.
620 case 'F': // immediate floating point.
621 case 'p': // address operand.
623 case ',': // multiple alternative constraint. Ignore comma.
625 case '#': // Ignore as constraint.
626 while (Name[1] && Name[1] != ',')
629 case '?': // Disparage slightly code.
630 case '!': // Disparage severely.
631 case '*': // Ignore for choosing register preferences.