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