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