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