fix broken code generated for certain ==/!= expressions

This commit is contained in:
Irmen de Jong 2022-01-16 17:10:49 +01:00
parent 564a6a1f62
commit 8e56656c8d
5 changed files with 39 additions and 26 deletions

View File

@ -744,18 +744,18 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
"^", "xor" -> asmgen.out(" lda $name | eor #$value | sta $name")
"==" -> {
asmgen.out("""
lda #$value
cmp $name
lda $name
cmp #$value
beq +
lda #0
bne ++
beq ++
+ lda #1
+ sta $name""")
}
"!=" -> {
asmgen.out("""
lda #$value
cmp $name
lda $name
cmp #$value
beq +
lda #1
bne ++

View File

@ -46,7 +46,7 @@ X = BinExpr X = LeftExpr
*/
if(binExpr.operator in AugmentAssignmentOperators + listOf("==", "!=") && isSimpleTarget(assignment.target)) {
if(binExpr.operator in AugmentAssignmentOperators && isSimpleTarget(assignment.target)) {
if(assignment.target isSameAs binExpr.right)
return noModifications
if(assignment.target isSameAs binExpr.left) {
@ -74,15 +74,6 @@ X = BinExpr X = LeftExpr
// )
}
if(binExpr.operator == "==" || binExpr.operator == "!=") {
// don't split if the operand(s) don't fit the type of the resulting variable
val targetDt = assignment.target.inferType(program)
val leftDt = binExpr.left.inferType(program)
val rightDt = binExpr.right.inferType(program)
if(leftDt isNotAssignableTo targetDt || rightDt isNotAssignableTo targetDt)
return noModifications
}
if(binExpr.right.isSimple) {
val firstAssign = Assignment(assignment.target.copy(), binExpr.left, AssignmentOrigin.OPTIMIZER, binExpr.left.position)
val targetExpr = assignment.target.toExpression()

View File

@ -89,7 +89,7 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
// determine common datatype and add typecast as required to make left and right equal types
val (commonDt, toFix) = BinaryExpression.commonDatatype(leftDt.getOr(DataType.UNDEFINED), rightDt.getOr(DataType.UNDEFINED), expr.left, expr.operator, expr.right)
val (commonDt, toFix) = BinaryExpression.commonDatatype(leftDt.getOr(DataType.UNDEFINED), rightDt.getOr(DataType.UNDEFINED), expr.left, expr.right)
if(toFix!=null) {
val modifications = mutableListOf<IAstModification>()
when {

View File

@ -204,7 +204,7 @@ class BinaryExpression(var left: Expression, var operator: String, var right: Ex
commonDatatype(
leftDt.getOr(DataType.BYTE),
rightDt.getOr(DataType.BYTE),
null, "", null
null, null
).first
)
} catch (x: FatalAstException) {
@ -227,7 +227,7 @@ class BinaryExpression(var left: Expression, var operator: String, var right: Ex
companion object {
fun commonDatatype(leftDt: DataType, rightDt: DataType,
left: Expression?, operator: String, right: Expression?): Pair<DataType, Expression?> {
left: Expression?, right: Expression?): Pair<DataType, Expression?> {
// byte + byte -> byte
// byte + word -> word
// word + byte -> word

View File

@ -7,17 +7,33 @@ main {
%option force_output
sub start() {
ubyte foobar = 2
str @shared @zp name = "irmen"
ubyte[] @shared @zp array = [1,2,3,4]
txt.print_uwhex(&name, true)
uword b1
b1 = 'a'
b1 = b1=='z' ; TODO fix code generation!
txt.print("should print 0: ")
txt.print_ub(b1)
txt.nl()
txt.print_uwhex(&name, true)
txt.print_ub(foobar)
b1 = 'a'
b1 = b1!='z' ; TODO fix code generation!
txt.print("should print 1: ")
txt.print_ub(b1)
txt.nl()
; str text = "???????????"
; void txt.input_chars(text)
; txt.print("\ninput=")
; txt.print(text)
; txt.nl()
; b1 = text[0]=='z'
; txt.print_ub(b1)
; txt.nl()
;
; if text[0]=='z' {
; txt.print("z!\n")
; }
;
; test(text[0]=='z')
; float fl
; test_stack.test()
;
@ -53,6 +69,12 @@ main {
; test_stack.test()
}
sub test(ubyte what) {
txt.print("test: what=")
txt.print_ub(what)
txt.nl()
}
sub func() -> ubyte {
txt.print("func!\n")
return 99