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