[Info-vax] decrementing & for loops in C

nobody nobody at nowhere.com
Mon Jan 12 17:44:31 EST 2009


Johnny Billquist wrote:
> nobody skrev:
> 
>> vaxinf at chemie.uni-konstanz.de wrote:
>>
>>> Hi,
>>>
>>> I tried to run one of the example found on Jim Duff's homepage and
>>> found a behavior of for loops written in C:
>>
>>
>>
>> Others have commented about the program not working, so I won't.
>>
>> Another thing you may experience with some compilers is that the 
>> result will be that both loops go in the same direction no matter what 
>> you write. The reason beeing that an optimizing compiler may realize 
>> that you are not doing anyting inside the loop that forces you to do 
>> it in a special order. Therefore it may choose to turn one of them 
>> around in order to do the execution faster.
> 
> 
> True.
> But for this example, the program is doing something inside the loop. A 
> function call (printf). And since one argument of that function call is 
> the loop variable, changing the order of the loop will change the order 
> of the function calls to printf with different values, so an optimizer 
> who did that, would potentially change the whole programs effect. I 
> doubt anyone would write an optimizer who did that. :-)
> 
> You can only reorder a loop if it is obvious that this won't change the 
> way the program acts.
> 
>     Johnny
> 

Well, I realized that soon after I posted. However I liked the amount of 
dust I stirred up. I'm not a C programmer but I am studying the crap and 
the more I learn the more I hate it.

A C compiler actually has to give up for the reason you specify. It has 
no way to know what the called function does, since it's not until link 
time that the function call is connected to some function that hopefully 
has the same argument list. In a hypothetical other language the 
compiler could look at the called function to see what side-effects it 
had and decide if it could reorder. In the specific case with printf it 
would probably not be able to do it. Unless its able to write streams 
backwards.

I believe Ada is such a language. There you have a package spec from 
which you can see if the code is ada or just something linked.

For those of us going down the MDA road any action language would have 
to be formalized to a point where this would be fully possible, even 
simple. In fact the action language in xtUML assumes that every 
statement that is not depending on the result of another statement CAN 
be executed in paralell. In the end most model compilers would make 
serially executing code but not all. (Think generating VHDL)

End-Of-Discussion




More information about the Info-vax mailing list