Triple.h revision bf4fc28303e00064fb24db1394010782299f5220
1b3c90d96bd64c4b2b1b471b3c45bb119c526ca7bGabor Greif//===-- llvm/ADT/Triple.h - Target triple helper class ----------*- C++ -*-===//
2ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//
3ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//                     The LLVM Compiler Infrastructure
4ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//
5ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif// This file is distributed under the University of Illinois Open Source
6ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif// License. See LICENSE.TXT for details.
7ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//
8ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif//===----------------------------------------------------------------------===//
9ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
10ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#ifndef LLVM_ADT_TRIPLE_H
11b3c90d96bd64c4b2b1b471b3c45bb119c526ca7bGabor Greif#define LLVM_ADT_TRIPLE_H
12b3c90d96bd64c4b2b1b471b3c45bb119c526ca7bGabor Greif
13ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include "llvm/ADT/StringRef.h"
14ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif#include <string>
15ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
16ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greifnamespace llvm {
17e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greifclass StringRef;
18e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greifclass Twine;
19e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
20e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// Triple - Helper class for working with target triples.
21e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif///
22e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// Target triples are strings in the format of:
23e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM
24d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif/// or
25d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif///   ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT
26f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif///
27f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif/// This class is used for clients which want to support arbitrary
28e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// target triples, but also want to implement certain special
29e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// behavior for particular targets. This class isolates the mapping
30e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// from the components of the target triple to well known IDs.
31e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif///
32e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// See autoconf/config.guess for a glimpse into what they look like
33e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif/// in practice.
34e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greifclass Triple {
35e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greifpublic:
36e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  enum ArchType {
37e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    UnknownArch,
38e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
39f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif    alpha,   // Alpha: alpha
40e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    arm,     // ARM; arm, armv.*
41e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    bfin,    // Blackfin: bfin
42e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    cellspu, // CellSPU: spu, cellspu
43e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    mips,    // MIPS: mips, mipsallegrex
44e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    mipsel,  // MIPSEL: mipsel, mipsallegrexel, psp
45e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    msp430,  // MPS430: msp430
46d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    ppc,     // PPC: powerpc
47e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    ppc64,   // PPC64: powerpc64
48d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    sparc,   // Sparc: sparc
49d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    systemz, // SystemZ: s390x
50d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    thumb,   // Thumb: thumb, thumbv.*
51d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    x86,     // X86: i[3-9]86
52d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    x86_64,  // X86-64: amd64, x86_64
53d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    xcore,   // XCore: xcore
54f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif
55d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    InvalidArch
56f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  };
57f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  enum VendorType {
58f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif    UnknownVendor,
59f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif
60f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif    Apple,
61e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    PC
62e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  };
63e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  enum OSType {
64e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif    UnknownOS,
65e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
66ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif    AuroraUX,
67ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif    Cygwin,
68ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif    Darwin,
69ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif    DragonFly,
7008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    FreeBSD,
7108993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    Linux,
72ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif    MinGW32,
73ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif    NetBSD,
7408993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    OpenBSD,
7508993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    Win32
7608993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  };
77ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
78af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greifprivate:
79ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif  std::string Data;
80af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greif
81af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greif  /// The parsed arch type (or InvalidArch if uninitialized).
82af8e2ef649b90e88f9d595a638279e3bc4892845Gabor Greif  mutable ArchType Arch;
83ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
84ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif  /// The parsed vendor type.
8508993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  mutable VendorType Vendor;
8608993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
8708993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  /// The parsed OS type.
8808993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  mutable OSType OS;
8908993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
9008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  bool isInitialized() const { return Arch != InvalidArch; }
9108993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  void Parse() const;
92e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
93e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greifpublic:
94e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// @name Constructors
95e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// @{
96e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
97e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  Triple() : Data(""), Arch(InvalidArch) {}
98d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif  explicit Triple(const char *Str) : Data(Str), Arch(InvalidArch) {}
99d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif  explicit Triple(const char *ArchStr, const char *VendorStr, const char *OSStr)
10008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    : Data(ArchStr), Arch(InvalidArch) {
10108993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    Data += '-';
10208993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    Data += VendorStr;
10308993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    Data += '-';
10408993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif    Data += OSStr;
10508993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  }
10608993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
10708993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  /// @}
10808993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  /// @name Typed Component Access
10908993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  /// @{
11008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
111d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif  /// getArch - Get the parsed architecture type of this triple.
112d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif  ArchType getArch() const {
113d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    if (!isInitialized()) Parse();
114d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif    return Arch;
115d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif  }
11646a35015f5268bf72150eb1b1517337e245e38e5Gabor Greif
11746a35015f5268bf72150eb1b1517337e245e38e5Gabor Greif  /// getVendor - Get the parsed vendor type of this triple.
11846a35015f5268bf72150eb1b1517337e245e38e5Gabor Greif  VendorType getVendor() const {
11946a35015f5268bf72150eb1b1517337e245e38e5Gabor Greif    if (!isInitialized()) Parse();
12046a35015f5268bf72150eb1b1517337e245e38e5Gabor Greif    return Vendor;
121d159467ad66b21172d6d88a9f8e496c5b6c2ba4eGabor Greif  }
122f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif
123f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  /// getOS - Get the parsed operating system type of this triple.
124f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  OSType getOS() const {
125f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif    if (!isInitialized()) Parse();
126f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif    return OS;
127f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  }
128f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif
129f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  /// hasEnvironment - Does this triple have the optional environment
130f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  /// (fourth) component?
131f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  bool hasEnvironment() const {
132f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif    return getEnvironmentName() != "";
133f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  }
134f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif
135f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  /// @}
13646a35015f5268bf72150eb1b1517337e245e38e5Gabor Greif  /// @name Direct Component Access
137f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif  /// @{
138f06eb3719aef56a31fb5c56e5b086995aefafef5Gabor Greif
13908993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif  const std::string &getTriple() const { return Data; }
14008993c03abed359171d12b50ab38da0ac331cfb7Gabor Greif
141e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// getArchName - Get the architecture (first) component of the
142e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// triple.
143e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  StringRef getArchName() const;
144e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
145e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// getVendorName - Get the vendor (second) component of the triple.
146e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  StringRef getVendorName() const;
147e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif
148e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// getOSName - Get the operating system (third) component of the
149ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif  /// triple.
150ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif  StringRef getOSName() const;
151ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif
152ee57dae3d8bcb2f3f41eb225bc615045d13d9ddaGabor Greif  /// getEnvironmentName - Get the optional environment (fourth)
153e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  /// component of the triple, or "" if empty.
154e8950977a5cf929ab15b6c5f7265a029500d9a7dGabor Greif  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