5d5e33f2a3574db3ec553f5bbf1b48cdc9734be0
[external/binutils.git] / gdb / reggroups.c
1 /* Register groupings for GDB, the GNU debugger.
2
3    Copyright (C) 2002-2017 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 "gdb_obstack.h"
30
31 /* Individual register groups.  */
32
33 struct reggroup
34 {
35   const char *name;
36   enum reggroup_type type;
37 };
38
39 struct reggroup *
40 reggroup_new (const char *name, enum reggroup_type type)
41 {
42   struct reggroup *group = XNEW (struct reggroup);
43
44   group->name = name;
45   group->type = type;
46   return group;
47 }
48
49 /* Register group attributes.  */
50
51 const char *
52 reggroup_name (struct reggroup *group)
53 {
54   return group->name;
55 }
56
57 enum reggroup_type
58 reggroup_type (struct reggroup *group)
59 {
60   return group->type;
61 }
62
63 /* A linked list of groups for the given architecture.  */
64
65 struct reggroup_el
66 {
67   struct reggroup *group;
68   struct reggroup_el *next;
69 };
70
71 struct reggroups
72 {
73   struct reggroup_el *first;
74   struct reggroup_el **last;
75 };
76
77 static struct gdbarch_data *reggroups_data;
78
79 static void *
80 reggroups_init (struct obstack *obstack)
81 {
82   struct reggroups *groups = OBSTACK_ZALLOC (obstack, struct reggroups);
83
84   groups->last = &groups->first;
85   return groups;
86 }
87
88 /* Add a register group (with attribute values) to the pre-defined
89    list.  */
90
91 static void
92 add_group (struct reggroups *groups, struct reggroup *group,
93            struct reggroup_el *el)
94 {
95   gdb_assert (group != NULL);
96   el->group = group;
97   el->next = NULL;
98   (*groups->last) = el;
99   groups->last = &el->next;
100 }
101
102 void
103 reggroup_add (struct gdbarch *gdbarch, struct reggroup *group)
104 {
105   struct reggroups *groups
106     = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
107
108   add_group (groups, group,
109              GDBARCH_OBSTACK_ZALLOC (gdbarch, struct reggroup_el));
110 }
111
112 /* The default register groups for an architecture.  */
113
114 static struct reggroups default_groups = { NULL, &default_groups.first };
115
116 /* A register group iterator.  */
117
118 struct reggroup *
119 reggroup_next (struct gdbarch *gdbarch, struct reggroup *last)
120 {
121   struct reggroups *groups;
122   struct reggroup_el *el;
123
124   /* Don't allow this function to be called during architecture
125      creation.  If there are no groups, use the default groups list.  */
126   groups = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
127   gdb_assert (groups != NULL);
128   if (groups->first == NULL)
129     groups = &default_groups;
130
131   /* Return the first/next reggroup.  */
132   if (last == NULL)
133     return groups->first->group;
134   for (el = groups->first; el != NULL; el = el->next)
135     {
136       if (el->group == last)
137         {
138           if (el->next != NULL)
139             return el->next->group;
140           else
141             return NULL;
142         }
143     }
144   return NULL;
145 }
146
147 /* See reggroups.h.  */
148
149 struct reggroup *
150 reggroup_prev (struct gdbarch *gdbarch, struct reggroup *curr)
151 {
152   struct reggroups *groups;
153   struct reggroup_el *el;
154   struct reggroup *prev;
155
156   /* Don't allow this function to be called during architecture
157      creation.  If there are no groups, use the default groups list.  */
158   groups = (struct reggroups *) gdbarch_data (gdbarch, reggroups_data);
159   gdb_assert (groups != NULL);
160   if (groups->first == NULL)
161     groups = &default_groups;
162
163   prev = NULL;
164   for (el = groups->first; el != NULL; el = el->next)
165     {
166       gdb_assert (el->group != NULL);
167       if (el->group == curr)
168         return prev;
169       prev = el->group;
170     }
171   if (curr == NULL)
172     return prev;
173   return NULL;
174 }
175
176 /* Is REGNUM a member of REGGROUP?  */
177 int
178 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
179                              struct reggroup *group)
180 {
181   int vector_p;
182   int float_p;
183   int raw_p;
184
185   if (gdbarch_register_name (gdbarch, regnum) == NULL
186       || *gdbarch_register_name (gdbarch, regnum) == '\0')
187     return 0;
188   if (group == all_reggroup)
189     return 1;
190   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
191   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
192   raw_p = regnum < gdbarch_num_regs (gdbarch);
193   if (group == float_reggroup)
194     return float_p;
195   if (group == vector_reggroup)
196     return vector_p;
197   if (group == general_reggroup)
198     return (!vector_p && !float_p);
199   if (group == save_reggroup || group == restore_reggroup)
200     return raw_p;
201   return 0;   
202 }
203
204 /* Dump out a table of register groups for the current architecture.  */
205
206 static void
207 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
208 {
209   struct reggroup *group = NULL;
210
211   do
212     {
213       /* Group name.  */
214       {
215         const char *name;
216
217         if (group == NULL)
218           name = "Group";
219         else
220           name = reggroup_name (group);
221         fprintf_unfiltered (file, " %-10s", name);
222       }
223       
224       /* Group type.  */
225       {
226         const char *type;
227
228         if (group == NULL)
229           type = "Type";
230         else
231           {
232             switch (reggroup_type (group))
233               {
234               case USER_REGGROUP:
235                 type = "user";
236                 break;
237               case INTERNAL_REGGROUP:
238                 type = "internal";
239                 break;
240               default:
241                 internal_error (__FILE__, __LINE__, _("bad switch"));
242               }
243           }
244         fprintf_unfiltered (file, " %-10s", type);
245       }
246
247       /* Note: If you change this, be sure to also update the
248          documentation.  */
249       
250       fprintf_unfiltered (file, "\n");
251
252       group = reggroup_next (gdbarch, group);
253     }
254   while (group != NULL);
255 }
256
257 static void
258 maintenance_print_reggroups (const char *args, int from_tty)
259 {
260   struct gdbarch *gdbarch = get_current_arch ();
261
262   if (args == NULL)
263     reggroups_dump (gdbarch, gdb_stdout);
264   else
265     {
266       stdio_file file;
267
268       if (!file.open (args, "w"))
269         perror_with_name (_("maintenance print reggroups"));
270       reggroups_dump (gdbarch, &file);
271     }
272 }
273
274 /* Pre-defined register groups.  */
275 static struct reggroup general_group = { "general", USER_REGGROUP };
276 static struct reggroup float_group = { "float", USER_REGGROUP };
277 static struct reggroup system_group = { "system", USER_REGGROUP };
278 static struct reggroup vector_group = { "vector", USER_REGGROUP };
279 static struct reggroup all_group = { "all", USER_REGGROUP };
280 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
281 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
282
283 struct reggroup *const general_reggroup = &general_group;
284 struct reggroup *const float_reggroup = &float_group;
285 struct reggroup *const system_reggroup = &system_group;
286 struct reggroup *const vector_reggroup = &vector_group;
287 struct reggroup *const all_reggroup = &all_group;
288 struct reggroup *const save_reggroup = &save_group;
289 struct reggroup *const restore_reggroup = &restore_group;
290
291 void
292 _initialize_reggroup (void)
293 {
294   reggroups_data = gdbarch_data_register_pre_init (reggroups_init);
295
296   /* The pre-defined list of groups.  */
297   add_group (&default_groups, general_reggroup, XNEW (struct reggroup_el));
298   add_group (&default_groups, float_reggroup, XNEW (struct reggroup_el));
299   add_group (&default_groups, system_reggroup, XNEW (struct reggroup_el));
300   add_group (&default_groups, vector_reggroup, XNEW (struct reggroup_el));
301   add_group (&default_groups, all_reggroup, XNEW (struct reggroup_el));
302   add_group (&default_groups, save_reggroup, XNEW (struct reggroup_el));
303   add_group (&default_groups, restore_reggroup, XNEW (struct reggroup_el));
304
305   add_cmd ("reggroups", class_maintenance,
306            maintenance_print_reggroups, _("\
307 Print the internal register group names.\n\
308 Takes an optional file parameter."),
309            &maintenanceprintlist);
310
311 }