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