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