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