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