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 */
45 property (const std::string
&key_
, const std::string
&value_
)
46 : key (key_
), value (value_
)
53 /* A named type from a target description. */
55 struct tdesc_type_field
57 tdesc_type_field (const std::string
&name_
, tdesc_type
*type_
,
59 : name (name_
), type (type_
), start (start_
), end (end_
)
63 struct tdesc_type
*type
;
64 /* For non-enum-values, either both are -1 (non-bitfield), or both are
65 not -1 (bitfield). For enum values, start is the value (which could be
72 /* Predefined types. */
86 TDESC_TYPE_IEEE_SINGLE
,
87 TDESC_TYPE_IEEE_DOUBLE
,
88 TDESC_TYPE_ARM_FPA_EXT
,
91 /* Types defined by a target feature. */
99 struct tdesc_type
: tdesc_element
101 tdesc_type (const std::string
&name_
, enum tdesc_type_kind kind_
)
102 : name (name_
), kind (kind_
)
105 virtual ~tdesc_type () = default;
107 DISABLE_COPY_AND_ASSIGN (tdesc_type
);
109 /* The name of this type. */
112 /* Identify the kind of this type. */
113 enum tdesc_type_kind kind
;
115 bool operator== (const tdesc_type
&other
) const
117 return name
== other
.name
&& kind
== other
.kind
;
120 bool operator!= (const tdesc_type
&other
) const
122 return !(*this == other
);
126 typedef std::unique_ptr
<tdesc_type
> tdesc_type_up
;
128 struct tdesc_type_builtin
: tdesc_type
130 tdesc_type_builtin (const std::string
&name
, enum tdesc_type_kind kind
)
131 : tdesc_type (name
, kind
)
134 void accept (tdesc_element_visitor
&v
) const override
140 /* tdesc_type for vector types. */
142 struct tdesc_type_vector
: tdesc_type
144 tdesc_type_vector (const std::string
&name
, tdesc_type
*element_type_
, int count_
)
145 : tdesc_type (name
, TDESC_TYPE_VECTOR
),
146 element_type (element_type_
), count (count_
)
149 void accept (tdesc_element_visitor
&v
) const override
154 struct tdesc_type
*element_type
;
158 /* tdesc_type for struct, union, flags, and enum types. */
160 struct tdesc_type_with_fields
: tdesc_type
162 tdesc_type_with_fields (const std::string
&name
, tdesc_type_kind kind
,
164 : tdesc_type (name
, kind
), size (size_
)
167 void accept (tdesc_element_visitor
&v
) const override
172 std::vector
<tdesc_type_field
> fields
;
176 /* Convert a tdesc_type to a gdb type. */
179 make_gdb_type (struct gdbarch
*gdbarch
, struct tdesc_type
*ttype
)
181 class gdb_type_creator
: public tdesc_element_visitor
184 gdb_type_creator (struct gdbarch
*gdbarch
)
185 : m_gdbarch (gdbarch
)
193 void visit (const tdesc_type_builtin
*e
) override
197 /* Predefined types. */
198 case TDESC_TYPE_BOOL
:
199 m_type
= builtin_type (m_gdbarch
)->builtin_bool
;
201 case TDESC_TYPE_INT8
:
202 m_type
= builtin_type (m_gdbarch
)->builtin_int8
;
204 case TDESC_TYPE_INT16
:
205 m_type
= builtin_type (m_gdbarch
)->builtin_int16
;
207 case TDESC_TYPE_INT32
:
208 m_type
= builtin_type (m_gdbarch
)->builtin_int32
;
210 case TDESC_TYPE_INT64
:
211 m_type
= builtin_type (m_gdbarch
)->builtin_int64
;
213 case TDESC_TYPE_INT128
:
214 m_type
= builtin_type (m_gdbarch
)->builtin_int128
;
216 case TDESC_TYPE_UINT8
:
217 m_type
= builtin_type (m_gdbarch
)->builtin_uint8
;
219 case TDESC_TYPE_UINT16
:
220 m_type
= builtin_type (m_gdbarch
)->builtin_uint16
;
222 case TDESC_TYPE_UINT32
:
223 m_type
= builtin_type (m_gdbarch
)->builtin_uint32
;
225 case TDESC_TYPE_UINT64
:
226 m_type
= builtin_type (m_gdbarch
)->builtin_uint64
;
228 case TDESC_TYPE_UINT128
:
229 m_type
= builtin_type (m_gdbarch
)->builtin_uint128
;
231 case TDESC_TYPE_CODE_PTR
:
232 m_type
= builtin_type (m_gdbarch
)->builtin_func_ptr
;
234 case TDESC_TYPE_DATA_PTR
:
235 m_type
= builtin_type (m_gdbarch
)->builtin_data_ptr
;
239 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
245 case TDESC_TYPE_IEEE_SINGLE
:
246 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_single",
247 floatformats_ieee_single
);
250 case TDESC_TYPE_IEEE_DOUBLE
:
251 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_ieee_double",
252 floatformats_ieee_double
);
254 case TDESC_TYPE_ARM_FPA_EXT
:
255 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_arm_ext",
256 floatformats_arm_ext
);
259 case TDESC_TYPE_I387_EXT
:
260 m_type
= arch_float_type (m_gdbarch
, -1, "builtin_type_i387_ext",
261 floatformats_i387_ext
);
265 internal_error (__FILE__
, __LINE__
,
266 "Type \"%s\" has an unknown kind %d",
267 e
->name
.c_str (), e
->kind
);
270 void visit (const tdesc_type_vector
*e
) override
272 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
276 type
*element_gdb_type
= make_gdb_type (m_gdbarch
, e
->element_type
);
277 m_type
= init_vector_type (element_gdb_type
, e
->count
);
278 TYPE_NAME (m_type
) = xstrdup (e
->name
.c_str ());
282 void visit (const tdesc_type_with_fields
*e
) override
284 m_type
= tdesc_find_type (m_gdbarch
, e
->name
.c_str ());
290 case TDESC_TYPE_STRUCT
:
291 make_gdb_type_struct (e
);
293 case TDESC_TYPE_UNION
:
294 make_gdb_type_union (e
);
296 case TDESC_TYPE_FLAGS
:
297 make_gdb_type_flags (e
);
299 case TDESC_TYPE_ENUM
:
300 make_gdb_type_enum (e
);
304 internal_error (__FILE__
, __LINE__
,
305 "Type \"%s\" has an unknown kind %d",
306 e
->name
.c_str (), e
->kind
);
311 void make_gdb_type_struct (const tdesc_type_with_fields
*e
)
313 m_type
= arch_composite_type (m_gdbarch
, NULL
, TYPE_CODE_STRUCT
);
314 TYPE_NAME (m_type
) = xstrdup (e
->name
.c_str ());
315 TYPE_TAG_NAME (m_type
) = TYPE_NAME (m_type
);
317 for (const tdesc_type_field
&f
: e
->fields
)
319 if (f
.start
!= -1 && f
.end
!= -1)
323 struct type
*field_gdb_type
;
324 int bitsize
, total_size
;
326 /* This invariant should be preserved while creating types. */
327 gdb_assert (e
->size
!= 0);
329 field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
330 else if (e
->size
> 4)
331 field_gdb_type
= builtin_type (m_gdbarch
)->builtin_uint64
;
333 field_gdb_type
= builtin_type (m_gdbarch
)->builtin_uint32
;
335 fld
= append_composite_type_field_raw
336 (m_type
, xstrdup (f
.name
.c_str ()), field_gdb_type
);
338 /* For little-endian, BITPOS counts from the LSB of
339 the structure and marks the LSB of the field. For
340 big-endian, BITPOS counts from the MSB of the
341 structure and marks the MSB of the field. Either
342 way, it is the number of bits to the "left" of the
343 field. To calculate this in big-endian, we need
344 the total size of the structure. */
345 bitsize
= f
.end
- f
.start
+ 1;
346 total_size
= e
->size
* TARGET_CHAR_BIT
;
347 if (gdbarch_bits_big_endian (m_gdbarch
))
348 SET_FIELD_BITPOS (fld
[0], total_size
- f
.start
- bitsize
);
350 SET_FIELD_BITPOS (fld
[0], f
.start
);
351 FIELD_BITSIZE (fld
[0]) = bitsize
;
355 gdb_assert (f
.start
== -1 && f
.end
== -1);
356 type
*field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
357 append_composite_type_field (m_type
,
358 xstrdup (f
.name
.c_str ()),
364 TYPE_LENGTH (m_type
) = e
->size
;
367 void make_gdb_type_union (const tdesc_type_with_fields
*e
)
369 m_type
= arch_composite_type (m_gdbarch
, NULL
, TYPE_CODE_UNION
);
370 TYPE_NAME (m_type
) = xstrdup (e
->name
.c_str ());
372 for (const tdesc_type_field
&f
: e
->fields
)
374 type
* field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
375 append_composite_type_field (m_type
, xstrdup (f
.name
.c_str ()),
378 /* If any of the children of a union are vectors, flag the
379 union as a vector also. This allows e.g. a union of two
380 vector types to show up automatically in "info vector". */
381 if (TYPE_VECTOR (field_gdb_type
))
382 TYPE_VECTOR (m_type
) = 1;
386 void make_gdb_type_flags (const tdesc_type_with_fields
*e
)
388 m_type
= arch_flags_type (m_gdbarch
, e
->name
.c_str (),
389 e
->size
* TARGET_CHAR_BIT
);
391 for (const tdesc_type_field
&f
: e
->fields
)
393 int bitsize
= f
.end
- f
.start
+ 1;
395 gdb_assert (f
.type
!= NULL
);
396 type
*field_gdb_type
= make_gdb_type (m_gdbarch
, f
.type
);
397 append_flags_type_field (m_type
, f
.start
, bitsize
,
398 field_gdb_type
, f
.name
.c_str ());
402 void make_gdb_type_enum (const tdesc_type_with_fields
*e
)
404 m_type
= arch_type (m_gdbarch
, TYPE_CODE_ENUM
, e
->size
* TARGET_CHAR_BIT
,
407 TYPE_UNSIGNED (m_type
) = 1;
408 for (const tdesc_type_field
&f
: e
->fields
)
411 = append_composite_type_field_raw (m_type
,
412 xstrdup (f
.name
.c_str ()),
415 SET_FIELD_BITPOS (fld
[0], f
.start
);
419 /* The gdbarch used. */
420 struct gdbarch
*m_gdbarch
;
422 /* The type created. */
426 gdb_type_creator
gdb_type (gdbarch
);
427 ttype
->accept (gdb_type
);
428 return gdb_type
.get_type ();
431 /* A feature from a target description. Each feature is a collection
432 of other elements, e.g. registers and types. */
434 struct tdesc_feature
: tdesc_element
436 tdesc_feature (const std::string
&name_
)
440 virtual ~tdesc_feature () = default;
442 DISABLE_COPY_AND_ASSIGN (tdesc_feature
);
444 /* The name of this feature. It may be recognized by the architecture
448 /* The registers associated with this feature. */
449 std::vector
<tdesc_reg_up
> registers
;
451 /* The types associated with this feature. */
452 std::vector
<tdesc_type_up
> types
;
454 void accept (tdesc_element_visitor
&v
) const override
458 for (const tdesc_type_up
&type
: types
)
461 for (const tdesc_reg_up
®
: registers
)
467 bool operator== (const tdesc_feature
&other
) const
469 if (name
!= other
.name
)
472 if (registers
.size () != other
.registers
.size ())
475 for (int ix
= 0; ix
< registers
.size (); ix
++)
477 const tdesc_reg_up
®1
= registers
[ix
];
478 const tdesc_reg_up
®2
= other
.registers
[ix
];
480 if (reg1
!= reg2
&& *reg1
!= *reg2
)
484 if (types
.size () != other
.types
.size ())
487 for (int ix
= 0; ix
< types
.size (); ix
++)
489 const tdesc_type_up
&type1
= types
[ix
];
490 const tdesc_type_up
&type2
= other
.types
[ix
];
492 if (type1
!= type2
&& *type1
!= *type2
)
499 bool operator!= (const tdesc_feature
&other
) const
501 return !(*this == other
);
505 typedef std::unique_ptr
<tdesc_feature
> tdesc_feature_up
;
507 /* A target description. */
509 struct target_desc
: tdesc_element
514 virtual ~target_desc () = default;
516 target_desc (const target_desc
&) = delete;
517 void operator= (const target_desc
&) = delete;
519 /* The architecture reported by the target, if any. */
520 const struct bfd_arch_info
*arch
= NULL
;
522 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
524 enum gdb_osabi osabi
= GDB_OSABI_UNKNOWN
;
526 /* The list of compatible architectures reported by the target. */
527 std::vector
<const bfd_arch_info
*> compatible
;
529 /* Any architecture-specific properties specified by the target. */
530 std::vector
<property
> properties
;
532 /* The features associated with this target. */
533 std::vector
<tdesc_feature_up
> features
;
535 void accept (tdesc_element_visitor
&v
) const override
539 for (const tdesc_feature_up
&feature
: features
)
545 bool operator== (const target_desc
&other
) const
547 if (arch
!= other
.arch
)
550 if (osabi
!= other
.osabi
)
553 if (features
.size () != other
.features
.size ())
556 for (int ix
= 0; ix
< features
.size (); ix
++)
558 const tdesc_feature_up
&feature1
= features
[ix
];
559 const tdesc_feature_up
&feature2
= other
.features
[ix
];
561 if (feature1
!= feature2
&& *feature1
!= *feature2
)
568 bool operator!= (const target_desc
&other
) const
570 return !(*this == other
);
574 /* Per-architecture data associated with a target description. The
575 target description may be shared by multiple architectures, but
576 this data is private to one gdbarch. */
578 struct tdesc_arch_reg
580 tdesc_arch_reg (tdesc_reg
*reg_
, struct type
*type_
)
581 : reg (reg_
), type (type_
)
584 struct tdesc_reg
*reg
;
588 struct tdesc_arch_data
590 /* A list of register/type pairs, indexed by GDB's internal register number.
591 During initialization of the gdbarch this list is used to store
592 registers which the architecture assigns a fixed register number.
593 Registers which are NULL in this array, or off the end, are
594 treated as zero-sized and nameless (i.e. placeholders in the
596 std::vector
<tdesc_arch_reg
> arch_regs
;
598 /* Functions which report the register name, type, and reggroups for
600 gdbarch_register_name_ftype
*pseudo_register_name
= NULL
;
601 gdbarch_register_type_ftype
*pseudo_register_type
= NULL
;
602 gdbarch_register_reggroup_p_ftype
*pseudo_register_reggroup_p
= NULL
;
605 /* Info about an inferior's target description. There's one of these
606 for each inferior. */
608 struct target_desc_info
610 /* A flag indicating that a description has already been fetched
611 from the target, so it should not be queried again. */
615 /* The description fetched from the target, or NULL if the target
616 did not supply any description. Only valid when
617 target_desc_fetched is set. Only the description initialization
618 code should access this; normally, the description should be
619 accessed through the gdbarch object. */
621 const struct target_desc
*tdesc
;
623 /* The filename to read a target description from, as set by "set
624 tdesc filename ..." */
629 /* Get the inferior INF's target description info, allocating one on
630 the stop if necessary. */
632 static struct target_desc_info
*
633 get_tdesc_info (struct inferior
*inf
)
635 if (inf
->tdesc_info
== NULL
)
636 inf
->tdesc_info
= XCNEW (struct target_desc_info
);
637 return inf
->tdesc_info
;
640 /* A handle for architecture-specific data associated with the
641 target description (see struct tdesc_arch_data). */
643 static struct gdbarch_data
*tdesc_data
;
645 /* See target-descriptions.h. */
648 target_desc_info_from_user_p (struct target_desc_info
*info
)
650 return info
!= NULL
&& info
->filename
!= NULL
;
653 /* See target-descriptions.h. */
656 copy_inferior_target_desc_info (struct inferior
*destinf
, struct inferior
*srcinf
)
658 struct target_desc_info
*src
= get_tdesc_info (srcinf
);
659 struct target_desc_info
*dest
= get_tdesc_info (destinf
);
661 dest
->fetched
= src
->fetched
;
662 dest
->tdesc
= src
->tdesc
;
663 dest
->filename
= src
->filename
!= NULL
? xstrdup (src
->filename
) : NULL
;
666 /* See target-descriptions.h. */
669 target_desc_info_free (struct target_desc_info
*tdesc_info
)
671 if (tdesc_info
!= NULL
)
673 xfree (tdesc_info
->filename
);
678 /* Convenience helper macros. */
680 #define target_desc_fetched \
681 get_tdesc_info (current_inferior ())->fetched
682 #define current_target_desc \
683 get_tdesc_info (current_inferior ())->tdesc
684 #define target_description_filename \
685 get_tdesc_info (current_inferior ())->filename
687 /* The string manipulated by the "set tdesc filename ..." command. */
689 static char *tdesc_filename_cmd_string
;
691 /* Fetch the current target's description, and switch the current
692 architecture to one which incorporates that description. */
695 target_find_description (void)
697 /* If we've already fetched a description from the target, don't do
698 it again. This allows a target to fetch the description early,
699 during its to_open or to_create_inferior, if it needs extra
700 information about the target to initialize. */
701 if (target_desc_fetched
)
704 /* The current architecture should not have any target description
705 specified. It should have been cleared, e.g. when we
706 disconnected from the previous target. */
707 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL
);
709 /* First try to fetch an XML description from the user-specified
711 current_target_desc
= NULL
;
712 if (target_description_filename
!= NULL
713 && *target_description_filename
!= '\0')
715 = file_read_description_xml (target_description_filename
);
717 /* Next try to read the description from the current target using
719 if (current_target_desc
== NULL
)
720 current_target_desc
= target_read_description_xml (¤t_target
);
722 /* If that failed try a target-specific hook. */
723 if (current_target_desc
== NULL
)
724 current_target_desc
= target_read_description (¤t_target
);
726 /* If a non-NULL description was returned, then update the current
728 if (current_target_desc
)
730 struct gdbarch_info info
;
732 gdbarch_info_init (&info
);
733 info
.target_desc
= current_target_desc
;
734 if (!gdbarch_update_p (info
))
735 warning (_("Architecture rejected target-supplied description"));
738 struct tdesc_arch_data
*data
;
740 data
= ((struct tdesc_arch_data
*)
741 gdbarch_data (target_gdbarch (), tdesc_data
));
742 if (tdesc_has_registers (current_target_desc
)
743 && data
->arch_regs
.empty ())
744 warning (_("Target-supplied registers are not supported "
745 "by the current architecture"));
749 /* Now that we know this description is usable, record that we
751 target_desc_fetched
= 1;
754 /* Discard any description fetched from the current target, and switch
755 the current architecture to one with no target description. */
758 target_clear_description (void)
760 struct gdbarch_info info
;
762 if (!target_desc_fetched
)
765 target_desc_fetched
= 0;
766 current_target_desc
= NULL
;
768 gdbarch_info_init (&info
);
769 if (!gdbarch_update_p (info
))
770 internal_error (__FILE__
, __LINE__
,
771 _("Could not remove target-supplied description"));
774 /* Return the global current target description. This should only be
775 used by gdbarch initialization code; most access should be through
776 an existing gdbarch. */
778 const struct target_desc
*
779 target_current_description (void)
781 if (target_desc_fetched
)
782 return current_target_desc
;
787 /* Return non-zero if this target description is compatible
788 with the given BFD architecture. */
791 tdesc_compatible_p (const struct target_desc
*target_desc
,
792 const struct bfd_arch_info
*arch
)
794 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
797 || arch
->compatible (arch
, compat
)
798 || compat
->compatible (compat
, arch
))
806 /* Direct accessors for target descriptions. */
808 /* Return the string value of a property named KEY, or NULL if the
809 property was not specified. */
812 tdesc_property (const struct target_desc
*target_desc
, const char *key
)
814 for (const property
&prop
: target_desc
->properties
)
816 return prop
.value
.c_str ();
821 /* Return the BFD architecture associated with this target
822 description, or NULL if no architecture was specified. */
824 const struct bfd_arch_info
*
825 tdesc_architecture (const struct target_desc
*target_desc
)
827 return target_desc
->arch
;
830 /* Return the OSABI associated with this target description, or
831 GDB_OSABI_UNKNOWN if no osabi was specified. */
834 tdesc_osabi (const struct target_desc
*target_desc
)
836 return target_desc
->osabi
;
841 /* Return 1 if this target description includes any registers. */
844 tdesc_has_registers (const struct target_desc
*target_desc
)
846 if (target_desc
== NULL
)
849 for (const tdesc_feature_up
&feature
: target_desc
->features
)
850 if (!feature
->registers
.empty ())
856 /* Return the feature with the given name, if present, or NULL if
857 the named feature is not found. */
859 const struct tdesc_feature
*
860 tdesc_find_feature (const struct target_desc
*target_desc
,
863 for (const tdesc_feature_up
&feature
: target_desc
->features
)
864 if (feature
->name
== name
)
865 return feature
.get ();
870 /* Return the name of FEATURE. */
873 tdesc_feature_name (const struct tdesc_feature
*feature
)
875 return feature
->name
.c_str ();
878 /* Predefined types. */
879 static tdesc_type_builtin tdesc_predefined_types
[] =
881 { "bool", TDESC_TYPE_BOOL
},
882 { "int8", TDESC_TYPE_INT8
},
883 { "int16", TDESC_TYPE_INT16
},
884 { "int32", TDESC_TYPE_INT32
},
885 { "int64", TDESC_TYPE_INT64
},
886 { "int128", TDESC_TYPE_INT128
},
887 { "uint8", TDESC_TYPE_UINT8
},
888 { "uint16", TDESC_TYPE_UINT16
},
889 { "uint32", TDESC_TYPE_UINT32
},
890 { "uint64", TDESC_TYPE_UINT64
},
891 { "uint128", TDESC_TYPE_UINT128
},
892 { "code_ptr", TDESC_TYPE_CODE_PTR
},
893 { "data_ptr", TDESC_TYPE_DATA_PTR
},
894 { "ieee_single", TDESC_TYPE_IEEE_SINGLE
},
895 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE
},
896 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT
},
897 { "i387_ext", TDESC_TYPE_I387_EXT
}
900 /* Lookup a predefined type. */
902 static struct tdesc_type
*
903 tdesc_predefined_type (enum tdesc_type_kind kind
)
905 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
906 if (tdesc_predefined_types
[ix
].kind
== kind
)
907 return &tdesc_predefined_types
[ix
];
909 gdb_assert_not_reached ("bad predefined tdesc type");
912 /* See common/tdesc.h. */
915 tdesc_named_type (const struct tdesc_feature
*feature
, const char *id
)
917 /* First try target-defined types. */
918 for (const tdesc_type_up
&type
: feature
->types
)
919 if (type
->name
== id
)
922 /* Next try the predefined types. */
923 for (int ix
= 0; ix
< ARRAY_SIZE (tdesc_predefined_types
); ix
++)
924 if (tdesc_predefined_types
[ix
].name
== id
)
925 return &tdesc_predefined_types
[ix
];
930 /* Lookup type associated with ID. */
933 tdesc_find_type (struct gdbarch
*gdbarch
, const char *id
)
935 tdesc_arch_data
*data
936 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
938 for (const tdesc_arch_reg
®
: data
->arch_regs
)
941 && reg
.reg
->tdesc_type
943 && reg
.reg
->tdesc_type
->name
== id
)
950 /* Support for registers from target descriptions. */
952 /* Construct the per-gdbarch data. */
955 tdesc_data_init (struct obstack
*obstack
)
957 struct tdesc_arch_data
*data
;
959 data
= OBSTACK_ZALLOC (obstack
, struct tdesc_arch_data
);
960 new (data
) tdesc_arch_data ();
965 /* Similar, but for the temporary copy used during architecture
968 struct tdesc_arch_data
*
969 tdesc_data_alloc (void)
971 return new tdesc_arch_data ();
974 /* Free something allocated by tdesc_data_alloc, if it is not going
975 to be used (for instance if it was unsuitable for the
979 tdesc_data_cleanup (void *data_untyped
)
981 struct tdesc_arch_data
*data
= (struct tdesc_arch_data
*) data_untyped
;
986 /* Search FEATURE for a register named NAME. */
988 static struct tdesc_reg
*
989 tdesc_find_register_early (const struct tdesc_feature
*feature
,
992 for (const tdesc_reg_up
®
: feature
->registers
)
993 if (strcasecmp (reg
->name
.c_str (), name
) == 0)
999 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
1002 tdesc_numbered_register (const struct tdesc_feature
*feature
,
1003 struct tdesc_arch_data
*data
,
1004 int regno
, const char *name
)
1006 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1011 /* Make sure the vector includes a REGNO'th element. */
1012 while (regno
>= data
->arch_regs
.size ())
1013 data
->arch_regs
.emplace_back (nullptr, nullptr);
1015 data
->arch_regs
[regno
] = tdesc_arch_reg (reg
, NULL
);
1020 /* Search FEATURE for a register named NAME, but do not assign a fixed
1021 register number to it. */
1024 tdesc_unnumbered_register (const struct tdesc_feature
*feature
,
1027 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1035 /* Search FEATURE for a register whose name is in NAMES and assign
1039 tdesc_numbered_register_choices (const struct tdesc_feature
*feature
,
1040 struct tdesc_arch_data
*data
,
1041 int regno
, const char *const names
[])
1045 for (i
= 0; names
[i
] != NULL
; i
++)
1046 if (tdesc_numbered_register (feature
, data
, regno
, names
[i
]))
1052 /* Search FEATURE for a register named NAME, and return its size in
1053 bits. The register must exist. */
1056 tdesc_register_size (const struct tdesc_feature
*feature
,
1059 struct tdesc_reg
*reg
= tdesc_find_register_early (feature
, name
);
1061 gdb_assert (reg
!= NULL
);
1062 return reg
->bitsize
;
1065 /* Look up a register by its GDB internal register number. */
1067 static struct tdesc_arch_reg
*
1068 tdesc_find_arch_register (struct gdbarch
*gdbarch
, int regno
)
1070 struct tdesc_arch_data
*data
;
1072 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1073 if (regno
< data
->arch_regs
.size ())
1074 return &data
->arch_regs
[regno
];
1079 static struct tdesc_reg
*
1080 tdesc_find_register (struct gdbarch
*gdbarch
, int regno
)
1082 struct tdesc_arch_reg
*reg
= tdesc_find_arch_register (gdbarch
, regno
);
1084 return reg
? reg
->reg
: NULL
;
1087 /* Return the name of register REGNO, from the target description or
1088 from an architecture-provided pseudo_register_name method. */
1091 tdesc_register_name (struct gdbarch
*gdbarch
, int regno
)
1093 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1094 int num_regs
= gdbarch_num_regs (gdbarch
);
1095 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1098 return reg
->name
.c_str ();
1100 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1102 struct tdesc_arch_data
*data
1103 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1105 gdb_assert (data
->pseudo_register_name
!= NULL
);
1106 return data
->pseudo_register_name (gdbarch
, regno
);
1113 tdesc_register_type (struct gdbarch
*gdbarch
, int regno
)
1115 struct tdesc_arch_reg
*arch_reg
= tdesc_find_arch_register (gdbarch
, regno
);
1116 struct tdesc_reg
*reg
= arch_reg
? arch_reg
->reg
: NULL
;
1117 int num_regs
= gdbarch_num_regs (gdbarch
);
1118 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1120 if (reg
== NULL
&& regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1122 struct tdesc_arch_data
*data
1123 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1125 gdb_assert (data
->pseudo_register_type
!= NULL
);
1126 return data
->pseudo_register_type (gdbarch
, regno
);
1130 /* Return "int0_t", since "void" has a misleading size of one. */
1131 return builtin_type (gdbarch
)->builtin_int0
;
1133 if (arch_reg
->type
== NULL
)
1135 /* First check for a predefined or target defined type. */
1136 if (reg
->tdesc_type
)
1137 arch_reg
->type
= make_gdb_type (gdbarch
, reg
->tdesc_type
);
1139 /* Next try size-sensitive type shortcuts. */
1140 else if (reg
->type
== "float")
1142 if (reg
->bitsize
== gdbarch_float_bit (gdbarch
))
1143 arch_reg
->type
= builtin_type (gdbarch
)->builtin_float
;
1144 else if (reg
->bitsize
== gdbarch_double_bit (gdbarch
))
1145 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1146 else if (reg
->bitsize
== gdbarch_long_double_bit (gdbarch
))
1147 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_double
;
1150 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1151 reg
->name
.c_str (), reg
->bitsize
);
1152 arch_reg
->type
= builtin_type (gdbarch
)->builtin_double
;
1155 else if (reg
->type
== "int")
1157 if (reg
->bitsize
== gdbarch_long_bit (gdbarch
))
1158 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1159 else if (reg
->bitsize
== TARGET_CHAR_BIT
)
1160 arch_reg
->type
= builtin_type (gdbarch
)->builtin_char
;
1161 else if (reg
->bitsize
== gdbarch_short_bit (gdbarch
))
1162 arch_reg
->type
= builtin_type (gdbarch
)->builtin_short
;
1163 else if (reg
->bitsize
== gdbarch_int_bit (gdbarch
))
1164 arch_reg
->type
= builtin_type (gdbarch
)->builtin_int
;
1165 else if (reg
->bitsize
== gdbarch_long_long_bit (gdbarch
))
1166 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long_long
;
1167 else if (reg
->bitsize
== gdbarch_ptr_bit (gdbarch
))
1168 /* A bit desperate by this point... */
1169 arch_reg
->type
= builtin_type (gdbarch
)->builtin_data_ptr
;
1172 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1173 reg
->name
.c_str (), reg
->bitsize
);
1174 arch_reg
->type
= builtin_type (gdbarch
)->builtin_long
;
1178 if (arch_reg
->type
== NULL
)
1179 internal_error (__FILE__
, __LINE__
,
1180 "Register \"%s\" has an unknown type \"%s\"",
1181 reg
->name
.c_str (), reg
->type
.c_str ());
1184 return arch_reg
->type
;
1188 tdesc_remote_register_number (struct gdbarch
*gdbarch
, int regno
)
1190 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1193 return reg
->target_regnum
;
1198 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1199 target description may be classified as general, float, vector or other
1200 register groups registered with reggroup_add(). Unlike a gdbarch
1201 register_reggroup_p method, this function will return -1 if it does not
1202 know; the caller should handle registers with no specified group.
1204 The names of containing features are not used. This might be extended
1205 to display registers in some more useful groupings.
1207 The save-restore flag is also implemented here. */
1210 tdesc_register_in_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1211 struct reggroup
*reggroup
)
1213 struct tdesc_reg
*reg
= tdesc_find_register (gdbarch
, regno
);
1215 if (reg
!= NULL
&& !reg
->group
.empty ()
1216 && (reg
->group
== reggroup_name (reggroup
)))
1220 && (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
))
1221 return reg
->save_restore
;
1226 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1227 group specified go to the default reggroup function and are handled
1231 tdesc_register_reggroup_p (struct gdbarch
*gdbarch
, int regno
,
1232 struct reggroup
*reggroup
)
1234 int num_regs
= gdbarch_num_regs (gdbarch
);
1235 int num_pseudo_regs
= gdbarch_num_pseudo_regs (gdbarch
);
1238 if (regno
>= num_regs
&& regno
< num_regs
+ num_pseudo_regs
)
1240 struct tdesc_arch_data
*data
1241 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1243 if (data
->pseudo_register_reggroup_p
!= NULL
)
1244 return data
->pseudo_register_reggroup_p (gdbarch
, regno
, reggroup
);
1245 /* Otherwise fall through to the default reggroup_p. */
1248 ret
= tdesc_register_in_reggroup_p (gdbarch
, regno
, reggroup
);
1252 return default_register_reggroup_p (gdbarch
, regno
, reggroup
);
1255 /* Record architecture-specific functions to call for pseudo-register
1259 set_tdesc_pseudo_register_name (struct gdbarch
*gdbarch
,
1260 gdbarch_register_name_ftype
*pseudo_name
)
1262 struct tdesc_arch_data
*data
1263 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1265 data
->pseudo_register_name
= pseudo_name
;
1269 set_tdesc_pseudo_register_type (struct gdbarch
*gdbarch
,
1270 gdbarch_register_type_ftype
*pseudo_type
)
1272 struct tdesc_arch_data
*data
1273 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1275 data
->pseudo_register_type
= pseudo_type
;
1279 set_tdesc_pseudo_register_reggroup_p
1280 (struct gdbarch
*gdbarch
,
1281 gdbarch_register_reggroup_p_ftype
*pseudo_reggroup_p
)
1283 struct tdesc_arch_data
*data
1284 = (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1286 data
->pseudo_register_reggroup_p
= pseudo_reggroup_p
;
1289 /* Update GDBARCH to use the target description for registers. */
1292 tdesc_use_registers (struct gdbarch
*gdbarch
,
1293 const struct target_desc
*target_desc
,
1294 struct tdesc_arch_data
*early_data
)
1296 int num_regs
= gdbarch_num_regs (gdbarch
);
1297 struct tdesc_arch_data
*data
;
1300 /* We can't use the description for registers if it doesn't describe
1301 any. This function should only be called after validating
1302 registers, so the caller should know that registers are
1304 gdb_assert (tdesc_has_registers (target_desc
));
1306 data
= (struct tdesc_arch_data
*) gdbarch_data (gdbarch
, tdesc_data
);
1307 data
->arch_regs
= early_data
->arch_regs
;
1310 /* Build up a set of all registers, so that we can assign register
1311 numbers where needed. The hash table expands as necessary, so
1312 the initial size is arbitrary. */
1313 reg_hash
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
1314 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1315 for (const tdesc_reg_up
®
: feature
->registers
)
1317 void **slot
= htab_find_slot (reg_hash
, reg
.get (), INSERT
);
1320 /* Add reggroup if its new. */
1321 if (!reg
->group
.empty ())
1322 if (reggroup_find (gdbarch
, reg
->group
.c_str ()) == NULL
)
1323 reggroup_add (gdbarch
, reggroup_gdbarch_new (gdbarch
,
1324 reg
->group
.c_str (),
1328 /* Remove any registers which were assigned numbers by the
1330 for (const tdesc_arch_reg
&arch_reg
: data
->arch_regs
)
1331 if (arch_reg
.reg
!= NULL
)
1332 htab_remove_elt (reg_hash
, arch_reg
.reg
);
1334 /* Assign numbers to the remaining registers and add them to the
1335 list of registers. The new numbers are always above gdbarch_num_regs.
1336 Iterate over the features, not the hash table, so that the order
1337 matches that in the target description. */
1339 gdb_assert (data
->arch_regs
.size () <= num_regs
);
1340 while (data
->arch_regs
.size () < num_regs
)
1341 data
->arch_regs
.emplace_back (nullptr, nullptr);
1343 for (const tdesc_feature_up
&feature
: target_desc
->features
)
1344 for (const tdesc_reg_up
®
: feature
->registers
)
1345 if (htab_find (reg_hash
, reg
.get ()) != NULL
)
1347 data
->arch_regs
.emplace_back (reg
.get (), nullptr);
1351 htab_delete (reg_hash
);
1353 /* Update the architecture. */
1354 set_gdbarch_num_regs (gdbarch
, num_regs
);
1355 set_gdbarch_register_name (gdbarch
, tdesc_register_name
);
1356 set_gdbarch_register_type (gdbarch
, tdesc_register_type
);
1357 set_gdbarch_remote_register_number (gdbarch
,
1358 tdesc_remote_register_number
);
1359 set_gdbarch_register_reggroup_p (gdbarch
, tdesc_register_reggroup_p
);
1363 /* See common/tdesc.h. */
1366 tdesc_create_reg (struct tdesc_feature
*feature
, const char *name
,
1367 int regnum
, int save_restore
, const char *group
,
1368 int bitsize
, const char *type
)
1370 tdesc_reg
*reg
= new tdesc_reg (feature
, name
, regnum
, save_restore
,
1371 group
, bitsize
, type
);
1373 feature
->registers
.emplace_back (reg
);
1376 /* See common/tdesc.h. */
1379 tdesc_create_vector (struct tdesc_feature
*feature
, const char *name
,
1380 struct tdesc_type
*field_type
, int count
)
1382 tdesc_type_vector
*type
= new tdesc_type_vector (name
, field_type
, count
);
1383 feature
->types
.emplace_back (type
);
1388 /* See common/tdesc.h. */
1390 tdesc_type_with_fields
*
1391 tdesc_create_struct (struct tdesc_feature
*feature
, const char *name
)
1393 tdesc_type_with_fields
*type
1394 = new tdesc_type_with_fields (name
, TDESC_TYPE_STRUCT
);
1395 feature
->types
.emplace_back (type
);
1400 /* See common/tdesc.h. */
1403 tdesc_set_struct_size (tdesc_type_with_fields
*type
, int size
)
1405 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1406 gdb_assert (size
> 0);
1410 /* See common/tdesc.h. */
1412 tdesc_type_with_fields
*
1413 tdesc_create_union (struct tdesc_feature
*feature
, const char *name
)
1415 tdesc_type_with_fields
*type
1416 = new tdesc_type_with_fields (name
, TDESC_TYPE_UNION
);
1417 feature
->types
.emplace_back (type
);
1422 /* See common/tdesc.h. */
1424 tdesc_type_with_fields
*
1425 tdesc_create_flags (struct tdesc_feature
*feature
, const char *name
,
1428 gdb_assert (size
> 0);
1430 tdesc_type_with_fields
*type
1431 = new tdesc_type_with_fields (name
, TDESC_TYPE_FLAGS
, size
);
1432 feature
->types
.emplace_back (type
);
1437 tdesc_type_with_fields
*
1438 tdesc_create_enum (struct tdesc_feature
*feature
, const char *name
,
1441 gdb_assert (size
> 0);
1443 tdesc_type_with_fields
*type
1444 = new tdesc_type_with_fields (name
, TDESC_TYPE_ENUM
, size
);
1445 feature
->types
.emplace_back (type
);
1450 /* See common/tdesc.h. */
1453 tdesc_add_field (tdesc_type_with_fields
*type
, const char *field_name
,
1454 struct tdesc_type
*field_type
)
1456 gdb_assert (type
->kind
== TDESC_TYPE_UNION
1457 || type
->kind
== TDESC_TYPE_STRUCT
);
1459 /* Initialize start and end so we know this is not a bit-field
1460 when we print-c-tdesc. */
1461 type
->fields
.emplace_back (field_name
, field_type
, -1, -1);
1465 tdesc_add_typed_bitfield (tdesc_type_with_fields
*type
, const char *field_name
,
1466 int start
, int end
, struct tdesc_type
*field_type
)
1468 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
1469 || type
->kind
== TDESC_TYPE_FLAGS
);
1470 gdb_assert (start
>= 0 && end
>= start
);
1472 type
->fields
.emplace_back (field_name
, field_type
, start
, end
);
1475 /* See common/tdesc.h. */
1478 tdesc_add_bitfield (tdesc_type_with_fields
*type
, const char *field_name
,
1481 struct tdesc_type
*field_type
;
1483 gdb_assert (start
>= 0 && end
>= start
);
1486 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT64
);
1488 field_type
= tdesc_predefined_type (TDESC_TYPE_UINT32
);
1490 tdesc_add_typed_bitfield (type
, field_name
, start
, end
, field_type
);
1493 /* See common/tdesc.h. */
1496 tdesc_add_flag (tdesc_type_with_fields
*type
, int start
,
1497 const char *flag_name
)
1499 gdb_assert (type
->kind
== TDESC_TYPE_FLAGS
1500 || type
->kind
== TDESC_TYPE_STRUCT
);
1502 type
->fields
.emplace_back (flag_name
,
1503 tdesc_predefined_type (TDESC_TYPE_BOOL
),
1508 tdesc_add_enum_value (tdesc_type_with_fields
*type
, int value
,
1511 gdb_assert (type
->kind
== TDESC_TYPE_ENUM
);
1512 type
->fields
.emplace_back (name
,
1513 tdesc_predefined_type (TDESC_TYPE_INT32
),
1517 /* See common/tdesc.h. */
1519 struct tdesc_feature
*
1520 tdesc_create_feature (struct target_desc
*tdesc
, const char *name
,
1523 struct tdesc_feature
*new_feature
= new tdesc_feature (name
);
1525 tdesc
->features
.emplace_back (new_feature
);
1530 struct target_desc
*
1531 allocate_target_description (void)
1533 return new target_desc ();
1537 free_target_description (void *arg
)
1539 struct target_desc
*target_desc
= (struct target_desc
*) arg
;
1545 make_cleanup_free_target_description (struct target_desc
*target_desc
)
1547 return make_cleanup (free_target_description
, target_desc
);
1551 tdesc_add_compatible (struct target_desc
*target_desc
,
1552 const struct bfd_arch_info
*compatible
)
1554 /* If this instance of GDB is compiled without BFD support for the
1555 compatible architecture, simply ignore it -- we would not be able
1556 to handle it anyway. */
1557 if (compatible
== NULL
)
1560 for (const bfd_arch_info
*compat
: target_desc
->compatible
)
1561 if (compat
== compatible
)
1562 internal_error (__FILE__
, __LINE__
,
1563 _("Attempted to add duplicate "
1564 "compatible architecture \"%s\""),
1565 compatible
->printable_name
);
1567 target_desc
->compatible
.push_back (compatible
);
1571 set_tdesc_property (struct target_desc
*target_desc
,
1572 const char *key
, const char *value
)
1574 gdb_assert (key
!= NULL
&& value
!= NULL
);
1576 if (tdesc_property (target_desc
, key
) != NULL
)
1577 internal_error (__FILE__
, __LINE__
,
1578 _("Attempted to add duplicate property \"%s\""), key
);
1580 target_desc
->properties
.emplace_back (key
, value
);
1583 /* See common/tdesc.h. */
1586 set_tdesc_architecture (struct target_desc
*target_desc
,
1589 set_tdesc_architecture (target_desc
, bfd_scan_arch (name
));
1593 set_tdesc_architecture (struct target_desc
*target_desc
,
1594 const struct bfd_arch_info
*arch
)
1596 target_desc
->arch
= arch
;
1599 /* See common/tdesc.h. */
1602 set_tdesc_osabi (struct target_desc
*target_desc
, const char *name
)
1604 set_tdesc_osabi (target_desc
, osabi_from_tdesc_string (name
));
1608 set_tdesc_osabi (struct target_desc
*target_desc
, enum gdb_osabi osabi
)
1610 target_desc
->osabi
= osabi
;
1614 static struct cmd_list_element
*tdesc_set_cmdlist
, *tdesc_show_cmdlist
;
1615 static struct cmd_list_element
*tdesc_unset_cmdlist
;
1617 /* Helper functions for the CLI commands. */
1620 set_tdesc_cmd (const char *args
, int from_tty
)
1622 help_list (tdesc_set_cmdlist
, "set tdesc ", all_commands
, gdb_stdout
);
1626 show_tdesc_cmd (const char *args
, int from_tty
)
1628 cmd_show_list (tdesc_show_cmdlist
, from_tty
, "");
1632 unset_tdesc_cmd (const char *args
, int from_tty
)
1634 help_list (tdesc_unset_cmdlist
, "unset tdesc ", all_commands
, gdb_stdout
);
1638 set_tdesc_filename_cmd (const char *args
, int from_tty
,
1639 struct cmd_list_element
*c
)
1641 xfree (target_description_filename
);
1642 target_description_filename
= xstrdup (tdesc_filename_cmd_string
);
1644 target_clear_description ();
1645 target_find_description ();
1649 show_tdesc_filename_cmd (struct ui_file
*file
, int from_tty
,
1650 struct cmd_list_element
*c
,
1653 value
= target_description_filename
;
1655 if (value
!= NULL
&& *value
!= '\0')
1656 printf_filtered (_("The target description will be read from \"%s\".\n"),
1659 printf_filtered (_("The target description will be "
1660 "read from the target.\n"));
1664 unset_tdesc_filename_cmd (const char *args
, int from_tty
)
1666 xfree (target_description_filename
);
1667 target_description_filename
= NULL
;
1668 target_clear_description ();
1669 target_find_description ();
1672 /* Print target description in C. */
1674 class print_c_tdesc
: public tdesc_element_visitor
1677 print_c_tdesc (std::string
&filename_after_features
)
1678 : m_filename_after_features (filename_after_features
)
1682 const char *filename
= lbasename (m_filename_after_features
.c_str ());
1684 m_function
= (char *) xmalloc (strlen (filename
) + 1);
1685 for (inp
= filename
, outp
= m_function
; *inp
!= '\0'; inp
++)
1688 else if (*inp
== '-')
1694 /* Standard boilerplate. */
1695 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1696 "-*- buffer-read-only: t -*- vi"
1705 void visit_pre (const target_desc
*e
) override
1707 printf_unfiltered (" Original: %s */\n\n",
1708 lbasename (m_filename_after_features
.c_str ()));
1710 printf_unfiltered ("#include \"defs.h\"\n");
1711 printf_unfiltered ("#include \"osabi.h\"\n");
1712 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1713 printf_unfiltered ("\n");
1715 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function
);
1716 printf_unfiltered ("static void\n");
1717 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function
);
1718 printf_unfiltered ("{\n");
1720 (" struct target_desc *result = allocate_target_description ();\n");
1722 if (tdesc_architecture (e
) != NULL
)
1725 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1726 tdesc_architecture (e
)->printable_name
);
1727 printf_unfiltered ("\n");
1729 if (tdesc_osabi (e
) > GDB_OSABI_UNKNOWN
1730 && tdesc_osabi (e
) < GDB_OSABI_INVALID
)
1733 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1734 gdbarch_osabi_name (tdesc_osabi (e
)));
1735 printf_unfiltered ("\n");
1738 for (const bfd_arch_info_type
*compatible
: e
->compatible
)
1740 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1741 compatible
->printable_name
);
1743 if (!e
->compatible
.empty ())
1744 printf_unfiltered ("\n");
1746 for (const property
&prop
: e
->properties
)
1747 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1748 prop
.key
.c_str (), prop
.value
.c_str ());
1750 printf_unfiltered (" struct tdesc_feature *feature;\n");
1753 void visit_pre (const tdesc_feature
*e
) override
1755 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1759 void visit_post (const tdesc_feature
*e
) override
1762 void visit_post (const target_desc
*e
) override
1764 printf_unfiltered ("\n tdesc_%s = result;\n", m_function
);
1765 printf_unfiltered ("}\n");
1768 void visit (const tdesc_type_builtin
*type
) override
1770 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1773 void visit (const tdesc_type_vector
*type
) override
1775 if (!m_printed_element_type
)
1777 printf_unfiltered (" tdesc_type *element_type;\n");
1778 m_printed_element_type
= true;
1782 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1783 type
->element_type
->name
.c_str ());
1785 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1786 type
->name
.c_str (), type
->count
);
1788 printf_unfiltered ("\n");
1791 void visit (const tdesc_type_with_fields
*type
) override
1793 if (!m_printed_type_with_fields
)
1795 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1796 m_printed_type_with_fields
= true;
1801 case TDESC_TYPE_STRUCT
:
1802 case TDESC_TYPE_FLAGS
:
1803 if (type
->kind
== TDESC_TYPE_STRUCT
)
1806 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1807 type
->name
.c_str ());
1808 if (type
->size
!= 0)
1810 (" tdesc_set_struct_size (type_with_fields, %d);\n", type
->size
);
1815 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1816 type
->name
.c_str (), type
->size
);
1818 for (const tdesc_type_field
&f
: type
->fields
)
1820 const char *type_name
;
1822 gdb_assert (f
.type
!= NULL
);
1823 type_name
= f
.type
->name
.c_str ();
1825 /* To minimize changes to generated files, don't emit type
1826 info for fields that have defaulted types. */
1829 gdb_assert (f
.end
!= -1);
1830 if (f
.type
->kind
== TDESC_TYPE_BOOL
)
1832 gdb_assert (f
.start
== f
.end
);
1834 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1835 f
.start
, f
.name
.c_str ());
1837 else if ((type
->size
== 4 && f
.type
->kind
== TDESC_TYPE_UINT32
)
1839 && f
.type
->kind
== TDESC_TYPE_UINT64
))
1842 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1843 f
.name
.c_str (), f
.start
, f
.end
);
1847 printf_field_type_assignment
1848 ("tdesc_named_type (feature, \"%s\");\n",
1851 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1852 " %d, %d, field_type);\n",
1853 f
.name
.c_str (), f
.start
, f
.end
);
1856 else /* Not a bitfield. */
1858 gdb_assert (f
.end
== -1);
1859 gdb_assert (type
->kind
== TDESC_TYPE_STRUCT
);
1860 printf_field_type_assignment
1861 ("tdesc_named_type (feature, \"%s\");\n", type_name
);
1863 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1868 case TDESC_TYPE_UNION
:
1870 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1871 type
->name
.c_str ());
1872 for (const tdesc_type_field
&f
: type
->fields
)
1874 printf_field_type_assignment
1875 ("tdesc_named_type (feature, \"%s\");\n", f
.type
->name
.c_str ());
1877 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1881 case TDESC_TYPE_ENUM
:
1883 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1884 type
->name
.c_str (), type
->size
);
1885 for (const tdesc_type_field
&f
: type
->fields
)
1887 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1888 f
.start
, f
.name
.c_str ());
1891 error (_("C output is not supported type \"%s\"."), type
->name
.c_str ());
1894 printf_unfiltered ("\n");
1897 void visit (const tdesc_reg
*reg
) override
1899 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1900 reg
->name
.c_str (), reg
->target_regnum
,
1902 if (!reg
->group
.empty ())
1903 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
1905 printf_unfiltered ("NULL, ");
1906 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
1910 std::string m_filename_after_features
;
1914 /* Print an assignment to the field_type variable. Print the declaration
1915 of field_type if that has not been done yet. */
1916 ATTRIBUTE_PRINTF (2, 3)
1917 void printf_field_type_assignment (const char *fmt
, ...)
1919 if (!m_printed_field_type
)
1921 printf_unfiltered (" tdesc_type *field_type;\n");
1922 m_printed_field_type
= true;
1925 printf_unfiltered (" field_type = ");
1928 va_start (args
, fmt
);
1929 vprintf_unfiltered (fmt
, args
);
1935 /* Did we print "struct tdesc_type *element_type;" yet? */
1936 bool m_printed_element_type
= false;
1938 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1939 bool m_printed_type_with_fields
= false;
1941 /* Did we print "struct tdesc_type *field_type;" yet? */
1942 bool m_printed_field_type
= false;
1945 /* Print target description feature in C. */
1947 class print_c_feature
: public print_c_tdesc
1950 print_c_feature (std::string
&file
)
1951 : print_c_tdesc (file
)
1954 auto const pos
= m_filename_after_features
.find_last_of ('.');
1956 m_filename_after_features
= m_filename_after_features
.substr (0, pos
);
1959 void visit_pre (const target_desc
*e
) override
1961 printf_unfiltered (" Original: %s */\n\n",
1962 lbasename (m_filename_after_features
.c_str ()));
1964 printf_unfiltered ("#include \"common/tdesc.h\"\n");
1965 printf_unfiltered ("\n");
1968 void visit_post (const target_desc
*e
) override
1971 void visit_pre (const tdesc_feature
*e
) override
1973 std::string
name (m_filename_after_features
);
1975 auto pos
= name
.find_first_of ('.');
1977 name
= name
.substr (0, pos
);
1978 std::replace (name
.begin (), name
.end (), '/', '_');
1979 std::replace (name
.begin (), name
.end (), '-', '_');
1981 printf_unfiltered ("static int\n");
1982 printf_unfiltered ("create_feature_%s ", name
.c_str ());
1983 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
1985 printf_unfiltered ("{\n");
1986 printf_unfiltered (" struct tdesc_feature *feature;\n");
1989 ("\n feature = tdesc_create_feature (result, \"%s\", \"%s\");\n",
1990 e
->name
.c_str (), lbasename (m_filename_after_features
.c_str ()));
1993 void visit_post (const tdesc_feature
*e
) override
1995 printf_unfiltered (" return regnum;\n");
1996 printf_unfiltered ("}\n");
1999 void visit (const tdesc_reg
*reg
) override
2001 /* Most "reg" in XML target descriptions don't have "regnum"
2002 attribute, so the register number is allocated sequentially.
2003 In case that reg has "regnum" attribute, register number
2004 should be set by that explicitly. */
2006 if (reg
->target_regnum
< m_next_regnum
)
2008 /* The integrity check, it can catch some errors on register
2009 number collision, like this,
2011 <reg name="x0" bitsize="32"/>
2012 <reg name="x1" bitsize="32"/>
2013 <reg name="x2" bitsize="32"/>
2014 <reg name="x3" bitsize="32"/>
2015 <reg name="ps" bitsize="32" regnum="3"/>
2017 but it also has false negatives. The target description
2020 <reg name="x1" bitsize="32" regnum="1"/>
2021 <reg name="x3" bitsize="32" regnum="3"/>
2022 <reg name="x2" bitsize="32" regnum="2"/>
2023 <reg name="x4" bitsize="32" regnum="4"/>
2025 but it is not a good practice, so still error on this,
2026 and also print the message so that it can be saved in the
2027 generated c file. */
2029 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2030 reg
->target_regnum
);
2031 printf_unfiltered ("is not the largest number (%d).\n",
2033 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2034 reg
->target_regnum
, m_next_regnum
);
2037 if (reg
->target_regnum
> m_next_regnum
)
2039 printf_unfiltered (" regnum = %ld;\n", reg
->target_regnum
);
2040 m_next_regnum
= reg
->target_regnum
;
2043 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2044 reg
->name
.c_str (), reg
->save_restore
);
2045 if (!reg
->group
.empty ())
2046 printf_unfiltered ("\"%s\", ", reg
->group
.c_str ());
2048 printf_unfiltered ("NULL, ");
2049 printf_unfiltered ("%d, \"%s\");\n", reg
->bitsize
, reg
->type
.c_str ());
2055 /* The register number to use for the next register we see. */
2056 int m_next_regnum
= 0;
2060 maint_print_c_tdesc_cmd (const char *args
, int from_tty
)
2062 const struct target_desc
*tdesc
;
2063 const char *filename
;
2067 /* Use the global target-supplied description, not the current
2068 architecture's. This lets a GDB for one architecture generate C
2069 for another architecture's description, even though the gdbarch
2070 initialization code will reject the new description. */
2071 tdesc
= current_target_desc
;
2072 filename
= target_description_filename
;
2076 /* Use the target description from the XML file. */
2078 tdesc
= file_read_description_xml (filename
);
2082 error (_("There is no target description to print."));
2084 if (filename
== NULL
)
2085 error (_("The current target description did not come from an XML file."));
2087 std::string
filename_after_features (filename
);
2088 auto loc
= filename_after_features
.rfind ("/features/");
2090 if (loc
!= std::string::npos
)
2091 filename_after_features
= filename_after_features
.substr (loc
+ 10);
2093 /* Print c files for target features instead of target descriptions,
2094 because c files got from target features are more flexible than the
2096 if (startswith (filename_after_features
.c_str (), "i386/32bit-")
2097 || startswith (filename_after_features
.c_str (), "i386/64bit-")
2098 || startswith (filename_after_features
.c_str (), "i386/x32-core.xml")
2099 || startswith (filename_after_features
.c_str (), "tic6x-")
2100 || startswith (filename_after_features
.c_str (), "aarch64"))
2102 print_c_feature
v (filename_after_features
);
2108 print_c_tdesc
v (filename_after_features
);
2114 namespace selftests
{
2116 static std::vector
<std::pair
<const char*, const target_desc
*>> xml_tdesc
;
2120 /* See target-descritpions.h. */
2123 record_xml_tdesc (const char *xml_file
, const struct target_desc
*tdesc
)
2125 xml_tdesc
.emplace_back (xml_file
, tdesc
);
2131 /* Check that the target descriptions created dynamically by
2132 architecture-specific code equal the descriptions created from XML files
2133 found in the specified directory DIR. */
2136 maintenance_check_xml_descriptions (const char *dir
, int from_tty
)
2139 error (_("Missing dir name"));
2141 gdb::unique_xmalloc_ptr
<char> dir1 (tilde_expand (dir
));
2142 std::string
feature_dir (dir1
.get ());
2143 unsigned int failed
= 0;
2145 for (auto const &e
: selftests::xml_tdesc
)
2147 std::string tdesc_xml
= (feature_dir
+ SLASH_STRING
+ e
.first
);
2148 const target_desc
*tdesc
2149 = file_read_description_xml (tdesc_xml
.data ());
2151 if (tdesc
== NULL
|| *tdesc
!= *e
.second
)
2154 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2155 (long) selftests::xml_tdesc
.size (), failed
);
2159 _initialize_target_descriptions (void)
2161 tdesc_data
= gdbarch_data_register_pre_init (tdesc_data_init
);
2163 add_prefix_cmd ("tdesc", class_maintenance
, set_tdesc_cmd
, _("\
2164 Set target description specific variables."),
2165 &tdesc_set_cmdlist
, "set tdesc ",
2166 0 /* allow-unknown */, &setlist
);
2167 add_prefix_cmd ("tdesc", class_maintenance
, show_tdesc_cmd
, _("\
2168 Show target description specific variables."),
2169 &tdesc_show_cmdlist
, "show tdesc ",
2170 0 /* allow-unknown */, &showlist
);
2171 add_prefix_cmd ("tdesc", class_maintenance
, unset_tdesc_cmd
, _("\
2172 Unset target description specific variables."),
2173 &tdesc_unset_cmdlist
, "unset tdesc ",
2174 0 /* allow-unknown */, &unsetlist
);
2176 add_setshow_filename_cmd ("filename", class_obscure
,
2177 &tdesc_filename_cmd_string
,
2179 Set the file to read for an XML target description"), _("\
2180 Show the file to read for an XML target description"), _("\
2181 When set, GDB will read the target description from a local\n\
2182 file instead of querying the remote target."),
2183 set_tdesc_filename_cmd
,
2184 show_tdesc_filename_cmd
,
2185 &tdesc_set_cmdlist
, &tdesc_show_cmdlist
);
2187 add_cmd ("filename", class_obscure
, unset_tdesc_filename_cmd
, _("\
2188 Unset the file to read for an XML target description. When unset,\n\
2189 GDB will read the description from the target."),
2190 &tdesc_unset_cmdlist
);
2192 add_cmd ("c-tdesc", class_maintenance
, maint_print_c_tdesc_cmd
, _("\
2193 Print the current target description as a C source file."),
2194 &maintenanceprintlist
);
2196 cmd_list_element
*cmd
;
2198 cmd
= add_cmd ("xml-descriptions", class_maintenance
,
2199 maintenance_check_xml_descriptions
, _("\
2200 Check the target descriptions created in GDB equal the descriptions\n\
2201 created from XML files in the directory.\n\
2202 The parameter is the directory name."),
2203 &maintenancechecklist
);
2204 set_cmd_completer (cmd
, filename_completer
);