]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Utility/ArchSpec.h
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Utility / ArchSpec.h
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 LLDB_UTILITY_ARCHSPEC_H
11 #define LLDB_UTILITY_ARCHSPEC_H
12
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "lldb/lldb-forward.h"
16 #include "lldb/lldb-private-enumerations.h"
17 #include "llvm/ADT/StringRef.h" // for StringRef
18 #include "llvm/ADT/Triple.h"
19 #include <cstddef> // for size_t
20 #include <cstdint> // for uint32_t
21 #include <string>  // for string
22
23 namespace lldb_private {
24
25 //----------------------------------------------------------------------
26 /// @class ArchSpec ArchSpec.h "lldb/Utility/ArchSpec.h"
27 /// @brief An architecture specification class.
28 ///
29 /// A class designed to be created from a cpu type and subtype, a
30 /// string representation, or an llvm::Triple.  Keeping all of the
31 /// conversions of strings to architecture enumeration values confined
32 /// to this class allows new architecture support to be added easily.
33 //----------------------------------------------------------------------
34 class ArchSpec {
35 public:
36   enum MIPSSubType {
37     eMIPSSubType_unknown,
38     eMIPSSubType_mips32,
39     eMIPSSubType_mips32r2,
40     eMIPSSubType_mips32r6,
41     eMIPSSubType_mips32el,
42     eMIPSSubType_mips32r2el,
43     eMIPSSubType_mips32r6el,
44     eMIPSSubType_mips64,
45     eMIPSSubType_mips64r2,
46     eMIPSSubType_mips64r6,
47     eMIPSSubType_mips64el,
48     eMIPSSubType_mips64r2el,
49     eMIPSSubType_mips64r6el,
50   };
51
52   // Masks for the ases word of an ABI flags structure.
53   enum MIPSASE {
54     eMIPSAse_dsp = 0x00000001,       // DSP ASE
55     eMIPSAse_dspr2 = 0x00000002,     // DSP R2 ASE
56     eMIPSAse_eva = 0x00000004,       // Enhanced VA Scheme
57     eMIPSAse_mcu = 0x00000008,       // MCU (MicroController) ASE
58     eMIPSAse_mdmx = 0x00000010,      // MDMX ASE
59     eMIPSAse_mips3d = 0x00000020,    // MIPS-3D ASE
60     eMIPSAse_mt = 0x00000040,        // MT ASE
61     eMIPSAse_smartmips = 0x00000080, // SmartMIPS ASE
62     eMIPSAse_virt = 0x00000100,      // VZ ASE
63     eMIPSAse_msa = 0x00000200,       // MSA ASE
64     eMIPSAse_mips16 = 0x00000400,    // MIPS16 ASE
65     eMIPSAse_micromips = 0x00000800, // MICROMIPS ASE
66     eMIPSAse_xpa = 0x00001000,       // XPA ASE
67     eMIPSAse_mask = 0x00001fff,
68     eMIPSABI_O32 = 0x00002000,
69     eMIPSABI_N32 = 0x00004000,
70     eMIPSABI_N64 = 0x00008000,
71     eMIPSABI_O64 = 0x00020000,
72     eMIPSABI_EABI32 = 0x00040000,
73     eMIPSABI_EABI64 = 0x00080000,
74     eMIPSABI_mask = 0x000ff000
75   };
76
77   // MIPS Floating point ABI Values
78   enum MIPS_ABI_FP {
79     eMIPS_ABI_FP_ANY = 0x00000000,
80     eMIPS_ABI_FP_DOUBLE = 0x00100000, // hard float / -mdouble-float
81     eMIPS_ABI_FP_SINGLE = 0x00200000, // hard float / -msingle-float
82     eMIPS_ABI_FP_SOFT = 0x00300000,   // soft float
83     eMIPS_ABI_FP_OLD_64 = 0x00400000, // -mips32r2 -mfp64
84     eMIPS_ABI_FP_XX = 0x00500000,     // -mfpxx
85     eMIPS_ABI_FP_64 = 0x00600000,     // -mips32r2 -mfp64
86     eMIPS_ABI_FP_64A = 0x00700000,    // -mips32r2 -mfp64 -mno-odd-spreg
87     eMIPS_ABI_FP_mask = 0x00700000
88   };
89
90   // ARM specific e_flags
91   enum ARMeflags {
92     eARM_abi_soft_float = 0x00000200,
93     eARM_abi_hard_float = 0x00000400
94   };
95
96   enum Core {
97     eCore_arm_generic,
98     eCore_arm_armv4,
99     eCore_arm_armv4t,
100     eCore_arm_armv5,
101     eCore_arm_armv5e,
102     eCore_arm_armv5t,
103     eCore_arm_armv6,
104     eCore_arm_armv6m,
105     eCore_arm_armv7,
106     eCore_arm_armv7f,
107     eCore_arm_armv7s,
108     eCore_arm_armv7k,
109     eCore_arm_armv7m,
110     eCore_arm_armv7em,
111     eCore_arm_xscale,
112
113     eCore_thumb,
114     eCore_thumbv4t,
115     eCore_thumbv5,
116     eCore_thumbv5e,
117     eCore_thumbv6,
118     eCore_thumbv6m,
119     eCore_thumbv7,
120     eCore_thumbv7s,
121     eCore_thumbv7k,
122     eCore_thumbv7f,
123     eCore_thumbv7m,
124     eCore_thumbv7em,
125     eCore_arm_arm64,
126     eCore_arm_armv8,
127     eCore_arm_aarch64,
128
129     eCore_mips32,
130     eCore_mips32r2,
131     eCore_mips32r3,
132     eCore_mips32r5,
133     eCore_mips32r6,
134     eCore_mips32el,
135     eCore_mips32r2el,
136     eCore_mips32r3el,
137     eCore_mips32r5el,
138     eCore_mips32r6el,
139     eCore_mips64,
140     eCore_mips64r2,
141     eCore_mips64r3,
142     eCore_mips64r5,
143     eCore_mips64r6,
144     eCore_mips64el,
145     eCore_mips64r2el,
146     eCore_mips64r3el,
147     eCore_mips64r5el,
148     eCore_mips64r6el,
149
150     eCore_ppc_generic,
151     eCore_ppc_ppc601,
152     eCore_ppc_ppc602,
153     eCore_ppc_ppc603,
154     eCore_ppc_ppc603e,
155     eCore_ppc_ppc603ev,
156     eCore_ppc_ppc604,
157     eCore_ppc_ppc604e,
158     eCore_ppc_ppc620,
159     eCore_ppc_ppc750,
160     eCore_ppc_ppc7400,
161     eCore_ppc_ppc7450,
162     eCore_ppc_ppc970,
163
164     eCore_ppc64le_generic,
165     eCore_ppc64_generic,
166     eCore_ppc64_ppc970_64,
167
168     eCore_s390x_generic,
169
170     eCore_sparc_generic,
171
172     eCore_sparc9_generic,
173
174     eCore_x86_32_i386,
175     eCore_x86_32_i486,
176     eCore_x86_32_i486sx,
177     eCore_x86_32_i686,
178
179     eCore_x86_64_x86_64,
180     eCore_x86_64_x86_64h, // Haswell enabled x86_64
181     eCore_hexagon_generic,
182     eCore_hexagon_hexagonv4,
183     eCore_hexagon_hexagonv5,
184
185     eCore_uknownMach32,
186     eCore_uknownMach64,
187
188     eCore_kalimba3,
189     eCore_kalimba4,
190     eCore_kalimba5,
191
192     kNumCores,
193
194     kCore_invalid,
195     // The following constants are used for wildcard matching only
196     kCore_any,
197     kCore_arm_any,
198     kCore_ppc_any,
199     kCore_ppc64_any,
200     kCore_x86_32_any,
201     kCore_x86_64_any,
202     kCore_hexagon_any,
203
204     kCore_arm_first = eCore_arm_generic,
205     kCore_arm_last = eCore_arm_xscale,
206
207     kCore_thumb_first = eCore_thumb,
208     kCore_thumb_last = eCore_thumbv7em,
209
210     kCore_ppc_first = eCore_ppc_generic,
211     kCore_ppc_last = eCore_ppc_ppc970,
212
213     kCore_ppc64_first = eCore_ppc64_generic,
214     kCore_ppc64_last = eCore_ppc64_ppc970_64,
215
216     kCore_x86_32_first = eCore_x86_32_i386,
217     kCore_x86_32_last = eCore_x86_32_i686,
218
219     kCore_x86_64_first = eCore_x86_64_x86_64,
220     kCore_x86_64_last = eCore_x86_64_x86_64h,
221
222     kCore_hexagon_first = eCore_hexagon_generic,
223     kCore_hexagon_last = eCore_hexagon_hexagonv5,
224
225     kCore_kalimba_first = eCore_kalimba3,
226     kCore_kalimba_last = eCore_kalimba5,
227
228     kCore_mips32_first = eCore_mips32,
229     kCore_mips32_last = eCore_mips32r6,
230
231     kCore_mips32el_first = eCore_mips32el,
232     kCore_mips32el_last = eCore_mips32r6el,
233
234     kCore_mips64_first = eCore_mips64,
235     kCore_mips64_last = eCore_mips64r6,
236
237     kCore_mips64el_first = eCore_mips64el,
238     kCore_mips64el_last = eCore_mips64r6el,
239
240     kCore_mips_first = eCore_mips32,
241     kCore_mips_last = eCore_mips64r6el
242
243   };
244
245   //------------------------------------------------------------------
246   /// Default constructor.
247   ///
248   /// Default constructor that initializes the object with invalid
249   /// cpu type and subtype values.
250   //------------------------------------------------------------------
251   ArchSpec();
252
253   //------------------------------------------------------------------
254   /// Constructor over triple.
255   ///
256   /// Constructs an ArchSpec with properties consistent with the given
257   /// Triple.
258   //------------------------------------------------------------------
259   explicit ArchSpec(const llvm::Triple &triple);
260   explicit ArchSpec(const char *triple_cstr);
261   explicit ArchSpec(llvm::StringRef triple_str);
262   //------------------------------------------------------------------
263   /// Constructor over architecture name.
264   ///
265   /// Constructs an ArchSpec with properties consistent with the given
266   /// object type and architecture name.
267   //------------------------------------------------------------------
268   explicit ArchSpec(ArchitectureType arch_type, uint32_t cpu_type,
269                     uint32_t cpu_subtype);
270
271   //------------------------------------------------------------------
272   /// Destructor.
273   //------------------------------------------------------------------
274   ~ArchSpec();
275
276   //------------------------------------------------------------------
277   /// Assignment operator.
278   ///
279   /// @param[in] rhs another ArchSpec object to copy.
280   ///
281   /// @return A const reference to this object.
282   //------------------------------------------------------------------
283   const ArchSpec &operator=(const ArchSpec &rhs);
284
285   //---------------------------------------------------------------------------
286   /// Returns true if the OS, vendor and environment fields of the triple are
287   /// unset. The triple is expected to be normalized (llvm::Triple::normalize).
288   //---------------------------------------------------------------------------
289   static bool ContainsOnlyArch(const llvm::Triple &normalized_triple);
290
291   static size_t AutoComplete(llvm::StringRef name, StringList &matches);
292
293   //------------------------------------------------------------------
294   /// Returns a static string representing the current architecture.
295   ///
296   /// @return A static string correcponding to the current
297   ///         architecture.
298   //------------------------------------------------------------------
299   const char *GetArchitectureName() const;
300
301   //-----------------------------------------------------------------
302   /// if MIPS architecture return true.
303   ///
304   ///  @return a boolean value.
305   //-----------------------------------------------------------------
306   bool IsMIPS() const;
307
308   //------------------------------------------------------------------
309   /// Returns a string representing current architecture as a target CPU
310   /// for tools like compiler, disassembler etc.
311   ///
312   /// @return A string representing target CPU for the current
313   ///         architecture.
314   //------------------------------------------------------------------
315   std::string GetClangTargetCPU() const;
316
317   //------------------------------------------------------------------
318   /// Return a string representing target application ABI.
319   ///
320   /// @return A string representing target application ABI.
321   //------------------------------------------------------------------
322   std::string GetTargetABI() const;
323
324   //------------------------------------------------------------------
325   /// Clears the object state.
326   ///
327   /// Clears the object state back to a default invalid state.
328   //------------------------------------------------------------------
329   void Clear();
330
331   //------------------------------------------------------------------
332   /// Returns the size in bytes of an address of the current
333   /// architecture.
334   ///
335   /// @return The byte size of an address of the current architecture.
336   //------------------------------------------------------------------
337   uint32_t GetAddressByteSize() const;
338
339   //------------------------------------------------------------------
340   /// Returns a machine family for the current architecture.
341   ///
342   /// @return An LLVM arch type.
343   //------------------------------------------------------------------
344   llvm::Triple::ArchType GetMachine() const;
345
346   //------------------------------------------------------------------
347   /// Returns the distribution id of the architecture.
348   ///
349   /// This will be something like "ubuntu", "fedora", etc. on Linux.
350   ///
351   /// @return A ConstString ref containing the distribution id,
352   ///         potentially empty.
353   //------------------------------------------------------------------
354   const ConstString &GetDistributionId() const;
355
356   //------------------------------------------------------------------
357   /// Set the distribution id of the architecture.
358   ///
359   /// This will be something like "ubuntu", "fedora", etc. on Linux.
360   /// This should be the same value returned by
361   /// HostInfo::GetDistributionId ().
362   ///------------------------------------------------------------------
363   void SetDistributionId(const char *distribution_id);
364
365   //------------------------------------------------------------------
366   /// Tests if this ArchSpec is valid.
367   ///
368   /// @return True if the current architecture is valid, false
369   ///         otherwise.
370   //------------------------------------------------------------------
371   bool IsValid() const {
372     return m_core >= eCore_arm_generic && m_core < kNumCores;
373   }
374
375   bool TripleVendorWasSpecified() const {
376     return !m_triple.getVendorName().empty();
377   }
378
379   bool TripleVendorIsUnspecifiedUnknown() const {
380     return m_triple.getVendor() == llvm::Triple::UnknownVendor &&
381            m_triple.getVendorName().empty();
382   }
383
384   bool TripleOSWasSpecified() const { return !m_triple.getOSName().empty(); }
385
386   bool TripleEnvironmentWasSpecified() const {
387     return !m_triple.getEnvironmentName().empty();
388   }
389
390   bool TripleOSIsUnspecifiedUnknown() const {
391     return m_triple.getOS() == llvm::Triple::UnknownOS &&
392            m_triple.getOSName().empty();
393   }
394
395   //------------------------------------------------------------------
396   /// Merges fields from another ArchSpec into this ArchSpec.
397   ///
398   /// This will use the supplied ArchSpec to fill in any fields of
399   /// the triple in this ArchSpec which were unspecified.  This can
400   /// be used to refine a generic ArchSpec with a more specific one.
401   /// For example, if this ArchSpec's triple is something like
402   /// i386-unknown-unknown-unknown, and we have a triple which is
403   /// x64-pc-windows-msvc, then merging that triple into this one
404   /// will result in the triple i386-pc-windows-msvc.
405   ///
406   //------------------------------------------------------------------
407   void MergeFrom(const ArchSpec &other);
408
409   //------------------------------------------------------------------
410   /// Change the architecture object type, CPU type and OS type.
411   ///
412   /// @param[in] arch_type The object type of this ArchSpec.
413   ///
414   /// @param[in] cpu The required CPU type.
415   ///
416   /// @param[in] os The optional OS type
417   /// The default value of 0 was chosen to from the ELF spec value
418   /// ELFOSABI_NONE.  ELF is the only one using this parameter.  If another
419   /// format uses this parameter and 0 does not work, use a value over
420   /// 255 because in the ELF header this is value is only a byte.
421   ///
422   /// @return True if the object, and CPU were successfully set.
423   ///
424   /// As a side effect, the vendor value is usually set to unknown.
425   /// The exections are
426   ///   aarch64-apple-ios
427   ///   arm-apple-ios
428   ///   thumb-apple-ios
429   ///   x86-apple-
430   ///   x86_64-apple-
431   ///
432   /// As a side effect, the os value is usually set to unknown
433   /// The exceptions are
434   ///   *-*-aix
435   ///   aarch64-apple-ios
436   ///   arm-apple-ios
437   ///   thumb-apple-ios
438   ///   powerpc-apple-darwin
439   ///   *-*-freebsd
440   ///   *-*-linux
441   ///   *-*-netbsd
442   ///   *-*-openbsd
443   ///   *-*-solaris
444   //------------------------------------------------------------------
445   bool SetArchitecture(ArchitectureType arch_type, uint32_t cpu, uint32_t sub,
446                        uint32_t os = 0);
447
448   //------------------------------------------------------------------
449   /// Returns the byte order for the architecture specification.
450   ///
451   /// @return The endian enumeration for the current endianness of
452   ///     the architecture specification
453   //------------------------------------------------------------------
454   lldb::ByteOrder GetByteOrder() const;
455
456   //------------------------------------------------------------------
457   /// Sets this ArchSpec's byte order.
458   ///
459   /// In the common case there is no need to call this method as the
460   /// byte order can almost always be determined by the architecture.
461   /// However, many CPU's are bi-endian (ARM, Alpha, PowerPC, etc)
462   /// and the default/assumed byte order may be incorrect.
463   //------------------------------------------------------------------
464   void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; }
465
466   uint32_t GetMinimumOpcodeByteSize() const;
467
468   uint32_t GetMaximumOpcodeByteSize() const;
469
470   Core GetCore() const { return m_core; }
471
472   uint32_t GetMachOCPUType() const;
473
474   uint32_t GetMachOCPUSubType() const;
475
476   //------------------------------------------------------------------
477   /// Architecture data byte width accessor
478   ///
479   /// @return the size in 8-bit (host) bytes of a minimum addressable
480   /// unit from the Architecture's data bus
481   //------------------------------------------------------------------
482   uint32_t GetDataByteSize() const;
483
484   //------------------------------------------------------------------
485   /// Architecture code byte width accessor
486   ///
487   /// @return the size in 8-bit (host) bytes of a minimum addressable
488   /// unit from the Architecture's code bus
489   //------------------------------------------------------------------
490   uint32_t GetCodeByteSize() const;
491
492   //------------------------------------------------------------------
493   /// Architecture tripple accessor.
494   ///
495   /// @return A triple describing this ArchSpec.
496   //------------------------------------------------------------------
497   llvm::Triple &GetTriple() { return m_triple; }
498
499   //------------------------------------------------------------------
500   /// Architecture tripple accessor.
501   ///
502   /// @return A triple describing this ArchSpec.
503   //------------------------------------------------------------------
504   const llvm::Triple &GetTriple() const { return m_triple; }
505
506   void DumpTriple(Stream &s) const;
507
508   //------------------------------------------------------------------
509   /// Architecture tripple setter.
510   ///
511   /// Configures this ArchSpec according to the given triple.  If the
512   /// triple has unknown components in all of the vendor, OS, and
513   /// the optional environment field (i.e. "i386-unknown-unknown")
514   /// then default values are taken from the host.  Architecture and
515   /// environment components are used to further resolve the CPU type
516   /// and subtype, endian characteristics, etc.
517   ///
518   /// @return A triple describing this ArchSpec.
519   //------------------------------------------------------------------
520   bool SetTriple(const llvm::Triple &triple);
521
522   bool SetTriple(llvm::StringRef triple_str);
523
524   //------------------------------------------------------------------
525   /// Returns the default endianness of the architecture.
526   ///
527   /// @return The endian enumeration for the default endianness of
528   ///         the architecture.
529   //------------------------------------------------------------------
530   lldb::ByteOrder GetDefaultEndian() const;
531
532   //------------------------------------------------------------------
533   /// Returns true if 'char' is a signed type by defualt in the
534   /// architecture false otherwise
535   ///
536   /// @return True if 'char' is a signed type by default on the
537   ///         architecture and false otherwise.
538   //------------------------------------------------------------------
539   bool CharIsSignedByDefault() const;
540
541   //------------------------------------------------------------------
542   /// Compare an ArchSpec to another ArchSpec, requiring an exact cpu
543   /// type match between them.
544   /// e.g. armv7s is not an exact match with armv7 - this would return false
545   ///
546   /// @return true if the two ArchSpecs match.
547   //------------------------------------------------------------------
548   bool IsExactMatch(const ArchSpec &rhs) const;
549
550   //------------------------------------------------------------------
551   /// Compare an ArchSpec to another ArchSpec, requiring a compatible
552   /// cpu type match between them.
553   /// e.g. armv7s is compatible with armv7 - this method would return true
554   ///
555   /// @return true if the two ArchSpecs are compatible
556   //------------------------------------------------------------------
557   bool IsCompatibleMatch(const ArchSpec &rhs) const;
558
559   bool IsFullySpecifiedTriple() const;
560
561   void PiecewiseTripleCompare(const ArchSpec &other, bool &arch_different,
562                               bool &vendor_different, bool &os_different,
563                               bool &os_version_different,
564                               bool &env_different) const;
565
566   //------------------------------------------------------------------
567   /// Detect whether this architecture uses thumb code exclusively
568   ///
569   /// Some embedded ARM chips (e.g. the ARM Cortex M0-7 line) can
570   /// only execute the Thumb instructions, never Arm.  We should normally
571   /// pick up arm/thumbness from their the processor status bits (cpsr/xpsr)
572   /// or hints on each function - but when doing bare-boards low level
573   /// debugging (especially common with these embedded processors), we may
574   /// not have those things easily accessible.
575   ///
576   /// @return true if this is an arm ArchSpec which can only execute Thumb
577   ///         instructions
578   //------------------------------------------------------------------
579   bool IsAlwaysThumbInstructions() const;
580
581   uint32_t GetFlags() const { return m_flags; }
582
583   void SetFlags(uint32_t flags) { m_flags = flags; }
584
585   void SetFlags(std::string elf_abi);
586
587 protected:
588   bool IsEqualTo(const ArchSpec &rhs, bool exact_match) const;
589   void UpdateCore();
590
591   llvm::Triple m_triple;
592   Core m_core = kCore_invalid;
593   lldb::ByteOrder m_byte_order = lldb::eByteOrderInvalid;
594
595   // Additional arch flags which we cannot get from triple and core
596   // For MIPS these are application specific extensions like
597   // micromips, mips16 etc.
598   uint32_t m_flags = 0;
599
600   ConstString m_distribution_id;
601
602   // Called when m_def or m_entry are changed.  Fills in all remaining
603   // members with default values.
604   void CoreUpdated(bool update_triple);
605 };
606
607 //------------------------------------------------------------------
608 /// @fn bool operator< (const ArchSpec& lhs, const ArchSpec& rhs)
609 /// @brief Less than operator.
610 ///
611 /// Tests two ArchSpec objects to see if \a lhs is less than \a
612 /// rhs.
613 ///
614 /// @param[in] lhs The Left Hand Side ArchSpec object to compare.
615 /// @param[in] rhs The Left Hand Side ArchSpec object to compare.
616 ///
617 /// @return true if \a lhs is less than \a rhs
618 //------------------------------------------------------------------
619 bool operator<(const ArchSpec &lhs, const ArchSpec &rhs);
620
621 bool ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str, ArchSpec &arch);
622
623 } // namespace lldb_private
624
625 #endif // #ifndef LLDB_UTILITY_ARCHSPEC_H