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