| 1 | /* Target description support for GDB. |
| 2 | |
| 3 | Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
| 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 |
| 11 | the Free Software Foundation; either version 3 of the License, or |
| 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 |
| 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "arch-utils.h" |
| 24 | #include "gdbcmd.h" |
| 25 | #include "gdbtypes.h" |
| 26 | #include "reggroups.h" |
| 27 | #include "target.h" |
| 28 | #include "target-descriptions.h" |
| 29 | #include "vec.h" |
| 30 | #include "xml-support.h" |
| 31 | #include "xml-tdesc.h" |
| 32 | |
| 33 | #include "gdb_assert.h" |
| 34 | #include "gdb_obstack.h" |
| 35 | #include "hashtab.h" |
| 36 | |
| 37 | /* Types. */ |
| 38 | |
| 39 | typedef struct property |
| 40 | { |
| 41 | char *key; |
| 42 | char *value; |
| 43 | } property_s; |
| 44 | DEF_VEC_O(property_s); |
| 45 | |
| 46 | /* An individual register from a target description. */ |
| 47 | |
| 48 | typedef struct tdesc_reg |
| 49 | { |
| 50 | /* The name of this register. In standard features, it may be |
| 51 | recognized by the architecture support code, or it may be purely |
| 52 | for the user. */ |
| 53 | char *name; |
| 54 | |
| 55 | /* The register number used by this target to refer to this |
| 56 | register. This is used for remote p/P packets and to determine |
| 57 | the ordering of registers in the remote g/G packets. */ |
| 58 | long target_regnum; |
| 59 | |
| 60 | /* If this flag is set, GDB should save and restore this register |
| 61 | around calls to an inferior function. */ |
| 62 | int save_restore; |
| 63 | |
| 64 | /* The name of the register group containing this register, or NULL |
| 65 | if the group should be automatically determined from the |
| 66 | register's type. If this is "general", "float", or "vector", the |
| 67 | corresponding "info" command should display this register's |
| 68 | value. It can be an arbitrary string, but should be limited to |
| 69 | alphanumeric characters and internal hyphens. Currently other |
| 70 | strings are ignored (treated as NULL). */ |
| 71 | char *group; |
| 72 | |
| 73 | /* The size of the register, in bits. */ |
| 74 | int bitsize; |
| 75 | |
| 76 | /* The type of the register. This string corresponds to either |
| 77 | a named type from the target description or a predefined |
| 78 | type from GDB. */ |
| 79 | char *type; |
| 80 | |
| 81 | /* The target-described type corresponding to TYPE, if found. */ |
| 82 | struct tdesc_type *tdesc_type; |
| 83 | } *tdesc_reg_p; |
| 84 | DEF_VEC_P(tdesc_reg_p); |
| 85 | |
| 86 | /* A named type from a target description. */ |
| 87 | |
| 88 | typedef struct tdesc_type_field |
| 89 | { |
| 90 | char *name; |
| 91 | struct tdesc_type *type; |
| 92 | } tdesc_type_field; |
| 93 | DEF_VEC_O(tdesc_type_field); |
| 94 | |
| 95 | typedef struct tdesc_type |
| 96 | { |
| 97 | /* The name of this type. */ |
| 98 | char *name; |
| 99 | |
| 100 | /* Identify the kind of this type. */ |
| 101 | enum |
| 102 | { |
| 103 | /* Predefined types. */ |
| 104 | TDESC_TYPE_INT8, |
| 105 | TDESC_TYPE_INT16, |
| 106 | TDESC_TYPE_INT32, |
| 107 | TDESC_TYPE_INT64, |
| 108 | TDESC_TYPE_INT128, |
| 109 | TDESC_TYPE_UINT8, |
| 110 | TDESC_TYPE_UINT16, |
| 111 | TDESC_TYPE_UINT32, |
| 112 | TDESC_TYPE_UINT64, |
| 113 | TDESC_TYPE_UINT128, |
| 114 | TDESC_TYPE_CODE_PTR, |
| 115 | TDESC_TYPE_DATA_PTR, |
| 116 | TDESC_TYPE_IEEE_SINGLE, |
| 117 | TDESC_TYPE_IEEE_DOUBLE, |
| 118 | TDESC_TYPE_ARM_FPA_EXT, |
| 119 | |
| 120 | /* Types defined by a target feature. */ |
| 121 | TDESC_TYPE_VECTOR, |
| 122 | TDESC_TYPE_UNION |
| 123 | } kind; |
| 124 | |
| 125 | /* Kind-specific data. */ |
| 126 | union |
| 127 | { |
| 128 | /* Vector type. */ |
| 129 | struct |
| 130 | { |
| 131 | struct tdesc_type *type; |
| 132 | int count; |
| 133 | } v; |
| 134 | |
| 135 | /* Union type. */ |
| 136 | struct |
| 137 | { |
| 138 | VEC(tdesc_type_field) *fields; |
| 139 | } u; |
| 140 | } u; |
| 141 | } *tdesc_type_p; |
| 142 | DEF_VEC_P(tdesc_type_p); |
| 143 | |
| 144 | /* A feature from a target description. Each feature is a collection |
| 145 | of other elements, e.g. registers and types. */ |
| 146 | |
| 147 | typedef struct tdesc_feature |
| 148 | { |
| 149 | /* The name of this feature. It may be recognized by the architecture |
| 150 | support code. */ |
| 151 | char *name; |
| 152 | |
| 153 | /* The registers associated with this feature. */ |
| 154 | VEC(tdesc_reg_p) *registers; |
| 155 | |
| 156 | /* The types associated with this feature. */ |
| 157 | VEC(tdesc_type_p) *types; |
| 158 | } *tdesc_feature_p; |
| 159 | DEF_VEC_P(tdesc_feature_p); |
| 160 | |
| 161 | /* A compatible architecture from a target description. */ |
| 162 | typedef const struct bfd_arch_info *arch_p; |
| 163 | DEF_VEC_P(arch_p); |
| 164 | |
| 165 | /* A target description. */ |
| 166 | |
| 167 | struct target_desc |
| 168 | { |
| 169 | /* The architecture reported by the target, if any. */ |
| 170 | const struct bfd_arch_info *arch; |
| 171 | |
| 172 | /* The osabi reported by the target, if any; GDB_OSABI_UNKNOWN |
| 173 | otherwise. */ |
| 174 | enum gdb_osabi osabi; |
| 175 | |
| 176 | /* The list of compatible architectures reported by the target. */ |
| 177 | VEC(arch_p) *compatible; |
| 178 | |
| 179 | /* Any architecture-specific properties specified by the target. */ |
| 180 | VEC(property_s) *properties; |
| 181 | |
| 182 | /* The features associated with this target. */ |
| 183 | VEC(tdesc_feature_p) *features; |
| 184 | }; |
| 185 | |
| 186 | /* Per-architecture data associated with a target description. The |
| 187 | target description may be shared by multiple architectures, but |
| 188 | this data is private to one gdbarch. */ |
| 189 | |
| 190 | typedef struct tdesc_arch_reg |
| 191 | { |
| 192 | struct tdesc_reg *reg; |
| 193 | struct type *type; |
| 194 | } tdesc_arch_reg; |
| 195 | DEF_VEC_O(tdesc_arch_reg); |
| 196 | |
| 197 | struct tdesc_arch_data |
| 198 | { |
| 199 | /* A list of register/type pairs, indexed by GDB's internal register number. |
| 200 | During initialization of the gdbarch this list is used to store |
| 201 | registers which the architecture assigns a fixed register number. |
| 202 | Registers which are NULL in this array, or off the end, are |
| 203 | treated as zero-sized and nameless (i.e. placeholders in the |
| 204 | numbering). */ |
| 205 | VEC(tdesc_arch_reg) *arch_regs; |
| 206 | |
| 207 | /* Functions which report the register name, type, and reggroups for |
| 208 | pseudo-registers. */ |
| 209 | gdbarch_register_name_ftype *pseudo_register_name; |
| 210 | gdbarch_register_type_ftype *pseudo_register_type; |
| 211 | gdbarch_register_reggroup_p_ftype *pseudo_register_reggroup_p; |
| 212 | }; |
| 213 | |
| 214 | /* Global state. These variables are associated with the current |
| 215 | target; if GDB adds support for multiple simultaneous targets, then |
| 216 | these variables should become target-specific data. */ |
| 217 | |
| 218 | /* A flag indicating that a description has already been fetched from |
| 219 | the current target, so it should not be queried again. */ |
| 220 | |
| 221 | static int target_desc_fetched; |
| 222 | |
| 223 | /* The description fetched from the current target, or NULL if the |
| 224 | current target did not supply any description. Only valid when |
| 225 | target_desc_fetched is set. Only the description initialization |
| 226 | code should access this; normally, the description should be |
| 227 | accessed through the gdbarch object. */ |
| 228 | |
| 229 | static const struct target_desc *current_target_desc; |
| 230 | |
| 231 | /* Other global variables. */ |
| 232 | |
| 233 | /* The filename to read a target description from. */ |
| 234 | |
| 235 | static char *target_description_filename; |
| 236 | |
| 237 | /* A handle for architecture-specific data associated with the |
| 238 | target description (see struct tdesc_arch_data). */ |
| 239 | |
| 240 | static struct gdbarch_data *tdesc_data; |
| 241 | |
| 242 | /* Fetch the current target's description, and switch the current |
| 243 | architecture to one which incorporates that description. */ |
| 244 | |
| 245 | void |
| 246 | target_find_description (void) |
| 247 | { |
| 248 | /* If we've already fetched a description from the target, don't do |
| 249 | it again. This allows a target to fetch the description early, |
| 250 | during its to_open or to_create_inferior, if it needs extra |
| 251 | information about the target to initialize. */ |
| 252 | if (target_desc_fetched) |
| 253 | return; |
| 254 | |
| 255 | /* The current architecture should not have any target description |
| 256 | specified. It should have been cleared, e.g. when we |
| 257 | disconnected from the previous target. */ |
| 258 | gdb_assert (gdbarch_target_desc (target_gdbarch) == NULL); |
| 259 | |
| 260 | /* First try to fetch an XML description from the user-specified |
| 261 | file. */ |
| 262 | current_target_desc = NULL; |
| 263 | if (target_description_filename != NULL |
| 264 | && *target_description_filename != '\0') |
| 265 | current_target_desc |
| 266 | = file_read_description_xml (target_description_filename); |
| 267 | |
| 268 | /* Next try to read the description from the current target using |
| 269 | target objects. */ |
| 270 | if (current_target_desc == NULL) |
| 271 | current_target_desc = target_read_description_xml (¤t_target); |
| 272 | |
| 273 | /* If that failed try a target-specific hook. */ |
| 274 | if (current_target_desc == NULL) |
| 275 | current_target_desc = target_read_description (¤t_target); |
| 276 | |
| 277 | /* If a non-NULL description was returned, then update the current |
| 278 | architecture. */ |
| 279 | if (current_target_desc) |
| 280 | { |
| 281 | struct gdbarch_info info; |
| 282 | |
| 283 | gdbarch_info_init (&info); |
| 284 | info.target_desc = current_target_desc; |
| 285 | if (!gdbarch_update_p (info)) |
| 286 | warning (_("Architecture rejected target-supplied description")); |
| 287 | else |
| 288 | { |
| 289 | struct tdesc_arch_data *data; |
| 290 | |
| 291 | data = gdbarch_data (target_gdbarch, tdesc_data); |
| 292 | if (tdesc_has_registers (current_target_desc) |
| 293 | && data->arch_regs == NULL) |
| 294 | warning (_("Target-supplied registers are not supported " |
| 295 | "by the current architecture")); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* Now that we know this description is usable, record that we |
| 300 | fetched it. */ |
| 301 | target_desc_fetched = 1; |
| 302 | } |
| 303 | |
| 304 | /* Discard any description fetched from the current target, and switch |
| 305 | the current architecture to one with no target description. */ |
| 306 | |
| 307 | void |
| 308 | target_clear_description (void) |
| 309 | { |
| 310 | struct gdbarch_info info; |
| 311 | |
| 312 | if (!target_desc_fetched) |
| 313 | return; |
| 314 | |
| 315 | target_desc_fetched = 0; |
| 316 | current_target_desc = NULL; |
| 317 | |
| 318 | gdbarch_info_init (&info); |
| 319 | if (!gdbarch_update_p (info)) |
| 320 | internal_error (__FILE__, __LINE__, |
| 321 | _("Could not remove target-supplied description")); |
| 322 | } |
| 323 | |
| 324 | /* Return the global current target description. This should only be |
| 325 | used by gdbarch initialization code; most access should be through |
| 326 | an existing gdbarch. */ |
| 327 | |
| 328 | const struct target_desc * |
| 329 | target_current_description (void) |
| 330 | { |
| 331 | if (target_desc_fetched) |
| 332 | return current_target_desc; |
| 333 | |
| 334 | return NULL; |
| 335 | } |
| 336 | |
| 337 | /* Return non-zero if this target description is compatible |
| 338 | with the given BFD architecture. */ |
| 339 | |
| 340 | int |
| 341 | tdesc_compatible_p (const struct target_desc *target_desc, |
| 342 | const struct bfd_arch_info *arch) |
| 343 | { |
| 344 | const struct bfd_arch_info *compat; |
| 345 | int ix; |
| 346 | |
| 347 | for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat); |
| 348 | ix++) |
| 349 | { |
| 350 | if (compat == arch |
| 351 | || arch->compatible (arch, compat) |
| 352 | || compat->compatible (compat, arch)) |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | \f |
| 359 | |
| 360 | /* Direct accessors for target descriptions. */ |
| 361 | |
| 362 | /* Return the string value of a property named KEY, or NULL if the |
| 363 | property was not specified. */ |
| 364 | |
| 365 | const char * |
| 366 | tdesc_property (const struct target_desc *target_desc, const char *key) |
| 367 | { |
| 368 | struct property *prop; |
| 369 | int ix; |
| 370 | |
| 371 | for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop); |
| 372 | ix++) |
| 373 | if (strcmp (prop->key, key) == 0) |
| 374 | return prop->value; |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | /* Return the BFD architecture associated with this target |
| 380 | description, or NULL if no architecture was specified. */ |
| 381 | |
| 382 | const struct bfd_arch_info * |
| 383 | tdesc_architecture (const struct target_desc *target_desc) |
| 384 | { |
| 385 | return target_desc->arch; |
| 386 | } |
| 387 | |
| 388 | /* Return the OSABI associated with this target description, or |
| 389 | GDB_OSABI_UNKNOWN if no osabi was specified. */ |
| 390 | |
| 391 | enum gdb_osabi |
| 392 | tdesc_osabi (const struct target_desc *target_desc) |
| 393 | { |
| 394 | return target_desc->osabi; |
| 395 | } |
| 396 | |
| 397 | \f |
| 398 | |
| 399 | /* Return 1 if this target description includes any registers. */ |
| 400 | |
| 401 | int |
| 402 | tdesc_has_registers (const struct target_desc *target_desc) |
| 403 | { |
| 404 | int ix; |
| 405 | struct tdesc_feature *feature; |
| 406 | |
| 407 | if (target_desc == NULL) |
| 408 | return 0; |
| 409 | |
| 410 | for (ix = 0; |
| 411 | VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature); |
| 412 | ix++) |
| 413 | if (! VEC_empty (tdesc_reg_p, feature->registers)) |
| 414 | return 1; |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | /* Return the feature with the given name, if present, or NULL if |
| 420 | the named feature is not found. */ |
| 421 | |
| 422 | const struct tdesc_feature * |
| 423 | tdesc_find_feature (const struct target_desc *target_desc, |
| 424 | const char *name) |
| 425 | { |
| 426 | int ix; |
| 427 | struct tdesc_feature *feature; |
| 428 | |
| 429 | for (ix = 0; |
| 430 | VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature); |
| 431 | ix++) |
| 432 | if (strcmp (feature->name, name) == 0) |
| 433 | return feature; |
| 434 | |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | /* Return the name of FEATURE. */ |
| 439 | |
| 440 | const char * |
| 441 | tdesc_feature_name (const struct tdesc_feature *feature) |
| 442 | { |
| 443 | return feature->name; |
| 444 | } |
| 445 | |
| 446 | /* Predefined types. */ |
| 447 | static struct tdesc_type tdesc_predefined_types[] = |
| 448 | { |
| 449 | { "int8", TDESC_TYPE_INT8 }, |
| 450 | { "int16", TDESC_TYPE_INT16 }, |
| 451 | { "int32", TDESC_TYPE_INT32 }, |
| 452 | { "int64", TDESC_TYPE_INT64 }, |
| 453 | { "int128", TDESC_TYPE_INT128 }, |
| 454 | { "uint8", TDESC_TYPE_UINT8 }, |
| 455 | { "uint16", TDESC_TYPE_UINT16 }, |
| 456 | { "uint32", TDESC_TYPE_UINT32 }, |
| 457 | { "uint64", TDESC_TYPE_UINT64 }, |
| 458 | { "uint128", TDESC_TYPE_UINT128 }, |
| 459 | { "code_ptr", TDESC_TYPE_CODE_PTR }, |
| 460 | { "data_ptr", TDESC_TYPE_DATA_PTR }, |
| 461 | { "ieee_single", TDESC_TYPE_IEEE_SINGLE }, |
| 462 | { "ieee_double", TDESC_TYPE_IEEE_DOUBLE }, |
| 463 | { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT } |
| 464 | }; |
| 465 | |
| 466 | /* Return the type associated with ID in the context of FEATURE, or |
| 467 | NULL if none. */ |
| 468 | |
| 469 | struct tdesc_type * |
| 470 | tdesc_named_type (const struct tdesc_feature *feature, const char *id) |
| 471 | { |
| 472 | int ix; |
| 473 | struct tdesc_type *type; |
| 474 | |
| 475 | /* First try target-defined types. */ |
| 476 | for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++) |
| 477 | if (strcmp (type->name, id) == 0) |
| 478 | return type; |
| 479 | |
| 480 | /* Next try the predefined types. */ |
| 481 | for (ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++) |
| 482 | if (strcmp (tdesc_predefined_types[ix].name, id) == 0) |
| 483 | return &tdesc_predefined_types[ix]; |
| 484 | |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | /* Construct, if necessary, and return the GDB type implementing target |
| 489 | type TDESC_TYPE for architecture GDBARCH. */ |
| 490 | |
| 491 | static struct type * |
| 492 | tdesc_gdb_type (struct gdbarch *gdbarch, struct tdesc_type *tdesc_type) |
| 493 | { |
| 494 | switch (tdesc_type->kind) |
| 495 | { |
| 496 | /* Predefined types. */ |
| 497 | case TDESC_TYPE_INT8: |
| 498 | return builtin_type (gdbarch)->builtin_int8; |
| 499 | |
| 500 | case TDESC_TYPE_INT16: |
| 501 | return builtin_type (gdbarch)->builtin_int16; |
| 502 | |
| 503 | case TDESC_TYPE_INT32: |
| 504 | return builtin_type (gdbarch)->builtin_int32; |
| 505 | |
| 506 | case TDESC_TYPE_INT64: |
| 507 | return builtin_type (gdbarch)->builtin_int64; |
| 508 | |
| 509 | case TDESC_TYPE_INT128: |
| 510 | return builtin_type (gdbarch)->builtin_int128; |
| 511 | |
| 512 | case TDESC_TYPE_UINT8: |
| 513 | return builtin_type (gdbarch)->builtin_uint8; |
| 514 | |
| 515 | case TDESC_TYPE_UINT16: |
| 516 | return builtin_type (gdbarch)->builtin_uint16; |
| 517 | |
| 518 | case TDESC_TYPE_UINT32: |
| 519 | return builtin_type (gdbarch)->builtin_uint32; |
| 520 | |
| 521 | case TDESC_TYPE_UINT64: |
| 522 | return builtin_type (gdbarch)->builtin_uint64; |
| 523 | |
| 524 | case TDESC_TYPE_UINT128: |
| 525 | return builtin_type (gdbarch)->builtin_uint128; |
| 526 | |
| 527 | case TDESC_TYPE_CODE_PTR: |
| 528 | return builtin_type (gdbarch)->builtin_func_ptr; |
| 529 | |
| 530 | case TDESC_TYPE_DATA_PTR: |
| 531 | return builtin_type (gdbarch)->builtin_data_ptr; |
| 532 | |
| 533 | case TDESC_TYPE_IEEE_SINGLE: |
| 534 | return arch_float_type (gdbarch, -1, "builtin_type_ieee_single", |
| 535 | floatformats_ieee_single); |
| 536 | |
| 537 | case TDESC_TYPE_IEEE_DOUBLE: |
| 538 | return arch_float_type (gdbarch, -1, "builtin_type_ieee_double", |
| 539 | floatformats_ieee_double); |
| 540 | |
| 541 | case TDESC_TYPE_ARM_FPA_EXT: |
| 542 | return arch_float_type (gdbarch, -1, "builtin_type_arm_ext", |
| 543 | floatformats_arm_ext); |
| 544 | |
| 545 | /* Types defined by a target feature. */ |
| 546 | case TDESC_TYPE_VECTOR: |
| 547 | { |
| 548 | struct type *type, *field_type; |
| 549 | |
| 550 | field_type = tdesc_gdb_type (gdbarch, tdesc_type->u.v.type); |
| 551 | type = init_vector_type (field_type, tdesc_type->u.v.count); |
| 552 | TYPE_NAME (type) = xstrdup (tdesc_type->name); |
| 553 | |
| 554 | return type; |
| 555 | } |
| 556 | |
| 557 | case TDESC_TYPE_UNION: |
| 558 | { |
| 559 | struct type *type, *field_type; |
| 560 | struct tdesc_type_field *f; |
| 561 | int ix; |
| 562 | |
| 563 | type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION); |
| 564 | TYPE_NAME (type) = xstrdup (tdesc_type->name); |
| 565 | |
| 566 | for (ix = 0; |
| 567 | VEC_iterate (tdesc_type_field, tdesc_type->u.u.fields, ix, f); |
| 568 | ix++) |
| 569 | { |
| 570 | field_type = tdesc_gdb_type (gdbarch, f->type); |
| 571 | append_composite_type_field (type, xstrdup (f->name), field_type); |
| 572 | |
| 573 | /* If any of the children of this union are vectors, flag the |
| 574 | union as a vector also. This allows e.g. a union of two |
| 575 | vector types to show up automatically in "info vector". */ |
| 576 | if (TYPE_VECTOR (field_type)) |
| 577 | TYPE_VECTOR (type) = 1; |
| 578 | } |
| 579 | |
| 580 | return type; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | internal_error (__FILE__, __LINE__, |
| 585 | "Type \"%s\" has an unknown kind %d", |
| 586 | tdesc_type->name, tdesc_type->kind); |
| 587 | } |
| 588 | \f |
| 589 | |
| 590 | /* Support for registers from target descriptions. */ |
| 591 | |
| 592 | /* Construct the per-gdbarch data. */ |
| 593 | |
| 594 | static void * |
| 595 | tdesc_data_init (struct obstack *obstack) |
| 596 | { |
| 597 | struct tdesc_arch_data *data; |
| 598 | |
| 599 | data = OBSTACK_ZALLOC (obstack, struct tdesc_arch_data); |
| 600 | return data; |
| 601 | } |
| 602 | |
| 603 | /* Similar, but for the temporary copy used during architecture |
| 604 | initialization. */ |
| 605 | |
| 606 | struct tdesc_arch_data * |
| 607 | tdesc_data_alloc (void) |
| 608 | { |
| 609 | return XZALLOC (struct tdesc_arch_data); |
| 610 | } |
| 611 | |
| 612 | /* Free something allocated by tdesc_data_alloc, if it is not going |
| 613 | to be used (for instance if it was unsuitable for the |
| 614 | architecture). */ |
| 615 | |
| 616 | void |
| 617 | tdesc_data_cleanup (void *data_untyped) |
| 618 | { |
| 619 | struct tdesc_arch_data *data = data_untyped; |
| 620 | |
| 621 | VEC_free (tdesc_arch_reg, data->arch_regs); |
| 622 | xfree (data); |
| 623 | } |
| 624 | |
| 625 | /* Search FEATURE for a register named NAME. */ |
| 626 | |
| 627 | static struct tdesc_reg * |
| 628 | tdesc_find_register_early (const struct tdesc_feature *feature, |
| 629 | const char *name) |
| 630 | { |
| 631 | int ixr; |
| 632 | struct tdesc_reg *reg; |
| 633 | |
| 634 | for (ixr = 0; |
| 635 | VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg); |
| 636 | ixr++) |
| 637 | if (strcasecmp (reg->name, name) == 0) |
| 638 | return reg; |
| 639 | |
| 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | /* Search FEATURE for a register named NAME. Assign REGNO to it. */ |
| 644 | |
| 645 | int |
| 646 | tdesc_numbered_register (const struct tdesc_feature *feature, |
| 647 | struct tdesc_arch_data *data, |
| 648 | int regno, const char *name) |
| 649 | { |
| 650 | struct tdesc_arch_reg arch_reg = { 0 }; |
| 651 | struct tdesc_reg *reg = tdesc_find_register_early (feature, name); |
| 652 | |
| 653 | if (reg == NULL) |
| 654 | return 0; |
| 655 | |
| 656 | /* Make sure the vector includes a REGNO'th element. */ |
| 657 | while (regno >= VEC_length (tdesc_arch_reg, data->arch_regs)) |
| 658 | VEC_safe_push (tdesc_arch_reg, data->arch_regs, &arch_reg); |
| 659 | |
| 660 | arch_reg.reg = reg; |
| 661 | VEC_replace (tdesc_arch_reg, data->arch_regs, regno, &arch_reg); |
| 662 | return 1; |
| 663 | } |
| 664 | |
| 665 | /* Search FEATURE for a register named NAME, but do not assign a fixed |
| 666 | register number to it. */ |
| 667 | |
| 668 | int |
| 669 | tdesc_unnumbered_register (const struct tdesc_feature *feature, |
| 670 | const char *name) |
| 671 | { |
| 672 | struct tdesc_reg *reg = tdesc_find_register_early (feature, name); |
| 673 | |
| 674 | if (reg == NULL) |
| 675 | return 0; |
| 676 | |
| 677 | return 1; |
| 678 | } |
| 679 | |
| 680 | /* Search FEATURE for a register whose name is in NAMES and assign |
| 681 | REGNO to it. */ |
| 682 | |
| 683 | int |
| 684 | tdesc_numbered_register_choices (const struct tdesc_feature *feature, |
| 685 | struct tdesc_arch_data *data, |
| 686 | int regno, const char *const names[]) |
| 687 | { |
| 688 | int i; |
| 689 | |
| 690 | for (i = 0; names[i] != NULL; i++) |
| 691 | if (tdesc_numbered_register (feature, data, regno, names[i])) |
| 692 | return 1; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /* Search FEATURE for a register named NAME, and return its size in |
| 698 | bits. The register must exist. */ |
| 699 | |
| 700 | int |
| 701 | tdesc_register_size (const struct tdesc_feature *feature, |
| 702 | const char *name) |
| 703 | { |
| 704 | struct tdesc_reg *reg = tdesc_find_register_early (feature, name); |
| 705 | |
| 706 | gdb_assert (reg != NULL); |
| 707 | return reg->bitsize; |
| 708 | } |
| 709 | |
| 710 | /* Look up a register by its GDB internal register number. */ |
| 711 | |
| 712 | static struct tdesc_arch_reg * |
| 713 | tdesc_find_arch_register (struct gdbarch *gdbarch, int regno) |
| 714 | { |
| 715 | struct tdesc_arch_reg *reg; |
| 716 | struct tdesc_arch_data *data; |
| 717 | |
| 718 | data = gdbarch_data (gdbarch, tdesc_data); |
| 719 | if (regno < VEC_length (tdesc_arch_reg, data->arch_regs)) |
| 720 | return VEC_index (tdesc_arch_reg, data->arch_regs, regno); |
| 721 | else |
| 722 | return NULL; |
| 723 | } |
| 724 | |
| 725 | static struct tdesc_reg * |
| 726 | tdesc_find_register (struct gdbarch *gdbarch, int regno) |
| 727 | { |
| 728 | struct tdesc_arch_reg *reg = tdesc_find_arch_register (gdbarch, regno); |
| 729 | return reg? reg->reg : NULL; |
| 730 | } |
| 731 | |
| 732 | /* Return the name of register REGNO, from the target description or |
| 733 | from an architecture-provided pseudo_register_name method. */ |
| 734 | |
| 735 | const char * |
| 736 | tdesc_register_name (struct gdbarch *gdbarch, int regno) |
| 737 | { |
| 738 | struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno); |
| 739 | int num_regs = gdbarch_num_regs (gdbarch); |
| 740 | int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch); |
| 741 | |
| 742 | if (reg != NULL) |
| 743 | return reg->name; |
| 744 | |
| 745 | if (regno >= num_regs && regno < num_regs + num_pseudo_regs) |
| 746 | { |
| 747 | struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data); |
| 748 | gdb_assert (data->pseudo_register_name != NULL); |
| 749 | return data->pseudo_register_name (gdbarch, regno); |
| 750 | } |
| 751 | |
| 752 | return ""; |
| 753 | } |
| 754 | |
| 755 | struct type * |
| 756 | tdesc_register_type (struct gdbarch *gdbarch, int regno) |
| 757 | { |
| 758 | struct tdesc_arch_reg *arch_reg = tdesc_find_arch_register (gdbarch, regno); |
| 759 | struct tdesc_reg *reg = arch_reg? arch_reg->reg : NULL; |
| 760 | int num_regs = gdbarch_num_regs (gdbarch); |
| 761 | int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch); |
| 762 | |
| 763 | if (reg == NULL && regno >= num_regs && regno < num_regs + num_pseudo_regs) |
| 764 | { |
| 765 | struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data); |
| 766 | gdb_assert (data->pseudo_register_type != NULL); |
| 767 | return data->pseudo_register_type (gdbarch, regno); |
| 768 | } |
| 769 | |
| 770 | if (reg == NULL) |
| 771 | /* Return "int0_t", since "void" has a misleading size of one. */ |
| 772 | return builtin_type (gdbarch)->builtin_int0; |
| 773 | |
| 774 | if (arch_reg->type == NULL) |
| 775 | { |
| 776 | /* First check for a predefined or target defined type. */ |
| 777 | if (reg->tdesc_type) |
| 778 | arch_reg->type = tdesc_gdb_type (gdbarch, reg->tdesc_type); |
| 779 | |
| 780 | /* Next try size-sensitive type shortcuts. */ |
| 781 | else if (strcmp (reg->type, "float") == 0) |
| 782 | { |
| 783 | if (reg->bitsize == gdbarch_float_bit (gdbarch)) |
| 784 | arch_reg->type = builtin_type (gdbarch)->builtin_float; |
| 785 | else if (reg->bitsize == gdbarch_double_bit (gdbarch)) |
| 786 | arch_reg->type = builtin_type (gdbarch)->builtin_double; |
| 787 | else if (reg->bitsize == gdbarch_long_double_bit (gdbarch)) |
| 788 | arch_reg->type = builtin_type (gdbarch)->builtin_long_double; |
| 789 | else |
| 790 | { |
| 791 | warning (_("Register \"%s\" has an unsupported size (%d bits)"), |
| 792 | reg->name, reg->bitsize); |
| 793 | arch_reg->type = builtin_type (gdbarch)->builtin_double; |
| 794 | } |
| 795 | } |
| 796 | else if (strcmp (reg->type, "int") == 0) |
| 797 | { |
| 798 | if (reg->bitsize == gdbarch_long_bit (gdbarch)) |
| 799 | arch_reg->type = builtin_type (gdbarch)->builtin_long; |
| 800 | else if (reg->bitsize == TARGET_CHAR_BIT) |
| 801 | arch_reg->type = builtin_type (gdbarch)->builtin_char; |
| 802 | else if (reg->bitsize == gdbarch_short_bit (gdbarch)) |
| 803 | arch_reg->type = builtin_type (gdbarch)->builtin_short; |
| 804 | else if (reg->bitsize == gdbarch_int_bit (gdbarch)) |
| 805 | arch_reg->type = builtin_type (gdbarch)->builtin_int; |
| 806 | else if (reg->bitsize == gdbarch_long_long_bit (gdbarch)) |
| 807 | arch_reg->type = builtin_type (gdbarch)->builtin_long_long; |
| 808 | else if (reg->bitsize == gdbarch_ptr_bit (gdbarch)) |
| 809 | /* A bit desperate by this point... */ |
| 810 | arch_reg->type = builtin_type (gdbarch)->builtin_data_ptr; |
| 811 | else |
| 812 | { |
| 813 | warning (_("Register \"%s\" has an unsupported size (%d bits)"), |
| 814 | reg->name, reg->bitsize); |
| 815 | arch_reg->type = builtin_type (gdbarch)->builtin_long; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (arch_reg->type == NULL) |
| 820 | internal_error (__FILE__, __LINE__, |
| 821 | "Register \"%s\" has an unknown type \"%s\"", |
| 822 | reg->name, reg->type); |
| 823 | } |
| 824 | |
| 825 | return arch_reg->type; |
| 826 | } |
| 827 | |
| 828 | static int |
| 829 | tdesc_remote_register_number (struct gdbarch *gdbarch, int regno) |
| 830 | { |
| 831 | struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno); |
| 832 | |
| 833 | if (reg != NULL) |
| 834 | return reg->target_regnum; |
| 835 | else |
| 836 | return -1; |
| 837 | } |
| 838 | |
| 839 | /* Check whether REGNUM is a member of REGGROUP. Registers from the |
| 840 | target description may be classified as general, float, or vector. |
| 841 | Unlike a gdbarch register_reggroup_p method, this function will |
| 842 | return -1 if it does not know; the caller should handle registers |
| 843 | with no specified group. |
| 844 | |
| 845 | Arbitrary strings (other than "general", "float", and "vector") |
| 846 | from the description are not used; they cause the register to be |
| 847 | displayed in "info all-registers" but excluded from "info |
| 848 | registers" et al. The names of containing features are also not |
| 849 | used. This might be extended to display registers in some more |
| 850 | useful groupings. |
| 851 | |
| 852 | The save-restore flag is also implemented here. */ |
| 853 | |
| 854 | int |
| 855 | tdesc_register_in_reggroup_p (struct gdbarch *gdbarch, int regno, |
| 856 | struct reggroup *reggroup) |
| 857 | { |
| 858 | struct tdesc_reg *reg = tdesc_find_register (gdbarch, regno); |
| 859 | |
| 860 | if (reg != NULL && reg->group != NULL) |
| 861 | { |
| 862 | int general_p = 0, float_p = 0, vector_p = 0; |
| 863 | |
| 864 | if (strcmp (reg->group, "general") == 0) |
| 865 | general_p = 1; |
| 866 | else if (strcmp (reg->group, "float") == 0) |
| 867 | float_p = 1; |
| 868 | else if (strcmp (reg->group, "vector") == 0) |
| 869 | vector_p = 1; |
| 870 | |
| 871 | if (reggroup == float_reggroup) |
| 872 | return float_p; |
| 873 | |
| 874 | if (reggroup == vector_reggroup) |
| 875 | return vector_p; |
| 876 | |
| 877 | if (reggroup == general_reggroup) |
| 878 | return general_p; |
| 879 | } |
| 880 | |
| 881 | if (reg != NULL |
| 882 | && (reggroup == save_reggroup || reggroup == restore_reggroup)) |
| 883 | return reg->save_restore; |
| 884 | |
| 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | /* Check whether REGNUM is a member of REGGROUP. Registers with no |
| 889 | group specified go to the default reggroup function and are handled |
| 890 | by type. */ |
| 891 | |
| 892 | static int |
| 893 | tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regno, |
| 894 | struct reggroup *reggroup) |
| 895 | { |
| 896 | int num_regs = gdbarch_num_regs (gdbarch); |
| 897 | int num_pseudo_regs = gdbarch_num_pseudo_regs (gdbarch); |
| 898 | int ret; |
| 899 | |
| 900 | if (regno >= num_regs && regno < num_regs + num_pseudo_regs) |
| 901 | { |
| 902 | struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data); |
| 903 | if (data->pseudo_register_reggroup_p != NULL) |
| 904 | return data->pseudo_register_reggroup_p (gdbarch, regno, reggroup); |
| 905 | /* Otherwise fall through to the default reggroup_p. */ |
| 906 | } |
| 907 | |
| 908 | ret = tdesc_register_in_reggroup_p (gdbarch, regno, reggroup); |
| 909 | if (ret != -1) |
| 910 | return ret; |
| 911 | |
| 912 | return default_register_reggroup_p (gdbarch, regno, reggroup); |
| 913 | } |
| 914 | |
| 915 | /* Record architecture-specific functions to call for pseudo-register |
| 916 | support. */ |
| 917 | |
| 918 | void |
| 919 | set_tdesc_pseudo_register_name (struct gdbarch *gdbarch, |
| 920 | gdbarch_register_name_ftype *pseudo_name) |
| 921 | { |
| 922 | struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data); |
| 923 | |
| 924 | data->pseudo_register_name = pseudo_name; |
| 925 | } |
| 926 | |
| 927 | void |
| 928 | set_tdesc_pseudo_register_type (struct gdbarch *gdbarch, |
| 929 | gdbarch_register_type_ftype *pseudo_type) |
| 930 | { |
| 931 | struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data); |
| 932 | |
| 933 | data->pseudo_register_type = pseudo_type; |
| 934 | } |
| 935 | |
| 936 | void |
| 937 | set_tdesc_pseudo_register_reggroup_p |
| 938 | (struct gdbarch *gdbarch, |
| 939 | gdbarch_register_reggroup_p_ftype *pseudo_reggroup_p) |
| 940 | { |
| 941 | struct tdesc_arch_data *data = gdbarch_data (gdbarch, tdesc_data); |
| 942 | |
| 943 | data->pseudo_register_reggroup_p = pseudo_reggroup_p; |
| 944 | } |
| 945 | |
| 946 | /* Update GDBARCH to use the target description for registers. */ |
| 947 | |
| 948 | void |
| 949 | tdesc_use_registers (struct gdbarch *gdbarch, |
| 950 | const struct target_desc *target_desc, |
| 951 | struct tdesc_arch_data *early_data) |
| 952 | { |
| 953 | int num_regs = gdbarch_num_regs (gdbarch); |
| 954 | int i, ixf, ixr; |
| 955 | struct tdesc_feature *feature; |
| 956 | struct tdesc_reg *reg; |
| 957 | struct tdesc_arch_data *data; |
| 958 | struct tdesc_arch_reg *arch_reg, new_arch_reg = { 0 }; |
| 959 | htab_t reg_hash; |
| 960 | |
| 961 | /* We can't use the description for registers if it doesn't describe |
| 962 | any. This function should only be called after validating |
| 963 | registers, so the caller should know that registers are |
| 964 | included. */ |
| 965 | gdb_assert (tdesc_has_registers (target_desc)); |
| 966 | |
| 967 | data = gdbarch_data (gdbarch, tdesc_data); |
| 968 | data->arch_regs = early_data->arch_regs; |
| 969 | xfree (early_data); |
| 970 | |
| 971 | /* Build up a set of all registers, so that we can assign register |
| 972 | numbers where needed. The hash table expands as necessary, so |
| 973 | the initial size is arbitrary. */ |
| 974 | reg_hash = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); |
| 975 | for (ixf = 0; |
| 976 | VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature); |
| 977 | ixf++) |
| 978 | for (ixr = 0; |
| 979 | VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg); |
| 980 | ixr++) |
| 981 | { |
| 982 | void **slot = htab_find_slot (reg_hash, reg, INSERT); |
| 983 | |
| 984 | *slot = reg; |
| 985 | } |
| 986 | |
| 987 | /* Remove any registers which were assigned numbers by the |
| 988 | architecture. */ |
| 989 | for (ixr = 0; |
| 990 | VEC_iterate (tdesc_arch_reg, data->arch_regs, ixr, arch_reg); |
| 991 | ixr++) |
| 992 | if (arch_reg->reg) |
| 993 | htab_remove_elt (reg_hash, arch_reg->reg); |
| 994 | |
| 995 | /* Assign numbers to the remaining registers and add them to the |
| 996 | list of registers. The new numbers are always above gdbarch_num_regs. |
| 997 | Iterate over the features, not the hash table, so that the order |
| 998 | matches that in the target description. */ |
| 999 | |
| 1000 | gdb_assert (VEC_length (tdesc_arch_reg, data->arch_regs) <= num_regs); |
| 1001 | while (VEC_length (tdesc_arch_reg, data->arch_regs) < num_regs) |
| 1002 | VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg); |
| 1003 | for (ixf = 0; |
| 1004 | VEC_iterate (tdesc_feature_p, target_desc->features, ixf, feature); |
| 1005 | ixf++) |
| 1006 | for (ixr = 0; |
| 1007 | VEC_iterate (tdesc_reg_p, feature->registers, ixr, reg); |
| 1008 | ixr++) |
| 1009 | if (htab_find (reg_hash, reg) != NULL) |
| 1010 | { |
| 1011 | new_arch_reg.reg = reg; |
| 1012 | VEC_safe_push (tdesc_arch_reg, data->arch_regs, &new_arch_reg); |
| 1013 | num_regs++; |
| 1014 | } |
| 1015 | |
| 1016 | htab_delete (reg_hash); |
| 1017 | |
| 1018 | /* Update the architecture. */ |
| 1019 | set_gdbarch_num_regs (gdbarch, num_regs); |
| 1020 | set_gdbarch_register_name (gdbarch, tdesc_register_name); |
| 1021 | set_gdbarch_register_type (gdbarch, tdesc_register_type); |
| 1022 | set_gdbarch_remote_register_number (gdbarch, |
| 1023 | tdesc_remote_register_number); |
| 1024 | set_gdbarch_register_reggroup_p (gdbarch, tdesc_register_reggroup_p); |
| 1025 | } |
| 1026 | \f |
| 1027 | |
| 1028 | /* Methods for constructing a target description. */ |
| 1029 | |
| 1030 | static void |
| 1031 | tdesc_free_reg (struct tdesc_reg *reg) |
| 1032 | { |
| 1033 | xfree (reg->name); |
| 1034 | xfree (reg->type); |
| 1035 | xfree (reg->group); |
| 1036 | xfree (reg); |
| 1037 | } |
| 1038 | |
| 1039 | void |
| 1040 | tdesc_create_reg (struct tdesc_feature *feature, const char *name, |
| 1041 | int regnum, int save_restore, const char *group, |
| 1042 | int bitsize, const char *type) |
| 1043 | { |
| 1044 | struct tdesc_reg *reg = XZALLOC (struct tdesc_reg); |
| 1045 | |
| 1046 | reg->name = xstrdup (name); |
| 1047 | reg->target_regnum = regnum; |
| 1048 | reg->save_restore = save_restore; |
| 1049 | reg->group = group ? xstrdup (group) : NULL; |
| 1050 | reg->bitsize = bitsize; |
| 1051 | reg->type = type ? xstrdup (type) : xstrdup ("<unknown>"); |
| 1052 | |
| 1053 | /* If the register's type is target-defined, look it up now. We may not |
| 1054 | have easy access to the containing feature when we want it later. */ |
| 1055 | reg->tdesc_type = tdesc_named_type (feature, reg->type); |
| 1056 | |
| 1057 | VEC_safe_push (tdesc_reg_p, feature->registers, reg); |
| 1058 | } |
| 1059 | |
| 1060 | static void |
| 1061 | tdesc_free_type (struct tdesc_type *type) |
| 1062 | { |
| 1063 | |
| 1064 | switch (type->kind) |
| 1065 | { |
| 1066 | case TDESC_TYPE_UNION: |
| 1067 | { |
| 1068 | struct tdesc_type_field *f; |
| 1069 | int ix; |
| 1070 | |
| 1071 | for (ix = 0; |
| 1072 | VEC_iterate (tdesc_type_field, type->u.u.fields, ix, f); |
| 1073 | ix++) |
| 1074 | xfree (f->name); |
| 1075 | |
| 1076 | VEC_free (tdesc_type_field, type->u.u.fields); |
| 1077 | } |
| 1078 | break; |
| 1079 | |
| 1080 | default: |
| 1081 | break; |
| 1082 | } |
| 1083 | |
| 1084 | xfree (type->name); |
| 1085 | xfree (type); |
| 1086 | } |
| 1087 | |
| 1088 | struct tdesc_type * |
| 1089 | tdesc_create_vector (struct tdesc_feature *feature, const char *name, |
| 1090 | struct tdesc_type *field_type, int count) |
| 1091 | { |
| 1092 | struct tdesc_type *type = XZALLOC (struct tdesc_type); |
| 1093 | |
| 1094 | type->name = xstrdup (name); |
| 1095 | type->kind = TDESC_TYPE_VECTOR; |
| 1096 | type->u.v.type = field_type; |
| 1097 | type->u.v.count = count; |
| 1098 | |
| 1099 | VEC_safe_push (tdesc_type_p, feature->types, type); |
| 1100 | return type; |
| 1101 | } |
| 1102 | |
| 1103 | struct tdesc_type * |
| 1104 | tdesc_create_union (struct tdesc_feature *feature, const char *name) |
| 1105 | { |
| 1106 | struct tdesc_type *type = XZALLOC (struct tdesc_type); |
| 1107 | |
| 1108 | type->name = xstrdup (name); |
| 1109 | type->kind = TDESC_TYPE_UNION; |
| 1110 | |
| 1111 | VEC_safe_push (tdesc_type_p, feature->types, type); |
| 1112 | return type; |
| 1113 | } |
| 1114 | |
| 1115 | void |
| 1116 | tdesc_add_field (struct tdesc_type *type, const char *field_name, |
| 1117 | struct tdesc_type *field_type) |
| 1118 | { |
| 1119 | struct tdesc_type_field f = { 0 }; |
| 1120 | |
| 1121 | gdb_assert (type->kind == TDESC_TYPE_UNION); |
| 1122 | |
| 1123 | f.name = xstrdup (field_name); |
| 1124 | f.type = field_type; |
| 1125 | |
| 1126 | VEC_safe_push (tdesc_type_field, type->u.u.fields, &f); |
| 1127 | } |
| 1128 | |
| 1129 | static void |
| 1130 | tdesc_free_feature (struct tdesc_feature *feature) |
| 1131 | { |
| 1132 | struct tdesc_reg *reg; |
| 1133 | struct tdesc_type *type; |
| 1134 | int ix; |
| 1135 | |
| 1136 | for (ix = 0; VEC_iterate (tdesc_reg_p, feature->registers, ix, reg); ix++) |
| 1137 | tdesc_free_reg (reg); |
| 1138 | VEC_free (tdesc_reg_p, feature->registers); |
| 1139 | |
| 1140 | for (ix = 0; VEC_iterate (tdesc_type_p, feature->types, ix, type); ix++) |
| 1141 | tdesc_free_type (type); |
| 1142 | VEC_free (tdesc_type_p, feature->types); |
| 1143 | |
| 1144 | xfree (feature->name); |
| 1145 | xfree (feature); |
| 1146 | } |
| 1147 | |
| 1148 | struct tdesc_feature * |
| 1149 | tdesc_create_feature (struct target_desc *tdesc, const char *name) |
| 1150 | { |
| 1151 | struct tdesc_feature *new_feature = XZALLOC (struct tdesc_feature); |
| 1152 | |
| 1153 | new_feature->name = xstrdup (name); |
| 1154 | |
| 1155 | VEC_safe_push (tdesc_feature_p, tdesc->features, new_feature); |
| 1156 | return new_feature; |
| 1157 | } |
| 1158 | |
| 1159 | struct target_desc * |
| 1160 | allocate_target_description (void) |
| 1161 | { |
| 1162 | return XZALLOC (struct target_desc); |
| 1163 | } |
| 1164 | |
| 1165 | static void |
| 1166 | free_target_description (void *arg) |
| 1167 | { |
| 1168 | struct target_desc *target_desc = arg; |
| 1169 | struct tdesc_feature *feature; |
| 1170 | struct property *prop; |
| 1171 | int ix; |
| 1172 | |
| 1173 | for (ix = 0; |
| 1174 | VEC_iterate (tdesc_feature_p, target_desc->features, ix, feature); |
| 1175 | ix++) |
| 1176 | tdesc_free_feature (feature); |
| 1177 | VEC_free (tdesc_feature_p, target_desc->features); |
| 1178 | |
| 1179 | for (ix = 0; |
| 1180 | VEC_iterate (property_s, target_desc->properties, ix, prop); |
| 1181 | ix++) |
| 1182 | { |
| 1183 | xfree (prop->key); |
| 1184 | xfree (prop->value); |
| 1185 | } |
| 1186 | VEC_free (property_s, target_desc->properties); |
| 1187 | |
| 1188 | VEC_free (arch_p, target_desc->compatible); |
| 1189 | |
| 1190 | xfree (target_desc); |
| 1191 | } |
| 1192 | |
| 1193 | struct cleanup * |
| 1194 | make_cleanup_free_target_description (struct target_desc *target_desc) |
| 1195 | { |
| 1196 | return make_cleanup (free_target_description, target_desc); |
| 1197 | } |
| 1198 | |
| 1199 | void |
| 1200 | tdesc_add_compatible (struct target_desc *target_desc, |
| 1201 | const struct bfd_arch_info *compatible) |
| 1202 | { |
| 1203 | const struct bfd_arch_info *compat; |
| 1204 | int ix; |
| 1205 | |
| 1206 | /* If this instance of GDB is compiled without BFD support for the |
| 1207 | compatible architecture, simply ignore it -- we would not be able |
| 1208 | to handle it anyway. */ |
| 1209 | if (compatible == NULL) |
| 1210 | return; |
| 1211 | |
| 1212 | for (ix = 0; VEC_iterate (arch_p, target_desc->compatible, ix, compat); |
| 1213 | ix++) |
| 1214 | if (compat == compatible) |
| 1215 | internal_error (__FILE__, __LINE__, |
| 1216 | _("Attempted to add duplicate " |
| 1217 | "compatible architecture \"%s\""), |
| 1218 | compatible->printable_name); |
| 1219 | |
| 1220 | VEC_safe_push (arch_p, target_desc->compatible, compatible); |
| 1221 | } |
| 1222 | |
| 1223 | void |
| 1224 | set_tdesc_property (struct target_desc *target_desc, |
| 1225 | const char *key, const char *value) |
| 1226 | { |
| 1227 | struct property *prop, new_prop; |
| 1228 | int ix; |
| 1229 | |
| 1230 | gdb_assert (key != NULL && value != NULL); |
| 1231 | |
| 1232 | for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop); |
| 1233 | ix++) |
| 1234 | if (strcmp (prop->key, key) == 0) |
| 1235 | internal_error (__FILE__, __LINE__, |
| 1236 | _("Attempted to add duplicate property \"%s\""), key); |
| 1237 | |
| 1238 | new_prop.key = xstrdup (key); |
| 1239 | new_prop.value = xstrdup (value); |
| 1240 | VEC_safe_push (property_s, target_desc->properties, &new_prop); |
| 1241 | } |
| 1242 | |
| 1243 | void |
| 1244 | set_tdesc_architecture (struct target_desc *target_desc, |
| 1245 | const struct bfd_arch_info *arch) |
| 1246 | { |
| 1247 | target_desc->arch = arch; |
| 1248 | } |
| 1249 | |
| 1250 | void |
| 1251 | set_tdesc_osabi (struct target_desc *target_desc, enum gdb_osabi osabi) |
| 1252 | { |
| 1253 | target_desc->osabi = osabi; |
| 1254 | } |
| 1255 | \f |
| 1256 | |
| 1257 | static struct cmd_list_element *tdesc_set_cmdlist, *tdesc_show_cmdlist; |
| 1258 | static struct cmd_list_element *tdesc_unset_cmdlist; |
| 1259 | |
| 1260 | /* Helper functions for the CLI commands. */ |
| 1261 | |
| 1262 | static void |
| 1263 | set_tdesc_cmd (char *args, int from_tty) |
| 1264 | { |
| 1265 | help_list (tdesc_set_cmdlist, "set tdesc ", -1, gdb_stdout); |
| 1266 | } |
| 1267 | |
| 1268 | static void |
| 1269 | show_tdesc_cmd (char *args, int from_tty) |
| 1270 | { |
| 1271 | cmd_show_list (tdesc_show_cmdlist, from_tty, ""); |
| 1272 | } |
| 1273 | |
| 1274 | static void |
| 1275 | unset_tdesc_cmd (char *args, int from_tty) |
| 1276 | { |
| 1277 | help_list (tdesc_unset_cmdlist, "unset tdesc ", -1, gdb_stdout); |
| 1278 | } |
| 1279 | |
| 1280 | static void |
| 1281 | set_tdesc_filename_cmd (char *args, int from_tty, |
| 1282 | struct cmd_list_element *c) |
| 1283 | { |
| 1284 | target_clear_description (); |
| 1285 | target_find_description (); |
| 1286 | } |
| 1287 | |
| 1288 | static void |
| 1289 | show_tdesc_filename_cmd (struct ui_file *file, int from_tty, |
| 1290 | struct cmd_list_element *c, |
| 1291 | const char *value) |
| 1292 | { |
| 1293 | if (value != NULL && *value != '\0') |
| 1294 | printf_filtered (_("\ |
| 1295 | The target description will be read from \"%s\".\n"), |
| 1296 | value); |
| 1297 | else |
| 1298 | printf_filtered (_("\ |
| 1299 | The target description will be read from the target.\n")); |
| 1300 | } |
| 1301 | |
| 1302 | static void |
| 1303 | unset_tdesc_filename_cmd (char *args, int from_tty) |
| 1304 | { |
| 1305 | xfree (target_description_filename); |
| 1306 | target_description_filename = NULL; |
| 1307 | target_clear_description (); |
| 1308 | target_find_description (); |
| 1309 | } |
| 1310 | |
| 1311 | static void |
| 1312 | maint_print_c_tdesc_cmd (char *args, int from_tty) |
| 1313 | { |
| 1314 | const struct target_desc *tdesc; |
| 1315 | const struct bfd_arch_info *compatible; |
| 1316 | const char *filename, *inp; |
| 1317 | char *function, *outp; |
| 1318 | struct property *prop; |
| 1319 | struct tdesc_feature *feature; |
| 1320 | struct tdesc_reg *reg; |
| 1321 | struct tdesc_type *type; |
| 1322 | struct tdesc_type_field *f; |
| 1323 | int ix, ix2, ix3; |
| 1324 | |
| 1325 | /* Use the global target-supplied description, not the current |
| 1326 | architecture's. This lets a GDB for one architecture generate C |
| 1327 | for another architecture's description, even though the gdbarch |
| 1328 | initialization code will reject the new description. */ |
| 1329 | tdesc = current_target_desc; |
| 1330 | if (tdesc == NULL) |
| 1331 | error (_("There is no target description to print.")); |
| 1332 | |
| 1333 | if (target_description_filename == NULL) |
| 1334 | error (_("The current target description did not come from an XML file.")); |
| 1335 | |
| 1336 | filename = lbasename (target_description_filename); |
| 1337 | function = alloca (strlen (filename) + 1); |
| 1338 | for (inp = filename, outp = function; *inp != '\0'; inp++) |
| 1339 | if (*inp == '.') |
| 1340 | break; |
| 1341 | else if (*inp == '-') |
| 1342 | *outp++ = '_'; |
| 1343 | else |
| 1344 | *outp++ = *inp; |
| 1345 | *outp = '\0'; |
| 1346 | |
| 1347 | /* Standard boilerplate. */ |
| 1348 | printf_unfiltered ("/* THIS FILE IS GENERATED. Original: %s */\n\n", |
| 1349 | filename); |
| 1350 | printf_unfiltered ("#include \"defs.h\"\n"); |
| 1351 | printf_unfiltered ("#include \"target-descriptions.h\"\n"); |
| 1352 | printf_unfiltered ("\n"); |
| 1353 | |
| 1354 | printf_unfiltered ("struct target_desc *tdesc_%s;\n", function); |
| 1355 | printf_unfiltered ("static void\n"); |
| 1356 | printf_unfiltered ("initialize_tdesc_%s (void)\n", function); |
| 1357 | printf_unfiltered ("{\n"); |
| 1358 | printf_unfiltered |
| 1359 | (" struct target_desc *result = allocate_target_description ();\n"); |
| 1360 | printf_unfiltered (" struct tdesc_feature *feature;\n"); |
| 1361 | printf_unfiltered (" struct tdesc_type *field_type, *type;\n"); |
| 1362 | printf_unfiltered ("\n"); |
| 1363 | |
| 1364 | if (tdesc_architecture (tdesc) != NULL) |
| 1365 | { |
| 1366 | printf_unfiltered |
| 1367 | (" set_tdesc_architecture (result, bfd_scan_arch (\"%s\"));\n", |
| 1368 | tdesc_architecture (tdesc)->printable_name); |
| 1369 | printf_unfiltered ("\n"); |
| 1370 | } |
| 1371 | |
| 1372 | for (ix = 0; VEC_iterate (arch_p, tdesc->compatible, ix, compatible); |
| 1373 | ix++) |
| 1374 | { |
| 1375 | printf_unfiltered |
| 1376 | (" tdesc_add_compatible (result, bfd_scan_arch (\"%s\"));\n", |
| 1377 | compatible->printable_name); |
| 1378 | } |
| 1379 | if (ix) |
| 1380 | printf_unfiltered ("\n"); |
| 1381 | |
| 1382 | for (ix = 0; VEC_iterate (property_s, tdesc->properties, ix, prop); |
| 1383 | ix++) |
| 1384 | { |
| 1385 | printf_unfiltered (" set_tdesc_property (result, \"%s\", \"%s\");\n", |
| 1386 | prop->key, prop->value); |
| 1387 | } |
| 1388 | |
| 1389 | for (ix = 0; |
| 1390 | VEC_iterate (tdesc_feature_p, tdesc->features, ix, feature); |
| 1391 | ix++) |
| 1392 | { |
| 1393 | printf_unfiltered (" feature = tdesc_create_feature (result, \"%s\");\n", |
| 1394 | feature->name); |
| 1395 | |
| 1396 | for (ix2 = 0; |
| 1397 | VEC_iterate (tdesc_type_p, feature->types, ix2, type); |
| 1398 | ix2++) |
| 1399 | { |
| 1400 | switch (type->kind) |
| 1401 | { |
| 1402 | case TDESC_TYPE_VECTOR: |
| 1403 | printf_unfiltered |
| 1404 | (" field_type = tdesc_named_type (feature, \"%s\");\n", |
| 1405 | type->u.v.type->name); |
| 1406 | printf_unfiltered |
| 1407 | (" tdesc_create_vector (feature, \"%s\", field_type, %d);\n", |
| 1408 | type->name, type->u.v.count); |
| 1409 | break; |
| 1410 | case TDESC_TYPE_UNION: |
| 1411 | printf_unfiltered |
| 1412 | (" type = tdesc_create_union (feature, \"%s\");\n", |
| 1413 | type->name); |
| 1414 | for (ix3 = 0; |
| 1415 | VEC_iterate (tdesc_type_field, type->u.u.fields, ix3, f); |
| 1416 | ix3++) |
| 1417 | { |
| 1418 | printf_unfiltered |
| 1419 | (" field_type = tdesc_named_type (feature, \"%s\");\n", |
| 1420 | f->type->name); |
| 1421 | printf_unfiltered |
| 1422 | (" tdesc_add_field (type, \"%s\", field_type);\n", |
| 1423 | f->name); |
| 1424 | } |
| 1425 | break; |
| 1426 | default: |
| 1427 | error (_("C output is not supported type \"%s\"."), type->name); |
| 1428 | } |
| 1429 | printf_unfiltered ("\n"); |
| 1430 | } |
| 1431 | |
| 1432 | for (ix2 = 0; |
| 1433 | VEC_iterate (tdesc_reg_p, feature->registers, ix2, reg); |
| 1434 | ix2++) |
| 1435 | { |
| 1436 | printf_unfiltered (" tdesc_create_reg (feature, \"%s\", %ld, %d, ", |
| 1437 | reg->name, reg->target_regnum, reg->save_restore); |
| 1438 | if (reg->group) |
| 1439 | printf_unfiltered ("\"%s\", ", reg->group); |
| 1440 | else |
| 1441 | printf_unfiltered ("NULL, "); |
| 1442 | printf_unfiltered ("%d, \"%s\");\n", reg->bitsize, reg->type); |
| 1443 | } |
| 1444 | |
| 1445 | printf_unfiltered ("\n"); |
| 1446 | } |
| 1447 | |
| 1448 | printf_unfiltered (" tdesc_%s = result;\n", function); |
| 1449 | printf_unfiltered ("}\n"); |
| 1450 | } |
| 1451 | |
| 1452 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
| 1453 | extern initialize_file_ftype _initialize_target_descriptions; |
| 1454 | |
| 1455 | void |
| 1456 | _initialize_target_descriptions (void) |
| 1457 | { |
| 1458 | tdesc_data = gdbarch_data_register_pre_init (tdesc_data_init); |
| 1459 | |
| 1460 | add_prefix_cmd ("tdesc", class_maintenance, set_tdesc_cmd, _("\ |
| 1461 | Set target description specific variables."), |
| 1462 | &tdesc_set_cmdlist, "set tdesc ", |
| 1463 | 0 /* allow-unknown */, &setlist); |
| 1464 | add_prefix_cmd ("tdesc", class_maintenance, show_tdesc_cmd, _("\ |
| 1465 | Show target description specific variables."), |
| 1466 | &tdesc_show_cmdlist, "show tdesc ", |
| 1467 | 0 /* allow-unknown */, &showlist); |
| 1468 | add_prefix_cmd ("tdesc", class_maintenance, unset_tdesc_cmd, _("\ |
| 1469 | Unset target description specific variables."), |
| 1470 | &tdesc_unset_cmdlist, "unset tdesc ", |
| 1471 | 0 /* allow-unknown */, &unsetlist); |
| 1472 | |
| 1473 | add_setshow_filename_cmd ("filename", class_obscure, |
| 1474 | &target_description_filename, |
| 1475 | _("\ |
| 1476 | Set the file to read for an XML target description"), _("\ |
| 1477 | Show the file to read for an XML target description"), _("\ |
| 1478 | When set, GDB will read the target description from a local\n\ |
| 1479 | file instead of querying the remote target."), |
| 1480 | set_tdesc_filename_cmd, |
| 1481 | show_tdesc_filename_cmd, |
| 1482 | &tdesc_set_cmdlist, &tdesc_show_cmdlist); |
| 1483 | |
| 1484 | add_cmd ("filename", class_obscure, unset_tdesc_filename_cmd, _("\ |
| 1485 | Unset the file to read for an XML target description. When unset,\n\ |
| 1486 | GDB will read the description from the target."), |
| 1487 | &tdesc_unset_cmdlist); |
| 1488 | |
| 1489 | add_cmd ("c-tdesc", class_maintenance, maint_print_c_tdesc_cmd, _("\ |
| 1490 | Print the current target description as a C source file."), |
| 1491 | &maintenanceprintlist); |
| 1492 | } |