blob: 3ec3551ebb32f0372ee6866a981e1669f41c2f5b [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 Lattner858eece2007-09-22 18:29:59 +000054//===----------------------------------------------------------------------===//
Chris Lattner858eece2007-09-22 18:29:59 +000055
Chris Lattner4b009652007-07-25 00:24:17 +000056
Chris Lattnerfc457002008-03-05 01:18:20 +000057static void removeGCCRegisterPrefix(const char *&Name) {
Anders Carlsson74385982008-02-06 00:11:32 +000058 if (Name[0] == '%' || Name[0] == '#')
59 Name++;
60}
61
Anders Carlsson7dd1c952007-11-24 23:38:12 +000062/// isValidGCCRegisterName - Returns whether the passed in string
63/// is a valid register name according to GCC. This is used by Sema for
64/// inline asm statements.
65bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +000066 const char * const *Names;
67 unsigned NumNames;
68
69 // Get rid of any register prefix.
Anders Carlsson74385982008-02-06 00:11:32 +000070 removeGCCRegisterPrefix(Name);
71
Anders Carlsson49dadd62007-11-25 00:25:21 +000072
73 if (strcmp(Name, "memory") == 0 ||
74 strcmp(Name, "cc") == 0)
75 return true;
76
Chris Lattner0fb8d852008-03-08 08:24:01 +000077 getGCCRegNames(Names, NumNames);
Anders Carlsson49dadd62007-11-25 00:25:21 +000078
79 // If we have a number it maps to an entry in the register name array.
80 if (isdigit(Name[0])) {
81 char *End;
82 int n = (int)strtol(Name, &End, 0);
83 if (*End == 0)
84 return n >= 0 && (unsigned)n < NumNames;
85 }
86
87 // Check register names.
88 for (unsigned i = 0; i < NumNames; i++) {
89 if (strcmp(Name, Names[i]) == 0)
90 return true;
91 }
92
93 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +000094 const GCCRegAlias *Aliases;
Anders Carlsson49dadd62007-11-25 00:25:21 +000095 unsigned NumAliases;
96
Chris Lattner0fb8d852008-03-08 08:24:01 +000097 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson49dadd62007-11-25 00:25:21 +000098 for (unsigned i = 0; i < NumAliases; i++) {
99 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
100 if (!Aliases[i].Aliases[j])
101 break;
102 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
103 return true;
104 }
105 }
106
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000107 return false;
108}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000109
Chris Lattner0fb8d852008-03-08 08:24:01 +0000110const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const {
Anders Carlsson4ce42302007-11-27 04:11:28 +0000111 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
112
Anders Carlsson74385982008-02-06 00:11:32 +0000113 removeGCCRegisterPrefix(Name);
Anders Carlsson3fb39b12008-02-05 23:30:20 +0000114
Anders Carlsson4ce42302007-11-27 04:11:28 +0000115 const char * const *Names;
116 unsigned NumNames;
117
Chris Lattner0fb8d852008-03-08 08:24:01 +0000118 getGCCRegNames(Names, NumNames);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000119
120 // First, check if we have a number.
121 if (isdigit(Name[0])) {
122 char *End;
123 int n = (int)strtol(Name, &End, 0);
124 if (*End == 0) {
125 assert(n >= 0 && (unsigned)n < NumNames &&
126 "Out of bounds register number!");
127 return Names[n];
128 }
129 }
130
131 // Now check aliases.
Chris Lattner0fb8d852008-03-08 08:24:01 +0000132 const GCCRegAlias *Aliases;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000133 unsigned NumAliases;
134
Chris Lattner0fb8d852008-03-08 08:24:01 +0000135 getGCCRegAliases(Aliases, NumAliases);
Anders Carlsson4ce42302007-11-27 04:11:28 +0000136 for (unsigned i = 0; i < NumAliases; i++) {
137 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
138 if (!Aliases[i].Aliases[j])
139 break;
140 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
141 return Aliases[i].Register;
142 }
143 }
144
145 return Name;
146}
147
148bool TargetInfo::validateOutputConstraint(const char *Name,
149 ConstraintInfo &info) const
150{
Anders Carlssonc87c3462009-01-12 02:15:29 +0000151 info = CI_None;
152
Anders Carlsson4ce42302007-11-27 04:11:28 +0000153 // An output constraint must start with '=' or '+'
154 if (*Name != '=' && *Name != '+')
155 return false;
156
157 if (*Name == '+')
158 info = CI_ReadWrite;
159 else
160 info = CI_None;
161
162 Name++;
163 while (*Name) {
164 switch (*Name) {
165 default:
Chris Lattner0fb8d852008-03-08 08:24:01 +0000166 if (!validateAsmConstraint(*Name, info)) {
Ted Kremenekd229d962008-04-24 16:36:38 +0000167 // FIXME: We temporarily return false
Anders Carlsson4ce42302007-11-27 04:11:28 +0000168 // so we can add more constraints as we hit it.
169 // Eventually, an unknown constraint should just be treated as 'g'.
Ted Kremenekd229d962008-04-24 16:36:38 +0000170 return false;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000171 }
172 case '&': // early clobber.
173 break;
174 case 'r': // general register.
175 info = (ConstraintInfo)(info|CI_AllowsRegister);
176 break;
177 case 'm': // memory operand.
178 info = (ConstraintInfo)(info|CI_AllowsMemory);
179 break;
180 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000181 case 'X': // any operand.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000182 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
183 break;
184 }
185
186 Name++;
187 }
188
189 return true;
190}
191
Anders Carlssonb5c85692009-01-18 01:56:57 +0000192bool TargetInfo::resolveSymbolicName(const char *&Name,
193 const std::string *OutputNamesBegin,
194 const std::string *OutputNamesEnd,
195 unsigned &Index) const
196{
197 assert(*Name == '[' && "Symbolic name did not start with '['");
198
199 Name++;
200 const char *Start = Name;
201 while (*Name && *Name != ']')
202 Name++;
203
204 if (!*Name) {
205 // Missing ']'
206 return false;
207 }
208
209 std::string SymbolicName(Start, Name - Start);
210
211 Index = 0;
212 for (const std::string *it = OutputNamesBegin;
213 it != OutputNamesEnd;
214 ++it, Index++) {
215 if (SymbolicName == *it)
216 return true;
217 }
218
219 return false;
220}
221
Anders Carlsson4ce42302007-11-27 04:11:28 +0000222bool TargetInfo::validateInputConstraint(const char *Name,
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000223 const std::string *OutputNamesBegin,
224 const std::string *OutputNamesEnd,
Chris Lattner0fb8d852008-03-08 08:24:01 +0000225 ConstraintInfo &info) const {
Anders Carlssonc87c3462009-01-12 02:15:29 +0000226 info = CI_None;
227
Anders Carlsson4ce42302007-11-27 04:11:28 +0000228 while (*Name) {
229 switch (*Name) {
230 default:
231 // Check if we have a matching constraint
232 if (*Name >= '0' && *Name <= '9') {
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000233 unsigned NumOutputs = OutputNamesEnd - OutputNamesBegin;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000234 unsigned i = *Name - '0';
Anders Carlsson7b49cec2009-01-17 23:36:15 +0000235
Anders Carlsson4ce42302007-11-27 04:11:28 +0000236 // Check if matching constraint is out of bounds.
237 if (i >= NumOutputs)
238 return false;
Chris Lattner0fb8d852008-03-08 08:24:01 +0000239 } else if (!validateAsmConstraint(*Name, info)) {
Daniel Dunbar157e1e62008-08-25 09:46:27 +0000240 // FIXME: This error return is in place temporarily so we can
241 // add more constraints as we hit it. Eventually, an unknown
242 // constraint should just be treated as 'g'.
243 return false;
Anders Carlssonb5c85692009-01-18 01:56:57 +0000244 }
245 break;
246 case '[': {
247 unsigned Index = 0;
248 if (!resolveSymbolicName(Name, OutputNamesBegin, OutputNamesEnd, Index))
249 return false;
250
251 break;
252 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000253 case '%': // commutative
254 // FIXME: Fail if % is used with the last operand.
255 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000256 case 'i': // immediate integer.
Anders Carlssonf8e50d92008-02-18 17:00:25 +0000257 case 'I':
Anders Carlssonbdb0e2b2008-03-09 06:02:02 +0000258 case 'n': // immediate integer with a known value.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000259 break;
260 case 'r': // general register.
261 info = (ConstraintInfo)(info|CI_AllowsRegister);
262 break;
263 case 'm': // memory operand.
264 info = (ConstraintInfo)(info|CI_AllowsMemory);
265 break;
266 case 'g': // general register, memory operand or immediate integer.
Anders Carlssona3e9bd22009-01-18 02:12:04 +0000267 case 'X': // any operand.
Anders Carlsson4ce42302007-11-27 04:11:28 +0000268 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
269 break;
270 }
271
272 Name++;
273 }
274
275 return true;
276}