fix augmentable check to align with what the asmgen understands

This commit is contained in:
Irmen de Jong 2021-11-20 21:48:30 +01:00
parent 7936fc5bd8
commit eacf8b896a
3 changed files with 56 additions and 41 deletions

View File

@ -302,7 +302,7 @@ class ArrayIndex(var indexExpr: Expression,
}
fun accept(visitor: IAstVisitor) = indexExpr.accept(visitor)
fun accept(visitor: AstWalker, parent: Node) = indexExpr.accept(visitor, this)
fun accept(visitor: AstWalker) = indexExpr.accept(visitor, this)
override fun toString(): String {
return("ArrayIndex($indexExpr, pos=$position)")
@ -353,13 +353,13 @@ open class Assignment(var target: AssignTarget, var value: Expression, final ove
if(binExpr.operator in "+-") {
val leftBinExpr = binExpr.left as? BinaryExpression
if(leftBinExpr!=null && leftBinExpr.operator in "+-") {
val rightBinExpr = binExpr.right as? BinaryExpression
if(rightBinExpr==null && leftBinExpr!=null && leftBinExpr.operator in "+-") {
// A = (A +- x) +- y
if(leftBinExpr.left isSameAs target || leftBinExpr.right isSameAs target || binExpr.right isSameAs target)
return true
}
val rightBinExpr = binExpr.right as? BinaryExpression
if(rightBinExpr!=null && rightBinExpr.operator in "+-") {
if(leftBinExpr==null && rightBinExpr!=null && rightBinExpr.operator in "+-") {
// A = y +- (A +- x)
if(rightBinExpr.left isSameAs target || rightBinExpr.right isSameAs target || binExpr.left isSameAs target)
return true
@ -371,15 +371,15 @@ open class Assignment(var target: AssignTarget, var value: Expression, final ove
return true // A = v <associative-operator> A
val leftBinExpr = binExpr.left as? BinaryExpression
if(leftBinExpr?.operator == binExpr.operator) {
val rightBinExpr = binExpr.right as? BinaryExpression
if(leftBinExpr?.operator == binExpr.operator && rightBinExpr==null) {
// one of these?
// A = (A <associative-operator> x) <same-operator> y
// A = (x <associative-operator> A) <same-operator> y
// A = (x <associative-operator> y) <same-operator> A
return leftBinExpr.left isSameAs target || leftBinExpr.right isSameAs target || binExpr.right isSameAs target
}
val rightBinExpr = binExpr.right as? BinaryExpression
if(rightBinExpr?.operator == binExpr.operator) {
if(rightBinExpr?.operator == binExpr.operator && leftBinExpr==null) {
// one of these?
// A = y <associative-operator> (A <same-operator> x)
// A = y <associative-operator> (x <same-operator> y)

View File

@ -235,7 +235,7 @@ abstract class AstWalker {
fun visit(decl: VarDecl, parent: Node) {
track(before(decl, parent), decl, parent)
decl.value?.accept(this, decl)
decl.arraysize?.accept(this, decl)
decl.arraysize?.accept(this)
track(after(decl, parent), decl, parent)
}
@ -375,7 +375,7 @@ abstract class AstWalker {
fun visit(arrayIndexedExpression: ArrayIndexedExpression, parent: Node) {
track(before(arrayIndexedExpression, parent), arrayIndexedExpression, parent)
arrayIndexedExpression.arrayvar.accept(this, arrayIndexedExpression)
arrayIndexedExpression.indexer.accept(this, arrayIndexedExpression)
arrayIndexedExpression.indexer.accept(this)
track(after(arrayIndexedExpression, parent), arrayIndexedExpression, parent)
}

View File

@ -1,5 +1,4 @@
%import textio
%import floats
main {
@ -7,46 +6,62 @@ main {
ubyte xx = 1
ubyte yy = 2
uword aw
byte bb
float fl
;; TODO add these constant folders:
;
;; (X + C1) + (Y + C2) => (X + Y) + (C1 + C2)
;; (X + C1) - (Y + C2) => (X - Y) + (C1 - C2)
;; ---> together: (X + C1) <plusmin> (Y + C2) => (X <plusmin> Y) + (C1 <plusmin> C2)
;
;; (X * C) + (Y * C) => (X + Y) * C
;; (X * C) - (Y * C) => (X - Y) * C
;; ---> together: (X * C) <plusmin> (Y * C) => (X <plusmin> Y) * C
;
;; (X - C1) + (Y - C2) => (X + Y) - (C1 + C2)
;; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2)
;
; xx=6
; yy=8
;
; yy = (xx+5)+(yy+10)
; txt.print_ub(yy) ; 29
; txt.nl()
;
; xx=100
; yy=8
; yy = (xx+5)-(yy+10)
; txt.print_ub(yy) ; 87
; txt.nl()
;
; xx=50
; yy=40
; yy = (xx-5)+(yy-10)
; txt.print_ub(yy) ; 75
; txt.nl()
;
; xx=50
; yy=20
; yy = (xx-5)-(yy-10)
; txt.print_ub(yy) ; 35
; txt.nl()
;
; repeat {
; }
; result should be: 29 42 40 87 75 35
xx=6
yy=8
yy = (xx+5)+(yy+10)
txt.print_ub(yy) ; 29
txt.nl()
xx=6
yy=8
yy = (xx*3)+(yy*3)
txt.print_ub(yy) ; 42
txt.nl()
xx=13
yy=5
yy = (xx*5)-(yy*5)
txt.print_ub(yy) ; 40
txt.nl()
xx=100
yy=8
yy = (xx+5)-(yy+10)
txt.print_ub(yy) ; 87
txt.nl()
xx=50
yy=40
yy = (xx-5)+(yy-10)
txt.print_ub(yy) ; 75
txt.nl()
xx=50
yy=20
yy = (xx-5)-(yy-10)
txt.print_ub(yy) ; 35
txt.nl()
repeat {
}
}
}