it compiles now
[official-gcc.git] / gcc / python / py-builtins.c
blobb65e232fe380efeca569b6a4f857dec4d6078e26
1 /* This file is part of GCC.
3 GCC is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3, or (at your option) any later
6 version.
8 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
17 #include "config.h"
18 #include "system.h"
19 #include "ansidecl.h"
20 #include "coretypes.h"
21 #include "opts.h"
22 #include "tree.h"
23 #include "gimple.h"
24 #include "toplev.h"
25 #include "debug.h"
26 #include "options.h"
27 #include "flags.h"
28 #include "convert.h"
29 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
32 #include "target.h"
34 #include <gmp.h>
35 #include <mpfr.h>
37 #include "vec.h"
38 #include "hashtab.h"
40 #include "gpython.h"
41 #include "py-dot.h"
42 #include "py-vec.h"
43 #include "py-tree.h"
44 #include "py-builtins.h"
46 VEC(tree,gc) * gpy_builtin_types_vec;
47 static tree gpy_build_py_object_type (void);
49 tree gpy_builtin_get_init_call (void)
51 tree fntype = build_function_type (void_type_node, void_list_node);
52 tree gpy_rr_init = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
53 get_identifier("gpy_rr_init_runtime"),
54 fntype);
55 tree restype = TREE_TYPE (gpy_rr_init);
56 tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
57 restype);
58 DECL_CONTEXT (resdecl) = gpy_rr_init;
59 DECL_RESULT (gpy_rr_init) = resdecl;
60 DECL_EXTERNAL (gpy_rr_init) = 1;
61 TREE_PUBLIC (gpy_rr_init) = 1;
63 return build_call_expr (gpy_rr_init, 0, NULL_TREE);
66 tree gpy_builtin_get_cleanup_final_call (void)
68 tree fntype = build_function_type( void_type_node, void_list_node );
69 tree gpy_rr_cleanup = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
70 get_identifier("gpy_rr_cleanup_final"),
71 fntype );
72 tree restype = TREE_TYPE( gpy_rr_cleanup );
73 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
74 restype );
75 DECL_CONTEXT(resdecl) = gpy_rr_cleanup;
76 DECL_RESULT(gpy_rr_cleanup) = resdecl;
77 DECL_EXTERNAL( gpy_rr_cleanup ) = 1;
78 TREE_PUBLIC( gpy_rr_cleanup ) = 1;
80 return build_call_expr( gpy_rr_cleanup, 0, NULL_TREE );
83 tree gpy_builtin_get_fold_int_call( int val )
85 tree params = NULL_TREE;
87 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
88 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
90 tree fntype = build_function_type( ptr_type_node, params );
91 tree gpy_eval_expr_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
92 get_identifier("gpy_rr_fold_integer"),
93 fntype );
94 tree restype = TREE_TYPE(gpy_eval_expr_decl);
95 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
96 restype );
97 DECL_CONTEXT(resdecl) = gpy_eval_expr_decl;
98 DECL_RESULT(gpy_eval_expr_decl) = resdecl;
99 DECL_EXTERNAL( gpy_eval_expr_decl ) = 1;
100 TREE_PUBLIC( gpy_eval_expr_decl ) = 1;
102 return build_call_expr (gpy_eval_expr_decl, 1,
103 build_int_cst (integer_type_node,
104 val)
108 tree gpy_builtin_get_eval_expression_call (tree t1, tree t2, opcode_t op)
110 tree params = NULL_TREE;
112 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
113 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
114 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
115 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
117 tree fntype = build_function_type( ptr_type_node, params );
118 tree gpy_eval_expr_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
119 get_identifier("gpy_rr_eval_expression"),
120 fntype );
121 tree restype = TREE_TYPE(gpy_eval_expr_decl);
122 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
123 restype );
124 DECL_CONTEXT( resdecl ) = gpy_eval_expr_decl;
125 DECL_RESULT( gpy_eval_expr_decl ) = resdecl;
126 DECL_EXTERNAL( gpy_eval_expr_decl ) = 1;
127 TREE_PUBLIC( gpy_eval_expr_decl ) = 1;
129 return build_call_expr (gpy_eval_expr_decl, 3, t1, t2,
130 build_int_cst (integer_type_node, op));
133 tree gpy_builtin_get_incr_ref_call (tree x)
135 tree params = NULL_TREE;
137 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
138 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
140 tree fntype = build_function_type( ptr_type_node, params );
141 tree gpy_incr_ref_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
142 get_identifier("gpy_rr_incr_ref_count"),
143 fntype );
144 tree restype = TREE_TYPE( gpy_incr_ref_decl );
145 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
146 restype );
147 DECL_CONTEXT( resdecl ) = gpy_incr_ref_decl;
148 DECL_RESULT( gpy_incr_ref_decl ) = resdecl;
149 DECL_EXTERNAL( gpy_incr_ref_decl ) = 1;
150 TREE_PUBLIC( gpy_incr_ref_decl ) = 1;
152 return build_call_expr (gpy_incr_ref_decl, 1, x);
155 tree gpy_builtin_get_decr_ref_call (tree x)
157 tree params = NULL_TREE;
159 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
160 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
162 tree fntype = build_function_type( ptr_type_node, params );
163 tree gpy_decr_ref_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
164 get_identifier("gpy_rr_decr_ref_count"),
165 fntype );
166 tree restype = TREE_TYPE( gpy_decr_ref_decl );
167 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
168 restype );
169 DECL_CONTEXT( resdecl ) = gpy_decr_ref_decl;
170 DECL_RESULT( gpy_decr_ref_decl ) = resdecl;
171 DECL_EXTERNAL( gpy_decr_ref_decl ) = 1;
172 TREE_PUBLIC( gpy_decr_ref_decl ) = 1;
174 return build_call_expr (gpy_decr_ref_decl, 1, x);
177 tree gpy_builtin_get_print_call (int n, tree * args)
179 tree params = NULL_TREE;
181 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
182 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
183 chainon( params, tree_cons (NULL_TREE, va_list_type_node, NULL_TREE) );
184 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
186 tree fntype = build_function_type( ptr_type_node, params );
187 tree gpy_eval_print_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
188 get_identifier("gpy_rr_eval_print"),
189 fntype );
190 tree restype = TREE_TYPE(gpy_eval_print_decl);
191 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
192 restype );
193 DECL_CONTEXT( resdecl ) = gpy_eval_print_decl;
194 DECL_RESULT( gpy_eval_print_decl ) = resdecl;
195 DECL_EXTERNAL( gpy_eval_print_decl ) = 1;
196 TREE_PUBLIC( gpy_eval_print_decl ) = 1;
198 tree * vec = XNEWVEC( tree, n+2 );
200 printf("n = <%i>!!\n\n", n );
202 vec[0] = build_int_cst( integer_type_node, 1 );
203 vec[1] = build_int_cst( integer_type_node, n );
205 int idx = 2, idy = 0;
206 for (; idy<n; ++idy)
208 vec[idx] = args[idy];
209 idx++;
212 return build_call_expr_loc_array (UNKNOWN_LOCATION, gpy_eval_print_decl,
213 n+2, vec);
216 static
217 tree gpy_build_py_object_type (void)
219 tree object_state_struct_Type = make_node (RECORD_TYPE);
221 tree name = get_identifier("obj_t_ident");
222 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
223 build_pointer_type(char_type_node));
224 DECL_CONTEXT(field) = object_state_struct_Type;
225 TYPE_FIELDS(object_state_struct_Type) = field;
226 tree last_field = field;
228 name = get_identifier("ref_count");
229 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
230 DECL_CONTEXT(field) = object_state_struct_Type;
231 DECL_CHAIN(last_field) = field;
232 last_field = field;
234 name = get_identifier("self");
235 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
236 build_pointer_type (void_type_node));
237 DECL_CONTEXT(field) = object_state_struct_Type;
238 DECL_CHAIN(last_field) = field;
239 last_field = field;
241 name = get_identifier("definition");
242 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
243 build_pointer_type (void_type_node));
244 DECL_CONTEXT(field) = object_state_struct_Type;
245 DECL_CHAIN(last_field) = field;
246 last_field = field;
248 layout_type(object_state_struct_Type);
250 // Give the struct a name for better debugging info.
251 name = get_identifier("gpy_object_state_t");
252 tree object_state_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
253 object_state_struct_Type);
254 DECL_ARTIFICIAL(object_state_type_decl) = 1;
255 TYPE_NAME(object_state_struct_Type) = object_state_type_decl;
256 gpy_preserve_from_gc(object_state_type_decl);
257 rest_of_decl_compilation(object_state_type_decl, 1, 0);
259 debug_tree (object_state_type_decl);
260 tree object_state_ptr_type = build_pointer_type (object_state_struct_Type);
261 gpy_preserve_from_gc (object_state_ptr_type);
263 //....................
265 tree union_type__ = make_node (UNION_TYPE);
267 name = get_identifier ("object_state");
268 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
269 object_state_ptr_type);
270 DECL_CONTEXT(field) = union_type__;
271 DECL_CHAIN(last_field) = field;
272 last_field = field;
274 name = get_identifier ("literal");
275 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
276 ptr_type_node);
277 DECL_CONTEXT(field) = union_type__;
278 DECL_CHAIN(last_field) = field;
279 last_field = field;
281 layout_type (union_type__);
283 name = get_identifier("o");
284 tree union_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
285 union_type__);
286 DECL_ARTIFICIAL(union_type_decl) = 1;
287 TYPE_NAME(union_type__) = union_type_decl;
288 gpy_preserve_from_gc(union_type_decl);
289 rest_of_decl_compilation(union_type_decl, 1, 0);
291 //.........................
293 tree gpy_object_struct_Type = make_node (RECORD_TYPE);
295 name = get_identifier("type");
296 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
297 DECL_CONTEXT(field) = gpy_object_struct_Type;
298 DECL_CHAIN(last_field) = field;
299 last_field = field;
301 name = get_identifier("o");
302 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
303 union_type_decl);
304 DECL_CONTEXT(field) = gpy_object_struct_Type;
305 DECL_CHAIN(last_field) = field;
306 last_field = field;
308 layout_type (object_state_struct_Type);
310 name = get_identifier("gpy_object_t");
311 tree gpy_object_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
312 gpy_object_struct_Type);
313 DECL_ARTIFICIAL(gpy_object_type_decl) = 1;
314 TYPE_NAME(gpy_object_struct_Type) = name;
315 gpy_preserve_from_gc(gpy_object_type_decl);
316 rest_of_decl_compilation(gpy_object_type_decl, 1, 0);
318 return build_pointer_type (gpy_object_struct_Type);
321 void gpy_initilize_types (void)
323 gpy_builtin_types_vec = VEC_alloc(tree,gc,0);
325 tree const_char_type = build_qualified_type(unsigned_char_type_node,
326 TYPE_QUAL_CONST);
327 tree ctype = build_pointer_type(const_char_type);
329 VEC_safe_push (tree,gc,gpy_builtin_types_vec,
330 gpy_build_py_object_type ());
331 gpy_preserve_from_gc (gpy_object_type_ptr);
333 VEC_safe_push (tree,gc,gpy_builtin_types_vec,
334 build_pointer_type (gpy_object_type_ptr));
335 gpy_preserve_from_gc (gpy_object_type_ptr_ptr);
337 VEC_safe_push (tree,gc,gpy_builtin_types_vec,ctype);
338 gpy_preserve_from_gc (gpy_const_char_ptr);