Re: Side effects in expressions

johnmce@world.std.com (John McEnerney)
18 Aug 1999 11:29:20 -0400

          From comp.compilers

Related articles
Side effects in expressions mikov@usa.net (Tzvetan Mikov) (1999-08-18)
Re: Side effects in expressions johnmce@world.std.com (1999-08-18)
Re: Side effects in expressions johnmce@world.std.com (1999-08-21)
| List of all articles for this month |
From: johnmce@world.std.com (John McEnerney)
Newsgroups: comp.compilers
Date: 18 Aug 1999 11:29:20 -0400
Organization: Metrowerks, Inc.
References: 99-08-078
Keywords: optimize

"Tzvetan Mikov" <mikov@usa.net> wrote:
> So, this expression is calculated correctly and we can even easily apply a
> very simple common sub-expression elimination. What happens however if we
> have sub-expressions with side effects? Examine this C code:
> int * p = ...;
> x = *(p = p + 1) + *(p = p + 1);


This is probably not a good example, as the behavior of this
expression is undefined (in ANSI C) because the compiler is free to
evaluate the assignments to 'p' anywhere between the previous ';' and
the subsequent one.


But I think that the usual understanding is that during translation
from abstract syntax trees to IL, side-effecting expressions are
hoisted as if they were independent statements. This is also true of
flow-of-control operators like && and || and ?:.


So all side-effects occur as the "root" of the expressions that
contain them. I suspect that the return values of function calls are
assigned to "variable" temporaries rather than expression temporaries.


I like the Morgan book a lot, but although it seems to be a modern
version of "The Design of an Optimizing Compiler", the difference is
that Wulf et al were describing a compiler that actually
existed. Morgan is describing a proposed design, some parts of which
are taken from compilers that exist, and some of which haven't been
implemented in the proposed form. Still, he's very complete.
--
John McEnerney (mcenerney@metrowerks.com)


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.