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