refactoring last of old Ast modification Api

This commit is contained in:
Irmen de Jong 2020-06-14 03:17:42 +02:00
parent 3e47dad12a
commit b5086b6a8f
3 changed files with 34 additions and 21 deletions

View File

@ -1,16 +1,35 @@
package prog8.optimizer
import prog8.ast.IFunctionCall
import prog8.ast.Node
import prog8.ast.Program
import prog8.ast.base.*
import prog8.ast.expressions.*
import prog8.ast.processing.AstWalker
import prog8.ast.processing.IAstModification
import prog8.ast.processing.IAstModifyingVisitor
import prog8.ast.statements.*
import prog8.compiler.target.CompilationTarget
import prog8.functions.BuiltinFunctions
// TODO implement using AstWalker instead of IAstModifyingVisitor
internal class ConstantFoldingOptimizer2(private val program: Program, private val errors: ErrorReporter) : AstWalker() {
private val noModifications = emptyList<IAstModification>()
override fun before(memread: DirectMemoryRead, parent: Node): Iterable<IAstModification> {
// @( &thing ) --> thing
val addrOf = memread.addressExpression as? AddressOf
return if(addrOf!=null)
listOf(IAstModification.ReplaceNode(memread, addrOf.identifier, parent))
else
noModifications
}
// TODO conversion from below
}
// -----------------------------------------------------------
internal class ConstantFoldingOptimizer(private val program: Program, private val errors: ErrorReporter) : IAstModifyingVisitor {
var optimizationsDone: Int = 0
@ -220,14 +239,6 @@ internal class ConstantFoldingOptimizer(private val program: Program, private va
}
}
override fun visit(memread: DirectMemoryRead): Expression {
// @( &thing ) --> thing
val addrOf = memread.addressExpression as? AddressOf
if(addrOf!=null)
return super.visit(addrOf.identifier)
return super.visit(memread)
}
/**
* Try to accept a unary prefix expression.
* Compile-time constant sub expressions will be evaluated on the spot.

View File

@ -5,14 +5,20 @@ import prog8.ast.base.ErrorReporter
internal fun Program.constantFold(errors: ErrorReporter) {
val optimizer = ConstantFoldingOptimizer(this, errors)
val optimizer = ConstantFoldingOptimizer2(this, errors)
optimizer.visit(this)
while(errors.isEmpty() && optimizer.optimizationsDone>0) {
optimizer.optimizationsDone = 0
while(errors.isEmpty() && optimizer.applyModifications() > 0) {
optimizer.visit(this)
}
val optimizer_old = ConstantFoldingOptimizer(this, errors)
optimizer_old.visit(this)
while(errors.isEmpty() && optimizer_old.optimizationsDone>0) {
optimizer_old.optimizationsDone = 0
optimizer_old.visit(this)
}
if(errors.isEmpty())
modules.forEach { it.linkParents(namespace) } // re-link in final configuration
}

View File

@ -19,14 +19,10 @@ blabla:
sub start() {
A <<= 2
A >>= 2
A -= 3
A = A+A
lsl(X)
lsl(Y)
lsl(A)
lsl(@($d020))
str sss = "zzz"
str x = "zxcvzxcv"
x = @(&sss)
}
}