Targets.cpp revision e03ae30bc26bbcb0cddc688e3f35a97558118a5f
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 = false;
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
165
166// DragonFlyBSD Target
167template<typename Target>
168class DragonFlyBSDTargetInfo : public OSTargetInfo<Target> {
169protected:
170  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
171                            MacroBuilder &Builder) const {
172    // DragonFly defines; list based off of gcc output
173    Builder.defineMacro("__DragonFly__");
174    Builder.defineMacro("__DragonFly_cc_version", "100001");
175    Builder.defineMacro("__ELF__");
176    Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
177    Builder.defineMacro("__tune_i386__");
178    DefineStd(Builder, "unix", Opts);
179  }
180public:
181  DragonFlyBSDTargetInfo(const std::string &triple)
182    : OSTargetInfo<Target>(triple) {}
183};
184
185// FreeBSD Target
186template<typename Target>
187class FreeBSDTargetInfo : public OSTargetInfo<Target> {
188protected:
189  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
190                            MacroBuilder &Builder) const {
191    // FreeBSD defines; list based off of gcc output
192
193    // FIXME: Move version number handling to llvm::Triple.
194    llvm::StringRef Release = Triple.getOSName().substr(strlen("freebsd"), 1);
195
196    Builder.defineMacro("__FreeBSD__", Release);
197    Builder.defineMacro("__FreeBSD_cc_version", Release + "00001");
198    Builder.defineMacro("__KPRINTF_ATTRIBUTE__");
199    DefineStd(Builder, "unix", Opts);
200    Builder.defineMacro("__ELF__");
201  }
202public:
203  FreeBSDTargetInfo(const std::string &triple)
204    : OSTargetInfo<Target>(triple) {
205      this->UserLabelPrefix = "";
206    }
207};
208
209// Linux target
210template<typename Target>
211class LinuxTargetInfo : public OSTargetInfo<Target> {
212protected:
213  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
214                            MacroBuilder &Builder) const {
215    // Linux defines; list based off of gcc output
216    DefineStd(Builder, "unix", Opts);
217    DefineStd(Builder, "linux", Opts);
218    Builder.defineMacro("__gnu_linux__");
219    Builder.defineMacro("__ELF__");
220    if (Opts.POSIXThreads)
221      Builder.defineMacro("_REENTRANT");
222  }
223public:
224  LinuxTargetInfo(const std::string& triple)
225    : OSTargetInfo<Target>(triple) {
226    this->UserLabelPrefix = "";
227  }
228};
229
230// NetBSD Target
231template<typename Target>
232class NetBSDTargetInfo : public OSTargetInfo<Target> {
233protected:
234  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
235                            MacroBuilder &Builder) const {
236    // NetBSD defines; list based off of gcc output
237    Builder.defineMacro("__NetBSD__");
238    Builder.defineMacro("__unix__");
239    Builder.defineMacro("__ELF__");
240    if (Opts.POSIXThreads)
241      Builder.defineMacro("_POSIX_THREADS");
242  }
243public:
244  NetBSDTargetInfo(const std::string &triple)
245    : OSTargetInfo<Target>(triple) {
246      this->UserLabelPrefix = "";
247    }
248};
249
250// OpenBSD Target
251template<typename Target>
252class OpenBSDTargetInfo : public OSTargetInfo<Target> {
253protected:
254  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
255                            MacroBuilder &Builder) const {
256    // OpenBSD defines; list based off of gcc output
257
258    Builder.defineMacro("__OpenBSD__");
259    DefineStd(Builder, "unix", Opts);
260    Builder.defineMacro("__ELF__");
261    if (Opts.POSIXThreads)
262      Builder.defineMacro("_POSIX_THREADS");
263  }
264public:
265  OpenBSDTargetInfo(const std::string &triple)
266    : OSTargetInfo<Target>(triple) {}
267};
268
269// PSP Target
270template<typename Target>
271class PSPTargetInfo : public OSTargetInfo<Target> {
272protected:
273  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
274                            MacroBuilder &Builder) const {
275    // PSP defines; list based on the output of the pspdev gcc toolchain.
276    Builder.defineMacro("PSP");
277    Builder.defineMacro("_PSP");
278    Builder.defineMacro("__psp__");
279    Builder.defineMacro("__ELF__");
280  }
281public:
282  PSPTargetInfo(const std::string& triple)
283    : OSTargetInfo<Target>(triple) {
284    this->UserLabelPrefix = "";
285  }
286};
287
288// PS3 PPU Target
289template<typename Target>
290class PS3PPUTargetInfo : public OSTargetInfo<Target> {
291protected:
292  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
293                            MacroBuilder &Builder) const {
294    // PS3 PPU defines.
295    Builder.defineMacro("__PPU__");
296    Builder.defineMacro("__CELLOS_LV2__");
297    Builder.defineMacro("__ELF__");
298    Builder.defineMacro("__LP32__");
299  }
300public:
301  PS3PPUTargetInfo(const std::string& triple)
302    : OSTargetInfo<Target>(triple) {
303    this->UserLabelPrefix = "";
304    this->LongWidth = this->LongAlign = this->PointerWidth = this->PointerAlign = 32;
305    this->SizeType = TargetInfo::UnsignedInt;
306  }
307};
308
309// FIXME: Need a real SPU target.
310// PS3 SPU Target
311template<typename Target>
312class PS3SPUTargetInfo : public OSTargetInfo<Target> {
313protected:
314  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
315                            MacroBuilder &Builder) const {
316    // PS3 PPU defines.
317    Builder.defineMacro("__SPU__");
318    Builder.defineMacro("__ELF__");
319  }
320public:
321  PS3SPUTargetInfo(const std::string& triple)
322    : OSTargetInfo<Target>(triple) {
323    this->UserLabelPrefix = "";
324  }
325};
326
327// AuroraUX target
328template<typename Target>
329class AuroraUXTargetInfo : public OSTargetInfo<Target> {
330protected:
331  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
332                            MacroBuilder &Builder) const {
333    DefineStd(Builder, "sun", Opts);
334    DefineStd(Builder, "unix", Opts);
335    Builder.defineMacro("__ELF__");
336    Builder.defineMacro("__svr4__");
337    Builder.defineMacro("__SVR4");
338  }
339public:
340  AuroraUXTargetInfo(const std::string& triple)
341    : OSTargetInfo<Target>(triple) {
342    this->UserLabelPrefix = "";
343    this->WCharType = this->SignedLong;
344    // FIXME: WIntType should be SignedLong
345  }
346};
347
348// Solaris target
349template<typename Target>
350class SolarisTargetInfo : public OSTargetInfo<Target> {
351protected:
352  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
353                            MacroBuilder &Builder) const {
354    DefineStd(Builder, "sun", Opts);
355    DefineStd(Builder, "unix", Opts);
356    Builder.defineMacro("__ELF__");
357    Builder.defineMacro("__svr4__");
358    Builder.defineMacro("__SVR4");
359  }
360public:
361  SolarisTargetInfo(const std::string& triple)
362    : OSTargetInfo<Target>(triple) {
363    this->UserLabelPrefix = "";
364    this->WCharType = this->SignedLong;
365    // FIXME: WIntType should be SignedLong
366  }
367};
368} // end anonymous namespace.
369
370//===----------------------------------------------------------------------===//
371// Specific target implementations.
372//===----------------------------------------------------------------------===//
373
374namespace {
375// PPC abstract base class
376class PPCTargetInfo : public TargetInfo {
377  static const Builtin::Info BuiltinInfo[];
378  static const char * const GCCRegNames[];
379  static const TargetInfo::GCCRegAlias GCCRegAliases[];
380
381public:
382  PPCTargetInfo(const std::string& triple) : TargetInfo(triple) {}
383
384  virtual void getTargetBuiltins(const Builtin::Info *&Records,
385                                 unsigned &NumRecords) const {
386    Records = BuiltinInfo;
387    NumRecords = clang::PPC::LastTSBuiltin-Builtin::FirstTSBuiltin;
388  }
389
390  virtual void getTargetDefines(const LangOptions &Opts,
391                                MacroBuilder &Builder) const;
392
393  virtual const char *getVAListDeclaration() const {
394    return "typedef char* __builtin_va_list;";
395    // This is the right definition for ABI/V4: System V.4/eabi.
396    /*return "typedef struct __va_list_tag {"
397           "  unsigned char gpr;"
398           "  unsigned char fpr;"
399           "  unsigned short reserved;"
400           "  void* overflow_arg_area;"
401           "  void* reg_save_area;"
402           "} __builtin_va_list[1];";*/
403  }
404  virtual void getGCCRegNames(const char * const *&Names,
405                              unsigned &NumNames) const;
406  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
407                                unsigned &NumAliases) const;
408  virtual bool validateAsmConstraint(const char *&Name,
409                                     TargetInfo::ConstraintInfo &Info) const {
410    switch (*Name) {
411    default: return false;
412    case 'O': // Zero
413      return true;
414    case 'b': // Base register
415    case 'f': // Floating point register
416      Info.setAllowsRegister();
417      return true;
418    }
419  }
420  virtual const char *getClobbers() const {
421    return "";
422  }
423};
424
425const Builtin::Info PPCTargetInfo::BuiltinInfo[] = {
426#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false },
427#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false },
428#include "clang/Basic/BuiltinsPPC.def"
429};
430
431
432/// PPCTargetInfo::getTargetDefines - Return a set of the PowerPC-specific
433/// #defines that are not tied to a specific subtarget.
434void PPCTargetInfo::getTargetDefines(const LangOptions &Opts,
435                                     MacroBuilder &Builder) const {
436  // Target identification.
437  Builder.defineMacro("__ppc__");
438  Builder.defineMacro("_ARCH_PPC");
439  Builder.defineMacro("__powerpc__");
440  Builder.defineMacro("__POWERPC__");
441  if (PointerWidth == 64) {
442    Builder.defineMacro("_ARCH_PPC64");
443    Builder.defineMacro("_LP64");
444    Builder.defineMacro("__LP64__");
445    Builder.defineMacro("__powerpc64__");
446    Builder.defineMacro("__ppc64__");
447  } else {
448    Builder.defineMacro("__ppc__");
449  }
450
451  // Target properties.
452  Builder.defineMacro("_BIG_ENDIAN");
453  Builder.defineMacro("__BIG_ENDIAN__");
454
455  // Subtarget options.
456  Builder.defineMacro("__NATURAL_ALIGNMENT__");
457  Builder.defineMacro("__REGISTER_PREFIX__", "");
458
459  // FIXME: Should be controlled by command line option.
460  Builder.defineMacro("__LONG_DOUBLE_128__");
461
462  if (Opts.AltiVec) {
463    Builder.defineMacro("__VEC__", "10206");
464    Builder.defineMacro("__ALTIVEC__");
465  }
466}
467
468
469const char * const PPCTargetInfo::GCCRegNames[] = {
470  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
471  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
472  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
473  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
474  "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
475  "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
476  "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
477  "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
478  "mq", "lr", "ctr", "ap",
479  "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
480  "xer",
481  "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
482  "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
483  "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
484  "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
485  "vrsave", "vscr",
486  "spe_acc", "spefscr",
487  "sfp"
488};
489
490void PPCTargetInfo::getGCCRegNames(const char * const *&Names,
491                                   unsigned &NumNames) const {
492  Names = GCCRegNames;
493  NumNames = llvm::array_lengthof(GCCRegNames);
494}
495
496const TargetInfo::GCCRegAlias PPCTargetInfo::GCCRegAliases[] = {
497  // While some of these aliases do map to different registers
498  // they still share the same register name.
499  { { "0" }, "r0" },
500  { { "1"}, "r1" },
501  { { "2" }, "r2" },
502  { { "3" }, "r3" },
503  { { "4" }, "r4" },
504  { { "5" }, "r5" },
505  { { "6" }, "r6" },
506  { { "7" }, "r7" },
507  { { "8" }, "r8" },
508  { { "9" }, "r9" },
509  { { "10" }, "r10" },
510  { { "11" }, "r11" },
511  { { "12" }, "r12" },
512  { { "13" }, "r13" },
513  { { "14" }, "r14" },
514  { { "15" }, "r15" },
515  { { "16" }, "r16" },
516  { { "17" }, "r17" },
517  { { "18" }, "r18" },
518  { { "19" }, "r19" },
519  { { "20" }, "r20" },
520  { { "21" }, "r21" },
521  { { "22" }, "r22" },
522  { { "23" }, "r23" },
523  { { "24" }, "r24" },
524  { { "25" }, "r25" },
525  { { "26" }, "r26" },
526  { { "27" }, "r27" },
527  { { "28" }, "r28" },
528  { { "29" }, "r29" },
529  { { "30" }, "r30" },
530  { { "31" }, "r31" },
531  { { "fr0" }, "f0" },
532  { { "fr1" }, "f1" },
533  { { "fr2" }, "f2" },
534  { { "fr3" }, "f3" },
535  { { "fr4" }, "f4" },
536  { { "fr5" }, "f5" },
537  { { "fr6" }, "f6" },
538  { { "fr7" }, "f7" },
539  { { "fr8" }, "f8" },
540  { { "fr9" }, "f9" },
541  { { "fr10" }, "f10" },
542  { { "fr11" }, "f11" },
543  { { "fr12" }, "f12" },
544  { { "fr13" }, "f13" },
545  { { "fr14" }, "f14" },
546  { { "fr15" }, "f15" },
547  { { "fr16" }, "f16" },
548  { { "fr17" }, "f17" },
549  { { "fr18" }, "f18" },
550  { { "fr19" }, "f19" },
551  { { "fr20" }, "f20" },
552  { { "fr21" }, "f21" },
553  { { "fr22" }, "f22" },
554  { { "fr23" }, "f23" },
555  { { "fr24" }, "f24" },
556  { { "fr25" }, "f25" },
557  { { "fr26" }, "f26" },
558  { { "fr27" }, "f27" },
559  { { "fr28" }, "f28" },
560  { { "fr29" }, "f29" },
561  { { "fr30" }, "f30" },
562  { { "fr31" }, "f31" },
563  { { "cc" }, "cr0" },
564};
565
566void PPCTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
567                                     unsigned &NumAliases) const {
568  Aliases = GCCRegAliases;
569  NumAliases = llvm::array_lengthof(GCCRegAliases);
570}
571} // end anonymous namespace.
572
573namespace {
574class PPC32TargetInfo : public PPCTargetInfo {
575public:
576  PPC32TargetInfo(const std::string &triple) : PPCTargetInfo(triple) {
577    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
578                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32";
579
580    if (getTriple().getOS() == llvm::Triple::FreeBSD)
581        this->SizeType = TargetInfo::UnsignedInt;
582  }
583};
584} // end anonymous namespace.
585
586namespace {
587class PPC64TargetInfo : public PPCTargetInfo {
588public:
589  PPC64TargetInfo(const std::string& triple) : PPCTargetInfo(triple) {
590    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
591    IntMaxType = SignedLong;
592    UIntMaxType = UnsignedLong;
593    Int64Type = SignedLong;
594    DescriptionString = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
595                        "i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32:64";
596  }
597};
598} // end anonymous namespace.
599
600namespace {
601// Namespace for x86 abstract base class
602const Builtin::Info BuiltinInfo[] = {
603#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false },
604#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false },
605#include "clang/Basic/BuiltinsX86.def"
606};
607
608static const char* const GCCRegNames[] = {
609  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
610  "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
611  "argp", "flags", "fspr", "dirflag", "frame",
612  "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
613  "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
614  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
615  "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
616};
617
618const TargetInfo::GCCRegAlias GCCRegAliases[] = {
619  { { "al", "ah", "eax", "rax" }, "ax" },
620  { { "bl", "bh", "ebx", "rbx" }, "bx" },
621  { { "cl", "ch", "ecx", "rcx" }, "cx" },
622  { { "dl", "dh", "edx", "rdx" }, "dx" },
623  { { "esi", "rsi" }, "si" },
624  { { "edi", "rdi" }, "di" },
625  { { "esp", "rsp" }, "sp" },
626  { { "ebp", "rbp" }, "bp" },
627};
628
629// X86 target abstract base class; x86-32 and x86-64 are very close, so
630// most of the implementation can be shared.
631class X86TargetInfo : public TargetInfo {
632  enum X86SSEEnum {
633    NoMMXSSE, MMX, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42
634  } SSELevel;
635  enum AMD3DNowEnum {
636    NoAMD3DNow, AMD3DNow, AMD3DNowAthlon
637  } AMD3DNowLevel;
638
639public:
640  X86TargetInfo(const std::string& triple)
641    : TargetInfo(triple), SSELevel(NoMMXSSE), AMD3DNowLevel(NoAMD3DNow) {
642    LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
643  }
644  virtual void getTargetBuiltins(const Builtin::Info *&Records,
645                                 unsigned &NumRecords) const {
646    Records = BuiltinInfo;
647    NumRecords = clang::X86::LastTSBuiltin-Builtin::FirstTSBuiltin;
648  }
649  virtual void getGCCRegNames(const char * const *&Names,
650                              unsigned &NumNames) const {
651    Names = GCCRegNames;
652    NumNames = llvm::array_lengthof(GCCRegNames);
653  }
654  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
655                                unsigned &NumAliases) const {
656    Aliases = GCCRegAliases;
657    NumAliases = llvm::array_lengthof(GCCRegAliases);
658  }
659  virtual bool validateAsmConstraint(const char *&Name,
660                                     TargetInfo::ConstraintInfo &info) const;
661  virtual std::string convertConstraint(const char Constraint) const;
662  virtual const char *getClobbers() const {
663    return "~{dirflag},~{fpsr},~{flags}";
664  }
665  virtual void getTargetDefines(const LangOptions &Opts,
666                                MacroBuilder &Builder) const;
667  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
668                                 const std::string &Name,
669                                 bool Enabled) const;
670  virtual void getDefaultFeatures(const std::string &CPU,
671                                  llvm::StringMap<bool> &Features) const;
672  virtual void HandleTargetFeatures(std::vector<std::string> &Features);
673};
674
675void X86TargetInfo::getDefaultFeatures(const std::string &CPU,
676                                       llvm::StringMap<bool> &Features) const {
677  // FIXME: This should not be here.
678  Features["3dnow"] = false;
679  Features["3dnowa"] = false;
680  Features["mmx"] = false;
681  Features["sse"] = false;
682  Features["sse2"] = false;
683  Features["sse3"] = false;
684  Features["ssse3"] = false;
685  Features["sse41"] = false;
686  Features["sse42"] = false;
687
688  // LLVM does not currently recognize this.
689  // Features["sse4a"] = false;
690
691  // FIXME: This *really* should not be here.
692
693  // X86_64 always has SSE2.
694  if (PointerWidth == 64)
695    Features["sse2"] = Features["sse"] = Features["mmx"] = true;
696
697  if (CPU == "generic" || CPU == "i386" || CPU == "i486" || CPU == "i586" ||
698      CPU == "pentium" || CPU == "i686" || CPU == "pentiumpro")
699    ;
700  else if (CPU == "pentium-mmx" || CPU == "pentium2")
701    setFeatureEnabled(Features, "mmx", true);
702  else if (CPU == "pentium3")
703    setFeatureEnabled(Features, "sse", true);
704  else if (CPU == "pentium-m" || CPU == "pentium4" || CPU == "x86-64")
705    setFeatureEnabled(Features, "sse2", true);
706  else if (CPU == "yonah" || CPU == "prescott" || CPU == "nocona")
707    setFeatureEnabled(Features, "sse3", true);
708  else if (CPU == "core2")
709    setFeatureEnabled(Features, "ssse3", true);
710  else if (CPU == "penryn") {
711    setFeatureEnabled(Features, "sse4", true);
712    Features["sse42"] = false;
713  } else if (CPU == "atom")
714    setFeatureEnabled(Features, "sse3", true);
715  else if (CPU == "corei7")
716    setFeatureEnabled(Features, "sse4", true);
717  else if (CPU == "k6" || CPU == "winchip-c6")
718    setFeatureEnabled(Features, "mmx", true);
719  else if (CPU == "k6-2" || CPU == "k6-3" || CPU == "athlon" ||
720           CPU == "athlon-tbird" || CPU == "winchip2" || CPU == "c3") {
721    setFeatureEnabled(Features, "mmx", true);
722    setFeatureEnabled(Features, "3dnow", true);
723  } else if (CPU == "athlon-4" || CPU == "athlon-xp" || CPU == "athlon-mp") {
724    setFeatureEnabled(Features, "sse", true);
725    setFeatureEnabled(Features, "3dnowa", true);
726  } else if (CPU == "k8" || CPU == "opteron" || CPU == "athlon64" ||
727           CPU == "athlon-fx") {
728    setFeatureEnabled(Features, "sse2", true);
729    setFeatureEnabled(Features, "3dnowa", true);
730  } else if (CPU == "c3-2")
731    setFeatureEnabled(Features, "sse", true);
732}
733
734bool X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
735                                      const std::string &Name,
736                                      bool Enabled) const {
737  // FIXME: This *really* should not be here.
738  if (!Features.count(Name) && Name != "sse4")
739    return false;
740
741  if (Enabled) {
742    if (Name == "mmx")
743      Features["mmx"] = true;
744    else if (Name == "sse")
745      Features["mmx"] = Features["sse"] = true;
746    else if (Name == "sse2")
747      Features["mmx"] = Features["sse"] = Features["sse2"] = true;
748    else if (Name == "sse3")
749      Features["mmx"] = Features["sse"] = Features["sse2"] =
750        Features["sse3"] = true;
751    else if (Name == "ssse3")
752      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
753        Features["ssse3"] = true;
754    else if (Name == "sse4")
755      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
756        Features["ssse3"] = Features["sse41"] = Features["sse42"] = true;
757    else if (Name == "3dnow")
758      Features["3dnowa"] = true;
759    else if (Name == "3dnowa")
760      Features["3dnow"] = Features["3dnowa"] = true;
761  } else {
762    if (Name == "mmx")
763      Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
764        Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
765    else if (Name == "sse")
766      Features["sse"] = Features["sse2"] = Features["sse3"] =
767        Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
768    else if (Name == "sse2")
769      Features["sse2"] = Features["sse3"] = Features["ssse3"] =
770        Features["sse41"] = Features["sse42"] = false;
771    else if (Name == "sse3")
772      Features["sse3"] = Features["ssse3"] = Features["sse41"] =
773        Features["sse42"] = false;
774    else if (Name == "ssse3")
775      Features["ssse3"] = Features["sse41"] = Features["sse42"] = false;
776    else if (Name == "sse4")
777      Features["sse41"] = Features["sse42"] = false;
778    else if (Name == "3dnow")
779      Features["3dnow"] = Features["3dnowa"] = false;
780    else if (Name == "3dnowa")
781      Features["3dnowa"] = false;
782  }
783
784  return true;
785}
786
787/// HandleTargetOptions - Perform initialization based on the user
788/// configured set of features.
789void X86TargetInfo::HandleTargetFeatures(std::vector<std::string> &Features) {
790  // Remember the maximum enabled sselevel.
791  for (unsigned i = 0, e = Features.size(); i !=e; ++i) {
792    // Ignore disabled features.
793    if (Features[i][0] == '-')
794      continue;
795
796    assert(Features[i][0] == '+' && "Invalid target feature!");
797    X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Features[i].substr(1))
798      .Case("sse42", SSE42)
799      .Case("sse41", SSE41)
800      .Case("ssse3", SSSE3)
801      .Case("sse2", SSE2)
802      .Case("sse", SSE1)
803      .Case("mmx", MMX)
804      .Default(NoMMXSSE);
805    SSELevel = std::max(SSELevel, Level);
806
807    AMD3DNowEnum ThreeDNowLevel =
808      llvm::StringSwitch<AMD3DNowEnum>(Features[i].substr(1))
809        .Case("3dnowa", AMD3DNowAthlon)
810        .Case("3dnow", AMD3DNow)
811        .Default(NoAMD3DNow);
812
813    AMD3DNowLevel = std::max(AMD3DNowLevel, ThreeDNowLevel);
814  }
815}
816
817/// X86TargetInfo::getTargetDefines - Return a set of the X86-specific #defines
818/// that are not tied to a specific subtarget.
819void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
820                                     MacroBuilder &Builder) const {
821  // Target identification.
822  if (PointerWidth == 64) {
823    Builder.defineMacro("_LP64");
824    Builder.defineMacro("__LP64__");
825    Builder.defineMacro("__amd64__");
826    Builder.defineMacro("__amd64");
827    Builder.defineMacro("__x86_64");
828    Builder.defineMacro("__x86_64__");
829  } else {
830    DefineStd(Builder, "i386", Opts);
831  }
832
833  // Target properties.
834  Builder.defineMacro("__LITTLE_ENDIAN__");
835
836  // Subtarget options.
837  Builder.defineMacro("__nocona");
838  Builder.defineMacro("__nocona__");
839  Builder.defineMacro("__tune_nocona__");
840  Builder.defineMacro("__REGISTER_PREFIX__", "");
841
842  // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
843  // functions in glibc header files that use FP Stack inline asm which the
844  // backend can't deal with (PR879).
845  Builder.defineMacro("__NO_MATH_INLINES");
846
847  // Each case falls through to the previous one here.
848  switch (SSELevel) {
849  case SSE42:
850    Builder.defineMacro("__SSE4_2__");
851  case SSE41:
852    Builder.defineMacro("__SSE4_1__");
853  case SSSE3:
854    Builder.defineMacro("__SSSE3__");
855  case SSE3:
856    Builder.defineMacro("__SSE3__");
857  case SSE2:
858    Builder.defineMacro("__SSE2__");
859    Builder.defineMacro("__SSE2_MATH__");  // -mfp-math=sse always implied.
860  case SSE1:
861    Builder.defineMacro("__SSE__");
862    Builder.defineMacro("__SSE_MATH__");   // -mfp-math=sse always implied.
863  case MMX:
864    Builder.defineMacro("__MMX__");
865  case NoMMXSSE:
866    break;
867  }
868
869  // Each case falls through to the previous one here.
870  switch (AMD3DNowLevel) {
871  case AMD3DNowAthlon:
872    Builder.defineMacro("__3dNOW_A__");
873  case AMD3DNow:
874    Builder.defineMacro("__3dNOW__");
875  case NoAMD3DNow:
876    break;
877  }
878}
879
880
881bool
882X86TargetInfo::validateAsmConstraint(const char *&Name,
883                                     TargetInfo::ConstraintInfo &Info) const {
884  switch (*Name) {
885  default: return false;
886  case 'a': // eax.
887  case 'b': // ebx.
888  case 'c': // ecx.
889  case 'd': // edx.
890  case 'S': // esi.
891  case 'D': // edi.
892  case 'A': // edx:eax.
893  case 't': // top of floating point stack.
894  case 'u': // second from top of floating point stack.
895  case 'q': // Any register accessible as [r]l: a, b, c, and d.
896  case 'y': // Any MMX register.
897  case 'x': // Any SSE register.
898  case 'Q': // Any register accessible as [r]h: a, b, c, and d.
899  case 'e': // 32-bit signed integer constant for use with zero-extending
900            // x86_64 instructions.
901  case 'Z': // 32-bit unsigned integer constant for use with zero-extending
902            // x86_64 instructions.
903  case 'N': // unsigned 8-bit integer constant for use with in and out
904            // instructions.
905  case 'R': // "legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
906    Info.setAllowsRegister();
907    return true;
908  }
909}
910
911std::string
912X86TargetInfo::convertConstraint(const char Constraint) const {
913  switch (Constraint) {
914  case 'a': return std::string("{ax}");
915  case 'b': return std::string("{bx}");
916  case 'c': return std::string("{cx}");
917  case 'd': return std::string("{dx}");
918  case 'S': return std::string("{si}");
919  case 'D': return std::string("{di}");
920  case 't': // top of floating point stack.
921    return std::string("{st}");
922  case 'u': // second from top of floating point stack.
923    return std::string("{st(1)}"); // second from top of floating point stack.
924  default:
925    return std::string(1, Constraint);
926  }
927}
928} // end anonymous namespace
929
930namespace {
931// X86-32 generic target
932class X86_32TargetInfo : public X86TargetInfo {
933public:
934  X86_32TargetInfo(const std::string& triple) : X86TargetInfo(triple) {
935    DoubleAlign = LongLongAlign = 32;
936    LongDoubleWidth = 96;
937    LongDoubleAlign = 32;
938    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
939                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
940                        "a0:0:64-f80:32:32-n8:16:32";
941    SizeType = UnsignedInt;
942    PtrDiffType = SignedInt;
943    IntPtrType = SignedInt;
944    RegParmMax = 3;
945  }
946  virtual const char *getVAListDeclaration() const {
947    return "typedef char* __builtin_va_list;";
948  }
949
950  int getEHDataRegisterNumber(unsigned RegNo) const {
951    if (RegNo == 0) return 0;
952    if (RegNo == 1) return 2;
953    return -1;
954  }
955};
956} // end anonymous namespace
957
958namespace {
959class OpenBSDI386TargetInfo : public OpenBSDTargetInfo<X86_32TargetInfo> {
960public:
961  OpenBSDI386TargetInfo(const std::string& triple) :
962    OpenBSDTargetInfo<X86_32TargetInfo>(triple) {
963    SizeType = UnsignedLong;
964    IntPtrType = SignedLong;
965    PtrDiffType = SignedLong;
966  }
967};
968} // end anonymous namespace
969
970namespace {
971class DarwinI386TargetInfo : public DarwinTargetInfo<X86_32TargetInfo> {
972public:
973  DarwinI386TargetInfo(const std::string& triple) :
974    DarwinTargetInfo<X86_32TargetInfo>(triple) {
975    LongDoubleWidth = 128;
976    LongDoubleAlign = 128;
977    SizeType = UnsignedLong;
978    IntPtrType = SignedLong;
979    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
980                        "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-"
981                        "a0:0:64-f80:128:128-n8:16:32";
982  }
983
984};
985} // end anonymous namespace
986
987namespace {
988// x86-32 Windows target
989class WindowsX86_32TargetInfo : public X86_32TargetInfo {
990public:
991  WindowsX86_32TargetInfo(const std::string& triple)
992    : X86_32TargetInfo(triple) {
993    TLSSupported = false;
994    WCharType = UnsignedShort;
995    DoubleAlign = LongLongAlign = 64;
996    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
997                        "i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-"
998                        "v128:128:128-a0:0:64-f80:32:32-n8:16:32";
999  }
1000  virtual void getTargetDefines(const LangOptions &Opts,
1001                                MacroBuilder &Builder) const {
1002    X86_32TargetInfo::getTargetDefines(Opts, Builder);
1003    // This list is based off of the the list of things MingW defines
1004    Builder.defineMacro("_WIN32");
1005    DefineStd(Builder, "WIN32", Opts);
1006    DefineStd(Builder, "WINNT", Opts);
1007    Builder.defineMacro("_X86_");
1008  }
1009};
1010} // end anonymous namespace
1011
1012namespace {
1013
1014// x86-32 Windows Visual Studio target
1015class VisualStudioWindowsX86_32TargetInfo : public WindowsX86_32TargetInfo {
1016public:
1017  VisualStudioWindowsX86_32TargetInfo(const std::string& triple)
1018    : WindowsX86_32TargetInfo(triple) {
1019  }
1020  virtual void getTargetDefines(const LangOptions &Opts,
1021                                MacroBuilder &Builder) const {
1022    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
1023    // The value of the following reflects processor type.
1024    // 300=386, 400=486, 500=Pentium, 600=Blend (default)
1025    // We lost the original triple, so we use the default.
1026    Builder.defineMacro("_M_IX86", "600");
1027  }
1028};
1029} // end anonymous namespace
1030
1031namespace {
1032// x86-32 MinGW target
1033class MinGWX86_32TargetInfo : public WindowsX86_32TargetInfo {
1034public:
1035  MinGWX86_32TargetInfo(const std::string& triple)
1036    : WindowsX86_32TargetInfo(triple) {
1037  }
1038  virtual void getTargetDefines(const LangOptions &Opts,
1039                                MacroBuilder &Builder) const {
1040    WindowsX86_32TargetInfo::getTargetDefines(Opts, Builder);
1041    Builder.defineMacro("__MSVCRT__");
1042    Builder.defineMacro("__MINGW32__");
1043    Builder.defineMacro("__declspec", "__declspec");
1044  }
1045};
1046} // end anonymous namespace
1047
1048namespace {
1049// x86-32 Cygwin target
1050class CygwinX86_32TargetInfo : public X86_32TargetInfo {
1051public:
1052  CygwinX86_32TargetInfo(const std::string& triple)
1053    : X86_32TargetInfo(triple) {
1054    TLSSupported = false;
1055    WCharType = UnsignedShort;
1056    DoubleAlign = LongLongAlign = 64;
1057    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1058                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
1059                        "a0:0:64-f80:32:32-n8:16:32";
1060  }
1061  virtual void getTargetDefines(const LangOptions &Opts,
1062                                MacroBuilder &Builder) const {
1063    X86_32TargetInfo::getTargetDefines(Opts, Builder);
1064    Builder.defineMacro("__CYGWIN__");
1065    Builder.defineMacro("__CYGWIN32__");
1066    DefineStd(Builder, "unix", Opts);
1067  }
1068};
1069} // end anonymous namespace
1070
1071namespace {
1072// x86-64 generic target
1073class X86_64TargetInfo : public X86TargetInfo {
1074public:
1075  X86_64TargetInfo(const std::string &triple) : X86TargetInfo(triple) {
1076    LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
1077    LongDoubleWidth = 128;
1078    LongDoubleAlign = 128;
1079    IntMaxType = SignedLong;
1080    UIntMaxType = UnsignedLong;
1081    Int64Type = SignedLong;
1082    RegParmMax = 6;
1083
1084    DescriptionString = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1085                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
1086                        "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64";
1087  }
1088  virtual const char *getVAListDeclaration() const {
1089    return "typedef struct __va_list_tag {"
1090           "  unsigned gp_offset;"
1091           "  unsigned fp_offset;"
1092           "  void* overflow_arg_area;"
1093           "  void* reg_save_area;"
1094           "} __va_list_tag;"
1095           "typedef __va_list_tag __builtin_va_list[1];";
1096  }
1097
1098  int getEHDataRegisterNumber(unsigned RegNo) const {
1099    if (RegNo == 0) return 0;
1100    if (RegNo == 1) return 1;
1101    return -1;
1102  }
1103};
1104} // end anonymous namespace
1105
1106namespace {
1107// x86-64 Windows target
1108class WindowsX86_64TargetInfo : public X86_64TargetInfo {
1109public:
1110  WindowsX86_64TargetInfo(const std::string& triple)
1111    : X86_64TargetInfo(triple) {
1112    TLSSupported = false;
1113    WCharType = UnsignedShort;
1114    LongWidth = LongAlign = 32;
1115    DoubleAlign = LongLongAlign = 64;
1116  }
1117  virtual void getTargetDefines(const LangOptions &Opts,
1118                                MacroBuilder &Builder) const {
1119    X86_64TargetInfo::getTargetDefines(Opts, Builder);
1120    Builder.defineMacro("_WIN64");
1121    DefineStd(Builder, "WIN64", Opts);
1122  }
1123};
1124} // end anonymous namespace
1125
1126namespace {
1127// x86-64 Windows Visual Studio target
1128class VisualStudioWindowsX86_64TargetInfo : public WindowsX86_64TargetInfo {
1129public:
1130  VisualStudioWindowsX86_64TargetInfo(const std::string& triple)
1131    : WindowsX86_64TargetInfo(triple) {
1132  }
1133  virtual void getTargetDefines(const LangOptions &Opts,
1134                                MacroBuilder &Builder) const {
1135    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
1136    Builder.defineMacro("_M_X64");
1137  }
1138  virtual const char *getVAListDeclaration() const {
1139    return "typedef char* va_list;";
1140  }
1141};
1142} // end anonymous namespace
1143
1144namespace {
1145// x86-64 MinGW target
1146class MinGWX86_64TargetInfo : public WindowsX86_64TargetInfo {
1147public:
1148  MinGWX86_64TargetInfo(const std::string& triple)
1149    : WindowsX86_64TargetInfo(triple) {
1150  }
1151  virtual void getTargetDefines(const LangOptions &Opts,
1152                                MacroBuilder &Builder) const {
1153    WindowsX86_64TargetInfo::getTargetDefines(Opts, Builder);
1154    Builder.defineMacro("__MSVCRT__");
1155    Builder.defineMacro("__MINGW64__");
1156    Builder.defineMacro("__declspec");
1157  }
1158};
1159} // end anonymous namespace
1160
1161namespace {
1162class DarwinX86_64TargetInfo : public DarwinTargetInfo<X86_64TargetInfo> {
1163public:
1164  DarwinX86_64TargetInfo(const std::string& triple)
1165      : DarwinTargetInfo<X86_64TargetInfo>(triple) {
1166    Int64Type = SignedLongLong;
1167  }
1168};
1169} // end anonymous namespace
1170
1171namespace {
1172class OpenBSDX86_64TargetInfo : public OpenBSDTargetInfo<X86_64TargetInfo> {
1173public:
1174  OpenBSDX86_64TargetInfo(const std::string& triple)
1175      : OpenBSDTargetInfo<X86_64TargetInfo>(triple) {
1176    IntMaxType = SignedLongLong;
1177    UIntMaxType = UnsignedLongLong;
1178    Int64Type = SignedLongLong;
1179  }
1180};
1181} // end anonymous namespace
1182
1183namespace {
1184class ARMTargetInfo : public TargetInfo {
1185  // Possible FPU choices.
1186  enum FPUMode {
1187    NoFPU,
1188    VFP2FPU,
1189    VFP3FPU,
1190    NeonFPU
1191  };
1192
1193  static bool FPUModeIsVFP(FPUMode Mode) {
1194    return Mode >= VFP2FPU && Mode <= NeonFPU;
1195  }
1196
1197  static const TargetInfo::GCCRegAlias GCCRegAliases[];
1198  static const char * const GCCRegNames[];
1199
1200  std::string ABI, CPU;
1201
1202  unsigned FPU : 3;
1203
1204  unsigned IsThumb : 1;
1205
1206  // Initialized via features.
1207  unsigned SoftFloat : 1;
1208  unsigned SoftFloatABI : 1;
1209
1210public:
1211  ARMTargetInfo(const std::string &TripleStr)
1212    : TargetInfo(TripleStr), ABI("aapcs-linux"), CPU("arm1136j-s")
1213  {
1214    SizeType = UnsignedInt;
1215    PtrDiffType = SignedInt;
1216
1217    // FIXME: Should we just treat this as a feature?
1218    IsThumb = getTriple().getArchName().startswith("thumb");
1219    if (IsThumb) {
1220      DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
1221                           "i64:64:64-f32:32:32-f64:64:64-"
1222                           "v64:64:64-v128:128:128-a0:0:32-n32");
1223    } else {
1224      DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1225                           "i64:64:64-f32:32:32-f64:64:64-"
1226                           "v64:64:64-v128:128:128-a0:0:64-n32");
1227    }
1228  }
1229  virtual const char *getABI() const { return ABI.c_str(); }
1230  virtual bool setABI(const std::string &Name) {
1231    ABI = Name;
1232
1233    // The defaults (above) are for AAPCS, check if we need to change them.
1234    //
1235    // FIXME: We need support for -meabi... we could just mangle it into the
1236    // name.
1237    if (Name == "apcs-gnu") {
1238      DoubleAlign = LongLongAlign = LongDoubleAlign = 32;
1239      SizeType = UnsignedLong;
1240
1241      if (IsThumb) {
1242        DescriptionString = ("e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
1243                             "i64:32:32-f32:32:32-f64:32:32-"
1244                             "v64:64:64-v128:128:128-a0:0:32-n32");
1245      } else {
1246        DescriptionString = ("e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1247                             "i64:32:32-f32:32:32-f64:32:32-"
1248                             "v64:64:64-v128:128:128-a0:0:64-n32");
1249      }
1250
1251      // FIXME: Override "preferred align" for double and long long.
1252    } else if (Name == "aapcs") {
1253      // FIXME: Enumerated types are variable width in straight AAPCS.
1254    } else if (Name == "aapcs-linux") {
1255      ;
1256    } else
1257      return false;
1258
1259    return true;
1260  }
1261
1262  void getDefaultFeatures(const std::string &CPU,
1263                          llvm::StringMap<bool> &Features) const {
1264    // FIXME: This should not be here.
1265    Features["vfp2"] = false;
1266    Features["vfp3"] = false;
1267    Features["neon"] = false;
1268
1269    if (CPU == "arm1136jf-s" || CPU == "arm1176jzf-s" || CPU == "mpcore")
1270      Features["vfp2"] = true;
1271    else if (CPU == "cortex-a8" || CPU == "cortex-a9")
1272      Features["neon"] = true;
1273  }
1274
1275  virtual bool setFeatureEnabled(llvm::StringMap<bool> &Features,
1276                                 const std::string &Name,
1277                                 bool Enabled) const {
1278    if (Name == "soft-float" || Name == "soft-float-abi") {
1279      Features[Name] = Enabled;
1280    } else if (Name == "vfp2" || Name == "vfp3" || Name == "neon") {
1281      // These effectively are a single option, reset them when any is enabled.
1282      if (Enabled)
1283        Features["vfp2"] = Features["vfp3"] = Features["neon"] = false;
1284      Features[Name] = Enabled;
1285    } else
1286      return false;
1287
1288    return true;
1289  }
1290
1291  virtual void HandleTargetFeatures(std::vector<std::string> &Features) {
1292    FPU = NoFPU;
1293    SoftFloat = SoftFloatABI = false;
1294    for (unsigned i = 0, e = Features.size(); i != e; ++i) {
1295      if (Features[i] == "+soft-float")
1296        SoftFloat = true;
1297      else if (Features[i] == "+soft-float-abi")
1298        SoftFloatABI = true;
1299      else if (Features[i] == "+vfp2")
1300        FPU = VFP2FPU;
1301      else if (Features[i] == "+vfp3")
1302        FPU = VFP3FPU;
1303      else if (Features[i] == "+neon")
1304        FPU = NeonFPU;
1305    }
1306
1307    // Remove front-end specific options which the backend handles differently.
1308    std::vector<std::string>::iterator it;
1309    it = std::find(Features.begin(), Features.end(), "+soft-float");
1310    if (it != Features.end())
1311      Features.erase(it);
1312    it = std::find(Features.begin(), Features.end(), "+soft-float-abi");
1313    if (it != Features.end())
1314      Features.erase(it);
1315  }
1316
1317  static const char *getCPUDefineSuffix(llvm::StringRef Name) {
1318    return llvm::StringSwitch<const char*>(Name)
1319      .Cases("arm8", "arm810", "4")
1320      .Cases("strongarm", "strongarm110", "strongarm1100", "strongarm1110", "4")
1321      .Cases("arm7tdmi", "arm7tdmi-s", "arm710t", "arm720t", "arm9", "4T")
1322      .Cases("arm9tdmi", "arm920", "arm920t", "arm922t", "arm940t", "4T")
1323      .Case("ep9312", "4T")
1324      .Cases("arm10tdmi", "arm1020t", "5T")
1325      .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "5TE")
1326      .Case("arm926ej-s", "5TEJ")
1327      .Cases("arm10e", "arm1020e", "arm1022e", "5TE")
1328      .Cases("xscale", "iwmmxt", "5TE")
1329      .Case("arm1136j-s", "6J")
1330      .Cases("arm1176jz-s", "arm1176jzf-s", "6ZK")
1331      .Cases("arm1136jf-s", "mpcorenovfp", "mpcore", "6K")
1332      .Cases("arm1156t2-s", "arm1156t2f-s", "6T2")
1333      .Cases("cortex-a8", "cortex-a9", "7A")
1334      .Default(0);
1335  }
1336  virtual bool setCPU(const std::string &Name) {
1337    if (!getCPUDefineSuffix(Name))
1338      return false;
1339
1340    CPU = Name;
1341    return true;
1342  }
1343  virtual void getTargetDefines(const LangOptions &Opts,
1344                                MacroBuilder &Builder) const {
1345    // Target identification.
1346    Builder.defineMacro("__arm");
1347    Builder.defineMacro("__arm__");
1348
1349    // Target properties.
1350    Builder.defineMacro("__ARMEL__");
1351    Builder.defineMacro("__LITTLE_ENDIAN__");
1352    Builder.defineMacro("__REGISTER_PREFIX__", "");
1353
1354    llvm::StringRef CPUArch = getCPUDefineSuffix(CPU);
1355    Builder.defineMacro("__ARM_ARCH_" + CPUArch + "__");
1356
1357    // Subtarget options.
1358
1359    // FIXME: It's more complicated than this and we don't really support
1360    // interworking.
1361    if ('5' <= CPUArch[0] && CPUArch[0] <= '7')
1362      Builder.defineMacro("__THUMB_INTERWORK__");
1363
1364    if (ABI == "aapcs" || ABI == "aapcs-linux")
1365      Builder.defineMacro("__ARM_EABI__");
1366
1367    if (SoftFloat)
1368      Builder.defineMacro("__SOFTFP__");
1369
1370    if (CPU == "xscale")
1371      Builder.defineMacro("__XSCALE__");
1372
1373    bool IsThumb2 = IsThumb && (CPUArch == "6T2" || CPUArch.startswith("7"));
1374    if (IsThumb) {
1375      Builder.defineMacro("__THUMBEL__");
1376      Builder.defineMacro("__thumb__");
1377      if (IsThumb2)
1378        Builder.defineMacro("__thumb2__");
1379    }
1380
1381    // Note, this is always on in gcc, even though it doesn't make sense.
1382    Builder.defineMacro("__APCS_32__");
1383
1384    if (FPUModeIsVFP((FPUMode) FPU))
1385      Builder.defineMacro("__VFP_FP__");
1386
1387    // This only gets set when Neon instructions are actually available, unlike
1388    // the VFP define, hence the soft float and arch check. This is subtly
1389    // different from gcc, we follow the intent which was that it should be set
1390    // when Neon instructions are actually available.
1391    if (FPU == NeonFPU && !SoftFloat && IsThumb2)
1392      Builder.defineMacro("__ARM_NEON__");
1393  }
1394  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1395                                 unsigned &NumRecords) const {
1396    // FIXME: Implement.
1397    Records = 0;
1398    NumRecords = 0;
1399  }
1400  virtual const char *getVAListDeclaration() const {
1401    return "typedef char* __builtin_va_list;";
1402  }
1403  virtual void getGCCRegNames(const char * const *&Names,
1404                              unsigned &NumNames) const;
1405  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1406                                unsigned &NumAliases) const;
1407  virtual bool validateAsmConstraint(const char *&Name,
1408                                     TargetInfo::ConstraintInfo &Info) const {
1409    // FIXME: Check if this is complete
1410    switch (*Name) {
1411    default:
1412    case 'l': // r0-r7
1413    case 'h': // r8-r15
1414    case 'w': // VFP Floating point register single precision
1415    case 'P': // VFP Floating point register double precision
1416      Info.setAllowsRegister();
1417      return true;
1418    }
1419    return false;
1420  }
1421  virtual const char *getClobbers() const {
1422    // FIXME: Is this really right?
1423    return "";
1424  }
1425};
1426
1427const char * const ARMTargetInfo::GCCRegNames[] = {
1428  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1429  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1430};
1431
1432void ARMTargetInfo::getGCCRegNames(const char * const *&Names,
1433                                       unsigned &NumNames) const {
1434  Names = GCCRegNames;
1435  NumNames = llvm::array_lengthof(GCCRegNames);
1436}
1437
1438const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
1439
1440  { { "a1" }, "r0" },
1441  { { "a2" }, "r1" },
1442  { { "a3" }, "r2" },
1443  { { "a4" }, "r3" },
1444  { { "v1" }, "r4" },
1445  { { "v2" }, "r5" },
1446  { { "v3" }, "r6" },
1447  { { "v4" }, "r7" },
1448  { { "v5" }, "r8" },
1449  { { "v6", "rfp" }, "r9" },
1450  { { "sl" }, "r10" },
1451  { { "fp" }, "r11" },
1452  { { "ip" }, "r12" },
1453  { { "sp" }, "r13" },
1454  { { "lr" }, "r14" },
1455  { { "pc" }, "r15" },
1456};
1457
1458void ARMTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1459                                       unsigned &NumAliases) const {
1460  Aliases = GCCRegAliases;
1461  NumAliases = llvm::array_lengthof(GCCRegAliases);
1462}
1463} // end anonymous namespace.
1464
1465
1466namespace {
1467class DarwinARMTargetInfo :
1468  public DarwinTargetInfo<ARMTargetInfo> {
1469protected:
1470  virtual void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple,
1471                            MacroBuilder &Builder) const {
1472    getDarwinDefines(Builder, Opts, Triple);
1473  }
1474
1475public:
1476  DarwinARMTargetInfo(const std::string& triple)
1477    : DarwinTargetInfo<ARMTargetInfo>(triple) {}
1478};
1479} // end anonymous namespace.
1480
1481namespace {
1482class SparcV8TargetInfo : public TargetInfo {
1483  static const TargetInfo::GCCRegAlias GCCRegAliases[];
1484  static const char * const GCCRegNames[];
1485public:
1486  SparcV8TargetInfo(const std::string& triple) : TargetInfo(triple) {
1487    // FIXME: Support Sparc quad-precision long double?
1488    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
1489                        "i64:64:64-f32:32:32-f64:64:64-v64:64:64-n32";
1490  }
1491  virtual void getTargetDefines(const LangOptions &Opts,
1492                                MacroBuilder &Builder) const {
1493    DefineStd(Builder, "sparc", Opts);
1494    Builder.defineMacro("__sparcv8");
1495    Builder.defineMacro("__REGISTER_PREFIX__", "");
1496  }
1497  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1498                                 unsigned &NumRecords) const {
1499    // FIXME: Implement!
1500  }
1501  virtual const char *getVAListDeclaration() const {
1502    return "typedef void* __builtin_va_list;";
1503  }
1504  virtual void getGCCRegNames(const char * const *&Names,
1505                              unsigned &NumNames) const;
1506  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1507                                unsigned &NumAliases) const;
1508  virtual bool validateAsmConstraint(const char *&Name,
1509                                     TargetInfo::ConstraintInfo &info) const {
1510    // FIXME: Implement!
1511    return false;
1512  }
1513  virtual const char *getClobbers() const {
1514    // FIXME: Implement!
1515    return "";
1516  }
1517};
1518
1519const char * const SparcV8TargetInfo::GCCRegNames[] = {
1520  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1521  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1522  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
1523  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
1524};
1525
1526void SparcV8TargetInfo::getGCCRegNames(const char * const *&Names,
1527                                       unsigned &NumNames) const {
1528  Names = GCCRegNames;
1529  NumNames = llvm::array_lengthof(GCCRegNames);
1530}
1531
1532const TargetInfo::GCCRegAlias SparcV8TargetInfo::GCCRegAliases[] = {
1533  { { "g0" }, "r0" },
1534  { { "g1" }, "r1" },
1535  { { "g2" }, "r2" },
1536  { { "g3" }, "r3" },
1537  { { "g4" }, "r4" },
1538  { { "g5" }, "r5" },
1539  { { "g6" }, "r6" },
1540  { { "g7" }, "r7" },
1541  { { "o0" }, "r8" },
1542  { { "o1" }, "r9" },
1543  { { "o2" }, "r10" },
1544  { { "o3" }, "r11" },
1545  { { "o4" }, "r12" },
1546  { { "o5" }, "r13" },
1547  { { "o6", "sp" }, "r14" },
1548  { { "o7" }, "r15" },
1549  { { "l0" }, "r16" },
1550  { { "l1" }, "r17" },
1551  { { "l2" }, "r18" },
1552  { { "l3" }, "r19" },
1553  { { "l4" }, "r20" },
1554  { { "l5" }, "r21" },
1555  { { "l6" }, "r22" },
1556  { { "l7" }, "r23" },
1557  { { "i0" }, "r24" },
1558  { { "i1" }, "r25" },
1559  { { "i2" }, "r26" },
1560  { { "i3" }, "r27" },
1561  { { "i4" }, "r28" },
1562  { { "i5" }, "r29" },
1563  { { "i6", "fp" }, "r30" },
1564  { { "i7" }, "r31" },
1565};
1566
1567void SparcV8TargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
1568                                         unsigned &NumAliases) const {
1569  Aliases = GCCRegAliases;
1570  NumAliases = llvm::array_lengthof(GCCRegAliases);
1571}
1572} // end anonymous namespace.
1573
1574namespace {
1575class AuroraUXSparcV8TargetInfo : public AuroraUXTargetInfo<SparcV8TargetInfo> {
1576public:
1577  AuroraUXSparcV8TargetInfo(const std::string& triple) :
1578      AuroraUXTargetInfo<SparcV8TargetInfo>(triple) {
1579    SizeType = UnsignedInt;
1580    PtrDiffType = SignedInt;
1581  }
1582};
1583class SolarisSparcV8TargetInfo : public SolarisTargetInfo<SparcV8TargetInfo> {
1584public:
1585  SolarisSparcV8TargetInfo(const std::string& triple) :
1586      SolarisTargetInfo<SparcV8TargetInfo>(triple) {
1587    SizeType = UnsignedInt;
1588    PtrDiffType = SignedInt;
1589  }
1590};
1591} // end anonymous namespace.
1592
1593namespace {
1594  class PIC16TargetInfo : public TargetInfo{
1595  public:
1596    PIC16TargetInfo(const std::string& triple) : TargetInfo(triple) {
1597      TLSSupported = false;
1598      IntWidth = 16;
1599      LongWidth = LongLongWidth = 32;
1600      PointerWidth = 16;
1601      IntAlign = 8;
1602      LongAlign = LongLongAlign = 8;
1603      PointerAlign = 8;
1604      SizeType = UnsignedInt;
1605      IntMaxType = SignedLong;
1606      UIntMaxType = UnsignedLong;
1607      IntPtrType = SignedShort;
1608      PtrDiffType = SignedInt;
1609      SigAtomicType = SignedLong;
1610      FloatWidth = 32;
1611      FloatAlign = 32;
1612      DoubleWidth = 32;
1613      DoubleAlign = 32;
1614      LongDoubleWidth = 32;
1615      LongDoubleAlign = 32;
1616      FloatFormat = &llvm::APFloat::IEEEsingle;
1617      DoubleFormat = &llvm::APFloat::IEEEsingle;
1618      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
1619      DescriptionString = "e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8-f32:32:32-n8";
1620
1621    }
1622    virtual uint64_t getPointerWidthV(unsigned AddrSpace) const { return 16; }
1623    virtual uint64_t getPointerAlignV(unsigned AddrSpace) const { return 8; }
1624    virtual void getTargetDefines(const LangOptions &Opts,
1625                                MacroBuilder &Builder) const {
1626      Builder.defineMacro("__pic16");
1627      Builder.defineMacro("__PIC16");
1628      Builder.defineMacro("rom", "__attribute__((address_space(1)))");
1629      Builder.defineMacro("ram", "__attribute__((address_space(0)))");
1630      Builder.defineMacro("__section(SectName)",
1631             "__attribute__((section(SectName)))");
1632      Builder.defineMacro("near",
1633             "__attribute__((section(\"Address=NEAR\")))");
1634      Builder.defineMacro("__address(Addr)",
1635             "__attribute__((section(\"Address=\"#Addr)))");
1636      Builder.defineMacro("__config(conf)", "asm(\"CONFIG \"#conf)");
1637      Builder.defineMacro("__idlocs(value)", "asm(\"__IDLOCS \"#value)");
1638      Builder.defineMacro("interrupt",
1639             "__attribute__((section(\"interrupt=0x4\"))) \
1640             __attribute__((used))");
1641    }
1642    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1643                                   unsigned &NumRecords) const {}
1644    virtual const char *getVAListDeclaration() const {
1645      return "typedef char* __builtin_va_list;";
1646    }
1647    virtual const char *getClobbers() const {
1648      return "";
1649    }
1650    virtual void getGCCRegNames(const char * const *&Names,
1651                                unsigned &NumNames) const {}
1652    virtual bool validateAsmConstraint(const char *&Name,
1653                                       TargetInfo::ConstraintInfo &info) const {
1654      return true;
1655    }
1656    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1657                                  unsigned &NumAliases) const {}
1658    virtual bool useGlobalsForAutomaticVariables() const {return true;}
1659  };
1660}
1661
1662namespace {
1663  class MSP430TargetInfo : public TargetInfo {
1664    static const char * const GCCRegNames[];
1665  public:
1666    MSP430TargetInfo(const std::string& triple) : TargetInfo(triple) {
1667      TLSSupported = false;
1668      IntWidth = 16; IntAlign = 16;
1669      LongWidth = 32; LongLongWidth = 64;
1670      LongAlign = LongLongAlign = 16;
1671      PointerWidth = 16; PointerAlign = 16;
1672      SizeType = UnsignedInt;
1673      IntMaxType = SignedLong;
1674      UIntMaxType = UnsignedLong;
1675      IntPtrType = SignedShort;
1676      PtrDiffType = SignedInt;
1677      SigAtomicType = SignedLong;
1678      DescriptionString = "e-p:16:16:16-i8:8:8-i16:16:16-i32:16:32-n8:16";
1679   }
1680    virtual void getTargetDefines(const LangOptions &Opts,
1681                                  MacroBuilder &Builder) const {
1682      Builder.defineMacro("MSP430");
1683      Builder.defineMacro("__MSP430__");
1684      // FIXME: defines for different 'flavours' of MCU
1685    }
1686    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1687                                   unsigned &NumRecords) const {
1688     // FIXME: Implement.
1689      Records = 0;
1690      NumRecords = 0;
1691    }
1692    virtual void getGCCRegNames(const char * const *&Names,
1693                                unsigned &NumNames) const;
1694    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1695                                  unsigned &NumAliases) const {
1696      // No aliases.
1697      Aliases = 0;
1698      NumAliases = 0;
1699    }
1700    virtual bool validateAsmConstraint(const char *&Name,
1701                                       TargetInfo::ConstraintInfo &info) const {
1702      // No target constraints for now.
1703      return false;
1704    }
1705    virtual const char *getClobbers() const {
1706      // FIXME: Is this really right?
1707      return "";
1708    }
1709    virtual const char *getVAListDeclaration() const {
1710      // FIXME: implement
1711      return "typedef char* __builtin_va_list;";
1712   }
1713  };
1714
1715  const char * const MSP430TargetInfo::GCCRegNames[] = {
1716    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1717    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1718  };
1719
1720  void MSP430TargetInfo::getGCCRegNames(const char * const *&Names,
1721                                        unsigned &NumNames) const {
1722    Names = GCCRegNames;
1723    NumNames = llvm::array_lengthof(GCCRegNames);
1724  }
1725}
1726
1727
1728namespace {
1729  class SystemZTargetInfo : public TargetInfo {
1730    static const char * const GCCRegNames[];
1731  public:
1732    SystemZTargetInfo(const std::string& triple) : TargetInfo(triple) {
1733      TLSSupported = false;
1734      IntWidth = IntAlign = 32;
1735      LongWidth = LongLongWidth = LongAlign = LongLongAlign = 64;
1736      PointerWidth = PointerAlign = 64;
1737      DescriptionString = "E-p:64:64:64-i8:8:16-i16:16:16-i32:32:32-"
1738      "i64:64:64-f32:32:32-f64:64:64-f128:128:128-a0:16:16-n32:64";
1739   }
1740    virtual void getTargetDefines(const LangOptions &Opts,
1741                                  MacroBuilder &Builder) const {
1742      Builder.defineMacro("__s390__");
1743      Builder.defineMacro("__s390x__");
1744    }
1745    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1746                                   unsigned &NumRecords) const {
1747      // FIXME: Implement.
1748      Records = 0;
1749      NumRecords = 0;
1750    }
1751
1752    virtual void getGCCRegNames(const char * const *&Names,
1753                                unsigned &NumNames) const;
1754    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1755                                  unsigned &NumAliases) const {
1756      // No aliases.
1757      Aliases = 0;
1758      NumAliases = 0;
1759    }
1760    virtual bool validateAsmConstraint(const char *&Name,
1761                                       TargetInfo::ConstraintInfo &info) const {
1762      // FIXME: implement
1763      return true;
1764    }
1765    virtual const char *getClobbers() const {
1766      // FIXME: Is this really right?
1767      return "";
1768    }
1769    virtual const char *getVAListDeclaration() const {
1770      // FIXME: implement
1771      return "typedef char* __builtin_va_list;";
1772   }
1773  };
1774
1775  const char * const SystemZTargetInfo::GCCRegNames[] = {
1776    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1777    "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1778  };
1779
1780  void SystemZTargetInfo::getGCCRegNames(const char * const *&Names,
1781                                         unsigned &NumNames) const {
1782    Names = GCCRegNames;
1783    NumNames = llvm::array_lengthof(GCCRegNames);
1784  }
1785}
1786
1787namespace {
1788  class BlackfinTargetInfo : public TargetInfo {
1789    static const char * const GCCRegNames[];
1790  public:
1791    BlackfinTargetInfo(const std::string& triple) : TargetInfo(triple) {
1792      TLSSupported = false;
1793      DoubleAlign = 32;
1794      LongLongAlign = 32;
1795      LongDoubleAlign = 32;
1796      DescriptionString = "e-p:32:32-i64:32-f64:32-n32";
1797    }
1798
1799    virtual void getTargetDefines(const LangOptions &Opts,
1800                                  MacroBuilder &Builder) const {
1801      DefineStd(Builder, "bfin", Opts);
1802      DefineStd(Builder, "BFIN", Opts);
1803      Builder.defineMacro("__ADSPBLACKFIN__");
1804      // FIXME: This one is really dependent on -mcpu
1805      Builder.defineMacro("__ADSPLPBLACKFIN__");
1806      // FIXME: Add cpu-dependent defines and __SILICON_REVISION__
1807    }
1808
1809    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1810                                   unsigned &NumRecords) const {
1811      // FIXME: Implement.
1812      Records = 0;
1813      NumRecords = 0;
1814    }
1815
1816    virtual void getGCCRegNames(const char * const *&Names,
1817                                unsigned &NumNames) const;
1818
1819    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1820                                  unsigned &NumAliases) const {
1821      // No aliases.
1822      Aliases = 0;
1823      NumAliases = 0;
1824    }
1825
1826    virtual bool validateAsmConstraint(const char *&Name,
1827                                       TargetInfo::ConstraintInfo &Info) const {
1828      if (strchr("adzDWeABbvfcCtukxywZY", Name[0])) {
1829        Info.setAllowsRegister();
1830        return true;
1831      }
1832      return false;
1833    }
1834
1835    virtual const char *getClobbers() const {
1836      return "";
1837    }
1838
1839    virtual const char *getVAListDeclaration() const {
1840      return "typedef char* __builtin_va_list;";
1841    }
1842  };
1843
1844  const char * const BlackfinTargetInfo::GCCRegNames[] = {
1845    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1846    "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
1847    "i0", "i1", "i2", "i3", "b0", "b1", "b2", "b3",
1848    "l0", "l1", "l2", "l3", "m0", "m1", "m2", "m3",
1849    "a0", "a1", "cc",
1850    "rets", "reti", "retx", "retn", "rete", "astat", "seqstat", "usp",
1851    "argp", "lt0", "lt1", "lc0", "lc1", "lb0", "lb1"
1852  };
1853
1854  void BlackfinTargetInfo::getGCCRegNames(const char * const *&Names,
1855                                          unsigned &NumNames) const {
1856    Names = GCCRegNames;
1857    NumNames = llvm::array_lengthof(GCCRegNames);
1858  }
1859}
1860
1861namespace {
1862
1863  // LLVM and Clang cannot be used directly to output native binaries for
1864  // target, but is used to compile C code to llvm bitcode with correct
1865  // type and alignment information.
1866  //
1867  // TCE uses the llvm bitcode as input and uses it for generating customized
1868  // target processor and program binary. TCE co-design environment is
1869  // publicly available in http://tce.cs.tut.fi
1870
1871  class TCETargetInfo : public TargetInfo{
1872  public:
1873    TCETargetInfo(const std::string& triple) : TargetInfo(triple) {
1874      TLSSupported = false;
1875      IntWidth = 32;
1876      LongWidth = LongLongWidth = 32;
1877      PointerWidth = 32;
1878      IntAlign = 32;
1879      LongAlign = LongLongAlign = 32;
1880      PointerAlign = 32;
1881      SizeType = UnsignedInt;
1882      IntMaxType = SignedLong;
1883      UIntMaxType = UnsignedLong;
1884      IntPtrType = SignedInt;
1885      PtrDiffType = SignedInt;
1886      FloatWidth = 32;
1887      FloatAlign = 32;
1888      DoubleWidth = 32;
1889      DoubleAlign = 32;
1890      LongDoubleWidth = 32;
1891      LongDoubleAlign = 32;
1892      FloatFormat = &llvm::APFloat::IEEEsingle;
1893      DoubleFormat = &llvm::APFloat::IEEEsingle;
1894      LongDoubleFormat = &llvm::APFloat::IEEEsingle;
1895      DescriptionString = "E-p:32:32:32-a0:32:32"
1896                          "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64"
1897                          "-f32:32:32-f64:32:64-n32";
1898    }
1899
1900    virtual void getTargetDefines(const LangOptions &Opts,
1901                                  MacroBuilder &Builder) const {
1902      DefineStd(Builder, "tce", Opts);
1903      Builder.defineMacro("__TCE__");
1904      Builder.defineMacro("__TCE_V1__");
1905    }
1906    virtual void getTargetBuiltins(const Builtin::Info *&Records,
1907                                   unsigned &NumRecords) const {}
1908    virtual const char *getClobbers() const {
1909      return "";
1910    }
1911    virtual const char *getVAListDeclaration() const {
1912      return "typedef void* __builtin_va_list;";
1913    }
1914    virtual void getGCCRegNames(const char * const *&Names,
1915                                unsigned &NumNames) const {}
1916    virtual bool validateAsmConstraint(const char *&Name,
1917                                       TargetInfo::ConstraintInfo &info) const {
1918      return true;
1919    }
1920    virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1921                                  unsigned &NumAliases) const {}
1922  };
1923}
1924
1925namespace {
1926class MipsTargetInfo : public TargetInfo {
1927  static const TargetInfo::GCCRegAlias GCCRegAliases[];
1928  static const char * const GCCRegNames[];
1929public:
1930  MipsTargetInfo(const std::string& triple) : TargetInfo(triple) {
1931    DescriptionString = "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
1932                        "i64:32:64-f32:32:32-f64:64:64-v64:64:64-n32";
1933  }
1934  virtual void getTargetDefines(const LangOptions &Opts,
1935                                MacroBuilder &Builder) const {
1936    DefineStd(Builder, "mips", Opts);
1937    Builder.defineMacro("_mips");
1938    DefineStd(Builder, "MIPSEB", Opts);
1939    Builder.defineMacro("_MIPSEB");
1940    Builder.defineMacro("__REGISTER_PREFIX__", "");
1941  }
1942  virtual void getTargetBuiltins(const Builtin::Info *&Records,
1943                                 unsigned &NumRecords) const {
1944    // FIXME: Implement!
1945  }
1946  virtual const char *getVAListDeclaration() const {
1947    return "typedef void* __builtin_va_list;";
1948  }
1949  virtual void getGCCRegNames(const char * const *&Names,
1950                              unsigned &NumNames) const;
1951  virtual void getGCCRegAliases(const GCCRegAlias *&Aliases,
1952                                unsigned &NumAliases) const;
1953  virtual bool validateAsmConstraint(const char *&Name,
1954                                     TargetInfo::ConstraintInfo &Info) const {
1955    switch (*Name) {
1956    default:
1957    case 'r': // CPU registers.
1958    case 'd': // Equivalent to "r" unless generating MIPS16 code.
1959    case 'y': // Equivalent to "r", backwards compatibility only.
1960    case 'f': // floating-point registers.
1961      Info.setAllowsRegister();
1962      return true;
1963    }
1964    return false;
1965  }
1966
1967  virtual const char *getClobbers() const {
1968    // FIXME: Implement!
1969    return "";
1970  }
1971};
1972
1973const char * const MipsTargetInfo::GCCRegNames[] = {
1974  "$0",   "$1",   "$2",   "$3",   "$4",   "$5",   "$6",   "$7",
1975  "$8",   "$9",   "$10",  "$11",  "$12",  "$13",  "$14",  "$15",
1976  "$16",  "$17",  "$18",  "$19",  "$20",  "$21",  "$22",  "$23",
1977  "$24",  "$25",  "$26",  "$27",  "$28",  "$sp",  "$fp",  "$31",
1978  "$f0",  "$f1",  "$f2",  "$f3",  "$f4",  "$f5",  "$f6",  "$f7",
1979  "$f8",  "$f9",  "$f10", "$f11", "$f12", "$f13", "$f14", "$f15",
1980  "$f16", "$f17", "$f18", "$f19", "$f20", "$f21", "$f22", "$f23",
1981  "$f24", "$f25", "$f26", "$f27", "$f28", "$f29", "$f30", "$f31",
1982  "hi",   "lo",   "",     "$fcc0","$fcc1","$fcc2","$fcc3","$fcc4",
1983  "$fcc5","$fcc6","$fcc7"
1984};
1985
1986void MipsTargetInfo::getGCCRegNames(const char * const *&Names,
1987                                       unsigned &NumNames) const {
1988  Names = GCCRegNames;
1989  NumNames = llvm::array_lengthof(GCCRegNames);
1990}
1991
1992const TargetInfo::GCCRegAlias MipsTargetInfo::GCCRegAliases[] = {
1993  { { "at" },  "$1" },
1994  { { "v0" },  "$2" },
1995  { { "v1" },  "$3" },
1996  { { "a0" },  "$4" },
1997  { { "a1" },  "$5" },
1998  { { "a2" },  "$6" },
1999  { { "a3" },  "$7" },
2000  { { "t0" },  "$8" },
2001  { { "t1" },  "$9" },
2002  { { "t2" }, "$10" },
2003  { { "t3" }, "$11" },
2004  { { "t4" }, "$12" },
2005  { { "t5" }, "$13" },
2006  { { "t6" }, "$14" },
2007  { { "t7" }, "$15" },
2008  { { "s0" }, "$16" },
2009  { { "s1" }, "$17" },
2010  { { "s2" }, "$18" },
2011  { { "s3" }, "$19" },
2012  { { "s4" }, "$20" },
2013  { { "s5" }, "$21" },
2014  { { "s6" }, "$22" },
2015  { { "s7" }, "$23" },
2016  { { "t8" }, "$24" },
2017  { { "t9" }, "$25" },
2018  { { "k0" }, "$26" },
2019  { { "k1" }, "$27" },
2020  { { "gp" }, "$28" },
2021  { { "sp" }, "$29" },
2022  { { "fp" }, "$30" },
2023  { { "ra" }, "$31" }
2024};
2025
2026void MipsTargetInfo::getGCCRegAliases(const GCCRegAlias *&Aliases,
2027                                         unsigned &NumAliases) const {
2028  Aliases = GCCRegAliases;
2029  NumAliases = llvm::array_lengthof(GCCRegAliases);
2030}
2031} // end anonymous namespace.
2032
2033namespace {
2034class MipselTargetInfo : public MipsTargetInfo {
2035public:
2036  MipselTargetInfo(const std::string& triple) : MipsTargetInfo(triple) {
2037    DescriptionString = "e-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-"
2038                        "i64:32:64-f32:32:32-f64:64:64-v64:64:64-n32";
2039  }
2040
2041  virtual void getTargetDefines(const LangOptions &Opts,
2042                                MacroBuilder &Builder) const;
2043};
2044
2045void MipselTargetInfo::getTargetDefines(const LangOptions &Opts,
2046                                        MacroBuilder &Builder) const {
2047  DefineStd(Builder, "mips", Opts);
2048  Builder.defineMacro("_mips");
2049  DefineStd(Builder, "MIPSEL", Opts);
2050  Builder.defineMacro("_MIPSEL");
2051  Builder.defineMacro("__REGISTER_PREFIX__", "");
2052}
2053} // end anonymous namespace.
2054
2055//===----------------------------------------------------------------------===//
2056// Driver code
2057//===----------------------------------------------------------------------===//
2058
2059static TargetInfo *AllocateTarget(const std::string &T) {
2060  llvm::Triple Triple(T);
2061  llvm::Triple::OSType os = Triple.getOS();
2062
2063  switch (Triple.getArch()) {
2064  default:
2065    return NULL;
2066
2067  case llvm::Triple::arm:
2068  case llvm::Triple::thumb:
2069    switch (os) {
2070    case llvm::Triple::Darwin:
2071      return new DarwinARMTargetInfo(T);
2072    case llvm::Triple::FreeBSD:
2073      return new FreeBSDTargetInfo<ARMTargetInfo>(T);
2074    default:
2075      return new ARMTargetInfo(T);
2076    }
2077
2078  case llvm::Triple::bfin:
2079    return new BlackfinTargetInfo(T);
2080
2081  case llvm::Triple::msp430:
2082    return new MSP430TargetInfo(T);
2083
2084  case llvm::Triple::mips:
2085    if (os == llvm::Triple::Psp)
2086      return new PSPTargetInfo<MipsTargetInfo>(T);
2087    if (os == llvm::Triple::Linux)
2088      return new LinuxTargetInfo<MipsTargetInfo>(T);
2089    return new MipsTargetInfo(T);
2090
2091  case llvm::Triple::mipsel:
2092    if (os == llvm::Triple::Psp)
2093      return new PSPTargetInfo<MipselTargetInfo>(T);
2094    if (os == llvm::Triple::Linux)
2095      return new LinuxTargetInfo<MipselTargetInfo>(T);
2096    return new MipselTargetInfo(T);
2097
2098  case llvm::Triple::pic16:
2099    return new PIC16TargetInfo(T);
2100
2101  case llvm::Triple::ppc:
2102    if (os == llvm::Triple::Darwin)
2103      return new DarwinTargetInfo<PPCTargetInfo>(T);
2104    else if (os == llvm::Triple::FreeBSD)
2105      return new FreeBSDTargetInfo<PPC32TargetInfo>(T);
2106    return new PPC32TargetInfo(T);
2107
2108  case llvm::Triple::ppc64:
2109    if (os == llvm::Triple::Darwin)
2110      return new DarwinTargetInfo<PPC64TargetInfo>(T);
2111    else if (os == llvm::Triple::Lv2)
2112      return new PS3PPUTargetInfo<PPC64TargetInfo>(T);
2113    else if (os == llvm::Triple::FreeBSD)
2114      return new FreeBSDTargetInfo<PPC64TargetInfo>(T);
2115    return new PPC64TargetInfo(T);
2116
2117  case llvm::Triple::sparc:
2118    if (os == llvm::Triple::AuroraUX)
2119      return new AuroraUXSparcV8TargetInfo(T);
2120    if (os == llvm::Triple::Solaris)
2121      return new SolarisSparcV8TargetInfo(T);
2122    return new SparcV8TargetInfo(T);
2123
2124  // FIXME: Need a real SPU target.
2125  case llvm::Triple::cellspu:
2126    return new PS3SPUTargetInfo<PPC64TargetInfo>(T);
2127
2128  case llvm::Triple::systemz:
2129    return new SystemZTargetInfo(T);
2130
2131  case llvm::Triple::tce:
2132    return new TCETargetInfo(T);
2133
2134  case llvm::Triple::x86:
2135    switch (os) {
2136    case llvm::Triple::AuroraUX:
2137      return new AuroraUXTargetInfo<X86_32TargetInfo>(T);
2138    case llvm::Triple::Darwin:
2139      return new DarwinI386TargetInfo(T);
2140    case llvm::Triple::Linux:
2141      return new LinuxTargetInfo<X86_32TargetInfo>(T);
2142    case llvm::Triple::DragonFly:
2143      return new DragonFlyBSDTargetInfo<X86_32TargetInfo>(T);
2144    case llvm::Triple::NetBSD:
2145      return new NetBSDTargetInfo<X86_32TargetInfo>(T);
2146    case llvm::Triple::OpenBSD:
2147      return new OpenBSDI386TargetInfo(T);
2148    case llvm::Triple::FreeBSD:
2149      return new FreeBSDTargetInfo<X86_32TargetInfo>(T);
2150    case llvm::Triple::Solaris:
2151      return new SolarisTargetInfo<X86_32TargetInfo>(T);
2152    case llvm::Triple::Cygwin:
2153      return new CygwinX86_32TargetInfo(T);
2154    case llvm::Triple::MinGW32:
2155      return new MinGWX86_32TargetInfo(T);
2156    case llvm::Triple::Win32:
2157      return new VisualStudioWindowsX86_32TargetInfo(T);
2158    default:
2159      return new X86_32TargetInfo(T);
2160    }
2161
2162  case llvm::Triple::x86_64:
2163    switch (os) {
2164    case llvm::Triple::AuroraUX:
2165      return new AuroraUXTargetInfo<X86_64TargetInfo>(T);
2166    case llvm::Triple::Darwin:
2167      return new DarwinX86_64TargetInfo(T);
2168    case llvm::Triple::Linux:
2169      return new LinuxTargetInfo<X86_64TargetInfo>(T);
2170    case llvm::Triple::DragonFly:
2171      return new DragonFlyBSDTargetInfo<X86_64TargetInfo>(T);
2172    case llvm::Triple::NetBSD:
2173      return new NetBSDTargetInfo<X86_64TargetInfo>(T);
2174    case llvm::Triple::OpenBSD:
2175      return new OpenBSDX86_64TargetInfo(T);
2176    case llvm::Triple::FreeBSD:
2177      return new FreeBSDTargetInfo<X86_64TargetInfo>(T);
2178    case llvm::Triple::Solaris:
2179      return new SolarisTargetInfo<X86_64TargetInfo>(T);
2180    case llvm::Triple::MinGW64:
2181      return new MinGWX86_64TargetInfo(T);
2182    case llvm::Triple::Win32:   // This is what Triple.h supports now.
2183      return new VisualStudioWindowsX86_64TargetInfo(T);
2184    default:
2185      return new X86_64TargetInfo(T);
2186    }
2187  }
2188}
2189
2190/// CreateTargetInfo - Return the target info object for the specified target
2191/// triple.
2192TargetInfo *TargetInfo::CreateTargetInfo(Diagnostic &Diags,
2193                                         TargetOptions &Opts) {
2194  llvm::Triple Triple(Opts.Triple);
2195
2196  // Construct the target
2197  llvm::OwningPtr<TargetInfo> Target(AllocateTarget(Triple.str()));
2198  if (!Target) {
2199    Diags.Report(diag::err_target_unknown_triple) << Triple.str();
2200    return 0;
2201  }
2202
2203  // Set the target CPU if specified.
2204  if (!Opts.CPU.empty() && !Target->setCPU(Opts.CPU)) {
2205    Diags.Report(diag::err_target_unknown_cpu) << Opts.CPU;
2206    return 0;
2207  }
2208
2209  // Set the target ABI if specified.
2210  if (!Opts.ABI.empty() && !Target->setABI(Opts.ABI)) {
2211    Diags.Report(diag::err_target_unknown_abi) << Opts.ABI;
2212    return 0;
2213  }
2214
2215  // Compute the default target features, we need the target to handle this
2216  // because features may have dependencies on one another.
2217  llvm::StringMap<bool> Features;
2218  Target->getDefaultFeatures(Opts.CPU, Features);
2219
2220  // Apply the user specified deltas.
2221  for (std::vector<std::string>::const_iterator it = Opts.Features.begin(),
2222         ie = Opts.Features.end(); it != ie; ++it) {
2223    const char *Name = it->c_str();
2224
2225    // Apply the feature via the target.
2226    if ((Name[0] != '-' && Name[0] != '+') ||
2227        !Target->setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
2228      Diags.Report(diag::err_target_invalid_feature) << Name;
2229      return 0;
2230    }
2231  }
2232
2233  // Add the features to the compile options.
2234  //
2235  // FIXME: If we are completely confident that we have the right set, we only
2236  // need to pass the minuses.
2237  Opts.Features.clear();
2238  for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
2239         ie = Features.end(); it != ie; ++it)
2240    Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
2241  Target->HandleTargetFeatures(Opts.Features);
2242
2243  return Target.take();
2244}
2245