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 support@ccsinfo.com

Problems with optimization

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



Joined: 10 Feb 2012
Posts: 28

View user's profile Send private message

Problems with optimization
PostPosted: Tue Apr 09, 2013 9:25 pm     Reply with quote

Running CCS compiler

CCS PCM C Compiler, Version 4.129, 59140
Executing: "C:\Program Files\PICC\Ccsc.exe" +FM "dental-main.c" +DF +LY +T +A +M -Z +Y=7 +EA #__16F1946=TRUE

----------------------------------------------------
.c:
Code:

M_INLINE void Encrypt(unsigned char source[], unsigned char number) {

   // this is supposed to be a const, but error in CCS compiler
   // does not allow it hence we get a warning
    unsigned char *pass_ptr = Passphrase + PassphraseIndex;
    unsigned int8 TmpIndex = PassphraseIndex;
    int i = 0;
    for (i = 0; i < number; i++ ) {
        source[i] = source[i] ^ (Passphrase[TmpIndex]);
        pass_ptr++;
        TmpIndex++;
        if (TmpIndex >= sizeof(Passphrase)) {
            TmpIndex = 0;
            pass_ptr = Passphrase;
        };
    };

 }
M_SEPARATE void AdvancePassphrase() {
    PassphraseIndex = (PassphraseIndex + PassphraseInc);
    PassphraseIndex = PassphraseIndex % sizeof(Passphrase);
}



.lst file:
024 PassphraseIndex

.asm:
Code:
34:                M_SEPARATE void AdvancePassphrase() {
35:                    PassphraseIndex = (PassphraseIndex + PassphraseInc);
  0707    300D     MOVLW 0xd
  0708    07A4     ADDWF 0x24, F
36:                    PassphraseIndex = PassphraseIndex % sizeof(Passphrase);
  0709    13A4     BCF 0x24, 0x7
37:                }
  070A    0008     RETURN

The problem here is that compiler does not set the BSR inside the AdvancePassphrase function.

It is sort of set before the call to AdvancePassphrase:
Code:
  1135    0020     MOVLB 0
140:                   AdvancePassphrase();
  1189    3180     MOVLP 0
  118A    2707     CALL AdvancePassphrase


However the "movlb 0" is never executed. I set the PC to the preceding function:
Code:
 
    Encrypt(SerialBuff+1, REQ_ADC_RESPONSE_SIZE);
    AdvancePassphrase();


And execution never goes to movlb. The function is executed with BSR=0x5, and does not really update PassphraseIndex.

That's pretty horrible. I spend whole day figuring this one out - the code was working before. Today I set optimization to a higher level because the code did not fit into the memory anymore.


Last edited by slavka012 on Tue Apr 09, 2013 9:55 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 09, 2013 9:40 pm     Reply with quote

Quote:
+Y=7

This means you are running #opt 7. The default opt level is 9.

I never change the #opt level. To do so, means you're running an
untested version of the compiler. 99.9% of the people on this forum
are using the default level, I would bet. So any bugs we find are
for the #opt 9 default level. No one really knows what the compiler
is doing for other #opt levels. I don't think they are tested by anyone.
Don't use them. And don't ask why #opt is included, lol.
slavka012



Joined: 10 Feb 2012
Posts: 28

View user's profile Send private message

PostPosted: Tue Apr 09, 2013 10:00 pm     Reply with quote

Hm... You might be right. I checked at #opt level 9, it works correctly. It works at level 6 and below as well.

I can't recall why I lowered it in the first place.
Ttelmah



Joined: 11 Mar 2010
Posts: 19331

View user's profile Send private message

PostPosted: Wed Apr 10, 2013 12:48 am     Reply with quote

I think 'black art' at opt=7. Smile
I think PCM programmer is 'spot on'. 99.9% of code will be at default, with the occasional 'test' at opt=0, if you think you have an optimisation problem.
Some years ago, there were some memory management problems which were fixed by dropping a couple of levels.

Best Wishes
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