From dbb92881a1211e138623ddbe4501bb8c6f6afad5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 4 Jun 2022 21:10:48 +0200 Subject: [PATCH] fixed X register corruption in some cases of rol() and ror() --- .../codegen/cpu6502/BuiltinFunctionsAsmGen.kt | 6 +- docs/source/todo.rst | 3 - examples/test.p8 | 113 ++++++++---------- 3 files changed, 57 insertions(+), 65 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index c695f8d61..fa0e2d5e2 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -512,7 +512,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, } else { val ptrAndIndex = asmgen.pointerViaIndexRegisterPossible(what.addressExpression) if(ptrAndIndex!=null) { - // TODO FIX X REGISTER CORRUPTION + asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine!!) asmgen.assignExpressionToRegister(ptrAndIndex.second, RegisterOrPair.X) asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine!!) asmgen.assignExpressionToRegister(ptrAndIndex.first, RegisterOrPair.AY) @@ -521,6 +521,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, sta (+) + 1 sty (+) + 2 + ror ${'$'}ffff,x ; modified""") + asmgen.restoreRegisterLocal(CpuRegister.X) } else { asmgen.assignExpressionToRegister(what.addressExpression, RegisterOrPair.AY) asmgen.out(""" @@ -614,7 +615,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, } else { val ptrAndIndex = asmgen.pointerViaIndexRegisterPossible(what.addressExpression) if(ptrAndIndex!=null) { - // TODO FIX X CORRUPTION + asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine!!) asmgen.assignExpressionToRegister(ptrAndIndex.second, RegisterOrPair.X) asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine!!) asmgen.assignExpressionToRegister(ptrAndIndex.first, RegisterOrPair.AY) @@ -623,6 +624,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, sta (+) + 1 sty (+) + 2 + rol ${'$'}ffff,x ; modified""") + asmgen.restoreRegisterLocal(CpuRegister.X) } else { asmgen.assignExpressionToRegister(what.addressExpression, RegisterOrPair.AY) asmgen.out(""" diff --git a/docs/source/todo.rst b/docs/source/todo.rst index bac05032c..99891de3f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,9 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- BUG in 6502 codegen: ubyte one = 1 | value = one+one+one+one+one is not 5 if rol(ptr[1]) is inbetween - caused by X register corruption in the ROL and ROR code - - add unit test for name error and name shadowing warning. - optimize pointervar indexing codegen: writing (all sorts of things) - pipe operator: (targets other than 'Virtual'): allow non-unary function calls in the pipe that specify the other argument(s) in the calls. Already working for VM target. diff --git a/examples/test.p8 b/examples/test.p8 index 4e22631fd..40281a10e 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -39,70 +39,63 @@ main { ubyte[10] data = [11,22,33,4,5,6,7,8,9,10] uword bitmapbuf = &data - rol(bitmapbuf[1]) - ror(bitmapbuf[1]) - value = one+one+one - txt.print_ub(value) ; 3 + value = bitmapbuf[2] + txt.print_ub(value) ;; 33 txt.nl() + + ; 11 22 33 + txt.print_ub(bitmapbuf[0]) + txt.spc() + txt.print_ub(bitmapbuf[1]) + txt.spc() + txt.print_ub(bitmapbuf[2]) + txt.nl() + rol(bitmapbuf[0]) + rol(bitmapbuf[0]) + txt.print_ub(bitmapbuf[0]) ; 44 + txt.spc() + ror(bitmapbuf[0]) + ror(bitmapbuf[0]) + txt.print_ub(bitmapbuf[0]) ; 11 + txt.nl() + + ; 22 44 66 + txt.print_ub(bitmapbuf[0]*2) + txt.spc() + txt.print_ub(bitmapbuf[1]*2) + txt.spc() + txt.print_ub(bitmapbuf[2]*2) + txt.nl() + + value = one+one+one+one+one + txt.print_ub(value) ; 5 + txt.nl() + + bitmapbuf[0] = one + bitmapbuf[1] = one+one + bitmapbuf[2] = one+one+one + bitmapbuf[2] += 4 + bitmapbuf[2] -= 2 + bitmapbuf[2] -= 2 + swap(bitmapbuf[0], bitmapbuf[1]) + + ; 2 1 3 + txt.print_ub(bitmapbuf[0]) + txt.spc() + txt.print_ub(bitmapbuf[1]) + txt.spc() + txt.print_ub(bitmapbuf[2]) + txt.nl() + + for value in data { + txt.print_ub(value) + txt.spc() + } + txt.nl() + test_stack.test() - -; value = bitmapbuf[2] -; txt.print_ub(value) ;; 33 -; txt.nl() -; -; ; 11 22 33 -; txt.print_ub(bitmapbuf[0]) -; txt.spc() -; txt.print_ub(bitmapbuf[1]) -; txt.spc() -; txt.print_ub(bitmapbuf[2]) -; txt.nl() -; rol(bitmapbuf[0]) -; rol(bitmapbuf[0]) -; txt.print_ub(bitmapbuf[0]) ; 44 -; txt.spc() -; ror(bitmapbuf[0]) -; ror(bitmapbuf[0]) -; txt.print_ub(bitmapbuf[0]) ; 11 -; txt.nl() -; -; ; 22 44 66 -; txt.print_ub(bitmapbuf[0]*2) -; txt.spc() -; txt.print_ub(bitmapbuf[1]*2) -; txt.spc() -; txt.print_ub(bitmapbuf[2]*2) -; txt.nl() - -; value = one+one+one+one+one -; txt.print_ub(value) ; 5 -; txt.nl() - -; bitmapbuf[0] = one -; bitmapbuf[1] = one+one -; bitmapbuf[2] = one+one+one -; bitmapbuf[2] += 4 -; bitmapbuf[2] -= 2 -; bitmapbuf[2] -= 2 -; swap(bitmapbuf[0], bitmapbuf[1]) -; -; ; 2 1 3 -; txt.print_ub(bitmapbuf[0]) -; txt.spc() -; txt.print_ub(bitmapbuf[1]) -; txt.spc() -; txt.print_ub(bitmapbuf[2]) -; txt.nl() -; -; for value in data { -; txt.print_ub(value) -; txt.spc() -; } -; txt.nl() - - ; ; a "pixelshader": ; sys.gfx_enable(0) ; enable lo res screen ; ubyte shifter