]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/reggroups.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / reggroups.c
CommitLineData
b59ff9d5
AC
1/* Register groupings for GDB, the GNU debugger.
2
d01e8234 3 Copyright (C) 2002-2025 Free Software Foundation, Inc.
b59ff9d5
AC
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
b59ff9d5
AC
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b59ff9d5 21
e17c207e 22#include "arch-utils.h"
b59ff9d5
AC
23#include "reggroups.h"
24#include "gdbtypes.h"
b59ff9d5
AC
25#include "regcache.h"
26#include "command.h"
5b9707eb 27#include "cli/cli-cmds.h"
bf31fd38 28#include "gdbsupport/gdb_obstack.h"
b59ff9d5 29
524ad5e3
AB
30/* See reggroups.h. */
31
e7fe1011 32const reggroup *
b59ff9d5
AC
33reggroup_new (const char *name, enum reggroup_type type)
34{
c30c0f06 35 return new reggroup (name, type);
b59ff9d5
AC
36}
37
f7efd549
SH
38/* See reggroups.h. */
39
e7fe1011 40const reggroup *
f7efd549
SH
41reggroup_gdbarch_new (struct gdbarch *gdbarch, const char *name,
42 enum reggroup_type type)
43{
c30c0f06
AB
44 name = gdbarch_obstack_strdup (gdbarch, name);
45 return obstack_new<struct reggroup> (gdbarch_obstack (gdbarch),
46 name, type);
f7efd549
SH
47}
48
b89f77be
AB
49/* A container holding all the register groups for a particular
50 architecture. */
b59ff9d5
AC
51
52struct reggroups
53{
cb275538
TT
54 reggroups ()
55 {
56 /* Add the default groups. */
57 add (general_reggroup);
58 add (float_reggroup);
59 add (system_reggroup);
60 add (vector_reggroup);
61 add (all_reggroup);
62 add (save_reggroup);
63 add (restore_reggroup);
64 }
65
66 DISABLE_COPY_AND_ASSIGN (reggroups);
67
b89f77be
AB
68 /* Add GROUP to the list of register groups. */
69
e7fe1011 70 void add (const reggroup *group)
b89f77be
AB
71 {
72 gdb_assert (group != nullptr);
9454c9ce
SM
73
74 auto find_by_name = [group] (const reggroup *g)
75 {
76 return streq (group->name (), g->name ());
77 };
78 gdb_assert (std::find_if (m_groups.begin (), m_groups.end (), find_by_name)
79 == m_groups.end ());
b89f77be
AB
80
81 m_groups.push_back (group);
82 }
83
84 /* The number of register groups. */
85
86 std::vector<struct reggroup *>::size_type
87 size () const
88 {
89 return m_groups.size ();
90 }
91
92 /* Return a reference to the list of all groups. */
93
1bca9b1e 94 const std::vector<const struct reggroup *> &
b89f77be
AB
95 groups () const
96 {
97 return m_groups;
98 }
99
100private:
101 /* The register groups. */
1bca9b1e 102 std::vector<const struct reggroup *> m_groups;
b59ff9d5
AC
103};
104
524ad5e3
AB
105/* Key used to lookup register group data from a gdbarch. */
106
cb275538
TT
107static const registry<gdbarch>::key<reggroups> reggroups_data;
108
109/* Get the reggroups for the architecture, creating if necessary. */
110
111static reggroups *
112get_reggroups (struct gdbarch *gdbarch)
113{
114 struct reggroups *groups = reggroups_data.get (gdbarch);
115 if (groups == nullptr)
116 groups = reggroups_data.emplace (gdbarch);
117 return groups;
118}
b59ff9d5 119
524ad5e3 120/* See reggroups.h. */
b59ff9d5
AC
121
122void
e7fe1011 123reggroup_add (struct gdbarch *gdbarch, const reggroup *group)
b59ff9d5 124{
cb275538 125 struct reggroups *groups = get_reggroups (gdbarch);
7e6d0ac8 126
e7d69e72 127 gdb_assert (groups != nullptr);
b89f77be 128 gdb_assert (group != nullptr);
e7d69e72 129
b89f77be 130 groups->add (group);
b59ff9d5
AC
131}
132
55b40027 133/* See reggroups.h. */
1bca9b1e
AB
134const std::vector<const reggroup *> &
135gdbarch_reggroups (struct gdbarch *gdbarch)
55b40027 136{
cb275538 137 struct reggroups *groups = get_reggroups (gdbarch);
b89f77be
AB
138 gdb_assert (groups != nullptr);
139 gdb_assert (groups->size () > 0);
1bca9b1e 140 return groups->groups ();
55b40027
AB
141}
142
524ad5e3
AB
143/* See reggroups.h. */
144
b59ff9d5
AC
145int
146default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 147 const struct reggroup *group)
b59ff9d5
AC
148{
149 int vector_p;
150 int float_p;
151 int raw_p;
7e6d0ac8 152
637b2f86 153 if (*gdbarch_register_name (gdbarch, regnum) == '\0')
b59ff9d5
AC
154 return 0;
155 if (group == all_reggroup)
156 return 1;
bd63c870 157 vector_p = register_type (gdbarch, regnum)->is_vector ();
78134374
SM
158 float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
159 || (register_type (gdbarch, regnum)->code ()
7ed29001 160 == TYPE_CODE_DECFLOAT));
6bcde365 161 raw_p = regnum < gdbarch_num_regs (gdbarch);
b59ff9d5
AC
162 if (group == float_reggroup)
163 return float_p;
164 if (group == vector_reggroup)
165 return vector_p;
166 if (group == general_reggroup)
167 return (!vector_p && !float_p);
168 if (group == save_reggroup || group == restore_reggroup)
169 return raw_p;
b89f77be 170 return 0;
b59ff9d5
AC
171}
172
f7efd549
SH
173/* See reggroups.h. */
174
2b72890e 175const reggroup *
f7efd549
SH
176reggroup_find (struct gdbarch *gdbarch, const char *name)
177{
1bca9b1e 178 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
f7efd549 179 {
c30c0f06 180 if (strcmp (name, group->name ()) == 0)
f7efd549
SH
181 return group;
182 }
183 return NULL;
184}
185
b59ff9d5
AC
186/* Dump out a table of register groups for the current architecture. */
187
188static void
e69d35f4 189reggroups_dump (gdbarch *gdbarch, ui_out *out)
b59ff9d5 190{
e69d35f4
TT
191 ui_out_emit_table table (out, 2, -1, "RegGroups");
192 out->table_header (10, ui_left, "group", "Group");
193 out->table_header (10, ui_left, "type", "Type");
194 out->table_body ();
1bca9b1e
AB
195
196 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
b59ff9d5 197 {
e69d35f4
TT
198 ui_out_emit_tuple tuple_emitter (out, nullptr);
199
b59ff9d5 200 /* Group name. */
e69d35f4 201 out->field_string ("group", group->name ());
b89f77be 202
b59ff9d5 203 /* Group type. */
1bca9b1e
AB
204 const char *type;
205
c30c0f06 206 switch (group->type ())
1bca9b1e
AB
207 {
208 case USER_REGGROUP:
209 type = "user";
210 break;
211 case INTERNAL_REGGROUP:
212 type = "internal";
213 break;
214 default:
f34652de 215 internal_error (_("bad switch"));
1bca9b1e 216 }
b59ff9d5
AC
217
218 /* Note: If you change this, be sure to also update the
dda83cd7 219 documentation. */
e69d35f4
TT
220 out->field_string ("type", type);
221 out->text ("\n");
b59ff9d5 222 }
b59ff9d5
AC
223}
224
524ad5e3
AB
225/* Implement 'maintenance print reggroups' command. */
226
b59ff9d5 227static void
34e5fa26 228maintenance_print_reggroups (const char *args, int from_tty)
b59ff9d5 229{
e17c207e
UW
230 struct gdbarch *gdbarch = get_current_arch ();
231
b59ff9d5 232 if (args == NULL)
e69d35f4 233 reggroups_dump (gdbarch, current_uiout);
b59ff9d5
AC
234 else
235 {
d7e74731 236 stdio_file file;
123f5f96 237
d7e74731 238 if (!file.open (args, "w"))
e2e0b3e5 239 perror_with_name (_("maintenance print reggroups"));
e69d35f4
TT
240 ui_out_redirect_pop redirect (current_uiout, &file);
241 reggroups_dump (gdbarch, current_uiout);
b59ff9d5
AC
242 }
243}
244
245/* Pre-defined register groups. */
3a471c03
AB
246static const reggroup general_group = { "general", USER_REGGROUP };
247static const reggroup float_group = { "float", USER_REGGROUP };
248static const reggroup system_group = { "system", USER_REGGROUP };
249static const reggroup vector_group = { "vector", USER_REGGROUP };
250static const reggroup all_group = { "all", USER_REGGROUP };
251static const reggroup save_group = { "save", INTERNAL_REGGROUP };
252static const reggroup restore_group = { "restore", INTERNAL_REGGROUP };
253
254const reggroup *const general_reggroup = &general_group;
255const reggroup *const float_reggroup = &float_group;
256const reggroup *const system_reggroup = &system_group;
257const reggroup *const vector_reggroup = &vector_group;
258const reggroup *const all_reggroup = &all_group;
259const reggroup *const save_reggroup = &save_group;
260const reggroup *const restore_reggroup = &restore_group;
b59ff9d5 261
5fe70629 262INIT_GDB_FILE (reggroup)
b59ff9d5 263{
b59ff9d5 264 add_cmd ("reggroups", class_maintenance,
1a966eab 265 maintenance_print_reggroups, _("\
b59ff9d5 266Print the internal register group names.\n\
1a966eab 267Takes an optional file parameter."),
b59ff9d5
AC
268 &maintenanceprintlist);
269
270}