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