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