-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-rtl-c.py
198 lines (167 loc) · 6.45 KB
/
generate-rtl-c.py
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
# Copyright 2011-2012, 2015 David Malcolm <[email protected]>
# Copyright 2011-2012, 2015 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/>.
from maketreetypes import iter_rtl_expr_types #, iter_rtl_struct_types
from cpybuilder import *
from testcpychecker import get_gcc_version
from wrapperbuilder import PyGccWrapperTypeObject
GCC_VERSION = get_gcc_version()
cu = CompilationUnit()
cu.add_include('gcc-python.h')
cu.add_include('gcc-python-wrappers.h')
cu.add_include('gcc-plugin.h')
cu.add_include("rtl.h")
modinit_preinit = ''
modinit_postinit = ''
#rtl_struct_types = list(iter_rtl_struct_types())
rtl_expr_types = list(iter_rtl_expr_types())
#print rtl_types
# Should be a three-level hierarchy:
#
# - Rtl base class
# - intermediate subclasses, one per enum rtx_class
# - concrete subclasses, one per enum rtx_code
def generate_rtl_base_class():
#
# Generate the gcc.Rtl class:
#
global modinit_preinit
global modinit_postinit
'''
cu.add_defn("""
static PyObject *
PyGccRtl_get_block(struct PyGccRtl *self, void *closure)
{
return PyGccTree_New(rtl_block(self->stmt));
}
""")
'''
getsettable = PyGetSetDefTable('PyGccRtl_getset_table', [])
if GCC_VERSION < 5000:
getsettable.add_gsdef('loc',
'PyGccRtl_get_location',
None,
'Source code location of this instruction, as a gcc.Location')
getsettable.add_gsdef('operands',
'PyGccRtl_get_operands',
None,
'Operands of this expression, as a tuple')
cu.add_defn(getsettable.c_defn())
pytype = PyGccWrapperTypeObject(identifier = 'PyGccRtl_TypeObj',
localname = 'Rtl',
tp_name = 'gcc.Rtl',
tp_dealloc = 'PyGccWrapper_Dealloc',
struct_name = 'PyGccRtl',
tp_new = 'PyType_GenericNew',
tp_getset = getsettable.identifier,
tp_repr = '(reprfunc)PyGccRtl_repr',
tp_str = '(reprfunc)PyGccRtl_str',
)
cu.add_defn(pytype.c_defn())
modinit_preinit += pytype.c_invoke_type_ready()
modinit_postinit += pytype.c_invoke_add_to_module()
generate_rtl_base_class()
# enum rtx_class from gcc/rtl.h (as seen in 4.6.0):
enum_rtx_class = ('RTX_COMPARE',
'RTX_COMM_COMPARE',
'RTX_BIN_ARITH',
'RTX_COMM_ARITH',
'RTX_UNARY',
'RTX_EXTRA',
'RTX_MATCH',
'RTX_INSN',
'RTX_OBJ',
'RTX_CONST_OBJ',
'RTX_TERNARY',
'RTX_BITFIELD_OPS',
'RTX_AUTOINC')
def generate_intermediate_rtx_class_subclasses():
global modinit_preinit
global modinit_postinit
for rtx_class in enum_rtx_class:
cc = camel_case(rtx_class)
pytype = PyGccWrapperTypeObject(identifier = 'PyGccRtlClassType%s_TypeObj' % cc,
localname = 'RtlClassType' + cc,
tp_name = 'gcc.%s' % cc,
struct_name = 'PyGccRtl',
tp_new = 'PyType_GenericNew',
tp_base = '&PyGccRtl_TypeObj',
#tp_getset = getsettable.identifier,
#tp_repr = '(reprfunc)PyGccRtl_repr',
#tp_str = '(reprfunc)PyGccRtl_str',
)
cu.add_defn(pytype.c_defn())
modinit_preinit += pytype.c_invoke_type_ready()
modinit_postinit += pytype.c_invoke_add_to_module()
generate_intermediate_rtx_class_subclasses()
def generate_concrete_rtx_code_subclasses():
global modinit_preinit
global modinit_postinit
for expr_type in rtl_expr_types:
cc = expr_type.camel_cased_string()
getsettable = None
if getsettable:
cu.add_defn(getsettable.c_defn())
pytype = PyGccWrapperTypeObject(identifier = 'PyGcc%s_TypeObj' % cc,
localname = cc,
tp_name = 'gcc.%s' % cc,
struct_name = 'PyGccRtl',
tp_new = 'PyType_GenericNew',
tp_base = ('&PyGccRtlClassType%s_TypeObj'
% camel_case(expr_type.CLASS)),
tp_getset = getsettable.identifier if getsettable else None,
tp_repr = '(reprfunc)PyGccRtl_repr',
tp_str = '(reprfunc)PyGccRtl_str',
)
cu.add_defn(pytype.c_defn())
modinit_preinit += pytype.c_invoke_type_ready()
modinit_postinit += pytype.c_invoke_add_to_module()
generate_concrete_rtx_code_subclasses()
def generate_rtl_code_map():
cu.add_defn('\n/* Map from GCC rtl codes to PyGccWrapperTypeObject* */\n')
cu.add_defn('PyGccWrapperTypeObject *pytype_for_rtx_code[] = {\n')
for expr_type in rtl_expr_types:
cc = expr_type.camel_cased_string()
cu.add_defn(' &PyGcc%s_TypeObj, /* %s */\n' % (cc, expr_type.ENUM))
cu.add_defn('};\n\n')
cu.add_defn("""
PyGccWrapperTypeObject*
PyGcc_autogenerated_rtl_type_for_stmt(gcc_rtl_insn insn)
{
enum rtx_code code = insn.inner->code;
/* printf("code:%i\\n", code); */
assert(code >= 0);
assert(code < LAST_AND_UNUSED_RTX_CODE);
return pytype_for_rtx_code[code];
}
""")
generate_rtl_code_map()
cu.add_defn("""
int autogenerated_rtl_init_types(void)
{
""" + modinit_preinit + """
return 1;
error:
return 0;
}
""")
cu.add_defn("""
void autogenerated_rtl_add_types(PyObject *m)
{
""" + modinit_postinit + """
}
""")
print(cu.as_str())