ArchSpec.h 17 KB

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