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;
64 typedef struct property
69 DEF_VEC_O(property_s);
71 /* An individual register from a target description. */
73 typedef struct tdesc_reg : tdesc_element
75 tdesc_reg (struct tdesc_feature *feature, const char *name_,
76 int regnum, int save_restore_, const char *group_,
77 int bitsize_, const char *type_)
78 : name (xstrdup (name_)), target_regnum (regnum),
79 save_restore (save_restore_),
80 group (group_ != NULL ? xstrdup (group_) : NULL),
82 type (type_ != NULL ? xstrdup (type_) : xstrdup ("<unknown>"))
84 /* If the register's type is target-defined, look it up now. We may not
85 have easy access to the containing feature when we want it later. */
86 tdesc_type = tdesc_named_type (feature, type);
96 /* Disable copying. */
97 tdesc_reg (const tdesc_reg &) = delete;
98 tdesc_reg &operator= (const tdesc_reg &) = delete;
100 /* The name of this register. In standard features, it may be
101 recognized by the architecture support code, or it may be purely
105 /* The register number used by this target to refer to this
106 register. This is used for remote p/P packets and to determine
107 the ordering of registers in the remote g/G packets. */
110 /* If this flag is set, GDB should save and restore this register
111 around calls to an inferior function. */
114 /* The name of the register group containing this register, or NULL
115 if the group should be automatically determined from the
116 register's type. If this is "general", "float", or "vector", the
117 corresponding "info" command should display this register's
118 value. It can be an arbitrary string, but should be limited to
119 alphanumeric characters and internal hyphens. Currently other
120 strings are ignored (treated as NULL). */
123 /* The size of the register, in bits. */
126 /* The type of the register. This string corresponds to either
127 a named type from the target description or a predefined
131 /* The target-described type corresponding to TYPE, if found. */
132 struct tdesc_type *tdesc_type;
134 void accept (tdesc_element_visitor &v) const override
139 bool operator== (const tdesc_reg &other) const
141 return (streq (name, other.name)
142 && target_regnum == other.target_regnum
143 && save_restore == other.save_restore
144 && bitsize == other.bitsize
145 && (group == other.group || streq (group, other.group))
146 && streq (type, other.type));
149 bool operator!= (const tdesc_reg &other) const
151 return !(*this == other);
154 DEF_VEC_P(tdesc_reg_p);
156 /* A named type from a target description. */
158 typedef struct tdesc_type_field
161 struct tdesc_type *type;
162 /* For non-enum-values, either both are -1 (non-bitfield), or both are
163 not -1 (bitfield). For enum values, start is the value (which could be
167 DEF_VEC_O(tdesc_type_field);
171 /* Predefined types. */
185 TDESC_TYPE_IEEE_SINGLE,
186 TDESC_TYPE_IEEE_DOUBLE,
187 TDESC_TYPE_ARM_FPA_EXT,
190 /* Types defined by a target feature. */
198 typedef struct tdesc_type : tdesc_element
200 tdesc_type (const char *name_, enum tdesc_type_kind kind_)
201 : name (xstrdup (name_)), kind (kind_)
203 memset (&u, 0, sizeof (u));
206 virtual ~tdesc_type ()
210 case TDESC_TYPE_STRUCT:
211 case TDESC_TYPE_UNION:
212 case TDESC_TYPE_FLAGS:
213 case TDESC_TYPE_ENUM:
215 struct tdesc_type_field *f;
219 VEC_iterate (tdesc_type_field, u.u.fields, ix, f);
223 VEC_free (tdesc_type_field, u.u.fields);
230 xfree ((char *) name);
232 /* Disable copying. */
233 tdesc_type (const tdesc_type &) = delete;
234 tdesc_type &operator= (const tdesc_type &) = delete;
236 /* The name of this type. If this type is a built-in type, this is
237 a pointer to a constant string. Otherwise, it's a
238 malloc-allocated string (and thus must be freed). */
241 /* Identify the kind of this type. */
242 enum tdesc_type_kind kind;
244 /* Kind-specific data. */
250 struct tdesc_type *type;
254 /* Struct, union, flags, or enum type. */
257 VEC(tdesc_type_field) *fields;
262 void accept (tdesc_element_visitor &v) const override
267 bool operator== (const tdesc_type &other) const
269 return (streq (name, other.name) && kind == other.kind);
272 bool operator!= (const tdesc_type &other) const
274 return !(*this == other);
277 DEF_VEC_P(tdesc_type_p);
279 /* A feature from a target description. Each feature is a collection
280 of other elements, e.g. registers and types. */
282 typedef struct tdesc_feature : tdesc_element
284 tdesc_feature (const char *name_)
285 : name (xstrdup (name_))
288 virtual ~tdesc_feature ()
290 struct tdesc_reg *reg;
291 struct tdesc_type *type;
294 for (ix = 0; VEC_iterate (tdesc_reg_p, registers, ix, reg); ix++)
296 VEC_free (tdesc_reg_p, registers);
298 for (ix = 0; VEC_iterate (tdesc_type_p, types, ix, type); ix++)
300 VEC_free (tdesc_type_p, types);
305 /* Disable copying. */
306 tdesc_feature (const tdesc_feature &) = delete;
307 tdesc_feature &operator= (const tdesc_feature &) = delete;
309 /* The name of this feature. It may be recognized by the architecture
313 /* The registers associated with this feature. */
314 VEC(tdesc_reg_p) *registers = NULL;
316 /* The types associated with this feature. */
317 VEC(tdesc_type_p) *types = NULL;
319 void accept (tdesc_element_visitor &v) const override
323 struct tdesc_type *type;
326 VEC_iterate (tdesc_type_p, types, ix, type);
330 struct tdesc_reg *reg;
333 VEC_iterate (tdesc_reg_p, registers, ix, reg);
341 bool operator== (const tdesc_feature &other) const
343 if (strcmp (name, other.name) != 0)
346 if (VEC_length (tdesc_reg_p, registers)
347 != VEC_length (tdesc_reg_p, other.registers))
350 struct tdesc_reg *reg;
353 VEC_iterate (tdesc_reg_p, registers, ix, reg);
357 = VEC_index (tdesc_reg_p, other.registers, ix);
359 if (reg != reg2 && *reg != *reg2)
363 if (VEC_length (tdesc_type_p, types)
364 != VEC_length (tdesc_type_p, other.types))
370 VEC_iterate (tdesc_type_p, types, ix, type);
374 = VEC_index (tdesc_type_p, other.types, ix);
376 if (type != type2 && *type != *type2)
383 bool operator!= (const tdesc_feature &other) const
385 return !(*this == other);
389 DEF_VEC_P(tdesc_feature_p);
391 /* A compatible architecture from a target description. */
392 typedef const struct bfd_arch_info *arch_p;
395 /* A target description. */
397 struct target_desc : tdesc_element
402 virtual ~target_desc ()
404 struct tdesc_feature *feature;
405 struct property *prop;
409 VEC_iterate (tdesc_feature_p, features, ix, feature);
412 VEC_free (tdesc_feature_p, features);
415 VEC_iterate (property_s, properties, ix, prop);
422 VEC_free (property_s, properties);
423 VEC_free (arch_p, compatible);
426 target_desc (const target_desc &) = delete;
427 void operator= (const target_desc &) = delete;
429 /* The architecture reported by the target, if any. */
430 const struct bfd_arch_info *arch = NULL;
432 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
434 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
436 /* The list of compatible architectures reported by the target. */
437 VEC(arch_p) *compatible = NULL;
439 /* Any architecture-specific properties specified by the target. */
440 VEC(property_s) *properties = NULL;
442 /* The features associated with this target. */
443 VEC(tdesc_feature_p) *features = NULL;
445 void accept (tdesc_element_visitor &v) const override
449 struct tdesc_feature *feature;
452 VEC_iterate (tdesc_feature_p, features, ix, feature);
459 bool operator== (const target_desc &other) const
461 if (arch != other.arch)
464 if (osabi != other.osabi)
467 if (VEC_length (tdesc_feature_p, features)
468 != VEC_length (tdesc_feature_p, other.features))
471 struct tdesc_feature *feature;
474 VEC_iterate (tdesc_feature_p, features, ix, feature);
477 struct tdesc_feature *feature2
478 = VEC_index (tdesc_feature_p, other.features, ix);
480 if (feature != feature2 && *feature != *feature2)
487 bool operator!= (const target_desc &other) const
489 return !(*this == other);
493 /* Per-architecture data associated with a target description. The
494 target description may be shared by multiple architectures, but
495 this data is private to one gdbarch. */
497 typedef struct tdesc_arch_reg
499 struct tdesc_reg *reg;
502 DEF_VEC_O(tdesc_arch_reg);
504 struct tdesc_arch_data
506 /* A list of register/type pairs, indexed by GDB's internal register number.
507 During initialization of the gdbarch this list is used to store
508 registers which the architecture assigns a fixed register number.
509 Registers which are NULL in this array, or off the end, are
510 treated as zero-sized and nameless (i.e. placeholders in the
512 VEC(tdesc_arch_reg) *arch_regs;
514 /* Functions which report the register name, type, and reggroups for
516 gdbarch_register_name_ftype *pseudo_register_name;
517 gdbarch_register_type_ftype *pseudo_register_type;
518 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
521 /* Info about an inferior's target description. There's one of these
522 for each inferior. */
524 struct target_desc_info
526 /* A flag indicating that a description has already been fetched
527 from the target, so it should not be queried again. */
531 /* The description fetched from the target, or NULL if the target
532 did not supply any description. Only valid when
533 target_desc_fetched is set. Only the description initialization
534 code should access this; normally, the description should be
535 accessed through the gdbarch object. */
537 const struct target_desc *tdesc;
539 /* The filename to read a target description from, as set by "set
540 tdesc filename ..." */
545 /* Get the inferior INF's target description info, allocating one on
546 the stop if necessary. */
548 static struct target_desc_info *
549 get_tdesc_info (struct inferior *inf)
551 if (inf->tdesc_info == NULL)
552 inf->tdesc_info = XCNEW (struct target_desc_info);
553 return inf->tdesc_info;
556 /* A handle for architecture-specific data associated with the
557 target description (see struct tdesc_arch_data). */
559 static struct gdbarch_data *tdesc_data;
561 /* See target-descriptions.h. */
564 target_desc_info_from_user_p (struct target_desc_info *info)
566 return info != NULL && info->filename != NULL;
569 /* See target-descriptions.h. */
572 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
574 struct target_desc_info *src = get_tdesc_info (srcinf);
575 struct target_desc_info *dest = get_tdesc_info (destinf);
577 dest->fetched = src->fetched;
578 dest->tdesc = src->tdesc;
579 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
582 /* See target-descriptions.h. */
585 target_desc_info_free (struct target_desc_info *tdesc_info)
587 if (tdesc_info != NULL)
589 xfree (tdesc_info->filename);
594 /* Convenience helper macros. */
596 #define target_desc_fetched \
597 get_tdesc_info (current_inferior ())->fetched
598 #define current_target_desc \
599 get_tdesc_info (current_inferior ())->tdesc
600 #define target_description_filename \
601 get_tdesc_info (current_inferior ())->filename
603 /* The string manipulated by the "set tdesc filename ..." command. */
605 static char *tdesc_filename_cmd_string;
607 /* Fetch the current target's description, and switch the current
608 architecture to one which incorporates that description. */
611 target_find_description (void)
613 /* If we've already fetched a description from the target, don't do
614 it again. This allows a target to fetch the description early,
615 during its to_open or to_create_inferior, if it needs extra
616 information about the target to initialize. */
617 if (target_desc_fetched)
620 /* The current architecture should not have any target description
621 specified. It should have been cleared, e.g. when we
622 disconnected from the previous target. */
623 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
625 /* First try to fetch an XML description from the user-specified
627 current_target_desc = NULL;
628 if (target_description_filename != NULL
629 && *target_description_filename != '\0')
631 = file_read_description_xml (target_description_filename);
633 /* Next try to read the description from the current target using
635 if (current_target_desc == NULL)
636 current_target_desc = target_read_description_xml (¤t_target);
638 /* If that failed try a target-specific hook. */
639 if (current_target_desc == NULL)
640 current_target_desc = target_read_description (¤t_target);
642 /* If a non-NULL description was returned, then update the current
644 if (current_target_desc)
646 struct gdbarch_info info;
648 gdbarch_info_init (&info);
649 info.target_desc = current_target_desc;
650 if (!gdbarch_update_p (info))
651 warning (_("Architecture rejected target-supplied description"));
654 struct tdesc_arch_data *data;
656 data = ((struct tdesc_arch_data *)
657 gdbarch_data (target_gdbarch (), tdesc_data));
658 if (tdesc_has_registers (current_target_desc)
659 && data->arch_regs == NULL)
660 warning (_("Target-supplied registers are not supported "
661 "by the current architecture"));
665 /* Now that we know this description is usable, record that we
667 target_desc_fetched = 1;
670 /* Discard any description fetched from the current target, and switch
671 the current architecture to one with no target description. */
674 target_clear_description (void)
676 struct gdbarch_info info;
678 if (!target_desc_fetched)
681 target_desc_fetched = 0;
682 current_target_desc = NULL;
684 gdbarch_info_init (&info);
685 if (!gdbarch_update_p (info))
686 internal_error (__FILE__, __LINE__,
687 _("Could not remove target-supplied description"));
690 /* Return the global current target description. This should only be
691 used by gdbarch initialization code; most access should be through
692 an existing gdbarch. */
694 const struct target_desc *
695 target_current_description (void)
697 if (target_desc_fetched)
698 return current_target_desc;
703 /* Return non-zero if this target description is compatible
704 with the given BFD architecture. */
707 tdesc_compatible_p (const struct target_desc *target_desc,
708 const struct bfd_arch_info *arch)
710 const struct bfd_arch_info *compat;
713 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
717 || arch->compatible (arch, compat)
718 || compat->compatible (compat, arch))
726 /* Direct accessors for target descriptions. */
728 /* Return the string value of a property named KEY, or NULL if the
729 property was not specified. */
732 tdesc_property (const struct target_desc *target_desc, const char *key)
734 struct property *prop;
737 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
739 if (strcmp (prop->key, key) == 0)
745 /* Return the BFD architecture associated with this target
746 description, or NULL if no architecture was specified. */
748 const struct bfd_arch_info *
749 tdesc_architecture (const struct target_desc *target_desc)
751 return target_desc->arch;
754 /* Return the OSABI associated with this target description, or
755 GDB_OSABI_UNKNOWN if no osabi was specified. */
758 tdesc_osabi (const struct target_desc *target_desc)
760 return target_desc->osabi;
765 /* Return 1 if this target description includes any registers. */
768 tdesc_has_registers (const struct target_desc *target_desc)
771 struct tdesc_feature *feature;
773 if (target_desc == NULL)
777 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
779 if (! VEC_empty (tdesc_reg_p, feature->registers))
785 /* Return the feature with the given name, if present, or NULL if
786 the named feature is not found. */
788 const struct tdesc_feature *
789 tdesc_find_feature (const struct target_desc *target_desc,
793 struct tdesc_feature *feature;
796 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
798 if (strcmp (feature->name, name) == 0)
804 /* Return the name of FEATURE. */
807 tdesc_feature_name (const struct tdesc_feature *feature)
809 return feature->name;
812 /* Predefined types. */
813 static struct tdesc_type tdesc_predefined_types[] =
815 { "bool", TDESC_TYPE_BOOL },
816 { "int8", TDESC_TYPE_INT8 },
817 { "int16", TDESC_TYPE_INT16 },
818 { "int32", TDESC_TYPE_INT32 },
819 { "int64", TDESC_TYPE_INT64 },
820 { "int128", TDESC_TYPE_INT128 },
821 { "uint8", TDESC_TYPE_UINT8 },
822 { "uint16", TDESC_TYPE_UINT16 },
823 { "uint32", TDESC_TYPE_UINT32 },
824 { "uint64", TDESC_TYPE_UINT64 },
825 { "uint128", TDESC_TYPE_UINT128 },
826 { "code_ptr", TDESC_TYPE_CODE_PTR },
827 { "data_ptr", TDESC_TYPE_DATA_PTR },
828 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
829 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
830 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
831 { "i387_ext", TDESC_TYPE_I387_EXT }
834 /* Lookup a predefined type. */
836 static struct tdesc_type *
837 tdesc_predefined_type (enum tdesc_type_kind kind)
840 struct tdesc_type *type;
842 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
843 if (tdesc_predefined_types[ix].kind == kind)
844 return &tdesc_predefined_types[ix];
846 gdb_assert_not_reached ("bad predefined tdesc type");
849 /* Return the type associated with ID in the context of FEATURE, or
853 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
856 struct tdesc_type *type;
858 /* First try target-defined types. */
859 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
860 if (strcmp (type->name, id) == 0)
863 /* Next try the predefined types. */
864 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
865 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
866 return &tdesc_predefined_types[ix];
871 /* Lookup type associated with ID. */
874 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
876 struct tdesc_arch_reg *reg;
877 struct tdesc_arch_data *data;
880 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
881 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
882 for (i = 0; i < num_regs; i++)
884 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
886 && reg->reg->tdesc_type
888 && strcmp (id, reg->reg->tdesc_type->name) == 0)
895 /* Construct, if necessary, and return the GDB type implementing target
896 type TDESC_TYPE for architecture GDBARCH. */
899 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
903 switch (tdesc_type->kind)
905 /* Predefined types. */
906 case TDESC_TYPE_BOOL:
907 return builtin_type (gdbarch)->builtin_bool;
909 case TDESC_TYPE_INT8:
910 return builtin_type (gdbarch)->builtin_int8;
912 case TDESC_TYPE_INT16:
913 return builtin_type (gdbarch)->builtin_int16;
915 case TDESC_TYPE_INT32:
916 return builtin_type (gdbarch)->builtin_int32;
918 case TDESC_TYPE_INT64:
919 return builtin_type (gdbarch)->builtin_int64;
921 case TDESC_TYPE_INT128:
922 return builtin_type (gdbarch)->builtin_int128;
924 case TDESC_TYPE_UINT8:
925 return builtin_type (gdbarch)->builtin_uint8;
927 case TDESC_TYPE_UINT16:
928 return builtin_type (gdbarch)->builtin_uint16;
930 case TDESC_TYPE_UINT32:
931 return builtin_type (gdbarch)->builtin_uint32;
933 case TDESC_TYPE_UINT64:
934 return builtin_type (gdbarch)->builtin_uint64;
936 case TDESC_TYPE_UINT128:
937 return builtin_type (gdbarch)->builtin_uint128;
939 case TDESC_TYPE_CODE_PTR:
940 return builtin_type (gdbarch)->builtin_func_ptr;
942 case TDESC_TYPE_DATA_PTR:
943 return builtin_type (gdbarch)->builtin_data_ptr;
949 type = tdesc_find_type (gdbarch, tdesc_type->name);
953 switch (tdesc_type->kind)
955 case TDESC_TYPE_IEEE_SINGLE:
956 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
957 floatformats_ieee_single);
959 case TDESC_TYPE_IEEE_DOUBLE:
960 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
961 floatformats_ieee_double);
963 case TDESC_TYPE_ARM_FPA_EXT:
964 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
965 floatformats_arm_ext);
967 case TDESC_TYPE_I387_EXT:
968 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
969 floatformats_i387_ext);
971 /* Types defined by a target feature. */
972 case TDESC_TYPE_VECTOR:
974 struct type *type, *field_type;
976 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
977 type = init_vector_type (field_type, tdesc_type->u.v.count);
978 TYPE_NAME (type) = xstrdup (tdesc_type->name);
983 case TDESC_TYPE_STRUCT:
985 struct type *type, *field_type;
986 struct tdesc_type_field *f;
989 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
990 TYPE_NAME (type) = xstrdup (tdesc_type->name);
991 TYPE_TAG_NAME (type) = TYPE_NAME (type);
994 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
997 if (f->start != -1 && f->end != -1)
1001 struct type *field_type;
1002 int bitsize, total_size;
1004 /* This invariant should be preserved while creating types. */
1005 gdb_assert (tdesc_type->u.u.size != 0);
1006 if (f->type != NULL)
1007 field_type = tdesc_gdb_type (gdbarch, f->type);
1008 else if (tdesc_type->u.u.size > 4)
1009 field_type = builtin_type (gdbarch)->builtin_uint64;
1011 field_type = builtin_type (gdbarch)->builtin_uint32;
1013 fld = append_composite_type_field_raw (type, xstrdup (f->name),
1016 /* For little-endian, BITPOS counts from the LSB of
1017 the structure and marks the LSB of the field. For
1018 big-endian, BITPOS counts from the MSB of the
1019 structure and marks the MSB of the field. Either
1020 way, it is the number of bits to the "left" of the
1021 field. To calculate this in big-endian, we need
1022 the total size of the structure. */
1023 bitsize = f->end - f->start + 1;
1024 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
1025 if (gdbarch_bits_big_endian (gdbarch))
1026 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
1028 SET_FIELD_BITPOS (fld[0], f->start);
1029 FIELD_BITSIZE (fld[0]) = bitsize;
1033 gdb_assert (f->start == -1 && f->end == -1);
1034 field_type = tdesc_gdb_type (gdbarch, f->type);
1035 append_composite_type_field (type, xstrdup (f->name),
1040 if (tdesc_type->u.u.size != 0)
1041 TYPE_LENGTH (type) = tdesc_type->u.u.size;
1045 case TDESC_TYPE_UNION:
1047 struct type *type, *field_type;
1048 struct tdesc_type_field *f;
1051 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1052 TYPE_NAME (type) = xstrdup (tdesc_type->name);
1055 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1058 field_type = tdesc_gdb_type (gdbarch, f->type);
1059 append_composite_type_field (type, xstrdup (f->name), field_type);
1061 /* If any of the children of a union are vectors, flag the
1062 union as a vector also. This allows e.g. a union of two
1063 vector types to show up automatically in "info vector". */
1064 if (TYPE_VECTOR (field_type))
1065 TYPE_VECTOR (type) = 1;
1070 case TDESC_TYPE_FLAGS:
1072 struct tdesc_type_field *f;
1075 type = arch_flags_type (gdbarch, tdesc_type->name,
1076 tdesc_type->u.u.size);
1078 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1081 struct type *field_type;
1082 int bitsize = f->end - f->start + 1;
1084 gdb_assert (f->type != NULL);
1085 field_type = tdesc_gdb_type (gdbarch, f->type);
1086 append_flags_type_field (type, f->start, bitsize,
1087 field_type, f->name);
1093 case TDESC_TYPE_ENUM:
1095 struct tdesc_type_field *f;
1098 type = arch_type (gdbarch, TYPE_CODE_ENUM,
1099 tdesc_type->u.u.size, tdesc_type->name);
1100 TYPE_UNSIGNED (type) = 1;
1102 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1106 = append_composite_type_field_raw (type, xstrdup (f->name),
1109 SET_FIELD_BITPOS (fld[0], f->start);
1116 internal_error (__FILE__, __LINE__,
1117 "Type \"%s\" has an unknown kind %d",
1118 tdesc_type->name, tdesc_type->kind);
1122 /* Support for registers from target descriptions. */
1124 /* Construct the per-gdbarch data. */
1127 tdesc_data_init (struct obstack *obstack)
1129 struct tdesc_arch_data *data;
1131 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1135 /* Similar, but for the temporary copy used during architecture
1138 struct tdesc_arch_data *
1139 tdesc_data_alloc (void)
1141 return XCNEW (struct tdesc_arch_data);
1144 /* Free something allocated by tdesc_data_alloc, if it is not going
1145 to be used (for instance if it was unsuitable for the
1149 tdesc_data_cleanup (void *data_untyped)
1151 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1153 VEC_free (tdesc_arch_reg, data->arch_regs);
1157 /* Search FEATURE for a register named NAME. */
1159 static struct tdesc_reg *
1160 tdesc_find_register_early (const struct tdesc_feature *feature,
1164 struct tdesc_reg *reg;
1167 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1169 if (strcasecmp (reg->name, name) == 0)
1175 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1178 tdesc_numbered_register (const struct tdesc_feature *feature,
1179 struct tdesc_arch_data *data,
1180 int regno, const char *name)
1182 struct tdesc_arch_reg arch_reg = { 0 };
1183 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1188 /* Make sure the vector includes a REGNO'th element. */
1189 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1190 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1193 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
1197 /* Search FEATURE for a register named NAME, but do not assign a fixed
1198 register number to it. */
1201 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1204 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1212 /* Search FEATURE for a register whose name is in NAMES and assign
1216 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1217 struct tdesc_arch_data *data,
1218 int regno, const char *const names[])
1222 for (i = 0; names[i] != NULL; i++)
1223 if (tdesc_numbered_register (feature, data, regno, names[i]))
1229 /* Search FEATURE for a register named NAME, and return its size in
1230 bits. The register must exist. */
1233 tdesc_register_size (const struct tdesc_feature *feature,
1236 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1238 gdb_assert (reg != NULL);
1239 return reg->bitsize;
1242 /* Look up a register by its GDB internal register number. */
1244 static struct tdesc_arch_reg *
1245 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1247 struct tdesc_arch_data *data;
1249 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1250 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1251 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
1256 static struct tdesc_reg *
1257 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1259 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1261 return reg? reg->reg : NULL;
1264 /* Return the name of register REGNO, from the target description or
1265 from an architecture-provided pseudo_register_name method. */
1268 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1270 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1271 int num_regs = gdbarch_num_regs (gdbarch);
1272 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1277 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1279 struct tdesc_arch_data *data
1280 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1282 gdb_assert (data->pseudo_register_name != NULL);
1283 return data->pseudo_register_name (gdbarch, regno);
1290 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1292 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1293 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1294 int num_regs = gdbarch_num_regs (gdbarch);
1295 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1297 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1299 struct tdesc_arch_data *data
1300 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1302 gdb_assert (data->pseudo_register_type != NULL);
1303 return data->pseudo_register_type (gdbarch, regno);
1307 /* Return "int0_t", since "void" has a misleading size of one. */
1308 return builtin_type (gdbarch)->builtin_int0;
1310 if (arch_reg->type == NULL)
1312 /* First check for a predefined or target defined type. */
1313 if (reg->tdesc_type)
1314 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1316 /* Next try size-sensitive type shortcuts. */
1317 else if (strcmp (reg->type, "float") == 0)
1319 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1320 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1321 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1322 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1323 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1324 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1327 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1328 reg->name, reg->bitsize);
1329 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1332 else if (strcmp (reg->type, "int") == 0)
1334 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1335 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1336 else if (reg->bitsize == TARGET_CHAR_BIT)
1337 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1338 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1339 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1340 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1341 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1342 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1343 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1344 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1345 /* A bit desperate by this point... */
1346 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1349 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1350 reg->name, reg->bitsize);
1351 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1355 if (arch_reg->type == NULL)
1356 internal_error (__FILE__, __LINE__,
1357 "Register \"%s\" has an unknown type \"%s\"",
1358 reg->name, reg->type);
1361 return arch_reg->type;
1365 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1367 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1370 return reg->target_regnum;
1375 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1376 target description may be classified as general, float, or vector.
1377 Unlike a gdbarch register_reggroup_p method, this function will
1378 return -1 if it does not know; the caller should handle registers
1379 with no specified group.
1381 Arbitrary strings (other than "general", "float", and "vector")
1382 from the description are not used; they cause the register to be
1383 displayed in "info all-registers" but excluded from "info
1384 registers" et al. The names of containing features are also not
1385 used. This might be extended to display registers in some more
1388 The save-restore flag is also implemented here. */
1391 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1392 struct reggroup *reggroup)
1394 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1396 if (reg != NULL && reg->group != NULL)
1398 int general_p = 0, float_p = 0, vector_p = 0;
1400 if (strcmp (reg->group, "general") == 0)
1402 else if (strcmp (reg->group, "float") == 0)
1404 else if (strcmp (reg->group, "vector") == 0)
1407 if (reggroup == float_reggroup)
1410 if (reggroup == vector_reggroup)
1413 if (reggroup == general_reggroup)
1418 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1419 return reg->save_restore;
1424 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1425 group specified go to the default reggroup function and are handled
1429 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1430 struct reggroup *reggroup)
1432 int num_regs = gdbarch_num_regs (gdbarch);
1433 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1436 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1438 struct tdesc_arch_data *data
1439 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1441 if (data->pseudo_register_reggroup_p != NULL)
1442 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1443 /* Otherwise fall through to the default reggroup_p. */
1446 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1450 return default_register_reggroup_p (gdbarch, regno, reggroup);
1453 /* Record architecture-specific functions to call for pseudo-register
1457 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1458 gdbarch_register_name_ftype *pseudo_name)
1460 struct tdesc_arch_data *data
1461 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1463 data->pseudo_register_name = pseudo_name;
1467 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1468 gdbarch_register_type_ftype *pseudo_type)
1470 struct tdesc_arch_data *data
1471 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1473 data->pseudo_register_type = pseudo_type;
1477 set_tdesc_pseudo_register_reggroup_p
1478 (struct gdbarch *gdbarch,
1479 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1481 struct tdesc_arch_data *data
1482 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1484 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1487 /* Update GDBARCH to use the target description for registers. */
1490 tdesc_use_registers (struct gdbarch *gdbarch,
1491 const struct target_desc *target_desc,
1492 struct tdesc_arch_data *early_data)
1494 int num_regs = gdbarch_num_regs (gdbarch);
1496 struct tdesc_feature *feature;
1497 struct tdesc_reg *reg;
1498 struct tdesc_arch_data *data;
1499 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1502 /* We can't use the description for registers if it doesn't describe
1503 any. This function should only be called after validating
1504 registers, so the caller should know that registers are
1506 gdb_assert (tdesc_has_registers (target_desc));
1508 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1509 data->arch_regs = early_data->arch_regs;
1512 /* Build up a set of all registers, so that we can assign register
1513 numbers where needed. The hash table expands as necessary, so
1514 the initial size is arbitrary. */
1515 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1517 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1520 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1523 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1528 /* Remove any registers which were assigned numbers by the
1531 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1534 htab_remove_elt (reg_hash, arch_reg->reg);
1536 /* Assign numbers to the remaining registers and add them to the
1537 list of registers. The new numbers are always above gdbarch_num_regs.
1538 Iterate over the features, not the hash table, so that the order
1539 matches that in the target description. */
1541 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1542 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1543 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1545 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1548 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1550 if (htab_find (reg_hash, reg) != NULL)
1552 new_arch_reg.reg = reg;
1553 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1557 htab_delete (reg_hash);
1559 /* Update the architecture. */
1560 set_gdbarch_num_regs (gdbarch, num_regs);
1561 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1562 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1563 set_gdbarch_remote_register_number (gdbarch,
1564 tdesc_remote_register_number);
1565 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1570 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1571 int regnum, int save_restore, const char *group,
1572 int bitsize, const char *type)
1574 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1575 group, bitsize, type);
1577 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1581 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1582 struct tdesc_type *field_type, int count)
1584 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1586 type->u.v.type = field_type;
1587 type->u.v.count = count;
1589 VEC_safe_push (tdesc_type_p, feature->types, type);
1594 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1596 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1598 VEC_safe_push (tdesc_type_p, feature->types, type);
1602 /* Set the total length of TYPE. Structs which contain bitfields may
1603 omit the reserved bits, so the end of the last field may not
1607 tdesc_set_struct_size (struct tdesc_type *type, int size)
1609 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1610 gdb_assert (size > 0);
1611 type->u.u.size = size;
1615 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1617 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1619 VEC_safe_push (tdesc_type_p, feature->types, type);
1624 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1627 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1629 gdb_assert (size > 0);
1631 type->u.u.size = size;
1633 VEC_safe_push (tdesc_type_p, feature->types, type);
1638 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1641 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1643 gdb_assert (size > 0);
1645 type->u.u.size = size;
1647 VEC_safe_push (tdesc_type_p, feature->types, type);
1651 /* Add a new field to TYPE. */
1654 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1655 struct tdesc_type *field_type)
1657 struct tdesc_type_field f = { 0 };
1659 gdb_assert (type->kind == TDESC_TYPE_UNION
1660 || type->kind == TDESC_TYPE_STRUCT);
1662 f.name = xstrdup (field_name);
1663 f.type = field_type;
1664 /* Initialize these values so we know this is not a bit-field
1665 when we print-c-tdesc. */
1669 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1672 /* Add a new typed bitfield to TYPE. */
1675 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1676 int start, int end, struct tdesc_type *field_type)
1678 struct tdesc_type_field f = { 0 };
1680 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1681 || type->kind == TDESC_TYPE_FLAGS);
1682 gdb_assert (start >= 0 && end >= start);
1684 f.name = xstrdup (field_name);
1687 f.type = field_type;
1689 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1692 /* Add a new untyped bitfield to TYPE.
1693 Untyped bitfields become either uint32 or uint64 depending on the size
1694 of the underlying type. */
1697 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1700 struct tdesc_type *field_type;
1702 gdb_assert (start >= 0 && end >= start);
1704 if (type->u.u.size > 4)
1705 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1707 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1709 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1712 /* A flag is just a typed(bool) single-bit bitfield.
1713 This function is kept to minimize changes in generated files. */
1716 tdesc_add_flag (struct tdesc_type *type, int start,
1717 const char *flag_name)
1719 struct tdesc_type_field f = { 0 };
1721 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1722 || type->kind == TDESC_TYPE_STRUCT);
1724 f.name = xstrdup (flag_name);
1727 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
1729 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1733 tdesc_add_enum_value (struct tdesc_type *type, int value,
1736 struct tdesc_type_field f = { 0 };
1738 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1740 f.name = xstrdup (name);
1743 f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1745 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1748 struct tdesc_feature *
1749 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1751 struct tdesc_feature *new_feature = new tdesc_feature (name);
1753 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1757 struct target_desc *
1758 allocate_target_description (void)
1760 return new target_desc ();
1764 free_target_description (void *arg)
1766 struct target_desc *target_desc = (struct target_desc *) arg;
1772 make_cleanup_free_target_description (struct target_desc *target_desc)
1774 return make_cleanup (free_target_description, target_desc);
1778 tdesc_add_compatible (struct target_desc *target_desc,
1779 const struct bfd_arch_info *compatible)
1781 const struct bfd_arch_info *compat;
1784 /* If this instance of GDB is compiled without BFD support for the
1785 compatible architecture, simply ignore it -- we would not be able
1786 to handle it anyway. */
1787 if (compatible == NULL)
1790 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1792 if (compat == compatible)
1793 internal_error (__FILE__, __LINE__,
1794 _("Attempted to add duplicate "
1795 "compatible architecture \"%s\""),
1796 compatible->printable_name);
1798 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1802 set_tdesc_property (struct target_desc *target_desc,
1803 const char *key, const char *value)
1805 struct property *prop, new_prop;
1808 gdb_assert (key != NULL && value != NULL);
1810 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1812 if (strcmp (prop->key, key) == 0)
1813 internal_error (__FILE__, __LINE__,
1814 _("Attempted to add duplicate property \"%s\""), key);
1816 new_prop.key = xstrdup (key);
1817 new_prop.value = xstrdup (value);
1818 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1822 set_tdesc_architecture (struct target_desc *target_desc,
1823 const struct bfd_arch_info *arch)
1825 target_desc->arch = arch;
1829 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1831 target_desc->osabi = osabi;
1835 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1836 static struct cmd_list_element *tdesc_unset_cmdlist;
1838 /* Helper functions for the CLI commands. */
1841 set_tdesc_cmd (char *args, int from_tty)
1843 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1847 show_tdesc_cmd (char *args, int from_tty)
1849 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1853 unset_tdesc_cmd (char *args, int from_tty)
1855 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1859 set_tdesc_filename_cmd (char *args, int from_tty,
1860 struct cmd_list_element *c)
1862 xfree (target_description_filename);
1863 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1865 target_clear_description ();
1866 target_find_description ();
1870 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1871 struct cmd_list_element *c,
1874 value = target_description_filename;
1876 if (value != NULL && *value != '\0')
1877 printf_filtered (_("The target description will be read from \"%s\".\n"),
1880 printf_filtered (_("The target description will be "
1881 "read from the target.\n"));
1885 unset_tdesc_filename_cmd (char *args, int from_tty)
1887 xfree (target_description_filename);
1888 target_description_filename = NULL;
1889 target_clear_description ();
1890 target_find_description ();
1893 /* Print target description in C. */
1895 class print_c_tdesc : public tdesc_element_visitor
1898 print_c_tdesc (std::string &filename_after_features)
1899 : m_filename_after_features (filename_after_features)
1903 const char *filename = lbasename (m_filename_after_features.c_str ());
1905 m_function = (char *) xmalloc (strlen (filename) + 1);
1906 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1909 else if (*inp == '-')
1915 /* Standard boilerplate. */
1916 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1917 "-*- buffer-read-only: t -*- vi"
1926 void visit_pre (const target_desc *e) override
1928 printf_unfiltered (" Original: %s */\n\n",
1929 lbasename (m_filename_after_features.c_str ()));
1931 printf_unfiltered ("#include \"defs.h\"\n");
1932 printf_unfiltered ("#include \"osabi.h\"\n");
1933 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1934 printf_unfiltered ("\n");
1936 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1937 printf_unfiltered ("static void\n");
1938 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1939 printf_unfiltered ("{\n");
1941 (" struct target_desc *result = allocate_target_description ();\n");
1943 if (tdesc_architecture (e) != NULL)
1946 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1947 tdesc_architecture (e)->printable_name);
1948 printf_unfiltered ("\n");
1950 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1951 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1954 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1955 gdbarch_osabi_name (tdesc_osabi (e)));
1956 printf_unfiltered ("\n");
1960 const struct bfd_arch_info *compatible;
1961 struct property *prop;
1963 for (ix = 0; VEC_iterate (arch_p, e->compatible, ix, compatible);
1967 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1968 compatible->printable_name);
1972 printf_unfiltered ("\n");
1974 for (ix = 0; VEC_iterate (property_s, e->properties, ix, prop);
1977 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1978 prop->key, prop->value);
1980 printf_unfiltered (" struct tdesc_feature *feature;\n");
1983 void visit_pre (const tdesc_feature *e) override
1985 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1989 void visit_post (const tdesc_feature *e) override
1992 void visit_post (const target_desc *e) override
1994 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1995 printf_unfiltered ("}\n");
1998 void visit (const tdesc_type *type) override
2000 struct tdesc_type_field *f;
2002 /* Now we do some "filtering" in order to know which variables to
2003 declare. This is needed because otherwise we would declare unused
2004 variables `field_type' and `type'. */
2005 if (!m_printed_field_type)
2007 printf_unfiltered (" struct tdesc_type *field_type;\n");
2008 m_printed_field_type = true;
2011 if ((type->kind == TDESC_TYPE_UNION
2012 || type->kind == TDESC_TYPE_STRUCT
2013 || type->kind == TDESC_TYPE_FLAGS
2014 || type->kind == TDESC_TYPE_ENUM)
2015 && VEC_length (tdesc_type_field, type->u.u.fields) > 0
2018 printf_unfiltered (" struct tdesc_type *type;\n");
2019 m_printed_type = true;
2024 case TDESC_TYPE_VECTOR:
2026 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2027 type->u.v.type->name);
2029 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
2030 type->name, type->u.v.count);
2032 case TDESC_TYPE_STRUCT:
2033 case TDESC_TYPE_FLAGS:
2034 if (type->kind == TDESC_TYPE_STRUCT)
2037 (" type = tdesc_create_struct (feature, \"%s\");\n",
2039 if (type->u.u.size != 0)
2041 (" tdesc_set_struct_size (type, %d);\n",
2047 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
2048 type->name, type->u.u.size);
2051 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2054 const char *type_name;
2056 gdb_assert (f->type != NULL);
2057 type_name = f->type->name;
2059 /* To minimize changes to generated files, don't emit type
2060 info for fields that have defaulted types. */
2063 gdb_assert (f->end != -1);
2064 if (f->type->kind == TDESC_TYPE_BOOL)
2066 gdb_assert (f->start == f->end);
2068 (" tdesc_add_flag (type, %d, \"%s\");\n",
2071 else if ((type->u.u.size == 4
2072 && f->type->kind == TDESC_TYPE_UINT32)
2073 || (type->u.u.size == 8
2074 && f->type->kind == TDESC_TYPE_UINT64))
2077 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
2078 f->name, f->start, f->end);
2083 (" field_type = tdesc_named_type (feature,"
2087 (" tdesc_add_typed_bitfield (type, \"%s\","
2088 " %d, %d, field_type);\n",
2089 f->name, f->start, f->end);
2092 else /* Not a bitfield. */
2094 gdb_assert (f->end == -1);
2095 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
2097 (" field_type = tdesc_named_type (feature,"
2101 (" tdesc_add_field (type, \"%s\", field_type);\n",
2106 case TDESC_TYPE_UNION:
2108 (" type = tdesc_create_union (feature, \"%s\");\n",
2111 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2115 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2118 (" tdesc_add_field (type, \"%s\", field_type);\n",
2122 case TDESC_TYPE_ENUM:
2124 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2125 type->name, type->u.u.size);
2127 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2130 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2134 error (_("C output is not supported type \"%s\"."), type->name);
2136 printf_unfiltered ("\n");
2139 void visit (const tdesc_reg *reg) override
2141 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2142 reg->name, reg->target_regnum, reg->save_restore);
2144 printf_unfiltered ("\"%s\", ", reg->group);
2146 printf_unfiltered ("NULL, ");
2147 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2151 std::string m_filename_after_features;
2155 bool m_printed_field_type = false;
2156 bool m_printed_type = false;
2159 /* Print target description feature in C. */
2161 class print_c_feature : public print_c_tdesc
2164 print_c_feature (std::string &file)
2165 : print_c_tdesc (file)
2168 auto const pos = m_filename_after_features.find_last_of ('.');
2170 m_filename_after_features = m_filename_after_features.substr (0, pos);
2173 void visit_pre (const target_desc *e) override
2175 printf_unfiltered (" Original: %s */\n\n",
2176 lbasename (m_filename_after_features.c_str ()));
2178 printf_unfiltered ("#include \"target-descriptions.h\"\n");
2179 printf_unfiltered ("\n");
2182 void visit_post (const target_desc *e) override
2185 void visit_pre (const tdesc_feature *e) override
2187 std::string name (m_filename_after_features);
2189 auto pos = name.find_first_of ('.');
2191 name = name.substr (0, pos);
2192 std::replace (name.begin (), name.end (), '/', '_');
2193 std::replace (name.begin (), name.end (), '-', '_');
2195 printf_unfiltered ("static int\n");
2196 printf_unfiltered ("create_feature_%s ", name.c_str ());
2197 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2199 printf_unfiltered ("{\n");
2200 printf_unfiltered (" struct tdesc_feature *feature;\n");
2201 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
2205 void visit_post (const tdesc_feature *e) override
2207 printf_unfiltered (" return regnum;\n");
2208 printf_unfiltered ("}\n");
2211 void visit (const tdesc_reg *reg) override
2213 /* Most "reg" in XML target descriptions don't have "regnum"
2214 attribute, so the register number is allocated sequentially.
2215 In case that reg has "regnum" attribute, register number
2216 should be set by that explicitly. */
2218 if (reg->target_regnum < m_next_regnum)
2220 /* The integrity check, it can catch some errors on register
2221 number collision, like this,
2223 <reg name="x0" bitsize="32"/>
2224 <reg name="x1" bitsize="32"/>
2225 <reg name="x2" bitsize="32"/>
2226 <reg name="x3" bitsize="32"/>
2227 <reg name="ps" bitsize="32" regnum="3"/>
2229 but it also has false negatives. The target description
2232 <reg name="x1" bitsize="32" regnum="1"/>
2233 <reg name="x3" bitsize="32" regnum="3"/>
2234 <reg name="x2" bitsize="32" regnum="2"/>
2235 <reg name="x4" bitsize="32" regnum="4"/>
2237 but it is not a good practice, so still error on this,
2238 and also print the message so that it can be saved in the
2239 generated c file. */
2241 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2242 reg->target_regnum);
2243 printf_unfiltered ("is not the largest number (%d).\n",
2245 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2246 reg->target_regnum, m_next_regnum);
2249 if (reg->target_regnum > m_next_regnum)
2251 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2252 m_next_regnum = reg->target_regnum;
2255 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2256 reg->name, reg->save_restore);
2258 printf_unfiltered ("\"%s\", ", reg->group);
2260 printf_unfiltered ("NULL, ");
2261 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2267 /* The register number to use for the next register we see. */
2268 int m_next_regnum = 0;
2272 maint_print_c_tdesc_cmd (char *args, int from_tty)
2274 const struct target_desc *tdesc;
2275 const char *filename;
2279 /* Use the global target-supplied description, not the current
2280 architecture's. This lets a GDB for one architecture generate C
2281 for another architecture's description, even though the gdbarch
2282 initialization code will reject the new description. */
2283 tdesc = current_target_desc;
2284 filename = target_description_filename;
2288 /* Use the target description from the XML file. */
2290 tdesc = file_read_description_xml (filename);
2294 error (_("There is no target description to print."));
2296 if (filename == NULL)
2297 error (_("The current target description did not come from an XML file."));
2299 std::string filename_after_features (filename);
2300 auto loc = filename_after_features.rfind ("/features/");
2302 if (loc != std::string::npos)
2303 filename_after_features = filename_after_features.substr (loc + 10);
2305 /* Print c files for target features instead of target descriptions,
2306 because c files got from target features are more flexible than the
2308 if (startswith (filename_after_features.c_str (), "i386/32bit-"))
2310 print_c_feature v (filename_after_features);
2316 print_c_tdesc v (filename_after_features);
2322 namespace selftests {
2324 static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2328 /* See target-descritpions.h. */
2331 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2333 xml_tdesc.emplace_back (xml_file, tdesc);
2339 /* Check that the target descriptions created dynamically by
2340 architecture-specific code equal the descriptions created from XML files
2341 found in the specified directory DIR. */
2344 maintenance_check_xml_descriptions (char *dir, int from_tty)
2347 error (_("Missing dir name"));
2349 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2350 std::string feature_dir (dir1.get ());
2351 unsigned int failed = 0;
2353 for (auto const &e : selftests::xml_tdesc)
2355 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2356 const target_desc *tdesc
2357 = file_read_description_xml (tdesc_xml.data ());
2359 if (tdesc == NULL || *tdesc != *e.second)
2362 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2363 (long) selftests::xml_tdesc.size (), failed);
2366 /* Provide a prototype to silence -Wmissing-prototypes. */
2367 extern initialize_file_ftype _initialize_target_descriptions;
2370 _initialize_target_descriptions (void)
2372 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2374 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2375 Set target description specific variables."),
2376 &tdesc_set_cmdlist, "set tdesc ",
2377 0 /* allow-unknown */, &setlist);
2378 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2379 Show target description specific variables."),
2380 &tdesc_show_cmdlist, "show tdesc ",
2381 0 /* allow-unknown */, &showlist);
2382 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2383 Unset target description specific variables."),
2384 &tdesc_unset_cmdlist, "unset tdesc ",
2385 0 /* allow-unknown */, &unsetlist);
2387 add_setshow_filename_cmd ("filename", class_obscure,
2388 &tdesc_filename_cmd_string,
2390 Set the file to read for an XML target description"), _("\
2391 Show the file to read for an XML target description"), _("\
2392 When set, GDB will read the target description from a local\n\
2393 file instead of querying the remote target."),
2394 set_tdesc_filename_cmd,
2395 show_tdesc_filename_cmd,
2396 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2398 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2399 Unset the file to read for an XML target description. When unset,\n\
2400 GDB will read the description from the target."),
2401 &tdesc_unset_cmdlist);
2403 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2404 Print the current target description as a C source file."),
2405 &maintenanceprintlist);
2407 cmd_list_element *cmd;
2409 cmd = add_cmd ("xml-descriptions", class_maintenance,
2410 maintenance_check_xml_descriptions, _("\
2411 Check the target descriptions created in GDB equal the descriptions\n\
2412 created from XML files in the directory.\n\
2413 The parameter is the directory name."),
2414 &maintenancechecklist);
2415 set_cmd_completer (cmd, filename_completer);