1 /* Target description support for GDB.
3 Copyright (C) 2006-2018 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 static type *make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype);
43 /* The interface to visit different elements of target description. */
45 class tdesc_element_visitor
48 virtual void visit_pre (const target_desc *e)
51 virtual void visit_post (const target_desc *e)
54 virtual void visit_pre (const tdesc_feature *e)
57 virtual void visit_post (const tdesc_feature *e)
60 virtual void visit (const tdesc_type_builtin *e)
63 virtual void visit (const tdesc_type_vector *e)
66 virtual void visit (const tdesc_type_with_fields *e)
69 virtual void visit (const tdesc_reg *e)
76 virtual void accept (tdesc_element_visitor &v) const = 0;
83 property (const std::string &key_, const std::string &value_)
84 : key (key_), value (value_)
91 /* An individual register from a target description. */
93 struct tdesc_reg : tdesc_element
95 tdesc_reg (struct tdesc_feature *feature, const std::string &name_,
96 int regnum, int save_restore_, const char *group_,
97 int bitsize_, const char *type_)
98 : name (name_), target_regnum (regnum),
99 save_restore (save_restore_),
100 group (group_ != NULL ? group_ : ""),
102 type (type_ != NULL ? type_ : "<unknown>")
104 /* If the register's type is target-defined, look it up now. We may not
105 have easy access to the containing feature when we want it later. */
106 tdesc_type = tdesc_named_type (feature, type.c_str ());
109 virtual ~tdesc_reg () = default;
111 DISABLE_COPY_AND_ASSIGN (tdesc_reg);
113 /* The name of this register. In standard features, it may be
114 recognized by the architecture support code, or it may be purely
118 /* The register number used by this target to refer to this
119 register. This is used for remote p/P packets and to determine
120 the ordering of registers in the remote g/G packets. */
123 /* If this flag is set, GDB should save and restore this register
124 around calls to an inferior function. */
127 /* The name of the register group containing this register, or empty
128 if the group should be automatically determined from the register's
129 type. This is traditionally "general", "float", "vector" but can
130 also be an arbitrary string. If defined the corresponding "info"
131 command should display this register's value. The string should be
132 limited to alphanumeric characters and internal hyphens. */
135 /* The size of the register, in bits. */
138 /* The type of the register. This string corresponds to either
139 a named type from the target description or a predefined
143 /* The target-described type corresponding to TYPE, if found. */
144 struct tdesc_type *tdesc_type;
146 void accept (tdesc_element_visitor &v) const override
151 bool operator== (const tdesc_reg &other) const
153 return (name == other.name
154 && target_regnum == other.target_regnum
155 && save_restore == other.save_restore
156 && bitsize == other.bitsize
157 && group == other.group
158 && type == other.type);
161 bool operator!= (const tdesc_reg &other) const
163 return !(*this == other);
167 typedef std::unique_ptr<tdesc_reg> tdesc_reg_up;
169 /* A named type from a target description. */
171 struct tdesc_type_field
173 tdesc_type_field (const std::string &name_, tdesc_type *type_,
174 int start_, int end_)
175 : name (name_), type (type_), start (start_), end (end_)
179 struct tdesc_type *type;
180 /* For non-enum-values, either both are -1 (non-bitfield), or both are
181 not -1 (bitfield). For enum values, start is the value (which could be
188 /* Predefined types. */
202 TDESC_TYPE_IEEE_SINGLE,
203 TDESC_TYPE_IEEE_DOUBLE,
204 TDESC_TYPE_ARM_FPA_EXT,
207 /* Types defined by a target feature. */
215 struct tdesc_type : tdesc_element
217 tdesc_type (const std::string &name_, enum tdesc_type_kind kind_)
218 : name (name_), kind (kind_)
221 virtual ~tdesc_type () = default;
223 DISABLE_COPY_AND_ASSIGN (tdesc_type);
225 /* The name of this type. */
228 /* Identify the kind of this type. */
229 enum tdesc_type_kind kind;
231 bool operator== (const tdesc_type &other) const
233 return name == other.name && kind == other.kind;
236 bool operator!= (const tdesc_type &other) const
238 return !(*this == other);
242 typedef std::unique_ptr<tdesc_type> tdesc_type_up;
244 struct tdesc_type_builtin : tdesc_type
246 tdesc_type_builtin (const std::string &name, enum tdesc_type_kind kind)
247 : tdesc_type (name, kind)
250 void accept (tdesc_element_visitor &v) const override
256 /* tdesc_type for vector types. */
258 struct tdesc_type_vector : tdesc_type
260 tdesc_type_vector (const std::string &name, tdesc_type *element_type_, int count_)
261 : tdesc_type (name, TDESC_TYPE_VECTOR),
262 element_type (element_type_), count (count_)
265 void accept (tdesc_element_visitor &v) const override
270 struct tdesc_type *element_type;
274 /* tdesc_type for struct, union, flags, and enum types. */
276 struct tdesc_type_with_fields : tdesc_type
278 tdesc_type_with_fields (const std::string &name, tdesc_type_kind kind,
280 : tdesc_type (name, kind), size (size_)
283 void accept (tdesc_element_visitor &v) const override
288 std::vector<tdesc_type_field> fields;
292 /* Convert a tdesc_type to a gdb type. */
295 make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
297 class gdb_type_creator : public tdesc_element_visitor
300 gdb_type_creator (struct gdbarch *gdbarch)
301 : m_gdbarch (gdbarch)
309 void visit (const tdesc_type_builtin *e) override
313 /* Predefined types. */
314 case TDESC_TYPE_BOOL:
315 m_type = builtin_type (m_gdbarch)->builtin_bool;
317 case TDESC_TYPE_INT8:
318 m_type = builtin_type (m_gdbarch)->builtin_int8;
320 case TDESC_TYPE_INT16:
321 m_type = builtin_type (m_gdbarch)->builtin_int16;
323 case TDESC_TYPE_INT32:
324 m_type = builtin_type (m_gdbarch)->builtin_int32;
326 case TDESC_TYPE_INT64:
327 m_type = builtin_type (m_gdbarch)->builtin_int64;
329 case TDESC_TYPE_INT128:
330 m_type = builtin_type (m_gdbarch)->builtin_int128;
332 case TDESC_TYPE_UINT8:
333 m_type = builtin_type (m_gdbarch)->builtin_uint8;
335 case TDESC_TYPE_UINT16:
336 m_type = builtin_type (m_gdbarch)->builtin_uint16;
338 case TDESC_TYPE_UINT32:
339 m_type = builtin_type (m_gdbarch)->builtin_uint32;
341 case TDESC_TYPE_UINT64:
342 m_type = builtin_type (m_gdbarch)->builtin_uint64;
344 case TDESC_TYPE_UINT128:
345 m_type = builtin_type (m_gdbarch)->builtin_uint128;
347 case TDESC_TYPE_CODE_PTR:
348 m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
350 case TDESC_TYPE_DATA_PTR:
351 m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
355 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
361 case TDESC_TYPE_IEEE_SINGLE:
362 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single",
363 floatformats_ieee_single);
366 case TDESC_TYPE_IEEE_DOUBLE:
367 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double",
368 floatformats_ieee_double);
370 case TDESC_TYPE_ARM_FPA_EXT:
371 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext",
372 floatformats_arm_ext);
375 case TDESC_TYPE_I387_EXT:
376 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext",
377 floatformats_i387_ext);
381 internal_error (__FILE__, __LINE__,
382 "Type \"%s\" has an unknown kind %d",
383 e->name.c_str (), e->kind);
386 void visit (const tdesc_type_vector *e) override
388 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
392 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
393 m_type = init_vector_type (element_gdb_type, e->count);
394 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
398 void visit (const tdesc_type_with_fields *e) override
400 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
406 case TDESC_TYPE_STRUCT:
407 make_gdb_type_struct (e);
409 case TDESC_TYPE_UNION:
410 make_gdb_type_union (e);
412 case TDESC_TYPE_FLAGS:
413 make_gdb_type_flags (e);
415 case TDESC_TYPE_ENUM:
416 make_gdb_type_enum (e);
420 internal_error (__FILE__, __LINE__,
421 "Type \"%s\" has an unknown kind %d",
422 e->name.c_str (), e->kind);
427 void make_gdb_type_struct (const tdesc_type_with_fields *e)
429 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
430 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
431 TYPE_TAG_NAME (m_type) = TYPE_NAME (m_type);
433 for (const tdesc_type_field &f : e->fields)
435 if (f.start != -1 && f.end != -1)
439 struct type *field_gdb_type;
440 int bitsize, total_size;
442 /* This invariant should be preserved while creating types. */
443 gdb_assert (e->size != 0);
445 field_gdb_type = make_gdb_type (m_gdbarch, f.type);
446 else if (e->size > 4)
447 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
449 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
451 fld = append_composite_type_field_raw
452 (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
454 /* For little-endian, BITPOS counts from the LSB of
455 the structure and marks the LSB of the field. For
456 big-endian, BITPOS counts from the MSB of the
457 structure and marks the MSB of the field. Either
458 way, it is the number of bits to the "left" of the
459 field. To calculate this in big-endian, we need
460 the total size of the structure. */
461 bitsize = f.end - f.start + 1;
462 total_size = e->size * TARGET_CHAR_BIT;
463 if (gdbarch_bits_big_endian (m_gdbarch))
464 SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
466 SET_FIELD_BITPOS (fld[0], f.start);
467 FIELD_BITSIZE (fld[0]) = bitsize;
471 gdb_assert (f.start == -1 && f.end == -1);
472 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
473 append_composite_type_field (m_type,
474 xstrdup (f.name.c_str ()),
480 TYPE_LENGTH (m_type) = e->size;
483 void make_gdb_type_union (const tdesc_type_with_fields *e)
485 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
486 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
488 for (const tdesc_type_field &f : e->fields)
490 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
491 append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
494 /* If any of the children of a union are vectors, flag the
495 union as a vector also. This allows e.g. a union of two
496 vector types to show up automatically in "info vector". */
497 if (TYPE_VECTOR (field_gdb_type))
498 TYPE_VECTOR (m_type) = 1;
502 void make_gdb_type_flags (const tdesc_type_with_fields *e)
504 m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
505 e->size * TARGET_CHAR_BIT);
507 for (const tdesc_type_field &f : e->fields)
509 int bitsize = f.end - f.start + 1;
511 gdb_assert (f.type != NULL);
512 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
513 append_flags_type_field (m_type, f.start, bitsize,
514 field_gdb_type, f.name.c_str ());
518 void make_gdb_type_enum (const tdesc_type_with_fields *e)
520 m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
523 TYPE_UNSIGNED (m_type) = 1;
524 for (const tdesc_type_field &f : e->fields)
527 = append_composite_type_field_raw (m_type,
528 xstrdup (f.name.c_str ()),
531 SET_FIELD_BITPOS (fld[0], f.start);
535 /* The gdbarch used. */
536 struct gdbarch *m_gdbarch;
538 /* The type created. */
542 gdb_type_creator gdb_type (gdbarch);
543 ttype->accept (gdb_type);
544 return gdb_type.get_type ();
547 /* A feature from a target description. Each feature is a collection
548 of other elements, e.g. registers and types. */
550 struct tdesc_feature : tdesc_element
552 tdesc_feature (const std::string &name_)
556 virtual ~tdesc_feature () = default;
558 DISABLE_COPY_AND_ASSIGN (tdesc_feature);
560 /* The name of this feature. It may be recognized by the architecture
564 /* The registers associated with this feature. */
565 std::vector<tdesc_reg_up> registers;
567 /* The types associated with this feature. */
568 std::vector<tdesc_type_up> types;
570 void accept (tdesc_element_visitor &v) const override
574 for (const tdesc_type_up &type : types)
577 for (const tdesc_reg_up ® : registers)
583 bool operator== (const tdesc_feature &other) const
585 if (name != other.name)
588 if (registers.size () != other.registers.size ())
591 for (int ix = 0; ix < registers.size (); ix++)
593 const tdesc_reg_up ®1 = registers[ix];
594 const tdesc_reg_up ®2 = other.registers[ix];
596 if (reg1 != reg2 && *reg1 != *reg2)
600 if (types.size () != other.types.size ())
603 for (int ix = 0; ix < types.size (); ix++)
605 const tdesc_type_up &type1 = types[ix];
606 const tdesc_type_up &type2 = other.types[ix];
608 if (type1 != type2 && *type1 != *type2)
615 bool operator!= (const tdesc_feature &other) const
617 return !(*this == other);
621 typedef std::unique_ptr<tdesc_feature> tdesc_feature_up;
623 /* A target description. */
625 struct target_desc : tdesc_element
630 virtual ~target_desc () = default;
632 target_desc (const target_desc &) = delete;
633 void operator= (const target_desc &) = delete;
635 /* The architecture reported by the target, if any. */
636 const struct bfd_arch_info *arch = NULL;
638 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
640 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
642 /* The list of compatible architectures reported by the target. */
643 std::vector<const bfd_arch_info *> compatible;
645 /* Any architecture-specific properties specified by the target. */
646 std::vector<property> properties;
648 /* The features associated with this target. */
649 std::vector<tdesc_feature_up> features;
651 void accept (tdesc_element_visitor &v) const override
655 for (const tdesc_feature_up &feature : features)
661 bool operator== (const target_desc &other) const
663 if (arch != other.arch)
666 if (osabi != other.osabi)
669 if (features.size () != other.features.size ())
672 for (int ix = 0; ix < features.size (); ix++)
674 const tdesc_feature_up &feature1 = features[ix];
675 const tdesc_feature_up &feature2 = other.features[ix];
677 if (feature1 != feature2 && *feature1 != *feature2)
684 bool operator!= (const target_desc &other) const
686 return !(*this == other);
690 /* Per-architecture data associated with a target description. The
691 target description may be shared by multiple architectures, but
692 this data is private to one gdbarch. */
694 struct tdesc_arch_reg
696 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
697 : reg (reg_), type (type_)
700 struct tdesc_reg *reg;
704 struct tdesc_arch_data
706 /* A list of register/type pairs, indexed by GDB's internal register number.
707 During initialization of the gdbarch this list is used to store
708 registers which the architecture assigns a fixed register number.
709 Registers which are NULL in this array, or off the end, are
710 treated as zero-sized and nameless (i.e. placeholders in the
712 std::vector<tdesc_arch_reg> arch_regs;
714 /* Functions which report the register name, type, and reggroups for
716 gdbarch_register_name_ftype *pseudo_register_name = NULL;
717 gdbarch_register_type_ftype *pseudo_register_type = NULL;
718 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
721 /* Info about an inferior's target description. There's one of these
722 for each inferior. */
724 struct target_desc_info
726 /* A flag indicating that a description has already been fetched
727 from the target, so it should not be queried again. */
731 /* The description fetched from the target, or NULL if the target
732 did not supply any description. Only valid when
733 target_desc_fetched is set. Only the description initialization
734 code should access this; normally, the description should be
735 accessed through the gdbarch object. */
737 const struct target_desc *tdesc;
739 /* The filename to read a target description from, as set by "set
740 tdesc filename ..." */
745 /* Get the inferior INF's target description info, allocating one on
746 the stop if necessary. */
748 static struct target_desc_info *
749 get_tdesc_info (struct inferior *inf)
751 if (inf->tdesc_info == NULL)
752 inf->tdesc_info = XCNEW (struct target_desc_info);
753 return inf->tdesc_info;
756 /* A handle for architecture-specific data associated with the
757 target description (see struct tdesc_arch_data). */
759 static struct gdbarch_data *tdesc_data;
761 /* See target-descriptions.h. */
764 target_desc_info_from_user_p (struct target_desc_info *info)
766 return info != NULL && info->filename != NULL;
769 /* See target-descriptions.h. */
772 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
774 struct target_desc_info *src = get_tdesc_info (srcinf);
775 struct target_desc_info *dest = get_tdesc_info (destinf);
777 dest->fetched = src->fetched;
778 dest->tdesc = src->tdesc;
779 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
782 /* See target-descriptions.h. */
785 target_desc_info_free (struct target_desc_info *tdesc_info)
787 if (tdesc_info != NULL)
789 xfree (tdesc_info->filename);
794 /* Convenience helper macros. */
796 #define target_desc_fetched \
797 get_tdesc_info (current_inferior ())->fetched
798 #define current_target_desc \
799 get_tdesc_info (current_inferior ())->tdesc
800 #define target_description_filename \
801 get_tdesc_info (current_inferior ())->filename
803 /* The string manipulated by the "set tdesc filename ..." command. */
805 static char *tdesc_filename_cmd_string;
807 /* Fetch the current target's description, and switch the current
808 architecture to one which incorporates that description. */
811 target_find_description (void)
813 /* If we've already fetched a description from the target, don't do
814 it again. This allows a target to fetch the description early,
815 during its to_open or to_create_inferior, if it needs extra
816 information about the target to initialize. */
817 if (target_desc_fetched)
820 /* The current architecture should not have any target description
821 specified. It should have been cleared, e.g. when we
822 disconnected from the previous target. */
823 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
825 /* First try to fetch an XML description from the user-specified
827 current_target_desc = NULL;
828 if (target_description_filename != NULL
829 && *target_description_filename != '\0')
831 = file_read_description_xml (target_description_filename);
833 /* Next try to read the description from the current target using
835 if (current_target_desc == NULL)
836 current_target_desc = target_read_description_xml (¤t_target);
838 /* If that failed try a target-specific hook. */
839 if (current_target_desc == NULL)
840 current_target_desc = target_read_description (¤t_target);
842 /* If a non-NULL description was returned, then update the current
844 if (current_target_desc)
846 struct gdbarch_info info;
848 gdbarch_info_init (&info);
849 info.target_desc = current_target_desc;
850 if (!gdbarch_update_p (info))
851 warning (_("Architecture rejected target-supplied description"));
854 struct tdesc_arch_data *data;
856 data = ((struct tdesc_arch_data *)
857 gdbarch_data (target_gdbarch (), tdesc_data));
858 if (tdesc_has_registers (current_target_desc)
859 && data->arch_regs.empty ())
860 warning (_("Target-supplied registers are not supported "
861 "by the current architecture"));
865 /* Now that we know this description is usable, record that we
867 target_desc_fetched = 1;
870 /* Discard any description fetched from the current target, and switch
871 the current architecture to one with no target description. */
874 target_clear_description (void)
876 struct gdbarch_info info;
878 if (!target_desc_fetched)
881 target_desc_fetched = 0;
882 current_target_desc = NULL;
884 gdbarch_info_init (&info);
885 if (!gdbarch_update_p (info))
886 internal_error (__FILE__, __LINE__,
887 _("Could not remove target-supplied description"));
890 /* Return the global current target description. This should only be
891 used by gdbarch initialization code; most access should be through
892 an existing gdbarch. */
894 const struct target_desc *
895 target_current_description (void)
897 if (target_desc_fetched)
898 return current_target_desc;
903 /* Return non-zero if this target description is compatible
904 with the given BFD architecture. */
907 tdesc_compatible_p (const struct target_desc *target_desc,
908 const struct bfd_arch_info *arch)
910 for (const bfd_arch_info *compat : target_desc->compatible)
913 || arch->compatible (arch, compat)
914 || compat->compatible (compat, arch))
922 /* Direct accessors for target descriptions. */
924 /* Return the string value of a property named KEY, or NULL if the
925 property was not specified. */
928 tdesc_property (const struct target_desc *target_desc, const char *key)
930 for (const property &prop : target_desc->properties)
932 return prop.value.c_str ();
937 /* Return the BFD architecture associated with this target
938 description, or NULL if no architecture was specified. */
940 const struct bfd_arch_info *
941 tdesc_architecture (const struct target_desc *target_desc)
943 return target_desc->arch;
946 /* Return the OSABI associated with this target description, or
947 GDB_OSABI_UNKNOWN if no osabi was specified. */
950 tdesc_osabi (const struct target_desc *target_desc)
952 return target_desc->osabi;
957 /* Return 1 if this target description includes any registers. */
960 tdesc_has_registers (const struct target_desc *target_desc)
962 if (target_desc == NULL)
965 for (const tdesc_feature_up &feature : target_desc->features)
966 if (!feature->registers.empty ())
972 /* Return the feature with the given name, if present, or NULL if
973 the named feature is not found. */
975 const struct tdesc_feature *
976 tdesc_find_feature (const struct target_desc *target_desc,
979 for (const tdesc_feature_up &feature : target_desc->features)
980 if (feature->name == name)
981 return feature.get ();
986 /* Return the name of FEATURE. */
989 tdesc_feature_name (const struct tdesc_feature *feature)
991 return feature->name.c_str ();
994 /* Predefined types. */
995 static tdesc_type_builtin tdesc_predefined_types[] =
997 { "bool", TDESC_TYPE_BOOL },
998 { "int8", TDESC_TYPE_INT8 },
999 { "int16", TDESC_TYPE_INT16 },
1000 { "int32", TDESC_TYPE_INT32 },
1001 { "int64", TDESC_TYPE_INT64 },
1002 { "int128", TDESC_TYPE_INT128 },
1003 { "uint8", TDESC_TYPE_UINT8 },
1004 { "uint16", TDESC_TYPE_UINT16 },
1005 { "uint32", TDESC_TYPE_UINT32 },
1006 { "uint64", TDESC_TYPE_UINT64 },
1007 { "uint128", TDESC_TYPE_UINT128 },
1008 { "code_ptr", TDESC_TYPE_CODE_PTR },
1009 { "data_ptr", TDESC_TYPE_DATA_PTR },
1010 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
1011 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
1012 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
1013 { "i387_ext", TDESC_TYPE_I387_EXT }
1016 /* Lookup a predefined type. */
1018 static struct tdesc_type *
1019 tdesc_predefined_type (enum tdesc_type_kind kind)
1021 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
1022 if (tdesc_predefined_types[ix].kind == kind)
1023 return &tdesc_predefined_types[ix];
1025 gdb_assert_not_reached ("bad predefined tdesc type");
1028 /* See common/tdesc.h. */
1031 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
1033 /* First try target-defined types. */
1034 for (const tdesc_type_up &type : feature->types)
1035 if (type->name == id)
1038 /* Next try the predefined types. */
1039 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
1040 if (tdesc_predefined_types[ix].name == id)
1041 return &tdesc_predefined_types[ix];
1046 /* Lookup type associated with ID. */
1049 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
1051 tdesc_arch_data *data
1052 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1054 for (const tdesc_arch_reg ® : data->arch_regs)
1057 && reg.reg->tdesc_type
1059 && reg.reg->tdesc_type->name == id)
1066 /* Support for registers from target descriptions. */
1068 /* Construct the per-gdbarch data. */
1071 tdesc_data_init (struct obstack *obstack)
1073 struct tdesc_arch_data *data;
1075 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1076 new (data) tdesc_arch_data ();
1081 /* Similar, but for the temporary copy used during architecture
1084 struct tdesc_arch_data *
1085 tdesc_data_alloc (void)
1087 return new tdesc_arch_data ();
1090 /* Free something allocated by tdesc_data_alloc, if it is not going
1091 to be used (for instance if it was unsuitable for the
1095 tdesc_data_cleanup (void *data_untyped)
1097 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1102 /* Search FEATURE for a register named NAME. */
1104 static struct tdesc_reg *
1105 tdesc_find_register_early (const struct tdesc_feature *feature,
1108 for (const tdesc_reg_up ® : feature->registers)
1109 if (strcasecmp (reg->name.c_str (), name) == 0)
1115 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1118 tdesc_numbered_register (const struct tdesc_feature *feature,
1119 struct tdesc_arch_data *data,
1120 int regno, const char *name)
1122 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1127 /* Make sure the vector includes a REGNO'th element. */
1128 while (regno >= data->arch_regs.size ())
1129 data->arch_regs.emplace_back (nullptr, nullptr);
1131 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
1136 /* Search FEATURE for a register named NAME, but do not assign a fixed
1137 register number to it. */
1140 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1143 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1151 /* Search FEATURE for a register whose name is in NAMES and assign
1155 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1156 struct tdesc_arch_data *data,
1157 int regno, const char *const names[])
1161 for (i = 0; names[i] != NULL; i++)
1162 if (tdesc_numbered_register (feature, data, regno, names[i]))
1168 /* Search FEATURE for a register named NAME, and return its size in
1169 bits. The register must exist. */
1172 tdesc_register_size (const struct tdesc_feature *feature,
1175 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1177 gdb_assert (reg != NULL);
1178 return reg->bitsize;
1181 /* Look up a register by its GDB internal register number. */
1183 static struct tdesc_arch_reg *
1184 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1186 struct tdesc_arch_data *data;
1188 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1189 if (regno < data->arch_regs.size ())
1190 return &data->arch_regs[regno];
1195 static struct tdesc_reg *
1196 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1198 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1200 return reg? reg->reg : NULL;
1203 /* Return the name of register REGNO, from the target description or
1204 from an architecture-provided pseudo_register_name method. */
1207 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1209 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1210 int num_regs = gdbarch_num_regs (gdbarch);
1211 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1214 return reg->name.c_str ();
1216 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1218 struct tdesc_arch_data *data
1219 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1221 gdb_assert (data->pseudo_register_name != NULL);
1222 return data->pseudo_register_name (gdbarch, regno);
1229 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1231 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1232 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1233 int num_regs = gdbarch_num_regs (gdbarch);
1234 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1236 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1238 struct tdesc_arch_data *data
1239 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1241 gdb_assert (data->pseudo_register_type != NULL);
1242 return data->pseudo_register_type (gdbarch, regno);
1246 /* Return "int0_t", since "void" has a misleading size of one. */
1247 return builtin_type (gdbarch)->builtin_int0;
1249 if (arch_reg->type == NULL)
1251 /* First check for a predefined or target defined type. */
1252 if (reg->tdesc_type)
1253 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
1255 /* Next try size-sensitive type shortcuts. */
1256 else if (reg->type == "float")
1258 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1259 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1260 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1261 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1262 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1263 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1266 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1267 reg->name.c_str (), reg->bitsize);
1268 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1271 else if (reg->type == "int")
1273 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1274 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1275 else if (reg->bitsize == TARGET_CHAR_BIT)
1276 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1277 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1278 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1279 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1280 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1281 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1282 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1283 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1284 /* A bit desperate by this point... */
1285 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1288 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1289 reg->name.c_str (), reg->bitsize);
1290 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1294 if (arch_reg->type == NULL)
1295 internal_error (__FILE__, __LINE__,
1296 "Register \"%s\" has an unknown type \"%s\"",
1297 reg->name.c_str (), reg->type.c_str ());
1300 return arch_reg->type;
1304 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1306 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1309 return reg->target_regnum;
1314 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1315 target description may be classified as general, float, vector or other
1316 register groups registered with reggroup_add(). Unlike a gdbarch
1317 register_reggroup_p method, this function will return -1 if it does not
1318 know; the caller should handle registers with no specified group.
1320 The names of containing features are not used. This might be extended
1321 to display registers in some more useful groupings.
1323 The save-restore flag is also implemented here. */
1326 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1327 struct reggroup *reggroup)
1329 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1331 if (reg != NULL && !reg->group.empty ()
1332 && (reg->group == reggroup_name (reggroup)))
1336 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1337 return reg->save_restore;
1342 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1343 group specified go to the default reggroup function and are handled
1347 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1348 struct reggroup *reggroup)
1350 int num_regs = gdbarch_num_regs (gdbarch);
1351 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1354 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1356 struct tdesc_arch_data *data
1357 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1359 if (data->pseudo_register_reggroup_p != NULL)
1360 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1361 /* Otherwise fall through to the default reggroup_p. */
1364 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1368 return default_register_reggroup_p (gdbarch, regno, reggroup);
1371 /* Record architecture-specific functions to call for pseudo-register
1375 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1376 gdbarch_register_name_ftype *pseudo_name)
1378 struct tdesc_arch_data *data
1379 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1381 data->pseudo_register_name = pseudo_name;
1385 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1386 gdbarch_register_type_ftype *pseudo_type)
1388 struct tdesc_arch_data *data
1389 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1391 data->pseudo_register_type = pseudo_type;
1395 set_tdesc_pseudo_register_reggroup_p
1396 (struct gdbarch *gdbarch,
1397 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1399 struct tdesc_arch_data *data
1400 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1402 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1405 /* Update GDBARCH to use the target description for registers. */
1408 tdesc_use_registers (struct gdbarch *gdbarch,
1409 const struct target_desc *target_desc,
1410 struct tdesc_arch_data *early_data)
1412 int num_regs = gdbarch_num_regs (gdbarch);
1413 struct tdesc_arch_data *data;
1416 /* We can't use the description for registers if it doesn't describe
1417 any. This function should only be called after validating
1418 registers, so the caller should know that registers are
1420 gdb_assert (tdesc_has_registers (target_desc));
1422 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1423 data->arch_regs = early_data->arch_regs;
1426 /* Build up a set of all registers, so that we can assign register
1427 numbers where needed. The hash table expands as necessary, so
1428 the initial size is arbitrary. */
1429 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1430 for (const tdesc_feature_up &feature : target_desc->features)
1431 for (const tdesc_reg_up ® : feature->registers)
1433 void **slot = htab_find_slot (reg_hash, reg.get (), INSERT);
1436 /* Add reggroup if its new. */
1437 if (!reg->group.empty ())
1438 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1439 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1440 reg->group.c_str (),
1444 /* Remove any registers which were assigned numbers by the
1446 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1447 if (arch_reg.reg != NULL)
1448 htab_remove_elt (reg_hash, arch_reg.reg);
1450 /* Assign numbers to the remaining registers and add them to the
1451 list of registers. The new numbers are always above gdbarch_num_regs.
1452 Iterate over the features, not the hash table, so that the order
1453 matches that in the target description. */
1455 gdb_assert (data->arch_regs.size () <= num_regs);
1456 while (data->arch_regs.size () < num_regs)
1457 data->arch_regs.emplace_back (nullptr, nullptr);
1459 for (const tdesc_feature_up &feature : target_desc->features)
1460 for (const tdesc_reg_up ® : feature->registers)
1461 if (htab_find (reg_hash, reg.get ()) != NULL)
1463 data->arch_regs.emplace_back (reg.get (), nullptr);
1467 htab_delete (reg_hash);
1469 /* Update the architecture. */
1470 set_gdbarch_num_regs (gdbarch, num_regs);
1471 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1472 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1473 set_gdbarch_remote_register_number (gdbarch,
1474 tdesc_remote_register_number);
1475 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1479 /* See common/tdesc.h. */
1482 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1483 int regnum, int save_restore, const char *group,
1484 int bitsize, const char *type)
1486 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1487 group, bitsize, type);
1489 feature->registers.emplace_back (reg);
1492 /* See common/tdesc.h. */
1495 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1496 struct tdesc_type *field_type, int count)
1498 tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count);
1499 feature->types.emplace_back (type);
1504 /* See common/tdesc.h. */
1506 tdesc_type_with_fields *
1507 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1509 tdesc_type_with_fields *type
1510 = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT);
1511 feature->types.emplace_back (type);
1516 /* See common/tdesc.h. */
1519 tdesc_set_struct_size (tdesc_type_with_fields *type, int size)
1521 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1522 gdb_assert (size > 0);
1526 /* See common/tdesc.h. */
1528 tdesc_type_with_fields *
1529 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1531 tdesc_type_with_fields *type
1532 = new tdesc_type_with_fields (name, TDESC_TYPE_UNION);
1533 feature->types.emplace_back (type);
1538 /* See common/tdesc.h. */
1540 tdesc_type_with_fields *
1541 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1544 gdb_assert (size > 0);
1546 tdesc_type_with_fields *type
1547 = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size);
1548 feature->types.emplace_back (type);
1553 tdesc_type_with_fields *
1554 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1557 gdb_assert (size > 0);
1559 tdesc_type_with_fields *type
1560 = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size);
1561 feature->types.emplace_back (type);
1566 /* See common/tdesc.h. */
1569 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name,
1570 struct tdesc_type *field_type)
1572 gdb_assert (type->kind == TDESC_TYPE_UNION
1573 || type->kind == TDESC_TYPE_STRUCT);
1575 /* Initialize start and end so we know this is not a bit-field
1576 when we print-c-tdesc. */
1577 type->fields.emplace_back (field_name, field_type, -1, -1);
1581 tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name,
1582 int start, int end, struct tdesc_type *field_type)
1584 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1585 || type->kind == TDESC_TYPE_FLAGS);
1586 gdb_assert (start >= 0 && end >= start);
1588 type->fields.emplace_back (field_name, field_type, start, end);
1591 /* See common/tdesc.h. */
1594 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name,
1597 struct tdesc_type *field_type;
1599 gdb_assert (start >= 0 && end >= start);
1602 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1604 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1606 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1609 /* See common/tdesc.h. */
1612 tdesc_add_flag (tdesc_type_with_fields *type, int start,
1613 const char *flag_name)
1615 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1616 || type->kind == TDESC_TYPE_STRUCT);
1618 type->fields.emplace_back (flag_name,
1619 tdesc_predefined_type (TDESC_TYPE_BOOL),
1624 tdesc_add_enum_value (tdesc_type_with_fields *type, int value,
1627 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1628 type->fields.emplace_back (name,
1629 tdesc_predefined_type (TDESC_TYPE_INT32),
1633 /* See common/tdesc.h. */
1635 struct tdesc_feature *
1636 tdesc_create_feature (struct target_desc *tdesc, const char *name,
1639 struct tdesc_feature *new_feature = new tdesc_feature (name);
1641 tdesc->features.emplace_back (new_feature);
1646 struct target_desc *
1647 allocate_target_description (void)
1649 return new target_desc ();
1653 free_target_description (void *arg)
1655 struct target_desc *target_desc = (struct target_desc *) arg;
1661 make_cleanup_free_target_description (struct target_desc *target_desc)
1663 return make_cleanup (free_target_description, target_desc);
1667 tdesc_add_compatible (struct target_desc *target_desc,
1668 const struct bfd_arch_info *compatible)
1670 /* If this instance of GDB is compiled without BFD support for the
1671 compatible architecture, simply ignore it -- we would not be able
1672 to handle it anyway. */
1673 if (compatible == NULL)
1676 for (const bfd_arch_info *compat : target_desc->compatible)
1677 if (compat == compatible)
1678 internal_error (__FILE__, __LINE__,
1679 _("Attempted to add duplicate "
1680 "compatible architecture \"%s\""),
1681 compatible->printable_name);
1683 target_desc->compatible.push_back (compatible);
1687 set_tdesc_property (struct target_desc *target_desc,
1688 const char *key, const char *value)
1690 gdb_assert (key != NULL && value != NULL);
1692 if (tdesc_property (target_desc, key) != NULL)
1693 internal_error (__FILE__, __LINE__,
1694 _("Attempted to add duplicate property \"%s\""), key);
1696 target_desc->properties.emplace_back (key, value);
1699 /* See common/tdesc.h. */
1702 set_tdesc_architecture (struct target_desc *target_desc,
1705 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1709 set_tdesc_architecture (struct target_desc *target_desc,
1710 const struct bfd_arch_info *arch)
1712 target_desc->arch = arch;
1715 /* See common/tdesc.h. */
1718 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1720 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1724 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1726 target_desc->osabi = osabi;
1730 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1731 static struct cmd_list_element *tdesc_unset_cmdlist;
1733 /* Helper functions for the CLI commands. */
1736 set_tdesc_cmd (const char *args, int from_tty)
1738 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1742 show_tdesc_cmd (const char *args, int from_tty)
1744 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1748 unset_tdesc_cmd (const char *args, int from_tty)
1750 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1754 set_tdesc_filename_cmd (const char *args, int from_tty,
1755 struct cmd_list_element *c)
1757 xfree (target_description_filename);
1758 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1760 target_clear_description ();
1761 target_find_description ();
1765 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1766 struct cmd_list_element *c,
1769 value = target_description_filename;
1771 if (value != NULL && *value != '\0')
1772 printf_filtered (_("The target description will be read from \"%s\".\n"),
1775 printf_filtered (_("The target description will be "
1776 "read from the target.\n"));
1780 unset_tdesc_filename_cmd (const char *args, int from_tty)
1782 xfree (target_description_filename);
1783 target_description_filename = NULL;
1784 target_clear_description ();
1785 target_find_description ();
1788 /* Print target description in C. */
1790 class print_c_tdesc : public tdesc_element_visitor
1793 print_c_tdesc (std::string &filename_after_features)
1794 : m_filename_after_features (filename_after_features)
1798 const char *filename = lbasename (m_filename_after_features.c_str ());
1800 m_function = (char *) xmalloc (strlen (filename) + 1);
1801 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1804 else if (*inp == '-')
1810 /* Standard boilerplate. */
1811 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1812 "-*- buffer-read-only: t -*- vi"
1821 void visit_pre (const target_desc *e) override
1823 printf_unfiltered (" Original: %s */\n\n",
1824 lbasename (m_filename_after_features.c_str ()));
1826 printf_unfiltered ("#include \"defs.h\"\n");
1827 printf_unfiltered ("#include \"osabi.h\"\n");
1828 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1829 printf_unfiltered ("\n");
1831 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1832 printf_unfiltered ("static void\n");
1833 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1834 printf_unfiltered ("{\n");
1836 (" struct target_desc *result = allocate_target_description ();\n");
1838 if (tdesc_architecture (e) != NULL)
1841 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1842 tdesc_architecture (e)->printable_name);
1843 printf_unfiltered ("\n");
1845 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1846 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1849 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1850 gdbarch_osabi_name (tdesc_osabi (e)));
1851 printf_unfiltered ("\n");
1854 for (const bfd_arch_info_type *compatible : e->compatible)
1856 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1857 compatible->printable_name);
1859 if (!e->compatible.empty ())
1860 printf_unfiltered ("\n");
1862 for (const property &prop : e->properties)
1863 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1864 prop.key.c_str (), prop.value.c_str ());
1866 printf_unfiltered (" struct tdesc_feature *feature;\n");
1869 void visit_pre (const tdesc_feature *e) override
1871 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1875 void visit_post (const tdesc_feature *e) override
1878 void visit_post (const target_desc *e) override
1880 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1881 printf_unfiltered ("}\n");
1884 void visit (const tdesc_type_builtin *type) override
1886 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1889 void visit (const tdesc_type_vector *type) override
1891 if (!m_printed_element_type)
1893 printf_unfiltered (" tdesc_type *element_type;\n");
1894 m_printed_element_type = true;
1898 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1899 type->element_type->name.c_str ());
1901 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1902 type->name.c_str (), type->count);
1904 printf_unfiltered ("\n");
1907 void visit (const tdesc_type_with_fields *type) override
1909 if (!m_printed_type_with_fields)
1911 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1912 m_printed_type_with_fields = true;
1917 case TDESC_TYPE_STRUCT:
1918 case TDESC_TYPE_FLAGS:
1919 if (type->kind == TDESC_TYPE_STRUCT)
1922 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1923 type->name.c_str ());
1924 if (type->size != 0)
1926 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
1931 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1932 type->name.c_str (), type->size);
1934 for (const tdesc_type_field &f : type->fields)
1936 const char *type_name;
1938 gdb_assert (f.type != NULL);
1939 type_name = f.type->name.c_str ();
1941 /* To minimize changes to generated files, don't emit type
1942 info for fields that have defaulted types. */
1945 gdb_assert (f.end != -1);
1946 if (f.type->kind == TDESC_TYPE_BOOL)
1948 gdb_assert (f.start == f.end);
1950 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1951 f.start, f.name.c_str ());
1953 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1955 && f.type->kind == TDESC_TYPE_UINT64))
1958 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1959 f.name.c_str (), f.start, f.end);
1963 printf_field_type_assignment
1964 ("tdesc_named_type (feature, \"%s\");\n",
1967 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1968 " %d, %d, field_type);\n",
1969 f.name.c_str (), f.start, f.end);
1972 else /* Not a bitfield. */
1974 gdb_assert (f.end == -1);
1975 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1976 printf_field_type_assignment
1977 ("tdesc_named_type (feature, \"%s\");\n", type_name);
1979 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1984 case TDESC_TYPE_UNION:
1986 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1987 type->name.c_str ());
1988 for (const tdesc_type_field &f : type->fields)
1990 printf_field_type_assignment
1991 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
1993 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1997 case TDESC_TYPE_ENUM:
1999 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
2000 type->name.c_str (), type->size);
2001 for (const tdesc_type_field &f : type->fields)
2003 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
2004 f.start, f.name.c_str ());
2007 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
2010 printf_unfiltered ("\n");
2013 void visit (const tdesc_reg *reg) override
2015 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2016 reg->name.c_str (), reg->target_regnum,
2018 if (!reg->group.empty ())
2019 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
2021 printf_unfiltered ("NULL, ");
2022 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
2026 std::string m_filename_after_features;
2030 /* Print an assignment to the field_type variable. Print the declaration
2031 of field_type if that has not been done yet. */
2032 ATTRIBUTE_PRINTF (2, 3)
2033 void printf_field_type_assignment (const char *fmt, ...)
2035 if (!m_printed_field_type)
2037 printf_unfiltered (" tdesc_type *field_type;\n");
2038 m_printed_field_type = true;
2041 printf_unfiltered (" field_type = ");
2044 va_start (args, fmt);
2045 vprintf_unfiltered (fmt, args);
2051 /* Did we print "struct tdesc_type *element_type;" yet? */
2052 bool m_printed_element_type = false;
2054 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
2055 bool m_printed_type_with_fields = false;
2057 /* Did we print "struct tdesc_type *field_type;" yet? */
2058 bool m_printed_field_type = false;
2061 /* Print target description feature in C. */
2063 class print_c_feature : public print_c_tdesc
2066 print_c_feature (std::string &file)
2067 : print_c_tdesc (file)
2070 auto const pos = m_filename_after_features.find_last_of ('.');
2072 m_filename_after_features = m_filename_after_features.substr (0, pos);
2075 void visit_pre (const target_desc *e) override
2077 printf_unfiltered (" Original: %s */\n\n",
2078 lbasename (m_filename_after_features.c_str ()));
2080 printf_unfiltered ("#include \"common/tdesc.h\"\n");
2081 printf_unfiltered ("\n");
2084 void visit_post (const target_desc *e) override
2087 void visit_pre (const tdesc_feature *e) override
2089 std::string name (m_filename_after_features);
2091 auto pos = name.find_first_of ('.');
2093 name = name.substr (0, pos);
2094 std::replace (name.begin (), name.end (), '/', '_');
2095 std::replace (name.begin (), name.end (), '-', '_');
2097 printf_unfiltered ("static int\n");
2098 printf_unfiltered ("create_feature_%s ", name.c_str ());
2099 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2101 printf_unfiltered ("{\n");
2102 printf_unfiltered (" struct tdesc_feature *feature;\n");
2105 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2106 e->name.c_str (), lbasename (m_filename_after_features.c_str ()));
2109 void visit_post (const tdesc_feature *e) override
2111 printf_unfiltered (" return regnum;\n");
2112 printf_unfiltered ("}\n");
2115 void visit (const tdesc_reg *reg) override
2117 /* Most "reg" in XML target descriptions don't have "regnum"
2118 attribute, so the register number is allocated sequentially.
2119 In case that reg has "regnum" attribute, register number
2120 should be set by that explicitly. */
2122 if (reg->target_regnum < m_next_regnum)
2124 /* The integrity check, it can catch some errors on register
2125 number collision, like this,
2127 <reg name="x0" bitsize="32"/>
2128 <reg name="x1" bitsize="32"/>
2129 <reg name="x2" bitsize="32"/>
2130 <reg name="x3" bitsize="32"/>
2131 <reg name="ps" bitsize="32" regnum="3"/>
2133 but it also has false negatives. The target description
2136 <reg name="x1" bitsize="32" regnum="1"/>
2137 <reg name="x3" bitsize="32" regnum="3"/>
2138 <reg name="x2" bitsize="32" regnum="2"/>
2139 <reg name="x4" bitsize="32" regnum="4"/>
2141 but it is not a good practice, so still error on this,
2142 and also print the message so that it can be saved in the
2143 generated c file. */
2145 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2146 reg->target_regnum);
2147 printf_unfiltered ("is not the largest number (%d).\n",
2149 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2150 reg->target_regnum, m_next_regnum);
2153 if (reg->target_regnum > m_next_regnum)
2155 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2156 m_next_regnum = reg->target_regnum;
2159 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2160 reg->name.c_str (), reg->save_restore);
2161 if (!reg->group.empty ())
2162 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
2164 printf_unfiltered ("NULL, ");
2165 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
2171 /* The register number to use for the next register we see. */
2172 int m_next_regnum = 0;
2176 maint_print_c_tdesc_cmd (const char *args, int from_tty)
2178 const struct target_desc *tdesc;
2179 const char *filename;
2183 /* Use the global target-supplied description, not the current
2184 architecture's. This lets a GDB for one architecture generate C
2185 for another architecture's description, even though the gdbarch
2186 initialization code will reject the new description. */
2187 tdesc = current_target_desc;
2188 filename = target_description_filename;
2192 /* Use the target description from the XML file. */
2194 tdesc = file_read_description_xml (filename);
2198 error (_("There is no target description to print."));
2200 if (filename == NULL)
2201 error (_("The current target description did not come from an XML file."));
2203 std::string filename_after_features (filename);
2204 auto loc = filename_after_features.rfind ("/features/");
2206 if (loc != std::string::npos)
2207 filename_after_features = filename_after_features.substr (loc + 10);
2209 /* Print c files for target features instead of target descriptions,
2210 because c files got from target features are more flexible than the
2212 if (startswith (filename_after_features.c_str (), "i386/32bit-")
2213 || startswith (filename_after_features.c_str (), "i386/64bit-")
2214 || startswith (filename_after_features.c_str (), "i386/x32-core.xml")
2215 || startswith (filename_after_features.c_str (), "tic6x-")
2216 || startswith (filename_after_features.c_str (), "aarch64"))
2218 print_c_feature v (filename_after_features);
2224 print_c_tdesc v (filename_after_features);
2230 namespace selftests {
2232 static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2236 /* See target-descritpions.h. */
2239 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2241 xml_tdesc.emplace_back (xml_file, tdesc);
2247 /* Check that the target descriptions created dynamically by
2248 architecture-specific code equal the descriptions created from XML files
2249 found in the specified directory DIR. */
2252 maintenance_check_xml_descriptions (const char *dir, int from_tty)
2255 error (_("Missing dir name"));
2257 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2258 std::string feature_dir (dir1.get ());
2259 unsigned int failed = 0;
2261 for (auto const &e : selftests::xml_tdesc)
2263 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2264 const target_desc *tdesc
2265 = file_read_description_xml (tdesc_xml.data ());
2267 if (tdesc == NULL || *tdesc != *e.second)
2270 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2271 (long) selftests::xml_tdesc.size (), failed);
2275 _initialize_target_descriptions (void)
2277 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2279 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2280 Set target description specific variables."),
2281 &tdesc_set_cmdlist, "set tdesc ",
2282 0 /* allow-unknown */, &setlist);
2283 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2284 Show target description specific variables."),
2285 &tdesc_show_cmdlist, "show tdesc ",
2286 0 /* allow-unknown */, &showlist);
2287 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2288 Unset target description specific variables."),
2289 &tdesc_unset_cmdlist, "unset tdesc ",
2290 0 /* allow-unknown */, &unsetlist);
2292 add_setshow_filename_cmd ("filename", class_obscure,
2293 &tdesc_filename_cmd_string,
2295 Set the file to read for an XML target description"), _("\
2296 Show the file to read for an XML target description"), _("\
2297 When set, GDB will read the target description from a local\n\
2298 file instead of querying the remote target."),
2299 set_tdesc_filename_cmd,
2300 show_tdesc_filename_cmd,
2301 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2303 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2304 Unset the file to read for an XML target description. When unset,\n\
2305 GDB will read the description from the target."),
2306 &tdesc_unset_cmdlist);
2308 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2309 Print the current target description as a C source file."),
2310 &maintenanceprintlist);
2312 cmd_list_element *cmd;
2314 cmd = add_cmd ("xml-descriptions", class_maintenance,
2315 maintenance_check_xml_descriptions, _("\
2316 Check the target descriptions created in GDB equal the descriptions\n\
2317 created from XML files in the directory.\n\
2318 The parameter is the directory name."),
2319 &maintenancechecklist);
2320 set_cmd_completer (cmd, filename_completer);