blob: fcaeccffc0ef3fe26396191864271cca0aed3fd4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInfo and TargetInfoImpl interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/TargetInfo.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/AST/Builtins.h"
Chris Lattner858eece2007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/STLExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include <set>
21using namespace clang;
22
23void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
24
25
Chris Lattner858eece2007-09-22 18:29:59 +000026//===----------------------------------------------------------------------===//
27// FIXME: These are temporary hacks, they should revector into the
28// TargetInfoImpl.
29
30void TargetInfo::getFloatInfo(uint64_t &Size, unsigned &Align,
31 const llvm::fltSemantics *&Format,
32 SourceLocation Loc) {
33 Align = 32; // FIXME: implement correctly.
34 Size = 32;
35 Format = &llvm::APFloat::IEEEsingle;
36}
37void TargetInfo::getDoubleInfo(uint64_t &Size, unsigned &Align,
38 const llvm::fltSemantics *&Format,
39 SourceLocation Loc) {
40 Size = Align = 64; // FIXME: implement correctly.
41 Format = &llvm::APFloat::IEEEdouble;
42}
43void TargetInfo::getLongDoubleInfo(uint64_t &Size, unsigned &Align,
44 const llvm::fltSemantics *&Format,
45 SourceLocation Loc) {
Chris Lattner36f46b82007-09-22 18:38:30 +000046 Size = Align = 64; // FIXME: implement correctly.
47 Format = &llvm::APFloat::IEEEdouble;
48 //Size = 80; Align = 32; // FIXME: implement correctly.
49 //Format = &llvm::APFloat::x87DoubleExtended;
Chris Lattner858eece2007-09-22 18:29:59 +000050}
51
52
53//===----------------------------------------------------------------------===//
54
Ted Kremenek40499482007-12-03 22:06:55 +000055const char* TargetInfo::getTargetTriple() const {
56 return PrimaryTarget->getTargetTriple();
57}
58
Anders Carlsson333052c2007-12-08 19:32:57 +000059const char *TargetInfo::getTargetPrefix() const {
60 return PrimaryTarget->getTargetPrefix();
61}
62
Chris Lattner4b009652007-07-25 00:24:17 +000063/// DiagnoseNonPortability - When a use of a non-portable target feature is
64/// used, this method emits the diagnostic and marks the translation unit as
65/// non-portable.
66void TargetInfo::DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind) {
67 NonPortable = true;
68 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
69}
70
71/// GetTargetDefineMap - Get the set of target #defines in an associative
72/// collection for easy lookup.
73static void GetTargetDefineMap(const TargetInfoImpl *Target,
74 llvm::StringMap<std::string> &Map) {
Chris Lattner0db667a2007-10-06 06:57:34 +000075 std::vector<char> Defines;
76 Defines.reserve(4096);
77 Target->getTargetDefines(Defines);
Chris Lattner4b009652007-07-25 00:24:17 +000078
Chris Lattner0db667a2007-10-06 06:57:34 +000079 for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size();
80 DefStr != E;) {
81 // Skip the '#define ' portion.
82 assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 &&
83 "#define didn't start with #define!");
84 DefStr += strlen("#define ");
Chris Lattner4b009652007-07-25 00:24:17 +000085
Chris Lattner0db667a2007-10-06 06:57:34 +000086 // Find the divider between the key and value.
87 const char *SpacePos = strchr(DefStr, ' ');
88
89 std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue();
90
91 const char *EndPos = strchr(SpacePos+1, '\n');
92 Entry = std::string(SpacePos+1, EndPos);
93 DefStr = EndPos+1;
Chris Lattner4b009652007-07-25 00:24:17 +000094 }
95}
96
97/// getTargetDefines - Appends the target-specific #define values for this
98/// target set to the specified buffer.
99void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
Chris Lattner8c9d29f2007-10-06 06:29:41 +0000100 // If we have no secondary targets, be a bit more efficient.
101 if (SecondaryTargets.empty()) {
Chris Lattner0db667a2007-10-06 06:57:34 +0000102 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattner8c9d29f2007-10-06 06:29:41 +0000103 return;
104 }
105
Chris Lattner4b009652007-07-25 00:24:17 +0000106 // This is tricky in the face of secondary targets. Specifically,
107 // target-specific #defines that are present and identical across all
108 // secondary targets are turned into #defines, #defines that are present in
109 // the primary target but are missing or different in the secondary targets
110 // are turned into #define_target, and #defines that are not defined in the
111 // primary, but are defined in a secondary are turned into
112 // #define_other_target. This allows the preprocessor to correctly track uses
113 // of target-specific macros.
114
115 // Get the set of primary #defines.
116 llvm::StringMap<std::string> PrimaryDefines;
117 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
118
Chris Lattner4b009652007-07-25 00:24:17 +0000119 // Get the sets of secondary #defines.
120 llvm::StringMap<std::string> *SecondaryDefines
121 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
122 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
123 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
124
125 // Loop over all defines in the primary target, processing them until we run
126 // out.
127 for (llvm::StringMap<std::string>::iterator PDI =
128 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
129 std::string DefineName(PDI->getKeyData(),
130 PDI->getKeyData() + PDI->getKeyLength());
131 std::string DefineValue = PDI->getValue();
132
133 // Check to see whether all secondary targets have this #define and whether
134 // it is to the same value. Remember if not, but remove the #define from
135 // their collection in any case if they have it.
136 bool isPortable = true;
137
138 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
139 llvm::StringMap<std::string>::iterator I =
140 SecondaryDefines[i].find(&DefineName[0],
141 &DefineName[0]+DefineName.size());
142 if (I == SecondaryDefines[i].end()) {
143 // Secondary target doesn't have this #define.
144 isPortable = false;
145 } else {
146 // Secondary target has this define, remember if it disagrees.
147 if (isPortable)
148 isPortable = I->getValue() == DefineValue;
149 // Remove it from the secondary target unconditionally.
150 SecondaryDefines[i].erase(I);
151 }
152 }
153
154 // If this define is non-portable, turn it into #define_target, otherwise
155 // just use #define.
156 const char *Command = isPortable ? "#define " : "#define_target ";
157 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
158
159 // Insert "defname defvalue\n".
160 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
161 Buffer.push_back(' ');
162 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
163 Buffer.push_back('\n');
164 }
165
166 // Now that all of the primary target's defines have been handled and removed
167 // from the secondary target's define sets, go through the remaining secondary
168 // target's #defines and taint them.
169 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
170 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
171 while (!Defs.empty()) {
172 const char *DefStart = Defs.begin()->getKeyData();
173 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
174
175 // Insert "#define_other_target defname".
176 const char *Command = "#define_other_target ";
177 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
178 Buffer.insert(Buffer.end(), DefStart, DefEnd);
179 Buffer.push_back('\n');
180
181 // If any other secondary targets have this same define, remove it from
182 // them to avoid duplicate #define_other_target directives.
183 for (unsigned j = i+1; j != e; ++j) {
184 llvm::StringMap<std::string>::iterator I =
185 SecondaryDefines[j].find(DefStart, DefEnd);
186 if (I != SecondaryDefines[j].end())
187 SecondaryDefines[j].erase(I);
188 }
189 Defs.erase(Defs.begin());
190 }
191 }
192
193 delete[] SecondaryDefines;
194}
195
196/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
197/// target, diagnosing whether this is non-portable across the secondary
198/// targets.
199void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
200 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
201
202 // Check whether this is portable across the secondary targets if the T-U is
203 // portable so far.
204 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
205 unsigned Width, Align;
206 SecondaryTargets[i]->getWCharInfo(Width, Align);
207 if (Width != WCharWidth || Align != WCharAlign)
208 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
209 }
210}
211
212
213/// getTargetBuiltins - Return information about target-specific builtins for
214/// the current primary target, and info about which builtins are non-portable
215/// across the current set of primary and secondary targets.
216void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
217 unsigned &NumRecords,
218 std::vector<const char *> &NPortable) const {
219 // Get info about what actual builtins we will expose.
220 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
221 if (SecondaryTargets.empty()) return;
222
223 // Compute the set of non-portable builtins.
224
225 // Start by computing a mapping from the primary target's builtins to their
226 // info records for efficient lookup.
227 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
228 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
229 const char *BIName = Records[i].Name;
230 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
231 = Records+i;
232 }
233
234 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
235 // Get the builtins for this secondary target.
236 const Builtin::Info *Records2nd;
237 unsigned NumRecords2nd;
238 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
239
240 // Remember all of the secondary builtin names.
241 std::set<std::string> BuiltinNames2nd;
242
243 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
244 BuiltinNames2nd.insert(Records2nd[j].Name);
245
246 // Check to see if the primary target has this builtin.
247 llvm::StringMap<const Builtin::Info*>::iterator I =
248 PrimaryRecs.find(Records2nd[j].Name,
249 Records2nd[j].Name+strlen(Records2nd[j].Name));
250 if (I != PrimaryRecs.end()) {
251 const Builtin::Info *PrimBI = I->getValue();
252 // If does. If they are not identical, mark the builtin as being
253 // non-portable.
254 if (Records2nd[j] != *PrimBI)
255 NPortable.push_back(PrimBI->Name);
256 } else {
257 // The primary target doesn't have this, it is non-portable.
258 NPortable.push_back(Records2nd[j].Name);
259 }
260 }
261
262 // Now that we checked all the secondary builtins, check to see if the
263 // primary target has any builtins that the secondary one doesn't. If so,
264 // then those are non-portable.
265 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
266 if (!BuiltinNames2nd.count(Records[j].Name))
267 NPortable.push_back(Records[j].Name);
268 }
269 }
270}
271
Anders Carlsson8b58e8a2007-10-13 00:45:48 +0000272/// getVAListDeclaration - Return the declaration to use for
273/// __builtin_va_list, which is target-specific.
274const char *TargetInfo::getVAListDeclaration() const {
275 return PrimaryTarget->getVAListDeclaration();
276}
Chris Lattner4b009652007-07-25 00:24:17 +0000277
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000278/// isValidGCCRegisterName - Returns whether the passed in string
279/// is a valid register name according to GCC. This is used by Sema for
280/// inline asm statements.
281bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson49dadd62007-11-25 00:25:21 +0000282 const char * const *Names;
283 unsigned NumNames;
284
285 // Get rid of any register prefix.
286 if (Name[0] == '%' || Name[0] == '#')
287 Name++;
288
289 if (strcmp(Name, "memory") == 0 ||
290 strcmp(Name, "cc") == 0)
291 return true;
292
293 PrimaryTarget->getGCCRegNames(Names, NumNames);
294
295 // If we have a number it maps to an entry in the register name array.
296 if (isdigit(Name[0])) {
297 char *End;
298 int n = (int)strtol(Name, &End, 0);
299 if (*End == 0)
300 return n >= 0 && (unsigned)n < NumNames;
301 }
302
303 // Check register names.
304 for (unsigned i = 0; i < NumNames; i++) {
305 if (strcmp(Name, Names[i]) == 0)
306 return true;
307 }
308
309 // Now check aliases.
310 const TargetInfoImpl::GCCRegAlias *Aliases;
311 unsigned NumAliases;
312
313 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
314 for (unsigned i = 0; i < NumAliases; i++) {
315 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
316 if (!Aliases[i].Aliases[j])
317 break;
318 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
319 return true;
320 }
321 }
322
Anders Carlsson7dd1c952007-11-24 23:38:12 +0000323 return false;
324}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000325
326const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
327{
328 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
329
330 const char * const *Names;
331 unsigned NumNames;
332
333 PrimaryTarget->getGCCRegNames(Names, NumNames);
334
335 // First, check if we have a number.
336 if (isdigit(Name[0])) {
337 char *End;
338 int n = (int)strtol(Name, &End, 0);
339 if (*End == 0) {
340 assert(n >= 0 && (unsigned)n < NumNames &&
341 "Out of bounds register number!");
342 return Names[n];
343 }
344 }
345
346 // Now check aliases.
347 const TargetInfoImpl::GCCRegAlias *Aliases;
348 unsigned NumAliases;
349
350 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
351 for (unsigned i = 0; i < NumAliases; i++) {
352 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
353 if (!Aliases[i].Aliases[j])
354 break;
355 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
356 return Aliases[i].Register;
357 }
358 }
359
360 return Name;
361}
362
363bool TargetInfo::validateOutputConstraint(const char *Name,
364 ConstraintInfo &info) const
365{
366 // An output constraint must start with '=' or '+'
367 if (*Name != '=' && *Name != '+')
368 return false;
369
370 if (*Name == '+')
371 info = CI_ReadWrite;
372 else
373 info = CI_None;
374
375 Name++;
376 while (*Name) {
377 switch (*Name) {
378 default:
379 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
380 // FIXME: This assert is in place temporarily
381 // so we can add more constraints as we hit it.
382 // Eventually, an unknown constraint should just be treated as 'g'.
383 assert(0 && "Unknown output constraint type!");
384 }
385 case '&': // early clobber.
386 break;
387 case 'r': // general register.
388 info = (ConstraintInfo)(info|CI_AllowsRegister);
389 break;
390 case 'm': // memory operand.
391 info = (ConstraintInfo)(info|CI_AllowsMemory);
392 break;
393 case 'g': // general register, memory operand or immediate integer.
394 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
395 break;
396 }
397
398 Name++;
399 }
400
401 return true;
402}
403
404bool TargetInfo::validateInputConstraint(const char *Name,
405 unsigned NumOutputs,
406 ConstraintInfo &info) const
407{
408 while (*Name) {
409 switch (*Name) {
410 default:
411 // Check if we have a matching constraint
412 if (*Name >= '0' && *Name <= '9') {
413 unsigned i = *Name - '0';
414
415 // Check if matching constraint is out of bounds.
416 if (i >= NumOutputs)
417 return false;
418 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
419 // FIXME: This assert is in place temporarily
420 // so we can add more constraints as we hit it.
421 // Eventually, an unknown constraint should just be treated as 'g'.
422 assert(0 && "Unknown input constraint type!");
423 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000424 case '%': // commutative
425 // FIXME: Fail if % is used with the last operand.
426 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000427 case 'i': // immediate integer.
428 break;
429 case 'r': // general register.
430 info = (ConstraintInfo)(info|CI_AllowsRegister);
431 break;
432 case 'm': // memory operand.
433 info = (ConstraintInfo)(info|CI_AllowsMemory);
434 break;
435 case 'g': // general register, memory operand or immediate integer.
436 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
437 break;
438 }
439
440 Name++;
441 }
442
443 return true;
444}
445
446const char *TargetInfo::getClobbers() const
447{
448 return PrimaryTarget->getClobbers();
449}
450
451