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