gdb: convert reggroups to use a std::vector
[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 const char *name;
36 enum reggroup_type type;
37 };
38
39 struct reggroup *
40 reggroup_new (const char *name, enum reggroup_type type)
41 {
42 struct reggroup *group = XNEW (struct reggroup);
43
44 group->name = name;
45 group->type = type;
46 return group;
47 }
48
49 /* See reggroups.h. */
50
51 struct reggroup *
52 reggroup_gdbarch_new (struct gdbarch *gdbarch, const char *name,
53 enum reggroup_type type)
54 {
55 struct reggroup *group = GDBARCH_OBSTACK_ZALLOC (gdbarch,
56 struct reggroup);
57
58 group->name = gdbarch_obstack_strdup (gdbarch, name);
59 group->type = type;
60 return group;
61 }
62
63 /* Register group attributes. */
64
65 const char *
66 reggroup_name (const struct reggroup *group)
67 {
68 return group->name;
69 }
70
71 enum reggroup_type
72 reggroup_type (const struct reggroup *group)
73 {
74 return group->type;
75 }
76
77 /* A container holding all the register groups for a particular
78 architecture. */
79
80 struct reggroups
81 {
82 /* Add GROUP to the list of register groups. */
83
84 void add (struct reggroup *group)
85 {
86 gdb_assert (group != nullptr);
87 gdb_assert (std::find (m_groups.begin(), m_groups.end(), group)
88 == m_groups.end());
89
90 m_groups.push_back (group);
91 }
92
93 /* The number of register groups. */
94
95 std::vector<struct reggroup *>::size_type
96 size () const
97 {
98 return m_groups.size ();
99 }
100
101 /* Return a reference to the list of all groups. */
102
103 const std::vector<struct reggroup *> &
104 groups () const
105 {
106 return m_groups;
107 }
108
109 private:
110 /* The register groups. */
111 std::vector<struct reggroup *> m_groups;
112 };
113
114 static struct gdbarch_data *reggroups_data;
115
116 /* Add GROUP to the list of register groups for GDBARCH. */
117
118 void
119 reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
120 {
121 struct reggroups *groups
122 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
123
124 gdb_assert (groups != nullptr);
125 gdb_assert (group != nullptr);
126
127 groups->add (group);
128 }
129
130 /* Called to initialize the per-gdbarch register group information. */
131
132 static void *
133 reggroups_init (struct obstack *obstack)
134 {
135 struct reggroups *groups = obstack_new<struct reggroups> (obstack);
136
137 /* Add the default groups. */
138 groups->add (general_reggroup);
139 groups->add (float_reggroup);
140 groups->add (system_reggroup);
141 groups->add (vector_reggroup);
142 groups->add (all_reggroup);
143 groups->add (save_reggroup);
144 groups->add (restore_reggroup);
145
146 return groups;
147 }
148
149 /* A register group iterator. */
150
151 struct reggroup *
152 reggroup_next (struct gdbarch *gdbarch, const struct reggroup *last)
153 {
154 /* Don't allow this function to be called during architecture
155 creation. If there are no groups, use the default groups list. */
156 struct reggroups *groups
157 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
158 gdb_assert (groups != nullptr);
159 gdb_assert (groups->size () > 0);
160
161 /* Return the first/next reggroup. */
162 if (last == nullptr)
163 return groups->groups ().front ();
164 for (int i = 0; i < groups->size (); ++i)
165 {
166 if (groups->groups ()[i] == last)
167 {
168 if (i + 1 < groups->size ())
169 return groups->groups ()[i + 1];
170 else
171 return nullptr;
172 }
173 }
174
175 return nullptr;
176 }
177
178 /* See reggroups.h. */
179
180 struct reggroup *
181 reggroup_prev (struct gdbarch *gdbarch, const struct reggroup *curr)
182 {
183 /* Don't allow this function to be called during architecture
184 creation. If there are no groups, use the default groups list. */
185 struct reggroups *groups
186 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
187 gdb_assert (groups != nullptr);
188 gdb_assert (groups->size () > 0);
189
190 /* Return the first/next reggroup. */
191 if (curr == nullptr)
192 return groups->groups ().back ();
193 for (int i = groups->size () - 1; i >= 0; --i)
194 {
195 if (groups->groups ()[i] == curr)
196 {
197 if (i - 1 >= 0)
198 return groups->groups ()[i - 1];
199 else
200 return nullptr;
201 }
202 }
203
204 return nullptr;
205 }
206
207 /* Is REGNUM a member of REGGROUP? */
208 int
209 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
210 const struct reggroup *group)
211 {
212 int vector_p;
213 int float_p;
214 int raw_p;
215
216 if (gdbarch_register_name (gdbarch, regnum) == NULL
217 || *gdbarch_register_name (gdbarch, regnum) == '\0')
218 return 0;
219 if (group == all_reggroup)
220 return 1;
221 vector_p = register_type (gdbarch, regnum)->is_vector ();
222 float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
223 || (register_type (gdbarch, regnum)->code ()
224 == TYPE_CODE_DECFLOAT));
225 raw_p = regnum < gdbarch_num_regs (gdbarch);
226 if (group == float_reggroup)
227 return float_p;
228 if (group == vector_reggroup)
229 return vector_p;
230 if (group == general_reggroup)
231 return (!vector_p && !float_p);
232 if (group == save_reggroup || group == restore_reggroup)
233 return raw_p;
234 return 0;
235 }
236
237 /* See reggroups.h. */
238
239 const reggroup *
240 reggroup_find (struct gdbarch *gdbarch, const char *name)
241 {
242 struct reggroup *group;
243
244 for (group = reggroup_next (gdbarch, NULL);
245 group != NULL;
246 group = reggroup_next (gdbarch, group))
247 {
248 if (strcmp (name, reggroup_name (group)) == 0)
249 return group;
250 }
251 return NULL;
252 }
253
254 /* Dump out a table of register groups for the current architecture. */
255
256 static void
257 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
258 {
259 struct reggroup *group = NULL;
260
261 do
262 {
263 /* Group name. */
264 {
265 const char *name;
266
267 if (group == NULL)
268 name = "Group";
269 else
270 name = reggroup_name (group);
271 gdb_printf (file, " %-10s", name);
272 }
273
274 /* Group type. */
275 {
276 const char *type;
277
278 if (group == NULL)
279 type = "Type";
280 else
281 {
282 switch (reggroup_type (group))
283 {
284 case USER_REGGROUP:
285 type = "user";
286 break;
287 case INTERNAL_REGGROUP:
288 type = "internal";
289 break;
290 default:
291 internal_error (__FILE__, __LINE__, _("bad switch"));
292 }
293 }
294 gdb_printf (file, " %-10s", type);
295 }
296
297 /* Note: If you change this, be sure to also update the
298 documentation. */
299
300 gdb_printf (file, "\n");
301
302 group = reggroup_next (gdbarch, group);
303 }
304 while (group != NULL);
305 }
306
307 static void
308 maintenance_print_reggroups (const char *args, int from_tty)
309 {
310 struct gdbarch *gdbarch = get_current_arch ();
311
312 if (args == NULL)
313 reggroups_dump (gdbarch, gdb_stdout);
314 else
315 {
316 stdio_file file;
317
318 if (!file.open (args, "w"))
319 perror_with_name (_("maintenance print reggroups"));
320 reggroups_dump (gdbarch, &file);
321 }
322 }
323
324 /* Pre-defined register groups. */
325 static struct reggroup general_group = { "general", USER_REGGROUP };
326 static struct reggroup float_group = { "float", USER_REGGROUP };
327 static struct reggroup system_group = { "system", USER_REGGROUP };
328 static struct reggroup vector_group = { "vector", USER_REGGROUP };
329 static struct reggroup all_group = { "all", USER_REGGROUP };
330 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
331 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
332
333 struct reggroup *const general_reggroup = &general_group;
334 struct reggroup *const float_reggroup = &float_group;
335 struct reggroup *const system_reggroup = &system_group;
336 struct reggroup *const vector_reggroup = &vector_group;
337 struct reggroup *const all_reggroup = &all_group;
338 struct reggroup *const save_reggroup = &save_group;
339 struct reggroup *const restore_reggroup = &restore_group;
340
341 void _initialize_reggroup ();
342 void
343 _initialize_reggroup ()
344 {
345 reggroups_data = gdbarch_data_register_pre_init (reggroups_init);
346
347 add_cmd ("reggroups", class_maintenance,
348 maintenance_print_reggroups, _("\
349 Print the internal register group names.\n\
350 Takes an optional file parameter."),
351 &maintenanceprintlist);
352
353 }