Merge in changes from:
[official-gcc.git] / gcc / events.c
blob0ea1ffe2a54531c8f8b0ebe6cf616e22cb60ab85
1 /* High-level event callback mechanism for GCC plugins.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 Contributed by Inria.
6 Authors: Grigori Fursin <grigori.fursin@inria.fr>, Cupertino Miranda
7 <cupertinomiranda@gmail.com>, Zbigniew Chamski <zbigniew.chamski@gmail.com>.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "hashtab.h"
29 #include "toplev.h"
30 #include "gcc-plugin.h"
32 #include "highlev-plugin-internal.h"
34 /* Event structure. */
35 struct hl_plugin_event
37 int event; /* Number of the event */
38 event_callback_t run; /* Callback function */
42 /* Parameter structure. */
43 struct event_parameter
45 const char *name; /* Name for the parameter */
46 void *value; /* Pointer to data */
47 event_parameter_type type; /* Type enumeration of value */
51 static htab_t parameters_hash = NULL;
54 /* Hash dependent functions. */
55 static hashval_t
56 string_htab_hash (const void *x)
58 const char *p = *((const char * const *) x);
60 if (p != NULL)
61 return htab_hash_string (p);
63 return 0;
67 static int
68 string_htab_eq (const void *x, const void *y)
70 const char *s1 = *((const char * const *) x);
71 const char *s2 = *((const char * const *) y);
73 return !strcmp (s1, s2);
77 static void
78 parameter_htab_del (void *p)
80 struct parameter *param = (struct parameter *) p;
81 free (param);
85 static void
86 hash_param_callback (void *gcc_data, void *user_data)
88 struct hl_plugin_event *ev = (struct hl_plugin_event *) user_data;
89 va_list va;
90 const char *name;
91 void *value;
93 /* Possible extension:
94 Might interpret gcc_data differently for specific values of ev->event,
95 e.g. we could get a struct loop and put all the struct members in
96 named parameters. */
97 va_copy (va, * (va_list *) gcc_data);
99 while ((name = va_arg (va, const char *)) != NULL)
101 value = va_arg (va, void *);
102 register_event_parameter (name, value);
104 va_end (va);
105 ev->run ();
106 va_copy (va, * (va_list *) gcc_data);
107 while ((name = va_arg (va, const char *)) != NULL)
109 unregister_event_parameter (name);
110 va_arg (va, void *);
114 /* Register a new event into hash table. */
115 void
116 register_plugin_event (const char *event_name, event_callback_t func)
118 struct hl_plugin_event *ev = XCNEW (struct hl_plugin_event);
120 if (event_name == NULL)
121 internal_error ("Event cannot be registered with NULL name string\n");
123 ev->event = get_named_event_id (event_name, INSERT);
124 ev->run = func;
125 register_callback ("ICI", ev->event, hash_param_callback, ev);
129 /* Unregister an event. */
130 void
131 unregister_plugin_event (const char *event_name)
133 int event = get_named_event_id (event_name, NO_INSERT);
135 if (event < 0)
136 return;
138 unregister_callback ("ICI", event);
142 /* Return the array of all event names, terminated by NULL. */
143 const char **
144 list_plugin_events (void)
146 int i;
148 /* allocate space for all names + the terminating NULL */
149 const char **all_events = XNEWVEC (const char *, (event_last + 1));
151 /* collect all event names */
152 for (i = 0; i < event_last; i++)
153 all_events[i] = plugin_event_name[i];
155 /* mark end-of-array */
156 all_events[i] = NULL;
158 return all_events;
162 /* Register parameter.
163 * Insert parameter into hash table. Allocates hash table
164 * if needed. */
165 void
166 register_event_parameter (const char *name, void* value,
167 event_parameter_type type)
169 void **slot;
170 struct event_parameter *param = XCNEW (struct event_parameter);
172 param->name = name;
173 param->value = value;
174 param->type = type;
176 if (param->name == NULL || !strcmp ("", param->name))
177 internal_error ("Parameter cannot be registered with NULL name string\n");
179 if (parameters_hash == NULL)
180 parameters_hash =
181 htab_create_alloc (150, string_htab_hash,
182 string_htab_eq, parameter_htab_del, xcalloc, free);
184 if (parameters_hash != NULL)
186 slot = htab_find_slot (parameters_hash, param, INSERT);
187 *slot = (void *) param;
192 /* Unregister parameter. Remove it from hash table. */
193 void
194 unregister_event_parameter (const char *name)
196 struct event_parameter param;
197 param.name = name;
199 if (parameters_hash == NULL)
200 internal_error ("Parameter hash not initialized.\n");
202 if (parameters_hash != NULL)
203 htab_remove_elt (parameters_hash, &param);
207 /* Get parameter pointer to data.
208 Used by plugin to get pointer to data of the parameter <name>. */
209 void *
210 get_event_parameter (const char *name)
212 struct event_parameter tmp_param, *param;
213 tmp_param.name = name;
215 if (parameters_hash == NULL)
216 internal_error ("Parameter hash not initialized.\n");
218 if (parameters_hash != NULL)
220 param = (struct event_parameter *)
221 htab_find (parameters_hash, &tmp_param);
223 if (param != NULL && param->value != NULL)
224 return param->value;
226 return NULL;
229 /* Get parameter type.
230 Used by plugin to get type of data pointer of the parameter <name>. */
231 event_parameter_type
232 get_event_parameter_type (const char *name)
234 struct event_parameter tmp_param, *param;
235 tmp_param.name = name;
237 if (parameters_hash == NULL)
238 internal_error ("Parameter hash not initialized.\n");
240 if (parameters_hash != NULL)
242 param = (struct event_parameter *)
243 htab_find (parameters_hash, &tmp_param);
245 if (param != NULL && param->value != NULL)
246 return param->type;
248 return EP_VOID;
251 /* Get list of names of all registered ICI parameters.
252 * Traverse the hash table collecting the names of entries.
254 static int
255 add_parameter_name (void **slot, void *data)
257 const char ***crnt_name_ptr = (const char ***) data;
258 struct event_parameter *parameter = *(struct event_parameter **) slot;
260 /* store the name of the current parameter at the corresponding
261 location */
262 **crnt_name_ptr = parameter->name;
264 /* advance the current location */
265 (*crnt_name_ptr)++;
267 /* return "OK" */
268 return 1;
272 /* return the array of all parameter names, terminated by NULL */
273 const char **
274 list_event_parameters (void)
276 size_t num_parameters = htab_elements (parameters_hash);
278 /* allocate space for all names + the terminating NULL */
279 const char **all_parameters = (const char **)
280 xmalloc (sizeof (const char *) * (num_parameters+1));
282 const char ***data = &all_parameters; /* data ptr for mapped function */
283 const char **first_parameter = all_parameters; /* keep track of actual start */
285 /* mark end-of-array */
286 all_parameters[num_parameters] = NULL;
288 /* collect all parameter names */
289 htab_traverse_noresize (parameters_hash, add_parameter_name, data);
291 /* use the stored start-of-array - all_parameters has changed during
292 htab_traverse_noresize */
293 return first_parameter;