clang API Documentation

Targets.cpp
Go to the documentation of this file.
00001 //===--- Targets.cpp - Implement -arch option and targets -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements construction of a TargetInfo object from a
00011 // target triple.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Basic/TargetInfo.h"
00016 #include "clang/Basic/Builtins.h"
00017 #include "clang/Basic/Diagnostic.h"
00018 #include "clang/Basic/LangOptions.h"
00019 #include "clang/Basic/MacroBuilder.h"
00020 #include "clang/Basic/TargetBuiltins.h"
00021 #include "clang/Basic/TargetOptions.h"
00022 #include "llvm/ADT/APFloat.h"
00023 #include "llvm/ADT/STLExtras.h"
00024 #include "llvm/ADT/StringExtras.h"
00025 #include "llvm/ADT/StringRef.h"
00026 #include "llvm/ADT/StringSwitch.h"
00027 #include "llvm/ADT/Triple.h"
00028 #include "llvm/IR/Type.h"
00029 #include "llvm/MC/MCSectionMachO.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 #include <algorithm>
00032 #include <memory>
00033 using namespace clang;
00034 
00035 //===----------------------------------------------------------------------===//
00036 //  Common code shared among targets.
00037 //===----------------------------------------------------------------------===//
00038 
00039 /// DefineStd - Define a macro name and standard variants.  For example if
00040 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
00041 /// when in GNU mode.
00042 static void DefineStd(MacroBuilder &Builder, StringRef MacroName,
00043                       const LangOptions &Opts) {
00044   assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
00045 
00046   // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
00047   // in the user's namespace.
00048   if (Opts.GNUMode)
00049     Builder.defineMacro(MacroName);
00050 
00051   // Define __unix.
00052   Builder.defineMacro("__" + MacroName);
00053 
00054   // Define __unix__.
00055   Builder.defineMacro("__" + MacroName + "__");
00056 }
00057 
00058 static void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName,
00059                             bool Tuning = true) {
00060   Builder.defineMacro("__" + CPUName);
00061   Builder.defineMacro("__" + CPUName + "__");
00062   if (Tuning)
00063     Builder.defineMacro("__tune_" + CPUName + "__");
00064 }
00065 
00066 //===----------------------------------------------------------------------===//
00067 // Defines specific to certain operating systems.
00068 //===----------------------------------------------------------------------===//
00069 
00070 namespace {
00071 template<typename TgtInfo>
00072 class OSTargetInfo : public TgtInfo {
00073 protected:
00074   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00075                             MacroBuilder &Builder) const=0;
00076 public:
00077   OSTargetInfo(const llvm::Triple &Triple) : TgtInfo(Triple) {}
00078   void getTargetDefines(const LangOptions &Opts,
00079                         MacroBuilder &Builder) const override {
00080     TgtInfo::getTargetDefines(Opts, Builder);
00081     getOSDefines(Opts, TgtInfo::getTriple(), Builder);
00082   }
00083 
00084 };
00085 } // end anonymous namespace
00086 
00087 
00088 static void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
00089                              const llvm::Triple &Triple,
00090                              StringRef &PlatformName,
00091                              VersionTuple &PlatformMinVersion) {
00092   Builder.defineMacro("__APPLE_CC__", "6000");
00093   Builder.defineMacro("__APPLE__");
00094   Builder.defineMacro("OBJC_NEW_PROPERTIES");
00095   // AddressSanitizer doesn't play well with source fortification, which is on
00096   // by default on Darwin.
00097   if (Opts.Sanitize.has(SanitizerKind::Address))
00098     Builder.defineMacro("_FORTIFY_SOURCE", "0");
00099 
00100   if (!Opts.ObjCAutoRefCount) {
00101     // __weak is always defined, for use in blocks and with objc pointers.
00102     Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
00103 
00104     // Darwin defines __strong even in C mode (just to nothing).
00105     if (Opts.getGC() != LangOptions::NonGC)
00106       Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
00107     else
00108       Builder.defineMacro("__strong", "");
00109 
00110     // __unsafe_unretained is defined to nothing in non-ARC mode. We even
00111     // allow this in C, since one might have block pointers in structs that
00112     // are used in pure C code and in Objective-C ARC.
00113     Builder.defineMacro("__unsafe_unretained", "");
00114   }
00115 
00116   if (Opts.Static)
00117     Builder.defineMacro("__STATIC__");
00118   else
00119     Builder.defineMacro("__DYNAMIC__");
00120 
00121   if (Opts.POSIXThreads)
00122     Builder.defineMacro("_REENTRANT");
00123 
00124   // Get the platform type and version number from the triple.
00125   unsigned Maj, Min, Rev;
00126   if (Triple.isMacOSX()) {
00127     Triple.getMacOSXVersion(Maj, Min, Rev);
00128     PlatformName = "macosx";
00129   } else {
00130     Triple.getOSVersion(Maj, Min, Rev);
00131     PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
00132   }
00133 
00134   // If -target arch-pc-win32-macho option specified, we're
00135   // generating code for Win32 ABI. No need to emit
00136   // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
00137   if (PlatformName == "win32") {
00138     PlatformMinVersion = VersionTuple(Maj, Min, Rev);
00139     return;
00140   }
00141 
00142   // Set the appropriate OS version define.
00143   if (Triple.isiOS()) {
00144     assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
00145     char Str[6];
00146     Str[0] = '0' + Maj;
00147     Str[1] = '0' + (Min / 10);
00148     Str[2] = '0' + (Min % 10);
00149     Str[3] = '0' + (Rev / 10);
00150     Str[4] = '0' + (Rev % 10);
00151     Str[5] = '\0';
00152     Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
00153                         Str);
00154   } else if (Triple.isMacOSX()) {
00155     // Note that the Driver allows versions which aren't representable in the
00156     // define (because we only get a single digit for the minor and micro
00157     // revision numbers). So, we limit them to the maximum representable
00158     // version.
00159     assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
00160     char Str[7];
00161     if (Maj < 10 || (Maj == 10 && Min < 10)) {
00162       Str[0] = '0' + (Maj / 10);
00163       Str[1] = '0' + (Maj % 10);
00164       Str[2] = '0' + std::min(Min, 9U);
00165       Str[3] = '0' + std::min(Rev, 9U);
00166       Str[4] = '\0';
00167     } else {
00168       // Handle versions > 10.9.
00169       Str[0] = '0' + (Maj / 10);
00170       Str[1] = '0' + (Maj % 10);
00171       Str[2] = '0' + (Min / 10);
00172       Str[3] = '0' + (Min % 10);
00173       Str[4] = '0' + (Rev / 10);
00174       Str[5] = '0' + (Rev % 10);
00175       Str[6] = '\0';
00176     }
00177     Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
00178   }
00179 
00180   // Tell users about the kernel if there is one.
00181   if (Triple.isOSDarwin())
00182     Builder.defineMacro("__MACH__");
00183 
00184   PlatformMinVersion = VersionTuple(Maj, Min, Rev);
00185 }
00186 
00187 namespace {
00188 template<typename Target>
00189 class DarwinTargetInfo : public OSTargetInfo<Target> {
00190 protected:
00191   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00192                     MacroBuilder &Builder) const override {
00193     getDarwinDefines(Builder, Opts, Triple, this->PlatformName,
00194                      this->PlatformMinVersion);
00195   }
00196 
00197 public:
00198   DarwinTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00199     this->TLSSupported = Triple.isMacOSX() && !Triple.isMacOSXVersionLT(10, 7);
00200     this->MCountName = "\01mcount";
00201   }
00202 
00203   std::string isValidSectionSpecifier(StringRef SR) const override {
00204     // Let MCSectionMachO validate this.
00205     StringRef Segment, Section;
00206     unsigned TAA, StubSize;
00207     bool HasTAA;
00208     return llvm::MCSectionMachO::ParseSectionSpecifier(SR, Segment, Section,
00209                                                        TAA, HasTAA, StubSize);
00210   }
00211 
00212   const char *getStaticInitSectionSpecifier() const override {
00213     // FIXME: We should return 0 when building kexts.
00214     return "__TEXT,__StaticInit,regular,pure_instructions";
00215   }
00216 
00217   /// Darwin does not support protected visibility.  Darwin's "default"
00218   /// is very similar to ELF's "protected";  Darwin requires a "weak"
00219   /// attribute on declarations that can be dynamically replaced.
00220   bool hasProtectedVisibility() const override {
00221     return false;
00222   }
00223 };
00224 
00225 
00226 // DragonFlyBSD Target
00227 template<typename Target>
00228 class DragonFlyBSDTargetInfo : public OSTargetInfo<Target> {
00229 protected:
00230   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00231                     MacroBuilder &Builder) const override {
00232     // DragonFly defines; list based off of gcc output
00233     Builder.defineMacro("__DragonFly__");
00234     Builder.defineMacro("__DragonFly_cc_version", "100001");
00235     Builder.defineMacro("__ELF__");
00236     Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
00237     Builder.defineMacro("__tune_i386__");
00238     DefineStd(Builder, "unix", Opts);
00239   }
00240 public:
00241   DragonFlyBSDTargetInfo(const llvm::Triple &Triple)
00242       : OSTargetInfo<Target>(Triple) {
00243     this->UserLabelPrefix = "";
00244 
00245     switch (Triple.getArch()) {
00246     default:
00247     case llvm::Triple::x86:
00248     case llvm::Triple::x86_64:
00249       this->MCountName = ".mcount";
00250       break;
00251     }
00252   }
00253 };
00254 
00255 // FreeBSD Target
00256 template<typename Target>
00257 class FreeBSDTargetInfo : public OSTargetInfo<Target> {
00258 protected:
00259   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00260                     MacroBuilder &Builder) const override {
00261     // FreeBSD defines; list based off of gcc output
00262 
00263     unsigned Release = Triple.getOSMajorVersion();
00264     if (Release == 0U)
00265       Release = 8;
00266 
00267     Builder.defineMacro("__FreeBSD__", Twine(Release));
00268     Builder.defineMacro("__FreeBSD_cc_version", Twine(Release * 100000U + 1U));
00269     Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
00270     DefineStd(Builder, "unix", Opts);
00271     Builder.defineMacro("__ELF__");
00272 
00273     // On FreeBSD, wchar_t contains the number of the code point as
00274     // used by the character set of the locale. These character sets are
00275     // not necessarily a superset of ASCII.
00276     Builder.defineMacro("__STDC_MB_MIGHT_NEQ_WC__", "1");
00277   }
00278 public:
00279   FreeBSDTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00280     this->UserLabelPrefix = "";
00281 
00282     switch (Triple.getArch()) {
00283     default:
00284     case llvm::Triple::x86:
00285     case llvm::Triple::x86_64:
00286       this->MCountName = ".mcount";
00287       break;
00288     case llvm::Triple::mips:
00289     case llvm::Triple::mipsel:
00290     case llvm::Triple::ppc:
00291     case llvm::Triple::ppc64:
00292     case llvm::Triple::ppc64le:
00293       this->MCountName = "_mcount";
00294       break;
00295     case llvm::Triple::arm:
00296       this->MCountName = "__mcount";
00297       break;
00298     }
00299   }
00300 };
00301 
00302 // GNU/kFreeBSD Target
00303 template<typename Target>
00304 class KFreeBSDTargetInfo : public OSTargetInfo<Target> {
00305 protected:
00306   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00307                     MacroBuilder &Builder) const override {
00308     // GNU/kFreeBSD defines; list based off of gcc output
00309 
00310     DefineStd(Builder, "unix", Opts);
00311     Builder.defineMacro("__FreeBSD_kernel__");
00312     Builder.defineMacro("__GLIBC__");
00313     Builder.defineMacro("__ELF__");
00314     if (Opts.POSIXThreads)
00315       Builder.defineMacro("_REENTRANT");
00316     if (Opts.CPlusPlus)
00317       Builder.defineMacro("_GNU_SOURCE");
00318   }
00319 public:
00320   KFreeBSDTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00321     this->UserLabelPrefix = "";
00322   }
00323 };
00324 
00325 // Minix Target
00326 template<typename Target>
00327 class MinixTargetInfo : public OSTargetInfo<Target> {
00328 protected:
00329   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00330                     MacroBuilder &Builder) const override {
00331     // Minix defines
00332 
00333     Builder.defineMacro("__minix", "3");
00334     Builder.defineMacro("_EM_WSIZE", "4");
00335     Builder.defineMacro("_EM_PSIZE", "4");
00336     Builder.defineMacro("_EM_SSIZE", "2");
00337     Builder.defineMacro("_EM_LSIZE", "4");
00338     Builder.defineMacro("_EM_FSIZE", "4");
00339     Builder.defineMacro("_EM_DSIZE", "8");
00340     Builder.defineMacro("__ELF__");
00341     DefineStd(Builder, "unix", Opts);
00342   }
00343 public:
00344   MinixTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00345     this->UserLabelPrefix = "";
00346   }
00347 };
00348 
00349 // Linux target
00350 template<typename Target>
00351 class LinuxTargetInfo : public OSTargetInfo<Target> {
00352 protected:
00353   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00354                     MacroBuilder &Builder) const override {
00355     // Linux defines; list based off of gcc output
00356     DefineStd(Builder, "unix", Opts);
00357     DefineStd(Builder, "linux", Opts);
00358     Builder.defineMacro("__gnu_linux__");
00359     Builder.defineMacro("__ELF__");
00360     if (Triple.getEnvironment() == llvm::Triple::Android)
00361       Builder.defineMacro("__ANDROID__", "1");
00362     if (Opts.POSIXThreads)
00363       Builder.defineMacro("_REENTRANT");
00364     if (Opts.CPlusPlus)
00365       Builder.defineMacro("_GNU_SOURCE");
00366   }
00367 public:
00368   LinuxTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00369     this->UserLabelPrefix = "";
00370     this->WIntType = TargetInfo::UnsignedInt;
00371 
00372     switch (Triple.getArch()) {
00373     default:
00374       break;
00375     case llvm::Triple::ppc:
00376     case llvm::Triple::ppc64:
00377     case llvm::Triple::ppc64le:
00378       this->MCountName = "_mcount";
00379       break;
00380     }
00381   }
00382 
00383   const char *getStaticInitSectionSpecifier() const override {
00384     return ".text.startup";
00385   }
00386 };
00387 
00388 // NetBSD Target
00389 template<typename Target>
00390 class NetBSDTargetInfo : public OSTargetInfo<Target> {
00391 protected:
00392   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00393                     MacroBuilder &Builder) const override {
00394     // NetBSD defines; list based off of gcc output
00395     Builder.defineMacro("__NetBSD__");
00396     Builder.defineMacro("__unix__");
00397     Builder.defineMacro("__ELF__");
00398     if (Opts.POSIXThreads)
00399       Builder.defineMacro("_POSIX_THREADS");
00400 
00401     switch (Triple.getArch()) {
00402     default:
00403       break;
00404     case llvm::Triple::arm:
00405     case llvm::Triple::armeb:
00406     case llvm::Triple::thumb:
00407     case llvm::Triple::thumbeb:
00408       Builder.defineMacro("__ARM_DWARF_EH__");
00409       break;
00410     }
00411   }
00412 public:
00413   NetBSDTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00414     this->UserLabelPrefix = "";
00415   }
00416 };
00417 
00418 // OpenBSD Target
00419 template<typename Target>
00420 class OpenBSDTargetInfo : public OSTargetInfo<Target> {
00421 protected:
00422   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00423                     MacroBuilder &Builder) const override {
00424     // OpenBSD defines; list based off of gcc output
00425 
00426     Builder.defineMacro("__OpenBSD__");
00427     DefineStd(Builder, "unix", Opts);
00428     Builder.defineMacro("__ELF__");
00429     if (Opts.POSIXThreads)
00430       Builder.defineMacro("_REENTRANT");
00431   }
00432 public:
00433   OpenBSDTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00434     this->UserLabelPrefix = "";
00435     this->TLSSupported = false;
00436 
00437       switch (Triple.getArch()) {
00438         default:
00439         case llvm::Triple::x86:
00440         case llvm::Triple::x86_64:
00441         case llvm::Triple::arm:
00442         case llvm::Triple::sparc:
00443           this->MCountName = "__mcount";
00444           break;
00445         case llvm::Triple::mips64:
00446         case llvm::Triple::mips64el:
00447         case llvm::Triple::ppc:
00448         case llvm::Triple::sparcv9:
00449           this->MCountName = "_mcount";
00450           break;
00451       }
00452   }
00453 };
00454 
00455 // Bitrig Target
00456 template<typename Target>
00457 class BitrigTargetInfo : public OSTargetInfo<Target> {
00458 protected:
00459   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00460                     MacroBuilder &Builder) const override {
00461     // Bitrig defines; list based off of gcc output
00462 
00463     Builder.defineMacro("__Bitrig__");
00464     DefineStd(Builder, "unix", Opts);
00465     Builder.defineMacro("__ELF__");
00466     if (Opts.POSIXThreads)
00467       Builder.defineMacro("_REENTRANT");
00468   }
00469 public:
00470   BitrigTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00471     this->UserLabelPrefix = "";
00472     this->MCountName = "__mcount";
00473   }
00474 };
00475 
00476 // PSP Target
00477 template<typename Target>
00478 class PSPTargetInfo : public OSTargetInfo<Target> {
00479 protected:
00480   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00481                     MacroBuilder &Builder) const override {
00482     // PSP defines; list based on the output of the pspdev gcc toolchain.
00483     Builder.defineMacro("PSP");
00484     Builder.defineMacro("_PSP");
00485     Builder.defineMacro("__psp__");
00486     Builder.defineMacro("__ELF__");
00487   }
00488 public:
00489   PSPTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00490     this->UserLabelPrefix = "";
00491   }
00492 };
00493 
00494 // PS3 PPU Target
00495 template<typename Target>
00496 class PS3PPUTargetInfo : public OSTargetInfo<Target> {
00497 protected:
00498   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00499                     MacroBuilder &Builder) const override {
00500     // PS3 PPU defines.
00501     Builder.defineMacro("__PPC__");
00502     Builder.defineMacro("__PPU__");
00503     Builder.defineMacro("__CELLOS_LV2__");
00504     Builder.defineMacro("__ELF__");
00505     Builder.defineMacro("__LP32__");
00506     Builder.defineMacro("_ARCH_PPC64");
00507     Builder.defineMacro("__powerpc64__");
00508   }
00509 public:
00510   PS3PPUTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00511     this->UserLabelPrefix = "";
00512     this->LongWidth = this->LongAlign = 32;
00513     this->PointerWidth = this->PointerAlign = 32;
00514     this->IntMaxType = TargetInfo::SignedLongLong;
00515     this->Int64Type = TargetInfo::SignedLongLong;
00516     this->SizeType = TargetInfo::UnsignedInt;
00517     this->DescriptionString = "E-m:e-p:32:32-i64:64-n32:64";
00518   }
00519 };
00520 
00521 // Solaris target
00522 template<typename Target>
00523 class SolarisTargetInfo : public OSTargetInfo<Target> {
00524 protected:
00525   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00526                     MacroBuilder &Builder) const override {
00527     DefineStd(Builder, "sun", Opts);
00528     DefineStd(Builder, "unix", Opts);
00529     Builder.defineMacro("__ELF__");
00530     Builder.defineMacro("__svr4__");
00531     Builder.defineMacro("__SVR4");
00532     // Solaris headers require _XOPEN_SOURCE to be set to 600 for C99 and
00533     // newer, but to 500 for everything else.  feature_test.h has a check to
00534     // ensure that you are not using C99 with an old version of X/Open or C89
00535     // with a new version.
00536     if (Opts.C99)
00537       Builder.defineMacro("_XOPEN_SOURCE", "600");
00538     else
00539       Builder.defineMacro("_XOPEN_SOURCE", "500");
00540     if (Opts.CPlusPlus)
00541       Builder.defineMacro("__C99FEATURES__");
00542     Builder.defineMacro("_LARGEFILE_SOURCE");
00543     Builder.defineMacro("_LARGEFILE64_SOURCE");
00544     Builder.defineMacro("__EXTENSIONS__");
00545     Builder.defineMacro("_REENTRANT");
00546   }
00547 public:
00548   SolarisTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00549     this->UserLabelPrefix = "";
00550     this->WCharType = this->SignedInt;
00551     // FIXME: WIntType should be SignedLong
00552   }
00553 };
00554 
00555 // Windows target
00556 template<typename Target>
00557 class WindowsTargetInfo : public OSTargetInfo<Target> {
00558 protected:
00559   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00560                     MacroBuilder &Builder) const override {
00561     Builder.defineMacro("_WIN32");
00562   }
00563   void getVisualStudioDefines(const LangOptions &Opts,
00564                               MacroBuilder &Builder) const {
00565     if (Opts.CPlusPlus) {
00566       if (Opts.RTTIData)
00567         Builder.defineMacro("_CPPRTTI");
00568 
00569       if (Opts.Exceptions)
00570         Builder.defineMacro("_CPPUNWIND");
00571     }
00572 
00573     if (!Opts.CharIsSigned)
00574       Builder.defineMacro("_CHAR_UNSIGNED");
00575 
00576     // FIXME: POSIXThreads isn't exactly the option this should be defined for,
00577     //        but it works for now.
00578     if (Opts.POSIXThreads)
00579       Builder.defineMacro("_MT");
00580 
00581     if (Opts.MSCompatibilityVersion) {
00582       Builder.defineMacro("_MSC_VER",
00583                           Twine(Opts.MSCompatibilityVersion / 100000));
00584       Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
00585       // FIXME We cannot encode the revision information into 32-bits
00586       Builder.defineMacro("_MSC_BUILD", Twine(1));
00587     }
00588 
00589     if (Opts.MicrosoftExt) {
00590       Builder.defineMacro("_MSC_EXTENSIONS");
00591 
00592       if (Opts.CPlusPlus11) {
00593         Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
00594         Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
00595         Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
00596       }
00597     }
00598 
00599     Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
00600   }
00601 
00602 public:
00603   WindowsTargetInfo(const llvm::Triple &Triple)
00604       : OSTargetInfo<Target>(Triple) {}
00605 };
00606 
00607 template <typename Target>
00608 class NaClTargetInfo : public OSTargetInfo<Target> {
00609 protected:
00610   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
00611                     MacroBuilder &Builder) const override {
00612     if (Opts.POSIXThreads)
00613       Builder.defineMacro("_REENTRANT");
00614     if (Opts.CPlusPlus)
00615       Builder.defineMacro("_GNU_SOURCE");
00616 
00617     DefineStd(Builder, "unix", Opts);
00618     Builder.defineMacro("__ELF__");
00619     Builder.defineMacro("__native_client__");
00620   }
00621 
00622 public:
00623   NaClTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
00624     this->UserLabelPrefix = "";
00625     this->LongAlign = 32;
00626     this->LongWidth = 32;
00627     this->PointerAlign = 32;
00628     this->PointerWidth = 32;
00629     this->IntMaxType = TargetInfo::SignedLongLong;
00630     this->Int64Type = TargetInfo::SignedLongLong;
00631     this->DoubleAlign = 64;
00632     this->LongDoubleWidth = 64;
00633     this->LongDoubleAlign = 64;
00634     this->LongLongWidth = 64;
00635     this->LongLongAlign = 64;
00636     this->SizeType = TargetInfo::UnsignedInt;
00637     this->PtrDiffType = TargetInfo::SignedInt;
00638     this->IntPtrType = TargetInfo::SignedInt;
00639     // RegParmMax is inherited from the underlying architecture
00640     this->LongDoubleFormat = &llvm::APFloat::IEEEdouble;
00641     if (Triple.getArch() == llvm::Triple::arm) {
00642       this->DescriptionString = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S128";
00643     } else if (Triple.getArch() == llvm::Triple::x86) {
00644       this->DescriptionString = "e-m:e-p:32:32-i64:64-n8:16:32-S128";
00645     } else if (Triple.getArch() == llvm::Triple::x86_64) {
00646       this->DescriptionString = "e-m:e-p:32:32-i64:64-n8:16:32:64-S128";
00647     } else if (Triple.getArch() == llvm::Triple::mipsel) {
00648       // Handled on mips' setDescriptionString.
00649     } else {
00650       assert(Triple.getArch() == llvm::Triple::le32);
00651       this->DescriptionString = "e-p:32:32-i64:64";
00652     }
00653   }
00654   typename Target::CallingConvCheckResult checkCallingConvention(
00655       CallingConv CC) const override {
00656     return CC == CC_PnaclCall ? Target::CCCR_OK :
00657         Target::checkCallingConvention(CC);
00658   }
00659 };
00660 } // end anonymous namespace.
00661 
00662 //===----------------------------------------------------------------------===//
00663 // Specific target implementations.
00664 //===----------------------------------------------------------------------===//
00665 
00666 namespace {
00667 // PPC abstract base class
00668 class PPCTargetInfo : public TargetInfo {
00669   static const Builtin::Info BuiltinInfo[];
00670   static const char * const GCCRegNames[];
00671   static const TargetInfo::GCCRegAlias GCCRegAliases[];
00672   std::string CPU;
00673 
00674   // Target cpu features.
00675   bool HasVSX;
00676   bool HasP8Vector;
00677 
00678 protected:
00679   std::string ABI;
00680 
00681 public:
00682   PPCTargetInfo(const llvm::Triple &Triple)
00683     : TargetInfo(Triple), HasVSX(false), HasP8Vector(false) {
00684     BigEndian = (Triple.getArch() != llvm::Triple::ppc64le);
00685     LongDoubleWidth = LongDoubleAlign = 128;
00686     LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble;
00687   }
00688 
00689   /// \brief Flags for architecture specific defines.
00690   typedef enum {
00691     ArchDefineNone  = 0,
00692     ArchDefineName  = 1 << 0, // <name> is substituted for arch name.
00693     ArchDefinePpcgr = 1 << 1,
00694     ArchDefinePpcsq = 1 << 2,
00695     ArchDefine440   = 1 << 3,
00696     ArchDefine603   = 1 << 4,
00697     ArchDefine604   = 1 << 5,
00698     ArchDefinePwr4  = 1 << 6,
00699     ArchDefinePwr5  = 1 << 7,
00700     ArchDefinePwr5x = 1 << 8,
00701     ArchDefinePwr6  = 1 << 9,
00702     ArchDefinePwr6x = 1 << 10,
00703     ArchDefinePwr7  = 1 << 11,
00704     ArchDefinePwr8  = 1 << 12,
00705     ArchDefineA2    = 1 << 13,
00706     ArchDefineA2q   = 1 << 14
00707   } ArchDefineTypes;
00708 
00709   // Note: GCC recognizes the following additional cpus:
00710   //  401, 403, 405, 405fp, 440fp, 464, 464fp, 476, 476fp, 505, 740, 801,
00711   //  821, 823, 8540, 8548, e300c2, e300c3, e500mc64, e6500, 860, cell,
00712   //  titan, rs64.
00713   bool setCPU(const std::string &Name) override {
00714     bool CPUKnown = llvm::StringSwitch<bool>(Name)
00715       .Case("generic", true)
00716       .Case("440", true)
00717       .Case("450", true)
00718       .Case("601", true)
00719       .Case("602", true)
00720       .Case("603", true)
00721       .Case("603e", true)
00722       .Case("603ev", true)
00723       .Case("604", true)
00724       .Case("604e", true)
00725       .Case("620", true)
00726       .Case("630", true)
00727       .Case("g3", true)
00728       .Case("7400", true)
00729       .Case("g4", true)
00730       .Case("7450", true)
00731       .Case("g4+", true)
00732       .Case("750", true)
00733       .Case("970", true)
00734       .Case("g5", true)
00735       .Case("a2", true)
00736       .Case("a2q", true)
00737       .Case("e500mc", true)
00738       .Case("e5500", true)
00739       .Case("power3", true)
00740       .Case("pwr3", true)
00741       .Case("power4", true)
00742       .Case("pwr4", true)
00743       .Case("power5", true)
00744       .Case("pwr5", true)
00745       .Case("power5x", true)
00746       .Case("pwr5x", true)
00747       .Case("power6", true)
00748       .Case("pwr6", true)
00749       .Case("power6x", true)
00750       .Case("pwr6x", true)
00751       .Case("power7", true)
00752       .Case("pwr7", true)
00753       .Case("power8", true)
00754       .Case("pwr8", true)
00755       .Case("powerpc", true)
00756       .Case("ppc", true)
00757       .Case("powerpc64", true)
00758       .Case("ppc64", true)
00759       .Case("powerpc64le", true)
00760       .Case("ppc64le", true)
00761       .Default(false);
00762 
00763     if (CPUKnown)
00764       CPU = Name;
00765 
00766     return CPUKnown;
00767   }
00768 
00769 
00770   StringRef getABI() const override { return ABI; }
00771 
00772   void getTargetBuiltins(const Builtin::Info *&Records,
00773                          unsigned &NumRecords) const override {
00774     Records = BuiltinInfo;
00775     NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
00776   }
00777 
00778   bool isCLZForZeroUndef() const override { return false; }
00779 
00780   void getTargetDefines(const LangOptions &Opts,
00781                         MacroBuilder &Builder) const override;
00782 
00783   void getDefaultFeatures(llvm::StringMap<bool> &Features) const override;
00784 
00785   bool handleTargetFeatures(std::vector<std::string> &Features,
00786                             DiagnosticsEngine &Diags) override;
00787   bool hasFeature(StringRef Feature) const override;
00788 
00789   void getGCCRegNames(const char * const *&Names,
00790                       unsigned &NumNames) const override;
00791   void getGCCRegAliases(const GCCRegAlias *&Aliases,
00792                         unsigned &NumAliases) const override;
00793   bool validateAsmConstraint(const char *&Name,
00794                              TargetInfo::ConstraintInfo &Info) const override {
00795     switch (*Name) {
00796     default: return false;
00797     case 'O': // Zero
00798       break;
00799     case 'b': // Base register
00800     case 'f': // Floating point register
00801       Info.setAllowsRegister();
00802       break;
00803     // FIXME: The following are added to allow parsing.
00804     // I just took a guess at what the actions should be.
00805     // Also, is more specific checking needed?  I.e. specific registers?
00806     case 'd': // Floating point register (containing 64-bit value)
00807     case 'v': // Altivec vector register
00808       Info.setAllowsRegister();
00809       break;
00810     case 'w':
00811       switch (Name[1]) {
00812         case 'd':// VSX vector register to hold vector double data
00813         case 'f':// VSX vector register to hold vector float data
00814         case 's':// VSX vector register to hold scalar float data
00815         case 'a':// Any VSX register
00816         case 'c':// An individual CR bit
00817           break;
00818         default:
00819           return false;
00820       }
00821       Info.setAllowsRegister();
00822       Name++; // Skip over 'w'.
00823       break;
00824     case 'h': // `MQ', `CTR', or `LINK' register
00825     case 'q': // `MQ' register
00826     case 'c': // `CTR' register
00827     case 'l': // `LINK' register
00828     case 'x': // `CR' register (condition register) number 0
00829     case 'y': // `CR' register (condition register)
00830     case 'z': // `XER[CA]' carry bit (part of the XER register)
00831       Info.setAllowsRegister();
00832       break;
00833     case 'I': // Signed 16-bit constant
00834     case 'J': // Unsigned 16-bit constant shifted left 16 bits
00835               //  (use `L' instead for SImode constants)
00836     case 'K': // Unsigned 16-bit constant
00837     case 'L': // Signed 16-bit constant shifted left 16 bits
00838     case 'M': // Constant larger than 31
00839     case 'N': // Exact power of 2
00840     case 'P': // Constant whose negation is a signed 16-bit constant
00841     case 'G': // Floating point constant that can be loaded into a
00842               // register with one instruction per word
00843     case 'H': // Integer/Floating point constant that can be loaded
00844               // into a register using three instructions
00845       break;
00846     case 'm': // Memory operand. Note that on PowerPC targets, m can
00847               // include addresses that update the base register. It
00848               // is therefore only safe to use `m' in an asm statement
00849               // if that asm statement accesses the operand exactly once.
00850               // The asm statement must also use `%U<opno>' as a
00851               // placeholder for the "update" flag in the corresponding
00852               // load or store instruction. For example:
00853               // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
00854               // is correct but:
00855               // asm ("st %1,%0" : "=m" (mem) : "r" (val));
00856               // is not. Use es rather than m if you don't want the base
00857               // register to be updated.
00858     case 'e':
00859       if (Name[1] != 's')
00860           return false;
00861               // es: A "stable" memory operand; that is, one which does not
00862               // include any automodification of the base register. Unlike
00863               // `m', this constraint can be used in asm statements that
00864               // might access the operand several times, or that might not
00865               // access it at all.
00866       Info.setAllowsMemory();
00867       Name++; // Skip over 'e'.
00868       break;
00869     case 'Q': // Memory operand that is an offset from a register (it is
00870               // usually better to use `m' or `es' in asm statements)
00871     case 'Z': // Memory operand that is an indexed or indirect from a
00872               // register (it is usually better to use `m' or `es' in
00873               // asm statements)
00874       Info.setAllowsMemory();
00875       Info.setAllowsRegister();
00876       break;
00877     case 'R': // AIX TOC entry
00878     case 'a': // Address operand that is an indexed or indirect from a
00879               // register (`p' is preferable for asm statements)
00880     case 'S': // Constant suitable as a 64-bit mask operand
00881     case 'T': // Constant suitable as a 32-bit mask operand
00882     case 'U': // System V Release 4 small data area reference
00883     case 't': // AND masks that can be performed by two rldic{l, r}
00884               // instructions
00885     case 'W': // Vector constant that does not require memory
00886     case 'j': // Vector constant that is all zeros.
00887       break;
00888     // End FIXME.
00889     }
00890     return true;
00891   }
00892   std::string convertConstraint(const char *&Constraint) const override {
00893     std::string R;
00894     switch (*Constraint) {
00895     case 'e':
00896     case 'w':
00897       // Two-character constraint; add "^" hint for later parsing.
00898       R = std::string("^") + std::string(Constraint, 2);
00899       Constraint++;
00900       break;
00901     default:
00902       return TargetInfo::convertConstraint(Constraint);
00903     }
00904     return R;
00905   }
00906   const char *getClobbers() const override {
00907     return "";
00908   }
00909   int getEHDataRegisterNumber(unsigned RegNo) const override {
00910     if (RegNo == 0) return 3;
00911     if (RegNo == 1) return 4;
00912     return -1;
00913   }
00914 };
00915 
00916 const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
00917 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
00918 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
00919                                               ALL_LANGUAGES },
00920 #include "clang/Basic/BuiltinsPPC.def"
00921 };
00922 
00923   /// handleTargetFeatures - Perform initialization based on the user
00924 /// configured set of features.
00925 bool PPCTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
00926                                          DiagnosticsEngine &Diags) {
00927   for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
00928     // Ignore disabled features.
00929     if (Features[i][0] == '-')
00930       continue;
00931 
00932     StringRef Feature = StringRef(Features[i]).substr(1);
00933 
00934     if (Feature == "vsx") {
00935       HasVSX = true;
00936       continue;
00937     }
00938 
00939     if (Feature == "power8-vector") {
00940       HasP8Vector = true;
00941       continue;
00942     }
00943 
00944     // TODO: Finish this list and add an assert that we've handled them
00945     // all.
00946   }
00947 
00948   return true;
00949 }
00950 
00951 /// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
00952 /// #defines that are not tied to a specific subtarget.
00953 void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
00954                                      MacroBuilder &Builder) const {
00955   // Target identification.
00956   Builder.defineMacro("__ppc__");
00957   Builder.defineMacro("__PPC__");
00958   Builder.defineMacro("_ARCH_PPC");
00959   Builder.defineMacro("__powerpc__");
00960   Builder.defineMacro("__POWERPC__");
00961   if (PointerWidth == 64) {
00962     Builder.defineMacro("_ARCH_PPC64");
00963     Builder.defineMacro("__powerpc64__");
00964     Builder.defineMacro("__ppc64__");
00965     Builder.defineMacro("__PPC64__");
00966   }
00967 
00968   // Target properties.
00969   if (getTriple().getArch() == llvm::Triple::ppc64le) {
00970     Builder.defineMacro("_LITTLE_ENDIAN");
00971   } else {
00972     if (getTriple().getOS() != llvm::Triple::NetBSD &&
00973         getTriple().getOS() != llvm::Triple::OpenBSD)
00974       Builder.defineMacro("_BIG_ENDIAN");
00975   }
00976 
00977   // ABI options.
00978   if (ABI == "elfv1")
00979     Builder.defineMacro("_CALL_ELF", "1");
00980   if (ABI == "elfv2")
00981     Builder.defineMacro("_CALL_ELF", "2");
00982 
00983   // Subtarget options.
00984   Builder.defineMacro("__NATURAL_ALIGNMENT__");
00985   Builder.defineMacro("__REGISTER_PREFIX__", "");
00986 
00987   // FIXME: Should be controlled by command line option.
00988   if (LongDoubleWidth == 128)
00989     Builder.defineMacro("__LONG_DOUBLE_128__");
00990 
00991   if (Opts.AltiVec) {
00992     Builder.defineMacro("__VEC__", "10206");
00993     Builder.defineMacro("__ALTIVEC__");
00994   }
00995 
00996   // CPU identification.
00997   ArchDefineTypes defs = (ArchDefineTypes)llvm::StringSwitch<int>(CPU)
00998     .Case("440",   ArchDefineName)
00999     .Case("450",   ArchDefineName | ArchDefine440)
01000     .Case("601",   ArchDefineName)
01001     .Case("602",   ArchDefineName | ArchDefinePpcgr)
01002     .Case("603",   ArchDefineName | ArchDefinePpcgr)
01003     .Case("603e",  ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
01004     .Case("603ev", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
01005     .Case("604",   ArchDefineName | ArchDefinePpcgr)
01006     .Case("604e",  ArchDefineName | ArchDefine604 | ArchDefinePpcgr)
01007     .Case("620",   ArchDefineName | ArchDefinePpcgr)
01008     .Case("630",   ArchDefineName | ArchDefinePpcgr)
01009     .Case("7400",  ArchDefineName | ArchDefinePpcgr)
01010     .Case("7450",  ArchDefineName | ArchDefinePpcgr)
01011     .Case("750",   ArchDefineName | ArchDefinePpcgr)
01012     .Case("970",   ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr
01013                      | ArchDefinePpcsq)
01014     .Case("a2",    ArchDefineA2)
01015     .Case("a2q",   ArchDefineName | ArchDefineA2 | ArchDefineA2q)
01016     .Case("pwr3",  ArchDefinePpcgr)
01017     .Case("pwr4",  ArchDefineName | ArchDefinePpcgr | ArchDefinePpcsq)
01018     .Case("pwr5",  ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr
01019                      | ArchDefinePpcsq)
01020     .Case("pwr5x", ArchDefineName | ArchDefinePwr5 | ArchDefinePwr4
01021                      | ArchDefinePpcgr | ArchDefinePpcsq)
01022     .Case("pwr6",  ArchDefineName | ArchDefinePwr5x | ArchDefinePwr5
01023                      | ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
01024     .Case("pwr6x", ArchDefineName | ArchDefinePwr6 | ArchDefinePwr5x
01025                      | ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr
01026                      | ArchDefinePpcsq)
01027     .Case("pwr7",  ArchDefineName | ArchDefinePwr6x | ArchDefinePwr6
01028                      | ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4
01029                      | ArchDefinePpcgr | ArchDefinePpcsq)
01030     .Case("pwr8",  ArchDefineName | ArchDefinePwr7 | ArchDefinePwr6x
01031                      | ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5
01032                      | ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
01033     .Case("power3",  ArchDefinePpcgr)
01034     .Case("power4",  ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
01035     .Case("power5",  ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr
01036                        | ArchDefinePpcsq)
01037     .Case("power5x", ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4
01038                        | ArchDefinePpcgr | ArchDefinePpcsq)
01039     .Case("power6",  ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5
01040                        | ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
01041     .Case("power6x", ArchDefinePwr6x | ArchDefinePwr6 | ArchDefinePwr5x
01042                        | ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr
01043                        | ArchDefinePpcsq)
01044     .Case("power7",  ArchDefinePwr7 | ArchDefinePwr6x | ArchDefinePwr6
01045                        | ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4
01046                        | ArchDefinePpcgr | ArchDefinePpcsq)
01047     .Case("power8",  ArchDefinePwr8 | ArchDefinePwr7 | ArchDefinePwr6x
01048                        | ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5
01049                        | ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
01050     .Default(ArchDefineNone);
01051 
01052   if (defs & ArchDefineName)
01053     Builder.defineMacro(Twine("_ARCH_", StringRef(CPU).upper()));
01054   if (defs & ArchDefinePpcgr)
01055     Builder.defineMacro("_ARCH_PPCGR");
01056   if (defs & ArchDefinePpcsq)
01057     Builder.defineMacro("_ARCH_PPCSQ");
01058   if (defs & ArchDefine440)
01059     Builder.defineMacro("_ARCH_440");
01060   if (defs & ArchDefine603)
01061     Builder.defineMacro("_ARCH_603");
01062   if (defs & ArchDefine604)
01063     Builder.defineMacro("_ARCH_604");
01064   if (defs & ArchDefinePwr4)
01065     Builder.defineMacro("_ARCH_PWR4");
01066   if (defs & ArchDefinePwr5)
01067     Builder.defineMacro("_ARCH_PWR5");
01068   if (defs & ArchDefinePwr5x)
01069     Builder.defineMacro("_ARCH_PWR5X");
01070   if (defs & ArchDefinePwr6)
01071     Builder.defineMacro("_ARCH_PWR6");
01072   if (defs & ArchDefinePwr6x)
01073     Builder.defineMacro("_ARCH_PWR6X");
01074   if (defs & ArchDefinePwr7)
01075     Builder.defineMacro("_ARCH_PWR7");
01076   if (defs & ArchDefinePwr8)
01077     Builder.defineMacro("_ARCH_PWR8");
01078   if (defs & ArchDefineA2)
01079     Builder.defineMacro("_ARCH_A2");
01080   if (defs & ArchDefineA2q) {
01081     Builder.defineMacro("_ARCH_A2Q");
01082     Builder.defineMacro("_ARCH_QP");
01083   }
01084 
01085   if (getTriple().getVendor() == llvm::Triple::BGQ) {
01086     Builder.defineMacro("__bg__");
01087     Builder.defineMacro("__THW_BLUEGENE__");
01088     Builder.defineMacro("__bgq__");
01089     Builder.defineMacro("__TOS_BGQ__");
01090   }
01091 
01092   if (HasVSX)
01093     Builder.defineMacro("__VSX__");
01094   if (HasP8Vector)
01095     Builder.defineMacro("__POWER8_VECTOR__");
01096 
01097   // FIXME: The following are not yet generated here by Clang, but are
01098   //        generated by GCC:
01099   //
01100   //   _SOFT_FLOAT_
01101   //   __RECIP_PRECISION__
01102   //   __APPLE_ALTIVEC__
01103   //   __RECIP__
01104   //   __RECIPF__
01105   //   __RSQRTE__
01106   //   __RSQRTEF__
01107   //   _SOFT_DOUBLE_
01108   //   __NO_LWSYNC__
01109   //   __HAVE_BSWAP__
01110   //   __LONGDOUBLE128
01111   //   __CMODEL_MEDIUM__
01112   //   __CMODEL_LARGE__
01113   //   _CALL_SYSV
01114   //   _CALL_DARWIN
01115   //   __NO_FPRS__
01116 }
01117 
01118 void PPCTargetInfo::getDefaultFeatures(llvm::StringMap<bool> &Features) const {
01119   Features["altivec"] = llvm::StringSwitch<bool>(CPU)
01120     .Case("7400", true)
01121     .Case("g4", true)
01122     .Case("7450", true)
01123     .Case("g4+", true)
01124     .Case("970", true)
01125     .Case("g5", true)
01126     .Case("pwr6", true)
01127     .Case("pwr7", true)
01128     .Case("pwr8", true)
01129     .Case("ppc64", true)
01130     .Case("ppc64le", true)
01131     .Default(false);
01132 
01133   Features["qpx"] = (CPU == "a2q");
01134 
01135   if (!ABI.empty())
01136     Features[ABI] = true;
01137 }
01138 
01139 bool PPCTargetInfo::hasFeature(StringRef Feature) const {
01140   return llvm::StringSwitch<bool>(Feature)
01141     .Case("powerpc", true)
01142     .Case("vsx", HasVSX)
01143     .Case("power8-vector", HasP8Vector)
01144     .Default(false);
01145 }
01146 
01147   
01148 const char * const PPCTargetInfo::GCCRegNames[] = {
01149   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
01150   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
01151   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
01152   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
01153   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
01154   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
01155   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
01156   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
01157   "mq", "lr", "ctr", "ap",
01158   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
01159   "xer",
01160   "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
01161   "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
01162   "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
01163   "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
01164   "vrsave", "vscr",
01165   "spe_acc", "spefscr",
01166   "sfp"
01167 };
01168 
01169 void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
01170                                    unsigned &NumNames) const {
01171   Names = GCCRegNames;
01172   NumNames = llvm::array_lengthof(GCCRegNames);
01173 }
01174 
01175 const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
01176   // While some of these aliases do map to different registers
01177   // they still share the same register name.
01178   { { "0" }, "r0" },
01179   { { "1"}, "r1" },
01180   { { "2" }, "r2" },
01181   { { "3" }, "r3" },
01182   { { "4" }, "r4" },
01183   { { "5" }, "r5" },
01184   { { "6" }, "r6" },
01185   { { "7" }, "r7" },
01186   { { "8" }, "r8" },
01187   { { "9" }, "r9" },
01188   { { "10" }, "r10" },
01189   { { "11" }, "r11" },
01190   { { "12" }, "r12" },
01191   { { "13" }, "r13" },
01192   { { "14" }, "r14" },
01193   { { "15" }, "r15" },
01194   { { "16" }, "r16" },
01195   { { "17" }, "r17" },
01196   { { "18" }, "r18" },
01197   { { "19" }, "r19" },
01198   { { "20" }, "r20" },
01199   { { "21" }, "r21" },
01200   { { "22" }, "r22" },
01201   { { "23" }, "r23" },
01202   { { "24" }, "r24" },
01203   { { "25" }, "r25" },
01204   { { "26" }, "r26" },
01205   { { "27" }, "r27" },
01206   { { "28" }, "r28" },
01207   { { "29" }, "r29" },
01208   { { "30" }, "r30" },
01209   { { "31" }, "r31" },
01210   { { "fr0" }, "f0" },
01211   { { "fr1" }, "f1" },
01212   { { "fr2" }, "f2" },
01213   { { "fr3" }, "f3" },
01214   { { "fr4" }, "f4" },
01215   { { "fr5" }, "f5" },
01216   { { "fr6" }, "f6" },
01217   { { "fr7" }, "f7" },
01218   { { "fr8" }, "f8" },
01219   { { "fr9" }, "f9" },
01220   { { "fr10" }, "f10" },
01221   { { "fr11" }, "f11" },
01222   { { "fr12" }, "f12" },
01223   { { "fr13" }, "f13" },
01224   { { "fr14" }, "f14" },
01225   { { "fr15" }, "f15" },
01226   { { "fr16" }, "f16" },
01227   { { "fr17" }, "f17" },
01228   { { "fr18" }, "f18" },
01229   { { "fr19" }, "f19" },
01230   { { "fr20" }, "f20" },
01231   { { "fr21" }, "f21" },
01232   { { "fr22" }, "f22" },
01233   { { "fr23" }, "f23" },
01234   { { "fr24" }, "f24" },
01235   { { "fr25" }, "f25" },
01236   { { "fr26" }, "f26" },
01237   { { "fr27" }, "f27" },
01238   { { "fr28" }, "f28" },
01239   { { "fr29" }, "f29" },
01240   { { "fr30" }, "f30" },
01241   { { "fr31" }, "f31" },
01242   { { "cc" }, "cr0" },
01243 };
01244 
01245 void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
01246                                      unsigned &NumAliases) const {
01247   Aliases = GCCRegAliases;
01248   NumAliases = llvm::array_lengthof(GCCRegAliases);
01249 }
01250 } // end anonymous namespace.
01251 
01252 namespace {
01253 class PPC32TargetInfo : public PPCTargetInfo {
01254 public:
01255   PPC32TargetInfo(const llvm::Triple &Triple) : PPCTargetInfo(Triple) {
01256     DescriptionString = "E-m:e-p:32:32-i64:64-n32";
01257 
01258     switch (getTriple().getOS()) {
01259     case llvm::Triple::Linux:
01260     case llvm::Triple::FreeBSD:
01261     case llvm::Triple::NetBSD:
01262       SizeType = UnsignedInt;
01263       PtrDiffType = SignedInt;
01264       IntPtrType = SignedInt;
01265       break;
01266     default:
01267       break;
01268     }
01269 
01270     if (getTriple().getOS() == llvm::Triple::FreeBSD) {
01271       LongDoubleWidth = LongDoubleAlign = 64;
01272       LongDoubleFormat = &llvm::APFloat::IEEEdouble;
01273     }
01274 
01275     // PPC32 supports atomics up to 4 bytes.
01276     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
01277   }
01278 
01279   BuiltinVaListKind getBuiltinVaListKind() const override {
01280     // This is the ELF definition, and is overridden by the Darwin sub-target
01281     return TargetInfo::PowerABIBuiltinVaList;
01282   }
01283 };
01284 } // end anonymous namespace.
01285 
01286 // Note: ABI differences may eventually require us to have a separate
01287 // TargetInfo for little endian.
01288 namespace {
01289 class PPC64TargetInfo : public PPCTargetInfo {
01290 public:
01291   PPC64TargetInfo(const llvm::Triple &Triple) : PPCTargetInfo(Triple) {
01292     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
01293     IntMaxType = SignedLong;
01294     Int64Type = SignedLong;
01295 
01296     if ((Triple.getArch() == llvm::Triple::ppc64le)) {
01297       DescriptionString = "e-m:e-i64:64-n32:64";
01298       ABI = "elfv2";
01299     } else {
01300       DescriptionString = "E-m:e-i64:64-n32:64";
01301       ABI = "elfv1";
01302     }
01303 
01304     switch (getTriple().getOS()) {
01305     case llvm::Triple::FreeBSD:
01306       LongDoubleWidth = LongDoubleAlign = 64;
01307       LongDoubleFormat = &llvm::APFloat::IEEEdouble;
01308       break;
01309     case llvm::Triple::NetBSD:
01310       IntMaxType = SignedLongLong;
01311       Int64Type = SignedLongLong;
01312       break;
01313     default:
01314       break;
01315     }
01316 
01317     // PPC64 supports atomics up to 8 bytes.
01318     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
01319   }
01320   BuiltinVaListKind getBuiltinVaListKind() const override {
01321     return TargetInfo::CharPtrBuiltinVaList;
01322   }
01323   // PPC64 Linux-specifc ABI options.
01324   bool setABI(const std::string &Name) override {
01325     if (Name == "elfv1" || Name == "elfv2") {
01326       ABI = Name;
01327       return true;
01328     }
01329     return false;
01330   }
01331 };
01332 } // end anonymous namespace.
01333 
01334 
01335 namespace {
01336 class DarwinPPC32TargetInfo :
01337   public DarwinTargetInfo<PPC32TargetInfo> {
01338 public:
01339   DarwinPPC32TargetInfo(const llvm::Triple &Triple)
01340       : DarwinTargetInfo<PPC32TargetInfo>(Triple) {
01341     HasAlignMac68kSupport = true;
01342     BoolWidth = BoolAlign = 32; //XXX support -mone-byte-bool?
01343     PtrDiffType = SignedInt;    // for http://llvm.org/bugs/show_bug.cgi?id=15726
01344     LongLongAlign = 32;
01345     SuitableAlign = 128;
01346     DescriptionString = "E-m:o-p:32:32-f64:32:64-n32";
01347   }
01348   BuiltinVaListKind getBuiltinVaListKind() const override {
01349     return TargetInfo::CharPtrBuiltinVaList;
01350   }
01351 };
01352 
01353 class DarwinPPC64TargetInfo :
01354   public DarwinTargetInfo<PPC64TargetInfo> {
01355 public:
01356   DarwinPPC64TargetInfo(const llvm::Triple &Triple)
01357       : DarwinTargetInfo<PPC64TargetInfo>(Triple) {
01358     HasAlignMac68kSupport = true;
01359     SuitableAlign = 128;
01360     DescriptionString = "E-m:o-i64:64-n32:64";
01361   }
01362 };
01363 } // end anonymous namespace.
01364 
01365 namespace {
01366   static const unsigned NVPTXAddrSpaceMap[] = {
01367     1,    // opencl_global
01368     3,    // opencl_local
01369     4,    // opencl_constant
01370     1,    // cuda_device
01371     4,    // cuda_constant
01372     3,    // cuda_shared
01373   };
01374   class NVPTXTargetInfo : public TargetInfo {
01375     static const char * const GCCRegNames[];
01376     static const Builtin::Info BuiltinInfo[];
01377   public:
01378     NVPTXTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
01379       BigEndian = false;
01380       TLSSupported = false;
01381       LongWidth = LongAlign = 64;
01382       AddrSpaceMap = &NVPTXAddrSpaceMap;
01383       UseAddrSpaceMapMangling = true;
01384       // Define available target features
01385       // These must be defined in sorted order!
01386       NoAsmVariants = true;
01387     }
01388     void getTargetDefines(const LangOptions &Opts,
01389                           MacroBuilder &Builder) const override {
01390       Builder.defineMacro("__PTX__");
01391       Builder.defineMacro("__NVPTX__");
01392     }
01393     void getTargetBuiltins(const Builtin::Info *&Records,
01394                            unsigned &NumRecords) const override {
01395       Records = BuiltinInfo;
01396       NumRecords = clang::NVPTX::LastTSBuiltin-Builtin::FirstTSBuiltin;
01397     }
01398     bool hasFeature(StringRef Feature) const override {
01399       return Feature == "ptx" || Feature == "nvptx";
01400     }
01401 
01402     void getGCCRegNames(const char * const *&Names,
01403                         unsigned &NumNames) const override;
01404     void getGCCRegAliases(const GCCRegAlias *&Aliases,
01405                                   unsigned &NumAliases) const override {
01406       // No aliases.
01407       Aliases = nullptr;
01408       NumAliases = 0;
01409     }
01410     bool validateAsmConstraint(const char *&Name,
01411                                TargetInfo::ConstraintInfo &Info) const override {
01412       switch (*Name) {
01413       default: return false;
01414       case 'c':
01415       case 'h':
01416       case 'r':
01417       case 'l':
01418       case 'f':
01419       case 'd':
01420         Info.setAllowsRegister();
01421         return true;
01422       }
01423     }
01424     const char *getClobbers() const override {
01425       // FIXME: Is this really right?
01426       return "";
01427     }
01428     BuiltinVaListKind getBuiltinVaListKind() const override {
01429       // FIXME: implement
01430       return TargetInfo::CharPtrBuiltinVaList;
01431     }
01432     bool setCPU(const std::string &Name) override {
01433       bool Valid = llvm::StringSwitch<bool>(Name)
01434         .Case("sm_20", true)
01435         .Case("sm_21", true)
01436         .Case("sm_30", true)
01437         .Case("sm_35", true)
01438         .Default(false);
01439 
01440       return Valid;
01441     }
01442   };
01443 
01444   const Builtin::Info NVPTXTargetInfo::BuiltinInfo[] = {
01445 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
01446 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
01447                                               ALL_LANGUAGES },
01448 #include "clang/Basic/BuiltinsNVPTX.def"
01449   };
01450 
01451   const char * const NVPTXTargetInfo::GCCRegNames[] = {
01452     "r0"
01453   };
01454 
01455   void NVPTXTargetInfo::getGCCRegNames(const char * const *&Names,
01456                                      unsigned &NumNames) const {
01457     Names = GCCRegNames;
01458     NumNames = llvm::array_lengthof(GCCRegNames);
01459   }
01460 
01461   class NVPTX32TargetInfo : public NVPTXTargetInfo {
01462   public:
01463     NVPTX32TargetInfo(const llvm::Triple &Triple) : NVPTXTargetInfo(Triple) {
01464       PointerWidth = PointerAlign = 32;
01465       SizeType     = PtrDiffType = TargetInfo::UnsignedInt;
01466       IntPtrType = TargetInfo::SignedInt;
01467       DescriptionString = "e-p:32:32-i64:64-v16:16-v32:32-n16:32:64";
01468   }
01469   };
01470 
01471   class NVPTX64TargetInfo : public NVPTXTargetInfo {
01472   public:
01473     NVPTX64TargetInfo(const llvm::Triple &Triple) : NVPTXTargetInfo(Triple) {
01474       PointerWidth = PointerAlign = 64;
01475       SizeType     = PtrDiffType = TargetInfo::UnsignedLongLong;
01476       IntPtrType = TargetInfo::SignedLongLong;
01477       DescriptionString = "e-i64:64-v16:16-v32:32-n16:32:64";
01478   }
01479   };
01480 }
01481 
01482 namespace {
01483 
01484 static const unsigned R600AddrSpaceMap[] = {
01485   1,    // opencl_global
01486   3,    // opencl_local
01487   2,    // opencl_constant
01488   1,    // cuda_device
01489   2,    // cuda_constant
01490   3     // cuda_shared
01491 };
01492 
01493 // If you edit the description strings, make sure you update
01494 // getPointerWidthV().
01495 
01496 static const char *DescriptionStringR600 =
01497   "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
01498   "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64";
01499 
01500 static const char *DescriptionStringR600DoubleOps =
01501   "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
01502   "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64";
01503 
01504 static const char *DescriptionStringSI =
01505   "e-p:32:32-p1:64:64-p2:64:64-p3:32:32-p4:64:64-p5:32:32-p24:64:64"
01506   "-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
01507   "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64";
01508 
01509 class R600TargetInfo : public TargetInfo {
01510   static const Builtin::Info BuiltinInfo[];
01511 
01512   /// \brief The GPU profiles supported by the R600 target.
01513   enum GPUKind {
01514     GK_NONE,
01515     GK_R600,
01516     GK_R600_DOUBLE_OPS,
01517     GK_R700,
01518     GK_R700_DOUBLE_OPS,
01519     GK_EVERGREEN,
01520     GK_EVERGREEN_DOUBLE_OPS,
01521     GK_NORTHERN_ISLANDS,
01522     GK_CAYMAN,
01523     GK_SOUTHERN_ISLANDS,
01524     GK_SEA_ISLANDS
01525   } GPU;
01526 
01527 public:
01528   R600TargetInfo(const llvm::Triple &Triple)
01529       : TargetInfo(Triple), GPU(GK_R600) {
01530     DescriptionString = DescriptionStringR600;
01531     AddrSpaceMap = &R600AddrSpaceMap;
01532     UseAddrSpaceMapMangling = true;
01533   }
01534 
01535   uint64_t getPointerWidthV(unsigned AddrSpace) const override {
01536     if (GPU <= GK_CAYMAN)
01537       return 32;
01538 
01539     switch(AddrSpace) {
01540       default:
01541         return 64;
01542       case 0:
01543       case 3:
01544       case 5:
01545         return 32;
01546     }
01547   }
01548 
01549   const char * getClobbers() const override {
01550     return "";
01551   }
01552 
01553   void getGCCRegNames(const char * const *&Names,
01554                       unsigned &numNames) const override {
01555     Names = nullptr;
01556     numNames = 0;
01557   }
01558 
01559   void getGCCRegAliases(const GCCRegAlias *&Aliases,
01560                         unsigned &NumAliases) const override {
01561     Aliases = nullptr;
01562     NumAliases = 0;
01563   }
01564 
01565   bool validateAsmConstraint(const char *&Name,
01566                              TargetInfo::ConstraintInfo &info) const override {
01567     return true;
01568   }
01569 
01570   void getTargetBuiltins(const Builtin::Info *&Records,
01571                          unsigned &NumRecords) const override {
01572     Records = BuiltinInfo;
01573     NumRecords = clang::R600::LastTSBuiltin - Builtin::FirstTSBuiltin;
01574   }
01575 
01576   void getTargetDefines(const LangOptions &Opts,
01577                         MacroBuilder &Builder) const override {
01578     Builder.defineMacro("__R600__");
01579   }
01580 
01581   BuiltinVaListKind getBuiltinVaListKind() const override {
01582     return TargetInfo::CharPtrBuiltinVaList;
01583   }
01584 
01585   bool setCPU(const std::string &Name) override {
01586     GPU = llvm::StringSwitch<GPUKind>(Name)
01587       .Case("r600" ,    GK_R600)
01588       .Case("rv610",    GK_R600)
01589       .Case("rv620",    GK_R600)
01590       .Case("rv630",    GK_R600)
01591       .Case("rv635",    GK_R600)
01592       .Case("rs780",    GK_R600)
01593       .Case("rs880",    GK_R600)
01594       .Case("rv670",    GK_R600_DOUBLE_OPS)
01595       .Case("rv710",    GK_R700)
01596       .Case("rv730",    GK_R700)
01597       .Case("rv740",    GK_R700_DOUBLE_OPS)
01598       .Case("rv770",    GK_R700_DOUBLE_OPS)
01599       .Case("palm",     GK_EVERGREEN)
01600       .Case("cedar",    GK_EVERGREEN)
01601       .Case("sumo",     GK_EVERGREEN)
01602       .Case("sumo2",    GK_EVERGREEN)
01603       .Case("redwood",  GK_EVERGREEN)
01604       .Case("juniper",  GK_EVERGREEN)
01605       .Case("hemlock",  GK_EVERGREEN_DOUBLE_OPS)
01606       .Case("cypress",  GK_EVERGREEN_DOUBLE_OPS)
01607       .Case("barts",    GK_NORTHERN_ISLANDS)
01608       .Case("turks",    GK_NORTHERN_ISLANDS)
01609       .Case("caicos",   GK_NORTHERN_ISLANDS)
01610       .Case("cayman",   GK_CAYMAN)
01611       .Case("aruba",    GK_CAYMAN)
01612       .Case("tahiti",   GK_SOUTHERN_ISLANDS)
01613       .Case("pitcairn", GK_SOUTHERN_ISLANDS)
01614       .Case("verde",    GK_SOUTHERN_ISLANDS)
01615       .Case("oland",    GK_SOUTHERN_ISLANDS)
01616       .Case("hainan",   GK_SOUTHERN_ISLANDS)
01617       .Case("bonaire",  GK_SEA_ISLANDS)
01618       .Case("kabini",   GK_SEA_ISLANDS)
01619       .Case("kaveri",   GK_SEA_ISLANDS)
01620       .Case("hawaii",   GK_SEA_ISLANDS)
01621       .Case("mullins",  GK_SEA_ISLANDS)
01622       .Default(GK_NONE);
01623 
01624     if (GPU == GK_NONE) {
01625       return false;
01626     }
01627 
01628     // Set the correct data layout
01629     switch (GPU) {
01630     case GK_NONE:
01631     case GK_R600:
01632     case GK_R700:
01633     case GK_EVERGREEN:
01634     case GK_NORTHERN_ISLANDS:
01635       DescriptionString = DescriptionStringR600;
01636       break;
01637     case GK_R600_DOUBLE_OPS:
01638     case GK_R700_DOUBLE_OPS:
01639     case GK_EVERGREEN_DOUBLE_OPS:
01640     case GK_CAYMAN:
01641       DescriptionString = DescriptionStringR600DoubleOps;
01642       break;
01643     case GK_SOUTHERN_ISLANDS:
01644     case GK_SEA_ISLANDS:
01645       DescriptionString = DescriptionStringSI;
01646       break;
01647     }
01648 
01649     return true;
01650   }
01651 };
01652 
01653 const Builtin::Info R600TargetInfo::BuiltinInfo[] = {
01654 #define BUILTIN(ID, TYPE, ATTRS)                \
01655   { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
01656 #include "clang/Basic/BuiltinsR600.def"
01657 };
01658 
01659 } // end anonymous namespace
01660 
01661 namespace {
01662 // Namespace for x86 abstract base class
01663 const Builtin::Info BuiltinInfo[] = {
01664 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
01665 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
01666                                               ALL_LANGUAGES },
01667 #include "clang/Basic/BuiltinsX86.def"
01668 };
01669 
01670 static const char* const GCCRegNames[] = {
01671   "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
01672   "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
01673   "argp", "flags", "fpcr", "fpsr", "dirflag", "frame",
01674   "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
01675   "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
01676   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
01677   "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
01678   "ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7",
01679   "ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15",
01680 };
01681 
01682 const TargetInfo::AddlRegName AddlRegNames[] = {
01683   { { "al", "ah", "eax", "rax" }, 0 },
01684   { { "bl", "bh", "ebx", "rbx" }, 3 },
01685   { { "cl", "ch", "ecx", "rcx" }, 2 },
01686   { { "dl", "dh", "edx", "rdx" }, 1 },
01687   { { "esi", "rsi" }, 4 },
01688   { { "edi", "rdi" }, 5 },
01689   { { "esp", "rsp" }, 7 },
01690   { { "ebp", "rbp" }, 6 },
01691 };
01692 
01693 // X86 target abstract base class; x86-32 and x86-64 are very close, so
01694 // most of the implementation can be shared.
01695 class X86TargetInfo : public TargetInfo {
01696   enum X86SSEEnum {
01697     NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2, AVX512F
01698   } SSELevel;
01699   enum MMX3DNowEnum {
01700     NoMMX3DNow, MMX, AMD3DNow, AMD3DNowAthlon
01701   } MMX3DNowLevel;
01702   enum XOPEnum {
01703     NoXOP,
01704     SSE4A,
01705     FMA4,
01706     XOP
01707   } XOPLevel;
01708 
01709   bool HasAES;
01710   bool HasPCLMUL;
01711   bool HasLZCNT;
01712   bool HasRDRND;
01713   bool HasFSGSBASE;
01714   bool HasBMI;
01715   bool HasBMI2;
01716   bool HasPOPCNT;
01717   bool HasRTM;
01718   bool HasPRFCHW;
01719   bool HasRDSEED;
01720   bool HasADX;
01721   bool HasTBM;
01722   bool HasFMA;
01723   bool HasF16C;
01724   bool HasAVX512CD, HasAVX512ER, HasAVX512PF, HasAVX512DQ, HasAVX512BW, HasAVX512VL;
01725   bool HasSHA;
01726   bool HasCX16;
01727 
01728   /// \brief Enumeration of all of the X86 CPUs supported by Clang.
01729   ///
01730   /// Each enumeration represents a particular CPU supported by Clang. These
01731   /// loosely correspond to the options passed to '-march' or '-mtune' flags.
01732   enum CPUKind {
01733     CK_Generic,
01734 
01735     /// \name i386
01736     /// i386-generation processors.
01737     //@{
01738     CK_i386,
01739     //@}
01740 
01741     /// \name i486
01742     /// i486-generation processors.
01743     //@{
01744     CK_i486,
01745     CK_WinChipC6,
01746     CK_WinChip2,
01747     CK_C3,
01748     //@}
01749 
01750     /// \name i586
01751     /// i586-generation processors, P5 microarchitecture based.
01752     //@{
01753     CK_i586,
01754     CK_Pentium,
01755     CK_PentiumMMX,
01756     //@}
01757 
01758     /// \name i686
01759     /// i686-generation processors, P6 / Pentium M microarchitecture based.
01760     //@{
01761     CK_i686,
01762     CK_PentiumPro,
01763     CK_Pentium2,
01764     CK_Pentium3,
01765     CK_Pentium3M,
01766     CK_PentiumM,
01767     CK_C3_2,
01768 
01769     /// This enumerator is a bit odd, as GCC no longer accepts -march=yonah.
01770     /// Clang however has some logic to suport this.
01771     // FIXME: Warn, deprecate, and potentially remove this.
01772     CK_Yonah,
01773     //@}
01774 
01775     /// \name Netburst
01776     /// Netburst microarchitecture based processors.
01777     //@{
01778     CK_Pentium4,
01779     CK_Pentium4M,
01780     CK_Prescott,
01781     CK_Nocona,
01782     //@}
01783 
01784     /// \name Core
01785     /// Core microarchitecture based processors.
01786     //@{
01787     CK_Core2,
01788 
01789     /// This enumerator, like \see CK_Yonah, is a bit odd. It is another
01790     /// codename which GCC no longer accepts as an option to -march, but Clang
01791     /// has some logic for recognizing it.
01792     // FIXME: Warn, deprecate, and potentially remove this.
01793     CK_Penryn,
01794     //@}
01795 
01796     /// \name Atom
01797     /// Atom processors
01798     //@{
01799     CK_Atom,
01800     CK_Silvermont,
01801     //@}
01802 
01803     /// \name Nehalem
01804     /// Nehalem microarchitecture based processors.
01805     //@{
01806     CK_Corei7,
01807     CK_Corei7AVX,
01808     CK_CoreAVXi,
01809     CK_CoreAVX2,
01810     CK_Broadwell,
01811     //@}
01812 
01813     /// \name Knights Landing
01814     /// Knights Landing processor.
01815     CK_KNL,
01816 
01817     /// \name Skylake Server
01818     /// Skylake server processor.
01819     CK_SKX,
01820 
01821     /// \name K6
01822     /// K6 architecture processors.
01823     //@{
01824     CK_K6,
01825     CK_K6_2,
01826     CK_K6_3,
01827     //@}
01828 
01829     /// \name K7
01830     /// K7 architecture processors.
01831     //@{
01832     CK_Athlon,
01833     CK_AthlonThunderbird,
01834     CK_Athlon4,
01835     CK_AthlonXP,
01836     CK_AthlonMP,
01837     //@}
01838 
01839     /// \name K8
01840     /// K8 architecture processors.
01841     //@{
01842     CK_Athlon64,
01843     CK_Athlon64SSE3,
01844     CK_AthlonFX,
01845     CK_K8,
01846     CK_K8SSE3,
01847     CK_Opteron,
01848     CK_OpteronSSE3,
01849     CK_AMDFAM10,
01850     //@}
01851 
01852     /// \name Bobcat
01853     /// Bobcat architecture processors.
01854     //@{
01855     CK_BTVER1,
01856     CK_BTVER2,
01857     //@}
01858 
01859     /// \name Bulldozer
01860     /// Bulldozer architecture processors.
01861     //@{
01862     CK_BDVER1,
01863     CK_BDVER2,
01864     CK_BDVER3,
01865     CK_BDVER4,
01866     //@}
01867 
01868     /// This specification is deprecated and will be removed in the future.
01869     /// Users should prefer \see CK_K8.
01870     // FIXME: Warn on this when the CPU is set to it.
01871     //@{
01872     CK_x86_64,
01873     //@}
01874 
01875     /// \name Geode
01876     /// Geode processors.
01877     //@{
01878     CK_Geode
01879     //@}
01880   } CPU;
01881 
01882   enum FPMathKind {
01883     FP_Default,
01884     FP_SSE,
01885     FP_387
01886   } FPMath;
01887 
01888 public:
01889   X86TargetInfo(const llvm::Triple &Triple)
01890       : TargetInfo(Triple), SSELevel(NoSSE), MMX3DNowLevel(NoMMX3DNow),
01891         XOPLevel(NoXOP), HasAES(false), HasPCLMUL(false), HasLZCNT(false),
01892         HasRDRND(false), HasFSGSBASE(false), HasBMI(false), HasBMI2(false),
01893         HasPOPCNT(false), HasRTM(false), HasPRFCHW(false), HasRDSEED(false),
01894         HasADX(false), HasTBM(false), HasFMA(false), HasF16C(false),
01895         HasAVX512CD(false), HasAVX512ER(false), HasAVX512PF(false),
01896         HasAVX512DQ(false), HasAVX512BW(false), HasAVX512VL(false),
01897         HasSHA(false), HasCX16(false), CPU(CK_Generic), FPMath(FP_Default) {
01898     BigEndian = false;
01899     LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
01900   }
01901   unsigned getFloatEvalMethod() const override {
01902     // X87 evaluates with 80 bits "long double" precision.
01903     return SSELevel == NoSSE ? 2 : 0;
01904   }
01905   void getTargetBuiltins(const Builtin::Info *&Records,
01906                                  unsigned &NumRecords) const override {
01907     Records = BuiltinInfo;
01908     NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
01909   }
01910   void getGCCRegNames(const char * const *&Names,
01911                       unsigned &NumNames) const override {
01912     Names = GCCRegNames;
01913     NumNames = llvm::array_lengthof(GCCRegNames);
01914   }
01915   void getGCCRegAliases(const GCCRegAlias *&Aliases,
01916                         unsigned &NumAliases) const override {
01917     Aliases = nullptr;
01918     NumAliases = 0;
01919   }
01920   void getGCCAddlRegNames(const AddlRegName *&Names,
01921                           unsigned &NumNames) const override {
01922     Names = AddlRegNames;
01923     NumNames = llvm::array_lengthof(AddlRegNames);
01924   }
01925   bool validateAsmConstraint(const char *&Name,
01926                                      TargetInfo::ConstraintInfo &info) const override;
01927 
01928   bool validateOutputSize(StringRef Constraint, unsigned Size) const override;
01929 
01930   bool validateInputSize(StringRef Constraint, unsigned Size) const override;
01931 
01932   virtual bool validateOperandSize(StringRef Constraint, unsigned Size) const;
01933 
01934   std::string convertConstraint(const char *&Constraint) const override;
01935   const char *getClobbers() const override {
01936     return "~{dirflag},~{fpsr},~{flags}";
01937   }
01938   void getTargetDefines(const LangOptions &Opts,
01939                         MacroBuilder &Builder) const override;
01940   static void setSSELevel(llvm::StringMap<bool> &Features, X86SSEEnum Level,
01941                           bool Enabled);
01942   static void setMMXLevel(llvm::StringMap<bool> &Features, MMX3DNowEnum Level,
01943                           bool Enabled);
01944   static void setXOPLevel(llvm::StringMap<bool> &Features, XOPEnum Level,
01945                           bool Enabled);
01946   void setFeatureEnabled(llvm::StringMap<bool> &Features,
01947                          StringRef Name, bool Enabled) const override {
01948     setFeatureEnabledImpl(Features, Name, Enabled);
01949   }
01950   // This exists purely to cut down on the number of virtual calls in
01951   // getDefaultFeatures which calls this repeatedly.
01952   static void setFeatureEnabledImpl(llvm::StringMap<bool> &Features,
01953                                     StringRef Name, bool Enabled);
01954   void getDefaultFeatures(llvm::StringMap<bool> &Features) const override;
01955   bool hasFeature(StringRef Feature) const override;
01956   bool handleTargetFeatures(std::vector<std::string> &Features,
01957                             DiagnosticsEngine &Diags) override;
01958   StringRef getABI() const override {
01959     if (getTriple().getArch() == llvm::Triple::x86_64 && SSELevel >= AVX)
01960       return "avx";
01961     else if (getTriple().getArch() == llvm::Triple::x86 &&
01962              MMX3DNowLevel == NoMMX3DNow)
01963       return "no-mmx";
01964     return "";
01965   }
01966   bool setCPU(const std::string &Name) override {
01967     CPU = llvm::StringSwitch<CPUKind>(Name)
01968       .Case("i386", CK_i386)
01969       .Case("i486", CK_i486)
01970       .Case("winchip-c6", CK_WinChipC6)
01971       .Case("winchip2", CK_WinChip2)
01972       .Case("c3", CK_C3)
01973       .Case("i586", CK_i586)
01974       .Case("pentium", CK_Pentium)
01975       .Case("pentium-mmx", CK_PentiumMMX)
01976       .Case("i686", CK_i686)
01977       .Case("pentiumpro", CK_PentiumPro)
01978       .Case("pentium2", CK_Pentium2)
01979       .Case("pentium3", CK_Pentium3)
01980       .Case("pentium3m", CK_Pentium3M)
01981       .Case("pentium-m", CK_PentiumM)
01982       .Case("c3-2", CK_C3_2)
01983       .Case("yonah", CK_Yonah)
01984       .Case("pentium4", CK_Pentium4)
01985       .Case("pentium4m", CK_Pentium4M)
01986       .Case("prescott", CK_Prescott)
01987       .Case("nocona", CK_Nocona)
01988       .Case("core2", CK_Core2)
01989       .Case("penryn", CK_Penryn)
01990       .Case("atom", CK_Atom)
01991       .Case("slm", CK_Silvermont)
01992       .Case("corei7", CK_Corei7)
01993       .Case("corei7-avx", CK_Corei7AVX)
01994       .Case("core-avx-i", CK_CoreAVXi)
01995       .Case("core-avx2", CK_CoreAVX2)
01996       .Case("broadwell", CK_Broadwell)
01997       .Case("knl", CK_KNL)
01998       .Case("skx", CK_SKX)
01999       .Case("k6", CK_K6)
02000       .Case("k6-2", CK_K6_2)
02001       .Case("k6-3", CK_K6_3)
02002       .Case("athlon", CK_Athlon)
02003       .Case("athlon-tbird", CK_AthlonThunderbird)
02004       .Case("athlon-4", CK_Athlon4)
02005       .Case("athlon-xp", CK_AthlonXP)
02006       .Case("athlon-mp", CK_AthlonMP)
02007       .Case("athlon64", CK_Athlon64)
02008       .Case("athlon64-sse3", CK_Athlon64SSE3)
02009       .Case("athlon-fx", CK_AthlonFX)
02010       .Case("k8", CK_K8)
02011       .Case("k8-sse3", CK_K8SSE3)
02012       .Case("opteron", CK_Opteron)
02013       .Case("opteron-sse3", CK_OpteronSSE3)
02014       .Case("amdfam10", CK_AMDFAM10)
02015       .Case("btver1", CK_BTVER1)
02016       .Case("btver2", CK_BTVER2)
02017       .Case("bdver1", CK_BDVER1)
02018       .Case("bdver2", CK_BDVER2)
02019       .Case("bdver3", CK_BDVER3)
02020       .Case("bdver4", CK_BDVER4)
02021       .Case("x86-64", CK_x86_64)
02022       .Case("geode", CK_Geode)
02023       .Default(CK_Generic);
02024 
02025     // Perform any per-CPU checks necessary to determine if this CPU is
02026     // acceptable.
02027     // FIXME: This results in terrible diagnostics. Clang just says the CPU is
02028     // invalid without explaining *why*.
02029     switch (CPU) {
02030     case CK_Generic:
02031       // No processor selected!
02032       return false;
02033 
02034     case CK_i386:
02035     case CK_i486:
02036     case CK_WinChipC6:
02037     case CK_WinChip2:
02038     case CK_C3:
02039     case CK_i586:
02040     case CK_Pentium:
02041     case CK_PentiumMMX:
02042     case CK_i686:
02043     case CK_PentiumPro:
02044     case CK_Pentium2:
02045     case CK_Pentium3:
02046     case CK_Pentium3M:
02047     case CK_PentiumM:
02048     case CK_Yonah:
02049     case CK_C3_2:
02050     case CK_Pentium4:
02051     case CK_Pentium4M:
02052     case CK_Prescott:
02053     case CK_K6:
02054     case CK_K6_2:
02055     case CK_K6_3:
02056     case CK_Athlon:
02057     case CK_AthlonThunderbird:
02058     case CK_Athlon4:
02059     case CK_AthlonXP:
02060     case CK_AthlonMP:
02061     case CK_Geode:
02062       // Only accept certain architectures when compiling in 32-bit mode.
02063       if (getTriple().getArch() != llvm::Triple::x86)
02064         return false;
02065 
02066       // Fallthrough
02067     case CK_Nocona:
02068     case CK_Core2:
02069     case CK_Penryn:
02070     case CK_Atom:
02071     case CK_Silvermont:
02072     case CK_Corei7:
02073     case CK_Corei7AVX:
02074     case CK_CoreAVXi:
02075     case CK_CoreAVX2:
02076     case CK_Broadwell:
02077     case CK_KNL:
02078     case CK_SKX:
02079     case CK_Athlon64:
02080     case CK_Athlon64SSE3:
02081     case CK_AthlonFX:
02082     case CK_K8:
02083     case CK_K8SSE3:
02084     case CK_Opteron:
02085     case CK_OpteronSSE3:
02086     case CK_AMDFAM10:
02087     case CK_BTVER1:
02088     case CK_BTVER2:
02089     case CK_BDVER1:
02090     case CK_BDVER2:
02091     case CK_BDVER3:
02092     case CK_BDVER4:
02093     case CK_x86_64:
02094       return true;
02095     }
02096     llvm_unreachable("Unhandled CPU kind");
02097   }
02098 
02099   bool setFPMath(StringRef Name) override;
02100 
02101   CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
02102     // We accept all non-ARM calling conventions
02103     return (CC == CC_X86ThisCall ||
02104             CC == CC_X86FastCall ||
02105             CC == CC_X86StdCall ||
02106             CC == CC_X86VectorCall ||
02107             CC == CC_C ||
02108             CC == CC_X86Pascal ||
02109             CC == CC_IntelOclBicc) ? CCCR_OK : CCCR_Warning;
02110   }
02111 
02112   CallingConv getDefaultCallingConv(CallingConvMethodType MT) const override {
02113     return MT == CCMT_Member ? CC_X86ThisCall : CC_C;
02114   }
02115 };
02116 
02117 bool X86TargetInfo::setFPMath(StringRef Name) {
02118   if (Name == "387") {
02119     FPMath = FP_387;
02120     return true;
02121   }
02122   if (Name == "sse") {
02123     FPMath = FP_SSE;
02124     return true;
02125   }
02126   return false;
02127 }
02128 
02129 void X86TargetInfo::getDefaultFeatures(llvm::StringMap<bool> &Features) const {
02130   // FIXME: This *really* should not be here.
02131 
02132   // X86_64 always has SSE2.
02133   if (getTriple().getArch() == llvm::Triple::x86_64)
02134     setFeatureEnabledImpl(Features, "sse2", true);
02135 
02136   switch (CPU) {
02137   case CK_Generic:
02138   case CK_i386:
02139   case CK_i486:
02140   case CK_i586:
02141   case CK_Pentium:
02142   case CK_i686:
02143   case CK_PentiumPro:
02144     break;
02145   case CK_PentiumMMX:
02146   case CK_Pentium2:
02147   case CK_K6:
02148   case CK_WinChipC6:
02149     setFeatureEnabledImpl(Features, "mmx", true);
02150     break;
02151   case CK_Pentium3:
02152   case CK_Pentium3M:
02153   case CK_C3_2:
02154     setFeatureEnabledImpl(Features, "sse", true);
02155     break;
02156   case CK_PentiumM:
02157   case CK_Pentium4:
02158   case CK_Pentium4M:
02159   case CK_x86_64:
02160     setFeatureEnabledImpl(Features, "sse2", true);
02161     break;
02162   case CK_Yonah:
02163   case CK_Prescott:
02164   case CK_Nocona:
02165     setFeatureEnabledImpl(Features, "sse3", true);
02166     setFeatureEnabledImpl(Features, "cx16", true);
02167     break;
02168   case CK_Core2:
02169   case CK_Atom:
02170     setFeatureEnabledImpl(Features, "ssse3", true);
02171     setFeatureEnabledImpl(Features, "cx16", true);
02172     break;
02173   case CK_Penryn:
02174     setFeatureEnabledImpl(Features, "sse4.1", true);
02175     setFeatureEnabledImpl(Features, "cx16", true);
02176     break;
02177   case CK_SKX:
02178     setFeatureEnabledImpl(Features, "avx512f", true);
02179     setFeatureEnabledImpl(Features, "avx512cd", true);
02180     setFeatureEnabledImpl(Features, "avx512dq", true);
02181     setFeatureEnabledImpl(Features, "avx512bw", true);
02182     setFeatureEnabledImpl(Features, "avx512vl", true);
02183     // FALLTHROUGH
02184   case CK_Broadwell:
02185     setFeatureEnabledImpl(Features, "rdseed", true);
02186     setFeatureEnabledImpl(Features, "adx", true);
02187     // FALLTHROUGH
02188   case CK_CoreAVX2:
02189     setFeatureEnabledImpl(Features, "avx2", true);
02190     setFeatureEnabledImpl(Features, "lzcnt", true);
02191     setFeatureEnabledImpl(Features, "bmi", true);
02192     setFeatureEnabledImpl(Features, "bmi2", true);
02193     setFeatureEnabledImpl(Features, "rtm", true);
02194     setFeatureEnabledImpl(Features, "fma", true);
02195     // FALLTHROUGH
02196   case CK_CoreAVXi:
02197     setFeatureEnabledImpl(Features, "rdrnd", true);
02198     setFeatureEnabledImpl(Features, "f16c", true);
02199     setFeatureEnabledImpl(Features, "fsgsbase", true);
02200     // FALLTHROUGH
02201   case CK_Corei7AVX:
02202     setFeatureEnabledImpl(Features, "avx", true);
02203     // FALLTHROUGH
02204   case CK_Silvermont:
02205     setFeatureEnabledImpl(Features, "aes", true);
02206     setFeatureEnabledImpl(Features, "pclmul", true);
02207     // FALLTHROUGH
02208   case CK_Corei7:
02209     setFeatureEnabledImpl(Features, "sse4.2", true);
02210     setFeatureEnabledImpl(Features, "cx16", true);
02211     break;
02212   case CK_KNL:
02213     setFeatureEnabledImpl(Features, "avx512f", true);
02214     setFeatureEnabledImpl(Features, "avx512cd", true);
02215     setFeatureEnabledImpl(Features, "avx512er", true);
02216     setFeatureEnabledImpl(Features, "avx512pf", true);
02217     setFeatureEnabledImpl(Features, "rdseed", true);
02218     setFeatureEnabledImpl(Features, "adx", true);
02219     setFeatureEnabledImpl(Features, "lzcnt", true);
02220     setFeatureEnabledImpl(Features, "bmi", true);
02221     setFeatureEnabledImpl(Features, "bmi2", true);
02222     setFeatureEnabledImpl(Features, "rtm", true);
02223     setFeatureEnabledImpl(Features, "fma", true);
02224     setFeatureEnabledImpl(Features, "rdrnd", true);
02225     setFeatureEnabledImpl(Features, "f16c", true);
02226     setFeatureEnabledImpl(Features, "fsgsbase", true);
02227     setFeatureEnabledImpl(Features, "aes", true);
02228     setFeatureEnabledImpl(Features, "pclmul", true);
02229     setFeatureEnabledImpl(Features, "cx16", true);
02230     break;
02231   case CK_K6_2:
02232   case CK_K6_3:
02233   case CK_WinChip2:
02234   case CK_C3:
02235     setFeatureEnabledImpl(Features, "3dnow", true);
02236     break;
02237   case CK_Athlon:
02238   case CK_AthlonThunderbird:
02239   case CK_Geode:
02240     setFeatureEnabledImpl(Features, "3dnowa", true);
02241     break;
02242   case CK_Athlon4:
02243   case CK_AthlonXP:
02244   case CK_AthlonMP:
02245     setFeatureEnabledImpl(Features, "sse", true);
02246     setFeatureEnabledImpl(Features, "3dnowa", true);
02247     break;
02248   case CK_K8:
02249   case CK_Opteron:
02250   case CK_Athlon64:
02251   case CK_AthlonFX:
02252     setFeatureEnabledImpl(Features, "sse2", true);
02253     setFeatureEnabledImpl(Features, "3dnowa", true);
02254     break;
02255   case CK_AMDFAM10:
02256     setFeatureEnabledImpl(Features, "sse4a", true);
02257     setFeatureEnabledImpl(Features, "lzcnt", true);
02258     setFeatureEnabledImpl(Features, "popcnt", true);
02259     // FALLTHROUGH
02260   case CK_K8SSE3:
02261   case CK_OpteronSSE3:
02262   case CK_Athlon64SSE3:
02263     setFeatureEnabledImpl(Features, "sse3", true);
02264     setFeatureEnabledImpl(Features, "3dnowa", true);
02265     break;
02266   case CK_BTVER2:
02267     setFeatureEnabledImpl(Features, "avx", true);
02268     setFeatureEnabledImpl(Features, "aes", true);
02269     setFeatureEnabledImpl(Features, "pclmul", true);
02270     setFeatureEnabledImpl(Features, "bmi", true);
02271     setFeatureEnabledImpl(Features, "f16c", true);
02272     // FALLTHROUGH
02273   case CK_BTVER1:
02274     setFeatureEnabledImpl(Features, "ssse3", true);
02275     setFeatureEnabledImpl(Features, "sse4a", true);
02276     setFeatureEnabledImpl(Features, "lzcnt", true);
02277     setFeatureEnabledImpl(Features, "popcnt", true);
02278     setFeatureEnabledImpl(Features, "prfchw", true);
02279     setFeatureEnabledImpl(Features, "cx16", true);
02280     break;
02281   case CK_BDVER4:
02282     setFeatureEnabledImpl(Features, "avx2", true);
02283     setFeatureEnabledImpl(Features, "bmi2", true);
02284     // FALLTHROUGH
02285   case CK_BDVER3:
02286     setFeatureEnabledImpl(Features, "fsgsbase", true);
02287     // FALLTHROUGH
02288   case CK_BDVER2:
02289     setFeatureEnabledImpl(Features, "bmi", true);
02290     setFeatureEnabledImpl(Features, "fma", true);
02291     setFeatureEnabledImpl(Features, "f16c", true);
02292     setFeatureEnabledImpl(Features, "tbm", true);
02293     // FALLTHROUGH
02294   case CK_BDVER1:
02295     // xop implies avx, sse4a and fma4.
02296     setFeatureEnabledImpl(Features, "xop", true);
02297     setFeatureEnabledImpl(Features, "lzcnt", true);
02298     setFeatureEnabledImpl(Features, "aes", true);
02299     setFeatureEnabledImpl(Features, "pclmul", true);
02300     setFeatureEnabledImpl(Features, "prfchw", true);
02301     setFeatureEnabledImpl(Features, "cx16", true);
02302     break;
02303   }
02304 }
02305 
02306 void X86TargetInfo::setSSELevel(llvm::StringMap<bool> &Features,
02307                                 X86SSEEnum Level, bool Enabled) {
02308   if (Enabled) {
02309     switch (Level) {
02310     case AVX512F:
02311       Features["avx512f"] = true;
02312     case AVX2:
02313       Features["avx2"] = true;
02314     case AVX:
02315       Features["avx"] = true;
02316     case SSE42:
02317       Features["sse4.2"] = true;
02318     case SSE41:
02319       Features["sse4.1"] = true;
02320     case SSSE3:
02321       Features["ssse3"] = true;
02322     case SSE3:
02323       Features["sse3"] = true;
02324     case SSE2:
02325       Features["sse2"] = true;
02326     case SSE1:
02327       Features["sse"] = true;
02328     case NoSSE:
02329       break;
02330     }
02331     return;
02332   }
02333 
02334   switch (Level) {
02335   case NoSSE:
02336   case SSE1:
02337     Features["sse"] = false;
02338   case SSE2:
02339     Features["sse2"] = Features["pclmul"] = Features["aes"] =
02340       Features["sha"] = false;
02341   case SSE3:
02342     Features["sse3"] = false;
02343     setXOPLevel(Features, NoXOP, false);
02344   case SSSE3:
02345     Features["ssse3"] = false;
02346   case SSE41:
02347     Features["sse4.1"] = false;
02348   case SSE42:
02349     Features["sse4.2"] = false;
02350   case AVX:
02351     Features["fma"] = Features["avx"] = Features["f16c"] = false;
02352     setXOPLevel(Features, FMA4, false);
02353   case AVX2:
02354     Features["avx2"] = false;
02355   case AVX512F:
02356     Features["avx512f"] = Features["avx512cd"] = Features["avx512er"] = Features["avx512pf"] =
02357     Features["avx512dq"] = Features["avx512bw"] = Features["avx512vl"] = false;
02358   }
02359 }
02360 
02361 void X86TargetInfo::setMMXLevel(llvm::StringMap<bool> &Features,
02362                                 MMX3DNowEnum Level, bool Enabled) {
02363   if (Enabled) {
02364     switch (Level) {
02365     case AMD3DNowAthlon:
02366       Features["3dnowa"] = true;
02367     case AMD3DNow:
02368       Features["3dnow"] = true;
02369     case MMX:
02370       Features["mmx"] = true;
02371     case NoMMX3DNow:
02372       break;
02373     }
02374     return;
02375   }
02376 
02377   switch (Level) {
02378   case NoMMX3DNow:
02379   case MMX:
02380     Features["mmx"] = false;
02381   case AMD3DNow:
02382     Features["3dnow"] = false;
02383   case AMD3DNowAthlon:
02384     Features["3dnowa"] = false;
02385   }
02386 }
02387 
02388 void X86TargetInfo::setXOPLevel(llvm::StringMap<bool> &Features, XOPEnum Level,
02389                                 bool Enabled) {
02390   if (Enabled) {
02391     switch (Level) {
02392     case XOP:
02393       Features["xop"] = true;
02394     case FMA4:
02395       Features["fma4"] = true;
02396       setSSELevel(Features, AVX, true);
02397     case SSE4A:
02398       Features["sse4a"] = true;
02399       setSSELevel(Features, SSE3, true);
02400     case NoXOP:
02401       break;
02402     }
02403     return;
02404   }
02405 
02406   switch (Level) {
02407   case NoXOP:
02408   case SSE4A:
02409     Features["sse4a"] = false;
02410   case FMA4:
02411     Features["fma4"] = false;
02412   case XOP:
02413     Features["xop"] = false;
02414   }
02415 }
02416 
02417 void X86TargetInfo::setFeatureEnabledImpl(llvm::StringMap<bool> &Features,
02418                                           StringRef Name, bool Enabled) {
02419   // FIXME: This *really* should not be here.  We need some way of translating
02420   // options into llvm subtarget features.
02421   if (Name == "sse4")
02422     Name = "sse4.2";
02423 
02424   Features[Name] = Enabled;
02425 
02426   if (Name == "mmx") {
02427     setMMXLevel(Features, MMX, Enabled);
02428   } else if (Name == "sse") {
02429     setSSELevel(Features, SSE1, Enabled);
02430   } else if (Name == "sse2") {
02431     setSSELevel(Features, SSE2, Enabled);
02432   } else if (Name == "sse3") {
02433     setSSELevel(Features, SSE3, Enabled);
02434   } else if (Name == "ssse3") {
02435     setSSELevel(Features, SSSE3, Enabled);
02436   } else if (Name == "sse4.2") {
02437     setSSELevel(Features, SSE42, Enabled);
02438   } else if (Name == "sse4.1") {
02439     setSSELevel(Features, SSE41, Enabled);
02440   } else if (Name == "3dnow") {
02441     setMMXLevel(Features, AMD3DNow, Enabled);
02442   } else if (Name == "3dnowa") {
02443     setMMXLevel(Features, AMD3DNowAthlon, Enabled);
02444   } else if (Name == "aes") {
02445     if (Enabled)
02446       setSSELevel(Features, SSE2, Enabled);
02447   } else if (Name == "pclmul") {
02448     if (Enabled)
02449       setSSELevel(Features, SSE2, Enabled);
02450   } else if (Name == "avx") {
02451     setSSELevel(Features, AVX, Enabled);
02452   } else if (Name == "avx2") {
02453     setSSELevel(Features, AVX2, Enabled);
02454   } else if (Name == "avx512f") {
02455     setSSELevel(Features, AVX512F, Enabled);
02456   } else if (Name == "avx512cd" || Name == "avx512er" || Name == "avx512pf"
02457           || Name == "avx512dq" || Name == "avx512bw" || Name == "avx512vl") {
02458     if (Enabled)
02459       setSSELevel(Features, AVX512F, Enabled);
02460   } else if (Name == "fma") {
02461     if (Enabled)
02462       setSSELevel(Features, AVX, Enabled);
02463   } else if (Name == "fma4") {
02464     setXOPLevel(Features, FMA4, Enabled);
02465   } else if (Name == "xop") {
02466     setXOPLevel(Features, XOP, Enabled);
02467   } else if (Name == "sse4a") {
02468     setXOPLevel(Features, SSE4A, Enabled);
02469   } else if (Name == "f16c") {
02470     if (Enabled)
02471       setSSELevel(Features, AVX, Enabled);
02472   } else if (Name == "sha") {
02473     if (Enabled)
02474       setSSELevel(Features, SSE2, Enabled);
02475   }
02476 }
02477 
02478 /// handleTargetFeatures - Perform initialization based on the user
02479 /// configured set of features.
02480 bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
02481                                          DiagnosticsEngine &Diags) {
02482   // Remember the maximum enabled sselevel.
02483   for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
02484     // Ignore disabled features.
02485     if (Features[i][0] == '-')
02486       continue;
02487 
02488     StringRef Feature = StringRef(Features[i]).substr(1);
02489 
02490     if (Feature == "aes") {
02491       HasAES = true;
02492       continue;
02493     }
02494 
02495     if (Feature == "pclmul") {
02496       HasPCLMUL = true;
02497       continue;
02498     }
02499 
02500     if (Feature == "lzcnt") {
02501       HasLZCNT = true;
02502       continue;
02503     }
02504 
02505     if (Feature == "rdrnd") {
02506       HasRDRND = true;
02507       continue;
02508     }
02509 
02510     if (Feature == "fsgsbase") {
02511       HasFSGSBASE = true;
02512       continue;
02513     }
02514 
02515     if (Feature == "bmi") {
02516       HasBMI = true;
02517       continue;
02518     }
02519 
02520     if (Feature == "bmi2") {
02521       HasBMI2 = true;
02522       continue;
02523     }
02524 
02525     if (Feature == "popcnt") {
02526       HasPOPCNT = true;
02527       continue;
02528     }
02529 
02530     if (Feature == "rtm") {
02531       HasRTM = true;
02532       continue;
02533     }
02534 
02535     if (Feature == "prfchw") {
02536       HasPRFCHW = true;
02537       continue;
02538     }
02539 
02540     if (Feature == "rdseed") {
02541       HasRDSEED = true;
02542       continue;
02543     }
02544 
02545     if (Feature == "adx") {
02546       HasADX = true;
02547       continue;
02548     }
02549 
02550     if (Feature == "tbm") {
02551       HasTBM = true;
02552       continue;
02553     }
02554 
02555     if (Feature == "fma") {
02556       HasFMA = true;
02557       continue;
02558     }
02559 
02560     if (Feature == "f16c") {
02561       HasF16C = true;
02562       continue;
02563     }
02564 
02565     if (Feature == "avx512cd") {
02566       HasAVX512CD = true;
02567       continue;
02568     }
02569 
02570     if (Feature == "avx512er") {
02571       HasAVX512ER = true;
02572       continue;
02573     }
02574 
02575     if (Feature == "avx512pf") {
02576       HasAVX512PF = true;
02577       continue;
02578     }
02579 
02580     if (Feature == "avx512dq") {
02581       HasAVX512DQ = true;
02582       continue;
02583     }
02584 
02585     if (Feature == "avx512bw") {
02586       HasAVX512BW = true;
02587       continue;
02588     }
02589 
02590     if (Feature == "avx512vl") {
02591       HasAVX512VL = true;
02592       continue;
02593     }
02594 
02595     if (Feature == "sha") {
02596       HasSHA = true;
02597       continue;
02598     }
02599 
02600     if (Feature == "cx16") {
02601       HasCX16 = true;
02602       continue;
02603     }
02604 
02605     assert(Features[i][0] == '+' && "Invalid target feature!");
02606     X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
02607       .Case("avx512f", AVX512F)
02608       .Case("avx2", AVX2)
02609       .Case("avx", AVX)
02610       .Case("sse4.2", SSE42)
02611       .Case("sse4.1", SSE41)
02612       .Case("ssse3", SSSE3)
02613       .Case("sse3", SSE3)
02614       .Case("sse2", SSE2)
02615       .Case("sse", SSE1)
02616       .Default(NoSSE);
02617     SSELevel = std::max(SSELevel, Level);
02618 
02619     MMX3DNowEnum ThreeDNowLevel =
02620       llvm::StringSwitch<MMX3DNowEnum>(Feature)
02621         .Case("3dnowa", AMD3DNowAthlon)
02622         .Case("3dnow", AMD3DNow)
02623         .Case("mmx", MMX)
02624         .Default(NoMMX3DNow);
02625     MMX3DNowLevel = std::max(MMX3DNowLevel, ThreeDNowLevel);
02626 
02627     XOPEnum XLevel = llvm::StringSwitch<XOPEnum>(Feature)
02628         .Case("xop", XOP)
02629         .Case("fma4", FMA4)
02630         .Case("sse4a", SSE4A)
02631         .Default(NoXOP);
02632     XOPLevel = std::max(XOPLevel, XLevel);
02633   }
02634 
02635   // Enable popcnt if sse4.2 is enabled and popcnt is not explicitly disabled.
02636   // Can't do this earlier because we need to be able to explicitly enable
02637   // popcnt and still disable sse4.2.
02638   if (!HasPOPCNT && SSELevel >= SSE42 &&
02639       std::find(Features.begin(), Features.end(), "-popcnt") == Features.end()){
02640     HasPOPCNT = true;
02641     Features.push_back("+popcnt");
02642   }
02643 
02644   // Enable prfchw if 3DNow! is enabled and prfchw is not explicitly disabled.
02645   if (!HasPRFCHW && MMX3DNowLevel >= AMD3DNow &&
02646       std::find(Features.begin(), Features.end(), "-prfchw") == Features.end()){
02647     HasPRFCHW = true;
02648     Features.push_back("+prfchw");
02649   }
02650 
02651   // LLVM doesn't have a separate switch for fpmath, so only accept it if it
02652   // matches the selected sse level.
02653   if (FPMath == FP_SSE && SSELevel < SSE1) {
02654     Diags.Report(diag::err_target_unsupported_fpmath) << "sse";
02655     return false;
02656   } else if (FPMath == FP_387 && SSELevel >= SSE1) {
02657     Diags.Report(diag::err_target_unsupported_fpmath) << "387";
02658     return false;
02659   }
02660 
02661   // Don't tell the backend if we're turning off mmx; it will end up disabling
02662   // SSE, which we don't want.
02663   // Additionally, if SSE is enabled and mmx is not explicitly disabled,
02664   // then enable MMX.
02665   std::vector<std::string>::iterator it;
02666   it = std::find(Features.begin(), Features.end(), "-mmx");
02667   if (it != Features.end())
02668     Features.erase(it);
02669   else if (SSELevel > NoSSE)
02670     MMX3DNowLevel = std::max(MMX3DNowLevel, MMX);
02671   return true;
02672 }
02673 
02674 /// X86TargetInfo::getTargetDefines - Return the set of the X86-specific macro
02675 /// definitions for this particular subtarget.
02676 void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
02677                                      MacroBuilder &Builder) const {
02678   // Target identification.
02679   if (getTriple().getArch() == llvm::Triple::x86_64) {
02680     Builder.defineMacro("__amd64__");
02681     Builder.defineMacro("__amd64");
02682     Builder.defineMacro("__x86_64");
02683     Builder.defineMacro("__x86_64__");
02684     if (getTriple().getArchName() == "x86_64h") {
02685       Builder.defineMacro("__x86_64h");
02686       Builder.defineMacro("__x86_64h__");
02687     }
02688   } else {
02689     DefineStd(Builder, "i386", Opts);
02690   }
02691 
02692   // Subtarget options.
02693   // FIXME: We are hard-coding the tune parameters based on the CPU, but they
02694   // truly should be based on -mtune options.
02695   switch (CPU) {
02696   case CK_Generic:
02697     break;
02698   case CK_i386:
02699     // The rest are coming from the i386 define above.
02700     Builder.defineMacro("__tune_i386__");
02701     break;
02702   case CK_i486:
02703   case CK_WinChipC6:
02704   case CK_WinChip2:
02705   case CK_C3:
02706     defineCPUMacros(Builder, "i486");
02707     break;
02708   case CK_PentiumMMX:
02709     Builder.defineMacro("__pentium_mmx__");
02710     Builder.defineMacro("__tune_pentium_mmx__");
02711     // Fallthrough
02712   case CK_i586:
02713   case CK_Pentium:
02714     defineCPUMacros(Builder, "i586");
02715     defineCPUMacros(Builder, "pentium");
02716     break;
02717   case CK_Pentium3:
02718   case CK_Pentium3M:
02719   case CK_PentiumM:
02720     Builder.defineMacro("__tune_pentium3__");
02721     // Fallthrough
02722   case CK_Pentium2:
02723   case CK_C3_2:
02724     Builder.defineMacro("__tune_pentium2__");
02725     // Fallthrough
02726   case CK_PentiumPro:
02727     Builder.defineMacro("__tune_i686__");
02728     Builder.defineMacro("__tune_pentiumpro__");
02729     // Fallthrough
02730   case CK_i686:
02731     Builder.defineMacro("__i686");
02732     Builder.defineMacro("__i686__");
02733     // Strangely, __tune_i686__ isn't defined by GCC when CPU == i686.
02734     Builder.defineMacro("__pentiumpro");
02735     Builder.defineMacro("__pentiumpro__");
02736     break;
02737   case CK_Pentium4:
02738   case CK_Pentium4M:
02739     defineCPUMacros(Builder, "pentium4");
02740     break;
02741   case CK_Yonah:
02742   case CK_Prescott:
02743   case CK_Nocona:
02744     defineCPUMacros(Builder, "nocona");
02745     break;
02746   case CK_Core2:
02747   case CK_Penryn:
02748     defineCPUMacros(Builder, "core2");
02749     break;
02750   case CK_Atom:
02751     defineCPUMacros(Builder, "atom");
02752     break;
02753   case CK_Silvermont:
02754     defineCPUMacros(Builder, "slm");
02755     break;
02756   case CK_Corei7:
02757   case CK_Corei7AVX:
02758   case CK_CoreAVXi:
02759   case CK_CoreAVX2:
02760   case CK_Broadwell:
02761     defineCPUMacros(Builder, "corei7");
02762     break;
02763   case CK_KNL:
02764     defineCPUMacros(Builder, "knl");
02765     break;
02766   case CK_SKX:
02767     defineCPUMacros(Builder, "skx");
02768     break;
02769   case CK_K6_2:
02770     Builder.defineMacro("__k6_2__");
02771     Builder.defineMacro("__tune_k6_2__");
02772     // Fallthrough
02773   case CK_K6_3:
02774     if (CPU != CK_K6_2) {  // In case of fallthrough
02775       // FIXME: GCC may be enabling these in cases where some other k6
02776       // architecture is specified but -m3dnow is explicitly provided. The
02777       // exact semantics need to be determined and emulated here.
02778       Builder.defineMacro("__k6_3__");
02779       Builder.defineMacro("__tune_k6_3__");
02780     }
02781     // Fallthrough
02782   case CK_K6:
02783     defineCPUMacros(Builder, "k6");
02784     break;
02785   case CK_Athlon:
02786   case CK_AthlonThunderbird:
02787   case CK_Athlon4:
02788   case CK_AthlonXP:
02789   case CK_AthlonMP:
02790     defineCPUMacros(Builder, "athlon");
02791     if (SSELevel != NoSSE) {
02792       Builder.defineMacro("__athlon_sse__");
02793       Builder.defineMacro("__tune_athlon_sse__");
02794     }
02795     break;
02796   case CK_K8:
02797   case CK_K8SSE3:
02798   case CK_x86_64:
02799   case CK_Opteron:
02800   case CK_OpteronSSE3:
02801   case CK_Athlon64:
02802   case CK_Athlon64SSE3:
02803   case CK_AthlonFX:
02804     defineCPUMacros(Builder, "k8");
02805     break;
02806   case CK_AMDFAM10:
02807     defineCPUMacros(Builder, "amdfam10");
02808     break;
02809   case CK_BTVER1:
02810     defineCPUMacros(Builder, "btver1");
02811     break;
02812   case CK_BTVER2:
02813     defineCPUMacros(Builder, "btver2");
02814     break;
02815   case CK_BDVER1:
02816     defineCPUMacros(Builder, "bdver1");
02817     break;
02818   case CK_BDVER2:
02819     defineCPUMacros(Builder, "bdver2");
02820     break;
02821   case CK_BDVER3:
02822     defineCPUMacros(Builder, "bdver3");
02823     break;
02824   case CK_BDVER4:
02825     defineCPUMacros(Builder, "bdver4");
02826     break;
02827   case CK_Geode:
02828     defineCPUMacros(Builder, "geode");
02829     break;
02830   }
02831 
02832   // Target properties.
02833   Builder.defineMacro("__REGISTER_PREFIX__", "");
02834 
02835   // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
02836   // functions in glibc header files that use FP Stack inline asm which the
02837   // backend can't deal with (PR879).
02838   Builder.defineMacro("__NO_MATH_INLINES");
02839 
02840   if (HasAES)
02841     Builder.defineMacro("__AES__");
02842 
02843   if (HasPCLMUL)
02844     Builder.defineMacro("__PCLMUL__");
02845 
02846   if (HasLZCNT)
02847     Builder.defineMacro("__LZCNT__");
02848 
02849   if (HasRDRND)
02850     Builder.defineMacro("__RDRND__");
02851 
02852   if (HasFSGSBASE)
02853     Builder.defineMacro("__FSGSBASE__");
02854 
02855   if (HasBMI)
02856     Builder.defineMacro("__BMI__");
02857 
02858   if (HasBMI2)
02859     Builder.defineMacro("__BMI2__");
02860 
02861   if (HasPOPCNT)
02862     Builder.defineMacro("__POPCNT__");
02863 
02864   if (HasRTM)
02865     Builder.defineMacro("__RTM__");
02866 
02867   if (HasPRFCHW)
02868     Builder.defineMacro("__PRFCHW__");
02869 
02870   if (HasRDSEED)
02871     Builder.defineMacro("__RDSEED__");
02872 
02873   if (HasADX)
02874     Builder.defineMacro("__ADX__");
02875 
02876   if (HasTBM)
02877     Builder.defineMacro("__TBM__");
02878 
02879   switch (XOPLevel) {
02880   case XOP:
02881     Builder.defineMacro("__XOP__");
02882   case FMA4:
02883     Builder.defineMacro("__FMA4__");
02884   case SSE4A:
02885     Builder.defineMacro("__SSE4A__");
02886   case NoXOP:
02887     break;
02888   }
02889 
02890   if (HasFMA)
02891     Builder.defineMacro("__FMA__");
02892 
02893   if (HasF16C)
02894     Builder.defineMacro("__F16C__");
02895 
02896   if (HasAVX512CD)
02897     Builder.defineMacro("__AVX512CD__");
02898   if (HasAVX512ER)
02899     Builder.defineMacro("__AVX512ER__");
02900   if (HasAVX512PF)
02901     Builder.defineMacro("__AVX512PF__");
02902   if (HasAVX512DQ)
02903     Builder.defineMacro("__AVX512DQ__");
02904   if (HasAVX512BW)
02905     Builder.defineMacro("__AVX512BW__");
02906   if (HasAVX512VL)
02907     Builder.defineMacro("__AVX512VL__");
02908 
02909   if (HasSHA)
02910     Builder.defineMacro("__SHA__");
02911 
02912   if (HasCX16)
02913     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16");
02914 
02915   // Each case falls through to the previous one here.
02916   switch (SSELevel) {
02917   case AVX512F:
02918     Builder.defineMacro("__AVX512F__");
02919   case AVX2:
02920     Builder.defineMacro("__AVX2__");
02921   case AVX:
02922     Builder.defineMacro("__AVX__");
02923   case SSE42:
02924     Builder.defineMacro("__SSE4_2__");
02925   case SSE41:
02926     Builder.defineMacro("__SSE4_1__");
02927   case SSSE3:
02928     Builder.defineMacro("__SSSE3__");
02929   case SSE3:
02930     Builder.defineMacro("__SSE3__");
02931   case SSE2:
02932     Builder.defineMacro("__SSE2__");
02933     Builder.defineMacro("__SSE2_MATH__");  // -mfp-math=sse always implied.
02934   case SSE1:
02935     Builder.defineMacro("__SSE__");
02936     Builder.defineMacro("__SSE_MATH__");   // -mfp-math=sse always implied.
02937   case NoSSE:
02938     break;
02939   }
02940 
02941   if (Opts.MicrosoftExt && getTriple().getArch() == llvm::Triple::x86) {
02942     switch (SSELevel) {
02943     case AVX512F:
02944     case AVX2:
02945     case AVX:
02946     case SSE42:
02947     case SSE41:
02948     case SSSE3:
02949     case SSE3:
02950     case SSE2:
02951       Builder.defineMacro("_M_IX86_FP", Twine(2));
02952       break;
02953     case SSE1:
02954       Builder.defineMacro("_M_IX86_FP", Twine(1));
02955       break;
02956     default:
02957       Builder.defineMacro("_M_IX86_FP", Twine(0));
02958     }
02959   }
02960 
02961   // Each case falls through to the previous one here.
02962   switch (MMX3DNowLevel) {
02963   case AMD3DNowAthlon:
02964     Builder.defineMacro("__3dNOW_A__");
02965   case AMD3DNow:
02966     Builder.defineMacro("__3dNOW__");
02967   case MMX:
02968     Builder.defineMacro("__MMX__");
02969   case NoMMX3DNow:
02970     break;
02971   }
02972 
02973   if (CPU >= CK_i486) {
02974     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
02975     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
02976     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
02977   }
02978   if (CPU >= CK_i586)
02979     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
02980 }
02981 
02982 bool X86TargetInfo::hasFeature(StringRef Feature) const {
02983   return llvm::StringSwitch<bool>(Feature)
02984       .Case("aes", HasAES)
02985       .Case("avx", SSELevel >= AVX)
02986       .Case("avx2", SSELevel >= AVX2)
02987       .Case("avx512f", SSELevel >= AVX512F)
02988       .Case("avx512cd", HasAVX512CD)
02989       .Case("avx512er", HasAVX512ER)
02990       .Case("avx512pf", HasAVX512PF)
02991       .Case("avx512dq", HasAVX512DQ)
02992       .Case("avx512bw", HasAVX512BW)
02993       .Case("avx512vl", HasAVX512VL)
02994       .Case("bmi", HasBMI)
02995       .Case("bmi2", HasBMI2)
02996       .Case("cx16", HasCX16)
02997       .Case("f16c", HasF16C)
02998       .Case("fma", HasFMA)
02999       .Case("fma4", XOPLevel >= FMA4)
03000       .Case("fsgsbase", HasFSGSBASE)
03001       .Case("lzcnt", HasLZCNT)
03002       .Case("mm3dnow", MMX3DNowLevel >= AMD3DNow)
03003       .Case("mm3dnowa", MMX3DNowLevel >= AMD3DNowAthlon)
03004       .Case("mmx", MMX3DNowLevel >= MMX)
03005       .Case("pclmul", HasPCLMUL)
03006       .Case("popcnt", HasPOPCNT)
03007       .Case("prfchw", HasPRFCHW)
03008       .Case("rdrnd", HasRDRND)
03009       .Case("rdseed", HasRDSEED)
03010       .Case("rtm", HasRTM)
03011       .Case("sha", HasSHA)
03012       .Case("sse", SSELevel >= SSE1)
03013       .Case("sse2", SSELevel >= SSE2)
03014       .Case("sse3", SSELevel >= SSE3)
03015       .Case("ssse3", SSELevel >= SSSE3)
03016       .Case("sse4.1", SSELevel >= SSE41)
03017       .Case("sse4.2", SSELevel >= SSE42)
03018       .Case("sse4a", XOPLevel >= SSE4A)
03019       .Case("tbm", HasTBM)
03020       .Case("x86", true)
03021       .Case("x86_32", getTriple().getArch() == llvm::Triple::x86)
03022       .Case("x86_64", getTriple().getArch() == llvm::Triple::x86_64)
03023       .Case("xop", XOPLevel >= XOP)
03024       .Default(false);
03025 }
03026 
03027 bool
03028 X86TargetInfo::validateAsmConstraint(const char *&Name,
03029                                      TargetInfo::ConstraintInfo &Info) const {
03030   switch (*Name) {
03031   default: return false;
03032   case 'Y': // first letter of a pair:
03033     switch (*(Name+1)) {
03034     default: return false;
03035     case '0':  // First SSE register.
03036     case 't':  // Any SSE register, when SSE2 is enabled.
03037     case 'i':  // Any SSE register, when SSE2 and inter-unit moves enabled.
03038     case 'm':  // any MMX register, when inter-unit moves enabled.
03039       break;   // falls through to setAllowsRegister.
03040   }
03041   case 'f': // any x87 floating point stack register.
03042     // Constraint 'f' cannot be used for output operands.
03043     if (Info.ConstraintStr[0] == '=')
03044       return false;
03045 
03046     Info.setAllowsRegister();
03047     return true;
03048   case 'a': // eax.
03049   case 'b': // ebx.
03050   case 'c': // ecx.
03051   case 'd': // edx.
03052   case 'S': // esi.
03053   case 'D': // edi.
03054   case 'A': // edx:eax.
03055   case 't': // top of floating point stack.
03056   case 'u': // second from top of floating point stack.
03057   case 'q': // Any register accessible as [r]l: a, b, c, and d.
03058   case 'y': // Any MMX register.
03059   case 'x': // Any SSE register.
03060   case 'Q': // Any register accessible as [r]h: a, b, c, and d.
03061   case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
03062   case 'l': // "Index" registers: any general register that can be used as an
03063             // index in a base+index memory access.
03064     Info.setAllowsRegister();
03065     return true;
03066   case 'C': // SSE floating point constant.
03067   case 'G': // x87 floating point constant.
03068   case 'e': // 32-bit signed integer constant for use with zero-extending
03069             // x86_64 instructions.
03070   case 'Z': // 32-bit unsigned integer constant for use with zero-extending
03071             // x86_64 instructions.
03072     return true;
03073   }
03074 }
03075 
03076 bool X86TargetInfo::validateOutputSize(StringRef Constraint,
03077                                        unsigned Size) const {
03078   // Strip off constraint modifiers.
03079   while (Constraint[0] == '=' ||
03080          Constraint[0] == '+' ||
03081          Constraint[0] == '&')
03082     Constraint = Constraint.substr(1);
03083 
03084   return validateOperandSize(Constraint, Size);
03085 }
03086 
03087 bool X86TargetInfo::validateInputSize(StringRef Constraint,
03088                                       unsigned Size) const {
03089   return validateOperandSize(Constraint, Size);
03090 }
03091 
03092 bool X86TargetInfo::validateOperandSize(StringRef Constraint,
03093                                         unsigned Size) const {
03094   switch (Constraint[0]) {
03095   default: break;
03096   case 'y':
03097     return Size <= 64;
03098   case 'f':
03099   case 't':
03100   case 'u':
03101     return Size <= 128;
03102   case 'x':
03103     // 256-bit ymm registers can be used if target supports AVX.
03104     return Size <= (SSELevel >= AVX ? 256U : 128U);
03105   }
03106 
03107   return true;
03108 }
03109 
03110 std::string
03111 X86TargetInfo::convertConstraint(const char *&Constraint) const {
03112   switch (*Constraint) {
03113   case 'a': return std::string("{ax}");
03114   case 'b': return std::string("{bx}");
03115   case 'c': return std::string("{cx}");
03116   case 'd': return std::string("{dx}");
03117   case 'S': return std::string("{si}");
03118   case 'D': return std::string("{di}");
03119   case 'p': // address
03120     return std::string("im");
03121   case 't': // top of floating point stack.
03122     return std::string("{st}");
03123   case 'u': // second from top of floating point stack.
03124     return std::string("{st(1)}"); // second from top of floating point stack.
03125   default:
03126     return std::string(1, *Constraint);
03127   }
03128 }
03129 } // end anonymous namespace
03130 
03131 namespace {
03132 // X86-32 generic target
03133 class X86_32TargetInfo : public X86TargetInfo {
03134 public:
03135   X86_32TargetInfo(const llvm::Triple &Triple) : X86TargetInfo(Triple) {
03136     DoubleAlign = LongLongAlign = 32;
03137     LongDoubleWidth = 96;
03138     LongDoubleAlign = 32;
03139     SuitableAlign = 128;
03140     DescriptionString = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128";
03141     SizeType = UnsignedInt;
03142     PtrDiffType = SignedInt;
03143     IntPtrType = SignedInt;
03144     RegParmMax = 3;
03145 
03146     // Use fpret for all types.
03147     RealTypeUsesObjCFPRet = ((1 << TargetInfo::Float) |
03148                              (1 << TargetInfo::Double) |
03149                              (1 << TargetInfo::LongDouble));
03150 
03151     // x86-32 has atomics up to 8 bytes
03152     // FIXME: Check that we actually have cmpxchg8b before setting
03153     // MaxAtomicInlineWidth. (cmpxchg8b is an i586 instruction.)
03154     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
03155   }
03156   BuiltinVaListKind getBuiltinVaListKind() const override {
03157     return TargetInfo::CharPtrBuiltinVaList;
03158   }
03159 
03160   int getEHDataRegisterNumber(unsigned RegNo) const override {
03161     if (RegNo == 0) return 0;
03162     if (RegNo == 1) return 2;
03163     return -1;
03164   }
03165   bool validateOperandSize(StringRef Constraint,
03166                            unsigned Size) const override {
03167     switch (Constraint[0]) {
03168     default: break;
03169     case 'R':
03170     case 'q':
03171     case 'Q':
03172     case 'a':
03173     case 'b':
03174     case 'c':
03175     case 'd':
03176     case 'S':
03177     case 'D':
03178       return Size <= 32;
03179     case 'A':
03180       return Size <= 64;
03181     }
03182 
03183     return X86TargetInfo::validateOperandSize(Constraint, Size);
03184   }
03185 };
03186 } // end anonymous namespace
03187 
03188 namespace {
03189 class NetBSDI386TargetInfo : public NetBSDTargetInfo<X86_32TargetInfo> {
03190 public:
03191   NetBSDI386TargetInfo(const llvm::Triple &Triple)
03192       : NetBSDTargetInfo<X86_32TargetInfo>(Triple) {}
03193 
03194   unsigned getFloatEvalMethod() const override {
03195     unsigned Major, Minor, Micro;
03196     getTriple().getOSVersion(Major, Minor, Micro);
03197     // New NetBSD uses the default rounding mode.
03198     if (Major >= 7 || (Major == 6 && Minor == 99 && Micro >= 26) || Major == 0)
03199       return X86_32TargetInfo::getFloatEvalMethod();
03200     // NetBSD before 6.99.26 defaults to "double" rounding.
03201     return 1;
03202   }
03203 };
03204 } // end anonymous namespace
03205 
03206 namespace {
03207 class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
03208 public:
03209   OpenBSDI386TargetInfo(const llvm::Triple &Triple)
03210       : OpenBSDTargetInfo<X86_32TargetInfo>(Triple) {
03211     SizeType = UnsignedLong;
03212     IntPtrType = SignedLong;
03213     PtrDiffType = SignedLong;
03214   }
03215 };
03216 } // end anonymous namespace
03217 
03218 namespace {
03219 class BitrigI386TargetInfo : public BitrigTargetInfo<X86_32TargetInfo> {
03220 public:
03221   BitrigI386TargetInfo(const llvm::Triple &Triple)
03222       : BitrigTargetInfo<X86_32TargetInfo>(Triple) {
03223     SizeType = UnsignedLong;
03224     IntPtrType = SignedLong;
03225     PtrDiffType = SignedLong;
03226   }
03227 };
03228 } // end anonymous namespace
03229 
03230 namespace {
03231 class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
03232 public:
03233   DarwinI386TargetInfo(const llvm::Triple &Triple)
03234       : DarwinTargetInfo<X86_32TargetInfo>(Triple) {
03235     LongDoubleWidth = 128;
03236     LongDoubleAlign = 128;
03237     SuitableAlign = 128;
03238     MaxVectorAlign = 256;
03239     SizeType = UnsignedLong;
03240     IntPtrType = SignedLong;
03241     DescriptionString = "e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128";
03242     HasAlignMac68kSupport = true;
03243   }
03244 
03245 };
03246 } // end anonymous namespace
03247 
03248 namespace {
03249 // x86-32 Windows target
03250 class WindowsX86_32TargetInfo : public WindowsTargetInfo<X86_32TargetInfo> {
03251 public:
03252   WindowsX86_32TargetInfo(const llvm::Triple &Triple)
03253       : WindowsTargetInfo<X86_32TargetInfo>(Triple) {
03254     WCharType = UnsignedShort;
03255     DoubleAlign = LongLongAlign = 64;
03256     DescriptionString = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32";
03257   }
03258   void getTargetDefines(const LangOptions &Opts,
03259                         MacroBuilder &Builder) const override {
03260     WindowsTargetInfo<X86_32TargetInfo>::getTargetDefines(Opts, Builder);
03261   }
03262 };
03263 
03264 // x86-32 Windows Visual Studio target
03265 class MicrosoftX86_32TargetInfo : public WindowsX86_32TargetInfo {
03266 public:
03267   MicrosoftX86_32TargetInfo(const llvm::Triple &Triple)
03268       : WindowsX86_32TargetInfo(Triple) {
03269     LongDoubleWidth = LongDoubleAlign = 64;
03270     LongDoubleFormat = &llvm::APFloat::IEEEdouble;
03271   }
03272   void getTargetDefines(const LangOptions &Opts,
03273                         MacroBuilder &Builder) const override {
03274     WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
03275     WindowsX86_32TargetInfo::getVisualStudioDefines(Opts, Builder);
03276     // The value of the following reflects processor type.
03277     // 300=386, 400=486, 500=Pentium, 600=Blend (default)
03278     // We lost the original triple, so we use the default.
03279     Builder.defineMacro("_M_IX86", "600");
03280   }
03281 };
03282 } // end anonymous namespace
03283 
03284 static void addMinGWDefines(const LangOptions &Opts, MacroBuilder &Builder) {
03285   Builder.defineMacro("__MSVCRT__");
03286   Builder.defineMacro("__MINGW32__");
03287 
03288   // Mingw defines __declspec(a) to __attribute__((a)).  Clang supports
03289   // __declspec natively under -fms-extensions, but we define a no-op __declspec
03290   // macro anyway for pre-processor compatibility.
03291   if (Opts.MicrosoftExt)
03292     Builder.defineMacro("__declspec", "__declspec");
03293   else
03294     Builder.defineMacro("__declspec(a)", "__attribute__((a))");
03295 
03296   if (!Opts.MicrosoftExt) {
03297     // Provide macros for all the calling convention keywords.  Provide both
03298     // single and double underscore prefixed variants.  These are available on
03299     // x64 as well as x86, even though they have no effect.
03300     const char *CCs[] = {"cdecl", "stdcall", "fastcall", "thiscall", "pascal"};
03301     for (const char *CC : CCs) {
03302       std::string GCCSpelling = "__attribute__((__";
03303       GCCSpelling += CC;
03304       GCCSpelling += "__))";
03305       Builder.defineMacro(Twine("_") + CC, GCCSpelling);
03306       Builder.defineMacro(Twine("__") + CC, GCCSpelling);
03307     }
03308   }
03309 }
03310 
03311 namespace {
03312 // x86-32 MinGW target
03313 class MinGWX86_32TargetInfo : public WindowsX86_32TargetInfo {
03314 public:
03315   MinGWX86_32TargetInfo(const llvm::Triple &Triple)
03316       : WindowsX86_32TargetInfo(Triple) {}
03317   void getTargetDefines(const LangOptions &Opts,
03318                         MacroBuilder &Builder) const override {
03319     WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
03320     DefineStd(Builder, "WIN32", Opts);
03321     DefineStd(Builder, "WINNT", Opts);
03322     Builder.defineMacro("_X86_");
03323     addMinGWDefines(Opts, Builder);
03324   }
03325 };
03326 } // end anonymous namespace
03327 
03328 namespace {
03329 // x86-32 Cygwin target
03330 class CygwinX86_32TargetInfo : public X86_32TargetInfo {
03331 public:
03332   CygwinX86_32TargetInfo(const llvm::Triple &Triple)
03333       : X86_32TargetInfo(Triple) {
03334     TLSSupported = false;
03335     WCharType = UnsignedShort;
03336     DoubleAlign = LongLongAlign = 64;
03337     DescriptionString = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32";
03338   }
03339   void getTargetDefines(const LangOptions &Opts,
03340                         MacroBuilder &Builder) const override {
03341     X86_32TargetInfo::getTargetDefines(Opts, Builder);
03342     Builder.defineMacro("_X86_");
03343     Builder.defineMacro("__CYGWIN__");
03344     Builder.defineMacro("__CYGWIN32__");
03345     DefineStd(Builder, "unix", Opts);
03346     if (Opts.CPlusPlus)
03347       Builder.defineMacro("_GNU_SOURCE");
03348   }
03349 };
03350 } // end anonymous namespace
03351 
03352 namespace {
03353 // x86-32 Haiku target
03354 class HaikuX86_32TargetInfo : public X86_32TargetInfo {
03355 public:
03356   HaikuX86_32TargetInfo(const llvm::Triple &Triple) : X86_32TargetInfo(Triple) {
03357     SizeType = UnsignedLong;
03358     IntPtrType = SignedLong;
03359     PtrDiffType = SignedLong;
03360     ProcessIDType = SignedLong;
03361     this->UserLabelPrefix = "";
03362     this->TLSSupported = false;
03363   }
03364   void getTargetDefines(const LangOptions &Opts,
03365                         MacroBuilder &Builder) const override {
03366     X86_32TargetInfo::getTargetDefines(Opts, Builder);
03367     Builder.defineMacro("__INTEL__");
03368     Builder.defineMacro("__HAIKU__");
03369   }
03370 };
03371 } // end anonymous namespace
03372 
03373 // RTEMS Target
03374 template<typename Target>
03375 class RTEMSTargetInfo : public OSTargetInfo<Target> {
03376 protected:
03377   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
03378                     MacroBuilder &Builder) const override {
03379     // RTEMS defines; list based off of gcc output
03380 
03381     Builder.defineMacro("__rtems__");
03382     Builder.defineMacro("__ELF__");
03383   }
03384 
03385 public:
03386   RTEMSTargetInfo(const llvm::Triple &Triple) : OSTargetInfo<Target>(Triple) {
03387     this->UserLabelPrefix = "";
03388 
03389     switch (Triple.getArch()) {
03390     default:
03391     case llvm::Triple::x86:
03392       // this->MCountName = ".mcount";
03393       break;
03394     case llvm::Triple::mips:
03395     case llvm::Triple::mipsel:
03396     case llvm::Triple::ppc:
03397     case llvm::Triple::ppc64:
03398     case llvm::Triple::ppc64le:
03399       // this->MCountName = "_mcount";
03400       break;
03401     case llvm::Triple::arm:
03402       // this->MCountName = "__mcount";
03403       break;
03404     }
03405   }
03406 };
03407 
03408 namespace {
03409 // x86-32 RTEMS target
03410 class RTEMSX86_32TargetInfo : public X86_32TargetInfo {
03411 public:
03412   RTEMSX86_32TargetInfo(const llvm::Triple &Triple) : X86_32TargetInfo(Triple) {
03413     SizeType = UnsignedLong;
03414     IntPtrType = SignedLong;
03415     PtrDiffType = SignedLong;
03416     this->UserLabelPrefix = "";
03417   }
03418   void getTargetDefines(const LangOptions &Opts,
03419                         MacroBuilder &Builder) const override {
03420     X86_32TargetInfo::getTargetDefines(Opts, Builder);
03421     Builder.defineMacro("__INTEL__");
03422     Builder.defineMacro("__rtems__");
03423   }
03424 };
03425 } // end anonymous namespace
03426 
03427 namespace {
03428 // x86-64 generic target
03429 class X86_64TargetInfo : public X86TargetInfo {
03430 public:
03431   X86_64TargetInfo(const llvm::Triple &Triple) : X86TargetInfo(Triple) {
03432     const bool IsX32 = getTriple().getEnvironment() == llvm::Triple::GNUX32;
03433     LongWidth = LongAlign = PointerWidth = PointerAlign = IsX32 ? 32 : 64;
03434     LongDoubleWidth = 128;
03435     LongDoubleAlign = 128;
03436     LargeArrayMinWidth = 128;
03437     LargeArrayAlign = 128;
03438     SuitableAlign = 128;
03439     SizeType    = IsX32 ? UnsignedInt      : UnsignedLong;
03440     PtrDiffType = IsX32 ? SignedInt        : SignedLong;
03441     IntPtrType  = IsX32 ? SignedInt        : SignedLong;
03442     IntMaxType  = IsX32 ? SignedLongLong   : SignedLong;
03443     Int64Type   = IsX32 ? SignedLongLong   : SignedLong;
03444     RegParmMax = 6;
03445 
03446     DescriptionString = (IsX32)
03447                             ? "e-m:e-" "p:32:32-" "i64:64-f80:128-n8:16:32:64-S128"
03448                             : "e-m:e-"            "i64:64-f80:128-n8:16:32:64-S128";
03449 
03450     // Use fpret only for long double.
03451     RealTypeUsesObjCFPRet = (1 << TargetInfo::LongDouble);
03452 
03453     // Use fp2ret for _Complex long double.
03454     ComplexLongDoubleUsesFP2Ret = true;
03455 
03456     // x86-64 has atomics up to 16 bytes.
03457     MaxAtomicPromoteWidth = 128;
03458     MaxAtomicInlineWidth = 128;
03459   }
03460   BuiltinVaListKind getBuiltinVaListKind() const override {
03461     return TargetInfo::X86_64ABIBuiltinVaList;
03462   }
03463 
03464   int getEHDataRegisterNumber(unsigned RegNo) const override {
03465     if (RegNo == 0) return 0;
03466     if (RegNo == 1) return 1;
03467     return -1;
03468   }
03469 
03470   CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
03471     return (CC == CC_C ||
03472             CC == CC_X86VectorCall ||
03473             CC == CC_IntelOclBicc ||
03474             CC == CC_X86_64Win64) ? CCCR_OK : CCCR_Warning;
03475   }
03476 
03477   CallingConv getDefaultCallingConv(CallingConvMethodType MT) const override {
03478     return CC_C;
03479   }
03480 
03481   // for x32 we need it here explicitly
03482   bool hasInt128Type() const override { return true; }
03483 };
03484 } // end anonymous namespace
03485 
03486 namespace {
03487 // x86-64 Windows target
03488 class WindowsX86_64TargetInfo : public WindowsTargetInfo<X86_64TargetInfo> {
03489 public:
03490   WindowsX86_64TargetInfo(const llvm::Triple &Triple)
03491       : WindowsTargetInfo<X86_64TargetInfo>(Triple) {
03492     WCharType = UnsignedShort;
03493     LongWidth = LongAlign = 32;
03494     DoubleAlign = LongLongAlign = 64;
03495     IntMaxType = SignedLongLong;
03496     Int64Type = SignedLongLong;
03497     SizeType = UnsignedLongLong;
03498     PtrDiffType = SignedLongLong;
03499     IntPtrType = SignedLongLong;
03500     this->UserLabelPrefix = "";
03501   }
03502   void getTargetDefines(const LangOptions &Opts,
03503                                 MacroBuilder &Builder) const override {
03504     WindowsTargetInfo<X86_64TargetInfo>::getTargetDefines(Opts, Builder);
03505     Builder.defineMacro("_WIN64");
03506   }
03507   BuiltinVaListKind getBuiltinVaListKind() const override {
03508     return TargetInfo::CharPtrBuiltinVaList;
03509   }
03510   CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
03511     return (CC == CC_C ||
03512             CC == CC_X86VectorCall ||
03513             CC == CC_IntelOclBicc ||
03514             CC == CC_X86_64SysV) ? CCCR_OK : CCCR_Warning;
03515   }
03516 };
03517 } // end anonymous namespace
03518 
03519 namespace {
03520 // x86-64 Windows Visual Studio target
03521 class MicrosoftX86_64TargetInfo : public WindowsX86_64TargetInfo {
03522 public:
03523   MicrosoftX86_64TargetInfo(const llvm::Triple &Triple)
03524       : WindowsX86_64TargetInfo(Triple) {
03525     LongDoubleWidth = LongDoubleAlign = 64;
03526     LongDoubleFormat = &llvm::APFloat::IEEEdouble;
03527   }
03528   void getTargetDefines(const LangOptions &Opts,
03529                         MacroBuilder &Builder) const override {
03530     WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
03531     WindowsX86_64TargetInfo::getVisualStudioDefines(Opts, Builder);
03532     Builder.defineMacro("_M_X64");
03533     Builder.defineMacro("_M_AMD64");
03534   }
03535 };
03536 } // end anonymous namespace
03537 
03538 namespace {
03539 // x86-64 MinGW target
03540 class MinGWX86_64TargetInfo : public WindowsX86_64TargetInfo {
03541 public:
03542   MinGWX86_64TargetInfo(const llvm::Triple &Triple)
03543       : WindowsX86_64TargetInfo(Triple) {}
03544   void getTargetDefines(const LangOptions &Opts,
03545                         MacroBuilder &Builder) const override {
03546     WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
03547     DefineStd(Builder, "WIN64", Opts);
03548     Builder.defineMacro("__MINGW64__");
03549     addMinGWDefines(Opts, Builder);
03550 
03551     // GCC defines this macro when it is using __gxx_personality_seh0.
03552     if (!Opts.SjLjExceptions)
03553       Builder.defineMacro("__SEH__");
03554   }
03555 };
03556 } // end anonymous namespace
03557 
03558 namespace {
03559 class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
03560 public:
03561   DarwinX86_64TargetInfo(const llvm::Triple &Triple)
03562       : DarwinTargetInfo<X86_64TargetInfo>(Triple) {
03563     Int64Type = SignedLongLong;
03564     MaxVectorAlign = 256;
03565     // The 64-bit iOS simulator uses the builtin bool type for Objective-C.
03566     llvm::Triple T = llvm::Triple(Triple);
03567     if (T.isiOS())
03568       UseSignedCharForObjCBool = false;
03569     DescriptionString = "e-m:o-i64:64-f80:128-n8:16:32:64-S128";
03570   }
03571 };
03572 } // end anonymous namespace
03573 
03574 namespace {
03575 class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
03576 public:
03577   OpenBSDX86_64TargetInfo(const llvm::Triple &Triple)
03578       : OpenBSDTargetInfo<X86_64TargetInfo>(Triple) {
03579     IntMaxType = SignedLongLong;
03580     Int64Type = SignedLongLong;
03581   }
03582 };
03583 } // end anonymous namespace
03584 
03585 namespace {
03586 class BitrigX86_64TargetInfo : public BitrigTargetInfo<X86_64TargetInfo> {
03587 public:
03588   BitrigX86_64TargetInfo(const llvm::Triple &Triple)
03589       : BitrigTargetInfo<X86_64TargetInfo>(Triple) {
03590     IntMaxType = SignedLongLong;
03591     Int64Type = SignedLongLong;
03592   }
03593 };
03594 }
03595 
03596 
03597 namespace {
03598 class ARMTargetInfo : public TargetInfo {
03599   // Possible FPU choices.
03600   enum FPUMode {
03601     VFP2FPU = (1 << 0),
03602     VFP3FPU = (1 << 1),
03603     VFP4FPU = (1 << 2),
03604     NeonFPU = (1 << 3),
03605     FPARMV8 = (1 << 4)
03606   };
03607 
03608   // Possible HWDiv features.
03609   enum HWDivMode {
03610     HWDivThumb = (1 << 0),
03611     HWDivARM = (1 << 1)
03612   };
03613 
03614   static bool FPUModeIsVFP(FPUMode Mode) {
03615     return Mode & (VFP2FPU | VFP3FPU | VFP4FPU | NeonFPU | FPARMV8);
03616   }
03617 
03618   static const TargetInfo::GCCRegAlias GCCRegAliases[];
03619   static const char * const GCCRegNames[];
03620 
03621   std::string ABI, CPU;
03622 
03623   enum {
03624     FP_Default,
03625     FP_VFP,
03626     FP_Neon
03627   } FPMath;
03628 
03629   unsigned FPU : 5;
03630 
03631   unsigned IsAAPCS : 1;
03632   unsigned IsThumb : 1;
03633   unsigned HWDiv : 2;
03634 
03635   // Initialized via features.
03636   unsigned SoftFloat : 1;
03637   unsigned SoftFloatABI : 1;
03638 
03639   unsigned CRC : 1;
03640   unsigned Crypto : 1;
03641 
03642   // ACLE 6.5.1 Hardware floating point
03643   enum {
03644     HW_FP_HP = (1 << 1), /// half (16-bit)
03645     HW_FP_SP = (1 << 2), /// single (32-bit)
03646     HW_FP_DP = (1 << 3), /// double (64-bit)
03647   };
03648   uint32_t HW_FP;
03649 
03650   static const Builtin::Info BuiltinInfo[];
03651 
03652   static bool shouldUseInlineAtomic(const llvm::Triple &T) {
03653     StringRef ArchName = T.getArchName();
03654     if (T.getArch() == llvm::Triple::arm ||
03655         T.getArch() == llvm::Triple::armeb) {
03656       StringRef VersionStr;
03657       if (ArchName.startswith("armv"))
03658         VersionStr = ArchName.substr(4, 1);
03659       else if (ArchName.startswith("armebv"))
03660         VersionStr = ArchName.substr(6, 1);
03661       else
03662         return false;
03663       unsigned Version;
03664       if (VersionStr.getAsInteger(10, Version))
03665         return false;
03666       return Version >= 6;
03667     }
03668     assert(T.getArch() == llvm::Triple::thumb ||
03669            T.getArch() == llvm::Triple::thumbeb);
03670     StringRef VersionStr;
03671     if (ArchName.startswith("thumbv"))
03672       VersionStr = ArchName.substr(6, 1);
03673     else if (ArchName.startswith("thumbebv"))
03674       VersionStr = ArchName.substr(8, 1);
03675     else
03676       return false;
03677     unsigned Version;
03678     if (VersionStr.getAsInteger(10, Version))
03679       return false;
03680     return Version >= 7;
03681   }
03682 
03683   void setABIAAPCS() {
03684     IsAAPCS = true;
03685 
03686     DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 64;
03687     const llvm::Triple &T = getTriple();
03688 
03689     // size_t is unsigned long on MachO-derived environments and NetBSD.
03690     if (T.isOSBinFormatMachO() || T.getOS() == llvm::Triple::NetBSD)
03691       SizeType = UnsignedLong;
03692     else
03693       SizeType = UnsignedInt;
03694 
03695     switch (T.getOS()) {
03696     case llvm::Triple::NetBSD:
03697       WCharType = SignedInt;
03698       break;
03699     case llvm::Triple::Win32:
03700       WCharType = UnsignedShort;
03701       break;
03702     case llvm::Triple::Linux:
03703     default:
03704       // AAPCS 7.1.1, ARM-Linux ABI 2.4: type of wchar_t is unsigned int.
03705       WCharType = UnsignedInt;
03706       break;
03707     }
03708 
03709     UseBitFieldTypeAlignment = true;
03710 
03711     ZeroLengthBitfieldBoundary = 0;
03712 
03713     // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
03714     // so set preferred for small types to 32.
03715     if (T.isOSBinFormatMachO()) {
03716       DescriptionString =
03717           BigEndian ? "E-m:o-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
03718                     : "e-m:o-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64";
03719     } else if (T.isOSWindows()) {
03720       // FIXME: this is invalid for WindowsCE
03721       assert(!BigEndian && "Windows on ARM does not support big endian");
03722       DescriptionString = "e"
03723                           "-m:e"
03724                           "-p:32:32"
03725                           "-i64:64"
03726                           "-v128:64:128"
03727                           "-a:0:32"
03728                           "-n32"
03729                           "-S64";
03730     } else {
03731       DescriptionString =
03732           BigEndian ? "E-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
03733                     : "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64";
03734     }
03735 
03736     // FIXME: Enumerated types are variable width in straight AAPCS.
03737   }
03738 
03739   void setABIAPCS() {
03740     const llvm::Triple &T = getTriple();
03741 
03742     IsAAPCS = false;
03743 
03744     DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 32;
03745 
03746     // size_t is unsigned int on FreeBSD.
03747     if (T.getOS() == llvm::Triple::FreeBSD)
03748       SizeType = UnsignedInt;
03749     else
03750       SizeType = UnsignedLong;
03751 
03752     // Revert to using SignedInt on apcs-gnu to comply with existing behaviour.
03753     WCharType = SignedInt;
03754 
03755     // Do not respect the alignment of bit-field types when laying out
03756     // structures. This corresponds to PCC_BITFIELD_TYPE_MATTERS in gcc.
03757     UseBitFieldTypeAlignment = false;
03758 
03759     /// gcc forces the alignment to 4 bytes, regardless of the type of the
03760     /// zero length bitfield.  This corresponds to EMPTY_FIELD_BOUNDARY in
03761     /// gcc.
03762     ZeroLengthBitfieldBoundary = 32;
03763 
03764     if (T.isOSBinFormatMachO())
03765       DescriptionString =
03766           BigEndian
03767               ? "E-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
03768               : "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32";
03769     else
03770       DescriptionString =
03771           BigEndian
03772               ? "E-m:e-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
03773               : "e-m:e-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32";
03774 
03775     // FIXME: Override "preferred align" for double and long long.
03776   }
03777 
03778 public:
03779   ARMTargetInfo(const llvm::Triple &Triple, bool IsBigEndian)
03780       : TargetInfo(Triple), CPU("arm1136j-s"), FPMath(FP_Default),
03781         IsAAPCS(true), HW_FP(0) {
03782     BigEndian = IsBigEndian;
03783 
03784     switch (getTriple().getOS()) {
03785     case llvm::Triple::NetBSD:
03786       PtrDiffType = SignedLong;
03787       break;
03788     default:
03789       PtrDiffType = SignedInt;
03790       break;
03791     }
03792 
03793     // {} in inline assembly are neon specifiers, not assembly variant
03794     // specifiers.
03795     NoAsmVariants = true;
03796 
03797     // FIXME: Should we just treat this as a feature?
03798     IsThumb = getTriple().getArchName().startswith("thumb");
03799 
03800     setABI("aapcs-linux");
03801 
03802     // ARM targets default to using the ARM C++ ABI.
03803     TheCXXABI.set(TargetCXXABI::GenericARM);
03804 
03805     // ARM has atomics up to 8 bytes
03806     MaxAtomicPromoteWidth = 64;
03807     if (shouldUseInlineAtomic(getTriple()))
03808       MaxAtomicInlineWidth = 64;
03809 
03810     // Do force alignment of members that follow zero length bitfields.  If
03811     // the alignment of the zero-length bitfield is greater than the member 
03812     // that follows it, `bar', `bar' will be aligned as the  type of the 
03813     // zero length bitfield.
03814     UseZeroLengthBitfieldAlignment = true;
03815   }
03816   StringRef getABI() const override { return ABI; }
03817   bool setABI(const std::string &Name) override {
03818     ABI = Name;
03819 
03820     // The defaults (above) are for AAPCS, check if we need to change them.
03821     //
03822     // FIXME: We need support for -meabi... we could just mangle it into the
03823     // name.
03824     if (Name == "apcs-gnu") {
03825       setABIAPCS();
03826       return true;
03827     }
03828     if (Name == "aapcs" || Name == "aapcs-vfp" || Name == "aapcs-linux") {
03829       setABIAAPCS();
03830       return true;
03831     }
03832     return false;
03833   }
03834 
03835   void getDefaultFeatures(llvm::StringMap<bool> &Features) const override {
03836     if (IsAAPCS)
03837       Features["aapcs"] = true;
03838     else
03839       Features["apcs"] = true;
03840 
03841     StringRef ArchName = getTriple().getArchName();
03842     if (CPU == "arm1136jf-s" || CPU == "arm1176jzf-s" || CPU == "mpcore")
03843       Features["vfp2"] = true;
03844     else if (CPU == "cortex-a8" || CPU == "cortex-a9") {
03845       Features["vfp3"] = true;
03846       Features["neon"] = true;
03847     }
03848     else if (CPU == "cortex-a5") {
03849       Features["vfp4"] = true;
03850       Features["neon"] = true;
03851     } else if (CPU == "swift" || CPU == "cortex-a7" ||
03852                CPU == "cortex-a12" || CPU == "cortex-a15" ||
03853                CPU == "cortex-a17" || CPU == "krait") {
03854       Features["vfp4"] = true;
03855       Features["neon"] = true;
03856       Features["hwdiv"] = true;
03857       Features["hwdiv-arm"] = true;
03858     } else if (CPU == "cyclone") {
03859       Features["v8fp"] = true;
03860       Features["neon"] = true;
03861       Features["hwdiv"] = true;
03862       Features["hwdiv-arm"] = true;
03863     } else if (CPU == "cortex-a53" || CPU == "cortex-a57") {
03864       Features["fp-armv8"] = true;
03865       Features["neon"] = true;
03866       Features["hwdiv"] = true;
03867       Features["hwdiv-arm"] = true;
03868       Features["crc"] = true;
03869       Features["crypto"] = true;
03870     } else if (CPU == "cortex-r5" ||
03871                // Enable the hwdiv extension for all v8a AArch32 cores by
03872                // default.
03873                ArchName == "armv8a" || ArchName == "armv8" ||
03874                ArchName == "armebv8a" || ArchName == "armebv8" ||
03875                ArchName == "thumbv8a" || ArchName == "thumbv8" ||
03876                ArchName == "thumbebv8a" || ArchName == "thumbebv8") {
03877       Features["hwdiv"] = true;
03878       Features["hwdiv-arm"] = true;
03879     } else if (CPU == "cortex-m3" || CPU == "cortex-m4" || CPU == "cortex-m7") {
03880       Features["hwdiv"] = true;
03881     }
03882   }
03883 
03884   bool handleTargetFeatures(std::vector<std::string> &Features,
03885                             DiagnosticsEngine &Diags) override {
03886     FPU = 0;
03887     CRC = 0;
03888     Crypto = 0;
03889     SoftFloat = SoftFloatABI = false;
03890     HWDiv = 0;
03891 
03892     for (const auto &Feature : Features) {
03893       if (Feature == "+soft-float") {
03894         SoftFloat = true;
03895       } else if (Feature == "+soft-float-abi") {
03896         SoftFloatABI = true;
03897       } else if (Feature == "+vfp2") {
03898         FPU |= VFP2FPU;
03899         HW_FP = HW_FP_SP | HW_FP_DP;
03900       } else if (Feature == "+vfp3") {
03901         FPU |= VFP3FPU;
03902         HW_FP = HW_FP_SP | HW_FP_DP;
03903       } else if (Feature == "+vfp4") {
03904         FPU |= VFP4FPU;
03905         HW_FP = HW_FP_SP | HW_FP_DP | HW_FP_HP;
03906       } else if (Feature == "+fp-armv8") {
03907         FPU |= FPARMV8;
03908         HW_FP = HW_FP_SP | HW_FP_DP | HW_FP_HP;
03909       } else if (Feature == "+neon") {
03910         FPU |= NeonFPU;
03911         HW_FP = HW_FP_SP | HW_FP_DP;
03912       } else if (Feature == "+hwdiv") {
03913         HWDiv |= HWDivThumb;
03914       } else if (Feature == "+hwdiv-arm") {
03915         HWDiv |= HWDivARM;
03916       } else if (Feature == "+crc") {
03917         CRC = 1;
03918       } else if (Feature == "+crypto") {
03919         Crypto = 1;
03920       } else if (Feature == "+fp-only-sp") {
03921         HW_FP &= ~HW_FP_DP;
03922       }
03923     }
03924 
03925     if (!(FPU & NeonFPU) && FPMath == FP_Neon) {
03926       Diags.Report(diag::err_target_unsupported_fpmath) << "neon";
03927       return false;
03928     }
03929 
03930     if (FPMath == FP_Neon)
03931       Features.push_back("+neonfp");
03932     else if (FPMath == FP_VFP)
03933       Features.push_back("-neonfp");
03934 
03935     // Remove front-end specific options which the backend handles differently.
03936     const StringRef FrontEndFeatures[] = { "+soft-float", "+soft-float-abi" };
03937     for (const auto &FEFeature : FrontEndFeatures) {
03938       auto Feature = std::find(Features.begin(), Features.end(), FEFeature);
03939       if (Feature != Features.end())
03940         Features.erase(Feature);
03941     }
03942 
03943     return true;
03944   }
03945 
03946   bool hasFeature(StringRef Feature) const override {
03947     return llvm::StringSwitch<bool>(Feature)
03948         .Case("arm", true)
03949         .Case("softfloat", SoftFloat)
03950         .Case("thumb", IsThumb)
03951         .Case("neon", (FPU & NeonFPU) && !SoftFloat)
03952         .Case("hwdiv", HWDiv & HWDivThumb)
03953         .Case("hwdiv-arm", HWDiv & HWDivARM)
03954         .Default(false);
03955   }
03956   // FIXME: Should we actually have some table instead of these switches?
03957   static const char *getCPUDefineSuffix(StringRef Name) {
03958     return llvm::StringSwitch<const char*>(Name)
03959       .Cases("arm8", "arm810", "4")
03960       .Cases("strongarm", "strongarm110", "strongarm1100", "strongarm1110", "4")
03961       .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "arm720t", "arm9", "4T")
03962       .Cases("arm9tdmi", "arm920", "arm920t", "arm922t", "arm940t", "4T")
03963       .Case("ep9312", "4T")
03964       .Cases("arm10tdmi", "arm1020t", "5T")
03965       .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "5TE")
03966       .Case("arm926ej-s", "5TEJ")
03967       .Cases("arm10e", "arm1020e", "arm1022e", "5TE")
03968       .Cases("xscale", "iwmmxt", "5TE")
03969       .Case("arm1136j-s", "6J")
03970       .Cases("arm1176jz-s", "arm1176jzf-s", "6ZK")
03971       .Cases("arm1136jf-s", "mpcorenovfp", "mpcore", "6K")
03972       .Cases("arm1156t2-s", "arm1156t2f-s", "6T2")
03973       .Cases("cortex-a5", "cortex-a7", "cortex-a8", "7A")
03974       .Cases("cortex-a9", "cortex-a12", "cortex-a15", "cortex-a17", "krait", "7A")
03975       .Cases("cortex-r4", "cortex-r5", "7R")
03976       .Case("swift", "7S")
03977       .Case("cyclone", "8A")
03978       .Case("cortex-m3", "7M")
03979       .Cases("cortex-m4", "cortex-m7", "7EM")
03980       .Case("cortex-m0", "6M")
03981       .Cases("cortex-a53", "cortex-a57", "8A")
03982       .Default(nullptr);
03983   }
03984   static const char *getCPUProfile(StringRef Name) {
03985     return llvm::StringSwitch<const char*>(Name)
03986       .Cases("cortex-a5", "cortex-a7", "cortex-a8", "A")
03987       .Cases("cortex-a9", "cortex-a12", "cortex-a15", "cortex-a17", "krait", "A")
03988       .Cases("cortex-a53", "cortex-a57", "A")
03989       .Cases("cortex-m3", "cortex-m4", "cortex-m0", "cortex-m7", "M")
03990       .Cases("cortex-r4", "cortex-r5", "R")
03991       .Default("");
03992   }
03993   bool setCPU(const std::string &Name) override {
03994     if (!getCPUDefineSuffix(Name))
03995       return false;
03996 
03997     // Cortex M does not support 8 byte atomics, while general Thumb2 does.
03998     StringRef Profile = getCPUProfile(Name);
03999     if (Profile == "M" && MaxAtomicInlineWidth) {
04000       MaxAtomicPromoteWidth = 32;
04001       MaxAtomicInlineWidth = 32;
04002     }
04003 
04004     CPU = Name;
04005     return true;
04006   }
04007   bool setFPMath(StringRef Name) override;
04008   bool supportsThumb(StringRef ArchName, StringRef CPUArch,
04009                      unsigned CPUArchVer) const {
04010     return CPUArchVer >= 7 || (CPUArch.find('T') != StringRef::npos) ||
04011            (CPUArch.find('M') != StringRef::npos);
04012   }
04013   bool supportsThumb2(StringRef ArchName, StringRef CPUArch,
04014                       unsigned CPUArchVer) const {
04015     // We check both CPUArchVer and ArchName because when only triple is
04016     // specified, the default CPU is arm1136j-s.
04017     return ArchName.endswith("v6t2") || ArchName.endswith("v7") ||
04018            ArchName.endswith("v8") || CPUArch == "6T2" || CPUArchVer >= 7;
04019   }
04020   void getTargetDefines(const LangOptions &Opts,
04021                         MacroBuilder &Builder) const override {
04022     // Target identification.
04023     Builder.defineMacro("__arm");
04024     Builder.defineMacro("__arm__");
04025 
04026     // Target properties.
04027     Builder.defineMacro("__REGISTER_PREFIX__", "");
04028 
04029     StringRef CPUArch = getCPUDefineSuffix(CPU);
04030     unsigned int CPUArchVer;
04031     if (CPUArch.substr(0, 1).getAsInteger<unsigned int>(10, CPUArchVer))
04032       llvm_unreachable("Invalid char for architecture version number");
04033     Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
04034 
04035     // ACLE 6.4.1 ARM/Thumb instruction set architecture
04036     StringRef CPUProfile = getCPUProfile(CPU);
04037     StringRef ArchName = getTriple().getArchName();
04038 
04039     // __ARM_ARCH is defined as an integer value indicating the current ARM ISA
04040     Builder.defineMacro("__ARM_ARCH", CPUArch.substr(0, 1));
04041     if (CPUArch[0] >= '8') {                                                    
04042       Builder.defineMacro("__ARM_FEATURE_NUMERIC_MAXMIN");                      
04043       Builder.defineMacro("__ARM_FEATURE_DIRECTED_ROUNDING");                   
04044     }
04045 
04046     // __ARM_ARCH_ISA_ARM is defined to 1 if the core supports the ARM ISA.  It
04047     // is not defined for the M-profile.
04048     // NOTE that the deffault profile is assumed to be 'A'
04049     if (CPUProfile.empty() || CPUProfile != "M")
04050       Builder.defineMacro("__ARM_ARCH_ISA_ARM", "1");
04051 
04052     // __ARM_ARCH_ISA_THUMB is defined to 1 if the core supporst the original
04053     // Thumb ISA (including v6-M).  It is set to 2 if the core supports the
04054     // Thumb-2 ISA as found in the v6T2 architecture and all v7 architecture.
04055     if (supportsThumb2(ArchName, CPUArch, CPUArchVer))
04056       Builder.defineMacro("__ARM_ARCH_ISA_THUMB", "2");
04057     else if (supportsThumb(ArchName, CPUArch, CPUArchVer))
04058       Builder.defineMacro("__ARM_ARCH_ISA_THUMB", "1");
04059 
04060     // __ARM_32BIT_STATE is defined to 1 if code is being generated for a 32-bit
04061     // instruction set such as ARM or Thumb.
04062     Builder.defineMacro("__ARM_32BIT_STATE", "1");
04063 
04064     // ACLE 6.4.2 Architectural Profile (A, R, M or pre-Cortex)
04065 
04066     // __ARM_ARCH_PROFILE is defined as 'A', 'R', 'M' or 'S', or unset.
04067     if (!CPUProfile.empty())
04068       Builder.defineMacro("__ARM_ARCH_PROFILE", "'" + CPUProfile + "'");
04069 
04070     // ACLE 6.5.1 Hardware Floating Point
04071     if (HW_FP)
04072       Builder.defineMacro("__ARM_FP", "0x" + llvm::utohexstr(HW_FP));
04073 
04074     // ACLE predefines.
04075     Builder.defineMacro("__ARM_ACLE", "200");
04076 
04077     // Subtarget options.
04078 
04079     // FIXME: It's more complicated than this and we don't really support
04080     // interworking.
04081     // Windows on ARM does not "support" interworking
04082     if (5 <= CPUArchVer && CPUArchVer <= 8 && !getTriple().isOSWindows())
04083       Builder.defineMacro("__THUMB_INTERWORK__");
04084 
04085     if (ABI == "aapcs" || ABI == "aapcs-linux" || ABI == "aapcs-vfp") {
04086       // Embedded targets on Darwin follow AAPCS, but not EABI.
04087       // Windows on ARM follows AAPCS VFP, but does not conform to EABI.
04088       if (!getTriple().isOSDarwin() && !getTriple().isOSWindows())
04089         Builder.defineMacro("__ARM_EABI__");
04090       Builder.defineMacro("__ARM_PCS", "1");
04091 
04092       if ((!SoftFloat && !SoftFloatABI) || ABI == "aapcs-vfp")
04093         Builder.defineMacro("__ARM_PCS_VFP", "1");
04094     }
04095 
04096     if (SoftFloat)
04097       Builder.defineMacro("__SOFTFP__");
04098 
04099     if (CPU == "xscale")
04100       Builder.defineMacro("__XSCALE__");
04101 
04102     if (IsThumb) {
04103       Builder.defineMacro("__THUMBEL__");
04104       Builder.defineMacro("__thumb__");
04105       if (supportsThumb2(ArchName, CPUArch, CPUArchVer))
04106         Builder.defineMacro("__thumb2__");
04107     }
04108     if (((HWDiv & HWDivThumb) && IsThumb) || ((HWDiv & HWDivARM) && !IsThumb))
04109       Builder.defineMacro("__ARM_ARCH_EXT_IDIV__", "1");
04110 
04111     // Note, this is always on in gcc, even though it doesn't make sense.
04112     Builder.defineMacro("__APCS_32__");
04113 
04114     if (FPUModeIsVFP((FPUMode) FPU)) {
04115       Builder.defineMacro("__VFP_FP__");
04116       if (FPU & VFP2FPU)
04117         Builder.defineMacro("__ARM_VFPV2__");
04118       if (FPU & VFP3FPU)
04119         Builder.defineMacro("__ARM_VFPV3__");
04120       if (FPU & VFP4FPU)
04121         Builder.defineMacro("__ARM_VFPV4__");
04122     }
04123 
04124     // This only gets set when Neon instructions are actually available, unlike
04125     // the VFP define, hence the soft float and arch check. This is subtly
04126     // different from gcc, we follow the intent which was that it should be set
04127     // when Neon instructions are actually available.
04128     if ((FPU & NeonFPU) && !SoftFloat && CPUArchVer >= 7) {
04129       Builder.defineMacro("__ARM_NEON");
04130       Builder.defineMacro("__ARM_NEON__");
04131     }
04132 
04133     Builder.defineMacro("__ARM_SIZEOF_WCHAR_T",
04134                         Opts.ShortWChar ? "2" : "4");
04135 
04136     Builder.defineMacro("__ARM_SIZEOF_MINIMAL_ENUM",
04137                         Opts.ShortEnums ? "1" : "4");
04138 
04139     if (CRC)
04140       Builder.defineMacro("__ARM_FEATURE_CRC32");
04141 
04142     if (Crypto)
04143       Builder.defineMacro("__ARM_FEATURE_CRYPTO");
04144 
04145     if (CPUArchVer >= 6 && CPUArch != "6M") {
04146       Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
04147       Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
04148       Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
04149       Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
04150     }
04151   }
04152   void getTargetBuiltins(const Builtin::Info *&Records,
04153                          unsigned &NumRecords) const override {
04154     Records = BuiltinInfo;
04155     NumRecords = clang::ARM::LastTSBuiltin-Builtin::FirstTSBuiltin;
04156   }
04157   bool isCLZForZeroUndef() const override { return false; }
04158   BuiltinVaListKind getBuiltinVaListKind() const override {
04159     return IsAAPCS ? AAPCSABIBuiltinVaList : TargetInfo::VoidPtrBuiltinVaList;
04160   }
04161   void getGCCRegNames(const char * const *&Names,
04162                       unsigned &NumNames) const override;
04163   void getGCCRegAliases(const GCCRegAlias *&Aliases,
04164                         unsigned &NumAliases) const override;
04165   bool validateAsmConstraint(const char *&Name,
04166                              TargetInfo::ConstraintInfo &Info) const override {
04167     switch (*Name) {
04168     default: break;
04169     case 'l': // r0-r7
04170     case 'h': // r8-r15
04171     case 'w': // VFP Floating point register single precision
04172     case 'P': // VFP Floating point register double precision
04173       Info.setAllowsRegister();
04174       return true;
04175     case 'Q': // A memory address that is a single base register.
04176       Info.setAllowsMemory();
04177       return true;
04178     case 'U': // a memory reference...
04179       switch (Name[1]) {
04180       case 'q': // ...ARMV4 ldrsb
04181       case 'v': // ...VFP load/store (reg+constant offset)
04182       case 'y': // ...iWMMXt load/store
04183       case 't': // address valid for load/store opaque types wider
04184                 // than 128-bits
04185       case 'n': // valid address for Neon doubleword vector load/store
04186       case 'm': // valid address for Neon element and structure load/store
04187       case 's': // valid address for non-offset loads/stores of quad-word
04188                 // values in four ARM registers
04189         Info.setAllowsMemory();
04190         Name++;
04191         return true;
04192       }
04193     }
04194     return false;
04195   }
04196   std::string convertConstraint(const char *&Constraint) const override {
04197     std::string R;
04198     switch (*Constraint) {
04199     case 'U':   // Two-character constraint; add "^" hint for later parsing.
04200       R = std::string("^") + std::string(Constraint, 2);
04201       Constraint++;
04202       break;
04203     case 'p': // 'p' should be translated to 'r' by default.
04204       R = std::string("r");
04205       break;
04206     default:
04207       return std::string(1, *Constraint);
04208     }
04209     return R;
04210   }
04211   bool
04212   validateConstraintModifier(StringRef Constraint, char Modifier, unsigned Size,
04213                              std::string &SuggestedModifier) const override {
04214     bool isOutput = (Constraint[0] == '=');
04215     bool isInOut = (Constraint[0] == '+');
04216 
04217     // Strip off constraint modifiers.
04218     while (Constraint[0] == '=' ||
04219            Constraint[0] == '+' ||
04220            Constraint[0] == '&')
04221       Constraint = Constraint.substr(1);
04222 
04223     switch (Constraint[0]) {
04224     default: break;
04225     case 'r': {
04226       switch (Modifier) {
04227       default:
04228         return (isInOut || isOutput || Size <= 64);
04229       case 'q':
04230         // A register of size 32 cannot fit a vector type.
04231         return false;
04232       }
04233     }
04234     }
04235 
04236     return true;
04237   }
04238   const char *getClobbers() const override {
04239     // FIXME: Is this really right?
04240     return "";
04241   }
04242 
04243   CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
04244     return (CC == CC_AAPCS || CC == CC_AAPCS_VFP) ? CCCR_OK : CCCR_Warning;
04245   }
04246 
04247   int getEHDataRegisterNumber(unsigned RegNo) const override {
04248     if (RegNo == 0) return 0;
04249     if (RegNo == 1) return 1;
04250     return -1;
04251   }
04252 };
04253 
04254 bool ARMTargetInfo::setFPMath(StringRef Name) {
04255   if (Name == "neon") {
04256     FPMath = FP_Neon;
04257     return true;
04258   } else if (Name == "vfp" || Name == "vfp2" || Name == "vfp3" ||
04259              Name == "vfp4") {
04260     FPMath = FP_VFP;
04261     return true;
04262   }
04263   return false;
04264 }
04265 
04266 const char * const ARMTargetInfo::GCCRegNames[] = {
04267   // Integer registers
04268   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
04269   "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
04270 
04271   // Float registers
04272   "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
04273   "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
04274   "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
04275   "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
04276 
04277   // Double registers
04278   "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
04279   "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
04280   "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
04281   "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
04282 
04283   // Quad registers
04284   "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
04285   "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"
04286 };
04287 
04288 void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
04289                                    unsigned &NumNames) const {
04290   Names = GCCRegNames;
04291   NumNames = llvm::array_lengthof(GCCRegNames);
04292 }
04293 
04294 const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
04295   { { "a1" }, "r0" },
04296   { { "a2" }, "r1" },
04297   { { "a3" }, "r2" },
04298   { { "a4" }, "r3" },
04299   { { "v1" }, "r4" },
04300   { { "v2" }, "r5" },
04301   { { "v3" }, "r6" },
04302   { { "v4" }, "r7" },
04303   { { "v5" }, "r8" },
04304   { { "v6", "rfp" }, "r9" },
04305   { { "sl" }, "r10" },
04306   { { "fp" }, "r11" },
04307   { { "ip" }, "r12" },
04308   { { "r13" }, "sp" },
04309   { { "r14" }, "lr" },
04310   { { "r15" }, "pc" },
04311   // The S, D and Q registers overlap, but aren't really aliases; we
04312   // don't want to substitute one of these for a different-sized one.
04313 };
04314 
04315 void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
04316                                        unsigned &NumAliases) const {
04317   Aliases = GCCRegAliases;
04318   NumAliases = llvm::array_lengthof(GCCRegAliases);
04319 }
04320 
04321 const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
04322 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
04323 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
04324                                               ALL_LANGUAGES },
04325 #include "clang/Basic/BuiltinsNEON.def"
04326 
04327 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
04328 #define LANGBUILTIN(ID, TYPE, ATTRS, LANG) { #ID, TYPE, ATTRS, 0, LANG },
04329 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
04330                                               ALL_LANGUAGES },
04331 #include "clang/Basic/BuiltinsARM.def"
04332 };
04333 
04334 class ARMleTargetInfo : public ARMTargetInfo {
04335 public:
04336   ARMleTargetInfo(const llvm::Triple &Triple)
04337     : ARMTargetInfo(Triple, false) { }
04338   virtual void getTargetDefines(const LangOptions &Opts,
04339                                 MacroBuilder &Builder) const {
04340     Builder.defineMacro("__ARMEL__");
04341     ARMTargetInfo::getTargetDefines(Opts, Builder);
04342   }
04343 };
04344 
04345 class ARMbeTargetInfo : public ARMTargetInfo {
04346 public:
04347   ARMbeTargetInfo(const llvm::Triple &Triple)
04348     : ARMTargetInfo(Triple, true) { }
04349   virtual void getTargetDefines(const LangOptions &Opts,
04350                                 MacroBuilder &Builder) const {
04351     Builder.defineMacro("__ARMEB__");
04352     Builder.defineMacro("__ARM_BIG_ENDIAN");
04353     ARMTargetInfo::getTargetDefines(Opts, Builder);
04354   }
04355 };
04356 } // end anonymous namespace.
04357 
04358 namespace {
04359 class WindowsARMTargetInfo : public WindowsTargetInfo<ARMleTargetInfo> {
04360   const llvm::Triple Triple;
04361 public:
04362   WindowsARMTargetInfo(const llvm::Triple &Triple)
04363     : WindowsTargetInfo<ARMleTargetInfo>(Triple), Triple(Triple) {
04364     TLSSupported = false;
04365     WCharType = UnsignedShort;
04366     SizeType = UnsignedInt;
04367     UserLabelPrefix = "";
04368   }
04369   void getVisualStudioDefines(const LangOptions &Opts,
04370                               MacroBuilder &Builder) const {
04371     WindowsTargetInfo<ARMleTargetInfo>::getVisualStudioDefines(Opts, Builder);
04372 
04373     // FIXME: this is invalid for WindowsCE
04374     Builder.defineMacro("_M_ARM_NT", "1");
04375     Builder.defineMacro("_M_ARMT", "_M_ARM");
04376     Builder.defineMacro("_M_THUMB", "_M_ARM");
04377 
04378     assert((Triple.getArch() == llvm::Triple::arm ||
04379             Triple.getArch() == llvm::Triple::thumb) &&
04380            "invalid architecture for Windows ARM target info");
04381     unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
04382     Builder.defineMacro("_M_ARM", Triple.getArchName().substr(Offset));
04383 
04384     // TODO map the complete set of values
04385     // 31: VFPv3 40: VFPv4
04386     Builder.defineMacro("_M_ARM_FP", "31");
04387   }
04388   BuiltinVaListKind getBuiltinVaListKind() const override {
04389     return TargetInfo::CharPtrBuiltinVaList;
04390   }
04391 };
04392 
04393 // Windows ARM + Itanium C++ ABI Target
04394 class ItaniumWindowsARMleTargetInfo : public WindowsARMTargetInfo {
04395 public:
04396   ItaniumWindowsARMleTargetInfo(const llvm::Triple &Triple)
04397     : WindowsARMTargetInfo(Triple) {
04398     TheCXXABI.set(TargetCXXABI::GenericARM);
04399   }
04400 
04401   void getTargetDefines(const LangOptions &Opts,
04402                         MacroBuilder &Builder) const override {
04403     WindowsARMTargetInfo::getTargetDefines(Opts, Builder);
04404 
04405     if (Opts.MSVCCompat)
04406       WindowsARMTargetInfo::getVisualStudioDefines(Opts, Builder);
04407   }
04408 };
04409 
04410 // Windows ARM, MS (C++) ABI
04411 class MicrosoftARMleTargetInfo : public WindowsARMTargetInfo {
04412 public:
04413   MicrosoftARMleTargetInfo(const llvm::Triple &Triple)
04414     : WindowsARMTargetInfo(Triple) {
04415     TheCXXABI.set(TargetCXXABI::Microsoft);
04416   }
04417 
04418   void getTargetDefines(const LangOptions &Opts,
04419                         MacroBuilder &Builder) const override {
04420     WindowsARMTargetInfo::getTargetDefines(Opts, Builder);
04421     WindowsARMTargetInfo::getVisualStudioDefines(Opts, Builder);
04422   }
04423 };
04424 }
04425 
04426 
04427 namespace {
04428 class DarwinARMTargetInfo :
04429   public DarwinTargetInfo<ARMleTargetInfo> {
04430 protected:
04431   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
04432                     MacroBuilder &Builder) const override {
04433     getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
04434   }
04435 
04436 public:
04437   DarwinARMTargetInfo(const llvm::Triple &Triple)
04438       : DarwinTargetInfo<ARMleTargetInfo>(Triple) {
04439     HasAlignMac68kSupport = true;
04440     // iOS always has 64-bit atomic instructions.
04441     // FIXME: This should be based off of the target features in ARMleTargetInfo.
04442     MaxAtomicInlineWidth = 64;
04443 
04444     // Darwin on iOS uses a variant of the ARM C++ ABI.
04445     TheCXXABI.set(TargetCXXABI::iOS);
04446   }
04447 };
04448 } // end anonymous namespace.
04449 
04450 
04451 namespace {
04452 class AArch64TargetInfo : public TargetInfo {
04453   virtual void setDescriptionString() = 0;
04454   static const TargetInfo::GCCRegAlias GCCRegAliases[];
04455   static const char *const GCCRegNames[];
04456 
04457   enum FPUModeEnum {
04458     FPUMode,
04459     NeonMode
04460   };
04461 
04462   unsigned FPU;
04463   unsigned CRC;
04464   unsigned Crypto;
04465 
04466   static const Builtin::Info BuiltinInfo[];
04467 
04468   std::string ABI;
04469 
04470 public:
04471   AArch64TargetInfo(const llvm::Triple &Triple)
04472       : TargetInfo(Triple), ABI("aapcs") {
04473 
04474     if (getTriple().getOS() == llvm::Triple::NetBSD) {
04475       WCharType = SignedInt;
04476 
04477       // NetBSD apparently prefers consistency across ARM targets to consistency
04478       // across 64-bit targets.
04479       Int64Type = SignedLongLong;
04480       IntMaxType = SignedLongLong;
04481     } else {
04482       WCharType = UnsignedInt;
04483       Int64Type = SignedLong;
04484       IntMaxType = SignedLong;
04485     }
04486 
04487     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
04488     MaxVectorAlign = 128;
04489     RegParmMax = 8;
04490     MaxAtomicInlineWidth = 128;
04491     MaxAtomicPromoteWidth = 128;
04492 
04493     LongDoubleWidth = LongDoubleAlign = 128;
04494     LongDoubleFormat = &llvm::APFloat::IEEEquad;
04495 
04496     // {} in inline assembly are neon specifiers, not assembly variant
04497     // specifiers.
04498     NoAsmVariants = true;
04499 
04500     // AArch64 targets default to using the ARM C++ ABI.
04501     TheCXXABI.set(TargetCXXABI::GenericAArch64);
04502   }
04503 
04504   StringRef getABI() const override { return ABI; }
04505   bool setABI(const std::string &Name) override {
04506     if (Name != "aapcs" && Name != "darwinpcs")
04507       return false;
04508 
04509     ABI = Name;
04510     return true;
04511   }
04512 
04513   bool setCPU(const std::string &Name) override {
04514     bool CPUKnown = llvm::StringSwitch<bool>(Name)
04515                         .Case("generic", true)
04516                         .Cases("cortex-a53", "cortex-a57", true)
04517                         .Case("cyclone", true)
04518                         .Default(false);
04519     return CPUKnown;
04520   }
04521 
04522   virtual void getTargetDefines(const LangOptions &Opts,
04523                                 MacroBuilder &Builder) const  override {
04524     // Target identification.
04525     Builder.defineMacro("__aarch64__");
04526 
04527     // Target properties.
04528     Builder.defineMacro("_LP64");
04529     Builder.defineMacro("__LP64__");
04530 
04531     // ACLE predefines. Many can only have one possible value on v8 AArch64.
04532     Builder.defineMacro("__ARM_ACLE", "200");
04533     Builder.defineMacro("__ARM_ARCH", "8");
04534     Builder.defineMacro("__ARM_ARCH_PROFILE", "'A'");
04535 
04536     Builder.defineMacro("__ARM_64BIT_STATE");
04537     Builder.defineMacro("__ARM_PCS_AAPCS64");
04538     Builder.defineMacro("__ARM_ARCH_ISA_A64");
04539 
04540     Builder.defineMacro("__ARM_FEATURE_UNALIGNED");
04541     Builder.defineMacro("__ARM_FEATURE_CLZ");
04542     Builder.defineMacro("__ARM_FEATURE_FMA");
04543     Builder.defineMacro("__ARM_FEATURE_DIV");
04544     Builder.defineMacro("__ARM_FEATURE_IDIV"); // As specified in ACLE
04545     Builder.defineMacro("__ARM_FEATURE_DIV");  // For backwards compatibility
04546     Builder.defineMacro("__ARM_FEATURE_NUMERIC_MAXMIN");
04547     Builder.defineMacro("__ARM_FEATURE_DIRECTED_ROUNDING");
04548 
04549     Builder.defineMacro("__ARM_ALIGN_MAX_STACK_PWR", "4");
04550 
04551     // 0xe implies support for half, single and double precision operations.
04552     Builder.defineMacro("__ARM_FP", "0xe");
04553 
04554     // PCS specifies this for SysV variants, which is all we support. Other ABIs
04555     // may choose __ARM_FP16_FORMAT_ALTERNATIVE.
04556     Builder.defineMacro("__ARM_FP16_FORMAT_IEEE");
04557 
04558     if (Opts.FastMath || Opts.FiniteMathOnly)
04559       Builder.defineMacro("__ARM_FP_FAST");
04560 
04561     if (Opts.C99 && !Opts.Freestanding)
04562       Builder.defineMacro("__ARM_FP_FENV_ROUNDING");
04563 
04564     Builder.defineMacro("__ARM_SIZEOF_WCHAR_T", Opts.ShortWChar ? "2" : "4");
04565 
04566     Builder.defineMacro("__ARM_SIZEOF_MINIMAL_ENUM",
04567                         Opts.ShortEnums ? "1" : "4");
04568 
04569     if (FPU == NeonMode) {
04570       Builder.defineMacro("__ARM_NEON");
04571       // 64-bit NEON supports half, single and double precision operations.
04572       Builder.defineMacro("__ARM_NEON_FP", "0xe");
04573     }
04574 
04575     if (CRC)
04576       Builder.defineMacro("__ARM_FEATURE_CRC32");
04577 
04578     if (Crypto)
04579       Builder.defineMacro("__ARM_FEATURE_CRYPTO");
04580   }
04581 
04582   virtual void getTargetBuiltins(const Builtin::Info *&Records,
04583                                  unsigned &NumRecords) const override {
04584     Records = BuiltinInfo;
04585     NumRecords = clang::AArch64::LastTSBuiltin - Builtin::FirstTSBuiltin;
04586   }
04587 
04588   bool hasFeature(StringRef Feature) const override {
04589     return Feature == "aarch64" ||
04590       Feature == "arm64" ||
04591       (Feature == "neon" && FPU == NeonMode);
04592   }
04593 
04594   bool handleTargetFeatures(std::vector<std::string> &Features,
04595                             DiagnosticsEngine &Diags) override {
04596     FPU = FPUMode;
04597     CRC = 0;
04598     Crypto = 0;
04599     for (unsigned i = 0, e = Features.size(); i != e; ++i) {
04600       if (Features[i] == "+neon")
04601         FPU = NeonMode;
04602       if (Features[i] == "+crc")
04603         CRC = 1;
04604       if (Features[i] == "+crypto")
04605         Crypto = 1;
04606     }
04607 
04608     setDescriptionString();
04609 
04610     return true;
04611   }
04612 
04613   bool isCLZForZeroUndef() const override { return false; }
04614 
04615   BuiltinVaListKind getBuiltinVaListKind() const override {
04616     return TargetInfo::AArch64ABIBuiltinVaList;
04617   }
04618 
04619   virtual void getGCCRegNames(const char *const *&Names,
04620                               unsigned &NumNames) const override;
04621   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
04622                                 unsigned &NumAliases) const override;
04623 
04624   virtual bool validateAsmConstraint(const char *&Name,
04625                                      TargetInfo::ConstraintInfo &Info) const override {
04626     switch (*Name) {
04627     default:
04628       return false;
04629     case 'w': // Floating point and SIMD registers (V0-V31)
04630       Info.setAllowsRegister();
04631       return true;
04632     case 'I': // Constant that can be used with an ADD instruction
04633     case 'J': // Constant that can be used with a SUB instruction
04634     case 'K': // Constant that can be used with a 32-bit logical instruction
04635     case 'L': // Constant that can be used with a 64-bit logical instruction
04636     case 'M': // Constant that can be used as a 32-bit MOV immediate
04637     case 'N': // Constant that can be used as a 64-bit MOV immediate
04638     case 'Y': // Floating point constant zero
04639     case 'Z': // Integer constant zero
04640       return true;
04641     case 'Q': // A memory reference with base register and no offset
04642       Info.setAllowsMemory();
04643       return true;
04644     case 'S': // A symbolic address
04645       Info.setAllowsRegister();
04646       return true;
04647     case 'U':
04648       // Ump: A memory address suitable for ldp/stp in SI, DI, SF and DF modes, whatever they may be
04649       // Utf: A memory address suitable for ldp/stp in TF mode, whatever it may be
04650       // Usa: An absolute symbolic address
04651       // Ush: The high part (bits 32:12) of a pc-relative symbolic address
04652       llvm_unreachable("FIXME: Unimplemented support for bizarre constraints");
04653     case 'z': // Zero register, wzr or xzr
04654       Info.setAllowsRegister();
04655       return true;
04656     case 'x': // Floating point and SIMD registers (V0-V15)
04657       Info.setAllowsRegister();
04658       return true;
04659     }
04660     return false;
04661   }
04662 
04663   bool
04664   validateConstraintModifier(StringRef Constraint, char Modifier, unsigned Size,
04665                              std::string &SuggestedModifier) const override {
04666     // Strip off constraint modifiers.
04667     while (Constraint[0] == '=' || Constraint[0] == '+' || Constraint[0] == '&')
04668       Constraint = Constraint.substr(1);
04669 
04670     switch (Constraint[0]) {
04671     default:
04672       return true;
04673     case 'z':
04674     case 'r': {
04675       switch (Modifier) {
04676       case 'x':
04677       case 'w':
04678         // For now assume that the person knows what they're
04679         // doing with the modifier.
04680         return true;
04681       default:
04682         // By default an 'r' constraint will be in the 'x'
04683         // registers.
04684         if (Size == 64)
04685           return true;
04686 
04687         SuggestedModifier = "w";
04688         return false;
04689       }
04690     }
04691     }
04692   }
04693 
04694   const char *getClobbers() const override { return ""; }
04695 
04696   int getEHDataRegisterNumber(unsigned RegNo) const override {
04697     if (RegNo == 0)
04698       return 0;
04699     if (RegNo == 1)
04700       return 1;
04701     return -1;
04702   }
04703 };
04704 
04705 const char *const AArch64TargetInfo::GCCRegNames[] = {
04706   // 32-bit Integer registers
04707   "w0",  "w1",  "w2",  "w3",  "w4",  "w5",  "w6",  "w7",  "w8",  "w9",  "w10",
04708   "w11", "w12", "w13", "w14", "w15", "w16", "w17", "w18", "w19", "w20", "w21",
04709   "w22", "w23", "w24", "w25", "w26", "w27", "w28", "w29", "w30", "wsp",
04710 
04711   // 64-bit Integer registers
04712   "x0",  "x1",  "x2",  "x3",  "x4",  "x5",  "x6",  "x7",  "x8",  "x9",  "x10",
04713   "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21",
04714   "x22", "x23", "x24", "x25", "x26", "x27", "x28", "fp",  "lr",  "sp",
04715 
04716   // 32-bit floating point regsisters
04717   "s0",  "s1",  "s2",  "s3",  "s4",  "s5",  "s6",  "s7",  "s8",  "s9",  "s10",
04718   "s11", "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20", "s21",
04719   "s22", "s23", "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
04720 
04721   // 64-bit floating point regsisters
04722   "d0",  "d1",  "d2",  "d3",  "d4",  "d5",  "d6",  "d7",  "d8",  "d9",  "d10",
04723   "d11", "d12", "d13", "d14", "d15", "d16", "d17", "d18", "d19", "d20", "d21",
04724   "d22", "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
04725 
04726   // Vector registers
04727   "v0",  "v1",  "v2",  "v3",  "v4",  "v5",  "v6",  "v7",  "v8",  "v9",  "v10",
04728   "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21",
04729   "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31"
04730 };
04731 
04732 void AArch64TargetInfo::getGCCRegNames(const char *const *&Names,
04733                                      unsigned &NumNames) const {
04734   Names = GCCRegNames;
04735   NumNames = llvm::array_lengthof(GCCRegNames);
04736 }
04737 
04738 const TargetInfo::GCCRegAlias AArch64TargetInfo::GCCRegAliases[] = {
04739   { { "w31" }, "wsp" },
04740   { { "x29" }, "fp" },
04741   { { "x30" }, "lr" },
04742   { { "x31" }, "sp" },
04743   // The S/D/Q and W/X registers overlap, but aren't really aliases; we
04744   // don't want to substitute one of these for a different-sized one.
04745 };
04746 
04747 void AArch64TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
04748                                        unsigned &NumAliases) const {
04749   Aliases = GCCRegAliases;
04750   NumAliases = llvm::array_lengthof(GCCRegAliases);
04751 }
04752 
04753 const Builtin::Info AArch64TargetInfo::BuiltinInfo[] = {
04754 #define BUILTIN(ID, TYPE, ATTRS)                                               \
04755   { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
04756 #include "clang/Basic/BuiltinsNEON.def"
04757 
04758 #define BUILTIN(ID, TYPE, ATTRS)                                               \
04759   { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
04760 #include "clang/Basic/BuiltinsAArch64.def"
04761 };
04762 
04763 class AArch64leTargetInfo : public AArch64TargetInfo {
04764   void setDescriptionString() override {
04765     if (getTriple().isOSBinFormatMachO())
04766       DescriptionString = "e-m:o-i64:64-i128:128-n32:64-S128";
04767     else
04768       DescriptionString = "e-m:e-i64:64-i128:128-n32:64-S128";
04769   }
04770 
04771 public:
04772   AArch64leTargetInfo(const llvm::Triple &Triple)
04773     : AArch64TargetInfo(Triple) {
04774     BigEndian = false;
04775     }
04776   void getTargetDefines(const LangOptions &Opts,
04777                         MacroBuilder &Builder) const override {
04778     Builder.defineMacro("__AARCH64EL__");
04779     AArch64TargetInfo::getTargetDefines(Opts, Builder);
04780   }
04781 };
04782 
04783 class AArch64beTargetInfo : public AArch64TargetInfo {
04784   void setDescriptionString() override {
04785     assert(!getTriple().isOSBinFormatMachO());
04786     DescriptionString = "E-m:e-i64:64-i128:128-n32:64-S128";
04787   }
04788 
04789 public:
04790   AArch64beTargetInfo(const llvm::Triple &Triple)
04791     : AArch64TargetInfo(Triple) { }
04792   void getTargetDefines(const LangOptions &Opts,
04793                         MacroBuilder &Builder) const override {
04794     Builder.defineMacro("__AARCH64EB__");
04795     Builder.defineMacro("__AARCH_BIG_ENDIAN");
04796     Builder.defineMacro("__ARM_BIG_ENDIAN");
04797     AArch64TargetInfo::getTargetDefines(Opts, Builder);
04798   }
04799 };
04800 } // end anonymous namespace.
04801 
04802 namespace {
04803 class DarwinAArch64TargetInfo : public DarwinTargetInfo<AArch64leTargetInfo> {
04804 protected:
04805   void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
04806                     MacroBuilder &Builder) const override {
04807     Builder.defineMacro("__AARCH64_SIMD__");
04808     Builder.defineMacro("__ARM64_ARCH_8__");
04809     Builder.defineMacro("__ARM_NEON__");
04810     Builder.defineMacro("__LITTLE_ENDIAN__");
04811     Builder.defineMacro("__REGISTER_PREFIX__", "");
04812     Builder.defineMacro("__arm64", "1");
04813     Builder.defineMacro("__arm64__", "1");
04814 
04815     getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
04816   }
04817 
04818 public:
04819   DarwinAArch64TargetInfo(const llvm::Triple &Triple)
04820       : DarwinTargetInfo<AArch64leTargetInfo>(Triple) {
04821     Int64Type = SignedLongLong;
04822     WCharType = SignedInt;
04823     UseSignedCharForObjCBool = false;
04824 
04825     LongDoubleWidth = LongDoubleAlign = 64;
04826     LongDoubleFormat = &llvm::APFloat::IEEEdouble;
04827 
04828     TheCXXABI.set(TargetCXXABI::iOS64);
04829   }
04830 
04831   BuiltinVaListKind getBuiltinVaListKind() const override {
04832     return TargetInfo::CharPtrBuiltinVaList;
04833   }
04834 };
04835 } // end anonymous namespace
04836 
04837 namespace {
04838 // Hexagon abstract base class
04839 class HexagonTargetInfo : public TargetInfo {
04840   static const Builtin::Info BuiltinInfo[];
04841   static const char * const GCCRegNames[];
04842   static const TargetInfo::GCCRegAlias GCCRegAliases[];
04843   std::string CPU;
04844 public:
04845   HexagonTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
04846     BigEndian = false;
04847     DescriptionString = "e-m:e-p:32:32-i1:32-i64:64-a:0-n32";
04848 
04849     // {} in inline assembly are packet specifiers, not assembly variant
04850     // specifiers.
04851     NoAsmVariants = true;
04852   }
04853 
04854   void getTargetBuiltins(const Builtin::Info *&Records,
04855                          unsigned &NumRecords) const override {
04856     Records = BuiltinInfo;
04857     NumRecords = clang::Hexagon::LastTSBuiltin-Builtin::FirstTSBuiltin;
04858   }
04859 
04860   bool validateAsmConstraint(const char *&Name,
04861                              TargetInfo::ConstraintInfo &Info) const override {
04862     return true;
04863   }
04864 
04865   void getTargetDefines(const LangOptions &Opts,
04866                         MacroBuilder &Builder) const override;
04867 
04868   bool hasFeature(StringRef Feature) const override {
04869     return Feature == "hexagon";
04870   }
04871 
04872   BuiltinVaListKind getBuiltinVaListKind() const override {
04873     return TargetInfo::CharPtrBuiltinVaList;
04874   }
04875   void getGCCRegNames(const char * const *&Names,
04876                       unsigned &NumNames) const override;
04877   void getGCCRegAliases(const GCCRegAlias *&Aliases,
04878                         unsigned &NumAliases) const override;
04879   const char *getClobbers() const override {
04880     return "";
04881   }
04882 
04883   static const char *getHexagonCPUSuffix(StringRef Name) {
04884     return llvm::StringSwitch<const char*>(Name)
04885       .Case("hexagonv4", "4")
04886       .Case("hexagonv5", "5")
04887       .Default(nullptr);
04888   }
04889 
04890   bool setCPU(const std::string &Name) override {
04891     if (!getHexagonCPUSuffix(Name))
04892       return false;
04893 
04894     CPU = Name;
04895     return true;
04896   }
04897 };
04898 
04899 void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts,
04900                                 MacroBuilder &Builder) const {
04901   Builder.defineMacro("qdsp6");
04902   Builder.defineMacro("__qdsp6", "1");
04903   Builder.defineMacro("__qdsp6__", "1");
04904 
04905   Builder.defineMacro("hexagon");
04906   Builder.defineMacro("__hexagon", "1");
04907   Builder.defineMacro("__hexagon__", "1");
04908 
04909   if(CPU == "hexagonv1") {
04910     Builder.defineMacro("__HEXAGON_V1__");
04911     Builder.defineMacro("__HEXAGON_ARCH__", "1");
04912     if(Opts.HexagonQdsp6Compat) {
04913       Builder.defineMacro("__QDSP6_V1__");
04914       Builder.defineMacro("__QDSP6_ARCH__", "1");
04915     }
04916   }
04917   else if(CPU == "hexagonv2") {
04918     Builder.defineMacro("__HEXAGON_V2__");
04919     Builder.defineMacro("__HEXAGON_ARCH__", "2");
04920     if(Opts.HexagonQdsp6Compat) {
04921       Builder.defineMacro("__QDSP6_V2__");
04922       Builder.defineMacro("__QDSP6_ARCH__", "2");
04923     }
04924   }
04925   else if(CPU == "hexagonv3") {
04926     Builder.defineMacro("__HEXAGON_V3__");
04927     Builder.defineMacro("__HEXAGON_ARCH__", "3");
04928     if(Opts.HexagonQdsp6Compat) {
04929       Builder.defineMacro("__QDSP6_V3__");
04930       Builder.defineMacro("__QDSP6_ARCH__", "3");
04931     }
04932   }
04933   else if(CPU == "hexagonv4") {
04934     Builder.defineMacro("__HEXAGON_V4__");
04935     Builder.defineMacro("__HEXAGON_ARCH__", "4");
04936     if(Opts.HexagonQdsp6Compat) {
04937       Builder.defineMacro("__QDSP6_V4__");
04938       Builder.defineMacro("__QDSP6_ARCH__", "4");
04939     }
04940   }
04941   else if(CPU == "hexagonv5") {
04942     Builder.defineMacro("__HEXAGON_V5__");
04943     Builder.defineMacro("__HEXAGON_ARCH__", "5");
04944     if(Opts.HexagonQdsp6Compat) {
04945       Builder.defineMacro("__QDSP6_V5__");
04946       Builder.defineMacro("__QDSP6_ARCH__", "5");
04947     }
04948   }
04949 }
04950 
04951 const char * const HexagonTargetInfo::GCCRegNames[] = {
04952   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
04953   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
04954   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
04955   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
04956   "p0", "p1", "p2", "p3",
04957   "sa0", "lc0", "sa1", "lc1", "m0", "m1", "usr", "ugp"
04958 };
04959 
04960 void HexagonTargetInfo::getGCCRegNames(const char * const *&Names,
04961                                    unsigned &NumNames) const {
04962   Names = GCCRegNames;
04963   NumNames = llvm::array_lengthof(GCCRegNames);
04964 }
04965 
04966 
04967 const TargetInfo::GCCRegAlias HexagonTargetInfo::GCCRegAliases[] = {
04968   { { "sp" }, "r29" },
04969   { { "fp" }, "r30" },
04970   { { "lr" }, "r31" },
04971  };
04972 
04973 void HexagonTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
04974                                      unsigned &NumAliases) const {
04975   Aliases = GCCRegAliases;
04976   NumAliases = llvm::array_lengthof(GCCRegAliases);
04977 }
04978 
04979 
04980 const Builtin::Info HexagonTargetInfo::BuiltinInfo[] = {
04981 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
04982 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
04983                                               ALL_LANGUAGES },
04984 #include "clang/Basic/BuiltinsHexagon.def"
04985 };
04986 }
04987 
04988 
04989 namespace {
04990 // Shared base class for SPARC v8 (32-bit) and SPARC v9 (64-bit).
04991 class SparcTargetInfo : public TargetInfo {
04992   static const TargetInfo::GCCRegAlias GCCRegAliases[];
04993   static const char * const GCCRegNames[];
04994   bool SoftFloat;
04995 public:
04996   SparcTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {}
04997 
04998   bool handleTargetFeatures(std::vector<std::string> &Features,
04999                             DiagnosticsEngine &Diags) override {
05000     SoftFloat = false;
05001     for (unsigned i = 0, e = Features.size(); i != e; ++i)
05002       if (Features[i] == "+soft-float")
05003         SoftFloat = true;
05004     return true;
05005   }
05006   void getTargetDefines(const LangOptions &Opts,
05007                         MacroBuilder &Builder) const override {
05008     DefineStd(Builder, "sparc", Opts);
05009     Builder.defineMacro("__REGISTER_PREFIX__", "");
05010 
05011     if (SoftFloat)
05012       Builder.defineMacro("SOFT_FLOAT", "1");
05013   }
05014 
05015   bool hasFeature(StringRef Feature) const override {
05016     return llvm::StringSwitch<bool>(Feature)
05017              .Case("softfloat", SoftFloat)
05018              .Case("sparc", true)
05019              .Default(false);
05020   }
05021 
05022   void getTargetBuiltins(const Builtin::Info *&Records,
05023                          unsigned &NumRecords) const override {
05024     // FIXME: Implement!
05025   }
05026   BuiltinVaListKind getBuiltinVaListKind() const override {
05027     return TargetInfo::VoidPtrBuiltinVaList;
05028   }
05029   void getGCCRegNames(const char * const *&Names,
05030                       unsigned &NumNames) const override;
05031   void getGCCRegAliases(const GCCRegAlias *&Aliases,
05032                         unsigned &NumAliases) const override;
05033   bool validateAsmConstraint(const char *&Name,
05034                              TargetInfo::ConstraintInfo &info) const override {
05035     // FIXME: Implement!
05036     return false;
05037   }
05038   const char *getClobbers() const override {
05039     // FIXME: Implement!
05040     return "";
05041   }
05042 };
05043 
05044 const char * const SparcTargetInfo::GCCRegNames[] = {
05045   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
05046   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
05047   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
05048   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
05049 };
05050 
05051 void SparcTargetInfo::getGCCRegNames(const char * const *&Names,
05052                                      unsigned &NumNames) const {
05053   Names = GCCRegNames;
05054   NumNames = llvm::array_lengthof(GCCRegNames);
05055 }
05056 
05057 const TargetInfo::GCCRegAlias SparcTargetInfo::GCCRegAliases[] = {
05058   { { "g0" }, "r0" },
05059   { { "g1" }, "r1" },
05060   { { "g2" }, "r2" },
05061   { { "g3" }, "r3" },
05062   { { "g4" }, "r4" },
05063   { { "g5" }, "r5" },
05064   { { "g6" }, "r6" },
05065   { { "g7" }, "r7" },
05066   { { "o0" }, "r8" },
05067   { { "o1" }, "r9" },
05068   { { "o2" }, "r10" },
05069   { { "o3" }, "r11" },
05070   { { "o4" }, "r12" },
05071   { { "o5" }, "r13" },
05072   { { "o6", "sp" }, "r14" },
05073   { { "o7" }, "r15" },
05074   { { "l0" }, "r16" },
05075   { { "l1" }, "r17" },
05076   { { "l2" }, "r18" },
05077   { { "l3" }, "r19" },
05078   { { "l4" }, "r20" },
05079   { { "l5" }, "r21" },
05080   { { "l6" }, "r22" },
05081   { { "l7" }, "r23" },
05082   { { "i0" }, "r24" },
05083   { { "i1" }, "r25" },
05084   { { "i2" }, "r26" },
05085   { { "i3" }, "r27" },
05086   { { "i4" }, "r28" },
05087   { { "i5" }, "r29" },
05088   { { "i6", "fp" }, "r30" },
05089   { { "i7" }, "r31" },
05090 };
05091 
05092 void SparcTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
05093                                        unsigned &NumAliases) const {
05094   Aliases = GCCRegAliases;
05095   NumAliases = llvm::array_lengthof(GCCRegAliases);
05096 }
05097 
05098 // SPARC v8 is the 32-bit mode selected by Triple::sparc.
05099 class SparcV8TargetInfo : public SparcTargetInfo {
05100 public:
05101   SparcV8TargetInfo(const llvm::Triple &Triple) : SparcTargetInfo(Triple) {
05102     DescriptionString = "E-m:e-p:32:32-i64:64-f128:64-n32-S64";
05103   }
05104 
05105   void getTargetDefines(const LangOptions &Opts,
05106                         MacroBuilder &Builder) const override {
05107     SparcTargetInfo::getTargetDefines(Opts, Builder);
05108     Builder.defineMacro("__sparcv8");
05109   }
05110 };
05111 
05112 // SPARC v9 is the 64-bit mode selected by Triple::sparcv9.
05113 class SparcV9TargetInfo : public SparcTargetInfo {
05114 public:
05115   SparcV9TargetInfo(const llvm::Triple &Triple) : SparcTargetInfo(Triple) {
05116     // FIXME: Support Sparc quad-precision long double?
05117     DescriptionString = "E-m:e-i64:64-n32:64-S128";
05118     // This is an LP64 platform.
05119     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
05120 
05121     // OpenBSD uses long long for int64_t and intmax_t.
05122     if (getTriple().getOS() == llvm::Triple::OpenBSD)
05123       IntMaxType = SignedLongLong;
05124     else
05125       IntMaxType = SignedLong;
05126     Int64Type = IntMaxType;
05127 
05128     // The SPARCv8 System V ABI has long double 128-bits in size, but 64-bit
05129     // aligned. The SPARCv9 SCD 2.4.1 says 16-byte aligned.
05130     LongDoubleWidth = 128;
05131     LongDoubleAlign = 128;
05132     LongDoubleFormat = &llvm::APFloat::IEEEquad;
05133     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
05134   }
05135 
05136   void getTargetDefines(const LangOptions &Opts,
05137                         MacroBuilder &Builder) const override {
05138     SparcTargetInfo::getTargetDefines(Opts, Builder);
05139     Builder.defineMacro("__sparcv9");
05140     Builder.defineMacro("__arch64__");
05141     // Solaris doesn't need these variants, but the BSDs do.
05142     if (getTriple().getOS() != llvm::Triple::Solaris) {
05143       Builder.defineMacro("__sparc64__");
05144       Builder.defineMacro("__sparc_v9__");
05145       Builder.defineMacro("__sparcv9__");
05146     }
05147   }
05148 
05149   bool setCPU(const std::string &Name) override {
05150     bool CPUKnown = llvm::StringSwitch<bool>(Name)
05151       .Case("v9", true)
05152       .Case("ultrasparc", true)
05153       .Case("ultrasparc3", true)
05154       .Case("niagara", true)
05155       .Case("niagara2", true)
05156       .Case("niagara3", true)
05157       .Case("niagara4", true)
05158       .Default(false);
05159 
05160     // No need to store the CPU yet.  There aren't any CPU-specific
05161     // macros to define.
05162     return CPUKnown;
05163   }
05164 };
05165 
05166 } // end anonymous namespace.
05167 
05168 namespace {
05169 class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
05170 public:
05171   SolarisSparcV8TargetInfo(const llvm::Triple &Triple)
05172       : SolarisTargetInfo<SparcV8TargetInfo>(Triple) {
05173     SizeType = UnsignedInt;
05174     PtrDiffType = SignedInt;
05175   }
05176 };
05177 } // end anonymous namespace.
05178 
05179 namespace {
05180 class SystemZTargetInfo : public TargetInfo {
05181   static const char *const GCCRegNames[];
05182 
05183 public:
05184   SystemZTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
05185     TLSSupported = true;
05186     IntWidth = IntAlign = 32;
05187     LongWidth = LongLongWidth = LongAlign = LongLongAlign = 64;
05188     PointerWidth = PointerAlign = 64;
05189     LongDoubleWidth = 128;
05190     LongDoubleAlign = 64;
05191     LongDoubleFormat = &llvm::APFloat::IEEEquad;
05192     MinGlobalAlign = 16;
05193     DescriptionString = "E-m:e-i1:8:16-i8:8:16-i64:64-f128:64-a:8:16-n32:64";
05194     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
05195   }
05196   void getTargetDefines(const LangOptions &Opts,
05197                         MacroBuilder &Builder) const override {
05198     Builder.defineMacro("__s390__");
05199     Builder.defineMacro("__s390x__");
05200     Builder.defineMacro("__zarch__");
05201     Builder.defineMacro("__LONG_DOUBLE_128__");
05202   }
05203   void getTargetBuiltins(const Builtin::Info *&Records,
05204                          unsigned &NumRecords) const override {
05205     // FIXME: Implement.
05206     Records = nullptr;
05207     NumRecords = 0;
05208   }
05209 
05210   void getGCCRegNames(const char *const *&Names,
05211                       unsigned &NumNames) const override;
05212   void getGCCRegAliases(const GCCRegAlias *&Aliases,
05213                         unsigned &NumAliases) const override {
05214     // No aliases.
05215     Aliases = nullptr;
05216     NumAliases = 0;
05217   }
05218   bool validateAsmConstraint(const char *&Name,
05219                              TargetInfo::ConstraintInfo &info) const override;
05220   const char *getClobbers() const override {
05221     // FIXME: Is this really right?
05222     return "";
05223   }
05224   BuiltinVaListKind getBuiltinVaListKind() const override {
05225     return TargetInfo::SystemZBuiltinVaList;
05226   }
05227   bool setCPU(const std::string &Name) override {
05228     bool CPUKnown = llvm::StringSwitch<bool>(Name)
05229       .Case("z10", true)
05230       .Case("z196", true)
05231       .Case("zEC12", true)
05232       .Default(false);
05233 
05234     // No need to store the CPU yet.  There aren't any CPU-specific
05235     // macros to define.
05236     return CPUKnown;
05237   }
05238 };
05239 
05240 const char *const SystemZTargetInfo::GCCRegNames[] = {
05241   "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
05242   "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
05243   "f0",  "f2",  "f4",  "f6",  "f1",  "f3",  "f5",  "f7",
05244   "f8",  "f10", "f12", "f14", "f9",  "f11", "f13", "f15"
05245 };
05246 
05247 void SystemZTargetInfo::getGCCRegNames(const char *const *&Names,
05248                                        unsigned &NumNames) const {
05249   Names = GCCRegNames;
05250   NumNames = llvm::array_lengthof(GCCRegNames);
05251 }
05252 
05253 bool SystemZTargetInfo::
05254 validateAsmConstraint(const char *&Name,
05255                       TargetInfo::ConstraintInfo &Info) const {
05256   switch (*Name) {
05257   default:
05258     return false;
05259 
05260   case 'a': // Address register
05261   case 'd': // Data register (equivalent to 'r')
05262   case 'f': // Floating-point register
05263     Info.setAllowsRegister();
05264     return true;
05265 
05266   case 'I': // Unsigned 8-bit constant
05267   case 'J': // Unsigned 12-bit constant
05268   case 'K': // Signed 16-bit constant
05269   case 'L': // Signed 20-bit displacement (on all targets we support)
05270   case 'M': // 0x7fffffff
05271     return true;
05272 
05273   case 'Q': // Memory with base and unsigned 12-bit displacement
05274   case 'R': // Likewise, plus an index
05275   case 'S': // Memory with base and signed 20-bit displacement
05276   case 'T': // Likewise, plus an index
05277     Info.setAllowsMemory();
05278     return true;
05279   }
05280 }
05281 }
05282 
05283 namespace {
05284   class MSP430TargetInfo : public TargetInfo {
05285     static const char * const GCCRegNames[];
05286   public:
05287     MSP430TargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
05288       BigEndian = false;
05289       TLSSupported = false;
05290       IntWidth = 16; IntAlign = 16;
05291       LongWidth = 32; LongLongWidth = 64;
05292       LongAlign = LongLongAlign = 16;
05293       PointerWidth = 16; PointerAlign = 16;
05294       SuitableAlign = 16;
05295       SizeType = UnsignedInt;
05296       IntMaxType = SignedLongLong;
05297       IntPtrType = SignedInt;
05298       PtrDiffType = SignedInt;
05299       SigAtomicType = SignedLong;
05300       DescriptionString = "e-m:e-p:16:16-i32:16:32-a:16-n8:16";
05301     }
05302     void getTargetDefines(const LangOptions &Opts,
05303                           MacroBuilder &Builder) const override {
05304       Builder.defineMacro("MSP430");
05305       Builder.defineMacro("__MSP430__");
05306       // FIXME: defines for different 'flavours' of MCU
05307     }
05308     void getTargetBuiltins(const Builtin::Info *&Records,
05309                            unsigned &NumRecords) const override {
05310       // FIXME: Implement.
05311       Records = nullptr;
05312       NumRecords = 0;
05313     }
05314     bool hasFeature(StringRef Feature) const override {
05315       return Feature == "msp430";
05316     }
05317     void getGCCRegNames(const char * const *&Names,
05318                         unsigned &NumNames) const override;
05319     void getGCCRegAliases(const GCCRegAlias *&Aliases,
05320                           unsigned &NumAliases) const override {
05321       // No aliases.
05322       Aliases = nullptr;
05323       NumAliases = 0;
05324     }
05325     bool validateAsmConstraint(const char *&Name,
05326                                TargetInfo::ConstraintInfo &info) const override {
05327       // No target constraints for now.
05328       return false;
05329     }
05330     const char *getClobbers() const override {
05331       // FIXME: Is this really right?
05332       return "";
05333     }
05334     BuiltinVaListKind getBuiltinVaListKind() const override {
05335       // FIXME: implement
05336       return TargetInfo::CharPtrBuiltinVaList;
05337    }
05338   };
05339 
05340   const char * const MSP430TargetInfo::GCCRegNames[] = {
05341     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
05342     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
05343   };
05344 
05345   void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
05346                                         unsigned &NumNames) const {
05347     Names = GCCRegNames;
05348     NumNames = llvm::array_lengthof(GCCRegNames);
05349   }
05350 }
05351 
05352 namespace {
05353 
05354   // LLVM and Clang cannot be used directly to output native binaries for
05355   // target, but is used to compile C code to llvm bitcode with correct
05356   // type and alignment information.
05357   //
05358   // TCE uses the llvm bitcode as input and uses it for generating customized
05359   // target processor and program binary. TCE co-design environment is
05360   // publicly available in http://tce.cs.tut.fi
05361 
05362   static const unsigned TCEOpenCLAddrSpaceMap[] = {
05363       3, // opencl_global
05364       4, // opencl_local
05365       5, // opencl_constant
05366       0, // cuda_device
05367       0, // cuda_constant
05368       0  // cuda_shared
05369   };
05370 
05371   class TCETargetInfo : public TargetInfo{
05372   public:
05373     TCETargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
05374       TLSSupported = false;
05375       IntWidth = 32;
05376       LongWidth = LongLongWidth = 32;
05377       PointerWidth = 32;
05378       IntAlign = 32;
05379       LongAlign = LongLongAlign = 32;
05380       PointerAlign = 32;
05381       SuitableAlign = 32;
05382       SizeType = UnsignedInt;
05383       IntMaxType = SignedLong;
05384       IntPtrType = SignedInt;
05385       PtrDiffType = SignedInt;
05386       FloatWidth = 32;
05387       FloatAlign = 32;
05388       DoubleWidth = 32;
05389       DoubleAlign = 32;
05390       LongDoubleWidth = 32;
05391       LongDoubleAlign = 32;
05392       FloatFormat = &llvm::APFloat::IEEEsingle;
05393       DoubleFormat = &llvm::APFloat::IEEEsingle;
05394       LongDoubleFormat = &llvm::APFloat::IEEEsingle;
05395       DescriptionString = "E-p:32:32-i8:8:32-i16:16:32-i64:32"
05396                           "-f64:32-v64:32-v128:32-a:0:32-n32";
05397       AddrSpaceMap = &TCEOpenCLAddrSpaceMap;
05398       UseAddrSpaceMapMangling = true;
05399     }
05400 
05401     void getTargetDefines(const LangOptions &Opts,
05402                           MacroBuilder &Builder) const override {
05403       DefineStd(Builder, "tce", Opts);
05404       Builder.defineMacro("__TCE__");
05405       Builder.defineMacro("__TCE_V1__");
05406     }
05407     bool hasFeature(StringRef Feature) const override {
05408       return Feature == "tce";
05409     }
05410 
05411     void getTargetBuiltins(const Builtin::Info *&Records,
05412                            unsigned &NumRecords) const override {}
05413     const char *getClobbers() const override {
05414       return "";
05415     }
05416     BuiltinVaListKind getBuiltinVaListKind() const override {
05417       return TargetInfo::VoidPtrBuiltinVaList;
05418     }
05419     void getGCCRegNames(const char * const *&Names,
05420                         unsigned &NumNames) const override {}
05421     bool validateAsmConstraint(const char *&Name,
05422                                TargetInfo::ConstraintInfo &info) const override{
05423       return true;
05424     }
05425     void getGCCRegAliases(const GCCRegAlias *&Aliases,
05426                           unsigned &NumAliases) const override {}
05427   };
05428 }
05429 
05430 namespace {
05431 class MipsTargetInfoBase : public TargetInfo {
05432   virtual void setDescriptionString() = 0;
05433 
05434   static const Builtin::Info BuiltinInfo[];
05435   std::string CPU;
05436   bool IsMips16;
05437   bool IsMicromips;
05438   bool IsNan2008;
05439   bool IsSingleFloat;
05440   enum MipsFloatABI {
05441     HardFloat, SoftFloat
05442   } FloatABI;
05443   enum DspRevEnum {
05444     NoDSP, DSP1, DSP2
05445   } DspRev;
05446   bool HasMSA;
05447 
05448 protected:
05449   bool HasFP64;
05450   std::string ABI;
05451 
05452 public:
05453   MipsTargetInfoBase(const llvm::Triple &Triple, const std::string &ABIStr,
05454                      const std::string &CPUStr)
05455       : TargetInfo(Triple), CPU(CPUStr), IsMips16(false), IsMicromips(false),
05456         IsNan2008(false), IsSingleFloat(false), FloatABI(HardFloat),
05457         DspRev(NoDSP), HasMSA(false), HasFP64(false), ABI(ABIStr) {}
05458 
05459   bool isNaN2008Default() const {
05460     return CPU == "mips32r6" || CPU == "mips64r6";
05461   }
05462 
05463   bool isFP64Default() const {
05464     return CPU == "mips32r6" || ABI == "n32" || ABI == "n64" || ABI == "64";
05465   }
05466 
05467   StringRef getABI() const override { return ABI; }
05468   bool setCPU(const std::string &Name) override {
05469     bool IsMips32 = getTriple().getArch() == llvm::Triple::mips ||
05470                     getTriple().getArch() == llvm::Triple::mipsel;
05471     CPU = Name;
05472     return llvm::StringSwitch<bool>(Name)
05473         .Case("mips1", IsMips32)
05474         .Case("mips2", IsMips32)
05475         .Case("mips3", true)
05476         .Case("mips4", true)
05477         .Case("mips5", true)
05478         .Case("mips32", IsMips32)
05479         .Case("mips32r2", IsMips32)
05480         .Case("mips32r6", IsMips32)
05481         .Case("mips64", true)
05482         .Case("mips64r2", true)
05483         .Case("mips64r6", true)
05484         .Case("octeon", true)
05485         .Default(false);
05486   }
05487   const std::string& getCPU() const { return CPU; }
05488   void getDefaultFeatures(llvm::StringMap<bool> &Features) const override {
05489     // The backend enables certain ABI's by default according to the
05490     // architecture.
05491     // Disable both possible defaults so that we don't end up with multiple
05492     // ABI's selected and trigger an assertion.
05493     Features["o32"] = false;
05494     Features["n64"] = false;
05495 
05496     Features[ABI] = true;
05497     if (CPU == "octeon")
05498       Features["mips64r2"] = Features["cnmips"] = true;
05499     else
05500       Features[CPU] = true;
05501   }
05502 
05503   void getTargetDefines(const LangOptions &Opts,
05504                         MacroBuilder &Builder) const override {
05505     Builder.defineMacro("__mips__");
05506     Builder.defineMacro("_mips");
05507     if (Opts.GNUMode)
05508       Builder.defineMacro("mips");
05509 
05510     Builder.defineMacro("__REGISTER_PREFIX__", "");
05511 
05512     switch (FloatABI) {
05513     case HardFloat:
05514       Builder.defineMacro("__mips_hard_float", Twine(1));
05515       break;
05516     case SoftFloat:
05517       Builder.defineMacro("__mips_soft_float", Twine(1));
05518       break;
05519     }
05520 
05521     if (IsSingleFloat)
05522       Builder.defineMacro("__mips_single_float", Twine(1));
05523 
05524     Builder.defineMacro("__mips_fpr", HasFP64 ? Twine(64) : Twine(32));
05525     Builder.defineMacro("_MIPS_FPSET",
05526                         Twine(32 / (HasFP64 || IsSingleFloat ? 1 : 2)));
05527 
05528     if (IsMips16)
05529       Builder.defineMacro("__mips16", Twine(1));
05530 
05531     if (IsMicromips)
05532       Builder.defineMacro("__mips_micromips", Twine(1));
05533 
05534     if (IsNan2008)
05535       Builder.defineMacro("__mips_nan2008", Twine(1));
05536 
05537     switch (DspRev) {
05538     default:
05539       break;
05540     case DSP1:
05541       Builder.defineMacro("__mips_dsp_rev", Twine(1));
05542       Builder.defineMacro("__mips_dsp", Twine(1));
05543       break;
05544     case DSP2:
05545       Builder.defineMacro("__mips_dsp_rev", Twine(2));
05546       Builder.defineMacro("__mips_dspr2", Twine(1));
05547       Builder.defineMacro("__mips_dsp", Twine(1));
05548       break;
05549     }
05550 
05551     if (HasMSA)
05552       Builder.defineMacro("__mips_msa", Twine(1));
05553 
05554     Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(0)));
05555     Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
05556     Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
05557 
05558     Builder.defineMacro("_MIPS_ARCH", "\"" + CPU + "\"");
05559     Builder.defineMacro("_MIPS_ARCH_" + StringRef(CPU).upper());
05560   }
05561 
05562   void getTargetBuiltins(const Builtin::Info *&Records,
05563                          unsigned &NumRecords) const override {
05564     Records = BuiltinInfo;
05565     NumRecords = clang::Mips::LastTSBuiltin - Builtin::FirstTSBuiltin;
05566   }
05567   bool hasFeature(StringRef Feature) const override {
05568     return llvm::StringSwitch<bool>(Feature)
05569       .Case("mips", true)
05570       .Case("fp64", HasFP64)
05571       .Default(false);
05572   }
05573   BuiltinVaListKind getBuiltinVaListKind() const override {
05574     return TargetInfo::VoidPtrBuiltinVaList;
05575   }
05576   void getGCCRegNames(const char * const *&Names,
05577                       unsigned &NumNames) const override {
05578     static const char *const GCCRegNames[] = {
05579       // CPU register names
05580       // Must match second column of GCCRegAliases
05581       "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
05582       "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
05583       "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
05584       "$24",  "$25",  "$26",  "$27",  "$28",  "$29",  "$30",  "$31",
05585       // Floating point register names
05586       "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
05587       "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
05588       "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
05589       "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
05590       // Hi/lo and condition register names
05591       "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
05592       "$fcc5","$fcc6","$fcc7",
05593       // MSA register names
05594       "$w0",  "$w1",  "$w2",  "$w3",  "$w4",  "$w5",  "$w6",  "$w7",
05595       "$w8",  "$w9",  "$w10", "$w11", "$w12", "$w13", "$w14", "$w15",
05596       "$w16", "$w17", "$w18", "$w19", "$w20", "$w21", "$w22", "$w23",
05597       "$w24", "$w25", "$w26", "$w27", "$w28", "$w29", "$w30", "$w31",
05598       // MSA control register names
05599       "$msair",      "$msacsr", "$msaaccess", "$msasave", "$msamodify",
05600       "$msarequest", "$msamap", "$msaunmap"
05601     };
05602     Names = GCCRegNames;
05603     NumNames = llvm::array_lengthof(GCCRegNames);
05604   }
05605   void getGCCRegAliases(const GCCRegAlias *&Aliases,
05606                         unsigned &NumAliases) const override = 0;
05607   bool validateAsmConstraint(const char *&Name,
05608                              TargetInfo::ConstraintInfo &Info) const override {
05609     switch (*Name) {
05610     default:
05611       return false;
05612         
05613     case 'r': // CPU registers.
05614     case 'd': // Equivalent to "r" unless generating MIPS16 code.
05615     case 'y': // Equivalent to "r", backward compatibility only.
05616     case 'f': // floating-point registers.
05617     case 'c': // $25 for indirect jumps
05618     case 'l': // lo register
05619     case 'x': // hilo register pair
05620       Info.setAllowsRegister();
05621       return true;
05622     case 'R': // An address that can be used in a non-macro load or store
05623       Info.setAllowsMemory();
05624       return true;
05625     }
05626   }
05627 
05628   const char *getClobbers() const override {
05629     // FIXME: Implement!
05630     return "";
05631   }
05632 
05633   bool handleTargetFeatures(std::vector<std::string> &Features,
05634                             DiagnosticsEngine &Diags) override {
05635     IsMips16 = false;
05636     IsMicromips = false;
05637     IsNan2008 = isNaN2008Default();
05638     IsSingleFloat = false;
05639     FloatABI = HardFloat;
05640     DspRev = NoDSP;
05641     HasFP64 = isFP64Default();
05642 
05643     for (std::vector<std::string>::iterator it = Features.begin(),
05644          ie = Features.end(); it != ie; ++it) {
05645       if (*it == "+single-float")
05646         IsSingleFloat = true;
05647       else if (*it == "+soft-float")
05648         FloatABI = SoftFloat;
05649       else if (*it == "+mips16")
05650         IsMips16 = true;
05651       else if (*it == "+micromips")
05652         IsMicromips = true;
05653       else if (*it == "+dsp")
05654         DspRev = std::max(DspRev, DSP1);
05655       else if (*it == "+dspr2")
05656         DspRev = std::max(DspRev, DSP2);
05657       else if (*it == "+msa")
05658         HasMSA = true;
05659       else if (*it == "+fp64")
05660         HasFP64 = true;
05661       else if (*it == "-fp64")
05662         HasFP64 = false;
05663       else if (*it == "+nan2008")
05664         IsNan2008 = true;
05665       else if (*it == "-nan2008")
05666         IsNan2008 = false;
05667     }
05668 
05669     // Remove front-end specific options.
05670     std::vector<std::string>::iterator it =
05671       std::find(Features.begin(), Features.end(), "+soft-float");
05672     if (it != Features.end())
05673       Features.erase(it);
05674 
05675     setDescriptionString();
05676 
05677     return true;
05678   }
05679 
05680   int getEHDataRegisterNumber(unsigned RegNo) const override {
05681     if (RegNo == 0) return 4;
05682     if (RegNo == 1) return 5;
05683     return -1;
05684   }
05685 
05686   bool isCLZForZeroUndef() const override { return false; }
05687 };
05688 
05689 const Builtin::Info MipsTargetInfoBase::BuiltinInfo[] = {
05690 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
05691 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
05692                                               ALL_LANGUAGES },
05693 #include "clang/Basic/BuiltinsMips.def"
05694 };
05695 
05696 class Mips32TargetInfoBase : public MipsTargetInfoBase {
05697 public:
05698   Mips32TargetInfoBase(const llvm::Triple &Triple)
05699       : MipsTargetInfoBase(Triple, "o32", "mips32r2") {
05700     SizeType = UnsignedInt;
05701     PtrDiffType = SignedInt;
05702     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
05703   }
05704   bool setABI(const std::string &Name) override {
05705     if (Name == "o32" || Name == "eabi") {
05706       ABI = Name;
05707       return true;
05708     }
05709     return false;
05710   }
05711   void getTargetDefines(const LangOptions &Opts,
05712                         MacroBuilder &Builder) const override {
05713     MipsTargetInfoBase::getTargetDefines(Opts, Builder);
05714 
05715     Builder.defineMacro("__mips", "32");
05716     Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS32");
05717 
05718     const std::string& CPUStr = getCPU();
05719     if (CPUStr == "mips32")
05720       Builder.defineMacro("__mips_isa_rev", "1");
05721     else if (CPUStr == "mips32r2")
05722       Builder.defineMacro("__mips_isa_rev", "2");
05723 
05724     if (ABI == "o32") {
05725       Builder.defineMacro("__mips_o32");
05726       Builder.defineMacro("_ABIO32", "1");
05727       Builder.defineMacro("_MIPS_SIM", "_ABIO32");
05728     }
05729     else if (ABI == "eabi")
05730       Builder.defineMacro("__mips_eabi");
05731     else
05732       llvm_unreachable("Invalid ABI for Mips32.");
05733   }
05734   void getGCCRegAliases(const GCCRegAlias *&Aliases,
05735                         unsigned &NumAliases) const override {
05736     static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
05737       { { "at" },  "$1" },
05738       { { "v0" },  "$2" },
05739       { { "v1" },  "$3" },
05740       { { "a0" },  "$4" },
05741       { { "a1" },  "$5" },
05742       { { "a2" },  "$6" },
05743       { { "a3" },  "$7" },
05744       { { "t0" },  "$8" },
05745       { { "t1" },  "$9" },
05746       { { "t2" }, "$10" },
05747       { { "t3" }, "$11" },
05748       { { "t4" }, "$12" },
05749       { { "t5" }, "$13" },
05750       { { "t6" }, "$14" },
05751       { { "t7" }, "$15" },
05752       { { "s0" }, "$16" },
05753       { { "s1" }, "$17" },
05754       { { "s2" }, "$18" },
05755       { { "s3" }, "$19" },
05756       { { "s4" }, "$20" },
05757       { { "s5" }, "$21" },
05758       { { "s6" }, "$22" },
05759       { { "s7" }, "$23" },
05760       { { "t8" }, "$24" },
05761       { { "t9" }, "$25" },
05762       { { "k0" }, "$26" },
05763       { { "k1" }, "$27" },
05764       { { "gp" }, "$28" },
05765       { { "sp","$sp" }, "$29" },
05766       { { "fp","$fp" }, "$30" },
05767       { { "ra" }, "$31" }
05768     };
05769     Aliases = GCCRegAliases;
05770     NumAliases = llvm::array_lengthof(GCCRegAliases);
05771   }
05772 };
05773 
05774 class Mips32EBTargetInfo : public Mips32TargetInfoBase {
05775   void setDescriptionString() override {
05776     DescriptionString = "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64";
05777   }
05778 
05779 public:
05780   Mips32EBTargetInfo(const llvm::Triple &Triple)
05781       : Mips32TargetInfoBase(Triple) {
05782   }
05783   void getTargetDefines(const LangOptions &Opts,
05784                         MacroBuilder &Builder) const override {
05785     DefineStd(Builder, "MIPSEB", Opts);
05786     Builder.defineMacro("_MIPSEB");
05787     Mips32TargetInfoBase::getTargetDefines(Opts, Builder);
05788   }
05789 };
05790 
05791 class Mips32ELTargetInfo : public Mips32TargetInfoBase {
05792   void setDescriptionString() override {
05793     DescriptionString = "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64";
05794   }
05795 
05796 public:
05797   Mips32ELTargetInfo(const llvm::Triple &Triple)
05798       : Mips32TargetInfoBase(Triple) {
05799     BigEndian = false;
05800   }
05801   void getTargetDefines(const LangOptions &Opts,
05802                         MacroBuilder &Builder) const override {
05803     DefineStd(Builder, "MIPSEL", Opts);
05804     Builder.defineMacro("_MIPSEL");
05805     Mips32TargetInfoBase::getTargetDefines(Opts, Builder);
05806   }
05807 };
05808 
05809 class Mips64TargetInfoBase : public MipsTargetInfoBase {
05810 public:
05811   Mips64TargetInfoBase(const llvm::Triple &Triple)
05812       : MipsTargetInfoBase(Triple, "n64", "mips64r2") {
05813     LongDoubleWidth = LongDoubleAlign = 128;
05814     LongDoubleFormat = &llvm::APFloat::IEEEquad;
05815     if (getTriple().getOS() == llvm::Triple::FreeBSD) {
05816       LongDoubleWidth = LongDoubleAlign = 64;
05817       LongDoubleFormat = &llvm::APFloat::IEEEdouble;
05818     }
05819     setN64ABITypes();
05820     SuitableAlign = 128;
05821     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
05822   }
05823 
05824   void setN64ABITypes() {
05825     LongWidth = LongAlign = 64;
05826     PointerWidth = PointerAlign = 64;
05827     SizeType = UnsignedLong;
05828     PtrDiffType = SignedLong;
05829   }
05830 
05831   void setN32ABITypes() {
05832     LongWidth = LongAlign = 32;
05833     PointerWidth = PointerAlign = 32;
05834     SizeType = UnsignedInt;
05835     PtrDiffType = SignedInt;
05836   }
05837 
05838   bool setABI(const std::string &Name) override {
05839     if (Name == "n32") {
05840       setN32ABITypes();
05841       ABI = Name;
05842       return true;
05843     }
05844     if (Name == "n64") {
05845       setN64ABITypes();
05846       ABI = Name;
05847       return true;
05848     }
05849     return false;
05850   }
05851 
05852   void getTargetDefines(const LangOptions &Opts,
05853                         MacroBuilder &Builder) const override {
05854     MipsTargetInfoBase::getTargetDefines(Opts, Builder);
05855 
05856     Builder.defineMacro("__mips", "64");
05857     Builder.defineMacro("__mips64");
05858     Builder.defineMacro("__mips64__");
05859     Builder.defineMacro("_MIPS_ISA", "_MIPS_ISA_MIPS64");
05860 
05861     const std::string& CPUStr = getCPU();
05862     if (CPUStr == "mips64")
05863       Builder.defineMacro("__mips_isa_rev", "1");
05864     else if (CPUStr == "mips64r2")
05865       Builder.defineMacro("__mips_isa_rev", "2");
05866 
05867     if (ABI == "n32") {
05868       Builder.defineMacro("__mips_n32");
05869       Builder.defineMacro("_ABIN32", "2");
05870       Builder.defineMacro("_MIPS_SIM", "_ABIN32");
05871     }
05872     else if (ABI == "n64") {
05873       Builder.defineMacro("__mips_n64");
05874       Builder.defineMacro("_ABI64", "3");
05875       Builder.defineMacro("_MIPS_SIM", "_ABI64");
05876     }
05877     else
05878       llvm_unreachable("Invalid ABI for Mips64.");
05879   }
05880   void getGCCRegAliases(const GCCRegAlias *&Aliases,
05881                         unsigned &NumAliases) const override {
05882     static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
05883       { { "at" },  "$1" },
05884       { { "v0" },  "$2" },
05885       { { "v1" },  "$3" },
05886       { { "a0" },  "$4" },
05887       { { "a1" },  "$5" },
05888       { { "a2" },  "$6" },
05889       { { "a3" },  "$7" },
05890       { { "a4" },  "$8" },
05891       { { "a5" },  "$9" },
05892       { { "a6" }, "$10" },
05893       { { "a7" }, "$11" },
05894       { { "t0" }, "$12" },
05895       { { "t1" }, "$13" },
05896       { { "t2" }, "$14" },
05897       { { "t3" }, "$15" },
05898       { { "s0" }, "$16" },
05899       { { "s1" }, "$17" },
05900       { { "s2" }, "$18" },
05901       { { "s3" }, "$19" },
05902       { { "s4" }, "$20" },
05903       { { "s5" }, "$21" },
05904       { { "s6" }, "$22" },
05905       { { "s7" }, "$23" },
05906       { { "t8" }, "$24" },
05907       { { "t9" }, "$25" },
05908       { { "k0" }, "$26" },
05909       { { "k1" }, "$27" },
05910       { { "gp" }, "$28" },
05911       { { "sp","$sp" }, "$29" },
05912       { { "fp","$fp" }, "$30" },
05913       { { "ra" }, "$31" }
05914     };
05915     Aliases = GCCRegAliases;
05916     NumAliases = llvm::array_lengthof(GCCRegAliases);
05917   }
05918 
05919   bool hasInt128Type() const override { return true; }
05920 };
05921 
05922 class Mips64EBTargetInfo : public Mips64TargetInfoBase {
05923   void setDescriptionString() override {
05924     if (ABI == "n32")
05925       DescriptionString = "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32:64-S128";
05926     else
05927       DescriptionString = "E-m:m-i8:8:32-i16:16:32-i64:64-n32:64-S128";
05928 
05929   }
05930 
05931 public:
05932   Mips64EBTargetInfo(const llvm::Triple &Triple)
05933       : Mips64TargetInfoBase(Triple) {}
05934   void getTargetDefines(const LangOptions &Opts,
05935                         MacroBuilder &Builder) const override {
05936     DefineStd(Builder, "MIPSEB", Opts);
05937     Builder.defineMacro("_MIPSEB");
05938     Mips64TargetInfoBase::getTargetDefines(Opts, Builder);
05939   }
05940 };
05941 
05942 class Mips64ELTargetInfo : public Mips64TargetInfoBase {
05943   void setDescriptionString() override {
05944     if (ABI == "n32")
05945       DescriptionString = "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32:64-S128";
05946     else
05947       DescriptionString = "e-m:m-i8:8:32-i16:16:32-i64:64-n32:64-S128";
05948   }
05949 public:
05950   Mips64ELTargetInfo(const llvm::Triple &Triple)
05951       : Mips64TargetInfoBase(Triple) {
05952     // Default ABI is n64.
05953     BigEndian = false;
05954   }
05955   void getTargetDefines(const LangOptions &Opts,
05956                         MacroBuilder &Builder) const override {
05957     DefineStd(Builder, "MIPSEL", Opts);
05958     Builder.defineMacro("_MIPSEL");
05959     Mips64TargetInfoBase::getTargetDefines(Opts, Builder);
05960   }
05961 };
05962 } // end anonymous namespace.
05963 
05964 namespace {
05965 class PNaClTargetInfo : public TargetInfo {
05966 public:
05967   PNaClTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
05968     BigEndian = false;
05969     this->UserLabelPrefix = "";
05970     this->LongAlign = 32;
05971     this->LongWidth = 32;
05972     this->PointerAlign = 32;
05973     this->PointerWidth = 32;
05974     this->IntMaxType = TargetInfo::SignedLongLong;
05975     this->Int64Type = TargetInfo::SignedLongLong;
05976     this->DoubleAlign = 64;
05977     this->LongDoubleWidth = 64;
05978     this->LongDoubleAlign = 64;
05979     this->SizeType = TargetInfo::UnsignedInt;
05980     this->PtrDiffType = TargetInfo::SignedInt;
05981     this->IntPtrType = TargetInfo::SignedInt;
05982     this->RegParmMax = 0; // Disallow regparm
05983   }
05984 
05985   void getDefaultFeatures(llvm::StringMap<bool> &Features) const override {
05986   }
05987   void getArchDefines(const LangOptions &Opts, MacroBuilder &Builder) const {
05988     Builder.defineMacro("__le32__");
05989     Builder.defineMacro("__pnacl__");
05990   }
05991   void getTargetDefines(const LangOptions &Opts,
05992                         MacroBuilder &Builder) const override {
05993     getArchDefines(Opts, Builder);
05994   }
05995   bool hasFeature(StringRef Feature) const override {
05996     return Feature == "pnacl";
05997   }
05998   void getTargetBuiltins(const Builtin::Info *&Records,
05999                          unsigned &NumRecords) const override {
06000   }
06001   BuiltinVaListKind getBuiltinVaListKind() const override {
06002     return TargetInfo::PNaClABIBuiltinVaList;
06003   }
06004   void getGCCRegNames(const char * const *&Names,
06005                       unsigned &NumNames) const override;
06006   void getGCCRegAliases(const GCCRegAlias *&Aliases,
06007                         unsigned &NumAliases) const override;
06008   bool validateAsmConstraint(const char *&Name,
06009                              TargetInfo::ConstraintInfo &Info) const override {
06010     return false;
06011   }
06012 
06013   const char *getClobbers() const override {
06014     return "";
06015   }
06016 };
06017 
06018 void PNaClTargetInfo::getGCCRegNames(const char * const *&Names,
06019                                      unsigned &NumNames) const {
06020   Names = nullptr;
06021   NumNames = 0;
06022 }
06023 
06024 void PNaClTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
06025                                        unsigned &NumAliases) const {
06026   Aliases = nullptr;
06027   NumAliases = 0;
06028 }
06029 } // end anonymous namespace.
06030 
06031 namespace {
06032 class Le64TargetInfo : public TargetInfo {
06033   static const Builtin::Info BuiltinInfo[];
06034 
06035 public:
06036   Le64TargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
06037     BigEndian = false;
06038     NoAsmVariants = true;
06039     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
06040     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
06041     DescriptionString =
06042         "e-S128-p:64:64-v16:16-v32:32-v64:64-v96:32-v128:32-m:e-n8:16:32:64";
06043   }
06044 
06045   void getTargetDefines(const LangOptions &Opts,
06046                         MacroBuilder &Builder) const override {
06047     DefineStd(Builder, "unix", Opts);
06048     defineCPUMacros(Builder, "le64", /*Tuning=*/false);
06049     Builder.defineMacro("__ELF__");
06050   }
06051   void getTargetBuiltins(const Builtin::Info *&Records,
06052                          unsigned &NumRecords) const override {
06053     Records = BuiltinInfo;
06054     NumRecords = clang::Le64::LastTSBuiltin - Builtin::FirstTSBuiltin;
06055   }
06056   BuiltinVaListKind getBuiltinVaListKind() const override {
06057     return TargetInfo::PNaClABIBuiltinVaList;
06058   }
06059   const char *getClobbers() const override { return ""; }
06060   void getGCCRegNames(const char *const *&Names,
06061                       unsigned &NumNames) const override {
06062     Names = nullptr;
06063     NumNames = 0;
06064   }
06065   void getGCCRegAliases(const GCCRegAlias *&Aliases,
06066                         unsigned &NumAliases) const override {
06067     Aliases = nullptr;
06068     NumAliases = 0;
06069   }
06070   bool validateAsmConstraint(const char *&Name,
06071                              TargetInfo::ConstraintInfo &Info) const override {
06072     return false;
06073   }
06074 
06075   bool hasProtectedVisibility() const override { return false; }
06076 };
06077 } // end anonymous namespace.
06078 
06079 const Builtin::Info Le64TargetInfo::BuiltinInfo[] = {
06080 #define BUILTIN(ID, TYPE, ATTRS)                                               \
06081   { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
06082 #include "clang/Basic/BuiltinsLe64.def"
06083 };
06084 
06085 namespace {
06086   static const unsigned SPIRAddrSpaceMap[] = {
06087     1,    // opencl_global
06088     3,    // opencl_local
06089     2,    // opencl_constant
06090     0,    // cuda_device
06091     0,    // cuda_constant
06092     0     // cuda_shared
06093   };
06094   class SPIRTargetInfo : public TargetInfo {
06095   public:
06096     SPIRTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
06097       assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
06098         "SPIR target must use unknown OS");
06099       assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
06100         "SPIR target must use unknown environment type");
06101       BigEndian = false;
06102       TLSSupported = false;
06103       LongWidth = LongAlign = 64;
06104       AddrSpaceMap = &SPIRAddrSpaceMap;
06105       UseAddrSpaceMapMangling = true;
06106       // Define available target features
06107       // These must be defined in sorted order!
06108       NoAsmVariants = true;
06109     }
06110     void getTargetDefines(const LangOptions &Opts,
06111                           MacroBuilder &Builder) const override {
06112       DefineStd(Builder, "SPIR", Opts);
06113     }
06114     bool hasFeature(StringRef Feature) const override {
06115       return Feature == "spir";
06116     }
06117 
06118     void getTargetBuiltins(const Builtin::Info *&Records,
06119                            unsigned &NumRecords) const override {}
06120     const char *getClobbers() const override {
06121       return "";
06122     }
06123     void getGCCRegNames(const char * const *&Names,
06124                         unsigned &NumNames) const override {}
06125     bool validateAsmConstraint(const char *&Name,
06126                                TargetInfo::ConstraintInfo &info) const override {
06127       return true;
06128     }
06129     void getGCCRegAliases(const GCCRegAlias *&Aliases,
06130                           unsigned &NumAliases) const override {}
06131     BuiltinVaListKind getBuiltinVaListKind() const override {
06132       return TargetInfo::VoidPtrBuiltinVaList;
06133     }
06134   };
06135 
06136 
06137   class SPIR32TargetInfo : public SPIRTargetInfo {
06138   public:
06139     SPIR32TargetInfo(const llvm::Triple &Triple) : SPIRTargetInfo(Triple) {
06140       PointerWidth = PointerAlign = 32;
06141       SizeType     = TargetInfo::UnsignedInt;
06142       PtrDiffType = IntPtrType = TargetInfo::SignedInt;
06143       DescriptionString
06144         = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-"
06145           "v96:128-v192:256-v256:256-v512:512-v1024:1024";
06146     }
06147     void getTargetDefines(const LangOptions &Opts,
06148                           MacroBuilder &Builder) const override {
06149       DefineStd(Builder, "SPIR32", Opts);
06150     }
06151   };
06152 
06153   class SPIR64TargetInfo : public SPIRTargetInfo {
06154   public:
06155     SPIR64TargetInfo(const llvm::Triple &Triple) : SPIRTargetInfo(Triple) {
06156       PointerWidth = PointerAlign = 64;
06157       SizeType     = TargetInfo::UnsignedLong;
06158       PtrDiffType = IntPtrType = TargetInfo::SignedLong;
06159       DescriptionString = "e-i64:64-v16:16-v24:32-v32:32-v48:64-"
06160                           "v96:128-v192:256-v256:256-v512:512-v1024:1024";
06161     }
06162     void getTargetDefines(const LangOptions &Opts,
06163                           MacroBuilder &Builder) const override {
06164       DefineStd(Builder, "SPIR64", Opts);
06165     }
06166   };
06167 }
06168 
06169 namespace {
06170 class XCoreTargetInfo : public TargetInfo {
06171   static const Builtin::Info BuiltinInfo[];
06172 public:
06173   XCoreTargetInfo(const llvm::Triple &Triple) : TargetInfo(Triple) {
06174     BigEndian = false;
06175     NoAsmVariants = true;
06176     LongLongAlign = 32;
06177     SuitableAlign = 32;
06178     DoubleAlign = LongDoubleAlign = 32;
06179     SizeType = UnsignedInt;
06180     PtrDiffType = SignedInt;
06181     IntPtrType = SignedInt;
06182     WCharType = UnsignedChar;
06183     WIntType = UnsignedInt;
06184     UseZeroLengthBitfieldAlignment = true;
06185     DescriptionString = "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i64:32"
06186                         "-f64:32-a:0:32-n32";
06187   }
06188   void getTargetDefines(const LangOptions &Opts,
06189                         MacroBuilder &Builder) const override {
06190     Builder.defineMacro("__XS1B__");
06191   }
06192   void getTargetBuiltins(const Builtin::Info *&Records,
06193                          unsigned &NumRecords) const override {
06194     Records = BuiltinInfo;
06195     NumRecords = clang::XCore::LastTSBuiltin-Builtin::FirstTSBuiltin;
06196   }
06197   BuiltinVaListKind getBuiltinVaListKind() const override {
06198     return TargetInfo::VoidPtrBuiltinVaList;
06199   }
06200   const char *getClobbers() const override {
06201     return "";
06202   }
06203   void getGCCRegNames(const char * const *&Names,
06204                       unsigned &NumNames) const override {
06205     static const char * const GCCRegNames[] = {
06206       "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
06207       "r8",   "r9",   "r10",  "r11",  "cp",   "dp",   "sp",   "lr"
06208     };
06209     Names = GCCRegNames;
06210     NumNames = llvm::array_lengthof(GCCRegNames);
06211   }
06212   void getGCCRegAliases(const GCCRegAlias *&Aliases,
06213                         unsigned &NumAliases) const override {
06214     Aliases = nullptr;
06215     NumAliases = 0;
06216   }
06217   bool validateAsmConstraint(const char *&Name,
06218                              TargetInfo::ConstraintInfo &Info) const override {
06219     return false;
06220   }
06221   int getEHDataRegisterNumber(unsigned RegNo) const override {
06222     // R0=ExceptionPointerRegister R1=ExceptionSelectorRegister
06223     return (RegNo < 2)? RegNo : -1;
06224   }
06225 };
06226 
06227 const Builtin::Info XCoreTargetInfo::BuiltinInfo[] = {
06228 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
06229 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
06230                                               ALL_LANGUAGES },
06231 #include "clang/Basic/BuiltinsXCore.def"
06232 };
06233 } // end anonymous namespace.
06234 
06235 
06236 //===----------------------------------------------------------------------===//
06237 // Driver code
06238 //===----------------------------------------------------------------------===//
06239 
06240 static TargetInfo *AllocateTarget(const llvm::Triple &Triple) {
06241   llvm::Triple::OSType os = Triple.getOS();
06242 
06243   switch (Triple.getArch()) {
06244   default:
06245     return nullptr;
06246 
06247   case llvm::Triple::xcore:
06248     return new XCoreTargetInfo(Triple);
06249 
06250   case llvm::Triple::hexagon:
06251     return new HexagonTargetInfo(Triple);
06252 
06253   case llvm::Triple::aarch64:
06254     if (Triple.isOSDarwin())
06255       return new DarwinAArch64TargetInfo(Triple);
06256 
06257     switch (os) {
06258     case llvm::Triple::FreeBSD:
06259       return new FreeBSDTargetInfo<AArch64leTargetInfo>(Triple);
06260     case llvm::Triple::Linux:
06261       return new LinuxTargetInfo<AArch64leTargetInfo>(Triple);
06262     case llvm::Triple::NetBSD:
06263       return new NetBSDTargetInfo<AArch64leTargetInfo>(Triple);
06264     default:
06265       return new AArch64leTargetInfo(Triple);
06266     }
06267 
06268   case llvm::Triple::aarch64_be:
06269     switch (os) {
06270     case llvm::Triple::FreeBSD:
06271       return new FreeBSDTargetInfo<AArch64beTargetInfo>(Triple);
06272     case llvm::Triple::Linux:
06273       return new LinuxTargetInfo<AArch64beTargetInfo>(Triple);
06274     case llvm::Triple::NetBSD:
06275       return new NetBSDTargetInfo<AArch64beTargetInfo>(Triple);
06276     default:
06277       return new AArch64beTargetInfo(Triple);
06278     }
06279 
06280   case llvm::Triple::arm:
06281   case llvm::Triple::thumb:
06282     if (Triple.isOSBinFormatMachO())
06283       return new DarwinARMTargetInfo(Triple);
06284 
06285     switch (os) {
06286     case llvm::Triple::Linux:
06287       return new LinuxTargetInfo<ARMleTargetInfo>(Triple);
06288     case llvm::Triple::FreeBSD:
06289       return new FreeBSDTargetInfo<ARMleTargetInfo>(Triple);
06290     case llvm::Triple::NetBSD:
06291       return new NetBSDTargetInfo<ARMleTargetInfo>(Triple);
06292     case llvm::Triple::OpenBSD:
06293       return new OpenBSDTargetInfo<ARMleTargetInfo>(Triple);
06294     case llvm::Triple::Bitrig:
06295       return new BitrigTargetInfo<ARMleTargetInfo>(Triple);
06296     case llvm::Triple::RTEMS:
06297       return new RTEMSTargetInfo<ARMleTargetInfo>(Triple);
06298     case llvm::Triple::NaCl:
06299       return new NaClTargetInfo<ARMleTargetInfo>(Triple);
06300     case llvm::Triple::Win32:
06301       switch (Triple.getEnvironment()) {
06302       default:
06303         return new ARMleTargetInfo(Triple);
06304       case llvm::Triple::Itanium:
06305         return new ItaniumWindowsARMleTargetInfo(Triple);
06306       case llvm::Triple::MSVC:
06307         return new MicrosoftARMleTargetInfo(Triple);
06308       }
06309     default:
06310       return new ARMleTargetInfo(Triple);
06311     }
06312 
06313   case llvm::Triple::armeb:
06314   case llvm::Triple::thumbeb:
06315     if (Triple.isOSDarwin())
06316       return new DarwinARMTargetInfo(Triple);
06317 
06318     switch (os) {
06319     case llvm::Triple::Linux:
06320       return new LinuxTargetInfo<ARMbeTargetInfo>(Triple);
06321     case llvm::Triple::FreeBSD:
06322       return new FreeBSDTargetInfo<ARMbeTargetInfo>(Triple);
06323     case llvm::Triple::NetBSD:
06324       return new NetBSDTargetInfo<ARMbeTargetInfo>(Triple);
06325     case llvm::Triple::OpenBSD:
06326       return new OpenBSDTargetInfo<ARMbeTargetInfo>(Triple);
06327     case llvm::Triple::Bitrig:
06328       return new BitrigTargetInfo<ARMbeTargetInfo>(Triple);
06329     case llvm::Triple::RTEMS:
06330       return new RTEMSTargetInfo<ARMbeTargetInfo>(Triple);
06331     case llvm::Triple::NaCl:
06332       return new NaClTargetInfo<ARMbeTargetInfo>(Triple);
06333     default:
06334       return new ARMbeTargetInfo(Triple);
06335     }
06336 
06337   case llvm::Triple::msp430:
06338     return new MSP430TargetInfo(Triple);
06339 
06340   case llvm::Triple::mips:
06341     switch (os) {
06342     case llvm::Triple::Linux:
06343       return new LinuxTargetInfo<Mips32EBTargetInfo>(Triple);
06344     case llvm::Triple::RTEMS:
06345       return new RTEMSTargetInfo<Mips32EBTargetInfo>(Triple);
06346     case llvm::Triple::FreeBSD:
06347       return new FreeBSDTargetInfo<Mips32EBTargetInfo>(Triple);
06348     case llvm::Triple::NetBSD:
06349       return new NetBSDTargetInfo<Mips32EBTargetInfo>(Triple);
06350     default:
06351       return new Mips32EBTargetInfo(Triple);
06352     }
06353 
06354   case llvm::Triple::mipsel:
06355     switch (os) {
06356     case llvm::Triple::Linux:
06357       return new LinuxTargetInfo<Mips32ELTargetInfo>(Triple);
06358     case llvm::Triple::RTEMS:
06359       return new RTEMSTargetInfo<Mips32ELTargetInfo>(Triple);
06360     case llvm::Triple::FreeBSD:
06361       return new FreeBSDTargetInfo<Mips32ELTargetInfo>(Triple);
06362     case llvm::Triple::NetBSD:
06363       return new NetBSDTargetInfo<Mips32ELTargetInfo>(Triple);
06364     case llvm::Triple::NaCl:
06365       return new NaClTargetInfo<Mips32ELTargetInfo>(Triple);
06366     default:
06367       return new Mips32ELTargetInfo(Triple);
06368     }
06369 
06370   case llvm::Triple::mips64:
06371     switch (os) {
06372     case llvm::Triple::Linux:
06373       return new LinuxTargetInfo<Mips64EBTargetInfo>(Triple);
06374     case llvm::Triple::RTEMS:
06375       return new RTEMSTargetInfo<Mips64EBTargetInfo>(Triple);
06376     case llvm::Triple::FreeBSD:
06377       return new FreeBSDTargetInfo<Mips64EBTargetInfo>(Triple);
06378     case llvm::Triple::NetBSD:
06379       return new NetBSDTargetInfo<Mips64EBTargetInfo>(Triple);
06380     case llvm::Triple::OpenBSD:
06381       return new OpenBSDTargetInfo<Mips64EBTargetInfo>(Triple);
06382     default:
06383       return new Mips64EBTargetInfo(Triple);
06384     }
06385 
06386   case llvm::Triple::mips64el:
06387     switch (os) {
06388     case llvm::Triple::Linux:
06389       return new LinuxTargetInfo<Mips64ELTargetInfo>(Triple);
06390     case llvm::Triple::RTEMS:
06391       return new RTEMSTargetInfo<Mips64ELTargetInfo>(Triple);
06392     case llvm::Triple::FreeBSD:
06393       return new FreeBSDTargetInfo<Mips64ELTargetInfo>(Triple);
06394     case llvm::Triple::NetBSD:
06395       return new NetBSDTargetInfo<Mips64ELTargetInfo>(Triple);
06396     case llvm::Triple::OpenBSD:
06397       return new OpenBSDTargetInfo<Mips64ELTargetInfo>(Triple);
06398     default:
06399       return new Mips64ELTargetInfo(Triple);
06400     }
06401 
06402   case llvm::Triple::le32:
06403     switch (os) {
06404       case llvm::Triple::NaCl:
06405         return new NaClTargetInfo<PNaClTargetInfo>(Triple);
06406       default:
06407         return nullptr;
06408     }
06409 
06410   case llvm::Triple::le64:
06411     return new Le64TargetInfo(Triple);
06412 
06413   case llvm::Triple::ppc:
06414     if (Triple.isOSDarwin())
06415       return new DarwinPPC32TargetInfo(Triple);
06416     switch (os) {
06417     case llvm::Triple::Linux:
06418       return new LinuxTargetInfo<PPC32TargetInfo>(Triple);
06419     case llvm::Triple::FreeBSD:
06420       return new FreeBSDTargetInfo<PPC32TargetInfo>(Triple);
06421     case llvm::Triple::NetBSD:
06422       return new NetBSDTargetInfo<PPC32TargetInfo>(Triple);
06423     case llvm::Triple::OpenBSD:
06424       return new OpenBSDTargetInfo<PPC32TargetInfo>(Triple);
06425     case llvm::Triple::RTEMS:
06426       return new RTEMSTargetInfo<PPC32TargetInfo>(Triple);
06427     default:
06428       return new PPC32TargetInfo(Triple);
06429     }
06430 
06431   case llvm::Triple::ppc64:
06432     if (Triple.isOSDarwin())
06433       return new DarwinPPC64TargetInfo(Triple);
06434     switch (os) {
06435     case llvm::Triple::Linux:
06436       return new LinuxTargetInfo<PPC64TargetInfo>(Triple);
06437     case llvm::Triple::Lv2:
06438       return new PS3PPUTargetInfo<PPC64TargetInfo>(Triple);
06439     case llvm::Triple::FreeBSD:
06440       return new FreeBSDTargetInfo<PPC64TargetInfo>(Triple);
06441     case llvm::Triple::NetBSD:
06442       return new NetBSDTargetInfo<PPC64TargetInfo>(Triple);
06443     default:
06444       return new PPC64TargetInfo(Triple);
06445     }
06446 
06447   case llvm::Triple::ppc64le:
06448     switch (os) {
06449     case llvm::Triple::Linux:
06450       return new LinuxTargetInfo<PPC64TargetInfo>(Triple);
06451     default:
06452       return new PPC64TargetInfo(Triple);
06453     }
06454 
06455   case llvm::Triple::nvptx:
06456     return new NVPTX32TargetInfo(Triple);
06457   case llvm::Triple::nvptx64:
06458     return new NVPTX64TargetInfo(Triple);
06459 
06460   case llvm::Triple::r600:
06461     return new R600TargetInfo(Triple);
06462 
06463   case llvm::Triple::sparc:
06464     switch (os) {
06465     case llvm::Triple::Linux:
06466       return new LinuxTargetInfo<SparcV8TargetInfo>(Triple);
06467     case llvm::Triple::Solaris:
06468       return new SolarisSparcV8TargetInfo(Triple);
06469     case llvm::Triple::NetBSD:
06470       return new NetBSDTargetInfo<SparcV8TargetInfo>(Triple);
06471     case llvm::Triple::OpenBSD:
06472       return new OpenBSDTargetInfo<SparcV8TargetInfo>(Triple);
06473     case llvm::Triple::RTEMS:
06474       return new RTEMSTargetInfo<SparcV8TargetInfo>(Triple);
06475     default:
06476       return new SparcV8TargetInfo(Triple);
06477     }
06478 
06479   case llvm::Triple::sparcv9:
06480     switch (os) {
06481     case llvm::Triple::Linux:
06482       return new LinuxTargetInfo<SparcV9TargetInfo>(Triple);
06483     case llvm::Triple::Solaris:
06484       return new SolarisTargetInfo<SparcV9TargetInfo>(Triple);
06485     case llvm::Triple::NetBSD:
06486       return new NetBSDTargetInfo<SparcV9TargetInfo>(Triple);
06487     case llvm::Triple::OpenBSD:
06488       return new OpenBSDTargetInfo<SparcV9TargetInfo>(Triple);
06489     case llvm::Triple::FreeBSD:
06490       return new FreeBSDTargetInfo<SparcV9TargetInfo>(Triple);
06491     default:
06492       return new SparcV9TargetInfo(Triple);
06493     }
06494 
06495   case llvm::Triple::systemz:
06496     switch (os) {
06497     case llvm::Triple::Linux:
06498       return new LinuxTargetInfo<SystemZTargetInfo>(Triple);
06499     default:
06500       return new SystemZTargetInfo(Triple);
06501     }
06502 
06503   case llvm::Triple::tce:
06504     return new TCETargetInfo(Triple);
06505 
06506   case llvm::Triple::x86:
06507     if (Triple.isOSDarwin())
06508       return new DarwinI386TargetInfo(Triple);
06509 
06510     switch (os) {
06511     case llvm::Triple::Linux:
06512       return new LinuxTargetInfo<X86_32TargetInfo>(Triple);
06513     case llvm::Triple::DragonFly:
06514       return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(Triple);
06515     case llvm::Triple::NetBSD:
06516       return new NetBSDI386TargetInfo(Triple);
06517     case llvm::Triple::OpenBSD:
06518       return new OpenBSDI386TargetInfo(Triple);
06519     case llvm::Triple::Bitrig:
06520       return new BitrigI386TargetInfo(Triple);
06521     case llvm::Triple::FreeBSD:
06522       return new FreeBSDTargetInfo<X86_32TargetInfo>(Triple);
06523     case llvm::Triple::KFreeBSD:
06524       return new KFreeBSDTargetInfo<X86_32TargetInfo>(Triple);
06525     case llvm::Triple::Minix:
06526       return new MinixTargetInfo<X86_32TargetInfo>(Triple);
06527     case llvm::Triple::Solaris:
06528       return new SolarisTargetInfo<X86_32TargetInfo>(Triple);
06529     case llvm::Triple::Win32: {
06530       switch (Triple.getEnvironment()) {
06531       default:
06532         return new X86_32TargetInfo(Triple);
06533       case llvm::Triple::Cygnus:
06534         return new CygwinX86_32TargetInfo(Triple);
06535       case llvm::Triple::GNU:
06536         return new MinGWX86_32TargetInfo(Triple);
06537       case llvm::Triple::Itanium:
06538       case llvm::Triple::MSVC:
06539         return new MicrosoftX86_32TargetInfo(Triple);
06540       }
06541     }
06542     case llvm::Triple::Haiku:
06543       return new HaikuX86_32TargetInfo(Triple);
06544     case llvm::Triple::RTEMS:
06545       return new RTEMSX86_32TargetInfo(Triple);
06546     case llvm::Triple::NaCl:
06547       return new NaClTargetInfo<X86_32TargetInfo>(Triple);
06548     default:
06549       return new X86_32TargetInfo(Triple);
06550     }
06551 
06552   case llvm::Triple::x86_64:
06553     if (Triple.isOSDarwin() || Triple.isOSBinFormatMachO())
06554       return new DarwinX86_64TargetInfo(Triple);
06555 
06556     switch (os) {
06557     case llvm::Triple::Linux:
06558       return new LinuxTargetInfo<X86_64TargetInfo>(Triple);
06559     case llvm::Triple::DragonFly:
06560       return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(Triple);
06561     case llvm::Triple::NetBSD:
06562       return new NetBSDTargetInfo<X86_64TargetInfo>(Triple);
06563     case llvm::Triple::OpenBSD:
06564       return new OpenBSDX86_64TargetInfo(Triple);
06565     case llvm::Triple::Bitrig:
06566       return new BitrigX86_64TargetInfo(Triple);
06567     case llvm::Triple::FreeBSD:
06568       return new FreeBSDTargetInfo<X86_64TargetInfo>(Triple);
06569     case llvm::Triple::KFreeBSD:
06570       return new KFreeBSDTargetInfo<X86_64TargetInfo>(Triple);
06571     case llvm::Triple::Solaris:
06572       return new SolarisTargetInfo<X86_64TargetInfo>(Triple);
06573     case llvm::Triple::Win32: {
06574       switch (Triple.getEnvironment()) {
06575       default:
06576         return new X86_64TargetInfo(Triple);
06577       case llvm::Triple::GNU:
06578         return new MinGWX86_64TargetInfo(Triple);
06579       case llvm::Triple::MSVC:
06580         return new MicrosoftX86_64TargetInfo(Triple);
06581       }
06582     }
06583     case llvm::Triple::NaCl:
06584       return new NaClTargetInfo<X86_64TargetInfo>(Triple);
06585     default:
06586       return new X86_64TargetInfo(Triple);
06587     }
06588 
06589     case llvm::Triple::spir: {
06590       if (Triple.getOS() != llvm::Triple::UnknownOS ||
06591           Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
06592         return nullptr;
06593       return new SPIR32TargetInfo(Triple);
06594     }
06595     case llvm::Triple::spir64: {
06596       if (Triple.getOS() != llvm::Triple::UnknownOS ||
06597           Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
06598         return nullptr;
06599       return new SPIR64TargetInfo(Triple);
06600     }
06601   }
06602 }
06603 
06604 /// CreateTargetInfo - Return the target info object for the specified target
06605 /// triple.
06606 TargetInfo *
06607 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
06608                              const std::shared_ptr<TargetOptions> &Opts) {
06609   llvm::Triple Triple(Opts->Triple);
06610 
06611   // Construct the target
06612   std::unique_ptr<TargetInfo> Target(AllocateTarget(Triple));
06613   if (!Target) {
06614     Diags.Report(diag::err_target_unknown_triple) << Triple.str();
06615     return nullptr;
06616   }
06617   Target->TargetOpts = Opts;
06618 
06619   // Set the target CPU if specified.
06620   if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
06621     Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
06622     return nullptr;
06623   }
06624 
06625   // Set the target ABI if specified.
06626   if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
06627     Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
06628     return nullptr;
06629   }
06630 
06631   // Set the fp math unit.
06632   if (!Opts->FPMath.empty() && !Target->setFPMath(Opts->FPMath)) {
06633     Diags.Report(diag::err_target_unknown_fpmath) << Opts->FPMath;
06634     return nullptr;
06635   }
06636 
06637   // Compute the default target features, we need the target to handle this
06638   // because features may have dependencies on one another.
06639   llvm::StringMap<bool> Features;
06640   Target->getDefaultFeatures(Features);
06641 
06642   // Apply the user specified deltas.
06643   for (unsigned I = 0, N = Opts->FeaturesAsWritten.size();
06644        I < N; ++I) {
06645     const char *Name = Opts->FeaturesAsWritten[I].c_str();
06646     // Apply the feature via the target.
06647     bool Enabled = Name[0] == '+';
06648     Target->setFeatureEnabled(Features, Name + 1, Enabled);
06649   }
06650 
06651   // Add the features to the compile options.
06652   //
06653   // FIXME: If we are completely confident that we have the right set, we only
06654   // need to pass the minuses.
06655   Opts->Features.clear();
06656   for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
06657          ie = Features.end(); it != ie; ++it)
06658     Opts->Features.push_back((it->second ? "+" : "-") + it->first().str());
06659   if (!Target->handleTargetFeatures(Opts->Features, Diags))
06660     return nullptr;
06661 
06662   return Target.release();
06663 }