The C166 compiler generates incorrect code when using __inline functions with bit return type. Test source code:
bit bitvar1 = 1; bit bitvar2; __inline bit test() { return bitvar1; } void main() { bitvar2 = test(); }
9: void main() 10: { 0000000A 4A00F000 BMOV R0.0,0xFD00.0 11: bitvar2 = test(); 0000000E 4AF00001 BMOV 0xFD00.1,R0.0 12: } 00000012 CB00 RET
; line 4: __inline bit test() ; line 5: { ; line 6: return bitvar1; BMOV R4.0,bitvar1 ; line 7: } RET ; line 8: ; line 9: void main() ; line 10: { ; line 11: bitvar2 = test(); CALL test BMOV bitvar2,R4.0 ; line 12: } RET
Hi Mike, this is interesting. My compiler V4.27 generates this disassembly: ; FUNCTION test (BEGIN RMASK = @0x0010) ; SOURCE LINE # 90 ; SOURCE LINE # 92 0000 4A00F400 R BMOV R4.0,bitvar1 ; SOURCE LINE # 93 0004 DB00 RETS ; FUNCTION test (END RMASK = @0x0010) ; FUNCTION main (BEGIN RMASK = @0x4000) ; SOURCE LINE # 95 ; SOURCE LINE # 97 ; SOURCE LINE # 92 ; SOURCE LINE # 97 0006 4A000000 R BMOV bitvar2,bitvar1 ; SOURCE LINE # 98 000A DB00 RETS ; FUNCTION main (END RMASK = @0x4000) The code should be correct, but the test-FUNCTION is dead code. May be this is different because I use the "MEDIUM" memory model? Anyway, Keil pretends that the C166 Compiler is capable of handling inline-functions, but this is not true. In 99% the __inline keyword is ignored. Particularly when you look at the example code in the release notes to C166 V4.25. When you compile this code with V4.27 you get totally different results, nothing is "inline". Holger
Hi Holger, That's strange, I couldn't get the compiler to generate the code that you posted. Anyway, how did you get that disassembly? Did you use the simulator or did you get the compiler to generate .SRC file? I'm asking this because it seems like the compiler generates different code with SRC option and without it. In particular, it seems that it ignores the __inline keyword when compiling with SRC. As for dead code, maybe it's left there just in case anyone needs the address of the function. The compiler cannot know if anyone needs it since references to the function can be in other modules. Besides, if you decide to use inline functions, you should be prepared for bigger code size anyway. - mike
Just found this: The bug doesn't show with this code at optimization level 7 (common tail merging.) It shows at optimization level 6 (constant propagation) which is default when a new project is created. - mike
View all questions in Keil forum