blob: ec91bdd1a25a1726a0147ee8fd5cc5128305761e [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,
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 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
Reid Spencer5f016e22007-07-11 17:01:13 +000055/// DiagnoseNonPortability - When a use of a non-portable target feature is
56/// used, this method emits the diagnostic and marks the translation unit as
57/// non-portable.
58void TargetInfo::DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind) {
59 NonPortable = true;
60 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
61}
62
63/// GetTargetDefineMap - Get the set of target #defines in an associative
64/// collection for easy lookup.
65static void GetTargetDefineMap(const TargetInfoImpl *Target,
Chris Lattnere36751b2007-07-22 20:11:46 +000066 llvm::StringMap<std::string> &Map) {
Chris Lattnerd15fa822007-10-06 06:57:34 +000067 std::vector<char> Defines;
68 Defines.reserve(4096);
69 Target->getTargetDefines(Defines);
Reid Spencer5f016e22007-07-11 17:01:13 +000070
Chris Lattnerd15fa822007-10-06 06:57:34 +000071 for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size();
72 DefStr != E;) {
73 // Skip the '#define ' portion.
74 assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 &&
75 "#define didn't start with #define!");
76 DefStr += strlen("#define ");
Chris Lattnere36751b2007-07-22 20:11:46 +000077
Chris Lattnerd15fa822007-10-06 06:57:34 +000078 // Find the divider between the key and value.
79 const char *SpacePos = strchr(DefStr, ' ');
80
81 std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue();
82
83 const char *EndPos = strchr(SpacePos+1, '\n');
84 Entry = std::string(SpacePos+1, EndPos);
85 DefStr = EndPos+1;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 }
87}
88
89/// getTargetDefines - Appends the target-specific #define values for this
90/// target set to the specified buffer.
91void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
Chris Lattnereab77922007-10-06 06:29:41 +000092 // If we have no secondary targets, be a bit more efficient.
93 if (SecondaryTargets.empty()) {
Chris Lattnerd15fa822007-10-06 06:57:34 +000094 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattnereab77922007-10-06 06:29:41 +000095 return;
96 }
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098 // This is tricky in the face of secondary targets. Specifically,
99 // target-specific #defines that are present and identical across all
100 // secondary targets are turned into #defines, #defines that are present in
101 // the primary target but are missing or different in the secondary targets
102 // are turned into #define_target, and #defines that are not defined in the
103 // primary, but are defined in a secondary are turned into
104 // #define_other_target. This allows the preprocessor to correctly track uses
105 // of target-specific macros.
106
107 // Get the set of primary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000108 llvm::StringMap<std::string> PrimaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 // Get the sets of secondary #defines.
Chris Lattnere36751b2007-07-22 20:11:46 +0000112 llvm::StringMap<std::string> *SecondaryDefines
113 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
115 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
116
117 // Loop over all defines in the primary target, processing them until we run
118 // out.
Chris Lattnere36751b2007-07-22 20:11:46 +0000119 for (llvm::StringMap<std::string>::iterator PDI =
120 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
121 std::string DefineName(PDI->getKeyData(),
122 PDI->getKeyData() + PDI->getKeyLength());
123 std::string DefineValue = PDI->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
125 // Check to see whether all secondary targets have this #define and whether
126 // it is to the same value. Remember if not, but remove the #define from
127 // their collection in any case if they have it.
128 bool isPortable = true;
129
Chris Lattnere36751b2007-07-22 20:11:46 +0000130 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
131 llvm::StringMap<std::string>::iterator I =
132 SecondaryDefines[i].find(&DefineName[0],
133 &DefineName[0]+DefineName.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 if (I == SecondaryDefines[i].end()) {
135 // Secondary target doesn't have this #define.
136 isPortable = false;
137 } else {
138 // Secondary target has this define, remember if it disagrees.
139 if (isPortable)
Chris Lattnere36751b2007-07-22 20:11:46 +0000140 isPortable = I->getValue() == DefineValue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 // Remove it from the secondary target unconditionally.
142 SecondaryDefines[i].erase(I);
143 }
144 }
145
146 // If this define is non-portable, turn it into #define_target, otherwise
147 // just use #define.
148 const char *Command = isPortable ? "#define " : "#define_target ";
149 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
150
151 // Insert "defname defvalue\n".
152 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
153 Buffer.push_back(' ');
154 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
155 Buffer.push_back('\n');
156 }
157
158 // Now that all of the primary target's defines have been handled and removed
159 // from the secondary target's define sets, go through the remaining secondary
160 // target's #defines and taint them.
Chris Lattnere36751b2007-07-22 20:11:46 +0000161 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
162 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 while (!Defs.empty()) {
Chris Lattnere36751b2007-07-22 20:11:46 +0000164 const char *DefStart = Defs.begin()->getKeyData();
165 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
167 // Insert "#define_other_target defname".
168 const char *Command = "#define_other_target ";
169 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Chris Lattnere36751b2007-07-22 20:11:46 +0000170 Buffer.insert(Buffer.end(), DefStart, DefEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 Buffer.push_back('\n');
172
173 // If any other secondary targets have this same define, remove it from
174 // them to avoid duplicate #define_other_target directives.
Chris Lattnere36751b2007-07-22 20:11:46 +0000175 for (unsigned j = i+1; j != e; ++j) {
176 llvm::StringMap<std::string>::iterator I =
177 SecondaryDefines[j].find(DefStart, DefEnd);
178 if (I != SecondaryDefines[j].end())
179 SecondaryDefines[j].erase(I);
180 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 Defs.erase(Defs.begin());
182 }
183 }
Chris Lattnere36751b2007-07-22 20:11:46 +0000184
185 delete[] SecondaryDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
188/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
189/// target, diagnosing whether this is non-portable across the secondary
190/// targets.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000191void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
192 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193
194 // Check whether this is portable across the secondary targets if the T-U is
195 // portable so far.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000196 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
197 unsigned Width, Align;
198 SecondaryTargets[i]->getWCharInfo(Width, Align);
199 if (Width != WCharWidth || Align != WCharAlign)
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000201 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000202}
203
204
205/// getTargetBuiltins - Return information about target-specific builtins for
206/// the current primary target, and info about which builtins are non-portable
207/// across the current set of primary and secondary targets.
208void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
209 unsigned &NumRecords,
210 std::vector<const char *> &NPortable) const {
211 // Get info about what actual builtins we will expose.
212 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
213 if (SecondaryTargets.empty()) return;
214
215 // Compute the set of non-portable builtins.
216
217 // Start by computing a mapping from the primary target's builtins to their
218 // info records for efficient lookup.
Chris Lattnere36751b2007-07-22 20:11:46 +0000219 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
220 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
221 const char *BIName = Records[i].Name;
222 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
223 = Records+i;
224 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
226 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
227 // Get the builtins for this secondary target.
228 const Builtin::Info *Records2nd;
229 unsigned NumRecords2nd;
230 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
231
232 // Remember all of the secondary builtin names.
233 std::set<std::string> BuiltinNames2nd;
234
235 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
236 BuiltinNames2nd.insert(Records2nd[j].Name);
237
238 // Check to see if the primary target has this builtin.
Chris Lattnere36751b2007-07-22 20:11:46 +0000239 llvm::StringMap<const Builtin::Info*>::iterator I =
240 PrimaryRecs.find(Records2nd[j].Name,
241 Records2nd[j].Name+strlen(Records2nd[j].Name));
242 if (I != PrimaryRecs.end()) {
243 const Builtin::Info *PrimBI = I->getValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 // If does. If they are not identical, mark the builtin as being
245 // non-portable.
246 if (Records2nd[j] != *PrimBI)
247 NPortable.push_back(PrimBI->Name);
248 } else {
249 // The primary target doesn't have this, it is non-portable.
250 NPortable.push_back(Records2nd[j].Name);
251 }
252 }
253
254 // Now that we checked all the secondary builtins, check to see if the
255 // primary target has any builtins that the secondary one doesn't. If so,
256 // then those are non-portable.
257 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
258 if (!BuiltinNames2nd.count(Records[j].Name))
259 NPortable.push_back(Records[j].Name);
260 }
261 }
262}
263
Anders Carlssonfb5e5ba2007-10-13 00:45:48 +0000264/// getVAListDeclaration - Return the declaration to use for
265/// __builtin_va_list, which is target-specific.
266const char *TargetInfo::getVAListDeclaration() const {
267 return PrimaryTarget->getVAListDeclaration();
268}
Reid Spencer5f016e22007-07-11 17:01:13 +0000269
Anders Carlsson3346ae62007-11-24 23:38:12 +0000270/// isValidGCCRegisterName - Returns whether the passed in string
271/// is a valid register name according to GCC. This is used by Sema for
272/// inline asm statements.
273bool TargetInfo::isValidGCCRegisterName(const char *Name) const {
Anders Carlsson6fa90862007-11-25 00:25:21 +0000274 const char * const *Names;
275 unsigned NumNames;
276
277 // Get rid of any register prefix.
278 if (Name[0] == '%' || Name[0] == '#')
279 Name++;
280
281 if (strcmp(Name, "memory") == 0 ||
282 strcmp(Name, "cc") == 0)
283 return true;
284
285 PrimaryTarget->getGCCRegNames(Names, NumNames);
286
287 // If we have a number it maps to an entry in the register name array.
288 if (isdigit(Name[0])) {
289 char *End;
290 int n = (int)strtol(Name, &End, 0);
291 if (*End == 0)
292 return n >= 0 && (unsigned)n < NumNames;
293 }
294
295 // Check register names.
296 for (unsigned i = 0; i < NumNames; i++) {
297 if (strcmp(Name, Names[i]) == 0)
298 return true;
299 }
300
301 // Now check aliases.
302 const TargetInfoImpl::GCCRegAlias *Aliases;
303 unsigned NumAliases;
304
305 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
306 for (unsigned i = 0; i < NumAliases; i++) {
307 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
308 if (!Aliases[i].Aliases[j])
309 break;
310 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
311 return true;
312 }
313 }
314
Anders Carlsson3346ae62007-11-24 23:38:12 +0000315 return false;
316}
Anders Carlssond04c6e22007-11-27 04:11:28 +0000317
318const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
319{
320 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
321
322 const char * const *Names;
323 unsigned NumNames;
324
325 PrimaryTarget->getGCCRegNames(Names, NumNames);
326
327 // First, check if we have a number.
328 if (isdigit(Name[0])) {
329 char *End;
330 int n = (int)strtol(Name, &End, 0);
331 if (*End == 0) {
332 assert(n >= 0 && (unsigned)n < NumNames &&
333 "Out of bounds register number!");
334 return Names[n];
335 }
336 }
337
338 // Now check aliases.
339 const TargetInfoImpl::GCCRegAlias *Aliases;
340 unsigned NumAliases;
341
342 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
343 for (unsigned i = 0; i < NumAliases; i++) {
344 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
345 if (!Aliases[i].Aliases[j])
346 break;
347 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
348 return Aliases[i].Register;
349 }
350 }
351
352 return Name;
353}
354
355bool TargetInfo::validateOutputConstraint(const char *Name,
356 ConstraintInfo &info) const
357{
358 // An output constraint must start with '=' or '+'
359 if (*Name != '=' && *Name != '+')
360 return false;
361
362 if (*Name == '+')
363 info = CI_ReadWrite;
364 else
365 info = CI_None;
366
367 Name++;
368 while (*Name) {
369 switch (*Name) {
370 default:
371 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
372 // FIXME: This assert is in place temporarily
373 // so we can add more constraints as we hit it.
374 // Eventually, an unknown constraint should just be treated as 'g'.
375 assert(0 && "Unknown output constraint type!");
376 }
377 case '&': // early clobber.
378 break;
379 case 'r': // general register.
380 info = (ConstraintInfo)(info|CI_AllowsRegister);
381 break;
382 case 'm': // memory operand.
383 info = (ConstraintInfo)(info|CI_AllowsMemory);
384 break;
385 case 'g': // general register, memory operand or immediate integer.
386 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
387 break;
388 }
389
390 Name++;
391 }
392
393 return true;
394}
395
396bool TargetInfo::validateInputConstraint(const char *Name,
397 unsigned NumOutputs,
398 ConstraintInfo &info) const
399{
400 while (*Name) {
401 switch (*Name) {
402 default:
403 // Check if we have a matching constraint
404 if (*Name >= '0' && *Name <= '9') {
405 unsigned i = *Name - '0';
406
407 // Check if matching constraint is out of bounds.
408 if (i >= NumOutputs)
409 return false;
410 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
411 // FIXME: This assert is in place temporarily
412 // so we can add more constraints as we hit it.
413 // Eventually, an unknown constraint should just be treated as 'g'.
414 assert(0 && "Unknown input constraint type!");
415 }
416 case 'i': // immediate integer.
417 break;
418 case 'r': // general register.
419 info = (ConstraintInfo)(info|CI_AllowsRegister);
420 break;
421 case 'm': // memory operand.
422 info = (ConstraintInfo)(info|CI_AllowsMemory);
423 break;
424 case 'g': // general register, memory operand or immediate integer.
425 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
426 break;
427 }
428
429 Name++;
430 }
431
432 return true;
433}
434
435const char *TargetInfo::getClobbers() const
436{
437 return PrimaryTarget->getClobbers();
438}
439
440