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