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