]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/target-descriptions.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / target-descriptions.c
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 "gdbtypes.h"
25 #include "reggroups.h"
26 #include "target.h"
27 #include "target-descriptions.h"
28 #include "xml-support.h"
29 #include "xml-tdesc.h"
30 #include "osabi.h"
31
32 #include "gdbsupport/gdb_obstack.h"
33 #include "hashtab.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 = &current_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 (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 = &current_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 (info))
541 internal_error (_("Could not remove target-supplied description"));
542 }
543
544 /* Return the global current target description. This should only be
545 used by gdbarch initialization code; most access should be through
546 an existing gdbarch. */
547
548 const struct target_desc *
549 target_current_description (void)
550 {
551 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
552
553 if (tdesc_info->fetched)
554 return tdesc_info->tdesc;
555
556 return NULL;
557 }
558
559 /* Return non-zero if this target description is compatible
560 with the given BFD architecture. */
561
562 int
563 tdesc_compatible_p (const struct target_desc *target_desc,
564 const struct bfd_arch_info *arch)
565 {
566 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
567 {
568 if (compat->arch () == arch
569 || arch->compatible (arch, compat->arch ())
570 || compat->arch ()->compatible (compat->arch (), arch))
571 return 1;
572 }
573
574 return 0;
575 }
576 \f
577
578 /* Direct accessors for target descriptions. */
579
580 /* Return the string value of a property named KEY, or NULL if the
581 property was not specified. */
582
583 const char *
584 tdesc_property (const struct target_desc *target_desc, const char *key)
585 {
586 for (const property &prop : target_desc->properties)
587 if (prop.key == key)
588 return prop.value.c_str ();
589
590 return NULL;
591 }
592
593 /* Return the BFD architecture associated with this target
594 description, or NULL if no architecture was specified. */
595
596 const struct bfd_arch_info *
597 tdesc_architecture (const struct target_desc *target_desc)
598 {
599 return target_desc->arch;
600 }
601
602 /* See gdbsupport/tdesc.h. */
603
604 const char *
605 tdesc_architecture_name (const struct target_desc *target_desc)
606 {
607 if (target_desc->arch != NULL)
608 return target_desc->arch->printable_name;
609 return NULL;
610 }
611
612 /* See gdbsupport/tdesc.h. */
613
614 const std::vector<tdesc_compatible_info_up> &
615 tdesc_compatible_info_list (const target_desc *target_desc)
616 {
617 return target_desc->compatible;
618 }
619
620 /* See gdbsupport/tdesc.h. */
621
622 const char *
623 tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
624 {
625 return compatible->arch ()->printable_name;
626 }
627
628 /* Return the OSABI associated with this target description, or
629 GDB_OSABI_UNKNOWN if no osabi was specified. */
630
631 enum gdb_osabi
632 tdesc_osabi (const struct target_desc *target_desc)
633 {
634 return target_desc->osabi;
635 }
636
637 /* See gdbsupport/tdesc.h. */
638
639 const char *
640 tdesc_osabi_name (const struct target_desc *target_desc)
641 {
642 enum gdb_osabi osabi = tdesc_osabi (target_desc);
643 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
644 return gdbarch_osabi_name (osabi);
645 return nullptr;
646 }
647
648 /* Return 1 if this target description includes any registers. */
649
650 int
651 tdesc_has_registers (const struct target_desc *target_desc)
652 {
653 if (target_desc == NULL)
654 return 0;
655
656 for (const tdesc_feature_up &feature : target_desc->features)
657 if (!feature->registers.empty ())
658 return 1;
659
660 return 0;
661 }
662
663 /* Return the feature with the given name, if present, or NULL if
664 the named feature is not found. */
665
666 const struct tdesc_feature *
667 tdesc_find_feature (const struct target_desc *target_desc,
668 const char *name)
669 {
670 for (const tdesc_feature_up &feature : target_desc->features)
671 if (feature->name == name)
672 return feature.get ();
673
674 return NULL;
675 }
676
677 /* Return the name of FEATURE. */
678
679 const char *
680 tdesc_feature_name (const struct tdesc_feature *feature)
681 {
682 return feature->name.c_str ();
683 }
684
685 /* Lookup type associated with ID. */
686
687 struct type *
688 tdesc_find_type (struct gdbarch *gdbarch, const char *id)
689 {
690 tdesc_arch_data *data = get_arch_data (gdbarch);
691
692 for (const tdesc_arch_reg &reg : data->arch_regs)
693 {
694 if (reg.reg
695 && reg.reg->tdesc_type
696 && reg.type
697 && reg.reg->tdesc_type->name == id)
698 return reg.type;
699 }
700
701 return NULL;
702 }
703
704 /* Support for registers from target descriptions. */
705
706 /* Construct the per-gdbarch data. */
707
708 tdesc_arch_data_up
709 tdesc_data_alloc (void)
710 {
711 return tdesc_arch_data_up (new tdesc_arch_data ());
712 }
713
714 /* See target-descriptions.h. */
715
716 void
717 tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
718 {
719 delete data;
720 }
721
722 /* Search FEATURE for a register named NAME. */
723
724 static struct tdesc_reg *
725 tdesc_find_register_early (const struct tdesc_feature *feature,
726 const char *name)
727 {
728 for (const tdesc_reg_up &reg : feature->registers)
729 if (strcasecmp (reg->name.c_str (), name) == 0)
730 return reg.get ();
731
732 return NULL;
733 }
734
735 /* Search FEATURE for a register named NAME. Assign REGNO to it. */
736
737 int
738 tdesc_numbered_register (const struct tdesc_feature *feature,
739 struct tdesc_arch_data *data,
740 int regno, const char *name)
741 {
742 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
743
744 if (reg == NULL)
745 return 0;
746
747 /* Make sure the vector includes a REGNO'th element. */
748 while (regno >= data->arch_regs.size ())
749 data->arch_regs.emplace_back (nullptr, nullptr);
750
751 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
752
753 return 1;
754 }
755
756 /* Search FEATURE for a register named NAME, but do not assign a fixed
757 register number to it. */
758
759 int
760 tdesc_unnumbered_register (const struct tdesc_feature *feature,
761 const char *name)
762 {
763 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
764
765 if (reg == NULL)
766 return 0;
767
768 return 1;
769 }
770
771 /* Search FEATURE for a register whose name is in NAMES and assign
772 REGNO to it. */
773
774 int
775 tdesc_numbered_register_choices (const struct tdesc_feature *feature,
776 struct tdesc_arch_data *data,
777 int regno, const char *const names[])
778 {
779 int i;
780
781 for (i = 0; names[i] != NULL; i++)
782 if (tdesc_numbered_register (feature, data, regno, names[i]))
783 return 1;
784
785 return 0;
786 }
787
788 /* See target-descriptions.h. */
789
790 bool
791 tdesc_found_register (struct tdesc_arch_data *data, int regno)
792 {
793 gdb_assert (regno >= 0);
794
795 return (regno < data->arch_regs.size ()
796 && data->arch_regs[regno].reg != nullptr);
797 }
798
799 /* Search FEATURE for a register named NAME, and return its size in
800 bits. The register must exist. */
801
802 int
803 tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
804 {
805 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
806
807 gdb_assert (reg != NULL);
808 return reg->bitsize;
809 }
810
811 /* Look up a register by its GDB internal register number. */
812
813 static struct tdesc_arch_reg *
814 tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
815 {
816 struct tdesc_arch_data *data = get_arch_data (gdbarch);
817
818 if (regno < data->arch_regs.size ())
819 return &data->arch_regs[regno];
820 else
821 return NULL;
822 }
823
824 static struct tdesc_reg *
825 tdesc_find_register (struct gdbarch *gdbarch, int regno)
826 {
827 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
828
829 return reg? reg->reg : NULL;
830 }
831
832 /* Return the name of register REGNO, from the target description or
833 from an architecture-provided pseudo_register_name method. */
834
835 const char *
836 tdesc_register_name (struct gdbarch *gdbarch, int regno)
837 {
838 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
839 int num_regs = gdbarch_num_regs (gdbarch);
840
841 if (reg != NULL)
842 return reg->name.c_str ();
843
844 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
845 {
846 struct tdesc_arch_data *data = get_arch_data (gdbarch);
847
848 gdb_assert (data->pseudo_register_name != NULL);
849 return data->pseudo_register_name (gdbarch, regno);
850 }
851
852 return "";
853 }
854
855 struct type *
856 tdesc_register_type (struct gdbarch *gdbarch, int regno)
857 {
858 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
859 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
860 int num_regs = gdbarch_num_regs (gdbarch);
861 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
862
863 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
864 {
865 struct tdesc_arch_data *data = get_arch_data (gdbarch);
866
867 gdb_assert (data->pseudo_register_type != NULL);
868 return data->pseudo_register_type (gdbarch, regno);
869 }
870
871 if (reg == NULL)
872 /* Return "int0_t", since "void" has a misleading size of one. */
873 return builtin_type (gdbarch)->builtin_int0;
874
875 if (arch_reg->type == NULL)
876 {
877 /* First check for a predefined or target defined type. */
878 if (reg->tdesc_type)
879 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
880
881 /* Next try size-sensitive type shortcuts. */
882 else if (reg->type == "float")
883 {
884 if (reg->bitsize == gdbarch_float_bit (gdbarch))
885 arch_reg->type = builtin_type (gdbarch)->builtin_float;
886 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
887 arch_reg->type = builtin_type (gdbarch)->builtin_double;
888 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
889 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
890 else
891 {
892 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
893 reg->name.c_str (), reg->bitsize);
894 arch_reg->type = builtin_type (gdbarch)->builtin_double;
895 }
896 }
897 else if (reg->type == "int")
898 {
899 if (reg->bitsize == gdbarch_long_bit (gdbarch))
900 arch_reg->type = builtin_type (gdbarch)->builtin_long;
901 else if (reg->bitsize == TARGET_CHAR_BIT)
902 arch_reg->type = builtin_type (gdbarch)->builtin_char;
903 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
904 arch_reg->type = builtin_type (gdbarch)->builtin_short;
905 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
906 arch_reg->type = builtin_type (gdbarch)->builtin_int;
907 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
908 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
909 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
910 /* A bit desperate by this point... */
911 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
912 else
913 {
914 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
915 reg->name.c_str (), reg->bitsize);
916 arch_reg->type = builtin_type (gdbarch)->builtin_long;
917 }
918 }
919
920 if (arch_reg->type == NULL)
921 internal_error ("Register \"%s\" has an unknown type \"%s\"",
922 reg->name.c_str (), reg->type.c_str ());
923 }
924
925 return arch_reg->type;
926 }
927
928 static int
929 tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
930 {
931 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
932
933 if (reg != NULL)
934 return reg->target_regnum;
935 else
936 return -1;
937 }
938
939 /* Check whether REGNUM is a member of REGGROUP. Registers from the
940 target description may be classified as general, float, vector or other
941 register groups registered with reggroup_add(). Unlike a gdbarch
942 register_reggroup_p method, this function will return -1 if it does not
943 know; the caller should handle registers with no specified group.
944
945 The names of containing features are not used. This might be extended
946 to display registers in some more useful groupings.
947
948 The save-restore flag is also implemented here. */
949
950 int
951 tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
952 const struct reggroup *reggroup)
953 {
954 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
955
956 if (reg != NULL && !reg->group.empty ()
957 && (reg->group == reggroup->name ()))
958 return 1;
959
960 if (reg != NULL
961 && (reggroup == save_reggroup || reggroup == restore_reggroup))
962 return reg->save_restore;
963
964 return -1;
965 }
966
967 /* Check whether REGNUM is a member of REGGROUP. Registers with no
968 group specified go to the default reggroup function and are handled
969 by type. */
970
971 static int
972 tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
973 const struct reggroup *reggroup)
974 {
975 int num_regs = gdbarch_num_regs (gdbarch);
976 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
977 int ret;
978
979 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
980 {
981 struct tdesc_arch_data *data = get_arch_data (gdbarch);
982
983 if (data->pseudo_register_reggroup_p != NULL)
984 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
985 /* Otherwise fall through to the default reggroup_p. */
986 }
987
988 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
989 if (ret != -1)
990 return ret;
991
992 return default_register_reggroup_p (gdbarch, regno, reggroup);
993 }
994
995 /* Record architecture-specific functions to call for pseudo-register
996 support. */
997
998 void
999 set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1000 gdbarch_register_name_ftype *pseudo_name)
1001 {
1002 struct tdesc_arch_data *data = get_arch_data (gdbarch);
1003
1004 data->pseudo_register_name = pseudo_name;
1005 }
1006
1007 void
1008 set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1009 gdbarch_register_type_ftype *pseudo_type)
1010 {
1011 struct tdesc_arch_data *data = get_arch_data (gdbarch);
1012
1013 data->pseudo_register_type = pseudo_type;
1014 }
1015
1016 void
1017 set_tdesc_pseudo_register_reggroup_p
1018 (struct gdbarch *gdbarch,
1019 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1020 {
1021 struct tdesc_arch_data *data = get_arch_data (gdbarch);
1022
1023 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1024 }
1025
1026 /* Update GDBARCH to use the target description for registers. */
1027
1028 void
1029 tdesc_use_registers (struct gdbarch *gdbarch,
1030 const struct target_desc *target_desc,
1031 tdesc_arch_data_up &&early_data,
1032 tdesc_unknown_register_ftype unk_reg_cb)
1033 {
1034 int num_regs = gdbarch_num_regs (gdbarch);
1035 struct tdesc_arch_data *data;
1036
1037 /* We can't use the description for registers if it doesn't describe
1038 any. This function should only be called after validating
1039 registers, so the caller should know that registers are
1040 included. */
1041 gdb_assert (tdesc_has_registers (target_desc));
1042
1043 data = get_arch_data (gdbarch);
1044 data->arch_regs = std::move (early_data->arch_regs);
1045
1046 /* Build up a set of all registers, so that we can assign register
1047 numbers where needed. The hash table expands as necessary, so
1048 the initial size is arbitrary. */
1049 htab_up reg_hash (htab_create (37, htab_hash_pointer, htab_eq_pointer,
1050 NULL));
1051 for (const tdesc_feature_up &feature : target_desc->features)
1052 for (const tdesc_reg_up &reg : feature->registers)
1053 {
1054 void **slot = htab_find_slot (reg_hash.get (), reg.get (), INSERT);
1055
1056 *slot = reg.get ();
1057 /* Add reggroup if its new. */
1058 if (!reg->group.empty ())
1059 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1060 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1061 reg->group.c_str (),
1062 USER_REGGROUP));
1063 }
1064
1065 /* Remove any registers which were assigned numbers by the
1066 architecture. */
1067 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1068 if (arch_reg.reg != NULL)
1069 htab_remove_elt (reg_hash.get (), arch_reg.reg);
1070
1071 /* Assign numbers to the remaining registers and add them to the
1072 list of registers. The new numbers are always above gdbarch_num_regs.
1073 Iterate over the features, not the hash table, so that the order
1074 matches that in the target description. */
1075
1076 gdb_assert (data->arch_regs.size () <= num_regs);
1077 while (data->arch_regs.size () < num_regs)
1078 data->arch_regs.emplace_back (nullptr, nullptr);
1079
1080 /* First we give the target a chance to number previously unknown
1081 registers. This allows targets to record the numbers assigned based
1082 on which feature the register was from. */
1083 if (unk_reg_cb != NULL)
1084 {
1085 for (const tdesc_feature_up &feature : target_desc->features)
1086 for (const tdesc_reg_up &reg : feature->registers)
1087 if (htab_find (reg_hash.get (), reg.get ()) != NULL)
1088 {
1089 int regno = unk_reg_cb (gdbarch, feature.get (),
1090 reg->name.c_str (), num_regs);
1091 gdb_assert (regno == -1 || regno >= num_regs);
1092 if (regno != -1)
1093 {
1094 while (regno >= data->arch_regs.size ())
1095 data->arch_regs.emplace_back (nullptr, nullptr);
1096 data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
1097 num_regs = regno + 1;
1098 htab_remove_elt (reg_hash.get (), reg.get ());
1099 }
1100 }
1101 }
1102
1103 /* Ensure the array was sized correctly above. */
1104 gdb_assert (data->arch_regs.size () == num_regs);
1105
1106 /* Now in a final pass we assign register numbers to any remaining
1107 unnumbered registers. */
1108 for (const tdesc_feature_up &feature : target_desc->features)
1109 for (const tdesc_reg_up &reg : feature->registers)
1110 if (htab_find (reg_hash.get (), reg.get ()) != NULL)
1111 {
1112 data->arch_regs.emplace_back (reg.get (), nullptr);
1113 num_regs++;
1114 }
1115
1116 /* Update the architecture. */
1117 set_gdbarch_num_regs (gdbarch, num_regs);
1118 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1119 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1120 set_gdbarch_remote_register_number (gdbarch,
1121 tdesc_remote_register_number);
1122 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1123 }
1124
1125 /* See gdbsupport/tdesc.h. */
1126
1127 struct tdesc_feature *
1128 tdesc_create_feature (struct target_desc *tdesc, const char *name)
1129 {
1130 struct tdesc_feature *new_feature = new tdesc_feature (name);
1131
1132 tdesc->features.emplace_back (new_feature);
1133
1134 return new_feature;
1135 }
1136
1137 /* See gdbsupport/tdesc.h. */
1138
1139 target_desc_up
1140 allocate_target_description (void)
1141 {
1142 return target_desc_up (new target_desc ());
1143 }
1144
1145 /* See gdbsupport/tdesc.h. */
1146
1147 void
1148 target_desc_deleter::operator() (struct target_desc *target_desc) const
1149 {
1150 delete target_desc;
1151 }
1152
1153 void
1154 tdesc_add_compatible (struct target_desc *target_desc,
1155 const struct bfd_arch_info *compatible)
1156 {
1157 /* If this instance of GDB is compiled without BFD support for the
1158 compatible architecture, simply ignore it -- we would not be able
1159 to handle it anyway. */
1160 if (compatible == NULL)
1161 return;
1162
1163 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
1164 if (compat->arch () == compatible)
1165 internal_error (_("Attempted to add duplicate "
1166 "compatible architecture \"%s\""),
1167 compatible->printable_name);
1168
1169 target_desc->compatible.push_back
1170 (std::unique_ptr<tdesc_compatible_info>
1171 (new tdesc_compatible_info (compatible)));
1172 }
1173
1174 void
1175 set_tdesc_property (struct target_desc *target_desc,
1176 const char *key, const char *value)
1177 {
1178 gdb_assert (key != NULL && value != NULL);
1179
1180 if (tdesc_property (target_desc, key) != NULL)
1181 internal_error (_("Attempted to add duplicate property \"%s\""), key);
1182
1183 target_desc->properties.emplace_back (key, value);
1184 }
1185
1186 /* See gdbsupport/tdesc.h. */
1187
1188 void
1189 set_tdesc_architecture (struct target_desc *target_desc,
1190 const char *name)
1191 {
1192 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1193 }
1194
1195 void
1196 set_tdesc_architecture (struct target_desc *target_desc,
1197 const struct bfd_arch_info *arch)
1198 {
1199 target_desc->arch = arch;
1200 }
1201
1202 /* See gdbsupport/tdesc.h. */
1203
1204 void
1205 set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1206 {
1207 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1208 }
1209
1210 void
1211 set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1212 {
1213 target_desc->osabi = osabi;
1214 }
1215 \f
1216
1217 static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1218 static struct cmd_list_element *tdesc_unset_cmdlist;
1219
1220 /* Helper functions for the CLI commands. */
1221
1222 static void
1223 set_tdesc_filename_cmd (const char *args, int from_tty,
1224 struct cmd_list_element *c)
1225 {
1226 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
1227
1228 tdesc_info->filename = tdesc_filename_cmd_string;
1229
1230 target_clear_description ();
1231 target_find_description ();
1232 }
1233
1234 static void
1235 show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1236 struct cmd_list_element *c,
1237 const char *value)
1238 {
1239 value = current_inferior ()->tdesc_info.filename.data ();
1240
1241 if (value != NULL && *value != '\0')
1242 gdb_printf (file,
1243 _("The target description will be read from \"%s\".\n"),
1244 value);
1245 else
1246 gdb_printf (file,
1247 _("The target description will be "
1248 "read from the target.\n"));
1249 }
1250
1251 static void
1252 unset_tdesc_filename_cmd (const char *args, int from_tty)
1253 {
1254 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
1255
1256 tdesc_info->filename.clear ();
1257 target_clear_description ();
1258 target_find_description ();
1259 }
1260
1261 /* Print target description in C. */
1262
1263 class print_c_tdesc : public tdesc_element_visitor
1264 {
1265 public:
1266 print_c_tdesc (std::string &filename_after_features)
1267 : m_filename_after_features (filename_after_features)
1268 {
1269 const char *inp;
1270 char *outp;
1271 const char *filename = lbasename (m_filename_after_features.c_str ());
1272
1273 m_function = (char *) xmalloc (strlen (filename) + 1);
1274 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1275 if (*inp == '.')
1276 break;
1277 else if (*inp == '-')
1278 *outp++ = '_';
1279 else if (*inp == ' ')
1280 *outp++ = '_';
1281 else
1282 *outp++ = *inp;
1283 *outp = '\0';
1284
1285 /* Standard boilerplate. */
1286 gdb_printf ("/* THIS FILE IS GENERATED. "
1287 "-*- buffer-read-only: t -*- vi"
1288 ":set ro:\n");
1289 }
1290
1291 ~print_c_tdesc ()
1292 {
1293 xfree (m_function);
1294 }
1295
1296 void visit_pre (const target_desc *e) override
1297 {
1298 gdb_printf (" Original: %s */\n\n",
1299 lbasename (m_filename_after_features.c_str ()));
1300
1301 gdb_printf ("#include \"osabi.h\"\n");
1302 gdb_printf ("#include \"target-descriptions.h\"\n");
1303 gdb_printf ("\n");
1304
1305 gdb_printf ("const struct target_desc *tdesc_%s;\n", m_function);
1306 gdb_printf ("static void\n");
1307 gdb_printf ("initialize_tdesc_%s (void)\n", m_function);
1308 gdb_printf ("{\n");
1309 gdb_printf
1310 (" target_desc_up result = allocate_target_description ();\n");
1311
1312 if (tdesc_architecture (e) != NULL)
1313 {
1314 gdb_printf
1315 (" set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
1316 tdesc_architecture (e)->printable_name);
1317 gdb_printf ("\n");
1318 }
1319 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1320 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1321 {
1322 gdb_printf
1323 (" set_tdesc_osabi (result.get (), osabi_from_tdesc_string (\"%s\"));\n",
1324 gdbarch_osabi_name (tdesc_osabi (e)));
1325 gdb_printf ("\n");
1326 }
1327
1328 for (const tdesc_compatible_info_up &compatible : e->compatible)
1329 gdb_printf
1330 (" tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
1331 compatible->arch ()->printable_name);
1332
1333 if (!e->compatible.empty ())
1334 gdb_printf ("\n");
1335
1336 for (const property &prop : e->properties)
1337 gdb_printf (" set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
1338 prop.key.c_str (), prop.value.c_str ());
1339
1340 gdb_printf (" struct tdesc_feature *feature;\n");
1341 }
1342
1343 void visit_pre (const tdesc_feature *e) override
1344 {
1345 gdb_printf ("\n feature = tdesc_create_feature (result.get (), \"%s\");\n",
1346 e->name.c_str ());
1347 }
1348
1349 void visit_post (const tdesc_feature *e) override
1350 {}
1351
1352 void visit_post (const target_desc *e) override
1353 {
1354 gdb_printf ("\n tdesc_%s = result.release ();\n", m_function);
1355 gdb_printf ("}\n");
1356 }
1357
1358 void visit (const tdesc_type_builtin *type) override
1359 {
1360 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1361 }
1362
1363 void visit (const tdesc_type_vector *type) override
1364 {
1365 if (!m_printed_element_type)
1366 {
1367 gdb_printf (" tdesc_type *element_type;\n");
1368 m_printed_element_type = true;
1369 }
1370
1371 gdb_printf
1372 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1373 type->element_type->name.c_str ());
1374 gdb_printf
1375 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1376 type->name.c_str (), type->count);
1377
1378 gdb_printf ("\n");
1379 }
1380
1381 void visit (const tdesc_type_with_fields *type) override
1382 {
1383 if (!m_printed_type_with_fields)
1384 {
1385 gdb_printf (" tdesc_type_with_fields *type_with_fields;\n");
1386 m_printed_type_with_fields = true;
1387 }
1388
1389 switch (type->kind)
1390 {
1391 case TDESC_TYPE_STRUCT:
1392 case TDESC_TYPE_FLAGS:
1393 if (type->kind == TDESC_TYPE_STRUCT)
1394 {
1395 gdb_printf
1396 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
1397 type->name.c_str ());
1398 if (type->size != 0)
1399 gdb_printf
1400 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
1401 }
1402 else
1403 {
1404 gdb_printf
1405 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1406 type->name.c_str (), type->size);
1407 }
1408 for (const tdesc_type_field &f : type->fields)
1409 {
1410 const char *type_name;
1411
1412 gdb_assert (f.type != NULL);
1413 type_name = f.type->name.c_str ();
1414
1415 /* To minimize changes to generated files, don't emit type
1416 info for fields that have defaulted types. */
1417 if (f.start != -1)
1418 {
1419 gdb_assert (f.end != -1);
1420 if (f.type->kind == TDESC_TYPE_BOOL)
1421 {
1422 gdb_assert (f.start == f.end);
1423 gdb_printf
1424 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
1425 f.start, f.name.c_str ());
1426 }
1427 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1428 || (type->size == 8
1429 && f.type->kind == TDESC_TYPE_UINT64))
1430 {
1431 gdb_printf
1432 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
1433 f.name.c_str (), f.start, f.end);
1434 }
1435 else
1436 {
1437 printf_field_type_assignment
1438 ("tdesc_named_type (feature, \"%s\");\n",
1439 type_name);
1440 gdb_printf
1441 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
1442 " %d, %d, field_type);\n",
1443 f.name.c_str (), f.start, f.end);
1444 }
1445 }
1446 else /* Not a bitfield. */
1447 {
1448 gdb_assert (f.end == -1);
1449 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
1450 printf_field_type_assignment
1451 ("tdesc_named_type (feature, \"%s\");\n", type_name);
1452 gdb_printf
1453 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1454 f.name.c_str ());
1455 }
1456 }
1457 break;
1458 case TDESC_TYPE_UNION:
1459 gdb_printf
1460 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
1461 type->name.c_str ());
1462 for (const tdesc_type_field &f : type->fields)
1463 {
1464 printf_field_type_assignment
1465 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
1466 gdb_printf
1467 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
1468 f.name.c_str ());
1469 }
1470 break;
1471 case TDESC_TYPE_ENUM:
1472 gdb_printf
1473 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1474 type->name.c_str (), type->size);
1475 for (const tdesc_type_field &f : type->fields)
1476 gdb_printf
1477 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
1478 f.start, f.name.c_str ());
1479 break;
1480 default:
1481 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1482 }
1483
1484 gdb_printf ("\n");
1485 }
1486
1487 void visit (const tdesc_reg *reg) override
1488 {
1489 gdb_printf (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
1490 reg->name.c_str (), reg->target_regnum,
1491 reg->save_restore);
1492 if (!reg->group.empty ())
1493 gdb_printf ("\"%s\", ", reg->group.c_str ());
1494 else
1495 gdb_printf ("NULL, ");
1496 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1497 }
1498
1499 protected:
1500 std::string m_filename_after_features;
1501
1502 private:
1503
1504 /* Print an assignment to the field_type variable. Print the declaration
1505 of field_type if that has not been done yet. */
1506 ATTRIBUTE_PRINTF (2, 3)
1507 void printf_field_type_assignment (const char *fmt, ...)
1508 {
1509 if (!m_printed_field_type)
1510 {
1511 gdb_printf (" tdesc_type *field_type;\n");
1512 m_printed_field_type = true;
1513 }
1514
1515 gdb_printf (" field_type = ");
1516
1517 va_list args;
1518 va_start (args, fmt);
1519 gdb_vprintf (fmt, args);
1520 va_end (args);
1521 }
1522
1523 char *m_function;
1524
1525 /* Did we print "struct tdesc_type *element_type;" yet? */
1526 bool m_printed_element_type = false;
1527
1528 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1529 bool m_printed_type_with_fields = false;
1530
1531 /* Did we print "struct tdesc_type *field_type;" yet? */
1532 bool m_printed_field_type = false;
1533 };
1534
1535 /* Print target description feature in C. */
1536
1537 class print_c_feature : public print_c_tdesc
1538 {
1539 public:
1540 print_c_feature (std::string &file)
1541 : print_c_tdesc (file)
1542 {
1543 /* Trim ".tmp". */
1544 auto const pos = m_filename_after_features.find_last_of ('.');
1545
1546 m_filename_after_features = m_filename_after_features.substr (0, pos);
1547 }
1548
1549 void visit_pre (const target_desc *e) override
1550 {
1551 gdb_printf (" Original: %s */\n\n",
1552 lbasename (m_filename_after_features.c_str ()));
1553
1554 gdb_printf ("#include \"gdbsupport/tdesc.h\"\n");
1555 gdb_printf ("\n");
1556 }
1557
1558 void visit_post (const target_desc *e) override
1559 {}
1560
1561 void visit_pre (const tdesc_feature *e) override
1562 {
1563 std::string name (m_filename_after_features);
1564
1565 auto pos = name.find_first_of ('.');
1566
1567 name = name.substr (0, pos);
1568 std::replace (name.begin (), name.end (), '/', '_');
1569 std::replace (name.begin (), name.end (), '-', '_');
1570
1571 gdb_printf ("static int\n");
1572 gdb_printf ("create_feature_%s ", name.c_str ());
1573 gdb_printf ("(struct target_desc *result, long regnum)\n");
1574
1575 gdb_printf ("{\n");
1576 gdb_printf (" struct tdesc_feature *feature;\n");
1577
1578 gdb_printf
1579 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1580 e->name.c_str ());
1581 }
1582
1583 void visit_post (const tdesc_feature *e) override
1584 {
1585 gdb_printf (" return regnum;\n");
1586 gdb_printf ("}\n");
1587 }
1588
1589 void visit (const tdesc_reg *reg) override
1590 {
1591 /* Most "reg" in XML target descriptions don't have "regnum"
1592 attribute, so the register number is allocated sequentially.
1593 In case that reg has "regnum" attribute, register number
1594 should be set by that explicitly. */
1595
1596 if (reg->target_regnum < m_next_regnum)
1597 {
1598 /* The integrity check, it can catch some errors on register
1599 number collision, like this,
1600
1601 <reg name="x0" bitsize="32"/>
1602 <reg name="x1" bitsize="32"/>
1603 <reg name="x2" bitsize="32"/>
1604 <reg name="x3" bitsize="32"/>
1605 <reg name="ps" bitsize="32" regnum="3"/>
1606
1607 but it also has false negatives. The target description
1608 below is correct,
1609
1610 <reg name="x1" bitsize="32" regnum="1"/>
1611 <reg name="x3" bitsize="32" regnum="3"/>
1612 <reg name="x2" bitsize="32" regnum="2"/>
1613 <reg name="x4" bitsize="32" regnum="4"/>
1614
1615 but it is not a good practice, so still error on this,
1616 and also print the message so that it can be saved in the
1617 generated c file. */
1618
1619 gdb_printf ("ERROR: \"regnum\" attribute %ld ",
1620 reg->target_regnum);
1621 gdb_printf ("is not the largest number (%d).\n",
1622 m_next_regnum);
1623 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1624 reg->target_regnum, m_next_regnum);
1625 }
1626
1627 if (reg->target_regnum > m_next_regnum)
1628 {
1629 gdb_printf (" regnum = %ld;\n", reg->target_regnum);
1630 m_next_regnum = reg->target_regnum;
1631 }
1632
1633 gdb_printf (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
1634 reg->name.c_str (), reg->save_restore);
1635 if (!reg->group.empty ())
1636 gdb_printf ("\"%s\", ", reg->group.c_str ());
1637 else
1638 gdb_printf ("NULL, ");
1639 gdb_printf ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
1640
1641 m_next_regnum++;
1642 }
1643
1644 private:
1645 /* The register number to use for the next register we see. */
1646 int m_next_regnum = 0;
1647 };
1648
1649 /* See gdbsupport/tdesc.h. */
1650
1651 const char *
1652 tdesc_get_features_xml (const target_desc *tdesc)
1653 {
1654 if (tdesc->xmltarget == nullptr)
1655 {
1656 std::string buffer ("@");
1657 print_xml_feature v (&buffer);
1658 tdesc->accept (v);
1659 tdesc->xmltarget = xstrdup (buffer.c_str ());
1660 }
1661 return tdesc->xmltarget;
1662 }
1663
1664 /* Data structures and functions to setup the option flags for 'maintenance
1665 print c-tdesc command. */
1666
1667 struct maint_print_c_tdesc_options
1668 {
1669 /* True when the '-single-feature' flag was passed. */
1670 bool single_feature = false;
1671 };
1672
1673 using maint_print_c_tdesc_opt_def
1674 = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
1675
1676 static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
1677 maint_print_c_tdesc_opt_def {
1678 "single-feature",
1679 [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
1680 N_("Print C description of just a single feature.")
1681 },
1682 };
1683
1684 static inline gdb::option::option_def_group
1685 make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
1686 {
1687 return {{maint_print_c_tdesc_opt_defs}, opts};
1688 }
1689
1690 /* Implement 'maintenance print c-tdesc' command. */
1691
1692 static void
1693 maint_print_c_tdesc_cmd (const char *args, int from_tty)
1694 {
1695 const struct target_desc *tdesc;
1696 const char *filename;
1697
1698 maint_print_c_tdesc_options opts;
1699 auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
1700 gdb::option::process_options
1701 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1702
1703 if (args == NULL)
1704 {
1705 /* Use the global target-supplied description, not the current
1706 architecture's. This lets a GDB for one architecture generate C
1707 for another architecture's description, even though the gdbarch
1708 initialization code will reject the new description. */
1709 target_desc_info *tdesc_info = &current_inferior ()->tdesc_info;
1710 tdesc = tdesc_info->tdesc;
1711 filename = tdesc_info->filename.data ();
1712 }
1713 else
1714 {
1715 /* Use the target description from the XML file. */
1716 filename = args;
1717 tdesc = file_read_description_xml (filename);
1718 }
1719
1720 if (tdesc == NULL)
1721 error (_("There is no target description to print."));
1722
1723 if (filename == NULL)
1724 filename = "fetched from target";
1725
1726 std::string filename_after_features (filename);
1727 auto loc = filename_after_features.rfind ("/features/");
1728
1729 if (loc != std::string::npos)
1730 filename_after_features = filename_after_features.substr (loc + 10);
1731
1732 /* Print c files for target features instead of target descriptions,
1733 because c files got from target features are more flexible than the
1734 counterparts. */
1735 if (opts.single_feature)
1736 {
1737 if (tdesc->features.size () != 1)
1738 error (_("only target descriptions with 1 feature can be used "
1739 "with -single-feature option"));
1740
1741 print_c_feature v (filename_after_features);
1742
1743 tdesc->accept (v);
1744 }
1745 else
1746 {
1747 print_c_tdesc v (filename_after_features);
1748
1749 tdesc->accept (v);
1750 }
1751 }
1752
1753 /* Completer for the "backtrace" command. */
1754
1755 static void
1756 maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
1757 completion_tracker &tracker,
1758 const char *text, const char *word)
1759 {
1760 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1761 if (gdb::option::complete_options
1762 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1763 return;
1764
1765 word = advance_to_filename_complete_word_point (tracker, text);
1766 filename_completer (ignore, tracker, text, word);
1767 }
1768
1769 /* Implement the maintenance print xml-tdesc command. */
1770
1771 static void
1772 maint_print_xml_tdesc_cmd (const char *args, int from_tty)
1773 {
1774 const struct target_desc *tdesc;
1775
1776 if (args == NULL)
1777 {
1778 /* Use the global target-supplied description, not the current
1779 architecture's. This lets a GDB for one architecture generate XML
1780 for another architecture's description, even though the gdbarch
1781 initialization code will reject the new description. */
1782 tdesc = current_inferior ()->tdesc_info.tdesc;
1783 }
1784 else
1785 {
1786 /* Use the target description from the XML file. */
1787 tdesc = file_read_description_xml (args);
1788 }
1789
1790 if (tdesc == NULL)
1791 error (_("There is no target description to print."));
1792
1793 std::string buf;
1794 print_xml_feature v (&buf);
1795 tdesc->accept (v);
1796 gdb_puts (buf.c_str ());
1797 }
1798
1799 namespace selftests {
1800
1801 /* A reference target description, used for testing (see record_xml_tdesc). */
1802
1803 struct xml_test_tdesc
1804 {
1805 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
1806 : name (name), tdesc (std::move (tdesc))
1807 {}
1808
1809 const char *name;
1810 std::unique_ptr<const target_desc> tdesc;
1811 };
1812
1813 static std::vector<xml_test_tdesc> xml_tdesc;
1814
1815 #if GDB_SELF_TEST
1816
1817 /* See target-descriptions.h. */
1818
1819 void
1820 record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1821 {
1822 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
1823 }
1824 #endif
1825
1826 }
1827
1828 /* Test the conversion process of a target description to/from xml: Take a target
1829 description TDESC, convert to xml, back to a description, and confirm the new
1830 tdesc is identical to the original. */
1831 static bool
1832 maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1833 {
1834 const char *xml = tdesc_get_features_xml (tdesc);
1835
1836 if (xml == nullptr || *xml != '@')
1837 {
1838 gdb_printf (_("Could not convert description for %s to xml.\n"),
1839 name);
1840 return false;
1841 }
1842
1843 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1844
1845 if (tdesc_trans == nullptr)
1846 {
1847 gdb_printf (_("Could not convert description for %s from xml.\n"),
1848 name);
1849 return false;
1850 }
1851 else if (*tdesc != *tdesc_trans)
1852 {
1853 gdb_printf (_("Converted description for %s does not match.\n"),
1854 name);
1855 return false;
1856 }
1857 return true;
1858 }
1859
1860
1861 /* Check that the target descriptions created dynamically by
1862 architecture-specific code equal the descriptions created from XML files
1863 found in the specified directory DIR. */
1864
1865 static void
1866 maintenance_check_xml_descriptions (const char *dir, int from_tty)
1867 {
1868 if (dir == NULL)
1869 error (_("Missing dir name"));
1870
1871 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1872 std::string feature_dir (dir1.get ());
1873 unsigned int failed = 0;
1874
1875 for (auto const &e : selftests::xml_tdesc)
1876 {
1877 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
1878 const target_desc *tdesc
1879 = file_read_description_xml (tdesc_xml.data ());
1880
1881 if (tdesc == NULL || *tdesc != *e.tdesc)
1882 {
1883 gdb_printf ( _("Descriptions for %s do not match.\n"), e.name);
1884 failed++;
1885 }
1886 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
1887 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
1888 failed++;
1889 }
1890 gdb_printf (_("Tested %lu XML files, %d failed\n"),
1891 (long) selftests::xml_tdesc.size (), failed);
1892 }
1893
1894 void _initialize_target_descriptions ();
1895 void
1896 _initialize_target_descriptions ()
1897 {
1898 cmd_list_element *cmd;
1899
1900 add_setshow_prefix_cmd ("tdesc", class_maintenance,
1901 _("Set target description specific variables."),
1902 _("Show target description specific variables."),
1903 &tdesc_set_cmdlist, &tdesc_show_cmdlist,
1904 &setlist, &showlist);
1905
1906 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
1907 Unset target description specific variables."),
1908 &tdesc_unset_cmdlist,
1909 0 /* allow-unknown */, &unsetlist);
1910
1911 add_setshow_filename_cmd ("filename", class_obscure,
1912 &tdesc_filename_cmd_string,
1913 _("\
1914 Set the file to read for an XML target description."), _("\
1915 Show the file to read for an XML target description."), _("\
1916 When set, GDB will read the target description from a local\n\
1917 file instead of querying the remote target."),
1918 set_tdesc_filename_cmd,
1919 show_tdesc_filename_cmd,
1920 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1921
1922 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
1923 Unset the file to read for an XML target description.\n\
1924 When unset, GDB will read the description from the target."),
1925 &tdesc_unset_cmdlist);
1926
1927 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1928 static std::string help_text
1929 = gdb::option::build_help (_("\
1930 Print the current target description as a C source file.\n\
1931 Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
1932 \n\
1933 Options:\n\
1934 %OPTIONS%\n\
1935 \n\
1936 When FILENAME is not provided then print the current target\n\
1937 description, otherwise an XML target description is read from\n\
1938 FILENAME and printed as a C function.\n\
1939 \n\
1940 When '-single-feature' is used then the target description should\n\
1941 contain a single feature and the generated C code will only create\n\
1942 that feature within an already existing target_desc object."), grp);
1943 cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
1944 help_text.c_str (), &maintenanceprintlist);
1945 set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
1946
1947 cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
1948 Print the current target description as an XML file."),
1949 &maintenanceprintlist);
1950 set_cmd_completer (cmd, filename_completer);
1951
1952 cmd = add_cmd ("xml-descriptions", class_maintenance,
1953 maintenance_check_xml_descriptions, _("\
1954 Check equality of GDB target descriptions and XML created descriptions.\n\
1955 Check the target descriptions created in GDB equal the descriptions\n\
1956 created from XML files in the directory.\n\
1957 The parameter is the directory name."),
1958 &maintenancechecklist);
1959 set_cmd_completer (cmd, filename_completer);
1960 }