gdb: convert reggroup to a C++ class with constructor, etc
[binutils-gdb.git] / gdb / reggroups.c
1 /* Register groupings for GDB, the GNU debugger.
2
3 Copyright (C) 2002-2022 Free Software Foundation, Inc.
4
5 Contributed by Red Hat.
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 "reggroups.h"
25 #include "gdbtypes.h"
26 #include "regcache.h"
27 #include "command.h"
28 #include "gdbcmd.h" /* For maintenanceprintlist. */
29 #include "gdbsupport/gdb_obstack.h"
30
31 /* Individual register groups. */
32
33 struct reggroup
34 {
35 /* Create a new register group object. The NAME is not owned by the new
36 reggroup object, so must outlive the object. */
37 reggroup (const char *name, enum reggroup_type type)
38 : m_name (name),
39 m_type (type)
40 { /* Nothing. */ }
41
42 /* Return the name for this register group. */
43 const char *name () const
44 { return m_name; }
45
46 /* Return the type of this register group. */
47 enum reggroup_type type () const
48 { return m_type; }
49
50 private:
51 /* The name of this register group. */
52 const char *m_name;
53
54 /* The type of this register group. */
55 enum reggroup_type m_type;
56 };
57
58 const reggroup *
59 reggroup_new (const char *name, enum reggroup_type type)
60 {
61 return new reggroup (name, type);
62 }
63
64 /* See reggroups.h. */
65
66 const reggroup *
67 reggroup_gdbarch_new (struct gdbarch *gdbarch, const char *name,
68 enum reggroup_type type)
69 {
70 name = gdbarch_obstack_strdup (gdbarch, name);
71 return obstack_new<struct reggroup> (gdbarch_obstack (gdbarch),
72 name, type);
73 }
74
75 /* Register group attributes. */
76
77 const char *
78 reggroup_name (const struct reggroup *group)
79 {
80 return group->name ();
81 }
82
83 enum reggroup_type
84 reggroup_type (const struct reggroup *group)
85 {
86 return group->type ();
87 }
88
89 /* A container holding all the register groups for a particular
90 architecture. */
91
92 struct reggroups
93 {
94 /* Add GROUP to the list of register groups. */
95
96 void add (const reggroup *group)
97 {
98 gdb_assert (group != nullptr);
99 gdb_assert (std::find (m_groups.begin(), m_groups.end(), group)
100 == m_groups.end());
101
102 m_groups.push_back (group);
103 }
104
105 /* The number of register groups. */
106
107 std::vector<struct reggroup *>::size_type
108 size () const
109 {
110 return m_groups.size ();
111 }
112
113 /* Return a reference to the list of all groups. */
114
115 const std::vector<const struct reggroup *> &
116 groups () const
117 {
118 return m_groups;
119 }
120
121 private:
122 /* The register groups. */
123 std::vector<const struct reggroup *> m_groups;
124 };
125
126 static struct gdbarch_data *reggroups_data;
127
128 /* Add GROUP to the list of register groups for GDBARCH. */
129
130 void
131 reggroup_add (struct gdbarch *gdbarch, const reggroup *group)
132 {
133 struct reggroups *groups
134 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
135
136 gdb_assert (groups != nullptr);
137 gdb_assert (group != nullptr);
138
139 groups->add (group);
140 }
141
142 /* Called to initialize the per-gdbarch register group information. */
143
144 static void *
145 reggroups_init (struct obstack *obstack)
146 {
147 struct reggroups *groups = obstack_new<struct reggroups> (obstack);
148
149 /* Add the default groups. */
150 groups->add (general_reggroup);
151 groups->add (float_reggroup);
152 groups->add (system_reggroup);
153 groups->add (vector_reggroup);
154 groups->add (all_reggroup);
155 groups->add (save_reggroup);
156 groups->add (restore_reggroup);
157
158 return groups;
159 }
160
161 /* See reggroups.h. */
162 const std::vector<const reggroup *> &
163 gdbarch_reggroups (struct gdbarch *gdbarch)
164 {
165 struct reggroups *groups
166 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
167 gdb_assert (groups != nullptr);
168 gdb_assert (groups->size () > 0);
169 return groups->groups ();
170 }
171
172 /* Is REGNUM a member of REGGROUP? */
173 int
174 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
175 const struct reggroup *group)
176 {
177 int vector_p;
178 int float_p;
179 int raw_p;
180
181 if (gdbarch_register_name (gdbarch, regnum) == NULL
182 || *gdbarch_register_name (gdbarch, regnum) == '\0')
183 return 0;
184 if (group == all_reggroup)
185 return 1;
186 vector_p = register_type (gdbarch, regnum)->is_vector ();
187 float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
188 || (register_type (gdbarch, regnum)->code ()
189 == TYPE_CODE_DECFLOAT));
190 raw_p = regnum < gdbarch_num_regs (gdbarch);
191 if (group == float_reggroup)
192 return float_p;
193 if (group == vector_reggroup)
194 return vector_p;
195 if (group == general_reggroup)
196 return (!vector_p && !float_p);
197 if (group == save_reggroup || group == restore_reggroup)
198 return raw_p;
199 return 0;
200 }
201
202 /* See reggroups.h. */
203
204 const reggroup *
205 reggroup_find (struct gdbarch *gdbarch, const char *name)
206 {
207 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
208 {
209 if (strcmp (name, group->name ()) == 0)
210 return group;
211 }
212 return NULL;
213 }
214
215 /* Dump out a table of register groups for the current architecture. */
216
217 static void
218 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
219 {
220 static constexpr const char *fmt = " %-10s %-10s\n";
221
222 gdb_printf (file, fmt, "Group", "Type");
223
224 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
225 {
226 /* Group name. */
227 const char *name = group->name ();
228
229 /* Group type. */
230 const char *type;
231
232 switch (group->type ())
233 {
234 case USER_REGGROUP:
235 type = "user";
236 break;
237 case INTERNAL_REGGROUP:
238 type = "internal";
239 break;
240 default:
241 internal_error (__FILE__, __LINE__, _("bad switch"));
242 }
243
244 /* Note: If you change this, be sure to also update the
245 documentation. */
246
247 gdb_printf (file, fmt, name, type);
248 }
249 }
250
251 static void
252 maintenance_print_reggroups (const char *args, int from_tty)
253 {
254 struct gdbarch *gdbarch = get_current_arch ();
255
256 if (args == NULL)
257 reggroups_dump (gdbarch, gdb_stdout);
258 else
259 {
260 stdio_file file;
261
262 if (!file.open (args, "w"))
263 perror_with_name (_("maintenance print reggroups"));
264 reggroups_dump (gdbarch, &file);
265 }
266 }
267
268 /* Pre-defined register groups. */
269 static const reggroup general_group = { "general", USER_REGGROUP };
270 static const reggroup float_group = { "float", USER_REGGROUP };
271 static const reggroup system_group = { "system", USER_REGGROUP };
272 static const reggroup vector_group = { "vector", USER_REGGROUP };
273 static const reggroup all_group = { "all", USER_REGGROUP };
274 static const reggroup save_group = { "save", INTERNAL_REGGROUP };
275 static const reggroup restore_group = { "restore", INTERNAL_REGGROUP };
276
277 const reggroup *const general_reggroup = &general_group;
278 const reggroup *const float_reggroup = &float_group;
279 const reggroup *const system_reggroup = &system_group;
280 const reggroup *const vector_reggroup = &vector_group;
281 const reggroup *const all_reggroup = &all_group;
282 const reggroup *const save_reggroup = &save_group;
283 const reggroup *const restore_reggroup = &restore_group;
284
285 void _initialize_reggroup ();
286 void
287 _initialize_reggroup ()
288 {
289 reggroups_data = gdbarch_data_register_pre_init (reggroups_init);
290
291 add_cmd ("reggroups", class_maintenance,
292 maintenance_print_reggroups, _("\
293 Print the internal register group names.\n\
294 Takes an optional file parameter."),
295 &maintenanceprintlist);
296
297 }