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