1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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. */
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. */
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 ();
41 iterator_for_loop_start (idecl)
44 iterator_loop_prologue (idecl, 0, 0);
48 iterator_for_loop_end (idecl)
51 iterator_loop_epilogue (idecl, 0, 0);
55 iterator_for_loop_record (idecl)
58 add_ixpansion (idecl, 0, 0, 0, 0);
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. */
76 build_iterator_decl (id, limit)
79 tree type = integer_type_node, lim_decl;
81 tree start_node, limit_node, step_node;
86 limit_node = save_expr (limit);
87 SAVE_EXPR_CONTEXT (limit_node) = current_function_decl;
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);
101 ITERATOR RTL EXPANSIONS
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
115 iterator_expand (stmt)
118 tree iter_list = collect_iterators (stmt, NULL_TREE);
119 expand_stmt_with_iterators_1 (stmt, iter_list);
120 istack_sublevel_to_current ();
125 expand_stmt_with_iterators_1 (stmt, iter_list)
126 tree stmt, iter_list;
129 expand_expr_stmt (stmt);
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;
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);
140 /** Delete all inner expansions based on current_iterator **/
141 /** before adding the outer one. **/
143 delete_ixpansion (current_iterator);
144 add_ixpansion (current_iterator, p_start, p_end, e_start, e_end);
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. */
154 collect_iterators (exp, list)
157 if (exp == 0) return list;
159 switch (TREE_CODE (exp))
162 if (! ITERATOR_P (exp) || ITERATOR_BOUND_P (exp))
164 if (value_member (exp, list))
166 return tree_cons (NULL_TREE, exp, list);
171 for (tail = exp; tail; tail = TREE_CHAIN (tail))
172 list = collect_iterators (TREE_VALUE (tail), list);
176 /* we do not automatically iterate blocks -- one must */
177 /* use the FOR construct to do that */
183 switch (TREE_CODE_CLASS (code))
191 int num_args = tree_code_length[code];
194 for (i = 0; i < num_args; i++)
195 list = collect_iterators (TREE_OPERAND (exp, i), list);
202 /* Emit rtl for the start of a loop for iterator IDECL.
204 If necessary, create loop counter rtx and store it as DECL_RTL of IDECL.
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. */
211 iterator_loop_prologue (idecl, start_note, end_note)
213 rtx *start_note, *end_note;
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);
219 if (DECL_RTL (idecl) == 0)
223 *start_note = emit_note (0, NOTE_INSN_DELETED);
224 /* Initialize counter. */
225 expand_expr (build_modify_expr (idecl, NOP_EXPR, integer_zero_node),
228 expand_start_loop_continue_elsewhere (1);
230 ITERATOR_BOUND_P (idecl) = 1;
233 *end_note = emit_note (0, NOTE_INSN_DELETED);
236 /* Similar to the previous function, but for the end of the loop.
238 DECL_RTL is zeroed unless we are inside "({...})". The reason for that is
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.
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). */
254 iterator_loop_epilogue (idecl, start_note, end_note)
256 rtx *start_note, *end_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);
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;
275 *end_note = emit_note (0, NOTE_INSN_DELETED);
279 KEEPING TRACK OF EXPANSIONS
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.
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:
297 -- On "({" a new node is added to the stack by PUSH_ITERATOR_STACK.
298 The sublevel list is not changed at this point.
300 -- On "})" the list for the current level is appended to the sublevel
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. */
312 tree ixdecl; /* Iterator decl */
313 rtx ixprologue_start; /* First insn of epilogue. NULL means */
314 /* explicit (FOR) expansion*/
316 rtx ixepilogue_start;
318 struct ixpansion *next; /* Next in the list */
321 static struct obstack ixp_obstack;
323 static char *ixp_firstobj;
325 struct iter_stack_node
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 */
332 struct iter_stack_node *iter_stack;
334 struct iter_stack_node sublevel_ixpansions;
336 /** Return true if we are not currently inside a "({...})" construct */
339 top_level_ixpansion_p ()
341 return iter_stack == 0;
344 /* Given two chains of iter_stack_nodes,
345 append the nodes in X into Y. */
349 struct iter_stack_node *x, *y;
361 y->last->next = x->first;
368 #define ISN_ZERO(X) (X).first=(X).last=0
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. */
375 istack_sublevel_to_current ()
377 /* At the top level we can throw away sublevel's expansions **/
378 /* because there is nobody above us to ask for a cleanup **/
380 /** Merging with empty sublevel list is a no-op **/
381 if (sublevel_ixpansions.last)
382 isn_append (&sublevel_ixpansions, iter_stack);
385 obstack_free (&ixp_obstack, ixp_firstobj);
387 ISN_ZERO (sublevel_ixpansions);
390 /* Push a new node on the iter_stack, when we enter a ({...}). */
393 push_iterator_stack ()
395 struct iter_stack_node *new_top
396 = (struct iter_stack_node*)
397 obstack_alloc (&ixp_obstack, sizeof (struct iter_stack_node));
401 new_top->next = iter_stack;
402 iter_stack = new_top;
405 /* Pop iter_stack, moving the ixpansions in the node being popped
406 into sublevel_ixpansions. */
409 pop_iterator_stack ()
414 isn_append (iter_stack, &sublevel_ixpansions);
415 /** Pop current level node: */
416 iter_stack = iter_stack->next;
420 /* Record an iterator expansion ("ixpansion") for IDECL.
421 The remaining paramters are the notes in the loop entry
425 add_ixpansion (idecl, pro_start, pro_end, epi_start, epi_end)
427 rtx pro_start, pro_end, epi_start, epi_end;
429 struct ixpansion* newix;
431 /* Do nothing if we are not inside "({...})",
432 as in that case this expansion can't need subsequent RTL modification. */
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;
444 newix->next = iter_stack->first;
445 iter_stack->first = newix;
446 if (iter_stack->last == 0)
447 iter_stack->last = newix;
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. */
455 delete_ixpansion (idecl)
458 struct ixpansion* previx = 0, *ix;
460 for (ix = sublevel_ixpansions.first; ix; ix = ix->next)
461 if (ix->ixdecl == idecl)
463 /** zero means that this is a mark for FOR -- **/
464 /** we do not delete anything, just issue an error. **/
466 if (ix->ixprologue_start == 0)
467 error_with_decl (idecl,
468 "`for (%s)' appears within implicit iteration")
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 */
476 for (insn = NEXT_INSN (ix->ixprologue_start);
477 insn != ix->ixprologue_end;
478 insn = NEXT_INSN (insn))
480 for (insn = NEXT_INSN (ix->ixepilogue_start);
481 insn != ix->ixepilogue_end;
482 insn = NEXT_INSN (insn))
486 /* Delete this ixpansion from sublevel_ixpansions. */
488 previx->next = ix->next;
490 sublevel_ixpansions.first = ix->next;
491 if (sublevel_ixpansions.last == ix)
492 sublevel_ixpansions.last = previx;
499 We initialize iterators obstack once per file
504 gcc_obstack_init (&ixp_obstack);
505 ixp_firstobj = (char *) obstack_alloc (&ixp_obstack, 0);
508 #ifdef DEBUG_ITERATORS
511 The functions below are for use from source level debugger.
512 They print short forms of iterator lists and the iterator stack.
515 /* Print the name of the iterator D */
522 if (TREE_CODE (D) == VAR_DECL)
524 tree tname = DECL_NAME (D);
525 char *dname = IDENTIFIER_POINTER (tname);
526 fprintf (stderr, dname);
529 fprintf (stderr, "<<Not a Decl!!!>>");
532 fprintf (stderr, "<<NULL!!>>");
535 /* Print Iterator List -- names only */
542 for (current=head; current; current = next)
544 tree node = TREE_VALUE (current);
546 next = TREE_CHAIN (current);
547 if (next) fprintf (stderr, ",");
549 fprintf (stderr, "\n");
552 /* Print IXpansion List */
556 struct ixpansion *head;
558 struct ixpansion *current, *next;
559 fprintf (stderr, "> ");
561 fprintf (stderr, "(empty)");
563 for (current=head; current; current = next)
565 tree node = current->ixdecl;
567 next = current->next;
569 fprintf (stderr, ",");
571 fprintf (stderr, "\n");
575 /* Print Iterator Stack*/
580 struct iter_stack_node *stack_node;
582 fprintf (stderr, "--SubLevel: ");
583 pixl (sublevel_ixpansions.first);
584 fprintf (stderr, "--Stack:--\n");
585 for (stack_node = iter_stack;
587 stack_node = stack_node->next)
588 pixl (stack_node->first);