Implement bswap
[llvm/msp430.git] / lib / Support / Triple.cpp
blob5310c3a6e73ed1e324f21bb3192ff6577d9bcc70
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 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
11 #include <cassert>
12 #include <cstring>
13 using namespace llvm;
17 const char *Triple::getArchTypeName(ArchType Kind) {
18 switch (Kind) {
19 case InvalidArch: return "<invalid>";
20 case UnknownArch: return "unknown";
22 case x86: return "i386";
23 case x86_64: return "x86_64";
24 case ppc: return "powerpc";
25 case ppc64: return "powerpc64";
28 return "<invalid>";
31 const char *Triple::getVendorTypeName(VendorType Kind) {
32 switch (Kind) {
33 case UnknownVendor: return "unknown";
35 case Apple: return "apple";
36 case PC: return "PC";
39 return "<invalid>";
42 const char *Triple::getOSTypeName(OSType Kind) {
43 switch (Kind) {
44 case UnknownOS: return "unknown";
46 case Darwin: return "darwin";
47 case FreeBSD: return "freebsd";
48 case Linux: return "linux";
51 return "<invalid>";
56 void Triple::Parse() const {
57 assert(!isInitialized() && "Invalid parse call.");
59 std::string ArchName = getArchName();
60 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
61 ArchName[2] == '8' && ArchName[3] == '6')
62 Arch = x86;
63 else if (ArchName == "amd64" || ArchName == "x86_64")
64 Arch = x86_64;
65 else if (ArchName == "powerpc")
66 Arch = ppc;
67 else if (ArchName == "powerpc64")
68 Arch = ppc64;
69 else
70 Arch = UnknownArch;
72 std::string VendorName = getVendorName();
73 if (VendorName == "apple")
74 Vendor = Apple;
75 else if (VendorName == "pc")
76 Vendor = PC;
77 else
78 Vendor = UnknownVendor;
80 std::string OSName = getOSName();
81 if (memcmp(&OSName[0], "darwin", 6) == 0)
82 OS = Darwin;
83 else if (memcmp(&OSName[0], "freebsd", 7) == 0)
84 OS = FreeBSD;
85 else if (memcmp(&OSName[0], "linux", 5) == 0)
86 OS = Linux;
87 else
88 OS = UnknownOS;
90 assert(isInitialized() && "Failed to initialize!");
93 static std::string extract(const std::string &A,
94 std::string::size_type begin,
95 std::string::size_type end) {
96 if (begin == std::string::npos)
97 return "";
98 if (end == std::string::npos)
99 return A.substr(begin);
100 return A.substr(begin, end - begin);
103 static std::string extract1(const std::string &A,
104 std::string::size_type begin,
105 std::string::size_type end) {
106 if (begin == std::string::npos || begin == end)
107 return "";
108 return extract(A, begin + 1, end);
111 std::string Triple::getArchName() const {
112 std::string Tmp = Data;
113 return extract(Tmp, 0, Tmp.find('-'));
116 std::string Triple::getVendorName() const {
117 std::string Tmp = Data;
118 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
119 return extract(Tmp, 0, Tmp.find('-'));
122 std::string Triple::getOSName() const {
123 std::string Tmp = Data;
124 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
125 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
126 return extract(Tmp, 0, Tmp.find('-'));
129 std::string Triple::getEnvironmentName() const {
130 std::string Tmp = Data;
131 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
132 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
133 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
134 return extract(Tmp, 0, std::string::npos);
137 std::string Triple::getOSAndEnvironmentName() const {
138 std::string Tmp = Data;
139 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
140 Tmp = extract1(Tmp, Tmp.find('-'), std::string::npos);
141 return extract(Tmp, 0, std::string::npos);
144 void Triple::setTriple(const std::string &Str) {
145 Data = Str;
146 Arch = InvalidArch;
149 void Triple::setArch(ArchType Kind) {
150 setArchName(getArchTypeName(Kind));
153 void Triple::setVendor(VendorType Kind) {
154 setVendorName(getVendorTypeName(Kind));
157 void Triple::setOS(OSType Kind) {
158 setOSName(getOSTypeName(Kind));
161 void Triple::setArchName(const std::string &Str) {
162 setTriple(Str + "-" + getVendorName() + "-" + getOSAndEnvironmentName());
165 void Triple::setVendorName(const std::string &Str) {
166 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
169 void Triple::setOSName(const std::string &Str) {
170 if (hasEnvironment())
171 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
172 "-" + getEnvironmentName());
173 else
174 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
177 void Triple::setEnvironmentName(const std::string &Str) {
178 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
179 "-" + Str);
182 void Triple::setOSAndEnvironmentName(const std::string &Str) {
183 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);