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