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