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