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