]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/Basic/TargetInfo.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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/AddressSpaces.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Basic/LangOptions.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include <cctype>
21 #include <cstdlib>
22 using namespace clang;
23
24 static const LangAS::Map DefaultAddrSpaceMap = { 0 };
25
26 // TargetInfo Constructor.
27 TargetInfo::TargetInfo(const std::string &T) : 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   TLSSupported = true;
31   NoAsmVariants = false;
32   PointerWidth = PointerAlign = 32;
33   BoolWidth = BoolAlign = 8;
34   IntWidth = IntAlign = 32;
35   LongWidth = LongAlign = 32;
36   LongLongWidth = LongLongAlign = 64;
37   HalfWidth = 16;
38   HalfAlign = 16;
39   FloatWidth = 32;
40   FloatAlign = 32;
41   DoubleWidth = 64;
42   DoubleAlign = 64;
43   LongDoubleWidth = 64;
44   LongDoubleAlign = 64;
45   LargeArrayMinWidth = 0;
46   LargeArrayAlign = 0;
47   MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
48   SizeType = UnsignedLong;
49   PtrDiffType = SignedLong;
50   IntMaxType = SignedLongLong;
51   UIntMaxType = UnsignedLongLong;
52   IntPtrType = SignedLong;
53   WCharType = SignedInt;
54   WIntType = SignedInt;
55   Char16Type = UnsignedShort;
56   Char32Type = UnsignedInt;
57   Int64Type = SignedLongLong;
58   SigAtomicType = SignedInt;
59   UseBitFieldTypeAlignment = true;
60   UseZeroLengthBitfieldAlignment = false;
61   ZeroLengthBitfieldBoundary = 0;
62   HalfFormat = &llvm::APFloat::IEEEhalf;
63   FloatFormat = &llvm::APFloat::IEEEsingle;
64   DoubleFormat = &llvm::APFloat::IEEEdouble;
65   LongDoubleFormat = &llvm::APFloat::IEEEdouble;
66   DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
67                       "i64:64:64-f32:32:32-f64:64:64-n32";
68   UserLabelPrefix = "_";
69   MCountName = "mcount";
70   RegParmMax = 0;
71   SSERegParmMax = 0;
72   HasAlignMac68kSupport = false;
73
74   // Default to no types using fpret.
75   RealTypeUsesObjCFPRet = 0;
76
77   // Default to using the Itanium ABI.
78   CXXABI = CXXABI_Itanium;
79
80   // Default to an empty address space map.
81   AddrSpaceMap = &DefaultAddrSpaceMap;
82
83   // Default to an unknown platform name.
84   PlatformName = "unknown";
85   PlatformMinVersion = VersionTuple();
86 }
87
88 // Out of line virtual dtor for TargetInfo.
89 TargetInfo::~TargetInfo() {}
90
91 /// getTypeName - Return the user string for the specified integer type enum.
92 /// For example, SignedShort -> "short".
93 const char *TargetInfo::getTypeName(IntType T) {
94   switch (T) {
95   default: llvm_unreachable("not an integer!");
96   case SignedShort:      return "short";
97   case UnsignedShort:    return "unsigned short";
98   case SignedInt:        return "int";
99   case UnsignedInt:      return "unsigned int";
100   case SignedLong:       return "long int";
101   case UnsignedLong:     return "long unsigned int";
102   case SignedLongLong:   return "long long int";
103   case UnsignedLongLong: return "long long unsigned int";
104   }
105 }
106
107 /// getTypeConstantSuffix - Return the constant suffix for the specified
108 /// integer type enum. For example, SignedLong -> "L".
109 const char *TargetInfo::getTypeConstantSuffix(IntType T) {
110   switch (T) {
111   default: llvm_unreachable("not an integer!");
112   case SignedShort:
113   case SignedInt:        return "";
114   case SignedLong:       return "L";
115   case SignedLongLong:   return "LL";
116   case UnsignedShort:
117   case UnsignedInt:      return "U";
118   case UnsignedLong:     return "UL";
119   case UnsignedLongLong: return "ULL";
120   }
121 }
122
123 /// getTypeWidth - Return the width (in bits) of the specified integer type
124 /// enum. For example, SignedInt -> getIntWidth().
125 unsigned TargetInfo::getTypeWidth(IntType T) const {
126   switch (T) {
127   default: llvm_unreachable("not an integer!");
128   case SignedShort:
129   case UnsignedShort:    return getShortWidth();
130   case SignedInt:
131   case UnsignedInt:      return getIntWidth();
132   case SignedLong:
133   case UnsignedLong:     return getLongWidth();
134   case SignedLongLong:
135   case UnsignedLongLong: return getLongLongWidth();
136   };
137 }
138
139 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
140 /// enum. For example, SignedInt -> getIntAlign().
141 unsigned TargetInfo::getTypeAlign(IntType T) const {
142   switch (T) {
143   default: llvm_unreachable("not an integer!");
144   case SignedShort:
145   case UnsignedShort:    return getShortAlign();
146   case SignedInt:
147   case UnsignedInt:      return getIntAlign();
148   case SignedLong:
149   case UnsignedLong:     return getLongAlign();
150   case SignedLongLong:
151   case UnsignedLongLong: return getLongLongAlign();
152   };
153 }
154
155 /// isTypeSigned - Return whether an integer types is signed. Returns true if
156 /// the type is signed; false otherwise.
157 bool TargetInfo::isTypeSigned(IntType T) {
158   switch (T) {
159   default: llvm_unreachable("not an integer!");
160   case SignedShort:
161   case SignedInt:
162   case SignedLong:
163   case SignedLongLong:
164     return true;
165   case UnsignedShort:
166   case UnsignedInt:
167   case UnsignedLong:
168   case UnsignedLongLong:
169     return false;
170   };
171 }
172
173 /// setForcedLangOptions - Set forced language options.
174 /// Apply changes to the target information with respect to certain
175 /// language options which change the target configuration.
176 void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
177   if (Opts.NoBitFieldTypeAlign)
178     UseBitFieldTypeAlignment = false;
179   if (Opts.ShortWChar)
180     WCharType = UnsignedShort;
181 }
182
183 //===----------------------------------------------------------------------===//
184
185
186 static StringRef removeGCCRegisterPrefix(StringRef Name) {
187   if (Name[0] == '%' || Name[0] == '#')
188     Name = Name.substr(1);
189
190   return Name;
191 }
192
193 /// isValidClobber - Returns whether the passed in string is
194 /// a valid clobber in an inline asm statement. This is used by
195 /// Sema.
196 bool TargetInfo::isValidClobber(StringRef Name) const {
197   return (isValidGCCRegisterName(Name) ||
198           Name == "memory" || Name == "cc");
199 }
200
201 /// isValidGCCRegisterName - Returns whether the passed in string
202 /// is a valid register name according to GCC. This is used by Sema for
203 /// inline asm statements.
204 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
205   if (Name.empty())
206     return false;
207
208   const char * const *Names;
209   unsigned NumNames;
210
211   // Get rid of any register prefix.
212   Name = removeGCCRegisterPrefix(Name);
213
214   getGCCRegNames(Names, NumNames);
215
216   // If we have a number it maps to an entry in the register name array.
217   if (isdigit(Name[0])) {
218     int n;
219     if (!Name.getAsInteger(0, n))
220       return n >= 0 && (unsigned)n < NumNames;
221   }
222
223   // Check register names.
224   for (unsigned i = 0; i < NumNames; i++) {
225     if (Name == Names[i])
226       return true;
227   }
228
229   // Check any additional names that we have.
230   const AddlRegName *AddlNames;
231   unsigned NumAddlNames;
232   getGCCAddlRegNames(AddlNames, NumAddlNames);
233   for (unsigned i = 0; i < NumAddlNames; i++)
234     for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
235       if (!AddlNames[i].Names[j])
236         break;
237       // Make sure the register that the additional name is for is within
238       // the bounds of the register names from above.
239       if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
240         return true;
241   }
242
243   // Now check aliases.
244   const GCCRegAlias *Aliases;
245   unsigned NumAliases;
246
247   getGCCRegAliases(Aliases, NumAliases);
248   for (unsigned i = 0; i < NumAliases; i++) {
249     for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
250       if (!Aliases[i].Aliases[j])
251         break;
252       if (Aliases[i].Aliases[j] == Name)
253         return true;
254     }
255   }
256
257   return false;
258 }
259
260 StringRef
261 TargetInfo::getNormalizedGCCRegisterName(StringRef Name) const {
262   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
263
264   // Get rid of any register prefix.
265   Name = removeGCCRegisterPrefix(Name);
266
267   const char * const *Names;
268   unsigned NumNames;
269
270   getGCCRegNames(Names, NumNames);
271
272   // First, check if we have a number.
273   if (isdigit(Name[0])) {
274     int n;
275     if (!Name.getAsInteger(0, n)) {
276       assert(n >= 0 && (unsigned)n < NumNames &&
277              "Out of bounds register number!");
278       return Names[n];
279     }
280   }
281
282   // Check any additional names that we have.
283   const AddlRegName *AddlNames;
284   unsigned NumAddlNames;
285   getGCCAddlRegNames(AddlNames, NumAddlNames);
286   for (unsigned i = 0; i < NumAddlNames; i++)
287     for (unsigned j = 0; j < llvm::array_lengthof(AddlNames[i].Names); j++) {
288       if (!AddlNames[i].Names[j])
289         break;
290       // Make sure the register that the additional name is for is within
291       // the bounds of the register names from above.
292       if (AddlNames[i].Names[j] == Name && AddlNames[i].RegNum < NumNames)
293         return Name;
294     }
295
296   // Now check aliases.
297   const GCCRegAlias *Aliases;
298   unsigned NumAliases;
299
300   getGCCRegAliases(Aliases, NumAliases);
301   for (unsigned i = 0; i < NumAliases; i++) {
302     for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
303       if (!Aliases[i].Aliases[j])
304         break;
305       if (Aliases[i].Aliases[j] == Name)
306         return Aliases[i].Register;
307     }
308   }
309
310   return Name;
311 }
312
313 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
314   const char *Name = Info.getConstraintStr().c_str();
315   // An output constraint must start with '=' or '+'
316   if (*Name != '=' && *Name != '+')
317     return false;
318
319   if (*Name == '+')
320     Info.setIsReadWrite();
321
322   Name++;
323   while (*Name) {
324     switch (*Name) {
325     default:
326       if (!validateAsmConstraint(Name, Info)) {
327         // FIXME: We temporarily return false
328         // so we can add more constraints as we hit it.
329         // Eventually, an unknown constraint should just be treated as 'g'.
330         return false;
331       }
332     case '&': // early clobber.
333       break;
334     case '%': // commutative.
335       // FIXME: Check that there is a another register after this one.
336       break;
337     case 'r': // general register.
338       Info.setAllowsRegister();
339       break;
340     case 'm': // memory operand.
341     case 'o': // offsetable memory operand.
342     case 'V': // non-offsetable memory operand.
343     case '<': // autodecrement memory operand.
344     case '>': // autoincrement memory operand.
345       Info.setAllowsMemory();
346       break;
347     case 'g': // general register, memory operand or immediate integer.
348     case 'X': // any operand.
349       Info.setAllowsRegister();
350       Info.setAllowsMemory();
351       break;
352     case ',': // multiple alternative constraint.  Pass it.
353       // Handle additional optional '=' or '+' modifiers.
354       if (Name[1] == '=' || Name[1] == '+')
355         Name++;
356       break;
357     case '?': // Disparage slightly code.
358     case '!': // Disparage severely.
359       break;  // Pass them.
360     }
361
362     Name++;
363   }
364
365   return true;
366 }
367
368 bool TargetInfo::resolveSymbolicName(const char *&Name,
369                                      ConstraintInfo *OutputConstraints,
370                                      unsigned NumOutputs,
371                                      unsigned &Index) const {
372   assert(*Name == '[' && "Symbolic name did not start with '['");
373   Name++;
374   const char *Start = Name;
375   while (*Name && *Name != ']')
376     Name++;
377
378   if (!*Name) {
379     // Missing ']'
380     return false;
381   }
382
383   std::string SymbolicName(Start, Name - Start);
384
385   for (Index = 0; Index != NumOutputs; ++Index)
386     if (SymbolicName == OutputConstraints[Index].getName())
387       return true;
388
389   return false;
390 }
391
392 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
393                                          unsigned NumOutputs,
394                                          ConstraintInfo &Info) const {
395   const char *Name = Info.ConstraintStr.c_str();
396
397   while (*Name) {
398     switch (*Name) {
399     default:
400       // Check if we have a matching constraint
401       if (*Name >= '0' && *Name <= '9') {
402         unsigned i = *Name - '0';
403
404         // Check if matching constraint is out of bounds.
405         if (i >= NumOutputs)
406           return false;
407
408         // A number must refer to an output only operand.
409         if (OutputConstraints[i].isReadWrite())
410           return false;
411
412         // If the constraint is already tied, it must be tied to the 
413         // same operand referenced to by the number.
414         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
415           return false;
416
417         // The constraint should have the same info as the respective
418         // output constraint.
419         Info.setTiedOperand(i, OutputConstraints[i]);
420       } else if (!validateAsmConstraint(Name, Info)) {
421         // FIXME: This error return is in place temporarily so we can
422         // add more constraints as we hit it.  Eventually, an unknown
423         // constraint should just be treated as 'g'.
424         return false;
425       }
426       break;
427     case '[': {
428       unsigned Index = 0;
429       if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
430         return false;
431
432       // If the constraint is already tied, it must be tied to the 
433       // same operand referenced to by the number.
434       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
435         return false;
436
437       Info.setTiedOperand(Index, OutputConstraints[Index]);
438       break;
439     }
440     case '%': // commutative
441       // FIXME: Fail if % is used with the last operand.
442       break;
443     case 'i': // immediate integer.
444     case 'n': // immediate integer with a known value.
445       break;
446     case 'I':  // Various constant constraints with target-specific meanings.
447     case 'J':
448     case 'K':
449     case 'L':
450     case 'M':
451     case 'N':
452     case 'O':
453     case 'P':
454       break;
455     case 'r': // general register.
456       Info.setAllowsRegister();
457       break;
458     case 'm': // memory operand.
459     case 'o': // offsettable memory operand.
460     case 'V': // non-offsettable memory operand.
461     case '<': // autodecrement memory operand.
462     case '>': // autoincrement memory operand.
463       Info.setAllowsMemory();
464       break;
465     case 'g': // general register, memory operand or immediate integer.
466     case 'X': // any operand.
467       Info.setAllowsRegister();
468       Info.setAllowsMemory();
469       break;
470     case 'E': // immediate floating point.
471     case 'F': // immediate floating point.
472     case 'p': // address operand.
473       break;
474     case ',': // multiple alternative constraint.  Ignore comma.
475       break;
476     case '?': // Disparage slightly code.
477     case '!': // Disparage severely.
478       break;  // Pass them.
479     }
480
481     Name++;
482   }
483
484   return true;
485 }