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