]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/tdesc.cc
Add padding with an expression rather than a hard-address
[thirdparty/binutils-gdb.git] / gdbsupport / tdesc.cc
CommitLineData
ea3e7d71
AH
1/* Target description support for GDB.
2
b811d2c2 3 Copyright (C) 2018-2020 Free Software Foundation, Inc.
ea3e7d71
AH
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "common-defs.h"
268a13a5 21#include "gdbsupport/tdesc.h"
ea3e7d71
AH
22
23tdesc_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_ : ""),
29 bitsize (bitsize_),
30 type (type_ != NULL ? type_ : "<unknown>")
31{
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 ());
35}
82ec9bc7 36
eee8a18d
AH
37/* Predefined types. */
38static tdesc_type_builtin tdesc_predefined_types[] =
39{
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 },
295ea638
LM
53 { "code_capability", TDESC_TYPE_CODE_CAPABILITY },
54 { "data_capability", TDESC_TYPE_DATA_CAPABILITY },
6664edc5
LM
55 { "intcap", TDESC_TYPE_INTCAP },
56 { "uintcap", TDESC_TYPE_UINTCAP },
a6d0f249 57 { "ieee_half", TDESC_TYPE_IEEE_HALF },
eee8a18d
AH
58 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
59 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
60 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
2a67f09d
FW
61 { "i387_ext", TDESC_TYPE_I387_EXT },
62 { "bfloat16", TDESC_TYPE_BFLOAT16 }
eee8a18d
AH
63};
64
82ec9bc7
AH
65void tdesc_feature::accept (tdesc_element_visitor &v) const
66{
67 v.visit_pre (this);
68
69 for (const tdesc_type_up &type : types)
70 type->accept (v);
71
72 for (const tdesc_reg_up &reg : registers)
73 reg->accept (v);
74
75 v.visit_post (this);
76}
77
78bool tdesc_feature::operator== (const tdesc_feature &other) const
79{
80 if (name != other.name)
81 return false;
82
83 if (registers.size () != other.registers.size ())
84 return false;
85
86 for (int ix = 0; ix < registers.size (); ix++)
87 {
88 const tdesc_reg_up &reg1 = registers[ix];
89 const tdesc_reg_up &reg2 = other.registers[ix];
90
91 if (reg1 != reg2 && *reg1 != *reg2)
92 return false;
93 }
94
95 if (types.size () != other.types.size ())
96 return false;
97
98 for (int ix = 0; ix < types.size (); ix++)
99 {
100 const tdesc_type_up &type1 = types[ix];
101 const tdesc_type_up &type2 = other.types[ix];
102
103 if (type1 != type2 && *type1 != *type2)
104 return false;
105 }
106
107 return true;
108}
109
eee8a18d
AH
110/* Lookup a predefined type. */
111
112static struct tdesc_type *
113tdesc_predefined_type (enum tdesc_type_kind kind)
114{
115 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
116 if (tdesc_predefined_types[ix].kind == kind)
117 return &tdesc_predefined_types[ix];
118
119 gdb_assert_not_reached ("bad predefined tdesc type");
120}
121
268a13a5 122/* See gdbsupport/tdesc.h. */
eee8a18d
AH
123
124struct tdesc_type *
125tdesc_named_type (const struct tdesc_feature *feature, const char *id)
126{
127 /* First try target-defined types. */
128 for (const tdesc_type_up &type : feature->types)
129 if (type->name == id)
130 return type.get ();
131
132 /* Next try the predefined types. */
133 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
134 if (tdesc_predefined_types[ix].name == id)
135 return &tdesc_predefined_types[ix];
136
137 return NULL;
138}
139
268a13a5 140/* See gdbsupport/tdesc.h. */
82ec9bc7
AH
141
142void
143tdesc_create_reg (struct tdesc_feature *feature, const char *name,
144 int regnum, int save_restore, const char *group,
145 int bitsize, const char *type)
146{
147 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
148 group, bitsize, type);
149
150 feature->registers.emplace_back (reg);
151}
eee8a18d 152
268a13a5 153/* See gdbsupport/tdesc.h. */
eee8a18d
AH
154
155struct tdesc_type *
156tdesc_create_vector (struct tdesc_feature *feature, const char *name,
157 struct tdesc_type *field_type, int count)
158{
159 tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
160 feature->types.emplace_back (type);
161
162 return type;
163}
164
268a13a5 165/* See gdbsupport/tdesc.h. */
eee8a18d
AH
166
167tdesc_type_with_fields *
168tdesc_create_struct (struct tdesc_feature *feature, const char *name)
169{
170 tdesc_type_with_fields *type
171 = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
172 feature->types.emplace_back (type);
173
174 return type;
175}
176
268a13a5 177/* See gdbsupport/tdesc.h. */
eee8a18d
AH
178
179void
180tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
181{
182 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
183 gdb_assert (size > 0);
184 type->size = size;
185}
186
268a13a5 187/* See gdbsupport/tdesc.h. */
eee8a18d
AH
188
189tdesc_type_with_fields *
190tdesc_create_union (struct tdesc_feature *feature, const char *name)
191{
192 tdesc_type_with_fields *type
193 = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
194 feature->types.emplace_back (type);
195
196 return type;
197}
198
268a13a5 199/* See gdbsupport/tdesc.h. */
eee8a18d
AH
200
201tdesc_type_with_fields *
202tdesc_create_flags (struct tdesc_feature *feature, const char *name,
203 int size)
204{
205 gdb_assert (size > 0);
206
207 tdesc_type_with_fields *type
208 = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
209 feature->types.emplace_back (type);
210
211 return type;
212}
213
268a13a5 214/* See gdbsupport/tdesc.h. */
eee8a18d
AH
215
216tdesc_type_with_fields *
217tdesc_create_enum (struct tdesc_feature *feature, const char *name,
218 int size)
219{
220 gdb_assert (size > 0);
221
222 tdesc_type_with_fields *type
223 = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
224 feature->types.emplace_back (type);
225
226 return type;
227}
228
268a13a5 229/* See gdbsupport/tdesc.h. */
eee8a18d
AH
230
231void
232tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
233 struct tdesc_type *field_type)
234{
235 gdb_assert (type->kind == TDESC_TYPE_UNION
236 || type->kind == TDESC_TYPE_STRUCT);
237
238 /* Initialize start and end so we know this is not a bit-field
239 when we print-c-tdesc. */
240 type->fields.emplace_back (field_name, field_type, -1, -1);
241}
242
268a13a5 243/* See gdbsupport/tdesc.h. */
eee8a18d
AH
244
245void
246tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
247 int start, int end, struct tdesc_type *field_type)
248{
249 gdb_assert (type->kind == TDESC_TYPE_STRUCT
250 || type->kind == TDESC_TYPE_FLAGS);
251 gdb_assert (start >= 0 && end >= start);
252
253 type->fields.emplace_back (field_name, field_type, start, end);
254}
255
268a13a5 256/* See gdbsupport/tdesc.h. */
eee8a18d
AH
257
258void
259tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
260 int start, int end)
261{
262 struct tdesc_type *field_type;
263
264 gdb_assert (start >= 0 && end >= start);
265
266 if (type->size > 4)
267 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
268 else
269 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
270
271 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
272}
273
268a13a5 274/* See gdbsupport/tdesc.h. */
eee8a18d
AH
275
276void
277tdesc_add_flag (tdesc_type_with_fields *type, int start,
278 const char *flag_name)
279{
280 gdb_assert (type->kind == TDESC_TYPE_FLAGS
281 || type->kind == TDESC_TYPE_STRUCT);
282
283 type->fields.emplace_back (flag_name,
284 tdesc_predefined_type (TDESC_TYPE_BOOL),
285 start, start);
286}
287
268a13a5 288/* See gdbsupport/tdesc.h. */
eee8a18d
AH
289
290void
291tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
292 const char *name)
293{
294 gdb_assert (type->kind == TDESC_TYPE_ENUM);
295 type->fields.emplace_back (name,
296 tdesc_predefined_type (TDESC_TYPE_INT32),
297 value, -1);
298}
e98577a9
AH
299
300void print_xml_feature::visit_pre (const tdesc_feature *e)
301{
caa7fd04
AB
302 add_line ("<feature name=\"%s\">", e->name.c_str ());
303 indent (1);
e98577a9
AH
304}
305
306void print_xml_feature::visit_post (const tdesc_feature *e)
307{
caa7fd04
AB
308 indent (-1);
309 add_line ("</feature>");
e98577a9
AH
310}
311
312void print_xml_feature::visit (const tdesc_type_builtin *t)
313{
314 error (_("xml output is not supported for type \"%s\"."), t->name.c_str ());
315}
316
317void print_xml_feature::visit (const tdesc_type_vector *t)
318{
caa7fd04
AB
319 add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
320 t->name.c_str (), t->element_type->name.c_str (), t->count);
e98577a9
AH
321}
322
323void print_xml_feature::visit (const tdesc_type_with_fields *t)
324{
e98577a9
AH
325 const static char *types[] = { "struct", "union", "flags", "enum" };
326
327 gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM);
328
caa7fd04
AB
329 std::string tmp;
330
331 string_appendf (tmp,
e98577a9
AH
332 "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT],
333 t->name.c_str ());
334
335 switch (t->kind)
336 {
337 case TDESC_TYPE_STRUCT:
338 case TDESC_TYPE_FLAGS:
339 if (t->size > 0)
caa7fd04
AB
340 string_appendf (tmp, " size=\"%d\"", t->size);
341 string_appendf (tmp, ">");
342 add_line (tmp);
e98577a9
AH
343
344 for (const tdesc_type_field &f : t->fields)
345 {
caa7fd04
AB
346 tmp.clear ();
347 string_appendf (tmp, " <field name=\"%s\"", f.name.c_str ());
348 if (f.start != -1)
349 string_appendf (tmp, " start=\"%d\" end=\"%d\"", f.start,
e98577a9 350 f.end);
caa7fd04
AB
351 string_appendf (tmp, " type=\"%s\"/>",
352 f.type->name.c_str ());
353 add_line (tmp);
e98577a9
AH
354 }
355 break;
356
357 case TDESC_TYPE_ENUM:
caa7fd04
AB
358 string_appendf (tmp, ">");
359 add_line (tmp);
e98577a9 360 for (const tdesc_type_field &f : t->fields)
caa7fd04
AB
361 add_line (" <field name=\"%s\" start=\"%d\"/>",
362 f.name.c_str (), f.start);
e98577a9
AH
363 break;
364
365 case TDESC_TYPE_UNION:
caa7fd04
AB
366 string_appendf (tmp, ">");
367 add_line (tmp);
e98577a9 368 for (const tdesc_type_field &f : t->fields)
caa7fd04
AB
369 add_line (" <field name=\"%s\" type=\"%s\"/>",
370 f.name.c_str (), f.type->name.c_str ());
e98577a9
AH
371 break;
372
373 default:
374 error (_("xml output is not supported for type \"%s\"."),
375 t->name.c_str ());
376 }
377
caa7fd04 378 add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]);
e98577a9
AH
379}
380
381void print_xml_feature::visit (const tdesc_reg *r)
382{
caa7fd04
AB
383 std::string tmp;
384
385 string_appendf (tmp,
e98577a9
AH
386 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
387 r->name.c_str (), r->bitsize, r->type.c_str (),
388 r->target_regnum);
389
390 if (r->group.length () > 0)
caa7fd04 391 string_appendf (tmp, " group=\"%s\"", r->group.c_str ());
e98577a9
AH
392
393 if (r->save_restore == 0)
caa7fd04 394 string_appendf (tmp, " save-restore=\"no\"");
e98577a9 395
caa7fd04
AB
396 string_appendf (tmp, "/>");
397
398 add_line (tmp);
e98577a9
AH
399}
400
401void print_xml_feature::visit_pre (const target_desc *e)
402{
403#ifndef IN_PROCESS_AGENT
caa7fd04
AB
404 add_line ("<?xml version=\"1.0\"?>");
405 add_line ("<!DOCTYPE target SYSTEM \"gdb-target.dtd\">");
406 add_line ("<target>");
407 indent (1);
408 if (tdesc_architecture_name (e))
409 add_line ("<architecture>%s</architecture>",
410 tdesc_architecture_name (e));
e98577a9
AH
411
412 const char *osabi = tdesc_osabi_name (e);
413 if (osabi != nullptr)
caa7fd04 414 add_line ("<osabi>%s</osabi>", osabi);
fbf42f4e
AB
415
416 const std::vector<tdesc_compatible_info_up> &compatible_list
417 = tdesc_compatible_info_list (e);
418 for (const auto &c : compatible_list)
caa7fd04
AB
419 add_line ("<compatible>%s</compatible>",
420 tdesc_compatible_info_arch_name (c));
e98577a9
AH
421#endif
422}
423
424void print_xml_feature::visit_post (const target_desc *e)
425{
caa7fd04
AB
426 indent (-1);
427 add_line ("</target>");
428}
429
430/* See gdbsupport/tdesc.h. */
431
432void
433print_xml_feature::add_line (const std::string &str)
434{
435 string_appendf (*m_buffer, "%*s", m_depth, "");
436 string_appendf (*m_buffer, "%s", str.c_str ());
437 string_appendf (*m_buffer, "\n");
438}
439
440/* See gdbsupport/tdesc.h. */
441
442void
443print_xml_feature::add_line (const char *fmt, ...)
444{
445 std::string tmp;
446
447 va_list ap;
448 va_start (ap, fmt);
449 string_vappendf (tmp, fmt, ap);
450 va_end (ap);
451 add_line (tmp);
e98577a9 452}