blob: 2c476b8627cf825cdee2d1437a0b6f2fe16307d2 [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 Lattnera81b3362007-07-22 20:11:46 +000017#include "llvm/ADT/StringMap.h"
Chris Lattnerec0a6d92007-09-22 18:29:59 +000018#include "llvm/ADT/APFloat.h"
Chris Lattner10a5b382007-01-29 05:24:35 +000019#include <set>
Chris Lattner1e27fe12006-10-14 07:06:20 +000020using namespace clang;
21
Chris Lattner2194ddc2006-10-14 18:32:26 +000022void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
23
24
Chris Lattnerec0a6d92007-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 Lattnerc2d09cf2007-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 Lattnerec0a6d92007-09-22 18:29:59 +000049}
50
51
52//===----------------------------------------------------------------------===//
53
Chris Lattner1e27fe12006-10-14 07:06:20 +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;
Chris Lattnerf033c142007-06-22 19:05:19 +000059 if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind);
Chris Lattner1e27fe12006-10-14 07:06:20 +000060}
61
Chris Lattner2194ddc2006-10-14 18:32:26 +000062/// GetTargetDefineMap - Get the set of target #defines in an associative
63/// collection for easy lookup.
64static void GetTargetDefineMap(const TargetInfoImpl *Target,
Chris Lattnera81b3362007-07-22 20:11:46 +000065 llvm::StringMap<std::string> &Map) {
Chris Lattner2194ddc2006-10-14 18:32:26 +000066 std::vector<std::string> PrimaryDefines;
67 Target->getTargetDefines(PrimaryDefines);
68
69 while (!PrimaryDefines.empty()) {
Chris Lattnera81b3362007-07-22 20:11:46 +000070 std::string &PrimDefineStr = PrimaryDefines.back();
71 const char *Str = PrimDefineStr.c_str();
72 const char *StrEnd = Str+PrimDefineStr.size();
73
Chris Lattner2194ddc2006-10-14 18:32:26 +000074 if (const char *Equal = strchr(Str, '=')) {
75 // Split at the '='.
Chris Lattnera81b3362007-07-22 20:11:46 +000076
77 std::string &Entry = Map.GetOrCreateValue(Str, Equal).getValue();
78 Entry = std::string(Equal+1, StrEnd);
Chris Lattner2194ddc2006-10-14 18:32:26 +000079 } else {
80 // Remember "macroname=1".
Chris Lattnera81b3362007-07-22 20:11:46 +000081 std::string &Entry = Map.GetOrCreateValue(Str, StrEnd).getValue();
82 Entry = "1";
Chris Lattner2194ddc2006-10-14 18:32:26 +000083 }
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.
Chris Lattnera81b3362007-07-22 20:11:46 +0000101 llvm::StringMap<std::string> PrimaryDefines;
Chris Lattner2194ddc2006-10-14 18:32:26 +0000102 GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
103
Chris Lattner720f5762007-01-30 06:01:31 +0000104 // If we have no secondary targets, be a bit more efficient.
105 if (SecondaryTargets.empty()) {
Chris Lattnera81b3362007-07-22 20:11:46 +0000106 for (llvm::StringMap<std::string>::iterator I =
Chris Lattner720f5762007-01-30 06:01:31 +0000107 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".
Chris Lattnera81b3362007-07-22 20:11:46 +0000114 const char *KeyStart = I->getKeyData();
115 const char *KeyEnd = KeyStart + I->getKeyLength();
116
117 Buffer.insert(Buffer.end(), KeyStart, KeyEnd);
Chris Lattner720f5762007-01-30 06:01:31 +0000118 Buffer.push_back(' ');
Chris Lattnera81b3362007-07-22 20:11:46 +0000119 Buffer.insert(Buffer.end(), I->getValue().begin(), I->getValue().end());
Chris Lattner720f5762007-01-30 06:01:31 +0000120 Buffer.push_back('\n');
121 }
122 return;
123 }
124
Chris Lattner2194ddc2006-10-14 18:32:26 +0000125 // Get the sets of secondary #defines.
Chris Lattnera81b3362007-07-22 20:11:46 +0000126 llvm::StringMap<std::string> *SecondaryDefines
127 = new llvm::StringMap<std::string>[SecondaryTargets.size()];
Chris Lattner2194ddc2006-10-14 18:32:26 +0000128 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.
Chris Lattnera81b3362007-07-22 20:11:46 +0000133 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();
Chris Lattner2194ddc2006-10-14 18:32:26 +0000138
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
Chris Lattnera81b3362007-07-22 20:11:46 +0000144 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());
Chris Lattner2194ddc2006-10-14 18:32:26 +0000148 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)
Chris Lattnera81b3362007-07-22 20:11:46 +0000154 isPortable = I->getValue() == DefineValue;
Chris Lattner2194ddc2006-10-14 18:32:26 +0000155 // 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.
Chris Lattnera81b3362007-07-22 20:11:46 +0000175 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
176 llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
Chris Lattner2194ddc2006-10-14 18:32:26 +0000177 while (!Defs.empty()) {
Chris Lattnera81b3362007-07-22 20:11:46 +0000178 const char *DefStart = Defs.begin()->getKeyData();
179 const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
Chris Lattner2194ddc2006-10-14 18:32:26 +0000180
181 // Insert "#define_other_target defname".
182 const char *Command = "#define_other_target ";
183 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Chris Lattnera81b3362007-07-22 20:11:46 +0000184 Buffer.insert(Buffer.end(), DefStart, DefEnd);
Chris Lattner2194ddc2006-10-14 18:32:26 +0000185 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.
Chris Lattnera81b3362007-07-22 20:11:46 +0000189 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 }
Chris Lattner2194ddc2006-10-14 18:32:26 +0000195 Defs.erase(Defs.begin());
196 }
197 }
Chris Lattnera81b3362007-07-22 20:11:46 +0000198
199 delete[] SecondaryDefines;
Chris Lattner2194ddc2006-10-14 18:32:26 +0000200}
Chris Lattner1e27fe12006-10-14 07:06:20 +0000201
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.
Chris Lattner4481b422007-07-14 01:29:45 +0000205void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
206 PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
Chris Lattner1e27fe12006-10-14 07:06:20 +0000207
208 // Check whether this is portable across the secondary targets if the T-U is
209 // portable so far.
Chris Lattner4481b422007-07-14 01:29:45 +0000210 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)
Chris Lattner1e27fe12006-10-14 07:06:20 +0000214 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
Chris Lattner4481b422007-07-14 01:29:45 +0000215 }
Chris Lattner1e27fe12006-10-14 07:06:20 +0000216}
217
Chris Lattner10a5b382007-01-29 05:24:35 +0000218
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.
Chris Lattnera81b3362007-07-22 20:11:46 +0000233 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 }
Chris Lattner10a5b382007-01-29 05:24:35 +0000239
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.
Chris Lattnera81b3362007-07-22 20:11:46 +0000253 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();
Chris Lattner10a5b382007-01-29 05:24:35 +0000258 // 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