Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/08/11 09:02
Read: times


 
#182535 - Fundamental philosophy of High-Level Languges (HLL)
Responding to: ???'s previous message
When you write in a High-Level Languge (HLL) - any HLL, including 'C' - you delegate the low-level control (specific register choices, specific instruction choices) to the compiler.

If you need to worry about such details, then you must write in assembler.

The compiler doesn't just look at individual source lines, and translate each one in isolation; you should think of it as looking at the code as a whole, and creating the required outcomes.

eg, if you write

char x;

x = 1;
x = 2;
x = 3;
Do not expect the compiler to generate three writes to x!
The compiler can see that only the final write actually has any lasting effect on the value of x - so will only generate code for that final write.
In fact, if the value is never used, the compiler may be able to see that - and will not generate any code at all!

(look up the 'volatile' keyword if you need to have such code...)

It is really important for you to understand this - especially when it comes to compiler optimisation.

Failure to understand this is a cause of many beginner's mistakes - such as trying to use HLL loops for timing delays: http://www.8052.com/forum/read/182476


Sandy B. Pal said:
I've inserted .LST file parts where anomalies were observed.


We really need to see the context of the original 'C' source

In part[2]
if(dn_pressed) gives jnb P0.6 ; which is as expected
whereas in part [1] & [3]
if(dn_pressed) is giving mov c, P0.6 ; which is not expected.

You are asking the wrong question here!

As explained above, The question should not be, "does this code generate some specific machine instrustion sequence?"; instead, you need to ask, "does the generated code implement the functionality of the source?"

If the generated code implements the functionality stated in the source code, then the compiler has fulfilled its obligation.


To analyse why the compiler might be generating different code, We really need to see the full context of the original 'C' source.

You haven't shown the code that relies upon these tests - which is almost certainly what dictates the compiler's different choice in the different places!

List of 17 messages in thread
TopicAuthorDate
Compiler variations??            01/01/70 00:00      
   Fundamental philosophy of High-Level Languges (HLL)            01/01/70 00:00      
      C code            01/01/70 00:00      
         and so what            01/01/70 00:00      
         Stop wondering about the compiler output            01/01/70 00:00      
            Very nice to learn this important matter            01/01/70 00:00      
         Exactly what you wrote            01/01/70 00:00      
            volatile sbit may be the problem            01/01/70 00:00      
               read up on (not) volatile            01/01/70 00:00      
               Look at my profile            01/01/70 00:00      
                  Not offence intended            01/01/70 00:00      
                     defining P0_6 so that compiler doesn't treat it as volatile            01/01/70 00:00      
                        Skip the goto - almost always exists beautiful rewrites            01/01/70 00:00      
                           Goto really is a bastard code construct            01/01/70 00:00      
                              and therefore ...            01/01/70 00:00      
                                 Will come back with modified code            01/01/70 00:00      
                           Wonderful as always!            01/01/70 00:00      

Back to Subject List