2003-08-04 Andrew Cagney <cagney@redhat.com>
[platform/upstream/binutils.git] / gdb / reggroups.c
1 /* Register groupings for GDB, the GNU debugger.
2
3    Copyright 2002 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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.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   if (groups == NULL)
107     {
108       /* ULGH, called during architecture initialization.  Patch
109          things up.  */
110       groups = reggroups_init (gdbarch);
111       set_gdbarch_data (gdbarch, reggroups_data, groups);
112     }
113   add_group (groups, group,
114              GDBARCH_OBSTACK_ZALLOC (gdbarch, struct reggroup_el));
115 }
116
117 /* The default register groups for an architecture.  */
118
119 static struct reggroups default_groups = { NULL, &default_groups.first };
120
121 /* A register group iterator.  */
122
123 struct reggroup *
124 reggroup_next (struct gdbarch *gdbarch, struct reggroup *last)
125 {
126   struct reggroups *groups;
127   struct reggroup_el *el;
128   /* Don't allow this function to be called during architecture
129      creation.  If there are no groups, use the default groups list.  */
130   groups = gdbarch_data (gdbarch, reggroups_data);
131   gdb_assert (groups != NULL);
132   if (groups->first == NULL)
133     groups = &default_groups;
134
135   /* Retun the first/next reggroup.  */
136   if (last == NULL)
137     return groups->first->group;
138   for (el = groups->first; el != NULL; el = el->next)
139     {
140       if (el->group == last)
141         return el->next->group;
142     }
143   return NULL;
144 }
145
146 /* Is REGNUM a member of REGGROUP?  */
147 int
148 default_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
149                              struct reggroup *group)
150 {
151   int vector_p;
152   int float_p;
153   int raw_p;
154   if (REGISTER_NAME (regnum) == NULL
155       || *REGISTER_NAME (regnum) == '\0')
156     return 0;
157   if (group == all_reggroup)
158     return 1;
159   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
160   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
161   /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
162      (gdbarch), as not all architectures are multi-arch.  */
163   raw_p = regnum < NUM_REGS;
164   if (group == float_reggroup)
165     return float_p;
166   if (group == vector_reggroup)
167     return vector_p;
168   if (group == general_reggroup)
169     return (!vector_p && !float_p);
170   if (group == save_reggroup || group == restore_reggroup)
171     return raw_p;
172   return 0;   
173 }
174
175 /* Dump out a table of register groups for the current architecture.  */
176
177 static void
178 reggroups_dump (struct gdbarch *gdbarch, struct ui_file *file)
179 {
180   struct reggroup *group = NULL;
181   do
182     {
183       /* Group name.  */
184       {
185         const char *name;
186         if (group == NULL)
187           name = "Group";
188         else
189           name = reggroup_name (group);
190         fprintf_unfiltered (file, " %-10s", name);
191       }
192       
193       /* Group type.  */
194       {
195         const char *type;
196         if (group == NULL)
197           type = "Type";
198         else
199           {
200             switch (reggroup_type (group))
201               {
202               case USER_REGGROUP:
203                 type = "user";
204                 break;
205               case INTERNAL_REGGROUP:
206                 type = "internal";
207                 break;
208               default:
209                 internal_error (__FILE__, __LINE__, "bad switch");
210               }
211           }
212         fprintf_unfiltered (file, " %-10s", type);
213       }
214
215       /* Note: If you change this, be sure to also update the
216          documentation.  */
217       
218       fprintf_unfiltered (file, "\n");
219
220       group = reggroup_next (gdbarch, group);
221     }
222   while (group != NULL);
223 }
224
225 static void
226 maintenance_print_reggroups (char *args, int from_tty)
227 {
228   if (args == NULL)
229     reggroups_dump (current_gdbarch, gdb_stdout);
230   else
231     {
232       struct ui_file *file = gdb_fopen (args, "w");
233       if (file == NULL)
234         perror_with_name ("maintenance print reggroups");
235       reggroups_dump (current_gdbarch, file);    
236       ui_file_delete (file);
237     }
238 }
239
240 /* Pre-defined register groups.  */
241 static struct reggroup general_group = { "general", USER_REGGROUP };
242 static struct reggroup float_group = { "float", USER_REGGROUP };
243 static struct reggroup system_group = { "system", USER_REGGROUP };
244 static struct reggroup vector_group = { "vector", USER_REGGROUP };
245 static struct reggroup all_group = { "all", USER_REGGROUP };
246 static struct reggroup save_group = { "save", INTERNAL_REGGROUP };
247 static struct reggroup restore_group = { "restore", INTERNAL_REGGROUP };
248
249 struct reggroup *const general_reggroup = &general_group;
250 struct reggroup *const float_reggroup = &float_group;
251 struct reggroup *const system_reggroup = &system_group;
252 struct reggroup *const vector_reggroup = &vector_group;
253 struct reggroup *const all_reggroup = &all_group;
254 struct reggroup *const save_reggroup = &save_group;
255 struct reggroup *const restore_reggroup = &restore_group;
256
257 extern initialize_file_ftype _initialize_reggroup; /* -Wmissing-prototypes */
258
259 void
260 _initialize_reggroup (void)
261 {
262   reggroups_data = register_gdbarch_data (reggroups_init, NULL);
263
264   /* The pre-defined list of groups.  */
265   add_group (&default_groups, general_reggroup, XMALLOC (struct reggroup_el));
266   add_group (&default_groups, float_reggroup, XMALLOC (struct reggroup_el));
267   add_group (&default_groups, system_reggroup, XMALLOC (struct reggroup_el));
268   add_group (&default_groups, vector_reggroup, XMALLOC (struct reggroup_el));
269   add_group (&default_groups, all_reggroup, XMALLOC (struct reggroup_el));
270   add_group (&default_groups, save_reggroup, XMALLOC (struct reggroup_el));
271   add_group (&default_groups, restore_reggroup, XMALLOC (struct reggroup_el));
272
273   add_cmd ("reggroups", class_maintenance,
274            maintenance_print_reggroups, "\
275 Print the internal register group names.\n\
276 Takes an optional file parameter.",
277            &maintenanceprintlist);
278
279 }