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