diff --git a/codeCore/src/prog8/code/SymbolTableMaker.kt b/codeCore/src/prog8/code/SymbolTableMaker.kt index f7ecf33c2..ad57077e1 100644 --- a/codeCore/src/prog8/code/SymbolTableMaker.kt +++ b/codeCore/src/prog8/code/SymbolTableMaker.kt @@ -29,6 +29,7 @@ class SymbolTableMaker(private val program: PtProgram, private val options: Comp PtMemMapped("P8ESTACK_LO", DataType.ARRAY_UB, options.compTarget.machine.ESTACK_LO, 256u, Position.DUMMY), PtMemMapped("P8ESTACK_HI", DataType.ARRAY_UB, options.compTarget.machine.ESTACK_HI, 256u, Position.DUMMY) ).forEach { + it.parent = program st.add(StMemVar(it.name, it.type, it.address, null, it)) } } diff --git a/codeCore/src/prog8/code/ast/AstStatements.kt b/codeCore/src/prog8/code/ast/AstStatements.kt index 51bd89fdd..ae5e94af5 100644 --- a/codeCore/src/prog8/code/ast/AstStatements.kt +++ b/codeCore/src/prog8/code/ast/AstStatements.kt @@ -38,6 +38,7 @@ class PtSub( throw AssemblyError("non-numeric parameter") if(returntype!=null && returntype !in NumericDatatypes) throw AssemblyError("non-numeric returntype $returntype") + parameters.forEach { it.parent=this } } } @@ -155,6 +156,10 @@ class PtJump(val identifier: PtIdentifier?, if(address!=null) print(address.toHex()) if(generatedLabel!=null) print(generatedLabel) } + + init { + identifier?.let {it.parent = this } + } } @@ -202,6 +207,9 @@ class PtVariable(name: String, override val type: DataType, val zeropage: Zeropa override fun printProperties() { print("$type $name") } + init { + value?.let {it.parent=this} + } } diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 81629b9b7..de6990b26 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -91,8 +91,27 @@ class AsmGen( fun asmVariableName(name: String) = fixNameSymbols(name) fun asmSymbolName(name: Iterable) = fixNameSymbols(name.joinToString(".")) fun asmVariableName(name: Iterable) = fixNameSymbols(name.joinToString(".")) - fun asmSymbolName(identifier: PtIdentifier) = asmSymbolName(identifier.name) - fun asmVariableName(identifier: PtIdentifier) = asmVariableName(identifier.name) + fun asmSymbolName(identifier: PtIdentifier): String { + val name = asmSymbolName(identifier.name) + + // see if we're inside a subroutine, if so, remove the whole prefix and just make the variable name locally scoped (64tass scopes it to the proper .proc block) + val subName = identifier.definingSub()?.scopedName + return if (subName != null && name.length>subName.length && name.startsWith(subName) && name[subName.length] == '.') + name.drop(subName.length + 1) + else + name + } + + fun asmVariableName(identifier: PtIdentifier): String { + val name = asmVariableName(identifier.name) + + // see if we're inside a subroutine, if so, remove the whole prefix and just make the variable name locally scoped (64tass scopes it to the proper .proc block) + val subName = identifier.definingSub()?.scopedName + return if (subName != null && name.length>subName.length && name.startsWith(subName) && name[subName.length] == '.') + name.drop(subName.length+1) + else + name + } internal fun getTempVarName(dt: DataType): String { return when(dt) { @@ -847,6 +866,7 @@ $repeatLabel lda $counterVar // all else take its address and assign that also to AY register pair val addrofValue = PtAddressOf(returnvalue.position) addrofValue.add(returnvalue as PtIdentifier) + addrofValue.parent = ret.parent assignmentAsmGen.assignExpressionToRegister(addrofValue, returnReg.registerOrPair!!, false) } } diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index ca4339177..50586bc3f 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -328,6 +328,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, val slabname = PtIdentifier("prog8_slabs.prog8_memoryslab_$name", DataType.UWORD, fcall.position) val addressOf = PtAddressOf(fcall.position) addressOf.add(slabname) + addressOf.parent = fcall val src = AsmAssignSource(SourceStorageKind.EXPRESSION, program, asmgen, DataType.UWORD, expression = addressOf) val target = if(resultToStack) @@ -631,6 +632,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, } else { val addressOf = PtAddressOf(arrayvar.position) addressOf.add(arrayvar) + addressOf.parent = arrayvar.parent.parent asmgen.assignExpressionToVariable(addressOf, "prog8_lib.${operation}_array_u${dt}._arg_target", DataType.UWORD, null) } asmgen.assignExpressionToVariable(indexer.index, "prog8_lib.${operation}_array_u${dt}._arg_index", DataType.UBYTE, null) @@ -1035,6 +1037,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, is PtIdentifier -> { val addr = PtAddressOf(value.position) addr.add(value) + addr.parent = call AsmAssignSource.fromAstSource(addr, program, asmgen) } is PtNumber -> { @@ -1048,6 +1051,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, val variable = PtIdentifier(subroutineFloatEvalResultVar2, DataType.FLOAT, value.position) val addr = PtAddressOf(value.position) addr.add(variable) + addr.parent = call asmgen.assignExpressionToVariable(value, asmgen.asmVariableName(variable), DataType.FLOAT, scope) AsmAssignSource.fromAstSource(addr, program, asmgen) } @@ -1067,6 +1071,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, // put the address of the argument in AY val addr = PtAddressOf(value.position) addr.add(value) + addr.parent = call AsmAssignSource.fromAstSource(addr, program, asmgen) } else -> { @@ -1084,6 +1089,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram, // put the address of the argument in AY val addr = PtAddressOf(value.position) addr.add(value) + addr.parent = call AsmAssignSource.fromAstSource(addr, program, asmgen) } else -> { diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt index 9391af44a..aad9ff9bf 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt @@ -213,6 +213,7 @@ internal class FunctionCallAsmGen(private val program: PtProgram, private val as if(value is PtIdentifier) { val addr = PtAddressOf(Position.DUMMY) addr.add(value) + addr.parent = sub as PtNode AsmAssignSource.fromAstSource(addr, program, asmgen).adjustSignedUnsigned(target) } else { AsmAssignSource.fromAstSource(value, program, asmgen).adjustSignedUnsigned(target) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index cfcf3efe9..b868bed79 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -312,7 +312,7 @@ internal class ProgramAndVarsGen( asmgen.out("; simple int arg(s) passed via register(s)") if(sub.parameters.size==1) { val dt = sub.parameters[0].type - val target = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, dt, sub, variableAsmName = sub.parameters[0].scopedName) + val target = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, dt, sub, variableAsmName = sub.parameters[0].name) if(dt in ByteDatatypes) asmgen.assignRegister(RegisterOrPair.A, target) else @@ -320,8 +320,8 @@ internal class ProgramAndVarsGen( } else { require(sub.parameters.size==2) // 2 simple byte args, first in A, second in Y - val target1 = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, sub.parameters[0].type, sub, variableAsmName = sub.parameters[0].scopedName) - val target2 = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, sub.parameters[1].type, sub, variableAsmName = sub.parameters[1].scopedName) + val target1 = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, sub.parameters[0].type, sub, variableAsmName = sub.parameters[0].name) + val target2 = AsmAssignTarget(TargetStorageKind.VARIABLE, asmgen, sub.parameters[1].type, sub, variableAsmName = sub.parameters[1].name) asmgen.assignRegister(RegisterOrPair.A, target1) asmgen.assignRegister(RegisterOrPair.Y, target2) } diff --git a/compiler/test/codegeneration/TestAsmGenSymbols.kt b/compiler/test/codegeneration/TestAsmGenSymbols.kt index 0eb7f549f..60cb3809a 100644 --- a/compiler/test/codegeneration/TestAsmGenSymbols.kt +++ b/compiler/test/codegeneration/TestAsmGenSymbols.kt @@ -13,6 +13,7 @@ import prog8.code.SymbolTableMaker import prog8.code.ast.PtAddressOf import prog8.code.ast.PtAssignment import prog8.code.ast.PtIdentifier +import prog8.code.ast.PtProgram import prog8.code.core.* import prog8.code.target.C64Target import prog8.code.target.VMTarget @@ -98,11 +99,11 @@ class TestAsmGenSymbols: StringSpec({ val sub = asmgen.program.entrypoint()!! val localvarIdent = sub.children.asSequence().filterIsInstance().first { it.value is PtIdentifier }.value as PtIdentifier - asmgen.asmSymbolName(localvarIdent) shouldBe "main.start.localvar" - asmgen.asmVariableName(localvarIdent) shouldBe "main.start.localvar" + asmgen.asmSymbolName(localvarIdent) shouldBe "localvar" + asmgen.asmVariableName(localvarIdent) shouldBe "localvar" val localvarIdentScoped = (sub.children.asSequence().filterIsInstance().first { (it.value as? PtAddressOf)?.identifier?.name=="main.start.localvar" }.value as PtAddressOf).identifier - asmgen.asmSymbolName(localvarIdentScoped) shouldBe "main.start.localvar" - asmgen.asmVariableName(localvarIdentScoped) shouldBe "main.start.localvar" + asmgen.asmSymbolName(localvarIdentScoped) shouldBe "localvar" + asmgen.asmVariableName(localvarIdentScoped) shouldBe "localvar" val scopedVarIdent = (sub.children.asSequence().filterIsInstance().first { (it.value as? PtAddressOf)?.identifier?.name=="main.var_outside" }.value as PtAddressOf).identifier asmgen.asmSymbolName(scopedVarIdent) shouldBe "main.var_outside" @@ -118,11 +119,11 @@ class TestAsmGenSymbols: StringSpec({ val sub = asmgen.program.entrypoint()!! val localLabelIdent = (sub.children.asSequence().filterIsInstance().first { (it.value as? PtAddressOf)?.identifier?.name=="main.start.locallabel" }.value as PtAddressOf).identifier - asmgen.asmSymbolName(localLabelIdent) shouldBe "main.start.locallabel" - asmgen.asmVariableName(localLabelIdent) shouldBe "main.start.locallabel" + asmgen.asmSymbolName(localLabelIdent) shouldBe "locallabel" + asmgen.asmVariableName(localLabelIdent) shouldBe "locallabel" val localLabelIdentScoped = (sub.children.asSequence().filterIsInstance().first { (it.value as? PtAddressOf)?.identifier?.name=="main.start.locallabel" }.value as PtAddressOf).identifier - asmgen.asmSymbolName(localLabelIdentScoped) shouldBe "main.start.locallabel" - asmgen.asmVariableName(localLabelIdentScoped) shouldBe "main.start.locallabel" + asmgen.asmSymbolName(localLabelIdentScoped) shouldBe "locallabel" + asmgen.asmVariableName(localLabelIdentScoped) shouldBe "locallabel" val scopedLabelIdent = (sub.children.asSequence().filterIsInstance().first { (it.value as? PtAddressOf)?.identifier?.name=="main.label_outside" }.value as PtAddressOf).identifier asmgen.asmSymbolName(scopedLabelIdent) shouldBe "main.label_outside" @@ -150,6 +151,8 @@ main { asmgen.asmSymbolName(listOf("prog8_lib","P8ZP_SCRATCH_W2")) shouldBe "P8ZP_SCRATCH_W2" val id1 = PtIdentifier("prog8_lib.P8ZP_SCRATCH_REG", DataType.UBYTE, Position.DUMMY) val id2 = PtIdentifier("prog8_lib.P8ZP_SCRATCH_W2", DataType.UWORD, Position.DUMMY) + id1.parent = PtProgram("test", DummyMemsizer, DummyStringEncoder) + id2.parent = PtProgram("test", DummyMemsizer, DummyStringEncoder) asmgen.asmSymbolName(id1) shouldBe "P8ZP_SCRATCH_REG" asmgen.asmSymbolName(id2) shouldBe "P8ZP_SCRATCH_W2" }