gdb: more 'const' in gdb/reggroups.{c,h}
[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 const 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 const 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 (const 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<const struct reggroup *> &
104 groups () const
105 {
106 return m_groups;
107 }
108
109 private:
110 /* The register groups. */
111 std::vector<const 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, const 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 /* See reggroups.h. */
150 const std::vector<const reggroup *> &
151 gdbarch_reggroups (struct gdbarch *gdbarch)
152 {
153 struct reggroups *groups
154 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
155 gdb_assert (groups != nullptr);
156 gdb_assert (groups->size () > 0);
157 return groups->groups ();
158 }
159
160 /* Is REGNUM a member of REGGROUP? */
161 int
162 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
163 const struct reggroup *group)
164 {
165 int vector_p;
166 int float_p;
167 int raw_p;
168
169 if (gdbarch_register_name (gdbarch, regnum) == NULL
170 || *gdbarch_register_name (gdbarch, regnum) == '\0')
171 return 0;
172 if (group == all_reggroup)
173 return 1;
174 vector_p = register_type (gdbarch, regnum)->is_vector ();
175 float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
176 || (register_type (gdbarch, regnum)->code ()
177 == TYPE_CODE_DECFLOAT));
178 raw_p = regnum < gdbarch_num_regs (gdbarch);
179 if (group == float_reggroup)
180 return float_p;
181 if (group == vector_reggroup)
182 return vector_p;
183 if (group == general_reggroup)
184 return (!vector_p && !float_p);
185 if (group == save_reggroup || group == restore_reggroup)
186 return raw_p;
187 return 0;
188 }
189
190 /* See reggroups.h. */
191
192 const reggroup *
193 reggroup_find (struct gdbarch *gdbarch, const char *name)
194 {
195 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
196 {
197 if (strcmp (name, reggroup_name (group)) == 0)
198 return group;
199 }
200 return NULL;
201 }
202
203 /* Dump out a table of register groups for the current architecture. */
204
205 static void
206 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
207 {
208 static constexpr const char *fmt = " %-10s %-10s\n";
209
210 gdb_printf (file, fmt, "Group", "Type");
211
212 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
213 {
214 /* Group name. */
215 const char *name = reggroup_name (group);
216
217 /* Group type. */
218 const char *type;
219
220 switch (reggroup_type (group))
221 {
222 case USER_REGGROUP:
223 type = "user";
224 break;
225 case INTERNAL_REGGROUP:
226 type = "internal";
227 break;
228 default:
229 internal_error (__FILE__, __LINE__, _("bad switch"));
230 }
231
232 /* Note: If you change this, be sure to also update the
233 documentation. */
234
235 gdb_printf (file, fmt, name, type);
236 }
237 }
238
239 static void
240 maintenance_print_reggroups (const char *args, int from_tty)
241 {
242 struct gdbarch *gdbarch = get_current_arch ();
243
244 if (args == NULL)
245 reggroups_dump (gdbarch, gdb_stdout);
246 else
247 {
248 stdio_file file;
249
250 if (!file.open (args, "w"))
251 perror_with_name (_("maintenance print reggroups"));
252 reggroups_dump (gdbarch, &file);
253 }
254 }
255
256 /* Pre-defined register groups. */
257 static struct reggroup general_group = { "general", USER_REGGROUP };
258 static struct reggroup float_group = { "float", USER_REGGROUP };
259 static struct reggroup system_group = { "system", USER_REGGROUP };
260 static struct reggroup vector_group = { "vector", USER_REGGROUP };
261 static struct reggroup all_group = { "all", USER_REGGROUP };
262 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
263 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
264
265 struct reggroup *const general_reggroup = &general_group;
266 struct reggroup *const float_reggroup = &float_group;
267 struct reggroup *const system_reggroup = &system_group;
268 struct reggroup *const vector_reggroup = &vector_group;
269 struct reggroup *const all_reggroup = &all_group;
270 struct reggroup *const save_reggroup = &save_group;
271 struct reggroup *const restore_reggroup = &restore_group;
272
273 void _initialize_reggroup ();
274 void
275 _initialize_reggroup ()
276 {
277 reggroups_data = gdbarch_data_register_pre_init (reggroups_init);
278
279 add_cmd ("reggroups", class_maintenance,
280 maintenance_print_reggroups, _("\
281 Print the internal register group names.\n\
282 Takes an optional file parameter."),
283 &maintenanceprintlist);
284
285 }