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 /* See arch/tdesc.h. */
852 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
855 struct tdesc_type *type;
857 /* First try target-defined types. */
858 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
859 if (strcmp (type->name, id) == 0)
862 /* Next try the predefined types. */
863 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
864 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
865 return &tdesc_predefined_types[ix];
870 /* Lookup type associated with ID. */
873 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
875 struct tdesc_arch_reg *reg;
876 struct tdesc_arch_data *data;
879 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
880 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
881 for (i = 0; i < num_regs; i++)
883 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
885 && reg->reg->tdesc_type
887 && strcmp (id, reg->reg->tdesc_type->name) == 0)
894 /* Construct, if necessary, and return the GDB type implementing target
895 type TDESC_TYPE for architecture GDBARCH. */
898 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
902 switch (tdesc_type->kind)
904 /* Predefined types. */
905 case TDESC_TYPE_BOOL:
906 return builtin_type (gdbarch)->builtin_bool;
908 case TDESC_TYPE_INT8:
909 return builtin_type (gdbarch)->builtin_int8;
911 case TDESC_TYPE_INT16:
912 return builtin_type (gdbarch)->builtin_int16;
914 case TDESC_TYPE_INT32:
915 return builtin_type (gdbarch)->builtin_int32;
917 case TDESC_TYPE_INT64:
918 return builtin_type (gdbarch)->builtin_int64;
920 case TDESC_TYPE_INT128:
921 return builtin_type (gdbarch)->builtin_int128;
923 case TDESC_TYPE_UINT8:
924 return builtin_type (gdbarch)->builtin_uint8;
926 case TDESC_TYPE_UINT16:
927 return builtin_type (gdbarch)->builtin_uint16;
929 case TDESC_TYPE_UINT32:
930 return builtin_type (gdbarch)->builtin_uint32;
932 case TDESC_TYPE_UINT64:
933 return builtin_type (gdbarch)->builtin_uint64;
935 case TDESC_TYPE_UINT128:
936 return builtin_type (gdbarch)->builtin_uint128;
938 case TDESC_TYPE_CODE_PTR:
939 return builtin_type (gdbarch)->builtin_func_ptr;
941 case TDESC_TYPE_DATA_PTR:
942 return builtin_type (gdbarch)->builtin_data_ptr;
948 type = tdesc_find_type (gdbarch, tdesc_type->name);
952 switch (tdesc_type->kind)
954 case TDESC_TYPE_IEEE_SINGLE:
955 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
956 floatformats_ieee_single);
958 case TDESC_TYPE_IEEE_DOUBLE:
959 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
960 floatformats_ieee_double);
962 case TDESC_TYPE_ARM_FPA_EXT:
963 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
964 floatformats_arm_ext);
966 case TDESC_TYPE_I387_EXT:
967 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
968 floatformats_i387_ext);
970 /* Types defined by a target feature. */
971 case TDESC_TYPE_VECTOR:
973 struct type *type, *field_type;
975 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
976 type = init_vector_type (field_type, tdesc_type->u.v.count);
977 TYPE_NAME (type) = xstrdup (tdesc_type->name);
982 case TDESC_TYPE_STRUCT:
984 struct type *type, *field_type;
985 struct tdesc_type_field *f;
988 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
989 TYPE_NAME (type) = xstrdup (tdesc_type->name);
990 TYPE_TAG_NAME (type) = TYPE_NAME (type);
993 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
996 if (f->start != -1 && f->end != -1)
1000 struct type *field_type;
1001 int bitsize, total_size;
1003 /* This invariant should be preserved while creating types. */
1004 gdb_assert (tdesc_type->u.u.size != 0);
1005 if (f->type != NULL)
1006 field_type = tdesc_gdb_type (gdbarch, f->type);
1007 else if (tdesc_type->u.u.size > 4)
1008 field_type = builtin_type (gdbarch)->builtin_uint64;
1010 field_type = builtin_type (gdbarch)->builtin_uint32;
1012 fld = append_composite_type_field_raw (type, xstrdup (f->name),
1015 /* For little-endian, BITPOS counts from the LSB of
1016 the structure and marks the LSB of the field. For
1017 big-endian, BITPOS counts from the MSB of the
1018 structure and marks the MSB of the field. Either
1019 way, it is the number of bits to the "left" of the
1020 field. To calculate this in big-endian, we need
1021 the total size of the structure. */
1022 bitsize = f->end - f->start + 1;
1023 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
1024 if (gdbarch_bits_big_endian (gdbarch))
1025 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
1027 SET_FIELD_BITPOS (fld[0], f->start);
1028 FIELD_BITSIZE (fld[0]) = bitsize;
1032 gdb_assert (f->start == -1 && f->end == -1);
1033 field_type = tdesc_gdb_type (gdbarch, f->type);
1034 append_composite_type_field (type, xstrdup (f->name),
1039 if (tdesc_type->u.u.size != 0)
1040 TYPE_LENGTH (type) = tdesc_type->u.u.size;
1044 case TDESC_TYPE_UNION:
1046 struct type *type, *field_type;
1047 struct tdesc_type_field *f;
1050 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1051 TYPE_NAME (type) = xstrdup (tdesc_type->name);
1054 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1057 field_type = tdesc_gdb_type (gdbarch, f->type);
1058 append_composite_type_field (type, xstrdup (f->name), field_type);
1060 /* If any of the children of a union are vectors, flag the
1061 union as a vector also. This allows e.g. a union of two
1062 vector types to show up automatically in "info vector". */
1063 if (TYPE_VECTOR (field_type))
1064 TYPE_VECTOR (type) = 1;
1069 case TDESC_TYPE_FLAGS:
1071 struct tdesc_type_field *f;
1074 type = arch_flags_type (gdbarch, tdesc_type->name,
1075 tdesc_type->u.u.size);
1077 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1080 struct type *field_type;
1081 int bitsize = f->end - f->start + 1;
1083 gdb_assert (f->type != NULL);
1084 field_type = tdesc_gdb_type (gdbarch, f->type);
1085 append_flags_type_field (type, f->start, bitsize,
1086 field_type, f->name);
1092 case TDESC_TYPE_ENUM:
1094 struct tdesc_type_field *f;
1097 type = arch_type (gdbarch, TYPE_CODE_ENUM,
1098 tdesc_type->u.u.size, tdesc_type->name);
1099 TYPE_UNSIGNED (type) = 1;
1101 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1105 = append_composite_type_field_raw (type, xstrdup (f->name),
1108 SET_FIELD_BITPOS (fld[0], f->start);
1115 internal_error (__FILE__, __LINE__,
1116 "Type \"%s\" has an unknown kind %d",
1117 tdesc_type->name, tdesc_type->kind);
1121 /* Support for registers from target descriptions. */
1123 /* Construct the per-gdbarch data. */
1126 tdesc_data_init (struct obstack *obstack)
1128 struct tdesc_arch_data *data;
1130 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1134 /* Similar, but for the temporary copy used during architecture
1137 struct tdesc_arch_data *
1138 tdesc_data_alloc (void)
1140 return XCNEW (struct tdesc_arch_data);
1143 /* Free something allocated by tdesc_data_alloc, if it is not going
1144 to be used (for instance if it was unsuitable for the
1148 tdesc_data_cleanup (void *data_untyped)
1150 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1152 VEC_free (tdesc_arch_reg, data->arch_regs);
1156 /* Search FEATURE for a register named NAME. */
1158 static struct tdesc_reg *
1159 tdesc_find_register_early (const struct tdesc_feature *feature,
1163 struct tdesc_reg *reg;
1166 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1168 if (strcasecmp (reg->name, name) == 0)
1174 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1177 tdesc_numbered_register (const struct tdesc_feature *feature,
1178 struct tdesc_arch_data *data,
1179 int regno, const char *name)
1181 struct tdesc_arch_reg arch_reg = { 0 };
1182 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1187 /* Make sure the vector includes a REGNO'th element. */
1188 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1189 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1192 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
1196 /* Search FEATURE for a register named NAME, but do not assign a fixed
1197 register number to it. */
1200 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1203 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1211 /* Search FEATURE for a register whose name is in NAMES and assign
1215 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1216 struct tdesc_arch_data *data,
1217 int regno, const char *const names[])
1221 for (i = 0; names[i] != NULL; i++)
1222 if (tdesc_numbered_register (feature, data, regno, names[i]))
1228 /* Search FEATURE for a register named NAME, and return its size in
1229 bits. The register must exist. */
1232 tdesc_register_size (const struct tdesc_feature *feature,
1235 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1237 gdb_assert (reg != NULL);
1238 return reg->bitsize;
1241 /* Look up a register by its GDB internal register number. */
1243 static struct tdesc_arch_reg *
1244 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1246 struct tdesc_arch_data *data;
1248 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1249 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1250 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
1255 static struct tdesc_reg *
1256 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1258 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1260 return reg? reg->reg : NULL;
1263 /* Return the name of register REGNO, from the target description or
1264 from an architecture-provided pseudo_register_name method. */
1267 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1269 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1270 int num_regs = gdbarch_num_regs (gdbarch);
1271 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1276 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1278 struct tdesc_arch_data *data
1279 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1281 gdb_assert (data->pseudo_register_name != NULL);
1282 return data->pseudo_register_name (gdbarch, regno);
1289 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1291 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1292 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1293 int num_regs = gdbarch_num_regs (gdbarch);
1294 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1296 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1298 struct tdesc_arch_data *data
1299 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1301 gdb_assert (data->pseudo_register_type != NULL);
1302 return data->pseudo_register_type (gdbarch, regno);
1306 /* Return "int0_t", since "void" has a misleading size of one. */
1307 return builtin_type (gdbarch)->builtin_int0;
1309 if (arch_reg->type == NULL)
1311 /* First check for a predefined or target defined type. */
1312 if (reg->tdesc_type)
1313 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1315 /* Next try size-sensitive type shortcuts. */
1316 else if (strcmp (reg->type, "float") == 0)
1318 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1319 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1320 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1321 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1322 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1323 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1326 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1327 reg->name, reg->bitsize);
1328 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1331 else if (strcmp (reg->type, "int") == 0)
1333 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1334 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1335 else if (reg->bitsize == TARGET_CHAR_BIT)
1336 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1337 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1338 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1339 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1340 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1341 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1342 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1343 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1344 /* A bit desperate by this point... */
1345 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1348 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1349 reg->name, reg->bitsize);
1350 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1354 if (arch_reg->type == NULL)
1355 internal_error (__FILE__, __LINE__,
1356 "Register \"%s\" has an unknown type \"%s\"",
1357 reg->name, reg->type);
1360 return arch_reg->type;
1364 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1366 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1369 return reg->target_regnum;
1374 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1375 target description may be classified as general, float, or vector.
1376 Unlike a gdbarch register_reggroup_p method, this function will
1377 return -1 if it does not know; the caller should handle registers
1378 with no specified group.
1380 Arbitrary strings (other than "general", "float", and "vector")
1381 from the description are not used; they cause the register to be
1382 displayed in "info all-registers" but excluded from "info
1383 registers" et al. The names of containing features are also not
1384 used. This might be extended to display registers in some more
1387 The save-restore flag is also implemented here. */
1390 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1391 struct reggroup *reggroup)
1393 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1395 if (reg != NULL && reg->group != NULL)
1397 int general_p = 0, float_p = 0, vector_p = 0;
1399 if (strcmp (reg->group, "general") == 0)
1401 else if (strcmp (reg->group, "float") == 0)
1403 else if (strcmp (reg->group, "vector") == 0)
1406 if (reggroup == float_reggroup)
1409 if (reggroup == vector_reggroup)
1412 if (reggroup == general_reggroup)
1417 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1418 return reg->save_restore;
1423 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1424 group specified go to the default reggroup function and are handled
1428 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1429 struct reggroup *reggroup)
1431 int num_regs = gdbarch_num_regs (gdbarch);
1432 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1435 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1437 struct tdesc_arch_data *data
1438 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1440 if (data->pseudo_register_reggroup_p != NULL)
1441 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1442 /* Otherwise fall through to the default reggroup_p. */
1445 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1449 return default_register_reggroup_p (gdbarch, regno, reggroup);
1452 /* Record architecture-specific functions to call for pseudo-register
1456 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1457 gdbarch_register_name_ftype *pseudo_name)
1459 struct tdesc_arch_data *data
1460 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1462 data->pseudo_register_name = pseudo_name;
1466 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1467 gdbarch_register_type_ftype *pseudo_type)
1469 struct tdesc_arch_data *data
1470 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1472 data->pseudo_register_type = pseudo_type;
1476 set_tdesc_pseudo_register_reggroup_p
1477 (struct gdbarch *gdbarch,
1478 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1480 struct tdesc_arch_data *data
1481 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1483 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1486 /* Update GDBARCH to use the target description for registers. */
1489 tdesc_use_registers (struct gdbarch *gdbarch,
1490 const struct target_desc *target_desc,
1491 struct tdesc_arch_data *early_data)
1493 int num_regs = gdbarch_num_regs (gdbarch);
1495 struct tdesc_feature *feature;
1496 struct tdesc_reg *reg;
1497 struct tdesc_arch_data *data;
1498 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1501 /* We can't use the description for registers if it doesn't describe
1502 any. This function should only be called after validating
1503 registers, so the caller should know that registers are
1505 gdb_assert (tdesc_has_registers (target_desc));
1507 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1508 data->arch_regs = early_data->arch_regs;
1511 /* Build up a set of all registers, so that we can assign register
1512 numbers where needed. The hash table expands as necessary, so
1513 the initial size is arbitrary. */
1514 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1516 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1519 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1522 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1527 /* Remove any registers which were assigned numbers by the
1530 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1533 htab_remove_elt (reg_hash, arch_reg->reg);
1535 /* Assign numbers to the remaining registers and add them to the
1536 list of registers. The new numbers are always above gdbarch_num_regs.
1537 Iterate over the features, not the hash table, so that the order
1538 matches that in the target description. */
1540 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1541 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1542 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1544 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1547 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1549 if (htab_find (reg_hash, reg) != NULL)
1551 new_arch_reg.reg = reg;
1552 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1556 htab_delete (reg_hash);
1558 /* Update the architecture. */
1559 set_gdbarch_num_regs (gdbarch, num_regs);
1560 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1561 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1562 set_gdbarch_remote_register_number (gdbarch,
1563 tdesc_remote_register_number);
1564 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1568 /* See arch/tdesc.h. */
1571 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1572 int regnum, int save_restore, const char *group,
1573 int bitsize, const char *type)
1575 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1576 group, bitsize, type);
1578 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1581 /* See arch/tdesc.h. */
1584 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1585 struct tdesc_type *field_type, int count)
1587 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1589 type->u.v.type = field_type;
1590 type->u.v.count = count;
1592 VEC_safe_push (tdesc_type_p, feature->types, type);
1596 /* See arch/tdesc.h. */
1599 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1601 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1603 VEC_safe_push (tdesc_type_p, feature->types, type);
1607 /* See arch/tdesc.h. */
1610 tdesc_set_struct_size (struct tdesc_type *type, int size)
1612 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1613 gdb_assert (size > 0);
1614 type->u.u.size = size;
1617 /* See arch/tdesc.h. */
1620 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1622 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1624 VEC_safe_push (tdesc_type_p, feature->types, type);
1628 /* See arch/tdesc.h. */
1631 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1634 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1636 gdb_assert (size > 0);
1638 type->u.u.size = size;
1640 VEC_safe_push (tdesc_type_p, feature->types, type);
1645 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1648 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1650 gdb_assert (size > 0);
1652 type->u.u.size = size;
1654 VEC_safe_push (tdesc_type_p, feature->types, type);
1658 /* See arch/tdesc.h. */
1661 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1662 struct tdesc_type *field_type)
1664 struct tdesc_type_field f = { 0 };
1666 gdb_assert (type->kind == TDESC_TYPE_UNION
1667 || type->kind == TDESC_TYPE_STRUCT);
1669 f.name = xstrdup (field_name);
1670 f.type = field_type;
1671 /* Initialize these values so we know this is not a bit-field
1672 when we print-c-tdesc. */
1676 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1680 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1681 int start, int end, struct tdesc_type *field_type)
1683 struct tdesc_type_field f = { 0 };
1685 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1686 || type->kind == TDESC_TYPE_FLAGS);
1687 gdb_assert (start >= 0 && end >= start);
1689 f.name = xstrdup (field_name);
1692 f.type = field_type;
1694 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1697 /* See arch/tdesc.h. */
1700 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1703 struct tdesc_type *field_type;
1705 gdb_assert (start >= 0 && end >= start);
1707 if (type->u.u.size > 4)
1708 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1710 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1712 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1715 /* See arch/tdesc.h. */
1718 tdesc_add_flag (struct tdesc_type *type, int start,
1719 const char *flag_name)
1721 struct tdesc_type_field f = { 0 };
1723 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1724 || type->kind == TDESC_TYPE_STRUCT);
1726 f.name = xstrdup (flag_name);
1729 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
1731 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1735 tdesc_add_enum_value (struct tdesc_type *type, int value,
1738 struct tdesc_type_field f = { 0 };
1740 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1742 f.name = xstrdup (name);
1745 f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1747 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1750 /* See arch/tdesc.h. */
1752 struct tdesc_feature *
1753 tdesc_create_feature (struct target_desc *tdesc, const char *name,
1756 struct tdesc_feature *new_feature = new tdesc_feature (name);
1758 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1762 struct target_desc *
1763 allocate_target_description (void)
1765 return new target_desc ();
1769 free_target_description (void *arg)
1771 struct target_desc *target_desc = (struct target_desc *) arg;
1777 make_cleanup_free_target_description (struct target_desc *target_desc)
1779 return make_cleanup (free_target_description, target_desc);
1783 tdesc_add_compatible (struct target_desc *target_desc,
1784 const struct bfd_arch_info *compatible)
1786 const struct bfd_arch_info *compat;
1789 /* If this instance of GDB is compiled without BFD support for the
1790 compatible architecture, simply ignore it -- we would not be able
1791 to handle it anyway. */
1792 if (compatible == NULL)
1795 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1797 if (compat == compatible)
1798 internal_error (__FILE__, __LINE__,
1799 _("Attempted to add duplicate "
1800 "compatible architecture \"%s\""),
1801 compatible->printable_name);
1803 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1807 set_tdesc_property (struct target_desc *target_desc,
1808 const char *key, const char *value)
1810 struct property *prop, new_prop;
1813 gdb_assert (key != NULL && value != NULL);
1815 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1817 if (strcmp (prop->key, key) == 0)
1818 internal_error (__FILE__, __LINE__,
1819 _("Attempted to add duplicate property \"%s\""), key);
1821 new_prop.key = xstrdup (key);
1822 new_prop.value = xstrdup (value);
1823 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1827 set_tdesc_architecture (struct target_desc *target_desc,
1828 const struct bfd_arch_info *arch)
1830 target_desc->arch = arch;
1834 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1836 target_desc->osabi = osabi;
1840 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1841 static struct cmd_list_element *tdesc_unset_cmdlist;
1843 /* Helper functions for the CLI commands. */
1846 set_tdesc_cmd (char *args, int from_tty)
1848 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1852 show_tdesc_cmd (char *args, int from_tty)
1854 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1858 unset_tdesc_cmd (char *args, int from_tty)
1860 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1864 set_tdesc_filename_cmd (char *args, int from_tty,
1865 struct cmd_list_element *c)
1867 xfree (target_description_filename);
1868 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1870 target_clear_description ();
1871 target_find_description ();
1875 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1876 struct cmd_list_element *c,
1879 value = target_description_filename;
1881 if (value != NULL && *value != '\0')
1882 printf_filtered (_("The target description will be read from \"%s\".\n"),
1885 printf_filtered (_("The target description will be "
1886 "read from the target.\n"));
1890 unset_tdesc_filename_cmd (char *args, int from_tty)
1892 xfree (target_description_filename);
1893 target_description_filename = NULL;
1894 target_clear_description ();
1895 target_find_description ();
1898 /* Print target description in C. */
1900 class print_c_tdesc : public tdesc_element_visitor
1903 print_c_tdesc (std::string &filename_after_features)
1904 : m_filename_after_features (filename_after_features)
1908 const char *filename = lbasename (m_filename_after_features.c_str ());
1910 m_function = (char *) xmalloc (strlen (filename) + 1);
1911 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1914 else if (*inp == '-')
1920 /* Standard boilerplate. */
1921 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1922 "-*- buffer-read-only: t -*- vi"
1931 void visit_pre (const target_desc *e) override
1933 printf_unfiltered (" Original: %s */\n\n",
1934 lbasename (m_filename_after_features.c_str ()));
1936 printf_unfiltered ("#include \"defs.h\"\n");
1937 printf_unfiltered ("#include \"osabi.h\"\n");
1938 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1939 printf_unfiltered ("\n");
1941 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1942 printf_unfiltered ("static void\n");
1943 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1944 printf_unfiltered ("{\n");
1946 (" struct target_desc *result = allocate_target_description ();\n");
1948 if (tdesc_architecture (e) != NULL)
1951 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1952 tdesc_architecture (e)->printable_name);
1953 printf_unfiltered ("\n");
1955 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1956 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1959 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1960 gdbarch_osabi_name (tdesc_osabi (e)));
1961 printf_unfiltered ("\n");
1965 const struct bfd_arch_info *compatible;
1966 struct property *prop;
1968 for (ix = 0; VEC_iterate (arch_p, e->compatible, ix, compatible);
1972 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1973 compatible->printable_name);
1977 printf_unfiltered ("\n");
1979 for (ix = 0; VEC_iterate (property_s, e->properties, ix, prop);
1982 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1983 prop->key, prop->value);
1985 printf_unfiltered (" struct tdesc_feature *feature;\n");
1988 void visit_pre (const tdesc_feature *e) override
1990 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1994 void visit_post (const tdesc_feature *e) override
1997 void visit_post (const target_desc *e) override
1999 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
2000 printf_unfiltered ("}\n");
2003 void visit (const tdesc_type *type) override
2005 struct tdesc_type_field *f;
2007 /* Now we do some "filtering" in order to know which variables to
2008 declare. This is needed because otherwise we would declare unused
2009 variables `field_type' and `type'. */
2010 if (!m_printed_field_type)
2012 printf_unfiltered (" struct tdesc_type *field_type;\n");
2013 m_printed_field_type = true;
2016 if ((type->kind == TDESC_TYPE_UNION
2017 || type->kind == TDESC_TYPE_STRUCT
2018 || type->kind == TDESC_TYPE_FLAGS
2019 || type->kind == TDESC_TYPE_ENUM)
2020 && VEC_length (tdesc_type_field, type->u.u.fields) > 0
2023 printf_unfiltered (" struct tdesc_type *type;\n");
2024 m_printed_type = true;
2029 case TDESC_TYPE_VECTOR:
2031 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2032 type->u.v.type->name);
2034 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
2035 type->name, type->u.v.count);
2037 case TDESC_TYPE_STRUCT:
2038 case TDESC_TYPE_FLAGS:
2039 if (type->kind == TDESC_TYPE_STRUCT)
2042 (" type = tdesc_create_struct (feature, \"%s\");\n",
2044 if (type->u.u.size != 0)
2046 (" tdesc_set_struct_size (type, %d);\n",
2052 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
2053 type->name, type->u.u.size);
2056 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2059 const char *type_name;
2061 gdb_assert (f->type != NULL);
2062 type_name = f->type->name;
2064 /* To minimize changes to generated files, don't emit type
2065 info for fields that have defaulted types. */
2068 gdb_assert (f->end != -1);
2069 if (f->type->kind == TDESC_TYPE_BOOL)
2071 gdb_assert (f->start == f->end);
2073 (" tdesc_add_flag (type, %d, \"%s\");\n",
2076 else if ((type->u.u.size == 4
2077 && f->type->kind == TDESC_TYPE_UINT32)
2078 || (type->u.u.size == 8
2079 && f->type->kind == TDESC_TYPE_UINT64))
2082 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
2083 f->name, f->start, f->end);
2088 (" field_type = tdesc_named_type (feature,"
2092 (" tdesc_add_typed_bitfield (type, \"%s\","
2093 " %d, %d, field_type);\n",
2094 f->name, f->start, f->end);
2097 else /* Not a bitfield. */
2099 gdb_assert (f->end == -1);
2100 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
2102 (" field_type = tdesc_named_type (feature,"
2106 (" tdesc_add_field (type, \"%s\", field_type);\n",
2111 case TDESC_TYPE_UNION:
2113 (" type = tdesc_create_union (feature, \"%s\");\n",
2116 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2120 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2123 (" tdesc_add_field (type, \"%s\", field_type);\n",
2127 case TDESC_TYPE_ENUM:
2129 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2130 type->name, type->u.u.size);
2132 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2135 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2139 error (_("C output is not supported type \"%s\"."), type->name);
2141 printf_unfiltered ("\n");
2144 void visit (const tdesc_reg *reg) override
2146 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2147 reg->name, reg->target_regnum, reg->save_restore);
2149 printf_unfiltered ("\"%s\", ", reg->group);
2151 printf_unfiltered ("NULL, ");
2152 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2156 std::string m_filename_after_features;
2160 bool m_printed_field_type = false;
2161 bool m_printed_type = false;
2164 /* Print target description feature in C. */
2166 class print_c_feature : public print_c_tdesc
2169 print_c_feature (std::string &file)
2170 : print_c_tdesc (file)
2173 auto const pos = m_filename_after_features.find_last_of ('.');
2175 m_filename_after_features = m_filename_after_features.substr (0, pos);
2178 void visit_pre (const target_desc *e) override
2180 printf_unfiltered (" Original: %s */\n\n",
2181 lbasename (m_filename_after_features.c_str ()));
2183 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2184 printf_unfiltered ("\n");
2187 void visit_post (const target_desc *e) override
2190 void visit_pre (const tdesc_feature *e) override
2192 std::string name (m_filename_after_features);
2194 auto pos = name.find_first_of ('.');
2196 name = name.substr (0, pos);
2197 std::replace (name.begin (), name.end (), '/', '_');
2198 std::replace (name.begin (), name.end (), '-', '_');
2200 printf_unfiltered ("static int\n");
2201 printf_unfiltered ("create_feature_%s ", name.c_str ());
2202 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2204 printf_unfiltered ("{\n");
2205 printf_unfiltered (" struct tdesc_feature *feature;\n");
2208 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2209 e->name, lbasename (m_filename_after_features.c_str ()));
2212 void visit_post (const tdesc_feature *e) override
2214 printf_unfiltered (" return regnum;\n");
2215 printf_unfiltered ("}\n");
2218 void visit (const tdesc_reg *reg) override
2220 /* Most "reg" in XML target descriptions don't have "regnum"
2221 attribute, so the register number is allocated sequentially.
2222 In case that reg has "regnum" attribute, register number
2223 should be set by that explicitly. */
2225 if (reg->target_regnum < m_next_regnum)
2227 /* The integrity check, it can catch some errors on register
2228 number collision, like this,
2230 <reg name="x0" bitsize="32"/>
2231 <reg name="x1" bitsize="32"/>
2232 <reg name="x2" bitsize="32"/>
2233 <reg name="x3" bitsize="32"/>
2234 <reg name="ps" bitsize="32" regnum="3"/>
2236 but it also has false negatives. The target description
2239 <reg name="x1" bitsize="32" regnum="1"/>
2240 <reg name="x3" bitsize="32" regnum="3"/>
2241 <reg name="x2" bitsize="32" regnum="2"/>
2242 <reg name="x4" bitsize="32" regnum="4"/>
2244 but it is not a good practice, so still error on this,
2245 and also print the message so that it can be saved in the
2246 generated c file. */
2248 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2249 reg->target_regnum);
2250 printf_unfiltered ("is not the largest number (%d).\n",
2252 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2253 reg->target_regnum, m_next_regnum);
2256 if (reg->target_regnum > m_next_regnum)
2258 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2259 m_next_regnum = reg->target_regnum;
2262 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2263 reg->name, reg->save_restore);
2265 printf_unfiltered ("\"%s\", ", reg->group);
2267 printf_unfiltered ("NULL, ");
2268 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2274 /* The register number to use for the next register we see. */
2275 int m_next_regnum = 0;
2279 maint_print_c_tdesc_cmd (char *args, int from_tty)
2281 const struct target_desc *tdesc;
2282 const char *filename;
2286 /* Use the global target-supplied description, not the current
2287 architecture's. This lets a GDB for one architecture generate C
2288 for another architecture's description, even though the gdbarch
2289 initialization code will reject the new description. */
2290 tdesc = current_target_desc;
2291 filename = target_description_filename;
2295 /* Use the target description from the XML file. */
2297 tdesc = file_read_description_xml (filename);
2301 error (_("There is no target description to print."));
2303 if (filename == NULL)
2304 error (_("The current target description did not come from an XML file."));
2306 std::string filename_after_features (filename);
2307 auto loc = filename_after_features.rfind ("/features/");
2309 if (loc != std::string::npos)
2310 filename_after_features = filename_after_features.substr (loc + 10);
2312 /* Print c files for target features instead of target descriptions,
2313 because c files got from target features are more flexible than the
2315 if (startswith (filename_after_features.c_str (), "i386/32bit-"))
2317 print_c_feature v (filename_after_features);
2323 print_c_tdesc v (filename_after_features);
2329 namespace selftests {
2331 static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2335 /* See target-descritpions.h. */
2338 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2340 xml_tdesc.emplace_back (xml_file, tdesc);
2346 /* Check that the target descriptions created dynamically by
2347 architecture-specific code equal the descriptions created from XML files
2348 found in the specified directory DIR. */
2351 maintenance_check_xml_descriptions (char *dir, int from_tty)
2354 error (_("Missing dir name"));
2356 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2357 std::string feature_dir (dir1.get ());
2358 unsigned int failed = 0;
2360 for (auto const &e : selftests::xml_tdesc)
2362 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2363 const target_desc *tdesc
2364 = file_read_description_xml (tdesc_xml.data ());
2366 if (tdesc == NULL || *tdesc != *e.second)
2369 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2370 (long) selftests::xml_tdesc.size (), failed);
2373 /* Provide a prototype to silence -Wmissing-prototypes. */
2374 extern initialize_file_ftype _initialize_target_descriptions;
2377 _initialize_target_descriptions (void)
2379 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2381 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2382 Set target description specific variables."),
2383 &tdesc_set_cmdlist, "set tdesc ",
2384 0 /* allow-unknown */, &setlist);
2385 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2386 Show target description specific variables."),
2387 &tdesc_show_cmdlist, "show tdesc ",
2388 0 /* allow-unknown */, &showlist);
2389 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2390 Unset target description specific variables."),
2391 &tdesc_unset_cmdlist, "unset tdesc ",
2392 0 /* allow-unknown */, &unsetlist);
2394 add_setshow_filename_cmd ("filename", class_obscure,
2395 &tdesc_filename_cmd_string,
2397 Set the file to read for an XML target description"), _("\
2398 Show the file to read for an XML target description"), _("\
2399 When set, GDB will read the target description from a local\n\
2400 file instead of querying the remote target."),
2401 set_tdesc_filename_cmd,
2402 show_tdesc_filename_cmd,
2403 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2405 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2406 Unset the file to read for an XML target description. When unset,\n\
2407 GDB will read the description from the target."),
2408 &tdesc_unset_cmdlist);
2410 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2411 Print the current target description as a C source file."),
2412 &maintenanceprintlist);
2414 cmd_list_element *cmd;
2416 cmd = add_cmd ("xml-descriptions", class_maintenance,
2417 maintenance_check_xml_descriptions, _("\
2418 Check the target descriptions created in GDB equal the descriptions\n\
2419 created from XML files in the directory.\n\
2420 The parameter is the directory name."),
2421 &maintenancechecklist);
2422 set_cmd_completer (cmd, filename_completer);