Triple.h revision 194612
1193323Sed//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed
10193323Sed#ifndef LLVM_ADT_TRIPLE_H
11193323Sed#define LLVM_ADT_TRIPLE_H
12193323Sed
13193323Sed#include <string>
14193323Sed
15193323Sednamespace llvm {
16193323Sed
17193323Sed/// Triple - Helper class for working with target triples.
18193323Sed///
19193323Sed/// Target triples are strings in the format of:
20193323Sed///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
21193323Sed/// or
22193323Sed///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
23193323Sed///
24193323Sed/// This class is used for clients which want to support arbitrary
25193323Sed/// target triples, but also want to implement certain special
26193323Sed/// behavior for particular targets. This class isolates the mapping
27193323Sed/// from the components of the target triple to well known IDs.
28193323Sed///
29193323Sed/// See autoconf/config.guess for a glimpse into what they look like
30193323Sed/// in practice.
31193323Sedclass Triple {
32193323Sedpublic:
33193323Sed  enum ArchType {
34193323Sed    UnknownArch,
35193323Sed
36193323Sed    x86,    // i?86
37193323Sed    ppc,    // powerpc
38193323Sed    ppc64,  // powerpc64
39193323Sed    x86_64, // amd64, x86_64
40193323Sed
41193323Sed    InvalidArch
42193323Sed  };
43193323Sed  enum VendorType {
44193323Sed    UnknownVendor,
45193323Sed
46193323Sed    Apple,
47193323Sed    PC
48193323Sed  };
49193323Sed  enum OSType {
50193323Sed    UnknownOS,
51193323Sed
52194612Sed    AuroraUX,
53193323Sed    Darwin,
54193323Sed    DragonFly,
55193323Sed    FreeBSD,
56193323Sed    Linux
57193323Sed  };
58193323Sed
59193323Sedprivate:
60193323Sed  std::string Data;
61193323Sed
62193323Sed  /// The parsed arch type (or InvalidArch if uninitialized).
63193323Sed  mutable ArchType Arch;
64193323Sed
65193323Sed  /// The parsed vendor type.
66193323Sed  mutable VendorType Vendor;
67193323Sed
68193323Sed  /// The parsed OS type.
69193323Sed  mutable OSType OS;
70193323Sed
71193323Sed  bool isInitialized() const { return Arch != InvalidArch; }
72193323Sed  void Parse() const;
73193323Sed
74193323Sedpublic:
75193323Sed  /// @name Constructors
76193323Sed  /// @{
77193323Sed
78193323Sed  Triple() : Data(""), Arch(InvalidArch) {}
79193323Sed  explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {}
80193323Sed  explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
81193323Sed    : Data(ArchStr), Arch(InvalidArch) {
82193323Sed    Data += '-';
83193323Sed    Data += VendorStr;
84193323Sed    Data += '-';
85193323Sed    Data += OSStr;
86193323Sed  }
87193323Sed
88193323Sed  /// @}
89193323Sed  /// @name Typed Component Access
90193323Sed  /// @{
91193323Sed
92193323Sed  /// getArch - Get the parsed architecture type of this triple.
93193323Sed  ArchType getArch() const {
94193323Sed    if (!isInitialized()) Parse();
95193323Sed    return Arch;
96193323Sed  }
97193323Sed
98193323Sed  /// getVendor - Get the parsed vendor type of this triple.
99193323Sed  VendorType getVendor() const {
100193323Sed    if (!isInitialized()) Parse();
101193323Sed    return Vendor;
102193323Sed  }
103193323Sed
104193323Sed  /// getOS - Get the parsed operating system type of this triple.
105193323Sed  OSType getOS() const {
106193323Sed    if (!isInitialized()) Parse();
107193323Sed    return OS;
108193323Sed  }
109193323Sed
110193323Sed  /// hasEnvironment - Does this triple have the optional environment
111193323Sed  /// (fourth) component?
112193323Sed  bool hasEnvironment() const {
113193323Sed    return getEnvironmentName() != "";
114193323Sed  }
115193323Sed
116193323Sed  /// @}
117193323Sed  /// @name Direct Component Access
118193323Sed  /// @{
119193323Sed
120193323Sed  const std::string &getTriple() const { return Data; }
121193323Sed
122193323Sed  // FIXME: Invent a lightweight string representation for these to
123193323Sed  // use.
124193323Sed
125193323Sed  /// getArchName - Get the architecture (first) component of the
126193323Sed  /// triple.
127193323Sed  std::string getArchName() const;
128193323Sed
129193323Sed  /// getVendorName - Get the vendor (second) component of the triple.
130193323Sed  std::string getVendorName() const;
131193323Sed
132193323Sed  /// getOSName - Get the operating system (third) component of the
133193323Sed  /// triple.
134193323Sed  std::string getOSName() const;
135193323Sed
136193323Sed  /// getEnvironmentName - Get the optional environment (fourth)
137193323Sed  /// component of the triple, or "" if empty.
138193323Sed  std::string getEnvironmentName() const;
139193323Sed
140193323Sed  /// getOSAndEnvironmentName - Get the operating system and optional
141193323Sed  /// environment components as a single string (separated by a '-'
142193323Sed  /// if the environment component is present).
143193323Sed  std::string getOSAndEnvironmentName() const;
144193323Sed
145193323Sed  /// @}
146193323Sed  /// @name Mutators
147193323Sed  /// @{
148193323Sed
149193323Sed  /// setArch - Set the architecture (first) component of the triple
150193323Sed  /// to a known type.
151193323Sed  void setArch(ArchType Kind);
152193323Sed
153193323Sed  /// setVendor - Set the vendor (second) component of the triple to a
154193323Sed  /// known type.
155193323Sed  void setVendor(VendorType Kind);
156193323Sed
157193323Sed  /// setOS - Set the operating system (third) component of the triple
158193323Sed  /// to a known type.
159193323Sed  void setOS(OSType Kind);
160193323Sed
161193323Sed  /// setTriple - Set all components to the new triple \arg Str.
162193323Sed  void setTriple(const std::string &Str);
163193323Sed
164193323Sed  /// setArchName - Set the architecture (first) component of the
165193323Sed  /// triple by name.
166193323Sed  void setArchName(const std::string &Str);
167193323Sed
168193323Sed  /// setVendorName - Set the vendor (second) component of the triple
169193323Sed  /// by name.
170193323Sed  void setVendorName(const std::string &Str);
171193323Sed
172193323Sed  /// setOSName - Set the operating system (third) component of the
173193323Sed  /// triple by name.
174193323Sed  void setOSName(const std::string &Str);
175193323Sed
176193323Sed  /// setEnvironmentName - Set the optional environment (fourth)
177193323Sed  /// component of the triple by name.
178193323Sed  void setEnvironmentName(const std::string &Str);
179193323Sed
180193323Sed  /// setOSAndEnvironmentName - Set the operating system and optional
181193323Sed  /// environment components with a single string.
182193323Sed  void setOSAndEnvironmentName(const std::string &Str);
183193323Sed
184193323Sed  /// @}
185193323Sed  /// @name Static helpers for IDs.
186193323Sed  /// @{
187193323Sed
188193323Sed  /// getArchTypeName - Get the canonical name for the \arg Kind
189193323Sed  /// architecture.
190193323Sed  static const char *getArchTypeName(ArchType Kind);
191193323Sed
192193323Sed  /// getVendorTypeName - Get the canonical name for the \arg Kind
193193323Sed  /// vendor.
194193323Sed  static const char *getVendorTypeName(VendorType Kind);
195193323Sed
196193323Sed  /// getOSTypeName - Get the canonical name for the \arg Kind vendor.
197193323Sed  static const char *getOSTypeName(OSType Kind);
198193323Sed
199193323Sed  /// @}
200193323Sed};
201193323Sed
202193323Sed} // End llvm namespace
203193323Sed
204193323Sed
205193323Sed#endif
206