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