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