Targets.cpp revision 39d5fa131ff0f2de0f04b16dca086c51102cb1bf
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/TargetInfo.h"
16#include "clang/Basic/Builtins.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/MacroBuilder.h"
20#include "clang/Basic/TargetBuiltins.h"
21#include "clang/Basic/TargetOptions.h"
22#include "llvm/ADT/APFloat.h"
23#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/StringSwitch.h"
27#include "llvm/ADT/Triple.h"
28#include "llvm/MC/MCSectionMachO.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Type.h"
31#include <algorithm>
32using namespace clang;
33
34//===----------------------------------------------------------------------===//
35//  Common code shared among targets.
36//===----------------------------------------------------------------------===//
37
38/// DefineStd - Define a macro name and standard variants.  For example if
39/// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
40/// when in GNU mode.
41static void DefineStd(MacroBuilder &Builder, StringRef MacroName,
42                      const LangOptions &Opts) {
43  assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
44
45  // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
46  // in the user's namespace.
47  if (Opts.GNUMode)
48    Builder.defineMacro(MacroName);
49
50  // Define __unix.
51  Builder.defineMacro("__" + MacroName);
52
53  // Define __unix__.
54  Builder.defineMacro("__" + MacroName + "__");
55}
56
57static void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName,
58                            bool Tuning = true) {
59  Builder.defineMacro("__" + CPUName);
60  Builder.defineMacro("__" + CPUName + "__");
61  if (Tuning)
62    Builder.defineMacro("__tune_" + CPUName + "__");
63}
64
65//===----------------------------------------------------------------------===//
66// Defines specific to certain operating systems.
67//===----------------------------------------------------------------------===//
68
69namespace {
70template<typename TgtInfo>
71class OSTargetInfo : public TgtInfo {
72protected:
73  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
74                            MacroBuilder &Builder) const=0;
75public:
76  OSTargetInfo(const std::string& triple) : TgtInfo(triple) {}
77  virtual void getTargetDefines(const LangOptions &Opts,
78                                MacroBuilder &Builder) const {
79    TgtInfo::getTargetDefines(Opts, Builder);
80    getOSDefines(Opts, TgtInfo::getTriple(), Builder);
81  }
82
83};
84} // end anonymous namespace
85
86
87static void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
88                             const llvm::Triple &Triple,
89                             StringRef &PlatformName,
90                             VersionTuple &PlatformMinVersion) {
91  Builder.defineMacro("__APPLE_CC__", "5621");
92  Builder.defineMacro("__APPLE__");
93  Builder.defineMacro("__MACH__");
94  Builder.defineMacro("OBJC_NEW_PROPERTIES");
95
96  if (!Opts.ObjCAutoRefCount) {
97    // __weak is always defined, for use in blocks and with objc pointers.
98    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
99
100    // Darwin defines __strong even in C mode (just to nothing).
101    if (Opts.getGC() != LangOptions::NonGC)
102      Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
103    else
104      Builder.defineMacro("__strong", "");
105
106    // __unsafe_unretained is defined to nothing in non-ARC mode. We even
107    // allow this in C, since one might have block pointers in structs that
108    // are used in pure C code and in Objective-C ARC.
109    Builder.defineMacro("__unsafe_unretained", "");
110  }
111
112  if (Opts.Static)
113    Builder.defineMacro("__STATIC__");
114  else
115    Builder.defineMacro("__DYNAMIC__");
116
117  if (Opts.POSIXThreads)
118    Builder.defineMacro("_REENTRANT");
119
120  // Get the platform type and version number from the triple.
121  unsigned Maj, Min, Rev;
122  if (Triple.isMacOSX()) {
123    Triple.getMacOSXVersion(Maj, Min, Rev);
124    PlatformName = "macosx";
125  } else {
126    Triple.getOSVersion(Maj, Min, Rev);
127    PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
128  }
129
130  // If -target arch-pc-win32-macho option specified, we're
131  // generating code for Win32 ABI. No need to emit
132  // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
133  if (PlatformName == "win32") {
134    PlatformMinVersion = VersionTuple(Maj, Min, Rev);
135    return;
136  }
137
138  // Set the appropriate OS version define.
139  if (Triple.getOS() == llvm::Triple::IOS) {
140    assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
141    char Str[6];
142    Str[0] = '0' + Maj;
143    Str[1] = '0' + (Min / 10);
144    Str[2] = '0' + (Min % 10);
145    Str[3] = '0' + (Rev / 10);
146    Str[4] = '0' + (Rev % 10);
147    Str[5] = '\0';
148    Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__", Str);
149  } else {
150    // Note that the Driver allows versions which aren't representable in the
151    // define (because we only get a single digit for the minor and micro
152    // revision numbers). So, we limit them to the maximum representable
153    // version.
154    assert(Triple.getEnvironmentName().empty() && "Invalid environment!");
155    assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
156    char Str[5];
157    Str[0] = '0' + (Maj / 10);
158    Str[1] = '0' + (Maj % 10);
159    Str[2] = '0' + std::min(Min, 9U);
160    Str[3] = '0' + std::min(Rev, 9U);
161    Str[4] = '\0';
162    Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
163  }
164
165  PlatformMinVersion = VersionTuple(Maj, Min, Rev);
166}
167
168namespace {
169template<typename Target>
170class DarwinTargetInfo : public OSTargetInfo<Target> {
171protected:
172  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
173                            MacroBuilder &Builder) const {
174    getDarwinDefines(Builder, Opts, Triple, this->PlatformName,
175                     this->PlatformMinVersion);
176  }
177
178public:
179  DarwinTargetInfo(const std::string& triple) :
180    OSTargetInfo<Target>(triple) {
181      llvm::Triple T = llvm::Triple(triple);
182      this->TLSSupported = T.isMacOSX() && !T.isMacOSXVersionLT(10,7);
183      this->MCountName = "\01mcount";
184    }
185
186  virtual std::string isValidSectionSpecifier(StringRef SR) const {
187    // Let MCSectionMachO validate this.
188    StringRef Segment, Section;
189    unsigned TAA, StubSize;
190    bool HasTAA;
191    return llvm::MCSectionMachO::ParseSectionSpecifier(SR, Segment, Section,
192                                                       TAA, HasTAA, StubSize);
193  }
194
195  virtual const char *getStaticInitSectionSpecifier() const {
196    // FIXME: We should return 0 when building kexts.
197    return "__TEXT,__StaticInit,regular,pure_instructions";
198  }
199
200  /// Darwin does not support protected visibility.  Darwin's "default"
201  /// is very similar to ELF's "protected";  Darwin requires a "weak"
202  /// attribute on declarations that can be dynamically replaced.
203  virtual bool hasProtectedVisibility() const {
204    return false;
205  }
206};
207
208
209// DragonFlyBSD Target
210template<typename Target>
211class DragonFlyBSDTargetInfo : public OSTargetInfo<Target> {
212protected:
213  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
214                            MacroBuilder &Builder) const {
215    // DragonFly defines; list based off of gcc output
216    Builder.defineMacro("__DragonFly__");
217    Builder.defineMacro("__DragonFly_cc_version", "100001");
218    Builder.defineMacro("__ELF__");
219    Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
220    Builder.defineMacro("__tune_i386__");
221    DefineStd(Builder, "unix", Opts);
222  }
223public:
224  DragonFlyBSDTargetInfo(const std::string &triple)
225    : OSTargetInfo<Target>(triple) {
226      this->UserLabelPrefix = "";
227
228      llvm::Triple Triple(triple);
229      switch (Triple.getArch()) {
230        default:
231        case llvm::Triple::x86:
232        case llvm::Triple::x86_64:
233          this->MCountName = ".mcount";
234          break;
235      }
236  }
237};
238
239// FreeBSD Target
240template<typename Target>
241class FreeBSDTargetInfo : public OSTargetInfo<Target> {
242protected:
243  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
244                            MacroBuilder &Builder) const {
245    // FreeBSD defines; list based off of gcc output
246
247    unsigned Release = Triple.getOSMajorVersion();
248    if (Release == 0U)
249      Release = 8;
250
251    Builder.defineMacro("__FreeBSD__", Twine(Release));
252    Builder.defineMacro("__FreeBSD_cc_version", Twine(Release * 100000U + 1U));
253    Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
254    DefineStd(Builder, "unix", Opts);
255    Builder.defineMacro("__ELF__");
256  }
257public:
258  FreeBSDTargetInfo(const std::string &triple)
259    : OSTargetInfo<Target>(triple) {
260      this->UserLabelPrefix = "";
261
262      llvm::Triple Triple(triple);
263      switch (Triple.getArch()) {
264        default:
265        case llvm::Triple::x86:
266        case llvm::Triple::x86_64:
267          this->MCountName = ".mcount";
268          break;
269        case llvm::Triple::mips:
270        case llvm::Triple::mipsel:
271        case llvm::Triple::ppc:
272        case llvm::Triple::ppc64:
273          this->MCountName = "_mcount";
274          break;
275        case llvm::Triple::arm:
276          this->MCountName = "__mcount";
277          break;
278      }
279
280    }
281};
282
283// Minix Target
284template<typename Target>
285class MinixTargetInfo : public OSTargetInfo<Target> {
286protected:
287  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
288                            MacroBuilder &Builder) const {
289    // Minix defines
290
291    Builder.defineMacro("__minix", "3");
292    Builder.defineMacro("_EM_WSIZE", "4");
293    Builder.defineMacro("_EM_PSIZE", "4");
294    Builder.defineMacro("_EM_SSIZE", "2");
295    Builder.defineMacro("_EM_LSIZE", "4");
296    Builder.defineMacro("_EM_FSIZE", "4");
297    Builder.defineMacro("_EM_DSIZE", "8");
298    Builder.defineMacro("__ELF__");
299    DefineStd(Builder, "unix", Opts);
300  }
301public:
302  MinixTargetInfo(const std::string &triple)
303    : OSTargetInfo<Target>(triple) {
304      this->UserLabelPrefix = "";
305    }
306};
307
308// Linux target
309template<typename Target>
310class LinuxTargetInfo : public OSTargetInfo<Target> {
311protected:
312  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
313                            MacroBuilder &Builder) const {
314    // Linux defines; list based off of gcc output
315    DefineStd(Builder, "unix", Opts);
316    DefineStd(Builder, "linux", Opts);
317    Builder.defineMacro("__gnu_linux__");
318    Builder.defineMacro("__ELF__");
319    if (Triple.getEnvironment() == llvm::Triple::ANDROIDEABI)
320      Builder.defineMacro("__ANDROID__", "1");
321    if (Opts.POSIXThreads)
322      Builder.defineMacro("_REENTRANT");
323    if (Opts.CPlusPlus)
324      Builder.defineMacro("_GNU_SOURCE");
325  }
326public:
327  LinuxTargetInfo(const std::string& triple)
328    : OSTargetInfo<Target>(triple) {
329    this->UserLabelPrefix = "";
330    this->WIntType = TargetInfo::UnsignedInt;
331  }
332
333  virtual const char *getStaticInitSectionSpecifier() const {
334    return ".text.startup";
335  }
336};
337
338// NetBSD Target
339template<typename Target>
340class NetBSDTargetInfo : public OSTargetInfo<Target> {
341protected:
342  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
343                            MacroBuilder &Builder) const {
344    // NetBSD defines; list based off of gcc output
345    Builder.defineMacro("__NetBSD__");
346    Builder.defineMacro("__unix__");
347    Builder.defineMacro("__ELF__");
348    if (Opts.POSIXThreads)
349      Builder.defineMacro("_POSIX_THREADS");
350  }
351public:
352  NetBSDTargetInfo(const std::string &triple)
353    : OSTargetInfo<Target>(triple) {
354      this->UserLabelPrefix = "";
355    }
356};
357
358// OpenBSD Target
359template<typename Target>
360class OpenBSDTargetInfo : public OSTargetInfo<Target> {
361protected:
362  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
363                            MacroBuilder &Builder) const {
364    // OpenBSD defines; list based off of gcc output
365
366    Builder.defineMacro("__OpenBSD__");
367    DefineStd(Builder, "unix", Opts);
368    Builder.defineMacro("__ELF__");
369    if (Opts.POSIXThreads)
370      Builder.defineMacro("_REENTRANT");
371  }
372public:
373  OpenBSDTargetInfo(const std::string &triple)
374    : OSTargetInfo<Target>(triple) {
375      this->UserLabelPrefix = "";
376
377      llvm::Triple Triple(triple);
378      switch (Triple.getArch()) {
379        default:
380        case llvm::Triple::x86:
381        case llvm::Triple::x86_64:
382        case llvm::Triple::arm:
383	case llvm::Triple::sparc:
384          this->MCountName = "__mcount";
385          break;
386        case llvm::Triple::mips64:
387        case llvm::Triple::mips64el:
388        case llvm::Triple::ppc:
389	case llvm::Triple::sparcv9:
390          this->MCountName = "_mcount";
391          break;
392      }
393  }
394};
395
396// PSP Target
397template<typename Target>
398class PSPTargetInfo : public OSTargetInfo<Target> {
399protected:
400  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
401                            MacroBuilder &Builder) const {
402    // PSP defines; list based on the output of the pspdev gcc toolchain.
403    Builder.defineMacro("PSP");
404    Builder.defineMacro("_PSP");
405    Builder.defineMacro("__psp__");
406    Builder.defineMacro("__ELF__");
407  }
408public:
409  PSPTargetInfo(const std::string& triple)
410    : OSTargetInfo<Target>(triple) {
411    this->UserLabelPrefix = "";
412  }
413};
414
415// PS3 PPU Target
416template<typename Target>
417class PS3PPUTargetInfo : public OSTargetInfo<Target> {
418protected:
419  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
420                            MacroBuilder &Builder) const {
421    // PS3 PPU defines.
422    Builder.defineMacro("__PPC__");
423    Builder.defineMacro("__PPU__");
424    Builder.defineMacro("__CELLOS_LV2__");
425    Builder.defineMacro("__ELF__");
426    Builder.defineMacro("__LP32__");
427    Builder.defineMacro("_ARCH_PPC64");
428    Builder.defineMacro("__powerpc64__");
429  }
430public:
431  PS3PPUTargetInfo(const std::string& triple)
432    : OSTargetInfo<Target>(triple) {
433    this->UserLabelPrefix = "";
434    this->LongWidth = this->LongAlign = 32;
435    this->PointerWidth = this->PointerAlign = 32;
436    this->IntMaxType = TargetInfo::SignedLongLong;
437    this->UIntMaxType = TargetInfo::UnsignedLongLong;
438    this->Int64Type = TargetInfo::SignedLongLong;
439    this->SizeType = TargetInfo::UnsignedInt;
440    this->DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
441                              "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
442  }
443};
444
445// FIXME: Need a real SPU target.
446// PS3 SPU Target
447template<typename Target>
448class PS3SPUTargetInfo : public OSTargetInfo<Target> {
449protected:
450  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
451                            MacroBuilder &Builder) const {
452    // PS3 PPU defines.
453    Builder.defineMacro("__SPU__");
454    Builder.defineMacro("__ELF__");
455  }
456public:
457  PS3SPUTargetInfo(const std::string& triple)
458    : OSTargetInfo<Target>(triple) {
459    this->UserLabelPrefix = "";
460  }
461};
462
463// AuroraUX target
464template<typename Target>
465class AuroraUXTargetInfo : public OSTargetInfo<Target> {
466protected:
467  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
468                            MacroBuilder &Builder) const {
469    DefineStd(Builder, "sun", Opts);
470    DefineStd(Builder, "unix", Opts);
471    Builder.defineMacro("__ELF__");
472    Builder.defineMacro("__svr4__");
473    Builder.defineMacro("__SVR4");
474  }
475public:
476  AuroraUXTargetInfo(const std::string& triple)
477    : OSTargetInfo<Target>(triple) {
478    this->UserLabelPrefix = "";
479    this->WCharType = this->SignedLong;
480    // FIXME: WIntType should be SignedLong
481  }
482};
483
484// Solaris target
485template<typename Target>
486class SolarisTargetInfo : public OSTargetInfo<Target> {
487protected:
488  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
489                            MacroBuilder &Builder) const {
490    DefineStd(Builder, "sun", Opts);
491    DefineStd(Builder, "unix", Opts);
492    Builder.defineMacro("__ELF__");
493    Builder.defineMacro("__svr4__");
494    Builder.defineMacro("__SVR4");
495    // Solaris headers require _XOPEN_SOURCE to be set to 600 for C99 and
496    // newer, but to 500 for everything else.  feature_test.h has a check to
497    // ensure that you are not using C99 with an old version of X/Open or C89
498    // with a new version.
499    if (Opts.C99 || Opts.C11)
500      Builder.defineMacro("_XOPEN_SOURCE", "600");
501    else
502      Builder.defineMacro("_XOPEN_SOURCE", "500");
503    if (Opts.CPlusPlus)
504      Builder.defineMacro("__C99FEATURES__");
505    Builder.defineMacro("_LARGEFILE_SOURCE");
506    Builder.defineMacro("_LARGEFILE64_SOURCE");
507    Builder.defineMacro("__EXTENSIONS__");
508    Builder.defineMacro("_REENTRANT");
509  }
510public:
511  SolarisTargetInfo(const std::string& triple)
512    : OSTargetInfo<Target>(triple) {
513    this->UserLabelPrefix = "";
514    this->WCharType = this->SignedInt;
515    // FIXME: WIntType should be SignedLong
516  }
517};
518
519// Windows target
520template<typename Target>
521class WindowsTargetInfo : public OSTargetInfo<Target> {
522protected:
523  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
524                            MacroBuilder &Builder) const {
525    Builder.defineMacro("_WIN32");
526  }
527  void getVisualStudioDefines(const LangOptions &Opts,
528                              MacroBuilder &Builder) const {
529    if (Opts.CPlusPlus) {
530      if (Opts.RTTI)
531        Builder.defineMacro("_CPPRTTI");
532
533      if (Opts.Exceptions)
534        Builder.defineMacro("_CPPUNWIND");
535    }
536
537    if (!Opts.CharIsSigned)
538      Builder.defineMacro("_CHAR_UNSIGNED");
539
540    // FIXME: POSIXThreads isn't exactly the option this should be defined for,
541    //        but it works for now.
542    if (Opts.POSIXThreads)
543      Builder.defineMacro("_MT");
544
545    if (Opts.MSCVersion != 0)
546      Builder.defineMacro("_MSC_VER", Twine(Opts.MSCVersion));
547
548    if (Opts.MicrosoftExt) {
549      Builder.defineMacro("_MSC_EXTENSIONS");
550
551      if (Opts.CPlusPlus0x) {
552        Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
553        Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
554        Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
555      }
556    }
557
558    Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
559  }
560
561public:
562  WindowsTargetInfo(const std::string &triple)
563    : OSTargetInfo<Target>(triple) {}
564};
565
566} // end anonymous namespace.
567
568//===----------------------------------------------------------------------===//
569// Specific target implementations.
570//===----------------------------------------------------------------------===//
571
572namespace {
573// PPC abstract base class
574class PPCTargetInfo : public TargetInfo {
575  static const Builtin::Info BuiltinInfo[];
576  static const char * const GCCRegNames[];
577  static const TargetInfo::GCCRegAlias GCCRegAliases[];
578  std::string CPU;
579public:
580  PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {
581    LongDoubleWidth = LongDoubleAlign = 128;
582    LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble;
583  }
584
585  /// \brief Flags for architecture specific defines.
586  typedef enum {
587    ArchDefineNone  = 0,
588    ArchDefineName  = 1 << 0, // <name> is substituted for arch name.
589    ArchDefinePpcgr = 1 << 1,
590    ArchDefinePpcsq = 1 << 2,
591    ArchDefine440   = 1 << 3,
592    ArchDefine603   = 1 << 4,
593    ArchDefine604   = 1 << 5,
594    ArchDefinePwr4  = 1 << 6,
595    ArchDefinePwr6  = 1 << 7
596  } ArchDefineTypes;
597
598  virtual bool setCPU(const std::string &Name) {
599    bool CPUKnown = llvm::StringSwitch<bool>(Name)
600      .Case("generic", true)
601      .Case("440", true)
602      .Case("450", true)
603      .Case("601", true)
604      .Case("602", true)
605      .Case("603", true)
606      .Case("603e", true)
607      .Case("603ev", true)
608      .Case("604", true)
609      .Case("604e", true)
610      .Case("620", true)
611      .Case("g3", true)
612      .Case("7400", true)
613      .Case("g4", true)
614      .Case("7450", true)
615      .Case("g4+", true)
616      .Case("750", true)
617      .Case("970", true)
618      .Case("g5", true)
619      .Case("a2", true)
620      .Case("pwr6", true)
621      .Case("pwr7", true)
622      .Case("ppc", true)
623      .Case("ppc64", true)
624      .Default(false);
625
626    if (CPUKnown)
627      CPU = Name;
628
629    return CPUKnown;
630  }
631
632  virtual void getTargetBuiltins(const Builtin::Info *&Records,
633                                 unsigned &NumRecords) const {
634    Records = BuiltinInfo;
635    NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
636  }
637
638  virtual bool isCLZForZeroUndef() const { return false; }
639
640  virtual void getTargetDefines(const LangOptions &Opts,
641                                MacroBuilder &Builder) const;
642
643  virtual bool hasFeature(StringRef Feature) const;
644
645  virtual void getGCCRegNames(const char * const *&Names,
646                              unsigned &NumNames) const;
647  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
648                                unsigned &NumAliases) const;
649  virtual bool validateAsmConstraint(const char *&Name,
650                                     TargetInfo::ConstraintInfo &Info) const {
651    switch (*Name) {
652    default: return false;
653    case 'O': // Zero
654      break;
655    case 'b': // Base register
656    case 'f': // Floating point register
657      Info.setAllowsRegister();
658      break;
659    // FIXME: The following are added to allow parsing.
660    // I just took a guess at what the actions should be.
661    // Also, is more specific checking needed?  I.e. specific registers?
662    case 'd': // Floating point register (containing 64-bit value)
663    case 'v': // Altivec vector register
664      Info.setAllowsRegister();
665      break;
666    case 'w':
667      switch (Name[1]) {
668        case 'd':// VSX vector register to hold vector double data
669        case 'f':// VSX vector register to hold vector float data
670        case 's':// VSX vector register to hold scalar float data
671        case 'a':// Any VSX register
672          break;
673        default:
674          return false;
675      }
676      Info.setAllowsRegister();
677      Name++; // Skip over 'w'.
678      break;
679    case 'h': // `MQ', `CTR', or `LINK' register
680    case 'q': // `MQ' register
681    case 'c': // `CTR' register
682    case 'l': // `LINK' register
683    case 'x': // `CR' register (condition register) number 0
684    case 'y': // `CR' register (condition register)
685    case 'z': // `XER[CA]' carry bit (part of the XER register)
686      Info.setAllowsRegister();
687      break;
688    case 'I': // Signed 16-bit constant
689    case 'J': // Unsigned 16-bit constant shifted left 16 bits
690              //  (use `L' instead for SImode constants)
691    case 'K': // Unsigned 16-bit constant
692    case 'L': // Signed 16-bit constant shifted left 16 bits
693    case 'M': // Constant larger than 31
694    case 'N': // Exact power of 2
695    case 'P': // Constant whose negation is a signed 16-bit constant
696    case 'G': // Floating point constant that can be loaded into a
697              // register with one instruction per word
698    case 'H': // Integer/Floating point constant that can be loaded
699              // into a register using three instructions
700      break;
701    case 'm': // Memory operand. Note that on PowerPC targets, m can
702              // include addresses that update the base register. It
703              // is therefore only safe to use `m' in an asm statement
704              // if that asm statement accesses the operand exactly once.
705              // The asm statement must also use `%U<opno>' as a
706              // placeholder for the "update" flag in the corresponding
707              // load or store instruction. For example:
708              // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
709              // is correct but:
710              // asm ("st %1,%0" : "=m" (mem) : "r" (val));
711              // is not. Use es rather than m if you don't want the base
712              // register to be updated.
713    case 'e':
714      if (Name[1] != 's')
715          return false;
716              // es: A "stable" memory operand; that is, one which does not
717              // include any automodification of the base register. Unlike
718              // `m', this constraint can be used in asm statements that
719              // might access the operand several times, or that might not
720              // access it at all.
721      Info.setAllowsMemory();
722      Name++; // Skip over 'e'.
723      break;
724    case 'Q': // Memory operand that is an offset from a register (it is
725              // usually better to use `m' or `es' in asm statements)
726    case 'Z': // Memory operand that is an indexed or indirect from a
727              // register (it is usually better to use `m' or `es' in
728              // asm statements)
729      Info.setAllowsMemory();
730      Info.setAllowsRegister();
731      break;
732    case 'R': // AIX TOC entry
733    case 'a': // Address operand that is an indexed or indirect from a
734              // register (`p' is preferable for asm statements)
735    case 'S': // Constant suitable as a 64-bit mask operand
736    case 'T': // Constant suitable as a 32-bit mask operand
737    case 'U': // System V Release 4 small data area reference
738    case 't': // AND masks that can be performed by two rldic{l, r}
739              // instructions
740    case 'W': // Vector constant that does not require memory
741    case 'j': // Vector constant that is all zeros.
742      break;
743    // End FIXME.
744    }
745    return true;
746  }
747  virtual const char *getClobbers() const {
748    return "";
749  }
750};
751
752const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
753#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
754#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
755                                              ALL_LANGUAGES },
756#include "clang/Basic/BuiltinsPPC.def"
757};
758
759
760/// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
761/// #defines that are not tied to a specific subtarget.
762void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
763                                     MacroBuilder &Builder) const {
764  // Target identification.
765  Builder.defineMacro("__ppc__");
766  Builder.defineMacro("_ARCH_PPC");
767  Builder.defineMacro("__powerpc__");
768  Builder.defineMacro("__POWERPC__");
769  if (PointerWidth == 64) {
770    Builder.defineMacro("_ARCH_PPC64");
771    Builder.defineMacro("_LP64");
772    Builder.defineMacro("__LP64__");
773    Builder.defineMacro("__powerpc64__");
774    Builder.defineMacro("__ppc64__");
775  } else {
776    Builder.defineMacro("__ppc__");
777  }
778
779  // Target properties.
780  if (getTriple().getOS() != llvm::Triple::NetBSD)
781    Builder.defineMacro("_BIG_ENDIAN");
782  Builder.defineMacro("__BIG_ENDIAN__");
783
784  // Subtarget options.
785  Builder.defineMacro("__NATURAL_ALIGNMENT__");
786  Builder.defineMacro("__REGISTER_PREFIX__", "");
787
788  // FIXME: Should be controlled by command line option.
789  Builder.defineMacro("__LONG_DOUBLE_128__");
790
791  if (Opts.AltiVec) {
792    Builder.defineMacro("__VEC__", "10206");
793    Builder.defineMacro("__ALTIVEC__");
794  }
795
796  // CPU identification.
797  ArchDefineTypes defs = (ArchDefineTypes)llvm::StringSwitch<int>(CPU)
798    .Case("440",   ArchDefineName)
799    .Case("450",   ArchDefineName | ArchDefine440)
800    .Case("601",   ArchDefineName)
801    .Case("602",   ArchDefineName | ArchDefinePpcgr)
802    .Case("603",   ArchDefineName | ArchDefinePpcgr)
803    .Case("603e",  ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
804    .Case("603ev", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
805    .Case("604",   ArchDefineName | ArchDefinePpcgr)
806    .Case("604e",  ArchDefineName | ArchDefine604 | ArchDefinePpcgr)
807    .Case("620",   ArchDefineName | ArchDefinePpcgr)
808    .Case("7400",  ArchDefineName | ArchDefinePpcgr)
809    .Case("7450",  ArchDefineName | ArchDefinePpcgr)
810    .Case("750",   ArchDefineName | ArchDefinePpcgr)
811    .Case("970",   ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr
812                     | ArchDefinePpcsq)
813    .Case("pwr6",  ArchDefinePwr6 | ArchDefinePpcgr | ArchDefinePpcsq)
814    .Case("pwr7",  ArchDefineName | ArchDefinePwr6 | ArchDefinePpcgr
815                     | ArchDefinePpcsq)
816    .Default(ArchDefineNone);
817
818  if (defs & ArchDefineName)
819    Builder.defineMacro(Twine("_ARCH_", StringRef(CPU).upper()));
820  if (defs & ArchDefinePpcgr)
821    Builder.defineMacro("_ARCH_PPCGR");
822  if (defs & ArchDefinePpcsq)
823    Builder.defineMacro("_ARCH_PPCSQ");
824  if (defs & ArchDefine440)
825    Builder.defineMacro("_ARCH_440");
826  if (defs & ArchDefine603)
827    Builder.defineMacro("_ARCH_603");
828  if (defs & ArchDefine604)
829    Builder.defineMacro("_ARCH_604");
830  if (defs & (ArchDefinePwr4 | ArchDefinePwr6))
831    Builder.defineMacro("_ARCH_PWR4");
832  if (defs & ArchDefinePwr6) {
833    Builder.defineMacro("_ARCH_PWR5");
834    Builder.defineMacro("_ARCH_PWR6");
835  }
836}
837
838bool PPCTargetInfo::hasFeature(StringRef Feature) const {
839  return Feature == "powerpc";
840}
841
842
843const char * const PPCTargetInfo::GCCRegNames[] = {
844  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
845  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
846  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
847  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
848  "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
849  "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
850  "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
851  "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
852  "mq", "lr", "ctr", "ap",
853  "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
854  "xer",
855  "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
856  "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
857  "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
858  "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
859  "vrsave", "vscr",
860  "spe_acc", "spefscr",
861  "sfp"
862};
863
864void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
865                                   unsigned &NumNames) const {
866  Names = GCCRegNames;
867  NumNames = llvm::array_lengthof(GCCRegNames);
868}
869
870const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
871  // While some of these aliases do map to different registers
872  // they still share the same register name.
873  { { "0" }, "r0" },
874  { { "1"}, "r1" },
875  { { "2" }, "r2" },
876  { { "3" }, "r3" },
877  { { "4" }, "r4" },
878  { { "5" }, "r5" },
879  { { "6" }, "r6" },
880  { { "7" }, "r7" },
881  { { "8" }, "r8" },
882  { { "9" }, "r9" },
883  { { "10" }, "r10" },
884  { { "11" }, "r11" },
885  { { "12" }, "r12" },
886  { { "13" }, "r13" },
887  { { "14" }, "r14" },
888  { { "15" }, "r15" },
889  { { "16" }, "r16" },
890  { { "17" }, "r17" },
891  { { "18" }, "r18" },
892  { { "19" }, "r19" },
893  { { "20" }, "r20" },
894  { { "21" }, "r21" },
895  { { "22" }, "r22" },
896  { { "23" }, "r23" },
897  { { "24" }, "r24" },
898  { { "25" }, "r25" },
899  { { "26" }, "r26" },
900  { { "27" }, "r27" },
901  { { "28" }, "r28" },
902  { { "29" }, "r29" },
903  { { "30" }, "r30" },
904  { { "31" }, "r31" },
905  { { "fr0" }, "f0" },
906  { { "fr1" }, "f1" },
907  { { "fr2" }, "f2" },
908  { { "fr3" }, "f3" },
909  { { "fr4" }, "f4" },
910  { { "fr5" }, "f5" },
911  { { "fr6" }, "f6" },
912  { { "fr7" }, "f7" },
913  { { "fr8" }, "f8" },
914  { { "fr9" }, "f9" },
915  { { "fr10" }, "f10" },
916  { { "fr11" }, "f11" },
917  { { "fr12" }, "f12" },
918  { { "fr13" }, "f13" },
919  { { "fr14" }, "f14" },
920  { { "fr15" }, "f15" },
921  { { "fr16" }, "f16" },
922  { { "fr17" }, "f17" },
923  { { "fr18" }, "f18" },
924  { { "fr19" }, "f19" },
925  { { "fr20" }, "f20" },
926  { { "fr21" }, "f21" },
927  { { "fr22" }, "f22" },
928  { { "fr23" }, "f23" },
929  { { "fr24" }, "f24" },
930  { { "fr25" }, "f25" },
931  { { "fr26" }, "f26" },
932  { { "fr27" }, "f27" },
933  { { "fr28" }, "f28" },
934  { { "fr29" }, "f29" },
935  { { "fr30" }, "f30" },
936  { { "fr31" }, "f31" },
937  { { "cc" }, "cr0" },
938};
939
940void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
941                                     unsigned &NumAliases) const {
942  Aliases = GCCRegAliases;
943  NumAliases = llvm::array_lengthof(GCCRegAliases);
944}
945} // end anonymous namespace.
946
947namespace {
948class PPC32TargetInfo : public PPCTargetInfo {
949public:
950  PPC32TargetInfo(const std::string &triple) : PPCTargetInfo(triple) {
951    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
952                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
953
954    switch (getTriple().getOS()) {
955    case llvm::Triple::Linux:
956    case llvm::Triple::FreeBSD:
957    case llvm::Triple::NetBSD:
958      SizeType = UnsignedInt;
959      PtrDiffType = SignedInt;
960      IntPtrType = SignedInt;
961      break;
962    default:
963      break;
964    }
965
966    if (getTriple().getOS() == llvm::Triple::FreeBSD) {
967      LongDoubleWidth = LongDoubleAlign = 64;
968      LongDoubleFormat = &llvm::APFloat::IEEEdouble;
969    }
970  }
971
972  virtual BuiltinVaListKind getBuiltinVaListKind() const {
973    // This is the ELF definition, and is overridden by the Darwin sub-target
974    return TargetInfo::PowerABIBuiltinVaList;
975  }
976};
977} // end anonymous namespace.
978
979namespace {
980class PPC64TargetInfo : public PPCTargetInfo {
981public:
982  PPC64TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
983    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
984    IntMaxType = SignedLong;
985    UIntMaxType = UnsignedLong;
986    Int64Type = SignedLong;
987    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
988                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32:64";
989
990    if (getTriple().getOS() == llvm::Triple::FreeBSD) {
991      LongDoubleWidth = LongDoubleAlign = 64;
992      LongDoubleFormat = &llvm::APFloat::IEEEdouble;
993    }
994  }
995  virtual BuiltinVaListKind getBuiltinVaListKind() const {
996    return TargetInfo::CharPtrBuiltinVaList;
997  }
998};
999} // end anonymous namespace.
1000
1001
1002namespace {
1003class DarwinPPC32TargetInfo :
1004  public DarwinTargetInfo<PPC32TargetInfo> {
1005public:
1006  DarwinPPC32TargetInfo(const std::string& triple)
1007    : DarwinTargetInfo<PPC32TargetInfo>(triple) {
1008    HasAlignMac68kSupport = true;
1009    BoolWidth = BoolAlign = 32; //XXX support -mone-byte-bool?
1010    LongLongAlign = 32;
1011    SuitableAlign = 128;
1012    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1013                        "i64:32:64-f32:32:32-f64:64:64-v128:128:128-n32";
1014  }
1015  virtual BuiltinVaListKind getBuiltinVaListKind() const {
1016    return TargetInfo::CharPtrBuiltinVaList;
1017  }
1018};
1019
1020class DarwinPPC64TargetInfo :
1021  public DarwinTargetInfo<PPC64TargetInfo> {
1022public:
1023  DarwinPPC64TargetInfo(const std::string& triple)
1024    : DarwinTargetInfo<PPC64TargetInfo>(triple) {
1025    HasAlignMac68kSupport = true;
1026    SuitableAlign = 128;
1027  }
1028};
1029} // end anonymous namespace.
1030
1031namespace {
1032  static const unsigned NVPTXAddrSpaceMap[] = {
1033    1,    // opencl_global
1034    3,    // opencl_local
1035    4,    // opencl_constant
1036    1,    // cuda_device
1037    4,    // cuda_constant
1038    3,    // cuda_shared
1039  };
1040  class NVPTXTargetInfo : public TargetInfo {
1041    static const char * const GCCRegNames[];
1042    static const Builtin::Info BuiltinInfo[];
1043    std::vector<llvm::StringRef> AvailableFeatures;
1044  public:
1045    NVPTXTargetInfo(const std::string& triple) : TargetInfo(triple) {
1046      BigEndian = false;
1047      TLSSupported = false;
1048      LongWidth = LongAlign = 64;
1049      AddrSpaceMap = &NVPTXAddrSpaceMap;
1050      // Define available target features
1051      // These must be defined in sorted order!
1052    }
1053    virtual void getTargetDefines(const LangOptions &Opts,
1054                                  MacroBuilder &Builder) const {
1055      Builder.defineMacro("__PTX__");
1056      Builder.defineMacro("__NVPTX__");
1057    }
1058    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1059                                   unsigned &NumRecords) const {
1060      Records = BuiltinInfo;
1061      NumRecords = clang::NVPTX::LastTSBuiltin-Builtin::FirstTSBuiltin;
1062    }
1063    virtual bool hasFeature(StringRef Feature) const {
1064      return Feature == "ptx" || Feature == "nvptx";
1065    }
1066
1067    virtual void getGCCRegNames(const char * const *&Names,
1068                                unsigned &NumNames) const;
1069    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1070                                  unsigned &NumAliases) const {
1071      // No aliases.
1072      Aliases = 0;
1073      NumAliases = 0;
1074    }
1075    virtual bool validateAsmConstraint(const char *&Name,
1076                                       TargetInfo::ConstraintInfo &info) const {
1077      // FIXME: implement
1078      return true;
1079    }
1080    virtual const char *getClobbers() const {
1081      // FIXME: Is this really right?
1082      return "";
1083    }
1084    virtual BuiltinVaListKind getBuiltinVaListKind() const {
1085      // FIXME: implement
1086      return TargetInfo::CharPtrBuiltinVaList;
1087    }
1088    virtual bool setCPU(const std::string &Name) {
1089      return Name == "sm_10" || Name == "sm_13" || Name == "sm_20";
1090    }
1091    virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1092                                   StringRef Name,
1093                                   bool Enabled) const;
1094  };
1095
1096  const Builtin::Info NVPTXTargetInfo::BuiltinInfo[] = {
1097#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
1098#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
1099                                              ALL_LANGUAGES },
1100#include "clang/Basic/BuiltinsNVPTX.def"
1101  };
1102
1103  const char * const NVPTXTargetInfo::GCCRegNames[] = {
1104    "r0"
1105  };
1106
1107  void NVPTXTargetInfo::getGCCRegNames(const char * const *&Names,
1108                                     unsigned &NumNames) const {
1109    Names = GCCRegNames;
1110    NumNames = llvm::array_lengthof(GCCRegNames);
1111  }
1112
1113  bool NVPTXTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1114                                          StringRef Name,
1115                                          bool Enabled) const {
1116    if(std::binary_search(AvailableFeatures.begin(), AvailableFeatures.end(),
1117                          Name)) {
1118      Features[Name] = Enabled;
1119      return true;
1120    } else {
1121      return false;
1122    }
1123  }
1124
1125  class NVPTX32TargetInfo : public NVPTXTargetInfo {
1126  public:
1127    NVPTX32TargetInfo(const std::string& triple) : NVPTXTargetInfo(triple) {
1128      PointerWidth = PointerAlign = 32;
1129      SizeType     = PtrDiffType = IntPtrType = TargetInfo::UnsignedInt;
1130      DescriptionString
1131        = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
1132          "f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-"
1133          "n16:32:64";
1134  }
1135  };
1136
1137  class NVPTX64TargetInfo : public NVPTXTargetInfo {
1138  public:
1139    NVPTX64TargetInfo(const std::string& triple) : NVPTXTargetInfo(triple) {
1140      PointerWidth = PointerAlign = 64;
1141      SizeType     = PtrDiffType = IntPtrType = TargetInfo::UnsignedLongLong;
1142      DescriptionString
1143        = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
1144          "f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-"
1145          "n16:32:64";
1146  }
1147  };
1148}
1149
1150namespace {
1151// MBlaze abstract base class
1152class MBlazeTargetInfo : public TargetInfo {
1153  static const char * const GCCRegNames[];
1154  static const TargetInfo::GCCRegAlias GCCRegAliases[];
1155
1156public:
1157  MBlazeTargetInfo(const std::string& triple) : TargetInfo(triple) {
1158    DescriptionString = "E-p:32:32:32-i8:8:8-i16:16:16";
1159  }
1160
1161  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1162                                 unsigned &NumRecords) const {
1163    // FIXME: Implement.
1164    Records = 0;
1165    NumRecords = 0;
1166  }
1167
1168  virtual void getTargetDefines(const LangOptions &Opts,
1169                                MacroBuilder &Builder) const;
1170
1171  virtual bool hasFeature(StringRef Feature) const {
1172    return Feature == "mblaze";
1173  }
1174
1175  virtual BuiltinVaListKind getBuiltinVaListKind() const {
1176    return TargetInfo::CharPtrBuiltinVaList;
1177  }
1178  virtual const char *getTargetPrefix() const {
1179    return "mblaze";
1180  }
1181  virtual void getGCCRegNames(const char * const *&Names,
1182                              unsigned &NumNames) const;
1183  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1184                                unsigned &NumAliases) const;
1185  virtual bool validateAsmConstraint(const char *&Name,
1186                                     TargetInfo::ConstraintInfo &Info) const {
1187    switch (*Name) {
1188    default: return false;
1189    case 'O': // Zero
1190      return true;
1191    case 'b': // Base register
1192    case 'f': // Floating point register
1193      Info.setAllowsRegister();
1194      return true;
1195    }
1196  }
1197  virtual const char *getClobbers() const {
1198    return "";
1199  }
1200};
1201
1202/// MBlazeTargetInfo::getTargetDefines - Return a set of the MBlaze-specific
1203/// #defines that are not tied to a specific subtarget.
1204void MBlazeTargetInfo::getTargetDefines(const LangOptions &Opts,
1205                                     MacroBuilder &Builder) const {
1206  // Target identification.
1207  Builder.defineMacro("__microblaze__");
1208  Builder.defineMacro("_ARCH_MICROBLAZE");
1209  Builder.defineMacro("__MICROBLAZE__");
1210
1211  // Target properties.
1212  Builder.defineMacro("_BIG_ENDIAN");
1213  Builder.defineMacro("__BIG_ENDIAN__");
1214
1215  // Subtarget options.
1216  Builder.defineMacro("__REGISTER_PREFIX__", "");
1217}
1218
1219
1220const char * const MBlazeTargetInfo::GCCRegNames[] = {
1221  "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
1222  "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
1223  "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
1224  "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
1225  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
1226  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
1227  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
1228  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
1229  "hi",   "lo",   "accum","rmsr", "$fcc1","$fcc2","$fcc3","$fcc4",
1230  "$fcc5","$fcc6","$fcc7","$ap",  "$rap", "$frp"
1231};
1232
1233void MBlazeTargetInfo::getGCCRegNames(const char * const *&Names,
1234                                   unsigned &NumNames) const {
1235  Names = GCCRegNames;
1236  NumNames = llvm::array_lengthof(GCCRegNames);
1237}
1238
1239const TargetInfo::GCCRegAlias MBlazeTargetInfo::GCCRegAliases[] = {
1240  { {"f0"},  "r0" },
1241  { {"f1"},  "r1" },
1242  { {"f2"},  "r2" },
1243  { {"f3"},  "r3" },
1244  { {"f4"},  "r4" },
1245  { {"f5"},  "r5" },
1246  { {"f6"},  "r6" },
1247  { {"f7"},  "r7" },
1248  { {"f8"},  "r8" },
1249  { {"f9"},  "r9" },
1250  { {"f10"}, "r10" },
1251  { {"f11"}, "r11" },
1252  { {"f12"}, "r12" },
1253  { {"f13"}, "r13" },
1254  { {"f14"}, "r14" },
1255  { {"f15"}, "r15" },
1256  { {"f16"}, "r16" },
1257  { {"f17"}, "r17" },
1258  { {"f18"}, "r18" },
1259  { {"f19"}, "r19" },
1260  { {"f20"}, "r20" },
1261  { {"f21"}, "r21" },
1262  { {"f22"}, "r22" },
1263  { {"f23"}, "r23" },
1264  { {"f24"}, "r24" },
1265  { {"f25"}, "r25" },
1266  { {"f26"}, "r26" },
1267  { {"f27"}, "r27" },
1268  { {"f28"}, "r28" },
1269  { {"f29"}, "r29" },
1270  { {"f30"}, "r30" },
1271  { {"f31"}, "r31" },
1272};
1273
1274void MBlazeTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1275                                     unsigned &NumAliases) const {
1276  Aliases = GCCRegAliases;
1277  NumAliases = llvm::array_lengthof(GCCRegAliases);
1278}
1279} // end anonymous namespace.
1280
1281namespace {
1282// Namespace for x86 abstract base class
1283const Builtin::Info BuiltinInfo[] = {
1284#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
1285#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
1286                                              ALL_LANGUAGES },
1287#include "clang/Basic/BuiltinsX86.def"
1288};
1289
1290static const char* const GCCRegNames[] = {
1291  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
1292  "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
1293  "argp", "flags", "fpcr", "fpsr", "dirflag", "frame",
1294  "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
1295  "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
1296  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1297  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
1298  "ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7",
1299  "ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15",
1300};
1301
1302const TargetInfo::AddlRegName AddlRegNames[] = {
1303  { { "al", "ah", "eax", "rax" }, 0 },
1304  { { "bl", "bh", "ebx", "rbx" }, 3 },
1305  { { "cl", "ch", "ecx", "rcx" }, 2 },
1306  { { "dl", "dh", "edx", "rdx" }, 1 },
1307  { { "esi", "rsi" }, 4 },
1308  { { "edi", "rdi" }, 5 },
1309  { { "esp", "rsp" }, 7 },
1310  { { "ebp", "rbp" }, 6 },
1311};
1312
1313// X86 target abstract base class; x86-32 and x86-64 are very close, so
1314// most of the implementation can be shared.
1315class X86TargetInfo : public TargetInfo {
1316  enum X86SSEEnum {
1317    NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2
1318  } SSELevel;
1319  enum MMX3DNowEnum {
1320    NoMMX3DNow, MMX, AMD3DNow, AMD3DNowAthlon
1321  } MMX3DNowLevel;
1322
1323  bool HasAES;
1324  bool HasPCLMUL;
1325  bool HasLZCNT;
1326  bool HasBMI;
1327  bool HasBMI2;
1328  bool HasPOPCNT;
1329  bool HasSSE4a;
1330  bool HasFMA4;
1331  bool HasFMA;
1332  bool HasXOP;
1333
1334  /// \brief Enumeration of all of the X86 CPUs supported by Clang.
1335  ///
1336  /// Each enumeration represents a particular CPU supported by Clang. These
1337  /// loosely correspond to the options passed to '-march' or '-mtune' flags.
1338  enum CPUKind {
1339    CK_Generic,
1340
1341    /// \name i386
1342    /// i386-generation processors.
1343    //@{
1344    CK_i386,
1345    //@}
1346
1347    /// \name i486
1348    /// i486-generation processors.
1349    //@{
1350    CK_i486,
1351    CK_WinChipC6,
1352    CK_WinChip2,
1353    CK_C3,
1354    //@}
1355
1356    /// \name i586
1357    /// i586-generation processors, P5 microarchitecture based.
1358    //@{
1359    CK_i586,
1360    CK_Pentium,
1361    CK_PentiumMMX,
1362    //@}
1363
1364    /// \name i686
1365    /// i686-generation processors, P6 / Pentium M microarchitecture based.
1366    //@{
1367    CK_i686,
1368    CK_PentiumPro,
1369    CK_Pentium2,
1370    CK_Pentium3,
1371    CK_Pentium3M,
1372    CK_PentiumM,
1373    CK_C3_2,
1374
1375    /// This enumerator is a bit odd, as GCC no longer accepts -march=yonah.
1376    /// Clang however has some logic to suport this.
1377    // FIXME: Warn, deprecate, and potentially remove this.
1378    CK_Yonah,
1379    //@}
1380
1381    /// \name Netburst
1382    /// Netburst microarchitecture based processors.
1383    //@{
1384    CK_Pentium4,
1385    CK_Pentium4M,
1386    CK_Prescott,
1387    CK_Nocona,
1388    //@}
1389
1390    /// \name Core
1391    /// Core microarchitecture based processors.
1392    //@{
1393    CK_Core2,
1394
1395    /// This enumerator, like \see CK_Yonah, is a bit odd. It is another
1396    /// codename which GCC no longer accepts as an option to -march, but Clang
1397    /// has some logic for recognizing it.
1398    // FIXME: Warn, deprecate, and potentially remove this.
1399    CK_Penryn,
1400    //@}
1401
1402    /// \name Atom
1403    /// Atom processors
1404    //@{
1405    CK_Atom,
1406    //@}
1407
1408    /// \name Nehalem
1409    /// Nehalem microarchitecture based processors.
1410    //@{
1411    CK_Corei7,
1412    CK_Corei7AVX,
1413    CK_CoreAVXi,
1414    CK_CoreAVX2,
1415    //@}
1416
1417    /// \name K6
1418    /// K6 architecture processors.
1419    //@{
1420    CK_K6,
1421    CK_K6_2,
1422    CK_K6_3,
1423    //@}
1424
1425    /// \name K7
1426    /// K7 architecture processors.
1427    //@{
1428    CK_Athlon,
1429    CK_AthlonThunderbird,
1430    CK_Athlon4,
1431    CK_AthlonXP,
1432    CK_AthlonMP,
1433    //@}
1434
1435    /// \name K8
1436    /// K8 architecture processors.
1437    //@{
1438    CK_Athlon64,
1439    CK_Athlon64SSE3,
1440    CK_AthlonFX,
1441    CK_K8,
1442    CK_K8SSE3,
1443    CK_Opteron,
1444    CK_OpteronSSE3,
1445    CK_AMDFAM10,
1446    //@}
1447
1448    /// \name Bobcat
1449    /// Bobcat architecture processors.
1450    //@{
1451    CK_BTVER1,
1452    //@}
1453
1454    /// \name Bulldozer
1455    /// Bulldozer architecture processors.
1456    //@{
1457    CK_BDVER1,
1458    CK_BDVER2,
1459    //@}
1460
1461    /// This specification is deprecated and will be removed in the future.
1462    /// Users should prefer \see CK_K8.
1463    // FIXME: Warn on this when the CPU is set to it.
1464    CK_x86_64,
1465    //@}
1466
1467    /// \name Geode
1468    /// Geode processors.
1469    //@{
1470    CK_Geode
1471    //@}
1472  } CPU;
1473
1474public:
1475  X86TargetInfo(const std::string& triple)
1476    : TargetInfo(triple), SSELevel(NoSSE), MMX3DNowLevel(NoMMX3DNow),
1477      HasAES(false), HasPCLMUL(false), HasLZCNT(false), HasBMI(false),
1478      HasBMI2(false), HasPOPCNT(false), HasSSE4a(false), HasFMA4(false),
1479      HasFMA(false), HasXOP(false), CPU(CK_Generic) {
1480    BigEndian = false;
1481    LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
1482  }
1483  virtual unsigned getFloatEvalMethod() const {
1484    // X87 evaluates with 80 bits "long double" precision.
1485    return SSELevel == NoSSE ? 2 : 0;
1486  }
1487  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1488                                 unsigned &NumRecords) const {
1489    Records = BuiltinInfo;
1490    NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
1491  }
1492  virtual void getGCCRegNames(const char * const *&Names,
1493                              unsigned &NumNames) const {
1494    Names = GCCRegNames;
1495    NumNames = llvm::array_lengthof(GCCRegNames);
1496  }
1497  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1498                                unsigned &NumAliases) const {
1499    Aliases = 0;
1500    NumAliases = 0;
1501  }
1502  virtual void getGCCAddlRegNames(const AddlRegName *&Names,
1503				  unsigned &NumNames) const {
1504    Names = AddlRegNames;
1505    NumNames = llvm::array_lengthof(AddlRegNames);
1506  }
1507  virtual bool validateAsmConstraint(const char *&Name,
1508                                     TargetInfo::ConstraintInfo &info) const;
1509  virtual std::string convertConstraint(const char *&Constraint) const;
1510  virtual const char *getClobbers() const {
1511    return "~{dirflag},~{fpsr},~{flags}";
1512  }
1513  virtual void getTargetDefines(const LangOptions &Opts,
1514                                MacroBuilder &Builder) const;
1515  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1516                                 StringRef Name,
1517                                 bool Enabled) const;
1518  virtual void getDefaultFeatures(llvm::StringMap<bool> &Features) const;
1519  virtual bool hasFeature(StringRef Feature) const;
1520  virtual void HandleTargetFeatures(std::vector<std::string> &Features);
1521  virtual const char* getABI() const {
1522    if (PointerWidth == 64 && SSELevel >= AVX)
1523      return "avx";
1524    else if (PointerWidth == 32 && MMX3DNowLevel == NoMMX3DNow)
1525      return "no-mmx";
1526    return "";
1527  }
1528  virtual bool setCPU(const std::string &Name) {
1529    CPU = llvm::StringSwitch<CPUKind>(Name)
1530      .Case("i386", CK_i386)
1531      .Case("i486", CK_i486)
1532      .Case("winchip-c6", CK_WinChipC6)
1533      .Case("winchip2", CK_WinChip2)
1534      .Case("c3", CK_C3)
1535      .Case("i586", CK_i586)
1536      .Case("pentium", CK_Pentium)
1537      .Case("pentium-mmx", CK_PentiumMMX)
1538      .Case("i686", CK_i686)
1539      .Case("pentiumpro", CK_PentiumPro)
1540      .Case("pentium2", CK_Pentium2)
1541      .Case("pentium3", CK_Pentium3)
1542      .Case("pentium3m", CK_Pentium3M)
1543      .Case("pentium-m", CK_PentiumM)
1544      .Case("c3-2", CK_C3_2)
1545      .Case("yonah", CK_Yonah)
1546      .Case("pentium4", CK_Pentium4)
1547      .Case("pentium4m", CK_Pentium4M)
1548      .Case("prescott", CK_Prescott)
1549      .Case("nocona", CK_Nocona)
1550      .Case("core2", CK_Core2)
1551      .Case("penryn", CK_Penryn)
1552      .Case("atom", CK_Atom)
1553      .Case("corei7", CK_Corei7)
1554      .Case("corei7-avx", CK_Corei7AVX)
1555      .Case("core-avx-i", CK_CoreAVXi)
1556      .Case("core-avx2", CK_CoreAVX2)
1557      .Case("k6", CK_K6)
1558      .Case("k6-2", CK_K6_2)
1559      .Case("k6-3", CK_K6_3)
1560      .Case("athlon", CK_Athlon)
1561      .Case("athlon-tbird", CK_AthlonThunderbird)
1562      .Case("athlon-4", CK_Athlon4)
1563      .Case("athlon-xp", CK_AthlonXP)
1564      .Case("athlon-mp", CK_AthlonMP)
1565      .Case("athlon64", CK_Athlon64)
1566      .Case("athlon64-sse3", CK_Athlon64SSE3)
1567      .Case("athlon-fx", CK_AthlonFX)
1568      .Case("k8", CK_K8)
1569      .Case("k8-sse3", CK_K8SSE3)
1570      .Case("opteron", CK_Opteron)
1571      .Case("opteron-sse3", CK_OpteronSSE3)
1572      .Case("amdfam10", CK_AMDFAM10)
1573      .Case("btver1", CK_BTVER1)
1574      .Case("bdver1", CK_BDVER1)
1575      .Case("bdver2", CK_BDVER2)
1576      .Case("x86-64", CK_x86_64)
1577      .Case("geode", CK_Geode)
1578      .Default(CK_Generic);
1579
1580    // Perform any per-CPU checks necessary to determine if this CPU is
1581    // acceptable.
1582    // FIXME: This results in terrible diagnostics. Clang just says the CPU is
1583    // invalid without explaining *why*.
1584    switch (CPU) {
1585    case CK_Generic:
1586      // No processor selected!
1587      return false;
1588
1589    case CK_i386:
1590    case CK_i486:
1591    case CK_WinChipC6:
1592    case CK_WinChip2:
1593    case CK_C3:
1594    case CK_i586:
1595    case CK_Pentium:
1596    case CK_PentiumMMX:
1597    case CK_i686:
1598    case CK_PentiumPro:
1599    case CK_Pentium2:
1600    case CK_Pentium3:
1601    case CK_Pentium3M:
1602    case CK_PentiumM:
1603    case CK_Yonah:
1604    case CK_C3_2:
1605    case CK_Pentium4:
1606    case CK_Pentium4M:
1607    case CK_Prescott:
1608    case CK_K6:
1609    case CK_K6_2:
1610    case CK_K6_3:
1611    case CK_Athlon:
1612    case CK_AthlonThunderbird:
1613    case CK_Athlon4:
1614    case CK_AthlonXP:
1615    case CK_AthlonMP:
1616    case CK_Geode:
1617      // Only accept certain architectures when compiling in 32-bit mode.
1618      if (PointerWidth != 32)
1619        return false;
1620
1621      // Fallthrough
1622    case CK_Nocona:
1623    case CK_Core2:
1624    case CK_Penryn:
1625    case CK_Atom:
1626    case CK_Corei7:
1627    case CK_Corei7AVX:
1628    case CK_CoreAVXi:
1629    case CK_CoreAVX2:
1630    case CK_Athlon64:
1631    case CK_Athlon64SSE3:
1632    case CK_AthlonFX:
1633    case CK_K8:
1634    case CK_K8SSE3:
1635    case CK_Opteron:
1636    case CK_OpteronSSE3:
1637    case CK_AMDFAM10:
1638    case CK_BTVER1:
1639    case CK_BDVER1:
1640    case CK_BDVER2:
1641    case CK_x86_64:
1642      return true;
1643    }
1644    llvm_unreachable("Unhandled CPU kind");
1645  }
1646};
1647
1648void X86TargetInfo::getDefaultFeatures(llvm::StringMap<bool> &Features) const {
1649  // FIXME: This should not be here.
1650  Features["3dnow"] = false;
1651  Features["3dnowa"] = false;
1652  Features["mmx"] = false;
1653  Features["sse"] = false;
1654  Features["sse2"] = false;
1655  Features["sse3"] = false;
1656  Features["ssse3"] = false;
1657  Features["sse41"] = false;
1658  Features["sse42"] = false;
1659  Features["sse4a"] = false;
1660  Features["aes"] = false;
1661  Features["pclmul"] = false;
1662  Features["avx"] = false;
1663  Features["avx2"] = false;
1664  Features["lzcnt"] = false;
1665  Features["bmi"] = false;
1666  Features["bmi2"] = false;
1667  Features["popcnt"] = false;
1668  Features["fma4"] = false;
1669  Features["fma"] = false;
1670  Features["xop"] = false;
1671
1672  // FIXME: This *really* should not be here.
1673
1674  // X86_64 always has SSE2.
1675  if (PointerWidth == 64)
1676    Features["sse2"] = Features["sse"] = Features["mmx"] = true;
1677
1678  switch (CPU) {
1679  case CK_Generic:
1680  case CK_i386:
1681  case CK_i486:
1682  case CK_i586:
1683  case CK_Pentium:
1684  case CK_i686:
1685  case CK_PentiumPro:
1686    break;
1687  case CK_PentiumMMX:
1688  case CK_Pentium2:
1689    setFeatureEnabled(Features, "mmx", true);
1690    break;
1691  case CK_Pentium3:
1692  case CK_Pentium3M:
1693    setFeatureEnabled(Features, "mmx", true);
1694    setFeatureEnabled(Features, "sse", true);
1695    break;
1696  case CK_PentiumM:
1697  case CK_Pentium4:
1698  case CK_Pentium4M:
1699  case CK_x86_64:
1700    setFeatureEnabled(Features, "mmx", true);
1701    setFeatureEnabled(Features, "sse2", true);
1702    break;
1703  case CK_Yonah:
1704  case CK_Prescott:
1705  case CK_Nocona:
1706    setFeatureEnabled(Features, "mmx", true);
1707    setFeatureEnabled(Features, "sse3", true);
1708    break;
1709  case CK_Core2:
1710    setFeatureEnabled(Features, "mmx", true);
1711    setFeatureEnabled(Features, "ssse3", true);
1712    break;
1713  case CK_Penryn:
1714    setFeatureEnabled(Features, "mmx", true);
1715    setFeatureEnabled(Features, "sse4.1", true);
1716    break;
1717  case CK_Atom:
1718    setFeatureEnabled(Features, "mmx", true);
1719    setFeatureEnabled(Features, "ssse3", true);
1720    break;
1721  case CK_Corei7:
1722    setFeatureEnabled(Features, "mmx", true);
1723    setFeatureEnabled(Features, "sse4", true);
1724    break;
1725  case CK_Corei7AVX:
1726  case CK_CoreAVXi:
1727    setFeatureEnabled(Features, "mmx", true);
1728    setFeatureEnabled(Features, "avx", true);
1729    setFeatureEnabled(Features, "aes", true);
1730    setFeatureEnabled(Features, "pclmul", true);
1731    break;
1732  case CK_CoreAVX2:
1733    setFeatureEnabled(Features, "mmx", true);
1734    setFeatureEnabled(Features, "avx2", true);
1735    setFeatureEnabled(Features, "aes", true);
1736    setFeatureEnabled(Features, "pclmul", true);
1737    setFeatureEnabled(Features, "lzcnt", true);
1738    setFeatureEnabled(Features, "bmi", true);
1739    setFeatureEnabled(Features, "bmi2", true);
1740    setFeatureEnabled(Features, "fma", true);
1741    break;
1742  case CK_K6:
1743  case CK_WinChipC6:
1744    setFeatureEnabled(Features, "mmx", true);
1745    break;
1746  case CK_K6_2:
1747  case CK_K6_3:
1748  case CK_WinChip2:
1749  case CK_C3:
1750    setFeatureEnabled(Features, "3dnow", true);
1751    break;
1752  case CK_Athlon:
1753  case CK_AthlonThunderbird:
1754  case CK_Geode:
1755    setFeatureEnabled(Features, "3dnowa", true);
1756    break;
1757  case CK_Athlon4:
1758  case CK_AthlonXP:
1759  case CK_AthlonMP:
1760    setFeatureEnabled(Features, "sse", true);
1761    setFeatureEnabled(Features, "3dnowa", true);
1762    break;
1763  case CK_K8:
1764  case CK_Opteron:
1765  case CK_Athlon64:
1766  case CK_AthlonFX:
1767    setFeatureEnabled(Features, "sse2", true);
1768    setFeatureEnabled(Features, "3dnowa", true);
1769    break;
1770  case CK_K8SSE3:
1771  case CK_OpteronSSE3:
1772  case CK_Athlon64SSE3:
1773    setFeatureEnabled(Features, "sse3", true);
1774    setFeatureEnabled(Features, "3dnowa", true);
1775    break;
1776  case CK_AMDFAM10:
1777    setFeatureEnabled(Features, "sse3", true);
1778    setFeatureEnabled(Features, "sse4a", true);
1779    setFeatureEnabled(Features, "3dnowa", true);
1780    break;
1781  case CK_BTVER1:
1782    setFeatureEnabled(Features, "ssse3", true);
1783    setFeatureEnabled(Features, "sse4a", true);
1784    break;
1785  case CK_BDVER1:
1786  case CK_BDVER2:
1787    setFeatureEnabled(Features, "avx", true);
1788    setFeatureEnabled(Features, "xop", true);
1789    setFeatureEnabled(Features, "aes", true);
1790    setFeatureEnabled(Features, "pclmul", true);
1791    break;
1792  case CK_C3_2:
1793    setFeatureEnabled(Features, "mmx", true);
1794    setFeatureEnabled(Features, "sse", true);
1795    break;
1796  }
1797}
1798
1799bool X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1800                                      StringRef Name,
1801                                      bool Enabled) const {
1802  // FIXME: This *really* should not be here.  We need some way of translating
1803  // options into llvm subtarget features.
1804  if (!Features.count(Name) &&
1805      (Name != "sse4" && Name != "sse4.2" && Name != "sse4.1"))
1806    return false;
1807
1808  // FIXME: this should probably use a switch with fall through.
1809
1810  if (Enabled) {
1811    if (Name == "mmx")
1812      Features["mmx"] = true;
1813    else if (Name == "sse")
1814      Features["mmx"] = Features["sse"] = true;
1815    else if (Name == "sse2")
1816      Features["mmx"] = Features["sse"] = Features["sse2"] = true;
1817    else if (Name == "sse3")
1818      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1819        true;
1820    else if (Name == "ssse3")
1821      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1822        Features["ssse3"] = true;
1823    else if (Name == "sse4" || Name == "sse4.2")
1824      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1825        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1826        Features["popcnt"] = true;
1827    else if (Name == "sse4.1")
1828      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1829        Features["ssse3"] = Features["sse41"] = true;
1830    else if (Name == "3dnow")
1831      Features["mmx"] = Features["3dnow"] = true;
1832    else if (Name == "3dnowa")
1833      Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = true;
1834    else if (Name == "aes")
1835      Features["sse"] = Features["sse2"] = Features["aes"] = true;
1836    else if (Name == "pclmul")
1837      Features["sse"] = Features["sse2"] = Features["pclmul"] = true;
1838    else if (Name == "avx")
1839      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1840        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1841        Features["popcnt"] = Features["avx"] = true;
1842    else if (Name == "avx2")
1843      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1844        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1845        Features["popcnt"] = Features["avx"] = Features["avx2"] = true;
1846    else if (Name == "fma")
1847      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1848        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1849        Features["popcnt"] = Features["avx"] = Features["fma"] = true;
1850    else if (Name == "fma4")
1851        Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1852        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1853        Features["popcnt"] = Features["avx"] = Features["sse4a"] =
1854        Features["fma4"] = true;
1855    else if (Name == "xop")
1856        Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1857        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1858        Features["popcnt"] = Features["avx"] = Features["sse4a"] =
1859        Features["fma4"] = Features["xop"] = true;
1860    else if (Name == "sse4a")
1861      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1862        Features["sse4a"] = true;
1863    else if (Name == "lzcnt")
1864      Features["lzcnt"] = true;
1865    else if (Name == "bmi")
1866      Features["bmi"] = true;
1867    else if (Name == "bmi2")
1868      Features["bmi2"] = true;
1869    else if (Name == "popcnt")
1870      Features["popcnt"] = true;
1871  } else {
1872    if (Name == "mmx")
1873      Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = false;
1874    else if (Name == "sse")
1875      Features["sse"] = Features["sse2"] = Features["sse3"] =
1876        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1877        Features["sse4a"] = Features["avx"] = Features["avx2"] =
1878        Features["fma"] = Features["fma4"] = Features["aes"] =
1879        Features["pclmul"] = Features["xop"] = false;
1880    else if (Name == "sse2")
1881      Features["sse2"] = Features["sse3"] = Features["ssse3"] =
1882        Features["sse41"] = Features["sse42"] = Features["sse4a"] =
1883        Features["avx"] = Features["avx2"] = Features["fma"] =
1884        Features["fma4"] = Features["aes"] = Features["pclmul"] =
1885        Features["xop"] = false;
1886    else if (Name == "sse3")
1887      Features["sse3"] = Features["ssse3"] = Features["sse41"] =
1888        Features["sse42"] = Features["sse4a"] = Features["avx"] =
1889        Features["avx2"] = Features["fma"] = Features["fma4"] =
1890        Features["xop"] = false;
1891    else if (Name == "ssse3")
1892      Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1893        Features["avx"] = Features["avx2"] = Features["fma"] = false;
1894    else if (Name == "sse4" || Name == "sse4.1")
1895      Features["sse41"] = Features["sse42"] = Features["avx"] =
1896        Features["avx2"] = Features["fma"] = false;
1897    else if (Name == "sse4.2")
1898      Features["sse42"] = Features["avx"] = Features["avx2"] =
1899        Features["fma"] = false;
1900    else if (Name == "3dnow")
1901      Features["3dnow"] = Features["3dnowa"] = false;
1902    else if (Name == "3dnowa")
1903      Features["3dnowa"] = false;
1904    else if (Name == "aes")
1905      Features["aes"] = false;
1906    else if (Name == "pclmul")
1907      Features["pclmul"] = false;
1908    else if (Name == "avx")
1909      Features["avx"] = Features["avx2"] = Features["fma"] =
1910        Features["fma4"] = Features["xop"] = false;
1911    else if (Name == "avx2")
1912      Features["avx2"] = false;
1913    else if (Name == "fma")
1914      Features["fma"] = false;
1915    else if (Name == "sse4a")
1916      Features["sse4a"] = Features["fma4"] = Features["xop"] = false;
1917    else if (Name == "lzcnt")
1918      Features["lzcnt"] = false;
1919    else if (Name == "bmi")
1920      Features["bmi"] = false;
1921    else if (Name == "bmi2")
1922      Features["bmi2"] = false;
1923    else if (Name == "popcnt")
1924      Features["popcnt"] = false;
1925    else if (Name == "fma4")
1926      Features["fma4"] = Features["xop"] = false;
1927    else if (Name == "xop")
1928      Features["xop"] = false;
1929  }
1930
1931  return true;
1932}
1933
1934/// HandleTargetOptions - Perform initialization based on the user
1935/// configured set of features.
1936void X86TargetInfo::HandleTargetFeatures(std::vector<std::string> &Features) {
1937  // Remember the maximum enabled sselevel.
1938  for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
1939    // Ignore disabled features.
1940    if (Features[i][0] == '-')
1941      continue;
1942
1943    StringRef Feature = StringRef(Features[i]).substr(1);
1944
1945    if (Feature == "aes") {
1946      HasAES = true;
1947      continue;
1948    }
1949
1950    if (Feature == "pclmul") {
1951      HasPCLMUL = true;
1952      continue;
1953    }
1954
1955    if (Feature == "lzcnt") {
1956      HasLZCNT = true;
1957      continue;
1958    }
1959
1960    if (Feature == "bmi") {
1961      HasBMI = true;
1962      continue;
1963    }
1964
1965    if (Feature == "bmi2") {
1966      HasBMI2 = true;
1967      continue;
1968    }
1969
1970    if (Feature == "popcnt") {
1971      HasPOPCNT = true;
1972      continue;
1973    }
1974
1975    if (Feature == "sse4a") {
1976      HasSSE4a = true;
1977      continue;
1978    }
1979
1980    if (Feature == "fma4") {
1981      HasFMA4 = true;
1982      continue;
1983    }
1984
1985    if (Feature == "fma") {
1986      HasFMA = true;
1987      continue;
1988    }
1989
1990    if (Feature == "xop") {
1991      HasXOP = true;
1992      continue;
1993    }
1994
1995    assert(Features[i][0] == '+' && "Invalid target feature!");
1996    X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
1997      .Case("avx2", AVX2)
1998      .Case("avx", AVX)
1999      .Case("sse42", SSE42)
2000      .Case("sse41", SSE41)
2001      .Case("ssse3", SSSE3)
2002      .Case("sse3", SSE3)
2003      .Case("sse2", SSE2)
2004      .Case("sse", SSE1)
2005      .Default(NoSSE);
2006    SSELevel = std::max(SSELevel, Level);
2007
2008    MMX3DNowEnum ThreeDNowLevel =
2009      llvm::StringSwitch<MMX3DNowEnum>(Feature)
2010        .Case("3dnowa", AMD3DNowAthlon)
2011        .Case("3dnow", AMD3DNow)
2012        .Case("mmx", MMX)
2013        .Default(NoMMX3DNow);
2014
2015    MMX3DNowLevel = std::max(MMX3DNowLevel, ThreeDNowLevel);
2016  }
2017
2018  // Don't tell the backend if we're turning off mmx; it will end up disabling
2019  // SSE, which we don't want.
2020  std::vector<std::string>::iterator it;
2021  it = std::find(Features.begin(), Features.end(), "-mmx");
2022  if (it != Features.end())
2023    Features.erase(it);
2024}
2025
2026/// X86TargetInfo::getTargetDefines - Return the set of the X86-specific macro
2027/// definitions for this particular subtarget.
2028void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
2029                                     MacroBuilder &Builder) const {
2030  // Target identification.
2031  if (PointerWidth == 64) {
2032    if (getLongWidth() == 64) {
2033      Builder.defineMacro("_LP64");
2034      Builder.defineMacro("__LP64__");
2035    }
2036    Builder.defineMacro("__amd64__");
2037    Builder.defineMacro("__amd64");
2038    Builder.defineMacro("__x86_64");
2039    Builder.defineMacro("__x86_64__");
2040  } else {
2041    DefineStd(Builder, "i386", Opts);
2042  }
2043
2044  // Subtarget options.
2045  // FIXME: We are hard-coding the tune parameters based on the CPU, but they
2046  // truly should be based on -mtune options.
2047  switch (CPU) {
2048  case CK_Generic:
2049    break;
2050  case CK_i386:
2051    // The rest are coming from the i386 define above.
2052    Builder.defineMacro("__tune_i386__");
2053    break;
2054  case CK_i486:
2055  case CK_WinChipC6:
2056  case CK_WinChip2:
2057  case CK_C3:
2058    defineCPUMacros(Builder, "i486");
2059    break;
2060  case CK_PentiumMMX:
2061    Builder.defineMacro("__pentium_mmx__");
2062    Builder.defineMacro("__tune_pentium_mmx__");
2063    // Fallthrough
2064  case CK_i586:
2065  case CK_Pentium:
2066    defineCPUMacros(Builder, "i586");
2067    defineCPUMacros(Builder, "pentium");
2068    break;
2069  case CK_Pentium3:
2070  case CK_Pentium3M:
2071  case CK_PentiumM:
2072    Builder.defineMacro("__tune_pentium3__");
2073    // Fallthrough
2074  case CK_Pentium2:
2075  case CK_C3_2:
2076    Builder.defineMacro("__tune_pentium2__");
2077    // Fallthrough
2078  case CK_PentiumPro:
2079    Builder.defineMacro("__tune_i686__");
2080    Builder.defineMacro("__tune_pentiumpro__");
2081    // Fallthrough
2082  case CK_i686:
2083    Builder.defineMacro("__i686");
2084    Builder.defineMacro("__i686__");
2085    // Strangely, __tune_i686__ isn't defined by GCC when CPU == i686.
2086    Builder.defineMacro("__pentiumpro");
2087    Builder.defineMacro("__pentiumpro__");
2088    break;
2089  case CK_Pentium4:
2090  case CK_Pentium4M:
2091    defineCPUMacros(Builder, "pentium4");
2092    break;
2093  case CK_Yonah:
2094  case CK_Prescott:
2095  case CK_Nocona:
2096    defineCPUMacros(Builder, "nocona");
2097    break;
2098  case CK_Core2:
2099  case CK_Penryn:
2100    defineCPUMacros(Builder, "core2");
2101    break;
2102  case CK_Atom:
2103    defineCPUMacros(Builder, "atom");
2104    break;
2105  case CK_Corei7:
2106  case CK_Corei7AVX:
2107  case CK_CoreAVXi:
2108  case CK_CoreAVX2:
2109    defineCPUMacros(Builder, "corei7");
2110    break;
2111  case CK_K6_2:
2112    Builder.defineMacro("__k6_2__");
2113    Builder.defineMacro("__tune_k6_2__");
2114    // Fallthrough
2115  case CK_K6_3:
2116    if (CPU != CK_K6_2) {  // In case of fallthrough
2117      // FIXME: GCC may be enabling these in cases where some other k6
2118      // architecture is specified but -m3dnow is explicitly provided. The
2119      // exact semantics need to be determined and emulated here.
2120      Builder.defineMacro("__k6_3__");
2121      Builder.defineMacro("__tune_k6_3__");
2122    }
2123    // Fallthrough
2124  case CK_K6:
2125    defineCPUMacros(Builder, "k6");
2126    break;
2127  case CK_Athlon:
2128  case CK_AthlonThunderbird:
2129  case CK_Athlon4:
2130  case CK_AthlonXP:
2131  case CK_AthlonMP:
2132    defineCPUMacros(Builder, "athlon");
2133    if (SSELevel != NoSSE) {
2134      Builder.defineMacro("__athlon_sse__");
2135      Builder.defineMacro("__tune_athlon_sse__");
2136    }
2137    break;
2138  case CK_K8:
2139  case CK_K8SSE3:
2140  case CK_x86_64:
2141  case CK_Opteron:
2142  case CK_OpteronSSE3:
2143  case CK_Athlon64:
2144  case CK_Athlon64SSE3:
2145  case CK_AthlonFX:
2146    defineCPUMacros(Builder, "k8");
2147    break;
2148  case CK_AMDFAM10:
2149    defineCPUMacros(Builder, "amdfam10");
2150    break;
2151  case CK_BTVER1:
2152    defineCPUMacros(Builder, "btver1");
2153    break;
2154  case CK_BDVER1:
2155    defineCPUMacros(Builder, "bdver1");
2156    break;
2157  case CK_BDVER2:
2158    defineCPUMacros(Builder, "bdver2");
2159    break;
2160  case CK_Geode:
2161    defineCPUMacros(Builder, "geode");
2162    break;
2163  }
2164
2165  // Target properties.
2166  Builder.defineMacro("__LITTLE_ENDIAN__");
2167  Builder.defineMacro("__REGISTER_PREFIX__", "");
2168
2169  // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
2170  // functions in glibc header files that use FP Stack inline asm which the
2171  // backend can't deal with (PR879).
2172  Builder.defineMacro("__NO_MATH_INLINES");
2173
2174  if (HasAES)
2175    Builder.defineMacro("__AES__");
2176
2177  if (HasPCLMUL)
2178    Builder.defineMacro("__PCLMUL__");
2179
2180  if (HasLZCNT)
2181    Builder.defineMacro("__LZCNT__");
2182
2183  if (HasBMI)
2184    Builder.defineMacro("__BMI__");
2185
2186  if (HasBMI2)
2187    Builder.defineMacro("__BMI2__");
2188
2189  if (HasPOPCNT)
2190    Builder.defineMacro("__POPCNT__");
2191
2192  if (HasSSE4a)
2193    Builder.defineMacro("__SSE4A__");
2194
2195  if (HasFMA4)
2196    Builder.defineMacro("__FMA4__");
2197
2198  if (HasFMA)
2199    Builder.defineMacro("__FMA__");
2200
2201  if (HasXOP)
2202    Builder.defineMacro("__XOP__");
2203
2204  // Each case falls through to the previous one here.
2205  switch (SSELevel) {
2206  case AVX2:
2207    Builder.defineMacro("__AVX2__");
2208  case AVX:
2209    Builder.defineMacro("__AVX__");
2210  case SSE42:
2211    Builder.defineMacro("__SSE4_2__");
2212  case SSE41:
2213    Builder.defineMacro("__SSE4_1__");
2214  case SSSE3:
2215    Builder.defineMacro("__SSSE3__");
2216  case SSE3:
2217    Builder.defineMacro("__SSE3__");
2218  case SSE2:
2219    Builder.defineMacro("__SSE2__");
2220    Builder.defineMacro("__SSE2_MATH__");  // -mfp-math=sse always implied.
2221  case SSE1:
2222    Builder.defineMacro("__SSE__");
2223    Builder.defineMacro("__SSE_MATH__");   // -mfp-math=sse always implied.
2224  case NoSSE:
2225    break;
2226  }
2227
2228  if (Opts.MicrosoftExt && PointerWidth == 32) {
2229    switch (SSELevel) {
2230    case AVX2:
2231    case AVX:
2232    case SSE42:
2233    case SSE41:
2234    case SSSE3:
2235    case SSE3:
2236    case SSE2:
2237      Builder.defineMacro("_M_IX86_FP", Twine(2));
2238      break;
2239    case SSE1:
2240      Builder.defineMacro("_M_IX86_FP", Twine(1));
2241      break;
2242    default:
2243      Builder.defineMacro("_M_IX86_FP", Twine(0));
2244    }
2245  }
2246
2247  // Each case falls through to the previous one here.
2248  switch (MMX3DNowLevel) {
2249  case AMD3DNowAthlon:
2250    Builder.defineMacro("__3dNOW_A__");
2251  case AMD3DNow:
2252    Builder.defineMacro("__3dNOW__");
2253  case MMX:
2254    Builder.defineMacro("__MMX__");
2255  case NoMMX3DNow:
2256    break;
2257  }
2258}
2259
2260bool X86TargetInfo::hasFeature(StringRef Feature) const {
2261  return llvm::StringSwitch<bool>(Feature)
2262      .Case("aes", HasAES)
2263      .Case("avx", SSELevel >= AVX)
2264      .Case("avx2", SSELevel >= AVX2)
2265      .Case("bmi", HasBMI)
2266      .Case("bmi2", HasBMI2)
2267      .Case("fma", HasFMA)
2268      .Case("fma4", HasFMA4)
2269      .Case("lzcnt", HasLZCNT)
2270      .Case("mm3dnow", MMX3DNowLevel >= AMD3DNow)
2271      .Case("mm3dnowa", MMX3DNowLevel >= AMD3DNowAthlon)
2272      .Case("mmx", MMX3DNowLevel >= MMX)
2273      .Case("pclmul", HasPCLMUL)
2274      .Case("popcnt", HasPOPCNT)
2275      .Case("sse", SSELevel >= SSE1)
2276      .Case("sse2", SSELevel >= SSE2)
2277      .Case("sse3", SSELevel >= SSE3)
2278      .Case("ssse3", SSELevel >= SSSE3)
2279      .Case("sse41", SSELevel >= SSE41)
2280      .Case("sse42", SSELevel >= SSE42)
2281      .Case("sse4a", HasSSE4a)
2282      .Case("x86", true)
2283      .Case("x86_32", PointerWidth == 32)
2284      .Case("x86_64", PointerWidth == 64)
2285      .Case("xop", HasXOP)
2286      .Default(false);
2287}
2288
2289bool
2290X86TargetInfo::validateAsmConstraint(const char *&Name,
2291                                     TargetInfo::ConstraintInfo &Info) const {
2292  switch (*Name) {
2293  default: return false;
2294  case 'Y': // first letter of a pair:
2295    switch (*(Name+1)) {
2296    default: return false;
2297    case '0':  // First SSE register.
2298    case 't':  // Any SSE register, when SSE2 is enabled.
2299    case 'i':  // Any SSE register, when SSE2 and inter-unit moves enabled.
2300    case 'm':  // any MMX register, when inter-unit moves enabled.
2301      break;   // falls through to setAllowsRegister.
2302  }
2303  case 'a': // eax.
2304  case 'b': // ebx.
2305  case 'c': // ecx.
2306  case 'd': // edx.
2307  case 'S': // esi.
2308  case 'D': // edi.
2309  case 'A': // edx:eax.
2310  case 'f': // any x87 floating point stack register.
2311  case 't': // top of floating point stack.
2312  case 'u': // second from top of floating point stack.
2313  case 'q': // Any register accessible as [r]l: a, b, c, and d.
2314  case 'y': // Any MMX register.
2315  case 'x': // Any SSE register.
2316  case 'Q': // Any register accessible as [r]h: a, b, c, and d.
2317  case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
2318  case 'l': // "Index" registers: any general register that can be used as an
2319            // index in a base+index memory access.
2320    Info.setAllowsRegister();
2321    return true;
2322  case 'C': // SSE floating point constant.
2323  case 'G': // x87 floating point constant.
2324  case 'e': // 32-bit signed integer constant for use with zero-extending
2325            // x86_64 instructions.
2326  case 'Z': // 32-bit unsigned integer constant for use with zero-extending
2327            // x86_64 instructions.
2328    return true;
2329  }
2330}
2331
2332
2333std::string
2334X86TargetInfo::convertConstraint(const char *&Constraint) const {
2335  switch (*Constraint) {
2336  case 'a': return std::string("{ax}");
2337  case 'b': return std::string("{bx}");
2338  case 'c': return std::string("{cx}");
2339  case 'd': return std::string("{dx}");
2340  case 'S': return std::string("{si}");
2341  case 'D': return std::string("{di}");
2342  case 'p': // address
2343    return std::string("im");
2344  case 't': // top of floating point stack.
2345    return std::string("{st}");
2346  case 'u': // second from top of floating point stack.
2347    return std::string("{st(1)}"); // second from top of floating point stack.
2348  default:
2349    return std::string(1, *Constraint);
2350  }
2351}
2352} // end anonymous namespace
2353
2354namespace {
2355// X86-32 generic target
2356class X86_32TargetInfo : public X86TargetInfo {
2357public:
2358  X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
2359    DoubleAlign = LongLongAlign = 32;
2360    LongDoubleWidth = 96;
2361    LongDoubleAlign = 32;
2362    SuitableAlign = 128;
2363    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2364                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
2365                        "a0:0:64-f80:32:32-n8:16:32-S128";
2366    SizeType = UnsignedInt;
2367    PtrDiffType = SignedInt;
2368    IntPtrType = SignedInt;
2369    RegParmMax = 3;
2370
2371    // Use fpret for all types.
2372    RealTypeUsesObjCFPRet = ((1 << TargetInfo::Float) |
2373                             (1 << TargetInfo::Double) |
2374                             (1 << TargetInfo::LongDouble));
2375
2376    // x86-32 has atomics up to 8 bytes
2377    // FIXME: Check that we actually have cmpxchg8b before setting
2378    // MaxAtomicInlineWidth. (cmpxchg8b is an i586 instruction.)
2379    MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
2380  }
2381  virtual BuiltinVaListKind getBuiltinVaListKind() const {
2382    return TargetInfo::CharPtrBuiltinVaList;
2383  }
2384
2385  int getEHDataRegisterNumber(unsigned RegNo) const {
2386    if (RegNo == 0) return 0;
2387    if (RegNo == 1) return 2;
2388    return -1;
2389  }
2390};
2391} // end anonymous namespace
2392
2393namespace {
2394class NetBSDI386TargetInfo : public NetBSDTargetInfo<X86_32TargetInfo> {
2395public:
2396  NetBSDI386TargetInfo(const std::string &triple) :
2397    NetBSDTargetInfo<X86_32TargetInfo>(triple) {
2398  }
2399
2400  virtual unsigned getFloatEvalMethod() const {
2401    // NetBSD defaults to "double" rounding
2402    return 1;
2403  }
2404};
2405} // end anonymous namespace
2406
2407namespace {
2408class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
2409public:
2410  OpenBSDI386TargetInfo(const std::string& triple) :
2411    OpenBSDTargetInfo<X86_32TargetInfo>(triple) {
2412    SizeType = UnsignedLong;
2413    IntPtrType = SignedLong;
2414    PtrDiffType = SignedLong;
2415  }
2416};
2417} // end anonymous namespace
2418
2419namespace {
2420class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
2421public:
2422  DarwinI386TargetInfo(const std::string& triple) :
2423    DarwinTargetInfo<X86_32TargetInfo>(triple) {
2424    LongDoubleWidth = 128;
2425    LongDoubleAlign = 128;
2426    SuitableAlign = 128;
2427    SizeType = UnsignedLong;
2428    IntPtrType = SignedLong;
2429    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2430                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
2431                        "a0:0:64-f80:128:128-n8:16:32-S128";
2432    HasAlignMac68kSupport = true;
2433  }
2434
2435};
2436} // end anonymous namespace
2437
2438namespace {
2439// x86-32 Windows target
2440class WindowsX86_32TargetInfo : public WindowsTargetInfo<X86_32TargetInfo> {
2441public:
2442  WindowsX86_32TargetInfo(const std::string& triple)
2443    : WindowsTargetInfo<X86_32TargetInfo>(triple) {
2444    TLSSupported = false;
2445    WCharType = UnsignedShort;
2446    DoubleAlign = LongLongAlign = 64;
2447    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2448                        "i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-"
2449                        "v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32";
2450  }
2451  virtual void getTargetDefines(const LangOptions &Opts,
2452                                MacroBuilder &Builder) const {
2453    WindowsTargetInfo<X86_32TargetInfo>::getTargetDefines(Opts, Builder);
2454  }
2455};
2456} // end anonymous namespace
2457
2458namespace {
2459
2460// x86-32 Windows Visual Studio target
2461class VisualStudioWindowsX86_32TargetInfo : public WindowsX86_32TargetInfo {
2462public:
2463  VisualStudioWindowsX86_32TargetInfo(const std::string& triple)
2464    : WindowsX86_32TargetInfo(triple) {
2465    LongDoubleWidth = LongDoubleAlign = 64;
2466    LongDoubleFormat = &llvm::APFloat::IEEEdouble;
2467  }
2468  virtual void getTargetDefines(const LangOptions &Opts,
2469                                MacroBuilder &Builder) const {
2470    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
2471    WindowsX86_32TargetInfo::getVisualStudioDefines(Opts, Builder);
2472    // The value of the following reflects processor type.
2473    // 300=386, 400=486, 500=Pentium, 600=Blend (default)
2474    // We lost the original triple, so we use the default.
2475    Builder.defineMacro("_M_IX86", "600");
2476  }
2477};
2478} // end anonymous namespace
2479
2480namespace {
2481// x86-32 MinGW target
2482class MinGWX86_32TargetInfo : public WindowsX86_32TargetInfo {
2483public:
2484  MinGWX86_32TargetInfo(const std::string& triple)
2485    : WindowsX86_32TargetInfo(triple) {
2486  }
2487  virtual void getTargetDefines(const LangOptions &Opts,
2488                                MacroBuilder &Builder) const {
2489    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
2490    DefineStd(Builder, "WIN32", Opts);
2491    DefineStd(Builder, "WINNT", Opts);
2492    Builder.defineMacro("_X86_");
2493    Builder.defineMacro("__MSVCRT__");
2494    Builder.defineMacro("__MINGW32__");
2495
2496    // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
2497    // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
2498    if (Opts.MicrosoftExt)
2499      // Provide "as-is" __declspec.
2500      Builder.defineMacro("__declspec", "__declspec");
2501    else
2502      // Provide alias of __attribute__ like mingw32-gcc.
2503      Builder.defineMacro("__declspec(a)", "__attribute__((a))");
2504  }
2505};
2506} // end anonymous namespace
2507
2508namespace {
2509// x86-32 Cygwin target
2510class CygwinX86_32TargetInfo : public X86_32TargetInfo {
2511public:
2512  CygwinX86_32TargetInfo(const std::string& triple)
2513    : X86_32TargetInfo(triple) {
2514    TLSSupported = false;
2515    WCharType = UnsignedShort;
2516    DoubleAlign = LongLongAlign = 64;
2517    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2518                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
2519                        "a0:0:64-f80:32:32-n8:16:32-S32";
2520  }
2521  virtual void getTargetDefines(const LangOptions &Opts,
2522                                MacroBuilder &Builder) const {
2523    X86_32TargetInfo::getTargetDefines(Opts, Builder);
2524    Builder.defineMacro("__CYGWIN__");
2525    Builder.defineMacro("__CYGWIN32__");
2526    DefineStd(Builder, "unix", Opts);
2527    if (Opts.CPlusPlus)
2528      Builder.defineMacro("_GNU_SOURCE");
2529  }
2530};
2531} // end anonymous namespace
2532
2533namespace {
2534// x86-32 Haiku target
2535class HaikuX86_32TargetInfo : public X86_32TargetInfo {
2536public:
2537  HaikuX86_32TargetInfo(const std::string& triple)
2538    : X86_32TargetInfo(triple) {
2539    SizeType = UnsignedLong;
2540    IntPtrType = SignedLong;
2541    PtrDiffType = SignedLong;
2542    this->UserLabelPrefix = "";
2543  }
2544  virtual void getTargetDefines(const LangOptions &Opts,
2545                                MacroBuilder &Builder) const {
2546    X86_32TargetInfo::getTargetDefines(Opts, Builder);
2547    Builder.defineMacro("__INTEL__");
2548    Builder.defineMacro("__HAIKU__");
2549  }
2550};
2551} // end anonymous namespace
2552
2553// RTEMS Target
2554template<typename Target>
2555class RTEMSTargetInfo : public OSTargetInfo<Target> {
2556protected:
2557  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
2558                            MacroBuilder &Builder) const {
2559    // RTEMS defines; list based off of gcc output
2560
2561    Builder.defineMacro("__rtems__");
2562    Builder.defineMacro("__ELF__");
2563  }
2564public:
2565  RTEMSTargetInfo(const std::string &triple)
2566    : OSTargetInfo<Target>(triple) {
2567      this->UserLabelPrefix = "";
2568
2569      llvm::Triple Triple(triple);
2570      switch (Triple.getArch()) {
2571        default:
2572        case llvm::Triple::x86:
2573          // this->MCountName = ".mcount";
2574          break;
2575        case llvm::Triple::mips:
2576        case llvm::Triple::mipsel:
2577        case llvm::Triple::ppc:
2578        case llvm::Triple::ppc64:
2579          // this->MCountName = "_mcount";
2580          break;
2581        case llvm::Triple::arm:
2582          // this->MCountName = "__mcount";
2583          break;
2584      }
2585
2586    }
2587};
2588
2589namespace {
2590// x86-32 RTEMS target
2591class RTEMSX86_32TargetInfo : public X86_32TargetInfo {
2592public:
2593  RTEMSX86_32TargetInfo(const std::string& triple)
2594    : X86_32TargetInfo(triple) {
2595    SizeType = UnsignedLong;
2596    IntPtrType = SignedLong;
2597    PtrDiffType = SignedLong;
2598    this->UserLabelPrefix = "";
2599  }
2600  virtual void getTargetDefines(const LangOptions &Opts,
2601                                MacroBuilder &Builder) const {
2602    X86_32TargetInfo::getTargetDefines(Opts, Builder);
2603    Builder.defineMacro("__INTEL__");
2604    Builder.defineMacro("__rtems__");
2605  }
2606};
2607} // end anonymous namespace
2608
2609namespace {
2610// x86-64 generic target
2611class X86_64TargetInfo : public X86TargetInfo {
2612public:
2613  X86_64TargetInfo(const std::string &triple) : X86TargetInfo(triple) {
2614    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
2615    LongDoubleWidth = 128;
2616    LongDoubleAlign = 128;
2617    LargeArrayMinWidth = 128;
2618    LargeArrayAlign = 128;
2619    SuitableAlign = 128;
2620    IntMaxType = SignedLong;
2621    UIntMaxType = UnsignedLong;
2622    Int64Type = SignedLong;
2623    RegParmMax = 6;
2624
2625    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2626                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
2627                        "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128";
2628
2629    // Use fpret only for long double.
2630    RealTypeUsesObjCFPRet = (1 << TargetInfo::LongDouble);
2631
2632    // Use fp2ret for _Complex long double.
2633    ComplexLongDoubleUsesFP2Ret = true;
2634
2635    // x86-64 has atomics up to 16 bytes.
2636    // FIXME: Once the backend is fixed, increase MaxAtomicInlineWidth to 128
2637    // on CPUs with cmpxchg16b
2638    MaxAtomicPromoteWidth = 128;
2639    MaxAtomicInlineWidth = 64;
2640  }
2641  virtual BuiltinVaListKind getBuiltinVaListKind() const {
2642    return TargetInfo::X86_64ABIBuiltinVaList;
2643  }
2644
2645  int getEHDataRegisterNumber(unsigned RegNo) const {
2646    if (RegNo == 0) return 0;
2647    if (RegNo == 1) return 1;
2648    return -1;
2649  }
2650};
2651} // end anonymous namespace
2652
2653namespace {
2654// x86-64 Windows target
2655class WindowsX86_64TargetInfo : public WindowsTargetInfo<X86_64TargetInfo> {
2656public:
2657  WindowsX86_64TargetInfo(const std::string& triple)
2658    : WindowsTargetInfo<X86_64TargetInfo>(triple) {
2659    TLSSupported = false;
2660    WCharType = UnsignedShort;
2661    LongWidth = LongAlign = 32;
2662    DoubleAlign = LongLongAlign = 64;
2663    IntMaxType = SignedLongLong;
2664    UIntMaxType = UnsignedLongLong;
2665    Int64Type = SignedLongLong;
2666    SizeType = UnsignedLongLong;
2667    PtrDiffType = SignedLongLong;
2668    IntPtrType = SignedLongLong;
2669    this->UserLabelPrefix = "";
2670  }
2671  virtual void getTargetDefines(const LangOptions &Opts,
2672                                MacroBuilder &Builder) const {
2673    WindowsTargetInfo<X86_64TargetInfo>::getTargetDefines(Opts, Builder);
2674    Builder.defineMacro("_WIN64");
2675  }
2676  virtual BuiltinVaListKind getBuiltinVaListKind() const {
2677    return TargetInfo::CharPtrBuiltinVaList;
2678  }
2679};
2680} // end anonymous namespace
2681
2682namespace {
2683// x86-64 Windows Visual Studio target
2684class VisualStudioWindowsX86_64TargetInfo : public WindowsX86_64TargetInfo {
2685public:
2686  VisualStudioWindowsX86_64TargetInfo(const std::string& triple)
2687    : WindowsX86_64TargetInfo(triple) {
2688    LongDoubleWidth = LongDoubleAlign = 64;
2689    LongDoubleFormat = &llvm::APFloat::IEEEdouble;
2690  }
2691  virtual void getTargetDefines(const LangOptions &Opts,
2692                                MacroBuilder &Builder) const {
2693    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
2694    WindowsX86_64TargetInfo::getVisualStudioDefines(Opts, Builder);
2695    Builder.defineMacro("_M_X64");
2696    Builder.defineMacro("_M_AMD64");
2697  }
2698};
2699} // end anonymous namespace
2700
2701namespace {
2702// x86-64 MinGW target
2703class MinGWX86_64TargetInfo : public WindowsX86_64TargetInfo {
2704public:
2705  MinGWX86_64TargetInfo(const std::string& triple)
2706    : WindowsX86_64TargetInfo(triple) {
2707  }
2708  virtual void getTargetDefines(const LangOptions &Opts,
2709                                MacroBuilder &Builder) const {
2710    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
2711    DefineStd(Builder, "WIN64", Opts);
2712    Builder.defineMacro("__MSVCRT__");
2713    Builder.defineMacro("__MINGW32__");
2714    Builder.defineMacro("__MINGW64__");
2715
2716    // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
2717    // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
2718    if (Opts.MicrosoftExt)
2719      // Provide "as-is" __declspec.
2720      Builder.defineMacro("__declspec", "__declspec");
2721    else
2722      // Provide alias of __attribute__ like mingw32-gcc.
2723      Builder.defineMacro("__declspec(a)", "__attribute__((a))");
2724  }
2725};
2726} // end anonymous namespace
2727
2728namespace {
2729class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
2730public:
2731  DarwinX86_64TargetInfo(const std::string& triple)
2732      : DarwinTargetInfo<X86_64TargetInfo>(triple) {
2733    Int64Type = SignedLongLong;
2734  }
2735};
2736} // end anonymous namespace
2737
2738namespace {
2739class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
2740public:
2741  OpenBSDX86_64TargetInfo(const std::string& triple)
2742      : OpenBSDTargetInfo<X86_64TargetInfo>(triple) {
2743    IntMaxType = SignedLongLong;
2744    UIntMaxType = UnsignedLongLong;
2745    Int64Type = SignedLongLong;
2746  }
2747};
2748} // end anonymous namespace
2749
2750namespace {
2751class ARMTargetInfo : public TargetInfo {
2752  // Possible FPU choices.
2753  enum FPUMode {
2754    NoFPU,
2755    VFP2FPU,
2756    VFP3FPU,
2757    NeonFPU
2758  };
2759
2760  static bool FPUModeIsVFP(FPUMode Mode) {
2761    return Mode >= VFP2FPU && Mode <= NeonFPU;
2762  }
2763
2764  static const TargetInfo::GCCRegAlias GCCRegAliases[];
2765  static const char * const GCCRegNames[];
2766
2767  std::string ABI, CPU;
2768
2769  unsigned FPU : 3;
2770
2771  unsigned IsThumb : 1;
2772
2773  // Initialized via features.
2774  unsigned SoftFloat : 1;
2775  unsigned SoftFloatABI : 1;
2776
2777  static const Builtin::Info BuiltinInfo[];
2778
2779public:
2780  ARMTargetInfo(const std::string &TripleStr)
2781    : TargetInfo(TripleStr), ABI("aapcs-linux"), CPU("arm1136j-s")
2782  {
2783    BigEndian = false;
2784    SizeType = UnsignedInt;
2785    PtrDiffType = SignedInt;
2786    // AAPCS 7.1.1, ARM-Linux ABI 2.4: type of wchar_t is unsigned int.
2787    WCharType = UnsignedInt;
2788
2789    // {} in inline assembly are neon specifiers, not assembly variant
2790    // specifiers.
2791    NoAsmVariants = true;
2792
2793    // FIXME: Should we just treat this as a feature?
2794    IsThumb = getTriple().getArchName().startswith("thumb");
2795    if (IsThumb) {
2796      // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
2797      // so set preferred for small types to 32.
2798      DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
2799                           "i64:64:64-f32:32:32-f64:64:64-"
2800                           "v64:64:64-v128:64:128-a0:0:32-n32-S64");
2801    } else {
2802      DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2803                           "i64:64:64-f32:32:32-f64:64:64-"
2804                           "v64:64:64-v128:64:128-a0:0:64-n32-S64");
2805    }
2806
2807    // ARM targets default to using the ARM C++ ABI.
2808    CXXABI = CXXABI_ARM;
2809
2810    // ARM has atomics up to 8 bytes
2811    // FIXME: Set MaxAtomicInlineWidth if we have the feature v6e
2812    MaxAtomicPromoteWidth = 64;
2813
2814    // Do force alignment of members that follow zero length bitfields.  If
2815    // the alignment of the zero-length bitfield is greater than the member
2816    // that follows it, `bar', `bar' will be aligned as the  type of the
2817    // zero length bitfield.
2818    UseZeroLengthBitfieldAlignment = true;
2819  }
2820  virtual const char *getABI() const { return ABI.c_str(); }
2821  virtual bool setABI(const std::string &Name) {
2822    ABI = Name;
2823
2824    // The defaults (above) are for AAPCS, check if we need to change them.
2825    //
2826    // FIXME: We need support for -meabi... we could just mangle it into the
2827    // name.
2828    if (Name == "apcs-gnu") {
2829      DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 32;
2830      SizeType = UnsignedLong;
2831
2832      // Revert to using SignedInt on apcs-gnu to comply with existing behaviour.
2833      WCharType = SignedInt;
2834
2835      // Do not respect the alignment of bit-field types when laying out
2836      // structures. This corresponds to PCC_BITFIELD_TYPE_MATTERS in gcc.
2837      UseBitFieldTypeAlignment = false;
2838
2839      /// gcc forces the alignment to 4 bytes, regardless of the type of the
2840      /// zero length bitfield.  This corresponds to EMPTY_FIELD_BOUNDARY in
2841      /// gcc.
2842      ZeroLengthBitfieldBoundary = 32;
2843
2844      if (IsThumb) {
2845        // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
2846        // so set preferred for small types to 32.
2847        DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
2848                             "i64:32:64-f32:32:32-f64:32:64-"
2849                             "v64:32:64-v128:32:128-a0:0:32-n32-S32");
2850      } else {
2851        DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2852                             "i64:32:64-f32:32:32-f64:32:64-"
2853                             "v64:32:64-v128:32:128-a0:0:32-n32-S32");
2854      }
2855
2856      // FIXME: Override "preferred align" for double and long long.
2857    } else if (Name == "aapcs") {
2858      // FIXME: Enumerated types are variable width in straight AAPCS.
2859    } else if (Name == "aapcs-linux") {
2860      ;
2861    } else
2862      return false;
2863
2864    return true;
2865  }
2866
2867  void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
2868    if (CPU == "arm1136jf-s" || CPU == "arm1176jzf-s" || CPU == "mpcore")
2869      Features["vfp2"] = true;
2870    else if (CPU == "cortex-a8" || CPU == "cortex-a9")
2871      Features["neon"] = true;
2872  }
2873
2874  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
2875                                 StringRef Name,
2876                                 bool Enabled) const {
2877    if (Name == "soft-float" || Name == "soft-float-abi" ||
2878        Name == "vfp2" || Name == "vfp3" || Name == "neon" || Name == "d16" ||
2879        Name == "neonfp") {
2880      Features[Name] = Enabled;
2881    } else
2882      return false;
2883
2884    return true;
2885  }
2886
2887  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
2888    FPU = NoFPU;
2889    SoftFloat = SoftFloatABI = false;
2890    for (unsigned i = 0, e = Features.size(); i != e; ++i) {
2891      if (Features[i] == "+soft-float")
2892        SoftFloat = true;
2893      else if (Features[i] == "+soft-float-abi")
2894        SoftFloatABI = true;
2895      else if (Features[i] == "+vfp2")
2896        FPU = VFP2FPU;
2897      else if (Features[i] == "+vfp3")
2898        FPU = VFP3FPU;
2899      else if (Features[i] == "+neon")
2900        FPU = NeonFPU;
2901    }
2902
2903    // Remove front-end specific options which the backend handles differently.
2904    std::vector<std::string>::iterator it;
2905    it = std::find(Features.begin(), Features.end(), "+soft-float");
2906    if (it != Features.end())
2907      Features.erase(it);
2908    it = std::find(Features.begin(), Features.end(), "+soft-float-abi");
2909    if (it != Features.end())
2910      Features.erase(it);
2911  }
2912
2913  virtual bool hasFeature(StringRef Feature) const {
2914    return llvm::StringSwitch<bool>(Feature)
2915        .Case("arm", true)
2916        .Case("softfloat", SoftFloat)
2917        .Case("thumb", IsThumb)
2918        .Case("neon", FPU == NeonFPU && !SoftFloat &&
2919              StringRef(getCPUDefineSuffix(CPU)).startswith("7"))
2920        .Default(false);
2921  }
2922  static const char *getCPUDefineSuffix(StringRef Name) {
2923    return llvm::StringSwitch<const char*>(Name)
2924      .Cases("arm8", "arm810", "4")
2925      .Cases("strongarm", "strongarm110", "strongarm1100", "strongarm1110", "4")
2926      .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "arm720t", "arm9", "4T")
2927      .Cases("arm9tdmi", "arm920", "arm920t", "arm922t", "arm940t", "4T")
2928      .Case("ep9312", "4T")
2929      .Cases("arm10tdmi", "arm1020t", "5T")
2930      .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "5TE")
2931      .Case("arm926ej-s", "5TEJ")
2932      .Cases("arm10e", "arm1020e", "arm1022e", "5TE")
2933      .Cases("xscale", "iwmmxt", "5TE")
2934      .Case("arm1136j-s", "6J")
2935      .Cases("arm1176jz-s", "arm1176jzf-s", "6ZK")
2936      .Cases("arm1136jf-s", "mpcorenovfp", "mpcore", "6K")
2937      .Cases("arm1156t2-s", "arm1156t2f-s", "6T2")
2938      .Cases("cortex-a8", "cortex-a9", "7A")
2939      .Case("cortex-m3", "7M")
2940      .Case("cortex-m4", "7M")
2941      .Case("cortex-m0", "6M")
2942      .Default(0);
2943  }
2944  virtual bool setCPU(const std::string &Name) {
2945    if (!getCPUDefineSuffix(Name))
2946      return false;
2947
2948    CPU = Name;
2949    return true;
2950  }
2951  virtual void getTargetDefines(const LangOptions &Opts,
2952                                MacroBuilder &Builder) const {
2953    // Target identification.
2954    Builder.defineMacro("__arm");
2955    Builder.defineMacro("__arm__");
2956
2957    // Target properties.
2958    Builder.defineMacro("__ARMEL__");
2959    Builder.defineMacro("__LITTLE_ENDIAN__");
2960    Builder.defineMacro("__REGISTER_PREFIX__", "");
2961
2962    StringRef CPUArch = getCPUDefineSuffix(CPU);
2963    Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
2964
2965    // Subtarget options.
2966
2967    // FIXME: It's more complicated than this and we don't really support
2968    // interworking.
2969    if ('5' <= CPUArch[0] && CPUArch[0] <= '7')
2970      Builder.defineMacro("__THUMB_INTERWORK__");
2971
2972    if (ABI == "aapcs" || ABI == "aapcs-linux")
2973      Builder.defineMacro("__ARM_EABI__");
2974
2975    if (SoftFloat)
2976      Builder.defineMacro("__SOFTFP__");
2977
2978    if (CPU == "xscale")
2979      Builder.defineMacro("__XSCALE__");
2980
2981    bool IsARMv7 = CPUArch.startswith("7");
2982    if (IsThumb) {
2983      Builder.defineMacro("__THUMBEL__");
2984      Builder.defineMacro("__thumb__");
2985      if (CPUArch == "6T2" || IsARMv7)
2986        Builder.defineMacro("__thumb2__");
2987    }
2988
2989    // Note, this is always on in gcc, even though it doesn't make sense.
2990    Builder.defineMacro("__APCS_32__");
2991
2992    if (FPUModeIsVFP((FPUMode) FPU))
2993      Builder.defineMacro("__VFP_FP__");
2994
2995    // This only gets set when Neon instructions are actually available, unlike
2996    // the VFP define, hence the soft float and arch check. This is subtly
2997    // different from gcc, we follow the intent which was that it should be set
2998    // when Neon instructions are actually available.
2999    if (FPU == NeonFPU && !SoftFloat && IsARMv7)
3000      Builder.defineMacro("__ARM_NEON__");
3001  }
3002  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3003                                 unsigned &NumRecords) const {
3004    Records = BuiltinInfo;
3005    NumRecords = clang::ARM::LastTSBuiltin-Builtin::FirstTSBuiltin;
3006  }
3007  virtual bool isCLZForZeroUndef() const { return false; }
3008  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3009    return TargetInfo::VoidPtrBuiltinVaList;
3010  }
3011  virtual void getGCCRegNames(const char * const *&Names,
3012                              unsigned &NumNames) const;
3013  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3014                                unsigned &NumAliases) const;
3015  virtual bool validateAsmConstraint(const char *&Name,
3016                                     TargetInfo::ConstraintInfo &Info) const {
3017    // FIXME: Check if this is complete
3018    switch (*Name) {
3019    default:
3020    case 'l': // r0-r7
3021    case 'h': // r8-r15
3022    case 'w': // VFP Floating point register single precision
3023    case 'P': // VFP Floating point register double precision
3024      Info.setAllowsRegister();
3025      return true;
3026    case 'Q': // A memory address that is a single base register.
3027      Info.setAllowsMemory();
3028      return true;
3029    case 'U': // a memory reference...
3030      switch (Name[1]) {
3031      case 'q': // ...ARMV4 ldrsb
3032      case 'v': // ...VFP load/store (reg+constant offset)
3033      case 'y': // ...iWMMXt load/store
3034      case 't': // address valid for load/store opaque types wider
3035	        // than 128-bits
3036      case 'n': // valid address for Neon doubleword vector load/store
3037      case 'm': // valid address for Neon element and structure load/store
3038      case 's': // valid address for non-offset loads/stores of quad-word
3039	        // values in four ARM registers
3040        Info.setAllowsMemory();
3041        Name++;
3042        return true;
3043      }
3044    }
3045    return false;
3046  }
3047  virtual std::string convertConstraint(const char *&Constraint) const {
3048    std::string R;
3049    switch (*Constraint) {
3050    case 'U':   // Two-character constraint; add "^" hint for later parsing.
3051      R = std::string("^") + std::string(Constraint, 2);
3052      Constraint++;
3053      break;
3054    case 'p': // 'p' should be translated to 'r' by default.
3055      R = std::string("r");
3056      break;
3057    default:
3058      return std::string(1, *Constraint);
3059    }
3060    return R;
3061  }
3062  virtual const char *getClobbers() const {
3063    // FIXME: Is this really right?
3064    return "";
3065  }
3066};
3067
3068const char * const ARMTargetInfo::GCCRegNames[] = {
3069  // Integer registers
3070  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3071  "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
3072
3073  // Float registers
3074  "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
3075  "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
3076  "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
3077  "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
3078
3079  // Double registers
3080  "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
3081  "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
3082  "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
3083  "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
3084
3085  // Quad registers
3086  "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
3087  "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"
3088};
3089
3090void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
3091                                   unsigned &NumNames) const {
3092  Names = GCCRegNames;
3093  NumNames = llvm::array_lengthof(GCCRegNames);
3094}
3095
3096const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
3097  { { "a1" }, "r0" },
3098  { { "a2" }, "r1" },
3099  { { "a3" }, "r2" },
3100  { { "a4" }, "r3" },
3101  { { "v1" }, "r4" },
3102  { { "v2" }, "r5" },
3103  { { "v3" }, "r6" },
3104  { { "v4" }, "r7" },
3105  { { "v5" }, "r8" },
3106  { { "v6", "rfp" }, "r9" },
3107  { { "sl" }, "r10" },
3108  { { "fp" }, "r11" },
3109  { { "ip" }, "r12" },
3110  { { "r13" }, "sp" },
3111  { { "r14" }, "lr" },
3112  { { "r15" }, "pc" },
3113  // The S, D and Q registers overlap, but aren't really aliases; we
3114  // don't want to substitute one of these for a different-sized one.
3115};
3116
3117void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3118                                       unsigned &NumAliases) const {
3119  Aliases = GCCRegAliases;
3120  NumAliases = llvm::array_lengthof(GCCRegAliases);
3121}
3122
3123const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
3124#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3125#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3126                                              ALL_LANGUAGES },
3127#include "clang/Basic/BuiltinsARM.def"
3128};
3129} // end anonymous namespace.
3130
3131namespace {
3132class DarwinARMTargetInfo :
3133  public DarwinTargetInfo<ARMTargetInfo> {
3134protected:
3135  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
3136                            MacroBuilder &Builder) const {
3137    getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
3138  }
3139
3140public:
3141  DarwinARMTargetInfo(const std::string& triple)
3142    : DarwinTargetInfo<ARMTargetInfo>(triple) {
3143    HasAlignMac68kSupport = true;
3144    // iOS always has 64-bit atomic instructions.
3145    // FIXME: This should be based off of the target features in ARMTargetInfo.
3146    MaxAtomicInlineWidth = 64;
3147  }
3148};
3149} // end anonymous namespace.
3150
3151
3152namespace {
3153// Hexagon abstract base class
3154class HexagonTargetInfo : public TargetInfo {
3155  static const Builtin::Info BuiltinInfo[];
3156  static const char * const GCCRegNames[];
3157  static const TargetInfo::GCCRegAlias GCCRegAliases[];
3158  std::string CPU;
3159public:
3160  HexagonTargetInfo(const std::string& triple) : TargetInfo(triple)  {
3161    BigEndian = false;
3162    DescriptionString = ("e-p:32:32:32-"
3163                         "i64:64:64-i32:32:32-i16:16:16-i1:32:32"
3164                         "f64:64:64-f32:32:32-a0:0-n32");
3165
3166    // {} in inline assembly are packet specifiers, not assembly variant
3167    // specifiers.
3168    NoAsmVariants = true;
3169  }
3170
3171  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3172                                 unsigned &NumRecords) const {
3173    Records = BuiltinInfo;
3174    NumRecords = clang::Hexagon::LastTSBuiltin-Builtin::FirstTSBuiltin;
3175  }
3176
3177  virtual bool validateAsmConstraint(const char *&Name,
3178                                     TargetInfo::ConstraintInfo &Info) const {
3179    return true;
3180  }
3181
3182  virtual void getTargetDefines(const LangOptions &Opts,
3183                                MacroBuilder &Builder) const;
3184
3185  virtual bool hasFeature(StringRef Feature) const {
3186    return Feature == "hexagon";
3187  }
3188
3189  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3190    return TargetInfo::CharPtrBuiltinVaList;
3191  }
3192  virtual void getGCCRegNames(const char * const *&Names,
3193                              unsigned &NumNames) const;
3194  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3195                                unsigned &NumAliases) const;
3196  virtual const char *getClobbers() const {
3197    return "";
3198  }
3199
3200  static const char *getHexagonCPUSuffix(StringRef Name) {
3201    return llvm::StringSwitch<const char*>(Name)
3202      .Case("hexagonv2", "2")
3203      .Case("hexagonv3", "3")
3204      .Case("hexagonv4", "4")
3205      .Case("hexagonv5", "5")
3206      .Default(0);
3207  }
3208
3209  virtual bool setCPU(const std::string &Name) {
3210    if (!getHexagonCPUSuffix(Name))
3211      return false;
3212
3213    CPU = Name;
3214    return true;
3215  }
3216};
3217
3218void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts,
3219                                MacroBuilder &Builder) const {
3220  Builder.defineMacro("qdsp6");
3221  Builder.defineMacro("__qdsp6", "1");
3222  Builder.defineMacro("__qdsp6__", "1");
3223
3224  Builder.defineMacro("hexagon");
3225  Builder.defineMacro("__hexagon", "1");
3226  Builder.defineMacro("__hexagon__", "1");
3227
3228  if(CPU == "hexagonv1") {
3229    Builder.defineMacro("__HEXAGON_V1__");
3230    Builder.defineMacro("__HEXAGON_ARCH__", "1");
3231    if(Opts.HexagonQdsp6Compat) {
3232      Builder.defineMacro("__QDSP6_V1__");
3233      Builder.defineMacro("__QDSP6_ARCH__", "1");
3234    }
3235  }
3236  else if(CPU == "hexagonv2") {
3237    Builder.defineMacro("__HEXAGON_V2__");
3238    Builder.defineMacro("__HEXAGON_ARCH__", "2");
3239    if(Opts.HexagonQdsp6Compat) {
3240      Builder.defineMacro("__QDSP6_V2__");
3241      Builder.defineMacro("__QDSP6_ARCH__", "2");
3242    }
3243  }
3244  else if(CPU == "hexagonv3") {
3245    Builder.defineMacro("__HEXAGON_V3__");
3246    Builder.defineMacro("__HEXAGON_ARCH__", "3");
3247    if(Opts.HexagonQdsp6Compat) {
3248      Builder.defineMacro("__QDSP6_V3__");
3249      Builder.defineMacro("__QDSP6_ARCH__", "3");
3250    }
3251  }
3252  else if(CPU == "hexagonv4") {
3253    Builder.defineMacro("__HEXAGON_V4__");
3254    Builder.defineMacro("__HEXAGON_ARCH__", "4");
3255    if(Opts.HexagonQdsp6Compat) {
3256      Builder.defineMacro("__QDSP6_V4__");
3257      Builder.defineMacro("__QDSP6_ARCH__", "4");
3258    }
3259  }
3260  else if(CPU == "hexagonv5") {
3261    Builder.defineMacro("__HEXAGON_V5__");
3262    Builder.defineMacro("__HEXAGON_ARCH__", "5");
3263    if(Opts.HexagonQdsp6Compat) {
3264      Builder.defineMacro("__QDSP6_V5__");
3265      Builder.defineMacro("__QDSP6_ARCH__", "5");
3266    }
3267  }
3268}
3269
3270const char * const HexagonTargetInfo::GCCRegNames[] = {
3271  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3272  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
3273  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
3274  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
3275  "p0", "p1", "p2", "p3",
3276  "sa0", "lc0", "sa1", "lc1", "m0", "m1", "usr", "ugp"
3277};
3278
3279void HexagonTargetInfo::getGCCRegNames(const char * const *&Names,
3280                                   unsigned &NumNames) const {
3281  Names = GCCRegNames;
3282  NumNames = llvm::array_lengthof(GCCRegNames);
3283}
3284
3285
3286const TargetInfo::GCCRegAlias HexagonTargetInfo::GCCRegAliases[] = {
3287  { { "sp" }, "r29" },
3288  { { "fp" }, "r30" },
3289  { { "lr" }, "r31" },
3290 };
3291
3292void HexagonTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3293                                     unsigned &NumAliases) const {
3294  Aliases = GCCRegAliases;
3295  NumAliases = llvm::array_lengthof(GCCRegAliases);
3296}
3297
3298
3299const Builtin::Info HexagonTargetInfo::BuiltinInfo[] = {
3300#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3301#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3302                                              ALL_LANGUAGES },
3303#include "clang/Basic/BuiltinsHexagon.def"
3304};
3305}
3306
3307
3308namespace {
3309class SparcV8TargetInfo : public TargetInfo {
3310  static const TargetInfo::GCCRegAlias GCCRegAliases[];
3311  static const char * const GCCRegNames[];
3312  bool SoftFloat;
3313public:
3314  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
3315    // FIXME: Support Sparc quad-precision long double?
3316    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3317                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
3318  }
3319  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
3320                                 StringRef Name,
3321                                 bool Enabled) const {
3322    if (Name == "soft-float")
3323      Features[Name] = Enabled;
3324    else
3325      return false;
3326
3327    return true;
3328  }
3329  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
3330    SoftFloat = false;
3331    for (unsigned i = 0, e = Features.size(); i != e; ++i)
3332      if (Features[i] == "+soft-float")
3333        SoftFloat = true;
3334  }
3335  virtual void getTargetDefines(const LangOptions &Opts,
3336                                MacroBuilder &Builder) const {
3337    DefineStd(Builder, "sparc", Opts);
3338    Builder.defineMacro("__sparcv8");
3339    Builder.defineMacro("__REGISTER_PREFIX__", "");
3340
3341    if (SoftFloat)
3342      Builder.defineMacro("SOFT_FLOAT", "1");
3343  }
3344
3345  virtual bool hasFeature(StringRef Feature) const {
3346    return llvm::StringSwitch<bool>(Feature)
3347             .Case("softfloat", SoftFloat)
3348             .Case("sparc", true)
3349             .Default(false);
3350  }
3351
3352  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3353                                 unsigned &NumRecords) const {
3354    // FIXME: Implement!
3355  }
3356  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3357    return TargetInfo::VoidPtrBuiltinVaList;
3358  }
3359  virtual void getGCCRegNames(const char * const *&Names,
3360                              unsigned &NumNames) const;
3361  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3362                                unsigned &NumAliases) const;
3363  virtual bool validateAsmConstraint(const char *&Name,
3364                                     TargetInfo::ConstraintInfo &info) const {
3365    // FIXME: Implement!
3366    return false;
3367  }
3368  virtual const char *getClobbers() const {
3369    // FIXME: Implement!
3370    return "";
3371  }
3372};
3373
3374const char * const SparcV8TargetInfo::GCCRegNames[] = {
3375  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3376  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
3377  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
3378  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
3379};
3380
3381void SparcV8TargetInfo::getGCCRegNames(const char * const *&Names,
3382                                       unsigned &NumNames) const {
3383  Names = GCCRegNames;
3384  NumNames = llvm::array_lengthof(GCCRegNames);
3385}
3386
3387const TargetInfo::GCCRegAlias SparcV8TargetInfo::GCCRegAliases[] = {
3388  { { "g0" }, "r0" },
3389  { { "g1" }, "r1" },
3390  { { "g2" }, "r2" },
3391  { { "g3" }, "r3" },
3392  { { "g4" }, "r4" },
3393  { { "g5" }, "r5" },
3394  { { "g6" }, "r6" },
3395  { { "g7" }, "r7" },
3396  { { "o0" }, "r8" },
3397  { { "o1" }, "r9" },
3398  { { "o2" }, "r10" },
3399  { { "o3" }, "r11" },
3400  { { "o4" }, "r12" },
3401  { { "o5" }, "r13" },
3402  { { "o6", "sp" }, "r14" },
3403  { { "o7" }, "r15" },
3404  { { "l0" }, "r16" },
3405  { { "l1" }, "r17" },
3406  { { "l2" }, "r18" },
3407  { { "l3" }, "r19" },
3408  { { "l4" }, "r20" },
3409  { { "l5" }, "r21" },
3410  { { "l6" }, "r22" },
3411  { { "l7" }, "r23" },
3412  { { "i0" }, "r24" },
3413  { { "i1" }, "r25" },
3414  { { "i2" }, "r26" },
3415  { { "i3" }, "r27" },
3416  { { "i4" }, "r28" },
3417  { { "i5" }, "r29" },
3418  { { "i6", "fp" }, "r30" },
3419  { { "i7" }, "r31" },
3420};
3421
3422void SparcV8TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3423                                         unsigned &NumAliases) const {
3424  Aliases = GCCRegAliases;
3425  NumAliases = llvm::array_lengthof(GCCRegAliases);
3426}
3427} // end anonymous namespace.
3428
3429namespace {
3430class AuroraUXSparcV8TargetInfo : public AuroraUXTargetInfo<SparcV8TargetInfo> {
3431public:
3432  AuroraUXSparcV8TargetInfo(const std::string& triple) :
3433      AuroraUXTargetInfo<SparcV8TargetInfo>(triple) {
3434    SizeType = UnsignedInt;
3435    PtrDiffType = SignedInt;
3436  }
3437};
3438class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
3439public:
3440  SolarisSparcV8TargetInfo(const std::string& triple) :
3441      SolarisTargetInfo<SparcV8TargetInfo>(triple) {
3442    SizeType = UnsignedInt;
3443    PtrDiffType = SignedInt;
3444  }
3445};
3446} // end anonymous namespace.
3447
3448namespace {
3449  class MSP430TargetInfo : public TargetInfo {
3450    static const char * const GCCRegNames[];
3451  public:
3452    MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
3453      BigEndian = false;
3454      TLSSupported = false;
3455      IntWidth = 16; IntAlign = 16;
3456      LongWidth = 32; LongLongWidth = 64;
3457      LongAlign = LongLongAlign = 16;
3458      PointerWidth = 16; PointerAlign = 16;
3459      SuitableAlign = 16;
3460      SizeType = UnsignedInt;
3461      IntMaxType = SignedLong;
3462      UIntMaxType = UnsignedLong;
3463      IntPtrType = SignedShort;
3464      PtrDiffType = SignedInt;
3465      SigAtomicType = SignedLong;
3466      DescriptionString = "e-p:16:16:16-i8:8:8-i16:16:16-i32:16:32-n8:16";
3467   }
3468    virtual void getTargetDefines(const LangOptions &Opts,
3469                                  MacroBuilder &Builder) const {
3470      Builder.defineMacro("MSP430");
3471      Builder.defineMacro("__MSP430__");
3472      // FIXME: defines for different 'flavours' of MCU
3473    }
3474    virtual void getTargetBuiltins(const Builtin::Info *&Records,
3475                                   unsigned &NumRecords) const {
3476     // FIXME: Implement.
3477      Records = 0;
3478      NumRecords = 0;
3479    }
3480    virtual bool hasFeature(StringRef Feature) const {
3481      return Feature == "msp430";
3482    }
3483    virtual void getGCCRegNames(const char * const *&Names,
3484                                unsigned &NumNames) const;
3485    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3486                                  unsigned &NumAliases) const {
3487      // No aliases.
3488      Aliases = 0;
3489      NumAliases = 0;
3490    }
3491    virtual bool validateAsmConstraint(const char *&Name,
3492                                       TargetInfo::ConstraintInfo &info) const {
3493      // No target constraints for now.
3494      return false;
3495    }
3496    virtual const char *getClobbers() const {
3497      // FIXME: Is this really right?
3498      return "";
3499    }
3500    virtual BuiltinVaListKind getBuiltinVaListKind() const {
3501      // FIXME: implement
3502      return TargetInfo::CharPtrBuiltinVaList;
3503   }
3504  };
3505
3506  const char * const MSP430TargetInfo::GCCRegNames[] = {
3507    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3508    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3509  };
3510
3511  void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
3512                                        unsigned &NumNames) const {
3513    Names = GCCRegNames;
3514    NumNames = llvm::array_lengthof(GCCRegNames);
3515  }
3516}
3517
3518namespace {
3519
3520  // LLVM and Clang cannot be used directly to output native binaries for
3521  // target, but is used to compile C code to llvm bitcode with correct
3522  // type and alignment information.
3523  //
3524  // TCE uses the llvm bitcode as input and uses it for generating customized
3525  // target processor and program binary. TCE co-design environment is
3526  // publicly available in http://tce.cs.tut.fi
3527
3528  static const unsigned TCEOpenCLAddrSpaceMap[] = {
3529      3, // opencl_global
3530      4, // opencl_local
3531      5, // opencl_constant
3532      0, // cuda_device
3533      0, // cuda_constant
3534      0  // cuda_shared
3535  };
3536
3537  class TCETargetInfo : public TargetInfo{
3538  public:
3539    TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
3540      TLSSupported = false;
3541      IntWidth = 32;
3542      LongWidth = LongLongWidth = 32;
3543      PointerWidth = 32;
3544      IntAlign = 32;
3545      LongAlign = LongLongAlign = 32;
3546      PointerAlign = 32;
3547      SuitableAlign = 32;
3548      SizeType = UnsignedInt;
3549      IntMaxType = SignedLong;
3550      UIntMaxType = UnsignedLong;
3551      IntPtrType = SignedInt;
3552      PtrDiffType = SignedInt;
3553      FloatWidth = 32;
3554      FloatAlign = 32;
3555      DoubleWidth = 32;
3556      DoubleAlign = 32;
3557      LongDoubleWidth = 32;
3558      LongDoubleAlign = 32;
3559      FloatFormat = &llvm::APFloat::IEEEsingle;
3560      DoubleFormat = &llvm::APFloat::IEEEsingle;
3561      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
3562      DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-"
3563                          "i16:16:32-i32:32:32-i64:32:32-"
3564                          "f32:32:32-f64:32:32-v64:32:32-"
3565                          "v128:32:32-a0:0:32-n32";
3566      AddrSpaceMap = &TCEOpenCLAddrSpaceMap;
3567    }
3568
3569    virtual void getTargetDefines(const LangOptions &Opts,
3570                                  MacroBuilder &Builder) const {
3571      DefineStd(Builder, "tce", Opts);
3572      Builder.defineMacro("__TCE__");
3573      Builder.defineMacro("__TCE_V1__");
3574    }
3575    virtual bool hasFeature(StringRef Feature) const {
3576      return Feature == "tce";
3577    }
3578
3579    virtual void getTargetBuiltins(const Builtin::Info *&Records,
3580                                   unsigned &NumRecords) const {}
3581    virtual const char *getClobbers() const {
3582      return "";
3583    }
3584    virtual BuiltinVaListKind getBuiltinVaListKind() const {
3585      return TargetInfo::VoidPtrBuiltinVaList;
3586    }
3587    virtual void getGCCRegNames(const char * const *&Names,
3588                                unsigned &NumNames) const {}
3589    virtual bool validateAsmConstraint(const char *&Name,
3590                                       TargetInfo::ConstraintInfo &info) const {
3591      return true;
3592    }
3593    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3594                                  unsigned &NumAliases) const {}
3595  };
3596}
3597
3598namespace {
3599class MipsTargetInfoBase : public TargetInfo {
3600  static const Builtin::Info BuiltinInfo[];
3601  std::string CPU;
3602  bool SoftFloat;
3603  bool SingleFloat;
3604
3605protected:
3606  std::string ABI;
3607
3608public:
3609  MipsTargetInfoBase(const std::string& triple,
3610                     const std::string& ABIStr,
3611                     const std::string& CPUStr)
3612    : TargetInfo(triple),
3613      CPU(CPUStr),
3614      SoftFloat(false), SingleFloat(false),
3615      ABI(ABIStr)
3616  {}
3617
3618  virtual const char *getABI() const { return ABI.c_str(); }
3619  virtual bool setABI(const std::string &Name) = 0;
3620  virtual bool setCPU(const std::string &Name) {
3621    CPU = Name;
3622    return true;
3623  }
3624  void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
3625    Features[ABI] = true;
3626    Features[CPU] = true;
3627  }
3628
3629  virtual void getArchDefines(const LangOptions &Opts,
3630                              MacroBuilder &Builder) const {
3631    if (SoftFloat && SingleFloat)
3632      llvm_unreachable("Invalid float ABI for Mips.");
3633    else if (SoftFloat)
3634      Builder.defineMacro("__mips_soft_float", Twine(1));
3635    else {
3636      Builder.defineMacro("__mips_hard_float", Twine(1));
3637      if (SingleFloat)
3638        Builder.defineMacro("__mips_single_float", Twine(1));
3639    }
3640
3641    Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(0)));
3642    Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
3643    Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
3644  }
3645
3646  virtual void getTargetDefines(const LangOptions &Opts,
3647                                MacroBuilder &Builder) const = 0;
3648  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3649                                 unsigned &NumRecords) const {
3650    Records = BuiltinInfo;
3651    NumRecords = clang::Mips::LastTSBuiltin - Builtin::FirstTSBuiltin;
3652  }
3653  virtual bool hasFeature(StringRef Feature) const {
3654    return Feature == "mips";
3655  }
3656  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3657    return TargetInfo::VoidPtrBuiltinVaList;
3658  }
3659  virtual void getGCCRegNames(const char * const *&Names,
3660                              unsigned &NumNames) const {
3661    static const char * const GCCRegNames[] = {
3662      // CPU register names
3663      // Must match second column of GCCRegAliases
3664      "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
3665      "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
3666      "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
3667      "$24",  "$25",  "$26",  "$27",  "$28",  "$29",  "$30",  "$31",
3668      // Floating point register names
3669      "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
3670      "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
3671      "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
3672      "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
3673      // Hi/lo and condition register names
3674      "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
3675      "$fcc5","$fcc6","$fcc7"
3676    };
3677    Names = GCCRegNames;
3678    NumNames = llvm::array_lengthof(GCCRegNames);
3679  }
3680  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3681                                unsigned &NumAliases) const = 0;
3682  virtual bool validateAsmConstraint(const char *&Name,
3683                                     TargetInfo::ConstraintInfo &Info) const {
3684    switch (*Name) {
3685    default:
3686      return false;
3687
3688    case 'r': // CPU registers.
3689    case 'd': // Equivalent to "r" unless generating MIPS16 code.
3690    case 'y': // Equivalent to "r", backwards compatibility only.
3691    case 'f': // floating-point registers.
3692    case 'c': // $25 for indirect jumps
3693    case 'l': // lo register
3694    case 'x': // hilo register pair
3695      Info.setAllowsRegister();
3696      return true;
3697    }
3698  }
3699
3700  virtual const char *getClobbers() const {
3701    // FIXME: Implement!
3702    return "";
3703  }
3704
3705  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
3706                                 StringRef Name,
3707                                 bool Enabled) const {
3708    if (Name == "soft-float" || Name == "single-float" ||
3709        Name == "o32" || Name == "n32" || Name == "n64" || Name == "eabi" ||
3710        Name == "mips32" || Name == "mips32r2" ||
3711        Name == "mips64" || Name == "mips64r2") {
3712      Features[Name] = Enabled;
3713      return true;
3714    }
3715    return false;
3716  }
3717
3718  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
3719    SoftFloat = false;
3720    SingleFloat = false;
3721
3722    for (std::vector<std::string>::iterator it = Features.begin(),
3723         ie = Features.end(); it != ie; ++it) {
3724      if (*it == "+single-float") {
3725        SingleFloat = true;
3726        break;
3727      }
3728
3729      if (*it == "+soft-float") {
3730        SoftFloat = true;
3731        // This option is front-end specific.
3732        // Do not need to pass it to the backend.
3733        Features.erase(it);
3734        break;
3735      }
3736    }
3737  }
3738};
3739
3740const Builtin::Info MipsTargetInfoBase::BuiltinInfo[] = {
3741#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3742#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3743                                              ALL_LANGUAGES },
3744#include "clang/Basic/BuiltinsMips.def"
3745};
3746
3747class Mips32TargetInfoBase : public MipsTargetInfoBase {
3748public:
3749  Mips32TargetInfoBase(const std::string& triple) :
3750    MipsTargetInfoBase(triple, "o32", "mips32") {
3751    SizeType = UnsignedInt;
3752    PtrDiffType = SignedInt;
3753  }
3754  virtual bool setABI(const std::string &Name) {
3755    if ((Name == "o32") || (Name == "eabi")) {
3756      ABI = Name;
3757      return true;
3758    } else
3759      return false;
3760  }
3761  virtual void getArchDefines(const LangOptions &Opts,
3762                              MacroBuilder &Builder) const {
3763    MipsTargetInfoBase::getArchDefines(Opts, Builder);
3764
3765    if (ABI == "o32") {
3766      Builder.defineMacro("__mips_o32");
3767      Builder.defineMacro("_ABIO32", "1");
3768      Builder.defineMacro("_MIPS_SIM", "_ABIO32");
3769    }
3770    else if (ABI == "eabi")
3771      Builder.defineMacro("__mips_eabi");
3772    else
3773      llvm_unreachable("Invalid ABI for Mips32.");
3774  }
3775  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3776                                unsigned &NumAliases) const {
3777    static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
3778      { { "at" },  "$1" },
3779      { { "v0" },  "$2" },
3780      { { "v1" },  "$3" },
3781      { { "a0" },  "$4" },
3782      { { "a1" },  "$5" },
3783      { { "a2" },  "$6" },
3784      { { "a3" },  "$7" },
3785      { { "t0" },  "$8" },
3786      { { "t1" },  "$9" },
3787      { { "t2" }, "$10" },
3788      { { "t3" }, "$11" },
3789      { { "t4" }, "$12" },
3790      { { "t5" }, "$13" },
3791      { { "t6" }, "$14" },
3792      { { "t7" }, "$15" },
3793      { { "s0" }, "$16" },
3794      { { "s1" }, "$17" },
3795      { { "s2" }, "$18" },
3796      { { "s3" }, "$19" },
3797      { { "s4" }, "$20" },
3798      { { "s5" }, "$21" },
3799      { { "s6" }, "$22" },
3800      { { "s7" }, "$23" },
3801      { { "t8" }, "$24" },
3802      { { "t9" }, "$25" },
3803      { { "k0" }, "$26" },
3804      { { "k1" }, "$27" },
3805      { { "gp" }, "$28" },
3806      { { "sp","$sp" }, "$29" },
3807      { { "fp","$fp" }, "$30" },
3808      { { "ra" }, "$31" }
3809    };
3810    Aliases = GCCRegAliases;
3811    NumAliases = llvm::array_lengthof(GCCRegAliases);
3812  }
3813};
3814
3815class Mips32EBTargetInfo : public Mips32TargetInfoBase {
3816public:
3817  Mips32EBTargetInfo(const std::string& triple) : Mips32TargetInfoBase(triple) {
3818    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3819                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
3820  }
3821  virtual void getTargetDefines(const LangOptions &Opts,
3822                                MacroBuilder &Builder) const {
3823    DefineStd(Builder, "mips", Opts);
3824    Builder.defineMacro("_mips");
3825    DefineStd(Builder, "MIPSEB", Opts);
3826    Builder.defineMacro("_MIPSEB");
3827    Builder.defineMacro("__REGISTER_PREFIX__", "");
3828    getArchDefines(Opts, Builder);
3829  }
3830};
3831
3832class Mips32ELTargetInfo : public Mips32TargetInfoBase {
3833public:
3834  Mips32ELTargetInfo(const std::string& triple) : Mips32TargetInfoBase(triple) {
3835    BigEndian = false;
3836    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3837                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
3838  }
3839  virtual void getTargetDefines(const LangOptions &Opts,
3840                                MacroBuilder &Builder) const {
3841    DefineStd(Builder, "mips", Opts);
3842    Builder.defineMacro("_mips");
3843    DefineStd(Builder, "MIPSEL", Opts);
3844    Builder.defineMacro("_MIPSEL");
3845    Builder.defineMacro("__REGISTER_PREFIX__", "");
3846    getArchDefines(Opts, Builder);
3847  }
3848};
3849
3850class Mips64TargetInfoBase : public MipsTargetInfoBase {
3851  virtual void SetDescriptionString(const std::string &Name) = 0;
3852public:
3853  Mips64TargetInfoBase(const std::string& triple) :
3854    MipsTargetInfoBase(triple, "n64", "mips64") {
3855    LongWidth = LongAlign = 64;
3856    PointerWidth = PointerAlign = 64;
3857    LongDoubleWidth = LongDoubleAlign = 128;
3858    LongDoubleFormat = &llvm::APFloat::IEEEquad;
3859    SuitableAlign = 128;
3860  }
3861  virtual bool setABI(const std::string &Name) {
3862    SetDescriptionString(Name);
3863
3864    if (Name != "n32" && Name != "n64")
3865      return false;
3866
3867    ABI = Name;
3868
3869    if (Name == "n32") {
3870      LongWidth = LongAlign = 32;
3871      PointerWidth = PointerAlign = 32;
3872    }
3873
3874    return true;
3875  }
3876  virtual void getArchDefines(const LangOptions &Opts,
3877                              MacroBuilder &Builder) const {
3878    MipsTargetInfoBase::getArchDefines(Opts, Builder);
3879
3880    if (ABI == "n32") {
3881      Builder.defineMacro("__mips_n32");
3882      Builder.defineMacro("_ABIN32", "2");
3883      Builder.defineMacro("_MIPS_SIM", "_ABIN32");
3884    }
3885    else if (ABI == "n64") {
3886      Builder.defineMacro("__mips_n64");
3887      Builder.defineMacro("_ABI64", "3");
3888      Builder.defineMacro("_MIPS_SIM", "_ABI64");
3889    }
3890    else
3891      llvm_unreachable("Invalid ABI for Mips64.");
3892  }
3893  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3894                                unsigned &NumAliases) const {
3895    static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
3896      { { "at" },  "$1" },
3897      { { "v0" },  "$2" },
3898      { { "v1" },  "$3" },
3899      { { "a0" },  "$4" },
3900      { { "a1" },  "$5" },
3901      { { "a2" },  "$6" },
3902      { { "a3" },  "$7" },
3903      { { "a4" },  "$8" },
3904      { { "a5" },  "$9" },
3905      { { "a6" }, "$10" },
3906      { { "a7" }, "$11" },
3907      { { "t0" }, "$12" },
3908      { { "t1" }, "$13" },
3909      { { "t2" }, "$14" },
3910      { { "t3" }, "$15" },
3911      { { "s0" }, "$16" },
3912      { { "s1" }, "$17" },
3913      { { "s2" }, "$18" },
3914      { { "s3" }, "$19" },
3915      { { "s4" }, "$20" },
3916      { { "s5" }, "$21" },
3917      { { "s6" }, "$22" },
3918      { { "s7" }, "$23" },
3919      { { "t8" }, "$24" },
3920      { { "t9" }, "$25" },
3921      { { "k0" }, "$26" },
3922      { { "k1" }, "$27" },
3923      { { "gp" }, "$28" },
3924      { { "sp","$sp" }, "$29" },
3925      { { "fp","$fp" }, "$30" },
3926      { { "ra" }, "$31" }
3927    };
3928    Aliases = GCCRegAliases;
3929    NumAliases = llvm::array_lengthof(GCCRegAliases);
3930  }
3931};
3932
3933class Mips64EBTargetInfo : public Mips64TargetInfoBase {
3934  virtual void SetDescriptionString(const std::string &Name) {
3935    // Change DescriptionString only if ABI is n32.
3936    if (Name == "n32")
3937      DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3938                          "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
3939                          "v64:64:64-n32";
3940  }
3941public:
3942  Mips64EBTargetInfo(const std::string& triple) : Mips64TargetInfoBase(triple) {
3943    // Default ABI is n64.
3944    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3945                        "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
3946                        "v64:64:64-n32";
3947  }
3948  virtual void getTargetDefines(const LangOptions &Opts,
3949                                MacroBuilder &Builder) const {
3950    DefineStd(Builder, "mips", Opts);
3951    Builder.defineMacro("_mips");
3952    DefineStd(Builder, "MIPSEB", Opts);
3953    Builder.defineMacro("_MIPSEB");
3954    Builder.defineMacro("__REGISTER_PREFIX__", "");
3955    getArchDefines(Opts, Builder);
3956  }
3957};
3958
3959class Mips64ELTargetInfo : public Mips64TargetInfoBase {
3960  virtual void SetDescriptionString(const std::string &Name) {
3961    // Change DescriptionString only if ABI is n32.
3962    if (Name == "n32")
3963      DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3964                          "i64:64:64-f32:32:32-f64:64:64-f128:128:128"
3965                          "-v64:64:64-n32";
3966  }
3967public:
3968  Mips64ELTargetInfo(const std::string& triple) : Mips64TargetInfoBase(triple) {
3969    // Default ABI is n64.
3970    BigEndian = false;
3971    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3972                        "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
3973                        "v64:64:64-n32";
3974  }
3975  virtual void getTargetDefines(const LangOptions &Opts,
3976                                MacroBuilder &Builder) const {
3977    DefineStd(Builder, "mips", Opts);
3978    Builder.defineMacro("_mips");
3979    DefineStd(Builder, "MIPSEL", Opts);
3980    Builder.defineMacro("_MIPSEL");
3981    Builder.defineMacro("__REGISTER_PREFIX__", "");
3982    getArchDefines(Opts, Builder);
3983  }
3984};
3985} // end anonymous namespace.
3986
3987namespace {
3988class PNaClTargetInfo : public TargetInfo {
3989public:
3990  PNaClTargetInfo(const std::string& triple) : TargetInfo(triple) {
3991    BigEndian = false;
3992    this->UserLabelPrefix = "";
3993    this->LongAlign = 32;
3994    this->LongWidth = 32;
3995    this->PointerAlign = 32;
3996    this->PointerWidth = 32;
3997    this->IntMaxType = TargetInfo::SignedLongLong;
3998    this->UIntMaxType = TargetInfo::UnsignedLongLong;
3999    this->Int64Type = TargetInfo::SignedLongLong;
4000    this->DoubleAlign = 64;
4001    this->LongDoubleWidth = 64;
4002    this->LongDoubleAlign = 64;
4003    this->SizeType = TargetInfo::UnsignedInt;
4004    this->PtrDiffType = TargetInfo::SignedInt;
4005    this->IntPtrType = TargetInfo::SignedInt;
4006    this->RegParmMax = 2;
4007    DescriptionString = "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
4008                        "f32:32:32-f64:64:64-p:32:32:32-v128:32:32";
4009  }
4010
4011  void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
4012  }
4013  virtual void getArchDefines(const LangOptions &Opts,
4014                              MacroBuilder &Builder) const {
4015    Builder.defineMacro("__le32__");
4016    Builder.defineMacro("__pnacl__");
4017  }
4018  virtual void getTargetDefines(const LangOptions &Opts,
4019                                MacroBuilder &Builder) const {
4020    DefineStd(Builder, "unix", Opts);
4021    Builder.defineMacro("__ELF__");
4022    if (Opts.POSIXThreads)
4023      Builder.defineMacro("_REENTRANT");
4024    if (Opts.CPlusPlus)
4025      Builder.defineMacro("_GNU_SOURCE");
4026
4027    Builder.defineMacro("__LITTLE_ENDIAN__");
4028    Builder.defineMacro("__native_client__");
4029    getArchDefines(Opts, Builder);
4030  }
4031  virtual bool hasFeature(StringRef Feature) const {
4032    return Feature == "pnacl";
4033  }
4034  virtual void getTargetBuiltins(const Builtin::Info *&Records,
4035                                 unsigned &NumRecords) const {
4036  }
4037  virtual BuiltinVaListKind getBuiltinVaListKind() const {
4038    return TargetInfo::PNaClABIBuiltinVaList;
4039  }
4040  virtual void getGCCRegNames(const char * const *&Names,
4041                              unsigned &NumNames) const;
4042  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4043                                unsigned &NumAliases) const;
4044  virtual bool validateAsmConstraint(const char *&Name,
4045                                     TargetInfo::ConstraintInfo &Info) const {
4046    return false;
4047  }
4048
4049  virtual const char *getClobbers() const {
4050    return "";
4051  }
4052};
4053
4054void PNaClTargetInfo::getGCCRegNames(const char * const *&Names,
4055                                     unsigned &NumNames) const {
4056  Names = NULL;
4057  NumNames = 0;
4058}
4059
4060void PNaClTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
4061                                       unsigned &NumAliases) const {
4062  Aliases = NULL;
4063  NumAliases = 0;
4064}
4065} // end anonymous namespace.
4066
4067
4068//===----------------------------------------------------------------------===//
4069// Driver code
4070//===----------------------------------------------------------------------===//
4071
4072static TargetInfo *AllocateTarget(const std::string &T) {
4073  llvm::Triple Triple(T);
4074  llvm::Triple::OSType os = Triple.getOS();
4075
4076  switch (Triple.getArch()) {
4077  default:
4078    return NULL;
4079
4080  case llvm::Triple::hexagon:
4081    return new HexagonTargetInfo(T);
4082
4083  case llvm::Triple::arm:
4084  case llvm::Triple::thumb:
4085    if (Triple.isOSDarwin())
4086      return new DarwinARMTargetInfo(T);
4087
4088    switch (os) {
4089    case llvm::Triple::Linux:
4090      return new LinuxTargetInfo<ARMTargetInfo>(T);
4091    case llvm::Triple::FreeBSD:
4092      return new FreeBSDTargetInfo<ARMTargetInfo>(T);
4093    case llvm::Triple::NetBSD:
4094      return new NetBSDTargetInfo<ARMTargetInfo>(T);
4095    case llvm::Triple::RTEMS:
4096      return new RTEMSTargetInfo<ARMTargetInfo>(T);
4097    default:
4098      return new ARMTargetInfo(T);
4099    }
4100
4101  case llvm::Triple::msp430:
4102    return new MSP430TargetInfo(T);
4103
4104  case llvm::Triple::mips:
4105    switch (os) {
4106    case llvm::Triple::Linux:
4107      return new LinuxTargetInfo<Mips32EBTargetInfo>(T);
4108    case llvm::Triple::RTEMS:
4109      return new RTEMSTargetInfo<Mips32EBTargetInfo>(T);
4110    case llvm::Triple::FreeBSD:
4111      return new FreeBSDTargetInfo<Mips32EBTargetInfo>(T);
4112    case llvm::Triple::NetBSD:
4113      return new NetBSDTargetInfo<Mips32EBTargetInfo>(T);
4114    default:
4115      return new Mips32EBTargetInfo(T);
4116    }
4117
4118  case llvm::Triple::mipsel:
4119    switch (os) {
4120    case llvm::Triple::Linux:
4121      return new LinuxTargetInfo<Mips32ELTargetInfo>(T);
4122    case llvm::Triple::RTEMS:
4123      return new RTEMSTargetInfo<Mips32ELTargetInfo>(T);
4124    case llvm::Triple::FreeBSD:
4125      return new FreeBSDTargetInfo<Mips32ELTargetInfo>(T);
4126    case llvm::Triple::NetBSD:
4127      return new NetBSDTargetInfo<Mips32ELTargetInfo>(T);
4128    default:
4129      return new Mips32ELTargetInfo(T);
4130    }
4131
4132  case llvm::Triple::mips64:
4133    switch (os) {
4134    case llvm::Triple::Linux:
4135      return new LinuxTargetInfo<Mips64EBTargetInfo>(T);
4136    case llvm::Triple::RTEMS:
4137      return new RTEMSTargetInfo<Mips64EBTargetInfo>(T);
4138    case llvm::Triple::FreeBSD:
4139      return new FreeBSDTargetInfo<Mips64EBTargetInfo>(T);
4140    case llvm::Triple::NetBSD:
4141      return new NetBSDTargetInfo<Mips64EBTargetInfo>(T);
4142    default:
4143      return new Mips64EBTargetInfo(T);
4144    }
4145
4146  case llvm::Triple::mips64el:
4147    switch (os) {
4148    case llvm::Triple::Linux:
4149      return new LinuxTargetInfo<Mips64ELTargetInfo>(T);
4150    case llvm::Triple::RTEMS:
4151      return new RTEMSTargetInfo<Mips64ELTargetInfo>(T);
4152    case llvm::Triple::FreeBSD:
4153      return new FreeBSDTargetInfo<Mips64ELTargetInfo>(T);
4154    case llvm::Triple::NetBSD:
4155      return new NetBSDTargetInfo<Mips64ELTargetInfo>(T);
4156    default:
4157      return new Mips64ELTargetInfo(T);
4158    }
4159
4160  case llvm::Triple::le32:
4161    switch (os) {
4162      case llvm::Triple::NativeClient:
4163        return new PNaClTargetInfo(T);
4164      default:
4165        return NULL;
4166    }
4167
4168  case llvm::Triple::ppc:
4169    if (Triple.isOSDarwin())
4170      return new DarwinPPC32TargetInfo(T);
4171    switch (os) {
4172    case llvm::Triple::Linux:
4173      return new LinuxTargetInfo<PPC32TargetInfo>(T);
4174    case llvm::Triple::FreeBSD:
4175      return new FreeBSDTargetInfo<PPC32TargetInfo>(T);
4176    case llvm::Triple::NetBSD:
4177      return new NetBSDTargetInfo<PPC32TargetInfo>(T);
4178    case llvm::Triple::RTEMS:
4179      return new RTEMSTargetInfo<PPC32TargetInfo>(T);
4180    default:
4181      return new PPC32TargetInfo(T);
4182    }
4183
4184  case llvm::Triple::ppc64:
4185    if (Triple.isOSDarwin())
4186      return new DarwinPPC64TargetInfo(T);
4187    switch (os) {
4188    case llvm::Triple::Linux:
4189      return new LinuxTargetInfo<PPC64TargetInfo>(T);
4190    case llvm::Triple::Lv2:
4191      return new PS3PPUTargetInfo<PPC64TargetInfo>(T);
4192    case llvm::Triple::FreeBSD:
4193      return new FreeBSDTargetInfo<PPC64TargetInfo>(T);
4194    case llvm::Triple::NetBSD:
4195      return new NetBSDTargetInfo<PPC64TargetInfo>(T);
4196    default:
4197      return new PPC64TargetInfo(T);
4198    }
4199
4200  case llvm::Triple::nvptx:
4201    return new NVPTX32TargetInfo(T);
4202  case llvm::Triple::nvptx64:
4203    return new NVPTX64TargetInfo(T);
4204
4205  case llvm::Triple::mblaze:
4206    return new MBlazeTargetInfo(T);
4207
4208  case llvm::Triple::sparc:
4209    switch (os) {
4210    case llvm::Triple::Linux:
4211      return new LinuxTargetInfo<SparcV8TargetInfo>(T);
4212    case llvm::Triple::AuroraUX:
4213      return new AuroraUXSparcV8TargetInfo(T);
4214    case llvm::Triple::Solaris:
4215      return new SolarisSparcV8TargetInfo(T);
4216    case llvm::Triple::NetBSD:
4217      return new NetBSDTargetInfo<SparcV8TargetInfo>(T);
4218    case llvm::Triple::RTEMS:
4219      return new RTEMSTargetInfo<SparcV8TargetInfo>(T);
4220    default:
4221      return new SparcV8TargetInfo(T);
4222    }
4223
4224  // FIXME: Need a real SPU target.
4225  case llvm::Triple::cellspu:
4226    return new PS3SPUTargetInfo<PPC64TargetInfo>(T);
4227
4228  case llvm::Triple::tce:
4229    return new TCETargetInfo(T);
4230
4231  case llvm::Triple::x86:
4232    if (Triple.isOSDarwin())
4233      return new DarwinI386TargetInfo(T);
4234
4235    switch (os) {
4236    case llvm::Triple::AuroraUX:
4237      return new AuroraUXTargetInfo<X86_32TargetInfo>(T);
4238    case llvm::Triple::Linux:
4239      return new LinuxTargetInfo<X86_32TargetInfo>(T);
4240    case llvm::Triple::DragonFly:
4241      return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
4242    case llvm::Triple::NetBSD:
4243      return new NetBSDI386TargetInfo(T);
4244    case llvm::Triple::OpenBSD:
4245      return new OpenBSDI386TargetInfo(T);
4246    case llvm::Triple::FreeBSD:
4247      return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
4248    case llvm::Triple::Minix:
4249      return new MinixTargetInfo<X86_32TargetInfo>(T);
4250    case llvm::Triple::Solaris:
4251      return new SolarisTargetInfo<X86_32TargetInfo>(T);
4252    case llvm::Triple::Cygwin:
4253      return new CygwinX86_32TargetInfo(T);
4254    case llvm::Triple::MinGW32:
4255      return new MinGWX86_32TargetInfo(T);
4256    case llvm::Triple::Win32:
4257      return new VisualStudioWindowsX86_32TargetInfo(T);
4258    case llvm::Triple::Haiku:
4259      return new HaikuX86_32TargetInfo(T);
4260    case llvm::Triple::RTEMS:
4261      return new RTEMSX86_32TargetInfo(T);
4262    default:
4263      return new X86_32TargetInfo(T);
4264    }
4265
4266  case llvm::Triple::x86_64:
4267    if (Triple.isOSDarwin() || Triple.getEnvironment() == llvm::Triple::MachO)
4268      return new DarwinX86_64TargetInfo(T);
4269
4270    switch (os) {
4271    case llvm::Triple::AuroraUX:
4272      return new AuroraUXTargetInfo<X86_64TargetInfo>(T);
4273    case llvm::Triple::Linux:
4274      return new LinuxTargetInfo<X86_64TargetInfo>(T);
4275    case llvm::Triple::DragonFly:
4276      return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(T);
4277    case llvm::Triple::NetBSD:
4278      return new NetBSDTargetInfo<X86_64TargetInfo>(T);
4279    case llvm::Triple::OpenBSD:
4280      return new OpenBSDX86_64TargetInfo(T);
4281    case llvm::Triple::FreeBSD:
4282      return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
4283    case llvm::Triple::Solaris:
4284      return new SolarisTargetInfo<X86_64TargetInfo>(T);
4285    case llvm::Triple::MinGW32:
4286      return new MinGWX86_64TargetInfo(T);
4287    case llvm::Triple::Win32:   // This is what Triple.h supports now.
4288      return new VisualStudioWindowsX86_64TargetInfo(T);
4289    default:
4290      return new X86_64TargetInfo(T);
4291    }
4292  }
4293}
4294
4295/// CreateTargetInfo - Return the target info object for the specified target
4296/// triple.
4297TargetInfo *TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
4298                                         TargetOptions &Opts) {
4299  llvm::Triple Triple(Opts.Triple);
4300
4301  // Construct the target
4302  OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
4303  if (!Target) {
4304    Diags.Report(diag::err_target_unknown_triple) << Triple.str();
4305    return 0;
4306  }
4307
4308  // Set the target CPU if specified.
4309  if (!Opts.CPU.empty() && !Target->setCPU(Opts.CPU)) {
4310    Diags.Report(diag::err_target_unknown_cpu) << Opts.CPU;
4311    return 0;
4312  }
4313
4314  // Set the target ABI if specified.
4315  if (!Opts.ABI.empty() && !Target->setABI(Opts.ABI)) {
4316    Diags.Report(diag::err_target_unknown_abi) << Opts.ABI;
4317    return 0;
4318  }
4319
4320  // Set the target C++ ABI.
4321  if (!Opts.CXXABI.empty() && !Target->setCXXABI(Opts.CXXABI)) {
4322    Diags.Report(diag::err_target_unknown_cxxabi) << Opts.CXXABI;
4323    return 0;
4324  }
4325
4326  // Compute the default target features, we need the target to handle this
4327  // because features may have dependencies on one another.
4328  llvm::StringMap<bool> Features;
4329  Target->getDefaultFeatures(Features);
4330
4331  // Apply the user specified deltas.
4332  // First the enables.
4333  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
4334         ie = Opts.Features.end(); it != ie; ++it) {
4335    const char *Name = it->c_str();
4336
4337    if (Name[0] != '+')
4338      continue;
4339
4340    // Apply the feature via the target.
4341    if (!Target->setFeatureEnabled(Features, Name + 1, true)) {
4342      Diags.Report(diag::err_target_invalid_feature) << Name;
4343      return 0;
4344    }
4345  }
4346
4347  // Then the disables.
4348  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
4349         ie = Opts.Features.end(); it != ie; ++it) {
4350    const char *Name = it->c_str();
4351
4352    if (Name[0] == '+')
4353      continue;
4354
4355    // Apply the feature via the target.
4356    if (Name[0] != '-' ||
4357        !Target->setFeatureEnabled(Features, Name + 1, false)) {
4358      Diags.Report(diag::err_target_invalid_feature) << Name;
4359      return 0;
4360    }
4361  }
4362
4363  // Add the features to the compile options.
4364  //
4365  // FIXME: If we are completely confident that we have the right set, we only
4366  // need to pass the minuses.
4367  Opts.Features.clear();
4368  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
4369         ie = Features.end(); it != ie; ++it)
4370    Opts.Features.push_back((it->second ? "+" : "-") + it->first().str());
4371  Target->HandleTargetFeatures(Opts.Features);
4372
4373  return Target.take();
4374}
4375