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