1 /* Target description support for GDB.
3 Copyright (C) 2018-2024 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 "gdbsupport/tdesc.h"
22 tdesc_reg::tdesc_reg (struct tdesc_feature
*feature
, const std::string
&name_
,
23 int regnum
, int save_restore_
, const char *group_
,
24 int bitsize_
, const char *type_
)
25 : name (name_
), target_regnum (regnum
),
26 save_restore (save_restore_
),
27 group (group_
!= NULL
? group_
: ""),
29 type (type_
!= NULL
? type_
: "<unknown>")
31 /* If the register's type is target-defined, look it up now. We may not
32 have easy access to the containing feature when we want it later. */
33 tdesc_type
= tdesc_named_type (feature
, type
.c_str ());
36 /* Predefined types. */
37 static tdesc_type_builtin tdesc_predefined_types
[] =
39 { "bool", TDESC_TYPE_BOOL
},
40 { "int8", TDESC_TYPE_INT8
},
41 { "int16", TDESC_TYPE_INT16
},
42 { "int32", TDESC_TYPE_INT32
},
43 { "int64", TDESC_TYPE_INT64
},
44 { "int128", TDESC_TYPE_INT128
},
45 { "uint8", TDESC_TYPE_UINT8
},
46 { "uint16", TDESC_TYPE_UINT16
},
47 { "uint32", TDESC_TYPE_UINT32
},
48 { "uint64", TDESC_TYPE_UINT64
},
49 { "uint128", TDESC_TYPE_UINT128
},
50 { "code_ptr", TDESC_TYPE_CODE_PTR
},
51 { "data_ptr", TDESC_TYPE_DATA_PTR
},
52 { "ieee_half", TDESC_TYPE_IEEE_HALF
},
53 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
54 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
55 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
},
56 { "i387_ext", TDESC_TYPE_I387_EXT
},
57 { "bfloat16", TDESC_TYPE_BFLOAT16
}
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 add_line ("<feature name=\"%s\">", e
->name
.c_str ());
301 void print_xml_feature::visit_post (const tdesc_feature
*e
)
304 add_line ("</feature>");
307 void print_xml_feature::visit (const tdesc_type_builtin
*t
)
309 error (_("xml output is not supported for type \"%s\"."), t
->name
.c_str ());
312 void print_xml_feature::visit (const tdesc_type_vector
*t
)
314 add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
315 t
->name
.c_str (), t
->element_type
->name
.c_str (), t
->count
);
318 void print_xml_feature::visit (const tdesc_type_with_fields
*t
)
320 const static char *types
[] = { "struct", "union", "flags", "enum" };
322 gdb_assert (t
->kind
>= TDESC_TYPE_STRUCT
&& t
->kind
<= TDESC_TYPE_ENUM
);
327 "<%s id=\"%s\"", types
[t
->kind
- TDESC_TYPE_STRUCT
],
332 case TDESC_TYPE_STRUCT
:
333 case TDESC_TYPE_FLAGS
:
335 string_appendf (tmp
, " size=\"%d\"", t
->size
);
336 string_appendf (tmp
, ">");
339 for (const tdesc_type_field
&f
: t
->fields
)
342 string_appendf (tmp
, " <field name=\"%s\"", f
.name
.c_str ());
344 string_appendf (tmp
, " start=\"%d\" end=\"%d\"", f
.start
,
346 string_appendf (tmp
, " type=\"%s\"/>",
347 f
.type
->name
.c_str ());
352 case TDESC_TYPE_ENUM
:
354 string_appendf (tmp
, " size=\"%d\"", t
->size
);
355 string_appendf (tmp
, ">");
357 /* The 'start' of the field is reused as the enum value. The 'end'
358 of the field is always set to -1 for enum values. */
359 for (const tdesc_type_field
&f
: t
->fields
)
360 add_line (" <evalue name=\"%s\" value=\"%d\"/>",
361 f
.name
.c_str (), f
.start
);
364 case TDESC_TYPE_UNION
:
365 string_appendf (tmp
, ">");
367 for (const tdesc_type_field
&f
: t
->fields
)
368 add_line (" <field name=\"%s\" type=\"%s\"/>",
369 f
.name
.c_str (), f
.type
->name
.c_str ());
373 error (_("xml output is not supported for type \"%s\"."),
377 add_line ("</%s>", types
[t
->kind
- TDESC_TYPE_STRUCT
]);
380 void print_xml_feature::visit (const tdesc_reg
*r
)
385 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
386 r
->name
.c_str (), r
->bitsize
, r
->type
.c_str (),
389 if (r
->group
.length () > 0)
390 string_appendf (tmp
, " group=\"%s\"", r
->group
.c_str ());
392 if (r
->save_restore
== 0)
393 string_appendf (tmp
, " save-restore=\"no\"");
395 string_appendf (tmp
, "/>");
400 void print_xml_feature::visit_pre (const target_desc
*e
)
402 #ifndef IN_PROCESS_AGENT
403 add_line ("<?xml version=\"1.0\"?>");
404 add_line ("<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
405 add_line ("<target>");
407 if (tdesc_architecture_name (e
))
408 add_line ("<architecture>%s</architecture>",
409 tdesc_architecture_name (e
));
411 const char *osabi
= tdesc_osabi_name (e
);
412 if (osabi
!= nullptr)
413 add_line ("<osabi>%s</osabi>", osabi
);
415 const std::vector
<tdesc_compatible_info_up
> &compatible_list
416 = tdesc_compatible_info_list (e
);
417 for (const auto &c
: compatible_list
)
418 add_line ("<compatible>%s</compatible>",
419 tdesc_compatible_info_arch_name (c
));
423 void print_xml_feature::visit_post (const target_desc
*e
)
426 add_line ("</target>");
429 /* See gdbsupport/tdesc.h. */
432 print_xml_feature::add_line (const std::string
&str
)
434 string_appendf (*m_buffer
, "%*s", m_depth
, "");
435 string_appendf (*m_buffer
, "%s", str
.c_str ());
436 string_appendf (*m_buffer
, "\n");
439 /* See gdbsupport/tdesc.h. */
442 print_xml_feature::add_line (const char *fmt
, ...)
448 string_vappendf (tmp
, fmt
, ap
);