compiler shouldn't use cx16.r15 as temp var

This commit is contained in:
Irmen de Jong 2021-11-14 02:38:59 +01:00
parent f0f52b9166
commit c858ceeb58
5 changed files with 20 additions and 33 deletions

View File

@ -75,10 +75,10 @@ X = BinExpr X = LeftExpr
// Once it's outside the typecast, the regular splitting can commence.
val tempDt = origExpr.inferType(program).getOr(DataType.UNDEFINED)
val tempVar = when(tempDt) {
DataType.UBYTE -> listOf("cx16", "r15L")
DataType.BYTE -> listOf("cx16", "r15sL")
DataType.UWORD -> listOf("cx16", "r15")
DataType.WORD -> listOf("cx16", "r15s")
DataType.UBYTE -> listOf("prog8_lib", "retval_interm_ub")
DataType.BYTE -> listOf("prog8_lib", "retval_interm_b")
DataType.UWORD -> listOf("prog8_lib", "retval_interm_uw")
DataType.WORD -> listOf("prog8_lib", "retval_interm_w")
DataType.FLOAT -> listOf("floats", "tempvar_swap_float")
else -> throw FatalAstException("invalid dt $tempDt")
}
@ -86,7 +86,6 @@ X = BinExpr X = LeftExpr
AssignTarget(IdentifierReference(tempVar, typecast.position), null, null, typecast.position),
typecast.expression, typecast.position
)
println("UNWRAP TYPECAST: ${assignment.position}") // TODO DEBUG
return listOf(
IAstModification.InsertBefore(assignment, assignTempVar, parent as IStatementContainer),
IAstModification.ReplaceNode(typecast.expression, IdentifierReference(tempVar, typecast.position), typecast)

View File

@ -242,8 +242,8 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o
if(!expr.left.isSimple) {
val dt = expr.left.inferType(program)
val name = when {
dt.istype(DataType.UBYTE) -> listOf("cx16","r15L")
dt.istype(DataType.UWORD) -> listOf("cx16","r15")
dt.istype(DataType.UBYTE) -> listOf("cx16","r9L") // assume (hope) cx16.r9 isn't used for anything else...
dt.istype(DataType.UWORD) -> listOf("cx16","r9") // assume (hope) cx16.r9 isn't used for anything else...
dt.istype(DataType.BYTE) -> listOf("prog8_lib","retval_interm_b")
dt.istype(DataType.WORD) -> listOf("prog8_lib","retval_interm_w")
else -> throw AssemblyError("invalid dt")
@ -409,10 +409,10 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o
val modifications = mutableListOf<IAstModification>()
val statement = expr.containingStatement
val dt = expr.indexer.indexExpr.inferType(program)
val register = if(dt istype DataType.UBYTE || dt istype DataType.BYTE ) "r9L" else "r9"
val register = if(dt istype DataType.UBYTE || dt istype DataType.BYTE ) "retval_interm_ub" else "retval_interm_b"
// replace the indexer with just the variable (simply use a cx16 virtual register r9, that we HOPE is not used for other things in the expression...)
// assign the indexing expression to the helper variable, but only if that hasn't been done already
val target = AssignTarget(IdentifierReference(listOf("cx16", register), expr.indexer.position), null, null, expr.indexer.position)
val target = AssignTarget(IdentifierReference(listOf("prog8_lib", register), expr.indexer.position), null, null, expr.indexer.position)
val assign = Assignment(target, expr.indexer.indexExpr, expr.indexer.position)
modifications.add(IAstModification.InsertBefore(statement, assign, statement.parent as IStatementContainer))
modifications.add(IAstModification.ReplaceNode(expr.indexer.indexExpr, target.identifier!!.copy(), expr.indexer))

View File

@ -224,21 +224,21 @@ class TestOptimization: FunSpec({
ubyte r
r = 0
ubyte bb
cx16.r15sL = cos8(r)
cx16.r15sL >>= 1
cx16.r15sL += 100
bb = cx16.r15sL
prog8_lib.retval_interm_b = cos8(r)
prog8_lib.retval_interm_b >>= 1
prog8_lib.retval_interm_b += 100
bb = prog8_lib.retval_interm_b
return
*/
val st = result.program.entrypoint.statements
st.size shouldBe 8
st.last() shouldBe instanceOf<Return>()
var assign = st[3] as Assignment
assign.target.identifier!!.nameInSource shouldBe listOf("cx16","r15sL")
assign.target.identifier!!.nameInSource shouldBe listOf("prog8_lib","retval_interm_b")
assign = st[4] as Assignment
assign.target.identifier!!.nameInSource shouldBe listOf("cx16","r15sL")
assign.target.identifier!!.nameInSource shouldBe listOf("prog8_lib","retval_interm_b")
assign = st[5] as Assignment
assign.target.identifier!!.nameInSource shouldBe listOf("cx16","r15sL")
assign.target.identifier!!.nameInSource shouldBe listOf("prog8_lib","retval_interm_b")
assign = st[6] as Assignment
assign.target.identifier!!.nameInSource shouldBe listOf("bb")
}

View File

@ -3,7 +3,6 @@ TODO
For next compiler release (7.3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- fix compiler crashing on imageviewer (add unit tests)
- add expression simplification to while and until loops as well.

View File

@ -5,26 +5,15 @@
main {
sub start() {
float[] farr = [1.111,2.222,3.333]
float[] farr = [1.111,2.222,3.333,4.444,5.555,6.666]
float f2 = 9.999
ubyte xx=1
ubyte yy=2
floats.print_f(f2)
floats.print_f(farr[3])
txt.nl()
floats.print_f(farr[0])
floats.print_f(farr[xx+yy])
txt.nl()
txt.nl()
swap(f2, farr[0])
floats.print_f(f2)
txt.nl()
floats.print_f(farr[0])
txt.nl()
txt.nl()
; ubyte bb
; uword ww
; uword @shared zz = not bb or not ww ; TODO WHY DOES THIS USE STACK EVAL-because it is a binaryexpression that isn't split
}