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