blob: 1e8ca2bd56c466f913458917289ceb6253a8d49a [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.
Chris Lattnercd4fc422008-03-08 08:59:43 +000025 CharIsSigned = true;
Eli Friedmanb030f022009-04-19 21:38:35 +000026 TLSSupported = true;
Chris Lattner927686f2008-05-09 06:08:39 +000027 PointerWidth = PointerAlign = 32;
Chris Lattnercd4fc422008-03-08 08:59:43 +000028 WCharWidth = WCharAlign = 32;
Chris Lattner2621fd12008-05-08 05:58:21 +000029 IntWidth = IntAlign = 32;
Chris Lattnerec10f582008-05-09 05:50:02 +000030 LongWidth = LongAlign = 32;
31 LongLongWidth = LongLongAlign = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000032 FloatWidth = 32;
33 FloatAlign = 32;
Nate Begeman3d292062008-04-18 17:17:24 +000034 DoubleWidth = 64;
Eli Friedman61538a72008-05-20 14:21:01 +000035 DoubleAlign = 64;
36 LongDoubleWidth = 64;
37 LongDoubleAlign = 64;
Nate Begeman22b9d5a2009-01-17 23:56:13 +000038 IntMaxTWidth = 64;
Anders Carlsson6a3615c2008-10-31 16:05:19 +000039 SizeType = UnsignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000040 PtrDiffType = SignedLong;
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +000041 IntMaxType = SignedLongLong;
42 UIntMaxType = UnsignedLongLong;
Chris Lattner6ad474f2009-02-13 22:28:55 +000043 IntPtrType = SignedLong;
Eli Friedmanf509d732008-11-02 02:43:55 +000044 WCharType = SignedInt;
Chris Lattnercd4fc422008-03-08 08:59:43 +000045 FloatFormat = &llvm::APFloat::IEEEsingle;
46 DoubleFormat = &llvm::APFloat::IEEEdouble;
47 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedmaned855cb2008-08-21 00:13:15 +000048 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
49 "i64:64:64-f32:32:32-f64:64:64";
Chris Lattner3fdf4672008-10-05 19:22:37 +000050 UserLabelPrefix = "_";
Chris Lattnercd4fc422008-03-08 08:59:43 +000051}
52
Chris Lattner0eaed122008-03-08 08:24:01 +000053// Out of line virtual dtor for TargetInfo.
54TargetInfo::~TargetInfo() {}
Reid Spencer5f016e22007-07-11 17:01:13 +000055
Chris Lattner2b5abf52009-02-06 05:04:11 +000056/// getTypeName - Return the user string for the specified integer type enum.
57/// For example, SignedShort -> "short".
58const char *TargetInfo::getTypeName(IntType T) {
59 switch (T) {
60 default: assert(0 && "not an integer!");
61 case SignedShort: return "short";
62 case UnsignedShort: return "unsigned short";
63 case SignedInt: return "int";
64 case UnsignedInt: return "unsigned int";
65 case SignedLong: return "long int";
66 case UnsignedLong: return "long unsigned int";
67 case SignedLongLong: return "long long int";
68 case UnsignedLongLong: return "long long unsigned int";
69 }
70}
71
Chris Lattner525a0502007-09-22 18:29:59 +000072//===----------------------------------------------------------------------===//
Chris Lattner525a0502007-09-22 18:29:59 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074
Chris Lattner42e67372008-03-05 01:18:20 +000075static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlssonea041752008-02-06 00:11:32 +000076 if (Name[0] == '%' || Name[0] == '#')
77 Name++;
78}
79
Anders Carlsson3346ae62007-11-24 23:38:12 +000080/// isValidGCCRegisterName - Returns whether the passed in string
81/// is a valid register name according to GCC. This is used by Sema for
82/// inline asm statements.
83bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +000084 const char * const *Names;
85 unsigned NumNames;
86
87 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +000088 removeGCCRegisterPrefix(Name);
89
Anders Carlsson6fa90862007-11-25 00:25:21 +000090
91 if (strcmp(Name, "memory") == 0 ||
92 strcmp(Name, "cc") == 0)
93 return true;
94
Chris Lattner0eaed122008-03-08 08:24:01 +000095 getGCCRegNames(Names, NumNames);
Anders Carlsson6fa90862007-11-25 00:25:21 +000096
97 // If we have a number it maps to an entry in the register name array.
98 if (isdigit(Name[0])) {
99 char *End;
100 int n = (int)strtol(Name, &End, 0);
101 if (*End == 0)
102 return n >= 0 && (unsigned)n < NumNames;
103 }
104
105 // Check register names.
106 for (unsigned i = 0; i < NumNames; i++) {
107 if (strcmp(Name, Names[i]) == 0)
108 return true;
109 }
110
111 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000112 const GCCRegAlias *Aliases;
Anders Carlsson6fa90862007-11-25 00:25:21 +0000113 unsigned NumAliases;
114
Chris Lattner0eaed122008-03-08 08:24:01 +0000115 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson6fa90862007-11-25 00:25:21 +0000116 for (unsigned i = 0; i < NumAliases; i++) {
117 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
118 if (!Aliases[i].Aliases[j])
119 break;
120 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
121 return true;
122 }
123 }
124
Anders Carlsson3346ae62007-11-24 23:38:12 +0000125 return false;
126}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000127
Chris Lattner0eaed122008-03-08 08:24:01 +0000128const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlssond04c6e22007-11-27 04:11:28 +0000129 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
130
Anders Carlssonea041752008-02-06 00:11:32 +0000131 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000132
Anders Carlssond04c6e22007-11-27 04:11:28 +0000133 const char * const *Names;
134 unsigned NumNames;
135
Chris Lattner0eaed122008-03-08 08:24:01 +0000136 getGCCRegNames(Names, NumNames);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000137
138 // First, check if we have a number.
139 if (isdigit(Name[0])) {
140 char *End;
141 int n = (int)strtol(Name, &End, 0);
142 if (*End == 0) {
143 assert(n >= 0 && (unsigned)n < NumNames &&
144 "Out of bounds register number!");
145 return Names[n];
146 }
147 }
148
149 // Now check aliases.
Chris Lattner0eaed122008-03-08 08:24:01 +0000150 const GCCRegAlias *Aliases;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000151 unsigned NumAliases;
152
Chris Lattner0eaed122008-03-08 08:24:01 +0000153 getGCCRegAliases(Aliases, NumAliases);
Anders Carlssond04c6e22007-11-27 04:11:28 +0000154 for (unsigned i = 0; i < NumAliases; i++) {
155 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
156 if (!Aliases[i].Aliases[j])
157 break;
158 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
159 return Aliases[i].Register;
160 }
161 }
162
163 return Name;
164}
165
Chris Lattner432c8692009-04-26 17:19:08 +0000166bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
167 const char *Name = Info.getConstraintStr().c_str();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000168 // An output constraint must start with '=' or '+'
169 if (*Name != '=' && *Name != '+')
170 return false;
171
172 if (*Name == '+')
Chris Lattner44def072009-04-26 07:16:29 +0000173 Info.setIsReadWrite();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000174
175 Name++;
176 while (*Name) {
177 switch (*Name) {
178 default:
Chris Lattner44def072009-04-26 07:16:29 +0000179 if (!validateAsmConstraint(Name, Info)) {
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000180 // FIXME: We temporarily return false
Anders Carlssond04c6e22007-11-27 04:11:28 +0000181 // so we can add more constraints as we hit it.
182 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenek5ab201a2008-04-24 16:36:38 +0000183 return false;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000184 }
185 case '&': // early clobber.
186 break;
187 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000188 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000189 break;
190 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000191 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000192 break;
193 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000194 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000195 Info.setAllowsRegister();
196 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000197 break;
198 }
199
200 Name++;
201 }
202
203 return true;
204}
205
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000206bool TargetInfo::resolveSymbolicName(const char *&Name,
Chris Lattner2819fa82009-04-26 17:57:12 +0000207 ConstraintInfo *OutputConstraints,
208 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000209 unsigned &Index) const {
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000210 assert(*Name == '[' && "Symbolic name did not start with '['");
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000211 Name++;
212 const char *Start = Name;
213 while (*Name && *Name != ']')
214 Name++;
215
216 if (!*Name) {
217 // Missing ']'
218 return false;
219 }
220
221 std::string SymbolicName(Start, Name - Start);
222
Chris Lattner2819fa82009-04-26 17:57:12 +0000223 for (Index = 0; Index != NumOutputs; ++Index)
224 if (SymbolicName == OutputConstraints[Index].getName())
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000225 return true;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000226
227 return false;
228}
229
Chris Lattner2819fa82009-04-26 17:57:12 +0000230bool TargetInfo::validateInputConstraint(ConstraintInfo *OutputConstraints,
231 unsigned NumOutputs,
Chris Lattner44def072009-04-26 07:16:29 +0000232 ConstraintInfo &Info) const {
Chris Lattner432c8692009-04-26 17:19:08 +0000233 const char *Name = Info.ConstraintStr.c_str();
234
Anders Carlssond04c6e22007-11-27 04:11:28 +0000235 while (*Name) {
236 switch (*Name) {
237 default:
238 // Check if we have a matching constraint
239 if (*Name >= '0' && *Name <= '9') {
240 unsigned i = *Name - '0';
Anders Carlsson45b050e2009-01-17 23:36:15 +0000241
Anders Carlssond04c6e22007-11-27 04:11:28 +0000242 // Check if matching constraint is out of bounds.
243 if (i >= NumOutputs)
244 return false;
Anders Carlsson03eb5432009-01-27 20:38:24 +0000245
246 // The constraint should have the same info as the respective
247 // output constraint.
Chris Lattnerd6887612009-04-26 18:05:25 +0000248 Info.setTiedOperand(i, OutputConstraints[i]);
Chris Lattner44def072009-04-26 07:16:29 +0000249 } else if (!validateAsmConstraint(Name, Info)) {
Daniel Dunbar302684c2008-08-25 09:46:27 +0000250 // FIXME: This error return is in place temporarily so we can
251 // add more constraints as we hit it. Eventually, an unknown
252 // constraint should just be treated as 'g'.
253 return false;
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000254 }
255 break;
256 case '[': {
257 unsigned Index = 0;
Chris Lattner2819fa82009-04-26 17:57:12 +0000258 if (!resolveSymbolicName(Name, OutputConstraints, NumOutputs, Index))
Anders Carlsson42e1ee02009-01-18 01:56:57 +0000259 return false;
260
261 break;
262 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000263 case '%': // commutative
264 // FIXME: Fail if % is used with the last operand.
265 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000266 case 'i': // immediate integer.
Anders Carlssonefdfa772008-03-09 06:02:02 +0000267 case 'n': // immediate integer with a known value.
Anders Carlssond04c6e22007-11-27 04:11:28 +0000268 break;
Chris Lattner1d138792009-05-06 04:33:31 +0000269 case 'I': // Various constant constraints with target-specific meanings.
270 case 'J':
271 case 'K':
272 case 'L':
273 case 'M':
274 case 'N':
275 case 'O':
276 case 'P':
277 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000278 case 'r': // general register.
Chris Lattner44def072009-04-26 07:16:29 +0000279 Info.setAllowsRegister();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000280 break;
281 case 'm': // memory operand.
Chris Lattner44def072009-04-26 07:16:29 +0000282 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000283 break;
284 case 'g': // general register, memory operand or immediate integer.
Anders Carlsson775841f2009-01-18 02:12:04 +0000285 case 'X': // any operand.
Chris Lattner44def072009-04-26 07:16:29 +0000286 Info.setAllowsRegister();
287 Info.setAllowsMemory();
Anders Carlssond04c6e22007-11-27 04:11:28 +0000288 break;
289 }
290
291 Name++;
292 }
293
294 return true;
295}