1 //===--- Targets.cpp - Implement -arch option and targets -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements construction of a TargetInfo object from a
11 // target triple.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Basic/Builtins.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/MacroBuilder.h"
20 #include "clang/Basic/TargetBuiltins.h"
21 #include "clang/Basic/TargetOptions.h"
22 #include "llvm/ADT/APFloat.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/MC/MCSectionMachO.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include <algorithm>
32 using namespace clang;
33 
34 //===----------------------------------------------------------------------===//
35 //  Common code shared among targets.
36 //===----------------------------------------------------------------------===//
37 
38 /// DefineStd - Define a macro name and standard variants.  For example if
39 /// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
40 /// when in GNU mode.
41 static void DefineStd(MacroBuilder &Builder, StringRef MacroName,
42                       const LangOptions &Opts) {
43   assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
44 
45   // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
46   // in the user's namespace.
47   if (Opts.GNUMode)
48     Builder.defineMacro(MacroName);
49 
50   // Define __unix.
51   Builder.defineMacro("__" + MacroName);
52 
53   // Define __unix__.
54   Builder.defineMacro("__" + MacroName + "__");
55 }
56 
57 static void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName,
58                             bool Tuning = true) {
59   Builder.defineMacro("__" + CPUName);
60   Builder.defineMacro("__" + CPUName + "__");
61   if (Tuning)
62     Builder.defineMacro("__tune_" + CPUName + "__");
63 }
64 
65 //===----------------------------------------------------------------------===//
66 // Defines specific to certain operating systems.
67 //===----------------------------------------------------------------------===//
68 
69 namespace {
70 template<typename TgtInfo>
71 class OSTargetInfo : public TgtInfo {
72 protected:
73   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
74                             MacroBuilder &Builder) const=0;
75 public:
76   OSTargetInfo(const std::string& triple) : TgtInfo(triple) {}
77   virtual void getTargetDefines(const LangOptions &Opts,
78                                 MacroBuilder &Builder) const {
79     TgtInfo::getTargetDefines(Opts, Builder);
80     getOSDefines(Opts, TgtInfo::getTriple(), Builder);
81   }
82 
83 };
84 } // end anonymous namespace
85 
86 
87 static void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
88                              const llvm::Triple &Triple,
89                              StringRef &PlatformName,
90                              VersionTuple &PlatformMinVersion) {
91   Builder.defineMacro("__APPLE_CC__", "5621");
92   Builder.defineMacro("__APPLE__");
93   Builder.defineMacro("__MACH__");
94   Builder.defineMacro("OBJC_NEW_PROPERTIES");
95   // AddressSanitizer doesn't play well with source fortification, which is on
96   // by default on Darwin.
97   if (Opts.Sanitize.Address) Builder.defineMacro("_FORTIFY_SOURCE", "0");
98 
99   if (!Opts.ObjCAutoRefCount) {
100     // __weak is always defined, for use in blocks and with objc pointers.
101     Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
102 
103     // Darwin defines __strong even in C mode (just to nothing).
104     if (Opts.getGC() != LangOptions::NonGC)
105       Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
106     else
107       Builder.defineMacro("__strong", "");
108 
109     // __unsafe_unretained is defined to nothing in non-ARC mode. We even
110     // allow this in C, since one might have block pointers in structs that
111     // are used in pure C code and in Objective-C ARC.
112     Builder.defineMacro("__unsafe_unretained", "");
113   }
114 
115   if (Opts.Static)
116     Builder.defineMacro("__STATIC__");
117   else
118     Builder.defineMacro("__DYNAMIC__");
119 
120   if (Opts.POSIXThreads)
121     Builder.defineMacro("_REENTRANT");
122 
123   // Get the platform type and version number from the triple.
124   unsigned Maj, Min, Rev;
125   if (Triple.isMacOSX()) {
126     Triple.getMacOSXVersion(Maj, Min, Rev);
127     PlatformName = "macosx";
128   } else {
129     Triple.getOSVersion(Maj, Min, Rev);
130     PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
131   }
132 
133   // If -target arch-pc-win32-macho option specified, we're
134   // generating code for Win32 ABI. No need to emit
135   // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
136   if (PlatformName == "win32") {
137     PlatformMinVersion = VersionTuple(Maj, Min, Rev);
138     return;
139   }
140 
141   // Set the appropriate OS version define.
142   if (Triple.getOS() == llvm::Triple::IOS) {
143     assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
144     char Str[6];
145     Str[0] = '0' + Maj;
146     Str[1] = '0' + (Min / 10);
147     Str[2] = '0' + (Min % 10);
148     Str[3] = '0' + (Rev / 10);
149     Str[4] = '0' + (Rev % 10);
150     Str[5] = '\0';
151     Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__", Str);
152   } else {
153     // Note that the Driver allows versions which aren't representable in the
154     // define (because we only get a single digit for the minor and micro
155     // revision numbers). So, we limit them to the maximum representable
156     // version.
157     assert(Triple.getEnvironmentName().empty() && "Invalid environment!");
158     assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
159     char Str[5];
160     Str[0] = '0' + (Maj / 10);
161     Str[1] = '0' + (Maj % 10);
162     Str[2] = '0' + std::min(Min, 9U);
163     Str[3] = '0' + std::min(Rev, 9U);
164     Str[4] = '\0';
165     Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
166   }
167 
168   PlatformMinVersion = VersionTuple(Maj, Min, Rev);
169 }
170 
171 namespace {
172 template<typename Target>
173 class DarwinTargetInfo : public OSTargetInfo<Target> {
174 protected:
175   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
176                             MacroBuilder &Builder) const {
177     getDarwinDefines(Builder, Opts, Triple, this->PlatformName,
178                      this->PlatformMinVersion);
179   }
180 
181 public:
182   DarwinTargetInfo(const std::string& triple) :
183     OSTargetInfo<Target>(triple) {
184       llvm::Triple T = llvm::Triple(triple);
185       this->TLSSupported = T.isMacOSX() && !T.isMacOSXVersionLT(10,7);
186       this->MCountName = "\01mcount";
187     }
188 
189   virtual std::string isValidSectionSpecifier(StringRef SR) const {
190     // Let MCSectionMachO validate this.
191     StringRef Segment, Section;
192     unsigned TAA, StubSize;
193     bool HasTAA;
194     return llvm::MCSectionMachO::ParseSectionSpecifier(SR, Segment, Section,
195                                                        TAA, HasTAA, StubSize);
196   }
197 
198   virtual const char *getStaticInitSectionSpecifier() const {
199     // FIXME: We should return 0 when building kexts.
200     return "__TEXT,__StaticInit,regular,pure_instructions";
201   }
202 
203   /// Darwin does not support protected visibility.  Darwin's "default"
204   /// is very similar to ELF's "protected";  Darwin requires a "weak"
205   /// attribute on declarations that can be dynamically replaced.
206   virtual bool hasProtectedVisibility() const {
207     return false;
208   }
209 };
210 
211 
212 // DragonFlyBSD Target
213 template<typename Target>
214 class DragonFlyBSDTargetInfo : public OSTargetInfo<Target> {
215 protected:
216   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
217                             MacroBuilder &Builder) const {
218     // DragonFly defines; list based off of gcc output
219     Builder.defineMacro("__DragonFly__");
220     Builder.defineMacro("__DragonFly_cc_version", "100001");
221     Builder.defineMacro("__ELF__");
222     Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
223     Builder.defineMacro("__tune_i386__");
224     DefineStd(Builder, "unix", Opts);
225   }
226 public:
227   DragonFlyBSDTargetInfo(const std::string &triple)
228     : OSTargetInfo<Target>(triple) {
229       this->UserLabelPrefix = "";
230 
231       llvm::Triple Triple(triple);
232       switch (Triple.getArch()) {
233         default:
234         case llvm::Triple::x86:
235         case llvm::Triple::x86_64:
236           this->MCountName = ".mcount";
237           break;
238       }
239   }
240 };
241 
242 // FreeBSD Target
243 template<typename Target>
244 class FreeBSDTargetInfo : public OSTargetInfo<Target> {
245 protected:
246   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
247                             MacroBuilder &Builder) const {
248     // FreeBSD defines; list based off of gcc output
249 
250     unsigned Release = Triple.getOSMajorVersion();
251     if (Release == 0U)
252       Release = 8;
253 
254     Builder.defineMacro("__FreeBSD__", Twine(Release));
255     Builder.defineMacro("__FreeBSD_cc_version", Twine(Release * 100000U + 1U));
256     Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
257     DefineStd(Builder, "unix", Opts);
258     Builder.defineMacro("__ELF__");
259   }
260 public:
261   FreeBSDTargetInfo(const std::string &triple)
262     : OSTargetInfo<Target>(triple) {
263       this->UserLabelPrefix = "";
264 
265       llvm::Triple Triple(triple);
266       switch (Triple.getArch()) {
267         default:
268         case llvm::Triple::x86:
269         case llvm::Triple::x86_64:
270           this->MCountName = ".mcount";
271           break;
272         case llvm::Triple::mips:
273         case llvm::Triple::mipsel:
274         case llvm::Triple::ppc:
275         case llvm::Triple::ppc64:
276           this->MCountName = "_mcount";
277           break;
278         case llvm::Triple::arm:
279           this->MCountName = "__mcount";
280           break;
281       }
282 
283     }
284 };
285 
286 // Minix Target
287 template<typename Target>
288 class MinixTargetInfo : public OSTargetInfo<Target> {
289 protected:
290   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
291                             MacroBuilder &Builder) const {
292     // Minix defines
293 
294     Builder.defineMacro("__minix", "3");
295     Builder.defineMacro("_EM_WSIZE", "4");
296     Builder.defineMacro("_EM_PSIZE", "4");
297     Builder.defineMacro("_EM_SSIZE", "2");
298     Builder.defineMacro("_EM_LSIZE", "4");
299     Builder.defineMacro("_EM_FSIZE", "4");
300     Builder.defineMacro("_EM_DSIZE", "8");
301     Builder.defineMacro("__ELF__");
302     DefineStd(Builder, "unix", Opts);
303   }
304 public:
305   MinixTargetInfo(const std::string &triple)
306     : OSTargetInfo<Target>(triple) {
307       this->UserLabelPrefix = "";
308     }
309 };
310 
311 // Linux target
312 template<typename Target>
313 class LinuxTargetInfo : public OSTargetInfo<Target> {
314 protected:
315   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
316                             MacroBuilder &Builder) const {
317     // Linux defines; list based off of gcc output
318     DefineStd(Builder, "unix", Opts);
319     DefineStd(Builder, "linux", Opts);
320     Builder.defineMacro("__gnu_linux__");
321     Builder.defineMacro("__ELF__");
322     if (Triple.getEnvironment() == llvm::Triple::Android)
323       Builder.defineMacro("__ANDROID__", "1");
324     if (Opts.POSIXThreads)
325       Builder.defineMacro("_REENTRANT");
326     if (Opts.CPlusPlus)
327       Builder.defineMacro("_GNU_SOURCE");
328   }
329 public:
330   LinuxTargetInfo(const std::string& triple)
331     : OSTargetInfo<Target>(triple) {
332     this->UserLabelPrefix = "";
333     this->WIntType = TargetInfo::UnsignedInt;
334   }
335 
336   virtual const char *getStaticInitSectionSpecifier() const {
337     return ".text.startup";
338   }
339 };
340 
341 // NetBSD Target
342 template<typename Target>
343 class NetBSDTargetInfo : public OSTargetInfo<Target> {
344 protected:
345   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
346                             MacroBuilder &Builder) const {
347     // NetBSD defines; list based off of gcc output
348     Builder.defineMacro("__NetBSD__");
349     Builder.defineMacro("__unix__");
350     Builder.defineMacro("__ELF__");
351     if (Opts.POSIXThreads)
352       Builder.defineMacro("_POSIX_THREADS");
353   }
354 public:
355   NetBSDTargetInfo(const std::string &triple)
356     : OSTargetInfo<Target>(triple) {
357       this->UserLabelPrefix = "";
358     }
359 };
360 
361 // OpenBSD Target
362 template<typename Target>
363 class OpenBSDTargetInfo : public OSTargetInfo<Target> {
364 protected:
365   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
366                             MacroBuilder &Builder) const {
367     // OpenBSD defines; list based off of gcc output
368 
369     Builder.defineMacro("__OpenBSD__");
370     DefineStd(Builder, "unix", Opts);
371     Builder.defineMacro("__ELF__");
372     if (Opts.POSIXThreads)
373       Builder.defineMacro("_REENTRANT");
374   }
375 public:
376   OpenBSDTargetInfo(const std::string &triple)
377     : OSTargetInfo<Target>(triple) {
378       this->UserLabelPrefix = "";
379       this->TLSSupported = false;
380 
381       llvm::Triple Triple(triple);
382       switch (Triple.getArch()) {
383         default:
384         case llvm::Triple::x86:
385         case llvm::Triple::x86_64:
386         case llvm::Triple::arm:
387         case llvm::Triple::sparc:
388           this->MCountName = "__mcount";
389           break;
390         case llvm::Triple::mips64:
391         case llvm::Triple::mips64el:
392         case llvm::Triple::ppc:
393         case llvm::Triple::sparcv9:
394           this->MCountName = "_mcount";
395           break;
396       }
397   }
398 };
399 
400 // Bitrig Target
401 template<typename Target>
402 class BitrigTargetInfo : public OSTargetInfo<Target> {
403 protected:
404   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
405                             MacroBuilder &Builder) const {
406     // Bitrig defines; list based off of gcc output
407 
408     Builder.defineMacro("__Bitrig__");
409     DefineStd(Builder, "unix", Opts);
410     Builder.defineMacro("__ELF__");
411     if (Opts.POSIXThreads)
412       Builder.defineMacro("_REENTRANT");
413   }
414 public:
415   BitrigTargetInfo(const std::string &triple)
416     : OSTargetInfo<Target>(triple) {
417       this->UserLabelPrefix = "";
418       this->TLSSupported = false;
419       this->MCountName = "__mcount";
420   }
421 };
422 
423 // PSP Target
424 template<typename Target>
425 class PSPTargetInfo : public OSTargetInfo<Target> {
426 protected:
427   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
428                             MacroBuilder &Builder) const {
429     // PSP defines; list based on the output of the pspdev gcc toolchain.
430     Builder.defineMacro("PSP");
431     Builder.defineMacro("_PSP");
432     Builder.defineMacro("__psp__");
433     Builder.defineMacro("__ELF__");
434   }
435 public:
436   PSPTargetInfo(const std::string& triple)
437     : OSTargetInfo<Target>(triple) {
438     this->UserLabelPrefix = "";
439   }
440 };
441 
442 // PS3 PPU Target
443 template<typename Target>
444 class PS3PPUTargetInfo : public OSTargetInfo<Target> {
445 protected:
446   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
447                             MacroBuilder &Builder) const {
448     // PS3 PPU defines.
449     Builder.defineMacro("__PPC__");
450     Builder.defineMacro("__PPU__");
451     Builder.defineMacro("__CELLOS_LV2__");
452     Builder.defineMacro("__ELF__");
453     Builder.defineMacro("__LP32__");
454     Builder.defineMacro("_ARCH_PPC64");
455     Builder.defineMacro("__powerpc64__");
456   }
457 public:
458   PS3PPUTargetInfo(const std::string& triple)
459     : OSTargetInfo<Target>(triple) {
460     this->UserLabelPrefix = "";
461     this->LongWidth = this->LongAlign = 32;
462     this->PointerWidth = this->PointerAlign = 32;
463     this->IntMaxType = TargetInfo::SignedLongLong;
464     this->UIntMaxType = TargetInfo::UnsignedLongLong;
465     this->Int64Type = TargetInfo::SignedLongLong;
466     this->SizeType = TargetInfo::UnsignedInt;
467     this->DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
468                               "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
469   }
470 };
471 
472 // FIXME: Need a real SPU target.
473 // PS3 SPU Target
474 template<typename Target>
475 class PS3SPUTargetInfo : public OSTargetInfo<Target> {
476 protected:
477   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
478                             MacroBuilder &Builder) const {
479     // PS3 PPU defines.
480     Builder.defineMacro("__SPU__");
481     Builder.defineMacro("__ELF__");
482   }
483 public:
484   PS3SPUTargetInfo(const std::string& triple)
485     : OSTargetInfo<Target>(triple) {
486     this->UserLabelPrefix = "";
487   }
488 };
489 
490 // AuroraUX target
491 template<typename Target>
492 class AuroraUXTargetInfo : public OSTargetInfo<Target> {
493 protected:
494   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
495                             MacroBuilder &Builder) const {
496     DefineStd(Builder, "sun", Opts);
497     DefineStd(Builder, "unix", Opts);
498     Builder.defineMacro("__ELF__");
499     Builder.defineMacro("__svr4__");
500     Builder.defineMacro("__SVR4");
501   }
502 public:
503   AuroraUXTargetInfo(const std::string& triple)
504     : OSTargetInfo<Target>(triple) {
505     this->UserLabelPrefix = "";
506     this->WCharType = this->SignedLong;
507     // FIXME: WIntType should be SignedLong
508   }
509 };
510 
511 // Solaris target
512 template<typename Target>
513 class SolarisTargetInfo : public OSTargetInfo<Target> {
514 protected:
515   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
516                             MacroBuilder &Builder) const {
517     DefineStd(Builder, "sun", Opts);
518     DefineStd(Builder, "unix", Opts);
519     Builder.defineMacro("__ELF__");
520     Builder.defineMacro("__svr4__");
521     Builder.defineMacro("__SVR4");
522     // Solaris headers require _XOPEN_SOURCE to be set to 600 for C99 and
523     // newer, but to 500 for everything else.  feature_test.h has a check to
524     // ensure that you are not using C99 with an old version of X/Open or C89
525     // with a new version.
526     if (Opts.C99 || Opts.C11)
527       Builder.defineMacro("_XOPEN_SOURCE", "600");
528     else
529       Builder.defineMacro("_XOPEN_SOURCE", "500");
530     if (Opts.CPlusPlus)
531       Builder.defineMacro("__C99FEATURES__");
532     Builder.defineMacro("_LARGEFILE_SOURCE");
533     Builder.defineMacro("_LARGEFILE64_SOURCE");
534     Builder.defineMacro("__EXTENSIONS__");
535     Builder.defineMacro("_REENTRANT");
536   }
537 public:
538   SolarisTargetInfo(const std::string& triple)
539     : OSTargetInfo<Target>(triple) {
540     this->UserLabelPrefix = "";
541     this->WCharType = this->SignedInt;
542     // FIXME: WIntType should be SignedLong
543   }
544 };
545 
546 // Windows target
547 template<typename Target>
548 class WindowsTargetInfo : public OSTargetInfo<Target> {
549 protected:
550   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
551                             MacroBuilder &Builder) const {
552     Builder.defineMacro("_WIN32");
553   }
554   void getVisualStudioDefines(const LangOptions &Opts,
555                               MacroBuilder &Builder) const {
556     if (Opts.CPlusPlus) {
557       if (Opts.RTTI)
558         Builder.defineMacro("_CPPRTTI");
559 
560       if (Opts.Exceptions)
561         Builder.defineMacro("_CPPUNWIND");
562     }
563 
564     if (!Opts.CharIsSigned)
565       Builder.defineMacro("_CHAR_UNSIGNED");
566 
567     // FIXME: POSIXThreads isn't exactly the option this should be defined for,
568     //        but it works for now.
569     if (Opts.POSIXThreads)
570       Builder.defineMacro("_MT");
571 
572     if (Opts.MSCVersion != 0)
573       Builder.defineMacro("_MSC_VER", Twine(Opts.MSCVersion));
574 
575     if (Opts.MicrosoftExt) {
576       Builder.defineMacro("_MSC_EXTENSIONS");
577 
578       if (Opts.CPlusPlus11) {
579         Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
580         Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
581         Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
582       }
583     }
584 
585     Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
586   }
587 
588 public:
589   WindowsTargetInfo(const std::string &triple)
590     : OSTargetInfo<Target>(triple) {}
591 };
592 
593 template <typename Target>
594 class NaClTargetInfo : public OSTargetInfo<Target> {
595  protected:
596   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
597                             MacroBuilder &Builder) const {
598     if (Opts.POSIXThreads)
599       Builder.defineMacro("_REENTRANT");
600     if (Opts.CPlusPlus)
601       Builder.defineMacro("_GNU_SOURCE");
602 
603     DefineStd(Builder, "unix", Opts);
604     Builder.defineMacro("__ELF__");
605     Builder.defineMacro("__native_client__");
606   }
607  public:
608   NaClTargetInfo(const std::string &triple)
609     : OSTargetInfo<Target>(triple) {
610     this->UserLabelPrefix = "";
611     this->LongAlign = 32;
612     this->LongWidth = 32;
613     this->PointerAlign = 32;
614     this->PointerWidth = 32;
615     this->IntMaxType = TargetInfo::SignedLongLong;
616     this->UIntMaxType = TargetInfo::UnsignedLongLong;
617     this->Int64Type = TargetInfo::SignedLongLong;
618     this->DoubleAlign = 64;
619     this->LongDoubleWidth = 64;
620     this->LongDoubleAlign = 64;
621     this->SizeType = TargetInfo::UnsignedInt;
622     this->PtrDiffType = TargetInfo::SignedInt;
623     this->IntPtrType = TargetInfo::SignedInt;
624     // RegParmMax is inherited from the underlying architecture
625     this->LongDoubleFormat = &llvm::APFloat::IEEEdouble;
626     this->DescriptionString = "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
627                               "f32:32:32-f64:64:64-p:32:32:32-v128:32:32";
628   }
629   virtual typename Target::CallingConvCheckResult checkCallingConvention(
630       CallingConv CC) const {
631     return CC == CC_PnaclCall ? Target::CCCR_OK :
632         Target::checkCallingConvention(CC);
633   }
634 };
635 } // end anonymous namespace.
636 
637 //===----------------------------------------------------------------------===//
638 // Specific target implementations.
639 //===----------------------------------------------------------------------===//
640 
641 namespace {
642 // PPC abstract base class
643 class PPCTargetInfo : public TargetInfo {
644   static const Builtin::Info BuiltinInfo[];
645   static const char * const GCCRegNames[];
646   static const TargetInfo::GCCRegAlias GCCRegAliases[];
647   std::string CPU;
648 public:
649   PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {
650     LongDoubleWidth = LongDoubleAlign = 128;
651     LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble;
652   }
653 
654   /// \brief Flags for architecture specific defines.
655   typedef enum {
656     ArchDefineNone  = 0,
657     ArchDefineName  = 1 << 0, // <name> is substituted for arch name.
658     ArchDefinePpcgr = 1 << 1,
659     ArchDefinePpcsq = 1 << 2,
660     ArchDefine440   = 1 << 3,
661     ArchDefine603   = 1 << 4,
662     ArchDefine604   = 1 << 5,
663     ArchDefinePwr4  = 1 << 6,
664     ArchDefinePwr5  = 1 << 7,
665     ArchDefinePwr5x = 1 << 8,
666     ArchDefinePwr6  = 1 << 9,
667     ArchDefinePwr6x = 1 << 10,
668     ArchDefinePwr7  = 1 << 11,
669     ArchDefineA2    = 1 << 12,
670     ArchDefineA2q   = 1 << 13
671   } ArchDefineTypes;
672 
673   // Note: GCC recognizes the following additional cpus:
674   //  401, 403, 405, 405fp, 440fp, 464, 464fp, 476, 476fp, 505, 740, 801,
675   //  821, 823, 8540, 8548, e300c2, e300c3, e500mc64, e6500, 860, cell,
676   //  titan, rs64.
677   virtual bool setCPU(const std::string &Name) {
678     bool CPUKnown = llvm::StringSwitch<bool>(Name)
679       .Case("generic", true)
680       .Case("440", true)
681       .Case("450", true)
682       .Case("601", true)
683       .Case("602", true)
684       .Case("603", true)
685       .Case("603e", true)
686       .Case("603ev", true)
687       .Case("604", true)
688       .Case("604e", true)
689       .Case("620", true)
690       .Case("630", true)
691       .Case("g3", true)
692       .Case("7400", true)
693       .Case("g4", true)
694       .Case("7450", true)
695       .Case("g4+", true)
696       .Case("750", true)
697       .Case("970", true)
698       .Case("g5", true)
699       .Case("a2", true)
700       .Case("a2q", true)
701       .Case("e500mc", true)
702       .Case("e5500", true)
703       .Case("power3", true)
704       .Case("pwr3", true)
705       .Case("power4", true)
706       .Case("pwr4", true)
707       .Case("power5", true)
708       .Case("pwr5", true)
709       .Case("power5x", true)
710       .Case("pwr5x", true)
711       .Case("power6", true)
712       .Case("pwr6", true)
713       .Case("power6x", true)
714       .Case("pwr6x", true)
715       .Case("power7", true)
716       .Case("pwr7", true)
717       .Case("powerpc", true)
718       .Case("ppc", true)
719       .Case("powerpc64", true)
720       .Case("ppc64", true)
721       .Default(false);
722 
723     if (CPUKnown)
724       CPU = Name;
725 
726     return CPUKnown;
727   }
728 
729   virtual void getTargetBuiltins(const Builtin::Info *&Records,
730                                  unsigned &NumRecords) const {
731     Records = BuiltinInfo;
732     NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
733   }
734 
735   virtual bool isCLZForZeroUndef() const { return false; }
736 
737   virtual void getTargetDefines(const LangOptions &Opts,
738                                 MacroBuilder &Builder) const;
739 
740   virtual void getDefaultFeatures(llvm::StringMap<bool> &Features) const;
741 
742   virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
743                                  StringRef Name,
744                                  bool Enabled) const;
745 
746   virtual bool hasFeature(StringRef Feature) const;
747 
748   virtual void getGCCRegNames(const char * const *&Names,
749                               unsigned &NumNames) const;
750   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
751                                 unsigned &NumAliases) const;
752   virtual bool validateAsmConstraint(const char *&Name,
753                                      TargetInfo::ConstraintInfo &Info) const {
754     switch (*Name) {
755     default: return false;
756     case 'O': // Zero
757       break;
758     case 'b': // Base register
759     case 'f': // Floating point register
760       Info.setAllowsRegister();
761       break;
762     // FIXME: The following are added to allow parsing.
763     // I just took a guess at what the actions should be.
764     // Also, is more specific checking needed?  I.e. specific registers?
765     case 'd': // Floating point register (containing 64-bit value)
766     case 'v': // Altivec vector register
767       Info.setAllowsRegister();
768       break;
769     case 'w':
770       switch (Name[1]) {
771         case 'd':// VSX vector register to hold vector double data
772         case 'f':// VSX vector register to hold vector float data
773         case 's':// VSX vector register to hold scalar float data
774         case 'a':// Any VSX register
775           break;
776         default:
777           return false;
778       }
779       Info.setAllowsRegister();
780       Name++; // Skip over 'w'.
781       break;
782     case 'h': // `MQ', `CTR', or `LINK' register
783     case 'q': // `MQ' register
784     case 'c': // `CTR' register
785     case 'l': // `LINK' register
786     case 'x': // `CR' register (condition register) number 0
787     case 'y': // `CR' register (condition register)
788     case 'z': // `XER[CA]' carry bit (part of the XER register)
789       Info.setAllowsRegister();
790       break;
791     case 'I': // Signed 16-bit constant
792     case 'J': // Unsigned 16-bit constant shifted left 16 bits
793               //  (use `L' instead for SImode constants)
794     case 'K': // Unsigned 16-bit constant
795     case 'L': // Signed 16-bit constant shifted left 16 bits
796     case 'M': // Constant larger than 31
797     case 'N': // Exact power of 2
798     case 'P': // Constant whose negation is a signed 16-bit constant
799     case 'G': // Floating point constant that can be loaded into a
800               // register with one instruction per word
801     case 'H': // Integer/Floating point constant that can be loaded
802               // into a register using three instructions
803       break;
804     case 'm': // Memory operand. Note that on PowerPC targets, m can
805               // include addresses that update the base register. It
806               // is therefore only safe to use `m' in an asm statement
807               // if that asm statement accesses the operand exactly once.
808               // The asm statement must also use `%U<opno>' as a
809               // placeholder for the "update" flag in the corresponding
810               // load or store instruction. For example:
811               // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
812               // is correct but:
813               // asm ("st %1,%0" : "=m" (mem) : "r" (val));
814               // is not. Use es rather than m if you don't want the base
815               // register to be updated.
816     case 'e':
817       if (Name[1] != 's')
818           return false;
819               // es: A "stable" memory operand; that is, one which does not
820               // include any automodification of the base register. Unlike
821               // `m', this constraint can be used in asm statements that
822               // might access the operand several times, or that might not
823               // access it at all.
824       Info.setAllowsMemory();
825       Name++; // Skip over 'e'.
826       break;
827     case 'Q': // Memory operand that is an offset from a register (it is
828               // usually better to use `m' or `es' in asm statements)
829     case 'Z': // Memory operand that is an indexed or indirect from a
830               // register (it is usually better to use `m' or `es' in
831               // asm statements)
832       Info.setAllowsMemory();
833       Info.setAllowsRegister();
834       break;
835     case 'R': // AIX TOC entry
836     case 'a': // Address operand that is an indexed or indirect from a
837               // register (`p' is preferable for asm statements)
838     case 'S': // Constant suitable as a 64-bit mask operand
839     case 'T': // Constant suitable as a 32-bit mask operand
840     case 'U': // System V Release 4 small data area reference
841     case 't': // AND masks that can be performed by two rldic{l, r}
842               // instructions
843     case 'W': // Vector constant that does not require memory
844     case 'j': // Vector constant that is all zeros.
845       break;
846     // End FIXME.
847     }
848     return true;
849   }
850   virtual const char *getClobbers() const {
851     return "";
852   }
853   int getEHDataRegisterNumber(unsigned RegNo) const {
854     if (RegNo == 0) return 3;
855     if (RegNo == 1) return 4;
856     return -1;
857   }
858 };
859 
860 const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
861 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
862 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
863                                               ALL_LANGUAGES },
864 #include "clang/Basic/BuiltinsPPC.def"
865 };
866 
867 
868 /// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
869 /// #defines that are not tied to a specific subtarget.
870 void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
871                                      MacroBuilder &Builder) const {
872   // Target identification.
873   Builder.defineMacro("__ppc__");
874   Builder.defineMacro("_ARCH_PPC");
875   Builder.defineMacro("__powerpc__");
876   Builder.defineMacro("__POWERPC__");
877   if (PointerWidth == 64) {
878     Builder.defineMacro("_ARCH_PPC64");
879     Builder.defineMacro("__powerpc64__");
880     Builder.defineMacro("__ppc64__");
881   } else {
882     Builder.defineMacro("__ppc__");
883   }
884 
885   // Target properties.
886   if (getTriple().getOS() != llvm::Triple::NetBSD &&
887       getTriple().getOS() != llvm::Triple::OpenBSD)
888     Builder.defineMacro("_BIG_ENDIAN");
889   Builder.defineMacro("__BIG_ENDIAN__");
890 
891   // Subtarget options.
892   Builder.defineMacro("__NATURAL_ALIGNMENT__");
893   Builder.defineMacro("__REGISTER_PREFIX__", "");
894 
895   // FIXME: Should be controlled by command line option.
896   Builder.defineMacro("__LONG_DOUBLE_128__");
897 
898   if (Opts.AltiVec) {
899     Builder.defineMacro("__VEC__", "10206");
900     Builder.defineMacro("__ALTIVEC__");
901   }
902 
903   // CPU identification.
904   ArchDefineTypes defs = (ArchDefineTypes)llvm::StringSwitch<int>(CPU)
905     .Case("440",   ArchDefineName)
906     .Case("450",   ArchDefineName | ArchDefine440)
907     .Case("601",   ArchDefineName)
908     .Case("602",   ArchDefineName | ArchDefinePpcgr)
909     .Case("603",   ArchDefineName | ArchDefinePpcgr)
910     .Case("603e",  ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
911     .Case("603ev", ArchDefineName | ArchDefine603 | ArchDefinePpcgr)
912     .Case("604",   ArchDefineName | ArchDefinePpcgr)
913     .Case("604e",  ArchDefineName | ArchDefine604 | ArchDefinePpcgr)
914     .Case("620",   ArchDefineName | ArchDefinePpcgr)
915     .Case("630",   ArchDefineName | ArchDefinePpcgr)
916     .Case("7400",  ArchDefineName | ArchDefinePpcgr)
917     .Case("7450",  ArchDefineName | ArchDefinePpcgr)
918     .Case("750",   ArchDefineName | ArchDefinePpcgr)
919     .Case("970",   ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr
920                      | ArchDefinePpcsq)
921     .Case("a2",    ArchDefineA2)
922     .Case("a2q",   ArchDefineName | ArchDefineA2 | ArchDefineA2q)
923     .Case("pwr3",  ArchDefinePpcgr)
924     .Case("pwr4",  ArchDefineName | ArchDefinePpcgr | ArchDefinePpcsq)
925     .Case("pwr5",  ArchDefineName | ArchDefinePwr4 | ArchDefinePpcgr
926                      | ArchDefinePpcsq)
927     .Case("pwr5x", ArchDefineName | ArchDefinePwr5 | ArchDefinePwr4
928                      | ArchDefinePpcgr | ArchDefinePpcsq)
929     .Case("pwr6",  ArchDefineName | ArchDefinePwr5x | ArchDefinePwr5
930                      | ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
931     .Case("pwr6x", ArchDefineName | ArchDefinePwr6 | ArchDefinePwr5x
932                      | ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr
933                      | ArchDefinePpcsq)
934     .Case("pwr7",  ArchDefineName | ArchDefinePwr6x | ArchDefinePwr6
935                      | ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4
936                      | ArchDefinePwr6 | ArchDefinePpcgr | ArchDefinePpcsq)
937     .Case("power3",  ArchDefinePpcgr)
938     .Case("power4",  ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
939     .Case("power5",  ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr
940                        | ArchDefinePpcsq)
941     .Case("power5x", ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4
942                        | ArchDefinePpcgr | ArchDefinePpcsq)
943     .Case("power6",  ArchDefinePwr6 | ArchDefinePwr5x | ArchDefinePwr5
944                        | ArchDefinePwr4 | ArchDefinePpcgr | ArchDefinePpcsq)
945     .Case("power6x", ArchDefinePwr6x | ArchDefinePwr6 | ArchDefinePwr5x
946                        | ArchDefinePwr5 | ArchDefinePwr4 | ArchDefinePpcgr
947                        | ArchDefinePpcsq)
948     .Case("power7",  ArchDefinePwr7 | ArchDefinePwr6x | ArchDefinePwr6
949                        | ArchDefinePwr5x | ArchDefinePwr5 | ArchDefinePwr4
950                        | ArchDefinePwr6 | ArchDefinePpcgr | ArchDefinePpcsq)
951     .Default(ArchDefineNone);
952 
953   if (defs & ArchDefineName)
954     Builder.defineMacro(Twine("_ARCH_", StringRef(CPU).upper()));
955   if (defs & ArchDefinePpcgr)
956     Builder.defineMacro("_ARCH_PPCGR");
957   if (defs & ArchDefinePpcsq)
958     Builder.defineMacro("_ARCH_PPCSQ");
959   if (defs & ArchDefine440)
960     Builder.defineMacro("_ARCH_440");
961   if (defs & ArchDefine603)
962     Builder.defineMacro("_ARCH_603");
963   if (defs & ArchDefine604)
964     Builder.defineMacro("_ARCH_604");
965   if (defs & ArchDefinePwr4)
966     Builder.defineMacro("_ARCH_PWR4");
967   if (defs & ArchDefinePwr5)
968     Builder.defineMacro("_ARCH_PWR5");
969   if (defs & ArchDefinePwr5x)
970     Builder.defineMacro("_ARCH_PWR5X");
971   if (defs & ArchDefinePwr6)
972     Builder.defineMacro("_ARCH_PWR6");
973   if (defs & ArchDefinePwr6x)
974     Builder.defineMacro("_ARCH_PWR6X");
975   if (defs & ArchDefinePwr7)
976     Builder.defineMacro("_ARCH_PWR7");
977   if (defs & ArchDefineA2)
978     Builder.defineMacro("_ARCH_A2");
979   if (defs & ArchDefineA2q) {
980     Builder.defineMacro("_ARCH_A2Q");
981     Builder.defineMacro("_ARCH_QP");
982   }
983 
984   if (getTriple().getVendor() == llvm::Triple::BGQ) {
985     Builder.defineMacro("__bg__");
986     Builder.defineMacro("__THW_BLUEGENE__");
987     Builder.defineMacro("__bgq__");
988     Builder.defineMacro("__TOS_BGQ__");
989   }
990 
991   // FIXME: The following are not yet generated here by Clang, but are
992   //        generated by GCC:
993   //
994   //   _SOFT_FLOAT_
995   //   __RECIP_PRECISION__
996   //   __APPLE_ALTIVEC__
997   //   __VSX__
998   //   __RECIP__
999   //   __RECIPF__
1000   //   __RSQRTE__
1001   //   __RSQRTEF__
1002   //   _SOFT_DOUBLE_
1003   //   __NO_LWSYNC__
1004   //   __HAVE_BSWAP__
1005   //   __LONGDOUBLE128
1006   //   __CMODEL_MEDIUM__
1007   //   __CMODEL_LARGE__
1008   //   _CALL_SYSV
1009   //   _CALL_DARWIN
1010   //   __NO_FPRS__
1011 }
1012 
1013 void PPCTargetInfo::getDefaultFeatures(llvm::StringMap<bool> &Features) const {
1014   Features["altivec"] = llvm::StringSwitch<bool>(CPU)
1015     .Case("7400", true)
1016     .Case("g4", true)
1017     .Case("7450", true)
1018     .Case("g4+", true)
1019     .Case("970", true)
1020     .Case("g5", true)
1021     .Case("pwr6", true)
1022     .Case("pwr7", true)
1023     .Case("ppc64", true)
1024     .Default(false);
1025 
1026   Features["qpx"] = (CPU == "a2q");
1027 }
1028 
1029 bool PPCTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1030                                          StringRef Name,
1031                                          bool Enabled) const {
1032   if (Name == "altivec" || Name == "fprnd" || Name == "mfocrf" ||
1033       Name == "popcntd" || Name == "qpx") {
1034     Features[Name] = Enabled;
1035     return true;
1036   }
1037 
1038   return false;
1039 }
1040 
1041 bool PPCTargetInfo::hasFeature(StringRef Feature) const {
1042   return Feature == "powerpc";
1043 }
1044 
1045 
1046 const char * const PPCTargetInfo::GCCRegNames[] = {
1047   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1048   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1049   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
1050   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
1051   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
1052   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
1053   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
1054   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
1055   "mq", "lr", "ctr", "ap",
1056   "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
1057   "xer",
1058   "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
1059   "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
1060   "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
1061   "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
1062   "vrsave", "vscr",
1063   "spe_acc", "spefscr",
1064   "sfp"
1065 };
1066 
1067 void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
1068                                    unsigned &NumNames) const {
1069   Names = GCCRegNames;
1070   NumNames = llvm::array_lengthof(GCCRegNames);
1071 }
1072 
1073 const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
1074   // While some of these aliases do map to different registers
1075   // they still share the same register name.
1076   { { "0" }, "r0" },
1077   { { "1"}, "r1" },
1078   { { "2" }, "r2" },
1079   { { "3" }, "r3" },
1080   { { "4" }, "r4" },
1081   { { "5" }, "r5" },
1082   { { "6" }, "r6" },
1083   { { "7" }, "r7" },
1084   { { "8" }, "r8" },
1085   { { "9" }, "r9" },
1086   { { "10" }, "r10" },
1087   { { "11" }, "r11" },
1088   { { "12" }, "r12" },
1089   { { "13" }, "r13" },
1090   { { "14" }, "r14" },
1091   { { "15" }, "r15" },
1092   { { "16" }, "r16" },
1093   { { "17" }, "r17" },
1094   { { "18" }, "r18" },
1095   { { "19" }, "r19" },
1096   { { "20" }, "r20" },
1097   { { "21" }, "r21" },
1098   { { "22" }, "r22" },
1099   { { "23" }, "r23" },
1100   { { "24" }, "r24" },
1101   { { "25" }, "r25" },
1102   { { "26" }, "r26" },
1103   { { "27" }, "r27" },
1104   { { "28" }, "r28" },
1105   { { "29" }, "r29" },
1106   { { "30" }, "r30" },
1107   { { "31" }, "r31" },
1108   { { "fr0" }, "f0" },
1109   { { "fr1" }, "f1" },
1110   { { "fr2" }, "f2" },
1111   { { "fr3" }, "f3" },
1112   { { "fr4" }, "f4" },
1113   { { "fr5" }, "f5" },
1114   { { "fr6" }, "f6" },
1115   { { "fr7" }, "f7" },
1116   { { "fr8" }, "f8" },
1117   { { "fr9" }, "f9" },
1118   { { "fr10" }, "f10" },
1119   { { "fr11" }, "f11" },
1120   { { "fr12" }, "f12" },
1121   { { "fr13" }, "f13" },
1122   { { "fr14" }, "f14" },
1123   { { "fr15" }, "f15" },
1124   { { "fr16" }, "f16" },
1125   { { "fr17" }, "f17" },
1126   { { "fr18" }, "f18" },
1127   { { "fr19" }, "f19" },
1128   { { "fr20" }, "f20" },
1129   { { "fr21" }, "f21" },
1130   { { "fr22" }, "f22" },
1131   { { "fr23" }, "f23" },
1132   { { "fr24" }, "f24" },
1133   { { "fr25" }, "f25" },
1134   { { "fr26" }, "f26" },
1135   { { "fr27" }, "f27" },
1136   { { "fr28" }, "f28" },
1137   { { "fr29" }, "f29" },
1138   { { "fr30" }, "f30" },
1139   { { "fr31" }, "f31" },
1140   { { "cc" }, "cr0" },
1141 };
1142 
1143 void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1144                                      unsigned &NumAliases) const {
1145   Aliases = GCCRegAliases;
1146   NumAliases = llvm::array_lengthof(GCCRegAliases);
1147 }
1148 } // end anonymous namespace.
1149 
1150 namespace {
1151 class PPC32TargetInfo : public PPCTargetInfo {
1152 public:
1153   PPC32TargetInfo(const std::string &triple) : PPCTargetInfo(triple) {
1154     DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1155                         "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
1156 
1157     switch (getTriple().getOS()) {
1158     case llvm::Triple::Linux:
1159     case llvm::Triple::FreeBSD:
1160     case llvm::Triple::NetBSD:
1161       SizeType = UnsignedInt;
1162       PtrDiffType = SignedInt;
1163       IntPtrType = SignedInt;
1164       break;
1165     default:
1166       break;
1167     }
1168 
1169     if (getTriple().getOS() == llvm::Triple::FreeBSD) {
1170       LongDoubleWidth = LongDoubleAlign = 64;
1171       LongDoubleFormat = &llvm::APFloat::IEEEdouble;
1172     }
1173 
1174     // PPC32 supports atomics up to 4 bytes.
1175     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
1176   }
1177 
1178   virtual BuiltinVaListKind getBuiltinVaListKind() const {
1179     // This is the ELF definition, and is overridden by the Darwin sub-target
1180     return TargetInfo::PowerABIBuiltinVaList;
1181   }
1182 };
1183 } // end anonymous namespace.
1184 
1185 namespace {
1186 class PPC64TargetInfo : public PPCTargetInfo {
1187 public:
1188   PPC64TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
1189     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
1190     IntMaxType = SignedLong;
1191     UIntMaxType = UnsignedLong;
1192     Int64Type = SignedLong;
1193 
1194     if (getTriple().getOS() == llvm::Triple::FreeBSD) {
1195       LongDoubleWidth = LongDoubleAlign = 64;
1196       LongDoubleFormat = &llvm::APFloat::IEEEdouble;
1197       DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1198                           "i64:64:64-f32:32:32-f64:64:64-f128:64:64-"
1199                           "v128:128:128-n32:64";
1200     } else
1201       DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1202                           "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
1203                           "v128:128:128-n32:64";
1204 
1205     // PPC64 supports atomics up to 8 bytes.
1206     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
1207   }
1208   virtual BuiltinVaListKind getBuiltinVaListKind() const {
1209     return TargetInfo::CharPtrBuiltinVaList;
1210   }
1211 };
1212 } // end anonymous namespace.
1213 
1214 
1215 namespace {
1216 class DarwinPPC32TargetInfo :
1217   public DarwinTargetInfo<PPC32TargetInfo> {
1218 public:
1219   DarwinPPC32TargetInfo(const std::string& triple)
1220     : DarwinTargetInfo<PPC32TargetInfo>(triple) {
1221     HasAlignMac68kSupport = true;
1222     BoolWidth = BoolAlign = 32; //XXX support -mone-byte-bool?
1223     PtrDiffType = SignedInt;	// for http://llvm.org/bugs/show_bug.cgi?id=15726
1224     LongLongAlign = 32;
1225     SuitableAlign = 128;
1226     DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1227                         "i64:32:64-f32:32:32-f64:64:64-v128:128:128-n32";
1228   }
1229   virtual BuiltinVaListKind getBuiltinVaListKind() const {
1230     return TargetInfo::CharPtrBuiltinVaList;
1231   }
1232 };
1233 
1234 class DarwinPPC64TargetInfo :
1235   public DarwinTargetInfo<PPC64TargetInfo> {
1236 public:
1237   DarwinPPC64TargetInfo(const std::string& triple)
1238     : DarwinTargetInfo<PPC64TargetInfo>(triple) {
1239     HasAlignMac68kSupport = true;
1240     SuitableAlign = 128;
1241     DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1242                         "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32:64";
1243   }
1244 };
1245 } // end anonymous namespace.
1246 
1247 namespace {
1248   static const unsigned NVPTXAddrSpaceMap[] = {
1249     1,    // opencl_global
1250     3,    // opencl_local
1251     4,    // opencl_constant
1252     1,    // cuda_device
1253     4,    // cuda_constant
1254     3,    // cuda_shared
1255   };
1256   class NVPTXTargetInfo : public TargetInfo {
1257     static const char * const GCCRegNames[];
1258     static const Builtin::Info BuiltinInfo[];
1259     std::vector<StringRef> AvailableFeatures;
1260   public:
1261     NVPTXTargetInfo(const std::string& triple) : TargetInfo(triple) {
1262       BigEndian = false;
1263       TLSSupported = false;
1264       LongWidth = LongAlign = 64;
1265       AddrSpaceMap = &NVPTXAddrSpaceMap;
1266       // Define available target features
1267       // These must be defined in sorted order!
1268       NoAsmVariants = true;
1269     }
1270     virtual void getTargetDefines(const LangOptions &Opts,
1271                                   MacroBuilder &Builder) const {
1272       Builder.defineMacro("__PTX__");
1273       Builder.defineMacro("__NVPTX__");
1274     }
1275     virtual void getTargetBuiltins(const Builtin::Info *&Records,
1276                                    unsigned &NumRecords) const {
1277       Records = BuiltinInfo;
1278       NumRecords = clang::NVPTX::LastTSBuiltin-Builtin::FirstTSBuiltin;
1279     }
1280     virtual bool hasFeature(StringRef Feature) const {
1281       return Feature == "ptx" || Feature == "nvptx";
1282     }
1283 
1284     virtual void getGCCRegNames(const char * const *&Names,
1285                                 unsigned &NumNames) const;
1286     virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1287                                   unsigned &NumAliases) const {
1288       // No aliases.
1289       Aliases = 0;
1290       NumAliases = 0;
1291     }
1292     virtual bool validateAsmConstraint(const char *&Name,
1293                                        TargetInfo::ConstraintInfo &info) const {
1294       // FIXME: implement
1295       return true;
1296     }
1297     virtual const char *getClobbers() const {
1298       // FIXME: Is this really right?
1299       return "";
1300     }
1301     virtual BuiltinVaListKind getBuiltinVaListKind() const {
1302       // FIXME: implement
1303       return TargetInfo::CharPtrBuiltinVaList;
1304     }
1305     virtual bool setCPU(const std::string &Name) {
1306       bool Valid = llvm::StringSwitch<bool>(Name)
1307         .Case("sm_20", true)
1308         .Case("sm_21", true)
1309         .Case("sm_30", true)
1310         .Case("sm_35", true)
1311         .Default(false);
1312 
1313       return Valid;
1314     }
1315     virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1316                                    StringRef Name,
1317                                    bool Enabled) const;
1318   };
1319 
1320   const Builtin::Info NVPTXTargetInfo::BuiltinInfo[] = {
1321 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
1322 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
1323                                               ALL_LANGUAGES },
1324 #include "clang/Basic/BuiltinsNVPTX.def"
1325   };
1326 
1327   const char * const NVPTXTargetInfo::GCCRegNames[] = {
1328     "r0"
1329   };
1330 
1331   void NVPTXTargetInfo::getGCCRegNames(const char * const *&Names,
1332                                      unsigned &NumNames) const {
1333     Names = GCCRegNames;
1334     NumNames = llvm::array_lengthof(GCCRegNames);
1335   }
1336 
1337   bool NVPTXTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1338                                           StringRef Name,
1339                                           bool Enabled) const {
1340     if(std::binary_search(AvailableFeatures.begin(), AvailableFeatures.end(),
1341                           Name)) {
1342       Features[Name] = Enabled;
1343       return true;
1344     } else {
1345       return false;
1346     }
1347   }
1348 
1349   class NVPTX32TargetInfo : public NVPTXTargetInfo {
1350   public:
1351     NVPTX32TargetInfo(const std::string& triple) : NVPTXTargetInfo(triple) {
1352       PointerWidth = PointerAlign = 32;
1353       SizeType     = PtrDiffType = IntPtrType = TargetInfo::UnsignedInt;
1354       DescriptionString
1355         = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
1356           "f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-"
1357           "n16:32:64";
1358   }
1359   };
1360 
1361   class NVPTX64TargetInfo : public NVPTXTargetInfo {
1362   public:
1363     NVPTX64TargetInfo(const std::string& triple) : NVPTXTargetInfo(triple) {
1364       PointerWidth = PointerAlign = 64;
1365       SizeType     = PtrDiffType = IntPtrType = TargetInfo::UnsignedLongLong;
1366       DescriptionString
1367         = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
1368           "f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-"
1369           "n16:32:64";
1370   }
1371   };
1372 }
1373 
1374 namespace {
1375 
1376 static const unsigned R600AddrSpaceMap[] = {
1377   1,    // opencl_global
1378   3,    // opencl_local
1379   2,    // opencl_constant
1380   1,    // cuda_device
1381   2,    // cuda_constant
1382   3     // cuda_shared
1383 };
1384 
1385 static const char *DescriptionStringR600 =
1386   "e"
1387   "-p:32:32:32"
1388   "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32"
1389   "-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128"
1390   "-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-v2048:2048:2048"
1391   "-n32:64";
1392 
1393 static const char *DescriptionStringR600DoubleOps =
1394   "e"
1395   "-p:32:32:32"
1396   "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64"
1397   "-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128"
1398   "-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-v2048:2048:2048"
1399   "-n32:64";
1400 
1401 static const char *DescriptionStringSI =
1402   "e"
1403   "-p:64:64:64"
1404   "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64"
1405   "-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128"
1406   "-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-v2048:2048:2048"
1407   "-n32:64";
1408 
1409 class R600TargetInfo : public TargetInfo {
1410   /// \brief The GPU profiles supported by the R600 target.
1411   enum GPUKind {
1412     GK_NONE,
1413     GK_R600,
1414     GK_R600_DOUBLE_OPS,
1415     GK_R700,
1416     GK_R700_DOUBLE_OPS,
1417     GK_EVERGREEN,
1418     GK_EVERGREEN_DOUBLE_OPS,
1419     GK_NORTHERN_ISLANDS,
1420     GK_CAYMAN,
1421     GK_SOUTHERN_ISLANDS
1422   } GPU;
1423 
1424 public:
1425   R600TargetInfo(const std::string& triple)
1426     : TargetInfo(triple),
1427       GPU(GK_R600) {
1428     DescriptionString = DescriptionStringR600;
1429     AddrSpaceMap = &R600AddrSpaceMap;
1430   }
1431 
1432   virtual const char * getClobbers() const {
1433     return "";
1434   }
1435 
1436   virtual void getGCCRegNames(const char * const *&Names,
1437                               unsigned &numNames) const  {
1438     Names = NULL;
1439     numNames = 0;
1440   }
1441 
1442   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1443                                 unsigned &NumAliases) const {
1444     Aliases = NULL;
1445     NumAliases = 0;
1446   }
1447 
1448   virtual bool validateAsmConstraint(const char *&Name,
1449                                      TargetInfo::ConstraintInfo &info) const {
1450     return true;
1451   }
1452 
1453   virtual void getTargetBuiltins(const Builtin::Info *&Records,
1454                                  unsigned &NumRecords) const {
1455     Records = NULL;
1456     NumRecords = 0;
1457   }
1458 
1459 
1460   virtual void getTargetDefines(const LangOptions &Opts,
1461                                 MacroBuilder &Builder) const {
1462     Builder.defineMacro("__R600__");
1463   }
1464 
1465   virtual BuiltinVaListKind getBuiltinVaListKind() const {
1466     return TargetInfo::CharPtrBuiltinVaList;
1467   }
1468 
1469   virtual bool setCPU(const std::string &Name) {
1470     GPU = llvm::StringSwitch<GPUKind>(Name)
1471       .Case("r600" ,    GK_R600)
1472       .Case("rv610",    GK_R600)
1473       .Case("rv620",    GK_R600)
1474       .Case("rv630",    GK_R600)
1475       .Case("rv635",    GK_R600)
1476       .Case("rs780",    GK_R600)
1477       .Case("rs880",    GK_R600)
1478       .Case("rv670",    GK_R600_DOUBLE_OPS)
1479       .Case("rv710",    GK_R700)
1480       .Case("rv730",    GK_R700)
1481       .Case("rv740",    GK_R700_DOUBLE_OPS)
1482       .Case("rv770",    GK_R700_DOUBLE_OPS)
1483       .Case("palm",     GK_EVERGREEN)
1484       .Case("cedar",    GK_EVERGREEN)
1485       .Case("sumo",     GK_EVERGREEN)
1486       .Case("sumo2",    GK_EVERGREEN)
1487       .Case("redwood",  GK_EVERGREEN)
1488       .Case("juniper",  GK_EVERGREEN)
1489       .Case("hemlock",  GK_EVERGREEN_DOUBLE_OPS)
1490       .Case("cypress",  GK_EVERGREEN_DOUBLE_OPS)
1491       .Case("barts",    GK_NORTHERN_ISLANDS)
1492       .Case("turks",    GK_NORTHERN_ISLANDS)
1493       .Case("caicos",   GK_NORTHERN_ISLANDS)
1494       .Case("cayman",   GK_CAYMAN)
1495       .Case("aruba",    GK_CAYMAN)
1496       .Case("tahiti",   GK_SOUTHERN_ISLANDS)
1497       .Case("pitcairn", GK_SOUTHERN_ISLANDS)
1498       .Case("verde",    GK_SOUTHERN_ISLANDS)
1499       .Case("oland",    GK_SOUTHERN_ISLANDS)
1500       .Default(GK_NONE);
1501 
1502     if (GPU == GK_NONE) {
1503       return false;
1504     }
1505 
1506     // Set the correct data layout
1507     switch (GPU) {
1508     case GK_NONE:
1509     case GK_R600:
1510     case GK_R700:
1511     case GK_EVERGREEN:
1512     case GK_NORTHERN_ISLANDS:
1513       DescriptionString = DescriptionStringR600;
1514       break;
1515     case GK_R600_DOUBLE_OPS:
1516     case GK_R700_DOUBLE_OPS:
1517     case GK_EVERGREEN_DOUBLE_OPS:
1518     case GK_CAYMAN:
1519       DescriptionString = DescriptionStringR600DoubleOps;
1520       break;
1521     case GK_SOUTHERN_ISLANDS:
1522       DescriptionString = DescriptionStringSI;
1523       break;
1524     }
1525 
1526     return true;
1527   }
1528 };
1529 
1530 } // end anonymous namespace
1531 
1532 namespace {
1533 // MBlaze abstract base class
1534 class MBlazeTargetInfo : public TargetInfo {
1535   static const char * const GCCRegNames[];
1536   static const TargetInfo::GCCRegAlias GCCRegAliases[];
1537 
1538 public:
1539   MBlazeTargetInfo(const std::string& triple) : TargetInfo(triple) {
1540     DescriptionString = "E-p:32:32:32-i8:8:8-i16:16:16";
1541   }
1542 
1543   virtual void getTargetBuiltins(const Builtin::Info *&Records,
1544                                  unsigned &NumRecords) const {
1545     // FIXME: Implement.
1546     Records = 0;
1547     NumRecords = 0;
1548   }
1549 
1550   virtual void getTargetDefines(const LangOptions &Opts,
1551                                 MacroBuilder &Builder) const;
1552 
1553   virtual bool hasFeature(StringRef Feature) const {
1554     return Feature == "mblaze";
1555   }
1556 
1557   virtual BuiltinVaListKind getBuiltinVaListKind() const {
1558     return TargetInfo::CharPtrBuiltinVaList;
1559   }
1560   virtual const char *getTargetPrefix() const {
1561     return "mblaze";
1562   }
1563   virtual void getGCCRegNames(const char * const *&Names,
1564                               unsigned &NumNames) const;
1565   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1566                                 unsigned &NumAliases) const;
1567   virtual bool validateAsmConstraint(const char *&Name,
1568                                      TargetInfo::ConstraintInfo &Info) const {
1569     switch (*Name) {
1570     default: return false;
1571     case 'O': // Zero
1572       return true;
1573     case 'b': // Base register
1574     case 'f': // Floating point register
1575       Info.setAllowsRegister();
1576       return true;
1577     }
1578   }
1579   virtual const char *getClobbers() const {
1580     return "";
1581   }
1582 };
1583 
1584 /// MBlazeTargetInfo::getTargetDefines - Return a set of the MBlaze-specific
1585 /// #defines that are not tied to a specific subtarget.
1586 void MBlazeTargetInfo::getTargetDefines(const LangOptions &Opts,
1587                                      MacroBuilder &Builder) const {
1588   // Target identification.
1589   Builder.defineMacro("__microblaze__");
1590   Builder.defineMacro("_ARCH_MICROBLAZE");
1591   Builder.defineMacro("__MICROBLAZE__");
1592 
1593   // Target properties.
1594   Builder.defineMacro("_BIG_ENDIAN");
1595   Builder.defineMacro("__BIG_ENDIAN__");
1596 
1597   // Subtarget options.
1598   Builder.defineMacro("__REGISTER_PREFIX__", "");
1599 }
1600 
1601 
1602 const char * const MBlazeTargetInfo::GCCRegNames[] = {
1603   "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
1604   "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
1605   "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
1606   "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
1607   "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
1608   "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
1609   "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
1610   "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
1611   "hi",   "lo",   "accum","rmsr", "$fcc1","$fcc2","$fcc3","$fcc4",
1612   "$fcc5","$fcc6","$fcc7","$ap",  "$rap", "$frp"
1613 };
1614 
1615 void MBlazeTargetInfo::getGCCRegNames(const char * const *&Names,
1616                                    unsigned &NumNames) const {
1617   Names = GCCRegNames;
1618   NumNames = llvm::array_lengthof(GCCRegNames);
1619 }
1620 
1621 const TargetInfo::GCCRegAlias MBlazeTargetInfo::GCCRegAliases[] = {
1622   { {"f0"},  "r0" },
1623   { {"f1"},  "r1" },
1624   { {"f2"},  "r2" },
1625   { {"f3"},  "r3" },
1626   { {"f4"},  "r4" },
1627   { {"f5"},  "r5" },
1628   { {"f6"},  "r6" },
1629   { {"f7"},  "r7" },
1630   { {"f8"},  "r8" },
1631   { {"f9"},  "r9" },
1632   { {"f10"}, "r10" },
1633   { {"f11"}, "r11" },
1634   { {"f12"}, "r12" },
1635   { {"f13"}, "r13" },
1636   { {"f14"}, "r14" },
1637   { {"f15"}, "r15" },
1638   { {"f16"}, "r16" },
1639   { {"f17"}, "r17" },
1640   { {"f18"}, "r18" },
1641   { {"f19"}, "r19" },
1642   { {"f20"}, "r20" },
1643   { {"f21"}, "r21" },
1644   { {"f22"}, "r22" },
1645   { {"f23"}, "r23" },
1646   { {"f24"}, "r24" },
1647   { {"f25"}, "r25" },
1648   { {"f26"}, "r26" },
1649   { {"f27"}, "r27" },
1650   { {"f28"}, "r28" },
1651   { {"f29"}, "r29" },
1652   { {"f30"}, "r30" },
1653   { {"f31"}, "r31" },
1654 };
1655 
1656 void MBlazeTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1657                                      unsigned &NumAliases) const {
1658   Aliases = GCCRegAliases;
1659   NumAliases = llvm::array_lengthof(GCCRegAliases);
1660 }
1661 } // end anonymous namespace.
1662 
1663 namespace {
1664 // Namespace for x86 abstract base class
1665 const Builtin::Info BuiltinInfo[] = {
1666 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
1667 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
1668                                               ALL_LANGUAGES },
1669 #include "clang/Basic/BuiltinsX86.def"
1670 };
1671 
1672 static const char* const GCCRegNames[] = {
1673   "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
1674   "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
1675   "argp", "flags", "fpcr", "fpsr", "dirflag", "frame",
1676   "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
1677   "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
1678   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1679   "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
1680   "ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7",
1681   "ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15",
1682 };
1683 
1684 const TargetInfo::AddlRegName AddlRegNames[] = {
1685   { { "al", "ah", "eax", "rax" }, 0 },
1686   { { "bl", "bh", "ebx", "rbx" }, 3 },
1687   { { "cl", "ch", "ecx", "rcx" }, 2 },
1688   { { "dl", "dh", "edx", "rdx" }, 1 },
1689   { { "esi", "rsi" }, 4 },
1690   { { "edi", "rdi" }, 5 },
1691   { { "esp", "rsp" }, 7 },
1692   { { "ebp", "rbp" }, 6 },
1693 };
1694 
1695 // X86 target abstract base class; x86-32 and x86-64 are very close, so
1696 // most of the implementation can be shared.
1697 class X86TargetInfo : public TargetInfo {
1698   enum X86SSEEnum {
1699     NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2
1700   } SSELevel;
1701   enum MMX3DNowEnum {
1702     NoMMX3DNow, MMX, AMD3DNow, AMD3DNowAthlon
1703   } MMX3DNowLevel;
1704 
1705   bool HasAES;
1706   bool HasPCLMUL;
1707   bool HasLZCNT;
1708   bool HasRDRND;
1709   bool HasBMI;
1710   bool HasBMI2;
1711   bool HasPOPCNT;
1712   bool HasRTM;
1713   bool HasPRFCHW;
1714   bool HasRDSEED;
1715   bool HasSSE4a;
1716   bool HasFMA4;
1717   bool HasFMA;
1718   bool HasXOP;
1719   bool HasF16C;
1720 
1721   /// \brief Enumeration of all of the X86 CPUs supported by Clang.
1722   ///
1723   /// Each enumeration represents a particular CPU supported by Clang. These
1724   /// loosely correspond to the options passed to '-march' or '-mtune' flags.
1725   enum CPUKind {
1726     CK_Generic,
1727 
1728     /// \name i386
1729     /// i386-generation processors.
1730     //@{
1731     CK_i386,
1732     //@}
1733 
1734     /// \name i486
1735     /// i486-generation processors.
1736     //@{
1737     CK_i486,
1738     CK_WinChipC6,
1739     CK_WinChip2,
1740     CK_C3,
1741     //@}
1742 
1743     /// \name i586
1744     /// i586-generation processors, P5 microarchitecture based.
1745     //@{
1746     CK_i586,
1747     CK_Pentium,
1748     CK_PentiumMMX,
1749     //@}
1750 
1751     /// \name i686
1752     /// i686-generation processors, P6 / Pentium M microarchitecture based.
1753     //@{
1754     CK_i686,
1755     CK_PentiumPro,
1756     CK_Pentium2,
1757     CK_Pentium3,
1758     CK_Pentium3M,
1759     CK_PentiumM,
1760     CK_C3_2,
1761 
1762     /// This enumerator is a bit odd, as GCC no longer accepts -march=yonah.
1763     /// Clang however has some logic to suport this.
1764     // FIXME: Warn, deprecate, and potentially remove this.
1765     CK_Yonah,
1766     //@}
1767 
1768     /// \name Netburst
1769     /// Netburst microarchitecture based processors.
1770     //@{
1771     CK_Pentium4,
1772     CK_Pentium4M,
1773     CK_Prescott,
1774     CK_Nocona,
1775     //@}
1776 
1777     /// \name Core
1778     /// Core microarchitecture based processors.
1779     //@{
1780     CK_Core2,
1781 
1782     /// This enumerator, like \see CK_Yonah, is a bit odd. It is another
1783     /// codename which GCC no longer accepts as an option to -march, but Clang
1784     /// has some logic for recognizing it.
1785     // FIXME: Warn, deprecate, and potentially remove this.
1786     CK_Penryn,
1787     //@}
1788 
1789     /// \name Atom
1790     /// Atom processors
1791     //@{
1792     CK_Atom,
1793     //@}
1794 
1795     /// \name Nehalem
1796     /// Nehalem microarchitecture based processors.
1797     //@{
1798     CK_Corei7,
1799     CK_Corei7AVX,
1800     CK_CoreAVXi,
1801     CK_CoreAVX2,
1802     //@}
1803 
1804     /// \name K6
1805     /// K6 architecture processors.
1806     //@{
1807     CK_K6,
1808     CK_K6_2,
1809     CK_K6_3,
1810     //@}
1811 
1812     /// \name K7
1813     /// K7 architecture processors.
1814     //@{
1815     CK_Athlon,
1816     CK_AthlonThunderbird,
1817     CK_Athlon4,
1818     CK_AthlonXP,
1819     CK_AthlonMP,
1820     //@}
1821 
1822     /// \name K8
1823     /// K8 architecture processors.
1824     //@{
1825     CK_Athlon64,
1826     CK_Athlon64SSE3,
1827     CK_AthlonFX,
1828     CK_K8,
1829     CK_K8SSE3,
1830     CK_Opteron,
1831     CK_OpteronSSE3,
1832     CK_AMDFAM10,
1833     //@}
1834 
1835     /// \name Bobcat
1836     /// Bobcat architecture processors.
1837     //@{
1838     CK_BTVER1,
1839     CK_BTVER2,
1840     //@}
1841 
1842     /// \name Bulldozer
1843     /// Bulldozer architecture processors.
1844     //@{
1845     CK_BDVER1,
1846     CK_BDVER2,
1847     //@}
1848 
1849     /// This specification is deprecated and will be removed in the future.
1850     /// Users should prefer \see CK_K8.
1851     // FIXME: Warn on this when the CPU is set to it.
1852     CK_x86_64,
1853     //@}
1854 
1855     /// \name Geode
1856     /// Geode processors.
1857     //@{
1858     CK_Geode
1859     //@}
1860   } CPU;
1861 
1862 public:
1863   X86TargetInfo(const std::string& triple)
1864     : TargetInfo(triple), SSELevel(NoSSE), MMX3DNowLevel(NoMMX3DNow),
1865       HasAES(false), HasPCLMUL(false), HasLZCNT(false), HasRDRND(false),
1866       HasBMI(false), HasBMI2(false), HasPOPCNT(false), HasRTM(false),
1867       HasPRFCHW(false), HasRDSEED(false), HasSSE4a(false), HasFMA4(false),
1868       HasFMA(false), HasXOP(false), HasF16C(false), CPU(CK_Generic) {
1869     BigEndian = false;
1870     LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
1871   }
1872   virtual unsigned getFloatEvalMethod() const {
1873     // X87 evaluates with 80 bits "long double" precision.
1874     return SSELevel == NoSSE ? 2 : 0;
1875   }
1876   virtual void getTargetBuiltins(const Builtin::Info *&Records,
1877                                  unsigned &NumRecords) const {
1878     Records = BuiltinInfo;
1879     NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
1880   }
1881   virtual void getGCCRegNames(const char * const *&Names,
1882                               unsigned &NumNames) const {
1883     Names = GCCRegNames;
1884     NumNames = llvm::array_lengthof(GCCRegNames);
1885   }
1886   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1887                                 unsigned &NumAliases) const {
1888     Aliases = 0;
1889     NumAliases = 0;
1890   }
1891   virtual void getGCCAddlRegNames(const AddlRegName *&Names,
1892                                   unsigned &NumNames) const {
1893     Names = AddlRegNames;
1894     NumNames = llvm::array_lengthof(AddlRegNames);
1895   }
1896   virtual bool validateAsmConstraint(const char *&Name,
1897                                      TargetInfo::ConstraintInfo &info) const;
1898   virtual std::string convertConstraint(const char *&Constraint) const;
1899   virtual const char *getClobbers() const {
1900     return "~{dirflag},~{fpsr},~{flags}";
1901   }
1902   virtual void getTargetDefines(const LangOptions &Opts,
1903                                 MacroBuilder &Builder) const;
1904   virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1905                                  StringRef Name,
1906                                  bool Enabled) const;
1907   virtual void getDefaultFeatures(llvm::StringMap<bool> &Features) const;
1908   virtual bool hasFeature(StringRef Feature) const;
1909   virtual void HandleTargetFeatures(std::vector<std::string> &Features);
1910   virtual const char* getABI() const {
1911     if (getTriple().getArch() == llvm::Triple::x86_64 && SSELevel >= AVX)
1912       return "avx";
1913     else if (getTriple().getArch() == llvm::Triple::x86 &&
1914              MMX3DNowLevel == NoMMX3DNow)
1915       return "no-mmx";
1916     return "";
1917   }
1918   virtual bool setCPU(const std::string &Name) {
1919     CPU = llvm::StringSwitch<CPUKind>(Name)
1920       .Case("i386", CK_i386)
1921       .Case("i486", CK_i486)
1922       .Case("winchip-c6", CK_WinChipC6)
1923       .Case("winchip2", CK_WinChip2)
1924       .Case("c3", CK_C3)
1925       .Case("i586", CK_i586)
1926       .Case("pentium", CK_Pentium)
1927       .Case("pentium-mmx", CK_PentiumMMX)
1928       .Case("i686", CK_i686)
1929       .Case("pentiumpro", CK_PentiumPro)
1930       .Case("pentium2", CK_Pentium2)
1931       .Case("pentium3", CK_Pentium3)
1932       .Case("pentium3m", CK_Pentium3M)
1933       .Case("pentium-m", CK_PentiumM)
1934       .Case("c3-2", CK_C3_2)
1935       .Case("yonah", CK_Yonah)
1936       .Case("pentium4", CK_Pentium4)
1937       .Case("pentium4m", CK_Pentium4M)
1938       .Case("prescott", CK_Prescott)
1939       .Case("nocona", CK_Nocona)
1940       .Case("core2", CK_Core2)
1941       .Case("penryn", CK_Penryn)
1942       .Case("atom", CK_Atom)
1943       .Case("corei7", CK_Corei7)
1944       .Case("corei7-avx", CK_Corei7AVX)
1945       .Case("core-avx-i", CK_CoreAVXi)
1946       .Case("core-avx2", CK_CoreAVX2)
1947       .Case("k6", CK_K6)
1948       .Case("k6-2", CK_K6_2)
1949       .Case("k6-3", CK_K6_3)
1950       .Case("athlon", CK_Athlon)
1951       .Case("athlon-tbird", CK_AthlonThunderbird)
1952       .Case("athlon-4", CK_Athlon4)
1953       .Case("athlon-xp", CK_AthlonXP)
1954       .Case("athlon-mp", CK_AthlonMP)
1955       .Case("athlon64", CK_Athlon64)
1956       .Case("athlon64-sse3", CK_Athlon64SSE3)
1957       .Case("athlon-fx", CK_AthlonFX)
1958       .Case("k8", CK_K8)
1959       .Case("k8-sse3", CK_K8SSE3)
1960       .Case("opteron", CK_Opteron)
1961       .Case("opteron-sse3", CK_OpteronSSE3)
1962       .Case("amdfam10", CK_AMDFAM10)
1963       .Case("btver1", CK_BTVER1)
1964       .Case("btver2", CK_BTVER2)
1965       .Case("bdver1", CK_BDVER1)
1966       .Case("bdver2", CK_BDVER2)
1967       .Case("x86-64", CK_x86_64)
1968       .Case("geode", CK_Geode)
1969       .Default(CK_Generic);
1970 
1971     // Perform any per-CPU checks necessary to determine if this CPU is
1972     // acceptable.
1973     // FIXME: This results in terrible diagnostics. Clang just says the CPU is
1974     // invalid without explaining *why*.
1975     switch (CPU) {
1976     case CK_Generic:
1977       // No processor selected!
1978       return false;
1979 
1980     case CK_i386:
1981     case CK_i486:
1982     case CK_WinChipC6:
1983     case CK_WinChip2:
1984     case CK_C3:
1985     case CK_i586:
1986     case CK_Pentium:
1987     case CK_PentiumMMX:
1988     case CK_i686:
1989     case CK_PentiumPro:
1990     case CK_Pentium2:
1991     case CK_Pentium3:
1992     case CK_Pentium3M:
1993     case CK_PentiumM:
1994     case CK_Yonah:
1995     case CK_C3_2:
1996     case CK_Pentium4:
1997     case CK_Pentium4M:
1998     case CK_Prescott:
1999     case CK_K6:
2000     case CK_K6_2:
2001     case CK_K6_3:
2002     case CK_Athlon:
2003     case CK_AthlonThunderbird:
2004     case CK_Athlon4:
2005     case CK_AthlonXP:
2006     case CK_AthlonMP:
2007     case CK_Geode:
2008       // Only accept certain architectures when compiling in 32-bit mode.
2009       if (getTriple().getArch() != llvm::Triple::x86)
2010         return false;
2011 
2012       // Fallthrough
2013     case CK_Nocona:
2014     case CK_Core2:
2015     case CK_Penryn:
2016     case CK_Atom:
2017     case CK_Corei7:
2018     case CK_Corei7AVX:
2019     case CK_CoreAVXi:
2020     case CK_CoreAVX2:
2021     case CK_Athlon64:
2022     case CK_Athlon64SSE3:
2023     case CK_AthlonFX:
2024     case CK_K8:
2025     case CK_K8SSE3:
2026     case CK_Opteron:
2027     case CK_OpteronSSE3:
2028     case CK_AMDFAM10:
2029     case CK_BTVER1:
2030     case CK_BTVER2:
2031     case CK_BDVER1:
2032     case CK_BDVER2:
2033     case CK_x86_64:
2034       return true;
2035     }
2036     llvm_unreachable("Unhandled CPU kind");
2037   }
2038 
2039   virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const {
2040     // We accept all non-ARM calling conventions
2041     return (CC == CC_X86ThisCall ||
2042             CC == CC_X86FastCall ||
2043             CC == CC_X86StdCall ||
2044             CC == CC_C ||
2045             CC == CC_X86Pascal ||
2046             CC == CC_IntelOclBicc) ? CCCR_OK : CCCR_Warning;
2047   }
2048 
2049   virtual CallingConv getDefaultCallingConv(CallingConvMethodType MT) const {
2050     return MT == CCMT_Member ? CC_X86ThisCall : CC_C;
2051   }
2052 };
2053 
2054 void X86TargetInfo::getDefaultFeatures(llvm::StringMap<bool> &Features) const {
2055   // FIXME: This should not be here.
2056   Features["3dnow"] = false;
2057   Features["3dnowa"] = false;
2058   Features["mmx"] = false;
2059   Features["sse"] = false;
2060   Features["sse2"] = false;
2061   Features["sse3"] = false;
2062   Features["ssse3"] = false;
2063   Features["sse41"] = false;
2064   Features["sse42"] = false;
2065   Features["sse4a"] = false;
2066   Features["aes"] = false;
2067   Features["pclmul"] = false;
2068   Features["avx"] = false;
2069   Features["avx2"] = false;
2070   Features["lzcnt"] = false;
2071   Features["rdrand"] = false;
2072   Features["bmi"] = false;
2073   Features["bmi2"] = false;
2074   Features["popcnt"] = false;
2075   Features["rtm"] = false;
2076   Features["prfchw"] = false;
2077   Features["rdseed"] = false;
2078   Features["fma4"] = false;
2079   Features["fma"] = false;
2080   Features["xop"] = false;
2081   Features["f16c"] = false;
2082 
2083   // FIXME: This *really* should not be here.
2084 
2085   // X86_64 always has SSE2.
2086   if (getTriple().getArch() == llvm::Triple::x86_64)
2087     setFeatureEnabled(Features, "sse2", true);
2088 
2089   switch (CPU) {
2090   case CK_Generic:
2091   case CK_i386:
2092   case CK_i486:
2093   case CK_i586:
2094   case CK_Pentium:
2095   case CK_i686:
2096   case CK_PentiumPro:
2097     break;
2098   case CK_PentiumMMX:
2099   case CK_Pentium2:
2100     setFeatureEnabled(Features, "mmx", true);
2101     break;
2102   case CK_Pentium3:
2103   case CK_Pentium3M:
2104     setFeatureEnabled(Features, "sse", true);
2105     break;
2106   case CK_PentiumM:
2107   case CK_Pentium4:
2108   case CK_Pentium4M:
2109   case CK_x86_64:
2110     setFeatureEnabled(Features, "sse2", true);
2111     break;
2112   case CK_Yonah:
2113   case CK_Prescott:
2114   case CK_Nocona:
2115     setFeatureEnabled(Features, "sse3", true);
2116     break;
2117   case CK_Core2:
2118     setFeatureEnabled(Features, "ssse3", true);
2119     break;
2120   case CK_Penryn:
2121     setFeatureEnabled(Features, "sse4.1", true);
2122     break;
2123   case CK_Atom:
2124     setFeatureEnabled(Features, "ssse3", true);
2125     break;
2126   case CK_Corei7:
2127     setFeatureEnabled(Features, "sse4", true);
2128     break;
2129   case CK_Corei7AVX:
2130     setFeatureEnabled(Features, "avx", true);
2131     setFeatureEnabled(Features, "aes", true);
2132     setFeatureEnabled(Features, "pclmul", true);
2133     break;
2134   case CK_CoreAVXi:
2135     setFeatureEnabled(Features, "avx", true);
2136     setFeatureEnabled(Features, "aes", true);
2137     setFeatureEnabled(Features, "pclmul", true);
2138     setFeatureEnabled(Features, "rdrnd", true);
2139     setFeatureEnabled(Features, "f16c", true);
2140     break;
2141   case CK_CoreAVX2:
2142     setFeatureEnabled(Features, "avx2", true);
2143     setFeatureEnabled(Features, "aes", true);
2144     setFeatureEnabled(Features, "pclmul", true);
2145     setFeatureEnabled(Features, "lzcnt", true);
2146     setFeatureEnabled(Features, "rdrnd", true);
2147     setFeatureEnabled(Features, "f16c", true);
2148     setFeatureEnabled(Features, "bmi", true);
2149     setFeatureEnabled(Features, "bmi2", true);
2150     setFeatureEnabled(Features, "rtm", true);
2151     setFeatureEnabled(Features, "fma", true);
2152     break;
2153   case CK_K6:
2154   case CK_WinChipC6:
2155     setFeatureEnabled(Features, "mmx", true);
2156     break;
2157   case CK_K6_2:
2158   case CK_K6_3:
2159   case CK_WinChip2:
2160   case CK_C3:
2161     setFeatureEnabled(Features, "3dnow", true);
2162     break;
2163   case CK_Athlon:
2164   case CK_AthlonThunderbird:
2165   case CK_Geode:
2166     setFeatureEnabled(Features, "3dnowa", true);
2167     break;
2168   case CK_Athlon4:
2169   case CK_AthlonXP:
2170   case CK_AthlonMP:
2171     setFeatureEnabled(Features, "sse", true);
2172     setFeatureEnabled(Features, "3dnowa", true);
2173     break;
2174   case CK_K8:
2175   case CK_Opteron:
2176   case CK_Athlon64:
2177   case CK_AthlonFX:
2178     setFeatureEnabled(Features, "sse2", true);
2179     setFeatureEnabled(Features, "3dnowa", true);
2180     break;
2181   case CK_K8SSE3:
2182   case CK_OpteronSSE3:
2183   case CK_Athlon64SSE3:
2184     setFeatureEnabled(Features, "sse3", true);
2185     setFeatureEnabled(Features, "3dnowa", true);
2186     break;
2187   case CK_AMDFAM10:
2188     setFeatureEnabled(Features, "sse3", true);
2189     setFeatureEnabled(Features, "sse4a", true);
2190     setFeatureEnabled(Features, "3dnowa", true);
2191     setFeatureEnabled(Features, "lzcnt", true);
2192     setFeatureEnabled(Features, "popcnt", true);
2193     break;
2194   case CK_BTVER1:
2195     setFeatureEnabled(Features, "ssse3", true);
2196     setFeatureEnabled(Features, "sse4a", true);
2197     setFeatureEnabled(Features, "lzcnt", true);
2198     setFeatureEnabled(Features, "popcnt", true);
2199     break;
2200   case CK_BTVER2:
2201     setFeatureEnabled(Features, "avx", true);
2202     setFeatureEnabled(Features, "sse4a", true);
2203     setFeatureEnabled(Features, "lzcnt", true);
2204     setFeatureEnabled(Features, "aes", true);
2205     setFeatureEnabled(Features, "pclmul", true);
2206     setFeatureEnabled(Features, "bmi", true);
2207     setFeatureEnabled(Features, "f16c", true);
2208     break;
2209   case CK_BDVER1:
2210     setFeatureEnabled(Features, "xop", true);
2211     setFeatureEnabled(Features, "lzcnt", true);
2212     setFeatureEnabled(Features, "aes", true);
2213     setFeatureEnabled(Features, "pclmul", true);
2214     break;
2215   case CK_BDVER2:
2216     setFeatureEnabled(Features, "xop", true);
2217     setFeatureEnabled(Features, "lzcnt", true);
2218     setFeatureEnabled(Features, "aes", true);
2219     setFeatureEnabled(Features, "pclmul", true);
2220     setFeatureEnabled(Features, "bmi", true);
2221     setFeatureEnabled(Features, "fma", true);
2222     setFeatureEnabled(Features, "f16c", true);
2223     break;
2224   case CK_C3_2:
2225     setFeatureEnabled(Features, "sse", true);
2226     break;
2227   }
2228 }
2229 
2230 bool X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
2231                                       StringRef Name,
2232                                       bool Enabled) const {
2233   // FIXME: This *really* should not be here.  We need some way of translating
2234   // options into llvm subtarget features.
2235   if (!Features.count(Name) &&
2236       (Name != "sse4" && Name != "sse4.2" && Name != "sse4.1" &&
2237        Name != "rdrnd"))
2238     return false;
2239 
2240   // FIXME: this should probably use a switch with fall through.
2241 
2242   if (Enabled) {
2243     if (Name == "mmx")
2244       Features["mmx"] = true;
2245     else if (Name == "sse")
2246       Features["mmx"] = Features["sse"] = true;
2247     else if (Name == "sse2")
2248       Features["mmx"] = Features["sse"] = Features["sse2"] = true;
2249     else if (Name == "sse3")
2250       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2251         true;
2252     else if (Name == "ssse3")
2253       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2254         Features["ssse3"] = true;
2255     else if (Name == "sse4" || Name == "sse4.2")
2256       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2257         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2258         Features["popcnt"] = true;
2259     else if (Name == "sse4.1")
2260       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2261         Features["ssse3"] = Features["sse41"] = true;
2262     else if (Name == "3dnow")
2263       Features["mmx"] = Features["3dnow"] = true;
2264     else if (Name == "3dnowa")
2265       Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = true;
2266     else if (Name == "aes")
2267       Features["sse"] = Features["sse2"] = Features["aes"] = true;
2268     else if (Name == "pclmul")
2269       Features["sse"] = Features["sse2"] = Features["pclmul"] = true;
2270     else if (Name == "avx")
2271       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2272         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2273         Features["popcnt"] = Features["avx"] = true;
2274     else if (Name == "avx2")
2275       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2276         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2277         Features["popcnt"] = Features["avx"] = Features["avx2"] = true;
2278     else if (Name == "fma")
2279       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2280         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2281         Features["popcnt"] = Features["avx"] = Features["fma"] = true;
2282     else if (Name == "fma4")
2283       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2284         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2285         Features["popcnt"] = Features["avx"] = Features["sse4a"] =
2286         Features["fma4"] = true;
2287     else if (Name == "xop")
2288       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2289         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2290         Features["popcnt"] = Features["avx"] = Features["sse4a"] =
2291         Features["fma4"] = Features["xop"] = true;
2292     else if (Name == "sse4a")
2293       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
2294         Features["sse4a"] = true;
2295     else if (Name == "lzcnt")
2296       Features["lzcnt"] = true;
2297     else if (Name == "rdrnd")
2298       Features["rdrand"] = true;
2299     else if (Name == "bmi")
2300       Features["bmi"] = true;
2301     else if (Name == "bmi2")
2302       Features["bmi2"] = true;
2303     else if (Name == "popcnt")
2304       Features["popcnt"] = true;
2305     else if (Name == "f16c")
2306       Features["f16c"] = true;
2307     else if (Name == "rtm")
2308       Features["rtm"] = true;
2309     else if (Name == "prfchw")
2310       Features["prfchw"] = true;
2311     else if (Name == "rdseed")
2312       Features["rdseed"] = true;
2313   } else {
2314     if (Name == "mmx")
2315       Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = false;
2316     else if (Name == "sse")
2317       Features["sse"] = Features["sse2"] = Features["sse3"] =
2318         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2319         Features["sse4a"] = Features["avx"] = Features["avx2"] =
2320         Features["fma"] = Features["fma4"] = Features["aes"] =
2321         Features["pclmul"] = Features["xop"] = false;
2322     else if (Name == "sse2")
2323       Features["sse2"] = Features["sse3"] = Features["ssse3"] =
2324         Features["sse41"] = Features["sse42"] = Features["sse4a"] =
2325         Features["avx"] = Features["avx2"] = Features["fma"] =
2326         Features["fma4"] = Features["aes"] = Features["pclmul"] =
2327         Features["xop"] = false;
2328     else if (Name == "sse3")
2329       Features["sse3"] = Features["ssse3"] = Features["sse41"] =
2330         Features["sse42"] = Features["sse4a"] = Features["avx"] =
2331         Features["avx2"] = Features["fma"] = Features["fma4"] =
2332         Features["xop"] = false;
2333     else if (Name == "ssse3")
2334       Features["ssse3"] = Features["sse41"] = Features["sse42"] =
2335         Features["avx"] = Features["avx2"] = Features["fma"] = false;
2336     else if (Name == "sse4" || Name == "sse4.1")
2337       Features["sse41"] = Features["sse42"] = Features["avx"] =
2338         Features["avx2"] = Features["fma"] = false;
2339     else if (Name == "sse4.2")
2340       Features["sse42"] = Features["avx"] = Features["avx2"] =
2341         Features["fma"] = false;
2342     else if (Name == "3dnow")
2343       Features["3dnow"] = Features["3dnowa"] = false;
2344     else if (Name == "3dnowa")
2345       Features["3dnowa"] = false;
2346     else if (Name == "aes")
2347       Features["aes"] = false;
2348     else if (Name == "pclmul")
2349       Features["pclmul"] = false;
2350     else if (Name == "avx")
2351       Features["avx"] = Features["avx2"] = Features["fma"] =
2352         Features["fma4"] = Features["xop"] = false;
2353     else if (Name == "avx2")
2354       Features["avx2"] = false;
2355     else if (Name == "fma")
2356       Features["fma"] = false;
2357     else if (Name == "sse4a")
2358       Features["sse4a"] = Features["fma4"] = Features["xop"] = false;
2359     else if (Name == "lzcnt")
2360       Features["lzcnt"] = false;
2361     else if (Name == "rdrnd")
2362       Features["rdrand"] = false;
2363     else if (Name == "bmi")
2364       Features["bmi"] = false;
2365     else if (Name == "bmi2")
2366       Features["bmi2"] = false;
2367     else if (Name == "popcnt")
2368       Features["popcnt"] = false;
2369     else if (Name == "fma4")
2370       Features["fma4"] = Features["xop"] = false;
2371     else if (Name == "xop")
2372       Features["xop"] = false;
2373     else if (Name == "f16c")
2374       Features["f16c"] = false;
2375     else if (Name == "rtm")
2376       Features["rtm"] = false;
2377     else if (Name == "prfchw")
2378       Features["prfchw"] = false;
2379     else if (Name == "rdseed")
2380       Features["rdseed"] = false;
2381   }
2382 
2383   return true;
2384 }
2385 
2386 /// HandleTargetOptions - Perform initialization based on the user
2387 /// configured set of features.
2388 void X86TargetInfo::HandleTargetFeatures(std::vector<std::string> &Features) {
2389   // Remember the maximum enabled sselevel.
2390   for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
2391     // Ignore disabled features.
2392     if (Features[i][0] == '-')
2393       continue;
2394 
2395     StringRef Feature = StringRef(Features[i]).substr(1);
2396 
2397     if (Feature == "aes") {
2398       HasAES = true;
2399       continue;
2400     }
2401 
2402     if (Feature == "pclmul") {
2403       HasPCLMUL = true;
2404       continue;
2405     }
2406 
2407     if (Feature == "lzcnt") {
2408       HasLZCNT = true;
2409       continue;
2410     }
2411 
2412     if (Feature == "rdrand") {
2413       HasRDRND = true;
2414       continue;
2415     }
2416 
2417     if (Feature == "bmi") {
2418       HasBMI = true;
2419       continue;
2420     }
2421 
2422     if (Feature == "bmi2") {
2423       HasBMI2 = true;
2424       continue;
2425     }
2426 
2427     if (Feature == "popcnt") {
2428       HasPOPCNT = true;
2429       continue;
2430     }
2431 
2432     if (Feature == "rtm") {
2433       HasRTM = true;
2434       continue;
2435     }
2436 
2437     if (Feature == "prfchw") {
2438       HasPRFCHW = true;
2439       continue;
2440     }
2441 
2442     if (Feature == "rdseed") {
2443       HasRDSEED = true;
2444       continue;
2445     }
2446 
2447     if (Feature == "sse4a") {
2448       HasSSE4a = true;
2449       continue;
2450     }
2451 
2452     if (Feature == "fma4") {
2453       HasFMA4 = true;
2454       continue;
2455     }
2456 
2457     if (Feature == "fma") {
2458       HasFMA = true;
2459       continue;
2460     }
2461 
2462     if (Feature == "xop") {
2463       HasXOP = true;
2464       continue;
2465     }
2466 
2467     if (Feature == "f16c") {
2468       HasF16C = true;
2469       continue;
2470     }
2471 
2472     assert(Features[i][0] == '+' && "Invalid target feature!");
2473     X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
2474       .Case("avx2", AVX2)
2475       .Case("avx", AVX)
2476       .Case("sse42", SSE42)
2477       .Case("sse41", SSE41)
2478       .Case("ssse3", SSSE3)
2479       .Case("sse3", SSE3)
2480       .Case("sse2", SSE2)
2481       .Case("sse", SSE1)
2482       .Default(NoSSE);
2483     SSELevel = std::max(SSELevel, Level);
2484 
2485     MMX3DNowEnum ThreeDNowLevel =
2486       llvm::StringSwitch<MMX3DNowEnum>(Feature)
2487         .Case("3dnowa", AMD3DNowAthlon)
2488         .Case("3dnow", AMD3DNow)
2489         .Case("mmx", MMX)
2490         .Default(NoMMX3DNow);
2491 
2492     MMX3DNowLevel = std::max(MMX3DNowLevel, ThreeDNowLevel);
2493   }
2494 
2495   // Don't tell the backend if we're turning off mmx; it will end up disabling
2496   // SSE, which we don't want.
2497   std::vector<std::string>::iterator it;
2498   it = std::find(Features.begin(), Features.end(), "-mmx");
2499   if (it != Features.end())
2500     Features.erase(it);
2501 }
2502 
2503 /// X86TargetInfo::getTargetDefines - Return the set of the X86-specific macro
2504 /// definitions for this particular subtarget.
2505 void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
2506                                      MacroBuilder &Builder) const {
2507   // Target identification.
2508   if (getTriple().getArch() == llvm::Triple::x86_64) {
2509     Builder.defineMacro("__amd64__");
2510     Builder.defineMacro("__amd64");
2511     Builder.defineMacro("__x86_64");
2512     Builder.defineMacro("__x86_64__");
2513   } else {
2514     DefineStd(Builder, "i386", Opts);
2515   }
2516 
2517   // Subtarget options.
2518   // FIXME: We are hard-coding the tune parameters based on the CPU, but they
2519   // truly should be based on -mtune options.
2520   switch (CPU) {
2521   case CK_Generic:
2522     break;
2523   case CK_i386:
2524     // The rest are coming from the i386 define above.
2525     Builder.defineMacro("__tune_i386__");
2526     break;
2527   case CK_i486:
2528   case CK_WinChipC6:
2529   case CK_WinChip2:
2530   case CK_C3:
2531     defineCPUMacros(Builder, "i486");
2532     break;
2533   case CK_PentiumMMX:
2534     Builder.defineMacro("__pentium_mmx__");
2535     Builder.defineMacro("__tune_pentium_mmx__");
2536     // Fallthrough
2537   case CK_i586:
2538   case CK_Pentium:
2539     defineCPUMacros(Builder, "i586");
2540     defineCPUMacros(Builder, "pentium");
2541     break;
2542   case CK_Pentium3:
2543   case CK_Pentium3M:
2544   case CK_PentiumM:
2545     Builder.defineMacro("__tune_pentium3__");
2546     // Fallthrough
2547   case CK_Pentium2:
2548   case CK_C3_2:
2549     Builder.defineMacro("__tune_pentium2__");
2550     // Fallthrough
2551   case CK_PentiumPro:
2552     Builder.defineMacro("__tune_i686__");
2553     Builder.defineMacro("__tune_pentiumpro__");
2554     // Fallthrough
2555   case CK_i686:
2556     Builder.defineMacro("__i686");
2557     Builder.defineMacro("__i686__");
2558     // Strangely, __tune_i686__ isn't defined by GCC when CPU == i686.
2559     Builder.defineMacro("__pentiumpro");
2560     Builder.defineMacro("__pentiumpro__");
2561     break;
2562   case CK_Pentium4:
2563   case CK_Pentium4M:
2564     defineCPUMacros(Builder, "pentium4");
2565     break;
2566   case CK_Yonah:
2567   case CK_Prescott:
2568   case CK_Nocona:
2569     defineCPUMacros(Builder, "nocona");
2570     break;
2571   case CK_Core2:
2572   case CK_Penryn:
2573     defineCPUMacros(Builder, "core2");
2574     break;
2575   case CK_Atom:
2576     defineCPUMacros(Builder, "atom");
2577     break;
2578   case CK_Corei7:
2579   case CK_Corei7AVX:
2580   case CK_CoreAVXi:
2581   case CK_CoreAVX2:
2582     defineCPUMacros(Builder, "corei7");
2583     break;
2584   case CK_K6_2:
2585     Builder.defineMacro("__k6_2__");
2586     Builder.defineMacro("__tune_k6_2__");
2587     // Fallthrough
2588   case CK_K6_3:
2589     if (CPU != CK_K6_2) {  // In case of fallthrough
2590       // FIXME: GCC may be enabling these in cases where some other k6
2591       // architecture is specified but -m3dnow is explicitly provided. The
2592       // exact semantics need to be determined and emulated here.
2593       Builder.defineMacro("__k6_3__");
2594       Builder.defineMacro("__tune_k6_3__");
2595     }
2596     // Fallthrough
2597   case CK_K6:
2598     defineCPUMacros(Builder, "k6");
2599     break;
2600   case CK_Athlon:
2601   case CK_AthlonThunderbird:
2602   case CK_Athlon4:
2603   case CK_AthlonXP:
2604   case CK_AthlonMP:
2605     defineCPUMacros(Builder, "athlon");
2606     if (SSELevel != NoSSE) {
2607       Builder.defineMacro("__athlon_sse__");
2608       Builder.defineMacro("__tune_athlon_sse__");
2609     }
2610     break;
2611   case CK_K8:
2612   case CK_K8SSE3:
2613   case CK_x86_64:
2614   case CK_Opteron:
2615   case CK_OpteronSSE3:
2616   case CK_Athlon64:
2617   case CK_Athlon64SSE3:
2618   case CK_AthlonFX:
2619     defineCPUMacros(Builder, "k8");
2620     break;
2621   case CK_AMDFAM10:
2622     defineCPUMacros(Builder, "amdfam10");
2623     break;
2624   case CK_BTVER1:
2625     defineCPUMacros(Builder, "btver1");
2626     break;
2627   case CK_BTVER2:
2628     defineCPUMacros(Builder, "btver2");
2629     break;
2630   case CK_BDVER1:
2631     defineCPUMacros(Builder, "bdver1");
2632     break;
2633   case CK_BDVER2:
2634     defineCPUMacros(Builder, "bdver2");
2635     break;
2636   case CK_Geode:
2637     defineCPUMacros(Builder, "geode");
2638     break;
2639   }
2640 
2641   // Target properties.
2642   Builder.defineMacro("__LITTLE_ENDIAN__");
2643   Builder.defineMacro("__REGISTER_PREFIX__", "");
2644 
2645   // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
2646   // functions in glibc header files that use FP Stack inline asm which the
2647   // backend can't deal with (PR879).
2648   Builder.defineMacro("__NO_MATH_INLINES");
2649 
2650   if (HasAES)
2651     Builder.defineMacro("__AES__");
2652 
2653   if (HasPCLMUL)
2654     Builder.defineMacro("__PCLMUL__");
2655 
2656   if (HasLZCNT)
2657     Builder.defineMacro("__LZCNT__");
2658 
2659   if (HasRDRND)
2660     Builder.defineMacro("__RDRND__");
2661 
2662   if (HasBMI)
2663     Builder.defineMacro("__BMI__");
2664 
2665   if (HasBMI2)
2666     Builder.defineMacro("__BMI2__");
2667 
2668   if (HasPOPCNT)
2669     Builder.defineMacro("__POPCNT__");
2670 
2671   if (HasRTM)
2672     Builder.defineMacro("__RTM__");
2673 
2674   if (HasPRFCHW)
2675     Builder.defineMacro("__PRFCHW__");
2676 
2677   if (HasRDSEED)
2678     Builder.defineMacro("__RDSEED__");
2679 
2680   if (HasSSE4a)
2681     Builder.defineMacro("__SSE4A__");
2682 
2683   if (HasFMA4)
2684     Builder.defineMacro("__FMA4__");
2685 
2686   if (HasFMA)
2687     Builder.defineMacro("__FMA__");
2688 
2689   if (HasXOP)
2690     Builder.defineMacro("__XOP__");
2691 
2692   if (HasF16C)
2693     Builder.defineMacro("__F16C__");
2694 
2695   // Each case falls through to the previous one here.
2696   switch (SSELevel) {
2697   case AVX2:
2698     Builder.defineMacro("__AVX2__");
2699   case AVX:
2700     Builder.defineMacro("__AVX__");
2701   case SSE42:
2702     Builder.defineMacro("__SSE4_2__");
2703   case SSE41:
2704     Builder.defineMacro("__SSE4_1__");
2705   case SSSE3:
2706     Builder.defineMacro("__SSSE3__");
2707   case SSE3:
2708     Builder.defineMacro("__SSE3__");
2709   case SSE2:
2710     Builder.defineMacro("__SSE2__");
2711     Builder.defineMacro("__SSE2_MATH__");  // -mfp-math=sse always implied.
2712   case SSE1:
2713     Builder.defineMacro("__SSE__");
2714     Builder.defineMacro("__SSE_MATH__");   // -mfp-math=sse always implied.
2715   case NoSSE:
2716     break;
2717   }
2718 
2719   if (Opts.MicrosoftExt && getTriple().getArch() == llvm::Triple::x86) {
2720     switch (SSELevel) {
2721     case AVX2:
2722     case AVX:
2723     case SSE42:
2724     case SSE41:
2725     case SSSE3:
2726     case SSE3:
2727     case SSE2:
2728       Builder.defineMacro("_M_IX86_FP", Twine(2));
2729       break;
2730     case SSE1:
2731       Builder.defineMacro("_M_IX86_FP", Twine(1));
2732       break;
2733     default:
2734       Builder.defineMacro("_M_IX86_FP", Twine(0));
2735     }
2736   }
2737 
2738   // Each case falls through to the previous one here.
2739   switch (MMX3DNowLevel) {
2740   case AMD3DNowAthlon:
2741     Builder.defineMacro("__3dNOW_A__");
2742   case AMD3DNow:
2743     Builder.defineMacro("__3dNOW__");
2744   case MMX:
2745     Builder.defineMacro("__MMX__");
2746   case NoMMX3DNow:
2747     break;
2748   }
2749 
2750   if (CPU >= CK_i486) {
2751     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
2752     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
2753     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
2754   }
2755   if (CPU >= CK_i586)
2756     Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
2757 }
2758 
2759 bool X86TargetInfo::hasFeature(StringRef Feature) const {
2760   return llvm::StringSwitch<bool>(Feature)
2761       .Case("aes", HasAES)
2762       .Case("avx", SSELevel >= AVX)
2763       .Case("avx2", SSELevel >= AVX2)
2764       .Case("bmi", HasBMI)
2765       .Case("bmi2", HasBMI2)
2766       .Case("fma", HasFMA)
2767       .Case("fma4", HasFMA4)
2768       .Case("lzcnt", HasLZCNT)
2769       .Case("rdrnd", HasRDRND)
2770       .Case("mm3dnow", MMX3DNowLevel >= AMD3DNow)
2771       .Case("mm3dnowa", MMX3DNowLevel >= AMD3DNowAthlon)
2772       .Case("mmx", MMX3DNowLevel >= MMX)
2773       .Case("pclmul", HasPCLMUL)
2774       .Case("popcnt", HasPOPCNT)
2775       .Case("rtm", HasRTM)
2776       .Case("prfchw", HasPRFCHW)
2777       .Case("rdseed", HasRDSEED)
2778       .Case("sse", SSELevel >= SSE1)
2779       .Case("sse2", SSELevel >= SSE2)
2780       .Case("sse3", SSELevel >= SSE3)
2781       .Case("ssse3", SSELevel >= SSSE3)
2782       .Case("sse41", SSELevel >= SSE41)
2783       .Case("sse42", SSELevel >= SSE42)
2784       .Case("sse4a", HasSSE4a)
2785       .Case("x86", true)
2786       .Case("x86_32", getTriple().getArch() == llvm::Triple::x86)
2787       .Case("x86_64", getTriple().getArch() == llvm::Triple::x86_64)
2788       .Case("xop", HasXOP)
2789       .Case("f16c", HasF16C)
2790       .Default(false);
2791 }
2792 
2793 bool
2794 X86TargetInfo::validateAsmConstraint(const char *&Name,
2795                                      TargetInfo::ConstraintInfo &Info) const {
2796   switch (*Name) {
2797   default: return false;
2798   case 'Y': // first letter of a pair:
2799     switch (*(Name+1)) {
2800     default: return false;
2801     case '0':  // First SSE register.
2802     case 't':  // Any SSE register, when SSE2 is enabled.
2803     case 'i':  // Any SSE register, when SSE2 and inter-unit moves enabled.
2804     case 'm':  // any MMX register, when inter-unit moves enabled.
2805       break;   // falls through to setAllowsRegister.
2806   }
2807   case 'a': // eax.
2808   case 'b': // ebx.
2809   case 'c': // ecx.
2810   case 'd': // edx.
2811   case 'S': // esi.
2812   case 'D': // edi.
2813   case 'A': // edx:eax.
2814   case 'f': // any x87 floating point stack register.
2815   case 't': // top of floating point stack.
2816   case 'u': // second from top of floating point stack.
2817   case 'q': // Any register accessible as [r]l: a, b, c, and d.
2818   case 'y': // Any MMX register.
2819   case 'x': // Any SSE register.
2820   case 'Q': // Any register accessible as [r]h: a, b, c, and d.
2821   case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
2822   case 'l': // "Index" registers: any general register that can be used as an
2823             // index in a base+index memory access.
2824     Info.setAllowsRegister();
2825     return true;
2826   case 'C': // SSE floating point constant.
2827   case 'G': // x87 floating point constant.
2828   case 'e': // 32-bit signed integer constant for use with zero-extending
2829             // x86_64 instructions.
2830   case 'Z': // 32-bit unsigned integer constant for use with zero-extending
2831             // x86_64 instructions.
2832     return true;
2833   }
2834 }
2835 
2836 
2837 std::string
2838 X86TargetInfo::convertConstraint(const char *&Constraint) const {
2839   switch (*Constraint) {
2840   case 'a': return std::string("{ax}");
2841   case 'b': return std::string("{bx}");
2842   case 'c': return std::string("{cx}");
2843   case 'd': return std::string("{dx}");
2844   case 'S': return std::string("{si}");
2845   case 'D': return std::string("{di}");
2846   case 'p': // address
2847     return std::string("im");
2848   case 't': // top of floating point stack.
2849     return std::string("{st}");
2850   case 'u': // second from top of floating point stack.
2851     return std::string("{st(1)}"); // second from top of floating point stack.
2852   default:
2853     return std::string(1, *Constraint);
2854   }
2855 }
2856 } // end anonymous namespace
2857 
2858 namespace {
2859 // X86-32 generic target
2860 class X86_32TargetInfo : public X86TargetInfo {
2861 public:
2862   X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
2863     DoubleAlign = LongLongAlign = 32;
2864     LongDoubleWidth = 96;
2865     LongDoubleAlign = 32;
2866     SuitableAlign = 128;
2867     DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2868                         "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
2869                         "a0:0:64-f80:32:32-n8:16:32-S128";
2870     SizeType = UnsignedInt;
2871     PtrDiffType = SignedInt;
2872     IntPtrType = SignedInt;
2873     RegParmMax = 3;
2874 
2875     // Use fpret for all types.
2876     RealTypeUsesObjCFPRet = ((1 << TargetInfo::Float) |
2877                              (1 << TargetInfo::Double) |
2878                              (1 << TargetInfo::LongDouble));
2879 
2880     // x86-32 has atomics up to 8 bytes
2881     // FIXME: Check that we actually have cmpxchg8b before setting
2882     // MaxAtomicInlineWidth. (cmpxchg8b is an i586 instruction.)
2883     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
2884   }
2885   virtual BuiltinVaListKind getBuiltinVaListKind() const {
2886     return TargetInfo::CharPtrBuiltinVaList;
2887   }
2888 
2889   int getEHDataRegisterNumber(unsigned RegNo) const {
2890     if (RegNo == 0) return 0;
2891     if (RegNo == 1) return 2;
2892     return -1;
2893   }
2894   virtual bool validateInputSize(StringRef Constraint,
2895                                  unsigned Size) const {
2896     switch (Constraint[0]) {
2897     default: break;
2898     case 'a':
2899     case 'b':
2900     case 'c':
2901     case 'd':
2902       return Size <= 32;
2903     }
2904 
2905     return true;
2906   }
2907 };
2908 } // end anonymous namespace
2909 
2910 namespace {
2911 class NetBSDI386TargetInfo : public NetBSDTargetInfo<X86_32TargetInfo> {
2912 public:
2913   NetBSDI386TargetInfo(const std::string &triple) :
2914     NetBSDTargetInfo<X86_32TargetInfo>(triple) {
2915   }
2916 
2917   virtual unsigned getFloatEvalMethod() const {
2918     // NetBSD defaults to "double" rounding
2919     return 1;
2920   }
2921 };
2922 } // end anonymous namespace
2923 
2924 namespace {
2925 class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
2926 public:
2927   OpenBSDI386TargetInfo(const std::string& triple) :
2928     OpenBSDTargetInfo<X86_32TargetInfo>(triple) {
2929     SizeType = UnsignedLong;
2930     IntPtrType = SignedLong;
2931     PtrDiffType = SignedLong;
2932   }
2933 };
2934 } // end anonymous namespace
2935 
2936 namespace {
2937 class BitrigI386TargetInfo : public BitrigTargetInfo<X86_32TargetInfo> {
2938 public:
2939   BitrigI386TargetInfo(const std::string& triple) :
2940     BitrigTargetInfo<X86_32TargetInfo>(triple) {
2941     SizeType = UnsignedLong;
2942     IntPtrType = SignedLong;
2943     PtrDiffType = SignedLong;
2944   }
2945 };
2946 } // end anonymous namespace
2947 
2948 namespace {
2949 class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
2950 public:
2951   DarwinI386TargetInfo(const std::string& triple) :
2952     DarwinTargetInfo<X86_32TargetInfo>(triple) {
2953     LongDoubleWidth = 128;
2954     LongDoubleAlign = 128;
2955     SuitableAlign = 128;
2956     MaxVectorAlign = 256;
2957     SizeType = UnsignedLong;
2958     IntPtrType = SignedLong;
2959     DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2960                         "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
2961                         "a0:0:64-f80:128:128-n8:16:32-S128";
2962     HasAlignMac68kSupport = true;
2963   }
2964 
2965 };
2966 } // end anonymous namespace
2967 
2968 namespace {
2969 // x86-32 Windows target
2970 class WindowsX86_32TargetInfo : public WindowsTargetInfo<X86_32TargetInfo> {
2971 public:
2972   WindowsX86_32TargetInfo(const std::string& triple)
2973     : WindowsTargetInfo<X86_32TargetInfo>(triple) {
2974     TLSSupported = false;
2975     WCharType = UnsignedShort;
2976     DoubleAlign = LongLongAlign = 64;
2977     DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2978                         "i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-"
2979                         "v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32";
2980   }
2981   virtual void getTargetDefines(const LangOptions &Opts,
2982                                 MacroBuilder &Builder) const {
2983     WindowsTargetInfo<X86_32TargetInfo>::getTargetDefines(Opts, Builder);
2984   }
2985 };
2986 } // end anonymous namespace
2987 
2988 namespace {
2989 
2990 // x86-32 Windows Visual Studio target
2991 class VisualStudioWindowsX86_32TargetInfo : public WindowsX86_32TargetInfo {
2992 public:
2993   VisualStudioWindowsX86_32TargetInfo(const std::string& triple)
2994     : WindowsX86_32TargetInfo(triple) {
2995     LongDoubleWidth = LongDoubleAlign = 64;
2996     LongDoubleFormat = &llvm::APFloat::IEEEdouble;
2997   }
2998   virtual void getTargetDefines(const LangOptions &Opts,
2999                                 MacroBuilder &Builder) const {
3000     WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
3001     WindowsX86_32TargetInfo::getVisualStudioDefines(Opts, Builder);
3002     // The value of the following reflects processor type.
3003     // 300=386, 400=486, 500=Pentium, 600=Blend (default)
3004     // We lost the original triple, so we use the default.
3005     Builder.defineMacro("_M_IX86", "600");
3006   }
3007 };
3008 } // end anonymous namespace
3009 
3010 namespace {
3011 // x86-32 MinGW target
3012 class MinGWX86_32TargetInfo : public WindowsX86_32TargetInfo {
3013 public:
3014   MinGWX86_32TargetInfo(const std::string& triple)
3015     : WindowsX86_32TargetInfo(triple) {
3016   }
3017   virtual void getTargetDefines(const LangOptions &Opts,
3018                                 MacroBuilder &Builder) const {
3019     WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
3020     DefineStd(Builder, "WIN32", Opts);
3021     DefineStd(Builder, "WINNT", Opts);
3022     Builder.defineMacro("_X86_");
3023     Builder.defineMacro("__MSVCRT__");
3024     Builder.defineMacro("__MINGW32__");
3025 
3026     // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
3027     // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
3028     if (Opts.MicrosoftExt)
3029       // Provide "as-is" __declspec.
3030       Builder.defineMacro("__declspec", "__declspec");
3031     else
3032       // Provide alias of __attribute__ like mingw32-gcc.
3033       Builder.defineMacro("__declspec(a)", "__attribute__((a))");
3034   }
3035 };
3036 } // end anonymous namespace
3037 
3038 namespace {
3039 // x86-32 Cygwin target
3040 class CygwinX86_32TargetInfo : public X86_32TargetInfo {
3041 public:
3042   CygwinX86_32TargetInfo(const std::string& triple)
3043     : X86_32TargetInfo(triple) {
3044     TLSSupported = false;
3045     WCharType = UnsignedShort;
3046     DoubleAlign = LongLongAlign = 64;
3047     DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3048                         "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
3049                         "a0:0:64-f80:32:32-n8:16:32-S32";
3050   }
3051   virtual void getTargetDefines(const LangOptions &Opts,
3052                                 MacroBuilder &Builder) const {
3053     X86_32TargetInfo::getTargetDefines(Opts, Builder);
3054     Builder.defineMacro("_X86_");
3055     Builder.defineMacro("__CYGWIN__");
3056     Builder.defineMacro("__CYGWIN32__");
3057     DefineStd(Builder, "unix", Opts);
3058     if (Opts.CPlusPlus)
3059       Builder.defineMacro("_GNU_SOURCE");
3060   }
3061 };
3062 } // end anonymous namespace
3063 
3064 namespace {
3065 // x86-32 Haiku target
3066 class HaikuX86_32TargetInfo : public X86_32TargetInfo {
3067 public:
3068   HaikuX86_32TargetInfo(const std::string& triple)
3069     : X86_32TargetInfo(triple) {
3070     SizeType = UnsignedLong;
3071     IntPtrType = SignedLong;
3072     PtrDiffType = SignedLong;
3073     ProcessIDType = SignedLong;
3074     this->UserLabelPrefix = "";
3075     this->TLSSupported = false;
3076   }
3077   virtual void getTargetDefines(const LangOptions &Opts,
3078                                 MacroBuilder &Builder) const {
3079     X86_32TargetInfo::getTargetDefines(Opts, Builder);
3080     Builder.defineMacro("__INTEL__");
3081     Builder.defineMacro("__HAIKU__");
3082   }
3083 };
3084 } // end anonymous namespace
3085 
3086 // RTEMS Target
3087 template<typename Target>
3088 class RTEMSTargetInfo : public OSTargetInfo<Target> {
3089 protected:
3090   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
3091                             MacroBuilder &Builder) const {
3092     // RTEMS defines; list based off of gcc output
3093 
3094     Builder.defineMacro("__rtems__");
3095     Builder.defineMacro("__ELF__");
3096   }
3097 public:
3098   RTEMSTargetInfo(const std::string &triple)
3099     : OSTargetInfo<Target>(triple) {
3100       this->UserLabelPrefix = "";
3101 
3102       llvm::Triple Triple(triple);
3103       switch (Triple.getArch()) {
3104         default:
3105         case llvm::Triple::x86:
3106           // this->MCountName = ".mcount";
3107           break;
3108         case llvm::Triple::mips:
3109         case llvm::Triple::mipsel:
3110         case llvm::Triple::ppc:
3111         case llvm::Triple::ppc64:
3112           // this->MCountName = "_mcount";
3113           break;
3114         case llvm::Triple::arm:
3115           // this->MCountName = "__mcount";
3116           break;
3117       }
3118 
3119     }
3120 };
3121 
3122 namespace {
3123 // x86-32 RTEMS target
3124 class RTEMSX86_32TargetInfo : public X86_32TargetInfo {
3125 public:
3126   RTEMSX86_32TargetInfo(const std::string& triple)
3127     : X86_32TargetInfo(triple) {
3128     SizeType = UnsignedLong;
3129     IntPtrType = SignedLong;
3130     PtrDiffType = SignedLong;
3131     this->UserLabelPrefix = "";
3132   }
3133   virtual void getTargetDefines(const LangOptions &Opts,
3134                                 MacroBuilder &Builder) const {
3135     X86_32TargetInfo::getTargetDefines(Opts, Builder);
3136     Builder.defineMacro("__INTEL__");
3137     Builder.defineMacro("__rtems__");
3138   }
3139 };
3140 } // end anonymous namespace
3141 
3142 namespace {
3143 // x86-64 generic target
3144 class X86_64TargetInfo : public X86TargetInfo {
3145 public:
3146   X86_64TargetInfo(const std::string &triple) : X86TargetInfo(triple) {
3147     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
3148     LongDoubleWidth = 128;
3149     LongDoubleAlign = 128;
3150     LargeArrayMinWidth = 128;
3151     LargeArrayAlign = 128;
3152     SuitableAlign = 128;
3153     IntMaxType = SignedLong;
3154     UIntMaxType = UnsignedLong;
3155     Int64Type = SignedLong;
3156     RegParmMax = 6;
3157 
3158     DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3159                         "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
3160                         "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128";
3161 
3162     // Use fpret only for long double.
3163     RealTypeUsesObjCFPRet = (1 << TargetInfo::LongDouble);
3164 
3165     // Use fp2ret for _Complex long double.
3166     ComplexLongDoubleUsesFP2Ret = true;
3167 
3168     // x86-64 has atomics up to 16 bytes.
3169     // FIXME: Once the backend is fixed, increase MaxAtomicInlineWidth to 128
3170     // on CPUs with cmpxchg16b
3171     MaxAtomicPromoteWidth = 128;
3172     MaxAtomicInlineWidth = 64;
3173   }
3174   virtual BuiltinVaListKind getBuiltinVaListKind() const {
3175     return TargetInfo::X86_64ABIBuiltinVaList;
3176   }
3177 
3178   int getEHDataRegisterNumber(unsigned RegNo) const {
3179     if (RegNo == 0) return 0;
3180     if (RegNo == 1) return 1;
3181     return -1;
3182   }
3183 
3184   virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const {
3185     return (CC == CC_Default ||
3186             CC == CC_C ||
3187             CC == CC_IntelOclBicc) ? CCCR_OK : CCCR_Warning;
3188   }
3189 
3190   virtual CallingConv getDefaultCallingConv(CallingConvMethodType MT) const {
3191     return CC_C;
3192   }
3193 
3194 };
3195 } // end anonymous namespace
3196 
3197 namespace {
3198 // x86-64 Windows target
3199 class WindowsX86_64TargetInfo : public WindowsTargetInfo<X86_64TargetInfo> {
3200 public:
3201   WindowsX86_64TargetInfo(const std::string& triple)
3202     : WindowsTargetInfo<X86_64TargetInfo>(triple) {
3203     TLSSupported = false;
3204     WCharType = UnsignedShort;
3205     LongWidth = LongAlign = 32;
3206     DoubleAlign = LongLongAlign = 64;
3207     IntMaxType = SignedLongLong;
3208     UIntMaxType = UnsignedLongLong;
3209     Int64Type = SignedLongLong;
3210     SizeType = UnsignedLongLong;
3211     PtrDiffType = SignedLongLong;
3212     IntPtrType = SignedLongLong;
3213     this->UserLabelPrefix = "";
3214   }
3215   virtual void getTargetDefines(const LangOptions &Opts,
3216                                 MacroBuilder &Builder) const {
3217     WindowsTargetInfo<X86_64TargetInfo>::getTargetDefines(Opts, Builder);
3218     Builder.defineMacro("_WIN64");
3219   }
3220   virtual BuiltinVaListKind getBuiltinVaListKind() const {
3221     return TargetInfo::CharPtrBuiltinVaList;
3222   }
3223 };
3224 } // end anonymous namespace
3225 
3226 namespace {
3227 // x86-64 Windows Visual Studio target
3228 class VisualStudioWindowsX86_64TargetInfo : public WindowsX86_64TargetInfo {
3229 public:
3230   VisualStudioWindowsX86_64TargetInfo(const std::string& triple)
3231     : WindowsX86_64TargetInfo(triple) {
3232     LongDoubleWidth = LongDoubleAlign = 64;
3233     LongDoubleFormat = &llvm::APFloat::IEEEdouble;
3234   }
3235   virtual void getTargetDefines(const LangOptions &Opts,
3236                                 MacroBuilder &Builder) const {
3237     WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
3238     WindowsX86_64TargetInfo::getVisualStudioDefines(Opts, Builder);
3239     Builder.defineMacro("_M_X64");
3240     Builder.defineMacro("_M_AMD64");
3241   }
3242 };
3243 } // end anonymous namespace
3244 
3245 namespace {
3246 // x86-64 MinGW target
3247 class MinGWX86_64TargetInfo : public WindowsX86_64TargetInfo {
3248 public:
3249   MinGWX86_64TargetInfo(const std::string& triple)
3250     : WindowsX86_64TargetInfo(triple) {
3251   }
3252   virtual void getTargetDefines(const LangOptions &Opts,
3253                                 MacroBuilder &Builder) const {
3254     WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
3255     DefineStd(Builder, "WIN64", Opts);
3256     Builder.defineMacro("__MSVCRT__");
3257     Builder.defineMacro("__MINGW32__");
3258     Builder.defineMacro("__MINGW64__");
3259 
3260     // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
3261     // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
3262     if (Opts.MicrosoftExt)
3263       // Provide "as-is" __declspec.
3264       Builder.defineMacro("__declspec", "__declspec");
3265     else
3266       // Provide alias of __attribute__ like mingw32-gcc.
3267       Builder.defineMacro("__declspec(a)", "__attribute__((a))");
3268   }
3269 };
3270 } // end anonymous namespace
3271 
3272 namespace {
3273 class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
3274 public:
3275   DarwinX86_64TargetInfo(const std::string& triple)
3276       : DarwinTargetInfo<X86_64TargetInfo>(triple) {
3277     Int64Type = SignedLongLong;
3278     MaxVectorAlign = 256;
3279   }
3280 };
3281 } // end anonymous namespace
3282 
3283 namespace {
3284 class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
3285 public:
3286   OpenBSDX86_64TargetInfo(const std::string& triple)
3287       : OpenBSDTargetInfo<X86_64TargetInfo>(triple) {
3288     IntMaxType = SignedLongLong;
3289     UIntMaxType = UnsignedLongLong;
3290     Int64Type = SignedLongLong;
3291   }
3292 };
3293 } // end anonymous namespace
3294 
3295 namespace {
3296 class BitrigX86_64TargetInfo : public BitrigTargetInfo<X86_64TargetInfo> {
3297 public:
3298   BitrigX86_64TargetInfo(const std::string& triple)
3299       : BitrigTargetInfo<X86_64TargetInfo>(triple) {
3300      IntMaxType = SignedLongLong;
3301      UIntMaxType = UnsignedLongLong;
3302      Int64Type = SignedLongLong;
3303   }
3304 };
3305 }
3306 
3307 namespace {
3308 class AArch64TargetInfo : public TargetInfo {
3309   static const char * const GCCRegNames[];
3310   static const TargetInfo::GCCRegAlias GCCRegAliases[];
3311 
3312   static const Builtin::Info BuiltinInfo[];
3313 public:
3314   AArch64TargetInfo(const std::string& triple) : TargetInfo(triple) {
3315     BigEndian = false;
3316     LongWidth = LongAlign = 64;
3317     LongDoubleWidth = LongDoubleAlign = 128;
3318     PointerWidth = PointerAlign = 64;
3319     SuitableAlign = 128;
3320     DescriptionString = "e-p:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3321                         "i64:64:64-i128:128:128-f32:32:32-f64:64:64-"
3322                         "f128:128:128-n32:64-S128";
3323 
3324     WCharType = UnsignedInt;
3325     LongDoubleFormat = &llvm::APFloat::IEEEquad;
3326 
3327     // AArch64 backend supports 64-bit operations at the moment. In principle
3328     // 128-bit is possible if register-pairs are used.
3329     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
3330 
3331     TheCXXABI.set(TargetCXXABI::GenericAArch64);
3332   }
3333   virtual void getTargetDefines(const LangOptions &Opts,
3334                                 MacroBuilder &Builder) const {
3335     // GCC defines theses currently
3336     Builder.defineMacro("__aarch64__");
3337     Builder.defineMacro("__AARCH64EL__");
3338 
3339     // ACLE predefines. Many can only have one possible value on v8 AArch64.
3340 
3341     // FIXME: these were written based on an unreleased version of a 32-bit ACLE
3342     // which was intended to be compatible with a 64-bit implementation. They
3343     // will need updating when a real 64-bit ACLE exists. Particularly pressing
3344     // instances are: __ARM_ARCH_ISA_ARM, __ARM_ARCH_ISA_THUMB, __ARM_PCS.
3345     Builder.defineMacro("__ARM_ACLE",         "101");
3346     Builder.defineMacro("__ARM_ARCH",         "8");
3347     Builder.defineMacro("__ARM_ARCH_PROFILE", "'A'");
3348 
3349     Builder.defineMacro("__ARM_FEATURE_UNALIGNED");
3350     Builder.defineMacro("__ARM_FEATURE_CLZ");
3351     Builder.defineMacro("__ARM_FEATURE_FMA");
3352 
3353     // FIXME: ACLE 1.1 reserves bit 4. Will almost certainly come to mean
3354     // 128-bit LDXP present, at which point this becomes 0x1f.
3355     Builder.defineMacro("__ARM_FEATURE_LDREX", "0xf");
3356 
3357     // 0xe implies support for half, single and double precision operations.
3358     Builder.defineMacro("__ARM_FP", "0xe");
3359 
3360     // PCS specifies this for SysV variants, which is all we support. Other ABIs
3361     // may choose __ARM_FP16_FORMAT_ALTERNATIVE.
3362     Builder.defineMacro("__ARM_FP16_FORMAT_IEEE");
3363 
3364     if (Opts.FastMath || Opts.FiniteMathOnly)
3365       Builder.defineMacro("__ARM_FP_FAST");
3366 
3367     if ((Opts.C99 || Opts.C11) && !Opts.Freestanding)
3368       Builder.defineMacro("__ARM_FP_FENV_ROUNDING");
3369 
3370     Builder.defineMacro("__ARM_SIZEOF_WCHAR_T",
3371                         Opts.ShortWChar ? "2" : "4");
3372 
3373     Builder.defineMacro("__ARM_SIZEOF_MINIMAL_ENUM",
3374                         Opts.ShortEnums ? "1" : "4");
3375 
3376     if (BigEndian)
3377       Builder.defineMacro("__ARM_BIG_ENDIAN");
3378   }
3379   virtual void getTargetBuiltins(const Builtin::Info *&Records,
3380                                  unsigned &NumRecords) const {
3381     Records = BuiltinInfo;
3382     NumRecords = clang::AArch64::LastTSBuiltin-Builtin::FirstTSBuiltin;
3383   }
3384   virtual bool hasFeature(StringRef Feature) const {
3385     return Feature == "aarch64";
3386   }
3387   virtual void getGCCRegNames(const char * const *&Names,
3388                               unsigned &NumNames) const;
3389   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3390                                 unsigned &NumAliases) const;
3391 
3392   virtual bool isCLZForZeroUndef() const { return false; }
3393 
3394   virtual bool validateAsmConstraint(const char *&Name,
3395                                      TargetInfo::ConstraintInfo &Info) const {
3396     switch (*Name) {
3397     default: return false;
3398     case 'w': // An FP/SIMD vector register
3399       Info.setAllowsRegister();
3400       return true;
3401     case 'I': // Constant that can be used with an ADD instruction
3402     case 'J': // Constant that can be used with a SUB instruction
3403     case 'K': // Constant that can be used with a 32-bit logical instruction
3404     case 'L': // Constant that can be used with a 64-bit logical instruction
3405     case 'M': // Constant that can be used as a 32-bit MOV immediate
3406     case 'N': // Constant that can be used as a 64-bit MOV immediate
3407     case 'Y': // Floating point constant zero
3408     case 'Z': // Integer constant zero
3409       return true;
3410     case 'Q': // A memory reference with base register and no offset
3411       Info.setAllowsMemory();
3412       return true;
3413     case 'S': // A symbolic address
3414       Info.setAllowsRegister();
3415       return true;
3416     case 'U':
3417       // Ump: A memory address suitable for ldp/stp in SI, DI, SF and DF modes, whatever they may be
3418       // Utf: A memory address suitable for ldp/stp in TF mode, whatever it may be
3419       // Usa: An absolute symbolic address
3420       // Ush: The high part (bits 32:12) of a pc-relative symbolic address
3421       llvm_unreachable("FIXME: Unimplemented support for bizarre constraints");
3422     }
3423   }
3424 
3425   virtual const char *getClobbers() const {
3426     // There are no AArch64 clobbers shared by all asm statements.
3427     return "";
3428   }
3429 
3430   virtual BuiltinVaListKind getBuiltinVaListKind() const {
3431     return TargetInfo::AArch64ABIBuiltinVaList;
3432   }
3433 };
3434 
3435 const char * const AArch64TargetInfo::GCCRegNames[] = {
3436   "w0", "w1", "w2", "w3", "w4", "w5", "w6", "w7",
3437   "w8", "w9", "w10", "w11", "w12", "w13", "w14", "w15",
3438   "w16", "w17", "w18", "w19", "w20", "w21", "w22", "w23",
3439   "w24", "w25", "w26", "w27", "w28", "w29", "w30", "wsp", "wzr",
3440 
3441   "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
3442   "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
3443   "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
3444   "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp", "xzr",
3445 
3446   "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7",
3447   "b8", "b9", "b10", "b11", "b12", "b13", "b14", "b15",
3448   "b16", "b17", "b18", "b19", "b20", "b21", "b22", "b23",
3449   "b24", "b25", "b26", "b27", "b28", "b29", "b30", "b31",
3450 
3451   "h0", "h1", "h2", "h3", "h4", "h5", "h6", "h7",
3452   "h8", "h9", "h10", "h11", "h12", "h13", "h14", "h15",
3453   "h16", "h17", "h18", "h19", "h20", "h21", "h22", "h23",
3454   "h24", "h25", "h26", "h27", "h28", "h29", "h30", "h31",
3455 
3456   "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
3457   "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
3458   "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
3459   "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
3460 
3461   "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
3462   "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
3463   "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
3464   "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
3465 
3466   "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
3467   "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
3468   "q16", "q17", "q18", "q19", "q20", "q21", "q22", "q23",
3469   "q24", "q25", "q26", "q27", "q28", "q29", "q30", "q31"
3470 };
3471 
3472 void AArch64TargetInfo::getGCCRegNames(const char * const *&Names,
3473                                        unsigned &NumNames) const {
3474   Names = GCCRegNames;
3475   NumNames = llvm::array_lengthof(GCCRegNames);
3476 }
3477 
3478 const TargetInfo::GCCRegAlias AArch64TargetInfo::GCCRegAliases[] = {
3479   { { "x16" }, "ip0"},
3480   { { "x17" }, "ip1"},
3481   { { "x29" }, "fp" },
3482   { { "x30" }, "lr" }
3483 };
3484 
3485 void AArch64TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3486                                          unsigned &NumAliases) const {
3487   Aliases = GCCRegAliases;
3488   NumAliases = llvm::array_lengthof(GCCRegAliases);
3489 
3490 }
3491 
3492 const Builtin::Info AArch64TargetInfo::BuiltinInfo[] = {
3493 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3494 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3495                                               ALL_LANGUAGES },
3496 #include "clang/Basic/BuiltinsAArch64.def"
3497 };
3498 
3499 } // end anonymous namespace
3500 
3501 namespace {
3502 class ARMTargetInfo : public TargetInfo {
3503   // Possible FPU choices.
3504   enum FPUMode {
3505     VFP2FPU = (1 << 0),
3506     VFP3FPU = (1 << 1),
3507     VFP4FPU = (1 << 2),
3508     NeonFPU = (1 << 3)
3509   };
3510 
3511   static bool FPUModeIsVFP(FPUMode Mode) {
3512     return Mode & (VFP2FPU | VFP3FPU | VFP4FPU | NeonFPU);
3513   }
3514 
3515   static const TargetInfo::GCCRegAlias GCCRegAliases[];
3516   static const char * const GCCRegNames[];
3517 
3518   std::string ABI, CPU;
3519 
3520   unsigned FPU : 4;
3521 
3522   unsigned IsAAPCS : 1;
3523   unsigned IsThumb : 1;
3524 
3525   // Initialized via features.
3526   unsigned SoftFloat : 1;
3527   unsigned SoftFloatABI : 1;
3528 
3529   static const Builtin::Info BuiltinInfo[];
3530 
3531   static bool shouldUseInlineAtomic(const llvm::Triple &T) {
3532     // On linux, binaries targeting old cpus call functions in libgcc to
3533     // perform atomic operations. The implementation in libgcc then calls into
3534     // the kernel which on armv6 and newer uses ldrex and strex. The net result
3535     // is that if we assume the kernel is at least as recent as the hardware,
3536     // it is safe to use atomic instructions on armv6 and newer.
3537     if (T.getOS() != llvm::Triple::Linux && T.getOS() != llvm::Triple::FreeBSD)
3538       return false;
3539     StringRef ArchName = T.getArchName();
3540     if (T.getArch() == llvm::Triple::arm) {
3541       if (!ArchName.startswith("armv"))
3542         return false;
3543       StringRef VersionStr = ArchName.substr(4);
3544       unsigned Version;
3545       if (VersionStr.getAsInteger(10, Version))
3546         return false;
3547       return Version >= 6;
3548     }
3549     assert(T.getArch() == llvm::Triple::thumb);
3550     if (!ArchName.startswith("thumbv"))
3551       return false;
3552     StringRef VersionStr = ArchName.substr(6);
3553     unsigned Version;
3554     if (VersionStr.getAsInteger(10, Version))
3555       return false;
3556     return Version >= 7;
3557   }
3558 
3559 public:
3560   ARMTargetInfo(const std::string &TripleStr)
3561     : TargetInfo(TripleStr), ABI("aapcs-linux"), CPU("arm1136j-s"), IsAAPCS(true)
3562   {
3563     BigEndian = false;
3564     SizeType = UnsignedInt;
3565     PtrDiffType = SignedInt;
3566     // AAPCS 7.1.1, ARM-Linux ABI 2.4: type of wchar_t is unsigned int.
3567     WCharType = UnsignedInt;
3568 
3569     // {} in inline assembly are neon specifiers, not assembly variant
3570     // specifiers.
3571     NoAsmVariants = true;
3572 
3573     // FIXME: Should we just treat this as a feature?
3574     IsThumb = getTriple().getArchName().startswith("thumb");
3575     if (IsThumb) {
3576       // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
3577       // so set preferred for small types to 32.
3578       DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
3579                            "i64:64:64-f32:32:32-f64:64:64-"
3580                            "v64:64:64-v128:64:128-a0:0:32-n32-S64");
3581     } else {
3582       DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3583                            "i64:64:64-f32:32:32-f64:64:64-"
3584                            "v64:64:64-v128:64:128-a0:0:64-n32-S64");
3585     }
3586 
3587     // ARM targets default to using the ARM C++ ABI.
3588     TheCXXABI.set(TargetCXXABI::GenericARM);
3589 
3590     // ARM has atomics up to 8 bytes
3591     MaxAtomicPromoteWidth = 64;
3592     if (shouldUseInlineAtomic(getTriple()))
3593       MaxAtomicInlineWidth = 64;
3594 
3595     // Do force alignment of members that follow zero length bitfields.  If
3596     // the alignment of the zero-length bitfield is greater than the member
3597     // that follows it, `bar', `bar' will be aligned as the  type of the
3598     // zero length bitfield.
3599     UseZeroLengthBitfieldAlignment = true;
3600   }
3601   virtual const char *getABI() const { return ABI.c_str(); }
3602   virtual bool setABI(const std::string &Name) {
3603     ABI = Name;
3604 
3605     // The defaults (above) are for AAPCS, check if we need to change them.
3606     //
3607     // FIXME: We need support for -meabi... we could just mangle it into the
3608     // name.
3609     if (Name == "apcs-gnu") {
3610       DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 32;
3611       // size_t is unsigned int on FreeBSD.
3612       if (getTriple().getOS() != llvm::Triple::FreeBSD)
3613         SizeType = UnsignedLong;
3614 
3615       // Revert to using SignedInt on apcs-gnu to comply with existing behaviour.
3616       WCharType = SignedInt;
3617 
3618       // Do not respect the alignment of bit-field types when laying out
3619       // structures. This corresponds to PCC_BITFIELD_TYPE_MATTERS in gcc.
3620       UseBitFieldTypeAlignment = false;
3621 
3622       /// gcc forces the alignment to 4 bytes, regardless of the type of the
3623       /// zero length bitfield.  This corresponds to EMPTY_FIELD_BOUNDARY in
3624       /// gcc.
3625       ZeroLengthBitfieldBoundary = 32;
3626 
3627       IsAAPCS = false;
3628 
3629       if (IsThumb) {
3630         // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
3631         // so set preferred for small types to 32.
3632         DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
3633                              "i64:32:64-f32:32:32-f64:32:64-"
3634                              "v64:32:64-v128:32:128-a0:0:32-n32-S32");
3635       } else {
3636         DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
3637                              "i64:32:64-f32:32:32-f64:32:64-"
3638                              "v64:32:64-v128:32:128-a0:0:32-n32-S32");
3639       }
3640 
3641       // FIXME: Override "preferred align" for double and long long.
3642     } else if (Name == "aapcs" || Name == "aapcs-vfp") {
3643       IsAAPCS = true;
3644       // FIXME: Enumerated types are variable width in straight AAPCS.
3645     } else if (Name == "aapcs-linux") {
3646       IsAAPCS = true;
3647     } else
3648       return false;
3649 
3650     return true;
3651   }
3652 
3653   void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
3654     if (CPU == "arm1136jf-s" || CPU == "arm1176jzf-s" || CPU == "mpcore")
3655       Features["vfp2"] = true;
3656     else if (CPU == "cortex-a8" || CPU == "cortex-a15" ||
3657              CPU == "cortex-a9" || CPU == "cortex-a9-mp")
3658       Features["neon"] = true;
3659     else if (CPU == "swift" || CPU == "cortex-a7") {
3660       Features["vfp4"] = true;
3661       Features["neon"] = true;
3662     }
3663   }
3664 
3665   virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
3666                                  StringRef Name,
3667                                  bool Enabled) const {
3668     if (Name == "soft-float" || Name == "soft-float-abi" ||
3669         Name == "vfp2" || Name == "vfp3" || Name == "vfp4" || Name == "neon" ||
3670         Name == "d16" || Name == "neonfp") {
3671       Features[Name] = Enabled;
3672     } else
3673       return false;
3674 
3675     return true;
3676   }
3677 
3678   virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
3679     FPU = 0;
3680     SoftFloat = SoftFloatABI = false;
3681     for (unsigned i = 0, e = Features.size(); i != e; ++i) {
3682       if (Features[i] == "+soft-float")
3683         SoftFloat = true;
3684       else if (Features[i] == "+soft-float-abi")
3685         SoftFloatABI = true;
3686       else if (Features[i] == "+vfp2")
3687         FPU |= VFP2FPU;
3688       else if (Features[i] == "+vfp3")
3689         FPU |= VFP3FPU;
3690       else if (Features[i] == "+vfp4")
3691         FPU |= VFP4FPU;
3692       else if (Features[i] == "+neon")
3693         FPU |= NeonFPU;
3694     }
3695 
3696     // Remove front-end specific options which the backend handles differently.
3697     std::vector<std::string>::iterator it;
3698     it = std::find(Features.begin(), Features.end(), "+soft-float");
3699     if (it != Features.end())
3700       Features.erase(it);
3701     it = std::find(Features.begin(), Features.end(), "+soft-float-abi");
3702     if (it != Features.end())
3703       Features.erase(it);
3704   }
3705 
3706   virtual bool hasFeature(StringRef Feature) const {
3707     return llvm::StringSwitch<bool>(Feature)
3708         .Case("arm", true)
3709         .Case("softfloat", SoftFloat)
3710         .Case("thumb", IsThumb)
3711         .Case("neon", FPU == NeonFPU && !SoftFloat &&
3712               StringRef(getCPUDefineSuffix(CPU)).startswith("7"))
3713         .Default(false);
3714   }
3715   // FIXME: Should we actually have some table instead of these switches?
3716   static const char *getCPUDefineSuffix(StringRef Name) {
3717     return llvm::StringSwitch<const char*>(Name)
3718       .Cases("arm8", "arm810", "4")
3719       .Cases("strongarm", "strongarm110", "strongarm1100", "strongarm1110", "4")
3720       .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "arm720t", "arm9", "4T")
3721       .Cases("arm9tdmi", "arm920", "arm920t", "arm922t", "arm940t", "4T")
3722       .Case("ep9312", "4T")
3723       .Cases("arm10tdmi", "arm1020t", "5T")
3724       .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "5TE")
3725       .Case("arm926ej-s", "5TEJ")
3726       .Cases("arm10e", "arm1020e", "arm1022e", "5TE")
3727       .Cases("xscale", "iwmmxt", "5TE")
3728       .Case("arm1136j-s", "6J")
3729       .Cases("arm1176jz-s", "arm1176jzf-s", "6ZK")
3730       .Cases("arm1136jf-s", "mpcorenovfp", "mpcore", "6K")
3731       .Cases("arm1156t2-s", "arm1156t2f-s", "6T2")
3732       .Cases("cortex-a5", "cortex-a7", "cortex-a8", "7A")
3733       .Cases("cortex-a9", "cortex-a15", "7A")
3734       .Case("cortex-r5", "7R")
3735       .Case("cortex-a9-mp", "7F")
3736       .Case("swift", "7S")
3737       .Cases("cortex-m3", "cortex-m4", "7M")
3738       .Case("cortex-m0", "6M")
3739       .Default(0);
3740   }
3741   static const char *getCPUProfile(StringRef Name) {
3742     return llvm::StringSwitch<const char*>(Name)
3743       .Cases("cortex-a8", "cortex-a9", "A")
3744       .Cases("cortex-m3", "cortex-m4", "cortex-m0", "M")
3745       .Case("cortex-r5", "R")
3746       .Default("");
3747   }
3748   virtual bool setCPU(const std::string &Name) {
3749     if (!getCPUDefineSuffix(Name))
3750       return false;
3751 
3752     CPU = Name;
3753     return true;
3754   }
3755   virtual void getTargetDefines(const LangOptions &Opts,
3756                                 MacroBuilder &Builder) const {
3757     // Target identification.
3758     Builder.defineMacro("__arm");
3759     Builder.defineMacro("__arm__");
3760 
3761     // Target properties.
3762     Builder.defineMacro("__ARMEL__");
3763     Builder.defineMacro("__LITTLE_ENDIAN__");
3764     Builder.defineMacro("__REGISTER_PREFIX__", "");
3765 
3766     StringRef CPUArch = getCPUDefineSuffix(CPU);
3767     Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
3768     Builder.defineMacro("__ARM_ARCH", CPUArch.substr(0, 1));
3769     StringRef CPUProfile = getCPUProfile(CPU);
3770     if (!CPUProfile.empty())
3771       Builder.defineMacro("__ARM_ARCH_PROFILE", CPUProfile);
3772 
3773     // Subtarget options.
3774 
3775     // FIXME: It's more complicated than this and we don't really support
3776     // interworking.
3777     if ('5' <= CPUArch[0] && CPUArch[0] <= '7')
3778       Builder.defineMacro("__THUMB_INTERWORK__");
3779 
3780     if (ABI == "aapcs" || ABI == "aapcs-linux" || ABI == "aapcs-vfp") {
3781       // M-class CPUs on Darwin follow AAPCS, but not EABI.
3782       if (!(getTriple().isOSDarwin() && CPUProfile == "M"))
3783         Builder.defineMacro("__ARM_EABI__");
3784       Builder.defineMacro("__ARM_PCS", "1");
3785 
3786       if ((!SoftFloat && !SoftFloatABI) || ABI == "aapcs-vfp")
3787         Builder.defineMacro("__ARM_PCS_VFP", "1");
3788     }
3789 
3790     if (SoftFloat)
3791       Builder.defineMacro("__SOFTFP__");
3792 
3793     if (CPU == "xscale")
3794       Builder.defineMacro("__XSCALE__");
3795 
3796     bool IsARMv7 = CPUArch.startswith("7");
3797     if (IsThumb) {
3798       Builder.defineMacro("__THUMBEL__");
3799       Builder.defineMacro("__thumb__");
3800       if (CPUArch == "6T2" || IsARMv7)
3801         Builder.defineMacro("__thumb2__");
3802     }
3803 
3804     // Note, this is always on in gcc, even though it doesn't make sense.
3805     Builder.defineMacro("__APCS_32__");
3806 
3807     if (FPUModeIsVFP((FPUMode) FPU)) {
3808       Builder.defineMacro("__VFP_FP__");
3809       if (FPU & VFP2FPU)
3810         Builder.defineMacro("__ARM_VFPV2__");
3811       if (FPU & VFP3FPU)
3812         Builder.defineMacro("__ARM_VFPV3__");
3813       if (FPU & VFP4FPU)
3814         Builder.defineMacro("__ARM_VFPV4__");
3815     }
3816 
3817     // This only gets set when Neon instructions are actually available, unlike
3818     // the VFP define, hence the soft float and arch check. This is subtly
3819     // different from gcc, we follow the intent which was that it should be set
3820     // when Neon instructions are actually available.
3821     if ((FPU & NeonFPU) && !SoftFloat && IsARMv7)
3822       Builder.defineMacro("__ARM_NEON__");
3823   }
3824   virtual void getTargetBuiltins(const Builtin::Info *&Records,
3825                                  unsigned &NumRecords) const {
3826     Records = BuiltinInfo;
3827     NumRecords = clang::ARM::LastTSBuiltin-Builtin::FirstTSBuiltin;
3828   }
3829   virtual bool isCLZForZeroUndef() const { return false; }
3830   virtual BuiltinVaListKind getBuiltinVaListKind() const {
3831     return IsAAPCS ? AAPCSABIBuiltinVaList : TargetInfo::VoidPtrBuiltinVaList;
3832   }
3833   virtual void getGCCRegNames(const char * const *&Names,
3834                               unsigned &NumNames) const;
3835   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
3836                                 unsigned &NumAliases) const;
3837   virtual bool validateAsmConstraint(const char *&Name,
3838                                      TargetInfo::ConstraintInfo &Info) const {
3839     switch (*Name) {
3840     default: break;
3841     case 'l': // r0-r7
3842     case 'h': // r8-r15
3843     case 'w': // VFP Floating point register single precision
3844     case 'P': // VFP Floating point register double precision
3845       Info.setAllowsRegister();
3846       return true;
3847     case 'Q': // A memory address that is a single base register.
3848       Info.setAllowsMemory();
3849       return true;
3850     case 'U': // a memory reference...
3851       switch (Name[1]) {
3852       case 'q': // ...ARMV4 ldrsb
3853       case 'v': // ...VFP load/store (reg+constant offset)
3854       case 'y': // ...iWMMXt load/store
3855       case 't': // address valid for load/store opaque types wider
3856                 // than 128-bits
3857       case 'n': // valid address for Neon doubleword vector load/store
3858       case 'm': // valid address for Neon element and structure load/store
3859       case 's': // valid address for non-offset loads/stores of quad-word
3860                 // values in four ARM registers
3861         Info.setAllowsMemory();
3862         Name++;
3863         return true;
3864       }
3865     }
3866     return false;
3867   }
3868   virtual std::string convertConstraint(const char *&Constraint) const {
3869     std::string R;
3870     switch (*Constraint) {
3871     case 'U':   // Two-character constraint; add "^" hint for later parsing.
3872       R = std::string("^") + std::string(Constraint, 2);
3873       Constraint++;
3874       break;
3875     case 'p': // 'p' should be translated to 'r' by default.
3876       R = std::string("r");
3877       break;
3878     default:
3879       return std::string(1, *Constraint);
3880     }
3881     return R;
3882   }
3883   virtual bool validateConstraintModifier(StringRef Constraint,
3884                                           const char Modifier,
3885                                           unsigned Size) const {
3886     bool isOutput = (Constraint[0] == '=');
3887     bool isInOut = (Constraint[0] == '+');
3888 
3889     // Strip off constraint modifiers.
3890     while (Constraint[0] == '=' ||
3891            Constraint[0] == '+' ||
3892            Constraint[0] == '&')
3893       Constraint = Constraint.substr(1);
3894 
3895     switch (Constraint[0]) {
3896     default: break;
3897     case 'r': {
3898       switch (Modifier) {
3899       default:
3900         return (isInOut || isOutput || Size <= 32);
3901       case 'q':
3902         // A register of size 32 cannot fit a vector type.
3903         return false;
3904       }
3905     }
3906     }
3907 
3908     return true;
3909   }
3910   virtual const char *getClobbers() const {
3911     // FIXME: Is this really right?
3912     return "";
3913   }
3914 
3915   virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const {
3916     return (CC == CC_AAPCS || CC == CC_AAPCS_VFP) ? CCCR_OK : CCCR_Warning;
3917   }
3918 
3919   virtual int getEHDataRegisterNumber(unsigned RegNo) const {
3920     if (RegNo == 0) return 0;
3921     if (RegNo == 1) return 1;
3922     return -1;
3923   }
3924 };
3925 
3926 const char * const ARMTargetInfo::GCCRegNames[] = {
3927   // Integer registers
3928   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3929   "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
3930 
3931   // Float registers
3932   "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
3933   "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
3934   "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
3935   "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
3936 
3937   // Double registers
3938   "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
3939   "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
3940   "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
3941   "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
3942 
3943   // Quad registers
3944   "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
3945   "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"
3946 };
3947 
3948 void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
3949                                    unsigned &NumNames) const {
3950   Names = GCCRegNames;
3951   NumNames = llvm::array_lengthof(GCCRegNames);
3952 }
3953 
3954 const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
3955   { { "a1" }, "r0" },
3956   { { "a2" }, "r1" },
3957   { { "a3" }, "r2" },
3958   { { "a4" }, "r3" },
3959   { { "v1" }, "r4" },
3960   { { "v2" }, "r5" },
3961   { { "v3" }, "r6" },
3962   { { "v4" }, "r7" },
3963   { { "v5" }, "r8" },
3964   { { "v6", "rfp" }, "r9" },
3965   { { "sl" }, "r10" },
3966   { { "fp" }, "r11" },
3967   { { "ip" }, "r12" },
3968   { { "r13" }, "sp" },
3969   { { "r14" }, "lr" },
3970   { { "r15" }, "pc" },
3971   // The S, D and Q registers overlap, but aren't really aliases; we
3972   // don't want to substitute one of these for a different-sized one.
3973 };
3974 
3975 void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
3976                                        unsigned &NumAliases) const {
3977   Aliases = GCCRegAliases;
3978   NumAliases = llvm::array_lengthof(GCCRegAliases);
3979 }
3980 
3981 const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
3982 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
3983 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
3984                                               ALL_LANGUAGES },
3985 #include "clang/Basic/BuiltinsARM.def"
3986 };
3987 } // end anonymous namespace.
3988 
3989 namespace {
3990 class DarwinARMTargetInfo :
3991   public DarwinTargetInfo<ARMTargetInfo> {
3992 protected:
3993   virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
3994                             MacroBuilder &Builder) const {
3995     getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
3996   }
3997 
3998 public:
3999   DarwinARMTargetInfo(const std::string& triple)
4000     : DarwinTargetInfo<ARMTargetInfo>(triple) {
4001     HasAlignMac68kSupport = true;
4002     // iOS always has 64-bit atomic instructions.
4003     // FIXME: This should be based off of the target features in ARMTargetInfo.
4004     MaxAtomicInlineWidth = 64;
4005 
4006     // Darwin on iOS uses a variant of the ARM C++ ABI.
4007     TheCXXABI.set(TargetCXXABI::iOS);
4008   }
4009 };
4010 } // end anonymous namespace.
4011 
4012 
4013 namespace {
4014 // Hexagon abstract base class
4015 class HexagonTargetInfo : public TargetInfo {
4016   static const Builtin::Info BuiltinInfo[];
4017   static const char * const GCCRegNames[];
4018   static const TargetInfo::GCCRegAlias GCCRegAliases[];
4019   std::string CPU;
4020 public:
4021   HexagonTargetInfo(const std::string& triple) : TargetInfo(triple)  {
4022     BigEndian = false;
4023     DescriptionString = ("e-p:32:32:32-"
4024                          "i64:64:64-i32:32:32-i16:16:16-i1:32:32-"
4025                          "f64:64:64-f32:32:32-a0:0-n32");
4026 
4027     // {} in inline assembly are packet specifiers, not assembly variant
4028     // specifiers.
4029     NoAsmVariants = true;
4030   }
4031 
4032   virtual void getTargetBuiltins(const Builtin::Info *&Records,
4033                                  unsigned &NumRecords) const {
4034     Records = BuiltinInfo;
4035     NumRecords = clang::Hexagon::LastTSBuiltin-Builtin::FirstTSBuiltin;
4036   }
4037 
4038   virtual bool validateAsmConstraint(const char *&Name,
4039                                      TargetInfo::ConstraintInfo &Info) const {
4040     return true;
4041   }
4042 
4043   virtual void getTargetDefines(const LangOptions &Opts,
4044                                 MacroBuilder &Builder) const;
4045 
4046   virtual bool hasFeature(StringRef Feature) const {
4047     return Feature == "hexagon";
4048   }
4049 
4050   virtual BuiltinVaListKind getBuiltinVaListKind() const {
4051     return TargetInfo::CharPtrBuiltinVaList;
4052   }
4053   virtual void getGCCRegNames(const char * const *&Names,
4054                               unsigned &NumNames) const;
4055   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4056                                 unsigned &NumAliases) const;
4057   virtual const char *getClobbers() const {
4058     return "";
4059   }
4060 
4061   static const char *getHexagonCPUSuffix(StringRef Name) {
4062     return llvm::StringSwitch<const char*>(Name)
4063       .Case("hexagonv4", "4")
4064       .Case("hexagonv5", "5")
4065       .Default(0);
4066   }
4067 
4068   virtual bool setCPU(const std::string &Name) {
4069     if (!getHexagonCPUSuffix(Name))
4070       return false;
4071 
4072     CPU = Name;
4073     return true;
4074   }
4075 };
4076 
4077 void HexagonTargetInfo::getTargetDefines(const LangOptions &Opts,
4078                                 MacroBuilder &Builder) const {
4079   Builder.defineMacro("qdsp6");
4080   Builder.defineMacro("__qdsp6", "1");
4081   Builder.defineMacro("__qdsp6__", "1");
4082 
4083   Builder.defineMacro("hexagon");
4084   Builder.defineMacro("__hexagon", "1");
4085   Builder.defineMacro("__hexagon__", "1");
4086 
4087   if(CPU == "hexagonv1") {
4088     Builder.defineMacro("__HEXAGON_V1__");
4089     Builder.defineMacro("__HEXAGON_ARCH__", "1");
4090     if(Opts.HexagonQdsp6Compat) {
4091       Builder.defineMacro("__QDSP6_V1__");
4092       Builder.defineMacro("__QDSP6_ARCH__", "1");
4093     }
4094   }
4095   else if(CPU == "hexagonv2") {
4096     Builder.defineMacro("__HEXAGON_V2__");
4097     Builder.defineMacro("__HEXAGON_ARCH__", "2");
4098     if(Opts.HexagonQdsp6Compat) {
4099       Builder.defineMacro("__QDSP6_V2__");
4100       Builder.defineMacro("__QDSP6_ARCH__", "2");
4101     }
4102   }
4103   else if(CPU == "hexagonv3") {
4104     Builder.defineMacro("__HEXAGON_V3__");
4105     Builder.defineMacro("__HEXAGON_ARCH__", "3");
4106     if(Opts.HexagonQdsp6Compat) {
4107       Builder.defineMacro("__QDSP6_V3__");
4108       Builder.defineMacro("__QDSP6_ARCH__", "3");
4109     }
4110   }
4111   else if(CPU == "hexagonv4") {
4112     Builder.defineMacro("__HEXAGON_V4__");
4113     Builder.defineMacro("__HEXAGON_ARCH__", "4");
4114     if(Opts.HexagonQdsp6Compat) {
4115       Builder.defineMacro("__QDSP6_V4__");
4116       Builder.defineMacro("__QDSP6_ARCH__", "4");
4117     }
4118   }
4119   else if(CPU == "hexagonv5") {
4120     Builder.defineMacro("__HEXAGON_V5__");
4121     Builder.defineMacro("__HEXAGON_ARCH__", "5");
4122     if(Opts.HexagonQdsp6Compat) {
4123       Builder.defineMacro("__QDSP6_V5__");
4124       Builder.defineMacro("__QDSP6_ARCH__", "5");
4125     }
4126   }
4127 }
4128 
4129 const char * const HexagonTargetInfo::GCCRegNames[] = {
4130   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
4131   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
4132   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
4133   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
4134   "p0", "p1", "p2", "p3",
4135   "sa0", "lc0", "sa1", "lc1", "m0", "m1", "usr", "ugp"
4136 };
4137 
4138 void HexagonTargetInfo::getGCCRegNames(const char * const *&Names,
4139                                    unsigned &NumNames) const {
4140   Names = GCCRegNames;
4141   NumNames = llvm::array_lengthof(GCCRegNames);
4142 }
4143 
4144 
4145 const TargetInfo::GCCRegAlias HexagonTargetInfo::GCCRegAliases[] = {
4146   { { "sp" }, "r29" },
4147   { { "fp" }, "r30" },
4148   { { "lr" }, "r31" },
4149  };
4150 
4151 void HexagonTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
4152                                      unsigned &NumAliases) const {
4153   Aliases = GCCRegAliases;
4154   NumAliases = llvm::array_lengthof(GCCRegAliases);
4155 }
4156 
4157 
4158 const Builtin::Info HexagonTargetInfo::BuiltinInfo[] = {
4159 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
4160 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
4161                                               ALL_LANGUAGES },
4162 #include "clang/Basic/BuiltinsHexagon.def"
4163 };
4164 }
4165 
4166 
4167 namespace {
4168 // Shared base class for SPARC v8 (32-bit) and SPARC v9 (64-bit).
4169 class SparcTargetInfo : public TargetInfo {
4170   static const TargetInfo::GCCRegAlias GCCRegAliases[];
4171   static const char * const GCCRegNames[];
4172   bool SoftFloat;
4173 public:
4174   SparcTargetInfo(const std::string &triple) : TargetInfo(triple) {}
4175 
4176   virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
4177                                  StringRef Name,
4178                                  bool Enabled) const {
4179     if (Name == "soft-float")
4180       Features[Name] = Enabled;
4181     else
4182       return false;
4183 
4184     return true;
4185   }
4186   virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
4187     SoftFloat = false;
4188     for (unsigned i = 0, e = Features.size(); i != e; ++i)
4189       if (Features[i] == "+soft-float")
4190         SoftFloat = true;
4191   }
4192   virtual void getTargetDefines(const LangOptions &Opts,
4193                                 MacroBuilder &Builder) const {
4194     DefineStd(Builder, "sparc", Opts);
4195     Builder.defineMacro("__REGISTER_PREFIX__", "");
4196 
4197     if (SoftFloat)
4198       Builder.defineMacro("SOFT_FLOAT", "1");
4199   }
4200 
4201   virtual bool hasFeature(StringRef Feature) const {
4202     return llvm::StringSwitch<bool>(Feature)
4203              .Case("softfloat", SoftFloat)
4204              .Case("sparc", true)
4205              .Default(false);
4206   }
4207 
4208   virtual void getTargetBuiltins(const Builtin::Info *&Records,
4209                                  unsigned &NumRecords) const {
4210     // FIXME: Implement!
4211   }
4212   virtual BuiltinVaListKind getBuiltinVaListKind() const {
4213     return TargetInfo::VoidPtrBuiltinVaList;
4214   }
4215   virtual void getGCCRegNames(const char * const *&Names,
4216                               unsigned &NumNames) const;
4217   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4218                                 unsigned &NumAliases) const;
4219   virtual bool validateAsmConstraint(const char *&Name,
4220                                      TargetInfo::ConstraintInfo &info) const {
4221     // FIXME: Implement!
4222     return false;
4223   }
4224   virtual const char *getClobbers() const {
4225     // FIXME: Implement!
4226     return "";
4227   }
4228 };
4229 
4230 const char * const SparcTargetInfo::GCCRegNames[] = {
4231   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
4232   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
4233   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
4234   "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
4235 };
4236 
4237 void SparcTargetInfo::getGCCRegNames(const char * const *&Names,
4238                                      unsigned &NumNames) const {
4239   Names = GCCRegNames;
4240   NumNames = llvm::array_lengthof(GCCRegNames);
4241 }
4242 
4243 const TargetInfo::GCCRegAlias SparcTargetInfo::GCCRegAliases[] = {
4244   { { "g0" }, "r0" },
4245   { { "g1" }, "r1" },
4246   { { "g2" }, "r2" },
4247   { { "g3" }, "r3" },
4248   { { "g4" }, "r4" },
4249   { { "g5" }, "r5" },
4250   { { "g6" }, "r6" },
4251   { { "g7" }, "r7" },
4252   { { "o0" }, "r8" },
4253   { { "o1" }, "r9" },
4254   { { "o2" }, "r10" },
4255   { { "o3" }, "r11" },
4256   { { "o4" }, "r12" },
4257   { { "o5" }, "r13" },
4258   { { "o6", "sp" }, "r14" },
4259   { { "o7" }, "r15" },
4260   { { "l0" }, "r16" },
4261   { { "l1" }, "r17" },
4262   { { "l2" }, "r18" },
4263   { { "l3" }, "r19" },
4264   { { "l4" }, "r20" },
4265   { { "l5" }, "r21" },
4266   { { "l6" }, "r22" },
4267   { { "l7" }, "r23" },
4268   { { "i0" }, "r24" },
4269   { { "i1" }, "r25" },
4270   { { "i2" }, "r26" },
4271   { { "i3" }, "r27" },
4272   { { "i4" }, "r28" },
4273   { { "i5" }, "r29" },
4274   { { "i6", "fp" }, "r30" },
4275   { { "i7" }, "r31" },
4276 };
4277 
4278 void SparcTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
4279                                        unsigned &NumAliases) const {
4280   Aliases = GCCRegAliases;
4281   NumAliases = llvm::array_lengthof(GCCRegAliases);
4282 }
4283 
4284 // SPARC v8 is the 32-bit mode selected by Triple::sparc.
4285 class SparcV8TargetInfo : public SparcTargetInfo {
4286 public:
4287   SparcV8TargetInfo(const std::string& triple) : SparcTargetInfo(triple) {
4288     // FIXME: Support Sparc quad-precision long double?
4289     DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
4290                         "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32-S64";
4291   }
4292 
4293   virtual void getTargetDefines(const LangOptions &Opts,
4294                                 MacroBuilder &Builder) const {
4295     SparcTargetInfo::getTargetDefines(Opts, Builder);
4296     Builder.defineMacro("__sparcv8");
4297   }
4298 };
4299 
4300 // SPARC v9 is the 64-bit mode selected by Triple::sparcv9.
4301 class SparcV9TargetInfo : public SparcTargetInfo {
4302 public:
4303   SparcV9TargetInfo(const std::string& triple) : SparcTargetInfo(triple) {
4304     // FIXME: Support Sparc quad-precision long double?
4305     DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
4306                         "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32:64-S128";
4307     // This is an LP64 platform.
4308     LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
4309 
4310     // OpenBSD uses long long for int64_t and intmax_t.
4311     if (getTriple().getOS() == llvm::Triple::OpenBSD) {
4312       IntMaxType = SignedLongLong;
4313       UIntMaxType = UnsignedLongLong;
4314     } else {
4315       IntMaxType = SignedLong;
4316       UIntMaxType = UnsignedLong;
4317     }
4318     Int64Type = IntMaxType;
4319   }
4320 
4321   virtual void getTargetDefines(const LangOptions &Opts,
4322                                 MacroBuilder &Builder) const {
4323     SparcTargetInfo::getTargetDefines(Opts, Builder);
4324     Builder.defineMacro("__sparcv9");
4325     Builder.defineMacro("__arch64__");
4326     // Solaris and its derivative AuroraUX don't need these variants, but the
4327     // BSDs do.
4328     if (getTriple().getOS() != llvm::Triple::Solaris &&
4329         getTriple().getOS() != llvm::Triple::AuroraUX) {
4330       Builder.defineMacro("__sparc64__");
4331       Builder.defineMacro("__sparc_v9__");
4332       Builder.defineMacro("__sparcv9__");
4333     }
4334   }
4335 };
4336 
4337 } // end anonymous namespace.
4338 
4339 namespace {
4340 class AuroraUXSparcV8TargetInfo : public AuroraUXTargetInfo<SparcV8TargetInfo> {
4341 public:
4342   AuroraUXSparcV8TargetInfo(const std::string& triple) :
4343       AuroraUXTargetInfo<SparcV8TargetInfo>(triple) {
4344     SizeType = UnsignedInt;
4345     PtrDiffType = SignedInt;
4346   }
4347 };
4348 class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
4349 public:
4350   SolarisSparcV8TargetInfo(const std::string& triple) :
4351       SolarisTargetInfo<SparcV8TargetInfo>(triple) {
4352     SizeType = UnsignedInt;
4353     PtrDiffType = SignedInt;
4354   }
4355 };
4356 } // end anonymous namespace.
4357 
4358 namespace {
4359   class SystemZTargetInfo : public TargetInfo {
4360     static const char *const GCCRegNames[];
4361 
4362   public:
4363     SystemZTargetInfo(const std::string& triple) : TargetInfo(triple) {
4364       TLSSupported = true;
4365       IntWidth = IntAlign = 32;
4366       LongWidth = LongLongWidth = LongAlign = LongLongAlign = 64;
4367       PointerWidth = PointerAlign = 64;
4368       LongDoubleWidth = 128;
4369       LongDoubleAlign = 64;
4370       LongDoubleFormat = &llvm::APFloat::IEEEquad;
4371       MinGlobalAlign = 16;
4372       DescriptionString = "E-p:64:64:64-i1:8:16-i8:8:16-i16:16-i32:32-i64:64"
4373        "-f32:32-f64:64-f128:64-a0:8:16-n32:64";
4374       MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
4375     }
4376     virtual void getTargetDefines(const LangOptions &Opts,
4377                                   MacroBuilder &Builder) const {
4378       Builder.defineMacro("__s390__");
4379       Builder.defineMacro("__s390x__");
4380       Builder.defineMacro("__zarch__");
4381       Builder.defineMacro("__LONG_DOUBLE_128__");
4382     }
4383     virtual void getTargetBuiltins(const Builtin::Info *&Records,
4384                                    unsigned &NumRecords) const {
4385       // FIXME: Implement.
4386       Records = 0;
4387       NumRecords = 0;
4388     }
4389 
4390     virtual void getGCCRegNames(const char *const *&Names,
4391                                 unsigned &NumNames) const;
4392     virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4393                                   unsigned &NumAliases) const {
4394       // No aliases.
4395       Aliases = 0;
4396       NumAliases = 0;
4397     }
4398     virtual bool validateAsmConstraint(const char *&Name,
4399                                        TargetInfo::ConstraintInfo &info) const;
4400     virtual const char *getClobbers() const {
4401       // FIXME: Is this really right?
4402       return "";
4403     }
4404     virtual BuiltinVaListKind getBuiltinVaListKind() const {
4405       return TargetInfo::SystemZBuiltinVaList;
4406     }
4407   };
4408 
4409   const char *const SystemZTargetInfo::GCCRegNames[] = {
4410     "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
4411     "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
4412     "f0",  "f2",  "f4",  "f6",  "f1",  "f3",  "f5",  "f7",
4413     "f8",  "f10", "f12", "f14", "f9",  "f11", "f13", "f15"
4414   };
4415 
4416   void SystemZTargetInfo::getGCCRegNames(const char *const *&Names,
4417                                          unsigned &NumNames) const {
4418     Names = GCCRegNames;
4419     NumNames = llvm::array_lengthof(GCCRegNames);
4420   }
4421 
4422   bool SystemZTargetInfo::
4423   validateAsmConstraint(const char *&Name,
4424                         TargetInfo::ConstraintInfo &Info) const {
4425     switch (*Name) {
4426     default:
4427       return false;
4428 
4429     case 'a': // Address register
4430     case 'd': // Data register (equivalent to 'r')
4431     case 'f': // Floating-point register
4432       Info.setAllowsRegister();
4433       return true;
4434 
4435     case 'I': // Unsigned 8-bit constant
4436     case 'J': // Unsigned 12-bit constant
4437     case 'K': // Signed 16-bit constant
4438     case 'L': // Signed 20-bit displacement (on all targets we support)
4439     case 'M': // 0x7fffffff
4440       return true;
4441 
4442     case 'Q': // Memory with base and unsigned 12-bit displacement
4443     case 'R': // Likewise, plus an index
4444     case 'S': // Memory with base and signed 20-bit displacement
4445     case 'T': // Likewise, plus an index
4446       Info.setAllowsMemory();
4447       return true;
4448     }
4449   }
4450 }
4451 
4452 namespace {
4453   class MSP430TargetInfo : public TargetInfo {
4454     static const char * const GCCRegNames[];
4455   public:
4456     MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
4457       BigEndian = false;
4458       TLSSupported = false;
4459       IntWidth = 16; IntAlign = 16;
4460       LongWidth = 32; LongLongWidth = 64;
4461       LongAlign = LongLongAlign = 16;
4462       PointerWidth = 16; PointerAlign = 16;
4463       SuitableAlign = 16;
4464       SizeType = UnsignedInt;
4465       IntMaxType = SignedLong;
4466       UIntMaxType = UnsignedLong;
4467       IntPtrType = SignedShort;
4468       PtrDiffType = SignedInt;
4469       SigAtomicType = SignedLong;
4470       DescriptionString = "e-p:16:16:16-i8:8:8-i16:16:16-i32:16:32-n8:16";
4471    }
4472     virtual void getTargetDefines(const LangOptions &Opts,
4473                                   MacroBuilder &Builder) const {
4474       Builder.defineMacro("MSP430");
4475       Builder.defineMacro("__MSP430__");
4476       // FIXME: defines for different 'flavours' of MCU
4477     }
4478     virtual void getTargetBuiltins(const Builtin::Info *&Records,
4479                                    unsigned &NumRecords) const {
4480      // FIXME: Implement.
4481       Records = 0;
4482       NumRecords = 0;
4483     }
4484     virtual bool hasFeature(StringRef Feature) const {
4485       return Feature == "msp430";
4486     }
4487     virtual void getGCCRegNames(const char * const *&Names,
4488                                 unsigned &NumNames) const;
4489     virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4490                                   unsigned &NumAliases) const {
4491       // No aliases.
4492       Aliases = 0;
4493       NumAliases = 0;
4494     }
4495     virtual bool validateAsmConstraint(const char *&Name,
4496                                        TargetInfo::ConstraintInfo &info) const {
4497       // No target constraints for now.
4498       return false;
4499     }
4500     virtual const char *getClobbers() const {
4501       // FIXME: Is this really right?
4502       return "";
4503     }
4504     virtual BuiltinVaListKind getBuiltinVaListKind() const {
4505       // FIXME: implement
4506       return TargetInfo::CharPtrBuiltinVaList;
4507    }
4508   };
4509 
4510   const char * const MSP430TargetInfo::GCCRegNames[] = {
4511     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
4512     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4513   };
4514 
4515   void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
4516                                         unsigned &NumNames) const {
4517     Names = GCCRegNames;
4518     NumNames = llvm::array_lengthof(GCCRegNames);
4519   }
4520 }
4521 
4522 namespace {
4523 
4524   // LLVM and Clang cannot be used directly to output native binaries for
4525   // target, but is used to compile C code to llvm bitcode with correct
4526   // type and alignment information.
4527   //
4528   // TCE uses the llvm bitcode as input and uses it for generating customized
4529   // target processor and program binary. TCE co-design environment is
4530   // publicly available in http://tce.cs.tut.fi
4531 
4532   static const unsigned TCEOpenCLAddrSpaceMap[] = {
4533       3, // opencl_global
4534       4, // opencl_local
4535       5, // opencl_constant
4536       0, // cuda_device
4537       0, // cuda_constant
4538       0  // cuda_shared
4539   };
4540 
4541   class TCETargetInfo : public TargetInfo{
4542   public:
4543     TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
4544       TLSSupported = false;
4545       IntWidth = 32;
4546       LongWidth = LongLongWidth = 32;
4547       PointerWidth = 32;
4548       IntAlign = 32;
4549       LongAlign = LongLongAlign = 32;
4550       PointerAlign = 32;
4551       SuitableAlign = 32;
4552       SizeType = UnsignedInt;
4553       IntMaxType = SignedLong;
4554       UIntMaxType = UnsignedLong;
4555       IntPtrType = SignedInt;
4556       PtrDiffType = SignedInt;
4557       FloatWidth = 32;
4558       FloatAlign = 32;
4559       DoubleWidth = 32;
4560       DoubleAlign = 32;
4561       LongDoubleWidth = 32;
4562       LongDoubleAlign = 32;
4563       FloatFormat = &llvm::APFloat::IEEEsingle;
4564       DoubleFormat = &llvm::APFloat::IEEEsingle;
4565       LongDoubleFormat = &llvm::APFloat::IEEEsingle;
4566       DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-"
4567                           "i16:16:32-i32:32:32-i64:32:32-"
4568                           "f32:32:32-f64:32:32-v64:32:32-"
4569                           "v128:32:32-a0:0:32-n32";
4570       AddrSpaceMap = &TCEOpenCLAddrSpaceMap;
4571     }
4572 
4573     virtual void getTargetDefines(const LangOptions &Opts,
4574                                   MacroBuilder &Builder) const {
4575       DefineStd(Builder, "tce", Opts);
4576       Builder.defineMacro("__TCE__");
4577       Builder.defineMacro("__TCE_V1__");
4578     }
4579     virtual bool hasFeature(StringRef Feature) const {
4580       return Feature == "tce";
4581     }
4582 
4583     virtual void getTargetBuiltins(const Builtin::Info *&Records,
4584                                    unsigned &NumRecords) const {}
4585     virtual const char *getClobbers() const {
4586       return "";
4587     }
4588     virtual BuiltinVaListKind getBuiltinVaListKind() const {
4589       return TargetInfo::VoidPtrBuiltinVaList;
4590     }
4591     virtual void getGCCRegNames(const char * const *&Names,
4592                                 unsigned &NumNames) const {}
4593     virtual bool validateAsmConstraint(const char *&Name,
4594                                        TargetInfo::ConstraintInfo &info) const {
4595       return true;
4596     }
4597     virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4598                                   unsigned &NumAliases) const {}
4599   };
4600 }
4601 
4602 namespace {
4603 class MipsTargetInfoBase : public TargetInfo {
4604   static const Builtin::Info BuiltinInfo[];
4605   std::string CPU;
4606   bool IsMips16;
4607   bool IsMicromips;
4608   bool IsSingleFloat;
4609   enum MipsFloatABI {
4610     HardFloat, SoftFloat
4611   } FloatABI;
4612   enum DspRevEnum {
4613     NoDSP, DSP1, DSP2
4614   } DspRev;
4615 
4616 protected:
4617   std::string ABI;
4618 
4619 public:
4620   MipsTargetInfoBase(const std::string& triple,
4621                      const std::string& ABIStr,
4622                      const std::string& CPUStr)
4623     : TargetInfo(triple),
4624       CPU(CPUStr),
4625       IsMips16(false),
4626       IsMicromips(false),
4627       IsSingleFloat(false),
4628       FloatABI(HardFloat),
4629       DspRev(NoDSP),
4630       ABI(ABIStr)
4631   {}
4632 
4633   virtual const char *getABI() const { return ABI.c_str(); }
4634   virtual bool setABI(const std::string &Name) = 0;
4635   virtual bool setCPU(const std::string &Name) {
4636     CPU = Name;
4637     return true;
4638   }
4639   void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
4640     Features[ABI] = true;
4641     Features[CPU] = true;
4642   }
4643 
4644   virtual void getTargetDefines(const LangOptions &Opts,
4645                                 MacroBuilder &Builder) const {
4646     DefineStd(Builder, "mips", Opts);
4647     Builder.defineMacro("_mips");
4648     Builder.defineMacro("__REGISTER_PREFIX__", "");
4649 
4650     switch (FloatABI) {
4651     case HardFloat:
4652       Builder.defineMacro("__mips_hard_float", Twine(1));
4653       break;
4654     case SoftFloat:
4655       Builder.defineMacro("__mips_soft_float", Twine(1));
4656       break;
4657     }
4658 
4659     if (IsSingleFloat)
4660       Builder.defineMacro("__mips_single_float", Twine(1));
4661 
4662     if (IsMips16)
4663       Builder.defineMacro("__mips16", Twine(1));
4664 
4665     if (IsMicromips)
4666       Builder.defineMacro("__mips_micromips", Twine(1));
4667 
4668     switch (DspRev) {
4669     default:
4670       break;
4671     case DSP1:
4672       Builder.defineMacro("__mips_dsp_rev", Twine(1));
4673       Builder.defineMacro("__mips_dsp", Twine(1));
4674       break;
4675     case DSP2:
4676       Builder.defineMacro("__mips_dsp_rev", Twine(2));
4677       Builder.defineMacro("__mips_dspr2", Twine(1));
4678       Builder.defineMacro("__mips_dsp", Twine(1));
4679       break;
4680     }
4681 
4682     Builder.defineMacro("_MIPS_SZPTR", Twine(getPointerWidth(0)));
4683     Builder.defineMacro("_MIPS_SZINT", Twine(getIntWidth()));
4684     Builder.defineMacro("_MIPS_SZLONG", Twine(getLongWidth()));
4685 
4686     Builder.defineMacro("_MIPS_ARCH", "\"" + CPU + "\"");
4687     Builder.defineMacro("_MIPS_ARCH_" + StringRef(CPU).upper());
4688   }
4689 
4690   virtual void getTargetBuiltins(const Builtin::Info *&Records,
4691                                  unsigned &NumRecords) const {
4692     Records = BuiltinInfo;
4693     NumRecords = clang::Mips::LastTSBuiltin - Builtin::FirstTSBuiltin;
4694   }
4695   virtual bool hasFeature(StringRef Feature) const {
4696     return Feature == "mips";
4697   }
4698   virtual BuiltinVaListKind getBuiltinVaListKind() const {
4699     return TargetInfo::VoidPtrBuiltinVaList;
4700   }
4701   virtual void getGCCRegNames(const char * const *&Names,
4702                               unsigned &NumNames) const {
4703     static const char * const GCCRegNames[] = {
4704       // CPU register names
4705       // Must match second column of GCCRegAliases
4706       "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
4707       "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
4708       "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
4709       "$24",  "$25",  "$26",  "$27",  "$28",  "$29",  "$30",  "$31",
4710       // Floating point register names
4711       "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
4712       "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
4713       "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
4714       "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
4715       // Hi/lo and condition register names
4716       "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
4717       "$fcc5","$fcc6","$fcc7"
4718     };
4719     Names = GCCRegNames;
4720     NumNames = llvm::array_lengthof(GCCRegNames);
4721   }
4722   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4723                                 unsigned &NumAliases) const = 0;
4724   virtual bool validateAsmConstraint(const char *&Name,
4725                                      TargetInfo::ConstraintInfo &Info) const {
4726     switch (*Name) {
4727     default:
4728       return false;
4729 
4730     case 'r': // CPU registers.
4731     case 'd': // Equivalent to "r" unless generating MIPS16 code.
4732     case 'y': // Equivalent to "r", backwards compatibility only.
4733     case 'f': // floating-point registers.
4734     case 'c': // $25 for indirect jumps
4735     case 'l': // lo register
4736     case 'x': // hilo register pair
4737       Info.setAllowsRegister();
4738       return true;
4739     case 'R': // An address that can be used in a non-macro load or store
4740       Info.setAllowsMemory();
4741       return true;
4742     }
4743   }
4744 
4745   virtual const char *getClobbers() const {
4746     // FIXME: Implement!
4747     return "";
4748   }
4749 
4750   virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
4751                                  StringRef Name,
4752                                  bool Enabled) const {
4753     if (Name == "soft-float" || Name == "single-float" ||
4754         Name == "o32" || Name == "n32" || Name == "n64" || Name == "eabi" ||
4755         Name == "mips32" || Name == "mips32r2" ||
4756         Name == "mips64" || Name == "mips64r2" ||
4757         Name == "mips16" || Name == "micromips" ||
4758         Name == "dsp" || Name == "dspr2") {
4759       Features[Name] = Enabled;
4760       return true;
4761     } else if (Name == "32") {
4762       Features["o32"] = Enabled;
4763       return true;
4764     } else if (Name == "64") {
4765       Features["n64"] = Enabled;
4766       return true;
4767     }
4768     return false;
4769   }
4770 
4771   virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
4772     IsMips16 = false;
4773     IsMicromips = false;
4774     IsSingleFloat = false;
4775     FloatABI = HardFloat;
4776     DspRev = NoDSP;
4777 
4778     for (std::vector<std::string>::iterator it = Features.begin(),
4779          ie = Features.end(); it != ie; ++it) {
4780       if (*it == "+single-float")
4781         IsSingleFloat = true;
4782       else if (*it == "+soft-float")
4783         FloatABI = SoftFloat;
4784       else if (*it == "+mips16")
4785         IsMips16 = true;
4786       else if (*it == "+micromips")
4787         IsMicromips = true;
4788       else if (*it == "+dsp")
4789         DspRev = std::max(DspRev, DSP1);
4790       else if (*it == "+dspr2")
4791         DspRev = std::max(DspRev, DSP2);
4792     }
4793 
4794     // Remove front-end specific option.
4795     std::vector<std::string>::iterator it =
4796       std::find(Features.begin(), Features.end(), "+soft-float");
4797     if (it != Features.end())
4798       Features.erase(it);
4799   }
4800 
4801   virtual int getEHDataRegisterNumber(unsigned RegNo) const {
4802     if (RegNo == 0) return 4;
4803     if (RegNo == 1) return 5;
4804     return -1;
4805   }
4806 };
4807 
4808 const Builtin::Info MipsTargetInfoBase::BuiltinInfo[] = {
4809 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
4810 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
4811                                               ALL_LANGUAGES },
4812 #include "clang/Basic/BuiltinsMips.def"
4813 };
4814 
4815 class Mips32TargetInfoBase : public MipsTargetInfoBase {
4816 public:
4817   Mips32TargetInfoBase(const std::string& triple) :
4818     MipsTargetInfoBase(triple, "o32", "mips32") {
4819     SizeType = UnsignedInt;
4820     PtrDiffType = SignedInt;
4821     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
4822   }
4823   virtual bool setABI(const std::string &Name) {
4824     if ((Name == "o32") || (Name == "eabi")) {
4825       ABI = Name;
4826       return true;
4827     } else if (Name == "32") {
4828       ABI = "o32";
4829       return true;
4830     } else
4831       return false;
4832   }
4833   virtual void getTargetDefines(const LangOptions &Opts,
4834                                 MacroBuilder &Builder) const {
4835     MipsTargetInfoBase::getTargetDefines(Opts, Builder);
4836 
4837     if (ABI == "o32") {
4838       Builder.defineMacro("__mips_o32");
4839       Builder.defineMacro("_ABIO32", "1");
4840       Builder.defineMacro("_MIPS_SIM", "_ABIO32");
4841     }
4842     else if (ABI == "eabi")
4843       Builder.defineMacro("__mips_eabi");
4844     else
4845       llvm_unreachable("Invalid ABI for Mips32.");
4846   }
4847   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4848                                 unsigned &NumAliases) const {
4849     static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
4850       { { "at" },  "$1" },
4851       { { "v0" },  "$2" },
4852       { { "v1" },  "$3" },
4853       { { "a0" },  "$4" },
4854       { { "a1" },  "$5" },
4855       { { "a2" },  "$6" },
4856       { { "a3" },  "$7" },
4857       { { "t0" },  "$8" },
4858       { { "t1" },  "$9" },
4859       { { "t2" }, "$10" },
4860       { { "t3" }, "$11" },
4861       { { "t4" }, "$12" },
4862       { { "t5" }, "$13" },
4863       { { "t6" }, "$14" },
4864       { { "t7" }, "$15" },
4865       { { "s0" }, "$16" },
4866       { { "s1" }, "$17" },
4867       { { "s2" }, "$18" },
4868       { { "s3" }, "$19" },
4869       { { "s4" }, "$20" },
4870       { { "s5" }, "$21" },
4871       { { "s6" }, "$22" },
4872       { { "s7" }, "$23" },
4873       { { "t8" }, "$24" },
4874       { { "t9" }, "$25" },
4875       { { "k0" }, "$26" },
4876       { { "k1" }, "$27" },
4877       { { "gp" }, "$28" },
4878       { { "sp","$sp" }, "$29" },
4879       { { "fp","$fp" }, "$30" },
4880       { { "ra" }, "$31" }
4881     };
4882     Aliases = GCCRegAliases;
4883     NumAliases = llvm::array_lengthof(GCCRegAliases);
4884   }
4885 };
4886 
4887 class Mips32EBTargetInfo : public Mips32TargetInfoBase {
4888 public:
4889   Mips32EBTargetInfo(const std::string& triple) : Mips32TargetInfoBase(triple) {
4890     DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
4891                         "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32-S64";
4892   }
4893   virtual void getTargetDefines(const LangOptions &Opts,
4894                                 MacroBuilder &Builder) const {
4895     DefineStd(Builder, "MIPSEB", Opts);
4896     Builder.defineMacro("_MIPSEB");
4897     Mips32TargetInfoBase::getTargetDefines(Opts, Builder);
4898   }
4899 };
4900 
4901 class Mips32ELTargetInfo : public Mips32TargetInfoBase {
4902 public:
4903   Mips32ELTargetInfo(const std::string& triple) : Mips32TargetInfoBase(triple) {
4904     BigEndian = false;
4905     DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
4906                         "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32-S64";
4907   }
4908   virtual void getTargetDefines(const LangOptions &Opts,
4909                                 MacroBuilder &Builder) const {
4910     DefineStd(Builder, "MIPSEL", Opts);
4911     Builder.defineMacro("_MIPSEL");
4912     Mips32TargetInfoBase::getTargetDefines(Opts, Builder);
4913   }
4914 };
4915 
4916 class Mips64TargetInfoBase : public MipsTargetInfoBase {
4917   virtual void SetDescriptionString(const std::string &Name) = 0;
4918 public:
4919   Mips64TargetInfoBase(const std::string& triple) :
4920     MipsTargetInfoBase(triple, "n64", "mips64") {
4921     LongWidth = LongAlign = 64;
4922     PointerWidth = PointerAlign = 64;
4923     LongDoubleWidth = LongDoubleAlign = 128;
4924     LongDoubleFormat = &llvm::APFloat::IEEEquad;
4925     if (getTriple().getOS() == llvm::Triple::FreeBSD) {
4926       LongDoubleWidth = LongDoubleAlign = 64;
4927       LongDoubleFormat = &llvm::APFloat::IEEEdouble;
4928     }
4929     SuitableAlign = 128;
4930     MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
4931   }
4932   virtual bool setABI(const std::string &Name) {
4933     SetDescriptionString(Name);
4934     if (Name == "n32") {
4935       LongWidth = LongAlign = 32;
4936       PointerWidth = PointerAlign = 32;
4937       ABI = Name;
4938       return true;
4939     } else if (Name == "n64") {
4940       ABI = Name;
4941       return true;
4942     } else if (Name == "64") {
4943       ABI = "n64";
4944       return true;
4945     } else
4946       return false;
4947   }
4948   virtual void getTargetDefines(const LangOptions &Opts,
4949                                 MacroBuilder &Builder) const {
4950     MipsTargetInfoBase::getTargetDefines(Opts, Builder);
4951 
4952     Builder.defineMacro("__mips64");
4953     Builder.defineMacro("__mips64__");
4954 
4955     if (ABI == "n32") {
4956       Builder.defineMacro("__mips_n32");
4957       Builder.defineMacro("_ABIN32", "2");
4958       Builder.defineMacro("_MIPS_SIM", "_ABIN32");
4959     }
4960     else if (ABI == "n64") {
4961       Builder.defineMacro("__mips_n64");
4962       Builder.defineMacro("_ABI64", "3");
4963       Builder.defineMacro("_MIPS_SIM", "_ABI64");
4964     }
4965     else
4966       llvm_unreachable("Invalid ABI for Mips64.");
4967   }
4968   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
4969                                 unsigned &NumAliases) const {
4970     static const TargetInfo::GCCRegAlias GCCRegAliases[] = {
4971       { { "at" },  "$1" },
4972       { { "v0" },  "$2" },
4973       { { "v1" },  "$3" },
4974       { { "a0" },  "$4" },
4975       { { "a1" },  "$5" },
4976       { { "a2" },  "$6" },
4977       { { "a3" },  "$7" },
4978       { { "a4" },  "$8" },
4979       { { "a5" },  "$9" },
4980       { { "a6" }, "$10" },
4981       { { "a7" }, "$11" },
4982       { { "t0" }, "$12" },
4983       { { "t1" }, "$13" },
4984       { { "t2" }, "$14" },
4985       { { "t3" }, "$15" },
4986       { { "s0" }, "$16" },
4987       { { "s1" }, "$17" },
4988       { { "s2" }, "$18" },
4989       { { "s3" }, "$19" },
4990       { { "s4" }, "$20" },
4991       { { "s5" }, "$21" },
4992       { { "s6" }, "$22" },
4993       { { "s7" }, "$23" },
4994       { { "t8" }, "$24" },
4995       { { "t9" }, "$25" },
4996       { { "k0" }, "$26" },
4997       { { "k1" }, "$27" },
4998       { { "gp" }, "$28" },
4999       { { "sp","$sp" }, "$29" },
5000       { { "fp","$fp" }, "$30" },
5001       { { "ra" }, "$31" }
5002     };
5003     Aliases = GCCRegAliases;
5004     NumAliases = llvm::array_lengthof(GCCRegAliases);
5005   }
5006 };
5007 
5008 class Mips64EBTargetInfo : public Mips64TargetInfoBase {
5009   virtual void SetDescriptionString(const std::string &Name) {
5010     // Change DescriptionString only if ABI is n32.
5011     if (Name == "n32")
5012       DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
5013                           "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
5014                           "v64:64:64-n32:64-S128";
5015   }
5016 public:
5017   Mips64EBTargetInfo(const std::string& triple) : Mips64TargetInfoBase(triple) {
5018     // Default ABI is n64.
5019     DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
5020                         "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
5021                         "v64:64:64-n32:64-S128";
5022   }
5023   virtual void getTargetDefines(const LangOptions &Opts,
5024                                 MacroBuilder &Builder) const {
5025     DefineStd(Builder, "MIPSEB", Opts);
5026     Builder.defineMacro("_MIPSEB");
5027     Mips64TargetInfoBase::getTargetDefines(Opts, Builder);
5028   }
5029 };
5030 
5031 class Mips64ELTargetInfo : public Mips64TargetInfoBase {
5032   virtual void SetDescriptionString(const std::string &Name) {
5033     // Change DescriptionString only if ABI is n32.
5034     if (Name == "n32")
5035       DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
5036                           "i64:64:64-f32:32:32-f64:64:64-f128:128:128"
5037                           "-v64:64:64-n32:64-S128";
5038   }
5039 public:
5040   Mips64ELTargetInfo(const std::string& triple) : Mips64TargetInfoBase(triple) {
5041     // Default ABI is n64.
5042     BigEndian = false;
5043     DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
5044                         "i64:64:64-f32:32:32-f64:64:64-f128:128:128-"
5045                         "v64:64:64-n32:64-S128";
5046   }
5047   virtual void getTargetDefines(const LangOptions &Opts,
5048                                 MacroBuilder &Builder) const {
5049     DefineStd(Builder, "MIPSEL", Opts);
5050     Builder.defineMacro("_MIPSEL");
5051     Mips64TargetInfoBase::getTargetDefines(Opts, Builder);
5052   }
5053 };
5054 } // end anonymous namespace.
5055 
5056 namespace {
5057 class PNaClTargetInfo : public TargetInfo {
5058 public:
5059   PNaClTargetInfo(const std::string& triple) : TargetInfo(triple) {
5060     BigEndian = false;
5061     this->UserLabelPrefix = "";
5062     this->LongAlign = 32;
5063     this->LongWidth = 32;
5064     this->PointerAlign = 32;
5065     this->PointerWidth = 32;
5066     this->IntMaxType = TargetInfo::SignedLongLong;
5067     this->UIntMaxType = TargetInfo::UnsignedLongLong;
5068     this->Int64Type = TargetInfo::SignedLongLong;
5069     this->DoubleAlign = 64;
5070     this->LongDoubleWidth = 64;
5071     this->LongDoubleAlign = 64;
5072     this->SizeType = TargetInfo::UnsignedInt;
5073     this->PtrDiffType = TargetInfo::SignedInt;
5074     this->IntPtrType = TargetInfo::SignedInt;
5075     this->RegParmMax = 0; // Disallow regparm
5076     DescriptionString = "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
5077                         "f32:32:32-f64:64:64-p:32:32:32-v128:32:32";
5078   }
5079 
5080   void getDefaultFeatures(llvm::StringMap<bool> &Features) const {
5081   }
5082   virtual void getArchDefines(const LangOptions &Opts,
5083                               MacroBuilder &Builder) const {
5084     Builder.defineMacro("__le32__");
5085     Builder.defineMacro("__pnacl__");
5086   }
5087   virtual void getTargetDefines(const LangOptions &Opts,
5088                                 MacroBuilder &Builder) const {
5089     Builder.defineMacro("__LITTLE_ENDIAN__");
5090     getArchDefines(Opts, Builder);
5091   }
5092   virtual bool hasFeature(StringRef Feature) const {
5093     return Feature == "pnacl";
5094   }
5095   virtual void getTargetBuiltins(const Builtin::Info *&Records,
5096                                  unsigned &NumRecords) const {
5097   }
5098   virtual BuiltinVaListKind getBuiltinVaListKind() const {
5099     return TargetInfo::PNaClABIBuiltinVaList;
5100   }
5101   virtual void getGCCRegNames(const char * const *&Names,
5102                               unsigned &NumNames) const;
5103   virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
5104                                 unsigned &NumAliases) const;
5105   virtual bool validateAsmConstraint(const char *&Name,
5106                                      TargetInfo::ConstraintInfo &Info) const {
5107     return false;
5108   }
5109 
5110   virtual const char *getClobbers() const {
5111     return "";
5112   }
5113 };
5114 
5115 void PNaClTargetInfo::getGCCRegNames(const char * const *&Names,
5116                                      unsigned &NumNames) const {
5117   Names = NULL;
5118   NumNames = 0;
5119 }
5120 
5121 void PNaClTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
5122                                        unsigned &NumAliases) const {
5123   Aliases = NULL;
5124   NumAliases = 0;
5125 }
5126 } // end anonymous namespace.
5127 
5128 namespace {
5129   static const unsigned SPIRAddrSpaceMap[] = {
5130     1,    // opencl_global
5131     3,    // opencl_local
5132     2,    // opencl_constant
5133     0,    // cuda_device
5134     0,    // cuda_constant
5135     0     // cuda_shared
5136   };
5137   class SPIRTargetInfo : public TargetInfo {
5138     static const char * const GCCRegNames[];
5139     static const Builtin::Info BuiltinInfo[];
5140     std::vector<StringRef> AvailableFeatures;
5141   public:
5142     SPIRTargetInfo(const std::string& triple) : TargetInfo(triple) {
5143       assert(getTriple().getOS() == llvm::Triple::UnknownOS &&
5144         "SPIR target must use unknown OS");
5145       assert(getTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
5146         "SPIR target must use unknown environment type");
5147       BigEndian = false;
5148       TLSSupported = false;
5149       LongWidth = LongAlign = 64;
5150       AddrSpaceMap = &SPIRAddrSpaceMap;
5151       // Define available target features
5152       // These must be defined in sorted order!
5153       NoAsmVariants = true;
5154     }
5155     virtual void getTargetDefines(const LangOptions &Opts,
5156                                   MacroBuilder &Builder) const {
5157       DefineStd(Builder, "SPIR", Opts);
5158     }
5159     virtual bool hasFeature(StringRef Feature) const {
5160       return Feature == "spir";
5161     }
5162 
5163     virtual void getTargetBuiltins(const Builtin::Info *&Records,
5164                                    unsigned &NumRecords) const {}
5165     virtual const char *getClobbers() const {
5166       return "";
5167     }
5168     virtual void getGCCRegNames(const char * const *&Names,
5169                                 unsigned &NumNames) const {}
5170     virtual bool validateAsmConstraint(const char *&Name,
5171                                        TargetInfo::ConstraintInfo &info) const {
5172       return true;
5173     }
5174     virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
5175                                   unsigned &NumAliases) const {}
5176     virtual BuiltinVaListKind getBuiltinVaListKind() const {
5177       return TargetInfo::VoidPtrBuiltinVaList;
5178     }
5179   };
5180 
5181 
5182   class SPIR32TargetInfo : public SPIRTargetInfo {
5183   public:
5184     SPIR32TargetInfo(const std::string& triple) : SPIRTargetInfo(triple) {
5185       PointerWidth = PointerAlign = 32;
5186       SizeType     = TargetInfo::UnsignedInt;
5187       PtrDiffType = IntPtrType = TargetInfo::SignedInt;
5188       DescriptionString
5189         = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
5190           "f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-"
5191           "v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-"
5192           "v512:512:512-v1024:1024:1024";
5193     }
5194     virtual void getTargetDefines(const LangOptions &Opts,
5195                                   MacroBuilder &Builder) const {
5196       DefineStd(Builder, "SPIR32", Opts);
5197     }
5198   };
5199 
5200   class SPIR64TargetInfo : public SPIRTargetInfo {
5201   public:
5202     SPIR64TargetInfo(const std::string& triple) : SPIRTargetInfo(triple) {
5203       PointerWidth = PointerAlign = 64;
5204       SizeType     = TargetInfo::UnsignedLong;
5205       PtrDiffType = IntPtrType = TargetInfo::SignedLong;
5206       DescriptionString
5207         = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
5208           "f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-"
5209           "v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-"
5210           "v512:512:512-v1024:1024:1024";
5211     }
5212     virtual void getTargetDefines(const LangOptions &Opts,
5213                                   MacroBuilder &Builder) const {
5214       DefineStd(Builder, "SPIR64", Opts);
5215     }
5216   };
5217 }
5218 
5219 
5220 //===----------------------------------------------------------------------===//
5221 // Driver code
5222 //===----------------------------------------------------------------------===//
5223 
5224 static TargetInfo *AllocateTarget(const std::string &T) {
5225   llvm::Triple Triple(T);
5226   llvm::Triple::OSType os = Triple.getOS();
5227 
5228   switch (Triple.getArch()) {
5229   default:
5230     return NULL;
5231 
5232   case llvm::Triple::hexagon:
5233     return new HexagonTargetInfo(T);
5234 
5235   case llvm::Triple::aarch64:
5236     switch (os) {
5237     case llvm::Triple::Linux:
5238       return new LinuxTargetInfo<AArch64TargetInfo>(T);
5239     default:
5240       return new AArch64TargetInfo(T);
5241     }
5242 
5243   case llvm::Triple::arm:
5244   case llvm::Triple::thumb:
5245     if (Triple.isOSDarwin())
5246       return new DarwinARMTargetInfo(T);
5247 
5248     switch (os) {
5249     case llvm::Triple::Linux:
5250       return new LinuxTargetInfo<ARMTargetInfo>(T);
5251     case llvm::Triple::FreeBSD:
5252       return new FreeBSDTargetInfo<ARMTargetInfo>(T);
5253     case llvm::Triple::NetBSD:
5254       return new NetBSDTargetInfo<ARMTargetInfo>(T);
5255     case llvm::Triple::OpenBSD:
5256       return new OpenBSDTargetInfo<ARMTargetInfo>(T);
5257     case llvm::Triple::Bitrig:
5258       return new BitrigTargetInfo<ARMTargetInfo>(T);
5259     case llvm::Triple::RTEMS:
5260       return new RTEMSTargetInfo<ARMTargetInfo>(T);
5261     case llvm::Triple::NaCl:
5262       return new NaClTargetInfo<ARMTargetInfo>(T);
5263     default:
5264       return new ARMTargetInfo(T);
5265     }
5266 
5267   case llvm::Triple::msp430:
5268     return new MSP430TargetInfo(T);
5269 
5270   case llvm::Triple::mips:
5271     switch (os) {
5272     case llvm::Triple::Linux:
5273       return new LinuxTargetInfo<Mips32EBTargetInfo>(T);
5274     case llvm::Triple::RTEMS:
5275       return new RTEMSTargetInfo<Mips32EBTargetInfo>(T);
5276     case llvm::Triple::FreeBSD:
5277       return new FreeBSDTargetInfo<Mips32EBTargetInfo>(T);
5278     case llvm::Triple::NetBSD:
5279       return new NetBSDTargetInfo<Mips32EBTargetInfo>(T);
5280     default:
5281       return new Mips32EBTargetInfo(T);
5282     }
5283 
5284   case llvm::Triple::mipsel:
5285     switch (os) {
5286     case llvm::Triple::Linux:
5287       return new LinuxTargetInfo<Mips32ELTargetInfo>(T);
5288     case llvm::Triple::RTEMS:
5289       return new RTEMSTargetInfo<Mips32ELTargetInfo>(T);
5290     case llvm::Triple::FreeBSD:
5291       return new FreeBSDTargetInfo<Mips32ELTargetInfo>(T);
5292     case llvm::Triple::NetBSD:
5293       return new NetBSDTargetInfo<Mips32ELTargetInfo>(T);
5294     default:
5295       return new Mips32ELTargetInfo(T);
5296     }
5297 
5298   case llvm::Triple::mips64:
5299     switch (os) {
5300     case llvm::Triple::Linux:
5301       return new LinuxTargetInfo<Mips64EBTargetInfo>(T);
5302     case llvm::Triple::RTEMS:
5303       return new RTEMSTargetInfo<Mips64EBTargetInfo>(T);
5304     case llvm::Triple::FreeBSD:
5305       return new FreeBSDTargetInfo<Mips64EBTargetInfo>(T);
5306     case llvm::Triple::NetBSD:
5307       return new NetBSDTargetInfo<Mips64EBTargetInfo>(T);
5308     case llvm::Triple::OpenBSD:
5309       return new OpenBSDTargetInfo<Mips64EBTargetInfo>(T);
5310     default:
5311       return new Mips64EBTargetInfo(T);
5312     }
5313 
5314   case llvm::Triple::mips64el:
5315     switch (os) {
5316     case llvm::Triple::Linux:
5317       return new LinuxTargetInfo<Mips64ELTargetInfo>(T);
5318     case llvm::Triple::RTEMS:
5319       return new RTEMSTargetInfo<Mips64ELTargetInfo>(T);
5320     case llvm::Triple::FreeBSD:
5321       return new FreeBSDTargetInfo<Mips64ELTargetInfo>(T);
5322     case llvm::Triple::NetBSD:
5323       return new NetBSDTargetInfo<Mips64ELTargetInfo>(T);
5324     case llvm::Triple::OpenBSD:
5325       return new OpenBSDTargetInfo<Mips64ELTargetInfo>(T);
5326     default:
5327       return new Mips64ELTargetInfo(T);
5328     }
5329 
5330   case llvm::Triple::le32:
5331     switch (os) {
5332       case llvm::Triple::NaCl:
5333         return new NaClTargetInfo<PNaClTargetInfo>(T);
5334       default:
5335         return NULL;
5336     }
5337 
5338   case llvm::Triple::ppc:
5339     if (Triple.isOSDarwin())
5340       return new DarwinPPC32TargetInfo(T);
5341     switch (os) {
5342     case llvm::Triple::Linux:
5343       return new LinuxTargetInfo<PPC32TargetInfo>(T);
5344     case llvm::Triple::FreeBSD:
5345       return new FreeBSDTargetInfo<PPC32TargetInfo>(T);
5346     case llvm::Triple::NetBSD:
5347       return new NetBSDTargetInfo<PPC32TargetInfo>(T);
5348     case llvm::Triple::OpenBSD:
5349       return new OpenBSDTargetInfo<PPC32TargetInfo>(T);
5350     case llvm::Triple::RTEMS:
5351       return new RTEMSTargetInfo<PPC32TargetInfo>(T);
5352     default:
5353       return new PPC32TargetInfo(T);
5354     }
5355 
5356   case llvm::Triple::ppc64:
5357     if (Triple.isOSDarwin())
5358       return new DarwinPPC64TargetInfo(T);
5359     switch (os) {
5360     case llvm::Triple::Linux:
5361       return new LinuxTargetInfo<PPC64TargetInfo>(T);
5362     case llvm::Triple::Lv2:
5363       return new PS3PPUTargetInfo<PPC64TargetInfo>(T);
5364     case llvm::Triple::FreeBSD:
5365       return new FreeBSDTargetInfo<PPC64TargetInfo>(T);
5366     case llvm::Triple::NetBSD:
5367       return new NetBSDTargetInfo<PPC64TargetInfo>(T);
5368     default:
5369       return new PPC64TargetInfo(T);
5370     }
5371 
5372   case llvm::Triple::nvptx:
5373     return new NVPTX32TargetInfo(T);
5374   case llvm::Triple::nvptx64:
5375     return new NVPTX64TargetInfo(T);
5376 
5377   case llvm::Triple::mblaze:
5378     return new MBlazeTargetInfo(T);
5379 
5380   case llvm::Triple::r600:
5381     return new R600TargetInfo(T);
5382 
5383   case llvm::Triple::sparc:
5384     switch (os) {
5385     case llvm::Triple::Linux:
5386       return new LinuxTargetInfo<SparcV8TargetInfo>(T);
5387     case llvm::Triple::AuroraUX:
5388       return new AuroraUXSparcV8TargetInfo(T);
5389     case llvm::Triple::Solaris:
5390       return new SolarisSparcV8TargetInfo(T);
5391     case llvm::Triple::NetBSD:
5392       return new NetBSDTargetInfo<SparcV8TargetInfo>(T);
5393     case llvm::Triple::OpenBSD:
5394       return new OpenBSDTargetInfo<SparcV8TargetInfo>(T);
5395     case llvm::Triple::RTEMS:
5396       return new RTEMSTargetInfo<SparcV8TargetInfo>(T);
5397     default:
5398       return new SparcV8TargetInfo(T);
5399     }
5400 
5401   case llvm::Triple::sparcv9:
5402     switch (os) {
5403     case llvm::Triple::Linux:
5404       return new LinuxTargetInfo<SparcV9TargetInfo>(T);
5405     case llvm::Triple::AuroraUX:
5406       return new AuroraUXTargetInfo<SparcV9TargetInfo>(T);
5407     case llvm::Triple::Solaris:
5408       return new SolarisTargetInfo<SparcV9TargetInfo>(T);
5409     case llvm::Triple::NetBSD:
5410       return new NetBSDTargetInfo<SparcV9TargetInfo>(T);
5411     case llvm::Triple::OpenBSD:
5412       return new OpenBSDTargetInfo<SparcV9TargetInfo>(T);
5413     case llvm::Triple::FreeBSD:
5414       return new FreeBSDTargetInfo<SparcV9TargetInfo>(T);
5415     default:
5416       return new SparcV9TargetInfo(T);
5417     }
5418 
5419   case llvm::Triple::systemz:
5420     switch (os) {
5421     case llvm::Triple::Linux:
5422       return new LinuxTargetInfo<SystemZTargetInfo>(T);
5423     default:
5424       return new SystemZTargetInfo(T);
5425     }
5426 
5427   case llvm::Triple::tce:
5428     return new TCETargetInfo(T);
5429 
5430   case llvm::Triple::x86:
5431     if (Triple.isOSDarwin())
5432       return new DarwinI386TargetInfo(T);
5433 
5434     switch (os) {
5435     case llvm::Triple::AuroraUX:
5436       return new AuroraUXTargetInfo<X86_32TargetInfo>(T);
5437     case llvm::Triple::Linux:
5438       return new LinuxTargetInfo<X86_32TargetInfo>(T);
5439     case llvm::Triple::DragonFly:
5440       return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
5441     case llvm::Triple::NetBSD:
5442       return new NetBSDI386TargetInfo(T);
5443     case llvm::Triple::OpenBSD:
5444       return new OpenBSDI386TargetInfo(T);
5445     case llvm::Triple::Bitrig:
5446       return new BitrigI386TargetInfo(T);
5447     case llvm::Triple::FreeBSD:
5448       return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
5449     case llvm::Triple::Minix:
5450       return new MinixTargetInfo<X86_32TargetInfo>(T);
5451     case llvm::Triple::Solaris:
5452       return new SolarisTargetInfo<X86_32TargetInfo>(T);
5453     case llvm::Triple::Cygwin:
5454       return new CygwinX86_32TargetInfo(T);
5455     case llvm::Triple::MinGW32:
5456       return new MinGWX86_32TargetInfo(T);
5457     case llvm::Triple::Win32:
5458       return new VisualStudioWindowsX86_32TargetInfo(T);
5459     case llvm::Triple::Haiku:
5460       return new HaikuX86_32TargetInfo(T);
5461     case llvm::Triple::RTEMS:
5462       return new RTEMSX86_32TargetInfo(T);
5463     case llvm::Triple::NaCl:
5464       return new NaClTargetInfo<X86_32TargetInfo>(T);
5465     default:
5466       return new X86_32TargetInfo(T);
5467     }
5468 
5469   case llvm::Triple::x86_64:
5470     if (Triple.isOSDarwin() || Triple.getEnvironment() == llvm::Triple::MachO)
5471       return new DarwinX86_64TargetInfo(T);
5472 
5473     switch (os) {
5474     case llvm::Triple::AuroraUX:
5475       return new AuroraUXTargetInfo<X86_64TargetInfo>(T);
5476     case llvm::Triple::Linux:
5477       return new LinuxTargetInfo<X86_64TargetInfo>(T);
5478     case llvm::Triple::DragonFly:
5479       return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(T);
5480     case llvm::Triple::NetBSD:
5481       return new NetBSDTargetInfo<X86_64TargetInfo>(T);
5482     case llvm::Triple::OpenBSD:
5483       return new OpenBSDX86_64TargetInfo(T);
5484     case llvm::Triple::Bitrig:
5485       return new BitrigX86_64TargetInfo(T);
5486     case llvm::Triple::FreeBSD:
5487       return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
5488     case llvm::Triple::Solaris:
5489       return new SolarisTargetInfo<X86_64TargetInfo>(T);
5490     case llvm::Triple::MinGW32:
5491       return new MinGWX86_64TargetInfo(T);
5492     case llvm::Triple::Win32:   // This is what Triple.h supports now.
5493       return new VisualStudioWindowsX86_64TargetInfo(T);
5494     case llvm::Triple::NaCl:
5495       return new NaClTargetInfo<X86_64TargetInfo>(T);
5496     default:
5497       return new X86_64TargetInfo(T);
5498     }
5499 
5500     case llvm::Triple::spir: {
5501       llvm::Triple Triple(T);
5502       if (Triple.getOS() != llvm::Triple::UnknownOS ||
5503         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
5504         return NULL;
5505       return new SPIR32TargetInfo(T);
5506     }
5507     case llvm::Triple::spir64: {
5508       llvm::Triple Triple(T);
5509       if (Triple.getOS() != llvm::Triple::UnknownOS ||
5510         Triple.getEnvironment() != llvm::Triple::UnknownEnvironment)
5511         return NULL;
5512       return new SPIR64TargetInfo(T);
5513     }
5514   }
5515 }
5516 
5517 /// CreateTargetInfo - Return the target info object for the specified target
5518 /// triple.
5519 TargetInfo *TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
5520                                          TargetOptions *Opts) {
5521   llvm::Triple Triple(Opts->Triple);
5522 
5523   // Construct the target
5524   OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
5525   if (!Target) {
5526     Diags.Report(diag::err_target_unknown_triple) << Triple.str();
5527     return 0;
5528   }
5529   Target->setTargetOpts(Opts);
5530 
5531   // Set the target CPU if specified.
5532   if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
5533     Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
5534     return 0;
5535   }
5536 
5537   // Set the target ABI if specified.
5538   if (!Opts->ABI.empty() && !Target->setABI(Opts->ABI)) {
5539     Diags.Report(diag::err_target_unknown_abi) << Opts->ABI;
5540     return 0;
5541   }
5542 
5543   // Set the target C++ ABI.
5544   if (!Opts->CXXABI.empty() && !Target->setCXXABI(Opts->CXXABI)) {
5545     Diags.Report(diag::err_target_unknown_cxxabi) << Opts->CXXABI;
5546     return 0;
5547   }
5548 
5549   // Compute the default target features, we need the target to handle this
5550   // because features may have dependencies on one another.
5551   llvm::StringMap<bool> Features;
5552   Target->getDefaultFeatures(Features);
5553 
5554   // Apply the user specified deltas.
5555   // First the enables.
5556   for (std::vector<std::string>::const_iterator
5557          it = Opts->FeaturesAsWritten.begin(),
5558          ie = Opts->FeaturesAsWritten.end();
5559        it != ie; ++it) {
5560     const char *Name = it->c_str();
5561 
5562     if (Name[0] != '+')
5563       continue;
5564 
5565     // Apply the feature via the target.
5566     if (!Target->setFeatureEnabled(Features, Name + 1, true)) {
5567       Diags.Report(diag::err_target_invalid_feature) << Name;
5568       return 0;
5569     }
5570   }
5571 
5572   // Then the disables.
5573   for (std::vector<std::string>::const_iterator
5574          it = Opts->FeaturesAsWritten.begin(),
5575          ie = Opts->FeaturesAsWritten.end();
5576        it != ie; ++it) {
5577     const char *Name = it->c_str();
5578 
5579     if (Name[0] == '+')
5580       continue;
5581 
5582     // Apply the feature via the target.
5583     if (Name[0] != '-' ||
5584         !Target->setFeatureEnabled(Features, Name + 1, false)) {
5585       Diags.Report(diag::err_target_invalid_feature) << Name;
5586       return 0;
5587     }
5588   }
5589 
5590   // Add the features to the compile options.
5591   //
5592   // FIXME: If we are completely confident that we have the right set, we only
5593   // need to pass the minuses.
5594   Opts->Features.clear();
5595   for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
5596          ie = Features.end(); it != ie; ++it)
5597     Opts->Features.push_back((it->second ? "+" : "-") + it->first().str());
5598   Target->HandleTargetFeatures(Opts->Features);
5599 
5600   return Target.take();
5601 }
5602