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