blob: 6b600a43a9b14027c8c532d80810c9adea09c8ee [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
Ted Kremenekae360762007-12-03 22:06:55 +000056const char* TargetInfo::getTargetTriple() const {
57 return PrimaryTarget->getTargetTriple();
58}
59
Anders Carlsson44fe49c2007-12-08 19:32:57 +000060const char *TargetInfo::getTargetPrefix() const {
61 return PrimaryTarget->getTargetPrefix();
62}
63
Reid Spencer5f016e22007-07-11 17:01:13 +000064/// DiagnoseNonPortability - When a use of a non-portable target feature is
65/// used, this method emits the diagnostic and marks the translation unit as
66/// non-portable.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000067void TargetInfo::DiagnoseNonPortability(FullSourceLoc Loc,
68 unsigned DiagKind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 NonPortable = true;
Ted Kremenek9c728dc2007-12-12 22:39:36 +000070 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
Reid Spencer5f016e22007-07-11 17:01:13 +000071}
72
73/// GetTargetDefineMap - Get the set of target #defines in an associative
74/// collection for easy lookup.
75static void GetTargetDefineMap(const TargetInfoImpl *Target,
Chris Lattnere36751b2007-07-22 20:11:46 +000076 llvm::StringMap<std::string> &Map) {
Chris Lattnerd15fa822007-10-06 06:57:34 +000077 std::vector<char> Defines;
78 Defines.reserve(4096);
79 Target->getTargetDefines(Defines);
Reid Spencer5f016e22007-07-11 17:01:13 +000080
Chris Lattnerd15fa822007-10-06 06:57:34 +000081 for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size();
82 DefStr != E;) {
83 // Skip the '#define ' portion.
84 assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 &&
85 "#define didn't start with #define!");
86 DefStr += strlen("#define ");
Chris Lattnere36751b2007-07-22 20:11:46 +000087
Chris Lattnerd15fa822007-10-06 06:57:34 +000088 // Find the divider between the key and value.
89 const char *SpacePos = strchr(DefStr, ' ');
90
91 std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue();
92
93 const char *EndPos = strchr(SpacePos+1, '\n');
94 Entry = std::string(SpacePos+1, EndPos);
95 DefStr = EndPos+1;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
97}
98
99/// getTargetDefines - Appends the target-specific #define values for this
100/// target set to the specified buffer.
101void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
Chris Lattnereab77922007-10-06 06:29:41 +0000102 // If we have no secondary targets, be a bit more efficient.
103 if (SecondaryTargets.empty()) {
Chris Lattnerd15fa822007-10-06 06:57:34 +0000104 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattnereab77922007-10-06 06:29:41 +0000105 return;
106 }
107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // This is tricky in the face of secondary targets. Specifically,
109 // target-specific #defines that are present and identical across all
110 // secondary targets are turned into #defines, #defines that are present in
111 // the primary target but are missing or different in the secondary targets
112 // are turned into #define_target, and #defines that are not defined in the
113 // primary, but are defined in a secondary are turned into
114 // #define_other_target. This allows the preprocessor to correctly track uses
115 // of target-specific macros.
116
117 // Get the set of primary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000118 llvm::StringMap<std::string> PrimaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
120
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 // Get the sets of secondary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000122 llvm::StringMap<std::string> *SecondaryDefines
123 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
125 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
126
127 // Loop over all defines in the primary target, processing them until we run
128 // out.
Chris Lattnere36751b2007-07-22 20:11:46 +0000129 for (llvm::StringMap<std::string>::iterator PDI =
130 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
131 std::string DefineName(PDI->getKeyData(),
132 PDI->getKeyData() + PDI->getKeyLength());
133 std::string DefineValue = PDI->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000134
135 // Check to see whether all secondary targets have this #define and whether
136 // it is to the same value. Remember if not, but remove the #define from
137 // their collection in any case if they have it.
138 bool isPortable = true;
139
Chris Lattnere36751b2007-07-22 20:11:46 +0000140 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
141 llvm::StringMap<std::string>::iterator I =
142 SecondaryDefines[i].find(&DefineName[0],
143 &DefineName[0]+DefineName.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 if (I == SecondaryDefines[i].end()) {
145 // Secondary target doesn't have this #define.
146 isPortable = false;
147 } else {
148 // Secondary target has this define, remember if it disagrees.
149 if (isPortable)
Chris Lattnere36751b2007-07-22 20:11:46 +0000150 isPortable = I->getValue() == DefineValue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 // Remove it from the secondary target unconditionally.
152 SecondaryDefines[i].erase(I);
153 }
154 }
155
156 // If this define is non-portable, turn it into #define_target, otherwise
157 // just use #define.
158 const char *Command = isPortable ? "#define " : "#define_target ";
159 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
160
161 // Insert "defname defvalue\n".
162 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
163 Buffer.push_back(' ');
164 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
165 Buffer.push_back('\n');
166 }
167
168 // Now that all of the primary target's defines have been handled and removed
169 // from the secondary target's define sets, go through the remaining secondary
170 // target's #defines and taint them.
Chris Lattnere36751b2007-07-22 20:11:46 +0000171 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
172 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 while (!Defs.empty()) {
Chris Lattnere36751b2007-07-22 20:11:46 +0000174 const char *DefStart = Defs.begin()->getKeyData();
175 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
Reid Spencer5f016e22007-07-11 17:01:13 +0000176
177 // Insert "#define_other_target defname".
178 const char *Command = "#define_other_target ";
179 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Chris Lattnere36751b2007-07-22 20:11:46 +0000180 Buffer.insert(Buffer.end(), DefStart, DefEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 Buffer.push_back('\n');
182
183 // If any other secondary targets have this same define, remove it from
184 // them to avoid duplicate #define_other_target directives.
Chris Lattnere36751b2007-07-22 20:11:46 +0000185 for (unsigned j = i+1; j != e; ++j) {
186 llvm::StringMap<std::string>::iterator I =
187 SecondaryDefines[j].find(DefStart, DefEnd);
188 if (I != SecondaryDefines[j].end())
189 SecondaryDefines[j].erase(I);
190 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 Defs.erase(Defs.begin());
192 }
193 }
Chris Lattnere36751b2007-07-22 20:11:46 +0000194
195 delete[] SecondaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000196}
197
198/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
199/// target, diagnosing whether this is non-portable across the secondary
200/// targets.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000201void TargetInfo::ComputeWCharInfo(FullSourceLoc Loc) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000202 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203
204 // Check whether this is portable across the secondary targets if the T-U is
205 // portable so far.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000206 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
207 unsigned Width, Align;
208 SecondaryTargets[i]->getWCharInfo(Width, Align);
209 if (Width != WCharWidth || Align != WCharAlign)
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000211 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000212}
213
214
215/// getTargetBuiltins - Return information about target-specific builtins for
216/// the current primary target, and info about which builtins are non-portable
217/// across the current set of primary and secondary targets.
218void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
219 unsigned &NumRecords,
220 std::vector<const char *> &NPortable) const {
221 // Get info about what actual builtins we will expose.
222 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
223 if (SecondaryTargets.empty()) return;
224
225 // Compute the set of non-portable builtins.
226
227 // Start by computing a mapping from the primary target's builtins to their
228 // info records for efficient lookup.
Chris Lattnere36751b2007-07-22 20:11:46 +0000229 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
230 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
231 const char *BIName = Records[i].Name;
232 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
233 = Records+i;
234 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000235
236 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
237 // Get the builtins for this secondary target.
238 const Builtin::Info *Records2nd;
239 unsigned NumRecords2nd;
240 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
241
242 // Remember all of the secondary builtin names.
243 std::set<std::string> BuiltinNames2nd;
244
245 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
246 BuiltinNames2nd.insert(Records2nd[j].Name);
247
248 // Check to see if the primary target has this builtin.
Chris Lattnere36751b2007-07-22 20:11:46 +0000249 llvm::StringMap<const Builtin::Info*>::iterator I =
250 PrimaryRecs.find(Records2nd[j].Name,
251 Records2nd[j].Name+strlen(Records2nd[j].Name));
252 if (I != PrimaryRecs.end()) {
253 const Builtin::Info *PrimBI = I->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 // If does. If they are not identical, mark the builtin as being
255 // non-portable.
256 if (Records2nd[j] != *PrimBI)
257 NPortable.push_back(PrimBI->Name);
258 } else {
259 // The primary target doesn't have this, it is non-portable.
260 NPortable.push_back(Records2nd[j].Name);
261 }
262 }
263
264 // Now that we checked all the secondary builtins, check to see if the
265 // primary target has any builtins that the secondary one doesn't. If so,
266 // then those are non-portable.
267 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
268 if (!BuiltinNames2nd.count(Records[j].Name))
269 NPortable.push_back(Records[j].Name);
270 }
271 }
272}
273
Anders Carlssonfb5e5ba2007-10-13 00:45:48 +0000274/// getVAListDeclaration - Return the declaration to use for
275/// __builtin_va_list, which is target-specific.
276const char *TargetInfo::getVAListDeclaration() const {
277 return PrimaryTarget->getVAListDeclaration();
278}
Reid Spencer5f016e22007-07-11 17:01:13 +0000279
Anders Carlssonea041752008-02-06 00:11:32 +0000280static void removeGCCRegisterPrefix(const char *&Name)
281{
282 if (Name[0] == '%' || Name[0] == '#')
283 Name++;
284}
285
Anders Carlsson3346ae62007-11-24 23:38:12 +0000286/// isValidGCCRegisterName - Returns whether the passed in string
287/// is a valid register name according to GCC. This is used by Sema for
288/// inline asm statements.
289bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000290 const char * const *Names;
291 unsigned NumNames;
292
293 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +0000294 removeGCCRegisterPrefix(Name);
295
Anders Carlsson6fa90862007-11-25 00:25:21 +0000296
297 if (strcmp(Name, "memory") == 0 ||
298 strcmp(Name, "cc") == 0)
299 return true;
300
301 PrimaryTarget->getGCCRegNames(Names, NumNames);
302
303 // If we have a number it maps to an entry in the register name array.
304 if (isdigit(Name[0])) {
305 char *End;
306 int n = (int)strtol(Name, &End, 0);
307 if (*End == 0)
308 return n >= 0 && (unsigned)n < NumNames;
309 }
310
311 // Check register names.
312 for (unsigned i = 0; i < NumNames; i++) {
313 if (strcmp(Name, Names[i]) == 0)
314 return true;
315 }
316
317 // Now check aliases.
318 const TargetInfoImpl::GCCRegAlias *Aliases;
319 unsigned NumAliases;
320
321 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
322 for (unsigned i = 0; i < NumAliases; i++) {
323 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
324 if (!Aliases[i].Aliases[j])
325 break;
326 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
327 return true;
328 }
329 }
330
Anders Carlsson3346ae62007-11-24 23:38:12 +0000331 return false;
332}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000333
334const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
335{
336 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
337
Anders Carlssonea041752008-02-06 00:11:32 +0000338 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000339
Anders Carlssond04c6e22007-11-27 04:11:28 +0000340 const char * const *Names;
341 unsigned NumNames;
342
343 PrimaryTarget->getGCCRegNames(Names, NumNames);
344
345 // First, check if we have a number.
346 if (isdigit(Name[0])) {
347 char *End;
348 int n = (int)strtol(Name, &End, 0);
349 if (*End == 0) {
350 assert(n >= 0 && (unsigned)n < NumNames &&
351 "Out of bounds register number!");
352 return Names[n];
353 }
354 }
355
356 // Now check aliases.
357 const TargetInfoImpl::GCCRegAlias *Aliases;
358 unsigned NumAliases;
359
360 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
361 for (unsigned i = 0; i < NumAliases; i++) {
362 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
363 if (!Aliases[i].Aliases[j])
364 break;
365 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
366 return Aliases[i].Register;
367 }
368 }
369
370 return Name;
371}
372
373bool TargetInfo::validateOutputConstraint(const char *Name,
374 ConstraintInfo &info) const
375{
376 // An output constraint must start with '=' or '+'
377 if (*Name != '=' && *Name != '+')
378 return false;
379
380 if (*Name == '+')
381 info = CI_ReadWrite;
382 else
383 info = CI_None;
384
385 Name++;
386 while (*Name) {
387 switch (*Name) {
388 default:
389 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
390 // FIXME: This assert is in place temporarily
391 // so we can add more constraints as we hit it.
392 // Eventually, an unknown constraint should just be treated as 'g'.
393 assert(0 && "Unknown output constraint type!");
394 }
395 case '&': // early clobber.
396 break;
397 case 'r': // general register.
398 info = (ConstraintInfo)(info|CI_AllowsRegister);
399 break;
400 case 'm': // memory operand.
401 info = (ConstraintInfo)(info|CI_AllowsMemory);
402 break;
403 case 'g': // general register, memory operand or immediate integer.
404 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
405 break;
406 }
407
408 Name++;
409 }
410
411 return true;
412}
413
414bool TargetInfo::validateInputConstraint(const char *Name,
415 unsigned NumOutputs,
416 ConstraintInfo &info) const
417{
418 while (*Name) {
419 switch (*Name) {
420 default:
421 // Check if we have a matching constraint
422 if (*Name >= '0' && *Name <= '9') {
423 unsigned i = *Name - '0';
424
425 // Check if matching constraint is out of bounds.
426 if (i >= NumOutputs)
427 return false;
428 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
429 // FIXME: This assert is in place temporarily
430 // so we can add more constraints as we hit it.
431 // Eventually, an unknown constraint should just be treated as 'g'.
432 assert(0 && "Unknown input constraint type!");
433 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000434 case '%': // commutative
435 // FIXME: Fail if % is used with the last operand.
436 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000437 case 'i': // immediate integer.
Anders Carlssonb41edf92008-02-18 17:00:25 +0000438 case 'I':
Anders Carlssond04c6e22007-11-27 04:11:28 +0000439 break;
440 case 'r': // general register.
441 info = (ConstraintInfo)(info|CI_AllowsRegister);
442 break;
443 case 'm': // memory operand.
444 info = (ConstraintInfo)(info|CI_AllowsMemory);
445 break;
446 case 'g': // general register, memory operand or immediate integer.
447 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
448 break;
449 }
450
451 Name++;
452 }
453
454 return true;
455}
456
457const char *TargetInfo::getClobbers() const
458{
459 return PrimaryTarget->getClobbers();
460}
461
462