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