1 /* Target description support for GDB.
3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
26 #include "reggroups.h"
28 #include "target-descriptions.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
34 #include "gdb_obstack.h"
38 #include "completer.h"
39 #include "readline/tilde.h" /* tilde_expand */
41 /* The interface to visit different elements of target description. */
43 class tdesc_element_visitor
46 virtual void visit_pre (const target_desc *e) = 0;
47 virtual void visit_post (const target_desc *e) = 0;
49 virtual void visit_pre (const tdesc_feature *e) = 0;
50 virtual void visit_post (const tdesc_feature *e) = 0;
52 virtual void visit (const tdesc_type *e) = 0;
53 virtual void visit (const tdesc_reg *e) = 0;
59 virtual void accept (tdesc_element_visitor &v) const = 0;
66 property (const std::string &key_, const std::string &value_)
67 : key (key_), value (value_)
74 /* An individual register from a target description. */
76 typedef struct tdesc_reg : tdesc_element
78 tdesc_reg (struct tdesc_feature *feature, const char *name_,
79 int regnum, int save_restore_, const char *group_,
80 int bitsize_, const char *type_)
81 : name (xstrdup (name_)), target_regnum (regnum),
82 save_restore (save_restore_),
83 group (group_ != NULL ? xstrdup (group_) : NULL),
85 type (type_ != NULL ? xstrdup (type_) : xstrdup ("<unknown>"))
87 /* If the register's type is target-defined, look it up now. We may not
88 have easy access to the containing feature when we want it later. */
89 tdesc_type = tdesc_named_type (feature, type);
99 DISABLE_COPY_AND_ASSIGN (tdesc_reg);
101 /* The name of this register. In standard features, it may be
102 recognized by the architecture support code, or it may be purely
106 /* The register number used by this target to refer to this
107 register. This is used for remote p/P packets and to determine
108 the ordering of registers in the remote g/G packets. */
111 /* If this flag is set, GDB should save and restore this register
112 around calls to an inferior function. */
115 /* The name of the register group containing this register, or NULL
116 if the group should be automatically determined from the
117 register's type. If this is "general", "float", or "vector", the
118 corresponding "info" command should display this register's
119 value. It can be an arbitrary string, but should be limited to
120 alphanumeric characters and internal hyphens. Currently other
121 strings are ignored (treated as NULL). */
124 /* The size of the register, in bits. */
127 /* The type of the register. This string corresponds to either
128 a named type from the target description or a predefined
132 /* The target-described type corresponding to TYPE, if found. */
133 struct tdesc_type *tdesc_type;
135 void accept (tdesc_element_visitor &v) const override
140 bool operator== (const tdesc_reg &other) const
142 return (streq (name, other.name)
143 && target_regnum == other.target_regnum
144 && save_restore == other.save_restore
145 && bitsize == other.bitsize
146 && (group == other.group || streq (group, other.group))
147 && streq (type, other.type));
150 bool operator!= (const tdesc_reg &other) const
152 return !(*this == other);
155 DEF_VEC_P(tdesc_reg_p);
157 /* A named type from a target description. */
159 typedef struct tdesc_type_field
162 struct tdesc_type *type;
163 /* For non-enum-values, either both are -1 (non-bitfield), or both are
164 not -1 (bitfield). For enum values, start is the value (which could be
168 DEF_VEC_O(tdesc_type_field);
172 /* Predefined types. */
186 TDESC_TYPE_IEEE_SINGLE,
187 TDESC_TYPE_IEEE_DOUBLE,
188 TDESC_TYPE_ARM_FPA_EXT,
191 /* Types defined by a target feature. */
199 typedef struct tdesc_type : tdesc_element
201 tdesc_type (const char *name_, enum tdesc_type_kind kind_)
202 : name (xstrdup (name_)), kind (kind_)
204 memset (&u, 0, sizeof (u));
207 virtual ~tdesc_type ()
211 case TDESC_TYPE_STRUCT:
212 case TDESC_TYPE_UNION:
213 case TDESC_TYPE_FLAGS:
214 case TDESC_TYPE_ENUM:
216 struct tdesc_type_field *f;
220 VEC_iterate (tdesc_type_field, u.u.fields, ix, f);
224 VEC_free (tdesc_type_field, u.u.fields);
231 xfree ((char *) name);
234 DISABLE_COPY_AND_ASSIGN (tdesc_type);
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 struct tdesc_feature : tdesc_element
284 tdesc_feature (const std::string &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);
303 DISABLE_COPY_AND_ASSIGN (tdesc_feature);
305 /* The name of this feature. It may be recognized by the architecture
309 /* The registers associated with this feature. */
310 VEC(tdesc_reg_p) *registers = NULL;
312 /* The types associated with this feature. */
313 VEC(tdesc_type_p) *types = NULL;
315 void accept (tdesc_element_visitor &v) const override
319 struct tdesc_type *type;
322 VEC_iterate (tdesc_type_p, types, ix, type);
326 struct tdesc_reg *reg;
329 VEC_iterate (tdesc_reg_p, registers, ix, reg);
337 bool operator== (const tdesc_feature &other) const
339 if (name != other.name)
342 if (VEC_length (tdesc_reg_p, registers)
343 != VEC_length (tdesc_reg_p, other.registers))
346 struct tdesc_reg *reg;
349 VEC_iterate (tdesc_reg_p, registers, ix, reg);
353 = VEC_index (tdesc_reg_p, other.registers, ix);
355 if (reg != reg2 && *reg != *reg2)
359 if (VEC_length (tdesc_type_p, types)
360 != VEC_length (tdesc_type_p, other.types))
366 VEC_iterate (tdesc_type_p, types, ix, type);
370 = VEC_index (tdesc_type_p, other.types, ix);
372 if (type != type2 && *type != *type2)
379 bool operator!= (const tdesc_feature &other) const
381 return !(*this == other);
386 typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
388 /* A target description. */
390 struct target_desc : tdesc_element
395 virtual ~target_desc () = default;
397 target_desc (const target_desc &) = delete;
398 void operator= (const target_desc &) = delete;
400 /* The architecture reported by the target, if any. */
401 const struct bfd_arch_info *arch = NULL;
403 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
405 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
407 /* The list of compatible architectures reported by the target. */
408 std::vector<const bfd_arch_info *> compatible;
410 /* Any architecture-specific properties specified by the target. */
411 std::vector<property> properties;
413 /* The features associated with this target. */
414 std::vector<std::unique_ptr<tdesc_feature>> features;
416 void accept (tdesc_element_visitor &v) const override
420 for (const tdesc_feature_up &feature : features)
426 bool operator== (const target_desc &other) const
428 if (arch != other.arch)
431 if (osabi != other.osabi)
434 if (features.size () != other.features.size ())
437 for (int ix = 0; ix < features.size (); ix++)
439 const tdesc_feature_up &feature1 = features[ix];
440 const tdesc_feature_up &feature2 = other.features[ix];
442 if (feature1 != feature2 && *feature1 != *feature2)
449 bool operator!= (const target_desc &other) const
451 return !(*this == other);
455 /* Per-architecture data associated with a target description. The
456 target description may be shared by multiple architectures, but
457 this data is private to one gdbarch. */
459 typedef struct tdesc_arch_reg
461 struct tdesc_reg *reg;
464 DEF_VEC_O(tdesc_arch_reg);
466 struct tdesc_arch_data
468 /* A list of register/type pairs, indexed by GDB's internal register number.
469 During initialization of the gdbarch this list is used to store
470 registers which the architecture assigns a fixed register number.
471 Registers which are NULL in this array, or off the end, are
472 treated as zero-sized and nameless (i.e. placeholders in the
474 VEC(tdesc_arch_reg) *arch_regs;
476 /* Functions which report the register name, type, and reggroups for
478 gdbarch_register_name_ftype *pseudo_register_name;
479 gdbarch_register_type_ftype *pseudo_register_type;
480 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
483 /* Info about an inferior's target description. There's one of these
484 for each inferior. */
486 struct target_desc_info
488 /* A flag indicating that a description has already been fetched
489 from the target, so it should not be queried again. */
493 /* The description fetched from the target, or NULL if the target
494 did not supply any description. Only valid when
495 target_desc_fetched is set. Only the description initialization
496 code should access this; normally, the description should be
497 accessed through the gdbarch object. */
499 const struct target_desc *tdesc;
501 /* The filename to read a target description from, as set by "set
502 tdesc filename ..." */
507 /* Get the inferior INF's target description info, allocating one on
508 the stop if necessary. */
510 static struct target_desc_info *
511 get_tdesc_info (struct inferior *inf)
513 if (inf->tdesc_info == NULL)
514 inf->tdesc_info = XCNEW (struct target_desc_info);
515 return inf->tdesc_info;
518 /* A handle for architecture-specific data associated with the
519 target description (see struct tdesc_arch_data). */
521 static struct gdbarch_data *tdesc_data;
523 /* See target-descriptions.h. */
526 target_desc_info_from_user_p (struct target_desc_info *info)
528 return info != NULL && info->filename != NULL;
531 /* See target-descriptions.h. */
534 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
536 struct target_desc_info *src = get_tdesc_info (srcinf);
537 struct target_desc_info *dest = get_tdesc_info (destinf);
539 dest->fetched = src->fetched;
540 dest->tdesc = src->tdesc;
541 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
544 /* See target-descriptions.h. */
547 target_desc_info_free (struct target_desc_info *tdesc_info)
549 if (tdesc_info != NULL)
551 xfree (tdesc_info->filename);
556 /* Convenience helper macros. */
558 #define target_desc_fetched \
559 get_tdesc_info (current_inferior ())->fetched
560 #define current_target_desc \
561 get_tdesc_info (current_inferior ())->tdesc
562 #define target_description_filename \
563 get_tdesc_info (current_inferior ())->filename
565 /* The string manipulated by the "set tdesc filename ..." command. */
567 static char *tdesc_filename_cmd_string;
569 /* Fetch the current target's description, and switch the current
570 architecture to one which incorporates that description. */
573 target_find_description (void)
575 /* If we've already fetched a description from the target, don't do
576 it again. This allows a target to fetch the description early,
577 during its to_open or to_create_inferior, if it needs extra
578 information about the target to initialize. */
579 if (target_desc_fetched)
582 /* The current architecture should not have any target description
583 specified. It should have been cleared, e.g. when we
584 disconnected from the previous target. */
585 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
587 /* First try to fetch an XML description from the user-specified
589 current_target_desc = NULL;
590 if (target_description_filename != NULL
591 && *target_description_filename != '\0')
593 = file_read_description_xml (target_description_filename);
595 /* Next try to read the description from the current target using
597 if (current_target_desc == NULL)
598 current_target_desc = target_read_description_xml (¤t_target);
600 /* If that failed try a target-specific hook. */
601 if (current_target_desc == NULL)
602 current_target_desc = target_read_description (¤t_target);
604 /* If a non-NULL description was returned, then update the current
606 if (current_target_desc)
608 struct gdbarch_info info;
610 gdbarch_info_init (&info);
611 info.target_desc = current_target_desc;
612 if (!gdbarch_update_p (info))
613 warning (_("Architecture rejected target-supplied description"));
616 struct tdesc_arch_data *data;
618 data = ((struct tdesc_arch_data *)
619 gdbarch_data (target_gdbarch (), tdesc_data));
620 if (tdesc_has_registers (current_target_desc)
621 && data->arch_regs == NULL)
622 warning (_("Target-supplied registers are not supported "
623 "by the current architecture"));
627 /* Now that we know this description is usable, record that we
629 target_desc_fetched = 1;
632 /* Discard any description fetched from the current target, and switch
633 the current architecture to one with no target description. */
636 target_clear_description (void)
638 struct gdbarch_info info;
640 if (!target_desc_fetched)
643 target_desc_fetched = 0;
644 current_target_desc = NULL;
646 gdbarch_info_init (&info);
647 if (!gdbarch_update_p (info))
648 internal_error (__FILE__, __LINE__,
649 _("Could not remove target-supplied description"));
652 /* Return the global current target description. This should only be
653 used by gdbarch initialization code; most access should be through
654 an existing gdbarch. */
656 const struct target_desc *
657 target_current_description (void)
659 if (target_desc_fetched)
660 return current_target_desc;
665 /* Return non-zero if this target description is compatible
666 with the given BFD architecture. */
669 tdesc_compatible_p (const struct target_desc *target_desc,
670 const struct bfd_arch_info *arch)
672 for (const bfd_arch_info *compat : target_desc->compatible)
675 || arch->compatible (arch, compat)
676 || compat->compatible (compat, arch))
684 /* Direct accessors for target descriptions. */
686 /* Return the string value of a property named KEY, or NULL if the
687 property was not specified. */
690 tdesc_property (const struct target_desc *target_desc, const char *key)
692 for (const property &prop : target_desc->properties)
694 return prop.value.c_str ();
699 /* Return the BFD architecture associated with this target
700 description, or NULL if no architecture was specified. */
702 const struct bfd_arch_info *
703 tdesc_architecture (const struct target_desc *target_desc)
705 return target_desc->arch;
708 /* Return the OSABI associated with this target description, or
709 GDB_OSABI_UNKNOWN if no osabi was specified. */
712 tdesc_osabi (const struct target_desc *target_desc)
714 return target_desc->osabi;
719 /* Return 1 if this target description includes any registers. */
722 tdesc_has_registers (const struct target_desc *target_desc)
724 if (target_desc == NULL)
727 for (const tdesc_feature_up &feature : target_desc->features)
728 if (! VEC_empty (tdesc_reg_p, feature->registers))
734 /* Return the feature with the given name, if present, or NULL if
735 the named feature is not found. */
737 const struct tdesc_feature *
738 tdesc_find_feature (const struct target_desc *target_desc,
741 for (const tdesc_feature_up &feature : target_desc->features)
742 if (feature->name == name)
743 return feature.get ();
748 /* Return the name of FEATURE. */
751 tdesc_feature_name (const struct tdesc_feature *feature)
753 return feature->name.c_str ();
756 /* Predefined types. */
757 static struct tdesc_type tdesc_predefined_types[] =
759 { "bool", TDESC_TYPE_BOOL },
760 { "int8", TDESC_TYPE_INT8 },
761 { "int16", TDESC_TYPE_INT16 },
762 { "int32", TDESC_TYPE_INT32 },
763 { "int64", TDESC_TYPE_INT64 },
764 { "int128", TDESC_TYPE_INT128 },
765 { "uint8", TDESC_TYPE_UINT8 },
766 { "uint16", TDESC_TYPE_UINT16 },
767 { "uint32", TDESC_TYPE_UINT32 },
768 { "uint64", TDESC_TYPE_UINT64 },
769 { "uint128", TDESC_TYPE_UINT128 },
770 { "code_ptr", TDESC_TYPE_CODE_PTR },
771 { "data_ptr", TDESC_TYPE_DATA_PTR },
772 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
773 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
774 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
775 { "i387_ext", TDESC_TYPE_I387_EXT }
778 /* Lookup a predefined type. */
780 static struct tdesc_type *
781 tdesc_predefined_type (enum tdesc_type_kind kind)
783 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
784 if (tdesc_predefined_types[ix].kind == kind)
785 return &tdesc_predefined_types[ix];
787 gdb_assert_not_reached ("bad predefined tdesc type");
790 /* See arch/tdesc.h. */
793 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
796 struct tdesc_type *type;
798 /* First try target-defined types. */
799 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
800 if (strcmp (type->name, id) == 0)
803 /* Next try the predefined types. */
804 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
805 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
806 return &tdesc_predefined_types[ix];
811 /* Lookup type associated with ID. */
814 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
816 struct tdesc_arch_reg *reg;
817 struct tdesc_arch_data *data;
820 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
821 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
822 for (i = 0; i < num_regs; i++)
824 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
826 && reg->reg->tdesc_type
828 && strcmp (id, reg->reg->tdesc_type->name) == 0)
835 /* Construct, if necessary, and return the GDB type implementing target
836 type TDESC_TYPE for architecture GDBARCH. */
839 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
843 switch (tdesc_type->kind)
845 /* Predefined types. */
846 case TDESC_TYPE_BOOL:
847 return builtin_type (gdbarch)->builtin_bool;
849 case TDESC_TYPE_INT8:
850 return builtin_type (gdbarch)->builtin_int8;
852 case TDESC_TYPE_INT16:
853 return builtin_type (gdbarch)->builtin_int16;
855 case TDESC_TYPE_INT32:
856 return builtin_type (gdbarch)->builtin_int32;
858 case TDESC_TYPE_INT64:
859 return builtin_type (gdbarch)->builtin_int64;
861 case TDESC_TYPE_INT128:
862 return builtin_type (gdbarch)->builtin_int128;
864 case TDESC_TYPE_UINT8:
865 return builtin_type (gdbarch)->builtin_uint8;
867 case TDESC_TYPE_UINT16:
868 return builtin_type (gdbarch)->builtin_uint16;
870 case TDESC_TYPE_UINT32:
871 return builtin_type (gdbarch)->builtin_uint32;
873 case TDESC_TYPE_UINT64:
874 return builtin_type (gdbarch)->builtin_uint64;
876 case TDESC_TYPE_UINT128:
877 return builtin_type (gdbarch)->builtin_uint128;
879 case TDESC_TYPE_CODE_PTR:
880 return builtin_type (gdbarch)->builtin_func_ptr;
882 case TDESC_TYPE_DATA_PTR:
883 return builtin_type (gdbarch)->builtin_data_ptr;
889 type = tdesc_find_type (gdbarch, tdesc_type->name);
893 switch (tdesc_type->kind)
895 case TDESC_TYPE_IEEE_SINGLE:
896 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
897 floatformats_ieee_single);
899 case TDESC_TYPE_IEEE_DOUBLE:
900 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
901 floatformats_ieee_double);
903 case TDESC_TYPE_ARM_FPA_EXT:
904 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
905 floatformats_arm_ext);
907 case TDESC_TYPE_I387_EXT:
908 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
909 floatformats_i387_ext);
911 /* Types defined by a target feature. */
912 case TDESC_TYPE_VECTOR:
914 struct type *type, *field_type;
916 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
917 type = init_vector_type (field_type, tdesc_type->u.v.count);
918 TYPE_NAME (type) = xstrdup (tdesc_type->name);
923 case TDESC_TYPE_STRUCT:
925 struct type *type, *field_type;
926 struct tdesc_type_field *f;
929 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
930 TYPE_NAME (type) = xstrdup (tdesc_type->name);
931 TYPE_TAG_NAME (type) = TYPE_NAME (type);
934 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
937 if (f->start != -1 && f->end != -1)
941 struct type *field_type;
942 int bitsize, total_size;
944 /* This invariant should be preserved while creating types. */
945 gdb_assert (tdesc_type->u.u.size != 0);
947 field_type = tdesc_gdb_type (gdbarch, f->type);
948 else if (tdesc_type->u.u.size > 4)
949 field_type = builtin_type (gdbarch)->builtin_uint64;
951 field_type = builtin_type (gdbarch)->builtin_uint32;
953 fld = append_composite_type_field_raw (type, xstrdup (f->name),
956 /* For little-endian, BITPOS counts from the LSB of
957 the structure and marks the LSB of the field. For
958 big-endian, BITPOS counts from the MSB of the
959 structure and marks the MSB of the field. Either
960 way, it is the number of bits to the "left" of the
961 field. To calculate this in big-endian, we need
962 the total size of the structure. */
963 bitsize = f->end - f->start + 1;
964 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
965 if (gdbarch_bits_big_endian (gdbarch))
966 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
968 SET_FIELD_BITPOS (fld[0], f->start);
969 FIELD_BITSIZE (fld[0]) = bitsize;
973 gdb_assert (f->start == -1 && f->end == -1);
974 field_type = tdesc_gdb_type (gdbarch, f->type);
975 append_composite_type_field (type, xstrdup (f->name),
980 if (tdesc_type->u.u.size != 0)
981 TYPE_LENGTH (type) = tdesc_type->u.u.size;
985 case TDESC_TYPE_UNION:
987 struct type *type, *field_type;
988 struct tdesc_type_field *f;
991 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
992 TYPE_NAME (type) = xstrdup (tdesc_type->name);
995 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
998 field_type = tdesc_gdb_type (gdbarch, f->type);
999 append_composite_type_field (type, xstrdup (f->name), field_type);
1001 /* If any of the children of a union are vectors, flag the
1002 union as a vector also. This allows e.g. a union of two
1003 vector types to show up automatically in "info vector". */
1004 if (TYPE_VECTOR (field_type))
1005 TYPE_VECTOR (type) = 1;
1010 case TDESC_TYPE_FLAGS:
1012 struct tdesc_type_field *f;
1015 type = arch_flags_type (gdbarch, tdesc_type->name,
1016 tdesc_type->u.u.size * TARGET_CHAR_BIT);
1018 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1021 struct type *field_type;
1022 int bitsize = f->end - f->start + 1;
1024 gdb_assert (f->type != NULL);
1025 field_type = tdesc_gdb_type (gdbarch, f->type);
1026 append_flags_type_field (type, f->start, bitsize,
1027 field_type, f->name);
1033 case TDESC_TYPE_ENUM:
1035 struct tdesc_type_field *f;
1038 type = arch_type (gdbarch, TYPE_CODE_ENUM,
1039 tdesc_type->u.u.size * TARGET_CHAR_BIT,
1041 TYPE_UNSIGNED (type) = 1;
1043 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1047 = append_composite_type_field_raw (type, xstrdup (f->name),
1050 SET_FIELD_BITPOS (fld[0], f->start);
1057 internal_error (__FILE__, __LINE__,
1058 "Type \"%s\" has an unknown kind %d",
1059 tdesc_type->name, tdesc_type->kind);
1063 /* Support for registers from target descriptions. */
1065 /* Construct the per-gdbarch data. */
1068 tdesc_data_init (struct obstack *obstack)
1070 struct tdesc_arch_data *data;
1072 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1076 /* Similar, but for the temporary copy used during architecture
1079 struct tdesc_arch_data *
1080 tdesc_data_alloc (void)
1082 return XCNEW (struct tdesc_arch_data);
1085 /* Free something allocated by tdesc_data_alloc, if it is not going
1086 to be used (for instance if it was unsuitable for the
1090 tdesc_data_cleanup (void *data_untyped)
1092 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1094 VEC_free (tdesc_arch_reg, data->arch_regs);
1098 /* Search FEATURE for a register named NAME. */
1100 static struct tdesc_reg *
1101 tdesc_find_register_early (const struct tdesc_feature *feature,
1105 struct tdesc_reg *reg;
1108 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1110 if (strcasecmp (reg->name, name) == 0)
1116 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1119 tdesc_numbered_register (const struct tdesc_feature *feature,
1120 struct tdesc_arch_data *data,
1121 int regno, const char *name)
1123 struct tdesc_arch_reg arch_reg = { 0 };
1124 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1129 /* Make sure the vector includes a REGNO'th element. */
1130 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1131 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1134 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
1138 /* Search FEATURE for a register named NAME, but do not assign a fixed
1139 register number to it. */
1142 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1145 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1153 /* Search FEATURE for a register whose name is in NAMES and assign
1157 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1158 struct tdesc_arch_data *data,
1159 int regno, const char *const names[])
1163 for (i = 0; names[i] != NULL; i++)
1164 if (tdesc_numbered_register (feature, data, regno, names[i]))
1170 /* Search FEATURE for a register named NAME, and return its size in
1171 bits. The register must exist. */
1174 tdesc_register_size (const struct tdesc_feature *feature,
1177 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1179 gdb_assert (reg != NULL);
1180 return reg->bitsize;
1183 /* Look up a register by its GDB internal register number. */
1185 static struct tdesc_arch_reg *
1186 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1188 struct tdesc_arch_data *data;
1190 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1191 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1192 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
1197 static struct tdesc_reg *
1198 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1200 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1202 return reg? reg->reg : NULL;
1205 /* Return the name of register REGNO, from the target description or
1206 from an architecture-provided pseudo_register_name method. */
1209 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1211 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1212 int num_regs = gdbarch_num_regs (gdbarch);
1213 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1218 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1220 struct tdesc_arch_data *data
1221 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1223 gdb_assert (data->pseudo_register_name != NULL);
1224 return data->pseudo_register_name (gdbarch, regno);
1231 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1233 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1234 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1235 int num_regs = gdbarch_num_regs (gdbarch);
1236 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1238 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1240 struct tdesc_arch_data *data
1241 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1243 gdb_assert (data->pseudo_register_type != NULL);
1244 return data->pseudo_register_type (gdbarch, regno);
1248 /* Return "int0_t", since "void" has a misleading size of one. */
1249 return builtin_type (gdbarch)->builtin_int0;
1251 if (arch_reg->type == NULL)
1253 /* First check for a predefined or target defined type. */
1254 if (reg->tdesc_type)
1255 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1257 /* Next try size-sensitive type shortcuts. */
1258 else if (strcmp (reg->type, "float") == 0)
1260 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1261 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1262 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1263 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1264 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1265 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1268 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1269 reg->name, reg->bitsize);
1270 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1273 else if (strcmp (reg->type, "int") == 0)
1275 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1276 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1277 else if (reg->bitsize == TARGET_CHAR_BIT)
1278 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1279 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1280 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1281 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1282 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1283 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1284 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1285 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1286 /* A bit desperate by this point... */
1287 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1290 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1291 reg->name, reg->bitsize);
1292 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1296 if (arch_reg->type == NULL)
1297 internal_error (__FILE__, __LINE__,
1298 "Register \"%s\" has an unknown type \"%s\"",
1299 reg->name, reg->type);
1302 return arch_reg->type;
1306 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1308 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1311 return reg->target_regnum;
1316 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1317 target description may be classified as general, float, or vector.
1318 Unlike a gdbarch register_reggroup_p method, this function will
1319 return -1 if it does not know; the caller should handle registers
1320 with no specified group.
1322 Arbitrary strings (other than "general", "float", and "vector")
1323 from the description are not used; they cause the register to be
1324 displayed in "info all-registers" but excluded from "info
1325 registers" et al. The names of containing features are also not
1326 used. This might be extended to display registers in some more
1329 The save-restore flag is also implemented here. */
1332 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1333 struct reggroup *reggroup)
1335 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1337 if (reg != NULL && reg->group != NULL)
1339 int general_p = 0, float_p = 0, vector_p = 0;
1341 if (strcmp (reg->group, "general") == 0)
1343 else if (strcmp (reg->group, "float") == 0)
1345 else if (strcmp (reg->group, "vector") == 0)
1348 if (reggroup == float_reggroup)
1351 if (reggroup == vector_reggroup)
1354 if (reggroup == general_reggroup)
1359 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1360 return reg->save_restore;
1365 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1366 group specified go to the default reggroup function and are handled
1370 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1371 struct reggroup *reggroup)
1373 int num_regs = gdbarch_num_regs (gdbarch);
1374 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1377 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1379 struct tdesc_arch_data *data
1380 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1382 if (data->pseudo_register_reggroup_p != NULL)
1383 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1384 /* Otherwise fall through to the default reggroup_p. */
1387 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1391 return default_register_reggroup_p (gdbarch, regno, reggroup);
1394 /* Record architecture-specific functions to call for pseudo-register
1398 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1399 gdbarch_register_name_ftype *pseudo_name)
1401 struct tdesc_arch_data *data
1402 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1404 data->pseudo_register_name = pseudo_name;
1408 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1409 gdbarch_register_type_ftype *pseudo_type)
1411 struct tdesc_arch_data *data
1412 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1414 data->pseudo_register_type = pseudo_type;
1418 set_tdesc_pseudo_register_reggroup_p
1419 (struct gdbarch *gdbarch,
1420 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1422 struct tdesc_arch_data *data
1423 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1425 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1428 /* Update GDBARCH to use the target description for registers. */
1431 tdesc_use_registers (struct gdbarch *gdbarch,
1432 const struct target_desc *target_desc,
1433 struct tdesc_arch_data *early_data)
1435 int num_regs = gdbarch_num_regs (gdbarch);
1437 struct tdesc_reg *reg;
1438 struct tdesc_arch_data *data;
1439 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1442 /* We can't use the description for registers if it doesn't describe
1443 any. This function should only be called after validating
1444 registers, so the caller should know that registers are
1446 gdb_assert (tdesc_has_registers (target_desc));
1448 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1449 data->arch_regs = early_data->arch_regs;
1452 /* Build up a set of all registers, so that we can assign register
1453 numbers where needed. The hash table expands as necessary, so
1454 the initial size is arbitrary. */
1455 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1456 for (const tdesc_feature_up &feature : target_desc->features)
1458 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1461 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1466 /* Remove any registers which were assigned numbers by the
1469 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1472 htab_remove_elt (reg_hash, arch_reg->reg);
1474 /* Assign numbers to the remaining registers and add them to the
1475 list of registers. The new numbers are always above gdbarch_num_regs.
1476 Iterate over the features, not the hash table, so that the order
1477 matches that in the target description. */
1479 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1480 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1481 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1482 for (const tdesc_feature_up &feature : target_desc->features)
1484 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1486 if (htab_find (reg_hash, reg) != NULL)
1488 new_arch_reg.reg = reg;
1489 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1493 htab_delete (reg_hash);
1495 /* Update the architecture. */
1496 set_gdbarch_num_regs (gdbarch, num_regs);
1497 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1498 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1499 set_gdbarch_remote_register_number (gdbarch,
1500 tdesc_remote_register_number);
1501 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1505 /* See arch/tdesc.h. */
1508 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1509 int regnum, int save_restore, const char *group,
1510 int bitsize, const char *type)
1512 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1513 group, bitsize, type);
1515 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1518 /* See arch/tdesc.h. */
1521 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1522 struct tdesc_type *field_type, int count)
1524 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1526 type->u.v.type = field_type;
1527 type->u.v.count = count;
1529 VEC_safe_push (tdesc_type_p, feature->types, type);
1533 /* See arch/tdesc.h. */
1536 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1538 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1540 VEC_safe_push (tdesc_type_p, feature->types, type);
1544 /* See arch/tdesc.h. */
1547 tdesc_set_struct_size (struct tdesc_type *type, int size)
1549 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1550 gdb_assert (size > 0);
1551 type->u.u.size = size;
1554 /* See arch/tdesc.h. */
1557 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1559 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1561 VEC_safe_push (tdesc_type_p, feature->types, type);
1565 /* See arch/tdesc.h. */
1568 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1571 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1573 gdb_assert (size > 0);
1575 type->u.u.size = size;
1577 VEC_safe_push (tdesc_type_p, feature->types, type);
1582 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1585 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1587 gdb_assert (size > 0);
1589 type->u.u.size = size;
1591 VEC_safe_push (tdesc_type_p, feature->types, type);
1595 /* See arch/tdesc.h. */
1598 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1599 struct tdesc_type *field_type)
1601 struct tdesc_type_field f = { 0 };
1603 gdb_assert (type->kind == TDESC_TYPE_UNION
1604 || type->kind == TDESC_TYPE_STRUCT);
1606 f.name = xstrdup (field_name);
1607 f.type = field_type;
1608 /* Initialize these values so we know this is not a bit-field
1609 when we print-c-tdesc. */
1613 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1617 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1618 int start, int end, struct tdesc_type *field_type)
1620 struct tdesc_type_field f = { 0 };
1622 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1623 || type->kind == TDESC_TYPE_FLAGS);
1624 gdb_assert (start >= 0 && end >= start);
1626 f.name = xstrdup (field_name);
1629 f.type = field_type;
1631 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1634 /* See arch/tdesc.h. */
1637 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1640 struct tdesc_type *field_type;
1642 gdb_assert (start >= 0 && end >= start);
1644 if (type->u.u.size > 4)
1645 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1647 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1649 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1652 /* See arch/tdesc.h. */
1655 tdesc_add_flag (struct tdesc_type *type, int start,
1656 const char *flag_name)
1658 struct tdesc_type_field f = { 0 };
1660 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1661 || type->kind == TDESC_TYPE_STRUCT);
1663 f.name = xstrdup (flag_name);
1666 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
1668 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1672 tdesc_add_enum_value (struct tdesc_type *type, int value,
1675 struct tdesc_type_field f = { 0 };
1677 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1679 f.name = xstrdup (name);
1682 f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1684 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1687 /* See arch/tdesc.h. */
1689 struct tdesc_feature *
1690 tdesc_create_feature (struct target_desc *tdesc, const char *name,
1693 struct tdesc_feature *new_feature = new tdesc_feature (name);
1695 tdesc->features.emplace_back (new_feature);
1700 struct target_desc *
1701 allocate_target_description (void)
1703 return new target_desc ();
1707 free_target_description (void *arg)
1709 struct target_desc *target_desc = (struct target_desc *) arg;
1715 make_cleanup_free_target_description (struct target_desc *target_desc)
1717 return make_cleanup (free_target_description, target_desc);
1721 tdesc_add_compatible (struct target_desc *target_desc,
1722 const struct bfd_arch_info *compatible)
1724 /* If this instance of GDB is compiled without BFD support for the
1725 compatible architecture, simply ignore it -- we would not be able
1726 to handle it anyway. */
1727 if (compatible == NULL)
1730 for (const bfd_arch_info *compat : target_desc->compatible)
1731 if (compat == compatible)
1732 internal_error (__FILE__, __LINE__,
1733 _("Attempted to add duplicate "
1734 "compatible architecture \"%s\""),
1735 compatible->printable_name);
1737 target_desc->compatible.push_back (compatible);
1741 set_tdesc_property (struct target_desc *target_desc,
1742 const char *key, const char *value)
1744 gdb_assert (key != NULL && value != NULL);
1746 if (tdesc_property (target_desc, key) != NULL)
1747 internal_error (__FILE__, __LINE__,
1748 _("Attempted to add duplicate property \"%s\""), key);
1750 target_desc->properties.emplace_back (key, value);
1753 /* See arch/tdesc.h. */
1756 set_tdesc_architecture (struct target_desc *target_desc,
1759 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1763 set_tdesc_architecture (struct target_desc *target_desc,
1764 const struct bfd_arch_info *arch)
1766 target_desc->arch = arch;
1769 /* See arch/tdesc.h. */
1772 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1774 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1778 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1780 target_desc->osabi = osabi;
1784 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1785 static struct cmd_list_element *tdesc_unset_cmdlist;
1787 /* Helper functions for the CLI commands. */
1790 set_tdesc_cmd (const char *args, int from_tty)
1792 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1796 show_tdesc_cmd (const char *args, int from_tty)
1798 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1802 unset_tdesc_cmd (const char *args, int from_tty)
1804 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1808 set_tdesc_filename_cmd (const char *args, int from_tty,
1809 struct cmd_list_element *c)
1811 xfree (target_description_filename);
1812 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1814 target_clear_description ();
1815 target_find_description ();
1819 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1820 struct cmd_list_element *c,
1823 value = target_description_filename;
1825 if (value != NULL && *value != '\0')
1826 printf_filtered (_("The target description will be read from \"%s\".\n"),
1829 printf_filtered (_("The target description will be "
1830 "read from the target.\n"));
1834 unset_tdesc_filename_cmd (const char *args, int from_tty)
1836 xfree (target_description_filename);
1837 target_description_filename = NULL;
1838 target_clear_description ();
1839 target_find_description ();
1842 /* Print target description in C. */
1844 class print_c_tdesc : public tdesc_element_visitor
1847 print_c_tdesc (std::string &filename_after_features)
1848 : m_filename_after_features (filename_after_features)
1852 const char *filename = lbasename (m_filename_after_features.c_str ());
1854 m_function = (char *) xmalloc (strlen (filename) + 1);
1855 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1858 else if (*inp == '-')
1864 /* Standard boilerplate. */
1865 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1866 "-*- buffer-read-only: t -*- vi"
1875 void visit_pre (const target_desc *e) override
1877 printf_unfiltered (" Original: %s */\n\n",
1878 lbasename (m_filename_after_features.c_str ()));
1880 printf_unfiltered ("#include \"defs.h\"\n");
1881 printf_unfiltered ("#include \"osabi.h\"\n");
1882 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1883 printf_unfiltered ("\n");
1885 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1886 printf_unfiltered ("static void\n");
1887 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1888 printf_unfiltered ("{\n");
1890 (" struct target_desc *result = allocate_target_description ();\n");
1892 if (tdesc_architecture (e) != NULL)
1895 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1896 tdesc_architecture (e)->printable_name);
1897 printf_unfiltered ("\n");
1899 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1900 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1903 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1904 gdbarch_osabi_name (tdesc_osabi (e)));
1905 printf_unfiltered ("\n");
1908 for (const struct bfd_arch_info *compatible : e->compatible)
1910 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1911 compatible->printable_name);
1913 if (!e->compatible.empty ())
1914 printf_unfiltered ("\n");
1916 for (const property &prop : e->properties)
1917 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1918 prop.key.c_str (), prop.value.c_str ());
1920 printf_unfiltered (" struct tdesc_feature *feature;\n");
1923 void visit_pre (const tdesc_feature *e) override
1925 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1929 void visit_post (const tdesc_feature *e) override
1932 void visit_post (const target_desc *e) override
1934 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1935 printf_unfiltered ("}\n");
1938 void visit (const tdesc_type *type) override
1940 struct tdesc_type_field *f;
1942 /* Now we do some "filtering" in order to know which variables to
1943 declare. This is needed because otherwise we would declare unused
1944 variables `field_type' and `type'. */
1945 if (!m_printed_field_type)
1947 printf_unfiltered (" struct tdesc_type *field_type;\n");
1948 m_printed_field_type = true;
1951 if ((type->kind == TDESC_TYPE_UNION
1952 || type->kind == TDESC_TYPE_STRUCT
1953 || type->kind == TDESC_TYPE_FLAGS
1954 || type->kind == TDESC_TYPE_ENUM)
1955 && VEC_length (tdesc_type_field, type->u.u.fields) > 0
1958 printf_unfiltered (" struct tdesc_type *type;\n");
1959 m_printed_type = true;
1964 case TDESC_TYPE_VECTOR:
1966 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1967 type->u.v.type->name);
1969 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1970 type->name, type->u.v.count);
1972 case TDESC_TYPE_STRUCT:
1973 case TDESC_TYPE_FLAGS:
1974 if (type->kind == TDESC_TYPE_STRUCT)
1977 (" type = tdesc_create_struct (feature, \"%s\");\n",
1979 if (type->u.u.size != 0)
1981 (" tdesc_set_struct_size (type, %d);\n",
1987 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1988 type->name, type->u.u.size);
1991 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1994 const char *type_name;
1996 gdb_assert (f->type != NULL);
1997 type_name = f->type->name;
1999 /* To minimize changes to generated files, don't emit type
2000 info for fields that have defaulted types. */
2003 gdb_assert (f->end != -1);
2004 if (f->type->kind == TDESC_TYPE_BOOL)
2006 gdb_assert (f->start == f->end);
2008 (" tdesc_add_flag (type, %d, \"%s\");\n",
2011 else if ((type->u.u.size == 4
2012 && f->type->kind == TDESC_TYPE_UINT32)
2013 || (type->u.u.size == 8
2014 && f->type->kind == TDESC_TYPE_UINT64))
2017 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
2018 f->name, f->start, f->end);
2023 (" field_type = tdesc_named_type (feature,"
2027 (" tdesc_add_typed_bitfield (type, \"%s\","
2028 " %d, %d, field_type);\n",
2029 f->name, f->start, f->end);
2032 else /* Not a bitfield. */
2034 gdb_assert (f->end == -1);
2035 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
2037 (" field_type = tdesc_named_type (feature,"
2041 (" tdesc_add_field (type, \"%s\", field_type);\n",
2046 case TDESC_TYPE_UNION:
2048 (" type = tdesc_create_union (feature, \"%s\");\n",
2051 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2055 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2058 (" tdesc_add_field (type, \"%s\", field_type);\n",
2062 case TDESC_TYPE_ENUM:
2064 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2065 type->name, type->u.u.size);
2067 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2070 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2074 error (_("C output is not supported type \"%s\"."), type->name);
2076 printf_unfiltered ("\n");
2079 void visit (const tdesc_reg *reg) override
2081 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2082 reg->name, reg->target_regnum, reg->save_restore);
2084 printf_unfiltered ("\"%s\", ", reg->group);
2086 printf_unfiltered ("NULL, ");
2087 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2091 std::string m_filename_after_features;
2095 bool m_printed_field_type = false;
2096 bool m_printed_type = false;
2099 /* Print target description feature in C. */
2101 class print_c_feature : public print_c_tdesc
2104 print_c_feature (std::string &file)
2105 : print_c_tdesc (file)
2108 auto const pos = m_filename_after_features.find_last_of ('.');
2110 m_filename_after_features = m_filename_after_features.substr (0, pos);
2113 void visit_pre (const target_desc *e) override
2115 printf_unfiltered (" Original: %s */\n\n",
2116 lbasename (m_filename_after_features.c_str ()));
2118 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2119 printf_unfiltered ("\n");
2122 void visit_post (const target_desc *e) override
2125 void visit_pre (const tdesc_feature *e) override
2127 std::string name (m_filename_after_features);
2129 auto pos = name.find_first_of ('.');
2131 name = name.substr (0, pos);
2132 std::replace (name.begin (), name.end (), '/', '_');
2133 std::replace (name.begin (), name.end (), '-', '_');
2135 printf_unfiltered ("static int\n");
2136 printf_unfiltered ("create_feature_%s ", name.c_str ());
2137 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2139 printf_unfiltered ("{\n");
2140 printf_unfiltered (" struct tdesc_feature *feature;\n");
2143 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2144 e->name.c_str (), lbasename (m_filename_after_features.c_str ()));
2147 void visit_post (const tdesc_feature *e) override
2149 printf_unfiltered (" return regnum;\n");
2150 printf_unfiltered ("}\n");
2153 void visit (const tdesc_reg *reg) override
2155 /* Most "reg" in XML target descriptions don't have "regnum"
2156 attribute, so the register number is allocated sequentially.
2157 In case that reg has "regnum" attribute, register number
2158 should be set by that explicitly. */
2160 if (reg->target_regnum < m_next_regnum)
2162 /* The integrity check, it can catch some errors on register
2163 number collision, like this,
2165 <reg name="x0" bitsize="32"/>
2166 <reg name="x1" bitsize="32"/>
2167 <reg name="x2" bitsize="32"/>
2168 <reg name="x3" bitsize="32"/>
2169 <reg name="ps" bitsize="32" regnum="3"/>
2171 but it also has false negatives. The target description
2174 <reg name="x1" bitsize="32" regnum="1"/>
2175 <reg name="x3" bitsize="32" regnum="3"/>
2176 <reg name="x2" bitsize="32" regnum="2"/>
2177 <reg name="x4" bitsize="32" regnum="4"/>
2179 but it is not a good practice, so still error on this,
2180 and also print the message so that it can be saved in the
2181 generated c file. */
2183 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2184 reg->target_regnum);
2185 printf_unfiltered ("is not the largest number (%d).\n",
2187 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2188 reg->target_regnum, m_next_regnum);
2191 if (reg->target_regnum > m_next_regnum)
2193 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2194 m_next_regnum = reg->target_regnum;
2197 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2198 reg->name, reg->save_restore);
2200 printf_unfiltered ("\"%s\", ", reg->group);
2202 printf_unfiltered ("NULL, ");
2203 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2209 /* The register number to use for the next register we see. */
2210 int m_next_regnum = 0;
2214 maint_print_c_tdesc_cmd (const char *args, int from_tty)
2216 const struct target_desc *tdesc;
2217 const char *filename;
2221 /* Use the global target-supplied description, not the current
2222 architecture's. This lets a GDB for one architecture generate C
2223 for another architecture's description, even though the gdbarch
2224 initialization code will reject the new description. */
2225 tdesc = current_target_desc;
2226 filename = target_description_filename;
2230 /* Use the target description from the XML file. */
2232 tdesc = file_read_description_xml (filename);
2236 error (_("There is no target description to print."));
2238 if (filename == NULL)
2239 error (_("The current target description did not come from an XML file."));
2241 std::string filename_after_features (filename);
2242 auto loc = filename_after_features.rfind ("/features/");
2244 if (loc != std::string::npos)
2245 filename_after_features = filename_after_features.substr (loc + 10);
2247 /* Print c files for target features instead of target descriptions,
2248 because c files got from target features are more flexible than the
2250 if (startswith (filename_after_features.c_str (), "i386/32bit-")
2251 || startswith (filename_after_features.c_str (), "i386/64bit-")
2252 || startswith (filename_after_features.c_str (), "i386/x32-core.xml")
2253 || startswith (filename_after_features.c_str (), "tic6x-")
2254 || startswith (filename_after_features.c_str (), "aarch64"))
2256 print_c_feature v (filename_after_features);
2262 print_c_tdesc v (filename_after_features);
2268 namespace selftests {
2270 static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2274 /* See target-descritpions.h. */
2277 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2279 xml_tdesc.emplace_back (xml_file, tdesc);
2285 /* Check that the target descriptions created dynamically by
2286 architecture-specific code equal the descriptions created from XML files
2287 found in the specified directory DIR. */
2290 maintenance_check_xml_descriptions (const char *dir, int from_tty)
2293 error (_("Missing dir name"));
2295 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2296 std::string feature_dir (dir1.get ());
2297 unsigned int failed = 0;
2299 for (auto const &e : selftests::xml_tdesc)
2301 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2302 const target_desc *tdesc
2303 = file_read_description_xml (tdesc_xml.data ());
2305 if (tdesc == NULL || *tdesc != *e.second)
2308 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2309 (long) selftests::xml_tdesc.size (), failed);
2313 _initialize_target_descriptions (void)
2315 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2317 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2318 Set target description specific variables."),
2319 &tdesc_set_cmdlist, "set tdesc ",
2320 0 /* allow-unknown */, &setlist);
2321 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2322 Show target description specific variables."),
2323 &tdesc_show_cmdlist, "show tdesc ",
2324 0 /* allow-unknown */, &showlist);
2325 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2326 Unset target description specific variables."),
2327 &tdesc_unset_cmdlist, "unset tdesc ",
2328 0 /* allow-unknown */, &unsetlist);
2330 add_setshow_filename_cmd ("filename", class_obscure,
2331 &tdesc_filename_cmd_string,
2333 Set the file to read for an XML target description"), _("\
2334 Show the file to read for an XML target description"), _("\
2335 When set, GDB will read the target description from a local\n\
2336 file instead of querying the remote target."),
2337 set_tdesc_filename_cmd,
2338 show_tdesc_filename_cmd,
2339 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2341 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2342 Unset the file to read for an XML target description. When unset,\n\
2343 GDB will read the description from the target."),
2344 &tdesc_unset_cmdlist);
2346 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2347 Print the current target description as a C source file."),
2348 &maintenanceprintlist);
2350 cmd_list_element *cmd;
2352 cmd = add_cmd ("xml-descriptions", class_maintenance,
2353 maintenance_check_xml_descriptions, _("\
2354 Check the target descriptions created in GDB equal the descriptions\n\
2355 created from XML files in the directory.\n\
2356 The parameter is the directory name."),
2357 &maintenancechecklist);
2358 set_cmd_completer (cmd, filename_completer);