Merge branch 'vendor/GDB'
[dragonfly.git] / contrib / gdb-7 / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3    Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6    Contributed by CodeSourcery.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "gdbcmd.h"
26 #include "gdbtypes.h"
27 #include "reggroups.h"
28 #include "target.h"
29 #include "target-descriptions.h"
30 #include "vec.h"
31 #include "xml-support.h"
32 #include "xml-tdesc.h"
33 #include "osabi.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 tdesc_type *tdesc_type;
85 } *tdesc_reg_p;
86 DEF_VEC_P(tdesc_reg_p);
87
88 /* A named type from a target description.  */
89
90 typedef struct tdesc_type_field
91 {
92   char *name;
93   struct tdesc_type *type;
94   int start, end;
95 } tdesc_type_field;
96 DEF_VEC_O(tdesc_type_field);
97
98 typedef struct tdesc_type_flag
99 {
100   char *name;
101   int start;
102 } tdesc_type_flag;
103 DEF_VEC_O(tdesc_type_flag);
104
105 typedef struct tdesc_type
106 {
107   /* The name of this type.  */
108   char *name;
109
110   /* Identify the kind of this type.  */
111   enum
112   {
113     /* Predefined types.  */
114     TDESC_TYPE_INT8,
115     TDESC_TYPE_INT16,
116     TDESC_TYPE_INT32,
117     TDESC_TYPE_INT64,
118     TDESC_TYPE_INT128,
119     TDESC_TYPE_UINT8,
120     TDESC_TYPE_UINT16,
121     TDESC_TYPE_UINT32,
122     TDESC_TYPE_UINT64,
123     TDESC_TYPE_UINT128,
124     TDESC_TYPE_CODE_PTR,
125     TDESC_TYPE_DATA_PTR,
126     TDESC_TYPE_IEEE_SINGLE,
127     TDESC_TYPE_IEEE_DOUBLE,
128     TDESC_TYPE_ARM_FPA_EXT,
129     TDESC_TYPE_I387_EXT,
130
131     /* Types defined by a target feature.  */
132     TDESC_TYPE_VECTOR,
133     TDESC_TYPE_STRUCT,
134     TDESC_TYPE_UNION,
135     TDESC_TYPE_FLAGS
136   } kind;
137
138   /* Kind-specific data.  */
139   union
140   {
141     /* Vector type.  */
142     struct
143     {
144       struct tdesc_type *type;
145       int count;
146     } v;
147
148     /* Struct or union type.  */
149     struct
150     {
151       VEC(tdesc_type_field) *fields;
152       LONGEST size;
153     } u;
154
155     /* Flags type.  */
156     struct
157     {
158       VEC(tdesc_type_flag) *flags;
159       LONGEST size;
160     } f;
161   } u;
162 } *tdesc_type_p;
163 DEF_VEC_P(tdesc_type_p);
164
165 /* A feature from a target description.  Each feature is a collection
166    of other elements, e.g. registers and types.  */
167
168 typedef struct tdesc_feature
169 {
170   /* The name of this feature.  It may be recognized by the architecture
171      support code.  */
172   char *name;
173
174   /* The registers associated with this feature.  */
175   VEC(tdesc_reg_p) *registers;
176
177   /* The types associated with this feature.  */
178   VEC(tdesc_type_p) *types;
179 } *tdesc_feature_p;
180 DEF_VEC_P(tdesc_feature_p);
181
182 /* A compatible architecture from a target description.  */
183 typedef const struct bfd_arch_info *arch_p;
184 DEF_VEC_P(arch_p);
185
186 /* A target description.  */
187
188 struct target_desc
189 {
190   /* The architecture reported by the target, if any.  */
191   const struct bfd_arch_info *arch;
192
193   /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
194      otherwise.  */
195   enum gdb_osabi osabi;
196
197   /* The list of compatible architectures reported by the target.  */
198   VEC(arch_p) *compatible;
199
200   /* Any architecture-specific properties specified by the target.  */
201   VEC(property_s) *properties;
202
203   /* The features associated with this target.  */
204   VEC(tdesc_feature_p) *features;
205 };
206
207 /* Per-architecture data associated with a target description.  The
208    target description may be shared by multiple architectures, but
209    this data is private to one gdbarch.  */
210
211 typedef struct tdesc_arch_reg
212 {
213   struct tdesc_reg *reg;
214   struct type *type;
215 } tdesc_arch_reg;
216 DEF_VEC_O(tdesc_arch_reg);
217
218 struct tdesc_arch_data
219 {
220   /* A list of register/type pairs, indexed by GDB's internal register number.
221      During initialization of the gdbarch this list is used to store
222      registers which the architecture assigns a fixed register number.
223      Registers which are NULL in this array, or off the end, are
224      treated as zero-sized and nameless (i.e. placeholders in the
225      numbering).  */
226   VEC(tdesc_arch_reg) *arch_regs;
227
228   /* Functions which report the register name, type, and reggroups for
229      pseudo-registers.  */
230   gdbarch_register_name_ftype *pseudo_register_name;
231   gdbarch_register_type_ftype *pseudo_register_type;
232   gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
233 };
234
235 /* Global state.  These variables are associated with the current
236    target; if GDB adds support for multiple simultaneous targets, then
237    these variables should become target-specific data.  */
238
239 /* A flag indicating that a description has already been fetched from
240    the current target, so it should not be queried again.  */
241
242 static int target_desc_fetched;
243
244 /* The description fetched from the current target, or NULL if the
245    current target did not supply any description.  Only valid when
246    target_desc_fetched is set.  Only the description initialization
247    code should access this; normally, the description should be
248    accessed through the gdbarch object.  */
249
250 static const struct target_desc *current_target_desc;
251
252 /* Other global variables.  */
253
254 /* The filename to read a target description from.  */
255
256 static char *target_description_filename;
257
258 /* A handle for architecture-specific data associated with the
259    target description (see struct tdesc_arch_data).  */
260
261 static struct gdbarch_data *tdesc_data;
262
263 /* Fetch the current target's description, and switch the current
264    architecture to one which incorporates that description.  */
265
266 void
267 target_find_description (void)
268 {
269   /* If we've already fetched a description from the target, don't do
270      it again.  This allows a target to fetch the description early,
271      during its to_open or to_create_inferior, if it needs extra
272      information about the target to initialize.  */
273   if (target_desc_fetched)
274     return;
275
276   /* The current architecture should not have any target description
277      specified.  It should have been cleared, e.g. when we
278      disconnected from the previous target.  */
279   gdb_assert (gdbarch_target_desc (target_gdbarch) == NULL);
280
281   /* First try to fetch an XML description from the user-specified
282      file.  */
283   current_target_desc = NULL;
284   if (target_description_filename != NULL
285       && *target_description_filename != '\0')
286     current_target_desc
287       = file_read_description_xml (target_description_filename);
288
289   /* Next try to read the description from the current target using
290      target objects.  */
291   if (current_target_desc == NULL)
292     current_target_desc = target_read_description_xml (&current_target);
293
294   /* If that failed try a target-specific hook.  */
295   if (current_target_desc == NULL)
296     current_target_desc = target_read_description (&current_target);
297
298   /* If a non-NULL description was returned, then update the current
299      architecture.  */
300   if (current_target_desc)
301     {
302       struct gdbarch_info info;
303
304       gdbarch_info_init (&info);
305       info.target_desc = current_target_desc;
306       if (!gdbarch_update_p (info))
307         warning (_("Architecture rejected target-supplied description"));
308       else
309         {
310           struct tdesc_arch_data *data;
311
312           data = gdbarch_data (target_gdbarch, tdesc_data);
313           if (tdesc_has_registers (current_target_desc)
314               && data->arch_regs == NULL)
315             warning (_("Target-supplied registers are not supported "
316                        "by the current architecture"));
317         }
318     }
319
320   /* Now that we know this description is usable, record that we
321      fetched it.  */
322   target_desc_fetched = 1;
323 }
324
325 /* Discard any description fetched from the current target, and switch
326    the current architecture to one with no target description.  */
327
328 void
329 target_clear_description (void)
330 {
331   struct gdbarch_info info;
332
333   if (!target_desc_fetched)
334     return;
335
336   target_desc_fetched = 0;
337   current_target_desc = NULL;
338
339   gdbarch_info_init (&info);
340   if (!gdbarch_update_p (info))
341     internal_error (__FILE__, __LINE__,
342                     _("Could not remove target-supplied description"));
343 }
344
345 /* Return the global current target description.  This should only be
346    used by gdbarch initialization code; most access should be through
347    an existing gdbarch.  */
348
349 const struct target_desc *
350 target_current_description (void)
351 {
352   if (target_desc_fetched)
353     return current_target_desc;
354
355   return NULL;
356 }
357
358 /* Return non-zero if this target description is compatible
359    with the given BFD architecture.  */
360
361 int
362 tdesc_compatible_p (const struct target_desc *target_desc,
363                     const struct bfd_arch_info *arch)
364 {
365   const struct bfd_arch_info *compat;
366   int ix;
367
368   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
369        ix++)
370     {
371       if (compat == arch
372           || arch->compatible (arch, compat)
373           || compat->compatible (compat, arch))
374         return 1;
375     }
376
377   return 0;
378 }
379 \f
380
381 /* Direct accessors for target descriptions.  */
382
383 /* Return the string value of a property named KEY, or NULL if the
384    property was not specified.  */
385
386 const char *
387 tdesc_property (const struct target_desc *target_desc, const char *key)
388 {
389   struct property *prop;
390   int ix;
391
392   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
393        ix++)
394     if (strcmp (prop->key, key) == 0)
395       return prop->value;
396
397   return NULL;
398 }
399
400 /* Return the BFD architecture associated with this target
401    description, or NULL if no architecture was specified.  */
402
403 const struct bfd_arch_info *
404 tdesc_architecture (const struct target_desc *target_desc)
405 {
406   return target_desc->arch;
407 }
408
409 /* Return the OSABI associated with this target description, or
410    GDB_OSABI_UNKNOWN if no osabi was specified.  */
411
412 enum gdb_osabi
413 tdesc_osabi (const struct target_desc *target_desc)
414 {
415   return target_desc->osabi;
416 }
417
418 \f
419
420 /* Return 1 if this target description includes any registers.  */
421
422 int
423 tdesc_has_registers (const struct target_desc *target_desc)
424 {
425   int ix;
426   struct tdesc_feature *feature;
427
428   if (target_desc == NULL)
429     return 0;
430
431   for (ix = 0;
432        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
433        ix++)
434     if (! VEC_empty (tdesc_reg_p, feature->registers))
435       return 1;
436
437   return 0;
438 }
439
440 /* Return the feature with the given name, if present, or NULL if
441    the named feature is not found.  */
442
443 const struct tdesc_feature *
444 tdesc_find_feature (const struct target_desc *target_desc,
445                     const char *name)
446 {
447   int ix;
448   struct tdesc_feature *feature;
449
450   for (ix = 0;
451        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
452        ix++)
453     if (strcmp (feature->name, name) == 0)
454       return feature;
455
456   return NULL;
457 }
458
459 /* Return the name of FEATURE.  */
460
461 const char *
462 tdesc_feature_name (const struct tdesc_feature *feature)
463 {
464   return feature->name;
465 }
466
467 /* Predefined types.  */
468 static struct tdesc_type tdesc_predefined_types[] =
469 {
470   { "int8", TDESC_TYPE_INT8 },
471   { "int16", TDESC_TYPE_INT16 },
472   { "int32", TDESC_TYPE_INT32 },
473   { "int64", TDESC_TYPE_INT64 },
474   { "int128", TDESC_TYPE_INT128 },
475   { "uint8", TDESC_TYPE_UINT8 },
476   { "uint16", TDESC_TYPE_UINT16 },
477   { "uint32", TDESC_TYPE_UINT32 },
478   { "uint64", TDESC_TYPE_UINT64 },
479   { "uint128", TDESC_TYPE_UINT128 },
480   { "code_ptr", TDESC_TYPE_CODE_PTR },
481   { "data_ptr", TDESC_TYPE_DATA_PTR },
482   { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
483   { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
484   { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
485   { "i387_ext", TDESC_TYPE_I387_EXT }
486 };
487
488 /* Return the type associated with ID in the context of FEATURE, or
489    NULL if none.  */
490
491 struct tdesc_type *
492 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
493 {
494   int ix;
495   struct tdesc_type *type;
496
497   /* First try target-defined types.  */
498   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
499     if (strcmp (type->name, id) == 0)
500       return type;
501
502   /* Next try the predefined types.  */
503   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
504     if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
505       return &tdesc_predefined_types[ix];
506
507   return NULL;
508 }
509
510 /* Lookup type associated with ID.  */
511
512 struct type *
513 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
514 {
515   struct tdesc_arch_reg *reg;
516   struct tdesc_arch_data *data;
517   int i, num_regs;
518
519   data = gdbarch_data (gdbarch, tdesc_data);
520   num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
521   for (i = 0; i < num_regs; i++)
522     {
523       reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
524       if (reg->reg
525           && reg->reg->tdesc_type
526           && reg->type
527           && strcmp (id, reg->reg->tdesc_type->name) == 0)
528         return reg->type;
529     }
530
531   return NULL;
532 }
533
534 /* Construct, if necessary, and return the GDB type implementing target
535    type TDESC_TYPE for architecture GDBARCH.  */
536
537 static struct type *
538 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
539 {
540   struct type *type;
541
542   switch (tdesc_type->kind)
543     {
544     /* Predefined types.  */
545     case TDESC_TYPE_INT8:
546       return builtin_type (gdbarch)->builtin_int8;
547
548     case TDESC_TYPE_INT16:
549       return builtin_type (gdbarch)->builtin_int16;
550
551     case TDESC_TYPE_INT32:
552       return builtin_type (gdbarch)->builtin_int32;
553
554     case TDESC_TYPE_INT64:
555       return builtin_type (gdbarch)->builtin_int64;
556
557     case TDESC_TYPE_INT128:
558       return builtin_type (gdbarch)->builtin_int128;
559
560     case TDESC_TYPE_UINT8:
561       return builtin_type (gdbarch)->builtin_uint8;
562
563     case TDESC_TYPE_UINT16:
564       return builtin_type (gdbarch)->builtin_uint16;
565
566     case TDESC_TYPE_UINT32:
567       return builtin_type (gdbarch)->builtin_uint32;
568
569     case TDESC_TYPE_UINT64:
570       return builtin_type (gdbarch)->builtin_uint64;
571
572     case TDESC_TYPE_UINT128:
573       return builtin_type (gdbarch)->builtin_uint128;
574
575     case TDESC_TYPE_CODE_PTR:
576       return builtin_type (gdbarch)->builtin_func_ptr;
577
578     case TDESC_TYPE_DATA_PTR:
579       return builtin_type (gdbarch)->builtin_data_ptr;
580
581     default:
582       break;
583     }
584
585   type = tdesc_find_type (gdbarch, tdesc_type->name);
586   if (type)
587     return type;
588
589   switch (tdesc_type->kind)
590     {
591     case TDESC_TYPE_IEEE_SINGLE:
592       return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
593                               floatformats_ieee_single);
594
595     case TDESC_TYPE_IEEE_DOUBLE:
596       return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
597                               floatformats_ieee_double);
598
599     case TDESC_TYPE_ARM_FPA_EXT:
600       return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
601                               floatformats_arm_ext);
602
603     case TDESC_TYPE_I387_EXT:
604       return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
605                               floatformats_i387_ext);
606
607     /* Types defined by a target feature.  */
608     case TDESC_TYPE_VECTOR:
609       {
610         struct type *type, *field_type;
611
612         field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
613         type = init_vector_type (field_type, tdesc_type->u.v.count);
614         TYPE_NAME (type) = xstrdup (tdesc_type->name);
615
616         return type;
617       }
618
619     case TDESC_TYPE_STRUCT:
620       {
621         struct type *type, *field_type;
622         struct tdesc_type_field *f;
623         int ix;
624
625         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
626         TYPE_NAME (type) = xstrdup (tdesc_type->name);
627         TYPE_TAG_NAME (type) = TYPE_NAME (type);
628
629         for (ix = 0;
630              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
631              ix++)
632           {
633             if (f->type == NULL)
634               {
635                 /* Bitfield.  */
636                 struct field *fld;
637                 struct type *field_type;
638                 int bitsize, total_size;
639
640                 /* This invariant should be preserved while creating
641                    types.  */
642                 gdb_assert (tdesc_type->u.u.size != 0);
643                 if (tdesc_type->u.u.size > 4)
644                   field_type = builtin_type (gdbarch)->builtin_uint64;
645                 else
646                   field_type = builtin_type (gdbarch)->builtin_uint32;
647
648                 fld = append_composite_type_field_raw (type, xstrdup (f->name),
649                                                        field_type);
650
651                 /* For little-endian, BITPOS counts from the LSB of
652                    the structure and marks the LSB of the field.  For
653                    big-endian, BITPOS counts from the MSB of the
654                    structure and marks the MSB of the field.  Either
655                    way, it is the number of bits to the "left" of the
656                    field.  To calculate this in big-endian, we need
657                    the total size of the structure.  */
658                 bitsize = f->end - f->start + 1;
659                 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
660                 if (gdbarch_bits_big_endian (gdbarch))
661                   FIELD_BITPOS (fld[0]) = total_size - f->start - bitsize;
662                 else
663                   FIELD_BITPOS (fld[0]) = f->start;
664                 FIELD_BITSIZE (fld[0]) = bitsize;
665               }
666             else
667               {
668                 field_type = tdesc_gdb_type (gdbarch, f->type);
669                 append_composite_type_field (type, xstrdup (f->name),
670                                              field_type);
671               }
672           }
673
674         if (tdesc_type->u.u.size != 0)
675           TYPE_LENGTH (type) = tdesc_type->u.u.size;
676         return type;
677       }
678
679     case TDESC_TYPE_UNION:
680       {
681         struct type *type, *field_type;
682         struct tdesc_type_field *f;
683         int ix;
684
685         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
686         TYPE_NAME (type) = xstrdup (tdesc_type->name);
687
688         for (ix = 0;
689              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
690              ix++)
691           {
692             field_type = tdesc_gdb_type (gdbarch, f->type);
693             append_composite_type_field (type, xstrdup (f->name), field_type);
694
695             /* If any of the children of a union are vectors, flag the
696                union as a vector also.  This allows e.g. a union of two
697                vector types to show up automatically in "info vector".  */
698             if (TYPE_VECTOR (field_type))
699               TYPE_VECTOR (type) = 1;
700           }
701         return type;
702       }
703
704     case TDESC_TYPE_FLAGS:
705       {
706         struct tdesc_type_flag *f;
707         int ix;
708
709         type = arch_flags_type (gdbarch, tdesc_type->name,
710                                 tdesc_type->u.f.size);
711         for (ix = 0;
712              VEC_iterate (tdesc_type_flag, tdesc_type->u.f.flags, ix, f);
713              ix++)
714           /* Note that contrary to the function name, this call will
715              just set the properties of an already-allocated
716              field.  */
717           append_flags_type_flag (type, f->start,
718                                   *f->name ? f->name : NULL);
719
720         return type;
721       }
722     }
723
724   internal_error (__FILE__, __LINE__,
725                   "Type \"%s\" has an unknown kind %d",
726                   tdesc_type->name, tdesc_type->kind);
727 }
728 \f
729
730 /* Support for registers from target descriptions.  */
731
732 /* Construct the per-gdbarch data.  */
733
734 static void *
735 tdesc_data_init (struct obstack *obstack)
736 {
737   struct tdesc_arch_data *data;
738
739   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
740   return data;
741 }
742
743 /* Similar, but for the temporary copy used during architecture
744    initialization.  */
745
746 struct tdesc_arch_data *
747 tdesc_data_alloc (void)
748 {
749   return XZALLOC (struct tdesc_arch_data);
750 }
751
752 /* Free something allocated by tdesc_data_alloc, if it is not going
753    to be used (for instance if it was unsuitable for the
754    architecture).  */
755
756 void
757 tdesc_data_cleanup (void *data_untyped)
758 {
759   struct tdesc_arch_data *data = data_untyped;
760
761   VEC_free (tdesc_arch_reg, data->arch_regs);
762   xfree (data);
763 }
764
765 /* Search FEATURE for a register named NAME.  */
766
767 static struct tdesc_reg *
768 tdesc_find_register_early (const struct tdesc_feature *feature,
769                            const char *name)
770 {
771   int ixr;
772   struct tdesc_reg *reg;
773
774   for (ixr = 0;
775        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
776        ixr++)
777     if (strcasecmp (reg->name, name) == 0)
778       return reg;
779
780   return NULL;
781 }
782
783 /* Search FEATURE for a register named NAME.  Assign REGNO to it.  */
784
785 int
786 tdesc_numbered_register (const struct tdesc_feature *feature,
787                          struct tdesc_arch_data *data,
788                          int regno, const char *name)
789 {
790   struct tdesc_arch_reg arch_reg = { 0 };
791   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
792
793   if (reg == NULL)
794     return 0;
795
796   /* Make sure the vector includes a REGNO'th element.  */
797   while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
798     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
799
800   arch_reg.reg = reg;
801   VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
802   return 1;
803 }
804
805 /* Search FEATURE for a register named NAME, but do not assign a fixed
806    register number to it.  */
807
808 int
809 tdesc_unnumbered_register (const struct tdesc_feature *feature,
810                            const char *name)
811 {
812   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
813
814   if (reg == NULL)
815     return 0;
816
817   return 1;
818 }
819
820 /* Search FEATURE for a register whose name is in NAMES and assign
821    REGNO to it.  */
822
823 int
824 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
825                                  struct tdesc_arch_data *data,
826                                  int regno, const char *const names[])
827 {
828   int i;
829
830   for (i = 0; names[i] != NULL; i++)
831     if (tdesc_numbered_register (feature, data, regno, names[i]))
832       return 1;
833
834   return 0;
835 }
836
837 /* Search FEATURE for a register named NAME, and return its size in
838    bits.  The register must exist.  */
839
840 int
841 tdesc_register_size (const struct tdesc_feature *feature,
842                      const char *name)
843 {
844   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
845
846   gdb_assert (reg != NULL);
847   return reg->bitsize;
848 }
849
850 /* Look up a register by its GDB internal register number.  */
851
852 static struct tdesc_arch_reg *
853 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
854 {
855   struct tdesc_arch_data *data;
856
857   data = gdbarch_data (gdbarch, tdesc_data);
858   if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
859     return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
860   else
861     return NULL;
862 }
863
864 static struct tdesc_reg *
865 tdesc_find_register (struct gdbarch *gdbarch, int regno)
866 {
867   struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
868
869   return reg? reg->reg : NULL;
870 }
871
872 /* Return the name of register REGNO, from the target description or
873    from an architecture-provided pseudo_register_name method.  */
874
875 const char *
876 tdesc_register_name (struct gdbarch *gdbarch, int regno)
877 {
878   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
879   int num_regs = gdbarch_num_regs (gdbarch);
880   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
881
882   if (reg != NULL)
883     return reg->name;
884
885   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
886     {
887       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
888
889       gdb_assert (data->pseudo_register_name != NULL);
890       return data->pseudo_register_name (gdbarch, regno);
891     }
892
893   return "";
894 }
895
896 struct type *
897 tdesc_register_type (struct gdbarch *gdbarch, int regno)
898 {
899   struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
900   struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
901   int num_regs = gdbarch_num_regs (gdbarch);
902   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
903
904   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
905     {
906       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
907
908       gdb_assert (data->pseudo_register_type != NULL);
909       return data->pseudo_register_type (gdbarch, regno);
910     }
911
912   if (reg == NULL)
913     /* Return "int0_t", since "void" has a misleading size of one.  */
914     return builtin_type (gdbarch)->builtin_int0;
915
916   if (arch_reg->type == NULL)
917     {
918       /* First check for a predefined or target defined type.  */
919       if (reg->tdesc_type)
920         arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
921
922       /* Next try size-sensitive type shortcuts.  */
923       else if (strcmp (reg->type, "float") == 0)
924         {
925           if (reg->bitsize == gdbarch_float_bit (gdbarch))
926             arch_reg->type = builtin_type (gdbarch)->builtin_float;
927           else if (reg->bitsize == gdbarch_double_bit (gdbarch))
928             arch_reg->type = builtin_type (gdbarch)->builtin_double;
929           else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
930             arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
931           else
932             {
933               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
934                        reg->name, reg->bitsize);
935               arch_reg->type = builtin_type (gdbarch)->builtin_double;
936             }
937         }
938       else if (strcmp (reg->type, "int") == 0)
939         {
940           if (reg->bitsize == gdbarch_long_bit (gdbarch))
941             arch_reg->type = builtin_type (gdbarch)->builtin_long;
942           else if (reg->bitsize == TARGET_CHAR_BIT)
943             arch_reg->type = builtin_type (gdbarch)->builtin_char;
944           else if (reg->bitsize == gdbarch_short_bit (gdbarch))
945             arch_reg->type = builtin_type (gdbarch)->builtin_short;
946           else if (reg->bitsize == gdbarch_int_bit (gdbarch))
947             arch_reg->type = builtin_type (gdbarch)->builtin_int;
948           else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
949             arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
950           else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
951           /* A bit desperate by this point...  */
952             arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
953           else
954             {
955               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
956                        reg->name, reg->bitsize);
957               arch_reg->type = builtin_type (gdbarch)->builtin_long;
958             }
959         }
960
961       if (arch_reg->type == NULL)
962         internal_error (__FILE__, __LINE__,
963                         "Register \"%s\" has an unknown type \"%s\"",
964                         reg->name, reg->type);
965     }
966
967   return arch_reg->type;
968 }
969
970 static int
971 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
972 {
973   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
974
975   if (reg != NULL)
976     return reg->target_regnum;
977   else
978     return -1;
979 }
980
981 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
982    target description may be classified as general, float, or vector.
983    Unlike a gdbarch register_reggroup_p method, this function will
984    return -1 if it does not know; the caller should handle registers
985    with no specified group.
986
987    Arbitrary strings (other than "general", "float", and "vector")
988    from the description are not used; they cause the register to be
989    displayed in "info all-registers" but excluded from "info
990    registers" et al.  The names of containing features are also not
991    used.  This might be extended to display registers in some more
992    useful groupings.
993
994    The save-restore flag is also implemented here.  */
995
996 int
997 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
998                               struct reggroup *reggroup)
999 {
1000   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1001
1002   if (reg != NULL && reg->group != NULL)
1003     {
1004       int general_p = 0, float_p = 0, vector_p = 0;
1005
1006       if (strcmp (reg->group, "general") == 0)
1007         general_p = 1;
1008       else if (strcmp (reg->group, "float") == 0)
1009         float_p = 1;
1010       else if (strcmp (reg->group, "vector") == 0)
1011         vector_p = 1;
1012
1013       if (reggroup == float_reggroup)
1014         return float_p;
1015
1016       if (reggroup == vector_reggroup)
1017         return vector_p;
1018
1019       if (reggroup == general_reggroup)
1020         return general_p;
1021     }
1022
1023   if (reg != NULL
1024       && (reggroup == save_reggroup || reggroup == restore_reggroup))
1025     return reg->save_restore;
1026
1027   return -1;
1028 }
1029
1030 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
1031    group specified go to the default reggroup function and are handled
1032    by type.  */
1033
1034 static int
1035 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1036                            struct reggroup *reggroup)
1037 {
1038   int num_regs = gdbarch_num_regs (gdbarch);
1039   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1040   int ret;
1041
1042   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1043     {
1044       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1045
1046       if (data->pseudo_register_reggroup_p != NULL)
1047         return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1048       /* Otherwise fall through to the default reggroup_p.  */
1049     }
1050
1051   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1052   if (ret != -1)
1053     return ret;
1054
1055   return default_register_reggroup_p (gdbarch, regno, reggroup);
1056 }
1057
1058 /* Record architecture-specific functions to call for pseudo-register
1059    support.  */
1060
1061 void
1062 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1063                                 gdbarch_register_name_ftype *pseudo_name)
1064 {
1065   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1066
1067   data->pseudo_register_name = pseudo_name;
1068 }
1069
1070 void
1071 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1072                                 gdbarch_register_type_ftype *pseudo_type)
1073 {
1074   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1075
1076   data->pseudo_register_type = pseudo_type;
1077 }
1078
1079 void
1080 set_tdesc_pseudo_register_reggroup_p
1081   (struct gdbarch *gdbarch,
1082    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1083 {
1084   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1085
1086   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1087 }
1088
1089 /* Update GDBARCH to use the target description for registers.  */
1090
1091 void
1092 tdesc_use_registers (struct gdbarch *gdbarch,
1093                      const struct target_desc *target_desc,
1094                      struct tdesc_arch_data *early_data)
1095 {
1096   int num_regs = gdbarch_num_regs (gdbarch);
1097   int ixf, ixr;
1098   struct tdesc_feature *feature;
1099   struct tdesc_reg *reg;
1100   struct tdesc_arch_data *data;
1101   struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1102   htab_t reg_hash;
1103
1104   /* We can't use the description for registers if it doesn't describe
1105      any.  This function should only be called after validating
1106      registers, so the caller should know that registers are
1107      included.  */
1108   gdb_assert (tdesc_has_registers (target_desc));
1109
1110   data = gdbarch_data (gdbarch, tdesc_data);
1111   data->arch_regs = early_data->arch_regs;
1112   xfree (early_data);
1113
1114   /* Build up a set of all registers, so that we can assign register
1115      numbers where needed.  The hash table expands as necessary, so
1116      the initial size is arbitrary.  */
1117   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1118   for (ixf = 0;
1119        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1120        ixf++)
1121     for (ixr = 0;
1122          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1123          ixr++)
1124       {
1125         void **slot = htab_find_slot (reg_hash, reg, INSERT);
1126
1127         *slot = reg;
1128       }
1129
1130   /* Remove any registers which were assigned numbers by the
1131      architecture.  */
1132   for (ixr = 0;
1133        VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1134        ixr++)
1135     if (arch_reg->reg)
1136       htab_remove_elt (reg_hash, arch_reg->reg);
1137
1138   /* Assign numbers to the remaining registers and add them to the
1139      list of registers.  The new numbers are always above gdbarch_num_regs.
1140      Iterate over the features, not the hash table, so that the order
1141      matches that in the target description.  */
1142
1143   gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1144   while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1145     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1146   for (ixf = 0;
1147        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1148        ixf++)
1149     for (ixr = 0;
1150          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1151          ixr++)
1152       if (htab_find (reg_hash, reg) != NULL)
1153         {
1154           new_arch_reg.reg = reg;
1155           VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1156           num_regs++;
1157         }
1158
1159   htab_delete (reg_hash);
1160
1161   /* Update the architecture.  */
1162   set_gdbarch_num_regs (gdbarch, num_regs);
1163   set_gdbarch_register_name (gdbarch, tdesc_register_name);
1164   set_gdbarch_register_type (gdbarch, tdesc_register_type);
1165   set_gdbarch_remote_register_number (gdbarch,
1166                                       tdesc_remote_register_number);
1167   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1168 }
1169 \f
1170
1171 /* Methods for constructing a target description.  */
1172
1173 static void
1174 tdesc_free_reg (struct tdesc_reg *reg)
1175 {
1176   xfree (reg->name);
1177   xfree (reg->type);
1178   xfree (reg->group);
1179   xfree (reg);
1180 }
1181
1182 void
1183 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1184                   int regnum, int save_restore, const char *group,
1185                   int bitsize, const char *type)
1186 {
1187   struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
1188
1189   reg->name = xstrdup (name);
1190   reg->target_regnum = regnum;
1191   reg->save_restore = save_restore;
1192   reg->group = group ? xstrdup (group) : NULL;
1193   reg->bitsize = bitsize;
1194   reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
1195
1196   /* If the register's type is target-defined, look it up now.  We may not
1197      have easy access to the containing feature when we want it later.  */
1198   reg->tdesc_type = tdesc_named_type (feature, reg->type);
1199
1200   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1201 }
1202
1203 static void
1204 tdesc_free_type (struct tdesc_type *type)
1205 {
1206   switch (type->kind)
1207     {
1208     case TDESC_TYPE_STRUCT:
1209     case TDESC_TYPE_UNION:
1210       {
1211         struct tdesc_type_field *f;
1212         int ix;
1213
1214         for (ix = 0;
1215              VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
1216              ix++)
1217           xfree (f->name);
1218
1219         VEC_free (tdesc_type_field, type->u.u.fields);
1220       }
1221       break;
1222
1223     case TDESC_TYPE_FLAGS:
1224       {
1225         struct tdesc_type_flag *f;
1226         int ix;
1227
1228         for (ix = 0;
1229              VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
1230              ix++)
1231           xfree (f->name);
1232
1233         VEC_free (tdesc_type_flag, type->u.f.flags);
1234       }
1235       break;
1236
1237     default:
1238       break;
1239     }
1240
1241   xfree (type->name);
1242   xfree (type);
1243 }
1244
1245 struct tdesc_type *
1246 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1247                      struct tdesc_type *field_type, int count)
1248 {
1249   struct tdesc_type *type = XZALLOC (struct tdesc_type);
1250
1251   type->name = xstrdup (name);
1252   type->kind = TDESC_TYPE_VECTOR;
1253   type->u.v.type = field_type;
1254   type->u.v.count = count;
1255
1256   VEC_safe_push (tdesc_type_p, feature->types, type);
1257   return type;
1258 }
1259
1260 struct tdesc_type *
1261 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1262 {
1263   struct tdesc_type *type = XZALLOC (struct tdesc_type);
1264
1265   type->name = xstrdup (name);
1266   type->kind = TDESC_TYPE_STRUCT;
1267
1268   VEC_safe_push (tdesc_type_p, feature->types, type);
1269   return type;
1270 }
1271
1272 /* Set the total length of TYPE.  Structs which contain bitfields may
1273    omit the reserved bits, so the end of the last field may not
1274    suffice.  */
1275
1276 void
1277 tdesc_set_struct_size (struct tdesc_type *type, LONGEST size)
1278 {
1279   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1280   type->u.u.size = size;
1281 }
1282
1283 struct tdesc_type *
1284 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1285 {
1286   struct tdesc_type *type = XZALLOC (struct tdesc_type);
1287
1288   type->name = xstrdup (name);
1289   type->kind = TDESC_TYPE_UNION;
1290
1291   VEC_safe_push (tdesc_type_p, feature->types, type);
1292   return type;
1293 }
1294
1295 struct tdesc_type *
1296 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1297                     LONGEST size)
1298 {
1299   struct tdesc_type *type = XZALLOC (struct tdesc_type);
1300
1301   type->name = xstrdup (name);
1302   type->kind = TDESC_TYPE_FLAGS;
1303   type->u.f.size = size;
1304
1305   VEC_safe_push (tdesc_type_p, feature->types, type);
1306   return type;
1307 }
1308
1309 /* Add a new field.  Return a temporary pointer to the field, which
1310    is only valid until the next call to tdesc_add_field (the vector
1311    might be reallocated).  */
1312
1313 void
1314 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1315                  struct tdesc_type *field_type)
1316 {
1317   struct tdesc_type_field f = { 0 };
1318
1319   gdb_assert (type->kind == TDESC_TYPE_UNION
1320               || type->kind == TDESC_TYPE_STRUCT);
1321
1322   f.name = xstrdup (field_name);
1323   f.type = field_type;
1324
1325   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1326 }
1327
1328 /* Add a new bitfield.  */
1329
1330 void
1331 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1332                     int start, int end)
1333 {
1334   struct tdesc_type_field f = { 0 };
1335
1336   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1337
1338   f.name = xstrdup (field_name);
1339   f.start = start;
1340   f.end = end;
1341
1342   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1343 }
1344
1345 void
1346 tdesc_add_flag (struct tdesc_type *type, int start,
1347                 const char *flag_name)
1348 {
1349   struct tdesc_type_flag f = { 0 };
1350
1351   gdb_assert (type->kind == TDESC_TYPE_FLAGS);
1352
1353   f.name = xstrdup (flag_name);
1354   f.start = start;
1355
1356   VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
1357 }
1358
1359 static void
1360 tdesc_free_feature (struct tdesc_feature *feature)
1361 {
1362   struct tdesc_reg *reg;
1363   struct tdesc_type *type;
1364   int ix;
1365
1366   for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
1367     tdesc_free_reg (reg);
1368   VEC_free (tdesc_reg_p, feature->registers);
1369
1370   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
1371     tdesc_free_type (type);
1372   VEC_free (tdesc_type_p, feature->types);
1373
1374   xfree (feature->name);
1375   xfree (feature);
1376 }
1377
1378 struct tdesc_feature *
1379 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1380 {
1381   struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
1382
1383   new_feature->name = xstrdup (name);
1384
1385   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1386   return new_feature;
1387 }
1388
1389 struct target_desc *
1390 allocate_target_description (void)
1391 {
1392   return XZALLOC (struct target_desc);
1393 }
1394
1395 static void
1396 free_target_description (void *arg)
1397 {
1398   struct target_desc *target_desc = arg;
1399   struct tdesc_feature *feature;
1400   struct property *prop;
1401   int ix;
1402
1403   for (ix = 0;
1404        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
1405        ix++)
1406     tdesc_free_feature (feature);
1407   VEC_free (tdesc_feature_p, target_desc->features);
1408
1409   for (ix = 0;
1410        VEC_iterate (property_s, target_desc->properties, ix, prop);
1411        ix++)
1412     {
1413       xfree (prop->key);
1414       xfree (prop->value);
1415     }
1416   VEC_free (property_s, target_desc->properties);
1417
1418   VEC_free (arch_p, target_desc->compatible);
1419
1420   xfree (target_desc);
1421 }
1422
1423 struct cleanup *
1424 make_cleanup_free_target_description (struct target_desc *target_desc)
1425 {
1426   return make_cleanup (free_target_description, target_desc);
1427 }
1428
1429 void
1430 tdesc_add_compatible (struct target_desc *target_desc,
1431                       const struct bfd_arch_info *compatible)
1432 {
1433   const struct bfd_arch_info *compat;
1434   int ix;
1435
1436   /* If this instance of GDB is compiled without BFD support for the
1437      compatible architecture, simply ignore it -- we would not be able
1438      to handle it anyway.  */
1439   if (compatible == NULL)
1440     return;
1441
1442   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1443        ix++)
1444     if (compat == compatible)
1445       internal_error (__FILE__, __LINE__,
1446                       _("Attempted to add duplicate "
1447                         "compatible architecture \"%s\""),
1448                       compatible->printable_name);
1449
1450   VEC_safe_push (arch_p, target_desc->compatible, compatible);
1451 }
1452
1453 void
1454 set_tdesc_property (struct target_desc *target_desc,
1455                     const char *key, const char *value)
1456 {
1457   struct property *prop, new_prop;
1458   int ix;
1459
1460   gdb_assert (key != NULL && value != NULL);
1461
1462   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1463        ix++)
1464     if (strcmp (prop->key, key) == 0)
1465       internal_error (__FILE__, __LINE__,
1466                       _("Attempted to add duplicate property \"%s\""), key);
1467
1468   new_prop.key = xstrdup (key);
1469   new_prop.value = xstrdup (value);
1470   VEC_safe_push (property_s, target_desc->properties, &new_prop);
1471 }
1472
1473 void
1474 set_tdesc_architecture (struct target_desc *target_desc,
1475                         const struct bfd_arch_info *arch)
1476 {
1477   target_desc->arch = arch;
1478 }
1479
1480 void
1481 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1482 {
1483   target_desc->osabi = osabi;
1484 }
1485 \f
1486
1487 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1488 static struct cmd_list_element *tdesc_unset_cmdlist;
1489
1490 /* Helper functions for the CLI commands.  */
1491
1492 static void
1493 set_tdesc_cmd (char *args, int from_tty)
1494 {
1495   help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
1496 }
1497
1498 static void
1499 show_tdesc_cmd (char *args, int from_tty)
1500 {
1501   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1502 }
1503
1504 static void
1505 unset_tdesc_cmd (char *args, int from_tty)
1506 {
1507   help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
1508 }
1509
1510 static void
1511 set_tdesc_filename_cmd (char *args, int from_tty,
1512                         struct cmd_list_element *c)
1513 {
1514   target_clear_description ();
1515   target_find_description ();
1516 }
1517
1518 static void
1519 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1520                          struct cmd_list_element *c,
1521                          const char *value)
1522 {
1523   if (value != NULL && *value != '\0')
1524     printf_filtered (_("The target description will be read from \"%s\".\n"),
1525                      value);
1526   else
1527     printf_filtered (_("The target description will be "
1528                        "read from the target.\n"));
1529 }
1530
1531 static void
1532 unset_tdesc_filename_cmd (char *args, int from_tty)
1533 {
1534   xfree (target_description_filename);
1535   target_description_filename = NULL;
1536   target_clear_description ();
1537   target_find_description ();
1538 }
1539
1540 static void
1541 maint_print_c_tdesc_cmd (char *args, int from_tty)
1542 {
1543   const struct target_desc *tdesc;
1544   const struct bfd_arch_info *compatible;
1545   const char *filename, *inp;
1546   char *function, *outp;
1547   struct property *prop;
1548   struct tdesc_feature *feature;
1549   struct tdesc_reg *reg;
1550   struct tdesc_type *type;
1551   struct tdesc_type_field *f;
1552   struct tdesc_type_flag *flag;
1553   int ix, ix2, ix3;
1554
1555   /* Use the global target-supplied description, not the current
1556      architecture's.  This lets a GDB for one architecture generate C
1557      for another architecture's description, even though the gdbarch
1558      initialization code will reject the new description.  */
1559   tdesc = current_target_desc;
1560   if (tdesc == NULL)
1561     error (_("There is no target description to print."));
1562
1563   if (target_description_filename == NULL)
1564     error (_("The current target description did not come from an XML file."));
1565
1566   filename = lbasename (target_description_filename);
1567   function = alloca (strlen (filename) + 1);
1568   for (inp = filename, outp = function; *inp != '\0'; inp++)
1569     if (*inp == '.')
1570       break;
1571     else if (*inp == '-')
1572       *outp++ = '_';
1573     else
1574       *outp++ = *inp;
1575   *outp = '\0';
1576
1577   /* Standard boilerplate.  */
1578   printf_unfiltered ("/* THIS FILE IS GENERATED.  Original: %s */\n\n",
1579                      filename);
1580   printf_unfiltered ("#include \"defs.h\"\n");
1581   printf_unfiltered ("#include \"osabi.h\"\n");
1582   printf_unfiltered ("#include \"target-descriptions.h\"\n");
1583   printf_unfiltered ("\n");
1584
1585   printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1586   printf_unfiltered ("static void\n");
1587   printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1588   printf_unfiltered ("{\n");
1589   printf_unfiltered
1590     ("  struct target_desc *result = allocate_target_description ();\n");
1591   printf_unfiltered ("  struct tdesc_feature *feature;\n");
1592   printf_unfiltered ("  struct tdesc_type *field_type, *type;\n");
1593   printf_unfiltered ("\n");
1594
1595   if (tdesc_architecture (tdesc) != NULL)
1596     {
1597       printf_unfiltered
1598         ("  set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1599          tdesc_architecture (tdesc)->printable_name);
1600       printf_unfiltered ("\n");
1601     }
1602
1603   if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
1604       && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
1605     {
1606       printf_unfiltered
1607         ("  set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1608          gdbarch_osabi_name (tdesc_osabi (tdesc)));
1609       printf_unfiltered ("\n");
1610     }
1611
1612   for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
1613        ix++)
1614     {
1615       printf_unfiltered
1616         ("  tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1617          compatible->printable_name);
1618     }
1619   if (ix)
1620     printf_unfiltered ("\n");
1621
1622   for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1623        ix++)
1624     {
1625       printf_unfiltered ("  set_tdesc_property (result, \"%s\", \"%s\");\n",
1626               prop->key, prop->value);
1627     }
1628
1629   for (ix = 0;
1630        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1631        ix++)
1632     {
1633       printf_unfiltered ("  \
1634 feature = tdesc_create_feature (result, \"%s\");\n",
1635                          feature->name);
1636
1637       for (ix2 = 0;
1638            VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1639            ix2++)
1640         {
1641           switch (type->kind)
1642             {
1643             case TDESC_TYPE_VECTOR:
1644               printf_unfiltered
1645                 ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1646                  type->u.v.type->name);
1647               printf_unfiltered
1648                 ("  tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1649                  type->name, type->u.v.count);
1650               break;
1651             case TDESC_TYPE_UNION:
1652               printf_unfiltered
1653                 ("  type = tdesc_create_union (feature, \"%s\");\n",
1654                  type->name);
1655               for (ix3 = 0;
1656                    VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1657                    ix3++)
1658                 {
1659                   printf_unfiltered
1660                     ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1661                      f->type->name);
1662                   printf_unfiltered
1663                     ("  tdesc_add_field (type, \"%s\", field_type);\n",
1664                      f->name);
1665                 }
1666               break;
1667             case TDESC_TYPE_FLAGS:
1668               printf_unfiltered
1669                 ("  field_type = tdesc_create_flags (feature, \"%s\", %d);\n",
1670                  type->name, (int) type->u.f.size);
1671               for (ix3 = 0;
1672                    VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
1673                                 flag);
1674                    ix3++)
1675                 printf_unfiltered
1676                   ("  tdesc_add_flag (field_type, %d, \"%s\");\n",
1677                    flag->start, flag->name);
1678               break;
1679             default:
1680               error (_("C output is not supported type \"%s\"."), type->name);
1681             }
1682           printf_unfiltered ("\n");
1683         }
1684
1685       for (ix2 = 0;
1686            VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1687            ix2++)
1688         {
1689           printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1690                              reg->name, reg->target_regnum, reg->save_restore);
1691           if (reg->group)
1692             printf_unfiltered ("\"%s\", ", reg->group);
1693           else
1694             printf_unfiltered ("NULL, ");
1695           printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1696         }
1697
1698       printf_unfiltered ("\n");
1699     }
1700
1701   printf_unfiltered ("  tdesc_%s = result;\n", function);
1702   printf_unfiltered ("}\n");
1703 }
1704
1705 /* Provide a prototype to silence -Wmissing-prototypes.  */
1706 extern initialize_file_ftype _initialize_target_descriptions;
1707
1708 void
1709 _initialize_target_descriptions (void)
1710 {
1711   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1712
1713   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1714 Set target description specific variables."),
1715                   &tdesc_set_cmdlist, "set tdesc ",
1716                   0 /* allow-unknown */, &setlist);
1717   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1718 Show target description specific variables."),
1719                   &tdesc_show_cmdlist, "show tdesc ",
1720                   0 /* allow-unknown */, &showlist);
1721   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1722 Unset target description specific variables."),
1723                   &tdesc_unset_cmdlist, "unset tdesc ",
1724                   0 /* allow-unknown */, &unsetlist);
1725
1726   add_setshow_filename_cmd ("filename", class_obscure,
1727                             &target_description_filename,
1728                             _("\
1729 Set the file to read for an XML target description"), _("\
1730 Show the file to read for an XML target description"), _("\
1731 When set, GDB will read the target description from a local\n\
1732 file instead of querying the remote target."),
1733                             set_tdesc_filename_cmd,
1734                             show_tdesc_filename_cmd,
1735                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1736
1737   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1738 Unset the file to read for an XML target description.  When unset,\n\
1739 GDB will read the description from the target."),
1740            &tdesc_unset_cmdlist);
1741
1742   add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1743 Print the current target description as a C source file."),
1744            &maintenanceprintlist);
1745 }