A tricky C problem

Started by
19 comments, last by greenhybrid 16 years, 11 months ago
Quote:Original post by Spoonbender
Quote:Original post by greenhybrid
To make it short: To the day of this very thread, I allways assumed the operators do what they're supposed to do, 'executed' with respect to priority and associativity.

Oh, they do.
They just don't tell their arguments when to execute.

Obviously, in a 'a * b + c' statement, a * b is computed before the +, because the result is needed for the addition. Priority and associativity takes care of these things just like you'd expect.
But they don't say anything about whether a, b and c themselves should be computed first. They might not be simple numbers, but could be, say, function calls. (let's call it a() * b() + c() now) Sure, we'll need to call the functions a and b before we can carry out the multiplication, but there's nothing in the rules about whether the functions a or b should be called first. Or even if c, which isn't used until a later, should be called first. But that's not the responsibility of the operators, or for that matter, their priority or associativity.


That's what I meant by 'executing'. The simple tokens "a", "b" and "c" may yield to "push a|b|c", thus the push is 'executed'. A thingie like "a+b" should yield to kind of "push a / push b / add (result in st+1) / pop".

If I feed my "xicc"-compiler (you may read more on my homepage) with the following code-fragment, ...
alpha = 1/delta + zulu;


...yields to:

push 1.000000push deltadivp st+1, stpush zuluaddp st+1, stmovtos alphapop


Currently not supported by my compiler is the "+=" operator, but I would go this way:

"alpha += delta * echo"
>> push delta
>> push echo
>> mul st+1, st
>> pop
>> push alpha
>> add st, st+1
>> pop alpha
>> pop

so at first, compute/load "delta", then, compute/load "echo", multiply the left operand with the right, then load "alpha", calculate "alpha+tmpValueOnStacl", store to "alpha".

I guess it's still unclear what I mean by "execution"...in the end every terminal token in a programm yields to the insertion of some assembly, which is 'executed' (bison/flex-users know what I mean). And that's what I mean when saying "a,b,c,+=,etc." are not executed in the intended order within a sequence point. Hope you got what I mean ;)


edit: doh, messed my handcoded assembly a bit

This topic is closed to new replies.

Advertisement