blob: afa4285aaa9ba8fcc4c4925151c5a8b2d3ad7579 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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"
17#include "llvm/ADT/StringMap.h"
Chris Lattner858eece2007-09-22 18:29:59 +000018#include "llvm/ADT/APFloat.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include <set>
20using namespace clang;
21
22void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
23
24
Chris Lattner858eece2007-09-22 18:29:59 +000025//===----------------------------------------------------------------------===//
26// FIXME: These are temporary hacks, they should revector into the
27// TargetInfoImpl.
28
29void TargetInfo::getFloatInfo(uint64_t &Size, unsigned &Align,
30 const llvm::fltSemantics *&Format,
31 SourceLocation Loc) {
32 Align = 32; // FIXME: implement correctly.
33 Size = 32;
34 Format = &llvm::APFloat::IEEEsingle;
35}
36void TargetInfo::getDoubleInfo(uint64_t &Size, unsigned &Align,
37 const llvm::fltSemantics *&Format,
38 SourceLocation Loc) {
39 Size = Align = 64; // FIXME: implement correctly.
40 Format = &llvm::APFloat::IEEEdouble;
41}
42void TargetInfo::getLongDoubleInfo(uint64_t &Size, unsigned &Align,
43 const llvm::fltSemantics *&Format,
44 SourceLocation Loc) {
Chris Lattner36f46b82007-09-22 18:38:30 +000045 Size = Align = 64; // FIXME: implement correctly.
46 Format = &llvm::APFloat::IEEEdouble;
47 //Size = 80; Align = 32; // FIXME: implement correctly.
48 //Format = &llvm::APFloat::x87DoubleExtended;
Chris Lattner858eece2007-09-22 18:29:59 +000049}
50
51
52//===----------------------------------------------------------------------===//
53
Chris Lattner4b009652007-07-25 00:24:17 +000054/// DiagnoseNonPortability - When a use of a non-portable target feature is
55/// used, this method emits the diagnostic and marks the translation unit as
56/// non-portable.
57void TargetInfo::DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind) {
58 NonPortable = true;
59 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
60}
61
62/// GetTargetDefineMap - Get the set of target #defines in an associative
63/// collection for easy lookup.
64static void GetTargetDefineMap(const TargetInfoImpl *Target,
65 llvm::StringMap<std::string> &Map) {
66 std::vector<std::string> PrimaryDefines;
67 Target->getTargetDefines(PrimaryDefines);
68
69 while (!PrimaryDefines.empty()) {
70 std::string &PrimDefineStr = PrimaryDefines.back();
71 const char *Str = PrimDefineStr.c_str();
72 const char *StrEnd = Str+PrimDefineStr.size();
73
74 if (const char *Equal = strchr(Str, '=')) {
75 // Split at the '='.
76
77 std::string &Entry = Map.GetOrCreateValue(Str, Equal).getValue();
78 Entry = std::string(Equal+1, StrEnd);
79 } else {
80 // Remember "macroname=1".
81 std::string &Entry = Map.GetOrCreateValue(Str, StrEnd).getValue();
82 Entry = "1";
83 }
84 PrimaryDefines.pop_back();
85 }
86}
87
88/// getTargetDefines - Appends the target-specific #define values for this
89/// target set to the specified buffer.
90void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
Chris Lattner8c9d29f2007-10-06 06:29:41 +000091 // If we have no secondary targets, be a bit more efficient.
92 if (SecondaryTargets.empty()) {
93 std::vector<std::string> PrimaryDefines;
94 PrimaryTarget->getTargetDefines(PrimaryDefines);
95
96 for (unsigned i = 0, e = PrimaryDefines.size(); i != e; ++i) {
97 // Always produce a #define.
98 const char *Command = "#define ";
99 Buffer.insert(Buffer.end(), Command, Command+strlen("#define "));
100
101 const std::string &Val = PrimaryDefines[i];
102 unsigned EqualPos = Val.find('=');
103 if (EqualPos != std::string::npos) {
104 // Insert "defname defvalue\n".
105 Buffer.insert(Buffer.end(), Val.begin(), Val.begin()+EqualPos);
106 Buffer.push_back(' ');
107 Buffer.insert(Buffer.end(), Val.begin()+EqualPos+1, Val.end());
108 Buffer.push_back('\n');
109 } else {
110 // Insert "defname 1\n".
111 Buffer.insert(Buffer.end(), Val.begin(), Val.end());
112 Buffer.push_back(' ');
113 Buffer.push_back('1');
114 Buffer.push_back('\n');
115 }
116 }
117 return;
118 }
119
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // This is tricky in the face of secondary targets. Specifically,
121 // target-specific #defines that are present and identical across all
122 // secondary targets are turned into #defines, #defines that are present in
123 // the primary target but are missing or different in the secondary targets
124 // are turned into #define_target, and #defines that are not defined in the
125 // primary, but are defined in a secondary are turned into
126 // #define_other_target. This allows the preprocessor to correctly track uses
127 // of target-specific macros.
128
129 // Get the set of primary #defines.
130 llvm::StringMap<std::string> PrimaryDefines;
131 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
132
Chris Lattner4b009652007-07-25 00:24:17 +0000133 // Get the sets of secondary #defines.
134 llvm::StringMap<std::string> *SecondaryDefines
135 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
136 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
137 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
138
139 // Loop over all defines in the primary target, processing them until we run
140 // out.
141 for (llvm::StringMap<std::string>::iterator PDI =
142 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
143 std::string DefineName(PDI->getKeyData(),
144 PDI->getKeyData() + PDI->getKeyLength());
145 std::string DefineValue = PDI->getValue();
146
147 // Check to see whether all secondary targets have this #define and whether
148 // it is to the same value. Remember if not, but remove the #define from
149 // their collection in any case if they have it.
150 bool isPortable = true;
151
152 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
153 llvm::StringMap<std::string>::iterator I =
154 SecondaryDefines[i].find(&DefineName[0],
155 &DefineName[0]+DefineName.size());
156 if (I == SecondaryDefines[i].end()) {
157 // Secondary target doesn't have this #define.
158 isPortable = false;
159 } else {
160 // Secondary target has this define, remember if it disagrees.
161 if (isPortable)
162 isPortable = I->getValue() == DefineValue;
163 // Remove it from the secondary target unconditionally.
164 SecondaryDefines[i].erase(I);
165 }
166 }
167
168 // If this define is non-portable, turn it into #define_target, otherwise
169 // just use #define.
170 const char *Command = isPortable ? "#define " : "#define_target ";
171 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
172
173 // Insert "defname defvalue\n".
174 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
175 Buffer.push_back(' ');
176 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
177 Buffer.push_back('\n');
178 }
179
180 // Now that all of the primary target's defines have been handled and removed
181 // from the secondary target's define sets, go through the remaining secondary
182 // target's #defines and taint them.
183 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
184 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
185 while (!Defs.empty()) {
186 const char *DefStart = Defs.begin()->getKeyData();
187 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
188
189 // Insert "#define_other_target defname".
190 const char *Command = "#define_other_target ";
191 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
192 Buffer.insert(Buffer.end(), DefStart, DefEnd);
193 Buffer.push_back('\n');
194
195 // If any other secondary targets have this same define, remove it from
196 // them to avoid duplicate #define_other_target directives.
197 for (unsigned j = i+1; j != e; ++j) {
198 llvm::StringMap<std::string>::iterator I =
199 SecondaryDefines[j].find(DefStart, DefEnd);
200 if (I != SecondaryDefines[j].end())
201 SecondaryDefines[j].erase(I);
202 }
203 Defs.erase(Defs.begin());
204 }
205 }
206
207 delete[] SecondaryDefines;
208}
209
210/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
211/// target, diagnosing whether this is non-portable across the secondary
212/// targets.
213void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
214 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
215
216 // Check whether this is portable across the secondary targets if the T-U is
217 // portable so far.
218 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
219 unsigned Width, Align;
220 SecondaryTargets[i]->getWCharInfo(Width, Align);
221 if (Width != WCharWidth || Align != WCharAlign)
222 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
223 }
224}
225
226
227/// getTargetBuiltins - Return information about target-specific builtins for
228/// the current primary target, and info about which builtins are non-portable
229/// across the current set of primary and secondary targets.
230void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
231 unsigned &NumRecords,
232 std::vector<const char *> &NPortable) const {
233 // Get info about what actual builtins we will expose.
234 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
235 if (SecondaryTargets.empty()) return;
236
237 // Compute the set of non-portable builtins.
238
239 // Start by computing a mapping from the primary target's builtins to their
240 // info records for efficient lookup.
241 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
242 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
243 const char *BIName = Records[i].Name;
244 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
245 = Records+i;
246 }
247
248 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
249 // Get the builtins for this secondary target.
250 const Builtin::Info *Records2nd;
251 unsigned NumRecords2nd;
252 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
253
254 // Remember all of the secondary builtin names.
255 std::set<std::string> BuiltinNames2nd;
256
257 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
258 BuiltinNames2nd.insert(Records2nd[j].Name);
259
260 // Check to see if the primary target has this builtin.
261 llvm::StringMap<const Builtin::Info*>::iterator I =
262 PrimaryRecs.find(Records2nd[j].Name,
263 Records2nd[j].Name+strlen(Records2nd[j].Name));
264 if (I != PrimaryRecs.end()) {
265 const Builtin::Info *PrimBI = I->getValue();
266 // If does. If they are not identical, mark the builtin as being
267 // non-portable.
268 if (Records2nd[j] != *PrimBI)
269 NPortable.push_back(PrimBI->Name);
270 } else {
271 // The primary target doesn't have this, it is non-portable.
272 NPortable.push_back(Records2nd[j].Name);
273 }
274 }
275
276 // Now that we checked all the secondary builtins, check to see if the
277 // primary target has any builtins that the secondary one doesn't. If so,
278 // then those are non-portable.
279 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
280 if (!BuiltinNames2nd.count(Records[j].Name))
281 NPortable.push_back(Records[j].Name);
282 }
283 }
284}
285
286