News:

Let's find out together what makes a PIC Tick!

Main Menu

18F27J53 compiler bug

Started by JonW, Nov 25, 2026, 04:17 PM

Previous topic - Next topic

JonW

Hi Les,

A code-generation bug found on an 18F27J53 while testing Trastikata's USB library.

Line: nWriteMask = nWriteMask << pIndex
(nWriteMask Word at 0x0BA, bank 0; pIndex Byte at 0x1BD, bank 1).  Original file and trace fixes with bug are here FILES HERE

WRITE-UP Here (write-up).  Ran with Optimiser = 0 and still remained, thus I am more confident it's a bug.

Generated code:

    movlb 0x01
    movf  pIndex,W,1
    bz    _sllb__3        ; count 0 -> skip loop, BSR still 1
_sllb__2
    movlb 0x00            ; bank switch emitted INSIDE the loop
    bcf   STATUS,0,0
    rlcf  nWriteMask,F,1
    rlcf  nWriteMaskH,F,1
    decfsz WREG,F,0
    bra   _sllb__2
_sllb__3                  ; compiler assumes bank 0 here
    movf  USB_NetRxPending,W,1   ; reads 0x1B6, not 0x0B6
    andwf nWriteMask,W,1         ; reads 0x1BA, not 0x0BA

With a count of 1 or more the loop runs its movlb 0x00 and all is fine. With a count of 0 the loop is skipped, BSR stays on the count's bank, and the next statement reads the wrong bank. Same result at Optimiser_Level 0. The movlb needs to be before the bz (or after _sllb__3).

Traced on the bench through a UART: mask 1, pending 1, AND result 0. Build-3 .asm attached if you want it.

Cheers,
Jon

▲