1 /* Target description support for GDB.
3 Copyright (C) 2018-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
21 #include "gdbsupport/tdesc.h"
23 tdesc_reg::tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
24 int regnum, int save_restore_, const char *group_,
25 int bitsize_, const char *type_)
26 : name (name_), target_regnum (regnum),
27 save_restore (save_restore_),
28 group (group_ != NULL ? group_ : ""),
30 type (type_ != NULL ? type_ : "<unknown>")
32 /* If the register's type is target-defined, look it up now. We may not
33 have easy access to the containing feature when we want it later. */
34 tdesc_type = tdesc_named_type (feature, type.c_str ());
37 /* Predefined types. */
38 static tdesc_type_builtin tdesc_predefined_types[] =
40 { "bool", TDESC_TYPE_BOOL },
41 { "int8", TDESC_TYPE_INT8 },
42 { "int16", TDESC_TYPE_INT16 },
43 { "int32", TDESC_TYPE_INT32 },
44 { "int64", TDESC_TYPE_INT64 },
45 { "int128", TDESC_TYPE_INT128 },
46 { "uint8", TDESC_TYPE_UINT8 },
47 { "uint16", TDESC_TYPE_UINT16 },
48 { "uint32", TDESC_TYPE_UINT32 },
49 { "uint64", TDESC_TYPE_UINT64 },
50 { "uint128", TDESC_TYPE_UINT128 },
51 { "code_ptr", TDESC_TYPE_CODE_PTR },
52 { "data_ptr", TDESC_TYPE_DATA_PTR },
53 { "ieee_half", TDESC_TYPE_IEEE_HALF },
54 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
55 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
56 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
57 { "i387_ext", TDESC_TYPE_I387_EXT }
60 void tdesc_feature::accept (tdesc_element_visitor &v) const
64 for (const tdesc_type_up &type : types)
67 for (const tdesc_reg_up ® : registers)
73 bool tdesc_feature::operator== (const tdesc_feature &other) const
75 if (name != other.name)
78 if (registers.size () != other.registers.size ())
81 for (int ix = 0; ix < registers.size (); ix++)
83 const tdesc_reg_up ®1 = registers[ix];
84 const tdesc_reg_up ®2 = other.registers[ix];
86 if (reg1 != reg2 && *reg1 != *reg2)
90 if (types.size () != other.types.size ())
93 for (int ix = 0; ix < types.size (); ix++)
95 const tdesc_type_up &type1 = types[ix];
96 const tdesc_type_up &type2 = other.types[ix];
98 if (type1 != type2 && *type1 != *type2)
105 /* Lookup a predefined type. */
107 static struct tdesc_type *
108 tdesc_predefined_type (enum tdesc_type_kind kind)
110 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
111 if (tdesc_predefined_types[ix].kind == kind)
112 return &tdesc_predefined_types[ix];
114 gdb_assert_not_reached ("bad predefined tdesc type");
117 /* See gdbsupport/tdesc.h. */
120 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
122 /* First try target-defined types. */
123 for (const tdesc_type_up &type : feature->types)
124 if (type->name == id)
127 /* Next try the predefined types. */
128 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
129 if (tdesc_predefined_types[ix].name == id)
130 return &tdesc_predefined_types[ix];
135 /* See gdbsupport/tdesc.h. */
138 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
139 int regnum, int save_restore, const char *group,
140 int bitsize, const char *type)
142 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
143 group, bitsize, type);
145 feature->registers.emplace_back (reg);
148 /* See gdbsupport/tdesc.h. */
151 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
152 struct tdesc_type *field_type, int count)
154 tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
155 feature->types.emplace_back (type);
160 /* See gdbsupport/tdesc.h. */
162 tdesc_type_with_fields *
163 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
165 tdesc_type_with_fields *type
166 = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
167 feature->types.emplace_back (type);
172 /* See gdbsupport/tdesc.h. */
175 tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
177 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
178 gdb_assert (size > 0);
182 /* See gdbsupport/tdesc.h. */
184 tdesc_type_with_fields *
185 tdesc_create_union (struct tdesc_feature *feature, const char *name)
187 tdesc_type_with_fields *type
188 = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
189 feature->types.emplace_back (type);
194 /* See gdbsupport/tdesc.h. */
196 tdesc_type_with_fields *
197 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
200 gdb_assert (size > 0);
202 tdesc_type_with_fields *type
203 = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
204 feature->types.emplace_back (type);
209 /* See gdbsupport/tdesc.h. */
211 tdesc_type_with_fields *
212 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
215 gdb_assert (size > 0);
217 tdesc_type_with_fields *type
218 = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
219 feature->types.emplace_back (type);
224 /* See gdbsupport/tdesc.h. */
227 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
228 struct tdesc_type *field_type)
230 gdb_assert (type->kind == TDESC_TYPE_UNION
231 || type->kind == TDESC_TYPE_STRUCT);
233 /* Initialize start and end so we know this is not a bit-field
234 when we print-c-tdesc. */
235 type->fields.emplace_back (field_name, field_type, -1, -1);
238 /* See gdbsupport/tdesc.h. */
241 tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
242 int start, int end, struct tdesc_type *field_type)
244 gdb_assert (type->kind == TDESC_TYPE_STRUCT
245 || type->kind == TDESC_TYPE_FLAGS);
246 gdb_assert (start >= 0 && end >= start);
248 type->fields.emplace_back (field_name, field_type, start, end);
251 /* See gdbsupport/tdesc.h. */
254 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
257 struct tdesc_type *field_type;
259 gdb_assert (start >= 0 && end >= start);
262 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
264 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
266 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
269 /* See gdbsupport/tdesc.h. */
272 tdesc_add_flag (tdesc_type_with_fields *type, int start,
273 const char *flag_name)
275 gdb_assert (type->kind == TDESC_TYPE_FLAGS
276 || type->kind == TDESC_TYPE_STRUCT);
278 type->fields.emplace_back (flag_name,
279 tdesc_predefined_type (TDESC_TYPE_BOOL),
283 /* See gdbsupport/tdesc.h. */
286 tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
289 gdb_assert (type->kind == TDESC_TYPE_ENUM);
290 type->fields.emplace_back (name,
291 tdesc_predefined_type (TDESC_TYPE_INT32),
295 void print_xml_feature::visit_pre (const tdesc_feature *e)
297 string_appendf (*m_buffer, "<feature name=\"%s\">\n", e->name.c_str ());
300 void print_xml_feature::visit_post (const tdesc_feature *e)
302 string_appendf (*m_buffer, "</feature>\n");
305 void print_xml_feature::visit (const tdesc_type_builtin *t)
307 error (_("xml output is not supported for type \"%s\"."), t->name.c_str ());
310 void print_xml_feature::visit (const tdesc_type_vector *t)
312 string_appendf (*m_buffer, "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
313 t->name.c_str (), t->element_type->name.c_str (), t->count);
316 void print_xml_feature::visit (const tdesc_type_with_fields *t)
318 const static char *types[] = { "struct", "union", "flags", "enum" };
320 gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM);
322 string_appendf (*m_buffer,
323 "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT],
328 case TDESC_TYPE_STRUCT:
329 case TDESC_TYPE_FLAGS:
331 string_appendf (*m_buffer, " size=\"%d\"", t->size);
332 string_appendf (*m_buffer, ">\n");
334 for (const tdesc_type_field &f : t->fields)
336 string_appendf (*m_buffer, " <field name=\"%s\" ", f.name.c_str ());
338 string_appendf (*m_buffer, "type=\"%s\"/>\n",
339 f.type->name.c_str ());
341 string_appendf (*m_buffer, "start=\"%d\" end=\"%d\"/>\n", f.start,
346 case TDESC_TYPE_ENUM:
347 string_appendf (*m_buffer, ">\n");
348 for (const tdesc_type_field &f : t->fields)
349 string_appendf (*m_buffer, " <field name=\"%s\" start=\"%d\"/>\n",
350 f.name.c_str (), f.start);
353 case TDESC_TYPE_UNION:
354 string_appendf (*m_buffer, ">\n");
355 for (const tdesc_type_field &f : t->fields)
356 string_appendf (*m_buffer, " <field name=\"%s\" type=\"%s\"/>\n",
357 f.name.c_str (), f.type->name.c_str ());
361 error (_("xml output is not supported for type \"%s\"."),
365 string_appendf (*m_buffer, "</%s>\n", types[t->kind - TDESC_TYPE_STRUCT]);
368 void print_xml_feature::visit (const tdesc_reg *r)
370 string_appendf (*m_buffer,
371 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
372 r->name.c_str (), r->bitsize, r->type.c_str (),
375 if (r->group.length () > 0)
376 string_appendf (*m_buffer, " group=\"%s\"", r->group.c_str ());
378 if (r->save_restore == 0)
379 string_appendf (*m_buffer, " save-restore=\"no\"");
381 string_appendf (*m_buffer, "/>\n");
384 void print_xml_feature::visit_pre (const target_desc *e)
386 #ifndef IN_PROCESS_AGENT
387 string_appendf (*m_buffer, "<?xml version=\"1.0\"?>\n");
388 string_appendf (*m_buffer, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n");
389 string_appendf (*m_buffer, "<target>\n<architecture>%s</architecture>\n",
390 tdesc_architecture_name (e));
392 const char *osabi = tdesc_osabi_name (e);
393 if (osabi != nullptr)
394 string_appendf (*m_buffer, "<osabi>%s</osabi>", osabi);
398 void print_xml_feature::visit_post (const target_desc *e)
400 string_appendf (*m_buffer, "</target>\n");