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