#include "config.h"
#include "system.h"
#include "tree.h"
#include "c-tree.h"
#include "flags.h"
#include "obstack.h"
#include "rtl.h"
#include "toplev.h"
#include "expr.h"
struct ixpansion
{
tree ixdecl;
rtx ixprologue_start;
rtx ixprologue_end;
rtx ixepilogue_start;
rtx ixepilogue_end;
struct ixpansion *next;
};
struct iter_stack_node
{
struct ixpansion *first;
struct ixpansion *last;
struct iter_stack_node *next;
};
struct iter_stack_node *iter_stack;
struct iter_stack_node sublevel_ixpansions;
static struct obstack ixp_obstack;
static char *ixp_firstobj;
static tree save_exprs;
static void expand_stmt_with_iterators_1 PROTO((tree, tree));
static tree collect_iterators PROTO((tree, tree));
static void iterator_loop_prologue PROTO((tree, rtx *, rtx *));
static void iterator_loop_epilogue PROTO((tree, rtx *, rtx *));
static int top_level_ixpansion_p PROTO((void));
static void isn_append PROTO((struct iter_stack_node *,
struct iter_stack_node *));
static void istack_sublevel_to_current PROTO((void));
static void add_ixpansion PROTO((tree, rtx, rtx, rtx, rtx));
static void delete_ixpansion PROTO((tree));
void
init_iterators ()
{
gcc_obstack_init (&ixp_obstack);
ixp_firstobj = (char *) obstack_alloc (&ixp_obstack, 0);
}
void
iterator_for_loop_start (idecl)
tree idecl;
{
ITERATOR_BOUND_P (idecl) = 1;
add_ixpansion (idecl, 0, 0, 0, 0);
iterator_loop_prologue (idecl, 0, 0);
}
void
iterator_for_loop_end (idecl)
tree idecl;
{
iterator_loop_epilogue (idecl, 0, 0);
ITERATOR_BOUND_P (idecl) = 0;
}
void
iterator_expand (stmt)
tree stmt;
{
tree iter_list;
save_exprs = NULL_TREE;
iter_list = collect_iterators (stmt, NULL_TREE);
expand_stmt_with_iterators_1 (stmt, iter_list);
istack_sublevel_to_current ();
}
static void
expand_stmt_with_iterators_1 (stmt, iter_list)
tree stmt, iter_list;
{
if (iter_list == 0)
expand_expr_stmt (stmt);
else
{
tree current_iterator = TREE_VALUE (iter_list);
tree iter_list_tail = TREE_CHAIN (iter_list);
rtx p_start, p_end, e_start, e_end;
iterator_loop_prologue (current_iterator, &p_start, &p_end);
expand_stmt_with_iterators_1 (stmt, iter_list_tail);
iterator_loop_epilogue (current_iterator, &e_start, &e_end);
delete_ixpansion (current_iterator);
add_ixpansion (current_iterator, p_start, p_end, e_start, e_end);
}
}
static tree
collect_iterators (exp, list)
tree exp, list;
{
if (exp == 0) return list;
switch (TREE_CODE (exp))
{
case VAR_DECL:
if (! ITERATOR_P (exp) || ITERATOR_BOUND_P (exp))
return list;
if (value_member (exp, list))
return list;
return tree_cons (NULL_TREE, exp, list);
case TREE_LIST:
{
tree tail;
for (tail = exp; tail; tail = TREE_CHAIN (tail))
list = collect_iterators (TREE_VALUE (tail), list);
return list;
}
case SAVE_EXPR:
if (value_member (exp, save_exprs))
return list;
save_exprs = tree_cons (NULL_TREE, exp, save_exprs);
return collect_iterators (TREE_OPERAND (exp, 0), list);
case BLOCK:
return list;
default:
switch (TREE_CODE_CLASS (TREE_CODE (exp)))
{
case '1':
return collect_iterators (TREE_OPERAND (exp, 0), list);
case '2':
case '<':
return collect_iterators (TREE_OPERAND (exp, 0),
collect_iterators (TREE_OPERAND (exp, 1),
list));
case 'e':
case 'r':
{
int num_args = tree_code_length[(int) TREE_CODE (exp)];
int i;
switch (TREE_CODE (exp))
{
case CALL_EXPR:
num_args = 2;
break;
case METHOD_CALL_EXPR:
num_args = 3;
break;
case WITH_CLEANUP_EXPR:
num_args = 1;
break;
case RTL_EXPR:
return list;
default:
break;
}
for (i = 0; i < num_args; i++)
list = collect_iterators (TREE_OPERAND (exp, i), list);
return list;
}
default:
return list;
}
}
}
static void
iterator_loop_prologue (idecl, start_note, end_note)
tree idecl;
rtx *start_note, *end_note;
{
tree expr;
expand_expr (DECL_INITIAL (idecl), const0_rtx, VOIDmode,
EXPAND_NORMAL);
if (DECL_RTL (idecl) == 0)
expand_decl (idecl);
if (start_note)
*start_note = emit_note (0, NOTE_INSN_DELETED);
expr = build (MODIFY_EXPR, TREE_TYPE (idecl), idecl, integer_zero_node);
TREE_SIDE_EFFECTS (expr) = 1;
expand_expr (expr, const0_rtx, VOIDmode, EXPAND_NORMAL);
expand_start_loop_continue_elsewhere (1);
ITERATOR_BOUND_P (idecl) = 1;
if (end_note)
*end_note = emit_note (0, NOTE_INSN_DELETED);
}
static void
iterator_loop_epilogue (idecl, start_note, end_note)
tree idecl;
rtx *start_note, *end_note;
{
tree test, incr;
if (start_note)
*start_note = emit_note (0, NOTE_INSN_DELETED);
expand_loop_continue_here ();
incr = build_binary_op (PLUS_EXPR, idecl, integer_one_node, 0);
incr = build (MODIFY_EXPR, TREE_TYPE (idecl), idecl, incr);
TREE_SIDE_EFFECTS (incr) = 1;
expand_expr (incr, const0_rtx, VOIDmode, EXPAND_NORMAL);
test = build_binary_op (LT_EXPR, idecl, DECL_INITIAL (idecl), 0);
expand_exit_loop_if_false (0, test);
expand_end_loop ();
ITERATOR_BOUND_P (idecl) = 0;
if (top_level_ixpansion_p () && ! TREE_STATIC (idecl))
DECL_RTL (idecl) = 0;
if (end_note)
*end_note = emit_note (0, NOTE_INSN_DELETED);
}
static int
top_level_ixpansion_p ()
{
return iter_stack == 0;
}
static void
isn_append (x, y)
struct iter_stack_node *x, *y;
{
if (x->first == 0)
return;
if (y->first == 0)
{
y->first = x->first;
y->last = x->last;
}
else
{
y->last->next = x->first;
y->last = x->last;
}
}
#define ISN_ZERO(X) (X).first=(X).last=0
static void
istack_sublevel_to_current ()
{
if (iter_stack != 0)
if (sublevel_ixpansions.last)
isn_append (&sublevel_ixpansions, iter_stack);
if (iter_stack == 0)
obstack_free (&ixp_obstack, ixp_firstobj);
ISN_ZERO (sublevel_ixpansions);
}
void
push_iterator_stack ()
{
struct iter_stack_node *new_top
= (struct iter_stack_node *)
obstack_alloc (&ixp_obstack, sizeof (struct iter_stack_node));
new_top->first = 0;
new_top->last = 0;
new_top->next = iter_stack;
iter_stack = new_top;
}
void
pop_iterator_stack ()
{
if (iter_stack == 0)
abort ();
isn_append (iter_stack, &sublevel_ixpansions);
iter_stack = iter_stack->next;
}
static void
add_ixpansion (idecl, pro_start, pro_end, epi_start, epi_end)
tree idecl;
rtx pro_start, pro_end, epi_start, epi_end;
{
struct ixpansion *newix;
if (iter_stack == 0)
return;
newix = (struct ixpansion *) obstack_alloc (&ixp_obstack,
sizeof (struct ixpansion));
newix->ixdecl = idecl;
newix->ixprologue_start = pro_start;
newix->ixprologue_end = pro_end;
newix->ixepilogue_start = epi_start;
newix->ixepilogue_end = epi_end;
newix->next = iter_stack->first;
iter_stack->first = newix;
if (iter_stack->last == 0)
iter_stack->last = newix;
}
static void
delete_ixpansion (idecl)
tree idecl;
{
struct ixpansion *previx = 0, *ix;
for (ix = sublevel_ixpansions.first; ix; ix = ix->next)
if (ix->ixdecl == idecl)
{
if (ix->ixprologue_start == 0)
error_with_decl (idecl,
"`for (%s)' appears within implicit iteration");
else
{
rtx insn;
for (insn = NEXT_INSN (ix->ixprologue_start);
insn != ix->ixprologue_end;
insn = NEXT_INSN (insn))
delete_insn (insn);
for (insn = NEXT_INSN (ix->ixepilogue_start);
insn != ix->ixepilogue_end;
insn = NEXT_INSN (insn))
delete_insn (insn);
}
if (previx)
previx->next = ix->next;
else
sublevel_ixpansions.first = ix->next;
if (sublevel_ixpansions.last == ix)
sublevel_ixpansions.last = previx;
}
else
previx = ix;
}
#ifdef DEBUG_ITERATORS
void
prdecl (d)
tree d;
{
if (d)
{
if (TREE_CODE (d) == VAR_DECL)
{
tree tname = DECL_NAME (d);
char *dname = IDENTIFIER_POINTER (tname);
fprintf (stderr, dname);
}
else
fprintf (stderr, "<<?>>");
}
else
fprintf (stderr, "<<0>>");
}
tree
pil (head)
tree head;
{
tree current, next;
for (current = head; current; current = next)
{
tree node = TREE_VALUE (current);
prdecl (node);
next = TREE_CHAIN (current);
if (next) fprintf (stderr, ",");
}
fprintf (stderr, "\n");
}
struct ixpansion *
pixl (head)
struct ixpansion *head;
{
struct ixpansion *current, *next;
fprintf (stderr, "> ");
if (head == 0)
fprintf (stderr, "(empty)");
for (current=head; current; current = next)
{
tree node = current->ixdecl;
prdecl (node);
next = current->next;
if (next)
fprintf (stderr, ",");
}
fprintf (stderr, "\n");
return head;
}
void
pis ()
{
struct iter_stack_node *stack_node;
fprintf (stderr, "--SubLevel: ");
pixl (sublevel_ixpansions.first);
fprintf (stderr, "--Stack:--\n");
for (stack_node = iter_stack;
stack_node;
stack_node = stack_node->next)
pixl (stack_node->first);
}
#endif