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