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