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