From 988a3e4446204de4b28cc1e2c29e241ba82d8280 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 31 Jan 2023 23:07:55 +0100 Subject: [PATCH] group the three Pt nodes that represent a variable in the p8 source under single interface IPtVariable --- codeCore/src/prog8/code/ast/AstStatements.kt | 12 +++++++++--- .../src/prog8/codegen/cpu6502/AsmGen.kt | 12 +++++++++--- .../codegen/cpu6502/BuiltinFunctionsAsmGen.kt | 18 ++++-------------- .../src/prog8/codegen/cpu6502/Extensions.kt | 4 ++-- .../prog8/codegen/cpu6502/ForLoopsAsmGen.kt | 7 ++----- .../codegen/cpu6502/FunctionCallAsmGen.kt | 2 +- .../cpu6502/assignment/AssignmentAsmGen.kt | 4 +--- 7 files changed, 28 insertions(+), 31 deletions(-) diff --git a/codeCore/src/prog8/code/ast/AstStatements.kt b/codeCore/src/prog8/code/ast/AstStatements.kt index 329281d6a..2cb29c771 100644 --- a/codeCore/src/prog8/code/ast/AstStatements.kt +++ b/codeCore/src/prog8/code/ast/AstStatements.kt @@ -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()}") } diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index e354ac3ef..ba4951b68 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -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) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 07e63f94e..f0d13c5f6 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -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!! diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt index efd4914a1..941c409a0 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/Extensions.kt @@ -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) } diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt index b62d09cf0..21a42764b 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ForLoopsAsmGen.kt @@ -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(""" diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt index 2d35e8df7..6499c7f9d 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/FunctionCallAsmGen.kt @@ -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()) } } diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 42c6eda1c..e09b0813a 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -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 -> {