blob: 3dd9488e315efd49ec5a6891f30ed67c98b6cb31 [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
71 // Get the sets of secondary #defines.
72 std::vector<std::map<std::string, std::string> > SecondaryDefines;
73 SecondaryDefines.resize(SecondaryTargets.size());
74 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
75 GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
76
77 // Loop over all defines in the primary target, processing them until we run
78 // out.
79 while (!PrimaryDefines.empty()) {
80 std::string DefineName = PrimaryDefines.begin()->first;
81 std::string DefineValue = PrimaryDefines.begin()->second;
82 PrimaryDefines.erase(PrimaryDefines.begin());
83
84 // Check to see whether all secondary targets have this #define and whether
85 // it is to the same value. Remember if not, but remove the #define from
86 // their collection in any case if they have it.
87 bool isPortable = true;
88
89 for (unsigned i = 0, e = SecondaryDefines.size(); i != e; ++i) {
90 std::map<std::string, std::string>::iterator I =
91 SecondaryDefines[i].find(DefineName);
92 if (I == SecondaryDefines[i].end()) {
93 // Secondary target doesn't have this #define.
94 isPortable = false;
95 } else {
96 // Secondary target has this define, remember if it disagrees.
97 if (isPortable)
98 isPortable = I->second == DefineValue;
99 // Remove it from the secondary target unconditionally.
100 SecondaryDefines[i].erase(I);
101 }
102 }
103
104 // If this define is non-portable, turn it into #define_target, otherwise
105 // just use #define.
106 const char *Command = isPortable ? "#define " : "#define_target ";
107 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
108
109 // Insert "defname defvalue\n".
110 Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
111 Buffer.push_back(' ');
112 Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
113 Buffer.push_back('\n');
114 }
115
116 // Now that all of the primary target's defines have been handled and removed
117 // from the secondary target's define sets, go through the remaining secondary
118 // target's #defines and taint them.
119 for (unsigned i = 0, e = SecondaryDefines.size(); i != e; ++i) {
120 std::map<std::string, std::string> &Defs = SecondaryDefines[i];
121 while (!Defs.empty()) {
122 const std::string &DefName = Defs.begin()->first;
123
124 // Insert "#define_other_target defname".
125 const char *Command = "#define_other_target ";
126 Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
127 Buffer.insert(Buffer.end(), DefName.begin(), DefName.end());
128 Buffer.push_back('\n');
129
130 // If any other secondary targets have this same define, remove it from
131 // them to avoid duplicate #define_other_target directives.
132 for (unsigned j = i+1; j != e; ++j)
133 SecondaryDefines[j].erase(DefName);
134
135 Defs.erase(Defs.begin());
136 }
137 }
138}
Chris Lattner1e27fe12006-10-14 07:06:20 +0000139
140/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
141/// target, diagnosing whether this is non-portable across the secondary
142/// targets.
143void TargetInfo::ComputeWCharWidth(SourceLocation Loc) {
144 WCharWidth = PrimaryTarget->getWCharWidth();
145
146 // Check whether this is portable across the secondary targets if the T-U is
147 // portable so far.
148 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
149 if (SecondaryTargets[i]->getWCharWidth() != WCharWidth)
150 return DiagnoseNonPortability(Loc, diag::port_wchar_t);
151}
152
Chris Lattner10a5b382007-01-29 05:24:35 +0000153
154/// getTargetBuiltins - Return information about target-specific builtins for
155/// the current primary target, and info about which builtins are non-portable
156/// across the current set of primary and secondary targets.
157void TargetInfo::getTargetBuiltins(const Builtin::Info *&Records,
158 unsigned &NumRecords,
159 std::vector<const char *> &NPortable) const {
160 // Get info about what actual builtins we will expose.
161 PrimaryTarget->getTargetBuiltins(Records, NumRecords);
162 if (SecondaryTargets.empty()) return;
163
164 // Compute the set of non-portable builtins.
165
166 // Start by computing a mapping from the primary target's builtins to their
167 // info records for efficient lookup.
168 std::map<std::string, const Builtin::Info*> PrimaryRecs;
169 for (unsigned i = 0, e = NumRecords; i != e; ++i)
170 PrimaryRecs[Records[i].Name] = Records+i;
171
172 for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
173 // Get the builtins for this secondary target.
174 const Builtin::Info *Records2nd;
175 unsigned NumRecords2nd;
176 SecondaryTargets[i]->getTargetBuiltins(Records2nd, NumRecords2nd);
177
178 // Remember all of the secondary builtin names.
179 std::set<std::string> BuiltinNames2nd;
180
181 for (unsigned j = 0, e = NumRecords2nd; j != e; ++j) {
182 BuiltinNames2nd.insert(Records2nd[j].Name);
183
184 // Check to see if the primary target has this builtin.
185 if (const Builtin::Info *PrimBI = PrimaryRecs[Records2nd[j].Name]) {
186 // If does. If they are not identical, mark the builtin as being
187 // non-portable.
188 if (Records2nd[j] != *PrimBI)
189 NPortable.push_back(PrimBI->Name);
190 } else {
191 // The primary target doesn't have this, it is non-portable.
192 NPortable.push_back(Records2nd[j].Name);
193 }
194 }
195
196 // Now that we checked all the secondary builtins, check to see if the
197 // primary target has any builtins that the secondary one doesn't. If so,
198 // then those are non-portable.
199 for (unsigned j = 0, e = NumRecords; j != e; ++j) {
200 if (!BuiltinNames2nd.count(Records[j].Name))
201 NPortable.push_back(Records[j].Name);
202 }
203 }
204}
205
206