#810 PIC16: shift left fails

closed-fixed
5
2013-05-25
2004-09-15
Raphael Neider
No

The following piece of code fails:

#pragma stack 0x100 60
char test(char target) {
return (1 << target);
}

int main() {
return test(4);
}

This is compiled into:
_test:
#LINE 2; shifttest.c char test(char target) {
MOVFF FSR2L, POSTDEC1
MOVFF FSR1L, FSR2L
MOVFF r0x2E, POSTDEC1
[1] MOVWF r0x2E
#LINE 3; shifttest.c return (1 << target);
[2] MOVF 0x01, W
[3] MOVWF r0x2E
COMF r0x2E, W
RRCF r0x2E, F

First problem (liveranges?):
[1]: Argument "target" is placed in r0x2E,
[3]: literal (!) 0x01 overwrites our argument...

Second problem:
[2]: a literal 0x01 is to be stored in WREG, but
register 0x01 is copied instead
This problem might be caused by gen.c:8603:
pic16_emitpcode(POC_MOVFW,
pic16_popGet(AOP(left),offset));
By replacing POC_MOVFW with POC_MOVLW this second
problem disappears. (But if this is correct in general,
we do not know...)

Everything done with SDCC pic16 2.4.4 #840 (Sep 13
2004) (UNIX).

Greetings,
Raphael Neider & Daniel Winkler

Discussion

  • Vangelis Rokas
    Vangelis Rokas
    2004-09-17

    • milestone: --> fixed
    • assigned_to: nobody --> vrokas
    • status: open --> closed-fixed
     
  • Vangelis Rokas
    Vangelis Rokas
    2004-09-17

    Logged In: YES
    user_id=770505

    Bug fixed in SDCC v.2.4.4 #843 .
    Code generator function for shift left did not inherently
    support cases where result and right (shift count) shared
    the same registers. Problem solved by modifying register
    allocator to always allocate different registers for result
    and right (and possibly left... but this needs extra
    optimization, work for the future! ... )