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/TargetInfo.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include <cstdlib>
18 using namespace clang;
19 
20 // TargetInfo Constructor.
21 TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
22   // Set defaults.  Defaults are set for a 32-bit RISC platform,
23   // like PPC or SPARC.
24   // These should be overridden by concrete targets as needed.
25   TLSSupported = true;
26   PointerWidth = PointerAlign = 32;
27   WCharWidth = WCharAlign = 32;
28   Char16Width = Char16Align = 16;
29   Char32Width = Char32Align = 32;
30   IntWidth = IntAlign = 32;
31   LongWidth = LongAlign = 32;
32   LongLongWidth = LongLongAlign = 64;
33   FloatWidth = 32;
34   FloatAlign = 32;
35   DoubleWidth = 64;
36   DoubleAlign = 64;
37   LongDoubleWidth = 64;
38   LongDoubleAlign = 64;
39   IntMaxTWidth = 64;
40   SizeType = UnsignedLong;
41   PtrDiffType = SignedLong;
42   IntMaxType = SignedLongLong;
43   UIntMaxType = UnsignedLongLong;
44   IntPtrType = SignedLong;
45   WCharType = SignedInt;
46   Char16Type = UnsignedShort;
47   Char32Type = UnsignedInt;
48   Int64Type = SignedLongLong;
49   FloatFormat = &llvm::APFloat::IEEEsingle;
50   DoubleFormat = &llvm::APFloat::IEEEdouble;
51   LongDoubleFormat = &llvm::APFloat::IEEEdouble;
52   DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
53                       "i64:64:64-f32:32:32-f64:64:64";
54   UserLabelPrefix = "_";
55 }
56 
57 // Out of line virtual dtor for TargetInfo.
58 TargetInfo::~TargetInfo() {}
59 
60 /// getTypeName - Return the user string for the specified integer type enum.
61 /// For example, SignedShort -> "short".
62 const char *TargetInfo::getTypeName(IntType T) {
63   switch (T) {
64   default: assert(0 && "not an integer!");
65   case SignedShort:      return "short";
66   case UnsignedShort:    return "unsigned short";
67   case SignedInt:        return "int";
68   case UnsignedInt:      return "unsigned int";
69   case SignedLong:       return "long int";
70   case UnsignedLong:     return "long unsigned int";
71   case SignedLongLong:   return "long long int";
72   case UnsignedLongLong: return "long long unsigned int";
73   }
74 }
75 
76 //===----------------------------------------------------------------------===//
77 
78 
79 static void removeGCCRegisterPrefix(const char *&Name) {
80   if (Name[0] == '%' || Name[0] == '#')
81     Name++;
82 }
83 
84 /// isValidGCCRegisterName - Returns whether the passed in string
85 /// is a valid register name according to GCC. This is used by Sema for
86 /// inline asm statements.
87 bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
88   const char * const *Names;
89   unsigned NumNames;
90 
91   // Get rid of any register prefix.
92   removeGCCRegisterPrefix(Name);
93 
94 
95   if (strcmp(Name, "memory") == 0 ||
96       strcmp(Name, "cc") == 0)
97     return true;
98 
99   getGCCRegNames(Names, NumNames);
100 
101   // If we have a number it maps to an entry in the register name array.
102   if (isdigit(Name[0])) {
103     char *End;
104     int n = (int)strtol(Name, &End, 0);
105     if (*End == 0)
106       return n >= 0 && (unsigned)n < NumNames;
107   }
108 
109   // Check register names.
110   for (unsigned i = 0; i < NumNames; i++) {
111     if (strcmp(Name, Names[i]) == 0)
112       return true;
113   }
114 
115   // Now check aliases.
116   const GCCRegAlias *Aliases;
117   unsigned NumAliases;
118 
119   getGCCRegAliases(Aliases, NumAliases);
120   for (unsigned i = 0; i < NumAliases; i++) {
121     for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
122       if (!Aliases[i].Aliases[j])
123         break;
124       if (strcmp(Aliases[i].Aliases[j], Name) == 0)
125         return true;
126     }
127   }
128 
129   return false;
130 }
131 
132 const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
133   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
134 
135   removeGCCRegisterPrefix(Name);
136 
137   const char * const *Names;
138   unsigned NumNames;
139 
140   getGCCRegNames(Names, NumNames);
141 
142   // First, check if we have a number.
143   if (isdigit(Name[0])) {
144     char *End;
145     int n = (int)strtol(Name, &End, 0);
146     if (*End == 0) {
147       assert(n >= 0 && (unsigned)n < NumNames &&
148              "Out of bounds register number!");
149       return Names[n];
150     }
151   }
152 
153   // Now check aliases.
154   const GCCRegAlias *Aliases;
155   unsigned NumAliases;
156 
157   getGCCRegAliases(Aliases, NumAliases);
158   for (unsigned i = 0; i < NumAliases; i++) {
159     for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
160       if (!Aliases[i].Aliases[j])
161         break;
162       if (strcmp(Aliases[i].Aliases[j], Name) == 0)
163         return Aliases[i].Register;
164     }
165   }
166 
167   return Name;
168 }
169 
170 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
171   const char *Name = Info.getConstraintStr().c_str();
172   // An output constraint must start with '=' or '+'
173   if (*Name != '=' && *Name != '+')
174     return false;
175 
176   if (*Name == '+')
177     Info.setIsReadWrite();
178 
179   Name++;
180   while (*Name) {
181     switch (*Name) {
182     default:
183       if (!validateAsmConstraint(Name, Info)) {
184         // FIXME: We temporarily return false
185         // so we can add more constraints as we hit it.
186         // Eventually, an unknown constraint should just be treated as 'g'.
187         return false;
188       }
189     case '&': // early clobber.
190       break;
191     case 'r': // general register.
192       Info.setAllowsRegister();
193       break;
194     case 'm': // memory operand.
195       Info.setAllowsMemory();
196       break;
197     case 'g': // general register, memory operand or immediate integer.
198     case 'X': // any operand.
199       Info.setAllowsRegister();
200       Info.setAllowsMemory();
201       break;
202     }
203 
204     Name++;
205   }
206 
207   return true;
208 }
209 
210 bool TargetInfo::resolveSymbolicName(const char *&Name,
211                                      ConstraintInfo *OutputConstraints,
212                                      unsigned NumOutputs,
213                                      unsigned &Index) const {
214   assert(*Name == '[' && "Symbolic name did not start with '['");
215   Name++;
216   const char *Start = Name;
217   while (*Name && *Name != ']')
218     Name++;
219 
220   if (!*Name) {
221     // Missing ']'
222     return false;
223   }
224 
225   std::string SymbolicName(Start, Name - Start);
226 
227   for (Index = 0; Index != NumOutputs; ++Index)
228     if (SymbolicName == OutputConstraints[Index].getName())
229       return true;
230 
231   return false;
232 }
233 
234 bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
235                                          unsigned NumOutputs,
236                                          ConstraintInfo &Info) const {
237   const char *Name = Info.ConstraintStr.c_str();
238 
239   while (*Name) {
240     switch (*Name) {
241     default:
242       // Check if we have a matching constraint
243       if (*Name >= '0' && *Name <= '9') {
244         unsigned i = *Name - '0';
245 
246         // Check if matching constraint is out of bounds.
247         if (i >= NumOutputs)
248           return false;
249 
250         // The constraint should have the same info as the respective
251         // output constraint.
252         Info.setTiedOperand(i, OutputConstraints[i]);
253       } else if (!validateAsmConstraint(Name, Info)) {
254         // FIXME: This error return is in place temporarily so we can
255         // add more constraints as we hit it.  Eventually, an unknown
256         // constraint should just be treated as 'g'.
257         return false;
258       }
259       break;
260     case '[': {
261       unsigned Index = 0;
262       if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
263         return false;
264 
265       break;
266     }
267     case '%': // commutative
268       // FIXME: Fail if % is used with the last operand.
269       break;
270     case 'i': // immediate integer.
271     case 'n': // immediate integer with a known value.
272       break;
273     case 'I':  // Various constant constraints with target-specific meanings.
274     case 'J':
275     case 'K':
276     case 'L':
277     case 'M':
278     case 'N':
279     case 'O':
280     case 'P':
281       break;
282     case 'r': // general register.
283       Info.setAllowsRegister();
284       break;
285     case 'm': // memory operand.
286       Info.setAllowsMemory();
287       break;
288     case 'g': // general register, memory operand or immediate integer.
289     case 'X': // any operand.
290       Info.setAllowsRegister();
291       Info.setAllowsMemory();
292       break;
293     }
294 
295     Name++;
296   }
297 
298   return true;
299 }
300