optimized swap()

This commit is contained in:
Irmen de Jong 2020-11-15 18:04:54 +01:00
parent 574eb0d174
commit edf5e69d39
3 changed files with 52 additions and 27 deletions

View File

@ -641,8 +641,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
} }
} }
// all other types of swap() calls are done via the evaluation stack // all other types of swap() calls are done via a temporary variable
// TODO find alternative way to swap here without using estack
fun targetFromExpr(expr: Expression, datatype: DataType): AsmAssignTarget { fun targetFromExpr(expr: Expression, datatype: DataType): AsmAssignTarget {
return when (expr) { return when (expr) {
@ -653,24 +652,43 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
} }
} }
asmgen.translateExpression(first) val datatype = first.inferType(program).typeOrElse(DataType.STRUCT)
asmgen.translateExpression(second) when(datatype) {
val idatatype = first.inferType(program) in ByteDatatypes, in WordDatatypes -> {
if(!idatatype.isKnown) asmgen.assignExpressionToVariable(first, "P8ZP_SCRATCH_W1", datatype, null)
throw AssemblyError("unknown dt") asmgen.assignExpressionToVariable(second, "P8ZP_SCRATCH_W2", datatype, null)
val datatype = idatatype.typeOrElse(DataType.STRUCT) val assignFirst = AsmAssignment(
val assignFirst = AsmAssignment( AsmAssignSource(SourceStorageKind.VARIABLE, program, asmgen, datatype, variableAsmName = "P8ZP_SCRATCH_W2"),
AsmAssignSource(SourceStorageKind.STACK, program, asmgen, datatype), targetFromExpr(first, datatype),
targetFromExpr(first, datatype), false, first.position
false, first.position )
) val assignSecond = AsmAssignment(
val assignSecond = AsmAssignment( AsmAssignSource(SourceStorageKind.VARIABLE, program, asmgen, datatype, variableAsmName = "P8ZP_SCRATCH_W1"),
AsmAssignSource(SourceStorageKind.STACK, program, asmgen, datatype), targetFromExpr(second, datatype),
targetFromExpr(second, datatype), false, second.position
false, second.position )
) asmgen.translateNormalAssignment(assignFirst)
asmgen.translateNormalAssignment(assignFirst) asmgen.translateNormalAssignment(assignSecond)
asmgen.translateNormalAssignment(assignSecond) }
DataType.FLOAT -> {
// via evaluation stack
asmgen.translateExpression(first)
asmgen.translateExpression(second)
val assignFirst = AsmAssignment(
AsmAssignSource(SourceStorageKind.STACK, program, asmgen, DataType.FLOAT),
targetFromExpr(first, datatype),
false, first.position
)
val assignSecond = AsmAssignment(
AsmAssignSource(SourceStorageKind.STACK, program, asmgen, DataType.FLOAT),
targetFromExpr(second, datatype),
false, second.position
)
asmgen.translateNormalAssignment(assignFirst)
asmgen.translateNormalAssignment(assignSecond)
}
else -> throw AssemblyError("weird swap dt")
}
} }
private fun swapArrayValues(elementDt: DataType, arrayVarName1: String, indexValue1: NumericLiteralValue, arrayVarName2: String, indexValue2: NumericLiteralValue) { private fun swapArrayValues(elementDt: DataType, arrayVarName1: String, indexValue1: NumericLiteralValue, arrayVarName2: String, indexValue2: NumericLiteralValue) {

View File

@ -2,7 +2,6 @@
TODO TODO
==== ====
- use assignment utility functions in asmgen/assignmentAsmgen instead of creating AsmAssignment instances everywhere
- make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_' - make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_'
- option to load the built-in library files from a directory instead of the embedded ones (for easier library development/debugging) - option to load the built-in library files from a directory instead of the embedded ones (for easier library development/debugging)
- see if we can group some errors together for instance the (now single) errors about unidentified symbols - see if we can group some errors together for instance the (now single) errors about unidentified symbols

View File

@ -6,14 +6,22 @@ main {
sub start() { sub start() {
ubyte[] barr = [%10011111, %10011111] float f = 1.1
float[] farr = [2.2, 3.3]
rol2(barr[0]) floats.print_f(f)
ror2(barr[1])
txt.print_ubbin(barr[0],0)
txt.chrout('\n') txt.chrout('\n')
txt.print_ubbin(barr[1],0) floats.print_f(farr[0])
txt.chrout(',')
floats.print_f(farr[1])
txt.chrout('\n')
swap(f, farr[1])
floats.print_f(f)
txt.chrout('\n')
floats.print_f(farr[0])
txt.chrout(',')
floats.print_f(farr[1])
txt.chrout('\n') txt.chrout('\n')
testX() testX()