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