If one macro is defined for "assignment": Ex. #define equal(a,b) {a=b; }
And write: equal(A,1); equal(A,2); equal(A,3);
where A is pointer to XDATA space(mapped to H/W)
Keil C will "optimize" them and final result is A=3 ? or Keil C won't do this and expand them one by one ?
Thanks in advance...
Macros are processed by the pre-processor - This is a simple text processor and does no optimizations.
The (expanded) output of this text processing is passed on to the compiler, which may decide to optimize the sequence.
If (as you are indicating) you do not want the sequence of access to XDATA to be optimized, then you should use the volatile attribute for the pointer to the location.
As Nevill says, the macros are expanded by the preprocessor, and the compiler "sees" only the result of the expansion.
Therefore whether the code originated from a macro expansion, or was typed verbatim, makes absolutely no differences whatsoever to the compiler.
This, in turn, means that the use of a macro (or not) makes absolutely no differences whatsoever to the optimisation behaviour.
This is all standard 'C' stuff - nothing specifically to do with Keil or the 8051.
Incidentally, what would be the point of such a macro?
Thanks for all your opinions first ! Maybe i used unsuitable example...Below is the true story and I want to explain why.
@1 I got one code base and found the following way is used to access h/w registers: #define regA *((unsigned char xdata *) 0xa1) ... regA = 0xFF; ... while(regA);
@2 I try to add code segments: regA = 0x01; regA = 0x02; regA=0x03; and I found each statement is executed and no loss...
@3 "volatile" is not used above and my entire code did not use "volatile" , but h/w access works well. Besides, XBYTE/XWORD and _at_ are NOT used... (that is, the 3 ways listed in doc: http://www.keil.com/support/docs/1455.htm are NOT used)
I can not explain the phenomenon and the only thing I can suspect is: #define creates a macro and maybe after expansion these codes are NOT optimized...
"I can not explain the phenomenon"
I explained it to you in your other thread on exactly the same subject:
http://www.keil.com/forum/docs/thread13388.asp
That's the trouble with creating duplicate threads on the same subject!
Thanks for your opinion...Initially I thought these 2 were different and thus posted 2 threads.
Please help to remove one of them if available...
Thanks anyway !
Thanks for your opinion
It is not an "opinion" =:0
They did not respond with opinions, but with facts.
Another thing - a #define named equal(a,b) should really represent a comparison - (is a equal to b?).
If you want your define to assign values, you should call it assign(a,b). Now, this was an opinion - never use confusing names for variables, defines, functions, ...
But as already mentioned, the preprocessor is really a preprocessor (even if implemented inside the compiler) doing text-based search/replace in your source code before the compiler starts to decide what the code looks like and starts with optimizations/code generation.
Thanks for all the facts you provided ^o^