]> Git Repo - binutils.git/blob - gdbsupport/tdesc.cc
Automatic date update in version.in
[binutils.git] / gdbsupport / tdesc.cc
1 /* Target description support for GDB.
2
3    Copyright (C) 2018-2022 Free Software Foundation, Inc.
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"
21 #include "gdbsupport/tdesc.h"
22
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_ : ""),
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 }
36
37 /* Predefined types.  */
38 static 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 },
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 },
58   { "bfloat16", TDESC_TYPE_BFLOAT16 }
59 };
60
61 void tdesc_feature::accept (tdesc_element_visitor &v) const
62 {
63   v.visit_pre (this);
64
65   for (const tdesc_type_up &type : types)
66     type->accept (v);
67
68   for (const tdesc_reg_up &reg : registers)
69     reg->accept (v);
70
71   v.visit_post (this);
72 }
73
74 bool tdesc_feature::operator== (const tdesc_feature &other) const
75 {
76   if (name != other.name)
77     return false;
78
79   if (registers.size () != other.registers.size ())
80     return false;
81
82   for (int ix = 0; ix < registers.size (); ix++)
83     {
84       const tdesc_reg_up &reg1 = registers[ix];
85       const tdesc_reg_up &reg2 = other.registers[ix];
86
87       if (reg1 != reg2 && *reg1 != *reg2)
88         return false;
89       }
90
91   if (types.size () != other.types.size ())
92     return false;
93
94   for (int ix = 0; ix < types.size (); ix++)
95     {
96       const tdesc_type_up &type1 = types[ix];
97       const tdesc_type_up &type2 = other.types[ix];
98
99       if (type1 != type2 && *type1 != *type2)
100         return false;
101     }
102
103   return true;
104 }
105
106 /* Lookup a predefined type.  */
107
108 static struct tdesc_type *
109 tdesc_predefined_type (enum tdesc_type_kind kind)
110 {
111   for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
112     if (tdesc_predefined_types[ix].kind == kind)
113       return &tdesc_predefined_types[ix];
114
115   gdb_assert_not_reached ("bad predefined tdesc type");
116 }
117
118 /* See gdbsupport/tdesc.h.  */
119
120 struct tdesc_type *
121 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
122 {
123   /* First try target-defined types.  */
124   for (const tdesc_type_up &type : feature->types)
125     if (type->name == id)
126       return type.get ();
127
128   /* Next try the predefined types.  */
129   for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
130     if (tdesc_predefined_types[ix].name == id)
131       return &tdesc_predefined_types[ix];
132
133   return NULL;
134 }
135
136 /* See gdbsupport/tdesc.h.  */
137
138 void
139 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
140                   int regnum, int save_restore, const char *group,
141                   int bitsize, const char *type)
142 {
143   tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
144                                   group, bitsize, type);
145
146   feature->registers.emplace_back (reg);
147 }
148
149 /* See gdbsupport/tdesc.h.  */
150
151 struct tdesc_type *
152 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
153                      struct tdesc_type *field_type, int count)
154 {
155   tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
156   feature->types.emplace_back (type);
157
158   return type;
159 }
160
161 /* See gdbsupport/tdesc.h.  */
162
163 tdesc_type_with_fields *
164 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
165 {
166   tdesc_type_with_fields *type
167     = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
168   feature->types.emplace_back (type);
169
170   return type;
171 }
172
173 /* See gdbsupport/tdesc.h.  */
174
175 void
176 tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
177 {
178   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
179   gdb_assert (size > 0);
180   type->size = size;
181 }
182
183 /* See gdbsupport/tdesc.h.  */
184
185 tdesc_type_with_fields *
186 tdesc_create_union (struct tdesc_feature *feature, const char *name)
187 {
188   tdesc_type_with_fields *type
189     = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
190   feature->types.emplace_back (type);
191
192   return type;
193 }
194
195 /* See gdbsupport/tdesc.h.  */
196
197 tdesc_type_with_fields *
198 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
199                     int size)
200 {
201   gdb_assert (size > 0);
202
203   tdesc_type_with_fields *type
204     = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
205   feature->types.emplace_back (type);
206
207   return type;
208 }
209
210 /* See gdbsupport/tdesc.h.  */
211
212 tdesc_type_with_fields *
213 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
214                    int size)
215 {
216   gdb_assert (size > 0);
217
218   tdesc_type_with_fields *type
219     = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
220   feature->types.emplace_back (type);
221
222   return type;
223 }
224
225 /* See gdbsupport/tdesc.h.  */
226
227 void
228 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
229                  struct tdesc_type *field_type)
230 {
231   gdb_assert (type->kind == TDESC_TYPE_UNION
232               || type->kind == TDESC_TYPE_STRUCT);
233
234   /* Initialize start and end so we know this is not a bit-field
235      when we print-c-tdesc.  */
236   type->fields.emplace_back (field_name, field_type, -1, -1);
237 }
238
239 /* See gdbsupport/tdesc.h.  */
240
241 void
242 tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
243                           int start, int end, struct tdesc_type *field_type)
244 {
245   gdb_assert (type->kind == TDESC_TYPE_STRUCT
246               || type->kind == TDESC_TYPE_FLAGS);
247   gdb_assert (start >= 0 && end >= start);
248
249   type->fields.emplace_back (field_name, field_type, start, end);
250 }
251
252 /* See gdbsupport/tdesc.h.  */
253
254 void
255 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
256                     int start, int end)
257 {
258   struct tdesc_type *field_type;
259
260   gdb_assert (start >= 0 && end >= start);
261
262   if (type->size > 4)
263     field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
264   else
265     field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
266
267   tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
268 }
269
270 /* See gdbsupport/tdesc.h.  */
271
272 void
273 tdesc_add_flag (tdesc_type_with_fields *type, int start,
274                 const char *flag_name)
275 {
276   gdb_assert (type->kind == TDESC_TYPE_FLAGS
277               || type->kind == TDESC_TYPE_STRUCT);
278
279   type->fields.emplace_back (flag_name,
280                              tdesc_predefined_type (TDESC_TYPE_BOOL),
281                              start, start);
282 }
283
284 /* See gdbsupport/tdesc.h.  */
285
286 void
287 tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
288                       const char *name)
289 {
290   gdb_assert (type->kind == TDESC_TYPE_ENUM);
291   type->fields.emplace_back (name,
292                              tdesc_predefined_type (TDESC_TYPE_INT32),
293                              value, -1);
294 }
295
296 void print_xml_feature::visit_pre (const tdesc_feature *e)
297 {
298   add_line ("<feature name=\"%s\">", e->name.c_str ());
299   indent (1);
300 }
301
302 void print_xml_feature::visit_post (const tdesc_feature *e)
303 {
304   indent (-1);
305   add_line ("</feature>");
306 }
307
308 void print_xml_feature::visit (const tdesc_type_builtin *t)
309 {
310   error (_("xml output is not supported for type \"%s\"."), t->name.c_str ());
311 }
312
313 void print_xml_feature::visit (const tdesc_type_vector *t)
314 {
315   add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>",
316             t->name.c_str (), t->element_type->name.c_str (), t->count);
317 }
318
319 void print_xml_feature::visit (const tdesc_type_with_fields *t)
320 {
321   const static char *types[] = { "struct", "union", "flags", "enum" };
322
323   gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM);
324
325   std::string tmp;
326
327   string_appendf (tmp,
328                   "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT],
329                   t->name.c_str ());
330
331   switch (t->kind)
332     {
333     case TDESC_TYPE_STRUCT:
334     case TDESC_TYPE_FLAGS:
335       if (t->size > 0)
336         string_appendf (tmp, " size=\"%d\"", t->size);
337       string_appendf (tmp, ">");
338       add_line (tmp);
339
340       for (const tdesc_type_field &f : t->fields)
341         {
342           tmp.clear ();
343           string_appendf (tmp, "  <field name=\"%s\"", f.name.c_str ());
344           if (f.start != -1)
345             string_appendf (tmp, " start=\"%d\" end=\"%d\"", f.start,
346                             f.end);
347           string_appendf (tmp, " type=\"%s\"/>",
348                           f.type->name.c_str ());
349           add_line (tmp);
350         }
351       break;
352
353     case TDESC_TYPE_ENUM:
354       if (t->size > 0)
355         string_appendf (tmp, " size=\"%d\"", t->size);
356       string_appendf (tmp, ">");
357       add_line (tmp);
358       /* The 'start' of the field is reused as the enum value.  The 'end'
359          of the field is always set to -1 for enum values.  */
360       for (const tdesc_type_field &f : t->fields)
361         add_line ("  <evalue name=\"%s\" value=\"%d\"/>",
362                   f.name.c_str (), f.start);
363       break;
364
365     case TDESC_TYPE_UNION:
366       string_appendf (tmp, ">");
367       add_line (tmp);
368       for (const tdesc_type_field &f : t->fields)
369         add_line ("  <field name=\"%s\" type=\"%s\"/>",
370                   f.name.c_str (), f.type->name.c_str ());
371       break;
372
373     default:
374       error (_("xml output is not supported for type \"%s\"."),
375              t->name.c_str ());
376     }
377
378   add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]);
379 }
380
381 void print_xml_feature::visit (const tdesc_reg *r)
382 {
383   std::string tmp;
384
385   string_appendf (tmp,
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)
391     string_appendf (tmp, " group=\"%s\"", r->group.c_str ());
392
393   if (r->save_restore == 0)
394     string_appendf (tmp, " save-restore=\"no\"");
395
396   string_appendf (tmp, "/>");
397
398   add_line (tmp);
399 }
400
401 void print_xml_feature::visit_pre (const target_desc *e)
402 {
403 #ifndef IN_PROCESS_AGENT
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));
411
412   const char *osabi = tdesc_osabi_name (e);
413   if (osabi != nullptr)
414     add_line ("<osabi>%s</osabi>", osabi);
415
416   const std::vector<tdesc_compatible_info_up> &compatible_list
417     = tdesc_compatible_info_list (e);
418   for (const auto &c : compatible_list)
419     add_line ("<compatible>%s</compatible>",
420               tdesc_compatible_info_arch_name (c));
421 #endif
422 }
423
424 void print_xml_feature::visit_post (const target_desc *e)
425 {
426   indent (-1);
427   add_line ("</target>");
428 }
429
430 /* See gdbsupport/tdesc.h.  */
431
432 void
433 print_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
442 void
443 print_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);
452 }
This page took 0.048284 seconds and 4 git commands to generate.