Automatic date update in version.in
[binutils-gdb.git] / gdb / target-descriptions.c
blob85954ac29395eae25cc222b26f958f7d1e99a83f
1 /* Target description support for GDB.
3 Copyright (C) 2006-2022 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 "defs.h"
23 #include "arch-utils.h"
24 #include "gdbcmd.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 "hashtab.h"
35 #include "inferior.h"
36 #include <algorithm>
37 #include "completer.h"
38 #include "readline/tilde.h" /* tilde_expand */
40 /* Types. */
42 struct property
44 property (const std::string &key_, const std::string &value_)
45 : key (key_), value (value_)
48 std::string key;
49 std::string value;
52 /* Convert a tdesc_type to a gdb type. */
54 static type *
55 make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
57 class gdb_type_creator : public tdesc_element_visitor
59 public:
60 gdb_type_creator (struct gdbarch *gdbarch)
61 : m_gdbarch (gdbarch)
64 type *get_type ()
66 return m_type;
69 void visit (const tdesc_type_builtin *e) override
71 switch (e->kind)
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;
115 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
116 if (m_type != NULL)
117 return;
119 switch (e->kind)
121 case TDESC_TYPE_IEEE_HALF:
122 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_half",
123 floatformats_ieee_half);
124 return;
126 case TDESC_TYPE_IEEE_SINGLE:
127 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single",
128 floatformats_ieee_single);
129 return;
131 case TDESC_TYPE_IEEE_DOUBLE:
132 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double",
133 floatformats_ieee_double);
134 return;
135 case TDESC_TYPE_ARM_FPA_EXT:
136 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext",
137 floatformats_arm_ext);
138 return;
140 case TDESC_TYPE_I387_EXT:
141 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext",
142 floatformats_i387_ext);
143 return;
145 case TDESC_TYPE_BFLOAT16:
146 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_bfloat16",
147 floatformats_bfloat16);
148 return;
151 internal_error (__FILE__, __LINE__,
152 "Type \"%s\" has an unknown kind %d",
153 e->name.c_str (), e->kind);
156 void visit (const tdesc_type_vector *e) override
158 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
159 if (m_type != NULL)
160 return;
162 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
163 m_type = init_vector_type (element_gdb_type, e->count);
164 m_type->set_name (xstrdup (e->name.c_str ()));
165 return;
168 void visit (const tdesc_type_with_fields *e) override
170 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
171 if (m_type != NULL)
172 return;
174 switch (e->kind)
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;
190 internal_error (__FILE__, __LINE__,
191 "Type \"%s\" has an unknown kind %d",
192 e->name.c_str (), e->kind);
195 private:
197 void make_gdb_type_struct (const tdesc_type_with_fields *e)
199 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
200 m_type->set_name (xstrdup (e->name.c_str ()));
202 for (const tdesc_type_field &f : e->fields)
204 if (f.start != -1 && f.end != -1)
206 /* Bitfield. */
207 struct field *fld;
208 struct type *field_gdb_type;
209 int bitsize, total_size;
211 /* This invariant should be preserved while creating types. */
212 gdb_assert (e->size != 0);
213 if (f.type != NULL)
214 field_gdb_type = make_gdb_type (m_gdbarch, f.type);
215 else if (e->size > 4)
216 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
217 else
218 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
220 fld = append_composite_type_field_raw
221 (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
223 /* For little-endian, BITPOS counts from the LSB of
224 the structure and marks the LSB of the field. For
225 big-endian, BITPOS counts from the MSB of the
226 structure and marks the MSB of the field. Either
227 way, it is the number of bits to the "left" of the
228 field. To calculate this in big-endian, we need
229 the total size of the structure. */
230 bitsize = f.end - f.start + 1;
231 total_size = e->size * TARGET_CHAR_BIT;
232 if (gdbarch_byte_order (m_gdbarch) == BFD_ENDIAN_BIG)
233 fld->set_loc_bitpos (total_size - f.start - bitsize);
234 else
235 fld->set_loc_bitpos (f.start);
236 FIELD_BITSIZE (fld[0]) = bitsize;
238 else
240 gdb_assert (f.start == -1 && f.end == -1);
241 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
242 append_composite_type_field (m_type,
243 xstrdup (f.name.c_str ()),
244 field_gdb_type);
248 if (e->size != 0)
249 TYPE_LENGTH (m_type) = e->size;
252 void make_gdb_type_union (const tdesc_type_with_fields *e)
254 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
255 m_type->set_name (xstrdup (e->name.c_str ()));
257 for (const tdesc_type_field &f : e->fields)
259 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
260 append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
261 field_gdb_type);
263 /* If any of the children of a union are vectors, flag the
264 union as a vector also. This allows e.g. a union of two
265 vector types to show up automatically in "info vector". */
266 if (field_gdb_type->is_vector ())
267 m_type->set_is_vector (true);
271 void make_gdb_type_flags (const tdesc_type_with_fields *e)
273 m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
274 e->size * TARGET_CHAR_BIT);
276 for (const tdesc_type_field &f : e->fields)
278 int bitsize = f.end - f.start + 1;
280 gdb_assert (f.type != NULL);
281 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
282 append_flags_type_field (m_type, f.start, bitsize,
283 field_gdb_type, f.name.c_str ());
287 void make_gdb_type_enum (const tdesc_type_with_fields *e)
289 m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
290 e->name.c_str ());
292 m_type->set_is_unsigned (true);
294 for (const tdesc_type_field &f : e->fields)
296 struct field *fld
297 = append_composite_type_field_raw (m_type,
298 xstrdup (f.name.c_str ()),
299 NULL);
301 fld->set_loc_enumval (f.start);
305 /* The gdbarch used. */
306 struct gdbarch *m_gdbarch;
308 /* The type created. */
309 type *m_type;
312 gdb_type_creator gdb_type (gdbarch);
313 ttype->accept (gdb_type);
314 return gdb_type.get_type ();
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. */
322 class tdesc_compatible_info
324 public:
325 /* Constructor. */
326 explicit tdesc_compatible_info (const bfd_arch_info_type *arch)
327 : m_arch (arch)
328 { /* Nothing. */ }
330 /* Access the contained pointer. */
331 const bfd_arch_info_type *arch () const
332 { return m_arch; }
334 private:
335 /* Architecture information looked up from the <compatible> entity within
336 a target description. */
337 const bfd_arch_info_type *m_arch;
340 /* A target description. */
342 struct target_desc : tdesc_element
344 target_desc ()
347 virtual ~target_desc () = default;
349 target_desc (const target_desc &) = delete;
350 void operator= (const target_desc &) = delete;
352 /* The architecture reported by the target, if any. */
353 const struct bfd_arch_info *arch = NULL;
355 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
356 otherwise. */
357 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
359 /* The list of compatible architectures reported by the target. */
360 std::vector<tdesc_compatible_info_up> compatible;
362 /* Any architecture-specific properties specified by the target. */
363 std::vector<property> properties;
365 /* The features associated with this target. */
366 std::vector<tdesc_feature_up> features;
368 /* Used to cache the generated xml version of the target description. */
369 mutable char *xmltarget = nullptr;
371 void accept (tdesc_element_visitor &v) const override
373 v.visit_pre (this);
375 for (const tdesc_feature_up &feature : features)
376 feature->accept (v);
378 v.visit_post (this);
381 bool operator== (const target_desc &other) const
383 if (arch != other.arch)
384 return false;
386 if (osabi != other.osabi)
387 return false;
389 if (features.size () != other.features.size ())
390 return false;
392 for (int ix = 0; ix < features.size (); ix++)
394 const tdesc_feature_up &feature1 = features[ix];
395 const tdesc_feature_up &feature2 = other.features[ix];
397 if (feature1 != feature2 && *feature1 != *feature2)
398 return false;
401 return true;
404 bool operator!= (const target_desc &other) const
406 return !(*this == other);
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. */
414 struct tdesc_arch_reg
416 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
417 : reg (reg_), type (type_)
420 struct tdesc_reg *reg;
421 struct type *type;
424 struct tdesc_arch_data
426 /* A list of register/type pairs, indexed by GDB's internal register number.
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). */
432 std::vector<tdesc_arch_reg> arch_regs;
434 /* Functions which report the register name, type, and reggroups for
435 pseudo-registers. */
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;
441 /* Info about an inferior's target description. There's one of these
442 for each inferior. */
444 struct target_desc_info
446 /* A flag indicating that a description has already been fetched
447 from the target, so it should not be queried again. */
449 bool fetched = false;
451 /* The description fetched from the target, or NULL if the target
452 did not supply any description. Only valid when
453 FETCHED is set. Only the description initialization
454 code should access this; normally, the description should be
455 accessed through the gdbarch object. */
457 const struct target_desc *tdesc = nullptr;
459 /* If not empty, the filename to read a target description from, as set by
460 "set tdesc filename ...".
462 If empty, there is not filename specified by the user. */
464 std::string filename;
467 /* Get the inferior INF's target description info, allocating one on
468 the stop if necessary. */
470 static struct target_desc_info *
471 get_tdesc_info (struct inferior *inf)
473 if (inf->tdesc_info == NULL)
474 inf->tdesc_info = new target_desc_info;
476 return inf->tdesc_info;
479 /* A handle for architecture-specific data associated with the
480 target description (see struct tdesc_arch_data). */
482 static struct gdbarch_data *tdesc_data;
484 /* See target-descriptions.h. */
487 target_desc_info_from_user_p (struct target_desc_info *info)
489 return info != nullptr && !info->filename.empty ();
492 /* See target-descriptions.h. */
494 void
495 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
497 struct target_desc_info *src = get_tdesc_info (srcinf);
498 struct target_desc_info *dest = get_tdesc_info (destinf);
500 *dest = *src;
503 /* See target-descriptions.h. */
505 void
506 target_desc_info_free (struct target_desc_info *tdesc_info)
508 delete tdesc_info;
511 /* The string manipulated by the "set tdesc filename ..." command. */
513 static std::string tdesc_filename_cmd_string;
515 /* Fetch the current target's description, and switch the current
516 architecture to one which incorporates that description. */
518 void
519 target_find_description (void)
521 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
523 /* If we've already fetched a description from the target, don't do
524 it again. This allows a target to fetch the description early,
525 during its to_open or to_create_inferior, if it needs extra
526 information about the target to initialize. */
527 if (tdesc_info->fetched)
528 return;
530 /* The current architecture should not have any target description
531 specified. It should have been cleared, e.g. when we
532 disconnected from the previous target. */
533 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
535 /* First try to fetch an XML description from the user-specified
536 file. */
537 tdesc_info->tdesc = nullptr;
538 if (!tdesc_info->filename.empty ())
539 tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
541 /* Next try to read the description from the current target using
542 target objects. */
543 if (tdesc_info->tdesc == nullptr)
544 tdesc_info->tdesc = target_read_description_xml
545 (current_inferior ()->top_target ());
547 /* If that failed try a target-specific hook. */
548 if (tdesc_info->tdesc == nullptr)
549 tdesc_info->tdesc = target_read_description
550 (current_inferior ()->top_target ());
552 /* If a non-NULL description was returned, then update the current
553 architecture. */
554 if (tdesc_info->tdesc != nullptr)
556 struct gdbarch_info info;
558 info.target_desc = tdesc_info->tdesc;
559 if (!gdbarch_update_p (info))
560 warning (_("Architecture rejected target-supplied description"));
561 else
563 struct tdesc_arch_data *data;
565 data = ((struct tdesc_arch_data *)
566 gdbarch_data (target_gdbarch (), tdesc_data));
567 if (tdesc_has_registers (tdesc_info->tdesc)
568 && data->arch_regs.empty ())
569 warning (_("Target-supplied registers are not supported "
570 "by the current architecture"));
574 /* Now that we know this description is usable, record that we
575 fetched it. */
576 tdesc_info->fetched = true;
579 /* Discard any description fetched from the current target, and switch
580 the current architecture to one with no target description. */
582 void
583 target_clear_description (void)
585 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
587 if (!tdesc_info->fetched)
588 return;
590 tdesc_info->fetched = false;
591 tdesc_info->tdesc = nullptr;
593 gdbarch_info info;
594 if (!gdbarch_update_p (info))
595 internal_error (__FILE__, __LINE__,
596 _("Could not remove target-supplied description"));
599 /* Return the global current target description. This should only be
600 used by gdbarch initialization code; most access should be through
601 an existing gdbarch. */
603 const struct target_desc *
604 target_current_description (void)
606 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
608 if (tdesc_info->fetched)
609 return tdesc_info->tdesc;
611 return NULL;
614 /* Return non-zero if this target description is compatible
615 with the given BFD architecture. */
618 tdesc_compatible_p (const struct target_desc *target_desc,
619 const struct bfd_arch_info *arch)
621 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
623 if (compat->arch () == arch
624 || arch->compatible (arch, compat->arch ())
625 || compat->arch ()->compatible (compat->arch (), arch))
626 return 1;
629 return 0;
633 /* Direct accessors for target descriptions. */
635 /* Return the string value of a property named KEY, or NULL if the
636 property was not specified. */
638 const char *
639 tdesc_property (const struct target_desc *target_desc, const char *key)
641 for (const property &prop : target_desc->properties)
642 if (prop.key == key)
643 return prop.value.c_str ();
645 return NULL;
648 /* Return the BFD architecture associated with this target
649 description, or NULL if no architecture was specified. */
651 const struct bfd_arch_info *
652 tdesc_architecture (const struct target_desc *target_desc)
654 return target_desc->arch;
657 /* See gdbsupport/tdesc.h. */
659 const char *
660 tdesc_architecture_name (const struct target_desc *target_desc)
662 if (target_desc->arch != NULL)
663 return target_desc->arch->printable_name;
664 return NULL;
667 /* See gdbsupport/tdesc.h. */
669 const std::vector<tdesc_compatible_info_up> &
670 tdesc_compatible_info_list (const target_desc *target_desc)
672 return target_desc->compatible;
675 /* See gdbsupport/tdesc.h. */
677 const char *
678 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
680 return compatible->arch ()->printable_name;
683 /* Return the OSABI associated with this target description, or
684 GDB_OSABI_UNKNOWN if no osabi was specified. */
686 enum gdb_osabi
687 tdesc_osabi (const struct target_desc *target_desc)
689 return target_desc->osabi;
692 /* See gdbsupport/tdesc.h. */
694 const char *
695 tdesc_osabi_name (const struct target_desc *target_desc)
697 enum gdb_osabi osabi = tdesc_osabi (target_desc);
698 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
699 return gdbarch_osabi_name (osabi);
700 return nullptr;
703 /* Return 1 if this target description includes any registers. */
706 tdesc_has_registers (const struct target_desc *target_desc)
708 if (target_desc == NULL)
709 return 0;
711 for (const tdesc_feature_up &feature : target_desc->features)
712 if (!feature->registers.empty ())
713 return 1;
715 return 0;
718 /* Return the feature with the given name, if present, or NULL if
719 the named feature is not found. */
721 const struct tdesc_feature *
722 tdesc_find_feature (const struct target_desc *target_desc,
723 const char *name)
725 for (const tdesc_feature_up &feature : target_desc->features)
726 if (feature->name == name)
727 return feature.get ();
729 return NULL;
732 /* Return the name of FEATURE. */
734 const char *
735 tdesc_feature_name (const struct tdesc_feature *feature)
737 return feature->name.c_str ();
740 /* Lookup type associated with ID. */
742 struct type *
743 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
745 tdesc_arch_data *data
746 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
748 for (const tdesc_arch_reg &reg : data->arch_regs)
750 if (reg.reg
751 && reg.reg->tdesc_type
752 && reg.type
753 && reg.reg->tdesc_type->name == id)
754 return reg.type;
757 return NULL;
760 /* Support for registers from target descriptions. */
762 /* Construct the per-gdbarch data. */
764 static void *
765 tdesc_data_init (struct obstack *obstack)
767 return obstack_new<tdesc_arch_data> (obstack);
770 /* Similar, but for the temporary copy used during architecture
771 initialization. */
773 tdesc_arch_data_up
774 tdesc_data_alloc (void)
776 return tdesc_arch_data_up (new tdesc_arch_data ());
779 /* See target-descriptions.h. */
781 void
782 tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
784 delete data;
787 /* Search FEATURE for a register named NAME. */
789 static struct tdesc_reg *
790 tdesc_find_register_early (const struct tdesc_feature *feature,
791 const char *name)
793 for (const tdesc_reg_up &reg : feature->registers)
794 if (strcasecmp (reg->name.c_str (), name) == 0)
795 return reg.get ();
797 return NULL;
800 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
803 tdesc_numbered_register (const struct tdesc_feature *feature,
804 struct tdesc_arch_data *data,
805 int regno, const char *name)
807 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
809 if (reg == NULL)
810 return 0;
812 /* Make sure the vector includes a REGNO'th element. */
813 while (regno >= data->arch_regs.size ())
814 data->arch_regs.emplace_back (nullptr, nullptr);
816 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
818 return 1;
821 /* Search FEATURE for a register named NAME, but do not assign a fixed
822 register number to it. */
825 tdesc_unnumbered_register (const struct tdesc_feature *feature,
826 const char *name)
828 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
830 if (reg == NULL)
831 return 0;
833 return 1;
836 /* Search FEATURE for a register whose name is in NAMES and assign
837 REGNO to it. */
840 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
841 struct tdesc_arch_data *data,
842 int regno, const char *const names[])
844 int i;
846 for (i = 0; names[i] != NULL; i++)
847 if (tdesc_numbered_register (feature, data, regno, names[i]))
848 return 1;
850 return 0;
853 /* Search FEATURE for a register named NAME, and return its size in
854 bits. The register must exist. */
857 tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
859 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
861 gdb_assert (reg != NULL);
862 return reg->bitsize;
865 /* Look up a register by its GDB internal register number. */
867 static struct tdesc_arch_reg *
868 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
870 struct tdesc_arch_data *data;
872 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
873 if (regno < data->arch_regs.size ())
874 return &data->arch_regs[regno];
875 else
876 return NULL;
879 static struct tdesc_reg *
880 tdesc_find_register (struct gdbarch *gdbarch, int regno)
882 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
884 return reg? reg->reg : NULL;
887 /* Return the name of register REGNO, from the target description or
888 from an architecture-provided pseudo_register_name method. */
890 const char *
891 tdesc_register_name (struct gdbarch *gdbarch, int regno)
893 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
894 int num_regs = gdbarch_num_regs (gdbarch);
896 if (reg != NULL)
897 return reg->name.c_str ();
899 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
901 struct tdesc_arch_data *data
902 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
904 gdb_assert (data->pseudo_register_name != NULL);
905 return data->pseudo_register_name (gdbarch, regno);
908 return "";
911 struct type *
912 tdesc_register_type (struct gdbarch *gdbarch, int regno)
914 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
915 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
916 int num_regs = gdbarch_num_regs (gdbarch);
917 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
919 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
921 struct tdesc_arch_data *data
922 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
924 gdb_assert (data->pseudo_register_type != NULL);
925 return data->pseudo_register_type (gdbarch, regno);
928 if (reg == NULL)
929 /* Return "int0_t", since "void" has a misleading size of one. */
930 return builtin_type (gdbarch)->builtin_int0;
932 if (arch_reg->type == NULL)
934 /* First check for a predefined or target defined type. */
935 if (reg->tdesc_type)
936 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
938 /* Next try size-sensitive type shortcuts. */
939 else if (reg->type == "float")
941 if (reg->bitsize == gdbarch_float_bit (gdbarch))
942 arch_reg->type = builtin_type (gdbarch)->builtin_float;
943 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
944 arch_reg->type = builtin_type (gdbarch)->builtin_double;
945 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
946 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
947 else
949 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
950 reg->name.c_str (), reg->bitsize);
951 arch_reg->type = builtin_type (gdbarch)->builtin_double;
954 else if (reg->type == "int")
956 if (reg->bitsize == gdbarch_long_bit (gdbarch))
957 arch_reg->type = builtin_type (gdbarch)->builtin_long;
958 else if (reg->bitsize == TARGET_CHAR_BIT)
959 arch_reg->type = builtin_type (gdbarch)->builtin_char;
960 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
961 arch_reg->type = builtin_type (gdbarch)->builtin_short;
962 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
963 arch_reg->type = builtin_type (gdbarch)->builtin_int;
964 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
965 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
966 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
967 /* A bit desperate by this point... */
968 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
969 else
971 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
972 reg->name.c_str (), reg->bitsize);
973 arch_reg->type = builtin_type (gdbarch)->builtin_long;
977 if (arch_reg->type == NULL)
978 internal_error (__FILE__, __LINE__,
979 "Register \"%s\" has an unknown type \"%s\"",
980 reg->name.c_str (), reg->type.c_str ());
983 return arch_reg->type;
986 static int
987 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
989 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
991 if (reg != NULL)
992 return reg->target_regnum;
993 else
994 return -1;
997 /* Check whether REGNUM is a member of REGGROUP. Registers from the
998 target description may be classified as general, float, vector or other
999 register groups registered with reggroup_add(). Unlike a gdbarch
1000 register_reggroup_p method, this function will return -1 if it does not
1001 know; the caller should handle registers with no specified group.
1003 The names of containing features are not used. This might be extended
1004 to display registers in some more useful groupings.
1006 The save-restore flag is also implemented here. */
1009 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1010 const struct reggroup *reggroup)
1012 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1014 if (reg != NULL && !reg->group.empty ()
1015 && (reg->group == reggroup->name ()))
1016 return 1;
1018 if (reg != NULL
1019 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1020 return reg->save_restore;
1022 return -1;
1025 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1026 group specified go to the default reggroup function and are handled
1027 by type. */
1029 static int
1030 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1031 const struct reggroup *reggroup)
1033 int num_regs = gdbarch_num_regs (gdbarch);
1034 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1035 int ret;
1037 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1039 struct tdesc_arch_data *data
1040 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1042 if (data->pseudo_register_reggroup_p != NULL)
1043 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1044 /* Otherwise fall through to the default reggroup_p. */
1047 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1048 if (ret != -1)
1049 return ret;
1051 return default_register_reggroup_p (gdbarch, regno, reggroup);
1054 /* Record architecture-specific functions to call for pseudo-register
1055 support. */
1057 void
1058 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1059 gdbarch_register_name_ftype *pseudo_name)
1061 struct tdesc_arch_data *data
1062 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1064 data->pseudo_register_name = pseudo_name;
1067 void
1068 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1069 gdbarch_register_type_ftype *pseudo_type)
1071 struct tdesc_arch_data *data
1072 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1074 data->pseudo_register_type = pseudo_type;
1077 void
1078 set_tdesc_pseudo_register_reggroup_p
1079 (struct gdbarch *gdbarch,
1080 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1082 struct tdesc_arch_data *data
1083 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1085 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1088 /* Update GDBARCH to use the target description for registers. */
1090 void
1091 tdesc_use_registers (struct gdbarch *gdbarch,
1092 const struct target_desc *target_desc,
1093 tdesc_arch_data_up &&early_data,
1094 tdesc_unknown_register_ftype unk_reg_cb)
1096 int num_regs = gdbarch_num_regs (gdbarch);
1097 struct tdesc_arch_data *data;
1099 /* We can't use the description for registers if it doesn't describe
1100 any. This function should only be called after validating
1101 registers, so the caller should know that registers are
1102 included. */
1103 gdb_assert (tdesc_has_registers (target_desc));
1105 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1106 data->arch_regs = std::move (early_data->arch_regs);
1108 /* Build up a set of all registers, so that we can assign register
1109 numbers where needed. The hash table expands as necessary, so
1110 the initial size is arbitrary. */
1111 htab_up reg_hash (htab_create (37, htab_hash_pointer, htab_eq_pointer,
1112 NULL));
1113 for (const tdesc_feature_up &feature : target_desc->features)
1114 for (const tdesc_reg_up &reg : feature->registers)
1116 void **slot = htab_find_slot (reg_hash.get (), reg.get (), INSERT);
1118 *slot = reg.get ();
1119 /* Add reggroup if its new. */
1120 if (!reg->group.empty ())
1121 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1122 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1123 reg->group.c_str (),
1124 USER_REGGROUP));
1127 /* Remove any registers which were assigned numbers by the
1128 architecture. */
1129 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1130 if (arch_reg.reg != NULL)
1131 htab_remove_elt (reg_hash.get (), arch_reg.reg);
1133 /* Assign numbers to the remaining registers and add them to the
1134 list of registers. The new numbers are always above gdbarch_num_regs.
1135 Iterate over the features, not the hash table, so that the order
1136 matches that in the target description. */
1138 gdb_assert (data->arch_regs.size () <= num_regs);
1139 while (data->arch_regs.size () < num_regs)
1140 data->arch_regs.emplace_back (nullptr, nullptr);
1142 /* First we give the target a chance to number previously unknown
1143 registers. This allows targets to record the numbers assigned based
1144 on which feature the register was from. */
1145 if (unk_reg_cb != NULL)
1147 for (const tdesc_feature_up &feature : target_desc->features)
1148 for (const tdesc_reg_up &reg : feature->registers)
1149 if (htab_find (reg_hash.get (), reg.get ()) != NULL)
1151 int regno = unk_reg_cb (gdbarch, feature.get (),
1152 reg->name.c_str (), num_regs);
1153 gdb_assert (regno == -1 || regno >= num_regs);
1154 if (regno != -1)
1156 while (regno >= data->arch_regs.size ())
1157 data->arch_regs.emplace_back (nullptr, nullptr);
1158 data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
1159 num_regs = regno + 1;
1160 htab_remove_elt (reg_hash.get (), reg.get ());
1165 /* Ensure the array was sized correctly above. */
1166 gdb_assert (data->arch_regs.size () == num_regs);
1168 /* Now in a final pass we assign register numbers to any remaining
1169 unnumbered registers. */
1170 for (const tdesc_feature_up &feature : target_desc->features)
1171 for (const tdesc_reg_up &reg : feature->registers)
1172 if (htab_find (reg_hash.get (), reg.get ()) != NULL)
1174 data->arch_regs.emplace_back (reg.get (), nullptr);
1175 num_regs++;
1178 /* Update the architecture. */
1179 set_gdbarch_num_regs (gdbarch, num_regs);
1180 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1181 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1182 set_gdbarch_remote_register_number (gdbarch,
1183 tdesc_remote_register_number);
1184 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1187 /* See gdbsupport/tdesc.h. */
1189 struct tdesc_feature *
1190 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1192 struct tdesc_feature *new_feature = new tdesc_feature (name);
1194 tdesc->features.emplace_back (new_feature);
1196 return new_feature;
1199 /* See gdbsupport/tdesc.h. */
1201 target_desc_up
1202 allocate_target_description (void)
1204 return target_desc_up (new target_desc ());
1207 /* See gdbsupport/tdesc.h. */
1209 void
1210 target_desc_deleter::operator() (struct target_desc *target_desc) const
1212 delete target_desc;
1215 void
1216 tdesc_add_compatible (struct target_desc *target_desc,
1217 const struct bfd_arch_info *compatible)
1219 /* If this instance of GDB is compiled without BFD support for the
1220 compatible architecture, simply ignore it -- we would not be able
1221 to handle it anyway. */
1222 if (compatible == NULL)
1223 return;
1225 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
1226 if (compat->arch () == compatible)
1227 internal_error (__FILE__, __LINE__,
1228 _("Attempted to add duplicate "
1229 "compatible architecture \"%s\""),
1230 compatible->printable_name);
1232 target_desc->compatible.push_back
1233 (std::unique_ptr<tdesc_compatible_info>
1234 (new tdesc_compatible_info (compatible)));
1237 void
1238 set_tdesc_property (struct target_desc *target_desc,
1239 const char *key, const char *value)
1241 gdb_assert (key != NULL && value != NULL);
1243 if (tdesc_property (target_desc, key) != NULL)
1244 internal_error (__FILE__, __LINE__,
1245 _("Attempted to add duplicate property \"%s\""), key);
1247 target_desc->properties.emplace_back (key, value);
1250 /* See gdbsupport/tdesc.h. */
1252 void
1253 set_tdesc_architecture (struct target_desc *target_desc,
1254 const char *name)
1256 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1259 void
1260 set_tdesc_architecture (struct target_desc *target_desc,
1261 const struct bfd_arch_info *arch)
1263 target_desc->arch = arch;
1266 /* See gdbsupport/tdesc.h. */
1268 void
1269 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1271 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1274 void
1275 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1277 target_desc->osabi = osabi;
1281 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1282 static struct cmd_list_element *tdesc_unset_cmdlist;
1284 /* Helper functions for the CLI commands. */
1286 static void
1287 set_tdesc_filename_cmd (const char *args, int from_tty,
1288 struct cmd_list_element *c)
1290 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
1292 tdesc_info->filename = tdesc_filename_cmd_string;
1294 target_clear_description ();
1295 target_find_description ();
1298 static void
1299 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1300 struct cmd_list_element *c,
1301 const char *value)
1303 value = get_tdesc_info (current_inferior ())->filename.data ();
1305 if (value != NULL && *value != '\0')
1306 gdb_printf (file,
1307 _("The target description will be read from \"%s\".\n"),
1308 value);
1309 else
1310 gdb_printf (file,
1311 _("The target description will be "
1312 "read from the target.\n"));
1315 static void
1316 unset_tdesc_filename_cmd (const char *args, int from_tty)
1318 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
1320 tdesc_info->filename.clear ();
1321 target_clear_description ();
1322 target_find_description ();
1325 /* Print target description in C. */
1327 class print_c_tdesc : public tdesc_element_visitor
1329 public:
1330 print_c_tdesc (std::string &filename_after_features)
1331 : m_filename_after_features (filename_after_features)
1333 const char *inp;
1334 char *outp;
1335 const char *filename = lbasename (m_filename_after_features.c_str ());
1337 m_function = (char *) xmalloc (strlen (filename) + 1);
1338 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1339 if (*inp == '.')
1340 break;
1341 else if (*inp == '-')
1342 *outp++ = '_';
1343 else if (*inp == ' ')
1344 *outp++ = '_';
1345 else
1346 *outp++ = *inp;
1347 *outp = '\0';
1349 /* Standard boilerplate. */
1350 gdb_printf ("/* THIS FILE IS GENERATED. "
1351 "-*- buffer-read-only: t -*- vi"
1352 ":set ro:\n");
1355 ~print_c_tdesc ()
1357 xfree (m_function);
1360 void visit_pre (const target_desc *e) override
1362 gdb_printf (" Original: %s */\n\n",
1363 lbasename (m_filename_after_features.c_str ()));
1365 gdb_printf ("#include \"defs.h\"\n");
1366 gdb_printf ("#include \"osabi.h\"\n");
1367 gdb_printf ("#include \"target-descriptions.h\"\n");
1368 gdb_printf ("\n");
1370 gdb_printf ("struct target_desc *tdesc_%s;\n", m_function);
1371 gdb_printf ("static void\n");
1372 gdb_printf ("initialize_tdesc_%s (void)\n", m_function);
1373 gdb_printf ("{\n");
1374 gdb_printf
1375 (" target_desc_up result = allocate_target_description ();\n");
1377 if (tdesc_architecture (e) != NULL)
1379 gdb_printf
1380 (" set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
1381 tdesc_architecture (e)->printable_name);
1382 gdb_printf ("\n");
1384 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1385 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1387 gdb_printf
1388 (" set_tdesc_osabi (result.get (), osabi_from_tdesc_string (\"%s\"));\n",
1389 gdbarch_osabi_name (tdesc_osabi (e)));
1390 gdb_printf ("\n");
1393 for (const tdesc_compatible_info_up &compatible : e->compatible)
1394 gdb_printf
1395 (" tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
1396 compatible->arch ()->printable_name);
1398 if (!e->compatible.empty ())
1399 gdb_printf ("\n");
1401 for (const property &prop : e->properties)
1402 gdb_printf (" set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
1403 prop.key.c_str (), prop.value.c_str ());
1405 gdb_printf (" struct tdesc_feature *feature;\n");
1408 void visit_pre (const tdesc_feature *e) override
1410 gdb_printf ("\n feature = tdesc_create_feature (result.get (), \"%s\");\n",
1411 e->name.c_str ());
1414 void visit_post (const tdesc_feature *e) override
1417 void visit_post (const target_desc *e) override
1419 gdb_printf ("\n tdesc_%s = result.release ();\n", m_function);
1420 gdb_printf ("}\n");
1423 void visit (const tdesc_type_builtin *type) override
1425 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1428 void visit (const tdesc_type_vector *type) override
1430 if (!m_printed_element_type)
1432 gdb_printf (" tdesc_type *element_type;\n");
1433 m_printed_element_type = true;
1436 gdb_printf
1437 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1438 type->element_type->name.c_str ());
1439 gdb_printf
1440 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1441 type->name.c_str (), type->count);
1443 gdb_printf ("\n");
1446 void visit (const tdesc_type_with_fields *type) override
1448 if (!m_printed_type_with_fields)
1450 gdb_printf (" tdesc_type_with_fields *type_with_fields;\n");
1451 m_printed_type_with_fields = true;
1454 switch (type->kind)
1456 case TDESC_TYPE_STRUCT:
1457 case TDESC_TYPE_FLAGS:
1458 if (type->kind == TDESC_TYPE_STRUCT)
1460 gdb_printf
1461 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1462 type->name.c_str ());
1463 if (type->size != 0)
1464 gdb_printf
1465 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
1467 else
1469 gdb_printf
1470 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1471 type->name.c_str (), type->size);
1473 for (const tdesc_type_field &f : type->fields)
1475 const char *type_name;
1477 gdb_assert (f.type != NULL);
1478 type_name = f.type->name.c_str ();
1480 /* To minimize changes to generated files, don't emit type
1481 info for fields that have defaulted types. */
1482 if (f.start != -1)
1484 gdb_assert (f.end != -1);
1485 if (f.type->kind == TDESC_TYPE_BOOL)
1487 gdb_assert (f.start == f.end);
1488 gdb_printf
1489 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1490 f.start, f.name.c_str ());
1492 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1493 || (type->size == 8
1494 && f.type->kind == TDESC_TYPE_UINT64))
1496 gdb_printf
1497 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1498 f.name.c_str (), f.start, f.end);
1500 else
1502 printf_field_type_assignment
1503 ("tdesc_named_type (feature, \"%s\");\n",
1504 type_name);
1505 gdb_printf
1506 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1507 " %d, %d, field_type);\n",
1508 f.name.c_str (), f.start, f.end);
1511 else /* Not a bitfield. */
1513 gdb_assert (f.end == -1);
1514 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1515 printf_field_type_assignment
1516 ("tdesc_named_type (feature, \"%s\");\n", type_name);
1517 gdb_printf
1518 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1519 f.name.c_str ());
1522 break;
1523 case TDESC_TYPE_UNION:
1524 gdb_printf
1525 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1526 type->name.c_str ());
1527 for (const tdesc_type_field &f : type->fields)
1529 printf_field_type_assignment
1530 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
1531 gdb_printf
1532 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1533 f.name.c_str ());
1535 break;
1536 case TDESC_TYPE_ENUM:
1537 gdb_printf
1538 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1539 type->name.c_str (), type->size);
1540 for (const tdesc_type_field &f : type->fields)
1541 gdb_printf
1542 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1543 f.start, f.name.c_str ());
1544 break;
1545 default:
1546 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1549 gdb_printf ("\n");
1552 void visit (const tdesc_reg *reg) override
1554 gdb_printf (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1555 reg->name.c_str (), reg->target_regnum,
1556 reg->save_restore);
1557 if (!reg->group.empty ())
1558 gdb_printf ("\"%s\", ", reg->group.c_str ());
1559 else
1560 gdb_printf ("NULL, ");
1561 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1564 protected:
1565 std::string m_filename_after_features;
1567 private:
1569 /* Print an assignment to the field_type variable. Print the declaration
1570 of field_type if that has not been done yet. */
1571 ATTRIBUTE_PRINTF (2, 3)
1572 void printf_field_type_assignment (const char *fmt, ...)
1574 if (!m_printed_field_type)
1576 gdb_printf (" tdesc_type *field_type;\n");
1577 m_printed_field_type = true;
1580 gdb_printf (" field_type = ");
1582 va_list args;
1583 va_start (args, fmt);
1584 gdb_vprintf (fmt, args);
1585 va_end (args);
1588 char *m_function;
1590 /* Did we print "struct tdesc_type *element_type;" yet? */
1591 bool m_printed_element_type = false;
1593 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1594 bool m_printed_type_with_fields = false;
1596 /* Did we print "struct tdesc_type *field_type;" yet? */
1597 bool m_printed_field_type = false;
1600 /* Print target description feature in C. */
1602 class print_c_feature : public print_c_tdesc
1604 public:
1605 print_c_feature (std::string &file)
1606 : print_c_tdesc (file)
1608 /* Trim ".tmp". */
1609 auto const pos = m_filename_after_features.find_last_of ('.');
1611 m_filename_after_features = m_filename_after_features.substr (0, pos);
1614 void visit_pre (const target_desc *e) override
1616 gdb_printf (" Original: %s */\n\n",
1617 lbasename (m_filename_after_features.c_str ()));
1619 gdb_printf ("#include \"gdbsupport/tdesc.h\"\n");
1620 gdb_printf ("\n");
1623 void visit_post (const target_desc *e) override
1626 void visit_pre (const tdesc_feature *e) override
1628 std::string name (m_filename_after_features);
1630 auto pos = name.find_first_of ('.');
1632 name = name.substr (0, pos);
1633 std::replace (name.begin (), name.end (), '/', '_');
1634 std::replace (name.begin (), name.end (), '-', '_');
1636 gdb_printf ("static int\n");
1637 gdb_printf ("create_feature_%s ", name.c_str ());
1638 gdb_printf ("(struct target_desc *result, long regnum)\n");
1640 gdb_printf ("{\n");
1641 gdb_printf (" struct tdesc_feature *feature;\n");
1643 gdb_printf
1644 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1645 e->name.c_str ());
1648 void visit_post (const tdesc_feature *e) override
1650 gdb_printf (" return regnum;\n");
1651 gdb_printf ("}\n");
1654 void visit (const tdesc_reg *reg) override
1656 /* Most "reg" in XML target descriptions don't have "regnum"
1657 attribute, so the register number is allocated sequentially.
1658 In case that reg has "regnum" attribute, register number
1659 should be set by that explicitly. */
1661 if (reg->target_regnum < m_next_regnum)
1663 /* The integrity check, it can catch some errors on register
1664 number collision, like this,
1666 <reg name="x0" bitsize="32"/>
1667 <reg name="x1" bitsize="32"/>
1668 <reg name="x2" bitsize="32"/>
1669 <reg name="x3" bitsize="32"/>
1670 <reg name="ps" bitsize="32" regnum="3"/>
1672 but it also has false negatives. The target description
1673 below is correct,
1675 <reg name="x1" bitsize="32" regnum="1"/>
1676 <reg name="x3" bitsize="32" regnum="3"/>
1677 <reg name="x2" bitsize="32" regnum="2"/>
1678 <reg name="x4" bitsize="32" regnum="4"/>
1680 but it is not a good practice, so still error on this,
1681 and also print the message so that it can be saved in the
1682 generated c file. */
1684 gdb_printf ("ERROR: \"regnum\" attribute %ld ",
1685 reg->target_regnum);
1686 gdb_printf ("is not the largest number (%d).\n",
1687 m_next_regnum);
1688 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1689 reg->target_regnum, m_next_regnum);
1692 if (reg->target_regnum > m_next_regnum)
1694 gdb_printf (" regnum = %ld;\n", reg->target_regnum);
1695 m_next_regnum = reg->target_regnum;
1698 gdb_printf (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1699 reg->name.c_str (), reg->save_restore);
1700 if (!reg->group.empty ())
1701 gdb_printf ("\"%s\", ", reg->group.c_str ());
1702 else
1703 gdb_printf ("NULL, ");
1704 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1706 m_next_regnum++;
1709 private:
1710 /* The register number to use for the next register we see. */
1711 int m_next_regnum = 0;
1714 /* See gdbsupport/tdesc.h. */
1716 const char *
1717 tdesc_get_features_xml (const target_desc *tdesc)
1719 if (tdesc->xmltarget == nullptr)
1721 std::string buffer ("@");
1722 print_xml_feature v (&buffer);
1723 tdesc->accept (v);
1724 tdesc->xmltarget = xstrdup (buffer.c_str ());
1726 return tdesc->xmltarget;
1729 /* Data structures and functions to setup the option flags for 'maintenance
1730 print c-tdesc command. */
1732 struct maint_print_c_tdesc_options
1734 /* True when the '-single-feature' flag was passed. */
1735 bool single_feature = false;
1738 using maint_print_c_tdesc_opt_def
1739 = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
1741 static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
1742 maint_print_c_tdesc_opt_def {
1743 "single-feature",
1744 [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
1745 N_("Print C description of just a single feature.")
1749 static inline gdb::option::option_def_group
1750 make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
1752 return {{maint_print_c_tdesc_opt_defs}, opts};
1755 /* Implement 'maintenance print c-tdesc' command. */
1757 static void
1758 maint_print_c_tdesc_cmd (const char *args, int from_tty)
1760 const struct target_desc *tdesc;
1761 const char *filename;
1763 maint_print_c_tdesc_options opts;
1764 auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
1765 gdb::option::process_options
1766 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1768 if (args == NULL)
1770 /* Use the global target-supplied description, not the current
1771 architecture's. This lets a GDB for one architecture generate C
1772 for another architecture's description, even though the gdbarch
1773 initialization code will reject the new description. */
1774 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
1775 tdesc = tdesc_info->tdesc;
1776 filename = tdesc_info->filename.data ();
1778 else
1780 /* Use the target description from the XML file. */
1781 filename = args;
1782 tdesc = file_read_description_xml (filename);
1785 if (tdesc == NULL)
1786 error (_("There is no target description to print."));
1788 if (filename == NULL)
1789 filename = "fetched from target";
1791 std::string filename_after_features (filename);
1792 auto loc = filename_after_features.rfind ("/features/");
1794 if (loc != std::string::npos)
1795 filename_after_features = filename_after_features.substr (loc + 10);
1797 /* Print c files for target features instead of target descriptions,
1798 because c files got from target features are more flexible than the
1799 counterparts. */
1800 if (opts.single_feature)
1802 if (tdesc->features.size () != 1)
1803 error (_("only target descriptions with 1 feature can be used "
1804 "with -single-feature option"));
1806 print_c_feature v (filename_after_features);
1808 tdesc->accept (v);
1810 else
1812 print_c_tdesc v (filename_after_features);
1814 tdesc->accept (v);
1818 /* Completer for the "backtrace" command. */
1820 static void
1821 maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
1822 completion_tracker &tracker,
1823 const char *text, const char *word)
1825 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1826 if (gdb::option::complete_options
1827 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1828 return;
1830 word = advance_to_filename_complete_word_point (tracker, text);
1831 filename_completer (ignore, tracker, text, word);
1834 /* Implement the maintenance print xml-tdesc command. */
1836 static void
1837 maint_print_xml_tdesc_cmd (const char *args, int from_tty)
1839 const struct target_desc *tdesc;
1841 if (args == NULL)
1843 /* Use the global target-supplied description, not the current
1844 architecture's. This lets a GDB for one architecture generate XML
1845 for another architecture's description, even though the gdbarch
1846 initialization code will reject the new description. */
1847 tdesc = get_tdesc_info (current_inferior ())->tdesc;
1849 else
1851 /* Use the target description from the XML file. */
1852 tdesc = file_read_description_xml (args);
1855 if (tdesc == NULL)
1856 error (_("There is no target description to print."));
1858 std::string buf;
1859 print_xml_feature v (&buf);
1860 tdesc->accept (v);
1861 gdb_puts (buf.c_str ());
1864 namespace selftests {
1866 /* A reference target description, used for testing (see record_xml_tdesc). */
1868 struct xml_test_tdesc
1870 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
1871 : name (name), tdesc (std::move (tdesc))
1874 const char *name;
1875 std::unique_ptr<const target_desc> tdesc;
1878 static std::vector<xml_test_tdesc> xml_tdesc;
1880 #if GDB_SELF_TEST
1882 /* See target-descriptions.h. */
1884 void
1885 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1887 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
1889 #endif
1893 /* Test the conversion process of a target description to/from xml: Take a target
1894 description TDESC, convert to xml, back to a description, and confirm the new
1895 tdesc is identical to the original. */
1896 static bool
1897 maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1899 const char *xml = tdesc_get_features_xml (tdesc);
1901 if (xml == nullptr || *xml != '@')
1903 gdb_printf (_("Could not convert description for %s to xml.\n"),
1904 name);
1905 return false;
1908 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1910 if (tdesc_trans == nullptr)
1912 gdb_printf (_("Could not convert description for %s from xml.\n"),
1913 name);
1914 return false;
1916 else if (*tdesc != *tdesc_trans)
1918 gdb_printf (_("Converted description for %s does not match.\n"),
1919 name);
1920 return false;
1922 return true;
1926 /* Check that the target descriptions created dynamically by
1927 architecture-specific code equal the descriptions created from XML files
1928 found in the specified directory DIR. */
1930 static void
1931 maintenance_check_xml_descriptions (const char *dir, int from_tty)
1933 if (dir == NULL)
1934 error (_("Missing dir name"));
1936 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1937 std::string feature_dir (dir1.get ());
1938 unsigned int failed = 0;
1940 for (auto const &e : selftests::xml_tdesc)
1942 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
1943 const target_desc *tdesc
1944 = file_read_description_xml (tdesc_xml.data ());
1946 if (tdesc == NULL || *tdesc != *e.tdesc)
1948 gdb_printf ( _("Descriptions for %s do not match.\n"), e.name);
1949 failed++;
1951 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
1952 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
1953 failed++;
1955 gdb_printf (_("Tested %lu XML files, %d failed\n"),
1956 (long) selftests::xml_tdesc.size (), failed);
1959 void _initialize_target_descriptions ();
1960 void
1961 _initialize_target_descriptions ()
1963 cmd_list_element *cmd;
1965 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1967 add_setshow_prefix_cmd ("tdesc", class_maintenance,
1968 _("Set target description specific variables."),
1969 _("Show target description specific variables."),
1970 &tdesc_set_cmdlist, &tdesc_show_cmdlist,
1971 &setlist, &showlist);
1973 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
1974 Unset target description specific variables."),
1975 &tdesc_unset_cmdlist,
1976 0 /* allow-unknown */, &unsetlist);
1978 add_setshow_filename_cmd ("filename", class_obscure,
1979 &tdesc_filename_cmd_string,
1980 _("\
1981 Set the file to read for an XML target description."), _("\
1982 Show the file to read for an XML target description."), _("\
1983 When set, GDB will read the target description from a local\n\
1984 file instead of querying the remote target."),
1985 set_tdesc_filename_cmd,
1986 show_tdesc_filename_cmd,
1987 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1989 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1990 Unset the file to read for an XML target description.\n\
1991 When unset, GDB will read the description from the target."),
1992 &tdesc_unset_cmdlist);
1994 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1995 static std::string help_text
1996 = gdb::option::build_help (_("\
1997 Print the current target description as a C source file.\n\
1998 Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
2000 Options:\n\
2001 %OPTIONS%\n\
2003 When FILENAME is not provided then print the current target\n\
2004 description, otherwise an XML target description is read from\n\
2005 FILENAME and printed as a C function.\n\
2007 When '-single-feature' is used then the target description should\n\
2008 contain a single feature and the generated C code will only create\n\
2009 that feature within an already existing target_desc object."), grp);
2010 cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
2011 help_text.c_str (), &maintenanceprintlist);
2012 set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
2014 cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
2015 Print the current target description as an XML file."),
2016 &maintenanceprintlist);
2017 set_cmd_completer (cmd, filename_completer);
2019 cmd = add_cmd ("xml-descriptions", class_maintenance,
2020 maintenance_check_xml_descriptions, _("\
2021 Check equality of GDB target descriptions and XML created descriptions.\n\
2022 Check the target descriptions created in GDB equal the descriptions\n\
2023 created from XML files in the directory.\n\
2024 The parameter is the directory name."),
2025 &maintenancechecklist);
2026 set_cmd_completer (cmd, filename_completer);