blob: 15472a473cabfadd39eecf1fe784899045d171a9 [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"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/AST/Builtins.h"
Chris Lattner525a0502007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson6fa90862007-11-25 00:25:21 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/STLExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include <set>
21using namespace clang;
22
23void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
24
25
Chris Lattner525a0502007-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,
Ted Kremenek9c728dc2007-12-12 22:39:36 +000032 FullSourceLoc Loc) {
Chris Lattner525a0502007-09-22 18:29:59 +000033 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,
Ted Kremenek9c728dc2007-12-12 22:39:36 +000039 FullSourceLoc Loc) {
Anders Carlssona9b20e52008-02-17 03:40:02 +000040 Size = 64; // FIXME: implement correctly.
41 Align = 32;
Chris Lattner525a0502007-09-22 18:29:59 +000042 Format = &llvm::APFloat::IEEEdouble;
43}
44void TargetInfo::getLongDoubleInfo(uint64_t &Size, unsigned &Align,
45 const llvm::fltSemantics *&Format,
Ted Kremenek9c728dc2007-12-12 22:39:36 +000046 FullSourceLoc Loc) {
Chris Lattner1c9bdae2007-09-22 18:38:30 +000047 Size = Align = 64; // FIXME: implement correctly.
48 Format = &llvm::APFloat::IEEEdouble;
49 //Size = 80; Align = 32; // FIXME: implement correctly.
50 //Format = &llvm::APFloat::x87DoubleExtended;
Chris Lattner525a0502007-09-22 18:29:59 +000051}
52
53
54//===----------------------------------------------------------------------===//
55
Chris Lattner544f0432008-03-05 00:53:34 +000056TargetInfo::~TargetInfo() {
57 delete PrimaryTarget;
58 for (unsigned i = 0; i < SecondaryTargets.size(); ++i)
59 delete SecondaryTargets[i];
60}
61
Ted Kremenekae360762007-12-03 22:06:55 +000062const char* TargetInfo::getTargetTriple() const {
63 return PrimaryTarget->getTargetTriple();
64}
65
Anders Carlsson44fe49c2007-12-08 19:32:57 +000066const char *TargetInfo::getTargetPrefix() const {
67 return PrimaryTarget->getTargetPrefix();
68}
69
Reid Spencer5f016e22007-07-11 17:01:13 +000070/// DiagnoseNonPortability - When a use of a non-portable target feature is
71/// used, this method emits the diagnostic and marks the translation unit as
72/// non-portable.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000073void TargetInfo::DiagnoseNonPortability(FullSourceLoc Loc,
74 unsigned DiagKind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000075 NonPortable = true;
Ted Kremenek9c728dc2007-12-12 22:39:36 +000076 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
Reid Spencer5f016e22007-07-11 17:01:13 +000077}
78
79/// GetTargetDefineMap - Get the set of target #defines in an associative
80/// collection for easy lookup.
81static void GetTargetDefineMap(const TargetInfoImpl *Target,
Chris Lattnere36751b2007-07-22 20:11:46 +000082 llvm::StringMap<std::string> &Map) {
Chris Lattnerd15fa822007-10-06 06:57:34 +000083 std::vector<char> Defines;
84 Defines.reserve(4096);
85 Target->getTargetDefines(Defines);
Reid Spencer5f016e22007-07-11 17:01:13 +000086
Chris Lattnerd15fa822007-10-06 06:57:34 +000087 for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size();
88 DefStr != E;) {
89 // Skip the '#define ' portion.
90 assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 &&
91 "#define didn't start with #define!");
92 DefStr += strlen("#define ");
Chris Lattnere36751b2007-07-22 20:11:46 +000093
Chris Lattnerd15fa822007-10-06 06:57:34 +000094 // Find the divider between the key and value.
95 const char *SpacePos = strchr(DefStr, ' ');
96
97 std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue();
98
99 const char *EndPos = strchr(SpacePos+1, '\n');
100 Entry = std::string(SpacePos+1, EndPos);
101 DefStr = EndPos+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 }
103}
104
105/// getTargetDefines - Appends the target-specific #define values for this
106/// target set to the specified buffer.
107void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
Chris Lattnereab77922007-10-06 06:29:41 +0000108 // If we have no secondary targets, be a bit more efficient.
109 if (SecondaryTargets.empty()) {
Chris Lattnerd15fa822007-10-06 06:57:34 +0000110 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattnereab77922007-10-06 06:29:41 +0000111 return;
112 }
113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // This is tricky in the face of secondary targets. Specifically,
115 // target-specific #defines that are present and identical across all
116 // secondary targets are turned into #defines, #defines that are present in
117 // the primary target but are missing or different in the secondary targets
118 // are turned into #define_target, and #defines that are not defined in the
119 // primary, but are defined in a secondary are turned into
120 // #define_other_target. This allows the preprocessor to correctly track uses
121 // of target-specific macros.
122
123 // Get the set of primary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000124 llvm::StringMap<std::string> PrimaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // Get the sets of secondary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000128 llvm::StringMap<std::string> *SecondaryDefines
129 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
131 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
132
133 // Loop over all defines in the primary target, processing them until we run
134 // out.
Chris Lattnere36751b2007-07-22 20:11:46 +0000135 for (llvm::StringMap<std::string>::iterator PDI =
136 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
137 std::string DefineName(PDI->getKeyData(),
138 PDI->getKeyData() + PDI->getKeyLength());
139 std::string DefineValue = PDI->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000140
141 // Check to see whether all secondary targets have this #define and whether
142 // it is to the same value. Remember if not, but remove the #define from
143 // their collection in any case if they have it.
144 bool isPortable = true;
145
Chris Lattnere36751b2007-07-22 20:11:46 +0000146 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
147 llvm::StringMap<std::string>::iterator I =
148 SecondaryDefines[i].find(&DefineName[0],
149 &DefineName[0]+DefineName.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 if (I == SecondaryDefines[i].end()) {
151 // Secondary target doesn't have this #define.
152 isPortable = false;
153 } else {
154 // Secondary target has this define, remember if it disagrees.
155 if (isPortable)
Chris Lattnere36751b2007-07-22 20:11:46 +0000156 isPortable = I->getValue() == DefineValue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // Remove it from the secondary target unconditionally.
158 SecondaryDefines[i].erase(I);
159 }
160 }
161
162 // If this define is non-portable, turn it into #define_target, otherwise
163 // just use #define.
164 const char *Command = isPortable ? "#define " : "#define_target ";
165 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
166
167 // Insert "defname defvalue\n".
168 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
169 Buffer.push_back(' ');
170 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
171 Buffer.push_back('\n');
172 }
173
174 // Now that all of the primary target's defines have been handled and removed
175 // from the secondary target's define sets, go through the remaining secondary
176 // target's #defines and taint them.
Chris Lattnere36751b2007-07-22 20:11:46 +0000177 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
178 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 while (!Defs.empty()) {
Chris Lattnere36751b2007-07-22 20:11:46 +0000180 const char *DefStart = Defs.begin()->getKeyData();
181 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
183 // Insert "#define_other_target defname".
184 const char *Command = "#define_other_target ";
185 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Chris Lattnere36751b2007-07-22 20:11:46 +0000186 Buffer.insert(Buffer.end(), DefStart, DefEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 Buffer.push_back('\n');
188
189 // If any other secondary targets have this same define, remove it from
190 // them to avoid duplicate #define_other_target directives.
Chris Lattnere36751b2007-07-22 20:11:46 +0000191 for (unsigned j = i+1; j != e; ++j) {
192 llvm::StringMap<std::string>::iterator I =
193 SecondaryDefines[j].find(DefStart, DefEnd);
194 if (I != SecondaryDefines[j].end())
195 SecondaryDefines[j].erase(I);
196 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 Defs.erase(Defs.begin());
198 }
199 }
Chris Lattnere36751b2007-07-22 20:11:46 +0000200
201 delete[] SecondaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000202}
203
204/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
205/// target, diagnosing whether this is non-portable across the secondary
206/// targets.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000207void TargetInfo::ComputeWCharInfo(FullSourceLoc Loc) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000208 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209
210 // Check whether this is portable across the secondary targets if the T-U is
211 // portable so far.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000212 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
213 unsigned Width, Align;
214 SecondaryTargets[i]->getWCharInfo(Width, Align);
215 if (Width != WCharWidth || Align != WCharAlign)
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000217 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000218}
219
220
221/// getTargetBuiltins - Return information about target-specific builtins for
222/// the current primary target, and info about which builtins are non-portable
223/// across the current set of primary and secondary targets.
224void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
225 unsigned &NumRecords,
226 std::vector<const char *> &NPortable) const {
227 // Get info about what actual builtins we will expose.
228 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
229 if (SecondaryTargets.empty()) return;
230
231 // Compute the set of non-portable builtins.
232
233 // Start by computing a mapping from the primary target's builtins to their
234 // info records for efficient lookup.
Chris Lattnere36751b2007-07-22 20:11:46 +0000235 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
236 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
237 const char *BIName = Records[i].Name;
238 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
239 = Records+i;
240 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000241
242 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
243 // Get the builtins for this secondary target.
244 const Builtin::Info *Records2nd;
245 unsigned NumRecords2nd;
246 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
247
248 // Remember all of the secondary builtin names.
249 std::set<std::string> BuiltinNames2nd;
250
251 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
252 BuiltinNames2nd.insert(Records2nd[j].Name);
253
254 // Check to see if the primary target has this builtin.
Chris Lattnere36751b2007-07-22 20:11:46 +0000255 llvm::StringMap<const Builtin::Info*>::iterator I =
256 PrimaryRecs.find(Records2nd[j].Name,
257 Records2nd[j].Name+strlen(Records2nd[j].Name));
258 if (I != PrimaryRecs.end()) {
259 const Builtin::Info *PrimBI = I->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 // If does. If they are not identical, mark the builtin as being
261 // non-portable.
262 if (Records2nd[j] != *PrimBI)
263 NPortable.push_back(PrimBI->Name);
264 } else {
265 // The primary target doesn't have this, it is non-portable.
266 NPortable.push_back(Records2nd[j].Name);
267 }
268 }
269
270 // Now that we checked all the secondary builtins, check to see if the
271 // primary target has any builtins that the secondary one doesn't. If so,
272 // then those are non-portable.
273 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
274 if (!BuiltinNames2nd.count(Records[j].Name))
275 NPortable.push_back(Records[j].Name);
276 }
277 }
278}
279
Anders Carlssonfb5e5ba2007-10-13 00:45:48 +0000280/// getVAListDeclaration - Return the declaration to use for
281/// __builtin_va_list, which is target-specific.
282const char *TargetInfo::getVAListDeclaration() const {
283 return PrimaryTarget->getVAListDeclaration();
284}
Reid Spencer5f016e22007-07-11 17:01:13 +0000285
Anders Carlssonea041752008-02-06 00:11:32 +0000286static void removeGCCRegisterPrefix(const char *&Name)
287{
288 if (Name[0] == '%' || Name[0] == '#')
289 Name++;
290}
291
Anders Carlsson3346ae62007-11-24 23:38:12 +0000292/// isValidGCCRegisterName - Returns whether the passed in string
293/// is a valid register name according to GCC. This is used by Sema for
294/// inline asm statements.
295bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000296 const char * const *Names;
297 unsigned NumNames;
298
299 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +0000300 removeGCCRegisterPrefix(Name);
301
Anders Carlsson6fa90862007-11-25 00:25:21 +0000302
303 if (strcmp(Name, "memory") == 0 ||
304 strcmp(Name, "cc") == 0)
305 return true;
306
307 PrimaryTarget->getGCCRegNames(Names, NumNames);
308
309 // If we have a number it maps to an entry in the register name array.
310 if (isdigit(Name[0])) {
311 char *End;
312 int n = (int)strtol(Name, &End, 0);
313 if (*End == 0)
314 return n >= 0 && (unsigned)n < NumNames;
315 }
316
317 // Check register names.
318 for (unsigned i = 0; i < NumNames; i++) {
319 if (strcmp(Name, Names[i]) == 0)
320 return true;
321 }
322
323 // Now check aliases.
324 const TargetInfoImpl::GCCRegAlias *Aliases;
325 unsigned NumAliases;
326
327 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
328 for (unsigned i = 0; i < NumAliases; i++) {
329 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
330 if (!Aliases[i].Aliases[j])
331 break;
332 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
333 return true;
334 }
335 }
336
Anders Carlsson3346ae62007-11-24 23:38:12 +0000337 return false;
338}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000339
340const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
341{
342 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
343
Anders Carlssonea041752008-02-06 00:11:32 +0000344 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000345
Anders Carlssond04c6e22007-11-27 04:11:28 +0000346 const char * const *Names;
347 unsigned NumNames;
348
349 PrimaryTarget->getGCCRegNames(Names, NumNames);
350
351 // First, check if we have a number.
352 if (isdigit(Name[0])) {
353 char *End;
354 int n = (int)strtol(Name, &End, 0);
355 if (*End == 0) {
356 assert(n >= 0 && (unsigned)n < NumNames &&
357 "Out of bounds register number!");
358 return Names[n];
359 }
360 }
361
362 // Now check aliases.
363 const TargetInfoImpl::GCCRegAlias *Aliases;
364 unsigned NumAliases;
365
366 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
367 for (unsigned i = 0; i < NumAliases; i++) {
368 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
369 if (!Aliases[i].Aliases[j])
370 break;
371 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
372 return Aliases[i].Register;
373 }
374 }
375
376 return Name;
377}
378
379bool TargetInfo::validateOutputConstraint(const char *Name,
380 ConstraintInfo &info) const
381{
382 // An output constraint must start with '=' or '+'
383 if (*Name != '=' && *Name != '+')
384 return false;
385
386 if (*Name == '+')
387 info = CI_ReadWrite;
388 else
389 info = CI_None;
390
391 Name++;
392 while (*Name) {
393 switch (*Name) {
394 default:
395 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
396 // FIXME: This assert is in place temporarily
397 // so we can add more constraints as we hit it.
398 // Eventually, an unknown constraint should just be treated as 'g'.
399 assert(0 && "Unknown output constraint type!");
400 }
401 case '&': // early clobber.
402 break;
403 case 'r': // general register.
404 info = (ConstraintInfo)(info|CI_AllowsRegister);
405 break;
406 case 'm': // memory operand.
407 info = (ConstraintInfo)(info|CI_AllowsMemory);
408 break;
409 case 'g': // general register, memory operand or immediate integer.
410 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
411 break;
412 }
413
414 Name++;
415 }
416
417 return true;
418}
419
420bool TargetInfo::validateInputConstraint(const char *Name,
421 unsigned NumOutputs,
422 ConstraintInfo &info) const
423{
424 while (*Name) {
425 switch (*Name) {
426 default:
427 // Check if we have a matching constraint
428 if (*Name >= '0' && *Name <= '9') {
429 unsigned i = *Name - '0';
430
431 // Check if matching constraint is out of bounds.
432 if (i >= NumOutputs)
433 return false;
434 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
435 // FIXME: This assert is in place temporarily
436 // so we can add more constraints as we hit it.
437 // Eventually, an unknown constraint should just be treated as 'g'.
438 assert(0 && "Unknown input constraint type!");
439 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000440 case '%': // commutative
441 // FIXME: Fail if % is used with the last operand.
442 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000443 case 'i': // immediate integer.
Anders Carlssonb41edf92008-02-18 17:00:25 +0000444 case 'I':
Anders Carlssond04c6e22007-11-27 04:11:28 +0000445 break;
446 case 'r': // general register.
447 info = (ConstraintInfo)(info|CI_AllowsRegister);
448 break;
449 case 'm': // memory operand.
450 info = (ConstraintInfo)(info|CI_AllowsMemory);
451 break;
452 case 'g': // general register, memory operand or immediate integer.
453 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
454 break;
455 }
456
457 Name++;
458 }
459
460 return true;
461}
462
Lauro Ramos Venancioa5694b82008-02-26 18:33:46 +0000463std::string TargetInfo::convertConstraint(const char Constraint) const {
464 return PrimaryTarget->convertConstraint(Constraint);
465}
466
Anders Carlssond04c6e22007-11-27 04:11:28 +0000467const char *TargetInfo::getClobbers() const
468{
469 return PrimaryTarget->getClobbers();
470}
471
472