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