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