1 /* Target description support for GDB.
3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
26 #include "reggroups.h"
28 #include "target-descriptions.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
34 #include "gdb_obstack.h"
38 #include "completer.h"
39 #include "readline/tilde.h" /* tilde_expand */
41 /* The interface to visit different elements of target description. */
43 class tdesc_element_visitor
46 virtual void visit_pre (const target_desc *e) = 0;
47 virtual void visit_post (const target_desc *e) = 0;
49 virtual void visit_pre (const tdesc_feature *e) = 0;
50 virtual void visit_post (const tdesc_feature *e) = 0;
52 virtual void visit (const tdesc_type *e) = 0;
53 virtual void visit (const tdesc_reg *e) = 0;
59 virtual void accept (tdesc_element_visitor &v) const = 0;
66 property (const std::string &key_, const std::string &value_)
67 : key (key_), value (value_)
74 /* An individual register from a target description. */
76 struct tdesc_reg : tdesc_element
78 tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
79 int regnum, int save_restore_, const char *group_,
80 int bitsize_, const char *type_)
81 : name (name_), target_regnum (regnum),
82 save_restore (save_restore_),
83 group (group_ != NULL ? group_ : ""),
85 type (type_ != NULL ? type_ : "<unknown>")
87 /* If the register's type is target-defined, look it up now. We may not
88 have easy access to the containing feature when we want it later. */
89 tdesc_type = tdesc_named_type (feature, type.c_str ());
92 virtual ~tdesc_reg () = default;
94 DISABLE_COPY_AND_ASSIGN (tdesc_reg);
96 /* The name of this register. In standard features, it may be
97 recognized by the architecture support code, or it may be purely
101 /* The register number used by this target to refer to this
102 register. This is used for remote p/P packets and to determine
103 the ordering of registers in the remote g/G packets. */
106 /* If this flag is set, GDB should save and restore this register
107 around calls to an inferior function. */
110 /* The name of the register group containing this register, or empty
111 if the group should be automatically determined from the
112 register's type. If this is "general", "float", or "vector", the
113 corresponding "info" command should display this register's
114 value. It can be an arbitrary string, but should be limited to
115 alphanumeric characters and internal hyphens. Currently other
116 strings are ignored (treated as empty). */
119 /* The size of the register, in bits. */
122 /* The type of the register. This string corresponds to either
123 a named type from the target description or a predefined
127 /* The target-described type corresponding to TYPE, if found. */
128 struct tdesc_type *tdesc_type;
130 void accept (tdesc_element_visitor &v) const override
135 bool operator== (const tdesc_reg &other) const
137 return (name == other.name
138 && target_regnum == other.target_regnum
139 && save_restore == other.save_restore
140 && bitsize == other.bitsize
141 && group == other.group
142 && type == other.type);
145 bool operator!= (const tdesc_reg &other) const
147 return !(*this == other);
151 typedef std::unique_ptr<tdesc_reg> tdesc_reg_up;
153 /* A named type from a target description. */
155 struct tdesc_type_field
157 tdesc_type_field (const std::string &name_, tdesc_type *type_,
158 int start_, int end_)
159 : name (name_), type (type_), start (start_), end (end_)
163 struct tdesc_type *type;
164 /* For non-enum-values, either both are -1 (non-bitfield), or both are
165 not -1 (bitfield). For enum values, start is the value (which could be
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 struct tdesc_type : tdesc_element
201 tdesc_type (const std::string &name_, enum tdesc_type_kind kind_)
202 : name (name_), kind (kind_)
204 memset (&u, 0, sizeof (u));
208 case TDESC_TYPE_STRUCT:
209 case TDESC_TYPE_UNION:
210 case TDESC_TYPE_FLAGS:
211 case TDESC_TYPE_ENUM:
212 u.u.fields = new std::vector<tdesc_type_field> ();
220 virtual ~tdesc_type ()
224 case TDESC_TYPE_STRUCT:
225 case TDESC_TYPE_UNION:
226 case TDESC_TYPE_FLAGS:
227 case TDESC_TYPE_ENUM:
236 DISABLE_COPY_AND_ASSIGN (tdesc_type);
238 /* The name of this type. */
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 std::vector<tdesc_type_field> *fields;
262 void accept (tdesc_element_visitor &v) const override
267 bool operator== (const tdesc_type &other) const
269 return name == other.name && kind == other.kind;
272 bool operator!= (const tdesc_type &other) const
274 return !(*this == other);
278 typedef std::unique_ptr<tdesc_type> tdesc_type_up;
280 /* A feature from a target description. Each feature is a collection
281 of other elements, e.g. registers and types. */
283 struct tdesc_feature : tdesc_element
285 tdesc_feature (const std::string &name_)
289 virtual ~tdesc_feature () = default;
291 DISABLE_COPY_AND_ASSIGN (tdesc_feature);
293 /* The name of this feature. It may be recognized by the architecture
297 /* The registers associated with this feature. */
298 std::vector<std::unique_ptr<tdesc_reg>> registers;
300 /* The types associated with this feature. */
301 std::vector<tdesc_type_up> types;
303 void accept (tdesc_element_visitor &v) const override
307 for (const tdesc_type_up &type : types)
310 for (const tdesc_reg_up ® : registers)
316 bool operator== (const tdesc_feature &other) const
318 if (name != other.name)
321 if (registers.size () != other.registers.size ())
324 for (int ix = 0; ix < registers.size (); ix++)
326 const tdesc_reg_up ®1 = registers[ix];
327 const tdesc_reg_up ®2 = other.registers[ix];
329 if (reg1 != reg2 && *reg1 != *reg2)
333 if (types.size () != other.types.size ())
336 for (int ix = 0; ix < types.size (); ix++)
338 const tdesc_type_up &type1 = types[ix];
339 const tdesc_type_up &type2 = other.types[ix];
341 if (type1 != type2 && *type1 != *type2)
348 bool operator!= (const tdesc_feature &other) const
350 return !(*this == other);
354 typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
356 /* A target description. */
358 struct target_desc : tdesc_element
363 virtual ~target_desc () = default;
365 target_desc (const target_desc &) = delete;
366 void operator= (const target_desc &) = delete;
368 /* The architecture reported by the target, if any. */
369 const struct bfd_arch_info *arch = NULL;
371 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
373 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
375 /* The list of compatible architectures reported by the target. */
376 std::vector<const bfd_arch_info *> compatible;
378 /* Any architecture-specific properties specified by the target. */
379 std::vector<property> properties;
381 /* The features associated with this target. */
382 std::vector<std::unique_ptr<tdesc_feature>> features;
384 void accept (tdesc_element_visitor &v) const override
388 for (const tdesc_feature_up &feature : features)
394 bool operator== (const target_desc &other) const
396 if (arch != other.arch)
399 if (osabi != other.osabi)
402 if (features.size () != other.features.size ())
405 for (int ix = 0; ix < features.size (); ix++)
407 const tdesc_feature_up &feature1 = features[ix];
408 const tdesc_feature_up &feature2 = other.features[ix];
410 if (feature1 != feature2 && *feature1 != *feature2)
417 bool operator!= (const target_desc &other) const
419 return !(*this == other);
423 /* Per-architecture data associated with a target description. The
424 target description may be shared by multiple architectures, but
425 this data is private to one gdbarch. */
427 struct tdesc_arch_reg
429 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
430 : reg (reg_), type (type_)
433 struct tdesc_reg *reg;
437 struct tdesc_arch_data
439 /* A list of register/type pairs, indexed by GDB's internal register number.
440 During initialization of the gdbarch this list is used to store
441 registers which the architecture assigns a fixed register number.
442 Registers which are NULL in this array, or off the end, are
443 treated as zero-sized and nameless (i.e. placeholders in the
445 std::vector<tdesc_arch_reg> arch_regs;
447 /* Functions which report the register name, type, and reggroups for
449 gdbarch_register_name_ftype *pseudo_register_name = NULL;
450 gdbarch_register_type_ftype *pseudo_register_type = NULL;
451 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
454 /* Info about an inferior's target description. There's one of these
455 for each inferior. */
457 struct target_desc_info
459 /* A flag indicating that a description has already been fetched
460 from the target, so it should not be queried again. */
464 /* The description fetched from the target, or NULL if the target
465 did not supply any description. Only valid when
466 target_desc_fetched is set. Only the description initialization
467 code should access this; normally, the description should be
468 accessed through the gdbarch object. */
470 const struct target_desc *tdesc;
472 /* The filename to read a target description from, as set by "set
473 tdesc filename ..." */
478 /* Get the inferior INF's target description info, allocating one on
479 the stop if necessary. */
481 static struct target_desc_info *
482 get_tdesc_info (struct inferior *inf)
484 if (inf->tdesc_info == NULL)
485 inf->tdesc_info = XCNEW (struct target_desc_info);
486 return inf->tdesc_info;
489 /* A handle for architecture-specific data associated with the
490 target description (see struct tdesc_arch_data). */
492 static struct gdbarch_data *tdesc_data;
494 /* See target-descriptions.h. */
497 target_desc_info_from_user_p (struct target_desc_info *info)
499 return info != NULL && info->filename != NULL;
502 /* See target-descriptions.h. */
505 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
507 struct target_desc_info *src = get_tdesc_info (srcinf);
508 struct target_desc_info *dest = get_tdesc_info (destinf);
510 dest->fetched = src->fetched;
511 dest->tdesc = src->tdesc;
512 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
515 /* See target-descriptions.h. */
518 target_desc_info_free (struct target_desc_info *tdesc_info)
520 if (tdesc_info != NULL)
522 xfree (tdesc_info->filename);
527 /* Convenience helper macros. */
529 #define target_desc_fetched \
530 get_tdesc_info (current_inferior ())->fetched
531 #define current_target_desc \
532 get_tdesc_info (current_inferior ())->tdesc
533 #define target_description_filename \
534 get_tdesc_info (current_inferior ())->filename
536 /* The string manipulated by the "set tdesc filename ..." command. */
538 static char *tdesc_filename_cmd_string;
540 /* Fetch the current target's description, and switch the current
541 architecture to one which incorporates that description. */
544 target_find_description (void)
546 /* If we've already fetched a description from the target, don't do
547 it again. This allows a target to fetch the description early,
548 during its to_open or to_create_inferior, if it needs extra
549 information about the target to initialize. */
550 if (target_desc_fetched)
553 /* The current architecture should not have any target description
554 specified. It should have been cleared, e.g. when we
555 disconnected from the previous target. */
556 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
558 /* First try to fetch an XML description from the user-specified
560 current_target_desc = NULL;
561 if (target_description_filename != NULL
562 && *target_description_filename != '\0')
564 = file_read_description_xml (target_description_filename);
566 /* Next try to read the description from the current target using
568 if (current_target_desc == NULL)
569 current_target_desc = target_read_description_xml (¤t_target);
571 /* If that failed try a target-specific hook. */
572 if (current_target_desc == NULL)
573 current_target_desc = target_read_description (¤t_target);
575 /* If a non-NULL description was returned, then update the current
577 if (current_target_desc)
579 struct gdbarch_info info;
581 gdbarch_info_init (&info);
582 info.target_desc = current_target_desc;
583 if (!gdbarch_update_p (info))
584 warning (_("Architecture rejected target-supplied description"));
587 struct tdesc_arch_data *data;
589 data = ((struct tdesc_arch_data *)
590 gdbarch_data (target_gdbarch (), tdesc_data));
591 if (tdesc_has_registers (current_target_desc)
592 && data->arch_regs.empty ())
593 warning (_("Target-supplied registers are not supported "
594 "by the current architecture"));
598 /* Now that we know this description is usable, record that we
600 target_desc_fetched = 1;
603 /* Discard any description fetched from the current target, and switch
604 the current architecture to one with no target description. */
607 target_clear_description (void)
609 struct gdbarch_info info;
611 if (!target_desc_fetched)
614 target_desc_fetched = 0;
615 current_target_desc = NULL;
617 gdbarch_info_init (&info);
618 if (!gdbarch_update_p (info))
619 internal_error (__FILE__, __LINE__,
620 _("Could not remove target-supplied description"));
623 /* Return the global current target description. This should only be
624 used by gdbarch initialization code; most access should be through
625 an existing gdbarch. */
627 const struct target_desc *
628 target_current_description (void)
630 if (target_desc_fetched)
631 return current_target_desc;
636 /* Return non-zero if this target description is compatible
637 with the given BFD architecture. */
640 tdesc_compatible_p (const struct target_desc *target_desc,
641 const struct bfd_arch_info *arch)
643 for (const bfd_arch_info *compat : target_desc->compatible)
646 || arch->compatible (arch, compat)
647 || compat->compatible (compat, arch))
655 /* Direct accessors for target descriptions. */
657 /* Return the string value of a property named KEY, or NULL if the
658 property was not specified. */
661 tdesc_property (const struct target_desc *target_desc, const char *key)
663 for (const property &prop : target_desc->properties)
665 return prop.value.c_str ();
670 /* Return the BFD architecture associated with this target
671 description, or NULL if no architecture was specified. */
673 const struct bfd_arch_info *
674 tdesc_architecture (const struct target_desc *target_desc)
676 return target_desc->arch;
679 /* Return the OSABI associated with this target description, or
680 GDB_OSABI_UNKNOWN if no osabi was specified. */
683 tdesc_osabi (const struct target_desc *target_desc)
685 return target_desc->osabi;
690 /* Return 1 if this target description includes any registers. */
693 tdesc_has_registers (const struct target_desc *target_desc)
695 if (target_desc == NULL)
698 for (const tdesc_feature_up &feature : target_desc->features)
699 if (!feature->registers.empty ())
705 /* Return the feature with the given name, if present, or NULL if
706 the named feature is not found. */
708 const struct tdesc_feature *
709 tdesc_find_feature (const struct target_desc *target_desc,
712 for (const tdesc_feature_up &feature : target_desc->features)
713 if (feature->name == name)
714 return feature.get ();
719 /* Return the name of FEATURE. */
722 tdesc_feature_name (const struct tdesc_feature *feature)
724 return feature->name.c_str ();
727 /* Predefined types. */
728 static struct tdesc_type tdesc_predefined_types[] =
730 { "bool", TDESC_TYPE_BOOL },
731 { "int8", TDESC_TYPE_INT8 },
732 { "int16", TDESC_TYPE_INT16 },
733 { "int32", TDESC_TYPE_INT32 },
734 { "int64", TDESC_TYPE_INT64 },
735 { "int128", TDESC_TYPE_INT128 },
736 { "uint8", TDESC_TYPE_UINT8 },
737 { "uint16", TDESC_TYPE_UINT16 },
738 { "uint32", TDESC_TYPE_UINT32 },
739 { "uint64", TDESC_TYPE_UINT64 },
740 { "uint128", TDESC_TYPE_UINT128 },
741 { "code_ptr", TDESC_TYPE_CODE_PTR },
742 { "data_ptr", TDESC_TYPE_DATA_PTR },
743 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
744 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
745 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
746 { "i387_ext", TDESC_TYPE_I387_EXT }
749 /* Lookup a predefined type. */
751 static struct tdesc_type *
752 tdesc_predefined_type (enum tdesc_type_kind kind)
754 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
755 if (tdesc_predefined_types[ix].kind == kind)
756 return &tdesc_predefined_types[ix];
758 gdb_assert_not_reached ("bad predefined tdesc type");
761 /* See arch/tdesc.h. */
764 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
766 /* First try target-defined types. */
767 for (const tdesc_type_up &type : feature->types)
768 if (type->name == id)
771 /* Next try the predefined types. */
772 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
773 if (tdesc_predefined_types[ix].name == id)
774 return &tdesc_predefined_types[ix];
779 /* Lookup type associated with ID. */
782 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
784 tdesc_arch_data *data
785 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
787 for (const tdesc_arch_reg ® : data->arch_regs)
790 && reg.reg->tdesc_type
792 && reg.reg->tdesc_type->name == id)
799 /* Construct, if necessary, and return the GDB type implementing target
800 type TDESC_TYPE for architecture GDBARCH. */
803 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
807 switch (tdesc_type->kind)
809 /* Predefined types. */
810 case TDESC_TYPE_BOOL:
811 return builtin_type (gdbarch)->builtin_bool;
813 case TDESC_TYPE_INT8:
814 return builtin_type (gdbarch)->builtin_int8;
816 case TDESC_TYPE_INT16:
817 return builtin_type (gdbarch)->builtin_int16;
819 case TDESC_TYPE_INT32:
820 return builtin_type (gdbarch)->builtin_int32;
822 case TDESC_TYPE_INT64:
823 return builtin_type (gdbarch)->builtin_int64;
825 case TDESC_TYPE_INT128:
826 return builtin_type (gdbarch)->builtin_int128;
828 case TDESC_TYPE_UINT8:
829 return builtin_type (gdbarch)->builtin_uint8;
831 case TDESC_TYPE_UINT16:
832 return builtin_type (gdbarch)->builtin_uint16;
834 case TDESC_TYPE_UINT32:
835 return builtin_type (gdbarch)->builtin_uint32;
837 case TDESC_TYPE_UINT64:
838 return builtin_type (gdbarch)->builtin_uint64;
840 case TDESC_TYPE_UINT128:
841 return builtin_type (gdbarch)->builtin_uint128;
843 case TDESC_TYPE_CODE_PTR:
844 return builtin_type (gdbarch)->builtin_func_ptr;
846 case TDESC_TYPE_DATA_PTR:
847 return builtin_type (gdbarch)->builtin_data_ptr;
853 type = tdesc_find_type (gdbarch, tdesc_type->name.c_str ());
857 switch (tdesc_type->kind)
859 case TDESC_TYPE_IEEE_SINGLE:
860 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
861 floatformats_ieee_single);
863 case TDESC_TYPE_IEEE_DOUBLE:
864 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
865 floatformats_ieee_double);
867 case TDESC_TYPE_ARM_FPA_EXT:
868 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
869 floatformats_arm_ext);
871 case TDESC_TYPE_I387_EXT:
872 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
873 floatformats_i387_ext);
875 /* Types defined by a target feature. */
876 case TDESC_TYPE_VECTOR:
878 struct type *type, *field_type;
880 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
881 type = init_vector_type (field_type, tdesc_type->u.v.count);
882 TYPE_NAME (type) = xstrdup (tdesc_type->name.c_str ());
887 case TDESC_TYPE_STRUCT:
889 struct type *type, *field_type;
891 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
892 TYPE_NAME (type) = xstrdup (tdesc_type->name.c_str ());
893 TYPE_TAG_NAME (type) = TYPE_NAME (type);
895 for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
897 if (f.start != -1 && f.end != -1)
901 struct type *field_type;
902 int bitsize, total_size;
904 /* This invariant should be preserved while creating types. */
905 gdb_assert (tdesc_type->u.u.size != 0);
907 field_type = tdesc_gdb_type (gdbarch, f.type);
908 else if (tdesc_type->u.u.size > 4)
909 field_type = builtin_type (gdbarch)->builtin_uint64;
911 field_type = builtin_type (gdbarch)->builtin_uint32;
913 fld = append_composite_type_field_raw
914 (type, xstrdup (f.name.c_str ()), field_type);
916 /* For little-endian, BITPOS counts from the LSB of
917 the structure and marks the LSB of the field. For
918 big-endian, BITPOS counts from the MSB of the
919 structure and marks the MSB of the field. Either
920 way, it is the number of bits to the "left" of the
921 field. To calculate this in big-endian, we need
922 the total size of the structure. */
923 bitsize = f.end - f.start + 1;
924 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
925 if (gdbarch_bits_big_endian (gdbarch))
926 SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
928 SET_FIELD_BITPOS (fld[0], f.start);
929 FIELD_BITSIZE (fld[0]) = bitsize;
933 gdb_assert (f.start == -1 && f.end == -1);
934 field_type = tdesc_gdb_type (gdbarch, f.type);
935 append_composite_type_field (type, xstrdup (f.name.c_str ()),
940 if (tdesc_type->u.u.size != 0)
941 TYPE_LENGTH (type) = tdesc_type->u.u.size;
945 case TDESC_TYPE_UNION:
947 struct type *type, *field_type;
949 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
950 TYPE_NAME (type) = xstrdup (tdesc_type->name.c_str ());
952 for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
954 field_type = tdesc_gdb_type (gdbarch, f.type);
955 append_composite_type_field (type, xstrdup (f.name.c_str ()),
958 /* If any of the children of a union are vectors, flag the
959 union as a vector also. This allows e.g. a union of two
960 vector types to show up automatically in "info vector". */
961 if (TYPE_VECTOR (field_type))
962 TYPE_VECTOR (type) = 1;
967 case TDESC_TYPE_FLAGS:
969 type = arch_flags_type (gdbarch, tdesc_type->name.c_str (),
970 tdesc_type->u.u.size * TARGET_CHAR_BIT);
971 for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
973 struct type *field_type;
974 int bitsize = f.end - f.start + 1;
976 gdb_assert (f.type != NULL);
977 field_type = tdesc_gdb_type (gdbarch, f.type);
978 append_flags_type_field (type, f.start, bitsize,
979 field_type, f.name.c_str ());
985 case TDESC_TYPE_ENUM:
987 type = arch_type (gdbarch, TYPE_CODE_ENUM,
988 tdesc_type->u.u.size * TARGET_CHAR_BIT,
989 tdesc_type->name.c_str ());
990 TYPE_UNSIGNED (type) = 1;
991 for (const tdesc_type_field &f : *tdesc_type->u.u.fields)
994 = append_composite_type_field_raw (type,
995 xstrdup (f.name.c_str ()),
998 SET_FIELD_BITPOS (fld[0], f.start);
1005 internal_error (__FILE__, __LINE__,
1006 "Type \"%s\" has an unknown kind %d",
1007 tdesc_type->name.c_str (), tdesc_type->kind);
1011 /* Support for registers from target descriptions. */
1013 /* Construct the per-gdbarch data. */
1016 tdesc_data_init (struct obstack *obstack)
1018 struct tdesc_arch_data *data;
1020 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1021 new (data) tdesc_arch_data ();
1026 /* Similar, but for the temporary copy used during architecture
1029 struct tdesc_arch_data *
1030 tdesc_data_alloc (void)
1032 return new tdesc_arch_data ();
1035 /* Free something allocated by tdesc_data_alloc, if it is not going
1036 to be used (for instance if it was unsuitable for the
1040 tdesc_data_cleanup (void *data_untyped)
1042 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1047 /* Search FEATURE for a register named NAME. */
1049 static struct tdesc_reg *
1050 tdesc_find_register_early (const struct tdesc_feature *feature,
1053 for (const tdesc_reg_up ® : feature->registers)
1054 if (strcasecmp (reg->name.c_str (), name) == 0)
1060 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1063 tdesc_numbered_register (const struct tdesc_feature *feature,
1064 struct tdesc_arch_data *data,
1065 int regno, const char *name)
1067 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1072 /* Make sure the vector includes a REGNO'th element. */
1073 while (regno >= data->arch_regs.size ())
1074 data->arch_regs.emplace_back (nullptr, nullptr);
1076 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
1081 /* Search FEATURE for a register named NAME, but do not assign a fixed
1082 register number to it. */
1085 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1088 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1096 /* Search FEATURE for a register whose name is in NAMES and assign
1100 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1101 struct tdesc_arch_data *data,
1102 int regno, const char *const names[])
1106 for (i = 0; names[i] != NULL; i++)
1107 if (tdesc_numbered_register (feature, data, regno, names[i]))
1113 /* Search FEATURE for a register named NAME, and return its size in
1114 bits. The register must exist. */
1117 tdesc_register_size (const struct tdesc_feature *feature,
1120 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1122 gdb_assert (reg != NULL);
1123 return reg->bitsize;
1126 /* Look up a register by its GDB internal register number. */
1128 static struct tdesc_arch_reg *
1129 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1131 struct tdesc_arch_data *data;
1133 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1134 if (regno < data->arch_regs.size ())
1135 return &data->arch_regs[regno];
1140 static struct tdesc_reg *
1141 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1143 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1145 return reg? reg->reg : NULL;
1148 /* Return the name of register REGNO, from the target description or
1149 from an architecture-provided pseudo_register_name method. */
1152 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1154 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1155 int num_regs = gdbarch_num_regs (gdbarch);
1156 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1159 return reg->name.c_str ();
1161 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1163 struct tdesc_arch_data *data
1164 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1166 gdb_assert (data->pseudo_register_name != NULL);
1167 return data->pseudo_register_name (gdbarch, regno);
1174 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1176 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1177 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1178 int num_regs = gdbarch_num_regs (gdbarch);
1179 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1181 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1183 struct tdesc_arch_data *data
1184 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1186 gdb_assert (data->pseudo_register_type != NULL);
1187 return data->pseudo_register_type (gdbarch, regno);
1191 /* Return "int0_t", since "void" has a misleading size of one. */
1192 return builtin_type (gdbarch)->builtin_int0;
1194 if (arch_reg->type == NULL)
1196 /* First check for a predefined or target defined type. */
1197 if (reg->tdesc_type)
1198 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1200 /* Next try size-sensitive type shortcuts. */
1201 else if (reg->type == "float")
1203 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1204 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1205 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1206 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1207 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1208 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1211 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1212 reg->name.c_str (), reg->bitsize);
1213 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1216 else if (reg->type == "int")
1218 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1219 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1220 else if (reg->bitsize == TARGET_CHAR_BIT)
1221 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1222 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1223 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1224 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1225 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1226 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1227 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1228 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1229 /* A bit desperate by this point... */
1230 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1233 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1234 reg->name.c_str (), reg->bitsize);
1235 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1239 if (arch_reg->type == NULL)
1240 internal_error (__FILE__, __LINE__,
1241 "Register \"%s\" has an unknown type \"%s\"",
1242 reg->name.c_str (), reg->type.c_str ());
1245 return arch_reg->type;
1249 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1251 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1254 return reg->target_regnum;
1259 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1260 target description may be classified as general, float, or vector.
1261 Unlike a gdbarch register_reggroup_p method, this function will
1262 return -1 if it does not know; the caller should handle registers
1263 with no specified group.
1265 Arbitrary strings (other than "general", "float", and "vector")
1266 from the description are not used; they cause the register to be
1267 displayed in "info all-registers" but excluded from "info
1268 registers" et al. The names of containing features are also not
1269 used. This might be extended to display registers in some more
1272 The save-restore flag is also implemented here. */
1275 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1276 struct reggroup *reggroup)
1278 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1280 if (reg != NULL && !reg->group.empty ())
1282 int general_p = 0, float_p = 0, vector_p = 0;
1284 if (reg->group == "general")
1286 else if (reg->group == "float")
1288 else if (reg->group == "vector")
1291 if (reggroup == float_reggroup)
1294 if (reggroup == vector_reggroup)
1297 if (reggroup == general_reggroup)
1302 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1303 return reg->save_restore;
1308 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1309 group specified go to the default reggroup function and are handled
1313 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1314 struct reggroup *reggroup)
1316 int num_regs = gdbarch_num_regs (gdbarch);
1317 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1320 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1322 struct tdesc_arch_data *data
1323 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1325 if (data->pseudo_register_reggroup_p != NULL)
1326 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1327 /* Otherwise fall through to the default reggroup_p. */
1330 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1334 return default_register_reggroup_p (gdbarch, regno, reggroup);
1337 /* Record architecture-specific functions to call for pseudo-register
1341 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1342 gdbarch_register_name_ftype *pseudo_name)
1344 struct tdesc_arch_data *data
1345 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1347 data->pseudo_register_name = pseudo_name;
1351 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1352 gdbarch_register_type_ftype *pseudo_type)
1354 struct tdesc_arch_data *data
1355 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1357 data->pseudo_register_type = pseudo_type;
1361 set_tdesc_pseudo_register_reggroup_p
1362 (struct gdbarch *gdbarch,
1363 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1365 struct tdesc_arch_data *data
1366 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1368 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1371 /* Update GDBARCH to use the target description for registers. */
1374 tdesc_use_registers (struct gdbarch *gdbarch,
1375 const struct target_desc *target_desc,
1376 struct tdesc_arch_data *early_data)
1378 int num_regs = gdbarch_num_regs (gdbarch);
1379 struct tdesc_arch_data *data;
1382 /* We can't use the description for registers if it doesn't describe
1383 any. This function should only be called after validating
1384 registers, so the caller should know that registers are
1386 gdb_assert (tdesc_has_registers (target_desc));
1388 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1389 data->arch_regs = early_data->arch_regs;
1392 /* Build up a set of all registers, so that we can assign register
1393 numbers where needed. The hash table expands as necessary, so
1394 the initial size is arbitrary. */
1395 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1396 for (const tdesc_feature_up &feature : target_desc->features)
1397 for (const tdesc_reg_up ® : feature->registers)
1399 void **slot = htab_find_slot (reg_hash, reg.get (), INSERT);
1404 /* Remove any registers which were assigned numbers by the
1406 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1407 if (arch_reg.reg != NULL)
1408 htab_remove_elt (reg_hash, arch_reg.reg);
1410 /* Assign numbers to the remaining registers and add them to the
1411 list of registers. The new numbers are always above gdbarch_num_regs.
1412 Iterate over the features, not the hash table, so that the order
1413 matches that in the target description. */
1415 gdb_assert (data->arch_regs.size () <= num_regs);
1416 while (data->arch_regs.size () < num_regs)
1417 data->arch_regs.emplace_back (nullptr, nullptr);
1419 for (const tdesc_feature_up &feature : target_desc->features)
1420 for (const tdesc_reg_up ® : feature->registers)
1421 if (htab_find (reg_hash, reg.get ()) != NULL)
1423 data->arch_regs.emplace_back (reg.get (), nullptr);
1427 htab_delete (reg_hash);
1429 /* Update the architecture. */
1430 set_gdbarch_num_regs (gdbarch, num_regs);
1431 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1432 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1433 set_gdbarch_remote_register_number (gdbarch,
1434 tdesc_remote_register_number);
1435 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1439 /* See arch/tdesc.h. */
1442 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1443 int regnum, int save_restore, const char *group,
1444 int bitsize, const char *type)
1446 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1447 group, bitsize, type);
1449 feature->registers.emplace_back (reg);
1452 /* See arch/tdesc.h. */
1455 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1456 struct tdesc_type *field_type, int count)
1458 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1460 type->u.v.type = field_type;
1461 type->u.v.count = count;
1463 feature->types.emplace_back (type);
1467 /* See arch/tdesc.h. */
1470 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1472 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1474 feature->types.emplace_back (type);
1478 /* See arch/tdesc.h. */
1481 tdesc_set_struct_size (struct tdesc_type *type, int size)
1483 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1484 gdb_assert (size > 0);
1485 type->u.u.size = size;
1488 /* See arch/tdesc.h. */
1491 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1493 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1495 feature->types.emplace_back (type);
1499 /* See arch/tdesc.h. */
1502 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1505 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1507 gdb_assert (size > 0);
1509 type->u.u.size = size;
1511 feature->types.emplace_back (type);
1516 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1519 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1521 gdb_assert (size > 0);
1523 type->u.u.size = size;
1525 feature->types.emplace_back (type);
1529 /* See arch/tdesc.h. */
1532 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1533 struct tdesc_type *field_type)
1535 gdb_assert (type->kind == TDESC_TYPE_UNION
1536 || type->kind == TDESC_TYPE_STRUCT);
1538 /* Initialize start and end so we know this is not a bit-field
1539 when we print-c-tdesc. */
1540 type->u.u.fields->emplace_back (field_name, field_type, -1, -1);
1544 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1545 int start, int end, struct tdesc_type *field_type)
1547 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1548 || type->kind == TDESC_TYPE_FLAGS);
1549 gdb_assert (start >= 0 && end >= start);
1551 type->u.u.fields->emplace_back (field_name, field_type, start, end);
1554 /* See arch/tdesc.h. */
1557 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1560 struct tdesc_type *field_type;
1562 gdb_assert (start >= 0 && end >= start);
1564 if (type->u.u.size > 4)
1565 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1567 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1569 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1572 /* See arch/tdesc.h. */
1575 tdesc_add_flag (struct tdesc_type *type, int start,
1576 const char *flag_name)
1578 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1579 || type->kind == TDESC_TYPE_STRUCT);
1581 type->u.u.fields->emplace_back (flag_name,
1582 tdesc_predefined_type (TDESC_TYPE_BOOL),
1587 tdesc_add_enum_value (struct tdesc_type *type, int value,
1590 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1592 type->u.u.fields->emplace_back (name,
1593 tdesc_predefined_type (TDESC_TYPE_INT32),
1597 /* See arch/tdesc.h. */
1599 struct tdesc_feature *
1600 tdesc_create_feature (struct target_desc *tdesc, const char *name,
1603 struct tdesc_feature *new_feature = new tdesc_feature (name);
1605 tdesc->features.emplace_back (new_feature);
1610 struct target_desc *
1611 allocate_target_description (void)
1613 return new target_desc ();
1617 free_target_description (void *arg)
1619 struct target_desc *target_desc = (struct target_desc *) arg;
1625 make_cleanup_free_target_description (struct target_desc *target_desc)
1627 return make_cleanup (free_target_description, target_desc);
1631 tdesc_add_compatible (struct target_desc *target_desc,
1632 const struct bfd_arch_info *compatible)
1634 /* If this instance of GDB is compiled without BFD support for the
1635 compatible architecture, simply ignore it -- we would not be able
1636 to handle it anyway. */
1637 if (compatible == NULL)
1640 for (const bfd_arch_info *compat : target_desc->compatible)
1641 if (compat == compatible)
1642 internal_error (__FILE__, __LINE__,
1643 _("Attempted to add duplicate "
1644 "compatible architecture \"%s\""),
1645 compatible->printable_name);
1647 target_desc->compatible.push_back (compatible);
1651 set_tdesc_property (struct target_desc *target_desc,
1652 const char *key, const char *value)
1654 gdb_assert (key != NULL && value != NULL);
1656 if (tdesc_property (target_desc, key) != NULL)
1657 internal_error (__FILE__, __LINE__,
1658 _("Attempted to add duplicate property \"%s\""), key);
1660 target_desc->properties.emplace_back (key, value);
1663 /* See arch/tdesc.h. */
1666 set_tdesc_architecture (struct target_desc *target_desc,
1669 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1673 set_tdesc_architecture (struct target_desc *target_desc,
1674 const struct bfd_arch_info *arch)
1676 target_desc->arch = arch;
1679 /* See arch/tdesc.h. */
1682 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1684 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1688 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1690 target_desc->osabi = osabi;
1694 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1695 static struct cmd_list_element *tdesc_unset_cmdlist;
1697 /* Helper functions for the CLI commands. */
1700 set_tdesc_cmd (const char *args, int from_tty)
1702 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1706 show_tdesc_cmd (const char *args, int from_tty)
1708 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1712 unset_tdesc_cmd (const char *args, int from_tty)
1714 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1718 set_tdesc_filename_cmd (const char *args, int from_tty,
1719 struct cmd_list_element *c)
1721 xfree (target_description_filename);
1722 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1724 target_clear_description ();
1725 target_find_description ();
1729 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1730 struct cmd_list_element *c,
1733 value = target_description_filename;
1735 if (value != NULL && *value != '\0')
1736 printf_filtered (_("The target description will be read from \"%s\".\n"),
1739 printf_filtered (_("The target description will be "
1740 "read from the target.\n"));
1744 unset_tdesc_filename_cmd (const char *args, int from_tty)
1746 xfree (target_description_filename);
1747 target_description_filename = NULL;
1748 target_clear_description ();
1749 target_find_description ();
1752 /* Print target description in C. */
1754 class print_c_tdesc : public tdesc_element_visitor
1757 print_c_tdesc (std::string &filename_after_features)
1758 : m_filename_after_features (filename_after_features)
1762 const char *filename = lbasename (m_filename_after_features.c_str ());
1764 m_function = (char *) xmalloc (strlen (filename) + 1);
1765 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1768 else if (*inp == '-')
1774 /* Standard boilerplate. */
1775 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1776 "-*- buffer-read-only: t -*- vi"
1785 void visit_pre (const target_desc *e) override
1787 printf_unfiltered (" Original: %s */\n\n",
1788 lbasename (m_filename_after_features.c_str ()));
1790 printf_unfiltered ("#include \"defs.h\"\n");
1791 printf_unfiltered ("#include \"osabi.h\"\n");
1792 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1793 printf_unfiltered ("\n");
1795 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1796 printf_unfiltered ("static void\n");
1797 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1798 printf_unfiltered ("{\n");
1800 (" struct target_desc *result = allocate_target_description ();\n");
1802 if (tdesc_architecture (e) != NULL)
1805 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1806 tdesc_architecture (e)->printable_name);
1807 printf_unfiltered ("\n");
1809 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1810 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1813 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1814 gdbarch_osabi_name (tdesc_osabi (e)));
1815 printf_unfiltered ("\n");
1818 for (const struct bfd_arch_info *compatible : e->compatible)
1820 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1821 compatible->printable_name);
1823 if (!e->compatible.empty ())
1824 printf_unfiltered ("\n");
1826 for (const property &prop : e->properties)
1827 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1828 prop.key.c_str (), prop.value.c_str ());
1830 printf_unfiltered (" struct tdesc_feature *feature;\n");
1833 void visit_pre (const tdesc_feature *e) override
1835 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1839 void visit_post (const tdesc_feature *e) override
1842 void visit_post (const target_desc *e) override
1844 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1845 printf_unfiltered ("}\n");
1848 void visit (const tdesc_type *type) override
1850 /* Now we do some "filtering" in order to know which variables to
1851 declare. This is needed because otherwise we would declare unused
1852 variables `field_type' and `type'. */
1853 if (!m_printed_field_type)
1855 printf_unfiltered (" struct tdesc_type *field_type;\n");
1856 m_printed_field_type = true;
1859 if ((type->kind == TDESC_TYPE_UNION
1860 || type->kind == TDESC_TYPE_STRUCT
1861 || type->kind == TDESC_TYPE_FLAGS
1862 || type->kind == TDESC_TYPE_ENUM)
1863 && !type->u.u.fields->empty ()
1866 printf_unfiltered (" struct tdesc_type *type;\n");
1867 m_printed_type = true;
1872 case TDESC_TYPE_VECTOR:
1874 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1875 type->u.v.type->name.c_str ());
1877 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1878 type->name.c_str (), type->u.v.count);
1880 case TDESC_TYPE_STRUCT:
1881 case TDESC_TYPE_FLAGS:
1882 if (type->kind == TDESC_TYPE_STRUCT)
1885 (" type = tdesc_create_struct (feature, \"%s\");\n",
1886 type->name.c_str ());
1887 if (type->u.u.size != 0)
1889 (" tdesc_set_struct_size (type, %d);\n",
1895 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1896 type->name.c_str (), type->u.u.size);
1898 for (const tdesc_type_field &f : *type->u.u.fields)
1900 const char *type_name;
1902 gdb_assert (f.type != NULL);
1903 type_name = f.type->name.c_str ();
1905 /* To minimize changes to generated files, don't emit type
1906 info for fields that have defaulted types. */
1909 gdb_assert (f.end != -1);
1910 if (f.type->kind == TDESC_TYPE_BOOL)
1912 gdb_assert (f.start == f.end);
1914 (" tdesc_add_flag (type, %d, \"%s\");\n",
1915 f.start, f.name.c_str ());
1917 else if ((type->u.u.size == 4
1918 && f.type->kind == TDESC_TYPE_UINT32)
1919 || (type->u.u.size == 8
1920 && f.type->kind == TDESC_TYPE_UINT64))
1923 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1924 f.name.c_str (), f.start, f.end);
1929 (" field_type = tdesc_named_type (feature,"
1933 (" tdesc_add_typed_bitfield (type, \"%s\","
1934 " %d, %d, field_type);\n",
1935 f.name.c_str (), f.start, f.end);
1938 else /* Not a bitfield. */
1940 gdb_assert (f.end == -1);
1941 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1943 (" field_type = tdesc_named_type (feature,"
1947 (" tdesc_add_field (type, \"%s\", field_type);\n",
1952 case TDESC_TYPE_UNION:
1954 (" type = tdesc_create_union (feature, \"%s\");\n",
1955 type->name.c_str ());
1956 for (const tdesc_type_field &f : *type->u.u.fields)
1959 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1960 f.type->name.c_str ());
1962 (" tdesc_add_field (type, \"%s\", field_type);\n",
1966 case TDESC_TYPE_ENUM:
1968 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
1969 type->name.c_str (), type->u.u.size);
1970 for (const tdesc_type_field &f : *type->u.u.fields)
1972 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
1973 f.start, f.name.c_str ());
1976 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1978 printf_unfiltered ("\n");
1981 void visit (const tdesc_reg *reg) override
1983 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1984 reg->name.c_str (), reg->target_regnum,
1986 if (!reg->group.empty ())
1987 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
1989 printf_unfiltered ("NULL, ");
1990 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1994 std::string m_filename_after_features;
1998 bool m_printed_field_type = false;
1999 bool m_printed_type = false;
2002 /* Print target description feature in C. */
2004 class print_c_feature : public print_c_tdesc
2007 print_c_feature (std::string &file)
2008 : print_c_tdesc (file)
2011 auto const pos = m_filename_after_features.find_last_of ('.');
2013 m_filename_after_features = m_filename_after_features.substr (0, pos);
2016 void visit_pre (const target_desc *e) override
2018 printf_unfiltered (" Original: %s */\n\n",
2019 lbasename (m_filename_after_features.c_str ()));
2021 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2022 printf_unfiltered ("\n");
2025 void visit_post (const target_desc *e) override
2028 void visit_pre (const tdesc_feature *e) override
2030 std::string name (m_filename_after_features);
2032 auto pos = name.find_first_of ('.');
2034 name = name.substr (0, pos);
2035 std::replace (name.begin (), name.end (), '/', '_');
2036 std::replace (name.begin (), name.end (), '-', '_');
2038 printf_unfiltered ("static int\n");
2039 printf_unfiltered ("create_feature_%s ", name.c_str ());
2040 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2042 printf_unfiltered ("{\n");
2043 printf_unfiltered (" struct tdesc_feature *feature;\n");
2046 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2047 e->name.c_str (), lbasename (m_filename_after_features.c_str ()));
2050 void visit_post (const tdesc_feature *e) override
2052 printf_unfiltered (" return regnum;\n");
2053 printf_unfiltered ("}\n");
2056 void visit (const tdesc_reg *reg) override
2058 /* Most "reg" in XML target descriptions don't have "regnum"
2059 attribute, so the register number is allocated sequentially.
2060 In case that reg has "regnum" attribute, register number
2061 should be set by that explicitly. */
2063 if (reg->target_regnum < m_next_regnum)
2065 /* The integrity check, it can catch some errors on register
2066 number collision, like this,
2068 <reg name="x0" bitsize="32"/>
2069 <reg name="x1" bitsize="32"/>
2070 <reg name="x2" bitsize="32"/>
2071 <reg name="x3" bitsize="32"/>
2072 <reg name="ps" bitsize="32" regnum="3"/>
2074 but it also has false negatives. The target description
2077 <reg name="x1" bitsize="32" regnum="1"/>
2078 <reg name="x3" bitsize="32" regnum="3"/>
2079 <reg name="x2" bitsize="32" regnum="2"/>
2080 <reg name="x4" bitsize="32" regnum="4"/>
2082 but it is not a good practice, so still error on this,
2083 and also print the message so that it can be saved in the
2084 generated c file. */
2086 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2087 reg->target_regnum);
2088 printf_unfiltered ("is not the largest number (%d).\n",
2090 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2091 reg->target_regnum, m_next_regnum);
2094 if (reg->target_regnum > m_next_regnum)
2096 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2097 m_next_regnum = reg->target_regnum;
2100 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2101 reg->name.c_str (), reg->save_restore);
2102 if (!reg->group.empty ())
2103 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
2105 printf_unfiltered ("NULL, ");
2106 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
2112 /* The register number to use for the next register we see. */
2113 int m_next_regnum = 0;
2117 maint_print_c_tdesc_cmd (const char *args, int from_tty)
2119 const struct target_desc *tdesc;
2120 const char *filename;
2124 /* Use the global target-supplied description, not the current
2125 architecture's. This lets a GDB for one architecture generate C
2126 for another architecture's description, even though the gdbarch
2127 initialization code will reject the new description. */
2128 tdesc = current_target_desc;
2129 filename = target_description_filename;
2133 /* Use the target description from the XML file. */
2135 tdesc = file_read_description_xml (filename);
2139 error (_("There is no target description to print."));
2141 if (filename == NULL)
2142 error (_("The current target description did not come from an XML file."));
2144 std::string filename_after_features (filename);
2145 auto loc = filename_after_features.rfind ("/features/");
2147 if (loc != std::string::npos)
2148 filename_after_features = filename_after_features.substr (loc + 10);
2150 /* Print c files for target features instead of target descriptions,
2151 because c files got from target features are more flexible than the
2153 if (startswith (filename_after_features.c_str (), "i386/32bit-")
2154 || startswith (filename_after_features.c_str (), "i386/64bit-")
2155 || startswith (filename_after_features.c_str (), "i386/x32-core.xml")
2156 || startswith (filename_after_features.c_str (), "tic6x-")
2157 || startswith (filename_after_features.c_str (), "aarch64"))
2159 print_c_feature v (filename_after_features);
2165 print_c_tdesc v (filename_after_features);
2171 namespace selftests {
2173 static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2177 /* See target-descritpions.h. */
2180 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2182 xml_tdesc.emplace_back (xml_file, tdesc);
2188 /* Check that the target descriptions created dynamically by
2189 architecture-specific code equal the descriptions created from XML files
2190 found in the specified directory DIR. */
2193 maintenance_check_xml_descriptions (const char *dir, int from_tty)
2196 error (_("Missing dir name"));
2198 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2199 std::string feature_dir (dir1.get ());
2200 unsigned int failed = 0;
2202 for (auto const &e : selftests::xml_tdesc)
2204 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2205 const target_desc *tdesc
2206 = file_read_description_xml (tdesc_xml.data ());
2208 if (tdesc == NULL || *tdesc != *e.second)
2211 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2212 (long) selftests::xml_tdesc.size (), failed);
2216 _initialize_target_descriptions (void)
2218 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2220 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2221 Set target description specific variables."),
2222 &tdesc_set_cmdlist, "set tdesc ",
2223 0 /* allow-unknown */, &setlist);
2224 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2225 Show target description specific variables."),
2226 &tdesc_show_cmdlist, "show tdesc ",
2227 0 /* allow-unknown */, &showlist);
2228 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2229 Unset target description specific variables."),
2230 &tdesc_unset_cmdlist, "unset tdesc ",
2231 0 /* allow-unknown */, &unsetlist);
2233 add_setshow_filename_cmd ("filename", class_obscure,
2234 &tdesc_filename_cmd_string,
2236 Set the file to read for an XML target description"), _("\
2237 Show the file to read for an XML target description"), _("\
2238 When set, GDB will read the target description from a local\n\
2239 file instead of querying the remote target."),
2240 set_tdesc_filename_cmd,
2241 show_tdesc_filename_cmd,
2242 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2244 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2245 Unset the file to read for an XML target description. When unset,\n\
2246 GDB will read the description from the target."),
2247 &tdesc_unset_cmdlist);
2249 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2250 Print the current target description as a C source file."),
2251 &maintenanceprintlist);
2253 cmd_list_element *cmd;
2255 cmd = add_cmd ("xml-descriptions", class_maintenance,
2256 maintenance_check_xml_descriptions, _("\
2257 Check the target descriptions created in GDB equal the descriptions\n\
2258 created from XML files in the directory.\n\
2259 The parameter is the directory name."),
2260 &maintenancechecklist);
2261 set_cmd_completer (cmd, filename_completer);