blob: e0a464219b44f3474144fa6a1aff3e67f1308d5c [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) {
Chris Lattner525a0502007-09-22 18:29:59 +000040 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,
Ted Kremenek9c728dc2007-12-12 22:39:36 +000045 FullSourceLoc Loc) {
Chris Lattner1c9bdae2007-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 Lattner525a0502007-09-22 18:29:59 +000050}
51
52
53//===----------------------------------------------------------------------===//
54
Ted Kremenekae360762007-12-03 22:06:55 +000055const char* TargetInfo::getTargetTriple() const {
56 return PrimaryTarget->getTargetTriple();
57}
58
Anders Carlsson44fe49c2007-12-08 19:32:57 +000059const char *TargetInfo::getTargetPrefix() const {
60 return PrimaryTarget->getTargetPrefix();
61}
62
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000066void TargetInfo::DiagnoseNonPortability(FullSourceLoc Loc,
67 unsigned DiagKind) {
Reid Spencer5f016e22007-07-11 17:01:13 +000068 NonPortable = true;
Ted Kremenek9c728dc2007-12-12 22:39:36 +000069 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
72/// GetTargetDefineMap - Get the set of target #defines in an associative
73/// collection for easy lookup.
74static void GetTargetDefineMap(const TargetInfoImpl *Target,
Chris Lattnere36751b2007-07-22 20:11:46 +000075 llvm::StringMap<std::string> &Map) {
Chris Lattnerd15fa822007-10-06 06:57:34 +000076 std::vector<char> Defines;
77 Defines.reserve(4096);
78 Target->getTargetDefines(Defines);
Reid Spencer5f016e22007-07-11 17:01:13 +000079
Chris Lattnerd15fa822007-10-06 06:57:34 +000080 for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size();
81 DefStr != E;) {
82 // Skip the '#define ' portion.
83 assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 &&
84 "#define didn't start with #define!");
85 DefStr += strlen("#define ");
Chris Lattnere36751b2007-07-22 20:11:46 +000086
Chris Lattnerd15fa822007-10-06 06:57:34 +000087 // Find the divider between the key and value.
88 const char *SpacePos = strchr(DefStr, ' ');
89
90 std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue();
91
92 const char *EndPos = strchr(SpacePos+1, '\n');
93 Entry = std::string(SpacePos+1, EndPos);
94 DefStr = EndPos+1;
Reid Spencer5f016e22007-07-11 17:01:13 +000095 }
96}
97
98/// getTargetDefines - Appends the target-specific #define values for this
99/// target set to the specified buffer.
100void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
Chris Lattnereab77922007-10-06 06:29:41 +0000101 // If we have no secondary targets, be a bit more efficient.
102 if (SecondaryTargets.empty()) {
Chris Lattnerd15fa822007-10-06 06:57:34 +0000103 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattnereab77922007-10-06 06:29:41 +0000104 return;
105 }
106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // This is tricky in the face of secondary targets. Specifically,
108 // target-specific #defines that are present and identical across all
109 // secondary targets are turned into #defines, #defines that are present in
110 // the primary target but are missing or different in the secondary targets
111 // are turned into #define_target, and #defines that are not defined in the
112 // primary, but are defined in a secondary are turned into
113 // #define_other_target. This allows the preprocessor to correctly track uses
114 // of target-specific macros.
115
116 // Get the set of primary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000117 llvm::StringMap<std::string> PrimaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // Get the sets of secondary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000121 llvm::StringMap<std::string> *SecondaryDefines
122 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
124 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
125
126 // Loop over all defines in the primary target, processing them until we run
127 // out.
Chris Lattnere36751b2007-07-22 20:11:46 +0000128 for (llvm::StringMap<std::string>::iterator PDI =
129 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
130 std::string DefineName(PDI->getKeyData(),
131 PDI->getKeyData() + PDI->getKeyLength());
132 std::string DefineValue = PDI->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000133
134 // Check to see whether all secondary targets have this #define and whether
135 // it is to the same value. Remember if not, but remove the #define from
136 // their collection in any case if they have it.
137 bool isPortable = true;
138
Chris Lattnere36751b2007-07-22 20:11:46 +0000139 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
140 llvm::StringMap<std::string>::iterator I =
141 SecondaryDefines[i].find(&DefineName[0],
142 &DefineName[0]+DefineName.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 if (I == SecondaryDefines[i].end()) {
144 // Secondary target doesn't have this #define.
145 isPortable = false;
146 } else {
147 // Secondary target has this define, remember if it disagrees.
148 if (isPortable)
Chris Lattnere36751b2007-07-22 20:11:46 +0000149 isPortable = I->getValue() == DefineValue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 // Remove it from the secondary target unconditionally.
151 SecondaryDefines[i].erase(I);
152 }
153 }
154
155 // If this define is non-portable, turn it into #define_target, otherwise
156 // just use #define.
157 const char *Command = isPortable ? "#define " : "#define_target ";
158 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
159
160 // Insert "defname defvalue\n".
161 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
162 Buffer.push_back(' ');
163 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
164 Buffer.push_back('\n');
165 }
166
167 // Now that all of the primary target's defines have been handled and removed
168 // from the secondary target's define sets, go through the remaining secondary
169 // target's #defines and taint them.
Chris Lattnere36751b2007-07-22 20:11:46 +0000170 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
171 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 while (!Defs.empty()) {
Chris Lattnere36751b2007-07-22 20:11:46 +0000173 const char *DefStart = Defs.begin()->getKeyData();
174 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
Reid Spencer5f016e22007-07-11 17:01:13 +0000175
176 // Insert "#define_other_target defname".
177 const char *Command = "#define_other_target ";
178 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Chris Lattnere36751b2007-07-22 20:11:46 +0000179 Buffer.insert(Buffer.end(), DefStart, DefEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 Buffer.push_back('\n');
181
182 // If any other secondary targets have this same define, remove it from
183 // them to avoid duplicate #define_other_target directives.
Chris Lattnere36751b2007-07-22 20:11:46 +0000184 for (unsigned j = i+1; j != e; ++j) {
185 llvm::StringMap<std::string>::iterator I =
186 SecondaryDefines[j].find(DefStart, DefEnd);
187 if (I != SecondaryDefines[j].end())
188 SecondaryDefines[j].erase(I);
189 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 Defs.erase(Defs.begin());
191 }
192 }
Chris Lattnere36751b2007-07-22 20:11:46 +0000193
194 delete[] SecondaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000195}
196
197/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
198/// target, diagnosing whether this is non-portable across the secondary
199/// targets.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000200void TargetInfo::ComputeWCharInfo(FullSourceLoc Loc) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000201 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
Reid Spencer5f016e22007-07-11 17:01:13 +0000202
203 // Check whether this is portable across the secondary targets if the T-U is
204 // portable so far.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000205 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
206 unsigned Width, Align;
207 SecondaryTargets[i]->getWCharInfo(Width, Align);
208 if (Width != WCharWidth || Align != WCharAlign)
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000210 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
213
214/// getTargetBuiltins - Return information about target-specific builtins for
215/// the current primary target, and info about which builtins are non-portable
216/// across the current set of primary and secondary targets.
217void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
218 unsigned &NumRecords,
219 std::vector<const char *> &NPortable) const {
220 // Get info about what actual builtins we will expose.
221 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
222 if (SecondaryTargets.empty()) return;
223
224 // Compute the set of non-portable builtins.
225
226 // Start by computing a mapping from the primary target's builtins to their
227 // info records for efficient lookup.
Chris Lattnere36751b2007-07-22 20:11:46 +0000228 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
229 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
230 const char *BIName = Records[i].Name;
231 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
232 = Records+i;
233 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
235 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
236 // Get the builtins for this secondary target.
237 const Builtin::Info *Records2nd;
238 unsigned NumRecords2nd;
239 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
240
241 // Remember all of the secondary builtin names.
242 std::set<std::string> BuiltinNames2nd;
243
244 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
245 BuiltinNames2nd.insert(Records2nd[j].Name);
246
247 // Check to see if the primary target has this builtin.
Chris Lattnere36751b2007-07-22 20:11:46 +0000248 llvm::StringMap<const Builtin::Info*>::iterator I =
249 PrimaryRecs.find(Records2nd[j].Name,
250 Records2nd[j].Name+strlen(Records2nd[j].Name));
251 if (I != PrimaryRecs.end()) {
252 const Builtin::Info *PrimBI = I->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // If does. If they are not identical, mark the builtin as being
254 // non-portable.
255 if (Records2nd[j] != *PrimBI)
256 NPortable.push_back(PrimBI->Name);
257 } else {
258 // The primary target doesn't have this, it is non-portable.
259 NPortable.push_back(Records2nd[j].Name);
260 }
261 }
262
263 // Now that we checked all the secondary builtins, check to see if the
264 // primary target has any builtins that the secondary one doesn't. If so,
265 // then those are non-portable.
266 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
267 if (!BuiltinNames2nd.count(Records[j].Name))
268 NPortable.push_back(Records[j].Name);
269 }
270 }
271}
272
Anders Carlssonfb5e5ba2007-10-13 00:45:48 +0000273/// getVAListDeclaration - Return the declaration to use for
274/// __builtin_va_list, which is target-specific.
275const char *TargetInfo::getVAListDeclaration() const {
276 return PrimaryTarget->getVAListDeclaration();
277}
Reid Spencer5f016e22007-07-11 17:01:13 +0000278
Anders Carlssonea041752008-02-06 00:11:32 +0000279static void removeGCCRegisterPrefix(const char *&Name)
280{
281 if (Name[0] == '%' || Name[0] == '#')
282 Name++;
283}
284
Anders Carlsson3346ae62007-11-24 23:38:12 +0000285/// isValidGCCRegisterName - Returns whether the passed in string
286/// is a valid register name according to GCC. This is used by Sema for
287/// inline asm statements.
288bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000289 const char * const *Names;
290 unsigned NumNames;
291
292 // Get rid of any register prefix.
Anders Carlssonea041752008-02-06 00:11:32 +0000293 removeGCCRegisterPrefix(Name);
294
Anders Carlsson6fa90862007-11-25 00:25:21 +0000295
296 if (strcmp(Name, "memory") == 0 ||
297 strcmp(Name, "cc") == 0)
298 return true;
299
300 PrimaryTarget->getGCCRegNames(Names, NumNames);
301
302 // If we have a number it maps to an entry in the register name array.
303 if (isdigit(Name[0])) {
304 char *End;
305 int n = (int)strtol(Name, &End, 0);
306 if (*End == 0)
307 return n >= 0 && (unsigned)n < NumNames;
308 }
309
310 // Check register names.
311 for (unsigned i = 0; i < NumNames; i++) {
312 if (strcmp(Name, Names[i]) == 0)
313 return true;
314 }
315
316 // Now check aliases.
317 const TargetInfoImpl::GCCRegAlias *Aliases;
318 unsigned NumAliases;
319
320 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
321 for (unsigned i = 0; i < NumAliases; i++) {
322 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
323 if (!Aliases[i].Aliases[j])
324 break;
325 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
326 return true;
327 }
328 }
329
Anders Carlsson3346ae62007-11-24 23:38:12 +0000330 return false;
331}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000332
333const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
334{
335 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
336
Anders Carlssonea041752008-02-06 00:11:32 +0000337 removeGCCRegisterPrefix(Name);
Anders Carlssonef3577d2008-02-05 23:30:20 +0000338
Anders Carlssond04c6e22007-11-27 04:11:28 +0000339 const char * const *Names;
340 unsigned NumNames;
341
342 PrimaryTarget->getGCCRegNames(Names, NumNames);
343
344 // First, check if we have a number.
345 if (isdigit(Name[0])) {
346 char *End;
347 int n = (int)strtol(Name, &End, 0);
348 if (*End == 0) {
349 assert(n >= 0 && (unsigned)n < NumNames &&
350 "Out of bounds register number!");
351 return Names[n];
352 }
353 }
354
355 // Now check aliases.
356 const TargetInfoImpl::GCCRegAlias *Aliases;
357 unsigned NumAliases;
358
359 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
360 for (unsigned i = 0; i < NumAliases; i++) {
361 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
362 if (!Aliases[i].Aliases[j])
363 break;
364 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
365 return Aliases[i].Register;
366 }
367 }
368
369 return Name;
370}
371
372bool TargetInfo::validateOutputConstraint(const char *Name,
373 ConstraintInfo &info) const
374{
375 // An output constraint must start with '=' or '+'
376 if (*Name != '=' && *Name != '+')
377 return false;
378
379 if (*Name == '+')
380 info = CI_ReadWrite;
381 else
382 info = CI_None;
383
384 Name++;
385 while (*Name) {
386 switch (*Name) {
387 default:
388 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
389 // FIXME: This assert is in place temporarily
390 // so we can add more constraints as we hit it.
391 // Eventually, an unknown constraint should just be treated as 'g'.
392 assert(0 && "Unknown output constraint type!");
393 }
394 case '&': // early clobber.
395 break;
396 case 'r': // general register.
397 info = (ConstraintInfo)(info|CI_AllowsRegister);
398 break;
399 case 'm': // memory operand.
400 info = (ConstraintInfo)(info|CI_AllowsMemory);
401 break;
402 case 'g': // general register, memory operand or immediate integer.
403 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
404 break;
405 }
406
407 Name++;
408 }
409
410 return true;
411}
412
413bool TargetInfo::validateInputConstraint(const char *Name,
414 unsigned NumOutputs,
415 ConstraintInfo &info) const
416{
417 while (*Name) {
418 switch (*Name) {
419 default:
420 // Check if we have a matching constraint
421 if (*Name >= '0' && *Name <= '9') {
422 unsigned i = *Name - '0';
423
424 // Check if matching constraint is out of bounds.
425 if (i >= NumOutputs)
426 return false;
427 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
428 // FIXME: This assert is in place temporarily
429 // so we can add more constraints as we hit it.
430 // Eventually, an unknown constraint should just be treated as 'g'.
431 assert(0 && "Unknown input constraint type!");
432 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000433 case '%': // commutative
434 // FIXME: Fail if % is used with the last operand.
435 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000436 case 'i': // immediate integer.
437 break;
438 case 'r': // general register.
439 info = (ConstraintInfo)(info|CI_AllowsRegister);
440 break;
441 case 'm': // memory operand.
442 info = (ConstraintInfo)(info|CI_AllowsMemory);
443 break;
444 case 'g': // general register, memory operand or immediate integer.
445 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
446 break;
447 }
448
449 Name++;
450 }
451
452 return true;
453}
454
455const char *TargetInfo::getClobbers() const
456{
457 return PrimaryTarget->getClobbers();
458}
459
460