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