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