-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-python-callbacks.c
403 lines (317 loc) · 10.6 KB
/
gcc-python-callbacks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Copyright 2011, 2012 David Malcolm <[email protected]>
Copyright 2011, 2012 Red Hat, Inc.
This is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include "gcc-python.h"
#include "gcc-python-closure.h"
#include "gcc-python-wrappers.h"
#include "gcc-c-api/gcc-location.h"
/*
Notes on the passes
As of 2011-03-30, http://gcc.gnu.org/onlinedocs/gccint/Plugins.html doesn't
seem to document the type of the gcc_data passed to each callback.
For reference, with gcc-4.6.0-0.15.fc15.x86_64 the types seem to be as
follows:
PLUGIN_ATTRIBUTES:
gcc_data=0x0
Called from: init_attributes () at ../../gcc/attribs.c:187
However, it seems at this point to have initialized these:
static const struct attribute_spec *attribute_tables[4];
static htab_t attribute_hash;
PLUGIN_PRAGMAS:
gcc_data=0x0
Called from: c_common_init () at ../../gcc/c-family/c-opts.c:1052
PLUGIN_START_UNIT:
gcc_data=0x0
Called from: compile_file () at ../../gcc/toplev.c:573
PLUGIN_PRE_GENERICIZE
gcc_data is: tree fndecl;
Called from: finish_function () at ../../gcc/c-decl.c:8323
PLUGIN_OVERRIDE_GATE
gcc_data:
&gate_status
bool gate_status;
Called from : execute_one_pass (pass=0x1011340) at ../../gcc/passes.c:1520
PLUGIN_PASS_EXECUTION
gcc_data: struct opt_pass *pass
Called from: execute_one_pass (pass=0x1011340) at ../../gcc/passes.c:1530
PLUGIN_ALL_IPA_PASSES_START
gcc_data=0x0
Called from: ipa_passes () at ../../gcc/cgraphunit.c:1779
PLUGIN_EARLY_GIMPLE_PASSES_START
gcc_data=0x0
Called from: execute_ipa_pass_list (pass=0x1011fa0) at ../../gcc/passes.c:1927
PLUGIN_EARLY_GIMPLE_PASSES_END
gcc_data=0x0
Called from: execute_ipa_pass_list (pass=0x1011fa0) at ../../gcc/passes.c:1930
PLUGIN_ALL_IPA_PASSES_END
gcc_data=0x0
Called from: ipa_passes () at ../../gcc/cgraphunit.c:1821
PLUGIN_ALL_PASSES_START
gcc_data=0x0
Called from: tree_rest_of_compilation (fndecl=0x7ffff16b1f00) at ../../gcc/tree-optimize.c:420
PLUGIN_ALL_PASSES_END
gcc_data=0x0
Called from: tree_rest_of_compilation (fndecl=0x7ffff16b1f00) at ../../gcc/tree-optimize.c:425
PLUGIN_FINISH_UNIT
gcc_data=0x0
Called from: compile_file () at ../../gcc/toplev.c:668
PLUGIN_FINISH_TYPE
gcc_data=tree
Called from c_parser_declspecs (parser=0x7fffef559730, specs=0x15296d0, scspec_ok=1 '\001', typespec_ok=1 '\001', start_attr_ok=<optimized out>, la=cla_nonabstract_decl) at ../../gcc/c-parser.c:2111
PLUGIN_PRAGMA
gcc_data=0x0
Called from: init_pragma at ../../gcc/c-family/c-pragma.c:1321
to "Allow plugins to register their own pragmas."
*/
static enum plugin_event current_event = (enum plugin_event)GCC_PYTHON_PLUGIN_BAD_EVENT;
int PyGcc_IsWithinEvent(enum plugin_event *out_event)
{
if (current_event != GCC_PYTHON_PLUGIN_BAD_EVENT) {
if (out_event) {
*out_event = current_event;
}
return 1;
} else {
return 0;
}
}
static void
PyGcc_FinishInvokingCallback(PyGILState_STATE gstate,
int expect_wrapped_data, PyObject *wrapped_gcc_data,
void *user_data)
CPYCHECKER_STEALS_REFERENCE_TO_ARG(3) /* wrapped_gcc_data */ ;
static void
PyGcc_FinishInvokingCallback(PyGILState_STATE gstate,
int expect_wrapped_data, PyObject *wrapped_gcc_data,
void *user_data)
{
struct callback_closure *closure = (struct callback_closure *)user_data;
PyObject *args = NULL;
PyObject *result = NULL;
gcc_location saved_loc = gcc_get_input_location();
enum plugin_event saved_event;
assert(closure);
/* We take ownership of wrapped_gcc_data.
For some callbacks types it will always be NULL; for others, it's only
NULL if an error has occurred: */
if (expect_wrapped_data && !wrapped_gcc_data) {
goto cleanup;
}
if (cfun) {
/* Temporarily override input_location to the top of the function: */
gcc_set_input_location(gcc_private_make_location(cfun->function_start_locus));
}
args = PyGcc_Closure_MakeArgs(closure, 1, wrapped_gcc_data);
if (!args) {
goto cleanup;
}
saved_event = current_event;
current_event = closure->event;
result = PyObject_Call(closure->callback, args, closure->kwargs);
current_event = saved_event;
if (!result) {
/* Treat an unhandled Python error as a compilation error: */
PyGcc_PrintException("Unhandled Python exception raised within callback");
}
// FIXME: the result is ignored
cleanup:
Py_XDECREF(wrapped_gcc_data);
Py_XDECREF(args);
Py_XDECREF(result);
/* We never cleanup "closure"; we don't know if we'll be called again */
PyGILState_Release(gstate);
gcc_set_input_location(saved_loc);
}
/*
C-level callbacks for each event ID follow, thunking into the registered
Python callable.
There's some repetition here, but it can be easier to debug if you have
separate breakpoint locations for each event ID.
*/
static void
PyGcc_CallbackFor_tree(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
tree t = (tree)gcc_data;
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
1, PyGccTree_New(gcc_private_make_tree(t)),
user_data);
}
static void
PyGcc_CallbackFor_PLUGIN_ATTRIBUTES(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
//printf("%s:%i:(%p, %p)\n", __FILE__, __LINE__, gcc_data, user_data);
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
0, NULL,
user_data);
}
static void
PyGcc_CallbackFor_PLUGIN_PASS_EXECUTION(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
struct opt_pass *pass = (struct opt_pass *)gcc_data;
//printf("%s:%i:(%p, %p)\n", __FILE__, __LINE__, gcc_data, user_data);
assert(pass);
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
1, PyGccPass_New(pass),
user_data);
}
static void
PyGcc_CallbackFor_FINISH(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
/* PLUGIN_FINISH:
gcc_data=0x0
called from: toplev_main at ../../gcc/toplev.c:1970
*/
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
0, NULL,
user_data);
}
static void
PyGcc_CallbackFor_FINISH_UNIT(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
0, NULL,
user_data);
}
static void
PyGcc_CallbackFor_GGC_START(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
0, NULL,
user_data);
}
static void
PyGcc_CallbackFor_GGC_MARKING(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
0, NULL,
user_data);
}
static void
PyGcc_CallbackFor_GGC_END(void *gcc_data, void *user_data)
{
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyGcc_FinishInvokingCallback(gstate,
0, NULL,
user_data);
}
PyObject*
PyGcc_RegisterCallback(PyObject *self, PyObject *args, PyObject *kwargs)
{
int event;
PyObject *callback = NULL;
PyObject *extraargs = NULL;
struct callback_closure *closure;
if (!PyArg_ParseTuple(args, "iO|O:register_callback", &event, &callback, &extraargs)) {
return NULL;
}
//printf("%s:%i:PyGcc_RegisterCallback\n", __FILE__, __LINE__);
closure = PyGcc_Closure_NewForPluginEvent(callback, extraargs, kwargs,
(enum plugin_event)event);
if (!closure) {
return PyErr_NoMemory();
}
switch ((enum plugin_event)event) {
case PLUGIN_ATTRIBUTES:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_PLUGIN_ATTRIBUTES,
closure);
break;
case PLUGIN_PRE_GENERICIZE:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_tree,
closure);
break;
case PLUGIN_PASS_EXECUTION:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_PLUGIN_PASS_EXECUTION,
closure);
break;
case PLUGIN_FINISH:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_FINISH,
closure);
break;
case PLUGIN_FINISH_UNIT:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_FINISH_UNIT,
closure);
break;
case PLUGIN_FINISH_TYPE:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_tree,
closure);
break;
case PLUGIN_GGC_START:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_GGC_START,
closure);
break;
case PLUGIN_GGC_MARKING:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_GGC_MARKING,
closure);
break;
case PLUGIN_GGC_END:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_GGC_END,
closure);
break;
/* PLUGIN_FINISH_DECL was added in gcc 4.7 onwards: */
#ifdef GCC_PYTHON_PLUGIN_CONFIG_has_PLUGIN_FINISH_DECL
case PLUGIN_FINISH_DECL:
register_callback("python", // FIXME
(enum plugin_event)event,
PyGcc_CallbackFor_tree,
closure);
break;
#endif /* GCC_PYTHON_PLUGIN_CONFIG_has_PLUGIN_FINISH_DECL */
default:
PyErr_Format(PyExc_ValueError, "event type %i invalid (or not wired up yet)", event);
return NULL;
}
Py_RETURN_NONE;
}
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/