it compiles now
[official-gcc.git] / gcc / python / py-stmt-pass-mangr.c
blob546de26ef8e0107fcf6fcb6d444dac4146166a27
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.h"
46 #include "py-vec.h"
47 #include "py-tree.h"
49 static VEC(gpydot,gc) * gpy_decls;
51 typedef VEC(gpydot,gc) * (*DOT_stmt_pass__)(VEC(gpydot,gc) *);
52 static DOT_stmt_pass__ gpy_stmt_pass_mngr[] =
54 NULL /* sentinal */
57 /* Pushes each decl from the parser onto the current translation unit */
58 void gpy_stmt_process_decl (gpy_dot_tree_t * const dot)
60 /* Push the declaration! */
61 VEC_safe_push (gpydot, gc, gpy_decls, dot);
62 debug ("decl <%p> was pushed!\n", (void*)dot);
65 /**
66 * Fairly Confusing Function to read.
68 * example:
69 * >>> x = y = z = 2 + 2 + 2;
71 * --- Currently Yacc parses that expression into this Tree, which is just
72 side effect of the way we generate the gpy_dot_tree's the much higher level IR
73 which we bring down to GENERIC:
76 / \
77 + 2
80 / \
81 x =
82 / \
83 y =
84 / \
85 z 2
87 -- Is converted into the procedure:
89 1. z = 2 + 2 + 2;
90 2. y = z;
91 3. x = y;
93 -- Tree structure as so:
96 / \
97 x =
98 / \
99 y =
107 gpy_dot_tree_t * gpy_stmt_process_AST_Align (gpy_dot_tree_t ** d_dot)
109 gpy_dot_tree_t * retval = NULL_DOT;
110 gpy_dot_tree_t * nn = NULL_DOT;
111 gpy_dot_tree_t * dot = *d_dot;
113 if (DOT_CHAIN(dot))
115 nn = DOT_CHAIN(dot);
116 DOT_CHAIN(dot) = NULL_DOT;
118 retval = (*d_dot);
120 if (DOT_TYPE(retval) != D_MODIFY_EXPR)
122 gpy_dot_tree_t *o = retval;
123 gpy_dot_tree_t *h = NULL_DOT;
125 while (o != NULL_DOT)
127 if ((DOT_TYPE(o) != D_IDENTIFIER) ||
128 (DOT_TYPE(o) != D_PRIMITIVE))
130 if (DOT_lhs_T(o) == D_TD_DOT)
132 if (DOT_TYPE(DOT_lhs_TT(o)) == D_MODIFY_EXPR)
134 h = o;
135 break;
137 else
139 o = DOT_lhs_TT(o);
142 else break;
144 else break;
146 if (h)
148 gpy_dot_tree_t *head = DOT_lhs_TT(h);
149 if (DOT_TYPE(DOT_rhs_TT(head)) == D_MODIFY_EXPR)
151 gpy_dot_tree_t *t = head, *m = NULL_DOT;
152 while (t)
154 if ((DOT_TYPE(t) != D_IDENTIFIER) ||
155 (DOT_TYPE(t) != D_PRIMITIVE))
157 if (DOT_TYPE(DOT_rhs_TT(t)) != D_MODIFY_EXPR)
159 m = t;
160 break;
162 else
164 t = t->opb.t;
167 else break;
170 if( m )
172 DOT_lhs_TT(h) = DOT_rhs_TT(m);
173 DOT_rhs_TT(m) = retval;
175 else
176 fatal_error ("error processing the expression AST!\n");
178 else
180 DOT_lhs_TT(h) = DOT_rhs_TT(head);
181 DOT_rhs_TT(head) = retval;
183 retval = head;
187 if (nn)
188 DOT_CHAIN(retval) = nn;
189 (*d_dot) = retval;
191 return retval;
195 * Things are quite complicated from here on and will change frequently
196 * We need to do a 1st pass over the code to generate our module.
198 Example:
200 x = 1
201 y = 2
202 def foo ():
203 return x + y
204 print foo ()
206 we need to generate out RECORD_TYPE with FIELDS x,y then pass again to generate
207 the rest of the code as our first pass will let us generate our TYPE so we know
208 how to access each variable in each context on the 2nd pass.
210 struct main.main {
211 gpy_object_t *x , *y;
214 gpy_object_t * main.foo (struct main.main * self, gpy_object_t ** __args)
216 T.1 = gpy_rr_bin_op (OP_ADDITION, self->x, self->y);
217 return T.1;
220 void main.init (struct main.main *self)
222 self->x = fold_int (1);
223 self->y = fold_int (2);
225 T.2 = fold_call (&main.foo, 1, self)
226 gpy_rr_print_stmt (1, T.2)
229 int main (int argc, char *argv[])
231 int retval;
233 init_runtime ();
235 struct main.main P;
236 main.init (&P);
238 cleanup_runtime ();
240 retval = 0;
241 return retval;
245 void gpy_stmt_write_globals (void)
247 int idx = 0;
248 VEC(gpydot,gc) * dot_decls = gpy_decls;
249 while (gpy_stmt_pass_mngr[idx] != NULL)
251 DOT_stmt_pass__ x = gpy_stmt_pass_mngr[idx];
252 dot_decls = x(dot_decls);
253 idx++;
256 VEC(tree,gc) * module_types = gpy_stmt_pass_generate_types (dot_decls);
257 VEC(tree,gc) * dot2gen_trees = gpy_stmt_pass_lower (module_types, gpy_decls);
258 VEC(tree,gc) * globals = dot2gen_trees;
260 int global_vec_len = VEC_length (tree, globals);
261 tree * global_vec = XNEWVEC (tree, global_vec_len);
262 tree itx = NULL_TREE;
263 int idy = 0;
265 FILE *tu_stream = dump_begin (TDI_tu, NULL);
266 for (idx=0; VEC_iterate (tree,globals,idx,itx); ++idx)
268 debug_tree (itx);
270 if (tu_stream)
271 dump_node (itx, 0, tu_stream);
273 global_vec [idy] = itx;
274 idy++;
276 if (tu_stream)
277 dump_end(TDI_tu, tu_stream);
279 debug("Finished processing!\n\n");
281 debug("global_vec len = <%i>!\n", global_vec_len);
283 wrapup_global_declarations (global_vec, global_vec_len);
285 check_global_declarations (global_vec, global_vec_len);
286 emit_debug_global_declarations (global_vec, global_vec_len);
288 cgraph_finalize_compilation_unit ();
290 debug("finished passing to middle-end!\n\n");