A bug in C166 v.4.27

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();
}
In the disassembly window of the simulator the generated code looks like this:
     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
which is obviously wrong since the user stack pointer R0 gets corrupted. If we add local variables to test the use of the user stack pointer we will see that a word memory access on odd address will be generated because of the corrupt R0.
When compiling via SRC file and assembler everything is different:
; 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
We can see that the __inline keyword is ignored here and the code is correct.

- mike

Parents
  • 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

Reply
  • 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

Children
More questions in this forum