blob: 4c9d1c8afe258149d7d06b03d1d3926ac8512a34 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 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 Carlsson3346ae62007-11-24 23:38:12 +0000279/// isValidGCCRegisterName - Returns whether the passed in string
280/// is a valid register name according to GCC. This is used by Sema for
281/// inline asm statements.
282bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000283 const char * const *Names;
284 unsigned NumNames;
285
286 // Get rid of any register prefix.
287 if (Name[0] == '%' || Name[0] == '#')
288 Name++;
289
290 if (strcmp(Name, "memory") == 0 ||
291 strcmp(Name, "cc") == 0)
292 return true;
293
294 PrimaryTarget->getGCCRegNames(Names, NumNames);
295
296 // If we have a number it maps to an entry in the register name array.
297 if (isdigit(Name[0])) {
298 char *End;
299 int n = (int)strtol(Name, &End, 0);
300 if (*End == 0)
301 return n >= 0 && (unsigned)n < NumNames;
302 }
303
304 // Check register names.
305 for (unsigned i = 0; i < NumNames; i++) {
306 if (strcmp(Name, Names[i]) == 0)
307 return true;
308 }
309
310 // Now check aliases.
311 const TargetInfoImpl::GCCRegAlias *Aliases;
312 unsigned NumAliases;
313
314 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
315 for (unsigned i = 0; i < NumAliases; i++) {
316 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
317 if (!Aliases[i].Aliases[j])
318 break;
319 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
320 return true;
321 }
322 }
323
Anders Carlsson3346ae62007-11-24 23:38:12 +0000324 return false;
325}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000326
327const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
328{
329 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
330
331 const char * const *Names;
332 unsigned NumNames;
333
334 PrimaryTarget->getGCCRegNames(Names, NumNames);
335
336 // First, check if we have a number.
337 if (isdigit(Name[0])) {
338 char *End;
339 int n = (int)strtol(Name, &End, 0);
340 if (*End == 0) {
341 assert(n >= 0 && (unsigned)n < NumNames &&
342 "Out of bounds register number!");
343 return Names[n];
344 }
345 }
346
347 // Now check aliases.
348 const TargetInfoImpl::GCCRegAlias *Aliases;
349 unsigned NumAliases;
350
351 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
352 for (unsigned i = 0; i < NumAliases; i++) {
353 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
354 if (!Aliases[i].Aliases[j])
355 break;
356 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
357 return Aliases[i].Register;
358 }
359 }
360
361 return Name;
362}
363
364bool TargetInfo::validateOutputConstraint(const char *Name,
365 ConstraintInfo &info) const
366{
367 // An output constraint must start with '=' or '+'
368 if (*Name != '=' && *Name != '+')
369 return false;
370
371 if (*Name == '+')
372 info = CI_ReadWrite;
373 else
374 info = CI_None;
375
376 Name++;
377 while (*Name) {
378 switch (*Name) {
379 default:
380 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
381 // FIXME: This assert is in place temporarily
382 // so we can add more constraints as we hit it.
383 // Eventually, an unknown constraint should just be treated as 'g'.
384 assert(0 && "Unknown output constraint type!");
385 }
386 case '&': // early clobber.
387 break;
388 case 'r': // general register.
389 info = (ConstraintInfo)(info|CI_AllowsRegister);
390 break;
391 case 'm': // memory operand.
392 info = (ConstraintInfo)(info|CI_AllowsMemory);
393 break;
394 case 'g': // general register, memory operand or immediate integer.
395 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
396 break;
397 }
398
399 Name++;
400 }
401
402 return true;
403}
404
405bool TargetInfo::validateInputConstraint(const char *Name,
406 unsigned NumOutputs,
407 ConstraintInfo &info) const
408{
409 while (*Name) {
410 switch (*Name) {
411 default:
412 // Check if we have a matching constraint
413 if (*Name >= '0' && *Name <= '9') {
414 unsigned i = *Name - '0';
415
416 // Check if matching constraint is out of bounds.
417 if (i >= NumOutputs)
418 return false;
419 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
420 // FIXME: This assert is in place temporarily
421 // so we can add more constraints as we hit it.
422 // Eventually, an unknown constraint should just be treated as 'g'.
423 assert(0 && "Unknown input constraint type!");
424 }
Anders Carlsson44fe49c2007-12-08 19:32:57 +0000425 case '%': // commutative
426 // FIXME: Fail if % is used with the last operand.
427 break;
Anders Carlssond04c6e22007-11-27 04:11:28 +0000428 case 'i': // immediate integer.
429 break;
430 case 'r': // general register.
431 info = (ConstraintInfo)(info|CI_AllowsRegister);
432 break;
433 case 'm': // memory operand.
434 info = (ConstraintInfo)(info|CI_AllowsMemory);
435 break;
436 case 'g': // general register, memory operand or immediate integer.
437 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
438 break;
439 }
440
441 Name++;
442 }
443
444 return true;
445}
446
447const char *TargetInfo::getClobbers() const
448{
449 return PrimaryTarget->getClobbers();
450}
451
452