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"
39 /* The interface to visit different elements of target description. */
41 class tdesc_element_visitor
44 virtual void visit_pre (const target_desc *e) = 0;
45 virtual void visit_post (const target_desc *e) = 0;
47 virtual void visit_pre (const tdesc_feature *e) = 0;
48 virtual void visit_post (const tdesc_feature *e) = 0;
50 virtual void visit (const tdesc_type *e) = 0;
51 virtual void visit (const tdesc_reg *e) = 0;
57 virtual void accept (tdesc_element_visitor &v) const = 0;
62 typedef struct property
67 DEF_VEC_O(property_s);
69 /* An individual register from a target description. */
71 typedef struct tdesc_reg : tdesc_element
73 tdesc_reg (struct tdesc_feature *feature, const char *name_,
74 int regnum, int save_restore_, const char *group_,
75 int bitsize_, const char *type_)
76 : name (xstrdup (name_)), target_regnum (regnum),
77 save_restore (save_restore_),
78 group (group_ != NULL ? xstrdup (group_) : NULL),
80 type (type_ != NULL ? xstrdup (type_) : xstrdup ("<unknown>"))
82 /* If the register's type is target-defined, look it up now. We may not
83 have easy access to the containing feature when we want it later. */
84 tdesc_type = tdesc_named_type (feature, type);
94 /* Disable copying. */
95 tdesc_reg (const tdesc_reg &) = delete;
96 tdesc_reg &operator= (const tdesc_reg &) = delete;
98 /* The name of this register. In standard features, it may be
99 recognized by the architecture support code, or it may be purely
103 /* The register number used by this target to refer to this
104 register. This is used for remote p/P packets and to determine
105 the ordering of registers in the remote g/G packets. */
108 /* If this flag is set, GDB should save and restore this register
109 around calls to an inferior function. */
112 /* The name of the register group containing this register, or NULL
113 if the group should be automatically determined from the
114 register's type. If this is "general", "float", or "vector", the
115 corresponding "info" command should display this register's
116 value. It can be an arbitrary string, but should be limited to
117 alphanumeric characters and internal hyphens. Currently other
118 strings are ignored (treated as NULL). */
121 /* The size of the register, in bits. */
124 /* The type of the register. This string corresponds to either
125 a named type from the target description or a predefined
129 /* The target-described type corresponding to TYPE, if found. */
130 struct tdesc_type *tdesc_type;
132 void accept (tdesc_element_visitor &v) const override
138 DEF_VEC_P(tdesc_reg_p);
140 /* A named type from a target description. */
142 typedef struct tdesc_type_field
145 struct tdesc_type *type;
146 /* For non-enum-values, either both are -1 (non-bitfield), or both are
147 not -1 (bitfield). For enum values, start is the value (which could be
151 DEF_VEC_O(tdesc_type_field);
155 /* Predefined types. */
169 TDESC_TYPE_IEEE_SINGLE,
170 TDESC_TYPE_IEEE_DOUBLE,
171 TDESC_TYPE_ARM_FPA_EXT,
174 /* Types defined by a target feature. */
182 typedef struct tdesc_type : tdesc_element
184 tdesc_type (const char *name_, enum tdesc_type_kind kind_)
185 : name (xstrdup (name_)), kind (kind_)
187 memset (&u, 0, sizeof (u));
190 virtual ~tdesc_type ()
194 case TDESC_TYPE_STRUCT:
195 case TDESC_TYPE_UNION:
196 case TDESC_TYPE_FLAGS:
197 case TDESC_TYPE_ENUM:
199 struct tdesc_type_field *f;
203 VEC_iterate (tdesc_type_field, u.u.fields, ix, f);
207 VEC_free (tdesc_type_field, u.u.fields);
214 xfree ((char *) name);
216 /* Disable copying. */
217 tdesc_type (const tdesc_type &) = delete;
218 tdesc_type &operator= (const tdesc_type &) = delete;
220 /* The name of this type. If this type is a built-in type, this is
221 a pointer to a constant string. Otherwise, it's a
222 malloc-allocated string (and thus must be freed). */
225 /* Identify the kind of this type. */
226 enum tdesc_type_kind kind;
228 /* Kind-specific data. */
234 struct tdesc_type *type;
238 /* Struct, union, flags, or enum type. */
241 VEC(tdesc_type_field) *fields;
246 void accept (tdesc_element_visitor &v) const override
252 DEF_VEC_P(tdesc_type_p);
254 /* A feature from a target description. Each feature is a collection
255 of other elements, e.g. registers and types. */
257 typedef struct tdesc_feature : tdesc_element
259 tdesc_feature (const char *name_)
260 : name (xstrdup (name_))
263 virtual ~tdesc_feature ()
265 struct tdesc_reg *reg;
266 struct tdesc_type *type;
269 for (ix = 0; VEC_iterate (tdesc_reg_p, registers, ix, reg); ix++)
271 VEC_free (tdesc_reg_p, registers);
273 for (ix = 0; VEC_iterate (tdesc_type_p, types, ix, type); ix++)
275 VEC_free (tdesc_type_p, types);
280 /* Disable copying. */
281 tdesc_feature (const tdesc_feature &) = delete;
282 tdesc_feature &operator= (const tdesc_feature &) = delete;
284 /* The name of this feature. It may be recognized by the architecture
288 /* The registers associated with this feature. */
289 VEC(tdesc_reg_p) *registers = NULL;
291 /* The types associated with this feature. */
292 VEC(tdesc_type_p) *types = NULL;
294 void accept (tdesc_element_visitor &v) const override
298 struct tdesc_type *type;
301 VEC_iterate (tdesc_type_p, types, ix, type);
305 struct tdesc_reg *reg;
308 VEC_iterate (tdesc_reg_p, registers, ix, reg);
316 DEF_VEC_P(tdesc_feature_p);
318 /* A compatible architecture from a target description. */
319 typedef const struct bfd_arch_info *arch_p;
322 /* A target description. */
324 struct target_desc : tdesc_element
329 virtual ~target_desc ()
331 struct tdesc_feature *feature;
332 struct property *prop;
336 VEC_iterate (tdesc_feature_p, features, ix, feature);
339 VEC_free (tdesc_feature_p, features);
342 VEC_iterate (property_s, properties, ix, prop);
349 VEC_free (property_s, properties);
350 VEC_free (arch_p, compatible);
353 target_desc (const target_desc &) = delete;
354 void operator= (const target_desc &) = delete;
356 /* The architecture reported by the target, if any. */
357 const struct bfd_arch_info *arch = NULL;
359 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
361 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
363 /* The list of compatible architectures reported by the target. */
364 VEC(arch_p) *compatible = NULL;
366 /* Any architecture-specific properties specified by the target. */
367 VEC(property_s) *properties = NULL;
369 /* The features associated with this target. */
370 VEC(tdesc_feature_p) *features = NULL;
372 void accept (tdesc_element_visitor &v) const override
376 struct tdesc_feature *feature;
379 VEC_iterate (tdesc_feature_p, features, ix, feature);
387 /* Per-architecture data associated with a target description. The
388 target description may be shared by multiple architectures, but
389 this data is private to one gdbarch. */
391 typedef struct tdesc_arch_reg
393 struct tdesc_reg *reg;
396 DEF_VEC_O(tdesc_arch_reg);
398 struct tdesc_arch_data
400 /* A list of register/type pairs, indexed by GDB's internal register number.
401 During initialization of the gdbarch this list is used to store
402 registers which the architecture assigns a fixed register number.
403 Registers which are NULL in this array, or off the end, are
404 treated as zero-sized and nameless (i.e. placeholders in the
406 VEC(tdesc_arch_reg) *arch_regs;
408 /* Functions which report the register name, type, and reggroups for
410 gdbarch_register_name_ftype *pseudo_register_name;
411 gdbarch_register_type_ftype *pseudo_register_type;
412 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
415 /* Info about an inferior's target description. There's one of these
416 for each inferior. */
418 struct target_desc_info
420 /* A flag indicating that a description has already been fetched
421 from the target, so it should not be queried again. */
425 /* The description fetched from the target, or NULL if the target
426 did not supply any description. Only valid when
427 target_desc_fetched is set. Only the description initialization
428 code should access this; normally, the description should be
429 accessed through the gdbarch object. */
431 const struct target_desc *tdesc;
433 /* The filename to read a target description from, as set by "set
434 tdesc filename ..." */
439 /* Get the inferior INF's target description info, allocating one on
440 the stop if necessary. */
442 static struct target_desc_info *
443 get_tdesc_info (struct inferior *inf)
445 if (inf->tdesc_info == NULL)
446 inf->tdesc_info = XCNEW (struct target_desc_info);
447 return inf->tdesc_info;
450 /* A handle for architecture-specific data associated with the
451 target description (see struct tdesc_arch_data). */
453 static struct gdbarch_data *tdesc_data;
455 /* See target-descriptions.h. */
458 target_desc_info_from_user_p (struct target_desc_info *info)
460 return info != NULL && info->filename != NULL;
463 /* See target-descriptions.h. */
466 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
468 struct target_desc_info *src = get_tdesc_info (srcinf);
469 struct target_desc_info *dest = get_tdesc_info (destinf);
471 dest->fetched = src->fetched;
472 dest->tdesc = src->tdesc;
473 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
476 /* See target-descriptions.h. */
479 target_desc_info_free (struct target_desc_info *tdesc_info)
481 if (tdesc_info != NULL)
483 xfree (tdesc_info->filename);
488 /* Convenience helper macros. */
490 #define target_desc_fetched \
491 get_tdesc_info (current_inferior ())->fetched
492 #define current_target_desc \
493 get_tdesc_info (current_inferior ())->tdesc
494 #define target_description_filename \
495 get_tdesc_info (current_inferior ())->filename
497 /* The string manipulated by the "set tdesc filename ..." command. */
499 static char *tdesc_filename_cmd_string;
501 /* Fetch the current target's description, and switch the current
502 architecture to one which incorporates that description. */
505 target_find_description (void)
507 /* If we've already fetched a description from the target, don't do
508 it again. This allows a target to fetch the description early,
509 during its to_open or to_create_inferior, if it needs extra
510 information about the target to initialize. */
511 if (target_desc_fetched)
514 /* The current architecture should not have any target description
515 specified. It should have been cleared, e.g. when we
516 disconnected from the previous target. */
517 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
519 /* First try to fetch an XML description from the user-specified
521 current_target_desc = NULL;
522 if (target_description_filename != NULL
523 && *target_description_filename != '\0')
525 = file_read_description_xml (target_description_filename);
527 /* Next try to read the description from the current target using
529 if (current_target_desc == NULL)
530 current_target_desc = target_read_description_xml (¤t_target);
532 /* If that failed try a target-specific hook. */
533 if (current_target_desc == NULL)
534 current_target_desc = target_read_description (¤t_target);
536 /* If a non-NULL description was returned, then update the current
538 if (current_target_desc)
540 struct gdbarch_info info;
542 gdbarch_info_init (&info);
543 info.target_desc = current_target_desc;
544 if (!gdbarch_update_p (info))
545 warning (_("Architecture rejected target-supplied description"));
548 struct tdesc_arch_data *data;
550 data = ((struct tdesc_arch_data *)
551 gdbarch_data (target_gdbarch (), tdesc_data));
552 if (tdesc_has_registers (current_target_desc)
553 && data->arch_regs == NULL)
554 warning (_("Target-supplied registers are not supported "
555 "by the current architecture"));
559 /* Now that we know this description is usable, record that we
561 target_desc_fetched = 1;
564 /* Discard any description fetched from the current target, and switch
565 the current architecture to one with no target description. */
568 target_clear_description (void)
570 struct gdbarch_info info;
572 if (!target_desc_fetched)
575 target_desc_fetched = 0;
576 current_target_desc = NULL;
578 gdbarch_info_init (&info);
579 if (!gdbarch_update_p (info))
580 internal_error (__FILE__, __LINE__,
581 _("Could not remove target-supplied description"));
584 /* Return the global current target description. This should only be
585 used by gdbarch initialization code; most access should be through
586 an existing gdbarch. */
588 const struct target_desc *
589 target_current_description (void)
591 if (target_desc_fetched)
592 return current_target_desc;
597 /* Return non-zero if this target description is compatible
598 with the given BFD architecture. */
601 tdesc_compatible_p (const struct target_desc *target_desc,
602 const struct bfd_arch_info *arch)
604 const struct bfd_arch_info *compat;
607 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
611 || arch->compatible (arch, compat)
612 || compat->compatible (compat, arch))
620 /* Direct accessors for target descriptions. */
622 /* Return the string value of a property named KEY, or NULL if the
623 property was not specified. */
626 tdesc_property (const struct target_desc *target_desc, const char *key)
628 struct property *prop;
631 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
633 if (strcmp (prop->key, key) == 0)
639 /* Return the BFD architecture associated with this target
640 description, or NULL if no architecture was specified. */
642 const struct bfd_arch_info *
643 tdesc_architecture (const struct target_desc *target_desc)
645 return target_desc->arch;
648 /* Return the OSABI associated with this target description, or
649 GDB_OSABI_UNKNOWN if no osabi was specified. */
652 tdesc_osabi (const struct target_desc *target_desc)
654 return target_desc->osabi;
659 /* Return 1 if this target description includes any registers. */
662 tdesc_has_registers (const struct target_desc *target_desc)
665 struct tdesc_feature *feature;
667 if (target_desc == NULL)
671 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
673 if (! VEC_empty (tdesc_reg_p, feature->registers))
679 /* Return the feature with the given name, if present, or NULL if
680 the named feature is not found. */
682 const struct tdesc_feature *
683 tdesc_find_feature (const struct target_desc *target_desc,
687 struct tdesc_feature *feature;
690 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
692 if (strcmp (feature->name, name) == 0)
698 /* Return the name of FEATURE. */
701 tdesc_feature_name (const struct tdesc_feature *feature)
703 return feature->name;
706 /* Predefined types. */
707 static struct tdesc_type tdesc_predefined_types[] =
709 { "bool", TDESC_TYPE_BOOL },
710 { "int8", TDESC_TYPE_INT8 },
711 { "int16", TDESC_TYPE_INT16 },
712 { "int32", TDESC_TYPE_INT32 },
713 { "int64", TDESC_TYPE_INT64 },
714 { "int128", TDESC_TYPE_INT128 },
715 { "uint8", TDESC_TYPE_UINT8 },
716 { "uint16", TDESC_TYPE_UINT16 },
717 { "uint32", TDESC_TYPE_UINT32 },
718 { "uint64", TDESC_TYPE_UINT64 },
719 { "uint128", TDESC_TYPE_UINT128 },
720 { "code_ptr", TDESC_TYPE_CODE_PTR },
721 { "data_ptr", TDESC_TYPE_DATA_PTR },
722 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
723 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
724 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
725 { "i387_ext", TDESC_TYPE_I387_EXT }
728 /* Lookup a predefined type. */
730 static struct tdesc_type *
731 tdesc_predefined_type (enum tdesc_type_kind kind)
734 struct tdesc_type *type;
736 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
737 if (tdesc_predefined_types[ix].kind == kind)
738 return &tdesc_predefined_types[ix];
740 gdb_assert_not_reached ("bad predefined tdesc type");
743 /* Return the type associated with ID in the context of FEATURE, or
747 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
750 struct tdesc_type *type;
752 /* First try target-defined types. */
753 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
754 if (strcmp (type->name, id) == 0)
757 /* Next try the predefined types. */
758 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
759 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
760 return &tdesc_predefined_types[ix];
765 /* Lookup type associated with ID. */
768 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
770 struct tdesc_arch_reg *reg;
771 struct tdesc_arch_data *data;
774 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
775 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
776 for (i = 0; i < num_regs; i++)
778 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
780 && reg->reg->tdesc_type
782 && strcmp (id, reg->reg->tdesc_type->name) == 0)
789 /* Construct, if necessary, and return the GDB type implementing target
790 type TDESC_TYPE for architecture GDBARCH. */
793 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
797 switch (tdesc_type->kind)
799 /* Predefined types. */
800 case TDESC_TYPE_BOOL:
801 return builtin_type (gdbarch)->builtin_bool;
803 case TDESC_TYPE_INT8:
804 return builtin_type (gdbarch)->builtin_int8;
806 case TDESC_TYPE_INT16:
807 return builtin_type (gdbarch)->builtin_int16;
809 case TDESC_TYPE_INT32:
810 return builtin_type (gdbarch)->builtin_int32;
812 case TDESC_TYPE_INT64:
813 return builtin_type (gdbarch)->builtin_int64;
815 case TDESC_TYPE_INT128:
816 return builtin_type (gdbarch)->builtin_int128;
818 case TDESC_TYPE_UINT8:
819 return builtin_type (gdbarch)->builtin_uint8;
821 case TDESC_TYPE_UINT16:
822 return builtin_type (gdbarch)->builtin_uint16;
824 case TDESC_TYPE_UINT32:
825 return builtin_type (gdbarch)->builtin_uint32;
827 case TDESC_TYPE_UINT64:
828 return builtin_type (gdbarch)->builtin_uint64;
830 case TDESC_TYPE_UINT128:
831 return builtin_type (gdbarch)->builtin_uint128;
833 case TDESC_TYPE_CODE_PTR:
834 return builtin_type (gdbarch)->builtin_func_ptr;
836 case TDESC_TYPE_DATA_PTR:
837 return builtin_type (gdbarch)->builtin_data_ptr;
843 type = tdesc_find_type (gdbarch, tdesc_type->name);
847 switch (tdesc_type->kind)
849 case TDESC_TYPE_IEEE_SINGLE:
850 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
851 floatformats_ieee_single);
853 case TDESC_TYPE_IEEE_DOUBLE:
854 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
855 floatformats_ieee_double);
857 case TDESC_TYPE_ARM_FPA_EXT:
858 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
859 floatformats_arm_ext);
861 case TDESC_TYPE_I387_EXT:
862 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
863 floatformats_i387_ext);
865 /* Types defined by a target feature. */
866 case TDESC_TYPE_VECTOR:
868 struct type *type, *field_type;
870 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
871 type = init_vector_type (field_type, tdesc_type->u.v.count);
872 TYPE_NAME (type) = xstrdup (tdesc_type->name);
877 case TDESC_TYPE_STRUCT:
879 struct type *type, *field_type;
880 struct tdesc_type_field *f;
883 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
884 TYPE_NAME (type) = xstrdup (tdesc_type->name);
885 TYPE_TAG_NAME (type) = TYPE_NAME (type);
888 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
891 if (f->start != -1 && f->end != -1)
895 struct type *field_type;
896 int bitsize, total_size;
898 /* This invariant should be preserved while creating types. */
899 gdb_assert (tdesc_type->u.u.size != 0);
901 field_type = tdesc_gdb_type (gdbarch, f->type);
902 else if (tdesc_type->u.u.size > 4)
903 field_type = builtin_type (gdbarch)->builtin_uint64;
905 field_type = builtin_type (gdbarch)->builtin_uint32;
907 fld = append_composite_type_field_raw (type, xstrdup (f->name),
910 /* For little-endian, BITPOS counts from the LSB of
911 the structure and marks the LSB of the field. For
912 big-endian, BITPOS counts from the MSB of the
913 structure and marks the MSB of the field. Either
914 way, it is the number of bits to the "left" of the
915 field. To calculate this in big-endian, we need
916 the total size of the structure. */
917 bitsize = f->end - f->start + 1;
918 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
919 if (gdbarch_bits_big_endian (gdbarch))
920 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
922 SET_FIELD_BITPOS (fld[0], f->start);
923 FIELD_BITSIZE (fld[0]) = bitsize;
927 gdb_assert (f->start == -1 && f->end == -1);
928 field_type = tdesc_gdb_type (gdbarch, f->type);
929 append_composite_type_field (type, xstrdup (f->name),
934 if (tdesc_type->u.u.size != 0)
935 TYPE_LENGTH (type) = tdesc_type->u.u.size;
939 case TDESC_TYPE_UNION:
941 struct type *type, *field_type;
942 struct tdesc_type_field *f;
945 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
946 TYPE_NAME (type) = xstrdup (tdesc_type->name);
949 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
952 field_type = tdesc_gdb_type (gdbarch, f->type);
953 append_composite_type_field (type, xstrdup (f->name), field_type);
955 /* If any of the children of a union are vectors, flag the
956 union as a vector also. This allows e.g. a union of two
957 vector types to show up automatically in "info vector". */
958 if (TYPE_VECTOR (field_type))
959 TYPE_VECTOR (type) = 1;
964 case TDESC_TYPE_FLAGS:
966 struct tdesc_type_field *f;
969 type = arch_flags_type (gdbarch, tdesc_type->name,
970 tdesc_type->u.u.size);
972 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
975 struct type *field_type;
976 int bitsize = f->end - f->start + 1;
978 gdb_assert (f->type != NULL);
979 field_type = tdesc_gdb_type (gdbarch, f->type);
980 append_flags_type_field (type, f->start, bitsize,
981 field_type, f->name);
987 case TDESC_TYPE_ENUM:
989 struct tdesc_type_field *f;
992 type = arch_type (gdbarch, TYPE_CODE_ENUM,
993 tdesc_type->u.u.size, tdesc_type->name);
994 TYPE_UNSIGNED (type) = 1;
996 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1000 = append_composite_type_field_raw (type, xstrdup (f->name),
1003 SET_FIELD_BITPOS (fld[0], f->start);
1010 internal_error (__FILE__, __LINE__,
1011 "Type \"%s\" has an unknown kind %d",
1012 tdesc_type->name, tdesc_type->kind);
1016 /* Support for registers from target descriptions. */
1018 /* Construct the per-gdbarch data. */
1021 tdesc_data_init (struct obstack *obstack)
1023 struct tdesc_arch_data *data;
1025 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1029 /* Similar, but for the temporary copy used during architecture
1032 struct tdesc_arch_data *
1033 tdesc_data_alloc (void)
1035 return XCNEW (struct tdesc_arch_data);
1038 /* Free something allocated by tdesc_data_alloc, if it is not going
1039 to be used (for instance if it was unsuitable for the
1043 tdesc_data_cleanup (void *data_untyped)
1045 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1047 VEC_free (tdesc_arch_reg, data->arch_regs);
1051 /* Search FEATURE for a register named NAME. */
1053 static struct tdesc_reg *
1054 tdesc_find_register_early (const struct tdesc_feature *feature,
1058 struct tdesc_reg *reg;
1061 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1063 if (strcasecmp (reg->name, name) == 0)
1069 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1072 tdesc_numbered_register (const struct tdesc_feature *feature,
1073 struct tdesc_arch_data *data,
1074 int regno, const char *name)
1076 struct tdesc_arch_reg arch_reg = { 0 };
1077 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1082 /* Make sure the vector includes a REGNO'th element. */
1083 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1084 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1087 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
1091 /* Search FEATURE for a register named NAME, but do not assign a fixed
1092 register number to it. */
1095 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1098 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1106 /* Search FEATURE for a register whose name is in NAMES and assign
1110 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1111 struct tdesc_arch_data *data,
1112 int regno, const char *const names[])
1116 for (i = 0; names[i] != NULL; i++)
1117 if (tdesc_numbered_register (feature, data, regno, names[i]))
1123 /* Search FEATURE for a register named NAME, and return its size in
1124 bits. The register must exist. */
1127 tdesc_register_size (const struct tdesc_feature *feature,
1130 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1132 gdb_assert (reg != NULL);
1133 return reg->bitsize;
1136 /* Look up a register by its GDB internal register number. */
1138 static struct tdesc_arch_reg *
1139 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1141 struct tdesc_arch_data *data;
1143 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1144 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1145 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
1150 static struct tdesc_reg *
1151 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1153 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1155 return reg? reg->reg : NULL;
1158 /* Return the name of register REGNO, from the target description or
1159 from an architecture-provided pseudo_register_name method. */
1162 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1164 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1165 int num_regs = gdbarch_num_regs (gdbarch);
1166 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1171 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1173 struct tdesc_arch_data *data
1174 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1176 gdb_assert (data->pseudo_register_name != NULL);
1177 return data->pseudo_register_name (gdbarch, regno);
1184 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1186 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1187 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1188 int num_regs = gdbarch_num_regs (gdbarch);
1189 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1191 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1193 struct tdesc_arch_data *data
1194 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1196 gdb_assert (data->pseudo_register_type != NULL);
1197 return data->pseudo_register_type (gdbarch, regno);
1201 /* Return "int0_t", since "void" has a misleading size of one. */
1202 return builtin_type (gdbarch)->builtin_int0;
1204 if (arch_reg->type == NULL)
1206 /* First check for a predefined or target defined type. */
1207 if (reg->tdesc_type)
1208 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1210 /* Next try size-sensitive type shortcuts. */
1211 else if (strcmp (reg->type, "float") == 0)
1213 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1214 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1215 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1216 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1217 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1218 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1221 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1222 reg->name, reg->bitsize);
1223 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1226 else if (strcmp (reg->type, "int") == 0)
1228 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1229 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1230 else if (reg->bitsize == TARGET_CHAR_BIT)
1231 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1232 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1233 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1234 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1235 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1236 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1237 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1238 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1239 /* A bit desperate by this point... */
1240 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1243 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1244 reg->name, reg->bitsize);
1245 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1249 if (arch_reg->type == NULL)
1250 internal_error (__FILE__, __LINE__,
1251 "Register \"%s\" has an unknown type \"%s\"",
1252 reg->name, reg->type);
1255 return arch_reg->type;
1259 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1261 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1264 return reg->target_regnum;
1269 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1270 target description may be classified as general, float, or vector.
1271 Unlike a gdbarch register_reggroup_p method, this function will
1272 return -1 if it does not know; the caller should handle registers
1273 with no specified group.
1275 Arbitrary strings (other than "general", "float", and "vector")
1276 from the description are not used; they cause the register to be
1277 displayed in "info all-registers" but excluded from "info
1278 registers" et al. The names of containing features are also not
1279 used. This might be extended to display registers in some more
1282 The save-restore flag is also implemented here. */
1285 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1286 struct reggroup *reggroup)
1288 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1290 if (reg != NULL && reg->group != NULL)
1292 int general_p = 0, float_p = 0, vector_p = 0;
1294 if (strcmp (reg->group, "general") == 0)
1296 else if (strcmp (reg->group, "float") == 0)
1298 else if (strcmp (reg->group, "vector") == 0)
1301 if (reggroup == float_reggroup)
1304 if (reggroup == vector_reggroup)
1307 if (reggroup == general_reggroup)
1312 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1313 return reg->save_restore;
1318 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1319 group specified go to the default reggroup function and are handled
1323 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1324 struct reggroup *reggroup)
1326 int num_regs = gdbarch_num_regs (gdbarch);
1327 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1330 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1332 struct tdesc_arch_data *data
1333 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1335 if (data->pseudo_register_reggroup_p != NULL)
1336 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1337 /* Otherwise fall through to the default reggroup_p. */
1340 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1344 return default_register_reggroup_p (gdbarch, regno, reggroup);
1347 /* Record architecture-specific functions to call for pseudo-register
1351 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1352 gdbarch_register_name_ftype *pseudo_name)
1354 struct tdesc_arch_data *data
1355 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1357 data->pseudo_register_name = pseudo_name;
1361 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1362 gdbarch_register_type_ftype *pseudo_type)
1364 struct tdesc_arch_data *data
1365 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1367 data->pseudo_register_type = pseudo_type;
1371 set_tdesc_pseudo_register_reggroup_p
1372 (struct gdbarch *gdbarch,
1373 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1375 struct tdesc_arch_data *data
1376 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1378 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1381 /* Update GDBARCH to use the target description for registers. */
1384 tdesc_use_registers (struct gdbarch *gdbarch,
1385 const struct target_desc *target_desc,
1386 struct tdesc_arch_data *early_data)
1388 int num_regs = gdbarch_num_regs (gdbarch);
1390 struct tdesc_feature *feature;
1391 struct tdesc_reg *reg;
1392 struct tdesc_arch_data *data;
1393 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1396 /* We can't use the description for registers if it doesn't describe
1397 any. This function should only be called after validating
1398 registers, so the caller should know that registers are
1400 gdb_assert (tdesc_has_registers (target_desc));
1402 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1403 data->arch_regs = early_data->arch_regs;
1406 /* Build up a set of all registers, so that we can assign register
1407 numbers where needed. The hash table expands as necessary, so
1408 the initial size is arbitrary. */
1409 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1411 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1414 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1417 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1422 /* Remove any registers which were assigned numbers by the
1425 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1428 htab_remove_elt (reg_hash, arch_reg->reg);
1430 /* Assign numbers to the remaining registers and add them to the
1431 list of registers. The new numbers are always above gdbarch_num_regs.
1432 Iterate over the features, not the hash table, so that the order
1433 matches that in the target description. */
1435 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1436 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1437 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1439 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1442 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1444 if (htab_find (reg_hash, reg) != NULL)
1446 new_arch_reg.reg = reg;
1447 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1451 htab_delete (reg_hash);
1453 /* Update the architecture. */
1454 set_gdbarch_num_regs (gdbarch, num_regs);
1455 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1456 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1457 set_gdbarch_remote_register_number (gdbarch,
1458 tdesc_remote_register_number);
1459 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
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 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1475 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1476 struct tdesc_type *field_type, int count)
1478 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1480 type->u.v.type = field_type;
1481 type->u.v.count = count;
1483 VEC_safe_push (tdesc_type_p, feature->types, type);
1488 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1490 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1492 VEC_safe_push (tdesc_type_p, feature->types, type);
1496 /* Set the total length of TYPE. Structs which contain bitfields may
1497 omit the reserved bits, so the end of the last field may not
1501 tdesc_set_struct_size (struct tdesc_type *type, int size)
1503 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1504 gdb_assert (size > 0);
1505 type->u.u.size = size;
1509 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1511 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1513 VEC_safe_push (tdesc_type_p, feature->types, type);
1518 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1521 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1523 gdb_assert (size > 0);
1525 type->u.u.size = size;
1527 VEC_safe_push (tdesc_type_p, feature->types, type);
1532 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1535 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1537 gdb_assert (size > 0);
1539 type->u.u.size = size;
1541 VEC_safe_push (tdesc_type_p, feature->types, type);
1545 /* Add a new field to TYPE. */
1548 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1549 struct tdesc_type *field_type)
1551 struct tdesc_type_field f = { 0 };
1553 gdb_assert (type->kind == TDESC_TYPE_UNION
1554 || type->kind == TDESC_TYPE_STRUCT);
1556 f.name = xstrdup (field_name);
1557 f.type = field_type;
1558 /* Initialize these values so we know this is not a bit-field
1559 when we print-c-tdesc. */
1563 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1566 /* Add a new typed bitfield to TYPE. */
1569 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1570 int start, int end, struct tdesc_type *field_type)
1572 struct tdesc_type_field f = { 0 };
1574 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1575 || type->kind == TDESC_TYPE_FLAGS);
1576 gdb_assert (start >= 0 && end >= start);
1578 f.name = xstrdup (field_name);
1581 f.type = field_type;
1583 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1586 /* Add a new untyped bitfield to TYPE.
1587 Untyped bitfields become either uint32 or uint64 depending on the size
1588 of the underlying type. */
1591 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1594 struct tdesc_type *field_type;
1596 gdb_assert (start >= 0 && end >= start);
1598 if (type->u.u.size > 4)
1599 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1601 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1603 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1606 /* A flag is just a typed(bool) single-bit bitfield.
1607 This function is kept to minimize changes in generated files. */
1610 tdesc_add_flag (struct tdesc_type *type, int start,
1611 const char *flag_name)
1613 struct tdesc_type_field f = { 0 };
1615 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1616 || type->kind == TDESC_TYPE_STRUCT);
1618 f.name = xstrdup (flag_name);
1621 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
1623 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1627 tdesc_add_enum_value (struct tdesc_type *type, int value,
1630 struct tdesc_type_field f = { 0 };
1632 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1634 f.name = xstrdup (name);
1637 f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1639 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1642 struct tdesc_feature *
1643 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1645 struct tdesc_feature *new_feature = new tdesc_feature (name);
1647 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1651 struct target_desc *
1652 allocate_target_description (void)
1654 return new target_desc ();
1658 free_target_description (void *arg)
1660 struct target_desc *target_desc = (struct target_desc *) arg;
1666 make_cleanup_free_target_description (struct target_desc *target_desc)
1668 return make_cleanup (free_target_description, target_desc);
1672 tdesc_add_compatible (struct target_desc *target_desc,
1673 const struct bfd_arch_info *compatible)
1675 const struct bfd_arch_info *compat;
1678 /* If this instance of GDB is compiled without BFD support for the
1679 compatible architecture, simply ignore it -- we would not be able
1680 to handle it anyway. */
1681 if (compatible == NULL)
1684 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1686 if (compat == compatible)
1687 internal_error (__FILE__, __LINE__,
1688 _("Attempted to add duplicate "
1689 "compatible architecture \"%s\""),
1690 compatible->printable_name);
1692 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1696 set_tdesc_property (struct target_desc *target_desc,
1697 const char *key, const char *value)
1699 struct property *prop, new_prop;
1702 gdb_assert (key != NULL && value != NULL);
1704 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1706 if (strcmp (prop->key, key) == 0)
1707 internal_error (__FILE__, __LINE__,
1708 _("Attempted to add duplicate property \"%s\""), key);
1710 new_prop.key = xstrdup (key);
1711 new_prop.value = xstrdup (value);
1712 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1716 set_tdesc_architecture (struct target_desc *target_desc,
1717 const struct bfd_arch_info *arch)
1719 target_desc->arch = arch;
1723 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1725 target_desc->osabi = osabi;
1729 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1730 static struct cmd_list_element *tdesc_unset_cmdlist;
1732 /* Helper functions for the CLI commands. */
1735 set_tdesc_cmd (char *args, int from_tty)
1737 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1741 show_tdesc_cmd (char *args, int from_tty)
1743 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1747 unset_tdesc_cmd (char *args, int from_tty)
1749 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1753 set_tdesc_filename_cmd (char *args, int from_tty,
1754 struct cmd_list_element *c)
1756 xfree (target_description_filename);
1757 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1759 target_clear_description ();
1760 target_find_description ();
1764 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1765 struct cmd_list_element *c,
1768 value = target_description_filename;
1770 if (value != NULL && *value != '\0')
1771 printf_filtered (_("The target description will be read from \"%s\".\n"),
1774 printf_filtered (_("The target description will be "
1775 "read from the target.\n"));
1779 unset_tdesc_filename_cmd (char *args, int from_tty)
1781 xfree (target_description_filename);
1782 target_description_filename = NULL;
1783 target_clear_description ();
1784 target_find_description ();
1787 /* Print target description in C. */
1789 class print_c_tdesc : public tdesc_element_visitor
1792 print_c_tdesc (std::string &filename_after_features)
1793 : m_filename_after_features (filename_after_features)
1797 const char *filename = lbasename (m_filename_after_features.c_str ());
1799 m_function = (char *) xmalloc (strlen (filename) + 1);
1800 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1803 else if (*inp == '-')
1809 /* Standard boilerplate. */
1810 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1811 "-*- buffer-read-only: t -*- vi"
1820 void visit_pre (const target_desc *e) override
1822 printf_unfiltered (" Original: %s */\n\n",
1823 lbasename (m_filename_after_features.c_str ()));
1825 printf_unfiltered ("#include \"defs.h\"\n");
1826 printf_unfiltered ("#include \"osabi.h\"\n");
1827 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1828 printf_unfiltered ("\n");
1830 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1831 printf_unfiltered ("static void\n");
1832 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1833 printf_unfiltered ("{\n");
1835 (" struct target_desc *result = allocate_target_description ();\n");
1837 if (tdesc_architecture (e) != NULL)
1840 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1841 tdesc_architecture (e)->printable_name);
1842 printf_unfiltered ("\n");
1844 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1845 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1848 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1849 gdbarch_osabi_name (tdesc_osabi (e)));
1850 printf_unfiltered ("\n");
1854 const struct bfd_arch_info *compatible;
1855 struct property *prop;
1857 for (ix = 0; VEC_iterate (arch_p, e->compatible, ix, compatible);
1861 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1862 compatible->printable_name);
1866 printf_unfiltered ("\n");
1868 for (ix = 0; VEC_iterate (property_s, e->properties, ix, prop);
1871 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1872 prop->key, prop->value);
1874 printf_unfiltered (" struct tdesc_feature *feature;\n");
1877 void visit_pre (const tdesc_feature *e) override
1879 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1883 void visit_post (const tdesc_feature *e) override
1886 void visit_post (const target_desc *e) override
1888 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1889 printf_unfiltered ("}\n");
1892 void visit (const tdesc_type *type) override
1894 struct tdesc_type_field *f;
1896 /* Now we do some "filtering" in order to know which variables to
1897 declare. This is needed because otherwise we would declare unused
1898 variables `field_type' and `type'. */
1899 if (!m_printed_field_type)
1901 printf_unfiltered (" struct tdesc_type *field_type;\n");
1902 m_printed_field_type = true;
1905 if ((type->kind == TDESC_TYPE_UNION
1906 || type->kind == TDESC_TYPE_STRUCT
1907 || type->kind == TDESC_TYPE_FLAGS
1908 || type->kind == TDESC_TYPE_ENUM)
1909 && VEC_length (tdesc_type_field, type->u.u.fields) > 0
1912 printf_unfiltered (" struct tdesc_type *type;\n");
1913 m_printed_type = true;
1918 case TDESC_TYPE_VECTOR:
1920 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1921 type->u.v.type->name);
1923 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1924 type->name, type->u.v.count);
1926 case TDESC_TYPE_STRUCT:
1927 case TDESC_TYPE_FLAGS:
1928 if (type->kind == TDESC_TYPE_STRUCT)
1931 (" type = tdesc_create_struct (feature, \"%s\");\n",
1933 if (type->u.u.size != 0)
1935 (" tdesc_set_struct_size (type, %d);\n",
1941 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1942 type->name, type->u.u.size);
1945 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1948 const char *type_name;
1950 gdb_assert (f->type != NULL);
1951 type_name = f->type->name;
1953 /* To minimize changes to generated files, don't emit type
1954 info for fields that have defaulted types. */
1957 gdb_assert (f->end != -1);
1958 if (f->type->kind == TDESC_TYPE_BOOL)
1960 gdb_assert (f->start == f->end);
1962 (" tdesc_add_flag (type, %d, \"%s\");\n",
1965 else if ((type->u.u.size == 4
1966 && f->type->kind == TDESC_TYPE_UINT32)
1967 || (type->u.u.size == 8
1968 && f->type->kind == TDESC_TYPE_UINT64))
1971 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1972 f->name, f->start, f->end);
1977 (" field_type = tdesc_named_type (feature,"
1981 (" tdesc_add_typed_bitfield (type, \"%s\","
1982 " %d, %d, field_type);\n",
1983 f->name, f->start, f->end);
1986 else /* Not a bitfield. */
1988 gdb_assert (f->end == -1);
1989 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1991 (" field_type = tdesc_named_type (feature,"
1995 (" tdesc_add_field (type, \"%s\", field_type);\n",
2000 case TDESC_TYPE_UNION:
2002 (" type = tdesc_create_union (feature, \"%s\");\n",
2005 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2009 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2012 (" tdesc_add_field (type, \"%s\", field_type);\n",
2016 case TDESC_TYPE_ENUM:
2018 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2019 type->name, type->u.u.size);
2021 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2024 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2028 error (_("C output is not supported type \"%s\"."), type->name);
2030 printf_unfiltered ("\n");
2033 void visit (const tdesc_reg *reg) override
2035 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2036 reg->name, reg->target_regnum, reg->save_restore);
2038 printf_unfiltered ("\"%s\", ", reg->group);
2040 printf_unfiltered ("NULL, ");
2041 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2045 std::string m_filename_after_features;
2049 bool m_printed_field_type = false;
2050 bool m_printed_type = false;
2053 /* Print target description feature in C. */
2055 class print_c_feature : public print_c_tdesc
2058 print_c_feature (std::string &file)
2059 : print_c_tdesc (file)
2062 auto const pos = m_filename_after_features.find_last_of ('.');
2064 m_filename_after_features = m_filename_after_features.substr (0, pos);
2067 void visit_pre (const target_desc *e) override
2069 printf_unfiltered (" Original: %s */\n\n",
2070 lbasename (m_filename_after_features.c_str ()));
2072 printf_unfiltered ("#include \"target-descriptions.h\"\n");
2073 printf_unfiltered ("\n");
2076 void visit_post (const target_desc *e) override
2079 void visit_pre (const tdesc_feature *e) override
2081 std::string name (m_filename_after_features);
2083 auto pos = name.find_first_of ('.');
2085 name = name.substr (0, pos);
2086 std::replace (name.begin (), name.end (), '/', '_');
2087 std::replace (name.begin (), name.end (), '-', '_');
2089 printf_unfiltered ("static int\n");
2090 printf_unfiltered ("create_feature_%s ", name.c_str ());
2091 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2093 printf_unfiltered ("{\n");
2094 printf_unfiltered (" struct tdesc_feature *feature;\n");
2095 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
2099 void visit_post (const tdesc_feature *e) override
2101 printf_unfiltered (" return regnum;\n");
2102 printf_unfiltered ("}\n");
2105 void visit (const tdesc_reg *reg) override
2107 /* Most "reg" in XML target descriptions don't have "regnum"
2108 attribute, so the register number is allocated sequentially.
2109 In case that reg has "regnum" attribute, register number
2110 should be set by that explicitly. */
2112 if (reg->target_regnum < m_next_regnum)
2114 /* The integrity check, it can catch some errors on register
2115 number collision, like this,
2117 <reg name="x0" bitsize="32"/>
2118 <reg name="x1" bitsize="32"/>
2119 <reg name="x2" bitsize="32"/>
2120 <reg name="x3" bitsize="32"/>
2121 <reg name="ps" bitsize="32" regnum="3"/>
2123 but it also has false negatives. The target description
2126 <reg name="x1" bitsize="32" regnum="1"/>
2127 <reg name="x3" bitsize="32" regnum="3"/>
2128 <reg name="x2" bitsize="32" regnum="2"/>
2129 <reg name="x4" bitsize="32" regnum="4"/>
2131 but it is not a good practice, so still error on this,
2132 and also print the message so that it can be saved in the
2133 generated c file. */
2135 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2136 reg->target_regnum);
2137 printf_unfiltered ("is not the largest number (%d).\n",
2139 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2140 reg->target_regnum, m_next_regnum);
2143 if (reg->target_regnum > m_next_regnum)
2145 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2146 m_next_regnum = reg->target_regnum;
2149 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2150 reg->name, reg->save_restore);
2152 printf_unfiltered ("\"%s\", ", reg->group);
2154 printf_unfiltered ("NULL, ");
2155 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2161 /* The register number to use for the next register we see. */
2162 int m_next_regnum = 0;
2166 maint_print_c_tdesc_cmd (char *args, int from_tty)
2168 const struct target_desc *tdesc;
2169 const char *filename;
2173 /* Use the global target-supplied description, not the current
2174 architecture's. This lets a GDB for one architecture generate C
2175 for another architecture's description, even though the gdbarch
2176 initialization code will reject the new description. */
2177 tdesc = current_target_desc;
2178 filename = target_description_filename;
2182 /* Use the target description from the XML file. */
2184 tdesc = file_read_description_xml (filename);
2188 error (_("There is no target description to print."));
2190 if (filename == NULL)
2191 error (_("The current target description did not come from an XML file."));
2193 std::string filename_after_features (filename);
2194 auto loc = filename_after_features.rfind ("/features/");
2196 if (loc != std::string::npos)
2197 filename_after_features = filename_after_features.substr (loc + 10);
2199 /* Print c files for target features instead of target descriptions,
2200 because c files got from target features are more flexible than the
2202 if (startswith (filename_after_features.c_str (), "i386/32bit-"))
2204 print_c_feature v (filename_after_features);
2210 print_c_tdesc v (filename_after_features);
2216 /* Provide a prototype to silence -Wmissing-prototypes. */
2217 extern initialize_file_ftype _initialize_target_descriptions;
2220 _initialize_target_descriptions (void)
2222 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2224 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2225 Set target description specific variables."),
2226 &tdesc_set_cmdlist, "set tdesc ",
2227 0 /* allow-unknown */, &setlist);
2228 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2229 Show target description specific variables."),
2230 &tdesc_show_cmdlist, "show tdesc ",
2231 0 /* allow-unknown */, &showlist);
2232 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2233 Unset target description specific variables."),
2234 &tdesc_unset_cmdlist, "unset tdesc ",
2235 0 /* allow-unknown */, &unsetlist);
2237 add_setshow_filename_cmd ("filename", class_obscure,
2238 &tdesc_filename_cmd_string,
2240 Set the file to read for an XML target description"), _("\
2241 Show the file to read for an XML target description"), _("\
2242 When set, GDB will read the target description from a local\n\
2243 file instead of querying the remote target."),
2244 set_tdesc_filename_cmd,
2245 show_tdesc_filename_cmd,
2246 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2248 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2249 Unset the file to read for an XML target description. When unset,\n\
2250 GDB will read the description from the target."),
2251 &tdesc_unset_cmdlist);
2253 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2254 Print the current target description as a C source file."),
2255 &maintenanceprintlist);