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