Menu

#2956 Invalid code for compound assignment operator

closed-fixed
None
other
5
2019-11-30
2019-11-28
Nikolay
No

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

Discussion

  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
    • Category: STM8 --> other
     
  • Philipp Klaus Krause

    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].

     
  • Nikolay

    Nikolay - 2019-11-30

    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

     

Log in to post a comment.

MongoDB Logo MongoDB