expr operands assignment refactor

This commit is contained in:
Irmen de Jong 2023-05-20 18:04:46 +02:00
parent 7215efe167
commit 125ce3240f
3 changed files with 71 additions and 255 deletions

View File

@ -1639,14 +1639,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.UBYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -1680,14 +1673,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.UBYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -1723,15 +1709,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleRightOperands(left, right, ::code)) if(wordJumpForSimpleRightOperands(left, right, ::code))
return return
if(left.isSimple()) { assignWordOperandsToAYAndVar(left, right, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD)
assignExpressionToRegister(left, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.UWORD, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_less_uw | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_less_uw | beq $jumpIfFalseLabel")
} }
@ -1769,15 +1747,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleRightOperands(left, right, ::code)) if(wordJumpForSimpleRightOperands(left, right, ::code))
return return
if(left.isSimple()) { assignWordOperandsToAYAndVar(left, right, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.WORD)
assignExpressionToRegister(left, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.WORD, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.WORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_less_w | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_less_w | beq $jumpIfFalseLabel")
} }
@ -1816,14 +1786,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.UBYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -1859,14 +1822,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.BYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.BYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.BYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -1908,15 +1864,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleRightOperands(left, right, ::code)) if(wordJumpForSimpleRightOperands(left, right, ::code))
return return
if(left.isSimple()) { assignWordOperandsToAYAndVar(left, right, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD)
assignExpressionToRegister(left, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.UWORD, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return code("P8ZP_SCRATCH_W2+1", "P8ZP_SCRATCH_W2") return code("P8ZP_SCRATCH_W2+1", "P8ZP_SCRATCH_W2")
} }
@ -1959,15 +1907,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleLeftOperand(left, right, ::code)) if(wordJumpForSimpleLeftOperand(left, right, ::code))
return return
if(right.isSimple()) { assignWordOperandsToAYAndVar(right, left, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.WORD)
assignExpressionToRegister(right, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.WORD, right)
assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.WORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_less_w | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_less_w | beq $jumpIfFalseLabel")
} }
@ -2007,14 +1947,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.UBYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -2050,14 +1983,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.BYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.BYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.BYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -2101,15 +2027,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleRightOperands(left, right, ::code)) if(wordJumpForSimpleRightOperands(left, right, ::code))
return return
if(left.isSimple()) { assignWordOperandsToAYAndVar(left, right, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD)
assignExpressionToRegister(left, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.UWORD, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.UWORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_lesseq_uw | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_lesseq_uw | beq $jumpIfFalseLabel")
} }
@ -2156,15 +2074,7 @@ $repeatLabel lda $counterVar
return code(asmVariableName(left)) return code(asmVariableName(left))
} }
if(right.isSimple()) { assignWordOperandsToAYAndVar(right, left, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.WORD)
assignExpressionToRegister(right, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.WORD, right)
assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.WORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_lesseq_w | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_lesseq_w | beq $jumpIfFalseLabel")
} }
@ -2200,14 +2110,7 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
if(left.isSimple()) { assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
assignExpressionToRegister(left, RegisterOrPair.A)
} else {
pushCpuStack(DataType.UBYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE)
out(" pla")
}
return code("P8ZP_SCRATCH_B1") return code("P8ZP_SCRATCH_B1")
} }
@ -2240,15 +2143,31 @@ $repeatLabel lda $counterVar
if(byteJumpForSimpleRightOperand(left, right, ::code)) if(byteJumpForSimpleRightOperand(left, right, ::code))
return return
assignByteOperandsToAAndVar(left, right, "P8ZP_SCRATCH_B1")
return code("P8ZP_SCRATCH_B1")
}
internal fun assignByteOperandsToAAndVar(left: PtExpression, right: PtExpression, rightVarName: String) {
if(left.isSimple()) { if(left.isSimple()) {
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.BYTE) assignExpressionToVariable(right, rightVarName, DataType.UBYTE)
assignExpressionToRegister(left, RegisterOrPair.A) assignExpressionToRegister(left, RegisterOrPair.A)
} else { } else {
pushCpuStack(DataType.BYTE, left) pushCpuStack(DataType.UBYTE, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.BYTE) assignExpressionToVariable(right, rightVarName, DataType.UBYTE)
out(" pla") out(" pla")
} }
return code("P8ZP_SCRATCH_B1") }
internal fun assignWordOperandsToAYAndVar(left: PtExpression, right: PtExpression, rightVarname: String) {
if(left.isSimple()) {
assignExpressionToVariable(right, rightVarname, DataType.UWORD)
assignExpressionToRegister(left, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.UWORD, left)
assignExpressionToVariable(right, rightVarname, DataType.UWORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
} }
private fun translateUwordGreaterOrEqualOrJumpElsewhere(left: PtExpression, right: PtExpression, leftConstVal: PtNumber?, rightConstVal: PtNumber?, jumpIfFalseLabel: String) { private fun translateUwordGreaterOrEqualOrJumpElsewhere(left: PtExpression, right: PtExpression, leftConstVal: PtNumber?, rightConstVal: PtNumber?, jumpIfFalseLabel: String) {
@ -2282,15 +2201,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleRightOperands(left, right, ::code)) if(wordJumpForSimpleRightOperands(left, right, ::code))
return return
if(right.isSimple()) { assignWordOperandsToAYAndVar(right, left, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD)
assignExpressionToRegister(right, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.UWORD, right)
assignExpressionToVariable(left, "P8ZP_SCRATCH_W2", DataType.UWORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_lesseq_uw | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_lesseq_uw | beq $jumpIfFalseLabel")
} }
@ -2328,15 +2239,7 @@ $repeatLabel lda $counterVar
if(wordJumpForSimpleRightOperands(left, right, ::code)) if(wordJumpForSimpleRightOperands(left, right, ::code))
return return
if(left.isSimple()) { assignWordOperandsToAYAndVar(left, right, "P8ZP_SCRATCH_W2")
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.WORD)
assignExpressionToRegister(left, RegisterOrPair.AY)
} else {
pushCpuStack(DataType.WORD, left)
assignExpressionToVariable(right, "P8ZP_SCRATCH_W2", DataType.WORD)
restoreRegisterStack(CpuRegister.Y, false)
restoreRegisterStack(CpuRegister.A, false)
}
return out(" jsr prog8_lib.reg_lesseq_w | beq $jumpIfFalseLabel") return out(" jsr prog8_lib.reg_lesseq_w | beq $jumpIfFalseLabel")
} }

View File

@ -96,15 +96,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
} }
private fun funcDivmodW(fcall: PtBuiltinFunctionCall) { private fun funcDivmodW(fcall: PtBuiltinFunctionCall) {
if(fcall.args[1].isSimple()) { asmgen.assignWordOperandsToAYAndVar(fcall.args[1], fcall.args[0], "P8ZP_SCRATCH_W1")
asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AY, false)
} else {
asmgen.pushCpuStack(DataType.UWORD, fcall.args[1])
asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
// math.divmod_uw_asm: -- divide two unsigned words (16 bit each) into 16 bit results // math.divmod_uw_asm: -- divide two unsigned words (16 bit each) into 16 bit results
// input: P8ZP_SCRATCH_W1 in ZP: 16 bit number, A/Y: 16 bit divisor // input: P8ZP_SCRATCH_W1 in ZP: 16 bit number, A/Y: 16 bit divisor
// output: P8ZP_SCRATCH_W2 in ZP: 16 bit remainder, A/Y: 16 bit division result // output: P8ZP_SCRATCH_W2 in ZP: 16 bit remainder, A/Y: 16 bit division result
@ -121,15 +113,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
} }
private fun funcStringCompare(fcall: PtBuiltinFunctionCall, resultToStack: Boolean) { private fun funcStringCompare(fcall: PtBuiltinFunctionCall, resultToStack: Boolean) {
if(fcall.args[0].isSimple()) { asmgen.assignWordOperandsToAYAndVar(fcall.args[0], fcall.args[1], "P8ZP_SCRATCH_W2")
assignAsmGen.assignExpressionToVariable(fcall.args[1], "P8ZP_SCRATCH_W2", DataType.UWORD)
assignAsmGen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY, false)
} else {
asmgen.pushCpuStack(DataType.UWORD, fcall.args[0])
asmgen.assignExpressionToVariable(fcall.args[1], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
asmgen.out(" jsr prog8_lib.strcmp_mem") asmgen.out(" jsr prog8_lib.strcmp_mem")
if(resultToStack) if(resultToStack)
asmgen.out(" sta P8ESTACK_LO,x | dex") asmgen.out(" sta P8ESTACK_LO,x | dex")
@ -221,27 +205,13 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
asmgen.assignExpressionToRegister(arg1, RegisterOrPair.A) asmgen.assignExpressionToRegister(arg1, RegisterOrPair.A)
asmgen.out(" cmp ${arg2.address.asConstInteger()!!.toHex()}") asmgen.out(" cmp ${arg2.address.asConstInteger()!!.toHex()}")
} else { } else {
if(arg1.isSimple()) { asmgen.assignByteOperandsToAAndVar(arg1, arg2, "P8ZP_SCRATCH_B1")
asmgen.assignExpressionToVariable(arg2, "P8ZP_SCRATCH_B1", DataType.UBYTE) asmgen.out(" cmp P8ZP_SCRATCH_B1")
asmgen.assignExpressionToRegister(arg1, RegisterOrPair.A)
asmgen.out(" cmp P8ZP_SCRATCH_B1")
} else {
asmgen.pushCpuStack(DataType.UBYTE, arg1)
asmgen.assignExpressionToVariable(arg2, "P8ZP_SCRATCH_B1", DataType.UBYTE)
asmgen.out(" pla | cmp P8ZP_SCRATCH_B1")
}
} }
} }
else -> { else -> {
if(arg1.isSimple()) { asmgen.assignByteOperandsToAAndVar(arg1, arg2, "P8ZP_SCRATCH_B1")
asmgen.assignExpressionToVariable(arg2, "P8ZP_SCRATCH_B1", DataType.UBYTE) asmgen.out(" cmp P8ZP_SCRATCH_B1")
asmgen.assignExpressionToRegister(arg1, RegisterOrPair.A)
asmgen.out(" cmp P8ZP_SCRATCH_B1")
} else {
asmgen.pushCpuStack(DataType.UBYTE, arg1)
asmgen.assignExpressionToVariable(arg2, "P8ZP_SCRATCH_B1", DataType.UBYTE)
asmgen.out(" pla | cmp P8ZP_SCRATCH_B1")
}
} }
} }
} else } else
@ -267,25 +237,12 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
+""") +""")
} }
else -> { else -> {
if(arg1.isSimple()) { asmgen.assignWordOperandsToAYAndVar(arg1, arg2, "P8ZP_SCRATCH_W1")
asmgen.assignExpressionToVariable(arg2, "P8ZP_SCRATCH_W1", DataType.UWORD) asmgen.out("""
asmgen.assignExpressionToRegister(arg1, RegisterOrPair.AY) cpy P8ZP_SCRATCH_W1+1
asmgen.out(""" bne +
cpy P8ZP_SCRATCH_W1+1 cmp P8ZP_SCRATCH_W1
bne + +""")
cmp P8ZP_SCRATCH_W1
+""")
} else {
asmgen.pushCpuStack(DataType.UWORD, arg1)
asmgen.assignExpressionToVariable(arg2, "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
asmgen.out("""
cpy P8ZP_SCRATCH_W1+1
bne +
cmp P8ZP_SCRATCH_W1
+""")
}
} }
} }
} else } else
@ -767,15 +724,7 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
} }
// fall through method: // fall through method:
if(fcall.args[1].isSimple()) { asmgen.assignWordOperandsToAYAndVar(fcall.args[1], fcall.args[0], "P8ZP_SCRATCH_W1")
asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.assignExpressionToRegister(fcall.args[1], RegisterOrPair.AY)
} else {
asmgen.pushCpuStack(DataType.UWORD, fcall.args[1])
asmgen.assignExpressionToVariable(fcall.args[0], "P8ZP_SCRATCH_W1", DataType.UWORD)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
asmgen.out(" jsr prog8_lib.func_pokew") asmgen.out(" jsr prog8_lib.func_pokew")
} }

View File

@ -427,7 +427,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
return true return true
} }
} }
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
when (expr.operator) { when (expr.operator) {
"&", "and" -> asmgen.out(" and P8ZP_SCRATCH_W1 | pha | tya | and P8ZP_SCRATCH_W1+1 | tay | pla") "&", "and" -> asmgen.out(" and P8ZP_SCRATCH_W1 | pha | tya | and P8ZP_SCRATCH_W1+1 | tay | pla")
"|", "or" -> asmgen.out(" ora P8ZP_SCRATCH_W1 | pha | tya | ora P8ZP_SCRATCH_W1+1 | tay | pla") "|", "or" -> asmgen.out(" ora P8ZP_SCRATCH_W1 | pha | tya | ora P8ZP_SCRATCH_W1+1 | tay | pla")
@ -469,7 +469,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
return true return true
} else if(expr.left.type in WordDatatypes && expr.right.type in WordDatatypes && } else if(expr.left.type in WordDatatypes && expr.right.type in WordDatatypes &&
expr.left.isSimple() && expr.right.isSimple()) { expr.left.isSimple() && expr.right.isSimple()) {
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
if(expr.operator=="==") { if(expr.operator=="==") {
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
@ -537,7 +537,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} else if(dt in WordDatatypes) { } else if(dt in WordDatatypes) {
fun doAddOrSubWordExpr() { fun doAddOrSubWordExpr() {
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
if(expr.operator=="+") if(expr.operator=="+")
asmgen.out(""" asmgen.out("""
clc clc
@ -733,7 +733,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
return true return true
} }
in WordDatatypes -> { in WordDatatypes -> {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
jsr math.multiply_words jsr math.multiply_words
lda math.multiply_words.result lda math.multiply_words.result
@ -793,13 +793,13 @@ internal class AssignmentAsmGen(private val program: PtProgram,
return true return true
} }
DataType.UWORD -> { DataType.UWORD -> {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(" jsr math.divmod_uw_asm") asmgen.out(" jsr math.divmod_uw_asm")
assignRegisterpairWord(assign.target, RegisterOrPair.AY) assignRegisterpairWord(assign.target, RegisterOrPair.AY)
return true return true
} }
DataType.WORD -> { DataType.WORD -> {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(" jsr math.divmod_w_asm") asmgen.out(" jsr math.divmod_w_asm")
assignRegisterpairWord(assign.target, RegisterOrPair.AY) assignRegisterpairWord(assign.target, RegisterOrPair.AY)
return true return true
@ -821,7 +821,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
return true return true
} }
DataType.UWORD -> { DataType.UWORD -> {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(" jsr math.divmod_uw_asm") asmgen.out(" jsr math.divmod_uw_asm")
assignVariableWord(assign.target, "P8ZP_SCRATCH_W2") assignVariableWord(assign.target, "P8ZP_SCRATCH_W2")
return true return true
@ -836,21 +836,9 @@ internal class AssignmentAsmGen(private val program: PtProgram,
private fun assignOptimizedComparisonBytes(expr: PtBinaryExpression, assign: AsmAssignment): Boolean { private fun assignOptimizedComparisonBytes(expr: PtBinaryExpression, assign: AsmAssignment): Boolean {
val signed = expr.left.type == DataType.BYTE || expr.right.type == DataType.BYTE val signed = expr.left.type == DataType.BYTE || expr.right.type == DataType.BYTE
fun assignExpressionOperandsLeftScratchRightA() {
if(expr.right.isSimple()) {
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", expr.left.type)
assignExpressionToRegister(expr.right, RegisterOrPair.A, signed)
} else {
assignExpressionToRegister(expr.right, RegisterOrPair.A, signed)
asmgen.saveRegisterStack(CpuRegister.A, false)
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_B1", expr.left.type)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
}
when(expr.operator) { when(expr.operator) {
"==" -> { "==" -> {
assignExpressionOperandsLeftScratchRightA() asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_B1 cmp P8ZP_SCRATCH_B1
beq + beq +
@ -860,7 +848,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
"!=" -> { "!=" -> {
assignExpressionOperandsLeftScratchRightA() asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_B1 cmp P8ZP_SCRATCH_B1
bne + bne +
@ -870,7 +858,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
"<" -> { "<" -> {
assignExpressionOperandsLeftScratchRightA() asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
if(signed) if(signed)
asmgen.out(""" asmgen.out("""
clc clc
@ -892,7 +880,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
"<=" -> { "<=" -> {
assignExpressionOperandsLeftScratchRightA() asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
if(signed) if(signed)
asmgen.out(""" asmgen.out("""
sec sec
@ -911,7 +899,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
rol a""") rol a""")
} }
">" -> { ">" -> {
assignExpressionOperandsLeftScratchRightA() asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
if(signed) if(signed)
asmgen.out(""" asmgen.out("""
sec sec
@ -931,7 +919,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
eor #1""") eor #1""")
} }
">=" -> { ">=" -> {
assignExpressionOperandsLeftScratchRightA() asmgen.assignByteOperandsToAAndVar(expr.right, expr.left, "P8ZP_SCRATCH_B1")
if(signed) if(signed)
asmgen.out(""" asmgen.out("""
clc clc
@ -964,7 +952,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
val signed = expr.left.type == DataType.WORD || expr.right.type == DataType.WORD val signed = expr.left.type == DataType.WORD || expr.right.type == DataType.WORD
when(expr.operator) { when(expr.operator) {
"==" -> { "==" -> {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
bne + bne +
@ -976,7 +964,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
"!=" -> { "!=" -> {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
bne + bne +
@ -989,7 +977,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} }
"<" -> { "<" -> {
if(signed) { if(signed) {
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
tya tya
@ -1003,7 +991,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
else { else {
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cpy P8ZP_SCRATCH_W1+1 cpy P8ZP_SCRATCH_W1+1
bcc + bcc +
@ -1018,7 +1006,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} }
"<=" -> { "<=" -> {
if(signed) { if(signed) {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
tya tya
@ -1032,7 +1020,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
else { else {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cpy P8ZP_SCRATCH_W1+1 cpy P8ZP_SCRATCH_W1+1
bcc ++ bcc ++
@ -1047,7 +1035,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} }
">" -> { ">" -> {
if(signed) { if(signed) {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
tya tya
@ -1061,7 +1049,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
else { else {
assignExpressionWordOperandsLeftScratchW1RightAY(expr) asmgen.assignWordOperandsToAYAndVar(expr.right, expr.left, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cpy P8ZP_SCRATCH_W1+1 cpy P8ZP_SCRATCH_W1+1
bcc + bcc +
@ -1076,7 +1064,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} }
">=" -> { ">=" -> {
if(signed) { if(signed) {
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cmp P8ZP_SCRATCH_W1 cmp P8ZP_SCRATCH_W1
tya tya
@ -1090,7 +1078,7 @@ internal class AssignmentAsmGen(private val program: PtProgram,
+""") +""")
} }
else { else {
assignExpressionWordOperandsLeftAYRightScratchW1(expr) asmgen.assignWordOperandsToAYAndVar(expr.left, expr.right, "P8ZP_SCRATCH_W1")
asmgen.out(""" asmgen.out("""
cpy P8ZP_SCRATCH_W1+1 cpy P8ZP_SCRATCH_W1+1
bcc ++ bcc ++
@ -1278,30 +1266,6 @@ internal class AssignmentAsmGen(private val program: PtProgram,
} }
} }
private fun assignExpressionWordOperandsLeftScratchW1RightAY(expr: PtBinaryExpression) {
if(expr.right.isSimple()) {
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_W1", expr.left.type)
assignExpressionToRegister(expr.right, RegisterOrPair.AY, expr.right.type in SignedDatatypes)
} else {
asmgen.pushCpuStack(DataType.UWORD, expr.right)
assignExpressionToVariable(expr.left, "P8ZP_SCRATCH_W1", expr.left.type)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
}
private fun assignExpressionWordOperandsLeftAYRightScratchW1(expr: PtBinaryExpression) {
if(expr.left.isSimple()) {
assignExpressionToVariable(expr.right, "P8ZP_SCRATCH_W1", expr.left.type)
assignExpressionToRegister(expr.left, RegisterOrPair.AY, expr.left.type in SignedDatatypes)
} else {
asmgen.pushCpuStack(DataType.UWORD, expr.left)
assignExpressionToVariable(expr.right, "P8ZP_SCRATCH_W1", expr.left.type)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false)
}
}
private fun assignStatusFlagByte(target: AsmAssignTarget, statusflag: Statusflag) { private fun assignStatusFlagByte(target: AsmAssignTarget, statusflag: Statusflag) {
when(statusflag) { when(statusflag) {
Statusflag.Pc -> { Statusflag.Pc -> {