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