mirror of
https://github.com/irmen/prog8.git
synced 2024-11-27 03:50:27 +00:00
refactoring last of old Ast modification Api
This commit is contained in:
parent
3e47dad12a
commit
b5086b6a8f
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user