fixes to strings in generic usuage
[official-gcc.git] / gcc / python / py-runtime.c
blob8f4a8455346b2bd5d2ce046ff99a772d232b58a9
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-codes.def"
42 #include "py-dot.h"
43 #include "py-vec.h"
44 #include "py-tree.h"
45 #include "py-types.h"
46 #include "py-runtime.h"
48 tree gpy_builtin_get_init_call( void )
50 tree fntype = build_function_type( void_type_node, void_list_node );
51 tree gpy_rr_init = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
52 get_identifier("gpy_rr_init_runtime"),
53 fntype );
54 tree restype = TREE_TYPE(gpy_rr_init);
55 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
56 restype );
57 DECL_CONTEXT(resdecl) = gpy_rr_init;
58 DECL_RESULT(gpy_rr_init) = resdecl;
59 DECL_EXTERNAL( gpy_rr_init ) = 1;
60 TREE_PUBLIC( gpy_rr_init ) = 1;
62 return ( build_call_expr( gpy_rr_init, 0, NULL_TREE ) );
65 tree gpy_builtin_get_cleanup_final_call( void )
67 tree fntype = build_function_type( void_type_node, void_list_node );
68 tree gpy_rr_cleanup = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
69 get_identifier("gpy_rr_cleanup_final"),
70 fntype );
71 tree restype = TREE_TYPE( gpy_rr_cleanup );
72 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
73 restype );
74 DECL_CONTEXT(resdecl) = gpy_rr_cleanup;
75 DECL_RESULT(gpy_rr_cleanup) = resdecl;
76 DECL_EXTERNAL( gpy_rr_cleanup ) = 1;
77 TREE_PUBLIC( gpy_rr_cleanup ) = 1;
79 return ( build_call_expr( gpy_rr_cleanup, 0, NULL_TREE ) );
82 tree gpy_builtin_get_push_context_call( void )
84 tree fntype = build_function_type( void_type_node, void_list_node );
85 tree gpy_rr_push = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
86 get_identifier("gpy_rr_push_context"),
87 fntype );
88 tree restype = TREE_TYPE( gpy_rr_push );
89 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
90 restype );
91 DECL_CONTEXT(resdecl) = gpy_rr_push;
92 DECL_RESULT(gpy_rr_push) = resdecl;
93 DECL_EXTERNAL( gpy_rr_push ) = 1;
94 TREE_PUBLIC( gpy_rr_push ) = 1;
96 return ( build_call_expr( gpy_rr_push, 0, NULL_TREE ) );
99 tree gpy_builtin_get_pop_context_call( void )
101 tree fntype = build_function_type( void_type_node, void_list_node );
102 tree gpy_rr_pop = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
103 get_identifier("gpy_rr_pop_context"),
104 fntype );
105 tree restype = TREE_TYPE( gpy_rr_pop );
106 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
107 restype );
108 DECL_CONTEXT(resdecl) = gpy_rr_pop;
109 DECL_RESULT(gpy_rr_pop) = resdecl;
110 DECL_EXTERNAL( gpy_rr_pop ) = 1;
111 TREE_PUBLIC( gpy_rr_pop ) = 1;
113 return ( build_call_expr( gpy_rr_pop, 0, NULL_TREE ) );
116 tree gpy_builtin_get_fold_int_call( int val )
118 tree params = NULL_TREE;
120 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
121 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
123 tree fntype = build_function_type( ptr_type_node, params );
124 tree gpy_eval_expr_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
125 get_identifier("gpy_rr_fold_integer"),
126 fntype );
127 tree restype = TREE_TYPE(gpy_eval_expr_decl);
128 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
129 restype );
130 DECL_CONTEXT(resdecl) = gpy_eval_expr_decl;
131 DECL_RESULT(gpy_eval_expr_decl) = resdecl;
132 DECL_EXTERNAL( gpy_eval_expr_decl ) = 1;
133 TREE_PUBLIC( gpy_eval_expr_decl ) = 1;
135 return ( build_call_expr( gpy_eval_expr_decl, 1,
136 build_int_cst( integer_type_node,
137 val )
142 tree gpy_builtin_get_eval_expression_call( tree t1, tree t2, gpy_opcode_t op )
144 tree params = NULL_TREE;
146 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
147 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
148 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
149 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
151 tree fntype = build_function_type( ptr_type_node, params );
152 tree gpy_eval_expr_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
153 get_identifier("gpy_rr_eval_expression"),
154 fntype );
155 tree restype = TREE_TYPE(gpy_eval_expr_decl);
156 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
157 restype );
158 DECL_CONTEXT( resdecl ) = gpy_eval_expr_decl;
159 DECL_RESULT( gpy_eval_expr_decl ) = resdecl;
160 DECL_EXTERNAL( gpy_eval_expr_decl ) = 1;
161 TREE_PUBLIC( gpy_eval_expr_decl ) = 1;
163 return ( build_call_expr( gpy_eval_expr_decl, 3, t1, t2,
164 build_int_cst( integer_type_node, op ))
168 tree gpy_builtin_get_incr_ref_call( tree x )
170 tree params = NULL_TREE;
172 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
173 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
175 tree fntype = build_function_type( ptr_type_node, params );
176 tree gpy_incr_ref_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
177 get_identifier("gpy_rr_incr_ref_count"),
178 fntype );
179 tree restype = TREE_TYPE( gpy_incr_ref_decl );
180 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
181 restype );
182 DECL_CONTEXT( resdecl ) = gpy_incr_ref_decl;
183 DECL_RESULT( gpy_incr_ref_decl ) = resdecl;
184 DECL_EXTERNAL( gpy_incr_ref_decl ) = 1;
185 TREE_PUBLIC( gpy_incr_ref_decl ) = 1;
187 return ( build_call_expr( gpy_incr_ref_decl, 1, x ) );
190 tree gpy_builtin_get_decr_ref_call( tree x )
192 tree params = NULL_TREE;
194 chainon( params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE) );
195 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
197 tree fntype = build_function_type( ptr_type_node, params );
198 tree gpy_decr_ref_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
199 get_identifier("gpy_rr_decr_ref_count"),
200 fntype );
201 tree restype = TREE_TYPE( gpy_decr_ref_decl );
202 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
203 restype );
204 DECL_CONTEXT( resdecl ) = gpy_decr_ref_decl;
205 DECL_RESULT( gpy_decr_ref_decl ) = resdecl;
206 DECL_EXTERNAL( gpy_decr_ref_decl ) = 1;
207 TREE_PUBLIC( gpy_decr_ref_decl ) = 1;
209 return ( build_call_expr( gpy_decr_ref_decl, 1, x ) );
212 tree gpy_builtin_get_print_call( int n, tree * args )
214 tree params = NULL_TREE;
216 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
217 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
218 chainon( params, tree_cons (NULL_TREE, va_list_type_node, NULL_TREE) );
219 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
221 tree fntype = build_function_type( ptr_type_node, params );
222 tree gpy_eval_print_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
223 get_identifier("gpy_rr_eval_print"),
224 fntype );
225 tree restype = TREE_TYPE(gpy_eval_print_decl);
226 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
227 restype );
228 DECL_CONTEXT( resdecl ) = gpy_eval_print_decl;
229 DECL_RESULT( gpy_eval_print_decl ) = resdecl;
230 DECL_EXTERNAL( gpy_eval_print_decl ) = 1;
231 TREE_PUBLIC( gpy_eval_print_decl ) = 1;
233 tree * vec = XNEWVEC( tree, n+2 );
235 printf("n = <%i>!!\n\n", n );
237 vec[0] = build_int_cst( integer_type_node, 1 );
238 vec[1] = build_int_cst( integer_type_node, n );
240 int idx = 2, idy = 0;
241 for( ; idy<n; ++idy )
243 vec[idx] = args[idy];
244 idx++;
247 return ( build_call_expr_loc_array( UNKNOWN_LOCATION, gpy_eval_print_decl,
248 n+2, vec )
252 tree gpy_builtin_get_finalize_block_call( int n, tree * args )
254 tree params = NULL_TREE;
256 chainon( params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE) );
257 chainon( params, tree_cons (NULL_TREE, va_list_type_node, NULL_TREE) );
258 chainon( params, tree_cons (NULL_TREE, void_type_node, NULL_TREE) );
260 tree fntype = build_function_type( ptr_type_node, params );
261 tree gpy_finalize_block_decl = build_decl( UNKNOWN_LOCATION, FUNCTION_DECL,
262 get_identifier("gpy_rr_finalize_block_decls"),
263 fntype );
265 tree restype = TREE_TYPE( gpy_finalize_block_decl );
266 tree resdecl = build_decl( UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
267 restype );
269 DECL_CONTEXT( resdecl ) = gpy_finalize_block_decl;
270 DECL_RESULT( gpy_finalize_block_decl ) = resdecl;
271 DECL_EXTERNAL( gpy_finalize_block_decl ) = 1;
272 TREE_PUBLIC( gpy_finalize_block_decl ) = 1;
274 tree * vec = XNEWVEC (tree, n+1);
276 vec[0] = build_int_cst (integer_type_node, n);
278 int idx = 1, idy = 0;
279 for( ; idy<n; ++idy )
281 vec[idx] = args[idy];
282 idx++;
285 return ( build_call_expr_loc_array( UNKNOWN_LOCATION, gpy_finalize_block_decl,
286 n+1, vec ) );
289 tree gpy_builtin_get_set_decl_call (tree decl)
291 tree params = NULL_TREE;
293 chainon (params, tree_cons (NULL_TREE, gpy_const_char_ptr, NULL_TREE));
294 chainon (params, tree_cons (NULL_TREE, gpy_object_type_ptr, NULL_TREE));
295 chainon (params, tree_cons (NULL_TREE, void_type_node, NULL_TREE));
297 tree fntype = build_function_type (void_type_node, params);
298 tree gpy_rr_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
299 get_identifier("gpy_rr_set_decl_val"),
300 fntype);
302 tree restype = TREE_TYPE (gpy_rr_decl);
303 tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
304 restype);
306 DECL_CONTEXT (resdecl ) = gpy_rr_decl;
307 DECL_RESULT (gpy_rr_decl ) = resdecl;
308 DECL_EXTERNAL (gpy_rr_decl ) = 1;
309 TREE_PUBLIC (gpy_rr_decl ) = 1;
311 const char * c_ident = IDENTIFIER_POINTER(DECL_NAME(decl));
312 debug ("c_ident = <%s>!\n", c_ident);
314 tree str = build_string (strlen (c_ident), c_ident);
315 TREE_TYPE (str) = gpy_const_char_ptr;
316 tree ident = build_fold_addr_expr (str);
318 return build_call_expr (gpy_rr_decl, 2, ident, decl);
321 tree gpy_builtin_get_register_decl_call (tree decl)
323 tree params = NULL_TREE;
324 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
326 chainon (params, tree_cons (NULL_TREE, gpy_const_char_ptr, NULL_TREE));
327 chainon (params, tree_cons (NULL_TREE, void_type_node, NULL_TREE));
329 tree fntype = build_function_type (void_type_node, params);
330 tree gpy_rr_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
331 get_identifier("gpy_rr_register_decl"),
332 fntype);
334 tree restype = TREE_TYPE (gpy_rr_decl);
335 tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
336 restype);
338 DECL_CONTEXT (resdecl ) = gpy_rr_decl;
339 DECL_RESULT (gpy_rr_decl ) = resdecl;
340 DECL_EXTERNAL (gpy_rr_decl ) = 1;
341 TREE_PUBLIC (gpy_rr_decl ) = 1;
343 const char * c_ident = IDENTIFIER_POINTER(DECL_NAME(decl));
344 debug ("c_ident = <%s>!\n", c_ident);
346 tree str = build_string (strlen (c_ident), c_ident);
347 TREE_TYPE (str) = gpy_const_char_ptr;
348 tree ident = build_fold_addr_expr (str);
351 return build_call_expr (gpy_rr_decl, 1, ident);
354 tree gpy_builtin_get_register_callable_call (tree decl, int n)
356 tree params = NULL_TREE;
357 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
359 chainon (params, tree_cons (NULL_TREE, ptr_type_node, NULL_TREE));
360 chainon (params, tree_cons (NULL_TREE, integer_type_node, NULL_TREE));
361 chainon (params, tree_cons (NULL_TREE, gpy_const_char_ptr, NULL_TREE));
362 chainon (params, tree_cons (NULL_TREE, void_type_node, NULL_TREE));
364 tree fntype = build_function_type (void_type_node, params);
365 tree gpy_rr_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
366 get_identifier("gpy_rr_register_callable"),
367 fntype);
369 tree restype = TREE_TYPE (gpy_rr_decl);
370 tree resdecl = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE,
371 restype);
373 DECL_CONTEXT (resdecl ) = gpy_rr_decl;
374 DECL_RESULT (gpy_rr_decl ) = resdecl;
375 DECL_EXTERNAL (gpy_rr_decl ) = 1;
376 TREE_PUBLIC (gpy_rr_decl ) = 1;
378 const char * c_ident = IDENTIFIER_POINTER(DECL_NAME(decl));
379 debug ("c_ident = <%s>!\n", c_ident);
381 tree str = build_string (strlen (c_ident), c_ident);
382 TREE_TYPE (str) = gpy_const_char_ptr;
383 tree ident = build_fold_addr_expr (str);
384 TREE_TYPE (ident) = gpy_const_char_ptr;
386 return build_call_expr (gpy_rr_decl, 3,
387 build_fold_addr_expr (decl),
388 build_int_cst (integer_type_node, n),
389 ident);
393 tree gpy_builtin_get_fold_call_call (const char * ident, int n,
394 tree *args)
396 tree retval = NULL_TREE;
398 return retval;
401 tree gpy_builtin_get_eval_accessor_call (tree t1, tree t2)
403 fatal_error("Accessor's not implemented yet!\n");