Switch the license of all .c files to GPLv3.
[external/binutils.git] / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4
5    Contributed by CodeSourcery.
6
7    This file is part of GDB.
8
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.
13
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.
18
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/>.  */
21
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
33 #include "gdb_assert.h"
34 #include "gdb_obstack.h"
35 #include "hashtab.h"
36
37 /* Types.  */
38
39 typedef struct property
40 {
41   char *key;
42   char *value;
43 } property_s;
44 DEF_VEC_O(property_s);
45
46 /* An individual register from a target description.  */
47
48 typedef struct tdesc_reg
49 {
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;
54
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;
59
60   /* If this flag is set, GDB should save and restore this register
61      around calls to an inferior function.  */
62   int save_restore;
63
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;
72
73   /* The size of the register, in bits.  */
74   int bitsize;
75
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;
80
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);
85
86 /* A named type from a target description.  */
87 typedef struct type *type_p;
88 DEF_VEC_P(type_p);
89
90 /* A feature from a target description.  Each feature is a collection
91    of other elements, e.g. registers and types.  */
92
93 typedef struct tdesc_feature
94 {
95   /* The name of this feature.  It may be recognized by the architecture
96      support code.  */
97   char *name;
98
99   /* The registers associated with this feature.  */
100   VEC(tdesc_reg_p) *registers;
101
102   /* The types associated with this feature.  */
103   VEC(type_p) *types;
104 } *tdesc_feature_p;
105 DEF_VEC_P(tdesc_feature_p);
106
107 /* A target description.  */
108
109 struct target_desc
110 {
111   /* The architecture reported by the target, if any.  */
112   const struct bfd_arch_info *arch;
113
114   /* Any architecture-specific properties specified by the target.  */
115   VEC(property_s) *properties;
116
117   /* The features associated with this target.  */
118   VEC(tdesc_feature_p) *features;
119 };
120
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.  */
124
125 struct tdesc_arch_data
126 {
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;
134
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;
140 };
141
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.  */
145
146 /* A flag indicating that a description has already been fetched from
147    the current target, so it should not be queried again.  */
148
149 static int target_desc_fetched;
150
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.  */
156
157 static const struct target_desc *current_target_desc;
158
159 /* Other global variables.  */
160
161 /* The filename to read a target description from.  */
162
163 static char *target_description_filename;
164
165 /* A handle for architecture-specific data associated with the
166    target description (see struct tdesc_arch_data).  */
167
168 static struct gdbarch_data *tdesc_data;
169
170 /* Fetch the current target's description, and switch the current
171    architecture to one which incorporates that description.  */
172
173 void
174 target_find_description (void)
175 {
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;
182
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 (current_gdbarch) == NULL);
187
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);
195
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);
200
201   /* If that failed try a target-specific hook.  */
202   if (current_target_desc == NULL)
203     current_target_desc = target_read_description (&current_target);
204
205   /* If a non-NULL description was returned, then update the current
206      architecture.  */
207   if (current_target_desc)
208     {
209       struct gdbarch_info info;
210
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
216         {
217           struct tdesc_arch_data *data;
218
219           data = gdbarch_data (current_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"));
224         }
225     }
226
227   /* Now that we know this description is usable, record that we
228      fetched it.  */
229   target_desc_fetched = 1;
230 }
231
232 /* Discard any description fetched from the current target, and switch
233    the current architecture to one with no target description.  */
234
235 void
236 target_clear_description (void)
237 {
238   struct gdbarch_info info;
239
240   if (!target_desc_fetched)
241     return;
242
243   target_desc_fetched = 0;
244   current_target_desc = NULL;
245
246   gdbarch_info_init (&info);
247   if (!gdbarch_update_p (info))
248     internal_error (__FILE__, __LINE__,
249                     _("Could not remove target-supplied description"));
250 }
251
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.  */
255
256 const struct target_desc *
257 target_current_description (void)
258 {
259   if (target_desc_fetched)
260     return current_target_desc;
261
262   return NULL;
263 }
264 \f
265
266 /* Direct accessors for target descriptions.  */
267
268 /* Return the string value of a property named KEY, or NULL if the
269    property was not specified.  */
270
271 const char *
272 tdesc_property (const struct target_desc *target_desc, const char *key)
273 {
274   struct property *prop;
275   int ix;
276
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;
281
282   return NULL;
283 }
284
285 /* Return the BFD architecture associated with this target
286    description, or NULL if no architecture was specified.  */
287
288 const struct bfd_arch_info *
289 tdesc_architecture (const struct target_desc *target_desc)
290 {
291   return target_desc->arch;
292 }
293 \f
294
295 /* Return 1 if this target description includes any registers.  */
296
297 int
298 tdesc_has_registers (const struct target_desc *target_desc)
299 {
300   int ix;
301   struct tdesc_feature *feature;
302
303   if (target_desc == NULL)
304     return 0;
305
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;
311
312   return 0;
313 }
314
315 /* Return the feature with the given name, if present, or NULL if
316    the named feature is not found.  */
317
318 const struct tdesc_feature *
319 tdesc_find_feature (const struct target_desc *target_desc,
320                     const char *name)
321 {
322   int ix;
323   struct tdesc_feature *feature;
324
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;
330
331   return NULL;
332 }
333
334 /* Return the name of FEATURE.  */
335
336 const char *
337 tdesc_feature_name (const struct tdesc_feature *feature)
338 {
339   return feature->name;
340 }
341
342 /* Return the type associated with ID in the context of FEATURE, or
343    NULL if none.  */
344
345 struct type *
346 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
347 {
348   int ix;
349   struct type *gdb_type;
350
351   /* First try target-defined types.  */
352   for (ix = 0; VEC_iterate (type_p, feature->types, ix, gdb_type); ix++)
353     if (strcmp (TYPE_NAME (gdb_type), id) == 0)
354       return gdb_type;
355
356   /* Next try some predefined types.  Note that none of these types
357      depend on the current architecture; some of the builtin_type_foo
358      variables are swapped based on the architecture.  */
359   if (strcmp (id, "int8") == 0)
360     return builtin_type_int8;
361
362   if (strcmp (id, "int16") == 0)
363     return builtin_type_int16;
364
365   if (strcmp (id, "int32") == 0)
366     return builtin_type_int32;
367
368   if (strcmp (id, "int64") == 0)
369     return builtin_type_int64;
370
371   if (strcmp (id, "uint8") == 0)
372     return builtin_type_uint8;
373
374   if (strcmp (id, "uint16") == 0)
375     return builtin_type_uint16;
376
377   if (strcmp (id, "uint32") == 0)
378     return builtin_type_uint32;
379
380   if (strcmp (id, "uint64") == 0)
381     return builtin_type_uint64;
382
383   if (strcmp (id, "ieee_single") == 0)
384     return builtin_type_ieee_single;
385
386   if (strcmp (id, "ieee_double") == 0)
387     return builtin_type_ieee_double;
388
389   if (strcmp (id, "arm_fpa_ext") == 0)
390     return builtin_type_arm_ext;
391
392   return NULL;
393 }
394 \f
395
396 /* Support for registers from target descriptions.  */
397
398 /* Construct the per-gdbarch data.  */
399
400 static void *
401 tdesc_data_init (struct obstack *obstack)
402 {
403   struct tdesc_arch_data *data;
404
405   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
406   return data;
407 }
408
409 /* Similar, but for the temporary copy used during architecture
410    initialization.  */
411
412 struct tdesc_arch_data *
413 tdesc_data_alloc (void)
414 {
415   return XZALLOC (struct tdesc_arch_data);
416 }
417
418 /* Free something allocated by tdesc_data_alloc, if it is not going
419    to be used (for instance if it was unsuitable for the
420    architecture).  */
421
422 void
423 tdesc_data_cleanup (void *data_untyped)
424 {
425   struct tdesc_arch_data *data = data_untyped;
426
427   VEC_free (tdesc_reg_p, data->registers);
428   xfree (data);
429 }
430
431 /* Search FEATURE for a register named NAME.  */
432
433 int
434 tdesc_numbered_register (const struct tdesc_feature *feature,
435                          struct tdesc_arch_data *data,
436                          int regno, const char *name)
437 {
438   int ixr;
439   struct tdesc_reg *reg;
440
441   for (ixr = 0;
442        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
443        ixr++)
444     if (strcasecmp (reg->name, name) == 0)
445       {
446         /* Make sure the vector includes a REGNO'th element.  */
447         while (regno >= VEC_length (tdesc_reg_p, data->registers))
448           VEC_safe_push (tdesc_reg_p, data->registers, NULL);
449         VEC_replace (tdesc_reg_p, data->registers, regno, reg);
450         return 1;
451       }
452
453   return 0;
454 }
455
456 /* Search FEATURE for a register whose name is in NAMES.  */
457
458 int
459 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
460                                  struct tdesc_arch_data *data,
461                                  int regno, const char *const names[])
462 {
463   int i;
464
465   for (i = 0; names[i] != NULL; i++)
466     if (tdesc_numbered_register (feature, data, regno, names[i]))
467       return 1;
468
469   return 0;
470 }
471
472 /* Look up a register by its GDB internal register number.  */
473
474 static struct tdesc_reg *
475 tdesc_find_register (struct gdbarch *gdbarch, int regno)
476 {
477   struct tdesc_reg *reg;
478   struct tdesc_arch_data *data;
479
480   data = gdbarch_data (gdbarch, tdesc_data);
481   if (regno < VEC_length (tdesc_reg_p, data->registers))
482     return VEC_index (tdesc_reg_p, data->registers, regno);
483   else
484     return NULL;
485 }
486
487 /* Return the name of register REGNO, from the target description or
488    from an architecture-provided pseudo_register_name method.  */
489
490 const char *
491 tdesc_register_name (int regno)
492 {
493   struct tdesc_reg *reg = tdesc_find_register (current_gdbarch, regno);
494   int num_regs = gdbarch_num_regs (current_gdbarch);
495   int num_pseudo_regs = gdbarch_num_pseudo_regs (current_gdbarch);
496
497   if (reg != NULL)
498     return reg->name;
499
500   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
501     {
502       struct tdesc_arch_data *data = gdbarch_data (current_gdbarch,
503                                                    tdesc_data);
504       gdb_assert (data->pseudo_register_name != NULL);
505       return data->pseudo_register_name (regno);
506     }
507
508   return "";
509 }
510
511 static struct type *
512 tdesc_register_type (struct gdbarch *gdbarch, int regno)
513 {
514   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
515   int num_regs = gdbarch_num_regs (gdbarch);
516   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
517
518   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
519     {
520       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
521       gdb_assert (data->pseudo_register_type != NULL);
522       return data->pseudo_register_type (gdbarch, regno);
523     }
524
525   if (reg == NULL)
526     /* Return "int0_t", since "void" has a misleading size of one.  */
527     return builtin_type_int0;
528
529   /* First check for a predefined or target defined type.  */
530   if (reg->gdb_type)
531     return reg->gdb_type;
532
533   /* Next try size-sensitive type shortcuts.  */
534   if (strcmp (reg->type, "float") == 0)
535     {
536       if (reg->bitsize == gdbarch_float_bit (gdbarch))
537         return builtin_type_float;
538       else if (reg->bitsize == gdbarch_double_bit (gdbarch))
539         return builtin_type_double;
540       else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
541         return builtin_type_long_double;
542     }
543   else if (strcmp (reg->type, "int") == 0)
544     {
545       if (reg->bitsize == gdbarch_long_bit (gdbarch))
546         return builtin_type_long;
547       else if (reg->bitsize == TARGET_CHAR_BIT)
548         return builtin_type_char;
549       else if (reg->bitsize == gdbarch_short_bit (gdbarch))
550         return builtin_type_short;
551       else if (reg->bitsize == gdbarch_int_bit (gdbarch))
552         return builtin_type_int;
553       else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
554         return builtin_type_long_long;
555       else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
556         /* A bit desperate by this point... */
557         return builtin_type_void_data_ptr;
558     }
559   else if (strcmp (reg->type, "code_ptr") == 0)
560     return builtin_type_void_func_ptr;
561   else if (strcmp (reg->type, "data_ptr") == 0)
562     return builtin_type_void_data_ptr;
563   else
564     internal_error (__FILE__, __LINE__,
565                     "Register \"%s\" has an unknown type \"%s\"",
566                     reg->name, reg->type);
567
568   warning (_("Register \"%s\" has an unsupported size (%d bits)"),
569            reg->name, reg->bitsize);
570   return builtin_type_long;
571 }
572
573 static int
574 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
575 {
576   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
577
578   if (reg != NULL)
579     return reg->target_regnum;
580   else
581     return -1;
582 }
583
584 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
585    target description may be classified as general, float, or vector.
586    Unlike a gdbarch register_reggroup_p method, this function will
587    return -1 if it does not know; the caller should handle registers
588    with no specified group.
589
590    Arbitrary strings (other than "general", "float", and "vector")
591    from the description are not used; they cause the register to be
592    displayed in "info all-registers" but excluded from "info
593    registers" et al.  The names of containing features are also not
594    used.  This might be extended to display registers in some more
595    useful groupings.
596
597    The save-restore flag is also implemented here.  */
598
599 int
600 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
601                               struct reggroup *reggroup)
602 {
603   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
604
605   if (reg != NULL && reg->group != NULL)
606     {
607       int general_p = 0, float_p = 0, vector_p = 0;
608
609       if (strcmp (reg->group, "general") == 0)
610         general_p = 1;
611       else if (strcmp (reg->group, "float") == 0)
612         float_p = 1;
613       else if (strcmp (reg->group, "vector") == 0)
614         vector_p = 1;
615
616       if (reggroup == float_reggroup)
617         return float_p;
618
619       if (reggroup == vector_reggroup)
620         return vector_p;
621
622       if (reggroup == general_reggroup)
623         return general_p;
624     }
625
626   if (reg != NULL
627       && (reggroup == save_reggroup || reggroup == restore_reggroup))
628     return reg->save_restore;
629
630   return -1;
631 }
632
633 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
634    group specified go to the default reggroup function and are handled
635    by type.  */
636
637 static int
638 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
639                            struct reggroup *reggroup)
640 {
641   int num_regs = gdbarch_num_regs (gdbarch);
642   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
643   int ret;
644
645   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
646     {
647       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
648       gdb_assert (data->pseudo_register_reggroup_p != NULL);
649       return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
650     }
651
652   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
653   if (ret != -1)
654     return ret;
655
656   return default_register_reggroup_p (gdbarch, regno, reggroup);
657 }
658
659 /* Record architecture-specific functions to call for pseudo-register
660    support.  */
661
662 void
663 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
664                                 gdbarch_register_name_ftype *pseudo_name)
665 {
666   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
667
668   data->pseudo_register_name = pseudo_name;
669 }
670
671 void
672 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
673                                 gdbarch_register_type_ftype *pseudo_type)
674 {
675   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
676
677   data->pseudo_register_type = pseudo_type;
678 }
679
680 void
681 set_tdesc_pseudo_register_reggroup_p
682   (struct gdbarch *gdbarch,
683    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
684 {
685   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
686
687   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
688 }
689
690 /* Update GDBARCH to use the target description for registers.  */
691
692 void
693 tdesc_use_registers (struct gdbarch *gdbarch,
694                      struct tdesc_arch_data *early_data)
695 {
696   int num_regs = gdbarch_num_regs (gdbarch);
697   int i, ixf, ixr;
698   const struct target_desc *target_desc;
699   struct tdesc_feature *feature;
700   struct tdesc_reg *reg;
701   struct tdesc_arch_data *data;
702   htab_t reg_hash;
703
704   target_desc = gdbarch_target_desc (gdbarch);
705
706   /* We can't use the description for registers if it doesn't describe
707      any.  This function should only be called after validating
708      registers, so the caller should know that registers are
709      included.  */
710   gdb_assert (tdesc_has_registers (target_desc));
711
712   data = gdbarch_data (gdbarch, tdesc_data);
713   data->registers = early_data->registers;
714   xfree (early_data);
715
716   /* Build up a set of all registers, so that we can assign register
717      numbers where needed.  The hash table expands as necessary, so
718      the initial size is arbitrary.  */
719   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
720   for (ixf = 0;
721        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
722        ixf++)
723     for (ixr = 0;
724          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
725          ixr++)
726       {
727         void **slot = htab_find_slot (reg_hash, reg, INSERT);
728
729         *slot = reg;
730       }
731
732   /* Remove any registers which were assigned numbers by the
733      architecture.  */
734   for (ixr = 0; VEC_iterate (tdesc_reg_p, data->registers, ixr, reg); ixr++)
735     if (reg)
736       htab_remove_elt (reg_hash, reg);
737
738   /* Assign numbers to the remaining registers and add them to the
739      list of registers.  The new numbers are always above gdbarch_num_regs.
740      Iterate over the features, not the hash table, so that the order
741      matches that in the target description.  */
742
743   gdb_assert (VEC_length (tdesc_reg_p, data->registers) <= num_regs);
744   while (VEC_length (tdesc_reg_p, data->registers) < num_regs)
745     VEC_safe_push (tdesc_reg_p, data->registers, NULL);
746   for (ixf = 0;
747        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
748        ixf++)
749     for (ixr = 0;
750          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
751          ixr++)
752       if (htab_find (reg_hash, reg) != NULL)
753         {
754           VEC_safe_push (tdesc_reg_p, data->registers, reg);
755           num_regs++;
756         }
757
758   htab_delete (reg_hash);
759
760   /* Update the architecture.  */
761   set_gdbarch_num_regs (gdbarch, num_regs);
762   set_gdbarch_register_name (gdbarch, tdesc_register_name);
763   set_gdbarch_register_type (gdbarch, tdesc_register_type);
764   set_gdbarch_remote_register_number (gdbarch,
765                                       tdesc_remote_register_number);
766   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
767 }
768 \f
769
770 /* Methods for constructing a target description.  */
771
772 static void
773 tdesc_free_reg (struct tdesc_reg *reg)
774 {
775   xfree (reg->name);
776   xfree (reg->type);
777   xfree (reg->group);
778   xfree (reg);
779 }
780
781 void
782 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
783                   int regnum, int save_restore, const char *group,
784                   int bitsize, const char *type)
785 {
786   struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
787
788   reg->name = xstrdup (name);
789   reg->target_regnum = regnum;
790   reg->save_restore = save_restore;
791   reg->group = group ? xstrdup (group) : NULL;
792   reg->bitsize = bitsize;
793   reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
794
795   /* If the register's type is target-defined, look it up now.  We may not
796      have easy access to the containing feature when we want it later.  */
797   reg->gdb_type = tdesc_named_type (feature, reg->type);
798
799   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
800 }
801
802 static void
803 tdesc_free_feature (struct tdesc_feature *feature)
804 {
805   struct tdesc_reg *reg;
806   int ix;
807
808   for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
809     tdesc_free_reg (reg);
810   VEC_free (tdesc_reg_p, feature->registers);
811
812   /* There is no easy way to free xmalloc-allocated types, nor is
813      there a way to allocate types on an obstack not associated with
814      an objfile.  Therefore we never free types.  Since we only ever
815      parse an identical XML document once, this memory leak is mostly
816      contained.  */
817   VEC_free (type_p, feature->types);
818
819   xfree (feature->name);
820   xfree (feature);
821 }
822
823 struct tdesc_feature *
824 tdesc_create_feature (struct target_desc *tdesc, const char *name)
825 {
826   struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
827
828   new_feature->name = xstrdup (name);
829
830   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
831   return new_feature;
832 }
833
834 void
835 tdesc_record_type (struct tdesc_feature *feature, struct type *type)
836 {
837   /* The type's ID should be used as its TYPE_NAME.  */
838   gdb_assert (TYPE_NAME (type) != NULL);
839
840   VEC_safe_push (type_p, feature->types, type);
841 }
842
843 struct target_desc *
844 allocate_target_description (void)
845 {
846   return XZALLOC (struct target_desc);
847 }
848
849 static void
850 free_target_description (void *arg)
851 {
852   struct target_desc *target_desc = arg;
853   struct tdesc_feature *feature;
854   struct property *prop;
855   int ix;
856
857   for (ix = 0;
858        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
859        ix++)
860     tdesc_free_feature (feature);
861   VEC_free (tdesc_feature_p, target_desc->features);
862
863   for (ix = 0;
864        VEC_iterate (property_s, target_desc->properties, ix, prop);
865        ix++)
866     {
867       xfree (prop->key);
868       xfree (prop->value);
869     }
870   VEC_free (property_s, target_desc->properties);
871
872   xfree (target_desc);
873 }
874
875 struct cleanup *
876 make_cleanup_free_target_description (struct target_desc *target_desc)
877 {
878   return make_cleanup (free_target_description, target_desc);
879 }
880
881 void
882 set_tdesc_property (struct target_desc *target_desc,
883                     const char *key, const char *value)
884 {
885   struct property *prop, new_prop;
886   int ix;
887
888   gdb_assert (key != NULL && value != NULL);
889
890   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
891        ix++)
892     if (strcmp (prop->key, key) == 0)
893       internal_error (__FILE__, __LINE__,
894                       _("Attempted to add duplicate property \"%s\""), key);
895
896   new_prop.key = xstrdup (key);
897   new_prop.value = xstrdup (value);
898   VEC_safe_push (property_s, target_desc->properties, &new_prop);
899 }
900
901 void
902 set_tdesc_architecture (struct target_desc *target_desc,
903                         const struct bfd_arch_info *arch)
904 {
905   target_desc->arch = arch;
906 }
907 \f
908
909 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
910 static struct cmd_list_element *tdesc_unset_cmdlist;
911
912 /* Helper functions for the CLI commands.  */
913
914 static void
915 set_tdesc_cmd (char *args, int from_tty)
916 {
917   help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
918 }
919
920 static void
921 show_tdesc_cmd (char *args, int from_tty)
922 {
923   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
924 }
925
926 static void
927 unset_tdesc_cmd (char *args, int from_tty)
928 {
929   help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
930 }
931
932 static void
933 set_tdesc_filename_cmd (char *args, int from_tty,
934                         struct cmd_list_element *c)
935 {
936   target_clear_description ();
937   target_find_description ();
938 }
939
940 static void
941 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
942                          struct cmd_list_element *c,
943                          const char *value)
944 {
945   if (value != NULL && *value != '\0')
946     printf_filtered (_("\
947 The target description will be read from \"%s\".\n"),
948                      value);
949   else
950     printf_filtered (_("\
951 The target description will be read from the target.\n"));
952 }
953
954 static void
955 unset_tdesc_filename_cmd (char *args, int from_tty)
956 {
957   xfree (target_description_filename);
958   target_description_filename = NULL;
959   target_clear_description ();
960   target_find_description ();
961 }
962
963 void
964 _initialize_target_descriptions (void)
965 {
966   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
967
968   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
969 Set target description specific variables."),
970                   &tdesc_set_cmdlist, "set tdesc ",
971                   0 /* allow-unknown */, &setlist);
972   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
973 Show target description specific variables."),
974                   &tdesc_show_cmdlist, "show tdesc ",
975                   0 /* allow-unknown */, &showlist);
976   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
977 Unset target description specific variables."),
978                   &tdesc_unset_cmdlist, "unset tdesc ",
979                   0 /* allow-unknown */, &unsetlist);
980
981   add_setshow_filename_cmd ("filename", class_obscure,
982                             &target_description_filename,
983                             _("\
984 Set the file to read for an XML target description"), _("\
985 Show the file to read for an XML target description"), _("\
986 When set, GDB will read the target description from a local\n\
987 file instead of querying the remote target."),
988                             set_tdesc_filename_cmd,
989                             show_tdesc_filename_cmd,
990                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);
991
992   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
993 Unset the file to read for an XML target description.  When unset,\n\
994 GDB will read the description from the target."),
995            &tdesc_unset_cmdlist);
996 }