]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/target-descriptions.c
Use visitor pattern for "maint print c-tdesc"
[thirdparty/binutils-gdb.git] / gdb / target-descriptions.c
CommitLineData
424163ea
DJ
1/* Target description support for GDB.
2
61baf725 3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
424163ea
DJ
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
424163ea
DJ
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
424163ea
DJ
21
22#include "defs.h"
23#include "arch-utils.h"
23181151 24#include "gdbcmd.h"
123dc839
DJ
25#include "gdbtypes.h"
26#include "reggroups.h"
424163ea
DJ
27#include "target.h"
28#include "target-descriptions.h"
29709017 29#include "vec.h"
123dc839 30#include "xml-support.h"
23181151 31#include "xml-tdesc.h"
c3f08eb7 32#include "osabi.h"
424163ea 33
123dc839
DJ
34#include "gdb_obstack.h"
35#include "hashtab.h"
6ecd4729 36#include "inferior.h"
424163ea 37
6eb1e6a8
YQ
38/* The interface to visit different elements of target description. */
39
40class tdesc_element_visitor
41{
42public:
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
51class tdesc_element
52{
53public:
54 virtual void accept (tdesc_element_visitor &v) const = 0;
55};
56
424163ea
DJ
57/* Types. */
58
29709017
DJ
59typedef struct property
60{
23181151
DJ
61 char *key;
62 char *value;
29709017
DJ
63} property_s;
64DEF_VEC_O(property_s);
65
123dc839
DJ
66/* An individual register from a target description. */
67
6eb1e6a8 68typedef struct tdesc_reg : tdesc_element
123dc839 69{
72ddacb7
YQ
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
6eb1e6a8 84 virtual ~tdesc_reg ()
72ddacb7
YQ
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
123dc839
DJ
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. */
ad068eab 127 struct tdesc_type *tdesc_type;
6eb1e6a8
YQ
128
129 void accept (tdesc_element_visitor &v) const override
130 {
131 v.visit (this);
132 }
133
123dc839
DJ
134} *tdesc_reg_p;
135DEF_VEC_P(tdesc_reg_p);
136
137/* A named type from a target description. */
ad068eab
UW
138
139typedef struct tdesc_type_field
140{
141 char *name;
142 struct tdesc_type *type;
81516450
DE
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. */
f5dff777 146 int start, end;
ad068eab
UW
147} tdesc_type_field;
148DEF_VEC_O(tdesc_type_field);
149
52059ffd
TT
150enum tdesc_type_kind
151{
152 /* Predefined types. */
81516450 153 TDESC_TYPE_BOOL,
52059ffd
TT
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,
81516450
DE
175 TDESC_TYPE_FLAGS,
176 TDESC_TYPE_ENUM
52059ffd
TT
177};
178
6eb1e6a8 179typedef struct tdesc_type : tdesc_element
ad068eab 180{
72ddacb7
YQ
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
6eb1e6a8 187 virtual ~tdesc_type ()
72ddacb7
YQ
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
a121b7c1
PA
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;
ad068eab
UW
221
222 /* Identify the kind of this type. */
52059ffd 223 enum tdesc_type_kind kind;
ad068eab
UW
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
81516450 235 /* Struct, union, flags, or enum type. */
ad068eab
UW
236 struct
237 {
238 VEC(tdesc_type_field) *fields;
54157a25 239 int size;
ad068eab
UW
240 } u;
241 } u;
6eb1e6a8
YQ
242
243 void accept (tdesc_element_visitor &v) const override
244 {
245 v.visit (this);
246 }
247
ad068eab
UW
248} *tdesc_type_p;
249DEF_VEC_P(tdesc_type_p);
123dc839
DJ
250
251/* A feature from a target description. Each feature is a collection
252 of other elements, e.g. registers and types. */
253
6eb1e6a8 254typedef struct tdesc_feature : tdesc_element
123dc839 255{
72ddacb7
YQ
256 tdesc_feature (const char *name_)
257 : name (xstrdup (name_))
258 {}
259
6eb1e6a8 260 virtual ~tdesc_feature ()
72ddacb7
YQ
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
123dc839
DJ
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. */
72ddacb7 286 VEC(tdesc_reg_p) *registers = NULL;
123dc839
DJ
287
288 /* The types associated with this feature. */
72ddacb7 289 VEC(tdesc_type_p) *types = NULL;
6eb1e6a8
YQ
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
123dc839
DJ
311} *tdesc_feature_p;
312DEF_VEC_P(tdesc_feature_p);
313
e35359c5
UW
314/* A compatible architecture from a target description. */
315typedef const struct bfd_arch_info *arch_p;
316DEF_VEC_P(arch_p);
317
123dc839
DJ
318/* A target description. */
319
6eb1e6a8 320struct target_desc : tdesc_element
424163ea 321{
b468ff4c
YQ
322 target_desc ()
323 {}
324
6eb1e6a8 325 virtual ~target_desc ()
b468ff4c
YQ
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
23181151 352 /* The architecture reported by the target, if any. */
b468ff4c 353 const struct bfd_arch_info *arch = NULL;
23181151 354
08d16641
PA
355 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
356 otherwise. */
b468ff4c 357 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
08d16641 358
e35359c5 359 /* The list of compatible architectures reported by the target. */
b468ff4c 360 VEC(arch_p) *compatible = NULL;
e35359c5 361
29709017 362 /* Any architecture-specific properties specified by the target. */
b468ff4c 363 VEC(property_s) *properties = NULL;
123dc839
DJ
364
365 /* The features associated with this target. */
b468ff4c 366 VEC(tdesc_feature_p) *features = NULL;
6eb1e6a8
YQ
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 }
123dc839
DJ
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
ad068eab
UW
387typedef struct tdesc_arch_reg
388{
389 struct tdesc_reg *reg;
390 struct type *type;
391} tdesc_arch_reg;
392DEF_VEC_O(tdesc_arch_reg);
393
123dc839
DJ
394struct tdesc_arch_data
395{
ad068eab 396 /* A list of register/type pairs, indexed by GDB's internal register number.
123dc839
DJ
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). */
ad068eab 402 VEC(tdesc_arch_reg) *arch_regs;
123dc839
DJ
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;
424163ea
DJ
409};
410
6ecd4729
PA
411/* Info about an inferior's target description. There's one of these
412 for each inferior. */
424163ea 413
6ecd4729
PA
414struct 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;
424163ea 420
6ecd4729
PA
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. */
424163ea 426
6ecd4729 427 const struct target_desc *tdesc;
424163ea 428
6ecd4729
PA
429 /* The filename to read a target description from, as set by "set
430 tdesc filename ..." */
424163ea 431
6ecd4729
PA
432 char *filename;
433};
23181151 434
6ecd4729
PA
435/* Get the inferior INF's target description info, allocating one on
436 the stop if necessary. */
23181151 437
6ecd4729
PA
438static struct target_desc_info *
439get_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}
23181151 445
123dc839
DJ
446/* A handle for architecture-specific data associated with the
447 target description (see struct tdesc_arch_data). */
448
449static struct gdbarch_data *tdesc_data;
450
6ecd4729
PA
451/* See target-descriptions.h. */
452
453int
454target_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
461void
462copy_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
474void
475target_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
495static char *tdesc_filename_cmd_string;
496
424163ea
DJ
497/* Fetch the current target's description, and switch the current
498 architecture to one which incorporates that description. */
499
500void
501target_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. */
f5656ead 513 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
424163ea 514
23181151
DJ
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);
424163ea
DJ
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))
123dc839
DJ
541 warning (_("Architecture rejected target-supplied description"));
542 else
543 {
544 struct tdesc_arch_data *data;
545
19ba03f4
SM
546 data = ((struct tdesc_arch_data *)
547 gdbarch_data (target_gdbarch (), tdesc_data));
123dc839 548 if (tdesc_has_registers (current_target_desc)
ad068eab 549 && data->arch_regs == NULL)
123dc839
DJ
550 warning (_("Target-supplied registers are not supported "
551 "by the current architecture"));
552 }
424163ea
DJ
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
563void
564target_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
584const struct target_desc *
585target_current_description (void)
586{
587 if (target_desc_fetched)
588 return current_target_desc;
589
590 return NULL;
591}
e35359c5
UW
592
593/* Return non-zero if this target description is compatible
594 with the given BFD architecture. */
595
596int
597tdesc_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}
23181151
DJ
614\f
615
123dc839 616/* Direct accessors for target descriptions. */
424163ea 617
29709017
DJ
618/* Return the string value of a property named KEY, or NULL if the
619 property was not specified. */
620
621const char *
622tdesc_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
23181151
DJ
635/* Return the BFD architecture associated with this target
636 description, or NULL if no architecture was specified. */
637
638const struct bfd_arch_info *
639tdesc_architecture (const struct target_desc *target_desc)
640{
641 return target_desc->arch;
642}
08d16641
PA
643
644/* Return the OSABI associated with this target description, or
645 GDB_OSABI_UNKNOWN if no osabi was specified. */
646
647enum gdb_osabi
648tdesc_osabi (const struct target_desc *target_desc)
649{
650 return target_desc->osabi;
651}
652
23181151
DJ
653\f
654
123dc839
DJ
655/* Return 1 if this target description includes any registers. */
656
657int
658tdesc_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
678const struct tdesc_feature *
679tdesc_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
696const char *
697tdesc_feature_name (const struct tdesc_feature *feature)
698{
699 return feature->name;
700}
701
ad068eab
UW
702/* Predefined types. */
703static struct tdesc_type tdesc_predefined_types[] =
81adfced 704{
81516450 705 { "bool", TDESC_TYPE_BOOL },
ad068eab
UW
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 },
9fd3625f 720 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT },
a6f5ef51 721 { "i387_ext", TDESC_TYPE_I387_EXT }
ad068eab 722};
81adfced 723
81516450
DE
724/* Lookup a predefined type. */
725
726static struct tdesc_type *
727tdesc_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
123dc839
DJ
739/* Return the type associated with ID in the context of FEATURE, or
740 NULL if none. */
741
ad068eab 742struct tdesc_type *
123dc839
DJ
743tdesc_named_type (const struct tdesc_feature *feature, const char *id)
744{
745 int ix;
ad068eab 746 struct tdesc_type *type;
123dc839
DJ
747
748 /* First try target-defined types. */
ad068eab
UW
749 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
750 if (strcmp (type->name, id) == 0)
751 return type;
123dc839 752
81adfced
DJ
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)
ad068eab 756 return &tdesc_predefined_types[ix];
123dc839
DJ
757
758 return NULL;
759}
ad068eab 760
9fd3625f
L
761/* Lookup type associated with ID. */
762
763struct type *
764tdesc_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
19ba03f4 770 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
9fd3625f
L
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
ad068eab
UW
785/* Construct, if necessary, and return the GDB type implementing target
786 type TDESC_TYPE for architecture GDBARCH. */
787
788static struct type *
789tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
790{
9fd3625f
L
791 struct type *type;
792
ad068eab
UW
793 switch (tdesc_type->kind)
794 {
795 /* Predefined types. */
81516450
DE
796 case TDESC_TYPE_BOOL:
797 return builtin_type (gdbarch)->builtin_bool;
798
ad068eab 799 case TDESC_TYPE_INT8:
df4df182 800 return builtin_type (gdbarch)->builtin_int8;
ad068eab
UW
801
802 case TDESC_TYPE_INT16:
df4df182 803 return builtin_type (gdbarch)->builtin_int16;
ad068eab
UW
804
805 case TDESC_TYPE_INT32:
df4df182 806 return builtin_type (gdbarch)->builtin_int32;
ad068eab
UW
807
808 case TDESC_TYPE_INT64:
df4df182 809 return builtin_type (gdbarch)->builtin_int64;
ad068eab
UW
810
811 case TDESC_TYPE_INT128:
df4df182 812 return builtin_type (gdbarch)->builtin_int128;
ad068eab
UW
813
814 case TDESC_TYPE_UINT8:
df4df182 815 return builtin_type (gdbarch)->builtin_uint8;
ad068eab
UW
816
817 case TDESC_TYPE_UINT16:
df4df182 818 return builtin_type (gdbarch)->builtin_uint16;
ad068eab
UW
819
820 case TDESC_TYPE_UINT32:
df4df182 821 return builtin_type (gdbarch)->builtin_uint32;
ad068eab
UW
822
823 case TDESC_TYPE_UINT64:
df4df182 824 return builtin_type (gdbarch)->builtin_uint64;
ad068eab
UW
825
826 case TDESC_TYPE_UINT128:
df4df182 827 return builtin_type (gdbarch)->builtin_uint128;
ad068eab
UW
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
9fd3625f
L
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 {
ad068eab 845 case TDESC_TYPE_IEEE_SINGLE:
e9bb382b 846 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
27067745 847 floatformats_ieee_single);
ad068eab
UW
848
849 case TDESC_TYPE_IEEE_DOUBLE:
e9bb382b 850 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
27067745 851 floatformats_ieee_double);
ad068eab
UW
852
853 case TDESC_TYPE_ARM_FPA_EXT:
e9bb382b 854 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745 855 floatformats_arm_ext);
ad068eab 856
9fd3625f
L
857 case TDESC_TYPE_I387_EXT:
858 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
859 floatformats_i387_ext);
860
ad068eab
UW
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
f5dff777
DJ
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 {
81516450 887 if (f->start != -1 && f->end != -1)
f5dff777
DJ
888 {
889 /* Bitfield. */
890 struct field *fld;
891 struct type *field_type;
892 int bitsize, total_size;
893
81516450 894 /* This invariant should be preserved while creating types. */
f5dff777 895 gdb_assert (tdesc_type->u.u.size != 0);
81516450
DE
896 if (f->type != NULL)
897 field_type = tdesc_gdb_type (gdbarch, f->type);
898 else if (tdesc_type->u.u.size > 4)
f5dff777
DJ
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))
f41f5e61 916 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
f5dff777 917 else
f41f5e61 918 SET_FIELD_BITPOS (fld[0], f->start);
f5dff777
DJ
919 FIELD_BITSIZE (fld[0]) = bitsize;
920 }
921 else
922 {
81516450 923 gdb_assert (f->start == -1 && f->end == -1);
f5dff777
DJ
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
ad068eab
UW
935 case TDESC_TYPE_UNION:
936 {
937 struct type *type, *field_type;
938 struct tdesc_type_field *f;
939 int ix;
940
e9bb382b 941 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
ad068eab
UW
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
f5dff777 951 /* If any of the children of a union are vectors, flag the
ad068eab
UW
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 }
f5dff777
DJ
957 return type;
958 }
959
960 case TDESC_TYPE_FLAGS:
961 {
81516450 962 struct tdesc_type_field *f;
f5dff777
DJ
963 int ix;
964
3494b66d 965 type = arch_flags_type (gdbarch, tdesc_type->name,
81516450
DE
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;
f5dff777 991 for (ix = 0;
81516450 992 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
f5dff777 993 ix++)
81516450
DE
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 }
ad068eab
UW
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}
123dc839
DJ
1010\f
1011
1012/* Support for registers from target descriptions. */
1013
1014/* Construct the per-gdbarch data. */
1015
1016static void *
1017tdesc_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
1028struct tdesc_arch_data *
1029tdesc_data_alloc (void)
1030{
41bf6aca 1031 return XCNEW (struct tdesc_arch_data);
123dc839
DJ
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
1038void
1039tdesc_data_cleanup (void *data_untyped)
1040{
19ba03f4 1041 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
123dc839 1042
ad068eab 1043 VEC_free (tdesc_arch_reg, data->arch_regs);
123dc839
DJ
1044 xfree (data);
1045}
1046
1047/* Search FEATURE for a register named NAME. */
1048
7cc46491
DJ
1049static struct tdesc_reg *
1050tdesc_find_register_early (const struct tdesc_feature *feature,
1051 const char *name)
123dc839
DJ
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)
7cc46491 1060 return reg;
123dc839 1061
7cc46491
DJ
1062 return NULL;
1063}
1064
1065/* Search FEATURE for a register named NAME. Assign REGNO to it. */
1066
1067int
1068tdesc_numbered_register (const struct tdesc_feature *feature,
1069 struct tdesc_arch_data *data,
1070 int regno, const char *name)
1071{
ad068eab 1072 struct tdesc_arch_reg arch_reg = { 0 };
7cc46491
DJ
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. */
ad068eab
UW
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);
7cc46491 1084 return 1;
123dc839
DJ
1085}
1086
58d6951d
DJ
1087/* Search FEATURE for a register named NAME, but do not assign a fixed
1088 register number to it. */
1089
1090int
1091tdesc_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
7cc46491
DJ
1102/* Search FEATURE for a register whose name is in NAMES and assign
1103 REGNO to it. */
123dc839
DJ
1104
1105int
1106tdesc_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
7cc46491
DJ
1119/* Search FEATURE for a register named NAME, and return its size in
1120 bits. The register must exist. */
1121
1122int
1123tdesc_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
123dc839
DJ
1132/* Look up a register by its GDB internal register number. */
1133
ad068eab
UW
1134static struct tdesc_arch_reg *
1135tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
123dc839 1136{
123dc839
DJ
1137 struct tdesc_arch_data *data;
1138
19ba03f4 1139 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
ad068eab
UW
1140 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1141 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
123dc839
DJ
1142 else
1143 return NULL;
1144}
1145
ad068eab
UW
1146static struct tdesc_reg *
1147tdesc_find_register (struct gdbarch *gdbarch, int regno)
1148{
1149 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
5d502164 1150
ad068eab
UW
1151 return reg? reg->reg : NULL;
1152}
1153
f8b73d13
DJ
1154/* Return the name of register REGNO, from the target description or
1155 from an architecture-provided pseudo_register_name method. */
1156
1157const char *
d93859e2 1158tdesc_register_name (struct gdbarch *gdbarch, int regno)
123dc839 1159{
d93859e2
UW
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);
123dc839
DJ
1163
1164 if (reg != NULL)
1165 return reg->name;
1166
1167 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1168 {
19ba03f4
SM
1169 struct tdesc_arch_data *data
1170 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1171
123dc839 1172 gdb_assert (data->pseudo_register_name != NULL);
d93859e2 1173 return data->pseudo_register_name (gdbarch, regno);
123dc839
DJ
1174 }
1175
1176 return "";
1177}
1178
58d6951d 1179struct type *
123dc839
DJ
1180tdesc_register_type (struct gdbarch *gdbarch, int regno)
1181{
ad068eab
UW
1182 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1183 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
123dc839
DJ
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 {
19ba03f4
SM
1189 struct tdesc_arch_data *data
1190 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1191
123dc839
DJ
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. */
df4df182 1198 return builtin_type (gdbarch)->builtin_int0;
123dc839 1199
ad068eab 1200 if (arch_reg->type == NULL)
123dc839 1201 {
ad068eab
UW
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))
c378eb4e 1235 /* A bit desperate by this point... */
ad068eab
UW
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);
123dc839 1249 }
123dc839 1250
ad068eab 1251 return arch_reg->type;
123dc839
DJ
1252}
1253
1254static int
1255tdesc_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.
f8b73d13
DJ
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.
123dc839
DJ
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
f8b73d13
DJ
1280int
1281tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1282 struct reggroup *reggroup)
123dc839 1283{
123dc839
DJ
1284 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1285
123dc839
DJ
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
f8b73d13
DJ
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
1318static int
1319tdesc_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 {
19ba03f4
SM
1328 struct tdesc_arch_data *data
1329 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1330
58d6951d
DJ
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. */
f8b73d13
DJ
1334 }
1335
1336 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1337 if (ret != -1)
1338 return ret;
1339
123dc839
DJ
1340 return default_register_reggroup_p (gdbarch, regno, reggroup);
1341}
1342
1343/* Record architecture-specific functions to call for pseudo-register
1344 support. */
1345
1346void
1347set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1348 gdbarch_register_name_ftype *pseudo_name)
1349{
19ba03f4
SM
1350 struct tdesc_arch_data *data
1351 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1352
1353 data->pseudo_register_name = pseudo_name;
1354}
1355
1356void
1357set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1358 gdbarch_register_type_ftype *pseudo_type)
1359{
19ba03f4
SM
1360 struct tdesc_arch_data *data
1361 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1362
1363 data->pseudo_register_type = pseudo_type;
1364}
1365
1366void
1367set_tdesc_pseudo_register_reggroup_p
1368 (struct gdbarch *gdbarch,
1369 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1370{
19ba03f4
SM
1371 struct tdesc_arch_data *data
1372 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1373
1374 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1375}
1376
1377/* Update GDBARCH to use the target description for registers. */
1378
1379void
1380tdesc_use_registers (struct gdbarch *gdbarch,
7cc46491 1381 const struct target_desc *target_desc,
123dc839
DJ
1382 struct tdesc_arch_data *early_data)
1383{
1384 int num_regs = gdbarch_num_regs (gdbarch);
c6913b7d 1385 int ixf, ixr;
123dc839
DJ
1386 struct tdesc_feature *feature;
1387 struct tdesc_reg *reg;
1388 struct tdesc_arch_data *data;
ad068eab 1389 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
123dc839
DJ
1390 htab_t reg_hash;
1391
123dc839
DJ
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
19ba03f4 1398 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
ad068eab 1399 data->arch_regs = early_data->arch_regs;
123dc839
DJ
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. */
ad068eab
UW
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);
123dc839
DJ
1425
1426 /* Assign numbers to the remaining registers and add them to the
f57d151a 1427 list of registers. The new numbers are always above gdbarch_num_regs.
123dc839
DJ
1428 Iterate over the features, not the hash table, so that the order
1429 matches that in the target description. */
1430
ad068eab
UW
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);
123dc839
DJ
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 {
ad068eab
UW
1442 new_arch_reg.reg = reg;
1443 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
123dc839
DJ
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
123dc839
DJ
1459void
1460tdesc_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{
72ddacb7
YQ
1464 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1465 group, bitsize, type);
123dc839
DJ
1466
1467 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1468}
1469
ad068eab
UW
1470struct tdesc_type *
1471tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1472 struct tdesc_type *field_type, int count)
1473{
72ddacb7 1474 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
ad068eab 1475
ad068eab
UW
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
f5dff777
DJ
1483struct tdesc_type *
1484tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1485{
72ddacb7 1486 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
f5dff777
DJ
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
1496void
54157a25 1497tdesc_set_struct_size (struct tdesc_type *type, int size)
f5dff777
DJ
1498{
1499 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
54157a25 1500 gdb_assert (size > 0);
f5dff777
DJ
1501 type->u.u.size = size;
1502}
1503
ad068eab
UW
1504struct tdesc_type *
1505tdesc_create_union (struct tdesc_feature *feature, const char *name)
1506{
72ddacb7 1507 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
ad068eab
UW
1508
1509 VEC_safe_push (tdesc_type_p, feature->types, type);
1510 return type;
1511}
1512
f5dff777
DJ
1513struct tdesc_type *
1514tdesc_create_flags (struct tdesc_feature *feature, const char *name,
54157a25 1515 int size)
f5dff777 1516{
72ddacb7 1517 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_FLAGS);
f5dff777 1518
54157a25
DE
1519 gdb_assert (size > 0);
1520
81516450 1521 type->u.u.size = size;
f5dff777
DJ
1522
1523 VEC_safe_push (tdesc_type_p, feature->types, type);
1524 return type;
1525}
1526
81516450
DE
1527struct tdesc_type *
1528tdesc_create_enum (struct tdesc_feature *feature, const char *name,
1529 int size)
1530{
72ddacb7 1531 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_ENUM);
81516450
DE
1532
1533 gdb_assert (size > 0);
1534
81516450
DE
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. */
f5dff777 1542
ad068eab
UW
1543void
1544tdesc_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
f5dff777
DJ
1549 gdb_assert (type->kind == TDESC_TYPE_UNION
1550 || type->kind == TDESC_TYPE_STRUCT);
ad068eab
UW
1551
1552 f.name = xstrdup (field_name);
1553 f.type = field_type;
81516450
DE
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;
ad068eab
UW
1558
1559 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1560}
1561
81516450 1562/* Add a new typed bitfield to TYPE. */
f5dff777
DJ
1563
1564void
81516450
DE
1565tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1566 int start, int end, struct tdesc_type *field_type)
f5dff777
DJ
1567{
1568 struct tdesc_type_field f = { 0 };
1569
81516450
DE
1570 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1571 || type->kind == TDESC_TYPE_FLAGS);
1572 gdb_assert (start >= 0 && end >= start);
f5dff777
DJ
1573
1574 f.name = xstrdup (field_name);
1575 f.start = start;
1576 f.end = end;
81516450 1577 f.type = field_type;
f5dff777
DJ
1578
1579 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1580}
1581
81516450
DE
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
1586void
1587tdesc_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
f5dff777
DJ
1605void
1606tdesc_add_flag (struct tdesc_type *type, int start,
1607 const char *flag_name)
1608{
81516450 1609 struct tdesc_type_field f = { 0 };
f5dff777 1610
81516450
DE
1611 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1612 || type->kind == TDESC_TYPE_STRUCT);
f5dff777
DJ
1613
1614 f.name = xstrdup (flag_name);
1615 f.start = start;
81516450
DE
1616 f.end = start;
1617 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
f5dff777 1618
81516450
DE
1619 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1620}
1621
1622void
1623tdesc_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);
f5dff777
DJ
1636}
1637
123dc839
DJ
1638struct tdesc_feature *
1639tdesc_create_feature (struct target_desc *tdesc, const char *name)
1640{
72ddacb7 1641 struct tdesc_feature *new_feature = new tdesc_feature (name);
123dc839
DJ
1642
1643 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1644 return new_feature;
1645}
1646
424163ea
DJ
1647struct target_desc *
1648allocate_target_description (void)
1649{
b468ff4c 1650 return new target_desc ();
424163ea 1651}
29709017 1652
23181151
DJ
1653static void
1654free_target_description (void *arg)
1655{
19ba03f4 1656 struct target_desc *target_desc = (struct target_desc *) arg;
e35359c5 1657
b468ff4c 1658 delete target_desc;
23181151
DJ
1659}
1660
1661struct cleanup *
1662make_cleanup_free_target_description (struct target_desc *target_desc)
1663{
1664 return make_cleanup (free_target_description, target_desc);
1665}
1666
e35359c5
UW
1667void
1668tdesc_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
29709017
DJ
1691void
1692set_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
23181151
DJ
1706 new_prop.key = xstrdup (key);
1707 new_prop.value = xstrdup (value);
29709017
DJ
1708 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1709}
23181151
DJ
1710
1711void
1712set_tdesc_architecture (struct target_desc *target_desc,
1713 const struct bfd_arch_info *arch)
1714{
1715 target_desc->arch = arch;
1716}
08d16641
PA
1717
1718void
1719set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1720{
1721 target_desc->osabi = osabi;
1722}
23181151
DJ
1723\f
1724
1725static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1726static struct cmd_list_element *tdesc_unset_cmdlist;
1727
1728/* Helper functions for the CLI commands. */
1729
1730static void
1731set_tdesc_cmd (char *args, int from_tty)
1732{
635c7e8a 1733 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
23181151
DJ
1734}
1735
1736static void
1737show_tdesc_cmd (char *args, int from_tty)
1738{
1739 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1740}
1741
1742static void
1743unset_tdesc_cmd (char *args, int from_tty)
1744{
635c7e8a 1745 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
23181151
DJ
1746}
1747
1748static void
1749set_tdesc_filename_cmd (char *args, int from_tty,
1750 struct cmd_list_element *c)
1751{
6ecd4729
PA
1752 xfree (target_description_filename);
1753 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1754
23181151
DJ
1755 target_clear_description ();
1756 target_find_description ();
1757}
1758
1759static void
1760show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1761 struct cmd_list_element *c,
1762 const char *value)
1763{
6ecd4729
PA
1764 value = target_description_filename;
1765
23181151 1766 if (value != NULL && *value != '\0')
3e43a32a 1767 printf_filtered (_("The target description will be read from \"%s\".\n"),
23181151
DJ
1768 value);
1769 else
3e43a32a
MS
1770 printf_filtered (_("The target description will be "
1771 "read from the target.\n"));
23181151
DJ
1772}
1773
1774static void
1775unset_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
6eb1e6a8
YQ
1783/* Print target description in C. */
1784
1785class print_c_tdesc : public tdesc_element_visitor
1786{
1787public:
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
2035private:
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
81adfced
DJ
2042static void
2043maint_print_c_tdesc_cmd (char *args, int from_tty)
2044{
2045 const struct target_desc *tdesc;
6eb1e6a8 2046 const char *filename;
81adfced 2047
8e2141c6
YQ
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
81adfced
DJ
2064 if (tdesc == NULL)
2065 error (_("There is no target description to print."));
2066
8e2141c6 2067 if (filename == NULL)
81adfced
DJ
2068 error (_("The current target description did not come from an XML file."));
2069
6eb1e6a8
YQ
2070 std::string filename_after_features (filename);
2071 auto loc = filename_after_features.rfind ("/features/");
4e2f8df6 2072
6eb1e6a8
YQ
2073 if (loc != std::string::npos)
2074 filename_after_features = filename_after_features.substr (loc + 10);
4e2f8df6 2075
6eb1e6a8 2076 print_c_tdesc v (filename_after_features);
81adfced 2077
6eb1e6a8 2078 tdesc->accept (v);
81adfced
DJ
2079}
2080
2c0b251b
PA
2081/* Provide a prototype to silence -Wmissing-prototypes. */
2082extern initialize_file_ftype _initialize_target_descriptions;
2083
23181151
DJ
2084void
2085_initialize_target_descriptions (void)
2086{
123dc839
DJ
2087 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2088
23181151
DJ
2089 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2090Set 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, _("\
2094Show 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, _("\
2098Unset target description specific variables."),
2099 &tdesc_unset_cmdlist, "unset tdesc ",
2100 0 /* allow-unknown */, &unsetlist);
2101
2102 add_setshow_filename_cmd ("filename", class_obscure,
6ecd4729 2103 &tdesc_filename_cmd_string,
23181151
DJ
2104 _("\
2105Set the file to read for an XML target description"), _("\
2106Show the file to read for an XML target description"), _("\
2107When set, GDB will read the target description from a local\n\
2108file 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, _("\
2114Unset the file to read for an XML target description. When unset,\n\
2115GDB will read the description from the target."),
2116 &tdesc_unset_cmdlist);
81adfced
DJ
2117
2118 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2119Print the current target description as a C source file."),
2120 &maintenanceprintlist);
23181151 2121}