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