zpu: test commit
[llvm/zpu.git] / lib / Support / Triple.cpp
blobe63e4afd76d54e428ff4b9b2cb3e2db4b7292f82
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"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/Twine.h"
15 #include <cassert>
16 #include <cstring>
17 using namespace llvm;
21 const char *Triple::getArchTypeName(ArchType Kind) {
22 switch (Kind) {
23 case InvalidArch: return "<invalid>";
24 case UnknownArch: return "unknown";
26 case alpha: return "alpha";
27 case arm: return "arm";
28 case bfin: return "bfin";
29 case cellspu: return "cellspu";
30 case mips: return "mips";
31 case mipsel: return "mipsel";
32 case msp430: return "msp430";
33 case pic16: return "pic16";
34 case ppc64: return "powerpc64";
35 case ppc: return "powerpc";
36 case sparc: return "sparc";
37 case sparcv9: return "sparcv9";
38 case systemz: return "s390x";
39 case tce: return "tce";
40 case thumb: return "thumb";
41 case x86: return "i386";
42 case x86_64: return "x86_64";
43 case xcore: return "xcore";
44 case mblaze: return "mblaze";
45 case ptx: return "ptx";
46 case zpu: return "zpu";
49 return "<invalid>";
52 const char *Triple::getArchTypePrefix(ArchType Kind) {
53 switch (Kind) {
54 default:
55 return 0;
57 case alpha: return "alpha";
59 case arm:
60 case thumb: return "arm";
62 case bfin: return "bfin";
64 case cellspu: return "spu";
66 case ppc64:
67 case ppc: return "ppc";
69 case mblaze: return "mblaze";
71 case sparcv9:
72 case sparc: return "sparc";
74 case x86:
75 case x86_64: return "x86";
77 case xcore: return "xcore";
79 case ptx: return "ptx";
80 case zpu: return "zpu";
84 const char *Triple::getVendorTypeName(VendorType Kind) {
85 switch (Kind) {
86 case UnknownVendor: return "unknown";
88 case Apple: return "apple";
89 case PC: return "pc";
92 return "<invalid>";
95 const char *Triple::getOSTypeName(OSType Kind) {
96 switch (Kind) {
97 case UnknownOS: return "unknown";
99 case AuroraUX: return "auroraux";
100 case Cygwin: return "cygwin";
101 case Darwin: return "darwin";
102 case DragonFly: return "dragonfly";
103 case FreeBSD: return "freebsd";
104 case Linux: return "linux";
105 case Lv2: return "lv2";
106 case MinGW32: return "mingw32";
107 case MinGW64: return "mingw64";
108 case NetBSD: return "netbsd";
109 case OpenBSD: return "openbsd";
110 case Psp: return "psp";
111 case Solaris: return "solaris";
112 case Win32: return "win32";
113 case Haiku: return "haiku";
114 case Minix: return "minix";
117 return "<invalid>";
120 const char *Triple::getEnvironmentTypeName(EnvironmentType Kind) {
121 switch (Kind) {
122 case UnknownEnvironment: return "unknown";
125 return "<invalid>";
128 Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
129 if (Name == "alpha")
130 return alpha;
131 if (Name == "arm")
132 return arm;
133 if (Name == "bfin")
134 return bfin;
135 if (Name == "cellspu")
136 return cellspu;
137 if (Name == "mips")
138 return mips;
139 if (Name == "mipsel")
140 return mipsel;
141 if (Name == "msp430")
142 return msp430;
143 if (Name == "pic16")
144 return pic16;
145 if (Name == "ppc64")
146 return ppc64;
147 if (Name == "ppc")
148 return ppc;
149 if (Name == "mblaze")
150 return mblaze;
151 if (Name == "sparc")
152 return sparc;
153 if (Name == "sparcv9")
154 return sparcv9;
155 if (Name == "systemz")
156 return systemz;
157 if (Name == "tce")
158 return tce;
159 if (Name == "thumb")
160 return thumb;
161 if (Name == "x86")
162 return x86;
163 if (Name == "x86-64")
164 return x86_64;
165 if (Name == "xcore")
166 return xcore;
167 if (Name == "ptx")
168 return ptx;
169 if (Name == "zpu")
170 return zpu;
172 return UnknownArch;
175 Triple::ArchType Triple::getArchTypeForDarwinArchName(StringRef Str) {
176 // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
177 // archs which Darwin doesn't use.
179 // The matching this routine does is fairly pointless, since it is neither the
180 // complete architecture list, nor a reasonable subset. The problem is that
181 // historically the driver driver accepts this and also ties its -march=
182 // handling to the architecture name, so we need to be careful before removing
183 // support for it.
185 // This code must be kept in sync with Clang's Darwin specific argument
186 // translation.
188 if (Str == "ppc" || Str == "ppc601" || Str == "ppc603" || Str == "ppc604" ||
189 Str == "ppc604e" || Str == "ppc750" || Str == "ppc7400" ||
190 Str == "ppc7450" || Str == "ppc970")
191 return Triple::ppc;
193 if (Str == "ppc64")
194 return Triple::ppc64;
196 if (Str == "i386" || Str == "i486" || Str == "i486SX" || Str == "pentium" ||
197 Str == "i586" || Str == "pentpro" || Str == "i686" || Str == "pentIIm3" ||
198 Str == "pentIIm5" || Str == "pentium4")
199 return Triple::x86;
201 if (Str == "x86_64")
202 return Triple::x86_64;
204 // This is derived from the driver driver.
205 if (Str == "arm" || Str == "armv4t" || Str == "armv5" || Str == "xscale" ||
206 Str == "armv6" || Str == "armv7")
207 return Triple::arm;
209 if (Str == "ptx")
210 return Triple::ptx;
212 return Triple::UnknownArch;
215 // Returns architecture name that is understood by the target assembler.
216 const char *Triple::getArchNameForAssembler() {
217 if (getOS() != Triple::Darwin && getVendor() != Triple::Apple)
218 return NULL;
220 StringRef Str = getArchName();
221 if (Str == "i386")
222 return "i386";
223 if (Str == "x86_64")
224 return "x86_64";
225 if (Str == "powerpc")
226 return "ppc";
227 if (Str == "powerpc64")
228 return "ppc64";
229 if (Str == "mblaze" || Str == "microblaze")
230 return "mblaze";
231 if (Str == "arm")
232 return "arm";
233 if (Str == "armv4t" || Str == "thumbv4t")
234 return "armv4t";
235 if (Str == "armv5" || Str == "armv5e" || Str == "thumbv5" || Str == "thumbv5e")
236 return "armv5";
237 if (Str == "armv6" || Str == "thumbv6")
238 return "armv6";
239 if (Str == "armv7" || Str == "thumbv7")
240 return "armv7";
241 if (Str == "ptx")
242 return "ptx";
243 if (Str == "zpu")
244 return "zpu";
245 return NULL;
250 Triple::ArchType Triple::ParseArch(StringRef ArchName) {
251 if (ArchName.size() == 4 && ArchName[0] == 'i' &&
252 ArchName[2] == '8' && ArchName[3] == '6' &&
253 ArchName[1] - '3' < 6) // i[3-9]86
254 return x86;
255 else if (ArchName == "amd64" || ArchName == "x86_64")
256 return x86_64;
257 else if (ArchName == "bfin")
258 return bfin;
259 else if (ArchName == "pic16")
260 return pic16;
261 else if (ArchName == "powerpc")
262 return ppc;
263 else if ((ArchName == "powerpc64") || (ArchName == "ppu"))
264 return ppc64;
265 else if (ArchName == "mblaze")
266 return mblaze;
267 else if (ArchName == "arm" ||
268 ArchName.startswith("armv") ||
269 ArchName == "xscale")
270 return arm;
271 else if (ArchName == "thumb" ||
272 ArchName.startswith("thumbv"))
273 return thumb;
274 else if (ArchName.startswith("alpha"))
275 return alpha;
276 else if (ArchName == "spu" || ArchName == "cellspu")
277 return cellspu;
278 else if (ArchName == "msp430")
279 return msp430;
280 else if (ArchName == "mips" || ArchName == "mipsallegrex")
281 return mips;
282 else if (ArchName == "mipsel" || ArchName == "mipsallegrexel" ||
283 ArchName == "psp")
284 return mipsel;
285 else if (ArchName == "sparc")
286 return sparc;
287 else if (ArchName == "sparcv9")
288 return sparcv9;
289 else if (ArchName == "s390x")
290 return systemz;
291 else if (ArchName == "tce")
292 return tce;
293 else if (ArchName == "xcore")
294 return xcore;
295 else if (ArchName == "ptx")
296 return ptx;
297 else if (ArchName == "zpu")
298 return zpu;
299 else
300 return UnknownArch;
303 Triple::VendorType Triple::ParseVendor(StringRef VendorName) {
304 if (VendorName == "apple")
305 return Apple;
306 else if (VendorName == "pc")
307 return PC;
308 else
309 return UnknownVendor;
312 Triple::OSType Triple::ParseOS(StringRef OSName) {
313 if (OSName.startswith("auroraux"))
314 return AuroraUX;
315 else if (OSName.startswith("cygwin"))
316 return Cygwin;
317 else if (OSName.startswith("darwin"))
318 return Darwin;
319 else if (OSName.startswith("dragonfly"))
320 return DragonFly;
321 else if (OSName.startswith("freebsd"))
322 return FreeBSD;
323 else if (OSName.startswith("linux"))
324 return Linux;
325 else if (OSName.startswith("lv2"))
326 return Lv2;
327 else if (OSName.startswith("mingw32"))
328 return MinGW32;
329 else if (OSName.startswith("mingw64"))
330 return MinGW64;
331 else if (OSName.startswith("netbsd"))
332 return NetBSD;
333 else if (OSName.startswith("openbsd"))
334 return OpenBSD;
335 else if (OSName.startswith("psp"))
336 return Psp;
337 else if (OSName.startswith("solaris"))
338 return Solaris;
339 else if (OSName.startswith("win32"))
340 return Win32;
341 else if (OSName.startswith("haiku"))
342 return Haiku;
343 else if (OSName.startswith("minix"))
344 return Minix;
345 else
346 return UnknownOS;
349 Triple::EnvironmentType Triple::ParseEnvironment(StringRef EnvironmentName) {
350 return UnknownEnvironment;
353 void Triple::Parse() const {
354 assert(!isInitialized() && "Invalid parse call.");
356 Arch = ParseArch(getArchName());
357 Vendor = ParseVendor(getVendorName());
358 OS = ParseOS(getOSName());
359 Environment = ParseEnvironment(getEnvironmentName());
361 assert(isInitialized() && "Failed to initialize!");
364 std::string Triple::normalize(StringRef Str) {
365 // Parse into components.
366 SmallVector<StringRef, 4> Components;
367 for (size_t First = 0, Last = 0; Last != StringRef::npos; First = Last + 1) {
368 Last = Str.find('-', First);
369 Components.push_back(Str.slice(First, Last));
372 // If the first component corresponds to a known architecture, preferentially
373 // use it for the architecture. If the second component corresponds to a
374 // known vendor, preferentially use it for the vendor, etc. This avoids silly
375 // component movement when a component parses as (eg) both a valid arch and a
376 // valid os.
377 ArchType Arch = UnknownArch;
378 if (Components.size() > 0)
379 Arch = ParseArch(Components[0]);
380 VendorType Vendor = UnknownVendor;
381 if (Components.size() > 1)
382 Vendor = ParseVendor(Components[1]);
383 OSType OS = UnknownOS;
384 if (Components.size() > 2)
385 OS = ParseOS(Components[2]);
386 EnvironmentType Environment = UnknownEnvironment;
387 if (Components.size() > 3)
388 Environment = ParseEnvironment(Components[3]);
390 // Note which components are already in their final position. These will not
391 // be moved.
392 bool Found[4];
393 Found[0] = Arch != UnknownArch;
394 Found[1] = Vendor != UnknownVendor;
395 Found[2] = OS != UnknownOS;
396 Found[3] = Environment != UnknownEnvironment;
398 // If they are not there already, permute the components into their canonical
399 // positions by seeing if they parse as a valid architecture, and if so moving
400 // the component to the architecture position etc.
401 for (unsigned Pos = 0; Pos != array_lengthof(Found); ++Pos) {
402 if (Found[Pos])
403 continue; // Already in the canonical position.
405 for (unsigned Idx = 0; Idx != Components.size(); ++Idx) {
406 // Do not reparse any components that already matched.
407 if (Idx < array_lengthof(Found) && Found[Idx])
408 continue;
410 // Does this component parse as valid for the target position?
411 bool Valid = false;
412 StringRef Comp = Components[Idx];
413 switch (Pos) {
414 default:
415 assert(false && "unexpected component type!");
416 case 0:
417 Arch = ParseArch(Comp);
418 Valid = Arch != UnknownArch;
419 break;
420 case 1:
421 Vendor = ParseVendor(Comp);
422 Valid = Vendor != UnknownVendor;
423 break;
424 case 2:
425 OS = ParseOS(Comp);
426 Valid = OS != UnknownOS;
427 break;
428 case 3:
429 Environment = ParseEnvironment(Comp);
430 Valid = Environment != UnknownEnvironment;
431 break;
433 if (!Valid)
434 continue; // Nope, try the next component.
436 // Move the component to the target position, pushing any non-fixed
437 // components that are in the way to the right. This tends to give
438 // good results in the common cases of a forgotten vendor component
439 // or a wrongly positioned environment.
440 if (Pos < Idx) {
441 // Insert left, pushing the existing components to the right. For
442 // example, a-b-i386 -> i386-a-b when moving i386 to the front.
443 StringRef CurrentComponent(""); // The empty component.
444 // Replace the component we are moving with an empty component.
445 std::swap(CurrentComponent, Components[Idx]);
446 // Insert the component being moved at Pos, displacing any existing
447 // components to the right.
448 for (unsigned i = Pos; !CurrentComponent.empty(); ++i) {
449 // Skip over any fixed components.
450 while (i < array_lengthof(Found) && Found[i]) ++i;
451 // Place the component at the new position, getting the component
452 // that was at this position - it will be moved right.
453 std::swap(CurrentComponent, Components[i]);
455 } else if (Pos > Idx) {
456 // Push right by inserting empty components until the component at Idx
457 // reaches the target position Pos. For example, pc-a -> -pc-a when
458 // moving pc to the second position.
459 do {
460 // Insert one empty component at Idx.
461 StringRef CurrentComponent(""); // The empty component.
462 for (unsigned i = Idx; i < Components.size(); ++i) {
463 // Skip over any fixed components.
464 while (i < array_lengthof(Found) && Found[i]) ++i;
465 // Place the component at the new position, getting the component
466 // that was at this position - it will be moved right.
467 std::swap(CurrentComponent, Components[i]);
468 // If it was placed on top of an empty component then we are done.
469 if (CurrentComponent.empty())
470 break;
472 // The last component was pushed off the end - append it.
473 if (!CurrentComponent.empty())
474 Components.push_back(CurrentComponent);
476 // Advance Idx to the component's new position.
477 while (++Idx < array_lengthof(Found) && Found[Idx]) {}
478 } while (Idx < Pos); // Add more until the final position is reached.
480 assert(Pos < Components.size() && Components[Pos] == Comp &&
481 "Component moved wrong!");
482 Found[Pos] = true;
483 break;
487 // Special case logic goes here. At this point Arch, Vendor and OS have the
488 // correct values for the computed components.
490 // Stick the corrected components back together to form the normalized string.
491 std::string Normalized;
492 for (unsigned i = 0, e = Components.size(); i != e; ++i) {
493 if (i) Normalized += '-';
494 Normalized += Components[i];
496 return Normalized;
499 StringRef Triple::getArchName() const {
500 return StringRef(Data).split('-').first; // Isolate first component
503 StringRef Triple::getVendorName() const {
504 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
505 return Tmp.split('-').first; // Isolate second component
508 StringRef Triple::getOSName() const {
509 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
510 Tmp = Tmp.split('-').second; // Strip second component
511 return Tmp.split('-').first; // Isolate third component
514 StringRef Triple::getEnvironmentName() const {
515 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
516 Tmp = Tmp.split('-').second; // Strip second component
517 return Tmp.split('-').second; // Strip third component
520 StringRef Triple::getOSAndEnvironmentName() const {
521 StringRef Tmp = StringRef(Data).split('-').second; // Strip first component
522 return Tmp.split('-').second; // Strip second component
525 static unsigned EatNumber(StringRef &Str) {
526 assert(!Str.empty() && Str[0] >= '0' && Str[0] <= '9' && "Not a number");
527 unsigned Result = Str[0]-'0';
529 // Eat the digit.
530 Str = Str.substr(1);
532 // Handle "darwin11".
533 if (Result == 1 && !Str.empty() && Str[0] >= '0' && Str[0] <= '9') {
534 Result = Result*10 + (Str[0] - '0');
535 // Eat the digit.
536 Str = Str.substr(1);
539 return Result;
542 /// getDarwinNumber - Parse the 'darwin number' out of the specific target
543 /// triple. For example, if we have darwin8.5 return 8,5,0. If any entry is
544 /// not defined, return 0's. This requires that the triple have an OSType of
545 /// darwin before it is called.
546 void Triple::getDarwinNumber(unsigned &Maj, unsigned &Min,
547 unsigned &Revision) const {
548 assert(getOS() == Darwin && "Not a darwin target triple!");
549 StringRef OSName = getOSName();
550 assert(OSName.startswith("darwin") && "Unknown darwin target triple!");
552 // Strip off "darwin".
553 OSName = OSName.substr(6);
555 Maj = Min = Revision = 0;
557 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
558 return;
560 // The major version is the first digit.
561 Maj = EatNumber(OSName);
562 if (OSName.empty()) return;
564 // Handle minor version: 10.4.9 -> darwin8.9.
565 if (OSName[0] != '.')
566 return;
568 // Eat the '.'.
569 OSName = OSName.substr(1);
571 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
572 return;
574 Min = EatNumber(OSName);
575 if (OSName.empty()) return;
577 // Handle revision darwin8.9.1
578 if (OSName[0] != '.')
579 return;
581 // Eat the '.'.
582 OSName = OSName.substr(1);
584 if (OSName.empty() || OSName[0] < '0' || OSName[0] > '9')
585 return;
587 Revision = EatNumber(OSName);
590 void Triple::setTriple(const Twine &Str) {
591 Data = Str.str();
592 Arch = InvalidArch;
595 void Triple::setArch(ArchType Kind) {
596 setArchName(getArchTypeName(Kind));
599 void Triple::setVendor(VendorType Kind) {
600 setVendorName(getVendorTypeName(Kind));
603 void Triple::setOS(OSType Kind) {
604 setOSName(getOSTypeName(Kind));
607 void Triple::setEnvironment(EnvironmentType Kind) {
608 setEnvironmentName(getEnvironmentTypeName(Kind));
611 void Triple::setArchName(StringRef Str) {
612 // Work around a miscompilation bug for Twines in gcc 4.0.3.
613 SmallString<64> Triple;
614 Triple += Str;
615 Triple += "-";
616 Triple += getVendorName();
617 Triple += "-";
618 Triple += getOSAndEnvironmentName();
619 setTriple(Triple.str());
622 void Triple::setVendorName(StringRef Str) {
623 setTriple(getArchName() + "-" + Str + "-" + getOSAndEnvironmentName());
626 void Triple::setOSName(StringRef Str) {
627 if (hasEnvironment())
628 setTriple(getArchName() + "-" + getVendorName() + "-" + Str +
629 "-" + getEnvironmentName());
630 else
631 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
634 void Triple::setEnvironmentName(StringRef Str) {
635 setTriple(getArchName() + "-" + getVendorName() + "-" + getOSName() +
636 "-" + Str);
639 void Triple::setOSAndEnvironmentName(StringRef Str) {
640 setTriple(getArchName() + "-" + getVendorName() + "-" + Str);