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