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