Fix Triple to recognize the 'bfin' 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 == "bfin")
127     Arch = bfin;
128   else if (ArchName == "pic16")
129     Arch = pic16;
130   else if (ArchName == "powerpc")
131     Arch = ppc;
132   else if (ArchName == "powerpc64")
133     Arch = ppc64;
134   else if (ArchName == "arm" ||
135            ArchName.startswith("armv") ||
136            ArchName == "xscale")
137     Arch = arm;
138   else if (ArchName == "thumb" ||
139            ArchName.startswith("thumbv"))
140     Arch = thumb;
141   else if (ArchName.startswith("alpha"))
142     Arch = alpha;
143   else if (ArchName == "spu" || ArchName == "cellspu")
144     Arch = cellspu;
145   else if (ArchName == "msp430")
146     Arch = msp430;
147   else if (ArchName == "mips" || ArchName == "mipsallegrex")
148     Arch = mips;
149   else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
150            ArchName == "psp")
151     Arch = mipsel;
152   else if (ArchName == "sparc")
153     Arch = sparc;
154   else if (ArchName == "s390x")
155     Arch = systemz;
156   else
157     Arch = UnknownArch;
158
159   StringRef VendorName = getVendorName();
160   if (VendorName == "apple")
161     Vendor = Apple;
162   else if (VendorName == "pc")
163     Vendor = PC;
164   else
165     Vendor = UnknownVendor;
166
167   StringRef OSName = getOSName();
168   if (OSName.startswith("auroraux"))
169     OS = AuroraUX;
170   else if (OSName.startswith("cygwin"))
171     OS = Cygwin;
172   else if (OSName.startswith("darwin"))
173     OS = Darwin;
174   else if (OSName.startswith("dragonfly"))
175     OS = DragonFly;
176   else if (OSName.startswith("freebsd"))
177     OS = FreeBSD;
178   else if (OSName.startswith("linux"))
179     OS = Linux;
180   else if (OSName.startswith("mingw32"))
181     OS = MinGW32;
182   else if (OSName.startswith("mingw64"))
183     OS = MinGW64;
184   else if (OSName.startswith("netbsd"))
185     OS = NetBSD;
186   else if (OSName.startswith("openbsd"))
187     OS = OpenBSD;
188   else if (OSName.startswith("solaris"))
189     OS = Solaris;
190   else if (OSName.startswith("win32"))
191     OS = Win32;
192   else
193     OS = UnknownOS;
194
195   assert(isInitialized() && "Failed to initialize!");
196 }
197
198 StringRef Triple::getArchName() const {
199   return StringRef(Data).split('-').first;           // Isolate first component
200 }
201
202 StringRef Triple::getVendorName() const {
203   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
204   return Tmp.split('-').first;                       // Isolate second component
205 }
206
207 StringRef Triple::getOSName() const {
208   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
209   Tmp = Tmp.split('-').second;                       // Strip second component
210   return Tmp.split('-').first;                       // Isolate third component
211 }
212
213 StringRef Triple::getEnvironmentName() const {
214   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
215   Tmp = Tmp.split('-').second;                       // Strip second component
216   return Tmp.split('-').second;                      // Strip third component
217 }
218
219 StringRef Triple::getOSAndEnvironmentName() const {
220   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
221   return Tmp.split('-').second;                      // Strip second component
222 }
223
224 static unsigned EatNumber(StringRef &Str) {
225   assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
226   unsigned Result = Str[0]-'0';
227   
228   // Eat the digit.
229   Str = Str.substr(1);
230   
231   // Handle "darwin11".
232   if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
233     Result = Result*10 + (Str[0] - '0');
234     // Eat the digit.
235     Str = Str.substr(1);
236   }
237   
238   return Result;
239 }
240
241 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
242 /// triple.  For example, if we have darwin8.5 return 8,5,0.  If any entry is
243 /// not defined, return 0's.  This requires that the triple have an OSType of
244 /// darwin before it is called.
245 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
246                              unsigned &Revision) const {
247   assert(getOS() == Darwin && "Not a darwin target triple!");
248   StringRef OSName = getOSName();
249   assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
250   
251   // Strip off "darwin".
252   OSName = OSName.substr(6);
253   
254   Maj = Min = Revision = 0;
255
256   if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
257     return;
258
259   // The major version is the first digit.
260   Maj = EatNumber(OSName);
261   if (OSName.empty()) return;
262   
263   // Handle minor version: 10.4.9 -> darwin8.9.
264   if (OSName[0] != '.')
265     return;
266   
267   // Eat the '.'.
268   OSName = OSName.substr(1);
269
270   if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
271     return;
272   
273   Min = EatNumber(OSName);
274   if (OSName.empty()) return;
275
276   // Handle revision darwin8.9.1
277   if (OSName[0] != '.')
278     return;
279   
280   // Eat the '.'.
281   OSName = OSName.substr(1);
282   
283   if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
284     return;
285
286   Revision = EatNumber(OSName);
287 }
288
289 void Triple::setTriple(const Twine &Str) {
290   Data = Str.str();
291   Arch = InvalidArch;
292 }
293
294 void Triple::setArch(ArchType Kind) {
295   setArchName(getArchTypeName(Kind));
296 }
297
298 void Triple::setVendor(VendorType Kind) {
299   setVendorName(getVendorTypeName(Kind));
300 }
301
302 void Triple::setOS(OSType Kind) {
303   setOSName(getOSTypeName(Kind));
304 }
305
306 void Triple::setArchName(const StringRef &Str) {
307   setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
308 }
309
310 void Triple::setVendorName(const StringRef &Str) {
311   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
312 }
313
314 void Triple::setOSName(const StringRef &Str) {
315   if (hasEnvironment())
316     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
317               "-" + getEnvironmentName());
318   else
319     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
320 }
321
322 void Triple::setEnvironmentName(const StringRef &Str) {
323   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 
324             "-" + Str);
325 }
326
327 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
328   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
329 }