1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
//===--- TargetInfo.cpp - Information about Target machine ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the TargetInfo and TargetInfoImpl interfaces.
//
//===----------------------------------------------------------------------===//
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/Builtins.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/STLExtras.h"
#include <set>
using namespace clang;
void TargetInfoImpl::ANCHOR() {} // out-of-line virtual method for class.
//===----------------------------------------------------------------------===//
// FIXME: These are temporary hacks, they should revector into the
// TargetInfoImpl.
void TargetInfo::getFloatInfo(uint64_t &Size, unsigned &Align,
const llvm::fltSemantics *&Format,
SourceLocation Loc) {
Align = 32; // FIXME: implement correctly.
Size = 32;
Format = &llvm::APFloat::IEEEsingle;
}
void TargetInfo::getDoubleInfo(uint64_t &Size, unsigned &Align,
const llvm::fltSemantics *&Format,
SourceLocation Loc) {
Size = Align = 64; // FIXME: implement correctly.
Format = &llvm::APFloat::IEEEdouble;
}
void TargetInfo::getLongDoubleInfo(uint64_t &Size, unsigned &Align,
const llvm::fltSemantics *&Format,
SourceLocation Loc) {
Size = Align = 64; // FIXME: implement correctly.
Format = &llvm::APFloat::IEEEdouble;
//Size = 80; Align = 32; // FIXME: implement correctly.
//Format = &llvm::APFloat::x87DoubleExtended;
}
//===----------------------------------------------------------------------===//
const char* TargetInfo::getTargetTriple() const {
return PrimaryTarget->getTargetTriple();
}
const char *TargetInfo::getTargetPrefix() const {
return PrimaryTarget->getTargetPrefix();
}
/// DiagnoseNonPortability - When a use of a non-portable target feature is
/// used, this method emits the diagnostic and marks the translation unit as
/// non-portable.
void TargetInfo::DiagnoseNonPortability(SourceLocation Loc, unsigned DiagKind) {
NonPortable = true;
if (Diag && Loc.isValid()) Diag->Report(Loc, DiagKind, SrcMgr);
}
/// GetTargetDefineMap - Get the set of target #defines in an associative
/// collection for easy lookup.
static void GetTargetDefineMap(const TargetInfoImpl *Target,
llvm::StringMap<std::string> &Map) {
std::vector<char> Defines;
Defines.reserve(4096);
Target->getTargetDefines(Defines);
for (const char *DefStr = &Defines[0], *E = DefStr+Defines.size();
DefStr != E;) {
// Skip the '#define ' portion.
assert(memcmp(DefStr, "#define ", strlen("#define ")) == 0 &&
"#define didn't start with #define!");
DefStr += strlen("#define ");
// Find the divider between the key and value.
const char *SpacePos = strchr(DefStr, ' ');
std::string &Entry = Map.GetOrCreateValue(DefStr, SpacePos).getValue();
const char *EndPos = strchr(SpacePos+1, '\n');
Entry = std::string(SpacePos+1, EndPos);
DefStr = EndPos+1;
}
}
/// getTargetDefines - Appends the target-specific #define values for this
/// target set to the specified buffer.
void TargetInfo::getTargetDefines(std::vector<char> &Buffer) {
// If we have no secondary targets, be a bit more efficient.
if (SecondaryTargets.empty()) {
PrimaryTarget->getTargetDefines(Buffer);
return;
}
// This is tricky in the face of secondary targets. Specifically,
// target-specific #defines that are present and identical across all
// secondary targets are turned into #defines, #defines that are present in
// the primary target but are missing or different in the secondary targets
// are turned into #define_target, and #defines that are not defined in the
// primary, but are defined in a secondary are turned into
// #define_other_target. This allows the preprocessor to correctly track uses
// of target-specific macros.
// Get the set of primary #defines.
llvm::StringMap<std::string> PrimaryDefines;
GetTargetDefineMap(PrimaryTarget, PrimaryDefines);
// Get the sets of secondary #defines.
llvm::StringMap<std::string> *SecondaryDefines
= new llvm::StringMap<std::string>[SecondaryTargets.size()];
for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i)
GetTargetDefineMap(SecondaryTargets[i], SecondaryDefines[i]);
// Loop over all defines in the primary target, processing them until we run
// out.
for (llvm::StringMap<std::string>::iterator PDI =
PrimaryDefines.begin(), E = PrimaryDefines.end(); PDI != E; ++PDI) {
std::string DefineName(PDI->getKeyData(),
PDI->getKeyData() + PDI->getKeyLength());
std::string DefineValue = PDI->getValue();
// Check to see whether all secondary targets have this #define and whether
// it is to the same value. Remember if not, but remove the #define from
// their collection in any case if they have it.
bool isPortable = true;
for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
llvm::StringMap<std::string>::iterator I =
SecondaryDefines[i].find(&DefineName[0],
&DefineName[0]+DefineName.size());
if (I == SecondaryDefines[i].end()) {
// Secondary target doesn't have this #define.
isPortable = false;
} else {
// Secondary target has this define, remember if it disagrees.
if (isPortable)
isPortable = I->getValue() == DefineValue;
// Remove it from the secondary target unconditionally.
SecondaryDefines[i].erase(I);
}
}
// If this define is non-portable, turn it into #define_target, otherwise
// just use #define.
const char *Command = isPortable ? "#define " : "#define_target ";
Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
// Insert "defname defvalue\n".
Buffer.insert(Buffer.end(), DefineName.begin(), DefineName.end());
Buffer.push_back(' ');
Buffer.insert(Buffer.end(), DefineValue.begin(), DefineValue.end());
Buffer.push_back('\n');
}
// Now that all of the primary target's defines have been handled and removed
// from the secondary target's define sets, go through the remaining secondary
// target's #defines and taint them.
for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
llvm::StringMap<std::string> &Defs = SecondaryDefines[i];
while (!Defs.empty()) {
const char *DefStart = Defs.begin()->getKeyData();
const char *DefEnd = DefStart + Defs.begin()->getKeyLength();
// Insert "#define_other_target defname".
const char *Command = "#define_other_target ";
Buffer.insert(Buffer.end(), Command, Command+strlen(Command));
Buffer.insert(Buffer.end(), DefStart, DefEnd);
Buffer.push_back('\n');
// If any other secondary targets have this same define, remove it from
// them to avoid duplicate #define_other_target directives.
for (unsigned j = i+1; j != e; ++j) {
llvm::StringMap<std::string>::iterator I =
SecondaryDefines[j].find(DefStart, DefEnd);
if (I != SecondaryDefines[j].end())
SecondaryDefines[j].erase(I);
}
Defs.erase(Defs.begin());
}
}
delete[] SecondaryDefines;
}
/// ComputeWCharWidth - Determine the width of the wchar_t type for the primary
/// target, diagnosing whether this is non-portable across the secondary
/// targets.
void TargetInfo::ComputeWCharInfo(SourceLocation Loc) {
PrimaryTarget->getWCharInfo(WCharWidth, WCharAlign);
// Check whether this is portable across the secondary targets if the T-U is
// portable so far.
for (unsigned i = 0, e = SecondaryTargets.size(); i != e; ++i) {
unsigned Width, Align;
SecondaryTargets[i]->getWCharInfo(Width, Align);
if (Width != WCharWidth || Align != WCharAlign)
return DiagnoseNonPortability(Loc, diag::port_wchar_t);
}
}
/// getTargetBuiltins - Return information about target-specific builtins for
/// the current primary target, and info about which builtins are non-portable
/// across the current set of primary a
|