Targets.cpp revision ef05abda02375a6565edcc0bdf20bdd41c0a87b3
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/Type.h"
30#include <algorithm>
31using namespace clang;
32
33//===----------------------------------------------------------------------===//
34//  Common code shared among targets.
35//===----------------------------------------------------------------------===//
36
37/// DefineStd - Define a macro name and standard variants.  For example if
38/// MacroName is "unix", then this will define "__unix", "__unix__", and "unix"
39/// when in GNU mode.
40static void DefineStd(MacroBuilder &Builder, StringRef MacroName,
41                      const LangOptions &Opts) {
42  assert(MacroName[0] != '_' && "Identifier should be in the user's namespace");
43
44  // If in GNU mode (e.g. -std=gnu99 but not -std=c99) define the raw identifier
45  // in the user's namespace.
46  if (Opts.GNUMode)
47    Builder.defineMacro(MacroName);
48
49  // Define __unix.
50  Builder.defineMacro("__" + MacroName);
51
52  // Define __unix__.
53  Builder.defineMacro("__" + MacroName + "__");
54}
55
56//===----------------------------------------------------------------------===//
57// Defines specific to certain operating systems.
58//===----------------------------------------------------------------------===//
59
60namespace {
61template<typename TgtInfo>
62class OSTargetInfo : public TgtInfo {
63protected:
64  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
65                            MacroBuilder &Builder) const=0;
66public:
67  OSTargetInfo(const std::string& triple) : TgtInfo(triple) {}
68  virtual void getTargetDefines(const LangOptions &Opts,
69                                MacroBuilder &Builder) const {
70    TgtInfo::getTargetDefines(Opts, Builder);
71    getOSDefines(Opts, TgtInfo::getTriple(), Builder);
72  }
73
74};
75} // end anonymous namespace
76
77
78static void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
79                             const llvm::Triple &Triple,
80                             StringRef &PlatformName,
81                             VersionTuple &PlatformMinVersion) {
82  Builder.defineMacro("__APPLE_CC__", "5621");
83  Builder.defineMacro("__APPLE__");
84  Builder.defineMacro("__MACH__");
85  Builder.defineMacro("OBJC_NEW_PROPERTIES");
86
87  if (!Opts.ObjCAutoRefCount) {
88    // __weak is always defined, for use in blocks and with objc pointers.
89    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
90
91    // Darwin defines __strong even in C mode (just to nothing).
92    if (Opts.getGCMode() != LangOptions::NonGC)
93      Builder.defineMacro("__strong", "__attribute__((objc_gc(strong)))");
94    else
95      Builder.defineMacro("__strong", "");
96
97    // __unsafe_unretained is defined to nothing in non-ARC mode. We even
98    // allow this in C, since one might have block pointers in structs that
99    // are used in pure C code and in Objective-C ARC.
100    Builder.defineMacro("__unsafe_unretained", "");
101
102    // The Objective-C bridged cast keywords are defined to nothing in non-ARC
103    // mode; then they become normal, C-style casts.
104    Builder.defineMacro("__bridge", "");
105    Builder.defineMacro("__bridge_transfer", "");
106    Builder.defineMacro("__bridge_retained", "");
107    Builder.defineMacro("__bridge_retain", "");
108  }
109
110  if (Opts.Static)
111    Builder.defineMacro("__STATIC__");
112  else
113    Builder.defineMacro("__DYNAMIC__");
114
115  if (Opts.POSIXThreads)
116    Builder.defineMacro("_REENTRANT");
117
118  // Get the platform type and version number from the triple.
119  unsigned Maj, Min, Rev;
120
121  // If no version was given, default to to 10.4.0, for simplifying tests.
122  if (Triple.getOSName() == "darwin" || Triple.getOSName() == "osx") {
123    PlatformName = "macosx";
124    Min = Rev = 0;
125    Maj = 8;
126  } else {
127    // Otherwise, honor all three triple forms ("-darwinNNN[-iphoneos]",
128    // "-osxNNN", and "-iosNNN").
129
130    if (Triple.getOS() == llvm::Triple::Darwin) {
131      // For historical reasons that make little sense, the version passed here
132      // is the "darwin" version, which drops the 10 and offsets by 4.
133      Triple.getOSVersion(Maj, Min, Rev);
134
135      if (Triple.getEnvironmentName() == "iphoneos") {
136        PlatformName = "ios";
137      } else {
138        PlatformName = "macosx";
139        Rev = Min;
140        Min = Maj - 4;
141        Maj = 10;
142      }
143    } else {
144      Triple.getOSVersion(Maj, Min, Rev);
145      PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
146    }
147  }
148
149  // If -ccc-host-triple arch-pc-win32-macho option specified, we're
150  // generating code for Win32 ABI. No need to emit
151  // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
152  if (PlatformName == "win32") {
153    PlatformMinVersion = VersionTuple(Maj, Min, Rev);
154    return;
155  }
156
157  // Set the appropriate OS version define.
158  if (PlatformName == "ios") {
159    assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
160    char Str[6];
161    Str[0] = '0' + Maj;
162    Str[1] = '0' + (Min / 10);
163    Str[2] = '0' + (Min % 10);
164    Str[3] = '0' + (Rev / 10);
165    Str[4] = '0' + (Rev % 10);
166    Str[5] = '\0';
167    Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__", Str);
168  } else {
169    // Note that the Driver allows versions which aren't representable in the
170    // define (because we only get a single digit for the minor and micro
171    // revision numbers). So, we limit them to the maximum representable
172    // version.
173    assert(Triple.getEnvironmentName().empty() && "Invalid environment!");
174    assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
175    char Str[5];
176    Str[0] = '0' + (Maj / 10);
177    Str[1] = '0' + (Maj % 10);
178    Str[2] = '0' + std::min(Min, 9U);
179    Str[3] = '0' + std::min(Rev, 9U);
180    Str[4] = '\0';
181    Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
182  }
183
184  PlatformMinVersion = VersionTuple(Maj, Min, Rev);
185}
186
187namespace {
188template<typename Target>
189class DarwinTargetInfo : public OSTargetInfo<Target> {
190protected:
191  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
192                            MacroBuilder &Builder) const {
193    getDarwinDefines(Builder, Opts, Triple, this->PlatformName,
194                     this->PlatformMinVersion);
195  }
196
197public:
198  DarwinTargetInfo(const std::string& triple) :
199    OSTargetInfo<Target>(triple) {
200      llvm::Triple T = llvm::Triple(triple);
201      this->TLSSupported = T.isMacOSX() && !T.isMacOSXVersionLT(10,7);
202      this->MCountName = "\01mcount";
203    }
204
205  virtual std::string isValidSectionSpecifier(StringRef SR) const {
206    // Let MCSectionMachO validate this.
207    StringRef Segment, Section;
208    unsigned TAA, StubSize;
209    bool HasTAA;
210    return llvm::MCSectionMachO::ParseSectionSpecifier(SR, Segment, Section,
211                                                       TAA, HasTAA, StubSize);
212  }
213
214  virtual const char *getStaticInitSectionSpecifier() const {
215    // FIXME: We should return 0 when building kexts.
216    return "__TEXT,__StaticInit,regular,pure_instructions";
217  }
218
219};
220
221
222// DragonFlyBSD Target
223template<typename Target>
224class DragonFlyBSDTargetInfo : public OSTargetInfo<Target> {
225protected:
226  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
227                            MacroBuilder &Builder) const {
228    // DragonFly defines; list based off of gcc output
229    Builder.defineMacro("__DragonFly__");
230    Builder.defineMacro("__DragonFly_cc_version", "100001");
231    Builder.defineMacro("__ELF__");
232    Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
233    Builder.defineMacro("__tune_i386__");
234    DefineStd(Builder, "unix", Opts);
235  }
236public:
237  DragonFlyBSDTargetInfo(const std::string &triple)
238    : OSTargetInfo<Target>(triple) {}
239};
240
241// FreeBSD Target
242template<typename Target>
243class FreeBSDTargetInfo : public OSTargetInfo<Target> {
244protected:
245  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
246                            MacroBuilder &Builder) const {
247    // FreeBSD defines; list based off of gcc output
248
249    // FIXME: Move version number handling to llvm::Triple.
250    StringRef Release = Triple.getOSName().substr(strlen("freebsd"), 1);
251
252    Builder.defineMacro("__FreeBSD__", Release);
253    Builder.defineMacro("__FreeBSD_cc_version", Release + "00001");
254    Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
255    DefineStd(Builder, "unix", Opts);
256    Builder.defineMacro("__ELF__");
257  }
258public:
259  FreeBSDTargetInfo(const std::string &triple)
260    : OSTargetInfo<Target>(triple) {
261      this->UserLabelPrefix = "";
262
263      llvm::Triple Triple(triple);
264      switch (Triple.getArch()) {
265        default:
266        case llvm::Triple::x86:
267        case llvm::Triple::x86_64:
268          this->MCountName = ".mcount";
269          break;
270        case llvm::Triple::mips:
271        case llvm::Triple::mipsel:
272        case llvm::Triple::ppc:
273        case llvm::Triple::ppc64:
274          this->MCountName = "_mcount";
275          break;
276        case llvm::Triple::arm:
277          this->MCountName = "__mcount";
278          break;
279      }
280
281    }
282};
283
284// Minix Target
285template<typename Target>
286class MinixTargetInfo : public OSTargetInfo<Target> {
287protected:
288  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
289                            MacroBuilder &Builder) const {
290    // Minix defines
291
292    Builder.defineMacro("__minix", "3");
293    Builder.defineMacro("_EM_WSIZE", "4");
294    Builder.defineMacro("_EM_PSIZE", "4");
295    Builder.defineMacro("_EM_SSIZE", "2");
296    Builder.defineMacro("_EM_LSIZE", "4");
297    Builder.defineMacro("_EM_FSIZE", "4");
298    Builder.defineMacro("_EM_DSIZE", "8");
299    DefineStd(Builder, "unix", Opts);
300  }
301public:
302  MinixTargetInfo(const std::string &triple)
303    : OSTargetInfo<Target>(triple) {
304      this->UserLabelPrefix = "";
305    }
306};
307
308// Linux target
309template<typename Target>
310class LinuxTargetInfo : public OSTargetInfo<Target> {
311protected:
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 (Opts.POSIXThreads)
320      Builder.defineMacro("_REENTRANT");
321    if (Opts.CPlusPlus)
322      Builder.defineMacro("_GNU_SOURCE");
323  }
324public:
325  LinuxTargetInfo(const std::string& triple)
326    : OSTargetInfo<Target>(triple) {
327    this->UserLabelPrefix = "";
328    this->WIntType = TargetInfo::UnsignedInt;
329  }
330};
331
332// NetBSD Target
333template<typename Target>
334class NetBSDTargetInfo : public OSTargetInfo<Target> {
335protected:
336  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
337                            MacroBuilder &Builder) const {
338    // NetBSD defines; list based off of gcc output
339    Builder.defineMacro("__NetBSD__");
340    Builder.defineMacro("__unix__");
341    Builder.defineMacro("__ELF__");
342    if (Opts.POSIXThreads)
343      Builder.defineMacro("_POSIX_THREADS");
344  }
345public:
346  NetBSDTargetInfo(const std::string &triple)
347    : OSTargetInfo<Target>(triple) {
348      this->UserLabelPrefix = "";
349    }
350};
351
352// OpenBSD Target
353template<typename Target>
354class OpenBSDTargetInfo : public OSTargetInfo<Target> {
355protected:
356  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
357                            MacroBuilder &Builder) const {
358    // OpenBSD defines; list based off of gcc output
359
360    Builder.defineMacro("__OpenBSD__");
361    DefineStd(Builder, "unix", Opts);
362    Builder.defineMacro("__ELF__");
363    if (Opts.POSIXThreads)
364      Builder.defineMacro("_POSIX_THREADS");
365  }
366public:
367  OpenBSDTargetInfo(const std::string &triple)
368    : OSTargetInfo<Target>(triple) {}
369};
370
371// PSP Target
372template<typename Target>
373class PSPTargetInfo : public OSTargetInfo<Target> {
374protected:
375  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
376                            MacroBuilder &Builder) const {
377    // PSP defines; list based on the output of the pspdev gcc toolchain.
378    Builder.defineMacro("PSP");
379    Builder.defineMacro("_PSP");
380    Builder.defineMacro("__psp__");
381    Builder.defineMacro("__ELF__");
382  }
383public:
384  PSPTargetInfo(const std::string& triple)
385    : OSTargetInfo<Target>(triple) {
386    this->UserLabelPrefix = "";
387  }
388};
389
390// PS3 PPU Target
391template<typename Target>
392class PS3PPUTargetInfo : public OSTargetInfo<Target> {
393protected:
394  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
395                            MacroBuilder &Builder) const {
396    // PS3 PPU defines.
397    Builder.defineMacro("__PPC__");
398    Builder.defineMacro("__PPU__");
399    Builder.defineMacro("__CELLOS_LV2__");
400    Builder.defineMacro("__ELF__");
401    Builder.defineMacro("__LP32__");
402    Builder.defineMacro("_ARCH_PPC64");
403    Builder.defineMacro("__powerpc64__");
404  }
405public:
406  PS3PPUTargetInfo(const std::string& triple)
407    : OSTargetInfo<Target>(triple) {
408    this->UserLabelPrefix = "";
409    this->LongWidth = this->LongAlign = this->PointerWidth = this->PointerAlign = 32;
410    this->IntMaxType = TargetInfo::SignedLongLong;
411    this->UIntMaxType = TargetInfo::UnsignedLongLong;
412    this->Int64Type = TargetInfo::SignedLongLong;
413    this->SizeType = TargetInfo::UnsignedInt;
414    this->DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
415                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
416  }
417};
418
419// FIXME: Need a real SPU target.
420// PS3 SPU Target
421template<typename Target>
422class PS3SPUTargetInfo : public OSTargetInfo<Target> {
423protected:
424  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
425                            MacroBuilder &Builder) const {
426    // PS3 PPU defines.
427    Builder.defineMacro("__SPU__");
428    Builder.defineMacro("__ELF__");
429  }
430public:
431  PS3SPUTargetInfo(const std::string& triple)
432    : OSTargetInfo<Target>(triple) {
433    this->UserLabelPrefix = "";
434  }
435};
436
437// AuroraUX target
438template<typename Target>
439class AuroraUXTargetInfo : public OSTargetInfo<Target> {
440protected:
441  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
442                            MacroBuilder &Builder) const {
443    DefineStd(Builder, "sun", Opts);
444    DefineStd(Builder, "unix", Opts);
445    Builder.defineMacro("__ELF__");
446    Builder.defineMacro("__svr4__");
447    Builder.defineMacro("__SVR4");
448  }
449public:
450  AuroraUXTargetInfo(const std::string& triple)
451    : OSTargetInfo<Target>(triple) {
452    this->UserLabelPrefix = "";
453    this->WCharType = this->SignedLong;
454    // FIXME: WIntType should be SignedLong
455  }
456};
457
458// Solaris target
459template<typename Target>
460class SolarisTargetInfo : public OSTargetInfo<Target> {
461protected:
462  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
463                            MacroBuilder &Builder) const {
464    DefineStd(Builder, "sun", Opts);
465    DefineStd(Builder, "unix", Opts);
466    Builder.defineMacro("__ELF__");
467    Builder.defineMacro("__svr4__");
468    Builder.defineMacro("__SVR4");
469  }
470public:
471  SolarisTargetInfo(const std::string& triple)
472    : OSTargetInfo<Target>(triple) {
473    this->UserLabelPrefix = "";
474    this->WCharType = this->SignedLong;
475    // FIXME: WIntType should be SignedLong
476  }
477};
478
479// Windows target
480template<typename Target>
481class WindowsTargetInfo : public OSTargetInfo<Target> {
482protected:
483  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
484                            MacroBuilder &Builder) const {
485    Builder.defineMacro("_WIN32");
486  }
487  void getVisualStudioDefines(const LangOptions &Opts,
488                              MacroBuilder &Builder) const {
489    if (Opts.CPlusPlus) {
490      if (Opts.RTTI)
491        Builder.defineMacro("_CPPRTTI");
492
493      if (Opts.Exceptions)
494        Builder.defineMacro("_CPPUNWIND");
495    }
496
497    if (!Opts.CharIsSigned)
498      Builder.defineMacro("_CHAR_UNSIGNED");
499
500    // FIXME: POSIXThreads isn't exactly the option this should be defined for,
501    //        but it works for now.
502    if (Opts.POSIXThreads)
503      Builder.defineMacro("_MT");
504
505    if (Opts.MSCVersion != 0)
506      Builder.defineMacro("_MSC_VER", Twine(Opts.MSCVersion));
507
508    if (Opts.Microsoft) {
509      Builder.defineMacro("_MSC_EXTENSIONS");
510
511      if (Opts.CPlusPlus0x) {
512        Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
513        Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
514        Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
515      }
516    }
517
518    Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
519  }
520
521public:
522  WindowsTargetInfo(const std::string &triple)
523    : OSTargetInfo<Target>(triple) {}
524};
525
526} // end anonymous namespace.
527
528//===----------------------------------------------------------------------===//
529// Specific target implementations.
530//===----------------------------------------------------------------------===//
531
532namespace {
533// PPC abstract base class
534class PPCTargetInfo : public TargetInfo {
535  static const Builtin::Info BuiltinInfo[];
536  static const char * const GCCRegNames[];
537  static const TargetInfo::GCCRegAlias GCCRegAliases[];
538public:
539  PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {}
540
541  virtual void getTargetBuiltins(const Builtin::Info *&Records,
542                                 unsigned &NumRecords) const {
543    Records = BuiltinInfo;
544    NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
545  }
546
547  virtual void getTargetDefines(const LangOptions &Opts,
548                                MacroBuilder &Builder) const;
549
550  virtual void getGCCRegNames(const char * const *&Names,
551                              unsigned &NumNames) const;
552  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
553                                unsigned &NumAliases) const;
554  virtual bool validateAsmConstraint(const char *&Name,
555                                     TargetInfo::ConstraintInfo &Info) const {
556    switch (*Name) {
557    default: return false;
558    case 'O': // Zero
559      break;
560    case 'b': // Base register
561    case 'f': // Floating point register
562      Info.setAllowsRegister();
563      break;
564    // FIXME: The following are added to allow parsing.
565    // I just took a guess at what the actions should be.
566    // Also, is more specific checking needed?  I.e. specific registers?
567    case 'd': // Floating point register (containing 64-bit value)
568    case 'v': // Altivec vector register
569      Info.setAllowsRegister();
570      break;
571    case 'w':
572      switch (Name[1]) {
573        case 'd':// VSX vector register to hold vector double data
574        case 'f':// VSX vector register to hold vector float data
575        case 's':// VSX vector register to hold scalar float data
576        case 'a':// Any VSX register
577          break;
578        default:
579          return false;
580      }
581      Info.setAllowsRegister();
582      Name++; // Skip over 'w'.
583      break;
584    case 'h': // `MQ', `CTR', or `LINK' register
585    case 'q': // `MQ' register
586    case 'c': // `CTR' register
587    case 'l': // `LINK' register
588    case 'x': // `CR' register (condition register) number 0
589    case 'y': // `CR' register (condition register)
590    case 'z': // `XER[CA]' carry bit (part of the XER register)
591      Info.setAllowsRegister();
592      break;
593    case 'I': // Signed 16-bit constant
594    case 'J': // Unsigned 16-bit constant shifted left 16 bits
595              //  (use `L' instead for SImode constants)
596    case 'K': // Unsigned 16-bit constant
597    case 'L': // Signed 16-bit constant shifted left 16 bits
598    case 'M': // Constant larger than 31
599    case 'N': // Exact power of 2
600    case 'P': // Constant whose negation is a signed 16-bit constant
601    case 'G': // Floating point constant that can be loaded into a
602              // register with one instruction per word
603    case 'H': // Integer/Floating point constant that can be loaded
604              // into a register using three instructions
605      break;
606    case 'm': // Memory operand. Note that on PowerPC targets, m can
607              // include addresses that update the base register. It
608              // is therefore only safe to use `m' in an asm statement
609              // if that asm statement accesses the operand exactly once.
610              // The asm statement must also use `%U<opno>' as a
611              // placeholder for the "update" flag in the corresponding
612              // load or store instruction. For example:
613              // asm ("st%U0 %1,%0" : "=m" (mem) : "r" (val));
614              // is correct but:
615              // asm ("st %1,%0" : "=m" (mem) : "r" (val));
616              // is not. Use es rather than m if you don't want the base
617              // register to be updated.
618    case 'e':
619      if (Name[1] != 's')
620          return false;
621              // es: A "stable" memory operand; that is, one which does not
622              // include any automodification of the base register. Unlike
623              // `m', this constraint can be used in asm statements that
624              // might access the operand several times, or that might not
625              // access it at all.
626      Info.setAllowsMemory();
627      Name++; // Skip over 'e'.
628      break;
629    case 'Q': // Memory operand that is an offset from a register (it is
630              // usually better to use `m' or `es' in asm statements)
631    case 'Z': // Memory operand that is an indexed or indirect from a
632              // register (it is usually better to use `m' or `es' in
633              // asm statements)
634      Info.setAllowsMemory();
635      Info.setAllowsRegister();
636      break;
637    case 'R': // AIX TOC entry
638    case 'a': // Address operand that is an indexed or indirect from a
639              // register (`p' is preferable for asm statements)
640    case 'S': // Constant suitable as a 64-bit mask operand
641    case 'T': // Constant suitable as a 32-bit mask operand
642    case 'U': // System V Release 4 small data area reference
643    case 't': // AND masks that can be performed by two rldic{l, r}
644              // instructions
645    case 'W': // Vector constant that does not require memory
646    case 'j': // Vector constant that is all zeros.
647      break;
648    // End FIXME.
649    }
650    return true;
651  }
652  virtual const char *getClobbers() const {
653    return "";
654  }
655};
656
657const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
658#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
659#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
660                                              ALL_LANGUAGES },
661#include "clang/Basic/BuiltinsPPC.def"
662};
663
664
665/// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
666/// #defines that are not tied to a specific subtarget.
667void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
668                                     MacroBuilder &Builder) const {
669  // Target identification.
670  Builder.defineMacro("__ppc__");
671  Builder.defineMacro("_ARCH_PPC");
672  Builder.defineMacro("__powerpc__");
673  Builder.defineMacro("__POWERPC__");
674  if (PointerWidth == 64) {
675    Builder.defineMacro("_ARCH_PPC64");
676    Builder.defineMacro("_LP64");
677    Builder.defineMacro("__LP64__");
678    Builder.defineMacro("__powerpc64__");
679    Builder.defineMacro("__ppc64__");
680  } else {
681    Builder.defineMacro("__ppc__");
682  }
683
684  // Target properties.
685  if (getTriple().getOS() != llvm::Triple::NetBSD)
686    Builder.defineMacro("_BIG_ENDIAN");
687  Builder.defineMacro("__BIG_ENDIAN__");
688
689  // Subtarget options.
690  Builder.defineMacro("__NATURAL_ALIGNMENT__");
691  Builder.defineMacro("__REGISTER_PREFIX__", "");
692
693  // FIXME: Should be controlled by command line option.
694  Builder.defineMacro("__LONG_DOUBLE_128__");
695
696  if (Opts.AltiVec) {
697    Builder.defineMacro("__VEC__", "10206");
698    Builder.defineMacro("__ALTIVEC__");
699  }
700}
701
702
703const char * const PPCTargetInfo::GCCRegNames[] = {
704  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
705  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
706  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
707  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
708  "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
709  "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
710  "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
711  "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
712  "mq", "lr", "ctr", "ap",
713  "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
714  "xer",
715  "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
716  "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
717  "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
718  "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
719  "vrsave", "vscr",
720  "spe_acc", "spefscr",
721  "sfp"
722};
723
724void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
725                                   unsigned &NumNames) const {
726  Names = GCCRegNames;
727  NumNames = llvm::array_lengthof(GCCRegNames);
728}
729
730const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
731  // While some of these aliases do map to different registers
732  // they still share the same register name.
733  { { "0" }, "r0" },
734  { { "1"}, "r1" },
735  { { "2" }, "r2" },
736  { { "3" }, "r3" },
737  { { "4" }, "r4" },
738  { { "5" }, "r5" },
739  { { "6" }, "r6" },
740  { { "7" }, "r7" },
741  { { "8" }, "r8" },
742  { { "9" }, "r9" },
743  { { "10" }, "r10" },
744  { { "11" }, "r11" },
745  { { "12" }, "r12" },
746  { { "13" }, "r13" },
747  { { "14" }, "r14" },
748  { { "15" }, "r15" },
749  { { "16" }, "r16" },
750  { { "17" }, "r17" },
751  { { "18" }, "r18" },
752  { { "19" }, "r19" },
753  { { "20" }, "r20" },
754  { { "21" }, "r21" },
755  { { "22" }, "r22" },
756  { { "23" }, "r23" },
757  { { "24" }, "r24" },
758  { { "25" }, "r25" },
759  { { "26" }, "r26" },
760  { { "27" }, "r27" },
761  { { "28" }, "r28" },
762  { { "29" }, "r29" },
763  { { "30" }, "r30" },
764  { { "31" }, "r31" },
765  { { "fr0" }, "f0" },
766  { { "fr1" }, "f1" },
767  { { "fr2" }, "f2" },
768  { { "fr3" }, "f3" },
769  { { "fr4" }, "f4" },
770  { { "fr5" }, "f5" },
771  { { "fr6" }, "f6" },
772  { { "fr7" }, "f7" },
773  { { "fr8" }, "f8" },
774  { { "fr9" }, "f9" },
775  { { "fr10" }, "f10" },
776  { { "fr11" }, "f11" },
777  { { "fr12" }, "f12" },
778  { { "fr13" }, "f13" },
779  { { "fr14" }, "f14" },
780  { { "fr15" }, "f15" },
781  { { "fr16" }, "f16" },
782  { { "fr17" }, "f17" },
783  { { "fr18" }, "f18" },
784  { { "fr19" }, "f19" },
785  { { "fr20" }, "f20" },
786  { { "fr21" }, "f21" },
787  { { "fr22" }, "f22" },
788  { { "fr23" }, "f23" },
789  { { "fr24" }, "f24" },
790  { { "fr25" }, "f25" },
791  { { "fr26" }, "f26" },
792  { { "fr27" }, "f27" },
793  { { "fr28" }, "f28" },
794  { { "fr29" }, "f29" },
795  { { "fr30" }, "f30" },
796  { { "fr31" }, "f31" },
797  { { "cc" }, "cr0" },
798};
799
800void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
801                                     unsigned &NumAliases) const {
802  Aliases = GCCRegAliases;
803  NumAliases = llvm::array_lengthof(GCCRegAliases);
804}
805} // end anonymous namespace.
806
807namespace {
808class PPC32TargetInfo : public PPCTargetInfo {
809public:
810  PPC32TargetInfo(const std::string &triple) : PPCTargetInfo(triple) {
811    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
812                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
813
814    switch (getTriple().getOS()) {
815    case llvm::Triple::FreeBSD:
816    case llvm::Triple::NetBSD:
817      SizeType = UnsignedInt;
818      break;
819    default:
820      break;
821    }
822  }
823
824  virtual const char *getVAListDeclaration() const {
825    // This is the ELF definition, and is overridden by the Darwin sub-target
826    return "typedef struct __va_list_tag {"
827           "  unsigned char gpr;"
828           "  unsigned char fpr;"
829           "  unsigned short reserved;"
830           "  void* overflow_arg_area;"
831           "  void* reg_save_area;"
832           "} __builtin_va_list[1];";
833  }
834};
835} // end anonymous namespace.
836
837namespace {
838class PPC64TargetInfo : public PPCTargetInfo {
839public:
840  PPC64TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
841    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
842    IntMaxType = SignedLong;
843    UIntMaxType = UnsignedLong;
844    Int64Type = SignedLong;
845    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
846                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32:64";
847  }
848  virtual const char *getVAListDeclaration() const {
849    return "typedef char* __builtin_va_list;";
850  }
851};
852} // end anonymous namespace.
853
854
855namespace {
856class DarwinPPC32TargetInfo :
857  public DarwinTargetInfo<PPC32TargetInfo> {
858public:
859  DarwinPPC32TargetInfo(const std::string& triple)
860    : DarwinTargetInfo<PPC32TargetInfo>(triple) {
861    HasAlignMac68kSupport = true;
862    BoolWidth = BoolAlign = 32; //XXX support -mone-byte-bool?
863  }
864  virtual const char *getVAListDeclaration() const {
865    return "typedef char* __builtin_va_list;";
866  }
867};
868
869class DarwinPPC64TargetInfo :
870  public DarwinTargetInfo<PPC64TargetInfo> {
871public:
872  DarwinPPC64TargetInfo(const std::string& triple)
873    : DarwinTargetInfo<PPC64TargetInfo>(triple) {
874    HasAlignMac68kSupport = true;
875  }
876};
877} // end anonymous namespace.
878
879namespace {
880  class PTXTargetInfo : public TargetInfo {
881    static const char * const GCCRegNames[];
882    static const Builtin::Info BuiltinInfo[];
883  public:
884    PTXTargetInfo(const std::string& triple) : TargetInfo(triple) {
885      TLSSupported = false;
886      LongWidth = LongAlign = 64;
887    }
888    virtual void getTargetDefines(const LangOptions &Opts,
889                                  MacroBuilder &Builder) const {
890      Builder.defineMacro("__PTX__");
891    }
892    virtual void getTargetBuiltins(const Builtin::Info *&Records,
893                                   unsigned &NumRecords) const {
894      Records = BuiltinInfo;
895      NumRecords = clang::PTX::LastTSBuiltin-Builtin::FirstTSBuiltin;
896    }
897
898    virtual void getGCCRegNames(const char * const *&Names,
899                                unsigned &NumNames) const;
900    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
901                                  unsigned &NumAliases) const {
902      // No aliases.
903      Aliases = 0;
904      NumAliases = 0;
905    }
906    virtual bool validateAsmConstraint(const char *&Name,
907                                       TargetInfo::ConstraintInfo &info) const {
908      // FIXME: implement
909      return true;
910    }
911    virtual const char *getClobbers() const {
912      // FIXME: Is this really right?
913      return "";
914    }
915    virtual const char *getVAListDeclaration() const {
916      // FIXME: implement
917      return "typedef char* __builtin_va_list;";
918    }
919  };
920
921  const Builtin::Info PTXTargetInfo::BuiltinInfo[] = {
922#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
923#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
924                                              ALL_LANGUAGES },
925#include "clang/Basic/BuiltinsPTX.def"
926  };
927
928  const char * const PTXTargetInfo::GCCRegNames[] = {
929    "r0"
930  };
931
932  void PTXTargetInfo::getGCCRegNames(const char * const *&Names,
933                                     unsigned &NumNames) const {
934    Names = GCCRegNames;
935    NumNames = llvm::array_lengthof(GCCRegNames);
936  }
937
938
939  class PTX32TargetInfo : public PTXTargetInfo {
940  public:
941  PTX32TargetInfo(const std::string& triple) : PTXTargetInfo(triple) {
942      PointerWidth = PointerAlign = 32;
943      SizeType = PtrDiffType = IntPtrType = TargetInfo::UnsignedInt;
944      DescriptionString
945        = "e-p:32:32-i64:64:64-f64:64:64-n1:8:16:32:64";
946    }
947  };
948
949  class PTX64TargetInfo : public PTXTargetInfo {
950  public:
951  PTX64TargetInfo(const std::string& triple) : PTXTargetInfo(triple) {
952      PointerWidth = PointerAlign = 64;
953      SizeType = PtrDiffType = IntPtrType = TargetInfo::UnsignedLongLong;
954      DescriptionString
955        = "e-p:64:64-i64:64:64-f64:64:64-n1:8:16:32:64";
956    }
957  };
958}
959
960namespace {
961// MBlaze abstract base class
962class MBlazeTargetInfo : public TargetInfo {
963  static const char * const GCCRegNames[];
964  static const TargetInfo::GCCRegAlias GCCRegAliases[];
965
966public:
967  MBlazeTargetInfo(const std::string& triple) : TargetInfo(triple) {
968    DescriptionString = "E-p:32:32:32-i8:8:8-i16:16:16";
969  }
970
971  virtual void getTargetBuiltins(const Builtin::Info *&Records,
972                                 unsigned &NumRecords) const {
973    // FIXME: Implement.
974    Records = 0;
975    NumRecords = 0;
976  }
977
978  virtual void getTargetDefines(const LangOptions &Opts,
979                                MacroBuilder &Builder) const;
980
981  virtual const char *getVAListDeclaration() const {
982    return "typedef char* __builtin_va_list;";
983  }
984  virtual const char *getTargetPrefix() const {
985    return "mblaze";
986  }
987  virtual void getGCCRegNames(const char * const *&Names,
988                              unsigned &NumNames) const;
989  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
990                                unsigned &NumAliases) const;
991  virtual bool validateAsmConstraint(const char *&Name,
992                                     TargetInfo::ConstraintInfo &Info) const {
993    switch (*Name) {
994    default: return false;
995    case 'O': // Zero
996      return true;
997    case 'b': // Base register
998    case 'f': // Floating point register
999      Info.setAllowsRegister();
1000      return true;
1001    }
1002  }
1003  virtual const char *getClobbers() const {
1004    return "";
1005  }
1006};
1007
1008/// MBlazeTargetInfo::getTargetDefines - Return a set of the MBlaze-specific
1009/// #defines that are not tied to a specific subtarget.
1010void MBlazeTargetInfo::getTargetDefines(const LangOptions &Opts,
1011                                     MacroBuilder &Builder) const {
1012  // Target identification.
1013  Builder.defineMacro("__microblaze__");
1014  Builder.defineMacro("_ARCH_MICROBLAZE");
1015  Builder.defineMacro("__MICROBLAZE__");
1016
1017  // Target properties.
1018  Builder.defineMacro("_BIG_ENDIAN");
1019  Builder.defineMacro("__BIG_ENDIAN__");
1020
1021  // Subtarget options.
1022  Builder.defineMacro("__REGISTER_PREFIX__", "");
1023}
1024
1025
1026const char * const MBlazeTargetInfo::GCCRegNames[] = {
1027  "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
1028  "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
1029  "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
1030  "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
1031  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
1032  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
1033  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
1034  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
1035  "hi",   "lo",   "accum","rmsr", "$fcc1","$fcc2","$fcc3","$fcc4",
1036  "$fcc5","$fcc6","$fcc7","$ap",  "$rap", "$frp"
1037};
1038
1039void MBlazeTargetInfo::getGCCRegNames(const char * const *&Names,
1040                                   unsigned &NumNames) const {
1041  Names = GCCRegNames;
1042  NumNames = llvm::array_lengthof(GCCRegNames);
1043}
1044
1045const TargetInfo::GCCRegAlias MBlazeTargetInfo::GCCRegAliases[] = {
1046  { {"f0"},  "r0" },
1047  { {"f1"},  "r1" },
1048  { {"f2"},  "r2" },
1049  { {"f3"},  "r3" },
1050  { {"f4"},  "r4" },
1051  { {"f5"},  "r5" },
1052  { {"f6"},  "r6" },
1053  { {"f7"},  "r7" },
1054  { {"f8"},  "r8" },
1055  { {"f9"},  "r9" },
1056  { {"f10"}, "r10" },
1057  { {"f11"}, "r11" },
1058  { {"f12"}, "r12" },
1059  { {"f13"}, "r13" },
1060  { {"f14"}, "r14" },
1061  { {"f15"}, "r15" },
1062  { {"f16"}, "r16" },
1063  { {"f17"}, "r17" },
1064  { {"f18"}, "r18" },
1065  { {"f19"}, "r19" },
1066  { {"f20"}, "r20" },
1067  { {"f21"}, "r21" },
1068  { {"f22"}, "r22" },
1069  { {"f23"}, "r23" },
1070  { {"f24"}, "r24" },
1071  { {"f25"}, "r25" },
1072  { {"f26"}, "r26" },
1073  { {"f27"}, "r27" },
1074  { {"f28"}, "r28" },
1075  { {"f29"}, "r29" },
1076  { {"f30"}, "r30" },
1077  { {"f31"}, "r31" },
1078};
1079
1080void MBlazeTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1081                                     unsigned &NumAliases) const {
1082  Aliases = GCCRegAliases;
1083  NumAliases = llvm::array_lengthof(GCCRegAliases);
1084}
1085} // end anonymous namespace.
1086
1087namespace {
1088// Namespace for x86 abstract base class
1089const Builtin::Info BuiltinInfo[] = {
1090#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
1091#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
1092                                              ALL_LANGUAGES },
1093#include "clang/Basic/BuiltinsX86.def"
1094};
1095
1096static const char* const GCCRegNames[] = {
1097  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
1098  "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
1099  "argp", "flags", "fpcr", "fpsr", "dirflag", "frame",
1100  "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
1101  "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
1102  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1103  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
1104};
1105
1106const TargetInfo::AddlRegName AddlRegNames[] = {
1107  { { "al", "ah", "eax", "rax" }, 0 },
1108  { { "bl", "bh", "ebx", "rbx" }, 3 },
1109  { { "cl", "ch", "ecx", "rcx" }, 2 },
1110  { { "dl", "dh", "edx", "rdx" }, 1 },
1111  { { "esi", "rsi" }, 4 },
1112  { { "edi", "rdi" }, 5 },
1113  { { "esp", "rsp" }, 7 },
1114  { { "ebp", "rbp" }, 6 },
1115};
1116
1117// X86 target abstract base class; x86-32 and x86-64 are very close, so
1118// most of the implementation can be shared.
1119class X86TargetInfo : public TargetInfo {
1120  enum X86SSEEnum {
1121    NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42
1122  } SSELevel;
1123  enum MMX3DNowEnum {
1124    NoMMX3DNow, MMX, AMD3DNow, AMD3DNowAthlon
1125  } MMX3DNowLevel;
1126
1127  bool HasAES;
1128  bool HasAVX;
1129
1130public:
1131  X86TargetInfo(const std::string& triple)
1132    : TargetInfo(triple), SSELevel(NoSSE), MMX3DNowLevel(NoMMX3DNow),
1133      HasAES(false), HasAVX(false) {
1134    LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
1135  }
1136  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1137                                 unsigned &NumRecords) const {
1138    Records = BuiltinInfo;
1139    NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
1140  }
1141  virtual void getGCCRegNames(const char * const *&Names,
1142                              unsigned &NumNames) const {
1143    Names = GCCRegNames;
1144    NumNames = llvm::array_lengthof(GCCRegNames);
1145  }
1146  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1147                                unsigned &NumAliases) const {
1148    Aliases = 0;
1149    NumAliases = 0;
1150  }
1151  virtual void getGCCAddlRegNames(const AddlRegName *&Names,
1152				  unsigned &NumNames) const {
1153    Names = AddlRegNames;
1154    NumNames = llvm::array_lengthof(AddlRegNames);
1155  }
1156  virtual bool validateAsmConstraint(const char *&Name,
1157                                     TargetInfo::ConstraintInfo &info) const;
1158  virtual std::string convertConstraint(const char *&Constraint) const;
1159  virtual const char *getClobbers() const {
1160    return "~{dirflag},~{fpsr},~{flags}";
1161  }
1162  virtual void getTargetDefines(const LangOptions &Opts,
1163                                MacroBuilder &Builder) const;
1164  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1165                                 const std::string &Name,
1166                                 bool Enabled) const;
1167  virtual void getDefaultFeatures(const std::string &CPU,
1168                                  llvm::StringMap<bool> &Features) const;
1169  virtual void HandleTargetFeatures(std::vector<std::string> &Features);
1170  virtual const char* getABI() const {
1171    return MMX3DNowLevel == NoMMX3DNow ? "no-mmx" : "";
1172  }
1173};
1174
1175void X86TargetInfo::getDefaultFeatures(const std::string &CPU,
1176                                       llvm::StringMap<bool> &Features) const {
1177  // FIXME: This should not be here.
1178  Features["3dnow"] = false;
1179  Features["3dnowa"] = false;
1180  Features["mmx"] = false;
1181  Features["sse"] = false;
1182  Features["sse2"] = false;
1183  Features["sse3"] = false;
1184  Features["ssse3"] = false;
1185  Features["sse41"] = false;
1186  Features["sse42"] = false;
1187  Features["aes"] = false;
1188  Features["avx"] = false;
1189
1190  // LLVM does not currently recognize this.
1191  // Features["sse4a"] = false;
1192
1193  // FIXME: This *really* should not be here.
1194
1195  // X86_64 always has SSE2.
1196  if (PointerWidth == 64)
1197    Features["sse2"] = Features["sse"] = Features["mmx"] = true;
1198
1199  if (CPU == "generic" || CPU == "i386" || CPU == "i486" || CPU == "i586" ||
1200      CPU == "pentium" || CPU == "i686" || CPU == "pentiumpro")
1201    ;
1202  else if (CPU == "pentium-mmx" || CPU == "pentium2")
1203    setFeatureEnabled(Features, "mmx", true);
1204  else if (CPU == "pentium3") {
1205    setFeatureEnabled(Features, "mmx", true);
1206    setFeatureEnabled(Features, "sse", true);
1207  } else if (CPU == "pentium-m" || CPU == "pentium4" || CPU == "x86-64") {
1208    setFeatureEnabled(Features, "mmx", true);
1209    setFeatureEnabled(Features, "sse2", true);
1210  } else if (CPU == "yonah" || CPU == "prescott" || CPU == "nocona") {
1211    setFeatureEnabled(Features, "mmx", true);
1212    setFeatureEnabled(Features, "sse3", true);
1213  } else if (CPU == "core2") {
1214    setFeatureEnabled(Features, "mmx", true);
1215    setFeatureEnabled(Features, "ssse3", true);
1216  } else if (CPU == "penryn") {
1217    setFeatureEnabled(Features, "mmx", true);
1218    setFeatureEnabled(Features, "sse4", true);
1219    Features["sse42"] = false;
1220  } else if (CPU == "atom") {
1221    setFeatureEnabled(Features, "mmx", true);
1222    setFeatureEnabled(Features, "sse3", true);
1223  } else if (CPU == "corei7") {
1224    setFeatureEnabled(Features, "mmx", true);
1225    setFeatureEnabled(Features, "sse4", true);
1226    setFeatureEnabled(Features, "aes", true);
1227  } else if (CPU == "corei7-avx") {
1228    setFeatureEnabled(Features, "mmx", true);
1229    setFeatureEnabled(Features, "sse4", true);
1230    setFeatureEnabled(Features, "aes", true);
1231    //setFeatureEnabled(Features, "avx", true);
1232  } else if (CPU == "k6" || CPU == "winchip-c6")
1233    setFeatureEnabled(Features, "mmx", true);
1234  else if (CPU == "k6-2" || CPU == "k6-3" || CPU == "athlon" ||
1235           CPU == "athlon-tbird" || CPU == "winchip2" || CPU == "c3") {
1236    setFeatureEnabled(Features, "3dnow", true);
1237  } else if (CPU == "athlon-4" || CPU == "athlon-xp" || CPU == "athlon-mp") {
1238    setFeatureEnabled(Features, "sse", true);
1239    setFeatureEnabled(Features, "3dnowa", true);
1240  } else if (CPU == "k8" || CPU == "opteron" || CPU == "athlon64" ||
1241           CPU == "athlon-fx") {
1242    setFeatureEnabled(Features, "sse2", true);
1243    setFeatureEnabled(Features, "3dnowa", true);
1244  } else if (CPU == "k8-sse3") {
1245    setFeatureEnabled(Features, "sse3", true);
1246    setFeatureEnabled(Features, "3dnowa", true);
1247  } else if (CPU == "c3-2") {
1248    setFeatureEnabled(Features, "mmx", true);
1249    setFeatureEnabled(Features, "sse", true);
1250  }
1251}
1252
1253bool X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1254                                      const std::string &Name,
1255                                      bool Enabled) const {
1256  // FIXME: This *really* should not be here.  We need some way of translating
1257  // options into llvm subtarget features.
1258  if (!Features.count(Name) &&
1259      (Name != "sse4" && Name != "sse4.2" && Name != "sse4.1"))
1260    return false;
1261
1262  if (Enabled) {
1263    if (Name == "mmx")
1264      Features["mmx"] = true;
1265    else if (Name == "sse")
1266      Features["sse"] = true;
1267    else if (Name == "sse2")
1268      Features["sse"] = Features["sse2"] = true;
1269    else if (Name == "sse3")
1270      Features["sse"] = Features["sse2"] = Features["sse3"] = true;
1271    else if (Name == "ssse3")
1272      Features["sse"] = Features["sse2"] = Features["sse3"] =
1273        Features["ssse3"] = true;
1274    else if (Name == "sse4" || Name == "sse4.2")
1275      Features["sse"] = Features["sse2"] = Features["sse3"] =
1276        Features["ssse3"] = Features["sse41"] = Features["sse42"] = true;
1277    else if (Name == "sse4.1")
1278      Features["sse"] = Features["sse2"] = Features["sse3"] =
1279        Features["ssse3"] = Features["sse41"] = true;
1280    else if (Name == "3dnow")
1281      Features["mmx"] = Features["3dnow"] = true;
1282    else if (Name == "3dnowa")
1283      Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = true;
1284    else if (Name == "aes")
1285      Features["aes"] = true;
1286    else if (Name == "avx")
1287      Features["avx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
1288        Features["ssse3"] = Features["sse41"] = Features["sse42"] = true;
1289  } else {
1290    if (Name == "mmx")
1291      Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = false;
1292    else if (Name == "sse")
1293      Features["sse"] = Features["sse2"] = Features["sse3"] =
1294        Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
1295    else if (Name == "sse2")
1296      Features["sse2"] = Features["sse3"] = Features["ssse3"] =
1297        Features["sse41"] = Features["sse42"] = false;
1298    else if (Name == "sse3")
1299      Features["sse3"] = Features["ssse3"] = Features["sse41"] =
1300        Features["sse42"] = false;
1301    else if (Name == "ssse3")
1302      Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
1303    else if (Name == "sse4" || Name == "sse4.1")
1304      Features["sse41"] = Features["sse42"] = false;
1305    else if (Name == "sse4.2")
1306      Features["sse42"] = false;
1307    else if (Name == "3dnow")
1308      Features["3dnow"] = Features["3dnowa"] = false;
1309    else if (Name == "3dnowa")
1310      Features["3dnowa"] = false;
1311    else if (Name == "aes")
1312      Features["aes"] = false;
1313    else if (Name == "avx")
1314      Features["avx"] = false;
1315  }
1316
1317  return true;
1318}
1319
1320/// HandleTargetOptions - Perform initialization based on the user
1321/// configured set of features.
1322void X86TargetInfo::HandleTargetFeatures(std::vector<std::string> &Features) {
1323  // Remember the maximum enabled sselevel.
1324  for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
1325    // Ignore disabled features.
1326    if (Features[i][0] == '-')
1327      continue;
1328
1329    if (Features[i].substr(1) == "aes") {
1330      HasAES = true;
1331      continue;
1332    }
1333
1334    // FIXME: Not sure yet how to treat AVX in regard to SSE levels.
1335    // For now let it be enabled together with other SSE levels.
1336    if (Features[i].substr(1) == "avx") {
1337      HasAVX = true;
1338      continue;
1339    }
1340
1341    assert(Features[i][0] == '+' && "Invalid target feature!");
1342    X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Features[i].substr(1))
1343      .Case("sse42", SSE42)
1344      .Case("sse41", SSE41)
1345      .Case("ssse3", SSSE3)
1346      .Case("sse3", SSE3)
1347      .Case("sse2", SSE2)
1348      .Case("sse", SSE1)
1349      .Default(NoSSE);
1350    SSELevel = std::max(SSELevel, Level);
1351
1352    MMX3DNowEnum ThreeDNowLevel =
1353      llvm::StringSwitch<MMX3DNowEnum>(Features[i].substr(1))
1354        .Case("3dnowa", AMD3DNowAthlon)
1355        .Case("3dnow", AMD3DNow)
1356        .Case("mmx", MMX)
1357        .Default(NoMMX3DNow);
1358
1359    MMX3DNowLevel = std::max(MMX3DNowLevel, ThreeDNowLevel);
1360  }
1361
1362  // Don't tell the backend if we're turning off mmx; it will end up disabling
1363  // SSE, which we don't want.
1364  std::vector<std::string>::iterator it;
1365  it = std::find(Features.begin(), Features.end(), "-mmx");
1366  if (it != Features.end())
1367    Features.erase(it);
1368}
1369
1370/// X86TargetInfo::getTargetDefines - Return a set of the X86-specific #defines
1371/// that are not tied to a specific subtarget.
1372void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
1373                                     MacroBuilder &Builder) const {
1374  // Target identification.
1375  if (PointerWidth == 64) {
1376    Builder.defineMacro("_LP64");
1377    Builder.defineMacro("__LP64__");
1378    Builder.defineMacro("__amd64__");
1379    Builder.defineMacro("__amd64");
1380    Builder.defineMacro("__x86_64");
1381    Builder.defineMacro("__x86_64__");
1382  } else {
1383    DefineStd(Builder, "i386", Opts);
1384  }
1385
1386  if (HasAES)
1387    Builder.defineMacro("__AES__");
1388
1389  if (HasAVX)
1390    Builder.defineMacro("__AVX__");
1391
1392  // Target properties.
1393  Builder.defineMacro("__LITTLE_ENDIAN__");
1394
1395  // Subtarget options.
1396  Builder.defineMacro("__nocona");
1397  Builder.defineMacro("__nocona__");
1398  Builder.defineMacro("__tune_nocona__");
1399  Builder.defineMacro("__REGISTER_PREFIX__", "");
1400
1401  // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
1402  // functions in glibc header files that use FP Stack inline asm which the
1403  // backend can't deal with (PR879).
1404  Builder.defineMacro("__NO_MATH_INLINES");
1405
1406  // Each case falls through to the previous one here.
1407  switch (SSELevel) {
1408  case SSE42:
1409    Builder.defineMacro("__SSE4_2__");
1410  case SSE41:
1411    Builder.defineMacro("__SSE4_1__");
1412  case SSSE3:
1413    Builder.defineMacro("__SSSE3__");
1414  case SSE3:
1415    Builder.defineMacro("__SSE3__");
1416  case SSE2:
1417    Builder.defineMacro("__SSE2__");
1418    Builder.defineMacro("__SSE2_MATH__");  // -mfp-math=sse always implied.
1419  case SSE1:
1420    Builder.defineMacro("__SSE__");
1421    Builder.defineMacro("__SSE_MATH__");   // -mfp-math=sse always implied.
1422  case NoSSE:
1423    break;
1424  }
1425
1426  if (Opts.Microsoft && PointerWidth == 32) {
1427    switch (SSELevel) {
1428    case SSE42:
1429    case SSE41:
1430    case SSSE3:
1431    case SSE3:
1432    case SSE2:
1433      Builder.defineMacro("_M_IX86_FP", Twine(2));
1434      break;
1435    case SSE1:
1436      Builder.defineMacro("_M_IX86_FP", Twine(1));
1437      break;
1438    default:
1439      Builder.defineMacro("_M_IX86_FP", Twine(0));
1440    }
1441  }
1442
1443  // Each case falls through to the previous one here.
1444  switch (MMX3DNowLevel) {
1445  case AMD3DNowAthlon:
1446    Builder.defineMacro("__3dNOW_A__");
1447  case AMD3DNow:
1448    Builder.defineMacro("__3dNOW__");
1449  case MMX:
1450    Builder.defineMacro("__MMX__");
1451  case NoMMX3DNow:
1452    break;
1453  }
1454}
1455
1456
1457bool
1458X86TargetInfo::validateAsmConstraint(const char *&Name,
1459                                     TargetInfo::ConstraintInfo &Info) const {
1460  switch (*Name) {
1461  default: return false;
1462  case 'Y': // first letter of a pair:
1463    switch (*(Name+1)) {
1464    default: return false;
1465    case '0':  // First SSE register.
1466    case 't':  // Any SSE register, when SSE2 is enabled.
1467    case 'i':  // Any SSE register, when SSE2 and inter-unit moves enabled.
1468    case 'm':  // any MMX register, when inter-unit moves enabled.
1469      break;   // falls through to setAllowsRegister.
1470  }
1471  case 'a': // eax.
1472  case 'b': // ebx.
1473  case 'c': // ecx.
1474  case 'd': // edx.
1475  case 'S': // esi.
1476  case 'D': // edi.
1477  case 'A': // edx:eax.
1478  case 'f': // any x87 floating point stack register.
1479  case 't': // top of floating point stack.
1480  case 'u': // second from top of floating point stack.
1481  case 'q': // Any register accessible as [r]l: a, b, c, and d.
1482  case 'y': // Any MMX register.
1483  case 'x': // Any SSE register.
1484  case 'Q': // Any register accessible as [r]h: a, b, c, and d.
1485  case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
1486  case 'l': // "Index" registers: any general register that can be used as an
1487            // index in a base+index memory access.
1488    Info.setAllowsRegister();
1489    return true;
1490  case 'C': // SSE floating point constant.
1491  case 'G': // x87 floating point constant.
1492  case 'e': // 32-bit signed integer constant for use with zero-extending
1493            // x86_64 instructions.
1494  case 'Z': // 32-bit unsigned integer constant for use with zero-extending
1495            // x86_64 instructions.
1496    return true;
1497  }
1498  return false;
1499}
1500
1501
1502std::string
1503X86TargetInfo::convertConstraint(const char *&Constraint) const {
1504  switch (*Constraint) {
1505  case 'a': return std::string("{ax}");
1506  case 'b': return std::string("{bx}");
1507  case 'c': return std::string("{cx}");
1508  case 'd': return std::string("{dx}");
1509  case 'S': return std::string("{si}");
1510  case 'D': return std::string("{di}");
1511  case 'p': // address
1512    return std::string("im");
1513  case 't': // top of floating point stack.
1514    return std::string("{st}");
1515  case 'u': // second from top of floating point stack.
1516    return std::string("{st(1)}"); // second from top of floating point stack.
1517  default:
1518    return std::string(1, *Constraint);
1519  }
1520}
1521} // end anonymous namespace
1522
1523namespace {
1524// X86-32 generic target
1525class X86_32TargetInfo : public X86TargetInfo {
1526public:
1527  X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
1528    DoubleAlign = LongLongAlign = 32;
1529    LongDoubleWidth = 96;
1530    LongDoubleAlign = 32;
1531    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1532                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
1533                        "a0:0:64-f80:32:32-n8:16:32";
1534    SizeType = UnsignedInt;
1535    PtrDiffType = SignedInt;
1536    IntPtrType = SignedInt;
1537    RegParmMax = 3;
1538
1539    // Use fpret for all types.
1540    RealTypeUsesObjCFPRet = ((1 << TargetInfo::Float) |
1541                             (1 << TargetInfo::Double) |
1542                             (1 << TargetInfo::LongDouble));
1543  }
1544  virtual const char *getVAListDeclaration() const {
1545    return "typedef char* __builtin_va_list;";
1546  }
1547
1548  int getEHDataRegisterNumber(unsigned RegNo) const {
1549    if (RegNo == 0) return 0;
1550    if (RegNo == 1) return 2;
1551    return -1;
1552  }
1553};
1554} // end anonymous namespace
1555
1556namespace {
1557class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
1558public:
1559  OpenBSDI386TargetInfo(const std::string& triple) :
1560    OpenBSDTargetInfo<X86_32TargetInfo>(triple) {
1561    SizeType = UnsignedLong;
1562    IntPtrType = SignedLong;
1563    PtrDiffType = SignedLong;
1564  }
1565};
1566} // end anonymous namespace
1567
1568namespace {
1569class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
1570public:
1571  DarwinI386TargetInfo(const std::string& triple) :
1572    DarwinTargetInfo<X86_32TargetInfo>(triple) {
1573    LongDoubleWidth = 128;
1574    LongDoubleAlign = 128;
1575    SizeType = UnsignedLong;
1576    IntPtrType = SignedLong;
1577    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1578                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
1579                        "a0:0:64-f80:128:128-n8:16:32";
1580    HasAlignMac68kSupport = true;
1581  }
1582
1583};
1584} // end anonymous namespace
1585
1586namespace {
1587// x86-32 Windows target
1588class WindowsX86_32TargetInfo : public WindowsTargetInfo<X86_32TargetInfo> {
1589public:
1590  WindowsX86_32TargetInfo(const std::string& triple)
1591    : WindowsTargetInfo<X86_32TargetInfo>(triple) {
1592    TLSSupported = false;
1593    WCharType = UnsignedShort;
1594    DoubleAlign = LongLongAlign = 64;
1595    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1596                        "i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-"
1597                        "v128:128:128-a0:0:64-f80:32:32-n8:16:32";
1598  }
1599  virtual void getTargetDefines(const LangOptions &Opts,
1600                                MacroBuilder &Builder) const {
1601    WindowsTargetInfo<X86_32TargetInfo>::getTargetDefines(Opts, Builder);
1602  }
1603};
1604} // end anonymous namespace
1605
1606namespace {
1607
1608// x86-32 Windows Visual Studio target
1609class VisualStudioWindowsX86_32TargetInfo : public WindowsX86_32TargetInfo {
1610public:
1611  VisualStudioWindowsX86_32TargetInfo(const std::string& triple)
1612    : WindowsX86_32TargetInfo(triple) {
1613    LongDoubleWidth = LongDoubleAlign = 64;
1614    LongDoubleFormat = &llvm::APFloat::IEEEdouble;
1615  }
1616  virtual void getTargetDefines(const LangOptions &Opts,
1617                                MacroBuilder &Builder) const {
1618    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
1619    WindowsX86_32TargetInfo::getVisualStudioDefines(Opts, Builder);
1620    // The value of the following reflects processor type.
1621    // 300=386, 400=486, 500=Pentium, 600=Blend (default)
1622    // We lost the original triple, so we use the default.
1623    Builder.defineMacro("_M_IX86", "600");
1624  }
1625};
1626} // end anonymous namespace
1627
1628namespace {
1629// x86-32 MinGW target
1630class MinGWX86_32TargetInfo : public WindowsX86_32TargetInfo {
1631public:
1632  MinGWX86_32TargetInfo(const std::string& triple)
1633    : WindowsX86_32TargetInfo(triple) {
1634  }
1635  virtual void getTargetDefines(const LangOptions &Opts,
1636                                MacroBuilder &Builder) const {
1637    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
1638    DefineStd(Builder, "WIN32", Opts);
1639    DefineStd(Builder, "WINNT", Opts);
1640    Builder.defineMacro("_X86_");
1641    Builder.defineMacro("__MSVCRT__");
1642    Builder.defineMacro("__MINGW32__");
1643
1644    // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
1645    // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
1646    if (Opts.Microsoft)
1647      // Provide "as-is" __declspec.
1648      Builder.defineMacro("__declspec", "__declspec");
1649    else
1650      // Provide alias of __attribute__ like mingw32-gcc.
1651      Builder.defineMacro("__declspec(a)", "__attribute__((a))");
1652  }
1653};
1654} // end anonymous namespace
1655
1656namespace {
1657// x86-32 Cygwin target
1658class CygwinX86_32TargetInfo : public X86_32TargetInfo {
1659public:
1660  CygwinX86_32TargetInfo(const std::string& triple)
1661    : X86_32TargetInfo(triple) {
1662    TLSSupported = false;
1663    WCharType = UnsignedShort;
1664    DoubleAlign = LongLongAlign = 64;
1665    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1666                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
1667                        "a0:0:64-f80:32:32-n8:16:32";
1668  }
1669  virtual void getTargetDefines(const LangOptions &Opts,
1670                                MacroBuilder &Builder) const {
1671    X86_32TargetInfo::getTargetDefines(Opts, Builder);
1672    Builder.defineMacro("__CYGWIN__");
1673    Builder.defineMacro("__CYGWIN32__");
1674    DefineStd(Builder, "unix", Opts);
1675    if (Opts.CPlusPlus)
1676      Builder.defineMacro("_GNU_SOURCE");
1677  }
1678};
1679} // end anonymous namespace
1680
1681namespace {
1682// x86-32 Haiku target
1683class HaikuX86_32TargetInfo : public X86_32TargetInfo {
1684public:
1685  HaikuX86_32TargetInfo(const std::string& triple)
1686    : X86_32TargetInfo(triple) {
1687    SizeType = UnsignedLong;
1688    IntPtrType = SignedLong;
1689    PtrDiffType = SignedLong;
1690    this->UserLabelPrefix = "";
1691  }
1692  virtual void getTargetDefines(const LangOptions &Opts,
1693                                MacroBuilder &Builder) const {
1694    X86_32TargetInfo::getTargetDefines(Opts, Builder);
1695    Builder.defineMacro("__INTEL__");
1696    Builder.defineMacro("__HAIKU__");
1697  }
1698};
1699} // end anonymous namespace
1700
1701// RTEMS Target
1702template<typename Target>
1703class RTEMSTargetInfo : public OSTargetInfo<Target> {
1704protected:
1705  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
1706                            MacroBuilder &Builder) const {
1707    // RTEMS defines; list based off of gcc output
1708
1709    Builder.defineMacro("__rtems__");
1710    Builder.defineMacro("__ELF__");
1711  }
1712public:
1713  RTEMSTargetInfo(const std::string &triple)
1714    : OSTargetInfo<Target>(triple) {
1715      this->UserLabelPrefix = "";
1716
1717      llvm::Triple Triple(triple);
1718      switch (Triple.getArch()) {
1719        default:
1720        case llvm::Triple::x86:
1721          // this->MCountName = ".mcount";
1722          break;
1723        case llvm::Triple::mips:
1724        case llvm::Triple::mipsel:
1725        case llvm::Triple::ppc:
1726        case llvm::Triple::ppc64:
1727          // this->MCountName = "_mcount";
1728          break;
1729        case llvm::Triple::arm:
1730          // this->MCountName = "__mcount";
1731          break;
1732      }
1733
1734    }
1735};
1736
1737namespace {
1738// x86-32 RTEMS target
1739class RTEMSX86_32TargetInfo : public X86_32TargetInfo {
1740public:
1741  RTEMSX86_32TargetInfo(const std::string& triple)
1742    : X86_32TargetInfo(triple) {
1743    SizeType = UnsignedLong;
1744    IntPtrType = SignedLong;
1745    PtrDiffType = SignedLong;
1746    this->UserLabelPrefix = "";
1747  }
1748  virtual void getTargetDefines(const LangOptions &Opts,
1749                                MacroBuilder &Builder) const {
1750    X86_32TargetInfo::getTargetDefines(Opts, Builder);
1751    Builder.defineMacro("__INTEL__");
1752    Builder.defineMacro("__rtems__");
1753  }
1754};
1755} // end anonymous namespace
1756
1757namespace {
1758// x86-64 generic target
1759class X86_64TargetInfo : public X86TargetInfo {
1760public:
1761  X86_64TargetInfo(const std::string &triple) : X86TargetInfo(triple) {
1762    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
1763    LongDoubleWidth = 128;
1764    LongDoubleAlign = 128;
1765    LargeArrayMinWidth = 128;
1766    LargeArrayAlign = 128;
1767    IntMaxType = SignedLong;
1768    UIntMaxType = UnsignedLong;
1769    Int64Type = SignedLong;
1770    RegParmMax = 6;
1771
1772    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1773                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
1774                        "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64";
1775
1776    // Use fpret only for long double.
1777    RealTypeUsesObjCFPRet = (1 << TargetInfo::LongDouble);
1778  }
1779  virtual const char *getVAListDeclaration() const {
1780    return "typedef struct __va_list_tag {"
1781           "  unsigned gp_offset;"
1782           "  unsigned fp_offset;"
1783           "  void* overflow_arg_area;"
1784           "  void* reg_save_area;"
1785           "} __va_list_tag;"
1786           "typedef __va_list_tag __builtin_va_list[1];";
1787  }
1788
1789  int getEHDataRegisterNumber(unsigned RegNo) const {
1790    if (RegNo == 0) return 0;
1791    if (RegNo == 1) return 1;
1792    return -1;
1793  }
1794};
1795} // end anonymous namespace
1796
1797namespace {
1798// x86-64 Windows target
1799class WindowsX86_64TargetInfo : public WindowsTargetInfo<X86_64TargetInfo> {
1800public:
1801  WindowsX86_64TargetInfo(const std::string& triple)
1802    : WindowsTargetInfo<X86_64TargetInfo>(triple) {
1803    TLSSupported = false;
1804    WCharType = UnsignedShort;
1805    LongWidth = LongAlign = 32;
1806    DoubleAlign = LongLongAlign = 64;
1807    IntMaxType = SignedLongLong;
1808    UIntMaxType = UnsignedLongLong;
1809    Int64Type = SignedLongLong;
1810    SizeType = UnsignedLongLong;
1811    PtrDiffType = SignedLongLong;
1812    IntPtrType = SignedLongLong;
1813    this->UserLabelPrefix = "";
1814  }
1815  virtual void getTargetDefines(const LangOptions &Opts,
1816                                MacroBuilder &Builder) const {
1817    WindowsTargetInfo<X86_64TargetInfo>::getTargetDefines(Opts, Builder);
1818    Builder.defineMacro("_WIN64");
1819  }
1820  virtual const char *getVAListDeclaration() const {
1821    return "typedef char* __builtin_va_list;";
1822  }
1823};
1824} // end anonymous namespace
1825
1826namespace {
1827// x86-64 Windows Visual Studio target
1828class VisualStudioWindowsX86_64TargetInfo : public WindowsX86_64TargetInfo {
1829public:
1830  VisualStudioWindowsX86_64TargetInfo(const std::string& triple)
1831    : WindowsX86_64TargetInfo(triple) {
1832    LongDoubleWidth = LongDoubleAlign = 64;
1833    LongDoubleFormat = &llvm::APFloat::IEEEdouble;
1834  }
1835  virtual void getTargetDefines(const LangOptions &Opts,
1836                                MacroBuilder &Builder) const {
1837    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
1838    WindowsX86_64TargetInfo::getVisualStudioDefines(Opts, Builder);
1839    Builder.defineMacro("_M_X64");
1840    Builder.defineMacro("_M_AMD64");
1841  }
1842};
1843} // end anonymous namespace
1844
1845namespace {
1846// x86-64 MinGW target
1847class MinGWX86_64TargetInfo : public WindowsX86_64TargetInfo {
1848public:
1849  MinGWX86_64TargetInfo(const std::string& triple)
1850    : WindowsX86_64TargetInfo(triple) {
1851  }
1852  virtual void getTargetDefines(const LangOptions &Opts,
1853                                MacroBuilder &Builder) const {
1854    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
1855    DefineStd(Builder, "WIN64", Opts);
1856    Builder.defineMacro("__MSVCRT__");
1857    Builder.defineMacro("__MINGW32__");
1858    Builder.defineMacro("__MINGW64__");
1859
1860    // mingw32-gcc provides __declspec(a) as alias of __attribute__((a)).
1861    // In contrast, clang-cc1 provides __declspec(a) with -fms-extensions.
1862    if (Opts.Microsoft)
1863      // Provide "as-is" __declspec.
1864      Builder.defineMacro("__declspec", "__declspec");
1865    else
1866      // Provide alias of __attribute__ like mingw32-gcc.
1867      Builder.defineMacro("__declspec(a)", "__attribute__((a))");
1868  }
1869};
1870} // end anonymous namespace
1871
1872namespace {
1873class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
1874public:
1875  DarwinX86_64TargetInfo(const std::string& triple)
1876      : DarwinTargetInfo<X86_64TargetInfo>(triple) {
1877    Int64Type = SignedLongLong;
1878  }
1879};
1880} // end anonymous namespace
1881
1882namespace {
1883class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
1884public:
1885  OpenBSDX86_64TargetInfo(const std::string& triple)
1886      : OpenBSDTargetInfo<X86_64TargetInfo>(triple) {
1887    IntMaxType = SignedLongLong;
1888    UIntMaxType = UnsignedLongLong;
1889    Int64Type = SignedLongLong;
1890  }
1891};
1892} // end anonymous namespace
1893
1894namespace {
1895class ARMTargetInfo : public TargetInfo {
1896  // Possible FPU choices.
1897  enum FPUMode {
1898    NoFPU,
1899    VFP2FPU,
1900    VFP3FPU,
1901    NeonFPU
1902  };
1903
1904  static bool FPUModeIsVFP(FPUMode Mode) {
1905    return Mode >= VFP2FPU && Mode <= NeonFPU;
1906  }
1907
1908  static const TargetInfo::GCCRegAlias GCCRegAliases[];
1909  static const char * const GCCRegNames[];
1910
1911  std::string ABI, CPU;
1912
1913  unsigned FPU : 3;
1914
1915  unsigned IsThumb : 1;
1916
1917  // Initialized via features.
1918  unsigned SoftFloat : 1;
1919  unsigned SoftFloatABI : 1;
1920
1921  static const Builtin::Info BuiltinInfo[];
1922
1923public:
1924  ARMTargetInfo(const std::string &TripleStr)
1925    : TargetInfo(TripleStr), ABI("aapcs-linux"), CPU("arm1136j-s")
1926  {
1927    SizeType = UnsignedInt;
1928    PtrDiffType = SignedInt;
1929
1930    // {} in inline assembly are neon specifiers, not assembly variant
1931    // specifiers.
1932    NoAsmVariants = true;
1933
1934    // FIXME: Should we just treat this as a feature?
1935    IsThumb = getTriple().getArchName().startswith("thumb");
1936    if (IsThumb) {
1937      // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
1938      // so set preferred for small types to 32.
1939      DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
1940                           "i64:64:64-f32:32:32-f64:64:64-"
1941                           "v64:64:64-v128:64:128-a0:0:32-n32");
1942    } else {
1943      DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1944                           "i64:64:64-f32:32:32-f64:64:64-"
1945                           "v64:64:64-v128:64:128-a0:0:64-n32");
1946    }
1947
1948    // ARM targets default to using the ARM C++ ABI.
1949    CXXABI = CXXABI_ARM;
1950  }
1951  virtual const char *getABI() const { return ABI.c_str(); }
1952  virtual bool setABI(const std::string &Name) {
1953    ABI = Name;
1954
1955    // The defaults (above) are for AAPCS, check if we need to change them.
1956    //
1957    // FIXME: We need support for -meabi... we could just mangle it into the
1958    // name.
1959    if (Name == "apcs-gnu") {
1960      DoubleAlign = LongLongAlign = LongDoubleAlign = 32;
1961      SizeType = UnsignedLong;
1962
1963      // Do not respect the alignment of bit-field types when laying out
1964      // structures. This corresponds to PCC_BITFIELD_TYPE_MATTERS in gcc.
1965      UseBitFieldTypeAlignment = false;
1966
1967      /// Do force alignment of members that follow zero length bitfields.  If
1968      /// the alignment of the zero-length bitfield is greater than the member
1969      /// that follows it, `bar', `bar' will be aligned as the  type of the
1970      /// zero length bitfield.
1971      UseZeroLengthBitfieldAlignment = true;
1972
1973      /// gcc forces the alignment to 4 bytes, regardless of the type of the
1974      /// zero length bitfield.  This corresponds to EMPTY_FIELD_BOUNDARY in
1975      /// gcc.
1976      ZeroLengthBitfieldBoundary = 32;
1977
1978      if (IsThumb) {
1979        // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
1980        // so set preferred for small types to 32.
1981        DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
1982                             "i64:32:64-f32:32:32-f64:32:64-"
1983                             "v64:32:64-v128:32:128-a0:0:32-n32");
1984      } else {
1985        DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1986                             "i64:32:64-f32:32:32-f64:32:64-"
1987                             "v64:32:64-v128:32:128-a0:0:32-n32");
1988      }
1989
1990      // FIXME: Override "preferred align" for double and long long.
1991    } else if (Name == "aapcs") {
1992      // FIXME: Enumerated types are variable width in straight AAPCS.
1993    } else if (Name == "aapcs-linux") {
1994      ;
1995    } else
1996      return false;
1997
1998    return true;
1999  }
2000
2001  void getDefaultFeatures(const std::string &CPU,
2002                          llvm::StringMap<bool> &Features) const {
2003    if (CPU == "arm1136jf-s" || CPU == "arm1176jzf-s" || CPU == "mpcore")
2004      Features["vfp2"] = true;
2005    else if (CPU == "cortex-a8" || CPU == "cortex-a9")
2006      Features["neon"] = true;
2007  }
2008
2009  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
2010                                 const std::string &Name,
2011                                 bool Enabled) const {
2012    if (Name == "soft-float" || Name == "soft-float-abi" ||
2013        Name == "vfp2" || Name == "vfp3" || Name == "neon") {
2014      Features[Name] = Enabled;
2015    } else
2016      return false;
2017
2018    return true;
2019  }
2020
2021  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
2022    FPU = NoFPU;
2023    SoftFloat = SoftFloatABI = false;
2024    for (unsigned i = 0, e = Features.size(); i != e; ++i) {
2025      if (Features[i] == "+soft-float")
2026        SoftFloat = true;
2027      else if (Features[i] == "+soft-float-abi")
2028        SoftFloatABI = true;
2029      else if (Features[i] == "+vfp2")
2030        FPU = VFP2FPU;
2031      else if (Features[i] == "+vfp3")
2032        FPU = VFP3FPU;
2033      else if (Features[i] == "+neon")
2034        FPU = NeonFPU;
2035    }
2036
2037    // Remove front-end specific options which the backend handles differently.
2038    std::vector<std::string>::iterator it;
2039    it = std::find(Features.begin(), Features.end(), "+soft-float");
2040    if (it != Features.end())
2041      Features.erase(it);
2042    it = std::find(Features.begin(), Features.end(), "+soft-float-abi");
2043    if (it != Features.end())
2044      Features.erase(it);
2045  }
2046
2047  static const char *getCPUDefineSuffix(StringRef Name) {
2048    return llvm::StringSwitch<const char*>(Name)
2049      .Cases("arm8", "arm810", "4")
2050      .Cases("strongarm", "strongarm110", "strongarm1100", "strongarm1110", "4")
2051      .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "arm720t", "arm9", "4T")
2052      .Cases("arm9tdmi", "arm920", "arm920t", "arm922t", "arm940t", "4T")
2053      .Case("ep9312", "4T")
2054      .Cases("arm10tdmi", "arm1020t", "5T")
2055      .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "5TE")
2056      .Case("arm926ej-s", "5TEJ")
2057      .Cases("arm10e", "arm1020e", "arm1022e", "5TE")
2058      .Cases("xscale", "iwmmxt", "5TE")
2059      .Case("arm1136j-s", "6J")
2060      .Cases("arm1176jz-s", "arm1176jzf-s", "6ZK")
2061      .Cases("arm1136jf-s", "mpcorenovfp", "mpcore", "6K")
2062      .Cases("arm1156t2-s", "arm1156t2f-s", "6T2")
2063      .Cases("cortex-a8", "cortex-a9", "7A")
2064      .Case("cortex-m3", "7M")
2065      .Case("cortex-m0", "6M")
2066      .Default(0);
2067  }
2068  virtual bool setCPU(const std::string &Name) {
2069    if (!getCPUDefineSuffix(Name))
2070      return false;
2071
2072    CPU = Name;
2073    return true;
2074  }
2075  virtual void getTargetDefines(const LangOptions &Opts,
2076                                MacroBuilder &Builder) const {
2077    // Target identification.
2078    Builder.defineMacro("__arm");
2079    Builder.defineMacro("__arm__");
2080
2081    // Target properties.
2082    Builder.defineMacro("__ARMEL__");
2083    Builder.defineMacro("__LITTLE_ENDIAN__");
2084    Builder.defineMacro("__REGISTER_PREFIX__", "");
2085
2086    StringRef CPUArch = getCPUDefineSuffix(CPU);
2087    Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
2088
2089    // Subtarget options.
2090
2091    // FIXME: It's more complicated than this and we don't really support
2092    // interworking.
2093    if ('5' <= CPUArch[0] && CPUArch[0] <= '7')
2094      Builder.defineMacro("__THUMB_INTERWORK__");
2095
2096    if (ABI == "aapcs" || ABI == "aapcs-linux")
2097      Builder.defineMacro("__ARM_EABI__");
2098
2099    if (SoftFloat)
2100      Builder.defineMacro("__SOFTFP__");
2101
2102    if (CPU == "xscale")
2103      Builder.defineMacro("__XSCALE__");
2104
2105    bool IsARMv7 = CPUArch.startswith("7");
2106    if (IsThumb) {
2107      Builder.defineMacro("__THUMBEL__");
2108      Builder.defineMacro("__thumb__");
2109      if (CPUArch == "6T2" || IsARMv7)
2110        Builder.defineMacro("__thumb2__");
2111    }
2112
2113    // Note, this is always on in gcc, even though it doesn't make sense.
2114    Builder.defineMacro("__APCS_32__");
2115
2116    if (FPUModeIsVFP((FPUMode) FPU))
2117      Builder.defineMacro("__VFP_FP__");
2118
2119    // This only gets set when Neon instructions are actually available, unlike
2120    // the VFP define, hence the soft float and arch check. This is subtly
2121    // different from gcc, we follow the intent which was that it should be set
2122    // when Neon instructions are actually available.
2123    if (FPU == NeonFPU && !SoftFloat && IsARMv7)
2124      Builder.defineMacro("__ARM_NEON__");
2125  }
2126  virtual void getTargetBuiltins(const Builtin::Info *&Records,
2127                                 unsigned &NumRecords) const {
2128    Records = BuiltinInfo;
2129    NumRecords = clang::ARM::LastTSBuiltin-Builtin::FirstTSBuiltin;
2130  }
2131  virtual const char *getVAListDeclaration() const {
2132    return "typedef void* __builtin_va_list;";
2133  }
2134  virtual void getGCCRegNames(const char * const *&Names,
2135                              unsigned &NumNames) const;
2136  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2137                                unsigned &NumAliases) const;
2138  virtual bool validateAsmConstraint(const char *&Name,
2139                                     TargetInfo::ConstraintInfo &Info) const {
2140    // FIXME: Check if this is complete
2141    switch (*Name) {
2142    default:
2143    case 'l': // r0-r7
2144    case 'h': // r8-r15
2145    case 'w': // VFP Floating point register single precision
2146    case 'P': // VFP Floating point register double precision
2147      Info.setAllowsRegister();
2148      return true;
2149    case 'Q': // A memory address that is a single base register.
2150      Info.setAllowsMemory();
2151      return true;
2152    case 'U': // a memory reference...
2153      switch (Name[1]) {
2154      case 'q': // ...ARMV4 ldrsb
2155      case 'v': // ...VFP load/store (reg+constant offset)
2156      case 'y': // ...iWMMXt load/store
2157      case 't': // address valid for load/store opaque types wider
2158	        // than 128-bits
2159      case 'n': // valid address for Neon doubleword vector load/store
2160      case 'm': // valid address for Neon element and structure load/store
2161      case 's': // valid address for non-offset loads/stores of quad-word
2162	        // values in four ARM registers
2163        Info.setAllowsMemory();
2164        Name++;
2165        return true;
2166      }
2167    }
2168    return false;
2169  }
2170  virtual std::string convertConstraint(const char *&Constraint) const {
2171    std::string R;
2172    switch (*Constraint) {
2173    case 'U':   // Two-character constraint; add "^" hint for later parsing.
2174      R = std::string("^") + std::string(Constraint, 2);
2175      Constraint++;
2176      break;
2177    case 'p': // 'p' should be translated to 'r' by default.
2178      R = std::string("r");
2179      break;
2180    default:
2181      return std::string(1, *Constraint);
2182    }
2183    return R;
2184  }
2185  virtual const char *getClobbers() const {
2186    // FIXME: Is this really right?
2187    return "";
2188  }
2189};
2190
2191const char * const ARMTargetInfo::GCCRegNames[] = {
2192  // Integer registers
2193  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2194  "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
2195
2196  // Float registers
2197  "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
2198  "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
2199  "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
2200  "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
2201
2202  // Double registers
2203  "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
2204  "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
2205  "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
2206  "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
2207
2208  // Quad registers
2209  "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
2210  "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"
2211};
2212
2213void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
2214                                   unsigned &NumNames) const {
2215  Names = GCCRegNames;
2216  NumNames = llvm::array_lengthof(GCCRegNames);
2217}
2218
2219const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
2220  { { "a1" }, "r0" },
2221  { { "a2" }, "r1" },
2222  { { "a3" }, "r2" },
2223  { { "a4" }, "r3" },
2224  { { "v1" }, "r4" },
2225  { { "v2" }, "r5" },
2226  { { "v3" }, "r6" },
2227  { { "v4" }, "r7" },
2228  { { "v5" }, "r8" },
2229  { { "v6", "rfp" }, "r9" },
2230  { { "sl" }, "r10" },
2231  { { "fp" }, "r11" },
2232  { { "ip" }, "r12" },
2233  { { "r13" }, "sp" },
2234  { { "r14" }, "lr" },
2235  { { "r15" }, "pc" },
2236  // The S, D and Q registers overlap, but aren't really aliases; we
2237  // don't want to substitute one of these for a different-sized one.
2238};
2239
2240void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2241                                       unsigned &NumAliases) const {
2242  Aliases = GCCRegAliases;
2243  NumAliases = llvm::array_lengthof(GCCRegAliases);
2244}
2245
2246const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
2247#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
2248#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
2249                                              ALL_LANGUAGES },
2250#include "clang/Basic/BuiltinsARM.def"
2251};
2252} // end anonymous namespace.
2253
2254
2255namespace {
2256class DarwinARMTargetInfo :
2257  public DarwinTargetInfo<ARMTargetInfo> {
2258protected:
2259  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
2260                            MacroBuilder &Builder) const {
2261    getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
2262  }
2263
2264public:
2265  DarwinARMTargetInfo(const std::string& triple)
2266    : DarwinTargetInfo<ARMTargetInfo>(triple) {
2267    HasAlignMac68kSupport = true;
2268  }
2269};
2270} // end anonymous namespace.
2271
2272namespace {
2273class SparcV8TargetInfo : public TargetInfo {
2274  static const TargetInfo::GCCRegAlias GCCRegAliases[];
2275  static const char * const GCCRegNames[];
2276  bool SoftFloat;
2277public:
2278  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
2279    // FIXME: Support Sparc quad-precision long double?
2280    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2281                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
2282  }
2283  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
2284                                 const std::string &Name,
2285                                 bool Enabled) const {
2286    if (Name == "soft-float")
2287      Features[Name] = Enabled;
2288    else
2289      return false;
2290
2291    return true;
2292  }
2293  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
2294    SoftFloat = false;
2295    for (unsigned i = 0, e = Features.size(); i != e; ++i)
2296      if (Features[i] == "+soft-float")
2297        SoftFloat = true;
2298  }
2299  virtual void getTargetDefines(const LangOptions &Opts,
2300                                MacroBuilder &Builder) const {
2301    DefineStd(Builder, "sparc", Opts);
2302    Builder.defineMacro("__sparcv8");
2303    Builder.defineMacro("__REGISTER_PREFIX__", "");
2304
2305    if (SoftFloat)
2306      Builder.defineMacro("SOFT_FLOAT", "1");
2307  }
2308  virtual void getTargetBuiltins(const Builtin::Info *&Records,
2309                                 unsigned &NumRecords) const {
2310    // FIXME: Implement!
2311  }
2312  virtual const char *getVAListDeclaration() const {
2313    return "typedef void* __builtin_va_list;";
2314  }
2315  virtual void getGCCRegNames(const char * const *&Names,
2316                              unsigned &NumNames) const;
2317  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2318                                unsigned &NumAliases) const;
2319  virtual bool validateAsmConstraint(const char *&Name,
2320                                     TargetInfo::ConstraintInfo &info) const {
2321    // FIXME: Implement!
2322    return false;
2323  }
2324  virtual const char *getClobbers() const {
2325    // FIXME: Implement!
2326    return "";
2327  }
2328};
2329
2330const char * const SparcV8TargetInfo::GCCRegNames[] = {
2331  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2332  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
2333  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
2334  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
2335};
2336
2337void SparcV8TargetInfo::getGCCRegNames(const char * const *&Names,
2338                                       unsigned &NumNames) const {
2339  Names = GCCRegNames;
2340  NumNames = llvm::array_lengthof(GCCRegNames);
2341}
2342
2343const TargetInfo::GCCRegAlias SparcV8TargetInfo::GCCRegAliases[] = {
2344  { { "g0" }, "r0" },
2345  { { "g1" }, "r1" },
2346  { { "g2" }, "r2" },
2347  { { "g3" }, "r3" },
2348  { { "g4" }, "r4" },
2349  { { "g5" }, "r5" },
2350  { { "g6" }, "r6" },
2351  { { "g7" }, "r7" },
2352  { { "o0" }, "r8" },
2353  { { "o1" }, "r9" },
2354  { { "o2" }, "r10" },
2355  { { "o3" }, "r11" },
2356  { { "o4" }, "r12" },
2357  { { "o5" }, "r13" },
2358  { { "o6", "sp" }, "r14" },
2359  { { "o7" }, "r15" },
2360  { { "l0" }, "r16" },
2361  { { "l1" }, "r17" },
2362  { { "l2" }, "r18" },
2363  { { "l3" }, "r19" },
2364  { { "l4" }, "r20" },
2365  { { "l5" }, "r21" },
2366  { { "l6" }, "r22" },
2367  { { "l7" }, "r23" },
2368  { { "i0" }, "r24" },
2369  { { "i1" }, "r25" },
2370  { { "i2" }, "r26" },
2371  { { "i3" }, "r27" },
2372  { { "i4" }, "r28" },
2373  { { "i5" }, "r29" },
2374  { { "i6", "fp" }, "r30" },
2375  { { "i7" }, "r31" },
2376};
2377
2378void SparcV8TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2379                                         unsigned &NumAliases) const {
2380  Aliases = GCCRegAliases;
2381  NumAliases = llvm::array_lengthof(GCCRegAliases);
2382}
2383} // end anonymous namespace.
2384
2385namespace {
2386class AuroraUXSparcV8TargetInfo : public AuroraUXTargetInfo<SparcV8TargetInfo> {
2387public:
2388  AuroraUXSparcV8TargetInfo(const std::string& triple) :
2389      AuroraUXTargetInfo<SparcV8TargetInfo>(triple) {
2390    SizeType = UnsignedInt;
2391    PtrDiffType = SignedInt;
2392  }
2393};
2394class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
2395public:
2396  SolarisSparcV8TargetInfo(const std::string& triple) :
2397      SolarisTargetInfo<SparcV8TargetInfo>(triple) {
2398    SizeType = UnsignedInt;
2399    PtrDiffType = SignedInt;
2400  }
2401};
2402} // end anonymous namespace.
2403
2404namespace {
2405  class MSP430TargetInfo : public TargetInfo {
2406    static const char * const GCCRegNames[];
2407  public:
2408    MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
2409      TLSSupported = false;
2410      IntWidth = 16; IntAlign = 16;
2411      LongWidth = 32; LongLongWidth = 64;
2412      LongAlign = LongLongAlign = 16;
2413      PointerWidth = 16; PointerAlign = 16;
2414      SizeType = UnsignedInt;
2415      IntMaxType = SignedLong;
2416      UIntMaxType = UnsignedLong;
2417      IntPtrType = SignedShort;
2418      PtrDiffType = SignedInt;
2419      SigAtomicType = SignedLong;
2420      DescriptionString = "e-p:16:16:16-i8:8:8-i16:16:16-i32:16:32-n8:16";
2421   }
2422    virtual void getTargetDefines(const LangOptions &Opts,
2423                                  MacroBuilder &Builder) const {
2424      Builder.defineMacro("MSP430");
2425      Builder.defineMacro("__MSP430__");
2426      // FIXME: defines for different 'flavours' of MCU
2427    }
2428    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2429                                   unsigned &NumRecords) const {
2430     // FIXME: Implement.
2431      Records = 0;
2432      NumRecords = 0;
2433    }
2434    virtual void getGCCRegNames(const char * const *&Names,
2435                                unsigned &NumNames) const;
2436    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2437                                  unsigned &NumAliases) const {
2438      // No aliases.
2439      Aliases = 0;
2440      NumAliases = 0;
2441    }
2442    virtual bool validateAsmConstraint(const char *&Name,
2443                                       TargetInfo::ConstraintInfo &info) const {
2444      // No target constraints for now.
2445      return false;
2446    }
2447    virtual const char *getClobbers() const {
2448      // FIXME: Is this really right?
2449      return "";
2450    }
2451    virtual const char *getVAListDeclaration() const {
2452      // FIXME: implement
2453      return "typedef char* __builtin_va_list;";
2454   }
2455  };
2456
2457  const char * const MSP430TargetInfo::GCCRegNames[] = {
2458    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2459    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2460  };
2461
2462  void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
2463                                        unsigned &NumNames) const {
2464    Names = GCCRegNames;
2465    NumNames = llvm::array_lengthof(GCCRegNames);
2466  }
2467}
2468
2469
2470namespace {
2471  class SystemZTargetInfo : public TargetInfo {
2472    static const char * const GCCRegNames[];
2473  public:
2474    SystemZTargetInfo(const std::string& triple) : TargetInfo(triple) {
2475      TLSSupported = false;
2476      IntWidth = IntAlign = 32;
2477      LongWidth = LongLongWidth = LongAlign = LongLongAlign = 64;
2478      PointerWidth = PointerAlign = 64;
2479      DescriptionString = "E-p:64:64:64-i8:8:16-i16:16:16-i32:32:32-"
2480      "i64:64:64-f32:32:32-f64:64:64-f128:128:128-a0:16:16-n32:64";
2481   }
2482    virtual void getTargetDefines(const LangOptions &Opts,
2483                                  MacroBuilder &Builder) const {
2484      Builder.defineMacro("__s390__");
2485      Builder.defineMacro("__s390x__");
2486    }
2487    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2488                                   unsigned &NumRecords) const {
2489      // FIXME: Implement.
2490      Records = 0;
2491      NumRecords = 0;
2492    }
2493
2494    virtual void getGCCRegNames(const char * const *&Names,
2495                                unsigned &NumNames) const;
2496    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2497                                  unsigned &NumAliases) const {
2498      // No aliases.
2499      Aliases = 0;
2500      NumAliases = 0;
2501    }
2502    virtual bool validateAsmConstraint(const char *&Name,
2503                                       TargetInfo::ConstraintInfo &info) const {
2504      // FIXME: implement
2505      return true;
2506    }
2507    virtual const char *getClobbers() const {
2508      // FIXME: Is this really right?
2509      return "";
2510    }
2511    virtual const char *getVAListDeclaration() const {
2512      // FIXME: implement
2513      return "typedef char* __builtin_va_list;";
2514   }
2515  };
2516
2517  const char * const SystemZTargetInfo::GCCRegNames[] = {
2518    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2519    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2520  };
2521
2522  void SystemZTargetInfo::getGCCRegNames(const char * const *&Names,
2523                                         unsigned &NumNames) const {
2524    Names = GCCRegNames;
2525    NumNames = llvm::array_lengthof(GCCRegNames);
2526  }
2527}
2528
2529namespace {
2530  class BlackfinTargetInfo : public TargetInfo {
2531    static const char * const GCCRegNames[];
2532  public:
2533    BlackfinTargetInfo(const std::string& triple) : TargetInfo(triple) {
2534      TLSSupported = false;
2535      DoubleAlign = 32;
2536      LongLongAlign = 32;
2537      LongDoubleAlign = 32;
2538      DescriptionString = "e-p:32:32-i64:32-f64:32-n32";
2539    }
2540
2541    virtual void getTargetDefines(const LangOptions &Opts,
2542                                  MacroBuilder &Builder) const {
2543      DefineStd(Builder, "bfin", Opts);
2544      DefineStd(Builder, "BFIN", Opts);
2545      Builder.defineMacro("__ADSPBLACKFIN__");
2546      // FIXME: This one is really dependent on -mcpu
2547      Builder.defineMacro("__ADSPLPBLACKFIN__");
2548      // FIXME: Add cpu-dependent defines and __SILICON_REVISION__
2549    }
2550
2551    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2552                                   unsigned &NumRecords) const {
2553      // FIXME: Implement.
2554      Records = 0;
2555      NumRecords = 0;
2556    }
2557
2558    virtual void getGCCRegNames(const char * const *&Names,
2559                                unsigned &NumNames) const;
2560
2561    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2562                                  unsigned &NumAliases) const {
2563      // No aliases.
2564      Aliases = 0;
2565      NumAliases = 0;
2566    }
2567
2568    virtual bool validateAsmConstraint(const char *&Name,
2569                                       TargetInfo::ConstraintInfo &Info) const {
2570      if (strchr("adzDWeABbvfcCtukxywZY", Name[0])) {
2571        Info.setAllowsRegister();
2572        return true;
2573      }
2574      return false;
2575    }
2576
2577    virtual const char *getClobbers() const {
2578      return "";
2579    }
2580
2581    virtual const char *getVAListDeclaration() const {
2582      return "typedef char* __builtin_va_list;";
2583    }
2584  };
2585
2586  const char * const BlackfinTargetInfo::GCCRegNames[] = {
2587    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2588    "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
2589    "i0", "i1", "i2", "i3", "b0", "b1", "b2", "b3",
2590    "l0", "l1", "l2", "l3", "m0", "m1", "m2", "m3",
2591    "a0", "a1", "cc",
2592    "rets", "reti", "retx", "retn", "rete", "astat", "seqstat", "usp",
2593    "argp", "lt0", "lt1", "lc0", "lc1", "lb0", "lb1"
2594  };
2595
2596  void BlackfinTargetInfo::getGCCRegNames(const char * const *&Names,
2597                                          unsigned &NumNames) const {
2598    Names = GCCRegNames;
2599    NumNames = llvm::array_lengthof(GCCRegNames);
2600  }
2601}
2602
2603namespace {
2604
2605  // LLVM and Clang cannot be used directly to output native binaries for
2606  // target, but is used to compile C code to llvm bitcode with correct
2607  // type and alignment information.
2608  //
2609  // TCE uses the llvm bitcode as input and uses it for generating customized
2610  // target processor and program binary. TCE co-design environment is
2611  // publicly available in http://tce.cs.tut.fi
2612
2613  class TCETargetInfo : public TargetInfo{
2614  public:
2615    TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
2616      TLSSupported = false;
2617      IntWidth = 32;
2618      LongWidth = LongLongWidth = 32;
2619      PointerWidth = 32;
2620      IntAlign = 32;
2621      LongAlign = LongLongAlign = 32;
2622      PointerAlign = 32;
2623      SizeType = UnsignedInt;
2624      IntMaxType = SignedLong;
2625      UIntMaxType = UnsignedLong;
2626      IntPtrType = SignedInt;
2627      PtrDiffType = SignedInt;
2628      FloatWidth = 32;
2629      FloatAlign = 32;
2630      DoubleWidth = 32;
2631      DoubleAlign = 32;
2632      LongDoubleWidth = 32;
2633      LongDoubleAlign = 32;
2634      FloatFormat = &llvm::APFloat::IEEEsingle;
2635      DoubleFormat = &llvm::APFloat::IEEEsingle;
2636      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
2637      DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-"
2638                          "i16:16:32-i32:32:32-i64:32:32-"
2639                          "f32:32:32-f64:32:32-v64:32:32-"
2640                          "v128:32:32-a0:0:32-n32";
2641    }
2642
2643    virtual void getTargetDefines(const LangOptions &Opts,
2644                                  MacroBuilder &Builder) const {
2645      DefineStd(Builder, "tce", Opts);
2646      Builder.defineMacro("__TCE__");
2647      Builder.defineMacro("__TCE_V1__");
2648    }
2649    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2650                                   unsigned &NumRecords) const {}
2651    virtual const char *getClobbers() const {
2652      return "";
2653    }
2654    virtual const char *getVAListDeclaration() const {
2655      return "typedef void* __builtin_va_list;";
2656    }
2657    virtual void getGCCRegNames(const char * const *&Names,
2658                                unsigned &NumNames) const {}
2659    virtual bool validateAsmConstraint(const char *&Name,
2660                                       TargetInfo::ConstraintInfo &info) const {
2661      return true;
2662    }
2663    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2664                                  unsigned &NumAliases) const {}
2665  };
2666}
2667
2668namespace {
2669class MipsTargetInfo : public TargetInfo {
2670  std::string ABI, CPU;
2671  static const TargetInfo::GCCRegAlias GCCRegAliases[];
2672  static const char * const GCCRegNames[];
2673public:
2674  MipsTargetInfo(const std::string& triple) : TargetInfo(triple), ABI("o32") {
2675    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
2676                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
2677    SizeType = UnsignedInt;
2678    PtrDiffType = SignedInt;
2679  }
2680  virtual const char *getABI() const { return ABI.c_str(); }
2681  virtual bool setABI(const std::string &Name) {
2682
2683    if ((Name == "o32") || (Name == "eabi")) {
2684      ABI = Name;
2685      return true;
2686    } else
2687      return false;
2688  }
2689  virtual bool setCPU(const std::string &Name) {
2690    CPU = Name;
2691    return true;
2692  }
2693  void getDefaultFeatures(const std::string &CPU,
2694                          llvm::StringMap<bool> &Features) const {
2695    Features[ABI] = true;
2696    Features[CPU] = true;
2697  }
2698  virtual void getArchDefines(const LangOptions &Opts,
2699                                MacroBuilder &Builder) const {
2700    if (ABI == "o32") {
2701      Builder.defineMacro("__mips_o32");
2702      Builder.defineMacro("_ABIO32", "1");
2703      Builder.defineMacro("_MIPS_SIM", "_ABIO32");
2704    }
2705    else if (ABI == "n32") {
2706      Builder.defineMacro("__mips_n32");
2707      Builder.defineMacro("_ABIN32", "2");
2708      Builder.defineMacro("_MIPS_SIM", "_ABIN32");
2709    }
2710    else if (ABI == "n64") {
2711      Builder.defineMacro("__mips_n64");
2712      Builder.defineMacro("_ABI64", "3");
2713      Builder.defineMacro("_MIPS_SIM", "_ABI64");
2714    }
2715    else if (ABI == "o64") {
2716      Builder.defineMacro("__mips_o64");
2717      Builder.defineMacro("_ABIO64", "4");
2718      Builder.defineMacro("_MIPS_SIM", "_ABIO64");
2719    }
2720    else if (ABI == "eabi")
2721      Builder.defineMacro("__mips_eabi");
2722  }
2723  virtual void getTargetDefines(const LangOptions &Opts,
2724                                MacroBuilder &Builder) const {
2725    DefineStd(Builder, "mips", Opts);
2726    Builder.defineMacro("_mips");
2727    DefineStd(Builder, "MIPSEB", Opts);
2728    Builder.defineMacro("_MIPSEB");
2729    Builder.defineMacro("__REGISTER_PREFIX__", "");
2730    getArchDefines(Opts, Builder);
2731  }
2732  virtual void getTargetBuiltins(const Builtin::Info *&Records,
2733                                 unsigned &NumRecords) const {
2734    // FIXME: Implement!
2735  }
2736  virtual const char *getVAListDeclaration() const {
2737    return "typedef void* __builtin_va_list;";
2738  }
2739  virtual void getGCCRegNames(const char * const *&Names,
2740                              unsigned &NumNames) const;
2741  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2742                                unsigned &NumAliases) const;
2743  virtual bool validateAsmConstraint(const char *&Name,
2744                                     TargetInfo::ConstraintInfo &Info) const {
2745    switch (*Name) {
2746    default:
2747    case 'r': // CPU registers.
2748    case 'd': // Equivalent to "r" unless generating MIPS16 code.
2749    case 'y': // Equivalent to "r", backwards compatibility only.
2750    case 'f': // floating-point registers.
2751      Info.setAllowsRegister();
2752      return true;
2753    }
2754    return false;
2755  }
2756
2757  virtual const char *getClobbers() const {
2758    // FIXME: Implement!
2759    return "";
2760  }
2761};
2762
2763const char * const MipsTargetInfo::GCCRegNames[] = {
2764  "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
2765  "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
2766  "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
2767  "$24",  "$25",  "$26",  "$27",  "$28",  "$sp",  "$fp",  "$31",
2768  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
2769  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
2770  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
2771  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
2772  "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
2773  "$fcc5","$fcc6","$fcc7"
2774};
2775
2776void MipsTargetInfo::getGCCRegNames(const char * const *&Names,
2777                                       unsigned &NumNames) const {
2778  Names = GCCRegNames;
2779  NumNames = llvm::array_lengthof(GCCRegNames);
2780}
2781
2782const TargetInfo::GCCRegAlias MipsTargetInfo::GCCRegAliases[] = {
2783  { { "at" },  "$1" },
2784  { { "v0" },  "$2" },
2785  { { "v1" },  "$3" },
2786  { { "a0" },  "$4" },
2787  { { "a1" },  "$5" },
2788  { { "a2" },  "$6" },
2789  { { "a3" },  "$7" },
2790  { { "t0" },  "$8" },
2791  { { "t1" },  "$9" },
2792  { { "t2" }, "$10" },
2793  { { "t3" }, "$11" },
2794  { { "t4" }, "$12" },
2795  { { "t5" }, "$13" },
2796  { { "t6" }, "$14" },
2797  { { "t7" }, "$15" },
2798  { { "s0" }, "$16" },
2799  { { "s1" }, "$17" },
2800  { { "s2" }, "$18" },
2801  { { "s3" }, "$19" },
2802  { { "s4" }, "$20" },
2803  { { "s5" }, "$21" },
2804  { { "s6" }, "$22" },
2805  { { "s7" }, "$23" },
2806  { { "t8" }, "$24" },
2807  { { "t9" }, "$25" },
2808  { { "k0" }, "$26" },
2809  { { "k1" }, "$27" },
2810  { { "gp" }, "$28" },
2811  { { "sp" }, "$29" },
2812  { { "fp" }, "$30" },
2813  { { "ra" }, "$31" }
2814};
2815
2816void MipsTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2817                                         unsigned &NumAliases) const {
2818  Aliases = GCCRegAliases;
2819  NumAliases = llvm::array_lengthof(GCCRegAliases);
2820}
2821} // end anonymous namespace.
2822
2823namespace {
2824class MipselTargetInfo : public MipsTargetInfo {
2825public:
2826  MipselTargetInfo(const std::string& triple) : MipsTargetInfo(triple) {
2827    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
2828                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
2829  }
2830
2831  virtual void getTargetDefines(const LangOptions &Opts,
2832                                MacroBuilder &Builder) const;
2833};
2834
2835void MipselTargetInfo::getTargetDefines(const LangOptions &Opts,
2836                                        MacroBuilder &Builder) const {
2837  DefineStd(Builder, "mips", Opts);
2838  Builder.defineMacro("_mips");
2839  DefineStd(Builder, "MIPSEL", Opts);
2840  Builder.defineMacro("_MIPSEL");
2841  Builder.defineMacro("__REGISTER_PREFIX__", "");
2842  getArchDefines(Opts, Builder);
2843}
2844} // end anonymous namespace.
2845
2846namespace {
2847class PNaClTargetInfo : public TargetInfo {
2848public:
2849  PNaClTargetInfo(const std::string& triple) : TargetInfo(triple) {
2850    this->UserLabelPrefix = "";
2851    this->LongAlign = 32;
2852    this->LongWidth = 32;
2853    this->PointerAlign = 32;
2854    this->PointerWidth = 32;
2855    this->IntMaxType = TargetInfo::SignedLongLong;
2856    this->UIntMaxType = TargetInfo::UnsignedLongLong;
2857    this->Int64Type = TargetInfo::SignedLongLong;
2858    this->SizeType = TargetInfo::UnsignedInt;
2859    DescriptionString = "e-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-"
2860                        "f32:32:32-f64:64:64-p:32:32:32-v128:32:32";
2861  }
2862
2863  void getDefaultFeatures(const std::string &CPU,
2864                          llvm::StringMap<bool> &Features) const {
2865  }
2866  virtual void getArchDefines(const LangOptions &Opts,
2867                              MacroBuilder &Builder) const {
2868    Builder.defineMacro("__le32__");
2869    Builder.defineMacro("__pnacl__");
2870  }
2871  virtual void getTargetDefines(const LangOptions &Opts,
2872                                MacroBuilder &Builder) const {
2873    Builder.defineMacro("__native_client__");
2874    getArchDefines(Opts, Builder);
2875  }
2876  virtual void getTargetBuiltins(const Builtin::Info *&Records,
2877                                 unsigned &NumRecords) const {
2878  }
2879  virtual const char *getVAListDeclaration() const {
2880    return "typedef void* __builtin_va_list;";
2881  }
2882  virtual void getGCCRegNames(const char * const *&Names,
2883                              unsigned &NumNames) const;
2884  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2885                                unsigned &NumAliases) const;
2886  virtual bool validateAsmConstraint(const char *&Name,
2887                                     TargetInfo::ConstraintInfo &Info) const {
2888    return false;
2889  }
2890
2891  virtual const char *getClobbers() const {
2892    return "";
2893  }
2894};
2895
2896void PNaClTargetInfo::getGCCRegNames(const char * const *&Names,
2897                                     unsigned &NumNames) const {
2898  Names = NULL;
2899  NumNames = 0;
2900}
2901
2902void PNaClTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2903                                       unsigned &NumAliases) const {
2904  Aliases = NULL;
2905  NumAliases = 0;
2906}
2907} // end anonymous namespace.
2908
2909
2910//===----------------------------------------------------------------------===//
2911// Driver code
2912//===----------------------------------------------------------------------===//
2913
2914static TargetInfo *AllocateTarget(const std::string &T) {
2915  llvm::Triple Triple(T);
2916  llvm::Triple::OSType os = Triple.getOS();
2917
2918  switch (Triple.getArch()) {
2919  default:
2920    return NULL;
2921
2922  case llvm::Triple::arm:
2923  case llvm::Triple::thumb:
2924    if (Triple.isOSDarwin())
2925      return new DarwinARMTargetInfo(T);
2926
2927    switch (os) {
2928    case llvm::Triple::Linux:
2929      return new LinuxTargetInfo<ARMTargetInfo>(T);
2930    case llvm::Triple::FreeBSD:
2931      return new FreeBSDTargetInfo<ARMTargetInfo>(T);
2932    case llvm::Triple::NetBSD:
2933      return new NetBSDTargetInfo<ARMTargetInfo>(T);
2934    case llvm::Triple::RTEMS:
2935      return new RTEMSTargetInfo<ARMTargetInfo>(T);
2936    default:
2937      return new ARMTargetInfo(T);
2938    }
2939
2940  case llvm::Triple::bfin:
2941    if ( os == llvm::Triple::RTEMS )
2942      return new RTEMSTargetInfo<BlackfinTargetInfo>(T);
2943    return new BlackfinTargetInfo(T);
2944
2945  case llvm::Triple::msp430:
2946    return new MSP430TargetInfo(T);
2947
2948  case llvm::Triple::mips:
2949    switch (os) {
2950    case llvm::Triple::Psp:
2951      return new PSPTargetInfo<MipsTargetInfo>(T);
2952    case llvm::Triple::Linux:
2953      return new LinuxTargetInfo<MipsTargetInfo>(T);
2954    case llvm::Triple::RTEMS:
2955      return new RTEMSTargetInfo<MipsTargetInfo>(T);
2956    case llvm::Triple::FreeBSD:
2957      return new FreeBSDTargetInfo<MipsTargetInfo>(T);
2958    case llvm::Triple::NetBSD:
2959      return new NetBSDTargetInfo<MipsTargetInfo>(T);
2960    default:
2961      return new MipsTargetInfo(T);
2962    }
2963
2964  case llvm::Triple::mipsel:
2965    switch (os) {
2966    case llvm::Triple::Psp:
2967      return new PSPTargetInfo<MipselTargetInfo>(T);
2968    case llvm::Triple::Linux:
2969      return new LinuxTargetInfo<MipselTargetInfo>(T);
2970    case llvm::Triple::RTEMS:
2971      return new RTEMSTargetInfo<MipselTargetInfo>(T);
2972    case llvm::Triple::FreeBSD:
2973      return new FreeBSDTargetInfo<MipselTargetInfo>(T);
2974    case llvm::Triple::NetBSD:
2975      return new NetBSDTargetInfo<MipselTargetInfo>(T);
2976    default:
2977      return new MipsTargetInfo(T);
2978    }
2979
2980  case llvm::Triple::le32:
2981    switch (os) {
2982      case llvm::Triple::NativeClient:
2983        return new PNaClTargetInfo(T);
2984      default:
2985        return NULL;
2986    }
2987
2988  case llvm::Triple::ppc:
2989    if (Triple.isOSDarwin())
2990      return new DarwinPPC32TargetInfo(T);
2991    switch (os) {
2992    case llvm::Triple::FreeBSD:
2993      return new FreeBSDTargetInfo<PPC32TargetInfo>(T);
2994    case llvm::Triple::NetBSD:
2995      return new NetBSDTargetInfo<PPC32TargetInfo>(T);
2996    case llvm::Triple::RTEMS:
2997      return new RTEMSTargetInfo<PPC32TargetInfo>(T);
2998    default:
2999      return new PPC32TargetInfo(T);
3000    }
3001
3002  case llvm::Triple::ppc64:
3003    if (Triple.isOSDarwin())
3004      return new DarwinPPC64TargetInfo(T);
3005    switch (os) {
3006    case llvm::Triple::Lv2:
3007      return new PS3PPUTargetInfo<PPC64TargetInfo>(T);
3008    case llvm::Triple::FreeBSD:
3009      return new FreeBSDTargetInfo<PPC64TargetInfo>(T);
3010    case llvm::Triple::NetBSD:
3011      return new NetBSDTargetInfo<PPC64TargetInfo>(T);
3012    default:
3013      return new PPC64TargetInfo(T);
3014    }
3015
3016  case llvm::Triple::ptx32:
3017    return new PTX32TargetInfo(T);
3018  case llvm::Triple::ptx64:
3019    return new PTX64TargetInfo(T);
3020
3021  case llvm::Triple::mblaze:
3022    return new MBlazeTargetInfo(T);
3023
3024  case llvm::Triple::sparc:
3025    switch (os) {
3026    case llvm::Triple::AuroraUX:
3027      return new AuroraUXSparcV8TargetInfo(T);
3028    case llvm::Triple::Solaris:
3029      return new SolarisSparcV8TargetInfo(T);
3030    case llvm::Triple::NetBSD:
3031      return new NetBSDTargetInfo<SparcV8TargetInfo>(T);
3032    case llvm::Triple::RTEMS:
3033      return new RTEMSTargetInfo<SparcV8TargetInfo>(T);
3034    default:
3035      return new SparcV8TargetInfo(T);
3036    }
3037
3038  // FIXME: Need a real SPU target.
3039  case llvm::Triple::cellspu:
3040    return new PS3SPUTargetInfo<PPC64TargetInfo>(T);
3041
3042  case llvm::Triple::systemz:
3043    return new SystemZTargetInfo(T);
3044
3045  case llvm::Triple::tce:
3046    return new TCETargetInfo(T);
3047
3048  case llvm::Triple::x86:
3049    if (Triple.isOSDarwin())
3050      return new DarwinI386TargetInfo(T);
3051
3052    switch (os) {
3053    case llvm::Triple::AuroraUX:
3054      return new AuroraUXTargetInfo<X86_32TargetInfo>(T);
3055    case llvm::Triple::Linux:
3056      return new LinuxTargetInfo<X86_32TargetInfo>(T);
3057    case llvm::Triple::DragonFly:
3058      return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
3059    case llvm::Triple::NetBSD:
3060      return new NetBSDTargetInfo<X86_32TargetInfo>(T);
3061    case llvm::Triple::OpenBSD:
3062      return new OpenBSDI386TargetInfo(T);
3063    case llvm::Triple::FreeBSD:
3064      return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
3065    case llvm::Triple::Minix:
3066      return new MinixTargetInfo<X86_32TargetInfo>(T);
3067    case llvm::Triple::Solaris:
3068      return new SolarisTargetInfo<X86_32TargetInfo>(T);
3069    case llvm::Triple::Cygwin:
3070      return new CygwinX86_32TargetInfo(T);
3071    case llvm::Triple::MinGW32:
3072      return new MinGWX86_32TargetInfo(T);
3073    case llvm::Triple::Win32:
3074      return new VisualStudioWindowsX86_32TargetInfo(T);
3075    case llvm::Triple::Haiku:
3076      return new HaikuX86_32TargetInfo(T);
3077    case llvm::Triple::RTEMS:
3078      return new RTEMSX86_32TargetInfo(T);
3079    default:
3080      return new X86_32TargetInfo(T);
3081    }
3082
3083  case llvm::Triple::x86_64:
3084    if (Triple.isOSDarwin() || Triple.getEnvironment() == llvm::Triple::MachO)
3085      return new DarwinX86_64TargetInfo(T);
3086
3087    switch (os) {
3088    case llvm::Triple::AuroraUX:
3089      return new AuroraUXTargetInfo<X86_64TargetInfo>(T);
3090    case llvm::Triple::Linux:
3091      return new LinuxTargetInfo<X86_64TargetInfo>(T);
3092    case llvm::Triple::DragonFly:
3093      return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(T);
3094    case llvm::Triple::NetBSD:
3095      return new NetBSDTargetInfo<X86_64TargetInfo>(T);
3096    case llvm::Triple::OpenBSD:
3097      return new OpenBSDX86_64TargetInfo(T);
3098    case llvm::Triple::FreeBSD:
3099      return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
3100    case llvm::Triple::Solaris:
3101      return new SolarisTargetInfo<X86_64TargetInfo>(T);
3102    case llvm::Triple::MinGW32:
3103      return new MinGWX86_64TargetInfo(T);
3104    case llvm::Triple::Win32:   // This is what Triple.h supports now.
3105      return new VisualStudioWindowsX86_64TargetInfo(T);
3106    default:
3107      return new X86_64TargetInfo(T);
3108    }
3109  }
3110}
3111
3112/// CreateTargetInfo - Return the target info object for the specified target
3113/// triple.
3114TargetInfo *TargetInfo::CreateTargetInfo(Diagnostic &Diags,
3115                                         TargetOptions &Opts) {
3116  llvm::Triple Triple(Opts.Triple);
3117
3118  // Construct the target
3119  llvm::OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
3120  if (!Target) {
3121    Diags.Report(diag::err_target_unknown_triple) << Triple.str();
3122    return 0;
3123  }
3124
3125  // Set the target CPU if specified.
3126  if (!Opts.CPU.empty() && !Target->setCPU(Opts.CPU)) {
3127    Diags.Report(diag::err_target_unknown_cpu) << Opts.CPU;
3128    return 0;
3129  }
3130
3131  // Set the target ABI if specified.
3132  if (!Opts.ABI.empty() && !Target->setABI(Opts.ABI)) {
3133    Diags.Report(diag::err_target_unknown_abi) << Opts.ABI;
3134    return 0;
3135  }
3136
3137  // Set the target C++ ABI.
3138  if (!Opts.CXXABI.empty() && !Target->setCXXABI(Opts.CXXABI)) {
3139    Diags.Report(diag::err_target_unknown_cxxabi) << Opts.CXXABI;
3140    return 0;
3141  }
3142
3143  // Compute the default target features, we need the target to handle this
3144  // because features may have dependencies on one another.
3145  llvm::StringMap<bool> Features;
3146  Target->getDefaultFeatures(Opts.CPU, Features);
3147
3148  // Apply the user specified deltas.
3149  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
3150         ie = Opts.Features.end(); it != ie; ++it) {
3151    const char *Name = it->c_str();
3152
3153    // Apply the feature via the target.
3154    if ((Name[0] != '-' && Name[0] != '+') ||
3155        !Target->setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
3156      Diags.Report(diag::err_target_invalid_feature) << Name;
3157      return 0;
3158    }
3159  }
3160
3161  // Add the features to the compile options.
3162  //
3163  // FIXME: If we are completely confident that we have the right set, we only
3164  // need to pass the minuses.
3165  Opts.Features.clear();
3166  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
3167         ie = Features.end(); it != ie; ++it)
3168    Opts.Features.push_back(std::string(it->second ? "+" : "-") +
3169                            it->first().str());
3170  Target->HandleTargetFeatures(Opts.Features);
3171
3172  return Target.take();
3173}
3174