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
|
/* This file is part of GCC.
GCC 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, or (at your option) any later
version.
GCC 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 GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "ansidecl.h"
#include "coretypes.h"
#include "opts.h"
#include "tree.h"
#include "gimple.h"
#include "toplev.h"
#include "debug.h"
#include "options.h"
#include "flags.h"
#include "convert.h"
#include "diagnostic-core.h"
#include "langhooks.h"
#include "langhooks-def.h"
#include "target.h"
#include <gmp.h>
#include <mpfr.h>
#include "vec.h"
#include "hashtab.h"
#include "gpython.h"
#include "py-dot.h"
#include "py-vec.h"
#include "py-tree.h"
#include "py-builtins.h"
VEC(tree,gc) * gpy_builtin_types_vec;
static tree gpy_build_py_object_type (void);
tree gpy_builtin_get_init_call (void)
{
tree fntype = build_function_type (void_type_node, void_list_node);
tree gpy_rr_init = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_init_runtime"),
fntype);
tree restype = TREE_TYPE (gpy_rr_init);
tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype);
DECL_CONTEXT (resdecl) = gpy_rr_init;
DECL_RESULT (gpy_rr_init) = resdecl;
DECL_EXTERNAL (gpy_rr_init) = 1;
TREE_PUBLIC (gpy_rr_init) = 1;
return build_call_expr (gpy_rr_init, 0, NULL_TREE);
}
tree gpy_builtin_get_cleanup_final_call (void)
{
tree fntype = build_function_type( void_type_node, void_list_node );
tree gpy_rr_cleanup = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_cleanup_final"),
fntype );
tree restype = TREE_TYPE( gpy_rr_cleanup );
tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype );
DECL_CONTEXT(resdecl) = gpy_rr_cleanup;
DECL_RESULT(gpy_rr_cleanup) = resdecl;
DECL_EXTERNAL( gpy_rr_cleanup ) = 1;
TREE_PUBLIC( gpy_rr_cleanup ) = 1;
return build_call_expr( gpy_rr_cleanup, 0, NULL_TREE );
}
tree gpy_builtin_get_fold_int_call( int val )
{
tree params = NULL_TREE;
chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
tree fntype = build_function_type( ptr_type_node, params );
tree gpy_eval_expr_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_fold_integer"),
fntype );
tree restype = TREE_TYPE(gpy_eval_expr_decl);
tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype );
DECL_CONTEXT(resdecl) = gpy_eval_expr_decl;
DECL_RESULT(gpy_eval_expr_decl) = resdecl;
DECL_EXTERNAL( gpy_eval_expr_decl ) = 1;
TREE_PUBLIC( gpy_eval_expr_decl ) = 1;
return build_call_expr (gpy_eval_expr_decl, 1,
build_int_cst (integer_type_node,
val)
);
}
tree gpy_builtin_get_eval_expression_call (tree t1, tree t2, opcode_t op)
{
tree params = NULL_TREE;
chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
tree fntype = build_function_type( ptr_type_node, params );
tree gpy_eval_expr_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_eval_expression"),
fntype );
tree restype = TREE_TYPE(gpy_eval_expr_decl);
tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype );
DECL_CONTEXT( resdecl ) = gpy_eval_expr_decl;
DECL_RESULT( gpy_eval_expr_decl ) = resdecl;
DECL_EXTERNAL( gpy_eval_expr_decl ) = 1;
TREE_PUBLIC( gpy_eval_expr_decl ) = 1;
return build_call_expr (gpy_eval_expr_decl, 3, t1, t2,
build_int_cst (integer_type_node, op));
}
tree gpy_builtin_get_incr_ref_call (tree x)
{
tree params = NULL_TREE;
chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
tree fntype = build_function_type( ptr_type_node, params );
tree gpy_incr_ref_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_incr_ref_count"),
fntype );
tree restype = TREE_TYPE( gpy_incr_ref_decl );
tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype );
DECL_CONTEXT( resdecl ) = gpy_incr_ref_decl;
DECL_RESULT( gpy_incr_ref_decl ) = resdecl;
DECL_EXTERNAL( gpy_incr_ref_decl ) = 1;
TREE_PUBLIC( gpy_incr_ref_decl ) = 1;
return build_call_expr (gpy_incr_ref_decl, 1, x);
}
tree gpy_builtin_get_decr_ref_call (tree x)
{
tree params = NULL_TREE;
chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
tree fntype = build_function_type( ptr_type_node, params );
tree gpy_decr_ref_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_decr_ref_count"),
fntype );
tree restype = TREE_TYPE( gpy_decr_ref_decl );
tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype );
DECL_CONTEXT( resdecl ) = gpy_decr_ref_decl;
DECL_RESULT( gpy_decr_ref_decl ) = resdecl;
DECL_EXTERNAL( gpy_decr_ref_decl ) = 1;
TREE_PUBLIC( gpy_decr_ref_decl ) = 1;
return build_call_expr (gpy_decr_ref_decl, 1, x);
}
tree gpy_builtin_get_print_call (int n, tree * args)
{
tree params = NULL_TREE;
chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, va_list_type_node, NULL_TREE) );
chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
tree fntype = build_function_type( ptr_type_node, params );
tree gpy_eval_print_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
get_identifier("gpy_rr_eval_print"),
fntype );
tree restype = TREE_TYPE(gpy_eval_print_decl);
tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
restype );
DECL_CONTEXT( resdecl ) = gpy_eval_print_decl;
DECL_RESULT( gpy_eval_print_decl ) = resdecl;
DECL_EXTERNAL( gpy_eval_print_decl ) = 1;
TREE_PUBLIC( gpy_eval_print_decl ) = 1;
tree * vec = XNEWVEC( tree, n+2 );
printf("n = <%i>!!\n\n", n );
vec[0] = build_int_cst( integer_type_node, 1 );
vec[1] = build_int_cst( integer_type_node, n );
int idx = 2, idy = 0;
for (; idy<n; ++idy)
{
vec[idx] = args[idy];
idx++;
}
return build_call_expr_loc_array (UNKNOWN_LOCATION, gpy_eval_print_decl,
n+2, vec);
}
static
tree gpy_build_py_object_type (void)
{
tree object_state_struct_Type = make_node (RECORD_TYPE);
tree name = get_identifier("obj_t_ident");
tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
build_pointer_type(char_type_node));
DECL_CONTEXT(field) = object_state_struct_Type;
TYPE_FIELDS(object_state_struct_Type) = field;
tree last_field = field;
name = get_identifier("ref_count");
field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
DECL_CONTEXT(field) = object_state_struct_Type;
DECL_CHAIN(last_field) = field;
last_field = field;
name = get_identifier("self");
field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
build_pointer_type (void_type_node));
DECL_CONTEXT(field) = object_state_struct_Type;
DECL_CHAIN(last_field) = field;
last_field = field;
name = get_identifier("definition");
field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
build_pointer_type (void_type_node));
DECL_CONTEXT(field) = object_state_struct_Type;
DECL_CHAIN(last_field) = field;
last_field = field;
layout_type(object_state_struct_Type);
// Give the struct a name for better debugging info.
name = get_identifier("gpy_object_state_t");
tree object_state_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
object_state_struct_Type);
DECL_ARTIFICIAL(object_state_type_decl) = 1;
TYPE_NAME(object_state_struct_Type) = object_state_type_decl;
gpy_preserve_from_gc(object_state_type_decl);
rest_of_decl_compilation(object_state_type_decl, 1, 0);
debug_tree (object_state_type_decl);
tree object_state_ptr_type = build_pointer_type (object_state_struct_Type);
gpy_preserve_from_gc (object_state_ptr_type);
//....................
tree union_type__ = make_node (UNION_TYPE);
name = get_identifier ("object_state");
field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
object_state_ptr_type);
DECL_CONTEXT(field) = union_type__;
DECL_CHAIN(last_field) = field;
last_field = field;
name = get_identifier ("literal");
field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
ptr_type_node);
DECL_CONTEXT(field) = union_type__;
DECL_CHAIN(last_field) = field;
last_field = field;
layout_type (union_type__);
name = get_identifier("o");
tree union_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
union_type__);
DECL_ARTIFICIAL(union_type_decl) = 1;
TYPE_NAME(union_type__) = union_type_decl;
gpy_preserve_from_gc(union_type_decl);
rest_of_decl_compilation(union_type_decl, 1, 0);
//.........................
tree gpy_object_struct_Type = make_node (RECORD_TYPE);
name = get_identifier("type");
field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
DECL_CONTEXT(field) = gpy_object_struct_Type;
DECL_CHAIN(last_field) = field;
last_field = field;
name = get_identifier("o");
field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
union_type_decl);
DECL_CONTEXT(field) = gpy_object_struct_Type;
DECL_CHAIN(last_field) = field;
last_field = field;
layout_type (object_state_struct_Type);
name = get_identifier("gpy_object_t");
tree gpy_object_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
gpy_object_struct_Type);
DECL_ARTIFICIAL(gpy_object_type_decl) = 1;
TYPE_NAME(gpy_object_struct_Type) = name;
gpy_preserve_from_gc(gpy_object_type_decl);
rest_of_decl_compilation(gpy_object_type_decl, 1, 0);
return build_pointer_type (gpy_object_struct_Type);
}
void gpy_initilize_types (void)
{
gpy_builtin_types_vec = VEC_alloc(tree,gc,0);
tree const_char_type = build_qualified_type(unsigned_char_type_node,
TYPE_QUAL_CONST);
tree ctype = build_pointer_type(const_char_type);
VEC_safe_push (tree,gc,gpy_builtin_types_vec,
gpy_build_py_object_type ());
gpy_preserve_from_gc (gpy_object_type_ptr);
VEC_safe_push (tree,gc,gpy_builtin_types_vec,
build_pointer_type (gpy_object_type_ptr));
gpy_preserve_from_gc (gpy_object_type_ptr_ptr);
VEC_safe_push (tree,gc,gpy_builtin_types_vec,ctype);
gpy_preserve_from_gc (gpy_const_char_ptr);
}
|