Orin 0 Posted November 17, 2007 Report Share Posted November 17, 2007 (edited) Bug description: Aggressive optimisation misses a bit-mapped variable when replacing local variables. Steps to reproduce: The following code should be sufficient. #define DATA1 porta.2 //I/O #define CLK1 porta.1 //out #pragma OPTIMIZE "a" unsigned char input_byte(void) { unsigned char byte; unsigned char j; // The do { ... } while form is shorter // for ( j = 0; j < 8; j++ ) j = 8; do { byte <<= 1; byte.0 = DATA1; CLK1 = 1; CLK1 = 0; } while ( --j != 0 ); return byte; } Assembly output: input_byte_00000 ; { input_byte; function begin MOVLW 0x08 BCF STATUS, RP0 BCF STATUS, RP1 MOVWF input_byte_00000_1_j label268439094 BCF STATUS,C RLF CompTempVarRet503, F BCF input_byte_00000_1_byte,0 BTFSC gbl_porta,2 BSF input_byte_00000_1_byte,0 BSF gbl_porta,1 BCF gbl_porta,1 DECF input_byte_00000_1_j, F BTFSS STATUS,Z GOTO label268439094 RETURN ; } input_byte function end Expected behaviour: ALL (or no) instances of the local variable "byte" should have been replaced by "CompTempVarRet503". Is the problem 100% reproduceable: YES IDE version: 6.81 Compiler: BoostC Compiler version: 6.81 Target device: PIC16F88 OS: OS version Comments: I'm surprised the compiler doesn't generate a DECFSZ/GOTO sequence for this loop. "while ( --j )" is longer than "while ( --j != 0 )" too. Edit: CLK1 definition was missing. Edited November 18, 2007 by Orin Quote Link to post Share on other sites
cac001 0 Posted December 3, 2007 Report Share Posted December 3, 2007 (edited) You did not seem to get a response from Dave or Pavel, so here is my work around: ;///////////////////////////////////////////////////////////////////////////////// ;// Code Generator: BoostC Compiler - http://www.sourceboost.com ;// Version : 6.81 ;// License Type : Pro License ;// Limitations : PIC12,PIC16 max code size:Unlimited, max RAM banks:Unlimited ;///////////////////////////////////////////////////////////////////////////////// // File: bug22.C // Target: PIC16F88 // OS: WinXP, SP2 // SourceBoostIDE: 6.81 // Compiler: BoostC 6.81 // Reproducible: always // Expected behavior: // ALL (or no) instances of the local variable "byte" // should have been replaced by "CompTempVarRet495". // // Workaround: // change bit set to bit-wise or // #include <system.h> unsigned char input_byte( void ); volatile unsigned char out; void main() { out = input_byte(); 0011 2003 CALL input_byte_00000 0012 0822 MOVF CompTempVarRet495, W 0013 00A0 MOVWF gbl_out for(;;); 0014 label268438922 0014 2814 GOTO label268438922 } #define WORK_AROUND #define DATA1 porta.2 //I/O #define CLK1 porta.1 //out #pragma OPTIMIZE "a" unsigned char input_byte( void ) { unsigned char j; unsigned char byte; // The do { ... } while form is shorter // for ( j = 0; j < 8; j++ ) j = 8; 0003 3008 MOVLW 0x08 0004 1283 BCF STATUS, RP0 0005 1303 BCF STATUS, RP1 0006 00A1 MOVWF input_byte_00000_1_j do 0007 label268438906 { byte <<= 1; 0007 1003 BCF STATUS,C 0008 0DA2 RLF CompTempVarRet495, F #ifdef WORK_AROUND if (DATA1) byte |= 1; 0009 1905 BTFSC gbl_porta,2 000A 1422 BSF CompTempVarRet495,0 #else byte.0 = DATA1; #endif CLK1 = 1; 000B 1485 BSF gbl_porta,1 CLK1 = 0; 000C 1085 BCF gbl_porta,1 } while ( --j != 0 ); 000D 03A1 DECF input_byte_00000_1_j, F 000E 1D03 BTFSS STATUS,Z 000F 2807 GOTO label268438906 return byte; } 0010 0008 RETURN //////////////////////////////////////// // Code with no source :-) //////////////////////////////////////// 0000 2815 GOTO _startup 0015 _startup 0015 118A BCF PCLATH,3 0016 120A BCF PCLATH,4 0017 2811 GOTO main I hope this helps a little. Edited December 3, 2007 by cac001 Quote Link to post Share on other sites
Orin 0 Posted December 3, 2007 Author Report Share Posted December 3, 2007 You did not seem to get a response from Dave or Pavel, so here is my work around: ;///////////////////////////////////////////////////////////////////////////////// ;// Code Generator: BoostC Compiler - http://www.sourceboost.com ;// Version : 6.81 ;// License Type : Pro License ;// Limitations : PIC12,PIC16 max code size:Unlimited, max RAM banks:Unlimited ;///////////////////////////////////////////////////////////////////////////////// // File: bug22.C // Target: PIC16F88 // OS: WinXP, SP2 // SourceBoostIDE: 6.81 // Compiler: BoostC 6.81 // Reproducible: always // Expected behavior: // ALL (or no) instances of the local variable "byte" // should have been replaced by "CompTempVarRet495". // // Workaround: // change bit set to bit-wise or // #include <system.h> unsigned char input_byte( void ); volatile unsigned char out; void main() { out = input_byte(); 0011 2003 CALL input_byte_00000 0012 0822 MOVF CompTempVarRet495, W 0013 00A0 MOVWF gbl_out for(;;); 0014 label268438922 0014 2814 GOTO label268438922 } #define WORK_AROUND #define DATA1 porta.2 //I/O #define CLK1 porta.1 //out #pragma OPTIMIZE "a" unsigned char input_byte( void ) { unsigned char j; unsigned char byte; // The do { ... } while form is shorter // for ( j = 0; j < 8; j++ ) j = 8; 0003 3008 MOVLW 0x08 0004 1283 BCF STATUS, RP0 0005 1303 BCF STATUS, RP1 0006 00A1 MOVWF input_byte_00000_1_j do 0007 label268438906 { byte <<= 1; 0007 1003 BCF STATUS,C 0008 0DA2 RLF CompTempVarRet495, F #ifdef WORK_AROUND if (DATA1) byte |= 1; 0009 1905 BTFSC gbl_porta,2 000A 1422 BSF CompTempVarRet495,0 #else byte.0 = DATA1; #endif CLK1 = 1; 000B 1485 BSF gbl_porta,1 CLK1 = 0; 000C 1085 BCF gbl_porta,1 } while ( --j != 0 ); 000D 03A1 DECF input_byte_00000_1_j, F 000E 1D03 BTFSS STATUS,Z 000F 2807 GOTO label268438906 return byte; } 0010 0008 RETURN //////////////////////////////////////// // Code with no source :-) //////////////////////////////////////// 0000 2815 GOTO _startup 0015 _startup 0015 118A BCF PCLATH,3 0016 120A BCF PCLATH,4 0017 2811 GOTO main I hope this helps a little. Thanks! I'd taken the easy way out and turned agressive optimisation off for now. Since this is a new compiler to me, I was trying to work out what produces the best code. Your workaround shaves another instruction out of the loop while allowing the aggressive optimisation. Orin. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.