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