* target-descriptions.c (tdesc_predefined_types): New.
[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 /* 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
346 {
347   const char *name;
348   struct type **type;
349 } tdesc_predefined_types[] =
350   {
351     { "int8", &builtin_type_int8 },
352     { "int16", &builtin_type_int16 },
353     { "int32", &builtin_type_int32 },
354     { "int64", &builtin_type_int64 },
355     { "uint8", &builtin_type_uint8 },
356     { "uint16", &builtin_type_uint16 },
357     { "uint32", &builtin_type_uint32 },
358     { "uint64", &builtin_type_uint64 },
359     { "ieee_single", &builtin_type_ieee_single },
360     { "ieee_double", &builtin_type_ieee_double },
361     { "arm_fpa_ext", &builtin_type_arm_ext }
362   };
363
364 /* Return the type associated with ID in the context of FEATURE, or
365    NULL if none.  */
366
367 struct type *
368 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
369 {
370   int ix;
371   struct type *gdb_type;
372
373   /* First try target-defined types.  */
374   for (ix = 0; VEC_iterate (type_p, feature->types, ix, gdb_type); ix++)
375     if (strcmp (TYPE_NAME (gdb_type), id) == 0)
376       return gdb_type;
377
378   /* Next try the predefined types.  */
379   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
380     if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
381       return *tdesc_predefined_types[ix].type;
382
383   return NULL;
384 }
385 \f
386
387 /* Support for registers from target descriptions.  */
388
389 /* Construct the per-gdbarch data.  */
390
391 static void *
392 tdesc_data_init (struct obstack *obstack)
393 {
394   struct tdesc_arch_data *data;
395
396   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
397   return data;
398 }
399
400 /* Similar, but for the temporary copy used during architecture
401    initialization.  */
402
403 struct tdesc_arch_data *
404 tdesc_data_alloc (void)
405 {
406   return XZALLOC (struct tdesc_arch_data);
407 }
408
409 /* Free something allocated by tdesc_data_alloc, if it is not going
410    to be used (for instance if it was unsuitable for the
411    architecture).  */
412
413 void
414 tdesc_data_cleanup (void *data_untyped)
415 {
416   struct tdesc_arch_data *data = data_untyped;
417
418   VEC_free (tdesc_reg_p, data->registers);
419   xfree (data);
420 }
421
422 /* Search FEATURE for a register named NAME.  */
423
424 int
425 tdesc_numbered_register (const struct tdesc_feature *feature,
426                          struct tdesc_arch_data *data,
427                          int regno, const char *name)
428 {
429   int ixr;
430   struct tdesc_reg *reg;
431
432   for (ixr = 0;
433        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
434        ixr++)
435     if (strcasecmp (reg->name, name) == 0)
436       {
437         /* Make sure the vector includes a REGNO'th element.  */
438         while (regno >= VEC_length (tdesc_reg_p, data->registers))
439           VEC_safe_push (tdesc_reg_p, data->registers, NULL);
440         VEC_replace (tdesc_reg_p, data->registers, regno, reg);
441         return 1;
442       }
443
444   return 0;
445 }
446
447 /* Search FEATURE for a register whose name is in NAMES.  */
448
449 int
450 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
451                                  struct tdesc_arch_data *data,
452                                  int regno, const char *const names[])
453 {
454   int i;
455
456   for (i = 0; names[i] != NULL; i++)
457     if (tdesc_numbered_register (feature, data, regno, names[i]))
458       return 1;
459
460   return 0;
461 }
462
463 /* Look up a register by its GDB internal register number.  */
464
465 static struct tdesc_reg *
466 tdesc_find_register (struct gdbarch *gdbarch, int regno)
467 {
468   struct tdesc_reg *reg;
469   struct tdesc_arch_data *data;
470
471   data = gdbarch_data (gdbarch, tdesc_data);
472   if (regno < VEC_length (tdesc_reg_p, data->registers))
473     return VEC_index (tdesc_reg_p, data->registers, regno);
474   else
475     return NULL;
476 }
477
478 /* Return the name of register REGNO, from the target description or
479    from an architecture-provided pseudo_register_name method.  */
480
481 const char *
482 tdesc_register_name (int regno)
483 {
484   struct tdesc_reg *reg = tdesc_find_register (current_gdbarch, regno);
485   int num_regs = gdbarch_num_regs (current_gdbarch);
486   int num_pseudo_regs = gdbarch_num_pseudo_regs (current_gdbarch);
487
488   if (reg != NULL)
489     return reg->name;
490
491   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
492     {
493       struct tdesc_arch_data *data = gdbarch_data (current_gdbarch,
494                                                    tdesc_data);
495       gdb_assert (data->pseudo_register_name != NULL);
496       return data->pseudo_register_name (regno);
497     }
498
499   return "";
500 }
501
502 static struct type *
503 tdesc_register_type (struct gdbarch *gdbarch, int regno)
504 {
505   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
506   int num_regs = gdbarch_num_regs (gdbarch);
507   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
508
509   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
510     {
511       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
512       gdb_assert (data->pseudo_register_type != NULL);
513       return data->pseudo_register_type (gdbarch, regno);
514     }
515
516   if (reg == NULL)
517     /* Return "int0_t", since "void" has a misleading size of one.  */
518     return builtin_type_int0;
519
520   /* First check for a predefined or target defined type.  */
521   if (reg->gdb_type)
522     return reg->gdb_type;
523
524   /* Next try size-sensitive type shortcuts.  */
525   if (strcmp (reg->type, "float") == 0)
526     {
527       if (reg->bitsize == gdbarch_float_bit (gdbarch))
528         return builtin_type_float;
529       else if (reg->bitsize == gdbarch_double_bit (gdbarch))
530         return builtin_type_double;
531       else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
532         return builtin_type_long_double;
533     }
534   else if (strcmp (reg->type, "int") == 0)
535     {
536       if (reg->bitsize == gdbarch_long_bit (gdbarch))
537         return builtin_type_long;
538       else if (reg->bitsize == TARGET_CHAR_BIT)
539         return builtin_type_char;
540       else if (reg->bitsize == gdbarch_short_bit (gdbarch))
541         return builtin_type_short;
542       else if (reg->bitsize == gdbarch_int_bit (gdbarch))
543         return builtin_type_int;
544       else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
545         return builtin_type_long_long;
546       else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
547         /* A bit desperate by this point... */
548         return builtin_type_void_data_ptr;
549     }
550   else if (strcmp (reg->type, "code_ptr") == 0)
551     return builtin_type_void_func_ptr;
552   else if (strcmp (reg->type, "data_ptr") == 0)
553     return builtin_type_void_data_ptr;
554   else
555     internal_error (__FILE__, __LINE__,
556                     "Register \"%s\" has an unknown type \"%s\"",
557                     reg->name, reg->type);
558
559   warning (_("Register \"%s\" has an unsupported size (%d bits)"),
560            reg->name, reg->bitsize);
561   return builtin_type_long;
562 }
563
564 static int
565 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
566 {
567   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
568
569   if (reg != NULL)
570     return reg->target_regnum;
571   else
572     return -1;
573 }
574
575 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
576    target description may be classified as general, float, or vector.
577    Unlike a gdbarch register_reggroup_p method, this function will
578    return -1 if it does not know; the caller should handle registers
579    with no specified group.
580
581    Arbitrary strings (other than "general", "float", and "vector")
582    from the description are not used; they cause the register to be
583    displayed in "info all-registers" but excluded from "info
584    registers" et al.  The names of containing features are also not
585    used.  This might be extended to display registers in some more
586    useful groupings.
587
588    The save-restore flag is also implemented here.  */
589
590 int
591 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
592                               struct reggroup *reggroup)
593 {
594   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
595
596   if (reg != NULL && reg->group != NULL)
597     {
598       int general_p = 0, float_p = 0, vector_p = 0;
599
600       if (strcmp (reg->group, "general") == 0)
601         general_p = 1;
602       else if (strcmp (reg->group, "float") == 0)
603         float_p = 1;
604       else if (strcmp (reg->group, "vector") == 0)
605         vector_p = 1;
606
607       if (reggroup == float_reggroup)
608         return float_p;
609
610       if (reggroup == vector_reggroup)
611         return vector_p;
612
613       if (reggroup == general_reggroup)
614         return general_p;
615     }
616
617   if (reg != NULL
618       && (reggroup == save_reggroup || reggroup == restore_reggroup))
619     return reg->save_restore;
620
621   return -1;
622 }
623
624 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
625    group specified go to the default reggroup function and are handled
626    by type.  */
627
628 static int
629 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
630                            struct reggroup *reggroup)
631 {
632   int num_regs = gdbarch_num_regs (gdbarch);
633   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
634   int ret;
635
636   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
637     {
638       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
639       gdb_assert (data->pseudo_register_reggroup_p != NULL);
640       return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
641     }
642
643   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
644   if (ret != -1)
645     return ret;
646
647   return default_register_reggroup_p (gdbarch, regno, reggroup);
648 }
649
650 /* Record architecture-specific functions to call for pseudo-register
651    support.  */
652
653 void
654 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
655                                 gdbarch_register_name_ftype *pseudo_name)
656 {
657   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
658
659   data->pseudo_register_name = pseudo_name;
660 }
661
662 void
663 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
664                                 gdbarch_register_type_ftype *pseudo_type)
665 {
666   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
667
668   data->pseudo_register_type = pseudo_type;
669 }
670
671 void
672 set_tdesc_pseudo_register_reggroup_p
673   (struct gdbarch *gdbarch,
674    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
675 {
676   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
677
678   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
679 }
680
681 /* Update GDBARCH to use the target description for registers.  */
682
683 void
684 tdesc_use_registers (struct gdbarch *gdbarch,
685                      struct tdesc_arch_data *early_data)
686 {
687   int num_regs = gdbarch_num_regs (gdbarch);
688   int i, ixf, ixr;
689   const struct target_desc *target_desc;
690   struct tdesc_feature *feature;
691   struct tdesc_reg *reg;
692   struct tdesc_arch_data *data;
693   htab_t reg_hash;
694
695   target_desc = gdbarch_target_desc (gdbarch);
696
697   /* We can't use the description for registers if it doesn't describe
698      any.  This function should only be called after validating
699      registers, so the caller should know that registers are
700      included.  */
701   gdb_assert (tdesc_has_registers (target_desc));
702
703   data = gdbarch_data (gdbarch, tdesc_data);
704   data->registers = early_data->registers;
705   xfree (early_data);
706
707   /* Build up a set of all registers, so that we can assign register
708      numbers where needed.  The hash table expands as necessary, so
709      the initial size is arbitrary.  */
710   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
711   for (ixf = 0;
712        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
713        ixf++)
714     for (ixr = 0;
715          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
716          ixr++)
717       {
718         void **slot = htab_find_slot (reg_hash, reg, INSERT);
719
720         *slot = reg;
721       }
722
723   /* Remove any registers which were assigned numbers by the
724      architecture.  */
725   for (ixr = 0; VEC_iterate (tdesc_reg_p, data->registers, ixr, reg); ixr++)
726     if (reg)
727       htab_remove_elt (reg_hash, reg);
728
729   /* Assign numbers to the remaining registers and add them to the
730      list of registers.  The new numbers are always above gdbarch_num_regs.
731      Iterate over the features, not the hash table, so that the order
732      matches that in the target description.  */
733
734   gdb_assert (VEC_length (tdesc_reg_p, data->registers) <= num_regs);
735   while (VEC_length (tdesc_reg_p, data->registers) < num_regs)
736     VEC_safe_push (tdesc_reg_p, data->registers, NULL);
737   for (ixf = 0;
738        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
739        ixf++)
740     for (ixr = 0;
741          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
742          ixr++)
743       if (htab_find (reg_hash, reg) != NULL)
744         {
745           VEC_safe_push (tdesc_reg_p, data->registers, reg);
746           num_regs++;
747         }
748
749   htab_delete (reg_hash);
750
751   /* Update the architecture.  */
752   set_gdbarch_num_regs (gdbarch, num_regs);
753   set_gdbarch_register_name (gdbarch, tdesc_register_name);
754   set_gdbarch_register_type (gdbarch, tdesc_register_type);
755   set_gdbarch_remote_register_number (gdbarch,
756                                       tdesc_remote_register_number);
757   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
758 }
759 \f
760
761 /* Methods for constructing a target description.  */
762
763 static void
764 tdesc_free_reg (struct tdesc_reg *reg)
765 {
766   xfree (reg->name);
767   xfree (reg->type);
768   xfree (reg->group);
769   xfree (reg);
770 }
771
772 void
773 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
774                   int regnum, int save_restore, const char *group,
775                   int bitsize, const char *type)
776 {
777   struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
778
779   reg->name = xstrdup (name);
780   reg->target_regnum = regnum;
781   reg->save_restore = save_restore;
782   reg->group = group ? xstrdup (group) : NULL;
783   reg->bitsize = bitsize;
784   reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
785
786   /* If the register's type is target-defined, look it up now.  We may not
787      have easy access to the containing feature when we want it later.  */
788   reg->gdb_type = tdesc_named_type (feature, reg->type);
789
790   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
791 }
792
793 static void
794 tdesc_free_feature (struct tdesc_feature *feature)
795 {
796   struct tdesc_reg *reg;
797   int ix;
798
799   for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
800     tdesc_free_reg (reg);
801   VEC_free (tdesc_reg_p, feature->registers);
802
803   /* There is no easy way to free xmalloc-allocated types, nor is
804      there a way to allocate types on an obstack not associated with
805      an objfile.  Therefore we never free types.  Since we only ever
806      parse an identical XML document once, this memory leak is mostly
807      contained.  */
808   VEC_free (type_p, feature->types);
809
810   xfree (feature->name);
811   xfree (feature);
812 }
813
814 struct tdesc_feature *
815 tdesc_create_feature (struct target_desc *tdesc, const char *name)
816 {
817   struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
818
819   new_feature->name = xstrdup (name);
820
821   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
822   return new_feature;
823 }
824
825 void
826 tdesc_record_type (struct tdesc_feature *feature, struct type *type)
827 {
828   /* The type's ID should be used as its TYPE_NAME.  */
829   gdb_assert (TYPE_NAME (type) != NULL);
830
831   VEC_safe_push (type_p, feature->types, type);
832 }
833
834 struct target_desc *
835 allocate_target_description (void)
836 {
837   return XZALLOC (struct target_desc);
838 }
839
840 static void
841 free_target_description (void *arg)
842 {
843   struct target_desc *target_desc = arg;
844   struct tdesc_feature *feature;
845   struct property *prop;
846   int ix;
847
848   for (ix = 0;
849        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
850        ix++)
851     tdesc_free_feature (feature);
852   VEC_free (tdesc_feature_p, target_desc->features);
853
854   for (ix = 0;
855        VEC_iterate (property_s, target_desc->properties, ix, prop);
856        ix++)
857     {
858       xfree (prop->key);
859       xfree (prop->value);
860     }
861   VEC_free (property_s, target_desc->properties);
862
863   xfree (target_desc);
864 }
865
866 struct cleanup *
867 make_cleanup_free_target_description (struct target_desc *target_desc)
868 {
869   return make_cleanup (free_target_description, target_desc);
870 }
871
872 void
873 set_tdesc_property (struct target_desc *target_desc,
874                     const char *key, const char *value)
875 {
876   struct property *prop, new_prop;
877   int ix;
878
879   gdb_assert (key != NULL && value != NULL);
880
881   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
882        ix++)
883     if (strcmp (prop->key, key) == 0)
884       internal_error (__FILE__, __LINE__,
885                       _("Attempted to add duplicate property \"%s\""), key);
886
887   new_prop.key = xstrdup (key);
888   new_prop.value = xstrdup (value);
889   VEC_safe_push (property_s, target_desc->properties, &new_prop);
890 }
891
892 void
893 set_tdesc_architecture (struct target_desc *target_desc,
894                         const struct bfd_arch_info *arch)
895 {
896   target_desc->arch = arch;
897 }
898 \f
899
900 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
901 static struct cmd_list_element *tdesc_unset_cmdlist;
902
903 /* Helper functions for the CLI commands.  */
904
905 static void
906 set_tdesc_cmd (char *args, int from_tty)
907 {
908   help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
909 }
910
911 static void
912 show_tdesc_cmd (char *args, int from_tty)
913 {
914   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
915 }
916
917 static void
918 unset_tdesc_cmd (char *args, int from_tty)
919 {
920   help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
921 }
922
923 static void
924 set_tdesc_filename_cmd (char *args, int from_tty,
925                         struct cmd_list_element *c)
926 {
927   target_clear_description ();
928   target_find_description ();
929 }
930
931 static void
932 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
933                          struct cmd_list_element *c,
934                          const char *value)
935 {
936   if (value != NULL && *value != '\0')
937     printf_filtered (_("\
938 The target description will be read from \"%s\".\n"),
939                      value);
940   else
941     printf_filtered (_("\
942 The target description will be read from the target.\n"));
943 }
944
945 static void
946 unset_tdesc_filename_cmd (char *args, int from_tty)
947 {
948   xfree (target_description_filename);
949   target_description_filename = NULL;
950   target_clear_description ();
951   target_find_description ();
952 }
953
954 static const char *
955 tdesc_type_id (struct type *type)
956 {
957   int ix;
958
959   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
960     if (TYPE_MAIN_TYPE (*tdesc_predefined_types[ix].type)
961         == TYPE_MAIN_TYPE (type))
962       return tdesc_predefined_types[ix].name;
963
964   return TYPE_NAME (type);
965 }
966
967 static void
968 maint_print_c_tdesc_cmd (char *args, int from_tty)
969 {
970   const struct target_desc *tdesc;
971   const char *filename, *inp;
972   char *function, *outp;
973   struct property *prop;
974   struct tdesc_feature *feature;
975   struct tdesc_reg *reg;
976   struct type *type;
977   int ix, ix2, ix3;
978
979   /* Use the global target-supplied description, not the current
980      architecture's.  This lets a GDB for one architecture generate C
981      for another architecture's description, even though the gdbarch
982      initialization code will reject the new description.  */
983   tdesc = current_target_desc;
984   if (tdesc == NULL)
985     error (_("There is no target description to print."));
986
987   if (target_description_filename == NULL)
988     error (_("The current target description did not come from an XML file."));
989
990   filename = lbasename (target_description_filename);
991   function = xmalloc (strlen (filename) + 1);
992   for (inp = filename, outp = function; *inp != '\0'; inp++)
993     if (*inp == '.')
994       break;
995     else if (*inp == '-')
996       *outp++ = '_';
997     else
998       *outp++ = *inp;
999   *outp = '\0';
1000
1001   /* Standard boilerplate.  */
1002   printf_unfiltered ("/* THIS FILE IS GENERATED.  Original: %s */\n\n",
1003                      filename);
1004   printf_unfiltered ("#include \"defs.h\"\n");
1005   printf_unfiltered ("#include \"gdbtypes.h\"\n");
1006   printf_unfiltered ("#include \"target-descriptions.h\"\n");
1007   printf_unfiltered ("\n");
1008
1009   printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1010   printf_unfiltered ("static void\n");
1011   printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1012   printf_unfiltered ("{\n");
1013   printf_unfiltered
1014     ("  struct target_desc *result = allocate_target_description ();\n");
1015   printf_unfiltered ("  struct tdesc_feature *feature;\n");
1016   printf_unfiltered ("  struct type *field_type, *type;\n");
1017   printf_unfiltered ("\n");
1018
1019   if (tdesc_architecture (tdesc) != NULL)
1020     {
1021       printf_unfiltered
1022         ("  set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1023          tdesc_architecture (tdesc)->printable_name);
1024       printf_unfiltered ("\n");
1025     }
1026
1027   for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1028        ix++)
1029     {
1030       printf_unfiltered ("  set_tdesc_property (result, \"%s\", \"%s\");\n",
1031               prop->key, prop->value);
1032     }
1033
1034   for (ix = 0;
1035        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1036        ix++)
1037     {
1038       printf_unfiltered ("  feature = tdesc_create_feature (result, \"%s\");\n",
1039                          feature->name);
1040
1041       for (ix2 = 0;
1042            VEC_iterate (type_p, feature->types, ix2, type);
1043            ix2++)
1044         {
1045           switch (TYPE_CODE (type))
1046             {
1047             case TYPE_CODE_ARRAY:
1048               printf_unfiltered
1049                 ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1050                  tdesc_type_id (TYPE_TARGET_TYPE (type)));
1051               printf_unfiltered
1052                 ("  type = init_vector_type (field_type, %d);\n",
1053                  TYPE_LENGTH (type) / TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
1054               printf_unfiltered
1055                 ("  TYPE_NAME (type) = xstrdup (\"%s\");\n", TYPE_NAME (type));
1056               break;
1057             case TYPE_CODE_UNION:
1058               printf_unfiltered
1059                 ("  type = init_composite_type (NULL, TYPE_CODE_UNION);\n");
1060               printf_unfiltered
1061                 ("  TYPE_NAME (type) = xstrdup (\"%s\");\n", TYPE_NAME (type));
1062               for (ix3 = 0; ix3 < TYPE_NFIELDS (type); ix3++)
1063                 {
1064                   printf_unfiltered
1065                     ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1066                      tdesc_type_id (TYPE_FIELD_TYPE (type, ix3)));
1067                   printf_unfiltered
1068                     ("  append_composite_type_field (type, "
1069                      "xstrdup (\"%s\"), field_type);\n",
1070                      TYPE_FIELD_NAME (type, ix3));
1071                 }
1072               if (TYPE_VECTOR (type))
1073                 printf_unfiltered
1074                   ("  TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;\n");
1075               break;
1076             default:
1077               error (_("C output is not supported type \"%s\"."), TYPE_NAME (type));
1078             }
1079           printf_unfiltered ("  tdesc_record_type (feature, type);\n");
1080           printf_unfiltered ("\n");
1081         }
1082
1083       for (ix2 = 0;
1084            VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1085            ix2++)
1086         {
1087           printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1088                              reg->name, reg->target_regnum, reg->save_restore);
1089           if (reg->group)
1090             printf_unfiltered ("\"%s\", ", reg->group);
1091           else
1092             printf_unfiltered ("NULL, ");
1093           printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1094         }
1095
1096       printf_unfiltered ("\n");
1097     }
1098
1099   printf_unfiltered ("  tdesc_%s = result;\n", function);
1100   printf_unfiltered ("}\n");
1101 }
1102
1103 void
1104 _initialize_target_descriptions (void)
1105 {
1106   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1107
1108   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1109 Set target description specific variables."),
1110                   &tdesc_set_cmdlist, "set tdesc ",
1111                   0 /* allow-unknown */, &setlist);
1112   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1113 Show target description specific variables."),
1114                   &tdesc_show_cmdlist, "show tdesc ",
1115                   0 /* allow-unknown */, &showlist);
1116   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1117 Unset target description specific variables."),
1118                   &tdesc_unset_cmdlist, "unset tdesc ",
1119                   0 /* allow-unknown */, &unsetlist);
1120
1121   add_setshow_filename_cmd ("filename", class_obscure,
1122                             &target_description_filename,
1123                             _("\
1124 Set the file to read for an XML target description"), _("\
1125 Show the file to read for an XML target description"), _("\
1126 When set, GDB will read the target description from a local\n\
1127 file instead of querying the remote target."),
1128                             set_tdesc_filename_cmd,
1129                             show_tdesc_filename_cmd,
1130                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1131
1132   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1133 Unset the file to read for an XML target description.  When unset,\n\
1134 GDB will read the description from the target."),
1135            &tdesc_unset_cmdlist);
1136
1137   add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1138 Print the current target description as a C source file."),
1139            &maintenanceprintlist);
1140 }