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