1 /* Target description support for GDB.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010 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_assert.h"
35 #include "gdb_obstack.h"
40 typedef struct property
45 DEF_VEC_O(property_s
);
47 /* An individual register from a target description. */
49 typedef struct tdesc_reg
51 /* The name of this register. In standard features, it may be
52 recognized by the architecture support code, or it may be purely
56 /* The register number used by this target to refer to this
57 register. This is used for remote p/P packets and to determine
58 the ordering of registers in the remote g/G packets. */
61 /* If this flag is set, GDB should save and restore this register
62 around calls to an inferior function. */
65 /* The name of the register group containing this register, or NULL
66 if the group should be automatically determined from the
67 register's type. If this is "general", "float", or "vector", the
68 corresponding "info" command should display this register's
69 value. It can be an arbitrary string, but should be limited to
70 alphanumeric characters and internal hyphens. Currently other
71 strings are ignored (treated as NULL). */
74 /* The size of the register, in bits. */
77 /* The type of the register. This string corresponds to either
78 a named type from the target description or a predefined
82 /* The target-described type corresponding to TYPE, if found. */
83 struct tdesc_type
*tdesc_type
;
85 DEF_VEC_P(tdesc_reg_p
);
87 /* A named type from a target description. */
89 typedef struct tdesc_type_field
92 struct tdesc_type
*type
;
94 DEF_VEC_O(tdesc_type_field
);
96 typedef struct tdesc_type
98 /* The name of this type. */
101 /* Identify the kind of this type. */
104 /* Predefined types. */
117 TDESC_TYPE_IEEE_SINGLE
,
118 TDESC_TYPE_IEEE_DOUBLE
,
119 TDESC_TYPE_ARM_FPA_EXT
,
121 /* Types defined by a target feature. */
126 /* Kind-specific data. */
132 struct tdesc_type
*type
;
139 VEC(tdesc_type_field
) *fields
;
143 DEF_VEC_P(tdesc_type_p
);
145 /* A feature from a target description. Each feature is a collection
146 of other elements, e.g. registers and types. */
148 typedef struct tdesc_feature
150 /* The name of this feature. It may be recognized by the architecture
154 /* The registers associated with this feature. */
155 VEC(tdesc_reg_p
) *registers
;
157 /* The types associated with this feature. */
158 VEC(tdesc_type_p
) *types
;
160 DEF_VEC_P(tdesc_feature_p
);
162 /* A compatible architecture from a target description. */
163 typedef const struct bfd_arch_info
*arch_p
;
166 /* A target description. */
170 /* The architecture reported by the target, if any. */
171 const struct bfd_arch_info
*arch
;
173 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
175 enum gdb_osabi osabi
;
177 /* The list of compatible architectures reported by the target. */
178 VEC(arch_p
) *compatible
;
180 /* Any architecture-specific properties specified by the target. */
181 VEC(property_s
) *properties
;
183 /* The features associated with this target. */
184 VEC(tdesc_feature_p
) *features
;
187 /* Per-architecture data associated with a target description. The
188 target description may be shared by multiple architectures, but
189 this data is private to one gdbarch. */
191 typedef struct tdesc_arch_reg
193 struct tdesc_reg
*reg
;
196 DEF_VEC_O(tdesc_arch_reg
);
198 struct tdesc_arch_data
200 /* A list of register/type pairs, indexed by GDB's internal register number.
201 During initialization of the gdbarch this list is used to store
202 registers which the architecture assigns a fixed register number.
203 Registers which are NULL in this array, or off the end, are
204 treated as zero-sized and nameless (i.e. placeholders in the
206 VEC(tdesc_arch_reg
) *arch_regs
;
208 /* Functions which report the register name, type, and reggroups for
210 gdbarch_register_name_ftype
*pseudo_register_name
;
211 gdbarch_register_type_ftype
*pseudo_register_type
;
212 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
;
215 /* Global state. These variables are associated with the current
216 target; if GDB adds support for multiple simultaneous targets, then
217 these variables should become target-specific data. */
219 /* A flag indicating that a description has already been fetched from
220 the current target, so it should not be queried again. */
222 static int target_desc_fetched
;
224 /* The description fetched from the current target, or NULL if the
225 current target did not supply any description. Only valid when
226 target_desc_fetched is set. Only the description initialization
227 code should access this; normally, the description should be
228 accessed through the gdbarch object. */
230 static const struct target_desc
*current_target_desc
;
232 /* Other global variables. */
234 /* The filename to read a target description from. */
236 static char *target_description_filename
;
238 /* A handle for architecture-specific data associated with the
239 target description (see struct tdesc_arch_data). */
241 static struct gdbarch_data
*tdesc_data
;
243 /* Fetch the current target's description, and switch the current
244 architecture to one which incorporates that description. */
247 target_find_description (void)
249 /* If we've already fetched a description from the target, don't do
250 it again. This allows a target to fetch the description early,
251 during its to_open or to_create_inferior, if it needs extra
252 information about the target to initialize. */
253 if (target_desc_fetched
)
256 /* The current architecture should not have any target description
257 specified. It should have been cleared, e.g. when we
258 disconnected from the previous target. */
259 gdb_assert (gdbarch_target_desc (target_gdbarch
) == NULL
);
261 /* First try to fetch an XML description from the user-specified
263 current_target_desc
= NULL
;
264 if (target_description_filename
!= NULL
265 && *target_description_filename
!= '\0')
267 = file_read_description_xml (target_description_filename
);
269 /* Next try to read the description from the current target using
271 if (current_target_desc
== NULL
)
272 current_target_desc
= target_read_description_xml (¤t_target
);
274 /* If that failed try a target-specific hook. */
275 if (current_target_desc
== NULL
)
276 current_target_desc
= target_read_description (¤t_target
);
278 /* If a non-NULL description was returned, then update the current
280 if (current_target_desc
)
282 struct gdbarch_info info
;
284 gdbarch_info_init (&info
);
285 info
.target_desc
= current_target_desc
;
286 if (!gdbarch_update_p (info
))
287 warning (_("Architecture rejected target-supplied description"));
290 struct tdesc_arch_data
*data
;
292 data
= gdbarch_data (target_gdbarch
, tdesc_data
);
293 if (tdesc_has_registers (current_target_desc
)
294 && data
->arch_regs
== NULL
)
295 warning (_("Target-supplied registers are not supported "
296 "by the current architecture"));
300 /* Now that we know this description is usable, record that we
302 target_desc_fetched
= 1;
305 /* Discard any description fetched from the current target, and switch
306 the current architecture to one with no target description. */
309 target_clear_description (void)
311 struct gdbarch_info info
;
313 if (!target_desc_fetched
)
316 target_desc_fetched
= 0;
317 current_target_desc
= NULL
;
319 gdbarch_info_init (&info
);
320 if (!gdbarch_update_p (info
))
321 internal_error (__FILE__
, __LINE__
,
322 _("Could not remove target-supplied description"));
325 /* Return the global current target description. This should only be
326 used by gdbarch initialization code; most access should be through
327 an existing gdbarch. */
329 const struct target_desc
*
330 target_current_description (void)
332 if (target_desc_fetched
)
333 return current_target_desc
;
338 /* Return non-zero if this target description is compatible
339 with the given BFD architecture. */
342 tdesc_compatible_p (const struct target_desc
*target_desc
,
343 const struct bfd_arch_info
*arch
)
345 const struct bfd_arch_info
*compat
;
348 for (ix
= 0; VEC_iterate (arch_p
, target_desc
->compatible
, ix
, compat
);
352 || arch
->compatible (arch
, compat
)
353 || compat
->compatible (compat
, arch
))
361 /* Direct accessors for target descriptions. */
363 /* Return the string value of a property named KEY, or NULL if the
364 property was not specified. */
367 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
369 struct property
*prop
;
372 for (ix
= 0; VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
374 if (strcmp (prop
->key
, key
) == 0)
380 /* Return the BFD architecture associated with this target
381 description, or NULL if no architecture was specified. */
383 const struct bfd_arch_info
*
384 tdesc_architecture (const struct target_desc
*target_desc
)
386 return target_desc
->arch
;
389 /* Return the OSABI associated with this target description, or
390 GDB_OSABI_UNKNOWN if no osabi was specified. */
393 tdesc_osabi (const struct target_desc
*target_desc
)
395 return target_desc
->osabi
;
400 /* Return 1 if this target description includes any registers. */
403 tdesc_has_registers (const struct target_desc
*target_desc
)
406 struct tdesc_feature
*feature
;
408 if (target_desc
== NULL
)
412 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
414 if (! VEC_empty (tdesc_reg_p
, feature
->registers
))
420 /* Return the feature with the given name, if present, or NULL if
421 the named feature is not found. */
423 const struct tdesc_feature
*
424 tdesc_find_feature (const struct target_desc
*target_desc
,
428 struct tdesc_feature
*feature
;
431 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
433 if (strcmp (feature
->name
, name
) == 0)
439 /* Return the name of FEATURE. */
442 tdesc_feature_name (const struct tdesc_feature
*feature
)
444 return feature
->name
;
447 /* Predefined types. */
448 static struct tdesc_type tdesc_predefined_types
[] =
450 { "int8", TDESC_TYPE_INT8
},
451 { "int16", TDESC_TYPE_INT16
},
452 { "int32", TDESC_TYPE_INT32
},
453 { "int64", TDESC_TYPE_INT64
},
454 { "int128", TDESC_TYPE_INT128
},
455 { "uint8", TDESC_TYPE_UINT8
},
456 { "uint16", TDESC_TYPE_UINT16
},
457 { "uint32", TDESC_TYPE_UINT32
},
458 { "uint64", TDESC_TYPE_UINT64
},
459 { "uint128", TDESC_TYPE_UINT128
},
460 { "code_ptr", TDESC_TYPE_CODE_PTR
},
461 { "data_ptr", TDESC_TYPE_DATA_PTR
},
462 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
463 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
464 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
}
467 /* Return the type associated with ID in the context of FEATURE, or
471 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
474 struct tdesc_type
*type
;
476 /* First try target-defined types. */
477 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
478 if (strcmp (type
->name
, id
) == 0)
481 /* Next try the predefined types. */
482 for (ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
483 if (strcmp (tdesc_predefined_types
[ix
].name
, id
) == 0)
484 return &tdesc_predefined_types
[ix
];
489 /* Construct, if necessary, and return the GDB type implementing target
490 type TDESC_TYPE for architecture GDBARCH. */
493 tdesc_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*tdesc_type
)
495 switch (tdesc_type
->kind
)
497 /* Predefined types. */
498 case TDESC_TYPE_INT8
:
499 return builtin_type (gdbarch
)->builtin_int8
;
501 case TDESC_TYPE_INT16
:
502 return builtin_type (gdbarch
)->builtin_int16
;
504 case TDESC_TYPE_INT32
:
505 return builtin_type (gdbarch
)->builtin_int32
;
507 case TDESC_TYPE_INT64
:
508 return builtin_type (gdbarch
)->builtin_int64
;
510 case TDESC_TYPE_INT128
:
511 return builtin_type (gdbarch
)->builtin_int128
;
513 case TDESC_TYPE_UINT8
:
514 return builtin_type (gdbarch
)->builtin_uint8
;
516 case TDESC_TYPE_UINT16
:
517 return builtin_type (gdbarch
)->builtin_uint16
;
519 case TDESC_TYPE_UINT32
:
520 return builtin_type (gdbarch
)->builtin_uint32
;
522 case TDESC_TYPE_UINT64
:
523 return builtin_type (gdbarch
)->builtin_uint64
;
525 case TDESC_TYPE_UINT128
:
526 return builtin_type (gdbarch
)->builtin_uint128
;
528 case TDESC_TYPE_CODE_PTR
:
529 return builtin_type (gdbarch
)->builtin_func_ptr
;
531 case TDESC_TYPE_DATA_PTR
:
532 return builtin_type (gdbarch
)->builtin_data_ptr
;
534 case TDESC_TYPE_IEEE_SINGLE
:
535 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_single",
536 floatformats_ieee_single
);
538 case TDESC_TYPE_IEEE_DOUBLE
:
539 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_double",
540 floatformats_ieee_double
);
542 case TDESC_TYPE_ARM_FPA_EXT
:
543 return arch_float_type (gdbarch
, -1, "builtin_type_arm_ext",
544 floatformats_arm_ext
);
546 /* Types defined by a target feature. */
547 case TDESC_TYPE_VECTOR
:
549 struct type
*type
, *field_type
;
551 field_type
= tdesc_gdb_type (gdbarch
, tdesc_type
->u
.v
.type
);
552 type
= init_vector_type (field_type
, tdesc_type
->u
.v
.count
);
553 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
558 case TDESC_TYPE_UNION
:
560 struct type
*type
, *field_type
;
561 struct tdesc_type_field
*f
;
564 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
565 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
568 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
571 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
572 append_composite_type_field (type
, xstrdup (f
->name
), field_type
);
574 /* If any of the children of this union are vectors, flag the
575 union as a vector also. This allows e.g. a union of two
576 vector types to show up automatically in "info vector". */
577 if (TYPE_VECTOR (field_type
))
578 TYPE_VECTOR (type
) = 1;
585 internal_error (__FILE__
, __LINE__
,
586 "Type \"%s\" has an unknown kind %d",
587 tdesc_type
->name
, tdesc_type
->kind
);
591 /* Support for registers from target descriptions. */
593 /* Construct the per-gdbarch data. */
596 tdesc_data_init (struct obstack
*obstack
)
598 struct tdesc_arch_data
*data
;
600 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
604 /* Similar, but for the temporary copy used during architecture
607 struct tdesc_arch_data
*
608 tdesc_data_alloc (void)
610 return XZALLOC (struct tdesc_arch_data
);
613 /* Free something allocated by tdesc_data_alloc, if it is not going
614 to be used (for instance if it was unsuitable for the
618 tdesc_data_cleanup (void *data_untyped
)
620 struct tdesc_arch_data
*data
= data_untyped
;
622 VEC_free (tdesc_arch_reg
, data
->arch_regs
);
626 /* Search FEATURE for a register named NAME. */
628 static struct tdesc_reg
*
629 tdesc_find_register_early (const struct tdesc_feature
*feature
,
633 struct tdesc_reg
*reg
;
636 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
638 if (strcasecmp (reg
->name
, name
) == 0)
644 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
647 tdesc_numbered_register (const struct tdesc_feature
*feature
,
648 struct tdesc_arch_data
*data
,
649 int regno
, const char *name
)
651 struct tdesc_arch_reg arch_reg
= { 0 };
652 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
657 /* Make sure the vector includes a REGNO'th element. */
658 while (regno
>= VEC_length (tdesc_arch_reg
, data
->arch_regs
))
659 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &arch_reg
);
662 VEC_replace (tdesc_arch_reg
, data
->arch_regs
, regno
, &arch_reg
);
666 /* Search FEATURE for a register named NAME, but do not assign a fixed
667 register number to it. */
670 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
673 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
681 /* Search FEATURE for a register whose name is in NAMES and assign
685 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
686 struct tdesc_arch_data
*data
,
687 int regno
, const char *const names
[])
691 for (i
= 0; names
[i
] != NULL
; i
++)
692 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
698 /* Search FEATURE for a register named NAME, and return its size in
699 bits. The register must exist. */
702 tdesc_register_size (const struct tdesc_feature
*feature
,
705 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
707 gdb_assert (reg
!= NULL
);
711 /* Look up a register by its GDB internal register number. */
713 static struct tdesc_arch_reg
*
714 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
716 struct tdesc_arch_reg
*reg
;
717 struct tdesc_arch_data
*data
;
719 data
= gdbarch_data (gdbarch
, tdesc_data
);
720 if (regno
< VEC_length (tdesc_arch_reg
, data
->arch_regs
))
721 return VEC_index (tdesc_arch_reg
, data
->arch_regs
, regno
);
726 static struct tdesc_reg
*
727 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
729 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
730 return reg
? reg
->reg
: NULL
;
733 /* Return the name of register REGNO, from the target description or
734 from an architecture-provided pseudo_register_name method. */
737 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
739 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
740 int num_regs
= gdbarch_num_regs (gdbarch
);
741 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
746 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
748 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
749 gdb_assert (data
->pseudo_register_name
!= NULL
);
750 return data
->pseudo_register_name (gdbarch
, regno
);
757 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
759 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
760 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
761 int num_regs
= gdbarch_num_regs (gdbarch
);
762 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
764 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
766 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
767 gdb_assert (data
->pseudo_register_type
!= NULL
);
768 return data
->pseudo_register_type (gdbarch
, regno
);
772 /* Return "int0_t", since "void" has a misleading size of one. */
773 return builtin_type (gdbarch
)->builtin_int0
;
775 if (arch_reg
->type
== NULL
)
777 /* First check for a predefined or target defined type. */
779 arch_reg
->type
= tdesc_gdb_type (gdbarch
, reg
->tdesc_type
);
781 /* Next try size-sensitive type shortcuts. */
782 else if (strcmp (reg
->type
, "float") == 0)
784 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
785 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
786 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
787 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
788 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
789 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
792 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
793 reg
->name
, reg
->bitsize
);
794 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
797 else if (strcmp (reg
->type
, "int") == 0)
799 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
800 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
801 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
802 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
803 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
804 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
805 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
806 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
807 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
808 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
809 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
810 /* A bit desperate by this point... */
811 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
814 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
815 reg
->name
, reg
->bitsize
);
816 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
820 if (arch_reg
->type
== NULL
)
821 internal_error (__FILE__
, __LINE__
,
822 "Register \"%s\" has an unknown type \"%s\"",
823 reg
->name
, reg
->type
);
826 return arch_reg
->type
;
830 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
832 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
835 return reg
->target_regnum
;
840 /* Check whether REGNUM is a member of REGGROUP. Registers from the
841 target description may be classified as general, float, or vector.
842 Unlike a gdbarch register_reggroup_p method, this function will
843 return -1 if it does not know; the caller should handle registers
844 with no specified group.
846 Arbitrary strings (other than "general", "float", and "vector")
847 from the description are not used; they cause the register to be
848 displayed in "info all-registers" but excluded from "info
849 registers" et al. The names of containing features are also not
850 used. This might be extended to display registers in some more
853 The save-restore flag is also implemented here. */
856 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
857 struct reggroup
*reggroup
)
859 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
861 if (reg
!= NULL
&& reg
->group
!= NULL
)
863 int general_p
= 0, float_p
= 0, vector_p
= 0;
865 if (strcmp (reg
->group
, "general") == 0)
867 else if (strcmp (reg
->group
, "float") == 0)
869 else if (strcmp (reg
->group
, "vector") == 0)
872 if (reggroup
== float_reggroup
)
875 if (reggroup
== vector_reggroup
)
878 if (reggroup
== general_reggroup
)
883 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
884 return reg
->save_restore
;
889 /* Check whether REGNUM is a member of REGGROUP. Registers with no
890 group specified go to the default reggroup function and are handled
894 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
895 struct reggroup
*reggroup
)
897 int num_regs
= gdbarch_num_regs (gdbarch
);
898 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
901 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
903 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
904 if (data
->pseudo_register_reggroup_p
!= NULL
)
905 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
906 /* Otherwise fall through to the default reggroup_p. */
909 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
913 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
916 /* Record architecture-specific functions to call for pseudo-register
920 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
921 gdbarch_register_name_ftype
*pseudo_name
)
923 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
925 data
->pseudo_register_name
= pseudo_name
;
929 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
930 gdbarch_register_type_ftype
*pseudo_type
)
932 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
934 data
->pseudo_register_type
= pseudo_type
;
938 set_tdesc_pseudo_register_reggroup_p
939 (struct gdbarch
*gdbarch
,
940 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
942 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
944 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
947 /* Update GDBARCH to use the target description for registers. */
950 tdesc_use_registers (struct gdbarch
*gdbarch
,
951 const struct target_desc
*target_desc
,
952 struct tdesc_arch_data
*early_data
)
954 int num_regs
= gdbarch_num_regs (gdbarch
);
956 struct tdesc_feature
*feature
;
957 struct tdesc_reg
*reg
;
958 struct tdesc_arch_data
*data
;
959 struct tdesc_arch_reg
*arch_reg
, new_arch_reg
= { 0 };
962 /* We can't use the description for registers if it doesn't describe
963 any. This function should only be called after validating
964 registers, so the caller should know that registers are
966 gdb_assert (tdesc_has_registers (target_desc
));
968 data
= gdbarch_data (gdbarch
, tdesc_data
);
969 data
->arch_regs
= early_data
->arch_regs
;
972 /* Build up a set of all registers, so that we can assign register
973 numbers where needed. The hash table expands as necessary, so
974 the initial size is arbitrary. */
975 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
977 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ixf
, feature
);
980 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
983 void **slot
= htab_find_slot (reg_hash
, reg
, INSERT
);
988 /* Remove any registers which were assigned numbers by the
991 VEC_iterate (tdesc_arch_reg
, data
->arch_regs
, ixr
, arch_reg
);
994 htab_remove_elt (reg_hash
, arch_reg
->reg
);
996 /* Assign numbers to the remaining registers and add them to the
997 list of registers. The new numbers are always above gdbarch_num_regs.
998 Iterate over the features, not the hash table, so that the order
999 matches that in the target description. */
1001 gdb_assert (VEC_length (tdesc_arch_reg
, data
->arch_regs
) <= num_regs
);
1002 while (VEC_length (tdesc_arch_reg
, data
->arch_regs
) < num_regs
)
1003 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1005 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ixf
, feature
);
1008 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
1010 if (htab_find (reg_hash
, reg
) != NULL
)
1012 new_arch_reg
.reg
= reg
;
1013 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1017 htab_delete (reg_hash
);
1019 /* Update the architecture. */
1020 set_gdbarch_num_regs (gdbarch
, num_regs
);
1021 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1022 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1023 set_gdbarch_remote_register_number (gdbarch
,
1024 tdesc_remote_register_number
);
1025 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1029 /* Methods for constructing a target description. */
1032 tdesc_free_reg (struct tdesc_reg
*reg
)
1041 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
1042 int regnum
, int save_restore
, const char *group
,
1043 int bitsize
, const char *type
)
1045 struct tdesc_reg
*reg
= XZALLOC (struct tdesc_reg
);
1047 reg
->name
= xstrdup (name
);
1048 reg
->target_regnum
= regnum
;
1049 reg
->save_restore
= save_restore
;
1050 reg
->group
= group
? xstrdup (group
) : NULL
;
1051 reg
->bitsize
= bitsize
;
1052 reg
->type
= type
? xstrdup (type
) : xstrdup ("<unknown>");
1054 /* If the register's type is target-defined, look it up now. We may not
1055 have easy access to the containing feature when we want it later. */
1056 reg
->tdesc_type
= tdesc_named_type (feature
, reg
->type
);
1058 VEC_safe_push (tdesc_reg_p
, feature
->registers
, reg
);
1062 tdesc_free_type (struct tdesc_type
*type
)
1067 case TDESC_TYPE_UNION
:
1069 struct tdesc_type_field
*f
;
1073 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix
, f
);
1077 VEC_free (tdesc_type_field
, type
->u
.u
.fields
);
1090 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1091 struct tdesc_type
*field_type
, int count
)
1093 struct tdesc_type
*type
= XZALLOC (struct tdesc_type
);
1095 type
->name
= xstrdup (name
);
1096 type
->kind
= TDESC_TYPE_VECTOR
;
1097 type
->u
.v
.type
= field_type
;
1098 type
->u
.v
.count
= count
;
1100 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1105 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1107 struct tdesc_type
*type
= XZALLOC (struct tdesc_type
);
1109 type
->name
= xstrdup (name
);
1110 type
->kind
= TDESC_TYPE_UNION
;
1112 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1117 tdesc_add_field (struct tdesc_type
*type
, const char *field_name
,
1118 struct tdesc_type
*field_type
)
1120 struct tdesc_type_field f
= { 0 };
1122 gdb_assert (type
->kind
== TDESC_TYPE_UNION
);
1124 f
.name
= xstrdup (field_name
);
1125 f
.type
= field_type
;
1127 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1131 tdesc_free_feature (struct tdesc_feature
*feature
)
1133 struct tdesc_reg
*reg
;
1134 struct tdesc_type
*type
;
1137 for (ix
= 0; VEC_iterate (tdesc_reg_p
, feature
->registers
, ix
, reg
); ix
++)
1138 tdesc_free_reg (reg
);
1139 VEC_free (tdesc_reg_p
, feature
->registers
);
1141 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
1142 tdesc_free_type (type
);
1143 VEC_free (tdesc_type_p
, feature
->types
);
1145 xfree (feature
->name
);
1149 struct tdesc_feature
*
1150 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
)
1152 struct tdesc_feature
*new_feature
= XZALLOC (struct tdesc_feature
);
1154 new_feature
->name
= xstrdup (name
);
1156 VEC_safe_push (tdesc_feature_p
, tdesc
->features
, new_feature
);
1160 struct target_desc
*
1161 allocate_target_description (void)
1163 return XZALLOC (struct target_desc
);
1167 free_target_description (void *arg
)
1169 struct target_desc
*target_desc
= arg
;
1170 struct tdesc_feature
*feature
;
1171 struct property
*prop
;
1175 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
1177 tdesc_free_feature (feature
);
1178 VEC_free (tdesc_feature_p
, target_desc
->features
);
1181 VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
1185 xfree (prop
->value
);
1187 VEC_free (property_s
, target_desc
->properties
);
1189 VEC_free (arch_p
, target_desc
->compatible
);
1191 xfree (target_desc
);
1195 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1197 return make_cleanup (free_target_description
, target_desc
);
1201 tdesc_add_compatible (struct target_desc
*target_desc
,
1202 const struct bfd_arch_info
*compatible
)
1204 const struct bfd_arch_info
*compat
;
1207 /* If this instance of GDB is compiled without BFD support for the
1208 compatible architecture, simply ignore it -- we would not be able
1209 to handle it anyway. */
1210 if (compatible
== NULL
)
1213 for (ix
= 0; VEC_iterate (arch_p
, target_desc
->compatible
, ix
, compat
);
1215 if (compat
== compatible
)
1216 internal_error (__FILE__
, __LINE__
,
1217 _("Attempted to add duplicate "
1218 "compatible architecture \"%s\""),
1219 compatible
->printable_name
);
1221 VEC_safe_push (arch_p
, target_desc
->compatible
, compatible
);
1225 set_tdesc_property (struct target_desc
*target_desc
,
1226 const char *key
, const char *value
)
1228 struct property
*prop
, new_prop
;
1231 gdb_assert (key
!= NULL
&& value
!= NULL
);
1233 for (ix
= 0; VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
1235 if (strcmp (prop
->key
, key
) == 0)
1236 internal_error (__FILE__
, __LINE__
,
1237 _("Attempted to add duplicate property \"%s\""), key
);
1239 new_prop
.key
= xstrdup (key
);
1240 new_prop
.value
= xstrdup (value
);
1241 VEC_safe_push (property_s
, target_desc
->properties
, &new_prop
);
1245 set_tdesc_architecture (struct target_desc
*target_desc
,
1246 const struct bfd_arch_info
*arch
)
1248 target_desc
->arch
= arch
;
1252 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1254 target_desc
->osabi
= osabi
;
1258 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1259 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1261 /* Helper functions for the CLI commands. */
1264 set_tdesc_cmd (char *args
, int from_tty
)
1266 help_list (tdesc_set_cmdlist
, "set tdesc ", -1, gdb_stdout
);
1270 show_tdesc_cmd (char *args
, int from_tty
)
1272 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1276 unset_tdesc_cmd (char *args
, int from_tty
)
1278 help_list (tdesc_unset_cmdlist
, "unset tdesc ", -1, gdb_stdout
);
1282 set_tdesc_filename_cmd (char *args
, int from_tty
,
1283 struct cmd_list_element
*c
)
1285 target_clear_description ();
1286 target_find_description ();
1290 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1291 struct cmd_list_element
*c
,
1294 if (value
!= NULL
&& *value
!= '\0')
1295 printf_filtered (_("\
1296 The target description will be read from \"%s\".\n"),
1299 printf_filtered (_("\
1300 The target description will be read from the target.\n"));
1304 unset_tdesc_filename_cmd (char *args
, int from_tty
)
1306 xfree (target_description_filename
);
1307 target_description_filename
= NULL
;
1308 target_clear_description ();
1309 target_find_description ();
1313 maint_print_c_tdesc_cmd (char *args
, int from_tty
)
1315 const struct target_desc
*tdesc
;
1316 const struct bfd_arch_info
*compatible
;
1317 const char *filename
, *inp
;
1318 char *function
, *outp
;
1319 struct property
*prop
;
1320 struct tdesc_feature
*feature
;
1321 struct tdesc_reg
*reg
;
1322 struct tdesc_type
*type
;
1323 struct tdesc_type_field
*f
;
1326 /* Use the global target-supplied description, not the current
1327 architecture's. This lets a GDB for one architecture generate C
1328 for another architecture's description, even though the gdbarch
1329 initialization code will reject the new description. */
1330 tdesc
= current_target_desc
;
1332 error (_("There is no target description to print."));
1334 if (target_description_filename
== NULL
)
1335 error (_("The current target description did not come from an XML file."));
1337 filename
= lbasename (target_description_filename
);
1338 function
= alloca (strlen (filename
) + 1);
1339 for (inp
= filename
, outp
= function
; *inp
!= '\0'; inp
++)
1342 else if (*inp
== '-')
1348 /* Standard boilerplate. */
1349 printf_unfiltered ("/* THIS FILE IS GENERATED. Original: %s */\n\n",
1351 printf_unfiltered ("#include \"defs.h\"\n");
1352 printf_unfiltered ("#include \"osabi.h\"\n");
1353 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1354 printf_unfiltered ("\n");
1356 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function
);
1357 printf_unfiltered ("static void\n");
1358 printf_unfiltered ("initialize_tdesc_%s (void)\n", function
);
1359 printf_unfiltered ("{\n");
1361 (" struct target_desc *result = allocate_target_description ();\n");
1362 printf_unfiltered (" struct tdesc_feature *feature;\n");
1363 printf_unfiltered (" struct tdesc_type *field_type, *type;\n");
1364 printf_unfiltered ("\n");
1366 if (tdesc_architecture (tdesc
) != NULL
)
1369 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1370 tdesc_architecture (tdesc
)->printable_name
);
1371 printf_unfiltered ("\n");
1374 if (tdesc_osabi (tdesc
) > GDB_OSABI_UNKNOWN
1375 && tdesc_osabi (tdesc
) < GDB_OSABI_INVALID
)
1378 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1379 gdbarch_osabi_name (tdesc_osabi (tdesc
)));
1380 printf_unfiltered ("\n");
1383 for (ix
= 0; VEC_iterate (arch_p
, tdesc
->compatible
, ix
, compatible
);
1387 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1388 compatible
->printable_name
);
1391 printf_unfiltered ("\n");
1393 for (ix
= 0; VEC_iterate (property_s
, tdesc
->properties
, ix
, prop
);
1396 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1397 prop
->key
, prop
->value
);
1401 VEC_iterate (tdesc_feature_p
, tdesc
->features
, ix
, feature
);
1404 printf_unfiltered (" feature = tdesc_create_feature (result, \"%s\");\n",
1408 VEC_iterate (tdesc_type_p
, feature
->types
, ix2
, type
);
1413 case TDESC_TYPE_VECTOR
:
1415 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1416 type
->u
.v
.type
->name
);
1418 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1419 type
->name
, type
->u
.v
.count
);
1421 case TDESC_TYPE_UNION
:
1423 (" type = tdesc_create_union (feature, \"%s\");\n",
1426 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
1430 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1433 (" tdesc_add_field (type, \"%s\", field_type);\n",
1438 error (_("C output is not supported type \"%s\"."), type
->name
);
1440 printf_unfiltered ("\n");
1444 VEC_iterate (tdesc_reg_p
, feature
->registers
, ix2
, reg
);
1447 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1448 reg
->name
, reg
->target_regnum
, reg
->save_restore
);
1450 printf_unfiltered ("\"%s\", ", reg
->group
);
1452 printf_unfiltered ("NULL, ");
1453 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
);
1456 printf_unfiltered ("\n");
1459 printf_unfiltered (" tdesc_%s = result;\n", function
);
1460 printf_unfiltered ("}\n");
1463 /* Provide a prototype to silence -Wmissing-prototypes. */
1464 extern initialize_file_ftype _initialize_target_descriptions
;
1467 _initialize_target_descriptions (void)
1469 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
1471 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
1472 Set target description specific variables."),
1473 &tdesc_set_cmdlist
, "set tdesc ",
1474 0 /* allow-unknown */, &setlist
);
1475 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
1476 Show target description specific variables."),
1477 &tdesc_show_cmdlist
, "show tdesc ",
1478 0 /* allow-unknown */, &showlist
);
1479 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
1480 Unset target description specific variables."),
1481 &tdesc_unset_cmdlist
, "unset tdesc ",
1482 0 /* allow-unknown */, &unsetlist
);
1484 add_setshow_filename_cmd ("filename", class_obscure
,
1485 &target_description_filename
,
1487 Set the file to read for an XML target description"), _("\
1488 Show the file to read for an XML target description"), _("\
1489 When set, GDB will read the target description from a local\n\
1490 file instead of querying the remote target."),
1491 set_tdesc_filename_cmd
,
1492 show_tdesc_filename_cmd
,
1493 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
1495 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
1496 Unset the file to read for an XML target description. When unset,\n\
1497 GDB will read the description from the target."),
1498 &tdesc_unset_cmdlist
);
1500 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
1501 Print the current target description as a C source file."),
1502 &maintenanceprintlist
);