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