blob: e965b9aec369f18f15d03324dbc03edb9873b826 [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-"
55 "i64:64:64-f32:32:32-f64:64:64";
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!");
99 case SignedShort: return getShortWidth();
100 case UnsignedShort: return getShortWidth();
101 case SignedInt: return getIntWidth();
102 case UnsignedInt: return getIntWidth();
103 case SignedLong: return getLongWidth();
104 case UnsignedLong: return getLongWidth();
105 case SignedLongLong: return getLongLongWidth();
106 case UnsignedLongLong: return getLongLongWidth();
107 };
108}
109
Chris Lattner3b822f12009-10-25 22:49:18 +0000110/// isTypeSigned - Return whether an integer types is signed. Returns true if
Chris Lattner40a75fe2009-10-21 06:24:21 +0000111/// the type is signed; false otherwise.
Chris Lattner3b822f12009-10-25 22:49:18 +0000112bool TargetInfo::isTypeSigned(IntType T) const {
Chris Lattner40a75fe2009-10-21 06:24:21 +0000113 switch (T) {
114 default: assert(0 && "not an integer!");
115 case SignedShort:
116 case SignedInt:
117 case SignedLong:
118 case SignedLongLong:
119 return true;
120 case UnsignedShort:
121 case UnsignedInt:
122 case UnsignedLong:
123 case UnsignedLongLong:
124 return false;
125 };
126}
127
John Thompsone65800a2009-11-05 20:14:16 +0000128/// setForcedLangOptions - Set forced language options.
129/// Apply changes to the target information with respect to certain
130/// language options which change the target configuration.
131void TargetInfo::setForcedLangOptions(LangOptions &Opts) {
132 if (Opts.ShortWChar) {
133 WCharType = UnsignedShort;
134 WCharWidth = WCharAlign = 16;
135 }
136}
Chris Lattner40a75fe2009-10-21 06:24:21 +0000137
Chris Lattner858eece2007-09-22 18:29:59 +0000138//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +0000139
Chris Lattner4b009652007-07-25 00:24:17 +0000140
Chris Lattnerfc457002008-03-05 01:18:20 +0000141static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +0000142 if (Name[0] == '%' || Name[0] == '#')
143 Name++;
144}
145
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000146/// isValidGCCRegisterName - Returns whether the passed in string
147/// is a valid register name according to GCC. This is used by Sema for
148/// inline asm statements.
149bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +0000150 const char * const *Names;
151 unsigned NumNames;
Mike Stump25cf7602009-09-09 15:08:12 +0000152
Anders Carlsson49dadd62007-11-25 00:25:21 +0000153 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +0000154 removeGCCRegisterPrefix(Name);
155
Mike Stump25cf7602009-09-09 15:08:12 +0000156
Anders Carlsson49dadd62007-11-25 00:25:21 +0000157 if (strcmp(Name, "memory") == 0 ||
158 strcmp(Name, "cc") == 0)
159 return true;
Mike Stump25cf7602009-09-09 15:08:12 +0000160
Chris Lattner0fb8d852008-03-08 08:24:01 +0000161 getGCCRegNames(Names, NumNames);
Mike Stump25cf7602009-09-09 15:08:12 +0000162
Anders Carlsson49dadd62007-11-25 00:25:21 +0000163 // If we have a number it maps to an entry in the register name array.
164 if (isdigit(Name[0])) {
165 char *End;
166 int n = (int)strtol(Name, &End, 0);
167 if (*End == 0)
168 return n >= 0 && (unsigned)n < NumNames;
169 }
170
171 // Check register names.
172 for (unsigned i = 0; i < NumNames; i++) {
173 if (strcmp(Name, Names[i]) == 0)
174 return true;
175 }
Mike Stump25cf7602009-09-09 15:08:12 +0000176
Anders Carlsson49dadd62007-11-25 00:25:21 +0000177 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000178 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +0000179 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000180
Chris Lattner0fb8d852008-03-08 08:24:01 +0000181 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +0000182 for (unsigned i = 0; i < NumAliases; i++) {
183 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
184 if (!Aliases[i].Aliases[j])
185 break;
186 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
187 return true;
188 }
189 }
Mike Stump25cf7602009-09-09 15:08:12 +0000190
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000191 return false;
192}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000193
Chris Lattner0fb8d852008-03-08 08:24:01 +0000194const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000195 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump25cf7602009-09-09 15:08:12 +0000196
Anders Carlsson74385982008-02-06 00:11:32 +0000197 removeGCCRegisterPrefix(Name);
Mike Stump25cf7602009-09-09 15:08:12 +0000198
Anders Carlsson4ce42302007-11-27 04:11:28 +0000199 const char * const *Names;
200 unsigned NumNames;
201
Chris Lattner0fb8d852008-03-08 08:24:01 +0000202 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000203
204 // First, check if we have a number.
205 if (isdigit(Name[0])) {
206 char *End;
207 int n = (int)strtol(Name, &End, 0);
208 if (*End == 0) {
Mike Stump25cf7602009-09-09 15:08:12 +0000209 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlsson4ce42302007-11-27 04:11:28 +0000210 "Out of bounds register number!");
211 return Names[n];
212 }
213 }
Mike Stump25cf7602009-09-09 15:08:12 +0000214
Anders Carlsson4ce42302007-11-27 04:11:28 +0000215 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000216 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000217 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000218
Chris Lattner0fb8d852008-03-08 08:24:01 +0000219 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000220 for (unsigned i = 0; i < NumAliases; i++) {
221 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
222 if (!Aliases[i].Aliases[j])
223 break;
224 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
225 return Aliases[i].Register;
226 }
227 }
Mike Stump25cf7602009-09-09 15:08:12 +0000228
Anders Carlsson4ce42302007-11-27 04:11:28 +0000229 return Name;
230}
231
Chris Lattner9f8e5022009-04-26 17:19:08 +0000232bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
233 const char *Name = Info.getConstraintStr().c_str();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000234 // An output constraint must start with '=' or '+'
235 if (*Name != '=' && *Name != '+')
236 return false;
237
238 if (*Name == '+')
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000239 Info.setIsReadWrite();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000240
241 Name++;
242 while (*Name) {
243 switch (*Name) {
244 default:
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000245 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000246 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000247 // so we can add more constraints as we hit it.
248 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000249 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000250 }
251 case '&': // early clobber.
252 break;
Chris Lattner79c224b2009-10-13 04:32:07 +0000253 case '%': // commutative.
254 // FIXME: Check that there is a another register after this one.
255 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000256 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000257 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000258 break;
259 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000260 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000261 break;
262 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000263 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000264 Info.setAllowsRegister();
265 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000266 break;
267 }
Mike Stump25cf7602009-09-09 15:08:12 +0000268
Anders Carlsson4ce42302007-11-27 04:11:28 +0000269 Name++;
270 }
Mike Stump25cf7602009-09-09 15:08:12 +0000271
Anders Carlsson4ce42302007-11-27 04:11:28 +0000272 return true;
273}
274
Anders Carlssonb5c85692009-01-18 01:56:57 +0000275bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner0cd323d2009-04-26 17:57:12 +0000276 ConstraintInfo *OutputConstraints,
277 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000278 unsigned &Index) const {
Anders Carlssonb5c85692009-01-18 01:56:57 +0000279 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssonb5c85692009-01-18 01:56:57 +0000280 Name++;
281 const char *Start = Name;
282 while (*Name && *Name != ']')
283 Name++;
Mike Stump25cf7602009-09-09 15:08:12 +0000284
Anders Carlssonb5c85692009-01-18 01:56:57 +0000285 if (!*Name) {
286 // Missing ']'
287 return false;
288 }
Mike Stump25cf7602009-09-09 15:08:12 +0000289
Anders Carlssonb5c85692009-01-18 01:56:57 +0000290 std::string SymbolicName(Start, Name - Start);
Mike Stump25cf7602009-09-09 15:08:12 +0000291
Chris Lattner0cd323d2009-04-26 17:57:12 +0000292 for (Index = 0; Index != NumOutputs; ++Index)
293 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssonb5c85692009-01-18 01:56:57 +0000294 return true;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000295
296 return false;
297}
298
Chris Lattner0cd323d2009-04-26 17:57:12 +0000299bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
300 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000301 ConstraintInfo &Info) const {
Chris Lattner9f8e5022009-04-26 17:19:08 +0000302 const char *Name = Info.ConstraintStr.c_str();
303
Anders Carlsson4ce42302007-11-27 04:11:28 +0000304 while (*Name) {
305 switch (*Name) {
306 default:
307 // Check if we have a matching constraint
308 if (*Name >= '0' && *Name <= '9') {
309 unsigned i = *Name - '0';
Mike Stump25cf7602009-09-09 15:08:12 +0000310
Anders Carlsson4ce42302007-11-27 04:11:28 +0000311 // Check if matching constraint is out of bounds.
312 if (i >= NumOutputs)
313 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000314
315 // The constraint should have the same info as the respective
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000316 // output constraint.
Chris Lattner9eb395f2009-04-26 18:05:25 +0000317 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000318 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar157e1e62008-08-25 09:46:27 +0000319 // FIXME: This error return is in place temporarily so we can
320 // add more constraints as we hit it. Eventually, an unknown
321 // constraint should just be treated as 'g'.
322 return false;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000323 }
324 break;
325 case '[': {
326 unsigned Index = 0;
Chris Lattner0cd323d2009-04-26 17:57:12 +0000327 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssonb5c85692009-01-18 01:56:57 +0000328 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000329
Anders Carlssonb5c85692009-01-18 01:56:57 +0000330 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000331 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000332 case '%': // commutative
333 // FIXME: Fail if % is used with the last operand.
334 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000335 case 'i': // immediate integer.
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000336 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000337 break;
Chris Lattnerf803ad82009-05-06 04:33:31 +0000338 case 'I': // Various constant constraints with target-specific meanings.
339 case 'J':
340 case 'K':
341 case 'L':
342 case 'M':
343 case 'N':
344 case 'O':
345 case 'P':
346 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000347 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000348 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000349 break;
350 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000351 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000352 break;
353 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000354 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000355 Info.setAllowsRegister();
356 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000357 break;
358 }
Mike Stump25cf7602009-09-09 15:08:12 +0000359
Anders Carlsson4ce42302007-11-27 04:11:28 +0000360 Name++;
361 }
Mike Stump25cf7602009-09-09 15:08:12 +0000362
Anders Carlsson4ce42302007-11-27 04:11:28 +0000363 return true;
364}