]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/target-descriptions.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / target-descriptions.c
CommitLineData
424163ea
DJ
1/* Target description support for GDB.
2
d01e8234 3 Copyright (C) 2006-2025 Free Software Foundation, Inc.
424163ea
DJ
4
5 Contributed by CodeSourcery.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
424163ea
DJ
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
424163ea 21
424163ea 22#include "arch-utils.h"
5b9707eb 23#include "cli/cli-cmds.h"
d64c62fd 24#include "gdbsupport/unordered_set.h"
123dc839
DJ
25#include "gdbtypes.h"
26#include "reggroups.h"
424163ea
DJ
27#include "target.h"
28#include "target-descriptions.h"
123dc839 29#include "xml-support.h"
23181151 30#include "xml-tdesc.h"
c3f08eb7 31#include "osabi.h"
424163ea 32
bf31fd38 33#include "gdbsupport/gdb_obstack.h"
6ecd4729 34#include "inferior.h"
25aa13e5 35#include <algorithm>
27d41eac 36#include "completer.h"
ef0f16cc 37#include "readline/tilde.h"
fc0a39f4 38#include "cli/cli-style.h"
424163ea
DJ
39
40/* Types. */
41
129c10bc 42struct property
29709017 43{
129c10bc
SM
44 property (const std::string &key_, const std::string &value_)
45 : key (key_), value (value_)
46 {}
47
48 std::string key;
49 std::string value;
50};
29709017 51
b8df6ca7
AH
52/* Convert a tdesc_type to a gdb type. */
53
54static type *
55make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
56{
57 class gdb_type_creator : public tdesc_element_visitor
27d41eac 58 {
b8df6ca7
AH
59 public:
60 gdb_type_creator (struct gdbarch *gdbarch)
61 : m_gdbarch (gdbarch)
62 {}
d4a0e8b5 63
b8df6ca7
AH
64 type *get_type ()
65 {
66 return m_type;
67 }
d4a0e8b5 68
b8df6ca7
AH
69 void visit (const tdesc_type_builtin *e) override
70 {
71 switch (e->kind)
72 {
73 /* Predefined types. */
74 case TDESC_TYPE_BOOL:
75 m_type = builtin_type (m_gdbarch)->builtin_bool;
76 return;
77 case TDESC_TYPE_INT8:
78 m_type = builtin_type (m_gdbarch)->builtin_int8;
79 return;
80 case TDESC_TYPE_INT16:
81 m_type = builtin_type (m_gdbarch)->builtin_int16;
82 return;
83 case TDESC_TYPE_INT32:
84 m_type = builtin_type (m_gdbarch)->builtin_int32;
85 return;
86 case TDESC_TYPE_INT64:
87 m_type = builtin_type (m_gdbarch)->builtin_int64;
88 return;
89 case TDESC_TYPE_INT128:
90 m_type = builtin_type (m_gdbarch)->builtin_int128;
91 return;
92 case TDESC_TYPE_UINT8:
93 m_type = builtin_type (m_gdbarch)->builtin_uint8;
94 return;
95 case TDESC_TYPE_UINT16:
96 m_type = builtin_type (m_gdbarch)->builtin_uint16;
97 return;
98 case TDESC_TYPE_UINT32:
99 m_type = builtin_type (m_gdbarch)->builtin_uint32;
100 return;
101 case TDESC_TYPE_UINT64:
102 m_type = builtin_type (m_gdbarch)->builtin_uint64;
103 return;
104 case TDESC_TYPE_UINT128:
105 m_type = builtin_type (m_gdbarch)->builtin_uint128;
106 return;
107 case TDESC_TYPE_CODE_PTR:
108 m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
109 return;
110 case TDESC_TYPE_DATA_PTR:
111 m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
112 return;
113 }
d4a0e8b5 114
b8df6ca7
AH
115 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
116 if (m_type != NULL)
117 return;
27d41eac 118
77c5f496 119 type_allocator alloc (m_gdbarch);
b8df6ca7
AH
120 switch (e->kind)
121 {
a6d0f249 122 case TDESC_TYPE_IEEE_HALF:
77c5f496 123 m_type = init_float_type (alloc, -1, "builtin_type_ieee_half",
a6d0f249
AH
124 floatformats_ieee_half);
125 return;
126
b8df6ca7 127 case TDESC_TYPE_IEEE_SINGLE:
77c5f496 128 m_type = init_float_type (alloc, -1, "builtin_type_ieee_single",
b8df6ca7
AH
129 floatformats_ieee_single);
130 return;
131
132 case TDESC_TYPE_IEEE_DOUBLE:
77c5f496 133 m_type = init_float_type (alloc, -1, "builtin_type_ieee_double",
b8df6ca7
AH
134 floatformats_ieee_double);
135 return;
136 case TDESC_TYPE_ARM_FPA_EXT:
77c5f496 137 m_type = init_float_type (alloc, -1, "builtin_type_arm_ext",
b8df6ca7
AH
138 floatformats_arm_ext);
139 return;
140
141 case TDESC_TYPE_I387_EXT:
77c5f496 142 m_type = init_float_type (alloc, -1, "builtin_type_i387_ext",
b8df6ca7
AH
143 floatformats_i387_ext);
144 return;
2a67f09d
FW
145
146 case TDESC_TYPE_BFLOAT16:
77c5f496 147 m_type = init_float_type (alloc, -1, "builtin_type_bfloat16",
2a67f09d
FW
148 floatformats_bfloat16);
149 return;
b8df6ca7 150 }
d4a0e8b5 151
f34652de 152 internal_error ("Type \"%s\" has an unknown kind %d",
b8df6ca7
AH
153 e->name.c_str (), e->kind);
154 }
d4a0e8b5 155
b8df6ca7
AH
156 void visit (const tdesc_type_vector *e) override
157 {
158 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
159 if (m_type != NULL)
160 return;
161
162 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
163 m_type = init_vector_type (element_gdb_type, e->count);
d0e39ea2 164 m_type->set_name (xstrdup (e->name.c_str ()));
b8df6ca7
AH
165 return;
166 }
53c934e9 167
b8df6ca7
AH
168 void visit (const tdesc_type_with_fields *e) override
169 {
170 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
171 if (m_type != NULL)
172 return;
d4a0e8b5 173
b8df6ca7
AH
174 switch (e->kind)
175 {
176 case TDESC_TYPE_STRUCT:
177 make_gdb_type_struct (e);
178 return;
179 case TDESC_TYPE_UNION:
180 make_gdb_type_union (e);
181 return;
182 case TDESC_TYPE_FLAGS:
183 make_gdb_type_flags (e);
184 return;
185 case TDESC_TYPE_ENUM:
186 make_gdb_type_enum (e);
187 return;
188 }
d4a0e8b5 189
f34652de 190 internal_error ("Type \"%s\" has an unknown kind %d",
b8df6ca7
AH
191 e->name.c_str (), e->kind);
192 }
d4a0e8b5 193
b8df6ca7 194 private:
d4a0e8b5 195
b8df6ca7
AH
196 void make_gdb_type_struct (const tdesc_type_with_fields *e)
197 {
198 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
d0e39ea2 199 m_type->set_name (xstrdup (e->name.c_str ()));
d4a0e8b5 200
b8df6ca7
AH
201 for (const tdesc_type_field &f : e->fields)
202 {
203 if (f.start != -1 && f.end != -1)
204 {
205 /* Bitfield. */
206 struct field *fld;
207 struct type *field_gdb_type;
208 int bitsize, total_size;
209
210 /* This invariant should be preserved while creating types. */
211 gdb_assert (e->size != 0);
212 if (f.type != NULL)
213 field_gdb_type = make_gdb_type (m_gdbarch, f.type);
214 else if (e->size > 4)
215 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
216 else
217 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
218
219 fld = append_composite_type_field_raw
220 (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
221
222 /* For little-endian, BITPOS counts from the LSB of
223 the structure and marks the LSB of the field. For
224 big-endian, BITPOS counts from the MSB of the
225 structure and marks the MSB of the field. Either
226 way, it is the number of bits to the "left" of the
227 field. To calculate this in big-endian, we need
228 the total size of the structure. */
229 bitsize = f.end - f.start + 1;
230 total_size = e->size * TARGET_CHAR_BIT;
d5a22e77 231 if (gdbarch_byte_order (m_gdbarch) == BFD_ENDIAN_BIG)
cd3f655c 232 fld->set_loc_bitpos (total_size - f.start - bitsize);
b8df6ca7 233 else
cd3f655c 234 fld->set_loc_bitpos (f.start);
886176b8 235 fld->set_bitsize (bitsize);
b8df6ca7
AH
236 }
237 else
238 {
239 gdb_assert (f.start == -1 && f.end == -1);
240 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
241 append_composite_type_field (m_type,
d4a0e8b5 242 xstrdup (f.name.c_str ()),
b8df6ca7
AH
243 field_gdb_type);
244 }
245 }
d4a0e8b5 246
b8df6ca7 247 if (e->size != 0)
b6cdbc9a 248 m_type->set_length (e->size);
b8df6ca7 249 }
d4a0e8b5 250
b8df6ca7
AH
251 void make_gdb_type_union (const tdesc_type_with_fields *e)
252 {
253 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
d0e39ea2 254 m_type->set_name (xstrdup (e->name.c_str ()));
d4a0e8b5 255
b8df6ca7
AH
256 for (const tdesc_type_field &f : e->fields)
257 {
258 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
259 append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
260 field_gdb_type);
261
262 /* If any of the children of a union are vectors, flag the
263 union as a vector also. This allows e.g. a union of two
264 vector types to show up automatically in "info vector". */
bd63c870 265 if (field_gdb_type->is_vector ())
2062087b 266 m_type->set_is_vector (true);
b8df6ca7
AH
267 }
268 }
d4a0e8b5 269
b8df6ca7 270 void make_gdb_type_flags (const tdesc_type_with_fields *e)
d4a0e8b5 271 {
b8df6ca7
AH
272 m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
273 e->size * TARGET_CHAR_BIT);
274
275 for (const tdesc_type_field &f : e->fields)
276 {
277 int bitsize = f.end - f.start + 1;
278
279 gdb_assert (f.type != NULL);
280 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
281 append_flags_type_field (m_type, f.start, bitsize,
282 field_gdb_type, f.name.c_str ());
283 }
d4a0e8b5
SM
284 }
285
b8df6ca7
AH
286 void make_gdb_type_enum (const tdesc_type_with_fields *e)
287 {
cc495054
TT
288 m_type = (type_allocator (m_gdbarch)
289 .new_type (TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
290 e->name.c_str ()));
d4a0e8b5 291
653223d3
SM
292 m_type->set_is_unsigned (true);
293
b8df6ca7
AH
294 for (const tdesc_type_field &f : e->fields)
295 {
296 struct field *fld
297 = append_composite_type_field_raw (m_type,
298 xstrdup (f.name.c_str ()),
299 NULL);
d4a0e8b5 300
cd3f655c 301 fld->set_loc_enumval (f.start);
b8df6ca7
AH
302 }
303 }
304
305 /* The gdbarch used. */
306 struct gdbarch *m_gdbarch;
307
308 /* The type created. */
309 type *m_type;
310 };
311
312 gdb_type_creator gdb_type (gdbarch);
313 ttype->accept (gdb_type);
314 return gdb_type.get_type ();
315}
123dc839 316
fbf42f4e
AB
317/* Wrapper around bfd_arch_info_type. A class with this name is used in
318 the API that is shared between gdb and gdbserver code, but gdbserver
319 doesn't use compatibility information, so its version of this class is
320 empty. */
321
322class tdesc_compatible_info
323{
324public:
325 /* Constructor. */
326 explicit tdesc_compatible_info (const bfd_arch_info_type *arch)
327 : m_arch (arch)
328 { /* Nothing. */ }
329
330 /* Access the contained pointer. */
331 const bfd_arch_info_type *arch () const
332 { return m_arch; }
333
334private:
335 /* Architecture information looked up from the <compatible> entity within
336 a target description. */
337 const bfd_arch_info_type *m_arch;
338};
339
123dc839
DJ
340/* A target description. */
341
6eb1e6a8 342struct target_desc : tdesc_element
424163ea 343{
b468ff4c
YQ
344 target_desc ()
345 {}
346
3eea796c 347 virtual ~target_desc () = default;
b468ff4c
YQ
348
349 target_desc (const target_desc &) = delete;
350 void operator= (const target_desc &) = delete;
351
23181151 352 /* The architecture reported by the target, if any. */
b468ff4c 353 const struct bfd_arch_info *arch = NULL;
23181151 354
08d16641
PA
355 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
356 otherwise. */
b468ff4c 357 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
08d16641 358
e35359c5 359 /* The list of compatible architectures reported by the target. */
fbf42f4e 360 std::vector<tdesc_compatible_info_up> compatible;
e35359c5 361
29709017 362 /* Any architecture-specific properties specified by the target. */
129c10bc 363 std::vector<property> properties;
123dc839
DJ
364
365 /* The features associated with this target. */
858c9d13 366 std::vector<tdesc_feature_up> features;
6eb1e6a8 367
e98577a9
AH
368 /* Used to cache the generated xml version of the target description. */
369 mutable char *xmltarget = nullptr;
370
6eb1e6a8
YQ
371 void accept (tdesc_element_visitor &v) const override
372 {
373 v.visit_pre (this);
374
3eea796c 375 for (const tdesc_feature_up &feature : features)
6eb1e6a8
YQ
376 feature->accept (v);
377
378 v.visit_post (this);
379 }
27d41eac
YQ
380
381 bool operator== (const target_desc &other) const
382 {
383 if (arch != other.arch)
384 return false;
385
386 if (osabi != other.osabi)
387 return false;
388
3eea796c 389 if (features.size () != other.features.size ())
27d41eac
YQ
390 return false;
391
3eea796c 392 for (int ix = 0; ix < features.size (); ix++)
27d41eac 393 {
3eea796c
SM
394 const tdesc_feature_up &feature1 = features[ix];
395 const tdesc_feature_up &feature2 = other.features[ix];
27d41eac 396
3eea796c 397 if (feature1 != feature2 && *feature1 != *feature2)
27d41eac
YQ
398 return false;
399 }
400
401 return true;
402 }
403
404 bool operator!= (const target_desc &other) const
405 {
406 return !(*this == other);
407 }
123dc839
DJ
408};
409
410/* Per-architecture data associated with a target description. The
411 target description may be shared by multiple architectures, but
412 this data is private to one gdbarch. */
413
f0cddbef 414struct tdesc_arch_reg
ad068eab 415{
f0cddbef
SM
416 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
417 : reg (reg_), type (type_)
418 {}
419
ad068eab
UW
420 struct tdesc_reg *reg;
421 struct type *type;
f0cddbef 422};
ad068eab 423
123dc839
DJ
424struct tdesc_arch_data
425{
ad068eab 426 /* A list of register/type pairs, indexed by GDB's internal register number.
123dc839
DJ
427 During initialization of the gdbarch this list is used to store
428 registers which the architecture assigns a fixed register number.
429 Registers which are NULL in this array, or off the end, are
430 treated as zero-sized and nameless (i.e. placeholders in the
431 numbering). */
f0cddbef 432 std::vector<tdesc_arch_reg> arch_regs;
123dc839
DJ
433
434 /* Functions which report the register name, type, and reggroups for
435 pseudo-registers. */
f0cddbef
SM
436 gdbarch_register_name_ftype *pseudo_register_name = NULL;
437 gdbarch_register_type_ftype *pseudo_register_type = NULL;
438 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
424163ea
DJ
439};
440
123dc839
DJ
441/* A handle for architecture-specific data associated with the
442 target description (see struct tdesc_arch_data). */
443
cb275538
TT
444static const registry<gdbarch>::key<tdesc_arch_data> tdesc_data;
445
446/* Get or create the tdesc_data. */
447static tdesc_arch_data *
448get_arch_data (struct gdbarch *gdbarch)
449{
450 tdesc_arch_data *result = tdesc_data.get (gdbarch);
451 if (result == nullptr)
452 result = tdesc_data.emplace (gdbarch);
453 return result;
454}
123dc839 455
6ecd4729
PA
456/* The string manipulated by the "set tdesc filename ..." command. */
457
e0700ba4 458static std::string tdesc_filename_cmd_string;
6ecd4729 459
424163ea
DJ
460/* Fetch the current target's description, and switch the current
461 architecture to one which incorporates that description. */
462
463void
464target_find_description (void)
465{
6b0b81b9 466 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
c2962e6a 467
424163ea
DJ
468 /* If we've already fetched a description from the target, don't do
469 it again. This allows a target to fetch the description early,
470 during its to_open or to_create_inferior, if it needs extra
471 information about the target to initialize. */
c2962e6a 472 if (tdesc_info->fetched)
424163ea
DJ
473 return;
474
475 /* The current architecture should not have any target description
476 specified. It should have been cleared, e.g. when we
477 disconnected from the previous target. */
99d9c3b9 478 gdb_assert (gdbarch_target_desc (current_inferior ()->arch ()) == NULL);
424163ea 479
23181151
DJ
480 /* First try to fetch an XML description from the user-specified
481 file. */
c2962e6a 482 tdesc_info->tdesc = nullptr;
91e3c425
SM
483 if (!tdesc_info->filename.empty ())
484 tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
23181151
DJ
485
486 /* Next try to read the description from the current target using
487 target objects. */
c2962e6a
SM
488 if (tdesc_info->tdesc == nullptr)
489 tdesc_info->tdesc = target_read_description_xml
328d42d8 490 (current_inferior ()->top_target ());
23181151
DJ
491
492 /* If that failed try a target-specific hook. */
c2962e6a
SM
493 if (tdesc_info->tdesc == nullptr)
494 tdesc_info->tdesc = target_read_description
328d42d8 495 (current_inferior ()->top_target ());
424163ea
DJ
496
497 /* If a non-NULL description was returned, then update the current
498 architecture. */
c2962e6a 499 if (tdesc_info->tdesc != nullptr)
424163ea
DJ
500 {
501 struct gdbarch_info info;
502
c2962e6a 503 info.target_desc = tdesc_info->tdesc;
91f378dd 504 if (!gdbarch_update_p (current_inferior (), info))
81b86ece
TT
505 {
506 warning (_("Architecture rejected target-supplied description"));
507 tdesc_info->tdesc = nullptr;
508 }
123dc839
DJ
509 else
510 {
511 struct tdesc_arch_data *data;
512
99d9c3b9 513 data = get_arch_data (current_inferior ()->arch ());
c2962e6a 514 if (tdesc_has_registers (tdesc_info->tdesc)
f0cddbef 515 && data->arch_regs.empty ())
123dc839
DJ
516 warning (_("Target-supplied registers are not supported "
517 "by the current architecture"));
518 }
424163ea
DJ
519 }
520
521 /* Now that we know this description is usable, record that we
522 fetched it. */
820c4490 523 tdesc_info->fetched = true;
424163ea
DJ
524}
525
526/* Discard any description fetched from the current target, and switch
527 the current architecture to one with no target description. */
528
529void
530target_clear_description (void)
531{
6b0b81b9 532 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
424163ea 533
c2962e6a 534 if (!tdesc_info->fetched)
424163ea
DJ
535 return;
536
820c4490 537 tdesc_info->fetched = false;
c2962e6a 538 tdesc_info->tdesc = nullptr;
424163ea 539
c2962e6a 540 gdbarch_info info;
91f378dd 541 if (!gdbarch_update_p (current_inferior (), info))
f34652de 542 internal_error (_("Could not remove target-supplied description"));
424163ea
DJ
543}
544
74e39223 545/* See target-descriptions.h. */
424163ea 546
74e39223
SM
547const target_desc *
548target_current_description (inferior *inf)
424163ea 549{
74e39223 550 target_desc_info *tdesc_info = &inf->tdesc_info;
c2962e6a
SM
551
552 if (tdesc_info->fetched)
553 return tdesc_info->tdesc;
424163ea
DJ
554
555 return NULL;
556}
e35359c5
UW
557
558/* Return non-zero if this target description is compatible
559 with the given BFD architecture. */
560
561int
562tdesc_compatible_p (const struct target_desc *target_desc,
563 const struct bfd_arch_info *arch)
564{
fbf42f4e 565 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
e35359c5 566 {
fbf42f4e
AB
567 if (compat->arch () == arch
568 || arch->compatible (arch, compat->arch ())
569 || compat->arch ()->compatible (compat->arch (), arch))
e35359c5
UW
570 return 1;
571 }
572
573 return 0;
574}
23181151
DJ
575\f
576
123dc839 577/* Direct accessors for target descriptions. */
424163ea 578
29709017
DJ
579/* Return the string value of a property named KEY, or NULL if the
580 property was not specified. */
581
582const char *
583tdesc_property (const struct target_desc *target_desc, const char *key)
584{
129c10bc
SM
585 for (const property &prop : target_desc->properties)
586 if (prop.key == key)
587 return prop.value.c_str ();
29709017
DJ
588
589 return NULL;
590}
591
23181151
DJ
592/* Return the BFD architecture associated with this target
593 description, or NULL if no architecture was specified. */
594
595const struct bfd_arch_info *
596tdesc_architecture (const struct target_desc *target_desc)
597{
598 return target_desc->arch;
599}
08d16641 600
268a13a5 601/* See gdbsupport/tdesc.h. */
d278f585
AH
602
603const char *
604tdesc_architecture_name (const struct target_desc *target_desc)
605{
caa7fd04
AB
606 if (target_desc->arch != NULL)
607 return target_desc->arch->printable_name;
608 return NULL;
d278f585
AH
609}
610
fbf42f4e
AB
611/* See gdbsupport/tdesc.h. */
612
613const std::vector<tdesc_compatible_info_up> &
614tdesc_compatible_info_list (const target_desc *target_desc)
615{
616 return target_desc->compatible;
617}
618
619/* See gdbsupport/tdesc.h. */
620
621const char *
622tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
623{
624 return compatible->arch ()->printable_name;
625}
626
08d16641
PA
627/* Return the OSABI associated with this target description, or
628 GDB_OSABI_UNKNOWN if no osabi was specified. */
629
630enum gdb_osabi
631tdesc_osabi (const struct target_desc *target_desc)
632{
633 return target_desc->osabi;
634}
635
268a13a5 636/* See gdbsupport/tdesc.h. */
d278f585
AH
637
638const char *
639tdesc_osabi_name (const struct target_desc *target_desc)
640{
641 enum gdb_osabi osabi = tdesc_osabi (target_desc);
642 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
643 return gdbarch_osabi_name (osabi);
644 return nullptr;
645}
23181151 646
123dc839
DJ
647/* Return 1 if this target description includes any registers. */
648
649int
650tdesc_has_registers (const struct target_desc *target_desc)
651{
123dc839
DJ
652 if (target_desc == NULL)
653 return 0;
654
3eea796c 655 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 656 if (!feature->registers.empty ())
123dc839
DJ
657 return 1;
658
659 return 0;
660}
661
662/* Return the feature with the given name, if present, or NULL if
663 the named feature is not found. */
664
665const struct tdesc_feature *
666tdesc_find_feature (const struct target_desc *target_desc,
667 const char *name)
668{
3eea796c 669 for (const tdesc_feature_up &feature : target_desc->features)
f65ff9f9 670 if (feature->name == name)
3eea796c 671 return feature.get ();
123dc839
DJ
672
673 return NULL;
674}
675
676/* Return the name of FEATURE. */
677
678const char *
679tdesc_feature_name (const struct tdesc_feature *feature)
680{
f65ff9f9 681 return feature->name.c_str ();
123dc839
DJ
682}
683
9fd3625f
L
684/* Lookup type associated with ID. */
685
686struct type *
687tdesc_find_type (struct gdbarch *gdbarch, const char *id)
688{
cb275538 689 tdesc_arch_data *data = get_arch_data (gdbarch);
9fd3625f 690
f0cddbef 691 for (const tdesc_arch_reg &reg : data->arch_regs)
9fd3625f 692 {
f0cddbef
SM
693 if (reg.reg
694 && reg.reg->tdesc_type
695 && reg.type
696 && reg.reg->tdesc_type->name == id)
697 return reg.type;
9fd3625f
L
698 }
699
700 return NULL;
701}
702
123dc839
DJ
703/* Support for registers from target descriptions. */
704
705/* Construct the per-gdbarch data. */
706
c1e1314d 707tdesc_arch_data_up
123dc839
DJ
708tdesc_data_alloc (void)
709{
c1e1314d 710 return tdesc_arch_data_up (new tdesc_arch_data ());
123dc839
DJ
711}
712
c1e1314d 713/* See target-descriptions.h. */
123dc839
DJ
714
715void
c1e1314d 716tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
123dc839 717{
f0cddbef 718 delete data;
123dc839
DJ
719}
720
721/* Search FEATURE for a register named NAME. */
722
7cc46491
DJ
723static struct tdesc_reg *
724tdesc_find_register_early (const struct tdesc_feature *feature,
725 const char *name)
123dc839 726{
c9c895b9 727 for (const tdesc_reg_up &reg : feature->registers)
a8142ee1 728 if (strcasecmp (reg->name.c_str (), name) == 0)
c9c895b9 729 return reg.get ();
123dc839 730
7cc46491
DJ
731 return NULL;
732}
733
734/* Search FEATURE for a register named NAME. Assign REGNO to it. */
735
736int
737tdesc_numbered_register (const struct tdesc_feature *feature,
738 struct tdesc_arch_data *data,
739 int regno, const char *name)
740{
741 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
742
743 if (reg == NULL)
744 return 0;
745
746 /* Make sure the vector includes a REGNO'th element. */
f0cddbef
SM
747 while (regno >= data->arch_regs.size ())
748 data->arch_regs.emplace_back (nullptr, nullptr);
749
750 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
ad068eab 751
7cc46491 752 return 1;
123dc839
DJ
753}
754
58d6951d
DJ
755/* Search FEATURE for a register named NAME, but do not assign a fixed
756 register number to it. */
757
758int
759tdesc_unnumbered_register (const struct tdesc_feature *feature,
760 const char *name)
761{
762 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
763
764 if (reg == NULL)
765 return 0;
766
767 return 1;
768}
769
7cc46491
DJ
770/* Search FEATURE for a register whose name is in NAMES and assign
771 REGNO to it. */
123dc839
DJ
772
773int
774tdesc_numbered_register_choices (const struct tdesc_feature *feature,
775 struct tdesc_arch_data *data,
776 int regno, const char *const names[])
777{
778 int i;
779
780 for (i = 0; names[i] != NULL; i++)
781 if (tdesc_numbered_register (feature, data, regno, names[i]))
782 return 1;
783
784 return 0;
785}
786
b49d7aa7
AB
787/* See target-descriptions.h. */
788
789bool
790tdesc_found_register (struct tdesc_arch_data *data, int regno)
791{
792 gdb_assert (regno >= 0);
793
794 return (regno < data->arch_regs.size ()
795 && data->arch_regs[regno].reg != nullptr);
796}
797
7cc46491
DJ
798/* Search FEATURE for a register named NAME, and return its size in
799 bits. The register must exist. */
800
801int
12863263 802tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
7cc46491
DJ
803{
804 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
805
806 gdb_assert (reg != NULL);
807 return reg->bitsize;
808}
809
123dc839
DJ
810/* Look up a register by its GDB internal register number. */
811
ad068eab
UW
812static struct tdesc_arch_reg *
813tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
123dc839 814{
cb275538 815 struct tdesc_arch_data *data = get_arch_data (gdbarch);
123dc839 816
f0cddbef
SM
817 if (regno < data->arch_regs.size ())
818 return &data->arch_regs[regno];
123dc839
DJ
819 else
820 return NULL;
821}
822
ad068eab
UW
823static struct tdesc_reg *
824tdesc_find_register (struct gdbarch *gdbarch, int regno)
825{
826 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
5d502164 827
ad068eab
UW
828 return reg? reg->reg : NULL;
829}
830
f8b73d13
DJ
831/* Return the name of register REGNO, from the target description or
832 from an architecture-provided pseudo_register_name method. */
833
834const char *
d93859e2 835tdesc_register_name (struct gdbarch *gdbarch, int regno)
123dc839 836{
d93859e2
UW
837 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
838 int num_regs = gdbarch_num_regs (gdbarch);
123dc839
DJ
839
840 if (reg != NULL)
a8142ee1 841 return reg->name.c_str ();
123dc839 842
f6efe3f8 843 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
123dc839 844 {
cb275538 845 struct tdesc_arch_data *data = get_arch_data (gdbarch);
5d502164 846
123dc839 847 gdb_assert (data->pseudo_register_name != NULL);
d93859e2 848 return data->pseudo_register_name (gdbarch, regno);
123dc839
DJ
849 }
850
851 return "";
852}
853
58d6951d 854struct type *
123dc839
DJ
855tdesc_register_type (struct gdbarch *gdbarch, int regno)
856{
ad068eab
UW
857 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
858 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
123dc839
DJ
859 int num_regs = gdbarch_num_regs (gdbarch);
860 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
861
862 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
863 {
cb275538 864 struct tdesc_arch_data *data = get_arch_data (gdbarch);
5d502164 865
123dc839
DJ
866 gdb_assert (data->pseudo_register_type != NULL);
867 return data->pseudo_register_type (gdbarch, regno);
868 }
869
870 if (reg == NULL)
871 /* Return "int0_t", since "void" has a misleading size of one. */
df4df182 872 return builtin_type (gdbarch)->builtin_int0;
123dc839 873
ad068eab 874 if (arch_reg->type == NULL)
123dc839 875 {
ad068eab
UW
876 /* First check for a predefined or target defined type. */
877 if (reg->tdesc_type)
b8df6ca7 878 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
ad068eab
UW
879
880 /* Next try size-sensitive type shortcuts. */
a8142ee1 881 else if (reg->type == "float")
ad068eab
UW
882 {
883 if (reg->bitsize == gdbarch_float_bit (gdbarch))
884 arch_reg->type = builtin_type (gdbarch)->builtin_float;
885 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
886 arch_reg->type = builtin_type (gdbarch)->builtin_double;
887 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
888 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
889 else
890 {
891 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
a8142ee1 892 reg->name.c_str (), reg->bitsize);
ad068eab
UW
893 arch_reg->type = builtin_type (gdbarch)->builtin_double;
894 }
895 }
a8142ee1 896 else if (reg->type == "int")
ad068eab
UW
897 {
898 if (reg->bitsize == gdbarch_long_bit (gdbarch))
899 arch_reg->type = builtin_type (gdbarch)->builtin_long;
900 else if (reg->bitsize == TARGET_CHAR_BIT)
901 arch_reg->type = builtin_type (gdbarch)->builtin_char;
902 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
903 arch_reg->type = builtin_type (gdbarch)->builtin_short;
904 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
905 arch_reg->type = builtin_type (gdbarch)->builtin_int;
906 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
907 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
908 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
c378eb4e 909 /* A bit desperate by this point... */
ad068eab
UW
910 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
911 else
912 {
913 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
a8142ee1 914 reg->name.c_str (), reg->bitsize);
ad068eab
UW
915 arch_reg->type = builtin_type (gdbarch)->builtin_long;
916 }
917 }
918
919 if (arch_reg->type == NULL)
f34652de 920 internal_error ("Register \"%s\" has an unknown type \"%s\"",
a8142ee1 921 reg->name.c_str (), reg->type.c_str ());
123dc839 922 }
123dc839 923
ad068eab 924 return arch_reg->type;
123dc839
DJ
925}
926
927static int
928tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
929{
930 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
931
932 if (reg != NULL)
933 return reg->target_regnum;
934 else
935 return -1;
936}
937
938/* Check whether REGNUM is a member of REGGROUP. Registers from the
cef0f868
SH
939 target description may be classified as general, float, vector or other
940 register groups registered with reggroup_add(). Unlike a gdbarch
941 register_reggroup_p method, this function will return -1 if it does not
942 know; the caller should handle registers with no specified group.
943
944 The names of containing features are not used. This might be extended
945 to display registers in some more useful groupings.
123dc839
DJ
946
947 The save-restore flag is also implemented here. */
948
f8b73d13
DJ
949int
950tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
dbf5d61b 951 const struct reggroup *reggroup)
123dc839 952{
123dc839
DJ
953 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
954
440cf44e 955 if (reg != NULL && !reg->group.empty ()
af7ce09b 956 && (reg->group == reggroup->name ()))
cef0f868 957 return 1;
123dc839 958
440cf44e
LM
959 if (reg != NULL
960 && (reggroup == save_reggroup || reggroup == restore_reggroup))
961 return reg->save_restore;
123dc839 962
f8b73d13
DJ
963 return -1;
964}
965
966/* Check whether REGNUM is a member of REGGROUP. Registers with no
967 group specified go to the default reggroup function and are handled
968 by type. */
969
970static int
971tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
dbf5d61b 972 const struct reggroup *reggroup)
f8b73d13
DJ
973{
974 int num_regs = gdbarch_num_regs (gdbarch);
975 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
976 int ret;
977
978 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
979 {
cb275538 980 struct tdesc_arch_data *data = get_arch_data (gdbarch);
5d502164 981
58d6951d
DJ
982 if (data->pseudo_register_reggroup_p != NULL)
983 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
984 /* Otherwise fall through to the default reggroup_p. */
f8b73d13
DJ
985 }
986
987 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
988 if (ret != -1)
989 return ret;
990
123dc839
DJ
991 return default_register_reggroup_p (gdbarch, regno, reggroup);
992}
993
994/* Record architecture-specific functions to call for pseudo-register
995 support. */
996
997void
998set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
999 gdbarch_register_name_ftype *pseudo_name)
1000{
cb275538 1001 struct tdesc_arch_data *data = get_arch_data (gdbarch);
123dc839
DJ
1002
1003 data->pseudo_register_name = pseudo_name;
1004}
1005
1006void
1007set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1008 gdbarch_register_type_ftype *pseudo_type)
1009{
cb275538 1010 struct tdesc_arch_data *data = get_arch_data (gdbarch);
123dc839
DJ
1011
1012 data->pseudo_register_type = pseudo_type;
1013}
1014
1015void
1016set_tdesc_pseudo_register_reggroup_p
1017 (struct gdbarch *gdbarch,
1018 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1019{
cb275538 1020 struct tdesc_arch_data *data = get_arch_data (gdbarch);
123dc839
DJ
1021
1022 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1023}
1024
1025/* Update GDBARCH to use the target description for registers. */
1026
1027void
1028tdesc_use_registers (struct gdbarch *gdbarch,
7cc46491 1029 const struct target_desc *target_desc,
c1e1314d 1030 tdesc_arch_data_up &&early_data,
be64fd07 1031 tdesc_unknown_register_ftype unk_reg_cb)
123dc839
DJ
1032{
1033 int num_regs = gdbarch_num_regs (gdbarch);
123dc839 1034 struct tdesc_arch_data *data;
123dc839 1035
123dc839
DJ
1036 /* We can't use the description for registers if it doesn't describe
1037 any. This function should only be called after validating
1038 registers, so the caller should know that registers are
1039 included. */
1040 gdb_assert (tdesc_has_registers (target_desc));
1041
cb275538 1042 data = get_arch_data (gdbarch);
c1e1314d 1043 data->arch_regs = std::move (early_data->arch_regs);
123dc839
DJ
1044
1045 /* Build up a set of all registers, so that we can assign register
d64c62fd
SM
1046 numbers where needed. */
1047 gdb::unordered_set<tdesc_reg *> reg_hash;
1048
3eea796c 1049 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 1050 for (const tdesc_reg_up &reg : feature->registers)
123dc839 1051 {
d64c62fd 1052 reg_hash.insert (reg.get ());
123dc839 1053
cef0f868
SH
1054 /* Add reggroup if its new. */
1055 if (!reg->group.empty ())
1056 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1057 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1058 reg->group.c_str (),
1059 USER_REGGROUP));
123dc839
DJ
1060 }
1061
1062 /* Remove any registers which were assigned numbers by the
1063 architecture. */
f0cddbef
SM
1064 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1065 if (arch_reg.reg != NULL)
d64c62fd 1066 reg_hash.erase (arch_reg.reg);
123dc839
DJ
1067
1068 /* Assign numbers to the remaining registers and add them to the
f57d151a 1069 list of registers. The new numbers are always above gdbarch_num_regs.
123dc839
DJ
1070 Iterate over the features, not the hash table, so that the order
1071 matches that in the target description. */
1072
f0cddbef
SM
1073 gdb_assert (data->arch_regs.size () <= num_regs);
1074 while (data->arch_regs.size () < num_regs)
1075 data->arch_regs.emplace_back (nullptr, nullptr);
1076
be64fd07
AB
1077 /* First we give the target a chance to number previously unknown
1078 registers. This allows targets to record the numbers assigned based
1079 on which feature the register was from. */
1080 if (unk_reg_cb != NULL)
1081 {
1082 for (const tdesc_feature_up &feature : target_desc->features)
1083 for (const tdesc_reg_up &reg : feature->registers)
d64c62fd 1084 if (reg_hash.contains (reg.get ()))
be64fd07
AB
1085 {
1086 int regno = unk_reg_cb (gdbarch, feature.get (),
1087 reg->name.c_str (), num_regs);
1088 gdb_assert (regno == -1 || regno >= num_regs);
1089 if (regno != -1)
1090 {
1091 while (regno >= data->arch_regs.size ())
1092 data->arch_regs.emplace_back (nullptr, nullptr);
1093 data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
1094 num_regs = regno + 1;
d64c62fd 1095 reg_hash.erase (reg.get ());
be64fd07
AB
1096 }
1097 }
1098 }
1099
1100 /* Ensure the array was sized correctly above. */
1101 gdb_assert (data->arch_regs.size () == num_regs);
1102
1103 /* Now in a final pass we assign register numbers to any remaining
1104 unnumbered registers. */
3eea796c 1105 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 1106 for (const tdesc_reg_up &reg : feature->registers)
d64c62fd 1107 if (reg_hash.contains (reg.get ()))
123dc839 1108 {
f0cddbef 1109 data->arch_regs.emplace_back (reg.get (), nullptr);
123dc839
DJ
1110 num_regs++;
1111 }
1112
123dc839
DJ
1113 /* Update the architecture. */
1114 set_gdbarch_num_regs (gdbarch, num_regs);
1115 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1116 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1117 set_gdbarch_remote_register_number (gdbarch,
1118 tdesc_remote_register_number);
1119 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1120}
123dc839 1121
268a13a5 1122/* See gdbsupport/tdesc.h. */
f49ff000 1123
123dc839 1124struct tdesc_feature *
3b74854b 1125tdesc_create_feature (struct target_desc *tdesc, const char *name)
123dc839 1126{
72ddacb7 1127 struct tdesc_feature *new_feature = new tdesc_feature (name);
123dc839 1128
3eea796c
SM
1129 tdesc->features.emplace_back (new_feature);
1130
123dc839
DJ
1131 return new_feature;
1132}
1133
0e267416
AB
1134/* See gdbsupport/tdesc.h. */
1135
51a948fd 1136target_desc_up
424163ea
DJ
1137allocate_target_description (void)
1138{
51a948fd 1139 return target_desc_up (new target_desc ());
424163ea 1140}
29709017 1141
0e267416
AB
1142/* See gdbsupport/tdesc.h. */
1143
c55d06ec
TT
1144void
1145target_desc_deleter::operator() (struct target_desc *target_desc) const
23181151 1146{
b468ff4c 1147 delete target_desc;
23181151
DJ
1148}
1149
e35359c5
UW
1150void
1151tdesc_add_compatible (struct target_desc *target_desc,
1152 const struct bfd_arch_info *compatible)
1153{
e35359c5
UW
1154 /* If this instance of GDB is compiled without BFD support for the
1155 compatible architecture, simply ignore it -- we would not be able
1156 to handle it anyway. */
1157 if (compatible == NULL)
1158 return;
1159
fbf42f4e
AB
1160 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
1161 if (compat->arch () == compatible)
f34652de 1162 internal_error (_("Attempted to add duplicate "
e35359c5
UW
1163 "compatible architecture \"%s\""),
1164 compatible->printable_name);
1165
fbf42f4e
AB
1166 target_desc->compatible.push_back
1167 (std::unique_ptr<tdesc_compatible_info>
1168 (new tdesc_compatible_info (compatible)));
e35359c5
UW
1169}
1170
29709017
DJ
1171void
1172set_tdesc_property (struct target_desc *target_desc,
1173 const char *key, const char *value)
1174{
29709017
DJ
1175 gdb_assert (key != NULL && value != NULL);
1176
129c10bc 1177 if (tdesc_property (target_desc, key) != NULL)
f34652de 1178 internal_error (_("Attempted to add duplicate property \"%s\""), key);
29709017 1179
129c10bc 1180 target_desc->properties.emplace_back (key, value);
29709017 1181}
23181151 1182
268a13a5 1183/* See gdbsupport/tdesc.h. */
5f035c07
YQ
1184
1185void
1186set_tdesc_architecture (struct target_desc *target_desc,
1187 const char *name)
1188{
1189 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1190}
1191
23181151
DJ
1192void
1193set_tdesc_architecture (struct target_desc *target_desc,
1194 const struct bfd_arch_info *arch)
1195{
1196 target_desc->arch = arch;
1197}
08d16641 1198
268a13a5 1199/* See gdbsupport/tdesc.h. */
5f035c07 1200
08d16641
PA
1201void
1202set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1203{
1204 target_desc->osabi = osabi;
1205}
23181151
DJ
1206\f
1207
1208static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1209static struct cmd_list_element *tdesc_unset_cmdlist;
1210
1211/* Helper functions for the CLI commands. */
1212
23181151 1213static void
eb4c3f4a 1214set_tdesc_filename_cmd (const char *args, int from_tty,
23181151
DJ
1215 struct cmd_list_element *c)
1216{
6b0b81b9 1217 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
c2962e6a 1218
91e3c425 1219 tdesc_info->filename = tdesc_filename_cmd_string;
6ecd4729 1220
23181151
DJ
1221 target_clear_description ();
1222 target_find_description ();
1223}
1224
1225static void
1226show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1227 struct cmd_list_element *c,
1228 const char *value)
1229{
6b0b81b9 1230 value = current_inferior ()->tdesc_info.filename.data ();
6ecd4729 1231
23181151 1232 if (value != NULL && *value != '\0')
6cb06a8c 1233 gdb_printf (file,
fc0a39f4
TT
1234 _("The target description will be read from \"%ps\".\n"),
1235 styled_string (file_name_style.style (), value));
23181151 1236 else
6cb06a8c
TT
1237 gdb_printf (file,
1238 _("The target description will be "
1239 "read from the target.\n"));
23181151
DJ
1240}
1241
1242static void
e100df1a 1243unset_tdesc_filename_cmd (const char *args, int from_tty)
23181151 1244{
6b0b81b9 1245 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
c2962e6a 1246
91e3c425 1247 tdesc_info->filename.clear ();
23181151
DJ
1248 target_clear_description ();
1249 target_find_description ();
1250}
1251
6eb1e6a8
YQ
1252/* Print target description in C. */
1253
1254class print_c_tdesc : public tdesc_element_visitor
1255{
1256public:
1257 print_c_tdesc (std::string &filename_after_features)
1258 : m_filename_after_features (filename_after_features)
1259 {
1260 const char *inp;
1261 char *outp;
1262 const char *filename = lbasename (m_filename_after_features.c_str ());
1263
1264 m_function = (char *) xmalloc (strlen (filename) + 1);
1265 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1266 if (*inp == '.')
1267 break;
1268 else if (*inp == '-')
1269 *outp++ = '_';
20821f4e
AB
1270 else if (*inp == ' ')
1271 *outp++ = '_';
6eb1e6a8
YQ
1272 else
1273 *outp++ = *inp;
1274 *outp = '\0';
1275
1276 /* Standard boilerplate. */
6cb06a8c
TT
1277 gdb_printf ("/* THIS FILE IS GENERATED. "
1278 "-*- buffer-read-only: t -*- vi"
1279 ":set ro:\n");
6eb1e6a8
YQ
1280 }
1281
1282 ~print_c_tdesc ()
1283 {
1284 xfree (m_function);
1285 }
1286
1287 void visit_pre (const target_desc *e) override
1288 {
6cb06a8c
TT
1289 gdb_printf (" Original: %s */\n\n",
1290 lbasename (m_filename_after_features.c_str ()));
1291
6cb06a8c
TT
1292 gdb_printf ("#include \"osabi.h\"\n");
1293 gdb_printf ("#include \"target-descriptions.h\"\n");
1294 gdb_printf ("\n");
1295
ac9b8c67 1296 gdb_printf ("const struct target_desc *tdesc_%s;\n", m_function);
6cb06a8c
TT
1297 gdb_printf ("static void\n");
1298 gdb_printf ("initialize_tdesc_%s (void)\n", m_function);
1299 gdb_printf ("{\n");
1300 gdb_printf
51a948fd 1301 (" target_desc_up result = allocate_target_description ();\n");
6eb1e6a8
YQ
1302
1303 if (tdesc_architecture (e) != NULL)
1304 {
6cb06a8c 1305 gdb_printf
51a948fd 1306 (" set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
6eb1e6a8 1307 tdesc_architecture (e)->printable_name);
6cb06a8c 1308 gdb_printf ("\n");
6eb1e6a8
YQ
1309 }
1310 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1311 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1312 {
d2f8a107
AB
1313 const char *enum_name = gdbarch_osabi_enum_name (tdesc_osabi (e));
1314 gdb_printf (" set_tdesc_osabi (result.get (), %s);\n", enum_name);
6cb06a8c 1315 gdb_printf ("\n");
6eb1e6a8
YQ
1316 }
1317
fbf42f4e 1318 for (const tdesc_compatible_info_up &compatible : e->compatible)
6cb06a8c 1319 gdb_printf
51a948fd 1320 (" tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
fbf42f4e 1321 compatible->arch ()->printable_name);
6eb1e6a8 1322
40e2a983 1323 if (!e->compatible.empty ())
6cb06a8c 1324 gdb_printf ("\n");
6eb1e6a8 1325
129c10bc 1326 for (const property &prop : e->properties)
6cb06a8c
TT
1327 gdb_printf (" set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
1328 prop.key.c_str (), prop.value.c_str ());
129c10bc 1329
6cb06a8c 1330 gdb_printf (" struct tdesc_feature *feature;\n");
6eb1e6a8
YQ
1331 }
1332
25aa13e5 1333 void visit_pre (const tdesc_feature *e) override
6eb1e6a8 1334 {
6cb06a8c
TT
1335 gdb_printf ("\n feature = tdesc_create_feature (result.get (), \"%s\");\n",
1336 e->name.c_str ());
6eb1e6a8
YQ
1337 }
1338
25aa13e5
YQ
1339 void visit_post (const tdesc_feature *e) override
1340 {}
1341
6eb1e6a8
YQ
1342 void visit_post (const target_desc *e) override
1343 {
6cb06a8c
TT
1344 gdb_printf ("\n tdesc_%s = result.release ();\n", m_function);
1345 gdb_printf ("}\n");
6eb1e6a8
YQ
1346 }
1347
d4a0e8b5
SM
1348 void visit (const tdesc_type_builtin *type) override
1349 {
1350 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1351 }
1352
1353 void visit (const tdesc_type_vector *type) override
6eb1e6a8 1354 {
d4a0e8b5 1355 if (!m_printed_element_type)
6eb1e6a8 1356 {
6cb06a8c 1357 gdb_printf (" tdesc_type *element_type;\n");
d4a0e8b5 1358 m_printed_element_type = true;
6eb1e6a8
YQ
1359 }
1360
6cb06a8c 1361 gdb_printf
d4a0e8b5
SM
1362 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1363 type->element_type->name.c_str ());
6cb06a8c 1364 gdb_printf
d4a0e8b5
SM
1365 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1366 type->name.c_str (), type->count);
1367
6cb06a8c 1368 gdb_printf ("\n");
d4a0e8b5
SM
1369 }
1370
1371 void visit (const tdesc_type_with_fields *type) override
1372 {
1373 if (!m_printed_type_with_fields)
6eb1e6a8 1374 {
6cb06a8c 1375 gdb_printf (" tdesc_type_with_fields *type_with_fields;\n");
d4a0e8b5
SM
1376 m_printed_type_with_fields = true;
1377 }
1378
6eb1e6a8
YQ
1379 switch (type->kind)
1380 {
6eb1e6a8
YQ
1381 case TDESC_TYPE_STRUCT:
1382 case TDESC_TYPE_FLAGS:
1383 if (type->kind == TDESC_TYPE_STRUCT)
1384 {
6cb06a8c 1385 gdb_printf
d4a0e8b5 1386 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
082b9140 1387 type->name.c_str ());
d4a0e8b5 1388 if (type->size != 0)
6cb06a8c 1389 gdb_printf
d4a0e8b5 1390 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
6eb1e6a8
YQ
1391 }
1392 else
1393 {
6cb06a8c 1394 gdb_printf
d4a0e8b5
SM
1395 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1396 type->name.c_str (), type->size);
6eb1e6a8 1397 }
d4a0e8b5 1398 for (const tdesc_type_field &f : type->fields)
6eb1e6a8
YQ
1399 {
1400 const char *type_name;
1401
d05200d1
SM
1402 gdb_assert (f.type != NULL);
1403 type_name = f.type->name.c_str ();
6eb1e6a8
YQ
1404
1405 /* To minimize changes to generated files, don't emit type
1406 info for fields that have defaulted types. */
d05200d1 1407 if (f.start != -1)
6eb1e6a8 1408 {
d05200d1
SM
1409 gdb_assert (f.end != -1);
1410 if (f.type->kind == TDESC_TYPE_BOOL)
6eb1e6a8 1411 {
d05200d1 1412 gdb_assert (f.start == f.end);
6cb06a8c 1413 gdb_printf
d4a0e8b5 1414 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
d05200d1 1415 f.start, f.name.c_str ());
6eb1e6a8 1416 }
d4a0e8b5
SM
1417 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1418 || (type->size == 8
d05200d1 1419 && f.type->kind == TDESC_TYPE_UINT64))
6eb1e6a8 1420 {
6cb06a8c 1421 gdb_printf
d4a0e8b5 1422 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
d05200d1 1423 f.name.c_str (), f.start, f.end);
6eb1e6a8
YQ
1424 }
1425 else
1426 {
a8d2e585
SM
1427 printf_field_type_assignment
1428 ("tdesc_named_type (feature, \"%s\");\n",
6eb1e6a8 1429 type_name);
6cb06a8c 1430 gdb_printf
d4a0e8b5 1431 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
6eb1e6a8 1432 " %d, %d, field_type);\n",
d05200d1 1433 f.name.c_str (), f.start, f.end);
6eb1e6a8
YQ
1434 }
1435 }
1436 else /* Not a bitfield. */
1437 {
d05200d1 1438 gdb_assert (f.end == -1);
6eb1e6a8 1439 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
a8d2e585
SM
1440 printf_field_type_assignment
1441 ("tdesc_named_type (feature, \"%s\");\n", type_name);
6cb06a8c 1442 gdb_printf
d4a0e8b5 1443 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
d05200d1 1444 f.name.c_str ());
6eb1e6a8
YQ
1445 }
1446 }
1447 break;
1448 case TDESC_TYPE_UNION:
6cb06a8c 1449 gdb_printf
d4a0e8b5 1450 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
082b9140 1451 type->name.c_str ());
d4a0e8b5 1452 for (const tdesc_type_field &f : type->fields)
6eb1e6a8 1453 {
a8d2e585
SM
1454 printf_field_type_assignment
1455 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
6cb06a8c 1456 gdb_printf
d4a0e8b5 1457 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
d05200d1 1458 f.name.c_str ());
6eb1e6a8
YQ
1459 }
1460 break;
1461 case TDESC_TYPE_ENUM:
6cb06a8c 1462 gdb_printf
d4a0e8b5
SM
1463 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1464 type->name.c_str (), type->size);
1465 for (const tdesc_type_field &f : type->fields)
6cb06a8c 1466 gdb_printf
d4a0e8b5 1467 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
d05200d1 1468 f.start, f.name.c_str ());
6eb1e6a8
YQ
1469 break;
1470 default:
082b9140 1471 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
6eb1e6a8 1472 }
d4a0e8b5 1473
6cb06a8c 1474 gdb_printf ("\n");
6eb1e6a8
YQ
1475 }
1476
1477 void visit (const tdesc_reg *reg) override
1478 {
6cb06a8c
TT
1479 gdb_printf (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1480 reg->name.c_str (), reg->target_regnum,
1481 reg->save_restore);
a8142ee1 1482 if (!reg->group.empty ())
6cb06a8c 1483 gdb_printf ("\"%s\", ", reg->group.c_str ());
6eb1e6a8 1484 else
6cb06a8c
TT
1485 gdb_printf ("NULL, ");
1486 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
6eb1e6a8
YQ
1487 }
1488
25aa13e5
YQ
1489protected:
1490 std::string m_filename_after_features;
1491
6eb1e6a8 1492private:
a8d2e585
SM
1493
1494 /* Print an assignment to the field_type variable. Print the declaration
1495 of field_type if that has not been done yet. */
6e8c24fe 1496 ATTRIBUTE_PRINTF (2, 3)
a8d2e585
SM
1497 void printf_field_type_assignment (const char *fmt, ...)
1498 {
1499 if (!m_printed_field_type)
1500 {
6cb06a8c 1501 gdb_printf (" tdesc_type *field_type;\n");
a8d2e585
SM
1502 m_printed_field_type = true;
1503 }
1504
6cb06a8c 1505 gdb_printf (" field_type = ");
a8d2e585
SM
1506
1507 va_list args;
1508 va_start (args, fmt);
19a7b8ab 1509 gdb_vprintf (fmt, args);
a8d2e585
SM
1510 va_end (args);
1511 }
1512
6eb1e6a8 1513 char *m_function;
d4a0e8b5
SM
1514
1515 /* Did we print "struct tdesc_type *element_type;" yet? */
1516 bool m_printed_element_type = false;
1517
1518 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1519 bool m_printed_type_with_fields = false;
1520
1521 /* Did we print "struct tdesc_type *field_type;" yet? */
6eb1e6a8 1522 bool m_printed_field_type = false;
6eb1e6a8
YQ
1523};
1524
25aa13e5
YQ
1525/* Print target description feature in C. */
1526
1527class print_c_feature : public print_c_tdesc
1528{
1529public:
1530 print_c_feature (std::string &file)
1531 : print_c_tdesc (file)
1532 {
1533 /* Trim ".tmp". */
1534 auto const pos = m_filename_after_features.find_last_of ('.');
1535
1536 m_filename_after_features = m_filename_after_features.substr (0, pos);
1537 }
1538
1539 void visit_pre (const target_desc *e) override
1540 {
6cb06a8c
TT
1541 gdb_printf (" Original: %s */\n\n",
1542 lbasename (m_filename_after_features.c_str ()));
25aa13e5 1543
6cb06a8c
TT
1544 gdb_printf ("#include \"gdbsupport/tdesc.h\"\n");
1545 gdb_printf ("\n");
25aa13e5
YQ
1546 }
1547
1548 void visit_post (const target_desc *e) override
1549 {}
1550
1551 void visit_pre (const tdesc_feature *e) override
1552 {
1553 std::string name (m_filename_after_features);
1554
1555 auto pos = name.find_first_of ('.');
1556
1557 name = name.substr (0, pos);
1558 std::replace (name.begin (), name.end (), '/', '_');
1559 std::replace (name.begin (), name.end (), '-', '_');
1560
6cb06a8c
TT
1561 gdb_printf ("static int\n");
1562 gdb_printf ("create_feature_%s ", name.c_str ());
1563 gdb_printf ("(struct target_desc *result, long regnum)\n");
25aa13e5 1564
6cb06a8c
TT
1565 gdb_printf ("{\n");
1566 gdb_printf (" struct tdesc_feature *feature;\n");
0abe8a89 1567
6cb06a8c 1568 gdb_printf
3b74854b
AH
1569 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1570 e->name.c_str ());
25aa13e5
YQ
1571 }
1572
1573 void visit_post (const tdesc_feature *e) override
1574 {
6cb06a8c
TT
1575 gdb_printf (" return regnum;\n");
1576 gdb_printf ("}\n");
25aa13e5
YQ
1577 }
1578
1579 void visit (const tdesc_reg *reg) override
1580 {
ea03d0d3
YQ
1581 /* Most "reg" in XML target descriptions don't have "regnum"
1582 attribute, so the register number is allocated sequentially.
1583 In case that reg has "regnum" attribute, register number
1584 should be set by that explicitly. */
1585
1586 if (reg->target_regnum < m_next_regnum)
1587 {
1588 /* The integrity check, it can catch some errors on register
1589 number collision, like this,
1590
1591 <reg name="x0" bitsize="32"/>
1592 <reg name="x1" bitsize="32"/>
1593 <reg name="x2" bitsize="32"/>
1594 <reg name="x3" bitsize="32"/>
1595 <reg name="ps" bitsize="32" regnum="3"/>
1596
1597 but it also has false negatives. The target description
1598 below is correct,
1599
1600 <reg name="x1" bitsize="32" regnum="1"/>
1601 <reg name="x3" bitsize="32" regnum="3"/>
1602 <reg name="x2" bitsize="32" regnum="2"/>
1603 <reg name="x4" bitsize="32" regnum="4"/>
1604
1605 but it is not a good practice, so still error on this,
1606 and also print the message so that it can be saved in the
1607 generated c file. */
1608
6cb06a8c
TT
1609 gdb_printf ("ERROR: \"regnum\" attribute %ld ",
1610 reg->target_regnum);
1611 gdb_printf ("is not the largest number (%d).\n",
1612 m_next_regnum);
ea03d0d3
YQ
1613 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1614 reg->target_regnum, m_next_regnum);
1615 }
1616
1617 if (reg->target_regnum > m_next_regnum)
1618 {
6cb06a8c 1619 gdb_printf (" regnum = %ld;\n", reg->target_regnum);
ea03d0d3
YQ
1620 m_next_regnum = reg->target_regnum;
1621 }
1622
6cb06a8c
TT
1623 gdb_printf (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1624 reg->name.c_str (), reg->save_restore);
a8142ee1 1625 if (!reg->group.empty ())
6cb06a8c 1626 gdb_printf ("\"%s\", ", reg->group.c_str ());
25aa13e5 1627 else
6cb06a8c
TT
1628 gdb_printf ("NULL, ");
1629 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
ea03d0d3
YQ
1630
1631 m_next_regnum++;
25aa13e5
YQ
1632 }
1633
ea03d0d3
YQ
1634private:
1635 /* The register number to use for the next register we see. */
1636 int m_next_regnum = 0;
25aa13e5
YQ
1637};
1638
268a13a5 1639/* See gdbsupport/tdesc.h. */
e98577a9
AH
1640
1641const char *
1642tdesc_get_features_xml (const target_desc *tdesc)
1643{
1644 if (tdesc->xmltarget == nullptr)
1645 {
1646 std::string buffer ("@");
1647 print_xml_feature v (&buffer);
1648 tdesc->accept (v);
1649 tdesc->xmltarget = xstrdup (buffer.c_str ());
1650 }
1651 return tdesc->xmltarget;
1652}
1653
ab33b152
AB
1654/* Data structures and functions to setup the option flags for 'maintenance
1655 print c-tdesc command. */
1656
1657struct maint_print_c_tdesc_options
1658{
1659 /* True when the '-single-feature' flag was passed. */
1660 bool single_feature = false;
1661};
1662
1663using maint_print_c_tdesc_opt_def
1664 = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
1665
1666static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
1667 maint_print_c_tdesc_opt_def {
1668 "single-feature",
1669 [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
1670 N_("Print C description of just a single feature.")
1671 },
1672};
1673
1674static inline gdb::option::option_def_group
1675make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
1676{
1677 return {{maint_print_c_tdesc_opt_defs}, opts};
1678}
1679
1680/* Implement 'maintenance print c-tdesc' command. */
1681
81adfced 1682static void
e100df1a 1683maint_print_c_tdesc_cmd (const char *args, int from_tty)
81adfced
DJ
1684{
1685 const struct target_desc *tdesc;
81adfced 1686
ab33b152
AB
1687 maint_print_c_tdesc_options opts;
1688 auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
1689 gdb::option::process_options
1690 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1691
e454ae41
AB
1692 std::string filename = extract_single_filename_arg (args);
1693
1694 if (filename.empty ())
8e2141c6
YQ
1695 {
1696 /* Use the global target-supplied description, not the current
1697 architecture's. This lets a GDB for one architecture generate C
1698 for another architecture's description, even though the gdbarch
1699 initialization code will reject the new description. */
6b0b81b9 1700 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
c2962e6a 1701 tdesc = tdesc_info->tdesc;
e454ae41
AB
1702 if (tdesc_info->filename.data () != nullptr)
1703 filename = std::string (tdesc_info->filename.data ());
8e2141c6
YQ
1704 }
1705 else
1706 {
1707 /* Use the target description from the XML file. */
e454ae41 1708 tdesc = file_read_description_xml (filename.c_str ());
8e2141c6
YQ
1709 }
1710
81adfced
DJ
1711 if (tdesc == NULL)
1712 error (_("There is no target description to print."));
1713
e454ae41 1714 if (filename.empty ())
20821f4e 1715 filename = "fetched from target";
81adfced 1716
e454ae41 1717 auto loc = filename.rfind ("/features/");
6eb1e6a8 1718 if (loc != std::string::npos)
e454ae41 1719 filename = filename.substr (loc + 10);
4e2f8df6 1720
25aa13e5
YQ
1721 /* Print c files for target features instead of target descriptions,
1722 because c files got from target features are more flexible than the
1723 counterparts. */
ab33b152 1724 if (opts.single_feature)
25aa13e5 1725 {
ab33b152
AB
1726 if (tdesc->features.size () != 1)
1727 error (_("only target descriptions with 1 feature can be used "
1728 "with -single-feature option"));
1729
e454ae41 1730 print_c_feature v (filename);
81adfced 1731
25aa13e5
YQ
1732 tdesc->accept (v);
1733 }
1734 else
1735 {
e454ae41 1736 print_c_tdesc v (filename);
25aa13e5
YQ
1737
1738 tdesc->accept (v);
1739 }
81adfced
DJ
1740}
1741
ab33b152
AB
1742/* Completer for the "backtrace" command. */
1743
1744static void
1745maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
1746 completion_tracker &tracker,
1747 const char *text, const char *word)
1748{
1749 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1750 if (gdb::option::complete_options
1751 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1752 return;
1753
e454ae41
AB
1754 word = advance_to_filename_maybe_quoted_complete_word_point (tracker, text);
1755 filename_maybe_quoted_completer (ignore, tracker, text, word);
ab33b152
AB
1756}
1757
caa7fd04
AB
1758/* Implement the maintenance print xml-tdesc command. */
1759
1760static void
1761maint_print_xml_tdesc_cmd (const char *args, int from_tty)
1762{
1763 const struct target_desc *tdesc;
1764
1765 if (args == NULL)
1766 {
1767 /* Use the global target-supplied description, not the current
1768 architecture's. This lets a GDB for one architecture generate XML
1769 for another architecture's description, even though the gdbarch
1770 initialization code will reject the new description. */
6b0b81b9 1771 tdesc = current_inferior ()->tdesc_info.tdesc;
caa7fd04
AB
1772 }
1773 else
1774 {
1775 /* Use the target description from the XML file. */
1776 tdesc = file_read_description_xml (args);
1777 }
1778
1779 if (tdesc == NULL)
1780 error (_("There is no target description to print."));
1781
1782 std::string buf;
1783 print_xml_feature v (&buf);
1784 tdesc->accept (v);
0426ad51 1785 gdb_puts (buf.c_str ());
caa7fd04
AB
1786}
1787
27d41eac
YQ
1788namespace selftests {
1789
1c28969e
SM
1790/* A reference target description, used for testing (see record_xml_tdesc). */
1791
1792struct xml_test_tdesc
1793{
1794 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
1795 : name (name), tdesc (std::move (tdesc))
1796 {}
1797
1798 const char *name;
1799 std::unique_ptr<const target_desc> tdesc;
1800};
1801
1802static std::vector<xml_test_tdesc> xml_tdesc;
27d41eac
YQ
1803
1804#if GDB_SELF_TEST
1805
405feb71 1806/* See target-descriptions.h. */
27d41eac
YQ
1807
1808void
1809record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1810{
1c28969e 1811 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
27d41eac
YQ
1812}
1813#endif
1814
1815}
1816
85102364 1817/* Test the conversion process of a target description to/from xml: Take a target
e98577a9
AH
1818 description TDESC, convert to xml, back to a description, and confirm the new
1819 tdesc is identical to the original. */
1820static bool
1821maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1822{
1823 const char *xml = tdesc_get_features_xml (tdesc);
1824
1825 if (xml == nullptr || *xml != '@')
1826 {
6cb06a8c
TT
1827 gdb_printf (_("Could not convert description for %s to xml.\n"),
1828 name);
e98577a9
AH
1829 return false;
1830 }
1831
1832 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1833
1834 if (tdesc_trans == nullptr)
1835 {
6cb06a8c
TT
1836 gdb_printf (_("Could not convert description for %s from xml.\n"),
1837 name);
e98577a9
AH
1838 return false;
1839 }
1840 else if (*tdesc != *tdesc_trans)
1841 {
6cb06a8c
TT
1842 gdb_printf (_("Converted description for %s does not match.\n"),
1843 name);
e98577a9
AH
1844 return false;
1845 }
1846 return true;
1847}
1848
1849
27d41eac
YQ
1850/* Check that the target descriptions created dynamically by
1851 architecture-specific code equal the descriptions created from XML files
1852 found in the specified directory DIR. */
1853
1854static void
e100df1a 1855maintenance_check_xml_descriptions (const char *dir, int from_tty)
27d41eac
YQ
1856{
1857 if (dir == NULL)
1858 error (_("Missing dir name"));
1859
1860 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1861 std::string feature_dir (dir1.get ());
1862 unsigned int failed = 0;
1863
1864 for (auto const &e : selftests::xml_tdesc)
1865 {
1c28969e 1866 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
27d41eac
YQ
1867 const target_desc *tdesc
1868 = file_read_description_xml (tdesc_xml.data ());
1869
1c28969e 1870 if (tdesc == NULL || *tdesc != *e.tdesc)
e98577a9 1871 {
6cb06a8c 1872 gdb_printf ( _("Descriptions for %s do not match.\n"), e.name);
e98577a9
AH
1873 failed++;
1874 }
1c28969e
SM
1875 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
1876 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
27d41eac
YQ
1877 failed++;
1878 }
6cb06a8c
TT
1879 gdb_printf (_("Tested %lu XML files, %d failed\n"),
1880 (long) selftests::xml_tdesc.size (), failed);
27d41eac
YQ
1881}
1882
5fe70629 1883INIT_GDB_FILE (target_descriptions)
23181151 1884{
20821f4e
AB
1885 cmd_list_element *cmd;
1886
f54bdb6d
SM
1887 add_setshow_prefix_cmd ("tdesc", class_maintenance,
1888 _("Set target description specific variables."),
1889 _("Show target description specific variables."),
1890 &tdesc_set_cmdlist, &tdesc_show_cmdlist,
1891 &setlist, &showlist);
1892
0743fc83 1893 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
23181151 1894Unset target description specific variables."),
2f822da5 1895 &tdesc_unset_cmdlist,
0743fc83 1896 0 /* allow-unknown */, &unsetlist);
23181151
DJ
1897
1898 add_setshow_filename_cmd ("filename", class_obscure,
6ecd4729 1899 &tdesc_filename_cmd_string,
23181151 1900 _("\
590042fc
PW
1901Set the file to read for an XML target description."), _("\
1902Show the file to read for an XML target description."), _("\
23181151
DJ
1903When set, GDB will read the target description from a local\n\
1904file instead of querying the remote target."),
1905 set_tdesc_filename_cmd,
1906 show_tdesc_filename_cmd,
1907 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1908
1909 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
590042fc
PW
1910Unset the file to read for an XML target description.\n\
1911When unset, GDB will read the description from the target."),
23181151 1912 &tdesc_unset_cmdlist);
81adfced 1913
ab33b152
AB
1914 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1915 static std::string help_text
1916 = gdb::option::build_help (_("\
1917Print the current target description as a C source file.\n\
1918Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
1919\n\
1920Options:\n\
1921%OPTIONS%\n\
1922\n\
1923When FILENAME is not provided then print the current target\n\
1924description, otherwise an XML target description is read from\n\
1925FILENAME and printed as a C function.\n\
1926\n\
1927When '-single-feature' is used then the target description should\n\
1928contain a single feature and the generated C code will only create\n\
1929that feature within an already existing target_desc object."), grp);
1930 cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
1931 help_text.c_str (), &maintenanceprintlist);
1932 set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
27d41eac 1933
caa7fd04
AB
1934 cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
1935Print the current target description as an XML file."),
1936 &maintenanceprintlist);
dc22ab49 1937 set_cmd_completer (cmd, deprecated_filename_completer);
caa7fd04 1938
27d41eac
YQ
1939 cmd = add_cmd ("xml-descriptions", class_maintenance,
1940 maintenance_check_xml_descriptions, _("\
590042fc 1941Check equality of GDB target descriptions and XML created descriptions.\n\
27d41eac
YQ
1942Check the target descriptions created in GDB equal the descriptions\n\
1943created from XML files in the directory.\n\
1944The parameter is the directory name."),
1945 &maintenancechecklist);
dc22ab49 1946 set_cmd_completer (cmd, deprecated_filename_completer);
23181151 1947}