diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt index 466adc2b1..858043685 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt @@ -319,6 +319,52 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen return } + if(origTypeCastExpression.type == DataType.UBYTE) { + val parentTc = origTypeCastExpression.parent as? TypecastExpression + if(parentTc!=null && parentTc.type==DataType.UWORD) { + // typecast something to ubyte and directly back to uword + // generate code for lsb(value) here instead of the ubyte typecast + return assignCastViaLsbFunc(value, target) + } + } + + if(valueDt==DataType.UBYTE) { + when(target.register) { + RegisterOrPair.A, + RegisterOrPair.X, + RegisterOrPair.Y -> { + // 'cast' a ubyte value to a byte register; no cast needed at all + return assignExpressionToRegister(value, target.register) + } + RegisterOrPair.AX, + RegisterOrPair.AY, + RegisterOrPair.XY, + in Cx16VirtualRegisters -> { + // cast an ubyte value to a 16 bits register, just assign it and make use of the value extension + return assignExpressionToRegister(value, target.register!!) + } + else -> {} + } + } else if(valueDt==DataType.UWORD) { + when(target.register) { + RegisterOrPair.A, + RegisterOrPair.X, + RegisterOrPair.Y -> { + // cast an uword to a byte register, do this via lsb(value) + // generate code for lsb(value) here instead of the ubyte typecast + return assignCastViaLsbFunc(value, target) + } + RegisterOrPair.AX, + RegisterOrPair.AY, + RegisterOrPair.XY, + in Cx16VirtualRegisters -> { + // 'cast' uword into a 16 bits register, just assign it + return assignExpressionToRegister(value, target.register!!) + } + else -> {} + } + } + // give up, do it via eval stack // TODO optimize typecasts for more special cases? // note: cannot use assignTypeCastedValue because that is ourselves :P @@ -328,6 +374,14 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen assignStackValue(target) } + private fun assignCastViaLsbFunc(value: Expression, target: AsmAssignTarget) { + val lsb = FunctionCall(IdentifierReference(listOf("lsb"), value.position), mutableListOf(value), value.position) + lsb.linkParents(value.parent) + val src = AsmAssignSource(SourceStorageKind.EXPRESSION, program, asmgen, DataType.UBYTE, expression = lsb) + val assign = AsmAssignment(src, target, false, value.position) + translateNormalAssignment(assign) + } + private fun assignTypecastedFloatFAC1(targetAsmVarName: String, targetDt: DataType) { if(targetDt==DataType.FLOAT) diff --git a/examples/cx16/cube3d.p8 b/examples/cx16/cube3d.p8 index b16ba90ca..d4b8a3d37 100644 --- a/examples/cx16/cube3d.p8 +++ b/examples/cx16/cube3d.p8 @@ -18,7 +18,6 @@ main { uword anglex uword angley uword anglez - ubyte timer_jiffies repeat { rotate_vertices(msb(anglex), msb(angley), msb(anglez)) diff --git a/examples/cx16/datetime.p8 b/examples/cx16/datetime.p8 index fa89dc9df..d5e670846 100644 --- a/examples/cx16/datetime.p8 +++ b/examples/cx16/datetime.p8 @@ -14,7 +14,7 @@ main { repeat { c64.CHROUT(19) ; HOME txt.print("\n yyyy-mm-dd HH:MM:SS.jj\n\n") - cx16.clock_get_date_time() + void cx16.clock_get_date_time() c64.CHROUT(' ') print_date() c64.CHROUT(' ') diff --git a/examples/test.p8 b/examples/test.p8 index e413c6eb8..473f85b6c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,43 +1,13 @@ %import test_stack %import textio -%import floats %zeropage basicsafe %option no_sysinit main { - sub start () { - uword cnt - ubyte ub - - - ; TODO differences between: - - repeat cnt as ubyte { ; TODO this goes via stack - ub++ - } - - repeat lsb(cnt) { ; TODO this doesnt - ub++ - } - - - ; TODO stack based evaluation for this function call even when it's inlined: - next_pixel((cnt as ubyte) + 30) - - test_stack.test() } - - - inline asmsub next_pixel(ubyte color @A) { - %asm {{ - nop - }} - } - - }