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_builtin *e) = 0;
53 virtual void visit (const tdesc_type_vector *e) = 0;
54 virtual void visit (const tdesc_type_with_fields *e) = 0;
56 virtual void visit (const tdesc_reg *e) = 0;
62 virtual void accept (tdesc_element_visitor &v) const = 0;
69 property (const std::string &key_, const std::string &value_)
70 : key (key_), value (value_)
77 /* An individual register from a target description. */
79 struct tdesc_reg : tdesc_element
81 tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
82 int regnum, int save_restore_, const char *group_,
83 int bitsize_, const char *type_)
84 : name (name_), target_regnum (regnum),
85 save_restore (save_restore_),
86 group (group_ != NULL ? group_ : ""),
88 type (type_ != NULL ? type_ : "<unknown>")
90 /* If the register's type is target-defined, look it up now. We may not
91 have easy access to the containing feature when we want it later. */
92 tdesc_type = tdesc_named_type (feature, type.c_str ());
95 virtual ~tdesc_reg () = default;
97 DISABLE_COPY_AND_ASSIGN (tdesc_reg);
99 /* The name of this register. In standard features, it may be
100 recognized by the architecture support code, or it may be purely
104 /* The register number used by this target to refer to this
105 register. This is used for remote p/P packets and to determine
106 the ordering of registers in the remote g/G packets. */
109 /* If this flag is set, GDB should save and restore this register
110 around calls to an inferior function. */
113 /* The name of the register group containing this register, or empty
114 if the group should be automatically determined from the
115 register's type. If this is "general", "float", or "vector", the
116 corresponding "info" command should display this register's
117 value. It can be an arbitrary string, but should be limited to
118 alphanumeric characters and internal hyphens. Currently other
119 strings are ignored (treated as empty). */
122 /* The size of the register, in bits. */
125 /* The type of the register. This string corresponds to either
126 a named type from the target description or a predefined
130 /* The target-described type corresponding to TYPE, if found. */
131 struct tdesc_type *tdesc_type;
133 void accept (tdesc_element_visitor &v) const override
138 bool operator== (const tdesc_reg &other) const
140 return (name == other.name
141 && target_regnum == other.target_regnum
142 && save_restore == other.save_restore
143 && bitsize == other.bitsize
144 && group == other.group
145 && type == other.type);
148 bool operator!= (const tdesc_reg &other) const
150 return !(*this == other);
154 typedef std::unique_ptr<tdesc_reg> tdesc_reg_up;
156 /* A named type from a target description. */
158 struct tdesc_type_field
160 tdesc_type_field (const std::string &name_, tdesc_type *type_,
161 int start_, int end_)
162 : name (name_), type (type_), start (start_), end (end_)
166 struct tdesc_type *type;
167 /* For non-enum-values, either both are -1 (non-bitfield), or both are
168 not -1 (bitfield). For enum values, start is the value (which could be
175 /* Predefined types. */
189 TDESC_TYPE_IEEE_SINGLE,
190 TDESC_TYPE_IEEE_DOUBLE,
191 TDESC_TYPE_ARM_FPA_EXT,
194 /* Types defined by a target feature. */
202 struct tdesc_type : tdesc_element
204 tdesc_type (const std::string &name_, enum tdesc_type_kind kind_)
205 : name (name_), kind (kind_)
208 virtual ~tdesc_type () = default;
210 DISABLE_COPY_AND_ASSIGN (tdesc_type);
212 /* The name of this type. */
215 /* Identify the kind of this type. */
216 enum tdesc_type_kind kind;
218 bool operator== (const tdesc_type &other) const
220 return name == other.name && kind == other.kind;
223 bool operator!= (const tdesc_type &other) const
225 return !(*this == other);
228 /* Construct, if necessary, and return the GDB type implementing this
229 target type for architecture GDBARCH. */
231 virtual type *make_gdb_type (struct gdbarch *gdbarch) const = 0;
234 typedef std::unique_ptr<tdesc_type> tdesc_type_up;
236 struct tdesc_type_builtin : tdesc_type
238 tdesc_type_builtin (const std::string &name, enum tdesc_type_kind kind)
239 : tdesc_type (name, kind)
242 void accept (tdesc_element_visitor &v) const override
247 type *make_gdb_type (struct gdbarch *gdbarch) const override
251 /* Predefined types. */
252 case TDESC_TYPE_BOOL:
253 return builtin_type (gdbarch)->builtin_bool;
255 case TDESC_TYPE_INT8:
256 return builtin_type (gdbarch)->builtin_int8;
258 case TDESC_TYPE_INT16:
259 return builtin_type (gdbarch)->builtin_int16;
261 case TDESC_TYPE_INT32:
262 return builtin_type (gdbarch)->builtin_int32;
264 case TDESC_TYPE_INT64:
265 return builtin_type (gdbarch)->builtin_int64;
267 case TDESC_TYPE_INT128:
268 return builtin_type (gdbarch)->builtin_int128;
270 case TDESC_TYPE_UINT8:
271 return builtin_type (gdbarch)->builtin_uint8;
273 case TDESC_TYPE_UINT16:
274 return builtin_type (gdbarch)->builtin_uint16;
276 case TDESC_TYPE_UINT32:
277 return builtin_type (gdbarch)->builtin_uint32;
279 case TDESC_TYPE_UINT64:
280 return builtin_type (gdbarch)->builtin_uint64;
282 case TDESC_TYPE_UINT128:
283 return builtin_type (gdbarch)->builtin_uint128;
285 case TDESC_TYPE_CODE_PTR:
286 return builtin_type (gdbarch)->builtin_func_ptr;
288 case TDESC_TYPE_DATA_PTR:
289 return builtin_type (gdbarch)->builtin_data_ptr;
292 type *gdb_type = tdesc_find_type (gdbarch, this->name.c_str ());
293 if (gdb_type != NULL)
298 case TDESC_TYPE_IEEE_SINGLE:
299 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
300 floatformats_ieee_single);
302 case TDESC_TYPE_IEEE_DOUBLE:
303 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
304 floatformats_ieee_double);
306 case TDESC_TYPE_ARM_FPA_EXT:
307 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
308 floatformats_arm_ext);
310 case TDESC_TYPE_I387_EXT:
311 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
312 floatformats_i387_ext);
315 internal_error (__FILE__, __LINE__,
316 "Type \"%s\" has an unknown kind %d",
317 this->name.c_str (), this->kind);
323 /* tdesc_type for vector types. */
325 struct tdesc_type_vector : tdesc_type
327 tdesc_type_vector (const std::string &name, tdesc_type *element_type_, int count_)
328 : tdesc_type (name, TDESC_TYPE_VECTOR),
329 element_type (element_type_), count (count_)
332 void accept (tdesc_element_visitor &v) const override
337 type *make_gdb_type (struct gdbarch *gdbarch) const override
339 type *vector_gdb_type = tdesc_find_type (gdbarch, this->name.c_str ());
340 if (vector_gdb_type != NULL)
341 return vector_gdb_type;
343 type *element_gdb_type = this->element_type->make_gdb_type (gdbarch);
344 vector_gdb_type = init_vector_type (element_gdb_type, this->count);
345 TYPE_NAME (vector_gdb_type) = xstrdup (this->name.c_str ());
347 return vector_gdb_type;
350 struct tdesc_type *element_type;
354 /* tdesc_type for struct, union, flags, and enum types. */
356 struct tdesc_type_with_fields : tdesc_type
358 tdesc_type_with_fields (const std::string &name, tdesc_type_kind kind,
360 : tdesc_type (name, kind), size (size_)
363 void accept (tdesc_element_visitor &v) const override
368 type *make_gdb_type_struct (struct gdbarch *gdbarch) const
370 type *struct_gdb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
371 TYPE_NAME (struct_gdb_type) = xstrdup (this->name.c_str ());
372 TYPE_TAG_NAME (struct_gdb_type) = TYPE_NAME (struct_gdb_type);
374 for (const tdesc_type_field &f : this->fields)
376 if (f.start != -1 && f.end != -1)
380 struct type *field_gdb_type;
381 int bitsize, total_size;
383 /* This invariant should be preserved while creating types. */
384 gdb_assert (this->size != 0);
386 field_gdb_type = f.type->make_gdb_type (gdbarch);
387 else if (this->size > 4)
388 field_gdb_type = builtin_type (gdbarch)->builtin_uint64;
390 field_gdb_type = builtin_type (gdbarch)->builtin_uint32;
392 fld = append_composite_type_field_raw
393 (struct_gdb_type, xstrdup (f.name.c_str ()), field_gdb_type);
395 /* For little-endian, BITPOS counts from the LSB of
396 the structure and marks the LSB of the field. For
397 big-endian, BITPOS counts from the MSB of the
398 structure and marks the MSB of the field. Either
399 way, it is the number of bits to the "left" of the
400 field. To calculate this in big-endian, we need
401 the total size of the structure. */
402 bitsize = f.end - f.start + 1;
403 total_size = this->size * TARGET_CHAR_BIT;
404 if (gdbarch_bits_big_endian (gdbarch))
405 SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
407 SET_FIELD_BITPOS (fld[0], f.start);
408 FIELD_BITSIZE (fld[0]) = bitsize;
412 gdb_assert (f.start == -1 && f.end == -1);
413 type *field_gdb_type = f.type->make_gdb_type (gdbarch);
414 append_composite_type_field (struct_gdb_type,
415 xstrdup (f.name.c_str ()),
421 TYPE_LENGTH (struct_gdb_type) = this->size;
423 return struct_gdb_type;
426 type *make_gdb_type_union (struct gdbarch *gdbarch) const
428 type *union_gdb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
429 TYPE_NAME (union_gdb_type) = xstrdup (this->name.c_str ());
431 for (const tdesc_type_field &f : this->fields)
433 type* field_gdb_type = f.type->make_gdb_type (gdbarch);
434 append_composite_type_field (union_gdb_type, xstrdup (f.name.c_str ()),
437 /* If any of the children of a union are vectors, flag the
438 union as a vector also. This allows e.g. a union of two
439 vector types to show up automatically in "info vector". */
440 if (TYPE_VECTOR (field_gdb_type))
441 TYPE_VECTOR (union_gdb_type) = 1;
444 return union_gdb_type;
447 type *make_gdb_type_flags (struct gdbarch *gdbarch) const
449 type *flags_gdb_type = arch_flags_type (gdbarch, this->name.c_str (),
450 this->size * TARGET_CHAR_BIT);
452 for (const tdesc_type_field &f : this->fields)
454 int bitsize = f.end - f.start + 1;
456 gdb_assert (f.type != NULL);
457 type *field_gdb_type = f.type->make_gdb_type (gdbarch);
458 append_flags_type_field (flags_gdb_type, f.start, bitsize,
459 field_gdb_type, f.name.c_str ());
462 return flags_gdb_type;
465 type *make_gdb_type_enum (struct gdbarch *gdbarch) const
467 type *enum_gdb_type = arch_type (gdbarch, TYPE_CODE_ENUM,
468 this->size * TARGET_CHAR_BIT,
469 this->name.c_str ());
471 TYPE_UNSIGNED (enum_gdb_type) = 1;
472 for (const tdesc_type_field &f : this->fields)
475 = append_composite_type_field_raw (enum_gdb_type,
476 xstrdup (f.name.c_str ()),
479 SET_FIELD_BITPOS (fld[0], f.start);
482 return enum_gdb_type;
485 type *make_gdb_type (struct gdbarch *gdbarch) const override
487 type *gdb_type = tdesc_find_type (gdbarch, this->name.c_str ());
488 if (gdb_type != NULL)
493 case TDESC_TYPE_STRUCT:
494 return make_gdb_type_struct (gdbarch);
495 case TDESC_TYPE_UNION:
496 return make_gdb_type_union (gdbarch);
497 case TDESC_TYPE_FLAGS:
498 return make_gdb_type_flags (gdbarch);
499 case TDESC_TYPE_ENUM:
500 return make_gdb_type_enum (gdbarch);
503 internal_error (__FILE__, __LINE__,
504 "Type \"%s\" has an unknown kind %d",
505 this->name.c_str (), this->kind);
510 std::vector<tdesc_type_field> fields;
514 /* A feature from a target description. Each feature is a collection
515 of other elements, e.g. registers and types. */
517 struct tdesc_feature : tdesc_element
519 tdesc_feature (const std::string &name_)
523 virtual ~tdesc_feature () = default;
525 DISABLE_COPY_AND_ASSIGN (tdesc_feature);
527 /* The name of this feature. It may be recognized by the architecture
531 /* The registers associated with this feature. */
532 std::vector<tdesc_reg_up> registers;
534 /* The types associated with this feature. */
535 std::vector<tdesc_type_up> types;
537 void accept (tdesc_element_visitor &v) const override
541 for (const tdesc_type_up &type : types)
544 for (const tdesc_reg_up ® : registers)
550 bool operator== (const tdesc_feature &other) const
552 if (name != other.name)
555 if (registers.size () != other.registers.size ())
558 for (int ix = 0; ix < registers.size (); ix++)
560 const tdesc_reg_up ®1 = registers[ix];
561 const tdesc_reg_up ®2 = other.registers[ix];
563 if (reg1 != reg2 && *reg1 != *reg2)
567 if (types.size () != other.types.size ())
570 for (int ix = 0; ix < types.size (); ix++)
572 const tdesc_type_up &type1 = types[ix];
573 const tdesc_type_up &type2 = other.types[ix];
575 if (type1 != type2 && *type1 != *type2)
582 bool operator!= (const tdesc_feature &other) const
584 return !(*this == other);
588 typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
590 /* A target description. */
592 struct target_desc : tdesc_element
597 virtual ~target_desc () = default;
599 target_desc (const target_desc &) = delete;
600 void operator= (const target_desc &) = delete;
602 /* The architecture reported by the target, if any. */
603 const struct bfd_arch_info *arch = NULL;
605 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
607 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
609 /* The list of compatible architectures reported by the target. */
610 std::vector<const bfd_arch_info *> compatible;
612 /* Any architecture-specific properties specified by the target. */
613 std::vector<property> properties;
615 /* The features associated with this target. */
616 std::vector<tdesc_feature_up> features;
618 void accept (tdesc_element_visitor &v) const override
622 for (const tdesc_feature_up &feature : features)
628 bool operator== (const target_desc &other) const
630 if (arch != other.arch)
633 if (osabi != other.osabi)
636 if (features.size () != other.features.size ())
639 for (int ix = 0; ix < features.size (); ix++)
641 const tdesc_feature_up &feature1 = features[ix];
642 const tdesc_feature_up &feature2 = other.features[ix];
644 if (feature1 != feature2 && *feature1 != *feature2)
651 bool operator!= (const target_desc &other) const
653 return !(*this == other);
657 /* Per-architecture data associated with a target description. The
658 target description may be shared by multiple architectures, but
659 this data is private to one gdbarch. */
661 struct tdesc_arch_reg
663 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
664 : reg (reg_), type (type_)
667 struct tdesc_reg *reg;
671 struct tdesc_arch_data
673 /* A list of register/type pairs, indexed by GDB's internal register number.
674 During initialization of the gdbarch this list is used to store
675 registers which the architecture assigns a fixed register number.
676 Registers which are NULL in this array, or off the end, are
677 treated as zero-sized and nameless (i.e. placeholders in the
679 std::vector<tdesc_arch_reg> arch_regs;
681 /* Functions which report the register name, type, and reggroups for
683 gdbarch_register_name_ftype *pseudo_register_name = NULL;
684 gdbarch_register_type_ftype *pseudo_register_type = NULL;
685 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
688 /* Info about an inferior's target description. There's one of these
689 for each inferior. */
691 struct target_desc_info
693 /* A flag indicating that a description has already been fetched
694 from the target, so it should not be queried again. */
698 /* The description fetched from the target, or NULL if the target
699 did not supply any description. Only valid when
700 target_desc_fetched is set. Only the description initialization
701 code should access this; normally, the description should be
702 accessed through the gdbarch object. */
704 const struct target_desc *tdesc;
706 /* The filename to read a target description from, as set by "set
707 tdesc filename ..." */
712 /* Get the inferior INF's target description info, allocating one on
713 the stop if necessary. */
715 static struct target_desc_info *
716 get_tdesc_info (struct inferior *inf)
718 if (inf->tdesc_info == NULL)
719 inf->tdesc_info = XCNEW (struct target_desc_info);
720 return inf->tdesc_info;
723 /* A handle for architecture-specific data associated with the
724 target description (see struct tdesc_arch_data). */
726 static struct gdbarch_data *tdesc_data;
728 /* See target-descriptions.h. */
731 target_desc_info_from_user_p (struct target_desc_info *info)
733 return info != NULL && info->filename != NULL;
736 /* See target-descriptions.h. */
739 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
741 struct target_desc_info *src = get_tdesc_info (srcinf);
742 struct target_desc_info *dest = get_tdesc_info (destinf);
744 dest->fetched = src->fetched;
745 dest->tdesc = src->tdesc;
746 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
749 /* See target-descriptions.h. */
752 target_desc_info_free (struct target_desc_info *tdesc_info)
754 if (tdesc_info != NULL)
756 xfree (tdesc_info->filename);
761 /* Convenience helper macros. */
763 #define target_desc_fetched \
764 get_tdesc_info (current_inferior ())->fetched
765 #define current_target_desc \
766 get_tdesc_info (current_inferior ())->tdesc
767 #define target_description_filename \
768 get_tdesc_info (current_inferior ())->filename
770 /* The string manipulated by the "set tdesc filename ..." command. */
772 static char *tdesc_filename_cmd_string;
774 /* Fetch the current target's description, and switch the current
775 architecture to one which incorporates that description. */
778 target_find_description (void)
780 /* If we've already fetched a description from the target, don't do
781 it again. This allows a target to fetch the description early,
782 during its to_open or to_create_inferior, if it needs extra
783 information about the target to initialize. */
784 if (target_desc_fetched)
787 /* The current architecture should not have any target description
788 specified. It should have been cleared, e.g. when we
789 disconnected from the previous target. */
790 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
792 /* First try to fetch an XML description from the user-specified
794 current_target_desc = NULL;
795 if (target_description_filename != NULL
796 && *target_description_filename != '\0')
798 = file_read_description_xml (target_description_filename);
800 /* Next try to read the description from the current target using
802 if (current_target_desc == NULL)
803 current_target_desc = target_read_description_xml (¤t_target);
805 /* If that failed try a target-specific hook. */
806 if (current_target_desc == NULL)
807 current_target_desc = target_read_description (¤t_target);
809 /* If a non-NULL description was returned, then update the current
811 if (current_target_desc)
813 struct gdbarch_info info;
815 gdbarch_info_init (&info);
816 info.target_desc = current_target_desc;
817 if (!gdbarch_update_p (info))
818 warning (_("Architecture rejected target-supplied description"));
821 struct tdesc_arch_data *data;
823 data = ((struct tdesc_arch_data *)
824 gdbarch_data (target_gdbarch (), tdesc_data));
825 if (tdesc_has_registers (current_target_desc)
826 && data->arch_regs.empty ())
827 warning (_("Target-supplied registers are not supported "
828 "by the current architecture"));
832 /* Now that we know this description is usable, record that we
834 target_desc_fetched = 1;
837 /* Discard any description fetched from the current target, and switch
838 the current architecture to one with no target description. */
841 target_clear_description (void)
843 struct gdbarch_info info;
845 if (!target_desc_fetched)
848 target_desc_fetched = 0;
849 current_target_desc = NULL;
851 gdbarch_info_init (&info);
852 if (!gdbarch_update_p (info))
853 internal_error (__FILE__, __LINE__,
854 _("Could not remove target-supplied description"));
857 /* Return the global current target description. This should only be
858 used by gdbarch initialization code; most access should be through
859 an existing gdbarch. */
861 const struct target_desc *
862 target_current_description (void)
864 if (target_desc_fetched)
865 return current_target_desc;
870 /* Return non-zero if this target description is compatible
871 with the given BFD architecture. */
874 tdesc_compatible_p (const struct target_desc *target_desc,
875 const struct bfd_arch_info *arch)
877 for (const bfd_arch_info *compat : target_desc->compatible)
880 || arch->compatible (arch, compat)
881 || compat->compatible (compat, arch))
889 /* Direct accessors for target descriptions. */
891 /* Return the string value of a property named KEY, or NULL if the
892 property was not specified. */
895 tdesc_property (const struct target_desc *target_desc, const char *key)
897 for (const property &prop : target_desc->properties)
899 return prop.value.c_str ();
904 /* Return the BFD architecture associated with this target
905 description, or NULL if no architecture was specified. */
907 const struct bfd_arch_info *
908 tdesc_architecture (const struct target_desc *target_desc)
910 return target_desc->arch;
913 /* Return the OSABI associated with this target description, or
914 GDB_OSABI_UNKNOWN if no osabi was specified. */
917 tdesc_osabi (const struct target_desc *target_desc)
919 return target_desc->osabi;
924 /* Return 1 if this target description includes any registers. */
927 tdesc_has_registers (const struct target_desc *target_desc)
929 if (target_desc == NULL)
932 for (const tdesc_feature_up &feature : target_desc->features)
933 if (!feature->registers.empty ())
939 /* Return the feature with the given name, if present, or NULL if
940 the named feature is not found. */
942 const struct tdesc_feature *
943 tdesc_find_feature (const struct target_desc *target_desc,
946 for (const tdesc_feature_up &feature : target_desc->features)
947 if (feature->name == name)
948 return feature.get ();
953 /* Return the name of FEATURE. */
956 tdesc_feature_name (const struct tdesc_feature *feature)
958 return feature->name.c_str ();
961 /* Predefined types. */
962 static tdesc_type_builtin tdesc_predefined_types[] =
964 { "bool", TDESC_TYPE_BOOL },
965 { "int8", TDESC_TYPE_INT8 },
966 { "int16", TDESC_TYPE_INT16 },
967 { "int32", TDESC_TYPE_INT32 },
968 { "int64", TDESC_TYPE_INT64 },
969 { "int128", TDESC_TYPE_INT128 },
970 { "uint8", TDESC_TYPE_UINT8 },
971 { "uint16", TDESC_TYPE_UINT16 },
972 { "uint32", TDESC_TYPE_UINT32 },
973 { "uint64", TDESC_TYPE_UINT64 },
974 { "uint128", TDESC_TYPE_UINT128 },
975 { "code_ptr", TDESC_TYPE_CODE_PTR },
976 { "data_ptr", TDESC_TYPE_DATA_PTR },
977 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
978 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
979 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
980 { "i387_ext", TDESC_TYPE_I387_EXT }
983 /* Lookup a predefined type. */
985 static struct tdesc_type *
986 tdesc_predefined_type (enum tdesc_type_kind kind)
988 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
989 if (tdesc_predefined_types[ix].kind == kind)
990 return &tdesc_predefined_types[ix];
992 gdb_assert_not_reached ("bad predefined tdesc type");
995 /* See arch/tdesc.h. */
998 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
1000 /* First try target-defined types. */
1001 for (const tdesc_type_up &type : feature->types)
1002 if (type->name == id)
1005 /* Next try the predefined types. */
1006 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
1007 if (tdesc_predefined_types[ix].name == id)
1008 return &tdesc_predefined_types[ix];
1013 /* Lookup type associated with ID. */
1016 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
1018 tdesc_arch_data *data
1019 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1021 for (const tdesc_arch_reg ® : data->arch_regs)
1024 && reg.reg->tdesc_type
1026 && reg.reg->tdesc_type->name == id)
1033 /* Support for registers from target descriptions. */
1035 /* Construct the per-gdbarch data. */
1038 tdesc_data_init (struct obstack *obstack)
1040 struct tdesc_arch_data *data;
1042 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1043 new (data) tdesc_arch_data ();
1048 /* Similar, but for the temporary copy used during architecture
1051 struct tdesc_arch_data *
1052 tdesc_data_alloc (void)
1054 return new tdesc_arch_data ();
1057 /* Free something allocated by tdesc_data_alloc, if it is not going
1058 to be used (for instance if it was unsuitable for the
1062 tdesc_data_cleanup (void *data_untyped)
1064 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1069 /* Search FEATURE for a register named NAME. */
1071 static struct tdesc_reg *
1072 tdesc_find_register_early (const struct tdesc_feature *feature,
1075 for (const tdesc_reg_up ® : feature->registers)
1076 if (strcasecmp (reg->name.c_str (), name) == 0)
1082 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1085 tdesc_numbered_register (const struct tdesc_feature *feature,
1086 struct tdesc_arch_data *data,
1087 int regno, const char *name)
1089 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1094 /* Make sure the vector includes a REGNO'th element. */
1095 while (regno >= data->arch_regs.size ())
1096 data->arch_regs.emplace_back (nullptr, nullptr);
1098 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
1103 /* Search FEATURE for a register named NAME, but do not assign a fixed
1104 register number to it. */
1107 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1110 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1118 /* Search FEATURE for a register whose name is in NAMES and assign
1122 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1123 struct tdesc_arch_data *data,
1124 int regno, const char *const names[])
1128 for (i = 0; names[i] != NULL; i++)
1129 if (tdesc_numbered_register (feature, data, regno, names[i]))
1135 /* Search FEATURE for a register named NAME, and return its size in
1136 bits. The register must exist. */
1139 tdesc_register_size (const struct tdesc_feature *feature,
1142 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1144 gdb_assert (reg != NULL);
1145 return reg->bitsize;
1148 /* Look up a register by its GDB internal register number. */
1150 static struct tdesc_arch_reg *
1151 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1153 struct tdesc_arch_data *data;
1155 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1156 if (regno < data->arch_regs.size ())
1157 return &data->arch_regs[regno];
1162 static struct tdesc_reg *
1163 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1165 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1167 return reg? reg->reg : NULL;
1170 /* Return the name of register REGNO, from the target description or
1171 from an architecture-provided pseudo_register_name method. */
1174 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1176 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1177 int num_regs = gdbarch_num_regs (gdbarch);
1178 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1181 return reg->name.c_str ();
1183 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1185 struct tdesc_arch_data *data
1186 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1188 gdb_assert (data->pseudo_register_name != NULL);
1189 return data->pseudo_register_name (gdbarch, regno);
1196 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1198 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1199 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1200 int num_regs = gdbarch_num_regs (gdbarch);
1201 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1203 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1205 struct tdesc_arch_data *data
1206 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1208 gdb_assert (data->pseudo_register_type != NULL);
1209 return data->pseudo_register_type (gdbarch, regno);
1213 /* Return "int0_t", since "void" has a misleading size of one. */
1214 return builtin_type (gdbarch)->builtin_int0;
1216 if (arch_reg->type == NULL)
1218 /* First check for a predefined or target defined type. */
1219 if (reg->tdesc_type)
1220 arch_reg->type = reg->tdesc_type->make_gdb_type (gdbarch);
1222 /* Next try size-sensitive type shortcuts. */
1223 else if (reg->type == "float")
1225 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1226 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1227 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1228 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1229 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1230 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
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_double;
1238 else if (reg->type == "int")
1240 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1241 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1242 else if (reg->bitsize == TARGET_CHAR_BIT)
1243 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1244 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1245 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1246 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1247 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1248 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1249 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1250 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1251 /* A bit desperate by this point... */
1252 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1255 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1256 reg->name.c_str (), reg->bitsize);
1257 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1261 if (arch_reg->type == NULL)
1262 internal_error (__FILE__, __LINE__,
1263 "Register \"%s\" has an unknown type \"%s\"",
1264 reg->name.c_str (), reg->type.c_str ());
1267 return arch_reg->type;
1271 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1273 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1276 return reg->target_regnum;
1281 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1282 target description may be classified as general, float, or vector.
1283 Unlike a gdbarch register_reggroup_p method, this function will
1284 return -1 if it does not know; the caller should handle registers
1285 with no specified group.
1287 Arbitrary strings (other than "general", "float", and "vector")
1288 from the description are not used; they cause the register to be
1289 displayed in "info all-registers" but excluded from "info
1290 registers" et al. The names of containing features are also not
1291 used. This might be extended to display registers in some more
1294 The save-restore flag is also implemented here. */
1297 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1298 struct reggroup *reggroup)
1300 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1302 if (reg != NULL && !reg->group.empty ())
1304 int general_p = 0, float_p = 0, vector_p = 0;
1306 if (reg->group == "general")
1308 else if (reg->group == "float")
1310 else if (reg->group == "vector")
1313 if (reggroup == float_reggroup)
1316 if (reggroup == vector_reggroup)
1319 if (reggroup == general_reggroup)
1324 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1325 return reg->save_restore;
1330 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1331 group specified go to the default reggroup function and are handled
1335 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1336 struct reggroup *reggroup)
1338 int num_regs = gdbarch_num_regs (gdbarch);
1339 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1342 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1344 struct tdesc_arch_data *data
1345 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1347 if (data->pseudo_register_reggroup_p != NULL)
1348 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1349 /* Otherwise fall through to the default reggroup_p. */
1352 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1356 return default_register_reggroup_p (gdbarch, regno, reggroup);
1359 /* Record architecture-specific functions to call for pseudo-register
1363 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1364 gdbarch_register_name_ftype *pseudo_name)
1366 struct tdesc_arch_data *data
1367 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1369 data->pseudo_register_name = pseudo_name;
1373 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1374 gdbarch_register_type_ftype *pseudo_type)
1376 struct tdesc_arch_data *data
1377 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1379 data->pseudo_register_type = pseudo_type;
1383 set_tdesc_pseudo_register_reggroup_p
1384 (struct gdbarch *gdbarch,
1385 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1387 struct tdesc_arch_data *data
1388 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1390 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1393 /* Update GDBARCH to use the target description for registers. */
1396 tdesc_use_registers (struct gdbarch *gdbarch,
1397 const struct target_desc *target_desc,
1398 struct tdesc_arch_data *early_data)
1400 int num_regs = gdbarch_num_regs (gdbarch);
1401 struct tdesc_arch_data *data;
1404 /* We can't use the description for registers if it doesn't describe
1405 any. This function should only be called after validating
1406 registers, so the caller should know that registers are
1408 gdb_assert (tdesc_has_registers (target_desc));
1410 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1411 data->arch_regs = early_data->arch_regs;
1414 /* Build up a set of all registers, so that we can assign register
1415 numbers where needed. The hash table expands as necessary, so
1416 the initial size is arbitrary. */
1417 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1418 for (const tdesc_feature_up &feature : target_desc->features)
1419 for (const tdesc_reg_up ® : feature->registers)
1421 void **slot = htab_find_slot (reg_hash, reg.get (), INSERT);
1426 /* Remove any registers which were assigned numbers by the
1428 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1429 if (arch_reg.reg != NULL)
1430 htab_remove_elt (reg_hash, arch_reg.reg);
1432 /* Assign numbers to the remaining registers and add them to the
1433 list of registers. The new numbers are always above gdbarch_num_regs.
1434 Iterate over the features, not the hash table, so that the order
1435 matches that in the target description. */
1437 gdb_assert (data->arch_regs.size () <= num_regs);
1438 while (data->arch_regs.size () < num_regs)
1439 data->arch_regs.emplace_back (nullptr, nullptr);
1441 for (const tdesc_feature_up &feature : target_desc->features)
1442 for (const tdesc_reg_up ® : feature->registers)
1443 if (htab_find (reg_hash, reg.get ()) != NULL)
1445 data->arch_regs.emplace_back (reg.get (), nullptr);
1449 htab_delete (reg_hash);
1451 /* Update the architecture. */
1452 set_gdbarch_num_regs (gdbarch, num_regs);
1453 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1454 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1455 set_gdbarch_remote_register_number (gdbarch,
1456 tdesc_remote_register_number);
1457 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1461 /* See arch/tdesc.h. */
1464 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1465 int regnum, int save_restore, const char *group,
1466 int bitsize, const char *type)
1468 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1469 group, bitsize, type);
1471 feature->registers.emplace_back (reg);
1474 /* See arch/tdesc.h. */
1477 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1478 struct tdesc_type *field_type, int count)
1480 tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
1481 feature->types.emplace_back (type);
1486 /* See arch/tdesc.h. */
1488 tdesc_type_with_fields *
1489 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1491 tdesc_type_with_fields *type
1492 = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
1493 feature->types.emplace_back (type);
1498 /* See arch/tdesc.h. */
1501 tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
1503 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1504 gdb_assert (size > 0);
1508 /* See arch/tdesc.h. */
1510 tdesc_type_with_fields *
1511 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1513 tdesc_type_with_fields *type
1514 = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
1515 feature->types.emplace_back (type);
1520 /* See arch/tdesc.h. */
1522 tdesc_type_with_fields *
1523 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1526 gdb_assert (size > 0);
1528 tdesc_type_with_fields *type
1529 = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
1530 feature->types.emplace_back (type);
1535 tdesc_type_with_fields *
1536 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1539 gdb_assert (size > 0);
1541 tdesc_type_with_fields *type
1542 = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
1543 feature->types.emplace_back (type);
1548 /* See arch/tdesc.h. */
1551 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
1552 struct tdesc_type *field_type)
1554 gdb_assert (type->kind == TDESC_TYPE_UNION
1555 || type->kind == TDESC_TYPE_STRUCT);
1557 /* Initialize start and end so we know this is not a bit-field
1558 when we print-c-tdesc. */
1559 type->fields.emplace_back (field_name, field_type, -1, -1);
1563 tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
1564 int start, int end, struct tdesc_type *field_type)
1566 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1567 || type->kind == TDESC_TYPE_FLAGS);
1568 gdb_assert (start >= 0 && end >= start);
1570 type->fields.emplace_back (field_name, field_type, start, end);
1573 /* See arch/tdesc.h. */
1576 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
1579 struct tdesc_type *field_type;
1581 gdb_assert (start >= 0 && end >= start);
1584 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1586 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1588 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1591 /* See arch/tdesc.h. */
1594 tdesc_add_flag (tdesc_type_with_fields *type, int start,
1595 const char *flag_name)
1597 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1598 || type->kind == TDESC_TYPE_STRUCT);
1600 type->fields.emplace_back (flag_name,
1601 tdesc_predefined_type (TDESC_TYPE_BOOL),
1606 tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
1609 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1610 type->fields.emplace_back (name,
1611 tdesc_predefined_type (TDESC_TYPE_INT32),
1615 /* See arch/tdesc.h. */
1617 struct tdesc_feature *
1618 tdesc_create_feature (struct target_desc *tdesc, const char *name,
1621 struct tdesc_feature *new_feature = new tdesc_feature (name);
1623 tdesc->features.emplace_back (new_feature);
1628 struct target_desc *
1629 allocate_target_description (void)
1631 return new target_desc ();
1635 free_target_description (void *arg)
1637 struct target_desc *target_desc = (struct target_desc *) arg;
1643 make_cleanup_free_target_description (struct target_desc *target_desc)
1645 return make_cleanup (free_target_description, target_desc);
1649 tdesc_add_compatible (struct target_desc *target_desc,
1650 const struct bfd_arch_info *compatible)
1652 /* If this instance of GDB is compiled without BFD support for the
1653 compatible architecture, simply ignore it -- we would not be able
1654 to handle it anyway. */
1655 if (compatible == NULL)
1658 for (const bfd_arch_info *compat : target_desc->compatible)
1659 if (compat == compatible)
1660 internal_error (__FILE__, __LINE__,
1661 _("Attempted to add duplicate "
1662 "compatible architecture \"%s\""),
1663 compatible->printable_name);
1665 target_desc->compatible.push_back (compatible);
1669 set_tdesc_property (struct target_desc *target_desc,
1670 const char *key, const char *value)
1672 gdb_assert (key != NULL && value != NULL);
1674 if (tdesc_property (target_desc, key) != NULL)
1675 internal_error (__FILE__, __LINE__,
1676 _("Attempted to add duplicate property \"%s\""), key);
1678 target_desc->properties.emplace_back (key, value);
1681 /* See arch/tdesc.h. */
1684 set_tdesc_architecture (struct target_desc *target_desc,
1687 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1691 set_tdesc_architecture (struct target_desc *target_desc,
1692 const struct bfd_arch_info *arch)
1694 target_desc->arch = arch;
1697 /* See arch/tdesc.h. */
1700 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1702 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1706 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1708 target_desc->osabi = osabi;
1712 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1713 static struct cmd_list_element *tdesc_unset_cmdlist;
1715 /* Helper functions for the CLI commands. */
1718 set_tdesc_cmd (const char *args, int from_tty)
1720 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1724 show_tdesc_cmd (const char *args, int from_tty)
1726 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1730 unset_tdesc_cmd (const char *args, int from_tty)
1732 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1736 set_tdesc_filename_cmd (const char *args, int from_tty,
1737 struct cmd_list_element *c)
1739 xfree (target_description_filename);
1740 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1742 target_clear_description ();
1743 target_find_description ();
1747 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1748 struct cmd_list_element *c,
1751 value = target_description_filename;
1753 if (value != NULL && *value != '\0')
1754 printf_filtered (_("The target description will be read from \"%s\".\n"),
1757 printf_filtered (_("The target description will be "
1758 "read from the target.\n"));
1762 unset_tdesc_filename_cmd (const char *args, int from_tty)
1764 xfree (target_description_filename);
1765 target_description_filename = NULL;
1766 target_clear_description ();
1767 target_find_description ();
1770 /* Print target description in C. */
1772 class print_c_tdesc : public tdesc_element_visitor
1775 print_c_tdesc (std::string &filename_after_features)
1776 : m_filename_after_features (filename_after_features)
1780 const char *filename = lbasename (m_filename_after_features.c_str ());
1782 m_function = (char *) xmalloc (strlen (filename) + 1);
1783 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1786 else if (*inp == '-')
1792 /* Standard boilerplate. */
1793 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1794 "-*- buffer-read-only: t -*- vi"
1803 void visit_pre (const target_desc *e) override
1805 printf_unfiltered (" Original: %s */\n\n",
1806 lbasename (m_filename_after_features.c_str ()));
1808 printf_unfiltered ("#include \"defs.h\"\n");
1809 printf_unfiltered ("#include \"osabi.h\"\n");
1810 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1811 printf_unfiltered ("\n");
1813 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1814 printf_unfiltered ("static void\n");
1815 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1816 printf_unfiltered ("{\n");
1818 (" struct target_desc *result = allocate_target_description ();\n");
1820 if (tdesc_architecture (e) != NULL)
1823 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1824 tdesc_architecture (e)->printable_name);
1825 printf_unfiltered ("\n");
1827 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1828 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1831 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1832 gdbarch_osabi_name (tdesc_osabi (e)));
1833 printf_unfiltered ("\n");
1836 for (const bfd_arch_info_type *compatible : e->compatible)
1838 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1839 compatible->printable_name);
1841 if (!e->compatible.empty ())
1842 printf_unfiltered ("\n");
1844 for (const property &prop : e->properties)
1845 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1846 prop.key.c_str (), prop.value.c_str ());
1848 printf_unfiltered (" struct tdesc_feature *feature;\n");
1851 void visit_pre (const tdesc_feature *e) override
1853 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1857 void visit_post (const tdesc_feature *e) override
1860 void visit_post (const target_desc *e) override
1862 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1863 printf_unfiltered ("}\n");
1866 void visit (const tdesc_type_builtin *type) override
1868 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1871 void visit (const tdesc_type_vector *type) override
1873 if (!m_printed_element_type)
1875 printf_unfiltered (" tdesc_type *element_type;\n");
1876 m_printed_element_type = true;
1880 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1881 type->element_type->name.c_str ());
1883 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1884 type->name.c_str (), type->count);
1886 printf_unfiltered ("\n");
1889 void visit (const tdesc_type_with_fields *type) override
1891 if (!m_printed_type_with_fields)
1893 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1894 m_printed_type_with_fields = true;
1899 case TDESC_TYPE_STRUCT:
1900 case TDESC_TYPE_FLAGS:
1901 if (type->kind == TDESC_TYPE_STRUCT)
1904 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1905 type->name.c_str ());
1906 if (type->size != 0)
1908 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
1913 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1914 type->name.c_str (), type->size);
1916 for (const tdesc_type_field &f : type->fields)
1918 const char *type_name;
1920 gdb_assert (f.type != NULL);
1921 type_name = f.type->name.c_str ();
1923 /* To minimize changes to generated files, don't emit type
1924 info for fields that have defaulted types. */
1927 gdb_assert (f.end != -1);
1928 if (f.type->kind == TDESC_TYPE_BOOL)
1930 gdb_assert (f.start == f.end);
1932 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1933 f.start, f.name.c_str ());
1935 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1937 && f.type->kind == TDESC_TYPE_UINT64))
1940 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1941 f.name.c_str (), f.start, f.end);
1945 printf_field_type_assignment
1946 ("tdesc_named_type (feature, \"%s\");\n",
1949 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1950 " %d, %d, field_type);\n",
1951 f.name.c_str (), f.start, f.end);
1954 else /* Not a bitfield. */
1956 gdb_assert (f.end == -1);
1957 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1958 printf_field_type_assignment
1959 ("tdesc_named_type (feature, \"%s\");\n", type_name);
1961 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1966 case TDESC_TYPE_UNION:
1968 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1969 type->name.c_str ());
1970 for (const tdesc_type_field &f : type->fields)
1972 printf_field_type_assignment
1973 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
1975 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1979 case TDESC_TYPE_ENUM:
1981 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1982 type->name.c_str (), type->size);
1983 for (const tdesc_type_field &f : type->fields)
1985 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1986 f.start, f.name.c_str ());
1989 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1992 printf_unfiltered ("\n");
1995 void visit (const tdesc_reg *reg) override
1997 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1998 reg->name.c_str (), reg->target_regnum,
2000 if (!reg->group.empty ())
2001 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
2003 printf_unfiltered ("NULL, ");
2004 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
2008 std::string m_filename_after_features;
2012 /* Print an assignment to the field_type variable. Print the declaration
2013 of field_type if that has not been done yet. */
2014 ATTRIBUTE_PRINTF (2, 3)
2015 void printf_field_type_assignment (const char *fmt, ...)
2017 if (!m_printed_field_type)
2019 printf_unfiltered (" tdesc_type *field_type;\n");
2020 m_printed_field_type = true;
2023 printf_unfiltered (" field_type = ");
2026 va_start (args, fmt);
2027 vprintf_unfiltered (fmt, args);
2033 /* Did we print "struct tdesc_type *element_type;" yet? */
2034 bool m_printed_element_type = false;
2036 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
2037 bool m_printed_type_with_fields = false;
2039 /* Did we print "struct tdesc_type *field_type;" yet? */
2040 bool m_printed_field_type = false;
2043 /* Print target description feature in C. */
2045 class print_c_feature : public print_c_tdesc
2048 print_c_feature (std::string &file)
2049 : print_c_tdesc (file)
2052 auto const pos = m_filename_after_features.find_last_of ('.');
2054 m_filename_after_features = m_filename_after_features.substr (0, pos);
2057 void visit_pre (const target_desc *e) override
2059 printf_unfiltered (" Original: %s */\n\n",
2060 lbasename (m_filename_after_features.c_str ()));
2062 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2063 printf_unfiltered ("\n");
2066 void visit_post (const target_desc *e) override
2069 void visit_pre (const tdesc_feature *e) override
2071 std::string name (m_filename_after_features);
2073 auto pos = name.find_first_of ('.');
2075 name = name.substr (0, pos);
2076 std::replace (name.begin (), name.end (), '/', '_');
2077 std::replace (name.begin (), name.end (), '-', '_');
2079 printf_unfiltered ("static int\n");
2080 printf_unfiltered ("create_feature_%s ", name.c_str ());
2081 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2083 printf_unfiltered ("{\n");
2084 printf_unfiltered (" struct tdesc_feature *feature;\n");
2087 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2088 e->name.c_str (), lbasename (m_filename_after_features.c_str ()));
2091 void visit_post (const tdesc_feature *e) override
2093 printf_unfiltered (" return regnum;\n");
2094 printf_unfiltered ("}\n");
2097 void visit (const tdesc_reg *reg) override
2099 /* Most "reg" in XML target descriptions don't have "regnum"
2100 attribute, so the register number is allocated sequentially.
2101 In case that reg has "regnum" attribute, register number
2102 should be set by that explicitly. */
2104 if (reg->target_regnum < m_next_regnum)
2106 /* The integrity check, it can catch some errors on register
2107 number collision, like this,
2109 <reg name="x0" bitsize="32"/>
2110 <reg name="x1" bitsize="32"/>
2111 <reg name="x2" bitsize="32"/>
2112 <reg name="x3" bitsize="32"/>
2113 <reg name="ps" bitsize="32" regnum="3"/>
2115 but it also has false negatives. The target description
2118 <reg name="x1" bitsize="32" regnum="1"/>
2119 <reg name="x3" bitsize="32" regnum="3"/>
2120 <reg name="x2" bitsize="32" regnum="2"/>
2121 <reg name="x4" bitsize="32" regnum="4"/>
2123 but it is not a good practice, so still error on this,
2124 and also print the message so that it can be saved in the
2125 generated c file. */
2127 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2128 reg->target_regnum);
2129 printf_unfiltered ("is not the largest number (%d).\n",
2131 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2132 reg->target_regnum, m_next_regnum);
2135 if (reg->target_regnum > m_next_regnum)
2137 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2138 m_next_regnum = reg->target_regnum;
2141 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2142 reg->name.c_str (), reg->save_restore);
2143 if (!reg->group.empty ())
2144 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
2146 printf_unfiltered ("NULL, ");
2147 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
2153 /* The register number to use for the next register we see. */
2154 int m_next_regnum = 0;
2158 maint_print_c_tdesc_cmd (const char *args, int from_tty)
2160 const struct target_desc *tdesc;
2161 const char *filename;
2165 /* Use the global target-supplied description, not the current
2166 architecture's. This lets a GDB for one architecture generate C
2167 for another architecture's description, even though the gdbarch
2168 initialization code will reject the new description. */
2169 tdesc = current_target_desc;
2170 filename = target_description_filename;
2174 /* Use the target description from the XML file. */
2176 tdesc = file_read_description_xml (filename);
2180 error (_("There is no target description to print."));
2182 if (filename == NULL)
2183 error (_("The current target description did not come from an XML file."));
2185 std::string filename_after_features (filename);
2186 auto loc = filename_after_features.rfind ("/features/");
2188 if (loc != std::string::npos)
2189 filename_after_features = filename_after_features.substr (loc + 10);
2191 /* Print c files for target features instead of target descriptions,
2192 because c files got from target features are more flexible than the
2194 if (startswith (filename_after_features.c_str (), "i386/32bit-")
2195 || startswith (filename_after_features.c_str (), "i386/64bit-")
2196 || startswith (filename_after_features.c_str (), "i386/x32-core.xml")
2197 || startswith (filename_after_features.c_str (), "tic6x-")
2198 || startswith (filename_after_features.c_str (), "aarch64"))
2200 print_c_feature v (filename_after_features);
2206 print_c_tdesc v (filename_after_features);
2212 namespace selftests {
2214 static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2218 /* See target-descritpions.h. */
2221 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2223 xml_tdesc.emplace_back (xml_file, tdesc);
2229 /* Check that the target descriptions created dynamically by
2230 architecture-specific code equal the descriptions created from XML files
2231 found in the specified directory DIR. */
2234 maintenance_check_xml_descriptions (const char *dir, int from_tty)
2237 error (_("Missing dir name"));
2239 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2240 std::string feature_dir (dir1.get ());
2241 unsigned int failed = 0;
2243 for (auto const &e : selftests::xml_tdesc)
2245 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2246 const target_desc *tdesc
2247 = file_read_description_xml (tdesc_xml.data ());
2249 if (tdesc == NULL || *tdesc != *e.second)
2252 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2253 (long) selftests::xml_tdesc.size (), failed);
2257 _initialize_target_descriptions (void)
2259 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2261 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2262 Set target description specific variables."),
2263 &tdesc_set_cmdlist, "set tdesc ",
2264 0 /* allow-unknown */, &setlist);
2265 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2266 Show target description specific variables."),
2267 &tdesc_show_cmdlist, "show tdesc ",
2268 0 /* allow-unknown */, &showlist);
2269 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2270 Unset target description specific variables."),
2271 &tdesc_unset_cmdlist, "unset tdesc ",
2272 0 /* allow-unknown */, &unsetlist);
2274 add_setshow_filename_cmd ("filename", class_obscure,
2275 &tdesc_filename_cmd_string,
2277 Set the file to read for an XML target description"), _("\
2278 Show the file to read for an XML target description"), _("\
2279 When set, GDB will read the target description from a local\n\
2280 file instead of querying the remote target."),
2281 set_tdesc_filename_cmd,
2282 show_tdesc_filename_cmd,
2283 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2285 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2286 Unset the file to read for an XML target description. When unset,\n\
2287 GDB will read the description from the target."),
2288 &tdesc_unset_cmdlist);
2290 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2291 Print the current target description as a C source file."),
2292 &maintenanceprintlist);
2294 cmd_list_element *cmd;
2296 cmd = add_cmd ("xml-descriptions", class_maintenance,
2297 maintenance_check_xml_descriptions, _("\
2298 Check the target descriptions created in GDB equal the descriptions\n\
2299 created from XML files in the directory.\n\
2300 The parameter is the directory name."),
2301 &maintenancechecklist);
2302 set_cmd_completer (cmd, filename_completer);