blob: 378a91503bf1108c002991999780c8057ae5eccf [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.
Chris Lattner3b74cac2008-03-08 08:59:43 +000025 CharIsSigned = true;
Chris Lattner727b3c42008-05-09 06:08:39 +000026 PointerWidth = PointerAlign = 32;
Chris Lattner3b74cac2008-03-08 08:59:43 +000027 WCharWidth = WCharAlign = 32;
Chris Lattner85970f32008-05-08 05:58:21 +000028 IntWidth = IntAlign = 32;
Chris Lattner481dcf22008-05-09 05:50:02 +000029 LongWidth = LongAlign = 32;
30 LongLongWidth = LongLongAlign = 64;
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000031 FloatWidth = 32;
32 FloatAlign = 32;
Nate Begemand5e35f82008-04-18 17:17:24 +000033 DoubleWidth = 64;
Eli Friedman0b7b1cb2008-05-20 14:21:01 +000034 DoubleAlign = 64;
35 LongDoubleWidth = 64;
36 LongDoubleAlign = 64;
Nate Begeman2aaa0a02009-01-17 23:56:13 +000037 IntMaxTWidth = 64;
Anders Carlssonc0b56cd2008-10-31 16:05:19 +000038 SizeType = UnsignedLong;
Eli Friedman7cca0982008-11-02 02:43:55 +000039 PtrDiffType = SignedLong;
Sanjiv Guptafa451432008-10-31 09:52:39 +000040 IntMaxType = SignedLongLong;
41 UIntMaxType = UnsignedLongLong;
Eli Friedman7cca0982008-11-02 02:43:55 +000042 WCharType = SignedInt;
Chris Lattner3b74cac2008-03-08 08:59:43 +000043 FloatFormat = &llvm::APFloat::IEEEsingle;
44 DoubleFormat = &llvm::APFloat::IEEEdouble;
45 LongDoubleFormat = &llvm::APFloat::IEEEdouble;
Eli Friedman2b161652008-08-21 00:13:15 +000046 DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
47 "i64:64:64-f32:32:32-f64:64:64";
Chris Lattner3f6d8cf2008-10-05 19:22:37 +000048 UserLabelPrefix = "_";
Chris Lattner3b74cac2008-03-08 08:59:43 +000049}
50
Chris Lattner0fb8d852008-03-08 08:24:01 +000051// Out of line virtual dtor for TargetInfo.
52TargetInfo::~TargetInfo() {}
Chris Lattner4b009652007-07-25 00:24:17 +000053
Chris Lattner534234c2009-02-06 05:04:11 +000054/// getTypeName - Return the user string for the specified integer type enum.
55/// For example, SignedShort -> "short".
56const char *TargetInfo::getTypeName(IntType T) {
57 switch (T) {
58 default: assert(0 && "not an integer!");
59 case SignedShort: return "short";
60 case UnsignedShort: return "unsigned short";
61 case SignedInt: return "int";
62 case UnsignedInt: return "unsigned int";
63 case SignedLong: return "long int";
64 case UnsignedLong: return "long unsigned int";
65 case SignedLongLong: return "long long int";
66 case UnsignedLongLong: return "long long unsigned int";
67 }
68}
69
Chris Lattner858eece2007-09-22 18:29:59 +000070//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000071
Chris Lattner4b009652007-07-25 00:24:17 +000072
Chris Lattnerfc457002008-03-05 01:18:20 +000073static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000074 if (Name[0] == '%' || Name[0] == '#')
75 Name++;
76}
77
Anders Carlsson7dd1c952007-11-24 23:38:12 +000078/// isValidGCCRegisterName - Returns whether the passed in string
79/// is a valid register name according to GCC. This is used by Sema for
80/// inline asm statements.
81bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000082 const char * const *Names;
83 unsigned NumNames;
84
85 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000086 removeGCCRegisterPrefix(Name);
87
Anders Carlsson49dadd62007-11-25 00:25:21 +000088
89 if (strcmp(Name, "memory") == 0 ||
90 strcmp(Name, "cc") == 0)
91 return true;
92
Chris Lattner0fb8d852008-03-08 08:24:01 +000093 getGCCRegNames(Names, NumNames);
Anders Carlsson49dadd62007-11-25 00:25:21 +000094
95 // If we have a number it maps to an entry in the register name array.
96 if (isdigit(Name[0])) {
97 char *End;
98 int n = (int)strtol(Name, &End, 0);
99 if (*End == 0)
100 return n >= 0 && (unsigned)n < NumNames;
101 }
102
103 // Check register names.
104 for (unsigned i = 0; i < NumNames; i++) {
105 if (strcmp(Name, Names[i]) == 0)
106 return true;
107 }
108
109 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000110 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +0000111 unsigned NumAliases;
112
Chris Lattner0fb8d852008-03-08 08:24:01 +0000113 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +0000114 for (unsigned i = 0; i < NumAliases; i++) {
115 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
116 if (!Aliases[i].Aliases[j])
117 break;
118 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
119 return true;
120 }
121 }
122
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000123 return false;
124}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000125
Chris Lattner0fb8d852008-03-08 08:24:01 +0000126const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000127 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
128
Anders Carlsson74385982008-02-06 00:11:32 +0000129 removeGCCRegisterPrefix(Name);
Anders Carlsson3fb39b12008-02-05 23:30:20 +0000130
Anders Carlsson4ce42302007-11-27 04:11:28 +0000131 const char * const *Names;
132 unsigned NumNames;
133
Chris Lattner0fb8d852008-03-08 08:24:01 +0000134 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000135
136 // First, check if we have a number.
137 if (isdigit(Name[0])) {
138 char *End;
139 int n = (int)strtol(Name, &End, 0);
140 if (*End == 0) {
141 assert(n >= 0 && (unsigned)n < NumNames &&
142 "Out of bounds register number!");
143 return Names[n];
144 }
145 }
146
147 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000148 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000149 unsigned NumAliases;
150
Chris Lattner0fb8d852008-03-08 08:24:01 +0000151 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000152 for (unsigned i = 0; i < NumAliases; i++) {
153 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
154 if (!Aliases[i].Aliases[j])
155 break;
156 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
157 return Aliases[i].Register;
158 }
159 }
160
161 return Name;
162}
163
164bool TargetInfo::validateOutputConstraint(const char *Name,
165 ConstraintInfo &info) const
166{
Anders Carlssonc87c3462009-01-12 02:15:29 +0000167 info = CI_None;
168
Anders Carlsson4ce42302007-11-27 04:11:28 +0000169 // An output constraint must start with '=' or '+'
170 if (*Name != '=' && *Name != '+')
171 return false;
172
173 if (*Name == '+')
174 info = CI_ReadWrite;
175 else
176 info = CI_None;
177
178 Name++;
179 while (*Name) {
180 switch (*Name) {
181 default:
Chris Lattner0fb8d852008-03-08 08:24:01 +0000182 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000183 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000184 // so we can add more constraints as we hit it.
185 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000186 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000187 }
188 case '&': // early clobber.
189 break;
190 case 'r': // general register.
191 info = (ConstraintInfo)(info|CI_AllowsRegister);
192 break;
193 case 'm': // memory operand.
194 info = (ConstraintInfo)(info|CI_AllowsMemory);
195 break;
196 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000197 case 'X': // any operand.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000198 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
199 break;
200 }
201
202 Name++;
203 }
204
205 return true;
206}
207
Anders Carlssonb5c85692009-01-18 01:56:57 +0000208bool TargetInfo::resolveSymbolicName(const char *&Name,
209 const std::string *OutputNamesBegin,
210 const std::string *OutputNamesEnd,
211 unsigned &Index) const
212{
213 assert(*Name == '[' && "Symbolic name did not start with '['");
214
215 Name++;
216 const char *Start = Name;
217 while (*Name && *Name != ']')
218 Name++;
219
220 if (!*Name) {
221 // Missing ']'
222 return false;
223 }
224
225 std::string SymbolicName(Start, Name - Start);
226
227 Index = 0;
228 for (const std::string *it = OutputNamesBegin;
229 it != OutputNamesEnd;
230 ++it, Index++) {
231 if (SymbolicName == *it)
232 return true;
233 }
234
235 return false;
236}
237
Anders Carlsson4ce42302007-11-27 04:11:28 +0000238bool TargetInfo::validateInputConstraint(const char *Name,
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000239 const std::string *OutputNamesBegin,
240 const std::string *OutputNamesEnd,
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000241 ConstraintInfo* OutputConstraints,
Chris Lattner0fb8d852008-03-08 08:24:01 +0000242 ConstraintInfo &info) const {
Anders Carlssonc87c3462009-01-12 02:15:29 +0000243 info = CI_None;
244
Anders Carlsson4ce42302007-11-27 04:11:28 +0000245 while (*Name) {
246 switch (*Name) {
247 default:
248 // Check if we have a matching constraint
249 if (*Name >= '0' && *Name <= '9') {
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000250 unsigned NumOutputs = OutputNamesEnd - OutputNamesBegin;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000251 unsigned i = *Name - '0';
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000252
Anders Carlsson4ce42302007-11-27 04:11:28 +0000253 // Check if matching constraint is out of bounds.
254 if (i >= NumOutputs)
255 return false;
Anders Carlssondd3a4fe2009-01-27 20:38:24 +0000256
257 // The constraint should have the same info as the respective
258 // output constraint.
259 info = (ConstraintInfo)(info|OutputConstraints[i]);
Chris Lattner0fb8d852008-03-08 08:24:01 +0000260 } else if (!validateAsmConstraint(*Name, info)) {
Daniel Dunbar157e1e62008-08-25 09:46:27 +0000261 // FIXME: This error return is in place temporarily so we can
262 // add more constraints as we hit it. Eventually, an unknown
263 // constraint should just be treated as 'g'.
264 return false;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000265 }
266 break;
267 case '[': {
268 unsigned Index = 0;
269 if (!resolveSymbolicName(Name, OutputNamesBegin, OutputNamesEnd, Index))
270 return false;
271
272 break;
273 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000274 case '%': // commutative
275 // FIXME: Fail if % is used with the last operand.
276 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000277 case 'i': // immediate integer.
Anders Carlssonf8e50d92008-02-18 17:00:25 +0000278 case 'I':
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000279 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000280 break;
281 case 'r': // general register.
282 info = (ConstraintInfo)(info|CI_AllowsRegister);
283 break;
284 case 'm': // memory operand.
285 info = (ConstraintInfo)(info|CI_AllowsMemory);
286 break;
287 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000288 case 'X': // any operand.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000289 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
290 break;
291 }
292
293 Name++;
294 }
295
296 return true;
297}