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