We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
When I write a simple switch case program: ----------source:
void main() { char e = 0; int a=0; int b=2; int c=3; switch(e) { case 1: a = a + b; break; case 2: a = a + c; break; default: break; } }
But,the c51 complier generates:
... 4: void main() 5: { 6: char e = 0; 7: C:0x000F E4 CLR A 8: int a=0; 9: int b=2; 10: int c=3; 11: switch(e) C:0x0010 24FE ADD A,#0xFE C:0x0012 6002 JZ C:0016 C:0x0014 04 INC A 12: { 13: case 1: 14: a = a + b; 15: break; C:0x0015 22 RET 16: case 2: C:0x0016 22 RET C:0x0017 00 NOP C:0x0018 00 NOP C:0x0019 00 NOP ...
How could it be possible?
This has been discussed many times. The compiler is allowed to emit ANY code as long as the emitted code generates the correct output. Your example generates NO output whatsoever, and the emitted code does so as well. That's optimization at work. In fact, I would expect the emitted code to consist of a single instruction RET. Try lowering optimization level.
Try lowering optimization level.
And while the OP is at it, he could also add an endless loop or some other measure to prevent main from returning.
Else we'll see the other question that's been discussed to death, "why does my program enter an endless loop?", shortly.
So,how can i choose the optimization level to satisfy my require? Is there any principias?
What is your requirement?
Currently, the source code does nothing useful - and the generated machine code meets that requirement!
?
Hi yu zhou,
I don't know much about this. But as Andy Neil says, it depends on your requirement.
For Study/Debug, it is usually better to choose the lowest optimization level. (Unless you want to know the behavior of compiler.)
If smaller binary size/better performance is important, it is usually better to choose the highest optimization level.
And if you find something strange, then choose the lowest optimization level.
http://www.keil.com/support/man/docs/armcc/armcc_cjaieafa.htm
www.keil.com/.../armccref_cihgfgfb.htm
The above URL is for ARM, not 8051. But it does provide some hints.
3
Maximum optimization. -O3 performs the same optimizations as -O2 however the balance between space and time optimizations in the generated code is more heavily weighted towards space or time compared with -O2. That is:
* -O3 -Otime aims to produce faster code than -O2 -Otime, at the risk of increasing your image size
* -O3 -Ospace aims to produce smaller code than -O2 -Ospace, but performance might be degraded.
In addition, -O3 performs extra optimizations that are more aggressive, such as:
* High-level scalar optimizations, including loop unrolling, for -O3 -Otime. This can give significant performance benefits at a small code size cost, but at the risk of a longer build time.
* More aggressive inlining and automatic inlining for -O3 -Otime.
* Multifile compilation by default.
If your requirement is that the code works properly then don't change anything.
If your requirement is that the compiler generates the object code you expected then reduce the optimisation level to zero and define all the variables as volatile.
I assume the latter suggestion is your requirement, but if it is something else then please explain.
Looking at a manual for a totally different compiler for a totally different architecture probably isn't too helpful - especially when the correct manual is readily to hand:
http://www.keil.com/support/man/docs/c51/c51_optimize.htm
Note also the Related Knowledgebase Articles linked at the bottom of the page...