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