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