gdb/
[deliverable/binutils-gdb.git] / gdb / reggroups.c
1 /* Register groupings for GDB, the GNU debugger.
2
3 Copyright (C) 2002, 2003, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 Contributed by Red Hat.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "reggroups.h"
26 #include "gdbtypes.h"
27 #include "gdb_assert.h"
28 #include "regcache.h"
29 #include "command.h"
30 #include "gdbcmd.h" /* For maintenanceprintlist. */
31
32 /* Individual register groups. */
33
34 struct reggroup
35 {
36 const char *name;
37 enum reggroup_type type;
38 };
39
40 struct reggroup *
41 reggroup_new (const char *name, enum reggroup_type type)
42 {
43 struct reggroup *group = XMALLOC (struct reggroup);
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 gdbarch *gdbarch)
81 {
82 struct reggroups *groups = GDBARCH_OBSTACK_ZALLOC (gdbarch,
83 struct reggroups);
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 = gdbarch_data (gdbarch, reggroups_data);
106
107 if (groups == NULL)
108 {
109 /* ULGH, called during architecture initialization. Patch
110 things up. */
111 groups = reggroups_init (gdbarch);
112 deprecated_set_gdbarch_data (gdbarch, reggroups_data, groups);
113 }
114 add_group (groups, group,
115 GDBARCH_OBSTACK_ZALLOC (gdbarch, struct reggroup_el));
116 }
117
118 /* The default register groups for an architecture. */
119
120 static struct reggroups default_groups = { NULL, &default_groups.first };
121
122 /* A register group iterator. */
123
124 struct reggroup *
125 reggroup_next (struct gdbarch *gdbarch, struct reggroup *last)
126 {
127 struct reggroups *groups;
128 struct reggroup_el *el;
129
130 /* Don't allow this function to be called during architecture
131 creation. If there are no groups, use the default groups list. */
132 groups = gdbarch_data (gdbarch, reggroups_data);
133 gdb_assert (groups != NULL);
134 if (groups->first == NULL)
135 groups = &default_groups;
136
137 /* Return the first/next reggroup. */
138 if (last == NULL)
139 return groups->first->group;
140 for (el = groups->first; el != NULL; el = el->next)
141 {
142 if (el->group == last)
143 {
144 if (el->next != NULL)
145 return el->next->group;
146 else
147 return NULL;
148 }
149 }
150 return NULL;
151 }
152
153 /* Is REGNUM a member of REGGROUP? */
154 int
155 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
156 struct reggroup *group)
157 {
158 int vector_p;
159 int float_p;
160 int raw_p;
161
162 if (gdbarch_register_name (gdbarch, regnum) == NULL
163 || *gdbarch_register_name (gdbarch, regnum) == '\0')
164 return 0;
165 if (group == all_reggroup)
166 return 1;
167 vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
168 float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
169 raw_p = regnum < gdbarch_num_regs (gdbarch);
170 if (group == float_reggroup)
171 return float_p;
172 if (group == vector_reggroup)
173 return vector_p;
174 if (group == general_reggroup)
175 return (!vector_p && !float_p);
176 if (group == save_reggroup || group == restore_reggroup)
177 return raw_p;
178 return 0;
179 }
180
181 /* Dump out a table of register groups for the current architecture. */
182
183 static void
184 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
185 {
186 struct reggroup *group = NULL;
187
188 do
189 {
190 /* Group name. */
191 {
192 const char *name;
193 if (group == NULL)
194 name = "Group";
195 else
196 name = reggroup_name (group);
197 fprintf_unfiltered (file, " %-10s", name);
198 }
199
200 /* Group type. */
201 {
202 const char *type;
203 if (group == NULL)
204 type = "Type";
205 else
206 {
207 switch (reggroup_type (group))
208 {
209 case USER_REGGROUP:
210 type = "user";
211 break;
212 case INTERNAL_REGGROUP:
213 type = "internal";
214 break;
215 default:
216 internal_error (__FILE__, __LINE__, _("bad switch"));
217 }
218 }
219 fprintf_unfiltered (file, " %-10s", type);
220 }
221
222 /* Note: If you change this, be sure to also update the
223 documentation. */
224
225 fprintf_unfiltered (file, "\n");
226
227 group = reggroup_next (gdbarch, group);
228 }
229 while (group != NULL);
230 }
231
232 static void
233 maintenance_print_reggroups (char *args, int from_tty)
234 {
235 struct gdbarch *gdbarch = get_current_arch ();
236
237 if (args == NULL)
238 reggroups_dump (gdbarch, gdb_stdout);
239 else
240 {
241 struct cleanup *cleanups;
242 struct ui_file *file = gdb_fopen (args, "w");
243 if (file == NULL)
244 perror_with_name (_("maintenance print reggroups"));
245 cleanups = make_cleanup_ui_file_delete (file);
246 reggroups_dump (gdbarch, file);
247 do_cleanups (cleanups);
248 }
249 }
250
251 /* Pre-defined register groups. */
252 static struct reggroup general_group = { "general", USER_REGGROUP };
253 static struct reggroup float_group = { "float", USER_REGGROUP };
254 static struct reggroup system_group = { "system", USER_REGGROUP };
255 static struct reggroup vector_group = { "vector", USER_REGGROUP };
256 static struct reggroup all_group = { "all", USER_REGGROUP };
257 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
258 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
259
260 struct reggroup *const general_reggroup = &general_group;
261 struct reggroup *const float_reggroup = &float_group;
262 struct reggroup *const system_reggroup = &system_group;
263 struct reggroup *const vector_reggroup = &vector_group;
264 struct reggroup *const all_reggroup = &all_group;
265 struct reggroup *const save_reggroup = &save_group;
266 struct reggroup *const restore_reggroup = &restore_group;
267
268 extern initialize_file_ftype _initialize_reggroup; /* -Wmissing-prototypes */
269
270 void
271 _initialize_reggroup (void)
272 {
273 reggroups_data = gdbarch_data_register_post_init (reggroups_init);
274
275 /* The pre-defined list of groups. */
276 add_group (&default_groups, general_reggroup, XMALLOC (struct reggroup_el));
277 add_group (&default_groups, float_reggroup, XMALLOC (struct reggroup_el));
278 add_group (&default_groups, system_reggroup, XMALLOC (struct reggroup_el));
279 add_group (&default_groups, vector_reggroup, XMALLOC (struct reggroup_el));
280 add_group (&default_groups, all_reggroup, XMALLOC (struct reggroup_el));
281 add_group (&default_groups, save_reggroup, XMALLOC (struct reggroup_el));
282 add_group (&default_groups, restore_reggroup, XMALLOC (struct reggroup_el));
283
284 add_cmd ("reggroups", class_maintenance,
285 maintenance_print_reggroups, _("\
286 Print the internal register group names.\n\
287 Takes an optional file parameter."),
288 &maintenanceprintlist);
289
290 }
This page took 0.034986 seconds and 4 git commands to generate.