|
|
View previous topic :: View next topic |
Author |
Message |
Ramey
Joined: 11 Sep 2007 Posts: 18
|
incorrect code generated for signed ints? |
Posted: Fri Mar 28, 2008 12:49 am |
|
|
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
|
|
Posted: Fri Mar 28, 2008 1:06 am |
|
|
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
|
|
Posted: Fri Mar 28, 2008 1:16 am |
|
|
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. |
|
|
|
|
|
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
|