Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / target-descriptions.c
CommitLineData
424163ea
DJ
1/* Target description support for GDB.
2
3666a048 3 Copyright (C) 2006-2021 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
0b2f7ade 449 bool fetched = false;
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
c2962e6a 453 FETCHED is set. Only the description initialization
6ecd4729
PA
454 code should access this; normally, the description should be
455 accessed through the gdbarch object. */
424163ea 456
0b2f7ade 457 const struct target_desc *tdesc = nullptr;
424163ea 458
91e3c425
SM
459 /* If not empty, the filename to read a target description from, as set by
460 "set tdesc filename ...".
424163ea 461
91e3c425
SM
462 If empty, there is not filename specified by the user. */
463
464 std::string filename;
6ecd4729 465};
23181151 466
6ecd4729
PA
467/* Get the inferior INF's target description info, allocating one on
468 the stop if necessary. */
23181151 469
6ecd4729
PA
470static struct target_desc_info *
471get_tdesc_info (struct inferior *inf)
472{
473 if (inf->tdesc_info == NULL)
0b2f7ade
SM
474 inf->tdesc_info = new target_desc_info;
475
6ecd4729
PA
476 return inf->tdesc_info;
477}
23181151 478
123dc839
DJ
479/* A handle for architecture-specific data associated with the
480 target description (see struct tdesc_arch_data). */
481
482static struct gdbarch_data *tdesc_data;
483
6ecd4729
PA
484/* See target-descriptions.h. */
485
486int
487target_desc_info_from_user_p (struct target_desc_info *info)
488{
91e3c425 489 return info != nullptr && !info->filename.empty ();
6ecd4729
PA
490}
491
492/* See target-descriptions.h. */
493
494void
495copy_inferior_target_desc_info (struct inferior *destinf, struct inferior *srcinf)
496{
497 struct target_desc_info *src = get_tdesc_info (srcinf);
498 struct target_desc_info *dest = get_tdesc_info (destinf);
499
91e3c425 500 *dest = *src;
6ecd4729
PA
501}
502
503/* See target-descriptions.h. */
504
505void
506target_desc_info_free (struct target_desc_info *tdesc_info)
507{
91e3c425 508 delete tdesc_info;
6ecd4729
PA
509}
510
6ecd4729
PA
511/* The string manipulated by the "set tdesc filename ..." command. */
512
513static char *tdesc_filename_cmd_string;
514
424163ea
DJ
515/* Fetch the current target's description, and switch the current
516 architecture to one which incorporates that description. */
517
518void
519target_find_description (void)
520{
c2962e6a
SM
521 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
522
424163ea
DJ
523 /* If we've already fetched a description from the target, don't do
524 it again. This allows a target to fetch the description early,
525 during its to_open or to_create_inferior, if it needs extra
526 information about the target to initialize. */
c2962e6a 527 if (tdesc_info->fetched)
424163ea
DJ
528 return;
529
530 /* The current architecture should not have any target description
531 specified. It should have been cleared, e.g. when we
532 disconnected from the previous target. */
f5656ead 533 gdb_assert (gdbarch_target_desc (target_gdbarch ()) == NULL);
424163ea 534
23181151
DJ
535 /* First try to fetch an XML description from the user-specified
536 file. */
c2962e6a 537 tdesc_info->tdesc = nullptr;
91e3c425
SM
538 if (!tdesc_info->filename.empty ())
539 tdesc_info->tdesc = file_read_description_xml (tdesc_info->filename.data ());
23181151
DJ
540
541 /* Next try to read the description from the current target using
542 target objects. */
c2962e6a
SM
543 if (tdesc_info->tdesc == nullptr)
544 tdesc_info->tdesc = target_read_description_xml
328d42d8 545 (current_inferior ()->top_target ());
23181151
DJ
546
547 /* If that failed try a target-specific hook. */
c2962e6a
SM
548 if (tdesc_info->tdesc == nullptr)
549 tdesc_info->tdesc = target_read_description
328d42d8 550 (current_inferior ()->top_target ());
424163ea
DJ
551
552 /* If a non-NULL description was returned, then update the current
553 architecture. */
c2962e6a 554 if (tdesc_info->tdesc != nullptr)
424163ea
DJ
555 {
556 struct gdbarch_info info;
557
c2962e6a 558 info.target_desc = tdesc_info->tdesc;
424163ea 559 if (!gdbarch_update_p (info))
123dc839
DJ
560 warning (_("Architecture rejected target-supplied description"));
561 else
562 {
563 struct tdesc_arch_data *data;
564
19ba03f4
SM
565 data = ((struct tdesc_arch_data *)
566 gdbarch_data (target_gdbarch (), tdesc_data));
c2962e6a 567 if (tdesc_has_registers (tdesc_info->tdesc)
f0cddbef 568 && data->arch_regs.empty ())
123dc839
DJ
569 warning (_("Target-supplied registers are not supported "
570 "by the current architecture"));
571 }
424163ea
DJ
572 }
573
574 /* Now that we know this description is usable, record that we
575 fetched it. */
820c4490 576 tdesc_info->fetched = true;
424163ea
DJ
577}
578
579/* Discard any description fetched from the current target, and switch
580 the current architecture to one with no target description. */
581
582void
583target_clear_description (void)
584{
c2962e6a 585 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
424163ea 586
c2962e6a 587 if (!tdesc_info->fetched)
424163ea
DJ
588 return;
589
820c4490 590 tdesc_info->fetched = false;
c2962e6a 591 tdesc_info->tdesc = nullptr;
424163ea 592
c2962e6a 593 gdbarch_info info;
424163ea
DJ
594 if (!gdbarch_update_p (info))
595 internal_error (__FILE__, __LINE__,
596 _("Could not remove target-supplied description"));
597}
598
599/* Return the global current target description. This should only be
600 used by gdbarch initialization code; most access should be through
601 an existing gdbarch. */
602
603const struct target_desc *
604target_current_description (void)
605{
c2962e6a
SM
606 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
607
608 if (tdesc_info->fetched)
609 return tdesc_info->tdesc;
424163ea
DJ
610
611 return NULL;
612}
e35359c5
UW
613
614/* Return non-zero if this target description is compatible
615 with the given BFD architecture. */
616
617int
618tdesc_compatible_p (const struct target_desc *target_desc,
619 const struct bfd_arch_info *arch)
620{
fbf42f4e 621 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
e35359c5 622 {
fbf42f4e
AB
623 if (compat->arch () == arch
624 || arch->compatible (arch, compat->arch ())
625 || compat->arch ()->compatible (compat->arch (), arch))
e35359c5
UW
626 return 1;
627 }
628
629 return 0;
630}
23181151
DJ
631\f
632
123dc839 633/* Direct accessors for target descriptions. */
424163ea 634
29709017
DJ
635/* Return the string value of a property named KEY, or NULL if the
636 property was not specified. */
637
638const char *
639tdesc_property (const struct target_desc *target_desc, const char *key)
640{
129c10bc
SM
641 for (const property &prop : target_desc->properties)
642 if (prop.key == key)
643 return prop.value.c_str ();
29709017
DJ
644
645 return NULL;
646}
647
23181151
DJ
648/* Return the BFD architecture associated with this target
649 description, or NULL if no architecture was specified. */
650
651const struct bfd_arch_info *
652tdesc_architecture (const struct target_desc *target_desc)
653{
654 return target_desc->arch;
655}
08d16641 656
268a13a5 657/* See gdbsupport/tdesc.h. */
d278f585
AH
658
659const char *
660tdesc_architecture_name (const struct target_desc *target_desc)
661{
caa7fd04
AB
662 if (target_desc->arch != NULL)
663 return target_desc->arch->printable_name;
664 return NULL;
d278f585
AH
665}
666
fbf42f4e
AB
667/* See gdbsupport/tdesc.h. */
668
669const std::vector<tdesc_compatible_info_up> &
670tdesc_compatible_info_list (const target_desc *target_desc)
671{
672 return target_desc->compatible;
673}
674
675/* See gdbsupport/tdesc.h. */
676
677const char *
678tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &compatible)
679{
680 return compatible->arch ()->printable_name;
681}
682
08d16641
PA
683/* Return the OSABI associated with this target description, or
684 GDB_OSABI_UNKNOWN if no osabi was specified. */
685
686enum gdb_osabi
687tdesc_osabi (const struct target_desc *target_desc)
688{
689 return target_desc->osabi;
690}
691
268a13a5 692/* See gdbsupport/tdesc.h. */
d278f585
AH
693
694const char *
695tdesc_osabi_name (const struct target_desc *target_desc)
696{
697 enum gdb_osabi osabi = tdesc_osabi (target_desc);
698 if (osabi > GDB_OSABI_UNKNOWN && osabi < GDB_OSABI_INVALID)
699 return gdbarch_osabi_name (osabi);
700 return nullptr;
701}
23181151 702
123dc839
DJ
703/* Return 1 if this target description includes any registers. */
704
705int
706tdesc_has_registers (const struct target_desc *target_desc)
707{
123dc839
DJ
708 if (target_desc == NULL)
709 return 0;
710
3eea796c 711 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 712 if (!feature->registers.empty ())
123dc839
DJ
713 return 1;
714
715 return 0;
716}
717
718/* Return the feature with the given name, if present, or NULL if
719 the named feature is not found. */
720
721const struct tdesc_feature *
722tdesc_find_feature (const struct target_desc *target_desc,
723 const char *name)
724{
3eea796c 725 for (const tdesc_feature_up &feature : target_desc->features)
f65ff9f9 726 if (feature->name == name)
3eea796c 727 return feature.get ();
123dc839
DJ
728
729 return NULL;
730}
731
732/* Return the name of FEATURE. */
733
734const char *
735tdesc_feature_name (const struct tdesc_feature *feature)
736{
f65ff9f9 737 return feature->name.c_str ();
123dc839
DJ
738}
739
9fd3625f
L
740/* Lookup type associated with ID. */
741
742struct type *
743tdesc_find_type (struct gdbarch *gdbarch, const char *id)
744{
f0cddbef
SM
745 tdesc_arch_data *data
746 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
9fd3625f 747
f0cddbef 748 for (const tdesc_arch_reg &reg : data->arch_regs)
9fd3625f 749 {
f0cddbef
SM
750 if (reg.reg
751 && reg.reg->tdesc_type
752 && reg.type
753 && reg.reg->tdesc_type->name == id)
754 return reg.type;
9fd3625f
L
755 }
756
757 return NULL;
758}
759
123dc839
DJ
760/* Support for registers from target descriptions. */
761
762/* Construct the per-gdbarch data. */
763
764static void *
765tdesc_data_init (struct obstack *obstack)
766{
284a0e3c 767 return obstack_new<tdesc_arch_data> (obstack);
123dc839
DJ
768}
769
770/* Similar, but for the temporary copy used during architecture
771 initialization. */
772
c1e1314d 773tdesc_arch_data_up
123dc839
DJ
774tdesc_data_alloc (void)
775{
c1e1314d 776 return tdesc_arch_data_up (new tdesc_arch_data ());
123dc839
DJ
777}
778
c1e1314d 779/* See target-descriptions.h. */
123dc839
DJ
780
781void
c1e1314d 782tdesc_arch_data_deleter::operator() (struct tdesc_arch_data *data) const
123dc839 783{
f0cddbef 784 delete data;
123dc839
DJ
785}
786
787/* Search FEATURE for a register named NAME. */
788
7cc46491
DJ
789static struct tdesc_reg *
790tdesc_find_register_early (const struct tdesc_feature *feature,
791 const char *name)
123dc839 792{
c9c895b9 793 for (const tdesc_reg_up &reg : feature->registers)
a8142ee1 794 if (strcasecmp (reg->name.c_str (), name) == 0)
c9c895b9 795 return reg.get ();
123dc839 796
7cc46491
DJ
797 return NULL;
798}
799
800/* Search FEATURE for a register named NAME. Assign REGNO to it. */
801
802int
803tdesc_numbered_register (const struct tdesc_feature *feature,
804 struct tdesc_arch_data *data,
805 int regno, const char *name)
806{
807 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
808
809 if (reg == NULL)
810 return 0;
811
812 /* Make sure the vector includes a REGNO'th element. */
f0cddbef
SM
813 while (regno >= data->arch_regs.size ())
814 data->arch_regs.emplace_back (nullptr, nullptr);
815
816 data->arch_regs[regno] = tdesc_arch_reg (reg, NULL);
ad068eab 817
7cc46491 818 return 1;
123dc839
DJ
819}
820
58d6951d
DJ
821/* Search FEATURE for a register named NAME, but do not assign a fixed
822 register number to it. */
823
824int
825tdesc_unnumbered_register (const struct tdesc_feature *feature,
826 const char *name)
827{
828 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
829
830 if (reg == NULL)
831 return 0;
832
833 return 1;
834}
835
7cc46491
DJ
836/* Search FEATURE for a register whose name is in NAMES and assign
837 REGNO to it. */
123dc839
DJ
838
839int
840tdesc_numbered_register_choices (const struct tdesc_feature *feature,
841 struct tdesc_arch_data *data,
842 int regno, const char *const names[])
843{
844 int i;
845
846 for (i = 0; names[i] != NULL; i++)
847 if (tdesc_numbered_register (feature, data, regno, names[i]))
848 return 1;
849
850 return 0;
851}
852
7cc46491
DJ
853/* Search FEATURE for a register named NAME, and return its size in
854 bits. The register must exist. */
855
856int
12863263 857tdesc_register_bitsize (const struct tdesc_feature *feature, const char *name)
7cc46491
DJ
858{
859 struct tdesc_reg *reg = tdesc_find_register_early (feature, name);
860
861 gdb_assert (reg != NULL);
862 return reg->bitsize;
863}
864
123dc839
DJ
865/* Look up a register by its GDB internal register number. */
866
ad068eab
UW
867static struct tdesc_arch_reg *
868tdesc_find_arch_register (struct gdbarch *gdbarch, int regno)
123dc839 869{
123dc839
DJ
870 struct tdesc_arch_data *data;
871
19ba03f4 872 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
f0cddbef
SM
873 if (regno < data->arch_regs.size ())
874 return &data->arch_regs[regno];
123dc839
DJ
875 else
876 return NULL;
877}
878
ad068eab
UW
879static struct tdesc_reg *
880tdesc_find_register (struct gdbarch *gdbarch, int regno)
881{
882 struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno);
5d502164 883
ad068eab
UW
884 return reg? reg->reg : NULL;
885}
886
f8b73d13
DJ
887/* Return the name of register REGNO, from the target description or
888 from an architecture-provided pseudo_register_name method. */
889
890const char *
d93859e2 891tdesc_register_name (struct gdbarch *gdbarch, int regno)
123dc839 892{
d93859e2
UW
893 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
894 int num_regs = gdbarch_num_regs (gdbarch);
123dc839
DJ
895
896 if (reg != NULL)
a8142ee1 897 return reg->name.c_str ();
123dc839 898
f6efe3f8 899 if (regno >= num_regs && regno < gdbarch_num_cooked_regs (gdbarch))
123dc839 900 {
19ba03f4
SM
901 struct tdesc_arch_data *data
902 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 903
123dc839 904 gdb_assert (data->pseudo_register_name != NULL);
d93859e2 905 return data->pseudo_register_name (gdbarch, regno);
123dc839
DJ
906 }
907
908 return "";
909}
910
58d6951d 911struct type *
123dc839
DJ
912tdesc_register_type (struct gdbarch *gdbarch, int regno)
913{
ad068eab
UW
914 struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno);
915 struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL;
123dc839
DJ
916 int num_regs = gdbarch_num_regs (gdbarch);
917 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
918
919 if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs)
920 {
19ba03f4
SM
921 struct tdesc_arch_data *data
922 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 923
123dc839
DJ
924 gdb_assert (data->pseudo_register_type != NULL);
925 return data->pseudo_register_type (gdbarch, regno);
926 }
927
928 if (reg == NULL)
929 /* Return "int0_t", since "void" has a misleading size of one. */
df4df182 930 return builtin_type (gdbarch)->builtin_int0;
123dc839 931
ad068eab 932 if (arch_reg->type == NULL)
123dc839 933 {
ad068eab
UW
934 /* First check for a predefined or target defined type. */
935 if (reg->tdesc_type)
b8df6ca7 936 arch_reg->type = make_gdb_type (gdbarch, reg->tdesc_type);
ad068eab
UW
937
938 /* Next try size-sensitive type shortcuts. */
a8142ee1 939 else if (reg->type == "float")
ad068eab
UW
940 {
941 if (reg->bitsize == gdbarch_float_bit (gdbarch))
942 arch_reg->type = builtin_type (gdbarch)->builtin_float;
943 else if (reg->bitsize == gdbarch_double_bit (gdbarch))
944 arch_reg->type = builtin_type (gdbarch)->builtin_double;
945 else if (reg->bitsize == gdbarch_long_double_bit (gdbarch))
946 arch_reg->type = builtin_type (gdbarch)->builtin_long_double;
947 else
948 {
949 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
a8142ee1 950 reg->name.c_str (), reg->bitsize);
ad068eab
UW
951 arch_reg->type = builtin_type (gdbarch)->builtin_double;
952 }
953 }
a8142ee1 954 else if (reg->type == "int")
ad068eab
UW
955 {
956 if (reg->bitsize == gdbarch_long_bit (gdbarch))
957 arch_reg->type = builtin_type (gdbarch)->builtin_long;
958 else if (reg->bitsize == TARGET_CHAR_BIT)
959 arch_reg->type = builtin_type (gdbarch)->builtin_char;
960 else if (reg->bitsize == gdbarch_short_bit (gdbarch))
961 arch_reg->type = builtin_type (gdbarch)->builtin_short;
962 else if (reg->bitsize == gdbarch_int_bit (gdbarch))
963 arch_reg->type = builtin_type (gdbarch)->builtin_int;
964 else if (reg->bitsize == gdbarch_long_long_bit (gdbarch))
965 arch_reg->type = builtin_type (gdbarch)->builtin_long_long;
966 else if (reg->bitsize == gdbarch_ptr_bit (gdbarch))
c378eb4e 967 /* A bit desperate by this point... */
ad068eab
UW
968 arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr;
969 else
970 {
971 warning (_("Register \"%s\" has an unsupported size (%d bits)"),
a8142ee1 972 reg->name.c_str (), reg->bitsize);
ad068eab
UW
973 arch_reg->type = builtin_type (gdbarch)->builtin_long;
974 }
975 }
976
977 if (arch_reg->type == NULL)
978 internal_error (__FILE__, __LINE__,
979 "Register \"%s\" has an unknown type \"%s\"",
a8142ee1 980 reg->name.c_str (), reg->type.c_str ());
123dc839 981 }
123dc839 982
ad068eab 983 return arch_reg->type;
123dc839
DJ
984}
985
986static int
987tdesc_remote_register_number (struct gdbarch *gdbarch, int regno)
988{
989 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
990
991 if (reg != NULL)
992 return reg->target_regnum;
993 else
994 return -1;
995}
996
997/* Check whether REGNUM is a member of REGGROUP. Registers from the
cef0f868
SH
998 target description may be classified as general, float, vector or other
999 register groups registered with reggroup_add(). Unlike a gdbarch
1000 register_reggroup_p method, this function will return -1 if it does not
1001 know; the caller should handle registers with no specified group.
1002
1003 The names of containing features are not used. This might be extended
1004 to display registers in some more useful groupings.
123dc839
DJ
1005
1006 The save-restore flag is also implemented here. */
1007
f8b73d13
DJ
1008int
1009tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno,
1010 struct reggroup *reggroup)
123dc839 1011{
123dc839
DJ
1012 struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno);
1013
440cf44e
LM
1014 if (reg != NULL && !reg->group.empty ()
1015 && (reg->group == reggroup_name (reggroup)))
cef0f868 1016 return 1;
123dc839 1017
440cf44e
LM
1018 if (reg != NULL
1019 && (reggroup == save_reggroup || reggroup == restore_reggroup))
1020 return reg->save_restore;
123dc839 1021
f8b73d13
DJ
1022 return -1;
1023}
1024
1025/* Check whether REGNUM is a member of REGGROUP. Registers with no
1026 group specified go to the default reggroup function and are handled
1027 by type. */
1028
1029static int
1030tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno,
1031 struct reggroup *reggroup)
1032{
1033 int num_regs = gdbarch_num_regs (gdbarch);
1034 int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch);
1035 int ret;
1036
1037 if (regno >= num_regs && regno < num_regs + num_pseudo_regs)
1038 {
19ba03f4
SM
1039 struct tdesc_arch_data *data
1040 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
5d502164 1041
58d6951d
DJ
1042 if (data->pseudo_register_reggroup_p != NULL)
1043 return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup);
1044 /* Otherwise fall through to the default reggroup_p. */
f8b73d13
DJ
1045 }
1046
1047 ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup);
1048 if (ret != -1)
1049 return ret;
1050
123dc839
DJ
1051 return default_register_reggroup_p (gdbarch, regno, reggroup);
1052}
1053
1054/* Record architecture-specific functions to call for pseudo-register
1055 support. */
1056
1057void
1058set_tdesc_pseudo_register_name (struct gdbarch *gdbarch,
1059 gdbarch_register_name_ftype *pseudo_name)
1060{
19ba03f4
SM
1061 struct tdesc_arch_data *data
1062 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1063
1064 data->pseudo_register_name = pseudo_name;
1065}
1066
1067void
1068set_tdesc_pseudo_register_type (struct gdbarch *gdbarch,
1069 gdbarch_register_type_ftype *pseudo_type)
1070{
19ba03f4
SM
1071 struct tdesc_arch_data *data
1072 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1073
1074 data->pseudo_register_type = pseudo_type;
1075}
1076
1077void
1078set_tdesc_pseudo_register_reggroup_p
1079 (struct gdbarch *gdbarch,
1080 gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p)
1081{
19ba03f4
SM
1082 struct tdesc_arch_data *data
1083 = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
123dc839
DJ
1084
1085 data->pseudo_register_reggroup_p = pseudo_reggroup_p;
1086}
1087
1088/* Update GDBARCH to use the target description for registers. */
1089
1090void
1091tdesc_use_registers (struct gdbarch *gdbarch,
7cc46491 1092 const struct target_desc *target_desc,
c1e1314d 1093 tdesc_arch_data_up &&early_data,
be64fd07 1094 tdesc_unknown_register_ftype unk_reg_cb)
123dc839
DJ
1095{
1096 int num_regs = gdbarch_num_regs (gdbarch);
123dc839 1097 struct tdesc_arch_data *data;
123dc839 1098
123dc839
DJ
1099 /* We can't use the description for registers if it doesn't describe
1100 any. This function should only be called after validating
1101 registers, so the caller should know that registers are
1102 included. */
1103 gdb_assert (tdesc_has_registers (target_desc));
1104
19ba03f4 1105 data = (struct tdesc_arch_data *) gdbarch_data (gdbarch, tdesc_data);
c1e1314d 1106 data->arch_regs = std::move (early_data->arch_regs);
123dc839
DJ
1107
1108 /* Build up a set of all registers, so that we can assign register
1109 numbers where needed. The hash table expands as necessary, so
1110 the initial size is arbitrary. */
eb53f105
TT
1111 htab_up reg_hash (htab_create (37, htab_hash_pointer, htab_eq_pointer,
1112 NULL));
3eea796c 1113 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 1114 for (const tdesc_reg_up &reg : feature->registers)
123dc839 1115 {
eb53f105 1116 void **slot = htab_find_slot (reg_hash.get (), reg.get (), INSERT);
123dc839 1117
c9c895b9 1118 *slot = reg.get ();
cef0f868
SH
1119 /* Add reggroup if its new. */
1120 if (!reg->group.empty ())
1121 if (reggroup_find (gdbarch, reg->group.c_str ()) == NULL)
1122 reggroup_add (gdbarch, reggroup_gdbarch_new (gdbarch,
1123 reg->group.c_str (),
1124 USER_REGGROUP));
123dc839
DJ
1125 }
1126
1127 /* Remove any registers which were assigned numbers by the
1128 architecture. */
f0cddbef
SM
1129 for (const tdesc_arch_reg &arch_reg : data->arch_regs)
1130 if (arch_reg.reg != NULL)
eb53f105 1131 htab_remove_elt (reg_hash.get (), arch_reg.reg);
123dc839
DJ
1132
1133 /* Assign numbers to the remaining registers and add them to the
f57d151a 1134 list of registers. The new numbers are always above gdbarch_num_regs.
123dc839
DJ
1135 Iterate over the features, not the hash table, so that the order
1136 matches that in the target description. */
1137
f0cddbef
SM
1138 gdb_assert (data->arch_regs.size () <= num_regs);
1139 while (data->arch_regs.size () < num_regs)
1140 data->arch_regs.emplace_back (nullptr, nullptr);
1141
be64fd07
AB
1142 /* First we give the target a chance to number previously unknown
1143 registers. This allows targets to record the numbers assigned based
1144 on which feature the register was from. */
1145 if (unk_reg_cb != NULL)
1146 {
1147 for (const tdesc_feature_up &feature : target_desc->features)
1148 for (const tdesc_reg_up &reg : feature->registers)
eb53f105 1149 if (htab_find (reg_hash.get (), reg.get ()) != NULL)
be64fd07
AB
1150 {
1151 int regno = unk_reg_cb (gdbarch, feature.get (),
1152 reg->name.c_str (), num_regs);
1153 gdb_assert (regno == -1 || regno >= num_regs);
1154 if (regno != -1)
1155 {
1156 while (regno >= data->arch_regs.size ())
1157 data->arch_regs.emplace_back (nullptr, nullptr);
1158 data->arch_regs[regno] = tdesc_arch_reg (reg.get (), NULL);
1159 num_regs = regno + 1;
eb53f105 1160 htab_remove_elt (reg_hash.get (), reg.get ());
be64fd07
AB
1161 }
1162 }
1163 }
1164
1165 /* Ensure the array was sized correctly above. */
1166 gdb_assert (data->arch_regs.size () == num_regs);
1167
1168 /* Now in a final pass we assign register numbers to any remaining
1169 unnumbered registers. */
3eea796c 1170 for (const tdesc_feature_up &feature : target_desc->features)
c9c895b9 1171 for (const tdesc_reg_up &reg : feature->registers)
eb53f105 1172 if (htab_find (reg_hash.get (), reg.get ()) != NULL)
123dc839 1173 {
f0cddbef 1174 data->arch_regs.emplace_back (reg.get (), nullptr);
123dc839
DJ
1175 num_regs++;
1176 }
1177
123dc839
DJ
1178 /* Update the architecture. */
1179 set_gdbarch_num_regs (gdbarch, num_regs);
1180 set_gdbarch_register_name (gdbarch, tdesc_register_name);
1181 set_gdbarch_register_type (gdbarch, tdesc_register_type);
1182 set_gdbarch_remote_register_number (gdbarch,
1183 tdesc_remote_register_number);
1184 set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p);
1185}
123dc839 1186
268a13a5 1187/* See gdbsupport/tdesc.h. */
f49ff000 1188
123dc839 1189struct tdesc_feature *
3b74854b 1190tdesc_create_feature (struct target_desc *tdesc, const char *name)
123dc839 1191{
72ddacb7 1192 struct tdesc_feature *new_feature = new tdesc_feature (name);
123dc839 1193
3eea796c
SM
1194 tdesc->features.emplace_back (new_feature);
1195
123dc839
DJ
1196 return new_feature;
1197}
1198
0e267416
AB
1199/* See gdbsupport/tdesc.h. */
1200
51a948fd 1201target_desc_up
424163ea
DJ
1202allocate_target_description (void)
1203{
51a948fd 1204 return target_desc_up (new target_desc ());
424163ea 1205}
29709017 1206
0e267416
AB
1207/* See gdbsupport/tdesc.h. */
1208
c55d06ec
TT
1209void
1210target_desc_deleter::operator() (struct target_desc *target_desc) const
23181151 1211{
b468ff4c 1212 delete target_desc;
23181151
DJ
1213}
1214
e35359c5
UW
1215void
1216tdesc_add_compatible (struct target_desc *target_desc,
1217 const struct bfd_arch_info *compatible)
1218{
e35359c5
UW
1219 /* If this instance of GDB is compiled without BFD support for the
1220 compatible architecture, simply ignore it -- we would not be able
1221 to handle it anyway. */
1222 if (compatible == NULL)
1223 return;
1224
fbf42f4e
AB
1225 for (const tdesc_compatible_info_up &compat : target_desc->compatible)
1226 if (compat->arch () == compatible)
e35359c5
UW
1227 internal_error (__FILE__, __LINE__,
1228 _("Attempted to add duplicate "
1229 "compatible architecture \"%s\""),
1230 compatible->printable_name);
1231
fbf42f4e
AB
1232 target_desc->compatible.push_back
1233 (std::unique_ptr<tdesc_compatible_info>
1234 (new tdesc_compatible_info (compatible)));
e35359c5
UW
1235}
1236
29709017
DJ
1237void
1238set_tdesc_property (struct target_desc *target_desc,
1239 const char *key, const char *value)
1240{
29709017
DJ
1241 gdb_assert (key != NULL && value != NULL);
1242
129c10bc
SM
1243 if (tdesc_property (target_desc, key) != NULL)
1244 internal_error (__FILE__, __LINE__,
1245 _("Attempted to add duplicate property \"%s\""), key);
29709017 1246
129c10bc 1247 target_desc->properties.emplace_back (key, value);
29709017 1248}
23181151 1249
268a13a5 1250/* See gdbsupport/tdesc.h. */
5f035c07
YQ
1251
1252void
1253set_tdesc_architecture (struct target_desc *target_desc,
1254 const char *name)
1255{
1256 set_tdesc_architecture (target_desc, bfd_scan_arch (name));
1257}
1258
23181151
DJ
1259void
1260set_tdesc_architecture (struct target_desc *target_desc,
1261 const struct bfd_arch_info *arch)
1262{
1263 target_desc->arch = arch;
1264}
08d16641 1265
268a13a5 1266/* See gdbsupport/tdesc.h. */
5f035c07
YQ
1267
1268void
1269set_tdesc_osabi (struct target_desc *target_desc, const char *name)
1270{
1271 set_tdesc_osabi (target_desc, osabi_from_tdesc_string (name));
1272}
1273
08d16641
PA
1274void
1275set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi)
1276{
1277 target_desc->osabi = osabi;
1278}
23181151
DJ
1279\f
1280
1281static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist;
1282static struct cmd_list_element *tdesc_unset_cmdlist;
1283
1284/* Helper functions for the CLI commands. */
1285
23181151 1286static void
eb4c3f4a 1287set_tdesc_filename_cmd (const char *args, int from_tty,
23181151
DJ
1288 struct cmd_list_element *c)
1289{
c2962e6a
SM
1290 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
1291
91e3c425 1292 tdesc_info->filename = tdesc_filename_cmd_string;
6ecd4729 1293
23181151
DJ
1294 target_clear_description ();
1295 target_find_description ();
1296}
1297
1298static void
1299show_tdesc_filename_cmd (struct ui_file *file, int from_tty,
1300 struct cmd_list_element *c,
1301 const char *value)
1302{
91e3c425 1303 value = get_tdesc_info (current_inferior ())->filename.data ();
6ecd4729 1304
23181151 1305 if (value != NULL && *value != '\0')
3e43a32a 1306 printf_filtered (_("The target description will be read from \"%s\".\n"),
23181151
DJ
1307 value);
1308 else
3e43a32a
MS
1309 printf_filtered (_("The target description will be "
1310 "read from the target.\n"));
23181151
DJ
1311}
1312
1313static void
e100df1a 1314unset_tdesc_filename_cmd (const char *args, int from_tty)
23181151 1315{
c2962e6a
SM
1316 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
1317
91e3c425 1318 tdesc_info->filename.clear ();
23181151
DJ
1319 target_clear_description ();
1320 target_find_description ();
1321}
1322
6eb1e6a8
YQ
1323/* Print target description in C. */
1324
1325class print_c_tdesc : public tdesc_element_visitor
1326{
1327public:
1328 print_c_tdesc (std::string &filename_after_features)
1329 : m_filename_after_features (filename_after_features)
1330 {
1331 const char *inp;
1332 char *outp;
1333 const char *filename = lbasename (m_filename_after_features.c_str ());
1334
1335 m_function = (char *) xmalloc (strlen (filename) + 1);
1336 for (inp = filename, outp = m_function; *inp != '\0'; inp++)
1337 if (*inp == '.')
1338 break;
1339 else if (*inp == '-')
1340 *outp++ = '_';
20821f4e
AB
1341 else if (*inp == ' ')
1342 *outp++ = '_';
6eb1e6a8
YQ
1343 else
1344 *outp++ = *inp;
1345 *outp = '\0';
1346
1347 /* Standard boilerplate. */
1348 printf_unfiltered ("/* THIS FILE IS GENERATED. "
1349 "-*- buffer-read-only: t -*- vi"
1350 ":set ro:\n");
6eb1e6a8
YQ
1351 }
1352
1353 ~print_c_tdesc ()
1354 {
1355 xfree (m_function);
1356 }
1357
1358 void visit_pre (const target_desc *e) override
1359 {
25aa13e5
YQ
1360 printf_unfiltered (" Original: %s */\n\n",
1361 lbasename (m_filename_after_features.c_str ()));
1362
6eb1e6a8
YQ
1363 printf_unfiltered ("#include \"defs.h\"\n");
1364 printf_unfiltered ("#include \"osabi.h\"\n");
1365 printf_unfiltered ("#include \"target-descriptions.h\"\n");
1366 printf_unfiltered ("\n");
1367
1368 printf_unfiltered ("struct target_desc *tdesc_%s;\n", m_function);
1369 printf_unfiltered ("static void\n");
1370 printf_unfiltered ("initialize_tdesc_%s (void)\n", m_function);
1371 printf_unfiltered ("{\n");
1372 printf_unfiltered
51a948fd 1373 (" target_desc_up result = allocate_target_description ();\n");
6eb1e6a8
YQ
1374
1375 if (tdesc_architecture (e) != NULL)
1376 {
1377 printf_unfiltered
51a948fd 1378 (" set_tdesc_architecture (result.get (), bfd_scan_arch (\"%s\"));\n",
6eb1e6a8
YQ
1379 tdesc_architecture (e)->printable_name);
1380 printf_unfiltered ("\n");
1381 }
1382 if (tdesc_osabi (e) > GDB_OSABI_UNKNOWN
1383 && tdesc_osabi (e) < GDB_OSABI_INVALID)
1384 {
1385 printf_unfiltered
51a948fd 1386 (" set_tdesc_osabi (result.get (), osabi_from_tdesc_string (\"%s\"));\n",
6eb1e6a8
YQ
1387 gdbarch_osabi_name (tdesc_osabi (e)));
1388 printf_unfiltered ("\n");
1389 }
1390
fbf42f4e 1391 for (const tdesc_compatible_info_up &compatible : e->compatible)
40e2a983 1392 printf_unfiltered
51a948fd 1393 (" tdesc_add_compatible (result.get (), bfd_scan_arch (\"%s\"));\n",
fbf42f4e 1394 compatible->arch ()->printable_name);
6eb1e6a8 1395
40e2a983 1396 if (!e->compatible.empty ())
6eb1e6a8
YQ
1397 printf_unfiltered ("\n");
1398
129c10bc 1399 for (const property &prop : e->properties)
51a948fd 1400 printf_unfiltered (" set_tdesc_property (result.get (), \"%s\", \"%s\");\n",
129c10bc
SM
1401 prop.key.c_str (), prop.value.c_str ());
1402
6eb1e6a8
YQ
1403 printf_unfiltered (" struct tdesc_feature *feature;\n");
1404 }
1405
25aa13e5 1406 void visit_pre (const tdesc_feature *e) override
6eb1e6a8 1407 {
51a948fd 1408 printf_unfiltered ("\n feature = tdesc_create_feature (result.get (), \"%s\");\n",
f65ff9f9 1409 e->name.c_str ());
6eb1e6a8
YQ
1410 }
1411
25aa13e5
YQ
1412 void visit_post (const tdesc_feature *e) override
1413 {}
1414
6eb1e6a8
YQ
1415 void visit_post (const target_desc *e) override
1416 {
51a948fd 1417 printf_unfiltered ("\n tdesc_%s = result.release ();\n", m_function);
6eb1e6a8
YQ
1418 printf_unfiltered ("}\n");
1419 }
1420
d4a0e8b5
SM
1421 void visit (const tdesc_type_builtin *type) override
1422 {
1423 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
1424 }
1425
1426 void visit (const tdesc_type_vector *type) override
6eb1e6a8 1427 {
d4a0e8b5 1428 if (!m_printed_element_type)
6eb1e6a8 1429 {
d4a0e8b5
SM
1430 printf_unfiltered (" tdesc_type *element_type;\n");
1431 m_printed_element_type = true;
6eb1e6a8
YQ
1432 }
1433
d4a0e8b5
SM
1434 printf_unfiltered
1435 (" element_type = tdesc_named_type (feature, \"%s\");\n",
1436 type->element_type->name.c_str ());
1437 printf_unfiltered
1438 (" tdesc_create_vector (feature, \"%s\", element_type, %d);\n",
1439 type->name.c_str (), type->count);
1440
1441 printf_unfiltered ("\n");
1442 }
1443
1444 void visit (const tdesc_type_with_fields *type) override
1445 {
1446 if (!m_printed_type_with_fields)
6eb1e6a8 1447 {
d4a0e8b5
SM
1448 printf_unfiltered (" tdesc_type_with_fields *type_with_fields;\n");
1449 m_printed_type_with_fields = true;
1450 }
1451
6eb1e6a8
YQ
1452 switch (type->kind)
1453 {
6eb1e6a8
YQ
1454 case TDESC_TYPE_STRUCT:
1455 case TDESC_TYPE_FLAGS:
1456 if (type->kind == TDESC_TYPE_STRUCT)
1457 {
1458 printf_unfiltered
d4a0e8b5 1459 (" type_with_fields = tdesc_create_struct (feature, \"%s\");\n",
082b9140 1460 type->name.c_str ());
d4a0e8b5 1461 if (type->size != 0)
6eb1e6a8 1462 printf_unfiltered
d4a0e8b5 1463 (" tdesc_set_struct_size (type_with_fields, %d);\n", type->size);
6eb1e6a8
YQ
1464 }
1465 else
1466 {
1467 printf_unfiltered
d4a0e8b5
SM
1468 (" type_with_fields = tdesc_create_flags (feature, \"%s\", %d);\n",
1469 type->name.c_str (), type->size);
6eb1e6a8 1470 }
d4a0e8b5 1471 for (const tdesc_type_field &f : type->fields)
6eb1e6a8
YQ
1472 {
1473 const char *type_name;
1474
d05200d1
SM
1475 gdb_assert (f.type != NULL);
1476 type_name = f.type->name.c_str ();
6eb1e6a8
YQ
1477
1478 /* To minimize changes to generated files, don't emit type
1479 info for fields that have defaulted types. */
d05200d1 1480 if (f.start != -1)
6eb1e6a8 1481 {
d05200d1
SM
1482 gdb_assert (f.end != -1);
1483 if (f.type->kind == TDESC_TYPE_BOOL)
6eb1e6a8 1484 {
d05200d1 1485 gdb_assert (f.start == f.end);
6eb1e6a8 1486 printf_unfiltered
d4a0e8b5 1487 (" tdesc_add_flag (type_with_fields, %d, \"%s\");\n",
d05200d1 1488 f.start, f.name.c_str ());
6eb1e6a8 1489 }
d4a0e8b5
SM
1490 else if ((type->size == 4 && f.type->kind == TDESC_TYPE_UINT32)
1491 || (type->size == 8
d05200d1 1492 && f.type->kind == TDESC_TYPE_UINT64))
6eb1e6a8
YQ
1493 {
1494 printf_unfiltered
d4a0e8b5 1495 (" tdesc_add_bitfield (type_with_fields, \"%s\", %d, %d);\n",
d05200d1 1496 f.name.c_str (), f.start, f.end);
6eb1e6a8
YQ
1497 }
1498 else
1499 {
a8d2e585
SM
1500 printf_field_type_assignment
1501 ("tdesc_named_type (feature, \"%s\");\n",
6eb1e6a8
YQ
1502 type_name);
1503 printf_unfiltered
d4a0e8b5 1504 (" tdesc_add_typed_bitfield (type_with_fields, \"%s\","
6eb1e6a8 1505 " %d, %d, field_type);\n",
d05200d1 1506 f.name.c_str (), f.start, f.end);
6eb1e6a8
YQ
1507 }
1508 }
1509 else /* Not a bitfield. */
1510 {
d05200d1 1511 gdb_assert (f.end == -1);
6eb1e6a8 1512 gdb_assert (type->kind == TDESC_TYPE_STRUCT);
a8d2e585
SM
1513 printf_field_type_assignment
1514 ("tdesc_named_type (feature, \"%s\");\n", type_name);
6eb1e6a8 1515 printf_unfiltered
d4a0e8b5 1516 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
d05200d1 1517 f.name.c_str ());
6eb1e6a8
YQ
1518 }
1519 }
1520 break;
1521 case TDESC_TYPE_UNION:
1522 printf_unfiltered
d4a0e8b5 1523 (" type_with_fields = tdesc_create_union (feature, \"%s\");\n",
082b9140 1524 type->name.c_str ());
d4a0e8b5 1525 for (const tdesc_type_field &f : type->fields)
6eb1e6a8 1526 {
a8d2e585
SM
1527 printf_field_type_assignment
1528 ("tdesc_named_type (feature, \"%s\");\n", f.type->name.c_str ());
6eb1e6a8 1529 printf_unfiltered
d4a0e8b5 1530 (" tdesc_add_field (type_with_fields, \"%s\", field_type);\n",
d05200d1 1531 f.name.c_str ());
6eb1e6a8
YQ
1532 }
1533 break;
1534 case TDESC_TYPE_ENUM:
1535 printf_unfiltered
d4a0e8b5
SM
1536 (" type_with_fields = tdesc_create_enum (feature, \"%s\", %d);\n",
1537 type->name.c_str (), type->size);
1538 for (const tdesc_type_field &f : type->fields)
6eb1e6a8 1539 printf_unfiltered
d4a0e8b5 1540 (" tdesc_add_enum_value (type_with_fields, %d, \"%s\");\n",
d05200d1 1541 f.start, f.name.c_str ());
6eb1e6a8
YQ
1542 break;
1543 default:
082b9140 1544 error (_("C output is not supported type \"%s\"."), type->name.c_str ());
6eb1e6a8 1545 }
d4a0e8b5 1546
6eb1e6a8
YQ
1547 printf_unfiltered ("\n");
1548 }
1549
1550 void visit (const tdesc_reg *reg) override
1551 {
1552 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ",
a8142ee1
SM
1553 reg->name.c_str (), reg->target_regnum,
1554 reg->save_restore);
1555 if (!reg->group.empty ())
1556 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
6eb1e6a8
YQ
1557 else
1558 printf_unfiltered ("NULL, ");
a8142ee1 1559 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
6eb1e6a8
YQ
1560 }
1561
25aa13e5
YQ
1562protected:
1563 std::string m_filename_after_features;
1564
6eb1e6a8 1565private:
a8d2e585
SM
1566
1567 /* Print an assignment to the field_type variable. Print the declaration
1568 of field_type if that has not been done yet. */
6e8c24fe 1569 ATTRIBUTE_PRINTF (2, 3)
a8d2e585
SM
1570 void printf_field_type_assignment (const char *fmt, ...)
1571 {
1572 if (!m_printed_field_type)
1573 {
1574 printf_unfiltered (" tdesc_type *field_type;\n");
1575 m_printed_field_type = true;
1576 }
1577
1578 printf_unfiltered (" field_type = ");
1579
1580 va_list args;
1581 va_start (args, fmt);
1582 vprintf_unfiltered (fmt, args);
1583 va_end (args);
1584 }
1585
6eb1e6a8 1586 char *m_function;
d4a0e8b5
SM
1587
1588 /* Did we print "struct tdesc_type *element_type;" yet? */
1589 bool m_printed_element_type = false;
1590
1591 /* Did we print "struct tdesc_type_with_fields *element_type;" yet? */
1592 bool m_printed_type_with_fields = false;
1593
1594 /* Did we print "struct tdesc_type *field_type;" yet? */
6eb1e6a8 1595 bool m_printed_field_type = false;
6eb1e6a8
YQ
1596};
1597
25aa13e5
YQ
1598/* Print target description feature in C. */
1599
1600class print_c_feature : public print_c_tdesc
1601{
1602public:
1603 print_c_feature (std::string &file)
1604 : print_c_tdesc (file)
1605 {
1606 /* Trim ".tmp". */
1607 auto const pos = m_filename_after_features.find_last_of ('.');
1608
1609 m_filename_after_features = m_filename_after_features.substr (0, pos);
1610 }
1611
1612 void visit_pre (const target_desc *e) override
1613 {
1614 printf_unfiltered (" Original: %s */\n\n",
1615 lbasename (m_filename_after_features.c_str ()));
1616
268a13a5 1617 printf_unfiltered ("#include \"gdbsupport/tdesc.h\"\n");
25aa13e5
YQ
1618 printf_unfiltered ("\n");
1619 }
1620
1621 void visit_post (const target_desc *e) override
1622 {}
1623
1624 void visit_pre (const tdesc_feature *e) override
1625 {
1626 std::string name (m_filename_after_features);
1627
1628 auto pos = name.find_first_of ('.');
1629
1630 name = name.substr (0, pos);
1631 std::replace (name.begin (), name.end (), '/', '_');
1632 std::replace (name.begin (), name.end (), '-', '_');
1633
1634 printf_unfiltered ("static int\n");
1635 printf_unfiltered ("create_feature_%s ", name.c_str ());
1636 printf_unfiltered ("(struct target_desc *result, long regnum)\n");
1637
1638 printf_unfiltered ("{\n");
1639 printf_unfiltered (" struct tdesc_feature *feature;\n");
0abe8a89
YQ
1640
1641 printf_unfiltered
3b74854b
AH
1642 ("\n feature = tdesc_create_feature (result, \"%s\");\n",
1643 e->name.c_str ());
25aa13e5
YQ
1644 }
1645
1646 void visit_post (const tdesc_feature *e) override
1647 {
1648 printf_unfiltered (" return regnum;\n");
1649 printf_unfiltered ("}\n");
1650 }
1651
1652 void visit (const tdesc_reg *reg) override
1653 {
ea03d0d3
YQ
1654 /* Most "reg" in XML target descriptions don't have "regnum"
1655 attribute, so the register number is allocated sequentially.
1656 In case that reg has "regnum" attribute, register number
1657 should be set by that explicitly. */
1658
1659 if (reg->target_regnum < m_next_regnum)
1660 {
1661 /* The integrity check, it can catch some errors on register
1662 number collision, like this,
1663
1664 <reg name="x0" bitsize="32"/>
1665 <reg name="x1" bitsize="32"/>
1666 <reg name="x2" bitsize="32"/>
1667 <reg name="x3" bitsize="32"/>
1668 <reg name="ps" bitsize="32" regnum="3"/>
1669
1670 but it also has false negatives. The target description
1671 below is correct,
1672
1673 <reg name="x1" bitsize="32" regnum="1"/>
1674 <reg name="x3" bitsize="32" regnum="3"/>
1675 <reg name="x2" bitsize="32" regnum="2"/>
1676 <reg name="x4" bitsize="32" regnum="4"/>
1677
1678 but it is not a good practice, so still error on this,
1679 and also print the message so that it can be saved in the
1680 generated c file. */
1681
1682 printf_unfiltered ("ERROR: \"regnum\" attribute %ld ",
1683 reg->target_regnum);
1684 printf_unfiltered ("is not the largest number (%d).\n",
1685 m_next_regnum);
1686 error (_("\"regnum\" attribute %ld is not the largest number (%d)."),
1687 reg->target_regnum, m_next_regnum);
1688 }
1689
1690 if (reg->target_regnum > m_next_regnum)
1691 {
1692 printf_unfiltered (" regnum = %ld;\n", reg->target_regnum);
1693 m_next_regnum = reg->target_regnum;
1694 }
1695
25aa13e5 1696 printf_unfiltered (" tdesc_create_reg (feature, \"%s\", regnum++, %d, ",
a8142ee1
SM
1697 reg->name.c_str (), reg->save_restore);
1698 if (!reg->group.empty ())
1699 printf_unfiltered ("\"%s\", ", reg->group.c_str ());
25aa13e5
YQ
1700 else
1701 printf_unfiltered ("NULL, ");
a8142ee1 1702 printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type.c_str ());
ea03d0d3
YQ
1703
1704 m_next_regnum++;
25aa13e5
YQ
1705 }
1706
ea03d0d3
YQ
1707private:
1708 /* The register number to use for the next register we see. */
1709 int m_next_regnum = 0;
25aa13e5
YQ
1710};
1711
268a13a5 1712/* See gdbsupport/tdesc.h. */
e98577a9
AH
1713
1714const char *
1715tdesc_get_features_xml (const target_desc *tdesc)
1716{
1717 if (tdesc->xmltarget == nullptr)
1718 {
1719 std::string buffer ("@");
1720 print_xml_feature v (&buffer);
1721 tdesc->accept (v);
1722 tdesc->xmltarget = xstrdup (buffer.c_str ());
1723 }
1724 return tdesc->xmltarget;
1725}
1726
ab33b152
AB
1727/* Data structures and functions to setup the option flags for 'maintenance
1728 print c-tdesc command. */
1729
1730struct maint_print_c_tdesc_options
1731{
1732 /* True when the '-single-feature' flag was passed. */
1733 bool single_feature = false;
1734};
1735
1736using maint_print_c_tdesc_opt_def
1737 = gdb::option::flag_option_def<maint_print_c_tdesc_options>;
1738
1739static const gdb::option::option_def maint_print_c_tdesc_opt_defs[] = {
1740 maint_print_c_tdesc_opt_def {
1741 "single-feature",
1742 [] (maint_print_c_tdesc_options *opt) { return &opt->single_feature; },
1743 N_("Print C description of just a single feature.")
1744 },
1745};
1746
1747static inline gdb::option::option_def_group
1748make_maint_print_c_tdesc_options_def_group (maint_print_c_tdesc_options *opts)
1749{
1750 return {{maint_print_c_tdesc_opt_defs}, opts};
1751}
1752
1753/* Implement 'maintenance print c-tdesc' command. */
1754
81adfced 1755static void
e100df1a 1756maint_print_c_tdesc_cmd (const char *args, int from_tty)
81adfced
DJ
1757{
1758 const struct target_desc *tdesc;
6eb1e6a8 1759 const char *filename;
81adfced 1760
ab33b152
AB
1761 maint_print_c_tdesc_options opts;
1762 auto grp = make_maint_print_c_tdesc_options_def_group (&opts);
1763 gdb::option::process_options
1764 (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1765
8e2141c6
YQ
1766 if (args == NULL)
1767 {
1768 /* Use the global target-supplied description, not the current
1769 architecture's. This lets a GDB for one architecture generate C
1770 for another architecture's description, even though the gdbarch
1771 initialization code will reject the new description. */
c2962e6a
SM
1772 target_desc_info *tdesc_info = get_tdesc_info (current_inferior ());
1773 tdesc = tdesc_info->tdesc;
91e3c425 1774 filename = tdesc_info->filename.data ();
8e2141c6
YQ
1775 }
1776 else
1777 {
1778 /* Use the target description from the XML file. */
1779 filename = args;
1780 tdesc = file_read_description_xml (filename);
1781 }
1782
81adfced
DJ
1783 if (tdesc == NULL)
1784 error (_("There is no target description to print."));
1785
8e2141c6 1786 if (filename == NULL)
20821f4e 1787 filename = "fetched from target";
81adfced 1788
6eb1e6a8
YQ
1789 std::string filename_after_features (filename);
1790 auto loc = filename_after_features.rfind ("/features/");
4e2f8df6 1791
6eb1e6a8
YQ
1792 if (loc != std::string::npos)
1793 filename_after_features = filename_after_features.substr (loc + 10);
4e2f8df6 1794
25aa13e5
YQ
1795 /* Print c files for target features instead of target descriptions,
1796 because c files got from target features are more flexible than the
1797 counterparts. */
ab33b152 1798 if (opts.single_feature)
25aa13e5 1799 {
ab33b152
AB
1800 if (tdesc->features.size () != 1)
1801 error (_("only target descriptions with 1 feature can be used "
1802 "with -single-feature option"));
1803
25aa13e5 1804 print_c_feature v (filename_after_features);
81adfced 1805
25aa13e5
YQ
1806 tdesc->accept (v);
1807 }
1808 else
1809 {
1810 print_c_tdesc v (filename_after_features);
1811
1812 tdesc->accept (v);
1813 }
81adfced
DJ
1814}
1815
ab33b152
AB
1816/* Completer for the "backtrace" command. */
1817
1818static void
1819maint_print_c_tdesc_cmd_completer (struct cmd_list_element *ignore,
1820 completion_tracker &tracker,
1821 const char *text, const char *word)
1822{
1823 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1824 if (gdb::option::complete_options
1825 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1826 return;
1827
1828 word = advance_to_filename_complete_word_point (tracker, text);
1829 filename_completer (ignore, tracker, text, word);
1830}
1831
caa7fd04
AB
1832/* Implement the maintenance print xml-tdesc command. */
1833
1834static void
1835maint_print_xml_tdesc_cmd (const char *args, int from_tty)
1836{
1837 const struct target_desc *tdesc;
1838
1839 if (args == NULL)
1840 {
1841 /* Use the global target-supplied description, not the current
1842 architecture's. This lets a GDB for one architecture generate XML
1843 for another architecture's description, even though the gdbarch
1844 initialization code will reject the new description. */
c2962e6a 1845 tdesc = get_tdesc_info (current_inferior ())->tdesc;
caa7fd04
AB
1846 }
1847 else
1848 {
1849 /* Use the target description from the XML file. */
1850 tdesc = file_read_description_xml (args);
1851 }
1852
1853 if (tdesc == NULL)
1854 error (_("There is no target description to print."));
1855
1856 std::string buf;
1857 print_xml_feature v (&buf);
1858 tdesc->accept (v);
1859 puts_unfiltered (buf.c_str ());
1860}
1861
27d41eac
YQ
1862namespace selftests {
1863
1c28969e
SM
1864/* A reference target description, used for testing (see record_xml_tdesc). */
1865
1866struct xml_test_tdesc
1867{
1868 xml_test_tdesc (const char *name, std::unique_ptr<const target_desc> &&tdesc)
1869 : name (name), tdesc (std::move (tdesc))
1870 {}
1871
1872 const char *name;
1873 std::unique_ptr<const target_desc> tdesc;
1874};
1875
1876static std::vector<xml_test_tdesc> xml_tdesc;
27d41eac
YQ
1877
1878#if GDB_SELF_TEST
1879
405feb71 1880/* See target-descriptions.h. */
27d41eac
YQ
1881
1882void
1883record_xml_tdesc (const char *xml_file, const struct target_desc *tdesc)
1884{
1c28969e 1885 xml_tdesc.emplace_back (xml_file, std::unique_ptr<const target_desc> (tdesc));
27d41eac
YQ
1886}
1887#endif
1888
1889}
1890
85102364 1891/* Test the conversion process of a target description to/from xml: Take a target
e98577a9
AH
1892 description TDESC, convert to xml, back to a description, and confirm the new
1893 tdesc is identical to the original. */
1894static bool
1895maintenance_check_tdesc_xml_convert (const target_desc *tdesc, const char *name)
1896{
1897 const char *xml = tdesc_get_features_xml (tdesc);
1898
1899 if (xml == nullptr || *xml != '@')
1900 {
1901 printf_filtered (_("Could not convert description for %s to xml.\n"),
1902 name);
1903 return false;
1904 }
1905
1906 const target_desc *tdesc_trans = string_read_description_xml (xml + 1);
1907
1908 if (tdesc_trans == nullptr)
1909 {
1910 printf_filtered (_("Could not convert description for %s from xml.\n"),
1911 name);
1912 return false;
1913 }
1914 else if (*tdesc != *tdesc_trans)
1915 {
1916 printf_filtered (_("Converted description for %s does not match.\n"),
1917 name);
1918 return false;
1919 }
1920 return true;
1921}
1922
1923
27d41eac
YQ
1924/* Check that the target descriptions created dynamically by
1925 architecture-specific code equal the descriptions created from XML files
1926 found in the specified directory DIR. */
1927
1928static void
e100df1a 1929maintenance_check_xml_descriptions (const char *dir, int from_tty)
27d41eac
YQ
1930{
1931 if (dir == NULL)
1932 error (_("Missing dir name"));
1933
1934 gdb::unique_xmalloc_ptr<char> dir1 (tilde_expand (dir));
1935 std::string feature_dir (dir1.get ());
1936 unsigned int failed = 0;
1937
1938 for (auto const &e : selftests::xml_tdesc)
1939 {
1c28969e 1940 std::string tdesc_xml = (feature_dir + SLASH_STRING + e.name);
27d41eac
YQ
1941 const target_desc *tdesc
1942 = file_read_description_xml (tdesc_xml.data ());
1943
1c28969e 1944 if (tdesc == NULL || *tdesc != *e.tdesc)
e98577a9 1945 {
1c28969e 1946 printf_filtered ( _("Descriptions for %s do not match.\n"), e.name);
e98577a9
AH
1947 failed++;
1948 }
1c28969e
SM
1949 else if (!maintenance_check_tdesc_xml_convert (tdesc, e.name)
1950 || !maintenance_check_tdesc_xml_convert (e.tdesc.get (), e.name))
27d41eac
YQ
1951 failed++;
1952 }
1953 printf_filtered (_("Tested %lu XML files, %d failed\n"),
1954 (long) selftests::xml_tdesc.size (), failed);
1955}
1956
6c265988 1957void _initialize_target_descriptions ();
23181151 1958void
6c265988 1959_initialize_target_descriptions ()
23181151 1960{
20821f4e
AB
1961 cmd_list_element *cmd;
1962
123dc839
DJ
1963 tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init);
1964
0743fc83 1965 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
23181151 1966Set target description specific variables."),
2f822da5 1967 &tdesc_set_cmdlist,
0743fc83
TT
1968 0 /* allow-unknown */, &setlist);
1969 add_show_prefix_cmd ("tdesc", class_maintenance, _("\
23181151 1970Show target description specific variables."),
2f822da5 1971 &tdesc_show_cmdlist,
0743fc83
TT
1972 0 /* allow-unknown */, &showlist);
1973 add_basic_prefix_cmd ("tdesc", class_maintenance, _("\
23181151 1974Unset target description specific variables."),
2f822da5 1975 &tdesc_unset_cmdlist,
0743fc83 1976 0 /* allow-unknown */, &unsetlist);
23181151
DJ
1977
1978 add_setshow_filename_cmd ("filename", class_obscure,
6ecd4729 1979 &tdesc_filename_cmd_string,
23181151 1980 _("\
590042fc
PW
1981Set the file to read for an XML target description."), _("\
1982Show the file to read for an XML target description."), _("\
23181151
DJ
1983When set, GDB will read the target description from a local\n\
1984file instead of querying the remote target."),
1985 set_tdesc_filename_cmd,
1986 show_tdesc_filename_cmd,
1987 &tdesc_set_cmdlist, &tdesc_show_cmdlist);
1988
1989 add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\
590042fc
PW
1990Unset the file to read for an XML target description.\n\
1991When unset, GDB will read the description from the target."),
23181151 1992 &tdesc_unset_cmdlist);
81adfced 1993
ab33b152
AB
1994 auto grp = make_maint_print_c_tdesc_options_def_group (nullptr);
1995 static std::string help_text
1996 = gdb::option::build_help (_("\
1997Print the current target description as a C source file.\n\
1998Usage: maintenance print c-tdesc [OPTION] [FILENAME]\n\
1999\n\
2000Options:\n\
2001%OPTIONS%\n\
2002\n\
2003When FILENAME is not provided then print the current target\n\
2004description, otherwise an XML target description is read from\n\
2005FILENAME and printed as a C function.\n\
2006\n\
2007When '-single-feature' is used then the target description should\n\
2008contain a single feature and the generated C code will only create\n\
2009that feature within an already existing target_desc object."), grp);
2010 cmd = add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd,
2011 help_text.c_str (), &maintenanceprintlist);
2012 set_cmd_completer_handle_brkchars (cmd, maint_print_c_tdesc_cmd_completer);
27d41eac 2013
caa7fd04
AB
2014 cmd = add_cmd ("xml-tdesc", class_maintenance, maint_print_xml_tdesc_cmd, _("\
2015Print the current target description as an XML file."),
2016 &maintenanceprintlist);
2017 set_cmd_completer (cmd, filename_completer);
2018
27d41eac
YQ
2019 cmd = add_cmd ("xml-descriptions", class_maintenance,
2020 maintenance_check_xml_descriptions, _("\
590042fc 2021Check equality of GDB target descriptions and XML created descriptions.\n\
27d41eac
YQ
2022Check the target descriptions created in GDB equal the descriptions\n\
2023created from XML files in the directory.\n\
2024The parameter is the directory name."),
2025 &maintenancechecklist);
2026 set_cmd_completer (cmd, filename_completer);
23181151 2027}
This page took 1.809119 seconds and 4 git commands to generate.