expression splitter for vardecls with binexpr init expression

This commit is contained in:
Irmen de Jong 2020-10-02 00:34:12 +02:00
parent 110f877dcc
commit 24c8d1f1f4
3 changed files with 46 additions and 119 deletions

View File

@ -16,32 +16,17 @@ class ExpressionSplitter(private val program: Program) : AstWalker() {
// TODO once this works, integrate it back into expressionsimplifier // TODO once this works, integrate it back into expressionsimplifier
override fun after(decl: VarDecl, parent: Node): Iterable<IAstModification> { override fun after(decl: VarDecl, parent: Node): Iterable<IAstModification> {
val expr = decl.value as? BinaryExpression if(decl.type==VarDeclType.VAR) {
if (expr != null) { val binExpr = decl.value as? BinaryExpression
// reduce the complexity of a (binary) expression that has to be evaluated on the eval stack, if (binExpr != null) {
// by attempting to splitting it up into individual simple steps: // split into a vardecl with just the left expression, and an aug. assignment with the right expression.
// X = <some-expression-not-X> <operator> <not-binary-expression> val augExpr = BinaryExpression(IdentifierReference(listOf(decl.name), decl.position), binExpr.operator, binExpr.right, binExpr.position)
// or X = <not-binary-expression> <associativeoperator> <some-expression-not-X> val target = AssignTarget(IdentifierReference(listOf(decl.name), decl.position), null, null, decl.position)
// split that into X = <some-expression-not-X> ; X = X <operator> <not-binary-expression> val assign = Assignment(target, augExpr, binExpr.position)
return listOf(
// TODO DOES THIS LOOP AS WELL? IAstModification.SetExpression({ decl.value = it }, binExpr.left, decl),
if (expr.operator !in comparisonOperators && decl.type==VarDeclType.VAR) { IAstModification.InsertAfter(decl, assign, parent)
if (expr.right !is BinaryExpression) { )
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))
}
} }
} }

View File

@ -2,16 +2,11 @@
%import syslib %import syslib
%import textio %import textio
; converted from plasma test program for cc65.
;/*****************************************************************************\ ; which is (w)2001 by groepaz/hitmen
;** plasma test program for cc65. ** ;
;** ** ; Cleanup and porting to C by Ullrich von Bassewitz.
;** (w)2001 by groepaz/hitmen ** ; Converted to prog8 by Irmen de Jong.
;** **
;** Cleanup and porting by Ullrich von Bassewitz. **
;** Converted to prog8 by Irmen de Jong **
;** **
;\*****************************************************************************/
main { main {
@ -78,8 +73,7 @@ main {
for y in 24 downto 0 { for y in 24 downto 0 {
for x in 39 downto 0 { for x in 39 downto 0 {
; using a temp var here to enable expression optimization that can't be done on a 'problematic' ROM/RAM memory location ; using a temp var here to enable expression optimization that can't be done on a 'problematic' ROM/RAM memory location
ubyte cc ubyte cc = xbuf[x] + ybuf[y]
cc = xbuf[x] + ybuf[y] ; TODO should be split!!
@(screen) = cc @(screen) = cc
; this is the fastest way to do this inner part: ; this is the fastest way to do this inner part:
; %asm {{ ; %asm {{

View File

@ -6,96 +6,44 @@
main { main {
struct Color {
; Color c1 = [11,22,33] ; TODO fix crash ubyte red
; Color c2 = [11,22,33] ; TODO fix crash ubyte green
; Color c3 = [11,22,33] ; TODO fix crash ubyte blue
; uword[] colors = [ c1, c2, c3] ; TODO should contain pointers to (the first element) of each struct }
; str[] names = ["aap", "noot", "mies", "vuur"] Color c1 = [11,22,33]
; uword[] names3 = ["aap", "noot", "mies", "vuur"] Color c2 = [11,22,33]
; ubyte[] values = [11,22,33,44] Color c3 = [11,22,33]
; uword[] arrays = [names, names3, values] uword[] colors = [ c1, c2, c3] ; TODO should contain pointers to (the first element) of each struct
; 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() { sub start() {
; byte bb = 100
; word ww = 30000
; float ff1 = 1000
; float ff2 = -1000
ubyte[10] xbuf Color c1 = [11,22,33]
ubyte[10] ybuf Color c2 = [11,22,33]
ubyte x Color c3 = [11,22,33]
ubyte y uword[] colors = [ c1, c2, c3] ; TODO should contain pointers to (the first element) of each struct
ubyte cc = xbuf[x] + ybuf[y] ; TODO should be split!! also fix plasma.p8 c1 = c2
ubyte cc2 ; c1 = [11,22,33] ; TODO implement rewrite into individual struct member assignments
cc2 = xbuf[x] + ybuf[y] +cc ; will be split correctly. }
return
; ff1 = 1+((-ff1) *3) asmsub testX() {
; floats.print_f(ff1) %asm {{
; floats.print_f(1+((-1000) *3)) stx _saveX
; testX() lda #13
; ff1 = 1+((-ff2) *3) jsr txt.chrout
; floats.print_f(ff1) lda _saveX
; floats.print_f(1+((- (-1000)) *3)) jsr txt.print_ub
; txt.chrout('\n') lda #13
; testX() jsr txt.chrout
; return ldx _saveX
rts
; struct Color { _saveX .byte 0
; ubyte red }}
; ubyte green
; ubyte blue
; }
;
; ;Color c1 = [11,22,33] ; TODO fix struct initializer crash
; Color c1
; Color c2
; Color c3
; ;Color c2 = [11,22,33]
; ;Color c3 = [11,22,33]
; ;uword[] colors = [ c1, c2, c3] ; TODO should contain pointers to (the first element) of each struct
;
; c1 = c2
; ;c1 = [11,22,33] ; TODO rewrite into individual struct member assignments
;
;
; uword s
; for s in names {
; txt.print(s)
; txt.chrout('\n')
; }
; txt.chrout('\n')
;
; txt.print(names[2])
; txt.chrout('\n')
; txt.print(names[3])
; txt.chrout('\n')
;
; repeat {
; txt.print(names3[rnd()&3])
; txt.chrout(' ')
; }
} }
} }