Commit | Line | Data |
---|---|---|
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 | |
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 | } | |
82ec9bc7 | 36 | |
eee8a18d AH |
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 }, | |
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 }, | |
2a67f09d FW |
57 | { "i387_ext", TDESC_TYPE_I387_EXT }, |
58 | { "bfloat16", TDESC_TYPE_BFLOAT16 } | |
eee8a18d AH |
59 | }; |
60 | ||
82ec9bc7 AH |
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 ® : 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 ®1 = registers[ix]; | |
85 | const tdesc_reg_up ®2 = 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 | ||
eee8a18d AH |
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 | ||
268a13a5 | 118 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 136 | /* See gdbsupport/tdesc.h. */ |
82ec9bc7 AH |
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 | } | |
eee8a18d | 148 | |
268a13a5 | 149 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 161 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 173 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 183 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 195 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 210 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 225 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 239 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 252 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 270 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | ||
268a13a5 | 284 | /* See gdbsupport/tdesc.h. */ |
eee8a18d AH |
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 | } | |
e98577a9 AH |
295 | |
296 | void print_xml_feature::visit_pre (const tdesc_feature *e) | |
297 | { | |
caa7fd04 AB |
298 | add_line ("<feature name=\"%s\">", e->name.c_str ()); |
299 | indent (1); | |
e98577a9 AH |
300 | } |
301 | ||
302 | void print_xml_feature::visit_post (const tdesc_feature *e) | |
303 | { | |
caa7fd04 AB |
304 | indent (-1); |
305 | add_line ("</feature>"); | |
e98577a9 AH |
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 | { | |
caa7fd04 AB |
315 | add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>", |
316 | t->name.c_str (), t->element_type->name.c_str (), t->count); | |
e98577a9 AH |
317 | } |
318 | ||
319 | void print_xml_feature::visit (const tdesc_type_with_fields *t) | |
320 | { | |
e98577a9 AH |
321 | const static char *types[] = { "struct", "union", "flags", "enum" }; |
322 | ||
323 | gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM); | |
324 | ||
caa7fd04 AB |
325 | std::string tmp; |
326 | ||
327 | string_appendf (tmp, | |
e98577a9 AH |
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) | |
caa7fd04 AB |
336 | string_appendf (tmp, " size=\"%d\"", t->size); |
337 | string_appendf (tmp, ">"); | |
338 | add_line (tmp); | |
e98577a9 AH |
339 | |
340 | for (const tdesc_type_field &f : t->fields) | |
341 | { | |
caa7fd04 AB |
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, | |
e98577a9 | 346 | f.end); |
caa7fd04 AB |
347 | string_appendf (tmp, " type=\"%s\"/>", |
348 | f.type->name.c_str ()); | |
349 | add_line (tmp); | |
e98577a9 AH |
350 | } |
351 | break; | |
352 | ||
353 | case TDESC_TYPE_ENUM: | |
caa7fd04 AB |
354 | string_appendf (tmp, ">"); |
355 | add_line (tmp); | |
e98577a9 | 356 | for (const tdesc_type_field &f : t->fields) |
caa7fd04 AB |
357 | add_line (" <field name=\"%s\" start=\"%d\"/>", |
358 | f.name.c_str (), f.start); | |
e98577a9 AH |
359 | break; |
360 | ||
361 | case TDESC_TYPE_UNION: | |
caa7fd04 AB |
362 | string_appendf (tmp, ">"); |
363 | add_line (tmp); | |
e98577a9 | 364 | for (const tdesc_type_field &f : t->fields) |
caa7fd04 AB |
365 | add_line (" <field name=\"%s\" type=\"%s\"/>", |
366 | f.name.c_str (), f.type->name.c_str ()); | |
e98577a9 AH |
367 | break; |
368 | ||
369 | default: | |
370 | error (_("xml output is not supported for type \"%s\"."), | |
371 | t->name.c_str ()); | |
372 | } | |
373 | ||
caa7fd04 | 374 | add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]); |
e98577a9 AH |
375 | } |
376 | ||
377 | void print_xml_feature::visit (const tdesc_reg *r) | |
378 | { | |
caa7fd04 AB |
379 | std::string tmp; |
380 | ||
381 | string_appendf (tmp, | |
e98577a9 AH |
382 | "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"", |
383 | r->name.c_str (), r->bitsize, r->type.c_str (), | |
384 | r->target_regnum); | |
385 | ||
386 | if (r->group.length () > 0) | |
caa7fd04 | 387 | string_appendf (tmp, " group=\"%s\"", r->group.c_str ()); |
e98577a9 AH |
388 | |
389 | if (r->save_restore == 0) | |
caa7fd04 | 390 | string_appendf (tmp, " save-restore=\"no\""); |
e98577a9 | 391 | |
caa7fd04 AB |
392 | string_appendf (tmp, "/>"); |
393 | ||
394 | add_line (tmp); | |
e98577a9 AH |
395 | } |
396 | ||
397 | void print_xml_feature::visit_pre (const target_desc *e) | |
398 | { | |
399 | #ifndef IN_PROCESS_AGENT | |
caa7fd04 AB |
400 | add_line ("<?xml version=\"1.0\"?>"); |
401 | add_line ("<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"); | |
402 | add_line ("<target>"); | |
403 | indent (1); | |
404 | if (tdesc_architecture_name (e)) | |
405 | add_line ("<architecture>%s</architecture>", | |
406 | tdesc_architecture_name (e)); | |
e98577a9 AH |
407 | |
408 | const char *osabi = tdesc_osabi_name (e); | |
409 | if (osabi != nullptr) | |
caa7fd04 | 410 | add_line ("<osabi>%s</osabi>", osabi); |
fbf42f4e AB |
411 | |
412 | const std::vector<tdesc_compatible_info_up> &compatible_list | |
413 | = tdesc_compatible_info_list (e); | |
414 | for (const auto &c : compatible_list) | |
caa7fd04 AB |
415 | add_line ("<compatible>%s</compatible>", |
416 | tdesc_compatible_info_arch_name (c)); | |
e98577a9 AH |
417 | #endif |
418 | } | |
419 | ||
420 | void print_xml_feature::visit_post (const target_desc *e) | |
421 | { | |
caa7fd04 AB |
422 | indent (-1); |
423 | add_line ("</target>"); | |
424 | } | |
425 | ||
426 | /* See gdbsupport/tdesc.h. */ | |
427 | ||
428 | void | |
429 | print_xml_feature::add_line (const std::string &str) | |
430 | { | |
431 | string_appendf (*m_buffer, "%*s", m_depth, ""); | |
432 | string_appendf (*m_buffer, "%s", str.c_str ()); | |
433 | string_appendf (*m_buffer, "\n"); | |
434 | } | |
435 | ||
436 | /* See gdbsupport/tdesc.h. */ | |
437 | ||
438 | void | |
439 | print_xml_feature::add_line (const char *fmt, ...) | |
440 | { | |
441 | std::string tmp; | |
442 | ||
443 | va_list ap; | |
444 | va_start (ap, fmt); | |
445 | string_vappendf (tmp, fmt, ap); | |
446 | va_end (ap); | |
447 | add_line (tmp); | |
e98577a9 | 448 | } |