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