GDB (API)
/home/stan/gdb/src/gdb/target-descriptions.c
Go to the documentation of this file.
00001 /* Target description support for GDB.
00002 
00003    Copyright (C) 2006-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by CodeSourcery.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "arch-utils.h"
00024 #include "gdbcmd.h"
00025 #include "gdbtypes.h"
00026 #include "reggroups.h"
00027 #include "target.h"
00028 #include "target-descriptions.h"
00029 #include "vec.h"
00030 #include "xml-support.h"
00031 #include "xml-tdesc.h"
00032 #include "osabi.h"
00033 
00034 #include "gdb_assert.h"
00035 #include "gdb_obstack.h"
00036 #include "hashtab.h"
00037 #include "inferior.h"
00038 
00039 /* Types.  */
00040 
00041 typedef struct property
00042 {
00043   char *key;
00044   char *value;
00045 } property_s;
00046 DEF_VEC_O(property_s);
00047 
00048 /* An individual register from a target description.  */
00049 
00050 typedef struct tdesc_reg
00051 {
00052   /* The name of this register.  In standard features, it may be
00053      recognized by the architecture support code, or it may be purely
00054      for the user.  */
00055   char *name;
00056 
00057   /* The register number used by this target to refer to this
00058      register.  This is used for remote p/P packets and to determine
00059      the ordering of registers in the remote g/G packets.  */
00060   long target_regnum;
00061 
00062   /* If this flag is set, GDB should save and restore this register
00063      around calls to an inferior function.  */
00064   int save_restore;
00065 
00066   /* The name of the register group containing this register, or NULL
00067      if the group should be automatically determined from the
00068      register's type.  If this is "general", "float", or "vector", the
00069      corresponding "info" command should display this register's
00070      value.  It can be an arbitrary string, but should be limited to
00071      alphanumeric characters and internal hyphens.  Currently other
00072      strings are ignored (treated as NULL).  */
00073   char *group;
00074 
00075   /* The size of the register, in bits.  */
00076   int bitsize;
00077 
00078   /* The type of the register.  This string corresponds to either
00079      a named type from the target description or a predefined
00080      type from GDB.  */
00081   char *type;
00082 
00083   /* The target-described type corresponding to TYPE, if found.  */
00084   struct tdesc_type *tdesc_type;
00085 } *tdesc_reg_p;
00086 DEF_VEC_P(tdesc_reg_p);
00087 
00088 /* A named type from a target description.  */
00089 
00090 typedef struct tdesc_type_field
00091 {
00092   char *name;
00093   struct tdesc_type *type;
00094   int start, end;
00095 } tdesc_type_field;
00096 DEF_VEC_O(tdesc_type_field);
00097 
00098 typedef struct tdesc_type_flag
00099 {
00100   char *name;
00101   int start;
00102 } tdesc_type_flag;
00103 DEF_VEC_O(tdesc_type_flag);
00104 
00105 typedef struct tdesc_type
00106 {
00107   /* The name of this type.  */
00108   char *name;
00109 
00110   /* Identify the kind of this type.  */
00111   enum
00112   {
00113     /* Predefined types.  */
00114     TDESC_TYPE_INT8,
00115     TDESC_TYPE_INT16,
00116     TDESC_TYPE_INT32,
00117     TDESC_TYPE_INT64,
00118     TDESC_TYPE_INT128,
00119     TDESC_TYPE_UINT8,
00120     TDESC_TYPE_UINT16,
00121     TDESC_TYPE_UINT32,
00122     TDESC_TYPE_UINT64,
00123     TDESC_TYPE_UINT128,
00124     TDESC_TYPE_CODE_PTR,
00125     TDESC_TYPE_DATA_PTR,
00126     TDESC_TYPE_IEEE_SINGLE,
00127     TDESC_TYPE_IEEE_DOUBLE,
00128     TDESC_TYPE_ARM_FPA_EXT,
00129     TDESC_TYPE_I387_EXT,
00130 
00131     /* Types defined by a target feature.  */
00132     TDESC_TYPE_VECTOR,
00133     TDESC_TYPE_STRUCT,
00134     TDESC_TYPE_UNION,
00135     TDESC_TYPE_FLAGS
00136   } kind;
00137 
00138   /* Kind-specific data.  */
00139   union
00140   {
00141     /* Vector type.  */
00142     struct
00143     {
00144       struct tdesc_type *type;
00145       int count;
00146     } v;
00147 
00148     /* Struct or union type.  */
00149     struct
00150     {
00151       VEC(tdesc_type_field) *fields;
00152       LONGEST size;
00153     } u;
00154 
00155     /* Flags type.  */
00156     struct
00157     {
00158       VEC(tdesc_type_flag) *flags;
00159       LONGEST size;
00160     } f;
00161   } u;
00162 } *tdesc_type_p;
00163 DEF_VEC_P(tdesc_type_p);
00164 
00165 /* A feature from a target description.  Each feature is a collection
00166    of other elements, e.g. registers and types.  */
00167 
00168 typedef struct tdesc_feature
00169 {
00170   /* The name of this feature.  It may be recognized by the architecture
00171      support code.  */
00172   char *name;
00173 
00174   /* The registers associated with this feature.  */
00175   VEC(tdesc_reg_p) *registers;
00176 
00177   /* The types associated with this feature.  */
00178   VEC(tdesc_type_p) *types;
00179 } *tdesc_feature_p;
00180 DEF_VEC_P(tdesc_feature_p);
00181 
00182 /* A compatible architecture from a target description.  */
00183 typedef const struct bfd_arch_info *arch_p;
00184 DEF_VEC_P(arch_p);
00185 
00186 /* A target description.  */
00187 
00188 struct target_desc
00189 {
00190   /* The architecture reported by the target, if any.  */
00191   const struct bfd_arch_info *arch;
00192 
00193   /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
00194      otherwise.  */
00195   enum gdb_osabi osabi;
00196 
00197   /* The list of compatible architectures reported by the target.  */
00198   VEC(arch_p) *compatible;
00199 
00200   /* Any architecture-specific properties specified by the target.  */
00201   VEC(property_s) *properties;
00202 
00203   /* The features associated with this target.  */
00204   VEC(tdesc_feature_p) *features;
00205 };
00206 
00207 /* Per-architecture data associated with a target description.  The
00208    target description may be shared by multiple architectures, but
00209    this data is private to one gdbarch.  */
00210 
00211 typedef struct tdesc_arch_reg
00212 {
00213   struct tdesc_reg *reg;
00214   struct type *type;
00215 } tdesc_arch_reg;
00216 DEF_VEC_O(tdesc_arch_reg);
00217 
00218 struct tdesc_arch_data
00219 {
00220   /* A list of register/type pairs, indexed by GDB's internal register number.
00221      During initialization of the gdbarch this list is used to store
00222      registers which the architecture assigns a fixed register number.
00223      Registers which are NULL in this array, or off the end, are
00224      treated as zero-sized and nameless (i.e. placeholders in the
00225      numbering).  */
00226   VEC(tdesc_arch_reg) *arch_regs;
00227 
00228   /* Functions which report the register name, type, and reggroups for
00229      pseudo-registers.  */
00230   gdbarch_register_name_ftype *pseudo_register_name;
00231   gdbarch_register_type_ftype *pseudo_register_type;
00232   gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
00233 };
00234 
00235 /* Info about an inferior's target description.  There's one of these
00236    for each inferior.  */
00237 
00238 struct target_desc_info
00239 {
00240   /* A flag indicating that a description has already been fetched
00241      from the target, so it should not be queried again.  */
00242 
00243   int fetched;
00244 
00245   /* The description fetched from the target, or NULL if the target
00246      did not supply any description.  Only valid when
00247      target_desc_fetched is set.  Only the description initialization
00248      code should access this; normally, the description should be
00249      accessed through the gdbarch object.  */
00250 
00251   const struct target_desc *tdesc;
00252 
00253   /* The filename to read a target description from, as set by "set
00254      tdesc filename ..."  */
00255 
00256   char *filename;
00257 };
00258 
00259 /* Get the inferior INF's target description info, allocating one on
00260    the stop if necessary.  */
00261 
00262 static struct target_desc_info *
00263 get_tdesc_info (struct inferior *inf)
00264 {
00265   if (inf->tdesc_info == NULL)
00266     inf->tdesc_info = XCNEW (struct target_desc_info);
00267   return inf->tdesc_info;
00268 }
00269 
00270 /* A handle for architecture-specific data associated with the
00271    target description (see struct tdesc_arch_data).  */
00272 
00273 static struct gdbarch_data *tdesc_data;
00274 
00275 /* See target-descriptions.h.  */
00276 
00277 int
00278 target_desc_info_from_user_p (struct target_desc_info *info)
00279 {
00280   return info != NULL && info->filename != NULL;
00281 }
00282 
00283 /* See target-descriptions.h.  */
00284 
00285 void
00286 copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
00287 {
00288   struct target_desc_info *src = get_tdesc_info (srcinf);
00289   struct target_desc_info *dest = get_tdesc_info (destinf);
00290 
00291   dest->fetched = src->fetched;
00292   dest->tdesc = src->tdesc;
00293   dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
00294 }
00295 
00296 /* See target-descriptions.h.  */
00297 
00298 void
00299 target_desc_info_free (struct target_desc_info *tdesc_info)
00300 {
00301   if (tdesc_info != NULL)
00302     {
00303       xfree (tdesc_info->filename);
00304       xfree (tdesc_info);
00305     }
00306 }
00307 
00308 /* Convenience helper macros.  */
00309 
00310 #define target_desc_fetched \
00311   get_tdesc_info (current_inferior ())->fetched
00312 #define current_target_desc \
00313   get_tdesc_info (current_inferior ())->tdesc
00314 #define target_description_filename \
00315   get_tdesc_info (current_inferior ())->filename
00316 
00317 /* The string manipulated by the "set tdesc filename ..." command.  */
00318 
00319 static char *tdesc_filename_cmd_string;
00320 
00321 /* Fetch the current target's description, and switch the current
00322    architecture to one which incorporates that description.  */
00323 
00324 void
00325 target_find_description (void)
00326 {
00327   /* If we've already fetched a description from the target, don't do
00328      it again.  This allows a target to fetch the description early,
00329      during its to_open or to_create_inferior, if it needs extra
00330      information about the target to initialize.  */
00331   if (target_desc_fetched)
00332     return;
00333 
00334   /* The current architecture should not have any target description
00335      specified.  It should have been cleared, e.g. when we
00336      disconnected from the previous target.  */
00337   gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
00338 
00339   /* First try to fetch an XML description from the user-specified
00340      file.  */
00341   current_target_desc = NULL;
00342   if (target_description_filename != NULL
00343       && *target_description_filename != '\0')
00344     current_target_desc
00345       = file_read_description_xml (target_description_filename);
00346 
00347   /* Next try to read the description from the current target using
00348      target objects.  */
00349   if (current_target_desc == NULL)
00350     current_target_desc = target_read_description_xml (&current_target);
00351 
00352   /* If that failed try a target-specific hook.  */
00353   if (current_target_desc == NULL)
00354     current_target_desc = target_read_description (&current_target);
00355 
00356   /* If a non-NULL description was returned, then update the current
00357      architecture.  */
00358   if (current_target_desc)
00359     {
00360       struct gdbarch_info info;
00361 
00362       gdbarch_info_init (&info);
00363       info.target_desc = current_target_desc;
00364       if (!gdbarch_update_p (info))
00365         warning (_("Architecture rejected target-supplied description"));
00366       else
00367         {
00368           struct tdesc_arch_data *data;
00369 
00370           data = gdbarch_data (target_gdbarch (), tdesc_data);
00371           if (tdesc_has_registers (current_target_desc)
00372               && data->arch_regs == NULL)
00373             warning (_("Target-supplied registers are not supported "
00374                        "by the current architecture"));
00375         }
00376     }
00377 
00378   /* Now that we know this description is usable, record that we
00379      fetched it.  */
00380   target_desc_fetched = 1;
00381 }
00382 
00383 /* Discard any description fetched from the current target, and switch
00384    the current architecture to one with no target description.  */
00385 
00386 void
00387 target_clear_description (void)
00388 {
00389   struct gdbarch_info info;
00390 
00391   if (!target_desc_fetched)
00392     return;
00393 
00394   target_desc_fetched = 0;
00395   current_target_desc = NULL;
00396 
00397   gdbarch_info_init (&info);
00398   if (!gdbarch_update_p (info))
00399     internal_error (__FILE__, __LINE__,
00400                     _("Could not remove target-supplied description"));
00401 }
00402 
00403 /* Return the global current target description.  This should only be
00404    used by gdbarch initialization code; most access should be through
00405    an existing gdbarch.  */
00406 
00407 const struct target_desc *
00408 target_current_description (void)
00409 {
00410   if (target_desc_fetched)
00411     return current_target_desc;
00412 
00413   return NULL;
00414 }
00415 
00416 /* Return non-zero if this target description is compatible
00417    with the given BFD architecture.  */
00418 
00419 int
00420 tdesc_compatible_p (const struct target_desc *target_desc,
00421                     const struct bfd_arch_info *arch)
00422 {
00423   const struct bfd_arch_info *compat;
00424   int ix;
00425 
00426   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
00427        ix++)
00428     {
00429       if (compat == arch
00430           || arch->compatible (arch, compat)
00431           || compat->compatible (compat, arch))
00432         return 1;
00433     }
00434 
00435   return 0;
00436 }
00437 
00438 
00439 /* Direct accessors for target descriptions.  */
00440 
00441 /* Return the string value of a property named KEY, or NULL if the
00442    property was not specified.  */
00443 
00444 const char *
00445 tdesc_property (const struct target_desc *target_desc, const char *key)
00446 {
00447   struct property *prop;
00448   int ix;
00449 
00450   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
00451        ix++)
00452     if (strcmp (prop->key, key) == 0)
00453       return prop->value;
00454 
00455   return NULL;
00456 }
00457 
00458 /* Return the BFD architecture associated with this target
00459    description, or NULL if no architecture was specified.  */
00460 
00461 const struct bfd_arch_info *
00462 tdesc_architecture (const struct target_desc *target_desc)
00463 {
00464   return target_desc->arch;
00465 }
00466 
00467 /* Return the OSABI associated with this target description, or
00468    GDB_OSABI_UNKNOWN if no osabi was specified.  */
00469 
00470 enum gdb_osabi
00471 tdesc_osabi (const struct target_desc *target_desc)
00472 {
00473   return target_desc->osabi;
00474 }
00475 
00476 
00477 
00478 /* Return 1 if this target description includes any registers.  */
00479 
00480 int
00481 tdesc_has_registers (const struct target_desc *target_desc)
00482 {
00483   int ix;
00484   struct tdesc_feature *feature;
00485 
00486   if (target_desc == NULL)
00487     return 0;
00488 
00489   for (ix = 0;
00490        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
00491        ix++)
00492     if (! VEC_empty (tdesc_reg_p, feature->registers))
00493       return 1;
00494 
00495   return 0;
00496 }
00497 
00498 /* Return the feature with the given name, if present, or NULL if
00499    the named feature is not found.  */
00500 
00501 const struct tdesc_feature *
00502 tdesc_find_feature (const struct target_desc *target_desc,
00503                     const char *name)
00504 {
00505   int ix;
00506   struct tdesc_feature *feature;
00507 
00508   for (ix = 0;
00509        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
00510        ix++)
00511     if (strcmp (feature->name, name) == 0)
00512       return feature;
00513 
00514   return NULL;
00515 }
00516 
00517 /* Return the name of FEATURE.  */
00518 
00519 const char *
00520 tdesc_feature_name (const struct tdesc_feature *feature)
00521 {
00522   return feature->name;
00523 }
00524 
00525 /* Predefined types.  */
00526 static struct tdesc_type tdesc_predefined_types[] =
00527 {
00528   { "int8", TDESC_TYPE_INT8 },
00529   { "int16", TDESC_TYPE_INT16 },
00530   { "int32", TDESC_TYPE_INT32 },
00531   { "int64", TDESC_TYPE_INT64 },
00532   { "int128", TDESC_TYPE_INT128 },
00533   { "uint8", TDESC_TYPE_UINT8 },
00534   { "uint16", TDESC_TYPE_UINT16 },
00535   { "uint32", TDESC_TYPE_UINT32 },
00536   { "uint64", TDESC_TYPE_UINT64 },
00537   { "uint128", TDESC_TYPE_UINT128 },
00538   { "code_ptr", TDESC_TYPE_CODE_PTR },
00539   { "data_ptr", TDESC_TYPE_DATA_PTR },
00540   { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
00541   { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
00542   { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
00543   { "i387_ext", TDESC_TYPE_I387_EXT }
00544 };
00545 
00546 /* Return the type associated with ID in the context of FEATURE, or
00547    NULL if none.  */
00548 
00549 struct tdesc_type *
00550 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
00551 {
00552   int ix;
00553   struct tdesc_type *type;
00554 
00555   /* First try target-defined types.  */
00556   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
00557     if (strcmp (type->name, id) == 0)
00558       return type;
00559 
00560   /* Next try the predefined types.  */
00561   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
00562     if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
00563       return &tdesc_predefined_types[ix];
00564 
00565   return NULL;
00566 }
00567 
00568 /* Lookup type associated with ID.  */
00569 
00570 struct type *
00571 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
00572 {
00573   struct tdesc_arch_reg *reg;
00574   struct tdesc_arch_data *data;
00575   int i, num_regs;
00576 
00577   data = gdbarch_data (gdbarch, tdesc_data);
00578   num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
00579   for (i = 0; i < num_regs; i++)
00580     {
00581       reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
00582       if (reg->reg
00583           && reg->reg->tdesc_type
00584           && reg->type
00585           && strcmp (id, reg->reg->tdesc_type->name) == 0)
00586         return reg->type;
00587     }
00588 
00589   return NULL;
00590 }
00591 
00592 /* Construct, if necessary, and return the GDB type implementing target
00593    type TDESC_TYPE for architecture GDBARCH.  */
00594 
00595 static struct type *
00596 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
00597 {
00598   struct type *type;
00599 
00600   switch (tdesc_type->kind)
00601     {
00602     /* Predefined types.  */
00603     case TDESC_TYPE_INT8:
00604       return builtin_type (gdbarch)->builtin_int8;
00605 
00606     case TDESC_TYPE_INT16:
00607       return builtin_type (gdbarch)->builtin_int16;
00608 
00609     case TDESC_TYPE_INT32:
00610       return builtin_type (gdbarch)->builtin_int32;
00611 
00612     case TDESC_TYPE_INT64:
00613       return builtin_type (gdbarch)->builtin_int64;
00614 
00615     case TDESC_TYPE_INT128:
00616       return builtin_type (gdbarch)->builtin_int128;
00617 
00618     case TDESC_TYPE_UINT8:
00619       return builtin_type (gdbarch)->builtin_uint8;
00620 
00621     case TDESC_TYPE_UINT16:
00622       return builtin_type (gdbarch)->builtin_uint16;
00623 
00624     case TDESC_TYPE_UINT32:
00625       return builtin_type (gdbarch)->builtin_uint32;
00626 
00627     case TDESC_TYPE_UINT64:
00628       return builtin_type (gdbarch)->builtin_uint64;
00629 
00630     case TDESC_TYPE_UINT128:
00631       return builtin_type (gdbarch)->builtin_uint128;
00632 
00633     case TDESC_TYPE_CODE_PTR:
00634       return builtin_type (gdbarch)->builtin_func_ptr;
00635 
00636     case TDESC_TYPE_DATA_PTR:
00637       return builtin_type (gdbarch)->builtin_data_ptr;
00638 
00639     default:
00640       break;
00641     }
00642 
00643   type = tdesc_find_type (gdbarch, tdesc_type->name);
00644   if (type)
00645     return type;
00646 
00647   switch (tdesc_type->kind)
00648     {
00649     case TDESC_TYPE_IEEE_SINGLE:
00650       return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
00651                               floatformats_ieee_single);
00652 
00653     case TDESC_TYPE_IEEE_DOUBLE:
00654       return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
00655                               floatformats_ieee_double);
00656 
00657     case TDESC_TYPE_ARM_FPA_EXT:
00658       return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
00659                               floatformats_arm_ext);
00660 
00661     case TDESC_TYPE_I387_EXT:
00662       return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
00663                               floatformats_i387_ext);
00664 
00665     /* Types defined by a target feature.  */
00666     case TDESC_TYPE_VECTOR:
00667       {
00668         struct type *type, *field_type;
00669 
00670         field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
00671         type = init_vector_type (field_type, tdesc_type->u.v.count);
00672         TYPE_NAME (type) = xstrdup (tdesc_type->name);
00673 
00674         return type;
00675       }
00676 
00677     case TDESC_TYPE_STRUCT:
00678       {
00679         struct type *type, *field_type;
00680         struct tdesc_type_field *f;
00681         int ix;
00682 
00683         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
00684         TYPE_NAME (type) = xstrdup (tdesc_type->name);
00685         TYPE_TAG_NAME (type) = TYPE_NAME (type);
00686 
00687         for (ix = 0;
00688              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
00689              ix++)
00690           {
00691             if (f->type == NULL)
00692               {
00693                 /* Bitfield.  */
00694                 struct field *fld;
00695                 struct type *field_type;
00696                 int bitsize, total_size;
00697 
00698                 /* This invariant should be preserved while creating
00699                    types.  */
00700                 gdb_assert (tdesc_type->u.u.size != 0);
00701                 if (tdesc_type->u.u.size > 4)
00702                   field_type = builtin_type (gdbarch)->builtin_uint64;
00703                 else
00704                   field_type = builtin_type (gdbarch)->builtin_uint32;
00705 
00706                 fld = append_composite_type_field_raw (type, xstrdup (f->name),
00707                                                        field_type);
00708 
00709                 /* For little-endian, BITPOS counts from the LSB of
00710                    the structure and marks the LSB of the field.  For
00711                    big-endian, BITPOS counts from the MSB of the
00712                    structure and marks the MSB of the field.  Either
00713                    way, it is the number of bits to the "left" of the
00714                    field.  To calculate this in big-endian, we need
00715                    the total size of the structure.  */
00716                 bitsize = f->end - f->start + 1;
00717                 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
00718                 if (gdbarch_bits_big_endian (gdbarch))
00719                   SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
00720                 else
00721                   SET_FIELD_BITPOS (fld[0], f->start);
00722                 FIELD_BITSIZE (fld[0]) = bitsize;
00723               }
00724             else
00725               {
00726                 field_type = tdesc_gdb_type (gdbarch, f->type);
00727                 append_composite_type_field (type, xstrdup (f->name),
00728                                              field_type);
00729               }
00730           }
00731 
00732         if (tdesc_type->u.u.size != 0)
00733           TYPE_LENGTH (type) = tdesc_type->u.u.size;
00734         return type;
00735       }
00736 
00737     case TDESC_TYPE_UNION:
00738       {
00739         struct type *type, *field_type;
00740         struct tdesc_type_field *f;
00741         int ix;
00742 
00743         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
00744         TYPE_NAME (type) = xstrdup (tdesc_type->name);
00745 
00746         for (ix = 0;
00747              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
00748              ix++)
00749           {
00750             field_type = tdesc_gdb_type (gdbarch, f->type);
00751             append_composite_type_field (type, xstrdup (f->name), field_type);
00752 
00753             /* If any of the children of a union are vectors, flag the
00754                union as a vector also.  This allows e.g. a union of two
00755                vector types to show up automatically in "info vector".  */
00756             if (TYPE_VECTOR (field_type))
00757               TYPE_VECTOR (type) = 1;
00758           }
00759         return type;
00760       }
00761 
00762     case TDESC_TYPE_FLAGS:
00763       {
00764         struct tdesc_type_flag *f;
00765         int ix;
00766 
00767         type = arch_flags_type (gdbarch, tdesc_type->name,
00768                                 tdesc_type->u.f.size);
00769         for (ix = 0;
00770              VEC_iterate (tdesc_type_flag, tdesc_type->u.f.flags, ix, f);
00771              ix++)
00772           /* Note that contrary to the function name, this call will
00773              just set the properties of an already-allocated
00774              field.  */
00775           append_flags_type_flag (type, f->start,
00776                                   *f->name ? f->name : NULL);
00777 
00778         return type;
00779       }
00780     }
00781 
00782   internal_error (__FILE__, __LINE__,
00783                   "Type \"%s\" has an unknown kind %d",
00784                   tdesc_type->name, tdesc_type->kind);
00785 }
00786 
00787 
00788 /* Support for registers from target descriptions.  */
00789 
00790 /* Construct the per-gdbarch data.  */
00791 
00792 static void *
00793 tdesc_data_init (struct obstack *obstack)
00794 {
00795   struct tdesc_arch_data *data;
00796 
00797   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
00798   return data;
00799 }
00800 
00801 /* Similar, but for the temporary copy used during architecture
00802    initialization.  */
00803 
00804 struct tdesc_arch_data *
00805 tdesc_data_alloc (void)
00806 {
00807   return XZALLOC (struct tdesc_arch_data);
00808 }
00809 
00810 /* Free something allocated by tdesc_data_alloc, if it is not going
00811    to be used (for instance if it was unsuitable for the
00812    architecture).  */
00813 
00814 void
00815 tdesc_data_cleanup (void *data_untyped)
00816 {
00817   struct tdesc_arch_data *data = data_untyped;
00818 
00819   VEC_free (tdesc_arch_reg, data->arch_regs);
00820   xfree (data);
00821 }
00822 
00823 /* Search FEATURE for a register named NAME.  */
00824 
00825 static struct tdesc_reg *
00826 tdesc_find_register_early (const struct tdesc_feature *feature,
00827                            const char *name)
00828 {
00829   int ixr;
00830   struct tdesc_reg *reg;
00831 
00832   for (ixr = 0;
00833        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
00834        ixr++)
00835     if (strcasecmp (reg->name, name) == 0)
00836       return reg;
00837 
00838   return NULL;
00839 }
00840 
00841 /* Search FEATURE for a register named NAME.  Assign REGNO to it.  */
00842 
00843 int
00844 tdesc_numbered_register (const struct tdesc_feature *feature,
00845                          struct tdesc_arch_data *data,
00846                          int regno, const char *name)
00847 {
00848   struct tdesc_arch_reg arch_reg = { 0 };
00849   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
00850 
00851   if (reg == NULL)
00852     return 0;
00853 
00854   /* Make sure the vector includes a REGNO'th element.  */
00855   while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
00856     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
00857 
00858   arch_reg.reg = reg;
00859   VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
00860   return 1;
00861 }
00862 
00863 /* Search FEATURE for a register named NAME, but do not assign a fixed
00864    register number to it.  */
00865 
00866 int
00867 tdesc_unnumbered_register (const struct tdesc_feature *feature,
00868                            const char *name)
00869 {
00870   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
00871 
00872   if (reg == NULL)
00873     return 0;
00874 
00875   return 1;
00876 }
00877 
00878 /* Search FEATURE for a register whose name is in NAMES and assign
00879    REGNO to it.  */
00880 
00881 int
00882 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
00883                                  struct tdesc_arch_data *data,
00884                                  int regno, const char *const names[])
00885 {
00886   int i;
00887 
00888   for (i = 0; names[i] != NULL; i++)
00889     if (tdesc_numbered_register (feature, data, regno, names[i]))
00890       return 1;
00891 
00892   return 0;
00893 }
00894 
00895 /* Search FEATURE for a register named NAME, and return its size in
00896    bits.  The register must exist.  */
00897 
00898 int
00899 tdesc_register_size (const struct tdesc_feature *feature,
00900                      const char *name)
00901 {
00902   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
00903 
00904   gdb_assert (reg != NULL);
00905   return reg->bitsize;
00906 }
00907 
00908 /* Look up a register by its GDB internal register number.  */
00909 
00910 static struct tdesc_arch_reg *
00911 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
00912 {
00913   struct tdesc_arch_data *data;
00914 
00915   data = gdbarch_data (gdbarch, tdesc_data);
00916   if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
00917     return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
00918   else
00919     return NULL;
00920 }
00921 
00922 static struct tdesc_reg *
00923 tdesc_find_register (struct gdbarch *gdbarch, int regno)
00924 {
00925   struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
00926 
00927   return reg? reg->reg : NULL;
00928 }
00929 
00930 /* Return the name of register REGNO, from the target description or
00931    from an architecture-provided pseudo_register_name method.  */
00932 
00933 const char *
00934 tdesc_register_name (struct gdbarch *gdbarch, int regno)
00935 {
00936   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
00937   int num_regs = gdbarch_num_regs (gdbarch);
00938   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
00939 
00940   if (reg != NULL)
00941     return reg->name;
00942 
00943   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
00944     {
00945       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
00946 
00947       gdb_assert (data->pseudo_register_name != NULL);
00948       return data->pseudo_register_name (gdbarch, regno);
00949     }
00950 
00951   return "";
00952 }
00953 
00954 struct type *
00955 tdesc_register_type (struct gdbarch *gdbarch, int regno)
00956 {
00957   struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
00958   struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
00959   int num_regs = gdbarch_num_regs (gdbarch);
00960   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
00961 
00962   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
00963     {
00964       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
00965 
00966       gdb_assert (data->pseudo_register_type != NULL);
00967       return data->pseudo_register_type (gdbarch, regno);
00968     }
00969 
00970   if (reg == NULL)
00971     /* Return "int0_t", since "void" has a misleading size of one.  */
00972     return builtin_type (gdbarch)->builtin_int0;
00973 
00974   if (arch_reg->type == NULL)
00975     {
00976       /* First check for a predefined or target defined type.  */
00977       if (reg->tdesc_type)
00978         arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
00979 
00980       /* Next try size-sensitive type shortcuts.  */
00981       else if (strcmp (reg->type, "float") == 0)
00982         {
00983           if (reg->bitsize == gdbarch_float_bit (gdbarch))
00984             arch_reg->type = builtin_type (gdbarch)->builtin_float;
00985           else if (reg->bitsize == gdbarch_double_bit (gdbarch))
00986             arch_reg->type = builtin_type (gdbarch)->builtin_double;
00987           else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
00988             arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
00989           else
00990             {
00991               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
00992                        reg->name, reg->bitsize);
00993               arch_reg->type = builtin_type (gdbarch)->builtin_double;
00994             }
00995         }
00996       else if (strcmp (reg->type, "int") == 0)
00997         {
00998           if (reg->bitsize == gdbarch_long_bit (gdbarch))
00999             arch_reg->type = builtin_type (gdbarch)->builtin_long;
01000           else if (reg->bitsize == TARGET_CHAR_BIT)
01001             arch_reg->type = builtin_type (gdbarch)->builtin_char;
01002           else if (reg->bitsize == gdbarch_short_bit (gdbarch))
01003             arch_reg->type = builtin_type (gdbarch)->builtin_short;
01004           else if (reg->bitsize == gdbarch_int_bit (gdbarch))
01005             arch_reg->type = builtin_type (gdbarch)->builtin_int;
01006           else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
01007             arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
01008           else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
01009           /* A bit desperate by this point...  */
01010             arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
01011           else
01012             {
01013               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
01014                        reg->name, reg->bitsize);
01015               arch_reg->type = builtin_type (gdbarch)->builtin_long;
01016             }
01017         }
01018 
01019       if (arch_reg->type == NULL)
01020         internal_error (__FILE__, __LINE__,
01021                         "Register \"%s\" has an unknown type \"%s\"",
01022                         reg->name, reg->type);
01023     }
01024 
01025   return arch_reg->type;
01026 }
01027 
01028 static int
01029 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
01030 {
01031   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
01032 
01033   if (reg != NULL)
01034     return reg->target_regnum;
01035   else
01036     return -1;
01037 }
01038 
01039 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
01040    target description may be classified as general, float, or vector.
01041    Unlike a gdbarch register_reggroup_p method, this function will
01042    return -1 if it does not know; the caller should handle registers
01043    with no specified group.
01044 
01045    Arbitrary strings (other than "general", "float", and "vector")
01046    from the description are not used; they cause the register to be
01047    displayed in "info all-registers" but excluded from "info
01048    registers" et al.  The names of containing features are also not
01049    used.  This might be extended to display registers in some more
01050    useful groupings.
01051 
01052    The save-restore flag is also implemented here.  */
01053 
01054 int
01055 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
01056                               struct reggroup *reggroup)
01057 {
01058   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
01059 
01060   if (reg != NULL && reg->group != NULL)
01061     {
01062       int general_p = 0, float_p = 0, vector_p = 0;
01063 
01064       if (strcmp (reg->group, "general") == 0)
01065         general_p = 1;
01066       else if (strcmp (reg->group, "float") == 0)
01067         float_p = 1;
01068       else if (strcmp (reg->group, "vector") == 0)
01069         vector_p = 1;
01070 
01071       if (reggroup == float_reggroup)
01072         return float_p;
01073 
01074       if (reggroup == vector_reggroup)
01075         return vector_p;
01076 
01077       if (reggroup == general_reggroup)
01078         return general_p;
01079     }
01080 
01081   if (reg != NULL
01082       && (reggroup == save_reggroup || reggroup == restore_reggroup))
01083     return reg->save_restore;
01084 
01085   return -1;
01086 }
01087 
01088 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
01089    group specified go to the default reggroup function and are handled
01090    by type.  */
01091 
01092 static int
01093 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
01094                            struct reggroup *reggroup)
01095 {
01096   int num_regs = gdbarch_num_regs (gdbarch);
01097   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
01098   int ret;
01099 
01100   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
01101     {
01102       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
01103 
01104       if (data->pseudo_register_reggroup_p != NULL)
01105         return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
01106       /* Otherwise fall through to the default reggroup_p.  */
01107     }
01108 
01109   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
01110   if (ret != -1)
01111     return ret;
01112 
01113   return default_register_reggroup_p (gdbarch, regno, reggroup);
01114 }
01115 
01116 /* Record architecture-specific functions to call for pseudo-register
01117    support.  */
01118 
01119 void
01120 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
01121                                 gdbarch_register_name_ftype *pseudo_name)
01122 {
01123   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
01124 
01125   data->pseudo_register_name = pseudo_name;
01126 }
01127 
01128 void
01129 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
01130                                 gdbarch_register_type_ftype *pseudo_type)
01131 {
01132   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
01133 
01134   data->pseudo_register_type = pseudo_type;
01135 }
01136 
01137 void
01138 set_tdesc_pseudo_register_reggroup_p
01139   (struct gdbarch *gdbarch,
01140    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
01141 {
01142   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
01143 
01144   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
01145 }
01146 
01147 /* Update GDBARCH to use the target description for registers.  */
01148 
01149 void
01150 tdesc_use_registers (struct gdbarch *gdbarch,
01151                      const struct target_desc *target_desc,
01152                      struct tdesc_arch_data *early_data)
01153 {
01154   int num_regs = gdbarch_num_regs (gdbarch);
01155   int ixf, ixr;
01156   struct tdesc_feature *feature;
01157   struct tdesc_reg *reg;
01158   struct tdesc_arch_data *data;
01159   struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
01160   htab_t reg_hash;
01161 
01162   /* We can't use the description for registers if it doesn't describe
01163      any.  This function should only be called after validating
01164      registers, so the caller should know that registers are
01165      included.  */
01166   gdb_assert (tdesc_has_registers (target_desc));
01167 
01168   data = gdbarch_data (gdbarch, tdesc_data);
01169   data->arch_regs = early_data->arch_regs;
01170   xfree (early_data);
01171 
01172   /* Build up a set of all registers, so that we can assign register
01173      numbers where needed.  The hash table expands as necessary, so
01174      the initial size is arbitrary.  */
01175   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
01176   for (ixf = 0;
01177        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
01178        ixf++)
01179     for (ixr = 0;
01180          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
01181          ixr++)
01182       {
01183         void **slot = htab_find_slot (reg_hash, reg, INSERT);
01184 
01185         *slot = reg;
01186       }
01187 
01188   /* Remove any registers which were assigned numbers by the
01189      architecture.  */
01190   for (ixr = 0;
01191        VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
01192        ixr++)
01193     if (arch_reg->reg)
01194       htab_remove_elt (reg_hash, arch_reg->reg);
01195 
01196   /* Assign numbers to the remaining registers and add them to the
01197      list of registers.  The new numbers are always above gdbarch_num_regs.
01198      Iterate over the features, not the hash table, so that the order
01199      matches that in the target description.  */
01200 
01201   gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
01202   while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
01203     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
01204   for (ixf = 0;
01205        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
01206        ixf++)
01207     for (ixr = 0;
01208          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
01209          ixr++)
01210       if (htab_find (reg_hash, reg) != NULL)
01211         {
01212           new_arch_reg.reg = reg;
01213           VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
01214           num_regs++;
01215         }
01216 
01217   htab_delete (reg_hash);
01218 
01219   /* Update the architecture.  */
01220   set_gdbarch_num_regs (gdbarch, num_regs);
01221   set_gdbarch_register_name (gdbarch, tdesc_register_name);
01222   set_gdbarch_register_type (gdbarch, tdesc_register_type);
01223   set_gdbarch_remote_register_number (gdbarch,
01224                                       tdesc_remote_register_number);
01225   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
01226 }
01227 
01228 
01229 /* Methods for constructing a target description.  */
01230 
01231 static void
01232 tdesc_free_reg (struct tdesc_reg *reg)
01233 {
01234   xfree (reg->name);
01235   xfree (reg->type);
01236   xfree (reg->group);
01237   xfree (reg);
01238 }
01239 
01240 void
01241 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
01242                   int regnum, int save_restore, const char *group,
01243                   int bitsize, const char *type)
01244 {
01245   struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
01246 
01247   reg->name = xstrdup (name);
01248   reg->target_regnum = regnum;
01249   reg->save_restore = save_restore;
01250   reg->group = group ? xstrdup (group) : NULL;
01251   reg->bitsize = bitsize;
01252   reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
01253 
01254   /* If the register's type is target-defined, look it up now.  We may not
01255      have easy access to the containing feature when we want it later.  */
01256   reg->tdesc_type = tdesc_named_type (feature, reg->type);
01257 
01258   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
01259 }
01260 
01261 static void
01262 tdesc_free_type (struct tdesc_type *type)
01263 {
01264   switch (type->kind)
01265     {
01266     case TDESC_TYPE_STRUCT:
01267     case TDESC_TYPE_UNION:
01268       {
01269         struct tdesc_type_field *f;
01270         int ix;
01271 
01272         for (ix = 0;
01273              VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
01274              ix++)
01275           xfree (f->name);
01276 
01277         VEC_free (tdesc_type_field, type->u.u.fields);
01278       }
01279       break;
01280 
01281     case TDESC_TYPE_FLAGS:
01282       {
01283         struct tdesc_type_flag *f;
01284         int ix;
01285 
01286         for (ix = 0;
01287              VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
01288              ix++)
01289           xfree (f->name);
01290 
01291         VEC_free (tdesc_type_flag, type->u.f.flags);
01292       }
01293       break;
01294 
01295     default:
01296       break;
01297     }
01298 
01299   xfree (type->name);
01300   xfree (type);
01301 }
01302 
01303 struct tdesc_type *
01304 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
01305                      struct tdesc_type *field_type, int count)
01306 {
01307   struct tdesc_type *type = XZALLOC (struct tdesc_type);
01308 
01309   type->name = xstrdup (name);
01310   type->kind = TDESC_TYPE_VECTOR;
01311   type->u.v.type = field_type;
01312   type->u.v.count = count;
01313 
01314   VEC_safe_push (tdesc_type_p, feature->types, type);
01315   return type;
01316 }
01317 
01318 struct tdesc_type *
01319 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
01320 {
01321   struct tdesc_type *type = XZALLOC (struct tdesc_type);
01322 
01323   type->name = xstrdup (name);
01324   type->kind = TDESC_TYPE_STRUCT;
01325 
01326   VEC_safe_push (tdesc_type_p, feature->types, type);
01327   return type;
01328 }
01329 
01330 /* Set the total length of TYPE.  Structs which contain bitfields may
01331    omit the reserved bits, so the end of the last field may not
01332    suffice.  */
01333 
01334 void
01335 tdesc_set_struct_size (struct tdesc_type *type, LONGEST size)
01336 {
01337   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
01338   type->u.u.size = size;
01339 }
01340 
01341 struct tdesc_type *
01342 tdesc_create_union (struct tdesc_feature *feature, const char *name)
01343 {
01344   struct tdesc_type *type = XZALLOC (struct tdesc_type);
01345 
01346   type->name = xstrdup (name);
01347   type->kind = TDESC_TYPE_UNION;
01348 
01349   VEC_safe_push (tdesc_type_p, feature->types, type);
01350   return type;
01351 }
01352 
01353 struct tdesc_type *
01354 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
01355                     LONGEST size)
01356 {
01357   struct tdesc_type *type = XZALLOC (struct tdesc_type);
01358 
01359   type->name = xstrdup (name);
01360   type->kind = TDESC_TYPE_FLAGS;
01361   type->u.f.size = size;
01362 
01363   VEC_safe_push (tdesc_type_p, feature->types, type);
01364   return type;
01365 }
01366 
01367 /* Add a new field.  Return a temporary pointer to the field, which
01368    is only valid until the next call to tdesc_add_field (the vector
01369    might be reallocated).  */
01370 
01371 void
01372 tdesc_add_field (struct tdesc_type *type, const char *field_name,
01373                  struct tdesc_type *field_type)
01374 {
01375   struct tdesc_type_field f = { 0 };
01376 
01377   gdb_assert (type->kind == TDESC_TYPE_UNION
01378               || type->kind == TDESC_TYPE_STRUCT);
01379 
01380   f.name = xstrdup (field_name);
01381   f.type = field_type;
01382 
01383   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
01384 }
01385 
01386 /* Add a new bitfield.  */
01387 
01388 void
01389 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
01390                     int start, int end)
01391 {
01392   struct tdesc_type_field f = { 0 };
01393 
01394   gdb_assert (type->kind == TDESC_TYPE_STRUCT);
01395 
01396   f.name = xstrdup (field_name);
01397   f.start = start;
01398   f.end = end;
01399 
01400   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
01401 }
01402 
01403 void
01404 tdesc_add_flag (struct tdesc_type *type, int start,
01405                 const char *flag_name)
01406 {
01407   struct tdesc_type_flag f = { 0 };
01408 
01409   gdb_assert (type->kind == TDESC_TYPE_FLAGS);
01410 
01411   f.name = xstrdup (flag_name);
01412   f.start = start;
01413 
01414   VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
01415 }
01416 
01417 static void
01418 tdesc_free_feature (struct tdesc_feature *feature)
01419 {
01420   struct tdesc_reg *reg;
01421   struct tdesc_type *type;
01422   int ix;
01423 
01424   for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
01425     tdesc_free_reg (reg);
01426   VEC_free (tdesc_reg_p, feature->registers);
01427 
01428   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
01429     tdesc_free_type (type);
01430   VEC_free (tdesc_type_p, feature->types);
01431 
01432   xfree (feature->name);
01433   xfree (feature);
01434 }
01435 
01436 struct tdesc_feature *
01437 tdesc_create_feature (struct target_desc *tdesc, const char *name)
01438 {
01439   struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
01440 
01441   new_feature->name = xstrdup (name);
01442 
01443   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
01444   return new_feature;
01445 }
01446 
01447 struct target_desc *
01448 allocate_target_description (void)
01449 {
01450   return XZALLOC (struct target_desc);
01451 }
01452 
01453 static void
01454 free_target_description (void *arg)
01455 {
01456   struct target_desc *target_desc = arg;
01457   struct tdesc_feature *feature;
01458   struct property *prop;
01459   int ix;
01460 
01461   for (ix = 0;
01462        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
01463        ix++)
01464     tdesc_free_feature (feature);
01465   VEC_free (tdesc_feature_p, target_desc->features);
01466 
01467   for (ix = 0;
01468        VEC_iterate (property_s, target_desc->properties, ix, prop);
01469        ix++)
01470     {
01471       xfree (prop->key);
01472       xfree (prop->value);
01473     }
01474   VEC_free (property_s, target_desc->properties);
01475 
01476   VEC_free (arch_p, target_desc->compatible);
01477 
01478   xfree (target_desc);
01479 }
01480 
01481 struct cleanup *
01482 make_cleanup_free_target_description (struct target_desc *target_desc)
01483 {
01484   return make_cleanup (free_target_description, target_desc);
01485 }
01486 
01487 void
01488 tdesc_add_compatible (struct target_desc *target_desc,
01489                       const struct bfd_arch_info *compatible)
01490 {
01491   const struct bfd_arch_info *compat;
01492   int ix;
01493 
01494   /* If this instance of GDB is compiled without BFD support for the
01495      compatible architecture, simply ignore it -- we would not be able
01496      to handle it anyway.  */
01497   if (compatible == NULL)
01498     return;
01499 
01500   for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
01501        ix++)
01502     if (compat == compatible)
01503       internal_error (__FILE__, __LINE__,
01504                       _("Attempted to add duplicate "
01505                         "compatible architecture \"%s\""),
01506                       compatible->printable_name);
01507 
01508   VEC_safe_push (arch_p, target_desc->compatible, compatible);
01509 }
01510 
01511 void
01512 set_tdesc_property (struct target_desc *target_desc,
01513                     const char *key, const char *value)
01514 {
01515   struct property *prop, new_prop;
01516   int ix;
01517 
01518   gdb_assert (key != NULL && value != NULL);
01519 
01520   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
01521        ix++)
01522     if (strcmp (prop->key, key) == 0)
01523       internal_error (__FILE__, __LINE__,
01524                       _("Attempted to add duplicate property \"%s\""), key);
01525 
01526   new_prop.key = xstrdup (key);
01527   new_prop.value = xstrdup (value);
01528   VEC_safe_push (property_s, target_desc->properties, &new_prop);
01529 }
01530 
01531 void
01532 set_tdesc_architecture (struct target_desc *target_desc,
01533                         const struct bfd_arch_info *arch)
01534 {
01535   target_desc->arch = arch;
01536 }
01537 
01538 void
01539 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
01540 {
01541   target_desc->osabi = osabi;
01542 }
01543 
01544 
01545 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
01546 static struct cmd_list_element *tdesc_unset_cmdlist;
01547 
01548 /* Helper functions for the CLI commands.  */
01549 
01550 static void
01551 set_tdesc_cmd (char *args, int from_tty)
01552 {
01553   help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
01554 }
01555 
01556 static void
01557 show_tdesc_cmd (char *args, int from_tty)
01558 {
01559   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
01560 }
01561 
01562 static void
01563 unset_tdesc_cmd (char *args, int from_tty)
01564 {
01565   help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
01566 }
01567 
01568 static void
01569 set_tdesc_filename_cmd (char *args, int from_tty,
01570                         struct cmd_list_element *c)
01571 {
01572   xfree (target_description_filename);
01573   target_description_filename = xstrdup (tdesc_filename_cmd_string);
01574 
01575   target_clear_description ();
01576   target_find_description ();
01577 }
01578 
01579 static void
01580 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
01581                          struct cmd_list_element *c,
01582                          const char *value)
01583 {
01584   value = target_description_filename;
01585 
01586   if (value != NULL && *value != '\0')
01587     printf_filtered (_("The target description will be read from \"%s\".\n"),
01588                      value);
01589   else
01590     printf_filtered (_("The target description will be "
01591                        "read from the target.\n"));
01592 }
01593 
01594 static void
01595 unset_tdesc_filename_cmd (char *args, int from_tty)
01596 {
01597   xfree (target_description_filename);
01598   target_description_filename = NULL;
01599   target_clear_description ();
01600   target_find_description ();
01601 }
01602 
01603 static void
01604 maint_print_c_tdesc_cmd (char *args, int from_tty)
01605 {
01606   const struct target_desc *tdesc;
01607   const struct bfd_arch_info *compatible;
01608   const char *filename, *inp;
01609   char *function, *outp;
01610   struct property *prop;
01611   struct tdesc_feature *feature;
01612   struct tdesc_reg *reg;
01613   struct tdesc_type *type;
01614   struct tdesc_type_field *f;
01615   struct tdesc_type_flag *flag;
01616   int ix, ix2, ix3;
01617   int printed_field_type = 0;
01618 
01619   /* Use the global target-supplied description, not the current
01620      architecture's.  This lets a GDB for one architecture generate C
01621      for another architecture's description, even though the gdbarch
01622      initialization code will reject the new description.  */
01623   tdesc = current_target_desc;
01624   if (tdesc == NULL)
01625     error (_("There is no target description to print."));
01626 
01627   if (target_description_filename == NULL)
01628     error (_("The current target description did not come from an XML file."));
01629 
01630   filename = lbasename (target_description_filename);
01631   function = alloca (strlen (filename) + 1);
01632   for (inp = filename, outp = function; *inp != '\0'; inp++)
01633     if (*inp == '.')
01634       break;
01635     else if (*inp == '-')
01636       *outp++ = '_';
01637     else
01638       *outp++ = *inp;
01639   *outp = '\0';
01640 
01641   /* Standard boilerplate.  */
01642   printf_unfiltered ("/* THIS FILE IS GENERATED.  "
01643                      "-*- buffer-read-only: t -*- vi"
01644                      ":set ro:\n");
01645   printf_unfiltered ("  Original: %s */\n\n", filename);
01646   printf_unfiltered ("#include \"defs.h\"\n");
01647   printf_unfiltered ("#include \"osabi.h\"\n");
01648   printf_unfiltered ("#include \"target-descriptions.h\"\n");
01649   printf_unfiltered ("\n");
01650 
01651   printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
01652   printf_unfiltered ("static void\n");
01653   printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
01654   printf_unfiltered ("{\n");
01655   printf_unfiltered
01656     ("  struct target_desc *result = allocate_target_description ();\n");
01657   printf_unfiltered ("  struct tdesc_feature *feature;\n");
01658 
01659   /* Now we do some "filtering" in order to know which variables to
01660      declare.  This is needed because otherwise we would declare unused
01661      variables `field_type' and `type'.  */
01662   for (ix = 0;
01663        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
01664        ix++)
01665     {
01666       int printed_desc_type = 0;
01667 
01668       for (ix2 = 0;
01669            VEC_iterate (tdesc_type_p, feature->types, ix2, type);
01670            ix2++)
01671         {
01672           if (!printed_field_type)
01673             {
01674               printf_unfiltered ("  struct tdesc_type *field_type;\n");
01675               printed_field_type = 1;
01676             }
01677 
01678           if ((type->kind == TDESC_TYPE_UNION
01679               || type->kind == TDESC_TYPE_STRUCT)
01680               && VEC_length (tdesc_type_field, type->u.u.fields) > 0)
01681             {
01682               printf_unfiltered ("  struct tdesc_type *type;\n");
01683               printed_desc_type = 1;
01684               break;
01685             }
01686         }
01687 
01688       if (printed_desc_type)
01689         break;
01690     }
01691 
01692   printf_unfiltered ("\n");
01693 
01694   if (tdesc_architecture (tdesc) != NULL)
01695     {
01696       printf_unfiltered
01697         ("  set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
01698          tdesc_architecture (tdesc)->printable_name);
01699       printf_unfiltered ("\n");
01700     }
01701 
01702   if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
01703       && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
01704     {
01705       printf_unfiltered
01706         ("  set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
01707          gdbarch_osabi_name (tdesc_osabi (tdesc)));
01708       printf_unfiltered ("\n");
01709     }
01710 
01711   for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
01712        ix++)
01713     {
01714       printf_unfiltered
01715         ("  tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
01716          compatible->printable_name);
01717     }
01718   if (ix)
01719     printf_unfiltered ("\n");
01720 
01721   for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
01722        ix++)
01723     {
01724       printf_unfiltered ("  set_tdesc_property (result, \"%s\", \"%s\");\n",
01725               prop->key, prop->value);
01726     }
01727 
01728   for (ix = 0;
01729        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
01730        ix++)
01731     {
01732       printf_unfiltered ("  \
01733 feature = tdesc_create_feature (result, \"%s\");\n",
01734                          feature->name);
01735 
01736       for (ix2 = 0;
01737            VEC_iterate (tdesc_type_p, feature->types, ix2, type);
01738            ix2++)
01739         {
01740           switch (type->kind)
01741             {
01742             case TDESC_TYPE_VECTOR:
01743               printf_unfiltered
01744                 ("  field_type = tdesc_named_type (feature, \"%s\");\n",
01745                  type->u.v.type->name);
01746               printf_unfiltered
01747                 ("  tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
01748                  type->name, type->u.v.count);
01749               break;
01750             case TDESC_TYPE_STRUCT:
01751               printf_unfiltered
01752                 ("  type = tdesc_create_struct (feature, \"%s\");\n",
01753                  type->name);
01754               if (type->u.u.size != 0)
01755                 printf_unfiltered
01756                   ("  tdesc_set_struct_size (type, %s);\n",
01757                    plongest (type->u.u.size));
01758               for (ix3 = 0;
01759                    VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
01760                    ix3++)
01761                 {
01762                   /* Going first for implicitly sized types, else part handles
01763                      bitfields.  As reported on xml-tdesc.c implicitly sized types
01764                      cannot contain a bitfield.  */
01765                   if (f->start == 0 && f->end == 0)
01766                     {
01767                       printf_unfiltered
01768                         ("  field_type = tdesc_named_type (feature, \"%s\");\n",
01769                          f->type->name);
01770                       printf_unfiltered
01771                         ("  tdesc_add_field (type, \"%s\", field_type);\n",
01772                          f->name);
01773                     }
01774                   else
01775                     printf_unfiltered
01776                       ("  tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
01777                        f->name, f->start, f->end);
01778                 }
01779               break;
01780             case TDESC_TYPE_UNION:
01781               printf_unfiltered
01782                 ("  type = tdesc_create_union (feature, \"%s\");\n",
01783                  type->name);
01784               for (ix3 = 0;
01785                    VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
01786                    ix3++)
01787                 {
01788                   printf_unfiltered
01789                     ("  field_type = tdesc_named_type (feature, \"%s\");\n",
01790                      f->type->name);
01791                   printf_unfiltered
01792                     ("  tdesc_add_field (type, \"%s\", field_type);\n",
01793                      f->name);
01794                 }
01795               break;
01796             case TDESC_TYPE_FLAGS:
01797               printf_unfiltered
01798                 ("  field_type = tdesc_create_flags (feature, \"%s\", %d);\n",
01799                  type->name, (int) type->u.f.size);
01800               for (ix3 = 0;
01801                    VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
01802                                 flag);
01803                    ix3++)
01804                 printf_unfiltered
01805                   ("  tdesc_add_flag (field_type, %d, \"%s\");\n",
01806                    flag->start, flag->name);
01807               break;
01808             default:
01809               error (_("C output is not supported type \"%s\"."), type->name);
01810             }
01811           printf_unfiltered ("\n");
01812         }
01813 
01814       for (ix2 = 0;
01815            VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
01816            ix2++)
01817         {
01818           printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
01819                              reg->name, reg->target_regnum, reg->save_restore);
01820           if (reg->group)
01821             printf_unfiltered ("\"%s\", ", reg->group);
01822           else
01823             printf_unfiltered ("NULL, ");
01824           printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
01825         }
01826 
01827       printf_unfiltered ("\n");
01828     }
01829 
01830   printf_unfiltered ("  tdesc_%s = result;\n", function);
01831   printf_unfiltered ("}\n");
01832 }
01833 
01834 /* Provide a prototype to silence -Wmissing-prototypes.  */
01835 extern initialize_file_ftype _initialize_target_descriptions;
01836 
01837 void
01838 _initialize_target_descriptions (void)
01839 {
01840   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
01841 
01842   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
01843 Set target description specific variables."),
01844                   &tdesc_set_cmdlist, "set tdesc ",
01845                   0 /* allow-unknown */, &setlist);
01846   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
01847 Show target description specific variables."),
01848                   &tdesc_show_cmdlist, "show tdesc ",
01849                   0 /* allow-unknown */, &showlist);
01850   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
01851 Unset target description specific variables."),
01852                   &tdesc_unset_cmdlist, "unset tdesc ",
01853                   0 /* allow-unknown */, &unsetlist);
01854 
01855   add_setshow_filename_cmd ("filename", class_obscure,
01856                             &tdesc_filename_cmd_string,
01857                             _("\
01858 Set the file to read for an XML target description"), _("\
01859 Show the file to read for an XML target description"), _("\
01860 When set, GDB will read the target description from a local\n\
01861 file instead of querying the remote target."),
01862                             set_tdesc_filename_cmd,
01863                             show_tdesc_filename_cmd,
01864                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);
01865 
01866   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
01867 Unset the file to read for an XML target description.  When unset,\n\
01868 GDB will read the description from the target."),
01869            &tdesc_unset_cmdlist);
01870 
01871   add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
01872 Print the current target description as a C source file."),
01873            &maintenanceprintlist);
01874 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines