OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / gcc / c-iterate.c
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* This file is part of the C front end.
22    It is responsible for implementing iterators,
23    both their declarations and the expansion of statements using them.  */
24
25 #include "config.h"
26 #include <stdio.h>
27 #include "tree.h"
28 #include "c-tree.h"
29 #include "flags.h"
30
31 static void expand_stmt_with_iterators_1 ();
32 static tree collect_iterators();
33 static void iterator_loop_prologue ();
34 static void iterator_loop_epilogue ();
35 static void add_ixpansion ();
36 static void delete_ixpansion();
37 static int top_level_ixpansion_p ();
38 static void istack_sublevel_to_current ();
39 \f
40 void
41 iterator_for_loop_start (idecl)
42      tree idecl;
43 {
44   iterator_loop_prologue (idecl, 0, 0);
45 }
46
47 void
48 iterator_for_loop_end (idecl)
49      tree idecl;
50 {
51   iterator_loop_epilogue (idecl, 0, 0);
52 }
53
54 void
55 iterator_for_loop_record (idecl)
56      tree idecl;
57 {
58   add_ixpansion (idecl, 0, 0, 0, 0);
59 }
60
61
62 /*
63                 ITERATOR DECLS
64
65 Iterators are  implemented  as integer decls  with a special  flag set
66 (rms's   idea).  This  makes  eliminates  the need   for  special type
67 checking.  The  flag  is accesed using   the  ITERATOR_P  macro.  Each
68 iterator's limit is saved as a  decl with a special  name. The decl is
69 initialized with the limit value -- this way we  get all the necessary
70 semantical processing for free by calling finish  decl. We might still
71 eliminate  that decl  later  -- it takes up  time and  space and, more
72 importantly, produces strange error  messages when  something is wrong
73 with the initializing expresison.  */
74
75 tree
76 build_iterator_decl (id, limit)
77     tree id, limit;
78 {
79   tree type = integer_type_node, lim_decl;
80   tree t1, t2, t3;
81   tree start_node, limit_node, step_node;
82   tree decl;
83     
84   if (limit)
85     {
86       limit_node = save_expr (limit);
87       SAVE_EXPR_CONTEXT (limit_node) = current_function_decl;
88     }
89   else
90     abort ();
91   lim_decl = build_limit_decl (id, limit_node);
92   push_obstacks_nochange ();
93   decl = build_decl (VAR_DECL, id, type);
94   ITERATOR_P (decl) = 1;
95   ITERATOR_LIMIT (decl) = lim_decl;
96   finish_decl (pushdecl (decl), 0, 0);
97   return decl;
98 }
99 \f
100 /*
101                 ITERATOR RTL EXPANSIONS
102
103    Expanding simple statements with iterators is  pretty straightforward:
104    collect (collect_iterators) the list  of  all "free" iterators  in the
105    statement and for each  of them add  a  special prologue before and an
106    epilogue after the expansion for  the statement. Iterator is "free" if
107    it has not been "bound" by a FOR operator. The rtx associated with the
108    iterator's  decl is used as  the loop counter.  Special processing  is
109    used  for "{(...)}" constructs:  each iterator expansion is registered
110    (by "add_ixpansion" function)  and inner expansions are superseded  by
111    outer ones. The cleanup of superseded expansions is done by  a call to
112    delete_ixpansion.  */
113
114 void
115 iterator_expand (stmt)
116     tree stmt;
117 {
118   tree iter_list = collect_iterators (stmt, NULL_TREE);
119   expand_stmt_with_iterators_1 (stmt, iter_list);
120   istack_sublevel_to_current ();
121 }
122
123
124 static void 
125 expand_stmt_with_iterators_1 (stmt, iter_list)
126      tree stmt, iter_list;
127 {
128   if (iter_list == 0)
129     expand_expr_stmt (stmt);
130   else
131     {
132       tree current_iterator = TREE_VALUE (iter_list);
133       tree iter_list_tail   = TREE_CHAIN (iter_list);
134       rtx p_start, p_end, e_start, e_end;
135
136       iterator_loop_prologue (current_iterator, &p_start, &p_end);
137       expand_stmt_with_iterators_1 (stmt, iter_list_tail);
138       iterator_loop_epilogue (current_iterator, &e_start, &e_end);
139
140       /** Delete all inner expansions based on current_iterator **/
141       /** before adding the outer one. **/
142
143       delete_ixpansion (current_iterator);
144       add_ixpansion (current_iterator, p_start, p_end, e_start, e_end);
145     }
146 }
147
148
149 /* Return a list containing all the free (i.e. not bound by "for"
150    statement or anaccumulator) iterators mentioned in EXP,
151    plus those in LIST.   Duplicates are avoided.  */
152
153 static tree
154 collect_iterators (exp, list)
155      tree exp, list;
156 {
157   if (exp == 0) return list;
158
159   switch (TREE_CODE (exp))
160     {
161     case VAR_DECL:
162       if (! ITERATOR_P (exp) || ITERATOR_BOUND_P (exp))
163         return list;
164       if (value_member (exp, list))
165         return list;
166       return tree_cons (NULL_TREE, exp, list);
167
168     case TREE_LIST:
169       {
170         tree tail;
171         for (tail = exp; tail; tail = TREE_CHAIN (tail))
172           list = collect_iterators (TREE_VALUE (tail), list);
173         return list;
174       }
175
176       /* we do not automatically iterate blocks -- one must */
177       /* use the FOR construct to do that */
178
179     case BLOCK:
180       return list;
181
182     default:
183       switch (TREE_CODE_CLASS (code))
184         {
185         case '1':
186         case '2':
187         case '<':
188         case 'e':
189         case 'r':
190           {
191             int num_args = tree_code_length[code];
192             int i;
193             the_list = (tree) 0;
194             for (i = 0; i < num_args; i++)
195               list = collect_iterators (TREE_OPERAND (exp, i), list);
196             return list;
197           }
198         }
199     }
200 }
201 \f
202 /* Emit rtl for the start of a loop for iterator IDECL.
203
204    If necessary, create loop counter rtx and store it as DECL_RTL of IDECL.
205
206    The prologue normally starts and ends with notes, which are returned
207    by this function in *START_NOTE and *END_NODE.
208    If START_NOTE and END_NODE are 0, we don't make those notes.  */
209
210 static void
211 iterator_loop_prologue (idecl, start_note, end_note)
212      tree idecl;
213      rtx *start_note, *end_note;
214 {
215   /* Force the save_expr in DECL_INITIAL to be calculated
216      if it hasn't been calculated yet.  */
217   expand_expr (DECL_INITIAL (idecl), 0, VOIDmode, 0);
218
219   if (DECL_RTL (idecl) == 0)
220     expand_decl (idecl);
221
222   if (start_note)
223     *start_note = emit_note (0, NOTE_INSN_DELETED);
224   /* Initialize counter.  */
225   expand_expr (build_modify_expr (idecl, NOP_EXPR, integer_zero_node),
226                0, VOIDmode, 0);
227
228   expand_start_loop_continue_elsewhere (1);
229
230   ITERATOR_BOUND_P (idecl) = 1;
231
232   if (end_note)
233     *end_note = emit_note (0, NOTE_INSN_DELETED);
234 }
235
236 /* Similar to the previous function, but for the end of the loop.
237
238    DECL_RTL is zeroed unless we are inside "({...})". The reason for that is
239    described below.
240
241    When we create two (or more)  loops based on the  same IDECL, and both
242    inside the same "({...})"  construct, we  must be prepared  to  delete
243    both of the loops  and create a single one  on the  level  above, i.e.
244    enclosing the "({...})". The new loop has to use  the same counter rtl
245    because the references to the iterator decl  (IDECL) have already been
246    expanded as references to the counter rtl.
247
248    It is incorrect to use the same counter reg in different functions,
249    and it is desirable to use different counters in disjoint loops
250    when we know there's no need to combine them
251    (because then they can get allocated separately).  */
252
253 static void
254 iterator_loop_epilogue (idecl, start_note, end_note)
255      tree idecl;
256      rtx *start_note, *end_note;
257 {
258   tree test, incr;
259
260   if (start_note)
261     *start_note = emit_note (0, NOTE_INSN_DELETED);
262   expand_loop_continue_here ();
263   incr = build_binary_op (PLUS_EXPR, idecl, integer_one_node, 0);
264   expand_expr (build_modify_expr (idecl, NOP_EXPR, incr));
265   test = build_binary_op (LT_EXPR, idecl, DECL_INITIAL (idecl), 0);
266   expand_exit_loop_if_false (0, test);
267   expand_end_loop ();
268
269   ITERATOR_BOUND_P (idecl) = 0;
270   /* we can reset rtl since there is not chance that this expansion */
271   /* would be superceded by a higher level one */
272   if (top_level_ixpansion_p ())
273     DECL_RTL (idecl) = 0;
274   if (end_note)
275     *end_note = emit_note (0, NOTE_INSN_DELETED);
276 }
277 \f
278 /*
279                 KEEPING TRACK OF EXPANSIONS
280
281    In order to clean out expansions corresponding to statements inside
282    "{(...)}" constructs we have to keep track of all expansions.  The
283    cleanup is needed when an automatic, or implicit, expansion on
284    iterator, say X, happens to a statement which contains a {(...)}
285    form with a statement already expanded on X.  In this case we have
286    to go back and cleanup the inner expansion.  This can be further
287    complicated by the fact that {(...)} can be nested.
288
289    To make this cleanup possible, we keep lists of all expansions, and
290    to make it work for nested constructs, we keep a stack.  The list at
291    the top of the stack (ITER_STACK.CURRENT_LEVEL) corresponds to the
292    currently parsed level.  All expansions of the levels below the
293    current one are kept in one list whose head is pointed to by
294    ITER_STACK.SUBLEVEL_FIRST (SUBLEVEL_LAST is there for making merges
295    easy).  The process works as follows:
296
297    -- On "({"  a new node is added to the stack by PUSH_ITERATOR_STACK.
298                The sublevel list is not changed at this point.
299
300    -- On "})" the list for the current level is appended to the sublevel
301               list. 
302
303    -- On ";"  sublevel lists are appended to the current level lists.
304               The reason is this: if they have not been superseded by the
305               expansion at the current level, they still might be
306               superseded later by the expansion on the higher level.
307               The levels do not have to distinguish levels below, so we
308               can merge the lists together.  */
309
310 struct  ixpansion
311 {
312   tree ixdecl;                  /* Iterator decl */
313   rtx  ixprologue_start;        /* First insn of epilogue. NULL means */
314   /* explicit (FOR) expansion*/
315   rtx  ixprologue_end;
316   rtx  ixepilogue_start;
317   rtx  ixepilogue_end;
318   struct ixpansion *next;       /* Next in the list */
319 };
320
321 static struct obstack ixp_obstack;
322
323 static char *ixp_firstobj;
324
325 struct iter_stack_node
326 {
327   struct ixpansion *first;      /* Head of list of ixpansions */
328   struct ixpansion *last;       /* Last node in list  of ixpansions */
329   struct iter_stack_node *next; /* Next level iterator stack node  */
330 };
331
332 struct iter_stack_node *iter_stack;
333
334 struct iter_stack_node sublevel_ixpansions;
335
336 /** Return true if we are not currently inside a "({...})" construct */
337
338 static int
339 top_level_ixpansion_p ()
340 {
341   return iter_stack == 0;
342 }
343
344 /* Given two chains of iter_stack_nodes,
345    append the nodes in X into Y.  */
346
347 static void
348 isn_append (x, y)
349      struct iter_stack_node *x, *y;
350 {
351   if (x->first == 0) 
352     return;
353
354   if (y->first == 0)
355     {
356       y->first = x->first;
357       y->last  = x->last;
358     }
359   else
360     {
361       y->last->next = x->first;
362       y->last = x->last;
363     }
364 }
365
366 /** Make X empty **/
367
368 #define ISN_ZERO(X) (X).first=(X).last=0
369 \f
370 /* Move the ixpansions in sublevel_ixpansions into the current
371    node on the iter_stack, or discard them if the iter_stack is empty.
372    We do this at the end of a statement.  */
373
374 static void
375 istack_sublevel_to_current ()
376 {
377   /* At the top level we can throw away sublevel's expansions  **/
378   /* because there is nobody above us to ask for a cleanup **/
379   if (iter_stack != 0)
380     /** Merging with empty sublevel list is a no-op **/
381     if (sublevel_ixpansions.last)
382       isn_append (&sublevel_ixpansions, iter_stack);
383
384   if (iter_stack == 0)
385     obstack_free (&ixp_obstack, ixp_firstobj);
386
387   ISN_ZERO (sublevel_ixpansions);
388 }
389
390 /* Push a new node on the iter_stack, when we enter a ({...}).  */
391
392 void
393 push_iterator_stack ()
394 {
395   struct iter_stack_node *new_top
396     = (struct iter_stack_node*) 
397       obstack_alloc (&ixp_obstack, sizeof (struct iter_stack_node));
398
399   new_top->first = 0;
400   new_top->last = 0;
401   new_top->next = iter_stack;
402   iter_stack = new_top;
403 }
404
405 /* Pop iter_stack, moving the ixpansions in the node being popped
406    into sublevel_ixpansions.  */
407
408 void
409 pop_iterator_stack ()
410 {
411   if (iter_stack == 0)
412     abort ();
413
414   isn_append (iter_stack, &sublevel_ixpansions);
415   /** Pop current level node: */
416   iter_stack = iter_stack->next;
417 }
418 \f
419
420 /* Record an iterator expansion ("ixpansion") for IDECL.
421    The remaining paramters are the notes in the loop entry
422    and exit rtl.  */
423
424 static void
425 add_ixpansion (idecl, pro_start, pro_end, epi_start, epi_end)
426      tree idecl;
427      rtx pro_start, pro_end, epi_start, epi_end;
428 {
429   struct ixpansion* newix;
430     
431   /* Do nothing if we are not inside "({...})",
432      as in that case this expansion can't need subsequent RTL modification.  */
433   if (iter_stack == 0)
434     return;
435
436   newix = (struct ixpansion*) obstack_alloc (&ixp_obstack,
437                                              sizeof (struct ixpansion));
438   newix->ixdecl = idecl;
439   newix->ixprologue_start = pro_start;
440   newix->ixprologue_end   = pro_end;
441   newix->ixepilogue_start = epi_start;
442   newix->ixepilogue_end   = epi_end;
443
444   newix->next = iter_stack->first;
445   iter_stack->first = newix;
446   if (iter_stack->last == 0)
447     iter_stack->last = newix;
448 }
449
450 /* Delete the RTL for all ixpansions for iterator IDECL
451    in our sublevels.  We do this when we make a larger
452    containing expansion for IDECL.  */
453
454 static void
455 delete_ixpansion (idecl)
456      tree idecl;
457 {
458   struct ixpansion* previx = 0, *ix;
459
460   for (ix = sublevel_ixpansions.first; ix; ix = ix->next)
461     if (ix->ixdecl == idecl)
462       {
463         /** zero means that this is a mark for FOR -- **/
464         /** we do not delete anything, just issue an error. **/
465
466         if (ix->ixprologue_start == 0)
467           error_with_decl (idecl,
468                            "`for (%s)' appears within implicit iteration")
469         else
470           {
471             rtx insn;
472             /* We delete all insns, including notes because leaving loop */
473             /* notes and barriers produced by iterator expansion would */
474             /* be misleading to other phases */
475
476             for (insn = NEXT_INSN (ix->ixprologue_start);
477                  insn != ix->ixprologue_end;
478                  insn = NEXT_INSN (insn)) 
479               delete_insn (insn);
480             for (insn = NEXT_INSN (ix->ixepilogue_start);
481                  insn != ix->ixepilogue_end;
482                  insn = NEXT_INSN (insn)) 
483               delete_insn (insn);
484           }
485
486         /* Delete this ixpansion from sublevel_ixpansions.  */
487         if (previx)
488           previx->next = ix->next;
489         else 
490           sublevel_ixpansions.first = ix->next;
491         if (sublevel_ixpansions.last == ix)
492           sublevel_ixpansions.last = previx;
493       }
494     else
495       previx = ix;
496 }
497 \f
498 /*
499 We initialize iterators obstack once per file
500 */
501
502 init_iterators ()
503 {
504   gcc_obstack_init (&ixp_obstack);
505   ixp_firstobj = (char *) obstack_alloc (&ixp_obstack, 0);
506 }
507
508 #ifdef DEBUG_ITERATORS
509
510 /*
511 The functions below are for use from source level debugger.
512 They print short forms of iterator lists and the iterator stack.
513 */
514
515 /* Print the name of the iterator D */
516 void
517 PRDECL (D)
518      tree D;
519 {
520   if (D)
521     {
522       if (TREE_CODE (D) == VAR_DECL)
523         {
524           tree tname = DECL_NAME (D);
525           char *dname = IDENTIFIER_POINTER (tname);
526           fprintf (stderr, dname);
527         }
528       else
529         fprintf (stderr, "<<Not a Decl!!!>>");
530     }
531   else
532     fprintf (stderr, "<<NULL!!>>");
533 }
534
535 /* Print Iterator List -- names only */
536
537 tree
538 pil (head)
539      tree head;
540 {
541   tree current, next;
542   for (current=head; current; current = next)
543     {
544       tree node = TREE_VALUE (current);
545       PRDECL (node);
546       next = TREE_CHAIN (current);
547       if (next) fprintf (stderr, ",");
548     }
549   fprintf (stderr, "\n");
550 }
551
552 /* Print IXpansion List */
553
554 struct ixpansion *
555 pixl (head)
556      struct ixpansion *head;
557 {
558   struct ixpansion *current, *next;
559   fprintf (stderr, "> ");
560   if (head == 0)
561     fprintf (stderr, "(empty)");
562         
563   for (current=head; current; current = next)
564     {
565       tree node = current->ixdecl;
566       PRDECL (node);
567       next = current->next;
568       if (next)
569         fprintf (stderr, ",");
570     }
571   fprintf (stderr, "\n");
572   return head;
573 }
574
575 /* Print Iterator Stack*/
576
577 void
578 pis ()
579 {
580   struct iter_stack_node *stack_node;
581
582   fprintf (stderr, "--SubLevel: ");
583   pixl (sublevel_ixpansions.first);
584   fprintf (stderr, "--Stack:--\n");
585   for (stack_node = iter_stack;
586        stack_node;
587        stack_node = stack_node->next)
588     pixl (stack_node->first);
589 }
590 #endif