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