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