1 /* Target description support for GDB.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by CodeSourcery.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "arch-utils.h"
27 #include "reggroups.h"
29 #include "target-descriptions.h"
31 #include "xml-support.h"
32 #include "xml-tdesc.h"
35 #include "gdb_assert.h"
36 #include "gdb_obstack.h"
41 typedef struct property
46 DEF_VEC_O(property_s);
48 /* An individual register from a target description. */
50 typedef struct tdesc_reg
52 /* The name of this register. In standard features, it may be
53 recognized by the architecture support code, or it may be purely
57 /* The register number used by this target to refer to this
58 register. This is used for remote p/P packets and to determine
59 the ordering of registers in the remote g/G packets. */
62 /* If this flag is set, GDB should save and restore this register
63 around calls to an inferior function. */
66 /* The name of the register group containing this register, or NULL
67 if the group should be automatically determined from the
68 register's type. If this is "general", "float", or "vector", the
69 corresponding "info" command should display this register's
70 value. It can be an arbitrary string, but should be limited to
71 alphanumeric characters and internal hyphens. Currently other
72 strings are ignored (treated as NULL). */
75 /* The size of the register, in bits. */
78 /* The type of the register. This string corresponds to either
79 a named type from the target description or a predefined
83 /* The target-described type corresponding to TYPE, if found. */
84 struct tdesc_type *tdesc_type;
86 DEF_VEC_P(tdesc_reg_p);
88 /* A named type from a target description. */
90 typedef struct tdesc_type_field
93 struct tdesc_type *type;
96 DEF_VEC_O(tdesc_type_field);
98 typedef struct tdesc_type_flag
103 DEF_VEC_O(tdesc_type_flag);
105 typedef struct tdesc_type
107 /* The name of this type. */
110 /* Identify the kind of this type. */
113 /* Predefined types. */
126 TDESC_TYPE_IEEE_SINGLE,
127 TDESC_TYPE_IEEE_DOUBLE,
128 TDESC_TYPE_ARM_FPA_EXT,
131 /* Types defined by a target feature. */
138 /* Kind-specific data. */
144 struct tdesc_type *type;
148 /* Struct or union type. */
151 VEC(tdesc_type_field) *fields;
158 VEC(tdesc_type_flag) *flags;
163 DEF_VEC_P(tdesc_type_p);
165 /* A feature from a target description. Each feature is a collection
166 of other elements, e.g. registers and types. */
168 typedef struct tdesc_feature
170 /* The name of this feature. It may be recognized by the architecture
174 /* The registers associated with this feature. */
175 VEC(tdesc_reg_p) *registers;
177 /* The types associated with this feature. */
178 VEC(tdesc_type_p) *types;
180 DEF_VEC_P(tdesc_feature_p);
182 /* A compatible architecture from a target description. */
183 typedef const struct bfd_arch_info *arch_p;
186 /* A target description. */
190 /* The architecture reported by the target, if any. */
191 const struct bfd_arch_info *arch;
193 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
195 enum gdb_osabi osabi;
197 /* The list of compatible architectures reported by the target. */
198 VEC(arch_p) *compatible;
200 /* Any architecture-specific properties specified by the target. */
201 VEC(property_s) *properties;
203 /* The features associated with this target. */
204 VEC(tdesc_feature_p) *features;
207 /* Per-architecture data associated with a target description. The
208 target description may be shared by multiple architectures, but
209 this data is private to one gdbarch. */
211 typedef struct tdesc_arch_reg
213 struct tdesc_reg *reg;
216 DEF_VEC_O(tdesc_arch_reg);
218 struct tdesc_arch_data
220 /* A list of register/type pairs, indexed by GDB's internal register number.
221 During initialization of the gdbarch this list is used to store
222 registers which the architecture assigns a fixed register number.
223 Registers which are NULL in this array, or off the end, are
224 treated as zero-sized and nameless (i.e. placeholders in the
226 VEC(tdesc_arch_reg) *arch_regs;
228 /* Functions which report the register name, type, and reggroups for
230 gdbarch_register_name_ftype *pseudo_register_name;
231 gdbarch_register_type_ftype *pseudo_register_type;
232 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
235 /* Global state. These variables are associated with the current
236 target; if GDB adds support for multiple simultaneous targets, then
237 these variables should become target-specific data. */
239 /* A flag indicating that a description has already been fetched from
240 the current target, so it should not be queried again. */
242 static int target_desc_fetched;
244 /* The description fetched from the current target, or NULL if the
245 current target did not supply any description. Only valid when
246 target_desc_fetched is set. Only the description initialization
247 code should access this; normally, the description should be
248 accessed through the gdbarch object. */
250 static const struct target_desc *current_target_desc;
252 /* Other global variables. */
254 /* The filename to read a target description from. */
256 static char *target_description_filename;
258 /* A handle for architecture-specific data associated with the
259 target description (see struct tdesc_arch_data). */
261 static struct gdbarch_data *tdesc_data;
263 /* Fetch the current target's description, and switch the current
264 architecture to one which incorporates that description. */
267 target_find_description (void)
269 /* If we've already fetched a description from the target, don't do
270 it again. This allows a target to fetch the description early,
271 during its to_open or to_create_inferior, if it needs extra
272 information about the target to initialize. */
273 if (target_desc_fetched)
276 /* The current architecture should not have any target description
277 specified. It should have been cleared, e.g. when we
278 disconnected from the previous target. */
279 gdb_assert (gdbarch_target_desc (target_gdbarch) == NULL);
281 /* First try to fetch an XML description from the user-specified
283 current_target_desc = NULL;
284 if (target_description_filename != NULL
285 && *target_description_filename != '\0')
287 = file_read_description_xml (target_description_filename);
289 /* Next try to read the description from the current target using
291 if (current_target_desc == NULL)
292 current_target_desc = target_read_description_xml (¤t_target);
294 /* If that failed try a target-specific hook. */
295 if (current_target_desc == NULL)
296 current_target_desc = target_read_description (¤t_target);
298 /* If a non-NULL description was returned, then update the current
300 if (current_target_desc)
302 struct gdbarch_info info;
304 gdbarch_info_init (&info);
305 info.target_desc = current_target_desc;
306 if (!gdbarch_update_p (info))
307 warning (_("Architecture rejected target-supplied description"));
310 struct tdesc_arch_data *data;
312 data = gdbarch_data (target_gdbarch, tdesc_data);
313 if (tdesc_has_registers (current_target_desc)
314 && data->arch_regs == NULL)
315 warning (_("Target-supplied registers are not supported "
316 "by the current architecture"));
320 /* Now that we know this description is usable, record that we
322 target_desc_fetched = 1;
325 /* Discard any description fetched from the current target, and switch
326 the current architecture to one with no target description. */
329 target_clear_description (void)
331 struct gdbarch_info info;
333 if (!target_desc_fetched)
336 target_desc_fetched = 0;
337 current_target_desc = NULL;
339 gdbarch_info_init (&info);
340 if (!gdbarch_update_p (info))
341 internal_error (__FILE__, __LINE__,
342 _("Could not remove target-supplied description"));
345 /* Return the global current target description. This should only be
346 used by gdbarch initialization code; most access should be through
347 an existing gdbarch. */
349 const struct target_desc *
350 target_current_description (void)
352 if (target_desc_fetched)
353 return current_target_desc;
358 /* Return non-zero if this target description is compatible
359 with the given BFD architecture. */
362 tdesc_compatible_p (const struct target_desc *target_desc,
363 const struct bfd_arch_info *arch)
365 const struct bfd_arch_info *compat;
368 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
372 || arch->compatible (arch, compat)
373 || compat->compatible (compat, arch))
381 /* Direct accessors for target descriptions. */
383 /* Return the string value of a property named KEY, or NULL if the
384 property was not specified. */
387 tdesc_property (const struct target_desc *target_desc, const char *key)
389 struct property *prop;
392 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
394 if (strcmp (prop->key, key) == 0)
400 /* Return the BFD architecture associated with this target
401 description, or NULL if no architecture was specified. */
403 const struct bfd_arch_info *
404 tdesc_architecture (const struct target_desc *target_desc)
406 return target_desc->arch;
409 /* Return the OSABI associated with this target description, or
410 GDB_OSABI_UNKNOWN if no osabi was specified. */
413 tdesc_osabi (const struct target_desc *target_desc)
415 return target_desc->osabi;
420 /* Return 1 if this target description includes any registers. */
423 tdesc_has_registers (const struct target_desc *target_desc)
426 struct tdesc_feature *feature;
428 if (target_desc == NULL)
432 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
434 if (! VEC_empty (tdesc_reg_p, feature->registers))
440 /* Return the feature with the given name, if present, or NULL if
441 the named feature is not found. */
443 const struct tdesc_feature *
444 tdesc_find_feature (const struct target_desc *target_desc,
448 struct tdesc_feature *feature;
451 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
453 if (strcmp (feature->name, name) == 0)
459 /* Return the name of FEATURE. */
462 tdesc_feature_name (const struct tdesc_feature *feature)
464 return feature->name;
467 /* Predefined types. */
468 static struct tdesc_type tdesc_predefined_types[] =
470 { "int8", TDESC_TYPE_INT8 },
471 { "int16", TDESC_TYPE_INT16 },
472 { "int32", TDESC_TYPE_INT32 },
473 { "int64", TDESC_TYPE_INT64 },
474 { "int128", TDESC_TYPE_INT128 },
475 { "uint8", TDESC_TYPE_UINT8 },
476 { "uint16", TDESC_TYPE_UINT16 },
477 { "uint32", TDESC_TYPE_UINT32 },
478 { "uint64", TDESC_TYPE_UINT64 },
479 { "uint128", TDESC_TYPE_UINT128 },
480 { "code_ptr", TDESC_TYPE_CODE_PTR },
481 { "data_ptr", TDESC_TYPE_DATA_PTR },
482 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
483 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
484 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
485 { "i387_ext", TDESC_TYPE_I387_EXT }
488 /* Return the type associated with ID in the context of FEATURE, or
492 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
495 struct tdesc_type *type;
497 /* First try target-defined types. */
498 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
499 if (strcmp (type->name, id) == 0)
502 /* Next try the predefined types. */
503 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
504 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
505 return &tdesc_predefined_types[ix];
510 /* Lookup type associated with ID. */
513 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
515 struct tdesc_arch_reg *reg;
516 struct tdesc_arch_data *data;
519 data = gdbarch_data (gdbarch, tdesc_data);
520 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
521 for (i = 0; i < num_regs; i++)
523 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
525 && reg->reg->tdesc_type
527 && strcmp (id, reg->reg->tdesc_type->name) == 0)
534 /* Construct, if necessary, and return the GDB type implementing target
535 type TDESC_TYPE for architecture GDBARCH. */
538 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
542 switch (tdesc_type->kind)
544 /* Predefined types. */
545 case TDESC_TYPE_INT8:
546 return builtin_type (gdbarch)->builtin_int8;
548 case TDESC_TYPE_INT16:
549 return builtin_type (gdbarch)->builtin_int16;
551 case TDESC_TYPE_INT32:
552 return builtin_type (gdbarch)->builtin_int32;
554 case TDESC_TYPE_INT64:
555 return builtin_type (gdbarch)->builtin_int64;
557 case TDESC_TYPE_INT128:
558 return builtin_type (gdbarch)->builtin_int128;
560 case TDESC_TYPE_UINT8:
561 return builtin_type (gdbarch)->builtin_uint8;
563 case TDESC_TYPE_UINT16:
564 return builtin_type (gdbarch)->builtin_uint16;
566 case TDESC_TYPE_UINT32:
567 return builtin_type (gdbarch)->builtin_uint32;
569 case TDESC_TYPE_UINT64:
570 return builtin_type (gdbarch)->builtin_uint64;
572 case TDESC_TYPE_UINT128:
573 return builtin_type (gdbarch)->builtin_uint128;
575 case TDESC_TYPE_CODE_PTR:
576 return builtin_type (gdbarch)->builtin_func_ptr;
578 case TDESC_TYPE_DATA_PTR:
579 return builtin_type (gdbarch)->builtin_data_ptr;
585 type = tdesc_find_type (gdbarch, tdesc_type->name);
589 switch (tdesc_type->kind)
591 case TDESC_TYPE_IEEE_SINGLE:
592 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
593 floatformats_ieee_single);
595 case TDESC_TYPE_IEEE_DOUBLE:
596 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
597 floatformats_ieee_double);
599 case TDESC_TYPE_ARM_FPA_EXT:
600 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
601 floatformats_arm_ext);
603 case TDESC_TYPE_I387_EXT:
604 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
605 floatformats_i387_ext);
607 /* Types defined by a target feature. */
608 case TDESC_TYPE_VECTOR:
610 struct type *type, *field_type;
612 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
613 type = init_vector_type (field_type, tdesc_type->u.v.count);
614 TYPE_NAME (type) = xstrdup (tdesc_type->name);
619 case TDESC_TYPE_STRUCT:
621 struct type *type, *field_type;
622 struct tdesc_type_field *f;
625 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
626 TYPE_NAME (type) = xstrdup (tdesc_type->name);
627 TYPE_TAG_NAME (type) = TYPE_NAME (type);
630 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
637 struct type *field_type;
638 int bitsize, total_size;
640 /* This invariant should be preserved while creating
642 gdb_assert (tdesc_type->u.u.size != 0);
643 if (tdesc_type->u.u.size > 4)
644 field_type = builtin_type (gdbarch)->builtin_uint64;
646 field_type = builtin_type (gdbarch)->builtin_uint32;
648 fld = append_composite_type_field_raw (type, xstrdup (f->name),
651 /* For little-endian, BITPOS counts from the LSB of
652 the structure and marks the LSB of the field. For
653 big-endian, BITPOS counts from the MSB of the
654 structure and marks the MSB of the field. Either
655 way, it is the number of bits to the "left" of the
656 field. To calculate this in big-endian, we need
657 the total size of the structure. */
658 bitsize = f->end - f->start + 1;
659 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
660 if (gdbarch_bits_big_endian (gdbarch))
661 FIELD_BITPOS (fld[0]) = total_size - f->start - bitsize;
663 FIELD_BITPOS (fld[0]) = f->start;
664 FIELD_BITSIZE (fld[0]) = bitsize;
668 field_type = tdesc_gdb_type (gdbarch, f->type);
669 append_composite_type_field (type, xstrdup (f->name),
674 if (tdesc_type->u.u.size != 0)
675 TYPE_LENGTH (type) = tdesc_type->u.u.size;
679 case TDESC_TYPE_UNION:
681 struct type *type, *field_type;
682 struct tdesc_type_field *f;
685 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
686 TYPE_NAME (type) = xstrdup (tdesc_type->name);
689 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
692 field_type = tdesc_gdb_type (gdbarch, f->type);
693 append_composite_type_field (type, xstrdup (f->name), field_type);
695 /* If any of the children of a union are vectors, flag the
696 union as a vector also. This allows e.g. a union of two
697 vector types to show up automatically in "info vector". */
698 if (TYPE_VECTOR (field_type))
699 TYPE_VECTOR (type) = 1;
704 case TDESC_TYPE_FLAGS:
706 struct tdesc_type_flag *f;
709 type = arch_flags_type (gdbarch, xstrdup (tdesc_type->name),
710 tdesc_type->u.f.size);
712 VEC_iterate (tdesc_type_flag, tdesc_type->u.f.flags, ix, f);
714 /* Note that contrary to the function name, this call will
715 just set the properties of an already-allocated
717 append_flags_type_flag (type, f->start,
718 *f->name ? f->name : NULL);
724 internal_error (__FILE__, __LINE__,
725 "Type \"%s\" has an unknown kind %d",
726 tdesc_type->name, tdesc_type->kind);
730 /* Support for registers from target descriptions. */
732 /* Construct the per-gdbarch data. */
735 tdesc_data_init (struct obstack *obstack)
737 struct tdesc_arch_data *data;
739 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
743 /* Similar, but for the temporary copy used during architecture
746 struct tdesc_arch_data *
747 tdesc_data_alloc (void)
749 return XZALLOC (struct tdesc_arch_data);
752 /* Free something allocated by tdesc_data_alloc, if it is not going
753 to be used (for instance if it was unsuitable for the
757 tdesc_data_cleanup (void *data_untyped)
759 struct tdesc_arch_data *data = data_untyped;
761 VEC_free (tdesc_arch_reg, data->arch_regs);
765 /* Search FEATURE for a register named NAME. */
767 static struct tdesc_reg *
768 tdesc_find_register_early (const struct tdesc_feature *feature,
772 struct tdesc_reg *reg;
775 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
777 if (strcasecmp (reg->name, name) == 0)
783 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
786 tdesc_numbered_register (const struct tdesc_feature *feature,
787 struct tdesc_arch_data *data,
788 int regno, const char *name)
790 struct tdesc_arch_reg arch_reg = { 0 };
791 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
796 /* Make sure the vector includes a REGNO'th element. */
797 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
798 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
801 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
805 /* Search FEATURE for a register named NAME, but do not assign a fixed
806 register number to it. */
809 tdesc_unnumbered_register (const struct tdesc_feature *feature,
812 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
820 /* Search FEATURE for a register whose name is in NAMES and assign
824 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
825 struct tdesc_arch_data *data,
826 int regno, const char *const names[])
830 for (i = 0; names[i] != NULL; i++)
831 if (tdesc_numbered_register (feature, data, regno, names[i]))
837 /* Search FEATURE for a register named NAME, and return its size in
838 bits. The register must exist. */
841 tdesc_register_size (const struct tdesc_feature *feature,
844 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
846 gdb_assert (reg != NULL);
850 /* Look up a register by its GDB internal register number. */
852 static struct tdesc_arch_reg *
853 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
855 struct tdesc_arch_data *data;
857 data = gdbarch_data (gdbarch, tdesc_data);
858 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
859 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
864 static struct tdesc_reg *
865 tdesc_find_register (struct gdbarch *gdbarch, int regno)
867 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
869 return reg? reg->reg : NULL;
872 /* Return the name of register REGNO, from the target description or
873 from an architecture-provided pseudo_register_name method. */
876 tdesc_register_name (struct gdbarch *gdbarch, int regno)
878 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
879 int num_regs = gdbarch_num_regs (gdbarch);
880 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
885 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
887 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
889 gdb_assert (data->pseudo_register_name != NULL);
890 return data->pseudo_register_name (gdbarch, regno);
897 tdesc_register_type (struct gdbarch *gdbarch, int regno)
899 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
900 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
901 int num_regs = gdbarch_num_regs (gdbarch);
902 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
904 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
906 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
908 gdb_assert (data->pseudo_register_type != NULL);
909 return data->pseudo_register_type (gdbarch, regno);
913 /* Return "int0_t", since "void" has a misleading size of one. */
914 return builtin_type (gdbarch)->builtin_int0;
916 if (arch_reg->type == NULL)
918 /* First check for a predefined or target defined type. */
920 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
922 /* Next try size-sensitive type shortcuts. */
923 else if (strcmp (reg->type, "float") == 0)
925 if (reg->bitsize == gdbarch_float_bit (gdbarch))
926 arch_reg->type = builtin_type (gdbarch)->builtin_float;
927 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
928 arch_reg->type = builtin_type (gdbarch)->builtin_double;
929 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
930 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
933 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
934 reg->name, reg->bitsize);
935 arch_reg->type = builtin_type (gdbarch)->builtin_double;
938 else if (strcmp (reg->type, "int") == 0)
940 if (reg->bitsize == gdbarch_long_bit (gdbarch))
941 arch_reg->type = builtin_type (gdbarch)->builtin_long;
942 else if (reg->bitsize == TARGET_CHAR_BIT)
943 arch_reg->type = builtin_type (gdbarch)->builtin_char;
944 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
945 arch_reg->type = builtin_type (gdbarch)->builtin_short;
946 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
947 arch_reg->type = builtin_type (gdbarch)->builtin_int;
948 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
949 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
950 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
951 /* A bit desperate by this point... */
952 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
955 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
956 reg->name, reg->bitsize);
957 arch_reg->type = builtin_type (gdbarch)->builtin_long;
961 if (arch_reg->type == NULL)
962 internal_error (__FILE__, __LINE__,
963 "Register \"%s\" has an unknown type \"%s\"",
964 reg->name, reg->type);
967 return arch_reg->type;
971 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
973 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
976 return reg->target_regnum;
981 /* Check whether REGNUM is a member of REGGROUP. Registers from the
982 target description may be classified as general, float, or vector.
983 Unlike a gdbarch register_reggroup_p method, this function will
984 return -1 if it does not know; the caller should handle registers
985 with no specified group.
987 Arbitrary strings (other than "general", "float", and "vector")
988 from the description are not used; they cause the register to be
989 displayed in "info all-registers" but excluded from "info
990 registers" et al. The names of containing features are also not
991 used. This might be extended to display registers in some more
994 The save-restore flag is also implemented here. */
997 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
998 struct reggroup *reggroup)
1000 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1002 if (reg != NULL && reg->group != NULL)
1004 int general_p = 0, float_p = 0, vector_p = 0;
1006 if (strcmp (reg->group, "general") == 0)
1008 else if (strcmp (reg->group, "float") == 0)
1010 else if (strcmp (reg->group, "vector") == 0)
1013 if (reggroup == float_reggroup)
1016 if (reggroup == vector_reggroup)
1019 if (reggroup == general_reggroup)
1024 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1025 return reg->save_restore;
1030 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1031 group specified go to the default reggroup function and are handled
1035 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1036 struct reggroup *reggroup)
1038 int num_regs = gdbarch_num_regs (gdbarch);
1039 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1042 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1044 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1046 if (data->pseudo_register_reggroup_p != NULL)
1047 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1048 /* Otherwise fall through to the default reggroup_p. */
1051 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1055 return default_register_reggroup_p (gdbarch, regno, reggroup);
1058 /* Record architecture-specific functions to call for pseudo-register
1062 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1063 gdbarch_register_name_ftype *pseudo_name)
1065 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1067 data->pseudo_register_name = pseudo_name;
1071 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1072 gdbarch_register_type_ftype *pseudo_type)
1074 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1076 data->pseudo_register_type = pseudo_type;
1080 set_tdesc_pseudo_register_reggroup_p
1081 (struct gdbarch *gdbarch,
1082 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1084 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1086 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1089 /* Update GDBARCH to use the target description for registers. */
1092 tdesc_use_registers (struct gdbarch *gdbarch,
1093 const struct target_desc *target_desc,
1094 struct tdesc_arch_data *early_data)
1096 int num_regs = gdbarch_num_regs (gdbarch);
1098 struct tdesc_feature *feature;
1099 struct tdesc_reg *reg;
1100 struct tdesc_arch_data *data;
1101 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1104 /* We can't use the description for registers if it doesn't describe
1105 any. This function should only be called after validating
1106 registers, so the caller should know that registers are
1108 gdb_assert (tdesc_has_registers (target_desc));
1110 data = gdbarch_data (gdbarch, tdesc_data);
1111 data->arch_regs = early_data->arch_regs;
1114 /* Build up a set of all registers, so that we can assign register
1115 numbers where needed. The hash table expands as necessary, so
1116 the initial size is arbitrary. */
1117 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1119 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1122 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1125 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1130 /* Remove any registers which were assigned numbers by the
1133 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1136 htab_remove_elt (reg_hash, arch_reg->reg);
1138 /* Assign numbers to the remaining registers and add them to the
1139 list of registers. The new numbers are always above gdbarch_num_regs.
1140 Iterate over the features, not the hash table, so that the order
1141 matches that in the target description. */
1143 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1144 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1145 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1147 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1150 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1152 if (htab_find (reg_hash, reg) != NULL)
1154 new_arch_reg.reg = reg;
1155 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1159 htab_delete (reg_hash);
1161 /* Update the architecture. */
1162 set_gdbarch_num_regs (gdbarch, num_regs);
1163 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1164 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1165 set_gdbarch_remote_register_number (gdbarch,
1166 tdesc_remote_register_number);
1167 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1171 /* Methods for constructing a target description. */
1174 tdesc_free_reg (struct tdesc_reg *reg)
1183 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1184 int regnum, int save_restore, const char *group,
1185 int bitsize, const char *type)
1187 struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
1189 reg->name = xstrdup (name);
1190 reg->target_regnum = regnum;
1191 reg->save_restore = save_restore;
1192 reg->group = group ? xstrdup (group) : NULL;
1193 reg->bitsize = bitsize;
1194 reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
1196 /* If the register's type is target-defined, look it up now. We may not
1197 have easy access to the containing feature when we want it later. */
1198 reg->tdesc_type = tdesc_named_type (feature, reg->type);
1200 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1204 tdesc_free_type (struct tdesc_type *type)
1208 case TDESC_TYPE_STRUCT:
1209 case TDESC_TYPE_UNION:
1211 struct tdesc_type_field *f;
1215 VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
1219 VEC_free (tdesc_type_field, type->u.u.fields);
1223 case TDESC_TYPE_FLAGS:
1225 struct tdesc_type_flag *f;
1229 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
1233 VEC_free (tdesc_type_flag, type->u.f.flags);
1246 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1247 struct tdesc_type *field_type, int count)
1249 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1251 type->name = xstrdup (name);
1252 type->kind = TDESC_TYPE_VECTOR;
1253 type->u.v.type = field_type;
1254 type->u.v.count = count;
1256 VEC_safe_push (tdesc_type_p, feature->types, type);
1261 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1263 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1265 type->name = xstrdup (name);
1266 type->kind = TDESC_TYPE_STRUCT;
1268 VEC_safe_push (tdesc_type_p, feature->types, type);
1272 /* Set the total length of TYPE. Structs which contain bitfields may
1273 omit the reserved bits, so the end of the last field may not
1277 tdesc_set_struct_size (struct tdesc_type *type, LONGEST size)
1279 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1280 type->u.u.size = size;
1284 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1286 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1288 type->name = xstrdup (name);
1289 type->kind = TDESC_TYPE_UNION;
1291 VEC_safe_push (tdesc_type_p, feature->types, type);
1296 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1299 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1301 type->name = xstrdup (name);
1302 type->kind = TDESC_TYPE_FLAGS;
1303 type->u.f.size = size;
1305 VEC_safe_push (tdesc_type_p, feature->types, type);
1309 /* Add a new field. Return a temporary pointer to the field, which
1310 is only valid until the next call to tdesc_add_field (the vector
1311 might be reallocated). */
1314 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1315 struct tdesc_type *field_type)
1317 struct tdesc_type_field f = { 0 };
1319 gdb_assert (type->kind == TDESC_TYPE_UNION
1320 || type->kind == TDESC_TYPE_STRUCT);
1322 f.name = xstrdup (field_name);
1323 f.type = field_type;
1325 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1328 /* Add a new bitfield. */
1331 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1334 struct tdesc_type_field f = { 0 };
1336 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1338 f.name = xstrdup (field_name);
1342 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1346 tdesc_add_flag (struct tdesc_type *type, int start,
1347 const char *flag_name)
1349 struct tdesc_type_flag f = { 0 };
1351 gdb_assert (type->kind == TDESC_TYPE_FLAGS);
1353 f.name = xstrdup (flag_name);
1356 VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
1360 tdesc_free_feature (struct tdesc_feature *feature)
1362 struct tdesc_reg *reg;
1363 struct tdesc_type *type;
1366 for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
1367 tdesc_free_reg (reg);
1368 VEC_free (tdesc_reg_p, feature->registers);
1370 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
1371 tdesc_free_type (type);
1372 VEC_free (tdesc_type_p, feature->types);
1374 xfree (feature->name);
1378 struct tdesc_feature *
1379 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1381 struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
1383 new_feature->name = xstrdup (name);
1385 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1389 struct target_desc *
1390 allocate_target_description (void)
1392 return XZALLOC (struct target_desc);
1396 free_target_description (void *arg)
1398 struct target_desc *target_desc = arg;
1399 struct tdesc_feature *feature;
1400 struct property *prop;
1404 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
1406 tdesc_free_feature (feature);
1407 VEC_free (tdesc_feature_p, target_desc->features);
1410 VEC_iterate (property_s, target_desc->properties, ix, prop);
1414 xfree (prop->value);
1416 VEC_free (property_s, target_desc->properties);
1418 VEC_free (arch_p, target_desc->compatible);
1420 xfree (target_desc);
1424 make_cleanup_free_target_description (struct target_desc *target_desc)
1426 return make_cleanup (free_target_description, target_desc);
1430 tdesc_add_compatible (struct target_desc *target_desc,
1431 const struct bfd_arch_info *compatible)
1433 const struct bfd_arch_info *compat;
1436 /* If this instance of GDB is compiled without BFD support for the
1437 compatible architecture, simply ignore it -- we would not be able
1438 to handle it anyway. */
1439 if (compatible == NULL)
1442 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1444 if (compat == compatible)
1445 internal_error (__FILE__, __LINE__,
1446 _("Attempted to add duplicate "
1447 "compatible architecture \"%s\""),
1448 compatible->printable_name);
1450 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1454 set_tdesc_property (struct target_desc *target_desc,
1455 const char *key, const char *value)
1457 struct property *prop, new_prop;
1460 gdb_assert (key != NULL && value != NULL);
1462 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1464 if (strcmp (prop->key, key) == 0)
1465 internal_error (__FILE__, __LINE__,
1466 _("Attempted to add duplicate property \"%s\""), key);
1468 new_prop.key = xstrdup (key);
1469 new_prop.value = xstrdup (value);
1470 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1474 set_tdesc_architecture (struct target_desc *target_desc,
1475 const struct bfd_arch_info *arch)
1477 target_desc->arch = arch;
1481 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1483 target_desc->osabi = osabi;
1487 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1488 static struct cmd_list_element *tdesc_unset_cmdlist;
1490 /* Helper functions for the CLI commands. */
1493 set_tdesc_cmd (char *args, int from_tty)
1495 help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
1499 show_tdesc_cmd (char *args, int from_tty)
1501 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1505 unset_tdesc_cmd (char *args, int from_tty)
1507 help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
1511 set_tdesc_filename_cmd (char *args, int from_tty,
1512 struct cmd_list_element *c)
1514 target_clear_description ();
1515 target_find_description ();
1519 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1520 struct cmd_list_element *c,
1523 if (value != NULL && *value != '\0')
1524 printf_filtered (_("The target description will be read from \"%s\".\n"),
1527 printf_filtered (_("The target description will be "
1528 "read from the target.\n"));
1532 unset_tdesc_filename_cmd (char *args, int from_tty)
1534 xfree (target_description_filename);
1535 target_description_filename = NULL;
1536 target_clear_description ();
1537 target_find_description ();
1541 maint_print_c_tdesc_cmd (char *args, int from_tty)
1543 const struct target_desc *tdesc;
1544 const struct bfd_arch_info *compatible;
1545 const char *filename, *inp;
1546 char *function, *outp;
1547 struct property *prop;
1548 struct tdesc_feature *feature;
1549 struct tdesc_reg *reg;
1550 struct tdesc_type *type;
1551 struct tdesc_type_field *f;
1552 struct tdesc_type_flag *flag;
1555 /* Use the global target-supplied description, not the current
1556 architecture's. This lets a GDB for one architecture generate C
1557 for another architecture's description, even though the gdbarch
1558 initialization code will reject the new description. */
1559 tdesc = current_target_desc;
1561 error (_("There is no target description to print."));
1563 if (target_description_filename == NULL)
1564 error (_("The current target description did not come from an XML file."));
1566 filename = lbasename (target_description_filename);
1567 function = alloca (strlen (filename) + 1);
1568 for (inp = filename, outp = function; *inp != '\0'; inp++)
1571 else if (*inp == '-')
1577 /* Standard boilerplate. */
1578 printf_unfiltered ("/* THIS FILE IS GENERATED. Original: %s */\n\n",
1580 printf_unfiltered ("#include \"defs.h\"\n");
1581 printf_unfiltered ("#include \"osabi.h\"\n");
1582 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1583 printf_unfiltered ("\n");
1585 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1586 printf_unfiltered ("static void\n");
1587 printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1588 printf_unfiltered ("{\n");
1590 (" struct target_desc *result = allocate_target_description ();\n");
1591 printf_unfiltered (" struct tdesc_feature *feature;\n");
1592 printf_unfiltered (" struct tdesc_type *field_type, *type;\n");
1593 printf_unfiltered ("\n");
1595 if (tdesc_architecture (tdesc) != NULL)
1598 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1599 tdesc_architecture (tdesc)->printable_name);
1600 printf_unfiltered ("\n");
1603 if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
1604 && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
1607 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1608 gdbarch_osabi_name (tdesc_osabi (tdesc)));
1609 printf_unfiltered ("\n");
1612 for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
1616 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1617 compatible->printable_name);
1620 printf_unfiltered ("\n");
1622 for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1625 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1626 prop->key, prop->value);
1630 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1633 printf_unfiltered (" \
1634 feature = tdesc_create_feature (result, \"%s\");\n",
1638 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1643 case TDESC_TYPE_VECTOR:
1645 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1646 type->u.v.type->name);
1648 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1649 type->name, type->u.v.count);
1651 case TDESC_TYPE_UNION:
1653 (" type = tdesc_create_union (feature, \"%s\");\n",
1656 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1660 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1663 (" tdesc_add_field (type, \"%s\", field_type);\n",
1667 case TDESC_TYPE_FLAGS:
1669 (" field_type = tdesc_create_flags (feature, \"%s\", %d);\n",
1670 type->name, (int) type->u.f.size);
1672 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
1676 (" tdesc_add_flag (field_type, %d, \"%s\");\n",
1677 flag->start, flag->name);
1680 error (_("C output is not supported type \"%s\"."), type->name);
1682 printf_unfiltered ("\n");
1686 VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1689 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1690 reg->name, reg->target_regnum, reg->save_restore);
1692 printf_unfiltered ("\"%s\", ", reg->group);
1694 printf_unfiltered ("NULL, ");
1695 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1698 printf_unfiltered ("\n");
1701 printf_unfiltered (" tdesc_%s = result;\n", function);
1702 printf_unfiltered ("}\n");
1705 /* Provide a prototype to silence -Wmissing-prototypes. */
1706 extern initialize_file_ftype _initialize_target_descriptions;
1709 _initialize_target_descriptions (void)
1711 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1713 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1714 Set target description specific variables."),
1715 &tdesc_set_cmdlist, "set tdesc ",
1716 0 /* allow-unknown */, &setlist);
1717 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1718 Show target description specific variables."),
1719 &tdesc_show_cmdlist, "show tdesc ",
1720 0 /* allow-unknown */, &showlist);
1721 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1722 Unset target description specific variables."),
1723 &tdesc_unset_cmdlist, "unset tdesc ",
1724 0 /* allow-unknown */, &unsetlist);
1726 add_setshow_filename_cmd ("filename", class_obscure,
1727 &target_description_filename,
1729 Set the file to read for an XML target description"), _("\
1730 Show the file to read for an XML target description"), _("\
1731 When set, GDB will read the target description from a local\n\
1732 file instead of querying the remote target."),
1733 set_tdesc_filename_cmd,
1734 show_tdesc_filename_cmd,
1735 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1737 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1738 Unset the file to read for an XML target description. When unset,\n\
1739 GDB will read the description from the target."),
1740 &tdesc_unset_cmdlist);
1742 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1743 Print the current target description as a C source file."),
1744 &maintenanceprintlist);