quick save point before we rebuild the static anlysis code
[official-gcc.git] / gcc / python / py-stmt.c
blobe1df190cbbca27743941f81fac4f565172ebe736
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 "tm.h"
22 #include "opts.h"
23 #include "tree.h"
24 #include "tree-iterator.h"
25 #include "tree-pass.h"
26 #include "gimple.h"
27 #include "toplev.h"
28 #include "debug.h"
29 #include "options.h"
30 #include "flags.h"
31 #include "convert.h"
32 #include "diagnostic-core.h"
33 #include "langhooks.h"
34 #include "langhooks-def.h"
35 #include "target.h"
36 #include "cgraph.h"
38 #include <gmp.h>
39 #include <mpfr.h>
41 #include "vec.h"
42 #include "hashtab.h"
44 #include "gpython.h"
45 #include "py-dot-codes.def"
46 #include "py-dot.h"
47 #include "py-vec.h"
48 #include "py-tree.h"
49 #include "py-types.h"
50 #include "py-runtime.h"
52 static VEC(gpy_sym,gc) * gpy_decls;
54 static gpy_symbol_obj * gpy_stmt_process_AST_Align (gpy_symbol_obj **);
56 static tree gpy_stmt_pass_1 (VEC(gpy_sym,gc) * const);
57 static VEC(tree,gc) * gpy_stmt_pass_2 (VEC(gpy_sym,gc) * const, tree);
59 static tree gpy_stmt_create_module_type (VEC(tree,gc) * const, const char *);
61 static tree gpy_stmt_create_main_fndecl (tree, tree);
62 static tree gpy_stmt_create_module_initilizer (VEC(tree,gc) *, VEC(tree,gc) *,
63 const char *);
65 void gpy_stmt_process_decl (gpy_symbol_obj * const sym)
67 /* Push the declaration! */
68 VEC_safe_push( gpy_sym, gc, gpy_decls, sym );
69 debug("decl <%p> was pushed!\n", (void*)sym );
72 /**
73 * Fairly Confusing Function to read.
75 * example:
76 * >>> x = y = z = 2 + 2 + 2;
78 * --- Currently Yacc parses that expression into this Tree:
81 / \
82 + 2
85 / \
86 x =
87 / \
88 y =
89 / \
90 z 2
92 -- Is converted into the procedure:
94 1. z = 2 + 2 + 2;
95 2. y = z;
96 3. x = y;
98 -- Tree structure as so:
112 static
113 gpy_symbol_obj * gpy_stmt_process_AST_Align (gpy_symbol_obj ** sym)
115 gpy_symbol_obj *nn = NULL;
116 gpy_symbol_obj *retval = NULL;
117 if ((*sym)->next)
119 nn = (*sym)->next;
120 (*sym)->next = NULL;
122 retval = (*sym);
124 if( (*sym)->exp == OP_EXPRESS )
126 debug("Processing Expression AST!\n");
127 if( retval->type != OP_ASSIGN_EVAL )
129 gpy_symbol_obj *o= retval;
130 gpy_symbol_obj *h= NULL;
131 while( o )
133 if( o->op_a_t == TYPE_SYMBOL )
135 if( o->op_a.symbol_table->type == OP_ASSIGN_EVAL )
137 h = o;
138 break;
140 else
142 o = o->op_a.symbol_table;
145 else break;
147 if( h )
149 gpy_symbol_obj *head = h->op_a.symbol_table;
150 if( head->op_b.symbol_table->type == OP_ASSIGN_EVAL )
152 gpy_symbol_obj *t = head, *m = NULL;
153 while( t )
155 if( t->op_b_t == TYPE_SYMBOL )
157 if( t->op_b.symbol_table->type != OP_ASSIGN_EVAL )
159 m = t;
160 break;
162 else
164 t = t->op_b.symbol_table;
167 else break;
169 if( m )
171 h->op_a.symbol_table = m->op_b.symbol_table;
172 m->op_b.symbol_table = retval;
174 else
176 fatal_error("error processing the expression AST!\n");
179 else
181 h->op_a.symbol_table = head->op_b.symbol_table;
182 head->op_b.symbol_table = retval;
184 retval = head;
189 if( nn )
191 retval->next = nn;
193 (*sym) = retval;
195 return retval;
198 tree gpy_stmt_process_functor (gpy_symbol_obj * const functor, const char * prefix,
199 tree module, VEC(gpy_ctx_t,gc) * context)
201 tree fntype = build_function_type_list (gpy_object_type_ptr, build_pointer_type (module),
202 gpy_object_type_ptr_ptr, NULL_TREE);
203 tree fndecl = build_decl (functor->loc, FUNCTION_DECL,
204 get_identifier (functor->identifier), fntype);
205 DECL_EXTERNAL (fndecl) = 0;
206 TREE_PUBLIC (fndecl) = 1;
207 TREE_STATIC (fndecl) = 1;
209 tree declare_vars = NULL_TREE;
210 tree bind = NULL_TREE;
211 tree block = alloc_stmt_list ();
213 tree result_decl = build_decl (input_location,
214 RESULT_DECL, NULL_TREE, integer_type_node);
215 DECL_ARTIFICIAL (result_decl) = 1;
216 DECL_IGNORED_P (result_decl) = 1;
217 DECL_CONTEXT (result_decl) = fndecl;
218 DECL_RESULT (fndecl) = result_decl;
220 tree arglist = NULL_TREE;
221 tree typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
223 tree tmp = TREE_VALUE (typelist);
224 tree self = build_decl (functor->loc, PARM_DECL, get_identifier ("self"), tmp);
225 DECL_CONTEXT (self) = fndecl;
226 DECL_ARG_TYPE (self) = TREE_VALUE (typelist);
227 TREE_READONLY (self) = 1;
228 arglist = chainon (arglist, self);
230 typelist = TREE_CHAIN (typelist);
231 tmp = TREE_VALUE (typelist);
232 tree __args__ = build_decl (functor->loc, PARM_DECL, get_identifier ("__args__"), tmp);
233 DECL_CONTEXT (__args__) = fndecl;
234 DECL_ARG_TYPE (__args__) = TREE_VALUE (typelist);
235 TREE_READONLY (__args__) = 1;
236 arglist = chainon (arglist, __args__);
238 TREE_USED (__args__) = 1;
239 TREE_USED (self) = 1;
241 DECL_ARGUMENTS (fndecl) = arglist;
243 gpy_symbol_obj * o = functor->op_a.symbol_table;
245 int idx;
246 while (o)
248 /* looping over the gpy_symbol_obj block of function statements
249 and getting the respective tree's and creating the GENERIC block
251 VEC(tree,gc) * x = gpy_stmt_get_tree_2 (o, module, context);
252 tree xt;
253 for(idx = 0; VEC_iterate(tree,x,idx,xt); ++idx )
255 gcc_assert (xt);
256 append_to_statement_list (xt, &block);
258 o = o->next;
261 gpy_hash_tab_t * ctx = VEC_index (gpy_ctx_t, context,
262 VEC_length (gpy_ctx_t,context)-1);
263 int size = ctx->size;
264 gpy_hash_entry_t *array= ctx->array;
266 for (idx = 0; idx<size; ++idx)
268 if (array[idx].data)
270 tree x = (tree) array[idx].data;
271 gcc_assert (TREE_CODE(x) == VAR_DECL);
272 debug("got decl <%p>:<%s> within func <%s>!\n", (void*)x,
273 IDENTIFIER_POINTER (DECL_NAME(x)), functor->identifier);
274 TREE_CHAIN( x ) = declare_vars;
275 declare_vars = x;
279 tree bl = make_node(BLOCK);
280 BLOCK_SUPERCONTEXT(bl) = fndecl;
281 DECL_INITIAL(fndecl) = bl;
282 BLOCK_VARS(bl) = declare_vars;
283 TREE_USED(bl) = 1;
284 bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(bl),
285 NULL_TREE, bl);
286 TREE_SIDE_EFFECTS(bind) = 1;
288 BIND_EXPR_BODY(bind) = block;
289 block = bind;
290 DECL_SAVED_TREE(fndecl) = block;
292 gimplify_function_tree (fndecl);
294 // cgraph_add_new_function (fndecl, false);
295 // cgraph_finalize_function (fndecl, true);
297 return fndecl;
301 VEC(tree,gc) * gpy_stmt_get_tree_1 (gpy_symbol_obj * sym,
302 VEC(gpy_ctx_t,gc) * context)
304 VEC(tree,gc) * retval = NULL;
306 debug ("processing decl of type <0x%X> object <%p>\n",
307 sym->type, (void*) sym);
309 if( sym->exp == OP_EXPRESS )
311 sym = gpy_stmt_process_AST_Align (&sym);
312 retval = gpy_stmt_process_expression (sym, context);
314 else
316 switch (sym->type)
318 case STRUCTURE_FUNCTION_DEF:
319 break;
321 case KEY_PRINT:
322 break;
324 default:
325 fatal_error("unhandled symbol type <0x%x>\n", sym->type );
326 break;
330 return retval;
333 VEC(tree,gc) * gpy_stmt_get_tree_2 (gpy_symbol_obj * sym,
334 VEC(gpy_ctx_t,gc) * context)
336 VEC(tree,gc) * retval = NULL;
338 debug ("processing decl of type <0x%X> object <%p>\n",
339 sym->type, (void*) sym);
341 if( sym->exp == OP_EXPRESS )
343 sym = gpy_stmt_process_AST_Align (&sym);
344 retval = gpy_stmt_process_expression (sym, context);
346 else
348 switch (sym->type)
350 case KEY_PRINT:
352 retval = gpy_stmt_process_print (sym, context);
354 break;
356 default:
357 fatal_error("unhandled symbol type <0x%x>\n", sym->type );
358 break;
362 return retval;
365 tree gpy_stmt_create_main_fndecl (tree module, tree init_fndecl)
367 return NULL;
370 tree gpy_stmt_create_module_initilizer (VEC(tree,gc) * stmts,
371 VEC(tree,gc) * var_decls,
372 const char * prefix)
374 return NULL;
377 tree gpy_stmt_create_module_type (VEC(tree,gc) * const fields,
378 const char * ident)
380 tree module = make_node (RECORD_TYPE);
381 int idx = 0;
382 tree itx = NULL_TREE, field = NULL_TREE, last_field = NULL_TREE;
384 for (idx = 0; VEC_iterate(tree,fields,idx,itx); ++idx)
386 gcc_assert (TREE_CODE(itx) == VAR_DECL);
387 tree name = DECL_NAME(itx);
388 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
389 gpy_object_type_ptr);
390 DECL_CONTEXT(field) = module;
391 if (idx>0)
392 DECL_CHAIN(last_field) = field;
393 else
394 TYPE_FIELDS(module) = field;
395 last_field = field;
398 layout_type (module);
400 finish_builtin_struct (module, ident, field, NULL_TREE);
401 TYPE_NAME (module) = get_identifier (ident);
403 return module;
406 tree gpy_stmt_pass_1 (VEC(gpy_sym,gc) * const decls)
408 VEC (gpy_ctx_t, gc) * context = VEC_alloc (gpy_ctx_t,gc,0);
409 gpy_hash_tab_t ctx;
410 gpy_dd_hash_init_table (&ctx);
411 VEC_safe_push (gpy_ctx_t, gc, context, ctx);
413 int idx, idy;
414 gpy_symbol_obj * it = NULL;
415 tree retval = NULL_TREE, itx = NULL_TREE;
417 for (idx = 0; VEC_iterate (gpy_sym,decls,idx,it); ++idx)
419 VEC(tree,gc) * x = gpy_stmt_get_tree_1 (it,context);
420 gcc_assert (x);
423 // all we care about is this toplevel context which
424 // should now be populated with VAR_DECL's we can
425 // create our module out of.
427 VEC(tree,gc) * global_var_decls = VEC_alloc (tree,gc,0);
429 gpy_hash_tab_t * hctx = VEC_index (gpy_ctx_t, context,
430 VEC_length (gpy_ctx_t,context)-1);
431 int size = hctx->size;
432 gpy_hash_entry_t *array = hctx->array;
433 for (idx = 0; idx < size; ++idx)
435 if (array[idx].data)
437 tree x = (tree) array[idx].data;
438 gcc_assert (x);
439 if (TREE_CODE (x) == VAR_DECL)
441 debug("got decl <%p>:<%s>!\n", (void*)x,
442 IDENTIFIER_POINTER (DECL_NAME(x)));
443 VEC_safe_push (tree, gc, global_var_decls, x);
448 retval = gpy_stmt_create_module_type (global_var_decls, "main.main");
450 return retval;
453 VEC(tree,gc) * gpy_stmt_pass_2 (VEC(gpy_sym,gc) * const decls,
454 tree module)
456 VEC(tree,gc) * retval = VEC_alloc (tree,gc,0);
457 if (module == error_mark_node)
459 gcc_unreachable ();
460 VEC_safe_push (tree, gc, retval, error_mark_node);
462 else
464 VEC (gpy_ctx_t, gc) * context = VEC_alloc (gpy_ctx_t,gc,0);
466 int idx, idy;
467 gpy_symbol_obj * it = NULL;
468 tree itx = NULL_TREE;
470 tree fntype = build_function_type_list (void_type_node, build_pointer_type (module),
471 NULL_TREE);
472 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
473 get_identifier ("main.main.init"), fntype);
474 DECL_EXTERNAL (fndecl) = 0;
475 TREE_PUBLIC (fndecl) = 1;
476 TREE_STATIC (fndecl) = 1;
478 tree declare_vars = NULL_TREE;
479 tree bind = NULL_TREE;
480 tree block = alloc_stmt_list ();
482 tree result_decl = build_decl (input_location,
483 RESULT_DECL, NULL_TREE, integer_type_node);
484 DECL_ARTIFICIAL (result_decl) = 1;
485 DECL_IGNORED_P (result_decl) = 1;
486 DECL_CONTEXT (result_decl) = fndecl;
487 DECL_RESULT (fndecl) = result_decl;
489 tree arglist = NULL_TREE;
490 tree typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
492 tree tmp = TREE_VALUE (typelist);
493 tree self = build_decl (functor->loc, PARM_DECL, get_identifier ("self"), tmp);
494 DECL_CONTEXT (self) = fndecl;
495 DECL_ARG_TYPE (self) = TREE_VALUE (typelist);
496 TREE_READONLY (self) = 1;
497 arglist = chainon (arglist, self);
499 TREE_USED (__args__) = 1;
500 TREE_USED (self) = 1;
502 DECL_ARGUMENTS (fndecl) = arglist;
504 gpy_hash_tab_t ctx;
505 gpy_dd_hash_init_table (&ctx);
506 tree field = TYPE_FIELDS (module);
507 while (field)
509 gcc_assert (gpy_ctx_push_decl (build3(COMPONENT_REF, TREE_TYPE(field),
510 self, field, NULL_TREE),
511 IDENTIFIER_POINTER (DECL_NAME (field)),
512 ctx)
514 field = DECL_CHAIN (field);
516 VEC_safe_push (gpy_ctx_t, gc, context, ctx);
518 for (idx = 0; VEC_iterate (gpy_sym,decls,idx,it); ++idx)
520 switch (it->type)
522 case STRUCTURE_FUNCTION_DEF:
523 continue;
525 default:
527 VEC(tree,gc) * xt = gpy_stmt_get_tree_2 (it,context);
528 for (idy = 0; VEC_iterate (tree,xt,idy,itx); ++idy)
530 gcc_assert (itx);
531 gcc_assert (itx != error_mark_node);
532 append_to_statement_list (itx, &block);
535 break;
539 int size = ctx->size;
540 gpy_hash_entry_t *array= ctx->array;
542 for (idx = 0; idx<size; ++idx)
544 if (array[idx].data)
546 tree x = (tree) array[idx].data;
547 if (TREE_CODE(x) == VAR_DECL)
549 debug("got decl <%p>:<%s> within func <%s>!\n", (void*)x,
550 IDENTIFIER_POINTER (DECL_NAME(x)), functor->identifier);
551 TREE_CHAIN( x ) = declare_vars;
552 declare_vars = x;
557 tree bl = make_node(BLOCK);
558 BLOCK_SUPERCONTEXT(bl) = fndecl;
559 DECL_INITIAL(fndecl) = bl;
560 BLOCK_VARS(bl) = declare_vars;
561 TREE_USED(bl) = 1;
562 bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(bl),
563 NULL_TREE, bl);
564 TREE_SIDE_EFFECTS(bind) = 1;
566 BIND_EXPR_BODY(bind) = block;
567 block = bind;
568 DECL_SAVED_TREE(fndecl) = block;
570 gimplify_function_tree (fndecl);
572 cgraph_add_new_function (fndecl, false);
573 cgraph_finalize_function (fndecl, true);
575 VEC_pop (gpy_ctx_t, context);
576 gpy_dd_hash_init_table (&ctx);
578 for (idx = 0; VEC_iterate (gpy_sym,decls,idx,it); ++idx)
580 switch (it->type)
582 case STRUCTURE_FUNCTION_DEF:
586 break;
588 default:
589 break;
594 return retval;
598 * Things are quite complicated from here on and will change frequently
599 * We need to do a 1st pass over the code to generate our module.
601 Example:
603 x = 1
604 y = 2
605 def foo ():
606 return x + y
607 print foo ()
609 we need to generate out RECORD_TYPE with FIELDS x,y then pass again to generate
610 the rest of the code as our first pass will let us generate our TYPE so we know
611 how to access each variable in each context on the 2nd pass.
613 struct main.main {
614 gpy_object_t *x , *y;
617 gpy_object_t * main.foo (struct main.main * self, gpy_object_t ** __args)
619 T.1 = gpy_rr_bin_op (OP_ADDITION, self->x, self->y);
620 return T.1;
623 void main.init (struct main.main *self)
625 self->x = fold_int (1);
626 self->y = fold_int (2);
628 T.2 = fold_call (&main.foo, 1, self)
629 gpy_rr_print_stmt (1, T.2)
632 int main (int argc, char *argv[])
634 int retval;
636 init_runtime ();
638 struct main.main P;
639 main.init (&P);
641 cleanup_runtime ();
643 retval = 0;
644 return retval;
648 void gpy_stmt_write_globals (void)
650 tree main_module = gpy_stmt_pass_1 (gpy_decls);
651 VEC(tree,gc) * globals_decls_vec = gpy_stmt_pass_2 (gpy_decls,main_module);
653 int idx, idy, global_vec_len = VEC_length (tree, globals_decls_vec);
654 tree itx = NULL_TREE;
655 tree * global_vec = XNEWVEC (tree, global_vec_len);
656 FILE *tu_stream = dump_begin (TDI_tu, NULL);
658 idy = 0;
659 for (idx = 0; VEC_iterate(tree, globals_decls_vec, idx, itx); ++idx)
661 debug_tree (itx);
663 if (tu_stream)
664 dump_node(itx, 0, tu_stream);
666 global_vec[idy] = itx;
667 idy++;
669 if (tu_stream)
670 dump_end(TDI_tu, tu_stream);
672 debug("Finished processing!\n\n");
673 debug("global_vec len = <%i>!\n", global_vec_len);
675 wrapup_global_declarations (global_vec, global_vec_len);
676 check_global_declarations (global_vec, global_vec_len);
677 emit_debug_global_declarations (global_vec, global_vec_len);
678 cgraph_finalize_compilation_unit ();
680 debug("finished passing to middle-end!\n\n");