From 58d9c46a9bd91c0da8938784c17c46e12f67e946 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 26 Nov 2021 20:56:30 +0100 Subject: [PATCH] got rid of old makeScopedName routine --- .../compiler/target/cpu6502/codegen/AsmGen.kt | 9 ++++---- .../codegen/assignment/AssignmentAsmGen.kt | 2 +- .../src/prog8/optimizer/StatementOptimizer.kt | 15 ++++++++----- .../src/prog8/ast/statements/AstStatements.kt | 22 +------------------ examples/test.p8 | 7 +++--- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 0e144d9be..9e7936737 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -265,7 +265,7 @@ class AsmGen(private val program: Program, for(variable in variables) { if(blockname=="prog8_lib" && variable.name.startsWith("P8ZP_SCRATCH_")) continue // the "hooks" to the temp vars are not generated as new variables - val fullName = variable.makeScopedName(variable.name) + val fullName = variable.scopedName.joinToString(".") val zpVar = allocatedZeropageVariables[fullName] if(zpVar==null) { // This var is not on the ZP yet. Attempt to move it there (if it's not a float, those take up too much space) @@ -403,7 +403,8 @@ class AsmGen(private val program: Program, val blockname = inBlock?.name vars.filter{ it.datatype != DataType.STR }.sortedBy { it.datatype }.forEach { - if(it.makeScopedName(it.name) !in allocatedZeropageVariables) { + val scopedname = it.scopedName.joinToString(".") + if(scopedname !in allocatedZeropageVariables) { if(blockname!="prog8_lib" || !it.name.startsWith("P8ZP_SCRATCH_")) // the "hooks" to the temp vars are not generated as new variables vardecl2asm(it) } @@ -581,7 +582,7 @@ class AsmGen(private val program: Program, } is VarDecl -> { val sourceName = asmVariableName(pointervar) - val scopedName = target.makeScopedName(target.name) + val scopedName = target.scopedName.joinToString(".") if (isTargetCpu(CpuType.CPU65c02)) { return if (isZpVar(scopedName)) { // pointervar is already in the zero page, no need to copy @@ -1485,7 +1486,7 @@ $label nop""") internal fun isZpVar(variable: IdentifierReference): Boolean { val vardecl = variable.targetVarDecl(program)!! - return vardecl.makeScopedName(vardecl.name) in allocatedZeropageVariables + return vardecl.scopedName.joinToString(".") in allocatedZeropageVariables } internal fun jmp(asmLabel: String) { diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AssignmentAsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AssignmentAsmGen.kt index 7838052ad..c023af335 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AssignmentAsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AssignmentAsmGen.kt @@ -2148,7 +2148,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen fun storeAIntoPointerVar(pointervar: IdentifierReference) { val sourceName = asmgen.asmVariableName(pointervar) val vardecl = pointervar.targetVarDecl(program)!! - val scopedName = vardecl.makeScopedName(vardecl.name) + val scopedName = vardecl.scopedName.joinToString(".") if (asmgen.isTargetCpu(CpuType.CPU65c02)) { if (asmgen.isZpVar(scopedName)) { // pointervar is already in the zero page, no need to copy diff --git a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt index ce5ef5067..0f753513d 100644 --- a/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/StatementOptimizer.kt @@ -25,13 +25,19 @@ class StatementOptimizer(private val program: Program, override fun before(functionCall: FunctionCall, parent: Node): Iterable { // if the first instruction in the called subroutine is a return statement with a simple value, // remove the jump altogeter and inline the returnvalue directly. + + fun scopePrefix(variable: IdentifierReference): IdentifierReference { + val target = variable.targetStatement(program) as INamedStatement + return IdentifierReference(target.scopedName, variable.position) + } + val subroutine = functionCall.target.targetSubroutine(program) if(subroutine!=null) { val first = subroutine.statements.asSequence().filterNot { it is VarDecl || it is Directive }.firstOrNull() if(first is Return && first.value?.isSimple==true) { val copy = when(val orig = first.value!!) { is AddressOf -> { - val scoped = scopePrefix(orig.identifier, subroutine) + val scoped = scopePrefix(orig.identifier) AddressOf(scoped, orig.position) } is DirectMemoryRead -> { @@ -40,7 +46,7 @@ class StatementOptimizer(private val program: Program, else -> return noModifications } } - is IdentifierReference -> scopePrefix(orig, subroutine) + is IdentifierReference -> scopePrefix(orig) is NumericLiteralValue -> orig.copy() is StringLiteralValue -> orig.copy() else -> return noModifications @@ -51,10 +57,7 @@ class StatementOptimizer(private val program: Program, return noModifications } - private fun scopePrefix(variable: IdentifierReference, subroutine: Subroutine): IdentifierReference { - val scoped = subroutine.makeScopedName(variable.nameInSource.last()) - return IdentifierReference(scoped.split('.'), variable.position) - } + override fun after(functionCallStatement: FunctionCallStatement, parent: Node): Iterable { if(functionCallStatement.target.targetStatement(program) is BuiltinFunctionStatementPlaceholder) { diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index 1e55d670d..09b03fcd6 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -29,27 +29,7 @@ sealed class Statement : Node { abstract fun accept(visitor: IAstVisitor) abstract fun accept(visitor: AstWalker, parent: Node) - @Deprecated("get rid of this in favor of INamedStatement.scopedName") // TODO - fun makeScopedName(name: String): String { - // easy way out is to always return the full scoped name. - // it would be nicer to find only the minimal prefixed scoped name, but that's too much hassle for now. - // and like this, we can cache the name even, - // like in a lazy property on the statement object itself (label, subroutine, vardecl) - val scope = mutableListOf() - var statementScope = this.parent - while(statementScope !is ParentSentinel && statementScope !is Module) { - if(statementScope is INameScope) { - scope.add(0, statementScope.name) - } - statementScope = statementScope.parent - } - if(name.isNotEmpty()) - scope.add(name) - return scope.joinToString(".") - } - - - fun nextSibling(): Statement? { +` fun nextSibling(): Statement? { val statements = (parent as? IStatementContainer)?.statements ?: return null val nextIdx = statements.indexOfFirst { it===this } + 1 return if(nextIdx < statements.size) diff --git a/examples/test.p8 b/examples/test.p8 index 4891695b3..e9e68381f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,7 +7,7 @@ main { uword xx ubyte yy = 33 - void sin8u(yy) + xx=xx-routine(2) ; concat_string(random_name()) ; ubyte xx=20 @@ -39,9 +39,10 @@ main { return name } + ubyte qqq sub routine(ubyte r1arg) -> ubyte { - r1arg++ - return r1arg + return qqq + return main.start.yy } asmsub routine2(ubyte r2arg @ A) {