blob: a513cb16886ed5d48d85063d960361cdb762ab25 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
Chris Lattner525a0502007-09-22 18:29:59 +000015#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner5483bdf2008-04-06 04:02:29 +000017#include <cstdlib>
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Chris Lattnercd4fc422008-03-08 08:59:43 +000020// TargetInfo Constructor.
21TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedman61538a72008-05-20 14:21:01 +000022 // 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.
Eli Friedmanb030f022009-04-19 21:38:35 +000025 TLSSupported = true;
Chris Lattner927686f2008-05-09 06:08:39 +000026 PointerWidth = PointerAlign = 32;
Chris Lattnercd4fc422008-03-08 08:59:43 +000027 WCharWidth = WCharAlign = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000029 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000031 FloatWidth = 32;
32 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000033 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000034 DoubleAlign = 64;
35 LongDoubleWidth = 64;
36 LongDoubleAlign = 64;
Nate Begeman22b9d5a2009-01-17 23:56:13 +000037 IntMaxTWidth = 64;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000038 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000039 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000040 IntMaxType = SignedLongLong;
41 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000042 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000043 WCharType = SignedInt;
Chris Lattnercd4fc422008-03-08 08:59:43 +000044 FloatFormat = &llvm::APFloat::IEEEsingle;
45 DoubleFormat = &llvm::APFloat::IEEEdouble;
46 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000047 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
48 "i64:64:64-f32:32:32-f64:64:64";
Chris Lattner3fdf4672008-10-05 19:22:37 +000049 UserLabelPrefix = "_";
Chris Lattnercd4fc422008-03-08 08:59:43 +000050}
51
Chris Lattner0eaed122008-03-08 08:24:01 +000052// Out of line virtual dtor for TargetInfo.
53TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000054
Chris Lattner2b5abf52009-02-06 05:04:11 +000055/// getTypeName - Return the user string for the specified integer type enum.
56/// For example, SignedShort -> "short".
57const char *TargetInfo::getTypeName(IntType T) {
58 switch (T) {
59 default: assert(0 && "not an integer!");
60 case SignedShort: return "short";
61 case UnsignedShort: return "unsigned short";
62 case SignedInt: return "int";
63 case UnsignedInt: return "unsigned int";
64 case SignedLong: return "long int";
65 case UnsignedLong: return "long unsigned int";
66 case SignedLongLong: return "long long int";
67 case UnsignedLongLong: return "long long unsigned int";
68 }
69}
70
Chris Lattner525a0502007-09-22 18:29:59 +000071//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +000072
Reid Spencer5f016e22007-07-11 17:01:13 +000073
Chris Lattner42e67372008-03-05 01:18:20 +000074static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlssonea041752008-02-06 00:11:32 +000075 if (Name[0] == '%' || Name[0] == '#')
76 Name++;
77}
78
Anders Carlsson3346ae62007-11-24 23:38:12 +000079/// isValidGCCRegisterName - Returns whether the passed in string
80/// is a valid register name according to GCC. This is used by Sema for
81/// inline asm statements.
82bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +000083 const char * const *Names;
84 unsigned NumNames;
85
86 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +000087 removeGCCRegisterPrefix(Name);
88
Anders Carlsson6fa90862007-11-25 00:25:21 +000089
90 if (strcmp(Name, "memory") == 0 ||
91 strcmp(Name, "cc") == 0)
92 return true;
93
Chris Lattner0eaed122008-03-08 08:24:01 +000094 getGCCRegNames(Names, NumNames);
Anders Carlsson6fa90862007-11-25 00:25:21 +000095
96 // If we have a number it maps to an entry in the register name array.
97 if (isdigit(Name[0])) {
98 char *End;
99 int n = (int)strtol(Name, &End, 0);
100 if (*End == 0)
101 return n >= 0 && (unsigned)n < NumNames;
102 }
103
104 // Check register names.
105 for (unsigned i = 0; i < NumNames; i++) {
106 if (strcmp(Name, Names[i]) == 0)
107 return true;
108 }
109
110 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000111 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000112 unsigned NumAliases;
113
Chris Lattner0eaed122008-03-08 08:24:01 +0000114 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000115 for (unsigned i = 0; i < NumAliases; i++) {
116 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
117 if (!Aliases[i].Aliases[j])
118 break;
119 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
120 return true;
121 }
122 }
123
Anders Carlsson3346ae62007-11-24 23:38:12 +0000124 return false;
125}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000126
Chris Lattner0eaed122008-03-08 08:24:01 +0000127const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000128 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
129
Anders Carlssonea041752008-02-06 00:11:32 +0000130 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000131
Anders Carlssond04c6e22007-11-27 04:11:28 +0000132 const char * const *Names;
133 unsigned NumNames;
134
Chris Lattner0eaed122008-03-08 08:24:01 +0000135 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000136
137 // First, check if we have a number.
138 if (isdigit(Name[0])) {
139 char *End;
140 int n = (int)strtol(Name, &End, 0);
141 if (*End == 0) {
142 assert(n >= 0 && (unsigned)n < NumNames &&
143 "Out of bounds register number!");
144 return Names[n];
145 }
146 }
147
148 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000149 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000150 unsigned NumAliases;
151
Chris Lattner0eaed122008-03-08 08:24:01 +0000152 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000153 for (unsigned i = 0; i < NumAliases; i++) {
154 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
155 if (!Aliases[i].Aliases[j])
156 break;
157 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
158 return Aliases[i].Register;
159 }
160 }
161
162 return Name;
163}
164
Chris Lattner432c8692009-04-26 17:19:08 +0000165bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
166 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000167 // An output constraint must start with '=' or '+'
168 if (*Name != '=' && *Name != '+')
169 return false;
170
171 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000172 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000173
174 Name++;
175 while (*Name) {
176 switch (*Name) {
177 default:
Chris Lattner44def072009-04-26 07:16:29 +0000178 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000179 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000180 // so we can add more constraints as we hit it.
181 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000182 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000183 }
184 case '&': // early clobber.
185 break;
186 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000187 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000188 break;
189 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000190 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000191 break;
192 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000193 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000194 Info.setAllowsRegister();
195 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000196 break;
197 }
198
199 Name++;
200 }
201
202 return true;
203}
204
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000205bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000206 ConstraintInfo *OutputConstraints,
207 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000208 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000209 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000210 Name++;
211 const char *Start = Name;
212 while (*Name && *Name != ']')
213 Name++;
214
215 if (!*Name) {
216 // Missing ']'
217 return false;
218 }
219
220 std::string SymbolicName(Start, Name - Start);
221
Chris Lattner2819fa82009-04-26 17:57:12 +0000222 for (Index = 0; Index != NumOutputs; ++Index)
223 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000224 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000225
226 return false;
227}
228
Chris Lattner2819fa82009-04-26 17:57:12 +0000229bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
230 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000231 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000232 const char *Name = Info.ConstraintStr.c_str();
233
Anders Carlssond04c6e22007-11-27 04:11:28 +0000234 while (*Name) {
235 switch (*Name) {
236 default:
237 // Check if we have a matching constraint
238 if (*Name >= '0' && *Name <= '9') {
239 unsigned i = *Name - '0';
Anders Carlsson45b050e2009-01-17 23:36:15 +0000240
Anders Carlssond04c6e22007-11-27 04:11:28 +0000241 // Check if matching constraint is out of bounds.
242 if (i >= NumOutputs)
243 return false;
Anders Carlsson03eb5432009-01-27 20:38:24 +0000244
245 // The constraint should have the same info as the respective
246 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000247 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000248 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000249 // FIXME: This error return is in place temporarily so we can
250 // add more constraints as we hit it. Eventually, an unknown
251 // constraint should just be treated as 'g'.
252 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000253 }
254 break;
255 case '[': {
256 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000257 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000258 return false;
259
260 break;
261 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000262 case '%': // commutative
263 // FIXME: Fail if % is used with the last operand.
264 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000265 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000266 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000267 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000268 case 'I': // Various constant constraints with target-specific meanings.
269 case 'J':
270 case 'K':
271 case 'L':
272 case 'M':
273 case 'N':
274 case 'O':
275 case 'P':
276 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000277 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000278 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000279 break;
280 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000281 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000282 break;
283 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000284 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000285 Info.setAllowsRegister();
286 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000287 break;
288 }
289
290 Name++;
291 }
292
293 return true;
294}