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