tdesc.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* Target description support for GDB.
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "gdbsupport/tdesc.h"
  16. tdesc_reg::tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
  17. int regnum, int save_restore_, const char *group_,
  18. int bitsize_, const char *type_)
  19. : name (name_), target_regnum (regnum),
  20. save_restore (save_restore_),
  21. group (group_ != NULL ? group_ : ""),
  22. bitsize (bitsize_),
  23. type (type_ != NULL ? type_ : "<unknown>")
  24. {
  25. /* If the register's type is target-defined, look it up now. We may not
  26. have easy access to the containing feature when we want it later. */
  27. tdesc_type = tdesc_named_type (feature, type.c_str ());
  28. }
  29. /* Predefined types. */
  30. static tdesc_type_builtin tdesc_predefined_types[] =
  31. {
  32. { "bool", TDESC_TYPE_BOOL },
  33. { "int8", TDESC_TYPE_INT8 },
  34. { "int16", TDESC_TYPE_INT16 },
  35. { "int32", TDESC_TYPE_INT32 },
  36. { "int64", TDESC_TYPE_INT64 },
  37. { "int128", TDESC_TYPE_INT128 },
  38. { "uint8", TDESC_TYPE_UINT8 },
  39. { "uint16", TDESC_TYPE_UINT16 },
  40. { "uint32", TDESC_TYPE_UINT32 },
  41. { "uint64", TDESC_TYPE_UINT64 },
  42. { "uint128", TDESC_TYPE_UINT128 },
  43. { "code_ptr", TDESC_TYPE_CODE_PTR },
  44. { "data_ptr", TDESC_TYPE_DATA_PTR },
  45. { "ieee_half", TDESC_TYPE_IEEE_HALF },
  46. { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
  47. { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
  48. { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
  49. { "i387_ext", TDESC_TYPE_I387_EXT },
  50. { "bfloat16", TDESC_TYPE_BFLOAT16 }
  51. };
  52. void tdesc_feature::accept (tdesc_element_visitor &v) const
  53. {
  54. v.visit_pre (this);
  55. for (const tdesc_type_up &type : types)
  56. type->accept (v);
  57. for (const tdesc_reg_up &reg : registers)
  58. reg->accept (v);
  59. v.visit_post (this);
  60. }
  61. bool tdesc_feature::operator== (const tdesc_feature &other) const
  62. {
  63. if (name != other.name)
  64. return false;
  65. if (registers.size () != other.registers.size ())
  66. return false;
  67. for (int ix = 0; ix < registers.size (); ix++)
  68. {
  69. const tdesc_reg_up &reg1 = registers[ix];
  70. const tdesc_reg_up &reg2 = other.registers[ix];
  71. if (reg1 != reg2 && *reg1 != *reg2)
  72. return false;
  73. }
  74. if (types.size () != other.types.size ())
  75. return false;
  76. for (int ix = 0; ix < types.size (); ix++)
  77. {
  78. const tdesc_type_up &type1 = types[ix];
  79. const tdesc_type_up &type2 = other.types[ix];
  80. if (type1 != type2 && *type1 != *type2)
  81. return false;
  82. }
  83. return true;
  84. }
  85. /* Lookup a predefined type. */
  86. static struct tdesc_type *
  87. tdesc_predefined_type (enum tdesc_type_kind kind)
  88. {
  89. for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
  90. if (tdesc_predefined_types[ix].kind == kind)
  91. return &tdesc_predefined_types[ix];
  92. gdb_assert_not_reached ("bad predefined tdesc type");
  93. }
  94. /* See gdbsupport/tdesc.h. */
  95. struct tdesc_type *
  96. tdesc_named_type (const struct tdesc_feature *feature, const char *id)
  97. {
  98. /* First try target-defined types. */
  99. for (const tdesc_type_up &type : feature->types)
  100. if (type->name == id)
  101. return type.get ();
  102. /* Next try the predefined types. */
  103. for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
  104. if (tdesc_predefined_types[ix].name == id)
  105. return &tdesc_predefined_types[ix];
  106. return NULL;
  107. }
  108. /* See gdbsupport/tdesc.h. */
  109. void
  110. tdesc_create_reg (struct tdesc_feature *feature, const char *name,
  111. int regnum, int save_restore, const char *group,
  112. int bitsize, const char *type)
  113. {
  114. tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
  115. group, bitsize, type);
  116. feature->registers.emplace_back (reg);
  117. }
  118. /* See gdbsupport/tdesc.h. */
  119. struct tdesc_type *
  120. tdesc_create_vector (struct tdesc_feature *feature, const char *name,
  121. struct tdesc_type *field_type, int count)
  122. {
  123. tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
  124. feature->types.emplace_back (type);
  125. return type;
  126. }
  127. /* See gdbsupport/tdesc.h. */
  128. tdesc_type_with_fields *
  129. tdesc_create_struct (struct tdesc_feature *feature, const char *name)
  130. {
  131. tdesc_type_with_fields *type
  132. = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
  133. feature->types.emplace_back (type);
  134. return type;
  135. }
  136. /* See gdbsupport/tdesc.h. */
  137. void
  138. tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
  139. {
  140. gdb_assert (type->kind == TDESC_TYPE_STRUCT);
  141. gdb_assert (size > 0);
  142. type->size = size;
  143. }
  144. /* See gdbsupport/tdesc.h. */
  145. tdesc_type_with_fields *
  146. tdesc_create_union (struct tdesc_feature *feature, const char *name)
  147. {
  148. tdesc_type_with_fields *type
  149. = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
  150. feature->types.emplace_back (type);
  151. return type;
  152. }
  153. /* See gdbsupport/tdesc.h. */
  154. tdesc_type_with_fields *
  155. tdesc_create_flags (struct tdesc_feature *feature, const char *name,
  156. int size)
  157. {
  158. gdb_assert (size > 0);
  159. tdesc_type_with_fields *type
  160. = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
  161. feature->types.emplace_back (type);
  162. return type;
  163. }
  164. /* See gdbsupport/tdesc.h. */
  165. tdesc_type_with_fields *
  166. tdesc_create_enum (struct tdesc_feature *feature, const char *name,
  167. int size)
  168. {
  169. gdb_assert (size > 0);
  170. tdesc_type_with_fields *type
  171. = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
  172. feature->types.emplace_back (type);
  173. return type;
  174. }
  175. /* See gdbsupport/tdesc.h. */
  176. void
  177. tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
  178. struct tdesc_type *field_type)
  179. {
  180. gdb_assert (type->kind == TDESC_TYPE_UNION
  181. || type->kind == TDESC_TYPE_STRUCT);
  182. /* Initialize start and end so we know this is not a bit-field
  183. when we print-c-tdesc. */
  184. type->fields.emplace_back (field_name, field_type, -1, -1);
  185. }
  186. /* See gdbsupport/tdesc.h. */
  187. void
  188. tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
  189. int start, int end, struct tdesc_type *field_type)
  190. {
  191. gdb_assert (type->kind == TDESC_TYPE_STRUCT
  192. || type->kind == TDESC_TYPE_FLAGS);
  193. gdb_assert (start >= 0 && end >= start);
  194. type->fields.emplace_back (field_name, field_type, start, end);
  195. }
  196. /* See gdbsupport/tdesc.h. */
  197. void
  198. tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
  199. int start, int end)
  200. {
  201. struct tdesc_type *field_type;
  202. gdb_assert (start >= 0 && end >= start);
  203. if (type->size > 4)
  204. field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
  205. else
  206. field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
  207. tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
  208. }
  209. /* See gdbsupport/tdesc.h. */
  210. void
  211. tdesc_add_flag (tdesc_type_with_fields *type, int start,
  212. const char *flag_name)
  213. {
  214. gdb_assert (type->kind == TDESC_TYPE_FLAGS
  215. || type->kind == TDESC_TYPE_STRUCT);
  216. type->fields.emplace_back (flag_name,
  217. tdesc_predefined_type (TDESC_TYPE_BOOL),
  218. start, start);
  219. }
  220. /* See gdbsupport/tdesc.h. */
  221. void
  222. tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
  223. const char *name)
  224. {
  225. gdb_assert (type->kind == TDESC_TYPE_ENUM);
  226. type->fields.emplace_back (name,
  227. tdesc_predefined_type (TDESC_TYPE_INT32),
  228. value, -1);
  229. }
  230. void print_xml_feature::visit_pre (const tdesc_feature *e)
  231. {
  232. add_line ("<feature name=\"%s\">", e->name.c_str ());
  233. indent (1);
  234. }
  235. void print_xml_feature::visit_post (const tdesc_feature *e)
  236. {
  237. indent (-1);
  238. add_line ("</feature>");
  239. }
  240. void print_xml_feature::visit (const tdesc_type_builtin *t)
  241. {
  242. error (_("xml output is not supported for type \"%s\"."), t->name.c_str ());
  243. }
  244. void print_xml_feature::visit (const tdesc_type_vector *t)
  245. {
  246. add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
  247. t->name.c_str (), t->element_type->name.c_str (), t->count);
  248. }
  249. void print_xml_feature::visit (const tdesc_type_with_fields *t)
  250. {
  251. const static char *types[] = { "struct", "union", "flags", "enum" };
  252. gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM);
  253. std::string tmp;
  254. string_appendf (tmp,
  255. "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT],
  256. t->name.c_str ());
  257. switch (t->kind)
  258. {
  259. case TDESC_TYPE_STRUCT:
  260. case TDESC_TYPE_FLAGS:
  261. if (t->size > 0)
  262. string_appendf (tmp, " size=\"%d\"", t->size);
  263. string_appendf (tmp, ">");
  264. add_line (tmp);
  265. for (const tdesc_type_field &f : t->fields)
  266. {
  267. tmp.clear ();
  268. string_appendf (tmp, " <field name=\"%s\"", f.name.c_str ());
  269. if (f.start != -1)
  270. string_appendf (tmp, " start=\"%d\" end=\"%d\"", f.start,
  271. f.end);
  272. string_appendf (tmp, " type=\"%s\"/>",
  273. f.type->name.c_str ());
  274. add_line (tmp);
  275. }
  276. break;
  277. case TDESC_TYPE_ENUM:
  278. if (t->size > 0)
  279. string_appendf (tmp, " size=\"%d\"", t->size);
  280. string_appendf (tmp, ">");
  281. add_line (tmp);
  282. /* The 'start' of the field is reused as the enum value. The 'end'
  283. of the field is always set to -1 for enum values. */
  284. for (const tdesc_type_field &f : t->fields)
  285. add_line (" <evalue name=\"%s\" value=\"%d\"/>",
  286. f.name.c_str (), f.start);
  287. break;
  288. case TDESC_TYPE_UNION:
  289. string_appendf (tmp, ">");
  290. add_line (tmp);
  291. for (const tdesc_type_field &f : t->fields)
  292. add_line (" <field name=\"%s\" type=\"%s\"/>",
  293. f.name.c_str (), f.type->name.c_str ());
  294. break;
  295. default:
  296. error (_("xml output is not supported for type \"%s\"."),
  297. t->name.c_str ());
  298. }
  299. add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]);
  300. }
  301. void print_xml_feature::visit (const tdesc_reg *r)
  302. {
  303. std::string tmp;
  304. string_appendf (tmp,
  305. "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
  306. r->name.c_str (), r->bitsize, r->type.c_str (),
  307. r->target_regnum);
  308. if (r->group.length () > 0)
  309. string_appendf (tmp, " group=\"%s\"", r->group.c_str ());
  310. if (r->save_restore == 0)
  311. string_appendf (tmp, " save-restore=\"no\"");
  312. string_appendf (tmp, "/>");
  313. add_line (tmp);
  314. }
  315. void print_xml_feature::visit_pre (const target_desc *e)
  316. {
  317. #ifndef IN_PROCESS_AGENT
  318. add_line ("<?xml version=\"1.0\"?>");
  319. add_line ("<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
  320. add_line ("<target>");
  321. indent (1);
  322. if (tdesc_architecture_name (e))
  323. add_line ("<architecture>%s</architecture>",
  324. tdesc_architecture_name (e));
  325. const char *osabi = tdesc_osabi_name (e);
  326. if (osabi != nullptr)
  327. add_line ("<osabi>%s</osabi>", osabi);
  328. const std::vector<tdesc_compatible_info_up> &compatible_list
  329. = tdesc_compatible_info_list (e);
  330. for (const auto &c : compatible_list)
  331. add_line ("<compatible>%s</compatible>",
  332. tdesc_compatible_info_arch_name (c));
  333. #endif
  334. }
  335. void print_xml_feature::visit_post (const target_desc *e)
  336. {
  337. indent (-1);
  338. add_line ("</target>");
  339. }
  340. /* See gdbsupport/tdesc.h. */
  341. void
  342. print_xml_feature::add_line (const std::string &str)
  343. {
  344. string_appendf (*m_buffer, "%*s", m_depth, "");
  345. string_appendf (*m_buffer, "%s", str.c_str ());
  346. string_appendf (*m_buffer, "\n");
  347. }
  348. /* See gdbsupport/tdesc.h. */
  349. void
  350. print_xml_feature::add_line (const char *fmt, ...)
  351. {
  352. std::string tmp;
  353. va_list ap;
  354. va_start (ap, fmt);
  355. string_vappendf (tmp, fmt, ap);
  356. va_end (ap);
  357. add_line (tmp);
  358. }