For testadd function
uint16_t val = 0xFFFF;
uint16_t testadd()
{
return (val += 1);
}
SDCC produces the next assembly
; .\Source\main.c: 390: uint16_t testadd()
; -----------------------------------------
; function testadd
; -----------------------------------------
_testadd:
; .\Source\main.c: 392: return (val += 1);
ldw x, _val+0
incw x
; .\Source\main.c: 393: }
ret
Incremented value was not written back to val variable. The same occurs with other compound assignment operators.
;--------------------------------------------------------
; File Created by SDCC : free open source ANSI-C Compiler
; Version 3.9.5 #11479 (MINGW32)
;--------------------------------------------------------
.module main
.optsdcc -mstm8
A bug in live-range shortening, moving the return before the assignment to the variable. Subsequent optimizations then removed the now unreachable assignment.
Fixed in [11481].
Now it's ok but there's extra y register usage:
; .\Source\main.c: 390: uint16_t testadd()
; -----------------------------------------
; function testadd
; -----------------------------------------
_testadd:
; .\Source\main.c: 392: return (val += 1);
ldw x, _val+0
incw x
ldw y, x
ldw _val+0, y
; .\Source\main.c: 393: }
ret