1 /* Target description support for GDB.
3 Copyright (C) 2006-2018 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "arch-utils.h"
26 #include "reggroups.h"
28 #include "target-descriptions.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
34 #include "gdb_obstack.h"
38 #include "completer.h"
39 #include "readline/tilde.h" /* tilde_expand */
41 /* The interface to visit different elements of target description. */
43 class tdesc_element_visitor
46 virtual void visit_pre (const target_desc
*e
) = 0;
47 virtual void visit_post (const target_desc
*e
) = 0;
49 virtual void visit_pre (const tdesc_feature
*e
) = 0;
50 virtual void visit_post (const tdesc_feature
*e
) = 0;
52 virtual void visit (const tdesc_type_builtin
*e
) = 0;
53 virtual void visit (const tdesc_type_vector
*e
) = 0;
54 virtual void visit (const tdesc_type_with_fields
*e
) = 0;
56 virtual void visit (const tdesc_reg
*e
) = 0;
62 virtual void accept (tdesc_element_visitor
&v
) const = 0;
69 property (const std::string
&key_
, const std::string
&value_
)
70 : key (key_
), value (value_
)
77 /* An individual register from a target description. */
79 struct tdesc_reg
: tdesc_element
81 tdesc_reg (struct tdesc_feature
*feature
, const std::string
&name_
,
82 int regnum
, int save_restore_
, const char *group_
,
83 int bitsize_
, const char *type_
)
84 : name (name_
), target_regnum (regnum
),
85 save_restore (save_restore_
),
86 group (group_
!= NULL
? group_
: ""),
88 type (type_
!= NULL
? type_
: "<unknown>")
90 /* If the register's type is target-defined, look it up now. We may not
91 have easy access to the containing feature when we want it later. */
92 tdesc_type
= tdesc_named_type (feature
, type
.c_str ());
95 virtual ~tdesc_reg () = default;
97 DISABLE_COPY_AND_ASSIGN (tdesc_reg
);
99 /* The name of this register. In standard features, it may be
100 recognized by the architecture support code, or it may be purely
104 /* The register number used by this target to refer to this
105 register. This is used for remote p/P packets and to determine
106 the ordering of registers in the remote g/G packets. */
109 /* If this flag is set, GDB should save and restore this register
110 around calls to an inferior function. */
113 /* The name of the register group containing this register, or empty
114 if the group should be automatically determined from the register's
115 type. This is traditionally "general", "float", "vector" but can
116 also be an arbitrary string. If defined the corresponding "info"
117 command should display this register's value. The string should be
118 limited to alphanumeric characters and internal hyphens. */
121 /* The size of the register, in bits. */
124 /* The type of the register. This string corresponds to either
125 a named type from the target description or a predefined
129 /* The target-described type corresponding to TYPE, if found. */
130 struct tdesc_type
*tdesc_type
;
132 void accept (tdesc_element_visitor
&v
) const override
137 bool operator== (const tdesc_reg
&other
) const
139 return (name
== other
.name
140 && target_regnum
== other
.target_regnum
141 && save_restore
== other
.save_restore
142 && bitsize
== other
.bitsize
143 && group
== other
.group
144 && type
== other
.type
);
147 bool operator!= (const tdesc_reg
&other
) const
149 return !(*this == other
);
153 typedef std::unique_ptr
<tdesc_reg
> tdesc_reg_up
;
155 /* A named type from a target description. */
157 struct tdesc_type_field
159 tdesc_type_field (const std::string
&name_
, tdesc_type
*type_
,
160 int start_
, int end_
)
161 : name (name_
), type (type_
), start (start_
), end (end_
)
165 struct tdesc_type
*type
;
166 /* For non-enum-values, either both are -1 (non-bitfield), or both are
167 not -1 (bitfield). For enum values, start is the value (which could be
174 /* Predefined types. */
188 TDESC_TYPE_IEEE_SINGLE
,
189 TDESC_TYPE_IEEE_DOUBLE
,
190 TDESC_TYPE_ARM_FPA_EXT
,
193 /* Types defined by a target feature. */
201 struct tdesc_type
: tdesc_element
203 tdesc_type (const std::string
&name_
, enum tdesc_type_kind kind_
)
204 : name (name_
), kind (kind_
)
207 virtual ~tdesc_type () = default;
209 DISABLE_COPY_AND_ASSIGN (tdesc_type
);
211 /* The name of this type. */
214 /* Identify the kind of this type. */
215 enum tdesc_type_kind kind
;
217 bool operator== (const tdesc_type
&other
) const
219 return name
== other
.name
&& kind
== other
.kind
;
222 bool operator!= (const tdesc_type
&other
) const
224 return !(*this == other
);
227 /* Construct, if necessary, and return the GDB type implementing this
228 target type for architecture GDBARCH. */
230 virtual type
*make_gdb_type (struct gdbarch
*gdbarch
) const = 0;
233 typedef std::unique_ptr
<tdesc_type
> tdesc_type_up
;
235 struct tdesc_type_builtin
: tdesc_type
237 tdesc_type_builtin (const std::string
&name
, enum tdesc_type_kind kind
)
238 : tdesc_type (name
, kind
)
241 void accept (tdesc_element_visitor
&v
) const override
246 type
*make_gdb_type (struct gdbarch
*gdbarch
) const override
250 /* Predefined types. */
251 case TDESC_TYPE_BOOL
:
252 return builtin_type (gdbarch
)->builtin_bool
;
254 case TDESC_TYPE_INT8
:
255 return builtin_type (gdbarch
)->builtin_int8
;
257 case TDESC_TYPE_INT16
:
258 return builtin_type (gdbarch
)->builtin_int16
;
260 case TDESC_TYPE_INT32
:
261 return builtin_type (gdbarch
)->builtin_int32
;
263 case TDESC_TYPE_INT64
:
264 return builtin_type (gdbarch
)->builtin_int64
;
266 case TDESC_TYPE_INT128
:
267 return builtin_type (gdbarch
)->builtin_int128
;
269 case TDESC_TYPE_UINT8
:
270 return builtin_type (gdbarch
)->builtin_uint8
;
272 case TDESC_TYPE_UINT16
:
273 return builtin_type (gdbarch
)->builtin_uint16
;
275 case TDESC_TYPE_UINT32
:
276 return builtin_type (gdbarch
)->builtin_uint32
;
278 case TDESC_TYPE_UINT64
:
279 return builtin_type (gdbarch
)->builtin_uint64
;
281 case TDESC_TYPE_UINT128
:
282 return builtin_type (gdbarch
)->builtin_uint128
;
284 case TDESC_TYPE_CODE_PTR
:
285 return builtin_type (gdbarch
)->builtin_func_ptr
;
287 case TDESC_TYPE_DATA_PTR
:
288 return builtin_type (gdbarch
)->builtin_data_ptr
;
291 type
*gdb_type
= tdesc_find_type (gdbarch
, this->name
.c_str ());
292 if (gdb_type
!= NULL
)
297 case TDESC_TYPE_IEEE_SINGLE
:
298 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_single",
299 floatformats_ieee_single
);
301 case TDESC_TYPE_IEEE_DOUBLE
:
302 return arch_float_type (gdbarch
, -1, "builtin_type_ieee_double",
303 floatformats_ieee_double
);
305 case TDESC_TYPE_ARM_FPA_EXT
:
306 return arch_float_type (gdbarch
, -1, "builtin_type_arm_ext",
307 floatformats_arm_ext
);
309 case TDESC_TYPE_I387_EXT
:
310 return arch_float_type (gdbarch
, -1, "builtin_type_i387_ext",
311 floatformats_i387_ext
);
314 internal_error (__FILE__
, __LINE__
,
315 "Type \"%s\" has an unknown kind %d",
316 this->name
.c_str (), this->kind
);
322 /* tdesc_type for vector types. */
324 struct tdesc_type_vector
: tdesc_type
326 tdesc_type_vector (const std::string
&name
, tdesc_type
*element_type_
, int count_
)
327 : tdesc_type (name
, TDESC_TYPE_VECTOR
),
328 element_type (element_type_
), count (count_
)
331 void accept (tdesc_element_visitor
&v
) const override
336 type
*make_gdb_type (struct gdbarch
*gdbarch
) const override
338 type
*vector_gdb_type
= tdesc_find_type (gdbarch
, this->name
.c_str ());
339 if (vector_gdb_type
!= NULL
)
340 return vector_gdb_type
;
342 type
*element_gdb_type
= this->element_type
->make_gdb_type (gdbarch
);
343 vector_gdb_type
= init_vector_type (element_gdb_type
, this->count
);
344 TYPE_NAME (vector_gdb_type
) = xstrdup (this->name
.c_str ());
346 return vector_gdb_type
;
349 struct tdesc_type
*element_type
;
353 /* tdesc_type for struct, union, flags, and enum types. */
355 struct tdesc_type_with_fields
: tdesc_type
357 tdesc_type_with_fields (const std::string
&name
, tdesc_type_kind kind
,
359 : tdesc_type (name
, kind
), size (size_
)
362 void accept (tdesc_element_visitor
&v
) const override
367 type
*make_gdb_type_struct (struct gdbarch
*gdbarch
) const
369 type
*struct_gdb_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
370 TYPE_NAME (struct_gdb_type
) = xstrdup (this->name
.c_str ());
371 TYPE_TAG_NAME (struct_gdb_type
) = TYPE_NAME (struct_gdb_type
);
373 for (const tdesc_type_field
&f
: this->fields
)
375 if (f
.start
!= -1 && f
.end
!= -1)
379 struct type
*field_gdb_type
;
380 int bitsize
, total_size
;
382 /* This invariant should be preserved while creating types. */
383 gdb_assert (this->size
!= 0);
385 field_gdb_type
= f
.type
->make_gdb_type (gdbarch
);
386 else if (this->size
> 4)
387 field_gdb_type
= builtin_type (gdbarch
)->builtin_uint64
;
389 field_gdb_type
= builtin_type (gdbarch
)->builtin_uint32
;
391 fld
= append_composite_type_field_raw
392 (struct_gdb_type
, xstrdup (f
.name
.c_str ()), field_gdb_type
);
394 /* For little-endian, BITPOS counts from the LSB of
395 the structure and marks the LSB of the field. For
396 big-endian, BITPOS counts from the MSB of the
397 structure and marks the MSB of the field. Either
398 way, it is the number of bits to the "left" of the
399 field. To calculate this in big-endian, we need
400 the total size of the structure. */
401 bitsize
= f
.end
- f
.start
+ 1;
402 total_size
= this->size
* TARGET_CHAR_BIT
;
403 if (gdbarch_bits_big_endian (gdbarch
))
404 SET_FIELD_BITPOS (fld
[0], total_size
- f
.start
- bitsize
);
406 SET_FIELD_BITPOS (fld
[0], f
.start
);
407 FIELD_BITSIZE (fld
[0]) = bitsize
;
411 gdb_assert (f
.start
== -1 && f
.end
== -1);
412 type
*field_gdb_type
= f
.type
->make_gdb_type (gdbarch
);
413 append_composite_type_field (struct_gdb_type
,
414 xstrdup (f
.name
.c_str ()),
420 TYPE_LENGTH (struct_gdb_type
) = this->size
;
422 return struct_gdb_type
;
425 type
*make_gdb_type_union (struct gdbarch
*gdbarch
) const
427 type
*union_gdb_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
428 TYPE_NAME (union_gdb_type
) = xstrdup (this->name
.c_str ());
430 for (const tdesc_type_field
&f
: this->fields
)
432 type
* field_gdb_type
= f
.type
->make_gdb_type (gdbarch
);
433 append_composite_type_field (union_gdb_type
, xstrdup (f
.name
.c_str ()),
436 /* If any of the children of a union are vectors, flag the
437 union as a vector also. This allows e.g. a union of two
438 vector types to show up automatically in "info vector". */
439 if (TYPE_VECTOR (field_gdb_type
))
440 TYPE_VECTOR (union_gdb_type
) = 1;
443 return union_gdb_type
;
446 type
*make_gdb_type_flags (struct gdbarch
*gdbarch
) const
448 type
*flags_gdb_type
= arch_flags_type (gdbarch
, this->name
.c_str (),
449 this->size
* TARGET_CHAR_BIT
);
451 for (const tdesc_type_field
&f
: this->fields
)
453 int bitsize
= f
.end
- f
.start
+ 1;
455 gdb_assert (f
.type
!= NULL
);
456 type
*field_gdb_type
= f
.type
->make_gdb_type (gdbarch
);
457 append_flags_type_field (flags_gdb_type
, f
.start
, bitsize
,
458 field_gdb_type
, f
.name
.c_str ());
461 return flags_gdb_type
;
464 type
*make_gdb_type_enum (struct gdbarch
*gdbarch
) const
466 type
*enum_gdb_type
= arch_type (gdbarch
, TYPE_CODE_ENUM
,
467 this->size
* TARGET_CHAR_BIT
,
468 this->name
.c_str ());
470 TYPE_UNSIGNED (enum_gdb_type
) = 1;
471 for (const tdesc_type_field
&f
: this->fields
)
474 = append_composite_type_field_raw (enum_gdb_type
,
475 xstrdup (f
.name
.c_str ()),
478 SET_FIELD_BITPOS (fld
[0], f
.start
);
481 return enum_gdb_type
;
484 type
*make_gdb_type (struct gdbarch
*gdbarch
) const override
486 type
*gdb_type
= tdesc_find_type (gdbarch
, this->name
.c_str ());
487 if (gdb_type
!= NULL
)
492 case TDESC_TYPE_STRUCT
:
493 return make_gdb_type_struct (gdbarch
);
494 case TDESC_TYPE_UNION
:
495 return make_gdb_type_union (gdbarch
);
496 case TDESC_TYPE_FLAGS
:
497 return make_gdb_type_flags (gdbarch
);
498 case TDESC_TYPE_ENUM
:
499 return make_gdb_type_enum (gdbarch
);
502 internal_error (__FILE__
, __LINE__
,
503 "Type \"%s\" has an unknown kind %d",
504 this->name
.c_str (), this->kind
);
509 std::vector
<tdesc_type_field
> fields
;
513 /* A feature from a target description. Each feature is a collection
514 of other elements, e.g. registers and types. */
516 struct tdesc_feature
: tdesc_element
518 tdesc_feature (const std::string
&name_
)
522 virtual ~tdesc_feature () = default;
524 DISABLE_COPY_AND_ASSIGN (tdesc_feature
);
526 /* The name of this feature. It may be recognized by the architecture
530 /* The registers associated with this feature. */
531 std::vector
<tdesc_reg_up
> registers
;
533 /* The types associated with this feature. */
534 std::vector
<tdesc_type_up
> types
;
536 void accept (tdesc_element_visitor
&v
) const override
540 for (const tdesc_type_up
&type
: types
)
543 for (const tdesc_reg_up
®
: registers
)
549 bool operator== (const tdesc_feature
&other
) const
551 if (name
!= other
.name
)
554 if (registers
.size () != other
.registers
.size ())
557 for (int ix
= 0; ix
< registers
.size (); ix
++)
559 const tdesc_reg_up
®1
= registers
[ix
];
560 const tdesc_reg_up
®2
= other
.registers
[ix
];
562 if (reg1
!= reg2
&& *reg1
!= *reg2
)
566 if (types
.size () != other
.types
.size ())
569 for (int ix
= 0; ix
< types
.size (); ix
++)
571 const tdesc_type_up
&type1
= types
[ix
];
572 const tdesc_type_up
&type2
= other
.types
[ix
];
574 if (type1
!= type2
&& *type1
!= *type2
)
581 bool operator!= (const tdesc_feature
&other
) const
583 return !(*this == other
);
587 typedef std::unique_ptr
<tdesc_feature
> tdesc_feature_up
;
589 /* A target description. */
591 struct target_desc
: tdesc_element
596 virtual ~target_desc () = default;
598 target_desc (const target_desc
&) = delete;
599 void operator= (const target_desc
&) = delete;
601 /* The architecture reported by the target, if any. */
602 const struct bfd_arch_info
*arch
= NULL
;
604 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
606 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
608 /* The list of compatible architectures reported by the target. */
609 std::vector
<const bfd_arch_info
*> compatible
;
611 /* Any architecture-specific properties specified by the target. */
612 std::vector
<property
> properties
;
614 /* The features associated with this target. */
615 std::vector
<tdesc_feature_up
> features
;
617 void accept (tdesc_element_visitor
&v
) const override
621 for (const tdesc_feature_up
&feature
: features
)
627 bool operator== (const target_desc
&other
) const
629 if (arch
!= other
.arch
)
632 if (osabi
!= other
.osabi
)
635 if (features
.size () != other
.features
.size ())
638 for (int ix
= 0; ix
< features
.size (); ix
++)
640 const tdesc_feature_up
&feature1
= features
[ix
];
641 const tdesc_feature_up
&feature2
= other
.features
[ix
];
643 if (feature1
!= feature2
&& *feature1
!= *feature2
)
650 bool operator!= (const target_desc
&other
) const
652 return !(*this == other
);
656 /* Per-architecture data associated with a target description. The
657 target description may be shared by multiple architectures, but
658 this data is private to one gdbarch. */
660 struct tdesc_arch_reg
662 tdesc_arch_reg (tdesc_reg
*reg_
, struct type
*type_
)
663 : reg (reg_
), type (type_
)
666 struct tdesc_reg
*reg
;
670 struct tdesc_arch_data
672 /* A list of register/type pairs, indexed by GDB's internal register number.
673 During initialization of the gdbarch this list is used to store
674 registers which the architecture assigns a fixed register number.
675 Registers which are NULL in this array, or off the end, are
676 treated as zero-sized and nameless (i.e. placeholders in the
678 std::vector
<tdesc_arch_reg
> arch_regs
;
680 /* Functions which report the register name, type, and reggroups for
682 gdbarch_register_name_ftype
*pseudo_register_name
= NULL
;
683 gdbarch_register_type_ftype
*pseudo_register_type
= NULL
;
684 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
= NULL
;
687 /* Info about an inferior's target description. There's one of these
688 for each inferior. */
690 struct target_desc_info
692 /* A flag indicating that a description has already been fetched
693 from the target, so it should not be queried again. */
697 /* The description fetched from the target, or NULL if the target
698 did not supply any description. Only valid when
699 target_desc_fetched is set. Only the description initialization
700 code should access this; normally, the description should be
701 accessed through the gdbarch object. */
703 const struct target_desc
*tdesc
;
705 /* The filename to read a target description from, as set by "set
706 tdesc filename ..." */
711 /* Get the inferior INF's target description info, allocating one on
712 the stop if necessary. */
714 static struct target_desc_info
*
715 get_tdesc_info (struct inferior
*inf
)
717 if (inf
->tdesc_info
== NULL
)
718 inf
->tdesc_info
= XCNEW (struct target_desc_info
);
719 return inf
->tdesc_info
;
722 /* A handle for architecture-specific data associated with the
723 target description (see struct tdesc_arch_data). */
725 static struct gdbarch_data
*tdesc_data
;
727 /* See target-descriptions.h. */
730 target_desc_info_from_user_p (struct target_desc_info
*info
)
732 return info
!= NULL
&& info
->filename
!= NULL
;
735 /* See target-descriptions.h. */
738 copy_inferior_target_desc_info (struct inferior
*destinf
, struct inferior
*srcinf
)
740 struct target_desc_info
*src
= get_tdesc_info (srcinf
);
741 struct target_desc_info
*dest
= get_tdesc_info (destinf
);
743 dest
->fetched
= src
->fetched
;
744 dest
->tdesc
= src
->tdesc
;
745 dest
->filename
= src
->filename
!= NULL
? xstrdup (src
->filename
) : NULL
;
748 /* See target-descriptions.h. */
751 target_desc_info_free (struct target_desc_info
*tdesc_info
)
753 if (tdesc_info
!= NULL
)
755 xfree (tdesc_info
->filename
);
760 /* Convenience helper macros. */
762 #define target_desc_fetched \
763 get_tdesc_info (current_inferior ())->fetched
764 #define current_target_desc \
765 get_tdesc_info (current_inferior ())->tdesc
766 #define target_description_filename \
767 get_tdesc_info (current_inferior ())->filename
769 /* The string manipulated by the "set tdesc filename ..." command. */
771 static char *tdesc_filename_cmd_string
;
773 /* Fetch the current target's description, and switch the current
774 architecture to one which incorporates that description. */
777 target_find_description (void)
779 /* If we've already fetched a description from the target, don't do
780 it again. This allows a target to fetch the description early,
781 during its to_open or to_create_inferior, if it needs extra
782 information about the target to initialize. */
783 if (target_desc_fetched
)
786 /* The current architecture should not have any target description
787 specified. It should have been cleared, e.g. when we
788 disconnected from the previous target. */
789 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL
);
791 /* First try to fetch an XML description from the user-specified
793 current_target_desc
= NULL
;
794 if (target_description_filename
!= NULL
795 && *target_description_filename
!= '\0')
797 = file_read_description_xml (target_description_filename
);
799 /* Next try to read the description from the current target using
801 if (current_target_desc
== NULL
)
802 current_target_desc
= target_read_description_xml (¤t_target
);
804 /* If that failed try a target-specific hook. */
805 if (current_target_desc
== NULL
)
806 current_target_desc
= target_read_description (¤t_target
);
808 /* If a non-NULL description was returned, then update the current
810 if (current_target_desc
)
812 struct gdbarch_info info
;
814 gdbarch_info_init (&info
);
815 info
.target_desc
= current_target_desc
;
816 if (!gdbarch_update_p (info
))
817 warning (_("Architecture rejected target-supplied description"));
820 struct tdesc_arch_data
*data
;
822 data
= ((struct tdesc_arch_data
*)
823 gdbarch_data (target_gdbarch (), tdesc_data
));
824 if (tdesc_has_registers (current_target_desc
)
825 && data
->arch_regs
.empty ())
826 warning (_("Target-supplied registers are not supported "
827 "by the current architecture"));
831 /* Now that we know this description is usable, record that we
833 target_desc_fetched
= 1;
836 /* Discard any description fetched from the current target, and switch
837 the current architecture to one with no target description. */
840 target_clear_description (void)
842 struct gdbarch_info info
;
844 if (!target_desc_fetched
)
847 target_desc_fetched
= 0;
848 current_target_desc
= NULL
;
850 gdbarch_info_init (&info
);
851 if (!gdbarch_update_p (info
))
852 internal_error (__FILE__
, __LINE__
,
853 _("Could not remove target-supplied description"));
856 /* Return the global current target description. This should only be
857 used by gdbarch initialization code; most access should be through
858 an existing gdbarch. */
860 const struct target_desc
*
861 target_current_description (void)
863 if (target_desc_fetched
)
864 return current_target_desc
;
869 /* Return non-zero if this target description is compatible
870 with the given BFD architecture. */
873 tdesc_compatible_p (const struct target_desc
*target_desc
,
874 const struct bfd_arch_info
*arch
)
876 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
879 || arch
->compatible (arch
, compat
)
880 || compat
->compatible (compat
, arch
))
888 /* Direct accessors for target descriptions. */
890 /* Return the string value of a property named KEY, or NULL if the
891 property was not specified. */
894 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
896 for (const property
&prop
: target_desc
->properties
)
898 return prop
.value
.c_str ();
903 /* Return the BFD architecture associated with this target
904 description, or NULL if no architecture was specified. */
906 const struct bfd_arch_info
*
907 tdesc_architecture (const struct target_desc
*target_desc
)
909 return target_desc
->arch
;
912 /* Return the OSABI associated with this target description, or
913 GDB_OSABI_UNKNOWN if no osabi was specified. */
916 tdesc_osabi (const struct target_desc
*target_desc
)
918 return target_desc
->osabi
;
923 /* Return 1 if this target description includes any registers. */
926 tdesc_has_registers (const struct target_desc
*target_desc
)
928 if (target_desc
== NULL
)
931 for (const tdesc_feature_up
&feature
: target_desc
->features
)
932 if (!feature
->registers
.empty ())
938 /* Return the feature with the given name, if present, or NULL if
939 the named feature is not found. */
941 const struct tdesc_feature
*
942 tdesc_find_feature (const struct target_desc
*target_desc
,
945 for (const tdesc_feature_up
&feature
: target_desc
->features
)
946 if (feature
->name
== name
)
947 return feature
.get ();
952 /* Return the name of FEATURE. */
955 tdesc_feature_name (const struct tdesc_feature
*feature
)
957 return feature
->name
.c_str ();
960 /* Predefined types. */
961 static tdesc_type_builtin tdesc_predefined_types
[] =
963 { "bool", TDESC_TYPE_BOOL
},
964 { "int8", TDESC_TYPE_INT8
},
965 { "int16", TDESC_TYPE_INT16
},
966 { "int32", TDESC_TYPE_INT32
},
967 { "int64", TDESC_TYPE_INT64
},
968 { "int128", TDESC_TYPE_INT128
},
969 { "uint8", TDESC_TYPE_UINT8
},
970 { "uint16", TDESC_TYPE_UINT16
},
971 { "uint32", TDESC_TYPE_UINT32
},
972 { "uint64", TDESC_TYPE_UINT64
},
973 { "uint128", TDESC_TYPE_UINT128
},
974 { "code_ptr", TDESC_TYPE_CODE_PTR
},
975 { "data_ptr", TDESC_TYPE_DATA_PTR
},
976 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
977 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
978 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
},
979 { "i387_ext", TDESC_TYPE_I387_EXT
}
982 /* Lookup a predefined type. */
984 static struct tdesc_type
*
985 tdesc_predefined_type (enum tdesc_type_kind kind
)
987 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
988 if (tdesc_predefined_types
[ix
].kind
== kind
)
989 return &tdesc_predefined_types
[ix
];
991 gdb_assert_not_reached ("bad predefined tdesc type");
994 /* See arch/tdesc.h. */
997 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
999 /* First try target-defined types. */
1000 for (const tdesc_type_up
&type
: feature
->types
)
1001 if (type
->name
== id
)
1004 /* Next try the predefined types. */
1005 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
1006 if (tdesc_predefined_types
[ix
].name
== id
)
1007 return &tdesc_predefined_types
[ix
];
1012 /* Lookup type associated with ID. */
1015 tdesc_find_type (struct gdbarch
*gdbarch
, const char *id
)
1017 tdesc_arch_data
*data
1018 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1020 for (const tdesc_arch_reg
®
: data
->arch_regs
)
1023 && reg
.reg
->tdesc_type
1025 && reg
.reg
->tdesc_type
->name
== id
)
1032 /* Support for registers from target descriptions. */
1034 /* Construct the per-gdbarch data. */
1037 tdesc_data_init (struct obstack
*obstack
)
1039 struct tdesc_arch_data
*data
;
1041 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
1042 new (data
) tdesc_arch_data ();
1047 /* Similar, but for the temporary copy used during architecture
1050 struct tdesc_arch_data
*
1051 tdesc_data_alloc (void)
1053 return new tdesc_arch_data ();
1056 /* Free something allocated by tdesc_data_alloc, if it is not going
1057 to be used (for instance if it was unsuitable for the
1061 tdesc_data_cleanup (void *data_untyped
)
1063 struct tdesc_arch_data
*data
= (struct tdesc_arch_data
*) data_untyped
;
1068 /* Search FEATURE for a register named NAME. */
1070 static struct tdesc_reg
*
1071 tdesc_find_register_early (const struct tdesc_feature
*feature
,
1074 for (const tdesc_reg_up
®
: feature
->registers
)
1075 if (strcasecmp (reg
->name
.c_str (), name
) == 0)
1081 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1084 tdesc_numbered_register (const struct tdesc_feature
*feature
,
1085 struct tdesc_arch_data
*data
,
1086 int regno
, const char *name
)
1088 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1093 /* Make sure the vector includes a REGNO'th element. */
1094 while (regno
>= data
->arch_regs
.size ())
1095 data
->arch_regs
.emplace_back (nullptr, nullptr);
1097 data
->arch_regs
[regno
] = tdesc_arch_reg (reg
, NULL
);
1102 /* Search FEATURE for a register named NAME, but do not assign a fixed
1103 register number to it. */
1106 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
1109 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1117 /* Search FEATURE for a register whose name is in NAMES and assign
1121 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
1122 struct tdesc_arch_data
*data
,
1123 int regno
, const char *const names
[])
1127 for (i
= 0; names
[i
] != NULL
; i
++)
1128 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
1134 /* Search FEATURE for a register named NAME, and return its size in
1135 bits. The register must exist. */
1138 tdesc_register_size (const struct tdesc_feature
*feature
,
1141 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1143 gdb_assert (reg
!= NULL
);
1144 return reg
->bitsize
;
1147 /* Look up a register by its GDB internal register number. */
1149 static struct tdesc_arch_reg
*
1150 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
1152 struct tdesc_arch_data
*data
;
1154 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1155 if (regno
< data
->arch_regs
.size ())
1156 return &data
->arch_regs
[regno
];
1161 static struct tdesc_reg
*
1162 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
1164 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
1166 return reg
? reg
->reg
: NULL
;
1169 /* Return the name of register REGNO, from the target description or
1170 from an architecture-provided pseudo_register_name method. */
1173 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
1175 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1176 int num_regs
= gdbarch_num_regs (gdbarch
);
1177 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1180 return reg
->name
.c_str ();
1182 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1184 struct tdesc_arch_data
*data
1185 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1187 gdb_assert (data
->pseudo_register_name
!= NULL
);
1188 return data
->pseudo_register_name (gdbarch
, regno
);
1195 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
1197 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
1198 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
1199 int num_regs
= gdbarch_num_regs (gdbarch
);
1200 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1202 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1204 struct tdesc_arch_data
*data
1205 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1207 gdb_assert (data
->pseudo_register_type
!= NULL
);
1208 return data
->pseudo_register_type (gdbarch
, regno
);
1212 /* Return "int0_t", since "void" has a misleading size of one. */
1213 return builtin_type (gdbarch
)->builtin_int0
;
1215 if (arch_reg
->type
== NULL
)
1217 /* First check for a predefined or target defined type. */
1218 if (reg
->tdesc_type
)
1219 arch_reg
->type
= reg
->tdesc_type
->make_gdb_type (gdbarch
);
1221 /* Next try size-sensitive type shortcuts. */
1222 else if (reg
->type
== "float")
1224 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
1225 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
1226 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
1227 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1228 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
1229 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
1232 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1233 reg
->name
.c_str (), reg
->bitsize
);
1234 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1237 else if (reg
->type
== "int")
1239 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
1240 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1241 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
1242 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
1243 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
1244 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
1245 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
1246 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
1247 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
1248 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
1249 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
1250 /* A bit desperate by this point... */
1251 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
1254 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1255 reg
->name
.c_str (), reg
->bitsize
);
1256 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1260 if (arch_reg
->type
== NULL
)
1261 internal_error (__FILE__
, __LINE__
,
1262 "Register \"%s\" has an unknown type \"%s\"",
1263 reg
->name
.c_str (), reg
->type
.c_str ());
1266 return arch_reg
->type
;
1270 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
1272 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1275 return reg
->target_regnum
;
1280 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1281 target description may be classified as general, float, vector or other
1282 register groups registered with reggroup_add(). Unlike a gdbarch
1283 register_reggroup_p method, this function will return -1 if it does not
1284 know; the caller should handle registers with no specified group.
1286 The names of containing features are not used. This might be extended
1287 to display registers in some more useful groupings.
1289 The save-restore flag is also implemented here. */
1292 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1293 struct reggroup
*reggroup
)
1295 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1297 if (reg
!= NULL
&& !reg
->group
.empty ()
1298 && (reg
->group
== reggroup_name (reggroup
)))
1302 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
1303 return reg
->save_restore
;
1308 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1309 group specified go to the default reggroup function and are handled
1313 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1314 struct reggroup
*reggroup
)
1316 int num_regs
= gdbarch_num_regs (gdbarch
);
1317 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1320 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1322 struct tdesc_arch_data
*data
1323 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1325 if (data
->pseudo_register_reggroup_p
!= NULL
)
1326 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
1327 /* Otherwise fall through to the default reggroup_p. */
1330 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
1334 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
1337 /* Record architecture-specific functions to call for pseudo-register
1341 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
1342 gdbarch_register_name_ftype
*pseudo_name
)
1344 struct tdesc_arch_data
*data
1345 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1347 data
->pseudo_register_name
= pseudo_name
;
1351 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
1352 gdbarch_register_type_ftype
*pseudo_type
)
1354 struct tdesc_arch_data
*data
1355 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1357 data
->pseudo_register_type
= pseudo_type
;
1361 set_tdesc_pseudo_register_reggroup_p
1362 (struct gdbarch
*gdbarch
,
1363 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
1365 struct tdesc_arch_data
*data
1366 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1368 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
1371 /* Update GDBARCH to use the target description for registers. */
1374 tdesc_use_registers (struct gdbarch
*gdbarch
,
1375 const struct target_desc
*target_desc
,
1376 struct tdesc_arch_data
*early_data
)
1378 int num_regs
= gdbarch_num_regs (gdbarch
);
1379 struct tdesc_arch_data
*data
;
1382 /* We can't use the description for registers if it doesn't describe
1383 any. This function should only be called after validating
1384 registers, so the caller should know that registers are
1386 gdb_assert (tdesc_has_registers (target_desc
));
1388 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1389 data
->arch_regs
= early_data
->arch_regs
;
1392 /* Build up a set of all registers, so that we can assign register
1393 numbers where needed. The hash table expands as necessary, so
1394 the initial size is arbitrary. */
1395 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1396 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1397 for (const tdesc_reg_up
®
: feature
->registers
)
1399 void **slot
= htab_find_slot (reg_hash
, reg
.get (), INSERT
);
1402 /* Add reggroup if its new. */
1403 if (!reg
->group
.empty ())
1404 if (reggroup_find (gdbarch
, reg
->group
.c_str ()) == NULL
)
1405 reggroup_add (gdbarch
, reggroup_gdbarch_new (gdbarch
,
1406 reg
->group
.c_str (),
1410 /* Remove any registers which were assigned numbers by the
1412 for (const tdesc_arch_reg
&arch_reg
: data
->arch_regs
)
1413 if (arch_reg
.reg
!= NULL
)
1414 htab_remove_elt (reg_hash
, arch_reg
.reg
);
1416 /* Assign numbers to the remaining registers and add them to the
1417 list of registers. The new numbers are always above gdbarch_num_regs.
1418 Iterate over the features, not the hash table, so that the order
1419 matches that in the target description. */
1421 gdb_assert (data
->arch_regs
.size () <= num_regs
);
1422 while (data
->arch_regs
.size () < num_regs
)
1423 data
->arch_regs
.emplace_back (nullptr, nullptr);
1425 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1426 for (const tdesc_reg_up
®
: feature
->registers
)
1427 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1429 data
->arch_regs
.emplace_back (reg
.get (), nullptr);
1433 htab_delete (reg_hash
);
1435 /* Update the architecture. */
1436 set_gdbarch_num_regs (gdbarch
, num_regs
);
1437 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1438 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1439 set_gdbarch_remote_register_number (gdbarch
,
1440 tdesc_remote_register_number
);
1441 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1445 /* See arch/tdesc.h. */
1448 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
1449 int regnum
, int save_restore
, const char *group
,
1450 int bitsize
, const char *type
)
1452 tdesc_reg
*reg
= new tdesc_reg (feature
, name
, regnum
, save_restore
,
1453 group
, bitsize
, type
);
1455 feature
->registers
.emplace_back (reg
);
1458 /* See arch/tdesc.h. */
1461 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1462 struct tdesc_type
*field_type
, int count
)
1464 tdesc_type_vector
*type
= new tdesc_type_vector (name
, field_type
, count
);
1465 feature
->types
.emplace_back (type
);
1470 /* See arch/tdesc.h. */
1472 tdesc_type_with_fields
*
1473 tdesc_create_struct (struct tdesc_feature
*feature
, const char *name
)
1475 tdesc_type_with_fields
*type
1476 = new tdesc_type_with_fields (name
, TDESC_TYPE_STRUCT
);
1477 feature
->types
.emplace_back (type
);
1482 /* See arch/tdesc.h. */
1485 tdesc_set_struct_size (tdesc_type_with_fields
*type
, int size
)
1487 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1488 gdb_assert (size
> 0);
1492 /* See arch/tdesc.h. */
1494 tdesc_type_with_fields
*
1495 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1497 tdesc_type_with_fields
*type
1498 = new tdesc_type_with_fields (name
, TDESC_TYPE_UNION
);
1499 feature
->types
.emplace_back (type
);
1504 /* See arch/tdesc.h. */
1506 tdesc_type_with_fields
*
1507 tdesc_create_flags (struct tdesc_feature
*feature
, const char *name
,
1510 gdb_assert (size
> 0);
1512 tdesc_type_with_fields
*type
1513 = new tdesc_type_with_fields (name
, TDESC_TYPE_FLAGS
, size
);
1514 feature
->types
.emplace_back (type
);
1519 tdesc_type_with_fields
*
1520 tdesc_create_enum (struct tdesc_feature
*feature
, const char *name
,
1523 gdb_assert (size
> 0);
1525 tdesc_type_with_fields
*type
1526 = new tdesc_type_with_fields (name
, TDESC_TYPE_ENUM
, size
);
1527 feature
->types
.emplace_back (type
);
1532 /* See arch/tdesc.h. */
1535 tdesc_add_field (tdesc_type_with_fields
*type
, const char *field_name
,
1536 struct tdesc_type
*field_type
)
1538 gdb_assert (type
->kind
== TDESC_TYPE_UNION
1539 || type
->kind
== TDESC_TYPE_STRUCT
);
1541 /* Initialize start and end so we know this is not a bit-field
1542 when we print-c-tdesc. */
1543 type
->fields
.emplace_back (field_name
, field_type
, -1, -1);
1547 tdesc_add_typed_bitfield (tdesc_type_with_fields
*type
, const char *field_name
,
1548 int start
, int end
, struct tdesc_type
*field_type
)
1550 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
1551 || type
->kind
== TDESC_TYPE_FLAGS
);
1552 gdb_assert (start
>= 0 && end
>= start
);
1554 type
->fields
.emplace_back (field_name
, field_type
, start
, end
);
1557 /* See arch/tdesc.h. */
1560 tdesc_add_bitfield (tdesc_type_with_fields
*type
, const char *field_name
,
1563 struct tdesc_type
*field_type
;
1565 gdb_assert (start
>= 0 && end
>= start
);
1568 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT64
);
1570 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT32
);
1572 tdesc_add_typed_bitfield (type
, field_name
, start
, end
, field_type
);
1575 /* See arch/tdesc.h. */
1578 tdesc_add_flag (tdesc_type_with_fields
*type
, int start
,
1579 const char *flag_name
)
1581 gdb_assert (type
->kind
== TDESC_TYPE_FLAGS
1582 || type
->kind
== TDESC_TYPE_STRUCT
);
1584 type
->fields
.emplace_back (flag_name
,
1585 tdesc_predefined_type (TDESC_TYPE_BOOL
),
1590 tdesc_add_enum_value (tdesc_type_with_fields
*type
, int value
,
1593 gdb_assert (type
->kind
== TDESC_TYPE_ENUM
);
1594 type
->fields
.emplace_back (name
,
1595 tdesc_predefined_type (TDESC_TYPE_INT32
),
1599 /* See arch/tdesc.h. */
1601 struct tdesc_feature
*
1602 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
,
1605 struct tdesc_feature
*new_feature
= new tdesc_feature (name
);
1607 tdesc
->features
.emplace_back (new_feature
);
1612 struct target_desc
*
1613 allocate_target_description (void)
1615 return new target_desc ();
1619 free_target_description (void *arg
)
1621 struct target_desc
*target_desc
= (struct target_desc
*) arg
;
1627 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1629 return make_cleanup (free_target_description
, target_desc
);
1633 tdesc_add_compatible (struct target_desc
*target_desc
,
1634 const struct bfd_arch_info
*compatible
)
1636 /* If this instance of GDB is compiled without BFD support for the
1637 compatible architecture, simply ignore it -- we would not be able
1638 to handle it anyway. */
1639 if (compatible
== NULL
)
1642 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
1643 if (compat
== compatible
)
1644 internal_error (__FILE__
, __LINE__
,
1645 _("Attempted to add duplicate "
1646 "compatible architecture \"%s\""),
1647 compatible
->printable_name
);
1649 target_desc
->compatible
.push_back (compatible
);
1653 set_tdesc_property (struct target_desc
*target_desc
,
1654 const char *key
, const char *value
)
1656 gdb_assert (key
!= NULL
&& value
!= NULL
);
1658 if (tdesc_property (target_desc
, key
) != NULL
)
1659 internal_error (__FILE__
, __LINE__
,
1660 _("Attempted to add duplicate property \"%s\""), key
);
1662 target_desc
->properties
.emplace_back (key
, value
);
1665 /* See arch/tdesc.h. */
1668 set_tdesc_architecture (struct target_desc
*target_desc
,
1671 set_tdesc_architecture (target_desc
, bfd_scan_arch (name
));
1675 set_tdesc_architecture (struct target_desc
*target_desc
,
1676 const struct bfd_arch_info
*arch
)
1678 target_desc
->arch
= arch
;
1681 /* See arch/tdesc.h. */
1684 set_tdesc_osabi (struct target_desc
*target_desc
, const char *name
)
1686 set_tdesc_osabi (target_desc
, osabi_from_tdesc_string (name
));
1690 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1692 target_desc
->osabi
= osabi
;
1696 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1697 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1699 /* Helper functions for the CLI commands. */
1702 set_tdesc_cmd (const char *args
, int from_tty
)
1704 help_list (tdesc_set_cmdlist
, "set tdesc ", all_commands
, gdb_stdout
);
1708 show_tdesc_cmd (const char *args
, int from_tty
)
1710 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1714 unset_tdesc_cmd (const char *args
, int from_tty
)
1716 help_list (tdesc_unset_cmdlist
, "unset tdesc ", all_commands
, gdb_stdout
);
1720 set_tdesc_filename_cmd (const char *args
, int from_tty
,
1721 struct cmd_list_element
*c
)
1723 xfree (target_description_filename
);
1724 target_description_filename
= xstrdup (tdesc_filename_cmd_string
);
1726 target_clear_description ();
1727 target_find_description ();
1731 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1732 struct cmd_list_element
*c
,
1735 value
= target_description_filename
;
1737 if (value
!= NULL
&& *value
!= '\0')
1738 printf_filtered (_("The target description will be read from \"%s\".\n"),
1741 printf_filtered (_("The target description will be "
1742 "read from the target.\n"));
1746 unset_tdesc_filename_cmd (const char *args
, int from_tty
)
1748 xfree (target_description_filename
);
1749 target_description_filename
= NULL
;
1750 target_clear_description ();
1751 target_find_description ();
1754 /* Print target description in C. */
1756 class print_c_tdesc
: public tdesc_element_visitor
1759 print_c_tdesc (std::string
&filename_after_features
)
1760 : m_filename_after_features (filename_after_features
)
1764 const char *filename
= lbasename (m_filename_after_features
.c_str ());
1766 m_function
= (char *) xmalloc (strlen (filename
) + 1);
1767 for (inp
= filename
, outp
= m_function
; *inp
!= '\0'; inp
++)
1770 else if (*inp
== '-')
1776 /* Standard boilerplate. */
1777 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1778 "-*- buffer-read-only: t -*- vi"
1787 void visit_pre (const target_desc
*e
) override
1789 printf_unfiltered (" Original: %s */\n\n",
1790 lbasename (m_filename_after_features
.c_str ()));
1792 printf_unfiltered ("#include \"defs.h\"\n");
1793 printf_unfiltered ("#include \"osabi.h\"\n");
1794 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1795 printf_unfiltered ("\n");
1797 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function
);
1798 printf_unfiltered ("static void\n");
1799 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function
);
1800 printf_unfiltered ("{\n");
1802 (" struct target_desc *result = allocate_target_description ();\n");
1804 if (tdesc_architecture (e
) != NULL
)
1807 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1808 tdesc_architecture (e
)->printable_name
);
1809 printf_unfiltered ("\n");
1811 if (tdesc_osabi (e
) > GDB_OSABI_UNKNOWN
1812 && tdesc_osabi (e
) < GDB_OSABI_INVALID
)
1815 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1816 gdbarch_osabi_name (tdesc_osabi (e
)));
1817 printf_unfiltered ("\n");
1820 for (const bfd_arch_info_type
*compatible
: e
->compatible
)
1822 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1823 compatible
->printable_name
);
1825 if (!e
->compatible
.empty ())
1826 printf_unfiltered ("\n");
1828 for (const property
&prop
: e
->properties
)
1829 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1830 prop
.key
.c_str (), prop
.value
.c_str ());
1832 printf_unfiltered (" struct tdesc_feature *feature;\n");
1835 void visit_pre (const tdesc_feature
*e
) override
1837 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1841 void visit_post (const tdesc_feature
*e
) override
1844 void visit_post (const target_desc
*e
) override
1846 printf_unfiltered ("\n tdesc_%s = result;\n", m_function
);
1847 printf_unfiltered ("}\n");
1850 void visit (const tdesc_type_builtin
*type
) override
1852 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1855 void visit (const tdesc_type_vector
*type
) override
1857 if (!m_printed_element_type
)
1859 printf_unfiltered (" tdesc_type *element_type;\n");
1860 m_printed_element_type
= true;
1864 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1865 type
->element_type
->name
.c_str ());
1867 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1868 type
->name
.c_str (), type
->count
);
1870 printf_unfiltered ("\n");
1873 void visit (const tdesc_type_with_fields
*type
) override
1875 if (!m_printed_type_with_fields
)
1877 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1878 m_printed_type_with_fields
= true;
1883 case TDESC_TYPE_STRUCT
:
1884 case TDESC_TYPE_FLAGS
:
1885 if (type
->kind
== TDESC_TYPE_STRUCT
)
1888 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1889 type
->name
.c_str ());
1890 if (type
->size
!= 0)
1892 (" tdesc_set_struct_size (type_with_fields, %d);\n", type
->size
);
1897 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1898 type
->name
.c_str (), type
->size
);
1900 for (const tdesc_type_field
&f
: type
->fields
)
1902 const char *type_name
;
1904 gdb_assert (f
.type
!= NULL
);
1905 type_name
= f
.type
->name
.c_str ();
1907 /* To minimize changes to generated files, don't emit type
1908 info for fields that have defaulted types. */
1911 gdb_assert (f
.end
!= -1);
1912 if (f
.type
->kind
== TDESC_TYPE_BOOL
)
1914 gdb_assert (f
.start
== f
.end
);
1916 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1917 f
.start
, f
.name
.c_str ());
1919 else if ((type
->size
== 4 && f
.type
->kind
== TDESC_TYPE_UINT32
)
1921 && f
.type
->kind
== TDESC_TYPE_UINT64
))
1924 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1925 f
.name
.c_str (), f
.start
, f
.end
);
1929 printf_field_type_assignment
1930 ("tdesc_named_type (feature, \"%s\");\n",
1933 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1934 " %d, %d, field_type);\n",
1935 f
.name
.c_str (), f
.start
, f
.end
);
1938 else /* Not a bitfield. */
1940 gdb_assert (f
.end
== -1);
1941 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1942 printf_field_type_assignment
1943 ("tdesc_named_type (feature, \"%s\");\n", type_name
);
1945 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1950 case TDESC_TYPE_UNION
:
1952 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1953 type
->name
.c_str ());
1954 for (const tdesc_type_field
&f
: type
->fields
)
1956 printf_field_type_assignment
1957 ("tdesc_named_type (feature, \"%s\");\n", f
.type
->name
.c_str ());
1959 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1963 case TDESC_TYPE_ENUM
:
1965 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1966 type
->name
.c_str (), type
->size
);
1967 for (const tdesc_type_field
&f
: type
->fields
)
1969 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1970 f
.start
, f
.name
.c_str ());
1973 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1976 printf_unfiltered ("\n");
1979 void visit (const tdesc_reg
*reg
) override
1981 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1982 reg
->name
.c_str (), reg
->target_regnum
,
1984 if (!reg
->group
.empty ())
1985 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
1987 printf_unfiltered ("NULL, ");
1988 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
1992 std::string m_filename_after_features
;
1996 /* Print an assignment to the field_type variable. Print the declaration
1997 of field_type if that has not been done yet. */
1998 ATTRIBUTE_PRINTF (2, 3)
1999 void printf_field_type_assignment (const char *fmt
, ...)
2001 if (!m_printed_field_type
)
2003 printf_unfiltered (" tdesc_type *field_type;\n");
2004 m_printed_field_type
= true;
2007 printf_unfiltered (" field_type = ");
2010 va_start (args
, fmt
);
2011 vprintf_unfiltered (fmt
, args
);
2017 /* Did we print "struct tdesc_type *element_type;" yet? */
2018 bool m_printed_element_type
= false;
2020 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
2021 bool m_printed_type_with_fields
= false;
2023 /* Did we print "struct tdesc_type *field_type;" yet? */
2024 bool m_printed_field_type
= false;
2027 /* Print target description feature in C. */
2029 class print_c_feature
: public print_c_tdesc
2032 print_c_feature (std::string
&file
)
2033 : print_c_tdesc (file
)
2036 auto const pos
= m_filename_after_features
.find_last_of ('.');
2038 m_filename_after_features
= m_filename_after_features
.substr (0, pos
);
2041 void visit_pre (const target_desc
*e
) override
2043 printf_unfiltered (" Original: %s */\n\n",
2044 lbasename (m_filename_after_features
.c_str ()));
2046 printf_unfiltered ("#include \"arch/tdesc.h\"\n");
2047 printf_unfiltered ("\n");
2050 void visit_post (const target_desc
*e
) override
2053 void visit_pre (const tdesc_feature
*e
) override
2055 std::string
name (m_filename_after_features
);
2057 auto pos
= name
.find_first_of ('.');
2059 name
= name
.substr (0, pos
);
2060 std::replace (name
.begin (), name
.end (), '/', '_');
2061 std::replace (name
.begin (), name
.end (), '-', '_');
2063 printf_unfiltered ("static int\n");
2064 printf_unfiltered ("create_feature_%s ", name
.c_str ());
2065 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2067 printf_unfiltered ("{\n");
2068 printf_unfiltered (" struct tdesc_feature *feature;\n");
2071 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
2072 e
->name
.c_str (), lbasename (m_filename_after_features
.c_str ()));
2075 void visit_post (const tdesc_feature
*e
) override
2077 printf_unfiltered (" return regnum;\n");
2078 printf_unfiltered ("}\n");
2081 void visit (const tdesc_reg
*reg
) override
2083 /* Most "reg" in XML target descriptions don't have "regnum"
2084 attribute, so the register number is allocated sequentially.
2085 In case that reg has "regnum" attribute, register number
2086 should be set by that explicitly. */
2088 if (reg
->target_regnum
< m_next_regnum
)
2090 /* The integrity check, it can catch some errors on register
2091 number collision, like this,
2093 <reg name="x0" bitsize="32"/>
2094 <reg name="x1" bitsize="32"/>
2095 <reg name="x2" bitsize="32"/>
2096 <reg name="x3" bitsize="32"/>
2097 <reg name="ps" bitsize="32" regnum="3"/>
2099 but it also has false negatives. The target description
2102 <reg name="x1" bitsize="32" regnum="1"/>
2103 <reg name="x3" bitsize="32" regnum="3"/>
2104 <reg name="x2" bitsize="32" regnum="2"/>
2105 <reg name="x4" bitsize="32" regnum="4"/>
2107 but it is not a good practice, so still error on this,
2108 and also print the message so that it can be saved in the
2109 generated c file. */
2111 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2112 reg
->target_regnum
);
2113 printf_unfiltered ("is not the largest number (%d).\n",
2115 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2116 reg
->target_regnum
, m_next_regnum
);
2119 if (reg
->target_regnum
> m_next_regnum
)
2121 printf_unfiltered (" regnum = %ld;\n", reg
->target_regnum
);
2122 m_next_regnum
= reg
->target_regnum
;
2125 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2126 reg
->name
.c_str (), reg
->save_restore
);
2127 if (!reg
->group
.empty ())
2128 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
2130 printf_unfiltered ("NULL, ");
2131 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
2137 /* The register number to use for the next register we see. */
2138 int m_next_regnum
= 0;
2142 maint_print_c_tdesc_cmd (const char *args
, int from_tty
)
2144 const struct target_desc
*tdesc
;
2145 const char *filename
;
2149 /* Use the global target-supplied description, not the current
2150 architecture's. This lets a GDB for one architecture generate C
2151 for another architecture's description, even though the gdbarch
2152 initialization code will reject the new description. */
2153 tdesc
= current_target_desc
;
2154 filename
= target_description_filename
;
2158 /* Use the target description from the XML file. */
2160 tdesc
= file_read_description_xml (filename
);
2164 error (_("There is no target description to print."));
2166 if (filename
== NULL
)
2167 error (_("The current target description did not come from an XML file."));
2169 std::string
filename_after_features (filename
);
2170 auto loc
= filename_after_features
.rfind ("/features/");
2172 if (loc
!= std::string::npos
)
2173 filename_after_features
= filename_after_features
.substr (loc
+ 10);
2175 /* Print c files for target features instead of target descriptions,
2176 because c files got from target features are more flexible than the
2178 if (startswith (filename_after_features
.c_str (), "i386/32bit-")
2179 || startswith (filename_after_features
.c_str (), "i386/64bit-")
2180 || startswith (filename_after_features
.c_str (), "i386/x32-core.xml")
2181 || startswith (filename_after_features
.c_str (), "tic6x-")
2182 || startswith (filename_after_features
.c_str (), "aarch64"))
2184 print_c_feature
v (filename_after_features
);
2190 print_c_tdesc
v (filename_after_features
);
2196 namespace selftests
{
2198 static std::vector
<std::pair
<const char*, const target_desc
*>> xml_tdesc
;
2202 /* See target-descritpions.h. */
2205 record_xml_tdesc (const char *xml_file
, const struct target_desc
*tdesc
)
2207 xml_tdesc
.emplace_back (xml_file
, tdesc
);
2213 /* Check that the target descriptions created dynamically by
2214 architecture-specific code equal the descriptions created from XML files
2215 found in the specified directory DIR. */
2218 maintenance_check_xml_descriptions (const char *dir
, int from_tty
)
2221 error (_("Missing dir name"));
2223 gdb::unique_xmalloc_ptr
<char> dir1 (tilde_expand (dir
));
2224 std::string
feature_dir (dir1
.get ());
2225 unsigned int failed
= 0;
2227 for (auto const &e
: selftests::xml_tdesc
)
2229 std::string tdesc_xml
= (feature_dir
+ SLASH_STRING
+ e
.first
);
2230 const target_desc
*tdesc
2231 = file_read_description_xml (tdesc_xml
.data ());
2233 if (tdesc
== NULL
|| *tdesc
!= *e
.second
)
2236 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2237 (long) selftests::xml_tdesc
.size (), failed
);
2241 _initialize_target_descriptions (void)
2243 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
2245 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
2246 Set target description specific variables."),
2247 &tdesc_set_cmdlist
, "set tdesc ",
2248 0 /* allow-unknown */, &setlist
);
2249 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
2250 Show target description specific variables."),
2251 &tdesc_show_cmdlist
, "show tdesc ",
2252 0 /* allow-unknown */, &showlist
);
2253 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
2254 Unset target description specific variables."),
2255 &tdesc_unset_cmdlist
, "unset tdesc ",
2256 0 /* allow-unknown */, &unsetlist
);
2258 add_setshow_filename_cmd ("filename", class_obscure
,
2259 &tdesc_filename_cmd_string
,
2261 Set the file to read for an XML target description"), _("\
2262 Show the file to read for an XML target description"), _("\
2263 When set, GDB will read the target description from a local\n\
2264 file instead of querying the remote target."),
2265 set_tdesc_filename_cmd
,
2266 show_tdesc_filename_cmd
,
2267 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
2269 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
2270 Unset the file to read for an XML target description. When unset,\n\
2271 GDB will read the description from the target."),
2272 &tdesc_unset_cmdlist
);
2274 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
2275 Print the current target description as a C source file."),
2276 &maintenanceprintlist
);
2278 cmd_list_element
*cmd
;
2280 cmd
= add_cmd ("xml-descriptions", class_maintenance
,
2281 maintenance_check_xml_descriptions
, _("\
2282 Check the target descriptions created in GDB equal the descriptions\n\
2283 created from XML files in the directory.\n\
2284 The parameter is the directory name."),
2285 &maintenancechecklist
);
2286 set_cmd_completer (cmd
, filename_completer
);