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 compatible architecture from a target description. */
162 typedef const struct bfd_arch_info
*arch_p
;
165 /* A target description. */
169 /* The architecture reported by the target, if any. */
170 const struct bfd_arch_info
*arch
;
172 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
174 enum gdb_osabi osabi
;
176 /* The list of compatible architectures reported by the target. */
177 VEC(arch_p
) *compatible
;
179 /* Any architecture-specific properties specified by the target. */
180 VEC(property_s
) *properties
;
182 /* The features associated with this target. */
183 VEC(tdesc_feature_p
) *features
;
186 /* Per-architecture data associated with a target description. The
187 target description may be shared by multiple architectures, but
188 this data is private to one gdbarch. */
190 typedef struct tdesc_arch_reg
192 struct tdesc_reg
*reg
;
195 DEF_VEC_O(tdesc_arch_reg
);
197 struct tdesc_arch_data
199 /* A list of register/type pairs, indexed by GDB's internal register number.
200 During initialization of the gdbarch this list is used to store
201 registers which the architecture assigns a fixed register number.
202 Registers which are NULL in this array, or off the end, are
203 treated as zero-sized and nameless (i.e. placeholders in the
205 VEC(tdesc_arch_reg
) *arch_regs
;
207 /* Functions which report the register name, type, and reggroups for
209 gdbarch_register_name_ftype
*pseudo_register_name
;
210 gdbarch_register_type_ftype
*pseudo_register_type
;
211 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
;
214 /* Global state. These variables are associated with the current
215 target; if GDB adds support for multiple simultaneous targets, then
216 these variables should become target-specific data. */
218 /* A flag indicating that a description has already been fetched from
219 the current target, so it should not be queried again. */
221 static int target_desc_fetched
;
223 /* The description fetched from the current target, or NULL if the
224 current target did not supply any description. Only valid when
225 target_desc_fetched is set. Only the description initialization
226 code should access this; normally, the description should be
227 accessed through the gdbarch object. */
229 static const struct target_desc
*current_target_desc
;
231 /* Other global variables. */
233 /* The filename to read a target description from. */
235 static char *target_description_filename
;
237 /* A handle for architecture-specific data associated with the
238 target description (see struct tdesc_arch_data). */
240 static struct gdbarch_data
*tdesc_data
;
242 /* Fetch the current target's description, and switch the current
243 architecture to one which incorporates that description. */
246 target_find_description (void)
248 /* If we've already fetched a description from the target, don't do
249 it again. This allows a target to fetch the description early,
250 during its to_open or to_create_inferior, if it needs extra
251 information about the target to initialize. */
252 if (target_desc_fetched
)
255 /* The current architecture should not have any target description
256 specified. It should have been cleared, e.g. when we
257 disconnected from the previous target. */
258 gdb_assert (gdbarch_target_desc (target_gdbarch
) == NULL
);
260 /* First try to fetch an XML description from the user-specified
262 current_target_desc
= NULL
;
263 if (target_description_filename
!= NULL
264 && *target_description_filename
!= '\0')
266 = file_read_description_xml (target_description_filename
);
268 /* Next try to read the description from the current target using
270 if (current_target_desc
== NULL
)
271 current_target_desc
= target_read_description_xml (¤t_target
);
273 /* If that failed try a target-specific hook. */
274 if (current_target_desc
== NULL
)
275 current_target_desc
= target_read_description (¤t_target
);
277 /* If a non-NULL description was returned, then update the current
279 if (current_target_desc
)
281 struct gdbarch_info info
;
283 gdbarch_info_init (&info
);
284 info
.target_desc
= current_target_desc
;
285 if (!gdbarch_update_p (info
))
286 warning (_("Architecture rejected target-supplied description"));
289 struct tdesc_arch_data
*data
;
291 data
= gdbarch_data (target_gdbarch
, tdesc_data
);
292 if (tdesc_has_registers (current_target_desc
)
293 && data
->arch_regs
== NULL
)
294 warning (_("Target-supplied registers are not supported "
295 "by the current architecture"));
299 /* Now that we know this description is usable, record that we
301 target_desc_fetched
= 1;
304 /* Discard any description fetched from the current target, and switch
305 the current architecture to one with no target description. */
308 target_clear_description (void)
310 struct gdbarch_info info
;
312 if (!target_desc_fetched
)
315 target_desc_fetched
= 0;
316 current_target_desc
= NULL
;
318 gdbarch_info_init (&info
);
319 if (!gdbarch_update_p (info
))
320 internal_error (__FILE__
, __LINE__
,
321 _("Could not remove target-supplied description"));
324 /* Return the global current target description. This should only be
325 used by gdbarch initialization code; most access should be through
326 an existing gdbarch. */
328 const struct target_desc
*
329 target_current_description (void)
331 if (target_desc_fetched
)
332 return current_target_desc
;
337 /* Return non-zero if this target description is compatible
338 with the given BFD architecture. */
341 tdesc_compatible_p (const struct target_desc
*target_desc
,
342 const struct bfd_arch_info
*arch
)
344 const struct bfd_arch_info
*compat
;
347 for (ix
= 0; VEC_iterate (arch_p
, target_desc
->compatible
, ix
, compat
);
351 || arch
->compatible (arch
, compat
)
352 || compat
->compatible (compat
, arch
))
360 /* Direct accessors for target descriptions. */
362 /* Return the string value of a property named KEY, or NULL if the
363 property was not specified. */
366 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
368 struct property
*prop
;
371 for (ix
= 0; VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
373 if (strcmp (prop
->key
, key
) == 0)
379 /* Return the BFD architecture associated with this target
380 description, or NULL if no architecture was specified. */
382 const struct bfd_arch_info
*
383 tdesc_architecture (const struct target_desc
*target_desc
)
385 return target_desc
->arch
;
388 /* Return the OSABI associated with this target description, or
389 GDB_OSABI_UNKNOWN if no osabi was specified. */
392 tdesc_osabi (const struct target_desc
*target_desc
)
394 return target_desc
->osabi
;
399 /* Return 1 if this target description includes any registers. */
402 tdesc_has_registers (const struct target_desc
*target_desc
)
405 struct tdesc_feature
*feature
;
407 if (target_desc
== NULL
)
411 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
413 if (! VEC_empty (tdesc_reg_p
, feature
->registers
))
419 /* Return the feature with the given name, if present, or NULL if
420 the named feature is not found. */
422 const struct tdesc_feature
*
423 tdesc_find_feature (const struct target_desc
*target_desc
,
427 struct tdesc_feature
*feature
;
430 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
432 if (strcmp (feature
->name
, name
) == 0)
438 /* Return the name of FEATURE. */
441 tdesc_feature_name (const struct tdesc_feature
*feature
)
443 return feature
->name
;
446 /* Predefined types. */
447 static struct tdesc_type tdesc_predefined_types
[] =
449 { "int8", TDESC_TYPE_INT8
},
450 { "int16", TDESC_TYPE_INT16
},
451 { "int32", TDESC_TYPE_INT32
},
452 { "int64", TDESC_TYPE_INT64
},
453 { "int128", TDESC_TYPE_INT128
},
454 { "uint8", TDESC_TYPE_UINT8
},
455 { "uint16", TDESC_TYPE_UINT16
},
456 { "uint32", TDESC_TYPE_UINT32
},
457 { "uint64", TDESC_TYPE_UINT64
},
458 { "uint128", TDESC_TYPE_UINT128
},
459 { "code_ptr", TDESC_TYPE_CODE_PTR
},
460 { "data_ptr", TDESC_TYPE_DATA_PTR
},
461 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
462 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
463 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
}
466 /* Return the type associated with ID in the context of FEATURE, or
470 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
473 struct tdesc_type
*type
;
475 /* First try target-defined types. */
476 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
477 if (strcmp (type
->name
, id
) == 0)
480 /* Next try the predefined types. */
481 for (ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
482 if (strcmp (tdesc_predefined_types
[ix
].name
, id
) == 0)
483 return &tdesc_predefined_types
[ix
];
488 /* Construct, if necessary, and return the GDB type implementing target
489 type TDESC_TYPE for architecture GDBARCH. */
492 tdesc_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*tdesc_type
)
494 switch (tdesc_type
->kind
)
496 /* Predefined types. */
497 case TDESC_TYPE_INT8
:
498 return builtin_type (gdbarch
)->builtin_int8
;
500 case TDESC_TYPE_INT16
:
501 return builtin_type (gdbarch
)->builtin_int16
;
503 case TDESC_TYPE_INT32
:
504 return builtin_type (gdbarch
)->builtin_int32
;
506 case TDESC_TYPE_INT64
:
507 return builtin_type (gdbarch
)->builtin_int64
;
509 case TDESC_TYPE_INT128
:
510 return builtin_type (gdbarch
)->builtin_int128
;
512 case TDESC_TYPE_UINT8
:
513 return builtin_type (gdbarch
)->builtin_uint8
;
515 case TDESC_TYPE_UINT16
:
516 return builtin_type (gdbarch
)->builtin_uint16
;
518 case TDESC_TYPE_UINT32
:
519 return builtin_type (gdbarch
)->builtin_uint32
;
521 case TDESC_TYPE_UINT64
:
522 return builtin_type (gdbarch
)->builtin_uint64
;
524 case TDESC_TYPE_UINT128
:
525 return builtin_type (gdbarch
)->builtin_uint128
;
527 case TDESC_TYPE_CODE_PTR
:
528 return builtin_type (gdbarch
)->builtin_func_ptr
;
530 case TDESC_TYPE_DATA_PTR
:
531 return builtin_type (gdbarch
)->builtin_data_ptr
;
533 case TDESC_TYPE_IEEE_SINGLE
:
534 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_single",
535 floatformats_ieee_single
);
537 case TDESC_TYPE_IEEE_DOUBLE
:
538 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_double",
539 floatformats_ieee_double
);
541 case TDESC_TYPE_ARM_FPA_EXT
:
542 return arch_float_type (gdbarch
, -1, "builtin_type_arm_ext",
543 floatformats_arm_ext
);
545 /* Types defined by a target feature. */
546 case TDESC_TYPE_VECTOR
:
548 struct type
*type
, *field_type
;
550 field_type
= tdesc_gdb_type (gdbarch
, tdesc_type
->u
.v
.type
);
551 type
= init_vector_type (field_type
, tdesc_type
->u
.v
.count
);
552 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
557 case TDESC_TYPE_UNION
:
559 struct type
*type
, *field_type
;
560 struct tdesc_type_field
*f
;
563 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
564 TYPE_NAME (type
) = xstrdup (tdesc_type
->name
);
567 VEC_iterate (tdesc_type_field
, tdesc_type
->u
.u
.fields
, ix
, f
);
570 field_type
= tdesc_gdb_type (gdbarch
, f
->type
);
571 append_composite_type_field (type
, xstrdup (f
->name
), field_type
);
573 /* If any of the children of this union are vectors, flag the
574 union as a vector also. This allows e.g. a union of two
575 vector types to show up automatically in "info vector". */
576 if (TYPE_VECTOR (field_type
))
577 TYPE_VECTOR (type
) = 1;
584 internal_error (__FILE__
, __LINE__
,
585 "Type \"%s\" has an unknown kind %d",
586 tdesc_type
->name
, tdesc_type
->kind
);
590 /* Support for registers from target descriptions. */
592 /* Construct the per-gdbarch data. */
595 tdesc_data_init (struct obstack
*obstack
)
597 struct tdesc_arch_data
*data
;
599 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
603 /* Similar, but for the temporary copy used during architecture
606 struct tdesc_arch_data
*
607 tdesc_data_alloc (void)
609 return XZALLOC (struct tdesc_arch_data
);
612 /* Free something allocated by tdesc_data_alloc, if it is not going
613 to be used (for instance if it was unsuitable for the
617 tdesc_data_cleanup (void *data_untyped
)
619 struct tdesc_arch_data
*data
= data_untyped
;
621 VEC_free (tdesc_arch_reg
, data
->arch_regs
);
625 /* Search FEATURE for a register named NAME. */
627 static struct tdesc_reg
*
628 tdesc_find_register_early (const struct tdesc_feature
*feature
,
632 struct tdesc_reg
*reg
;
635 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
637 if (strcasecmp (reg
->name
, name
) == 0)
643 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
646 tdesc_numbered_register (const struct tdesc_feature
*feature
,
647 struct tdesc_arch_data
*data
,
648 int regno
, const char *name
)
650 struct tdesc_arch_reg arch_reg
= { 0 };
651 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
656 /* Make sure the vector includes a REGNO'th element. */
657 while (regno
>= VEC_length (tdesc_arch_reg
, data
->arch_regs
))
658 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &arch_reg
);
661 VEC_replace (tdesc_arch_reg
, data
->arch_regs
, regno
, &arch_reg
);
665 /* Search FEATURE for a register named NAME, but do not assign a fixed
666 register number to it. */
669 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
672 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
680 /* Search FEATURE for a register whose name is in NAMES and assign
684 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
685 struct tdesc_arch_data
*data
,
686 int regno
, const char *const names
[])
690 for (i
= 0; names
[i
] != NULL
; i
++)
691 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
697 /* Search FEATURE for a register named NAME, and return its size in
698 bits. The register must exist. */
701 tdesc_register_size (const struct tdesc_feature
*feature
,
704 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
706 gdb_assert (reg
!= NULL
);
710 /* Look up a register by its GDB internal register number. */
712 static struct tdesc_arch_reg
*
713 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
715 struct tdesc_arch_reg
*reg
;
716 struct tdesc_arch_data
*data
;
718 data
= gdbarch_data (gdbarch
, tdesc_data
);
719 if (regno
< VEC_length (tdesc_arch_reg
, data
->arch_regs
))
720 return VEC_index (tdesc_arch_reg
, data
->arch_regs
, regno
);
725 static struct tdesc_reg
*
726 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
728 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
729 return reg
? reg
->reg
: NULL
;
732 /* Return the name of register REGNO, from the target description or
733 from an architecture-provided pseudo_register_name method. */
736 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
738 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
739 int num_regs
= gdbarch_num_regs (gdbarch
);
740 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
745 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
747 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
748 gdb_assert (data
->pseudo_register_name
!= NULL
);
749 return data
->pseudo_register_name (gdbarch
, regno
);
756 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
758 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
759 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
760 int num_regs
= gdbarch_num_regs (gdbarch
);
761 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
763 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
765 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
766 gdb_assert (data
->pseudo_register_type
!= NULL
);
767 return data
->pseudo_register_type (gdbarch
, regno
);
771 /* Return "int0_t", since "void" has a misleading size of one. */
772 return builtin_type (gdbarch
)->builtin_int0
;
774 if (arch_reg
->type
== NULL
)
776 /* First check for a predefined or target defined type. */
778 arch_reg
->type
= tdesc_gdb_type (gdbarch
, reg
->tdesc_type
);
780 /* Next try size-sensitive type shortcuts. */
781 else if (strcmp (reg
->type
, "float") == 0)
783 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
784 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
785 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
786 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
787 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
788 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
791 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
792 reg
->name
, reg
->bitsize
);
793 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
796 else if (strcmp (reg
->type
, "int") == 0)
798 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
799 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
800 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
801 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
802 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
803 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
804 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
805 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
806 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
807 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
808 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
809 /* A bit desperate by this point... */
810 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
813 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
814 reg
->name
, reg
->bitsize
);
815 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
819 if (arch_reg
->type
== NULL
)
820 internal_error (__FILE__
, __LINE__
,
821 "Register \"%s\" has an unknown type \"%s\"",
822 reg
->name
, reg
->type
);
825 return arch_reg
->type
;
829 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
831 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
834 return reg
->target_regnum
;
839 /* Check whether REGNUM is a member of REGGROUP. Registers from the
840 target description may be classified as general, float, or vector.
841 Unlike a gdbarch register_reggroup_p method, this function will
842 return -1 if it does not know; the caller should handle registers
843 with no specified group.
845 Arbitrary strings (other than "general", "float", and "vector")
846 from the description are not used; they cause the register to be
847 displayed in "info all-registers" but excluded from "info
848 registers" et al. The names of containing features are also not
849 used. This might be extended to display registers in some more
852 The save-restore flag is also implemented here. */
855 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
856 struct reggroup
*reggroup
)
858 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
860 if (reg
!= NULL
&& reg
->group
!= NULL
)
862 int general_p
= 0, float_p
= 0, vector_p
= 0;
864 if (strcmp (reg
->group
, "general") == 0)
866 else if (strcmp (reg
->group
, "float") == 0)
868 else if (strcmp (reg
->group
, "vector") == 0)
871 if (reggroup
== float_reggroup
)
874 if (reggroup
== vector_reggroup
)
877 if (reggroup
== general_reggroup
)
882 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
883 return reg
->save_restore
;
888 /* Check whether REGNUM is a member of REGGROUP. Registers with no
889 group specified go to the default reggroup function and are handled
893 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
894 struct reggroup
*reggroup
)
896 int num_regs
= gdbarch_num_regs (gdbarch
);
897 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
900 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
902 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
903 if (data
->pseudo_register_reggroup_p
!= NULL
)
904 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
905 /* Otherwise fall through to the default reggroup_p. */
908 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
912 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
915 /* Record architecture-specific functions to call for pseudo-register
919 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
920 gdbarch_register_name_ftype
*pseudo_name
)
922 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
924 data
->pseudo_register_name
= pseudo_name
;
928 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
929 gdbarch_register_type_ftype
*pseudo_type
)
931 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
933 data
->pseudo_register_type
= pseudo_type
;
937 set_tdesc_pseudo_register_reggroup_p
938 (struct gdbarch
*gdbarch
,
939 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
941 struct tdesc_arch_data
*data
= gdbarch_data (gdbarch
, tdesc_data
);
943 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
946 /* Update GDBARCH to use the target description for registers. */
949 tdesc_use_registers (struct gdbarch
*gdbarch
,
950 const struct target_desc
*target_desc
,
951 struct tdesc_arch_data
*early_data
)
953 int num_regs
= gdbarch_num_regs (gdbarch
);
955 struct tdesc_feature
*feature
;
956 struct tdesc_reg
*reg
;
957 struct tdesc_arch_data
*data
;
958 struct tdesc_arch_reg
*arch_reg
, new_arch_reg
= { 0 };
961 /* We can't use the description for registers if it doesn't describe
962 any. This function should only be called after validating
963 registers, so the caller should know that registers are
965 gdb_assert (tdesc_has_registers (target_desc
));
967 data
= gdbarch_data (gdbarch
, tdesc_data
);
968 data
->arch_regs
= early_data
->arch_regs
;
971 /* Build up a set of all registers, so that we can assign register
972 numbers where needed. The hash table expands as necessary, so
973 the initial size is arbitrary. */
974 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
976 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ixf
, feature
);
979 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
982 void **slot
= htab_find_slot (reg_hash
, reg
, INSERT
);
987 /* Remove any registers which were assigned numbers by the
990 VEC_iterate (tdesc_arch_reg
, data
->arch_regs
, ixr
, arch_reg
);
993 htab_remove_elt (reg_hash
, arch_reg
->reg
);
995 /* Assign numbers to the remaining registers and add them to the
996 list of registers. The new numbers are always above gdbarch_num_regs.
997 Iterate over the features, not the hash table, so that the order
998 matches that in the target description. */
1000 gdb_assert (VEC_length (tdesc_arch_reg
, data
->arch_regs
) <= num_regs
);
1001 while (VEC_length (tdesc_arch_reg
, data
->arch_regs
) < num_regs
)
1002 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1004 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ixf
, feature
);
1007 VEC_iterate (tdesc_reg_p
, feature
->registers
, ixr
, reg
);
1009 if (htab_find (reg_hash
, reg
) != NULL
)
1011 new_arch_reg
.reg
= reg
;
1012 VEC_safe_push (tdesc_arch_reg
, data
->arch_regs
, &new_arch_reg
);
1016 htab_delete (reg_hash
);
1018 /* Update the architecture. */
1019 set_gdbarch_num_regs (gdbarch
, num_regs
);
1020 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1021 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1022 set_gdbarch_remote_register_number (gdbarch
,
1023 tdesc_remote_register_number
);
1024 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1028 /* Methods for constructing a target description. */
1031 tdesc_free_reg (struct tdesc_reg
*reg
)
1040 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
1041 int regnum
, int save_restore
, const char *group
,
1042 int bitsize
, const char *type
)
1044 struct tdesc_reg
*reg
= XZALLOC (struct tdesc_reg
);
1046 reg
->name
= xstrdup (name
);
1047 reg
->target_regnum
= regnum
;
1048 reg
->save_restore
= save_restore
;
1049 reg
->group
= group
? xstrdup (group
) : NULL
;
1050 reg
->bitsize
= bitsize
;
1051 reg
->type
= type
? xstrdup (type
) : xstrdup ("<unknown>");
1053 /* If the register's type is target-defined, look it up now. We may not
1054 have easy access to the containing feature when we want it later. */
1055 reg
->tdesc_type
= tdesc_named_type (feature
, reg
->type
);
1057 VEC_safe_push (tdesc_reg_p
, feature
->registers
, reg
);
1061 tdesc_free_type (struct tdesc_type
*type
)
1066 case TDESC_TYPE_UNION
:
1068 struct tdesc_type_field
*f
;
1072 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix
, f
);
1076 VEC_free (tdesc_type_field
, type
->u
.u
.fields
);
1089 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1090 struct tdesc_type
*field_type
, int count
)
1092 struct tdesc_type
*type
= XZALLOC (struct tdesc_type
);
1094 type
->name
= xstrdup (name
);
1095 type
->kind
= TDESC_TYPE_VECTOR
;
1096 type
->u
.v
.type
= field_type
;
1097 type
->u
.v
.count
= count
;
1099 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1104 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1106 struct tdesc_type
*type
= XZALLOC (struct tdesc_type
);
1108 type
->name
= xstrdup (name
);
1109 type
->kind
= TDESC_TYPE_UNION
;
1111 VEC_safe_push (tdesc_type_p
, feature
->types
, type
);
1116 tdesc_add_field (struct tdesc_type
*type
, const char *field_name
,
1117 struct tdesc_type
*field_type
)
1119 struct tdesc_type_field f
= { 0 };
1121 gdb_assert (type
->kind
== TDESC_TYPE_UNION
);
1123 f
.name
= xstrdup (field_name
);
1124 f
.type
= field_type
;
1126 VEC_safe_push (tdesc_type_field
, type
->u
.u
.fields
, &f
);
1130 tdesc_free_feature (struct tdesc_feature
*feature
)
1132 struct tdesc_reg
*reg
;
1133 struct tdesc_type
*type
;
1136 for (ix
= 0; VEC_iterate (tdesc_reg_p
, feature
->registers
, ix
, reg
); ix
++)
1137 tdesc_free_reg (reg
);
1138 VEC_free (tdesc_reg_p
, feature
->registers
);
1140 for (ix
= 0; VEC_iterate (tdesc_type_p
, feature
->types
, ix
, type
); ix
++)
1141 tdesc_free_type (type
);
1142 VEC_free (tdesc_type_p
, feature
->types
);
1144 xfree (feature
->name
);
1148 struct tdesc_feature
*
1149 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
)
1151 struct tdesc_feature
*new_feature
= XZALLOC (struct tdesc_feature
);
1153 new_feature
->name
= xstrdup (name
);
1155 VEC_safe_push (tdesc_feature_p
, tdesc
->features
, new_feature
);
1159 struct target_desc
*
1160 allocate_target_description (void)
1162 return XZALLOC (struct target_desc
);
1166 free_target_description (void *arg
)
1168 struct target_desc
*target_desc
= arg
;
1169 struct tdesc_feature
*feature
;
1170 struct property
*prop
;
1174 VEC_iterate (tdesc_feature_p
, target_desc
->features
, ix
, feature
);
1176 tdesc_free_feature (feature
);
1177 VEC_free (tdesc_feature_p
, target_desc
->features
);
1180 VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
1184 xfree (prop
->value
);
1186 VEC_free (property_s
, target_desc
->properties
);
1188 VEC_free (arch_p
, target_desc
->compatible
);
1190 xfree (target_desc
);
1194 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1196 return make_cleanup (free_target_description
, target_desc
);
1200 tdesc_add_compatible (struct target_desc
*target_desc
,
1201 const struct bfd_arch_info
*compatible
)
1203 const struct bfd_arch_info
*compat
;
1206 /* If this instance of GDB is compiled without BFD support for the
1207 compatible architecture, simply ignore it -- we would not be able
1208 to handle it anyway. */
1209 if (compatible
== NULL
)
1212 for (ix
= 0; VEC_iterate (arch_p
, target_desc
->compatible
, ix
, compat
);
1214 if (compat
== compatible
)
1215 internal_error (__FILE__
, __LINE__
,
1216 _("Attempted to add duplicate "
1217 "compatible architecture \"%s\""),
1218 compatible
->printable_name
);
1220 VEC_safe_push (arch_p
, target_desc
->compatible
, compatible
);
1224 set_tdesc_property (struct target_desc
*target_desc
,
1225 const char *key
, const char *value
)
1227 struct property
*prop
, new_prop
;
1230 gdb_assert (key
!= NULL
&& value
!= NULL
);
1232 for (ix
= 0; VEC_iterate (property_s
, target_desc
->properties
, ix
, prop
);
1234 if (strcmp (prop
->key
, key
) == 0)
1235 internal_error (__FILE__
, __LINE__
,
1236 _("Attempted to add duplicate property \"%s\""), key
);
1238 new_prop
.key
= xstrdup (key
);
1239 new_prop
.value
= xstrdup (value
);
1240 VEC_safe_push (property_s
, target_desc
->properties
, &new_prop
);
1244 set_tdesc_architecture (struct target_desc
*target_desc
,
1245 const struct bfd_arch_info
*arch
)
1247 target_desc
->arch
= arch
;
1251 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1253 target_desc
->osabi
= osabi
;
1257 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1258 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1260 /* Helper functions for the CLI commands. */
1263 set_tdesc_cmd (char *args
, int from_tty
)
1265 help_list (tdesc_set_cmdlist
, "set tdesc ", -1, gdb_stdout
);
1269 show_tdesc_cmd (char *args
, int from_tty
)
1271 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1275 unset_tdesc_cmd (char *args
, int from_tty
)
1277 help_list (tdesc_unset_cmdlist
, "unset tdesc ", -1, gdb_stdout
);
1281 set_tdesc_filename_cmd (char *args
, int from_tty
,
1282 struct cmd_list_element
*c
)
1284 target_clear_description ();
1285 target_find_description ();
1289 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1290 struct cmd_list_element
*c
,
1293 if (value
!= NULL
&& *value
!= '\0')
1294 printf_filtered (_("\
1295 The target description will be read from \"%s\".\n"),
1298 printf_filtered (_("\
1299 The target description will be read from the target.\n"));
1303 unset_tdesc_filename_cmd (char *args
, int from_tty
)
1305 xfree (target_description_filename
);
1306 target_description_filename
= NULL
;
1307 target_clear_description ();
1308 target_find_description ();
1312 maint_print_c_tdesc_cmd (char *args
, int from_tty
)
1314 const struct target_desc
*tdesc
;
1315 const struct bfd_arch_info
*compatible
;
1316 const char *filename
, *inp
;
1317 char *function
, *outp
;
1318 struct property
*prop
;
1319 struct tdesc_feature
*feature
;
1320 struct tdesc_reg
*reg
;
1321 struct tdesc_type
*type
;
1322 struct tdesc_type_field
*f
;
1325 /* Use the global target-supplied description, not the current
1326 architecture's. This lets a GDB for one architecture generate C
1327 for another architecture's description, even though the gdbarch
1328 initialization code will reject the new description. */
1329 tdesc
= current_target_desc
;
1331 error (_("There is no target description to print."));
1333 if (target_description_filename
== NULL
)
1334 error (_("The current target description did not come from an XML file."));
1336 filename
= lbasename (target_description_filename
);
1337 function
= alloca (strlen (filename
) + 1);
1338 for (inp
= filename
, outp
= function
; *inp
!= '\0'; inp
++)
1341 else if (*inp
== '-')
1347 /* Standard boilerplate. */
1348 printf_unfiltered ("/* THIS FILE IS GENERATED. Original: %s */\n\n",
1350 printf_unfiltered ("#include \"defs.h\"\n");
1351 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1352 printf_unfiltered ("\n");
1354 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function
);
1355 printf_unfiltered ("static void\n");
1356 printf_unfiltered ("initialize_tdesc_%s (void)\n", function
);
1357 printf_unfiltered ("{\n");
1359 (" struct target_desc *result = allocate_target_description ();\n");
1360 printf_unfiltered (" struct tdesc_feature *feature;\n");
1361 printf_unfiltered (" struct tdesc_type *field_type, *type;\n");
1362 printf_unfiltered ("\n");
1364 if (tdesc_architecture (tdesc
) != NULL
)
1367 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1368 tdesc_architecture (tdesc
)->printable_name
);
1369 printf_unfiltered ("\n");
1372 for (ix
= 0; VEC_iterate (arch_p
, tdesc
->compatible
, ix
, compatible
);
1376 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1377 compatible
->printable_name
);
1380 printf_unfiltered ("\n");
1382 for (ix
= 0; VEC_iterate (property_s
, tdesc
->properties
, ix
, prop
);
1385 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1386 prop
->key
, prop
->value
);
1390 VEC_iterate (tdesc_feature_p
, tdesc
->features
, ix
, feature
);
1393 printf_unfiltered (" feature = tdesc_create_feature (result, \"%s\");\n",
1397 VEC_iterate (tdesc_type_p
, feature
->types
, ix2
, type
);
1402 case TDESC_TYPE_VECTOR
:
1404 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1405 type
->u
.v
.type
->name
);
1407 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1408 type
->name
, type
->u
.v
.count
);
1410 case TDESC_TYPE_UNION
:
1412 (" type = tdesc_create_union (feature, \"%s\");\n",
1415 VEC_iterate (tdesc_type_field
, type
->u
.u
.fields
, ix3
, f
);
1419 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1422 (" tdesc_add_field (type, \"%s\", field_type);\n",
1427 error (_("C output is not supported type \"%s\"."), type
->name
);
1429 printf_unfiltered ("\n");
1433 VEC_iterate (tdesc_reg_p
, feature
->registers
, ix2
, reg
);
1436 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1437 reg
->name
, reg
->target_regnum
, reg
->save_restore
);
1439 printf_unfiltered ("\"%s\", ", reg
->group
);
1441 printf_unfiltered ("NULL, ");
1442 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
);
1445 printf_unfiltered ("\n");
1448 printf_unfiltered (" tdesc_%s = result;\n", function
);
1449 printf_unfiltered ("}\n");
1452 /* Provide a prototype to silence -Wmissing-prototypes. */
1453 extern initialize_file_ftype _initialize_target_descriptions
;
1456 _initialize_target_descriptions (void)
1458 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
1460 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
1461 Set target description specific variables."),
1462 &tdesc_set_cmdlist
, "set tdesc ",
1463 0 /* allow-unknown */, &setlist
);
1464 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
1465 Show target description specific variables."),
1466 &tdesc_show_cmdlist
, "show tdesc ",
1467 0 /* allow-unknown */, &showlist
);
1468 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
1469 Unset target description specific variables."),
1470 &tdesc_unset_cmdlist
, "unset tdesc ",
1471 0 /* allow-unknown */, &unsetlist
);
1473 add_setshow_filename_cmd ("filename", class_obscure
,
1474 &target_description_filename
,
1476 Set the file to read for an XML target description"), _("\
1477 Show the file to read for an XML target description"), _("\
1478 When set, GDB will read the target description from a local\n\
1479 file instead of querying the remote target."),
1480 set_tdesc_filename_cmd
,
1481 show_tdesc_filename_cmd
,
1482 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
1484 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
1485 Unset the file to read for an XML target description. When unset,\n\
1486 GDB will read the description from the target."),
1487 &tdesc_unset_cmdlist
);
1489 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
1490 Print the current target description as a C source file."),
1491 &maintenanceprintlist
);