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