* gdbtypes.h (TYPE_OBJFILE_OWNED, TYPE_OWNER): New macros.
[external/binutils.git] / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3    Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5    Contributed by CodeSourcery.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "gdbcmd.h"
25 #include "gdbtypes.h"
26 #include "reggroups.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "vec.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
32
33 #include "gdb_assert.h"
34 #include "gdb_obstack.h"
35 #include "hashtab.h"
36
37 /* Types.  */
38
39 typedef struct property
40 {
41   char *key;
42   char *value;
43 } property_s;
44 DEF_VEC_O(property_s);
45
46 /* An individual register from a target description.  */
47
48 typedef struct tdesc_reg
49 {
50   /* The name of this register.  In standard features, it may be
51      recognized by the architecture support code, or it may be purely
52      for the user.  */
53   char *name;
54
55   /* The register number used by this target to refer to this
56      register.  This is used for remote p/P packets and to determine
57      the ordering of registers in the remote g/G packets.  */
58   long target_regnum;
59
60   /* If this flag is set, GDB should save and restore this register
61      around calls to an inferior function.  */
62   int save_restore;
63
64   /* The name of the register group containing this register, or NULL
65      if the group should be automatically determined from the
66      register's type.  If this is "general", "float", or "vector", the
67      corresponding "info" command should display this register's
68      value.  It can be an arbitrary string, but should be limited to
69      alphanumeric characters and internal hyphens.  Currently other
70      strings are ignored (treated as NULL).  */
71   char *group;
72
73   /* The size of the register, in bits.  */
74   int bitsize;
75
76   /* The type of the register.  This string corresponds to either
77      a named type from the target description or a predefined
78      type from GDB.  */
79   char *type;
80
81   /* The target-described type corresponding to TYPE, if found.  */
82   struct tdesc_type *tdesc_type;
83 } *tdesc_reg_p;
84 DEF_VEC_P(tdesc_reg_p);
85
86 /* A named type from a target description.  */
87
88 typedef struct tdesc_type_field
89 {
90   char *name;
91   struct tdesc_type *type;
92 } tdesc_type_field;
93 DEF_VEC_O(tdesc_type_field);
94
95 typedef struct tdesc_type
96 {
97   /* The name of this type.  */
98   char *name;
99
100   /* Identify the kind of this type.  */
101   enum
102   {
103     /* Predefined types.  */
104     TDESC_TYPE_INT8,
105     TDESC_TYPE_INT16,
106     TDESC_TYPE_INT32,
107     TDESC_TYPE_INT64,
108     TDESC_TYPE_INT128,
109     TDESC_TYPE_UINT8,
110     TDESC_TYPE_UINT16,
111     TDESC_TYPE_UINT32,
112     TDESC_TYPE_UINT64,
113     TDESC_TYPE_UINT128,
114     TDESC_TYPE_CODE_PTR,
115     TDESC_TYPE_DATA_PTR,
116     TDESC_TYPE_IEEE_SINGLE,
117     TDESC_TYPE_IEEE_DOUBLE,
118     TDESC_TYPE_ARM_FPA_EXT,
119
120     /* Types defined by a target feature.  */
121     TDESC_TYPE_VECTOR,
122     TDESC_TYPE_UNION
123   } kind;
124
125   /* Kind-specific data.  */
126   union
127   {
128     /* Vector type.  */
129     struct
130     {
131       struct tdesc_type *type;
132       int count;
133     } v;
134
135     /* Union type.  */
136     struct
137     {
138       VEC(tdesc_type_field) *fields;
139     } u;
140   } u;
141 } *tdesc_type_p;
142 DEF_VEC_P(tdesc_type_p);
143
144 /* A feature from a target description.  Each feature is a collection
145    of other elements, e.g. registers and types.  */
146
147 typedef struct tdesc_feature
148 {
149   /* The name of this feature.  It may be recognized by the architecture
150      support code.  */
151   char *name;
152
153   /* The registers associated with this feature.  */
154   VEC(tdesc_reg_p) *registers;
155
156   /* The types associated with this feature.  */
157   VEC(tdesc_type_p) *types;
158 } *tdesc_feature_p;
159 DEF_VEC_P(tdesc_feature_p);
160
161 /* A target description.  */
162
163 struct target_desc
164 {
165   /* The architecture reported by the target, if any.  */
166   const struct bfd_arch_info *arch;
167
168   /* Any architecture-specific properties specified by the target.  */
169   VEC(property_s) *properties;
170
171   /* The features associated with this target.  */
172   VEC(tdesc_feature_p) *features;
173 };
174
175 /* Per-architecture data associated with a target description.  The
176    target description may be shared by multiple architectures, but
177    this data is private to one gdbarch.  */
178
179 typedef struct tdesc_arch_reg
180 {
181   struct tdesc_reg *reg;
182   struct type *type;
183 } tdesc_arch_reg;
184 DEF_VEC_O(tdesc_arch_reg);
185
186 struct tdesc_arch_data
187 {
188   /* A list of register/type pairs, indexed by GDB's internal register number.
189      During initialization of the gdbarch this list is used to store
190      registers which the architecture assigns a fixed register number.
191      Registers which are NULL in this array, or off the end, are
192      treated as zero-sized and nameless (i.e. placeholders in the
193      numbering).  */
194   VEC(tdesc_arch_reg) *arch_regs;
195
196   /* Functions which report the register name, type, and reggroups for
197      pseudo-registers.  */
198   gdbarch_register_name_ftype *pseudo_register_name;
199   gdbarch_register_type_ftype *pseudo_register_type;
200   gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p;
201 };
202
203 /* Global state.  These variables are associated with the current
204    target; if GDB adds support for multiple simultaneous targets, then
205    these variables should become target-specific data.  */
206
207 /* A flag indicating that a description has already been fetched from
208    the current target, so it should not be queried again.  */
209
210 static int target_desc_fetched;
211
212 /* The description fetched from the current target, or NULL if the
213    current target did not supply any description.  Only valid when
214    target_desc_fetched is set.  Only the description initialization
215    code should access this; normally, the description should be
216    accessed through the gdbarch object.  */
217
218 static const struct target_desc *current_target_desc;
219
220 /* Other global variables.  */
221
222 /* The filename to read a target description from.  */
223
224 static char *target_description_filename;
225
226 /* A handle for architecture-specific data associated with the
227    target description (see struct tdesc_arch_data).  */
228
229 static struct gdbarch_data *tdesc_data;
230
231 /* Fetch the current target's description, and switch the current
232    architecture to one which incorporates that description.  */
233
234 void
235 target_find_description (void)
236 {
237   /* If we've already fetched a description from the target, don't do
238      it again.  This allows a target to fetch the description early,
239      during its to_open or to_create_inferior, if it needs extra
240      information about the target to initialize.  */
241   if (target_desc_fetched)
242     return;
243
244   /* The current architecture should not have any target description
245      specified.  It should have been cleared, e.g. when we
246      disconnected from the previous target.  */
247   gdb_assert (gdbarch_target_desc (target_gdbarch) == NULL);
248
249   /* First try to fetch an XML description from the user-specified
250      file.  */
251   current_target_desc = NULL;
252   if (target_description_filename != NULL
253       && *target_description_filename != '\0')
254     current_target_desc
255       = file_read_description_xml (target_description_filename);
256
257   /* Next try to read the description from the current target using
258      target objects.  */
259   if (current_target_desc == NULL)
260     current_target_desc = target_read_description_xml (&current_target);
261
262   /* If that failed try a target-specific hook.  */
263   if (current_target_desc == NULL)
264     current_target_desc = target_read_description (&current_target);
265
266   /* If a non-NULL description was returned, then update the current
267      architecture.  */
268   if (current_target_desc)
269     {
270       struct gdbarch_info info;
271
272       gdbarch_info_init (&info);
273       info.target_desc = current_target_desc;
274       if (!gdbarch_update_p (info))
275         warning (_("Architecture rejected target-supplied description"));
276       else
277         {
278           struct tdesc_arch_data *data;
279
280           data = gdbarch_data (target_gdbarch, tdesc_data);
281           if (tdesc_has_registers (current_target_desc)
282               && data->arch_regs == NULL)
283             warning (_("Target-supplied registers are not supported "
284                        "by the current architecture"));
285         }
286     }
287
288   /* Now that we know this description is usable, record that we
289      fetched it.  */
290   target_desc_fetched = 1;
291 }
292
293 /* Discard any description fetched from the current target, and switch
294    the current architecture to one with no target description.  */
295
296 void
297 target_clear_description (void)
298 {
299   struct gdbarch_info info;
300
301   if (!target_desc_fetched)
302     return;
303
304   target_desc_fetched = 0;
305   current_target_desc = NULL;
306
307   gdbarch_info_init (&info);
308   if (!gdbarch_update_p (info))
309     internal_error (__FILE__, __LINE__,
310                     _("Could not remove target-supplied description"));
311 }
312
313 /* Return the global current target description.  This should only be
314    used by gdbarch initialization code; most access should be through
315    an existing gdbarch.  */
316
317 const struct target_desc *
318 target_current_description (void)
319 {
320   if (target_desc_fetched)
321     return current_target_desc;
322
323   return NULL;
324 }
325 \f
326
327 /* Direct accessors for target descriptions.  */
328
329 /* Return the string value of a property named KEY, or NULL if the
330    property was not specified.  */
331
332 const char *
333 tdesc_property (const struct target_desc *target_desc, const char *key)
334 {
335   struct property *prop;
336   int ix;
337
338   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
339        ix++)
340     if (strcmp (prop->key, key) == 0)
341       return prop->value;
342
343   return NULL;
344 }
345
346 /* Return the BFD architecture associated with this target
347    description, or NULL if no architecture was specified.  */
348
349 const struct bfd_arch_info *
350 tdesc_architecture (const struct target_desc *target_desc)
351 {
352   return target_desc->arch;
353 }
354 \f
355
356 /* Return 1 if this target description includes any registers.  */
357
358 int
359 tdesc_has_registers (const struct target_desc *target_desc)
360 {
361   int ix;
362   struct tdesc_feature *feature;
363
364   if (target_desc == NULL)
365     return 0;
366
367   for (ix = 0;
368        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
369        ix++)
370     if (! VEC_empty (tdesc_reg_p, feature->registers))
371       return 1;
372
373   return 0;
374 }
375
376 /* Return the feature with the given name, if present, or NULL if
377    the named feature is not found.  */
378
379 const struct tdesc_feature *
380 tdesc_find_feature (const struct target_desc *target_desc,
381                     const char *name)
382 {
383   int ix;
384   struct tdesc_feature *feature;
385
386   for (ix = 0;
387        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
388        ix++)
389     if (strcmp (feature->name, name) == 0)
390       return feature;
391
392   return NULL;
393 }
394
395 /* Return the name of FEATURE.  */
396
397 const char *
398 tdesc_feature_name (const struct tdesc_feature *feature)
399 {
400   return feature->name;
401 }
402
403 /* Predefined types.  */
404 static struct tdesc_type tdesc_predefined_types[] =
405 {
406   { "int8", TDESC_TYPE_INT8 },
407   { "int16", TDESC_TYPE_INT16 },
408   { "int32", TDESC_TYPE_INT32 },
409   { "int64", TDESC_TYPE_INT64 },
410   { "int128", TDESC_TYPE_INT128 },
411   { "uint8", TDESC_TYPE_UINT8 },
412   { "uint16", TDESC_TYPE_UINT16 },
413   { "uint32", TDESC_TYPE_UINT32 },
414   { "uint64", TDESC_TYPE_UINT64 },
415   { "uint128", TDESC_TYPE_UINT128 },
416   { "code_ptr", TDESC_TYPE_CODE_PTR },
417   { "data_ptr", TDESC_TYPE_DATA_PTR },
418   { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
419   { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
420   { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT }
421 };
422
423 /* Return the type associated with ID in the context of FEATURE, or
424    NULL if none.  */
425
426 struct tdesc_type *
427 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
428 {
429   int ix;
430   struct tdesc_type *type;
431
432   /* First try target-defined types.  */
433   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
434     if (strcmp (type->name, id) == 0)
435       return type;
436
437   /* Next try the predefined types.  */
438   for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
439     if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
440       return &tdesc_predefined_types[ix];
441
442   return NULL;
443 }
444
445 /* Construct, if necessary, and return the GDB type implementing target
446    type TDESC_TYPE for architecture GDBARCH.  */
447
448 static struct type *
449 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
450 {
451   switch (tdesc_type->kind)
452     {
453     /* Predefined types.  */
454     case TDESC_TYPE_INT8:
455       return builtin_type (gdbarch)->builtin_int8;
456
457     case TDESC_TYPE_INT16:
458       return builtin_type (gdbarch)->builtin_int16;
459
460     case TDESC_TYPE_INT32:
461       return builtin_type (gdbarch)->builtin_int32;
462
463     case TDESC_TYPE_INT64:
464       return builtin_type (gdbarch)->builtin_int64;
465
466     case TDESC_TYPE_INT128:
467       return builtin_type (gdbarch)->builtin_int128;
468
469     case TDESC_TYPE_UINT8:
470       return builtin_type (gdbarch)->builtin_uint8;
471
472     case TDESC_TYPE_UINT16:
473       return builtin_type (gdbarch)->builtin_uint16;
474
475     case TDESC_TYPE_UINT32:
476       return builtin_type (gdbarch)->builtin_uint32;
477
478     case TDESC_TYPE_UINT64:
479       return builtin_type (gdbarch)->builtin_uint64;
480
481     case TDESC_TYPE_UINT128:
482       return builtin_type (gdbarch)->builtin_uint128;
483
484     case TDESC_TYPE_CODE_PTR:
485       return builtin_type (gdbarch)->builtin_func_ptr;
486
487     case TDESC_TYPE_DATA_PTR:
488       return builtin_type (gdbarch)->builtin_data_ptr;
489
490     case TDESC_TYPE_IEEE_SINGLE:
491       return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
492                               floatformats_ieee_single);
493
494     case TDESC_TYPE_IEEE_DOUBLE:
495       return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
496                               floatformats_ieee_double);
497
498     case TDESC_TYPE_ARM_FPA_EXT:
499       return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
500                               floatformats_arm_ext);
501
502     /* Types defined by a target feature.  */
503     case TDESC_TYPE_VECTOR:
504       {
505         struct type *type, *field_type;
506
507         field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
508         type = init_vector_type (field_type, tdesc_type->u.v.count);
509         TYPE_NAME (type) = xstrdup (tdesc_type->name);
510
511         return type;
512       }
513
514     case TDESC_TYPE_UNION:
515       {
516         struct type *type, *field_type;
517         struct tdesc_type_field *f;
518         int ix;
519
520         type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
521         TYPE_NAME (type) = xstrdup (tdesc_type->name);
522
523         for (ix = 0;
524              VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
525              ix++)
526           {
527             field_type = tdesc_gdb_type (gdbarch, f->type);
528             append_composite_type_field (type, xstrdup (f->name), field_type);
529
530             /* If any of the children of this union are vectors, flag the
531                union as a vector also.  This allows e.g. a union of two
532                vector types to show up automatically in "info vector".  */
533             if (TYPE_VECTOR (field_type))
534               TYPE_VECTOR (type) = 1;
535           }
536
537         return type;
538       }
539     }
540
541   internal_error (__FILE__, __LINE__,
542                   "Type \"%s\" has an unknown kind %d",
543                   tdesc_type->name, tdesc_type->kind);
544 }
545 \f
546
547 /* Support for registers from target descriptions.  */
548
549 /* Construct the per-gdbarch data.  */
550
551 static void *
552 tdesc_data_init (struct obstack *obstack)
553 {
554   struct tdesc_arch_data *data;
555
556   data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
557   return data;
558 }
559
560 /* Similar, but for the temporary copy used during architecture
561    initialization.  */
562
563 struct tdesc_arch_data *
564 tdesc_data_alloc (void)
565 {
566   return XZALLOC (struct tdesc_arch_data);
567 }
568
569 /* Free something allocated by tdesc_data_alloc, if it is not going
570    to be used (for instance if it was unsuitable for the
571    architecture).  */
572
573 void
574 tdesc_data_cleanup (void *data_untyped)
575 {
576   struct tdesc_arch_data *data = data_untyped;
577
578   VEC_free (tdesc_arch_reg, data->arch_regs);
579   xfree (data);
580 }
581
582 /* Search FEATURE for a register named NAME.  */
583
584 static struct tdesc_reg *
585 tdesc_find_register_early (const struct tdesc_feature *feature,
586                            const char *name)
587 {
588   int ixr;
589   struct tdesc_reg *reg;
590
591   for (ixr = 0;
592        VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
593        ixr++)
594     if (strcasecmp (reg->name, name) == 0)
595       return reg;
596
597   return NULL;
598 }
599
600 /* Search FEATURE for a register named NAME.  Assign REGNO to it.  */
601
602 int
603 tdesc_numbered_register (const struct tdesc_feature *feature,
604                          struct tdesc_arch_data *data,
605                          int regno, const char *name)
606 {
607   struct tdesc_arch_reg arch_reg = { 0 };
608   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
609
610   if (reg == NULL)
611     return 0;
612
613   /* Make sure the vector includes a REGNO'th element.  */
614   while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
615     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
616
617   arch_reg.reg = reg;
618   VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
619   return 1;
620 }
621
622 /* Search FEATURE for a register whose name is in NAMES and assign
623    REGNO to it.  */
624
625 int
626 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
627                                  struct tdesc_arch_data *data,
628                                  int regno, const char *const names[])
629 {
630   int i;
631
632   for (i = 0; names[i] != NULL; i++)
633     if (tdesc_numbered_register (feature, data, regno, names[i]))
634       return 1;
635
636   return 0;
637 }
638
639 /* Search FEATURE for a register named NAME, and return its size in
640    bits.  The register must exist.  */
641
642 int
643 tdesc_register_size (const struct tdesc_feature *feature,
644                      const char *name)
645 {
646   struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
647
648   gdb_assert (reg != NULL);
649   return reg->bitsize;
650 }
651
652 /* Look up a register by its GDB internal register number.  */
653
654 static struct tdesc_arch_reg *
655 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
656 {
657   struct tdesc_arch_reg *reg;
658   struct tdesc_arch_data *data;
659
660   data = gdbarch_data (gdbarch, tdesc_data);
661   if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
662     return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
663   else
664     return NULL;
665 }
666
667 static struct tdesc_reg *
668 tdesc_find_register (struct gdbarch *gdbarch, int regno)
669 {
670   struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
671   return reg? reg->reg : NULL;
672 }
673
674 /* Return the name of register REGNO, from the target description or
675    from an architecture-provided pseudo_register_name method.  */
676
677 const char *
678 tdesc_register_name (struct gdbarch *gdbarch, int regno)
679 {
680   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
681   int num_regs = gdbarch_num_regs (gdbarch);
682   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
683
684   if (reg != NULL)
685     return reg->name;
686
687   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
688     {
689       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
690       gdb_assert (data->pseudo_register_name != NULL);
691       return data->pseudo_register_name (gdbarch, regno);
692     }
693
694   return "";
695 }
696
697 static struct type *
698 tdesc_register_type (struct gdbarch *gdbarch, int regno)
699 {
700   struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
701   struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
702   int num_regs = gdbarch_num_regs (gdbarch);
703   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
704
705   if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
706     {
707       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
708       gdb_assert (data->pseudo_register_type != NULL);
709       return data->pseudo_register_type (gdbarch, regno);
710     }
711
712   if (reg == NULL)
713     /* Return "int0_t", since "void" has a misleading size of one.  */
714     return builtin_type (gdbarch)->builtin_int0;
715
716   if (arch_reg->type == NULL)
717     {
718       /* First check for a predefined or target defined type.  */
719       if (reg->tdesc_type)
720         arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
721
722       /* Next try size-sensitive type shortcuts.  */
723       else if (strcmp (reg->type, "float") == 0)
724         {
725           if (reg->bitsize == gdbarch_float_bit (gdbarch))
726             arch_reg->type = builtin_type (gdbarch)->builtin_float;
727           else if (reg->bitsize == gdbarch_double_bit (gdbarch))
728             arch_reg->type = builtin_type (gdbarch)->builtin_double;
729           else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
730             arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
731           else
732             {
733               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
734                        reg->name, reg->bitsize);
735               arch_reg->type = builtin_type (gdbarch)->builtin_double;
736             }
737         }
738       else if (strcmp (reg->type, "int") == 0)
739         {
740           if (reg->bitsize == gdbarch_long_bit (gdbarch))
741             arch_reg->type = builtin_type (gdbarch)->builtin_long;
742           else if (reg->bitsize == TARGET_CHAR_BIT)
743             arch_reg->type = builtin_type (gdbarch)->builtin_char;
744           else if (reg->bitsize == gdbarch_short_bit (gdbarch))
745             arch_reg->type = builtin_type (gdbarch)->builtin_short;
746           else if (reg->bitsize == gdbarch_int_bit (gdbarch))
747             arch_reg->type = builtin_type (gdbarch)->builtin_int;
748           else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
749             arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
750           else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
751           /* A bit desperate by this point... */
752             arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
753           else
754             {
755               warning (_("Register \"%s\" has an unsupported size (%d bits)"),
756                        reg->name, reg->bitsize);
757               arch_reg->type = builtin_type (gdbarch)->builtin_long;
758             }
759         }
760
761       if (arch_reg->type == NULL)
762         internal_error (__FILE__, __LINE__,
763                         "Register \"%s\" has an unknown type \"%s\"",
764                         reg->name, reg->type);
765     }
766
767   return arch_reg->type;
768 }
769
770 static int
771 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
772 {
773   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
774
775   if (reg != NULL)
776     return reg->target_regnum;
777   else
778     return -1;
779 }
780
781 /* Check whether REGNUM is a member of REGGROUP.  Registers from the
782    target description may be classified as general, float, or vector.
783    Unlike a gdbarch register_reggroup_p method, this function will
784    return -1 if it does not know; the caller should handle registers
785    with no specified group.
786
787    Arbitrary strings (other than "general", "float", and "vector")
788    from the description are not used; they cause the register to be
789    displayed in "info all-registers" but excluded from "info
790    registers" et al.  The names of containing features are also not
791    used.  This might be extended to display registers in some more
792    useful groupings.
793
794    The save-restore flag is also implemented here.  */
795
796 int
797 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
798                               struct reggroup *reggroup)
799 {
800   struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
801
802   if (reg != NULL && reg->group != NULL)
803     {
804       int general_p = 0, float_p = 0, vector_p = 0;
805
806       if (strcmp (reg->group, "general") == 0)
807         general_p = 1;
808       else if (strcmp (reg->group, "float") == 0)
809         float_p = 1;
810       else if (strcmp (reg->group, "vector") == 0)
811         vector_p = 1;
812
813       if (reggroup == float_reggroup)
814         return float_p;
815
816       if (reggroup == vector_reggroup)
817         return vector_p;
818
819       if (reggroup == general_reggroup)
820         return general_p;
821     }
822
823   if (reg != NULL
824       && (reggroup == save_reggroup || reggroup == restore_reggroup))
825     return reg->save_restore;
826
827   return -1;
828 }
829
830 /* Check whether REGNUM is a member of REGGROUP.  Registers with no
831    group specified go to the default reggroup function and are handled
832    by type.  */
833
834 static int
835 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
836                            struct reggroup *reggroup)
837 {
838   int num_regs = gdbarch_num_regs (gdbarch);
839   int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
840   int ret;
841
842   if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
843     {
844       struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
845       gdb_assert (data->pseudo_register_reggroup_p != NULL);
846       return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
847     }
848
849   ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
850   if (ret != -1)
851     return ret;
852
853   return default_register_reggroup_p (gdbarch, regno, reggroup);
854 }
855
856 /* Record architecture-specific functions to call for pseudo-register
857    support.  */
858
859 void
860 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
861                                 gdbarch_register_name_ftype *pseudo_name)
862 {
863   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
864
865   data->pseudo_register_name = pseudo_name;
866 }
867
868 void
869 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
870                                 gdbarch_register_type_ftype *pseudo_type)
871 {
872   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
873
874   data->pseudo_register_type = pseudo_type;
875 }
876
877 void
878 set_tdesc_pseudo_register_reggroup_p
879   (struct gdbarch *gdbarch,
880    gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
881 {
882   struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
883
884   data->pseudo_register_reggroup_p = pseudo_reggroup_p;
885 }
886
887 /* Update GDBARCH to use the target description for registers.  */
888
889 void
890 tdesc_use_registers (struct gdbarch *gdbarch,
891                      const struct target_desc *target_desc,
892                      struct tdesc_arch_data *early_data)
893 {
894   int num_regs = gdbarch_num_regs (gdbarch);
895   int i, ixf, ixr;
896   struct tdesc_feature *feature;
897   struct tdesc_reg *reg;
898   struct tdesc_arch_data *data;
899   struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
900   htab_t reg_hash;
901
902   /* We can't use the description for registers if it doesn't describe
903      any.  This function should only be called after validating
904      registers, so the caller should know that registers are
905      included.  */
906   gdb_assert (tdesc_has_registers (target_desc));
907
908   data = gdbarch_data (gdbarch, tdesc_data);
909   data->arch_regs = early_data->arch_regs;
910   xfree (early_data);
911
912   /* Build up a set of all registers, so that we can assign register
913      numbers where needed.  The hash table expands as necessary, so
914      the initial size is arbitrary.  */
915   reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
916   for (ixf = 0;
917        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
918        ixf++)
919     for (ixr = 0;
920          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
921          ixr++)
922       {
923         void **slot = htab_find_slot (reg_hash, reg, INSERT);
924
925         *slot = reg;
926       }
927
928   /* Remove any registers which were assigned numbers by the
929      architecture.  */
930   for (ixr = 0;
931        VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
932        ixr++)
933     if (arch_reg->reg)
934       htab_remove_elt (reg_hash, arch_reg->reg);
935
936   /* Assign numbers to the remaining registers and add them to the
937      list of registers.  The new numbers are always above gdbarch_num_regs.
938      Iterate over the features, not the hash table, so that the order
939      matches that in the target description.  */
940
941   gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
942   while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
943     VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
944   for (ixf = 0;
945        VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
946        ixf++)
947     for (ixr = 0;
948          VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
949          ixr++)
950       if (htab_find (reg_hash, reg) != NULL)
951         {
952           new_arch_reg.reg = reg;
953           VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
954           num_regs++;
955         }
956
957   htab_delete (reg_hash);
958
959   /* Update the architecture.  */
960   set_gdbarch_num_regs (gdbarch, num_regs);
961   set_gdbarch_register_name (gdbarch, tdesc_register_name);
962   set_gdbarch_register_type (gdbarch, tdesc_register_type);
963   set_gdbarch_remote_register_number (gdbarch,
964                                       tdesc_remote_register_number);
965   set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
966 }
967 \f
968
969 /* Methods for constructing a target description.  */
970
971 static void
972 tdesc_free_reg (struct tdesc_reg *reg)
973 {
974   xfree (reg->name);
975   xfree (reg->type);
976   xfree (reg->group);
977   xfree (reg);
978 }
979
980 void
981 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
982                   int regnum, int save_restore, const char *group,
983                   int bitsize, const char *type)
984 {
985   struct tdesc_reg *reg = XZALLOC (struct tdesc_reg);
986
987   reg->name = xstrdup (name);
988   reg->target_regnum = regnum;
989   reg->save_restore = save_restore;
990   reg->group = group ? xstrdup (group) : NULL;
991   reg->bitsize = bitsize;
992   reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
993
994   /* If the register's type is target-defined, look it up now.  We may not
995      have easy access to the containing feature when we want it later.  */
996   reg->tdesc_type = tdesc_named_type (feature, reg->type);
997
998   VEC_safe_push (tdesc_reg_p, feature->registers, reg);
999 }
1000
1001 static void
1002 tdesc_free_type (struct tdesc_type *type)
1003 {
1004
1005   switch (type->kind)
1006     {
1007     case TDESC_TYPE_UNION:
1008       {
1009         struct tdesc_type_field *f;
1010         int ix;
1011
1012         for (ix = 0;
1013              VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
1014              ix++)
1015           xfree (f->name);
1016
1017         VEC_free (tdesc_type_field, type->u.u.fields);
1018       }
1019       break;
1020
1021     default:
1022       break;
1023     }
1024
1025   xfree (type->name);
1026   xfree (type);
1027 }
1028
1029 struct tdesc_type *
1030 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1031                      struct tdesc_type *field_type, int count)
1032 {
1033   struct tdesc_type *type = XZALLOC (struct tdesc_type);
1034
1035   type->name = xstrdup (name);
1036   type->kind = TDESC_TYPE_VECTOR;
1037   type->u.v.type = field_type;
1038   type->u.v.count = count;
1039
1040   VEC_safe_push (tdesc_type_p, feature->types, type);
1041   return type;
1042 }
1043
1044 struct tdesc_type *
1045 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1046 {
1047   struct tdesc_type *type = XZALLOC (struct tdesc_type);
1048
1049   type->name = xstrdup (name);
1050   type->kind = TDESC_TYPE_UNION;
1051
1052   VEC_safe_push (tdesc_type_p, feature->types, type);
1053   return type;
1054 }
1055
1056 void
1057 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1058                  struct tdesc_type *field_type)
1059 {
1060   struct tdesc_type_field f = { 0 };
1061
1062   gdb_assert (type->kind == TDESC_TYPE_UNION);
1063
1064   f.name = xstrdup (field_name);
1065   f.type = field_type;
1066
1067   VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1068 }
1069
1070 static void
1071 tdesc_free_feature (struct tdesc_feature *feature)
1072 {
1073   struct tdesc_reg *reg;
1074   struct tdesc_type *type;
1075   int ix;
1076
1077   for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
1078     tdesc_free_reg (reg);
1079   VEC_free (tdesc_reg_p, feature->registers);
1080
1081   for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
1082     tdesc_free_type (type);
1083   VEC_free (tdesc_type_p, feature->types);
1084
1085   xfree (feature->name);
1086   xfree (feature);
1087 }
1088
1089 struct tdesc_feature *
1090 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1091 {
1092   struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature);
1093
1094   new_feature->name = xstrdup (name);
1095
1096   VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1097   return new_feature;
1098 }
1099
1100 struct target_desc *
1101 allocate_target_description (void)
1102 {
1103   return XZALLOC (struct target_desc);
1104 }
1105
1106 static void
1107 free_target_description (void *arg)
1108 {
1109   struct target_desc *target_desc = arg;
1110   struct tdesc_feature *feature;
1111   struct property *prop;
1112   int ix;
1113
1114   for (ix = 0;
1115        VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
1116        ix++)
1117     tdesc_free_feature (feature);
1118   VEC_free (tdesc_feature_p, target_desc->features);
1119
1120   for (ix = 0;
1121        VEC_iterate (property_s, target_desc->properties, ix, prop);
1122        ix++)
1123     {
1124       xfree (prop->key);
1125       xfree (prop->value);
1126     }
1127   VEC_free (property_s, target_desc->properties);
1128
1129   xfree (target_desc);
1130 }
1131
1132 struct cleanup *
1133 make_cleanup_free_target_description (struct target_desc *target_desc)
1134 {
1135   return make_cleanup (free_target_description, target_desc);
1136 }
1137
1138 void
1139 set_tdesc_property (struct target_desc *target_desc,
1140                     const char *key, const char *value)
1141 {
1142   struct property *prop, new_prop;
1143   int ix;
1144
1145   gdb_assert (key != NULL && value != NULL);
1146
1147   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1148        ix++)
1149     if (strcmp (prop->key, key) == 0)
1150       internal_error (__FILE__, __LINE__,
1151                       _("Attempted to add duplicate property \"%s\""), key);
1152
1153   new_prop.key = xstrdup (key);
1154   new_prop.value = xstrdup (value);
1155   VEC_safe_push (property_s, target_desc->properties, &new_prop);
1156 }
1157
1158 void
1159 set_tdesc_architecture (struct target_desc *target_desc,
1160                         const struct bfd_arch_info *arch)
1161 {
1162   target_desc->arch = arch;
1163 }
1164 \f
1165
1166 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1167 static struct cmd_list_element *tdesc_unset_cmdlist;
1168
1169 /* Helper functions for the CLI commands.  */
1170
1171 static void
1172 set_tdesc_cmd (char *args, int from_tty)
1173 {
1174   help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout);
1175 }
1176
1177 static void
1178 show_tdesc_cmd (char *args, int from_tty)
1179 {
1180   cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1181 }
1182
1183 static void
1184 unset_tdesc_cmd (char *args, int from_tty)
1185 {
1186   help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout);
1187 }
1188
1189 static void
1190 set_tdesc_filename_cmd (char *args, int from_tty,
1191                         struct cmd_list_element *c)
1192 {
1193   target_clear_description ();
1194   target_find_description ();
1195 }
1196
1197 static void
1198 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1199                          struct cmd_list_element *c,
1200                          const char *value)
1201 {
1202   if (value != NULL && *value != '\0')
1203     printf_filtered (_("\
1204 The target description will be read from \"%s\".\n"),
1205                      value);
1206   else
1207     printf_filtered (_("\
1208 The target description will be read from the target.\n"));
1209 }
1210
1211 static void
1212 unset_tdesc_filename_cmd (char *args, int from_tty)
1213 {
1214   xfree (target_description_filename);
1215   target_description_filename = NULL;
1216   target_clear_description ();
1217   target_find_description ();
1218 }
1219
1220 static void
1221 maint_print_c_tdesc_cmd (char *args, int from_tty)
1222 {
1223   const struct target_desc *tdesc;
1224   const char *filename, *inp;
1225   char *function, *outp;
1226   struct property *prop;
1227   struct tdesc_feature *feature;
1228   struct tdesc_reg *reg;
1229   struct tdesc_type *type;
1230   struct tdesc_type_field *f;
1231   int ix, ix2, ix3;
1232
1233   /* Use the global target-supplied description, not the current
1234      architecture's.  This lets a GDB for one architecture generate C
1235      for another architecture's description, even though the gdbarch
1236      initialization code will reject the new description.  */
1237   tdesc = current_target_desc;
1238   if (tdesc == NULL)
1239     error (_("There is no target description to print."));
1240
1241   if (target_description_filename == NULL)
1242     error (_("The current target description did not come from an XML file."));
1243
1244   filename = lbasename (target_description_filename);
1245   function = alloca (strlen (filename) + 1);
1246   for (inp = filename, outp = function; *inp != '\0'; inp++)
1247     if (*inp == '.')
1248       break;
1249     else if (*inp == '-')
1250       *outp++ = '_';
1251     else
1252       *outp++ = *inp;
1253   *outp = '\0';
1254
1255   /* Standard boilerplate.  */
1256   printf_unfiltered ("/* THIS FILE IS GENERATED.  Original: %s */\n\n",
1257                      filename);
1258   printf_unfiltered ("#include \"defs.h\"\n");
1259   printf_unfiltered ("#include \"target-descriptions.h\"\n");
1260   printf_unfiltered ("\n");
1261
1262   printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1263   printf_unfiltered ("static void\n");
1264   printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1265   printf_unfiltered ("{\n");
1266   printf_unfiltered
1267     ("  struct target_desc *result = allocate_target_description ();\n");
1268   printf_unfiltered ("  struct tdesc_feature *feature;\n");
1269   printf_unfiltered ("  struct tdesc_type *field_type, *type;\n");
1270   printf_unfiltered ("\n");
1271
1272   if (tdesc_architecture (tdesc) != NULL)
1273     {
1274       printf_unfiltered
1275         ("  set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1276          tdesc_architecture (tdesc)->printable_name);
1277       printf_unfiltered ("\n");
1278     }
1279
1280   for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1281        ix++)
1282     {
1283       printf_unfiltered ("  set_tdesc_property (result, \"%s\", \"%s\");\n",
1284               prop->key, prop->value);
1285     }
1286
1287   for (ix = 0;
1288        VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1289        ix++)
1290     {
1291       printf_unfiltered ("  feature = tdesc_create_feature (result, \"%s\");\n",
1292                          feature->name);
1293
1294       for (ix2 = 0;
1295            VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1296            ix2++)
1297         {
1298           switch (type->kind)
1299             {
1300             case TDESC_TYPE_VECTOR:
1301               printf_unfiltered
1302                 ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1303                  type->u.v.type->name);
1304               printf_unfiltered
1305                 ("  tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1306                  type->name, type->u.v.count);
1307               break;
1308             case TDESC_TYPE_UNION:
1309               printf_unfiltered
1310                 ("  type = tdesc_create_union (feature, \"%s\");\n",
1311                  type->name);
1312               for (ix3 = 0;
1313                    VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1314                    ix3++)
1315                 {
1316                   printf_unfiltered
1317                     ("  field_type = tdesc_named_type (feature, \"%s\");\n",
1318                      f->type->name);
1319                   printf_unfiltered
1320                     ("  tdesc_add_field (type, \"%s\", field_type);\n",
1321                      f->name);
1322                 }
1323               break;
1324             default:
1325               error (_("C output is not supported type \"%s\"."), type->name);
1326             }
1327           printf_unfiltered ("\n");
1328         }
1329
1330       for (ix2 = 0;
1331            VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1332            ix2++)
1333         {
1334           printf_unfiltered ("  tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1335                              reg->name, reg->target_regnum, reg->save_restore);
1336           if (reg->group)
1337             printf_unfiltered ("\"%s\", ", reg->group);
1338           else
1339             printf_unfiltered ("NULL, ");
1340           printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1341         }
1342
1343       printf_unfiltered ("\n");
1344     }
1345
1346   printf_unfiltered ("  tdesc_%s = result;\n", function);
1347   printf_unfiltered ("}\n");
1348 }
1349
1350 /* Provide a prototype to silence -Wmissing-prototypes.  */
1351 extern initialize_file_ftype _initialize_target_descriptions;
1352
1353 void
1354 _initialize_target_descriptions (void)
1355 {
1356   tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1357
1358   add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1359 Set target description specific variables."),
1360                   &tdesc_set_cmdlist, "set tdesc ",
1361                   0 /* allow-unknown */, &setlist);
1362   add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1363 Show target description specific variables."),
1364                   &tdesc_show_cmdlist, "show tdesc ",
1365                   0 /* allow-unknown */, &showlist);
1366   add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1367 Unset target description specific variables."),
1368                   &tdesc_unset_cmdlist, "unset tdesc ",
1369                   0 /* allow-unknown */, &unsetlist);
1370
1371   add_setshow_filename_cmd ("filename", class_obscure,
1372                             &target_description_filename,
1373                             _("\
1374 Set the file to read for an XML target description"), _("\
1375 Show the file to read for an XML target description"), _("\
1376 When set, GDB will read the target description from a local\n\
1377 file instead of querying the remote target."),
1378                             set_tdesc_filename_cmd,
1379                             show_tdesc_filename_cmd,
1380                             &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1381
1382   add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1383 Unset the file to read for an XML target description.  When unset,\n\
1384 GDB will read the description from the target."),
1385            &tdesc_unset_cmdlist);
1386
1387   add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1388 Print the current target description as a C source file."),
1389            &maintenanceprintlist);
1390 }