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