This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [tree-ssa] Make tsi insert iterators handle chains of COMPOUND_EXPRs
Hello,
> > and we don't want to support making bugs in code, do we? IMHO it would
> > be better if the meaning of flags was consistent (i.e. CHAIN_END -- I
> > want to continue adding other statements in this direction, NEW_STMT --
> > I want to process the newly added members).
>
> TSI_NEW_STMT means return an iterator which points to the new stmt which
> was inserted. There is some abiguity about what this means in the
> context of a list of stmt's so I would suggest that if we go to a new
> function for inserting a chain of stmt's, that TSI_NEW_STMT is no longer
> a valid enumeration for that function, and that we have something like
> TSI_SAME_STMT, TSI_HEAD_STMT, TSI_END_STMT valid for the new
> function.
>
> That removes any ambiguity, and makes it pretty clear as to whats going
> on I think.
here is the patch. I just could not resist adding TSI_CONTINUE_LINKING,
with obvious meaning; hope you don't mind.
Zdenek
* tree-iterator.h (tsi_iterator_update): Added TSI_CHAIN_START,
TSI_CHAIN_END, TSI_CONTINUE_LINKING and comments.
(tsi_link_chain_before, tsi_link_chain_after): Declare.
* tree.c (tsi_link_chain_before, tsi_link_chain_after): New.
(tsi_link_before, tsi_link_after): Handle new TSI_... positions.
Index: tree.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree.c,v
retrieving revision 1.263.2.49
diff -c -3 -p -r1.263.2.49 tree.c
*** tree.c 27 Aug 2003 04:31:37 -0000 1.263.2.49
--- tree.c 30 Aug 2003 08:44:09 -0000
*************** tsi_link_before (tree_stmt_iterator *i,
*** 5115,5120 ****
--- 5115,5125 ----
{
tree ce;
+ if (mode == TSI_CHAIN_START || mode == TSI_CHAIN_END)
+ abort ();
+ if (mode == TSI_CONTINUE_LINKING)
+ mode = TSI_NEW_STMT;
+
/* Build a new CE which points to the current node. */
ce = build (COMPOUND_EXPR, void_type_node, t, *(i->tp));
*************** tsi_link_before (tree_stmt_iterator *i,
*** 5126,5132 ****
node, which is the current stmt again. */
if (mode == TSI_SAME_STMT)
i->tp = &(TREE_OPERAND (*(i->tp), 1));
-
}
/* Links a stmt after the current stmt. */
--- 5131,5136 ----
*************** tsi_link_after (tree_stmt_iterator *i, t
*** 5137,5142 ****
--- 5141,5151 ----
tree ce;
tree next;
+ if (mode == TSI_CHAIN_START || mode == TSI_CHAIN_END)
+ abort ();
+ if (mode == TSI_CONTINUE_LINKING)
+ mode = TSI_NEW_STMT;
+
/* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
if (TREE_CODE (*(i->tp)) != COMPOUND_EXPR)
{
*************** tsi_link_after (tree_stmt_iterator *i, t
*** 5165,5174 ****
if (mode == TSI_NEW_STMT)
i->tp = &(TREE_OPERAND (*(i->tp), 1));
}
!
}
/* Remove a stmt from the tree list. The iterator is updated to point to
the next stmt. */
--- 5174,5266 ----
if (mode == TSI_NEW_STMT)
i->tp = &(TREE_OPERAND (*(i->tp), 1));
}
+ }
+
+ /* Links a chain of statements T before the current stmt. */
+
+ void
+ tsi_link_chain_before (tree_stmt_iterator *i, tree t,
+ enum tsi_iterator_update mode)
+ {
+ tree *last;
+
+ if (mode == TSI_NEW_STMT)
+ abort ();
+ if (mode == TSI_CONTINUE_LINKING)
+ mode = TSI_CHAIN_START;
+
+ for (last = &t;
+ TREE_CODE (*last) == COMPOUND_EXPR;
+ last = &TREE_OPERAND (*last, 1))
+ continue;
+
+ /* Build a new CE which points to the current node. */
+ *last = build (COMPOUND_EXPR, void_type_node, *last, *(i->tp));
+
+ /* Make the parent pointer point to this new node. At this point, the
+ iterator will be pointing to the new node we just inserted. */
+ *(i->tp) = t;
! /* Update the iterator to points to the address of the next ptr in our new
! node, which is the current stmt again. */
! if (mode == TSI_SAME_STMT)
! i->tp = &(TREE_OPERAND (*last, 1));
! else if (mode == TSI_CHAIN_END)
! i->tp = last != &t ? last : i->tp;
}
+ /* Links a chain of statements T after the current stmt. */
+
+ void
+ tsi_link_chain_after (tree_stmt_iterator *i, tree t,
+ enum tsi_iterator_update mode)
+ {
+ tree ce;
+ tree next;
+ tree *last;
+
+ if (mode == TSI_NEW_STMT)
+ abort ();
+ if (mode == TSI_CONTINUE_LINKING)
+ mode = TSI_CHAIN_END;
+
+ /* If this node isnt a COMPUND_EXPR, we need to insert a CE node. */
+ if (TREE_CODE (*(i->tp)) != COMPOUND_EXPR)
+ {
+ /* Create a new node with the current stmt on the left, and the new
+ stmt on the right. */
+ ce = build (COMPOUND_EXPR, void_type_node, *(i->tp), t);
+
+ /* Update link to point to this CE node. */
+ *(i->tp) = ce;
+
+ /* Change new iterator to point to the new stmt. */
+ if (mode == TSI_CHAIN_START)
+ i->tp = &(TREE_OPERAND (ce, 1));
+ else if (mode == TSI_CHAIN_END)
+ while (TREE_CODE (*i->tp) == COMPOUND_EXPR)
+ i->tp = &(TREE_OPERAND (*i->tp, 1));
+ }
+ else
+ {
+ for (last = &t;
+ TREE_CODE (*last) == COMPOUND_EXPR;
+ last = &TREE_OPERAND (*last, 1))
+ continue;
+
+ next = TREE_OPERAND (*(i->tp), 1);
+
+ /* Create a new node with the same 'next' link as the current one. */
+ *last = build (COMPOUND_EXPR, void_type_node, *last, next);
+ TREE_OPERAND (*(i->tp), 1) = t;
+
+ /* Update the iterator to the new stmt. */
+ if (mode == TSI_CHAIN_START)
+ i->tp = &(TREE_OPERAND (*(i->tp), 1));
+ else if (mode == TSI_CHAIN_END)
+ i->tp = last != &t ? last : &(TREE_OPERAND (*(i->tp), 1));
+ }
+ }
/* Remove a stmt from the tree list. The iterator is updated to point to
the next stmt. */
Index: tree-iterator.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-iterator.h,v
retrieving revision 1.1.2.7
diff -c -3 -p -r1.1.2.7 tree-iterator.h
*** tree-iterator.h 15 Jul 2003 03:13:40 -0000 1.1.2.7
--- tree-iterator.h 30 Aug 2003 08:44:09 -0000
*************** typedef tree tree_stmt_anchor;
*** 148,159 ****
enum tsi_iterator_update
{
! TSI_NEW_STMT,
! TSI_SAME_STMT
};
void tsi_link_before (tree_stmt_iterator *, tree, enum tsi_iterator_update);
void tsi_link_after (tree_stmt_iterator *, tree, enum tsi_iterator_update);
void tsi_delink (tree_stmt_iterator *);
tree_stmt_iterator tsi_new_stmt_list (tree, tree_stmt_anchor *);
tree_stmt_iterator tsi_stmt_list_head (tree_stmt_anchor);
--- 148,169 ----
enum tsi_iterator_update
{
! TSI_NEW_STMT, /* Leave the iterator at the same statement. */
! TSI_SAME_STMT, /* Only valid when single statement is added, move
! iterator to it. */
! TSI_CHAIN_START, /* Only valid when chain of statements is added, move
! iterator to the first statement in the chain. */
! TSI_CHAIN_END, /* Only valid when chain of statements is added, move
! iterator to the last statement in the chain. */
! TSI_CONTINUE_LINKING /* Move iterator to whatever position is suitable for
! linking other statements/chains of statements in
! the same direction. */
};
void tsi_link_before (tree_stmt_iterator *, tree, enum tsi_iterator_update);
void tsi_link_after (tree_stmt_iterator *, tree, enum tsi_iterator_update);
+ void tsi_link_chain_before (tree_stmt_iterator *, tree, enum tsi_iterator_update);
+ void tsi_link_chain_after (tree_stmt_iterator *, tree, enum tsi_iterator_update);
void tsi_delink (tree_stmt_iterator *);
tree_stmt_iterator tsi_new_stmt_list (tree, tree_stmt_anchor *);
tree_stmt_iterator tsi_stmt_list_head (tree_stmt_anchor);