blob: fc6976af41fd5c3bd7cd0e39f9457a73d262e0a2 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- TargetInfo.cpp - Information about Target machine ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner858eece2007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson49dadd62007-11-25 00:25:21 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/STLExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include <set>
21using namespace clang;
22
23void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
24
25
Chris Lattner858eece2007-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 Kremenekd7f64cd2007-12-12 22:39:36 +000032 FullSourceLoc Loc) {
Chris Lattner858eece2007-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 Kremenekd7f64cd2007-12-12 22:39:36 +000039 FullSourceLoc Loc) {
Chris Lattner858eece2007-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 Kremenekd7f64cd2007-12-12 22:39:36 +000045 FullSourceLoc Loc) {
Chris Lattner36f46b82007-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 Lattner858eece2007-09-22 18:29:59 +000050}
51
52
53//===----------------------------------------------------------------------===//
54
Ted Kremenek40499482007-12-03 22:06:55 +000055const char* TargetInfo::getTargetTriple() const {
56 return PrimaryTarget->getTargetTriple();
57}
58
Anders Carlsson333052c2007-12-08 19:32:57 +000059const char *TargetInfo::getTargetPrefix() const {
60 return PrimaryTarget->getTargetPrefix();
61}
62
Chris Lattner4b009652007-07-25 00:24:17 +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 Kremenekd7f64cd2007-12-12 22:39:36 +000066void TargetInfo::DiagnoseNonPortability(FullSourceLoc Loc,
67 unsigned DiagKind) {
Chris Lattner4b009652007-07-25 00:24:17 +000068 NonPortable = true;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000069 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
Chris Lattner4b009652007-07-25 00:24:17 +000070}
71
72/// GetTargetDefineMap - Get the set of target #defines in an associative
73/// collection for easy lookup.
74static void GetTargetDefineMap(const TargetInfoImpl *Target,
75 llvm::StringMap<std::string> &Map) {
Chris Lattner0db667a2007-10-06 06:57:34 +000076 std::vector<char> Defines;
77 Defines.reserve(4096);
78 Target->getTargetDefines(Defines);
Chris Lattner4b009652007-07-25 00:24:17 +000079
Chris Lattner0db667a2007-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 Lattner4b009652007-07-25 00:24:17 +000086
Chris Lattner0db667a2007-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;
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner8c9d29f2007-10-06 06:29:41 +0000101 // If we have no secondary targets, be a bit more efficient.
102 if (SecondaryTargets.empty()) {
Chris Lattner0db667a2007-10-06 06:57:34 +0000103 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattner8c9d29f2007-10-06 06:29:41 +0000104 return;
105 }
106
Chris Lattner4b009652007-07-25 00:24:17 +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.
117 llvm::StringMap<std::string> PrimaryDefines;
118 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
119
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // Get the sets of secondary #defines.
121 llvm::StringMap<std::string> *SecondaryDefines
122 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
123 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.
128 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();
133
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
139 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());
143 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)
149 isPortable = I->getValue() == DefineValue;
150 // 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.
170 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
171 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
172 while (!Defs.empty()) {
173 const char *DefStart = Defs.begin()->getKeyData();
174 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
175
176 // Insert "#define_other_target defname".
177 const char *Command = "#define_other_target ";
178 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
179 Buffer.insert(Buffer.end(), DefStart, DefEnd);
180 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.
184 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 }
190 Defs.erase(Defs.begin());
191 }
192 }
193
194 delete[] SecondaryDefines;
195}
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 Kremenekd7f64cd2007-12-12 22:39:36 +0000200void TargetInfo::ComputeWCharInfo(FullSourceLoc Loc) {
Chris Lattner4b009652007-07-25 00:24:17 +0000201 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
202
203 // Check whether this is portable across the secondary targets if the T-U is
204 // portable so far.
205 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)
209 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
210 }
211}
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.
228 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 }
234
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.
248 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();
253 // 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 Carlsson8b58e8a2007-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}
Chris Lattner4b009652007-07-25 00:24:17 +0000278
Anders Carlsson7dd1c952007-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 Carlsson49dadd62007-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 Carlsson7dd1c952007-11-24 23:38:12 +0000324 return false;
325}
Anders Carlsson4ce42302007-11-27 04:11:28 +0000326
327const char *TargetInfo::getNormalizedGCCRegisterName(const char *Name) const
328{
329 assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
330
Anders Carlsson3fb39b12008-02-05 23:30:20 +0000331 if (strcmp(Name, "memory") == 0)
332 return "~{memory}";
333
Anders Carlsson4ce42302007-11-27 04:11:28 +0000334 const char * const *Names;
335 unsigned NumNames;
336
337 PrimaryTarget->getGCCRegNames(Names, NumNames);
338
339 // First, check if we have a number.
340 if (isdigit(Name[0])) {
341 char *End;
342 int n = (int)strtol(Name, &End, 0);
343 if (*End == 0) {
344 assert(n >= 0 && (unsigned)n < NumNames &&
345 "Out of bounds register number!");
346 return Names[n];
347 }
348 }
349
350 // Now check aliases.
351 const TargetInfoImpl::GCCRegAlias *Aliases;
352 unsigned NumAliases;
353
354 PrimaryTarget->getGCCRegAliases(Aliases, NumAliases);
355 for (unsigned i = 0; i < NumAliases; i++) {
356 for (unsigned j = 0 ; j < llvm::array_lengthof(Aliases[i].Aliases); j++) {
357 if (!Aliases[i].Aliases[j])
358 break;
359 if (strcmp(Aliases[i].Aliases[j], Name) == 0)
360 return Aliases[i].Register;
361 }
362 }
363
364 return Name;
365}
366
367bool TargetInfo::validateOutputConstraint(const char *Name,
368 ConstraintInfo &info) const
369{
370 // An output constraint must start with '=' or '+'
371 if (*Name != '=' && *Name != '+')
372 return false;
373
374 if (*Name == '+')
375 info = CI_ReadWrite;
376 else
377 info = CI_None;
378
379 Name++;
380 while (*Name) {
381 switch (*Name) {
382 default:
383 if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
384 // FIXME: This assert is in place temporarily
385 // so we can add more constraints as we hit it.
386 // Eventually, an unknown constraint should just be treated as 'g'.
387 assert(0 && "Unknown output constraint type!");
388 }
389 case '&': // early clobber.
390 break;
391 case 'r': // general register.
392 info = (ConstraintInfo)(info|CI_AllowsRegister);
393 break;
394 case 'm': // memory operand.
395 info = (ConstraintInfo)(info|CI_AllowsMemory);
396 break;
397 case 'g': // general register, memory operand or immediate integer.
398 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
399 break;
400 }
401
402 Name++;
403 }
404
405 return true;
406}
407
408bool TargetInfo::validateInputConstraint(const char *Name,
409 unsigned NumOutputs,
410 ConstraintInfo &info) const
411{
412 while (*Name) {
413 switch (*Name) {
414 default:
415 // Check if we have a matching constraint
416 if (*Name >= '0' && *Name <= '9') {
417 unsigned i = *Name - '0';
418
419 // Check if matching constraint is out of bounds.
420 if (i >= NumOutputs)
421 return false;
422 } else if (!PrimaryTarget->validateAsmConstraint(*Name, info)) {
423 // FIXME: This assert is in place temporarily
424 // so we can add more constraints as we hit it.
425 // Eventually, an unknown constraint should just be treated as 'g'.
426 assert(0 && "Unknown input constraint type!");
427 }
Anders Carlsson333052c2007-12-08 19:32:57 +0000428 case '%': // commutative
429 // FIXME: Fail if % is used with the last operand.
430 break;
Anders Carlsson4ce42302007-11-27 04:11:28 +0000431 case 'i': // immediate integer.
432 break;
433 case 'r': // general register.
434 info = (ConstraintInfo)(info|CI_AllowsRegister);
435 break;
436 case 'm': // memory operand.
437 info = (ConstraintInfo)(info|CI_AllowsMemory);
438 break;
439 case 'g': // general register, memory operand or immediate integer.
440 info = (ConstraintInfo)(info|CI_AllowsMemory|CI_AllowsRegister);
441 break;
442 }
443
444 Name++;
445 }
446
447 return true;
448}
449
450const char *TargetInfo::getClobbers() const
451{
452 return PrimaryTarget->getClobbers();
453}
454
455