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