spin2cpp
spin2cpp copied to clipboard
Failed assignment when using .byte[n] syntax
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)
Any news on this fix?
No, it will probably be a while before it can be fixed. For now continue to use the work-around.
This actually does work now, but the generated code is poor, so I'm leaving it open for optimization.
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.