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