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