Use int instead of LONGEST in tdesc_type sizes.
[deliverable/binutils-gdb.git] / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3 Copyright (C) 2006-2016 Free Software Foundation, Inc.
4
5 Contributed by CodeSourcery.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "gdbcmd.h"
25 #include "gdbtypes.h"
26 #include "reggroups.h"
27 #include "target.h"
28 #include "target-descriptions.h"
29 #include "vec.h"
30 #include "xml-support.h"
31 #include "xml-tdesc.h"
32 #include "osabi.h"
33
34 #include "gdb_obstack.h"
35 #include "hashtab.h"
36 #include "inferior.h"
37
38 /* Types. */
39
40 typedef struct property
41 {
42 char *key;
43 char *value;
44 } property_s;
45 DEF_VEC_O(property_s);
46
47 /* An individual register from a target description. */
48
49 typedef struct tdesc_reg
50 {
51 /* The name of this register. In standard features, it may be
52 recognized by the architecture support code, or it may be purely
53 for the user. */
54 char *name;
55
56 /* The register number used by this target to refer to this
57 register. This is used for remote p/P packets and to determine
58 the ordering of registers in the remote g/G packets. */
59 long target_regnum;
60
61 /* If this flag is set, GDB should save and restore this register
62 around calls to an inferior function. */
63 int save_restore;
64
65 /* The name of the register group containing this register, or NULL
66 if the group should be automatically determined from the
67 register's type. If this is "general", "float", or "vector", the
68 corresponding "info" command should display this register's
69 value. It can be an arbitrary string, but should be limited to
70 alphanumeric characters and internal hyphens. Currently other
71 strings are ignored (treated as NULL). */
72 char *group;
73
74 /* The size of the register, in bits. */
75 int bitsize;
76
77 /* The type of the register. This string corresponds to either
78 a named type from the target description or a predefined
79 type from GDB. */
80 char *type;
81
82 /* The target-described type corresponding to TYPE, if found. */
83 struct tdesc_type *tdesc_type;
84 } *tdesc_reg_p;
85 DEF_VEC_P(tdesc_reg_p);
86
87 /* A named type from a target description. */
88
89 typedef struct tdesc_type_field
90 {
91 char *name;
92 struct tdesc_type *type;
93 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 int size;
154 } u;
155
156 /* Flags type. */
157 struct
158 {
159 VEC(tdesc_type_flag) *flags;
160 int 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 = ((struct tdesc_arch_data *)
372 gdbarch_data (target_gdbarch (), tdesc_data));
373 if (tdesc_has_registers (current_target_desc)
374 && data->arch_regs == NULL)
375 warning (_("Target-supplied registers are not supported "
376 "by the current architecture"));
377 }
378 }
379
380 /* Now that we know this description is usable, record that we
381 fetched it. */
382 target_desc_fetched = 1;
383 }
384
385 /* Discard any description fetched from the current target, and switch
386 the current architecture to one with no target description. */
387
388 void
389 target_clear_description (void)
390 {
391 struct gdbarch_info info;
392
393 if (!target_desc_fetched)
394 return;
395
396 target_desc_fetched = 0;
397 current_target_desc = NULL;
398
399 gdbarch_info_init (&info);
400 if (!gdbarch_update_p (info))
401 internal_error (__FILE__, __LINE__,
402 _("Could not remove target-supplied description"));
403 }
404
405 /* Return the global current target description. This should only be
406 used by gdbarch initialization code; most access should be through
407 an existing gdbarch. */
408
409 const struct target_desc *
410 target_current_description (void)
411 {
412 if (target_desc_fetched)
413 return current_target_desc;
414
415 return NULL;
416 }
417
418 /* Return non-zero if this target description is compatible
419 with the given BFD architecture. */
420
421 int
422 tdesc_compatible_p (const struct target_desc *target_desc,
423 const struct bfd_arch_info *arch)
424 {
425 const struct bfd_arch_info *compat;
426 int ix;
427
428 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
429 ix++)
430 {
431 if (compat == arch
432 || arch->compatible (arch, compat)
433 || compat->compatible (compat, arch))
434 return 1;
435 }
436
437 return 0;
438 }
439 \f
440
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 \f
479
480 /* Return 1 if this target description includes any registers. */
481
482 int
483 tdesc_has_registers (const struct target_desc *target_desc)
484 {
485 int ix;
486 struct tdesc_feature *feature;
487
488 if (target_desc == NULL)
489 return 0;
490
491 for (ix = 0;
492 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
493 ix++)
494 if (! VEC_empty (tdesc_reg_p, feature->registers))
495 return 1;
496
497 return 0;
498 }
499
500 /* Return the feature with the given name, if present, or NULL if
501 the named feature is not found. */
502
503 const struct tdesc_feature *
504 tdesc_find_feature (const struct target_desc *target_desc,
505 const char *name)
506 {
507 int ix;
508 struct tdesc_feature *feature;
509
510 for (ix = 0;
511 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
512 ix++)
513 if (strcmp (feature->name, name) == 0)
514 return feature;
515
516 return NULL;
517 }
518
519 /* Return the name of FEATURE. */
520
521 const char *
522 tdesc_feature_name (const struct tdesc_feature *feature)
523 {
524 return feature->name;
525 }
526
527 /* Predefined types. */
528 static struct tdesc_type tdesc_predefined_types[] =
529 {
530 { "int8", TDESC_TYPE_INT8 },
531 { "int16", TDESC_TYPE_INT16 },
532 { "int32", TDESC_TYPE_INT32 },
533 { "int64", TDESC_TYPE_INT64 },
534 { "int128", TDESC_TYPE_INT128 },
535 { "uint8", TDESC_TYPE_UINT8 },
536 { "uint16", TDESC_TYPE_UINT16 },
537 { "uint32", TDESC_TYPE_UINT32 },
538 { "uint64", TDESC_TYPE_UINT64 },
539 { "uint128", TDESC_TYPE_UINT128 },
540 { "code_ptr", TDESC_TYPE_CODE_PTR },
541 { "data_ptr", TDESC_TYPE_DATA_PTR },
542 { "ieee_single", TDESC_TYPE_IEEE_SINGLE },
543 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE },
544 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
545 { "i387_ext", TDESC_TYPE_I387_EXT }
546 };
547
548 /* Return the type associated with ID in the context of FEATURE, or
549 NULL if none. */
550
551 struct tdesc_type *
552 tdesc_named_type (const struct tdesc_feature *feature, const char *id)
553 {
554 int ix;
555 struct tdesc_type *type;
556
557 /* First try target-defined types. */
558 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
559 if (strcmp (type->name, id) == 0)
560 return type;
561
562 /* Next try the predefined types. */
563 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
564 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
565 return &tdesc_predefined_types[ix];
566
567 return NULL;
568 }
569
570 /* Lookup type associated with ID. */
571
572 struct type *
573 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
574 {
575 struct tdesc_arch_reg *reg;
576 struct tdesc_arch_data *data;
577 int i, num_regs;
578
579 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
580 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
581 for (i = 0; i < num_regs; i++)
582 {
583 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
584 if (reg->reg
585 && reg->reg->tdesc_type
586 && reg->type
587 && strcmp (id, reg->reg->tdesc_type->name) == 0)
588 return reg->type;
589 }
590
591 return NULL;
592 }
593
594 /* Construct, if necessary, and return the GDB type implementing target
595 type TDESC_TYPE for architecture GDBARCH. */
596
597 static struct type *
598 tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
599 {
600 struct type *type;
601
602 switch (tdesc_type->kind)
603 {
604 /* Predefined types. */
605 case TDESC_TYPE_INT8:
606 return builtin_type (gdbarch)->builtin_int8;
607
608 case TDESC_TYPE_INT16:
609 return builtin_type (gdbarch)->builtin_int16;
610
611 case TDESC_TYPE_INT32:
612 return builtin_type (gdbarch)->builtin_int32;
613
614 case TDESC_TYPE_INT64:
615 return builtin_type (gdbarch)->builtin_int64;
616
617 case TDESC_TYPE_INT128:
618 return builtin_type (gdbarch)->builtin_int128;
619
620 case TDESC_TYPE_UINT8:
621 return builtin_type (gdbarch)->builtin_uint8;
622
623 case TDESC_TYPE_UINT16:
624 return builtin_type (gdbarch)->builtin_uint16;
625
626 case TDESC_TYPE_UINT32:
627 return builtin_type (gdbarch)->builtin_uint32;
628
629 case TDESC_TYPE_UINT64:
630 return builtin_type (gdbarch)->builtin_uint64;
631
632 case TDESC_TYPE_UINT128:
633 return builtin_type (gdbarch)->builtin_uint128;
634
635 case TDESC_TYPE_CODE_PTR:
636 return builtin_type (gdbarch)->builtin_func_ptr;
637
638 case TDESC_TYPE_DATA_PTR:
639 return builtin_type (gdbarch)->builtin_data_ptr;
640
641 default:
642 break;
643 }
644
645 type = tdesc_find_type (gdbarch, tdesc_type->name);
646 if (type)
647 return type;
648
649 switch (tdesc_type->kind)
650 {
651 case TDESC_TYPE_IEEE_SINGLE:
652 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
653 floatformats_ieee_single);
654
655 case TDESC_TYPE_IEEE_DOUBLE:
656 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
657 floatformats_ieee_double);
658
659 case TDESC_TYPE_ARM_FPA_EXT:
660 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
661 floatformats_arm_ext);
662
663 case TDESC_TYPE_I387_EXT:
664 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
665 floatformats_i387_ext);
666
667 /* Types defined by a target feature. */
668 case TDESC_TYPE_VECTOR:
669 {
670 struct type *type, *field_type;
671
672 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
673 type = init_vector_type (field_type, tdesc_type->u.v.count);
674 TYPE_NAME (type) = xstrdup (tdesc_type->name);
675
676 return type;
677 }
678
679 case TDESC_TYPE_STRUCT:
680 {
681 struct type *type, *field_type;
682 struct tdesc_type_field *f;
683 int ix;
684
685 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
686 TYPE_NAME (type) = xstrdup (tdesc_type->name);
687 TYPE_TAG_NAME (type) = TYPE_NAME (type);
688
689 for (ix = 0;
690 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
691 ix++)
692 {
693 if (f->type == NULL)
694 {
695 /* Bitfield. */
696 struct field *fld;
697 struct type *field_type;
698 int bitsize, total_size;
699
700 /* This invariant should be preserved while creating
701 types. */
702 gdb_assert (tdesc_type->u.u.size != 0);
703 if (tdesc_type->u.u.size > 4)
704 field_type = builtin_type (gdbarch)->builtin_uint64;
705 else
706 field_type = builtin_type (gdbarch)->builtin_uint32;
707
708 fld = append_composite_type_field_raw (type, xstrdup (f->name),
709 field_type);
710
711 /* For little-endian, BITPOS counts from the LSB of
712 the structure and marks the LSB of the field. For
713 big-endian, BITPOS counts from the MSB of the
714 structure and marks the MSB of the field. Either
715 way, it is the number of bits to the "left" of the
716 field. To calculate this in big-endian, we need
717 the total size of the structure. */
718 bitsize = f->end - f->start + 1;
719 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
720 if (gdbarch_bits_big_endian (gdbarch))
721 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
722 else
723 SET_FIELD_BITPOS (fld[0], f->start);
724 FIELD_BITSIZE (fld[0]) = bitsize;
725 }
726 else
727 {
728 field_type = tdesc_gdb_type (gdbarch, f->type);
729 append_composite_type_field (type, xstrdup (f->name),
730 field_type);
731 }
732 }
733
734 if (tdesc_type->u.u.size != 0)
735 TYPE_LENGTH (type) = tdesc_type->u.u.size;
736 return type;
737 }
738
739 case TDESC_TYPE_UNION:
740 {
741 struct type *type, *field_type;
742 struct tdesc_type_field *f;
743 int ix;
744
745 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
746 TYPE_NAME (type) = xstrdup (tdesc_type->name);
747
748 for (ix = 0;
749 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
750 ix++)
751 {
752 field_type = tdesc_gdb_type (gdbarch, f->type);
753 append_composite_type_field (type, xstrdup (f->name), field_type);
754
755 /* If any of the children of a union are vectors, flag the
756 union as a vector also. This allows e.g. a union of two
757 vector types to show up automatically in "info vector". */
758 if (TYPE_VECTOR (field_type))
759 TYPE_VECTOR (type) = 1;
760 }
761 return type;
762 }
763
764 case TDESC_TYPE_FLAGS:
765 {
766 struct tdesc_type_flag *f;
767 int ix;
768
769 type = arch_flags_type (gdbarch, tdesc_type->name,
770 tdesc_type->u.f.size);
771 for (ix = 0;
772 VEC_iterate (tdesc_type_flag, tdesc_type->u.f.flags, ix, f);
773 ix++)
774 /* Note that contrary to the function name, this call will
775 just set the properties of an already-allocated
776 field. */
777 append_flags_type_flag (type, f->start,
778 *f->name ? f->name : NULL);
779
780 return type;
781 }
782 }
783
784 internal_error (__FILE__, __LINE__,
785 "Type \"%s\" has an unknown kind %d",
786 tdesc_type->name, tdesc_type->kind);
787 }
788 \f
789
790 /* Support for registers from target descriptions. */
791
792 /* Construct the per-gdbarch data. */
793
794 static void *
795 tdesc_data_init (struct obstack *obstack)
796 {
797 struct tdesc_arch_data *data;
798
799 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
800 return data;
801 }
802
803 /* Similar, but for the temporary copy used during architecture
804 initialization. */
805
806 struct tdesc_arch_data *
807 tdesc_data_alloc (void)
808 {
809 return XCNEW (struct tdesc_arch_data);
810 }
811
812 /* Free something allocated by tdesc_data_alloc, if it is not going
813 to be used (for instance if it was unsuitable for the
814 architecture). */
815
816 void
817 tdesc_data_cleanup (void *data_untyped)
818 {
819 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
820
821 VEC_free (tdesc_arch_reg, data->arch_regs);
822 xfree (data);
823 }
824
825 /* Search FEATURE for a register named NAME. */
826
827 static struct tdesc_reg *
828 tdesc_find_register_early (const struct tdesc_feature *feature,
829 const char *name)
830 {
831 int ixr;
832 struct tdesc_reg *reg;
833
834 for (ixr = 0;
835 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
836 ixr++)
837 if (strcasecmp (reg->name, name) == 0)
838 return reg;
839
840 return NULL;
841 }
842
843 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
844
845 int
846 tdesc_numbered_register (const struct tdesc_feature *feature,
847 struct tdesc_arch_data *data,
848 int regno, const char *name)
849 {
850 struct tdesc_arch_reg arch_reg = { 0 };
851 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
852
853 if (reg == NULL)
854 return 0;
855
856 /* Make sure the vector includes a REGNO'th element. */
857 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
858 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
859
860 arch_reg.reg = reg;
861 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
862 return 1;
863 }
864
865 /* Search FEATURE for a register named NAME, but do not assign a fixed
866 register number to it. */
867
868 int
869 tdesc_unnumbered_register (const struct tdesc_feature *feature,
870 const char *name)
871 {
872 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
873
874 if (reg == NULL)
875 return 0;
876
877 return 1;
878 }
879
880 /* Search FEATURE for a register whose name is in NAMES and assign
881 REGNO to it. */
882
883 int
884 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
885 struct tdesc_arch_data *data,
886 int regno, const char *const names[])
887 {
888 int i;
889
890 for (i = 0; names[i] != NULL; i++)
891 if (tdesc_numbered_register (feature, data, regno, names[i]))
892 return 1;
893
894 return 0;
895 }
896
897 /* Search FEATURE for a register named NAME, and return its size in
898 bits. The register must exist. */
899
900 int
901 tdesc_register_size (const struct tdesc_feature *feature,
902 const char *name)
903 {
904 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
905
906 gdb_assert (reg != NULL);
907 return reg->bitsize;
908 }
909
910 /* Look up a register by its GDB internal register number. */
911
912 static struct tdesc_arch_reg *
913 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
914 {
915 struct tdesc_arch_data *data;
916
917 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
918 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
919 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
920 else
921 return NULL;
922 }
923
924 static struct tdesc_reg *
925 tdesc_find_register (struct gdbarch *gdbarch, int regno)
926 {
927 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
928
929 return reg? reg->reg : NULL;
930 }
931
932 /* Return the name of register REGNO, from the target description or
933 from an architecture-provided pseudo_register_name method. */
934
935 const char *
936 tdesc_register_name (struct gdbarch *gdbarch, int regno)
937 {
938 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
939 int num_regs = gdbarch_num_regs (gdbarch);
940 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
941
942 if (reg != NULL)
943 return reg->name;
944
945 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
946 {
947 struct tdesc_arch_data *data
948 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
949
950 gdb_assert (data->pseudo_register_name != NULL);
951 return data->pseudo_register_name (gdbarch, regno);
952 }
953
954 return "";
955 }
956
957 struct type *
958 tdesc_register_type (struct gdbarch *gdbarch, int regno)
959 {
960 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
961 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
962 int num_regs = gdbarch_num_regs (gdbarch);
963 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
964
965 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
966 {
967 struct tdesc_arch_data *data
968 = (struct tdesc_arch_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
1107 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1108
1109 if (data->pseudo_register_reggroup_p != NULL)
1110 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1111 /* Otherwise fall through to the default reggroup_p. */
1112 }
1113
1114 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1115 if (ret != -1)
1116 return ret;
1117
1118 return default_register_reggroup_p (gdbarch, regno, reggroup);
1119 }
1120
1121 /* Record architecture-specific functions to call for pseudo-register
1122 support. */
1123
1124 void
1125 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1126 gdbarch_register_name_ftype *pseudo_name)
1127 {
1128 struct tdesc_arch_data *data
1129 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1130
1131 data->pseudo_register_name = pseudo_name;
1132 }
1133
1134 void
1135 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1136 gdbarch_register_type_ftype *pseudo_type)
1137 {
1138 struct tdesc_arch_data *data
1139 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1140
1141 data->pseudo_register_type = pseudo_type;
1142 }
1143
1144 void
1145 set_tdesc_pseudo_register_reggroup_p
1146 (struct gdbarch *gdbarch,
1147 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1148 {
1149 struct tdesc_arch_data *data
1150 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1151
1152 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1153 }
1154
1155 /* Update GDBARCH to use the target description for registers. */
1156
1157 void
1158 tdesc_use_registers (struct gdbarch *gdbarch,
1159 const struct target_desc *target_desc,
1160 struct tdesc_arch_data *early_data)
1161 {
1162 int num_regs = gdbarch_num_regs (gdbarch);
1163 int ixf, ixr;
1164 struct tdesc_feature *feature;
1165 struct tdesc_reg *reg;
1166 struct tdesc_arch_data *data;
1167 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
1168 htab_t reg_hash;
1169
1170 /* We can't use the description for registers if it doesn't describe
1171 any. This function should only be called after validating
1172 registers, so the caller should know that registers are
1173 included. */
1174 gdb_assert (tdesc_has_registers (target_desc));
1175
1176 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
1177 data->arch_regs = early_data->arch_regs;
1178 xfree (early_data);
1179
1180 /* Build up a set of all registers, so that we can assign register
1181 numbers where needed. The hash table expands as necessary, so
1182 the initial size is arbitrary. */
1183 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1184 for (ixf = 0;
1185 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1186 ixf++)
1187 for (ixr = 0;
1188 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1189 ixr++)
1190 {
1191 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1192
1193 *slot = reg;
1194 }
1195
1196 /* Remove any registers which were assigned numbers by the
1197 architecture. */
1198 for (ixr = 0;
1199 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1200 ixr++)
1201 if (arch_reg->reg)
1202 htab_remove_elt (reg_hash, arch_reg->reg);
1203
1204 /* Assign numbers to the remaining registers and add them to the
1205 list of registers. The new numbers are always above gdbarch_num_regs.
1206 Iterate over the features, not the hash table, so that the order
1207 matches that in the target description. */
1208
1209 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1210 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1211 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1212 for (ixf = 0;
1213 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1214 ixf++)
1215 for (ixr = 0;
1216 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1217 ixr++)
1218 if (htab_find (reg_hash, reg) != NULL)
1219 {
1220 new_arch_reg.reg = reg;
1221 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
1222 num_regs++;
1223 }
1224
1225 htab_delete (reg_hash);
1226
1227 /* Update the architecture. */
1228 set_gdbarch_num_regs (gdbarch, num_regs);
1229 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1230 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1231 set_gdbarch_remote_register_number (gdbarch,
1232 tdesc_remote_register_number);
1233 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1234 }
1235 \f
1236
1237 /* Methods for constructing a target description. */
1238
1239 static void
1240 tdesc_free_reg (struct tdesc_reg *reg)
1241 {
1242 xfree (reg->name);
1243 xfree (reg->type);
1244 xfree (reg->group);
1245 xfree (reg);
1246 }
1247
1248 void
1249 tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1250 int regnum, int save_restore, const char *group,
1251 int bitsize, const char *type)
1252 {
1253 struct tdesc_reg *reg = XCNEW (struct tdesc_reg);
1254
1255 reg->name = xstrdup (name);
1256 reg->target_regnum = regnum;
1257 reg->save_restore = save_restore;
1258 reg->group = group ? xstrdup (group) : NULL;
1259 reg->bitsize = bitsize;
1260 reg->type = type ? xstrdup (type) : xstrdup ("<unknown>");
1261
1262 /* If the register's type is target-defined, look it up now. We may not
1263 have easy access to the containing feature when we want it later. */
1264 reg->tdesc_type = tdesc_named_type (feature, reg->type);
1265
1266 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1267 }
1268
1269 static void
1270 tdesc_free_type (struct tdesc_type *type)
1271 {
1272 switch (type->kind)
1273 {
1274 case TDESC_TYPE_STRUCT:
1275 case TDESC_TYPE_UNION:
1276 {
1277 struct tdesc_type_field *f;
1278 int ix;
1279
1280 for (ix = 0;
1281 VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f);
1282 ix++)
1283 xfree (f->name);
1284
1285 VEC_free (tdesc_type_field, type->u.u.fields);
1286 }
1287 break;
1288
1289 case TDESC_TYPE_FLAGS:
1290 {
1291 struct tdesc_type_flag *f;
1292 int ix;
1293
1294 for (ix = 0;
1295 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix, f);
1296 ix++)
1297 xfree (f->name);
1298
1299 VEC_free (tdesc_type_flag, type->u.f.flags);
1300 }
1301 break;
1302
1303 default:
1304 break;
1305 }
1306
1307 xfree (type->name);
1308 xfree (type);
1309 }
1310
1311 struct tdesc_type *
1312 tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1313 struct tdesc_type *field_type, int count)
1314 {
1315 struct tdesc_type *type = XCNEW (struct tdesc_type);
1316
1317 type->name = xstrdup (name);
1318 type->kind = TDESC_TYPE_VECTOR;
1319 type->u.v.type = field_type;
1320 type->u.v.count = count;
1321
1322 VEC_safe_push (tdesc_type_p, feature->types, type);
1323 return type;
1324 }
1325
1326 struct tdesc_type *
1327 tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1328 {
1329 struct tdesc_type *type = XCNEW (struct tdesc_type);
1330
1331 type->name = xstrdup (name);
1332 type->kind = TDESC_TYPE_STRUCT;
1333
1334 VEC_safe_push (tdesc_type_p, feature->types, type);
1335 return type;
1336 }
1337
1338 /* Set the total length of TYPE. Structs which contain bitfields may
1339 omit the reserved bits, so the end of the last field may not
1340 suffice. */
1341
1342 void
1343 tdesc_set_struct_size (struct tdesc_type *type, int size)
1344 {
1345 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1346 gdb_assert (size > 0);
1347 type->u.u.size = size;
1348 }
1349
1350 struct tdesc_type *
1351 tdesc_create_union (struct tdesc_feature *feature, const char *name)
1352 {
1353 struct tdesc_type *type = XCNEW (struct tdesc_type);
1354
1355 type->name = xstrdup (name);
1356 type->kind = TDESC_TYPE_UNION;
1357
1358 VEC_safe_push (tdesc_type_p, feature->types, type);
1359 return type;
1360 }
1361
1362 struct tdesc_type *
1363 tdesc_create_flags (struct tdesc_feature *feature, const char *name,
1364 int size)
1365 {
1366 struct tdesc_type *type = XCNEW (struct tdesc_type);
1367
1368 gdb_assert (size > 0);
1369
1370 type->name = xstrdup (name);
1371 type->kind = TDESC_TYPE_FLAGS;
1372 type->u.f.size = size;
1373
1374 VEC_safe_push (tdesc_type_p, feature->types, type);
1375 return type;
1376 }
1377
1378 /* Add a new field. Return a temporary pointer to the field, which
1379 is only valid until the next call to tdesc_add_field (the vector
1380 might be reallocated). */
1381
1382 void
1383 tdesc_add_field (struct tdesc_type *type, const char *field_name,
1384 struct tdesc_type *field_type)
1385 {
1386 struct tdesc_type_field f = { 0 };
1387
1388 gdb_assert (type->kind == TDESC_TYPE_UNION
1389 || type->kind == TDESC_TYPE_STRUCT);
1390
1391 f.name = xstrdup (field_name);
1392 f.type = field_type;
1393
1394 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1395 }
1396
1397 /* Add a new bitfield. */
1398
1399 void
1400 tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1401 int start, int end)
1402 {
1403 struct tdesc_type_field f = { 0 };
1404
1405 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1406
1407 f.name = xstrdup (field_name);
1408 f.start = start;
1409 f.end = end;
1410
1411 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1412 }
1413
1414 void
1415 tdesc_add_flag (struct tdesc_type *type, int start,
1416 const char *flag_name)
1417 {
1418 struct tdesc_type_flag f = { 0 };
1419
1420 gdb_assert (type->kind == TDESC_TYPE_FLAGS);
1421
1422 f.name = xstrdup (flag_name);
1423 f.start = start;
1424
1425 VEC_safe_push (tdesc_type_flag, type->u.f.flags, &f);
1426 }
1427
1428 static void
1429 tdesc_free_feature (struct tdesc_feature *feature)
1430 {
1431 struct tdesc_reg *reg;
1432 struct tdesc_type *type;
1433 int ix;
1434
1435 for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++)
1436 tdesc_free_reg (reg);
1437 VEC_free (tdesc_reg_p, feature->registers);
1438
1439 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
1440 tdesc_free_type (type);
1441 VEC_free (tdesc_type_p, feature->types);
1442
1443 xfree (feature->name);
1444 xfree (feature);
1445 }
1446
1447 struct tdesc_feature *
1448 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1449 {
1450 struct tdesc_feature *new_feature = XCNEW (struct tdesc_feature);
1451
1452 new_feature->name = xstrdup (name);
1453
1454 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1455 return new_feature;
1456 }
1457
1458 struct target_desc *
1459 allocate_target_description (void)
1460 {
1461 return XCNEW (struct target_desc);
1462 }
1463
1464 static void
1465 free_target_description (void *arg)
1466 {
1467 struct target_desc *target_desc = (struct target_desc *) arg;
1468 struct tdesc_feature *feature;
1469 struct property *prop;
1470 int ix;
1471
1472 for (ix = 0;
1473 VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature);
1474 ix++)
1475 tdesc_free_feature (feature);
1476 VEC_free (tdesc_feature_p, target_desc->features);
1477
1478 for (ix = 0;
1479 VEC_iterate (property_s, target_desc->properties, ix, prop);
1480 ix++)
1481 {
1482 xfree (prop->key);
1483 xfree (prop->value);
1484 }
1485 VEC_free (property_s, target_desc->properties);
1486
1487 VEC_free (arch_p, target_desc->compatible);
1488
1489 xfree (target_desc);
1490 }
1491
1492 struct cleanup *
1493 make_cleanup_free_target_description (struct target_desc *target_desc)
1494 {
1495 return make_cleanup (free_target_description, target_desc);
1496 }
1497
1498 void
1499 tdesc_add_compatible (struct target_desc *target_desc,
1500 const struct bfd_arch_info *compatible)
1501 {
1502 const struct bfd_arch_info *compat;
1503 int ix;
1504
1505 /* If this instance of GDB is compiled without BFD support for the
1506 compatible architecture, simply ignore it -- we would not be able
1507 to handle it anyway. */
1508 if (compatible == NULL)
1509 return;
1510
1511 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1512 ix++)
1513 if (compat == compatible)
1514 internal_error (__FILE__, __LINE__,
1515 _("Attempted to add duplicate "
1516 "compatible architecture \"%s\""),
1517 compatible->printable_name);
1518
1519 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1520 }
1521
1522 void
1523 set_tdesc_property (struct target_desc *target_desc,
1524 const char *key, const char *value)
1525 {
1526 struct property *prop, new_prop;
1527 int ix;
1528
1529 gdb_assert (key != NULL && value != NULL);
1530
1531 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1532 ix++)
1533 if (strcmp (prop->key, key) == 0)
1534 internal_error (__FILE__, __LINE__,
1535 _("Attempted to add duplicate property \"%s\""), key);
1536
1537 new_prop.key = xstrdup (key);
1538 new_prop.value = xstrdup (value);
1539 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1540 }
1541
1542 void
1543 set_tdesc_architecture (struct target_desc *target_desc,
1544 const struct bfd_arch_info *arch)
1545 {
1546 target_desc->arch = arch;
1547 }
1548
1549 void
1550 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1551 {
1552 target_desc->osabi = osabi;
1553 }
1554 \f
1555
1556 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1557 static struct cmd_list_element *tdesc_unset_cmdlist;
1558
1559 /* Helper functions for the CLI commands. */
1560
1561 static void
1562 set_tdesc_cmd (char *args, int from_tty)
1563 {
1564 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
1565 }
1566
1567 static void
1568 show_tdesc_cmd (char *args, int from_tty)
1569 {
1570 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1571 }
1572
1573 static void
1574 unset_tdesc_cmd (char *args, int from_tty)
1575 {
1576 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
1577 }
1578
1579 static void
1580 set_tdesc_filename_cmd (char *args, int from_tty,
1581 struct cmd_list_element *c)
1582 {
1583 xfree (target_description_filename);
1584 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1585
1586 target_clear_description ();
1587 target_find_description ();
1588 }
1589
1590 static void
1591 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1592 struct cmd_list_element *c,
1593 const char *value)
1594 {
1595 value = target_description_filename;
1596
1597 if (value != NULL && *value != '\0')
1598 printf_filtered (_("The target description will be read from \"%s\".\n"),
1599 value);
1600 else
1601 printf_filtered (_("The target description will be "
1602 "read from the target.\n"));
1603 }
1604
1605 static void
1606 unset_tdesc_filename_cmd (char *args, int from_tty)
1607 {
1608 xfree (target_description_filename);
1609 target_description_filename = NULL;
1610 target_clear_description ();
1611 target_find_description ();
1612 }
1613
1614 static void
1615 maint_print_c_tdesc_cmd (char *args, int from_tty)
1616 {
1617 const struct target_desc *tdesc;
1618 const struct bfd_arch_info *compatible;
1619 const char *filename, *inp;
1620 char *function, *outp;
1621 struct property *prop;
1622 struct tdesc_feature *feature;
1623 struct tdesc_reg *reg;
1624 struct tdesc_type *type;
1625 struct tdesc_type_field *f;
1626 struct tdesc_type_flag *flag;
1627 int ix, ix2, ix3;
1628 int printed_field_type = 0;
1629
1630 /* Use the global target-supplied description, not the current
1631 architecture's. This lets a GDB for one architecture generate C
1632 for another architecture's description, even though the gdbarch
1633 initialization code will reject the new description. */
1634 tdesc = current_target_desc;
1635 if (tdesc == NULL)
1636 error (_("There is no target description to print."));
1637
1638 if (target_description_filename == NULL)
1639 error (_("The current target description did not come from an XML file."));
1640
1641 filename = lbasename (target_description_filename);
1642 function = (char *) alloca (strlen (filename) + 1);
1643 for (inp = filename, outp = function; *inp != '\0'; inp++)
1644 if (*inp == '.')
1645 break;
1646 else if (*inp == '-')
1647 *outp++ = '_';
1648 else
1649 *outp++ = *inp;
1650 *outp = '\0';
1651
1652 /* Standard boilerplate. */
1653 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1654 "-*- buffer-read-only: t -*- vi"
1655 ":set ro:\n");
1656 printf_unfiltered (" Original: %s */\n\n", filename);
1657 printf_unfiltered ("#include \"defs.h\"\n");
1658 printf_unfiltered ("#include \"osabi.h\"\n");
1659 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1660 printf_unfiltered ("\n");
1661
1662 printf_unfiltered ("struct target_desc *tdesc_%s;\n", function);
1663 printf_unfiltered ("static void\n");
1664 printf_unfiltered ("initialize_tdesc_%s (void)\n", function);
1665 printf_unfiltered ("{\n");
1666 printf_unfiltered
1667 (" struct target_desc *result = allocate_target_description ();\n");
1668 printf_unfiltered (" struct tdesc_feature *feature;\n");
1669
1670 /* Now we do some "filtering" in order to know which variables to
1671 declare. This is needed because otherwise we would declare unused
1672 variables `field_type' and `type'. */
1673 for (ix = 0;
1674 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1675 ix++)
1676 {
1677 int printed_desc_type = 0;
1678
1679 for (ix2 = 0;
1680 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1681 ix2++)
1682 {
1683 if (!printed_field_type)
1684 {
1685 printf_unfiltered (" struct tdesc_type *field_type;\n");
1686 printed_field_type = 1;
1687 }
1688
1689 if (((type->kind == TDESC_TYPE_UNION
1690 || type->kind == TDESC_TYPE_STRUCT)
1691 && VEC_length (tdesc_type_field, type->u.u.fields) > 0)
1692 || (type->kind == TDESC_TYPE_FLAGS
1693 && VEC_length (tdesc_type_flag, type->u.f.flags) > 0))
1694 {
1695 printf_unfiltered (" struct tdesc_type *type;\n");
1696 printed_desc_type = 1;
1697 break;
1698 }
1699 }
1700
1701 if (printed_desc_type)
1702 break;
1703 }
1704
1705 printf_unfiltered ("\n");
1706
1707 if (tdesc_architecture (tdesc) != NULL)
1708 {
1709 printf_unfiltered
1710 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1711 tdesc_architecture (tdesc)->printable_name);
1712 printf_unfiltered ("\n");
1713 }
1714
1715 if (tdesc_osabi (tdesc) > GDB_OSABI_UNKNOWN
1716 && tdesc_osabi (tdesc) < GDB_OSABI_INVALID)
1717 {
1718 printf_unfiltered
1719 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1720 gdbarch_osabi_name (tdesc_osabi (tdesc)));
1721 printf_unfiltered ("\n");
1722 }
1723
1724 for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible);
1725 ix++)
1726 {
1727 printf_unfiltered
1728 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1729 compatible->printable_name);
1730 }
1731 if (ix)
1732 printf_unfiltered ("\n");
1733
1734 for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop);
1735 ix++)
1736 {
1737 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1738 prop->key, prop->value);
1739 }
1740
1741 for (ix = 0;
1742 VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature);
1743 ix++)
1744 {
1745 printf_unfiltered (" \
1746 feature = tdesc_create_feature (result, \"%s\");\n",
1747 feature->name);
1748
1749 for (ix2 = 0;
1750 VEC_iterate (tdesc_type_p, feature->types, ix2, type);
1751 ix2++)
1752 {
1753 switch (type->kind)
1754 {
1755 case TDESC_TYPE_VECTOR:
1756 printf_unfiltered
1757 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1758 type->u.v.type->name);
1759 printf_unfiltered
1760 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
1761 type->name, type->u.v.count);
1762 break;
1763 case TDESC_TYPE_STRUCT:
1764 printf_unfiltered
1765 (" type = tdesc_create_struct (feature, \"%s\");\n",
1766 type->name);
1767 if (type->u.u.size != 0)
1768 printf_unfiltered
1769 (" tdesc_set_struct_size (type, %s);\n",
1770 plongest (type->u.u.size));
1771 for (ix3 = 0;
1772 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1773 ix3++)
1774 {
1775 /* Going first for implicitly sized types, else part handles
1776 bitfields. As reported on xml-tdesc.c implicitly sized types
1777 cannot contain a bitfield. */
1778 if (f->type != NULL)
1779 {
1780 printf_unfiltered
1781 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1782 f->type->name);
1783 printf_unfiltered
1784 (" tdesc_add_field (type, \"%s\", field_type);\n",
1785 f->name);
1786 }
1787 else
1788 printf_unfiltered
1789 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
1790 f->name, f->start, f->end);
1791 }
1792 break;
1793 case TDESC_TYPE_UNION:
1794 printf_unfiltered
1795 (" type = tdesc_create_union (feature, \"%s\");\n",
1796 type->name);
1797 for (ix3 = 0;
1798 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
1799 ix3++)
1800 {
1801 printf_unfiltered
1802 (" field_type = tdesc_named_type (feature, \"%s\");\n",
1803 f->type->name);
1804 printf_unfiltered
1805 (" tdesc_add_field (type, \"%s\", field_type);\n",
1806 f->name);
1807 }
1808 break;
1809 case TDESC_TYPE_FLAGS:
1810 printf_unfiltered
1811 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
1812 type->name, (int) type->u.f.size);
1813 for (ix3 = 0;
1814 VEC_iterate (tdesc_type_flag, type->u.f.flags, ix3,
1815 flag);
1816 ix3++)
1817 printf_unfiltered
1818 (" tdesc_add_flag (type, %d, \"%s\");\n",
1819 flag->start, flag->name);
1820 break;
1821 default:
1822 error (_("C output is not supported type \"%s\"."), type->name);
1823 }
1824 printf_unfiltered ("\n");
1825 }
1826
1827 for (ix2 = 0;
1828 VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg);
1829 ix2++)
1830 {
1831 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1832 reg->name, reg->target_regnum, reg->save_restore);
1833 if (reg->group)
1834 printf_unfiltered ("\"%s\", ", reg->group);
1835 else
1836 printf_unfiltered ("NULL, ");
1837 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
1838 }
1839
1840 printf_unfiltered ("\n");
1841 }
1842
1843 printf_unfiltered (" tdesc_%s = result;\n", function);
1844 printf_unfiltered ("}\n");
1845 }
1846
1847 /* Provide a prototype to silence -Wmissing-prototypes. */
1848 extern initialize_file_ftype _initialize_target_descriptions;
1849
1850 void
1851 _initialize_target_descriptions (void)
1852 {
1853 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1854
1855 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1856 Set target description specific variables."),
1857 &tdesc_set_cmdlist, "set tdesc ",
1858 0 /* allow-unknown */, &setlist);
1859 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1860 Show target description specific variables."),
1861 &tdesc_show_cmdlist, "show tdesc ",
1862 0 /* allow-unknown */, &showlist);
1863 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1864 Unset target description specific variables."),
1865 &tdesc_unset_cmdlist, "unset tdesc ",
1866 0 /* allow-unknown */, &unsetlist);
1867
1868 add_setshow_filename_cmd ("filename", class_obscure,
1869 &tdesc_filename_cmd_string,
1870 _("\
1871 Set the file to read for an XML target description"), _("\
1872 Show the file to read for an XML target description"), _("\
1873 When set, GDB will read the target description from a local\n\
1874 file instead of querying the remote target."),
1875 set_tdesc_filename_cmd,
1876 show_tdesc_filename_cmd,
1877 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1878
1879 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1880 Unset the file to read for an XML target description. When unset,\n\
1881 GDB will read the description from the target."),
1882 &tdesc_unset_cmdlist);
1883
1884 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1885 Print the current target description as a C source file."),
1886 &maintenanceprintlist);
1887 }
This page took 0.067906 seconds and 5 git commands to generate.