1 //===--- TargetInfo.cpp - Information about Target machine ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TargetInfo and TargetInfoImpl interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
21 #include <cstdlib>
22 using namespace clang;
23
24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
26 // TargetInfo Constructor.
TargetInfo(const llvm::Triple & T)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.
30 BigEndian = true;
31 TLSSupported = true;
32 NoAsmVariants = false;
33 PointerWidth = PointerAlign = 32;
34 BoolWidth = BoolAlign = 8;
35 IntWidth = IntAlign = 32;
36 LongWidth = LongAlign = 32;
37 LongLongWidth = LongLongAlign = 64;
38 SuitableAlign = 64;
39 DefaultAlignForAttributeAligned = 128;
40 MinGlobalAlign = 0;
41 HalfWidth = 16;
42 HalfAlign = 16;
43 FloatWidth = 32;
44 FloatAlign = 32;
45 DoubleWidth = 64;
46 DoubleAlign = 64;
47 LongDoubleWidth = 64;
48 LongDoubleAlign = 64;
49 LargeArrayMinWidth = 0;
50 LargeArrayAlign = 0;
51 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
52 MaxVectorAlign = 0;
53 MaxTLSAlign = 0;
54 SimdDefaultAlign = 0;
55 SizeType = UnsignedLong;
56 PtrDiffType = SignedLong;
57 IntMaxType = SignedLongLong;
58 IntPtrType = SignedLong;
59 WCharType = SignedInt;
60 WIntType = 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";
77 RegParmMax = 0;
78 SSERegParmMax = 0;
79 HasAlignMac68kSupport = false;
80 HasBuiltinMSVaList = false;
81
82 // Default to no types using fpret.
83 RealTypeUsesObjCFPRet = 0;
84
85 // Default to not using fp2ret for __Complex long double
86 ComplexLongDoubleUsesFP2Ret = false;
87
88 // Set the C++ ABI based on the triple.
89 TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
90 ? TargetCXXABI::Microsoft
91 : TargetCXXABI::GenericItanium);
92
93 // Default to an empty address space map.
94 AddrSpaceMap = &DefaultAddrSpaceMap;
95 UseAddrSpaceMapMangling = false;
96
97 // Default to an unknown platform name.
98 PlatformName = "unknown";
99 PlatformMinVersion = VersionTuple();
100 }
101
102 // Out of line virtual dtor for TargetInfo.
~TargetInfo()103 TargetInfo::~TargetInfo() {}
104
105 /// getTypeName - Return the user string for the specified integer type enum.
106 /// For example, SignedShort -> "short".
getTypeName(IntType T)107 const char *TargetInfo::getTypeName(IntType T) {
108 switch (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";
120 }
121 }
122
123 /// getTypeConstantSuffix - Return the constant suffix for the specified
124 /// integer type enum. For example, SignedLong -> "L".
getTypeConstantSuffix(IntType T) const125 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
126 switch (T) {
127 default: llvm_unreachable("not an integer!");
128 case SignedChar:
129 case SignedShort:
130 case SignedInt: return "";
131 case SignedLong: return "L";
132 case SignedLongLong: return "LL";
133 case UnsignedChar:
134 if (getCharWidth() < getIntWidth())
135 return "";
136 case UnsignedShort:
137 if (getShortWidth() < getIntWidth())
138 return "";
139 case UnsignedInt: return "U";
140 case UnsignedLong: return "UL";
141 case UnsignedLongLong: return "ULL";
142 }
143 }
144
145 /// getTypeFormatModifier - Return the printf format modifier for the
146 /// specified integer type enum. For example, SignedLong -> "l".
147
getTypeFormatModifier(IntType T)148 const char *TargetInfo::getTypeFormatModifier(IntType T) {
149 switch (T) {
150 default: llvm_unreachable("not an integer!");
151 case SignedChar:
152 case UnsignedChar: return "hh";
153 case SignedShort:
154 case UnsignedShort: return "h";
155 case SignedInt:
156 case UnsignedInt: return "";
157 case SignedLong:
158 case UnsignedLong: return "l";
159 case SignedLongLong:
160 case UnsignedLongLong: return "ll";
161 }
162 }
163
164 /// getTypeWidth - Return the width (in bits) of the specified integer type
165 /// enum. For example, SignedInt -> getIntWidth().
getTypeWidth(IntType T) const166 unsigned TargetInfo::getTypeWidth(IntType T) const {
167 switch (T) {
168 default: llvm_unreachable("not an integer!");
169 case SignedChar:
170 case UnsignedChar: return getCharWidth();
171 case SignedShort:
172 case UnsignedShort: return getShortWidth();
173 case SignedInt:
174 case UnsignedInt: return getIntWidth();
175 case SignedLong:
176 case UnsignedLong: return getLongWidth();
177 case SignedLongLong:
178 case UnsignedLongLong: return getLongLongWidth();
179 };
180 }
181
getIntTypeByWidth(unsigned BitWidth,bool IsSigned) const182 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;
194 return NoInt;
195 }
196
getLeastIntTypeByWidth(unsigned BitWidth,bool IsSigned) const197 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;
209 return NoInt;
210 }
211
getRealTypeByWidth(unsigned BitWidth) const212 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
213 if (getFloatWidth() == BitWidth)
214 return Float;
215 if (getDoubleWidth() == BitWidth)
216 return Double;
217
218 switch (BitWidth) {
219 case 96:
220 if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
221 return LongDouble;
222 break;
223 case 128:
224 if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
225 &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
226 return LongDouble;
227 break;
228 }
229
230 return NoFloat;
231 }
232
233 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
234 /// enum. For example, SignedInt -> getIntAlign().
getTypeAlign(IntType T) const235 unsigned TargetInfo::getTypeAlign(IntType T) const {
236 switch (T) {
237 default: llvm_unreachable("not an integer!");
238 case SignedChar:
239 case UnsignedChar: return getCharAlign();
240 case SignedShort:
241 case UnsignedShort: return getShortAlign();
242 case SignedInt:
243 case UnsignedInt: return getIntAlign();
244 case SignedLong:
245 case UnsignedLong: return getLongAlign();
246 case SignedLongLong:
247 case UnsignedLongLong: return getLongLongAlign();
248 };
249 }
250
251 /// isTypeSigned - Return whether an integer types is signed. Returns true if
252 /// the type is signed; false otherwise.
isTypeSigned(IntType T)253 bool TargetInfo::isTypeSigned(IntType T) {
254 switch (T) {
255 default: llvm_unreachable("not an integer!");
256 case SignedChar:
257 case SignedShort:
258 case SignedInt:
259 case SignedLong:
260 case SignedLongLong:
261 return true;
262 case UnsignedChar:
263 case UnsignedShort:
264 case UnsignedInt:
265 case UnsignedLong:
266 case UnsignedLongLong:
267 return false;
268 };
269 }
270
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.
adjust(const LangOptions & Opts)274 void TargetInfo::adjust(const LangOptions &Opts) {
275 if (Opts.NoBitFieldTypeAlign)
276 UseBitFieldTypeAlignment = false;
277 if (Opts.ShortWChar)
278 WCharType = UnsignedShort;
279
280 if (Opts.OpenCL) {
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;
290
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;
297 }
298 LongDoubleWidth = LongDoubleAlign = 128;
299
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;
305
306 IntMaxType = SignedLongLong;
307 Int64Type = SignedLong;
308
309 HalfFormat = &llvm::APFloat::IEEEhalf;
310 FloatFormat = &llvm::APFloat::IEEEsingle;
311 LongDoubleFormat = &llvm::APFloat::IEEEquad;
312 }
313 }
314
initFeatureMap(llvm::StringMap<bool> & Features,DiagnosticsEngine & Diags,StringRef CPU,const std::vector<std::string> & FeatureVec) const315 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) {
319 StringRef Name = F;
320 // Apply the feature via the target.
321 bool Enabled = Name[0] == '+';
322 setFeatureEnabled(Features, Name.substr(1), Enabled);
323 }
324 return true;
325 }
326
327 //===----------------------------------------------------------------------===//
328
329
removeGCCRegisterPrefix(StringRef Name)330 static StringRef removeGCCRegisterPrefix(StringRef Name) {
331 if (Name[0] == '%' || Name[0] == '#')
332 Name = Name.substr(1);
333
334 return Name;
335 }
336
337 /// isValidClobber - Returns whether the passed in string is
338 /// a valid clobber in an inline asm statement. This is used by
339 /// Sema.
isValidClobber(StringRef Name) const340 bool TargetInfo::isValidClobber(StringRef Name) const {
341 return (isValidGCCRegisterName(Name) ||
342 Name == "memory" || Name == "cc");
343 }
344
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.
isValidGCCRegisterName(StringRef Name) const348 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
349 if (Name.empty())
350 return false;
351
352 // Get rid of any register prefix.
353 Name = removeGCCRegisterPrefix(Name);
354 if (Name.empty())
355 return false;
356
357 ArrayRef<const char *> Names = getGCCRegNames();
358
359 // If we have a number it maps to an entry in the register name array.
360 if (isDigit(Name[0])) {
361 unsigned n;
362 if (!Name.getAsInteger(0, n))
363 return n < Names.size();
364 }
365
366 // Check register names.
367 if (std::find(Names.begin(), Names.end(), Name) != Names.end())
368 return true;
369
370 // Check any additional names that we have.
371 for (const AddlRegName &ARN : getGCCAddlRegNames())
372 for (const char *AN : ARN.Names) {
373 if (!AN)
374 break;
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())
378 return true;
379 }
380
381 // Now check aliases.
382 for (const GCCRegAlias &GRA : getGCCRegAliases())
383 for (const char *A : GRA.Aliases) {
384 if (!A)
385 break;
386 if (A == Name)
387 return true;
388 }
389
390 return false;
391 }
392
393 StringRef
getNormalizedGCCRegisterName(StringRef Name) const394 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
395 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
396
397 // Get rid of any register prefix.
398 Name = removeGCCRegisterPrefix(Name);
399
400 ArrayRef<const char *> Names = getGCCRegNames();
401
402 // First, check if we have a number.
403 if (isDigit(Name[0])) {
404 unsigned n;
405 if (!Name.getAsInteger(0, n)) {
406 assert(n < Names.size() && "Out of bounds register number!");
407 return Names[n];
408 }
409 }
410
411 // Check any additional names that we have.
412 for (const AddlRegName &ARN : getGCCAddlRegNames())
413 for (const char *AN : ARN.Names) {
414 if (!AN)
415 break;
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())
419 return Name;
420 }
421
422 // Now check aliases.
423 for (const GCCRegAlias &RA : getGCCRegAliases())
424 for (const char *A : RA.Aliases) {
425 if (!A)
426 break;
427 if (A == Name)
428 return RA.Register;
429 }
430
431 return Name;
432 }
433
validateOutputConstraint(ConstraintInfo & Info) const434 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 != '+')
438 return false;
439
440 if (*Name == '+')
441 Info.setIsReadWrite();
442
443 Name++;
444 while (*Name) {
445 switch (*Name) {
446 default:
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'.
451 return false;
452 }
453 break;
454 case '&': // early clobber.
455 Info.setEarlyClobber();
456 break;
457 case '%': // commutative.
458 // FIXME: Check that there is a another register after this one.
459 break;
460 case 'r': // general register.
461 Info.setAllowsRegister();
462 break;
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();
469 break;
470 case 'g': // general register, memory operand or immediate integer.
471 case 'X': // any operand.
472 Info.setAllowsRegister();
473 Info.setAllowsMemory();
474 break;
475 case ',': // multiple alternative constraint. Pass it.
476 // Handle additional optional '=' or '+' modifiers.
477 if (Name[1] == '=' || Name[1] == '+')
478 Name++;
479 break;
480 case '#': // Ignore as constraint.
481 while (Name[1] && Name[1] != ',')
482 Name++;
483 break;
484 case '?': // Disparage slightly code.
485 case '!': // Disparage severely.
486 case '*': // Ignore for choosing register preferences.
487 break; // Pass them.
488 }
489
490 Name++;
491 }
492
493 // Early clobber with a read-write constraint which doesn't permit registers
494 // is invalid.
495 if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
496 return false;
497
498 // If a constraint allows neither memory nor register operands it contains
499 // only modifiers. Reject it.
500 return Info.allowsMemory() || Info.allowsRegister();
501 }
502
resolveSymbolicName(const char * & Name,ArrayRef<ConstraintInfo> OutputConstraints,unsigned & Index) const503 bool TargetInfo::resolveSymbolicName(const char *&Name,
504 ArrayRef<ConstraintInfo> OutputConstraints,
505 unsigned &Index) const {
506 assert(*Name == '[' && "Symbolic name did not start with '['");
507 Name++;
508 const char *Start = Name;
509 while (*Name && *Name != ']')
510 Name++;
511
512 if (!*Name) {
513 // Missing ']'
514 return false;
515 }
516
517 std::string SymbolicName(Start, Name - Start);
518
519 for (Index = 0; Index != OutputConstraints.size(); ++Index)
520 if (SymbolicName == OutputConstraints[Index].getName())
521 return true;
522
523 return false;
524 }
525
validateInputConstraint(MutableArrayRef<ConstraintInfo> OutputConstraints,ConstraintInfo & Info) const526 bool TargetInfo::validateInputConstraint(
527 MutableArrayRef<ConstraintInfo> OutputConstraints,
528 ConstraintInfo &Info) const {
529 const char *Name = Info.ConstraintStr.c_str();
530
531 if (!*Name)
532 return false;
533
534 while (*Name) {
535 switch (*Name) {
536 default:
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')
541 Name++;
542 const char *DigitEnd = Name;
543 unsigned i;
544 if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
545 .getAsInteger(10, i))
546 return false;
547
548 // Check if matching constraint is out of bounds.
549 if (i >= OutputConstraints.size()) return false;
550
551 // A number must refer to an output only operand.
552 if (OutputConstraints[i].isReadWrite())
553 return false;
554
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)
558 return false;
559
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'.
567 return false;
568 }
569 break;
570 case '[': {
571 unsigned Index = 0;
572 if (!resolveSymbolicName(Name, OutputConstraints, Index))
573 return false;
574
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)
578 return false;
579
580 // A number must refer to an output only operand.
581 if (OutputConstraints[Index].isReadWrite())
582 return false;
583
584 Info.setTiedOperand(Index, OutputConstraints[Index]);
585 break;
586 }
587 case '%': // commutative
588 // FIXME: Fail if % is used with the last operand.
589 break;
590 case 'i': // immediate integer.
591 case 'n': // immediate integer with a known value.
592 break;
593 case 'I': // Various constant constraints with target-specific meanings.
594 case 'J':
595 case 'K':
596 case 'L':
597 case 'M':
598 case 'N':
599 case 'O':
600 case 'P':
601 if (!validateAsmConstraint(Name, Info))
602 return false;
603 break;
604 case 'r': // general register.
605 Info.setAllowsRegister();
606 break;
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();
613 break;
614 case 'g': // general register, memory operand or immediate integer.
615 case 'X': // any operand.
616 Info.setAllowsRegister();
617 Info.setAllowsMemory();
618 break;
619 case 'E': // immediate floating point.
620 case 'F': // immediate floating point.
621 case 'p': // address operand.
622 break;
623 case ',': // multiple alternative constraint. Ignore comma.
624 break;
625 case '#': // Ignore as constraint.
626 while (Name[1] && Name[1] != ',')
627 Name++;
628 break;
629 case '?': // Disparage slightly code.
630 case '!': // Disparage severely.
631 case '*': // Ignore for choosing register preferences.
632 break; // Pass them.
633 }
634
635 Name++;
636 }
637
638 return true;
639 }
640