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