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