1*5ba1f45fSchristos /* Copyright (C) 2006-2024 Free Software Foundation, Inc. 28dffb485Schristos 38dffb485Schristos This file is part of GDB. 48dffb485Schristos 58dffb485Schristos This program is free software; you can redistribute it and/or modify 68dffb485Schristos it under the terms of the GNU General Public License as published by 78dffb485Schristos the Free Software Foundation; either version 3 of the License, or 88dffb485Schristos (at your option) any later version. 98dffb485Schristos 108dffb485Schristos This program is distributed in the hope that it will be useful, 118dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 128dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138dffb485Schristos GNU General Public License for more details. 148dffb485Schristos 158dffb485Schristos You should have received a copy of the GNU General Public License 168dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 178dffb485Schristos 188dffb485Schristos #ifndef COMMON_TDESC_H 198dffb485Schristos #define COMMON_TDESC_H 208dffb485Schristos 218dffb485Schristos struct tdesc_feature; 228dffb485Schristos struct tdesc_type; 238dffb485Schristos struct tdesc_type_builtin; 248dffb485Schristos struct tdesc_type_vector; 258dffb485Schristos struct tdesc_type_with_fields; 268dffb485Schristos struct tdesc_reg; 278dffb485Schristos struct target_desc; 288dffb485Schristos 298dffb485Schristos /* The interface to visit different elements of target description. */ 308dffb485Schristos 318dffb485Schristos class tdesc_element_visitor 328dffb485Schristos { 338dffb485Schristos public: 348dffb485Schristos virtual void visit_pre (const target_desc *e) 358dffb485Schristos {} 368dffb485Schristos 378dffb485Schristos virtual void visit_post (const target_desc *e) 388dffb485Schristos {} 398dffb485Schristos 408dffb485Schristos virtual void visit_pre (const tdesc_feature *e) 418dffb485Schristos {} 428dffb485Schristos 438dffb485Schristos virtual void visit_post (const tdesc_feature *e) 448dffb485Schristos {} 458dffb485Schristos 468dffb485Schristos virtual void visit (const tdesc_type_builtin *e) 478dffb485Schristos {} 488dffb485Schristos 498dffb485Schristos virtual void visit (const tdesc_type_vector *e) 508dffb485Schristos {} 518dffb485Schristos 528dffb485Schristos virtual void visit (const tdesc_type_with_fields *e) 538dffb485Schristos {} 548dffb485Schristos 558dffb485Schristos virtual void visit (const tdesc_reg *e) 568dffb485Schristos {} 578dffb485Schristos }; 588dffb485Schristos 598dffb485Schristos class tdesc_element 608dffb485Schristos { 618dffb485Schristos public: 628dffb485Schristos virtual void accept (tdesc_element_visitor &v) const = 0; 638dffb485Schristos }; 648dffb485Schristos 658dffb485Schristos /* An individual register from a target description. */ 668dffb485Schristos 678dffb485Schristos struct tdesc_reg : tdesc_element 688dffb485Schristos { 698dffb485Schristos tdesc_reg (struct tdesc_feature *feature, const std::string &name_, 708dffb485Schristos int regnum, int save_restore_, const char *group_, 718dffb485Schristos int bitsize_, const char *type_); 728dffb485Schristos 738dffb485Schristos virtual ~tdesc_reg () = default; 748dffb485Schristos 758dffb485Schristos DISABLE_COPY_AND_ASSIGN (tdesc_reg); 768dffb485Schristos 778dffb485Schristos /* The name of this register. In standard features, it may be 788dffb485Schristos recognized by the architecture support code, or it may be purely 798dffb485Schristos for the user. */ 808dffb485Schristos std::string name; 818dffb485Schristos 828dffb485Schristos /* The register number used by this target to refer to this 838dffb485Schristos register. This is used for remote p/P packets and to determine 848dffb485Schristos the ordering of registers in the remote g/G packets. */ 858dffb485Schristos long target_regnum; 868dffb485Schristos 878dffb485Schristos /* If this flag is set, GDB should save and restore this register 888dffb485Schristos around calls to an inferior function. */ 898dffb485Schristos int save_restore; 908dffb485Schristos 918dffb485Schristos /* The name of the register group containing this register, or empty 928dffb485Schristos if the group should be automatically determined from the 938dffb485Schristos register's type. If this is "general", "float", or "vector", the 948dffb485Schristos corresponding "info" command should display this register's 958dffb485Schristos value. It can be an arbitrary string, but should be limited to 968dffb485Schristos alphanumeric characters and internal hyphens. Currently other 978dffb485Schristos strings are ignored (treated as empty). */ 988dffb485Schristos std::string group; 998dffb485Schristos 1008dffb485Schristos /* The size of the register, in bits. */ 1018dffb485Schristos int bitsize; 1028dffb485Schristos 1038dffb485Schristos /* The type of the register. This string corresponds to either 1048dffb485Schristos a named type from the target description or a predefined 1058dffb485Schristos type from GDB. */ 1068dffb485Schristos std::string type; 1078dffb485Schristos 1088dffb485Schristos /* The target-described type corresponding to TYPE, if found. */ 1098dffb485Schristos struct tdesc_type *tdesc_type; 1108dffb485Schristos 1118dffb485Schristos void accept (tdesc_element_visitor &v) const override 1128dffb485Schristos { 1138dffb485Schristos v.visit (this); 1148dffb485Schristos } 1158dffb485Schristos 1168dffb485Schristos bool operator== (const tdesc_reg &other) const 1178dffb485Schristos { 1188dffb485Schristos return (name == other.name 1198dffb485Schristos && target_regnum == other.target_regnum 1208dffb485Schristos && save_restore == other.save_restore 1218dffb485Schristos && bitsize == other.bitsize 1228dffb485Schristos && group == other.group 1238dffb485Schristos && type == other.type); 1248dffb485Schristos } 1258dffb485Schristos 1268dffb485Schristos bool operator!= (const tdesc_reg &other) const 1278dffb485Schristos { 1288dffb485Schristos return !(*this == other); 1298dffb485Schristos } 1308dffb485Schristos }; 1318dffb485Schristos 1328dffb485Schristos typedef std::unique_ptr<tdesc_reg> tdesc_reg_up; 1338dffb485Schristos 1348dffb485Schristos /* Declaration of a structure that holds information about one 1358dffb485Schristos "compatibility" entry within a target description. */ 1368dffb485Schristos 1378dffb485Schristos struct tdesc_compatible_info; 1388dffb485Schristos 1398dffb485Schristos /* A pointer to a single piece of compatibility information. */ 1408dffb485Schristos 1418dffb485Schristos typedef std::unique_ptr<tdesc_compatible_info> tdesc_compatible_info_up; 1428dffb485Schristos 1438dffb485Schristos /* Return a vector of compatibility information pointers from the target 1448dffb485Schristos description TARGET_DESC. */ 1458dffb485Schristos 1468dffb485Schristos const std::vector<tdesc_compatible_info_up> &tdesc_compatible_info_list 1478dffb485Schristos (const target_desc *target_desc); 1488dffb485Schristos 1498dffb485Schristos /* Return the architecture name from a compatibility information 1508dffb485Schristos COMPATIBLE. */ 1518dffb485Schristos 1528dffb485Schristos const char *tdesc_compatible_info_arch_name 1538dffb485Schristos (const tdesc_compatible_info_up &compatible); 1548dffb485Schristos 1558dffb485Schristos enum tdesc_type_kind 1568dffb485Schristos { 1578dffb485Schristos /* Predefined types. */ 1588dffb485Schristos TDESC_TYPE_BOOL, 1598dffb485Schristos TDESC_TYPE_INT8, 1608dffb485Schristos TDESC_TYPE_INT16, 1618dffb485Schristos TDESC_TYPE_INT32, 1628dffb485Schristos TDESC_TYPE_INT64, 1638dffb485Schristos TDESC_TYPE_INT128, 1648dffb485Schristos TDESC_TYPE_UINT8, 1658dffb485Schristos TDESC_TYPE_UINT16, 1668dffb485Schristos TDESC_TYPE_UINT32, 1678dffb485Schristos TDESC_TYPE_UINT64, 1688dffb485Schristos TDESC_TYPE_UINT128, 1698dffb485Schristos TDESC_TYPE_CODE_PTR, 1708dffb485Schristos TDESC_TYPE_DATA_PTR, 1718dffb485Schristos TDESC_TYPE_IEEE_HALF, 1728dffb485Schristos TDESC_TYPE_IEEE_SINGLE, 1738dffb485Schristos TDESC_TYPE_IEEE_DOUBLE, 1748dffb485Schristos TDESC_TYPE_ARM_FPA_EXT, 1758dffb485Schristos TDESC_TYPE_I387_EXT, 1768dffb485Schristos TDESC_TYPE_BFLOAT16, 1778dffb485Schristos 1788dffb485Schristos /* Types defined by a target feature. */ 1798dffb485Schristos TDESC_TYPE_VECTOR, 1808dffb485Schristos TDESC_TYPE_STRUCT, 1818dffb485Schristos TDESC_TYPE_UNION, 1828dffb485Schristos TDESC_TYPE_FLAGS, 1838dffb485Schristos TDESC_TYPE_ENUM 1848dffb485Schristos }; 1858dffb485Schristos 1868dffb485Schristos struct tdesc_type : tdesc_element 1878dffb485Schristos { 1888dffb485Schristos tdesc_type (const std::string &name_, enum tdesc_type_kind kind_) 1898dffb485Schristos : name (name_), kind (kind_) 1908dffb485Schristos {} 1918dffb485Schristos 1928dffb485Schristos virtual ~tdesc_type () = default; 1938dffb485Schristos 1948dffb485Schristos DISABLE_COPY_AND_ASSIGN (tdesc_type); 1958dffb485Schristos 1968dffb485Schristos /* The name of this type. */ 1978dffb485Schristos std::string name; 1988dffb485Schristos 1998dffb485Schristos /* Identify the kind of this type. */ 2008dffb485Schristos enum tdesc_type_kind kind; 2018dffb485Schristos 2028dffb485Schristos bool operator== (const tdesc_type &other) const 2038dffb485Schristos { 2048dffb485Schristos return name == other.name && kind == other.kind; 2058dffb485Schristos } 2068dffb485Schristos 2078dffb485Schristos bool operator!= (const tdesc_type &other) const 2088dffb485Schristos { 2098dffb485Schristos return !(*this == other); 2108dffb485Schristos } 2118dffb485Schristos }; 2128dffb485Schristos 2138dffb485Schristos typedef std::unique_ptr<tdesc_type> tdesc_type_up; 2148dffb485Schristos 2158dffb485Schristos struct tdesc_type_builtin : tdesc_type 2168dffb485Schristos { 2178dffb485Schristos tdesc_type_builtin (const std::string &name, enum tdesc_type_kind kind) 2188dffb485Schristos : tdesc_type (name, kind) 2198dffb485Schristos {} 2208dffb485Schristos 2218dffb485Schristos void accept (tdesc_element_visitor &v) const override 2228dffb485Schristos { 2238dffb485Schristos v.visit (this); 2248dffb485Schristos } 2258dffb485Schristos }; 2268dffb485Schristos 2278dffb485Schristos /* tdesc_type for vector types. */ 2288dffb485Schristos 2298dffb485Schristos struct tdesc_type_vector : tdesc_type 2308dffb485Schristos { 2318dffb485Schristos tdesc_type_vector (const std::string &name, tdesc_type *element_type_, 2328dffb485Schristos int count_) 2338dffb485Schristos : tdesc_type (name, TDESC_TYPE_VECTOR), 2348dffb485Schristos element_type (element_type_), count (count_) 2358dffb485Schristos {} 2368dffb485Schristos 2378dffb485Schristos void accept (tdesc_element_visitor &v) const override 2388dffb485Schristos { 2398dffb485Schristos v.visit (this); 2408dffb485Schristos } 2418dffb485Schristos 2428dffb485Schristos struct tdesc_type *element_type; 2438dffb485Schristos int count; 2448dffb485Schristos }; 2458dffb485Schristos 2468dffb485Schristos /* A named type from a target description. */ 2478dffb485Schristos 2488dffb485Schristos struct tdesc_type_field 2498dffb485Schristos { 2508dffb485Schristos tdesc_type_field (const std::string &name_, tdesc_type *type_, 2518dffb485Schristos int start_, int end_) 2528dffb485Schristos : name (name_), type (type_), start (start_), end (end_) 2538dffb485Schristos {} 2548dffb485Schristos 2558dffb485Schristos std::string name; 2568dffb485Schristos struct tdesc_type *type; 2578dffb485Schristos /* For non-enum-values, either both are -1 (non-bitfield), or both are 2588dffb485Schristos not -1 (bitfield). For enum values, start is the value (which could be 2598dffb485Schristos -1), end is -1. */ 2608dffb485Schristos int start, end; 2618dffb485Schristos }; 2628dffb485Schristos 2638dffb485Schristos /* tdesc_type for struct, union, flags, and enum types. */ 2648dffb485Schristos 2658dffb485Schristos struct tdesc_type_with_fields : tdesc_type 2668dffb485Schristos { 2678dffb485Schristos tdesc_type_with_fields (const std::string &name, tdesc_type_kind kind, 2688dffb485Schristos int size_ = 0) 2698dffb485Schristos : tdesc_type (name, kind), size (size_) 2708dffb485Schristos {} 2718dffb485Schristos 2728dffb485Schristos void accept (tdesc_element_visitor &v) const override 2738dffb485Schristos { 2748dffb485Schristos v.visit (this); 2758dffb485Schristos } 2768dffb485Schristos 2778dffb485Schristos std::vector<tdesc_type_field> fields; 2788dffb485Schristos int size; 2798dffb485Schristos }; 2808dffb485Schristos 2818dffb485Schristos /* A feature from a target description. Each feature is a collection 2828dffb485Schristos of other elements, e.g. registers and types. */ 2838dffb485Schristos 2848dffb485Schristos struct tdesc_feature : tdesc_element 2858dffb485Schristos { 2868dffb485Schristos tdesc_feature (const std::string &name_) 2878dffb485Schristos : name (name_) 2888dffb485Schristos {} 2898dffb485Schristos 2908dffb485Schristos virtual ~tdesc_feature () = default; 2918dffb485Schristos 2928dffb485Schristos DISABLE_COPY_AND_ASSIGN (tdesc_feature); 2938dffb485Schristos 2948dffb485Schristos /* The name of this feature. It may be recognized by the architecture 2958dffb485Schristos support code. */ 2968dffb485Schristos std::string name; 2978dffb485Schristos 2988dffb485Schristos /* The registers associated with this feature. */ 2998dffb485Schristos std::vector<tdesc_reg_up> registers; 3008dffb485Schristos 3018dffb485Schristos /* The types associated with this feature. */ 3028dffb485Schristos std::vector<tdesc_type_up> types; 3038dffb485Schristos 3048dffb485Schristos void accept (tdesc_element_visitor &v) const override; 3058dffb485Schristos 3068dffb485Schristos bool operator== (const tdesc_feature &other) const; 3078dffb485Schristos 3088dffb485Schristos bool operator!= (const tdesc_feature &other) const 3098dffb485Schristos { 3108dffb485Schristos return !(*this == other); 3118dffb485Schristos } 3128dffb485Schristos }; 3138dffb485Schristos 3148dffb485Schristos typedef std::unique_ptr<tdesc_feature> tdesc_feature_up; 3158dffb485Schristos 3168dffb485Schristos /* A deleter adapter for a target_desc. There are different 3178dffb485Schristos implementations of this deleter class in gdb and gdbserver because even 3188dffb485Schristos though the target_desc name is shared between the two projects, the 3198dffb485Schristos actual implementations of target_desc are completely different. */ 3208dffb485Schristos 3218dffb485Schristos struct target_desc_deleter 3228dffb485Schristos { 3238dffb485Schristos void operator() (struct target_desc *desc) const; 3248dffb485Schristos }; 3258dffb485Schristos 3268dffb485Schristos /* A unique pointer specialization that holds a target_desc. */ 3278dffb485Schristos 3288dffb485Schristos typedef std::unique_ptr<target_desc, target_desc_deleter> target_desc_up; 3298dffb485Schristos 3308dffb485Schristos /* Allocate a new target_desc. */ 3314b169a6bSchristos target_desc_up allocate_target_description (void); 3328dffb485Schristos 3338dffb485Schristos /* Set TARGET_DESC's architecture by NAME. */ 3348dffb485Schristos void set_tdesc_architecture (target_desc *target_desc, 3358dffb485Schristos const char *name); 3368dffb485Schristos 3378dffb485Schristos /* Return the architecture associated with this target description as a string, 3388dffb485Schristos or NULL if no architecture was specified. */ 3398dffb485Schristos const char *tdesc_architecture_name (const struct target_desc *target_desc); 3408dffb485Schristos 3418dffb485Schristos /* Set TARGET_DESC's osabi by NAME. */ 3428dffb485Schristos void set_tdesc_osabi (target_desc *target_desc, const char *name); 3438dffb485Schristos 3448dffb485Schristos /* Return the osabi associated with this target description as a string, 3458dffb485Schristos or NULL if no osabi was specified. */ 3468dffb485Schristos const char *tdesc_osabi_name (const struct target_desc *target_desc); 3478dffb485Schristos 3488dffb485Schristos /* Return the type associated with ID in the context of FEATURE, or 3498dffb485Schristos NULL if none. */ 3508dffb485Schristos struct tdesc_type *tdesc_named_type (const struct tdesc_feature *feature, 3518dffb485Schristos const char *id); 3528dffb485Schristos 3538dffb485Schristos /* Return the created feature named NAME in target description TDESC. */ 3548dffb485Schristos struct tdesc_feature *tdesc_create_feature (struct target_desc *tdesc, 3558dffb485Schristos const char *name); 3568dffb485Schristos 3578dffb485Schristos /* Return the created vector tdesc_type named NAME in FEATURE. */ 3588dffb485Schristos struct tdesc_type *tdesc_create_vector (struct tdesc_feature *feature, 3598dffb485Schristos const char *name, 3608dffb485Schristos struct tdesc_type *field_type, 3618dffb485Schristos int count); 3628dffb485Schristos 3638dffb485Schristos /* Return the created struct tdesc_type named NAME in FEATURE. */ 3648dffb485Schristos tdesc_type_with_fields *tdesc_create_struct (struct tdesc_feature *feature, 3658dffb485Schristos const char *name); 3668dffb485Schristos 3678dffb485Schristos /* Return the created union tdesc_type named NAME in FEATURE. */ 3688dffb485Schristos tdesc_type_with_fields *tdesc_create_union (struct tdesc_feature *feature, 3698dffb485Schristos const char *name); 3708dffb485Schristos 3718dffb485Schristos /* Return the created flags tdesc_type named NAME in FEATURE. */ 3728dffb485Schristos tdesc_type_with_fields *tdesc_create_flags (struct tdesc_feature *feature, 3738dffb485Schristos const char *name, 3748dffb485Schristos int size); 3758dffb485Schristos 3768dffb485Schristos /* Return the created enum tdesc_type named NAME in FEATURE. */ 3778dffb485Schristos tdesc_type_with_fields *tdesc_create_enum (struct tdesc_feature *feature, 3788dffb485Schristos const char *name, 3798dffb485Schristos int size); 3808dffb485Schristos 3818dffb485Schristos /* Add a new field to TYPE. FIELD_NAME is its name, and FIELD_TYPE is 3828dffb485Schristos its type. */ 3838dffb485Schristos void tdesc_add_field (tdesc_type_with_fields *type, const char *field_name, 3848dffb485Schristos struct tdesc_type *field_type); 3858dffb485Schristos 3868dffb485Schristos /* Add a new bitfield to TYPE, with range START to END. FIELD_NAME is its name, 3878dffb485Schristos and FIELD_TYPE is its type. */ 3888dffb485Schristos void tdesc_add_typed_bitfield (tdesc_type_with_fields *type, 3898dffb485Schristos const char *field_name, 3908dffb485Schristos int start, int end, 3918dffb485Schristos struct tdesc_type *field_type); 3928dffb485Schristos 3938dffb485Schristos /* Set the total length of TYPE. Structs which contain bitfields may 3948dffb485Schristos omit the reserved bits, so the end of the last field may not 3958dffb485Schristos suffice. */ 3968dffb485Schristos void tdesc_set_struct_size (tdesc_type_with_fields *type, int size); 3978dffb485Schristos 3988dffb485Schristos /* Add a new untyped bitfield to TYPE. 3998dffb485Schristos Untyped bitfields become either uint32 or uint64 depending on the size 4008dffb485Schristos of the underlying type. */ 4018dffb485Schristos void tdesc_add_bitfield (tdesc_type_with_fields *type, const char *field_name, 4028dffb485Schristos int start, int end); 4038dffb485Schristos 4048dffb485Schristos /* A flag is just a typed(bool) single-bit bitfield. 4058dffb485Schristos This function is kept to minimize changes in generated files. */ 4068dffb485Schristos void tdesc_add_flag (tdesc_type_with_fields *type, int start, 4078dffb485Schristos const char *flag_name); 4088dffb485Schristos 4098dffb485Schristos /* Add field with VALUE and NAME to the enum TYPE. */ 4108dffb485Schristos void tdesc_add_enum_value (tdesc_type_with_fields *type, int value, 4118dffb485Schristos const char *name); 4128dffb485Schristos 4138dffb485Schristos /* Create a register in feature FEATURE. */ 4148dffb485Schristos void tdesc_create_reg (struct tdesc_feature *feature, const char *name, 4158dffb485Schristos int regnum, int save_restore, const char *group, 4168dffb485Schristos int bitsize, const char *type); 4178dffb485Schristos 4188dffb485Schristos /* Return the tdesc in string XML format. */ 4198dffb485Schristos 4208dffb485Schristos const char *tdesc_get_features_xml (const target_desc *tdesc); 4218dffb485Schristos 4228dffb485Schristos /* Print target description as xml. */ 4238dffb485Schristos 4248dffb485Schristos class print_xml_feature : public tdesc_element_visitor 4258dffb485Schristos { 4268dffb485Schristos public: 4278dffb485Schristos print_xml_feature (std::string *buffer_) 4288dffb485Schristos : m_buffer (buffer_), 4298dffb485Schristos m_depth (0) 4308dffb485Schristos {} 4318dffb485Schristos 4328dffb485Schristos void visit_pre (const target_desc *e) override; 4338dffb485Schristos void visit_post (const target_desc *e) override; 4348dffb485Schristos void visit_pre (const tdesc_feature *e) override; 4358dffb485Schristos void visit_post (const tdesc_feature *e) override; 4368dffb485Schristos void visit (const tdesc_type_builtin *type) override; 4378dffb485Schristos void visit (const tdesc_type_vector *type) override; 4388dffb485Schristos void visit (const tdesc_type_with_fields *type) override; 4398dffb485Schristos void visit (const tdesc_reg *reg) override; 4408dffb485Schristos 4418dffb485Schristos private: 4428dffb485Schristos 4438dffb485Schristos /* Called with a positive value of ADJUST when we move inside an element, 4448dffb485Schristos for example inside <target>, and with a negative value when we leave 4458dffb485Schristos the element. In this class this function does nothing, but a 4468dffb485Schristos sub-class can override this to track the current level of nesting. */ 4478dffb485Schristos void indent (int adjust) 4488dffb485Schristos { 4498dffb485Schristos m_depth += (adjust * 2); 4508dffb485Schristos } 4518dffb485Schristos 4528dffb485Schristos /* Functions to add lines to the output buffer M_BUFFER. Each of these 4538dffb485Schristos functions appends a newline, so don't include one in the strings being 4548dffb485Schristos passed. */ 4558dffb485Schristos void add_line (const std::string &str); 4568dffb485Schristos void add_line (const char *fmt, ...) ATTRIBUTE_PRINTF (2, 3); 4578dffb485Schristos 4588dffb485Schristos /* The buffer we are writing too. */ 4598dffb485Schristos std::string *m_buffer; 4608dffb485Schristos 4618dffb485Schristos /* The current indentation depth. */ 4628dffb485Schristos int m_depth; 4638dffb485Schristos }; 4648dffb485Schristos 4658dffb485Schristos #endif /* COMMON_TDESC_H */ 466