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