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
;
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
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
);
106 va_copy (va
, * (va_list *) gcc_data
);
107 while ((name
= va_arg (va
, const char *)) != NULL
)
109 unregister_event_parameter (name
);
114 /* Register a new event into hash table. */
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
);
125 register_callback ("ICI", ev
->event
, hash_param_callback
, ev
);
129 /* Unregister an event. */
131 unregister_plugin_event (const char *event_name
)
133 int event
= get_named_event_id (event_name
, NO_INSERT
);
138 unregister_callback ("ICI", event
);
142 /* Return the array of all event names, terminated by NULL. */
144 list_plugin_events (void)
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
;
162 /* Register parameter.
163 * Insert parameter into hash table. Allocates hash table
166 register_event_parameter (const char *name
, void* value
,
167 event_parameter_type type
)
170 struct event_parameter
*param
= XCNEW (struct event_parameter
);
173 param
->value
= value
;
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
)
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. */
194 unregister_event_parameter (const char *name
)
196 struct event_parameter param
;
199 if (parameters_hash
== NULL
)
200 internal_error ("Parameter hash not initialized.\n");
202 if (parameters_hash
!= NULL
)
203 htab_remove_elt (parameters_hash
, ¶m
);
207 /* Get parameter pointer to data.
208 Used by plugin to get pointer to data of the parameter <name>. */
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
)
229 /* Get parameter type.
230 Used by plugin to get type of data pointer of the parameter <name>. */
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
)
251 /* Get list of names of all registered ICI parameters.
252 * Traverse the hash table collecting the names of entries.
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
262 **crnt_name_ptr
= parameter
->name
;
264 /* advance the current location */
272 /* return the array of all parameter names, terminated by NULL */
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
;