Stefan Behnel wrote:
> Dag Sverre Seljebotn wrote:
>> When it comes to FlattenInListTransform, I was thinking about something
>> along the lines of an "StatAsExprNode", which would be sort of the
>> inverse of ExprStatNode: It would contain a block which could contain
>> "ReturnFromStatAsExprNodeNode" (though a better name is needed :-))
>> which would yield the value of the statement list, evaluated as an
>> expression.
>
> I thought about this, too. The main problems with TempsBlockNode are that
> it a) requires the type of a temp variable before hand (which is bad for
> transforms that run before type analysis), and b) is a block statement, not
> an expression. Making it an ExprNode and moving the current use into a
> ExprStatNode would fix that.
I added a new node in UtilNodes that handles this. It's called
EvalWithTempExprNode - better name pending.
The idea is that you wrap an ExprNode in a EvalWithTempExprNode and replace
the expression node that is used more than once inside the ExprNode with a
ResultRefNode. The ResultRefNode will copy its type from the original
expression during type analysis, so that it doesn't need to know it at
creation time. The TempBlockExprNode then sets the result_code of the
ResultRefNode after evaluating its expression and before evaluating the
original ExprNode that needs the result. That way, evaluation of the
expression and its use are separated by the EvalWithTempExprNode which can
handle the temp allocation as required.
Example from FlattenInListTransform:
lhs = UtilNodes.ResultRefNode(node.operand1)
conds = []
for arg in args:
cond = ExprNodes.PrimaryCmpNode(
pos = node.pos,
operand1 = lhs,
operator = eq_or_neq,
operand2 = arg,
cascade = None)
conds.append(ExprNodes.TypecastNode(
pos = node.pos,
operand = cond,
type = PyrexTypes.c_bint_type))
def concat(left, right):
return ExprNodes.BoolBinopNode(
pos = node.pos,
operator = conjunction,
operand1 = left,
operand2 = right)
condition = reduce(concat, conds)
return UtilNodes.EvalWithTempExprNode(lhs, condition)
I wonder if this can also be useful for common subexpression elimination -
and if it makes sense to try such an optimisation in Cython...
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev