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