1 //===----------- Triple.cpp - Triple unit tests ---------------------------===//
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 "gtest/gtest.h"
11 #include "llvm/ADT/Triple.h"
12
13 using namespace llvm;
14
15 namespace {
16
TEST(TripleTest,BasicParsing)17 TEST(TripleTest, BasicParsing) {
18 Triple T;
19
20 T = Triple("");
21 EXPECT_EQ("", T.getArchName().str());
22 EXPECT_EQ("", T.getVendorName().str());
23 EXPECT_EQ("", T.getOSName().str());
24 EXPECT_EQ("", T.getEnvironmentName().str());
25
26 T = Triple("-");
27 EXPECT_EQ("", T.getArchName().str());
28 EXPECT_EQ("", T.getVendorName().str());
29 EXPECT_EQ("", T.getOSName().str());
30 EXPECT_EQ("", T.getEnvironmentName().str());
31
32 T = Triple("--");
33 EXPECT_EQ("", T.getArchName().str());
34 EXPECT_EQ("", T.getVendorName().str());
35 EXPECT_EQ("", T.getOSName().str());
36 EXPECT_EQ("", T.getEnvironmentName().str());
37
38 T = Triple("---");
39 EXPECT_EQ("", T.getArchName().str());
40 EXPECT_EQ("", T.getVendorName().str());
41 EXPECT_EQ("", T.getOSName().str());
42 EXPECT_EQ("", T.getEnvironmentName().str());
43
44 T = Triple("----");
45 EXPECT_EQ("", T.getArchName().str());
46 EXPECT_EQ("", T.getVendorName().str());
47 EXPECT_EQ("", T.getOSName().str());
48 EXPECT_EQ("-", T.getEnvironmentName().str());
49
50 T = Triple("a");
51 EXPECT_EQ("a", T.getArchName().str());
52 EXPECT_EQ("", T.getVendorName().str());
53 EXPECT_EQ("", T.getOSName().str());
54 EXPECT_EQ("", T.getEnvironmentName().str());
55
56 T = Triple("a-b");
57 EXPECT_EQ("a", T.getArchName().str());
58 EXPECT_EQ("b", T.getVendorName().str());
59 EXPECT_EQ("", T.getOSName().str());
60 EXPECT_EQ("", T.getEnvironmentName().str());
61
62 T = Triple("a-b-c");
63 EXPECT_EQ("a", T.getArchName().str());
64 EXPECT_EQ("b", T.getVendorName().str());
65 EXPECT_EQ("c", T.getOSName().str());
66 EXPECT_EQ("", T.getEnvironmentName().str());
67
68 T = Triple("a-b-c-d");
69 EXPECT_EQ("a", T.getArchName().str());
70 EXPECT_EQ("b", T.getVendorName().str());
71 EXPECT_EQ("c", T.getOSName().str());
72 EXPECT_EQ("d", T.getEnvironmentName().str());
73 }
74
TEST(TripleTest,ParsedIDs)75 TEST(TripleTest, ParsedIDs) {
76 Triple T;
77
78 T = Triple("i386-apple-darwin");
79 EXPECT_EQ(Triple::x86, T.getArch());
80 EXPECT_EQ(Triple::Apple, T.getVendor());
81 EXPECT_EQ(Triple::Darwin, T.getOS());
82 EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
83
84 T = Triple("x86_64-pc-linux-gnu");
85 EXPECT_EQ(Triple::x86_64, T.getArch());
86 EXPECT_EQ(Triple::PC, T.getVendor());
87 EXPECT_EQ(Triple::Linux, T.getOS());
88 EXPECT_EQ(Triple::GNU, T.getEnvironment());
89
90 T = Triple("powerpc-bgp-linux");
91 EXPECT_EQ(Triple::ppc, T.getArch());
92 EXPECT_EQ(Triple::BGP, T.getVendor());
93 EXPECT_EQ(Triple::Linux, T.getOS());
94 EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
95
96 T = Triple("powerpc-bgp-cnk");
97 EXPECT_EQ(Triple::ppc, T.getArch());
98 EXPECT_EQ(Triple::BGP, T.getVendor());
99 EXPECT_EQ(Triple::CNK, T.getOS());
100 EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
101
102 T = Triple("powerpc64-bgq-linux");
103 EXPECT_EQ(Triple::ppc64, T.getArch());
104 EXPECT_EQ(Triple::BGQ, T.getVendor());
105 EXPECT_EQ(Triple::Linux, T.getOS());
106 EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
107
108 T = Triple("powerpc-dunno-notsure");
109 EXPECT_EQ(Triple::ppc, T.getArch());
110 EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
111 EXPECT_EQ(Triple::UnknownOS, T.getOS());
112 EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
113
114 T = Triple("arm-none-none-eabi");
115 EXPECT_EQ(Triple::arm, T.getArch());
116 EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
117 EXPECT_EQ(Triple::UnknownOS, T.getOS());
118 EXPECT_EQ(Triple::EABI, T.getEnvironment());
119
120 T = Triple("huh");
121 EXPECT_EQ(Triple::UnknownArch, T.getArch());
122 }
123
Join(StringRef A,StringRef B,StringRef C)124 static std::string Join(StringRef A, StringRef B, StringRef C) {
125 std::string Str = A; Str += '-'; Str += B; Str += '-'; Str += C;
126 return Str;
127 }
128
Join(StringRef A,StringRef B,StringRef C,StringRef D)129 static std::string Join(StringRef A, StringRef B, StringRef C, StringRef D) {
130 std::string Str = A; Str += '-'; Str += B; Str += '-'; Str += C; Str += '-';
131 Str += D; return Str;
132 }
133
TEST(TripleTest,Normalization)134 TEST(TripleTest, Normalization) {
135
136 EXPECT_EQ("", Triple::normalize(""));
137 EXPECT_EQ("-", Triple::normalize("-"));
138 EXPECT_EQ("--", Triple::normalize("--"));
139 EXPECT_EQ("---", Triple::normalize("---"));
140 EXPECT_EQ("----", Triple::normalize("----"));
141
142 EXPECT_EQ("a", Triple::normalize("a"));
143 EXPECT_EQ("a-b", Triple::normalize("a-b"));
144 EXPECT_EQ("a-b-c", Triple::normalize("a-b-c"));
145 EXPECT_EQ("a-b-c-d", Triple::normalize("a-b-c-d"));
146
147 EXPECT_EQ("i386-b-c", Triple::normalize("i386-b-c"));
148 EXPECT_EQ("i386-a-c", Triple::normalize("a-i386-c"));
149 EXPECT_EQ("i386-a-b", Triple::normalize("a-b-i386"));
150 EXPECT_EQ("i386-a-b-c", Triple::normalize("a-b-c-i386"));
151
152 EXPECT_EQ("a-pc-c", Triple::normalize("a-pc-c"));
153 EXPECT_EQ("-pc-b-c", Triple::normalize("pc-b-c"));
154 EXPECT_EQ("a-pc-b", Triple::normalize("a-b-pc"));
155 EXPECT_EQ("a-pc-b-c", Triple::normalize("a-b-c-pc"));
156
157 EXPECT_EQ("a-b-linux", Triple::normalize("a-b-linux"));
158 EXPECT_EQ("--linux-b-c", Triple::normalize("linux-b-c"));
159 EXPECT_EQ("a--linux-c", Triple::normalize("a-linux-c"));
160
161 EXPECT_EQ("i386-pc-a", Triple::normalize("a-pc-i386"));
162 EXPECT_EQ("i386-pc-", Triple::normalize("-pc-i386"));
163 EXPECT_EQ("-pc-linux-c", Triple::normalize("linux-pc-c"));
164 EXPECT_EQ("-pc-linux", Triple::normalize("linux-pc-"));
165
166 EXPECT_EQ("i386", Triple::normalize("i386"));
167 EXPECT_EQ("-pc", Triple::normalize("pc"));
168 EXPECT_EQ("--linux", Triple::normalize("linux"));
169
170 EXPECT_EQ("x86_64--linux-gnu", Triple::normalize("x86_64-gnu-linux"));
171
172 // Check that normalizing a permutated set of valid components returns a
173 // triple with the unpermuted components.
174 StringRef C[4];
175 for (int Arch = 1+Triple::UnknownArch; Arch <= Triple::amdil; ++Arch) {
176 C[0] = Triple::getArchTypeName(Triple::ArchType(Arch));
177 for (int Vendor = 1+Triple::UnknownVendor; Vendor <= Triple::PC;
178 ++Vendor) {
179 C[1] = Triple::getVendorTypeName(Triple::VendorType(Vendor));
180 for (int OS = 1+Triple::UnknownOS; OS <= Triple::Minix; ++OS) {
181 C[2] = Triple::getOSTypeName(Triple::OSType(OS));
182
183 std::string E = Join(C[0], C[1], C[2]);
184 EXPECT_EQ(E, Triple::normalize(Join(C[0], C[1], C[2])));
185
186 EXPECT_EQ(E, Triple::normalize(Join(C[0], C[2], C[1])));
187 EXPECT_EQ(E, Triple::normalize(Join(C[1], C[2], C[0])));
188 EXPECT_EQ(E, Triple::normalize(Join(C[1], C[0], C[2])));
189 EXPECT_EQ(E, Triple::normalize(Join(C[2], C[0], C[1])));
190 EXPECT_EQ(E, Triple::normalize(Join(C[2], C[1], C[0])));
191
192 for (int Env = 1+Triple::UnknownEnvironment; Env <= Triple::MachO;
193 ++Env) {
194 C[3] = Triple::getEnvironmentTypeName(Triple::EnvironmentType(Env));
195
196 std::string F = Join(C[0], C[1], C[2], C[3]);
197 EXPECT_EQ(F, Triple::normalize(Join(C[0], C[1], C[2], C[3])));
198
199 EXPECT_EQ(F, Triple::normalize(Join(C[0], C[1], C[3], C[2])));
200 EXPECT_EQ(F, Triple::normalize(Join(C[0], C[2], C[3], C[1])));
201 EXPECT_EQ(F, Triple::normalize(Join(C[0], C[2], C[1], C[3])));
202 EXPECT_EQ(F, Triple::normalize(Join(C[0], C[3], C[1], C[2])));
203 EXPECT_EQ(F, Triple::normalize(Join(C[0], C[3], C[2], C[1])));
204 EXPECT_EQ(F, Triple::normalize(Join(C[1], C[2], C[3], C[0])));
205 EXPECT_EQ(F, Triple::normalize(Join(C[1], C[2], C[0], C[3])));
206 EXPECT_EQ(F, Triple::normalize(Join(C[1], C[3], C[0], C[2])));
207 EXPECT_EQ(F, Triple::normalize(Join(C[1], C[3], C[2], C[0])));
208 EXPECT_EQ(F, Triple::normalize(Join(C[1], C[0], C[2], C[3])));
209 EXPECT_EQ(F, Triple::normalize(Join(C[1], C[0], C[3], C[2])));
210 EXPECT_EQ(F, Triple::normalize(Join(C[2], C[3], C[0], C[1])));
211 EXPECT_EQ(F, Triple::normalize(Join(C[2], C[3], C[1], C[0])));
212 EXPECT_EQ(F, Triple::normalize(Join(C[2], C[0], C[1], C[3])));
213 EXPECT_EQ(F, Triple::normalize(Join(C[2], C[0], C[3], C[1])));
214 EXPECT_EQ(F, Triple::normalize(Join(C[2], C[1], C[3], C[0])));
215 EXPECT_EQ(F, Triple::normalize(Join(C[2], C[1], C[0], C[3])));
216 EXPECT_EQ(F, Triple::normalize(Join(C[3], C[0], C[1], C[2])));
217 EXPECT_EQ(F, Triple::normalize(Join(C[3], C[0], C[2], C[1])));
218 EXPECT_EQ(F, Triple::normalize(Join(C[3], C[1], C[2], C[0])));
219 EXPECT_EQ(F, Triple::normalize(Join(C[3], C[1], C[0], C[2])));
220 EXPECT_EQ(F, Triple::normalize(Join(C[3], C[2], C[0], C[1])));
221 EXPECT_EQ(F, Triple::normalize(Join(C[3], C[2], C[1], C[0])));
222 }
223 }
224 }
225 }
226
227 // Various real-world funky triples. The value returned by GCC's config.sub
228 // is given in the comment.
229 EXPECT_EQ("i386--mingw32", Triple::normalize("i386-mingw32")); // i386-pc-mingw32
230 EXPECT_EQ("x86_64--linux-gnu", Triple::normalize("x86_64-linux-gnu")); // x86_64-pc-linux-gnu
231 EXPECT_EQ("i486--linux-gnu", Triple::normalize("i486-linux-gnu")); // i486-pc-linux-gnu
232 EXPECT_EQ("i386-redhat-linux", Triple::normalize("i386-redhat-linux")); // i386-redhat-linux-gnu
233 EXPECT_EQ("i686--linux", Triple::normalize("i686-linux")); // i686-pc-linux-gnu
234 EXPECT_EQ("arm-none--eabi", Triple::normalize("arm-none-eabi")); // arm-none-eabi
235 }
236
TEST(TripleTest,MutateName)237 TEST(TripleTest, MutateName) {
238 Triple T;
239 EXPECT_EQ(Triple::UnknownArch, T.getArch());
240 EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
241 EXPECT_EQ(Triple::UnknownOS, T.getOS());
242 EXPECT_EQ(Triple::UnknownEnvironment, T.getEnvironment());
243
244 T.setArchName("i386");
245 EXPECT_EQ(Triple::x86, T.getArch());
246 EXPECT_EQ("i386--", T.getTriple());
247
248 T.setVendorName("pc");
249 EXPECT_EQ(Triple::x86, T.getArch());
250 EXPECT_EQ(Triple::PC, T.getVendor());
251 EXPECT_EQ("i386-pc-", T.getTriple());
252
253 T.setOSName("linux");
254 EXPECT_EQ(Triple::x86, T.getArch());
255 EXPECT_EQ(Triple::PC, T.getVendor());
256 EXPECT_EQ(Triple::Linux, T.getOS());
257 EXPECT_EQ("i386-pc-linux", T.getTriple());
258
259 T.setEnvironmentName("gnu");
260 EXPECT_EQ(Triple::x86, T.getArch());
261 EXPECT_EQ(Triple::PC, T.getVendor());
262 EXPECT_EQ(Triple::Linux, T.getOS());
263 EXPECT_EQ("i386-pc-linux-gnu", T.getTriple());
264
265 T.setOSName("freebsd");
266 EXPECT_EQ(Triple::x86, T.getArch());
267 EXPECT_EQ(Triple::PC, T.getVendor());
268 EXPECT_EQ(Triple::FreeBSD, T.getOS());
269 EXPECT_EQ("i386-pc-freebsd-gnu", T.getTriple());
270
271 T.setOSAndEnvironmentName("darwin");
272 EXPECT_EQ(Triple::x86, T.getArch());
273 EXPECT_EQ(Triple::PC, T.getVendor());
274 EXPECT_EQ(Triple::Darwin, T.getOS());
275 EXPECT_EQ("i386-pc-darwin", T.getTriple());
276
277 }
278
TEST(TripleTest,BitWidthPredicates)279 TEST(TripleTest, BitWidthPredicates) {
280 Triple T;
281 EXPECT_FALSE(T.isArch16Bit());
282 EXPECT_FALSE(T.isArch32Bit());
283 EXPECT_FALSE(T.isArch64Bit());
284
285 T.setArch(Triple::arm);
286 EXPECT_FALSE(T.isArch16Bit());
287 EXPECT_TRUE(T.isArch32Bit());
288 EXPECT_FALSE(T.isArch64Bit());
289
290 T.setArch(Triple::hexagon);
291 EXPECT_FALSE(T.isArch16Bit());
292 EXPECT_TRUE(T.isArch32Bit());
293 EXPECT_FALSE(T.isArch64Bit());
294
295 T.setArch(Triple::mips);
296 EXPECT_FALSE(T.isArch16Bit());
297 EXPECT_TRUE(T.isArch32Bit());
298 EXPECT_FALSE(T.isArch64Bit());
299
300 T.setArch(Triple::mips64);
301 EXPECT_FALSE(T.isArch16Bit());
302 EXPECT_FALSE(T.isArch32Bit());
303 EXPECT_TRUE(T.isArch64Bit());
304
305 T.setArch(Triple::msp430);
306 EXPECT_TRUE(T.isArch16Bit());
307 EXPECT_FALSE(T.isArch32Bit());
308 EXPECT_FALSE(T.isArch64Bit());
309
310 T.setArch(Triple::ppc);
311 EXPECT_FALSE(T.isArch16Bit());
312 EXPECT_TRUE(T.isArch32Bit());
313 EXPECT_FALSE(T.isArch64Bit());
314
315 T.setArch(Triple::ppc64);
316 EXPECT_FALSE(T.isArch16Bit());
317 EXPECT_FALSE(T.isArch32Bit());
318 EXPECT_TRUE(T.isArch64Bit());
319
320 T.setArch(Triple::x86);
321 EXPECT_FALSE(T.isArch16Bit());
322 EXPECT_TRUE(T.isArch32Bit());
323 EXPECT_FALSE(T.isArch64Bit());
324
325 T.setArch(Triple::x86_64);
326 EXPECT_FALSE(T.isArch16Bit());
327 EXPECT_FALSE(T.isArch32Bit());
328 EXPECT_TRUE(T.isArch64Bit());
329 }
330
TEST(TripleTest,BitWidthArchVariants)331 TEST(TripleTest, BitWidthArchVariants) {
332 Triple T;
333 EXPECT_EQ(Triple::UnknownArch, T.get32BitArchVariant().getArch());
334 EXPECT_EQ(Triple::UnknownArch, T.get64BitArchVariant().getArch());
335
336 T.setArch(Triple::UnknownArch);
337 EXPECT_EQ(Triple::UnknownArch, T.get32BitArchVariant().getArch());
338 EXPECT_EQ(Triple::UnknownArch, T.get64BitArchVariant().getArch());
339
340 T.setArch(Triple::arm);
341 EXPECT_EQ(Triple::arm, T.get32BitArchVariant().getArch());
342 EXPECT_EQ(Triple::UnknownArch, T.get64BitArchVariant().getArch());
343
344 T.setArch(Triple::mips);
345 EXPECT_EQ(Triple::mips, T.get32BitArchVariant().getArch());
346 EXPECT_EQ(Triple::mips64, T.get64BitArchVariant().getArch());
347
348 T.setArch(Triple::mipsel);
349 EXPECT_EQ(Triple::mipsel, T.get32BitArchVariant().getArch());
350 EXPECT_EQ(Triple::mips64el, T.get64BitArchVariant().getArch());
351
352 T.setArch(Triple::ppc);
353 EXPECT_EQ(Triple::ppc, T.get32BitArchVariant().getArch());
354 EXPECT_EQ(Triple::ppc64, T.get64BitArchVariant().getArch());
355
356 T.setArch(Triple::ptx32);
357 EXPECT_EQ(Triple::ptx32, T.get32BitArchVariant().getArch());
358 EXPECT_EQ(Triple::ptx64, T.get64BitArchVariant().getArch());
359
360 T.setArch(Triple::sparc);
361 EXPECT_EQ(Triple::sparc, T.get32BitArchVariant().getArch());
362 EXPECT_EQ(Triple::sparcv9, T.get64BitArchVariant().getArch());
363
364 T.setArch(Triple::x86);
365 EXPECT_EQ(Triple::x86, T.get32BitArchVariant().getArch());
366 EXPECT_EQ(Triple::x86_64, T.get64BitArchVariant().getArch());
367
368 T.setArch(Triple::mips64);
369 EXPECT_EQ(Triple::mips, T.get32BitArchVariant().getArch());
370 EXPECT_EQ(Triple::mips64, T.get64BitArchVariant().getArch());
371
372 T.setArch(Triple::mips64el);
373 EXPECT_EQ(Triple::mipsel, T.get32BitArchVariant().getArch());
374 EXPECT_EQ(Triple::mips64el, T.get64BitArchVariant().getArch());
375
376 T.setArch(Triple::ppc64);
377 EXPECT_EQ(Triple::ppc, T.get32BitArchVariant().getArch());
378 EXPECT_EQ(Triple::ppc64, T.get64BitArchVariant().getArch());
379
380 T.setArch(Triple::ptx64);
381 EXPECT_EQ(Triple::ptx32, T.get32BitArchVariant().getArch());
382 EXPECT_EQ(Triple::ptx64, T.get64BitArchVariant().getArch());
383
384 T.setArch(Triple::sparcv9);
385 EXPECT_EQ(Triple::sparc, T.get32BitArchVariant().getArch());
386 EXPECT_EQ(Triple::sparcv9, T.get64BitArchVariant().getArch());
387
388 T.setArch(Triple::x86_64);
389 EXPECT_EQ(Triple::x86, T.get32BitArchVariant().getArch());
390 EXPECT_EQ(Triple::x86_64, T.get64BitArchVariant().getArch());
391 }
392
393 }
394