refactor byte '==', '!=', '<' and '>' codegen

This commit is contained in:
Irmen de Jong 2021-03-15 22:45:48 +01:00
parent bf23ad78e6
commit 38e40084f1

View File

@ -403,6 +403,16 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
private fun translateByteLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateByteLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
fun code(sbcOperand: String) {
asmgen.out("""
sec
sbc $sbcOperand
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
}
if(rightConstVal!=null) { if(rightConstVal!=null) {
if(leftConstVal!=null) { if(leftConstVal!=null) {
if(rightConstVal>=leftConstVal) if(rightConstVal>=leftConstVal)
@ -411,29 +421,27 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
if(rightConstVal.number.toInt()!=0) return if(rightConstVal.number.toInt()!=0)
asmgen.out(""" code("#${rightConstVal.number}")
sec
sbc #${rightConstVal.number}
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
else else
asmgen.out(" bpl $jumpIfFalseLabel") asmgen.out(" bpl $jumpIfFalseLabel")
return
} }
} }
} }
if(byteJumpForSimpleRightOperands(left, right, ::code))
return
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
return code("P8ZP_SCRATCH_B1")
}
private fun byteJumpForSimpleRightOperands(left: Expression, right: Expression, code: (String)->Unit): Boolean {
if(right is IdentifierReference) { if(right is IdentifierReference) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(""" code(asmgen.asmVariableName(right))
sec return true
sbc ${asmgen.asmVariableName(right)}
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
return
} }
var memread = right as? DirectMemoryRead var memread = right as? DirectMemoryRead
if(memread==null && right is TypecastExpression) if(memread==null && right is TypecastExpression)
@ -442,24 +450,11 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
val address = memread.addressExpression as? NumericLiteralValue val address = memread.addressExpression as? NumericLiteralValue
if(address!=null) { if(address!=null) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(""" code(address.number.toHex())
sec return true
sbc ${address.number.toHex()}
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
return
} }
} }
return false
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
sec
sbc P8ZP_SCRATCH_B1
bvc +
eor #$80
+ bpl $jumpIfFalseLabel""")
} }
private fun translateUwordLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateUwordLessJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
@ -565,6 +560,18 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
private fun translateByteGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateByteGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
fun code(sbcOperand: String) {
asmgen.out("""
clc
sbc $sbcOperand
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
}
if(rightConstVal!=null) { if(rightConstVal!=null) {
if(leftConstVal!=null) { if(leftConstVal!=null) {
if(rightConstVal<=leftConstVal) if(rightConstVal<=leftConstVal)
@ -573,63 +580,20 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
if(rightConstVal.number.toInt()!=0) return if(rightConstVal.number.toInt()!=0)
asmgen.out(""" code("#${rightConstVal.number}")
clc
sbc #${rightConstVal.number}
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
else else
asmgen.out(" bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel") asmgen.out(" bmi $jumpIfFalseLabel | beq $jumpIfFalseLabel")
return
} }
} }
} }
if(right is IdentifierReference) { if(byteJumpForSimpleRightOperands(left, right, ::code))
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
clc
sbc ${asmgen.asmVariableName(right)}
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
return return
}
var memread = right as? DirectMemoryRead
if(memread==null && right is TypecastExpression)
memread = right.expression as? DirectMemoryRead
if(memread!=null) {
val address = memread.addressExpression as? NumericLiteralValue
if(address!=null) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out("""
clc
sbc ${address.number.toHex()}
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
return
}
}
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(""" return code("P8ZP_SCRATCH_B1")
clc
sbc P8ZP_SCRATCH_B1
bvc +
eor #$80
+ bpl +
bmi $jumpIfFalseLabel
+""")
} }
private fun translateUwordGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateUwordGreaterJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
@ -995,6 +959,10 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
private fun translateByteEqualsJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateByteEqualsJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
fun code(cmpOperand: String) {
asmgen.out(" cmp $cmpOperand | bne $jumpIfFalseLabel")
}
if(rightConstVal!=null) { if(rightConstVal!=null) {
if(leftConstVal!=null) { if(leftConstVal!=null) {
if(rightConstVal!=leftConstVal) if(rightConstVal!=leftConstVal)
@ -1002,40 +970,24 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
return return
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
val name = asmgen.asmVariableName(left) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
if(rightConstVal.number.toInt()!=0) return if(rightConstVal.number.toInt()!=0)
asmgen.out(" lda $name | cmp #${rightConstVal.number} | bne $jumpIfFalseLabel") code("#${rightConstVal.number}")
else else
asmgen.out(" lda $name | bne $jumpIfFalseLabel") asmgen.out(" bne $jumpIfFalseLabel")
return }
} if (left is DirectMemoryRead) {
else if (left is DirectMemoryRead) { translateDirectMemReadExpression(left, false)
translateDirectMemReadExpression(left, false) return if(rightConstVal.number.toInt()!=0)
if(rightConstVal.number.toInt()!=0) code("#${rightConstVal.number}")
asmgen.out(" cmp #${rightConstVal.number} | bne $jumpIfFalseLabel")
else else
asmgen.out(" bne $jumpIfFalseLabel") asmgen.out(" bne $jumpIfFalseLabel")
return
} }
} }
} }
if(right is IdentifierReference) { if(byteJumpForSimpleRightOperands(left, right, ::code))
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(" cmp ${asmgen.asmVariableName(right)} | bne $jumpIfFalseLabel")
return return
}
var memread = right as? DirectMemoryRead
if(memread==null && right is TypecastExpression)
memread = right.expression as? DirectMemoryRead
if(memread!=null) {
val address = memread.addressExpression as? NumericLiteralValue
if(address!=null) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(" cmp ${address.number.toHex()} | bne $jumpIfFalseLabel")
return
}
}
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
@ -1043,6 +995,11 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
} }
private fun translateByteNotEqualsJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateByteNotEqualsJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {
fun code(cmpOperand: String) {
asmgen.out(" cmp $cmpOperand | beq $jumpIfFalseLabel")
}
if(rightConstVal!=null) { if(rightConstVal!=null) {
if(leftConstVal!=null) { if(leftConstVal!=null) {
if(rightConstVal==leftConstVal) if(rightConstVal==leftConstVal)
@ -1050,44 +1007,28 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge
return return
} else { } else {
if (left is IdentifierReference) { if (left is IdentifierReference) {
val name = asmgen.asmVariableName(left) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
if(rightConstVal.number.toInt()!=0) return if(rightConstVal.number.toInt()!=0)
asmgen.out(" lda $name | cmp #${rightConstVal.number} | beq $jumpIfFalseLabel") code("#${rightConstVal.number}")
else else
asmgen.out(" lda $name | beq $jumpIfFalseLabel") asmgen.out(" beq $jumpIfFalseLabel")
return }
} if (left is DirectMemoryRead) {
else if (left is DirectMemoryRead) { translateDirectMemReadExpression(left, false)
translateDirectMemReadExpression(left, false) return if(rightConstVal.number.toInt()!=0)
if(rightConstVal.number.toInt()!=0) code("#${rightConstVal.number}")
asmgen.out(" cmp #${rightConstVal.number} | beq $jumpIfFalseLabel")
else else
asmgen.out(" beq $jumpIfFalseLabel") asmgen.out(" beq $jumpIfFalseLabel")
return
} }
} }
} }
if(right is IdentifierReference) { if(byteJumpForSimpleRightOperands(left, right, ::code))
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(" cmp ${asmgen.asmVariableName(right)} | beq $jumpIfFalseLabel")
return return
}
var memread = right as? DirectMemoryRead
if(memread==null && right is TypecastExpression)
memread = right.expression as? DirectMemoryRead
if(memread!=null) {
val address = memread.addressExpression as? NumericLiteralValue
if(address!=null) {
asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(" cmp ${address.number.toHex()} | beq $jumpIfFalseLabel")
return
}
}
asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null) asmgen.assignExpressionToVariable(right, "P8ZP_SCRATCH_B1", DataType.UBYTE, null)
asmgen.assignExpressionToRegister(left, RegisterOrPair.A) asmgen.assignExpressionToRegister(left, RegisterOrPair.A)
asmgen.out(" cmp P8ZP_SCRATCH_B1 | beq $jumpIfFalseLabel") return code("P8ZP_SCRATCH_B1")
} }
private fun translateWordEqualsJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) { private fun translateWordEqualsJump(left: Expression, right: Expression, leftConstVal: NumericLiteralValue?, rightConstVal: NumericLiteralValue?, jumpIfFalseLabel: String) {