1 //===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
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 #ifndef LLVM_ADT_TRIPLE_H
11 #define LLVM_ADT_TRIPLE_H
12 
13 #include "llvm/ADT/Twine.h"
14 
15 // Some system headers or GCC predefined macros conflict with identifiers in
16 // this file.  Undefine them here.
17 #undef mips
18 #undef sparc
19 
20 namespace llvm {
21 
22 /// Triple - Helper class for working with target triples.
23 ///
24 /// Target triples are strings in the canonical form:
25 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
26 /// or
27 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
28 ///
29 /// This class is used for clients which want to support arbitrary
30 /// target triples, but also want to implement certain special
31 /// behavior for particular targets. This class isolates the mapping
32 /// from the components of the target triple to well known IDs.
33 ///
34 /// At its core the Triple class is designed to be a wrapper for a triple
35 /// string; the constructor does not change or normalize the triple string.
36 /// Clients that need to handle the non-canonical triples that users often
37 /// specify should use the normalize method.
38 ///
39 /// See autoconf/config.guess for a glimpse into what triples look like in
40 /// practice.
41 class Triple {
42 public:
43   enum ArchType {
44     UnknownArch,
45 
46     alpha,   // Alpha: alpha
47     arm,     // ARM; arm, armv.*, xscale
48     bfin,    // Blackfin: bfin
49     cellspu, // CellSPU: spu, cellspu
50     mips,    // MIPS: mips, mipsallegrex
51     mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
52     mips64,  // MIPS64: mips64
53     mips64el,// MIPS64EL: mips64el
54     msp430,  // MSP430: msp430
55     ppc,     // PPC: powerpc
56     ppc64,   // PPC64: powerpc64, ppu
57     sparc,   // Sparc: sparc
58     sparcv9, // Sparcv9: Sparcv9
59     systemz, // SystemZ: s390x
60     tce,     // TCE (http://tce.cs.tut.fi/): tce
61     thumb,   // Thumb: thumb, thumbv.*
62     x86,     // X86: i[3-9]86
63     x86_64,  // X86-64: amd64, x86_64
64     xcore,   // XCore: xcore
65     mblaze,  // MBlaze: mblaze
66     ptx32,   // PTX: ptx (32-bit)
67     ptx64,   // PTX: ptx (64-bit)
68     le32,    // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
69     amdil,   // amdil: amd IL
70 
71     InvalidArch
72   };
73   enum VendorType {
74     UnknownVendor,
75 
76     Apple,
77     PC,
78     SCEI
79   };
80   enum OSType {
81     UnknownOS,
82 
83     AuroraUX,
84     Cygwin,
85     Darwin,
86     DragonFly,
87     FreeBSD,
88     IOS,
89     KFreeBSD,
90     Linux,
91     Lv2,        // PS3
92     MacOSX,
93     MinGW32,    // i*86-pc-mingw32, *-w64-mingw32
94     NetBSD,
95     OpenBSD,
96     Psp,
97     Solaris,
98     Win32,
99     Haiku,
100     Minix,
101     RTEMS,
102     NativeClient
103   };
104   enum EnvironmentType {
105     UnknownEnvironment,
106 
107     GNU,
108     GNUEABI,
109     EABI,
110     MachO
111   };
112 
113 private:
114   std::string Data;
115 
116   /// The parsed arch type (or InvalidArch if uninitialized).
117   mutable ArchType Arch;
118 
119   /// The parsed vendor type.
120   mutable VendorType Vendor;
121 
122   /// The parsed OS type.
123   mutable OSType OS;
124 
125   /// The parsed Environment type.
126   mutable EnvironmentType Environment;
127 
isInitialized()128   bool isInitialized() const { return Arch != InvalidArch; }
129   static ArchType ParseArch(StringRef ArchName);
130   static VendorType ParseVendor(StringRef VendorName);
131   static OSType ParseOS(StringRef OSName);
132   static EnvironmentType ParseEnvironment(StringRef EnvironmentName);
133   void Parse() const;
134 
135 public:
136   /// @name Constructors
137   /// @{
138 
Triple()139   Triple() : Data(), Arch(InvalidArch) {}
Triple(const Twine & Str)140   explicit Triple(const Twine &Str) : Data(Str.str()), Arch(InvalidArch) {}
Triple(const Twine & ArchStr,const Twine & VendorStr,const Twine & OSStr)141   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr)
142     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr).str()),
143       Arch(InvalidArch) {
144   }
145 
Triple(const Twine & ArchStr,const Twine & VendorStr,const Twine & OSStr,const Twine & EnvironmentStr)146   Triple(const Twine &ArchStr, const Twine &VendorStr, const Twine &OSStr,
147          const Twine &EnvironmentStr)
148     : Data((ArchStr + Twine('-') + VendorStr + Twine('-') + OSStr + Twine('-') +
149             EnvironmentStr).str()), Arch(InvalidArch) {
150   }
151 
152   /// @}
153   /// @name Normalization
154   /// @{
155 
156   /// normalize - Turn an arbitrary machine specification into the canonical
157   /// triple form (or something sensible that the Triple class understands if
158   /// nothing better can reasonably be done).  In particular, it handles the
159   /// common case in which otherwise valid components are in the wrong order.
160   static std::string normalize(StringRef Str);
161 
162   /// @}
163   /// @name Typed Component Access
164   /// @{
165 
166   /// getArch - Get the parsed architecture type of this triple.
getArch()167   ArchType getArch() const {
168     if (!isInitialized()) Parse();
169     return Arch;
170   }
171 
172   /// getVendor - Get the parsed vendor type of this triple.
getVendor()173   VendorType getVendor() const {
174     if (!isInitialized()) Parse();
175     return Vendor;
176   }
177 
178   /// getOS - Get the parsed operating system type of this triple.
getOS()179   OSType getOS() const {
180     if (!isInitialized()) Parse();
181     return OS;
182   }
183 
184   /// hasEnvironment - Does this triple have the optional environment
185   /// (fourth) component?
hasEnvironment()186   bool hasEnvironment() const {
187     return getEnvironmentName() != "";
188   }
189 
190   /// getEnvironment - Get the parsed environment type of this triple.
getEnvironment()191   EnvironmentType getEnvironment() const {
192     if (!isInitialized()) Parse();
193     return Environment;
194   }
195 
196   /// @}
197   /// @name Direct Component Access
198   /// @{
199 
str()200   const std::string &str() const { return Data; }
201 
getTriple()202   const std::string &getTriple() const { return Data; }
203 
204   /// getArchName - Get the architecture (first) component of the
205   /// triple.
206   StringRef getArchName() const;
207 
208   /// getVendorName - Get the vendor (second) component of the triple.
209   StringRef getVendorName() const;
210 
211   /// getOSName - Get the operating system (third) component of the
212   /// triple.
213   StringRef getOSName() const;
214 
215   /// getEnvironmentName - Get the optional environment (fourth)
216   /// component of the triple, or "" if empty.
217   StringRef getEnvironmentName() const;
218 
219   /// getOSAndEnvironmentName - Get the operating system and optional
220   /// environment components as a single string (separated by a '-'
221   /// if the environment component is present).
222   StringRef getOSAndEnvironmentName() const;
223 
224   /// getOSVersion - Parse the version number from the OS name component of the
225   /// triple, if present.
226   ///
227   /// For example, "fooos1.2.3" would return (1, 2, 3).
228   ///
229   /// If an entry is not defined, it will be returned as 0.
230   void getOSVersion(unsigned &Major, unsigned &Minor, unsigned &Micro) const;
231 
232   /// getOSMajorVersion - Return just the major version number, this is
233   /// specialized because it is a common query.
getOSMajorVersion()234   unsigned getOSMajorVersion() const {
235     unsigned Maj, Min, Micro;
236     getOSVersion(Maj, Min, Micro);
237     return Maj;
238   }
239 
240   /// isOSVersionLT - Helper function for doing comparisons against version
241   /// numbers included in the target triple.
242   bool isOSVersionLT(unsigned Major, unsigned Minor = 0,
243                      unsigned Micro = 0) const {
244     unsigned LHS[3];
245     getOSVersion(LHS[0], LHS[1], LHS[2]);
246 
247     if (LHS[0] != Major)
248       return LHS[0] < Major;
249     if (LHS[1] != Minor)
250       return LHS[1] < Minor;
251     if (LHS[2] != Micro)
252       return LHS[1] < Micro;
253 
254     return false;
255   }
256 
257   /// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
258   /// "darwin" and "osx" as OS X triples.
isMacOSX()259   bool isMacOSX() const {
260     return getOS() == Triple::Darwin || getOS() == Triple::MacOSX;
261   }
262 
263   /// isOSDarwin - Is this a "Darwin" OS (OS X or iOS).
isOSDarwin()264   bool isOSDarwin() const {
265     return isMacOSX() || getOS() == Triple::IOS;
266   }
267 
268   /// isOSWindows - Is this a "Windows" OS.
isOSWindows()269   bool isOSWindows() const {
270     return getOS() == Triple::Win32 || getOS() == Triple::Cygwin ||
271       getOS() == Triple::MinGW32;
272   }
273 
274   /// isMacOSXVersionLT - Comparison function for checking OS X version
275   /// compatibility, which handles supporting skewed version numbering schemes
276   /// used by the "darwin" triples.
277   unsigned isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
278 			     unsigned Micro = 0) const {
279     assert(isMacOSX() && "Not an OS X triple!");
280 
281     // If this is OS X, expect a sane version number.
282     if (getOS() == Triple::MacOSX)
283       return isOSVersionLT(Major, Minor, Micro);
284 
285     // Otherwise, compare to the "Darwin" number.
286     assert(Major == 10 && "Unexpected major version");
287     return isOSVersionLT(Minor + 4, Micro, 0);
288   }
289 
290   /// @}
291   /// @name Mutators
292   /// @{
293 
294   /// setArch - Set the architecture (first) component of the triple
295   /// to a known type.
296   void setArch(ArchType Kind);
297 
298   /// setVendor - Set the vendor (second) component of the triple to a
299   /// known type.
300   void setVendor(VendorType Kind);
301 
302   /// setOS - Set the operating system (third) component of the triple
303   /// to a known type.
304   void setOS(OSType Kind);
305 
306   /// setEnvironment - Set the environment (fourth) component of the triple
307   /// to a known type.
308   void setEnvironment(EnvironmentType Kind);
309 
310   /// setTriple - Set all components to the new triple \arg Str.
311   void setTriple(const Twine &Str);
312 
313   /// setArchName - Set the architecture (first) component of the
314   /// triple by name.
315   void setArchName(StringRef Str);
316 
317   /// setVendorName - Set the vendor (second) component of the triple
318   /// by name.
319   void setVendorName(StringRef Str);
320 
321   /// setOSName - Set the operating system (third) component of the
322   /// triple by name.
323   void setOSName(StringRef Str);
324 
325   /// setEnvironmentName - Set the optional environment (fourth)
326   /// component of the triple by name.
327   void setEnvironmentName(StringRef Str);
328 
329   /// setOSAndEnvironmentName - Set the operating system and optional
330   /// environment components with a single string.
331   void setOSAndEnvironmentName(StringRef Str);
332 
333   /// getArchNameForAssembler - Get an architecture name that is understood by
334   /// the target assembler.
335   const char *getArchNameForAssembler();
336 
337   /// @}
338   /// @name Static helpers for IDs.
339   /// @{
340 
341   /// getArchTypeName - Get the canonical name for the \arg Kind
342   /// architecture.
343   static const char *getArchTypeName(ArchType Kind);
344 
345   /// getArchTypePrefix - Get the "prefix" canonical name for the \arg Kind
346   /// architecture. This is the prefix used by the architecture specific
347   /// builtins, and is suitable for passing to \see
348   /// Intrinsic::getIntrinsicForGCCBuiltin().
349   ///
350   /// \return - The architecture prefix, or 0 if none is defined.
351   static const char *getArchTypePrefix(ArchType Kind);
352 
353   /// getVendorTypeName - Get the canonical name for the \arg Kind
354   /// vendor.
355   static const char *getVendorTypeName(VendorType Kind);
356 
357   /// getOSTypeName - Get the canonical name for the \arg Kind operating
358   /// system.
359   static const char *getOSTypeName(OSType Kind);
360 
361   /// getEnvironmentTypeName - Get the canonical name for the \arg Kind
362   /// environment.
363   static const char *getEnvironmentTypeName(EnvironmentType Kind);
364 
365   /// @}
366   /// @name Static helpers for converting alternate architecture names.
367   /// @{
368 
369   /// getArchTypeForLLVMName - The canonical type for the given LLVM
370   /// architecture name (e.g., "x86").
371   static ArchType getArchTypeForLLVMName(StringRef Str);
372 
373   /// getArchTypeForDarwinArchName - Get the architecture type for a "Darwin"
374   /// architecture name, for example as accepted by "gcc -arch" (see also
375   /// arch(3)).
376   static ArchType getArchTypeForDarwinArchName(StringRef Str);
377 
378   /// @}
379 };
380 
381 } // End llvm namespace
382 
383 
384 #endif
385