From fb0d7a190837831362a0c803fe6aa6a7412149ba Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 18 Aug 2019 13:38:03 +0200 Subject: [PATCH] some array literals weren't put on the heap --- compiler/build.gradle | 3 - .../ast/processing/AstIdentifiersChecker.kt | 8 +- compiler/src/prog8/compiler/Main.kt | 2 +- .../src/prog8/optimizer/ConstantFolding.kt | 2 + examples/test.p8 | 104 ++---------------- examples/testforloops.p8 | 6 +- 6 files changed, 18 insertions(+), 107 deletions(-) diff --git a/compiler/build.gradle b/compiler/build.gradle index c46121238..4c5cfd5e8 100644 --- a/compiler/build.gradle +++ b/compiler/build.gradle @@ -81,9 +81,6 @@ artifacts { } -// To create a fat-jar use the 'create_compiler_jar' script for now -// @todo investigate https://imperceptiblethoughts.com/shadow/introduction/ - shadowJar { baseName = 'prog8compiler' version = prog8version diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index a5a62e25a..80fb83d19 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -67,7 +67,7 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi checkResult.add(NameError("builtin function cannot be redefined", decl.position)) if(decl.name in AssemblyProgram.opcodeNames) - checkResult.add(NameError("can't use a cpu opcode name as a symbol", decl.position)) + checkResult.add(NameError("can't use a cpu opcode name as a symbol: '${decl.name}'", decl.position)) // is it a struct variable? then define all its struct members as mangled names, // and include the original decl as well. @@ -104,7 +104,7 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi override fun visit(subroutine: Subroutine): Statement { if(subroutine.name in AssemblyProgram.opcodeNames) { - checkResult.add(NameError("can't use a cpu opcode name as a symbol", subroutine.position)) + checkResult.add(NameError("can't use a cpu opcode name as a symbol: '${subroutine.name}'", subroutine.position)) } else if(subroutine.name in BuiltinFunctions) { // the builtin functions can't be redefined checkResult.add(NameError("builtin function cannot be redefined", subroutine.position)) @@ -165,7 +165,7 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi override fun visit(label: Label): Statement { if(label.name in AssemblyProgram.opcodeNames) - checkResult.add(NameError("can't use a cpu opcode name as a symbol", label.position)) + checkResult.add(NameError("can't use a cpu opcode name as a symbol: '${label.name}'", label.position)) if(label.name in BuiltinFunctions) { // the builtin functions can't be redefined @@ -429,8 +429,6 @@ internal fun fixupArrayDatatype(array: ArrayLiteralValue, vardecl: VarDecl, prog litval2.addToHeap(program.heap) return litval2 } - } else { - array.addToHeap(program.heap) } return array } diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index e4a2b0ffc..304e34c7c 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -96,7 +96,7 @@ fun compileProgram(filepath: Path, programAst.checkValid(compilerOptions) // check if final tree is valid programAst.checkRecursion() // check if there are recursive subroutine calls - printAst(programAst) + // printAst(programAst) if(writeAssembly) { // asm generation directly from the Ast, no need for intermediate code diff --git a/compiler/src/prog8/optimizer/ConstantFolding.kt b/compiler/src/prog8/optimizer/ConstantFolding.kt index b8a5e9164..8466abea5 100644 --- a/compiler/src/prog8/optimizer/ConstantFolding.kt +++ b/compiler/src/prog8/optimizer/ConstantFolding.kt @@ -601,6 +601,8 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { override fun visit(arrayLiteral: ArrayLiteralValue): Expression { val array = super.visit(arrayLiteral) if(array is ArrayLiteralValue) { + if(array.heapId==null) + array.addToHeap(program.heap) val vardecl = array.parent as? VarDecl return if (vardecl!=null) { fixupArrayDatatype(array, vardecl, program) diff --git a/examples/test.p8 b/examples/test.p8 index 569c2b45f..59ec3b405 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,104 +7,18 @@ main { sub start() { ubyte i - for i in 0 to 10 { - A=i + + for i in [1,3,5,99] { + c64scr.print_ub(i) + c64.CHROUT(',') } + c64.CHROUT('\n') - for i in 0 to 10 { - Y=i + for A in [1,3,5,99] { + c64scr.print_ub(A) + c64.CHROUT(',') } - -; ubyte[] uba = [10,0,2,8,5,4,3,9] -; uword[] uwa = [1000,0,200,8000,50,40000,3,900] -; byte[] ba = [-10,0,-2,8,5,4,-3,9,-99] -; word[] wa = [-1000,0,-200,8000,50,31111,3,-900] -; -; for ubyte ub in uba { -; c64scr.print_ub(ub) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for uword uw in uwa { -; c64scr.print_uw(uw) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for byte bb in ba { -; c64scr.print_b(bb) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for word ww in wa { -; c64scr.print_w(ww) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; c64.CHROUT('\n') -; -; sort(uba) -; sort(uwa) -; sort(ba) -; sort(wa) -; -; for ubyte ub2 in uba { -; c64scr.print_ub(ub2) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for uword uw2 in uwa { -; c64scr.print_uw(uw2) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for byte bb2 in ba { -; c64scr.print_b(bb2) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for word ww2 in wa { -; c64scr.print_w(ww2) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; c64.CHROUT('\n') -; -; reverse(uba) -; reverse(uwa) -; reverse(ba) -; reverse(wa) -; -; for ubyte ub3 in uba { -; c64scr.print_ub(ub3) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for uword uw3 in uwa { -; c64scr.print_uw(uw3) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for byte bb3 in ba { -; c64scr.print_b(bb3) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; -; for word ww3 in wa { -; c64scr.print_w(ww3) -; c64.CHROUT(',') -; } -; c64.CHROUT('\n') -; c64.CHROUT('\n') - + c64.CHROUT('\n') } } diff --git a/examples/testforloops.p8 b/examples/testforloops.p8 index 79fe28f17..a7ba3a9d4 100644 --- a/examples/testforloops.p8 +++ b/examples/testforloops.p8 @@ -105,8 +105,9 @@ main { } c64.CHROUT('\n') - for uw in warr { - c64scr.print_w(uw) + word ww + for ww in warr { + c64scr.print_w(ww) c64.CHROUT(',') } c64.CHROUT('\n') @@ -129,7 +130,6 @@ main { } c64.CHROUT('\n') - word ww for ww in 999 to -999 step -500 { c64scr.print_w(ww) c64.CHROUT(',')