This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/MSP-CGT: BUG: Incorrect optimisation with mixed short/long arithmetic.

Part Number: MSP-CGT

Tool/software: TI C/C++ Compiler

If I have the following function:

extern unsigned short a, b;
extern unsigned long l;

void func(void)
{
    l += a;
l -= b; }

The the compiler produces the following incorrect output:

;*****************************************************************************
;* FUNCTION NAME: func                                                       *
;*                                                                           *
;*   Regs Modified     : SP,SR,r15                                           *
;*   Regs Used         : SP,SR,r15                                           *
;*   Local Frame Size  : 0 Args + 0 Auto + 0 Save = 0 byte                   *
;*****************************************************************************
func:
;* --------------------------------------------------------------------------*
        .dwcfi  cfa_offset, 4
        .dwcfi  save_reg_to_mem, 16, -4
;** 7   -----------------------    l += a-b;
        .dwpsn  file "../src/longaddsub.c",line 7,column 5,is_stmt,isa 0
        MOV.W     &a+0,r15              ; [] |7|
        SUB.W     &b+0,r15              ; [] |7|
        ADD.W     r15,&l+0              ; [] |7|
        ADDC.W    #0,&l+2               ; [] |7|
;**     -----------------------    return;
$C$DW$5 .dwtag  DW_TAG_TI_branch
        .dwattr $C$DW$5, DW_AT_low_pc(0x00)
        .dwattr $C$DW$5, DW_AT_TI_return

        RETA      ; []
        ; []
        .dwattr $C$DW$4, DW_AT_TI_end_file("../src/longaddsub.c")
.dwattr $C$DW$4, DW_AT_TI_end_line(0x09) .dwattr $C$DW$4, DW_AT_TI_end_column(0x01) .dwendentry .dwendtag $C$DW$4

This result is incorrect because it calculates the difference of the two short values as a short value and then adds the result.  Consider the case where `a=0` and `b=1`, then as written the result should be to subtract 1 from `l`, but as compiled it adds 65535 to `l` instead.