blob: 12caf0c8e512e9e7d0fadcada9c681f1d6b54638 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
Chris Lattner858eece2007-09-22 18:29:59 +000015#include "llvm/ADT/APFloat.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner5c81ea02008-04-06 04:02:29 +000017#include <cstdlib>
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
Chris Lattner3b74cac2008-03-08 08:59:43 +000020// TargetInfo Constructor.
21TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedman0b7b1cb2008-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 Friedman8f575172009-04-19 21:38:35 +000025 TLSSupported = true;
Chris Lattner727b3c42008-05-09 06:08:39 +000026 PointerWidth = PointerAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000027 WCharWidth = WCharAlign = 32;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +000028 Char16Width = Char16Align = 16;
29 Char32Width = Char32Align = 32;
Chris Lattner85970f32008-05-08 05:58:21 +000030 IntWidth = IntAlign = 32;
Chris Lattner481dcf22008-05-09 05:50:02 +000031 LongWidth = LongAlign = 32;
32 LongLongWidth = LongLongAlign = 64;
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000033 FloatWidth = 32;
34 FloatAlign = 32;
Nate Begemand5e35f82008-04-18 17:17:24 +000035 DoubleWidth = 64;
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000036 DoubleAlign = 64;
37 LongDoubleWidth = 64;
38 LongDoubleAlign = 64;
Nate Begeman2aaa0a02009-01-17 23:56:13 +000039 IntMaxTWidth = 64;
Anders Carlssonc0b56cd2008-10-31 16:05:19 +000040 SizeType = UnsignedLong;
Eli Friedman7cca0982008-11-02 02:43:55 +000041 PtrDiffType = SignedLong;
Sanjiv Guptafa451432008-10-31 09:52:39 +000042 IntMaxType = SignedLongLong;
43 UIntMaxType = UnsignedLongLong;
Chris Lattnerd9ef7242009-02-13 22:28:55 +000044 IntPtrType = SignedLong;
Eli Friedman7cca0982008-11-02 02:43:55 +000045 WCharType = SignedInt;
Chris Lattner72f4aae2009-10-21 04:59:34 +000046 WIntType = SignedInt;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +000047 Char16Type = UnsignedShort;
48 Char32Type = UnsignedInt;
Eli Friedman38e31802009-07-01 03:36:11 +000049 Int64Type = SignedLongLong;
Chris Lattner3b74cac2008-03-08 08:59:43 +000050 FloatFormat = &llvm::APFloat::IEEEsingle;
51 DoubleFormat = &llvm::APFloat::IEEEdouble;
52 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman2b161652008-08-21 00:13:15 +000053 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
54 "i64:64:64-f32:32:32-f64:64:64";
Chris Lattner3f6d8cf2008-10-05 19:22:37 +000055 UserLabelPrefix = "_";
Chris Lattner3b74cac2008-03-08 08:59:43 +000056}
57
Chris Lattner0fb8d852008-03-08 08:24:01 +000058// Out of line virtual dtor for TargetInfo.
59TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000060
Chris Lattner534234c2009-02-06 05:04:11 +000061/// getTypeName - Return the user string for the specified integer type enum.
62/// For example, SignedShort -> "short".
63const char *TargetInfo::getTypeName(IntType T) {
64 switch (T) {
65 default: assert(0 && "not an integer!");
66 case SignedShort: return "short";
67 case UnsignedShort: return "unsigned short";
68 case SignedInt: return "int";
69 case UnsignedInt: return "unsigned int";
70 case SignedLong: return "long int";
71 case UnsignedLong: return "long unsigned int";
72 case SignedLongLong: return "long long int";
73 case UnsignedLongLong: return "long long unsigned int";
74 }
75}
76
Chris Lattner40a75fe2009-10-21 06:24:21 +000077/// getTypeConstantSuffix - Return the constant suffix for the specified
78/// integer type enum. For example, SignedLong -> "L".
79const char *TargetInfo::getTypeConstantSuffix(IntType T) {
80 switch (T) {
81 default: assert(0 && "not an integer!");
82 case SignedShort:
83 case SignedInt: return "";
84 case SignedLong: return "L";
85 case SignedLongLong: return "LL";
86 case UnsignedShort:
87 case UnsignedInt: return "U";
88 case UnsignedLong: return "UL";
89 case UnsignedLongLong: return "ULL";
90 }
91}
92
93/// getTypeWidth - Return the width (in bits) of the specified integer type
94/// enum. For example, SignedInt -> getIntWidth().
95unsigned TargetInfo::getTypeWidth(IntType T) const {
96 switch (T) {
97 default: assert(0 && "not an integer!");
98 case SignedShort: return getShortWidth();
99 case UnsignedShort: return getShortWidth();
100 case SignedInt: return getIntWidth();
101 case UnsignedInt: return getIntWidth();
102 case SignedLong: return getLongWidth();
103 case UnsignedLong: return getLongWidth();
104 case SignedLongLong: return getLongLongWidth();
105 case UnsignedLongLong: return getLongLongWidth();
106 };
107}
108
Chris Lattner3b822f12009-10-25 22:49:18 +0000109/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner40a75fe2009-10-21 06:24:21 +0000110/// the type is signed; false otherwise.
Chris Lattner3b822f12009-10-25 22:49:18 +0000111bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattner40a75fe2009-10-21 06:24:21 +0000112 switch (T) {
113 default: assert(0 && "not an integer!");
114 case SignedShort:
115 case SignedInt:
116 case SignedLong:
117 case SignedLongLong:
118 return true;
119 case UnsignedShort:
120 case UnsignedInt:
121 case UnsignedLong:
122 case UnsignedLongLong:
123 return false;
124 };
125}
126
127
Chris Lattner858eece2007-09-22 18:29:59 +0000128//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +0000129
Chris Lattner4b009652007-07-25 00:24:17 +0000130
Chris Lattnerfc457002008-03-05 01:18:20 +0000131static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +0000132 if (Name[0] == '%' || Name[0] == '#')
133 Name++;
134}
135
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000136/// isValidGCCRegisterName - Returns whether the passed in string
137/// is a valid register name according to GCC. This is used by Sema for
138/// inline asm statements.
139bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +0000140 const char * const *Names;
141 unsigned NumNames;
Mike Stump25cf7602009-09-09 15:08:12 +0000142
Anders Carlsson49dadd62007-11-25 00:25:21 +0000143 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +0000144 removeGCCRegisterPrefix(Name);
145
Mike Stump25cf7602009-09-09 15:08:12 +0000146
Anders Carlsson49dadd62007-11-25 00:25:21 +0000147 if (strcmp(Name, "memory") == 0 ||
148 strcmp(Name, "cc") == 0)
149 return true;
Mike Stump25cf7602009-09-09 15:08:12 +0000150
Chris Lattner0fb8d852008-03-08 08:24:01 +0000151 getGCCRegNames(Names, NumNames);
Mike Stump25cf7602009-09-09 15:08:12 +0000152
Anders Carlsson49dadd62007-11-25 00:25:21 +0000153 // If we have a number it maps to an entry in the register name array.
154 if (isdigit(Name[0])) {
155 char *End;
156 int n = (int)strtol(Name, &End, 0);
157 if (*End == 0)
158 return n >= 0 && (unsigned)n < NumNames;
159 }
160
161 // Check register names.
162 for (unsigned i = 0; i < NumNames; i++) {
163 if (strcmp(Name, Names[i]) == 0)
164 return true;
165 }
Mike Stump25cf7602009-09-09 15:08:12 +0000166
Anders Carlsson49dadd62007-11-25 00:25:21 +0000167 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000168 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +0000169 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000170
Chris Lattner0fb8d852008-03-08 08:24:01 +0000171 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +0000172 for (unsigned i = 0; i < NumAliases; i++) {
173 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
174 if (!Aliases[i].Aliases[j])
175 break;
176 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
177 return true;
178 }
179 }
Mike Stump25cf7602009-09-09 15:08:12 +0000180
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000181 return false;
182}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000183
Chris Lattner0fb8d852008-03-08 08:24:01 +0000184const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000185 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump25cf7602009-09-09 15:08:12 +0000186
Anders Carlsson74385982008-02-06 00:11:32 +0000187 removeGCCRegisterPrefix(Name);
Mike Stump25cf7602009-09-09 15:08:12 +0000188
Anders Carlsson4ce42302007-11-27 04:11:28 +0000189 const char * const *Names;
190 unsigned NumNames;
191
Chris Lattner0fb8d852008-03-08 08:24:01 +0000192 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000193
194 // First, check if we have a number.
195 if (isdigit(Name[0])) {
196 char *End;
197 int n = (int)strtol(Name, &End, 0);
198 if (*End == 0) {
Mike Stump25cf7602009-09-09 15:08:12 +0000199 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlsson4ce42302007-11-27 04:11:28 +0000200 "Out of bounds register number!");
201 return Names[n];
202 }
203 }
Mike Stump25cf7602009-09-09 15:08:12 +0000204
Anders Carlsson4ce42302007-11-27 04:11:28 +0000205 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000206 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000207 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000208
Chris Lattner0fb8d852008-03-08 08:24:01 +0000209 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000210 for (unsigned i = 0; i < NumAliases; i++) {
211 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
212 if (!Aliases[i].Aliases[j])
213 break;
214 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
215 return Aliases[i].Register;
216 }
217 }
Mike Stump25cf7602009-09-09 15:08:12 +0000218
Anders Carlsson4ce42302007-11-27 04:11:28 +0000219 return Name;
220}
221
Chris Lattner9f8e5022009-04-26 17:19:08 +0000222bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
223 const char *Name = Info.getConstraintStr().c_str();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000224 // An output constraint must start with '=' or '+'
225 if (*Name != '=' && *Name != '+')
226 return false;
227
228 if (*Name == '+')
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000229 Info.setIsReadWrite();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000230
231 Name++;
232 while (*Name) {
233 switch (*Name) {
234 default:
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000235 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000236 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000237 // so we can add more constraints as we hit it.
238 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000239 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000240 }
241 case '&': // early clobber.
242 break;
Chris Lattner79c224b2009-10-13 04:32:07 +0000243 case '%': // commutative.
244 // FIXME: Check that there is a another register after this one.
245 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000246 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000247 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000248 break;
249 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000250 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000251 break;
252 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000253 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000254 Info.setAllowsRegister();
255 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000256 break;
257 }
Mike Stump25cf7602009-09-09 15:08:12 +0000258
Anders Carlsson4ce42302007-11-27 04:11:28 +0000259 Name++;
260 }
Mike Stump25cf7602009-09-09 15:08:12 +0000261
Anders Carlsson4ce42302007-11-27 04:11:28 +0000262 return true;
263}
264
Anders Carlssonb5c85692009-01-18 01:56:57 +0000265bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner0cd323d2009-04-26 17:57:12 +0000266 ConstraintInfo *OutputConstraints,
267 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000268 unsigned &Index) const {
Anders Carlssonb5c85692009-01-18 01:56:57 +0000269 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssonb5c85692009-01-18 01:56:57 +0000270 Name++;
271 const char *Start = Name;
272 while (*Name && *Name != ']')
273 Name++;
Mike Stump25cf7602009-09-09 15:08:12 +0000274
Anders Carlssonb5c85692009-01-18 01:56:57 +0000275 if (!*Name) {
276 // Missing ']'
277 return false;
278 }
Mike Stump25cf7602009-09-09 15:08:12 +0000279
Anders Carlssonb5c85692009-01-18 01:56:57 +0000280 std::string SymbolicName(Start, Name - Start);
Mike Stump25cf7602009-09-09 15:08:12 +0000281
Chris Lattner0cd323d2009-04-26 17:57:12 +0000282 for (Index = 0; Index != NumOutputs; ++Index)
283 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssonb5c85692009-01-18 01:56:57 +0000284 return true;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000285
286 return false;
287}
288
Chris Lattner0cd323d2009-04-26 17:57:12 +0000289bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
290 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000291 ConstraintInfo &Info) const {
Chris Lattner9f8e5022009-04-26 17:19:08 +0000292 const char *Name = Info.ConstraintStr.c_str();
293
Anders Carlsson4ce42302007-11-27 04:11:28 +0000294 while (*Name) {
295 switch (*Name) {
296 default:
297 // Check if we have a matching constraint
298 if (*Name >= '0' && *Name <= '9') {
299 unsigned i = *Name - '0';
Mike Stump25cf7602009-09-09 15:08:12 +0000300
Anders Carlsson4ce42302007-11-27 04:11:28 +0000301 // Check if matching constraint is out of bounds.
302 if (i >= NumOutputs)
303 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000304
305 // The constraint should have the same info as the respective
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000306 // output constraint.
Chris Lattner9eb395f2009-04-26 18:05:25 +0000307 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000308 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar157e1e62008-08-25 09:46:27 +0000309 // FIXME: This error return is in place temporarily so we can
310 // add more constraints as we hit it. Eventually, an unknown
311 // constraint should just be treated as 'g'.
312 return false;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000313 }
314 break;
315 case '[': {
316 unsigned Index = 0;
Chris Lattner0cd323d2009-04-26 17:57:12 +0000317 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssonb5c85692009-01-18 01:56:57 +0000318 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000319
Anders Carlssonb5c85692009-01-18 01:56:57 +0000320 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000321 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000322 case '%': // commutative
323 // FIXME: Fail if % is used with the last operand.
324 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000325 case 'i': // immediate integer.
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000326 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000327 break;
Chris Lattnerf803ad82009-05-06 04:33:31 +0000328 case 'I': // Various constant constraints with target-specific meanings.
329 case 'J':
330 case 'K':
331 case 'L':
332 case 'M':
333 case 'N':
334 case 'O':
335 case 'P':
336 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000337 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000338 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000339 break;
340 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000341 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000342 break;
343 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000344 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000345 Info.setAllowsRegister();
346 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000347 break;
348 }
Mike Stump25cf7602009-09-09 15:08:12 +0000349
Anders Carlsson4ce42302007-11-27 04:11:28 +0000350 Name++;
351 }
Mike Stump25cf7602009-09-09 15:08:12 +0000352
Anders Carlsson4ce42302007-11-27 04:11:28 +0000353 return true;
354}