Recognize xscale as an ARM arch.
[oota-llvm.git] / lib / Support / Triple.cpp
1 //===--- Triple.cpp - Target triple helper class --------------------------===//
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 #include "llvm/ADT/Triple.h"
11
12 #include "llvm/ADT/Twine.h"
13 #include <cassert>
14 #include <cstring>
15 using namespace llvm;
16
17 //
18
19 const char *Triple::getArchTypeName(ArchType Kind) {
20   switch (Kind) {
21   case InvalidArch: return "<invalid>";
22   case UnknownArch: return "unknown";
23     
24   case alpha:   return "alpha";
25   case arm:     return "arm";
26   case bfin:    return "bfin";
27   case cellspu: return "cellspu";
28   case mips:    return "mips";
29   case mipsel:  return "mipsel";
30   case msp430:  return "msp430";
31   case pic16:   return "pic16";
32   case ppc64:   return "powerpc64";
33   case ppc:     return "powerpc";
34   case sparc:   return "sparc";
35   case systemz: return "s390x";
36   case thumb:   return "thumb";
37   case x86:     return "i386";
38   case x86_64:  return "x86_64";
39   case xcore:   return "xcore";
40   }
41
42   return "<invalid>";
43 }
44
45 const char *Triple::getVendorTypeName(VendorType Kind) {
46   switch (Kind) {
47   case UnknownVendor: return "unknown";
48
49   case Apple: return "apple";
50   case PC: return "pc";
51   }
52
53   return "<invalid>";
54 }
55
56 const char *Triple::getOSTypeName(OSType Kind) {
57   switch (Kind) {
58   case UnknownOS: return "unknown";
59
60   case AuroraUX: return "auroraux";
61   case Cygwin: return "cygwin";
62   case Darwin: return "darwin";
63   case DragonFly: return "dragonfly";
64   case FreeBSD: return "freebsd";
65   case Linux: return "linux";
66   case MinGW32: return "mingw32";
67   case MinGW64: return "mingw64";
68   case NetBSD: return "netbsd";
69   case OpenBSD: return "openbsd";
70   case Solaris: return "solaris";
71   case Win32: return "win32";
72   }
73
74   return "<invalid>";
75 }
76
77 Triple::ArchType Triple::getArchTypeForLLVMName(const StringRef &Name) {
78   if (Name == "alpha")
79     return alpha;
80   if (Name == "arm")
81     return arm;
82   if (Name == "bfin")
83     return bfin;
84   if (Name == "cellspu")
85     return cellspu;
86   if (Name == "mips")
87     return mips;
88   if (Name == "mipsel")
89     return mipsel;
90   if (Name == "msp430")
91     return msp430;
92   if (Name == "pic16")
93     return pic16;
94   if (Name == "ppc64")
95     return ppc64;
96   if (Name == "ppc")
97     return ppc;
98   if (Name == "sparc")
99     return sparc;
100   if (Name == "systemz")
101     return systemz;
102   if (Name == "thumb")
103     return thumb;
104   if (Name == "x86")
105     return x86;
106   if (Name == "x86-64")
107     return x86_64;
108   if (Name == "xcore")
109     return xcore;
110
111   return UnknownArch;
112 }
113
114 //
115
116 void Triple::Parse() const {
117   assert(!isInitialized() && "Invalid parse call.");
118
119   StringRef ArchName = getArchName();
120   if (ArchName.size() == 4 && ArchName[0] == 'i' && 
121       ArchName[2] == '8' && ArchName[3] == '6' && 
122       ArchName[1] - '3' < 6) // i[3-9]86
123     Arch = x86;
124   else if (ArchName == "amd64" || ArchName == "x86_64")
125     Arch = x86_64;
126   else if (ArchName == "pic16")
127     Arch = pic16;
128   else if (ArchName == "powerpc")
129     Arch = ppc;
130   else if (ArchName == "powerpc64")
131     Arch = ppc64;
132   else if (ArchName == "arm" ||
133            ArchName.startswith("armv") ||
134            ArchName == "xscale")
135     Arch = arm;
136   else if (ArchName == "thumb" ||
137            ArchName.startswith("thumbv"))
138     Arch = thumb;
139   else if (ArchName.startswith("alpha"))
140     Arch = alpha;
141   else if (ArchName == "spu" || ArchName == "cellspu")
142     Arch = cellspu;
143   else if (ArchName == "msp430")
144     Arch = msp430;
145   else if (ArchName == "mips" || ArchName == "mipsallegrex")
146     Arch = mips;
147   else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
148            ArchName == "psp")
149     Arch = mipsel;
150   else if (ArchName == "sparc")
151     Arch = sparc;
152   else if (ArchName == "s390x")
153     Arch = systemz;
154   else
155     Arch = UnknownArch;
156
157   StringRef VendorName = getVendorName();
158   if (VendorName == "apple")
159     Vendor = Apple;
160   else if (VendorName == "pc")
161     Vendor = PC;
162   else
163     Vendor = UnknownVendor;
164
165   StringRef OSName = getOSName();
166   if (OSName.startswith("auroraux"))
167     OS = AuroraUX;
168   else if (OSName.startswith("cygwin"))
169     OS = Cygwin;
170   else if (OSName.startswith("darwin"))
171     OS = Darwin;
172   else if (OSName.startswith("dragonfly"))
173     OS = DragonFly;
174   else if (OSName.startswith("freebsd"))
175     OS = FreeBSD;
176   else if (OSName.startswith("linux"))
177     OS = Linux;
178   else if (OSName.startswith("mingw32"))
179     OS = MinGW32;
180   else if (OSName.startswith("mingw64"))
181     OS = MinGW64;
182   else if (OSName.startswith("netbsd"))
183     OS = NetBSD;
184   else if (OSName.startswith("openbsd"))
185     OS = OpenBSD;
186   else if (OSName.startswith("solaris"))
187     OS = Solaris;
188   else if (OSName.startswith("win32"))
189     OS = Win32;
190   else
191     OS = UnknownOS;
192
193   assert(isInitialized() && "Failed to initialize!");
194 }
195
196 StringRef Triple::getArchName() const {
197   return StringRef(Data).split('-').first;           // Isolate first component
198 }
199
200 StringRef Triple::getVendorName() const {
201   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
202   return Tmp.split('-').first;                       // Isolate second component
203 }
204
205 StringRef Triple::getOSName() const {
206   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
207   Tmp = Tmp.split('-').second;                       // Strip second component
208   return Tmp.split('-').first;                       // Isolate third component
209 }
210
211 StringRef Triple::getEnvironmentName() const {
212   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
213   Tmp = Tmp.split('-').second;                       // Strip second component
214   return Tmp.split('-').second;                      // Strip third component
215 }
216
217 StringRef Triple::getOSAndEnvironmentName() const {
218   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
219   return Tmp.split('-').second;                      // Strip second component
220 }
221
222 static unsigned EatNumber(StringRef &Str) {
223   assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
224   unsigned Result = Str[0]-'0';
225   
226   // Eat the digit.
227   Str = Str.substr(1);
228   
229   // Handle "darwin11".
230   if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
231     Result = Result*10 + (Str[0] - '0');
232     // Eat the digit.
233     Str = Str.substr(1);
234   }
235   
236   return Result;
237 }
238
239 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
240 /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
241 /// not defined, return 0's.  This requires that the triple have an OSType of
242 /// darwin before it is called.
243 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
244                              unsigned &Revision) const {
245   assert(getOS() == Darwin && "Not a darwin target triple!");
246   StringRef OSName = getOSName();
247   assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
248   
249   // Strip off "darwin".
250   OSName = OSName.substr(6);
251   
252   Maj = Min = Revision = 0;
253
254   if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
255     return;
256
257   // The major version is the first digit.
258   Maj = EatNumber(OSName);
259   if (OSName.empty()) return;
260   
261   // Handle minor version: 10.4.9 -> darwin8.9.
262   if (OSName[0] != '.')
263     return;
264   
265   // Eat the '.'.
266   OSName = OSName.substr(1);
267
268   if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
269     return;
270   
271   Min = EatNumber(OSName);
272   if (OSName.empty()) return;
273
274   // Handle revision darwin8.9.1
275   if (OSName[0] != '.')
276     return;
277   
278   // Eat the '.'.
279   OSName = OSName.substr(1);
280   
281   if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
282     return;
283
284   Revision = EatNumber(OSName);
285 }
286
287 void Triple::setTriple(const Twine &Str) {
288   Data = Str.str();
289   Arch = InvalidArch;
290 }
291
292 void Triple::setArch(ArchType Kind) {
293   setArchName(getArchTypeName(Kind));
294 }
295
296 void Triple::setVendor(VendorType Kind) {
297   setVendorName(getVendorTypeName(Kind));
298 }
299
300 void Triple::setOS(OSType Kind) {
301   setOSName(getOSTypeName(Kind));
302 }
303
304 void Triple::setArchName(const StringRef &Str) {
305   setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
306 }
307
308 void Triple::setVendorName(const StringRef &Str) {
309   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
310 }
311
312 void Triple::setOSName(const StringRef &Str) {
313   if (hasEnvironment())
314     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
315               "-" + getEnvironmentName());
316   else
317     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
318 }
319
320 void Triple::setEnvironmentName(const StringRef &Str) {
321   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 
322             "-" + Str);
323 }
324
325 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
326   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
327 }