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

incorrect code generated for signed ints?

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



Joined: 11 Sep 2007
Posts: 18

View user's profile Send private message

incorrect code generated for signed ints?
PostPosted: Fri Mar 28, 2008 12:49 am     Reply with quote

Below is a small excerpt from a *.lst file generated by my PCH compiler. It looks to me that the code for the x < 0 branch is not being generated. Am I missing something? What should I do about this?

Robert Ramey

Code:

CCS PCH C Compiler, Version 4.055, 40531               27-Mar-08 23:35

               Filename: LEM.lst

               ROM used: 7690 bytes (23%)
                         Largest free fragment is 25076
               RAM used: 1301 (64%) at main() level
                         1369 (67%) worst case
               Stack:    12 worst case (5 in main + 7 for interrupts)

<skipped>...
.................... int8  test(int16 x){
....................     if(x < 0)
....................         return -1;
....................     else
....................     if(x > 0)
*
1006:  MOVLB  1
1008:  MOVF   x38,F
100A:  BNZ   1010
100C:  MOVF   x39,F
100E:  BZ    1018
....................         return 1;
1010:  MOVLW  01
1012:  MOVWF  01
1014:  BRA    101E
....................     else
1016:  BRA    101E
....................         return 0;
1018:  MOVLW  00
101A:  MOVWF  01
101C:  BRA    101E
.................... }
101E:  MOVLB  0
1020:  GOTO   12FE (RETURN)
.................... 
Ramey



Joined: 11 Sep 2007
Posts: 18

View user's profile Send private message

PostPosted: Fri Mar 28, 2008 1:06 am     Reply with quote

OK, I discovered the problem. int16 is by default unsigned. This was certainly a suprise to me - oh well. I wonder why CCS chose to do it differently than every other C compiler in the world - and not emit a warning or compiler error.

Looking at the code
Code:

CCS PCH C Compiler, Version 4.055, 40531               27-Mar-08 23:35

               Filename: LEM.lst

               ROM used: 7690 bytes (23%)
                         Largest free fragment is 25076
               RAM used: 1301 (64%) at main() level
                         1369 (67%) worst case
               Stack:    12 worst case (5 in main + 7 for interrupts)

<skipped>...
.................... int8  test(int16 x){
....................     if(x < 0)
....................         return -1;
....................     else
....................     if(x > 0)
*
1006:  MOVLB  1
1008:  MOVF   x38,F
100A:  BNZ   1010
100C:  MOVF   x39,F
100E:  BZ    1018
....................         return 1;
1010:  MOVLW  01
1012:  MOVWF  01
1014:  BRA    101E
....................     else
1016:  BRA    101E
....................         return 0;
1018:  MOVLW  00
101A:  MOVWF  01
101C:  BRA    101E
.................... }
101E:  MOVLB  0
1020:  GOTO   12FE (RETURN)


I see two adjacent BRA at 1014 and 1016. Isn't this a waste of memory?
same goes for BRA at 101C. Shouldn't this stuff be filtered out with a peephole optimizer.

Robert Ramey
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 28, 2008 1:16 am     Reply with quote

If you enable the compiler warning messages, you will see this:
Quote:

Warning 205 Line 10(1,1): Unsigned variable is never less than zero

In CCS, all integer data types are unsigned by default. This is in
the manual, in the section on Data Definitions -- Basic and Special Types:
Quote:

Note: All types, except float, by default are unsigned; however, they
may be preceded by unsigned or signed.
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