* solib-svr4.c (IGNORE_FIRST_LINK_MAP_ENTRY): Avoid dereferencing
[binutils-gdb.git] / gdb / target-descriptions.c
blobc1eec14542eb416771768e1e8ab2e312b5521492
1 /* Target description support for GDB.
3 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 Contributed by CodeSourcery.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
33 #include "gdb_assert.h"
34 #include "gdb_obstack.h"
35 #include "hashtab.h"
37 /* Types. */
39 typedef struct property
41 char *key;
42 char *value;
43 } property_s;
44 DEF_VEC_O(property_s);
46 /* An individual register from a target description. */
48 typedef struct tdesc_reg
50 /* The name of this register. In standard features, it may be
51 recognized by the architecture support code, or it may be purely
52 for the user. */
53 char *name;
55 /* The register number used by this target to refer to this
56 register. This is used for remote p/P packets and to determine
57 the ordering of registers in the remote g/G packets. */
58 long target_regnum;
60 /* If this flag is set, GDB should save and restore this register
61 around calls to an inferior function. */
62 int save_restore;
64 /* The name of the register group containing this register, or NULL
65 if the group should be automatically determined from the
66 register's type. If this is "general", "float", or "vector", the
67 corresponding "info" command should display this register's
68 value. It can be an arbitrary string, but should be limited to
69 alphanumeric characters and internal hyphens. Currently other
70 strings are ignored (treated as NULL). */
71 char *group;
73 /* The size of the register, in bits. */
74 int bitsize;
76 /* The type of the register. This string corresponds to either
77 a named type from the target description or a predefined
78 type from GDB. */
79 char *type;
81 /* The target-described type corresponding to TYPE, if found. */
82 struct type *gdb_type;
83 } *tdesc_reg_p;
84 DEF_VEC_P(tdesc_reg_p);
86 /* A named type from a target description. */
87 typedef struct type *type_p;
88 DEF_VEC_P(type_p);
90 /* A feature from a target description. Each feature is a collection
91 of other elements, e.g. registers and types. */
93 typedef struct tdesc_feature
95 /* The name of this feature. It may be recognized by the architecture
96 support code. */
97 char *name;
99 /* The registers associated with this feature. */
100 VEC(tdesc_reg_p) *registers;
102 /* The types associated with this feature. */
103 VEC(type_p) *types;
104 } *tdesc_feature_p;
105 DEF_VEC_P(tdesc_feature_p);
107 /* A target description. */
109 struct target_desc
111 /* The architecture reported by the target, if any. */
112 const struct bfd_arch_info *arch;
114 /* Any architecture-specific properties specified by the target. */
115 VEC(property_s) *properties;
117 /* The features associated with this target. */
118 VEC(tdesc_feature_p) *features;
121 /* Per-architecture data associated with a target description. The
122 target description may be shared by multiple architectures, but
123 this data is private to one gdbarch. */
125 struct tdesc_arch_data
127 /* A list of registers, indexed by GDB's internal register number.
128 During initialization of the gdbarch this list is used to store
129 registers which the architecture assigns a fixed register number.
130 Registers which are NULL in this array, or off the end, are
131 treated as zero-sized and nameless (i.e. placeholders in the
132 numbering). */
133 VEC(tdesc_reg_p) *registers;
135 /* Functions which report the register name, type, and reggroups for
136 pseudo-registers. */
137 gdbarch_register_name_ftype *pseudo_register_name;
138 gdbarch_register_type_ftype *pseudo_register_type;
139 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
142 /* Global state. These variables are associated with the current
143 target; if GDB adds support for multiple simultaneous targets, then
144 these variables should become target-specific data. */
146 /* A flag indicating that a description has already been fetched from
147 the current target, so it should not be queried again. */
149 static int target_desc_fetched;
151 /* The description fetched from the current target, or NULL if the
152 current target did not supply any description. Only valid when
153 target_desc_fetched is set. Only the description initialization
154 code should access this; normally, the description should be
155 accessed through the gdbarch object. */
157 static const struct target_desc *current_target_desc;
159 /* Other global variables. */
161 /* The filename to read a target description from. */
163 static char *target_description_filename;
165 /* A handle for architecture-specific data associated with the
166 target description (see struct tdesc_arch_data). */
168 static struct gdbarch_data *tdesc_data;
170 /* Fetch the current target's description, and switch the current
171 architecture to one which incorporates that description. */
173 void
174 target_find_description (void)
176 /* If we've already fetched a description from the target, don't do
177 it again. This allows a target to fetch the description early,
178 during its to_open or to_create_inferior, if it needs extra
179 information about the target to initialize. */
180 if (target_desc_fetched)
181 return;
183 /* The current architecture should not have any target description
184 specified. It should have been cleared, e.g. when we
185 disconnected from the previous target. */
186 gdb_assert (gdbarch_target_desc (target_gdbarch) == NULL);
188 /* First try to fetch an XML description from the user-specified
189 file. */
190 current_target_desc = NULL;
191 if (target_description_filename != NULL
192 && *target_description_filename != '\0')
193 current_target_desc
194 = file_read_description_xml (target_description_filename);
196 /* Next try to read the description from the current target using
197 target objects. */
198 if (current_target_desc == NULL)
199 current_target_desc = target_read_description_xml (&current_target);
201 /* If that failed try a target-specific hook. */
202 if (current_target_desc == NULL)
203 current_target_desc = target_read_description (&current_target);
205 /* If a non-NULL description was returned, then update the current
206 architecture. */
207 if (current_target_desc)
209 struct gdbarch_info info;
211 gdbarch_info_init (&info);
212 info.target_desc = current_target_desc;
213 if (!gdbarch_update_p (info))
214 warning (_("Architecture rejected target-supplied description"));
215 else
217 struct tdesc_arch_data *data;
219 data = gdbarch_data (target_gdbarch, tdesc_data);
220 if (tdesc_has_registers (current_target_desc)
221 && data->registers == NULL)
222 warning (_("Target-supplied registers are not supported "
223 "by the current architecture"));
227 /* Now that we know this description is usable, record that we
228 fetched it. */
229 target_desc_fetched = 1;
232 /* Discard any description fetched from the current target, and switch
233 the current architecture to one with no target description. */
235 void
236 target_clear_description (void)
238 struct gdbarch_info info;
240 if (!target_desc_fetched)
241 return;
243 target_desc_fetched = 0;
244 current_target_desc = NULL;
246 gdbarch_info_init (&info);
247 if (!gdbarch_update_p (info))
248 internal_error (__FILE__, __LINE__,
249 _("Could not remove target-supplied description"));
252 /* Return the global current target description. This should only be
253 used by gdbarch initialization code; most access should be through
254 an existing gdbarch. */
256 const struct target_desc *
257 target_current_description (void)
259 if (target_desc_fetched)
260 return current_target_desc;
262 return NULL;
266 /* Direct accessors for target descriptions. */
268 /* Return the string value of a property named KEY, or NULL if the
269 property was not specified. */
271 const char *
272 tdesc_property (const struct target_desc *target_desc, const char *key)
274 struct property *prop;
275 int ix;
277 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
278 ix++)
279 if (strcmp (prop->key, key) == 0)
280 return prop->value;
282 return NULL;
285 /* Return the BFD architecture associated with this target
286 description, or NULL if no architecture was specified. */
288 const struct bfd_arch_info *
289 tdesc_architecture (const struct target_desc *target_desc)
291 return target_desc->arch;
295 /* Return 1 if this target description includes any registers. */
298 tdesc_has_registers (const struct target_desc *target_desc)
300 int ix;
301 struct tdesc_feature *feature;
303 if (target_desc == NULL)
304 return 0;
306 for (ix = 0;
307 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
308 ix++)
309 if (! VEC_empty (tdesc_reg_p, feature->registers))
310 return 1;
312 return 0;
315 /* Return the feature with the given name, if present, or NULL if
316 the named feature is not found. */
318 const struct tdesc_feature *
319 tdesc_find_feature (const struct target_desc *target_desc,
320 const char *name)
322 int ix;
323 struct tdesc_feature *feature;
325 for (ix = 0;
326 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
327 ix++)
328 if (strcmp (feature->name, name) == 0)
329 return feature;
331 return NULL;
334 /* Return the name of FEATURE. */
336 const char *
337 tdesc_feature_name (const struct tdesc_feature *feature)
339 return feature->name;
342 /* Predefined types. Note that none of these types depend on the
343 current architecture; some of the builtin_type_foo variables are
344 swapped based on the architecture. */
345 static struct
347 const char *name;
348 struct type **type;
349 } tdesc_predefined_types[] =
351 { "int8", &builtin_type_int8 },
352 { "int16", &builtin_type_int16 },
353 { "int32", &builtin_type_int32 },
354 { "int64", &builtin_type_int64 },
355 { "int128", &builtin_type_int128 },
356 { "uint8", &builtin_type_uint8 },
357 { "uint16", &builtin_type_uint16 },
358 { "uint32", &builtin_type_uint32 },
359 { "uint64", &builtin_type_uint64 },
360 { "uint128", &builtin_type_uint128 },
361 { "ieee_single", &builtin_type_ieee_single },
362 { "ieee_double", &builtin_type_ieee_double },
363 { "arm_fpa_ext", &builtin_type_arm_ext }
366 /* Return the type associated with ID in the context of FEATURE, or
367 NULL if none. */
369 struct type *
370 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
372 int ix;
373 struct type *gdb_type;
375 /* First try target-defined types. */
376 for (ix = 0; VEC_iterate (type_p, feature->types, ix, gdb_type); ix++)
377 if (strcmp (TYPE_NAME (gdb_type), id) == 0)
378 return gdb_type;
380 /* Next try the predefined types. */
381 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
382 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
383 return *tdesc_predefined_types[ix].type;
385 return NULL;
389 /* Support for registers from target descriptions. */
391 /* Construct the per-gdbarch data. */
393 static void *
394 tdesc_data_init (struct obstack *obstack)
396 struct tdesc_arch_data *data;
398 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
399 return data;
402 /* Similar, but for the temporary copy used during architecture
403 initialization. */
405 struct tdesc_arch_data *
406 tdesc_data_alloc (void)
408 return XZALLOC (struct tdesc_arch_data);
411 /* Free something allocated by tdesc_data_alloc, if it is not going
412 to be used (for instance if it was unsuitable for the
413 architecture). */
415 void
416 tdesc_data_cleanup (void *data_untyped)
418 struct tdesc_arch_data *data = data_untyped;
420 VEC_free (tdesc_reg_p, data->registers);
421 xfree (data);
424 /* Search FEATURE for a register named NAME. */
426 static struct tdesc_reg *
427 tdesc_find_register_early (const struct tdesc_feature *feature,
428 const char *name)
430 int ixr;
431 struct tdesc_reg *reg;
433 for (ixr = 0;
434 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
435 ixr++)
436 if (strcasecmp (reg->name, name) == 0)
437 return reg;
439 return NULL;
442 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
445 tdesc_numbered_register (const struct tdesc_feature *feature,
446 struct tdesc_arch_data *data,
447 int regno, const char *name)
449 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
451 if (reg == NULL)
452 return 0;
454 /* Make sure the vector includes a REGNO'th element. */
455 while (regno >= VEC_length (tdesc_reg_p, data->registers))
456 VEC_safe_push (tdesc_reg_p, data->registers, NULL);
457 VEC_replace (tdesc_reg_p, data->registers, regno, reg);
458 return 1;
461 /* Search FEATURE for a register whose name is in NAMES and assign
462 REGNO to it. */
465 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
466 struct tdesc_arch_data *data,
467 int regno, const char *const names[])
469 int i;
471 for (i = 0; names[i] != NULL; i++)
472 if (tdesc_numbered_register (feature, data, regno, names[i]))
473 return 1;
475 return 0;
478 /* Search FEATURE for a register named NAME, and return its size in
479 bits. The register must exist. */
482 tdesc_register_size (const struct tdesc_feature *feature,
483 const char *name)
485 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
487 gdb_assert (reg != NULL);
488 return reg->bitsize;
491 /* Look up a register by its GDB internal register number. */
493 static struct tdesc_reg *
494 tdesc_find_register (struct gdbarch *gdbarch, int regno)
496 struct tdesc_reg *reg;
497 struct tdesc_arch_data *data;
499 data = gdbarch_data (gdbarch, tdesc_data);
500 if (regno < VEC_length (tdesc_reg_p, data->registers))
501 return VEC_index (tdesc_reg_p, data->registers, regno);
502 else
503 return NULL;
506 /* Return the name of register REGNO, from the target description or
507 from an architecture-provided pseudo_register_name method. */
509 const char *
510 tdesc_register_name (struct gdbarch *gdbarch, int regno)
512 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
513 int num_regs = gdbarch_num_regs (gdbarch);
514 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
516 if (reg != NULL)
517 return reg->name;
519 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
521 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
522 gdb_assert (data->pseudo_register_name != NULL);
523 return data->pseudo_register_name (gdbarch, regno);
526 return "";
529 static struct type *
530 tdesc_register_type (struct gdbarch *gdbarch, int regno)
532 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
533 int num_regs = gdbarch_num_regs (gdbarch);
534 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
536 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
538 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
539 gdb_assert (data->pseudo_register_type != NULL);
540 return data->pseudo_register_type (gdbarch, regno);
543 if (reg == NULL)
544 /* Return "int0_t", since "void" has a misleading size of one. */
545 return builtin_type_int0;
547 /* First check for a predefined or target defined type. */
548 if (reg->gdb_type)
549 return reg->gdb_type;
551 /* Next try size-sensitive type shortcuts. */
552 if (strcmp (reg->type, "float") == 0)
554 if (reg->bitsize == gdbarch_float_bit (gdbarch))
555 return builtin_type (gdbarch)->builtin_float;
556 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
557 return builtin_type (gdbarch)->builtin_double;
558 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
559 return builtin_type (gdbarch)->builtin_long_double;
561 else if (strcmp (reg->type, "int") == 0)
563 if (reg->bitsize == gdbarch_long_bit (gdbarch))
564 return builtin_type (gdbarch)->builtin_long;
565 else if (reg->bitsize == TARGET_CHAR_BIT)
566 return builtin_type (gdbarch)->builtin_char;
567 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
568 return builtin_type (gdbarch)->builtin_short;
569 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
570 return builtin_type (gdbarch)->builtin_int;
571 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
572 return builtin_type (gdbarch)->builtin_long_long;
573 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
574 /* A bit desperate by this point... */
575 return builtin_type (gdbarch)->builtin_data_ptr;
577 else if (strcmp (reg->type, "code_ptr") == 0)
578 return builtin_type (gdbarch)->builtin_func_ptr;
579 else if (strcmp (reg->type, "data_ptr") == 0)
580 return builtin_type (gdbarch)->builtin_data_ptr;
581 else
582 internal_error (__FILE__, __LINE__,
583 "Register \"%s\" has an unknown type \"%s\"",
584 reg->name, reg->type);
586 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
587 reg->name, reg->bitsize);
588 return builtin_type (gdbarch)->builtin_long;
591 static int
592 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
594 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
596 if (reg != NULL)
597 return reg->target_regnum;
598 else
599 return -1;
602 /* Check whether REGNUM is a member of REGGROUP. Registers from the
603 target description may be classified as general, float, or vector.
604 Unlike a gdbarch register_reggroup_p method, this function will
605 return -1 if it does not know; the caller should handle registers
606 with no specified group.
608 Arbitrary strings (other than "general", "float", and "vector")
609 from the description are not used; they cause the register to be
610 displayed in "info all-registers" but excluded from "info
611 registers" et al. The names of containing features are also not
612 used. This might be extended to display registers in some more
613 useful groupings.
615 The save-restore flag is also implemented here. */
618 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
619 struct reggroup *reggroup)
621 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
623 if (reg != NULL && reg->group != NULL)
625 int general_p = 0, float_p = 0, vector_p = 0;
627 if (strcmp (reg->group, "general") == 0)
628 general_p = 1;
629 else if (strcmp (reg->group, "float") == 0)
630 float_p = 1;
631 else if (strcmp (reg->group, "vector") == 0)
632 vector_p = 1;
634 if (reggroup == float_reggroup)
635 return float_p;
637 if (reggroup == vector_reggroup)
638 return vector_p;
640 if (reggroup == general_reggroup)
641 return general_p;
644 if (reg != NULL
645 && (reggroup == save_reggroup || reggroup == restore_reggroup))
646 return reg->save_restore;
648 return -1;
651 /* Check whether REGNUM is a member of REGGROUP. Registers with no
652 group specified go to the default reggroup function and are handled
653 by type. */
655 static int
656 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
657 struct reggroup *reggroup)
659 int num_regs = gdbarch_num_regs (gdbarch);
660 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
661 int ret;
663 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
665 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
666 gdb_assert (data->pseudo_register_reggroup_p != NULL);
667 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
670 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
671 if (ret != -1)
672 return ret;
674 return default_register_reggroup_p (gdbarch, regno, reggroup);
677 /* Record architecture-specific functions to call for pseudo-register
678 support. */
680 void
681 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
682 gdbarch_register_name_ftype *pseudo_name)
684 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
686 data->pseudo_register_name = pseudo_name;
689 void
690 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
691 gdbarch_register_type_ftype *pseudo_type)
693 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
695 data->pseudo_register_type = pseudo_type;
698 void
699 set_tdesc_pseudo_register_reggroup_p
700 (struct gdbarch *gdbarch,
701 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
703 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
705 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
708 /* Update GDBARCH to use the target description for registers. */
710 void
711 tdesc_use_registers (struct gdbarch *gdbarch,
712 const struct target_desc *target_desc,
713 struct tdesc_arch_data *early_data)
715 int num_regs = gdbarch_num_regs (gdbarch);
716 int i, ixf, ixr;
717 struct tdesc_feature *feature;
718 struct tdesc_reg *reg;
719 struct tdesc_arch_data *data;
720 htab_t reg_hash;
722 /* We can't use the description for registers if it doesn't describe
723 any. This function should only be called after validating
724 registers, so the caller should know that registers are
725 included. */
726 gdb_assert (tdesc_has_registers (target_desc));
728 data = gdbarch_data (gdbarch, tdesc_data);
729 data->registers = early_data->registers;
730 xfree (early_data);
732 /* Build up a set of all registers, so that we can assign register
733 numbers where needed. The hash table expands as necessary, so
734 the initial size is arbitrary. */
735 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
736 for (ixf = 0;
737 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
738 ixf++)
739 for (ixr = 0;
740 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
741 ixr++)
743 void **slot = htab_find_slot (reg_hash, reg, INSERT);
745 *slot = reg;
748 /* Remove any registers which were assigned numbers by the
749 architecture. */
750 for (ixr = 0; VEC_iterate (tdesc_reg_p, data->registers, ixr, reg); ixr++)
751 if (reg)
752 htab_remove_elt (reg_hash, reg);
754 /* Assign numbers to the remaining registers and add them to the
755 list of registers. The new numbers are always above gdbarch_num_regs.
756 Iterate over the features, not the hash table, so that the order
757 matches that in the target description. */
759 gdb_assert (VEC_length (tdesc_reg_p, data->registers) <= num_regs);
760 while (VEC_length (tdesc_reg_p, data->registers) < num_regs)
761 VEC_safe_push (tdesc_reg_p, data->registers, NULL);
762 for (ixf = 0;
763 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
764 ixf++)
765 for (ixr = 0;
766 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
767 ixr++)
768 if (htab_find (reg_hash, reg) != NULL)
770 VEC_safe_push (tdesc_reg_p, data->registers, reg);
771 num_regs++;
774 htab_delete (reg_hash);
776 /* Update the architecture. */
777 set_gdbarch_num_regs (gdbarch, num_regs);
778 set_gdbarch_register_name (gdbarch, tdesc_register_name);
779 set_gdbarch_register_type (gdbarch, tdesc_register_type);
780 set_gdbarch_remote_register_number (gdbarch,
781 tdesc_remote_register_number);
782 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
786 /* Methods for constructing a target description. */
788 static void
789 tdesc_free_reg (struct tdesc_reg *reg)
791 xfree (reg->name);
792 xfree (reg->type);
793 xfree (reg->group);
794 xfree (reg);
797 void
798 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
799 int regnum, int save_restore, const char *group,
800 int bitsize, const char *type)
802 struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
804 reg->name = xstrdup (name);
805 reg->target_regnum = regnum;
806 reg->save_restore = save_restore;
807 reg->group = group ? xstrdup (group) : NULL;
808 reg->bitsize = bitsize;
809 reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
811 /* If the register's type is target-defined, look it up now. We may not
812 have easy access to the containing feature when we want it later. */
813 reg->gdb_type = tdesc_named_type (feature, reg->type);
815 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
818 static void
819 tdesc_free_feature (struct tdesc_feature *feature)
821 struct tdesc_reg *reg;
822 int ix;
824 for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
825 tdesc_free_reg (reg);
826 VEC_free (tdesc_reg_p, feature->registers);
828 /* There is no easy way to free xmalloc-allocated types, nor is
829 there a way to allocate types on an obstack not associated with
830 an objfile. Therefore we never free types. Since we only ever
831 parse an identical XML document once, this memory leak is mostly
832 contained. */
833 VEC_free (type_p, feature->types);
835 xfree (feature->name);
836 xfree (feature);
839 struct tdesc_feature *
840 tdesc_create_feature (struct target_desc *tdesc, const char *name)
842 struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
844 new_feature->name = xstrdup (name);
846 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
847 return new_feature;
850 void
851 tdesc_record_type (struct tdesc_feature *feature, struct type *type)
853 /* The type's ID should be used as its TYPE_NAME. */
854 gdb_assert (TYPE_NAME (type) != NULL);
856 VEC_safe_push (type_p, feature->types, type);
859 struct target_desc *
860 allocate_target_description (void)
862 return XZALLOC (struct target_desc);
865 static void
866 free_target_description (void *arg)
868 struct target_desc *target_desc = arg;
869 struct tdesc_feature *feature;
870 struct property *prop;
871 int ix;
873 for (ix = 0;
874 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
875 ix++)
876 tdesc_free_feature (feature);
877 VEC_free (tdesc_feature_p, target_desc->features);
879 for (ix = 0;
880 VEC_iterate (property_s, target_desc->properties, ix, prop);
881 ix++)
883 xfree (prop->key);
884 xfree (prop->value);
886 VEC_free (property_s, target_desc->properties);
888 xfree (target_desc);
891 struct cleanup *
892 make_cleanup_free_target_description (struct target_desc *target_desc)
894 return make_cleanup (free_target_description, target_desc);
897 void
898 set_tdesc_property (struct target_desc *target_desc,
899 const char *key, const char *value)
901 struct property *prop, new_prop;
902 int ix;
904 gdb_assert (key != NULL && value != NULL);
906 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
907 ix++)
908 if (strcmp (prop->key, key) == 0)
909 internal_error (__FILE__, __LINE__,
910 _("Attempted to add duplicate property \"%s\""), key);
912 new_prop.key = xstrdup (key);
913 new_prop.value = xstrdup (value);
914 VEC_safe_push (property_s, target_desc->properties, &new_prop);
917 void
918 set_tdesc_architecture (struct target_desc *target_desc,
919 const struct bfd_arch_info *arch)
921 target_desc->arch = arch;
925 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
926 static struct cmd_list_element *tdesc_unset_cmdlist;
928 /* Helper functions for the CLI commands. */
930 static void
931 set_tdesc_cmd (char *args, int from_tty)
933 help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
936 static void
937 show_tdesc_cmd (char *args, int from_tty)
939 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
942 static void
943 unset_tdesc_cmd (char *args, int from_tty)
945 help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
948 static void
949 set_tdesc_filename_cmd (char *args, int from_tty,
950 struct cmd_list_element *c)
952 target_clear_description ();
953 target_find_description ();
956 static void
957 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
958 struct cmd_list_element *c,
959 const char *value)
961 if (value != NULL && *value != '\0')
962 printf_filtered (_("\
963 The target description will be read from \"%s\".\n"),
964 value);
965 else
966 printf_filtered (_("\
967 The target description will be read from the target.\n"));
970 static void
971 unset_tdesc_filename_cmd (char *args, int from_tty)
973 xfree (target_description_filename);
974 target_description_filename = NULL;
975 target_clear_description ();
976 target_find_description ();
979 static const char *
980 tdesc_type_id (struct type *type)
982 int ix;
984 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
985 if (TYPE_MAIN_TYPE (*tdesc_predefined_types[ix].type)
986 == TYPE_MAIN_TYPE (type))
987 return tdesc_predefined_types[ix].name;
989 return TYPE_NAME (type);
992 static void
993 maint_print_c_tdesc_cmd (char *args, int from_tty)
995 const struct target_desc *tdesc;
996 const char *filename, *inp;
997 char *function, *outp;
998 struct property *prop;
999 struct tdesc_feature *feature;
1000 struct tdesc_reg *reg;
1001 struct type *type;
1002 int ix, ix2, ix3;
1004 /* Use the global target-supplied description, not the current
1005 architecture's. This lets a GDB for one architecture generate C
1006 for another architecture's description, even though the gdbarch
1007 initialization code will reject the new description. */
1008 tdesc = current_target_desc;
1009 if (tdesc == NULL)
1010 error (_("There is no target description to print."));
1012 if (target_description_filename == NULL)
1013 error (_("The current target description did not come from an XML file."));
1015 filename = lbasename (target_description_filename);
1016 function = alloca (strlen (filename) + 1);
1017 for (inp = filename, outp = function; *inp != '\0'; inp++)
1018 if (*inp == '.')
1019 break;
1020 else if (*inp == '-')
1021 *outp++ = '_';
1022 else
1023 *outp++ = *inp;
1024 *outp = '\0';
1026 /* Standard boilerplate. */
1027 printf_unfiltered ("/* THIS FILE IS GENERATED. Original: %s */\n\n",
1028 filename);
1029 printf_unfiltered ("#include \"defs.h\"\n");
1030 printf_unfiltered ("#include \"gdbtypes.h\"\n");
1031 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1032 printf_unfiltered ("\n");
1034 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1035 printf_unfiltered ("static void\n");
1036 printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1037 printf_unfiltered ("{\n");
1038 printf_unfiltered
1039 (" struct target_desc *result = allocate_target_description ();\n");
1040 printf_unfiltered (" struct tdesc_feature *feature;\n");
1041 printf_unfiltered (" struct type *field_type, *type;\n");
1042 printf_unfiltered ("\n");
1044 if (tdesc_architecture (tdesc) != NULL)
1046 printf_unfiltered
1047 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1048 tdesc_architecture (tdesc)->printable_name);
1049 printf_unfiltered ("\n");
1052 for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1053 ix++)
1055 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1056 prop->key, prop->value);
1059 for (ix = 0;
1060 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1061 ix++)
1063 printf_unfiltered (" feature = tdesc_create_feature (result, \"%s\");\n",
1064 feature->name);
1066 for (ix2 = 0;
1067 VEC_iterate (type_p, feature->types, ix2, type);
1068 ix2++)
1070 switch (TYPE_CODE (type))
1072 case TYPE_CODE_ARRAY:
1073 printf_unfiltered
1074 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1075 tdesc_type_id (TYPE_TARGET_TYPE (type)));
1076 printf_unfiltered
1077 (" type = init_vector_type (field_type, %d);\n",
1078 TYPE_LENGTH (type) / TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
1079 printf_unfiltered
1080 (" TYPE_NAME (type) = xstrdup (\"%s\");\n", TYPE_NAME (type));
1081 break;
1082 case TYPE_CODE_UNION:
1083 printf_unfiltered
1084 (" type = init_composite_type (NULL, TYPE_CODE_UNION);\n");
1085 printf_unfiltered
1086 (" TYPE_NAME (type) = xstrdup (\"%s\");\n", TYPE_NAME (type));
1087 for (ix3 = 0; ix3 < TYPE_NFIELDS (type); ix3++)
1089 printf_unfiltered
1090 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1091 tdesc_type_id (TYPE_FIELD_TYPE (type, ix3)));
1092 printf_unfiltered
1093 (" append_composite_type_field (type, "
1094 "xstrdup (\"%s\"), field_type);\n",
1095 TYPE_FIELD_NAME (type, ix3));
1097 if (TYPE_VECTOR (type))
1098 printf_unfiltered
1099 (" TYPE_VECTOR (type) = 1;\n");
1100 break;
1101 default:
1102 error (_("C output is not supported type \"%s\"."), TYPE_NAME (type));
1104 printf_unfiltered (" tdesc_record_type (feature, type);\n");
1105 printf_unfiltered ("\n");
1108 for (ix2 = 0;
1109 VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1110 ix2++)
1112 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1113 reg->name, reg->target_regnum, reg->save_restore);
1114 if (reg->group)
1115 printf_unfiltered ("\"%s\", ", reg->group);
1116 else
1117 printf_unfiltered ("NULL, ");
1118 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1121 printf_unfiltered ("\n");
1124 printf_unfiltered (" tdesc_%s = result;\n", function);
1125 printf_unfiltered ("}\n");
1128 /* Provide a prototype to silence -Wmissing-prototypes. */
1129 extern initialize_file_ftype _initialize_target_descriptions;
1131 void
1132 _initialize_target_descriptions (void)
1134 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1136 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1137 Set target description specific variables."),
1138 &tdesc_set_cmdlist, "set tdesc ",
1139 0 /* allow-unknown */, &setlist);
1140 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1141 Show target description specific variables."),
1142 &tdesc_show_cmdlist, "show tdesc ",
1143 0 /* allow-unknown */, &showlist);
1144 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1145 Unset target description specific variables."),
1146 &tdesc_unset_cmdlist, "unset tdesc ",
1147 0 /* allow-unknown */, &unsetlist);
1149 add_setshow_filename_cmd ("filename", class_obscure,
1150 &target_description_filename,
1151 _("\
1152 Set the file to read for an XML target description"), _("\
1153 Show the file to read for an XML target description"), _("\
1154 When set, GDB will read the target description from a local\n\
1155 file instead of querying the remote target."),
1156 set_tdesc_filename_cmd,
1157 show_tdesc_filename_cmd,
1158 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1160 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1161 Unset the file to read for an XML target description. When unset,\n\
1162 GDB will read the description from the target."),
1163 &tdesc_unset_cmdlist);
1165 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1166 Print the current target description as a C source file."),
1167 &maintenanceprintlist);