mirror of
https://github.com/irmen/prog8.git
synced 2025-01-11 13:29:45 +00:00
binexpr expression splitter for assignments
This commit is contained in:
parent
9cd3a9f8e8
commit
110f877dcc
@ -3,19 +3,20 @@ package prog8.optimizer
|
||||
import prog8.ast.INameScope
|
||||
import prog8.ast.Node
|
||||
import prog8.ast.Program
|
||||
import prog8.ast.base.VarDeclType
|
||||
import prog8.ast.expressions.*
|
||||
import prog8.ast.processing.AstWalker
|
||||
import prog8.ast.processing.IAstModification
|
||||
import prog8.ast.statements.AssignTarget
|
||||
import prog8.ast.statements.Assignment
|
||||
import prog8.ast.statements.VarDecl
|
||||
|
||||
class ExpressionSplitter(private val program: Program) : AstWalker() {
|
||||
|
||||
// TODO once this works, integrate it back into expressionsimplifier
|
||||
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
|
||||
override fun after(decl: VarDecl, parent: Node): Iterable<IAstModification> {
|
||||
|
||||
if(assignment!=null) {
|
||||
val expr = assignment.value as? BinaryExpression
|
||||
val expr = decl.value as? BinaryExpression
|
||||
if (expr != null) {
|
||||
// reduce the complexity of a (binary) expression that has to be evaluated on the eval stack,
|
||||
// by attempting to splitting it up into individual simple steps:
|
||||
@ -23,26 +24,85 @@ class ExpressionSplitter(private val program: Program) : AstWalker() {
|
||||
// or X = <not-binary-expression> <associativeoperator> <some-expression-not-X>
|
||||
// split that into X = <some-expression-not-X> ; X = X <operator> <not-binary-expression>
|
||||
|
||||
// TODO FIX THIS, IT SOMETIMES JUST LOOPS... (for example on plasma.p8)
|
||||
if (expr.operator !in comparisonOperators && !assignment.isAugmentable && isSimpleTarget(assignment.target, program.namespace)) {
|
||||
// TODO DOES THIS LOOP AS WELL?
|
||||
if (expr.operator !in comparisonOperators && decl.type==VarDeclType.VAR) {
|
||||
if (expr.right !is BinaryExpression) {
|
||||
println("SPLIT RIGHT BINEXPR $expr")
|
||||
val firstAssign = Assignment(assignment.target, expr.left, assignment.position)
|
||||
val augExpr = BinaryExpression(assignment.target.toExpression(), expr.operator, expr.right, expr.position)
|
||||
println("SPLIT VARDECL RIGHT BINEXPR $expr") // TODO
|
||||
// val firstAssign = Assignment(assignment.target, expr.left, assignment.position)
|
||||
// val augExpr = BinaryExpression(assignment.target.toExpression(), expr.operator, expr.right, expr.position)
|
||||
// return listOf(
|
||||
// IAstModification.InsertBefore(assignment, firstAssign, parent),
|
||||
// IAstModification.ReplaceNode(assignment.value, augExpr, assignment)
|
||||
// )
|
||||
} else if (expr.left !is BinaryExpression && expr.operator in associativeOperators) {
|
||||
println("SPLIT VARDECL LEFT BINEXPR $expr") // TODO
|
||||
// val firstAssign = Assignment(assignment.target, expr.right, assignment.position)
|
||||
// val augExpr = BinaryExpression(assignment.target.toExpression(), expr.operator, expr.left, expr.position)
|
||||
// return listOf(
|
||||
// IAstModification.InsertBefore(assignment, firstAssign, parent),
|
||||
// IAstModification.ReplaceNode(assignment.value, augExpr, assignment))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
// TODO once this works, integrate it back into expressionsimplifier
|
||||
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
|
||||
|
||||
val binExpr = assignment.value as? BinaryExpression
|
||||
if (binExpr != null) {
|
||||
/*
|
||||
|
||||
reduce the complexity of a (binary) expression that has to be evaluated on the eval stack,
|
||||
by attempting to splitting it up into individual simple steps:
|
||||
|
||||
|
||||
X = BinExpr X = LeftExpr
|
||||
<operator> followed by
|
||||
/ \ IF 'X' not used X = BinExpr
|
||||
/ \ IN LEFTEXPR ==> <operator>
|
||||
/ \ / \
|
||||
LeftExpr. RightExpr. / \
|
||||
/ \ / \ X RightExpr.
|
||||
.. .. .. ..
|
||||
|
||||
|
||||
|
||||
X = BinExpr X = RightExpr
|
||||
<operator> followed by
|
||||
/ \ IF ASSOCIATIVE X = BinExpr
|
||||
/ \ <operator> ==> <operator>
|
||||
/ \ / \
|
||||
LeftExpr. SimpleExpr. / \
|
||||
/ \ (not X) X LeftExpr.
|
||||
.. ..
|
||||
|
||||
*/
|
||||
if(!assignment.isAugmentable && isSimpleTarget(assignment.target, program.namespace)) {
|
||||
val firstAssign = Assignment(assignment.target, binExpr.left, binExpr.left.position)
|
||||
val targetExpr = assignment.target.toExpression()
|
||||
val augExpr = BinaryExpression(targetExpr, binExpr.operator, binExpr.right, binExpr.right.position)
|
||||
return listOf(
|
||||
IAstModification.InsertBefore(assignment, firstAssign, parent),
|
||||
IAstModification.ReplaceNode(assignment.value, augExpr, assignment)
|
||||
)
|
||||
} else if (expr.left !is BinaryExpression && expr.operator in associativeOperators) {
|
||||
println("SPLIT LEFT BINEXPR $expr")
|
||||
val firstAssign = Assignment(assignment.target, expr.right, assignment.position)
|
||||
val augExpr = BinaryExpression(assignment.target.toExpression(), expr.operator, expr.left, expr.position)
|
||||
IAstModification.ReplaceNode(assignment.value, augExpr, assignment))
|
||||
}
|
||||
|
||||
if(binExpr.operator in associativeOperators && binExpr.left is BinaryExpression) {
|
||||
if (binExpr.right !is BinaryExpression && !(binExpr.right isSameAs assignment.target)) {
|
||||
val firstAssign = Assignment(assignment.target, binExpr.right, binExpr.right.position)
|
||||
val targetExpr = assignment.target.toExpression()
|
||||
val augExpr = BinaryExpression(targetExpr, binExpr.operator, binExpr.left, binExpr.left.position)
|
||||
return listOf(
|
||||
IAstModification.InsertBefore(assignment, firstAssign, parent),
|
||||
IAstModification.ReplaceNode(assignment.value, augExpr, assignment))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO further unraveling of binary expression trees into flat statements.
|
||||
// however this should probably be done in a more generic way to also service
|
||||
// the expressiontrees that are not used in an assignment statement...
|
||||
}
|
||||
|
||||
return emptyList()
|
||||
@ -54,7 +114,7 @@ class ExpressionSplitter(private val program: Program) : AstWalker() {
|
||||
target.memoryAddress!=null -> target.isInRegularRAM(namespace)
|
||||
target.arrayindexed!=null -> {
|
||||
val index = target.arrayindexed!!.arrayspec.index
|
||||
if(index is NumericLiteralValue || index is IdentifierReference)
|
||||
if(index is NumericLiteralValue)
|
||||
target.isInRegularRAM(namespace)
|
||||
else
|
||||
false
|
||||
|
@ -55,8 +55,8 @@ internal fun Program.simplifyExpressions() : Int {
|
||||
}
|
||||
|
||||
internal fun Program.splitExpressions() : Int {
|
||||
val opti = ExpressionSplitter(this)
|
||||
opti.visit(this)
|
||||
return opti.applyModifications()
|
||||
val splitter = ExpressionSplitter(this)
|
||||
splitter.visit(this)
|
||||
return splitter.applyModifications()
|
||||
}
|
||||
|
||||
|
@ -19,20 +19,20 @@ main {
|
||||
; uword[] arrays = [names, names3, values]
|
||||
|
||||
|
||||
asmsub testX() {
|
||||
%asm {{
|
||||
stx _saveX
|
||||
lda #13
|
||||
jsr txt.chrout
|
||||
lda _saveX
|
||||
jsr txt.print_ub
|
||||
lda #13
|
||||
jsr txt.chrout
|
||||
ldx _saveX
|
||||
rts
|
||||
_saveX .byte 0
|
||||
}}
|
||||
}
|
||||
; asmsub testX() {
|
||||
; %asm {{
|
||||
; stx _saveX
|
||||
; lda #13
|
||||
; jsr txt.chrout
|
||||
; lda _saveX
|
||||
; jsr txt.print_ub
|
||||
; lda #13
|
||||
; jsr txt.chrout
|
||||
; ldx _saveX
|
||||
; rts
|
||||
;_saveX .byte 0
|
||||
; }}
|
||||
; }
|
||||
|
||||
sub start() {
|
||||
; byte bb = 100
|
||||
|
Loading…
x
Reference in New Issue
Block a user