Triple.h revision d950941e138455ebcd7a5f55805dcb977892e3e3
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    bfin,    // blackfin
42    cellspu, // spu, cellspu
43    mips,    // mips, mipsallegrex
44    mipsel,  // mipsel, mipsallegrexel, psp
45    msp430,  // msp430
46    ppc,     // powerpc
47    ppc64,   // powerpc64
48    sparc,   // sparc
49    systemz, // s390x
50    thumb,   // thumb, thumbv.*
51    x86,     // i[3-9]86
52    x86_64,  // amd64, x86_64
53    xcore,   // xcore
54
55    InvalidArch
56  };
57  enum VendorType {
58    UnknownVendor,
59
60    Apple,
61    PC
62  };
63  enum OSType {
64    UnknownOS,
65
66    AuroraUX,
67    Cygwin,
68    Darwin,
69    DragonFly,
70    FreeBSD,
71    Linux,
72    MinGW32,
73    NetBSD,
74    OpenBSD,
75    Win32
76  };
77
78private:
79  std::string Data;
80
81  /// The parsed arch type (or InvalidArch if uninitialized).
82  mutable ArchType Arch;
83
84  /// The parsed vendor type.
85  mutable VendorType Vendor;
86
87  /// The parsed OS type.
88  mutable OSType OS;
89
90  bool isInitialized() const { return Arch != InvalidArch; }
91  void Parse() const;
92
93public:
94  /// @name Constructors
95  /// @{
96
97  Triple() : Data(""), Arch(InvalidArch) {}
98  explicit Triple(const char *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  /// @}
216};
217
218} // End llvm namespace
219
220
221#endif
222