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