]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/target-descriptions.c
Return X86_TDESC_MMX in x86_get_ipa_tdesc_idx
[thirdparty/binutils-gdb.git] / gdb / target-descriptions.c
CommitLineData
424163ea
DJ
1/* Target description support for GDB.
2
61baf725 3 Copyright (C) 2006-2017 Free Software Foundation, Inc.
424163ea
DJ
4
5 Contributed by CodeSourcery.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
424163ea
DJ
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
424163ea
DJ
21
22#include "defs.h"
23#include "arch-utils.h"
23181151 24#include "gdbcmd.h"
123dc839
DJ
25#include "gdbtypes.h"
26#include "reggroups.h"
424163ea
DJ
27#include "target.h"
28#include "target-descriptions.h"
29709017 29#include "vec.h"
123dc839 30#include "xml-support.h"
23181151 31#include "xml-tdesc.h"
c3f08eb7 32#include "osabi.h"
424163ea 33
123dc839
DJ
34#include "gdb_obstack.h"
35#include "hashtab.h"
6ecd4729 36#include "inferior.h"
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
123dc839
DJ
849/* Return the type associated with ID in the context of FEATURE, or
850 NULL if none. */
851
ad068eab 852struct tdesc_type *
123dc839
DJ
853tdesc_named_type (const struct tdesc_feature *feature, const char *id)
854{
855 int ix;
ad068eab 856 struct tdesc_type *type;
123dc839
DJ
857
858 /* First try target-defined types. */
ad068eab
UW
859 for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++)
860 if (strcmp (type->name, id) == 0)
861 return type;
123dc839 862
81adfced
DJ
863 /* Next try the predefined types. */
864 for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++)
865 if (strcmp (tdesc_predefined_types[ix].name, id) == 0)
ad068eab 866 return &tdesc_predefined_types[ix];
123dc839
DJ
867
868 return NULL;
869}
ad068eab 870
9fd3625f
L
871/* Lookup type associated with ID. */
872
873struct type *
874tdesc_find_type (struct gdbarch *gdbarch, const char *id)
875{
876 struct tdesc_arch_reg *reg;
877 struct tdesc_arch_data *data;
878 int i, num_regs;
879
19ba03f4 880 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
9fd3625f
L
881 num_regs = VEC_length (tdesc_arch_reg, data->arch_regs);
882 for (i = 0; i < num_regs; i++)
883 {
884 reg = VEC_index (tdesc_arch_reg, data->arch_regs, i);
885 if (reg->reg
886 && reg->reg->tdesc_type
887 && reg->type
888 && strcmp (id, reg->reg->tdesc_type->name) == 0)
889 return reg->type;
890 }
891
892 return NULL;
893}
894
ad068eab
UW
895/* Construct, if necessary, and return the GDB type implementing target
896 type TDESC_TYPE for architecture GDBARCH. */
897
898static struct type *
899tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type)
900{
9fd3625f
L
901 struct type *type;
902
ad068eab
UW
903 switch (tdesc_type->kind)
904 {
905 /* Predefined types. */
81516450
DE
906 case TDESC_TYPE_BOOL:
907 return builtin_type (gdbarch)->builtin_bool;
908
ad068eab 909 case TDESC_TYPE_INT8:
df4df182 910 return builtin_type (gdbarch)->builtin_int8;
ad068eab
UW
911
912 case TDESC_TYPE_INT16:
df4df182 913 return builtin_type (gdbarch)->builtin_int16;
ad068eab
UW
914
915 case TDESC_TYPE_INT32:
df4df182 916 return builtin_type (gdbarch)->builtin_int32;
ad068eab
UW
917
918 case TDESC_TYPE_INT64:
df4df182 919 return builtin_type (gdbarch)->builtin_int64;
ad068eab
UW
920
921 case TDESC_TYPE_INT128:
df4df182 922 return builtin_type (gdbarch)->builtin_int128;
ad068eab
UW
923
924 case TDESC_TYPE_UINT8:
df4df182 925 return builtin_type (gdbarch)->builtin_uint8;
ad068eab
UW
926
927 case TDESC_TYPE_UINT16:
df4df182 928 return builtin_type (gdbarch)->builtin_uint16;
ad068eab
UW
929
930 case TDESC_TYPE_UINT32:
df4df182 931 return builtin_type (gdbarch)->builtin_uint32;
ad068eab
UW
932
933 case TDESC_TYPE_UINT64:
df4df182 934 return builtin_type (gdbarch)->builtin_uint64;
ad068eab
UW
935
936 case TDESC_TYPE_UINT128:
df4df182 937 return builtin_type (gdbarch)->builtin_uint128;
ad068eab
UW
938
939 case TDESC_TYPE_CODE_PTR:
940 return builtin_type (gdbarch)->builtin_func_ptr;
941
942 case TDESC_TYPE_DATA_PTR:
943 return builtin_type (gdbarch)->builtin_data_ptr;
944
9fd3625f
L
945 default:
946 break;
947 }
948
949 type = tdesc_find_type (gdbarch, tdesc_type->name);
950 if (type)
951 return type;
952
953 switch (tdesc_type->kind)
954 {
ad068eab 955 case TDESC_TYPE_IEEE_SINGLE:
e9bb382b 956 return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
27067745 957 floatformats_ieee_single);
ad068eab
UW
958
959 case TDESC_TYPE_IEEE_DOUBLE:
e9bb382b 960 return arch_float_type (gdbarch, -1, "builtin_type_ieee_double",
27067745 961 floatformats_ieee_double);
ad068eab
UW
962
963 case TDESC_TYPE_ARM_FPA_EXT:
e9bb382b 964 return arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745 965 floatformats_arm_ext);
ad068eab 966
9fd3625f
L
967 case TDESC_TYPE_I387_EXT:
968 return arch_float_type (gdbarch, -1, "builtin_type_i387_ext",
969 floatformats_i387_ext);
970
ad068eab
UW
971 /* Types defined by a target feature. */
972 case TDESC_TYPE_VECTOR:
973 {
974 struct type *type, *field_type;
975
976 field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type);
977 type = init_vector_type (field_type, tdesc_type->u.v.count);
978 TYPE_NAME (type) = xstrdup (tdesc_type->name);
979
980 return type;
981 }
982
f5dff777
DJ
983 case TDESC_TYPE_STRUCT:
984 {
985 struct type *type, *field_type;
986 struct tdesc_type_field *f;
987 int ix;
988
989 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
990 TYPE_NAME (type) = xstrdup (tdesc_type->name);
991 TYPE_TAG_NAME (type) = TYPE_NAME (type);
992
993 for (ix = 0;
994 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
995 ix++)
996 {
81516450 997 if (f->start != -1 && f->end != -1)
f5dff777
DJ
998 {
999 /* Bitfield. */
1000 struct field *fld;
1001 struct type *field_type;
1002 int bitsize, total_size;
1003
81516450 1004 /* This invariant should be preserved while creating types. */
f5dff777 1005 gdb_assert (tdesc_type->u.u.size != 0);
81516450
DE
1006 if (f->type != NULL)
1007 field_type = tdesc_gdb_type (gdbarch, f->type);
1008 else if (tdesc_type->u.u.size > 4)
f5dff777
DJ
1009 field_type = builtin_type (gdbarch)->builtin_uint64;
1010 else
1011 field_type = builtin_type (gdbarch)->builtin_uint32;
1012
1013 fld = append_composite_type_field_raw (type, xstrdup (f->name),
1014 field_type);
1015
1016 /* For little-endian, BITPOS counts from the LSB of
1017 the structure and marks the LSB of the field. For
1018 big-endian, BITPOS counts from the MSB of the
1019 structure and marks the MSB of the field. Either
1020 way, it is the number of bits to the "left" of the
1021 field. To calculate this in big-endian, we need
1022 the total size of the structure. */
1023 bitsize = f->end - f->start + 1;
1024 total_size = tdesc_type->u.u.size * TARGET_CHAR_BIT;
1025 if (gdbarch_bits_big_endian (gdbarch))
f41f5e61 1026 SET_FIELD_BITPOS (fld[0], total_size - f->start - bitsize);
f5dff777 1027 else
f41f5e61 1028 SET_FIELD_BITPOS (fld[0], f->start);
f5dff777
DJ
1029 FIELD_BITSIZE (fld[0]) = bitsize;
1030 }
1031 else
1032 {
81516450 1033 gdb_assert (f->start == -1 && f->end == -1);
f5dff777
DJ
1034 field_type = tdesc_gdb_type (gdbarch, f->type);
1035 append_composite_type_field (type, xstrdup (f->name),
1036 field_type);
1037 }
1038 }
1039
1040 if (tdesc_type->u.u.size != 0)
1041 TYPE_LENGTH (type) = tdesc_type->u.u.size;
1042 return type;
1043 }
1044
ad068eab
UW
1045 case TDESC_TYPE_UNION:
1046 {
1047 struct type *type, *field_type;
1048 struct tdesc_type_field *f;
1049 int ix;
1050
e9bb382b 1051 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
ad068eab
UW
1052 TYPE_NAME (type) = xstrdup (tdesc_type->name);
1053
1054 for (ix = 0;
1055 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1056 ix++)
1057 {
1058 field_type = tdesc_gdb_type (gdbarch, f->type);
1059 append_composite_type_field (type, xstrdup (f->name), field_type);
1060
f5dff777 1061 /* If any of the children of a union are vectors, flag the
ad068eab
UW
1062 union as a vector also. This allows e.g. a union of two
1063 vector types to show up automatically in "info vector". */
1064 if (TYPE_VECTOR (field_type))
1065 TYPE_VECTOR (type) = 1;
1066 }
f5dff777
DJ
1067 return type;
1068 }
1069
1070 case TDESC_TYPE_FLAGS:
1071 {
81516450 1072 struct tdesc_type_field *f;
f5dff777
DJ
1073 int ix;
1074
3494b66d 1075 type = arch_flags_type (gdbarch, tdesc_type->name,
81516450
DE
1076 tdesc_type->u.u.size);
1077 for (ix = 0;
1078 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
1079 ix++)
1080 {
1081 struct type *field_type;
1082 int bitsize = f->end - f->start + 1;
1083
1084 gdb_assert (f->type != NULL);
1085 field_type = tdesc_gdb_type (gdbarch, f->type);
1086 append_flags_type_field (type, f->start, bitsize,
1087 field_type, f->name);
1088 }
1089
1090 return type;
1091 }
1092
1093 case TDESC_TYPE_ENUM:
1094 {
1095 struct tdesc_type_field *f;
1096 int ix;
1097
1098 type = arch_type (gdbarch, TYPE_CODE_ENUM,
1099 tdesc_type->u.u.size, tdesc_type->name);
1100 TYPE_UNSIGNED (type) = 1;
f5dff777 1101 for (ix = 0;
81516450 1102 VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f);
f5dff777 1103 ix++)
81516450
DE
1104 {
1105 struct field *fld
1106 = append_composite_type_field_raw (type, xstrdup (f->name),
1107 NULL);
1108
1109 SET_FIELD_BITPOS (fld[0], f->start);
1110 }
ad068eab
UW
1111
1112 return type;
1113 }
1114 }
1115
1116 internal_error (__FILE__, __LINE__,
1117 "Type \"%s\" has an unknown kind %d",
1118 tdesc_type->name, tdesc_type->kind);
1119}
123dc839
DJ
1120\f
1121
1122/* Support for registers from target descriptions. */
1123
1124/* Construct the per-gdbarch data. */
1125
1126static void *
1127tdesc_data_init (struct obstack *obstack)
1128{
1129 struct tdesc_arch_data *data;
1130
1131 data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data);
1132 return data;
1133}
1134
1135/* Similar, but for the temporary copy used during architecture
1136 initialization. */
1137
1138struct tdesc_arch_data *
1139tdesc_data_alloc (void)
1140{
41bf6aca 1141 return XCNEW (struct tdesc_arch_data);
123dc839
DJ
1142}
1143
1144/* Free something allocated by tdesc_data_alloc, if it is not going
1145 to be used (for instance if it was unsuitable for the
1146 architecture). */
1147
1148void
1149tdesc_data_cleanup (void *data_untyped)
1150{
19ba03f4 1151 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
123dc839 1152
ad068eab 1153 VEC_free (tdesc_arch_reg, data->arch_regs);
123dc839
DJ
1154 xfree (data);
1155}
1156
1157/* Search FEATURE for a register named NAME. */
1158
7cc46491
DJ
1159static struct tdesc_reg *
1160tdesc_find_register_early (const struct tdesc_feature *feature,
1161 const char *name)
123dc839
DJ
1162{
1163 int ixr;
1164 struct tdesc_reg *reg;
1165
1166 for (ixr = 0;
1167 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1168 ixr++)
1169 if (strcasecmp (reg->name, name) == 0)
7cc46491 1170 return reg;
123dc839 1171
7cc46491
DJ
1172 return NULL;
1173}
1174
1175/* Search FEATURE for a register named NAME. Assign REGNO to it. */
1176
1177int
1178tdesc_numbered_register (const struct tdesc_feature *feature,
1179 struct tdesc_arch_data *data,
1180 int regno, const char *name)
1181{
ad068eab 1182 struct tdesc_arch_reg arch_reg = { 0 };
7cc46491
DJ
1183 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1184
1185 if (reg == NULL)
1186 return 0;
1187
1188 /* Make sure the vector includes a REGNO'th element. */
ad068eab
UW
1189 while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs))
1190 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg);
1191
1192 arch_reg.reg = reg;
1193 VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg);
7cc46491 1194 return 1;
123dc839
DJ
1195}
1196
58d6951d
DJ
1197/* Search FEATURE for a register named NAME, but do not assign a fixed
1198 register number to it. */
1199
1200int
1201tdesc_unnumbered_register (const struct tdesc_feature *feature,
1202 const char *name)
1203{
1204 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1205
1206 if (reg == NULL)
1207 return 0;
1208
1209 return 1;
1210}
1211
7cc46491
DJ
1212/* Search FEATURE for a register whose name is in NAMES and assign
1213 REGNO to it. */
123dc839
DJ
1214
1215int
1216tdesc_numbered_register_choices (const struct tdesc_feature *feature,
1217 struct tdesc_arch_data *data,
1218 int regno, const char *const names[])
1219{
1220 int i;
1221
1222 for (i = 0; names[i] != NULL; i++)
1223 if (tdesc_numbered_register (feature, data, regno, names[i]))
1224 return 1;
1225
1226 return 0;
1227}
1228
7cc46491
DJ
1229/* Search FEATURE for a register named NAME, and return its size in
1230 bits. The register must exist. */
1231
1232int
1233tdesc_register_size (const struct tdesc_feature *feature,
1234 const char *name)
1235{
1236 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
1237
1238 gdb_assert (reg != NULL);
1239 return reg->bitsize;
1240}
1241
123dc839
DJ
1242/* Look up a register by its GDB internal register number. */
1243
ad068eab
UW
1244static struct tdesc_arch_reg *
1245tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
123dc839 1246{
123dc839
DJ
1247 struct tdesc_arch_data *data;
1248
19ba03f4 1249 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
ad068eab
UW
1250 if (regno < VEC_length (tdesc_arch_reg, data->arch_regs))
1251 return VEC_index (tdesc_arch_reg, data->arch_regs, regno);
123dc839
DJ
1252 else
1253 return NULL;
1254}
1255
ad068eab
UW
1256static struct tdesc_reg *
1257tdesc_find_register (struct gdbarch *gdbarch, int regno)
1258{
1259 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
5d502164 1260
ad068eab
UW
1261 return reg? reg->reg : NULL;
1262}
1263
f8b73d13
DJ
1264/* Return the name of register REGNO, from the target description or
1265 from an architecture-provided pseudo_register_name method. */
1266
1267const char *
d93859e2 1268tdesc_register_name (struct gdbarch *gdbarch, int regno)
123dc839 1269{
d93859e2
UW
1270 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1271 int num_regs = gdbarch_num_regs (gdbarch);
1272 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
123dc839
DJ
1273
1274 if (reg != NULL)
1275 return reg->name;
1276
1277 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1278 {
19ba03f4
SM
1279 struct tdesc_arch_data *data
1280 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1281
123dc839 1282 gdb_assert (data->pseudo_register_name != NULL);
d93859e2 1283 return data->pseudo_register_name (gdbarch, regno);
123dc839
DJ
1284 }
1285
1286 return "";
1287}
1288
58d6951d 1289struct type *
123dc839
DJ
1290tdesc_register_type (struct gdbarch *gdbarch, int regno)
1291{
ad068eab
UW
1292 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
1293 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
123dc839
DJ
1294 int num_regs = gdbarch_num_regs (gdbarch);
1295 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1296
1297 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
1298 {
19ba03f4
SM
1299 struct tdesc_arch_data *data
1300 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1301
123dc839
DJ
1302 gdb_assert (data->pseudo_register_type != NULL);
1303 return data->pseudo_register_type (gdbarch, regno);
1304 }
1305
1306 if (reg == NULL)
1307 /* Return "int0_t", since "void" has a misleading size of one. */
df4df182 1308 return builtin_type (gdbarch)->builtin_int0;
123dc839 1309
ad068eab 1310 if (arch_reg->type == NULL)
123dc839 1311 {
ad068eab
UW
1312 /* First check for a predefined or target defined type. */
1313 if (reg->tdesc_type)
1314 arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type);
1315
1316 /* Next try size-sensitive type shortcuts. */
1317 else if (strcmp (reg->type, "float") == 0)
1318 {
1319 if (reg->bitsize == gdbarch_float_bit (gdbarch))
1320 arch_reg->type = builtin_type (gdbarch)->builtin_float;
1321 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
1322 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1323 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
1324 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
1325 else
1326 {
1327 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1328 reg->name, reg->bitsize);
1329 arch_reg->type = builtin_type (gdbarch)->builtin_double;
1330 }
1331 }
1332 else if (strcmp (reg->type, "int") == 0)
1333 {
1334 if (reg->bitsize == gdbarch_long_bit (gdbarch))
1335 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1336 else if (reg->bitsize == TARGET_CHAR_BIT)
1337 arch_reg->type = builtin_type (gdbarch)->builtin_char;
1338 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
1339 arch_reg->type = builtin_type (gdbarch)->builtin_short;
1340 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
1341 arch_reg->type = builtin_type (gdbarch)->builtin_int;
1342 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
1343 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
1344 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
c378eb4e 1345 /* A bit desperate by this point... */
ad068eab
UW
1346 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
1347 else
1348 {
1349 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
1350 reg->name, reg->bitsize);
1351 arch_reg->type = builtin_type (gdbarch)->builtin_long;
1352 }
1353 }
1354
1355 if (arch_reg->type == NULL)
1356 internal_error (__FILE__, __LINE__,
1357 "Register \"%s\" has an unknown type \"%s\"",
1358 reg->name, reg->type);
123dc839 1359 }
123dc839 1360
ad068eab 1361 return arch_reg->type;
123dc839
DJ
1362}
1363
1364static int
1365tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
1366{
1367 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1368
1369 if (reg != NULL)
1370 return reg->target_regnum;
1371 else
1372 return -1;
1373}
1374
1375/* Check whether REGNUM is a member of REGGROUP. Registers from the
1376 target description may be classified as general, float, or vector.
f8b73d13
DJ
1377 Unlike a gdbarch register_reggroup_p method, this function will
1378 return -1 if it does not know; the caller should handle registers
1379 with no specified group.
123dc839
DJ
1380
1381 Arbitrary strings (other than "general", "float", and "vector")
1382 from the description are not used; they cause the register to be
1383 displayed in "info all-registers" but excluded from "info
1384 registers" et al. The names of containing features are also not
1385 used. This might be extended to display registers in some more
1386 useful groupings.
1387
1388 The save-restore flag is also implemented here. */
1389
f8b73d13
DJ
1390int
1391tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1392 struct reggroup *reggroup)
123dc839 1393{
123dc839
DJ
1394 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1395
123dc839
DJ
1396 if (reg != NULL && reg->group != NULL)
1397 {
1398 int general_p = 0, float_p = 0, vector_p = 0;
1399
1400 if (strcmp (reg->group, "general") == 0)
1401 general_p = 1;
1402 else if (strcmp (reg->group, "float") == 0)
1403 float_p = 1;
1404 else if (strcmp (reg->group, "vector") == 0)
1405 vector_p = 1;
1406
1407 if (reggroup == float_reggroup)
1408 return float_p;
1409
1410 if (reggroup == vector_reggroup)
1411 return vector_p;
1412
1413 if (reggroup == general_reggroup)
1414 return general_p;
1415 }
1416
1417 if (reg != NULL
1418 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1419 return reg->save_restore;
1420
f8b73d13
DJ
1421 return -1;
1422}
1423
1424/* Check whether REGNUM is a member of REGGROUP. Registers with no
1425 group specified go to the default reggroup function and are handled
1426 by type. */
1427
1428static int
1429tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1430 struct reggroup *reggroup)
1431{
1432 int num_regs = gdbarch_num_regs (gdbarch);
1433 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1434 int ret;
1435
1436 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1437 {
19ba03f4
SM
1438 struct tdesc_arch_data *data
1439 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1440
58d6951d
DJ
1441 if (data->pseudo_register_reggroup_p != NULL)
1442 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1443 /* Otherwise fall through to the default reggroup_p. */
f8b73d13
DJ
1444 }
1445
1446 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1447 if (ret != -1)
1448 return ret;
1449
123dc839
DJ
1450 return default_register_reggroup_p (gdbarch, regno, reggroup);
1451}
1452
1453/* Record architecture-specific functions to call for pseudo-register
1454 support. */
1455
1456void
1457set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1458 gdbarch_register_name_ftype *pseudo_name)
1459{
19ba03f4
SM
1460 struct tdesc_arch_data *data
1461 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1462
1463 data->pseudo_register_name = pseudo_name;
1464}
1465
1466void
1467set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1468 gdbarch_register_type_ftype *pseudo_type)
1469{
19ba03f4
SM
1470 struct tdesc_arch_data *data
1471 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1472
1473 data->pseudo_register_type = pseudo_type;
1474}
1475
1476void
1477set_tdesc_pseudo_register_reggroup_p
1478 (struct gdbarch *gdbarch,
1479 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1480{
19ba03f4
SM
1481 struct tdesc_arch_data *data
1482 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1483
1484 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1485}
1486
1487/* Update GDBARCH to use the target description for registers. */
1488
1489void
1490tdesc_use_registers (struct gdbarch *gdbarch,
7cc46491 1491 const struct target_desc *target_desc,
123dc839
DJ
1492 struct tdesc_arch_data *early_data)
1493{
1494 int num_regs = gdbarch_num_regs (gdbarch);
c6913b7d 1495 int ixf, ixr;
123dc839
DJ
1496 struct tdesc_feature *feature;
1497 struct tdesc_reg *reg;
1498 struct tdesc_arch_data *data;
ad068eab 1499 struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 };
123dc839
DJ
1500 htab_t reg_hash;
1501
123dc839
DJ
1502 /* We can't use the description for registers if it doesn't describe
1503 any. This function should only be called after validating
1504 registers, so the caller should know that registers are
1505 included. */
1506 gdb_assert (tdesc_has_registers (target_desc));
1507
19ba03f4 1508 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
ad068eab 1509 data->arch_regs = early_data->arch_regs;
123dc839
DJ
1510 xfree (early_data);
1511
1512 /* Build up a set of all registers, so that we can assign register
1513 numbers where needed. The hash table expands as necessary, so
1514 the initial size is arbitrary. */
1515 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1516 for (ixf = 0;
1517 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1518 ixf++)
1519 for (ixr = 0;
1520 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1521 ixr++)
1522 {
1523 void **slot = htab_find_slot (reg_hash, reg, INSERT);
1524
1525 *slot = reg;
1526 }
1527
1528 /* Remove any registers which were assigned numbers by the
1529 architecture. */
ad068eab
UW
1530 for (ixr = 0;
1531 VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg);
1532 ixr++)
1533 if (arch_reg->reg)
1534 htab_remove_elt (reg_hash, arch_reg->reg);
123dc839
DJ
1535
1536 /* Assign numbers to the remaining registers and add them to the
f57d151a 1537 list of registers. The new numbers are always above gdbarch_num_regs.
123dc839
DJ
1538 Iterate over the features, not the hash table, so that the order
1539 matches that in the target description. */
1540
ad068eab
UW
1541 gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs);
1542 while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs)
1543 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
123dc839
DJ
1544 for (ixf = 0;
1545 VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature);
1546 ixf++)
1547 for (ixr = 0;
1548 VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg);
1549 ixr++)
1550 if (htab_find (reg_hash, reg) != NULL)
1551 {
ad068eab
UW
1552 new_arch_reg.reg = reg;
1553 VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg);
123dc839
DJ
1554 num_regs++;
1555 }
1556
1557 htab_delete (reg_hash);
1558
1559 /* Update the architecture. */
1560 set_gdbarch_num_regs (gdbarch, num_regs);
1561 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1562 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1563 set_gdbarch_remote_register_number (gdbarch,
1564 tdesc_remote_register_number);
1565 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1566}
1567\f
1568
123dc839
DJ
1569void
1570tdesc_create_reg (struct tdesc_feature *feature, const char *name,
1571 int regnum, int save_restore, const char *group,
1572 int bitsize, const char *type)
1573{
72ddacb7
YQ
1574 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore,
1575 group, bitsize, type);
123dc839
DJ
1576
1577 VEC_safe_push (tdesc_reg_p, feature->registers, reg);
1578}
1579
ad068eab
UW
1580struct tdesc_type *
1581tdesc_create_vector (struct tdesc_feature *feature, const char *name,
1582 struct tdesc_type *field_type, int count)
1583{
72ddacb7 1584 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_VECTOR);
ad068eab 1585
ad068eab
UW
1586 type->u.v.type = field_type;
1587 type->u.v.count = count;
1588
1589 VEC_safe_push (tdesc_type_p, feature->types, type);
1590 return type;
1591}
1592
f5dff777
DJ
1593struct tdesc_type *
1594tdesc_create_struct (struct tdesc_feature *feature, const char *name)
1595{
72ddacb7 1596 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_STRUCT);
f5dff777
DJ
1597
1598 VEC_safe_push (tdesc_type_p, feature->types, type);
1599 return type;
1600}
1601
1602/* Set the total length of TYPE. Structs which contain bitfields may
1603 omit the reserved bits, so the end of the last field may not
1604 suffice. */
1605
1606void
54157a25 1607tdesc_set_struct_size (struct tdesc_type *type, int size)
f5dff777
DJ
1608{
1609 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
54157a25 1610 gdb_assert (size > 0);
f5dff777
DJ
1611 type->u.u.size = size;
1612}
1613
ad068eab
UW
1614struct tdesc_type *
1615tdesc_create_union (struct tdesc_feature *feature, const char *name)
1616{
72ddacb7 1617 struct tdesc_type *type = new tdesc_type (name, TDESC_TYPE_UNION);
ad068eab
UW
1618
1619 VEC_safe_push (tdesc_type_p, feature->types, type);
1620 return type;
1621}
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
1651/* Add a new field to TYPE. */
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
81516450 1672/* Add a new typed bitfield to TYPE. */
f5dff777
DJ
1673
1674void
81516450
DE
1675tdesc_add_typed_bitfield (struct tdesc_type *type, const char *field_name,
1676 int start, int end, struct tdesc_type *field_type)
f5dff777
DJ
1677{
1678 struct tdesc_type_field f = { 0 };
1679
81516450
DE
1680 gdb_assert (type->kind == TDESC_TYPE_STRUCT
1681 || type->kind == TDESC_TYPE_FLAGS);
1682 gdb_assert (start >= 0 && end >= start);
f5dff777
DJ
1683
1684 f.name = xstrdup (field_name);
1685 f.start = start;
1686 f.end = end;
81516450 1687 f.type = field_type;
f5dff777
DJ
1688
1689 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1690}
1691
81516450
DE
1692/* Add a new untyped bitfield to TYPE.
1693 Untyped bitfields become either uint32 or uint64 depending on the size
1694 of the underlying type. */
1695
1696void
1697tdesc_add_bitfield (struct tdesc_type *type, const char *field_name,
1698 int start, int end)
1699{
1700 struct tdesc_type *field_type;
1701
1702 gdb_assert (start >= 0 && end >= start);
1703
1704 if (type->u.u.size > 4)
1705 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64);
1706 else
1707 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32);
1708
1709 tdesc_add_typed_bitfield (type, field_name, start, end, field_type);
1710}
1711
1712/* A flag is just a typed(bool) single-bit bitfield.
1713 This function is kept to minimize changes in generated files. */
1714
f5dff777
DJ
1715void
1716tdesc_add_flag (struct tdesc_type *type, int start,
1717 const char *flag_name)
1718{
81516450 1719 struct tdesc_type_field f = { 0 };
f5dff777 1720
81516450
DE
1721 gdb_assert (type->kind == TDESC_TYPE_FLAGS
1722 || type->kind == TDESC_TYPE_STRUCT);
f5dff777
DJ
1723
1724 f.name = xstrdup (flag_name);
1725 f.start = start;
81516450
DE
1726 f.end = start;
1727 f.type = tdesc_predefined_type (TDESC_TYPE_BOOL);
f5dff777 1728
81516450
DE
1729 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
1730}
1731
1732void
1733tdesc_add_enum_value (struct tdesc_type *type, int value,
1734 const char *name)
1735{
1736 struct tdesc_type_field f = { 0 };
1737
1738 gdb_assert (type->kind == TDESC_TYPE_ENUM);
1739
1740 f.name = xstrdup (name);
1741 f.start = value;
1742 f.end = -1;
1743 f.type = tdesc_predefined_type (TDESC_TYPE_INT32);
1744
1745 VEC_safe_push (tdesc_type_field, type->u.u.fields, &f);
f5dff777
DJ
1746}
1747
123dc839
DJ
1748struct tdesc_feature *
1749tdesc_create_feature (struct target_desc *tdesc, const char *name)
1750{
72ddacb7 1751 struct tdesc_feature *new_feature = new tdesc_feature (name);
123dc839
DJ
1752
1753 VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature);
1754 return new_feature;
1755}
1756
424163ea
DJ
1757struct target_desc *
1758allocate_target_description (void)
1759{
b468ff4c 1760 return new target_desc ();
424163ea 1761}
29709017 1762
23181151
DJ
1763static void
1764free_target_description (void *arg)
1765{
19ba03f4 1766 struct target_desc *target_desc = (struct target_desc *) arg;
e35359c5 1767
b468ff4c 1768 delete target_desc;
23181151
DJ
1769}
1770
1771struct cleanup *
1772make_cleanup_free_target_description (struct target_desc *target_desc)
1773{
1774 return make_cleanup (free_target_description, target_desc);
1775}
1776
e35359c5
UW
1777void
1778tdesc_add_compatible (struct target_desc *target_desc,
1779 const struct bfd_arch_info *compatible)
1780{
1781 const struct bfd_arch_info *compat;
1782 int ix;
1783
1784 /* If this instance of GDB is compiled without BFD support for the
1785 compatible architecture, simply ignore it -- we would not be able
1786 to handle it anyway. */
1787 if (compatible == NULL)
1788 return;
1789
1790 for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat);
1791 ix++)
1792 if (compat == compatible)
1793 internal_error (__FILE__, __LINE__,
1794 _("Attempted to add duplicate "
1795 "compatible architecture \"%s\""),
1796 compatible->printable_name);
1797
1798 VEC_safe_push (arch_p, target_desc->compatible, compatible);
1799}
1800
29709017
DJ
1801void
1802set_tdesc_property (struct target_desc *target_desc,
1803 const char *key, const char *value)
1804{
1805 struct property *prop, new_prop;
1806 int ix;
1807
1808 gdb_assert (key != NULL && value != NULL);
1809
1810 for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
1811 ix++)
1812 if (strcmp (prop->key, key) == 0)
1813 internal_error (__FILE__, __LINE__,
1814 _("Attempted to add duplicate property \"%s\""), key);
1815
23181151
DJ
1816 new_prop.key = xstrdup (key);
1817 new_prop.value = xstrdup (value);
29709017
DJ
1818 VEC_safe_push (property_s, target_desc->properties, &new_prop);
1819}
23181151
DJ
1820
1821void
1822set_tdesc_architecture (struct target_desc *target_desc,
1823 const struct bfd_arch_info *arch)
1824{
1825 target_desc->arch = arch;
1826}
08d16641
PA
1827
1828void
1829set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1830{
1831 target_desc->osabi = osabi;
1832}
23181151
DJ
1833\f
1834
1835static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1836static struct cmd_list_element *tdesc_unset_cmdlist;
1837
1838/* Helper functions for the CLI commands. */
1839
1840static void
1841set_tdesc_cmd (char *args, int from_tty)
1842{
635c7e8a 1843 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
23181151
DJ
1844}
1845
1846static void
1847show_tdesc_cmd (char *args, int from_tty)
1848{
1849 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1850}
1851
1852static void
1853unset_tdesc_cmd (char *args, int from_tty)
1854{
635c7e8a 1855 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
23181151
DJ
1856}
1857
1858static void
1859set_tdesc_filename_cmd (char *args, int from_tty,
1860 struct cmd_list_element *c)
1861{
6ecd4729
PA
1862 xfree (target_description_filename);
1863 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1864
23181151
DJ
1865 target_clear_description ();
1866 target_find_description ();
1867}
1868
1869static void
1870show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1871 struct cmd_list_element *c,
1872 const char *value)
1873{
6ecd4729
PA
1874 value = target_description_filename;
1875
23181151 1876 if (value != NULL && *value != '\0')
3e43a32a 1877 printf_filtered (_("The target description will be read from \"%s\".\n"),
23181151
DJ
1878 value);
1879 else
3e43a32a
MS
1880 printf_filtered (_("The target description will be "
1881 "read from the target.\n"));
23181151
DJ
1882}
1883
1884static void
1885unset_tdesc_filename_cmd (char *args, int from_tty)
1886{
1887 xfree (target_description_filename);
1888 target_description_filename = NULL;
1889 target_clear_description ();
1890 target_find_description ();
1891}
1892
6eb1e6a8
YQ
1893/* Print target description in C. */
1894
1895class print_c_tdesc : public tdesc_element_visitor
1896{
1897public:
1898 print_c_tdesc (std::string &filename_after_features)
1899 : m_filename_after_features (filename_after_features)
1900 {
1901 const char *inp;
1902 char *outp;
1903 const char *filename = lbasename (m_filename_after_features.c_str ());
1904
1905 m_function = (char *) xmalloc (strlen (filename) + 1);
1906 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1907 if (*inp == '.')
1908 break;
1909 else if (*inp == '-')
1910 *outp++ = '_';
1911 else
1912 *outp++ = *inp;
1913 *outp = '\0';
1914
1915 /* Standard boilerplate. */
1916 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1917 "-*- buffer-read-only: t -*- vi"
1918 ":set ro:\n");
6eb1e6a8
YQ
1919 }
1920
1921 ~print_c_tdesc ()
1922 {
1923 xfree (m_function);
1924 }
1925
1926 void visit_pre (const target_desc *e) override
1927 {
25aa13e5
YQ
1928 printf_unfiltered (" Original: %s */\n\n",
1929 lbasename (m_filename_after_features.c_str ()));
1930
6eb1e6a8
YQ
1931 printf_unfiltered ("#include \"defs.h\"\n");
1932 printf_unfiltered ("#include \"osabi.h\"\n");
1933 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1934 printf_unfiltered ("\n");
1935
1936 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1937 printf_unfiltered ("static void\n");
1938 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1939 printf_unfiltered ("{\n");
1940 printf_unfiltered
1941 (" struct target_desc *result = allocate_target_description ();\n");
1942
1943 if (tdesc_architecture (e) != NULL)
1944 {
1945 printf_unfiltered
1946 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1947 tdesc_architecture (e)->printable_name);
1948 printf_unfiltered ("\n");
1949 }
1950 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1951 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1952 {
1953 printf_unfiltered
1954 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1955 gdbarch_osabi_name (tdesc_osabi (e)));
1956 printf_unfiltered ("\n");
1957 }
1958
1959 int ix;
1960 const struct bfd_arch_info *compatible;
1961 struct property *prop;
1962
1963 for (ix = 0; VEC_iterate (arch_p, e->compatible, ix, compatible);
1964 ix++)
1965 {
1966 printf_unfiltered
1967 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1968 compatible->printable_name);
1969 }
1970
1971 if (ix)
1972 printf_unfiltered ("\n");
1973
1974 for (ix = 0; VEC_iterate (property_s, e->properties, ix, prop);
1975 ix++)
1976 {
1977 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1978 prop->key, prop->value);
1979 }
1980 printf_unfiltered (" struct tdesc_feature *feature;\n");
1981 }
1982
25aa13e5 1983 void visit_pre (const tdesc_feature *e) override
6eb1e6a8
YQ
1984 {
1985 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1986 e->name);
1987 }
1988
25aa13e5
YQ
1989 void visit_post (const tdesc_feature *e) override
1990 {}
1991
6eb1e6a8
YQ
1992 void visit_post (const target_desc *e) override
1993 {
1994 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1995 printf_unfiltered ("}\n");
1996 }
1997
1998 void visit (const tdesc_type *type) override
1999 {
2000 struct tdesc_type_field *f;
2001
2002 /* Now we do some "filtering" in order to know which variables to
2003 declare. This is needed because otherwise we would declare unused
2004 variables `field_type' and `type'. */
2005 if (!m_printed_field_type)
2006 {
2007 printf_unfiltered (" struct tdesc_type *field_type;\n");
2008 m_printed_field_type = true;
2009 }
2010
2011 if ((type->kind == TDESC_TYPE_UNION
2012 || type->kind == TDESC_TYPE_STRUCT
2013 || type->kind == TDESC_TYPE_FLAGS
2014 || type->kind == TDESC_TYPE_ENUM)
2015 && VEC_length (tdesc_type_field, type->u.u.fields) > 0
2016 && !m_printed_type)
2017 {
2018 printf_unfiltered (" struct tdesc_type *type;\n");
2019 m_printed_type = true;
2020 }
2021
2022 switch (type->kind)
2023 {
2024 case TDESC_TYPE_VECTOR:
2025 printf_unfiltered
2026 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2027 type->u.v.type->name);
2028 printf_unfiltered
2029 (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n",
2030 type->name, type->u.v.count);
2031 break;
2032 case TDESC_TYPE_STRUCT:
2033 case TDESC_TYPE_FLAGS:
2034 if (type->kind == TDESC_TYPE_STRUCT)
2035 {
2036 printf_unfiltered
2037 (" type = tdesc_create_struct (feature, \"%s\");\n",
2038 type->name);
2039 if (type->u.u.size != 0)
2040 printf_unfiltered
2041 (" tdesc_set_struct_size (type, %d);\n",
2042 type->u.u.size);
2043 }
2044 else
2045 {
2046 printf_unfiltered
2047 (" type = tdesc_create_flags (feature, \"%s\", %d);\n",
2048 type->name, type->u.u.size);
2049 }
2050 for (int ix3 = 0;
2051 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2052 ix3++)
2053 {
2054 const char *type_name;
2055
2056 gdb_assert (f->type != NULL);
2057 type_name = f->type->name;
2058
2059 /* To minimize changes to generated files, don't emit type
2060 info for fields that have defaulted types. */
2061 if (f->start != -1)
2062 {
2063 gdb_assert (f->end != -1);
2064 if (f->type->kind == TDESC_TYPE_BOOL)
2065 {
2066 gdb_assert (f->start == f->end);
2067 printf_unfiltered
2068 (" tdesc_add_flag (type, %d, \"%s\");\n",
2069 f->start, f->name);
2070 }
2071 else if ((type->u.u.size == 4
2072 && f->type->kind == TDESC_TYPE_UINT32)
2073 || (type->u.u.size == 8
2074 && f->type->kind == TDESC_TYPE_UINT64))
2075 {
2076 printf_unfiltered
2077 (" tdesc_add_bitfield (type, \"%s\", %d, %d);\n",
2078 f->name, f->start, f->end);
2079 }
2080 else
2081 {
2082 printf_unfiltered
2083 (" field_type = tdesc_named_type (feature,"
2084 " \"%s\");\n",
2085 type_name);
2086 printf_unfiltered
2087 (" tdesc_add_typed_bitfield (type, \"%s\","
2088 " %d, %d, field_type);\n",
2089 f->name, f->start, f->end);
2090 }
2091 }
2092 else /* Not a bitfield. */
2093 {
2094 gdb_assert (f->end == -1);
2095 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
2096 printf_unfiltered
2097 (" field_type = tdesc_named_type (feature,"
2098 " \"%s\");\n",
2099 type_name);
2100 printf_unfiltered
2101 (" tdesc_add_field (type, \"%s\", field_type);\n",
2102 f->name);
2103 }
2104 }
2105 break;
2106 case TDESC_TYPE_UNION:
2107 printf_unfiltered
2108 (" type = tdesc_create_union (feature, \"%s\");\n",
2109 type->name);
2110 for (int ix3 = 0;
2111 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2112 ix3++)
2113 {
2114 printf_unfiltered
2115 (" field_type = tdesc_named_type (feature, \"%s\");\n",
2116 f->type->name);
2117 printf_unfiltered
2118 (" tdesc_add_field (type, \"%s\", field_type);\n",
2119 f->name);
2120 }
2121 break;
2122 case TDESC_TYPE_ENUM:
2123 printf_unfiltered
2124 (" type = tdesc_create_enum (feature, \"%s\", %d);\n",
2125 type->name, type->u.u.size);
2126 for (int ix3 = 0;
2127 VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f);
2128 ix3++)
2129 printf_unfiltered
2130 (" tdesc_add_enum_value (type, %d, \"%s\");\n",
2131 f->start, f->name);
2132 break;
2133 default:
2134 error (_("C output is not supported type \"%s\"."), type->name);
2135 }
2136 printf_unfiltered ("\n");
2137 }
2138
2139 void visit (const tdesc_reg *reg) override
2140 {
2141 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
2142 reg->name, reg->target_regnum, reg->save_restore);
2143 if (reg->group)
2144 printf_unfiltered ("\"%s\", ", reg->group);
2145 else
2146 printf_unfiltered ("NULL, ");
2147 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
2148 }
2149
25aa13e5
YQ
2150protected:
2151 std::string m_filename_after_features;
2152
6eb1e6a8
YQ
2153private:
2154 char *m_function;
6eb1e6a8
YQ
2155 bool m_printed_field_type = false;
2156 bool m_printed_type = false;
2157};
2158
25aa13e5
YQ
2159/* Print target description feature in C. */
2160
2161class print_c_feature : public print_c_tdesc
2162{
2163public:
2164 print_c_feature (std::string &file)
2165 : print_c_tdesc (file)
2166 {
2167 /* Trim ".tmp". */
2168 auto const pos = m_filename_after_features.find_last_of ('.');
2169
2170 m_filename_after_features = m_filename_after_features.substr (0, pos);
2171 }
2172
2173 void visit_pre (const target_desc *e) override
2174 {
2175 printf_unfiltered (" Original: %s */\n\n",
2176 lbasename (m_filename_after_features.c_str ()));
2177
2178 printf_unfiltered ("#include \"target-descriptions.h\"\n");
2179 printf_unfiltered ("\n");
2180 }
2181
2182 void visit_post (const target_desc *e) override
2183 {}
2184
2185 void visit_pre (const tdesc_feature *e) override
2186 {
2187 std::string name (m_filename_after_features);
2188
2189 auto pos = name.find_first_of ('.');
2190
2191 name = name.substr (0, pos);
2192 std::replace (name.begin (), name.end (), '/', '_');
2193 std::replace (name.begin (), name.end (), '-', '_');
2194
2195 printf_unfiltered ("static int\n");
2196 printf_unfiltered ("create_feature_%s ", name.c_str ());
2197 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
2198
2199 printf_unfiltered ("{\n");
2200 printf_unfiltered (" struct tdesc_feature *feature;\n");
2201 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
2202 e->name);
2203 }
2204
2205 void visit_post (const tdesc_feature *e) override
2206 {
2207 printf_unfiltered (" return regnum;\n");
2208 printf_unfiltered ("}\n");
2209 }
2210
2211 void visit (const tdesc_reg *reg) override
2212 {
ea03d0d3
YQ
2213 /* Most "reg" in XML target descriptions don't have "regnum"
2214 attribute, so the register number is allocated sequentially.
2215 In case that reg has "regnum" attribute, register number
2216 should be set by that explicitly. */
2217
2218 if (reg->target_regnum < m_next_regnum)
2219 {
2220 /* The integrity check, it can catch some errors on register
2221 number collision, like this,
2222
2223 <reg name="x0" bitsize="32"/>
2224 <reg name="x1" bitsize="32"/>
2225 <reg name="x2" bitsize="32"/>
2226 <reg name="x3" bitsize="32"/>
2227 <reg name="ps" bitsize="32" regnum="3"/>
2228
2229 but it also has false negatives. The target description
2230 below is correct,
2231
2232 <reg name="x1" bitsize="32" regnum="1"/>
2233 <reg name="x3" bitsize="32" regnum="3"/>
2234 <reg name="x2" bitsize="32" regnum="2"/>
2235 <reg name="x4" bitsize="32" regnum="4"/>
2236
2237 but it is not a good practice, so still error on this,
2238 and also print the message so that it can be saved in the
2239 generated c file. */
2240
2241 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
2242 reg->target_regnum);
2243 printf_unfiltered ("is not the largest number (%d).\n",
2244 m_next_regnum);
2245 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
2246 reg->target_regnum, m_next_regnum);
2247 }
2248
2249 if (reg->target_regnum > m_next_regnum)
2250 {
2251 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
2252 m_next_regnum = reg->target_regnum;
2253 }
2254
25aa13e5
YQ
2255 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
2256 reg->name, reg->save_restore);
2257 if (reg->group)
2258 printf_unfiltered ("\"%s\", ", reg->group);
2259 else
2260 printf_unfiltered ("NULL, ");
2261 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type);
ea03d0d3
YQ
2262
2263 m_next_regnum++;
25aa13e5
YQ
2264 }
2265
ea03d0d3
YQ
2266private:
2267 /* The register number to use for the next register we see. */
2268 int m_next_regnum = 0;
25aa13e5
YQ
2269};
2270
81adfced
DJ
2271static void
2272maint_print_c_tdesc_cmd (char *args, int from_tty)
2273{
2274 const struct target_desc *tdesc;
6eb1e6a8 2275 const char *filename;
81adfced 2276
8e2141c6
YQ
2277 if (args == NULL)
2278 {
2279 /* Use the global target-supplied description, not the current
2280 architecture's. This lets a GDB for one architecture generate C
2281 for another architecture's description, even though the gdbarch
2282 initialization code will reject the new description. */
2283 tdesc = current_target_desc;
2284 filename = target_description_filename;
2285 }
2286 else
2287 {
2288 /* Use the target description from the XML file. */
2289 filename = args;
2290 tdesc = file_read_description_xml (filename);
2291 }
2292
81adfced
DJ
2293 if (tdesc == NULL)
2294 error (_("There is no target description to print."));
2295
8e2141c6 2296 if (filename == NULL)
81adfced
DJ
2297 error (_("The current target description did not come from an XML file."));
2298
6eb1e6a8
YQ
2299 std::string filename_after_features (filename);
2300 auto loc = filename_after_features.rfind ("/features/");
4e2f8df6 2301
6eb1e6a8
YQ
2302 if (loc != std::string::npos)
2303 filename_after_features = filename_after_features.substr (loc + 10);
4e2f8df6 2304
25aa13e5
YQ
2305 /* Print c files for target features instead of target descriptions,
2306 because c files got from target features are more flexible than the
2307 counterparts. */
2308 if (startswith (filename_after_features.c_str (), "i386/32bit-"))
2309 {
2310 print_c_feature v (filename_after_features);
81adfced 2311
25aa13e5
YQ
2312 tdesc->accept (v);
2313 }
2314 else
2315 {
2316 print_c_tdesc v (filename_after_features);
2317
2318 tdesc->accept (v);
2319 }
81adfced
DJ
2320}
2321
27d41eac
YQ
2322namespace selftests {
2323
2324static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
2325
2326#if GDB_SELF_TEST
2327
2328/* See target-descritpions.h. */
2329
2330void
2331record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
2332{
2333 xml_tdesc.emplace_back (xml_file, tdesc);
2334}
2335#endif
2336
2337}
2338
2339/* Check that the target descriptions created dynamically by
2340 architecture-specific code equal the descriptions created from XML files
2341 found in the specified directory DIR. */
2342
2343static void
2344maintenance_check_xml_descriptions (char *dir, int from_tty)
2345{
2346 if (dir == NULL)
2347 error (_("Missing dir name"));
2348
2349 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
2350 std::string feature_dir (dir1.get ());
2351 unsigned int failed = 0;
2352
2353 for (auto const &e : selftests::xml_tdesc)
2354 {
2355 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
2356 const target_desc *tdesc
2357 = file_read_description_xml (tdesc_xml.data ());
2358
2359 if (tdesc == NULL || *tdesc != *e.second)
2360 failed++;
2361 }
2362 printf_filtered (_("Tested %lu XML files, %d failed\n"),
2363 (long) selftests::xml_tdesc.size (), failed);
2364}
2365
2c0b251b
PA
2366/* Provide a prototype to silence -Wmissing-prototypes. */
2367extern initialize_file_ftype _initialize_target_descriptions;
2368
23181151
DJ
2369void
2370_initialize_target_descriptions (void)
2371{
123dc839
DJ
2372 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
2373
23181151
DJ
2374 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
2375Set target description specific variables."),
2376 &tdesc_set_cmdlist, "set tdesc ",
2377 0 /* allow-unknown */, &setlist);
2378 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
2379Show target description specific variables."),
2380 &tdesc_show_cmdlist, "show tdesc ",
2381 0 /* allow-unknown */, &showlist);
2382 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
2383Unset target description specific variables."),
2384 &tdesc_unset_cmdlist, "unset tdesc ",
2385 0 /* allow-unknown */, &unsetlist);
2386
2387 add_setshow_filename_cmd ("filename", class_obscure,
6ecd4729 2388 &tdesc_filename_cmd_string,
23181151
DJ
2389 _("\
2390Set the file to read for an XML target description"), _("\
2391Show the file to read for an XML target description"), _("\
2392When set, GDB will read the target description from a local\n\
2393file instead of querying the remote target."),
2394 set_tdesc_filename_cmd,
2395 show_tdesc_filename_cmd,
2396 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
2397
2398 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
2399Unset the file to read for an XML target description. When unset,\n\
2400GDB will read the description from the target."),
2401 &tdesc_unset_cmdlist);
81adfced
DJ
2402
2403 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
2404Print the current target description as a C source file."),
2405 &maintenanceprintlist);
27d41eac
YQ
2406
2407 cmd_list_element *cmd;
2408
2409 cmd = add_cmd ("xml-descriptions", class_maintenance,
2410 maintenance_check_xml_descriptions, _("\
2411Check the target descriptions created in GDB equal the descriptions\n\
2412created from XML files in the directory.\n\
2413The parameter is the directory name."),
2414 &maintenancechecklist);
2415 set_cmd_completer (cmd, filename_completer);
23181151 2416}