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