fixed missing repeated constant folding in expression optimization

This commit is contained in:
Irmen de Jong 2020-06-04 20:22:37 +02:00
parent 9ca1c66f2b
commit c66fc8630c
3 changed files with 26 additions and 89 deletions

View File

@ -164,13 +164,11 @@ private fun optimizeAst(programAst: Program, errors: ErrorReporter) {
// keep optimizing expressions and statements until no more steps remain
val optsDone1 = programAst.simplifyExpressions()
val optsDone2 = programAst.optimizeStatements(errors)
programAst.constantFold(errors) // because simplified statements and expressions could give rise to more constants that can be folded away:
errors.handle()
if (optsDone1 + optsDone2 == 0)
break
}
// because simplified statements and expressions could give rise to more constants that can be folded away:
programAst.constantFold(errors)
errors.handle()
val remover = UnusedCodeRemover()
remover.visit(programAst)

View File

@ -14,11 +14,6 @@ import kotlin.math.pow
/*
todo add more expression optimizations
x + x -> x << 1 (for words... for bytes too?)
x + x + x + x -> x << 2 (for words... for bytes too?)
x + x + x -> ???? x*3 ??? words/bytes?
x - x -> 0
(assignment) x += y + 1 -> x += y , x++ (add another x++ for +2)
(assignment) x += y - 1 -> x += y , x--
(assignment) x -= y + 1 -> x -= y , x--
@ -107,7 +102,6 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker()
if (!leftIDt.isKnown || !rightIDt.isKnown)
throw FatalAstException("can't determine datatype of both expression operands $expr")
// ConstValue <associativeoperator> X --> X <associativeoperator> ConstValue
if (leftVal != null && expr.operator in associativeOperators && rightVal == null)
return listOf(IAstModification.SwapOperands(expr))
@ -242,7 +236,7 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker()
}
}
// simplify when a term is constant and directly determines the outcome
// simplify when a term is constant and directly determines the outcome
val constTrue = NumericLiteralValue.fromBoolean(true, expr.position)
val constFalse = NumericLiteralValue.fromBoolean(false, expr.position)
val newExpr: Expression? = when (expr.operator) {

View File

@ -7,86 +7,31 @@
main {
sub start() {
ubyte ubb
byte bb
uword uww
word ww
A=Y+Y
A=Y+Y+Y
A=Y+Y+Y+Y
A=Y+Y+Y+Y+Y
bb = -1
ww = -1
if bb<0
c64scr.print("1 ok\n")
else
c64scr.print("1 fail\n")
if ww<0
c64scr.print("2 ok\n")
else
c64scr.print("2 fail\n")
bb = 0
ww = 0
if bb>=0
c64scr.print("4 ok\n")
else
c64scr.print("4 fail\n")
if ww>=0
c64scr.print("5 ok\n")
else
c64scr.print("5 fail\n")
bb = 0
ww = 0
if bb>=0
c64scr.print("7 ok\n")
else
c64scr.print("7 fail\n")
if ww>=0
c64scr.print("8 ok\n")
else
c64scr.print("8 fail\n")
ubb = 0
uww = 0
if ubb>=0
c64scr.print("10 ok\n")
else
c64scr.print("10 fail\n")
if uww>=0
c64scr.print("11 ok\n")
else
c64scr.print("11 fail\n")
if ubb<0
c64scr.print("12 fail\n")
else
c64scr.print("12 ok\n")
if uww<0
c64scr.print("13 fail\n")
else
c64scr.print("13 ok\n")
ubb = $ff
uww = $ffff
if ubb>=0
c64scr.print("14 ok\n")
else
c64scr.print("14 fail\n")
if uww>=0
c64scr.print("15 ok\n")
else
c64scr.print("15 fail\n")
if ubb<0
c64scr.print("16 fail\n")
else
c64scr.print("16 ok\n")
if uww<0
c64scr.print("17 fail\n")
else
c64scr.print("17 ok\n")
; ubyte ubb
; byte bb
; uword uww
; word ww
; word ww2
;
; A = ubb*0
; Y = ubb*1
; A = ubb*2
; Y = ubb*4
; A = ubb*8
; Y = ubb*16
; A = ubb*32
; Y = ubb*64
; A = ubb*128
; Y = ubb+ubb+ubb
; A = ubb+ubb+ubb+ubb
; ww = ww2+ww2
; ww = ww2+ww2+ww2
; ww = ww2+ww2+ww2+ww2
}
}