gdb/target-descriptions.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target description support for GDB.

  2.    Copyright (C) 2006-2015 Free Software Foundation, Inc.

  3.    Contributed by CodeSourcery.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "gdbcmd.h"
  18. #include "gdbtypes.h"
  19. #include "reggroups.h"
  20. #include "target.h"
  21. #include "target-descriptions.h"
  22. #include "vec.h"
  23. #include "xml-support.h"
  24. #include "xml-tdesc.h"
  25. #include "osabi.h"

  26. #include "gdb_obstack.h"
  27. #include "hashtab.h"
  28. #include "inferior.h"

  29. /* Types.  */

  30. typedef struct property
  31. {
  32.   char *key;
  33.   char *value;
  34. } property_s;
  35. DEF_VEC_O(property_s);

  36. /* An individual register from a target description.  */

  37. typedef struct tdesc_reg
  38. {
  39.   /* The name of this register.  In standard features, it may be
  40.      recognized by the architecture support code, or it may be purely
  41.      for the user.  */
  42.   char *name;

  43.   /* The register number used by this target to refer to this
  44.      register.  This is used for remote p/P packets and to determine
  45.      the ordering of registers in the remote g/G packets.  */
  46.   long target_regnum;

  47.   /* If this flag is set, GDB should save and restore this register
  48.      around calls to an inferior function.  */
  49.   int save_restore;

  50.   /* The name of the register group containing this register, or NULL
  51.      if the group should be automatically determined from the
  52.      register's type.  If this is "general", "float", or "vector", the
  53.      corresponding "info" command should display this register's
  54.      value.  It can be an arbitrary string, but should be limited to
  55.      alphanumeric characters and internal hyphens.  Currently other
  56.      strings are ignored (treated as NULL).  */
  57.   char *group;

  58.   /* The size of the register, in bits.  */
  59.   int bitsize;

  60.   /* The type of the register.  This string corresponds to either
  61.      a named type from the target description or a predefined
  62.      type from GDB.  */
  63.   char *type;

  64.   /* The target-described type corresponding to TYPE, if found.  */
  65.   struct tdesc_type *tdesc_type;
  66. } *tdesc_reg_p;
  67. DEF_VEC_P(tdesc_reg_p);

  68. /* A named type from a target description.  */

  69. typedef struct tdesc_type_field
  70. {
  71.   char *name;
  72.   struct tdesc_type *type;
  73.   int start, end;
  74. } tdesc_type_field;
  75. DEF_VEC_O(tdesc_type_field);

  76. typedef struct tdesc_type_flag
  77. {
  78.   char *name;
  79.   int start;
  80. } tdesc_type_flag;
  81. DEF_VEC_O(tdesc_type_flag);

  82. typedef struct tdesc_type
  83. {
  84.   /* The name of this type.  */
  85.   char *name;

  86.   /* Identify the kind of this type.  */
  87.   enum
  88.   {
  89.     /* Predefined types.  */
  90.     TDESC_TYPE_INT8,
  91.     TDESC_TYPE_INT16,
  92.     TDESC_TYPE_INT32,
  93.     TDESC_TYPE_INT64,
  94.     TDESC_TYPE_INT128,
  95.     TDESC_TYPE_UINT8,
  96.     TDESC_TYPE_UINT16,
  97.     TDESC_TYPE_UINT32,
  98.     TDESC_TYPE_UINT64,
  99.     TDESC_TYPE_UINT128,
  100.     TDESC_TYPE_CODE_PTR,
  101.     TDESC_TYPE_DATA_PTR,
  102.     TDESC_TYPE_IEEE_SINGLE,
  103.     TDESC_TYPE_IEEE_DOUBLE,
  104.     TDESC_TYPE_ARM_FPA_EXT,
  105.     TDESC_TYPE_I387_EXT,

  106.     /* Types defined by a target feature.  */
  107.     TDESC_TYPE_VECTOR,
  108.     TDESC_TYPE_STRUCT,
  109.     TDESC_TYPE_UNION,
  110.     TDESC_TYPE_FLAGS
  111.   } kind;

  112.   /* Kind-specific data.  */
  113.   union
  114.   {
  115.     /* Vector type.  */
  116.     struct
  117.     {
  118.       struct tdesc_type *type;
  119.       int count;
  120.     } v;

  121.     /* Struct or union type.  */
  122.     struct
  123.     {
  124.       VEC(tdesc_type_field) *fields;
  125.       LONGEST size;
  126.     } u;

  127.     /* Flags type.  */
  128.     struct
  129.     {
  130.       VEC(tdesc_type_flag) *flags;
  131.       LONGEST size;
  132.     } f;
  133.   } u;
  134. } *tdesc_type_p;
  135. DEF_VEC_P(tdesc_type_p);

  136. /* A feature from a target description.  Each feature is a collection
  137.    of other elements, e.g. registers and types.  */

  138. typedef struct tdesc_feature
  139. {
  140.   /* The name of this feature.  It may be recognized by the architecture
  141.      support code.  */
  142.   char *name;

  143.   /* The registers associated with this feature.  */
  144.   VEC(tdesc_reg_p) *registers;

  145.   /* The types associated with this feature.  */
  146.   VEC(tdesc_type_p) *types;
  147. } *tdesc_feature_p;
  148. DEF_VEC_P(tdesc_feature_p);

  149. /* A compatible architecture from a target description.  */
  150. typedef const struct bfd_arch_info *arch_p;
  151. DEF_VEC_P(arch_p);

  152. /* A target description.  */

  153. struct target_desc
  154. {
  155.   /* The architecture reported by the target, if any.  */
  156.   const struct bfd_arch_info *arch;

  157.   /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
  158.      otherwise.  */
  159.   enum gdb_osabi osabi;

  160.   /* The list of compatible architectures reported by the target.  */
  161.   VEC(arch_p) *compatible;

  162.   /* Any architecture-specific properties specified by the target.  */
  163.   VEC(property_s) *properties;

  164.   /* The features associated with this target.  */
  165.   VEC(tdesc_feature_p) *features;
  166. };

  167. /* Per-architecture data associated with a target description.  The
  168.    target description may be shared by multiple architectures, but
  169.    this data is private to one gdbarch.  */

  170. typedef struct tdesc_arch_reg
  171. {
  172.   struct tdesc_reg *reg;
  173.   struct type *type;
  174. } tdesc_arch_reg;
  175. DEF_VEC_O(tdesc_arch_reg);

  176. struct tdesc_arch_data
  177. {
  178.   /* A list of register/type pairs, indexed by GDB's internal register number.
  179.      During initialization of the gdbarch this list is used to store
  180.      registers which the architecture assigns a fixed register number.
  181.      Registers which are NULL in this array, or off the end, are
  182.      treated as zero-sized and nameless (i.e. placeholders in the
  183.      numbering).  */
  184.   VEC(tdesc_arch_reg) *arch_regs;

  185.   /* Functions which report the register name, type, and reggroups for
  186.      pseudo-registers.  */
  187.   gdbarch_register_name_ftype *pseudo_register_name;
  188.   gdbarch_register_type_ftype *pseudo_register_type;
  189.   gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
  190. };

  191. /* Info about an inferior's target description.  There's one of these
  192.    for each inferior.  */

  193. struct target_desc_info
  194. {
  195.   /* A flag indicating that a description has already been fetched
  196.      from the target, so it should not be queried again.  */

  197.   int fetched;

  198.   /* The description fetched from the target, or NULL if the target
  199.      did not supply any description.  Only valid when
  200.      target_desc_fetched is set.  Only the description initialization
  201.      code should access this; normally, the description should be
  202.      accessed through the gdbarch object.  */

  203.   const struct target_desc *tdesc;

  204.   /* The filename to read a target description from, as set by "set
  205.      tdesc filename ..."  */

  206.   char *filename;
  207. };

  208. /* Get the inferior INF's target description info, allocating one on
  209.    the stop if necessary.  */

  210. static struct target_desc_info *
  211. get_tdesc_info (struct inferior *inf)
  212. {
  213.   if (inf->tdesc_info == NULL)
  214.     inf->tdesc_info = XCNEW (struct target_desc_info);
  215.   return inf->tdesc_info;
  216. }

  217. /* A handle for architecture-specific data associated with the
  218.    target description (see struct tdesc_arch_data).  */

  219. static struct gdbarch_data *tdesc_data;

  220. /* See target-descriptions.h.  */

  221. int
  222. target_desc_info_from_user_p (struct target_desc_info *info)
  223. {
  224.   return info != NULL && info->filename != NULL;
  225. }

  226. /* See target-descriptions.h.  */

  227. void
  228. copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
  229. {
  230.   struct target_desc_info *src = get_tdesc_info (srcinf);
  231.   struct target_desc_info *dest = get_tdesc_info (destinf);

  232.   dest->fetched = src->fetched;
  233.   dest->tdesc = src->tdesc;
  234.   dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
  235. }

  236. /* See target-descriptions.h.  */

  237. void
  238. target_desc_info_free (struct target_desc_info *tdesc_info)
  239. {
  240.   if (tdesc_info != NULL)
  241.     {
  242.       xfree (tdesc_info->filename);
  243.       xfree (tdesc_info);
  244.     }
  245. }

  246. /* Convenience helper macros.  */

  247. #define target_desc_fetched \
  248.   get_tdesc_info (current_inferior ())->fetched
  249. #define current_target_desc \
  250.   get_tdesc_info (current_inferior ())->tdesc
  251. #define target_description_filename \
  252.   get_tdesc_info (current_inferior ())->filename

  253. /* The string manipulated by the "set tdesc filename ..." command.  */

  254. static char *tdesc_filename_cmd_string;

  255. /* Fetch the current target's description, and switch the current
  256.    architecture to one which incorporates that description.  */

  257. void
  258. target_find_description (void)
  259. {
  260.   /* If we've already fetched a description from the target, don't do
  261.      it again.  This allows a target to fetch the description early,
  262.      during its to_open or to_create_inferior, if it needs extra
  263.      information about the target to initialize.  */
  264.   if (target_desc_fetched)
  265.     return;

  266.   /* The current architecture should not have any target description
  267.      specified.  It should have been cleared, e.g. when we
  268.      disconnected from the previous target.  */
  269.   gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);

  270.   /* First try to fetch an XML description from the user-specified
  271.      file.  */
  272.   current_target_desc = NULL;
  273.   if (target_description_filename != NULL
  274.       && *target_description_filename != '\0')
  275.     current_target_desc
  276.       = file_read_description_xml (target_description_filename);

  277.   /* Next try to read the description from the current target using
  278.      target objects.  */
  279.   if (current_target_desc == NULL)
  280.     current_target_desc = target_read_description_xml (&current_target);

  281.   /* If that failed try a target-specific hook.  */
  282.   if (current_target_desc == NULL)
  283.     current_target_desc = target_read_description (&current_target);

  284.   /* If a non-NULL description was returned, then update the current
  285.      architecture.  */
  286.   if (current_target_desc)
  287.     {
  288.       struct gdbarch_info info;

  289.       gdbarch_info_init (&info);
  290.       info.target_desc = current_target_desc;
  291.       if (!gdbarch_update_p (info))
  292.         warning (_("Architecture rejected target-supplied description"));
  293.       else
  294.         {
  295.           struct tdesc_arch_data *data;

  296.           data = gdbarch_data (target_gdbarch (), tdesc_data);
  297.           if (tdesc_has_registers (current_target_desc)
  298.               && data->arch_regs == NULL)
  299.             warning (_("Target-supplied registers are not supported "
  300.                        "by the current architecture"));
  301.         }
  302.     }

  303.   /* Now that we know this description is usable, record that we
  304.      fetched it.  */
  305.   target_desc_fetched = 1;
  306. }

  307. /* Discard any description fetched from the current target, and switch
  308.    the current architecture to one with no target description.  */

  309. void
  310. target_clear_description (void)
  311. {
  312.   struct gdbarch_info info;

  313.   if (!target_desc_fetched)
  314.     return;

  315.   target_desc_fetched = 0;
  316.   current_target_desc = NULL;

  317.   gdbarch_info_init (&info);
  318.   if (!gdbarch_update_p (info))
  319.     internal_error (__FILE__, __LINE__,
  320.                     _("Could not remove target-supplied description"));
  321. }

  322. /* Return the global current target description.  This should only be
  323.    used by gdbarch initialization code; most access should be through
  324.    an existing gdbarch.  */

  325. const struct target_desc *
  326. target_current_description (void)
  327. {
  328.   if (target_desc_fetched)
  329.     return current_target_desc;

  330.   return NULL;
  331. }

  332. /* Return non-zero if this target description is compatible
  333.    with the given BFD architecture.  */

  334. int
  335. tdesc_compatible_p (const struct target_desc *target_desc,
  336.                     const struct bfd_arch_info *arch)
  337. {
  338.   const struct bfd_arch_info *compat;
  339.   int ix;

  340.   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
  341.        ix++)
  342.     {
  343.       if (compat == arch
  344.           || arch->compatible (arch, compat)
  345.           || compat->compatible (compat, arch))
  346.         return 1;
  347.     }

  348.   return 0;
  349. }


  350. /* Direct accessors for target descriptions.  */

  351. /* Return the string value of a property named KEY, or NULL if the
  352.    property was not specified.  */

  353. const char *
  354. tdesc_property (const struct target_desc *target_desc, const char *key)
  355. {
  356.   struct property *prop;
  357.   int ix;

  358.   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
  359.        ix++)
  360.     if (strcmp (prop->key, key) == 0)
  361.       return prop->value;

  362.   return NULL;
  363. }

  364. /* Return the BFD architecture associated with this target
  365.    description, or NULL if no architecture was specified.  */

  366. const struct bfd_arch_info *
  367. tdesc_architecture (const struct target_desc *target_desc)
  368. {
  369.   return target_desc->arch;
  370. }

  371. /* Return the OSABI associated with this target description, or
  372.    GDB_OSABI_UNKNOWN if no osabi was specified.  */

  373. enum gdb_osabi
  374. tdesc_osabi (const struct target_desc *target_desc)
  375. {
  376.   return target_desc->osabi;
  377. }



  378. /* Return 1 if this target description includes any registers.  */

  379. int
  380. tdesc_has_registers (const struct target_desc *target_desc)
  381. {
  382.   int ix;
  383.   struct tdesc_feature *feature;

  384.   if (target_desc == NULL)
  385.     return 0;

  386.   for (ix = 0;
  387.        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
  388.        ix++)
  389.     if (! VEC_empty (tdesc_reg_p, feature->registers))
  390.       return 1;

  391.   return 0;
  392. }

  393. /* Return the feature with the given name, if present, or NULL if
  394.    the named feature is not found.  */

  395. const struct tdesc_feature *
  396. tdesc_find_feature (const struct target_desc *target_desc,
  397.                     const char *name)
  398. {
  399.   int ix;
  400.   struct tdesc_feature *feature;

  401.   for (ix = 0;
  402.        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
  403.        ix++)
  404.     if (strcmp (feature->name, name) == 0)
  405.       return feature;

  406.   return NULL;
  407. }

  408. /* Return the name of FEATURE.  */

  409. const char *
  410. tdesc_feature_name (const struct tdesc_feature *feature)
  411. {
  412.   return feature->name;
  413. }

  414. /* Predefined types.  */
  415. static struct tdesc_type tdesc_predefined_types[] =
  416. {
  417.   { "int8", TDESC_TYPE_INT8 },
  418.   { "int16", TDESC_TYPE_INT16 },
  419.   { "int32", TDESC_TYPE_INT32 },
  420.   { "int64", TDESC_TYPE_INT64 },
  421.   { "int128", TDESC_TYPE_INT128 },
  422.   { "uint8", TDESC_TYPE_UINT8 },
  423.   { "uint16", TDESC_TYPE_UINT16 },
  424.   { "uint32", TDESC_TYPE_UINT32 },
  425.   { "uint64", TDESC_TYPE_UINT64 },
  426.   { "uint128", TDESC_TYPE_UINT128 },
  427.   { "code_ptr", TDESC_TYPE_CODE_PTR },
  428.   { "data_ptr", TDESC_TYPE_DATA_PTR },
  429.   { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
  430.   { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
  431.   { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
  432.   { "i387_ext", TDESC_TYPE_I387_EXT }
  433. };

  434. /* Return the type associated with ID in the context of FEATURE, or
  435.    NULL if none.  */

  436. struct tdesc_type *
  437. tdesc_named_type (const struct tdesc_feature *feature, const char *id)
  438. {
  439.   int ix;
  440.   struct tdesc_type *type;

  441.   /* First try target-defined types.  */
  442.   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
  443.     if (strcmp (type->name, id) == 0)
  444.       return type;

  445.   /* Next try the predefined types.  */
  446.   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
  447.     if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
  448.       return &tdesc_predefined_types[ix];

  449.   return NULL;
  450. }

  451. /* Lookup type associated with ID.  */

  452. struct type *
  453. tdesc_find_type (struct gdbarch *gdbarch, const char *id)
  454. {
  455.   struct tdesc_arch_reg *reg;
  456.   struct tdesc_arch_data *data;
  457.   int i, num_regs;

  458.   data = gdbarch_data (gdbarch, tdesc_data);
  459.   num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
  460.   for (i = 0; i < num_regs; i++)
  461.     {
  462.       reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
  463.       if (reg->reg
  464.           && reg->reg->tdesc_type
  465.           && reg->type
  466.           && strcmp (id, reg->reg->tdesc_type->name) == 0)
  467.         return reg->type;
  468.     }

  469.   return NULL;
  470. }

  471. /* Construct, if necessary, and return the GDB type implementing target
  472.    type TDESC_TYPE for architecture GDBARCH.  */

  473. static struct type *
  474. tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
  475. {
  476.   struct type *type;

  477.   switch (tdesc_type->kind)
  478.     {
  479.     /* Predefined types.  */
  480.     case TDESC_TYPE_INT8:
  481.       return builtin_type (gdbarch)->builtin_int8;

  482.     case TDESC_TYPE_INT16:
  483.       return builtin_type (gdbarch)->builtin_int16;

  484.     case TDESC_TYPE_INT32:
  485.       return builtin_type (gdbarch)->builtin_int32;

  486.     case TDESC_TYPE_INT64:
  487.       return builtin_type (gdbarch)->builtin_int64;

  488.     case TDESC_TYPE_INT128:
  489.       return builtin_type (gdbarch)->builtin_int128;

  490.     case TDESC_TYPE_UINT8:
  491.       return builtin_type (gdbarch)->builtin_uint8;

  492.     case TDESC_TYPE_UINT16:
  493.       return builtin_type (gdbarch)->builtin_uint16;

  494.     case TDESC_TYPE_UINT32:
  495.       return builtin_type (gdbarch)->builtin_uint32;

  496.     case TDESC_TYPE_UINT64:
  497.       return builtin_type (gdbarch)->builtin_uint64;

  498.     case TDESC_TYPE_UINT128:
  499.       return builtin_type (gdbarch)->builtin_uint128;

  500.     case TDESC_TYPE_CODE_PTR:
  501.       return builtin_type (gdbarch)->builtin_func_ptr;

  502.     case TDESC_TYPE_DATA_PTR:
  503.       return builtin_type (gdbarch)->builtin_data_ptr;

  504.     default:
  505.       break;
  506.     }

  507.   type = tdesc_find_type (gdbarch, tdesc_type->name);
  508.   if (type)
  509.     return type;

  510.   switch (tdesc_type->kind)
  511.     {
  512.     case TDESC_TYPE_IEEE_SINGLE:
  513.       return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
  514.                               floatformats_ieee_single);

  515.     case TDESC_TYPE_IEEE_DOUBLE:
  516.       return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
  517.                               floatformats_ieee_double);

  518.     case TDESC_TYPE_ARM_FPA_EXT:
  519.       return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
  520.                               floatformats_arm_ext);

  521.     case TDESC_TYPE_I387_EXT:
  522.       return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
  523.                               floatformats_i387_ext);

  524.     /* Types defined by a target feature.  */
  525.     case TDESC_TYPE_VECTOR:
  526.       {
  527.         struct type *type, *field_type;

  528.         field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
  529.         type = init_vector_type (field_type, tdesc_type->u.v.count);
  530.         TYPE_NAME (type) = xstrdup (tdesc_type->name);

  531.         return type;
  532.       }

  533.     case TDESC_TYPE_STRUCT:
  534.       {
  535.         struct type *type, *field_type;
  536.         struct tdesc_type_field *f;
  537.         int ix;

  538.         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
  539.         TYPE_NAME (type) = xstrdup (tdesc_type->name);
  540.         TYPE_TAG_NAME (type) = TYPE_NAME (type);

  541.         for (ix = 0;
  542.              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
  543.              ix++)
  544.           {
  545.             if (f->type == NULL)
  546.               {
  547.                 /* Bitfield.  */
  548.                 struct field *fld;
  549.                 struct type *field_type;
  550.                 int bitsize, total_size;

  551.                 /* This invariant should be preserved while creating
  552.                    types.  */
  553.                 gdb_assert (tdesc_type->u.u.size != 0);
  554.                 if (tdesc_type->u.u.size > 4)
  555.                   field_type = builtin_type (gdbarch)->builtin_uint64;
  556.                 else
  557.                   field_type = builtin_type (gdbarch)->builtin_uint32;

  558.                 fld = append_composite_type_field_raw (type, xstrdup (f->name),
  559.                                                        field_type);

  560.                 /* For little-endian, BITPOS counts from the LSB of
  561.                    the structure and marks the LSB of the field.  For
  562.                    big-endian, BITPOS counts from the MSB of the
  563.                    structure and marks the MSB of the field.  Either
  564.                    way, it is the number of bits to the "left" of the
  565.                    field.  To calculate this in big-endian, we need
  566.                    the total size of the structure.  */
  567.                 bitsize = f->end - f->start + 1;
  568.                 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
  569.                 if (gdbarch_bits_big_endian (gdbarch))
  570.                   SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
  571.                 else
  572.                   SET_FIELD_BITPOS (fld[0], f->start);
  573.                 FIELD_BITSIZE (fld[0]) = bitsize;
  574.               }
  575.             else
  576.               {
  577.                 field_type = tdesc_gdb_type (gdbarch, f->type);
  578.                 append_composite_type_field (type, xstrdup (f->name),
  579.                                              field_type);
  580.               }
  581.           }

  582.         if (tdesc_type->u.u.size != 0)
  583.           TYPE_LENGTH (type) = tdesc_type->u.u.size;
  584.         return type;
  585.       }

  586.     case TDESC_TYPE_UNION:
  587.       {
  588.         struct type *type, *field_type;
  589.         struct tdesc_type_field *f;
  590.         int ix;

  591.         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
  592.         TYPE_NAME (type) = xstrdup (tdesc_type->name);

  593.         for (ix = 0;
  594.              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
  595.              ix++)
  596.           {
  597.             field_type = tdesc_gdb_type (gdbarch, f->type);
  598.             append_composite_type_field (type, xstrdup (f->name), field_type);

  599.             /* If any of the children of a union are vectors, flag the
  600.                union as a vector also.  This allows e.g. a union of two
  601.                vector types to show up automatically in "info vector".  */
  602.             if (TYPE_VECTOR (field_type))
  603.               TYPE_VECTOR (type) = 1;
  604.           }
  605.         return type;
  606.       }

  607.     case TDESC_TYPE_FLAGS:
  608.       {
  609.         struct tdesc_type_flag *f;
  610.         int ix;

  611.         type = arch_flags_type (gdbarch, tdesc_type->name,
  612.                                 tdesc_type->u.f.size);
  613.         for (ix = 0;
  614.              VEC_iterate (tdesc_type_flag, tdesc_type->u.f.flags, ix, f);
  615.              ix++)
  616.           /* Note that contrary to the function name, this call will
  617.              just set the properties of an already-allocated
  618.              field.  */
  619.           append_flags_type_flag (type, f->start,
  620.                                   *f->name ? f->name : NULL);

  621.         return type;
  622.       }
  623.     }

  624.   internal_error (__FILE__, __LINE__,
  625.                   "Type \"%s\" has an unknown kind %d",
  626.                   tdesc_type->name, tdesc_type->kind);
  627. }


  628. /* Support for registers from target descriptions.  */

  629. /* Construct the per-gdbarch data.  */

  630. static void *
  631. tdesc_data_init (struct obstack *obstack)
  632. {
  633.   struct tdesc_arch_data *data;

  634.   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
  635.   return data;
  636. }

  637. /* Similar, but for the temporary copy used during architecture
  638.    initialization.  */

  639. struct tdesc_arch_data *
  640. tdesc_data_alloc (void)
  641. {
  642.   return XCNEW (struct tdesc_arch_data);
  643. }

  644. /* Free something allocated by tdesc_data_alloc, if it is not going
  645.    to be used (for instance if it was unsuitable for the
  646.    architecture).  */

  647. void
  648. tdesc_data_cleanup (void *data_untyped)
  649. {
  650.   struct tdesc_arch_data *data = data_untyped;

  651.   VEC_free (tdesc_arch_reg, data->arch_regs);
  652.   xfree (data);
  653. }

  654. /* Search FEATURE for a register named NAME.  */

  655. static struct tdesc_reg *
  656. tdesc_find_register_early (const struct tdesc_feature *feature,
  657.                            const char *name)
  658. {
  659.   int ixr;
  660.   struct tdesc_reg *reg;

  661.   for (ixr = 0;
  662.        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
  663.        ixr++)
  664.     if (strcasecmp (reg->name, name) == 0)
  665.       return reg;

  666.   return NULL;
  667. }

  668. /* Search FEATURE for a register named NAME.  Assign REGNO to it.  */

  669. int
  670. tdesc_numbered_register (const struct tdesc_feature *feature,
  671.                          struct tdesc_arch_data *data,
  672.                          int regno, const char *name)
  673. {
  674.   struct tdesc_arch_reg arch_reg = { 0 };
  675.   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);

  676.   if (reg == NULL)
  677.     return 0;

  678.   /* Make sure the vector includes a REGNO'th element.  */
  679.   while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
  680.     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);

  681.   arch_reg.reg = reg;
  682.   VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
  683.   return 1;
  684. }

  685. /* Search FEATURE for a register named NAME, but do not assign a fixed
  686.    register number to it.  */

  687. int
  688. tdesc_unnumbered_register (const struct tdesc_feature *feature,
  689.                            const char *name)
  690. {
  691.   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);

  692.   if (reg == NULL)
  693.     return 0;

  694.   return 1;
  695. }

  696. /* Search FEATURE for a register whose name is in NAMES and assign
  697.    REGNO to it.  */

  698. int
  699. tdesc_numbered_register_choices (const struct tdesc_feature *feature,
  700.                                  struct tdesc_arch_data *data,
  701.                                  int regno, const char *const names[])
  702. {
  703.   int i;

  704.   for (i = 0; names[i] != NULL; i++)
  705.     if (tdesc_numbered_register (feature, data, regno, names[i]))
  706.       return 1;

  707.   return 0;
  708. }

  709. /* Search FEATURE for a register named NAME, and return its size in
  710.    bits.  The register must exist.  */

  711. int
  712. tdesc_register_size (const struct tdesc_feature *feature,
  713.                      const char *name)
  714. {
  715.   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);

  716.   gdb_assert (reg != NULL);
  717.   return reg->bitsize;
  718. }

  719. /* Look up a register by its GDB internal register number.  */

  720. static struct tdesc_arch_reg *
  721. tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
  722. {
  723.   struct tdesc_arch_data *data;

  724.   data = gdbarch_data (gdbarch, tdesc_data);
  725.   if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
  726.     return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
  727.   else
  728.     return NULL;
  729. }

  730. static struct tdesc_reg *
  731. tdesc_find_register (struct gdbarch *gdbarch, int regno)
  732. {
  733.   struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);

  734.   return reg? reg->reg : NULL;
  735. }

  736. /* Return the name of register REGNO, from the target description or
  737.    from an architecture-provided pseudo_register_name method.  */

  738. const char *
  739. tdesc_register_name (struct gdbarch *gdbarch, int regno)
  740. {
  741.   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
  742.   int num_regs = gdbarch_num_regs (gdbarch);
  743.   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);

  744.   if (reg != NULL)
  745.     return reg->name;

  746.   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
  747.     {
  748.       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);

  749.       gdb_assert (data->pseudo_register_name != NULL);
  750.       return data->pseudo_register_name (gdbarch, regno);
  751.     }

  752.   return "";
  753. }

  754. struct type *
  755. tdesc_register_type (struct gdbarch *gdbarch, int regno)
  756. {
  757.   struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
  758.   struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
  759.   int num_regs = gdbarch_num_regs (gdbarch);
  760.   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);

  761.   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
  762.     {
  763.       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);

  764.       gdb_assert (data->pseudo_register_type != NULL);
  765.       return data->pseudo_register_type (gdbarch, regno);
  766.     }

  767.   if (reg == NULL)
  768.     /* Return "int0_t", since "void" has a misleading size of one.  */
  769.     return builtin_type (gdbarch)->builtin_int0;

  770.   if (arch_reg->type == NULL)
  771.     {
  772.       /* First check for a predefined or target defined type.  */
  773.       if (reg->tdesc_type)
  774.         arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);

  775.       /* Next try size-sensitive type shortcuts.  */
  776.       else if (strcmp (reg->type, "float") == 0)
  777.         {
  778.           if (reg->bitsize == gdbarch_float_bit (gdbarch))
  779.             arch_reg->type = builtin_type (gdbarch)->builtin_float;
  780.           else if (reg->bitsize == gdbarch_double_bit (gdbarch))
  781.             arch_reg->type = builtin_type (gdbarch)->builtin_double;
  782.           else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
  783.             arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
  784.           else
  785.             {
  786.               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
  787.                        reg->name, reg->bitsize);
  788.               arch_reg->type = builtin_type (gdbarch)->builtin_double;
  789.             }
  790.         }
  791.       else if (strcmp (reg->type, "int") == 0)
  792.         {
  793.           if (reg->bitsize == gdbarch_long_bit (gdbarch))
  794.             arch_reg->type = builtin_type (gdbarch)->builtin_long;
  795.           else if (reg->bitsize == TARGET_CHAR_BIT)
  796.             arch_reg->type = builtin_type (gdbarch)->builtin_char;
  797.           else if (reg->bitsize == gdbarch_short_bit (gdbarch))
  798.             arch_reg->type = builtin_type (gdbarch)->builtin_short;
  799.           else if (reg->bitsize == gdbarch_int_bit (gdbarch))
  800.             arch_reg->type = builtin_type (gdbarch)->builtin_int;
  801.           else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
  802.             arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
  803.           else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
  804.           /* A bit desperate by this point...  */
  805.             arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
  806.           else
  807.             {
  808.               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
  809.                        reg->name, reg->bitsize);
  810.               arch_reg->type = builtin_type (gdbarch)->builtin_long;
  811.             }
  812.         }

  813.       if (arch_reg->type == NULL)
  814.         internal_error (__FILE__, __LINE__,
  815.                         "Register \"%s\" has an unknown type \"%s\"",
  816.                         reg->name, reg->type);
  817.     }

  818.   return arch_reg->type;
  819. }

  820. static int
  821. tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
  822. {
  823.   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);

  824.   if (reg != NULL)
  825.     return reg->target_regnum;
  826.   else
  827.     return -1;
  828. }

  829. /* Check whether REGNUM is a member of REGGROUP.  Registers from the
  830.    target description may be classified as general, float, or vector.
  831.    Unlike a gdbarch register_reggroup_p method, this function will
  832.    return -1 if it does not know; the caller should handle registers
  833.    with no specified group.

  834.    Arbitrary strings (other than "general", "float", and "vector")
  835.    from the description are not used; they cause the register to be
  836.    displayed in "info all-registers" but excluded from "info
  837.    registers" et al.  The names of containing features are also not
  838.    used.  This might be extended to display registers in some more
  839.    useful groupings.

  840.    The save-restore flag is also implemented here.  */

  841. int
  842. tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
  843.                               struct reggroup *reggroup)
  844. {
  845.   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);

  846.   if (reg != NULL && reg->group != NULL)
  847.     {
  848.       int general_p = 0, float_p = 0, vector_p = 0;

  849.       if (strcmp (reg->group, "general") == 0)
  850.         general_p = 1;
  851.       else if (strcmp (reg->group, "float") == 0)
  852.         float_p = 1;
  853.       else if (strcmp (reg->group, "vector") == 0)
  854.         vector_p = 1;

  855.       if (reggroup == float_reggroup)
  856.         return float_p;

  857.       if (reggroup == vector_reggroup)
  858.         return vector_p;

  859.       if (reggroup == general_reggroup)
  860.         return general_p;
  861.     }

  862.   if (reg != NULL
  863.       && (reggroup == save_reggroup || reggroup == restore_reggroup))
  864.     return reg->save_restore;

  865.   return -1;
  866. }

  867. /* Check whether REGNUM is a member of REGGROUP.  Registers with no
  868.    group specified go to the default reggroup function and are handled
  869.    by type.  */

  870. static int
  871. tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
  872.                            struct reggroup *reggroup)
  873. {
  874.   int num_regs = gdbarch_num_regs (gdbarch);
  875.   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
  876.   int ret;

  877.   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
  878.     {
  879.       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);

  880.       if (data->pseudo_register_reggroup_p != NULL)
  881.         return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
  882.       /* Otherwise fall through to the default reggroup_p.  */
  883.     }

  884.   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
  885.   if (ret != -1)
  886.     return ret;

  887.   return default_register_reggroup_p (gdbarch, regno, reggroup);
  888. }

  889. /* Record architecture-specific functions to call for pseudo-register
  890.    support.  */

  891. void
  892. set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
  893.                                 gdbarch_register_name_ftype *pseudo_name)
  894. {
  895.   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);

  896.   data->pseudo_register_name = pseudo_name;
  897. }

  898. void
  899. set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
  900.                                 gdbarch_register_type_ftype *pseudo_type)
  901. {
  902.   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);

  903.   data->pseudo_register_type = pseudo_type;
  904. }

  905. void
  906. set_tdesc_pseudo_register_reggroup_p
  907.   (struct gdbarch *gdbarch,
  908.    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
  909. {
  910.   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);

  911.   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
  912. }

  913. /* Update GDBARCH to use the target description for registers.  */

  914. void
  915. tdesc_use_registers (struct gdbarch *gdbarch,
  916.                      const struct target_desc *target_desc,
  917.                      struct tdesc_arch_data *early_data)
  918. {
  919.   int num_regs = gdbarch_num_regs (gdbarch);
  920.   int ixf, ixr;
  921.   struct tdesc_feature *feature;
  922.   struct tdesc_reg *reg;
  923.   struct tdesc_arch_data *data;
  924.   struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
  925.   htab_t reg_hash;

  926.   /* We can't use the description for registers if it doesn't describe
  927.      any.  This function should only be called after validating
  928.      registers, so the caller should know that registers are
  929.      included.  */
  930.   gdb_assert (tdesc_has_registers (target_desc));

  931.   data = gdbarch_data (gdbarch, tdesc_data);
  932.   data->arch_regs = early_data->arch_regs;
  933.   xfree (early_data);

  934.   /* Build up a set of all registers, so that we can assign register
  935.      numbers where needed.  The hash table expands as necessary, so
  936.      the initial size is arbitrary.  */
  937.   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
  938.   for (ixf = 0;
  939.        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
  940.        ixf++)
  941.     for (ixr = 0;
  942.          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
  943.          ixr++)
  944.       {
  945.         void **slot = htab_find_slot (reg_hash, reg, INSERT);

  946.         *slot = reg;
  947.       }

  948.   /* Remove any registers which were assigned numbers by the
  949.      architecture.  */
  950.   for (ixr = 0;
  951.        VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
  952.        ixr++)
  953.     if (arch_reg->reg)
  954.       htab_remove_elt (reg_hash, arch_reg->reg);

  955.   /* Assign numbers to the remaining registers and add them to the
  956.      list of registers.  The new numbers are always above gdbarch_num_regs.
  957.      Iterate over the features, not the hash table, so that the order
  958.      matches that in the target description.  */

  959.   gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
  960.   while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
  961.     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
  962.   for (ixf = 0;
  963.        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
  964.        ixf++)
  965.     for (ixr = 0;
  966.          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
  967.          ixr++)
  968.       if (htab_find (reg_hash, reg) != NULL)
  969.         {
  970.           new_arch_reg.reg = reg;
  971.           VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
  972.           num_regs++;
  973.         }

  974.   htab_delete (reg_hash);

  975.   /* Update the architecture.  */
  976.   set_gdbarch_num_regs (gdbarch, num_regs);
  977.   set_gdbarch_register_name (gdbarch, tdesc_register_name);
  978.   set_gdbarch_register_type (gdbarch, tdesc_register_type);
  979.   set_gdbarch_remote_register_number (gdbarch,
  980.                                       tdesc_remote_register_number);
  981.   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
  982. }


  983. /* Methods for constructing a target description.  */

  984. static void
  985. tdesc_free_reg (struct tdesc_reg *reg)
  986. {
  987.   xfree (reg->name);
  988.   xfree (reg->type);
  989.   xfree (reg->group);
  990.   xfree (reg);
  991. }

  992. void
  993. tdesc_create_reg (struct tdesc_feature *feature, const char *name,
  994.                   int regnum, int save_restore, const char *group,
  995.                   int bitsize, const char *type)
  996. {
  997.   struct tdesc_reg *reg = XCNEW (struct tdesc_reg);

  998.   reg->name = xstrdup (name);
  999.   reg->target_regnum = regnum;
  1000.   reg->save_restore = save_restore;
  1001.   reg->group = group ? xstrdup (group) : NULL;
  1002.   reg->bitsize = bitsize;
  1003.   reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");

  1004.   /* If the register's type is target-defined, look it up now.  We may not
  1005.      have easy access to the containing feature when we want it later.  */
  1006.   reg->tdesc_type = tdesc_named_type (feature, reg->type);

  1007.   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
  1008. }

  1009. static void
  1010. tdesc_free_type (struct tdesc_type *type)
  1011. {
  1012.   switch (type->kind)
  1013.     {
  1014.     case TDESC_TYPE_STRUCT:
  1015.     case TDESC_TYPE_UNION:
  1016.       {
  1017.         struct tdesc_type_field *f;
  1018.         int ix;

  1019.         for (ix = 0;
  1020.              VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
  1021.              ix++)
  1022.           xfree (f->name);

  1023.         VEC_free (tdesc_type_field, type->u.u.fields);
  1024.       }
  1025.       break;

  1026.     case TDESC_TYPE_FLAGS:
  1027.       {
  1028.         struct tdesc_type_flag *f;
  1029.         int ix;

  1030.         for (ix = 0;
  1031.              VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
  1032.              ix++)
  1033.           xfree (f->name);

  1034.         VEC_free (tdesc_type_flag, type->u.f.flags);
  1035.       }
  1036.       break;

  1037.     default:
  1038.       break;
  1039.     }

  1040.   xfree (type->name);
  1041.   xfree (type);
  1042. }

  1043. struct tdesc_type *
  1044. tdesc_create_vector (struct tdesc_feature *feature, const char *name,
  1045.                      struct tdesc_type *field_type, int count)
  1046. {
  1047.   struct tdesc_type *type = XCNEW (struct tdesc_type);

  1048.   type->name = xstrdup (name);
  1049.   type->kind = TDESC_TYPE_VECTOR;
  1050.   type->u.v.type = field_type;
  1051.   type->u.v.count = count;

  1052.   VEC_safe_push (tdesc_type_p, feature->types, type);
  1053.   return type;
  1054. }

  1055. struct tdesc_type *
  1056. tdesc_create_struct (struct tdesc_feature *feature, const char *name)
  1057. {
  1058.   struct tdesc_type *type = XCNEW (struct tdesc_type);

  1059.   type->name = xstrdup (name);
  1060.   type->kind = TDESC_TYPE_STRUCT;

  1061.   VEC_safe_push (tdesc_type_p, feature->types, type);
  1062.   return type;
  1063. }

  1064. /* Set the total length of TYPE.  Structs which contain bitfields may
  1065.    omit the reserved bits, so the end of the last field may not
  1066.    suffice.  */

  1067. void
  1068. tdesc_set_struct_size (struct tdesc_type *type, LONGEST size)
  1069. {
  1070.   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
  1071.   type->u.u.size = size;
  1072. }

  1073. struct tdesc_type *
  1074. tdesc_create_union (struct tdesc_feature *feature, const char *name)
  1075. {
  1076.   struct tdesc_type *type = XCNEW (struct tdesc_type);

  1077.   type->name = xstrdup (name);
  1078.   type->kind = TDESC_TYPE_UNION;

  1079.   VEC_safe_push (tdesc_type_p, feature->types, type);
  1080.   return type;
  1081. }

  1082. struct tdesc_type *
  1083. tdesc_create_flags (struct tdesc_feature *feature, const char *name,
  1084.                     LONGEST size)
  1085. {
  1086.   struct tdesc_type *type = XCNEW (struct tdesc_type);

  1087.   type->name = xstrdup (name);
  1088.   type->kind = TDESC_TYPE_FLAGS;
  1089.   type->u.f.size = size;

  1090.   VEC_safe_push (tdesc_type_p, feature->types, type);
  1091.   return type;
  1092. }

  1093. /* Add a new field.  Return a temporary pointer to the field, which
  1094.    is only valid until the next call to tdesc_add_field (the vector
  1095.    might be reallocated).  */

  1096. void
  1097. tdesc_add_field (struct tdesc_type *type, const char *field_name,
  1098.                  struct tdesc_type *field_type)
  1099. {
  1100.   struct tdesc_type_field f = { 0 };

  1101.   gdb_assert (type->kind == TDESC_TYPE_UNION
  1102.               || type->kind == TDESC_TYPE_STRUCT);

  1103.   f.name = xstrdup (field_name);
  1104.   f.type = field_type;

  1105.   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
  1106. }

  1107. /* Add a new bitfield.  */

  1108. void
  1109. tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
  1110.                     int start, int end)
  1111. {
  1112.   struct tdesc_type_field f = { 0 };

  1113.   gdb_assert (type->kind == TDESC_TYPE_STRUCT);

  1114.   f.name = xstrdup (field_name);
  1115.   f.start = start;
  1116.   f.end = end;

  1117.   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
  1118. }

  1119. void
  1120. tdesc_add_flag (struct tdesc_type *type, int start,
  1121.                 const char *flag_name)
  1122. {
  1123.   struct tdesc_type_flag f = { 0 };

  1124.   gdb_assert (type->kind == TDESC_TYPE_FLAGS);

  1125.   f.name = xstrdup (flag_name);
  1126.   f.start = start;

  1127.   VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
  1128. }

  1129. static void
  1130. tdesc_free_feature (struct tdesc_feature *feature)
  1131. {
  1132.   struct tdesc_reg *reg;
  1133.   struct tdesc_type *type;
  1134.   int ix;

  1135.   for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
  1136.     tdesc_free_reg (reg);
  1137.   VEC_free (tdesc_reg_p, feature->registers);

  1138.   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
  1139.     tdesc_free_type (type);
  1140.   VEC_free (tdesc_type_p, feature->types);

  1141.   xfree (feature->name);
  1142.   xfree (feature);
  1143. }

  1144. struct tdesc_feature *
  1145. tdesc_create_feature (struct target_desc *tdesc, const char *name)
  1146. {
  1147.   struct tdesc_feature *new_feature = XCNEW (struct tdesc_feature);

  1148.   new_feature->name = xstrdup (name);

  1149.   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
  1150.   return new_feature;
  1151. }

  1152. struct target_desc *
  1153. allocate_target_description (void)
  1154. {
  1155.   return XCNEW (struct target_desc);
  1156. }

  1157. static void
  1158. free_target_description (void *arg)
  1159. {
  1160.   struct target_desc *target_desc = arg;
  1161.   struct tdesc_feature *feature;
  1162.   struct property *prop;
  1163.   int ix;

  1164.   for (ix = 0;
  1165.        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
  1166.        ix++)
  1167.     tdesc_free_feature (feature);
  1168.   VEC_free (tdesc_feature_p, target_desc->features);

  1169.   for (ix = 0;
  1170.        VEC_iterate (property_s, target_desc->properties, ix, prop);
  1171.        ix++)
  1172.     {
  1173.       xfree (prop->key);
  1174.       xfree (prop->value);
  1175.     }
  1176.   VEC_free (property_s, target_desc->properties);

  1177.   VEC_free (arch_p, target_desc->compatible);

  1178.   xfree (target_desc);
  1179. }

  1180. struct cleanup *
  1181. make_cleanup_free_target_description (struct target_desc *target_desc)
  1182. {
  1183.   return make_cleanup (free_target_description, target_desc);
  1184. }

  1185. void
  1186. tdesc_add_compatible (struct target_desc *target_desc,
  1187.                       const struct bfd_arch_info *compatible)
  1188. {
  1189.   const struct bfd_arch_info *compat;
  1190.   int ix;

  1191.   /* If this instance of GDB is compiled without BFD support for the
  1192.      compatible architecture, simply ignore it -- we would not be able
  1193.      to handle it anyway.  */
  1194.   if (compatible == NULL)
  1195.     return;

  1196.   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
  1197.        ix++)
  1198.     if (compat == compatible)
  1199.       internal_error (__FILE__, __LINE__,
  1200.                       _("Attempted to add duplicate "
  1201.                         "compatible architecture \"%s\""),
  1202.                       compatible->printable_name);

  1203.   VEC_safe_push (arch_p, target_desc->compatible, compatible);
  1204. }

  1205. void
  1206. set_tdesc_property (struct target_desc *target_desc,
  1207.                     const char *key, const char *value)
  1208. {
  1209.   struct property *prop, new_prop;
  1210.   int ix;

  1211.   gdb_assert (key != NULL && value != NULL);

  1212.   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
  1213.        ix++)
  1214.     if (strcmp (prop->key, key) == 0)
  1215.       internal_error (__FILE__, __LINE__,
  1216.                       _("Attempted to add duplicate property \"%s\""), key);

  1217.   new_prop.key = xstrdup (key);
  1218.   new_prop.value = xstrdup (value);
  1219.   VEC_safe_push (property_s, target_desc->properties, &new_prop);
  1220. }

  1221. void
  1222. set_tdesc_architecture (struct target_desc *target_desc,
  1223.                         const struct bfd_arch_info *arch)
  1224. {
  1225.   target_desc->arch = arch;
  1226. }

  1227. void
  1228. set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
  1229. {
  1230.   target_desc->osabi = osabi;
  1231. }


  1232. static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
  1233. static struct cmd_list_element *tdesc_unset_cmdlist;

  1234. /* Helper functions for the CLI commands.  */

  1235. static void
  1236. set_tdesc_cmd (char *args, int from_tty)
  1237. {
  1238.   help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
  1239. }

  1240. static void
  1241. show_tdesc_cmd (char *args, int from_tty)
  1242. {
  1243.   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
  1244. }

  1245. static void
  1246. unset_tdesc_cmd (char *args, int from_tty)
  1247. {
  1248.   help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
  1249. }

  1250. static void
  1251. set_tdesc_filename_cmd (char *args, int from_tty,
  1252.                         struct cmd_list_element *c)
  1253. {
  1254.   xfree (target_description_filename);
  1255.   target_description_filename = xstrdup (tdesc_filename_cmd_string);

  1256.   target_clear_description ();
  1257.   target_find_description ();
  1258. }

  1259. static void
  1260. show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
  1261.                          struct cmd_list_element *c,
  1262.                          const char *value)
  1263. {
  1264.   value = target_description_filename;

  1265.   if (value != NULL && *value != '\0')
  1266.     printf_filtered (_("The target description will be read from \"%s\".\n"),
  1267.                      value);
  1268.   else
  1269.     printf_filtered (_("The target description will be "
  1270.                        "read from the target.\n"));
  1271. }

  1272. static void
  1273. unset_tdesc_filename_cmd (char *args, int from_tty)
  1274. {
  1275.   xfree (target_description_filename);
  1276.   target_description_filename = NULL;
  1277.   target_clear_description ();
  1278.   target_find_description ();
  1279. }

  1280. static void
  1281. maint_print_c_tdesc_cmd (char *args, int from_tty)
  1282. {
  1283.   const struct target_desc *tdesc;
  1284.   const struct bfd_arch_info *compatible;
  1285.   const char *filename, *inp;
  1286.   char *function, *outp;
  1287.   struct property *prop;
  1288.   struct tdesc_feature *feature;
  1289.   struct tdesc_reg *reg;
  1290.   struct tdesc_type *type;
  1291.   struct tdesc_type_field *f;
  1292.   struct tdesc_type_flag *flag;
  1293.   int ix, ix2, ix3;
  1294.   int printed_field_type = 0;

  1295.   /* Use the global target-supplied description, not the current
  1296.      architecture's.  This lets a GDB for one architecture generate C
  1297.      for another architecture's description, even though the gdbarch
  1298.      initialization code will reject the new description.  */
  1299.   tdesc = current_target_desc;
  1300.   if (tdesc == NULL)
  1301.     error (_("There is no target description to print."));

  1302.   if (target_description_filename == NULL)
  1303.     error (_("The current target description did not come from an XML file."));

  1304.   filename = lbasename (target_description_filename);
  1305.   function = alloca (strlen (filename) + 1);
  1306.   for (inp = filename, outp = function; *inp != '\0'; inp++)
  1307.     if (*inp == '.')
  1308.       break;
  1309.     else if (*inp == '-')
  1310.       *outp++ = '_';
  1311.     else
  1312.       *outp++ = *inp;
  1313.   *outp = '\0';

  1314.   /* Standard boilerplate.  */
  1315.   printf_unfiltered ("/* THIS FILE IS GENERATED.  "
  1316.                      "-*- buffer-read-only: t -*- vi"
  1317.                      ":set ro:\n");
  1318.   printf_unfiltered ("  Original: %s */\n\n", filename);
  1319.   printf_unfiltered ("#include \"defs.h\"\n");
  1320.   printf_unfiltered ("#include \"osabi.h\"\n");
  1321.   printf_unfiltered ("#include \"target-descriptions.h\"\n");
  1322.   printf_unfiltered ("\n");

  1323.   printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
  1324.   printf_unfiltered ("static void\n");
  1325.   printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
  1326.   printf_unfiltered ("{\n");
  1327.   printf_unfiltered
  1328.     ("  struct target_desc *result = allocate_target_description ();\n");
  1329.   printf_unfiltered ("  struct tdesc_feature *feature;\n");

  1330.   /* Now we do some "filtering" in order to know which variables to
  1331.      declare.  This is needed because otherwise we would declare unused
  1332.      variables `field_type' and `type'.  */
  1333.   for (ix = 0;
  1334.        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
  1335.        ix++)
  1336.     {
  1337.       int printed_desc_type = 0;

  1338.       for (ix2 = 0;
  1339.            VEC_iterate (tdesc_type_p, feature->types, ix2, type);
  1340.            ix2++)
  1341.         {
  1342.           if (!printed_field_type)
  1343.             {
  1344.               printf_unfiltered ("  struct tdesc_type *field_type;\n");
  1345.               printed_field_type = 1;
  1346.             }

  1347.           if ((type->kind == TDESC_TYPE_UNION
  1348.               || type->kind == TDESC_TYPE_STRUCT)
  1349.               && VEC_length (tdesc_type_field, type->u.u.fields) > 0)
  1350.             {
  1351.               printf_unfiltered ("  struct tdesc_type *type;\n");
  1352.               printed_desc_type = 1;
  1353.               break;
  1354.             }
  1355.         }

  1356.       if (printed_desc_type)
  1357.         break;
  1358.     }

  1359.   printf_unfiltered ("\n");

  1360.   if (tdesc_architecture (tdesc) != NULL)
  1361.     {
  1362.       printf_unfiltered
  1363.         (set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
  1364.          tdesc_architecture (tdesc)->printable_name);
  1365.       printf_unfiltered ("\n");
  1366.     }

  1367.   if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
  1368.       && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
  1369.     {
  1370.       printf_unfiltered
  1371.         (set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
  1372.          gdbarch_osabi_name (tdesc_osabi (tdesc)));
  1373.       printf_unfiltered ("\n");
  1374.     }

  1375.   for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
  1376.        ix++)
  1377.     {
  1378.       printf_unfiltered
  1379.         (tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
  1380.          compatible->printable_name);
  1381.     }
  1382.   if (ix)
  1383.     printf_unfiltered ("\n");

  1384.   for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
  1385.        ix++)
  1386.     {
  1387.       printf_unfiltered (set_tdesc_property (result, \"%s\", \"%s\");\n",
  1388.               prop->key, prop->value);
  1389.     }

  1390.   for (ix = 0;
  1391.        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
  1392.        ix++)
  1393.     {
  1394.       printf_unfiltered (\
  1395. feature = tdesc_create_feature (result, \"%s\");\n",
  1396.                          feature->name);

  1397.       for (ix2 = 0;
  1398.            VEC_iterate (tdesc_type_p, feature->types, ix2, type);
  1399.            ix2++)
  1400.         {
  1401.           switch (type->kind)
  1402.             {
  1403.             case TDESC_TYPE_VECTOR:
  1404.               printf_unfiltered
  1405.                 ("  field_type = tdesc_named_type (feature, \"%s\");\n",
  1406.                  type->u.v.type->name);
  1407.               printf_unfiltered
  1408.                 (tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
  1409.                  type->name, type->u.v.count);
  1410.               break;
  1411.             case TDESC_TYPE_STRUCT:
  1412.               printf_unfiltered
  1413.                 (type = tdesc_create_struct (feature, \"%s\");\n",
  1414.                  type->name);
  1415.               if (type->u.u.size != 0)
  1416.                 printf_unfiltered
  1417.                   (tdesc_set_struct_size (type, %s);\n",
  1418.                    plongest (type->u.u.size));
  1419.               for (ix3 = 0;
  1420.                    VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
  1421.                    ix3++)
  1422.                 {
  1423.                   /* Going first for implicitly sized types, else part handles
  1424.                      bitfields.  As reported on xml-tdesc.c implicitly sized types
  1425.                      cannot contain a bitfield.  */
  1426.                   if (f->type != NULL)
  1427.                     {
  1428.                       printf_unfiltered
  1429.                         ("  field_type = tdesc_named_type (feature, \"%s\");\n",
  1430.                          f->type->name);
  1431.                       printf_unfiltered
  1432.                         (tdesc_add_field (type, \"%s\", field_type);\n",
  1433.                          f->name);
  1434.                     }
  1435.                   else
  1436.                     printf_unfiltered
  1437.                       (tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
  1438.                        f->name, f->start, f->end);
  1439.                 }
  1440.               break;
  1441.             case TDESC_TYPE_UNION:
  1442.               printf_unfiltered
  1443.                 (type = tdesc_create_union (feature, \"%s\");\n",
  1444.                  type->name);
  1445.               for (ix3 = 0;
  1446.                    VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
  1447.                    ix3++)
  1448.                 {
  1449.                   printf_unfiltered
  1450.                     ("  field_type = tdesc_named_type (feature, \"%s\");\n",
  1451.                      f->type->name);
  1452.                   printf_unfiltered
  1453.                     (tdesc_add_field (type, \"%s\", field_type);\n",
  1454.                      f->name);
  1455.                 }
  1456.               break;
  1457.             case TDESC_TYPE_FLAGS:
  1458.               printf_unfiltered
  1459.                 ("  field_type = tdesc_create_flags (feature, \"%s\", %d);\n",
  1460.                  type->name, (int) type->u.f.size);
  1461.               for (ix3 = 0;
  1462.                    VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
  1463.                                 flag);
  1464.                    ix3++)
  1465.                 printf_unfiltered
  1466.                   (tdesc_add_flag (field_type, %d, \"%s\");\n",
  1467.                    flag->start, flag->name);
  1468.               break;
  1469.             default:
  1470.               error (_("C output is not supported type \"%s\"."), type->name);
  1471.             }
  1472.           printf_unfiltered ("\n");
  1473.         }

  1474.       for (ix2 = 0;
  1475.            VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
  1476.            ix2++)
  1477.         {
  1478.           printf_unfiltered (tdesc_create_reg (feature, \"%s\", %ld, %d, ",
  1479.                              reg->name, reg->target_regnum, reg->save_restore);
  1480.           if (reg->group)
  1481.             printf_unfiltered ("\"%s\", ", reg->group);
  1482.           else
  1483.             printf_unfiltered ("NULL, ");
  1484.           printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
  1485.         }

  1486.       printf_unfiltered ("\n");
  1487.     }

  1488.   printf_unfiltered ("  tdesc_%s = result;\n", function);
  1489.   printf_unfiltered ("}\n");
  1490. }

  1491. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1492. extern initialize_file_ftype _initialize_target_descriptions;

  1493. void
  1494. _initialize_target_descriptions (void)
  1495. {
  1496.   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);

  1497.   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
  1498. Set target description specific variables."),
  1499.                   &tdesc_set_cmdlist, "set tdesc ",
  1500.                   0 /* allow-unknown */, &setlist);
  1501.   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
  1502. Show target description specific variables."),
  1503.                   &tdesc_show_cmdlist, "show tdesc ",
  1504.                   0 /* allow-unknown */, &showlist);
  1505.   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
  1506. Unset target description specific variables."),
  1507.                   &tdesc_unset_cmdlist, "unset tdesc ",
  1508.                   0 /* allow-unknown */, &unsetlist);

  1509.   add_setshow_filename_cmd ("filename", class_obscure,
  1510.                             &tdesc_filename_cmd_string,
  1511.                             _("\
  1512. Set the file to read for an XML target description"), _("\
  1513. Show the file to read for an XML target description"), _("\
  1514. When set, GDB will read the target description from a local\n\
  1515. file instead of querying the remote target."),
  1516.                             set_tdesc_filename_cmd,
  1517.                             show_tdesc_filename_cmd,
  1518.                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);

  1519.   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
  1520. Unset the file to read for an XML target description.  When unset,\n\
  1521. GDB will read the description from the target."),
  1522.            &tdesc_unset_cmdlist);

  1523.   add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
  1524. Print the current target description as a C source file."),
  1525.            &maintenanceprintlist);
  1526. }