Tests for validate symbol file using build-id.
[gdb/archer.git] / gdb / target-descriptions.c
blob95980c534575145aba35f72b8479bc3b953d66c9
1 /* Target description support for GDB.
3 Copyright (C) 2006-2013 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/>. */
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "gdbcmd.h"
25 #include "gdbtypes.h"
26 #include "reggroups.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "vec.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
32 #include "osabi.h"
34 #include "gdb_assert.h"
35 #include "gdb_obstack.h"
36 #include "hashtab.h"
37 #include "inferior.h"
39 /* Types. */
41 typedef struct property
43 char *key;
44 char *value;
45 } property_s;
46 DEF_VEC_O(property_s);
48 /* An individual register from a target description. */
50 typedef struct tdesc_reg
52 /* The name of this register. In standard features, it may be
53 recognized by the architecture support code, or it may be purely
54 for the user. */
55 char *name;
57 /* The register number used by this target to refer to this
58 register. This is used for remote p/P packets and to determine
59 the ordering of registers in the remote g/G packets. */
60 long target_regnum;
62 /* If this flag is set, GDB should save and restore this register
63 around calls to an inferior function. */
64 int save_restore;
66 /* The name of the register group containing this register, or NULL
67 if the group should be automatically determined from the
68 register's type. If this is "general", "float", or "vector", the
69 corresponding "info" command should display this register's
70 value. It can be an arbitrary string, but should be limited to
71 alphanumeric characters and internal hyphens. Currently other
72 strings are ignored (treated as NULL). */
73 char *group;
75 /* The size of the register, in bits. */
76 int bitsize;
78 /* The type of the register. This string corresponds to either
79 a named type from the target description or a predefined
80 type from GDB. */
81 char *type;
83 /* The target-described type corresponding to TYPE, if found. */
84 struct tdesc_type *tdesc_type;
85 } *tdesc_reg_p;
86 DEF_VEC_P(tdesc_reg_p);
88 /* A named type from a target description. */
90 typedef struct tdesc_type_field
92 char *name;
93 struct tdesc_type *type;
94 int start, end;
95 } tdesc_type_field;
96 DEF_VEC_O(tdesc_type_field);
98 typedef struct tdesc_type_flag
100 char *name;
101 int start;
102 } tdesc_type_flag;
103 DEF_VEC_O(tdesc_type_flag);
105 typedef struct tdesc_type
107 /* The name of this type. */
108 char *name;
110 /* Identify the kind of this type. */
111 enum
113 /* Predefined types. */
114 TDESC_TYPE_INT8,
115 TDESC_TYPE_INT16,
116 TDESC_TYPE_INT32,
117 TDESC_TYPE_INT64,
118 TDESC_TYPE_INT128,
119 TDESC_TYPE_UINT8,
120 TDESC_TYPE_UINT16,
121 TDESC_TYPE_UINT32,
122 TDESC_TYPE_UINT64,
123 TDESC_TYPE_UINT128,
124 TDESC_TYPE_CODE_PTR,
125 TDESC_TYPE_DATA_PTR,
126 TDESC_TYPE_IEEE_SINGLE,
127 TDESC_TYPE_IEEE_DOUBLE,
128 TDESC_TYPE_ARM_FPA_EXT,
129 TDESC_TYPE_I387_EXT,
131 /* Types defined by a target feature. */
132 TDESC_TYPE_VECTOR,
133 TDESC_TYPE_STRUCT,
134 TDESC_TYPE_UNION,
135 TDESC_TYPE_FLAGS
136 } kind;
138 /* Kind-specific data. */
139 union
141 /* Vector type. */
142 struct
144 struct tdesc_type *type;
145 int count;
146 } v;
148 /* Struct or union type. */
149 struct
151 VEC(tdesc_type_field) *fields;
152 LONGEST size;
153 } u;
155 /* Flags type. */
156 struct
158 VEC(tdesc_type_flag) *flags;
159 LONGEST size;
160 } f;
161 } u;
162 } *tdesc_type_p;
163 DEF_VEC_P(tdesc_type_p);
165 /* A feature from a target description. Each feature is a collection
166 of other elements, e.g. registers and types. */
168 typedef struct tdesc_feature
170 /* The name of this feature. It may be recognized by the architecture
171 support code. */
172 char *name;
174 /* The registers associated with this feature. */
175 VEC(tdesc_reg_p) *registers;
177 /* The types associated with this feature. */
178 VEC(tdesc_type_p) *types;
179 } *tdesc_feature_p;
180 DEF_VEC_P(tdesc_feature_p);
182 /* A compatible architecture from a target description. */
183 typedef const struct bfd_arch_info *arch_p;
184 DEF_VEC_P(arch_p);
186 /* A target description. */
188 struct target_desc
190 /* The architecture reported by the target, if any. */
191 const struct bfd_arch_info *arch;
193 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
194 otherwise. */
195 enum gdb_osabi osabi;
197 /* The list of compatible architectures reported by the target. */
198 VEC(arch_p) *compatible;
200 /* Any architecture-specific properties specified by the target. */
201 VEC(property_s) *properties;
203 /* The features associated with this target. */
204 VEC(tdesc_feature_p) *features;
207 /* Per-architecture data associated with a target description. The
208 target description may be shared by multiple architectures, but
209 this data is private to one gdbarch. */
211 typedef struct tdesc_arch_reg
213 struct tdesc_reg *reg;
214 struct type *type;
215 } tdesc_arch_reg;
216 DEF_VEC_O(tdesc_arch_reg);
218 struct tdesc_arch_data
220 /* A list of register/type pairs, indexed by GDB's internal register number.
221 During initialization of the gdbarch this list is used to store
222 registers which the architecture assigns a fixed register number.
223 Registers which are NULL in this array, or off the end, are
224 treated as zero-sized and nameless (i.e. placeholders in the
225 numbering). */
226 VEC(tdesc_arch_reg) *arch_regs;
228 /* Functions which report the register name, type, and reggroups for
229 pseudo-registers. */
230 gdbarch_register_name_ftype *pseudo_register_name;
231 gdbarch_register_type_ftype *pseudo_register_type;
232 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
235 /* Info about an inferior's target description. There's one of these
236 for each inferior. */
238 struct target_desc_info
240 /* A flag indicating that a description has already been fetched
241 from the target, so it should not be queried again. */
243 int fetched;
245 /* The description fetched from the target, or NULL if the target
246 did not supply any description. Only valid when
247 target_desc_fetched is set. Only the description initialization
248 code should access this; normally, the description should be
249 accessed through the gdbarch object. */
251 const struct target_desc *tdesc;
253 /* The filename to read a target description from, as set by "set
254 tdesc filename ..." */
256 char *filename;
259 /* Get the inferior INF's target description info, allocating one on
260 the stop if necessary. */
262 static struct target_desc_info *
263 get_tdesc_info (struct inferior *inf)
265 if (inf->tdesc_info == NULL)
266 inf->tdesc_info = XCNEW (struct target_desc_info);
267 return inf->tdesc_info;
270 /* A handle for architecture-specific data associated with the
271 target description (see struct tdesc_arch_data). */
273 static struct gdbarch_data *tdesc_data;
275 /* See target-descriptions.h. */
278 target_desc_info_from_user_p (struct target_desc_info *info)
280 return info != NULL && info->filename != NULL;
283 /* See target-descriptions.h. */
285 void
286 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
288 struct target_desc_info *src = get_tdesc_info (srcinf);
289 struct target_desc_info *dest = get_tdesc_info (destinf);
291 dest->fetched = src->fetched;
292 dest->tdesc = src->tdesc;
293 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
296 /* See target-descriptions.h. */
298 void
299 target_desc_info_free (struct target_desc_info *tdesc_info)
301 if (tdesc_info != NULL)
303 xfree (tdesc_info->filename);
304 xfree (tdesc_info);
308 /* Convenience helper macros. */
310 #define target_desc_fetched \
311 get_tdesc_info (current_inferior ())->fetched
312 #define current_target_desc \
313 get_tdesc_info (current_inferior ())->tdesc
314 #define target_description_filename \
315 get_tdesc_info (current_inferior ())->filename
317 /* The string manipulated by the "set tdesc filename ..." command. */
319 static char *tdesc_filename_cmd_string;
321 /* Fetch the current target's description, and switch the current
322 architecture to one which incorporates that description. */
324 void
325 target_find_description (void)
327 /* If we've already fetched a description from the target, don't do
328 it again. This allows a target to fetch the description early,
329 during its to_open or to_create_inferior, if it needs extra
330 information about the target to initialize. */
331 if (target_desc_fetched)
332 return;
334 /* The current architecture should not have any target description
335 specified. It should have been cleared, e.g. when we
336 disconnected from the previous target. */
337 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
339 /* First try to fetch an XML description from the user-specified
340 file. */
341 current_target_desc = NULL;
342 if (target_description_filename != NULL
343 && *target_description_filename != '\0')
344 current_target_desc
345 = file_read_description_xml (target_description_filename);
347 /* Next try to read the description from the current target using
348 target objects. */
349 if (current_target_desc == NULL)
350 current_target_desc = target_read_description_xml (&current_target);
352 /* If that failed try a target-specific hook. */
353 if (current_target_desc == NULL)
354 current_target_desc = target_read_description (&current_target);
356 /* If a non-NULL description was returned, then update the current
357 architecture. */
358 if (current_target_desc)
360 struct gdbarch_info info;
362 gdbarch_info_init (&info);
363 info.target_desc = current_target_desc;
364 if (!gdbarch_update_p (info))
365 warning (_("Architecture rejected target-supplied description"));
366 else
368 struct tdesc_arch_data *data;
370 data = gdbarch_data (target_gdbarch (), tdesc_data);
371 if (tdesc_has_registers (current_target_desc)
372 && data->arch_regs == NULL)
373 warning (_("Target-supplied registers are not supported "
374 "by the current architecture"));
378 /* Now that we know this description is usable, record that we
379 fetched it. */
380 target_desc_fetched = 1;
383 /* Discard any description fetched from the current target, and switch
384 the current architecture to one with no target description. */
386 void
387 target_clear_description (void)
389 struct gdbarch_info info;
391 if (!target_desc_fetched)
392 return;
394 target_desc_fetched = 0;
395 current_target_desc = NULL;
397 gdbarch_info_init (&info);
398 if (!gdbarch_update_p (info))
399 internal_error (__FILE__, __LINE__,
400 _("Could not remove target-supplied description"));
403 /* Return the global current target description. This should only be
404 used by gdbarch initialization code; most access should be through
405 an existing gdbarch. */
407 const struct target_desc *
408 target_current_description (void)
410 if (target_desc_fetched)
411 return current_target_desc;
413 return NULL;
416 /* Return non-zero if this target description is compatible
417 with the given BFD architecture. */
420 tdesc_compatible_p (const struct target_desc *target_desc,
421 const struct bfd_arch_info *arch)
423 const struct bfd_arch_info *compat;
424 int ix;
426 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
427 ix++)
429 if (compat == arch
430 || arch->compatible (arch, compat)
431 || compat->compatible (compat, arch))
432 return 1;
435 return 0;
439 /* Direct accessors for target descriptions. */
441 /* Return the string value of a property named KEY, or NULL if the
442 property was not specified. */
444 const char *
445 tdesc_property (const struct target_desc *target_desc, const char *key)
447 struct property *prop;
448 int ix;
450 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
451 ix++)
452 if (strcmp (prop->key, key) == 0)
453 return prop->value;
455 return NULL;
458 /* Return the BFD architecture associated with this target
459 description, or NULL if no architecture was specified. */
461 const struct bfd_arch_info *
462 tdesc_architecture (const struct target_desc *target_desc)
464 return target_desc->arch;
467 /* Return the OSABI associated with this target description, or
468 GDB_OSABI_UNKNOWN if no osabi was specified. */
470 enum gdb_osabi
471 tdesc_osabi (const struct target_desc *target_desc)
473 return target_desc->osabi;
478 /* Return 1 if this target description includes any registers. */
481 tdesc_has_registers (const struct target_desc *target_desc)
483 int ix;
484 struct tdesc_feature *feature;
486 if (target_desc == NULL)
487 return 0;
489 for (ix = 0;
490 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
491 ix++)
492 if (! VEC_empty (tdesc_reg_p, feature->registers))
493 return 1;
495 return 0;
498 /* Return the feature with the given name, if present, or NULL if
499 the named feature is not found. */
501 const struct tdesc_feature *
502 tdesc_find_feature (const struct target_desc *target_desc,
503 const char *name)
505 int ix;
506 struct tdesc_feature *feature;
508 for (ix = 0;
509 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
510 ix++)
511 if (strcmp (feature->name, name) == 0)
512 return feature;
514 return NULL;
517 /* Return the name of FEATURE. */
519 const char *
520 tdesc_feature_name (const struct tdesc_feature *feature)
522 return feature->name;
525 /* Predefined types. */
526 static struct tdesc_type tdesc_predefined_types[] =
528 { "int8", TDESC_TYPE_INT8 },
529 { "int16", TDESC_TYPE_INT16 },
530 { "int32", TDESC_TYPE_INT32 },
531 { "int64", TDESC_TYPE_INT64 },
532 { "int128", TDESC_TYPE_INT128 },
533 { "uint8", TDESC_TYPE_UINT8 },
534 { "uint16", TDESC_TYPE_UINT16 },
535 { "uint32", TDESC_TYPE_UINT32 },
536 { "uint64", TDESC_TYPE_UINT64 },
537 { "uint128", TDESC_TYPE_UINT128 },
538 { "code_ptr", TDESC_TYPE_CODE_PTR },
539 { "data_ptr", TDESC_TYPE_DATA_PTR },
540 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
541 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
542 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
543 { "i387_ext", TDESC_TYPE_I387_EXT }
546 /* Return the type associated with ID in the context of FEATURE, or
547 NULL if none. */
549 struct tdesc_type *
550 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
552 int ix;
553 struct tdesc_type *type;
555 /* First try target-defined types. */
556 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
557 if (strcmp (type->name, id) == 0)
558 return type;
560 /* Next try the predefined types. */
561 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
562 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
563 return &tdesc_predefined_types[ix];
565 return NULL;
568 /* Lookup type associated with ID. */
570 struct type *
571 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
573 struct tdesc_arch_reg *reg;
574 struct tdesc_arch_data *data;
575 int i, num_regs;
577 data = gdbarch_data (gdbarch, tdesc_data);
578 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
579 for (i = 0; i < num_regs; i++)
581 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
582 if (reg->reg
583 && reg->reg->tdesc_type
584 && reg->type
585 && strcmp (id, reg->reg->tdesc_type->name) == 0)
586 return reg->type;
589 return NULL;
592 /* Construct, if necessary, and return the GDB type implementing target
593 type TDESC_TYPE for architecture GDBARCH. */
595 static struct type *
596 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
598 struct type *type;
600 switch (tdesc_type->kind)
602 /* Predefined types. */
603 case TDESC_TYPE_INT8:
604 return builtin_type (gdbarch)->builtin_int8;
606 case TDESC_TYPE_INT16:
607 return builtin_type (gdbarch)->builtin_int16;
609 case TDESC_TYPE_INT32:
610 return builtin_type (gdbarch)->builtin_int32;
612 case TDESC_TYPE_INT64:
613 return builtin_type (gdbarch)->builtin_int64;
615 case TDESC_TYPE_INT128:
616 return builtin_type (gdbarch)->builtin_int128;
618 case TDESC_TYPE_UINT8:
619 return builtin_type (gdbarch)->builtin_uint8;
621 case TDESC_TYPE_UINT16:
622 return builtin_type (gdbarch)->builtin_uint16;
624 case TDESC_TYPE_UINT32:
625 return builtin_type (gdbarch)->builtin_uint32;
627 case TDESC_TYPE_UINT64:
628 return builtin_type (gdbarch)->builtin_uint64;
630 case TDESC_TYPE_UINT128:
631 return builtin_type (gdbarch)->builtin_uint128;
633 case TDESC_TYPE_CODE_PTR:
634 return builtin_type (gdbarch)->builtin_func_ptr;
636 case TDESC_TYPE_DATA_PTR:
637 return builtin_type (gdbarch)->builtin_data_ptr;
639 default:
640 break;
643 type = tdesc_find_type (gdbarch, tdesc_type->name);
644 if (type)
645 return type;
647 switch (tdesc_type->kind)
649 case TDESC_TYPE_IEEE_SINGLE:
650 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
651 floatformats_ieee_single);
653 case TDESC_TYPE_IEEE_DOUBLE:
654 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
655 floatformats_ieee_double);
657 case TDESC_TYPE_ARM_FPA_EXT:
658 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
659 floatformats_arm_ext);
661 case TDESC_TYPE_I387_EXT:
662 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
663 floatformats_i387_ext);
665 /* Types defined by a target feature. */
666 case TDESC_TYPE_VECTOR:
668 struct type *type, *field_type;
670 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
671 type = init_vector_type (field_type, tdesc_type->u.v.count);
672 TYPE_NAME (type) = xstrdup (tdesc_type->name);
674 return type;
677 case TDESC_TYPE_STRUCT:
679 struct type *type, *field_type;
680 struct tdesc_type_field *f;
681 int ix;
683 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
684 TYPE_NAME (type) = xstrdup (tdesc_type->name);
685 TYPE_TAG_NAME (type) = TYPE_NAME (type);
687 for (ix = 0;
688 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
689 ix++)
691 if (f->type == NULL)
693 /* Bitfield. */
694 struct field *fld;
695 struct type *field_type;
696 int bitsize, total_size;
698 /* This invariant should be preserved while creating
699 types. */
700 gdb_assert (tdesc_type->u.u.size != 0);
701 if (tdesc_type->u.u.size > 4)
702 field_type = builtin_type (gdbarch)->builtin_uint64;
703 else
704 field_type = builtin_type (gdbarch)->builtin_uint32;
706 fld = append_composite_type_field_raw (type, xstrdup (f->name),
707 field_type);
709 /* For little-endian, BITPOS counts from the LSB of
710 the structure and marks the LSB of the field. For
711 big-endian, BITPOS counts from the MSB of the
712 structure and marks the MSB of the field. Either
713 way, it is the number of bits to the "left" of the
714 field. To calculate this in big-endian, we need
715 the total size of the structure. */
716 bitsize = f->end - f->start + 1;
717 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
718 if (gdbarch_bits_big_endian (gdbarch))
719 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
720 else
721 SET_FIELD_BITPOS (fld[0], f->start);
722 FIELD_BITSIZE (fld[0]) = bitsize;
724 else
726 field_type = tdesc_gdb_type (gdbarch, f->type);
727 append_composite_type_field (type, xstrdup (f->name),
728 field_type);
732 if (tdesc_type->u.u.size != 0)
733 TYPE_LENGTH (type) = tdesc_type->u.u.size;
734 return type;
737 case TDESC_TYPE_UNION:
739 struct type *type, *field_type;
740 struct tdesc_type_field *f;
741 int ix;
743 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
744 TYPE_NAME (type) = xstrdup (tdesc_type->name);
746 for (ix = 0;
747 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
748 ix++)
750 field_type = tdesc_gdb_type (gdbarch, f->type);
751 append_composite_type_field (type, xstrdup (f->name), field_type);
753 /* If any of the children of a union are vectors, flag the
754 union as a vector also. This allows e.g. a union of two
755 vector types to show up automatically in "info vector". */
756 if (TYPE_VECTOR (field_type))
757 TYPE_VECTOR (type) = 1;
759 return type;
762 case TDESC_TYPE_FLAGS:
764 struct tdesc_type_flag *f;
765 int ix;
767 type = arch_flags_type (gdbarch, tdesc_type->name,
768 tdesc_type->u.f.size);
769 for (ix = 0;
770 VEC_iterate (tdesc_type_flag, tdesc_type->u.f.flags, ix, f);
771 ix++)
772 /* Note that contrary to the function name, this call will
773 just set the properties of an already-allocated
774 field. */
775 append_flags_type_flag (type, f->start,
776 *f->name ? f->name : NULL);
778 return type;
782 internal_error (__FILE__, __LINE__,
783 "Type \"%s\" has an unknown kind %d",
784 tdesc_type->name, tdesc_type->kind);
788 /* Support for registers from target descriptions. */
790 /* Construct the per-gdbarch data. */
792 static void *
793 tdesc_data_init (struct obstack *obstack)
795 struct tdesc_arch_data *data;
797 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
798 return data;
801 /* Similar, but for the temporary copy used during architecture
802 initialization. */
804 struct tdesc_arch_data *
805 tdesc_data_alloc (void)
807 return XZALLOC (struct tdesc_arch_data);
810 /* Free something allocated by tdesc_data_alloc, if it is not going
811 to be used (for instance if it was unsuitable for the
812 architecture). */
814 void
815 tdesc_data_cleanup (void *data_untyped)
817 struct tdesc_arch_data *data = data_untyped;
819 VEC_free (tdesc_arch_reg, data->arch_regs);
820 xfree (data);
823 /* Search FEATURE for a register named NAME. */
825 static struct tdesc_reg *
826 tdesc_find_register_early (const struct tdesc_feature *feature,
827 const char *name)
829 int ixr;
830 struct tdesc_reg *reg;
832 for (ixr = 0;
833 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
834 ixr++)
835 if (strcasecmp (reg->name, name) == 0)
836 return reg;
838 return NULL;
841 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
844 tdesc_numbered_register (const struct tdesc_feature *feature,
845 struct tdesc_arch_data *data,
846 int regno, const char *name)
848 struct tdesc_arch_reg arch_reg = { 0 };
849 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
851 if (reg == NULL)
852 return 0;
854 /* Make sure the vector includes a REGNO'th element. */
855 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
856 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
858 arch_reg.reg = reg;
859 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
860 return 1;
863 /* Search FEATURE for a register named NAME, but do not assign a fixed
864 register number to it. */
867 tdesc_unnumbered_register (const struct tdesc_feature *feature,
868 const char *name)
870 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
872 if (reg == NULL)
873 return 0;
875 return 1;
878 /* Search FEATURE for a register whose name is in NAMES and assign
879 REGNO to it. */
882 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
883 struct tdesc_arch_data *data,
884 int regno, const char *const names[])
886 int i;
888 for (i = 0; names[i] != NULL; i++)
889 if (tdesc_numbered_register (feature, data, regno, names[i]))
890 return 1;
892 return 0;
895 /* Search FEATURE for a register named NAME, and return its size in
896 bits. The register must exist. */
899 tdesc_register_size (const struct tdesc_feature *feature,
900 const char *name)
902 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
904 gdb_assert (reg != NULL);
905 return reg->bitsize;
908 /* Look up a register by its GDB internal register number. */
910 static struct tdesc_arch_reg *
911 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
913 struct tdesc_arch_data *data;
915 data = gdbarch_data (gdbarch, tdesc_data);
916 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
917 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
918 else
919 return NULL;
922 static struct tdesc_reg *
923 tdesc_find_register (struct gdbarch *gdbarch, int regno)
925 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
927 return reg? reg->reg : NULL;
930 /* Return the name of register REGNO, from the target description or
931 from an architecture-provided pseudo_register_name method. */
933 const char *
934 tdesc_register_name (struct gdbarch *gdbarch, int regno)
936 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
937 int num_regs = gdbarch_num_regs (gdbarch);
938 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
940 if (reg != NULL)
941 return reg->name;
943 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
945 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
947 gdb_assert (data->pseudo_register_name != NULL);
948 return data->pseudo_register_name (gdbarch, regno);
951 return "";
954 struct type *
955 tdesc_register_type (struct gdbarch *gdbarch, int regno)
957 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
958 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
959 int num_regs = gdbarch_num_regs (gdbarch);
960 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
962 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
964 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
966 gdb_assert (data->pseudo_register_type != NULL);
967 return data->pseudo_register_type (gdbarch, regno);
970 if (reg == NULL)
971 /* Return "int0_t", since "void" has a misleading size of one. */
972 return builtin_type (gdbarch)->builtin_int0;
974 if (arch_reg->type == NULL)
976 /* First check for a predefined or target defined type. */
977 if (reg->tdesc_type)
978 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
980 /* Next try size-sensitive type shortcuts. */
981 else if (strcmp (reg->type, "float") == 0)
983 if (reg->bitsize == gdbarch_float_bit (gdbarch))
984 arch_reg->type = builtin_type (gdbarch)->builtin_float;
985 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
986 arch_reg->type = builtin_type (gdbarch)->builtin_double;
987 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
988 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
989 else
991 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
992 reg->name, reg->bitsize);
993 arch_reg->type = builtin_type (gdbarch)->builtin_double;
996 else if (strcmp (reg->type, "int") == 0)
998 if (reg->bitsize == gdbarch_long_bit (gdbarch))
999 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1000 else if (reg->bitsize == TARGET_CHAR_BIT)
1001 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1002 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1003 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1004 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1005 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1006 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1007 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1008 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1009 /* A bit desperate by this point... */
1010 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1011 else
1013 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1014 reg->name, reg->bitsize);
1015 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1019 if (arch_reg->type == NULL)
1020 internal_error (__FILE__, __LINE__,
1021 "Register \"%s\" has an unknown type \"%s\"",
1022 reg->name, reg->type);
1025 return arch_reg->type;
1028 static int
1029 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1031 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1033 if (reg != NULL)
1034 return reg->target_regnum;
1035 else
1036 return -1;
1039 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1040 target description may be classified as general, float, or vector.
1041 Unlike a gdbarch register_reggroup_p method, this function will
1042 return -1 if it does not know; the caller should handle registers
1043 with no specified group.
1045 Arbitrary strings (other than "general", "float", and "vector")
1046 from the description are not used; they cause the register to be
1047 displayed in "info all-registers" but excluded from "info
1048 registers" et al. The names of containing features are also not
1049 used. This might be extended to display registers in some more
1050 useful groupings.
1052 The save-restore flag is also implemented here. */
1055 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1056 struct reggroup *reggroup)
1058 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1060 if (reg != NULL && reg->group != NULL)
1062 int general_p = 0, float_p = 0, vector_p = 0;
1064 if (strcmp (reg->group, "general") == 0)
1065 general_p = 1;
1066 else if (strcmp (reg->group, "float") == 0)
1067 float_p = 1;
1068 else if (strcmp (reg->group, "vector") == 0)
1069 vector_p = 1;
1071 if (reggroup == float_reggroup)
1072 return float_p;
1074 if (reggroup == vector_reggroup)
1075 return vector_p;
1077 if (reggroup == general_reggroup)
1078 return general_p;
1081 if (reg != NULL
1082 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1083 return reg->save_restore;
1085 return -1;
1088 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1089 group specified go to the default reggroup function and are handled
1090 by type. */
1092 static int
1093 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1094 struct reggroup *reggroup)
1096 int num_regs = gdbarch_num_regs (gdbarch);
1097 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1098 int ret;
1100 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1102 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1104 if (data->pseudo_register_reggroup_p != NULL)
1105 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1106 /* Otherwise fall through to the default reggroup_p. */
1109 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1110 if (ret != -1)
1111 return ret;
1113 return default_register_reggroup_p (gdbarch, regno, reggroup);
1116 /* Record architecture-specific functions to call for pseudo-register
1117 support. */
1119 void
1120 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1121 gdbarch_register_name_ftype *pseudo_name)
1123 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1125 data->pseudo_register_name = pseudo_name;
1128 void
1129 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1130 gdbarch_register_type_ftype *pseudo_type)
1132 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1134 data->pseudo_register_type = pseudo_type;
1137 void
1138 set_tdesc_pseudo_register_reggroup_p
1139 (struct gdbarch *gdbarch,
1140 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1142 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1144 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1147 /* Update GDBARCH to use the target description for registers. */
1149 void
1150 tdesc_use_registers (struct gdbarch *gdbarch,
1151 const struct target_desc *target_desc,
1152 struct tdesc_arch_data *early_data)
1154 int num_regs = gdbarch_num_regs (gdbarch);
1155 int ixf, ixr;
1156 struct tdesc_feature *feature;
1157 struct tdesc_reg *reg;
1158 struct tdesc_arch_data *data;
1159 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1160 htab_t reg_hash;
1162 /* We can't use the description for registers if it doesn't describe
1163 any. This function should only be called after validating
1164 registers, so the caller should know that registers are
1165 included. */
1166 gdb_assert (tdesc_has_registers (target_desc));
1168 data = gdbarch_data (gdbarch, tdesc_data);
1169 data->arch_regs = early_data->arch_regs;
1170 xfree (early_data);
1172 /* Build up a set of all registers, so that we can assign register
1173 numbers where needed. The hash table expands as necessary, so
1174 the initial size is arbitrary. */
1175 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1176 for (ixf = 0;
1177 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1178 ixf++)
1179 for (ixr = 0;
1180 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1181 ixr++)
1183 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1185 *slot = reg;
1188 /* Remove any registers which were assigned numbers by the
1189 architecture. */
1190 for (ixr = 0;
1191 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1192 ixr++)
1193 if (arch_reg->reg)
1194 htab_remove_elt (reg_hash, arch_reg->reg);
1196 /* Assign numbers to the remaining registers and add them to the
1197 list of registers. The new numbers are always above gdbarch_num_regs.
1198 Iterate over the features, not the hash table, so that the order
1199 matches that in the target description. */
1201 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1202 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1203 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1204 for (ixf = 0;
1205 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1206 ixf++)
1207 for (ixr = 0;
1208 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1209 ixr++)
1210 if (htab_find (reg_hash, reg) != NULL)
1212 new_arch_reg.reg = reg;
1213 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1214 num_regs++;
1217 htab_delete (reg_hash);
1219 /* Update the architecture. */
1220 set_gdbarch_num_regs (gdbarch, num_regs);
1221 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1222 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1223 set_gdbarch_remote_register_number (gdbarch,
1224 tdesc_remote_register_number);
1225 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1229 /* Methods for constructing a target description. */
1231 static void
1232 tdesc_free_reg (struct tdesc_reg *reg)
1234 xfree (reg->name);
1235 xfree (reg->type);
1236 xfree (reg->group);
1237 xfree (reg);
1240 void
1241 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1242 int regnum, int save_restore, const char *group,
1243 int bitsize, const char *type)
1245 struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
1247 reg->name = xstrdup (name);
1248 reg->target_regnum = regnum;
1249 reg->save_restore = save_restore;
1250 reg->group = group ? xstrdup (group) : NULL;
1251 reg->bitsize = bitsize;
1252 reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
1254 /* If the register's type is target-defined, look it up now. We may not
1255 have easy access to the containing feature when we want it later. */
1256 reg->tdesc_type = tdesc_named_type (feature, reg->type);
1258 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1261 static void
1262 tdesc_free_type (struct tdesc_type *type)
1264 switch (type->kind)
1266 case TDESC_TYPE_STRUCT:
1267 case TDESC_TYPE_UNION:
1269 struct tdesc_type_field *f;
1270 int ix;
1272 for (ix = 0;
1273 VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
1274 ix++)
1275 xfree (f->name);
1277 VEC_free (tdesc_type_field, type->u.u.fields);
1279 break;
1281 case TDESC_TYPE_FLAGS:
1283 struct tdesc_type_flag *f;
1284 int ix;
1286 for (ix = 0;
1287 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
1288 ix++)
1289 xfree (f->name);
1291 VEC_free (tdesc_type_flag, type->u.f.flags);
1293 break;
1295 default:
1296 break;
1299 xfree (type->name);
1300 xfree (type);
1303 struct tdesc_type *
1304 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1305 struct tdesc_type *field_type, int count)
1307 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1309 type->name = xstrdup (name);
1310 type->kind = TDESC_TYPE_VECTOR;
1311 type->u.v.type = field_type;
1312 type->u.v.count = count;
1314 VEC_safe_push (tdesc_type_p, feature->types, type);
1315 return type;
1318 struct tdesc_type *
1319 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1321 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1323 type->name = xstrdup (name);
1324 type->kind = TDESC_TYPE_STRUCT;
1326 VEC_safe_push (tdesc_type_p, feature->types, type);
1327 return type;
1330 /* Set the total length of TYPE. Structs which contain bitfields may
1331 omit the reserved bits, so the end of the last field may not
1332 suffice. */
1334 void
1335 tdesc_set_struct_size (struct tdesc_type *type, LONGEST size)
1337 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1338 type->u.u.size = size;
1341 struct tdesc_type *
1342 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1344 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1346 type->name = xstrdup (name);
1347 type->kind = TDESC_TYPE_UNION;
1349 VEC_safe_push (tdesc_type_p, feature->types, type);
1350 return type;
1353 struct tdesc_type *
1354 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1355 LONGEST size)
1357 struct tdesc_type *type = XZALLOC (struct tdesc_type);
1359 type->name = xstrdup (name);
1360 type->kind = TDESC_TYPE_FLAGS;
1361 type->u.f.size = size;
1363 VEC_safe_push (tdesc_type_p, feature->types, type);
1364 return type;
1367 /* Add a new field. Return a temporary pointer to the field, which
1368 is only valid until the next call to tdesc_add_field (the vector
1369 might be reallocated). */
1371 void
1372 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1373 struct tdesc_type *field_type)
1375 struct tdesc_type_field f = { 0 };
1377 gdb_assert (type->kind == TDESC_TYPE_UNION
1378 || type->kind == TDESC_TYPE_STRUCT);
1380 f.name = xstrdup (field_name);
1381 f.type = field_type;
1383 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1386 /* Add a new bitfield. */
1388 void
1389 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1390 int start, int end)
1392 struct tdesc_type_field f = { 0 };
1394 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1396 f.name = xstrdup (field_name);
1397 f.start = start;
1398 f.end = end;
1400 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1403 void
1404 tdesc_add_flag (struct tdesc_type *type, int start,
1405 const char *flag_name)
1407 struct tdesc_type_flag f = { 0 };
1409 gdb_assert (type->kind == TDESC_TYPE_FLAGS);
1411 f.name = xstrdup (flag_name);
1412 f.start = start;
1414 VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
1417 static void
1418 tdesc_free_feature (struct tdesc_feature *feature)
1420 struct tdesc_reg *reg;
1421 struct tdesc_type *type;
1422 int ix;
1424 for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
1425 tdesc_free_reg (reg);
1426 VEC_free (tdesc_reg_p, feature->registers);
1428 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
1429 tdesc_free_type (type);
1430 VEC_free (tdesc_type_p, feature->types);
1432 xfree (feature->name);
1433 xfree (feature);
1436 struct tdesc_feature *
1437 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1439 struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
1441 new_feature->name = xstrdup (name);
1443 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1444 return new_feature;
1447 struct target_desc *
1448 allocate_target_description (void)
1450 return XZALLOC (struct target_desc);
1453 static void
1454 free_target_description (void *arg)
1456 struct target_desc *target_desc = arg;
1457 struct tdesc_feature *feature;
1458 struct property *prop;
1459 int ix;
1461 for (ix = 0;
1462 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
1463 ix++)
1464 tdesc_free_feature (feature);
1465 VEC_free (tdesc_feature_p, target_desc->features);
1467 for (ix = 0;
1468 VEC_iterate (property_s, target_desc->properties, ix, prop);
1469 ix++)
1471 xfree (prop->key);
1472 xfree (prop->value);
1474 VEC_free (property_s, target_desc->properties);
1476 VEC_free (arch_p, target_desc->compatible);
1478 xfree (target_desc);
1481 struct cleanup *
1482 make_cleanup_free_target_description (struct target_desc *target_desc)
1484 return make_cleanup (free_target_description, target_desc);
1487 void
1488 tdesc_add_compatible (struct target_desc *target_desc,
1489 const struct bfd_arch_info *compatible)
1491 const struct bfd_arch_info *compat;
1492 int ix;
1494 /* If this instance of GDB is compiled without BFD support for the
1495 compatible architecture, simply ignore it -- we would not be able
1496 to handle it anyway. */
1497 if (compatible == NULL)
1498 return;
1500 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1501 ix++)
1502 if (compat == compatible)
1503 internal_error (__FILE__, __LINE__,
1504 _("Attempted to add duplicate "
1505 "compatible architecture \"%s\""),
1506 compatible->printable_name);
1508 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1511 void
1512 set_tdesc_property (struct target_desc *target_desc,
1513 const char *key, const char *value)
1515 struct property *prop, new_prop;
1516 int ix;
1518 gdb_assert (key != NULL && value != NULL);
1520 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1521 ix++)
1522 if (strcmp (prop->key, key) == 0)
1523 internal_error (__FILE__, __LINE__,
1524 _("Attempted to add duplicate property \"%s\""), key);
1526 new_prop.key = xstrdup (key);
1527 new_prop.value = xstrdup (value);
1528 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1531 void
1532 set_tdesc_architecture (struct target_desc *target_desc,
1533 const struct bfd_arch_info *arch)
1535 target_desc->arch = arch;
1538 void
1539 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1541 target_desc->osabi = osabi;
1545 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1546 static struct cmd_list_element *tdesc_unset_cmdlist;
1548 /* Helper functions for the CLI commands. */
1550 static void
1551 set_tdesc_cmd (char *args, int from_tty)
1553 help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
1556 static void
1557 show_tdesc_cmd (char *args, int from_tty)
1559 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1562 static void
1563 unset_tdesc_cmd (char *args, int from_tty)
1565 help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
1568 static void
1569 set_tdesc_filename_cmd (char *args, int from_tty,
1570 struct cmd_list_element *c)
1572 xfree (target_description_filename);
1573 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1575 target_clear_description ();
1576 target_find_description ();
1579 static void
1580 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1581 struct cmd_list_element *c,
1582 const char *value)
1584 value = target_description_filename;
1586 if (value != NULL && *value != '\0')
1587 printf_filtered (_("The target description will be read from \"%s\".\n"),
1588 value);
1589 else
1590 printf_filtered (_("The target description will be "
1591 "read from the target.\n"));
1594 static void
1595 unset_tdesc_filename_cmd (char *args, int from_tty)
1597 xfree (target_description_filename);
1598 target_description_filename = NULL;
1599 target_clear_description ();
1600 target_find_description ();
1603 static void
1604 maint_print_c_tdesc_cmd (char *args, int from_tty)
1606 const struct target_desc *tdesc;
1607 const struct bfd_arch_info *compatible;
1608 const char *filename, *inp;
1609 char *function, *outp;
1610 struct property *prop;
1611 struct tdesc_feature *feature;
1612 struct tdesc_reg *reg;
1613 struct tdesc_type *type;
1614 struct tdesc_type_field *f;
1615 struct tdesc_type_flag *flag;
1616 int ix, ix2, ix3;
1617 int printed_field_type = 0;
1619 /* Use the global target-supplied description, not the current
1620 architecture's. This lets a GDB for one architecture generate C
1621 for another architecture's description, even though the gdbarch
1622 initialization code will reject the new description. */
1623 tdesc = current_target_desc;
1624 if (tdesc == NULL)
1625 error (_("There is no target description to print."));
1627 if (target_description_filename == NULL)
1628 error (_("The current target description did not come from an XML file."));
1630 filename = lbasename (target_description_filename);
1631 function = alloca (strlen (filename) + 1);
1632 for (inp = filename, outp = function; *inp != '\0'; inp++)
1633 if (*inp == '.')
1634 break;
1635 else if (*inp == '-')
1636 *outp++ = '_';
1637 else
1638 *outp++ = *inp;
1639 *outp = '\0';
1641 /* Standard boilerplate. */
1642 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1643 "-*- buffer-read-only: t -*- vi"
1644 ":set ro:\n");
1645 printf_unfiltered (" Original: %s */\n\n", filename);
1646 printf_unfiltered ("#include \"defs.h\"\n");
1647 printf_unfiltered ("#include \"osabi.h\"\n");
1648 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1649 printf_unfiltered ("\n");
1651 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1652 printf_unfiltered ("static void\n");
1653 printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1654 printf_unfiltered ("{\n");
1655 printf_unfiltered
1656 (" struct target_desc *result = allocate_target_description ();\n");
1657 printf_unfiltered (" struct tdesc_feature *feature;\n");
1659 /* Now we do some "filtering" in order to know which variables to
1660 declare. This is needed because otherwise we would declare unused
1661 variables `field_type' and `type'. */
1662 for (ix = 0;
1663 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1664 ix++)
1666 int printed_desc_type = 0;
1668 for (ix2 = 0;
1669 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1670 ix2++)
1672 if (!printed_field_type)
1674 printf_unfiltered (" struct tdesc_type *field_type;\n");
1675 printed_field_type = 1;
1678 if (type->kind == TDESC_TYPE_UNION
1679 && VEC_length (tdesc_type_field, type->u.u.fields) > 0)
1681 printf_unfiltered (" struct tdesc_type *type;\n");
1682 printed_desc_type = 1;
1683 break;
1687 if (printed_desc_type)
1688 break;
1691 printf_unfiltered ("\n");
1693 if (tdesc_architecture (tdesc) != NULL)
1695 printf_unfiltered
1696 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1697 tdesc_architecture (tdesc)->printable_name);
1698 printf_unfiltered ("\n");
1701 if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
1702 && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
1704 printf_unfiltered
1705 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1706 gdbarch_osabi_name (tdesc_osabi (tdesc)));
1707 printf_unfiltered ("\n");
1710 for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
1711 ix++)
1713 printf_unfiltered
1714 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1715 compatible->printable_name);
1717 if (ix)
1718 printf_unfiltered ("\n");
1720 for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1721 ix++)
1723 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1724 prop->key, prop->value);
1727 for (ix = 0;
1728 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1729 ix++)
1731 printf_unfiltered (" \
1732 feature = tdesc_create_feature (result, \"%s\");\n",
1733 feature->name);
1735 for (ix2 = 0;
1736 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1737 ix2++)
1739 switch (type->kind)
1741 case TDESC_TYPE_VECTOR:
1742 printf_unfiltered
1743 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1744 type->u.v.type->name);
1745 printf_unfiltered
1746 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1747 type->name, type->u.v.count);
1748 break;
1749 case TDESC_TYPE_UNION:
1750 printf_unfiltered
1751 (" type = tdesc_create_union (feature, \"%s\");\n",
1752 type->name);
1753 for (ix3 = 0;
1754 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1755 ix3++)
1757 printf_unfiltered
1758 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1759 f->type->name);
1760 printf_unfiltered
1761 (" tdesc_add_field (type, \"%s\", field_type);\n",
1762 f->name);
1764 break;
1765 case TDESC_TYPE_FLAGS:
1766 printf_unfiltered
1767 (" field_type = tdesc_create_flags (feature, \"%s\", %d);\n",
1768 type->name, (int) type->u.f.size);
1769 for (ix3 = 0;
1770 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
1771 flag);
1772 ix3++)
1773 printf_unfiltered
1774 (" tdesc_add_flag (field_type, %d, \"%s\");\n",
1775 flag->start, flag->name);
1776 break;
1777 default:
1778 error (_("C output is not supported type \"%s\"."), type->name);
1780 printf_unfiltered ("\n");
1783 for (ix2 = 0;
1784 VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1785 ix2++)
1787 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1788 reg->name, reg->target_regnum, reg->save_restore);
1789 if (reg->group)
1790 printf_unfiltered ("\"%s\", ", reg->group);
1791 else
1792 printf_unfiltered ("NULL, ");
1793 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1796 printf_unfiltered ("\n");
1799 printf_unfiltered (" tdesc_%s = result;\n", function);
1800 printf_unfiltered ("}\n");
1803 /* Provide a prototype to silence -Wmissing-prototypes. */
1804 extern initialize_file_ftype _initialize_target_descriptions;
1806 void
1807 _initialize_target_descriptions (void)
1809 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1811 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1812 Set target description specific variables."),
1813 &tdesc_set_cmdlist, "set tdesc ",
1814 0 /* allow-unknown */, &setlist);
1815 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1816 Show target description specific variables."),
1817 &tdesc_show_cmdlist, "show tdesc ",
1818 0 /* allow-unknown */, &showlist);
1819 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1820 Unset target description specific variables."),
1821 &tdesc_unset_cmdlist, "unset tdesc ",
1822 0 /* allow-unknown */, &unsetlist);
1824 add_setshow_filename_cmd ("filename", class_obscure,
1825 &tdesc_filename_cmd_string,
1826 _("\
1827 Set the file to read for an XML target description"), _("\
1828 Show the file to read for an XML target description"), _("\
1829 When set, GDB will read the target description from a local\n\
1830 file instead of querying the remote target."),
1831 set_tdesc_filename_cmd,
1832 show_tdesc_filename_cmd,
1833 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1835 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1836 Unset the file to read for an XML target description. When unset,\n\
1837 GDB will read the description from the target."),
1838 &tdesc_unset_cmdlist);
1840 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1841 Print the current target description as a C source file."),
1842 &maintenanceprintlist);