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