Triple.h revision 8c2f1d7e44d214bc2242a8c6faa4b624b3876540
1//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===// 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#ifndef LLVM_ADT_TRIPLE_H 11#define LLVM_ADT_TRIPLE_H 12 13#include "llvm/ADT/StringRef.h" 14#include <string> 15 16namespace llvm { 17class StringRef; 18class Twine; 19 20/// Triple - Helper class for working with target triples. 21/// 22/// Target triples are strings in the format of: 23/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM 24/// or 25/// ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT 26/// 27/// This class is used for clients which want to support arbitrary 28/// target triples, but also want to implement certain special 29/// behavior for particular targets. This class isolates the mapping 30/// from the components of the target triple to well known IDs. 31/// 32/// See autoconf/config.guess for a glimpse into what they look like 33/// in practice. 34class Triple { 35public: 36 enum ArchType { 37 UnknownArch, 38 39 alpha, // alpha 40 arm, // arm, armv.* 41 cellspu, // spu, cellspu 42 mips, // mips, mipsallegrex 43 mipsel, // mipsel, mipsallegrexel, psp 44 msp430, // msp430 45 ppc, // powerpc 46 ppc64, // powerpc64 47 sparc, // sparc 48 systemz, // s390x 49 thumb, // thumb, thumbv.* 50 x86, // i[3-9]86 51 x86_64, // amd64, x86_64 52 xcore, // xcore 53 54 InvalidArch 55 }; 56 enum VendorType { 57 UnknownVendor, 58 59 Apple, 60 PC 61 }; 62 enum OSType { 63 UnknownOS, 64 65 AuroraUX, 66 Cygwin, 67 Darwin, 68 DragonFly, 69 FreeBSD, 70 Linux, 71 MinGW32, 72 NetBSD, 73 OpenBSD, 74 Win32 75 }; 76 77private: 78 std::string Data; 79 80 /// The parsed arch type (or InvalidArch if uninitialized). 81 mutable ArchType Arch; 82 83 /// The parsed vendor type. 84 mutable VendorType Vendor; 85 86 /// The parsed OS type. 87 mutable OSType OS; 88 89 bool isInitialized() const { return Arch != InvalidArch; } 90 void Parse() const; 91 92public: 93 /// @name Constructors 94 /// @{ 95 96 Triple() : Data(""), Arch(InvalidArch) {} 97 explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {} 98 explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr) 99 : Data(ArchStr), Arch(InvalidArch) { 100 Data += '-'; 101 Data += VendorStr; 102 Data += '-'; 103 Data += OSStr; 104 } 105 106 /// @} 107 /// @name Typed Component Access 108 /// @{ 109 110 /// getArch - Get the parsed architecture type of this triple. 111 ArchType getArch() const { 112 if (!isInitialized()) Parse(); 113 return Arch; 114 } 115 116 /// getVendor - Get the parsed vendor type of this triple. 117 VendorType getVendor() const { 118 if (!isInitialized()) Parse(); 119 return Vendor; 120 } 121 122 /// getOS - Get the parsed operating system type of this triple. 123 OSType getOS() const { 124 if (!isInitialized()) Parse(); 125 return OS; 126 } 127 128 /// hasEnvironment - Does this triple have the optional environment 129 /// (fourth) component? 130 bool hasEnvironment() const { 131 return getEnvironmentName() != ""; 132 } 133 134 /// @} 135 /// @name Direct Component Access 136 /// @{ 137 138 const std::string &getTriple() const { return Data; } 139 140 /// getArchName - Get the architecture (first) component of the 141 /// triple. 142 StringRef getArchName() const; 143 144 /// getVendorName - Get the vendor (second) component of the triple. 145 StringRef getVendorName() const; 146 147 /// getOSName - Get the operating system (third) component of the 148 /// triple. 149 StringRef getOSName() const; 150 151 /// getEnvironmentName - Get the optional environment (fourth) 152 /// component of the triple, or "" if empty. 153 StringRef getEnvironmentName() const; 154 155 /// getOSAndEnvironmentName - Get the operating system and optional 156 /// environment components as a single string (separated by a '-' 157 /// if the environment component is present). 158 StringRef getOSAndEnvironmentName() const; 159 160 /// @} 161 /// @name Mutators 162 /// @{ 163 164 /// setArch - Set the architecture (first) component of the triple 165 /// to a known type. 166 void setArch(ArchType Kind); 167 168 /// setVendor - Set the vendor (second) component of the triple to a 169 /// known type. 170 void setVendor(VendorType Kind); 171 172 /// setOS - Set the operating system (third) component of the triple 173 /// to a known type. 174 void setOS(OSType Kind); 175 176 /// setTriple - Set all components to the new triple \arg Str. 177 void setTriple(const Twine &Str); 178 179 /// setArchName - Set the architecture (first) component of the 180 /// triple by name. 181 void setArchName(const StringRef &Str); 182 183 /// setVendorName - Set the vendor (second) component of the triple 184 /// by name. 185 void setVendorName(const StringRef &Str); 186 187 /// setOSName - Set the operating system (third) component of the 188 /// triple by name. 189 void setOSName(const StringRef &Str); 190 191 /// setEnvironmentName - Set the optional environment (fourth) 192 /// component of the triple by name. 193 void setEnvironmentName(const StringRef &Str); 194 195 /// setOSAndEnvironmentName - Set the operating system and optional 196 /// environment components with a single string. 197 void setOSAndEnvironmentName(const StringRef &Str); 198 199 /// @} 200 /// @name Static helpers for IDs. 201 /// @{ 202 203 /// getArchTypeName - Get the canonical name for the \arg Kind 204 /// architecture. 205 static const char *getArchTypeName(ArchType Kind); 206 207 /// getVendorTypeName - Get the canonical name for the \arg Kind 208 /// vendor. 209 static const char *getVendorTypeName(VendorType Kind); 210 211 /// getOSTypeName - Get the canonical name for the \arg Kind vendor. 212 static const char *getOSTypeName(OSType Kind); 213 214 /// @} 215}; 216 217} // End llvm namespace 218 219 220#endif 221