1 /* High-level event callback mechanism for GCC plugins.
2 Copyright (C) 2009 Free Software Foundation, Inc.
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
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
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/>. */
27 #include "coretypes.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. */
56 string_htab_hash (const void *x
)
58 const char *p
= *((const char * const *) x
);
61 return htab_hash_string (p
);
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
);
78 parameter_htab_del (void *p
)
80 struct parameter
*param
= (struct parameter
*) p
;
86 hash_param_callback (void *gcc_data
, void *user_data
)
88 struct hl_plugin_event
*ev
= (struct hl_plugin_event
*) user_data
;
94 /* Possible extension:
95 Might interpret gcc_data differently for specific values of ev->event,
96 e.g. we could get a struct loop and put all the struct members in
98 va_copy (va
, * (va_list *) gcc_data
);
100 while ((name
= va_arg (va
, const char *)) != NULL
)
102 type
= va_arg (va
, int);
103 value
= va_arg (va
, void *);
104 register_event_parameter (name
, value
, type
);
108 va_copy (va
, * (va_list *) gcc_data
);
109 while ((name
= va_arg (va
, const char *)) != NULL
)
111 unregister_event_parameter (name
);
116 /* Register a new event into hash table. */
118 register_plugin_event (const char *event_name
, event_callback_t func
)
120 struct hl_plugin_event
*ev
= XCNEW (struct hl_plugin_event
);
122 if (event_name
== NULL
)
123 internal_error ("Event cannot be registered with NULL name string\n");
125 ev
->event
= get_named_event_id (event_name
, INSERT
);
127 register_callback ("ICI", ev
->event
, hash_param_callback
, ev
);
131 /* Unregister an event. */
133 unregister_plugin_event (const char *event_name
)
135 int event
= get_named_event_id (event_name
, NO_INSERT
);
140 unregister_callback ("ICI", event
);
144 /* Return the array of all event names, terminated by NULL. */
146 list_plugin_events (void)
150 /* allocate space for all names + the terminating NULL */
151 const char **all_events
= XNEWVEC (const char *, (event_last
+ 1));
153 /* collect all event names */
154 for (i
= 0; i
< event_last
; i
++)
155 all_events
[i
] = plugin_event_name
[i
];
157 /* mark end-of-array */
158 all_events
[i
] = NULL
;
164 /* Register parameter.
165 * Insert parameter into hash table. Allocates hash table
168 register_event_parameter (const char *name
, void* value
,
169 event_parameter_type type
)
172 struct event_parameter
*param
= XCNEW (struct event_parameter
);
175 param
->value
= value
;
178 if (param
->name
== NULL
|| !strcmp ("", param
->name
))
179 internal_error ("Parameter cannot be registered with NULL name string\n");
181 if (parameters_hash
== NULL
)
183 htab_create_alloc (150, string_htab_hash
,
184 string_htab_eq
, parameter_htab_del
, xcalloc
, free
);
186 if (parameters_hash
!= NULL
)
188 slot
= htab_find_slot (parameters_hash
, param
, INSERT
);
189 *slot
= (void *) param
;
194 /* Unregister parameter. Remove it from hash table. */
196 unregister_event_parameter (const char *name
)
198 struct event_parameter param
;
201 if (parameters_hash
== NULL
)
202 internal_error ("Parameter hash not initialized.\n");
204 if (parameters_hash
!= NULL
)
205 htab_remove_elt (parameters_hash
, ¶m
);
209 /* Get parameter pointer to data.
210 Used by plugin to get pointer to data of the parameter <name>. */
212 get_event_parameter (const char *name
)
214 struct event_parameter tmp_param
, *param
;
215 tmp_param
.name
= name
;
217 if (parameters_hash
== NULL
)
218 internal_error ("Parameter hash not initialized.\n");
220 if (parameters_hash
!= NULL
)
222 param
= (struct event_parameter
*)
223 htab_find (parameters_hash
, &tmp_param
);
225 if (param
!= NULL
&& param
->value
!= NULL
)
231 /* Get parameter type.
232 Used by plugin to get type of data pointer of the parameter <name>. */
234 get_event_parameter_type (const char *name
)
236 struct event_parameter tmp_param
, *param
;
237 tmp_param
.name
= name
;
239 if (parameters_hash
== NULL
)
240 internal_error ("Parameter hash not initialized.\n");
242 if (parameters_hash
!= NULL
)
244 param
= (struct event_parameter
*)
245 htab_find (parameters_hash
, &tmp_param
);
247 if (param
!= NULL
&& param
->value
!= NULL
)
253 /* Get list of names of all registered ICI parameters.
254 * Traverse the hash table collecting the names of entries.
257 add_parameter_name (void **slot
, void *data
)
259 const char ***crnt_name_ptr
= (const char ***) data
;
260 struct event_parameter
*parameter
= *(struct event_parameter
**) slot
;
262 /* store the name of the current parameter at the corresponding
264 **crnt_name_ptr
= parameter
->name
;
266 /* advance the current location */
274 /* return the array of all parameter names, terminated by NULL */
276 list_event_parameters (void)
278 size_t num_parameters
= htab_elements (parameters_hash
);
280 /* allocate space for all names + the terminating NULL */
281 const char **all_parameters
= (const char **)
282 xmalloc (sizeof (const char *) * (num_parameters
+1));
284 const char ***data
= &all_parameters
; /* data ptr for mapped function */
285 const char **first_parameter
= all_parameters
; /* keep track of actual start */
287 /* mark end-of-array */
288 all_parameters
[num_parameters
] = NULL
;
290 /* collect all parameter names */
291 htab_traverse_noresize (parameters_hash
, add_parameter_name
, data
);
293 /* use the stored start-of-array - all_parameters has changed during
294 htab_traverse_noresize */
295 return first_parameter
;