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