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