1 /* Target description support for GDB.
3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
26 #include "reggroups.h"
28 #include "target-descriptions.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
34 #include "gdb_obstack.h"
38 /* The interface to visit different elements of target description. */
40 class tdesc_element_visitor
43 virtual void visit_pre (const target_desc *e) = 0;
44 virtual void visit_post (const target_desc *e) = 0;
46 virtual void visit (const tdesc_feature *e) = 0;
47 virtual void visit (const tdesc_type *e) = 0;
48 virtual void visit (const tdesc_reg *e) = 0;
54 virtual void accept (tdesc_element_visitor &v) const = 0;
59 typedef struct property
64 DEF_VEC_O(property_s);
66 /* An individual register from a target description. */
68 typedef struct tdesc_reg : tdesc_element
70 tdesc_reg (struct tdesc_feature *feature, const char *name_,
71 int regnum, int save_restore_, const char *group_,
72 int bitsize_, const char *type_)
73 : name (xstrdup (name_)), target_regnum (regnum),
74 save_restore (save_restore_),
75 group (group_ != NULL ? xstrdup (group_) : NULL),
77 type (type_ != NULL ? xstrdup (type_) : xstrdup ("<unknown>"))
79 /* If the register's type is target-defined, look it up now. We may not
80 have easy access to the containing feature when we want it later. */
81 tdesc_type = tdesc_named_type (feature, type);
91 /* Disable copying. */
92 tdesc_reg (const tdesc_reg &) = delete;
93 tdesc_reg &operator= (const tdesc_reg &) = delete;
95 /* The name of this register. In standard features, it may be
96 recognized by the architecture support code, or it may be purely
100 /* The register number used by this target to refer to this
101 register. This is used for remote p/P packets and to determine
102 the ordering of registers in the remote g/G packets. */
105 /* If this flag is set, GDB should save and restore this register
106 around calls to an inferior function. */
109 /* The name of the register group containing this register, or NULL
110 if the group should be automatically determined from the
111 register's type. If this is "general", "float", or "vector", the
112 corresponding "info" command should display this register's
113 value. It can be an arbitrary string, but should be limited to
114 alphanumeric characters and internal hyphens. Currently other
115 strings are ignored (treated as NULL). */
118 /* The size of the register, in bits. */
121 /* The type of the register. This string corresponds to either
122 a named type from the target description or a predefined
126 /* The target-described type corresponding to TYPE, if found. */
127 struct tdesc_type *tdesc_type;
129 void accept (tdesc_element_visitor &v) const override
135 DEF_VEC_P(tdesc_reg_p);
137 /* A named type from a target description. */
139 typedef struct tdesc_type_field
142 struct tdesc_type *type;
143 /* For non-enum-values, either both are -1 (non-bitfield), or both are
144 not -1 (bitfield). For enum values, start is the value (which could be
148 DEF_VEC_O(tdesc_type_field);
152 /* Predefined types. */
166 TDESC_TYPE_IEEE_SINGLE,
167 TDESC_TYPE_IEEE_DOUBLE,
168 TDESC_TYPE_ARM_FPA_EXT,
171 /* Types defined by a target feature. */
179 typedef struct tdesc_type : tdesc_element
181 tdesc_type (const char *name_, enum tdesc_type_kind kind_)
182 : name (xstrdup (name_)), kind (kind_)
184 memset (&u, 0, sizeof (u));
187 virtual ~tdesc_type ()
191 case TDESC_TYPE_STRUCT:
192 case TDESC_TYPE_UNION:
193 case TDESC_TYPE_FLAGS:
194 case TDESC_TYPE_ENUM:
196 struct tdesc_type_field *f;
200 VEC_iterate (tdesc_type_field, u.u.fields, ix, f);
204 VEC_free (tdesc_type_field, u.u.fields);
211 xfree ((char *) name);
213 /* Disable copying. */
214 tdesc_type (const tdesc_type &) = delete;
215 tdesc_type &operator= (const tdesc_type &) = delete;
217 /* The name of this type. If this type is a built-in type, this is
218 a pointer to a constant string. Otherwise, it's a
219 malloc-allocated string (and thus must be freed). */
222 /* Identify the kind of this type. */
223 enum tdesc_type_kind kind;
225 /* Kind-specific data. */
231 struct tdesc_type *type;
235 /* Struct, union, flags, or enum type. */
238 VEC(tdesc_type_field) *fields;
243 void accept (tdesc_element_visitor &v) const override
249 DEF_VEC_P(tdesc_type_p);
251 /* A feature from a target description. Each feature is a collection
252 of other elements, e.g. registers and types. */
254 typedef struct tdesc_feature : tdesc_element
256 tdesc_feature (const char *name_)
257 : name (xstrdup (name_))
260 virtual ~tdesc_feature ()
262 struct tdesc_reg *reg;
263 struct tdesc_type *type;
266 for (ix = 0; VEC_iterate (tdesc_reg_p, registers, ix, reg); ix++)
268 VEC_free (tdesc_reg_p, registers);
270 for (ix = 0; VEC_iterate (tdesc_type_p, types, ix, type); ix++)
272 VEC_free (tdesc_type_p, types);
277 /* Disable copying. */
278 tdesc_feature (const tdesc_feature &) = delete;
279 tdesc_feature &operator= (const tdesc_feature &) = delete;
281 /* The name of this feature. It may be recognized by the architecture
285 /* The registers associated with this feature. */
286 VEC(tdesc_reg_p) *registers = NULL;
288 /* The types associated with this feature. */
289 VEC(tdesc_type_p) *types = NULL;
291 void accept (tdesc_element_visitor &v) const override
295 struct tdesc_type *type;
298 VEC_iterate (tdesc_type_p, types, ix, type);
302 struct tdesc_reg *reg;
305 VEC_iterate (tdesc_reg_p, registers, ix, reg);
312 DEF_VEC_P(tdesc_feature_p);
314 /* A compatible architecture from a target description. */
315 typedef const struct bfd_arch_info *arch_p;
318 /* A target description. */
320 struct target_desc : tdesc_element
325 virtual ~target_desc ()
327 struct tdesc_feature *feature;
328 struct property *prop;
332 VEC_iterate (tdesc_feature_p, features, ix, feature);
335 VEC_free (tdesc_feature_p, features);
338 VEC_iterate (property_s, properties, ix, prop);
345 VEC_free (property_s, properties);
346 VEC_free (arch_p, compatible);
349 target_desc (const target_desc &) = delete;
350 void operator= (const target_desc &) = delete;
352 /* The architecture reported by the target, if any. */
353 const struct bfd_arch_info *arch = NULL;
355 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
357 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
359 /* The list of compatible architectures reported by the target. */
360 VEC(arch_p) *compatible = NULL;
362 /* Any architecture-specific properties specified by the target. */
363 VEC(property_s) *properties = NULL;
365 /* The features associated with this target. */
366 VEC(tdesc_feature_p) *features = NULL;
368 void accept (tdesc_element_visitor &v) const override
372 struct tdesc_feature *feature;
375 VEC_iterate (tdesc_feature_p, features, ix, feature);
383 /* Per-architecture data associated with a target description. The
384 target description may be shared by multiple architectures, but
385 this data is private to one gdbarch. */
387 typedef struct tdesc_arch_reg
389 struct tdesc_reg *reg;
392 DEF_VEC_O(tdesc_arch_reg);
394 struct tdesc_arch_data
396 /* A list of register/type pairs, indexed by GDB's internal register number.
397 During initialization of the gdbarch this list is used to store
398 registers which the architecture assigns a fixed register number.
399 Registers which are NULL in this array, or off the end, are
400 treated as zero-sized and nameless (i.e. placeholders in the
402 VEC(tdesc_arch_reg) *arch_regs;
404 /* Functions which report the register name, type, and reggroups for
406 gdbarch_register_name_ftype *pseudo_register_name;
407 gdbarch_register_type_ftype *pseudo_register_type;
408 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
411 /* Info about an inferior's target description. There's one of these
412 for each inferior. */
414 struct target_desc_info
416 /* A flag indicating that a description has already been fetched
417 from the target, so it should not be queried again. */
421 /* The description fetched from the target, or NULL if the target
422 did not supply any description. Only valid when
423 target_desc_fetched is set. Only the description initialization
424 code should access this; normally, the description should be
425 accessed through the gdbarch object. */
427 const struct target_desc *tdesc;
429 /* The filename to read a target description from, as set by "set
430 tdesc filename ..." */
435 /* Get the inferior INF's target description info, allocating one on
436 the stop if necessary. */
438 static struct target_desc_info *
439 get_tdesc_info (struct inferior *inf)
441 if (inf->tdesc_info == NULL)
442 inf->tdesc_info = XCNEW (struct target_desc_info);
443 return inf->tdesc_info;
446 /* A handle for architecture-specific data associated with the
447 target description (see struct tdesc_arch_data). */
449 static struct gdbarch_data *tdesc_data;
451 /* See target-descriptions.h. */
454 target_desc_info_from_user_p (struct target_desc_info *info)
456 return info != NULL && info->filename != NULL;
459 /* See target-descriptions.h. */
462 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
464 struct target_desc_info *src = get_tdesc_info (srcinf);
465 struct target_desc_info *dest = get_tdesc_info (destinf);
467 dest->fetched = src->fetched;
468 dest->tdesc = src->tdesc;
469 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
472 /* See target-descriptions.h. */
475 target_desc_info_free (struct target_desc_info *tdesc_info)
477 if (tdesc_info != NULL)
479 xfree (tdesc_info->filename);
484 /* Convenience helper macros. */
486 #define target_desc_fetched \
487 get_tdesc_info (current_inferior ())->fetched
488 #define current_target_desc \
489 get_tdesc_info (current_inferior ())->tdesc
490 #define target_description_filename \
491 get_tdesc_info (current_inferior ())->filename
493 /* The string manipulated by the "set tdesc filename ..." command. */
495 static char *tdesc_filename_cmd_string;
497 /* Fetch the current target's description, and switch the current
498 architecture to one which incorporates that description. */
501 target_find_description (void)
503 /* If we've already fetched a description from the target, don't do
504 it again. This allows a target to fetch the description early,
505 during its to_open or to_create_inferior, if it needs extra
506 information about the target to initialize. */
507 if (target_desc_fetched)
510 /* The current architecture should not have any target description
511 specified. It should have been cleared, e.g. when we
512 disconnected from the previous target. */
513 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
515 /* First try to fetch an XML description from the user-specified
517 current_target_desc = NULL;
518 if (target_description_filename != NULL
519 && *target_description_filename != '\0')
521 = file_read_description_xml (target_description_filename);
523 /* Next try to read the description from the current target using
525 if (current_target_desc == NULL)
526 current_target_desc = target_read_description_xml (¤t_target);
528 /* If that failed try a target-specific hook. */
529 if (current_target_desc == NULL)
530 current_target_desc = target_read_description (¤t_target);
532 /* If a non-NULL description was returned, then update the current
534 if (current_target_desc)
536 struct gdbarch_info info;
538 gdbarch_info_init (&info);
539 info.target_desc = current_target_desc;
540 if (!gdbarch_update_p (info))
541 warning (_("Architecture rejected target-supplied description"));
544 struct tdesc_arch_data *data;
546 data = ((struct tdesc_arch_data *)
547 gdbarch_data (target_gdbarch (), tdesc_data));
548 if (tdesc_has_registers (current_target_desc)
549 && data->arch_regs == NULL)
550 warning (_("Target-supplied registers are not supported "
551 "by the current architecture"));
555 /* Now that we know this description is usable, record that we
557 target_desc_fetched = 1;
560 /* Discard any description fetched from the current target, and switch
561 the current architecture to one with no target description. */
564 target_clear_description (void)
566 struct gdbarch_info info;
568 if (!target_desc_fetched)
571 target_desc_fetched = 0;
572 current_target_desc = NULL;
574 gdbarch_info_init (&info);
575 if (!gdbarch_update_p (info))
576 internal_error (__FILE__, __LINE__,
577 _("Could not remove target-supplied description"));
580 /* Return the global current target description. This should only be
581 used by gdbarch initialization code; most access should be through
582 an existing gdbarch. */
584 const struct target_desc *
585 target_current_description (void)
587 if (target_desc_fetched)
588 return current_target_desc;
593 /* Return non-zero if this target description is compatible
594 with the given BFD architecture. */
597 tdesc_compatible_p (const struct target_desc *target_desc,
598 const struct bfd_arch_info *arch)
600 const struct bfd_arch_info *compat;
603 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
607 || arch->compatible (arch, compat)
608 || compat->compatible (compat, arch))
616 /* Direct accessors for target descriptions. */
618 /* Return the string value of a property named KEY, or NULL if the
619 property was not specified. */
622 tdesc_property (const struct target_desc *target_desc, const char *key)
624 struct property *prop;
627 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
629 if (strcmp (prop->key, key) == 0)
635 /* Return the BFD architecture associated with this target
636 description, or NULL if no architecture was specified. */
638 const struct bfd_arch_info *
639 tdesc_architecture (const struct target_desc *target_desc)
641 return target_desc->arch;
644 /* Return the OSABI associated with this target description, or
645 GDB_OSABI_UNKNOWN if no osabi was specified. */
648 tdesc_osabi (const struct target_desc *target_desc)
650 return target_desc->osabi;
655 /* Return 1 if this target description includes any registers. */
658 tdesc_has_registers (const struct target_desc *target_desc)
661 struct tdesc_feature *feature;
663 if (target_desc == NULL)
667 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
669 if (! VEC_empty (tdesc_reg_p, feature->registers))
675 /* Return the feature with the given name, if present, or NULL if
676 the named feature is not found. */
678 const struct tdesc_feature *
679 tdesc_find_feature (const struct target_desc *target_desc,
683 struct tdesc_feature *feature;
686 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
688 if (strcmp (feature->name, name) == 0)
694 /* Return the name of FEATURE. */
697 tdesc_feature_name (const struct tdesc_feature *feature)
699 return feature->name;
702 /* Predefined types. */
703 static struct tdesc_type tdesc_predefined_types[] =
705 { "bool", TDESC_TYPE_BOOL },
706 { "int8", TDESC_TYPE_INT8 },
707 { "int16", TDESC_TYPE_INT16 },
708 { "int32", TDESC_TYPE_INT32 },
709 { "int64", TDESC_TYPE_INT64 },
710 { "int128", TDESC_TYPE_INT128 },
711 { "uint8", TDESC_TYPE_UINT8 },
712 { "uint16", TDESC_TYPE_UINT16 },
713 { "uint32", TDESC_TYPE_UINT32 },
714 { "uint64", TDESC_TYPE_UINT64 },
715 { "uint128", TDESC_TYPE_UINT128 },
716 { "code_ptr", TDESC_TYPE_CODE_PTR },
717 { "data_ptr", TDESC_TYPE_DATA_PTR },
718 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
719 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
720 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
721 { "i387_ext", TDESC_TYPE_I387_EXT }
724 /* Lookup a predefined type. */
726 static struct tdesc_type *
727 tdesc_predefined_type (enum tdesc_type_kind kind)
730 struct tdesc_type *type;
732 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
733 if (tdesc_predefined_types[ix].kind == kind)
734 return &tdesc_predefined_types[ix];
736 gdb_assert_not_reached ("bad predefined tdesc type");
739 /* Return the type associated with ID in the context of FEATURE, or
743 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
746 struct tdesc_type *type;
748 /* First try target-defined types. */
749 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
750 if (strcmp (type->name, id) == 0)
753 /* Next try the predefined types. */
754 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
755 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
756 return &tdesc_predefined_types[ix];
761 /* Lookup type associated with ID. */
764 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
766 struct tdesc_arch_reg *reg;
767 struct tdesc_arch_data *data;
770 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
771 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
772 for (i = 0; i < num_regs; i++)
774 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
776 && reg->reg->tdesc_type
778 && strcmp (id, reg->reg->tdesc_type->name) == 0)
785 /* Construct, if necessary, and return the GDB type implementing target
786 type TDESC_TYPE for architecture GDBARCH. */
789 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
793 switch (tdesc_type->kind)
795 /* Predefined types. */
796 case TDESC_TYPE_BOOL:
797 return builtin_type (gdbarch)->builtin_bool;
799 case TDESC_TYPE_INT8:
800 return builtin_type (gdbarch)->builtin_int8;
802 case TDESC_TYPE_INT16:
803 return builtin_type (gdbarch)->builtin_int16;
805 case TDESC_TYPE_INT32:
806 return builtin_type (gdbarch)->builtin_int32;
808 case TDESC_TYPE_INT64:
809 return builtin_type (gdbarch)->builtin_int64;
811 case TDESC_TYPE_INT128:
812 return builtin_type (gdbarch)->builtin_int128;
814 case TDESC_TYPE_UINT8:
815 return builtin_type (gdbarch)->builtin_uint8;
817 case TDESC_TYPE_UINT16:
818 return builtin_type (gdbarch)->builtin_uint16;
820 case TDESC_TYPE_UINT32:
821 return builtin_type (gdbarch)->builtin_uint32;
823 case TDESC_TYPE_UINT64:
824 return builtin_type (gdbarch)->builtin_uint64;
826 case TDESC_TYPE_UINT128:
827 return builtin_type (gdbarch)->builtin_uint128;
829 case TDESC_TYPE_CODE_PTR:
830 return builtin_type (gdbarch)->builtin_func_ptr;
832 case TDESC_TYPE_DATA_PTR:
833 return builtin_type (gdbarch)->builtin_data_ptr;
839 type = tdesc_find_type (gdbarch, tdesc_type->name);
843 switch (tdesc_type->kind)
845 case TDESC_TYPE_IEEE_SINGLE:
846 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
847 floatformats_ieee_single);
849 case TDESC_TYPE_IEEE_DOUBLE:
850 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
851 floatformats_ieee_double);
853 case TDESC_TYPE_ARM_FPA_EXT:
854 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
855 floatformats_arm_ext);
857 case TDESC_TYPE_I387_EXT:
858 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
859 floatformats_i387_ext);
861 /* Types defined by a target feature. */
862 case TDESC_TYPE_VECTOR:
864 struct type *type, *field_type;
866 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
867 type = init_vector_type (field_type, tdesc_type->u.v.count);
868 TYPE_NAME (type) = xstrdup (tdesc_type->name);
873 case TDESC_TYPE_STRUCT:
875 struct type *type, *field_type;
876 struct tdesc_type_field *f;
879 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
880 TYPE_NAME (type) = xstrdup (tdesc_type->name);
881 TYPE_TAG_NAME (type) = TYPE_NAME (type);
884 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
887 if (f->start != -1 && f->end != -1)
891 struct type *field_type;
892 int bitsize, total_size;
894 /* This invariant should be preserved while creating types. */
895 gdb_assert (tdesc_type->u.u.size != 0);
897 field_type = tdesc_gdb_type (gdbarch, f->type);
898 else if (tdesc_type->u.u.size > 4)
899 field_type = builtin_type (gdbarch)->builtin_uint64;
901 field_type = builtin_type (gdbarch)->builtin_uint32;
903 fld = append_composite_type_field_raw (type, xstrdup (f->name),
906 /* For little-endian, BITPOS counts from the LSB of
907 the structure and marks the LSB of the field. For
908 big-endian, BITPOS counts from the MSB of the
909 structure and marks the MSB of the field. Either
910 way, it is the number of bits to the "left" of the
911 field. To calculate this in big-endian, we need
912 the total size of the structure. */
913 bitsize = f->end - f->start + 1;
914 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
915 if (gdbarch_bits_big_endian (gdbarch))
916 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
918 SET_FIELD_BITPOS (fld[0], f->start);
919 FIELD_BITSIZE (fld[0]) = bitsize;
923 gdb_assert (f->start == -1 && f->end == -1);
924 field_type = tdesc_gdb_type (gdbarch, f->type);
925 append_composite_type_field (type, xstrdup (f->name),
930 if (tdesc_type->u.u.size != 0)
931 TYPE_LENGTH (type) = tdesc_type->u.u.size;
935 case TDESC_TYPE_UNION:
937 struct type *type, *field_type;
938 struct tdesc_type_field *f;
941 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
942 TYPE_NAME (type) = xstrdup (tdesc_type->name);
945 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
948 field_type = tdesc_gdb_type (gdbarch, f->type);
949 append_composite_type_field (type, xstrdup (f->name), field_type);
951 /* If any of the children of a union are vectors, flag the
952 union as a vector also. This allows e.g. a union of two
953 vector types to show up automatically in "info vector". */
954 if (TYPE_VECTOR (field_type))
955 TYPE_VECTOR (type) = 1;
960 case TDESC_TYPE_FLAGS:
962 struct tdesc_type_field *f;
965 type = arch_flags_type (gdbarch, tdesc_type->name,
966 tdesc_type->u.u.size);
968 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
971 struct type *field_type;
972 int bitsize = f->end - f->start + 1;
974 gdb_assert (f->type != NULL);
975 field_type = tdesc_gdb_type (gdbarch, f->type);
976 append_flags_type_field (type, f->start, bitsize,
977 field_type, f->name);
983 case TDESC_TYPE_ENUM:
985 struct tdesc_type_field *f;
988 type = arch_type (gdbarch, TYPE_CODE_ENUM,
989 tdesc_type->u.u.size, tdesc_type->name);
990 TYPE_UNSIGNED (type) = 1;
992 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
996 = append_composite_type_field_raw (type, xstrdup (f->name),
999 SET_FIELD_BITPOS (fld[0], f->start);
1006 internal_error (__FILE__, __LINE__,
1007 "Type \"%s\" has an unknown kind %d",
1008 tdesc_type->name, tdesc_type->kind);
1012 /* Support for registers from target descriptions. */
1014 /* Construct the per-gdbarch data. */
1017 tdesc_data_init (struct obstack *obstack)
1019 struct tdesc_arch_data *data;
1021 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1025 /* Similar, but for the temporary copy used during architecture
1028 struct tdesc_arch_data *
1029 tdesc_data_alloc (void)
1031 return XCNEW (struct tdesc_arch_data);
1034 /* Free something allocated by tdesc_data_alloc, if it is not going
1035 to be used (for instance if it was unsuitable for the
1039 tdesc_data_cleanup (void *data_untyped)
1041 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1043 VEC_free (tdesc_arch_reg, data->arch_regs);
1047 /* Search FEATURE for a register named NAME. */
1049 static struct tdesc_reg *
1050 tdesc_find_register_early (const struct tdesc_feature *feature,
1054 struct tdesc_reg *reg;
1057 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1059 if (strcasecmp (reg->name, name) == 0)
1065 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1068 tdesc_numbered_register (const struct tdesc_feature *feature,
1069 struct tdesc_arch_data *data,
1070 int regno, const char *name)
1072 struct tdesc_arch_reg arch_reg = { 0 };
1073 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1078 /* Make sure the vector includes a REGNO'th element. */
1079 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1080 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1083 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
1087 /* Search FEATURE for a register named NAME, but do not assign a fixed
1088 register number to it. */
1091 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1094 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1102 /* Search FEATURE for a register whose name is in NAMES and assign
1106 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1107 struct tdesc_arch_data *data,
1108 int regno, const char *const names[])
1112 for (i = 0; names[i] != NULL; i++)
1113 if (tdesc_numbered_register (feature, data, regno, names[i]))
1119 /* Search FEATURE for a register named NAME, and return its size in
1120 bits. The register must exist. */
1123 tdesc_register_size (const struct tdesc_feature *feature,
1126 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1128 gdb_assert (reg != NULL);
1129 return reg->bitsize;
1132 /* Look up a register by its GDB internal register number. */
1134 static struct tdesc_arch_reg *
1135 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1137 struct tdesc_arch_data *data;
1139 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1140 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1141 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
1146 static struct tdesc_reg *
1147 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1149 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1151 return reg? reg->reg : NULL;
1154 /* Return the name of register REGNO, from the target description or
1155 from an architecture-provided pseudo_register_name method. */
1158 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1160 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1161 int num_regs = gdbarch_num_regs (gdbarch);
1162 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1167 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1169 struct tdesc_arch_data *data
1170 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1172 gdb_assert (data->pseudo_register_name != NULL);
1173 return data->pseudo_register_name (gdbarch, regno);
1180 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1182 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1183 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1184 int num_regs = gdbarch_num_regs (gdbarch);
1185 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1187 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1189 struct tdesc_arch_data *data
1190 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1192 gdb_assert (data->pseudo_register_type != NULL);
1193 return data->pseudo_register_type (gdbarch, regno);
1197 /* Return "int0_t", since "void" has a misleading size of one. */
1198 return builtin_type (gdbarch)->builtin_int0;
1200 if (arch_reg->type == NULL)
1202 /* First check for a predefined or target defined type. */
1203 if (reg->tdesc_type)
1204 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1206 /* Next try size-sensitive type shortcuts. */
1207 else if (strcmp (reg->type, "float") == 0)
1209 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1210 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1211 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1212 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1213 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1214 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1217 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1218 reg->name, reg->bitsize);
1219 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1222 else if (strcmp (reg->type, "int") == 0)
1224 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1225 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1226 else if (reg->bitsize == TARGET_CHAR_BIT)
1227 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1228 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1229 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1230 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1231 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1232 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1233 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1234 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1235 /* A bit desperate by this point... */
1236 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1239 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1240 reg->name, reg->bitsize);
1241 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1245 if (arch_reg->type == NULL)
1246 internal_error (__FILE__, __LINE__,
1247 "Register \"%s\" has an unknown type \"%s\"",
1248 reg->name, reg->type);
1251 return arch_reg->type;
1255 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1257 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1260 return reg->target_regnum;
1265 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1266 target description may be classified as general, float, or vector.
1267 Unlike a gdbarch register_reggroup_p method, this function will
1268 return -1 if it does not know; the caller should handle registers
1269 with no specified group.
1271 Arbitrary strings (other than "general", "float", and "vector")
1272 from the description are not used; they cause the register to be
1273 displayed in "info all-registers" but excluded from "info
1274 registers" et al. The names of containing features are also not
1275 used. This might be extended to display registers in some more
1278 The save-restore flag is also implemented here. */
1281 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1282 struct reggroup *reggroup)
1284 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1286 if (reg != NULL && reg->group != NULL)
1288 int general_p = 0, float_p = 0, vector_p = 0;
1290 if (strcmp (reg->group, "general") == 0)
1292 else if (strcmp (reg->group, "float") == 0)
1294 else if (strcmp (reg->group, "vector") == 0)
1297 if (reggroup == float_reggroup)
1300 if (reggroup == vector_reggroup)
1303 if (reggroup == general_reggroup)
1308 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1309 return reg->save_restore;
1314 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1315 group specified go to the default reggroup function and are handled
1319 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1320 struct reggroup *reggroup)
1322 int num_regs = gdbarch_num_regs (gdbarch);
1323 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1326 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1328 struct tdesc_arch_data *data
1329 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1331 if (data->pseudo_register_reggroup_p != NULL)
1332 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1333 /* Otherwise fall through to the default reggroup_p. */
1336 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1340 return default_register_reggroup_p (gdbarch, regno, reggroup);
1343 /* Record architecture-specific functions to call for pseudo-register
1347 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1348 gdbarch_register_name_ftype *pseudo_name)
1350 struct tdesc_arch_data *data
1351 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1353 data->pseudo_register_name = pseudo_name;
1357 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1358 gdbarch_register_type_ftype *pseudo_type)
1360 struct tdesc_arch_data *data
1361 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1363 data->pseudo_register_type = pseudo_type;
1367 set_tdesc_pseudo_register_reggroup_p
1368 (struct gdbarch *gdbarch,
1369 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1371 struct tdesc_arch_data *data
1372 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1374 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1377 /* Update GDBARCH to use the target description for registers. */
1380 tdesc_use_registers (struct gdbarch *gdbarch,
1381 const struct target_desc *target_desc,
1382 struct tdesc_arch_data *early_data)
1384 int num_regs = gdbarch_num_regs (gdbarch);
1386 struct tdesc_feature *feature;
1387 struct tdesc_reg *reg;
1388 struct tdesc_arch_data *data;
1389 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1392 /* We can't use the description for registers if it doesn't describe
1393 any. This function should only be called after validating
1394 registers, so the caller should know that registers are
1396 gdb_assert (tdesc_has_registers (target_desc));
1398 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1399 data->arch_regs = early_data->arch_regs;
1402 /* Build up a set of all registers, so that we can assign register
1403 numbers where needed. The hash table expands as necessary, so
1404 the initial size is arbitrary. */
1405 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1407 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1410 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1413 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1418 /* Remove any registers which were assigned numbers by the
1421 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1424 htab_remove_elt (reg_hash, arch_reg->reg);
1426 /* Assign numbers to the remaining registers and add them to the
1427 list of registers. The new numbers are always above gdbarch_num_regs.
1428 Iterate over the features, not the hash table, so that the order
1429 matches that in the target description. */
1431 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1432 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1433 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1435 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1438 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1440 if (htab_find (reg_hash, reg) != NULL)
1442 new_arch_reg.reg = reg;
1443 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1447 htab_delete (reg_hash);
1449 /* Update the architecture. */
1450 set_gdbarch_num_regs (gdbarch, num_regs);
1451 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1452 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1453 set_gdbarch_remote_register_number (gdbarch,
1454 tdesc_remote_register_number);
1455 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1460 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1461 int regnum, int save_restore, const char *group,
1462 int bitsize, const char *type)
1464 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1465 group, bitsize, type);
1467 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1471 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1472 struct tdesc_type *field_type, int count)
1474 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1476 type->u.v.type = field_type;
1477 type->u.v.count = count;
1479 VEC_safe_push (tdesc_type_p, feature->types, type);
1484 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1486 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1488 VEC_safe_push (tdesc_type_p, feature->types, type);
1492 /* Set the total length of TYPE. Structs which contain bitfields may
1493 omit the reserved bits, so the end of the last field may not
1497 tdesc_set_struct_size (struct tdesc_type *type, int size)
1499 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1500 gdb_assert (size > 0);
1501 type->u.u.size = size;
1505 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1507 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1509 VEC_safe_push (tdesc_type_p, feature->types, type);
1514 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1517 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1519 gdb_assert (size > 0);
1521 type->u.u.size = size;
1523 VEC_safe_push (tdesc_type_p, feature->types, type);
1528 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1531 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1533 gdb_assert (size > 0);
1535 type->u.u.size = size;
1537 VEC_safe_push (tdesc_type_p, feature->types, type);
1541 /* Add a new field to TYPE. */
1544 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1545 struct tdesc_type *field_type)
1547 struct tdesc_type_field f = { 0 };
1549 gdb_assert (type->kind == TDESC_TYPE_UNION
1550 || type->kind == TDESC_TYPE_STRUCT);
1552 f.name = xstrdup (field_name);
1553 f.type = field_type;
1554 /* Initialize these values so we know this is not a bit-field
1555 when we print-c-tdesc. */
1559 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1562 /* Add a new typed bitfield to TYPE. */
1565 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1566 int start, int end, struct tdesc_type *field_type)
1568 struct tdesc_type_field f = { 0 };
1570 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1571 || type->kind == TDESC_TYPE_FLAGS);
1572 gdb_assert (start >= 0 && end >= start);
1574 f.name = xstrdup (field_name);
1577 f.type = field_type;
1579 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1582 /* Add a new untyped bitfield to TYPE.
1583 Untyped bitfields become either uint32 or uint64 depending on the size
1584 of the underlying type. */
1587 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1590 struct tdesc_type *field_type;
1592 gdb_assert (start >= 0 && end >= start);
1594 if (type->u.u.size > 4)
1595 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1597 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1599 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1602 /* A flag is just a typed(bool) single-bit bitfield.
1603 This function is kept to minimize changes in generated files. */
1606 tdesc_add_flag (struct tdesc_type *type, int start,
1607 const char *flag_name)
1609 struct tdesc_type_field f = { 0 };
1611 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1612 || type->kind == TDESC_TYPE_STRUCT);
1614 f.name = xstrdup (flag_name);
1617 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
1619 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1623 tdesc_add_enum_value (struct tdesc_type *type, int value,
1626 struct tdesc_type_field f = { 0 };
1628 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1630 f.name = xstrdup (name);
1633 f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1635 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1638 struct tdesc_feature *
1639 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1641 struct tdesc_feature *new_feature = new tdesc_feature (name);
1643 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1647 struct target_desc *
1648 allocate_target_description (void)
1650 return new target_desc ();
1654 free_target_description (void *arg)
1656 struct target_desc *target_desc = (struct target_desc *) arg;
1662 make_cleanup_free_target_description (struct target_desc *target_desc)
1664 return make_cleanup (free_target_description, target_desc);
1668 tdesc_add_compatible (struct target_desc *target_desc,
1669 const struct bfd_arch_info *compatible)
1671 const struct bfd_arch_info *compat;
1674 /* If this instance of GDB is compiled without BFD support for the
1675 compatible architecture, simply ignore it -- we would not be able
1676 to handle it anyway. */
1677 if (compatible == NULL)
1680 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1682 if (compat == compatible)
1683 internal_error (__FILE__, __LINE__,
1684 _("Attempted to add duplicate "
1685 "compatible architecture \"%s\""),
1686 compatible->printable_name);
1688 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1692 set_tdesc_property (struct target_desc *target_desc,
1693 const char *key, const char *value)
1695 struct property *prop, new_prop;
1698 gdb_assert (key != NULL && value != NULL);
1700 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1702 if (strcmp (prop->key, key) == 0)
1703 internal_error (__FILE__, __LINE__,
1704 _("Attempted to add duplicate property \"%s\""), key);
1706 new_prop.key = xstrdup (key);
1707 new_prop.value = xstrdup (value);
1708 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1712 set_tdesc_architecture (struct target_desc *target_desc,
1713 const struct bfd_arch_info *arch)
1715 target_desc->arch = arch;
1719 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1721 target_desc->osabi = osabi;
1725 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1726 static struct cmd_list_element *tdesc_unset_cmdlist;
1728 /* Helper functions for the CLI commands. */
1731 set_tdesc_cmd (char *args, int from_tty)
1733 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1737 show_tdesc_cmd (char *args, int from_tty)
1739 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1743 unset_tdesc_cmd (char *args, int from_tty)
1745 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1749 set_tdesc_filename_cmd (char *args, int from_tty,
1750 struct cmd_list_element *c)
1752 xfree (target_description_filename);
1753 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1755 target_clear_description ();
1756 target_find_description ();
1760 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1761 struct cmd_list_element *c,
1764 value = target_description_filename;
1766 if (value != NULL && *value != '\0')
1767 printf_filtered (_("The target description will be read from \"%s\".\n"),
1770 printf_filtered (_("The target description will be "
1771 "read from the target.\n"));
1775 unset_tdesc_filename_cmd (char *args, int from_tty)
1777 xfree (target_description_filename);
1778 target_description_filename = NULL;
1779 target_clear_description ();
1780 target_find_description ();
1783 /* Print target description in C. */
1785 class print_c_tdesc : public tdesc_element_visitor
1788 print_c_tdesc (std::string &filename_after_features)
1789 : m_filename_after_features (filename_after_features)
1793 const char *filename = lbasename (m_filename_after_features.c_str ());
1795 m_function = (char *) xmalloc (strlen (filename) + 1);
1796 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1799 else if (*inp == '-')
1805 /* Standard boilerplate. */
1806 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1807 "-*- buffer-read-only: t -*- vi"
1809 printf_unfiltered (" Original: %s */\n\n", filename);
1817 void visit_pre (const target_desc *e) override
1819 printf_unfiltered ("#include \"defs.h\"\n");
1820 printf_unfiltered ("#include \"osabi.h\"\n");
1821 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1822 printf_unfiltered ("\n");
1824 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1825 printf_unfiltered ("static void\n");
1826 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1827 printf_unfiltered ("{\n");
1829 (" struct target_desc *result = allocate_target_description ();\n");
1831 if (tdesc_architecture (e) != NULL)
1834 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1835 tdesc_architecture (e)->printable_name);
1836 printf_unfiltered ("\n");
1838 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1839 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1842 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1843 gdbarch_osabi_name (tdesc_osabi (e)));
1844 printf_unfiltered ("\n");
1848 const struct bfd_arch_info *compatible;
1849 struct property *prop;
1851 for (ix = 0; VEC_iterate (arch_p, e->compatible, ix, compatible);
1855 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1856 compatible->printable_name);
1860 printf_unfiltered ("\n");
1862 for (ix = 0; VEC_iterate (property_s, e->properties, ix, prop);
1865 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1866 prop->key, prop->value);
1868 printf_unfiltered (" struct tdesc_feature *feature;\n");
1871 void visit (const tdesc_feature *e) override
1873 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1877 void visit_post (const target_desc *e) override
1879 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1880 printf_unfiltered ("}\n");
1883 void visit (const tdesc_type *type) override
1885 struct tdesc_type_field *f;
1887 /* Now we do some "filtering" in order to know which variables to
1888 declare. This is needed because otherwise we would declare unused
1889 variables `field_type' and `type'. */
1890 if (!m_printed_field_type)
1892 printf_unfiltered (" struct tdesc_type *field_type;\n");
1893 m_printed_field_type = true;
1896 if ((type->kind == TDESC_TYPE_UNION
1897 || type->kind == TDESC_TYPE_STRUCT
1898 || type->kind == TDESC_TYPE_FLAGS
1899 || type->kind == TDESC_TYPE_ENUM)
1900 && VEC_length (tdesc_type_field, type->u.u.fields) > 0
1903 printf_unfiltered (" struct tdesc_type *type;\n");
1904 m_printed_type = true;
1909 case TDESC_TYPE_VECTOR:
1911 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1912 type->u.v.type->name);
1914 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1915 type->name, type->u.v.count);
1917 case TDESC_TYPE_STRUCT:
1918 case TDESC_TYPE_FLAGS:
1919 if (type->kind == TDESC_TYPE_STRUCT)
1922 (" type = tdesc_create_struct (feature, \"%s\");\n",
1924 if (type->u.u.size != 0)
1926 (" tdesc_set_struct_size (type, %d);\n",
1932 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1933 type->name, type->u.u.size);
1936 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1939 const char *type_name;
1941 gdb_assert (f->type != NULL);
1942 type_name = f->type->name;
1944 /* To minimize changes to generated files, don't emit type
1945 info for fields that have defaulted types. */
1948 gdb_assert (f->end != -1);
1949 if (f->type->kind == TDESC_TYPE_BOOL)
1951 gdb_assert (f->start == f->end);
1953 (" tdesc_add_flag (type, %d, \"%s\");\n",
1956 else if ((type->u.u.size == 4
1957 && f->type->kind == TDESC_TYPE_UINT32)
1958 || (type->u.u.size == 8
1959 && f->type->kind == TDESC_TYPE_UINT64))
1962 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1963 f->name, f->start, f->end);
1968 (" field_type = tdesc_named_type (feature,"
1972 (" tdesc_add_typed_bitfield (type, \"%s\","
1973 " %d, %d, field_type);\n",
1974 f->name, f->start, f->end);
1977 else /* Not a bitfield. */
1979 gdb_assert (f->end == -1);
1980 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1982 (" field_type = tdesc_named_type (feature,"
1986 (" tdesc_add_field (type, \"%s\", field_type);\n",
1991 case TDESC_TYPE_UNION:
1993 (" type = tdesc_create_union (feature, \"%s\");\n",
1996 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2000 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2003 (" tdesc_add_field (type, \"%s\", field_type);\n",
2007 case TDESC_TYPE_ENUM:
2009 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2010 type->name, type->u.u.size);
2012 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2015 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2019 error (_("C output is not supported type \"%s\"."), type->name);
2021 printf_unfiltered ("\n");
2024 void visit (const tdesc_reg *reg) override
2026 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2027 reg->name, reg->target_regnum, reg->save_restore);
2029 printf_unfiltered ("\"%s\", ", reg->group);
2031 printf_unfiltered ("NULL, ");
2032 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2037 std::string m_filename_after_features;
2038 bool m_printed_field_type = false;
2039 bool m_printed_type = false;
2043 maint_print_c_tdesc_cmd (char *args, int from_tty)
2045 const struct target_desc *tdesc;
2046 const char *filename;
2050 /* Use the global target-supplied description, not the current
2051 architecture's. This lets a GDB for one architecture generate C
2052 for another architecture's description, even though the gdbarch
2053 initialization code will reject the new description. */
2054 tdesc = current_target_desc;
2055 filename = target_description_filename;
2059 /* Use the target description from the XML file. */
2061 tdesc = file_read_description_xml (filename);
2065 error (_("There is no target description to print."));
2067 if (filename == NULL)
2068 error (_("The current target description did not come from an XML file."));
2070 std::string filename_after_features (filename);
2071 auto loc = filename_after_features.rfind ("/features/");
2073 if (loc != std::string::npos)
2074 filename_after_features = filename_after_features.substr (loc + 10);
2076 print_c_tdesc v (filename_after_features);
2081 /* Provide a prototype to silence -Wmissing-prototypes. */
2082 extern initialize_file_ftype _initialize_target_descriptions;
2085 _initialize_target_descriptions (void)
2087 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2089 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2090 Set target description specific variables."),
2091 &tdesc_set_cmdlist, "set tdesc ",
2092 0 /* allow-unknown */, &setlist);
2093 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2094 Show target description specific variables."),
2095 &tdesc_show_cmdlist, "show tdesc ",
2096 0 /* allow-unknown */, &showlist);
2097 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2098 Unset target description specific variables."),
2099 &tdesc_unset_cmdlist, "unset tdesc ",
2100 0 /* allow-unknown */, &unsetlist);
2102 add_setshow_filename_cmd ("filename", class_obscure,
2103 &tdesc_filename_cmd_string,
2105 Set the file to read for an XML target description"), _("\
2106 Show the file to read for an XML target description"), _("\
2107 When set, GDB will read the target description from a local\n\
2108 file instead of querying the remote target."),
2109 set_tdesc_filename_cmd,
2110 show_tdesc_filename_cmd,
2111 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2113 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2114 Unset the file to read for an XML target description. When unset,\n\
2115 GDB will read the description from the target."),
2116 &tdesc_unset_cmdlist);
2118 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2119 Print the current target description as a C source file."),
2120 &maintenanceprintlist);