I want to perform math compares using constants so that we test that the compare operation performs correctly. The compiler obviously recognizes that the compare operation produces a true result with the constants given so it doesn't generate any code for it. What can I specify to the compiler to tell it to generate code for what it sees and not perform any optimization? Here is an example piece of the code:
BOOL BoolResult = FALSE; /*--------------------------------------------------- * integer compare operations *---------------------------------------------------*/ if ( (10000 < 20000) && (20000 <= 30000) && (30000 > 25000) && (25000 >= 20000) && (20000 == 20000) && (20000 != 20001) ) { BoolResult = TRUE; }
You could move the comparisons into separate functions and hope that the compiler doesn't inline them:
BOOL less(int a, int b) { return a < b ? TRUE : FALSE; } BOOL more(int a, int b) { return a > b ? TRUE : FALSE; }
And so on. Then use these functions instead of the comparison operators. After testing, you can use macros to 'inline' the functions.
What can I specify to the compiler to tell it to generate code for what it sees and not perform any optimization? the only "optimization" (the one you want) at level 2 is local variable overlaying
Erik
No level of optimization seems to work, unless there is some other setting in the configuration that changes the way optimization is done.
What, exactly, are you trying to achieve here?
Why do you want to "test" this?
Not true:
"Each higher optimization level contains all of the characteristics of the preceding lower optimization level"
http://www.keil.com/support/man/docs/c51/c51_optimize.htm
So level-2 includes everything from levels 0 & 1; viz:
Constant Folding; Simple Access Optimizing; Jump Optimizing; Dead Code Elimination; Jump Negation.
The object is to validate that the processor is working correctly (for safety reasons), this is one of the suggested tests.
I don't know about others, but this sounds like a rather strange thing to do - at least this way! Just RAM a ram test, man...
Regardless of whether this test has any merit or not, the purpose of this thread is to find out if there is any way to tell the compiler to not do any optimization and generate code to perform the instructions as written.
You can try the compiler manual - maybe there is a #pragma for that...
Setting aside the huge issue of whether that kind of test still makes any sense whatsoever, these days: if that's what you want to test, you really have to write that code in assembly. If you want to force the tools to generate a particular code sequence, no high-level language, not even C, is the proper tool for the job.
That is a fundamental misconception of the nature of a high-level language!
You should not think of HLL source code as a sequence of instructions that the compiler simply transliterates into a sequence of machine instructions.
If you want to guarantee a specific set of machine instructions then, as already noted, you must write in assembler.
If possible, use volatile variables instead of constants.