Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdbsupport / tdesc.cc
blob080d39c485dc5d9d7ced21e067bec5391b132c94
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_ : ""),
28 bitsize (bitsize_),
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
62 v.visit_pre (this);
64 for (const tdesc_type_up &type : types)
65 type->accept (v);
67 for (const tdesc_reg_up &reg : registers)
68 reg->accept (v);
70 v.visit_post (this);
73 bool tdesc_feature::operator== (const tdesc_feature &other) const
75 if (name != other.name)
76 return false;
78 if (registers.size () != other.registers.size ())
79 return false;
81 for (int ix = 0; ix < registers.size (); ix++)
83 const tdesc_reg_up &reg1 = registers[ix];
84 const tdesc_reg_up &reg2 = other.registers[ix];
86 if (reg1 != reg2 && *reg1 != *reg2)
87 return false;
90 if (types.size () != other.types.size ())
91 return false;
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)
99 return false;
102 return true;
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. */
119 struct tdesc_type *
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)
125 return type.get ();
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];
132 return NULL;
135 /* See gdbsupport/tdesc.h. */
137 void
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. */
150 struct tdesc_type *
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);
157 return 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);
169 return type;
172 /* See gdbsupport/tdesc.h. */
174 void
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);
179 type->size = size;
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);
191 return type;
194 /* See gdbsupport/tdesc.h. */
196 tdesc_type_with_fields *
197 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
198 int size)
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);
206 return type;
209 /* See gdbsupport/tdesc.h. */
211 tdesc_type_with_fields *
212 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
213 int size)
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);
221 return type;
224 /* See gdbsupport/tdesc.h. */
226 void
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. */
240 void
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. */
253 void
254 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
255 int start, int end)
257 struct tdesc_type *field_type;
259 gdb_assert (start >= 0 && end >= start);
261 if (type->size > 4)
262 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
263 else
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. */
271 void
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),
280 start, start);
283 /* See gdbsupport/tdesc.h. */
285 void
286 tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
287 const char *name)
289 gdb_assert (type->kind == TDESC_TYPE_ENUM);
290 type->fields.emplace_back (name,
291 tdesc_predefined_type (TDESC_TYPE_INT32),
292 value, -1);
295 void print_xml_feature::visit_pre (const tdesc_feature *e)
297 add_line ("<feature name=\"%s\">", e->name.c_str ());
298 indent (1);
301 void print_xml_feature::visit_post (const tdesc_feature *e)
303 indent (-1);
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);
324 std::string tmp;
326 string_appendf (tmp,
327 "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT],
328 t->name.c_str ());
330 switch (t->kind)
332 case TDESC_TYPE_STRUCT:
333 case TDESC_TYPE_FLAGS:
334 if (t->size > 0)
335 string_appendf (tmp, " size=\"%d\"", t->size);
336 string_appendf (tmp, ">");
337 add_line (tmp);
339 for (const tdesc_type_field &f : t->fields)
341 tmp.clear ();
342 string_appendf (tmp, " <field name=\"%s\"", f.name.c_str ());
343 if (f.start != -1)
344 string_appendf (tmp, " start=\"%d\" end=\"%d\"", f.start,
345 f.end);
346 string_appendf (tmp, " type=\"%s\"/>",
347 f.type->name.c_str ());
348 add_line (tmp);
350 break;
352 case TDESC_TYPE_ENUM:
353 if (t->size > 0)
354 string_appendf (tmp, " size=\"%d\"", t->size);
355 string_appendf (tmp, ">");
356 add_line (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);
362 break;
364 case TDESC_TYPE_UNION:
365 string_appendf (tmp, ">");
366 add_line (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 ());
370 break;
372 default:
373 error (_("xml output is not supported for type \"%s\"."),
374 t->name.c_str ());
377 add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]);
380 void print_xml_feature::visit (const tdesc_reg *r)
382 std::string tmp;
384 string_appendf (tmp,
385 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
386 r->name.c_str (), r->bitsize, r->type.c_str (),
387 r->target_regnum);
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, "/>");
397 add_line (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>");
406 indent (1);
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));
420 #endif
423 void print_xml_feature::visit_post (const target_desc *e)
425 indent (-1);
426 add_line ("</target>");
429 /* See gdbsupport/tdesc.h. */
431 void
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. */
441 void
442 print_xml_feature::add_line (const char *fmt, ...)
444 std::string tmp;
446 va_list ap;
447 va_start (ap, fmt);
448 string_vappendf (tmp, fmt, ap);
449 va_end (ap);
450 add_line (tmp);