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