1 /* Target description support for GDB. 2 3 Copyright (C) 2018-2024 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "gdbsupport/tdesc.h" 21 22 tdesc_reg::tdesc_reg (struct tdesc_feature *feature, const std::string &name_, 23 int regnum, int save_restore_, const char *group_, 24 int bitsize_, const char *type_) 25 : name (name_), target_regnum (regnum), 26 save_restore (save_restore_), 27 group (group_ != NULL ? group_ : ""), 28 bitsize (bitsize_), 29 type (type_ != NULL ? type_ : "<unknown>") 30 { 31 /* If the register's type is target-defined, look it up now. We may not 32 have easy access to the containing feature when we want it later. */ 33 tdesc_type = tdesc_named_type (feature, type.c_str ()); 34 } 35 36 /* Predefined types. */ 37 static tdesc_type_builtin tdesc_predefined_types[] = 38 { 39 { "bool", TDESC_TYPE_BOOL }, 40 { "int8", TDESC_TYPE_INT8 }, 41 { "int16", TDESC_TYPE_INT16 }, 42 { "int32", TDESC_TYPE_INT32 }, 43 { "int64", TDESC_TYPE_INT64 }, 44 { "int128", TDESC_TYPE_INT128 }, 45 { "uint8", TDESC_TYPE_UINT8 }, 46 { "uint16", TDESC_TYPE_UINT16 }, 47 { "uint32", TDESC_TYPE_UINT32 }, 48 { "uint64", TDESC_TYPE_UINT64 }, 49 { "uint128", TDESC_TYPE_UINT128 }, 50 { "code_ptr", TDESC_TYPE_CODE_PTR }, 51 { "data_ptr", TDESC_TYPE_DATA_PTR }, 52 { "ieee_half", TDESC_TYPE_IEEE_HALF }, 53 { "ieee_single", TDESC_TYPE_IEEE_SINGLE }, 54 { "ieee_double", TDESC_TYPE_IEEE_DOUBLE }, 55 { "arm_fpa_ext", TDESC_TYPE_ARM_FPA_EXT }, 56 { "i387_ext", TDESC_TYPE_I387_EXT }, 57 { "bfloat16", TDESC_TYPE_BFLOAT16 } 58 }; 59 60 void tdesc_feature::accept (tdesc_element_visitor &v) const 61 { 62 v.visit_pre (this); 63 64 for (const tdesc_type_up &type : types) 65 type->accept (v); 66 67 for (const tdesc_reg_up ® : registers) 68 reg->accept (v); 69 70 v.visit_post (this); 71 } 72 73 bool tdesc_feature::operator== (const tdesc_feature &other) const 74 { 75 if (name != other.name) 76 return false; 77 78 if (registers.size () != other.registers.size ()) 79 return false; 80 81 for (int ix = 0; ix < registers.size (); ix++) 82 { 83 const tdesc_reg_up ®1 = registers[ix]; 84 const tdesc_reg_up ®2 = other.registers[ix]; 85 86 if (reg1 != reg2 && *reg1 != *reg2) 87 return false; 88 } 89 90 if (types.size () != other.types.size ()) 91 return false; 92 93 for (int ix = 0; ix < types.size (); ix++) 94 { 95 const tdesc_type_up &type1 = types[ix]; 96 const tdesc_type_up &type2 = other.types[ix]; 97 98 if (type1 != type2 && *type1 != *type2) 99 return false; 100 } 101 102 return true; 103 } 104 105 /* Lookup a predefined type. */ 106 107 static struct tdesc_type * 108 tdesc_predefined_type (enum tdesc_type_kind kind) 109 { 110 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++) 111 if (tdesc_predefined_types[ix].kind == kind) 112 return &tdesc_predefined_types[ix]; 113 114 gdb_assert_not_reached ("bad predefined tdesc type"); 115 } 116 117 /* See gdbsupport/tdesc.h. */ 118 119 struct tdesc_type * 120 tdesc_named_type (const struct tdesc_feature *feature, const char *id) 121 { 122 /* First try target-defined types. */ 123 for (const tdesc_type_up &type : feature->types) 124 if (type->name == id) 125 return type.get (); 126 127 /* Next try the predefined types. */ 128 for (int ix = 0; ix < ARRAY_SIZE (tdesc_predefined_types); ix++) 129 if (tdesc_predefined_types[ix].name == id) 130 return &tdesc_predefined_types[ix]; 131 132 return NULL; 133 } 134 135 /* See gdbsupport/tdesc.h. */ 136 137 void 138 tdesc_create_reg (struct tdesc_feature *feature, const char *name, 139 int regnum, int save_restore, const char *group, 140 int bitsize, const char *type) 141 { 142 tdesc_reg *reg = new tdesc_reg (feature, name, regnum, save_restore, 143 group, bitsize, type); 144 145 feature->registers.emplace_back (reg); 146 } 147 148 /* See gdbsupport/tdesc.h. */ 149 150 struct tdesc_type * 151 tdesc_create_vector (struct tdesc_feature *feature, const char *name, 152 struct tdesc_type *field_type, int count) 153 { 154 tdesc_type_vector *type = new tdesc_type_vector (name, field_type, count); 155 feature->types.emplace_back (type); 156 157 return type; 158 } 159 160 /* See gdbsupport/tdesc.h. */ 161 162 tdesc_type_with_fields * 163 tdesc_create_struct (struct tdesc_feature *feature, const char *name) 164 { 165 tdesc_type_with_fields *type 166 = new tdesc_type_with_fields (name, TDESC_TYPE_STRUCT); 167 feature->types.emplace_back (type); 168 169 return type; 170 } 171 172 /* See gdbsupport/tdesc.h. */ 173 174 void 175 tdesc_set_struct_size (tdesc_type_with_fields *type, int size) 176 { 177 gdb_assert (type->kind == TDESC_TYPE_STRUCT); 178 gdb_assert (size > 0); 179 type->size = size; 180 } 181 182 /* See gdbsupport/tdesc.h. */ 183 184 tdesc_type_with_fields * 185 tdesc_create_union (struct tdesc_feature *feature, const char *name) 186 { 187 tdesc_type_with_fields *type 188 = new tdesc_type_with_fields (name, TDESC_TYPE_UNION); 189 feature->types.emplace_back (type); 190 191 return type; 192 } 193 194 /* See gdbsupport/tdesc.h. */ 195 196 tdesc_type_with_fields * 197 tdesc_create_flags (struct tdesc_feature *feature, const char *name, 198 int size) 199 { 200 gdb_assert (size > 0); 201 202 tdesc_type_with_fields *type 203 = new tdesc_type_with_fields (name, TDESC_TYPE_FLAGS, size); 204 feature->types.emplace_back (type); 205 206 return type; 207 } 208 209 /* See gdbsupport/tdesc.h. */ 210 211 tdesc_type_with_fields * 212 tdesc_create_enum (struct tdesc_feature *feature, const char *name, 213 int size) 214 { 215 gdb_assert (size > 0); 216 217 tdesc_type_with_fields *type 218 = new tdesc_type_with_fields (name, TDESC_TYPE_ENUM, size); 219 feature->types.emplace_back (type); 220 221 return type; 222 } 223 224 /* See gdbsupport/tdesc.h. */ 225 226 void 227 tdesc_add_field (tdesc_type_with_fields *type, const char *field_name, 228 struct tdesc_type *field_type) 229 { 230 gdb_assert (type->kind == TDESC_TYPE_UNION 231 || type->kind == TDESC_TYPE_STRUCT); 232 233 /* Initialize start and end so we know this is not a bit-field 234 when we print-c-tdesc. */ 235 type->fields.emplace_back (field_name, field_type, -1, -1); 236 } 237 238 /* See gdbsupport/tdesc.h. */ 239 240 void 241 tdesc_add_typed_bitfield (tdesc_type_with_fields *type, const char *field_name, 242 int start, int end, struct tdesc_type *field_type) 243 { 244 gdb_assert (type->kind == TDESC_TYPE_STRUCT 245 || type->kind == TDESC_TYPE_FLAGS); 246 gdb_assert (start >= 0 && end >= start); 247 248 type->fields.emplace_back (field_name, field_type, start, end); 249 } 250 251 /* See gdbsupport/tdesc.h. */ 252 253 void 254 tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name, 255 int start, int end) 256 { 257 struct tdesc_type *field_type; 258 259 gdb_assert (start >= 0 && end >= start); 260 261 if (type->size > 4) 262 field_type = tdesc_predefined_type (TDESC_TYPE_UINT64); 263 else 264 field_type = tdesc_predefined_type (TDESC_TYPE_UINT32); 265 266 tdesc_add_typed_bitfield (type, field_name, start, end, field_type); 267 } 268 269 /* See gdbsupport/tdesc.h. */ 270 271 void 272 tdesc_add_flag (tdesc_type_with_fields *type, int start, 273 const char *flag_name) 274 { 275 gdb_assert (type->kind == TDESC_TYPE_FLAGS 276 || type->kind == TDESC_TYPE_STRUCT); 277 278 type->fields.emplace_back (flag_name, 279 tdesc_predefined_type (TDESC_TYPE_BOOL), 280 start, start); 281 } 282 283 /* See gdbsupport/tdesc.h. */ 284 285 void 286 tdesc_add_enum_value (tdesc_type_with_fields *type, int value, 287 const char *name) 288 { 289 gdb_assert (type->kind == TDESC_TYPE_ENUM); 290 type->fields.emplace_back (name, 291 tdesc_predefined_type (TDESC_TYPE_INT32), 292 value, -1); 293 } 294 295 void print_xml_feature::visit_pre (const tdesc_feature *e) 296 { 297 add_line ("<feature name=\"%s\">", e->name.c_str ()); 298 indent (1); 299 } 300 301 void print_xml_feature::visit_post (const tdesc_feature *e) 302 { 303 indent (-1); 304 add_line ("</feature>"); 305 } 306 307 void print_xml_feature::visit (const tdesc_type_builtin *t) 308 { 309 error (_("xml output is not supported for type \"%s\"."), t->name.c_str ()); 310 } 311 312 void print_xml_feature::visit (const tdesc_type_vector *t) 313 { 314 add_line ("<vector id=\"%s\" type=\"%s\" count=\"%d\"/>", 315 t->name.c_str (), t->element_type->name.c_str (), t->count); 316 } 317 318 void print_xml_feature::visit (const tdesc_type_with_fields *t) 319 { 320 const static char *types[] = { "struct", "union", "flags", "enum" }; 321 322 gdb_assert (t->kind >= TDESC_TYPE_STRUCT && t->kind <= TDESC_TYPE_ENUM); 323 324 std::string tmp; 325 326 string_appendf (tmp, 327 "<%s id=\"%s\"", types[t->kind - TDESC_TYPE_STRUCT], 328 t->name.c_str ()); 329 330 switch (t->kind) 331 { 332 case TDESC_TYPE_STRUCT: 333 case TDESC_TYPE_FLAGS: 334 if (t->size > 0) 335 string_appendf (tmp, " size=\"%d\"", t->size); 336 string_appendf (tmp, ">"); 337 add_line (tmp); 338 339 for (const tdesc_type_field &f : t->fields) 340 { 341 tmp.clear (); 342 string_appendf (tmp, " <field name=\"%s\"", f.name.c_str ()); 343 if (f.start != -1) 344 string_appendf (tmp, " start=\"%d\" end=\"%d\"", f.start, 345 f.end); 346 string_appendf (tmp, " type=\"%s\"/>", 347 f.type->name.c_str ()); 348 add_line (tmp); 349 } 350 break; 351 352 case TDESC_TYPE_ENUM: 353 if (t->size > 0) 354 string_appendf (tmp, " size=\"%d\"", t->size); 355 string_appendf (tmp, ">"); 356 add_line (tmp); 357 /* The 'start' of the field is reused as the enum value. The 'end' 358 of the field is always set to -1 for enum values. */ 359 for (const tdesc_type_field &f : t->fields) 360 add_line (" <evalue name=\"%s\" value=\"%d\"/>", 361 f.name.c_str (), f.start); 362 break; 363 364 case TDESC_TYPE_UNION: 365 string_appendf (tmp, ">"); 366 add_line (tmp); 367 for (const tdesc_type_field &f : t->fields) 368 add_line (" <field name=\"%s\" type=\"%s\"/>", 369 f.name.c_str (), f.type->name.c_str ()); 370 break; 371 372 default: 373 error (_("xml output is not supported for type \"%s\"."), 374 t->name.c_str ()); 375 } 376 377 add_line ("</%s>", types[t->kind - TDESC_TYPE_STRUCT]); 378 } 379 380 void print_xml_feature::visit (const tdesc_reg *r) 381 { 382 std::string tmp; 383 384 string_appendf (tmp, 385 "<reg name=\"%s\" bitsize=\"%d\" type=\"%s\" regnum=\"%ld\"", 386 r->name.c_str (), r->bitsize, r->type.c_str (), 387 r->target_regnum); 388 389 if (r->group.length () > 0) 390 string_appendf (tmp, " group=\"%s\"", r->group.c_str ()); 391 392 if (r->save_restore == 0) 393 string_appendf (tmp, " save-restore=\"no\""); 394 395 string_appendf (tmp, "/>"); 396 397 add_line (tmp); 398 } 399 400 void print_xml_feature::visit_pre (const target_desc *e) 401 { 402 #ifndef IN_PROCESS_AGENT 403 add_line ("<?xml version=\"1.0\"?>"); 404 add_line ("<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"); 405 add_line ("<target>"); 406 indent (1); 407 if (tdesc_architecture_name (e)) 408 add_line ("<architecture>%s</architecture>", 409 tdesc_architecture_name (e)); 410 411 const char *osabi = tdesc_osabi_name (e); 412 if (osabi != nullptr) 413 add_line ("<osabi>%s</osabi>", osabi); 414 415 const std::vector<tdesc_compatible_info_up> &compatible_list 416 = tdesc_compatible_info_list (e); 417 for (const auto &c : compatible_list) 418 add_line ("<compatible>%s</compatible>", 419 tdesc_compatible_info_arch_name (c)); 420 #endif 421 } 422 423 void print_xml_feature::visit_post (const target_desc *e) 424 { 425 indent (-1); 426 add_line ("</target>"); 427 } 428 429 /* See gdbsupport/tdesc.h. */ 430 431 void 432 print_xml_feature::add_line (const std::string &str) 433 { 434 string_appendf (*m_buffer, "%*s", m_depth, ""); 435 string_appendf (*m_buffer, "%s", str.c_str ()); 436 string_appendf (*m_buffer, "\n"); 437 } 438 439 /* See gdbsupport/tdesc.h. */ 440 441 void 442 print_xml_feature::add_line (const char *fmt, ...) 443 { 444 std::string tmp; 445 446 va_list ap; 447 va_start (ap, fmt); 448 string_vappendf (tmp, fmt, ap); 449 va_end (ap); 450 add_line (tmp); 451 } 452