tdesc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* Copyright (C) 2006-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef COMMON_TDESC_H
  14. #define COMMON_TDESC_H
  15. struct tdesc_feature;
  16. struct tdesc_type;
  17. struct tdesc_type_builtin;
  18. struct tdesc_type_vector;
  19. struct tdesc_type_with_fields;
  20. struct tdesc_reg;
  21. struct target_desc;
  22. /* The interface to visit different elements of target description. */
  23. class tdesc_element_visitor
  24. {
  25. public:
  26. virtual void visit_pre (const target_desc *e)
  27. {}
  28. virtual void visit_post (const target_desc *e)
  29. {}
  30. virtual void visit_pre (const tdesc_feature *e)
  31. {}
  32. virtual void visit_post (const tdesc_feature *e)
  33. {}
  34. virtual void visit (const tdesc_type_builtin *e)
  35. {}
  36. virtual void visit (const tdesc_type_vector *e)
  37. {}
  38. virtual void visit (const tdesc_type_with_fields *e)
  39. {}
  40. virtual void visit (const tdesc_reg *e)
  41. {}
  42. };
  43. class tdesc_element
  44. {
  45. public:
  46. virtual void accept (tdesc_element_visitor &v) const = 0;
  47. };
  48. /* An individual register from a target description. */
  49. struct tdesc_reg : tdesc_element
  50. {
  51. tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
  52. int regnum, int save_restore_, const char *group_,
  53. int bitsize_, const char *type_);
  54. virtual ~tdesc_reg () = default;
  55. DISABLE_COPY_AND_ASSIGN (tdesc_reg);
  56. /* The name of this register. In standard features, it may be
  57. recognized by the architecture support code, or it may be purely
  58. for the user. */
  59. std::string name;
  60. /* The register number used by this target to refer to this
  61. register. This is used for remote p/P packets and to determine
  62. the ordering of registers in the remote g/G packets. */
  63. long target_regnum;
  64. /* If this flag is set, GDB should save and restore this register
  65. around calls to an inferior function. */
  66. int save_restore;
  67. /* The name of the register group containing this register, or empty
  68. if the group should be automatically determined from the
  69. register's type. If this is "general", "float", or "vector", the
  70. corresponding "info" command should display this register's
  71. value. It can be an arbitrary string, but should be limited to
  72. alphanumeric characters and internal hyphens. Currently other
  73. strings are ignored (treated as empty). */
  74. std::string group;
  75. /* The size of the register, in bits. */
  76. int bitsize;
  77. /* The type of the register. This string corresponds to either
  78. a named type from the target description or a predefined
  79. type from GDB. */
  80. std::string type;
  81. /* The target-described type corresponding to TYPE, if found. */
  82. struct tdesc_type *tdesc_type;
  83. void accept (tdesc_element_visitor &v) const override
  84. {
  85. v.visit (this);
  86. }
  87. bool operator== (const tdesc_reg &other) const
  88. {
  89. return (name == other.name
  90. && target_regnum == other.target_regnum
  91. && save_restore == other.save_restore
  92. && bitsize == other.bitsize
  93. && group == other.group
  94. && type == other.type);
  95. }
  96. bool operator!= (const tdesc_reg &other) const
  97. {
  98. return !(*this == other);
  99. }
  100. };
  101. typedef std::unique_ptr<tdesc_reg> tdesc_reg_up;
  102. /* Declaration of a structure that holds information about one
  103. "compatibility" entry within a target description. */
  104. struct tdesc_compatible_info;
  105. /* A pointer to a single piece of compatibility information. */
  106. typedef std::unique_ptr<tdesc_compatible_info> tdesc_compatible_info_up;
  107. /* Return a vector of compatibility information pointers from the target
  108. description TARGET_DESC. */
  109. const std::vector<tdesc_compatible_info_up> &tdesc_compatible_info_list
  110. (const target_desc *target_desc);
  111. /* Return the architecture name from a compatibility information
  112. COMPATIBLE. */
  113. const char *tdesc_compatible_info_arch_name
  114. (const tdesc_compatible_info_up &compatible);
  115. enum tdesc_type_kind
  116. {
  117. /* Predefined types. */
  118. TDESC_TYPE_BOOL,
  119. TDESC_TYPE_INT8,
  120. TDESC_TYPE_INT16,
  121. TDESC_TYPE_INT32,
  122. TDESC_TYPE_INT64,
  123. TDESC_TYPE_INT128,
  124. TDESC_TYPE_UINT8,
  125. TDESC_TYPE_UINT16,
  126. TDESC_TYPE_UINT32,
  127. TDESC_TYPE_UINT64,
  128. TDESC_TYPE_UINT128,
  129. TDESC_TYPE_CODE_PTR,
  130. TDESC_TYPE_DATA_PTR,
  131. TDESC_TYPE_IEEE_HALF,
  132. TDESC_TYPE_IEEE_SINGLE,
  133. TDESC_TYPE_IEEE_DOUBLE,
  134. TDESC_TYPE_ARM_FPA_EXT,
  135. TDESC_TYPE_I387_EXT,
  136. TDESC_TYPE_BFLOAT16,
  137. /* Types defined by a target feature. */
  138. TDESC_TYPE_VECTOR,
  139. TDESC_TYPE_STRUCT,
  140. TDESC_TYPE_UNION,
  141. TDESC_TYPE_FLAGS,
  142. TDESC_TYPE_ENUM
  143. };
  144. struct tdesc_type : tdesc_element
  145. {
  146. tdesc_type (const std::string &name_, enum tdesc_type_kind kind_)
  147. : name (name_), kind (kind_)
  148. {}
  149. virtual ~tdesc_type () = default;
  150. DISABLE_COPY_AND_ASSIGN (tdesc_type);
  151. /* The name of this type. */
  152. std::string name;
  153. /* Identify the kind of this type. */
  154. enum tdesc_type_kind kind;
  155. bool operator== (const tdesc_type &other) const
  156. {
  157. return name == other.name && kind == other.kind;
  158. }
  159. bool operator!= (const tdesc_type &other) const
  160. {
  161. return !(*this == other);
  162. }
  163. };
  164. typedef std::unique_ptr<tdesc_type> tdesc_type_up;
  165. struct tdesc_type_builtin : tdesc_type
  166. {
  167. tdesc_type_builtin (const std::string &name, enum tdesc_type_kind kind)
  168. : tdesc_type (name, kind)
  169. {}
  170. void accept (tdesc_element_visitor &v) const override
  171. {
  172. v.visit (this);
  173. }
  174. };
  175. /* tdesc_type for vector types. */
  176. struct tdesc_type_vector : tdesc_type
  177. {
  178. tdesc_type_vector (const std::string &name, tdesc_type *element_type_,
  179. int count_)
  180. : tdesc_type (name, TDESC_TYPE_VECTOR),
  181. element_type (element_type_), count (count_)
  182. {}
  183. void accept (tdesc_element_visitor &v) const override
  184. {
  185. v.visit (this);
  186. }
  187. struct tdesc_type *element_type;
  188. int count;
  189. };
  190. /* A named type from a target description. */
  191. struct tdesc_type_field
  192. {
  193. tdesc_type_field (const std::string &name_, tdesc_type *type_,
  194. int start_, int end_)
  195. : name (name_), type (type_), start (start_), end (end_)
  196. {}
  197. std::string name;
  198. struct tdesc_type *type;
  199. /* For non-enum-values, either both are -1 (non-bitfield), or both are
  200. not -1 (bitfield). For enum values, start is the value (which could be
  201. -1), end is -1. */
  202. int start, end;
  203. };
  204. /* tdesc_type for struct, union, flags, and enum types. */
  205. struct tdesc_type_with_fields : tdesc_type
  206. {
  207. tdesc_type_with_fields (const std::string &name, tdesc_type_kind kind,
  208. int size_ = 0)
  209. : tdesc_type (name, kind), size (size_)
  210. {}
  211. void accept (tdesc_element_visitor &v) const override
  212. {
  213. v.visit (this);
  214. }
  215. std::vector<tdesc_type_field> fields;
  216. int size;
  217. };
  218. /* A feature from a target description. Each feature is a collection
  219. of other elements, e.g. registers and types. */
  220. struct tdesc_feature : tdesc_element
  221. {
  222. tdesc_feature (const std::string &name_)
  223. : name (name_)
  224. {}
  225. virtual ~tdesc_feature () = default;
  226. DISABLE_COPY_AND_ASSIGN (tdesc_feature);
  227. /* The name of this feature. It may be recognized by the architecture
  228. support code. */
  229. std::string name;
  230. /* The registers associated with this feature. */
  231. std::vector<tdesc_reg_up> registers;
  232. /* The types associated with this feature. */
  233. std::vector<tdesc_type_up> types;
  234. void accept (tdesc_element_visitor &v) const override;
  235. bool operator== (const tdesc_feature &other) const;
  236. bool operator!= (const tdesc_feature &other) const
  237. {
  238. return !(*this == other);
  239. }
  240. };
  241. typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
  242. /* A deleter adapter for a target_desc. There are different
  243. implementations of this deleter class in gdb and gdbserver because even
  244. though the target_desc name is shared between the two projects, the
  245. actual implementations of target_desc are completely different. */
  246. struct target_desc_deleter
  247. {
  248. void operator() (struct target_desc *desc) const;
  249. };
  250. /* A unique pointer specialization that holds a target_desc. */
  251. typedef std::unique_ptr<target_desc, target_desc_deleter> target_desc_up;
  252. /* Allocate a new target_desc. */
  253. target_desc_up allocate_target_description (void);
  254. /* Set TARGET_DESC's architecture by NAME. */
  255. void set_tdesc_architecture (target_desc *target_desc,
  256. const char *name);
  257. /* Return the architecture associated with this target description as a string,
  258. or NULL if no architecture was specified. */
  259. const char *tdesc_architecture_name (const struct target_desc *target_desc);
  260. /* Set TARGET_DESC's osabi by NAME. */
  261. void set_tdesc_osabi (target_desc *target_desc, const char *name);
  262. /* Return the osabi associated with this target description as a string,
  263. or NULL if no osabi was specified. */
  264. const char *tdesc_osabi_name (const struct target_desc *target_desc);
  265. /* Return the type associated with ID in the context of FEATURE, or
  266. NULL if none. */
  267. struct tdesc_type *tdesc_named_type (const struct tdesc_feature *feature,
  268. const char *id);
  269. /* Return the created feature named NAME in target description TDESC. */
  270. struct tdesc_feature *tdesc_create_feature (struct target_desc *tdesc,
  271. const char *name);
  272. /* Return the created vector tdesc_type named NAME in FEATURE. */
  273. struct tdesc_type *tdesc_create_vector (struct tdesc_feature *feature,
  274. const char *name,
  275. struct tdesc_type *field_type,
  276. int count);
  277. /* Return the created struct tdesc_type named NAME in FEATURE. */
  278. tdesc_type_with_fields *tdesc_create_struct (struct tdesc_feature *feature,
  279. const char *name);
  280. /* Return the created union tdesc_type named NAME in FEATURE. */
  281. tdesc_type_with_fields *tdesc_create_union (struct tdesc_feature *feature,
  282. const char *name);
  283. /* Return the created flags tdesc_type named NAME in FEATURE. */
  284. tdesc_type_with_fields *tdesc_create_flags (struct tdesc_feature *feature,
  285. const char *name,
  286. int size);
  287. /* Return the created enum tdesc_type named NAME in FEATURE. */
  288. tdesc_type_with_fields *tdesc_create_enum (struct tdesc_feature *feature,
  289. const char *name,
  290. int size);
  291. /* Add a new field to TYPE. FIELD_NAME is its name, and FIELD_TYPE is
  292. its type. */
  293. void tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
  294. struct tdesc_type *field_type);
  295. /* Add a new bitfield to TYPE, with range START to END. FIELD_NAME is its name,
  296. and FIELD_TYPE is its type. */
  297. void tdesc_add_typed_bitfield (tdesc_type_with_fields *type,
  298. const char *field_name,
  299. int start, int end,
  300. struct tdesc_type *field_type);
  301. /* Set the total length of TYPE. Structs which contain bitfields may
  302. omit the reserved bits, so the end of the last field may not
  303. suffice. */
  304. void tdesc_set_struct_size (tdesc_type_with_fields *type, int size);
  305. /* Add a new untyped bitfield to TYPE.
  306. Untyped bitfields become either uint32 or uint64 depending on the size
  307. of the underlying type. */
  308. void tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
  309. int start, int end);
  310. /* A flag is just a typed(bool) single-bit bitfield.
  311. This function is kept to minimize changes in generated files. */
  312. void tdesc_add_flag (tdesc_type_with_fields *type, int start,
  313. const char *flag_name);
  314. /* Add field with VALUE and NAME to the enum TYPE. */
  315. void tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
  316. const char *name);
  317. /* Create a register in feature FEATURE. */
  318. void tdesc_create_reg (struct tdesc_feature *feature, const char *name,
  319. int regnum, int save_restore, const char *group,
  320. int bitsize, const char *type);
  321. /* Return the tdesc in string XML format. */
  322. const char *tdesc_get_features_xml (const target_desc *tdesc);
  323. /* Print target description as xml. */
  324. class print_xml_feature : public tdesc_element_visitor
  325. {
  326. public:
  327. print_xml_feature (std::string *buffer_)
  328. : m_buffer (buffer_),
  329. m_depth (0)
  330. {}
  331. void visit_pre (const target_desc *e) override;
  332. void visit_post (const target_desc *e) override;
  333. void visit_pre (const tdesc_feature *e) override;
  334. void visit_post (const tdesc_feature *e) override;
  335. void visit (const tdesc_type_builtin *type) override;
  336. void visit (const tdesc_type_vector *type) override;
  337. void visit (const tdesc_type_with_fields *type) override;
  338. void visit (const tdesc_reg *reg) override;
  339. private:
  340. /* Called with a positive value of ADJUST when we move inside an element,
  341. for example inside <target>, and with a negative value when we leave
  342. the element. In this class this function does nothing, but a
  343. sub-class can override this to track the current level of nesting. */
  344. void indent (int adjust)
  345. {
  346. m_depth += (adjust * 2);
  347. }
  348. /* Functions to add lines to the output buffer M_BUFFER. Each of these
  349. functions appends a newline, so don't include one in the strings being
  350. passed. */
  351. void add_line (const std::string &str);
  352. void add_line (const char *fmt, ...) ATTRIBUTE_PRINTF (2, 3);
  353. /* The buffer we are writing too. */
  354. std::string *m_buffer;
  355. /* The current indentation depth. */
  356. int m_depth;
  357. };
  358. #endif /* COMMON_TDESC_H */