1 /* Target description support for GDB.
3 Copyright (C) 2006, 2007, 2008, 2009 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"
33 #include "gdb_assert.h"
34 #include "gdb_obstack.h"
39 typedef struct property
44 DEF_VEC_O(property_s
);
46 /* An individual register from a target description. */
48 typedef struct tdesc_reg
50 /* The name of this register. In standard features, it may be
51 recognized by the architecture support code, or it may be purely
55 /* The register number used by this target to refer to this
56 register. This is used for remote p/P packets and to determine
57 the ordering of registers in the remote g/G packets. */
60 /* If this flag is set, GDB should save and restore this register
61 around calls to an inferior function. */
64 /* The name of the register group containing this register, or NULL
65 if the group should be automatically determined from the
66 register's type. If this is "general", "float", or "vector", the
67 corresponding "info" command should display this register's
68 value. It can be an arbitrary string, but should be limited to
69 alphanumeric characters and internal hyphens. Currently other
70 strings are ignored (treated as NULL). */
73 /* The size of the register, in bits. */
76 /* The type of the register. This string corresponds to either
77 a named type from the target description or a predefined
81 /* The target-described type corresponding to TYPE, if found. */
82 struct tdesc_type
*tdesc_type
;
84 DEF_VEC_P(tdesc_reg_p
);
86 /* A named type from a target description. */
88 typedef struct tdesc_type_field
91 struct tdesc_type
*type
;
93 DEF_VEC_O(tdesc_type_field
);
95 typedef struct tdesc_type
97 /* The name of this type. */
100 /* Identify the kind of this type. */
103 /* Predefined types. */
116 TDESC_TYPE_IEEE_SINGLE
,
117 TDESC_TYPE_IEEE_DOUBLE
,
118 TDESC_TYPE_ARM_FPA_EXT
,
120 /* Types defined by a target feature. */
125 /* Kind-specific data. */
131 struct tdesc_type
*type
;
138 VEC(tdesc_type_field
) *fields
;
142 DEF_VEC_P(tdesc_type_p
);
144 /* A feature from a target description. Each feature is a collection
145 of other elements, e.g. registers and types. */
147 typedef struct tdesc_feature
149 /* The name of this feature. It may be recognized by the architecture
153 /* The registers associated with this feature. */
154 VEC(tdesc_reg_p
) *registers
;
156 /* The types associated with this feature. */
157 VEC(tdesc_type_p
) *types
;
159 DEF_VEC_P(tdesc_feature_p
);
161 /* A target description. */
165 /* The architecture reported by the target, if any. */
166 const struct bfd_arch_info
*arch
;
168 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
170 enum gdb_osabi osabi
;
172 /* Any architecture-specific properties specified by the target. */
173 VEC(property_s
) *properties
;
175 /* The features associated with this target. */
176 VEC(tdesc_feature_p
) *features
;
179 /* Per-architecture data associated with a target description. The
180 target description may be shared by multiple architectures, but
181 this data is private to one gdbarch. */
183 typedef struct tdesc_arch_reg
185 struct tdesc_reg
*reg
;
188 DEF_VEC_O(tdesc_arch_reg
);
190 struct tdesc_arch_data
192 /* A list of register/type pairs, indexed by GDB's internal register number.
193 During initialization of the gdbarch this list is used to store
194 registers which the architecture assigns a fixed register number.
195 Registers which are NULL in this array, or off the end, are
196 treated as zero-sized and nameless (i.e. placeholders in the
198 VEC(tdesc_arch_reg
) *arch_regs
;
200 /* Functions which report the register name, type, and reggroups for
202 gdbarch_register_name_ftype
*pseudo_register_name
;
203 gdbarch_register_type_ftype
*pseudo_register_type
;
204 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
;
207 /* Global state. These variables are associated with the current
208 target; if GDB adds support for multiple simultaneous targets, then
209 these variables should become target-specific data. */
211 /* A flag indicating that a description has already been fetched from
212 the current target, so it should not be queried again. */
214 static int target_desc_fetched
;
216 /* The description fetched from the current target, or NULL if the
217 current target did not supply any description. Only valid when
218 target_desc_fetched is set. Only the description initialization
219 code should access this; normally, the description should be
220 accessed through the gdbarch object. */
222 static const struct target_desc
*current_target_desc
;
224 /* Other global variables. */
226 /* The filename to read a target description from. */
228 static char *target_description_filename
;
230 /* A handle for architecture-specific data associated with the
231 target description (see struct tdesc_arch_data). */
233 static struct gdbarch_data
*tdesc_data
;
235 /* Fetch the current target's description, and switch the current
236 architecture to one which incorporates that description. */
239 target_find_description (void)
241 /* If we've already fetched a description from the target, don't do
242 it again. This allows a target to fetch the description early,
243 during its to_open or to_create_inferior, if it needs extra
244 information about the target to initialize. */
245 if (target_desc_fetched
)
248 /* The current architecture should not have any target description
249 specified. It should have been cleared, e.g. when we
250 disconnected from the previous target. */
251 gdb_assert (gdbarch_target_desc (target_gdbarch
) == NULL
);
253 /* First try to fetch an XML description from the user-specified
255 current_target_desc
= NULL
;
256 if (target_description_filename
!= NULL
257 && *target_description_filename
!= '\0')
259 = file_read_description_xml (target_description_filename
);
261 /* Next try to read the description from the current target using
263 if (current_target_desc
== NULL
)
264 current_target_desc
= target_read_description_xml (¤t_target
);
266 /* If that failed try a target-specific hook. */
267 if (current_target_desc
== NULL
)
268 current_target_desc
= target_read_description (¤t_target
);
270 /* If a non-NULL description was returned, then update the current
272 if (current_target_desc
)
274 struct gdbarch_info info
;
276 gdbarch_info_init (&info
);
277 info
.target_desc
= current_target_desc
;
278 if (!gdbarch_update_p (info
))
279 warning (_("Architecture rejected target-supplied description"));
282 struct tdesc_arch_data
*data
;
284 data
= gdbarch_data (target_gdbarch
, tdesc_data
);
285 if (tdesc_has_registers (current_target_desc
)
286 && data
->arch_regs
== NULL
)
287 warning (_("Target-supplied registers are not supported "
288 "by the current architecture"));
292 /* Now that we know this description is usable, record that we
294 target_desc_fetched
= 1;
297 /* Discard any description fetched from the current target, and switch
298 the current architecture to one with no target description. */
301 target_clear_description (void)
303 struct gdbarch_info info
;
305 if (!target_desc_fetched
)
308 target_desc_fetched
= 0;
309 current_target_desc
= NULL
;
311 gdbarch_info_init (&info
);
312 if (!gdbarch_update_p (info
))
313 internal_error (__FILE__
, __LINE__
,
314 _("Could not remove target-supplied description"));
317 /* Return the global current target description. This should only be
318 used by gdbarch initialization code; most access should be through
319 an existing gdbarch. */
321 const struct target_desc
*
322 target_current_description (void)
324 if (target_desc_fetched
)
325 return current_target_desc
;
331 /* Direct accessors for target descriptions. */
333 /* Return the string value of a property named KEY, or NULL if the
334 property was not specified. */
337 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
339 struct property
*prop
;
342 for (ix
= 0; VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
344 if (strcmp (prop
->key
, key
) == 0)
350 /* Return the BFD architecture associated with this target
351 description, or NULL if no architecture was specified. */
353 const struct bfd_arch_info
*
354 tdesc_architecture (const struct target_desc
*target_desc
)
356 return target_desc
->arch
;
359 /* Return the OSABI associated with this target description, or
360 GDB_OSABI_UNKNOWN if no osabi was specified. */
363 tdesc_osabi (const struct target_desc
*target_desc
)
365 return target_desc
->osabi
;
370 /* Return 1 if this target description includes any registers. */
373 tdesc_has_registers (const struct target_desc
*target_desc
)
376 struct tdesc_feature
*feature
;
378 if (target_desc
== NULL
)
382 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
384 if (! VEC_empty (tdesc_reg_p
, feature
->registers
))
390 /* Return the feature with the given name, if present, or NULL if
391 the named feature is not found. */
393 const struct tdesc_feature
*
394 tdesc_find_feature (const struct target_desc
*target_desc
,
398 struct tdesc_feature
*feature
;
401 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
403 if (strcmp (feature
->name
, name
) == 0)
409 /* Return the name of FEATURE. */
412 tdesc_feature_name (const struct tdesc_feature
*feature
)
414 return feature
->name
;
417 /* Predefined types. */
418 static struct tdesc_type tdesc_predefined_types
[] =
420 { "int8", TDESC_TYPE_INT8
},
421 { "int16", TDESC_TYPE_INT16
},
422 { "int32", TDESC_TYPE_INT32
},
423 { "int64", TDESC_TYPE_INT64
},
424 { "int128", TDESC_TYPE_INT128
},
425 { "uint8", TDESC_TYPE_UINT8
},
426 { "uint16", TDESC_TYPE_UINT16
},
427 { "uint32", TDESC_TYPE_UINT32
},
428 { "uint64", TDESC_TYPE_UINT64
},
429 { "uint128", TDESC_TYPE_UINT128
},
430 { "code_ptr", TDESC_TYPE_CODE_PTR
},
431 { "data_ptr", TDESC_TYPE_DATA_PTR
},
432 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
433 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
434 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
}
437 /* Return the type associated with ID in the context of FEATURE, or
441 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
444 struct tdesc_type
*type
;
446 /* First try target-defined types. */
447 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
448 if (strcmp (type
->name
, id
) == 0)
451 /* Next try the predefined types. */
452 for (ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
453 if (strcmp (tdesc_predefined_types
[ix
].name
, id
) == 0)
454 return &tdesc_predefined_types
[ix
];
459 /* Construct, if necessary, and return the GDB type implementing target
460 type TDESC_TYPE for architecture GDBARCH. */
463 tdesc_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*tdesc_type
)
465 switch (tdesc_type
->kind
)
467 /* Predefined types. */
468 case TDESC_TYPE_INT8
:
469 return builtin_type (gdbarch
)->builtin_int8
;
471 case TDESC_TYPE_INT16
:
472 return builtin_type (gdbarch
)->builtin_int16
;
474 case TDESC_TYPE_INT32
:
475 return builtin_type (gdbarch
)->builtin_int32
;
477 case TDESC_TYPE_INT64
:
478 return builtin_type (gdbarch
)->builtin_int64
;
480 case TDESC_TYPE_INT128
:
481 return builtin_type (gdbarch
)->builtin_int128
;
483 case TDESC_TYPE_UINT8
:
484 return builtin_type (gdbarch
)->builtin_uint8
;
486 case TDESC_TYPE_UINT16
:
487 return builtin_type (gdbarch
)->builtin_uint16
;
489 case TDESC_TYPE_UINT32
:
490 return builtin_type (gdbarch
)->builtin_uint32
;
492 case TDESC_TYPE_UINT64
:
493 return builtin_type (gdbarch
)->builtin_uint64
;
495 case TDESC_TYPE_UINT128
:
496 return builtin_type (gdbarch
)->builtin_uint128
;
498 case TDESC_TYPE_CODE_PTR
:
499 return builtin_type (gdbarch
)->builtin_func_ptr
;
501 case TDESC_TYPE_DATA_PTR
:
502 return builtin_type (gdbarch
)->builtin_data_ptr
;
504 case TDESC_TYPE_IEEE_SINGLE
:
505 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_single",
506 floatformats_ieee_single
);
508 case TDESC_TYPE_IEEE_DOUBLE
:
509 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_double",
510 floatformats_ieee_double
);
512 case TDESC_TYPE_ARM_FPA_EXT
:
513 return arch_float_type (gdbarch
, -1, "builtin_type_arm_ext",
514 floatformats_arm_ext
);
516 /* Types defined by a target feature. */
517 case TDESC_TYPE_VECTOR
:
519 struct type
*type
, *field_type
;
521 field_type
= tdesc_gdb_type (gdbarch
, tdesc_type
->u
.v
.type
);
522 type
= init_vector_type (field_type
, tdesc_type
->u
.v
.count
);
523 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
528 case TDESC_TYPE_UNION
:
530 struct type
*type
, *field_type
;
531 struct tdesc_type_field
*f
;
534 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
535 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
538 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
541 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
542 append_composite_type_field (type
, xstrdup (f
->name
), field_type
);
544 /* If any of the children of this union are vectors, flag the
545 union as a vector also. This allows e.g. a union of two
546 vector types to show up automatically in "info vector". */
547 if (TYPE_VECTOR (field_type
))
548 TYPE_VECTOR (type
) = 1;
555 internal_error (__FILE__
, __LINE__
,
556 "Type \"%s\" has an unknown kind %d",
557 tdesc_type
->name
, tdesc_type
->kind
);
561 /* Support for registers from target descriptions. */
563 /* Construct the per-gdbarch data. */
566 tdesc_data_init (struct obstack
*obstack
)
568 struct tdesc_arch_data
*data
;
570 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
574 /* Similar, but for the temporary copy used during architecture
577 struct tdesc_arch_data
*
578 tdesc_data_alloc (void)
580 return XZALLOC (struct tdesc_arch_data
);
583 /* Free something allocated by tdesc_data_alloc, if it is not going
584 to be used (for instance if it was unsuitable for the
588 tdesc_data_cleanup (void *data_untyped
)
590 struct tdesc_arch_data
*data
= data_untyped
;
592 VEC_free (tdesc_arch_reg
, data
->arch_regs
);
596 /* Search FEATURE for a register named NAME. */
598 static struct tdesc_reg
*
599 tdesc_find_register_early (const struct tdesc_feature
*feature
,
603 struct tdesc_reg
*reg
;
606 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
608 if (strcasecmp (reg
->name
, name
) == 0)
614 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
617 tdesc_numbered_register (const struct tdesc_feature
*feature
,
618 struct tdesc_arch_data
*data
,
619 int regno
, const char *name
)
621 struct tdesc_arch_reg arch_reg
= { 0 };
622 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
627 /* Make sure the vector includes a REGNO'th element. */
628 while (regno
>= VEC_length (tdesc_arch_reg
, data
->arch_regs
))
629 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &arch_reg
);
632 VEC_replace (tdesc_arch_reg
, data
->arch_regs
, regno
, &arch_reg
);
636 /* Search FEATURE for a register whose name is in NAMES and assign
640 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
641 struct tdesc_arch_data
*data
,
642 int regno
, const char *const names
[])
646 for (i
= 0; names
[i
] != NULL
; i
++)
647 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
653 /* Search FEATURE for a register named NAME, and return its size in
654 bits. The register must exist. */
657 tdesc_register_size (const struct tdesc_feature
*feature
,
660 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
662 gdb_assert (reg
!= NULL
);
666 /* Look up a register by its GDB internal register number. */
668 static struct tdesc_arch_reg
*
669 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
671 struct tdesc_arch_reg
*reg
;
672 struct tdesc_arch_data
*data
;
674 data
= gdbarch_data (gdbarch
, tdesc_data
);
675 if (regno
< VEC_length (tdesc_arch_reg
, data
->arch_regs
))
676 return VEC_index (tdesc_arch_reg
, data
->arch_regs
, regno
);
681 static struct tdesc_reg
*
682 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
684 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
685 return reg
? reg
->reg
: NULL
;
688 /* Return the name of register REGNO, from the target description or
689 from an architecture-provided pseudo_register_name method. */
692 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
694 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
695 int num_regs
= gdbarch_num_regs (gdbarch
);
696 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
701 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
703 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
704 gdb_assert (data
->pseudo_register_name
!= NULL
);
705 return data
->pseudo_register_name (gdbarch
, regno
);
712 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
714 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
715 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
716 int num_regs
= gdbarch_num_regs (gdbarch
);
717 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
719 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
721 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
722 gdb_assert (data
->pseudo_register_type
!= NULL
);
723 return data
->pseudo_register_type (gdbarch
, regno
);
727 /* Return "int0_t", since "void" has a misleading size of one. */
728 return builtin_type (gdbarch
)->builtin_int0
;
730 if (arch_reg
->type
== NULL
)
732 /* First check for a predefined or target defined type. */
734 arch_reg
->type
= tdesc_gdb_type (gdbarch
, reg
->tdesc_type
);
736 /* Next try size-sensitive type shortcuts. */
737 else if (strcmp (reg
->type
, "float") == 0)
739 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
740 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
741 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
742 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
743 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
744 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
747 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
748 reg
->name
, reg
->bitsize
);
749 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
752 else if (strcmp (reg
->type
, "int") == 0)
754 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
755 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
756 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
757 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
758 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
759 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
760 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
761 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
762 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
763 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
764 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
765 /* A bit desperate by this point... */
766 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
769 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
770 reg
->name
, reg
->bitsize
);
771 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
775 if (arch_reg
->type
== NULL
)
776 internal_error (__FILE__
, __LINE__
,
777 "Register \"%s\" has an unknown type \"%s\"",
778 reg
->name
, reg
->type
);
781 return arch_reg
->type
;
785 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
787 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
790 return reg
->target_regnum
;
795 /* Check whether REGNUM is a member of REGGROUP. Registers from the
796 target description may be classified as general, float, or vector.
797 Unlike a gdbarch register_reggroup_p method, this function will
798 return -1 if it does not know; the caller should handle registers
799 with no specified group.
801 Arbitrary strings (other than "general", "float", and "vector")
802 from the description are not used; they cause the register to be
803 displayed in "info all-registers" but excluded from "info
804 registers" et al. The names of containing features are also not
805 used. This might be extended to display registers in some more
808 The save-restore flag is also implemented here. */
811 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
812 struct reggroup
*reggroup
)
814 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
816 if (reg
!= NULL
&& reg
->group
!= NULL
)
818 int general_p
= 0, float_p
= 0, vector_p
= 0;
820 if (strcmp (reg
->group
, "general") == 0)
822 else if (strcmp (reg
->group
, "float") == 0)
824 else if (strcmp (reg
->group
, "vector") == 0)
827 if (reggroup
== float_reggroup
)
830 if (reggroup
== vector_reggroup
)
833 if (reggroup
== general_reggroup
)
838 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
839 return reg
->save_restore
;
844 /* Check whether REGNUM is a member of REGGROUP. Registers with no
845 group specified go to the default reggroup function and are handled
849 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
850 struct reggroup
*reggroup
)
852 int num_regs
= gdbarch_num_regs (gdbarch
);
853 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
856 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
858 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
859 gdb_assert (data
->pseudo_register_reggroup_p
!= NULL
);
860 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
863 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
867 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
870 /* Record architecture-specific functions to call for pseudo-register
874 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
875 gdbarch_register_name_ftype
*pseudo_name
)
877 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
879 data
->pseudo_register_name
= pseudo_name
;
883 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
884 gdbarch_register_type_ftype
*pseudo_type
)
886 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
888 data
->pseudo_register_type
= pseudo_type
;
892 set_tdesc_pseudo_register_reggroup_p
893 (struct gdbarch
*gdbarch
,
894 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
896 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
898 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
901 /* Update GDBARCH to use the target description for registers. */
904 tdesc_use_registers (struct gdbarch
*gdbarch
,
905 const struct target_desc
*target_desc
,
906 struct tdesc_arch_data
*early_data
)
908 int num_regs
= gdbarch_num_regs (gdbarch
);
910 struct tdesc_feature
*feature
;
911 struct tdesc_reg
*reg
;
912 struct tdesc_arch_data
*data
;
913 struct tdesc_arch_reg
*arch_reg
, new_arch_reg
= { 0 };
916 /* We can't use the description for registers if it doesn't describe
917 any. This function should only be called after validating
918 registers, so the caller should know that registers are
920 gdb_assert (tdesc_has_registers (target_desc
));
922 data
= gdbarch_data (gdbarch
, tdesc_data
);
923 data
->arch_regs
= early_data
->arch_regs
;
926 /* Build up a set of all registers, so that we can assign register
927 numbers where needed. The hash table expands as necessary, so
928 the initial size is arbitrary. */
929 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
931 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ixf
, feature
);
934 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
937 void **slot
= htab_find_slot (reg_hash
, reg
, INSERT
);
942 /* Remove any registers which were assigned numbers by the
945 VEC_iterate (tdesc_arch_reg
, data
->arch_regs
, ixr
, arch_reg
);
948 htab_remove_elt (reg_hash
, arch_reg
->reg
);
950 /* Assign numbers to the remaining registers and add them to the
951 list of registers. The new numbers are always above gdbarch_num_regs.
952 Iterate over the features, not the hash table, so that the order
953 matches that in the target description. */
955 gdb_assert (VEC_length (tdesc_arch_reg
, data
->arch_regs
) <= num_regs
);
956 while (VEC_length (tdesc_arch_reg
, data
->arch_regs
) < num_regs
)
957 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
959 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ixf
, feature
);
962 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
964 if (htab_find (reg_hash
, reg
) != NULL
)
966 new_arch_reg
.reg
= reg
;
967 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
971 htab_delete (reg_hash
);
973 /* Update the architecture. */
974 set_gdbarch_num_regs (gdbarch
, num_regs
);
975 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
976 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
977 set_gdbarch_remote_register_number (gdbarch
,
978 tdesc_remote_register_number
);
979 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
983 /* Methods for constructing a target description. */
986 tdesc_free_reg (struct tdesc_reg
*reg
)
995 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
996 int regnum
, int save_restore
, const char *group
,
997 int bitsize
, const char *type
)
999 struct tdesc_reg
*reg
= XZALLOC (struct tdesc_reg
);
1001 reg
->name
= xstrdup (name
);
1002 reg
->target_regnum
= regnum
;
1003 reg
->save_restore
= save_restore
;
1004 reg
->group
= group
? xstrdup (group
) : NULL
;
1005 reg
->bitsize
= bitsize
;
1006 reg
->type
= type
? xstrdup (type
) : xstrdup ("<unknown>");
1008 /* If the register's type is target-defined, look it up now. We may not
1009 have easy access to the containing feature when we want it later. */
1010 reg
->tdesc_type
= tdesc_named_type (feature
, reg
->type
);
1012 VEC_safe_push (tdesc_reg_p
, feature
->registers
, reg
);
1016 tdesc_free_type (struct tdesc_type
*type
)
1021 case TDESC_TYPE_UNION
:
1023 struct tdesc_type_field
*f
;
1027 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix
, f
);
1031 VEC_free (tdesc_type_field
, type
->u
.u
.fields
);
1044 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1045 struct tdesc_type
*field_type
, int count
)
1047 struct tdesc_type
*type
= XZALLOC (struct tdesc_type
);
1049 type
->name
= xstrdup (name
);
1050 type
->kind
= TDESC_TYPE_VECTOR
;
1051 type
->u
.v
.type
= field_type
;
1052 type
->u
.v
.count
= count
;
1054 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1059 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1061 struct tdesc_type
*type
= XZALLOC (struct tdesc_type
);
1063 type
->name
= xstrdup (name
);
1064 type
->kind
= TDESC_TYPE_UNION
;
1066 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1071 tdesc_add_field (struct tdesc_type
*type
, const char *field_name
,
1072 struct tdesc_type
*field_type
)
1074 struct tdesc_type_field f
= { 0 };
1076 gdb_assert (type
->kind
== TDESC_TYPE_UNION
);
1078 f
.name
= xstrdup (field_name
);
1079 f
.type
= field_type
;
1081 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1085 tdesc_free_feature (struct tdesc_feature
*feature
)
1087 struct tdesc_reg
*reg
;
1088 struct tdesc_type
*type
;
1091 for (ix
= 0; VEC_iterate (tdesc_reg_p
, feature
->registers
, ix
, reg
); ix
++)
1092 tdesc_free_reg (reg
);
1093 VEC_free (tdesc_reg_p
, feature
->registers
);
1095 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
1096 tdesc_free_type (type
);
1097 VEC_free (tdesc_type_p
, feature
->types
);
1099 xfree (feature
->name
);
1103 struct tdesc_feature
*
1104 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
)
1106 struct tdesc_feature
*new_feature
= XZALLOC (struct tdesc_feature
);
1108 new_feature
->name
= xstrdup (name
);
1110 VEC_safe_push (tdesc_feature_p
, tdesc
->features
, new_feature
);
1114 struct target_desc
*
1115 allocate_target_description (void)
1117 return XZALLOC (struct target_desc
);
1121 free_target_description (void *arg
)
1123 struct target_desc
*target_desc
= arg
;
1124 struct tdesc_feature
*feature
;
1125 struct property
*prop
;
1129 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
1131 tdesc_free_feature (feature
);
1132 VEC_free (tdesc_feature_p
, target_desc
->features
);
1135 VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
1139 xfree (prop
->value
);
1141 VEC_free (property_s
, target_desc
->properties
);
1143 xfree (target_desc
);
1147 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1149 return make_cleanup (free_target_description
, target_desc
);
1153 set_tdesc_property (struct target_desc
*target_desc
,
1154 const char *key
, const char *value
)
1156 struct property
*prop
, new_prop
;
1159 gdb_assert (key
!= NULL
&& value
!= NULL
);
1161 for (ix
= 0; VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
1163 if (strcmp (prop
->key
, key
) == 0)
1164 internal_error (__FILE__
, __LINE__
,
1165 _("Attempted to add duplicate property \"%s\""), key
);
1167 new_prop
.key
= xstrdup (key
);
1168 new_prop
.value
= xstrdup (value
);
1169 VEC_safe_push (property_s
, target_desc
->properties
, &new_prop
);
1173 set_tdesc_architecture (struct target_desc
*target_desc
,
1174 const struct bfd_arch_info
*arch
)
1176 target_desc
->arch
= arch
;
1180 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1182 target_desc
->osabi
= osabi
;
1186 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1187 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1189 /* Helper functions for the CLI commands. */
1192 set_tdesc_cmd (char *args
, int from_tty
)
1194 help_list (tdesc_set_cmdlist
, "set tdesc ", -1, gdb_stdout
);
1198 show_tdesc_cmd (char *args
, int from_tty
)
1200 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1204 unset_tdesc_cmd (char *args
, int from_tty
)
1206 help_list (tdesc_unset_cmdlist
, "unset tdesc ", -1, gdb_stdout
);
1210 set_tdesc_filename_cmd (char *args
, int from_tty
,
1211 struct cmd_list_element
*c
)
1213 target_clear_description ();
1214 target_find_description ();
1218 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1219 struct cmd_list_element
*c
,
1222 if (value
!= NULL
&& *value
!= '\0')
1223 printf_filtered (_("\
1224 The target description will be read from \"%s\".\n"),
1227 printf_filtered (_("\
1228 The target description will be read from the target.\n"));
1232 unset_tdesc_filename_cmd (char *args
, int from_tty
)
1234 xfree (target_description_filename
);
1235 target_description_filename
= NULL
;
1236 target_clear_description ();
1237 target_find_description ();
1241 maint_print_c_tdesc_cmd (char *args
, int from_tty
)
1243 const struct target_desc
*tdesc
;
1244 const char *filename
, *inp
;
1245 char *function
, *outp
;
1246 struct property
*prop
;
1247 struct tdesc_feature
*feature
;
1248 struct tdesc_reg
*reg
;
1249 struct tdesc_type
*type
;
1250 struct tdesc_type_field
*f
;
1253 /* Use the global target-supplied description, not the current
1254 architecture's. This lets a GDB for one architecture generate C
1255 for another architecture's description, even though the gdbarch
1256 initialization code will reject the new description. */
1257 tdesc
= current_target_desc
;
1259 error (_("There is no target description to print."));
1261 if (target_description_filename
== NULL
)
1262 error (_("The current target description did not come from an XML file."));
1264 filename
= lbasename (target_description_filename
);
1265 function
= alloca (strlen (filename
) + 1);
1266 for (inp
= filename
, outp
= function
; *inp
!= '\0'; inp
++)
1269 else if (*inp
== '-')
1275 /* Standard boilerplate. */
1276 printf_unfiltered ("/* THIS FILE IS GENERATED. Original: %s */\n\n",
1278 printf_unfiltered ("#include \"defs.h\"\n");
1279 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1280 printf_unfiltered ("\n");
1282 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function
);
1283 printf_unfiltered ("static void\n");
1284 printf_unfiltered ("initialize_tdesc_%s (void)\n", function
);
1285 printf_unfiltered ("{\n");
1287 (" struct target_desc *result = allocate_target_description ();\n");
1288 printf_unfiltered (" struct tdesc_feature *feature;\n");
1289 printf_unfiltered (" struct tdesc_type *field_type, *type;\n");
1290 printf_unfiltered ("\n");
1292 if (tdesc_architecture (tdesc
) != NULL
)
1295 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1296 tdesc_architecture (tdesc
)->printable_name
);
1297 printf_unfiltered ("\n");
1300 for (ix
= 0; VEC_iterate (property_s
, tdesc
->properties
, ix
, prop
);
1303 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1304 prop
->key
, prop
->value
);
1308 VEC_iterate (tdesc_feature_p
, tdesc
->features
, ix
, feature
);
1311 printf_unfiltered (" feature = tdesc_create_feature (result, \"%s\");\n",
1315 VEC_iterate (tdesc_type_p
, feature
->types
, ix2
, type
);
1320 case TDESC_TYPE_VECTOR
:
1322 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1323 type
->u
.v
.type
->name
);
1325 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1326 type
->name
, type
->u
.v
.count
);
1328 case TDESC_TYPE_UNION
:
1330 (" type = tdesc_create_union (feature, \"%s\");\n",
1333 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
1337 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1340 (" tdesc_add_field (type, \"%s\", field_type);\n",
1345 error (_("C output is not supported type \"%s\"."), type
->name
);
1347 printf_unfiltered ("\n");
1351 VEC_iterate (tdesc_reg_p
, feature
->registers
, ix2
, reg
);
1354 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1355 reg
->name
, reg
->target_regnum
, reg
->save_restore
);
1357 printf_unfiltered ("\"%s\", ", reg
->group
);
1359 printf_unfiltered ("NULL, ");
1360 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
);
1363 printf_unfiltered ("\n");
1366 printf_unfiltered (" tdesc_%s = result;\n", function
);
1367 printf_unfiltered ("}\n");
1370 /* Provide a prototype to silence -Wmissing-prototypes. */
1371 extern initialize_file_ftype _initialize_target_descriptions
;
1374 _initialize_target_descriptions (void)
1376 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
1378 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
1379 Set target description specific variables."),
1380 &tdesc_set_cmdlist
, "set tdesc ",
1381 0 /* allow-unknown */, &setlist
);
1382 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
1383 Show target description specific variables."),
1384 &tdesc_show_cmdlist
, "show tdesc ",
1385 0 /* allow-unknown */, &showlist
);
1386 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
1387 Unset target description specific variables."),
1388 &tdesc_unset_cmdlist
, "unset tdesc ",
1389 0 /* allow-unknown */, &unsetlist
);
1391 add_setshow_filename_cmd ("filename", class_obscure
,
1392 &target_description_filename
,
1394 Set the file to read for an XML target description"), _("\
1395 Show the file to read for an XML target description"), _("\
1396 When set, GDB will read the target description from a local\n\
1397 file instead of querying the remote target."),
1398 set_tdesc_filename_cmd
,
1399 show_tdesc_filename_cmd
,
1400 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
1402 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
1403 Unset the file to read for an XML target description. When unset,\n\
1404 GDB will read the description from the target."),
1405 &tdesc_unset_cmdlist
);
1407 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
1408 Print the current target description as a C source file."),
1409 &maintenanceprintlist
);