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