1 /* Target description support for GDB.
3 Copyright (C) 2006-2017 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"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
34 #include "gdb_obstack.h"
38 #include "completer.h"
39 #include "readline/tilde.h" /* tilde_expand */
41 /* The interface to visit different elements of target description. */
43 class tdesc_element_visitor
46 virtual void visit_pre (const target_desc
*e
) = 0;
47 virtual void visit_post (const target_desc
*e
) = 0;
49 virtual void visit_pre (const tdesc_feature
*e
) = 0;
50 virtual void visit_post (const tdesc_feature
*e
) = 0;
52 virtual void visit (const tdesc_type
*e
) = 0;
53 virtual void visit (const tdesc_reg
*e
) = 0;
59 virtual void accept (tdesc_element_visitor
&v
) const = 0;
66 property (const std::string
&key_
, const std::string
&value_
)
67 : key (key_
), value (value_
)
74 /* An individual register from a target description. */
76 struct tdesc_reg
: tdesc_element
78 tdesc_reg (struct tdesc_feature
*feature
, const std::string
&name_
,
79 int regnum
, int save_restore_
, const char *group_
,
80 int bitsize_
, const char *type_
)
81 : name (name_
), target_regnum (regnum
),
82 save_restore (save_restore_
),
83 group (group_
!= NULL
? group_
: ""),
85 type (type_
!= NULL
? type_
: "<unknown>")
87 /* If the register's type is target-defined, look it up now. We may not
88 have easy access to the containing feature when we want it later. */
89 tdesc_type
= tdesc_named_type (feature
, type
.c_str ());
92 virtual ~tdesc_reg () = default;
94 DISABLE_COPY_AND_ASSIGN (tdesc_reg
);
96 /* The name of this register. In standard features, it may be
97 recognized by the architecture support code, or it may be purely
101 /* The register number used by this target to refer to this
102 register. This is used for remote p/P packets and to determine
103 the ordering of registers in the remote g/G packets. */
106 /* If this flag is set, GDB should save and restore this register
107 around calls to an inferior function. */
110 /* The name of the register group containing this register, or empty
111 if the group should be automatically determined from the
112 register's type. If this is "general", "float", or "vector", the
113 corresponding "info" command should display this register's
114 value. It can be an arbitrary string, but should be limited to
115 alphanumeric characters and internal hyphens. Currently other
116 strings are ignored (treated as empty). */
119 /* The size of the register, in bits. */
122 /* The type of the register. This string corresponds to either
123 a named type from the target description or a predefined
127 /* The target-described type corresponding to TYPE, if found. */
128 struct tdesc_type
*tdesc_type
;
130 void accept (tdesc_element_visitor
&v
) const override
135 bool operator== (const tdesc_reg
&other
) const
137 return (name
== other
.name
138 && target_regnum
== other
.target_regnum
139 && save_restore
== other
.save_restore
140 && bitsize
== other
.bitsize
141 && group
== other
.group
142 && type
== other
.type
);
145 bool operator!= (const tdesc_reg
&other
) const
147 return !(*this == other
);
151 typedef std::unique_ptr
<tdesc_reg
> tdesc_reg_up
;
153 /* A named type from a target description. */
155 typedef struct tdesc_type_field
158 struct tdesc_type
*type
;
159 /* For non-enum-values, either both are -1 (non-bitfield), or both are
160 not -1 (bitfield). For enum values, start is the value (which could be
164 DEF_VEC_O(tdesc_type_field
);
168 /* Predefined types. */
182 TDESC_TYPE_IEEE_SINGLE
,
183 TDESC_TYPE_IEEE_DOUBLE
,
184 TDESC_TYPE_ARM_FPA_EXT
,
187 /* Types defined by a target feature. */
195 typedef struct tdesc_type
: tdesc_element
197 tdesc_type (const char *name_
, enum tdesc_type_kind kind_
)
198 : name (xstrdup (name_
)), kind (kind_
)
200 memset (&u
, 0, sizeof (u
));
203 virtual ~tdesc_type ()
207 case TDESC_TYPE_STRUCT
:
208 case TDESC_TYPE_UNION
:
209 case TDESC_TYPE_FLAGS
:
210 case TDESC_TYPE_ENUM
:
212 struct tdesc_type_field
*f
;
216 VEC_iterate (tdesc_type_field
, u
.u
.fields
, ix
, f
);
220 VEC_free (tdesc_type_field
, u
.u
.fields
);
227 xfree ((char *) name
);
230 DISABLE_COPY_AND_ASSIGN (tdesc_type
);
232 /* The name of this type. If this type is a built-in type, this is
233 a pointer to a constant string. Otherwise, it's a
234 malloc-allocated string (and thus must be freed). */
237 /* Identify the kind of this type. */
238 enum tdesc_type_kind kind
;
240 /* Kind-specific data. */
246 struct tdesc_type
*type
;
250 /* Struct, union, flags, or enum type. */
253 VEC(tdesc_type_field
) *fields
;
258 void accept (tdesc_element_visitor
&v
) const override
263 bool operator== (const tdesc_type
&other
) const
265 return (streq (name
, other
.name
) && kind
== other
.kind
);
268 bool operator!= (const tdesc_type
&other
) const
270 return !(*this == other
);
273 DEF_VEC_P(tdesc_type_p
);
275 /* A feature from a target description. Each feature is a collection
276 of other elements, e.g. registers and types. */
278 struct tdesc_feature
: tdesc_element
280 tdesc_feature (const std::string
&name_
)
284 virtual ~tdesc_feature ()
286 struct tdesc_type
*type
;
289 for (ix
= 0; VEC_iterate (tdesc_type_p
, types
, ix
, type
); ix
++)
291 VEC_free (tdesc_type_p
, types
);
294 DISABLE_COPY_AND_ASSIGN (tdesc_feature
);
296 /* The name of this feature. It may be recognized by the architecture
300 /* The registers associated with this feature. */
301 std::vector
<std::unique_ptr
<tdesc_reg
>> registers
;
303 /* The types associated with this feature. */
304 VEC(tdesc_type_p
) *types
= NULL
;
306 void accept (tdesc_element_visitor
&v
) const override
310 struct tdesc_type
*type
;
313 VEC_iterate (tdesc_type_p
, types
, ix
, type
);
317 for (const tdesc_reg_up
®
: registers
)
323 bool operator== (const tdesc_feature
&other
) const
325 if (name
!= other
.name
)
328 if (registers
.size () != other
.registers
.size ())
331 for (int ix
= 0; ix
< registers
.size (); ix
++)
333 const tdesc_reg_up
®1
= registers
[ix
];
334 const tdesc_reg_up
®2
= other
.registers
[ix
];
336 if (reg1
!= reg2
&& *reg1
!= *reg2
)
340 if (VEC_length (tdesc_type_p
, types
)
341 != VEC_length (tdesc_type_p
, other
.types
))
347 VEC_iterate (tdesc_type_p
, types
, ix
, type
);
351 = VEC_index (tdesc_type_p
, other
.types
, ix
);
353 if (type
!= type2
&& *type
!= *type2
)
360 bool operator!= (const tdesc_feature
&other
) const
362 return !(*this == other
);
367 typedef std::unique_ptr
<tdesc_feature
> tdesc_feature_up
;
369 /* A target description. */
371 struct target_desc
: tdesc_element
376 virtual ~target_desc () = default;
378 target_desc (const target_desc
&) = delete;
379 void operator= (const target_desc
&) = delete;
381 /* The architecture reported by the target, if any. */
382 const struct bfd_arch_info
*arch
= NULL
;
384 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
386 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
388 /* The list of compatible architectures reported by the target. */
389 std::vector
<const bfd_arch_info
*> compatible
;
391 /* Any architecture-specific properties specified by the target. */
392 std::vector
<property
> properties
;
394 /* The features associated with this target. */
395 std::vector
<std::unique_ptr
<tdesc_feature
>> features
;
397 void accept (tdesc_element_visitor
&v
) const override
401 for (const tdesc_feature_up
&feature
: features
)
407 bool operator== (const target_desc
&other
) const
409 if (arch
!= other
.arch
)
412 if (osabi
!= other
.osabi
)
415 if (features
.size () != other
.features
.size ())
418 for (int ix
= 0; ix
< features
.size (); ix
++)
420 const tdesc_feature_up
&feature1
= features
[ix
];
421 const tdesc_feature_up
&feature2
= other
.features
[ix
];
423 if (feature1
!= feature2
&& *feature1
!= *feature2
)
430 bool operator!= (const target_desc
&other
) const
432 return !(*this == other
);
436 /* Per-architecture data associated with a target description. The
437 target description may be shared by multiple architectures, but
438 this data is private to one gdbarch. */
440 typedef struct tdesc_arch_reg
442 struct tdesc_reg
*reg
;
445 DEF_VEC_O(tdesc_arch_reg
);
447 struct tdesc_arch_data
449 /* A list of register/type pairs, indexed by GDB's internal register number.
450 During initialization of the gdbarch this list is used to store
451 registers which the architecture assigns a fixed register number.
452 Registers which are NULL in this array, or off the end, are
453 treated as zero-sized and nameless (i.e. placeholders in the
455 VEC(tdesc_arch_reg
) *arch_regs
;
457 /* Functions which report the register name, type, and reggroups for
459 gdbarch_register_name_ftype
*pseudo_register_name
;
460 gdbarch_register_type_ftype
*pseudo_register_type
;
461 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
;
464 /* Info about an inferior's target description. There's one of these
465 for each inferior. */
467 struct target_desc_info
469 /* A flag indicating that a description has already been fetched
470 from the target, so it should not be queried again. */
474 /* The description fetched from the target, or NULL if the target
475 did not supply any description. Only valid when
476 target_desc_fetched is set. Only the description initialization
477 code should access this; normally, the description should be
478 accessed through the gdbarch object. */
480 const struct target_desc
*tdesc
;
482 /* The filename to read a target description from, as set by "set
483 tdesc filename ..." */
488 /* Get the inferior INF's target description info, allocating one on
489 the stop if necessary. */
491 static struct target_desc_info
*
492 get_tdesc_info (struct inferior
*inf
)
494 if (inf
->tdesc_info
== NULL
)
495 inf
->tdesc_info
= XCNEW (struct target_desc_info
);
496 return inf
->tdesc_info
;
499 /* A handle for architecture-specific data associated with the
500 target description (see struct tdesc_arch_data). */
502 static struct gdbarch_data
*tdesc_data
;
504 /* See target-descriptions.h. */
507 target_desc_info_from_user_p (struct target_desc_info
*info
)
509 return info
!= NULL
&& info
->filename
!= NULL
;
512 /* See target-descriptions.h. */
515 copy_inferior_target_desc_info (struct inferior
*destinf
, struct inferior
*srcinf
)
517 struct target_desc_info
*src
= get_tdesc_info (srcinf
);
518 struct target_desc_info
*dest
= get_tdesc_info (destinf
);
520 dest
->fetched
= src
->fetched
;
521 dest
->tdesc
= src
->tdesc
;
522 dest
->filename
= src
->filename
!= NULL
? xstrdup (src
->filename
) : NULL
;
525 /* See target-descriptions.h. */
528 target_desc_info_free (struct target_desc_info
*tdesc_info
)
530 if (tdesc_info
!= NULL
)
532 xfree (tdesc_info
->filename
);
537 /* Convenience helper macros. */
539 #define target_desc_fetched \
540 get_tdesc_info (current_inferior ())->fetched
541 #define current_target_desc \
542 get_tdesc_info (current_inferior ())->tdesc
543 #define target_description_filename \
544 get_tdesc_info (current_inferior ())->filename
546 /* The string manipulated by the "set tdesc filename ..." command. */
548 static char *tdesc_filename_cmd_string
;
550 /* Fetch the current target's description, and switch the current
551 architecture to one which incorporates that description. */
554 target_find_description (void)
556 /* If we've already fetched a description from the target, don't do
557 it again. This allows a target to fetch the description early,
558 during its to_open or to_create_inferior, if it needs extra
559 information about the target to initialize. */
560 if (target_desc_fetched
)
563 /* The current architecture should not have any target description
564 specified. It should have been cleared, e.g. when we
565 disconnected from the previous target. */
566 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL
);
568 /* First try to fetch an XML description from the user-specified
570 current_target_desc
= NULL
;
571 if (target_description_filename
!= NULL
572 && *target_description_filename
!= '\0')
574 = file_read_description_xml (target_description_filename
);
576 /* Next try to read the description from the current target using
578 if (current_target_desc
== NULL
)
579 current_target_desc
= target_read_description_xml (¤t_target
);
581 /* If that failed try a target-specific hook. */
582 if (current_target_desc
== NULL
)
583 current_target_desc
= target_read_description (¤t_target
);
585 /* If a non-NULL description was returned, then update the current
587 if (current_target_desc
)
589 struct gdbarch_info info
;
591 gdbarch_info_init (&info
);
592 info
.target_desc
= current_target_desc
;
593 if (!gdbarch_update_p (info
))
594 warning (_("Architecture rejected target-supplied description"));
597 struct tdesc_arch_data
*data
;
599 data
= ((struct tdesc_arch_data
*)
600 gdbarch_data (target_gdbarch (), tdesc_data
));
601 if (tdesc_has_registers (current_target_desc
)
602 && data
->arch_regs
== NULL
)
603 warning (_("Target-supplied registers are not supported "
604 "by the current architecture"));
608 /* Now that we know this description is usable, record that we
610 target_desc_fetched
= 1;
613 /* Discard any description fetched from the current target, and switch
614 the current architecture to one with no target description. */
617 target_clear_description (void)
619 struct gdbarch_info info
;
621 if (!target_desc_fetched
)
624 target_desc_fetched
= 0;
625 current_target_desc
= NULL
;
627 gdbarch_info_init (&info
);
628 if (!gdbarch_update_p (info
))
629 internal_error (__FILE__
, __LINE__
,
630 _("Could not remove target-supplied description"));
633 /* Return the global current target description. This should only be
634 used by gdbarch initialization code; most access should be through
635 an existing gdbarch. */
637 const struct target_desc
*
638 target_current_description (void)
640 if (target_desc_fetched
)
641 return current_target_desc
;
646 /* Return non-zero if this target description is compatible
647 with the given BFD architecture. */
650 tdesc_compatible_p (const struct target_desc
*target_desc
,
651 const struct bfd_arch_info
*arch
)
653 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
656 || arch
->compatible (arch
, compat
)
657 || compat
->compatible (compat
, arch
))
665 /* Direct accessors for target descriptions. */
667 /* Return the string value of a property named KEY, or NULL if the
668 property was not specified. */
671 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
673 for (const property
&prop
: target_desc
->properties
)
675 return prop
.value
.c_str ();
680 /* Return the BFD architecture associated with this target
681 description, or NULL if no architecture was specified. */
683 const struct bfd_arch_info
*
684 tdesc_architecture (const struct target_desc
*target_desc
)
686 return target_desc
->arch
;
689 /* Return the OSABI associated with this target description, or
690 GDB_OSABI_UNKNOWN if no osabi was specified. */
693 tdesc_osabi (const struct target_desc
*target_desc
)
695 return target_desc
->osabi
;
700 /* Return 1 if this target description includes any registers. */
703 tdesc_has_registers (const struct target_desc
*target_desc
)
705 if (target_desc
== NULL
)
708 for (const tdesc_feature_up
&feature
: target_desc
->features
)
709 if (!feature
->registers
.empty ())
715 /* Return the feature with the given name, if present, or NULL if
716 the named feature is not found. */
718 const struct tdesc_feature
*
719 tdesc_find_feature (const struct target_desc
*target_desc
,
722 for (const tdesc_feature_up
&feature
: target_desc
->features
)
723 if (feature
->name
== name
)
724 return feature
.get ();
729 /* Return the name of FEATURE. */
732 tdesc_feature_name (const struct tdesc_feature
*feature
)
734 return feature
->name
.c_str ();
737 /* Predefined types. */
738 static struct tdesc_type tdesc_predefined_types
[] =
740 { "bool", TDESC_TYPE_BOOL
},
741 { "int8", TDESC_TYPE_INT8
},
742 { "int16", TDESC_TYPE_INT16
},
743 { "int32", TDESC_TYPE_INT32
},
744 { "int64", TDESC_TYPE_INT64
},
745 { "int128", TDESC_TYPE_INT128
},
746 { "uint8", TDESC_TYPE_UINT8
},
747 { "uint16", TDESC_TYPE_UINT16
},
748 { "uint32", TDESC_TYPE_UINT32
},
749 { "uint64", TDESC_TYPE_UINT64
},
750 { "uint128", TDESC_TYPE_UINT128
},
751 { "code_ptr", TDESC_TYPE_CODE_PTR
},
752 { "data_ptr", TDESC_TYPE_DATA_PTR
},
753 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
754 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
755 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
},
756 { "i387_ext", TDESC_TYPE_I387_EXT
}
759 /* Lookup a predefined type. */
761 static struct tdesc_type
*
762 tdesc_predefined_type (enum tdesc_type_kind kind
)
764 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
765 if (tdesc_predefined_types
[ix
].kind
== kind
)
766 return &tdesc_predefined_types
[ix
];
768 gdb_assert_not_reached ("bad predefined tdesc type");
771 /* See arch/tdesc.h. */
774 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
777 struct tdesc_type
*type
;
779 /* First try target-defined types. */
780 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
781 if (strcmp (type
->name
, id
) == 0)
784 /* Next try the predefined types. */
785 for (ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
786 if (strcmp (tdesc_predefined_types
[ix
].name
, id
) == 0)
787 return &tdesc_predefined_types
[ix
];
792 /* Lookup type associated with ID. */
795 tdesc_find_type (struct gdbarch
*gdbarch
, const char *id
)
797 struct tdesc_arch_reg
*reg
;
798 struct tdesc_arch_data
*data
;
801 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
802 num_regs
= VEC_length (tdesc_arch_reg
, data
->arch_regs
);
803 for (i
= 0; i
< num_regs
; i
++)
805 reg
= VEC_index (tdesc_arch_reg
, data
->arch_regs
, i
);
807 && reg
->reg
->tdesc_type
809 && strcmp (id
, reg
->reg
->tdesc_type
->name
) == 0)
816 /* Construct, if necessary, and return the GDB type implementing target
817 type TDESC_TYPE for architecture GDBARCH. */
820 tdesc_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*tdesc_type
)
824 switch (tdesc_type
->kind
)
826 /* Predefined types. */
827 case TDESC_TYPE_BOOL
:
828 return builtin_type (gdbarch
)->builtin_bool
;
830 case TDESC_TYPE_INT8
:
831 return builtin_type (gdbarch
)->builtin_int8
;
833 case TDESC_TYPE_INT16
:
834 return builtin_type (gdbarch
)->builtin_int16
;
836 case TDESC_TYPE_INT32
:
837 return builtin_type (gdbarch
)->builtin_int32
;
839 case TDESC_TYPE_INT64
:
840 return builtin_type (gdbarch
)->builtin_int64
;
842 case TDESC_TYPE_INT128
:
843 return builtin_type (gdbarch
)->builtin_int128
;
845 case TDESC_TYPE_UINT8
:
846 return builtin_type (gdbarch
)->builtin_uint8
;
848 case TDESC_TYPE_UINT16
:
849 return builtin_type (gdbarch
)->builtin_uint16
;
851 case TDESC_TYPE_UINT32
:
852 return builtin_type (gdbarch
)->builtin_uint32
;
854 case TDESC_TYPE_UINT64
:
855 return builtin_type (gdbarch
)->builtin_uint64
;
857 case TDESC_TYPE_UINT128
:
858 return builtin_type (gdbarch
)->builtin_uint128
;
860 case TDESC_TYPE_CODE_PTR
:
861 return builtin_type (gdbarch
)->builtin_func_ptr
;
863 case TDESC_TYPE_DATA_PTR
:
864 return builtin_type (gdbarch
)->builtin_data_ptr
;
870 type
= tdesc_find_type (gdbarch
, tdesc_type
->name
);
874 switch (tdesc_type
->kind
)
876 case TDESC_TYPE_IEEE_SINGLE
:
877 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_single",
878 floatformats_ieee_single
);
880 case TDESC_TYPE_IEEE_DOUBLE
:
881 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_double",
882 floatformats_ieee_double
);
884 case TDESC_TYPE_ARM_FPA_EXT
:
885 return arch_float_type (gdbarch
, -1, "builtin_type_arm_ext",
886 floatformats_arm_ext
);
888 case TDESC_TYPE_I387_EXT
:
889 return arch_float_type (gdbarch
, -1, "builtin_type_i387_ext",
890 floatformats_i387_ext
);
892 /* Types defined by a target feature. */
893 case TDESC_TYPE_VECTOR
:
895 struct type
*type
, *field_type
;
897 field_type
= tdesc_gdb_type (gdbarch
, tdesc_type
->u
.v
.type
);
898 type
= init_vector_type (field_type
, tdesc_type
->u
.v
.count
);
899 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
904 case TDESC_TYPE_STRUCT
:
906 struct type
*type
, *field_type
;
907 struct tdesc_type_field
*f
;
910 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
911 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
912 TYPE_TAG_NAME (type
) = TYPE_NAME (type
);
915 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
918 if (f
->start
!= -1 && f
->end
!= -1)
922 struct type
*field_type
;
923 int bitsize
, total_size
;
925 /* This invariant should be preserved while creating types. */
926 gdb_assert (tdesc_type
->u
.u
.size
!= 0);
928 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
929 else if (tdesc_type
->u
.u
.size
> 4)
930 field_type
= builtin_type (gdbarch
)->builtin_uint64
;
932 field_type
= builtin_type (gdbarch
)->builtin_uint32
;
934 fld
= append_composite_type_field_raw (type
, xstrdup (f
->name
),
937 /* For little-endian, BITPOS counts from the LSB of
938 the structure and marks the LSB of the field. For
939 big-endian, BITPOS counts from the MSB of the
940 structure and marks the MSB of the field. Either
941 way, it is the number of bits to the "left" of the
942 field. To calculate this in big-endian, we need
943 the total size of the structure. */
944 bitsize
= f
->end
- f
->start
+ 1;
945 total_size
= tdesc_type
->u
.u
.size
* TARGET_CHAR_BIT
;
946 if (gdbarch_bits_big_endian (gdbarch
))
947 SET_FIELD_BITPOS (fld
[0], total_size
- f
->start
- bitsize
);
949 SET_FIELD_BITPOS (fld
[0], f
->start
);
950 FIELD_BITSIZE (fld
[0]) = bitsize
;
954 gdb_assert (f
->start
== -1 && f
->end
== -1);
955 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
956 append_composite_type_field (type
, xstrdup (f
->name
),
961 if (tdesc_type
->u
.u
.size
!= 0)
962 TYPE_LENGTH (type
) = tdesc_type
->u
.u
.size
;
966 case TDESC_TYPE_UNION
:
968 struct type
*type
, *field_type
;
969 struct tdesc_type_field
*f
;
972 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
973 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
976 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
979 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
980 append_composite_type_field (type
, xstrdup (f
->name
), field_type
);
982 /* If any of the children of a union are vectors, flag the
983 union as a vector also. This allows e.g. a union of two
984 vector types to show up automatically in "info vector". */
985 if (TYPE_VECTOR (field_type
))
986 TYPE_VECTOR (type
) = 1;
991 case TDESC_TYPE_FLAGS
:
993 struct tdesc_type_field
*f
;
996 type
= arch_flags_type (gdbarch
, tdesc_type
->name
,
997 tdesc_type
->u
.u
.size
* TARGET_CHAR_BIT
);
999 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
1002 struct type
*field_type
;
1003 int bitsize
= f
->end
- f
->start
+ 1;
1005 gdb_assert (f
->type
!= NULL
);
1006 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
1007 append_flags_type_field (type
, f
->start
, bitsize
,
1008 field_type
, f
->name
);
1014 case TDESC_TYPE_ENUM
:
1016 struct tdesc_type_field
*f
;
1019 type
= arch_type (gdbarch
, TYPE_CODE_ENUM
,
1020 tdesc_type
->u
.u
.size
* TARGET_CHAR_BIT
,
1022 TYPE_UNSIGNED (type
) = 1;
1024 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
1028 = append_composite_type_field_raw (type
, xstrdup (f
->name
),
1031 SET_FIELD_BITPOS (fld
[0], f
->start
);
1038 internal_error (__FILE__
, __LINE__
,
1039 "Type \"%s\" has an unknown kind %d",
1040 tdesc_type
->name
, tdesc_type
->kind
);
1044 /* Support for registers from target descriptions. */
1046 /* Construct the per-gdbarch data. */
1049 tdesc_data_init (struct obstack
*obstack
)
1051 struct tdesc_arch_data
*data
;
1053 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
1057 /* Similar, but for the temporary copy used during architecture
1060 struct tdesc_arch_data
*
1061 tdesc_data_alloc (void)
1063 return XCNEW (struct tdesc_arch_data
);
1066 /* Free something allocated by tdesc_data_alloc, if it is not going
1067 to be used (for instance if it was unsuitable for the
1071 tdesc_data_cleanup (void *data_untyped
)
1073 struct tdesc_arch_data
*data
= (struct tdesc_arch_data
*) data_untyped
;
1075 VEC_free (tdesc_arch_reg
, data
->arch_regs
);
1079 /* Search FEATURE for a register named NAME. */
1081 static struct tdesc_reg
*
1082 tdesc_find_register_early (const struct tdesc_feature
*feature
,
1085 for (const tdesc_reg_up
®
: feature
->registers
)
1086 if (strcasecmp (reg
->name
.c_str (), name
) == 0)
1092 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1095 tdesc_numbered_register (const struct tdesc_feature
*feature
,
1096 struct tdesc_arch_data
*data
,
1097 int regno
, const char *name
)
1099 struct tdesc_arch_reg arch_reg
= { 0 };
1100 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1105 /* Make sure the vector includes a REGNO'th element. */
1106 while (regno
>= VEC_length (tdesc_arch_reg
, data
->arch_regs
))
1107 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &arch_reg
);
1110 VEC_replace (tdesc_arch_reg
, data
->arch_regs
, regno
, &arch_reg
);
1114 /* Search FEATURE for a register named NAME, but do not assign a fixed
1115 register number to it. */
1118 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
1121 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1129 /* Search FEATURE for a register whose name is in NAMES and assign
1133 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
1134 struct tdesc_arch_data
*data
,
1135 int regno
, const char *const names
[])
1139 for (i
= 0; names
[i
] != NULL
; i
++)
1140 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
1146 /* Search FEATURE for a register named NAME, and return its size in
1147 bits. The register must exist. */
1150 tdesc_register_size (const struct tdesc_feature
*feature
,
1153 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1155 gdb_assert (reg
!= NULL
);
1156 return reg
->bitsize
;
1159 /* Look up a register by its GDB internal register number. */
1161 static struct tdesc_arch_reg
*
1162 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
1164 struct tdesc_arch_data
*data
;
1166 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1167 if (regno
< VEC_length (tdesc_arch_reg
, data
->arch_regs
))
1168 return VEC_index (tdesc_arch_reg
, data
->arch_regs
, regno
);
1173 static struct tdesc_reg
*
1174 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
1176 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
1178 return reg
? reg
->reg
: NULL
;
1181 /* Return the name of register REGNO, from the target description or
1182 from an architecture-provided pseudo_register_name method. */
1185 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
1187 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1188 int num_regs
= gdbarch_num_regs (gdbarch
);
1189 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1192 return reg
->name
.c_str ();
1194 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1196 struct tdesc_arch_data
*data
1197 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1199 gdb_assert (data
->pseudo_register_name
!= NULL
);
1200 return data
->pseudo_register_name (gdbarch
, regno
);
1207 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
1209 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
1210 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
1211 int num_regs
= gdbarch_num_regs (gdbarch
);
1212 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1214 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1216 struct tdesc_arch_data
*data
1217 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1219 gdb_assert (data
->pseudo_register_type
!= NULL
);
1220 return data
->pseudo_register_type (gdbarch
, regno
);
1224 /* Return "int0_t", since "void" has a misleading size of one. */
1225 return builtin_type (gdbarch
)->builtin_int0
;
1227 if (arch_reg
->type
== NULL
)
1229 /* First check for a predefined or target defined type. */
1230 if (reg
->tdesc_type
)
1231 arch_reg
->type
= tdesc_gdb_type (gdbarch
, reg
->tdesc_type
);
1233 /* Next try size-sensitive type shortcuts. */
1234 else if (reg
->type
== "float")
1236 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
1237 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
1238 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
1239 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1240 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
1241 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
1244 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1245 reg
->name
.c_str (), reg
->bitsize
);
1246 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1249 else if (reg
->type
== "int")
1251 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
1252 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1253 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
1254 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
1255 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
1256 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
1257 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
1258 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
1259 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
1260 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
1261 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
1262 /* A bit desperate by this point... */
1263 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
1266 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1267 reg
->name
.c_str (), reg
->bitsize
);
1268 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1272 if (arch_reg
->type
== NULL
)
1273 internal_error (__FILE__
, __LINE__
,
1274 "Register \"%s\" has an unknown type \"%s\"",
1275 reg
->name
.c_str (), reg
->type
.c_str ());
1278 return arch_reg
->type
;
1282 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
1284 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1287 return reg
->target_regnum
;
1292 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1293 target description may be classified as general, float, or vector.
1294 Unlike a gdbarch register_reggroup_p method, this function will
1295 return -1 if it does not know; the caller should handle registers
1296 with no specified group.
1298 Arbitrary strings (other than "general", "float", and "vector")
1299 from the description are not used; they cause the register to be
1300 displayed in "info all-registers" but excluded from "info
1301 registers" et al. The names of containing features are also not
1302 used. This might be extended to display registers in some more
1305 The save-restore flag is also implemented here. */
1308 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1309 struct reggroup
*reggroup
)
1311 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1313 if (reg
!= NULL
&& !reg
->group
.empty ())
1315 int general_p
= 0, float_p
= 0, vector_p
= 0;
1317 if (reg
->group
== "general")
1319 else if (reg
->group
== "float")
1321 else if (reg
->group
== "vector")
1324 if (reggroup
== float_reggroup
)
1327 if (reggroup
== vector_reggroup
)
1330 if (reggroup
== general_reggroup
)
1335 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
1336 return reg
->save_restore
;
1341 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1342 group specified go to the default reggroup function and are handled
1346 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1347 struct reggroup
*reggroup
)
1349 int num_regs
= gdbarch_num_regs (gdbarch
);
1350 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1353 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1355 struct tdesc_arch_data
*data
1356 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1358 if (data
->pseudo_register_reggroup_p
!= NULL
)
1359 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
1360 /* Otherwise fall through to the default reggroup_p. */
1363 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
1367 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
1370 /* Record architecture-specific functions to call for pseudo-register
1374 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
1375 gdbarch_register_name_ftype
*pseudo_name
)
1377 struct tdesc_arch_data
*data
1378 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1380 data
->pseudo_register_name
= pseudo_name
;
1384 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
1385 gdbarch_register_type_ftype
*pseudo_type
)
1387 struct tdesc_arch_data
*data
1388 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1390 data
->pseudo_register_type
= pseudo_type
;
1394 set_tdesc_pseudo_register_reggroup_p
1395 (struct gdbarch
*gdbarch
,
1396 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
1398 struct tdesc_arch_data
*data
1399 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1401 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
1404 /* Update GDBARCH to use the target description for registers. */
1407 tdesc_use_registers (struct gdbarch
*gdbarch
,
1408 const struct target_desc
*target_desc
,
1409 struct tdesc_arch_data
*early_data
)
1411 int num_regs
= gdbarch_num_regs (gdbarch
);
1412 struct tdesc_arch_data
*data
;
1413 struct tdesc_arch_reg
*arch_reg
, new_arch_reg
= { 0 };
1416 /* We can't use the description for registers if it doesn't describe
1417 any. This function should only be called after validating
1418 registers, so the caller should know that registers are
1420 gdb_assert (tdesc_has_registers (target_desc
));
1422 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1423 data
->arch_regs
= early_data
->arch_regs
;
1426 /* Build up a set of all registers, so that we can assign register
1427 numbers where needed. The hash table expands as necessary, so
1428 the initial size is arbitrary. */
1429 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1430 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1431 for (const tdesc_reg_up
®
: feature
->registers
)
1433 void **slot
= htab_find_slot (reg_hash
, reg
.get (), INSERT
);
1438 /* Remove any registers which were assigned numbers by the
1441 VEC_iterate (tdesc_arch_reg
, data
->arch_regs
, ixr
, arch_reg
);
1444 htab_remove_elt (reg_hash
, arch_reg
->reg
);
1446 /* Assign numbers to the remaining registers and add them to the
1447 list of registers. The new numbers are always above gdbarch_num_regs.
1448 Iterate over the features, not the hash table, so that the order
1449 matches that in the target description. */
1451 gdb_assert (VEC_length (tdesc_arch_reg
, data
->arch_regs
) <= num_regs
);
1452 while (VEC_length (tdesc_arch_reg
, data
->arch_regs
) < num_regs
)
1453 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1454 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1455 for (const tdesc_reg_up
®
: feature
->registers
)
1456 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1458 new_arch_reg
.reg
= reg
.get ();
1459 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1463 htab_delete (reg_hash
);
1465 /* Update the architecture. */
1466 set_gdbarch_num_regs (gdbarch
, num_regs
);
1467 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1468 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1469 set_gdbarch_remote_register_number (gdbarch
,
1470 tdesc_remote_register_number
);
1471 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1475 /* See arch/tdesc.h. */
1478 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
1479 int regnum
, int save_restore
, const char *group
,
1480 int bitsize
, const char *type
)
1482 tdesc_reg
*reg
= new tdesc_reg (feature
, name
, regnum
, save_restore
,
1483 group
, bitsize
, type
);
1485 feature
->registers
.emplace_back (reg
);
1488 /* See arch/tdesc.h. */
1491 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1492 struct tdesc_type
*field_type
, int count
)
1494 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_VECTOR
);
1496 type
->u
.v
.type
= field_type
;
1497 type
->u
.v
.count
= count
;
1499 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1503 /* See arch/tdesc.h. */
1506 tdesc_create_struct (struct tdesc_feature
*feature
, const char *name
)
1508 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_STRUCT
);
1510 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1514 /* See arch/tdesc.h. */
1517 tdesc_set_struct_size (struct tdesc_type
*type
, int size
)
1519 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1520 gdb_assert (size
> 0);
1521 type
->u
.u
.size
= size
;
1524 /* See arch/tdesc.h. */
1527 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1529 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_UNION
);
1531 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1535 /* See arch/tdesc.h. */
1538 tdesc_create_flags (struct tdesc_feature
*feature
, const char *name
,
1541 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_FLAGS
);
1543 gdb_assert (size
> 0);
1545 type
->u
.u
.size
= size
;
1547 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1552 tdesc_create_enum (struct tdesc_feature
*feature
, const char *name
,
1555 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_ENUM
);
1557 gdb_assert (size
> 0);
1559 type
->u
.u
.size
= size
;
1561 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1565 /* See arch/tdesc.h. */
1568 tdesc_add_field (struct tdesc_type
*type
, const char *field_name
,
1569 struct tdesc_type
*field_type
)
1571 struct tdesc_type_field f
= { 0 };
1573 gdb_assert (type
->kind
== TDESC_TYPE_UNION
1574 || type
->kind
== TDESC_TYPE_STRUCT
);
1576 f
.name
= xstrdup (field_name
);
1577 f
.type
= field_type
;
1578 /* Initialize these values so we know this is not a bit-field
1579 when we print-c-tdesc. */
1583 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1587 tdesc_add_typed_bitfield (struct tdesc_type
*type
, const char *field_name
,
1588 int start
, int end
, struct tdesc_type
*field_type
)
1590 struct tdesc_type_field f
= { 0 };
1592 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
1593 || type
->kind
== TDESC_TYPE_FLAGS
);
1594 gdb_assert (start
>= 0 && end
>= start
);
1596 f
.name
= xstrdup (field_name
);
1599 f
.type
= field_type
;
1601 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1604 /* See arch/tdesc.h. */
1607 tdesc_add_bitfield (struct tdesc_type
*type
, const char *field_name
,
1610 struct tdesc_type
*field_type
;
1612 gdb_assert (start
>= 0 && end
>= start
);
1614 if (type
->u
.u
.size
> 4)
1615 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT64
);
1617 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT32
);
1619 tdesc_add_typed_bitfield (type
, field_name
, start
, end
, field_type
);
1622 /* See arch/tdesc.h. */
1625 tdesc_add_flag (struct tdesc_type
*type
, int start
,
1626 const char *flag_name
)
1628 struct tdesc_type_field f
= { 0 };
1630 gdb_assert (type
->kind
== TDESC_TYPE_FLAGS
1631 || type
->kind
== TDESC_TYPE_STRUCT
);
1633 f
.name
= xstrdup (flag_name
);
1636 f
.type
= tdesc_predefined_type (TDESC_TYPE_BOOL
);
1638 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1642 tdesc_add_enum_value (struct tdesc_type
*type
, int value
,
1645 struct tdesc_type_field f
= { 0 };
1647 gdb_assert (type
->kind
== TDESC_TYPE_ENUM
);
1649 f
.name
= xstrdup (name
);
1652 f
.type
= tdesc_predefined_type (TDESC_TYPE_INT32
);
1654 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1657 /* See arch/tdesc.h. */
1659 struct tdesc_feature
*
1660 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
,
1663 struct tdesc_feature
*new_feature
= new tdesc_feature (name
);
1665 tdesc
->features
.emplace_back (new_feature
);
1670 struct target_desc
*
1671 allocate_target_description (void)
1673 return new target_desc ();
1677 free_target_description (void *arg
)
1679 struct target_desc
*target_desc
= (struct target_desc
*) arg
;
1685 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1687 return make_cleanup (free_target_description
, target_desc
);
1691 tdesc_add_compatible (struct target_desc
*target_desc
,
1692 const struct bfd_arch_info
*compatible
)
1694 /* If this instance of GDB is compiled without BFD support for the
1695 compatible architecture, simply ignore it -- we would not be able
1696 to handle it anyway. */
1697 if (compatible
== NULL
)
1700 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
1701 if (compat
== compatible
)
1702 internal_error (__FILE__
, __LINE__
,
1703 _("Attempted to add duplicate "
1704 "compatible architecture \"%s\""),
1705 compatible
->printable_name
);
1707 target_desc
->compatible
.push_back (compatible
);
1711 set_tdesc_property (struct target_desc
*target_desc
,
1712 const char *key
, const char *value
)
1714 gdb_assert (key
!= NULL
&& value
!= NULL
);
1716 if (tdesc_property (target_desc
, key
) != NULL
)
1717 internal_error (__FILE__
, __LINE__
,
1718 _("Attempted to add duplicate property \"%s\""), key
);
1720 target_desc
->properties
.emplace_back (key
, value
);
1723 /* See arch/tdesc.h. */
1726 set_tdesc_architecture (struct target_desc
*target_desc
,
1729 set_tdesc_architecture (target_desc
, bfd_scan_arch (name
));
1733 set_tdesc_architecture (struct target_desc
*target_desc
,
1734 const struct bfd_arch_info
*arch
)
1736 target_desc
->arch
= arch
;
1739 /* See arch/tdesc.h. */
1742 set_tdesc_osabi (struct target_desc
*target_desc
, const char *name
)
1744 set_tdesc_osabi (target_desc
, osabi_from_tdesc_string (name
));
1748 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1750 target_desc
->osabi
= osabi
;
1754 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1755 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1757 /* Helper functions for the CLI commands. */
1760 set_tdesc_cmd (const char *args
, int from_tty
)
1762 help_list (tdesc_set_cmdlist
, "set tdesc ", all_commands
, gdb_stdout
);
1766 show_tdesc_cmd (const char *args
, int from_tty
)
1768 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1772 unset_tdesc_cmd (const char *args
, int from_tty
)
1774 help_list (tdesc_unset_cmdlist
, "unset tdesc ", all_commands
, gdb_stdout
);
1778 set_tdesc_filename_cmd (const char *args
, int from_tty
,
1779 struct cmd_list_element
*c
)
1781 xfree (target_description_filename
);
1782 target_description_filename
= xstrdup (tdesc_filename_cmd_string
);
1784 target_clear_description ();
1785 target_find_description ();
1789 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1790 struct cmd_list_element
*c
,
1793 value
= target_description_filename
;
1795 if (value
!= NULL
&& *value
!= '\0')
1796 printf_filtered (_("The target description will be read from \"%s\".\n"),
1799 printf_filtered (_("The target description will be "
1800 "read from the target.\n"));
1804 unset_tdesc_filename_cmd (const char *args
, int from_tty
)
1806 xfree (target_description_filename
);
1807 target_description_filename
= NULL
;
1808 target_clear_description ();
1809 target_find_description ();
1812 /* Print target description in C. */
1814 class print_c_tdesc
: public tdesc_element_visitor
1817 print_c_tdesc (std::string
&filename_after_features
)
1818 : m_filename_after_features (filename_after_features
)
1822 const char *filename
= lbasename (m_filename_after_features
.c_str ());
1824 m_function
= (char *) xmalloc (strlen (filename
) + 1);
1825 for (inp
= filename
, outp
= m_function
; *inp
!= '\0'; inp
++)
1828 else if (*inp
== '-')
1834 /* Standard boilerplate. */
1835 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1836 "-*- buffer-read-only: t -*- vi"
1845 void visit_pre (const target_desc
*e
) override
1847 printf_unfiltered (" Original: %s */\n\n",
1848 lbasename (m_filename_after_features
.c_str ()));
1850 printf_unfiltered ("#include \"defs.h\"\n");
1851 printf_unfiltered ("#include \"osabi.h\"\n");
1852 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1853 printf_unfiltered ("\n");
1855 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function
);
1856 printf_unfiltered ("static void\n");
1857 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function
);
1858 printf_unfiltered ("{\n");
1860 (" struct target_desc *result = allocate_target_description ();\n");
1862 if (tdesc_architecture (e
) != NULL
)
1865 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1866 tdesc_architecture (e
)->printable_name
);
1867 printf_unfiltered ("\n");
1869 if (tdesc_osabi (e
) > GDB_OSABI_UNKNOWN
1870 && tdesc_osabi (e
) < GDB_OSABI_INVALID
)
1873 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1874 gdbarch_osabi_name (tdesc_osabi (e
)));
1875 printf_unfiltered ("\n");
1878 for (const struct bfd_arch_info
*compatible
: e
->compatible
)
1880 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1881 compatible
->printable_name
);
1883 if (!e
->compatible
.empty ())
1884 printf_unfiltered ("\n");
1886 for (const property
&prop
: e
->properties
)
1887 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1888 prop
.key
.c_str (), prop
.value
.c_str ());
1890 printf_unfiltered (" struct tdesc_feature *feature;\n");
1893 void visit_pre (const tdesc_feature
*e
) override
1895 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1899 void visit_post (const tdesc_feature
*e
) override
1902 void visit_post (const target_desc
*e
) override
1904 printf_unfiltered ("\n tdesc_%s = result;\n", m_function
);
1905 printf_unfiltered ("}\n");
1908 void visit (const tdesc_type
*type
) override
1910 struct tdesc_type_field
*f
;
1912 /* Now we do some "filtering" in order to know which variables to
1913 declare. This is needed because otherwise we would declare unused
1914 variables `field_type' and `type'. */
1915 if (!m_printed_field_type
)
1917 printf_unfiltered (" struct tdesc_type *field_type;\n");
1918 m_printed_field_type
= true;
1921 if ((type
->kind
== TDESC_TYPE_UNION
1922 || type
->kind
== TDESC_TYPE_STRUCT
1923 || type
->kind
== TDESC_TYPE_FLAGS
1924 || type
->kind
== TDESC_TYPE_ENUM
)
1925 && VEC_length (tdesc_type_field
, type
->u
.u
.fields
) > 0
1928 printf_unfiltered (" struct tdesc_type *type;\n");
1929 m_printed_type
= true;
1934 case TDESC_TYPE_VECTOR
:
1936 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1937 type
->u
.v
.type
->name
);
1939 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1940 type
->name
, type
->u
.v
.count
);
1942 case TDESC_TYPE_STRUCT
:
1943 case TDESC_TYPE_FLAGS
:
1944 if (type
->kind
== TDESC_TYPE_STRUCT
)
1947 (" type = tdesc_create_struct (feature, \"%s\");\n",
1949 if (type
->u
.u
.size
!= 0)
1951 (" tdesc_set_struct_size (type, %d);\n",
1957 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1958 type
->name
, type
->u
.u
.size
);
1961 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
1964 const char *type_name
;
1966 gdb_assert (f
->type
!= NULL
);
1967 type_name
= f
->type
->name
;
1969 /* To minimize changes to generated files, don't emit type
1970 info for fields that have defaulted types. */
1973 gdb_assert (f
->end
!= -1);
1974 if (f
->type
->kind
== TDESC_TYPE_BOOL
)
1976 gdb_assert (f
->start
== f
->end
);
1978 (" tdesc_add_flag (type, %d, \"%s\");\n",
1981 else if ((type
->u
.u
.size
== 4
1982 && f
->type
->kind
== TDESC_TYPE_UINT32
)
1983 || (type
->u
.u
.size
== 8
1984 && f
->type
->kind
== TDESC_TYPE_UINT64
))
1987 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1988 f
->name
, f
->start
, f
->end
);
1993 (" field_type = tdesc_named_type (feature,"
1997 (" tdesc_add_typed_bitfield (type, \"%s\","
1998 " %d, %d, field_type);\n",
1999 f
->name
, f
->start
, f
->end
);
2002 else /* Not a bitfield. */
2004 gdb_assert (f
->end
== -1);
2005 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
2007 (" field_type = tdesc_named_type (feature,"
2011 (" tdesc_add_field (type, \"%s\", field_type);\n",
2016 case TDESC_TYPE_UNION
:
2018 (" type = tdesc_create_union (feature, \"%s\");\n",
2021 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
2025 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2028 (" tdesc_add_field (type, \"%s\", field_type);\n",
2032 case TDESC_TYPE_ENUM
:
2034 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2035 type
->name
, type
->u
.u
.size
);
2037 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
2040 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2044 error (_("C output is not supported type \"%s\"."), type
->name
);
2046 printf_unfiltered ("\n");
2049 void visit (const tdesc_reg
*reg
) override
2051 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2052 reg
->name
.c_str (), reg
->target_regnum
,
2054 if (!reg
->group
.empty ())
2055 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
2057 printf_unfiltered ("NULL, ");
2058 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
2062 std::string m_filename_after_features
;
2066 bool m_printed_field_type
= false;
2067 bool m_printed_type
= false;
2070 /* Print target description feature in C. */
2072 class print_c_feature
: public print_c_tdesc
2075 print_c_feature (std::string
&file
)
2076 : print_c_tdesc (file
)
2079 auto const pos
= m_filename_after_features
.find_last_of ('.');
2081 m_filename_after_features
= m_filename_after_features
.substr (0, pos
);
2084 void visit_pre (const target_desc
*e
) override
2086 printf_unfiltered (" Original: %s */\n\n",
2087 lbasename (m_filename_after_features
.c_str ()));
2089 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2090 printf_unfiltered ("\n");
2093 void visit_post (const target_desc
*e
) override
2096 void visit_pre (const tdesc_feature
*e
) override
2098 std::string
name (m_filename_after_features
);
2100 auto pos
= name
.find_first_of ('.');
2102 name
= name
.substr (0, pos
);
2103 std::replace (name
.begin (), name
.end (), '/', '_');
2104 std::replace (name
.begin (), name
.end (), '-', '_');
2106 printf_unfiltered ("static int\n");
2107 printf_unfiltered ("create_feature_%s ", name
.c_str ());
2108 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2110 printf_unfiltered ("{\n");
2111 printf_unfiltered (" struct tdesc_feature *feature;\n");
2114 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2115 e
->name
.c_str (), lbasename (m_filename_after_features
.c_str ()));
2118 void visit_post (const tdesc_feature
*e
) override
2120 printf_unfiltered (" return regnum;\n");
2121 printf_unfiltered ("}\n");
2124 void visit (const tdesc_reg
*reg
) override
2126 /* Most "reg" in XML target descriptions don't have "regnum"
2127 attribute, so the register number is allocated sequentially.
2128 In case that reg has "regnum" attribute, register number
2129 should be set by that explicitly. */
2131 if (reg
->target_regnum
< m_next_regnum
)
2133 /* The integrity check, it can catch some errors on register
2134 number collision, like this,
2136 <reg name="x0" bitsize="32"/>
2137 <reg name="x1" bitsize="32"/>
2138 <reg name="x2" bitsize="32"/>
2139 <reg name="x3" bitsize="32"/>
2140 <reg name="ps" bitsize="32" regnum="3"/>
2142 but it also has false negatives. The target description
2145 <reg name="x1" bitsize="32" regnum="1"/>
2146 <reg name="x3" bitsize="32" regnum="3"/>
2147 <reg name="x2" bitsize="32" regnum="2"/>
2148 <reg name="x4" bitsize="32" regnum="4"/>
2150 but it is not a good practice, so still error on this,
2151 and also print the message so that it can be saved in the
2152 generated c file. */
2154 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2155 reg
->target_regnum
);
2156 printf_unfiltered ("is not the largest number (%d).\n",
2158 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2159 reg
->target_regnum
, m_next_regnum
);
2162 if (reg
->target_regnum
> m_next_regnum
)
2164 printf_unfiltered (" regnum = %ld;\n", reg
->target_regnum
);
2165 m_next_regnum
= reg
->target_regnum
;
2168 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2169 reg
->name
.c_str (), reg
->save_restore
);
2170 if (!reg
->group
.empty ())
2171 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
2173 printf_unfiltered ("NULL, ");
2174 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
2180 /* The register number to use for the next register we see. */
2181 int m_next_regnum
= 0;
2185 maint_print_c_tdesc_cmd (const char *args
, int from_tty
)
2187 const struct target_desc
*tdesc
;
2188 const char *filename
;
2192 /* Use the global target-supplied description, not the current
2193 architecture's. This lets a GDB for one architecture generate C
2194 for another architecture's description, even though the gdbarch
2195 initialization code will reject the new description. */
2196 tdesc
= current_target_desc
;
2197 filename
= target_description_filename
;
2201 /* Use the target description from the XML file. */
2203 tdesc
= file_read_description_xml (filename
);
2207 error (_("There is no target description to print."));
2209 if (filename
== NULL
)
2210 error (_("The current target description did not come from an XML file."));
2212 std::string
filename_after_features (filename
);
2213 auto loc
= filename_after_features
.rfind ("/features/");
2215 if (loc
!= std::string::npos
)
2216 filename_after_features
= filename_after_features
.substr (loc
+ 10);
2218 /* Print c files for target features instead of target descriptions,
2219 because c files got from target features are more flexible than the
2221 if (startswith (filename_after_features
.c_str (), "i386/32bit-")
2222 || startswith (filename_after_features
.c_str (), "i386/64bit-")
2223 || startswith (filename_after_features
.c_str (), "i386/x32-core.xml")
2224 || startswith (filename_after_features
.c_str (), "tic6x-")
2225 || startswith (filename_after_features
.c_str (), "aarch64"))
2227 print_c_feature
v (filename_after_features
);
2233 print_c_tdesc
v (filename_after_features
);
2239 namespace selftests
{
2241 static std::vector
<std::pair
<const char*, const target_desc
*>> xml_tdesc
;
2245 /* See target-descritpions.h. */
2248 record_xml_tdesc (const char *xml_file
, const struct target_desc
*tdesc
)
2250 xml_tdesc
.emplace_back (xml_file
, tdesc
);
2256 /* Check that the target descriptions created dynamically by
2257 architecture-specific code equal the descriptions created from XML files
2258 found in the specified directory DIR. */
2261 maintenance_check_xml_descriptions (const char *dir
, int from_tty
)
2264 error (_("Missing dir name"));
2266 gdb::unique_xmalloc_ptr
<char> dir1 (tilde_expand (dir
));
2267 std::string
feature_dir (dir1
.get ());
2268 unsigned int failed
= 0;
2270 for (auto const &e
: selftests::xml_tdesc
)
2272 std::string tdesc_xml
= (feature_dir
+ SLASH_STRING
+ e
.first
);
2273 const target_desc
*tdesc
2274 = file_read_description_xml (tdesc_xml
.data ());
2276 if (tdesc
== NULL
|| *tdesc
!= *e
.second
)
2279 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2280 (long) selftests::xml_tdesc
.size (), failed
);
2284 _initialize_target_descriptions (void)
2286 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
2288 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
2289 Set target description specific variables."),
2290 &tdesc_set_cmdlist
, "set tdesc ",
2291 0 /* allow-unknown */, &setlist
);
2292 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
2293 Show target description specific variables."),
2294 &tdesc_show_cmdlist
, "show tdesc ",
2295 0 /* allow-unknown */, &showlist
);
2296 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
2297 Unset target description specific variables."),
2298 &tdesc_unset_cmdlist
, "unset tdesc ",
2299 0 /* allow-unknown */, &unsetlist
);
2301 add_setshow_filename_cmd ("filename", class_obscure
,
2302 &tdesc_filename_cmd_string
,
2304 Set the file to read for an XML target description"), _("\
2305 Show the file to read for an XML target description"), _("\
2306 When set, GDB will read the target description from a local\n\
2307 file instead of querying the remote target."),
2308 set_tdesc_filename_cmd
,
2309 show_tdesc_filename_cmd
,
2310 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
2312 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
2313 Unset the file to read for an XML target description. When unset,\n\
2314 GDB will read the description from the target."),
2315 &tdesc_unset_cmdlist
);
2317 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
2318 Print the current target description as a C source file."),
2319 &maintenanceprintlist
);
2321 cmd_list_element
*cmd
;
2323 cmd
= add_cmd ("xml-descriptions", class_maintenance
,
2324 maintenance_check_xml_descriptions
, _("\
2325 Check the target descriptions created in GDB equal the descriptions\n\
2326 created from XML files in the directory.\n\
2327 The parameter is the directory name."),
2328 &maintenancechecklist
);
2329 set_cmd_completer (cmd
, filename_completer
);