]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Basic/TargetInfo.cpp
Merge clang 3.5.0 release from ^/vendor/clang/dist, resolve conflicts,
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Basic / TargetInfo.cpp
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.
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   MinGlobalAlign = 0;
40   HalfWidth = 16;
41   HalfAlign = 16;
42   FloatWidth = 32;
43   FloatAlign = 32;
44   DoubleWidth = 64;
45   DoubleAlign = 64;
46   LongDoubleWidth = 64;
47   LongDoubleAlign = 64;
48   LargeArrayMinWidth = 0;
49   LargeArrayAlign = 0;
50   MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
51   MaxVectorAlign = 0;
52   SizeType = UnsignedLong;
53   PtrDiffType = SignedLong;
54   IntMaxType = SignedLongLong;
55   IntPtrType = SignedLong;
56   WCharType = SignedInt;
57   WIntType = 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";
74   RegParmMax = 0;
75   SSERegParmMax = 0;
76   HasAlignMac68kSupport = false;
77
78   // Default to no types using fpret.
79   RealTypeUsesObjCFPRet = 0;
80
81   // Default to not using fp2ret for __Complex long double
82   ComplexLongDoubleUsesFP2Ret = false;
83
84   // Set the C++ ABI based on the triple.
85   TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
86                     ? TargetCXXABI::Microsoft
87                     : TargetCXXABI::GenericItanium);
88
89   // Default to an empty address space map.
90   AddrSpaceMap = &DefaultAddrSpaceMap;
91   UseAddrSpaceMapMangling = false;
92
93   // Default to an unknown platform name.
94   PlatformName = "unknown";
95   PlatformMinVersion = VersionTuple();
96 }
97
98 // Out of line virtual dtor for TargetInfo.
99 TargetInfo::~TargetInfo() {}
100
101 /// getTypeName - Return the user string for the specified integer type enum.
102 /// For example, SignedShort -> "short".
103 const char *TargetInfo::getTypeName(IntType T) {
104   switch (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";
116   }
117 }
118
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 {
122   switch (T) {
123   default: llvm_unreachable("not an integer!");
124   case SignedChar:
125   case SignedShort:
126   case SignedInt:        return "";
127   case SignedLong:       return "L";
128   case SignedLongLong:   return "LL";
129   case UnsignedChar:
130     if (getCharWidth() < getIntWidth())
131       return "";
132   case UnsignedShort:
133     if (getShortWidth() < getIntWidth())
134       return "";
135   case UnsignedInt:      return "U";
136   case UnsignedLong:     return "UL";
137   case UnsignedLongLong: return "ULL";
138   }
139 }
140
141 /// getTypeFormatModifier - Return the printf format modifier for the
142 /// specified integer type enum. For example, SignedLong -> "l".
143
144 const char *TargetInfo::getTypeFormatModifier(IntType T) {
145   switch (T) {
146   default: llvm_unreachable("not an integer!");
147   case SignedChar:
148   case UnsignedChar:     return "hh";
149   case SignedShort:
150   case UnsignedShort:    return "h";
151   case SignedInt:
152   case UnsignedInt:      return "";
153   case SignedLong:
154   case UnsignedLong:     return "l";
155   case SignedLongLong:
156   case UnsignedLongLong: return "ll";
157   }
158 }
159
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 {
163   switch (T) {
164   default: llvm_unreachable("not an integer!");
165   case SignedChar:
166   case UnsignedChar:     return getCharWidth();
167   case SignedShort:
168   case UnsignedShort:    return getShortWidth();
169   case SignedInt:
170   case UnsignedInt:      return getIntWidth();
171   case SignedLong:
172   case UnsignedLong:     return getLongWidth();
173   case SignedLongLong:
174   case UnsignedLongLong: return getLongLongWidth();
175   };
176 }
177
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;
190   return NoInt;
191 }
192
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;
205   return NoInt;
206 }
207
208 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth) const {
209   if (getFloatWidth() == BitWidth)
210     return Float;
211   if (getDoubleWidth() == BitWidth)
212     return Double;
213
214   switch (BitWidth) {
215   case 96:
216     if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended)
217       return LongDouble;
218     break;
219   case 128:
220     if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble ||
221         &getLongDoubleFormat() == &llvm::APFloat::IEEEquad)
222       return LongDouble;
223     break;
224   }
225
226   return NoFloat;
227 }
228
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 {
232   switch (T) {
233   default: llvm_unreachable("not an integer!");
234   case SignedChar:
235   case UnsignedChar:     return getCharAlign();
236   case SignedShort:
237   case UnsignedShort:    return getShortAlign();
238   case SignedInt:
239   case UnsignedInt:      return getIntAlign();
240   case SignedLong:
241   case UnsignedLong:     return getLongAlign();
242   case SignedLongLong:
243   case UnsignedLongLong: return getLongLongAlign();
244   };
245 }
246
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) {
250   switch (T) {
251   default: llvm_unreachable("not an integer!");
252   case SignedChar:
253   case SignedShort:
254   case SignedInt:
255   case SignedLong:
256   case SignedLongLong:
257     return true;
258   case UnsignedChar:
259   case UnsignedShort:
260   case UnsignedInt:
261   case UnsignedLong:
262   case UnsignedLongLong:
263     return false;
264   };
265 }
266
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;
273   if (Opts.ShortWChar)
274     WCharType = UnsignedShort;
275
276   if (Opts.OpenCL) {
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;
286     
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;
293     }
294     LongDoubleWidth = LongDoubleAlign = 128;
295
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;
301
302     IntMaxType = SignedLongLong;
303     Int64Type = SignedLong;
304
305     HalfFormat = &llvm::APFloat::IEEEhalf;
306     FloatFormat = &llvm::APFloat::IEEEsingle;
307     LongDoubleFormat = &llvm::APFloat::IEEEquad;
308   }
309 }
310
311 //===----------------------------------------------------------------------===//
312
313
314 static StringRef removeGCCRegisterPrefix(StringRef Name) {
315   if (Name[0] == '%' || Name[0] == '#')
316     Name = Name.substr(1);
317
318   return Name;
319 }
320
321 /// isValidClobber - Returns whether the passed in string is
322 /// a valid clobber in an inline asm statement. This is used by
323 /// Sema.
324 bool TargetInfo::isValidClobber(StringRef Name) const {
325   return (isValidGCCRegisterName(Name) ||
326           Name == "memory" || Name == "cc");
327 }
328
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 {
333   if (Name.empty())
334     return false;
335
336   const char * const *Names;
337   unsigned NumNames;
338
339   // Get rid of any register prefix.
340   Name = removeGCCRegisterPrefix(Name);
341
342   getGCCRegNames(Names, NumNames);
343
344   // If we have a number it maps to an entry in the register name array.
345   if (isDigit(Name[0])) {
346     int n;
347     if (!Name.getAsInteger(0, n))
348       return n >= 0 && (unsigned)n < NumNames;
349   }
350
351   // Check register names.
352   for (unsigned i = 0; i < NumNames; i++) {
353     if (Name == Names[i])
354       return true;
355   }
356
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])
364         break;
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)
368         return true;
369   }
370
371   // Now check aliases.
372   const GCCRegAlias *Aliases;
373   unsigned NumAliases;
374
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])
379         break;
380       if (Aliases[i].Aliases[j] == Name)
381         return true;
382     }
383   }
384
385   return false;
386 }
387
388 StringRef
389 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
390   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
391
392   // Get rid of any register prefix.
393   Name = removeGCCRegisterPrefix(Name);
394
395   const char * const *Names;
396   unsigned NumNames;
397
398   getGCCRegNames(Names, NumNames);
399
400   // First, check if we have a number.
401   if (isDigit(Name[0])) {
402     int n;
403     if (!Name.getAsInteger(0, n)) {
404       assert(n >= 0 && (unsigned)n < NumNames &&
405              "Out of bounds register number!");
406       return Names[n];
407     }
408   }
409
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])
417         break;
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)
421         return Name;
422     }
423
424   // Now check aliases.
425   const GCCRegAlias *Aliases;
426   unsigned NumAliases;
427
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])
432         break;
433       if (Aliases[i].Aliases[j] == Name)
434         return Aliases[i].Register;
435     }
436   }
437
438   return Name;
439 }
440
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 != '+')
445     return false;
446
447   if (*Name == '+')
448     Info.setIsReadWrite();
449
450   Name++;
451   while (*Name) {
452     switch (*Name) {
453     default:
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'.
458         return false;
459       }
460     case '&': // early clobber.
461       break;
462     case '%': // commutative.
463       // FIXME: Check that there is a another register after this one.
464       break;
465     case 'r': // general register.
466       Info.setAllowsRegister();
467       break;
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();
474       break;
475     case 'g': // general register, memory operand or immediate integer.
476     case 'X': // any operand.
477       Info.setAllowsRegister();
478       Info.setAllowsMemory();
479       break;
480     case ',': // multiple alternative constraint.  Pass it.
481       // Handle additional optional '=' or '+' modifiers.
482       if (Name[1] == '=' || Name[1] == '+')
483         Name++;
484       break;
485     case '?': // Disparage slightly code.
486     case '!': // Disparage severely.
487     case '#': // Ignore as constraint.
488     case '*': // Ignore for choosing register preferences.
489       break;  // Pass them.
490     }
491
492     Name++;
493   }
494
495   // If a constraint allows neither memory nor register operands it contains
496   // only modifiers. Reject it.
497   return Info.allowsMemory() || Info.allowsRegister();
498 }
499
500 bool TargetInfo::resolveSymbolicName(const char *&Name,
501                                      ConstraintInfo *OutputConstraints,
502                                      unsigned NumOutputs,
503                                      unsigned &Index) const {
504   assert(*Name == '[' && "Symbolic name did not start with '['");
505   Name++;
506   const char *Start = Name;
507   while (*Name && *Name != ']')
508     Name++;
509
510   if (!*Name) {
511     // Missing ']'
512     return false;
513   }
514
515   std::string SymbolicName(Start, Name - Start);
516
517   for (Index = 0; Index != NumOutputs; ++Index)
518     if (SymbolicName == OutputConstraints[Index].getName())
519       return true;
520
521   return false;
522 }
523
524 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
525                                          unsigned NumOutputs,
526                                          ConstraintInfo &Info) const {
527   const char *Name = Info.ConstraintStr.c_str();
528
529   if (!*Name)
530     return false;
531
532   while (*Name) {
533     switch (*Name) {
534     default:
535       // Check if we have a matching constraint
536       if (*Name >= '0' && *Name <= '9') {
537         unsigned i = *Name - '0';
538
539         // Check if matching constraint is out of bounds.
540         if (i >= NumOutputs)
541           return false;
542
543         // A number must refer to an output only operand.
544         if (OutputConstraints[i].isReadWrite())
545           return false;
546
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)
550           return false;
551
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'.
559         return false;
560       }
561       break;
562     case '[': {
563       unsigned Index = 0;
564       if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
565         return false;
566
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)
570         return false;
571
572       Info.setTiedOperand(Index, OutputConstraints[Index]);
573       break;
574     }
575     case '%': // commutative
576       // FIXME: Fail if % is used with the last operand.
577       break;
578     case 'i': // immediate integer.
579     case 'n': // immediate integer with a known value.
580       break;
581     case 'I':  // Various constant constraints with target-specific meanings.
582     case 'J':
583     case 'K':
584     case 'L':
585     case 'M':
586     case 'N':
587     case 'O':
588     case 'P':
589       break;
590     case 'r': // general register.
591       Info.setAllowsRegister();
592       break;
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();
599       break;
600     case 'g': // general register, memory operand or immediate integer.
601     case 'X': // any operand.
602       Info.setAllowsRegister();
603       Info.setAllowsMemory();
604       break;
605     case 'E': // immediate floating point.
606     case 'F': // immediate floating point.
607     case 'p': // address operand.
608       break;
609     case ',': // multiple alternative constraint.  Ignore comma.
610       break;
611     case '?': // Disparage slightly code.
612     case '!': // Disparage severely.
613     case '#': // Ignore as constraint.
614     case '*': // Ignore for choosing register preferences.
615       break;  // Pass them.
616     }
617
618     Name++;
619   }
620
621   return true;
622 }
623
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)
628     .Case("ios", iOS)
629     .Case("itanium", GenericItanium)
630     .Case("microsoft", Microsoft)
631     .Default(unknown);
632   if (kind == unknown) return false;
633
634   set(kind);
635   return true;
636 }