blob: 3b20d3aa554c572ccd25a69aacf59147b415238b [file] [log] [blame]
Chris Lattner1e27fe12006-10-14 07:06:20 +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"
Chris Lattner10a5b382007-01-29 05:24:35 +000016#include "clang/AST/Builtins.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000017#include "llvm/ADT/APFloat.h"
Anders Carlsson290aa852007-11-25 00:25:21 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/STLExtras.h"
Chris Lattner10a5b382007-01-29 05:24:35 +000020#include <set>
Chris Lattner1e27fe12006-10-14 07:06:20 +000021using namespace clang;
22
Chris Lattner2194ddc2006-10-14 18:32:26 +000023void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
24
25
Chris Lattnerec0a6d92007-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 Lattnerc2d09cf2007-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 Lattnerec0a6d92007-09-22 18:29:59 +000050}
51
52
53//===----------------------------------------------------------------------===//
54
Chris Lattner1e27fe12006-10-14 07:06:20 +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;
Chris Lattnerf033c142007-06-22 19:05:19 +000060 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
Chris Lattner1e27fe12006-10-14 07:06:20 +000061}
62
Chris Lattner2194ddc2006-10-14 18:32:26 +000063/// GetTargetDefineMap - Get the set of target #defines in an associative
64/// collection for easy lookup.
65static void GetTargetDefineMap(const TargetInfoImpl *Target,
Chris Lattnera81b3362007-07-22 20:11:46 +000066 llvm::StringMap<std::string> &Map) {
Chris Lattnerb2d486a2007-10-06 06:57:34 +000067 std::vector<char> Defines;
68 Defines.reserve(4096);
69 Target->getTargetDefines(Defines);
Chris Lattner2194ddc2006-10-14 18:32:26 +000070
Chris Lattnerb2d486a2007-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 Lattnera81b3362007-07-22 20:11:46 +000077
Chris Lattnerb2d486a2007-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;
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnerb5fc6fb2007-10-06 06:29:41 +000092 // If we have no secondary targets, be a bit more efficient.
93 if (SecondaryTargets.empty()) {
Chris Lattnerb2d486a2007-10-06 06:57:34 +000094 PrimaryTarget->getTargetDefines(Buffer);
Chris Lattnerb5fc6fb2007-10-06 06:29:41 +000095 return;
96 }
97
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnera81b3362007-07-22 20:11:46 +0000108 llvm::StringMap<std::string> PrimaryDefines;
Chris Lattner2194ddc2006-10-14 18:32:26 +0000109 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
110
111 // Get the sets of secondary #defines.
Chris Lattnera81b3362007-07-22 20:11:46 +0000112 llvm::StringMap<std::string> *SecondaryDefines
113 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnera81b3362007-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();
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnera81b3362007-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());
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnera81b3362007-07-22 20:11:46 +0000140 isPortable = I->getValue() == DefineValue;
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnera81b3362007-07-22 20:11:46 +0000161 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
162 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
Chris Lattner2194ddc2006-10-14 18:32:26 +0000163 while (!Defs.empty()) {
Chris Lattnera81b3362007-07-22 20:11:46 +0000164 const char *DefStart = Defs.begin()->getKeyData();
165 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
Chris Lattner2194ddc2006-10-14 18:32:26 +0000166
167 // Insert "#define_other_target defname".
168 const char *Command = "#define_other_target ";
169 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Chris Lattnera81b3362007-07-22 20:11:46 +0000170 Buffer.insert(Buffer.end(), DefStart, DefEnd);
Chris Lattner2194ddc2006-10-14 18:32:26 +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 Lattnera81b3362007-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 }
Chris Lattner2194ddc2006-10-14 18:32:26 +0000181 Defs.erase(Defs.begin());
182 }
183 }
Chris Lattnera81b3362007-07-22 20:11:46 +0000184
185 delete[] SecondaryDefines;
Chris Lattner2194ddc2006-10-14 18:32:26 +0000186}
Chris Lattner1e27fe12006-10-14 07:06:20 +0000187
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 Lattner4481b422007-07-14 01:29:45 +0000191void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
192 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
Chris Lattner1e27fe12006-10-14 07:06:20 +0000193
194 // Check whether this is portable across the secondary targets if the T-U is
195 // portable so far.
Chris Lattner4481b422007-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)
Chris Lattner1e27fe12006-10-14 07:06:20 +0000200 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
Chris Lattner4481b422007-07-14 01:29:45 +0000201 }
Chris Lattner1e27fe12006-10-14 07:06:20 +0000202}
203
Chris Lattner10a5b382007-01-29 05:24:35 +0000204
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 Lattnera81b3362007-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 }
Chris Lattner10a5b382007-01-29 05:24:35 +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 Lattnera81b3362007-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();
Chris Lattner10a5b382007-01-29 05:24:35 +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 Carlssona7408e72007-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}
Chris Lattner10a5b382007-01-29 05:24:35 +0000269
Anders Carlsson5fa3f342007-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 Carlsson290aa852007-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 Carlsson5fa3f342007-11-24 23:38:12 +0000315 return false;
316}