Targets.cpp revision 62a7cbc34a427d95d86fb35c69ac073c252465a9
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/Basic/Builtins.h"
16#include "clang/Basic/TargetBuiltins.h"
17#include "clang/Basic/TargetInfo.h"
18#include "clang/Basic/LangOptions.h"
19#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/Triple.h"
24#include "llvm/MC/MCSectionMachO.h"
25using namespace clang;
26
27//===----------------------------------------------------------------------===//
28//  Common code shared among targets.
29//===----------------------------------------------------------------------===//
30
31static void Define(std::vector<char> &Buf, const llvm::StringRef &Macro,
32                   const llvm::StringRef &Val = "1") {
33  const char *Def = "#define ";
34  Buf.insert(Buf.end(), Def, Def+strlen(Def));
35  Buf.insert(Buf.end(), Macro.begin(), Macro.end());
36  Buf.push_back(' ');
37  Buf.insert(Buf.end(), Val.begin(), Val.end());
38  Buf.push_back('\n');
39}
40
41/// DefineStd - Define a macro name and standard variants.  For example if
42/// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
43/// when in GNU mode.
44static void DefineStd(std::vector<char> &Buf, const char *MacroName,
45                      const LangOptions &Opts) {
46  assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
47
48  // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
49  // in the user's namespace.
50  if (Opts.GNUMode)
51    Define(Buf, MacroName);
52
53  // Define __unix.
54  llvm::SmallString<20> TmpStr;
55  TmpStr = "__";
56  TmpStr += MacroName;
57  Define(Buf, TmpStr.str());
58
59  // Define __unix__.
60  TmpStr += "__";
61  Define(Buf, TmpStr.str());
62}
63
64//===----------------------------------------------------------------------===//
65// Defines specific to certain operating systems.
66//===----------------------------------------------------------------------===//
67
68namespace {
69template<typename TgtInfo>
70class OSTargetInfo : public TgtInfo {
71protected:
72  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
73                            std::vector<char> &Defines) const=0;
74public:
75  OSTargetInfo(const std::string& triple) : TgtInfo(triple) {}
76  virtual void getTargetDefines(const LangOptions &Opts,
77                                std::vector<char> &Defines) const {
78    TgtInfo::getTargetDefines(Opts, Defines);
79    getOSDefines(Opts, TgtInfo::getTriple(), Defines);
80  }
81
82};
83} // end anonymous namespace
84
85
86static void getDarwinDefines(std::vector<char> &Defs, const LangOptions &Opts) {
87  Define(Defs, "__APPLE_CC__", "5621");
88  Define(Defs, "__APPLE__");
89  Define(Defs, "__MACH__");
90  Define(Defs, "OBJC_NEW_PROPERTIES");
91
92  // __weak is always defined, for use in blocks and with objc pointers.
93  Define(Defs, "__weak", "__attribute__((objc_gc(weak)))");
94
95  // Darwin defines __strong even in C mode (just to nothing).
96  if (!Opts.ObjC1 || Opts.getGCMode() == LangOptions::NonGC)
97    Define(Defs, "__strong", "");
98  else
99    Define(Defs, "__strong", "__attribute__((objc_gc(strong)))");
100
101  if (Opts.Static)
102    Define(Defs, "__STATIC__");
103  else
104    Define(Defs, "__DYNAMIC__");
105
106  if (Opts.POSIXThreads)
107    Define(Defs, "_REENTRANT", "1");
108}
109
110static void getDarwinOSXDefines(std::vector<char> &Defs,
111                                const llvm::Triple &Triple) {
112  if (Triple.getOS() != llvm::Triple::Darwin)
113    return;
114
115  // Figure out which "darwin number" the target triple is.  "darwin9" -> 10.5.
116  unsigned Maj, Min, Rev;
117  Triple.getDarwinNumber(Maj, Min, Rev);
118
119  char MacOSXStr[] = "1000";
120  if (Maj >= 4 && Maj <= 13) { // 10.0-10.9
121    // darwin7 -> 1030, darwin8 -> 1040, darwin9 -> 1050, etc.
122    MacOSXStr[2] = '0' + Maj-4;
123  }
124
125  // Handle minor version: 10.4.9 -> darwin8.9 -> "1049"
126  // Cap 10.4.11 -> darwin8.11 -> "1049"
127  MacOSXStr[3] = std::min(Min, 9U)+'0';
128  Define(Defs, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", MacOSXStr);
129}
130
131static void getDarwinIPhoneOSDefines(std::vector<char> &Defs,
132                                     const llvm::Triple &Triple) {
133  if (Triple.getOS() != llvm::Triple::Darwin)
134    return;
135
136  // Figure out which "darwin number" the target triple is.  "darwin9" -> 10.5.
137  unsigned Maj, Min, Rev;
138  Triple.getDarwinNumber(Maj, Min, Rev);
139
140  // When targetting iPhone OS, interpret the minor version and
141  // revision as the iPhone OS version
142  char iPhoneOSStr[] = "10000";
143  if (Min >= 2 && Min <= 9) { // iPhone OS 2.0-9.0
144    // darwin9.2.0 -> 20000, darwin9.3.0 -> 30000, etc.
145    iPhoneOSStr[0] = '0' + Min;
146  }
147
148  // Handle minor version: 2.2 -> darwin9.2.2 -> 20200
149  iPhoneOSStr[2] = std::min(Rev, 9U)+'0';
150  Define(Defs, "__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
151         iPhoneOSStr);
152}
153
154/// GetDarwinLanguageOptions - Set the default language options for darwin.
155static void GetDarwinLanguageOptions(LangOptions &Opts,
156                                     const llvm::Triple &Triple) {
157  Opts.NeXTRuntime = true;
158
159  if (Triple.getOS() != llvm::Triple::Darwin)
160    return;
161
162  unsigned MajorVersion = Triple.getDarwinMajorNumber();
163
164  // Blocks and stack protectors default to on for 10.6 (darwin10) and beyond.
165  if (MajorVersion > 9) {
166    Opts.Blocks = 1;
167    Opts.setStackProtectorMode(LangOptions::SSPOn);
168  }
169
170  // Non-fragile ABI (in 64-bit mode) default to on for 10.5 (darwin9) and
171  // beyond.
172  if (MajorVersion >= 9 && Opts.ObjC1 &&
173      Triple.getArch() == llvm::Triple::x86_64)
174    Opts.ObjCNonFragileABI = 1;
175}
176
177namespace {
178template<typename Target>
179class DarwinTargetInfo : public OSTargetInfo<Target> {
180protected:
181  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
182                    std::vector<char> &Defines) const {
183    getDarwinDefines(Defines, Opts);
184    getDarwinOSXDefines(Defines, Triple);
185  }
186
187  /// getDefaultLangOptions - Allow the target to specify default settings for
188  /// various language options.  These may be overridden by command line
189  /// options.
190  virtual void getDefaultLangOptions(LangOptions &Opts) {
191    TargetInfo::getDefaultLangOptions(Opts);
192    GetDarwinLanguageOptions(Opts, TargetInfo::getTriple());
193  }
194public:
195  DarwinTargetInfo(const std::string& triple) :
196    OSTargetInfo<Target>(triple) {
197      this->TLSSupported = false;
198    }
199
200  virtual const char *getUnicodeStringSymbolPrefix() const {
201    return "__utf16_string_";
202  }
203
204  virtual const char *getUnicodeStringSection() const {
205    return "__TEXT,__ustring";
206  }
207
208  virtual std::string isValidSectionSpecifier(const llvm::StringRef &SR) const {
209    // Let MCSectionMachO validate this.
210    llvm::StringRef Segment, Section;
211    unsigned TAA, StubSize;
212    return llvm::MCSectionMachO::ParseSectionSpecifier(SR, Segment, Section,
213                                                       TAA, StubSize);
214  }
215};
216
217
218// DragonFlyBSD Target
219template<typename Target>
220class DragonFlyBSDTargetInfo : public OSTargetInfo<Target> {
221protected:
222  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
223                    std::vector<char> &Defs) const {
224    // DragonFly defines; list based off of gcc output
225    Define(Defs, "__DragonFly__");
226    Define(Defs, "__DragonFly_cc_version", "100001");
227    Define(Defs, "__ELF__");
228    Define(Defs, "__KPRINTF_ATTRIBUTE__");
229    Define(Defs, "__tune_i386__");
230    DefineStd(Defs, "unix", Opts);
231  }
232public:
233  DragonFlyBSDTargetInfo(const std::string &triple)
234    : OSTargetInfo<Target>(triple) {}
235};
236
237// FreeBSD Target
238template<typename Target>
239class FreeBSDTargetInfo : public OSTargetInfo<Target> {
240protected:
241  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
242                    std::vector<char> &Defs) const {
243    // FreeBSD defines; list based off of gcc output
244
245    // FIXME: Move version number handling to llvm::Triple.
246    const char *FreeBSD = strstr(Triple.getTriple().c_str(),
247                                 "-freebsd");
248    FreeBSD += strlen("-freebsd");
249    char release[] = "X";
250    release[0] = FreeBSD[0];
251    char version[] = "X00001";
252    version[0] = FreeBSD[0];
253
254    Define(Defs, "__FreeBSD__", release);
255    Define(Defs, "__FreeBSD_cc_version", version);
256    Define(Defs, "__KPRINTF_ATTRIBUTE__");
257    DefineStd(Defs, "unix", Opts);
258    Define(Defs, "__ELF__", "1");
259  }
260public:
261  FreeBSDTargetInfo(const std::string &triple)
262    : OSTargetInfo<Target>(triple) {
263      this->UserLabelPrefix = "";
264    }
265};
266
267// Linux target
268template<typename Target>
269class LinuxTargetInfo : public OSTargetInfo<Target> {
270protected:
271  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
272                           std::vector<char> &Defs) const {
273    // Linux defines; list based off of gcc output
274    DefineStd(Defs, "unix", Opts);
275    DefineStd(Defs, "linux", Opts);
276    Define(Defs, "__gnu_linux__");
277    Define(Defs, "__ELF__", "1");
278    if (Opts.POSIXThreads)
279      Define(Defs, "_REENTRANT", "1");
280  }
281public:
282  LinuxTargetInfo(const std::string& triple)
283    : OSTargetInfo<Target>(triple) {
284    this->UserLabelPrefix = "";
285  }
286};
287
288// NetBSD Target
289template<typename Target>
290class NetBSDTargetInfo : public OSTargetInfo<Target> {
291protected:
292  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
293                    std::vector<char> &Defs) const {
294    // NetBSD defines; list based off of gcc output
295    Define(Defs, "__NetBSD__", "1");
296    Define(Defs, "__unix__", "1");
297    Define(Defs, "__ELF__", "1");
298    if (Opts.POSIXThreads)
299      Define(Defs, "_POSIX_THREADS", "1");
300  }
301public:
302  NetBSDTargetInfo(const std::string &triple)
303    : OSTargetInfo<Target>(triple) {
304      this->UserLabelPrefix = "";
305    }
306};
307
308// OpenBSD Target
309template<typename Target>
310class OpenBSDTargetInfo : public OSTargetInfo<Target> {
311protected:
312  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
313                    std::vector<char> &Defs) const {
314    // OpenBSD defines; list based off of gcc output
315
316    Define(Defs, "__OpenBSD__", "1");
317    DefineStd(Defs, "unix", Opts);
318    Define(Defs, "__ELF__", "1");
319    if (Opts.POSIXThreads)
320      Define(Defs, "_POSIX_THREADS", "1");
321  }
322public:
323  OpenBSDTargetInfo(const std::string &triple)
324    : OSTargetInfo<Target>(triple) {}
325};
326
327// Solaris target
328template<typename Target>
329class SolarisTargetInfo : public OSTargetInfo<Target> {
330protected:
331  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
332                                std::vector<char> &Defs) const {
333    DefineStd(Defs, "sun", Opts);
334    DefineStd(Defs, "unix", Opts);
335    Define(Defs, "__ELF__");
336    Define(Defs, "__svr4__");
337    Define(Defs, "__SVR4");
338  }
339public:
340  SolarisTargetInfo(const std::string& triple)
341    : OSTargetInfo<Target>(triple) {
342    this->UserLabelPrefix = "";
343    this->WCharType = this->SignedLong;
344    // FIXME: WIntType should be SignedLong
345  }
346};
347} // end anonymous namespace.
348
349/// GetWindowsLanguageOptions - Set the default language options for Windows.
350static void GetWindowsLanguageOptions(LangOptions &Opts,
351                                     const llvm::Triple &Triple) {
352  Opts.Microsoft = true;
353}
354
355//===----------------------------------------------------------------------===//
356// Specific target implementations.
357//===----------------------------------------------------------------------===//
358
359namespace {
360// PPC abstract base class
361class PPCTargetInfo : public TargetInfo {
362  static const Builtin::Info BuiltinInfo[];
363  static const char * const GCCRegNames[];
364  static const TargetInfo::GCCRegAlias GCCRegAliases[];
365
366public:
367  PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {}
368
369  virtual void getTargetBuiltins(const Builtin::Info *&Records,
370                                 unsigned &NumRecords) const {
371    Records = BuiltinInfo;
372    NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
373  }
374
375  virtual void getTargetDefines(const LangOptions &Opts,
376                                std::vector<char> &Defines) const;
377
378  virtual const char *getVAListDeclaration() const {
379    return "typedef char* __builtin_va_list;";
380    // This is the right definition for ABI/V4: System V.4/eabi.
381    /*return "typedef struct __va_list_tag {"
382           "  unsigned char gpr;"
383           "  unsigned char fpr;"
384           "  unsigned short reserved;"
385           "  void* overflow_arg_area;"
386           "  void* reg_save_area;"
387           "} __builtin_va_list[1];";*/
388  }
389  virtual void getGCCRegNames(const char * const *&Names,
390                              unsigned &NumNames) const;
391  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
392                                unsigned &NumAliases) const;
393  virtual bool validateAsmConstraint(const char *&Name,
394                                     TargetInfo::ConstraintInfo &Info) const {
395    switch (*Name) {
396    default: return false;
397    case 'O': // Zero
398      return true;
399    case 'b': // Base register
400    case 'f': // Floating point register
401      Info.setAllowsRegister();
402      return true;
403    }
404  }
405  virtual void getDefaultLangOptions(LangOptions &Opts) {
406    TargetInfo::getDefaultLangOptions(Opts);
407    Opts.CharIsSigned = false;
408  }
409  virtual const char *getClobbers() const {
410    return "";
411  }
412};
413
414const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
415#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false },
416#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false },
417#include "clang/Basic/BuiltinsPPC.def"
418};
419
420
421/// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
422/// #defines that are not tied to a specific subtarget.
423void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
424                                     std::vector<char> &Defs) const {
425  // Target identification.
426  Define(Defs, "__ppc__");
427  Define(Defs, "_ARCH_PPC");
428  Define(Defs, "__POWERPC__");
429  if (PointerWidth == 64) {
430    Define(Defs, "_ARCH_PPC64");
431    Define(Defs, "_LP64");
432    Define(Defs, "__LP64__");
433    Define(Defs, "__ppc64__");
434  } else {
435    Define(Defs, "__ppc__");
436  }
437
438  // Target properties.
439  Define(Defs, "_BIG_ENDIAN");
440  Define(Defs, "__BIG_ENDIAN__");
441
442  // Subtarget options.
443  Define(Defs, "__NATURAL_ALIGNMENT__");
444  Define(Defs, "__REGISTER_PREFIX__", "");
445
446  // FIXME: Should be controlled by command line option.
447  Define(Defs, "__LONG_DOUBLE_128__");
448}
449
450
451const char * const PPCTargetInfo::GCCRegNames[] = {
452  "0", "1", "2", "3", "4", "5", "6", "7",
453  "8", "9", "10", "11", "12", "13", "14", "15",
454  "16", "17", "18", "19", "20", "21", "22", "23",
455  "24", "25", "26", "27", "28", "29", "30", "31",
456  "0", "1", "2", "3", "4", "5", "6", "7",
457  "8", "9", "10", "11", "12", "13", "14", "15",
458  "16", "17", "18", "19", "20", "21", "22", "23",
459  "24", "25", "26", "27", "28", "29", "30", "31",
460  "mq", "lr", "ctr", "ap",
461  "0", "1", "2", "3", "4", "5", "6", "7",
462  "xer",
463  "0", "1", "2", "3", "4", "5", "6", "7",
464  "8", "9", "10", "11", "12", "13", "14", "15",
465  "16", "17", "18", "19", "20", "21", "22", "23",
466  "24", "25", "26", "27", "28", "29", "30", "31",
467  "vrsave", "vscr",
468  "spe_acc", "spefscr",
469  "sfp"
470};
471
472void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
473                                   unsigned &NumNames) const {
474  Names = GCCRegNames;
475  NumNames = llvm::array_lengthof(GCCRegNames);
476}
477
478const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
479  // While some of these aliases do map to different registers
480  // they still share the same register name.
481  { { "cc", "cr0", "fr0", "r0", "v0"}, "0" },
482  { { "cr1", "fr1", "r1", "sp", "v1"}, "1" },
483  { { "cr2", "fr2", "r2", "toc", "v2"}, "2" },
484  { { "cr3", "fr3", "r3", "v3"}, "3" },
485  { { "cr4", "fr4", "r4", "v4"}, "4" },
486  { { "cr5", "fr5", "r5", "v5"}, "5" },
487  { { "cr6", "fr6", "r6", "v6"}, "6" },
488  { { "cr7", "fr7", "r7", "v7"}, "7" },
489  { { "fr8", "r8", "v8"}, "8" },
490  { { "fr9", "r9", "v9"}, "9" },
491  { { "fr10", "r10", "v10"}, "10" },
492  { { "fr11", "r11", "v11"}, "11" },
493  { { "fr12", "r12", "v12"}, "12" },
494  { { "fr13", "r13", "v13"}, "13" },
495  { { "fr14", "r14", "v14"}, "14" },
496  { { "fr15", "r15", "v15"}, "15" },
497  { { "fr16", "r16", "v16"}, "16" },
498  { { "fr17", "r17", "v17"}, "17" },
499  { { "fr18", "r18", "v18"}, "18" },
500  { { "fr19", "r19", "v19"}, "19" },
501  { { "fr20", "r20", "v20"}, "20" },
502  { { "fr21", "r21", "v21"}, "21" },
503  { { "fr22", "r22", "v22"}, "22" },
504  { { "fr23", "r23", "v23"}, "23" },
505  { { "fr24", "r24", "v24"}, "24" },
506  { { "fr25", "r25", "v25"}, "25" },
507  { { "fr26", "r26", "v26"}, "26" },
508  { { "fr27", "r27", "v27"}, "27" },
509  { { "fr28", "r28", "v28"}, "28" },
510  { { "fr29", "r29", "v29"}, "29" },
511  { { "fr30", "r30", "v30"}, "30" },
512  { { "fr31", "r31", "v31"}, "31" },
513};
514
515void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
516                                     unsigned &NumAliases) const {
517  Aliases = GCCRegAliases;
518  NumAliases = llvm::array_lengthof(GCCRegAliases);
519}
520} // end anonymous namespace.
521
522namespace {
523class PPC32TargetInfo : public PPCTargetInfo {
524public:
525  PPC32TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
526    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
527                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128";
528  }
529};
530} // end anonymous namespace.
531
532namespace {
533class PPC64TargetInfo : public PPCTargetInfo {
534public:
535  PPC64TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
536    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
537    IntMaxType = SignedLong;
538    UIntMaxType = UnsignedLong;
539    Int64Type = SignedLong;
540    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
541                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128";
542  }
543};
544} // end anonymous namespace.
545
546namespace {
547// Namespace for x86 abstract base class
548const Builtin::Info BuiltinInfo[] = {
549#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false },
550#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false },
551#include "clang/Basic/BuiltinsX86.def"
552};
553
554const char *GCCRegNames[] = {
555  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
556  "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
557  "argp", "flags", "fspr", "dirflag", "frame",
558  "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
559  "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
560  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
561  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
562};
563
564const TargetInfo::GCCRegAlias GCCRegAliases[] = {
565  { { "al", "ah", "eax", "rax" }, "ax" },
566  { { "bl", "bh", "ebx", "rbx" }, "bx" },
567  { { "cl", "ch", "ecx", "rcx" }, "cx" },
568  { { "dl", "dh", "edx", "rdx" }, "dx" },
569  { { "esi", "rsi" }, "si" },
570  { { "edi", "rdi" }, "di" },
571  { { "esp", "rsp" }, "sp" },
572  { { "ebp", "rbp" }, "bp" },
573};
574
575// X86 target abstract base class; x86-32 and x86-64 are very close, so
576// most of the implementation can be shared.
577class X86TargetInfo : public TargetInfo {
578  enum X86SSEEnum {
579    NoMMXSSE, MMX, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42
580  } SSELevel;
581public:
582  X86TargetInfo(const std::string& triple)
583    : TargetInfo(triple), SSELevel(NoMMXSSE) {
584    LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
585  }
586  virtual void getTargetBuiltins(const Builtin::Info *&Records,
587                                 unsigned &NumRecords) const {
588    Records = BuiltinInfo;
589    NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
590  }
591  virtual void getGCCRegNames(const char * const *&Names,
592                              unsigned &NumNames) const {
593    Names = GCCRegNames;
594    NumNames = llvm::array_lengthof(GCCRegNames);
595  }
596  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
597                                unsigned &NumAliases) const {
598    Aliases = GCCRegAliases;
599    NumAliases = llvm::array_lengthof(GCCRegAliases);
600  }
601  virtual bool validateAsmConstraint(const char *&Name,
602                                     TargetInfo::ConstraintInfo &info) const;
603  virtual std::string convertConstraint(const char Constraint) const;
604  virtual const char *getClobbers() const {
605    return "~{dirflag},~{fpsr},~{flags}";
606  }
607  virtual void getTargetDefines(const LangOptions &Opts,
608                                std::vector<char> &Defines) const;
609  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
610                                 const std::string &Name,
611                                 bool Enabled) const;
612  virtual void getDefaultFeatures(const std::string &CPU,
613                                  llvm::StringMap<bool> &Features) const;
614  virtual void HandleTargetFeatures(const llvm::StringMap<bool> &Features);
615};
616
617void X86TargetInfo::getDefaultFeatures(const std::string &CPU,
618                                       llvm::StringMap<bool> &Features) const {
619  // FIXME: This should not be here.
620  Features["3dnow"] = false;
621  Features["3dnowa"] = false;
622  Features["mmx"] = false;
623  Features["sse"] = false;
624  Features["sse2"] = false;
625  Features["sse3"] = false;
626  Features["ssse3"] = false;
627  Features["sse41"] = false;
628  Features["sse42"] = false;
629
630  // LLVM does not currently recognize this.
631  // Features["sse4a"] = false;
632
633  // FIXME: This *really* should not be here.
634
635  // X86_64 always has SSE2.
636  if (PointerWidth == 64)
637    Features["sse2"] = Features["sse"] = Features["mmx"] = true;
638
639  if (CPU == "generic" || CPU == "i386" || CPU == "i486" || CPU == "i586" ||
640      CPU == "pentium" || CPU == "i686" || CPU == "pentiumpro")
641    ;
642  else if (CPU == "pentium-mmx" || CPU == "pentium2")
643    setFeatureEnabled(Features, "mmx", true);
644  else if (CPU == "pentium3")
645    setFeatureEnabled(Features, "sse", true);
646  else if (CPU == "pentium-m" || CPU == "pentium4" || CPU == "x86-64")
647    setFeatureEnabled(Features, "sse2", true);
648  else if (CPU == "yonah" || CPU == "prescott" || CPU == "nocona")
649    setFeatureEnabled(Features, "sse3", true);
650  else if (CPU == "core2")
651    setFeatureEnabled(Features, "ssse3", true);
652  else if (CPU == "penryn") {
653    setFeatureEnabled(Features, "sse4", true);
654    Features["sse42"] = false;
655  } else if (CPU == "atom")
656    setFeatureEnabled(Features, "sse3", true);
657  else if (CPU == "corei7")
658    setFeatureEnabled(Features, "sse4", true);
659  else if (CPU == "k6" || CPU == "winchip-c6")
660    setFeatureEnabled(Features, "mmx", true);
661  else if (CPU == "k6-2" || CPU == "k6-3" || CPU == "athlon" ||
662           CPU == "athlon-tbird" || CPU == "winchip2" || CPU == "c3") {
663    setFeatureEnabled(Features, "mmx", true);
664    setFeatureEnabled(Features, "3dnow", true);
665  } else if (CPU == "athlon-4" || CPU == "athlon-xp" || CPU == "athlon-mp") {
666    setFeatureEnabled(Features, "sse", true);
667    setFeatureEnabled(Features, "3dnowa", true);
668  } else if (CPU == "k8" || CPU == "opteron" || CPU == "athlon64" ||
669           CPU == "athlon-fx") {
670    setFeatureEnabled(Features, "sse2", true);
671    setFeatureEnabled(Features, "3dnowa", true);
672  } else if (CPU == "c3-2")
673    setFeatureEnabled(Features, "sse", true);
674}
675
676bool X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
677                                      const std::string &Name,
678                                      bool Enabled) const {
679  // FIXME: This *really* should not be here.
680  if (!Features.count(Name) && Name != "sse4")
681    return false;
682
683  if (Enabled) {
684    if (Name == "mmx")
685      Features["mmx"] = true;
686    else if (Name == "sse")
687      Features["mmx"] = Features["sse"] = true;
688    else if (Name == "sse2")
689      Features["mmx"] = Features["sse"] = Features["sse2"] = true;
690    else if (Name == "sse3")
691      Features["mmx"] = Features["sse"] = Features["sse2"] =
692        Features["sse3"] = true;
693    else if (Name == "ssse3")
694      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
695        Features["ssse3"] = true;
696    else if (Name == "sse4")
697      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
698        Features["ssse3"] = Features["sse41"] = Features["sse42"] = true;
699    else if (Name == "3dnow")
700      Features["3dnowa"] = true;
701    else if (Name == "3dnowa")
702      Features["3dnow"] = Features["3dnowa"] = true;
703  } else {
704    if (Name == "mmx")
705      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
706        Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
707    else if (Name == "sse")
708      Features["sse"] = Features["sse2"] = Features["sse3"] =
709        Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
710    else if (Name == "sse2")
711      Features["sse2"] = Features["sse3"] = Features["ssse3"] =
712        Features["sse41"] = Features["sse42"] = false;
713    else if (Name == "sse3")
714      Features["sse3"] = Features["ssse3"] = Features["sse41"] =
715        Features["sse42"] = false;
716    else if (Name == "ssse3")
717      Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
718    else if (Name == "sse4")
719      Features["sse41"] = Features["sse42"] = false;
720    else if (Name == "3dnow")
721      Features["3dnow"] = Features["3dnowa"] = false;
722    else if (Name == "3dnowa")
723      Features["3dnowa"] = false;
724  }
725
726  return true;
727}
728
729/// HandleTargetOptions - Perform initialization based on the user
730/// configured set of features.
731void X86TargetInfo::HandleTargetFeatures(const llvm::StringMap<bool>&Features) {
732  if (Features.lookup("sse42"))
733    SSELevel = SSE42;
734  else if (Features.lookup("sse41"))
735    SSELevel = SSE41;
736  else if (Features.lookup("ssse3"))
737    SSELevel = SSSE3;
738  else if (Features.lookup("sse3"))
739    SSELevel = SSE3;
740  else if (Features.lookup("sse2"))
741    SSELevel = SSE2;
742  else if (Features.lookup("sse"))
743    SSELevel = SSE1;
744  else if (Features.lookup("mmx"))
745    SSELevel = MMX;
746}
747
748/// X86TargetInfo::getTargetDefines - Return a set of the X86-specific #defines
749/// that are not tied to a specific subtarget.
750void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
751                                     std::vector<char> &Defs) const {
752  // Target identification.
753  if (PointerWidth == 64) {
754    Define(Defs, "_LP64");
755    Define(Defs, "__LP64__");
756    Define(Defs, "__amd64__");
757    Define(Defs, "__amd64");
758    Define(Defs, "__x86_64");
759    Define(Defs, "__x86_64__");
760  } else {
761    DefineStd(Defs, "i386", Opts);
762  }
763
764  // Target properties.
765  Define(Defs, "__LITTLE_ENDIAN__");
766
767  // Subtarget options.
768  Define(Defs, "__nocona");
769  Define(Defs, "__nocona__");
770  Define(Defs, "__tune_nocona__");
771  Define(Defs, "__REGISTER_PREFIX__", "");
772
773  // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
774  // functions in glibc header files that use FP Stack inline asm which the
775  // backend can't deal with (PR879).
776  Define(Defs, "__NO_MATH_INLINES");
777
778  // Each case falls through to the previous one here.
779  switch (SSELevel) {
780  case SSE42:
781    Define(Defs, "__SSE4_2__");
782  case SSE41:
783    Define(Defs, "__SSE4_1__");
784  case SSSE3:
785    Define(Defs, "__SSSE3__");
786  case SSE3:
787    Define(Defs, "__SSE3__");
788  case SSE2:
789    Define(Defs, "__SSE2__");
790    Define(Defs, "__SSE2_MATH__");  // -mfp-math=sse always implied.
791  case SSE1:
792    Define(Defs, "__SSE__");
793    Define(Defs, "__SSE_MATH__");   // -mfp-math=sse always implied.
794  case MMX:
795    Define(Defs, "__MMX__");
796  case NoMMXSSE:
797    break;
798  }
799}
800
801
802bool
803X86TargetInfo::validateAsmConstraint(const char *&Name,
804                                     TargetInfo::ConstraintInfo &Info) const {
805  switch (*Name) {
806  default: return false;
807  case 'a': // eax.
808  case 'b': // ebx.
809  case 'c': // ecx.
810  case 'd': // edx.
811  case 'S': // esi.
812  case 'D': // edi.
813  case 'A': // edx:eax.
814  case 't': // top of floating point stack.
815  case 'u': // second from top of floating point stack.
816  case 'q': // Any register accessible as [r]l: a, b, c, and d.
817  case 'y': // Any MMX register.
818  case 'x': // Any SSE register.
819  case 'Q': // Any register accessible as [r]h: a, b, c, and d.
820  case 'e': // 32-bit signed integer constant for use with zero-extending
821            // x86_64 instructions.
822  case 'Z': // 32-bit unsigned integer constant for use with zero-extending
823            // x86_64 instructions.
824  case 'N': // unsigned 8-bit integer constant for use with in and out
825            // instructions.
826  case 'R': // "legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
827    Info.setAllowsRegister();
828    return true;
829  }
830}
831
832std::string
833X86TargetInfo::convertConstraint(const char Constraint) const {
834  switch (Constraint) {
835  case 'a': return std::string("{ax}");
836  case 'b': return std::string("{bx}");
837  case 'c': return std::string("{cx}");
838  case 'd': return std::string("{dx}");
839  case 'S': return std::string("{si}");
840  case 'D': return std::string("{di}");
841  case 't': // top of floating point stack.
842    return std::string("{st}");
843  case 'u': // second from top of floating point stack.
844    return std::string("{st(1)}"); // second from top of floating point stack.
845  default:
846    return std::string(1, Constraint);
847  }
848}
849} // end anonymous namespace
850
851namespace {
852// X86-32 generic target
853class X86_32TargetInfo : public X86TargetInfo {
854public:
855  X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
856    DoubleAlign = LongLongAlign = 32;
857    LongDoubleWidth = 96;
858    LongDoubleAlign = 32;
859    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
860                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
861                        "a0:0:64-f80:32:32";
862    SizeType = UnsignedInt;
863    PtrDiffType = SignedInt;
864    IntPtrType = SignedInt;
865    RegParmMax = 3;
866  }
867  virtual const char *getVAListDeclaration() const {
868    return "typedef char* __builtin_va_list;";
869  }
870};
871} // end anonymous namespace
872
873namespace {
874class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
875public:
876  OpenBSDI386TargetInfo(const std::string& triple) :
877    OpenBSDTargetInfo<X86_32TargetInfo>(triple) {
878    SizeType = UnsignedLong;
879    IntPtrType = SignedLong;
880    PtrDiffType = SignedLong;
881  }
882};
883} // end anonymous namespace
884
885namespace {
886class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
887public:
888  DarwinI386TargetInfo(const std::string& triple) :
889    DarwinTargetInfo<X86_32TargetInfo>(triple) {
890    LongDoubleWidth = 128;
891    LongDoubleAlign = 128;
892    SizeType = UnsignedLong;
893    IntPtrType = SignedLong;
894    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
895                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
896                        "a0:0:64-f80:128:128";
897  }
898
899};
900} // end anonymous namespace
901
902namespace {
903// x86-32 Windows target
904class WindowsX86_32TargetInfo : public X86_32TargetInfo {
905public:
906  WindowsX86_32TargetInfo(const std::string& triple)
907    : X86_32TargetInfo(triple) {
908    TLSSupported = false;
909    WCharType = UnsignedShort;
910    WCharWidth = WCharAlign = 16;
911    DoubleAlign = LongLongAlign = 64;
912    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
913                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
914                        "a0:0:64-f80:32:32";
915  }
916  virtual void getTargetDefines(const LangOptions &Opts,
917                                std::vector<char> &Defines) const {
918    X86_32TargetInfo::getTargetDefines(Opts, Defines);
919    // This list is based off of the the list of things MingW defines
920    Define(Defines, "_WIN32");
921    DefineStd(Defines, "WIN32", Opts);
922    DefineStd(Defines, "WINNT", Opts);
923    Define(Defines, "_X86_");
924    Define(Defines, "__MSVCRT__");
925  }
926
927  virtual void getDefaultLangOptions(LangOptions &Opts) {
928    X86_32TargetInfo::getDefaultLangOptions(Opts);
929    GetWindowsLanguageOptions(Opts, getTriple());
930  }
931};
932} // end anonymous namespace
933
934namespace {
935// x86-64 generic target
936class X86_64TargetInfo : public X86TargetInfo {
937public:
938  X86_64TargetInfo(const std::string &triple) : X86TargetInfo(triple) {
939    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
940    LongDoubleWidth = 128;
941    LongDoubleAlign = 128;
942    IntMaxType = SignedLong;
943    UIntMaxType = UnsignedLong;
944    Int64Type = SignedLong;
945    RegParmMax = 6;
946
947    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
948                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
949                        "a0:0:64-s0:64:64-f80:128:128";
950  }
951  virtual const char *getVAListDeclaration() const {
952    return "typedef struct __va_list_tag {"
953           "  unsigned gp_offset;"
954           "  unsigned fp_offset;"
955           "  void* overflow_arg_area;"
956           "  void* reg_save_area;"
957           "} __va_list_tag;"
958           "typedef __va_list_tag __builtin_va_list[1];";
959  }
960};
961} // end anonymous namespace
962
963namespace {
964class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
965public:
966  DarwinX86_64TargetInfo(const std::string& triple)
967      : DarwinTargetInfo<X86_64TargetInfo>(triple) {
968    Int64Type = SignedLongLong;
969  }
970};
971} // end anonymous namespace
972
973namespace {
974class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
975public:
976  OpenBSDX86_64TargetInfo(const std::string& triple)
977      : OpenBSDTargetInfo<X86_64TargetInfo>(triple) {
978    IntMaxType = SignedLongLong;
979    UIntMaxType = UnsignedLongLong;
980    Int64Type = SignedLongLong;
981  }
982};
983} // end anonymous namespace
984
985namespace {
986class ARMTargetInfo : public TargetInfo {
987  enum {
988    Armv4t,
989    Armv5,
990    Armv6,
991    Armv7a,
992    XScale
993  } ArmArch;
994public:
995  ARMTargetInfo(const std::string& triple) : TargetInfo(triple) {
996    // FIXME: Are the defaults correct for ARM?
997    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
998                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:64:64";
999    if (triple.find("armv7-") == 0)
1000      ArmArch = Armv7a;
1001    else if (triple.find("arm-") == 0 || triple.find("armv6-") == 0)
1002      ArmArch = Armv6;
1003    else if (triple.find("armv5-") == 0)
1004      ArmArch = Armv5;
1005    else if (triple.find("armv4t-") == 0)
1006      ArmArch = Armv4t;
1007    else if (triple.find("xscale-") == 0)
1008      ArmArch = XScale;
1009    else if (triple.find("armv") == 0) {
1010      // FIXME: fuzzy match for other random weird arm triples.  This is useful
1011      // for the static analyzer and other clients, but probably should be
1012      // re-evaluated when codegen is brought up.
1013      ArmArch = Armv6;
1014    }
1015  }
1016  virtual void getTargetDefines(const LangOptions &Opts,
1017                                std::vector<char> &Defs) const {
1018    // Target identification.
1019    Define(Defs, "__arm");
1020    Define(Defs, "__arm__");
1021
1022    // Target properties.
1023    Define(Defs, "__LITTLE_ENDIAN__");
1024
1025    // Subtarget options.
1026    if (ArmArch == Armv7a) {
1027      Define(Defs, "__ARM_ARCH_7A__");
1028      Define(Defs, "__THUMB_INTERWORK__");
1029    } else if (ArmArch == Armv6) {
1030      Define(Defs, "__ARM_ARCH_6K__");
1031      Define(Defs, "__THUMB_INTERWORK__");
1032    } else if (ArmArch == Armv5) {
1033      Define(Defs, "__ARM_ARCH_5TEJ__");
1034      Define(Defs, "__THUMB_INTERWORK__");
1035      Define(Defs, "__SOFTFP__");
1036    } else if (ArmArch == Armv4t) {
1037      Define(Defs, "__ARM_ARCH_4T__");
1038      Define(Defs, "__SOFTFP__");
1039    } else if (ArmArch == XScale) {
1040      Define(Defs, "__ARM_ARCH_5TE__");
1041      Define(Defs, "__XSCALE__");
1042      Define(Defs, "__SOFTFP__");
1043    }
1044    Define(Defs, "__ARMEL__");
1045    Define(Defs, "__APCS_32__");
1046    Define(Defs, "__VFP_FP__");
1047  }
1048  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1049                                 unsigned &NumRecords) const {
1050    // FIXME: Implement.
1051    Records = 0;
1052    NumRecords = 0;
1053  }
1054  virtual const char *getVAListDeclaration() const {
1055    return "typedef char* __builtin_va_list;";
1056  }
1057  virtual void getGCCRegNames(const char * const *&Names,
1058                              unsigned &NumNames) const {
1059    // FIXME: Implement.
1060    Names = 0;
1061    NumNames = 0;
1062  }
1063  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1064                                unsigned &NumAliases) const {
1065    // FIXME: Implement.
1066    Aliases = 0;
1067    NumAliases = 0;
1068  }
1069  virtual bool validateAsmConstraint(const char *&Name,
1070                                     TargetInfo::ConstraintInfo &Info) const {
1071    // FIXME: Check if this is complete
1072    switch (*Name) {
1073    default:
1074    case 'l': // r0-r7
1075    case 'h': // r8-r15
1076    case 'w': // VFP Floating point register single precision
1077    case 'P': // VFP Floating point register double precision
1078      Info.setAllowsRegister();
1079      return true;
1080    }
1081    return false;
1082  }
1083  virtual const char *getClobbers() const {
1084    // FIXME: Is this really right?
1085    return "";
1086  }
1087};
1088} // end anonymous namespace.
1089
1090
1091namespace {
1092class DarwinARMTargetInfo :
1093  public DarwinTargetInfo<ARMTargetInfo> {
1094protected:
1095  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
1096                    std::vector<char> &Defines) const {
1097    getDarwinDefines(Defines, Opts);
1098    getDarwinIPhoneOSDefines(Defines, Triple);
1099  }
1100
1101public:
1102  DarwinARMTargetInfo(const std::string& triple)
1103    : DarwinTargetInfo<ARMTargetInfo>(triple) {}
1104};
1105} // end anonymous namespace.
1106
1107namespace {
1108class SparcV8TargetInfo : public TargetInfo {
1109  static const TargetInfo::GCCRegAlias GCCRegAliases[];
1110  static const char * const GCCRegNames[];
1111public:
1112  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
1113    // FIXME: Support Sparc quad-precision long double?
1114    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1115                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64";
1116  }
1117  virtual void getTargetDefines(const LangOptions &Opts,
1118                                std::vector<char> &Defines) const {
1119    DefineStd(Defines, "sparc", Opts);
1120    Define(Defines, "__sparcv8");
1121    Define(Defines, "__REGISTER_PREFIX__", "");
1122  }
1123  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1124                                 unsigned &NumRecords) const {
1125    // FIXME: Implement!
1126  }
1127  virtual const char *getVAListDeclaration() const {
1128    return "typedef void* __builtin_va_list;";
1129  }
1130  virtual void getGCCRegNames(const char * const *&Names,
1131                              unsigned &NumNames) const;
1132  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1133                                unsigned &NumAliases) const;
1134  virtual bool validateAsmConstraint(const char *&Name,
1135                                     TargetInfo::ConstraintInfo &info) const {
1136    // FIXME: Implement!
1137    return false;
1138  }
1139  virtual const char *getClobbers() const {
1140    // FIXME: Implement!
1141    return "";
1142  }
1143};
1144
1145const char * const SparcV8TargetInfo::GCCRegNames[] = {
1146  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1147  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1148  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
1149  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
1150};
1151
1152void SparcV8TargetInfo::getGCCRegNames(const char * const *&Names,
1153                                       unsigned &NumNames) const {
1154  Names = GCCRegNames;
1155  NumNames = llvm::array_lengthof(GCCRegNames);
1156}
1157
1158const TargetInfo::GCCRegAlias SparcV8TargetInfo::GCCRegAliases[] = {
1159  { { "g0" }, "r0" },
1160  { { "g1" }, "r1" },
1161  { { "g2" }, "r2" },
1162  { { "g3" }, "r3" },
1163  { { "g4" }, "r4" },
1164  { { "g5" }, "r5" },
1165  { { "g6" }, "r6" },
1166  { { "g7" }, "r7" },
1167  { { "o0" }, "r8" },
1168  { { "o1" }, "r9" },
1169  { { "o2" }, "r10" },
1170  { { "o3" }, "r11" },
1171  { { "o4" }, "r12" },
1172  { { "o5" }, "r13" },
1173  { { "o6", "sp" }, "r14" },
1174  { { "o7" }, "r15" },
1175  { { "l0" }, "r16" },
1176  { { "l1" }, "r17" },
1177  { { "l2" }, "r18" },
1178  { { "l3" }, "r19" },
1179  { { "l4" }, "r20" },
1180  { { "l5" }, "r21" },
1181  { { "l6" }, "r22" },
1182  { { "l7" }, "r23" },
1183  { { "i0" }, "r24" },
1184  { { "i1" }, "r25" },
1185  { { "i2" }, "r26" },
1186  { { "i3" }, "r27" },
1187  { { "i4" }, "r28" },
1188  { { "i5" }, "r29" },
1189  { { "i6", "fp" }, "r30" },
1190  { { "i7" }, "r31" },
1191};
1192
1193void SparcV8TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1194                                         unsigned &NumAliases) const {
1195  Aliases = GCCRegAliases;
1196  NumAliases = llvm::array_lengthof(GCCRegAliases);
1197}
1198} // end anonymous namespace.
1199
1200namespace {
1201class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
1202public:
1203  SolarisSparcV8TargetInfo(const std::string& triple) :
1204      SolarisTargetInfo<SparcV8TargetInfo>(triple) {
1205    SizeType = UnsignedInt;
1206    PtrDiffType = SignedInt;
1207  }
1208};
1209} // end anonymous namespace.
1210
1211namespace {
1212  class PIC16TargetInfo : public TargetInfo{
1213  public:
1214    PIC16TargetInfo(const std::string& triple) : TargetInfo(triple) {
1215      TLSSupported = false;
1216      IntWidth = 16;
1217      LongWidth = LongLongWidth = 32;
1218      IntMaxTWidth = 32;
1219      PointerWidth = 16;
1220      IntAlign = 8;
1221      LongAlign = LongLongAlign = 8;
1222      PointerAlign = 8;
1223      SizeType = UnsignedInt;
1224      IntMaxType = SignedLong;
1225      UIntMaxType = UnsignedLong;
1226      IntPtrType = SignedShort;
1227      PtrDiffType = SignedInt;
1228      FloatWidth = 32;
1229      FloatAlign = 32;
1230      DoubleWidth = 32;
1231      DoubleAlign = 32;
1232      LongDoubleWidth = 32;
1233      LongDoubleAlign = 32;
1234      FloatFormat = &llvm::APFloat::IEEEsingle;
1235      DoubleFormat = &llvm::APFloat::IEEEsingle;
1236      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
1237      DescriptionString = "e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8-f32:32:32";
1238
1239    }
1240    virtual uint64_t getPointerWidthV(unsigned AddrSpace) const { return 16; }
1241    virtual uint64_t getPointerAlignV(unsigned AddrSpace) const { return 8; }
1242    virtual void getTargetDefines(const LangOptions &Opts,
1243                                  std::vector<char> &Defines) const {
1244      Define(Defines, "__pic16");
1245      Define(Defines, "rom", "__attribute__((address_space(1)))");
1246      Define(Defines, "ram", "__attribute__((address_space(0)))");
1247      Define(Defines, "_section(SectName)",
1248             "__attribute__((section(SectName)))");
1249      Define(Defines, "_address(Addr)",
1250             "__attribute__((section(\"Address=\"#Addr)))");
1251      Define(Defines, "_CONFIG(conf)", "asm(\"CONFIG \"#conf)");
1252      Define(Defines, "_interrupt",
1253             "__attribute__((section(\"interrupt=0x4\"))) \
1254             __attribute__((used))");
1255    }
1256    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1257                                   unsigned &NumRecords) const {}
1258    virtual const char *getVAListDeclaration() const {
1259      return "";
1260    }
1261    virtual const char *getClobbers() const {
1262      return "";
1263    }
1264    virtual void getGCCRegNames(const char * const *&Names,
1265                                unsigned &NumNames) const {}
1266    virtual bool validateAsmConstraint(const char *&Name,
1267                                       TargetInfo::ConstraintInfo &info) const {
1268      return true;
1269    }
1270    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1271                                  unsigned &NumAliases) const {}
1272    virtual bool useGlobalsForAutomaticVariables() const {return true;}
1273  };
1274}
1275
1276namespace {
1277  class MSP430TargetInfo : public TargetInfo {
1278    static const char * const GCCRegNames[];
1279  public:
1280    MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
1281      TLSSupported = false;
1282      IntWidth = 16;
1283      LongWidth = LongLongWidth = 32;
1284      IntMaxTWidth = 32;
1285      PointerWidth = 16;
1286      IntAlign = 8;
1287      LongAlign = LongLongAlign = 8;
1288      PointerAlign = 8;
1289      SizeType = UnsignedInt;
1290      IntMaxType = SignedLong;
1291      UIntMaxType = UnsignedLong;
1292      IntPtrType = SignedShort;
1293      PtrDiffType = SignedInt;
1294      DescriptionString = "e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8";
1295   }
1296    virtual void getTargetDefines(const LangOptions &Opts,
1297                                 std::vector<char> &Defines) const {
1298      Define(Defines, "MSP430");
1299      Define(Defines, "__MSP430__");
1300      // FIXME: defines for different 'flavours' of MCU
1301    }
1302    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1303                                   unsigned &NumRecords) const {
1304     // FIXME: Implement.
1305      Records = 0;
1306      NumRecords = 0;
1307    }
1308    virtual void getGCCRegNames(const char * const *&Names,
1309                                unsigned &NumNames) const;
1310    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1311                                  unsigned &NumAliases) const {
1312      // No aliases.
1313      Aliases = 0;
1314      NumAliases = 0;
1315    }
1316    virtual bool validateAsmConstraint(const char *&Name,
1317                                       TargetInfo::ConstraintInfo &info) const {
1318      // FIXME: implement
1319      return true;
1320    }
1321    virtual const char *getClobbers() const {
1322      // FIXME: Is this really right?
1323      return "";
1324    }
1325    virtual const char *getVAListDeclaration() const {
1326      // FIXME: implement
1327      return "typedef char* __builtin_va_list;";
1328   }
1329  };
1330
1331  const char * const MSP430TargetInfo::GCCRegNames[] = {
1332    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1333    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1334  };
1335
1336  void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
1337                                        unsigned &NumNames) const {
1338    Names = GCCRegNames;
1339    NumNames = llvm::array_lengthof(GCCRegNames);
1340  }
1341}
1342
1343
1344namespace {
1345  class SystemZTargetInfo : public TargetInfo {
1346    static const char * const GCCRegNames[];
1347  public:
1348    SystemZTargetInfo(const std::string& triple) : TargetInfo(triple) {
1349      TLSSupported = false;
1350      IntWidth = IntAlign = 32;
1351      LongWidth = LongLongWidth = LongAlign = LongLongAlign = 64;
1352      PointerWidth = PointerAlign = 64;
1353      DescriptionString = "E-p:64:64:64-i8:8:16-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f128:128:128-a0:16:16";
1354   }
1355    virtual void getTargetDefines(const LangOptions &Opts,
1356                                 std::vector<char> &Defines) const {
1357      Define(Defines, "__s390__");
1358      Define(Defines, "__s390x__");
1359    }
1360    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1361                                   unsigned &NumRecords) const {
1362      // FIXME: Implement.
1363      Records = 0;
1364      NumRecords = 0;
1365    }
1366
1367    virtual void getDefaultLangOptions(LangOptions &Opts) {
1368      TargetInfo::getDefaultLangOptions(Opts);
1369      Opts.CharIsSigned = false;
1370    }
1371
1372    virtual void getGCCRegNames(const char * const *&Names,
1373                                unsigned &NumNames) const;
1374    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1375                                  unsigned &NumAliases) const {
1376      // No aliases.
1377      Aliases = 0;
1378      NumAliases = 0;
1379    }
1380    virtual bool validateAsmConstraint(const char *&Name,
1381                                       TargetInfo::ConstraintInfo &info) const {
1382      // FIXME: implement
1383      return true;
1384    }
1385    virtual const char *getClobbers() const {
1386      // FIXME: Is this really right?
1387      return "";
1388    }
1389    virtual const char *getVAListDeclaration() const {
1390      // FIXME: implement
1391      return "typedef char* __builtin_va_list;";
1392   }
1393  };
1394
1395  const char * const SystemZTargetInfo::GCCRegNames[] = {
1396    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1397    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1398  };
1399
1400  void SystemZTargetInfo::getGCCRegNames(const char * const *&Names,
1401                                         unsigned &NumNames) const {
1402    Names = GCCRegNames;
1403    NumNames = llvm::array_lengthof(GCCRegNames);
1404  }
1405}
1406
1407namespace {
1408  class BlackfinTargetInfo : public TargetInfo {
1409    static const char * const GCCRegNames[];
1410  public:
1411    BlackfinTargetInfo(const std::string& triple) : TargetInfo(triple) {
1412      TLSSupported = false;
1413      DoubleAlign = 32;
1414      LongLongAlign = 32;
1415      LongDoubleAlign = 32;
1416      DescriptionString = "e-p:32:32-i64:32-f64:32";
1417    }
1418
1419    virtual void getTargetDefines(const LangOptions &Opts,
1420                                  std::vector<char> &Defines) const {
1421      DefineStd(Defines, "bfin", Opts);
1422      DefineStd(Defines, "BFIN", Opts);
1423      Define(Defines, "__ADSPBLACKFIN__");
1424      // FIXME: This one is really dependent on -mcpu
1425      Define(Defines, "__ADSPLPBLACKFIN__");
1426      // FIXME: Add cpu-dependent defines and __SILICON_REVISION__
1427    }
1428
1429    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1430                                   unsigned &NumRecords) const {
1431      // FIXME: Implement.
1432      Records = 0;
1433      NumRecords = 0;
1434    }
1435
1436    virtual void getGCCRegNames(const char * const *&Names,
1437                                unsigned &NumNames) const;
1438
1439    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1440                                  unsigned &NumAliases) const {
1441      // No aliases.
1442      Aliases = 0;
1443      NumAliases = 0;
1444    }
1445
1446    virtual bool validateAsmConstraint(const char *&Name,
1447                                       TargetInfo::ConstraintInfo &Info) const {
1448      if (strchr("adzDWeABbvfcCtukxywZY", Name[0])) {
1449        Info.setAllowsRegister();
1450        return true;
1451      }
1452      return false;
1453    }
1454
1455    virtual const char *getClobbers() const {
1456      return "";
1457    }
1458
1459    virtual const char *getVAListDeclaration() const {
1460      return "typedef char* __builtin_va_list;";
1461    }
1462  };
1463
1464  const char * const BlackfinTargetInfo::GCCRegNames[] = {
1465    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1466    "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
1467    "i0", "i1", "i2", "i3", "b0", "b1", "b2", "b3",
1468    "l0", "l1", "l2", "l3", "m0", "m1", "m2", "m3",
1469    "a0", "a1", "cc",
1470    "rets", "reti", "retx", "retn", "rete", "astat", "seqstat", "usp",
1471    "argp", "lt0", "lt1", "lc0", "lc1", "lb0", "lb1"
1472  };
1473
1474  void BlackfinTargetInfo::getGCCRegNames(const char * const *&Names,
1475                                          unsigned &NumNames) const {
1476    Names = GCCRegNames;
1477    NumNames = llvm::array_lengthof(GCCRegNames);
1478  }
1479}
1480
1481namespace {
1482
1483  // LLVM and Clang cannot be used directly to output native binaries for
1484  // target, but is used to compile C code to llvm bitcode with correct
1485  // type and alignment information.
1486  //
1487  // TCE uses the llvm bitcode as input and uses it for generating customized
1488  // target processor and program binary. TCE co-design environment is
1489  // publicly available in http://tce.cs.tut.fi
1490
1491  class TCETargetInfo : public TargetInfo{
1492  public:
1493    TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
1494      TLSSupported = false;
1495      IntWidth = 32;
1496      LongWidth = LongLongWidth = 32;
1497      IntMaxTWidth = 32;
1498      PointerWidth = 32;
1499      IntAlign = 32;
1500      LongAlign = LongLongAlign = 32;
1501      PointerAlign = 32;
1502      SizeType = UnsignedInt;
1503      IntMaxType = SignedLong;
1504      UIntMaxType = UnsignedLong;
1505      IntPtrType = SignedInt;
1506      PtrDiffType = SignedInt;
1507      FloatWidth = 32;
1508      FloatAlign = 32;
1509      DoubleWidth = 32;
1510      DoubleAlign = 32;
1511      LongDoubleWidth = 32;
1512      LongDoubleAlign = 32;
1513      FloatFormat = &llvm::APFloat::IEEEsingle;
1514      DoubleFormat = &llvm::APFloat::IEEEsingle;
1515      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
1516      DescriptionString = "E-p:32:32:32-a0:32:32"
1517                          "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64"
1518                          "-f32:32:32-f64:32:64";
1519    }
1520
1521    virtual void getTargetDefines(const LangOptions &Opts,
1522                                  std::vector<char> &Defines) const {
1523      DefineStd(Defines, "tce", Opts);
1524      Define(Defines, "__TCE__");
1525      Define(Defines, "__TCE_V1__");
1526    }
1527    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1528                                   unsigned &NumRecords) const {}
1529    virtual const char *getClobbers() const {
1530      return "";
1531    }
1532    virtual const char *getVAListDeclaration() const {
1533      return "typedef void* __builtin_va_list;";
1534    }
1535    virtual void getGCCRegNames(const char * const *&Names,
1536                                unsigned &NumNames) const {}
1537    virtual bool validateAsmConstraint(const char *&Name,
1538                                       TargetInfo::ConstraintInfo &info) const {
1539      return true;
1540    }
1541    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1542                                  unsigned &NumAliases) const {}
1543  };
1544}
1545
1546//===----------------------------------------------------------------------===//
1547// Driver code
1548//===----------------------------------------------------------------------===//
1549
1550/// CreateTargetInfo - Return the target info object for the specified target
1551/// triple.
1552TargetInfo* TargetInfo::CreateTargetInfo(const std::string &T) {
1553  llvm::Triple Triple(T);
1554  llvm::Triple::OSType os = Triple.getOS();
1555
1556  switch (Triple.getArch()) {
1557  default:
1558    return NULL;
1559
1560  case llvm::Triple::arm:
1561    switch (os) {
1562    case llvm::Triple::Darwin:
1563      return new DarwinARMTargetInfo(T);
1564    case llvm::Triple::FreeBSD:
1565      return new FreeBSDTargetInfo<ARMTargetInfo>(T);
1566    default:
1567      return new ARMTargetInfo(T);
1568    }
1569
1570  case llvm::Triple::bfin:
1571    return new BlackfinTargetInfo(T);
1572
1573  case llvm::Triple::msp430:
1574    return new MSP430TargetInfo(T);
1575
1576  case llvm::Triple::pic16:
1577    return new PIC16TargetInfo(T);
1578
1579  case llvm::Triple::ppc:
1580    if (os == llvm::Triple::Darwin)
1581      return new DarwinTargetInfo<PPCTargetInfo>(T);
1582    return new PPC32TargetInfo(T);
1583
1584  case llvm::Triple::ppc64:
1585    if (os == llvm::Triple::Darwin)
1586      return new DarwinTargetInfo<PPC64TargetInfo>(T);
1587    return new PPC64TargetInfo(T);
1588
1589  case llvm::Triple::sparc:
1590    if (os == llvm::Triple::Solaris)
1591      return new SolarisSparcV8TargetInfo(T);
1592    return new SparcV8TargetInfo(T);
1593
1594  case llvm::Triple::systemz:
1595    return new SystemZTargetInfo(T);
1596
1597  case llvm::Triple::tce:
1598    return new TCETargetInfo(T);
1599
1600  case llvm::Triple::x86:
1601    switch (os) {
1602    case llvm::Triple::Darwin:
1603      return new DarwinI386TargetInfo(T);
1604    case llvm::Triple::Linux:
1605      return new LinuxTargetInfo<X86_32TargetInfo>(T);
1606    case llvm::Triple::DragonFly:
1607      return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
1608    case llvm::Triple::NetBSD:
1609      return new NetBSDTargetInfo<X86_32TargetInfo>(T);
1610    case llvm::Triple::OpenBSD:
1611      return new OpenBSDI386TargetInfo(T);
1612    case llvm::Triple::FreeBSD:
1613      return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
1614    case llvm::Triple::Solaris:
1615      return new SolarisTargetInfo<X86_32TargetInfo>(T);
1616    case llvm::Triple::Cygwin:
1617    case llvm::Triple::MinGW32:
1618    case llvm::Triple::MinGW64:
1619    case llvm::Triple::Win32:
1620      return new WindowsX86_32TargetInfo(T);
1621    default:
1622      return new X86_32TargetInfo(T);
1623    }
1624
1625  case llvm::Triple::x86_64:
1626    switch (os) {
1627    case llvm::Triple::Darwin:
1628      return new DarwinX86_64TargetInfo(T);
1629    case llvm::Triple::Linux:
1630      return new LinuxTargetInfo<X86_64TargetInfo>(T);
1631    case llvm::Triple::NetBSD:
1632      return new NetBSDTargetInfo<X86_64TargetInfo>(T);
1633    case llvm::Triple::OpenBSD:
1634      return new OpenBSDX86_64TargetInfo(T);
1635    case llvm::Triple::FreeBSD:
1636      return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
1637    case llvm::Triple::Solaris:
1638      return new SolarisTargetInfo<X86_64TargetInfo>(T);
1639    default:
1640      return new X86_64TargetInfo(T);
1641    }
1642  }
1643}
1644