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