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