group the three Pt nodes that represent a variable in the p8 source under single interface IPtVariable

This commit is contained in:
Irmen de Jong 2023-01-31 23:07:55 +01:00
parent 0f5cd22bb7
commit 988a3e4446
7 changed files with 28 additions and 31 deletions

View File

@ -194,21 +194,27 @@ class PtReturn(position: Position) : PtNode(position) {
}
class PtVariable(name: String, val type: DataType, var value: PtExpression?, var arraySize: UInt?, position: Position) : PtNamedNode(name, position) {
sealed interface IPtVariable {
val name: String
val type: DataType
}
class PtVariable(name: String, override val type: DataType, var value: PtExpression?, var arraySize: UInt?, position: Position) : PtNamedNode(name, position), IPtVariable {
override fun printProperties() {
print("$type $name")
}
}
class PtConstant(name: String, val type: DataType, val value: Double, position: Position) : PtNamedNode(name, position) {
class PtConstant(name: String, override val type: DataType, val value: Double, position: Position) : PtNamedNode(name, position), IPtVariable {
override fun printProperties() {
print("$type $name = $value")
}
}
class PtMemMapped(name: String, val type: DataType, val address: UInt, position: Position) : PtNamedNode(name, position) {
class PtMemMapped(name: String, override val type: DataType, val address: UInt, position: Position) : PtNamedNode(name, position), IPtVariable {
override fun printProperties() {
print("&$type $name = ${address.toHex()}")
}

View File

@ -2875,9 +2875,9 @@ $repeatLabel lda $counterVar
}
}
internal fun popCpuStack(dt: DataType, target: PtVariable, scope: IPtSubroutine?) {
internal fun popCpuStack(dt: DataType, target: IPtVariable, scope: IPtSubroutine?) {
// note: because A is pushed first so popped last, saving A is often not required here.
val parameter = target.definingSub()?.parameters?.singleOrNull { it.name===target.name }
val parameter = (target as PtNode).definingSub()?.parameters?.singleOrNull { it.name===target.name }
if(parameter!=null) {
val sub = parameter.definingAsmSub() ?: throw AssemblyError("push/pop arg passing only supported on asmsubs ${target.position}")
val shouldKeepA = sub.parameters.any { it.second.registerOrPair==RegisterOrPair.AX || it.second.registerOrPair==RegisterOrPair.AY}
@ -2951,7 +2951,13 @@ $repeatLabel lda $counterVar
}
}
} else {
val tgt = AsmAssignTarget(TargetStorageKind.VARIABLE, this, target.type, scope, variableAsmName = asmVariableName(target.scopedName))
val scopedName = when(target) {
is PtConstant -> target.scopedName
is PtMemMapped -> target.scopedName
is PtVariable -> target.scopedName
else -> throw AssemblyError("weird target var")
}
val tgt = AsmAssignTarget(TargetStorageKind.VARIABLE, this, target.type, scope, variableAsmName = asmVariableName(scopedName))
if (dt in ByteDatatypes) {
out(" pla")
assignRegister(RegisterOrPair.A, tgt)

View File

@ -24,7 +24,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
if(discardResult && resultToStack)
throw AssemblyError("cannot both discard the result AND put it onto stack")
val sscope = fcall.definingISub()!!
val sscope = fcall.definingISub()
when (fcall.name) {
"msb" -> funcMsb(fcall, resultToStack, resultRegister)
@ -53,11 +53,6 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
"attempt to pop a value into a differently typed variable, or in something else that isn't supported ${fcall.position}"
}
val target = (fcall.args[0] as PtIdentifier).targetVarDecl(program)
val target2 = (fcall.args[0] as PtIdentifier).targetStatement(program)
if(target2==null)
TODO("huh1")
if(target==null)
TODO("huh2")
asmgen.popCpuStack(DataType.UBYTE, target!!, fcall.definingISub())
}
"popw" -> {
@ -65,11 +60,6 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
"attempt to pop a value into a differently typed variable, or in something else that isn't supported ${fcall.position}"
}
val target = (fcall.args[0] as PtIdentifier).targetVarDecl(program)
val target2 = (fcall.args[0] as PtIdentifier).targetStatement(program)
if(target2==null)
TODO("huh1")
if(target==null)
TODO("huh2")
asmgen.popCpuStack(DataType.UWORD, target!!, fcall.definingISub())
}
"rsave" -> funcRsave()
@ -359,7 +349,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
private fun funcReverse(fcall: PtBuiltinFunctionCall) {
val variable = fcall.args.single()
if (variable is PtIdentifier) {
val decl = variable.targetVarDecl(program)!!
val decl = variable.targetVarDecl(program) as PtVariable
val varName = asmgen.asmVariableName(variable)
val numElements = decl.arraySize!!
when (decl.type) {
@ -398,7 +388,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
private fun funcSort(fcall: PtBuiltinFunctionCall) {
val variable = fcall.args.single()
if (variable is PtIdentifier) {
val decl = variable.targetVarDecl(program)!!
val decl = variable.targetVarDecl(program) as PtVariable
val varName = asmgen.asmVariableName(variable)
val numElements = decl.arraySize!!
when (decl.type) {
@ -1017,7 +1007,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
private fun outputAddressAndLenghtOfArray(arg: PtExpression) {
// address in P8ZP_SCRATCH_W1, number of elements in A
arg as PtIdentifier
val arrayVar = arg.targetVarDecl(program)!!
val arrayVar = arg.targetVarDecl(program)!! as PtVariable
if(arrayVar.arraySize==null)
throw AssemblyError("length of non-array requested")
val size = arrayVar.arraySize!!

View File

@ -108,8 +108,8 @@ internal fun PtProgram.lookup(name: String): PtNode {
return remainder.fold(this as PtNode) { acc, namePart -> searchLocalSymbol(acc, namePart)!! }
}
internal fun PtIdentifier.targetVarDecl(program: PtProgram): PtVariable? =
this.targetStatement(program) as? PtVariable
internal fun PtIdentifier.targetVarDecl(program: PtProgram): IPtVariable? =
this.targetStatement(program) as? IPtVariable
internal fun IPtSubroutine.regXasResult(): Boolean =
(this is PtAsmSub) && this.retvalRegisters.any { it.registerOrPair in arrayOf(RegisterOrPair.X, RegisterOrPair.AX, RegisterOrPair.XY) }

View File

@ -1,10 +1,7 @@
package prog8.codegen.cpu6502
import com.github.michaelbull.result.fold
import prog8.code.ast.PtForLoop
import prog8.code.ast.PtIdentifier
import prog8.code.ast.PtProgram
import prog8.code.ast.PtRange
import prog8.code.ast.*
import prog8.code.core.*
import kotlin.math.absoluteValue
@ -242,7 +239,7 @@ $endLabel""")
val endLabel = asmgen.makeLabel("for_end")
asmgen.loopEndLabels.push(endLabel)
val iterableName = asmgen.asmVariableName(ident)
val decl = ident.targetVarDecl(program)!!
val decl = ident.targetVarDecl(program)!! as PtVariable
when(iterableDt) {
DataType.STR -> {
asmgen.out("""

View File

@ -133,7 +133,7 @@ internal class FunctionCallAsmGen(private val program: PtProgram, private val as
val param = callee.parameters[it]
TODO("pop cpu stack into asmsub param ${param.first.name} ${param.second}")
// val targetVar = callee.searchParameter(param.name)!!
// asmgen.popCpuStack(param.type, targetVar, call.definingISub())
// asmgen.popCpuStack(param.first.type, targetVar, call.definingISub())
}
}

View File

@ -806,9 +806,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
private fun containmentCheckIntoA(containment: PtContainmentCheck) {
val elementDt = containment.element.type
val variable = (containment.iterable as? PtIdentifier)?.targetVarDecl(program)
?: throw AssemblyError("invalid containment iterable type")
val variable = (containment.iterable as? PtIdentifier)?.targetVarDecl(program) as PtVariable
val varname = asmgen.asmVariableName(containment.iterable)
when(variable.type) {
DataType.STR -> {