C problem, order of evaluation

Niels Ull Jacobsen null at rimfaxe.diku.dk
Wed Apr 11 05:58:22 AEST 1990


karl at haddock.ima.isc.com (Karl Heuer) 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.


>I think the problem was with the multiple references to `sp' on the left side
>of the comma.  Niels: am I correct in assuming that
>	(stack[++sp] = stack[++sp] + stack[++sp], stack[sp -= 2])
>is an example of the generated code?

>My recommendation is to force your translator to generate code like
>	(++sp, stack[sp] = stack[sp+1] + stack[sp+2])
>instead.  That's the only way to be sure that the compiler knows what you're
>talking about.

Thank you, but your assumption is wrong. I am not making a stack machine, so
the arguments will not be on the stack. The exp could itself contain
expressions of this type. I could have an expression like :

      (stack[++sp] = g((stack[++sp] = make_cons(x,y), stack[sp -= 0]),
                       (stack[++sp] = make_cons(x,z), stack[sp -= 0])),
       stack[sp -= 2])

The stack is actually only used to keep track of the cons-cells for the garbage collection.

>Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint

In article <19539 at grebyn.com> ckp at grebyn.UUCP (Checkpoint Technologies) writes:

>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.

Thank you, but that won't work either, see above.

               Niels Ull Jacobsen, Inst. of Datalogy, U of Copenhagen.
               null at diku.dk
               (Speaking from the void ..)



More information about the Comp.lang.c mailing list