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 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
);
274 typedef std::unique_ptr
<tdesc_type
> tdesc_type_up
;
276 /* A feature from a target description. Each feature is a collection
277 of other elements, e.g. registers and types. */
279 struct tdesc_feature
: tdesc_element
281 tdesc_feature (const std::string
&name_
)
285 virtual ~tdesc_feature () = default;
287 DISABLE_COPY_AND_ASSIGN (tdesc_feature
);
289 /* The name of this feature. It may be recognized by the architecture
293 /* The registers associated with this feature. */
294 std::vector
<std::unique_ptr
<tdesc_reg
>> registers
;
296 /* The types associated with this feature. */
297 std::vector
<tdesc_type_up
> types
;
299 void accept (tdesc_element_visitor
&v
) const override
303 for (const tdesc_type_up
&type
: types
)
306 for (const tdesc_reg_up
®
: registers
)
312 bool operator== (const tdesc_feature
&other
) const
314 if (name
!= other
.name
)
317 if (registers
.size () != other
.registers
.size ())
320 for (int ix
= 0; ix
< registers
.size (); ix
++)
322 const tdesc_reg_up
®1
= registers
[ix
];
323 const tdesc_reg_up
®2
= other
.registers
[ix
];
325 if (reg1
!= reg2
&& *reg1
!= *reg2
)
329 if (types
.size () != other
.types
.size ())
332 for (int ix
= 0; ix
< types
.size (); ix
++)
334 const tdesc_type_up
&type1
= types
[ix
];
335 const tdesc_type_up
&type2
= other
.types
[ix
];
337 if (type1
!= type2
&& *type1
!= *type2
)
344 bool operator!= (const tdesc_feature
&other
) const
346 return !(*this == other
);
350 typedef std::unique_ptr
<tdesc_feature
> tdesc_feature_up
;
352 /* A target description. */
354 struct target_desc
: tdesc_element
359 virtual ~target_desc () = default;
361 target_desc (const target_desc
&) = delete;
362 void operator= (const target_desc
&) = delete;
364 /* The architecture reported by the target, if any. */
365 const struct bfd_arch_info
*arch
= NULL
;
367 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
369 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
371 /* The list of compatible architectures reported by the target. */
372 std::vector
<const bfd_arch_info
*> compatible
;
374 /* Any architecture-specific properties specified by the target. */
375 std::vector
<property
> properties
;
377 /* The features associated with this target. */
378 std::vector
<std::unique_ptr
<tdesc_feature
>> features
;
380 void accept (tdesc_element_visitor
&v
) const override
384 for (const tdesc_feature_up
&feature
: features
)
390 bool operator== (const target_desc
&other
) const
392 if (arch
!= other
.arch
)
395 if (osabi
!= other
.osabi
)
398 if (features
.size () != other
.features
.size ())
401 for (int ix
= 0; ix
< features
.size (); ix
++)
403 const tdesc_feature_up
&feature1
= features
[ix
];
404 const tdesc_feature_up
&feature2
= other
.features
[ix
];
406 if (feature1
!= feature2
&& *feature1
!= *feature2
)
413 bool operator!= (const target_desc
&other
) const
415 return !(*this == other
);
419 /* Per-architecture data associated with a target description. The
420 target description may be shared by multiple architectures, but
421 this data is private to one gdbarch. */
423 typedef struct tdesc_arch_reg
425 struct tdesc_reg
*reg
;
428 DEF_VEC_O(tdesc_arch_reg
);
430 struct tdesc_arch_data
432 /* A list of register/type pairs, indexed by GDB's internal register number.
433 During initialization of the gdbarch this list is used to store
434 registers which the architecture assigns a fixed register number.
435 Registers which are NULL in this array, or off the end, are
436 treated as zero-sized and nameless (i.e. placeholders in the
438 VEC(tdesc_arch_reg
) *arch_regs
;
440 /* Functions which report the register name, type, and reggroups for
442 gdbarch_register_name_ftype
*pseudo_register_name
;
443 gdbarch_register_type_ftype
*pseudo_register_type
;
444 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
;
447 /* Info about an inferior's target description. There's one of these
448 for each inferior. */
450 struct target_desc_info
452 /* A flag indicating that a description has already been fetched
453 from the target, so it should not be queried again. */
457 /* The description fetched from the target, or NULL if the target
458 did not supply any description. Only valid when
459 target_desc_fetched is set. Only the description initialization
460 code should access this; normally, the description should be
461 accessed through the gdbarch object. */
463 const struct target_desc
*tdesc
;
465 /* The filename to read a target description from, as set by "set
466 tdesc filename ..." */
471 /* Get the inferior INF's target description info, allocating one on
472 the stop if necessary. */
474 static struct target_desc_info
*
475 get_tdesc_info (struct inferior
*inf
)
477 if (inf
->tdesc_info
== NULL
)
478 inf
->tdesc_info
= XCNEW (struct target_desc_info
);
479 return inf
->tdesc_info
;
482 /* A handle for architecture-specific data associated with the
483 target description (see struct tdesc_arch_data). */
485 static struct gdbarch_data
*tdesc_data
;
487 /* See target-descriptions.h. */
490 target_desc_info_from_user_p (struct target_desc_info
*info
)
492 return info
!= NULL
&& info
->filename
!= NULL
;
495 /* See target-descriptions.h. */
498 copy_inferior_target_desc_info (struct inferior
*destinf
, struct inferior
*srcinf
)
500 struct target_desc_info
*src
= get_tdesc_info (srcinf
);
501 struct target_desc_info
*dest
= get_tdesc_info (destinf
);
503 dest
->fetched
= src
->fetched
;
504 dest
->tdesc
= src
->tdesc
;
505 dest
->filename
= src
->filename
!= NULL
? xstrdup (src
->filename
) : NULL
;
508 /* See target-descriptions.h. */
511 target_desc_info_free (struct target_desc_info
*tdesc_info
)
513 if (tdesc_info
!= NULL
)
515 xfree (tdesc_info
->filename
);
520 /* Convenience helper macros. */
522 #define target_desc_fetched \
523 get_tdesc_info (current_inferior ())->fetched
524 #define current_target_desc \
525 get_tdesc_info (current_inferior ())->tdesc
526 #define target_description_filename \
527 get_tdesc_info (current_inferior ())->filename
529 /* The string manipulated by the "set tdesc filename ..." command. */
531 static char *tdesc_filename_cmd_string
;
533 /* Fetch the current target's description, and switch the current
534 architecture to one which incorporates that description. */
537 target_find_description (void)
539 /* If we've already fetched a description from the target, don't do
540 it again. This allows a target to fetch the description early,
541 during its to_open or to_create_inferior, if it needs extra
542 information about the target to initialize. */
543 if (target_desc_fetched
)
546 /* The current architecture should not have any target description
547 specified. It should have been cleared, e.g. when we
548 disconnected from the previous target. */
549 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL
);
551 /* First try to fetch an XML description from the user-specified
553 current_target_desc
= NULL
;
554 if (target_description_filename
!= NULL
555 && *target_description_filename
!= '\0')
557 = file_read_description_xml (target_description_filename
);
559 /* Next try to read the description from the current target using
561 if (current_target_desc
== NULL
)
562 current_target_desc
= target_read_description_xml (¤t_target
);
564 /* If that failed try a target-specific hook. */
565 if (current_target_desc
== NULL
)
566 current_target_desc
= target_read_description (¤t_target
);
568 /* If a non-NULL description was returned, then update the current
570 if (current_target_desc
)
572 struct gdbarch_info info
;
574 gdbarch_info_init (&info
);
575 info
.target_desc
= current_target_desc
;
576 if (!gdbarch_update_p (info
))
577 warning (_("Architecture rejected target-supplied description"));
580 struct tdesc_arch_data
*data
;
582 data
= ((struct tdesc_arch_data
*)
583 gdbarch_data (target_gdbarch (), tdesc_data
));
584 if (tdesc_has_registers (current_target_desc
)
585 && data
->arch_regs
== NULL
)
586 warning (_("Target-supplied registers are not supported "
587 "by the current architecture"));
591 /* Now that we know this description is usable, record that we
593 target_desc_fetched
= 1;
596 /* Discard any description fetched from the current target, and switch
597 the current architecture to one with no target description. */
600 target_clear_description (void)
602 struct gdbarch_info info
;
604 if (!target_desc_fetched
)
607 target_desc_fetched
= 0;
608 current_target_desc
= NULL
;
610 gdbarch_info_init (&info
);
611 if (!gdbarch_update_p (info
))
612 internal_error (__FILE__
, __LINE__
,
613 _("Could not remove target-supplied description"));
616 /* Return the global current target description. This should only be
617 used by gdbarch initialization code; most access should be through
618 an existing gdbarch. */
620 const struct target_desc
*
621 target_current_description (void)
623 if (target_desc_fetched
)
624 return current_target_desc
;
629 /* Return non-zero if this target description is compatible
630 with the given BFD architecture. */
633 tdesc_compatible_p (const struct target_desc
*target_desc
,
634 const struct bfd_arch_info
*arch
)
636 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
639 || arch
->compatible (arch
, compat
)
640 || compat
->compatible (compat
, arch
))
648 /* Direct accessors for target descriptions. */
650 /* Return the string value of a property named KEY, or NULL if the
651 property was not specified. */
654 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
656 for (const property
&prop
: target_desc
->properties
)
658 return prop
.value
.c_str ();
663 /* Return the BFD architecture associated with this target
664 description, or NULL if no architecture was specified. */
666 const struct bfd_arch_info
*
667 tdesc_architecture (const struct target_desc
*target_desc
)
669 return target_desc
->arch
;
672 /* Return the OSABI associated with this target description, or
673 GDB_OSABI_UNKNOWN if no osabi was specified. */
676 tdesc_osabi (const struct target_desc
*target_desc
)
678 return target_desc
->osabi
;
683 /* Return 1 if this target description includes any registers. */
686 tdesc_has_registers (const struct target_desc
*target_desc
)
688 if (target_desc
== NULL
)
691 for (const tdesc_feature_up
&feature
: target_desc
->features
)
692 if (!feature
->registers
.empty ())
698 /* Return the feature with the given name, if present, or NULL if
699 the named feature is not found. */
701 const struct tdesc_feature
*
702 tdesc_find_feature (const struct target_desc
*target_desc
,
705 for (const tdesc_feature_up
&feature
: target_desc
->features
)
706 if (feature
->name
== name
)
707 return feature
.get ();
712 /* Return the name of FEATURE. */
715 tdesc_feature_name (const struct tdesc_feature
*feature
)
717 return feature
->name
.c_str ();
720 /* Predefined types. */
721 static struct tdesc_type tdesc_predefined_types
[] =
723 { "bool", TDESC_TYPE_BOOL
},
724 { "int8", TDESC_TYPE_INT8
},
725 { "int16", TDESC_TYPE_INT16
},
726 { "int32", TDESC_TYPE_INT32
},
727 { "int64", TDESC_TYPE_INT64
},
728 { "int128", TDESC_TYPE_INT128
},
729 { "uint8", TDESC_TYPE_UINT8
},
730 { "uint16", TDESC_TYPE_UINT16
},
731 { "uint32", TDESC_TYPE_UINT32
},
732 { "uint64", TDESC_TYPE_UINT64
},
733 { "uint128", TDESC_TYPE_UINT128
},
734 { "code_ptr", TDESC_TYPE_CODE_PTR
},
735 { "data_ptr", TDESC_TYPE_DATA_PTR
},
736 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
737 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
738 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
},
739 { "i387_ext", TDESC_TYPE_I387_EXT
}
742 /* Lookup a predefined type. */
744 static struct tdesc_type
*
745 tdesc_predefined_type (enum tdesc_type_kind kind
)
747 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
748 if (tdesc_predefined_types
[ix
].kind
== kind
)
749 return &tdesc_predefined_types
[ix
];
751 gdb_assert_not_reached ("bad predefined tdesc type");
754 /* See arch/tdesc.h. */
757 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
759 /* First try target-defined types. */
760 for (const tdesc_type_up
&type
: feature
->types
)
761 if (strcmp (type
->name
, id
) == 0)
764 /* Next try the predefined types. */
765 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
766 if (strcmp (tdesc_predefined_types
[ix
].name
, id
) == 0)
767 return &tdesc_predefined_types
[ix
];
772 /* Lookup type associated with ID. */
775 tdesc_find_type (struct gdbarch
*gdbarch
, const char *id
)
777 struct tdesc_arch_reg
*reg
;
778 struct tdesc_arch_data
*data
;
781 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
782 num_regs
= VEC_length (tdesc_arch_reg
, data
->arch_regs
);
783 for (i
= 0; i
< num_regs
; i
++)
785 reg
= VEC_index (tdesc_arch_reg
, data
->arch_regs
, i
);
787 && reg
->reg
->tdesc_type
789 && strcmp (id
, reg
->reg
->tdesc_type
->name
) == 0)
796 /* Construct, if necessary, and return the GDB type implementing target
797 type TDESC_TYPE for architecture GDBARCH. */
800 tdesc_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*tdesc_type
)
804 switch (tdesc_type
->kind
)
806 /* Predefined types. */
807 case TDESC_TYPE_BOOL
:
808 return builtin_type (gdbarch
)->builtin_bool
;
810 case TDESC_TYPE_INT8
:
811 return builtin_type (gdbarch
)->builtin_int8
;
813 case TDESC_TYPE_INT16
:
814 return builtin_type (gdbarch
)->builtin_int16
;
816 case TDESC_TYPE_INT32
:
817 return builtin_type (gdbarch
)->builtin_int32
;
819 case TDESC_TYPE_INT64
:
820 return builtin_type (gdbarch
)->builtin_int64
;
822 case TDESC_TYPE_INT128
:
823 return builtin_type (gdbarch
)->builtin_int128
;
825 case TDESC_TYPE_UINT8
:
826 return builtin_type (gdbarch
)->builtin_uint8
;
828 case TDESC_TYPE_UINT16
:
829 return builtin_type (gdbarch
)->builtin_uint16
;
831 case TDESC_TYPE_UINT32
:
832 return builtin_type (gdbarch
)->builtin_uint32
;
834 case TDESC_TYPE_UINT64
:
835 return builtin_type (gdbarch
)->builtin_uint64
;
837 case TDESC_TYPE_UINT128
:
838 return builtin_type (gdbarch
)->builtin_uint128
;
840 case TDESC_TYPE_CODE_PTR
:
841 return builtin_type (gdbarch
)->builtin_func_ptr
;
843 case TDESC_TYPE_DATA_PTR
:
844 return builtin_type (gdbarch
)->builtin_data_ptr
;
850 type
= tdesc_find_type (gdbarch
, tdesc_type
->name
);
854 switch (tdesc_type
->kind
)
856 case TDESC_TYPE_IEEE_SINGLE
:
857 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_single",
858 floatformats_ieee_single
);
860 case TDESC_TYPE_IEEE_DOUBLE
:
861 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_double",
862 floatformats_ieee_double
);
864 case TDESC_TYPE_ARM_FPA_EXT
:
865 return arch_float_type (gdbarch
, -1, "builtin_type_arm_ext",
866 floatformats_arm_ext
);
868 case TDESC_TYPE_I387_EXT
:
869 return arch_float_type (gdbarch
, -1, "builtin_type_i387_ext",
870 floatformats_i387_ext
);
872 /* Types defined by a target feature. */
873 case TDESC_TYPE_VECTOR
:
875 struct type
*type
, *field_type
;
877 field_type
= tdesc_gdb_type (gdbarch
, tdesc_type
->u
.v
.type
);
878 type
= init_vector_type (field_type
, tdesc_type
->u
.v
.count
);
879 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
884 case TDESC_TYPE_STRUCT
:
886 struct type
*type
, *field_type
;
887 struct tdesc_type_field
*f
;
890 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
891 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
892 TYPE_TAG_NAME (type
) = TYPE_NAME (type
);
895 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
898 if (f
->start
!= -1 && f
->end
!= -1)
902 struct type
*field_type
;
903 int bitsize
, total_size
;
905 /* This invariant should be preserved while creating types. */
906 gdb_assert (tdesc_type
->u
.u
.size
!= 0);
908 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
909 else if (tdesc_type
->u
.u
.size
> 4)
910 field_type
= builtin_type (gdbarch
)->builtin_uint64
;
912 field_type
= builtin_type (gdbarch
)->builtin_uint32
;
914 fld
= append_composite_type_field_raw (type
, xstrdup (f
->name
),
917 /* For little-endian, BITPOS counts from the LSB of
918 the structure and marks the LSB of the field. For
919 big-endian, BITPOS counts from the MSB of the
920 structure and marks the MSB of the field. Either
921 way, it is the number of bits to the "left" of the
922 field. To calculate this in big-endian, we need
923 the total size of the structure. */
924 bitsize
= f
->end
- f
->start
+ 1;
925 total_size
= tdesc_type
->u
.u
.size
* TARGET_CHAR_BIT
;
926 if (gdbarch_bits_big_endian (gdbarch
))
927 SET_FIELD_BITPOS (fld
[0], total_size
- f
->start
- bitsize
);
929 SET_FIELD_BITPOS (fld
[0], f
->start
);
930 FIELD_BITSIZE (fld
[0]) = bitsize
;
934 gdb_assert (f
->start
== -1 && f
->end
== -1);
935 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
936 append_composite_type_field (type
, xstrdup (f
->name
),
941 if (tdesc_type
->u
.u
.size
!= 0)
942 TYPE_LENGTH (type
) = tdesc_type
->u
.u
.size
;
946 case TDESC_TYPE_UNION
:
948 struct type
*type
, *field_type
;
949 struct tdesc_type_field
*f
;
952 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
953 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
956 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
959 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
960 append_composite_type_field (type
, xstrdup (f
->name
), field_type
);
962 /* If any of the children of a union are vectors, flag the
963 union as a vector also. This allows e.g. a union of two
964 vector types to show up automatically in "info vector". */
965 if (TYPE_VECTOR (field_type
))
966 TYPE_VECTOR (type
) = 1;
971 case TDESC_TYPE_FLAGS
:
973 struct tdesc_type_field
*f
;
976 type
= arch_flags_type (gdbarch
, tdesc_type
->name
,
977 tdesc_type
->u
.u
.size
* TARGET_CHAR_BIT
);
979 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
982 struct type
*field_type
;
983 int bitsize
= f
->end
- f
->start
+ 1;
985 gdb_assert (f
->type
!= NULL
);
986 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
987 append_flags_type_field (type
, f
->start
, bitsize
,
988 field_type
, f
->name
);
994 case TDESC_TYPE_ENUM
:
996 struct tdesc_type_field
*f
;
999 type
= arch_type (gdbarch
, TYPE_CODE_ENUM
,
1000 tdesc_type
->u
.u
.size
* TARGET_CHAR_BIT
,
1002 TYPE_UNSIGNED (type
) = 1;
1004 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
1008 = append_composite_type_field_raw (type
, xstrdup (f
->name
),
1011 SET_FIELD_BITPOS (fld
[0], f
->start
);
1018 internal_error (__FILE__
, __LINE__
,
1019 "Type \"%s\" has an unknown kind %d",
1020 tdesc_type
->name
, tdesc_type
->kind
);
1024 /* Support for registers from target descriptions. */
1026 /* Construct the per-gdbarch data. */
1029 tdesc_data_init (struct obstack
*obstack
)
1031 struct tdesc_arch_data
*data
;
1033 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
1037 /* Similar, but for the temporary copy used during architecture
1040 struct tdesc_arch_data
*
1041 tdesc_data_alloc (void)
1043 return XCNEW (struct tdesc_arch_data
);
1046 /* Free something allocated by tdesc_data_alloc, if it is not going
1047 to be used (for instance if it was unsuitable for the
1051 tdesc_data_cleanup (void *data_untyped
)
1053 struct tdesc_arch_data
*data
= (struct tdesc_arch_data
*) data_untyped
;
1055 VEC_free (tdesc_arch_reg
, data
->arch_regs
);
1059 /* Search FEATURE for a register named NAME. */
1061 static struct tdesc_reg
*
1062 tdesc_find_register_early (const struct tdesc_feature
*feature
,
1065 for (const tdesc_reg_up
®
: feature
->registers
)
1066 if (strcasecmp (reg
->name
.c_str (), name
) == 0)
1072 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1075 tdesc_numbered_register (const struct tdesc_feature
*feature
,
1076 struct tdesc_arch_data
*data
,
1077 int regno
, const char *name
)
1079 struct tdesc_arch_reg arch_reg
= { 0 };
1080 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1085 /* Make sure the vector includes a REGNO'th element. */
1086 while (regno
>= VEC_length (tdesc_arch_reg
, data
->arch_regs
))
1087 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &arch_reg
);
1090 VEC_replace (tdesc_arch_reg
, data
->arch_regs
, regno
, &arch_reg
);
1094 /* Search FEATURE for a register named NAME, but do not assign a fixed
1095 register number to it. */
1098 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
1101 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1109 /* Search FEATURE for a register whose name is in NAMES and assign
1113 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
1114 struct tdesc_arch_data
*data
,
1115 int regno
, const char *const names
[])
1119 for (i
= 0; names
[i
] != NULL
; i
++)
1120 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
1126 /* Search FEATURE for a register named NAME, and return its size in
1127 bits. The register must exist. */
1130 tdesc_register_size (const struct tdesc_feature
*feature
,
1133 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1135 gdb_assert (reg
!= NULL
);
1136 return reg
->bitsize
;
1139 /* Look up a register by its GDB internal register number. */
1141 static struct tdesc_arch_reg
*
1142 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
1144 struct tdesc_arch_data
*data
;
1146 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1147 if (regno
< VEC_length (tdesc_arch_reg
, data
->arch_regs
))
1148 return VEC_index (tdesc_arch_reg
, data
->arch_regs
, regno
);
1153 static struct tdesc_reg
*
1154 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
1156 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
1158 return reg
? reg
->reg
: NULL
;
1161 /* Return the name of register REGNO, from the target description or
1162 from an architecture-provided pseudo_register_name method. */
1165 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
1167 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1168 int num_regs
= gdbarch_num_regs (gdbarch
);
1169 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1172 return reg
->name
.c_str ();
1174 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1176 struct tdesc_arch_data
*data
1177 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1179 gdb_assert (data
->pseudo_register_name
!= NULL
);
1180 return data
->pseudo_register_name (gdbarch
, regno
);
1187 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
1189 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
1190 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
1191 int num_regs
= gdbarch_num_regs (gdbarch
);
1192 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1194 if (reg
== NULL
&& 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_type
!= NULL
);
1200 return data
->pseudo_register_type (gdbarch
, regno
);
1204 /* Return "int0_t", since "void" has a misleading size of one. */
1205 return builtin_type (gdbarch
)->builtin_int0
;
1207 if (arch_reg
->type
== NULL
)
1209 /* First check for a predefined or target defined type. */
1210 if (reg
->tdesc_type
)
1211 arch_reg
->type
= tdesc_gdb_type (gdbarch
, reg
->tdesc_type
);
1213 /* Next try size-sensitive type shortcuts. */
1214 else if (reg
->type
== "float")
1216 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
1217 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
1218 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
1219 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1220 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
1221 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
1224 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1225 reg
->name
.c_str (), reg
->bitsize
);
1226 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1229 else if (reg
->type
== "int")
1231 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
1232 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1233 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
1234 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
1235 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
1236 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
1237 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
1238 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
1239 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
1240 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
1241 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
1242 /* A bit desperate by this point... */
1243 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
1246 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1247 reg
->name
.c_str (), reg
->bitsize
);
1248 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1252 if (arch_reg
->type
== NULL
)
1253 internal_error (__FILE__
, __LINE__
,
1254 "Register \"%s\" has an unknown type \"%s\"",
1255 reg
->name
.c_str (), reg
->type
.c_str ());
1258 return arch_reg
->type
;
1262 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
1264 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1267 return reg
->target_regnum
;
1272 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1273 target description may be classified as general, float, or vector.
1274 Unlike a gdbarch register_reggroup_p method, this function will
1275 return -1 if it does not know; the caller should handle registers
1276 with no specified group.
1278 Arbitrary strings (other than "general", "float", and "vector")
1279 from the description are not used; they cause the register to be
1280 displayed in "info all-registers" but excluded from "info
1281 registers" et al. The names of containing features are also not
1282 used. This might be extended to display registers in some more
1285 The save-restore flag is also implemented here. */
1288 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1289 struct reggroup
*reggroup
)
1291 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1293 if (reg
!= NULL
&& !reg
->group
.empty ())
1295 int general_p
= 0, float_p
= 0, vector_p
= 0;
1297 if (reg
->group
== "general")
1299 else if (reg
->group
== "float")
1301 else if (reg
->group
== "vector")
1304 if (reggroup
== float_reggroup
)
1307 if (reggroup
== vector_reggroup
)
1310 if (reggroup
== general_reggroup
)
1315 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
1316 return reg
->save_restore
;
1321 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1322 group specified go to the default reggroup function and are handled
1326 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1327 struct reggroup
*reggroup
)
1329 int num_regs
= gdbarch_num_regs (gdbarch
);
1330 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1333 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1335 struct tdesc_arch_data
*data
1336 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1338 if (data
->pseudo_register_reggroup_p
!= NULL
)
1339 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
1340 /* Otherwise fall through to the default reggroup_p. */
1343 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
1347 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
1350 /* Record architecture-specific functions to call for pseudo-register
1354 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
1355 gdbarch_register_name_ftype
*pseudo_name
)
1357 struct tdesc_arch_data
*data
1358 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1360 data
->pseudo_register_name
= pseudo_name
;
1364 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
1365 gdbarch_register_type_ftype
*pseudo_type
)
1367 struct tdesc_arch_data
*data
1368 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1370 data
->pseudo_register_type
= pseudo_type
;
1374 set_tdesc_pseudo_register_reggroup_p
1375 (struct gdbarch
*gdbarch
,
1376 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
1378 struct tdesc_arch_data
*data
1379 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1381 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
1384 /* Update GDBARCH to use the target description for registers. */
1387 tdesc_use_registers (struct gdbarch
*gdbarch
,
1388 const struct target_desc
*target_desc
,
1389 struct tdesc_arch_data
*early_data
)
1391 int num_regs
= gdbarch_num_regs (gdbarch
);
1392 struct tdesc_arch_data
*data
;
1393 struct tdesc_arch_reg
*arch_reg
, new_arch_reg
= { 0 };
1396 /* We can't use the description for registers if it doesn't describe
1397 any. This function should only be called after validating
1398 registers, so the caller should know that registers are
1400 gdb_assert (tdesc_has_registers (target_desc
));
1402 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1403 data
->arch_regs
= early_data
->arch_regs
;
1406 /* Build up a set of all registers, so that we can assign register
1407 numbers where needed. The hash table expands as necessary, so
1408 the initial size is arbitrary. */
1409 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1410 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1411 for (const tdesc_reg_up
®
: feature
->registers
)
1413 void **slot
= htab_find_slot (reg_hash
, reg
.get (), INSERT
);
1418 /* Remove any registers which were assigned numbers by the
1421 VEC_iterate (tdesc_arch_reg
, data
->arch_regs
, ixr
, arch_reg
);
1424 htab_remove_elt (reg_hash
, arch_reg
->reg
);
1426 /* Assign numbers to the remaining registers and add them to the
1427 list of registers. The new numbers are always above gdbarch_num_regs.
1428 Iterate over the features, not the hash table, so that the order
1429 matches that in the target description. */
1431 gdb_assert (VEC_length (tdesc_arch_reg
, data
->arch_regs
) <= num_regs
);
1432 while (VEC_length (tdesc_arch_reg
, data
->arch_regs
) < num_regs
)
1433 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1434 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1435 for (const tdesc_reg_up
®
: feature
->registers
)
1436 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1438 new_arch_reg
.reg
= reg
.get ();
1439 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1443 htab_delete (reg_hash
);
1445 /* Update the architecture. */
1446 set_gdbarch_num_regs (gdbarch
, num_regs
);
1447 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1448 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1449 set_gdbarch_remote_register_number (gdbarch
,
1450 tdesc_remote_register_number
);
1451 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1455 /* See arch/tdesc.h. */
1458 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
1459 int regnum
, int save_restore
, const char *group
,
1460 int bitsize
, const char *type
)
1462 tdesc_reg
*reg
= new tdesc_reg (feature
, name
, regnum
, save_restore
,
1463 group
, bitsize
, type
);
1465 feature
->registers
.emplace_back (reg
);
1468 /* See arch/tdesc.h. */
1471 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1472 struct tdesc_type
*field_type
, int count
)
1474 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_VECTOR
);
1476 type
->u
.v
.type
= field_type
;
1477 type
->u
.v
.count
= count
;
1479 feature
->types
.emplace_back (type
);
1483 /* See arch/tdesc.h. */
1486 tdesc_create_struct (struct tdesc_feature
*feature
, const char *name
)
1488 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_STRUCT
);
1490 feature
->types
.emplace_back (type
);
1494 /* See arch/tdesc.h. */
1497 tdesc_set_struct_size (struct tdesc_type
*type
, int size
)
1499 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1500 gdb_assert (size
> 0);
1501 type
->u
.u
.size
= size
;
1504 /* See arch/tdesc.h. */
1507 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1509 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_UNION
);
1511 feature
->types
.emplace_back (type
);
1515 /* See arch/tdesc.h. */
1518 tdesc_create_flags (struct tdesc_feature
*feature
, const char *name
,
1521 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_FLAGS
);
1523 gdb_assert (size
> 0);
1525 type
->u
.u
.size
= size
;
1527 feature
->types
.emplace_back (type
);
1532 tdesc_create_enum (struct tdesc_feature
*feature
, const char *name
,
1535 struct tdesc_type
*type
= new tdesc_type (name
, TDESC_TYPE_ENUM
);
1537 gdb_assert (size
> 0);
1539 type
->u
.u
.size
= size
;
1541 feature
->types
.emplace_back (type
);
1545 /* See arch/tdesc.h. */
1548 tdesc_add_field (struct tdesc_type
*type
, const char *field_name
,
1549 struct tdesc_type
*field_type
)
1551 struct tdesc_type_field f
= { 0 };
1553 gdb_assert (type
->kind
== TDESC_TYPE_UNION
1554 || type
->kind
== TDESC_TYPE_STRUCT
);
1556 f
.name
= xstrdup (field_name
);
1557 f
.type
= field_type
;
1558 /* Initialize these values so we know this is not a bit-field
1559 when we print-c-tdesc. */
1563 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1567 tdesc_add_typed_bitfield (struct tdesc_type
*type
, const char *field_name
,
1568 int start
, int end
, struct tdesc_type
*field_type
)
1570 struct tdesc_type_field f
= { 0 };
1572 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
1573 || type
->kind
== TDESC_TYPE_FLAGS
);
1574 gdb_assert (start
>= 0 && end
>= start
);
1576 f
.name
= xstrdup (field_name
);
1579 f
.type
= field_type
;
1581 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1584 /* See arch/tdesc.h. */
1587 tdesc_add_bitfield (struct tdesc_type
*type
, const char *field_name
,
1590 struct tdesc_type
*field_type
;
1592 gdb_assert (start
>= 0 && end
>= start
);
1594 if (type
->u
.u
.size
> 4)
1595 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT64
);
1597 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT32
);
1599 tdesc_add_typed_bitfield (type
, field_name
, start
, end
, field_type
);
1602 /* See arch/tdesc.h. */
1605 tdesc_add_flag (struct tdesc_type
*type
, int start
,
1606 const char *flag_name
)
1608 struct tdesc_type_field f
= { 0 };
1610 gdb_assert (type
->kind
== TDESC_TYPE_FLAGS
1611 || type
->kind
== TDESC_TYPE_STRUCT
);
1613 f
.name
= xstrdup (flag_name
);
1616 f
.type
= tdesc_predefined_type (TDESC_TYPE_BOOL
);
1618 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1622 tdesc_add_enum_value (struct tdesc_type
*type
, int value
,
1625 struct tdesc_type_field f
= { 0 };
1627 gdb_assert (type
->kind
== TDESC_TYPE_ENUM
);
1629 f
.name
= xstrdup (name
);
1632 f
.type
= tdesc_predefined_type (TDESC_TYPE_INT32
);
1634 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1637 /* See arch/tdesc.h. */
1639 struct tdesc_feature
*
1640 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
,
1643 struct tdesc_feature
*new_feature
= new tdesc_feature (name
);
1645 tdesc
->features
.emplace_back (new_feature
);
1650 struct target_desc
*
1651 allocate_target_description (void)
1653 return new target_desc ();
1657 free_target_description (void *arg
)
1659 struct target_desc
*target_desc
= (struct target_desc
*) arg
;
1665 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1667 return make_cleanup (free_target_description
, target_desc
);
1671 tdesc_add_compatible (struct target_desc
*target_desc
,
1672 const struct bfd_arch_info
*compatible
)
1674 /* If this instance of GDB is compiled without BFD support for the
1675 compatible architecture, simply ignore it -- we would not be able
1676 to handle it anyway. */
1677 if (compatible
== NULL
)
1680 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
1681 if (compat
== compatible
)
1682 internal_error (__FILE__
, __LINE__
,
1683 _("Attempted to add duplicate "
1684 "compatible architecture \"%s\""),
1685 compatible
->printable_name
);
1687 target_desc
->compatible
.push_back (compatible
);
1691 set_tdesc_property (struct target_desc
*target_desc
,
1692 const char *key
, const char *value
)
1694 gdb_assert (key
!= NULL
&& value
!= NULL
);
1696 if (tdesc_property (target_desc
, key
) != NULL
)
1697 internal_error (__FILE__
, __LINE__
,
1698 _("Attempted to add duplicate property \"%s\""), key
);
1700 target_desc
->properties
.emplace_back (key
, value
);
1703 /* See arch/tdesc.h. */
1706 set_tdesc_architecture (struct target_desc
*target_desc
,
1709 set_tdesc_architecture (target_desc
, bfd_scan_arch (name
));
1713 set_tdesc_architecture (struct target_desc
*target_desc
,
1714 const struct bfd_arch_info
*arch
)
1716 target_desc
->arch
= arch
;
1719 /* See arch/tdesc.h. */
1722 set_tdesc_osabi (struct target_desc
*target_desc
, const char *name
)
1724 set_tdesc_osabi (target_desc
, osabi_from_tdesc_string (name
));
1728 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1730 target_desc
->osabi
= osabi
;
1734 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1735 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1737 /* Helper functions for the CLI commands. */
1740 set_tdesc_cmd (const char *args
, int from_tty
)
1742 help_list (tdesc_set_cmdlist
, "set tdesc ", all_commands
, gdb_stdout
);
1746 show_tdesc_cmd (const char *args
, int from_tty
)
1748 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1752 unset_tdesc_cmd (const char *args
, int from_tty
)
1754 help_list (tdesc_unset_cmdlist
, "unset tdesc ", all_commands
, gdb_stdout
);
1758 set_tdesc_filename_cmd (const char *args
, int from_tty
,
1759 struct cmd_list_element
*c
)
1761 xfree (target_description_filename
);
1762 target_description_filename
= xstrdup (tdesc_filename_cmd_string
);
1764 target_clear_description ();
1765 target_find_description ();
1769 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1770 struct cmd_list_element
*c
,
1773 value
= target_description_filename
;
1775 if (value
!= NULL
&& *value
!= '\0')
1776 printf_filtered (_("The target description will be read from \"%s\".\n"),
1779 printf_filtered (_("The target description will be "
1780 "read from the target.\n"));
1784 unset_tdesc_filename_cmd (const char *args
, int from_tty
)
1786 xfree (target_description_filename
);
1787 target_description_filename
= NULL
;
1788 target_clear_description ();
1789 target_find_description ();
1792 /* Print target description in C. */
1794 class print_c_tdesc
: public tdesc_element_visitor
1797 print_c_tdesc (std::string
&filename_after_features
)
1798 : m_filename_after_features (filename_after_features
)
1802 const char *filename
= lbasename (m_filename_after_features
.c_str ());
1804 m_function
= (char *) xmalloc (strlen (filename
) + 1);
1805 for (inp
= filename
, outp
= m_function
; *inp
!= '\0'; inp
++)
1808 else if (*inp
== '-')
1814 /* Standard boilerplate. */
1815 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1816 "-*- buffer-read-only: t -*- vi"
1825 void visit_pre (const target_desc
*e
) override
1827 printf_unfiltered (" Original: %s */\n\n",
1828 lbasename (m_filename_after_features
.c_str ()));
1830 printf_unfiltered ("#include \"defs.h\"\n");
1831 printf_unfiltered ("#include \"osabi.h\"\n");
1832 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1833 printf_unfiltered ("\n");
1835 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function
);
1836 printf_unfiltered ("static void\n");
1837 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function
);
1838 printf_unfiltered ("{\n");
1840 (" struct target_desc *result = allocate_target_description ();\n");
1842 if (tdesc_architecture (e
) != NULL
)
1845 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1846 tdesc_architecture (e
)->printable_name
);
1847 printf_unfiltered ("\n");
1849 if (tdesc_osabi (e
) > GDB_OSABI_UNKNOWN
1850 && tdesc_osabi (e
) < GDB_OSABI_INVALID
)
1853 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1854 gdbarch_osabi_name (tdesc_osabi (e
)));
1855 printf_unfiltered ("\n");
1858 for (const struct bfd_arch_info
*compatible
: e
->compatible
)
1860 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1861 compatible
->printable_name
);
1863 if (!e
->compatible
.empty ())
1864 printf_unfiltered ("\n");
1866 for (const property
&prop
: e
->properties
)
1867 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1868 prop
.key
.c_str (), prop
.value
.c_str ());
1870 printf_unfiltered (" struct tdesc_feature *feature;\n");
1873 void visit_pre (const tdesc_feature
*e
) override
1875 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1879 void visit_post (const tdesc_feature
*e
) override
1882 void visit_post (const target_desc
*e
) override
1884 printf_unfiltered ("\n tdesc_%s = result;\n", m_function
);
1885 printf_unfiltered ("}\n");
1888 void visit (const tdesc_type
*type
) override
1890 struct tdesc_type_field
*f
;
1892 /* Now we do some "filtering" in order to know which variables to
1893 declare. This is needed because otherwise we would declare unused
1894 variables `field_type' and `type'. */
1895 if (!m_printed_field_type
)
1897 printf_unfiltered (" struct tdesc_type *field_type;\n");
1898 m_printed_field_type
= true;
1901 if ((type
->kind
== TDESC_TYPE_UNION
1902 || type
->kind
== TDESC_TYPE_STRUCT
1903 || type
->kind
== TDESC_TYPE_FLAGS
1904 || type
->kind
== TDESC_TYPE_ENUM
)
1905 && VEC_length (tdesc_type_field
, type
->u
.u
.fields
) > 0
1908 printf_unfiltered (" struct tdesc_type *type;\n");
1909 m_printed_type
= true;
1914 case TDESC_TYPE_VECTOR
:
1916 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1917 type
->u
.v
.type
->name
);
1919 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1920 type
->name
, type
->u
.v
.count
);
1922 case TDESC_TYPE_STRUCT
:
1923 case TDESC_TYPE_FLAGS
:
1924 if (type
->kind
== TDESC_TYPE_STRUCT
)
1927 (" type = tdesc_create_struct (feature, \"%s\");\n",
1929 if (type
->u
.u
.size
!= 0)
1931 (" tdesc_set_struct_size (type, %d);\n",
1937 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1938 type
->name
, type
->u
.u
.size
);
1941 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
1944 const char *type_name
;
1946 gdb_assert (f
->type
!= NULL
);
1947 type_name
= f
->type
->name
;
1949 /* To minimize changes to generated files, don't emit type
1950 info for fields that have defaulted types. */
1953 gdb_assert (f
->end
!= -1);
1954 if (f
->type
->kind
== TDESC_TYPE_BOOL
)
1956 gdb_assert (f
->start
== f
->end
);
1958 (" tdesc_add_flag (type, %d, \"%s\");\n",
1961 else if ((type
->u
.u
.size
== 4
1962 && f
->type
->kind
== TDESC_TYPE_UINT32
)
1963 || (type
->u
.u
.size
== 8
1964 && f
->type
->kind
== TDESC_TYPE_UINT64
))
1967 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1968 f
->name
, f
->start
, f
->end
);
1973 (" field_type = tdesc_named_type (feature,"
1977 (" tdesc_add_typed_bitfield (type, \"%s\","
1978 " %d, %d, field_type);\n",
1979 f
->name
, f
->start
, f
->end
);
1982 else /* Not a bitfield. */
1984 gdb_assert (f
->end
== -1);
1985 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1987 (" field_type = tdesc_named_type (feature,"
1991 (" tdesc_add_field (type, \"%s\", field_type);\n",
1996 case TDESC_TYPE_UNION
:
1998 (" type = tdesc_create_union (feature, \"%s\");\n",
2001 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
2005 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2008 (" tdesc_add_field (type, \"%s\", field_type);\n",
2012 case TDESC_TYPE_ENUM
:
2014 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2015 type
->name
, type
->u
.u
.size
);
2017 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
2020 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2024 error (_("C output is not supported type \"%s\"."), type
->name
);
2026 printf_unfiltered ("\n");
2029 void visit (const tdesc_reg
*reg
) override
2031 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2032 reg
->name
.c_str (), reg
->target_regnum
,
2034 if (!reg
->group
.empty ())
2035 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
2037 printf_unfiltered ("NULL, ");
2038 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
2042 std::string m_filename_after_features
;
2046 bool m_printed_field_type
= false;
2047 bool m_printed_type
= false;
2050 /* Print target description feature in C. */
2052 class print_c_feature
: public print_c_tdesc
2055 print_c_feature (std::string
&file
)
2056 : print_c_tdesc (file
)
2059 auto const pos
= m_filename_after_features
.find_last_of ('.');
2061 m_filename_after_features
= m_filename_after_features
.substr (0, pos
);
2064 void visit_pre (const target_desc
*e
) override
2066 printf_unfiltered (" Original: %s */\n\n",
2067 lbasename (m_filename_after_features
.c_str ()));
2069 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2070 printf_unfiltered ("\n");
2073 void visit_post (const target_desc
*e
) override
2076 void visit_pre (const tdesc_feature
*e
) override
2078 std::string
name (m_filename_after_features
);
2080 auto pos
= name
.find_first_of ('.');
2082 name
= name
.substr (0, pos
);
2083 std::replace (name
.begin (), name
.end (), '/', '_');
2084 std::replace (name
.begin (), name
.end (), '-', '_');
2086 printf_unfiltered ("static int\n");
2087 printf_unfiltered ("create_feature_%s ", name
.c_str ());
2088 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2090 printf_unfiltered ("{\n");
2091 printf_unfiltered (" struct tdesc_feature *feature;\n");
2094 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2095 e
->name
.c_str (), lbasename (m_filename_after_features
.c_str ()));
2098 void visit_post (const tdesc_feature
*e
) override
2100 printf_unfiltered (" return regnum;\n");
2101 printf_unfiltered ("}\n");
2104 void visit (const tdesc_reg
*reg
) override
2106 /* Most "reg" in XML target descriptions don't have "regnum"
2107 attribute, so the register number is allocated sequentially.
2108 In case that reg has "regnum" attribute, register number
2109 should be set by that explicitly. */
2111 if (reg
->target_regnum
< m_next_regnum
)
2113 /* The integrity check, it can catch some errors on register
2114 number collision, like this,
2116 <reg name="x0" bitsize="32"/>
2117 <reg name="x1" bitsize="32"/>
2118 <reg name="x2" bitsize="32"/>
2119 <reg name="x3" bitsize="32"/>
2120 <reg name="ps" bitsize="32" regnum="3"/>
2122 but it also has false negatives. The target description
2125 <reg name="x1" bitsize="32" regnum="1"/>
2126 <reg name="x3" bitsize="32" regnum="3"/>
2127 <reg name="x2" bitsize="32" regnum="2"/>
2128 <reg name="x4" bitsize="32" regnum="4"/>
2130 but it is not a good practice, so still error on this,
2131 and also print the message so that it can be saved in the
2132 generated c file. */
2134 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2135 reg
->target_regnum
);
2136 printf_unfiltered ("is not the largest number (%d).\n",
2138 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2139 reg
->target_regnum
, m_next_regnum
);
2142 if (reg
->target_regnum
> m_next_regnum
)
2144 printf_unfiltered (" regnum = %ld;\n", reg
->target_regnum
);
2145 m_next_regnum
= reg
->target_regnum
;
2148 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2149 reg
->name
.c_str (), reg
->save_restore
);
2150 if (!reg
->group
.empty ())
2151 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
2153 printf_unfiltered ("NULL, ");
2154 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
2160 /* The register number to use for the next register we see. */
2161 int m_next_regnum
= 0;
2165 maint_print_c_tdesc_cmd (const char *args
, int from_tty
)
2167 const struct target_desc
*tdesc
;
2168 const char *filename
;
2172 /* Use the global target-supplied description, not the current
2173 architecture's. This lets a GDB for one architecture generate C
2174 for another architecture's description, even though the gdbarch
2175 initialization code will reject the new description. */
2176 tdesc
= current_target_desc
;
2177 filename
= target_description_filename
;
2181 /* Use the target description from the XML file. */
2183 tdesc
= file_read_description_xml (filename
);
2187 error (_("There is no target description to print."));
2189 if (filename
== NULL
)
2190 error (_("The current target description did not come from an XML file."));
2192 std::string
filename_after_features (filename
);
2193 auto loc
= filename_after_features
.rfind ("/features/");
2195 if (loc
!= std::string::npos
)
2196 filename_after_features
= filename_after_features
.substr (loc
+ 10);
2198 /* Print c files for target features instead of target descriptions,
2199 because c files got from target features are more flexible than the
2201 if (startswith (filename_after_features
.c_str (), "i386/32bit-")
2202 || startswith (filename_after_features
.c_str (), "i386/64bit-")
2203 || startswith (filename_after_features
.c_str (), "i386/x32-core.xml")
2204 || startswith (filename_after_features
.c_str (), "tic6x-")
2205 || startswith (filename_after_features
.c_str (), "aarch64"))
2207 print_c_feature
v (filename_after_features
);
2213 print_c_tdesc
v (filename_after_features
);
2219 namespace selftests
{
2221 static std::vector
<std::pair
<const char*, const target_desc
*>> xml_tdesc
;
2225 /* See target-descritpions.h. */
2228 record_xml_tdesc (const char *xml_file
, const struct target_desc
*tdesc
)
2230 xml_tdesc
.emplace_back (xml_file
, tdesc
);
2236 /* Check that the target descriptions created dynamically by
2237 architecture-specific code equal the descriptions created from XML files
2238 found in the specified directory DIR. */
2241 maintenance_check_xml_descriptions (const char *dir
, int from_tty
)
2244 error (_("Missing dir name"));
2246 gdb::unique_xmalloc_ptr
<char> dir1 (tilde_expand (dir
));
2247 std::string
feature_dir (dir1
.get ());
2248 unsigned int failed
= 0;
2250 for (auto const &e
: selftests::xml_tdesc
)
2252 std::string tdesc_xml
= (feature_dir
+ SLASH_STRING
+ e
.first
);
2253 const target_desc
*tdesc
2254 = file_read_description_xml (tdesc_xml
.data ());
2256 if (tdesc
== NULL
|| *tdesc
!= *e
.second
)
2259 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2260 (long) selftests::xml_tdesc
.size (), failed
);
2264 _initialize_target_descriptions (void)
2266 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
2268 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
2269 Set target description specific variables."),
2270 &tdesc_set_cmdlist
, "set tdesc ",
2271 0 /* allow-unknown */, &setlist
);
2272 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
2273 Show target description specific variables."),
2274 &tdesc_show_cmdlist
, "show tdesc ",
2275 0 /* allow-unknown */, &showlist
);
2276 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
2277 Unset target description specific variables."),
2278 &tdesc_unset_cmdlist
, "unset tdesc ",
2279 0 /* allow-unknown */, &unsetlist
);
2281 add_setshow_filename_cmd ("filename", class_obscure
,
2282 &tdesc_filename_cmd_string
,
2284 Set the file to read for an XML target description"), _("\
2285 Show the file to read for an XML target description"), _("\
2286 When set, GDB will read the target description from a local\n\
2287 file instead of querying the remote target."),
2288 set_tdesc_filename_cmd
,
2289 show_tdesc_filename_cmd
,
2290 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
2292 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
2293 Unset the file to read for an XML target description. When unset,\n\
2294 GDB will read the description from the target."),
2295 &tdesc_unset_cmdlist
);
2297 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
2298 Print the current target description as a C source file."),
2299 &maintenanceprintlist
);
2301 cmd_list_element
*cmd
;
2303 cmd
= add_cmd ("xml-descriptions", class_maintenance
,
2304 maintenance_check_xml_descriptions
, _("\
2305 Check the target descriptions created in GDB equal the descriptions\n\
2306 created from XML files in the directory.\n\
2307 The parameter is the directory name."),
2308 &maintenancechecklist
);
2309 set_cmd_completer (cmd
, filename_completer
);