4ba8576af5ffa640bfc40768a6985c64e9e09cc4
[oota-llvm.git] / include / llvm / ADT / Triple.h
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/StringRef.h"
14 #include <string>
15
16 namespace llvm {
17 class StringRef;
18 class Twine;
19
20 /// Triple - Helper class for working with target triples.
21 ///
22 /// Target triples are strings in the format of:
23 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
24 /// or
25 ///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
26 ///
27 /// This class is used for clients which want to support arbitrary
28 /// target triples, but also want to implement certain special
29 /// behavior for particular targets. This class isolates the mapping
30 /// from the components of the target triple to well known IDs.
31 ///
32 /// See autoconf/config.guess for a glimpse into what they look like
33 /// in practice.
34 class Triple {
35 public:
36   enum ArchType {
37     UnknownArch,
38     
39     alpha,   // Alpha: alpha
40     arm,     // ARM; arm, armv.*, xscale
41     bfin,    // Blackfin: bfin
42     cellspu, // CellSPU: spu, cellspu
43     mips,    // MIPS: mips, mipsallegrex
44     mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
45     msp430,  // MPS430: msp430
46     pic16,   // PIC16: pic16
47     ppc,     // PPC: powerpc
48     ppc64,   // PPC64: powerpc64
49     sparc,   // Sparc: sparc
50     systemz, // SystemZ: s390x
51     thumb,   // Thumb: thumb, thumbv.*
52     x86,     // X86: i[3-9]86
53     x86_64,  // X86-64: amd64, x86_64
54     xcore,   // XCore: xcore
55
56     InvalidArch
57   };
58   enum VendorType {
59     UnknownVendor,
60
61     Apple, 
62     PC
63   };
64   enum OSType {
65     UnknownOS,
66
67     AuroraUX,
68     Cygwin,
69     Darwin,
70     DragonFly,
71     FreeBSD,
72     Linux,
73     MinGW32,
74     MinGW64,
75     NetBSD,
76     OpenBSD,
77     Solaris,
78     Win32
79   };
80   
81 private:
82   std::string Data;
83
84   /// The parsed arch type (or InvalidArch if uninitialized).
85   mutable ArchType Arch;
86
87   /// The parsed vendor type.
88   mutable VendorType Vendor;
89
90   /// The parsed OS type.
91   mutable OSType OS;
92
93   bool isInitialized() const { return Arch != InvalidArch; }
94   void Parse() const;
95
96 public:
97   /// @name Constructors
98   /// @{
99   
100   Triple() : Data(), Arch(InvalidArch) {}
101   explicit Triple(const StringRef &Str) : Data(Str), Arch(InvalidArch) {}
102   explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
103     : Data(ArchStr), Arch(InvalidArch) {
104     Data += '-';
105     Data += VendorStr;
106     Data += '-';
107     Data += OSStr;
108   }
109
110   /// @}
111   /// @name Typed Component Access
112   /// @{
113   
114   /// getArch - Get the parsed architecture type of this triple.
115   ArchType getArch() const { 
116     if (!isInitialized()) Parse(); 
117     return Arch;
118   }
119   
120   /// getVendor - Get the parsed vendor type of this triple.
121   VendorType getVendor() const { 
122     if (!isInitialized()) Parse(); 
123     return Vendor;
124   }
125   
126   /// getOS - Get the parsed operating system type of this triple.
127   OSType getOS() const { 
128     if (!isInitialized()) Parse(); 
129     return OS;
130   }
131
132   /// hasEnvironment - Does this triple have the optional environment
133   /// (fourth) component?
134   bool hasEnvironment() const {
135     return getEnvironmentName() != "";
136   }
137
138   /// @}
139   /// @name Direct Component Access
140   /// @{
141
142   const std::string &getTriple() const { return Data; }
143
144   /// getArchName - Get the architecture (first) component of the
145   /// triple.
146   StringRef getArchName() const;
147
148   /// getVendorName - Get the vendor (second) component of the triple.
149   StringRef getVendorName() const;
150
151   /// getOSName - Get the operating system (third) component of the
152   /// triple.
153   StringRef getOSName() const;
154
155   /// getEnvironmentName - Get the optional environment (fourth)
156   /// component of the triple, or "" if empty.
157   StringRef getEnvironmentName() const;
158
159   /// getOSAndEnvironmentName - Get the operating system and optional
160   /// environment components as a single string (separated by a '-'
161   /// if the environment component is present).
162   StringRef getOSAndEnvironmentName() const;
163
164   
165   /// getDarwinNumber - Parse the 'darwin number' out of the specific target
166   /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
167   /// not defined, return 0's.  This requires that the triple have an OSType of
168   /// darwin before it is called.
169   void getDarwinNumber(unsigned &Maj, unsigned &Min, unsigned &Revision) const;
170   
171   /// getDarwinMajorNumber - Return just the major version number, this is
172   /// specialized because it is a common query.
173   unsigned getDarwinMajorNumber() const {
174     unsigned Maj, Min, Rev;
175     getDarwinNumber(Maj, Min, Rev);
176     return Maj;
177   }
178   
179   /// @}
180   /// @name Mutators
181   /// @{
182
183   /// setArch - Set the architecture (first) component of the triple
184   /// to a known type.
185   void setArch(ArchType Kind);
186
187   /// setVendor - Set the vendor (second) component of the triple to a
188   /// known type.
189   void setVendor(VendorType Kind);
190
191   /// setOS - Set the operating system (third) component of the triple
192   /// to a known type.
193   void setOS(OSType Kind);
194
195   /// setTriple - Set all components to the new triple \arg Str.
196   void setTriple(const Twine &Str);
197
198   /// setArchName - Set the architecture (first) component of the
199   /// triple by name.
200   void setArchName(const StringRef &Str);
201
202   /// setVendorName - Set the vendor (second) component of the triple
203   /// by name.
204   void setVendorName(const StringRef &Str);
205
206   /// setOSName - Set the operating system (third) component of the
207   /// triple by name.
208   void setOSName(const StringRef &Str);
209
210   /// setEnvironmentName - Set the optional environment (fourth)
211   /// component of the triple by name.
212   void setEnvironmentName(const StringRef &Str);
213
214   /// setOSAndEnvironmentName - Set the operating system and optional
215   /// environment components with a single string.
216   void setOSAndEnvironmentName(const StringRef &Str);
217
218   /// @}
219   /// @name Static helpers for IDs.
220   /// @{
221
222   /// getArchTypeName - Get the canonical name for the \arg Kind
223   /// architecture.
224   static const char *getArchTypeName(ArchType Kind);
225
226   /// getVendorTypeName - Get the canonical name for the \arg Kind
227   /// vendor.
228   static const char *getVendorTypeName(VendorType Kind);
229
230   /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
231   static const char *getOSTypeName(OSType Kind);
232
233   /// getArchTypeForLLVMName - The canonical type for the given LLVM
234   /// architecture name (e.g., "x86").
235   static ArchType getArchTypeForLLVMName(const StringRef &Str);
236
237   /// @}
238 };
239
240 } // End llvm namespace
241
242
243 #endif