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