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