digitalmars.D - Order of evaluation vs. sequence points
- Jens Mueller (37/37) Aug 08 2011 Hi,
- bearophile (6/7) Aug 08 2011 I think to simplify the implementation, because I think it comes from th...
- Kagamin (2/4) Aug 08 2011 Order of evaluation matters when subexpressions contain sequence points.
- Jens Mueller (12/18) Aug 09 2011 Because it moves sequence points around. Right?
Hi, at http://www.d-programming-language.org/expression.html the following expressions are said to be illegal: i = i++; c = a + (a = b); The documentation says they are illegal because the order of evaluation is implementation-defined. That is possible because the order of evaluation of the AssignExpression is implementation-defined. I'm having trouble understanding the difference between order of evaluation and sequence points. Sequence points are the points in execution where all side effects of previous expressions must have been performed. An analysis for the second example: int a = 1; int b = 2; int c; c = a + (a = b); -> c = 1 + (a = b) (AddExpression is evaluated left-to-right http://www.d-programming-language.org/expression.html#AddExpression) -> c = 1 + 2 (with side effect a = 2) -> c = 3 It seems that D follows C/C++'s sequence points. I.e. the side effect 'a = 2' has to be performed until specific execution points. I believe the above expression is illegal because the side effect may be executed before the subexpression 'a' is evaluated. In this reading evaluating an expression does not say anything about sequence points. I.e. in the above case it is not the order of evaluation that makes the expression illegal. It is C/C++'s inherited sequence points. This complicates matters in contrast to Java where each (sub)expression is a sequence point. Leaving behavior (implementation-)undefined usually allows the compiler to better optimize. What are the usual/optimal gains for less restrictive sequence points (i.e. more flexible operation reordering)? Why did D choose to follow C/C++'s defined sequence points? When is an expression considered to be fully evaluated? Does it include performing its side effects? I believe it does not, because then the above expressions would be legal. Jens
Aug 08 2011
Jens Mueller:Why did D choose to follow C/C++'s defined sequence points?I think to simplify the implementation, because I think it comes from the evolution (the back-end too) of a C++ compiler. But Walter has said two or three times that he's interested in changing D semantics, and define the order of evaluation in both function calls and in expressions. The little (but how much? I have never seen realistic benchmarks on this) decrease in performance is in my opinion worth the gain in code determinism, across compilers too. Bye, bearophile
Aug 08 2011
Jens Mueller Wrote:I'm having trouble understanding the difference between order of evaluation and sequence points.Order of evaluation matters when subexpressions contain sequence points.
Aug 08 2011
Kagamin wrote:Jens Mueller Wrote:Because it moves sequence points around. Right? Reasoning about the evaluation of expressions with sequence points is difficult. Because the programmer has to first known all the rules and second follow them appropriately. My current definition for illegal expressions is as follows: An expression is illegal iff it has a side effect that modifies a variable which is either read or written as part of the expression. I only see one justification for having this more difficult evaluation of expressions. That is better performance. I wish I only had a feeling for it. Because right now it just feels complicated for no reason. JensI'm having trouble understanding the difference between order of evaluation and sequence points.Order of evaluation matters when subexpressions contain sequence points.
Aug 09 2011