View previous topic :: View next topic |
Author |
Message |
staticmonkey Guest
|
Compiler 'quirks' (strange code generation) |
Posted: Wed Aug 26, 2009 3:43 am |
|
|
Hi,
I've been working on a project using the PIC10F220 which only has 256 program words and therefore code space is at a premium.
Through trying to reduce the size of the code I have come accross what I can only describe as compiler 'quirks' which produce more code that is necessary. Below describes some of the instances that I have encountered thus far:
The coding standard that I am working to requires enclosing braces on all if statements. You would expect the following:
To generate the same code as:
Code: | if (a != b) {
a = b;
}
|
However the latter statement adds an extra instruction.
If I have a function called IsTrue() which returns a boolean then you would expect:
Code: | if(IsTrue() == TRUE) {
....
}
|
To generate the same code as:
Code: | if(IsTrue()) {
....
}
|
However the latter statement adds an extra instruction.
There's a load of other example which seem to add extra instructions just for the hell of it. I've even examined the list file and saw the additions of goto the next line!
Rant over =P |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Aug 26, 2009 5:28 am |
|
|
I remember to have seen similar cases before, but I can't reproduce your example. You should supply a minimal demonstration source and also mention your compiler version. |
|
![](templates/subSilver/images/spacer.gif) |
staticmonkey Guest
|
|
Posted: Wed Aug 26, 2009 5:46 am |
|
|
As requested here is a piece of sample code to demonstrate the first point. Compiler is version 4.093.
Code: | #include <10F220.h>
#device adc=8
#FUSES NOWDT, MCLR, NOPROTECT, NOMCPU, IOSC4
#use delay(clock=4000000)
void main()
{
int8 a = 0;
while (TRUE) {
if (a == 10)
a = 0;
a++;
}
return;
}
|
This program in total uses 11 instructions. The .lst file around the if statement shows:
Code: | .................... if (a == 10)
0004: MOVLW 0A
0005: SUBWF 13,W
0006: BTFSC 03.2
.................... a = 0;
0007: CLRF 13
|
If I change the code to have braces around the single statement:
Code: | ....
if (a == 10) {
a = 0;
}
....
|
Then the program uses 12 instructions. The .lst file around the if statement now shows:
Code: | .................... if (a == 10) {
0004: MOVLW 0A
0005: SUBWF 13,W
0006: BTFSS 03.2
0007: GOTO 009
.................... a = 0;
0008: CLRF 13
|
Hopefully this demonstrates the problem. Obviously the compiler is missing the case for a single entry in a if block. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Aug 26, 2009 7:44 am |
|
|
It actually makes quite good 'sense'!....
If you think about it, the presence of the brackets, implies that this code section _will_ contain several instructions. Hence it probably 'makes sense' to optimise the routing, on the assumption there will be more than one code instruction here.
It's obviously be 'better' if the compiler looked at how much code there really was, but as a basic assumption, it makes a lot of sense.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Aug 26, 2009 8:38 am |
|
|
A few additional remarks:
-The title strange code generation is missing the point. The discussion is actually about code optimization.
- Your above example if (a != b) a = b; don't show different code length, because a = b is compiled to two machine instructions and a goto is needed anyway.
- Finally. You'll find much more severe examples, where CCS C lacks a high level of optimization, compared e.g. to Microsoft Embedded Visual C. It's the same with most compilers for small embedded processors. Typical examples are recalculation of array element addresses or arithmetic expressions that are repeated in the code. |
|
![](templates/subSilver/images/spacer.gif) |
|