Targets.cpp revision a291d5fb78bd7b95d825cb3c6ddd5dff44f6f624
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      .Case("cortex-m0", "6M")
1844      .Default(0);
1845  }
1846  virtual bool setCPU(const std::string &Name) {
1847    if (!getCPUDefineSuffix(Name))
1848      return false;
1849
1850    CPU = Name;
1851    return true;
1852  }
1853  virtual void getTargetDefines(const LangOptions &Opts,
1854                                MacroBuilder &Builder) const {
1855    // Target identification.
1856    Builder.defineMacro("__arm");
1857    Builder.defineMacro("__arm__");
1858
1859    // Target properties.
1860    Builder.defineMacro("__ARMEL__");
1861    Builder.defineMacro("__LITTLE_ENDIAN__");
1862    Builder.defineMacro("__REGISTER_PREFIX__", "");
1863
1864    llvm::StringRef CPUArch = getCPUDefineSuffix(CPU);
1865    Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
1866
1867    // Subtarget options.
1868
1869    // FIXME: It's more complicated than this and we don't really support
1870    // interworking.
1871    if ('5' <= CPUArch[0] && CPUArch[0] <= '7')
1872      Builder.defineMacro("__THUMB_INTERWORK__");
1873
1874    if (ABI == "aapcs" || ABI == "aapcs-linux")
1875      Builder.defineMacro("__ARM_EABI__");
1876
1877    if (SoftFloat)
1878      Builder.defineMacro("__SOFTFP__");
1879
1880    if (CPU == "xscale")
1881      Builder.defineMacro("__XSCALE__");
1882
1883    bool IsThumb2 = IsThumb && (CPUArch == "6T2" || CPUArch.startswith("7"));
1884    if (IsThumb) {
1885      Builder.defineMacro("__THUMBEL__");
1886      Builder.defineMacro("__thumb__");
1887      if (IsThumb2)
1888        Builder.defineMacro("__thumb2__");
1889    }
1890
1891    // Note, this is always on in gcc, even though it doesn't make sense.
1892    Builder.defineMacro("__APCS_32__");
1893
1894    if (FPUModeIsVFP((FPUMode) FPU))
1895      Builder.defineMacro("__VFP_FP__");
1896
1897    // This only gets set when Neon instructions are actually available, unlike
1898    // the VFP define, hence the soft float and arch check. This is subtly
1899    // different from gcc, we follow the intent which was that it should be set
1900    // when Neon instructions are actually available.
1901    if (FPU == NeonFPU && !SoftFloat && IsThumb2)
1902      Builder.defineMacro("__ARM_NEON__");
1903  }
1904  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1905                                 unsigned &NumRecords) const {
1906    Records = BuiltinInfo;
1907    NumRecords = clang::ARM::LastTSBuiltin-Builtin::FirstTSBuiltin;
1908  }
1909  virtual const char *getVAListDeclaration() const {
1910    return "typedef char* __builtin_va_list;";
1911  }
1912  virtual void getGCCRegNames(const char * const *&Names,
1913                              unsigned &NumNames) const;
1914  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1915                                unsigned &NumAliases) const;
1916  virtual bool validateAsmConstraint(const char *&Name,
1917                                     TargetInfo::ConstraintInfo &Info) const {
1918    // FIXME: Check if this is complete
1919    switch (*Name) {
1920    default:
1921    case 'l': // r0-r7
1922    case 'h': // r8-r15
1923    case 'w': // VFP Floating point register single precision
1924    case 'P': // VFP Floating point register double precision
1925      Info.setAllowsRegister();
1926      return true;
1927    }
1928    return false;
1929  }
1930  virtual const char *getClobbers() const {
1931    // FIXME: Is this really right?
1932    return "";
1933  }
1934};
1935
1936const char * const ARMTargetInfo::GCCRegNames[] = {
1937  // Integer registers
1938  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1939  "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
1940
1941  // Float registers
1942  "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
1943  "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
1944  "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
1945  "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
1946
1947  // Double registers
1948  "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
1949  "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
1950  "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
1951  "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
1952
1953  // Quad registers
1954  "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
1955  "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15"
1956};
1957
1958void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
1959                                   unsigned &NumNames) const {
1960  Names = GCCRegNames;
1961  NumNames = llvm::array_lengthof(GCCRegNames);
1962}
1963
1964const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
1965  { { "a1" }, "r0" },
1966  { { "a2" }, "r1" },
1967  { { "a3" }, "r2" },
1968  { { "a4" }, "r3" },
1969  { { "v1" }, "r4" },
1970  { { "v2" }, "r5" },
1971  { { "v3" }, "r6" },
1972  { { "v4" }, "r7" },
1973  { { "v5" }, "r8" },
1974  { { "v6", "rfp" }, "r9" },
1975  { { "sl" }, "r10" },
1976  { { "fp" }, "r11" },
1977  { { "ip" }, "r12" },
1978  { { "r13" }, "sp" },
1979  { { "r14" }, "lr" },
1980  { { "r15" }, "pc" },
1981  // The S, D and Q registers overlap, but aren't really aliases; we
1982  // don't want to substitute one of these for a different-sized one.
1983};
1984
1985void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1986                                       unsigned &NumAliases) const {
1987  Aliases = GCCRegAliases;
1988  NumAliases = llvm::array_lengthof(GCCRegAliases);
1989}
1990
1991const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
1992#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES, false },
1993#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,\
1994                                              ALL_LANGUAGES, false },
1995#include "clang/Basic/BuiltinsARM.def"
1996};
1997} // end anonymous namespace.
1998
1999
2000namespace {
2001class DarwinARMTargetInfo :
2002  public DarwinTargetInfo<ARMTargetInfo> {
2003protected:
2004  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
2005                            MacroBuilder &Builder) const {
2006    getDarwinDefines(Builder, Opts, Triple);
2007  }
2008
2009public:
2010  DarwinARMTargetInfo(const std::string& triple)
2011    : DarwinTargetInfo<ARMTargetInfo>(triple) {
2012    HasAlignMac68kSupport = true;
2013  }
2014};
2015} // end anonymous namespace.
2016
2017namespace {
2018class SparcV8TargetInfo : public TargetInfo {
2019  static const TargetInfo::GCCRegAlias GCCRegAliases[];
2020  static const char * const GCCRegNames[];
2021  bool SoftFloat;
2022public:
2023  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
2024    // FIXME: Support Sparc quad-precision long double?
2025    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
2026                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
2027  }
2028  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
2029                                 const std::string &Name,
2030                                 bool Enabled) const {
2031    if (Name == "soft-float")
2032      Features[Name] = Enabled;
2033    else
2034      return false;
2035
2036    return true;
2037  }
2038  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
2039    SoftFloat = false;
2040    for (unsigned i = 0, e = Features.size(); i != e; ++i)
2041      if (Features[i] == "+soft-float")
2042        SoftFloat = true;
2043  }
2044  virtual void getTargetDefines(const LangOptions &Opts,
2045                                MacroBuilder &Builder) const {
2046    DefineStd(Builder, "sparc", Opts);
2047    Builder.defineMacro("__sparcv8");
2048    Builder.defineMacro("__REGISTER_PREFIX__", "");
2049
2050    if (SoftFloat)
2051      Builder.defineMacro("SOFT_FLOAT", "1");
2052  }
2053  virtual void getTargetBuiltins(const Builtin::Info *&Records,
2054                                 unsigned &NumRecords) const {
2055    // FIXME: Implement!
2056  }
2057  virtual const char *getVAListDeclaration() const {
2058    return "typedef void* __builtin_va_list;";
2059  }
2060  virtual void getGCCRegNames(const char * const *&Names,
2061                              unsigned &NumNames) const;
2062  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2063                                unsigned &NumAliases) const;
2064  virtual bool validateAsmConstraint(const char *&Name,
2065                                     TargetInfo::ConstraintInfo &info) const {
2066    // FIXME: Implement!
2067    return false;
2068  }
2069  virtual const char *getClobbers() const {
2070    // FIXME: Implement!
2071    return "";
2072  }
2073};
2074
2075const char * const SparcV8TargetInfo::GCCRegNames[] = {
2076  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2077  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
2078  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
2079  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
2080};
2081
2082void SparcV8TargetInfo::getGCCRegNames(const char * const *&Names,
2083                                       unsigned &NumNames) const {
2084  Names = GCCRegNames;
2085  NumNames = llvm::array_lengthof(GCCRegNames);
2086}
2087
2088const TargetInfo::GCCRegAlias SparcV8TargetInfo::GCCRegAliases[] = {
2089  { { "g0" }, "r0" },
2090  { { "g1" }, "r1" },
2091  { { "g2" }, "r2" },
2092  { { "g3" }, "r3" },
2093  { { "g4" }, "r4" },
2094  { { "g5" }, "r5" },
2095  { { "g6" }, "r6" },
2096  { { "g7" }, "r7" },
2097  { { "o0" }, "r8" },
2098  { { "o1" }, "r9" },
2099  { { "o2" }, "r10" },
2100  { { "o3" }, "r11" },
2101  { { "o4" }, "r12" },
2102  { { "o5" }, "r13" },
2103  { { "o6", "sp" }, "r14" },
2104  { { "o7" }, "r15" },
2105  { { "l0" }, "r16" },
2106  { { "l1" }, "r17" },
2107  { { "l2" }, "r18" },
2108  { { "l3" }, "r19" },
2109  { { "l4" }, "r20" },
2110  { { "l5" }, "r21" },
2111  { { "l6" }, "r22" },
2112  { { "l7" }, "r23" },
2113  { { "i0" }, "r24" },
2114  { { "i1" }, "r25" },
2115  { { "i2" }, "r26" },
2116  { { "i3" }, "r27" },
2117  { { "i4" }, "r28" },
2118  { { "i5" }, "r29" },
2119  { { "i6", "fp" }, "r30" },
2120  { { "i7" }, "r31" },
2121};
2122
2123void SparcV8TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2124                                         unsigned &NumAliases) const {
2125  Aliases = GCCRegAliases;
2126  NumAliases = llvm::array_lengthof(GCCRegAliases);
2127}
2128} // end anonymous namespace.
2129
2130namespace {
2131class AuroraUXSparcV8TargetInfo : public AuroraUXTargetInfo<SparcV8TargetInfo> {
2132public:
2133  AuroraUXSparcV8TargetInfo(const std::string& triple) :
2134      AuroraUXTargetInfo<SparcV8TargetInfo>(triple) {
2135    SizeType = UnsignedInt;
2136    PtrDiffType = SignedInt;
2137  }
2138};
2139class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
2140public:
2141  SolarisSparcV8TargetInfo(const std::string& triple) :
2142      SolarisTargetInfo<SparcV8TargetInfo>(triple) {
2143    SizeType = UnsignedInt;
2144    PtrDiffType = SignedInt;
2145  }
2146};
2147} // end anonymous namespace.
2148
2149namespace {
2150  class MSP430TargetInfo : public TargetInfo {
2151    static const char * const GCCRegNames[];
2152  public:
2153    MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
2154      TLSSupported = false;
2155      IntWidth = 16; IntAlign = 16;
2156      LongWidth = 32; LongLongWidth = 64;
2157      LongAlign = LongLongAlign = 16;
2158      PointerWidth = 16; PointerAlign = 16;
2159      SizeType = UnsignedInt;
2160      IntMaxType = SignedLong;
2161      UIntMaxType = UnsignedLong;
2162      IntPtrType = SignedShort;
2163      PtrDiffType = SignedInt;
2164      SigAtomicType = SignedLong;
2165      DescriptionString = "e-p:16:16:16-i8:8:8-i16:16:16-i32:16:32-n8:16";
2166   }
2167    virtual void getTargetDefines(const LangOptions &Opts,
2168                                  MacroBuilder &Builder) const {
2169      Builder.defineMacro("MSP430");
2170      Builder.defineMacro("__MSP430__");
2171      // FIXME: defines for different 'flavours' of MCU
2172    }
2173    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2174                                   unsigned &NumRecords) const {
2175     // FIXME: Implement.
2176      Records = 0;
2177      NumRecords = 0;
2178    }
2179    virtual void getGCCRegNames(const char * const *&Names,
2180                                unsigned &NumNames) const;
2181    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2182                                  unsigned &NumAliases) const {
2183      // No aliases.
2184      Aliases = 0;
2185      NumAliases = 0;
2186    }
2187    virtual bool validateAsmConstraint(const char *&Name,
2188                                       TargetInfo::ConstraintInfo &info) const {
2189      // No target constraints for now.
2190      return false;
2191    }
2192    virtual const char *getClobbers() const {
2193      // FIXME: Is this really right?
2194      return "";
2195    }
2196    virtual const char *getVAListDeclaration() const {
2197      // FIXME: implement
2198      return "typedef char* __builtin_va_list;";
2199   }
2200  };
2201
2202  const char * const MSP430TargetInfo::GCCRegNames[] = {
2203    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2204    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2205  };
2206
2207  void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
2208                                        unsigned &NumNames) const {
2209    Names = GCCRegNames;
2210    NumNames = llvm::array_lengthof(GCCRegNames);
2211  }
2212}
2213
2214
2215namespace {
2216  class SystemZTargetInfo : public TargetInfo {
2217    static const char * const GCCRegNames[];
2218  public:
2219    SystemZTargetInfo(const std::string& triple) : TargetInfo(triple) {
2220      TLSSupported = false;
2221      IntWidth = IntAlign = 32;
2222      LongWidth = LongLongWidth = LongAlign = LongLongAlign = 64;
2223      PointerWidth = PointerAlign = 64;
2224      DescriptionString = "E-p:64:64:64-i8:8:16-i16:16:16-i32:32:32-"
2225      "i64:64:64-f32:32:32-f64:64:64-f128:128:128-a0:16:16-n32:64";
2226   }
2227    virtual void getTargetDefines(const LangOptions &Opts,
2228                                  MacroBuilder &Builder) const {
2229      Builder.defineMacro("__s390__");
2230      Builder.defineMacro("__s390x__");
2231    }
2232    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2233                                   unsigned &NumRecords) const {
2234      // FIXME: Implement.
2235      Records = 0;
2236      NumRecords = 0;
2237    }
2238
2239    virtual void getGCCRegNames(const char * const *&Names,
2240                                unsigned &NumNames) const;
2241    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2242                                  unsigned &NumAliases) const {
2243      // No aliases.
2244      Aliases = 0;
2245      NumAliases = 0;
2246    }
2247    virtual bool validateAsmConstraint(const char *&Name,
2248                                       TargetInfo::ConstraintInfo &info) const {
2249      // FIXME: implement
2250      return true;
2251    }
2252    virtual const char *getClobbers() const {
2253      // FIXME: Is this really right?
2254      return "";
2255    }
2256    virtual const char *getVAListDeclaration() const {
2257      // FIXME: implement
2258      return "typedef char* __builtin_va_list;";
2259   }
2260  };
2261
2262  const char * const SystemZTargetInfo::GCCRegNames[] = {
2263    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2264    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2265  };
2266
2267  void SystemZTargetInfo::getGCCRegNames(const char * const *&Names,
2268                                         unsigned &NumNames) const {
2269    Names = GCCRegNames;
2270    NumNames = llvm::array_lengthof(GCCRegNames);
2271  }
2272}
2273
2274namespace {
2275  class BlackfinTargetInfo : public TargetInfo {
2276    static const char * const GCCRegNames[];
2277  public:
2278    BlackfinTargetInfo(const std::string& triple) : TargetInfo(triple) {
2279      TLSSupported = false;
2280      DoubleAlign = 32;
2281      LongLongAlign = 32;
2282      LongDoubleAlign = 32;
2283      DescriptionString = "e-p:32:32-i64:32-f64:32-n32";
2284    }
2285
2286    virtual void getTargetDefines(const LangOptions &Opts,
2287                                  MacroBuilder &Builder) const {
2288      DefineStd(Builder, "bfin", Opts);
2289      DefineStd(Builder, "BFIN", Opts);
2290      Builder.defineMacro("__ADSPBLACKFIN__");
2291      // FIXME: This one is really dependent on -mcpu
2292      Builder.defineMacro("__ADSPLPBLACKFIN__");
2293      // FIXME: Add cpu-dependent defines and __SILICON_REVISION__
2294    }
2295
2296    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2297                                   unsigned &NumRecords) const {
2298      // FIXME: Implement.
2299      Records = 0;
2300      NumRecords = 0;
2301    }
2302
2303    virtual void getGCCRegNames(const char * const *&Names,
2304                                unsigned &NumNames) const;
2305
2306    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2307                                  unsigned &NumAliases) const {
2308      // No aliases.
2309      Aliases = 0;
2310      NumAliases = 0;
2311    }
2312
2313    virtual bool validateAsmConstraint(const char *&Name,
2314                                       TargetInfo::ConstraintInfo &Info) const {
2315      if (strchr("adzDWeABbvfcCtukxywZY", Name[0])) {
2316        Info.setAllowsRegister();
2317        return true;
2318      }
2319      return false;
2320    }
2321
2322    virtual const char *getClobbers() const {
2323      return "";
2324    }
2325
2326    virtual const char *getVAListDeclaration() const {
2327      return "typedef char* __builtin_va_list;";
2328    }
2329  };
2330
2331  const char * const BlackfinTargetInfo::GCCRegNames[] = {
2332    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2333    "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
2334    "i0", "i1", "i2", "i3", "b0", "b1", "b2", "b3",
2335    "l0", "l1", "l2", "l3", "m0", "m1", "m2", "m3",
2336    "a0", "a1", "cc",
2337    "rets", "reti", "retx", "retn", "rete", "astat", "seqstat", "usp",
2338    "argp", "lt0", "lt1", "lc0", "lc1", "lb0", "lb1"
2339  };
2340
2341  void BlackfinTargetInfo::getGCCRegNames(const char * const *&Names,
2342                                          unsigned &NumNames) const {
2343    Names = GCCRegNames;
2344    NumNames = llvm::array_lengthof(GCCRegNames);
2345  }
2346}
2347
2348namespace {
2349
2350  // LLVM and Clang cannot be used directly to output native binaries for
2351  // target, but is used to compile C code to llvm bitcode with correct
2352  // type and alignment information.
2353  //
2354  // TCE uses the llvm bitcode as input and uses it for generating customized
2355  // target processor and program binary. TCE co-design environment is
2356  // publicly available in http://tce.cs.tut.fi
2357
2358  class TCETargetInfo : public TargetInfo{
2359  public:
2360    TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
2361      TLSSupported = false;
2362      IntWidth = 32;
2363      LongWidth = LongLongWidth = 32;
2364      PointerWidth = 32;
2365      IntAlign = 32;
2366      LongAlign = LongLongAlign = 32;
2367      PointerAlign = 32;
2368      SizeType = UnsignedInt;
2369      IntMaxType = SignedLong;
2370      UIntMaxType = UnsignedLong;
2371      IntPtrType = SignedInt;
2372      PtrDiffType = SignedInt;
2373      FloatWidth = 32;
2374      FloatAlign = 32;
2375      DoubleWidth = 32;
2376      DoubleAlign = 32;
2377      LongDoubleWidth = 32;
2378      LongDoubleAlign = 32;
2379      FloatFormat = &llvm::APFloat::IEEEsingle;
2380      DoubleFormat = &llvm::APFloat::IEEEsingle;
2381      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
2382      DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-"
2383                          "i16:16:32-i32:32:32-i64:32:32-"
2384                          "f32:32:32-f64:32:32-v64:32:32-"
2385                          "v128:32:32-a0:0:32-n32";
2386    }
2387
2388    virtual void getTargetDefines(const LangOptions &Opts,
2389                                  MacroBuilder &Builder) const {
2390      DefineStd(Builder, "tce", Opts);
2391      Builder.defineMacro("__TCE__");
2392      Builder.defineMacro("__TCE_V1__");
2393    }
2394    virtual void getTargetBuiltins(const Builtin::Info *&Records,
2395                                   unsigned &NumRecords) const {}
2396    virtual const char *getClobbers() const {
2397      return "";
2398    }
2399    virtual const char *getVAListDeclaration() const {
2400      return "typedef void* __builtin_va_list;";
2401    }
2402    virtual void getGCCRegNames(const char * const *&Names,
2403                                unsigned &NumNames) const {}
2404    virtual bool validateAsmConstraint(const char *&Name,
2405                                       TargetInfo::ConstraintInfo &info) const {
2406      return true;
2407    }
2408    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2409                                  unsigned &NumAliases) const {}
2410  };
2411}
2412
2413namespace {
2414class MipsTargetInfo : public TargetInfo {
2415  std::string ABI, CPU;
2416  static const TargetInfo::GCCRegAlias GCCRegAliases[];
2417  static const char * const GCCRegNames[];
2418public:
2419  MipsTargetInfo(const std::string& triple) : TargetInfo(triple), ABI("o32") {
2420    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
2421                        "i64:32:64-f32:32:32-f64:64:64-v64:64:64-n32";
2422  }
2423  virtual const char *getABI() const { return ABI.c_str(); }
2424  virtual bool setABI(const std::string &Name) {
2425
2426    if ((Name == "o32") || (Name == "eabi")) {
2427      ABI = Name;
2428      return true;
2429    } else
2430      return false;
2431  }
2432  virtual bool setCPU(const std::string &Name) {
2433    CPU = Name;
2434    return true;
2435  }
2436  void getDefaultFeatures(const std::string &CPU,
2437                          llvm::StringMap<bool> &Features) const {
2438    Features[ABI] = true;
2439    Features[CPU] = true;
2440  }
2441  virtual void getArchDefines(const LangOptions &Opts,
2442                                MacroBuilder &Builder) const {
2443    if (ABI == "o32")
2444      Builder.defineMacro("__mips_o32");
2445    else if (ABI == "eabi")
2446      Builder.defineMacro("__mips_eabi");
2447  }
2448  virtual void getTargetDefines(const LangOptions &Opts,
2449                                MacroBuilder &Builder) const {
2450    DefineStd(Builder, "mips", Opts);
2451    Builder.defineMacro("_mips");
2452    DefineStd(Builder, "MIPSEB", Opts);
2453    Builder.defineMacro("_MIPSEB");
2454    Builder.defineMacro("__REGISTER_PREFIX__", "");
2455    getArchDefines(Opts, Builder);
2456  }
2457  virtual void getTargetBuiltins(const Builtin::Info *&Records,
2458                                 unsigned &NumRecords) const {
2459    // FIXME: Implement!
2460  }
2461  virtual const char *getVAListDeclaration() const {
2462    return "typedef void* __builtin_va_list;";
2463  }
2464  virtual void getGCCRegNames(const char * const *&Names,
2465                              unsigned &NumNames) const;
2466  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
2467                                unsigned &NumAliases) const;
2468  virtual bool validateAsmConstraint(const char *&Name,
2469                                     TargetInfo::ConstraintInfo &Info) const {
2470    switch (*Name) {
2471    default:
2472    case 'r': // CPU registers.
2473    case 'd': // Equivalent to "r" unless generating MIPS16 code.
2474    case 'y': // Equivalent to "r", backwards compatibility only.
2475    case 'f': // floating-point registers.
2476      Info.setAllowsRegister();
2477      return true;
2478    }
2479    return false;
2480  }
2481
2482  virtual const char *getClobbers() const {
2483    // FIXME: Implement!
2484    return "";
2485  }
2486};
2487
2488const char * const MipsTargetInfo::GCCRegNames[] = {
2489  "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
2490  "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
2491  "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
2492  "$24",  "$25",  "$26",  "$27",  "$28",  "$sp",  "$fp",  "$31",
2493  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
2494  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
2495  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
2496  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
2497  "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
2498  "$fcc5","$fcc6","$fcc7"
2499};
2500
2501void MipsTargetInfo::getGCCRegNames(const char * const *&Names,
2502                                       unsigned &NumNames) const {
2503  Names = GCCRegNames;
2504  NumNames = llvm::array_lengthof(GCCRegNames);
2505}
2506
2507const TargetInfo::GCCRegAlias MipsTargetInfo::GCCRegAliases[] = {
2508  { { "at" },  "$1" },
2509  { { "v0" },  "$2" },
2510  { { "v1" },  "$3" },
2511  { { "a0" },  "$4" },
2512  { { "a1" },  "$5" },
2513  { { "a2" },  "$6" },
2514  { { "a3" },  "$7" },
2515  { { "t0" },  "$8" },
2516  { { "t1" },  "$9" },
2517  { { "t2" }, "$10" },
2518  { { "t3" }, "$11" },
2519  { { "t4" }, "$12" },
2520  { { "t5" }, "$13" },
2521  { { "t6" }, "$14" },
2522  { { "t7" }, "$15" },
2523  { { "s0" }, "$16" },
2524  { { "s1" }, "$17" },
2525  { { "s2" }, "$18" },
2526  { { "s3" }, "$19" },
2527  { { "s4" }, "$20" },
2528  { { "s5" }, "$21" },
2529  { { "s6" }, "$22" },
2530  { { "s7" }, "$23" },
2531  { { "t8" }, "$24" },
2532  { { "t9" }, "$25" },
2533  { { "k0" }, "$26" },
2534  { { "k1" }, "$27" },
2535  { { "gp" }, "$28" },
2536  { { "sp" }, "$29" },
2537  { { "fp" }, "$30" },
2538  { { "ra" }, "$31" }
2539};
2540
2541void MipsTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2542                                         unsigned &NumAliases) const {
2543  Aliases = GCCRegAliases;
2544  NumAliases = llvm::array_lengthof(GCCRegAliases);
2545}
2546} // end anonymous namespace.
2547
2548namespace {
2549class MipselTargetInfo : public MipsTargetInfo {
2550public:
2551  MipselTargetInfo(const std::string& triple) : MipsTargetInfo(triple) {
2552    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
2553                        "i64:32:64-f32:32:32-f64:64:64-v64:64:64-n32";
2554  }
2555
2556  virtual void getTargetDefines(const LangOptions &Opts,
2557                                MacroBuilder &Builder) const;
2558};
2559
2560void MipselTargetInfo::getTargetDefines(const LangOptions &Opts,
2561                                        MacroBuilder &Builder) const {
2562  DefineStd(Builder, "mips", Opts);
2563  Builder.defineMacro("_mips");
2564  DefineStd(Builder, "MIPSEL", Opts);
2565  Builder.defineMacro("_MIPSEL");
2566  Builder.defineMacro("__REGISTER_PREFIX__", "");
2567  getArchDefines(Opts, Builder);
2568}
2569} // end anonymous namespace.
2570
2571//===----------------------------------------------------------------------===//
2572// Driver code
2573//===----------------------------------------------------------------------===//
2574
2575static TargetInfo *AllocateTarget(const std::string &T) {
2576  llvm::Triple Triple(T);
2577  llvm::Triple::OSType os = Triple.getOS();
2578
2579  switch (Triple.getArch()) {
2580  default:
2581    return NULL;
2582
2583  case llvm::Triple::arm:
2584  case llvm::Triple::thumb:
2585    switch (os) {
2586    case llvm::Triple::Linux:
2587      return new LinuxTargetInfo<ARMTargetInfo>(T);
2588    case llvm::Triple::Darwin:
2589      return new DarwinARMTargetInfo(T);
2590    case llvm::Triple::FreeBSD:
2591      return new FreeBSDTargetInfo<ARMTargetInfo>(T);
2592    default:
2593      return new ARMTargetInfo(T);
2594    }
2595
2596  case llvm::Triple::bfin:
2597    return new BlackfinTargetInfo(T);
2598
2599  case llvm::Triple::msp430:
2600    return new MSP430TargetInfo(T);
2601
2602  case llvm::Triple::mips:
2603    if (os == llvm::Triple::Psp)
2604      return new PSPTargetInfo<MipsTargetInfo>(T);
2605    if (os == llvm::Triple::Linux)
2606      return new LinuxTargetInfo<MipsTargetInfo>(T);
2607    return new MipsTargetInfo(T);
2608
2609  case llvm::Triple::mipsel:
2610    if (os == llvm::Triple::Psp)
2611      return new PSPTargetInfo<MipselTargetInfo>(T);
2612    if (os == llvm::Triple::Linux)
2613      return new LinuxTargetInfo<MipselTargetInfo>(T);
2614    return new MipselTargetInfo(T);
2615
2616  case llvm::Triple::ppc:
2617    if (os == llvm::Triple::Darwin)
2618      return new DarwinPPC32TargetInfo(T);
2619    else if (os == llvm::Triple::FreeBSD)
2620      return new FreeBSDTargetInfo<PPC32TargetInfo>(T);
2621    return new PPC32TargetInfo(T);
2622
2623  case llvm::Triple::ppc64:
2624    if (os == llvm::Triple::Darwin)
2625      return new DarwinPPC64TargetInfo(T);
2626    else if (os == llvm::Triple::Lv2)
2627      return new PS3PPUTargetInfo<PPC64TargetInfo>(T);
2628    else if (os == llvm::Triple::FreeBSD)
2629      return new FreeBSDTargetInfo<PPC64TargetInfo>(T);
2630    return new PPC64TargetInfo(T);
2631
2632  case llvm::Triple::mblaze:
2633    return new MBlazeTargetInfo(T);
2634
2635  case llvm::Triple::sparc:
2636    if (os == llvm::Triple::AuroraUX)
2637      return new AuroraUXSparcV8TargetInfo(T);
2638    if (os == llvm::Triple::Solaris)
2639      return new SolarisSparcV8TargetInfo(T);
2640    return new SparcV8TargetInfo(T);
2641
2642  // FIXME: Need a real SPU target.
2643  case llvm::Triple::cellspu:
2644    return new PS3SPUTargetInfo<PPC64TargetInfo>(T);
2645
2646  case llvm::Triple::systemz:
2647    return new SystemZTargetInfo(T);
2648
2649  case llvm::Triple::tce:
2650    return new TCETargetInfo(T);
2651
2652  case llvm::Triple::x86:
2653    switch (os) {
2654    case llvm::Triple::AuroraUX:
2655      return new AuroraUXTargetInfo<X86_32TargetInfo>(T);
2656    case llvm::Triple::Darwin:
2657      return new DarwinI386TargetInfo(T);
2658    case llvm::Triple::Linux:
2659      return new LinuxTargetInfo<X86_32TargetInfo>(T);
2660    case llvm::Triple::DragonFly:
2661      return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
2662    case llvm::Triple::NetBSD:
2663      return new NetBSDTargetInfo<X86_32TargetInfo>(T);
2664    case llvm::Triple::OpenBSD:
2665      return new OpenBSDI386TargetInfo(T);
2666    case llvm::Triple::FreeBSD:
2667      return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
2668    case llvm::Triple::Minix:
2669      return new MinixTargetInfo<X86_32TargetInfo>(T);
2670    case llvm::Triple::Solaris:
2671      return new SolarisTargetInfo<X86_32TargetInfo>(T);
2672    case llvm::Triple::Cygwin:
2673      return new CygwinX86_32TargetInfo(T);
2674    case llvm::Triple::MinGW32:
2675      return new MinGWX86_32TargetInfo(T);
2676    case llvm::Triple::Win32:
2677      return new VisualStudioWindowsX86_32TargetInfo(T);
2678    case llvm::Triple::Haiku:
2679      return new HaikuX86_32TargetInfo(T);
2680    default:
2681      return new X86_32TargetInfo(T);
2682    }
2683
2684  case llvm::Triple::x86_64:
2685    switch (os) {
2686    case llvm::Triple::AuroraUX:
2687      return new AuroraUXTargetInfo<X86_64TargetInfo>(T);
2688    case llvm::Triple::Darwin:
2689      return new DarwinX86_64TargetInfo(T);
2690    case llvm::Triple::Linux:
2691      return new LinuxTargetInfo<X86_64TargetInfo>(T);
2692    case llvm::Triple::DragonFly:
2693      return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(T);
2694    case llvm::Triple::NetBSD:
2695      return new NetBSDTargetInfo<X86_64TargetInfo>(T);
2696    case llvm::Triple::OpenBSD:
2697      return new OpenBSDX86_64TargetInfo(T);
2698    case llvm::Triple::FreeBSD:
2699      return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
2700    case llvm::Triple::Solaris:
2701      return new SolarisTargetInfo<X86_64TargetInfo>(T);
2702    case llvm::Triple::MinGW32:
2703      return new MinGWX86_64TargetInfo(T);
2704    case llvm::Triple::Win32:   // This is what Triple.h supports now.
2705      if (Triple.getEnvironment() == llvm::Triple::MachO)
2706        return new DarwinX86_64TargetInfo(T);
2707      else
2708        return new VisualStudioWindowsX86_64TargetInfo(T);
2709    default:
2710      return new X86_64TargetInfo(T);
2711    }
2712  }
2713}
2714
2715/// CreateTargetInfo - Return the target info object for the specified target
2716/// triple.
2717TargetInfo *TargetInfo::CreateTargetInfo(Diagnostic &Diags,
2718                                         TargetOptions &Opts) {
2719  llvm::Triple Triple(Opts.Triple);
2720
2721  // Construct the target
2722  llvm::OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
2723  if (!Target) {
2724    Diags.Report(diag::err_target_unknown_triple) << Triple.str();
2725    return 0;
2726  }
2727
2728  // Set the target CPU if specified.
2729  if (!Opts.CPU.empty() && !Target->setCPU(Opts.CPU)) {
2730    Diags.Report(diag::err_target_unknown_cpu) << Opts.CPU;
2731    return 0;
2732  }
2733
2734  // Set the target ABI if specified.
2735  if (!Opts.ABI.empty() && !Target->setABI(Opts.ABI)) {
2736    Diags.Report(diag::err_target_unknown_abi) << Opts.ABI;
2737    return 0;
2738  }
2739
2740  // Set the target C++ ABI.
2741  if (!Opts.CXXABI.empty() && !Target->setCXXABI(Opts.CXXABI)) {
2742    Diags.Report(diag::err_target_unknown_cxxabi) << Opts.CXXABI;
2743    return 0;
2744  }
2745
2746  // Compute the default target features, we need the target to handle this
2747  // because features may have dependencies on one another.
2748  llvm::StringMap<bool> Features;
2749  Target->getDefaultFeatures(Opts.CPU, Features);
2750
2751  // Apply the user specified deltas.
2752  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
2753         ie = Opts.Features.end(); it != ie; ++it) {
2754    const char *Name = it->c_str();
2755
2756    // Apply the feature via the target.
2757    if ((Name[0] != '-' && Name[0] != '+') ||
2758        !Target->setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
2759      Diags.Report(diag::err_target_invalid_feature) << Name;
2760      return 0;
2761    }
2762  }
2763
2764  // Add the features to the compile options.
2765  //
2766  // FIXME: If we are completely confident that we have the right set, we only
2767  // need to pass the minuses.
2768  Opts.Features.clear();
2769  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
2770         ie = Features.end(); it != ie; ++it)
2771    Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
2772  Target->HandleTargetFeatures(Opts.Features);
2773
2774  return Target.take();
2775}
2776