Fix struct, union, and enum nesting in C++
[deliverable/binutils-gdb.git] / gdb / target-descriptions.c
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 (&current_target);
352
353 /* If that failed try a target-specific hook. */
354 if (current_target_desc == NULL)
355 current_target_desc = target_read_description (&current_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 \f
439
440 /* Direct accessors for target descriptions. */
441
442 /* Return the string value of a property named KEY, or NULL if the
443 property was not specified. */
444
445 const char *
446 tdesc_property (const struct target_desc *target_desc, const char *key)
447 {
448 struct property *prop;
449 int ix;
450
451 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
452 ix++)
453 if (strcmp (prop->key, key) == 0)
454 return prop->value;
455
456 return NULL;
457 }
458
459 /* Return the BFD architecture associated with this target
460 description, or NULL if no architecture was specified. */
461
462 const struct bfd_arch_info *
463 tdesc_architecture (const struct target_desc *target_desc)
464 {
465 return target_desc->arch;
466 }
467
468 /* Return the OSABI associated with this target description, or
469 GDB_OSABI_UNKNOWN if no osabi was specified. */
470
471 enum gdb_osabi
472 tdesc_osabi (const struct target_desc *target_desc)
473 {
474 return target_desc->osabi;
475 }
476
477 \f
478
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 \f
788
789 /* Support for registers from target descriptions. */
790
791 /* Construct the per-gdbarch data. */
792
793 static void *
794 tdesc_data_init (struct obstack *obstack)
795 {
796 struct tdesc_arch_data *data;
797
798 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
799 return data;
800 }
801
802 /* Similar, but for the temporary copy used during architecture
803 initialization. */
804
805 struct tdesc_arch_data *
806 tdesc_data_alloc (void)
807 {
808 return XCNEW (struct tdesc_arch_data);
809 }
810
811 /* Free something allocated by tdesc_data_alloc, if it is not going
812 to be used (for instance if it was unsuitable for the
813 architecture). */
814
815 void
816 tdesc_data_cleanup (void *data_untyped)
817 {
818 struct tdesc_arch_data *data = data_untyped;
819
820 VEC_free (tdesc_arch_reg, data->arch_regs);
821 xfree (data);
822 }
823
824 /* Search FEATURE for a register named NAME. */
825
826 static struct tdesc_reg *
827 tdesc_find_register_early (const struct tdesc_feature *feature,
828 const char *name)
829 {
830 int ixr;
831 struct tdesc_reg *reg;
832
833 for (ixr = 0;
834 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
835 ixr++)
836 if (strcasecmp (reg->name, name) == 0)
837 return reg;
838
839 return NULL;
840 }
841
842 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
843
844 int
845 tdesc_numbered_register (const struct tdesc_feature *feature,
846 struct tdesc_arch_data *data,
847 int regno, const char *name)
848 {
849 struct tdesc_arch_reg arch_reg = { 0 };
850 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
851
852 if (reg == NULL)
853 return 0;
854
855 /* Make sure the vector includes a REGNO'th element. */
856 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
857 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
858
859 arch_reg.reg = reg;
860 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
861 return 1;
862 }
863
864 /* Search FEATURE for a register named NAME, but do not assign a fixed
865 register number to it. */
866
867 int
868 tdesc_unnumbered_register (const struct tdesc_feature *feature,
869 const char *name)
870 {
871 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
872
873 if (reg == NULL)
874 return 0;
875
876 return 1;
877 }
878
879 /* Search FEATURE for a register whose name is in NAMES and assign
880 REGNO to it. */
881
882 int
883 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
884 struct tdesc_arch_data *data,
885 int regno, const char *const names[])
886 {
887 int i;
888
889 for (i = 0; names[i] != NULL; i++)
890 if (tdesc_numbered_register (feature, data, regno, names[i]))
891 return 1;
892
893 return 0;
894 }
895
896 /* Search FEATURE for a register named NAME, and return its size in
897 bits. The register must exist. */
898
899 int
900 tdesc_register_size (const struct tdesc_feature *feature,
901 const char *name)
902 {
903 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
904
905 gdb_assert (reg != NULL);
906 return reg->bitsize;
907 }
908
909 /* Look up a register by its GDB internal register number. */
910
911 static struct tdesc_arch_reg *
912 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
913 {
914 struct tdesc_arch_data *data;
915
916 data = gdbarch_data (gdbarch, tdesc_data);
917 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
918 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
919 else
920 return NULL;
921 }
922
923 static struct tdesc_reg *
924 tdesc_find_register (struct gdbarch *gdbarch, int regno)
925 {
926 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
927
928 return reg? reg->reg : NULL;
929 }
930
931 /* Return the name of register REGNO, from the target description or
932 from an architecture-provided pseudo_register_name method. */
933
934 const char *
935 tdesc_register_name (struct gdbarch *gdbarch, int regno)
936 {
937 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
938 int num_regs = gdbarch_num_regs (gdbarch);
939 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
940
941 if (reg != NULL)
942 return reg->name;
943
944 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
945 {
946 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
947
948 gdb_assert (data->pseudo_register_name != NULL);
949 return data->pseudo_register_name (gdbarch, regno);
950 }
951
952 return "";
953 }
954
955 struct type *
956 tdesc_register_type (struct gdbarch *gdbarch, int regno)
957 {
958 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
959 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
960 int num_regs = gdbarch_num_regs (gdbarch);
961 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
962
963 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
964 {
965 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
966
967 gdb_assert (data->pseudo_register_type != NULL);
968 return data->pseudo_register_type (gdbarch, regno);
969 }
970
971 if (reg == NULL)
972 /* Return "int0_t", since "void" has a misleading size of one. */
973 return builtin_type (gdbarch)->builtin_int0;
974
975 if (arch_reg->type == NULL)
976 {
977 /* First check for a predefined or target defined type. */
978 if (reg->tdesc_type)
979 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
980
981 /* Next try size-sensitive type shortcuts. */
982 else if (strcmp (reg->type, "float") == 0)
983 {
984 if (reg->bitsize == gdbarch_float_bit (gdbarch))
985 arch_reg->type = builtin_type (gdbarch)->builtin_float;
986 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
987 arch_reg->type = builtin_type (gdbarch)->builtin_double;
988 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
989 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
990 else
991 {
992 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
993 reg->name, reg->bitsize);
994 arch_reg->type = builtin_type (gdbarch)->builtin_double;
995 }
996 }
997 else if (strcmp (reg->type, "int") == 0)
998 {
999 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1000 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1001 else if (reg->bitsize == TARGET_CHAR_BIT)
1002 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1003 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1004 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1005 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1006 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1007 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1008 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1009 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
1010 /* A bit desperate by this point... */
1011 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1012 else
1013 {
1014 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1015 reg->name, reg->bitsize);
1016 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1017 }
1018 }
1019
1020 if (arch_reg->type == NULL)
1021 internal_error (__FILE__, __LINE__,
1022 "Register \"%s\" has an unknown type \"%s\"",
1023 reg->name, reg->type);
1024 }
1025
1026 return arch_reg->type;
1027 }
1028
1029 static int
1030 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1031 {
1032 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1033
1034 if (reg != NULL)
1035 return reg->target_regnum;
1036 else
1037 return -1;
1038 }
1039
1040 /* Check whether REGNUM is a member of REGGROUP. Registers from the
1041 target description may be classified as general, float, or vector.
1042 Unlike a gdbarch register_reggroup_p method, this function will
1043 return -1 if it does not know; the caller should handle registers
1044 with no specified group.
1045
1046 Arbitrary strings (other than "general", "float", and "vector")
1047 from the description are not used; they cause the register to be
1048 displayed in "info all-registers" but excluded from "info
1049 registers" et al. The names of containing features are also not
1050 used. This might be extended to display registers in some more
1051 useful groupings.
1052
1053 The save-restore flag is also implemented here. */
1054
1055 int
1056 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1057 struct reggroup *reggroup)
1058 {
1059 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1060
1061 if (reg != NULL && reg->group != NULL)
1062 {
1063 int general_p = 0, float_p = 0, vector_p = 0;
1064
1065 if (strcmp (reg->group, "general") == 0)
1066 general_p = 1;
1067 else if (strcmp (reg->group, "float") == 0)
1068 float_p = 1;
1069 else if (strcmp (reg->group, "vector") == 0)
1070 vector_p = 1;
1071
1072 if (reggroup == float_reggroup)
1073 return float_p;
1074
1075 if (reggroup == vector_reggroup)
1076 return vector_p;
1077
1078 if (reggroup == general_reggroup)
1079 return general_p;
1080 }
1081
1082 if (reg != NULL
1083 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1084 return reg->save_restore;
1085
1086 return -1;
1087 }
1088
1089 /* Check whether REGNUM is a member of REGGROUP. Registers with no
1090 group specified go to the default reggroup function and are handled
1091 by type. */
1092
1093 static int
1094 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1095 struct reggroup *reggroup)
1096 {
1097 int num_regs = gdbarch_num_regs (gdbarch);
1098 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1099 int ret;
1100
1101 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1102 {
1103 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1104
1105 if (data->pseudo_register_reggroup_p != NULL)
1106 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1107 /* Otherwise fall through to the default reggroup_p. */
1108 }
1109
1110 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1111 if (ret != -1)
1112 return ret;
1113
1114 return default_register_reggroup_p (gdbarch, regno, reggroup);
1115 }
1116
1117 /* Record architecture-specific functions to call for pseudo-register
1118 support. */
1119
1120 void
1121 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1122 gdbarch_register_name_ftype *pseudo_name)
1123 {
1124 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1125
1126 data->pseudo_register_name = pseudo_name;
1127 }
1128
1129 void
1130 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1131 gdbarch_register_type_ftype *pseudo_type)
1132 {
1133 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1134
1135 data->pseudo_register_type = pseudo_type;
1136 }
1137
1138 void
1139 set_tdesc_pseudo_register_reggroup_p
1140 (struct gdbarch *gdbarch,
1141 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1142 {
1143 struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data);
1144
1145 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1146 }
1147
1148 /* Update GDBARCH to use the target description for registers. */
1149
1150 void
1151 tdesc_use_registers (struct gdbarch *gdbarch,
1152 const struct target_desc *target_desc,
1153 struct tdesc_arch_data *early_data)
1154 {
1155 int num_regs = gdbarch_num_regs (gdbarch);
1156 int ixf, ixr;
1157 struct tdesc_feature *feature;
1158 struct tdesc_reg *reg;
1159 struct tdesc_arch_data *data;
1160 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1161 htab_t reg_hash;
1162
1163 /* We can't use the description for registers if it doesn't describe
1164 any. This function should only be called after validating
1165 registers, so the caller should know that registers are
1166 included. */
1167 gdb_assert (tdesc_has_registers (target_desc));
1168
1169 data = gdbarch_data (gdbarch, tdesc_data);
1170 data->arch_regs = early_data->arch_regs;
1171 xfree (early_data);
1172
1173 /* Build up a set of all registers, so that we can assign register
1174 numbers where needed. The hash table expands as necessary, so
1175 the initial size is arbitrary. */
1176 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1177 for (ixf = 0;
1178 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1179 ixf++)
1180 for (ixr = 0;
1181 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1182 ixr++)
1183 {
1184 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1185
1186 *slot = reg;
1187 }
1188
1189 /* Remove any registers which were assigned numbers by the
1190 architecture. */
1191 for (ixr = 0;
1192 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1193 ixr++)
1194 if (arch_reg->reg)
1195 htab_remove_elt (reg_hash, arch_reg->reg);
1196
1197 /* Assign numbers to the remaining registers and add them to the
1198 list of registers. The new numbers are always above gdbarch_num_regs.
1199 Iterate over the features, not the hash table, so that the order
1200 matches that in the target description. */
1201
1202 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1203 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1204 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1205 for (ixf = 0;
1206 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1207 ixf++)
1208 for (ixr = 0;
1209 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1210 ixr++)
1211 if (htab_find (reg_hash, reg) != NULL)
1212 {
1213 new_arch_reg.reg = reg;
1214 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1215 num_regs++;
1216 }
1217
1218 htab_delete (reg_hash);
1219
1220 /* Update the architecture. */
1221 set_gdbarch_num_regs (gdbarch, num_regs);
1222 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1223 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1224 set_gdbarch_remote_register_number (gdbarch,
1225 tdesc_remote_register_number);
1226 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1227 }
1228 \f
1229
1230 /* Methods for constructing a target description. */
1231
1232 static void
1233 tdesc_free_reg (struct tdesc_reg *reg)
1234 {
1235 xfree (reg->name);
1236 xfree (reg->type);
1237 xfree (reg->group);
1238 xfree (reg);
1239 }
1240
1241 void
1242 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1243 int regnum, int save_restore, const char *group,
1244 int bitsize, const char *type)
1245 {
1246 struct tdesc_reg *reg = XCNEW (struct tdesc_reg);
1247
1248 reg->name = xstrdup (name);
1249 reg->target_regnum = regnum;
1250 reg->save_restore = save_restore;
1251 reg->group = group ? xstrdup (group) : NULL;
1252 reg->bitsize = bitsize;
1253 reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
1254
1255 /* If the register's type is target-defined, look it up now. We may not
1256 have easy access to the containing feature when we want it later. */
1257 reg->tdesc_type = tdesc_named_type (feature, reg->type);
1258
1259 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1260 }
1261
1262 static void
1263 tdesc_free_type (struct tdesc_type *type)
1264 {
1265 switch (type->kind)
1266 {
1267 case TDESC_TYPE_STRUCT:
1268 case TDESC_TYPE_UNION:
1269 {
1270 struct tdesc_type_field *f;
1271 int ix;
1272
1273 for (ix = 0;
1274 VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
1275 ix++)
1276 xfree (f->name);
1277
1278 VEC_free (tdesc_type_field, type->u.u.fields);
1279 }
1280 break;
1281
1282 case TDESC_TYPE_FLAGS:
1283 {
1284 struct tdesc_type_flag *f;
1285 int ix;
1286
1287 for (ix = 0;
1288 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
1289 ix++)
1290 xfree (f->name);
1291
1292 VEC_free (tdesc_type_flag, type->u.f.flags);
1293 }
1294 break;
1295
1296 default:
1297 break;
1298 }
1299
1300 xfree (type->name);
1301 xfree (type);
1302 }
1303
1304 struct tdesc_type *
1305 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1306 struct tdesc_type *field_type, int count)
1307 {
1308 struct tdesc_type *type = XCNEW (struct tdesc_type);
1309
1310 type->name = xstrdup (name);
1311 type->kind = TDESC_TYPE_VECTOR;
1312 type->u.v.type = field_type;
1313 type->u.v.count = count;
1314
1315 VEC_safe_push (tdesc_type_p, feature->types, type);
1316 return type;
1317 }
1318
1319 struct tdesc_type *
1320 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1321 {
1322 struct tdesc_type *type = XCNEW (struct tdesc_type);
1323
1324 type->name = xstrdup (name);
1325 type->kind = TDESC_TYPE_STRUCT;
1326
1327 VEC_safe_push (tdesc_type_p, feature->types, type);
1328 return type;
1329 }
1330
1331 /* Set the total length of TYPE. Structs which contain bitfields may
1332 omit the reserved bits, so the end of the last field may not
1333 suffice. */
1334
1335 void
1336 tdesc_set_struct_size (struct tdesc_type *type, LONGEST size)
1337 {
1338 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1339 type->u.u.size = size;
1340 }
1341
1342 struct tdesc_type *
1343 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1344 {
1345 struct tdesc_type *type = XCNEW (struct tdesc_type);
1346
1347 type->name = xstrdup (name);
1348 type->kind = TDESC_TYPE_UNION;
1349
1350 VEC_safe_push (tdesc_type_p, feature->types, type);
1351 return type;
1352 }
1353
1354 struct tdesc_type *
1355 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1356 LONGEST size)
1357 {
1358 struct tdesc_type *type = XCNEW (struct tdesc_type);
1359
1360 type->name = xstrdup (name);
1361 type->kind = TDESC_TYPE_FLAGS;
1362 type->u.f.size = size;
1363
1364 VEC_safe_push (tdesc_type_p, feature->types, type);
1365 return type;
1366 }
1367
1368 /* Add a new field. Return a temporary pointer to the field, which
1369 is only valid until the next call to tdesc_add_field (the vector
1370 might be reallocated). */
1371
1372 void
1373 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1374 struct tdesc_type *field_type)
1375 {
1376 struct tdesc_type_field f = { 0 };
1377
1378 gdb_assert (type->kind == TDESC_TYPE_UNION
1379 || type->kind == TDESC_TYPE_STRUCT);
1380
1381 f.name = xstrdup (field_name);
1382 f.type = field_type;
1383
1384 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1385 }
1386
1387 /* Add a new bitfield. */
1388
1389 void
1390 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1391 int start, int end)
1392 {
1393 struct tdesc_type_field f = { 0 };
1394
1395 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1396
1397 f.name = xstrdup (field_name);
1398 f.start = start;
1399 f.end = end;
1400
1401 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1402 }
1403
1404 void
1405 tdesc_add_flag (struct tdesc_type *type, int start,
1406 const char *flag_name)
1407 {
1408 struct tdesc_type_flag f = { 0 };
1409
1410 gdb_assert (type->kind == TDESC_TYPE_FLAGS);
1411
1412 f.name = xstrdup (flag_name);
1413 f.start = start;
1414
1415 VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
1416 }
1417
1418 static void
1419 tdesc_free_feature (struct tdesc_feature *feature)
1420 {
1421 struct tdesc_reg *reg;
1422 struct tdesc_type *type;
1423 int ix;
1424
1425 for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
1426 tdesc_free_reg (reg);
1427 VEC_free (tdesc_reg_p, feature->registers);
1428
1429 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
1430 tdesc_free_type (type);
1431 VEC_free (tdesc_type_p, feature->types);
1432
1433 xfree (feature->name);
1434 xfree (feature);
1435 }
1436
1437 struct tdesc_feature *
1438 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1439 {
1440 struct tdesc_feature *new_feature = XCNEW (struct tdesc_feature);
1441
1442 new_feature->name = xstrdup (name);
1443
1444 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1445 return new_feature;
1446 }
1447
1448 struct target_desc *
1449 allocate_target_description (void)
1450 {
1451 return XCNEW (struct target_desc);
1452 }
1453
1454 static void
1455 free_target_description (void *arg)
1456 {
1457 struct target_desc *target_desc = arg;
1458 struct tdesc_feature *feature;
1459 struct property *prop;
1460 int ix;
1461
1462 for (ix = 0;
1463 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
1464 ix++)
1465 tdesc_free_feature (feature);
1466 VEC_free (tdesc_feature_p, target_desc->features);
1467
1468 for (ix = 0;
1469 VEC_iterate (property_s, target_desc->properties, ix, prop);
1470 ix++)
1471 {
1472 xfree (prop->key);
1473 xfree (prop->value);
1474 }
1475 VEC_free (property_s, target_desc->properties);
1476
1477 VEC_free (arch_p, target_desc->compatible);
1478
1479 xfree (target_desc);
1480 }
1481
1482 struct cleanup *
1483 make_cleanup_free_target_description (struct target_desc *target_desc)
1484 {
1485 return make_cleanup (free_target_description, target_desc);
1486 }
1487
1488 void
1489 tdesc_add_compatible (struct target_desc *target_desc,
1490 const struct bfd_arch_info *compatible)
1491 {
1492 const struct bfd_arch_info *compat;
1493 int ix;
1494
1495 /* If this instance of GDB is compiled without BFD support for the
1496 compatible architecture, simply ignore it -- we would not be able
1497 to handle it anyway. */
1498 if (compatible == NULL)
1499 return;
1500
1501 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1502 ix++)
1503 if (compat == compatible)
1504 internal_error (__FILE__, __LINE__,
1505 _("Attempted to add duplicate "
1506 "compatible architecture \"%s\""),
1507 compatible->printable_name);
1508
1509 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1510 }
1511
1512 void
1513 set_tdesc_property (struct target_desc *target_desc,
1514 const char *key, const char *value)
1515 {
1516 struct property *prop, new_prop;
1517 int ix;
1518
1519 gdb_assert (key != NULL && value != NULL);
1520
1521 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1522 ix++)
1523 if (strcmp (prop->key, key) == 0)
1524 internal_error (__FILE__, __LINE__,
1525 _("Attempted to add duplicate property \"%s\""), key);
1526
1527 new_prop.key = xstrdup (key);
1528 new_prop.value = xstrdup (value);
1529 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1530 }
1531
1532 void
1533 set_tdesc_architecture (struct target_desc *target_desc,
1534 const struct bfd_arch_info *arch)
1535 {
1536 target_desc->arch = arch;
1537 }
1538
1539 void
1540 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1541 {
1542 target_desc->osabi = osabi;
1543 }
1544 \f
1545
1546 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1547 static struct cmd_list_element *tdesc_unset_cmdlist;
1548
1549 /* Helper functions for the CLI commands. */
1550
1551 static void
1552 set_tdesc_cmd (char *args, int from_tty)
1553 {
1554 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1555 }
1556
1557 static void
1558 show_tdesc_cmd (char *args, int from_tty)
1559 {
1560 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1561 }
1562
1563 static void
1564 unset_tdesc_cmd (char *args, int from_tty)
1565 {
1566 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1567 }
1568
1569 static void
1570 set_tdesc_filename_cmd (char *args, int from_tty,
1571 struct cmd_list_element *c)
1572 {
1573 xfree (target_description_filename);
1574 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1575
1576 target_clear_description ();
1577 target_find_description ();
1578 }
1579
1580 static void
1581 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1582 struct cmd_list_element *c,
1583 const char *value)
1584 {
1585 value = target_description_filename;
1586
1587 if (value != NULL && *value != '\0')
1588 printf_filtered (_("The target description will be read from \"%s\".\n"),
1589 value);
1590 else
1591 printf_filtered (_("The target description will be "
1592 "read from the target.\n"));
1593 }
1594
1595 static void
1596 unset_tdesc_filename_cmd (char *args, int from_tty)
1597 {
1598 xfree (target_description_filename);
1599 target_description_filename = NULL;
1600 target_clear_description ();
1601 target_find_description ();
1602 }
1603
1604 static void
1605 maint_print_c_tdesc_cmd (char *args, int from_tty)
1606 {
1607 const struct target_desc *tdesc;
1608 const struct bfd_arch_info *compatible;
1609 const char *filename, *inp;
1610 char *function, *outp;
1611 struct property *prop;
1612 struct tdesc_feature *feature;
1613 struct tdesc_reg *reg;
1614 struct tdesc_type *type;
1615 struct tdesc_type_field *f;
1616 struct tdesc_type_flag *flag;
1617 int ix, ix2, ix3;
1618 int printed_field_type = 0;
1619
1620 /* Use the global target-supplied description, not the current
1621 architecture's. This lets a GDB for one architecture generate C
1622 for another architecture's description, even though the gdbarch
1623 initialization code will reject the new description. */
1624 tdesc = current_target_desc;
1625 if (tdesc == NULL)
1626 error (_("There is no target description to print."));
1627
1628 if (target_description_filename == NULL)
1629 error (_("The current target description did not come from an XML file."));
1630
1631 filename = lbasename (target_description_filename);
1632 function = alloca (strlen (filename) + 1);
1633 for (inp = filename, outp = function; *inp != '\0'; inp++)
1634 if (*inp == '.')
1635 break;
1636 else if (*inp == '-')
1637 *outp++ = '_';
1638 else
1639 *outp++ = *inp;
1640 *outp = '\0';
1641
1642 /* Standard boilerplate. */
1643 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1644 "-*- buffer-read-only: t -*- vi"
1645 ":set ro:\n");
1646 printf_unfiltered (" Original: %s */\n\n", filename);
1647 printf_unfiltered ("#include \"defs.h\"\n");
1648 printf_unfiltered ("#include \"osabi.h\"\n");
1649 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1650 printf_unfiltered ("\n");
1651
1652 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1653 printf_unfiltered ("static void\n");
1654 printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1655 printf_unfiltered ("{\n");
1656 printf_unfiltered
1657 (" struct target_desc *result = allocate_target_description ();\n");
1658 printf_unfiltered (" struct tdesc_feature *feature;\n");
1659
1660 /* Now we do some "filtering" in order to know which variables to
1661 declare. This is needed because otherwise we would declare unused
1662 variables `field_type' and `type'. */
1663 for (ix = 0;
1664 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1665 ix++)
1666 {
1667 int printed_desc_type = 0;
1668
1669 for (ix2 = 0;
1670 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1671 ix2++)
1672 {
1673 if (!printed_field_type)
1674 {
1675 printf_unfiltered (" struct tdesc_type *field_type;\n");
1676 printed_field_type = 1;
1677 }
1678
1679 if ((type->kind == TDESC_TYPE_UNION
1680 || type->kind == TDESC_TYPE_STRUCT)
1681 && VEC_length (tdesc_type_field, type->u.u.fields) > 0)
1682 {
1683 printf_unfiltered (" struct tdesc_type *type;\n");
1684 printed_desc_type = 1;
1685 break;
1686 }
1687 }
1688
1689 if (printed_desc_type)
1690 break;
1691 }
1692
1693 printf_unfiltered ("\n");
1694
1695 if (tdesc_architecture (tdesc) != NULL)
1696 {
1697 printf_unfiltered
1698 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1699 tdesc_architecture (tdesc)->printable_name);
1700 printf_unfiltered ("\n");
1701 }
1702
1703 if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
1704 && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
1705 {
1706 printf_unfiltered
1707 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1708 gdbarch_osabi_name (tdesc_osabi (tdesc)));
1709 printf_unfiltered ("\n");
1710 }
1711
1712 for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
1713 ix++)
1714 {
1715 printf_unfiltered
1716 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1717 compatible->printable_name);
1718 }
1719 if (ix)
1720 printf_unfiltered ("\n");
1721
1722 for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1723 ix++)
1724 {
1725 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1726 prop->key, prop->value);
1727 }
1728
1729 for (ix = 0;
1730 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1731 ix++)
1732 {
1733 printf_unfiltered (" \
1734 feature = tdesc_create_feature (result, \"%s\");\n",
1735 feature->name);
1736
1737 for (ix2 = 0;
1738 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1739 ix2++)
1740 {
1741 switch (type->kind)
1742 {
1743 case TDESC_TYPE_VECTOR:
1744 printf_unfiltered
1745 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1746 type->u.v.type->name);
1747 printf_unfiltered
1748 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1749 type->name, type->u.v.count);
1750 break;
1751 case TDESC_TYPE_STRUCT:
1752 printf_unfiltered
1753 (" type = tdesc_create_struct (feature, \"%s\");\n",
1754 type->name);
1755 if (type->u.u.size != 0)
1756 printf_unfiltered
1757 (" tdesc_set_struct_size (type, %s);\n",
1758 plongest (type->u.u.size));
1759 for (ix3 = 0;
1760 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1761 ix3++)
1762 {
1763 /* Going first for implicitly sized types, else part handles
1764 bitfields. As reported on xml-tdesc.c implicitly sized types
1765 cannot contain a bitfield. */
1766 if (f->type != NULL)
1767 {
1768 printf_unfiltered
1769 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1770 f->type->name);
1771 printf_unfiltered
1772 (" tdesc_add_field (type, \"%s\", field_type);\n",
1773 f->name);
1774 }
1775 else
1776 printf_unfiltered
1777 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1778 f->name, f->start, f->end);
1779 }
1780 break;
1781 case TDESC_TYPE_UNION:
1782 printf_unfiltered
1783 (" type = tdesc_create_union (feature, \"%s\");\n",
1784 type->name);
1785 for (ix3 = 0;
1786 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1787 ix3++)
1788 {
1789 printf_unfiltered
1790 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1791 f->type->name);
1792 printf_unfiltered
1793 (" tdesc_add_field (type, \"%s\", field_type);\n",
1794 f->name);
1795 }
1796 break;
1797 case TDESC_TYPE_FLAGS:
1798 printf_unfiltered
1799 (" field_type = tdesc_create_flags (feature, \"%s\", %d);\n",
1800 type->name, (int) type->u.f.size);
1801 for (ix3 = 0;
1802 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
1803 flag);
1804 ix3++)
1805 printf_unfiltered
1806 (" tdesc_add_flag (field_type, %d, \"%s\");\n",
1807 flag->start, flag->name);
1808 break;
1809 default:
1810 error (_("C output is not supported type \"%s\"."), type->name);
1811 }
1812 printf_unfiltered ("\n");
1813 }
1814
1815 for (ix2 = 0;
1816 VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1817 ix2++)
1818 {
1819 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1820 reg->name, reg->target_regnum, reg->save_restore);
1821 if (reg->group)
1822 printf_unfiltered ("\"%s\", ", reg->group);
1823 else
1824 printf_unfiltered ("NULL, ");
1825 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1826 }
1827
1828 printf_unfiltered ("\n");
1829 }
1830
1831 printf_unfiltered (" tdesc_%s = result;\n", function);
1832 printf_unfiltered ("}\n");
1833 }
1834
1835 /* Provide a prototype to silence -Wmissing-prototypes. */
1836 extern initialize_file_ftype _initialize_target_descriptions;
1837
1838 void
1839 _initialize_target_descriptions (void)
1840 {
1841 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1842
1843 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1844 Set target description specific variables."),
1845 &tdesc_set_cmdlist, "set tdesc ",
1846 0 /* allow-unknown */, &setlist);
1847 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1848 Show target description specific variables."),
1849 &tdesc_show_cmdlist, "show tdesc ",
1850 0 /* allow-unknown */, &showlist);
1851 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1852 Unset target description specific variables."),
1853 &tdesc_unset_cmdlist, "unset tdesc ",
1854 0 /* allow-unknown */, &unsetlist);
1855
1856 add_setshow_filename_cmd ("filename", class_obscure,
1857 &tdesc_filename_cmd_string,
1858 _("\
1859 Set the file to read for an XML target description"), _("\
1860 Show the file to read for an XML target description"), _("\
1861 When set, GDB will read the target description from a local\n\
1862 file instead of querying the remote target."),
1863 set_tdesc_filename_cmd,
1864 show_tdesc_filename_cmd,
1865 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1866
1867 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1868 Unset the file to read for an XML target description. When unset,\n\
1869 GDB will read the description from the target."),
1870 &tdesc_unset_cmdlist);
1871
1872 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1873 Print the current target description as a C source file."),
1874 &maintenanceprintlist);
1875 }
This page took 0.06756 seconds and 5 git commands to generate.