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