Lazily and dynamically create i386-linux target descriptions
[external/binutils.git] / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3    Copyright (C) 2006-2017 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 #include "osabi.h"
33
34 #include "gdb_obstack.h"
35 #include "hashtab.h"
36 #include "inferior.h"
37 #include <algorithm>
38
39 /* The interface to visit different elements of target description.  */
40
41 class tdesc_element_visitor
42 {
43 public:
44   virtual void visit_pre (const target_desc *e) = 0;
45   virtual void visit_post (const target_desc *e) = 0;
46
47   virtual void visit_pre (const tdesc_feature *e) = 0;
48   virtual void visit_post (const tdesc_feature *e) = 0;
49
50   virtual void visit (const tdesc_type *e) = 0;
51   virtual void visit (const tdesc_reg *e) = 0;
52 };
53
54 class tdesc_element
55 {
56 public:
57   virtual void accept (tdesc_element_visitor &v) const = 0;
58 };
59
60 /* Types.  */
61
62 typedef struct property
63 {
64   char *key;
65   char *value;
66 } property_s;
67 DEF_VEC_O(property_s);
68
69 /* An individual register from a target description.  */
70
71 typedef struct tdesc_reg : tdesc_element
72 {
73   tdesc_reg (struct tdesc_feature *feature, const char *name_,
74              int regnum, int save_restore_, const char *group_,
75              int bitsize_, const char *type_)
76     : name (xstrdup (name_)), target_regnum (regnum),
77       save_restore (save_restore_),
78       group (group_ != NULL ? xstrdup (group_) : NULL),
79       bitsize (bitsize_),
80       type (type_ != NULL ? xstrdup (type_) : xstrdup ("<unknown>"))
81   {
82     /* If the register's type is target-defined, look it up now.  We may not
83        have easy access to the containing feature when we want it later.  */
84     tdesc_type = tdesc_named_type (feature, type);
85   }
86
87   virtual ~tdesc_reg ()
88   {
89     xfree (name);
90     xfree (type);
91     xfree (group);
92   }
93
94   /* Disable copying.  */
95   tdesc_reg (const tdesc_reg &) = delete;
96   tdesc_reg &operator= (const tdesc_reg &) = delete;
97
98   /* The name of this register.  In standard features, it may be
99      recognized by the architecture support code, or it may be purely
100      for the user.  */
101   char *name;
102
103   /* The register number used by this target to refer to this
104      register.  This is used for remote p/P packets and to determine
105      the ordering of registers in the remote g/G packets.  */
106   long target_regnum;
107
108   /* If this flag is set, GDB should save and restore this register
109      around calls to an inferior function.  */
110   int save_restore;
111
112   /* The name of the register group containing this register, or NULL
113      if the group should be automatically determined from the
114      register's type.  If this is "general", "float", or "vector", the
115      corresponding "info" command should display this register's
116      value.  It can be an arbitrary string, but should be limited to
117      alphanumeric characters and internal hyphens.  Currently other
118      strings are ignored (treated as NULL).  */
119   char *group;
120
121   /* The size of the register, in bits.  */
122   int bitsize;
123
124   /* The type of the register.  This string corresponds to either
125      a named type from the target description or a predefined
126      type from GDB.  */
127   char *type;
128
129   /* The target-described type corresponding to TYPE, if found.  */
130   struct tdesc_type *tdesc_type;
131
132   void accept (tdesc_element_visitor &v) const override
133   {
134     v.visit (this);
135   }
136
137 } *tdesc_reg_p;
138 DEF_VEC_P(tdesc_reg_p);
139
140 /* A named type from a target description.  */
141
142 typedef struct tdesc_type_field
143 {
144   char *name;
145   struct tdesc_type *type;
146   /* For non-enum-values, either both are -1 (non-bitfield), or both are
147      not -1 (bitfield).  For enum values, start is the value (which could be
148      -1), end is -1.  */
149   int start, end;
150 } tdesc_type_field;
151 DEF_VEC_O(tdesc_type_field);
152
153 enum tdesc_type_kind
154 {
155   /* Predefined types.  */
156   TDESC_TYPE_BOOL,
157   TDESC_TYPE_INT8,
158   TDESC_TYPE_INT16,
159   TDESC_TYPE_INT32,
160   TDESC_TYPE_INT64,
161   TDESC_TYPE_INT128,
162   TDESC_TYPE_UINT8,
163   TDESC_TYPE_UINT16,
164   TDESC_TYPE_UINT32,
165   TDESC_TYPE_UINT64,
166   TDESC_TYPE_UINT128,
167   TDESC_TYPE_CODE_PTR,
168   TDESC_TYPE_DATA_PTR,
169   TDESC_TYPE_IEEE_SINGLE,
170   TDESC_TYPE_IEEE_DOUBLE,
171   TDESC_TYPE_ARM_FPA_EXT,
172   TDESC_TYPE_I387_EXT,
173
174   /* Types defined by a target feature.  */
175   TDESC_TYPE_VECTOR,
176   TDESC_TYPE_STRUCT,
177   TDESC_TYPE_UNION,
178   TDESC_TYPE_FLAGS,
179   TDESC_TYPE_ENUM
180 };
181
182 typedef struct tdesc_type : tdesc_element
183 {
184   tdesc_type (const char *name_, enum tdesc_type_kind kind_)
185     : name (xstrdup (name_)), kind (kind_)
186   {
187     memset (&u, 0, sizeof (u));
188   }
189
190   virtual ~tdesc_type ()
191   {
192     switch (kind)
193       {
194       case TDESC_TYPE_STRUCT:
195       case TDESC_TYPE_UNION:
196       case TDESC_TYPE_FLAGS:
197       case TDESC_TYPE_ENUM:
198         {
199           struct tdesc_type_field *f;
200           int ix;
201
202           for (ix = 0;
203                VEC_iterate (tdesc_type_field, u.u.fields, ix, f);
204                ix++)
205             xfree (f->name);
206
207           VEC_free (tdesc_type_field, u.u.fields);
208         }
209         break;
210
211       default:
212         break;
213       }
214     xfree ((char *) name);
215   }
216   /* Disable copying.  */
217   tdesc_type (const tdesc_type &) = delete;
218   tdesc_type &operator= (const tdesc_type &) = delete;
219
220   /* The name of this type.  If this type is a built-in type, this is
221      a pointer to a constant string.  Otherwise, it's a
222      malloc-allocated string (and thus must be freed).  */
223   const char *name;
224
225   /* Identify the kind of this type.  */
226   enum tdesc_type_kind kind;
227
228   /* Kind-specific data.  */
229   union
230   {
231     /* Vector type.  */
232     struct
233     {
234       struct tdesc_type *type;
235       int count;
236     } v;
237
238     /* Struct, union, flags, or enum type.  */
239     struct
240     {
241       VEC(tdesc_type_field) *fields;
242       int size;
243     } u;
244   } u;
245
246   void accept (tdesc_element_visitor &v) const override
247   {
248     v.visit (this);
249   }
250
251 } *tdesc_type_p;
252 DEF_VEC_P(tdesc_type_p);
253
254 /* A feature from a target description.  Each feature is a collection
255    of other elements, e.g. registers and types.  */
256
257 typedef struct tdesc_feature : tdesc_element
258 {
259   tdesc_feature (const char *name_)
260     : name (xstrdup (name_))
261   {}
262
263   virtual ~tdesc_feature ()
264   {
265     struct tdesc_reg *reg;
266     struct tdesc_type *type;
267     int ix;
268
269     for (ix = 0; VEC_iterate (tdesc_reg_p, registers, ix, reg); ix++)
270       delete reg;
271     VEC_free (tdesc_reg_p, registers);
272
273     for (ix = 0; VEC_iterate (tdesc_type_p, types, ix, type); ix++)
274       delete type;
275     VEC_free (tdesc_type_p, types);
276
277     xfree (name);
278   }
279
280   /* Disable copying.  */
281   tdesc_feature (const tdesc_feature &) = delete;
282   tdesc_feature &operator= (const tdesc_feature &) = delete;
283
284   /* The name of this feature.  It may be recognized by the architecture
285      support code.  */
286   char *name;
287
288   /* The registers associated with this feature.  */
289   VEC(tdesc_reg_p) *registers = NULL;
290
291   /* The types associated with this feature.  */
292   VEC(tdesc_type_p) *types = NULL;
293
294   void accept (tdesc_element_visitor &v) const override
295   {
296     v.visit_pre (this);
297
298     struct tdesc_type *type;
299
300     for (int ix = 0;
301          VEC_iterate (tdesc_type_p, types, ix, type);
302          ix++)
303       type->accept (v);
304
305     struct tdesc_reg *reg;
306
307     for (int ix = 0;
308          VEC_iterate (tdesc_reg_p, registers, ix, reg);
309          ix++)
310       reg->accept (v);
311
312
313     v.visit_post (this);
314   }
315 } *tdesc_feature_p;
316 DEF_VEC_P(tdesc_feature_p);
317
318 /* A compatible architecture from a target description.  */
319 typedef const struct bfd_arch_info *arch_p;
320 DEF_VEC_P(arch_p);
321
322 /* A target description.  */
323
324 struct target_desc : tdesc_element
325 {
326   target_desc ()
327   {}
328
329   virtual ~target_desc ()
330   {
331     struct tdesc_feature *feature;
332     struct property *prop;
333     int ix;
334
335     for (ix = 0;
336          VEC_iterate (tdesc_feature_p, features, ix, feature);
337          ix++)
338       delete feature;
339     VEC_free (tdesc_feature_p, features);
340
341     for (ix = 0;
342          VEC_iterate (property_s, properties, ix, prop);
343          ix++)
344       {
345         xfree (prop->key);
346         xfree (prop->value);
347       }
348
349     VEC_free (property_s, properties);
350     VEC_free (arch_p, compatible);
351   }
352
353   target_desc (const target_desc &) = delete;
354   void operator= (const target_desc &) = delete;
355
356   /* The architecture reported by the target, if any.  */
357   const struct bfd_arch_info *arch = NULL;
358
359   /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
360      otherwise.  */
361   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
362
363   /* The list of compatible architectures reported by the target.  */
364   VEC(arch_p) *compatible = NULL;
365
366   /* Any architecture-specific properties specified by the target.  */
367   VEC(property_s) *properties = NULL;
368
369   /* The features associated with this target.  */
370   VEC(tdesc_feature_p) *features = NULL;
371
372   void accept (tdesc_element_visitor &v) const override
373   {
374     v.visit_pre (this);
375
376     struct tdesc_feature *feature;
377
378     for (int ix = 0;
379          VEC_iterate (tdesc_feature_p, features, ix, feature);
380          ix++)
381       feature->accept (v);
382
383     v.visit_post (this);
384   }
385 };
386
387 /* Per-architecture data associated with a target description.  The
388    target description may be shared by multiple architectures, but
389    this data is private to one gdbarch.  */
390
391 typedef struct tdesc_arch_reg
392 {
393   struct tdesc_reg *reg;
394   struct type *type;
395 } tdesc_arch_reg;
396 DEF_VEC_O(tdesc_arch_reg);
397
398 struct tdesc_arch_data
399 {
400   /* A list of register/type pairs, indexed by GDB's internal register number.
401      During initialization of the gdbarch this list is used to store
402      registers which the architecture assigns a fixed register number.
403      Registers which are NULL in this array, or off the end, are
404      treated as zero-sized and nameless (i.e. placeholders in the
405      numbering).  */
406   VEC(tdesc_arch_reg) *arch_regs;
407
408   /* Functions which report the register name, type, and reggroups for
409      pseudo-registers.  */
410   gdbarch_register_name_ftype *pseudo_register_name;
411   gdbarch_register_type_ftype *pseudo_register_type;
412   gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
413 };
414
415 /* Info about an inferior's target description.  There's one of these
416    for each inferior.  */
417
418 struct target_desc_info
419 {
420   /* A flag indicating that a description has already been fetched
421      from the target, so it should not be queried again.  */
422
423   int fetched;
424
425   /* The description fetched from the target, or NULL if the target
426      did not supply any description.  Only valid when
427      target_desc_fetched is set.  Only the description initialization
428      code should access this; normally, the description should be
429      accessed through the gdbarch object.  */
430
431   const struct target_desc *tdesc;
432
433   /* The filename to read a target description from, as set by "set
434      tdesc filename ..."  */
435
436   char *filename;
437 };
438
439 /* Get the inferior INF's target description info, allocating one on
440    the stop if necessary.  */
441
442 static struct target_desc_info *
443 get_tdesc_info (struct inferior *inf)
444 {
445   if (inf->tdesc_info == NULL)
446     inf->tdesc_info = XCNEW (struct target_desc_info);
447   return inf->tdesc_info;
448 }
449
450 /* A handle for architecture-specific data associated with the
451    target description (see struct tdesc_arch_data).  */
452
453 static struct gdbarch_data *tdesc_data;
454
455 /* See target-descriptions.h.  */
456
457 int
458 target_desc_info_from_user_p (struct target_desc_info *info)
459 {
460   return info != NULL && info->filename != NULL;
461 }
462
463 /* See target-descriptions.h.  */
464
465 void
466 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
467 {
468   struct target_desc_info *src = get_tdesc_info (srcinf);
469   struct target_desc_info *dest = get_tdesc_info (destinf);
470
471   dest->fetched = src->fetched;
472   dest->tdesc = src->tdesc;
473   dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
474 }
475
476 /* See target-descriptions.h.  */
477
478 void
479 target_desc_info_free (struct target_desc_info *tdesc_info)
480 {
481   if (tdesc_info != NULL)
482     {
483       xfree (tdesc_info->filename);
484       xfree (tdesc_info);
485     }
486 }
487
488 /* Convenience helper macros.  */
489
490 #define target_desc_fetched \
491   get_tdesc_info (current_inferior ())->fetched
492 #define current_target_desc \
493   get_tdesc_info (current_inferior ())->tdesc
494 #define target_description_filename \
495   get_tdesc_info (current_inferior ())->filename
496
497 /* The string manipulated by the "set tdesc filename ..." command.  */
498
499 static char *tdesc_filename_cmd_string;
500
501 /* Fetch the current target's description, and switch the current
502    architecture to one which incorporates that description.  */
503
504 void
505 target_find_description (void)
506 {
507   /* If we've already fetched a description from the target, don't do
508      it again.  This allows a target to fetch the description early,
509      during its to_open or to_create_inferior, if it needs extra
510      information about the target to initialize.  */
511   if (target_desc_fetched)
512     return;
513
514   /* The current architecture should not have any target description
515      specified.  It should have been cleared, e.g. when we
516      disconnected from the previous target.  */
517   gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
518
519   /* First try to fetch an XML description from the user-specified
520      file.  */
521   current_target_desc = NULL;
522   if (target_description_filename != NULL
523       && *target_description_filename != '\0')
524     current_target_desc
525       = file_read_description_xml (target_description_filename);
526
527   /* Next try to read the description from the current target using
528      target objects.  */
529   if (current_target_desc == NULL)
530     current_target_desc = target_read_description_xml (&current_target);
531
532   /* If that failed try a target-specific hook.  */
533   if (current_target_desc == NULL)
534     current_target_desc = target_read_description (&current_target);
535
536   /* If a non-NULL description was returned, then update the current
537      architecture.  */
538   if (current_target_desc)
539     {
540       struct gdbarch_info info;
541
542       gdbarch_info_init (&info);
543       info.target_desc = current_target_desc;
544       if (!gdbarch_update_p (info))
545         warning (_("Architecture rejected target-supplied description"));
546       else
547         {
548           struct tdesc_arch_data *data;
549
550           data = ((struct tdesc_arch_data *)
551                   gdbarch_data (target_gdbarch (), tdesc_data));
552           if (tdesc_has_registers (current_target_desc)
553               && data->arch_regs == NULL)
554             warning (_("Target-supplied registers are not supported "
555                        "by the current architecture"));
556         }
557     }
558
559   /* Now that we know this description is usable, record that we
560      fetched it.  */
561   target_desc_fetched = 1;
562 }
563
564 /* Discard any description fetched from the current target, and switch
565    the current architecture to one with no target description.  */
566
567 void
568 target_clear_description (void)
569 {
570   struct gdbarch_info info;
571
572   if (!target_desc_fetched)
573     return;
574
575   target_desc_fetched = 0;
576   current_target_desc = NULL;
577
578   gdbarch_info_init (&info);
579   if (!gdbarch_update_p (info))
580     internal_error (__FILE__, __LINE__,
581                     _("Could not remove target-supplied description"));
582 }
583
584 /* Return the global current target description.  This should only be
585    used by gdbarch initialization code; most access should be through
586    an existing gdbarch.  */
587
588 const struct target_desc *
589 target_current_description (void)
590 {
591   if (target_desc_fetched)
592     return current_target_desc;
593
594   return NULL;
595 }
596
597 /* Return non-zero if this target description is compatible
598    with the given BFD architecture.  */
599
600 int
601 tdesc_compatible_p (const struct target_desc *target_desc,
602                     const struct bfd_arch_info *arch)
603 {
604   const struct bfd_arch_info *compat;
605   int ix;
606
607   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
608        ix++)
609     {
610       if (compat == arch
611           || arch->compatible (arch, compat)
612           || compat->compatible (compat, arch))
613         return 1;
614     }
615
616   return 0;
617 }
618 \f
619
620 /* Direct accessors for target descriptions.  */
621
622 /* Return the string value of a property named KEY, or NULL if the
623    property was not specified.  */
624
625 const char *
626 tdesc_property (const struct target_desc *target_desc, const char *key)
627 {
628   struct property *prop;
629   int ix;
630
631   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
632        ix++)
633     if (strcmp (prop->key, key) == 0)
634       return prop->value;
635
636   return NULL;
637 }
638
639 /* Return the BFD architecture associated with this target
640    description, or NULL if no architecture was specified.  */
641
642 const struct bfd_arch_info *
643 tdesc_architecture (const struct target_desc *target_desc)
644 {
645   return target_desc->arch;
646 }
647
648 /* Return the OSABI associated with this target description, or
649    GDB_OSABI_UNKNOWN if no osabi was specified.  */
650
651 enum gdb_osabi
652 tdesc_osabi (const struct target_desc *target_desc)
653 {
654   return target_desc->osabi;
655 }
656
657 \f
658
659 /* Return 1 if this target description includes any registers.  */
660
661 int
662 tdesc_has_registers (const struct target_desc *target_desc)
663 {
664   int ix;
665   struct tdesc_feature *feature;
666
667   if (target_desc == NULL)
668     return 0;
669
670   for (ix = 0;
671        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
672        ix++)
673     if (! VEC_empty (tdesc_reg_p, feature->registers))
674       return 1;
675
676   return 0;
677 }
678
679 /* Return the feature with the given name, if present, or NULL if
680    the named feature is not found.  */
681
682 const struct tdesc_feature *
683 tdesc_find_feature (const struct target_desc *target_desc,
684                     const char *name)
685 {
686   int ix;
687   struct tdesc_feature *feature;
688
689   for (ix = 0;
690        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
691        ix++)
692     if (strcmp (feature->name, name) == 0)
693       return feature;
694
695   return NULL;
696 }
697
698 /* Return the name of FEATURE.  */
699
700 const char *
701 tdesc_feature_name (const struct tdesc_feature *feature)
702 {
703   return feature->name;
704 }
705
706 /* Predefined types.  */
707 static struct tdesc_type tdesc_predefined_types[] =
708 {
709   { "bool", TDESC_TYPE_BOOL },
710   { "int8", TDESC_TYPE_INT8 },
711   { "int16", TDESC_TYPE_INT16 },
712   { "int32", TDESC_TYPE_INT32 },
713   { "int64", TDESC_TYPE_INT64 },
714   { "int128", TDESC_TYPE_INT128 },
715   { "uint8", TDESC_TYPE_UINT8 },
716   { "uint16", TDESC_TYPE_UINT16 },
717   { "uint32", TDESC_TYPE_UINT32 },
718   { "uint64", TDESC_TYPE_UINT64 },
719   { "uint128", TDESC_TYPE_UINT128 },
720   { "code_ptr", TDESC_TYPE_CODE_PTR },
721   { "data_ptr", TDESC_TYPE_DATA_PTR },
722   { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
723   { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
724   { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
725   { "i387_ext", TDESC_TYPE_I387_EXT }
726 };
727
728 /* Lookup a predefined type.  */
729
730 static struct tdesc_type *
731 tdesc_predefined_type (enum tdesc_type_kind kind)
732 {
733   int ix;
734   struct tdesc_type *type;
735
736   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
737     if (tdesc_predefined_types[ix].kind == kind)
738       return &tdesc_predefined_types[ix];
739
740   gdb_assert_not_reached ("bad predefined tdesc type");
741 }
742
743 /* Return the type associated with ID in the context of FEATURE, or
744    NULL if none.  */
745
746 struct tdesc_type *
747 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
748 {
749   int ix;
750   struct tdesc_type *type;
751
752   /* First try target-defined types.  */
753   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
754     if (strcmp (type->name, id) == 0)
755       return type;
756
757   /* Next try the predefined types.  */
758   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
759     if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
760       return &tdesc_predefined_types[ix];
761
762   return NULL;
763 }
764
765 /* Lookup type associated with ID.  */
766
767 struct type *
768 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
769 {
770   struct tdesc_arch_reg *reg;
771   struct tdesc_arch_data *data;
772   int i, num_regs;
773
774   data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
775   num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
776   for (i = 0; i < num_regs; i++)
777     {
778       reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
779       if (reg->reg
780           && reg->reg->tdesc_type
781           && reg->type
782           && strcmp (id, reg->reg->tdesc_type->name) == 0)
783         return reg->type;
784     }
785
786   return NULL;
787 }
788
789 /* Construct, if necessary, and return the GDB type implementing target
790    type TDESC_TYPE for architecture GDBARCH.  */
791
792 static struct type *
793 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
794 {
795   struct type *type;
796
797   switch (tdesc_type->kind)
798     {
799     /* Predefined types.  */
800     case TDESC_TYPE_BOOL:
801       return builtin_type (gdbarch)->builtin_bool;
802
803     case TDESC_TYPE_INT8:
804       return builtin_type (gdbarch)->builtin_int8;
805
806     case TDESC_TYPE_INT16:
807       return builtin_type (gdbarch)->builtin_int16;
808
809     case TDESC_TYPE_INT32:
810       return builtin_type (gdbarch)->builtin_int32;
811
812     case TDESC_TYPE_INT64:
813       return builtin_type (gdbarch)->builtin_int64;
814
815     case TDESC_TYPE_INT128:
816       return builtin_type (gdbarch)->builtin_int128;
817
818     case TDESC_TYPE_UINT8:
819       return builtin_type (gdbarch)->builtin_uint8;
820
821     case TDESC_TYPE_UINT16:
822       return builtin_type (gdbarch)->builtin_uint16;
823
824     case TDESC_TYPE_UINT32:
825       return builtin_type (gdbarch)->builtin_uint32;
826
827     case TDESC_TYPE_UINT64:
828       return builtin_type (gdbarch)->builtin_uint64;
829
830     case TDESC_TYPE_UINT128:
831       return builtin_type (gdbarch)->builtin_uint128;
832
833     case TDESC_TYPE_CODE_PTR:
834       return builtin_type (gdbarch)->builtin_func_ptr;
835
836     case TDESC_TYPE_DATA_PTR:
837       return builtin_type (gdbarch)->builtin_data_ptr;
838
839     default:
840       break;
841     }
842
843   type = tdesc_find_type (gdbarch, tdesc_type->name);
844   if (type)
845     return type;
846
847   switch (tdesc_type->kind)
848     {
849     case TDESC_TYPE_IEEE_SINGLE:
850       return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
851                               floatformats_ieee_single);
852
853     case TDESC_TYPE_IEEE_DOUBLE:
854       return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
855                               floatformats_ieee_double);
856
857     case TDESC_TYPE_ARM_FPA_EXT:
858       return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
859                               floatformats_arm_ext);
860
861     case TDESC_TYPE_I387_EXT:
862       return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
863                               floatformats_i387_ext);
864
865     /* Types defined by a target feature.  */
866     case TDESC_TYPE_VECTOR:
867       {
868         struct type *type, *field_type;
869
870         field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
871         type = init_vector_type (field_type, tdesc_type->u.v.count);
872         TYPE_NAME (type) = xstrdup (tdesc_type->name);
873
874         return type;
875       }
876
877     case TDESC_TYPE_STRUCT:
878       {
879         struct type *type, *field_type;
880         struct tdesc_type_field *f;
881         int ix;
882
883         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
884         TYPE_NAME (type) = xstrdup (tdesc_type->name);
885         TYPE_TAG_NAME (type) = TYPE_NAME (type);
886
887         for (ix = 0;
888              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
889              ix++)
890           {
891             if (f->start != -1 && f->end != -1)
892               {
893                 /* Bitfield.  */
894                 struct field *fld;
895                 struct type *field_type;
896                 int bitsize, total_size;
897
898                 /* This invariant should be preserved while creating types.  */
899                 gdb_assert (tdesc_type->u.u.size != 0);
900                 if (f->type != NULL)
901                   field_type = tdesc_gdb_type (gdbarch, f->type);
902                 else if (tdesc_type->u.u.size > 4)
903                   field_type = builtin_type (gdbarch)->builtin_uint64;
904                 else
905                   field_type = builtin_type (gdbarch)->builtin_uint32;
906
907                 fld = append_composite_type_field_raw (type, xstrdup (f->name),
908                                                        field_type);
909
910                 /* For little-endian, BITPOS counts from the LSB of
911                    the structure and marks the LSB of the field.  For
912                    big-endian, BITPOS counts from the MSB of the
913                    structure and marks the MSB of the field.  Either
914                    way, it is the number of bits to the "left" of the
915                    field.  To calculate this in big-endian, we need
916                    the total size of the structure.  */
917                 bitsize = f->end - f->start + 1;
918                 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
919                 if (gdbarch_bits_big_endian (gdbarch))
920                   SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
921                 else
922                   SET_FIELD_BITPOS (fld[0], f->start);
923                 FIELD_BITSIZE (fld[0]) = bitsize;
924               }
925             else
926               {
927                 gdb_assert (f->start == -1 && f->end == -1);
928                 field_type = tdesc_gdb_type (gdbarch, f->type);
929                 append_composite_type_field (type, xstrdup (f->name),
930                                              field_type);
931               }
932           }
933
934         if (tdesc_type->u.u.size != 0)
935           TYPE_LENGTH (type) = tdesc_type->u.u.size;
936         return type;
937       }
938
939     case TDESC_TYPE_UNION:
940       {
941         struct type *type, *field_type;
942         struct tdesc_type_field *f;
943         int ix;
944
945         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
946         TYPE_NAME (type) = xstrdup (tdesc_type->name);
947
948         for (ix = 0;
949              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
950              ix++)
951           {
952             field_type = tdesc_gdb_type (gdbarch, f->type);
953             append_composite_type_field (type, xstrdup (f->name), field_type);
954
955             /* If any of the children of a union are vectors, flag the
956                union as a vector also.  This allows e.g. a union of two
957                vector types to show up automatically in "info vector".  */
958             if (TYPE_VECTOR (field_type))
959               TYPE_VECTOR (type) = 1;
960           }
961         return type;
962       }
963
964     case TDESC_TYPE_FLAGS:
965       {
966         struct tdesc_type_field *f;
967         int ix;
968
969         type = arch_flags_type (gdbarch, tdesc_type->name,
970                                 tdesc_type->u.u.size);
971         for (ix = 0;
972              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
973              ix++)
974           {
975             struct type *field_type;
976             int bitsize = f->end - f->start + 1;
977
978             gdb_assert (f->type != NULL);
979             field_type = tdesc_gdb_type (gdbarch, f->type);
980             append_flags_type_field (type, f->start, bitsize,
981                                      field_type, f->name);
982           }
983
984         return type;
985       }
986
987     case TDESC_TYPE_ENUM:
988       {
989         struct tdesc_type_field *f;
990         int ix;
991
992         type = arch_type (gdbarch, TYPE_CODE_ENUM,
993                           tdesc_type->u.u.size, tdesc_type->name);
994         TYPE_UNSIGNED (type) = 1;
995         for (ix = 0;
996              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
997              ix++)
998           {
999             struct field *fld
1000               = append_composite_type_field_raw (type, xstrdup (f->name),
1001                                                  NULL);
1002
1003             SET_FIELD_BITPOS (fld[0], f->start);
1004           }
1005
1006         return type;
1007       }
1008     }
1009
1010   internal_error (__FILE__, __LINE__,
1011                   "Type \"%s\" has an unknown kind %d",
1012                   tdesc_type->name, tdesc_type->kind);
1013 }
1014 \f
1015
1016 /* Support for registers from target descriptions.  */
1017
1018 /* Construct the per-gdbarch data.  */
1019
1020 static void *
1021 tdesc_data_init (struct obstack *obstack)
1022 {
1023   struct tdesc_arch_data *data;
1024
1025   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1026   return data;
1027 }
1028
1029 /* Similar, but for the temporary copy used during architecture
1030    initialization.  */
1031
1032 struct tdesc_arch_data *
1033 tdesc_data_alloc (void)
1034 {
1035   return XCNEW (struct tdesc_arch_data);
1036 }
1037
1038 /* Free something allocated by tdesc_data_alloc, if it is not going
1039    to be used (for instance if it was unsuitable for the
1040    architecture).  */
1041
1042 void
1043 tdesc_data_cleanup (void *data_untyped)
1044 {
1045   struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
1046
1047   VEC_free (tdesc_arch_reg, data->arch_regs);
1048   xfree (data);
1049 }
1050
1051 /* Search FEATURE for a register named NAME.  */
1052
1053 static struct tdesc_reg *
1054 tdesc_find_register_early (const struct tdesc_feature *feature,
1055                            const char *name)
1056 {
1057   int ixr;
1058   struct tdesc_reg *reg;
1059
1060   for (ixr = 0;
1061        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1062        ixr++)
1063     if (strcasecmp (reg->name, name) == 0)
1064       return reg;
1065
1066   return NULL;
1067 }
1068
1069 /* Search FEATURE for a register named NAME.  Assign REGNO to it.  */
1070
1071 int
1072 tdesc_numbered_register (const struct tdesc_feature *feature,
1073                          struct tdesc_arch_data *data,
1074                          int regno, const char *name)
1075 {
1076   struct tdesc_arch_reg arch_reg = { 0 };
1077   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1078
1079   if (reg == NULL)
1080     return 0;
1081
1082   /* Make sure the vector includes a REGNO'th element.  */
1083   while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1084     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1085
1086   arch_reg.reg = reg;
1087   VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
1088   return 1;
1089 }
1090
1091 /* Search FEATURE for a register named NAME, but do not assign a fixed
1092    register number to it.  */
1093
1094 int
1095 tdesc_unnumbered_register (const struct tdesc_feature *feature,
1096                            const char *name)
1097 {
1098   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1099
1100   if (reg == NULL)
1101     return 0;
1102
1103   return 1;
1104 }
1105
1106 /* Search FEATURE for a register whose name is in NAMES and assign
1107    REGNO to it.  */
1108
1109 int
1110 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1111                                  struct tdesc_arch_data *data,
1112                                  int regno, const char *const names[])
1113 {
1114   int i;
1115
1116   for (i = 0; names[i] != NULL; i++)
1117     if (tdesc_numbered_register (feature, data, regno, names[i]))
1118       return 1;
1119
1120   return 0;
1121 }
1122
1123 /* Search FEATURE for a register named NAME, and return its size in
1124    bits.  The register must exist.  */
1125
1126 int
1127 tdesc_register_size (const struct tdesc_feature *feature,
1128                      const char *name)
1129 {
1130   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1131
1132   gdb_assert (reg != NULL);
1133   return reg->bitsize;
1134 }
1135
1136 /* Look up a register by its GDB internal register number.  */
1137
1138 static struct tdesc_arch_reg *
1139 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
1140 {
1141   struct tdesc_arch_data *data;
1142
1143   data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1144   if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1145     return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
1146   else
1147     return NULL;
1148 }
1149
1150 static struct tdesc_reg *
1151 tdesc_find_register (struct gdbarch *gdbarch, int regno)
1152 {
1153   struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
1154
1155   return reg? reg->reg : NULL;
1156 }
1157
1158 /* Return the name of register REGNO, from the target description or
1159    from an architecture-provided pseudo_register_name method.  */
1160
1161 const char *
1162 tdesc_register_name (struct gdbarch *gdbarch, int regno)
1163 {
1164   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1165   int num_regs = gdbarch_num_regs (gdbarch);
1166   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1167
1168   if (reg != NULL)
1169     return reg->name;
1170
1171   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1172     {
1173       struct tdesc_arch_data *data
1174         = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1175
1176       gdb_assert (data->pseudo_register_name != NULL);
1177       return data->pseudo_register_name (gdbarch, regno);
1178     }
1179
1180   return "";
1181 }
1182
1183 struct type *
1184 tdesc_register_type (struct gdbarch *gdbarch, int regno)
1185 {
1186   struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1187   struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
1188   int num_regs = gdbarch_num_regs (gdbarch);
1189   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1190
1191   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1192     {
1193       struct tdesc_arch_data *data
1194         = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1195
1196       gdb_assert (data->pseudo_register_type != NULL);
1197       return data->pseudo_register_type (gdbarch, regno);
1198     }
1199
1200   if (reg == NULL)
1201     /* Return "int0_t", since "void" has a misleading size of one.  */
1202     return builtin_type (gdbarch)->builtin_int0;
1203
1204   if (arch_reg->type == NULL)
1205     {
1206       /* First check for a predefined or target defined type.  */
1207       if (reg->tdesc_type)
1208         arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1209
1210       /* Next try size-sensitive type shortcuts.  */
1211       else if (strcmp (reg->type, "float") == 0)
1212         {
1213           if (reg->bitsize == gdbarch_float_bit (gdbarch))
1214             arch_reg->type = builtin_type (gdbarch)->builtin_float;
1215           else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1216             arch_reg->type = builtin_type (gdbarch)->builtin_double;
1217           else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1218             arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1219           else
1220             {
1221               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1222                        reg->name, reg->bitsize);
1223               arch_reg->type = builtin_type (gdbarch)->builtin_double;
1224             }
1225         }
1226       else if (strcmp (reg->type, "int") == 0)
1227         {
1228           if (reg->bitsize == gdbarch_long_bit (gdbarch))
1229             arch_reg->type = builtin_type (gdbarch)->builtin_long;
1230           else if (reg->bitsize == TARGET_CHAR_BIT)
1231             arch_reg->type = builtin_type (gdbarch)->builtin_char;
1232           else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1233             arch_reg->type = builtin_type (gdbarch)->builtin_short;
1234           else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1235             arch_reg->type = builtin_type (gdbarch)->builtin_int;
1236           else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1237             arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1238           else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1239           /* A bit desperate by this point...  */
1240             arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1241           else
1242             {
1243               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1244                        reg->name, reg->bitsize);
1245               arch_reg->type = builtin_type (gdbarch)->builtin_long;
1246             }
1247         }
1248
1249       if (arch_reg->type == NULL)
1250         internal_error (__FILE__, __LINE__,
1251                         "Register \"%s\" has an unknown type \"%s\"",
1252                         reg->name, reg->type);
1253     }
1254
1255   return arch_reg->type;
1256 }
1257
1258 static int
1259 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1260 {
1261   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1262
1263   if (reg != NULL)
1264     return reg->target_regnum;
1265   else
1266     return -1;
1267 }
1268
1269 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
1270    target description may be classified as general, float, or vector.
1271    Unlike a gdbarch register_reggroup_p method, this function will
1272    return -1 if it does not know; the caller should handle registers
1273    with no specified group.
1274
1275    Arbitrary strings (other than "general", "float", and "vector")
1276    from the description are not used; they cause the register to be
1277    displayed in "info all-registers" but excluded from "info
1278    registers" et al.  The names of containing features are also not
1279    used.  This might be extended to display registers in some more
1280    useful groupings.
1281
1282    The save-restore flag is also implemented here.  */
1283
1284 int
1285 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1286                               struct reggroup *reggroup)
1287 {
1288   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1289
1290   if (reg != NULL && reg->group != NULL)
1291     {
1292       int general_p = 0, float_p = 0, vector_p = 0;
1293
1294       if (strcmp (reg->group, "general") == 0)
1295         general_p = 1;
1296       else if (strcmp (reg->group, "float") == 0)
1297         float_p = 1;
1298       else if (strcmp (reg->group, "vector") == 0)
1299         vector_p = 1;
1300
1301       if (reggroup == float_reggroup)
1302         return float_p;
1303
1304       if (reggroup == vector_reggroup)
1305         return vector_p;
1306
1307       if (reggroup == general_reggroup)
1308         return general_p;
1309     }
1310
1311   if (reg != NULL
1312       && (reggroup == save_reggroup || reggroup == restore_reggroup))
1313     return reg->save_restore;
1314
1315   return -1;
1316 }
1317
1318 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
1319    group specified go to the default reggroup function and are handled
1320    by type.  */
1321
1322 static int
1323 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1324                            struct reggroup *reggroup)
1325 {
1326   int num_regs = gdbarch_num_regs (gdbarch);
1327   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1328   int ret;
1329
1330   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1331     {
1332       struct tdesc_arch_data *data
1333         = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1334
1335       if (data->pseudo_register_reggroup_p != NULL)
1336         return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1337       /* Otherwise fall through to the default reggroup_p.  */
1338     }
1339
1340   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1341   if (ret != -1)
1342     return ret;
1343
1344   return default_register_reggroup_p (gdbarch, regno, reggroup);
1345 }
1346
1347 /* Record architecture-specific functions to call for pseudo-register
1348    support.  */
1349
1350 void
1351 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1352                                 gdbarch_register_name_ftype *pseudo_name)
1353 {
1354   struct tdesc_arch_data *data
1355     = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1356
1357   data->pseudo_register_name = pseudo_name;
1358 }
1359
1360 void
1361 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1362                                 gdbarch_register_type_ftype *pseudo_type)
1363 {
1364   struct tdesc_arch_data *data
1365     = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1366
1367   data->pseudo_register_type = pseudo_type;
1368 }
1369
1370 void
1371 set_tdesc_pseudo_register_reggroup_p
1372   (struct gdbarch *gdbarch,
1373    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1374 {
1375   struct tdesc_arch_data *data
1376     = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1377
1378   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1379 }
1380
1381 /* Update GDBARCH to use the target description for registers.  */
1382
1383 void
1384 tdesc_use_registers (struct gdbarch *gdbarch,
1385                      const struct target_desc *target_desc,
1386                      struct tdesc_arch_data *early_data)
1387 {
1388   int num_regs = gdbarch_num_regs (gdbarch);
1389   int ixf, ixr;
1390   struct tdesc_feature *feature;
1391   struct tdesc_reg *reg;
1392   struct tdesc_arch_data *data;
1393   struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1394   htab_t reg_hash;
1395
1396   /* We can't use the description for registers if it doesn't describe
1397      any.  This function should only be called after validating
1398      registers, so the caller should know that registers are
1399      included.  */
1400   gdb_assert (tdesc_has_registers (target_desc));
1401
1402   data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1403   data->arch_regs = early_data->arch_regs;
1404   xfree (early_data);
1405
1406   /* Build up a set of all registers, so that we can assign register
1407      numbers where needed.  The hash table expands as necessary, so
1408      the initial size is arbitrary.  */
1409   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1410   for (ixf = 0;
1411        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1412        ixf++)
1413     for (ixr = 0;
1414          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1415          ixr++)
1416       {
1417         void **slot = htab_find_slot (reg_hash, reg, INSERT);
1418
1419         *slot = reg;
1420       }
1421
1422   /* Remove any registers which were assigned numbers by the
1423      architecture.  */
1424   for (ixr = 0;
1425        VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1426        ixr++)
1427     if (arch_reg->reg)
1428       htab_remove_elt (reg_hash, arch_reg->reg);
1429
1430   /* Assign numbers to the remaining registers and add them to the
1431      list of registers.  The new numbers are always above gdbarch_num_regs.
1432      Iterate over the features, not the hash table, so that the order
1433      matches that in the target description.  */
1434
1435   gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1436   while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1437     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1438   for (ixf = 0;
1439        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1440        ixf++)
1441     for (ixr = 0;
1442          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1443          ixr++)
1444       if (htab_find (reg_hash, reg) != NULL)
1445         {
1446           new_arch_reg.reg = reg;
1447           VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1448           num_regs++;
1449         }
1450
1451   htab_delete (reg_hash);
1452
1453   /* Update the architecture.  */
1454   set_gdbarch_num_regs (gdbarch, num_regs);
1455   set_gdbarch_register_name (gdbarch, tdesc_register_name);
1456   set_gdbarch_register_type (gdbarch, tdesc_register_type);
1457   set_gdbarch_remote_register_number (gdbarch,
1458                                       tdesc_remote_register_number);
1459   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1460 }
1461 \f
1462
1463 void
1464 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1465                   int regnum, int save_restore, const char *group,
1466                   int bitsize, const char *type)
1467 {
1468   tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1469                                   group, bitsize, type);
1470
1471   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1472 }
1473
1474 struct tdesc_type *
1475 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1476                      struct tdesc_type *field_type, int count)
1477 {
1478   struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
1479
1480   type->u.v.type = field_type;
1481   type->u.v.count = count;
1482
1483   VEC_safe_push (tdesc_type_p, feature->types, type);
1484   return type;
1485 }
1486
1487 struct tdesc_type *
1488 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1489 {
1490   struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
1491
1492   VEC_safe_push (tdesc_type_p, feature->types, type);
1493   return type;
1494 }
1495
1496 /* Set the total length of TYPE.  Structs which contain bitfields may
1497    omit the reserved bits, so the end of the last field may not
1498    suffice.  */
1499
1500 void
1501 tdesc_set_struct_size (struct tdesc_type *type, int size)
1502 {
1503   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1504   gdb_assert (size > 0);
1505   type->u.u.size = size;
1506 }
1507
1508 struct tdesc_type *
1509 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1510 {
1511   struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
1512
1513   VEC_safe_push (tdesc_type_p, feature->types, type);
1514   return type;
1515 }
1516
1517 struct tdesc_type *
1518 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1519                     int size)
1520 {
1521   struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
1522
1523   gdb_assert (size > 0);
1524
1525   type->u.u.size = size;
1526
1527   VEC_safe_push (tdesc_type_p, feature->types, type);
1528   return type;
1529 }
1530
1531 struct tdesc_type *
1532 tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1533                    int size)
1534 {
1535   struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
1536
1537   gdb_assert (size > 0);
1538
1539   type->u.u.size = size;
1540
1541   VEC_safe_push (tdesc_type_p, feature->types, type);
1542   return type;
1543 }
1544
1545 /* Add a new field to TYPE.  */
1546
1547 void
1548 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1549                  struct tdesc_type *field_type)
1550 {
1551   struct tdesc_type_field f = { 0 };
1552
1553   gdb_assert (type->kind == TDESC_TYPE_UNION
1554               || type->kind == TDESC_TYPE_STRUCT);
1555
1556   f.name = xstrdup (field_name);
1557   f.type = field_type;
1558   /* Initialize these values so we know this is not a bit-field
1559      when we print-c-tdesc.  */
1560   f.start = -1;
1561   f.end = -1;
1562
1563   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1564 }
1565
1566 /* Add a new typed bitfield to TYPE.  */
1567
1568 void
1569 tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1570                           int start, int end, struct tdesc_type *field_type)
1571 {
1572   struct tdesc_type_field f = { 0 };
1573
1574   gdb_assert (type->kind == TDESC_TYPE_STRUCT
1575               || type->kind == TDESC_TYPE_FLAGS);
1576   gdb_assert (start >= 0 && end >= start);
1577
1578   f.name = xstrdup (field_name);
1579   f.start = start;
1580   f.end = end;
1581   f.type = field_type;
1582
1583   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1584 }
1585
1586 /* Add a new untyped bitfield to TYPE.
1587    Untyped bitfields become either uint32 or uint64 depending on the size
1588    of the underlying type.  */
1589
1590 void
1591 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1592                     int start, int end)
1593 {
1594   struct tdesc_type *field_type;
1595
1596   gdb_assert (start >= 0 && end >= start);
1597
1598   if (type->u.u.size > 4)
1599     field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1600   else
1601     field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1602
1603   tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1604 }
1605
1606 /* A flag is just a typed(bool) single-bit bitfield.
1607    This function is kept to minimize changes in generated files.  */
1608
1609 void
1610 tdesc_add_flag (struct tdesc_type *type, int start,
1611                 const char *flag_name)
1612 {
1613   struct tdesc_type_field f = { 0 };
1614
1615   gdb_assert (type->kind == TDESC_TYPE_FLAGS
1616               || type->kind == TDESC_TYPE_STRUCT);
1617
1618   f.name = xstrdup (flag_name);
1619   f.start = start;
1620   f.end = start;
1621   f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
1622
1623   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1624 }
1625
1626 void
1627 tdesc_add_enum_value (struct tdesc_type *type, int value,
1628                       const char *name)
1629 {
1630   struct tdesc_type_field f = { 0 };
1631
1632   gdb_assert (type->kind == TDESC_TYPE_ENUM);
1633
1634   f.name = xstrdup (name);
1635   f.start = value;
1636   f.end = -1;
1637   f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1638
1639   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1640 }
1641
1642 struct tdesc_feature *
1643 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1644 {
1645   struct tdesc_feature *new_feature = new tdesc_feature (name);
1646
1647   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1648   return new_feature;
1649 }
1650
1651 struct target_desc *
1652 allocate_target_description (void)
1653 {
1654   return new target_desc ();
1655 }
1656
1657 static void
1658 free_target_description (void *arg)
1659 {
1660   struct target_desc *target_desc = (struct target_desc *) arg;
1661
1662   delete target_desc;
1663 }
1664
1665 struct cleanup *
1666 make_cleanup_free_target_description (struct target_desc *target_desc)
1667 {
1668   return make_cleanup (free_target_description, target_desc);
1669 }
1670
1671 void
1672 tdesc_add_compatible (struct target_desc *target_desc,
1673                       const struct bfd_arch_info *compatible)
1674 {
1675   const struct bfd_arch_info *compat;
1676   int ix;
1677
1678   /* If this instance of GDB is compiled without BFD support for the
1679      compatible architecture, simply ignore it -- we would not be able
1680      to handle it anyway.  */
1681   if (compatible == NULL)
1682     return;
1683
1684   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1685        ix++)
1686     if (compat == compatible)
1687       internal_error (__FILE__, __LINE__,
1688                       _("Attempted to add duplicate "
1689                         "compatible architecture \"%s\""),
1690                       compatible->printable_name);
1691
1692   VEC_safe_push (arch_p, target_desc->compatible, compatible);
1693 }
1694
1695 void
1696 set_tdesc_property (struct target_desc *target_desc,
1697                     const char *key, const char *value)
1698 {
1699   struct property *prop, new_prop;
1700   int ix;
1701
1702   gdb_assert (key != NULL && value != NULL);
1703
1704   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1705        ix++)
1706     if (strcmp (prop->key, key) == 0)
1707       internal_error (__FILE__, __LINE__,
1708                       _("Attempted to add duplicate property \"%s\""), key);
1709
1710   new_prop.key = xstrdup (key);
1711   new_prop.value = xstrdup (value);
1712   VEC_safe_push (property_s, target_desc->properties, &new_prop);
1713 }
1714
1715 void
1716 set_tdesc_architecture (struct target_desc *target_desc,
1717                         const struct bfd_arch_info *arch)
1718 {
1719   target_desc->arch = arch;
1720 }
1721
1722 void
1723 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1724 {
1725   target_desc->osabi = osabi;
1726 }
1727 \f
1728
1729 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1730 static struct cmd_list_element *tdesc_unset_cmdlist;
1731
1732 /* Helper functions for the CLI commands.  */
1733
1734 static void
1735 set_tdesc_cmd (char *args, int from_tty)
1736 {
1737   help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1738 }
1739
1740 static void
1741 show_tdesc_cmd (char *args, int from_tty)
1742 {
1743   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1744 }
1745
1746 static void
1747 unset_tdesc_cmd (char *args, int from_tty)
1748 {
1749   help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1750 }
1751
1752 static void
1753 set_tdesc_filename_cmd (char *args, int from_tty,
1754                         struct cmd_list_element *c)
1755 {
1756   xfree (target_description_filename);
1757   target_description_filename = xstrdup (tdesc_filename_cmd_string);
1758
1759   target_clear_description ();
1760   target_find_description ();
1761 }
1762
1763 static void
1764 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1765                          struct cmd_list_element *c,
1766                          const char *value)
1767 {
1768   value = target_description_filename;
1769
1770   if (value != NULL && *value != '\0')
1771     printf_filtered (_("The target description will be read from \"%s\".\n"),
1772                      value);
1773   else
1774     printf_filtered (_("The target description will be "
1775                        "read from the target.\n"));
1776 }
1777
1778 static void
1779 unset_tdesc_filename_cmd (char *args, int from_tty)
1780 {
1781   xfree (target_description_filename);
1782   target_description_filename = NULL;
1783   target_clear_description ();
1784   target_find_description ();
1785 }
1786
1787 /* Print target description in C.  */
1788
1789 class print_c_tdesc : public tdesc_element_visitor
1790 {
1791 public:
1792   print_c_tdesc (std::string &filename_after_features)
1793     : m_filename_after_features (filename_after_features)
1794   {
1795     const char *inp;
1796     char *outp;
1797     const char *filename = lbasename (m_filename_after_features.c_str ());
1798
1799     m_function = (char *) xmalloc (strlen (filename) + 1);
1800     for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1801       if (*inp == '.')
1802         break;
1803       else if (*inp == '-')
1804         *outp++ = '_';
1805       else
1806         *outp++ = *inp;
1807     *outp = '\0';
1808
1809     /* Standard boilerplate.  */
1810     printf_unfiltered ("/* THIS FILE IS GENERATED.  "
1811                        "-*- buffer-read-only: t -*- vi"
1812                        ":set ro:\n");
1813   }
1814
1815   ~print_c_tdesc ()
1816   {
1817     xfree (m_function);
1818   }
1819
1820   void visit_pre (const target_desc *e) override
1821   {
1822     printf_unfiltered ("  Original: %s */\n\n",
1823                        lbasename (m_filename_after_features.c_str ()));
1824
1825     printf_unfiltered ("#include \"defs.h\"\n");
1826     printf_unfiltered ("#include \"osabi.h\"\n");
1827     printf_unfiltered ("#include \"target-descriptions.h\"\n");
1828     printf_unfiltered ("\n");
1829
1830     printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1831     printf_unfiltered ("static void\n");
1832     printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1833     printf_unfiltered ("{\n");
1834     printf_unfiltered
1835       ("  struct target_desc *result = allocate_target_description ();\n");
1836
1837     if (tdesc_architecture (e) != NULL)
1838       {
1839         printf_unfiltered
1840           ("  set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1841            tdesc_architecture (e)->printable_name);
1842         printf_unfiltered ("\n");
1843       }
1844     if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1845         && tdesc_osabi (e) < GDB_OSABI_INVALID)
1846       {
1847         printf_unfiltered
1848           ("  set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1849            gdbarch_osabi_name (tdesc_osabi (e)));
1850         printf_unfiltered ("\n");
1851       }
1852
1853     int ix;
1854     const struct bfd_arch_info *compatible;
1855     struct property *prop;
1856
1857     for (ix = 0; VEC_iterate (arch_p, e->compatible, ix, compatible);
1858          ix++)
1859       {
1860         printf_unfiltered
1861           ("  tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1862            compatible->printable_name);
1863       }
1864
1865     if (ix)
1866       printf_unfiltered ("\n");
1867
1868     for (ix = 0; VEC_iterate (property_s, e->properties, ix, prop);
1869          ix++)
1870       {
1871         printf_unfiltered ("  set_tdesc_property (result, \"%s\", \"%s\");\n",
1872                            prop->key, prop->value);
1873       }
1874     printf_unfiltered ("  struct tdesc_feature *feature;\n");
1875   }
1876
1877   void visit_pre (const tdesc_feature *e) override
1878   {
1879     printf_unfiltered ("\n  feature = tdesc_create_feature (result, \"%s\");\n",
1880                        e->name);
1881   }
1882
1883   void visit_post (const tdesc_feature *e) override
1884   {}
1885
1886   void visit_post (const target_desc *e) override
1887   {
1888     printf_unfiltered ("\n  tdesc_%s = result;\n", m_function);
1889     printf_unfiltered ("}\n");
1890   }
1891
1892   void visit (const tdesc_type *type) override
1893   {
1894     struct tdesc_type_field *f;
1895
1896     /* Now we do some "filtering" in order to know which variables to
1897        declare.  This is needed because otherwise we would declare unused
1898        variables `field_type' and `type'.  */
1899     if (!m_printed_field_type)
1900       {
1901         printf_unfiltered ("  struct tdesc_type *field_type;\n");
1902         m_printed_field_type = true;
1903       }
1904
1905     if ((type->kind == TDESC_TYPE_UNION
1906          || type->kind == TDESC_TYPE_STRUCT
1907          || type->kind == TDESC_TYPE_FLAGS
1908          || type->kind == TDESC_TYPE_ENUM)
1909         && VEC_length (tdesc_type_field, type->u.u.fields) > 0
1910         && !m_printed_type)
1911       {
1912         printf_unfiltered ("  struct tdesc_type *type;\n");
1913         m_printed_type = true;
1914       }
1915
1916     switch (type->kind)
1917       {
1918       case TDESC_TYPE_VECTOR:
1919         printf_unfiltered
1920           ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1921            type->u.v.type->name);
1922         printf_unfiltered
1923           ("  tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1924            type->name, type->u.v.count);
1925         break;
1926       case TDESC_TYPE_STRUCT:
1927       case TDESC_TYPE_FLAGS:
1928         if (type->kind == TDESC_TYPE_STRUCT)
1929           {
1930             printf_unfiltered
1931               ("  type = tdesc_create_struct (feature, \"%s\");\n",
1932                type->name);
1933             if (type->u.u.size != 0)
1934               printf_unfiltered
1935                 ("  tdesc_set_struct_size (type, %d);\n",
1936                  type->u.u.size);
1937           }
1938         else
1939           {
1940             printf_unfiltered
1941               ("  type = tdesc_create_flags (feature, \"%s\", %d);\n",
1942                type->name, type->u.u.size);
1943           }
1944         for (int ix3 = 0;
1945              VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1946              ix3++)
1947           {
1948             const char *type_name;
1949
1950             gdb_assert (f->type != NULL);
1951             type_name = f->type->name;
1952
1953             /* To minimize changes to generated files, don't emit type
1954                info for fields that have defaulted types.  */
1955             if (f->start != -1)
1956               {
1957                 gdb_assert (f->end != -1);
1958                 if (f->type->kind == TDESC_TYPE_BOOL)
1959                   {
1960                     gdb_assert (f->start == f->end);
1961                     printf_unfiltered
1962                       ("  tdesc_add_flag (type, %d, \"%s\");\n",
1963                        f->start, f->name);
1964                   }
1965                 else if ((type->u.u.size == 4
1966                           && f->type->kind == TDESC_TYPE_UINT32)
1967                          || (type->u.u.size == 8
1968                              && f->type->kind == TDESC_TYPE_UINT64))
1969                   {
1970                     printf_unfiltered
1971                       ("  tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1972                        f->name, f->start, f->end);
1973                   }
1974                 else
1975                   {
1976                     printf_unfiltered
1977                       ("  field_type = tdesc_named_type (feature,"
1978                        " \"%s\");\n",
1979                        type_name);
1980                     printf_unfiltered
1981                       ("  tdesc_add_typed_bitfield (type, \"%s\","
1982                        " %d, %d, field_type);\n",
1983                        f->name, f->start, f->end);
1984                   }
1985               }
1986             else /* Not a bitfield.  */
1987               {
1988                 gdb_assert (f->end == -1);
1989                 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1990                 printf_unfiltered
1991                   ("  field_type = tdesc_named_type (feature,"
1992                    " \"%s\");\n",
1993                    type_name);
1994                 printf_unfiltered
1995                   ("  tdesc_add_field (type, \"%s\", field_type);\n",
1996                    f->name);
1997               }
1998           }
1999         break;
2000       case TDESC_TYPE_UNION:
2001         printf_unfiltered
2002           ("  type = tdesc_create_union (feature, \"%s\");\n",
2003            type->name);
2004         for (int ix3 = 0;
2005              VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2006              ix3++)
2007           {
2008             printf_unfiltered
2009               ("  field_type = tdesc_named_type (feature, \"%s\");\n",
2010                f->type->name);
2011             printf_unfiltered
2012               ("  tdesc_add_field (type, \"%s\", field_type);\n",
2013                f->name);
2014           }
2015         break;
2016       case TDESC_TYPE_ENUM:
2017         printf_unfiltered
2018           ("  type = tdesc_create_enum (feature, \"%s\", %d);\n",
2019            type->name, type->u.u.size);
2020         for (int ix3 = 0;
2021              VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2022              ix3++)
2023           printf_unfiltered
2024             ("  tdesc_add_enum_value (type, %d, \"%s\");\n",
2025              f->start, f->name);
2026         break;
2027       default:
2028         error (_("C output is not supported type \"%s\"."), type->name);
2029       }
2030     printf_unfiltered ("\n");
2031   }
2032
2033   void visit (const tdesc_reg *reg) override
2034   {
2035     printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2036                        reg->name, reg->target_regnum, reg->save_restore);
2037     if (reg->group)
2038       printf_unfiltered ("\"%s\", ", reg->group);
2039     else
2040       printf_unfiltered ("NULL, ");
2041     printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2042   }
2043
2044 protected:
2045   std::string m_filename_after_features;
2046
2047 private:
2048   char *m_function;
2049   bool m_printed_field_type = false;
2050   bool m_printed_type = false;
2051 };
2052
2053 /* Print target description feature in C.  */
2054
2055 class print_c_feature : public print_c_tdesc
2056 {
2057 public:
2058   print_c_feature (std::string &file)
2059     : print_c_tdesc (file)
2060   {
2061     /* Trim ".tmp".  */
2062     auto const pos = m_filename_after_features.find_last_of ('.');
2063
2064     m_filename_after_features = m_filename_after_features.substr (0, pos);
2065   }
2066
2067   void visit_pre (const target_desc *e) override
2068   {
2069     printf_unfiltered ("  Original: %s */\n\n",
2070                        lbasename (m_filename_after_features.c_str ()));
2071
2072     printf_unfiltered ("#include \"target-descriptions.h\"\n");
2073     printf_unfiltered ("\n");
2074   }
2075
2076   void visit_post (const target_desc *e) override
2077   {}
2078
2079   void visit_pre (const tdesc_feature *e) override
2080   {
2081     std::string name (m_filename_after_features);
2082
2083     auto pos = name.find_first_of ('.');
2084
2085     name = name.substr (0, pos);
2086     std::replace (name.begin (), name.end (), '/', '_');
2087     std::replace (name.begin (), name.end (), '-', '_');
2088
2089     printf_unfiltered ("static int\n");
2090     printf_unfiltered ("create_feature_%s ", name.c_str ());
2091     printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2092
2093     printf_unfiltered ("{\n");
2094     printf_unfiltered ("  struct tdesc_feature *feature;\n");
2095     printf_unfiltered ("\n  feature = tdesc_create_feature (result, \"%s\");\n",
2096                        e->name);
2097   }
2098
2099   void visit_post (const tdesc_feature *e) override
2100   {
2101     printf_unfiltered ("  return regnum;\n");
2102     printf_unfiltered ("}\n");
2103   }
2104
2105   void visit (const tdesc_reg *reg) override
2106   {
2107     /* Most "reg" in XML target descriptions don't have "regnum"
2108        attribute, so the register number is allocated sequentially.
2109        In case that reg has "regnum" attribute, register number
2110        should be set by that explicitly.  */
2111
2112     if (reg->target_regnum < m_next_regnum)
2113       {
2114         /* The integrity check, it can catch some errors on register
2115            number collision, like this,
2116
2117           <reg name="x0" bitsize="32"/>
2118           <reg name="x1" bitsize="32"/>
2119           <reg name="x2" bitsize="32"/>
2120           <reg name="x3" bitsize="32"/>
2121           <reg name="ps" bitsize="32" regnum="3"/>
2122
2123           but it also has false negatives.  The target description
2124           below is correct,
2125
2126           <reg name="x1" bitsize="32" regnum="1"/>
2127           <reg name="x3" bitsize="32" regnum="3"/>
2128           <reg name="x2" bitsize="32" regnum="2"/>
2129           <reg name="x4" bitsize="32" regnum="4"/>
2130
2131           but it is not a good practice, so still error on this,
2132           and also print the message so that it can be saved in the
2133           generated c file.  */
2134
2135         printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2136                            reg->target_regnum);
2137         printf_unfiltered ("is not the largest number (%d).\n",
2138                            m_next_regnum);
2139         error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2140                reg->target_regnum, m_next_regnum);
2141       }
2142
2143     if (reg->target_regnum > m_next_regnum)
2144       {
2145         printf_unfiltered ("  regnum = %ld;\n", reg->target_regnum);
2146         m_next_regnum = reg->target_regnum;
2147       }
2148
2149     printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2150                        reg->name, reg->save_restore);
2151     if (reg->group)
2152       printf_unfiltered ("\"%s\", ", reg->group);
2153     else
2154       printf_unfiltered ("NULL, ");
2155     printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2156
2157     m_next_regnum++;
2158   }
2159
2160 private:
2161   /* The register number to use for the next register we see.  */
2162   int m_next_regnum = 0;
2163 };
2164
2165 static void
2166 maint_print_c_tdesc_cmd (char *args, int from_tty)
2167 {
2168   const struct target_desc *tdesc;
2169   const char *filename;
2170
2171   if (args == NULL)
2172     {
2173       /* Use the global target-supplied description, not the current
2174          architecture's.  This lets a GDB for one architecture generate C
2175          for another architecture's description, even though the gdbarch
2176          initialization code will reject the new description.  */
2177       tdesc = current_target_desc;
2178       filename = target_description_filename;
2179     }
2180   else
2181     {
2182       /* Use the target description from the XML file.  */
2183       filename = args;
2184       tdesc = file_read_description_xml (filename);
2185     }
2186
2187   if (tdesc == NULL)
2188     error (_("There is no target description to print."));
2189
2190   if (filename == NULL)
2191     error (_("The current target description did not come from an XML file."));
2192
2193   std::string filename_after_features (filename);
2194   auto loc = filename_after_features.rfind ("/features/");
2195
2196   if (loc != std::string::npos)
2197     filename_after_features = filename_after_features.substr (loc + 10);
2198
2199   /* Print c files for target features instead of target descriptions,
2200      because c files got from target features are more flexible than the
2201      counterparts.  */
2202   if (startswith (filename_after_features.c_str (), "i386/32bit-"))
2203     {
2204       print_c_feature v (filename_after_features);
2205
2206       tdesc->accept (v);
2207     }
2208   else
2209     {
2210       print_c_tdesc v (filename_after_features);
2211
2212       tdesc->accept (v);
2213     }
2214 }
2215
2216 /* Provide a prototype to silence -Wmissing-prototypes.  */
2217 extern initialize_file_ftype _initialize_target_descriptions;
2218
2219 void
2220 _initialize_target_descriptions (void)
2221 {
2222   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2223
2224   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2225 Set target description specific variables."),
2226                   &tdesc_set_cmdlist, "set tdesc ",
2227                   0 /* allow-unknown */, &setlist);
2228   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2229 Show target description specific variables."),
2230                   &tdesc_show_cmdlist, "show tdesc ",
2231                   0 /* allow-unknown */, &showlist);
2232   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2233 Unset target description specific variables."),
2234                   &tdesc_unset_cmdlist, "unset tdesc ",
2235                   0 /* allow-unknown */, &unsetlist);
2236
2237   add_setshow_filename_cmd ("filename", class_obscure,
2238                             &tdesc_filename_cmd_string,
2239                             _("\
2240 Set the file to read for an XML target description"), _("\
2241 Show the file to read for an XML target description"), _("\
2242 When set, GDB will read the target description from a local\n\
2243 file instead of querying the remote target."),
2244                             set_tdesc_filename_cmd,
2245                             show_tdesc_filename_cmd,
2246                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2247
2248   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2249 Unset the file to read for an XML target description.  When unset,\n\
2250 GDB will read the description from the target."),
2251            &tdesc_unset_cmdlist);
2252
2253   add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2254 Print the current target description as a C source file."),
2255            &maintenanceprintlist);
2256 }