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