Targets.cpp revision 762fecfd5a2597d2d03222606eafc73ae1c269f1
1//===--- Targets.cpp - Implement -arch option and targets -----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements construction of a TargetInfo object from a
11// target triple.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/Builtins.h"
16#include "clang/AST/TargetBuiltins.h"
17#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/APFloat.h"
20
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24//  Common code shared among targets.
25//===----------------------------------------------------------------------===//
26
27static void Define(std::vector<char> &Buf, const char *Macro,
28                   const char *Val = "1") {
29  const char *Def = "#define ";
30  Buf.insert(Buf.end(), Def, Def+strlen(Def));
31  Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
32  Buf.push_back(' ');
33  Buf.insert(Buf.end(), Val, Val+strlen(Val));
34  Buf.push_back('\n');
35}
36
37static void getSolarisDefines(std::vector<char> &Defs) {
38  Define(Defs, "__SUN__");
39  Define(Defs, "__SOLARIS__");
40}
41
42static void getDarwinDefines(std::vector<char> &Defs) {
43  Define(Defs, "__APPLE__");
44  Define(Defs, "__MACH__");
45
46  if (0)  // darwin_pascal_strings
47    Define(Defs, "__PASCAL_STRINGS__");
48}
49
50static void getDragonFlyDefines(std::vector<char> &Defs) {
51  // DragonFly defines; list based off of gcc output
52  Define(Defs, "__DragonFly__");
53  Define(Defs, "__DragonFly_cc_version", "100001");
54  Define(Defs, "__ELF__");
55  Define(Defs, "__KPRINTF_ATTRIBUTE__");
56  Define(Defs, "__tune_i386__");
57  Define(Defs, "unix");
58  Define(Defs, "__unix");
59  Define(Defs, "__unix__");
60}
61
62static void getLinuxDefines(std::vector<char> &Defs) {
63  // Linux defines; list based off of gcc output
64  Define(Defs, "__unix__");
65  Define(Defs, "__unix");
66  Define(Defs, "unix");
67  Define(Defs, "__linux__");
68  Define(Defs, "__linux");
69  Define(Defs, "linux");
70  Define(Defs, "__gnu_linux__");
71}
72
73/// getPowerPCDefines - Return a set of the PowerPC-specific #defines that are
74/// not tied to a specific subtarget.
75static void getPowerPCDefines(std::vector<char> &Defs, bool is64Bit) {
76  // Target identification.
77  Define(Defs, "__ppc__");
78  Define(Defs, "_ARCH_PPC");
79  Define(Defs, "__POWERPC__");
80  if (is64Bit) {
81    Define(Defs, "_ARCH_PPC64");
82    Define(Defs, "_LP64");
83    Define(Defs, "__LP64__");
84    Define(Defs, "__ppc64__");
85  } else {
86    Define(Defs, "__ppc__");
87  }
88
89  // Target properties.
90  Define(Defs, "_BIG_ENDIAN");
91  Define(Defs, "__BIG_ENDIAN__");
92
93  if (is64Bit) {
94    Define(Defs, "__INTMAX_MAX__", "9223372036854775807L");
95    Define(Defs, "__INTMAX_TYPE__", "long int");
96    Define(Defs, "__LONG_MAX__", "9223372036854775807L");
97    Define(Defs, "__PTRDIFF_TYPE__", "long int");
98    Define(Defs, "__UINTMAX_TYPE__", "long unsigned int");
99  } else {
100    Define(Defs, "__INTMAX_MAX__", "9223372036854775807LL");
101    Define(Defs, "__INTMAX_TYPE__", "long long int");
102    Define(Defs, "__LONG_MAX__", "2147483647L");
103    Define(Defs, "__PTRDIFF_TYPE__", "int");
104    Define(Defs, "__UINTMAX_TYPE__", "long long unsigned int");
105  }
106  Define(Defs, "__INT_MAX__", "2147483647");
107  Define(Defs, "__LONG_LONG_MAX__", "9223372036854775807LL");
108  Define(Defs, "__CHAR_BIT__", "8");
109  Define(Defs, "__SCHAR_MAX__", "127");
110  Define(Defs, "__SHRT_MAX__", "32767");
111  Define(Defs, "__SIZE_TYPE__", "long unsigned int");
112
113  // Subtarget options.
114  Define(Defs, "__USER_LABEL_PREFIX__", "_");
115  Define(Defs, "__NATURAL_ALIGNMENT__");
116  Define(Defs, "__REGISTER_PREFIX__", "");
117
118  Define(Defs, "__WCHAR_MAX__", "2147483647");
119  Define(Defs, "__WCHAR_TYPE__", "int");
120  Define(Defs, "__WINT_TYPE__", "int");
121
122  // Float macros.
123  Define(Defs, "__FLT_DENORM_MIN__", "1.40129846e-45F");
124  Define(Defs, "__FLT_DIG__", "6");
125  Define(Defs, "__FLT_EPSILON__", "1.19209290e-7F");
126  Define(Defs, "__FLT_EVAL_METHOD__", "0");
127  Define(Defs, "__FLT_HAS_INFINITY__");
128  Define(Defs, "__FLT_HAS_QUIET_NAN__");
129  Define(Defs, "__FLT_MANT_DIG__", "24");
130  Define(Defs, "__FLT_MAX_10_EXP__", "38");
131  Define(Defs, "__FLT_MAX_EXP__", "128");
132  Define(Defs, "__FLT_MAX__", "3.40282347e+38F");
133  Define(Defs, "__FLT_MIN_10_EXP__", "(-37)");
134  Define(Defs, "__FLT_MIN_EXP__", "(-125)");
135  Define(Defs, "__FLT_MIN__", "1.17549435e-38F");
136  Define(Defs, "__FLT_RADIX__", "2");
137
138  // double macros.
139  Define(Defs, "__DBL_DENORM_MIN__", "4.9406564584124654e-324");
140  Define(Defs, "__DBL_DIG__", "15");
141  Define(Defs, "__DBL_EPSILON__", "2.2204460492503131e-16");
142  Define(Defs, "__DBL_HAS_INFINITY__");
143  Define(Defs, "__DBL_HAS_QUIET_NAN__");
144  Define(Defs, "__DBL_MANT_DIG__", "53");
145  Define(Defs, "__DBL_MAX_10_EXP__", "308");
146  Define(Defs, "__DBL_MAX_EXP__", "1024");
147  Define(Defs, "__DBL_MAX__", "1.7976931348623157e+308");
148  Define(Defs, "__DBL_MIN_10_EXP__", "(-307)");
149  Define(Defs, "__DBL_MIN_EXP__", "(-1021)");
150  Define(Defs, "__DBL_MIN__", "2.2250738585072014e-308");
151  Define(Defs, "__DECIMAL_DIG__", "33");
152
153  // 128-bit long double macros.
154  Define(Defs, "__LDBL_DENORM_MIN__",
155         "4.94065645841246544176568792868221e-324L");
156  Define(Defs, "__LDBL_DIG__", "31");
157  Define(Defs, "__LDBL_EPSILON__",
158         "4.94065645841246544176568792868221e-324L");
159  Define(Defs, "__LDBL_HAS_INFINITY__");
160  Define(Defs, "__LDBL_HAS_QUIET_NAN__");
161  Define(Defs, "__LDBL_MANT_DIG__", "106");
162  Define(Defs, "__LDBL_MAX_10_EXP__", "308");
163  Define(Defs, "__LDBL_MAX_EXP__", "1024");
164  Define(Defs, "__LDBL_MAX__",
165         "1.79769313486231580793728971405301e+308L");
166  Define(Defs, "__LDBL_MIN_10_EXP__", "(-291)");
167  Define(Defs, "__LDBL_MIN_EXP__", "(-968)");
168  Define(Defs, "__LDBL_MIN__",
169         "2.00416836000897277799610805135016e-292L");
170  Define(Defs, "__LONG_DOUBLE_128__");
171}
172
173/// getX86Defines - Return a set of the X86-specific #defines that are
174/// not tied to a specific subtarget.
175static void getX86Defines(std::vector<char> &Defs, bool is64Bit) {
176  // Target identification.
177  if (is64Bit) {
178    Define(Defs, "_LP64");
179    Define(Defs, "__LP64__");
180    Define(Defs, "__amd64__");
181    Define(Defs, "__amd64");
182    Define(Defs, "__x86_64");
183    Define(Defs, "__x86_64__");
184  } else {
185    Define(Defs, "__i386__");
186    Define(Defs, "__i386");
187    Define(Defs, "i386");
188  }
189
190  // Target properties.
191  Define(Defs, "__LITTLE_ENDIAN__");
192
193  if (is64Bit) {
194    Define(Defs, "__INTMAX_MAX__", "9223372036854775807L");
195    Define(Defs, "__INTMAX_TYPE__", "long int");
196    Define(Defs, "__LONG_MAX__", "9223372036854775807L");
197    Define(Defs, "__PTRDIFF_TYPE__", "long int");
198    Define(Defs, "__UINTMAX_TYPE__", "long unsigned int");
199  } else {
200    Define(Defs, "__INTMAX_MAX__", "9223372036854775807LL");
201    Define(Defs, "__INTMAX_TYPE__", "long long int");
202    Define(Defs, "__LONG_MAX__", "2147483647L");
203    Define(Defs, "__PTRDIFF_TYPE__", "int");
204    Define(Defs, "__UINTMAX_TYPE__", "long long unsigned int");
205  }
206  Define(Defs, "__SIZE_TYPE__", "long unsigned int");
207  Define(Defs, "__CHAR_BIT__", "8");
208  Define(Defs, "__INT_MAX__", "2147483647");
209  Define(Defs, "__LONG_LONG_MAX__", "9223372036854775807LL");
210  Define(Defs, "__SCHAR_MAX__", "127");
211  Define(Defs, "__SHRT_MAX__", "32767");
212
213  // Subtarget options.
214  Define(Defs, "__nocona");
215  Define(Defs, "__nocona__");
216  Define(Defs, "__tune_nocona__");
217  Define(Defs, "__SSE2_MATH__");
218  Define(Defs, "__SSE2__");
219  Define(Defs, "__SSE_MATH__");
220  Define(Defs, "__SSE__");
221  Define(Defs, "__MMX__");
222  Define(Defs, "__REGISTER_PREFIX__", "");
223
224  Define(Defs, "__WCHAR_MAX__", "2147483647");
225  Define(Defs, "__WCHAR_TYPE__", "int");
226  Define(Defs, "__WINT_TYPE__", "int");
227
228  // Float macros.
229  Define(Defs, "__FLT_DENORM_MIN__", "1.40129846e-45F");
230  Define(Defs, "__FLT_DIG__", "6");
231  Define(Defs, "__FLT_EPSILON__", "1.19209290e-7F");
232  Define(Defs, "__FLT_EVAL_METHOD__", "0");
233  Define(Defs, "__FLT_HAS_INFINITY__");
234  Define(Defs, "__FLT_HAS_QUIET_NAN__");
235  Define(Defs, "__FLT_MANT_DIG__", "24");
236  Define(Defs, "__FLT_MAX_10_EXP__", "38");
237  Define(Defs, "__FLT_MAX_EXP__", "128");
238  Define(Defs, "__FLT_MAX__", "3.40282347e+38F");
239  Define(Defs, "__FLT_MIN_10_EXP__", "(-37)");
240  Define(Defs, "__FLT_MIN_EXP__", "(-125)");
241  Define(Defs, "__FLT_MIN__", "1.17549435e-38F");
242  Define(Defs, "__FLT_RADIX__", "2");
243
244  // Double macros.
245  Define(Defs, "__DBL_DENORM_MIN__", "4.9406564584124654e-324");
246  Define(Defs, "__DBL_DIG__", "15");
247  Define(Defs, "__DBL_EPSILON__", "2.2204460492503131e-16");
248  Define(Defs, "__DBL_HAS_INFINITY__");
249  Define(Defs, "__DBL_HAS_QUIET_NAN__");
250  Define(Defs, "__DBL_MANT_DIG__", "53");
251  Define(Defs, "__DBL_MAX_10_EXP__", "308");
252  Define(Defs, "__DBL_MAX_EXP__", "1024");
253  Define(Defs, "__DBL_MAX__", "1.7976931348623157e+308");
254  Define(Defs, "__DBL_MIN_10_EXP__", "(-307)");
255  Define(Defs, "__DBL_MIN_EXP__", "(-1021)");
256  Define(Defs, "__DBL_MIN__", "2.2250738585072014e-308");
257  Define(Defs, "__DECIMAL_DIG__", "21");
258
259  // 80-bit Long double macros.
260  Define(Defs, "__LDBL_DENORM_MIN__", "3.64519953188247460253e-4951L");
261  Define(Defs, "__LDBL_DIG__", "18");
262  Define(Defs, "__LDBL_EPSILON__", "1.08420217248550443401e-19L");
263  Define(Defs, "__LDBL_HAS_INFINITY__");
264  Define(Defs, "__LDBL_HAS_QUIET_NAN__");
265  Define(Defs, "__LDBL_MANT_DIG__", "64");
266  Define(Defs, "__LDBL_MAX_10_EXP__", "4932");
267  Define(Defs, "__LDBL_MAX_EXP__", "16384");
268  Define(Defs, "__LDBL_MAX__", "1.18973149535723176502e+4932L");
269  Define(Defs, "__LDBL_MIN_10_EXP__", "(-4931)");
270  Define(Defs, "__LDBL_MIN_EXP__", "(-16381)");
271  Define(Defs, "__LDBL_MIN__", "3.36210314311209350626e-4932L");
272}
273
274/// getARMDefines - Return a set of the ARM-specific #defines that are
275/// not tied to a specific subtarget.
276static void getARMDefines(std::vector<char> &Defs) {
277  // Target identification.
278  Define(Defs, "__arm");
279  Define(Defs, "__arm__");
280
281  // Target properties.
282  Define(Defs, "__LITTLE_ENDIAN__");
283
284  Define(Defs, "__INTMAX_MAX__", "9223372036854775807LL");
285  Define(Defs, "__INTMAX_TYPE__", "long long int");
286  Define(Defs, "__LONG_MAX__", "2147483647L");
287  Define(Defs, "__PTRDIFF_TYPE__", "int");
288  Define(Defs, "__UINTMAX_TYPE__", "long long unsigned int");
289  Define(Defs, "__SIZE_TYPE__", "long unsigned int");
290
291  Define(Defs, "__CHAR_BIT__", "8");
292  Define(Defs, "__INT_MAX__", "2147483647");
293  Define(Defs, "__LONG_LONG_MAX__", "9223372036854775807LL");
294  Define(Defs, "__SCHAR_MAX__", "127");
295  Define(Defs, "__SHRT_MAX__", "32767");
296
297  // Subtarget options.  [hard coded to v6 for now]
298  Define(Defs, "__ARM_ARCH_6K__");
299  Define(Defs, "__ARMEL__");
300  Define(Defs, "__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__", "20000");
301
302  Define(Defs, "__WCHAR_MAX__", "2147483647");
303  Define(Defs, "__WCHAR_TYPE__", "int");
304  Define(Defs, "__WINT_TYPE__", "int");
305  Define(Defs, "__DECIMAL_DIG__", "17");
306  Define(Defs, "__FLT_RADIX__", "2");
307
308  // Float macros.
309  Define(Defs, "__FLT_DENORM_MIN__", "1.40129846e-45F");
310  Define(Defs, "__FLT_DIG__", "6");
311  Define(Defs, "__FLT_EPSILON__", "1.19209290e-7F");
312  Define(Defs, "__FLT_EVAL_METHOD__", "0");
313  Define(Defs, "__FLT_HAS_INFINITY__");
314  Define(Defs, "__FLT_HAS_QUIET_NAN__");
315  Define(Defs, "__FLT_MANT_DIG__", "24");
316  Define(Defs, "__FLT_MAX_10_EXP__", "38");
317  Define(Defs, "__FLT_MAX_EXP__", "128");
318  Define(Defs, "__FLT_MAX__", "3.40282347e+38F");
319  Define(Defs, "__FLT_MIN_10_EXP__", "(-37)");
320  Define(Defs, "__FLT_MIN_EXP__", "(-125)");
321  Define(Defs, "__FLT_MIN__", "1.17549435e-38F");
322
323  // Double macros.
324  Define(Defs, "__DBL_DENORM_MIN__", "4.9406564584124654e-324");
325  Define(Defs, "__DBL_DIG__", "15");
326  Define(Defs, "__DBL_EPSILON__", "2.2204460492503131e-16");
327  Define(Defs, "__DBL_HAS_INFINITY__");
328  Define(Defs, "__DBL_HAS_QUIET_NAN__");
329  Define(Defs, "__DBL_MANT_DIG__", "53");
330  Define(Defs, "__DBL_MAX_10_EXP__", "308");
331  Define(Defs, "__DBL_MAX_EXP__", "1024");
332  Define(Defs, "__DBL_MAX__", "1.7976931348623157e+308");
333  Define(Defs, "__DBL_MIN_10_EXP__", "(-307)");
334  Define(Defs, "__DBL_MIN_EXP__", "(-1021)");
335  Define(Defs, "__DBL_MIN__", "2.2250738585072014e-308");
336
337  // 64-bit Long double macros (same as double).
338  Define(Defs, "__LDBL_DENORM_MIN__", "4.9406564584124654e-324");
339  Define(Defs, "__LDBL_DIG__", "15");
340  Define(Defs, "__LDBL_EPSILON__", "2.2204460492503131e-16");
341  Define(Defs, "__LDBL_HAS_INFINITY__");
342  Define(Defs, "__LDBL_HAS_QUIET_NAN__");
343  Define(Defs, "__LDBL_MANT_DIG__", "53");
344  Define(Defs, "__LDBL_MAX_10_EXP__", "308");
345  Define(Defs, "__LDBL_MAX_EXP__", "1024");
346  Define(Defs, "__LDBL_MAX__", "1.7976931348623157e+308");
347  Define(Defs, "__LDBL_MIN_10_EXP__", "(-307)");
348  Define(Defs, "__LDBL_MIN_EXP__", "(-1021)");
349  Define(Defs, "__LDBL_MIN__", "2.2250738585072014e-308");
350}
351
352//===----------------------------------------------------------------------===//
353// Specific target implementations.
354//===----------------------------------------------------------------------===//
355
356namespace {
357// PPC abstract base class
358class PPCTargetInfo : public TargetInfo {
359  static const Builtin::Info BuiltinInfo[];
360  static const char * const GCCRegNames[];
361  static const TargetInfo::GCCRegAlias GCCRegAliases[];
362
363public:
364  PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {
365    CharIsSigned = false;
366  }
367  virtual void getTargetBuiltins(const Builtin::Info *&Records,
368                                 unsigned &NumRecords) const {
369    Records = BuiltinInfo;
370    NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
371  }
372  virtual const char *getVAListDeclaration() const {
373    return "typedef struct __va_list_tag {"
374           "  unsigned char gpr;"
375           "  unsigned char fpr;"
376           "  unsigned short reserved;"
377           "  void* overflow_arg_area;"
378           "  void* reg_save_area;"
379           "} __builtin_va_list[1];";
380  }
381  virtual const char *getTargetPrefix() const {
382    return "ppc";
383  }
384  virtual void getGCCRegNames(const char * const *&Names,
385                              unsigned &NumNames) const;
386  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
387                                unsigned &NumAliases) const;
388  virtual bool validateAsmConstraint(char c,
389                                     TargetInfo::ConstraintInfo &info) const {
390    switch (c) {
391    default: return false;
392    case 'O': // Zero
393      return true;
394    case 'b': // Base register
395    case 'f': // Floating point register
396      info = (TargetInfo::ConstraintInfo)(info|TargetInfo::CI_AllowsRegister);
397      return true;
398    }
399  }
400  virtual const char *getClobbers() const {
401    return "";
402  }
403};
404
405const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
406#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS },
407#include "clang/AST/PPCBuiltins.def"
408};
409
410const char * const PPCTargetInfo::GCCRegNames[] = {
411  "0", "1", "2", "3", "4", "5", "6", "7",
412  "8", "9", "10", "11", "12", "13", "14", "15",
413  "16", "17", "18", "19", "20", "21", "22", "23",
414  "24", "25", "26", "27", "28", "29", "30", "31",
415  "0", "1", "2", "3", "4", "5", "6", "7",
416  "8", "9", "10", "11", "12", "13", "14", "15",
417  "16", "17", "18", "19", "20", "21", "22", "23",
418  "24", "25", "26", "27", "28", "29", "30", "31",
419  "mq", "lr", "ctr", "ap",
420  "0", "1", "2", "3", "4", "5", "6", "7",
421  "xer",
422  "0", "1", "2", "3", "4", "5", "6", "7",
423  "8", "9", "10", "11", "12", "13", "14", "15",
424  "16", "17", "18", "19", "20", "21", "22", "23",
425  "24", "25", "26", "27", "28", "29", "30", "31",
426  "vrsave", "vscr",
427  "spe_acc", "spefscr",
428  "sfp"
429};
430
431void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
432                                   unsigned &NumNames) const {
433  Names = GCCRegNames;
434  NumNames = llvm::array_lengthof(GCCRegNames);
435}
436
437const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
438  // While some of these aliases do map to different registers
439  // they still share the same register name.
440  { { "cc", "cr0", "fr0", "r0", "v0"}, "0" },
441  { { "cr1", "fr1", "r1", "sp", "v1"}, "1" },
442  { { "cr2", "fr2", "r2", "toc", "v2"}, "2" },
443  { { "cr3", "fr3", "r3", "v3"}, "3" },
444  { { "cr4", "fr4", "r4", "v4"}, "4" },
445  { { "cr5", "fr5", "r5", "v5"}, "5" },
446  { { "cr6", "fr6", "r6", "v6"}, "6" },
447  { { "cr7", "fr7", "r7", "v7"}, "7" },
448  { { "fr8", "r8", "v8"}, "8" },
449  { { "fr9", "r9", "v9"}, "9" },
450  { { "fr10", "r10", "v10"}, "10" },
451  { { "fr11", "r11", "v11"}, "11" },
452  { { "fr12", "r12", "v12"}, "12" },
453  { { "fr13", "r13", "v13"}, "13" },
454  { { "fr14", "r14", "v14"}, "14" },
455  { { "fr15", "r15", "v15"}, "15" },
456  { { "fr16", "r16", "v16"}, "16" },
457  { { "fr17", "r17", "v17"}, "17" },
458  { { "fr18", "r18", "v18"}, "18" },
459  { { "fr19", "r19", "v19"}, "19" },
460  { { "fr20", "r20", "v20"}, "20" },
461  { { "fr21", "r21", "v21"}, "21" },
462  { { "fr22", "r22", "v22"}, "22" },
463  { { "fr23", "r23", "v23"}, "23" },
464  { { "fr24", "r24", "v24"}, "24" },
465  { { "fr25", "r25", "v25"}, "25" },
466  { { "fr26", "r26", "v26"}, "26" },
467  { { "fr27", "r27", "v27"}, "27" },
468  { { "fr28", "r28", "v28"}, "28" },
469  { { "fr29", "r29", "v29"}, "29" },
470  { { "fr30", "r30", "v30"}, "30" },
471  { { "fr31", "r31", "v31"}, "31" },
472};
473
474void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
475                                     unsigned &NumAliases) const {
476  Aliases = GCCRegAliases;
477  NumAliases = llvm::array_lengthof(GCCRegAliases);
478}
479} // end anonymous namespace.
480
481namespace {
482class PPC32TargetInfo : public PPCTargetInfo {
483public:
484  PPC32TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
485    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
486                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128";
487  }
488  virtual void getTargetDefines(std::vector<char> &Defines) const {
489    getPowerPCDefines(Defines, false);
490  }
491};
492} // end anonymous namespace.
493
494namespace {
495class PPC64TargetInfo : public PPCTargetInfo {
496public:
497  PPC64TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
498    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
499    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
500                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128";
501  }
502  virtual void getTargetDefines(std::vector<char> &Defines) const {
503    getPowerPCDefines(Defines, true);
504  }
505};
506} // end anonymous namespace.
507
508namespace {
509class DarwinPPCTargetInfo : public PPC32TargetInfo {
510public:
511  DarwinPPCTargetInfo(const std::string& triple) : PPC32TargetInfo(triple) {}
512  virtual void getTargetDefines(std::vector<char> &Defines) const {
513    PPC32TargetInfo::getTargetDefines(Defines);
514    getDarwinDefines(Defines);
515  }
516
517  virtual bool useNeXTRuntimeAsDefault() const { return true; }
518};
519} // end anonymous namespace.
520
521namespace {
522class DarwinPPC64TargetInfo : public PPC64TargetInfo {
523public:
524  DarwinPPC64TargetInfo(const std::string& triple) : PPC64TargetInfo(triple) {}
525  virtual void getTargetDefines(std::vector<char> &Defines) const {
526    PPC64TargetInfo::getTargetDefines(Defines);
527    getDarwinDefines(Defines);
528  }
529
530  virtual bool useNeXTRuntimeAsDefault() const { return true; }
531};
532} // end anonymous namespace.
533
534namespace {
535// Namespace for x86 abstract base class
536const Builtin::Info BuiltinInfo[] = {
537#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS },
538#include "clang/AST/X86Builtins.def"
539};
540
541const char *GCCRegNames[] = {
542  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
543  "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
544  "argp", "flags", "fspr", "dirflag", "frame",
545  "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
546  "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
547  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
548  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
549};
550
551const TargetInfo::GCCRegAlias GCCRegAliases[] = {
552  { { "al", "ah", "eax", "rax" }, "ax" },
553  { { "bl", "bh", "ebx", "rbx" }, "bx" },
554  { { "cl", "ch", "ecx", "rcx" }, "cx" },
555  { { "dl", "dh", "edx", "rdx" }, "dx" },
556  { { "esi", "rsi" }, "si" },
557  { { "edi", "rdi" }, "di" },
558  { { "esp", "rsp" }, "sp" },
559  { { "ebp", "rbp" }, "bp" },
560};
561
562// X86 target abstract base class; x86-32 and x86-64 are very close, so
563// most of the implementation can be shared.
564class X86TargetInfo : public TargetInfo {
565public:
566  X86TargetInfo(const std::string& triple) : TargetInfo(triple) {
567    LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
568  }
569  virtual void getTargetBuiltins(const Builtin::Info *&Records,
570                                 unsigned &NumRecords) const {
571    Records = BuiltinInfo;
572    NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
573  }
574  virtual const char *getTargetPrefix() const {
575    return "x86";
576  }
577  virtual void getGCCRegNames(const char * const *&Names,
578                              unsigned &NumNames) const {
579    Names = GCCRegNames;
580    NumNames = llvm::array_lengthof(GCCRegNames);
581  }
582  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
583                                unsigned &NumAliases) const {
584    Aliases = GCCRegAliases;
585    NumAliases = llvm::array_lengthof(GCCRegAliases);
586  }
587  virtual bool validateAsmConstraint(char c,
588                                     TargetInfo::ConstraintInfo &info) const;
589  virtual std::string convertConstraint(const char Constraint) const;
590  virtual const char *getClobbers() const {
591    return "~{dirflag},~{fpsr},~{flags}";
592  }
593};
594
595bool
596X86TargetInfo::validateAsmConstraint(char c,
597                                     TargetInfo::ConstraintInfo &info) const {
598  switch (c) {
599  default: return false;
600  case 'a': // eax.
601  case 'b': // ebx.
602  case 'c': // ecx.
603  case 'd': // edx.
604  case 'S': // esi.
605  case 'D': // edi.
606  case 'A': // edx:eax.
607  case 't': // top of floating point stack.
608  case 'u': // second from top of floating point stack.
609  case 'q': // Any register accessible as [r]l: a, b, c, and d.
610  case 'Q': // Any register accessible as [r]h: a, b, c, and d.
611  case 'Z': // 32-bit integer constant for use with zero-extending x86_64
612            // instructions.
613  case 'N': // unsigned 8-bit integer constant for use with in and out
614            // instructions.
615    info = (TargetInfo::ConstraintInfo)(info|TargetInfo::CI_AllowsRegister);
616    return true;
617  }
618}
619
620std::string
621X86TargetInfo::convertConstraint(const char Constraint) const {
622  switch (Constraint) {
623  case 'a': return std::string("{ax}");
624  case 'b': return std::string("{bx}");
625  case 'c': return std::string("{cx}");
626  case 'd': return std::string("{dx}");
627  case 'S': return std::string("{si}");
628  case 'D': return std::string("{di}");
629  case 't': // top of floating point stack.
630    return std::string("{st}");
631  case 'u': // second from top of floating point stack.
632    return std::string("{st(1)}"); // second from top of floating point stack.
633  default:
634    return std::string(1, Constraint);
635  }
636}
637} // end anonymous namespace
638
639namespace {
640// X86-32 generic target
641class X86_32TargetInfo : public X86TargetInfo {
642public:
643  X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
644    DoubleAlign = LongLongAlign = 32;
645    LongDoubleWidth = 96;
646    LongDoubleAlign = 32;
647    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
648                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
649                        "a0:0:64-f80:32:32";
650  }
651  virtual const char *getVAListDeclaration() const {
652    return "typedef char* __builtin_va_list;";
653  }
654  virtual void getTargetDefines(std::vector<char> &Defines) const {
655    getX86Defines(Defines, false);
656  }
657};
658} // end anonymous namespace
659
660namespace {
661// x86-32 Darwin (OS X) target
662class DarwinI386TargetInfo : public X86_32TargetInfo {
663public:
664  DarwinI386TargetInfo(const std::string& triple) : X86_32TargetInfo(triple) {
665    LongDoubleWidth = 128;
666    LongDoubleAlign = 128;
667    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
668                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
669                        "a0:0:64-f80:128:128";
670  }
671  virtual void getTargetDefines(std::vector<char> &Defines) const {
672    X86_32TargetInfo::getTargetDefines(Defines);
673    getDarwinDefines(Defines);
674  }
675  virtual bool useNeXTRuntimeAsDefault() const { return true; }
676};
677} // end anonymous namespace
678
679namespace {
680// x86-32 DragonFly target
681class DragonFlyX86_32TargetInfo : public X86_32TargetInfo {
682public:
683  DragonFlyX86_32TargetInfo(const std::string& triple) : X86_32TargetInfo(triple) {
684  }
685  virtual void getTargetDefines(std::vector<char> &Defines) const {
686    X86_32TargetInfo::getTargetDefines(Defines);
687    getDragonFlyDefines(Defines);
688  }
689};
690} // end anonymous namespace
691
692namespace {
693// x86-32 Linux target
694class LinuxX86_32TargetInfo : public X86_32TargetInfo {
695public:
696  LinuxX86_32TargetInfo(const std::string& triple) : X86_32TargetInfo(triple) {
697  }
698  virtual void getTargetDefines(std::vector<char> &Defines) const {
699    X86_32TargetInfo::getTargetDefines(Defines);
700    getLinuxDefines(Defines);
701    Define(Defines, "__USER_LABEL_PREFIX__", "");
702  }
703};
704} // end anonymous namespace
705
706namespace {
707// x86-32 Windows target
708class WindowsX86_32TargetInfo : public X86_32TargetInfo {
709public:
710  WindowsX86_32TargetInfo(const std::string& triple)
711    : X86_32TargetInfo(triple) {
712    // FIXME: Fix wchar_t.
713    // FIXME: We should probably enable -fms-extensions by default for
714    // this target.
715  }
716  virtual void getTargetDefines(std::vector<char> &Defines) const {
717    X86_32TargetInfo::getTargetDefines(Defines);
718    // This list is based off of the the list of things MingW defines
719    Define(Defines, "__WIN32__");
720    Define(Defines, "__WIN32");
721    Define(Defines, "_WIN32");
722    Define(Defines, "WIN32");
723    Define(Defines, "__WINNT__");
724    Define(Defines, "__WINNT");
725    Define(Defines, "WINNT");
726    Define(Defines, "_X86_");
727    Define(Defines, "__MSVCRT__");
728    Define(Defines, "__USER_LABEL_PREFIX__", "_");
729  }
730};
731} // end anonymous namespace
732
733namespace {
734// x86-64 generic target
735class X86_64TargetInfo : public X86TargetInfo {
736public:
737  X86_64TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
738    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
739    LongDoubleWidth = 128;
740    LongDoubleAlign = 128;
741    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
742                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
743                        "a0:0:64-f80:128:128";
744  }
745  virtual const char *getVAListDeclaration() const {
746    return "typedef struct __va_list_tag {"
747           "  unsigned gp_offset;"
748           "  unsigned fp_offset;"
749           "  void* overflow_arg_area;"
750           "  void* reg_save_area;"
751           "} __builtin_va_list[1];";
752  }
753  virtual void getTargetDefines(std::vector<char> &Defines) const {
754    getX86Defines(Defines, true);
755  }
756};
757} // end anonymous namespace
758
759namespace {
760// x86-64 Darwin (OS X) target
761class DarwinX86_64TargetInfo : public X86_64TargetInfo {
762public:
763  DarwinX86_64TargetInfo(const std::string& triple) :
764    X86_64TargetInfo(triple) {}
765
766  virtual void getTargetDefines(std::vector<char> &Defines) const {
767    X86_64TargetInfo::getTargetDefines(Defines);
768    getDarwinDefines(Defines);
769  }
770
771  virtual bool useNeXTRuntimeAsDefault() const { return true; }
772};
773} // end anonymous namespace.
774
775namespace {
776class ARMTargetInfo : public TargetInfo {
777public:
778  ARMTargetInfo(const std::string& triple) : TargetInfo(triple) {
779    // FIXME: Are the defaults correct for ARM?
780    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
781                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:64";
782  }
783  virtual void getTargetDefines(std::vector<char> &Defines) const {
784    getARMDefines(Defines);
785  }
786  virtual void getTargetBuiltins(const Builtin::Info *&Records,
787                                 unsigned &NumRecords) const {
788    // FIXME: Implement.
789    Records = 0;
790    NumRecords = 0;
791  }
792  virtual const char *getVAListDeclaration() const {
793    return "typedef char* __builtin_va_list;";
794  }
795  virtual const char *getTargetPrefix() const {
796    return "arm";
797  }
798  virtual void getGCCRegNames(const char * const *&Names,
799                              unsigned &NumNames) const {
800    // FIXME: Implement.
801    Names = 0;
802    NumNames = 0;
803  }
804  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
805                                unsigned &NumAliases) const {
806    // FIXME: Implement.
807    Aliases = 0;
808    NumAliases = 0;
809  }
810  virtual bool validateAsmConstraint(char c,
811                                     TargetInfo::ConstraintInfo &info) const {
812    // FIXME: Check if this is complete
813    switch (c) {
814    default:
815    case 'l': // r0-r7
816    case 'h': // r8-r15
817    case 'w': // VFP Floating point register single precision
818    case 'P': // VFP Floating point register double precision
819      info = (TargetInfo::ConstraintInfo)(info|TargetInfo::CI_AllowsRegister);
820      return true;
821    }
822    return false;
823  }
824  virtual const char *getClobbers() const {
825    // FIXME: Is this really right?
826    return "";
827  }
828};
829} // end anonymous namespace.
830
831
832namespace {
833class DarwinARMTargetInfo : public ARMTargetInfo {
834public:
835  DarwinARMTargetInfo(const std::string& triple) : ARMTargetInfo(triple) {}
836
837  virtual void getTargetDefines(std::vector<char> &Defines) const {
838    ARMTargetInfo::getTargetDefines(Defines);
839    getDarwinDefines(Defines);
840  }
841};
842} // end anonymous namespace.
843
844namespace {
845class SparcV8TargetInfo : public TargetInfo {
846public:
847  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
848    // FIXME: Support Sparc quad-precision long double?
849    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
850                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64";
851  }
852  virtual void getTargetDefines(std::vector<char> &Defines) const {
853    // FIXME: This is missing a lot of important defines; some of the
854    // missing stuff is likely to break system headers.
855    Define(Defines, "__sparc");
856    Define(Defines, "__sparc__");
857    Define(Defines, "__sparcv8");
858  }
859  virtual void getTargetBuiltins(const Builtin::Info *&Records,
860                                 unsigned &NumRecords) const {
861    // FIXME: Implement!
862  }
863  virtual const char *getVAListDeclaration() const {
864    return "typedef void* __builtin_va_list;";
865  }
866  virtual const char *getTargetPrefix() const {
867    return "sparc";
868  }
869  virtual void getGCCRegNames(const char * const *&Names,
870                              unsigned &NumNames) const {
871    // FIXME: Implement!
872    Names = 0;
873    NumNames = 0;
874  }
875  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
876                                unsigned &NumAliases) const {
877    // FIXME: Implement!
878    Aliases = 0;
879    NumAliases = 0;
880  }
881  virtual bool validateAsmConstraint(char c,
882                                     TargetInfo::ConstraintInfo &info) const {
883    // FIXME: Implement!
884    return false;
885  }
886  virtual const char *getClobbers() const {
887    // FIXME: Implement!
888    return "";
889  }
890};
891
892} // end anonymous namespace.
893
894namespace {
895class SolarisSparcV8TargetInfo : public SparcV8TargetInfo {
896public:
897  SolarisSparcV8TargetInfo(const std::string& triple) :
898    SparcV8TargetInfo(triple) {}
899
900  virtual void getTargetDefines(std::vector<char> &Defines) const {
901    SparcV8TargetInfo::getTargetDefines(Defines);
902    getSolarisDefines(Defines);
903  }
904};
905} // end anonymous namespace.
906
907namespace {
908  class PIC16TargetInfo : public TargetInfo{
909  public:
910    PIC16TargetInfo(const std::string& triple) : TargetInfo(triple) {
911      // FIXME: Is IntAlign really supposed to be 16?  There seems
912      // little point on a platform with 8-bit loads.
913      IntWidth = IntAlign = LongAlign = LongLongAlign = PointerWidth = 16;
914      PointerAlign = 8;
915      DescriptionString = "e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8";
916    }
917    virtual uint64_t getPointerWidthV(unsigned AddrSpace) const { return 16; }
918    virtual uint64_t getPointerAlignV(unsigned AddrSpace) const { return 8; }
919    virtual void getTargetDefines(std::vector<char> &Defines) const {
920      Define(Defines, "__pic16");
921    }
922    virtual void getTargetBuiltins(const Builtin::Info *&Records,
923                                   unsigned &NumRecords) const {}
924    virtual const char *getVAListDeclaration() const { return "";}
925    virtual const char *getClobbers() const {return "";}
926    virtual const char *getTargetPrefix() const {return "";}
927    virtual void getGCCRegNames(const char * const *&Names,
928                                unsigned &NumNames) const {}
929    virtual bool validateAsmConstraint(char c,
930                                       TargetInfo::ConstraintInfo &info) const {
931      return true;
932    }
933    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
934                                  unsigned &NumAliases) const {}
935    virtual bool useGlobalsForAutomaticVariables() const {return true;}
936  };
937}
938
939//===----------------------------------------------------------------------===//
940// Driver code
941//===----------------------------------------------------------------------===//
942
943static inline bool IsX86(const std::string& TT) {
944  return (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
945          TT[4] == '-' && TT[1] - '3' < 6);
946}
947
948/// CreateTargetInfo - Return the target info object for the specified target
949/// triple.
950TargetInfo* TargetInfo::CreateTargetInfo(const std::string &T) {
951  // OS detection; this isn't really anywhere near complete.
952  // Additions and corrections are welcome.
953  bool isDarwin = T.find("-darwin") != std::string::npos;
954  bool isDragonFly = T.find("-dragonfly") != std::string::npos;
955  bool isSolaris = T.find("-solaris") != std::string::npos;
956  bool isLinux = T.find("-linux") != std::string::npos;
957  bool isWindows = T.find("-windows") != std::string::npos ||
958                   T.find("-win32") != std::string::npos ||
959                   T.find("-mingw") != std::string::npos;
960
961  if (T.find("ppc-") == 0 || T.find("powerpc-") == 0) {
962    if (isDarwin)
963      return new DarwinPPCTargetInfo(T);
964    return new PPC32TargetInfo(T);
965  }
966
967  if (T.find("ppc64-") == 0 || T.find("powerpc64-") == 0) {
968    if (isDarwin)
969      return new DarwinPPC64TargetInfo(T);
970    return new PPC64TargetInfo(T);
971  }
972
973  if (T.find("armv6-") == 0 || T.find("arm-") == 0) {
974    if (isDarwin)
975      return new DarwinARMTargetInfo(T);
976    return new ARMTargetInfo(T);
977  }
978
979  if (T.find("sparc-") == 0) {
980    if (isSolaris)
981      return new SolarisSparcV8TargetInfo(T);
982    return new SparcV8TargetInfo(T);
983  }
984
985  if (T.find("x86_64-") == 0) {
986    if (isDarwin)
987      return new DarwinX86_64TargetInfo(T);
988    return new X86_64TargetInfo(T);
989  }
990
991  if (T.find("pic16-") == 0)
992    return new PIC16TargetInfo(T);
993
994  if (IsX86(T)) {
995    if (isDarwin)
996      return new DarwinI386TargetInfo(T);
997    if (isLinux)
998      return new LinuxX86_32TargetInfo(T);
999    if (isDragonFly)
1000      return new DragonFlyX86_32TargetInfo(T);
1001    if (isWindows)
1002      return new WindowsX86_32TargetInfo(T);
1003    return new X86_32TargetInfo(T);
1004  }
1005
1006  return NULL;
1007}
1008
1009