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