1 //===--- Triple.cpp - Target triple helper class --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Triple.h"
17 const char *Triple::getArchTypeName(ArchType 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";
31 const char *Triple::getVendorTypeName(VendorType Kind
) {
33 case UnknownVendor
: return "unknown";
35 case Apple
: return "apple";
42 const char *Triple::getOSTypeName(OSType Kind
) {
44 case UnknownOS
: return "unknown";
46 case Darwin
: return "darwin";
47 case FreeBSD
: return "freebsd";
48 case Linux
: return "linux";
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')
63 else if (ArchName
== "amd64" || ArchName
== "x86_64")
65 else if (ArchName
== "powerpc")
67 else if (ArchName
== "powerpc64")
72 std::string VendorName
= getVendorName();
73 if (VendorName
== "apple")
75 else if (VendorName
== "pc")
78 Vendor
= UnknownVendor
;
80 std::string OSName
= getOSName();
81 if (memcmp(&OSName
[0], "darwin", 6) == 0)
83 else if (memcmp(&OSName
[0], "freebsd", 7) == 0)
85 else if (memcmp(&OSName
[0], "linux", 5) == 0)
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
)
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
)
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
) {
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());
174 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
);
177 void Triple::setEnvironmentName(const std::string
&Str
) {
178 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
182 void Triple::setOSAndEnvironmentName(const std::string
&Str
) {
183 setTriple(getArchName() + "-" + getVendorName() + "-" + Str
);