From 10bf7f5d07e142cf18cced36a58a5c0b04df4c21 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 4 Jun 2022 19:43:53 +0200 Subject: [PATCH] fix: again gives proper name redefinition errors in same scope --- .../codegen/cpu6502/BuiltinFunctionsAsmGen.kt | 2 + .../astprocessing/AstIdentifiersChecker.kt | 14 ++- .../astprocessing/AstOnetimeTransforms.kt | 7 +- docs/source/todo.rst | 4 +- examples/test.p8 | 108 ++++++++++-------- 5 files changed, 80 insertions(+), 55 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 3e5c5452d..c695f8d61 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -512,6 +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.assignExpressionToRegister(ptrAndIndex.second, RegisterOrPair.X) asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine!!) asmgen.assignExpressionToRegister(ptrAndIndex.first, RegisterOrPair.AY) @@ -613,6 +614,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, } else { val ptrAndIndex = asmgen.pointerViaIndexRegisterPossible(what.addressExpression) if(ptrAndIndex!=null) { + // TODO FIX X CORRUPTION asmgen.assignExpressionToRegister(ptrAndIndex.second, RegisterOrPair.X) asmgen.saveRegisterLocal(CpuRegister.X, (fcall as Node).definingSubroutine!!) asmgen.assignExpressionToRegister(ptrAndIndex.first, RegisterOrPair.AY) diff --git a/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt index b7cfe8c37..c1379810e 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt @@ -54,12 +54,16 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter, if(decl.name in compTarget.machine.opcodeNames) errors.err("can't use a cpu opcode name as a symbol: '${decl.name}'", decl.position) - val existing = decl.parent.definingScope.lookup(listOf(decl.name)) - if (existing != null && existing !== decl && existing is VarDecl) { - if(existing.parent!==decl.parent) - nameShadowWarning(decl.name, decl.position, existing) + val existingInSameScope = decl.definingScope.lookup(listOf(decl.name)) + if(existingInSameScope!=null && existingInSameScope!==decl) + nameError(decl.name, decl.position, existingInSameScope) + + val existingOuter = decl.parent.definingScope.lookup(listOf(decl.name)) + if (existingOuter != null && existingOuter !== decl && existingOuter is VarDecl) { + if(existingOuter.parent!==decl.parent) + nameShadowWarning(decl.name, decl.position, existingOuter) else - nameError(decl.name, decl.position, existing) + nameError(decl.name, decl.position, existingOuter) } if(decl.definingBlock.name==decl.name) diff --git a/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt b/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt index 17102229c..15ca750e8 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt @@ -6,6 +6,7 @@ import prog8.ast.Program import prog8.ast.expressions.ArrayIndexedExpression import prog8.ast.expressions.BinaryExpression import prog8.ast.expressions.DirectMemoryRead +import prog8.ast.expressions.Expression import prog8.ast.statements.AssignTarget import prog8.ast.statements.DirectMemoryWrite import prog8.ast.statements.VarDecl @@ -36,7 +37,11 @@ internal class AstOnetimeTransforms(private val program: Program, private val op if(arrayVar!=null && arrayVar.datatype == DataType.UWORD) { // rewrite pointervar[index] into @(pointervar+index) val indexer = arrayIndexedExpression.indexer - val add = BinaryExpression(arrayIndexedExpression.arrayvar.copy(), "+", indexer.indexExpr, arrayIndexedExpression.position) + val add: Expression = + if(indexer.indexExpr.constValue(program)?.number==0.0) + arrayIndexedExpression.arrayvar.copy() + else + BinaryExpression(arrayIndexedExpression.arrayvar.copy(), "+", indexer.indexExpr, arrayIndexedExpression.position) if(parent is AssignTarget) { // we're part of the target of an assignment, we have to actually change the assign target itself val memwrite = DirectMemoryWrite(add, arrayIndexedExpression.position) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 384b35b82..bac05032c 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,10 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- BUG in 6502 codegen: ubyte one = 1 | value = one+one+one+one+one is not 5 +- 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. - add McCarthy evaluation to shortcircuit and/or expressions. First do ifs by splitting them up? Then do expressions that compute a value? diff --git a/examples/test.p8 b/examples/test.p8 index de18b4ffe..4e22631fd 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,4 +1,5 @@ %import textio +%import test_stack %zeropage basicsafe @@ -30,65 +31,76 @@ main { sub start() { ; mcCarthy() + test_stack.test() + ubyte value = 0 - ubyte size = 9 + ubyte one = 1 ubyte[10] data = [11,22,33,4,5,6,7,8,9,10] uword bitmapbuf = &data - 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 + rol(bitmapbuf[1]) + ror(bitmapbuf[1]) + value = one+one+one + txt.print_ub(value) ; 3 txt.nl() + test_stack.test() - ; 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() - ubyte one = 1 - 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]) +; 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() - ; 2 1 3 - txt.print_ub(bitmapbuf[0]) - txt.spc() - txt.print_ub(bitmapbuf[1]) - txt.spc() - txt.print_ub(bitmapbuf[2]) - txt.nl() +; value = one+one+one+one+one +; txt.print_ub(value) ; 5 +; txt.nl() - for value in data { - txt.print_ub(value) - txt.spc() - } - 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":