diff --git a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt index b7b522f51..68a674e6e 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt @@ -23,7 +23,12 @@ internal fun Program.processAstBeforeAsmGeneration(errors: IErrorReporter, compT internal fun Program.reorderStatements(errors: IErrorReporter) { val reorder = StatementReorderer(this, errors) reorder.visit(this) - reorder.applyModifications() + if(errors.noErrors()) { + reorder.applyModifications() + reorder.visit(this) + if(errors.noErrors()) + reorder.applyModifications() + } } internal fun Program.addTypecasts(errors: IErrorReporter) { diff --git a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt index bcc84d662..0265c4222 100644 --- a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt +++ b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt @@ -210,7 +210,7 @@ internal class StatementReorderer(val program: Program, val errors: IErrorReport val assign = Assignment(target, declValue, decl.position) return listOf( IAstModification.ReplaceNode(decl, assign, parent), - IAstModification.InsertFirst(decl, decl.definingScope()) + IAstModification.InsertFirst(decl.copy(), decl.definingScope()) ) } } diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index 0bce281a1..854f6c166 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -234,9 +234,7 @@ open class VarDecl(val type: VarDeclType, } override fun replaceChildNode(node: Node, replacement: Node) { - require(replacement is Expression && node===value) - // NOTE: ideally you also want to check that node===value but this sometimes crashes the optimizer when queueing multiple node replacements - // just accept the risk of having the wrong node specified in the IAstModification object... + require(replacement is Expression && (value==null || node===value)) value = replacement replacement.parent = this } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 93dd5395a..394c43d39 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,6 +2,8 @@ TODO ==== +- optimize %32 to & 31 + - optimize several inner loops in gfx2 - hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine) - optimize swap of two memread values with index, using the same pointer expression/variable, like swap(@(ptr+1), @(ptr+2)) diff --git a/examples/test.p8 b/examples/test.p8 index 2d6e7e89a..acdd62e61 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,54 +1,12 @@ -%target cx16 -%import textio -%option no_sysinit -%zeropage basicsafe - -; simple test program for the "VTUI" text user interface library -; see: https://github.com/JimmyDansbo/VTUIlib main { sub start() { - vtui.initialize() - txt.print("ok") + + uword world = $2000 + ubyte xx + + ubyte chr = world[2] + ubyte chr2 = world[xx] } } - -vtui $1000 { - - %asmbinary "cx16/vtui/VTUI0.8.BIN", 2 ; skip the 2 dummy load address bytes - - ; NOTE: base address $1000 here must be the same as the block's memory address, for obvious reasons! - ; The routines below are for VTUI 0.8 - romsub $1000 = initialize() clobbers(A, X, Y) - romsub $1002 = screen_set(ubyte mode @A) clobbers(A, X, Y) - romsub $1005 = set_bank(ubyte bank @Pc) clobbers(A) - romsub $1008 = set_stride(ubyte stride @A) clobbers(A) - romsub $100b = set_decr(ubyte incrdecr @Pc) clobbers(A) - romsub $100e = clr_scr(ubyte char @A, ubyte colors @X) clobbers(Y) - romsub $1011 = gotoxy(ubyte column @A, ubyte row @Y) - romsub $1014 = plot_char(ubyte char @A, ubyte colors @X) - romsub $1017 = scan_char() -> ubyte @A, ubyte @X - romsub $101a = hline(ubyte char @A, ubyte length @Y, ubyte colors @X) clobbers(A) - romsub $101d = vline(ubyte char @A, ubyte height @Y, ubyte colors @X) clobbers(A) - romsub $1020 = print_str(str txtstring @R0, ubyte length @Y, ubyte colors @X, ubyte convertchars @A) clobbers(A, Y) - romsub $1023 = fill_box(ubyte char @A, ubyte width @R1, ubyte height @R2, ubyte colors @X) clobbers(A, Y) - romsub $1026 = pet2scr(ubyte char @A) -> ubyte @A - romsub $1029 = scr2pet(ubyte char @A) -> ubyte @A - romsub $102c = border(ubyte mode @A, ubyte width @R1, ubyte height @R2, ubyte colors @X) clobbers(Y) ; NOTE: mode 6 means 'custom' characters taken from r3 - r6 - romsub $102f = save_rect(ubyte ramtype @A, ubyte vbank @Pc, uword address @R0, ubyte width @R1, ubyte height @R2) clobbers(A, X, Y) - romsub $1032 = rest_rect(ubyte ramtype @A, ubyte vbank @Pc, uword address @R0, ubyte width @R1, ubyte height @R2) clobbers(A, X, Y) - romsub $1035 = input_str(uword buffer @R0, ubyte buflen @Y, ubyte colors @X) clobbers (A) -> ubyte @Y - - ; -- helper function to do string length counting for you internally - asmsub print_str2(str txtstring @R0, ubyte colors @X, ubyte convertchars @A) clobbers(A, Y) { - %asm {{ - pha - lda cx16.r0 - ldy cx16.r0+1 - jsr prog8_lib.strlen - pla - jmp print_str - }} - } -}