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