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