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