1 /* Target description support for GDB.
3 Copyright (C) 2006-2020 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/>. */
23 #include "arch-utils.h"
26 #include "reggroups.h"
28 #include "target-descriptions.h"
29 #include "xml-support.h"
30 #include "xml-tdesc.h"
33 #include "gdb_obstack.h"
37 #include "completer.h"
38 #include "readline/tilde.h" /* tilde_expand */
44 property (const std::string
&key_
, const std::string
&value_
)
45 : key (key_
), value (value_
)
52 /* Convert a tdesc_type to a gdb type. */
55 make_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*ttype
)
57 class gdb_type_creator
: public tdesc_element_visitor
60 gdb_type_creator (struct gdbarch
*gdbarch
)
69 void visit (const tdesc_type_builtin
*e
) override
73 /* Predefined types. */
75 m_type
= builtin_type (m_gdbarch
)->builtin_bool
;
78 m_type
= builtin_type (m_gdbarch
)->builtin_int8
;
80 case TDESC_TYPE_INT16
:
81 m_type
= builtin_type (m_gdbarch
)->builtin_int16
;
83 case TDESC_TYPE_INT32
:
84 m_type
= builtin_type (m_gdbarch
)->builtin_int32
;
86 case TDESC_TYPE_INT64
:
87 m_type
= builtin_type (m_gdbarch
)->builtin_int64
;
89 case TDESC_TYPE_INT128
:
90 m_type
= builtin_type (m_gdbarch
)->builtin_int128
;
92 case TDESC_TYPE_UINT8
:
93 m_type
= builtin_type (m_gdbarch
)->builtin_uint8
;
95 case TDESC_TYPE_UINT16
:
96 m_type
= builtin_type (m_gdbarch
)->builtin_uint16
;
98 case TDESC_TYPE_UINT32
:
99 m_type
= builtin_type (m_gdbarch
)->builtin_uint32
;
101 case TDESC_TYPE_UINT64
:
102 m_type
= builtin_type (m_gdbarch
)->builtin_uint64
;
104 case TDESC_TYPE_UINT128
:
105 m_type
= builtin_type (m_gdbarch
)->builtin_uint128
;
107 case TDESC_TYPE_CODE_PTR
:
108 m_type
= builtin_type (m_gdbarch
)->builtin_func_ptr
;
110 case TDESC_TYPE_DATA_PTR
:
111 m_type
= builtin_type (m_gdbarch
)->builtin_data_ptr
;
115 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
121 case TDESC_TYPE_IEEE_HALF
:
122 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_half",
123 floatformats_ieee_half
);
126 case TDESC_TYPE_IEEE_SINGLE
:
127 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_single",
128 floatformats_ieee_single
);
131 case TDESC_TYPE_IEEE_DOUBLE
:
132 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_double",
133 floatformats_ieee_double
);
135 case TDESC_TYPE_ARM_FPA_EXT
:
136 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_arm_ext",
137 floatformats_arm_ext
);
140 case TDESC_TYPE_I387_EXT
:
141 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_i387_ext",
142 floatformats_i387_ext
);
146 internal_error (__FILE__
, __LINE__
,
147 "Type \"%s\" has an unknown kind %d",
148 e
->name
.c_str (), e
->kind
);
151 void visit (const tdesc_type_vector
*e
) override
153 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
157 type
*element_gdb_type
= make_gdb_type (m_gdbarch
, e
->element_type
);
158 m_type
= init_vector_type (element_gdb_type
, e
->count
);
159 m_type
->set_name (xstrdup (e
->name
.c_str ()));
163 void visit (const tdesc_type_with_fields
*e
) override
165 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
171 case TDESC_TYPE_STRUCT
:
172 make_gdb_type_struct (e
);
174 case TDESC_TYPE_UNION
:
175 make_gdb_type_union (e
);
177 case TDESC_TYPE_FLAGS
:
178 make_gdb_type_flags (e
);
180 case TDESC_TYPE_ENUM
:
181 make_gdb_type_enum (e
);
185 internal_error (__FILE__
, __LINE__
,
186 "Type \"%s\" has an unknown kind %d",
187 e
->name
.c_str (), e
->kind
);
192 void make_gdb_type_struct (const tdesc_type_with_fields
*e
)
194 m_type
= arch_composite_type (m_gdbarch
, NULL
, TYPE_CODE_STRUCT
);
195 m_type
->set_name (xstrdup (e
->name
.c_str ()));
197 for (const tdesc_type_field
&f
: e
->fields
)
199 if (f
.start
!= -1 && f
.end
!= -1)
203 struct type
*field_gdb_type
;
204 int bitsize
, total_size
;
206 /* This invariant should be preserved while creating types. */
207 gdb_assert (e
->size
!= 0);
209 field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
210 else if (e
->size
> 4)
211 field_gdb_type
= builtin_type (m_gdbarch
)->builtin_uint64
;
213 field_gdb_type
= builtin_type (m_gdbarch
)->builtin_uint32
;
215 fld
= append_composite_type_field_raw
216 (m_type
, xstrdup (f
.name
.c_str ()), field_gdb_type
);
218 /* For little-endian, BITPOS counts from the LSB of
219 the structure and marks the LSB of the field. For
220 big-endian, BITPOS counts from the MSB of the
221 structure and marks the MSB of the field. Either
222 way, it is the number of bits to the "left" of the
223 field. To calculate this in big-endian, we need
224 the total size of the structure. */
225 bitsize
= f
.end
- f
.start
+ 1;
226 total_size
= e
->size
* TARGET_CHAR_BIT
;
227 if (gdbarch_byte_order (m_gdbarch
) == BFD_ENDIAN_BIG
)
228 SET_FIELD_BITPOS (fld
[0], total_size
- f
.start
- bitsize
);
230 SET_FIELD_BITPOS (fld
[0], f
.start
);
231 FIELD_BITSIZE (fld
[0]) = bitsize
;
235 gdb_assert (f
.start
== -1 && f
.end
== -1);
236 type
*field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
237 append_composite_type_field (m_type
,
238 xstrdup (f
.name
.c_str ()),
244 TYPE_LENGTH (m_type
) = e
->size
;
247 void make_gdb_type_union (const tdesc_type_with_fields
*e
)
249 m_type
= arch_composite_type (m_gdbarch
, NULL
, TYPE_CODE_UNION
);
250 m_type
->set_name (xstrdup (e
->name
.c_str ()));
252 for (const tdesc_type_field
&f
: e
->fields
)
254 type
* field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
255 append_composite_type_field (m_type
, xstrdup (f
.name
.c_str ()),
258 /* If any of the children of a union are vectors, flag the
259 union as a vector also. This allows e.g. a union of two
260 vector types to show up automatically in "info vector". */
261 if (TYPE_VECTOR (field_gdb_type
))
262 TYPE_VECTOR (m_type
) = 1;
266 void make_gdb_type_flags (const tdesc_type_with_fields
*e
)
268 m_type
= arch_flags_type (m_gdbarch
, e
->name
.c_str (),
269 e
->size
* TARGET_CHAR_BIT
);
271 for (const tdesc_type_field
&f
: e
->fields
)
273 int bitsize
= f
.end
- f
.start
+ 1;
275 gdb_assert (f
.type
!= NULL
);
276 type
*field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
277 append_flags_type_field (m_type
, f
.start
, bitsize
,
278 field_gdb_type
, f
.name
.c_str ());
282 void make_gdb_type_enum (const tdesc_type_with_fields
*e
)
284 m_type
= arch_type (m_gdbarch
, TYPE_CODE_ENUM
, e
->size
* TARGET_CHAR_BIT
,
287 TYPE_UNSIGNED (m_type
) = 1;
288 for (const tdesc_type_field
&f
: e
->fields
)
291 = append_composite_type_field_raw (m_type
,
292 xstrdup (f
.name
.c_str ()),
295 SET_FIELD_BITPOS (fld
[0], f
.start
);
299 /* The gdbarch used. */
300 struct gdbarch
*m_gdbarch
;
302 /* The type created. */
306 gdb_type_creator
gdb_type (gdbarch
);
307 ttype
->accept (gdb_type
);
308 return gdb_type
.get_type ();
311 /* Wrapper around bfd_arch_info_type. A class with this name is used in
312 the API that is shared between gdb and gdbserver code, but gdbserver
313 doesn't use compatibility information, so its version of this class is
316 class tdesc_compatible_info
320 explicit tdesc_compatible_info (const bfd_arch_info_type
*arch
)
324 /* Access the contained pointer. */
325 const bfd_arch_info_type
*arch () const
329 /* Architecture information looked up from the <compatible> entity within
330 a target description. */
331 const bfd_arch_info_type
*m_arch
;
334 /* A target description. */
336 struct target_desc
: tdesc_element
341 virtual ~target_desc () = default;
343 target_desc (const target_desc
&) = delete;
344 void operator= (const target_desc
&) = delete;
346 /* The architecture reported by the target, if any. */
347 const struct bfd_arch_info
*arch
= NULL
;
349 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
351 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
353 /* The list of compatible architectures reported by the target. */
354 std::vector
<tdesc_compatible_info_up
> compatible
;
356 /* Any architecture-specific properties specified by the target. */
357 std::vector
<property
> properties
;
359 /* The features associated with this target. */
360 std::vector
<tdesc_feature_up
> features
;
362 /* Used to cache the generated xml version of the target description. */
363 mutable char *xmltarget
= nullptr;
365 void accept (tdesc_element_visitor
&v
) const override
369 for (const tdesc_feature_up
&feature
: features
)
375 bool operator== (const target_desc
&other
) const
377 if (arch
!= other
.arch
)
380 if (osabi
!= other
.osabi
)
383 if (features
.size () != other
.features
.size ())
386 for (int ix
= 0; ix
< features
.size (); ix
++)
388 const tdesc_feature_up
&feature1
= features
[ix
];
389 const tdesc_feature_up
&feature2
= other
.features
[ix
];
391 if (feature1
!= feature2
&& *feature1
!= *feature2
)
398 bool operator!= (const target_desc
&other
) const
400 return !(*this == other
);
404 /* Per-architecture data associated with a target description. The
405 target description may be shared by multiple architectures, but
406 this data is private to one gdbarch. */
408 struct tdesc_arch_reg
410 tdesc_arch_reg (tdesc_reg
*reg_
, struct type
*type_
)
411 : reg (reg_
), type (type_
)
414 struct tdesc_reg
*reg
;
418 struct tdesc_arch_data
420 /* A list of register/type pairs, indexed by GDB's internal register number.
421 During initialization of the gdbarch this list is used to store
422 registers which the architecture assigns a fixed register number.
423 Registers which are NULL in this array, or off the end, are
424 treated as zero-sized and nameless (i.e. placeholders in the
426 std::vector
<tdesc_arch_reg
> arch_regs
;
428 /* Functions which report the register name, type, and reggroups for
430 gdbarch_register_name_ftype
*pseudo_register_name
= NULL
;
431 gdbarch_register_type_ftype
*pseudo_register_type
= NULL
;
432 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
= NULL
;
435 /* Info about an inferior's target description. There's one of these
436 for each inferior. */
438 struct target_desc_info
440 /* A flag indicating that a description has already been fetched
441 from the target, so it should not be queried again. */
445 /* The description fetched from the target, or NULL if the target
446 did not supply any description. Only valid when
447 target_desc_fetched is set. Only the description initialization
448 code should access this; normally, the description should be
449 accessed through the gdbarch object. */
451 const struct target_desc
*tdesc
;
453 /* The filename to read a target description from, as set by "set
454 tdesc filename ..." */
459 /* Get the inferior INF's target description info, allocating one on
460 the stop if necessary. */
462 static struct target_desc_info
*
463 get_tdesc_info (struct inferior
*inf
)
465 if (inf
->tdesc_info
== NULL
)
466 inf
->tdesc_info
= XCNEW (struct target_desc_info
);
467 return inf
->tdesc_info
;
470 /* A handle for architecture-specific data associated with the
471 target description (see struct tdesc_arch_data). */
473 static struct gdbarch_data
*tdesc_data
;
475 /* See target-descriptions.h. */
478 target_desc_info_from_user_p (struct target_desc_info
*info
)
480 return info
!= NULL
&& info
->filename
!= NULL
;
483 /* See target-descriptions.h. */
486 copy_inferior_target_desc_info (struct inferior
*destinf
, struct inferior
*srcinf
)
488 struct target_desc_info
*src
= get_tdesc_info (srcinf
);
489 struct target_desc_info
*dest
= get_tdesc_info (destinf
);
491 dest
->fetched
= src
->fetched
;
492 dest
->tdesc
= src
->tdesc
;
493 dest
->filename
= src
->filename
!= NULL
? xstrdup (src
->filename
) : NULL
;
496 /* See target-descriptions.h. */
499 target_desc_info_free (struct target_desc_info
*tdesc_info
)
501 if (tdesc_info
!= NULL
)
503 xfree (tdesc_info
->filename
);
508 /* Convenience helper macros. */
510 #define target_desc_fetched \
511 get_tdesc_info (current_inferior ())->fetched
512 #define current_target_desc \
513 get_tdesc_info (current_inferior ())->tdesc
514 #define target_description_filename \
515 get_tdesc_info (current_inferior ())->filename
517 /* The string manipulated by the "set tdesc filename ..." command. */
519 static char *tdesc_filename_cmd_string
;
521 /* Fetch the current target's description, and switch the current
522 architecture to one which incorporates that description. */
525 target_find_description (void)
527 /* If we've already fetched a description from the target, don't do
528 it again. This allows a target to fetch the description early,
529 during its to_open or to_create_inferior, if it needs extra
530 information about the target to initialize. */
531 if (target_desc_fetched
)
534 /* The current architecture should not have any target description
535 specified. It should have been cleared, e.g. when we
536 disconnected from the previous target. */
537 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL
);
539 /* First try to fetch an XML description from the user-specified
541 current_target_desc
= NULL
;
542 if (target_description_filename
!= NULL
543 && *target_description_filename
!= '\0')
545 = file_read_description_xml (target_description_filename
);
547 /* Next try to read the description from the current target using
549 if (current_target_desc
== NULL
)
550 current_target_desc
= target_read_description_xml (current_top_target ());
552 /* If that failed try a target-specific hook. */
553 if (current_target_desc
== NULL
)
554 current_target_desc
= target_read_description (current_top_target ());
556 /* If a non-NULL description was returned, then update the current
558 if (current_target_desc
)
560 struct gdbarch_info info
;
562 gdbarch_info_init (&info
);
563 info
.target_desc
= current_target_desc
;
564 if (!gdbarch_update_p (info
))
565 warning (_("Architecture rejected target-supplied description"));
568 struct tdesc_arch_data
*data
;
570 data
= ((struct tdesc_arch_data
*)
571 gdbarch_data (target_gdbarch (), tdesc_data
));
572 if (tdesc_has_registers (current_target_desc
)
573 && data
->arch_regs
.empty ())
574 warning (_("Target-supplied registers are not supported "
575 "by the current architecture"));
579 /* Now that we know this description is usable, record that we
581 target_desc_fetched
= 1;
584 /* Discard any description fetched from the current target, and switch
585 the current architecture to one with no target description. */
588 target_clear_description (void)
590 struct gdbarch_info info
;
592 if (!target_desc_fetched
)
595 target_desc_fetched
= 0;
596 current_target_desc
= NULL
;
598 gdbarch_info_init (&info
);
599 if (!gdbarch_update_p (info
))
600 internal_error (__FILE__
, __LINE__
,
601 _("Could not remove target-supplied description"));
604 /* Return the global current target description. This should only be
605 used by gdbarch initialization code; most access should be through
606 an existing gdbarch. */
608 const struct target_desc
*
609 target_current_description (void)
611 if (target_desc_fetched
)
612 return current_target_desc
;
617 /* Return non-zero if this target description is compatible
618 with the given BFD architecture. */
621 tdesc_compatible_p (const struct target_desc
*target_desc
,
622 const struct bfd_arch_info
*arch
)
624 for (const tdesc_compatible_info_up
&compat
: target_desc
->compatible
)
626 if (compat
->arch () == arch
627 || arch
->compatible (arch
, compat
->arch ())
628 || compat
->arch ()->compatible (compat
->arch (), arch
))
636 /* Direct accessors for target descriptions. */
638 /* Return the string value of a property named KEY, or NULL if the
639 property was not specified. */
642 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
644 for (const property
&prop
: target_desc
->properties
)
646 return prop
.value
.c_str ();
651 /* Return the BFD architecture associated with this target
652 description, or NULL if no architecture was specified. */
654 const struct bfd_arch_info
*
655 tdesc_architecture (const struct target_desc
*target_desc
)
657 return target_desc
->arch
;
660 /* See gdbsupport/tdesc.h. */
663 tdesc_architecture_name (const struct target_desc
*target_desc
)
665 if (target_desc
->arch
!= NULL
)
666 return target_desc
->arch
->printable_name
;
670 /* See gdbsupport/tdesc.h. */
672 const std::vector
<tdesc_compatible_info_up
> &
673 tdesc_compatible_info_list (const target_desc
*target_desc
)
675 return target_desc
->compatible
;
678 /* See gdbsupport/tdesc.h. */
681 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up
&compatible
)
683 return compatible
->arch ()->printable_name
;
686 /* Return the OSABI associated with this target description, or
687 GDB_OSABI_UNKNOWN if no osabi was specified. */
690 tdesc_osabi (const struct target_desc
*target_desc
)
692 return target_desc
->osabi
;
695 /* See gdbsupport/tdesc.h. */
698 tdesc_osabi_name (const struct target_desc
*target_desc
)
700 enum gdb_osabi osabi
= tdesc_osabi (target_desc
);
701 if (osabi
> GDB_OSABI_UNKNOWN
&& osabi
< GDB_OSABI_INVALID
)
702 return gdbarch_osabi_name (osabi
);
706 /* Return 1 if this target description includes any registers. */
709 tdesc_has_registers (const struct target_desc
*target_desc
)
711 if (target_desc
== NULL
)
714 for (const tdesc_feature_up
&feature
: target_desc
->features
)
715 if (!feature
->registers
.empty ())
721 /* Return the feature with the given name, if present, or NULL if
722 the named feature is not found. */
724 const struct tdesc_feature
*
725 tdesc_find_feature (const struct target_desc
*target_desc
,
728 for (const tdesc_feature_up
&feature
: target_desc
->features
)
729 if (feature
->name
== name
)
730 return feature
.get ();
735 /* Return the name of FEATURE. */
738 tdesc_feature_name (const struct tdesc_feature
*feature
)
740 return feature
->name
.c_str ();
743 /* Lookup type associated with ID. */
746 tdesc_find_type (struct gdbarch
*gdbarch
, const char *id
)
748 tdesc_arch_data
*data
749 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
751 for (const tdesc_arch_reg
®
: data
->arch_regs
)
754 && reg
.reg
->tdesc_type
756 && reg
.reg
->tdesc_type
->name
== id
)
763 /* Support for registers from target descriptions. */
765 /* Construct the per-gdbarch data. */
768 tdesc_data_init (struct obstack
*obstack
)
770 return obstack_new
<tdesc_arch_data
> (obstack
);
773 /* Similar, but for the temporary copy used during architecture
776 struct tdesc_arch_data
*
777 tdesc_data_alloc (void)
779 return new tdesc_arch_data ();
782 /* Free something allocated by tdesc_data_alloc, if it is not going
783 to be used (for instance if it was unsuitable for the
787 tdesc_data_cleanup (void *data_untyped
)
789 struct tdesc_arch_data
*data
= (struct tdesc_arch_data
*) data_untyped
;
794 /* Search FEATURE for a register named NAME. */
796 static struct tdesc_reg
*
797 tdesc_find_register_early (const struct tdesc_feature
*feature
,
800 for (const tdesc_reg_up
®
: feature
->registers
)
801 if (strcasecmp (reg
->name
.c_str (), name
) == 0)
807 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
810 tdesc_numbered_register (const struct tdesc_feature
*feature
,
811 struct tdesc_arch_data
*data
,
812 int regno
, const char *name
)
814 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
819 /* Make sure the vector includes a REGNO'th element. */
820 while (regno
>= data
->arch_regs
.size ())
821 data
->arch_regs
.emplace_back (nullptr, nullptr);
823 data
->arch_regs
[regno
] = tdesc_arch_reg (reg
, NULL
);
828 /* Search FEATURE for a register named NAME, but do not assign a fixed
829 register number to it. */
832 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
835 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
843 /* Search FEATURE for a register whose name is in NAMES and assign
847 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
848 struct tdesc_arch_data
*data
,
849 int regno
, const char *const names
[])
853 for (i
= 0; names
[i
] != NULL
; i
++)
854 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
860 /* Search FEATURE for a register named NAME, and return its size in
861 bits. The register must exist. */
864 tdesc_register_bitsize (const struct tdesc_feature
*feature
, const char *name
)
866 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
868 gdb_assert (reg
!= NULL
);
872 /* Look up a register by its GDB internal register number. */
874 static struct tdesc_arch_reg
*
875 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
877 struct tdesc_arch_data
*data
;
879 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
880 if (regno
< data
->arch_regs
.size ())
881 return &data
->arch_regs
[regno
];
886 static struct tdesc_reg
*
887 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
889 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
891 return reg
? reg
->reg
: NULL
;
894 /* Return the name of register REGNO, from the target description or
895 from an architecture-provided pseudo_register_name method. */
898 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
900 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
901 int num_regs
= gdbarch_num_regs (gdbarch
);
904 return reg
->name
.c_str ();
906 if (regno
>= num_regs
&& regno
< gdbarch_num_cooked_regs (gdbarch
))
908 struct tdesc_arch_data
*data
909 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
911 gdb_assert (data
->pseudo_register_name
!= NULL
);
912 return data
->pseudo_register_name (gdbarch
, regno
);
919 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
921 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
922 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
923 int num_regs
= gdbarch_num_regs (gdbarch
);
924 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
926 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
928 struct tdesc_arch_data
*data
929 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
931 gdb_assert (data
->pseudo_register_type
!= NULL
);
932 return data
->pseudo_register_type (gdbarch
, regno
);
936 /* Return "int0_t", since "void" has a misleading size of one. */
937 return builtin_type (gdbarch
)->builtin_int0
;
939 if (arch_reg
->type
== NULL
)
941 /* First check for a predefined or target defined type. */
943 arch_reg
->type
= make_gdb_type (gdbarch
, reg
->tdesc_type
);
945 /* Next try size-sensitive type shortcuts. */
946 else if (reg
->type
== "float")
948 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
949 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
950 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
951 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
952 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
953 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
956 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
957 reg
->name
.c_str (), reg
->bitsize
);
958 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
961 else if (reg
->type
== "int")
963 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
964 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
965 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
966 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
967 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
968 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
969 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
970 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
971 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
972 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
973 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
974 /* A bit desperate by this point... */
975 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
978 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
979 reg
->name
.c_str (), reg
->bitsize
);
980 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
984 if (arch_reg
->type
== NULL
)
985 internal_error (__FILE__
, __LINE__
,
986 "Register \"%s\" has an unknown type \"%s\"",
987 reg
->name
.c_str (), reg
->type
.c_str ());
990 return arch_reg
->type
;
994 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
996 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
999 return reg
->target_regnum
;
1004 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1005 target description may be classified as general, float, vector or other
1006 register groups registered with reggroup_add(). Unlike a gdbarch
1007 register_reggroup_p method, this function will return -1 if it does not
1008 know; the caller should handle registers with no specified group.
1010 The names of containing features are not used. This might be extended
1011 to display registers in some more useful groupings.
1013 The save-restore flag is also implemented here. */
1016 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1017 struct reggroup
*reggroup
)
1019 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1021 if (reg
!= NULL
&& !reg
->group
.empty ()
1022 && (reg
->group
== reggroup_name (reggroup
)))
1026 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
1027 return reg
->save_restore
;
1032 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1033 group specified go to the default reggroup function and are handled
1037 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1038 struct reggroup
*reggroup
)
1040 int num_regs
= gdbarch_num_regs (gdbarch
);
1041 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1044 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1046 struct tdesc_arch_data
*data
1047 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1049 if (data
->pseudo_register_reggroup_p
!= NULL
)
1050 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
1051 /* Otherwise fall through to the default reggroup_p. */
1054 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
1058 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
1061 /* Record architecture-specific functions to call for pseudo-register
1065 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
1066 gdbarch_register_name_ftype
*pseudo_name
)
1068 struct tdesc_arch_data
*data
1069 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1071 data
->pseudo_register_name
= pseudo_name
;
1075 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
1076 gdbarch_register_type_ftype
*pseudo_type
)
1078 struct tdesc_arch_data
*data
1079 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1081 data
->pseudo_register_type
= pseudo_type
;
1085 set_tdesc_pseudo_register_reggroup_p
1086 (struct gdbarch
*gdbarch
,
1087 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
1089 struct tdesc_arch_data
*data
1090 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1092 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
1095 /* Update GDBARCH to use the target description for registers. */
1098 tdesc_use_registers (struct gdbarch
*gdbarch
,
1099 const struct target_desc
*target_desc
,
1100 struct tdesc_arch_data
*early_data
,
1101 tdesc_unknown_register_ftype unk_reg_cb
)
1103 int num_regs
= gdbarch_num_regs (gdbarch
);
1104 struct tdesc_arch_data
*data
;
1107 /* We can't use the description for registers if it doesn't describe
1108 any. This function should only be called after validating
1109 registers, so the caller should know that registers are
1111 gdb_assert (tdesc_has_registers (target_desc
));
1113 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1114 data
->arch_regs
= early_data
->arch_regs
;
1117 /* Build up a set of all registers, so that we can assign register
1118 numbers where needed. The hash table expands as necessary, so
1119 the initial size is arbitrary. */
1120 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1121 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1122 for (const tdesc_reg_up
®
: feature
->registers
)
1124 void **slot
= htab_find_slot (reg_hash
, reg
.get (), INSERT
);
1127 /* Add reggroup if its new. */
1128 if (!reg
->group
.empty ())
1129 if (reggroup_find (gdbarch
, reg
->group
.c_str ()) == NULL
)
1130 reggroup_add (gdbarch
, reggroup_gdbarch_new (gdbarch
,
1131 reg
->group
.c_str (),
1135 /* Remove any registers which were assigned numbers by the
1137 for (const tdesc_arch_reg
&arch_reg
: data
->arch_regs
)
1138 if (arch_reg
.reg
!= NULL
)
1139 htab_remove_elt (reg_hash
, arch_reg
.reg
);
1141 /* Assign numbers to the remaining registers and add them to the
1142 list of registers. The new numbers are always above gdbarch_num_regs.
1143 Iterate over the features, not the hash table, so that the order
1144 matches that in the target description. */
1146 gdb_assert (data
->arch_regs
.size () <= num_regs
);
1147 while (data
->arch_regs
.size () < num_regs
)
1148 data
->arch_regs
.emplace_back (nullptr, nullptr);
1150 /* First we give the target a chance to number previously unknown
1151 registers. This allows targets to record the numbers assigned based
1152 on which feature the register was from. */
1153 if (unk_reg_cb
!= NULL
)
1155 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1156 for (const tdesc_reg_up
®
: feature
->registers
)
1157 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1159 int regno
= unk_reg_cb (gdbarch
, feature
.get (),
1160 reg
->name
.c_str (), num_regs
);
1161 gdb_assert (regno
== -1 || regno
>= num_regs
);
1164 while (regno
>= data
->arch_regs
.size ())
1165 data
->arch_regs
.emplace_back (nullptr, nullptr);
1166 data
->arch_regs
[regno
] = tdesc_arch_reg (reg
.get (), NULL
);
1167 num_regs
= regno
+ 1;
1168 htab_remove_elt (reg_hash
, reg
.get ());
1173 /* Ensure the array was sized correctly above. */
1174 gdb_assert (data
->arch_regs
.size () == num_regs
);
1176 /* Now in a final pass we assign register numbers to any remaining
1177 unnumbered registers. */
1178 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1179 for (const tdesc_reg_up
®
: feature
->registers
)
1180 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1182 data
->arch_regs
.emplace_back (reg
.get (), nullptr);
1186 htab_delete (reg_hash
);
1188 /* Update the architecture. */
1189 set_gdbarch_num_regs (gdbarch
, num_regs
);
1190 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1191 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1192 set_gdbarch_remote_register_number (gdbarch
,
1193 tdesc_remote_register_number
);
1194 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1197 /* See gdbsupport/tdesc.h. */
1199 struct tdesc_feature
*
1200 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
)
1202 struct tdesc_feature
*new_feature
= new tdesc_feature (name
);
1204 tdesc
->features
.emplace_back (new_feature
);
1209 /* See gdbsupport/tdesc.h. */
1211 struct target_desc
*
1212 allocate_target_description (void)
1214 return new target_desc ();
1217 /* See gdbsupport/tdesc.h. */
1220 target_desc_deleter::operator() (struct target_desc
*target_desc
) const
1226 tdesc_add_compatible (struct target_desc
*target_desc
,
1227 const struct bfd_arch_info
*compatible
)
1229 /* If this instance of GDB is compiled without BFD support for the
1230 compatible architecture, simply ignore it -- we would not be able
1231 to handle it anyway. */
1232 if (compatible
== NULL
)
1235 for (const tdesc_compatible_info_up
&compat
: target_desc
->compatible
)
1236 if (compat
->arch () == compatible
)
1237 internal_error (__FILE__
, __LINE__
,
1238 _("Attempted to add duplicate "
1239 "compatible architecture \"%s\""),
1240 compatible
->printable_name
);
1242 target_desc
->compatible
.push_back
1243 (std::unique_ptr
<tdesc_compatible_info
>
1244 (new tdesc_compatible_info (compatible
)));
1248 set_tdesc_property (struct target_desc
*target_desc
,
1249 const char *key
, const char *value
)
1251 gdb_assert (key
!= NULL
&& value
!= NULL
);
1253 if (tdesc_property (target_desc
, key
) != NULL
)
1254 internal_error (__FILE__
, __LINE__
,
1255 _("Attempted to add duplicate property \"%s\""), key
);
1257 target_desc
->properties
.emplace_back (key
, value
);
1260 /* See gdbsupport/tdesc.h. */
1263 set_tdesc_architecture (struct target_desc
*target_desc
,
1266 set_tdesc_architecture (target_desc
, bfd_scan_arch (name
));
1270 set_tdesc_architecture (struct target_desc
*target_desc
,
1271 const struct bfd_arch_info
*arch
)
1273 target_desc
->arch
= arch
;
1276 /* See gdbsupport/tdesc.h. */
1279 set_tdesc_osabi (struct target_desc
*target_desc
, const char *name
)
1281 set_tdesc_osabi (target_desc
, osabi_from_tdesc_string (name
));
1285 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1287 target_desc
->osabi
= osabi
;
1291 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1292 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1294 /* Helper functions for the CLI commands. */
1297 set_tdesc_filename_cmd (const char *args
, int from_tty
,
1298 struct cmd_list_element
*c
)
1300 xfree (target_description_filename
);
1301 target_description_filename
= xstrdup (tdesc_filename_cmd_string
);
1303 target_clear_description ();
1304 target_find_description ();
1308 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1309 struct cmd_list_element
*c
,
1312 value
= target_description_filename
;
1314 if (value
!= NULL
&& *value
!= '\0')
1315 printf_filtered (_("The target description will be read from \"%s\".\n"),
1318 printf_filtered (_("The target description will be "
1319 "read from the target.\n"));
1323 unset_tdesc_filename_cmd (const char *args
, int from_tty
)
1325 xfree (target_description_filename
);
1326 target_description_filename
= NULL
;
1327 target_clear_description ();
1328 target_find_description ();
1331 /* Print target description in C. */
1333 class print_c_tdesc
: public tdesc_element_visitor
1336 print_c_tdesc (std::string
&filename_after_features
)
1337 : m_filename_after_features (filename_after_features
)
1341 const char *filename
= lbasename (m_filename_after_features
.c_str ());
1343 m_function
= (char *) xmalloc (strlen (filename
) + 1);
1344 for (inp
= filename
, outp
= m_function
; *inp
!= '\0'; inp
++)
1347 else if (*inp
== '-')
1349 else if (*inp
== ' ')
1355 /* Standard boilerplate. */
1356 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1357 "-*- buffer-read-only: t -*- vi"
1366 void visit_pre (const target_desc
*e
) override
1368 printf_unfiltered (" Original: %s */\n\n",
1369 lbasename (m_filename_after_features
.c_str ()));
1371 printf_unfiltered ("#include \"defs.h\"\n");
1372 printf_unfiltered ("#include \"osabi.h\"\n");
1373 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1374 printf_unfiltered ("\n");
1376 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function
);
1377 printf_unfiltered ("static void\n");
1378 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function
);
1379 printf_unfiltered ("{\n");
1381 (" struct target_desc *result = allocate_target_description ();\n");
1383 if (tdesc_architecture (e
) != NULL
)
1386 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1387 tdesc_architecture (e
)->printable_name
);
1388 printf_unfiltered ("\n");
1390 if (tdesc_osabi (e
) > GDB_OSABI_UNKNOWN
1391 && tdesc_osabi (e
) < GDB_OSABI_INVALID
)
1394 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1395 gdbarch_osabi_name (tdesc_osabi (e
)));
1396 printf_unfiltered ("\n");
1399 for (const tdesc_compatible_info_up
&compatible
: e
->compatible
)
1401 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1402 compatible
->arch ()->printable_name
);
1404 if (!e
->compatible
.empty ())
1405 printf_unfiltered ("\n");
1407 for (const property
&prop
: e
->properties
)
1408 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1409 prop
.key
.c_str (), prop
.value
.c_str ());
1411 printf_unfiltered (" struct tdesc_feature *feature;\n");
1414 void visit_pre (const tdesc_feature
*e
) override
1416 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1420 void visit_post (const tdesc_feature
*e
) override
1423 void visit_post (const target_desc
*e
) override
1425 printf_unfiltered ("\n tdesc_%s = result;\n", m_function
);
1426 printf_unfiltered ("}\n");
1429 void visit (const tdesc_type_builtin
*type
) override
1431 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1434 void visit (const tdesc_type_vector
*type
) override
1436 if (!m_printed_element_type
)
1438 printf_unfiltered (" tdesc_type *element_type;\n");
1439 m_printed_element_type
= true;
1443 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1444 type
->element_type
->name
.c_str ());
1446 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1447 type
->name
.c_str (), type
->count
);
1449 printf_unfiltered ("\n");
1452 void visit (const tdesc_type_with_fields
*type
) override
1454 if (!m_printed_type_with_fields
)
1456 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1457 m_printed_type_with_fields
= true;
1462 case TDESC_TYPE_STRUCT
:
1463 case TDESC_TYPE_FLAGS
:
1464 if (type
->kind
== TDESC_TYPE_STRUCT
)
1467 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1468 type
->name
.c_str ());
1469 if (type
->size
!= 0)
1471 (" tdesc_set_struct_size (type_with_fields, %d);\n", type
->size
);
1476 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1477 type
->name
.c_str (), type
->size
);
1479 for (const tdesc_type_field
&f
: type
->fields
)
1481 const char *type_name
;
1483 gdb_assert (f
.type
!= NULL
);
1484 type_name
= f
.type
->name
.c_str ();
1486 /* To minimize changes to generated files, don't emit type
1487 info for fields that have defaulted types. */
1490 gdb_assert (f
.end
!= -1);
1491 if (f
.type
->kind
== TDESC_TYPE_BOOL
)
1493 gdb_assert (f
.start
== f
.end
);
1495 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1496 f
.start
, f
.name
.c_str ());
1498 else if ((type
->size
== 4 && f
.type
->kind
== TDESC_TYPE_UINT32
)
1500 && f
.type
->kind
== TDESC_TYPE_UINT64
))
1503 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1504 f
.name
.c_str (), f
.start
, f
.end
);
1508 printf_field_type_assignment
1509 ("tdesc_named_type (feature, \"%s\");\n",
1512 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1513 " %d, %d, field_type);\n",
1514 f
.name
.c_str (), f
.start
, f
.end
);
1517 else /* Not a bitfield. */
1519 gdb_assert (f
.end
== -1);
1520 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1521 printf_field_type_assignment
1522 ("tdesc_named_type (feature, \"%s\");\n", type_name
);
1524 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1529 case TDESC_TYPE_UNION
:
1531 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1532 type
->name
.c_str ());
1533 for (const tdesc_type_field
&f
: type
->fields
)
1535 printf_field_type_assignment
1536 ("tdesc_named_type (feature, \"%s\");\n", f
.type
->name
.c_str ());
1538 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1542 case TDESC_TYPE_ENUM
:
1544 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1545 type
->name
.c_str (), type
->size
);
1546 for (const tdesc_type_field
&f
: type
->fields
)
1548 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1549 f
.start
, f
.name
.c_str ());
1552 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1555 printf_unfiltered ("\n");
1558 void visit (const tdesc_reg
*reg
) override
1560 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1561 reg
->name
.c_str (), reg
->target_regnum
,
1563 if (!reg
->group
.empty ())
1564 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
1566 printf_unfiltered ("NULL, ");
1567 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
1571 std::string m_filename_after_features
;
1575 /* Print an assignment to the field_type variable. Print the declaration
1576 of field_type if that has not been done yet. */
1577 ATTRIBUTE_PRINTF (2, 3)
1578 void printf_field_type_assignment (const char *fmt
, ...)
1580 if (!m_printed_field_type
)
1582 printf_unfiltered (" tdesc_type *field_type;\n");
1583 m_printed_field_type
= true;
1586 printf_unfiltered (" field_type = ");
1589 va_start (args
, fmt
);
1590 vprintf_unfiltered (fmt
, args
);
1596 /* Did we print "struct tdesc_type *element_type;" yet? */
1597 bool m_printed_element_type
= false;
1599 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1600 bool m_printed_type_with_fields
= false;
1602 /* Did we print "struct tdesc_type *field_type;" yet? */
1603 bool m_printed_field_type
= false;
1606 /* Print target description feature in C. */
1608 class print_c_feature
: public print_c_tdesc
1611 print_c_feature (std::string
&file
)
1612 : print_c_tdesc (file
)
1615 auto const pos
= m_filename_after_features
.find_last_of ('.');
1617 m_filename_after_features
= m_filename_after_features
.substr (0, pos
);
1620 void visit_pre (const target_desc
*e
) override
1622 printf_unfiltered (" Original: %s */\n\n",
1623 lbasename (m_filename_after_features
.c_str ()));
1625 printf_unfiltered ("#include \"gdbsupport/tdesc.h\"\n");
1626 printf_unfiltered ("\n");
1629 void visit_post (const target_desc
*e
) override
1632 void visit_pre (const tdesc_feature
*e
) override
1634 std::string
name (m_filename_after_features
);
1636 auto pos
= name
.find_first_of ('.');
1638 name
= name
.substr (0, pos
);
1639 std::replace (name
.begin (), name
.end (), '/', '_');
1640 std::replace (name
.begin (), name
.end (), '-', '_');
1642 printf_unfiltered ("static int\n");
1643 printf_unfiltered ("create_feature_%s ", name
.c_str ());
1644 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
1646 printf_unfiltered ("{\n");
1647 printf_unfiltered (" struct tdesc_feature *feature;\n");
1650 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1654 void visit_post (const tdesc_feature
*e
) override
1656 printf_unfiltered (" return regnum;\n");
1657 printf_unfiltered ("}\n");
1660 void visit (const tdesc_reg
*reg
) override
1662 /* Most "reg" in XML target descriptions don't have "regnum"
1663 attribute, so the register number is allocated sequentially.
1664 In case that reg has "regnum" attribute, register number
1665 should be set by that explicitly. */
1667 if (reg
->target_regnum
< m_next_regnum
)
1669 /* The integrity check, it can catch some errors on register
1670 number collision, like this,
1672 <reg name="x0" bitsize="32"/>
1673 <reg name="x1" bitsize="32"/>
1674 <reg name="x2" bitsize="32"/>
1675 <reg name="x3" bitsize="32"/>
1676 <reg name="ps" bitsize="32" regnum="3"/>
1678 but it also has false negatives. The target description
1681 <reg name="x1" bitsize="32" regnum="1"/>
1682 <reg name="x3" bitsize="32" regnum="3"/>
1683 <reg name="x2" bitsize="32" regnum="2"/>
1684 <reg name="x4" bitsize="32" regnum="4"/>
1686 but it is not a good practice, so still error on this,
1687 and also print the message so that it can be saved in the
1688 generated c file. */
1690 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
1691 reg
->target_regnum
);
1692 printf_unfiltered ("is not the largest number (%d).\n",
1694 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1695 reg
->target_regnum
, m_next_regnum
);
1698 if (reg
->target_regnum
> m_next_regnum
)
1700 printf_unfiltered (" regnum = %ld;\n", reg
->target_regnum
);
1701 m_next_regnum
= reg
->target_regnum
;
1704 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1705 reg
->name
.c_str (), reg
->save_restore
);
1706 if (!reg
->group
.empty ())
1707 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
1709 printf_unfiltered ("NULL, ");
1710 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
1716 /* The register number to use for the next register we see. */
1717 int m_next_regnum
= 0;
1720 /* See gdbsupport/tdesc.h. */
1723 tdesc_get_features_xml (const target_desc
*tdesc
)
1725 if (tdesc
->xmltarget
== nullptr)
1727 std::string
buffer ("@");
1728 print_xml_feature
v (&buffer
);
1730 tdesc
->xmltarget
= xstrdup (buffer
.c_str ());
1732 return tdesc
->xmltarget
;
1736 maint_print_c_tdesc_cmd (const char *args
, int from_tty
)
1738 const struct target_desc
*tdesc
;
1739 const char *filename
;
1743 /* Use the global target-supplied description, not the current
1744 architecture's. This lets a GDB for one architecture generate C
1745 for another architecture's description, even though the gdbarch
1746 initialization code will reject the new description. */
1747 tdesc
= current_target_desc
;
1748 filename
= target_description_filename
;
1752 /* Use the target description from the XML file. */
1754 tdesc
= file_read_description_xml (filename
);
1758 error (_("There is no target description to print."));
1760 if (filename
== NULL
)
1761 filename
= "fetched from target";
1763 std::string
filename_after_features (filename
);
1764 auto loc
= filename_after_features
.rfind ("/features/");
1766 if (loc
!= std::string::npos
)
1767 filename_after_features
= filename_after_features
.substr (loc
+ 10);
1769 /* Print c files for target features instead of target descriptions,
1770 because c files got from target features are more flexible than the
1772 if (startswith (filename_after_features
.c_str (), "i386/32bit-")
1773 || startswith (filename_after_features
.c_str (), "i386/64bit-")
1774 || startswith (filename_after_features
.c_str (), "i386/x32-core.xml")
1775 || startswith (filename_after_features
.c_str (), "riscv/")
1776 || startswith (filename_after_features
.c_str (), "tic6x-")
1777 || startswith (filename_after_features
.c_str (), "aarch64")
1778 || startswith (filename_after_features
.c_str (), "arm/")
1779 || startswith (filename_after_features
.c_str (), "arc/"))
1781 print_c_feature
v (filename_after_features
);
1787 print_c_tdesc
v (filename_after_features
);
1793 /* Implement the maintenance print xml-tdesc command. */
1796 maint_print_xml_tdesc_cmd (const char *args
, int from_tty
)
1798 const struct target_desc
*tdesc
;
1802 /* Use the global target-supplied description, not the current
1803 architecture's. This lets a GDB for one architecture generate XML
1804 for another architecture's description, even though the gdbarch
1805 initialization code will reject the new description. */
1806 tdesc
= current_target_desc
;
1810 /* Use the target description from the XML file. */
1811 tdesc
= file_read_description_xml (args
);
1815 error (_("There is no target description to print."));
1818 print_xml_feature
v (&buf
);
1820 puts_unfiltered (buf
.c_str ());
1823 namespace selftests
{
1825 /* A reference target description, used for testing (see record_xml_tdesc). */
1827 struct xml_test_tdesc
1829 xml_test_tdesc (const char *name
, std::unique_ptr
<const target_desc
> &&tdesc
)
1830 : name (name
), tdesc (std::move (tdesc
))
1834 std::unique_ptr
<const target_desc
> tdesc
;
1837 static std::vector
<xml_test_tdesc
> xml_tdesc
;
1841 /* See target-descriptions.h. */
1844 record_xml_tdesc (const char *xml_file
, const struct target_desc
*tdesc
)
1846 xml_tdesc
.emplace_back (xml_file
, std::unique_ptr
<const target_desc
> (tdesc
));
1852 /* Test the conversion process of a target description to/from xml: Take a target
1853 description TDESC, convert to xml, back to a description, and confirm the new
1854 tdesc is identical to the original. */
1856 maintenance_check_tdesc_xml_convert (const target_desc
*tdesc
, const char *name
)
1858 const char *xml
= tdesc_get_features_xml (tdesc
);
1860 if (xml
== nullptr || *xml
!= '@')
1862 printf_filtered (_("Could not convert description for %s to xml.\n"),
1867 const target_desc
*tdesc_trans
= string_read_description_xml (xml
+ 1);
1869 if (tdesc_trans
== nullptr)
1871 printf_filtered (_("Could not convert description for %s from xml.\n"),
1875 else if (*tdesc
!= *tdesc_trans
)
1877 printf_filtered (_("Converted description for %s does not match.\n"),
1885 /* Check that the target descriptions created dynamically by
1886 architecture-specific code equal the descriptions created from XML files
1887 found in the specified directory DIR. */
1890 maintenance_check_xml_descriptions (const char *dir
, int from_tty
)
1893 error (_("Missing dir name"));
1895 gdb::unique_xmalloc_ptr
<char> dir1 (tilde_expand (dir
));
1896 std::string
feature_dir (dir1
.get ());
1897 unsigned int failed
= 0;
1899 for (auto const &e
: selftests::xml_tdesc
)
1901 std::string tdesc_xml
= (feature_dir
+ SLASH_STRING
+ e
.name
);
1902 const target_desc
*tdesc
1903 = file_read_description_xml (tdesc_xml
.data ());
1905 if (tdesc
== NULL
|| *tdesc
!= *e
.tdesc
)
1907 printf_filtered ( _("Descriptions for %s do not match.\n"), e
.name
);
1910 else if (!maintenance_check_tdesc_xml_convert (tdesc
, e
.name
)
1911 || !maintenance_check_tdesc_xml_convert (e
.tdesc
.get (), e
.name
))
1914 printf_filtered (_("Tested %lu XML files, %d failed\n"),
1915 (long) selftests::xml_tdesc
.size (), failed
);
1918 void _initialize_target_descriptions ();
1920 _initialize_target_descriptions ()
1922 cmd_list_element
*cmd
;
1924 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
1926 add_basic_prefix_cmd ("tdesc", class_maintenance
, _("\
1927 Set target description specific variables."),
1928 &tdesc_set_cmdlist
, "set tdesc ",
1929 0 /* allow-unknown */, &setlist
);
1930 add_show_prefix_cmd ("tdesc", class_maintenance
, _("\
1931 Show target description specific variables."),
1932 &tdesc_show_cmdlist
, "show tdesc ",
1933 0 /* allow-unknown */, &showlist
);
1934 add_basic_prefix_cmd ("tdesc", class_maintenance
, _("\
1935 Unset target description specific variables."),
1936 &tdesc_unset_cmdlist
, "unset tdesc ",
1937 0 /* allow-unknown */, &unsetlist
);
1939 add_setshow_filename_cmd ("filename", class_obscure
,
1940 &tdesc_filename_cmd_string
,
1942 Set the file to read for an XML target description."), _("\
1943 Show the file to read for an XML target description."), _("\
1944 When set, GDB will read the target description from a local\n\
1945 file instead of querying the remote target."),
1946 set_tdesc_filename_cmd
,
1947 show_tdesc_filename_cmd
,
1948 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
1950 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
1951 Unset the file to read for an XML target description.\n\
1952 When unset, GDB will read the description from the target."),
1953 &tdesc_unset_cmdlist
);
1955 cmd
= add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
1956 Print the current target description as a C source file."),
1957 &maintenanceprintlist
);
1958 set_cmd_completer (cmd
, filename_completer
);
1960 cmd
= add_cmd ("xml-tdesc", class_maintenance
, maint_print_xml_tdesc_cmd
, _("\
1961 Print the current target description as an XML file."),
1962 &maintenanceprintlist
);
1963 set_cmd_completer (cmd
, filename_completer
);
1965 cmd
= add_cmd ("xml-descriptions", class_maintenance
,
1966 maintenance_check_xml_descriptions
, _("\
1967 Check equality of GDB target descriptions and XML created descriptions.\n\
1968 Check the target descriptions created in GDB equal the descriptions\n\
1969 created from XML files in the directory.\n\
1970 The parameter is the directory name."),
1971 &maintenancechecklist
);
1972 set_cmd_completer (cmd
, filename_completer
);