C problem, order of evaluation

Checkpoint Technologies ckp at grebyn.com
Tue Apr 10 05:25:29 AEST 1990


In article <19539 at grebyn.com> ckp at grebyn.UUCP (Checkpoint Technologies) writes:
>In article <1990Apr8.185047.7385 at diku.dk> null at diku.dk (Niels Ull Jacobsen) writes:
>>I need to make an expression of the type:
>>
>>"( stack[++sp]= exp , stack[sp -= N])", where exp contains N "stack[++sp]"'s.
>>
>>The expression is supposed to have the value of the right side of the
>>assignment, and to push this on the stack as well. The expression is to be a
>>part of a function argument, and can therefore not contain any local block
>>declarations.
>
> [ then I talk about the comma operator, completely missing the point ]

OOPS - I misread the question.  Well, what I said about comma is true,
even if you weren't asking about it...

Well, then, you're using ++sp many times in the same expression it
seems, once on the left side ("stack[++sp] = ..."  and several times on
the right. This is not gonna work.  But I do have a suggestion.

I presume you're building a stack machine, such that a stack operator
takes several stack arguments, pops them, and writes one stack result
in their place.  Try doing it this way:

( sp += N, stack[sp] = exp)

where exp contains several instances of stack[sp-C],
   where each C is a negative offset to the argument you wish.

Now this expression exp contains no side effects and therefore it's
order of evaluation is unimportant.  The stack 'pop' operation is
performed first.  The value of this expression will be the value stored
into the 'result' stack location, which is what you wanted, and it's why
I put the stack pointer modify operation first.



More information about the Comp.lang.c mailing list