local varnames and fix uninitialized parents

This commit is contained in:
Irmen de Jong 2023-02-12 14:26:19 +01:00
parent 75bd66326a
commit 4914609485
7 changed files with 52 additions and 13 deletions

View File

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

View File

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

View File

@ -91,8 +91,27 @@ class AsmGen(
fun asmVariableName(name: String) = fixNameSymbols(name)
fun asmSymbolName(name: Iterable<String>) = fixNameSymbols(name.joinToString("."))
fun asmVariableName(name: Iterable<String>) = 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)
}
}

View File

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

View File

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

View File

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

View File

@ -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<PtAssignment>().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<PtAssignment>().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<PtAssignment>().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<PtAssignment>().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<PtAssignment>().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<PtAssignment>().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"
}