fixed bug in operand equality comparison, could lead to compiler endless loop

This commit is contained in:
Irmen de Jong 2020-08-20 22:21:26 +02:00
parent 58f323c087
commit b03597ac13
4 changed files with 35 additions and 20 deletions

View File

@ -43,6 +43,23 @@ sealed class Expression: Node {
(other is ArrayIndexedExpression && other.identifier.nameInSource == identifier.nameInSource
&& other.arrayspec.index isSameAs arrayspec.index)
}
is DirectMemoryRead -> {
(other is DirectMemoryRead && other.addressExpression isSameAs addressExpression)
}
is TypecastExpression -> {
(other is TypecastExpression && other.implicit==implicit && other.type==type && other.expression isSameAs expression)
}
is AddressOf -> {
(other is AddressOf && other.identifier.nameInSource == identifier.nameInSource)
}
is RangeExpr -> {
(other is RangeExpr && other.from==from && other.to==to && other.step==step)
}
is FunctionCall -> {
(other is FunctionCall && other.target.nameInSource == target.nameInSource
&& other.args.size == args.size
&& other.args.zip(args).all { it.first isSameAs it.second } )
}
else -> other==this
}
}

View File

@ -40,13 +40,6 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker()
// remove the sub-typecast if its datatype is larger than the outer typecast
if(subTypecast.type largerThan typecast.type) {
mods += IAstModification.ReplaceNode(typecast.expression, subTypecast.expression, typecast)
} else {
if(subTypecast.type == DataType.UBYTE && typecast.type == DataType.UWORD) {
// (X as ubyte) as uword -> X & 255
// TODO don't optimize this because the asm code generated by it is worse...
// val and255 = BinaryExpression(subTypecast.expression, "&", NumericLiteralValue.optimalInteger(255, subTypecast.position), subTypecast.position)
// mods += IAstModification.ReplaceNode(typecast, and255, parent)
}
}
} else {
if (typecast.expression.inferType(program).istype(typecast.type)) {

View File

@ -368,11 +368,12 @@ internal class StatementOptimizer(private val program: Program,
}
}
}
if(binExpr.right isSameAs assignment.target) {
if(binExpr.operator in setOf("+", "*", "&", "|")) {
// associative operator, swap the operands so that the assignment target is first (left)
if(binExpr.operator in associativeOperators && binExpr.right isSameAs assignment.target) {
// associative operator, swap the operands so that the assignment target is first (left)
// unless the other operand is the same in which case we don't swap (endless loop!)
if (!(binExpr.left isSameAs binExpr.right))
return listOf(IAstModification.SwapOperands(binExpr))
}
}
}

View File

@ -7,18 +7,22 @@ main {
sub start() {
;@($d020) += @($d020) ; TODO fix compiler hang
@($d020) += @($d020) ; TODO fix compiler hang
ubyte A = 10
@($c00a) = $4a
@($c000+A) ++ ; TODO implement this
ubyte A
c64scr.print_ubhex(@($c00a), true)
c64.CHROUT('\n')
@($c000+A) -- ; TODO implement this
A = 44+A
c64scr.print_ubhex(@($c00a), true)
c64.CHROUT('\n')
; ubyte A = 10
; @($c00a) = $4a
; @($c000+A) ++ ; TODO implement this
;
; c64scr.print_ubhex(@($c00a), true)
; c64.CHROUT('\n')
; @($c000+A) -- ; TODO implement this
;
; c64scr.print_ubhex(@($c00a), true)
; c64.CHROUT('\n')
}
}