-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-python.h
412 lines (332 loc) · 11.9 KB
/
gcc-python.h
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
404
405
406
407
408
409
410
411
412
/*
Copyright 2011-2013, 2015, 2017 David Malcolm <[email protected]>
Copyright 2011-2013, 2015, 2017 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/>.
*/
#ifndef INCLUDED__GCC_PYTHON_H
#define INCLUDED__GCC_PYTHON_H
#include <gcc-plugin.h>
#include "autogenerated-config.h"
#if 1
#include "tree.h"
#if (GCC_VERSION >= 4009)
#include "basic-block.h" /* needed by gimple.h in 4.9 */
#include "function.h" /* needed by gimple.h in 4.9 */
#include "tree-ssa-alias.h" /* needed by gimple.h in 4.9 */
#include "internal-fn.h" /* needed by gimple.h in 4.9 */
#include "is-a.h" /* needed by gimple.h in 4.9 */
#include "predict.h" /* needed by gimple.h in 4.9 */
#include "gimple-expr.h" /* needed by gimple.h in 4.9 */
#endif
#include "gimple.h"
/* gcc 10 removed this header */
#if (GCC_VERSION < 10000)
#include "params.h"
#endif
#endif
#include "gcc-c-api/gcc-cfg.h"
/* GCC doesn't seem to give us an ID for "invalid event", so invent one: */
#define GCC_PYTHON_PLUGIN_BAD_EVENT (0xffff)
/*
Define some macros to allow us to use cpychecker's custom attributes when
compiling the plugin using itself (gcc-with-cpychecker), but also to turn
them off when compiling with vanilla gcc (and other compilers).
*/
#if defined(WITH_CPYCHECKER_RETURNS_BORROWED_REF_ATTRIBUTE)
#define CPYCHECKER_RETURNS_BORROWED_REF \
__attribute__((cpychecker_returns_borrowed_ref))
#else
#define CPYCHECKER_RETURNS_BORROWED_REF
#endif
#if defined(WITH_CPYCHECKER_STEALS_REFERENCE_TO_ARG_ATTRIBUTE)
#define CPYCHECKER_STEALS_REFERENCE_TO_ARG(n) \
__attribute__((cpychecker_steals_reference_to_arg(n)))
#else
#define CPYCHECKER_STEALS_REFERENCE_TO_ARG(n)
#endif
#if defined(WITH_CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF_ATTRIBUTE)
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(typename) \
__attribute__((cpychecker_type_object_for_typedef(typename)))
#else
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(typename)
#endif
/*
PyObject shared header for wrapping GCC objects, for integration
with GCC's garbage collector (so that things we wrap don't get collected
from under us)
*/
typedef struct PyGccWrapper
{
PyObject_HEAD
/*
Keep track of a linked list of all live wrapper objects,
so that we can mark the wrapped objects for GCC's garbage
collector:
*/
struct PyGccWrapper *wr_prev;
struct PyGccWrapper *wr_next;
} PyGccWrapper;
/*
PyTypeObject subclass for PyGccWrapper, adding a GC-marking callback:
*/
typedef void (*wrtp_marker) (PyGccWrapper *wrapper);
typedef struct PyGccWrapperTypeObject
{
PyHeapTypeObject wrtp_base;
/* Callback for marking the wrapped objects when GCC's garbage
collector runs: */
wrtp_marker wrtp_mark;
} PyGccWrapperTypeObject;
/* gcc-python-wrapper.c */
/*
Allocate a new PyGccWrapper of the given type, setting up:
- ob_refcnt
- ob_type
and calling PyGccWrapper_Track() on it:
*/
#define PyGccWrapper_New(ARG_structname, ARG_typeobj) \
( (ARG_structname*) _PyGccWrapper_New(ARG_typeobj) )
extern PyGccWrapper *
_PyGccWrapper_New(PyGccWrapperTypeObject *typeobj);
extern void
PyGccWrapper_Track(PyGccWrapper *obj);
extern void
PyGccWrapper_Dealloc(PyObject *obj);
extern PyTypeObject PyGccWrapperMeta_TypeObj;
/*
Macro DECLARE_SIMPLE_WRAPPER():
ARG_structname:
the struct tag for the resulting python wrapper class,
e.g. "PyGccPass"
ARG_typeobj:
the singleton PyTypeObject for the resulting python wrapper class
e.g. "PyGccPass_TypeObj"
ARG_typename:
a C identifier to use when referring to instances of the underlying
type e.g. "pass"
ARG_wrappedtype:
the GCC type being wrapped e.g. "struct opt_pass *"
ARG_fieldname:
the name of the field within the CPython struct, containing the pointer
to the GCC data
*/
#define DECLARE_SIMPLE_WRAPPER(ARG_structname, ARG_typeobj, ARG_typename, ARG_wrappedtype, ARG_fieldname) \
struct ARG_structname { \
struct PyGccWrapper head; \
ARG_wrappedtype ARG_fieldname; \
}; \
\
typedef struct ARG_structname ARG_structname; \
\
extern PyObject * \
ARG_structname##_New(ARG_wrappedtype ARG_fieldname); \
\
extern PyObject * \
ARG_structname##_NewUnique(ARG_wrappedtype ARG_fieldname); \
\
extern PyGccWrapperTypeObject ARG_typeobj \
CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(#ARG_structname); \
\
extern void \
PyGcc_WrtpMarkFor##ARG_structname(ARG_structname *wrapper); \
/* end of macro */
DECLARE_SIMPLE_WRAPPER(PyGccCallgraphEdge,
PyGccCallgraphEdge_TypeObj,
cgraph_edge,
gcc_cgraph_edge, edge)
DECLARE_SIMPLE_WRAPPER(PyGccCallgraphNode,
PyGccCallgraphNode_TypeObj,
cgraph_node,
gcc_cgraph_node, node)
DECLARE_SIMPLE_WRAPPER(PyGccPass,
PyGccPass_TypeObj,
pass,
struct opt_pass *, pass)
DECLARE_SIMPLE_WRAPPER(PyGccLocation,
PyGccLocation_TypeObj,
location,
gcc_location, loc)
/* class rich_location was added to libcpp in gcc 6. */
#if (GCC_VERSION >= 6000)
DECLARE_SIMPLE_WRAPPER(PyGccRichLocation,
PyGccRichLocation_TypeObj,
rich_location,
rich_location, richloc)
#endif
DECLARE_SIMPLE_WRAPPER(PyGccGimple,
PyGccGimple_TypeObj,
gimple,
gcc_gimple, stmt);
DECLARE_SIMPLE_WRAPPER(PyGccEdge,
PyGccEdge_TypeObj,
edge,
gcc_cfg_edge, e)
DECLARE_SIMPLE_WRAPPER(PyGccBasicBlock,
PyGccBasicBlock_TypeObj,
basic_block,
gcc_cfg_block, bb)
DECLARE_SIMPLE_WRAPPER(PyGccCfg,
PyGccCfg_TypeObj,
cfg,
gcc_cfg, cfg)
DECLARE_SIMPLE_WRAPPER(PyGccFunction,
PyGccFunction_TypeObj,
function,
gcc_function, fun)
DECLARE_SIMPLE_WRAPPER(PyGccOption,
PyGccOption_TypeObj,
option,
gcc_option, opt)
/* gcc 10 removed params.h */
#if (GCC_VERSION < 10000)
DECLARE_SIMPLE_WRAPPER(PyGccParameter,
PyGccParameter_TypeObj,
param_num,
compiler_param, param_num)
#endif
DECLARE_SIMPLE_WRAPPER(PyGccRtl,
PyGccRtl_TypeObj,
#if (GCC_VERSION >= 5000)
rtl_insn *,
#else
rtl,
#endif
gcc_rtl_insn, insn)
DECLARE_SIMPLE_WRAPPER(PyGccTree,
PyGccTree_TypeObj,
tree,
gcc_tree, t)
DECLARE_SIMPLE_WRAPPER(PyGccVariable,
PyGccVariable_TypeObj,
variable,
gcc_variable, var)
/* autogenerated-callgraph.c */
int autogenerated_callgraph_init_types(void);
void autogenerated_callgraph_add_types(PyObject *m);
/* autogenerated-cfg.c */
int autogenerated_cfg_init_types(void);
void autogenerated_cfg_add_types(PyObject *m);
/* autogenerated-function.c */
int autogenerated_function_init_types(void);
void autogenerated_function_add_types(PyObject *m);
/* autogenerated-gimple.c */
int autogenerated_gimple_init_types(void);
void autogenerated_gimple_add_types(PyObject *m);
PyGccWrapperTypeObject*
PyGcc_autogenerated_gimple_type_for_stmt(gcc_gimple stmt);
/* autogenerated-location.c */
int autogenerated_location_init_types(void);
void autogenerated_location_add_types(PyObject *m);
/* autogenerated-option.c */
int autogenerated_option_init_types(void);
void autogenerated_option_add_types(PyObject *m);
/* GCC 10 removed params.h */
#if (GCC_VERSION < 10000)
/* autogenerated-parameter.c */
int autogenerated_parameter_init_types(void);
void autogenerated_parameter_add_types(PyObject *m);
#endif
/* autogenerated-pass.c */
int autogenerated_pass_init_types(void);
void autogenerated_pass_add_types(PyObject *m);
extern PyGccWrapperTypeObject PyGccGimplePass_TypeObj;
extern PyGccWrapperTypeObject PyGccRtlPass_TypeObj;
extern PyGccWrapperTypeObject PyGccSimpleIpaPass_TypeObj;
extern PyGccWrapperTypeObject PyGccIpaPass_TypeObj;
/* autogenerated-pretty-printer.c */
int autogenerated_pretty_printer_init_types(void);
void autogenerated_pretty_printer_add_types(PyObject *m);
/* autogenerated-rtl.c */
int autogenerated_rtl_init_types(void);
void autogenerated_rtl_add_types(PyObject *m);
PyGccWrapperTypeObject *
PyGcc_autogenerated_rtl_type_for_stmt(gcc_rtl_insn insn);
/* autogenerated-tree.c */
int autogenerated_tree_init_types(void);
void autogenerated_tree_add_types(PyObject *m);
PyGccWrapperTypeObject*
PyGcc_autogenerated_tree_type_for_tree(gcc_tree t, int borrow_ref);
PyGccWrapperTypeObject*
PyGcc_autogenerated_tree_type_for_tree_code(enum tree_code code, int borrow_ref);
extern PyGccWrapperTypeObject PyGccComponentRef_TypeObj;
/* autogenerated-variable.c */
int autogenerated_variable_init_types(void);
void autogenerated_variable_add_types(PyObject *m);
PyObject *
PyGccStringOrNone(const char *str_or_null);
#if (GCC_VERSION >= 4008)
PyObject *
VEC_tree_as_PyList(vec<tree, va_gc> *vec_nodes);
#else
PyObject *
VEC_tree_as_PyList(VEC(tree,gc) *vec_nodes);
#endif
PyObject *
PyGcc_int_from_int_cst(tree int_cst);
PyObject *
PyGcc_int_from_decimal_string_buffer(const char *buf);
void
PyGcc_DoubleIntAsText(double_int di, bool is_unsigned,
char *out, int bufsize) __attribute__((nonnull));
#if (GCC_VERSION >= 5000)
PyObject *
PyGcc_int_from_wide_int();
#else
PyObject *
PyGcc_int_from_double_int(double_int di, bool is_unsigned);
#endif
PyObject *
PyGcc_LazilyCreateWrapper(PyObject **cache,
void *ptr,
PyObject *(*ctor)(void *ptr));
int
PyGcc_insert_new_wrapper_into_cache(PyObject **cache,
void *ptr,
PyObject *obj);
/* gcc-python.c */
int PyGcc_IsWithinEvent(enum plugin_event *out_event);
char * PyGcc_strdup(const char *str) __attribute__((nonnull));
void PyGcc_PrintException(const char *msg);
/*
Shorthand for: repr(getattr(obj, attrname))
*/
PyObject *
PyGcc_GetReprOfAttribute(PyObject *obj, const char *attrname);
/* Python 2 vs Python 3 compat: */
#if PY_MAJOR_VERSION == 3
/* Python 3: use PyUnicode for "str" and PyLong for "int": */
#define PyGccString_FromFormat PyUnicode_FromFormat
#define PyGccString_FromString PyUnicode_FromString
#define PyGccString_FromString_and_size PyUnicode_FromStringAndSize
#define PyGccString_AsString _PyUnicode_AsString
#define PyGccInt_FromLong PyLong_FromLong
#define PyGccInt_Check PyLong_Check
#define PyGccInt_AsLong PyLong_AsLong
#else
/* Python 2: use PyString for "str" and PyInt for "int": */
#define PyGccString_FromFormat PyString_FromFormat
#define PyGccString_FromString PyString_FromString
#define PyGccString_FromString_and_size PyString_FromStringAndSize
#define PyGccString_AsString PyString_AsString
#define PyGccInt_FromLong PyInt_FromLong
#define PyGccInt_Check PyInt_Check
#define PyGccInt_AsLong PyInt_AsLong
#endif
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/
#endif /* INCLUDED__GCC_PYTHON_H */