Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / reggroups.c
blob1a13cb2fba0b9fbd39cc1de939d8d089dc9da451
1 /* Register groupings for GDB, the GNU debugger.
3 Copyright (C) 2002-2022 Free Software Foundation, Inc.
5 Contributed by Red Hat.
7 This file is part of GDB.
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.
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.
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/>. */
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"
31 /* See reggroups.h. */
33 const reggroup *
34 reggroup_new (const char *name, enum reggroup_type type)
36 return new reggroup (name, type);
39 /* See reggroups.h. */
41 const reggroup *
42 reggroup_gdbarch_new (struct gdbarch *gdbarch, const char *name,
43 enum reggroup_type type)
45 name = gdbarch_obstack_strdup (gdbarch, name);
46 return obstack_new<struct reggroup> (gdbarch_obstack (gdbarch),
47 name, type);
50 /* A container holding all the register groups for a particular
51 architecture. */
53 struct reggroups
55 /* Add GROUP to the list of register groups. */
57 void add (const reggroup *group)
59 gdb_assert (group != nullptr);
60 gdb_assert (std::find (m_groups.begin(), m_groups.end(), group)
61 == m_groups.end());
63 m_groups.push_back (group);
66 /* The number of register groups. */
68 std::vector<struct reggroup *>::size_type
69 size () const
71 return m_groups.size ();
74 /* Return a reference to the list of all groups. */
76 const std::vector<const struct reggroup *> &
77 groups () const
79 return m_groups;
82 private:
83 /* The register groups. */
84 std::vector<const struct reggroup *> m_groups;
87 /* Key used to lookup register group data from a gdbarch. */
89 static struct gdbarch_data *reggroups_data;
91 /* See reggroups.h. */
93 void
94 reggroup_add (struct gdbarch *gdbarch, const reggroup *group)
96 struct reggroups *groups
97 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
99 gdb_assert (groups != nullptr);
100 gdb_assert (group != nullptr);
102 groups->add (group);
105 /* Called to initialize the per-gdbarch register group information. */
107 static void *
108 reggroups_init (struct obstack *obstack)
110 struct reggroups *groups = obstack_new<struct reggroups> (obstack);
112 /* Add the default groups. */
113 groups->add (general_reggroup);
114 groups->add (float_reggroup);
115 groups->add (system_reggroup);
116 groups->add (vector_reggroup);
117 groups->add (all_reggroup);
118 groups->add (save_reggroup);
119 groups->add (restore_reggroup);
121 return groups;
124 /* See reggroups.h. */
125 const std::vector<const reggroup *> &
126 gdbarch_reggroups (struct gdbarch *gdbarch)
128 struct reggroups *groups
129 = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
130 gdb_assert (groups != nullptr);
131 gdb_assert (groups->size () > 0);
132 return groups->groups ();
135 /* See reggroups.h. */
138 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
139 const struct reggroup *group)
141 int vector_p;
142 int float_p;
143 int raw_p;
145 if (gdbarch_register_name (gdbarch, regnum) == NULL
146 || *gdbarch_register_name (gdbarch, regnum) == '\0')
147 return 0;
148 if (group == all_reggroup)
149 return 1;
150 vector_p = register_type (gdbarch, regnum)->is_vector ();
151 float_p = (register_type (gdbarch, regnum)->code () == TYPE_CODE_FLT
152 || (register_type (gdbarch, regnum)->code ()
153 == TYPE_CODE_DECFLOAT));
154 raw_p = regnum < gdbarch_num_regs (gdbarch);
155 if (group == float_reggroup)
156 return float_p;
157 if (group == vector_reggroup)
158 return vector_p;
159 if (group == general_reggroup)
160 return (!vector_p && !float_p);
161 if (group == save_reggroup || group == restore_reggroup)
162 return raw_p;
163 return 0;
166 /* See reggroups.h. */
168 const reggroup *
169 reggroup_find (struct gdbarch *gdbarch, const char *name)
171 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
173 if (strcmp (name, group->name ()) == 0)
174 return group;
176 return NULL;
179 /* Dump out a table of register groups for the current architecture. */
181 static void
182 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
184 static constexpr const char *fmt = " %-10s %-10s\n";
186 gdb_printf (file, fmt, "Group", "Type");
188 for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
190 /* Group name. */
191 const char *name = group->name ();
193 /* Group type. */
194 const char *type;
196 switch (group->type ())
198 case USER_REGGROUP:
199 type = "user";
200 break;
201 case INTERNAL_REGGROUP:
202 type = "internal";
203 break;
204 default:
205 internal_error (__FILE__, __LINE__, _("bad switch"));
208 /* Note: If you change this, be sure to also update the
209 documentation. */
211 gdb_printf (file, fmt, name, type);
215 /* Implement 'maintenance print reggroups' command. */
217 static void
218 maintenance_print_reggroups (const char *args, int from_tty)
220 struct gdbarch *gdbarch = get_current_arch ();
222 if (args == NULL)
223 reggroups_dump (gdbarch, gdb_stdout);
224 else
226 stdio_file file;
228 if (!file.open (args, "w"))
229 perror_with_name (_("maintenance print reggroups"));
230 reggroups_dump (gdbarch, &file);
234 /* Pre-defined register groups. */
235 static const reggroup general_group = { "general", USER_REGGROUP };
236 static const reggroup float_group = { "float", USER_REGGROUP };
237 static const reggroup system_group = { "system", USER_REGGROUP };
238 static const reggroup vector_group = { "vector", USER_REGGROUP };
239 static const reggroup all_group = { "all", USER_REGGROUP };
240 static const reggroup save_group = { "save", INTERNAL_REGGROUP };
241 static const reggroup restore_group = { "restore", INTERNAL_REGGROUP };
243 const reggroup *const general_reggroup = &general_group;
244 const reggroup *const float_reggroup = &float_group;
245 const reggroup *const system_reggroup = &system_group;
246 const reggroup *const vector_reggroup = &vector_group;
247 const reggroup *const all_reggroup = &all_group;
248 const reggroup *const save_reggroup = &save_group;
249 const reggroup *const restore_reggroup = &restore_group;
251 void _initialize_reggroup ();
252 void
253 _initialize_reggroup ()
255 reggroups_data = gdbarch_data_register_pre_init (reggroups_init);
257 add_cmd ("reggroups", class_maintenance,
258 maintenance_print_reggroups, _("\
259 Print the internal register group names.\n\
260 Takes an optional file parameter."),
261 &maintenanceprintlist);