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