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