blob: f9e789c36d567f92fe9da8320a1fa689220b363b [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 Lattner858eece2007-09-22 18:29:59 +000077//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000078
Chris Lattner4b009652007-07-25 00:24:17 +000079
Chris Lattnerfc457002008-03-05 01:18:20 +000080static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000081 if (Name[0] == '%' || Name[0] == '#')
82 Name++;
83}
84
Anders Carlsson7dd1c952007-11-24 23:38:12 +000085/// isValidGCCRegisterName - Returns whether the passed in string
86/// is a valid register name according to GCC. This is used by Sema for
87/// inline asm statements.
88bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000089 const char * const *Names;
90 unsigned NumNames;
Mike Stump25cf7602009-09-09 15:08:12 +000091
Anders Carlsson49dadd62007-11-25 00:25:21 +000092 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000093 removeGCCRegisterPrefix(Name);
94
Mike Stump25cf7602009-09-09 15:08:12 +000095
Anders Carlsson49dadd62007-11-25 00:25:21 +000096 if (strcmp(Name, "memory") == 0 ||
97 strcmp(Name, "cc") == 0)
98 return true;
Mike Stump25cf7602009-09-09 15:08:12 +000099
Chris Lattner0fb8d852008-03-08 08:24:01 +0000100 getGCCRegNames(Names, NumNames);
Mike Stump25cf7602009-09-09 15:08:12 +0000101
Anders Carlsson49dadd62007-11-25 00:25:21 +0000102 // If we have a number it maps to an entry in the register name array.
103 if (isdigit(Name[0])) {
104 char *End;
105 int n = (int)strtol(Name, &End, 0);
106 if (*End == 0)
107 return n >= 0 && (unsigned)n < NumNames;
108 }
109
110 // Check register names.
111 for (unsigned i = 0; i < NumNames; i++) {
112 if (strcmp(Name, Names[i]) == 0)
113 return true;
114 }
Mike Stump25cf7602009-09-09 15:08:12 +0000115
Anders Carlsson49dadd62007-11-25 00:25:21 +0000116 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000117 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +0000118 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000119
Chris Lattner0fb8d852008-03-08 08:24:01 +0000120 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +0000121 for (unsigned i = 0; i < NumAliases; i++) {
122 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
123 if (!Aliases[i].Aliases[j])
124 break;
125 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
126 return true;
127 }
128 }
Mike Stump25cf7602009-09-09 15:08:12 +0000129
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000130 return false;
131}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000132
Chris Lattner0fb8d852008-03-08 08:24:01 +0000133const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000134 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
Mike Stump25cf7602009-09-09 15:08:12 +0000135
Anders Carlsson74385982008-02-06 00:11:32 +0000136 removeGCCRegisterPrefix(Name);
Mike Stump25cf7602009-09-09 15:08:12 +0000137
Anders Carlsson4ce42302007-11-27 04:11:28 +0000138 const char * const *Names;
139 unsigned NumNames;
140
Chris Lattner0fb8d852008-03-08 08:24:01 +0000141 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000142
143 // First, check if we have a number.
144 if (isdigit(Name[0])) {
145 char *End;
146 int n = (int)strtol(Name, &End, 0);
147 if (*End == 0) {
Mike Stump25cf7602009-09-09 15:08:12 +0000148 assert(n >= 0 && (unsigned)n < NumNames &&
Anders Carlsson4ce42302007-11-27 04:11:28 +0000149 "Out of bounds register number!");
150 return Names[n];
151 }
152 }
Mike Stump25cf7602009-09-09 15:08:12 +0000153
Anders Carlsson4ce42302007-11-27 04:11:28 +0000154 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000155 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000156 unsigned NumAliases;
Mike Stump25cf7602009-09-09 15:08:12 +0000157
Chris Lattner0fb8d852008-03-08 08:24:01 +0000158 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000159 for (unsigned i = 0; i < NumAliases; i++) {
160 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
161 if (!Aliases[i].Aliases[j])
162 break;
163 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
164 return Aliases[i].Register;
165 }
166 }
Mike Stump25cf7602009-09-09 15:08:12 +0000167
Anders Carlsson4ce42302007-11-27 04:11:28 +0000168 return Name;
169}
170
Chris Lattner9f8e5022009-04-26 17:19:08 +0000171bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
172 const char *Name = Info.getConstraintStr().c_str();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000173 // An output constraint must start with '=' or '+'
174 if (*Name != '=' && *Name != '+')
175 return false;
176
177 if (*Name == '+')
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000178 Info.setIsReadWrite();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000179
180 Name++;
181 while (*Name) {
182 switch (*Name) {
183 default:
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000184 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000185 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000186 // so we can add more constraints as we hit it.
187 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000188 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000189 }
190 case '&': // early clobber.
191 break;
Chris Lattner79c224b2009-10-13 04:32:07 +0000192 case '%': // commutative.
193 // FIXME: Check that there is a another register after this one.
194 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000195 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000196 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000197 break;
198 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000199 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000200 break;
201 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000202 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000203 Info.setAllowsRegister();
204 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000205 break;
206 }
Mike Stump25cf7602009-09-09 15:08:12 +0000207
Anders Carlsson4ce42302007-11-27 04:11:28 +0000208 Name++;
209 }
Mike Stump25cf7602009-09-09 15:08:12 +0000210
Anders Carlsson4ce42302007-11-27 04:11:28 +0000211 return true;
212}
213
Anders Carlssonb5c85692009-01-18 01:56:57 +0000214bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner0cd323d2009-04-26 17:57:12 +0000215 ConstraintInfo *OutputConstraints,
216 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000217 unsigned &Index) const {
Anders Carlssonb5c85692009-01-18 01:56:57 +0000218 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlssonb5c85692009-01-18 01:56:57 +0000219 Name++;
220 const char *Start = Name;
221 while (*Name && *Name != ']')
222 Name++;
Mike Stump25cf7602009-09-09 15:08:12 +0000223
Anders Carlssonb5c85692009-01-18 01:56:57 +0000224 if (!*Name) {
225 // Missing ']'
226 return false;
227 }
Mike Stump25cf7602009-09-09 15:08:12 +0000228
Anders Carlssonb5c85692009-01-18 01:56:57 +0000229 std::string SymbolicName(Start, Name - Start);
Mike Stump25cf7602009-09-09 15:08:12 +0000230
Chris Lattner0cd323d2009-04-26 17:57:12 +0000231 for (Index = 0; Index != NumOutputs; ++Index)
232 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlssonb5c85692009-01-18 01:56:57 +0000233 return true;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000234
235 return false;
236}
237
Chris Lattner0cd323d2009-04-26 17:57:12 +0000238bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
239 unsigned NumOutputs,
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000240 ConstraintInfo &Info) const {
Chris Lattner9f8e5022009-04-26 17:19:08 +0000241 const char *Name = Info.ConstraintStr.c_str();
242
Anders Carlsson4ce42302007-11-27 04:11:28 +0000243 while (*Name) {
244 switch (*Name) {
245 default:
246 // Check if we have a matching constraint
247 if (*Name >= '0' && *Name <= '9') {
248 unsigned i = *Name - '0';
Mike Stump25cf7602009-09-09 15:08:12 +0000249
Anders Carlsson4ce42302007-11-27 04:11:28 +0000250 // Check if matching constraint is out of bounds.
251 if (i >= NumOutputs)
252 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000253
254 // The constraint should have the same info as the respective
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000255 // output constraint.
Chris Lattner9eb395f2009-04-26 18:05:25 +0000256 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000257 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar157e1e62008-08-25 09:46:27 +0000258 // FIXME: This error return is in place temporarily so we can
259 // add more constraints as we hit it. Eventually, an unknown
260 // constraint should just be treated as 'g'.
261 return false;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000262 }
263 break;
264 case '[': {
265 unsigned Index = 0;
Chris Lattner0cd323d2009-04-26 17:57:12 +0000266 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlssonb5c85692009-01-18 01:56:57 +0000267 return false;
Mike Stump25cf7602009-09-09 15:08:12 +0000268
Anders Carlssonb5c85692009-01-18 01:56:57 +0000269 break;
Mike Stump25cf7602009-09-09 15:08:12 +0000270 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000271 case '%': // commutative
272 // FIXME: Fail if % is used with the last operand.
273 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000274 case 'i': // immediate integer.
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000275 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000276 break;
Chris Lattnerf803ad82009-05-06 04:33:31 +0000277 case 'I': // Various constant constraints with target-specific meanings.
278 case 'J':
279 case 'K':
280 case 'L':
281 case 'M':
282 case 'N':
283 case 'O':
284 case 'P':
285 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000286 case 'r': // general register.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000287 Info.setAllowsRegister();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000288 break;
289 case 'm': // memory operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000290 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000291 break;
292 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000293 case 'X': // any operand.
Chris Lattnerc49cc1a2009-04-26 07:16:29 +0000294 Info.setAllowsRegister();
295 Info.setAllowsMemory();
Anders Carlsson4ce42302007-11-27 04:11:28 +0000296 break;
297 }
Mike Stump25cf7602009-09-09 15:08:12 +0000298
Anders Carlsson4ce42302007-11-27 04:11:28 +0000299 Name++;
300 }
Mike Stump25cf7602009-09-09 15:08:12 +0000301
Anders Carlsson4ce42302007-11-27 04:11:28 +0000302 return true;
303}