blob: 2c476b8627cf825cdee2d1437a0b6f2fe16307d2 [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) {
91 // This is tricky in the face of secondary targets. Specifically,
92 // target-specific #defines that are present and identical across all
93 // secondary targets are turned into #defines, #defines that are present in
94 // the primary target but are missing or different in the secondary targets
95 // are turned into #define_target, and #defines that are not defined in the
96 // primary, but are defined in a secondary are turned into
97 // #define_other_target. This allows the preprocessor to correctly track uses
98 // of target-specific macros.
99
100 // Get the set of primary #defines.
101 llvm::StringMap<std::string> PrimaryDefines;
102 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
103
104 // If we have no secondary targets, be a bit more efficient.
105 if (SecondaryTargets.empty()) {
106 for (llvm::StringMap<std::string>::iterator I =
107 PrimaryDefines.begin(), E = PrimaryDefines.end(); I != E; ++I) {
108 // If this define is non-portable, turn it into #define_target, otherwise
109 // just use #define.
110 const char *Command = "#define ";
111 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
112
113 // Insert "defname defvalue\n".
114 const char *KeyStart = I->getKeyData();
115 const char *KeyEnd = KeyStart + I->getKeyLength();
116
117 Buffer.insert(Buffer.end(), KeyStart, KeyEnd);
118 Buffer.push_back(' ');
119 Buffer.insert(Buffer.end(), I->getValue().begin(), I->getValue().end());
120 Buffer.push_back('\n');
121 }
122 return;
123 }
124
125 // Get the sets of secondary #defines.
126 llvm::StringMap<std::string> *SecondaryDefines
127 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
128 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
129 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
130
131 // Loop over all defines in the primary target, processing them until we run
132 // out.
133 for (llvm::StringMap<std::string>::iterator PDI =
134 PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
135 std::string DefineName(PDI->getKeyData(),
136 PDI->getKeyData() + PDI->getKeyLength());
137 std::string DefineValue = PDI->getValue();
138
139 // Check to see whether all secondary targets have this #define and whether
140 // it is to the same value. Remember if not, but remove the #define from
141 // their collection in any case if they have it.
142 bool isPortable = true;
143
144 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
145 llvm::StringMap<std::string>::iterator I =
146 SecondaryDefines[i].find(&DefineName[0],
147 &DefineName[0]+DefineName.size());
148 if (I == SecondaryDefines[i].end()) {
149 // Secondary target doesn't have this #define.
150 isPortable = false;
151 } else {
152 // Secondary target has this define, remember if it disagrees.
153 if (isPortable)
154 isPortable = I->getValue() == DefineValue;
155 // Remove it from the secondary target unconditionally.
156 SecondaryDefines[i].erase(I);
157 }
158 }
159
160 // If this define is non-portable, turn it into #define_target, otherwise
161 // just use #define.
162 const char *Command = isPortable ? "#define " : "#define_target ";
163 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
164
165 // Insert "defname defvalue\n".
166 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
167 Buffer.push_back(' ');
168 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
169 Buffer.push_back('\n');
170 }
171
172 // Now that all of the primary target's defines have been handled and removed
173 // from the secondary target's define sets, go through the remaining secondary
174 // target's #defines and taint them.
175 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
176 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
177 while (!Defs.empty()) {
178 const char *DefStart = Defs.begin()->getKeyData();
179 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
180
181 // Insert "#define_other_target defname".
182 const char *Command = "#define_other_target ";
183 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
184 Buffer.insert(Buffer.end(), DefStart, DefEnd);
185 Buffer.push_back('\n');
186
187 // If any other secondary targets have this same define, remove it from
188 // them to avoid duplicate #define_other_target directives.
189 for (unsigned j = i+1; j != e; ++j) {
190 llvm::StringMap<std::string>::iterator I =
191 SecondaryDefines[j].find(DefStart, DefEnd);
192 if (I != SecondaryDefines[j].end())
193 SecondaryDefines[j].erase(I);
194 }
195 Defs.erase(Defs.begin());
196 }
197 }
198
199 delete[] SecondaryDefines;
200}
201
202/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
203/// target, diagnosing whether this is non-portable across the secondary
204/// targets.
205void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
206 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
207
208 // Check whether this is portable across the secondary targets if the T-U is
209 // portable so far.
210 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
211 unsigned Width, Align;
212 SecondaryTargets[i]->getWCharInfo(Width, Align);
213 if (Width != WCharWidth || Align != WCharAlign)
214 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
215 }
216}
217
218
219/// getTargetBuiltins - Return information about target-specific builtins for
220/// the current primary target, and info about which builtins are non-portable
221/// across the current set of primary and secondary targets.
222void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
223 unsigned &NumRecords,
224 std::vector<const char *> &NPortable) const {
225 // Get info about what actual builtins we will expose.
226 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
227 if (SecondaryTargets.empty()) return;
228
229 // Compute the set of non-portable builtins.
230
231 // Start by computing a mapping from the primary target's builtins to their
232 // info records for efficient lookup.
233 llvm::StringMap<const Builtin::Info*> PrimaryRecs;
234 for (unsigned i = 0, e = NumRecords; i != e; ++i) {
235 const char *BIName = Records[i].Name;
236 PrimaryRecs.GetOrCreateValue(BIName, BIName+strlen(BIName)).getValue()
237 = Records+i;
238 }
239
240 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
241 // Get the builtins for this secondary target.
242 const Builtin::Info *Records2nd;
243 unsigned NumRecords2nd;
244 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
245
246 // Remember all of the secondary builtin names.
247 std::set<std::string> BuiltinNames2nd;
248
249 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
250 BuiltinNames2nd.insert(Records2nd[j].Name);
251
252 // Check to see if the primary target has this builtin.
253 llvm::StringMap<const Builtin::Info*>::iterator I =
254 PrimaryRecs.find(Records2nd[j].Name,
255 Records2nd[j].Name+strlen(Records2nd[j].Name));
256 if (I != PrimaryRecs.end()) {
257 const Builtin::Info *PrimBI = I->getValue();
258 // If does. If they are not identical, mark the builtin as being
259 // non-portable.
260 if (Records2nd[j] != *PrimBI)
261 NPortable.push_back(PrimBI->Name);
262 } else {
263 // The primary target doesn't have this, it is non-portable.
264 NPortable.push_back(Records2nd[j].Name);
265 }
266 }
267
268 // Now that we checked all the secondary builtins, check to see if the
269 // primary target has any builtins that the secondary one doesn't. If so,
270 // then those are non-portable.
271 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
272 if (!BuiltinNames2nd.count(Records[j].Name))
273 NPortable.push_back(Records[j].Name);
274 }
275 }
276}
277
278