Analog Devices Blackfin back-end.
[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 ppc64:   return "powerpc64";
32   case ppc:     return "powerpc";
33   case sparc:   return "sparc";
34   case systemz: return "s390x";
35   case thumb:   return "thumb";
36   case x86:     return "i386";
37   case x86_64:  return "x86_64";
38   case xcore:   return "xcore";
39   }
40
41   return "<invalid>";
42 }
43
44 const char *Triple::getVendorTypeName(VendorType Kind) {
45   switch (Kind) {
46   case UnknownVendor: return "unknown";
47
48   case Apple: return "apple";
49   case PC: return "PC";
50   }
51
52   return "<invalid>";
53 }
54
55 const char *Triple::getOSTypeName(OSType Kind) {
56   switch (Kind) {
57   case UnknownOS: return "unknown";
58
59   case AuroraUX: return "auroraux";
60   case Cygwin: return "cygwin";
61   case Darwin: return "darwin";
62   case DragonFly: return "dragonfly";
63   case FreeBSD: return "freebsd";
64   case Linux: return "linux";
65   case MinGW32: return "mingw32";
66   case NetBSD: return "netbsd";
67   case OpenBSD: return "openbsd";
68   case Win32: return "win32";
69   }
70
71   return "<invalid>";
72 }
73
74 //
75
76 void Triple::Parse() const {
77   assert(!isInitialized() && "Invalid parse call.");
78
79   StringRef ArchName = getArchName();
80   if (ArchName.size() == 4 && ArchName[0] == 'i' && 
81       ArchName[2] == '8' && ArchName[3] == '6' && 
82       ArchName[1] - '3' < 6) // i[3-9]86
83     Arch = x86;
84   else if (ArchName == "amd64" || ArchName == "x86_64")
85     Arch = x86_64;
86   else if (ArchName == "powerpc")
87     Arch = ppc;
88   else if (ArchName == "powerpc64")
89     Arch = ppc64;
90   else if (ArchName == "arm" ||
91            ArchName.startswith("armv"))
92     Arch = arm;
93   else if (ArchName == "thumb" ||
94            ArchName.startswith("thumbv"))
95     Arch = thumb;
96   else if (ArchName.startswith("alpha"))
97     Arch = alpha;
98   else if (ArchName == "spu" || ArchName == "cellspu")
99     Arch = cellspu;
100   else if (ArchName == "msp430")
101     Arch = msp430;
102   else if (ArchName == "mips" || ArchName == "mipsallegrex")
103     Arch = mips;
104   else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
105            ArchName == "psp")
106     Arch = mipsel;
107   else if (ArchName == "sparc")
108     Arch = sparc;
109   else if (ArchName == "s390x")
110     Arch = systemz;
111   else if (ArchName == "bfin")
112     Arch = bfin;
113   else
114     Arch = UnknownArch;
115
116   StringRef VendorName = getVendorName();
117   if (VendorName == "apple")
118     Vendor = Apple;
119   else if (VendorName == "pc")
120     Vendor = PC;
121   else
122     Vendor = UnknownVendor;
123
124   StringRef OSName = getOSName();
125   if (OSName.startswith("auroraux"))
126     OS = AuroraUX;
127   else if (OSName.startswith("cygwin"))
128     OS = Cygwin;
129   else if (OSName.startswith("darwin"))
130     OS = Darwin;
131   else if (OSName.startswith("dragonfly"))
132     OS = DragonFly;
133   else if (OSName.startswith("freebsd"))
134     OS = FreeBSD;
135   else if (OSName.startswith("linux"))
136     OS = Linux;
137   else if (OSName.startswith("mingw32"))
138     OS = MinGW32;
139   else if (OSName.startswith("netbsd"))
140     OS = NetBSD;
141   else if (OSName.startswith("openbsd"))
142     OS = OpenBSD;
143   else if (OSName.startswith("win32"))
144     OS = Win32;
145   else
146     OS = UnknownOS;
147
148   assert(isInitialized() && "Failed to initialize!");
149 }
150
151 StringRef Triple::getArchName() const {
152   return StringRef(Data).split('-').first;           // Isolate first component
153 }
154
155 StringRef Triple::getVendorName() const {
156   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
157   return Tmp.split('-').first;                       // Isolate second component
158 }
159
160 StringRef Triple::getOSName() const {
161   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
162   Tmp = Tmp.split('-').second;                       // Strip second component
163   return Tmp.split('-').first;                       // Isolate third component
164 }
165
166 StringRef Triple::getEnvironmentName() const {
167   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
168   Tmp = Tmp.split('-').second;                       // Strip second component
169   return Tmp.split('-').second;                      // Strip third component
170 }
171
172 StringRef Triple::getOSAndEnvironmentName() const {
173   StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
174   return Tmp.split('-').second;                      // Strip second component
175 }
176
177 void Triple::setTriple(const Twine &Str) {
178   Data = Str.str();
179   Arch = InvalidArch;
180 }
181
182 void Triple::setArch(ArchType Kind) {
183   setArchName(getArchTypeName(Kind));
184 }
185
186 void Triple::setVendor(VendorType Kind) {
187   setVendorName(getVendorTypeName(Kind));
188 }
189
190 void Triple::setOS(OSType Kind) {
191   setOSName(getOSTypeName(Kind));
192 }
193
194 void Triple::setArchName(const StringRef &Str) {
195   setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
196 }
197
198 void Triple::setVendorName(const StringRef &Str) {
199   setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
200 }
201
202 void Triple::setOSName(const StringRef &Str) {
203   if (hasEnvironment())
204     setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
205               "-" + getEnvironmentName());
206   else
207     setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
208 }
209
210 void Triple::setEnvironmentName(const StringRef &Str) {
211   setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() + 
212             "-" + Str);
213 }
214
215 void Triple::setOSAndEnvironmentName(const StringRef &Str) {
216   setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
217 }