spin2cpp icon indicating copy to clipboard operation
spin2cpp copied to clipboard

Failed assignment when using .byte[n] syntax

Open ironsheep opened this issue 4 years ago • 3 comments

The following code works in pNut and PropTool:

PUB readMagWord(magRegister) : result16 | tmpOK, ok
    '' Read two bytes from MAG regAddr lo is 1st byte read, hi is 2nd byte read
    writeMagByte(M9DF_MAG_CNTL, $01)
    ' waitms(10); ' wait for mesurement complete - already done in writeMagByte()
    tmpOK, result16.byte[1] := readMagByte(magRegister + 1)
    ok := tmpOK
    tmpOK, result16.byte[0] := readMagByte(magRegister)
    ok |= tmpOK
    result16 signx= 16
    if bShowDebug
        term.fstr6(string("* readMagWord(0x%.02x) ok:%d, hi:0x%.02x, lo:0x%.02x, ret 0x%.04x(%d)\r\n"), magRegister, ok, result16.byte[1], result16.byte[0], result16, result16)

The following is a workaround: (assignment to intermediate variable then to variable using .byte[n] syntax)

PUB readMagWordGood(magRegister) : result16 | tmpOK, ok, result8
    '' Read two bytes from MAG regAddr lo is 1st byte read, hi is 2nd byte read
    writeMagByte(M9DF_MAG_CNTL, $01)
    ' waitms(10); ' wait for mesurement complete - already done in writeMagByte()
    tmpOK, result8 := readMagByte(magRegister + 1)
    result16.byte[1] := result8
    ok := tmpOK
    tmpOK, result8 := readMagByte(magRegister)
    result16.byte[0] := result8
    ok |= tmpOK
    result16 signx= 16
    if bShowDebug
        term.fstr6(string("* readMagWord(0x%.02x) ok:%d, hi:0x%.02x, lo:0x%.02x, ret 0x%.04x(%d)\r\n"), magRegister, ok, result16.byte[1], result16.byte[0], result16, result16)

ironsheep avatar Jun 01 '21 17:06 ironsheep

Any news on this fix?

ironsheep avatar Jun 10 '21 23:06 ironsheep

No, it will probably be a while before it can be fixed. For now continue to use the work-around.

totalspectrum avatar Jun 11 '21 00:06 totalspectrum

This actually does work now, but the generated code is poor, so I'm leaving it open for optimization.

totalspectrum avatar Nov 28 '21 13:11 totalspectrum

The output is at least correct now, and various optimization improvements over the years have helped with the poor code. The generated code can still be pretty meh sometimes, but probably that needs a separate bug.

totalspectrum avatar Jan 04 '23 17:01 totalspectrum