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