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

View File

@ -2,7 +2,6 @@
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 '_'
- 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

View File

@ -6,14 +6,22 @@ main {
sub start() {
ubyte[] barr = [%10011111, %10011111]
float f = 1.1
float[] farr = [2.2, 3.3]
rol2(barr[0])
ror2(barr[1])
txt.print_ubbin(barr[0],0)
floats.print_f(f)
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')
testX()