Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / gdb / target-descriptions.c
blob3ee8a751f3b303b7d02df40b3b3836c8a8c83066
1 /* Target description support for GDB.
3 Copyright (C) 2006-2024 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
23 #include "cli/cli-cmds.h"
24 #include "gdbsupport/unordered_set.h"
25 #include "gdbtypes.h"
26 #include "reggroups.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "xml-support.h"
30 #include "xml-tdesc.h"
31 #include "osabi.h"
33 #include "gdbsupport/gdb_obstack.h"
34 #include "inferior.h"
35 #include <algorithm>
36 #include "completer.h"
37 #include "readline/tilde.h"
39 /* Types. */
41 struct property
43 property (const std::string &key_, const std::string &value_)
44 : key (key_), value (value_)
47 std::string key;
48 std::string value;
51 /* Convert a tdesc_type to a gdb type. */
53 static type *
54 make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
56 class gdb_type_creator : public tdesc_element_visitor
58 public:
59 gdb_type_creator (struct gdbarch *gdbarch)
60 : m_gdbarch (gdbarch)
63 type *get_type ()
65 return m_type;
68 void visit (const tdesc_type_builtin *e) override
70 switch (e->kind)
72 /* Predefined types. */
73 case TDESC_TYPE_BOOL:
74 m_type = builtin_type (m_gdbarch)->builtin_bool;
75 return;
76 case TDESC_TYPE_INT8:
77 m_type = builtin_type (m_gdbarch)->builtin_int8;
78 return;
79 case TDESC_TYPE_INT16:
80 m_type = builtin_type (m_gdbarch)->builtin_int16;
81 return;
82 case TDESC_TYPE_INT32:
83 m_type = builtin_type (m_gdbarch)->builtin_int32;
84 return;
85 case TDESC_TYPE_INT64:
86 m_type = builtin_type (m_gdbarch)->builtin_int64;
87 return;
88 case TDESC_TYPE_INT128:
89 m_type = builtin_type (m_gdbarch)->builtin_int128;
90 return;
91 case TDESC_TYPE_UINT8:
92 m_type = builtin_type (m_gdbarch)->builtin_uint8;
93 return;
94 case TDESC_TYPE_UINT16:
95 m_type = builtin_type (m_gdbarch)->builtin_uint16;
96 return;
97 case TDESC_TYPE_UINT32:
98 m_type = builtin_type (m_gdbarch)->builtin_uint32;
99 return;
100 case TDESC_TYPE_UINT64:
101 m_type = builtin_type (m_gdbarch)->builtin_uint64;
102 return;
103 case TDESC_TYPE_UINT128:
104 m_type = builtin_type (m_gdbarch)->builtin_uint128;
105 return;
106 case TDESC_TYPE_CODE_PTR:
107 m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
108 return;
109 case TDESC_TYPE_DATA_PTR:
110 m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
111 return;
114 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
115 if (m_type != NULL)
116 return;
118 type_allocator alloc (m_gdbarch);
119 switch (e->kind)
121 case TDESC_TYPE_IEEE_HALF:
122 m_type = init_float_type (alloc, -1, "builtin_type_ieee_half",
123 floatformats_ieee_half);
124 return;
126 case TDESC_TYPE_IEEE_SINGLE:
127 m_type = init_float_type (alloc, -1, "builtin_type_ieee_single",
128 floatformats_ieee_single);
129 return;
131 case TDESC_TYPE_IEEE_DOUBLE:
132 m_type = init_float_type (alloc, -1, "builtin_type_ieee_double",
133 floatformats_ieee_double);
134 return;
135 case TDESC_TYPE_ARM_FPA_EXT:
136 m_type = init_float_type (alloc, -1, "builtin_type_arm_ext",
137 floatformats_arm_ext);
138 return;
140 case TDESC_TYPE_I387_EXT:
141 m_type = init_float_type (alloc, -1, "builtin_type_i387_ext",
142 floatformats_i387_ext);
143 return;
145 case TDESC_TYPE_BFLOAT16:
146 m_type = init_float_type (alloc, -1, "builtin_type_bfloat16",
147 floatformats_bfloat16);
148 return;
151 internal_error ("Type \"%s\" has an unknown kind %d",
152 e->name.c_str (), e->kind);
155 void visit (const tdesc_type_vector *e) override
157 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
158 if (m_type != NULL)
159 return;
161 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
162 m_type = init_vector_type (element_gdb_type, e->count);
163 m_type->set_name (xstrdup (e->name.c_str ()));
164 return;
167 void visit (const tdesc_type_with_fields *e) override
169 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
170 if (m_type != NULL)
171 return;
173 switch (e->kind)
175 case TDESC_TYPE_STRUCT:
176 make_gdb_type_struct (e);
177 return;
178 case TDESC_TYPE_UNION:
179 make_gdb_type_union (e);
180 return;
181 case TDESC_TYPE_FLAGS:
182 make_gdb_type_flags (e);
183 return;
184 case TDESC_TYPE_ENUM:
185 make_gdb_type_enum (e);
186 return;
189 internal_error ("Type \"%s\" has an unknown kind %d",
190 e->name.c_str (), e->kind);
193 private:
195 void make_gdb_type_struct (const tdesc_type_with_fields *e)
197 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
198 m_type->set_name (xstrdup (e->name.c_str ()));
200 for (const tdesc_type_field &f : e->fields)
202 if (f.start != -1 && f.end != -1)
204 /* Bitfield. */
205 struct field *fld;
206 struct type *field_gdb_type;
207 int bitsize, total_size;
209 /* This invariant should be preserved while creating types. */
210 gdb_assert (e->size != 0);
211 if (f.type != NULL)
212 field_gdb_type = make_gdb_type (m_gdbarch, f.type);
213 else if (e->size > 4)
214 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
215 else
216 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
218 fld = append_composite_type_field_raw
219 (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
221 /* For little-endian, BITPOS counts from the LSB of
222 the structure and marks the LSB of the field. For
223 big-endian, BITPOS counts from the MSB of the
224 structure and marks the MSB of the field. Either
225 way, it is the number of bits to the "left" of the
226 field. To calculate this in big-endian, we need
227 the total size of the structure. */
228 bitsize = f.end - f.start + 1;
229 total_size = e->size * TARGET_CHAR_BIT;
230 if (gdbarch_byte_order (m_gdbarch) == BFD_ENDIAN_BIG)
231 fld->set_loc_bitpos (total_size - f.start - bitsize);
232 else
233 fld->set_loc_bitpos (f.start);
234 fld->set_bitsize (bitsize);
236 else
238 gdb_assert (f.start == -1 && f.end == -1);
239 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
240 append_composite_type_field (m_type,
241 xstrdup (f.name.c_str ()),
242 field_gdb_type);
246 if (e->size != 0)
247 m_type->set_length (e->size);
250 void make_gdb_type_union (const tdesc_type_with_fields *e)
252 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
253 m_type->set_name (xstrdup (e->name.c_str ()));
255 for (const tdesc_type_field &f : e->fields)
257 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
258 append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
259 field_gdb_type);
261 /* If any of the children of a union are vectors, flag the
262 union as a vector also. This allows e.g. a union of two
263 vector types to show up automatically in "info vector". */
264 if (field_gdb_type->is_vector ())
265 m_type->set_is_vector (true);
269 void make_gdb_type_flags (const tdesc_type_with_fields *e)
271 m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
272 e->size * TARGET_CHAR_BIT);
274 for (const tdesc_type_field &f : e->fields)
276 int bitsize = f.end - f.start + 1;
278 gdb_assert (f.type != NULL);
279 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
280 append_flags_type_field (m_type, f.start, bitsize,
281 field_gdb_type, f.name.c_str ());
285 void make_gdb_type_enum (const tdesc_type_with_fields *e)
287 m_type = (type_allocator (m_gdbarch)
288 .new_type (TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
289 e->name.c_str ()));
291 m_type->set_is_unsigned (true);
293 for (const tdesc_type_field &f : e->fields)
295 struct field *fld
296 = append_composite_type_field_raw (m_type,
297 xstrdup (f.name.c_str ()),
298 NULL);
300 fld->set_loc_enumval (f.start);
304 /* The gdbarch used. */
305 struct gdbarch *m_gdbarch;
307 /* The type created. */
308 type *m_type;
311 gdb_type_creator gdb_type (gdbarch);
312 ttype->accept (gdb_type);
313 return gdb_type.get_type ();
316 /* Wrapper around bfd_arch_info_type. A class with this name is used in
317 the API that is shared between gdb and gdbserver code, but gdbserver
318 doesn't use compatibility information, so its version of this class is
319 empty. */
321 class tdesc_compatible_info
323 public:
324 /* Constructor. */
325 explicit tdesc_compatible_info (const bfd_arch_info_type *arch)
326 : m_arch (arch)
327 { /* Nothing. */ }
329 /* Access the contained pointer. */
330 const bfd_arch_info_type *arch () const
331 { return m_arch; }
333 private:
334 /* Architecture information looked up from the <compatible> entity within
335 a target description. */
336 const bfd_arch_info_type *m_arch;
339 /* A target description. */
341 struct target_desc : tdesc_element
343 target_desc ()
346 virtual ~target_desc () = default;
348 target_desc (const target_desc &) = delete;
349 void operator= (const target_desc &) = delete;
351 /* The architecture reported by the target, if any. */
352 const struct bfd_arch_info *arch = NULL;
354 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
355 otherwise. */
356 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
358 /* The list of compatible architectures reported by the target. */
359 std::vector<tdesc_compatible_info_up> compatible;
361 /* Any architecture-specific properties specified by the target. */
362 std::vector<property> properties;
364 /* The features associated with this target. */
365 std::vector<tdesc_feature_up> features;
367 /* Used to cache the generated xml version of the target description. */
368 mutable char *xmltarget = nullptr;
370 void accept (tdesc_element_visitor &v) const override
372 v.visit_pre (this);
374 for (const tdesc_feature_up &feature : features)
375 feature->accept (v);
377 v.visit_post (this);
380 bool operator== (const target_desc &other) const
382 if (arch != other.arch)
383 return false;
385 if (osabi != other.osabi)
386 return false;
388 if (features.size () != other.features.size ())
389 return false;
391 for (int ix = 0; ix < features.size (); ix++)
393 const tdesc_feature_up &feature1 = features[ix];
394 const tdesc_feature_up &feature2 = other.features[ix];
396 if (feature1 != feature2 && *feature1 != *feature2)
397 return false;
400 return true;
403 bool operator!= (const target_desc &other) const
405 return !(*this == other);
409 /* Per-architecture data associated with a target description. The
410 target description may be shared by multiple architectures, but
411 this data is private to one gdbarch. */
413 struct tdesc_arch_reg
415 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
416 : reg (reg_), type (type_)
419 struct tdesc_reg *reg;
420 struct type *type;
423 struct tdesc_arch_data
425 /* A list of register/type pairs, indexed by GDB's internal register number.
426 During initialization of the gdbarch this list is used to store
427 registers which the architecture assigns a fixed register number.
428 Registers which are NULL in this array, or off the end, are
429 treated as zero-sized and nameless (i.e. placeholders in the
430 numbering). */
431 std::vector<tdesc_arch_reg> arch_regs;
433 /* Functions which report the register name, type, and reggroups for
434 pseudo-registers. */
435 gdbarch_register_name_ftype *pseudo_register_name = NULL;
436 gdbarch_register_type_ftype *pseudo_register_type = NULL;
437 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
440 /* A handle for architecture-specific data associated with the
441 target description (see struct tdesc_arch_data). */
443 static const registry<gdbarch>::key<tdesc_arch_data> tdesc_data;
445 /* Get or create the tdesc_data. */
446 static tdesc_arch_data *
447 get_arch_data (struct gdbarch *gdbarch)
449 tdesc_arch_data *result = tdesc_data.get (gdbarch);
450 if (result == nullptr)
451 result = tdesc_data.emplace (gdbarch);
452 return result;
455 /* The string manipulated by the "set tdesc filename ..." command. */
457 static std::string tdesc_filename_cmd_string;
459 /* Fetch the current target's description, and switch the current
460 architecture to one which incorporates that description. */
462 void
463 target_find_description (void)
465 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
467 /* If we've already fetched a description from the target, don't do
468 it again. This allows a target to fetch the description early,
469 during its to_open or to_create_inferior, if it needs extra
470 information about the target to initialize. */
471 if (tdesc_info->fetched)
472 return;
474 /* The current architecture should not have any target description
475 specified. It should have been cleared, e.g. when we
476 disconnected from the previous target. */
477 gdb_assert (gdbarch_target_desc (current_inferior ()->arch ()) == NULL);
479 /* First try to fetch an XML description from the user-specified
480 file. */
481 tdesc_info->tdesc = nullptr;
482 if (!tdesc_info->filename.empty ())
483 tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
485 /* Next try to read the description from the current target using
486 target objects. */
487 if (tdesc_info->tdesc == nullptr)
488 tdesc_info->tdesc = target_read_description_xml
489 (current_inferior ()->top_target ());
491 /* If that failed try a target-specific hook. */
492 if (tdesc_info->tdesc == nullptr)
493 tdesc_info->tdesc = target_read_description
494 (current_inferior ()->top_target ());
496 /* If a non-NULL description was returned, then update the current
497 architecture. */
498 if (tdesc_info->tdesc != nullptr)
500 struct gdbarch_info info;
502 info.target_desc = tdesc_info->tdesc;
503 if (!gdbarch_update_p (current_inferior (), info))
505 warning (_("Architecture rejected target-supplied description"));
506 tdesc_info->tdesc = nullptr;
508 else
510 struct tdesc_arch_data *data;
512 data = get_arch_data (current_inferior ()->arch ());
513 if (tdesc_has_registers (tdesc_info->tdesc)
514 && data->arch_regs.empty ())
515 warning (_("Target-supplied registers are not supported "
516 "by the current architecture"));
520 /* Now that we know this description is usable, record that we
521 fetched it. */
522 tdesc_info->fetched = true;
525 /* Discard any description fetched from the current target, and switch
526 the current architecture to one with no target description. */
528 void
529 target_clear_description (void)
531 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
533 if (!tdesc_info->fetched)
534 return;
536 tdesc_info->fetched = false;
537 tdesc_info->tdesc = nullptr;
539 gdbarch_info info;
540 if (!gdbarch_update_p (current_inferior (), info))
541 internal_error (_("Could not remove target-supplied description"));
544 /* See target-descriptions.h. */
546 const target_desc *
547 target_current_description (inferior *inf)
549 target_desc_info *tdesc_info = &inf->tdesc_info;
551 if (tdesc_info->fetched)
552 return tdesc_info->tdesc;
554 return NULL;
557 /* Return non-zero if this target description is compatible
558 with the given BFD architecture. */
561 tdesc_compatible_p (const struct target_desc *target_desc,
562 const struct bfd_arch_info *arch)
564 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
566 if (compat->arch () == arch
567 || arch->compatible (arch, compat->arch ())
568 || compat->arch ()->compatible (compat->arch (), arch))
569 return 1;
572 return 0;
576 /* Direct accessors for target descriptions. */
578 /* Return the string value of a property named KEY, or NULL if the
579 property was not specified. */
581 const char *
582 tdesc_property (const struct target_desc *target_desc, const char *key)
584 for (const property &prop : target_desc->properties)
585 if (prop.key == key)
586 return prop.value.c_str ();
588 return NULL;
591 /* Return the BFD architecture associated with this target
592 description, or NULL if no architecture was specified. */
594 const struct bfd_arch_info *
595 tdesc_architecture (const struct target_desc *target_desc)
597 return target_desc->arch;
600 /* See gdbsupport/tdesc.h. */
602 const char *
603 tdesc_architecture_name (const struct target_desc *target_desc)
605 if (target_desc->arch != NULL)
606 return target_desc->arch->printable_name;
607 return NULL;
610 /* See gdbsupport/tdesc.h. */
612 const std::vector<tdesc_compatible_info_up> &
613 tdesc_compatible_info_list (const target_desc *target_desc)
615 return target_desc->compatible;
618 /* See gdbsupport/tdesc.h. */
620 const char *
621 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
623 return compatible->arch ()->printable_name;
626 /* Return the OSABI associated with this target description, or
627 GDB_OSABI_UNKNOWN if no osabi was specified. */
629 enum gdb_osabi
630 tdesc_osabi (const struct target_desc *target_desc)
632 return target_desc->osabi;
635 /* See gdbsupport/tdesc.h. */
637 const char *
638 tdesc_osabi_name (const struct target_desc *target_desc)
640 enum gdb_osabi osabi = tdesc_osabi (target_desc);
641 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
642 return gdbarch_osabi_name (osabi);
643 return nullptr;
646 /* Return 1 if this target description includes any registers. */
649 tdesc_has_registers (const struct target_desc *target_desc)
651 if (target_desc == NULL)
652 return 0;
654 for (const tdesc_feature_up &feature : target_desc->features)
655 if (!feature->registers.empty ())
656 return 1;
658 return 0;
661 /* Return the feature with the given name, if present, or NULL if
662 the named feature is not found. */
664 const struct tdesc_feature *
665 tdesc_find_feature (const struct target_desc *target_desc,
666 const char *name)
668 for (const tdesc_feature_up &feature : target_desc->features)
669 if (feature->name == name)
670 return feature.get ();
672 return NULL;
675 /* Return the name of FEATURE. */
677 const char *
678 tdesc_feature_name (const struct tdesc_feature *feature)
680 return feature->name.c_str ();
683 /* Lookup type associated with ID. */
685 struct type *
686 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
688 tdesc_arch_data *data = get_arch_data (gdbarch);
690 for (const tdesc_arch_reg &reg : data->arch_regs)
692 if (reg.reg
693 && reg.reg->tdesc_type
694 && reg.type
695 && reg.reg->tdesc_type->name == id)
696 return reg.type;
699 return NULL;
702 /* Support for registers from target descriptions. */
704 /* Construct the per-gdbarch data. */
706 tdesc_arch_data_up
707 tdesc_data_alloc (void)
709 return tdesc_arch_data_up (new tdesc_arch_data ());
712 /* See target-descriptions.h. */
714 void
715 tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
717 delete data;
720 /* Search FEATURE for a register named NAME. */
722 static struct tdesc_reg *
723 tdesc_find_register_early (const struct tdesc_feature *feature,
724 const char *name)
726 for (const tdesc_reg_up &reg : feature->registers)
727 if (strcasecmp (reg->name.c_str (), name) == 0)
728 return reg.get ();
730 return NULL;
733 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
736 tdesc_numbered_register (const struct tdesc_feature *feature,
737 struct tdesc_arch_data *data,
738 int regno, const char *name)
740 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
742 if (reg == NULL)
743 return 0;
745 /* Make sure the vector includes a REGNO'th element. */
746 while (regno >= data->arch_regs.size ())
747 data->arch_regs.emplace_back (nullptr, nullptr);
749 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
751 return 1;
754 /* Search FEATURE for a register named NAME, but do not assign a fixed
755 register number to it. */
758 tdesc_unnumbered_register (const struct tdesc_feature *feature,
759 const char *name)
761 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
763 if (reg == NULL)
764 return 0;
766 return 1;
769 /* Search FEATURE for a register whose name is in NAMES and assign
770 REGNO to it. */
773 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
774 struct tdesc_arch_data *data,
775 int regno, const char *const names[])
777 int i;
779 for (i = 0; names[i] != NULL; i++)
780 if (tdesc_numbered_register (feature, data, regno, names[i]))
781 return 1;
783 return 0;
786 /* See target-descriptions.h. */
788 bool
789 tdesc_found_register (struct tdesc_arch_data *data, int regno)
791 gdb_assert (regno >= 0);
793 return (regno < data->arch_regs.size ()
794 && data->arch_regs[regno].reg != nullptr);
797 /* Search FEATURE for a register named NAME, and return its size in
798 bits. The register must exist. */
801 tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
803 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
805 gdb_assert (reg != NULL);
806 return reg->bitsize;
809 /* Look up a register by its GDB internal register number. */
811 static struct tdesc_arch_reg *
812 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
814 struct tdesc_arch_data *data = get_arch_data (gdbarch);
816 if (regno < data->arch_regs.size ())
817 return &data->arch_regs[regno];
818 else
819 return NULL;
822 static struct tdesc_reg *
823 tdesc_find_register (struct gdbarch *gdbarch, int regno)
825 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
827 return reg? reg->reg : NULL;
830 /* Return the name of register REGNO, from the target description or
831 from an architecture-provided pseudo_register_name method. */
833 const char *
834 tdesc_register_name (struct gdbarch *gdbarch, int regno)
836 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
837 int num_regs = gdbarch_num_regs (gdbarch);
839 if (reg != NULL)
840 return reg->name.c_str ();
842 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
844 struct tdesc_arch_data *data = get_arch_data (gdbarch);
846 gdb_assert (data->pseudo_register_name != NULL);
847 return data->pseudo_register_name (gdbarch, regno);
850 return "";
853 struct type *
854 tdesc_register_type (struct gdbarch *gdbarch, int regno)
856 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
857 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
858 int num_regs = gdbarch_num_regs (gdbarch);
859 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
861 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
863 struct tdesc_arch_data *data = get_arch_data (gdbarch);
865 gdb_assert (data->pseudo_register_type != NULL);
866 return data->pseudo_register_type (gdbarch, regno);
869 if (reg == NULL)
870 /* Return "int0_t", since "void" has a misleading size of one. */
871 return builtin_type (gdbarch)->builtin_int0;
873 if (arch_reg->type == NULL)
875 /* First check for a predefined or target defined type. */
876 if (reg->tdesc_type)
877 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
879 /* Next try size-sensitive type shortcuts. */
880 else if (reg->type == "float")
882 if (reg->bitsize == gdbarch_float_bit (gdbarch))
883 arch_reg->type = builtin_type (gdbarch)->builtin_float;
884 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
885 arch_reg->type = builtin_type (gdbarch)->builtin_double;
886 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
887 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
888 else
890 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
891 reg->name.c_str (), reg->bitsize);
892 arch_reg->type = builtin_type (gdbarch)->builtin_double;
895 else if (reg->type == "int")
897 if (reg->bitsize == gdbarch_long_bit (gdbarch))
898 arch_reg->type = builtin_type (gdbarch)->builtin_long;
899 else if (reg->bitsize == TARGET_CHAR_BIT)
900 arch_reg->type = builtin_type (gdbarch)->builtin_char;
901 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
902 arch_reg->type = builtin_type (gdbarch)->builtin_short;
903 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
904 arch_reg->type = builtin_type (gdbarch)->builtin_int;
905 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
906 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
907 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
908 /* A bit desperate by this point... */
909 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
910 else
912 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
913 reg->name.c_str (), reg->bitsize);
914 arch_reg->type = builtin_type (gdbarch)->builtin_long;
918 if (arch_reg->type == NULL)
919 internal_error ("Register \"%s\" has an unknown type \"%s\"",
920 reg->name.c_str (), reg->type.c_str ());
923 return arch_reg->type;
926 static int
927 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
929 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
931 if (reg != NULL)
932 return reg->target_regnum;
933 else
934 return -1;
937 /* Check whether REGNUM is a member of REGGROUP. Registers from the
938 target description may be classified as general, float, vector or other
939 register groups registered with reggroup_add(). Unlike a gdbarch
940 register_reggroup_p method, this function will return -1 if it does not
941 know; the caller should handle registers with no specified group.
943 The names of containing features are not used. This might be extended
944 to display registers in some more useful groupings.
946 The save-restore flag is also implemented here. */
949 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
950 const struct reggroup *reggroup)
952 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
954 if (reg != NULL && !reg->group.empty ()
955 && (reg->group == reggroup->name ()))
956 return 1;
958 if (reg != NULL
959 && (reggroup == save_reggroup || reggroup == restore_reggroup))
960 return reg->save_restore;
962 return -1;
965 /* Check whether REGNUM is a member of REGGROUP. Registers with no
966 group specified go to the default reggroup function and are handled
967 by type. */
969 static int
970 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
971 const struct reggroup *reggroup)
973 int num_regs = gdbarch_num_regs (gdbarch);
974 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
975 int ret;
977 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
979 struct tdesc_arch_data *data = get_arch_data (gdbarch);
981 if (data->pseudo_register_reggroup_p != NULL)
982 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
983 /* Otherwise fall through to the default reggroup_p. */
986 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
987 if (ret != -1)
988 return ret;
990 return default_register_reggroup_p (gdbarch, regno, reggroup);
993 /* Record architecture-specific functions to call for pseudo-register
994 support. */
996 void
997 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
998 gdbarch_register_name_ftype *pseudo_name)
1000 struct tdesc_arch_data *data = get_arch_data (gdbarch);
1002 data->pseudo_register_name = pseudo_name;
1005 void
1006 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1007 gdbarch_register_type_ftype *pseudo_type)
1009 struct tdesc_arch_data *data = get_arch_data (gdbarch);
1011 data->pseudo_register_type = pseudo_type;
1014 void
1015 set_tdesc_pseudo_register_reggroup_p
1016 (struct gdbarch *gdbarch,
1017 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1019 struct tdesc_arch_data *data = get_arch_data (gdbarch);
1021 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1024 /* Update GDBARCH to use the target description for registers. */
1026 void
1027 tdesc_use_registers (struct gdbarch *gdbarch,
1028 const struct target_desc *target_desc,
1029 tdesc_arch_data_up &&early_data,
1030 tdesc_unknown_register_ftype unk_reg_cb)
1032 int num_regs = gdbarch_num_regs (gdbarch);
1033 struct tdesc_arch_data *data;
1035 /* We can't use the description for registers if it doesn't describe
1036 any. This function should only be called after validating
1037 registers, so the caller should know that registers are
1038 included. */
1039 gdb_assert (tdesc_has_registers (target_desc));
1041 data = get_arch_data (gdbarch);
1042 data->arch_regs = std::move (early_data->arch_regs);
1044 /* Build up a set of all registers, so that we can assign register
1045 numbers where needed. */
1046 gdb::unordered_set<tdesc_reg *> reg_hash;
1048 for (const tdesc_feature_up &feature : target_desc->features)
1049 for (const tdesc_reg_up &reg : feature->registers)
1051 reg_hash.insert (reg.get ());
1053 /* Add reggroup if its new. */
1054 if (!reg->group.empty ())
1055 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1056 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1057 reg->group.c_str (),
1058 USER_REGGROUP));
1061 /* Remove any registers which were assigned numbers by the
1062 architecture. */
1063 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1064 if (arch_reg.reg != NULL)
1065 reg_hash.erase (arch_reg.reg);
1067 /* Assign numbers to the remaining registers and add them to the
1068 list of registers. The new numbers are always above gdbarch_num_regs.
1069 Iterate over the features, not the hash table, so that the order
1070 matches that in the target description. */
1072 gdb_assert (data->arch_regs.size () <= num_regs);
1073 while (data->arch_regs.size () < num_regs)
1074 data->arch_regs.emplace_back (nullptr, nullptr);
1076 /* First we give the target a chance to number previously unknown
1077 registers. This allows targets to record the numbers assigned based
1078 on which feature the register was from. */
1079 if (unk_reg_cb != NULL)
1081 for (const tdesc_feature_up &feature : target_desc->features)
1082 for (const tdesc_reg_up &reg : feature->registers)
1083 if (reg_hash.contains (reg.get ()))
1085 int regno = unk_reg_cb (gdbarch, feature.get (),
1086 reg->name.c_str (), num_regs);
1087 gdb_assert (regno == -1 || regno >= num_regs);
1088 if (regno != -1)
1090 while (regno >= data->arch_regs.size ())
1091 data->arch_regs.emplace_back (nullptr, nullptr);
1092 data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
1093 num_regs = regno + 1;
1094 reg_hash.erase (reg.get ());
1099 /* Ensure the array was sized correctly above. */
1100 gdb_assert (data->arch_regs.size () == num_regs);
1102 /* Now in a final pass we assign register numbers to any remaining
1103 unnumbered registers. */
1104 for (const tdesc_feature_up &feature : target_desc->features)
1105 for (const tdesc_reg_up &reg : feature->registers)
1106 if (reg_hash.contains (reg.get ()))
1108 data->arch_regs.emplace_back (reg.get (), nullptr);
1109 num_regs++;
1112 /* Update the architecture. */
1113 set_gdbarch_num_regs (gdbarch, num_regs);
1114 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1115 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1116 set_gdbarch_remote_register_number (gdbarch,
1117 tdesc_remote_register_number);
1118 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1121 /* See gdbsupport/tdesc.h. */
1123 struct tdesc_feature *
1124 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1126 struct tdesc_feature *new_feature = new tdesc_feature (name);
1128 tdesc->features.emplace_back (new_feature);
1130 return new_feature;
1133 /* See gdbsupport/tdesc.h. */
1135 target_desc_up
1136 allocate_target_description (void)
1138 return target_desc_up (new target_desc ());
1141 /* See gdbsupport/tdesc.h. */
1143 void
1144 target_desc_deleter::operator() (struct target_desc *target_desc) const
1146 delete target_desc;
1149 void
1150 tdesc_add_compatible (struct target_desc *target_desc,
1151 const struct bfd_arch_info *compatible)
1153 /* If this instance of GDB is compiled without BFD support for the
1154 compatible architecture, simply ignore it -- we would not be able
1155 to handle it anyway. */
1156 if (compatible == NULL)
1157 return;
1159 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
1160 if (compat->arch () == compatible)
1161 internal_error (_("Attempted to add duplicate "
1162 "compatible architecture \"%s\""),
1163 compatible->printable_name);
1165 target_desc->compatible.push_back
1166 (std::unique_ptr<tdesc_compatible_info>
1167 (new tdesc_compatible_info (compatible)));
1170 void
1171 set_tdesc_property (struct target_desc *target_desc,
1172 const char *key, const char *value)
1174 gdb_assert (key != NULL && value != NULL);
1176 if (tdesc_property (target_desc, key) != NULL)
1177 internal_error (_("Attempted to add duplicate property \"%s\""), key);
1179 target_desc->properties.emplace_back (key, value);
1182 /* See gdbsupport/tdesc.h. */
1184 void
1185 set_tdesc_architecture (struct target_desc *target_desc,
1186 const char *name)
1188 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1191 void
1192 set_tdesc_architecture (struct target_desc *target_desc,
1193 const struct bfd_arch_info *arch)
1195 target_desc->arch = arch;
1198 /* See gdbsupport/tdesc.h. */
1200 void
1201 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1203 target_desc->osabi = osabi;
1207 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1208 static struct cmd_list_element *tdesc_unset_cmdlist;
1210 /* Helper functions for the CLI commands. */
1212 static void
1213 set_tdesc_filename_cmd (const char *args, int from_tty,
1214 struct cmd_list_element *c)
1216 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
1218 tdesc_info->filename = tdesc_filename_cmd_string;
1220 target_clear_description ();
1221 target_find_description ();
1224 static void
1225 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1226 struct cmd_list_element *c,
1227 const char *value)
1229 value = current_inferior ()->tdesc_info.filename.data ();
1231 if (value != NULL && *value != '\0')
1232 gdb_printf (file,
1233 _("The target description will be read from \"%s\".\n"),
1234 value);
1235 else
1236 gdb_printf (file,
1237 _("The target description will be "
1238 "read from the target.\n"));
1241 static void
1242 unset_tdesc_filename_cmd (const char *args, int from_tty)
1244 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
1246 tdesc_info->filename.clear ();
1247 target_clear_description ();
1248 target_find_description ();
1251 /* Print target description in C. */
1253 class print_c_tdesc : public tdesc_element_visitor
1255 public:
1256 print_c_tdesc (std::string &filename_after_features)
1257 : m_filename_after_features (filename_after_features)
1259 const char *inp;
1260 char *outp;
1261 const char *filename = lbasename (m_filename_after_features.c_str ());
1263 m_function = (char *) xmalloc (strlen (filename) + 1);
1264 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1265 if (*inp == '.')
1266 break;
1267 else if (*inp == '-')
1268 *outp++ = '_';
1269 else if (*inp == ' ')
1270 *outp++ = '_';
1271 else
1272 *outp++ = *inp;
1273 *outp = '\0';
1275 /* Standard boilerplate. */
1276 gdb_printf ("/* THIS FILE IS GENERATED. "
1277 "-*- buffer-read-only: t -*- vi"
1278 ":set ro:\n");
1281 ~print_c_tdesc ()
1283 xfree (m_function);
1286 void visit_pre (const target_desc *e) override
1288 gdb_printf (" Original: %s */\n\n",
1289 lbasename (m_filename_after_features.c_str ()));
1291 gdb_printf ("#include \"osabi.h\"\n");
1292 gdb_printf ("#include \"target-descriptions.h\"\n");
1293 gdb_printf ("\n");
1295 gdb_printf ("const struct target_desc *tdesc_%s;\n", m_function);
1296 gdb_printf ("static void\n");
1297 gdb_printf ("initialize_tdesc_%s (void)\n", m_function);
1298 gdb_printf ("{\n");
1299 gdb_printf
1300 (" target_desc_up result = allocate_target_description ();\n");
1302 if (tdesc_architecture (e) != NULL)
1304 gdb_printf
1305 (" set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
1306 tdesc_architecture (e)->printable_name);
1307 gdb_printf ("\n");
1309 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1310 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1312 const char *enum_name = gdbarch_osabi_enum_name (tdesc_osabi (e));
1313 gdb_printf (" set_tdesc_osabi (result.get (), %s);\n", enum_name);
1314 gdb_printf ("\n");
1317 for (const tdesc_compatible_info_up &compatible : e->compatible)
1318 gdb_printf
1319 (" tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
1320 compatible->arch ()->printable_name);
1322 if (!e->compatible.empty ())
1323 gdb_printf ("\n");
1325 for (const property &prop : e->properties)
1326 gdb_printf (" set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
1327 prop.key.c_str (), prop.value.c_str ());
1329 gdb_printf (" struct tdesc_feature *feature;\n");
1332 void visit_pre (const tdesc_feature *e) override
1334 gdb_printf ("\n feature = tdesc_create_feature (result.get (), \"%s\");\n",
1335 e->name.c_str ());
1338 void visit_post (const tdesc_feature *e) override
1341 void visit_post (const target_desc *e) override
1343 gdb_printf ("\n tdesc_%s = result.release ();\n", m_function);
1344 gdb_printf ("}\n");
1347 void visit (const tdesc_type_builtin *type) override
1349 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1352 void visit (const tdesc_type_vector *type) override
1354 if (!m_printed_element_type)
1356 gdb_printf (" tdesc_type *element_type;\n");
1357 m_printed_element_type = true;
1360 gdb_printf
1361 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1362 type->element_type->name.c_str ());
1363 gdb_printf
1364 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1365 type->name.c_str (), type->count);
1367 gdb_printf ("\n");
1370 void visit (const tdesc_type_with_fields *type) override
1372 if (!m_printed_type_with_fields)
1374 gdb_printf (" tdesc_type_with_fields *type_with_fields;\n");
1375 m_printed_type_with_fields = true;
1378 switch (type->kind)
1380 case TDESC_TYPE_STRUCT:
1381 case TDESC_TYPE_FLAGS:
1382 if (type->kind == TDESC_TYPE_STRUCT)
1384 gdb_printf
1385 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1386 type->name.c_str ());
1387 if (type->size != 0)
1388 gdb_printf
1389 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
1391 else
1393 gdb_printf
1394 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1395 type->name.c_str (), type->size);
1397 for (const tdesc_type_field &f : type->fields)
1399 const char *type_name;
1401 gdb_assert (f.type != NULL);
1402 type_name = f.type->name.c_str ();
1404 /* To minimize changes to generated files, don't emit type
1405 info for fields that have defaulted types. */
1406 if (f.start != -1)
1408 gdb_assert (f.end != -1);
1409 if (f.type->kind == TDESC_TYPE_BOOL)
1411 gdb_assert (f.start == f.end);
1412 gdb_printf
1413 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1414 f.start, f.name.c_str ());
1416 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1417 || (type->size == 8
1418 && f.type->kind == TDESC_TYPE_UINT64))
1420 gdb_printf
1421 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1422 f.name.c_str (), f.start, f.end);
1424 else
1426 printf_field_type_assignment
1427 ("tdesc_named_type (feature, \"%s\");\n",
1428 type_name);
1429 gdb_printf
1430 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1431 " %d, %d, field_type);\n",
1432 f.name.c_str (), f.start, f.end);
1435 else /* Not a bitfield. */
1437 gdb_assert (f.end == -1);
1438 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1439 printf_field_type_assignment
1440 ("tdesc_named_type (feature, \"%s\");\n", type_name);
1441 gdb_printf
1442 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1443 f.name.c_str ());
1446 break;
1447 case TDESC_TYPE_UNION:
1448 gdb_printf
1449 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1450 type->name.c_str ());
1451 for (const tdesc_type_field &f : type->fields)
1453 printf_field_type_assignment
1454 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
1455 gdb_printf
1456 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1457 f.name.c_str ());
1459 break;
1460 case TDESC_TYPE_ENUM:
1461 gdb_printf
1462 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1463 type->name.c_str (), type->size);
1464 for (const tdesc_type_field &f : type->fields)
1465 gdb_printf
1466 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1467 f.start, f.name.c_str ());
1468 break;
1469 default:
1470 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1473 gdb_printf ("\n");
1476 void visit (const tdesc_reg *reg) override
1478 gdb_printf (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1479 reg->name.c_str (), reg->target_regnum,
1480 reg->save_restore);
1481 if (!reg->group.empty ())
1482 gdb_printf ("\"%s\", ", reg->group.c_str ());
1483 else
1484 gdb_printf ("NULL, ");
1485 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1488 protected:
1489 std::string m_filename_after_features;
1491 private:
1493 /* Print an assignment to the field_type variable. Print the declaration
1494 of field_type if that has not been done yet. */
1495 ATTRIBUTE_PRINTF (2, 3)
1496 void printf_field_type_assignment (const char *fmt, ...)
1498 if (!m_printed_field_type)
1500 gdb_printf (" tdesc_type *field_type;\n");
1501 m_printed_field_type = true;
1504 gdb_printf (" field_type = ");
1506 va_list args;
1507 va_start (args, fmt);
1508 gdb_vprintf (fmt, args);
1509 va_end (args);
1512 char *m_function;
1514 /* Did we print "struct tdesc_type *element_type;" yet? */
1515 bool m_printed_element_type = false;
1517 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1518 bool m_printed_type_with_fields = false;
1520 /* Did we print "struct tdesc_type *field_type;" yet? */
1521 bool m_printed_field_type = false;
1524 /* Print target description feature in C. */
1526 class print_c_feature : public print_c_tdesc
1528 public:
1529 print_c_feature (std::string &file)
1530 : print_c_tdesc (file)
1532 /* Trim ".tmp". */
1533 auto const pos = m_filename_after_features.find_last_of ('.');
1535 m_filename_after_features = m_filename_after_features.substr (0, pos);
1538 void visit_pre (const target_desc *e) override
1540 gdb_printf (" Original: %s */\n\n",
1541 lbasename (m_filename_after_features.c_str ()));
1543 gdb_printf ("#include \"gdbsupport/tdesc.h\"\n");
1544 gdb_printf ("\n");
1547 void visit_post (const target_desc *e) override
1550 void visit_pre (const tdesc_feature *e) override
1552 std::string name (m_filename_after_features);
1554 auto pos = name.find_first_of ('.');
1556 name = name.substr (0, pos);
1557 std::replace (name.begin (), name.end (), '/', '_');
1558 std::replace (name.begin (), name.end (), '-', '_');
1560 gdb_printf ("static int\n");
1561 gdb_printf ("create_feature_%s ", name.c_str ());
1562 gdb_printf ("(struct target_desc *result, long regnum)\n");
1564 gdb_printf ("{\n");
1565 gdb_printf (" struct tdesc_feature *feature;\n");
1567 gdb_printf
1568 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1569 e->name.c_str ());
1572 void visit_post (const tdesc_feature *e) override
1574 gdb_printf (" return regnum;\n");
1575 gdb_printf ("}\n");
1578 void visit (const tdesc_reg *reg) override
1580 /* Most "reg" in XML target descriptions don't have "regnum"
1581 attribute, so the register number is allocated sequentially.
1582 In case that reg has "regnum" attribute, register number
1583 should be set by that explicitly. */
1585 if (reg->target_regnum < m_next_regnum)
1587 /* The integrity check, it can catch some errors on register
1588 number collision, like this,
1590 <reg name="x0" bitsize="32"/>
1591 <reg name="x1" bitsize="32"/>
1592 <reg name="x2" bitsize="32"/>
1593 <reg name="x3" bitsize="32"/>
1594 <reg name="ps" bitsize="32" regnum="3"/>
1596 but it also has false negatives. The target description
1597 below is correct,
1599 <reg name="x1" bitsize="32" regnum="1"/>
1600 <reg name="x3" bitsize="32" regnum="3"/>
1601 <reg name="x2" bitsize="32" regnum="2"/>
1602 <reg name="x4" bitsize="32" regnum="4"/>
1604 but it is not a good practice, so still error on this,
1605 and also print the message so that it can be saved in the
1606 generated c file. */
1608 gdb_printf ("ERROR: \"regnum\" attribute %ld ",
1609 reg->target_regnum);
1610 gdb_printf ("is not the largest number (%d).\n",
1611 m_next_regnum);
1612 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1613 reg->target_regnum, m_next_regnum);
1616 if (reg->target_regnum > m_next_regnum)
1618 gdb_printf (" regnum = %ld;\n", reg->target_regnum);
1619 m_next_regnum = reg->target_regnum;
1622 gdb_printf (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1623 reg->name.c_str (), reg->save_restore);
1624 if (!reg->group.empty ())
1625 gdb_printf ("\"%s\", ", reg->group.c_str ());
1626 else
1627 gdb_printf ("NULL, ");
1628 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1630 m_next_regnum++;
1633 private:
1634 /* The register number to use for the next register we see. */
1635 int m_next_regnum = 0;
1638 /* See gdbsupport/tdesc.h. */
1640 const char *
1641 tdesc_get_features_xml (const target_desc *tdesc)
1643 if (tdesc->xmltarget == nullptr)
1645 std::string buffer ("@");
1646 print_xml_feature v (&buffer);
1647 tdesc->accept (v);
1648 tdesc->xmltarget = xstrdup (buffer.c_str ());
1650 return tdesc->xmltarget;
1653 /* Data structures and functions to setup the option flags for 'maintenance
1654 print c-tdesc command. */
1656 struct maint_print_c_tdesc_options
1658 /* True when the '-single-feature' flag was passed. */
1659 bool single_feature = false;
1662 using maint_print_c_tdesc_opt_def
1663 = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
1665 static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
1666 maint_print_c_tdesc_opt_def {
1667 "single-feature",
1668 [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
1669 N_("Print C description of just a single feature.")
1673 static inline gdb::option::option_def_group
1674 make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
1676 return {{maint_print_c_tdesc_opt_defs}, opts};
1679 /* Implement 'maintenance print c-tdesc' command. */
1681 static void
1682 maint_print_c_tdesc_cmd (const char *args, int from_tty)
1684 const struct target_desc *tdesc;
1686 maint_print_c_tdesc_options opts;
1687 auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
1688 gdb::option::process_options
1689 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1691 std::string filename = extract_single_filename_arg (args);
1693 if (filename.empty ())
1695 /* Use the global target-supplied description, not the current
1696 architecture's. This lets a GDB for one architecture generate C
1697 for another architecture's description, even though the gdbarch
1698 initialization code will reject the new description. */
1699 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
1700 tdesc = tdesc_info->tdesc;
1701 if (tdesc_info->filename.data () != nullptr)
1702 filename = std::string (tdesc_info->filename.data ());
1704 else
1706 /* Use the target description from the XML file. */
1707 tdesc = file_read_description_xml (filename.c_str ());
1710 if (tdesc == NULL)
1711 error (_("There is no target description to print."));
1713 if (filename.empty ())
1714 filename = "fetched from target";
1716 auto loc = filename.rfind ("/features/");
1717 if (loc != std::string::npos)
1718 filename = filename.substr (loc + 10);
1720 /* Print c files for target features instead of target descriptions,
1721 because c files got from target features are more flexible than the
1722 counterparts. */
1723 if (opts.single_feature)
1725 if (tdesc->features.size () != 1)
1726 error (_("only target descriptions with 1 feature can be used "
1727 "with -single-feature option"));
1729 print_c_feature v (filename);
1731 tdesc->accept (v);
1733 else
1735 print_c_tdesc v (filename);
1737 tdesc->accept (v);
1741 /* Completer for the "backtrace" command. */
1743 static void
1744 maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
1745 completion_tracker &tracker,
1746 const char *text, const char *word)
1748 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1749 if (gdb::option::complete_options
1750 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1751 return;
1753 word = advance_to_filename_maybe_quoted_complete_word_point (tracker, text);
1754 filename_maybe_quoted_completer (ignore, tracker, text, word);
1757 /* Implement the maintenance print xml-tdesc command. */
1759 static void
1760 maint_print_xml_tdesc_cmd (const char *args, int from_tty)
1762 const struct target_desc *tdesc;
1764 if (args == NULL)
1766 /* Use the global target-supplied description, not the current
1767 architecture's. This lets a GDB for one architecture generate XML
1768 for another architecture's description, even though the gdbarch
1769 initialization code will reject the new description. */
1770 tdesc = current_inferior ()->tdesc_info.tdesc;
1772 else
1774 /* Use the target description from the XML file. */
1775 tdesc = file_read_description_xml (args);
1778 if (tdesc == NULL)
1779 error (_("There is no target description to print."));
1781 std::string buf;
1782 print_xml_feature v (&buf);
1783 tdesc->accept (v);
1784 gdb_puts (buf.c_str ());
1787 namespace selftests {
1789 /* A reference target description, used for testing (see record_xml_tdesc). */
1791 struct xml_test_tdesc
1793 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
1794 : name (name), tdesc (std::move (tdesc))
1797 const char *name;
1798 std::unique_ptr<const target_desc> tdesc;
1801 static std::vector<xml_test_tdesc> xml_tdesc;
1803 #if GDB_SELF_TEST
1805 /* See target-descriptions.h. */
1807 void
1808 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1810 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
1812 #endif
1816 /* Test the conversion process of a target description to/from xml: Take a target
1817 description TDESC, convert to xml, back to a description, and confirm the new
1818 tdesc is identical to the original. */
1819 static bool
1820 maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1822 const char *xml = tdesc_get_features_xml (tdesc);
1824 if (xml == nullptr || *xml != '@')
1826 gdb_printf (_("Could not convert description for %s to xml.\n"),
1827 name);
1828 return false;
1831 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1833 if (tdesc_trans == nullptr)
1835 gdb_printf (_("Could not convert description for %s from xml.\n"),
1836 name);
1837 return false;
1839 else if (*tdesc != *tdesc_trans)
1841 gdb_printf (_("Converted description for %s does not match.\n"),
1842 name);
1843 return false;
1845 return true;
1849 /* Check that the target descriptions created dynamically by
1850 architecture-specific code equal the descriptions created from XML files
1851 found in the specified directory DIR. */
1853 static void
1854 maintenance_check_xml_descriptions (const char *dir, int from_tty)
1856 if (dir == NULL)
1857 error (_("Missing dir name"));
1859 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1860 std::string feature_dir (dir1.get ());
1861 unsigned int failed = 0;
1863 for (auto const &e : selftests::xml_tdesc)
1865 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
1866 const target_desc *tdesc
1867 = file_read_description_xml (tdesc_xml.data ());
1869 if (tdesc == NULL || *tdesc != *e.tdesc)
1871 gdb_printf ( _("Descriptions for %s do not match.\n"), e.name);
1872 failed++;
1874 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
1875 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
1876 failed++;
1878 gdb_printf (_("Tested %lu XML files, %d failed\n"),
1879 (long) selftests::xml_tdesc.size (), failed);
1882 void _initialize_target_descriptions ();
1883 void
1884 _initialize_target_descriptions ()
1886 cmd_list_element *cmd;
1888 add_setshow_prefix_cmd ("tdesc", class_maintenance,
1889 _("Set target description specific variables."),
1890 _("Show target description specific variables."),
1891 &tdesc_set_cmdlist, &tdesc_show_cmdlist,
1892 &setlist, &showlist);
1894 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
1895 Unset target description specific variables."),
1896 &tdesc_unset_cmdlist,
1897 0 /* allow-unknown */, &unsetlist);
1899 add_setshow_filename_cmd ("filename", class_obscure,
1900 &tdesc_filename_cmd_string,
1901 _("\
1902 Set the file to read for an XML target description."), _("\
1903 Show the file to read for an XML target description."), _("\
1904 When set, GDB will read the target description from a local\n\
1905 file instead of querying the remote target."),
1906 set_tdesc_filename_cmd,
1907 show_tdesc_filename_cmd,
1908 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1910 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1911 Unset the file to read for an XML target description.\n\
1912 When unset, GDB will read the description from the target."),
1913 &tdesc_unset_cmdlist);
1915 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1916 static std::string help_text
1917 = gdb::option::build_help (_("\
1918 Print the current target description as a C source file.\n\
1919 Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
1921 Options:\n\
1922 %OPTIONS%\n\
1924 When FILENAME is not provided then print the current target\n\
1925 description, otherwise an XML target description is read from\n\
1926 FILENAME and printed as a C function.\n\
1928 When '-single-feature' is used then the target description should\n\
1929 contain a single feature and the generated C code will only create\n\
1930 that feature within an already existing target_desc object."), grp);
1931 cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
1932 help_text.c_str (), &maintenanceprintlist);
1933 set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
1935 cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
1936 Print the current target description as an XML file."),
1937 &maintenanceprintlist);
1938 set_cmd_completer (cmd, deprecated_filename_completer);
1940 cmd = add_cmd ("xml-descriptions", class_maintenance,
1941 maintenance_check_xml_descriptions, _("\
1942 Check equality of GDB target descriptions and XML created descriptions.\n\
1943 Check the target descriptions created in GDB equal the descriptions\n\
1944 created from XML files in the directory.\n\
1945 The parameter is the directory name."),
1946 &maintenancechecklist);
1947 set_cmd_completer (cmd, deprecated_filename_completer);