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/>. */
23 #include "arch-utils.h"
24 #include "reggroups.h"
28 #include "gdbcmd.h" /* For maintenanceprintlist. */
29 #include "gdbsupport/gdb_obstack.h"
31 /* Individual register groups. */
36 enum reggroup_type type
;
40 reggroup_new (const char *name
, enum reggroup_type type
)
42 struct reggroup
*group
= XNEW (struct reggroup
);
49 /* See reggroups.h. */
52 reggroup_gdbarch_new (struct gdbarch
*gdbarch
, const char *name
,
53 enum reggroup_type type
)
55 struct reggroup
*group
= GDBARCH_OBSTACK_ZALLOC (gdbarch
,
58 group
->name
= gdbarch_obstack_strdup (gdbarch
, name
);
63 /* Register group attributes. */
66 reggroup_name (const struct reggroup
*group
)
72 reggroup_type (const struct reggroup
*group
)
77 /* A container holding all the register groups for a particular
82 /* Add GROUP to the list of register groups. */
84 void add (struct reggroup
*group
)
86 gdb_assert (group
!= nullptr);
87 gdb_assert (std::find (m_groups
.begin(), m_groups
.end(), group
)
90 m_groups
.push_back (group
);
93 /* The number of register groups. */
95 std::vector
<struct reggroup
*>::size_type
98 return m_groups
.size ();
101 /* Return a reference to the list of all groups. */
103 const std::vector
<struct reggroup
*> &
110 /* The register groups. */
111 std::vector
<struct reggroup
*> m_groups
;
114 static struct gdbarch_data
*reggroups_data
;
116 /* Add GROUP to the list of register groups for GDBARCH. */
119 reggroup_add (struct gdbarch
*gdbarch
, struct reggroup
*group
)
121 struct reggroups
*groups
122 = (struct reggroups
*) gdbarch_data (gdbarch
, reggroups_data
);
124 gdb_assert (groups
!= nullptr);
125 gdb_assert (group
!= nullptr);
130 /* Called to initialize the per-gdbarch register group information. */
133 reggroups_init (struct obstack
*obstack
)
135 struct reggroups
*groups
= obstack_new
<struct reggroups
> (obstack
);
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
);
149 /* A register group iterator. */
152 reggroup_next (struct gdbarch
*gdbarch
, const struct reggroup
*last
)
154 /* Don't allow this function to be called during architecture
155 creation. If there are no groups, use the default groups list. */
156 struct reggroups
*groups
157 = (struct reggroups
*) gdbarch_data (gdbarch
, reggroups_data
);
158 gdb_assert (groups
!= nullptr);
159 gdb_assert (groups
->size () > 0);
161 /* Return the first/next reggroup. */
163 return groups
->groups ().front ();
164 for (int i
= 0; i
< groups
->size (); ++i
)
166 if (groups
->groups ()[i
] == last
)
168 if (i
+ 1 < groups
->size ())
169 return groups
->groups ()[i
+ 1];
178 /* See reggroups.h. */
181 reggroup_prev (struct gdbarch
*gdbarch
, const struct reggroup
*curr
)
183 /* Don't allow this function to be called during architecture
184 creation. If there are no groups, use the default groups list. */
185 struct reggroups
*groups
186 = (struct reggroups
*) gdbarch_data (gdbarch
, reggroups_data
);
187 gdb_assert (groups
!= nullptr);
188 gdb_assert (groups
->size () > 0);
190 /* Return the first/next reggroup. */
192 return groups
->groups ().back ();
193 for (int i
= groups
->size () - 1; i
>= 0; --i
)
195 if (groups
->groups ()[i
] == curr
)
198 return groups
->groups ()[i
- 1];
207 /* Is REGNUM a member of REGGROUP? */
209 default_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
210 const struct reggroup
*group
)
216 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
217 || *gdbarch_register_name (gdbarch
, regnum
) == '\0')
219 if (group
== all_reggroup
)
221 vector_p
= register_type (gdbarch
, regnum
)->is_vector ();
222 float_p
= (register_type (gdbarch
, regnum
)->code () == TYPE_CODE_FLT
223 || (register_type (gdbarch
, regnum
)->code ()
224 == TYPE_CODE_DECFLOAT
));
225 raw_p
= regnum
< gdbarch_num_regs (gdbarch
);
226 if (group
== float_reggroup
)
228 if (group
== vector_reggroup
)
230 if (group
== general_reggroup
)
231 return (!vector_p
&& !float_p
);
232 if (group
== save_reggroup
|| group
== restore_reggroup
)
237 /* See reggroups.h. */
240 reggroup_find (struct gdbarch
*gdbarch
, const char *name
)
242 struct reggroup
*group
;
244 for (group
= reggroup_next (gdbarch
, NULL
);
246 group
= reggroup_next (gdbarch
, group
))
248 if (strcmp (name
, reggroup_name (group
)) == 0)
254 /* Dump out a table of register groups for the current architecture. */
257 reggroups_dump (struct gdbarch
*gdbarch
, struct ui_file
*file
)
259 struct reggroup
*group
= NULL
;
270 name
= reggroup_name (group
);
271 gdb_printf (file
, " %-10s", name
);
282 switch (reggroup_type (group
))
287 case INTERNAL_REGGROUP
:
291 internal_error (__FILE__
, __LINE__
, _("bad switch"));
294 gdb_printf (file
, " %-10s", type
);
297 /* Note: If you change this, be sure to also update the
300 gdb_printf (file
, "\n");
302 group
= reggroup_next (gdbarch
, group
);
304 while (group
!= NULL
);
308 maintenance_print_reggroups (const char *args
, int from_tty
)
310 struct gdbarch
*gdbarch
= get_current_arch ();
313 reggroups_dump (gdbarch
, gdb_stdout
);
318 if (!file
.open (args
, "w"))
319 perror_with_name (_("maintenance print reggroups"));
320 reggroups_dump (gdbarch
, &file
);
324 /* Pre-defined register groups. */
325 static struct reggroup general_group
= { "general", USER_REGGROUP
};
326 static struct reggroup float_group
= { "float", USER_REGGROUP
};
327 static struct reggroup system_group
= { "system", USER_REGGROUP
};
328 static struct reggroup vector_group
= { "vector", USER_REGGROUP
};
329 static struct reggroup all_group
= { "all", USER_REGGROUP
};
330 static struct reggroup save_group
= { "save", INTERNAL_REGGROUP
};
331 static struct reggroup restore_group
= { "restore", INTERNAL_REGGROUP
};
333 struct reggroup
*const general_reggroup
= &general_group
;
334 struct reggroup
*const float_reggroup
= &float_group
;
335 struct reggroup
*const system_reggroup
= &system_group
;
336 struct reggroup
*const vector_reggroup
= &vector_group
;
337 struct reggroup
*const all_reggroup
= &all_group
;
338 struct reggroup
*const save_reggroup
= &save_group
;
339 struct reggroup
*const restore_reggroup
= &restore_group
;
341 void _initialize_reggroup ();
343 _initialize_reggroup ()
345 reggroups_data
= gdbarch_data_register_pre_init (reggroups_init
);
347 add_cmd ("reggroups", class_maintenance
,
348 maintenance_print_reggroups
, _("\
349 Print the internal register group names.\n\
350 Takes an optional file parameter."),
351 &maintenanceprintlist
);