1 /* Target description support for GDB.
3 Copyright (C) 2018 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 "common-defs.h"
21 #include "common/tdesc.h"
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_
: ""),
30 type (type_
!= NULL
? type_
: "<unknown>")
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 ());
37 /* Predefined types. */
38 static tdesc_type_builtin tdesc_predefined_types
[] =
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_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
}
59 void tdesc_feature::accept (tdesc_element_visitor
&v
) const
63 for (const tdesc_type_up
&type
: types
)
66 for (const tdesc_reg_up
®
: registers
)
72 bool tdesc_feature::operator== (const tdesc_feature
&other
) const
74 if (name
!= other
.name
)
77 if (registers
.size () != other
.registers
.size ())
80 for (int ix
= 0; ix
< registers
.size (); ix
++)
82 const tdesc_reg_up
®1
= registers
[ix
];
83 const tdesc_reg_up
®2
= other
.registers
[ix
];
85 if (reg1
!= reg2
&& *reg1
!= *reg2
)
89 if (types
.size () != other
.types
.size ())
92 for (int ix
= 0; ix
< types
.size (); ix
++)
94 const tdesc_type_up
&type1
= types
[ix
];
95 const tdesc_type_up
&type2
= other
.types
[ix
];
97 if (type1
!= type2
&& *type1
!= *type2
)
104 /* Lookup a predefined type. */
106 static struct tdesc_type
*
107 tdesc_predefined_type (enum tdesc_type_kind kind
)
109 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
110 if (tdesc_predefined_types
[ix
].kind
== kind
)
111 return &tdesc_predefined_types
[ix
];
113 gdb_assert_not_reached ("bad predefined tdesc type");
116 /* See common/tdesc.h. */
119 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
121 /* First try target-defined types. */
122 for (const tdesc_type_up
&type
: feature
->types
)
123 if (type
->name
== id
)
126 /* Next try the predefined types. */
127 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
128 if (tdesc_predefined_types
[ix
].name
== id
)
129 return &tdesc_predefined_types
[ix
];
134 /* See common/tdesc.h. */
137 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
138 int regnum
, int save_restore
, const char *group
,
139 int bitsize
, const char *type
)
141 tdesc_reg
*reg
= new tdesc_reg (feature
, name
, regnum
, save_restore
,
142 group
, bitsize
, type
);
144 feature
->registers
.emplace_back (reg
);
147 /* See common/tdesc.h. */
150 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
151 struct tdesc_type
*field_type
, int count
)
153 tdesc_type_vector
*type
= new tdesc_type_vector (name
, field_type
, count
);
154 feature
->types
.emplace_back (type
);
159 /* See common/tdesc.h. */
161 tdesc_type_with_fields
*
162 tdesc_create_struct (struct tdesc_feature
*feature
, const char *name
)
164 tdesc_type_with_fields
*type
165 = new tdesc_type_with_fields (name
, TDESC_TYPE_STRUCT
);
166 feature
->types
.emplace_back (type
);
171 /* See common/tdesc.h. */
174 tdesc_set_struct_size (tdesc_type_with_fields
*type
, int size
)
176 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
177 gdb_assert (size
> 0);
181 /* See common/tdesc.h. */
183 tdesc_type_with_fields
*
184 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
186 tdesc_type_with_fields
*type
187 = new tdesc_type_with_fields (name
, TDESC_TYPE_UNION
);
188 feature
->types
.emplace_back (type
);
193 /* See common/tdesc.h. */
195 tdesc_type_with_fields
*
196 tdesc_create_flags (struct tdesc_feature
*feature
, const char *name
,
199 gdb_assert (size
> 0);
201 tdesc_type_with_fields
*type
202 = new tdesc_type_with_fields (name
, TDESC_TYPE_FLAGS
, size
);
203 feature
->types
.emplace_back (type
);
208 /* See common/tdesc.h. */
210 tdesc_type_with_fields
*
211 tdesc_create_enum (struct tdesc_feature
*feature
, const char *name
,
214 gdb_assert (size
> 0);
216 tdesc_type_with_fields
*type
217 = new tdesc_type_with_fields (name
, TDESC_TYPE_ENUM
, size
);
218 feature
->types
.emplace_back (type
);
223 /* See common/tdesc.h. */
226 tdesc_add_field (tdesc_type_with_fields
*type
, const char *field_name
,
227 struct tdesc_type
*field_type
)
229 gdb_assert (type
->kind
== TDESC_TYPE_UNION
230 || type
->kind
== TDESC_TYPE_STRUCT
);
232 /* Initialize start and end so we know this is not a bit-field
233 when we print-c-tdesc. */
234 type
->fields
.emplace_back (field_name
, field_type
, -1, -1);
237 /* See common/tdesc.h. */
240 tdesc_add_typed_bitfield (tdesc_type_with_fields
*type
, const char *field_name
,
241 int start
, int end
, struct tdesc_type
*field_type
)
243 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
244 || type
->kind
== TDESC_TYPE_FLAGS
);
245 gdb_assert (start
>= 0 && end
>= start
);
247 type
->fields
.emplace_back (field_name
, field_type
, start
, end
);
250 /* See common/tdesc.h. */
253 tdesc_add_bitfield (tdesc_type_with_fields
*type
, const char *field_name
,
256 struct tdesc_type
*field_type
;
258 gdb_assert (start
>= 0 && end
>= start
);
261 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT64
);
263 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT32
);
265 tdesc_add_typed_bitfield (type
, field_name
, start
, end
, field_type
);
268 /* See common/tdesc.h. */
271 tdesc_add_flag (tdesc_type_with_fields
*type
, int start
,
272 const char *flag_name
)
274 gdb_assert (type
->kind
== TDESC_TYPE_FLAGS
275 || type
->kind
== TDESC_TYPE_STRUCT
);
277 type
->fields
.emplace_back (flag_name
,
278 tdesc_predefined_type (TDESC_TYPE_BOOL
),
282 /* See common/tdesc.h. */
285 tdesc_add_enum_value (tdesc_type_with_fields
*type
, int value
,
288 gdb_assert (type
->kind
== TDESC_TYPE_ENUM
);
289 type
->fields
.emplace_back (name
,
290 tdesc_predefined_type (TDESC_TYPE_INT32
),
294 void print_xml_feature::visit_pre (const tdesc_feature
*e
)
296 string_appendf (*m_buffer
, "<feature name=\"%s\">\n", e
->name
.c_str ());
299 void print_xml_feature::visit_post (const tdesc_feature
*e
)
301 string_appendf (*m_buffer
, "</feature>\n");
304 void print_xml_feature::visit (const tdesc_type_builtin
*t
)
306 error (_("xml output is not supported for type \"%s\"."), t
->name
.c_str ());
309 void print_xml_feature::visit (const tdesc_type_vector
*t
)
311 string_appendf (*m_buffer
, "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
312 t
->name
.c_str (), t
->element_type
->name
.c_str (), t
->count
);
315 void print_xml_feature::visit (const tdesc_type_with_fields
*t
)
317 struct tdesc_type_field
*f
;
318 const static char *types
[] = { "struct", "union", "flags", "enum" };
320 gdb_assert (t
->kind
>= TDESC_TYPE_STRUCT
&& t
->kind
<= TDESC_TYPE_ENUM
);
322 string_appendf (*m_buffer
,
323 "<%s id=\"%s\"", types
[t
->kind
- TDESC_TYPE_STRUCT
],
328 case TDESC_TYPE_STRUCT
:
329 case TDESC_TYPE_FLAGS
:
331 string_appendf (*m_buffer
, " size=\"%d\"", t
->size
);
332 string_appendf (*m_buffer
, ">\n");
334 for (const tdesc_type_field
&f
: t
->fields
)
336 string_appendf (*m_buffer
, " <field name=\"%s\" ", f
.name
.c_str ());
338 string_appendf (*m_buffer
, "type=\"%s\"/>\n",
339 f
.type
->name
.c_str ());
341 string_appendf (*m_buffer
, "start=\"%d\" end=\"%d\"/>\n", f
.start
,
346 case TDESC_TYPE_ENUM
:
347 string_appendf (*m_buffer
, ">\n");
348 for (const tdesc_type_field
&f
: t
->fields
)
349 string_appendf (*m_buffer
, " <field name=\"%s\" start=\"%d\"/>\n",
350 f
.name
.c_str (), f
.start
);
353 case TDESC_TYPE_UNION
:
354 string_appendf (*m_buffer
, ">\n");
355 for (const tdesc_type_field
&f
: t
->fields
)
356 string_appendf (*m_buffer
, " <field name=\"%s\" type=\"%s\"/>\n",
357 f
.name
.c_str (), f
.type
->name
.c_str ());
361 error (_("xml output is not supported for type \"%s\"."),
365 string_appendf (*m_buffer
, "</%s>\n", types
[t
->kind
- TDESC_TYPE_STRUCT
]);
368 void print_xml_feature::visit (const tdesc_reg
*r
)
370 string_appendf (*m_buffer
,
371 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"",
372 r
->name
.c_str (), r
->bitsize
, r
->type
.c_str (),
375 if (r
->group
.length () > 0)
376 string_appendf (*m_buffer
, " group=\"%s\"", r
->group
.c_str ());
378 if (r
->save_restore
== 0)
379 string_appendf (*m_buffer
, " save-restore=\"no\"");
381 string_appendf (*m_buffer
, "/>\n");
384 void print_xml_feature::visit_pre (const target_desc
*e
)
386 #ifndef IN_PROCESS_AGENT
387 string_appendf (*m_buffer
, "<?xml version=\"1.0\"?>\n");
388 string_appendf (*m_buffer
, "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n");
389 string_appendf (*m_buffer
, "<target>\n<architecture>%s</architecture>\n",
390 tdesc_architecture_name (e
));
392 const char *osabi
= tdesc_osabi_name (e
);
393 if (osabi
!= nullptr)
394 string_appendf (*m_buffer
, "<osabi>%s</osabi>", osabi
);
398 void print_xml_feature::visit_post (const target_desc
*e
)
400 string_appendf (*m_buffer
, "</target>\n");