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