blob: 1318130b8779e62d43a7087f3979601f70fa8650 [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"
John Thompsone65800a2009-11-05 20:14:16 +000015#include "clang/Basic/LangOptions.h"
Chris Lattner858eece2007-09-22 18:29:59 +000016#include "llvm/ADT/APFloat.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner5c81ea02008-04-06 04:02:29 +000018#include <cstdlib>
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattner3b74cac2008-03-08 08:59:43 +000021// TargetInfo Constructor.
22TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000023 // Set defaults. Defaults are set for a 32-bit RISC platform,
24 // like PPC or SPARC.
25 // These should be overridden by concrete targets as needed.
Eli Friedman8f575172009-04-19 21:38:35 +000026 TLSSupported = true;
Chris Lattner727b3c42008-05-09 06:08:39 +000027 PointerWidth = PointerAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000028 WCharWidth = WCharAlign = 32;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +000029 Char16Width = Char16Align = 16;
30 Char32Width = Char32Align = 32;
Chris Lattner85970f32008-05-08 05:58:21 +000031 IntWidth = IntAlign = 32;
Chris Lattner481dcf22008-05-09 05:50:02 +000032 LongWidth = LongAlign = 32;
33 LongLongWidth = LongLongAlign = 64;
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000034 FloatWidth = 32;
35 FloatAlign = 32;
Nate Begemand5e35f82008-04-18 17:17:24 +000036 DoubleWidth = 64;
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000037 DoubleAlign = 64;
38 LongDoubleWidth = 64;
39 LongDoubleAlign = 64;
Nate Begeman2aaa0a02009-01-17 23:56:13 +000040 IntMaxTWidth = 64;
Anders Carlssonc0b56cd2008-10-31 16:05:19 +000041 SizeType = UnsignedLong;
Eli Friedman7cca0982008-11-02 02:43:55 +000042 PtrDiffType = SignedLong;
Sanjiv Guptafa451432008-10-31 09:52:39 +000043 IntMaxType = SignedLongLong;
44 UIntMaxType = UnsignedLongLong;
Chris Lattnerd9ef7242009-02-13 22:28:55 +000045 IntPtrType = SignedLong;
Eli Friedman7cca0982008-11-02 02:43:55 +000046 WCharType = SignedInt;
Chris Lattner72f4aae2009-10-21 04:59:34 +000047 WIntType = SignedInt;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +000048 Char16Type = UnsignedShort;
49 Char32Type = UnsignedInt;
Eli Friedman38e31802009-07-01 03:36:11 +000050 Int64Type = SignedLongLong;
Chris Lattner3b74cac2008-03-08 08:59:43 +000051 FloatFormat = &llvm::APFloat::IEEEsingle;
52 DoubleFormat = &llvm::APFloat::IEEEdouble;
53 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman2b161652008-08-21 00:13:15 +000054 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
Chris Lattner2e17d912009-11-07 18:59:41 +000055 "i64:64:64-f32:32:32-f64:64:64-n32";
Chris Lattner3f6d8cf2008-10-05 19:22:37 +000056 UserLabelPrefix = "_";
Chris Lattner3b74cac2008-03-08 08:59:43 +000057}
58
Chris Lattner0fb8d852008-03-08 08:24:01 +000059// Out of line virtual dtor for TargetInfo.
60TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000061
Chris Lattner534234c2009-02-06 05:04:11 +000062/// getTypeName - Return the user string for the specified integer type enum.
63/// For example, SignedShort -> "short".
64const char *TargetInfo::getTypeName(IntType T) {
65 switch (T) {
66 default: assert(0 && "not an integer!");
67 case SignedShort: return "short";
68 case UnsignedShort: return "unsigned short";
69 case SignedInt: return "int";
70 case UnsignedInt: return "unsigned int";
71 case SignedLong: return "long int";
72 case UnsignedLong: return "long unsigned int";
73 case SignedLongLong: return "long long int";
74 case UnsignedLongLong: return "long long unsigned int";
75 }
76}
77
Chris Lattner40a75fe2009-10-21 06:24:21 +000078/// getTypeConstantSuffix - Return the constant suffix for the specified
79/// integer type enum. For example, SignedLong -> "L".
80const char *TargetInfo::getTypeConstantSuffix(IntType T) {
81 switch (T) {
82 default: assert(0 && "not an integer!");
83 case SignedShort:
84 case SignedInt: return "";
85 case SignedLong: return "L";
86 case SignedLongLong: return "LL";
87 case UnsignedShort:
88 case UnsignedInt: return "U";
89 case UnsignedLong: return "UL";
90 case UnsignedLongLong: return "ULL";
91 }
92}
93
94/// getTypeWidth - Return the width (in bits) of the specified integer type
95/// enum. For example, SignedInt -> getIntWidth().
96unsigned TargetInfo::getTypeWidth(IntType T) const {
97 switch (T) {
98 default: assert(0 && "not an integer!");
Chris Lattnercffa5b702009-11-05 21:21:32 +000099 case SignedShort:
Chris Lattner40a75fe2009-10-21 06:24:21 +0000100 case UnsignedShort: return getShortWidth();
Chris Lattnercffa5b702009-11-05 21:21:32 +0000101 case SignedInt:
Chris Lattner40a75fe2009-10-21 06:24:21 +0000102 case UnsignedInt: return getIntWidth();
Chris Lattnercffa5b702009-11-05 21:21:32 +0000103 case SignedLong:
Chris Lattner40a75fe2009-10-21 06:24:21 +0000104 case UnsignedLong: return getLongWidth();
Chris Lattnercffa5b702009-11-05 21:21:32 +0000105 case SignedLongLong:
Chris Lattner40a75fe2009-10-21 06:24:21 +0000106 case UnsignedLongLong: return getLongLongWidth();
107 };
108}
109
Chris Lattnercffa5b702009-11-05 21:21:32 +0000110/// getTypeAlign - Return the alignment (in bits) of the specified integer type
111/// enum. For example, SignedInt -> getIntAlign().
112unsigned TargetInfo::getTypeAlign(IntType T) const {
113 switch (T) {
114 default: assert(0 && "not an integer!");
115 case SignedShort:
116 case UnsignedShort: return getShortAlign();
117 case SignedInt:
118 case UnsignedInt: return getIntAlign();
119 case SignedLong:
120 case UnsignedLong: return getLongAlign();
121 case SignedLongLong:
122 case UnsignedLongLong: return getLongLongAlign();
123 };
124}
125
Chris Lattner3b822f12009-10-25 22:49:18 +0000126/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner40a75fe2009-10-21 06:24:21 +0000127/// the type is signed; false otherwise.
Chris Lattner3b822f12009-10-25 22:49:18 +0000128bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattner40a75fe2009-10-21 06:24:21 +0000129 switch (T) {
130 default: assert(0 && "not an integer!");
131 case SignedShort:
132 case SignedInt:
133 case SignedLong:
134 case SignedLongLong:
135 return true;
136 case UnsignedShort:
137 case UnsignedInt:
138 case UnsignedLong:
139 case UnsignedLongLong:
140 return false;
141 };
142}
143
John Thompsone65800a2009-11-05 20:14:16 +0000144/// setForcedLangOptions - Set forced language options.
145/// Apply changes to the target information with respect to certain
146/// language options which change the target configuration.
147void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
John Thompson41767052009-11-05 22:03:02 +0000148 if (Opts.ShortWChar) {
John Thompsone65800a2009-11-05 20:14:16 +0000149 WCharType = UnsignedShort;
150 WCharWidth = WCharAlign = 16;
John Thompson41767052009-11-05 22:03:02 +0000151 }
John Thompsone65800a2009-11-05 20:14:16 +0000152}
Chris Lattner40a75fe2009-10-21 06:24:21 +0000153
Chris Lattner858eece2007-09-22 18:29:59 +0000154//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +0000155
Chris Lattner4b009652007-07-25 00:24:17 +0000156
Chris Lattnerfc457002008-03-05 01:18:20 +0000157static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +0000158 if (Name[0] == '%' || Name[0] == '#')
159 Name++;
160}
161
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000162/// isValidGCCRegisterName - Returns whether the passed in string
163/// is a valid register name according to GCC. This is used by Sema for
164/// inline asm statements.
165bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +0000166 const char * const *Names;
167 unsigned NumNames;
Mike Stump25cf7602009-09-09 15:08:12 +0000168
Anders Carlsson49dadd62007-11-25 00:25:21 +0000169 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +0000170 removeGCCRegisterPrefix(Name);
171
Mike Stump25cf7602009-09-09 15:08:12 +0000172
Anders Carlsson49dadd62007-11-25 00:25:21 +0000173 if (strcmp(Name, "memory") == 0 ||
174 strcmp(Name, "cc") == 0)
175 return true;
Mike Stump25cf7602009-09-09 15:08:12 +0000176
Chris Lattner0fb8d852008-03-08 08:24:01 +0000177 getGCCRegNames(Names, NumNames);
Mike Stump25cf7602009-09-09 15:08:12 +0000178
Anders Carlsson49dadd62007-11-25 00:25:21 +0000179 // If we have a number it maps to an entry in the register name array.
180 if (isdigit(Name[0])) {
181 char *End;
182 int n = (int)strtol(Name, &End, 0);
183 if (*End == 0)
184 return n >= 0 && (unsigned)n < NumNames;
185 }
186
187 // Check register names.
188 for (unsigned i = 0; i < NumNames; i++) {
189 if (strcmp(Name, Names[i]) == 0)
190 return true;
191 }
Mike Stump25cf7602009-09-09 15:08:12 +0000192
Anders Carlsson49dadd62007-11-25 00:25:21 +0000193 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000194 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +0000195 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000196
Chris Lattner0fb8d852008-03-08 08:24:01 +0000197 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +0000198 for (unsigned i = 0; i < NumAliases; i++) {
199 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
200 if (!Aliases[i].Aliases[j])
201 break;
202 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
203 return true;
204 }
205 }
Mike Stump25cf7602009-09-09 15:08:12 +0000206
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000207 return false;
208}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000209
Chris Lattner0fb8d852008-03-08 08:24:01 +0000210const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000211 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump25cf7602009-09-09 15:08:12 +0000212
Anders Carlsson74385982008-02-06 00:11:32 +0000213 removeGCCRegisterPrefix(Name);
Mike Stump25cf7602009-09-09 15:08:12 +0000214
Anders Carlsson4ce42302007-11-27 04:11:28 +0000215 const char * const *Names;
216 unsigned NumNames;
217
Chris Lattner0fb8d852008-03-08 08:24:01 +0000218 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000219
220 // First, check if we have a number.
221 if (isdigit(Name[0])) {
222 char *End;
223 int n = (int)strtol(Name, &End, 0);
224 if (*End == 0) {
Mike Stump25cf7602009-09-09 15:08:12 +0000225 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlsson4ce42302007-11-27 04:11:28 +0000226 "Out of bounds register number!");
227 return Names[n];
228 }
229 }
Mike Stump25cf7602009-09-09 15:08:12 +0000230
Anders Carlsson4ce42302007-11-27 04:11:28 +0000231 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000232 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000233 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000234
Chris Lattner0fb8d852008-03-08 08:24:01 +0000235 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000236 for (unsigned i = 0; i < NumAliases; i++) {
237 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
238 if (!Aliases[i].Aliases[j])
239 break;
240 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
241 return Aliases[i].Register;
242 }
243 }
Mike Stump25cf7602009-09-09 15:08:12 +0000244
Anders Carlsson4ce42302007-11-27 04:11:28 +0000245 return Name;
246}
247
Chris Lattner9f8e5022009-04-26 17:19:08 +0000248bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
249 const char *Name = Info.getConstraintStr().c_str();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000250 // An output constraint must start with '=' or '+'
251 if (*Name != '=' && *Name != '+')
252 return false;
253
254 if (*Name == '+')
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000255 Info.setIsReadWrite();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000256
257 Name++;
258 while (*Name) {
259 switch (*Name) {
260 default:
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000261 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000262 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000263 // so we can add more constraints as we hit it.
264 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000265 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000266 }
267 case '&': // early clobber.
268 break;
Chris Lattner79c224b2009-10-13 04:32:07 +0000269 case '%': // commutative.
270 // FIXME: Check that there is a another register after this one.
271 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000272 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000273 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000274 break;
275 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000276 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000277 break;
278 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000279 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000280 Info.setAllowsRegister();
281 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000282 break;
283 }
Mike Stump25cf7602009-09-09 15:08:12 +0000284
Anders Carlsson4ce42302007-11-27 04:11:28 +0000285 Name++;
286 }
Mike Stump25cf7602009-09-09 15:08:12 +0000287
Anders Carlsson4ce42302007-11-27 04:11:28 +0000288 return true;
289}
290
Anders Carlssonb5c85692009-01-18 01:56:57 +0000291bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner0cd323d2009-04-26 17:57:12 +0000292 ConstraintInfo *OutputConstraints,
293 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000294 unsigned &Index) const {
Anders Carlssonb5c85692009-01-18 01:56:57 +0000295 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssonb5c85692009-01-18 01:56:57 +0000296 Name++;
297 const char *Start = Name;
298 while (*Name && *Name != ']')
299 Name++;
Mike Stump25cf7602009-09-09 15:08:12 +0000300
Anders Carlssonb5c85692009-01-18 01:56:57 +0000301 if (!*Name) {
302 // Missing ']'
303 return false;
304 }
Mike Stump25cf7602009-09-09 15:08:12 +0000305
Anders Carlssonb5c85692009-01-18 01:56:57 +0000306 std::string SymbolicName(Start, Name - Start);
Mike Stump25cf7602009-09-09 15:08:12 +0000307
Chris Lattner0cd323d2009-04-26 17:57:12 +0000308 for (Index = 0; Index != NumOutputs; ++Index)
309 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssonb5c85692009-01-18 01:56:57 +0000310 return true;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000311
312 return false;
313}
314
Chris Lattner0cd323d2009-04-26 17:57:12 +0000315bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
316 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000317 ConstraintInfo &Info) const {
Chris Lattner9f8e5022009-04-26 17:19:08 +0000318 const char *Name = Info.ConstraintStr.c_str();
319
Anders Carlsson4ce42302007-11-27 04:11:28 +0000320 while (*Name) {
321 switch (*Name) {
322 default:
323 // Check if we have a matching constraint
324 if (*Name >= '0' && *Name <= '9') {
325 unsigned i = *Name - '0';
Mike Stump25cf7602009-09-09 15:08:12 +0000326
Anders Carlsson4ce42302007-11-27 04:11:28 +0000327 // Check if matching constraint is out of bounds.
328 if (i >= NumOutputs)
329 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000330
331 // The constraint should have the same info as the respective
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000332 // output constraint.
Chris Lattner9eb395f2009-04-26 18:05:25 +0000333 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000334 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar157e1e62008-08-25 09:46:27 +0000335 // FIXME: This error return is in place temporarily so we can
336 // add more constraints as we hit it. Eventually, an unknown
337 // constraint should just be treated as 'g'.
338 return false;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000339 }
340 break;
341 case '[': {
342 unsigned Index = 0;
Chris Lattner0cd323d2009-04-26 17:57:12 +0000343 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssonb5c85692009-01-18 01:56:57 +0000344 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000345
Anders Carlssonb5c85692009-01-18 01:56:57 +0000346 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000347 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000348 case '%': // commutative
349 // FIXME: Fail if % is used with the last operand.
350 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000351 case 'i': // immediate integer.
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000352 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000353 break;
Chris Lattnerf803ad82009-05-06 04:33:31 +0000354 case 'I': // Various constant constraints with target-specific meanings.
355 case 'J':
356 case 'K':
357 case 'L':
358 case 'M':
359 case 'N':
360 case 'O':
361 case 'P':
362 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000363 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000364 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000365 break;
366 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000367 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000368 break;
369 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000370 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000371 Info.setAllowsRegister();
372 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000373 break;
374 }
Mike Stump25cf7602009-09-09 15:08:12 +0000375
Anders Carlsson4ce42302007-11-27 04:11:28 +0000376 Name++;
377 }
Mike Stump25cf7602009-09-09 15:08:12 +0000378
Anders Carlsson4ce42302007-11-27 04:11:28 +0000379 return true;
380}