CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Compiler 'quirks' (strange code generation)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
staticmonkey
Guest







Compiler 'quirks' (strange code generation)
PostPosted: Wed Aug 26, 2009 3:43 am     Reply with quote

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:

Code:
if (a != b)
   a = b;

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 5:28 am     Reply with quote

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.
staticmonkey
Guest







PostPosted: Wed Aug 26, 2009 5:46 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Wed Aug 26, 2009 7:44 am     Reply with quote

It actually makes quite good 'sense'!.... Smile

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
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 8:38 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group