Change program_space::added_solibs to a std::vector
[deliverable/binutils-gdb.git] / gdb / target-descriptions.c
CommitLineData
424163ea
DJ
1/* Target description support for GDB.
2
e2882c85 3 Copyright (C) 2006-2018 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
DJ
40
41/* Types. */
42
129c10bc 43struct property
29709017 44{
129c10bc
SM
45 property (const std::string &key_, const std::string &value_)
46 : key (key_), value (value_)
47 {}
48
49 std::string key;
50 std::string value;
51};
29709017 52
b8df6ca7
AH
53/* Convert a tdesc_type to a gdb type. */
54
55static type *
56make_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *ttype)
57{
58 class gdb_type_creator : public tdesc_element_visitor
27d41eac 59 {
b8df6ca7
AH
60 public:
61 gdb_type_creator (struct gdbarch *gdbarch)
62 : m_gdbarch (gdbarch)
63 {}
d4a0e8b5 64
b8df6ca7
AH
65 type *get_type ()
66 {
67 return m_type;
68 }
d4a0e8b5 69
b8df6ca7
AH
70 void visit (const tdesc_type_builtin *e) override
71 {
72 switch (e->kind)
73 {
74 /* Predefined types. */
75 case TDESC_TYPE_BOOL:
76 m_type = builtin_type (m_gdbarch)->builtin_bool;
77 return;
78 case TDESC_TYPE_INT8:
79 m_type = builtin_type (m_gdbarch)->builtin_int8;
80 return;
81 case TDESC_TYPE_INT16:
82 m_type = builtin_type (m_gdbarch)->builtin_int16;
83 return;
84 case TDESC_TYPE_INT32:
85 m_type = builtin_type (m_gdbarch)->builtin_int32;
86 return;
87 case TDESC_TYPE_INT64:
88 m_type = builtin_type (m_gdbarch)->builtin_int64;
89 return;
90 case TDESC_TYPE_INT128:
91 m_type = builtin_type (m_gdbarch)->builtin_int128;
92 return;
93 case TDESC_TYPE_UINT8:
94 m_type = builtin_type (m_gdbarch)->builtin_uint8;
95 return;
96 case TDESC_TYPE_UINT16:
97 m_type = builtin_type (m_gdbarch)->builtin_uint16;
98 return;
99 case TDESC_TYPE_UINT32:
100 m_type = builtin_type (m_gdbarch)->builtin_uint32;
101 return;
102 case TDESC_TYPE_UINT64:
103 m_type = builtin_type (m_gdbarch)->builtin_uint64;
104 return;
105 case TDESC_TYPE_UINT128:
106 m_type = builtin_type (m_gdbarch)->builtin_uint128;
107 return;
108 case TDESC_TYPE_CODE_PTR:
109 m_type = builtin_type (m_gdbarch)->builtin_func_ptr;
110 return;
111 case TDESC_TYPE_DATA_PTR:
112 m_type = builtin_type (m_gdbarch)->builtin_data_ptr;
113 return;
114 }
d4a0e8b5 115
b8df6ca7
AH
116 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
117 if (m_type != NULL)
118 return;
27d41eac 119
b8df6ca7
AH
120 switch (e->kind)
121 {
122 case TDESC_TYPE_IEEE_SINGLE:
123 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_single",
124 floatformats_ieee_single);
125 return;
126
127 case TDESC_TYPE_IEEE_DOUBLE:
128 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_ieee_double",
129 floatformats_ieee_double);
130 return;
131 case TDESC_TYPE_ARM_FPA_EXT:
132 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_arm_ext",
133 floatformats_arm_ext);
134 return;
135
136 case TDESC_TYPE_I387_EXT:
137 m_type = arch_float_type (m_gdbarch, -1, "builtin_type_i387_ext",
138 floatformats_i387_ext);
139 return;
140 }
d4a0e8b5 141
b8df6ca7
AH
142 internal_error (__FILE__, __LINE__,
143 "Type \"%s\" has an unknown kind %d",
144 e->name.c_str (), e->kind);
145 }
d4a0e8b5 146
b8df6ca7
AH
147 void visit (const tdesc_type_vector *e) override
148 {
149 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
150 if (m_type != NULL)
151 return;
152
153 type *element_gdb_type = make_gdb_type (m_gdbarch, e->element_type);
154 m_type = init_vector_type (element_gdb_type, e->count);
155 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
156 return;
157 }
53c934e9 158
b8df6ca7
AH
159 void visit (const tdesc_type_with_fields *e) override
160 {
161 m_type = tdesc_find_type (m_gdbarch, e->name.c_str ());
162 if (m_type != NULL)
163 return;
d4a0e8b5 164
b8df6ca7
AH
165 switch (e->kind)
166 {
167 case TDESC_TYPE_STRUCT:
168 make_gdb_type_struct (e);
169 return;
170 case TDESC_TYPE_UNION:
171 make_gdb_type_union (e);
172 return;
173 case TDESC_TYPE_FLAGS:
174 make_gdb_type_flags (e);
175 return;
176 case TDESC_TYPE_ENUM:
177 make_gdb_type_enum (e);
178 return;
179 }
d4a0e8b5 180
b8df6ca7
AH
181 internal_error (__FILE__, __LINE__,
182 "Type \"%s\" has an unknown kind %d",
183 e->name.c_str (), e->kind);
184 }
d4a0e8b5 185
b8df6ca7 186 private:
d4a0e8b5 187
b8df6ca7
AH
188 void make_gdb_type_struct (const tdesc_type_with_fields *e)
189 {
190 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_STRUCT);
191 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
192 TYPE_TAG_NAME (m_type) = TYPE_NAME (m_type);
d4a0e8b5 193
b8df6ca7
AH
194 for (const tdesc_type_field &f : e->fields)
195 {
196 if (f.start != -1 && f.end != -1)
197 {
198 /* Bitfield. */
199 struct field *fld;
200 struct type *field_gdb_type;
201 int bitsize, total_size;
202
203 /* This invariant should be preserved while creating types. */
204 gdb_assert (e->size != 0);
205 if (f.type != NULL)
206 field_gdb_type = make_gdb_type (m_gdbarch, f.type);
207 else if (e->size > 4)
208 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint64;
209 else
210 field_gdb_type = builtin_type (m_gdbarch)->builtin_uint32;
211
212 fld = append_composite_type_field_raw
213 (m_type, xstrdup (f.name.c_str ()), field_gdb_type);
214
215 /* For little-endian, BITPOS counts from the LSB of
216 the structure and marks the LSB of the field. For
217 big-endian, BITPOS counts from the MSB of the
218 structure and marks the MSB of the field. Either
219 way, it is the number of bits to the "left" of the
220 field. To calculate this in big-endian, we need
221 the total size of the structure. */
222 bitsize = f.end - f.start + 1;
223 total_size = e->size * TARGET_CHAR_BIT;
224 if (gdbarch_bits_big_endian (m_gdbarch))
225 SET_FIELD_BITPOS (fld[0], total_size - f.start - bitsize);
226 else
227 SET_FIELD_BITPOS (fld[0], f.start);
228 FIELD_BITSIZE (fld[0]) = bitsize;
229 }
230 else
231 {
232 gdb_assert (f.start == -1 && f.end == -1);
233 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
234 append_composite_type_field (m_type,
d4a0e8b5 235 xstrdup (f.name.c_str ()),
b8df6ca7
AH
236 field_gdb_type);
237 }
238 }
d4a0e8b5 239
b8df6ca7
AH
240 if (e->size != 0)
241 TYPE_LENGTH (m_type) = e->size;
242 }
d4a0e8b5 243
b8df6ca7
AH
244 void make_gdb_type_union (const tdesc_type_with_fields *e)
245 {
246 m_type = arch_composite_type (m_gdbarch, NULL, TYPE_CODE_UNION);
247 TYPE_NAME (m_type) = xstrdup (e->name.c_str ());
d4a0e8b5 248
b8df6ca7
AH
249 for (const tdesc_type_field &f : e->fields)
250 {
251 type* field_gdb_type = make_gdb_type (m_gdbarch, f.type);
252 append_composite_type_field (m_type, xstrdup (f.name.c_str ()),
253 field_gdb_type);
254
255 /* If any of the children of a union are vectors, flag the
256 union as a vector also. This allows e.g. a union of two
257 vector types to show up automatically in "info vector". */
258 if (TYPE_VECTOR (field_gdb_type))
259 TYPE_VECTOR (m_type) = 1;
260 }
261 }
d4a0e8b5 262
b8df6ca7 263 void make_gdb_type_flags (const tdesc_type_with_fields *e)
d4a0e8b5 264 {
b8df6ca7
AH
265 m_type = arch_flags_type (m_gdbarch, e->name.c_str (),
266 e->size * TARGET_CHAR_BIT);
267
268 for (const tdesc_type_field &f : e->fields)
269 {
270 int bitsize = f.end - f.start + 1;
271
272 gdb_assert (f.type != NULL);
273 type *field_gdb_type = make_gdb_type (m_gdbarch, f.type);
274 append_flags_type_field (m_type, f.start, bitsize,
275 field_gdb_type, f.name.c_str ());
276 }
d4a0e8b5
SM
277 }
278
b8df6ca7
AH
279 void make_gdb_type_enum (const tdesc_type_with_fields *e)
280 {
281 m_type = arch_type (m_gdbarch, TYPE_CODE_ENUM, e->size * TARGET_CHAR_BIT,
282 e->name.c_str ());
d4a0e8b5 283
b8df6ca7
AH
284 TYPE_UNSIGNED (m_type) = 1;
285 for (const tdesc_type_field &f : e->fields)
286 {
287 struct field *fld
288 = append_composite_type_field_raw (m_type,
289 xstrdup (f.name.c_str ()),
290 NULL);
d4a0e8b5 291
b8df6ca7
AH
292 SET_FIELD_BITPOS (fld[0], f.start);
293 }
294 }
295
296 /* The gdbarch used. */
297 struct gdbarch *m_gdbarch;
298
299 /* The type created. */
300 type *m_type;
301 };
302
303 gdb_type_creator gdb_type (gdbarch);
304 ttype->accept (gdb_type);
305 return gdb_type.get_type ();
306}
123dc839 307
123dc839
DJ
308/* A target description. */
309
6eb1e6a8 310struct target_desc : tdesc_element
424163ea 311{
b468ff4c
YQ
312 target_desc ()
313 {}
314
3eea796c 315 virtual ~target_desc () = default;
b468ff4c
YQ
316
317 target_desc (const target_desc &) = delete;
318 void operator= (const target_desc &) = delete;
319
23181151 320 /* The architecture reported by the target, if any. */
b468ff4c 321 const struct bfd_arch_info *arch = NULL;
23181151 322
08d16641
PA
323 /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN
324 otherwise. */
b468ff4c 325 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
08d16641 326
e35359c5 327 /* The list of compatible architectures reported by the target. */
40e2a983 328 std::vector<const bfd_arch_info *> compatible;
e35359c5 329
29709017 330 /* Any architecture-specific properties specified by the target. */
129c10bc 331 std::vector<property> properties;
123dc839
DJ
332
333 /* The features associated with this target. */
858c9d13 334 std::vector<tdesc_feature_up> features;
6eb1e6a8 335
e98577a9
AH
336 /* Used to cache the generated xml version of the target description. */
337 mutable char *xmltarget = nullptr;
338
6eb1e6a8
YQ
339 void accept (tdesc_element_visitor &v) const override
340 {
341 v.visit_pre (this);
342
3eea796c 343 for (const tdesc_feature_up &feature : features)
6eb1e6a8
YQ
344 feature->accept (v);
345
346 v.visit_post (this);
347 }
27d41eac
YQ
348
349 bool operator== (const target_desc &other) const
350 {
351 if (arch != other.arch)
352 return false;
353
354 if (osabi != other.osabi)
355 return false;
356
3eea796c 357 if (features.size () != other.features.size ())
27d41eac
YQ
358 return false;
359
3eea796c 360 for (int ix = 0; ix < features.size (); ix++)
27d41eac 361 {
3eea796c
SM
362 const tdesc_feature_up &feature1 = features[ix];
363 const tdesc_feature_up &feature2 = other.features[ix];
27d41eac 364
3eea796c 365 if (feature1 != feature2 && *feature1 != *feature2)
27d41eac
YQ
366 return false;
367 }
368
369 return true;
370 }
371
372 bool operator!= (const target_desc &other) const
373 {
374 return !(*this == other);
375 }
123dc839
DJ
376};
377
378/* Per-architecture data associated with a target description. The
379 target description may be shared by multiple architectures, but
380 this data is private to one gdbarch. */
381
f0cddbef 382struct tdesc_arch_reg
ad068eab 383{
f0cddbef
SM
384 tdesc_arch_reg (tdesc_reg *reg_, struct type *type_)
385 : reg (reg_), type (type_)
386 {}
387
ad068eab
UW
388 struct tdesc_reg *reg;
389 struct type *type;
f0cddbef 390};
ad068eab 391
123dc839
DJ
392struct tdesc_arch_data
393{
ad068eab 394 /* A list of register/type pairs, indexed by GDB's internal register number.
123dc839
DJ
395 During initialization of the gdbarch this list is used to store
396 registers which the architecture assigns a fixed register number.
397 Registers which are NULL in this array, or off the end, are
398 treated as zero-sized and nameless (i.e. placeholders in the
399 numbering). */
f0cddbef 400 std::vector<tdesc_arch_reg> arch_regs;
123dc839
DJ
401
402 /* Functions which report the register name, type, and reggroups for
403 pseudo-registers. */
f0cddbef
SM
404 gdbarch_register_name_ftype *pseudo_register_name = NULL;
405 gdbarch_register_type_ftype *pseudo_register_type = NULL;
406 gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p = NULL;
424163ea
DJ
407};
408
6ecd4729
PA
409/* Info about an inferior's target description. There's one of these
410 for each inferior. */
424163ea 411
6ecd4729
PA
412struct target_desc_info
413{
414 /* A flag indicating that a description has already been fetched
415 from the target, so it should not be queried again. */
416
417 int fetched;
424163ea 418
6ecd4729
PA
419 /* The description fetched from the target, or NULL if the target
420 did not supply any description. Only valid when
421 target_desc_fetched is set. Only the description initialization
422 code should access this; normally, the description should be
423 accessed through the gdbarch object. */
424163ea 424
6ecd4729 425 const struct target_desc *tdesc;
424163ea 426
6ecd4729
PA
427 /* The filename to read a target description from, as set by "set
428 tdesc filename ..." */
424163ea 429
6ecd4729
PA
430 char *filename;
431};
23181151 432
6ecd4729
PA
433/* Get the inferior INF's target description info, allocating one on
434 the stop if necessary. */
23181151 435
6ecd4729
PA
436static struct target_desc_info *
437get_tdesc_info (struct inferior *inf)
438{
439 if (inf->tdesc_info == NULL)
440 inf->tdesc_info = XCNEW (struct target_desc_info);
441 return inf->tdesc_info;
442}
23181151 443
123dc839
DJ
444/* A handle for architecture-specific data associated with the
445 target description (see struct tdesc_arch_data). */
446
447static struct gdbarch_data *tdesc_data;
448
6ecd4729
PA
449/* See target-descriptions.h. */
450
451int
452target_desc_info_from_user_p (struct target_desc_info *info)
453{
454 return info != NULL && info->filename != NULL;
455}
456
457/* See target-descriptions.h. */
458
459void
460copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
461{
462 struct target_desc_info *src = get_tdesc_info (srcinf);
463 struct target_desc_info *dest = get_tdesc_info (destinf);
464
465 dest->fetched = src->fetched;
466 dest->tdesc = src->tdesc;
467 dest->filename = src->filename != NULL ? xstrdup (src->filename) : NULL;
468}
469
470/* See target-descriptions.h. */
471
472void
473target_desc_info_free (struct target_desc_info *tdesc_info)
474{
475 if (tdesc_info != NULL)
476 {
477 xfree (tdesc_info->filename);
478 xfree (tdesc_info);
479 }
480}
481
482/* Convenience helper macros. */
483
484#define target_desc_fetched \
485 get_tdesc_info (current_inferior ())->fetched
486#define current_target_desc \
487 get_tdesc_info (current_inferior ())->tdesc
488#define target_description_filename \
489 get_tdesc_info (current_inferior ())->filename
490
491/* The string manipulated by the "set tdesc filename ..." command. */
492
493static char *tdesc_filename_cmd_string;
494
424163ea
DJ
495/* Fetch the current target's description, and switch the current
496 architecture to one which incorporates that description. */
497
498void
499target_find_description (void)
500{
501 /* If we've already fetched a description from the target, don't do
502 it again. This allows a target to fetch the description early,
503 during its to_open or to_create_inferior, if it needs extra
504 information about the target to initialize. */
505 if (target_desc_fetched)
506 return;
507
508 /* The current architecture should not have any target description
509 specified. It should have been cleared, e.g. when we
510 disconnected from the previous target. */
f5656ead 511 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
424163ea 512
23181151
DJ
513 /* First try to fetch an XML description from the user-specified
514 file. */
515 current_target_desc = NULL;
516 if (target_description_filename != NULL
517 && *target_description_filename != '\0')
518 current_target_desc
519 = file_read_description_xml (target_description_filename);
520
521 /* Next try to read the description from the current target using
522 target objects. */
523 if (current_target_desc == NULL)
f6ac5f3d 524 current_target_desc = target_read_description_xml (target_stack);
23181151
DJ
525
526 /* If that failed try a target-specific hook. */
527 if (current_target_desc == NULL)
f6ac5f3d 528 current_target_desc = target_read_description (target_stack);
424163ea
DJ
529
530 /* If a non-NULL description was returned, then update the current
531 architecture. */
532 if (current_target_desc)
533 {
534 struct gdbarch_info info;
535
536 gdbarch_info_init (&info);
537 info.target_desc = current_target_desc;
538 if (!gdbarch_update_p (info))
123dc839
DJ
539 warning (_("Architecture rejected target-supplied description"));
540 else
541 {
542 struct tdesc_arch_data *data;
543
19ba03f4
SM
544 data = ((struct tdesc_arch_data *)
545 gdbarch_data (target_gdbarch (), tdesc_data));
123dc839 546 if (tdesc_has_registers (current_target_desc)
f0cddbef 547 && data->arch_regs.empty ())
123dc839
DJ
548 warning (_("Target-supplied registers are not supported "
549 "by the current architecture"));
550 }
424163ea
DJ
551 }
552
553 /* Now that we know this description is usable, record that we
554 fetched it. */
555 target_desc_fetched = 1;
556}
557
558/* Discard any description fetched from the current target, and switch
559 the current architecture to one with no target description. */
560
561void
562target_clear_description (void)
563{
564 struct gdbarch_info info;
565
566 if (!target_desc_fetched)
567 return;
568
569 target_desc_fetched = 0;
570 current_target_desc = NULL;
571
572 gdbarch_info_init (&info);
573 if (!gdbarch_update_p (info))
574 internal_error (__FILE__, __LINE__,
575 _("Could not remove target-supplied description"));
576}
577
578/* Return the global current target description. This should only be
579 used by gdbarch initialization code; most access should be through
580 an existing gdbarch. */
581
582const struct target_desc *
583target_current_description (void)
584{
585 if (target_desc_fetched)
586 return current_target_desc;
587
588 return NULL;
589}
e35359c5
UW
590
591/* Return non-zero if this target description is compatible
592 with the given BFD architecture. */
593
594int
595tdesc_compatible_p (const struct target_desc *target_desc,
596 const struct bfd_arch_info *arch)
597{
40e2a983 598 for (const bfd_arch_info *compat : target_desc->compatible)
e35359c5
UW
599 {
600 if (compat == arch
601 || arch->compatible (arch, compat)
602 || compat->compatible (compat, arch))
603 return 1;
604 }
605
606 return 0;
607}
23181151
DJ
608\f
609
123dc839 610/* Direct accessors for target descriptions. */
424163ea 611
29709017
DJ
612/* Return the string value of a property named KEY, or NULL if the
613 property was not specified. */
614
615const char *
616tdesc_property (const struct target_desc *target_desc, const char *key)
617{
129c10bc
SM
618 for (const property &prop : target_desc->properties)
619 if (prop.key == key)
620 return prop.value.c_str ();
29709017
DJ
621
622 return NULL;
623}
624
23181151
DJ
625/* Return the BFD architecture associated with this target
626 description, or NULL if no architecture was specified. */
627
628const struct bfd_arch_info *
629tdesc_architecture (const struct target_desc *target_desc)
630{
631 return target_desc->arch;
632}
08d16641 633
d278f585
AH
634/* See common/tdesc.h. */
635
636const char *
637tdesc_architecture_name (const struct target_desc *target_desc)
638{
639 return target_desc->arch->printable_name;
640}
641
08d16641
PA
642/* Return the OSABI associated with this target description, or
643 GDB_OSABI_UNKNOWN if no osabi was specified. */
644
645enum gdb_osabi
646tdesc_osabi (const struct target_desc *target_desc)
647{
648 return target_desc->osabi;
649}
650
d278f585
AH
651/* See common/tdesc.h. */
652
653const char *
654tdesc_osabi_name (const struct target_desc *target_desc)
655{
656 enum gdb_osabi osabi = tdesc_osabi (target_desc);
657 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
658 return gdbarch_osabi_name (osabi);
659 return nullptr;
660}
23181151 661
123dc839
DJ
662/* Return 1 if this target description includes any registers. */
663
664int
665tdesc_has_registers (const struct target_desc *target_desc)
666{
123dc839
DJ
667 if (target_desc == NULL)
668 return 0;
669
3eea796c 670 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 671 if (!feature->registers.empty ())
123dc839
DJ
672 return 1;
673
674 return 0;
675}
676
677/* Return the feature with the given name, if present, or NULL if
678 the named feature is not found. */
679
680const struct tdesc_feature *
681tdesc_find_feature (const struct target_desc *target_desc,
682 const char *name)
683{
3eea796c 684 for (const tdesc_feature_up &feature : target_desc->features)
f65ff9f9 685 if (feature->name == name)
3eea796c 686 return feature.get ();
123dc839
DJ
687
688 return NULL;
689}
690
691/* Return the name of FEATURE. */
692
693const char *
694tdesc_feature_name (const struct tdesc_feature *feature)
695{
f65ff9f9 696 return feature->name.c_str ();
123dc839
DJ
697}
698
9fd3625f
L
699/* Lookup type associated with ID. */
700
701struct type *
702tdesc_find_type (struct gdbarch *gdbarch, const char *id)
703{
f0cddbef
SM
704 tdesc_arch_data *data
705 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
9fd3625f 706
f0cddbef 707 for (const tdesc_arch_reg &reg : data->arch_regs)
9fd3625f 708 {
f0cddbef
SM
709 if (reg.reg
710 && reg.reg->tdesc_type
711 && reg.type
712 && reg.reg->tdesc_type->name == id)
713 return reg.type;
9fd3625f
L
714 }
715
716 return NULL;
717}
718
123dc839
DJ
719/* Support for registers from target descriptions. */
720
721/* Construct the per-gdbarch data. */
722
723static void *
724tdesc_data_init (struct obstack *obstack)
725{
284a0e3c 726 return obstack_new<tdesc_arch_data> (obstack);
123dc839
DJ
727}
728
729/* Similar, but for the temporary copy used during architecture
730 initialization. */
731
732struct tdesc_arch_data *
733tdesc_data_alloc (void)
734{
f0cddbef 735 return new tdesc_arch_data ();
123dc839
DJ
736}
737
738/* Free something allocated by tdesc_data_alloc, if it is not going
739 to be used (for instance if it was unsuitable for the
740 architecture). */
741
742void
743tdesc_data_cleanup (void *data_untyped)
744{
19ba03f4 745 struct tdesc_arch_data *data = (struct tdesc_arch_data *) data_untyped;
123dc839 746
f0cddbef 747 delete data;
123dc839
DJ
748}
749
750/* Search FEATURE for a register named NAME. */
751
7cc46491
DJ
752static struct tdesc_reg *
753tdesc_find_register_early (const struct tdesc_feature *feature,
754 const char *name)
123dc839 755{
c9c895b9 756 for (const tdesc_reg_up &reg : feature->registers)
a8142ee1 757 if (strcasecmp (reg->name.c_str (), name) == 0)
c9c895b9 758 return reg.get ();
123dc839 759
7cc46491
DJ
760 return NULL;
761}
762
763/* Search FEATURE for a register named NAME. Assign REGNO to it. */
764
765int
766tdesc_numbered_register (const struct tdesc_feature *feature,
767 struct tdesc_arch_data *data,
768 int regno, const char *name)
769{
770 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
771
772 if (reg == NULL)
773 return 0;
774
775 /* Make sure the vector includes a REGNO'th element. */
f0cddbef
SM
776 while (regno >= data->arch_regs.size ())
777 data->arch_regs.emplace_back (nullptr, nullptr);
778
779 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
ad068eab 780
7cc46491 781 return 1;
123dc839
DJ
782}
783
58d6951d
DJ
784/* Search FEATURE for a register named NAME, but do not assign a fixed
785 register number to it. */
786
787int
788tdesc_unnumbered_register (const struct tdesc_feature *feature,
789 const char *name)
790{
791 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
792
793 if (reg == NULL)
794 return 0;
795
796 return 1;
797}
798
7cc46491
DJ
799/* Search FEATURE for a register whose name is in NAMES and assign
800 REGNO to it. */
123dc839
DJ
801
802int
803tdesc_numbered_register_choices (const struct tdesc_feature *feature,
804 struct tdesc_arch_data *data,
805 int regno, const char *const names[])
806{
807 int i;
808
809 for (i = 0; names[i] != NULL; i++)
810 if (tdesc_numbered_register (feature, data, regno, names[i]))
811 return 1;
812
813 return 0;
814}
815
7cc46491
DJ
816/* Search FEATURE for a register named NAME, and return its size in
817 bits. The register must exist. */
818
819int
820tdesc_register_size (const struct tdesc_feature *feature,
821 const char *name)
822{
823 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
824
825 gdb_assert (reg != NULL);
826 return reg->bitsize;
827}
828
123dc839
DJ
829/* Look up a register by its GDB internal register number. */
830
ad068eab
UW
831static struct tdesc_arch_reg *
832tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
123dc839 833{
123dc839
DJ
834 struct tdesc_arch_data *data;
835
19ba03f4 836 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
f0cddbef
SM
837 if (regno < data->arch_regs.size ())
838 return &data->arch_regs[regno];
123dc839
DJ
839 else
840 return NULL;
841}
842
ad068eab
UW
843static struct tdesc_reg *
844tdesc_find_register (struct gdbarch *gdbarch, int regno)
845{
846 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
5d502164 847
ad068eab
UW
848 return reg? reg->reg : NULL;
849}
850
f8b73d13
DJ
851/* Return the name of register REGNO, from the target description or
852 from an architecture-provided pseudo_register_name method. */
853
854const char *
d93859e2 855tdesc_register_name (struct gdbarch *gdbarch, int regno)
123dc839 856{
d93859e2
UW
857 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
858 int num_regs = gdbarch_num_regs (gdbarch);
859 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
123dc839
DJ
860
861 if (reg != NULL)
a8142ee1 862 return reg->name.c_str ();
123dc839
DJ
863
864 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
865 {
19ba03f4
SM
866 struct tdesc_arch_data *data
867 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 868
123dc839 869 gdb_assert (data->pseudo_register_name != NULL);
d93859e2 870 return data->pseudo_register_name (gdbarch, regno);
123dc839
DJ
871 }
872
873 return "";
874}
875
58d6951d 876struct type *
123dc839
DJ
877tdesc_register_type (struct gdbarch *gdbarch, int regno)
878{
ad068eab
UW
879 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
880 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
123dc839
DJ
881 int num_regs = gdbarch_num_regs (gdbarch);
882 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
883
884 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
885 {
19ba03f4
SM
886 struct tdesc_arch_data *data
887 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 888
123dc839
DJ
889 gdb_assert (data->pseudo_register_type != NULL);
890 return data->pseudo_register_type (gdbarch, regno);
891 }
892
893 if (reg == NULL)
894 /* Return "int0_t", since "void" has a misleading size of one. */
df4df182 895 return builtin_type (gdbarch)->builtin_int0;
123dc839 896
ad068eab 897 if (arch_reg->type == NULL)
123dc839 898 {
ad068eab
UW
899 /* First check for a predefined or target defined type. */
900 if (reg->tdesc_type)
b8df6ca7 901 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
ad068eab
UW
902
903 /* Next try size-sensitive type shortcuts. */
a8142ee1 904 else if (reg->type == "float")
ad068eab
UW
905 {
906 if (reg->bitsize == gdbarch_float_bit (gdbarch))
907 arch_reg->type = builtin_type (gdbarch)->builtin_float;
908 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
909 arch_reg->type = builtin_type (gdbarch)->builtin_double;
910 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
911 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
912 else
913 {
914 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
a8142ee1 915 reg->name.c_str (), reg->bitsize);
ad068eab
UW
916 arch_reg->type = builtin_type (gdbarch)->builtin_double;
917 }
918 }
a8142ee1 919 else if (reg->type == "int")
ad068eab
UW
920 {
921 if (reg->bitsize == gdbarch_long_bit (gdbarch))
922 arch_reg->type = builtin_type (gdbarch)->builtin_long;
923 else if (reg->bitsize == TARGET_CHAR_BIT)
924 arch_reg->type = builtin_type (gdbarch)->builtin_char;
925 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
926 arch_reg->type = builtin_type (gdbarch)->builtin_short;
927 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
928 arch_reg->type = builtin_type (gdbarch)->builtin_int;
929 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
930 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
931 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
c378eb4e 932 /* A bit desperate by this point... */
ad068eab
UW
933 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
934 else
935 {
936 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
a8142ee1 937 reg->name.c_str (), reg->bitsize);
ad068eab
UW
938 arch_reg->type = builtin_type (gdbarch)->builtin_long;
939 }
940 }
941
942 if (arch_reg->type == NULL)
943 internal_error (__FILE__, __LINE__,
944 "Register \"%s\" has an unknown type \"%s\"",
a8142ee1 945 reg->name.c_str (), reg->type.c_str ());
123dc839 946 }
123dc839 947
ad068eab 948 return arch_reg->type;
123dc839
DJ
949}
950
951static int
952tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
953{
954 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
955
956 if (reg != NULL)
957 return reg->target_regnum;
958 else
959 return -1;
960}
961
962/* Check whether REGNUM is a member of REGGROUP. Registers from the
cef0f868
SH
963 target description may be classified as general, float, vector or other
964 register groups registered with reggroup_add(). Unlike a gdbarch
965 register_reggroup_p method, this function will return -1 if it does not
966 know; the caller should handle registers with no specified group.
967
968 The names of containing features are not used. This might be extended
969 to display registers in some more useful groupings.
123dc839
DJ
970
971 The save-restore flag is also implemented here. */
972
f8b73d13
DJ
973int
974tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
975 struct reggroup *reggroup)
123dc839 976{
123dc839
DJ
977 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
978
cef0f868
SH
979 if (reg != NULL && !reg->group.empty ()
980 && (reg->group == reggroup_name (reggroup)))
981 return 1;
123dc839
DJ
982
983 if (reg != NULL
984 && (reggroup == save_reggroup || reggroup == restore_reggroup))
985 return reg->save_restore;
986
f8b73d13
DJ
987 return -1;
988}
989
990/* Check whether REGNUM is a member of REGGROUP. Registers with no
991 group specified go to the default reggroup function and are handled
992 by type. */
993
994static int
995tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
996 struct reggroup *reggroup)
997{
998 int num_regs = gdbarch_num_regs (gdbarch);
999 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1000 int ret;
1001
1002 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1003 {
19ba03f4
SM
1004 struct tdesc_arch_data *data
1005 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1006
58d6951d
DJ
1007 if (data->pseudo_register_reggroup_p != NULL)
1008 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1009 /* Otherwise fall through to the default reggroup_p. */
f8b73d13
DJ
1010 }
1011
1012 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1013 if (ret != -1)
1014 return ret;
1015
123dc839
DJ
1016 return default_register_reggroup_p (gdbarch, regno, reggroup);
1017}
1018
1019/* Record architecture-specific functions to call for pseudo-register
1020 support. */
1021
1022void
1023set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1024 gdbarch_register_name_ftype *pseudo_name)
1025{
19ba03f4
SM
1026 struct tdesc_arch_data *data
1027 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1028
1029 data->pseudo_register_name = pseudo_name;
1030}
1031
1032void
1033set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1034 gdbarch_register_type_ftype *pseudo_type)
1035{
19ba03f4
SM
1036 struct tdesc_arch_data *data
1037 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1038
1039 data->pseudo_register_type = pseudo_type;
1040}
1041
1042void
1043set_tdesc_pseudo_register_reggroup_p
1044 (struct gdbarch *gdbarch,
1045 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1046{
19ba03f4
SM
1047 struct tdesc_arch_data *data
1048 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1049
1050 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1051}
1052
1053/* Update GDBARCH to use the target description for registers. */
1054
1055void
1056tdesc_use_registers (struct gdbarch *gdbarch,
7cc46491 1057 const struct target_desc *target_desc,
123dc839
DJ
1058 struct tdesc_arch_data *early_data)
1059{
1060 int num_regs = gdbarch_num_regs (gdbarch);
123dc839
DJ
1061 struct tdesc_arch_data *data;
1062 htab_t reg_hash;
1063
123dc839
DJ
1064 /* We can't use the description for registers if it doesn't describe
1065 any. This function should only be called after validating
1066 registers, so the caller should know that registers are
1067 included. */
1068 gdb_assert (tdesc_has_registers (target_desc));
1069
19ba03f4 1070 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
ad068eab 1071 data->arch_regs = early_data->arch_regs;
f0cddbef 1072 delete early_data;
123dc839
DJ
1073
1074 /* Build up a set of all registers, so that we can assign register
1075 numbers where needed. The hash table expands as necessary, so
1076 the initial size is arbitrary. */
1077 reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3eea796c 1078 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 1079 for (const tdesc_reg_up &reg : feature->registers)
123dc839 1080 {
c9c895b9 1081 void **slot = htab_find_slot (reg_hash, reg.get (), INSERT);
123dc839 1082
c9c895b9 1083 *slot = reg.get ();
cef0f868
SH
1084 /* Add reggroup if its new. */
1085 if (!reg->group.empty ())
1086 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1087 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1088 reg->group.c_str (),
1089 USER_REGGROUP));
123dc839
DJ
1090 }
1091
1092 /* Remove any registers which were assigned numbers by the
1093 architecture. */
f0cddbef
SM
1094 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1095 if (arch_reg.reg != NULL)
1096 htab_remove_elt (reg_hash, arch_reg.reg);
123dc839
DJ
1097
1098 /* Assign numbers to the remaining registers and add them to the
f57d151a 1099 list of registers. The new numbers are always above gdbarch_num_regs.
123dc839
DJ
1100 Iterate over the features, not the hash table, so that the order
1101 matches that in the target description. */
1102
f0cddbef
SM
1103 gdb_assert (data->arch_regs.size () <= num_regs);
1104 while (data->arch_regs.size () < num_regs)
1105 data->arch_regs.emplace_back (nullptr, nullptr);
1106
3eea796c 1107 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9
SM
1108 for (const tdesc_reg_up &reg : feature->registers)
1109 if (htab_find (reg_hash, reg.get ()) != NULL)
123dc839 1110 {
f0cddbef 1111 data->arch_regs.emplace_back (reg.get (), nullptr);
123dc839
DJ
1112 num_regs++;
1113 }
1114
1115 htab_delete (reg_hash);
1116
1117 /* Update the architecture. */
1118 set_gdbarch_num_regs (gdbarch, num_regs);
1119 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1120 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1121 set_gdbarch_remote_register_number (gdbarch,
1122 tdesc_remote_register_number);
1123 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1124}
123dc839 1125
f46cd62a 1126/* See common/tdesc.h. */
f49ff000 1127
123dc839 1128struct tdesc_feature *
3b74854b 1129tdesc_create_feature (struct target_desc *tdesc, const char *name)
123dc839 1130{
72ddacb7 1131 struct tdesc_feature *new_feature = new tdesc_feature (name);
123dc839 1132
3eea796c
SM
1133 tdesc->features.emplace_back (new_feature);
1134
123dc839
DJ
1135 return new_feature;
1136}
1137
424163ea
DJ
1138struct target_desc *
1139allocate_target_description (void)
1140{
b468ff4c 1141 return new target_desc ();
424163ea 1142}
29709017 1143
23181151
DJ
1144static void
1145free_target_description (void *arg)
1146{
19ba03f4 1147 struct target_desc *target_desc = (struct target_desc *) arg;
e35359c5 1148
b468ff4c 1149 delete target_desc;
23181151
DJ
1150}
1151
1152struct cleanup *
1153make_cleanup_free_target_description (struct target_desc *target_desc)
1154{
1155 return make_cleanup (free_target_description, target_desc);
1156}
1157
e35359c5
UW
1158void
1159tdesc_add_compatible (struct target_desc *target_desc,
1160 const struct bfd_arch_info *compatible)
1161{
e35359c5
UW
1162 /* If this instance of GDB is compiled without BFD support for the
1163 compatible architecture, simply ignore it -- we would not be able
1164 to handle it anyway. */
1165 if (compatible == NULL)
1166 return;
1167
40e2a983 1168 for (const bfd_arch_info *compat : target_desc->compatible)
e35359c5
UW
1169 if (compat == compatible)
1170 internal_error (__FILE__, __LINE__,
1171 _("Attempted to add duplicate "
1172 "compatible architecture \"%s\""),
1173 compatible->printable_name);
1174
40e2a983 1175 target_desc->compatible.push_back (compatible);
e35359c5
UW
1176}
1177
29709017
DJ
1178void
1179set_tdesc_property (struct target_desc *target_desc,
1180 const char *key, const char *value)
1181{
29709017
DJ
1182 gdb_assert (key != NULL && value != NULL);
1183
129c10bc
SM
1184 if (tdesc_property (target_desc, key) != NULL)
1185 internal_error (__FILE__, __LINE__,
1186 _("Attempted to add duplicate property \"%s\""), key);
29709017 1187
129c10bc 1188 target_desc->properties.emplace_back (key, value);
29709017 1189}
23181151 1190
f46cd62a 1191/* See common/tdesc.h. */
5f035c07
YQ
1192
1193void
1194set_tdesc_architecture (struct target_desc *target_desc,
1195 const char *name)
1196{
1197 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1198}
1199
23181151
DJ
1200void
1201set_tdesc_architecture (struct target_desc *target_desc,
1202 const struct bfd_arch_info *arch)
1203{
1204 target_desc->arch = arch;
1205}
08d16641 1206
f46cd62a 1207/* See common/tdesc.h. */
5f035c07
YQ
1208
1209void
1210set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1211{
1212 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1213}
1214
08d16641
PA
1215void
1216set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1217{
1218 target_desc->osabi = osabi;
1219}
23181151
DJ
1220\f
1221
1222static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1223static struct cmd_list_element *tdesc_unset_cmdlist;
1224
1225/* Helper functions for the CLI commands. */
1226
1227static void
981a3fb3 1228set_tdesc_cmd (const char *args, int from_tty)
23181151 1229{
635c7e8a 1230 help_list (tdesc_set_cmdlist, "set tdesc ", all_commands, gdb_stdout);
23181151
DJ
1231}
1232
1233static void
981a3fb3 1234show_tdesc_cmd (const char *args, int from_tty)
23181151
DJ
1235{
1236 cmd_show_list (tdesc_show_cmdlist, from_tty, "");
1237}
1238
1239static void
981a3fb3 1240unset_tdesc_cmd (const char *args, int from_tty)
23181151 1241{
635c7e8a 1242 help_list (tdesc_unset_cmdlist, "unset tdesc ", all_commands, gdb_stdout);
23181151
DJ
1243}
1244
1245static void
eb4c3f4a 1246set_tdesc_filename_cmd (const char *args, int from_tty,
23181151
DJ
1247 struct cmd_list_element *c)
1248{
6ecd4729
PA
1249 xfree (target_description_filename);
1250 target_description_filename = xstrdup (tdesc_filename_cmd_string);
1251
23181151
DJ
1252 target_clear_description ();
1253 target_find_description ();
1254}
1255
1256static void
1257show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1258 struct cmd_list_element *c,
1259 const char *value)
1260{
6ecd4729
PA
1261 value = target_description_filename;
1262
23181151 1263 if (value != NULL && *value != '\0')
3e43a32a 1264 printf_filtered (_("The target description will be read from \"%s\".\n"),
23181151
DJ
1265 value);
1266 else
3e43a32a
MS
1267 printf_filtered (_("The target description will be "
1268 "read from the target.\n"));
23181151
DJ
1269}
1270
1271static void
e100df1a 1272unset_tdesc_filename_cmd (const char *args, int from_tty)
23181151
DJ
1273{
1274 xfree (target_description_filename);
1275 target_description_filename = NULL;
1276 target_clear_description ();
1277 target_find_description ();
1278}
1279
6eb1e6a8
YQ
1280/* Print target description in C. */
1281
1282class print_c_tdesc : public tdesc_element_visitor
1283{
1284public:
1285 print_c_tdesc (std::string &filename_after_features)
1286 : m_filename_after_features (filename_after_features)
1287 {
1288 const char *inp;
1289 char *outp;
1290 const char *filename = lbasename (m_filename_after_features.c_str ());
1291
1292 m_function = (char *) xmalloc (strlen (filename) + 1);
1293 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1294 if (*inp == '.')
1295 break;
1296 else if (*inp == '-')
1297 *outp++ = '_';
1298 else
1299 *outp++ = *inp;
1300 *outp = '\0';
1301
1302 /* Standard boilerplate. */
1303 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1304 "-*- buffer-read-only: t -*- vi"
1305 ":set ro:\n");
6eb1e6a8
YQ
1306 }
1307
1308 ~print_c_tdesc ()
1309 {
1310 xfree (m_function);
1311 }
1312
1313 void visit_pre (const target_desc *e) override
1314 {
25aa13e5
YQ
1315 printf_unfiltered (" Original: %s */\n\n",
1316 lbasename (m_filename_after_features.c_str ()));
1317
6eb1e6a8
YQ
1318 printf_unfiltered ("#include \"defs.h\"\n");
1319 printf_unfiltered ("#include \"osabi.h\"\n");
1320 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1321 printf_unfiltered ("\n");
1322
1323 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1324 printf_unfiltered ("static void\n");
1325 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1326 printf_unfiltered ("{\n");
1327 printf_unfiltered
1328 (" struct target_desc *result = allocate_target_description ();\n");
1329
1330 if (tdesc_architecture (e) != NULL)
1331 {
1332 printf_unfiltered
1333 (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n",
1334 tdesc_architecture (e)->printable_name);
1335 printf_unfiltered ("\n");
1336 }
1337 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1338 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1339 {
1340 printf_unfiltered
1341 (" set_tdesc_osabi (result, osabi_from_tdesc_string (\"%s\"));\n",
1342 gdbarch_osabi_name (tdesc_osabi (e)));
1343 printf_unfiltered ("\n");
1344 }
1345
23a8d186 1346 for (const bfd_arch_info_type *compatible : e->compatible)
40e2a983
SM
1347 printf_unfiltered
1348 (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n",
1349 compatible->printable_name);
6eb1e6a8 1350
40e2a983 1351 if (!e->compatible.empty ())
6eb1e6a8
YQ
1352 printf_unfiltered ("\n");
1353
129c10bc
SM
1354 for (const property &prop : e->properties)
1355 printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n",
1356 prop.key.c_str (), prop.value.c_str ());
1357
6eb1e6a8
YQ
1358 printf_unfiltered (" struct tdesc_feature *feature;\n");
1359 }
1360
25aa13e5 1361 void visit_pre (const tdesc_feature *e) override
6eb1e6a8
YQ
1362 {
1363 printf_unfiltered ("\n feature = tdesc_create_feature (result, \"%s\");\n",
f65ff9f9 1364 e->name.c_str ());
6eb1e6a8
YQ
1365 }
1366
25aa13e5
YQ
1367 void visit_post (const tdesc_feature *e) override
1368 {}
1369
6eb1e6a8
YQ
1370 void visit_post (const target_desc *e) override
1371 {
1372 printf_unfiltered ("\n tdesc_%s = result;\n", m_function);
1373 printf_unfiltered ("}\n");
1374 }
1375
d4a0e8b5
SM
1376 void visit (const tdesc_type_builtin *type) override
1377 {
1378 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1379 }
1380
1381 void visit (const tdesc_type_vector *type) override
6eb1e6a8 1382 {
d4a0e8b5 1383 if (!m_printed_element_type)
6eb1e6a8 1384 {
d4a0e8b5
SM
1385 printf_unfiltered (" tdesc_type *element_type;\n");
1386 m_printed_element_type = true;
6eb1e6a8
YQ
1387 }
1388
d4a0e8b5
SM
1389 printf_unfiltered
1390 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1391 type->element_type->name.c_str ());
1392 printf_unfiltered
1393 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1394 type->name.c_str (), type->count);
1395
1396 printf_unfiltered ("\n");
1397 }
1398
1399 void visit (const tdesc_type_with_fields *type) override
1400 {
1401 if (!m_printed_type_with_fields)
6eb1e6a8 1402 {
d4a0e8b5
SM
1403 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1404 m_printed_type_with_fields = true;
1405 }
1406
6eb1e6a8
YQ
1407 switch (type->kind)
1408 {
6eb1e6a8
YQ
1409 case TDESC_TYPE_STRUCT:
1410 case TDESC_TYPE_FLAGS:
1411 if (type->kind == TDESC_TYPE_STRUCT)
1412 {
1413 printf_unfiltered
d4a0e8b5 1414 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
082b9140 1415 type->name.c_str ());
d4a0e8b5 1416 if (type->size != 0)
6eb1e6a8 1417 printf_unfiltered
d4a0e8b5 1418 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
6eb1e6a8
YQ
1419 }
1420 else
1421 {
1422 printf_unfiltered
d4a0e8b5
SM
1423 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1424 type->name.c_str (), type->size);
6eb1e6a8 1425 }
d4a0e8b5 1426 for (const tdesc_type_field &f : type->fields)
6eb1e6a8
YQ
1427 {
1428 const char *type_name;
1429
d05200d1
SM
1430 gdb_assert (f.type != NULL);
1431 type_name = f.type->name.c_str ();
6eb1e6a8
YQ
1432
1433 /* To minimize changes to generated files, don't emit type
1434 info for fields that have defaulted types. */
d05200d1 1435 if (f.start != -1)
6eb1e6a8 1436 {
d05200d1
SM
1437 gdb_assert (f.end != -1);
1438 if (f.type->kind == TDESC_TYPE_BOOL)
6eb1e6a8 1439 {
d05200d1 1440 gdb_assert (f.start == f.end);
6eb1e6a8 1441 printf_unfiltered
d4a0e8b5 1442 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
d05200d1 1443 f.start, f.name.c_str ());
6eb1e6a8 1444 }
d4a0e8b5
SM
1445 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1446 || (type->size == 8
d05200d1 1447 && f.type->kind == TDESC_TYPE_UINT64))
6eb1e6a8
YQ
1448 {
1449 printf_unfiltered
d4a0e8b5 1450 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
d05200d1 1451 f.name.c_str (), f.start, f.end);
6eb1e6a8
YQ
1452 }
1453 else
1454 {
a8d2e585
SM
1455 printf_field_type_assignment
1456 ("tdesc_named_type (feature, \"%s\");\n",
6eb1e6a8
YQ
1457 type_name);
1458 printf_unfiltered
d4a0e8b5 1459 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
6eb1e6a8 1460 " %d, %d, field_type);\n",
d05200d1 1461 f.name.c_str (), f.start, f.end);
6eb1e6a8
YQ
1462 }
1463 }
1464 else /* Not a bitfield. */
1465 {
d05200d1 1466 gdb_assert (f.end == -1);
6eb1e6a8 1467 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
a8d2e585
SM
1468 printf_field_type_assignment
1469 ("tdesc_named_type (feature, \"%s\");\n", type_name);
6eb1e6a8 1470 printf_unfiltered
d4a0e8b5 1471 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
d05200d1 1472 f.name.c_str ());
6eb1e6a8
YQ
1473 }
1474 }
1475 break;
1476 case TDESC_TYPE_UNION:
1477 printf_unfiltered
d4a0e8b5 1478 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
082b9140 1479 type->name.c_str ());
d4a0e8b5 1480 for (const tdesc_type_field &f : type->fields)
6eb1e6a8 1481 {
a8d2e585
SM
1482 printf_field_type_assignment
1483 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
6eb1e6a8 1484 printf_unfiltered
d4a0e8b5 1485 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
d05200d1 1486 f.name.c_str ());
6eb1e6a8
YQ
1487 }
1488 break;
1489 case TDESC_TYPE_ENUM:
1490 printf_unfiltered
d4a0e8b5
SM
1491 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1492 type->name.c_str (), type->size);
1493 for (const tdesc_type_field &f : type->fields)
6eb1e6a8 1494 printf_unfiltered
d4a0e8b5 1495 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
d05200d1 1496 f.start, f.name.c_str ());
6eb1e6a8
YQ
1497 break;
1498 default:
082b9140 1499 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
6eb1e6a8 1500 }
d4a0e8b5 1501
6eb1e6a8
YQ
1502 printf_unfiltered ("\n");
1503 }
1504
1505 void visit (const tdesc_reg *reg) override
1506 {
1507 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
a8142ee1
SM
1508 reg->name.c_str (), reg->target_regnum,
1509 reg->save_restore);
1510 if (!reg->group.empty ())
1511 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
6eb1e6a8
YQ
1512 else
1513 printf_unfiltered ("NULL, ");
a8142ee1 1514 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
6eb1e6a8
YQ
1515 }
1516
25aa13e5
YQ
1517protected:
1518 std::string m_filename_after_features;
1519
6eb1e6a8 1520private:
a8d2e585
SM
1521
1522 /* Print an assignment to the field_type variable. Print the declaration
1523 of field_type if that has not been done yet. */
6e8c24fe 1524 ATTRIBUTE_PRINTF (2, 3)
a8d2e585
SM
1525 void printf_field_type_assignment (const char *fmt, ...)
1526 {
1527 if (!m_printed_field_type)
1528 {
1529 printf_unfiltered (" tdesc_type *field_type;\n");
1530 m_printed_field_type = true;
1531 }
1532
1533 printf_unfiltered (" field_type = ");
1534
1535 va_list args;
1536 va_start (args, fmt);
1537 vprintf_unfiltered (fmt, args);
1538 va_end (args);
1539 }
1540
6eb1e6a8 1541 char *m_function;
d4a0e8b5
SM
1542
1543 /* Did we print "struct tdesc_type *element_type;" yet? */
1544 bool m_printed_element_type = false;
1545
1546 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1547 bool m_printed_type_with_fields = false;
1548
1549 /* Did we print "struct tdesc_type *field_type;" yet? */
6eb1e6a8 1550 bool m_printed_field_type = false;
6eb1e6a8
YQ
1551};
1552
25aa13e5
YQ
1553/* Print target description feature in C. */
1554
1555class print_c_feature : public print_c_tdesc
1556{
1557public:
1558 print_c_feature (std::string &file)
1559 : print_c_tdesc (file)
1560 {
1561 /* Trim ".tmp". */
1562 auto const pos = m_filename_after_features.find_last_of ('.');
1563
1564 m_filename_after_features = m_filename_after_features.substr (0, pos);
1565 }
1566
1567 void visit_pre (const target_desc *e) override
1568 {
1569 printf_unfiltered (" Original: %s */\n\n",
1570 lbasename (m_filename_after_features.c_str ()));
1571
f46cd62a 1572 printf_unfiltered ("#include \"common/tdesc.h\"\n");
25aa13e5
YQ
1573 printf_unfiltered ("\n");
1574 }
1575
1576 void visit_post (const target_desc *e) override
1577 {}
1578
1579 void visit_pre (const tdesc_feature *e) override
1580 {
1581 std::string name (m_filename_after_features);
1582
1583 auto pos = name.find_first_of ('.');
1584
1585 name = name.substr (0, pos);
1586 std::replace (name.begin (), name.end (), '/', '_');
1587 std::replace (name.begin (), name.end (), '-', '_');
1588
1589 printf_unfiltered ("static int\n");
1590 printf_unfiltered ("create_feature_%s ", name.c_str ());
1591 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
1592
1593 printf_unfiltered ("{\n");
1594 printf_unfiltered (" struct tdesc_feature *feature;\n");
0abe8a89
YQ
1595
1596 printf_unfiltered
3b74854b
AH
1597 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1598 e->name.c_str ());
25aa13e5
YQ
1599 }
1600
1601 void visit_post (const tdesc_feature *e) override
1602 {
1603 printf_unfiltered (" return regnum;\n");
1604 printf_unfiltered ("}\n");
1605 }
1606
1607 void visit (const tdesc_reg *reg) override
1608 {
ea03d0d3
YQ
1609 /* Most "reg" in XML target descriptions don't have "regnum"
1610 attribute, so the register number is allocated sequentially.
1611 In case that reg has "regnum" attribute, register number
1612 should be set by that explicitly. */
1613
1614 if (reg->target_regnum < m_next_regnum)
1615 {
1616 /* The integrity check, it can catch some errors on register
1617 number collision, like this,
1618
1619 <reg name="x0" bitsize="32"/>
1620 <reg name="x1" bitsize="32"/>
1621 <reg name="x2" bitsize="32"/>
1622 <reg name="x3" bitsize="32"/>
1623 <reg name="ps" bitsize="32" regnum="3"/>
1624
1625 but it also has false negatives. The target description
1626 below is correct,
1627
1628 <reg name="x1" bitsize="32" regnum="1"/>
1629 <reg name="x3" bitsize="32" regnum="3"/>
1630 <reg name="x2" bitsize="32" regnum="2"/>
1631 <reg name="x4" bitsize="32" regnum="4"/>
1632
1633 but it is not a good practice, so still error on this,
1634 and also print the message so that it can be saved in the
1635 generated c file. */
1636
1637 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
1638 reg->target_regnum);
1639 printf_unfiltered ("is not the largest number (%d).\n",
1640 m_next_regnum);
1641 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1642 reg->target_regnum, m_next_regnum);
1643 }
1644
1645 if (reg->target_regnum > m_next_regnum)
1646 {
1647 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
1648 m_next_regnum = reg->target_regnum;
1649 }
1650
25aa13e5 1651 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
a8142ee1
SM
1652 reg->name.c_str (), reg->save_restore);
1653 if (!reg->group.empty ())
1654 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
25aa13e5
YQ
1655 else
1656 printf_unfiltered ("NULL, ");
a8142ee1 1657 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
ea03d0d3
YQ
1658
1659 m_next_regnum++;
25aa13e5
YQ
1660 }
1661
ea03d0d3
YQ
1662private:
1663 /* The register number to use for the next register we see. */
1664 int m_next_regnum = 0;
25aa13e5
YQ
1665};
1666
e98577a9
AH
1667/* See common/tdesc.h. */
1668
1669const char *
1670tdesc_get_features_xml (const target_desc *tdesc)
1671{
1672 if (tdesc->xmltarget == nullptr)
1673 {
1674 std::string buffer ("@");
1675 print_xml_feature v (&buffer);
1676 tdesc->accept (v);
1677 tdesc->xmltarget = xstrdup (buffer.c_str ());
1678 }
1679 return tdesc->xmltarget;
1680}
1681
81adfced 1682static void
e100df1a 1683maint_print_c_tdesc_cmd (const char *args, int from_tty)
81adfced
DJ
1684{
1685 const struct target_desc *tdesc;
6eb1e6a8 1686 const char *filename;
81adfced 1687
8e2141c6
YQ
1688 if (args == NULL)
1689 {
1690 /* Use the global target-supplied description, not the current
1691 architecture's. This lets a GDB for one architecture generate C
1692 for another architecture's description, even though the gdbarch
1693 initialization code will reject the new description. */
1694 tdesc = current_target_desc;
1695 filename = target_description_filename;
1696 }
1697 else
1698 {
1699 /* Use the target description from the XML file. */
1700 filename = args;
1701 tdesc = file_read_description_xml (filename);
1702 }
1703
81adfced
DJ
1704 if (tdesc == NULL)
1705 error (_("There is no target description to print."));
1706
8e2141c6 1707 if (filename == NULL)
81adfced
DJ
1708 error (_("The current target description did not come from an XML file."));
1709
6eb1e6a8
YQ
1710 std::string filename_after_features (filename);
1711 auto loc = filename_after_features.rfind ("/features/");
4e2f8df6 1712
6eb1e6a8
YQ
1713 if (loc != std::string::npos)
1714 filename_after_features = filename_after_features.substr (loc + 10);
4e2f8df6 1715
25aa13e5
YQ
1716 /* Print c files for target features instead of target descriptions,
1717 because c files got from target features are more flexible than the
1718 counterparts. */
6c73f67f
YQ
1719 if (startswith (filename_after_features.c_str (), "i386/32bit-")
1720 || startswith (filename_after_features.c_str (), "i386/64bit-")
506fe5f4 1721 || startswith (filename_after_features.c_str (), "i386/x32-core.xml")
49bdb7ee
AH
1722 || startswith (filename_after_features.c_str (), "tic6x-")
1723 || startswith (filename_after_features.c_str (), "aarch64"))
25aa13e5
YQ
1724 {
1725 print_c_feature v (filename_after_features);
81adfced 1726
25aa13e5
YQ
1727 tdesc->accept (v);
1728 }
1729 else
1730 {
1731 print_c_tdesc v (filename_after_features);
1732
1733 tdesc->accept (v);
1734 }
81adfced
DJ
1735}
1736
27d41eac
YQ
1737namespace selftests {
1738
1739static std::vector<std::pair<const char*, const target_desc *>> xml_tdesc;
1740
1741#if GDB_SELF_TEST
1742
1743/* See target-descritpions.h. */
1744
1745void
1746record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1747{
1748 xml_tdesc.emplace_back (xml_file, tdesc);
1749}
1750#endif
1751
1752}
1753
e98577a9
AH
1754/* Test the convesion process of a target description to/from xml: Take a target
1755 description TDESC, convert to xml, back to a description, and confirm the new
1756 tdesc is identical to the original. */
1757static bool
1758maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1759{
1760 const char *xml = tdesc_get_features_xml (tdesc);
1761
1762 if (xml == nullptr || *xml != '@')
1763 {
1764 printf_filtered (_("Could not convert description for %s to xml.\n"),
1765 name);
1766 return false;
1767 }
1768
1769 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1770
1771 if (tdesc_trans == nullptr)
1772 {
1773 printf_filtered (_("Could not convert description for %s from xml.\n"),
1774 name);
1775 return false;
1776 }
1777 else if (*tdesc != *tdesc_trans)
1778 {
1779 printf_filtered (_("Converted description for %s does not match.\n"),
1780 name);
1781 return false;
1782 }
1783 return true;
1784}
1785
1786
27d41eac
YQ
1787/* Check that the target descriptions created dynamically by
1788 architecture-specific code equal the descriptions created from XML files
1789 found in the specified directory DIR. */
1790
1791static void
e100df1a 1792maintenance_check_xml_descriptions (const char *dir, int from_tty)
27d41eac
YQ
1793{
1794 if (dir == NULL)
1795 error (_("Missing dir name"));
1796
1797 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1798 std::string feature_dir (dir1.get ());
1799 unsigned int failed = 0;
1800
1801 for (auto const &e : selftests::xml_tdesc)
1802 {
1803 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.first);
1804 const target_desc *tdesc
1805 = file_read_description_xml (tdesc_xml.data ());
1806
1807 if (tdesc == NULL || *tdesc != *e.second)
e98577a9
AH
1808 {
1809 printf_filtered ( _("Descriptions for %s do not match.\n"), e.first);
1810 failed++;
1811 }
1812 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.first)
1813 || !maintenance_check_tdesc_xml_convert (e.second, e.first))
27d41eac
YQ
1814 failed++;
1815 }
1816 printf_filtered (_("Tested %lu XML files, %d failed\n"),
1817 (long) selftests::xml_tdesc.size (), failed);
1818}
1819
23181151
DJ
1820void
1821_initialize_target_descriptions (void)
1822{
123dc839
DJ
1823 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1824
23181151
DJ
1825 add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\
1826Set target description specific variables."),
1827 &tdesc_set_cmdlist, "set tdesc ",
1828 0 /* allow-unknown */, &setlist);
1829 add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\
1830Show target description specific variables."),
1831 &tdesc_show_cmdlist, "show tdesc ",
1832 0 /* allow-unknown */, &showlist);
1833 add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\
1834Unset target description specific variables."),
1835 &tdesc_unset_cmdlist, "unset tdesc ",
1836 0 /* allow-unknown */, &unsetlist);
1837
1838 add_setshow_filename_cmd ("filename", class_obscure,
6ecd4729 1839 &tdesc_filename_cmd_string,
23181151
DJ
1840 _("\
1841Set the file to read for an XML target description"), _("\
1842Show the file to read for an XML target description"), _("\
1843When set, GDB will read the target description from a local\n\
1844file instead of querying the remote target."),
1845 set_tdesc_filename_cmd,
1846 show_tdesc_filename_cmd,
1847 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1848
1849 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1850Unset the file to read for an XML target description. When unset,\n\
1851GDB will read the description from the target."),
1852 &tdesc_unset_cmdlist);
81adfced
DJ
1853
1854 add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\
1855Print the current target description as a C source file."),
1856 &maintenanceprintlist);
27d41eac
YQ
1857
1858 cmd_list_element *cmd;
1859
1860 cmd = add_cmd ("xml-descriptions", class_maintenance,
1861 maintenance_check_xml_descriptions, _("\
1862Check the target descriptions created in GDB equal the descriptions\n\
1863created from XML files in the directory.\n\
1864The parameter is the directory name."),
1865 &maintenancechecklist);
1866 set_cmd_completer (cmd, filename_completer);
23181151 1867}
This page took 1.278466 seconds and 4 git commands to generate.