ArchSpec.h revision 41a857a34dd83831c4e9db23179608cb8bd28328
1//===-- ArchSpec.h ----------------------------------------------*- 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 liblldb_ArchSpec_h_
11#define liblldb_ArchSpec_h_
12
13#if defined(__cplusplus)
14
15#include "lldb/lldb-private.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Triple.h"
18
19namespace lldb_private {
20
21struct CoreDefinition;
22
23//----------------------------------------------------------------------
24/// @class ArchSpec ArchSpec.h "lldb/Core/ArchSpec.h"
25/// @brief An architecture specification class.
26///
27/// A class designed to be created from a cpu type and subtype, a
28/// string representation, or an llvm::Triple.  Keeping all of the
29/// conversions of strings to architecture enumeration values confined
30/// to this class allows new architecture support to be added easily.
31//----------------------------------------------------------------------
32class ArchSpec
33{
34public:
35    enum Core
36    {
37        eCore_arm_generic,
38        eCore_arm_armv4,
39        eCore_arm_armv4t,
40        eCore_arm_armv5,
41        eCore_arm_armv5t,
42        eCore_arm_armv6,
43        eCore_arm_armv7,
44        eCore_arm_armv7f,
45        eCore_arm_armv7k,
46        eCore_arm_armv7s,
47        eCore_arm_xscale,
48        eCore_thumb_generic,
49
50        eCore_ppc_generic,
51        eCore_ppc_ppc601,
52        eCore_ppc_ppc602,
53        eCore_ppc_ppc603,
54        eCore_ppc_ppc603e,
55        eCore_ppc_ppc603ev,
56        eCore_ppc_ppc604,
57        eCore_ppc_ppc604e,
58        eCore_ppc_ppc620,
59        eCore_ppc_ppc750,
60        eCore_ppc_ppc7400,
61        eCore_ppc_ppc7450,
62        eCore_ppc_ppc970,
63
64        eCore_ppc64_generic,
65        eCore_ppc64_ppc970_64,
66
67        eCore_sparc_generic,
68
69        eCore_sparc9_generic,
70
71        eCore_x86_32_i386,
72        eCore_x86_32_i486,
73        eCore_x86_32_i486sx,
74
75        eCore_x86_64_x86_64,
76        kNumCores,
77
78        kCore_invalid,
79        // The following constants are used for wildcard matching only
80        kCore_any,
81
82        kCore_arm_any,
83        kCore_arm_first     = eCore_arm_generic,
84        kCore_arm_last      = eCore_arm_xscale,
85
86        kCore_ppc_any,
87        kCore_ppc_first     = eCore_ppc_generic,
88        kCore_ppc_last      = eCore_ppc_ppc970,
89
90        kCore_ppc64_any,
91        kCore_ppc64_first   = eCore_ppc64_generic,
92        kCore_ppc64_last    = eCore_ppc64_ppc970_64,
93
94        kCore_x86_32_any,
95        kCore_x86_32_first  = eCore_x86_32_i386,
96        kCore_x86_32_last   = eCore_x86_32_i486sx
97    };
98
99    //------------------------------------------------------------------
100    /// Default constructor.
101    ///
102    /// Default constructor that initializes the object with invalid
103    /// cpu type and subtype values.
104    //------------------------------------------------------------------
105    ArchSpec ();
106
107    //------------------------------------------------------------------
108    /// Constructor over triple.
109    ///
110    /// Constructs an ArchSpec with properties consistent with the given
111    /// Triple.
112    //------------------------------------------------------------------
113    explicit
114    ArchSpec (const llvm::Triple &triple);
115    explicit
116    ArchSpec (const char *triple_cstr, Platform *platform);
117    //------------------------------------------------------------------
118    /// Constructor over architecture name.
119    ///
120    /// Constructs an ArchSpec with properties consistent with the given
121    /// object type and architecture name.
122    //------------------------------------------------------------------
123    explicit
124    ArchSpec (ArchitectureType arch_type,
125              uint32_t cpu_type,
126              uint32_t cpu_subtype);
127
128    //------------------------------------------------------------------
129    /// Destructor.
130    ///
131    /// The destructor is virtual in case this class is subclassed.
132    //------------------------------------------------------------------
133    virtual
134    ~ArchSpec ();
135
136    //------------------------------------------------------------------
137    /// Assignment operator.
138    ///
139    /// @param[in] rhs another ArchSpec object to copy.
140    ///
141    /// @return A const reference to this object.
142    //------------------------------------------------------------------
143    const ArchSpec&
144    operator= (const ArchSpec& rhs);
145
146    static uint32_t
147    AutoComplete (const char *name,
148                  StringList &matches);
149
150    //------------------------------------------------------------------
151    /// Returns a static string representing the current architecture.
152    ///
153    /// @return A static string correcponding to the current
154    ///         architecture.
155    //------------------------------------------------------------------
156    const char *
157    GetArchitectureName () const;
158
159    //------------------------------------------------------------------
160    /// Clears the object state.
161    ///
162    /// Clears the object state back to a default invalid state.
163    //------------------------------------------------------------------
164    void
165    Clear ();
166
167    //------------------------------------------------------------------
168    /// Returns the size in bytes of an address of the current
169    /// architecture.
170    ///
171    /// @return The byte size of an address of the current architecture.
172    //------------------------------------------------------------------
173    uint32_t
174    GetAddressByteSize () const;
175
176    //------------------------------------------------------------------
177    /// Returns a machine family for the current architecture.
178    ///
179    /// @return An LLVM arch type.
180    //------------------------------------------------------------------
181    llvm::Triple::ArchType
182    GetMachine () const;
183
184    //------------------------------------------------------------------
185    /// Tests if this ArchSpec is valid.
186    ///
187    /// @return True if the current architecture is valid, false
188    ///         otherwise.
189    //------------------------------------------------------------------
190    bool
191    IsValid () const
192    {
193        return m_core >= eCore_arm_generic && m_core < kNumCores;
194    }
195
196
197    //------------------------------------------------------------------
198    /// Sets this ArchSpec according to the given architecture name.
199    ///
200    /// The architecture name can be one of the generic system default
201    /// values:
202    ///
203    /// @li \c LLDB_ARCH_DEFAULT - The arch the current system defaults
204    ///        to when a program is launched without any extra
205    ///        attributes or settings.
206    /// @li \c LLDB_ARCH_DEFAULT_32BIT - The default host architecture
207    ///        for 32 bit (if any).
208    /// @li \c LLDB_ARCH_DEFAULT_64BIT - The default host architecture
209    ///        for 64 bit (if any).
210    ///
211    /// Alternatively, if the object type of this ArchSpec has been
212    /// configured,  a concrete architecture can be specified to set
213    /// the CPU type ("x86_64" for example).
214    ///
215    /// Finally, an encoded object and archetecture format is accepted.
216    /// The format contains an object type (like "macho" or "elf"),
217    /// followed by a platform dependent encoding of CPU type and
218    /// subtype.  For example:
219    ///
220    ///     "macho"        : Specifies an object type of MachO.
221    ///     "macho-16-6"   : MachO specific encoding for ARMv6.
222    ///     "elf-43        : ELF specific encoding for Sparc V9.
223    ///
224    /// @param[in] arch_name The name of an architecture.
225    ///
226    /// @return True if @p arch_name was successfully translated, false
227    ///         otherwise.
228    //------------------------------------------------------------------
229//    bool
230//    SetArchitecture (const llvm::StringRef& arch_name);
231//
232//    bool
233//    SetArchitecture (const char *arch_name);
234
235    //------------------------------------------------------------------
236    /// Change the architecture object type and CPU type.
237    ///
238    /// @param[in] arch_type The object type of this ArchSpec.
239    ///
240    /// @param[in] cpu The required CPU type.
241    ///
242    /// @return True if the object and CPU type were sucessfully set.
243    //------------------------------------------------------------------
244    bool
245    SetArchitecture (ArchitectureType arch_type,
246                     uint32_t cpu,
247                     uint32_t sub);
248
249    //------------------------------------------------------------------
250    /// Returns the byte order for the architecture specification.
251    ///
252    /// @return The endian enumeration for the current endianness of
253    ///     the architecture specification
254    //------------------------------------------------------------------
255    lldb::ByteOrder
256    GetByteOrder () const;
257
258    //------------------------------------------------------------------
259    /// Sets this ArchSpec's byte order.
260    ///
261    /// In the common case there is no need to call this method as the
262    /// byte order can almost always be determined by the architecture.
263    /// However, many CPU's are bi-endian (ARM, Alpha, PowerPC, etc)
264    /// and the default/assumed byte order may be incorrect.
265    //------------------------------------------------------------------
266    void
267    SetByteOrder (lldb::ByteOrder byte_order)
268    {
269        m_byte_order = byte_order;
270    }
271
272    uint32_t
273    GetMinimumOpcodeByteSize() const;
274
275    uint32_t
276    GetMaximumOpcodeByteSize() const;
277
278    Core
279    GetCore () const
280    {
281        return m_core;
282    }
283
284    uint32_t
285    GetMachOCPUType () const;
286
287    uint32_t
288    GetMachOCPUSubType () const;
289
290    //------------------------------------------------------------------
291    /// Architecture tripple accessor.
292    ///
293    /// @return A triple describing this ArchSpec.
294    //------------------------------------------------------------------
295    llvm::Triple &
296    GetTriple ()
297    {
298        return m_triple;
299    }
300
301    //------------------------------------------------------------------
302    /// Architecture tripple accessor.
303    ///
304    /// @return A triple describing this ArchSpec.
305    //------------------------------------------------------------------
306    const llvm::Triple &
307    GetTriple () const
308    {
309        return m_triple;
310    }
311
312    //------------------------------------------------------------------
313    /// Architecture tripple setter.
314    ///
315    /// Configures this ArchSpec according to the given triple.  If the
316    /// triple has unknown components in all of the vendor, OS, and
317    /// the optional environment field (i.e. "i386-unknown-unknown")
318    /// then default values are taken from the host.  Architecture and
319    /// environment components are used to further resolve the CPU type
320    /// and subtype, endian characteristics, etc.
321    ///
322    /// @return A triple describing this ArchSpec.
323    //------------------------------------------------------------------
324    bool
325    SetTriple (const llvm::Triple &triple);
326
327    bool
328    SetTriple (const char *triple_cstr, Platform *platform);
329
330    //------------------------------------------------------------------
331    /// Returns the default endianness of the architecture.
332    ///
333    /// @return The endian enumeration for the default endianness of
334    ///         the architecture.
335    //------------------------------------------------------------------
336    lldb::ByteOrder
337    GetDefaultEndian () const;
338
339protected:
340    llvm::Triple m_triple;
341    Core m_core;
342    lldb::ByteOrder m_byte_order;
343
344    // Called when m_def or m_entry are changed.  Fills in all remaining
345    // members with default values.
346    void
347    CoreUpdated (bool update_triple);
348};
349
350
351//------------------------------------------------------------------
352/// @fn bool operator== (const ArchSpec& lhs, const ArchSpec& rhs)
353/// @brief Equal to operator.
354///
355/// Tests two ArchSpec objects to see if they are equal.
356///
357/// @param[in] lhs The Left Hand Side ArchSpec object to compare.
358/// @param[in] rhs The Left Hand Side ArchSpec object to compare.
359///
360/// @return true if \a lhs is equal to \a rhs
361//------------------------------------------------------------------
362bool operator==(const ArchSpec& lhs, const ArchSpec& rhs);
363
364//------------------------------------------------------------------
365/// @fn bool operator!= (const ArchSpec& lhs, const ArchSpec& rhs)
366/// @brief Not equal to operator.
367///
368/// Tests two ArchSpec objects to see if they are not equal.
369///
370/// @param[in] lhs The Left Hand Side ArchSpec object to compare.
371/// @param[in] rhs The Left Hand Side ArchSpec object to compare.
372///
373/// @return true if \a lhs is not equal to \a rhs
374//------------------------------------------------------------------
375bool operator!=(const ArchSpec& lhs, const ArchSpec& rhs);
376
377//------------------------------------------------------------------
378/// @fn bool operator< (const ArchSpec& lhs, const ArchSpec& rhs)
379/// @brief Less than operator.
380///
381/// Tests two ArchSpec objects to see if \a lhs is less than \a
382/// rhs.
383///
384/// @param[in] lhs The Left Hand Side ArchSpec object to compare.
385/// @param[in] rhs The Left Hand Side ArchSpec object to compare.
386///
387/// @return true if \a lhs is less than \a rhs
388//------------------------------------------------------------------
389bool operator< (const ArchSpec& lhs, const ArchSpec& rhs);
390
391} // namespace lldb_private
392
393#endif  // #if defined(__cplusplus)
394#endif  // #ifndef liblldb_ArchSpec_h_
395