non-optimized typecast assignments now attempt to not use evalstack

This commit is contained in:
Irmen de Jong 2021-11-05 23:25:07 +01:00
parent 23961f695d
commit 1d2d217b94
5 changed files with 40 additions and 30 deletions

View File

@ -11,9 +11,12 @@ import prog8.compiler.target.C64Target
import prog8.compiler.target.Cx16Target import prog8.compiler.target.Cx16Target
import prog8.compiler.target.cbm.AssemblyProgram import prog8.compiler.target.cbm.AssemblyProgram
import prog8.compiler.target.cbm.loadAsmIncludeFile import prog8.compiler.target.cbm.loadAsmIncludeFile
import prog8.compiler.target.cpu6502.codegen.assignment.*
import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignSource
import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignTarget import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignTarget
import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignment import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignment
import prog8.compiler.target.cpu6502.codegen.assignment.AssignmentAsmGen import prog8.compiler.target.cpu6502.codegen.assignment.AssignmentAsmGen
import prog8.compiler.target.cpu6502.codegen.assignment.SourceStorageKind
import prog8.compilerinterface.* import prog8.compilerinterface.*
import prog8.parser.SourceCode import prog8.parser.SourceCode
import java.nio.file.Path import java.nio.file.Path
@ -872,6 +875,30 @@ class AsmGen(private val program: Program,
} }
} }
internal fun assignExpressionTo(value: Expression, target: AsmAssignTarget) {
// don't use asmgen.translateExpression() to avoid evalstack
when (target.datatype) {
in ByteDatatypes -> {
assignExpressionToRegister(value, RegisterOrPair.A)
assignRegister(RegisterOrPair.A, target)
}
in WordDatatypes, in PassByReferenceDatatypes -> {
assignExpressionToRegister(value, RegisterOrPair.AY)
translateNormalAssignment(
AsmAssignment(
AsmAssignSource(SourceStorageKind.REGISTER, program, this, target.datatype, register=RegisterOrPair.AY),
target, false, program.memsizer, value.position
)
)
}
DataType.FLOAT -> {
assignExpressionToRegister(value, RegisterOrPair.FAC1)
assignRegister(RegisterOrPair.FAC1, target)
}
else -> throw AssemblyError("weird dt ${target.datatype}")
}
}
private fun translateSubroutine(sub: Subroutine) { private fun translateSubroutine(sub: Subroutine) {
var onlyVariables = false var onlyVariables = false

View File

@ -13,7 +13,6 @@ import prog8.compiler.target.AssemblyError
import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignSource import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignSource
import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignTarget import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignTarget
import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignment import prog8.compiler.target.cpu6502.codegen.assignment.AsmAssignment
import prog8.compiler.target.cpu6502.codegen.assignment.SourceStorageKind
import prog8.compiler.target.cpu6502.codegen.assignment.TargetStorageKind import prog8.compiler.target.cpu6502.codegen.assignment.TargetStorageKind
import prog8.compilerinterface.CpuType import prog8.compilerinterface.CpuType
@ -148,28 +147,10 @@ internal class FunctionCallAsmGen(private val program: Program, private val asmg
if(arg.isSimple) { // TODO FOR ALL ARG TYPES? if(arg.isSimple) { // TODO FOR ALL ARG TYPES?
// note this stuff below is needed to (eventually) avoid calling asmgen.translateExpression() // note this stuff below is needed to (eventually) avoid calling asmgen.translateExpression()
// TODO but This STILL requires the translateNormalAssignment() to be fixed to avoid stack eval for expressions... // TODO but This STILL requires the translateNormalAssignment() to be fixed to avoid stack eval for expressions...
// println("*** ALT PARAM PASSING FOR ASMSUB $stmt $arg") // TODO DEBUG println("*** ALT PARAM PASSING FOR ASMSUB $stmt $arg") // TODO DEBUG
when (val dt = arg.inferType(program).getOr(DataType.UNDEFINED)) { val dt = arg.inferType(program).getOr(DataType.UNDEFINED)
in ByteDatatypes -> { val target = AsmAssignTarget(TargetStorageKind.STACK, program, asmgen, dt, sub)
asmgen.assignExpressionToRegister(arg, RegisterOrPair.A) asmgen.assignExpressionTo(arg, target)
asmgen.assignRegister(RegisterOrPair.A, AsmAssignTarget(TargetStorageKind.STACK, program, asmgen, dt, sub))
}
in WordDatatypes, in PassByReferenceDatatypes -> {
asmgen.assignExpressionToRegister(arg, RegisterOrPair.AY)
asmgen.translateNormalAssignment(
AsmAssignment(
AsmAssignSource(SourceStorageKind.REGISTER, program, asmgen, dt, register=RegisterOrPair.AY),
AsmAssignTarget(TargetStorageKind.STACK, program, asmgen, dt, sub),
false, program.memsizer, arg.position
)
)
}
DataType.FLOAT -> {
asmgen.assignExpressionToRegister(arg, RegisterOrPair.FAC1)
asmgen.assignRegister(RegisterOrPair.FAC1, AsmAssignTarget(TargetStorageKind.STACK, program, asmgen, dt, sub))
}
else -> throw AssemblyError("weird dt $dt")
}
} else { } else {
asmgen.translateExpression(arg) // TODO GET RID OF THIS, if the above actually produces compact code asmgen.translateExpression(arg) // TODO GET RID OF THIS, if the above actually produces compact code
} }

View File

@ -427,11 +427,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
} }
} }
// give up, do it via eval stack // No more special optmized cases yet. Do the rest via more complex evaluation
// note: cannot use assignTypeCastedValue because that is ourselves :P // note: cannot use assignTypeCastedValue because that is ourselves :P
// TODO optimize typecasts for more special cases? asmgen.assignExpressionTo(origTypeCastExpression, target)
asmgen.translateExpression(origTypeCastExpression) // this performs the actual type cast in translateExpression(Typecast)
assignStackValue(target)
} }
private fun assignCastViaLsbFunc(value: Expression, target: AsmAssignTarget) { private fun assignCastViaLsbFunc(value: Expression, target: AsmAssignTarget) {

View File

@ -181,8 +181,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
} }
} }
else -> { else -> {
// TODO OTHER EVALUATION HERE
asmgen.translateExpression(memory.addressExpression) asmgen.translateExpression(memory.addressExpression)
asmgen.out(" jsr prog8_lib.read_byte_from_address_on_stack | sta P8ZP_SCRATCH_B1") asmgen.out(" jsr prog8_lib.read_byte_from_address_on_stack | sta P8ZP_SCRATCH_B1") // TODO don't use estack to transfer the address to read from
when { when {
valueLv != null -> inplaceModification_byte_litval_to_variable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, valueLv.toInt()) valueLv != null -> inplaceModification_byte_litval_to_variable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, valueLv.toInt())
ident != null -> inplaceModification_byte_variable_to_variable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, ident) ident != null -> inplaceModification_byte_variable_to_variable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, ident)
@ -193,7 +194,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
} }
else -> inplaceModification_byte_value_to_variable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value) else -> inplaceModification_byte_value_to_variable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value)
} }
asmgen.out(" lda P8ZP_SCRATCH_B1 | jsr prog8_lib.write_byte_to_address_on_stack | inx") asmgen.out(" lda P8ZP_SCRATCH_B1 | jsr prog8_lib.write_byte_to_address_on_stack | inx") // TODO don't use estack to transfer the address to read from
} }
} }
} }

View File

@ -1,3 +1,4 @@
%import textio
%zeropage basicsafe %zeropage basicsafe
main { main {
@ -5,6 +6,8 @@ main {
uword xx=$2000 uword xx=$2000
ubyte yy=30 ubyte yy=30
ubyte zz=9 ubyte zz=9
sys.memset(xx+200, yy*2, zz+yy) ; sys.memset(xx+200, yy*2, zz+yy)
@($d020) = (xx+(yy*zz)) as ubyte
} }
} }