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