Targets.cpp revision 84f30809a2ce02183609a738aaa116d832e61da3
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 HasRDRND;
1327  bool HasBMI;
1328  bool HasBMI2;
1329  bool HasPOPCNT;
1330  bool HasSSE4a;
1331  bool HasFMA4;
1332  bool HasFMA;
1333  bool HasXOP;
1334
1335  /// \brief Enumeration of all of the X86 CPUs supported by Clang.
1336  ///
1337  /// Each enumeration represents a particular CPU supported by Clang. These
1338  /// loosely correspond to the options passed to '-march' or '-mtune' flags.
1339  enum CPUKind {
1340    CK_Generic,
1341
1342    /// \name i386
1343    /// i386-generation processors.
1344    //@{
1345    CK_i386,
1346    //@}
1347
1348    /// \name i486
1349    /// i486-generation processors.
1350    //@{
1351    CK_i486,
1352    CK_WinChipC6,
1353    CK_WinChip2,
1354    CK_C3,
1355    //@}
1356
1357    /// \name i586
1358    /// i586-generation processors, P5 microarchitecture based.
1359    //@{
1360    CK_i586,
1361    CK_Pentium,
1362    CK_PentiumMMX,
1363    //@}
1364
1365    /// \name i686
1366    /// i686-generation processors, P6 / Pentium M microarchitecture based.
1367    //@{
1368    CK_i686,
1369    CK_PentiumPro,
1370    CK_Pentium2,
1371    CK_Pentium3,
1372    CK_Pentium3M,
1373    CK_PentiumM,
1374    CK_C3_2,
1375
1376    /// This enumerator is a bit odd, as GCC no longer accepts -march=yonah.
1377    /// Clang however has some logic to suport this.
1378    // FIXME: Warn, deprecate, and potentially remove this.
1379    CK_Yonah,
1380    //@}
1381
1382    /// \name Netburst
1383    /// Netburst microarchitecture based processors.
1384    //@{
1385    CK_Pentium4,
1386    CK_Pentium4M,
1387    CK_Prescott,
1388    CK_Nocona,
1389    //@}
1390
1391    /// \name Core
1392    /// Core microarchitecture based processors.
1393    //@{
1394    CK_Core2,
1395
1396    /// This enumerator, like \see CK_Yonah, is a bit odd. It is another
1397    /// codename which GCC no longer accepts as an option to -march, but Clang
1398    /// has some logic for recognizing it.
1399    // FIXME: Warn, deprecate, and potentially remove this.
1400    CK_Penryn,
1401    //@}
1402
1403    /// \name Atom
1404    /// Atom processors
1405    //@{
1406    CK_Atom,
1407    //@}
1408
1409    /// \name Nehalem
1410    /// Nehalem microarchitecture based processors.
1411    //@{
1412    CK_Corei7,
1413    CK_Corei7AVX,
1414    CK_CoreAVXi,
1415    CK_CoreAVX2,
1416    //@}
1417
1418    /// \name K6
1419    /// K6 architecture processors.
1420    //@{
1421    CK_K6,
1422    CK_K6_2,
1423    CK_K6_3,
1424    //@}
1425
1426    /// \name K7
1427    /// K7 architecture processors.
1428    //@{
1429    CK_Athlon,
1430    CK_AthlonThunderbird,
1431    CK_Athlon4,
1432    CK_AthlonXP,
1433    CK_AthlonMP,
1434    //@}
1435
1436    /// \name K8
1437    /// K8 architecture processors.
1438    //@{
1439    CK_Athlon64,
1440    CK_Athlon64SSE3,
1441    CK_AthlonFX,
1442    CK_K8,
1443    CK_K8SSE3,
1444    CK_Opteron,
1445    CK_OpteronSSE3,
1446    CK_AMDFAM10,
1447    //@}
1448
1449    /// \name Bobcat
1450    /// Bobcat architecture processors.
1451    //@{
1452    CK_BTVER1,
1453    //@}
1454
1455    /// \name Bulldozer
1456    /// Bulldozer architecture processors.
1457    //@{
1458    CK_BDVER1,
1459    CK_BDVER2,
1460    //@}
1461
1462    /// This specification is deprecated and will be removed in the future.
1463    /// Users should prefer \see CK_K8.
1464    // FIXME: Warn on this when the CPU is set to it.
1465    CK_x86_64,
1466    //@}
1467
1468    /// \name Geode
1469    /// Geode processors.
1470    //@{
1471    CK_Geode
1472    //@}
1473  } CPU;
1474
1475public:
1476  X86TargetInfo(const std::string& triple)
1477    : TargetInfo(triple), SSELevel(NoSSE), MMX3DNowLevel(NoMMX3DNow),
1478      HasAES(false), HasPCLMUL(false), HasLZCNT(false), HasRDRND(false),
1479      HasBMI(false), HasBMI2(false), HasPOPCNT(false), HasSSE4a(false),
1480      HasFMA4(false), HasFMA(false), HasXOP(false), CPU(CK_Generic) {
1481    BigEndian = false;
1482    LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
1483  }
1484  virtual unsigned getFloatEvalMethod() const {
1485    // X87 evaluates with 80 bits "long double" precision.
1486    return SSELevel == NoSSE ? 2 : 0;
1487  }
1488  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1489                                 unsigned &NumRecords) const {
1490    Records = BuiltinInfo;
1491    NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
1492  }
1493  virtual void getGCCRegNames(const char * const *&Names,
1494                              unsigned &NumNames) const {
1495    Names = GCCRegNames;
1496    NumNames = llvm::array_lengthof(GCCRegNames);
1497  }
1498  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1499                                unsigned &NumAliases) const {
1500    Aliases = 0;
1501    NumAliases = 0;
1502  }
1503  virtual void getGCCAddlRegNames(const AddlRegName *&Names,
1504				  unsigned &NumNames) const {
1505    Names = AddlRegNames;
1506    NumNames = llvm::array_lengthof(AddlRegNames);
1507  }
1508  virtual bool validateAsmConstraint(const char *&Name,
1509                                     TargetInfo::ConstraintInfo &info) const;
1510  virtual std::string convertConstraint(const char *&Constraint) const;
1511  virtual const char *getClobbers() const {
1512    return "~{dirflag},~{fpsr},~{flags}";
1513  }
1514  virtual void getTargetDefines(const LangOptions &Opts,
1515                                MacroBuilder &Builder) const;
1516  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1517                                 StringRef Name,
1518                                 bool Enabled) const;
1519  virtual void getDefaultFeatures(llvm::StringMap<bool> &Features) const;
1520  virtual bool hasFeature(StringRef Feature) const;
1521  virtual void HandleTargetFeatures(std::vector<std::string> &Features);
1522  virtual const char* getABI() const {
1523    if (PointerWidth == 64 && SSELevel >= AVX)
1524      return "avx";
1525    else if (PointerWidth == 32 && MMX3DNowLevel == NoMMX3DNow)
1526      return "no-mmx";
1527    return "";
1528  }
1529  virtual bool setCPU(const std::string &Name) {
1530    CPU = llvm::StringSwitch<CPUKind>(Name)
1531      .Case("i386", CK_i386)
1532      .Case("i486", CK_i486)
1533      .Case("winchip-c6", CK_WinChipC6)
1534      .Case("winchip2", CK_WinChip2)
1535      .Case("c3", CK_C3)
1536      .Case("i586", CK_i586)
1537      .Case("pentium", CK_Pentium)
1538      .Case("pentium-mmx", CK_PentiumMMX)
1539      .Case("i686", CK_i686)
1540      .Case("pentiumpro", CK_PentiumPro)
1541      .Case("pentium2", CK_Pentium2)
1542      .Case("pentium3", CK_Pentium3)
1543      .Case("pentium3m", CK_Pentium3M)
1544      .Case("pentium-m", CK_PentiumM)
1545      .Case("c3-2", CK_C3_2)
1546      .Case("yonah", CK_Yonah)
1547      .Case("pentium4", CK_Pentium4)
1548      .Case("pentium4m", CK_Pentium4M)
1549      .Case("prescott", CK_Prescott)
1550      .Case("nocona", CK_Nocona)
1551      .Case("core2", CK_Core2)
1552      .Case("penryn", CK_Penryn)
1553      .Case("atom", CK_Atom)
1554      .Case("corei7", CK_Corei7)
1555      .Case("corei7-avx", CK_Corei7AVX)
1556      .Case("core-avx-i", CK_CoreAVXi)
1557      .Case("core-avx2", CK_CoreAVX2)
1558      .Case("k6", CK_K6)
1559      .Case("k6-2", CK_K6_2)
1560      .Case("k6-3", CK_K6_3)
1561      .Case("athlon", CK_Athlon)
1562      .Case("athlon-tbird", CK_AthlonThunderbird)
1563      .Case("athlon-4", CK_Athlon4)
1564      .Case("athlon-xp", CK_AthlonXP)
1565      .Case("athlon-mp", CK_AthlonMP)
1566      .Case("athlon64", CK_Athlon64)
1567      .Case("athlon64-sse3", CK_Athlon64SSE3)
1568      .Case("athlon-fx", CK_AthlonFX)
1569      .Case("k8", CK_K8)
1570      .Case("k8-sse3", CK_K8SSE3)
1571      .Case("opteron", CK_Opteron)
1572      .Case("opteron-sse3", CK_OpteronSSE3)
1573      .Case("amdfam10", CK_AMDFAM10)
1574      .Case("btver1", CK_BTVER1)
1575      .Case("bdver1", CK_BDVER1)
1576      .Case("bdver2", CK_BDVER2)
1577      .Case("x86-64", CK_x86_64)
1578      .Case("geode", CK_Geode)
1579      .Default(CK_Generic);
1580
1581    // Perform any per-CPU checks necessary to determine if this CPU is
1582    // acceptable.
1583    // FIXME: This results in terrible diagnostics. Clang just says the CPU is
1584    // invalid without explaining *why*.
1585    switch (CPU) {
1586    case CK_Generic:
1587      // No processor selected!
1588      return false;
1589
1590    case CK_i386:
1591    case CK_i486:
1592    case CK_WinChipC6:
1593    case CK_WinChip2:
1594    case CK_C3:
1595    case CK_i586:
1596    case CK_Pentium:
1597    case CK_PentiumMMX:
1598    case CK_i686:
1599    case CK_PentiumPro:
1600    case CK_Pentium2:
1601    case CK_Pentium3:
1602    case CK_Pentium3M:
1603    case CK_PentiumM:
1604    case CK_Yonah:
1605    case CK_C3_2:
1606    case CK_Pentium4:
1607    case CK_Pentium4M:
1608    case CK_Prescott:
1609    case CK_K6:
1610    case CK_K6_2:
1611    case CK_K6_3:
1612    case CK_Athlon:
1613    case CK_AthlonThunderbird:
1614    case CK_Athlon4:
1615    case CK_AthlonXP:
1616    case CK_AthlonMP:
1617    case CK_Geode:
1618      // Only accept certain architectures when compiling in 32-bit mode.
1619      if (PointerWidth != 32)
1620        return false;
1621
1622      // Fallthrough
1623    case CK_Nocona:
1624    case CK_Core2:
1625    case CK_Penryn:
1626    case CK_Atom:
1627    case CK_Corei7:
1628    case CK_Corei7AVX:
1629    case CK_CoreAVXi:
1630    case CK_CoreAVX2:
1631    case CK_Athlon64:
1632    case CK_Athlon64SSE3:
1633    case CK_AthlonFX:
1634    case CK_K8:
1635    case CK_K8SSE3:
1636    case CK_Opteron:
1637    case CK_OpteronSSE3:
1638    case CK_AMDFAM10:
1639    case CK_BTVER1:
1640    case CK_BDVER1:
1641    case CK_BDVER2:
1642    case CK_x86_64:
1643      return true;
1644    }
1645    llvm_unreachable("Unhandled CPU kind");
1646  }
1647};
1648
1649void X86TargetInfo::getDefaultFeatures(llvm::StringMap<bool> &Features) const {
1650  // FIXME: This should not be here.
1651  Features["3dnow"] = false;
1652  Features["3dnowa"] = false;
1653  Features["mmx"] = false;
1654  Features["sse"] = false;
1655  Features["sse2"] = false;
1656  Features["sse3"] = false;
1657  Features["ssse3"] = false;
1658  Features["sse41"] = false;
1659  Features["sse42"] = false;
1660  Features["sse4a"] = false;
1661  Features["aes"] = false;
1662  Features["pclmul"] = false;
1663  Features["avx"] = false;
1664  Features["avx2"] = false;
1665  Features["lzcnt"] = false;
1666  Features["rdrand"] = false;
1667  Features["bmi"] = false;
1668  Features["bmi2"] = false;
1669  Features["popcnt"] = false;
1670  Features["fma4"] = false;
1671  Features["fma"] = false;
1672  Features["xop"] = false;
1673
1674  // FIXME: This *really* should not be here.
1675
1676  // X86_64 always has SSE2.
1677  if (PointerWidth == 64)
1678    Features["sse2"] = Features["sse"] = Features["mmx"] = true;
1679
1680  switch (CPU) {
1681  case CK_Generic:
1682  case CK_i386:
1683  case CK_i486:
1684  case CK_i586:
1685  case CK_Pentium:
1686  case CK_i686:
1687  case CK_PentiumPro:
1688    break;
1689  case CK_PentiumMMX:
1690  case CK_Pentium2:
1691    setFeatureEnabled(Features, "mmx", true);
1692    break;
1693  case CK_Pentium3:
1694  case CK_Pentium3M:
1695    setFeatureEnabled(Features, "mmx", true);
1696    setFeatureEnabled(Features, "sse", true);
1697    break;
1698  case CK_PentiumM:
1699  case CK_Pentium4:
1700  case CK_Pentium4M:
1701  case CK_x86_64:
1702    setFeatureEnabled(Features, "mmx", true);
1703    setFeatureEnabled(Features, "sse2", true);
1704    break;
1705  case CK_Yonah:
1706  case CK_Prescott:
1707  case CK_Nocona:
1708    setFeatureEnabled(Features, "mmx", true);
1709    setFeatureEnabled(Features, "sse3", true);
1710    break;
1711  case CK_Core2:
1712    setFeatureEnabled(Features, "mmx", true);
1713    setFeatureEnabled(Features, "ssse3", true);
1714    break;
1715  case CK_Penryn:
1716    setFeatureEnabled(Features, "mmx", true);
1717    setFeatureEnabled(Features, "sse4.1", true);
1718    break;
1719  case CK_Atom:
1720    setFeatureEnabled(Features, "mmx", true);
1721    setFeatureEnabled(Features, "ssse3", true);
1722    break;
1723  case CK_Corei7:
1724    setFeatureEnabled(Features, "mmx", true);
1725    setFeatureEnabled(Features, "sse4", true);
1726    break;
1727  case CK_Corei7AVX:
1728    setFeatureEnabled(Features, "mmx", true);
1729    setFeatureEnabled(Features, "avx", true);
1730    setFeatureEnabled(Features, "aes", true);
1731    setFeatureEnabled(Features, "pclmul", true);
1732    break;
1733  case CK_CoreAVXi:
1734    setFeatureEnabled(Features, "mmx", true);
1735    setFeatureEnabled(Features, "avx", true);
1736    setFeatureEnabled(Features, "aes", true);
1737    setFeatureEnabled(Features, "pclmul", true);
1738    setFeatureEnabled(Features, "rdrnd", true);
1739    break;
1740  case CK_CoreAVX2:
1741    setFeatureEnabled(Features, "mmx", true);
1742    setFeatureEnabled(Features, "avx2", true);
1743    setFeatureEnabled(Features, "aes", true);
1744    setFeatureEnabled(Features, "pclmul", true);
1745    setFeatureEnabled(Features, "lzcnt", true);
1746    setFeatureEnabled(Features, "rdrnd", true);
1747    setFeatureEnabled(Features, "bmi", true);
1748    setFeatureEnabled(Features, "bmi2", true);
1749    setFeatureEnabled(Features, "fma", true);
1750    break;
1751  case CK_K6:
1752  case CK_WinChipC6:
1753    setFeatureEnabled(Features, "mmx", true);
1754    break;
1755  case CK_K6_2:
1756  case CK_K6_3:
1757  case CK_WinChip2:
1758  case CK_C3:
1759    setFeatureEnabled(Features, "3dnow", true);
1760    break;
1761  case CK_Athlon:
1762  case CK_AthlonThunderbird:
1763  case CK_Geode:
1764    setFeatureEnabled(Features, "3dnowa", true);
1765    break;
1766  case CK_Athlon4:
1767  case CK_AthlonXP:
1768  case CK_AthlonMP:
1769    setFeatureEnabled(Features, "sse", true);
1770    setFeatureEnabled(Features, "3dnowa", true);
1771    break;
1772  case CK_K8:
1773  case CK_Opteron:
1774  case CK_Athlon64:
1775  case CK_AthlonFX:
1776    setFeatureEnabled(Features, "sse2", true);
1777    setFeatureEnabled(Features, "3dnowa", true);
1778    break;
1779  case CK_K8SSE3:
1780  case CK_OpteronSSE3:
1781  case CK_Athlon64SSE3:
1782    setFeatureEnabled(Features, "sse3", true);
1783    setFeatureEnabled(Features, "3dnowa", true);
1784    break;
1785  case CK_AMDFAM10:
1786    setFeatureEnabled(Features, "sse3", true);
1787    setFeatureEnabled(Features, "sse4a", true);
1788    setFeatureEnabled(Features, "3dnowa", true);
1789    break;
1790  case CK_BTVER1:
1791    setFeatureEnabled(Features, "ssse3", true);
1792    setFeatureEnabled(Features, "sse4a", true);
1793    break;
1794  case CK_BDVER1:
1795  case CK_BDVER2:
1796    setFeatureEnabled(Features, "avx", true);
1797    setFeatureEnabled(Features, "xop", true);
1798    setFeatureEnabled(Features, "aes", true);
1799    setFeatureEnabled(Features, "pclmul", true);
1800    break;
1801  case CK_C3_2:
1802    setFeatureEnabled(Features, "mmx", true);
1803    setFeatureEnabled(Features, "sse", true);
1804    break;
1805  }
1806}
1807
1808bool X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1809                                      StringRef Name,
1810                                      bool Enabled) const {
1811  // FIXME: This *really* should not be here.  We need some way of translating
1812  // options into llvm subtarget features.
1813  if (!Features.count(Name) &&
1814      (Name != "sse4" && Name != "sse4.2" && Name != "sse4.1" &&
1815       Name != "rdrnd"))
1816    return false;
1817
1818  // FIXME: this should probably use a switch with fall through.
1819
1820  if (Enabled) {
1821    if (Name == "mmx")
1822      Features["mmx"] = true;
1823    else if (Name == "sse")
1824      Features["mmx"] = Features["sse"] = true;
1825    else if (Name == "sse2")
1826      Features["mmx"] = Features["sse"] = Features["sse2"] = true;
1827    else if (Name == "sse3")
1828      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1829        true;
1830    else if (Name == "ssse3")
1831      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1832        Features["ssse3"] = true;
1833    else if (Name == "sse4" || Name == "sse4.2")
1834      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1835        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1836        Features["popcnt"] = true;
1837    else if (Name == "sse4.1")
1838      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1839        Features["ssse3"] = Features["sse41"] = true;
1840    else if (Name == "3dnow")
1841      Features["mmx"] = Features["3dnow"] = true;
1842    else if (Name == "3dnowa")
1843      Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = true;
1844    else if (Name == "aes")
1845      Features["sse"] = Features["sse2"] = Features["aes"] = true;
1846    else if (Name == "pclmul")
1847      Features["sse"] = Features["sse2"] = Features["pclmul"] = true;
1848    else if (Name == "avx")
1849      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1850        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1851        Features["popcnt"] = Features["avx"] = true;
1852    else if (Name == "avx2")
1853      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1854        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1855        Features["popcnt"] = Features["avx"] = Features["avx2"] = true;
1856    else if (Name == "fma")
1857      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1858        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1859        Features["popcnt"] = Features["avx"] = Features["fma"] = true;
1860    else if (Name == "fma4")
1861        Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1862        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1863        Features["popcnt"] = Features["avx"] = Features["sse4a"] =
1864        Features["fma4"] = true;
1865    else if (Name == "xop")
1866        Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1867        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1868        Features["popcnt"] = Features["avx"] = Features["sse4a"] =
1869        Features["fma4"] = Features["xop"] = true;
1870    else if (Name == "sse4a")
1871      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1872        Features["sse4a"] = true;
1873    else if (Name == "lzcnt")
1874      Features["lzcnt"] = true;
1875    else if (Name == "rdrnd")
1876      Features["rdrand"] = true;
1877    else if (Name == "bmi")
1878      Features["bmi"] = true;
1879    else if (Name == "bmi2")
1880      Features["bmi2"] = true;
1881    else if (Name == "popcnt")
1882      Features["popcnt"] = true;
1883  } else {
1884    if (Name == "mmx")
1885      Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = false;
1886    else if (Name == "sse")
1887      Features["sse"] = Features["sse2"] = Features["sse3"] =
1888        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1889        Features["sse4a"] = Features["avx"] = Features["avx2"] =
1890        Features["fma"] = Features["fma4"] = Features["aes"] =
1891        Features["pclmul"] = Features["xop"] = false;
1892    else if (Name == "sse2")
1893      Features["sse2"] = Features["sse3"] = Features["ssse3"] =
1894        Features["sse41"] = Features["sse42"] = Features["sse4a"] =
1895        Features["avx"] = Features["avx2"] = Features["fma"] =
1896        Features["fma4"] = Features["aes"] = Features["pclmul"] =
1897        Features["xop"] = false;
1898    else if (Name == "sse3")
1899      Features["sse3"] = Features["ssse3"] = Features["sse41"] =
1900        Features["sse42"] = Features["sse4a"] = Features["avx"] =
1901        Features["avx2"] = Features["fma"] = Features["fma4"] =
1902        Features["xop"] = false;
1903    else if (Name == "ssse3")
1904      Features["ssse3"] = Features["sse41"] = Features["sse42"] =
1905        Features["avx"] = Features["avx2"] = Features["fma"] = false;
1906    else if (Name == "sse4" || Name == "sse4.1")
1907      Features["sse41"] = Features["sse42"] = Features["avx"] =
1908        Features["avx2"] = Features["fma"] = false;
1909    else if (Name == "sse4.2")
1910      Features["sse42"] = Features["avx"] = Features["avx2"] =
1911        Features["fma"] = false;
1912    else if (Name == "3dnow")
1913      Features["3dnow"] = Features["3dnowa"] = false;
1914    else if (Name == "3dnowa")
1915      Features["3dnowa"] = false;
1916    else if (Name == "aes")
1917      Features["aes"] = false;
1918    else if (Name == "pclmul")
1919      Features["pclmul"] = false;
1920    else if (Name == "avx")
1921      Features["avx"] = Features["avx2"] = Features["fma"] =
1922        Features["fma4"] = Features["xop"] = false;
1923    else if (Name == "avx2")
1924      Features["avx2"] = false;
1925    else if (Name == "fma")
1926      Features["fma"] = false;
1927    else if (Name == "sse4a")
1928      Features["sse4a"] = Features["fma4"] = Features["xop"] = false;
1929    else if (Name == "lzcnt")
1930      Features["lzcnt"] = false;
1931    else if (Name == "rdrnd")
1932      Features["rdrand"] = false;
1933    else if (Name == "bmi")
1934      Features["bmi"] = false;
1935    else if (Name == "bmi2")
1936      Features["bmi2"] = false;
1937    else if (Name == "popcnt")
1938      Features["popcnt"] = false;
1939    else if (Name == "fma4")
1940      Features["fma4"] = Features["xop"] = false;
1941    else if (Name == "xop")
1942      Features["xop"] = false;
1943  }
1944
1945  return true;
1946}
1947
1948/// HandleTargetOptions - Perform initialization based on the user
1949/// configured set of features.
1950void X86TargetInfo::HandleTargetFeatures(std::vector<std::string> &Features) {
1951  // Remember the maximum enabled sselevel.
1952  for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
1953    // Ignore disabled features.
1954    if (Features[i][0] == '-')
1955      continue;
1956
1957    StringRef Feature = StringRef(Features[i]).substr(1);
1958
1959    if (Feature == "aes") {
1960      HasAES = true;
1961      continue;
1962    }
1963
1964    if (Feature == "pclmul") {
1965      HasPCLMUL = true;
1966      continue;
1967    }
1968
1969    if (Feature == "lzcnt") {
1970      HasLZCNT = true;
1971      continue;
1972    }
1973
1974    if (Feature == "rdrand") {
1975      HasRDRND = true;
1976      continue;
1977    }
1978
1979    if (Feature == "bmi") {
1980      HasBMI = true;
1981      continue;
1982    }
1983
1984    if (Feature == "bmi2") {
1985      HasBMI2 = true;
1986      continue;
1987    }
1988
1989    if (Feature == "popcnt") {
1990      HasPOPCNT = true;
1991      continue;
1992    }
1993
1994    if (Feature == "sse4a") {
1995      HasSSE4a = true;
1996      continue;
1997    }
1998
1999    if (Feature == "fma4") {
2000      HasFMA4 = true;
2001      continue;
2002    }
2003
2004    if (Feature == "fma") {
2005      HasFMA = true;
2006      continue;
2007    }
2008
2009    if (Feature == "xop") {
2010      HasXOP = true;
2011      continue;
2012    }
2013
2014    assert(Features[i][0] == '+' && "Invalid target feature!");
2015    X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
2016      .Case("avx2", AVX2)
2017      .Case("avx", AVX)
2018      .Case("sse42", SSE42)
2019      .Case("sse41", SSE41)
2020      .Case("ssse3", SSSE3)
2021      .Case("sse3", SSE3)
2022      .Case("sse2", SSE2)
2023      .Case("sse", SSE1)
2024      .Default(NoSSE);
2025    SSELevel = std::max(SSELevel, Level);
2026
2027    MMX3DNowEnum ThreeDNowLevel =
2028      llvm::StringSwitch<MMX3DNowEnum>(Feature)
2029        .Case("3dnowa", AMD3DNowAthlon)
2030        .Case("3dnow", AMD3DNow)
2031        .Case("mmx", MMX)
2032        .Default(NoMMX3DNow);
2033
2034    MMX3DNowLevel = std::max(MMX3DNowLevel, ThreeDNowLevel);
2035  }
2036
2037  // Don't tell the backend if we're turning off mmx; it will end up disabling
2038  // SSE, which we don't want.
2039  std::vector<std::string>::iterator it;
2040  it = std::find(Features.begin(), Features.end(), "-mmx");
2041  if (it != Features.end())
2042    Features.erase(it);
2043}
2044
2045/// X86TargetInfo::getTargetDefines - Return the set of the X86-specific macro
2046/// definitions for this particular subtarget.
2047void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
2048                                     MacroBuilder &Builder) const {
2049  // Target identification.
2050  if (PointerWidth == 64) {
2051    if (getLongWidth() == 64) {
2052      Builder.defineMacro("_LP64");
2053      Builder.defineMacro("__LP64__");
2054    }
2055    Builder.defineMacro("__amd64__");
2056    Builder.defineMacro("__amd64");
2057    Builder.defineMacro("__x86_64");
2058    Builder.defineMacro("__x86_64__");
2059  } else {
2060    DefineStd(Builder, "i386", Opts);
2061  }
2062
2063  // Subtarget options.
2064  // FIXME: We are hard-coding the tune parameters based on the CPU, but they
2065  // truly should be based on -mtune options.
2066  switch (CPU) {
2067  case CK_Generic:
2068    break;
2069  case CK_i386:
2070    // The rest are coming from the i386 define above.
2071    Builder.defineMacro("__tune_i386__");
2072    break;
2073  case CK_i486:
2074  case CK_WinChipC6:
2075  case CK_WinChip2:
2076  case CK_C3:
2077    defineCPUMacros(Builder, "i486");
2078    break;
2079  case CK_PentiumMMX:
2080    Builder.defineMacro("__pentium_mmx__");
2081    Builder.defineMacro("__tune_pentium_mmx__");
2082    // Fallthrough
2083  case CK_i586:
2084  case CK_Pentium:
2085    defineCPUMacros(Builder, "i586");
2086    defineCPUMacros(Builder, "pentium");
2087    break;
2088  case CK_Pentium3:
2089  case CK_Pentium3M:
2090  case CK_PentiumM:
2091    Builder.defineMacro("__tune_pentium3__");
2092    // Fallthrough
2093  case CK_Pentium2:
2094  case CK_C3_2:
2095    Builder.defineMacro("__tune_pentium2__");
2096    // Fallthrough
2097  case CK_PentiumPro:
2098    Builder.defineMacro("__tune_i686__");
2099    Builder.defineMacro("__tune_pentiumpro__");
2100    // Fallthrough
2101  case CK_i686:
2102    Builder.defineMacro("__i686");
2103    Builder.defineMacro("__i686__");
2104    // Strangely, __tune_i686__ isn't defined by GCC when CPU == i686.
2105    Builder.defineMacro("__pentiumpro");
2106    Builder.defineMacro("__pentiumpro__");
2107    break;
2108  case CK_Pentium4:
2109  case CK_Pentium4M:
2110    defineCPUMacros(Builder, "pentium4");
2111    break;
2112  case CK_Yonah:
2113  case CK_Prescott:
2114  case CK_Nocona:
2115    defineCPUMacros(Builder, "nocona");
2116    break;
2117  case CK_Core2:
2118  case CK_Penryn:
2119    defineCPUMacros(Builder, "core2");
2120    break;
2121  case CK_Atom:
2122    defineCPUMacros(Builder, "atom");
2123    break;
2124  case CK_Corei7:
2125  case CK_Corei7AVX:
2126  case CK_CoreAVXi:
2127  case CK_CoreAVX2:
2128    defineCPUMacros(Builder, "corei7");
2129    break;
2130  case CK_K6_2:
2131    Builder.defineMacro("__k6_2__");
2132    Builder.defineMacro("__tune_k6_2__");
2133    // Fallthrough
2134  case CK_K6_3:
2135    if (CPU != CK_K6_2) {  // In case of fallthrough
2136      // FIXME: GCC may be enabling these in cases where some other k6
2137      // architecture is specified but -m3dnow is explicitly provided. The
2138      // exact semantics need to be determined and emulated here.
2139      Builder.defineMacro("__k6_3__");
2140      Builder.defineMacro("__tune_k6_3__");
2141    }
2142    // Fallthrough
2143  case CK_K6:
2144    defineCPUMacros(Builder, "k6");
2145    break;
2146  case CK_Athlon:
2147  case CK_AthlonThunderbird:
2148  case CK_Athlon4:
2149  case CK_AthlonXP:
2150  case CK_AthlonMP:
2151    defineCPUMacros(Builder, "athlon");
2152    if (SSELevel != NoSSE) {
2153      Builder.defineMacro("__athlon_sse__");
2154      Builder.defineMacro("__tune_athlon_sse__");
2155    }
2156    break;
2157  case CK_K8:
2158  case CK_K8SSE3:
2159  case CK_x86_64:
2160  case CK_Opteron:
2161  case CK_OpteronSSE3:
2162  case CK_Athlon64:
2163  case CK_Athlon64SSE3:
2164  case CK_AthlonFX:
2165    defineCPUMacros(Builder, "k8");
2166    break;
2167  case CK_AMDFAM10:
2168    defineCPUMacros(Builder, "amdfam10");
2169    break;
2170  case CK_BTVER1:
2171    defineCPUMacros(Builder, "btver1");
2172    break;
2173  case CK_BDVER1:
2174    defineCPUMacros(Builder, "bdver1");
2175    break;
2176  case CK_BDVER2:
2177    defineCPUMacros(Builder, "bdver2");
2178    break;
2179  case CK_Geode:
2180    defineCPUMacros(Builder, "geode");
2181    break;
2182  }
2183
2184  // Target properties.
2185  Builder.defineMacro("__LITTLE_ENDIAN__");
2186  Builder.defineMacro("__REGISTER_PREFIX__", "");
2187
2188  // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
2189  // functions in glibc header files that use FP Stack inline asm which the
2190  // backend can't deal with (PR879).
2191  Builder.defineMacro("__NO_MATH_INLINES");
2192
2193  if (HasAES)
2194    Builder.defineMacro("__AES__");
2195
2196  if (HasPCLMUL)
2197    Builder.defineMacro("__PCLMUL__");
2198
2199  if (HasLZCNT)
2200    Builder.defineMacro("__LZCNT__");
2201
2202  if (HasRDRND)
2203    Builder.defineMacro("__RDRND__");
2204
2205  if (HasBMI)
2206    Builder.defineMacro("__BMI__");
2207
2208  if (HasBMI2)
2209    Builder.defineMacro("__BMI2__");
2210
2211  if (HasPOPCNT)
2212    Builder.defineMacro("__POPCNT__");
2213
2214  if (HasSSE4a)
2215    Builder.defineMacro("__SSE4A__");
2216
2217  if (HasFMA4)
2218    Builder.defineMacro("__FMA4__");
2219
2220  if (HasFMA)
2221    Builder.defineMacro("__FMA__");
2222
2223  if (HasXOP)
2224    Builder.defineMacro("__XOP__");
2225
2226  // Each case falls through to the previous one here.
2227  switch (SSELevel) {
2228  case AVX2:
2229    Builder.defineMacro("__AVX2__");
2230  case AVX:
2231    Builder.defineMacro("__AVX__");
2232  case SSE42:
2233    Builder.defineMacro("__SSE4_2__");
2234  case SSE41:
2235    Builder.defineMacro("__SSE4_1__");
2236  case SSSE3:
2237    Builder.defineMacro("__SSSE3__");
2238  case SSE3:
2239    Builder.defineMacro("__SSE3__");
2240  case SSE2:
2241    Builder.defineMacro("__SSE2__");
2242    Builder.defineMacro("__SSE2_MATH__");  // -mfp-math=sse always implied.
2243  case SSE1:
2244    Builder.defineMacro("__SSE__");
2245    Builder.defineMacro("__SSE_MATH__");   // -mfp-math=sse always implied.
2246  case NoSSE:
2247    break;
2248  }
2249
2250  if (Opts.MicrosoftExt && PointerWidth == 32) {
2251    switch (SSELevel) {
2252    case AVX2:
2253    case AVX:
2254    case SSE42:
2255    case SSE41:
2256    case SSSE3:
2257    case SSE3:
2258    case SSE2:
2259      Builder.defineMacro("_M_IX86_FP", Twine(2));
2260      break;
2261    case SSE1:
2262      Builder.defineMacro("_M_IX86_FP", Twine(1));
2263      break;
2264    default:
2265      Builder.defineMacro("_M_IX86_FP", Twine(0));
2266    }
2267  }
2268
2269  // Each case falls through to the previous one here.
2270  switch (MMX3DNowLevel) {
2271  case AMD3DNowAthlon:
2272    Builder.defineMacro("__3dNOW_A__");
2273  case AMD3DNow:
2274    Builder.defineMacro("__3dNOW__");
2275  case MMX:
2276    Builder.defineMacro("__MMX__");
2277  case NoMMX3DNow:
2278    break;
2279  }
2280}
2281
2282bool X86TargetInfo::hasFeature(StringRef Feature) const {
2283  return llvm::StringSwitch<bool>(Feature)
2284      .Case("aes", HasAES)
2285      .Case("avx", SSELevel >= AVX)
2286      .Case("avx2", SSELevel >= AVX2)
2287      .Case("bmi", HasBMI)
2288      .Case("bmi2", HasBMI2)
2289      .Case("fma", HasFMA)
2290      .Case("fma4", HasFMA4)
2291      .Case("lzcnt", HasLZCNT)
2292      .Case("rdrnd", HasRDRND)
2293      .Case("mm3dnow", MMX3DNowLevel >= AMD3DNow)
2294      .Case("mm3dnowa", MMX3DNowLevel >= AMD3DNowAthlon)
2295      .Case("mmx", MMX3DNowLevel >= MMX)
2296      .Case("pclmul", HasPCLMUL)
2297      .Case("popcnt", HasPOPCNT)
2298      .Case("sse", SSELevel >= SSE1)
2299      .Case("sse2", SSELevel >= SSE2)
2300      .Case("sse3", SSELevel >= SSE3)
2301      .Case("ssse3", SSELevel >= SSSE3)
2302      .Case("sse41", SSELevel >= SSE41)
2303      .Case("sse42", SSELevel >= SSE42)
2304      .Case("sse4a", HasSSE4a)
2305      .Case("x86", true)
2306      .Case("x86_32", PointerWidth == 32)
2307      .Case("x86_64", PointerWidth == 64)
2308      .Case("xop", HasXOP)
2309      .Default(false);
2310}
2311
2312bool
2313X86TargetInfo::validateAsmConstraint(const char *&Name,
2314                                     TargetInfo::ConstraintInfo &Info) const {
2315  switch (*Name) {
2316  default: return false;
2317  case 'Y': // first letter of a pair:
2318    switch (*(Name+1)) {
2319    default: return false;
2320    case '0':  // First SSE register.
2321    case 't':  // Any SSE register, when SSE2 is enabled.
2322    case 'i':  // Any SSE register, when SSE2 and inter-unit moves enabled.
2323    case 'm':  // any MMX register, when inter-unit moves enabled.
2324      break;   // falls through to setAllowsRegister.
2325  }
2326  case 'a': // eax.
2327  case 'b': // ebx.
2328  case 'c': // ecx.
2329  case 'd': // edx.
2330  case 'S': // esi.
2331  case 'D': // edi.
2332  case 'A': // edx:eax.
2333  case 'f': // any x87 floating point stack register.
2334  case 't': // top of floating point stack.
2335  case 'u': // second from top of floating point stack.
2336  case 'q': // Any register accessible as [r]l: a, b, c, and d.
2337  case 'y': // Any MMX register.
2338  case 'x': // Any SSE register.
2339  case 'Q': // Any register accessible as [r]h: a, b, c, and d.
2340  case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
2341  case 'l': // "Index" registers: any general register that can be used as an
2342            // index in a base+index memory access.
2343    Info.setAllowsRegister();
2344    return true;
2345  case 'C': // SSE floating point constant.
2346  case 'G': // x87 floating point constant.
2347  case 'e': // 32-bit signed integer constant for use with zero-extending
2348            // x86_64 instructions.
2349  case 'Z': // 32-bit unsigned integer constant for use with zero-extending
2350            // x86_64 instructions.
2351    return true;
2352  }
2353}
2354
2355
2356std::string
2357X86TargetInfo::convertConstraint(const char *&Constraint) const {
2358  switch (*Constraint) {
2359  case 'a': return std::string("{ax}");
2360  case 'b': return std::string("{bx}");
2361  case 'c': return std::string("{cx}");
2362  case 'd': return std::string("{dx}");
2363  case 'S': return std::string("{si}");
2364  case 'D': return std::string("{di}");
2365  case 'p': // address
2366    return std::string("im");
2367  case 't': // top of floating point stack.
2368    return std::string("{st}");
2369  case 'u': // second from top of floating point stack.
2370    return std::string("{st(1)}"); // second from top of floating point stack.
2371  default:
2372    return std::string(1, *Constraint);
2373  }
2374}
2375} // end anonymous namespace
2376
2377namespace {
2378// X86-32 generic target
2379class X86_32TargetInfo : public X86TargetInfo {
2380public:
2381  X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
2382    DoubleAlign = LongLongAlign = 32;
2383    LongDoubleWidth = 96;
2384    LongDoubleAlign = 32;
2385    SuitableAlign = 128;
2386    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2387                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
2388                        "a0:0:64-f80:32:32-n8:16:32-S128";
2389    SizeType = UnsignedInt;
2390    PtrDiffType = SignedInt;
2391    IntPtrType = SignedInt;
2392    RegParmMax = 3;
2393
2394    // Use fpret for all types.
2395    RealTypeUsesObjCFPRet = ((1 << TargetInfo::Float) |
2396                             (1 << TargetInfo::Double) |
2397                             (1 << TargetInfo::LongDouble));
2398
2399    // x86-32 has atomics up to 8 bytes
2400    // FIXME: Check that we actually have cmpxchg8b before setting
2401    // MaxAtomicInlineWidth. (cmpxchg8b is an i586 instruction.)
2402    MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
2403  }
2404  virtual BuiltinVaListKind getBuiltinVaListKind() const {
2405    return TargetInfo::CharPtrBuiltinVaList;
2406  }
2407
2408  int getEHDataRegisterNumber(unsigned RegNo) const {
2409    if (RegNo == 0) return 0;
2410    if (RegNo == 1) return 2;
2411    return -1;
2412  }
2413};
2414} // end anonymous namespace
2415
2416namespace {
2417class NetBSDI386TargetInfo : public NetBSDTargetInfo<X86_32TargetInfo> {
2418public:
2419  NetBSDI386TargetInfo(const std::string &triple) :
2420    NetBSDTargetInfo<X86_32TargetInfo>(triple) {
2421  }
2422
2423  virtual unsigned getFloatEvalMethod() const {
2424    // NetBSD defaults to "double" rounding
2425    return 1;
2426  }
2427};
2428} // end anonymous namespace
2429
2430namespace {
2431class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
2432public:
2433  OpenBSDI386TargetInfo(const std::string& triple) :
2434    OpenBSDTargetInfo<X86_32TargetInfo>(triple) {
2435    SizeType = UnsignedLong;
2436    IntPtrType = SignedLong;
2437    PtrDiffType = SignedLong;
2438  }
2439};
2440} // end anonymous namespace
2441
2442namespace {
2443class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
2444public:
2445  DarwinI386TargetInfo(const std::string& triple) :
2446    DarwinTargetInfo<X86_32TargetInfo>(triple) {
2447    LongDoubleWidth = 128;
2448    LongDoubleAlign = 128;
2449    SuitableAlign = 128;
2450    SizeType = UnsignedLong;
2451    IntPtrType = SignedLong;
2452    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2453                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
2454                        "a0:0:64-f80:128:128-n8:16:32-S128";
2455    HasAlignMac68kSupport = true;
2456  }
2457
2458};
2459} // end anonymous namespace
2460
2461namespace {
2462// x86-32 Windows target
2463class WindowsX86_32TargetInfo : public WindowsTargetInfo<X86_32TargetInfo> {
2464public:
2465  WindowsX86_32TargetInfo(const std::string& triple)
2466    : WindowsTargetInfo<X86_32TargetInfo>(triple) {
2467    TLSSupported = false;
2468    WCharType = UnsignedShort;
2469    DoubleAlign = LongLongAlign = 64;
2470    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2471                        "i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-"
2472                        "v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32";
2473  }
2474  virtual void getTargetDefines(const LangOptions &Opts,
2475                                MacroBuilder &Builder) const {
2476    WindowsTargetInfo<X86_32TargetInfo>::getTargetDefines(Opts, Builder);
2477  }
2478};
2479} // end anonymous namespace
2480
2481namespace {
2482
2483// x86-32 Windows Visual Studio target
2484class VisualStudioWindowsX86_32TargetInfo : public WindowsX86_32TargetInfo {
2485public:
2486  VisualStudioWindowsX86_32TargetInfo(const std::string& triple)
2487    : WindowsX86_32TargetInfo(triple) {
2488    LongDoubleWidth = LongDoubleAlign = 64;
2489    LongDoubleFormat = &llvm::APFloat::IEEEdouble;
2490  }
2491  virtual void getTargetDefines(const LangOptions &Opts,
2492                                MacroBuilder &Builder) const {
2493    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
2494    WindowsX86_32TargetInfo::getVisualStudioDefines(Opts, Builder);
2495    // The value of the following reflects processor type.
2496    // 300=386, 400=486, 500=Pentium, 600=Blend (default)
2497    // We lost the original triple, so we use the default.
2498    Builder.defineMacro("_M_IX86", "600");
2499  }
2500};
2501} // end anonymous namespace
2502
2503namespace {
2504// x86-32 MinGW target
2505class MinGWX86_32TargetInfo : public WindowsX86_32TargetInfo {
2506public:
2507  MinGWX86_32TargetInfo(const std::string& triple)
2508    : WindowsX86_32TargetInfo(triple) {
2509  }
2510  virtual void getTargetDefines(const LangOptions &Opts,
2511                                MacroBuilder &Builder) const {
2512    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
2513    DefineStd(Builder, "WIN32", Opts);
2514    DefineStd(Builder, "WINNT", Opts);
2515    Builder.defineMacro("_X86_");
2516    Builder.defineMacro("__MSVCRT__");
2517    Builder.defineMacro("__MINGW32__");
2518
2519    // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
2520    // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
2521    if (Opts.MicrosoftExt)
2522      // Provide "as-is" __declspec.
2523      Builder.defineMacro("__declspec", "__declspec");
2524    else
2525      // Provide alias of __attribute__ like mingw32-gcc.
2526      Builder.defineMacro("__declspec(a)", "__attribute__((a))");
2527  }
2528};
2529} // end anonymous namespace
2530
2531namespace {
2532// x86-32 Cygwin target
2533class CygwinX86_32TargetInfo : public X86_32TargetInfo {
2534public:
2535  CygwinX86_32TargetInfo(const std::string& triple)
2536    : X86_32TargetInfo(triple) {
2537    TLSSupported = false;
2538    WCharType = UnsignedShort;
2539    DoubleAlign = LongLongAlign = 64;
2540    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2541                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
2542                        "a0:0:64-f80:32:32-n8:16:32-S32";
2543  }
2544  virtual void getTargetDefines(const LangOptions &Opts,
2545                                MacroBuilder &Builder) const {
2546    X86_32TargetInfo::getTargetDefines(Opts, Builder);
2547    Builder.defineMacro("__CYGWIN__");
2548    Builder.defineMacro("__CYGWIN32__");
2549    DefineStd(Builder, "unix", Opts);
2550    if (Opts.CPlusPlus)
2551      Builder.defineMacro("_GNU_SOURCE");
2552  }
2553};
2554} // end anonymous namespace
2555
2556namespace {
2557// x86-32 Haiku target
2558class HaikuX86_32TargetInfo : public X86_32TargetInfo {
2559public:
2560  HaikuX86_32TargetInfo(const std::string& triple)
2561    : X86_32TargetInfo(triple) {
2562    SizeType = UnsignedLong;
2563    IntPtrType = SignedLong;
2564    PtrDiffType = SignedLong;
2565    this->UserLabelPrefix = "";
2566  }
2567  virtual void getTargetDefines(const LangOptions &Opts,
2568                                MacroBuilder &Builder) const {
2569    X86_32TargetInfo::getTargetDefines(Opts, Builder);
2570    Builder.defineMacro("__INTEL__");
2571    Builder.defineMacro("__HAIKU__");
2572  }
2573};
2574} // end anonymous namespace
2575
2576// RTEMS Target
2577template<typename Target>
2578class RTEMSTargetInfo : public OSTargetInfo<Target> {
2579protected:
2580  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
2581                            MacroBuilder &Builder) const {
2582    // RTEMS defines; list based off of gcc output
2583
2584    Builder.defineMacro("__rtems__");
2585    Builder.defineMacro("__ELF__");
2586  }
2587public:
2588  RTEMSTargetInfo(const std::string &triple)
2589    : OSTargetInfo<Target>(triple) {
2590      this->UserLabelPrefix = "";
2591
2592      llvm::Triple Triple(triple);
2593      switch (Triple.getArch()) {
2594        default:
2595        case llvm::Triple::x86:
2596          // this->MCountName = ".mcount";
2597          break;
2598        case llvm::Triple::mips:
2599        case llvm::Triple::mipsel:
2600        case llvm::Triple::ppc:
2601        case llvm::Triple::ppc64:
2602          // this->MCountName = "_mcount";
2603          break;
2604        case llvm::Triple::arm:
2605          // this->MCountName = "__mcount";
2606          break;
2607      }
2608
2609    }
2610};
2611
2612namespace {
2613// x86-32 RTEMS target
2614class RTEMSX86_32TargetInfo : public X86_32TargetInfo {
2615public:
2616  RTEMSX86_32TargetInfo(const std::string& triple)
2617    : X86_32TargetInfo(triple) {
2618    SizeType = UnsignedLong;
2619    IntPtrType = SignedLong;
2620    PtrDiffType = SignedLong;
2621    this->UserLabelPrefix = "";
2622  }
2623  virtual void getTargetDefines(const LangOptions &Opts,
2624                                MacroBuilder &Builder) const {
2625    X86_32TargetInfo::getTargetDefines(Opts, Builder);
2626    Builder.defineMacro("__INTEL__");
2627    Builder.defineMacro("__rtems__");
2628  }
2629};
2630} // end anonymous namespace
2631
2632namespace {
2633// x86-64 generic target
2634class X86_64TargetInfo : public X86TargetInfo {
2635public:
2636  X86_64TargetInfo(const std::string &triple) : X86TargetInfo(triple) {
2637    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
2638    LongDoubleWidth = 128;
2639    LongDoubleAlign = 128;
2640    LargeArrayMinWidth = 128;
2641    LargeArrayAlign = 128;
2642    SuitableAlign = 128;
2643    IntMaxType = SignedLong;
2644    UIntMaxType = UnsignedLong;
2645    Int64Type = SignedLong;
2646    RegParmMax = 6;
2647
2648    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2649                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
2650                        "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128";
2651
2652    // Use fpret only for long double.
2653    RealTypeUsesObjCFPRet = (1 << TargetInfo::LongDouble);
2654
2655    // Use fp2ret for _Complex long double.
2656    ComplexLongDoubleUsesFP2Ret = true;
2657
2658    // x86-64 has atomics up to 16 bytes.
2659    // FIXME: Once the backend is fixed, increase MaxAtomicInlineWidth to 128
2660    // on CPUs with cmpxchg16b
2661    MaxAtomicPromoteWidth = 128;
2662    MaxAtomicInlineWidth = 64;
2663  }
2664  virtual BuiltinVaListKind getBuiltinVaListKind() const {
2665    return TargetInfo::X86_64ABIBuiltinVaList;
2666  }
2667
2668  int getEHDataRegisterNumber(unsigned RegNo) const {
2669    if (RegNo == 0) return 0;
2670    if (RegNo == 1) return 1;
2671    return -1;
2672  }
2673};
2674} // end anonymous namespace
2675
2676namespace {
2677// x86-64 Windows target
2678class WindowsX86_64TargetInfo : public WindowsTargetInfo<X86_64TargetInfo> {
2679public:
2680  WindowsX86_64TargetInfo(const std::string& triple)
2681    : WindowsTargetInfo<X86_64TargetInfo>(triple) {
2682    TLSSupported = false;
2683    WCharType = UnsignedShort;
2684    LongWidth = LongAlign = 32;
2685    DoubleAlign = LongLongAlign = 64;
2686    IntMaxType = SignedLongLong;
2687    UIntMaxType = UnsignedLongLong;
2688    Int64Type = SignedLongLong;
2689    SizeType = UnsignedLongLong;
2690    PtrDiffType = SignedLongLong;
2691    IntPtrType = SignedLongLong;
2692    this->UserLabelPrefix = "";
2693  }
2694  virtual void getTargetDefines(const LangOptions &Opts,
2695                                MacroBuilder &Builder) const {
2696    WindowsTargetInfo<X86_64TargetInfo>::getTargetDefines(Opts, Builder);
2697    Builder.defineMacro("_WIN64");
2698  }
2699  virtual BuiltinVaListKind getBuiltinVaListKind() const {
2700    return TargetInfo::CharPtrBuiltinVaList;
2701  }
2702};
2703} // end anonymous namespace
2704
2705namespace {
2706// x86-64 Windows Visual Studio target
2707class VisualStudioWindowsX86_64TargetInfo : public WindowsX86_64TargetInfo {
2708public:
2709  VisualStudioWindowsX86_64TargetInfo(const std::string& triple)
2710    : WindowsX86_64TargetInfo(triple) {
2711    LongDoubleWidth = LongDoubleAlign = 64;
2712    LongDoubleFormat = &llvm::APFloat::IEEEdouble;
2713  }
2714  virtual void getTargetDefines(const LangOptions &Opts,
2715                                MacroBuilder &Builder) const {
2716    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
2717    WindowsX86_64TargetInfo::getVisualStudioDefines(Opts, Builder);
2718    Builder.defineMacro("_M_X64");
2719    Builder.defineMacro("_M_AMD64");
2720  }
2721};
2722} // end anonymous namespace
2723
2724namespace {
2725// x86-64 MinGW target
2726class MinGWX86_64TargetInfo : public WindowsX86_64TargetInfo {
2727public:
2728  MinGWX86_64TargetInfo(const std::string& triple)
2729    : WindowsX86_64TargetInfo(triple) {
2730  }
2731  virtual void getTargetDefines(const LangOptions &Opts,
2732                                MacroBuilder &Builder) const {
2733    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
2734    DefineStd(Builder, "WIN64", Opts);
2735    Builder.defineMacro("__MSVCRT__");
2736    Builder.defineMacro("__MINGW32__");
2737    Builder.defineMacro("__MINGW64__");
2738
2739    // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
2740    // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
2741    if (Opts.MicrosoftExt)
2742      // Provide "as-is" __declspec.
2743      Builder.defineMacro("__declspec", "__declspec");
2744    else
2745      // Provide alias of __attribute__ like mingw32-gcc.
2746      Builder.defineMacro("__declspec(a)", "__attribute__((a))");
2747  }
2748};
2749} // end anonymous namespace
2750
2751namespace {
2752class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
2753public:
2754  DarwinX86_64TargetInfo(const std::string& triple)
2755      : DarwinTargetInfo<X86_64TargetInfo>(triple) {
2756    Int64Type = SignedLongLong;
2757  }
2758};
2759} // end anonymous namespace
2760
2761namespace {
2762class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
2763public:
2764  OpenBSDX86_64TargetInfo(const std::string& triple)
2765      : OpenBSDTargetInfo<X86_64TargetInfo>(triple) {
2766    IntMaxType = SignedLongLong;
2767    UIntMaxType = UnsignedLongLong;
2768    Int64Type = SignedLongLong;
2769  }
2770};
2771} // end anonymous namespace
2772
2773namespace {
2774class ARMTargetInfo : public TargetInfo {
2775  // Possible FPU choices.
2776  enum FPUMode {
2777    NoFPU,
2778    VFP2FPU,
2779    VFP3FPU,
2780    NeonFPU
2781  };
2782
2783  static bool FPUModeIsVFP(FPUMode Mode) {
2784    return Mode >= VFP2FPU && Mode <= NeonFPU;
2785  }
2786
2787  static const TargetInfo::GCCRegAlias GCCRegAliases[];
2788  static const char * const GCCRegNames[];
2789
2790  std::string ABI, CPU;
2791
2792  unsigned FPU : 3;
2793
2794  unsigned IsThumb : 1;
2795
2796  // Initialized via features.
2797  unsigned SoftFloat : 1;
2798  unsigned SoftFloatABI : 1;
2799
2800  static const Builtin::Info BuiltinInfo[];
2801
2802public:
2803  ARMTargetInfo(const std::string &TripleStr)
2804    : TargetInfo(TripleStr), ABI("aapcs-linux"), CPU("arm1136j-s")
2805  {
2806    BigEndian = false;
2807    SizeType = UnsignedInt;
2808    PtrDiffType = SignedInt;
2809    // AAPCS 7.1.1, ARM-Linux ABI 2.4: type of wchar_t is unsigned int.
2810    WCharType = UnsignedInt;
2811
2812    // {} in inline assembly are neon specifiers, not assembly variant
2813    // specifiers.
2814    NoAsmVariants = true;
2815
2816    // FIXME: Should we just treat this as a feature?
2817    IsThumb = getTriple().getArchName().startswith("thumb");
2818    if (IsThumb) {
2819      // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
2820      // so set preferred for small types to 32.
2821      DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
2822                           "i64:64:64-f32:32:32-f64:64:64-"
2823                           "v64:64:64-v128:64:128-a0:0:32-n32-S64");
2824    } else {
2825      DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2826                           "i64:64:64-f32:32:32-f64:64:64-"
2827                           "v64:64:64-v128:64:128-a0:0:64-n32-S64");
2828    }
2829
2830    // ARM targets default to using the ARM C++ ABI.
2831    CXXABI = CXXABI_ARM;
2832
2833    // ARM has atomics up to 8 bytes
2834    // FIXME: Set MaxAtomicInlineWidth if we have the feature v6e
2835    MaxAtomicPromoteWidth = 64;
2836
2837    // Do force alignment of members that follow zero length bitfields.  If
2838    // the alignment of the zero-length bitfield is greater than the member
2839    // that follows it, `bar', `bar' will be aligned as the  type of the
2840    // zero length bitfield.
2841    UseZeroLengthBitfieldAlignment = true;
2842  }
2843  virtual const char *getABI() const { return ABI.c_str(); }
2844  virtual bool setABI(const std::string &Name) {
2845    ABI = Name;
2846
2847    // The defaults (above) are for AAPCS, check if we need to change them.
2848    //
2849    // FIXME: We need support for -meabi... we could just mangle it into the
2850    // name.
2851    if (Name == "apcs-gnu") {
2852      DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 32;
2853      SizeType = UnsignedLong;
2854
2855      // Revert to using SignedInt on apcs-gnu to comply with existing behaviour.
2856      WCharType = SignedInt;
2857
2858      // Do not respect the alignment of bit-field types when laying out
2859      // structures. This corresponds to PCC_BITFIELD_TYPE_MATTERS in gcc.
2860      UseBitFieldTypeAlignment = false;
2861
2862      /// gcc forces the alignment to 4 bytes, regardless of the type of the
2863      /// zero length bitfield.  This corresponds to EMPTY_FIELD_BOUNDARY in
2864      /// gcc.
2865      ZeroLengthBitfieldBoundary = 32;
2866
2867      if (IsThumb) {
2868        // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
2869        // so set preferred for small types to 32.
2870        DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
2871                             "i64:32:64-f32:32:32-f64:32:64-"
2872                             "v64:32:64-v128:32:128-a0:0:32-n32-S32");
2873      } else {
2874        DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2875                             "i64:32:64-f32:32:32-f64:32:64-"
2876                             "v64:32:64-v128:32:128-a0:0:32-n32-S32");
2877      }
2878
2879      // FIXME: Override "preferred align" for double and long long.
2880    } else if (Name == "aapcs") {
2881      // FIXME: Enumerated types are variable width in straight AAPCS.
2882    } else if (Name == "aapcs-linux") {
2883      ;
2884    } else
2885      return false;
2886
2887    return true;
2888  }
2889
2890  void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
2891    if (CPU == "arm1136jf-s" || CPU == "arm1176jzf-s" || CPU == "mpcore")
2892      Features["vfp2"] = true;
2893    else if (CPU == "cortex-a8" || CPU == "cortex-a9")
2894      Features["neon"] = true;
2895  }
2896
2897  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
2898                                 StringRef Name,
2899                                 bool Enabled) const {
2900    if (Name == "soft-float" || Name == "soft-float-abi" ||
2901        Name == "vfp2" || Name == "vfp3" || Name == "neon" || Name == "d16" ||
2902        Name == "neonfp") {
2903      Features[Name] = Enabled;
2904    } else
2905      return false;
2906
2907    return true;
2908  }
2909
2910  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
2911    FPU = NoFPU;
2912    SoftFloat = SoftFloatABI = false;
2913    for (unsigned i = 0, e = Features.size(); i != e; ++i) {
2914      if (Features[i] == "+soft-float")
2915        SoftFloat = true;
2916      else if (Features[i] == "+soft-float-abi")
2917        SoftFloatABI = true;
2918      else if (Features[i] == "+vfp2")
2919        FPU = VFP2FPU;
2920      else if (Features[i] == "+vfp3")
2921        FPU = VFP3FPU;
2922      else if (Features[i] == "+neon")
2923        FPU = NeonFPU;
2924    }
2925
2926    // Remove front-end specific options which the backend handles differently.
2927    std::vector<std::string>::iterator it;
2928    it = std::find(Features.begin(), Features.end(), "+soft-float");
2929    if (it != Features.end())
2930      Features.erase(it);
2931    it = std::find(Features.begin(), Features.end(), "+soft-float-abi");
2932    if (it != Features.end())
2933      Features.erase(it);
2934  }
2935
2936  virtual bool hasFeature(StringRef Feature) const {
2937    return llvm::StringSwitch<bool>(Feature)
2938        .Case("arm", true)
2939        .Case("softfloat", SoftFloat)
2940        .Case("thumb", IsThumb)
2941        .Case("neon", FPU == NeonFPU && !SoftFloat &&
2942              StringRef(getCPUDefineSuffix(CPU)).startswith("7"))
2943        .Default(false);
2944  }
2945  static const char *getCPUDefineSuffix(StringRef Name) {
2946    return llvm::StringSwitch<const char*>(Name)
2947      .Cases("arm8", "arm810", "4")
2948      .Cases("strongarm", "strongarm110", "strongarm1100", "strongarm1110", "4")
2949      .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "arm720t", "arm9", "4T")
2950      .Cases("arm9tdmi", "arm920", "arm920t", "arm922t", "arm940t", "4T")
2951      .Case("ep9312", "4T")
2952      .Cases("arm10tdmi", "arm1020t", "5T")
2953      .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "5TE")
2954      .Case("arm926ej-s", "5TEJ")
2955      .Cases("arm10e", "arm1020e", "arm1022e", "5TE")
2956      .Cases("xscale", "iwmmxt", "5TE")
2957      .Case("arm1136j-s", "6J")
2958      .Cases("arm1176jz-s", "arm1176jzf-s", "6ZK")
2959      .Cases("arm1136jf-s", "mpcorenovfp", "mpcore", "6K")
2960      .Cases("arm1156t2-s", "arm1156t2f-s", "6T2")
2961      .Cases("cortex-a8", "cortex-a9", "7A")
2962      .Case("cortex-m3", "7M")
2963      .Case("cortex-m4", "7M")
2964      .Case("cortex-m0", "6M")
2965      .Default(0);
2966  }
2967  virtual bool setCPU(const std::string &Name) {
2968    if (!getCPUDefineSuffix(Name))
2969      return false;
2970
2971    CPU = Name;
2972    return true;
2973  }
2974  virtual void getTargetDefines(const LangOptions &Opts,
2975                                MacroBuilder &Builder) const {
2976    // Target identification.
2977    Builder.defineMacro("__arm");
2978    Builder.defineMacro("__arm__");
2979
2980    // Target properties.
2981    Builder.defineMacro("__ARMEL__");
2982    Builder.defineMacro("__LITTLE_ENDIAN__");
2983    Builder.defineMacro("__REGISTER_PREFIX__", "");
2984
2985    StringRef CPUArch = getCPUDefineSuffix(CPU);
2986    Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
2987
2988    // Subtarget options.
2989
2990    // FIXME: It's more complicated than this and we don't really support
2991    // interworking.
2992    if ('5' <= CPUArch[0] && CPUArch[0] <= '7')
2993      Builder.defineMacro("__THUMB_INTERWORK__");
2994
2995    if (ABI == "aapcs" || ABI == "aapcs-linux")
2996      Builder.defineMacro("__ARM_EABI__");
2997
2998    if (SoftFloat)
2999      Builder.defineMacro("__SOFTFP__");
3000
3001    if (CPU == "xscale")
3002      Builder.defineMacro("__XSCALE__");
3003
3004    bool IsARMv7 = CPUArch.startswith("7");
3005    if (IsThumb) {
3006      Builder.defineMacro("__THUMBEL__");
3007      Builder.defineMacro("__thumb__");
3008      if (CPUArch == "6T2" || IsARMv7)
3009        Builder.defineMacro("__thumb2__");
3010    }
3011
3012    // Note, this is always on in gcc, even though it doesn't make sense.
3013    Builder.defineMacro("__APCS_32__");
3014
3015    if (FPUModeIsVFP((FPUMode) FPU))
3016      Builder.defineMacro("__VFP_FP__");
3017
3018    // This only gets set when Neon instructions are actually available, unlike
3019    // the VFP define, hence the soft float and arch check. This is subtly
3020    // different from gcc, we follow the intent which was that it should be set
3021    // when Neon instructions are actually available.
3022    if (FPU == NeonFPU && !SoftFloat && IsARMv7)
3023      Builder.defineMacro("__ARM_NEON__");
3024  }
3025  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3026                                 unsigned &NumRecords) const {
3027    Records = BuiltinInfo;
3028    NumRecords = clang::ARM::LastTSBuiltin-Builtin::FirstTSBuiltin;
3029  }
3030  virtual bool isCLZForZeroUndef() const { return false; }
3031  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3032    return TargetInfo::VoidPtrBuiltinVaList;
3033  }
3034  virtual void getGCCRegNames(const char * const *&Names,
3035                              unsigned &NumNames) const;
3036  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3037                                unsigned &NumAliases) const;
3038  virtual bool validateAsmConstraint(const char *&Name,
3039                                     TargetInfo::ConstraintInfo &Info) const {
3040    // FIXME: Check if this is complete
3041    switch (*Name) {
3042    default:
3043    case 'l': // r0-r7
3044    case 'h': // r8-r15
3045    case 'w': // VFP Floating point register single precision
3046    case 'P': // VFP Floating point register double precision
3047      Info.setAllowsRegister();
3048      return true;
3049    case 'Q': // A memory address that is a single base register.
3050      Info.setAllowsMemory();
3051      return true;
3052    case 'U': // a memory reference...
3053      switch (Name[1]) {
3054      case 'q': // ...ARMV4 ldrsb
3055      case 'v': // ...VFP load/store (reg+constant offset)
3056      case 'y': // ...iWMMXt load/store
3057      case 't': // address valid for load/store opaque types wider
3058	        // than 128-bits
3059      case 'n': // valid address for Neon doubleword vector load/store
3060      case 'm': // valid address for Neon element and structure load/store
3061      case 's': // valid address for non-offset loads/stores of quad-word
3062	        // values in four ARM registers
3063        Info.setAllowsMemory();
3064        Name++;
3065        return true;
3066      }
3067    }
3068    return false;
3069  }
3070  virtual std::string convertConstraint(const char *&Constraint) const {
3071    std::string R;
3072    switch (*Constraint) {
3073    case 'U':   // Two-character constraint; add "^" hint for later parsing.
3074      R = std::string("^") + std::string(Constraint, 2);
3075      Constraint++;
3076      break;
3077    case 'p': // 'p' should be translated to 'r' by default.
3078      R = std::string("r");
3079      break;
3080    default:
3081      return std::string(1, *Constraint);
3082    }
3083    return R;
3084  }
3085  virtual const char *getClobbers() const {
3086    // FIXME: Is this really right?
3087    return "";
3088  }
3089};
3090
3091const char * const ARMTargetInfo::GCCRegNames[] = {
3092  // Integer registers
3093  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3094  "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
3095
3096  // Float registers
3097  "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
3098  "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
3099  "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
3100  "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
3101
3102  // Double registers
3103  "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
3104  "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
3105  "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
3106  "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
3107
3108  // Quad registers
3109  "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
3110  "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"
3111};
3112
3113void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
3114                                   unsigned &NumNames) const {
3115  Names = GCCRegNames;
3116  NumNames = llvm::array_lengthof(GCCRegNames);
3117}
3118
3119const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
3120  { { "a1" }, "r0" },
3121  { { "a2" }, "r1" },
3122  { { "a3" }, "r2" },
3123  { { "a4" }, "r3" },
3124  { { "v1" }, "r4" },
3125  { { "v2" }, "r5" },
3126  { { "v3" }, "r6" },
3127  { { "v4" }, "r7" },
3128  { { "v5" }, "r8" },
3129  { { "v6", "rfp" }, "r9" },
3130  { { "sl" }, "r10" },
3131  { { "fp" }, "r11" },
3132  { { "ip" }, "r12" },
3133  { { "r13" }, "sp" },
3134  { { "r14" }, "lr" },
3135  { { "r15" }, "pc" },
3136  // The S, D and Q registers overlap, but aren't really aliases; we
3137  // don't want to substitute one of these for a different-sized one.
3138};
3139
3140void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3141                                       unsigned &NumAliases) const {
3142  Aliases = GCCRegAliases;
3143  NumAliases = llvm::array_lengthof(GCCRegAliases);
3144}
3145
3146const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
3147#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3148#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3149                                              ALL_LANGUAGES },
3150#include "clang/Basic/BuiltinsARM.def"
3151};
3152} // end anonymous namespace.
3153
3154namespace {
3155class DarwinARMTargetInfo :
3156  public DarwinTargetInfo<ARMTargetInfo> {
3157protected:
3158  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
3159                            MacroBuilder &Builder) const {
3160    getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
3161  }
3162
3163public:
3164  DarwinARMTargetInfo(const std::string& triple)
3165    : DarwinTargetInfo<ARMTargetInfo>(triple) {
3166    HasAlignMac68kSupport = true;
3167    // iOS always has 64-bit atomic instructions.
3168    // FIXME: This should be based off of the target features in ARMTargetInfo.
3169    MaxAtomicInlineWidth = 64;
3170  }
3171};
3172} // end anonymous namespace.
3173
3174
3175namespace {
3176// Hexagon abstract base class
3177class HexagonTargetInfo : public TargetInfo {
3178  static const Builtin::Info BuiltinInfo[];
3179  static const char * const GCCRegNames[];
3180  static const TargetInfo::GCCRegAlias GCCRegAliases[];
3181  std::string CPU;
3182public:
3183  HexagonTargetInfo(const std::string& triple) : TargetInfo(triple)  {
3184    BigEndian = false;
3185    DescriptionString = ("e-p:32:32:32-"
3186                         "i64:64:64-i32:32:32-i16:16:16-i1:32:32"
3187                         "f64:64:64-f32:32:32-a0:0-n32");
3188
3189    // {} in inline assembly are packet specifiers, not assembly variant
3190    // specifiers.
3191    NoAsmVariants = true;
3192  }
3193
3194  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3195                                 unsigned &NumRecords) const {
3196    Records = BuiltinInfo;
3197    NumRecords = clang::Hexagon::LastTSBuiltin-Builtin::FirstTSBuiltin;
3198  }
3199
3200  virtual bool validateAsmConstraint(const char *&Name,
3201                                     TargetInfo::ConstraintInfo &Info) const {
3202    return true;
3203  }
3204
3205  virtual void getTargetDefines(const LangOptions &Opts,
3206                                MacroBuilder &Builder) const;
3207
3208  virtual bool hasFeature(StringRef Feature) const {
3209    return Feature == "hexagon";
3210  }
3211
3212  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3213    return TargetInfo::CharPtrBuiltinVaList;
3214  }
3215  virtual void getGCCRegNames(const char * const *&Names,
3216                              unsigned &NumNames) const;
3217  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3218                                unsigned &NumAliases) const;
3219  virtual const char *getClobbers() const {
3220    return "";
3221  }
3222
3223  static const char *getHexagonCPUSuffix(StringRef Name) {
3224    return llvm::StringSwitch<const char*>(Name)
3225      .Case("hexagonv2", "2")
3226      .Case("hexagonv3", "3")
3227      .Case("hexagonv4", "4")
3228      .Case("hexagonv5", "5")
3229      .Default(0);
3230  }
3231
3232  virtual bool setCPU(const std::string &Name) {
3233    if (!getHexagonCPUSuffix(Name))
3234      return false;
3235
3236    CPU = Name;
3237    return true;
3238  }
3239};
3240
3241void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts,
3242                                MacroBuilder &Builder) const {
3243  Builder.defineMacro("qdsp6");
3244  Builder.defineMacro("__qdsp6", "1");
3245  Builder.defineMacro("__qdsp6__", "1");
3246
3247  Builder.defineMacro("hexagon");
3248  Builder.defineMacro("__hexagon", "1");
3249  Builder.defineMacro("__hexagon__", "1");
3250
3251  if(CPU == "hexagonv1") {
3252    Builder.defineMacro("__HEXAGON_V1__");
3253    Builder.defineMacro("__HEXAGON_ARCH__", "1");
3254    if(Opts.HexagonQdsp6Compat) {
3255      Builder.defineMacro("__QDSP6_V1__");
3256      Builder.defineMacro("__QDSP6_ARCH__", "1");
3257    }
3258  }
3259  else if(CPU == "hexagonv2") {
3260    Builder.defineMacro("__HEXAGON_V2__");
3261    Builder.defineMacro("__HEXAGON_ARCH__", "2");
3262    if(Opts.HexagonQdsp6Compat) {
3263      Builder.defineMacro("__QDSP6_V2__");
3264      Builder.defineMacro("__QDSP6_ARCH__", "2");
3265    }
3266  }
3267  else if(CPU == "hexagonv3") {
3268    Builder.defineMacro("__HEXAGON_V3__");
3269    Builder.defineMacro("__HEXAGON_ARCH__", "3");
3270    if(Opts.HexagonQdsp6Compat) {
3271      Builder.defineMacro("__QDSP6_V3__");
3272      Builder.defineMacro("__QDSP6_ARCH__", "3");
3273    }
3274  }
3275  else if(CPU == "hexagonv4") {
3276    Builder.defineMacro("__HEXAGON_V4__");
3277    Builder.defineMacro("__HEXAGON_ARCH__", "4");
3278    if(Opts.HexagonQdsp6Compat) {
3279      Builder.defineMacro("__QDSP6_V4__");
3280      Builder.defineMacro("__QDSP6_ARCH__", "4");
3281    }
3282  }
3283  else if(CPU == "hexagonv5") {
3284    Builder.defineMacro("__HEXAGON_V5__");
3285    Builder.defineMacro("__HEXAGON_ARCH__", "5");
3286    if(Opts.HexagonQdsp6Compat) {
3287      Builder.defineMacro("__QDSP6_V5__");
3288      Builder.defineMacro("__QDSP6_ARCH__", "5");
3289    }
3290  }
3291}
3292
3293const char * const HexagonTargetInfo::GCCRegNames[] = {
3294  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3295  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
3296  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
3297  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
3298  "p0", "p1", "p2", "p3",
3299  "sa0", "lc0", "sa1", "lc1", "m0", "m1", "usr", "ugp"
3300};
3301
3302void HexagonTargetInfo::getGCCRegNames(const char * const *&Names,
3303                                   unsigned &NumNames) const {
3304  Names = GCCRegNames;
3305  NumNames = llvm::array_lengthof(GCCRegNames);
3306}
3307
3308
3309const TargetInfo::GCCRegAlias HexagonTargetInfo::GCCRegAliases[] = {
3310  { { "sp" }, "r29" },
3311  { { "fp" }, "r30" },
3312  { { "lr" }, "r31" },
3313 };
3314
3315void HexagonTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3316                                     unsigned &NumAliases) const {
3317  Aliases = GCCRegAliases;
3318  NumAliases = llvm::array_lengthof(GCCRegAliases);
3319}
3320
3321
3322const Builtin::Info HexagonTargetInfo::BuiltinInfo[] = {
3323#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3324#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3325                                              ALL_LANGUAGES },
3326#include "clang/Basic/BuiltinsHexagon.def"
3327};
3328}
3329
3330
3331namespace {
3332class SparcV8TargetInfo : public TargetInfo {
3333  static const TargetInfo::GCCRegAlias GCCRegAliases[];
3334  static const char * const GCCRegNames[];
3335  bool SoftFloat;
3336public:
3337  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
3338    // FIXME: Support Sparc quad-precision long double?
3339    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3340                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
3341  }
3342  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
3343                                 StringRef Name,
3344                                 bool Enabled) const {
3345    if (Name == "soft-float")
3346      Features[Name] = Enabled;
3347    else
3348      return false;
3349
3350    return true;
3351  }
3352  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
3353    SoftFloat = false;
3354    for (unsigned i = 0, e = Features.size(); i != e; ++i)
3355      if (Features[i] == "+soft-float")
3356        SoftFloat = true;
3357  }
3358  virtual void getTargetDefines(const LangOptions &Opts,
3359                                MacroBuilder &Builder) const {
3360    DefineStd(Builder, "sparc", Opts);
3361    Builder.defineMacro("__sparcv8");
3362    Builder.defineMacro("__REGISTER_PREFIX__", "");
3363
3364    if (SoftFloat)
3365      Builder.defineMacro("SOFT_FLOAT", "1");
3366  }
3367
3368  virtual bool hasFeature(StringRef Feature) const {
3369    return llvm::StringSwitch<bool>(Feature)
3370             .Case("softfloat", SoftFloat)
3371             .Case("sparc", true)
3372             .Default(false);
3373  }
3374
3375  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3376                                 unsigned &NumRecords) const {
3377    // FIXME: Implement!
3378  }
3379  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3380    return TargetInfo::VoidPtrBuiltinVaList;
3381  }
3382  virtual void getGCCRegNames(const char * const *&Names,
3383                              unsigned &NumNames) const;
3384  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3385                                unsigned &NumAliases) const;
3386  virtual bool validateAsmConstraint(const char *&Name,
3387                                     TargetInfo::ConstraintInfo &info) const {
3388    // FIXME: Implement!
3389    return false;
3390  }
3391  virtual const char *getClobbers() const {
3392    // FIXME: Implement!
3393    return "";
3394  }
3395};
3396
3397const char * const SparcV8TargetInfo::GCCRegNames[] = {
3398  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3399  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
3400  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
3401  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
3402};
3403
3404void SparcV8TargetInfo::getGCCRegNames(const char * const *&Names,
3405                                       unsigned &NumNames) const {
3406  Names = GCCRegNames;
3407  NumNames = llvm::array_lengthof(GCCRegNames);
3408}
3409
3410const TargetInfo::GCCRegAlias SparcV8TargetInfo::GCCRegAliases[] = {
3411  { { "g0" }, "r0" },
3412  { { "g1" }, "r1" },
3413  { { "g2" }, "r2" },
3414  { { "g3" }, "r3" },
3415  { { "g4" }, "r4" },
3416  { { "g5" }, "r5" },
3417  { { "g6" }, "r6" },
3418  { { "g7" }, "r7" },
3419  { { "o0" }, "r8" },
3420  { { "o1" }, "r9" },
3421  { { "o2" }, "r10" },
3422  { { "o3" }, "r11" },
3423  { { "o4" }, "r12" },
3424  { { "o5" }, "r13" },
3425  { { "o6", "sp" }, "r14" },
3426  { { "o7" }, "r15" },
3427  { { "l0" }, "r16" },
3428  { { "l1" }, "r17" },
3429  { { "l2" }, "r18" },
3430  { { "l3" }, "r19" },
3431  { { "l4" }, "r20" },
3432  { { "l5" }, "r21" },
3433  { { "l6" }, "r22" },
3434  { { "l7" }, "r23" },
3435  { { "i0" }, "r24" },
3436  { { "i1" }, "r25" },
3437  { { "i2" }, "r26" },
3438  { { "i3" }, "r27" },
3439  { { "i4" }, "r28" },
3440  { { "i5" }, "r29" },
3441  { { "i6", "fp" }, "r30" },
3442  { { "i7" }, "r31" },
3443};
3444
3445void SparcV8TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3446                                         unsigned &NumAliases) const {
3447  Aliases = GCCRegAliases;
3448  NumAliases = llvm::array_lengthof(GCCRegAliases);
3449}
3450} // end anonymous namespace.
3451
3452namespace {
3453class AuroraUXSparcV8TargetInfo : public AuroraUXTargetInfo<SparcV8TargetInfo> {
3454public:
3455  AuroraUXSparcV8TargetInfo(const std::string& triple) :
3456      AuroraUXTargetInfo<SparcV8TargetInfo>(triple) {
3457    SizeType = UnsignedInt;
3458    PtrDiffType = SignedInt;
3459  }
3460};
3461class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
3462public:
3463  SolarisSparcV8TargetInfo(const std::string& triple) :
3464      SolarisTargetInfo<SparcV8TargetInfo>(triple) {
3465    SizeType = UnsignedInt;
3466    PtrDiffType = SignedInt;
3467  }
3468};
3469} // end anonymous namespace.
3470
3471namespace {
3472  class MSP430TargetInfo : public TargetInfo {
3473    static const char * const GCCRegNames[];
3474  public:
3475    MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
3476      BigEndian = false;
3477      TLSSupported = false;
3478      IntWidth = 16; IntAlign = 16;
3479      LongWidth = 32; LongLongWidth = 64;
3480      LongAlign = LongLongAlign = 16;
3481      PointerWidth = 16; PointerAlign = 16;
3482      SuitableAlign = 16;
3483      SizeType = UnsignedInt;
3484      IntMaxType = SignedLong;
3485      UIntMaxType = UnsignedLong;
3486      IntPtrType = SignedShort;
3487      PtrDiffType = SignedInt;
3488      SigAtomicType = SignedLong;
3489      DescriptionString = "e-p:16:16:16-i8:8:8-i16:16:16-i32:16:32-n8:16";
3490   }
3491    virtual void getTargetDefines(const LangOptions &Opts,
3492                                  MacroBuilder &Builder) const {
3493      Builder.defineMacro("MSP430");
3494      Builder.defineMacro("__MSP430__");
3495      // FIXME: defines for different 'flavours' of MCU
3496    }
3497    virtual void getTargetBuiltins(const Builtin::Info *&Records,
3498                                   unsigned &NumRecords) const {
3499     // FIXME: Implement.
3500      Records = 0;
3501      NumRecords = 0;
3502    }
3503    virtual bool hasFeature(StringRef Feature) const {
3504      return Feature == "msp430";
3505    }
3506    virtual void getGCCRegNames(const char * const *&Names,
3507                                unsigned &NumNames) const;
3508    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3509                                  unsigned &NumAliases) const {
3510      // No aliases.
3511      Aliases = 0;
3512      NumAliases = 0;
3513    }
3514    virtual bool validateAsmConstraint(const char *&Name,
3515                                       TargetInfo::ConstraintInfo &info) const {
3516      // No target constraints for now.
3517      return false;
3518    }
3519    virtual const char *getClobbers() const {
3520      // FIXME: Is this really right?
3521      return "";
3522    }
3523    virtual BuiltinVaListKind getBuiltinVaListKind() const {
3524      // FIXME: implement
3525      return TargetInfo::CharPtrBuiltinVaList;
3526   }
3527  };
3528
3529  const char * const MSP430TargetInfo::GCCRegNames[] = {
3530    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3531    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3532  };
3533
3534  void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
3535                                        unsigned &NumNames) const {
3536    Names = GCCRegNames;
3537    NumNames = llvm::array_lengthof(GCCRegNames);
3538  }
3539}
3540
3541namespace {
3542
3543  // LLVM and Clang cannot be used directly to output native binaries for
3544  // target, but is used to compile C code to llvm bitcode with correct
3545  // type and alignment information.
3546  //
3547  // TCE uses the llvm bitcode as input and uses it for generating customized
3548  // target processor and program binary. TCE co-design environment is
3549  // publicly available in http://tce.cs.tut.fi
3550
3551  static const unsigned TCEOpenCLAddrSpaceMap[] = {
3552      3, // opencl_global
3553      4, // opencl_local
3554      5, // opencl_constant
3555      0, // cuda_device
3556      0, // cuda_constant
3557      0  // cuda_shared
3558  };
3559
3560  class TCETargetInfo : public TargetInfo{
3561  public:
3562    TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
3563      TLSSupported = false;
3564      IntWidth = 32;
3565      LongWidth = LongLongWidth = 32;
3566      PointerWidth = 32;
3567      IntAlign = 32;
3568      LongAlign = LongLongAlign = 32;
3569      PointerAlign = 32;
3570      SuitableAlign = 32;
3571      SizeType = UnsignedInt;
3572      IntMaxType = SignedLong;
3573      UIntMaxType = UnsignedLong;
3574      IntPtrType = SignedInt;
3575      PtrDiffType = SignedInt;
3576      FloatWidth = 32;
3577      FloatAlign = 32;
3578      DoubleWidth = 32;
3579      DoubleAlign = 32;
3580      LongDoubleWidth = 32;
3581      LongDoubleAlign = 32;
3582      FloatFormat = &llvm::APFloat::IEEEsingle;
3583      DoubleFormat = &llvm::APFloat::IEEEsingle;
3584      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
3585      DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-"
3586                          "i16:16:32-i32:32:32-i64:32:32-"
3587                          "f32:32:32-f64:32:32-v64:32:32-"
3588                          "v128:32:32-a0:0:32-n32";
3589      AddrSpaceMap = &TCEOpenCLAddrSpaceMap;
3590    }
3591
3592    virtual void getTargetDefines(const LangOptions &Opts,
3593                                  MacroBuilder &Builder) const {
3594      DefineStd(Builder, "tce", Opts);
3595      Builder.defineMacro("__TCE__");
3596      Builder.defineMacro("__TCE_V1__");
3597    }
3598    virtual bool hasFeature(StringRef Feature) const {
3599      return Feature == "tce";
3600    }
3601
3602    virtual void getTargetBuiltins(const Builtin::Info *&Records,
3603                                   unsigned &NumRecords) const {}
3604    virtual const char *getClobbers() const {
3605      return "";
3606    }
3607    virtual BuiltinVaListKind getBuiltinVaListKind() const {
3608      return TargetInfo::VoidPtrBuiltinVaList;
3609    }
3610    virtual void getGCCRegNames(const char * const *&Names,
3611                                unsigned &NumNames) const {}
3612    virtual bool validateAsmConstraint(const char *&Name,
3613                                       TargetInfo::ConstraintInfo &info) const {
3614      return true;
3615    }
3616    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3617                                  unsigned &NumAliases) const {}
3618  };
3619}
3620
3621namespace {
3622class MipsTargetInfoBase : public TargetInfo {
3623  static const Builtin::Info BuiltinInfo[];
3624  std::string CPU;
3625  bool IsMips16;
3626  enum MipsFloatABI {
3627    HardFloat, SingleFloat, SoftFloat
3628  } FloatABI;
3629  enum DspRevEnum {
3630    NoDSP, DSP1, DSP2
3631  } DspRev;
3632
3633protected:
3634  std::string ABI;
3635
3636public:
3637  MipsTargetInfoBase(const std::string& triple,
3638                     const std::string& ABIStr,
3639                     const std::string& CPUStr)
3640    : TargetInfo(triple),
3641      CPU(CPUStr),
3642      IsMips16(false),
3643      FloatABI(HardFloat),
3644      DspRev(NoDSP),
3645      ABI(ABIStr)
3646  {}
3647
3648  virtual const char *getABI() const { return ABI.c_str(); }
3649  virtual bool setABI(const std::string &Name) = 0;
3650  virtual bool setCPU(const std::string &Name) {
3651    CPU = Name;
3652    return true;
3653  }
3654  void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
3655    Features[ABI] = true;
3656    Features[CPU] = true;
3657  }
3658
3659  virtual void getArchDefines(const LangOptions &Opts,
3660                              MacroBuilder &Builder) const {
3661    switch (FloatABI) {
3662    case HardFloat:
3663      Builder.defineMacro("__mips_hard_float", Twine(1));
3664      break;
3665    case SingleFloat:
3666      Builder.defineMacro("__mips_hard_float", Twine(1));
3667      Builder.defineMacro("__mips_single_float", Twine(1));
3668      break;
3669    case SoftFloat:
3670      Builder.defineMacro("__mips_soft_float", Twine(1));
3671      break;
3672    }
3673
3674    if (IsMips16)
3675      Builder.defineMacro("__mips16", Twine(1));
3676
3677    switch (DspRev) {
3678    default:
3679      break;
3680    case DSP1:
3681      Builder.defineMacro("__mips_dsp_rev", Twine(1));
3682      Builder.defineMacro("__mips_dsp", Twine(1));
3683      break;
3684    case DSP2:
3685      Builder.defineMacro("__mips_dsp_rev", Twine(2));
3686      Builder.defineMacro("__mips_dspr2", Twine(1));
3687      Builder.defineMacro("__mips_dsp", Twine(1));
3688      break;
3689    }
3690
3691    Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(0)));
3692    Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
3693    Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
3694  }
3695
3696  virtual void getTargetDefines(const LangOptions &Opts,
3697                                MacroBuilder &Builder) const = 0;
3698  virtual void getTargetBuiltins(const Builtin::Info *&Records,
3699                                 unsigned &NumRecords) const {
3700    Records = BuiltinInfo;
3701    NumRecords = clang::Mips::LastTSBuiltin - Builtin::FirstTSBuiltin;
3702  }
3703  virtual bool hasFeature(StringRef Feature) const {
3704    return Feature == "mips";
3705  }
3706  virtual BuiltinVaListKind getBuiltinVaListKind() const {
3707    return TargetInfo::VoidPtrBuiltinVaList;
3708  }
3709  virtual void getGCCRegNames(const char * const *&Names,
3710                              unsigned &NumNames) const {
3711    static const char * const GCCRegNames[] = {
3712      // CPU register names
3713      // Must match second column of GCCRegAliases
3714      "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
3715      "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
3716      "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
3717      "$24",  "$25",  "$26",  "$27",  "$28",  "$29",  "$30",  "$31",
3718      // Floating point register names
3719      "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
3720      "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
3721      "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
3722      "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
3723      // Hi/lo and condition register names
3724      "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
3725      "$fcc5","$fcc6","$fcc7"
3726    };
3727    Names = GCCRegNames;
3728    NumNames = llvm::array_lengthof(GCCRegNames);
3729  }
3730  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3731                                unsigned &NumAliases) const = 0;
3732  virtual bool validateAsmConstraint(const char *&Name,
3733                                     TargetInfo::ConstraintInfo &Info) const {
3734    switch (*Name) {
3735    default:
3736      return false;
3737
3738    case 'r': // CPU registers.
3739    case 'd': // Equivalent to "r" unless generating MIPS16 code.
3740    case 'y': // Equivalent to "r", backwards compatibility only.
3741    case 'f': // floating-point registers.
3742    case 'c': // $25 for indirect jumps
3743    case 'l': // lo register
3744    case 'x': // hilo register pair
3745      Info.setAllowsRegister();
3746      return true;
3747    }
3748  }
3749
3750  virtual const char *getClobbers() const {
3751    // FIXME: Implement!
3752    return "";
3753  }
3754
3755  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
3756                                 StringRef Name,
3757                                 bool Enabled) const {
3758    if (Name == "soft-float" || Name == "single-float" ||
3759        Name == "o32" || Name == "n32" || Name == "n64" || Name == "eabi" ||
3760        Name == "mips32" || Name == "mips32r2" ||
3761        Name == "mips64" || Name == "mips64r2" ||
3762        Name == "mips16" || Name == "dsp" || Name == "dspr2") {
3763      Features[Name] = Enabled;
3764      return true;
3765    }
3766    return false;
3767  }
3768
3769  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
3770    IsMips16 = false;
3771    FloatABI = HardFloat;
3772    DspRev = NoDSP;
3773
3774    for (std::vector<std::string>::iterator it = Features.begin(),
3775         ie = Features.end(); it != ie; ++it) {
3776      if (*it == "+single-float")
3777        FloatABI = SingleFloat;
3778      else if (*it == "+soft-float")
3779        FloatABI = SoftFloat;
3780      else if (*it == "+mips16")
3781        IsMips16 = true;
3782      else if (*it == "+dsp")
3783        DspRev = std::max(DspRev, DSP1);
3784      else if (*it == "+dspr2")
3785        DspRev = std::max(DspRev, DSP2);
3786    }
3787
3788    // Remove front-end specific option.
3789    std::vector<std::string>::iterator it =
3790      std::find(Features.begin(), Features.end(), "+soft-float");
3791    if (it != Features.end())
3792      Features.erase(it);
3793  }
3794};
3795
3796const Builtin::Info MipsTargetInfoBase::BuiltinInfo[] = {
3797#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3798#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3799                                              ALL_LANGUAGES },
3800#include "clang/Basic/BuiltinsMips.def"
3801};
3802
3803class Mips32TargetInfoBase : public MipsTargetInfoBase {
3804public:
3805  Mips32TargetInfoBase(const std::string& triple) :
3806    MipsTargetInfoBase(triple, "o32", "mips32") {
3807    SizeType = UnsignedInt;
3808    PtrDiffType = SignedInt;
3809  }
3810  virtual bool setABI(const std::string &Name) {
3811    if ((Name == "o32") || (Name == "eabi")) {
3812      ABI = Name;
3813      return true;
3814    } else
3815      return false;
3816  }
3817  virtual void getArchDefines(const LangOptions &Opts,
3818                              MacroBuilder &Builder) const {
3819    MipsTargetInfoBase::getArchDefines(Opts, Builder);
3820
3821    if (ABI == "o32") {
3822      Builder.defineMacro("__mips_o32");
3823      Builder.defineMacro("_ABIO32", "1");
3824      Builder.defineMacro("_MIPS_SIM", "_ABIO32");
3825    }
3826    else if (ABI == "eabi")
3827      Builder.defineMacro("__mips_eabi");
3828    else
3829      llvm_unreachable("Invalid ABI for Mips32.");
3830  }
3831  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3832                                unsigned &NumAliases) const {
3833    static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
3834      { { "at" },  "$1" },
3835      { { "v0" },  "$2" },
3836      { { "v1" },  "$3" },
3837      { { "a0" },  "$4" },
3838      { { "a1" },  "$5" },
3839      { { "a2" },  "$6" },
3840      { { "a3" },  "$7" },
3841      { { "t0" },  "$8" },
3842      { { "t1" },  "$9" },
3843      { { "t2" }, "$10" },
3844      { { "t3" }, "$11" },
3845      { { "t4" }, "$12" },
3846      { { "t5" }, "$13" },
3847      { { "t6" }, "$14" },
3848      { { "t7" }, "$15" },
3849      { { "s0" }, "$16" },
3850      { { "s1" }, "$17" },
3851      { { "s2" }, "$18" },
3852      { { "s3" }, "$19" },
3853      { { "s4" }, "$20" },
3854      { { "s5" }, "$21" },
3855      { { "s6" }, "$22" },
3856      { { "s7" }, "$23" },
3857      { { "t8" }, "$24" },
3858      { { "t9" }, "$25" },
3859      { { "k0" }, "$26" },
3860      { { "k1" }, "$27" },
3861      { { "gp" }, "$28" },
3862      { { "sp","$sp" }, "$29" },
3863      { { "fp","$fp" }, "$30" },
3864      { { "ra" }, "$31" }
3865    };
3866    Aliases = GCCRegAliases;
3867    NumAliases = llvm::array_lengthof(GCCRegAliases);
3868  }
3869};
3870
3871class Mips32EBTargetInfo : public Mips32TargetInfoBase {
3872public:
3873  Mips32EBTargetInfo(const std::string& triple) : Mips32TargetInfoBase(triple) {
3874    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3875                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
3876  }
3877  virtual void getTargetDefines(const LangOptions &Opts,
3878                                MacroBuilder &Builder) const {
3879    DefineStd(Builder, "mips", Opts);
3880    Builder.defineMacro("_mips");
3881    DefineStd(Builder, "MIPSEB", Opts);
3882    Builder.defineMacro("_MIPSEB");
3883    Builder.defineMacro("__REGISTER_PREFIX__", "");
3884    getArchDefines(Opts, Builder);
3885  }
3886};
3887
3888class Mips32ELTargetInfo : public Mips32TargetInfoBase {
3889public:
3890  Mips32ELTargetInfo(const std::string& triple) : Mips32TargetInfoBase(triple) {
3891    BigEndian = false;
3892    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3893                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
3894  }
3895  virtual void getTargetDefines(const LangOptions &Opts,
3896                                MacroBuilder &Builder) const {
3897    DefineStd(Builder, "mips", Opts);
3898    Builder.defineMacro("_mips");
3899    DefineStd(Builder, "MIPSEL", Opts);
3900    Builder.defineMacro("_MIPSEL");
3901    Builder.defineMacro("__REGISTER_PREFIX__", "");
3902    getArchDefines(Opts, Builder);
3903  }
3904};
3905
3906class Mips64TargetInfoBase : public MipsTargetInfoBase {
3907  virtual void SetDescriptionString(const std::string &Name) = 0;
3908public:
3909  Mips64TargetInfoBase(const std::string& triple) :
3910    MipsTargetInfoBase(triple, "n64", "mips64") {
3911    LongWidth = LongAlign = 64;
3912    PointerWidth = PointerAlign = 64;
3913    LongDoubleWidth = LongDoubleAlign = 128;
3914    LongDoubleFormat = &llvm::APFloat::IEEEquad;
3915    SuitableAlign = 128;
3916  }
3917  virtual bool setABI(const std::string &Name) {
3918    SetDescriptionString(Name);
3919
3920    if (Name != "n32" && Name != "n64")
3921      return false;
3922
3923    ABI = Name;
3924
3925    if (Name == "n32") {
3926      LongWidth = LongAlign = 32;
3927      PointerWidth = PointerAlign = 32;
3928    }
3929
3930    return true;
3931  }
3932  virtual void getArchDefines(const LangOptions &Opts,
3933                              MacroBuilder &Builder) const {
3934    MipsTargetInfoBase::getArchDefines(Opts, Builder);
3935
3936    if (ABI == "n32") {
3937      Builder.defineMacro("__mips_n32");
3938      Builder.defineMacro("_ABIN32", "2");
3939      Builder.defineMacro("_MIPS_SIM", "_ABIN32");
3940    }
3941    else if (ABI == "n64") {
3942      Builder.defineMacro("__mips_n64");
3943      Builder.defineMacro("_ABI64", "3");
3944      Builder.defineMacro("_MIPS_SIM", "_ABI64");
3945    }
3946    else
3947      llvm_unreachable("Invalid ABI for Mips64.");
3948  }
3949  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3950                                unsigned &NumAliases) const {
3951    static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
3952      { { "at" },  "$1" },
3953      { { "v0" },  "$2" },
3954      { { "v1" },  "$3" },
3955      { { "a0" },  "$4" },
3956      { { "a1" },  "$5" },
3957      { { "a2" },  "$6" },
3958      { { "a3" },  "$7" },
3959      { { "a4" },  "$8" },
3960      { { "a5" },  "$9" },
3961      { { "a6" }, "$10" },
3962      { { "a7" }, "$11" },
3963      { { "t0" }, "$12" },
3964      { { "t1" }, "$13" },
3965      { { "t2" }, "$14" },
3966      { { "t3" }, "$15" },
3967      { { "s0" }, "$16" },
3968      { { "s1" }, "$17" },
3969      { { "s2" }, "$18" },
3970      { { "s3" }, "$19" },
3971      { { "s4" }, "$20" },
3972      { { "s5" }, "$21" },
3973      { { "s6" }, "$22" },
3974      { { "s7" }, "$23" },
3975      { { "t8" }, "$24" },
3976      { { "t9" }, "$25" },
3977      { { "k0" }, "$26" },
3978      { { "k1" }, "$27" },
3979      { { "gp" }, "$28" },
3980      { { "sp","$sp" }, "$29" },
3981      { { "fp","$fp" }, "$30" },
3982      { { "ra" }, "$31" }
3983    };
3984    Aliases = GCCRegAliases;
3985    NumAliases = llvm::array_lengthof(GCCRegAliases);
3986  }
3987};
3988
3989class Mips64EBTargetInfo : public Mips64TargetInfoBase {
3990  virtual void SetDescriptionString(const std::string &Name) {
3991    // Change DescriptionString only if ABI is n32.
3992    if (Name == "n32")
3993      DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
3994                          "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
3995                          "v64:64:64-n32";
3996  }
3997public:
3998  Mips64EBTargetInfo(const std::string& triple) : Mips64TargetInfoBase(triple) {
3999    // Default ABI is n64.
4000    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
4001                        "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
4002                        "v64:64:64-n32";
4003  }
4004  virtual void getTargetDefines(const LangOptions &Opts,
4005                                MacroBuilder &Builder) const {
4006    DefineStd(Builder, "mips", Opts);
4007    Builder.defineMacro("_mips");
4008    DefineStd(Builder, "MIPSEB", Opts);
4009    Builder.defineMacro("_MIPSEB");
4010    Builder.defineMacro("__REGISTER_PREFIX__", "");
4011    getArchDefines(Opts, Builder);
4012  }
4013};
4014
4015class Mips64ELTargetInfo : public Mips64TargetInfoBase {
4016  virtual void SetDescriptionString(const std::string &Name) {
4017    // Change DescriptionString only if ABI is n32.
4018    if (Name == "n32")
4019      DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
4020                          "i64:64:64-f32:32:32-f64:64:64-f128:128:128"
4021                          "-v64:64:64-n32";
4022  }
4023public:
4024  Mips64ELTargetInfo(const std::string& triple) : Mips64TargetInfoBase(triple) {
4025    // Default ABI is n64.
4026    BigEndian = false;
4027    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
4028                        "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
4029                        "v64:64:64-n32";
4030  }
4031  virtual void getTargetDefines(const LangOptions &Opts,
4032                                MacroBuilder &Builder) const {
4033    DefineStd(Builder, "mips", Opts);
4034    Builder.defineMacro("_mips");
4035    DefineStd(Builder, "MIPSEL", Opts);
4036    Builder.defineMacro("_MIPSEL");
4037    Builder.defineMacro("__REGISTER_PREFIX__", "");
4038    getArchDefines(Opts, Builder);
4039  }
4040};
4041} // end anonymous namespace.
4042
4043namespace {
4044class PNaClTargetInfo : public TargetInfo {
4045public:
4046  PNaClTargetInfo(const std::string& triple) : TargetInfo(triple) {
4047    BigEndian = false;
4048    this->UserLabelPrefix = "";
4049    this->LongAlign = 32;
4050    this->LongWidth = 32;
4051    this->PointerAlign = 32;
4052    this->PointerWidth = 32;
4053    this->IntMaxType = TargetInfo::SignedLongLong;
4054    this->UIntMaxType = TargetInfo::UnsignedLongLong;
4055    this->Int64Type = TargetInfo::SignedLongLong;
4056    this->DoubleAlign = 64;
4057    this->LongDoubleWidth = 64;
4058    this->LongDoubleAlign = 64;
4059    this->SizeType = TargetInfo::UnsignedInt;
4060    this->PtrDiffType = TargetInfo::SignedInt;
4061    this->IntPtrType = TargetInfo::SignedInt;
4062    this->RegParmMax = 2;
4063    DescriptionString = "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
4064                        "f32:32:32-f64:64:64-p:32:32:32-v128:32:32";
4065  }
4066
4067  void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
4068  }
4069  virtual void getArchDefines(const LangOptions &Opts,
4070                              MacroBuilder &Builder) const {
4071    Builder.defineMacro("__le32__");
4072    Builder.defineMacro("__pnacl__");
4073  }
4074  virtual void getTargetDefines(const LangOptions &Opts,
4075                                MacroBuilder &Builder) const {
4076    DefineStd(Builder, "unix", Opts);
4077    Builder.defineMacro("__ELF__");
4078    if (Opts.POSIXThreads)
4079      Builder.defineMacro("_REENTRANT");
4080    if (Opts.CPlusPlus)
4081      Builder.defineMacro("_GNU_SOURCE");
4082
4083    Builder.defineMacro("__LITTLE_ENDIAN__");
4084    Builder.defineMacro("__native_client__");
4085    getArchDefines(Opts, Builder);
4086  }
4087  virtual bool hasFeature(StringRef Feature) const {
4088    return Feature == "pnacl";
4089  }
4090  virtual void getTargetBuiltins(const Builtin::Info *&Records,
4091                                 unsigned &NumRecords) const {
4092  }
4093  virtual BuiltinVaListKind getBuiltinVaListKind() const {
4094    return TargetInfo::PNaClABIBuiltinVaList;
4095  }
4096  virtual void getGCCRegNames(const char * const *&Names,
4097                              unsigned &NumNames) const;
4098  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4099                                unsigned &NumAliases) const;
4100  virtual bool validateAsmConstraint(const char *&Name,
4101                                     TargetInfo::ConstraintInfo &Info) const {
4102    return false;
4103  }
4104
4105  virtual const char *getClobbers() const {
4106    return "";
4107  }
4108};
4109
4110void PNaClTargetInfo::getGCCRegNames(const char * const *&Names,
4111                                     unsigned &NumNames) const {
4112  Names = NULL;
4113  NumNames = 0;
4114}
4115
4116void PNaClTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
4117                                       unsigned &NumAliases) const {
4118  Aliases = NULL;
4119  NumAliases = 0;
4120}
4121} // end anonymous namespace.
4122
4123
4124//===----------------------------------------------------------------------===//
4125// Driver code
4126//===----------------------------------------------------------------------===//
4127
4128static TargetInfo *AllocateTarget(const std::string &T) {
4129  llvm::Triple Triple(T);
4130  llvm::Triple::OSType os = Triple.getOS();
4131
4132  switch (Triple.getArch()) {
4133  default:
4134    return NULL;
4135
4136  case llvm::Triple::hexagon:
4137    return new HexagonTargetInfo(T);
4138
4139  case llvm::Triple::arm:
4140  case llvm::Triple::thumb:
4141    if (Triple.isOSDarwin())
4142      return new DarwinARMTargetInfo(T);
4143
4144    switch (os) {
4145    case llvm::Triple::Linux:
4146      return new LinuxTargetInfo<ARMTargetInfo>(T);
4147    case llvm::Triple::FreeBSD:
4148      return new FreeBSDTargetInfo<ARMTargetInfo>(T);
4149    case llvm::Triple::NetBSD:
4150      return new NetBSDTargetInfo<ARMTargetInfo>(T);
4151    case llvm::Triple::RTEMS:
4152      return new RTEMSTargetInfo<ARMTargetInfo>(T);
4153    default:
4154      return new ARMTargetInfo(T);
4155    }
4156
4157  case llvm::Triple::msp430:
4158    return new MSP430TargetInfo(T);
4159
4160  case llvm::Triple::mips:
4161    switch (os) {
4162    case llvm::Triple::Linux:
4163      return new LinuxTargetInfo<Mips32EBTargetInfo>(T);
4164    case llvm::Triple::RTEMS:
4165      return new RTEMSTargetInfo<Mips32EBTargetInfo>(T);
4166    case llvm::Triple::FreeBSD:
4167      return new FreeBSDTargetInfo<Mips32EBTargetInfo>(T);
4168    case llvm::Triple::NetBSD:
4169      return new NetBSDTargetInfo<Mips32EBTargetInfo>(T);
4170    default:
4171      return new Mips32EBTargetInfo(T);
4172    }
4173
4174  case llvm::Triple::mipsel:
4175    switch (os) {
4176    case llvm::Triple::Linux:
4177      return new LinuxTargetInfo<Mips32ELTargetInfo>(T);
4178    case llvm::Triple::RTEMS:
4179      return new RTEMSTargetInfo<Mips32ELTargetInfo>(T);
4180    case llvm::Triple::FreeBSD:
4181      return new FreeBSDTargetInfo<Mips32ELTargetInfo>(T);
4182    case llvm::Triple::NetBSD:
4183      return new NetBSDTargetInfo<Mips32ELTargetInfo>(T);
4184    default:
4185      return new Mips32ELTargetInfo(T);
4186    }
4187
4188  case llvm::Triple::mips64:
4189    switch (os) {
4190    case llvm::Triple::Linux:
4191      return new LinuxTargetInfo<Mips64EBTargetInfo>(T);
4192    case llvm::Triple::RTEMS:
4193      return new RTEMSTargetInfo<Mips64EBTargetInfo>(T);
4194    case llvm::Triple::FreeBSD:
4195      return new FreeBSDTargetInfo<Mips64EBTargetInfo>(T);
4196    case llvm::Triple::NetBSD:
4197      return new NetBSDTargetInfo<Mips64EBTargetInfo>(T);
4198    default:
4199      return new Mips64EBTargetInfo(T);
4200    }
4201
4202  case llvm::Triple::mips64el:
4203    switch (os) {
4204    case llvm::Triple::Linux:
4205      return new LinuxTargetInfo<Mips64ELTargetInfo>(T);
4206    case llvm::Triple::RTEMS:
4207      return new RTEMSTargetInfo<Mips64ELTargetInfo>(T);
4208    case llvm::Triple::FreeBSD:
4209      return new FreeBSDTargetInfo<Mips64ELTargetInfo>(T);
4210    case llvm::Triple::NetBSD:
4211      return new NetBSDTargetInfo<Mips64ELTargetInfo>(T);
4212    default:
4213      return new Mips64ELTargetInfo(T);
4214    }
4215
4216  case llvm::Triple::le32:
4217    switch (os) {
4218      case llvm::Triple::NativeClient:
4219        return new PNaClTargetInfo(T);
4220      default:
4221        return NULL;
4222    }
4223
4224  case llvm::Triple::ppc:
4225    if (Triple.isOSDarwin())
4226      return new DarwinPPC32TargetInfo(T);
4227    switch (os) {
4228    case llvm::Triple::Linux:
4229      return new LinuxTargetInfo<PPC32TargetInfo>(T);
4230    case llvm::Triple::FreeBSD:
4231      return new FreeBSDTargetInfo<PPC32TargetInfo>(T);
4232    case llvm::Triple::NetBSD:
4233      return new NetBSDTargetInfo<PPC32TargetInfo>(T);
4234    case llvm::Triple::RTEMS:
4235      return new RTEMSTargetInfo<PPC32TargetInfo>(T);
4236    default:
4237      return new PPC32TargetInfo(T);
4238    }
4239
4240  case llvm::Triple::ppc64:
4241    if (Triple.isOSDarwin())
4242      return new DarwinPPC64TargetInfo(T);
4243    switch (os) {
4244    case llvm::Triple::Linux:
4245      return new LinuxTargetInfo<PPC64TargetInfo>(T);
4246    case llvm::Triple::Lv2:
4247      return new PS3PPUTargetInfo<PPC64TargetInfo>(T);
4248    case llvm::Triple::FreeBSD:
4249      return new FreeBSDTargetInfo<PPC64TargetInfo>(T);
4250    case llvm::Triple::NetBSD:
4251      return new NetBSDTargetInfo<PPC64TargetInfo>(T);
4252    default:
4253      return new PPC64TargetInfo(T);
4254    }
4255
4256  case llvm::Triple::nvptx:
4257    return new NVPTX32TargetInfo(T);
4258  case llvm::Triple::nvptx64:
4259    return new NVPTX64TargetInfo(T);
4260
4261  case llvm::Triple::mblaze:
4262    return new MBlazeTargetInfo(T);
4263
4264  case llvm::Triple::sparc:
4265    switch (os) {
4266    case llvm::Triple::Linux:
4267      return new LinuxTargetInfo<SparcV8TargetInfo>(T);
4268    case llvm::Triple::AuroraUX:
4269      return new AuroraUXSparcV8TargetInfo(T);
4270    case llvm::Triple::Solaris:
4271      return new SolarisSparcV8TargetInfo(T);
4272    case llvm::Triple::NetBSD:
4273      return new NetBSDTargetInfo<SparcV8TargetInfo>(T);
4274    case llvm::Triple::RTEMS:
4275      return new RTEMSTargetInfo<SparcV8TargetInfo>(T);
4276    default:
4277      return new SparcV8TargetInfo(T);
4278    }
4279
4280  // FIXME: Need a real SPU target.
4281  case llvm::Triple::cellspu:
4282    return new PS3SPUTargetInfo<PPC64TargetInfo>(T);
4283
4284  case llvm::Triple::tce:
4285    return new TCETargetInfo(T);
4286
4287  case llvm::Triple::x86:
4288    if (Triple.isOSDarwin())
4289      return new DarwinI386TargetInfo(T);
4290
4291    switch (os) {
4292    case llvm::Triple::AuroraUX:
4293      return new AuroraUXTargetInfo<X86_32TargetInfo>(T);
4294    case llvm::Triple::Linux:
4295      return new LinuxTargetInfo<X86_32TargetInfo>(T);
4296    case llvm::Triple::DragonFly:
4297      return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
4298    case llvm::Triple::NetBSD:
4299      return new NetBSDI386TargetInfo(T);
4300    case llvm::Triple::OpenBSD:
4301      return new OpenBSDI386TargetInfo(T);
4302    case llvm::Triple::FreeBSD:
4303      return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
4304    case llvm::Triple::Minix:
4305      return new MinixTargetInfo<X86_32TargetInfo>(T);
4306    case llvm::Triple::Solaris:
4307      return new SolarisTargetInfo<X86_32TargetInfo>(T);
4308    case llvm::Triple::Cygwin:
4309      return new CygwinX86_32TargetInfo(T);
4310    case llvm::Triple::MinGW32:
4311      return new MinGWX86_32TargetInfo(T);
4312    case llvm::Triple::Win32:
4313      return new VisualStudioWindowsX86_32TargetInfo(T);
4314    case llvm::Triple::Haiku:
4315      return new HaikuX86_32TargetInfo(T);
4316    case llvm::Triple::RTEMS:
4317      return new RTEMSX86_32TargetInfo(T);
4318    default:
4319      return new X86_32TargetInfo(T);
4320    }
4321
4322  case llvm::Triple::x86_64:
4323    if (Triple.isOSDarwin() || Triple.getEnvironment() == llvm::Triple::MachO)
4324      return new DarwinX86_64TargetInfo(T);
4325
4326    switch (os) {
4327    case llvm::Triple::AuroraUX:
4328      return new AuroraUXTargetInfo<X86_64TargetInfo>(T);
4329    case llvm::Triple::Linux:
4330      return new LinuxTargetInfo<X86_64TargetInfo>(T);
4331    case llvm::Triple::DragonFly:
4332      return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(T);
4333    case llvm::Triple::NetBSD:
4334      return new NetBSDTargetInfo<X86_64TargetInfo>(T);
4335    case llvm::Triple::OpenBSD:
4336      return new OpenBSDX86_64TargetInfo(T);
4337    case llvm::Triple::FreeBSD:
4338      return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
4339    case llvm::Triple::Solaris:
4340      return new SolarisTargetInfo<X86_64TargetInfo>(T);
4341    case llvm::Triple::MinGW32:
4342      return new MinGWX86_64TargetInfo(T);
4343    case llvm::Triple::Win32:   // This is what Triple.h supports now.
4344      return new VisualStudioWindowsX86_64TargetInfo(T);
4345    default:
4346      return new X86_64TargetInfo(T);
4347    }
4348  }
4349}
4350
4351/// CreateTargetInfo - Return the target info object for the specified target
4352/// triple.
4353TargetInfo *TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
4354                                         TargetOptions &Opts) {
4355  llvm::Triple Triple(Opts.Triple);
4356
4357  // Construct the target
4358  OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
4359  if (!Target) {
4360    Diags.Report(diag::err_target_unknown_triple) << Triple.str();
4361    return 0;
4362  }
4363
4364  // Set the target CPU if specified.
4365  if (!Opts.CPU.empty() && !Target->setCPU(Opts.CPU)) {
4366    Diags.Report(diag::err_target_unknown_cpu) << Opts.CPU;
4367    return 0;
4368  }
4369
4370  // Set the target ABI if specified.
4371  if (!Opts.ABI.empty() && !Target->setABI(Opts.ABI)) {
4372    Diags.Report(diag::err_target_unknown_abi) << Opts.ABI;
4373    return 0;
4374  }
4375
4376  // Set the target C++ ABI.
4377  if (!Opts.CXXABI.empty() && !Target->setCXXABI(Opts.CXXABI)) {
4378    Diags.Report(diag::err_target_unknown_cxxabi) << Opts.CXXABI;
4379    return 0;
4380  }
4381
4382  // Compute the default target features, we need the target to handle this
4383  // because features may have dependencies on one another.
4384  llvm::StringMap<bool> Features;
4385  Target->getDefaultFeatures(Features);
4386
4387  // Apply the user specified deltas.
4388  // First the enables.
4389  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
4390         ie = Opts.Features.end(); it != ie; ++it) {
4391    const char *Name = it->c_str();
4392
4393    if (Name[0] != '+')
4394      continue;
4395
4396    // Apply the feature via the target.
4397    if (!Target->setFeatureEnabled(Features, Name + 1, true)) {
4398      Diags.Report(diag::err_target_invalid_feature) << Name;
4399      return 0;
4400    }
4401  }
4402
4403  // Then the disables.
4404  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
4405         ie = Opts.Features.end(); it != ie; ++it) {
4406    const char *Name = it->c_str();
4407
4408    if (Name[0] == '+')
4409      continue;
4410
4411    // Apply the feature via the target.
4412    if (Name[0] != '-' ||
4413        !Target->setFeatureEnabled(Features, Name + 1, false)) {
4414      Diags.Report(diag::err_target_invalid_feature) << Name;
4415      return 0;
4416    }
4417  }
4418
4419  // Add the features to the compile options.
4420  //
4421  // FIXME: If we are completely confident that we have the right set, we only
4422  // need to pass the minuses.
4423  Opts.Features.clear();
4424  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
4425         ie = Features.end(); it != ie; ++it)
4426    Opts.Features.push_back((it->second ? "+" : "-") + it->first().str());
4427  Target->HandleTargetFeatures(Opts.Features);
4428
4429  return Target.take();
4430}
4431