mirror of
https://github.com/irmen/prog8.git
synced 2025-01-11 13:29:45 +00:00
group the three Pt nodes that represent a variable in the p8 source under single interface IPtVariable
This commit is contained in:
parent
0f5cd22bb7
commit
988a3e4446
@ -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() {
|
override fun printProperties() {
|
||||||
print("$type $name")
|
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() {
|
override fun printProperties() {
|
||||||
print("$type $name = $value")
|
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() {
|
override fun printProperties() {
|
||||||
print("&$type $name = ${address.toHex()}")
|
print("&$type $name = ${address.toHex()}")
|
||||||
}
|
}
|
||||||
|
@ -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.
|
// 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) {
|
if(parameter!=null) {
|
||||||
val sub = parameter.definingAsmSub() ?: throw AssemblyError("push/pop arg passing only supported on asmsubs ${target.position}")
|
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}
|
val shouldKeepA = sub.parameters.any { it.second.registerOrPair==RegisterOrPair.AX || it.second.registerOrPair==RegisterOrPair.AY}
|
||||||
@ -2951,7 +2951,13 @@ $repeatLabel lda $counterVar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
if (dt in ByteDatatypes) {
|
||||||
out(" pla")
|
out(" pla")
|
||||||
assignRegister(RegisterOrPair.A, tgt)
|
assignRegister(RegisterOrPair.A, tgt)
|
||||||
|
@ -24,7 +24,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
|
|||||||
if(discardResult && resultToStack)
|
if(discardResult && resultToStack)
|
||||||
throw AssemblyError("cannot both discard the result AND put it onto stack")
|
throw AssemblyError("cannot both discard the result AND put it onto stack")
|
||||||
|
|
||||||
val sscope = fcall.definingISub()!!
|
val sscope = fcall.definingISub()
|
||||||
|
|
||||||
when (fcall.name) {
|
when (fcall.name) {
|
||||||
"msb" -> funcMsb(fcall, resultToStack, resultRegister)
|
"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}"
|
"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 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())
|
asmgen.popCpuStack(DataType.UBYTE, target!!, fcall.definingISub())
|
||||||
}
|
}
|
||||||
"popw" -> {
|
"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}"
|
"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 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())
|
asmgen.popCpuStack(DataType.UWORD, target!!, fcall.definingISub())
|
||||||
}
|
}
|
||||||
"rsave" -> funcRsave()
|
"rsave" -> funcRsave()
|
||||||
@ -359,7 +349,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
|
|||||||
private fun funcReverse(fcall: PtBuiltinFunctionCall) {
|
private fun funcReverse(fcall: PtBuiltinFunctionCall) {
|
||||||
val variable = fcall.args.single()
|
val variable = fcall.args.single()
|
||||||
if (variable is PtIdentifier) {
|
if (variable is PtIdentifier) {
|
||||||
val decl = variable.targetVarDecl(program)!!
|
val decl = variable.targetVarDecl(program) as PtVariable
|
||||||
val varName = asmgen.asmVariableName(variable)
|
val varName = asmgen.asmVariableName(variable)
|
||||||
val numElements = decl.arraySize!!
|
val numElements = decl.arraySize!!
|
||||||
when (decl.type) {
|
when (decl.type) {
|
||||||
@ -398,7 +388,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
|
|||||||
private fun funcSort(fcall: PtBuiltinFunctionCall) {
|
private fun funcSort(fcall: PtBuiltinFunctionCall) {
|
||||||
val variable = fcall.args.single()
|
val variable = fcall.args.single()
|
||||||
if (variable is PtIdentifier) {
|
if (variable is PtIdentifier) {
|
||||||
val decl = variable.targetVarDecl(program)!!
|
val decl = variable.targetVarDecl(program) as PtVariable
|
||||||
val varName = asmgen.asmVariableName(variable)
|
val varName = asmgen.asmVariableName(variable)
|
||||||
val numElements = decl.arraySize!!
|
val numElements = decl.arraySize!!
|
||||||
when (decl.type) {
|
when (decl.type) {
|
||||||
@ -1017,7 +1007,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
|
|||||||
private fun outputAddressAndLenghtOfArray(arg: PtExpression) {
|
private fun outputAddressAndLenghtOfArray(arg: PtExpression) {
|
||||||
// address in P8ZP_SCRATCH_W1, number of elements in A
|
// address in P8ZP_SCRATCH_W1, number of elements in A
|
||||||
arg as PtIdentifier
|
arg as PtIdentifier
|
||||||
val arrayVar = arg.targetVarDecl(program)!!
|
val arrayVar = arg.targetVarDecl(program)!! as PtVariable
|
||||||
if(arrayVar.arraySize==null)
|
if(arrayVar.arraySize==null)
|
||||||
throw AssemblyError("length of non-array requested")
|
throw AssemblyError("length of non-array requested")
|
||||||
val size = arrayVar.arraySize!!
|
val size = arrayVar.arraySize!!
|
||||||
|
@ -108,8 +108,8 @@ internal fun PtProgram.lookup(name: String): PtNode {
|
|||||||
return remainder.fold(this as PtNode) { acc, namePart -> searchLocalSymbol(acc, namePart)!! }
|
return remainder.fold(this as PtNode) { acc, namePart -> searchLocalSymbol(acc, namePart)!! }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun PtIdentifier.targetVarDecl(program: PtProgram): PtVariable? =
|
internal fun PtIdentifier.targetVarDecl(program: PtProgram): IPtVariable? =
|
||||||
this.targetStatement(program) as? PtVariable
|
this.targetStatement(program) as? IPtVariable
|
||||||
|
|
||||||
internal fun IPtSubroutine.regXasResult(): Boolean =
|
internal fun IPtSubroutine.regXasResult(): Boolean =
|
||||||
(this is PtAsmSub) && this.retvalRegisters.any { it.registerOrPair in arrayOf(RegisterOrPair.X, RegisterOrPair.AX, RegisterOrPair.XY) }
|
(this is PtAsmSub) && this.retvalRegisters.any { it.registerOrPair in arrayOf(RegisterOrPair.X, RegisterOrPair.AX, RegisterOrPair.XY) }
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package prog8.codegen.cpu6502
|
package prog8.codegen.cpu6502
|
||||||
|
|
||||||
import com.github.michaelbull.result.fold
|
import com.github.michaelbull.result.fold
|
||||||
import prog8.code.ast.PtForLoop
|
import prog8.code.ast.*
|
||||||
import prog8.code.ast.PtIdentifier
|
|
||||||
import prog8.code.ast.PtProgram
|
|
||||||
import prog8.code.ast.PtRange
|
|
||||||
import prog8.code.core.*
|
import prog8.code.core.*
|
||||||
import kotlin.math.absoluteValue
|
import kotlin.math.absoluteValue
|
||||||
|
|
||||||
@ -242,7 +239,7 @@ $endLabel""")
|
|||||||
val endLabel = asmgen.makeLabel("for_end")
|
val endLabel = asmgen.makeLabel("for_end")
|
||||||
asmgen.loopEndLabels.push(endLabel)
|
asmgen.loopEndLabels.push(endLabel)
|
||||||
val iterableName = asmgen.asmVariableName(ident)
|
val iterableName = asmgen.asmVariableName(ident)
|
||||||
val decl = ident.targetVarDecl(program)!!
|
val decl = ident.targetVarDecl(program)!! as PtVariable
|
||||||
when(iterableDt) {
|
when(iterableDt) {
|
||||||
DataType.STR -> {
|
DataType.STR -> {
|
||||||
asmgen.out("""
|
asmgen.out("""
|
||||||
|
@ -133,7 +133,7 @@ internal class FunctionCallAsmGen(private val program: PtProgram, private val as
|
|||||||
val param = callee.parameters[it]
|
val param = callee.parameters[it]
|
||||||
TODO("pop cpu stack into asmsub param ${param.first.name} ${param.second}")
|
TODO("pop cpu stack into asmsub param ${param.first.name} ${param.second}")
|
||||||
// val targetVar = callee.searchParameter(param.name)!!
|
// val targetVar = callee.searchParameter(param.name)!!
|
||||||
// asmgen.popCpuStack(param.type, targetVar, call.definingISub())
|
// asmgen.popCpuStack(param.first.type, targetVar, call.definingISub())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -806,9 +806,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
|
|||||||
|
|
||||||
private fun containmentCheckIntoA(containment: PtContainmentCheck) {
|
private fun containmentCheckIntoA(containment: PtContainmentCheck) {
|
||||||
val elementDt = containment.element.type
|
val elementDt = containment.element.type
|
||||||
val variable = (containment.iterable as? PtIdentifier)?.targetVarDecl(program)
|
val variable = (containment.iterable as? PtIdentifier)?.targetVarDecl(program) as PtVariable
|
||||||
?: throw AssemblyError("invalid containment iterable type")
|
|
||||||
|
|
||||||
val varname = asmgen.asmVariableName(containment.iterable)
|
val varname = asmgen.asmVariableName(containment.iterable)
|
||||||
when(variable.type) {
|
when(variable.type) {
|
||||||
DataType.STR -> {
|
DataType.STR -> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user