got rid of old makeScopedName routine

This commit is contained in:
Irmen de Jong 2021-11-26 20:56:30 +01:00
parent e4648e2138
commit 58d9c46a9b
5 changed files with 20 additions and 35 deletions

View File

@ -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) {

View File

@ -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

View File

@ -25,13 +25,19 @@ class StatementOptimizer(private val program: Program,
override fun before(functionCall: FunctionCall, parent: Node): Iterable<IAstModification> {
// 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<IAstModification> {
if(functionCallStatement.target.targetStatement(program) is BuiltinFunctionStatementPlaceholder) {

View File

@ -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<String>()
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)

View File

@ -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) {