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