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