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