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