From 0c561d852877687fa1a65486136af73c74ce9d28 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 22 Mar 2020 23:50:15 +0100 Subject: [PATCH] fixed subroutine parameter value issue --- .../ast/processing/AstIdentifiersChecker.kt | 3 +- .../src/prog8/ast/statements/AstStatements.kt | 8 +- .../compiler/target/c64/codegen/AsmGen.kt | 1 + docs/source/todo.rst | 3 + examples/arithmetic/aggregates.p8 | 9 +-- examples/arithmetic/bitshift.p8 | 12 +-- examples/comparison_ifs_byte.p8 | 9 +-- examples/comparison_ifs_float.p8 | 9 +-- examples/comparison_ifs_ubyte.p8 | 9 +-- examples/comparison_ifs_uword.p8 | 9 +-- examples/comparison_ifs_word.p8 | 9 +-- examples/comparisons_byte.p8 | 9 +-- examples/comparisons_float.p8 | 9 +-- examples/comparisons_ubyte.p8 | 9 +-- examples/comparisons_uword.p8 | 9 +-- examples/comparisons_word.p8 | 9 +-- examples/fibonacci.p8 | 4 +- examples/lines-circles.p8 | 19 +---- examples/mandelbrot.p8 | 4 + examples/test.p8 | 16 ++-- examples/testprints.p8 | 73 ------------------- 21 files changed, 81 insertions(+), 161 deletions(-) delete mode 100644 examples/testprints.p8 diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index c5ec703e1..e8b1284ca 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -147,8 +147,7 @@ internal class AstIdentifiersChecker(private val program: Program, subroutine.parameters .filter { it.name !in namesInSub } .forEach { - val vardecl = VarDecl(VarDeclType.VAR, it.type, ZeropageWish.NOT_IN_ZEROPAGE, null, it.name, null, null, - isArray = false, autogeneratedDontRemove = true, position = subroutine.position) + val vardecl = ParameterVarDecl(it.name, it.type, subroutine.position) vardecl.linkParents(subroutine) subroutine.statements.add(0, vardecl) } diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index 7fb3fbcf1..61dd10f15 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -196,7 +196,8 @@ enum class ZeropageWish { NOT_IN_ZEROPAGE } -class VarDecl(val type: VarDeclType, + +open class VarDecl(val type: VarDeclType, private val declaredDatatype: DataType, val zeropage: ZeropageWish, var arraysize: ArrayIndex?, @@ -307,6 +308,11 @@ class VarDecl(val type: VarDeclType, } } +// a vardecl used only for subroutine parameters +class ParameterVarDecl(name: String, declaredDatatype: DataType, position: Position) + : VarDecl(VarDeclType.VAR, declaredDatatype, ZeropageWish.NOT_IN_ZEROPAGE, null, name, null, null, false, true, position) + + class ArrayIndex(var index: Expression, override val position: Position) : Node { override lateinit var parent: Node diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 0f74a136f..75ada9b43 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -604,6 +604,7 @@ internal class AsmGen(private val program: Program, internal fun translate(stmt: Statement) { outputSourceLine(stmt) when(stmt) { + is ParameterVarDecl -> { /* subroutine parameter vardecls don't get any special treatment here */ } is VarDecl -> translate(stmt) is StructDecl, is NopStatement -> {} is Directive -> translate(stmt) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index b521d044c..92a3966ef 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,6 +2,9 @@ TODO ==== +- fix manderbrot compiler issue + + - remove statements after an exit() or return - fix warnings about that unreachable code? diff --git a/examples/arithmetic/aggregates.p8 b/examples/arithmetic/aggregates.p8 index 727990d79..ed1341d0a 100644 --- a/examples/arithmetic/aggregates.p8 +++ b/examples/arithmetic/aggregates.p8 @@ -110,12 +110,11 @@ main { } sub check_eval_stack() { - c64scr.print("x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/arithmetic/bitshift.p8 b/examples/arithmetic/bitshift.p8 index 7ea78ed49..0194cb1fd 100644 --- a/examples/arithmetic/bitshift.p8 +++ b/examples/arithmetic/bitshift.p8 @@ -36,7 +36,8 @@ main { } sub start() { - ; TODO call this too: unimplemented() + ; TODO call this once implemented: + ; unimplemented() lsr(A) lsl(A) @@ -113,12 +114,11 @@ main { } sub check_eval_stack() { - c64scr.print("x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparison_ifs_byte.p8 b/examples/comparison_ifs_byte.p8 index 44e582930..84cdb6c0a 100644 --- a/examples/comparison_ifs_byte.p8 +++ b/examples/comparison_ifs_byte.p8 @@ -111,11 +111,10 @@ main { sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparison_ifs_float.p8 b/examples/comparison_ifs_float.p8 index c9b934790..381f6cb9d 100644 --- a/examples/comparison_ifs_float.p8 +++ b/examples/comparison_ifs_float.p8 @@ -110,12 +110,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparison_ifs_ubyte.p8 b/examples/comparison_ifs_ubyte.p8 index 851ef7403..44f430b92 100644 --- a/examples/comparison_ifs_ubyte.p8 +++ b/examples/comparison_ifs_ubyte.p8 @@ -110,12 +110,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparison_ifs_uword.p8 b/examples/comparison_ifs_uword.p8 index 2b2bbc6d6..c337da068 100644 --- a/examples/comparison_ifs_uword.p8 +++ b/examples/comparison_ifs_uword.p8 @@ -110,12 +110,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparison_ifs_word.p8 b/examples/comparison_ifs_word.p8 index 6f35beb68..b782c9345 100644 --- a/examples/comparison_ifs_word.p8 +++ b/examples/comparison_ifs_word.p8 @@ -142,12 +142,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparisons_byte.p8 b/examples/comparisons_byte.p8 index d605973bd..b5458ef0a 100644 --- a/examples/comparisons_byte.p8 +++ b/examples/comparisons_byte.p8 @@ -93,12 +93,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparisons_float.p8 b/examples/comparisons_float.p8 index 6c2ab1530..b7bac68ef 100644 --- a/examples/comparisons_float.p8 +++ b/examples/comparisons_float.p8 @@ -109,11 +109,10 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparisons_ubyte.p8 b/examples/comparisons_ubyte.p8 index de9bb3bce..336f4140a 100644 --- a/examples/comparisons_ubyte.p8 +++ b/examples/comparisons_ubyte.p8 @@ -93,12 +93,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparisons_uword.p8 b/examples/comparisons_uword.p8 index 1580e9655..e597b7dd0 100644 --- a/examples/comparisons_uword.p8 +++ b/examples/comparisons_uword.p8 @@ -123,12 +123,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/comparisons_word.p8 b/examples/comparisons_word.p8 index b1cab8178..459eb3d44 100644 --- a/examples/comparisons_word.p8 +++ b/examples/comparisons_word.p8 @@ -159,12 +159,11 @@ main { } sub check_eval_stack() { - c64scr.print("stack x=") - c64scr.print_ub(X) - if X==255 - c64scr.print(" ok\n") - else + if X!=255 { + c64scr.print("x=") + c64scr.print_ub(X) c64scr.print(" error!\n") + } } } diff --git a/examples/fibonacci.p8 b/examples/fibonacci.p8 index 93b6667d2..1cfd5a72a 100644 --- a/examples/fibonacci.p8 +++ b/examples/fibonacci.p8 @@ -12,8 +12,8 @@ main { } } - uword fib_prev = 0 ; TODO fix initialization of block-global vars (outside of a subroutine) - uword fib_current = 1 ; TODO fix initialization of block-global vars (outside of a subroutine) + uword fib_prev = 0 + uword fib_current = 1 sub fib_next() -> uword { uword new = fib_current + fib_prev diff --git a/examples/lines-circles.p8 b/examples/lines-circles.p8 index 67c8963b9..5dc0f3999 100644 --- a/examples/lines-circles.p8 +++ b/examples/lines-circles.p8 @@ -8,14 +8,10 @@ main { sub start() { c64scr.print("mid-point\ncircle\n and\nbresenham\nline\nalgorithms.\n") - circle(20, 12, 6) - circle(20, 12, 6) ; TODO FIX ERROR IN LOCALS - circle(20, 12, 6) ; TODO FIX ERROR IN LOCALS - -; ubyte r -; for r in 3 to 12 step 3 { -; circle(20, 12, r) -; } + ubyte r + for r in 3 to 12 step 3 { + circle(20, 12, r) + } c64scr.print("enter for disc:") void c64.CHRIN() @@ -112,13 +108,6 @@ main { byte y = 0 byte decisionOver2 = 1-x - c64scr.print_b(x) - c64.CHROUT(',') - c64scr.print_b(y) - c64.CHROUT(',') - c64scr.print_b(decisionOver2) - c64.CHROUT('\n') - while x>=y { c64scr.setcc(xcenter + x as ubyte, ycenter + y as ubyte, 81, 1) c64scr.setcc(xcenter - x as ubyte, ycenter + y as ubyte, 81, 2) diff --git a/examples/mandelbrot.p8 b/examples/mandelbrot.p8 index 468510dc3..44e442598 100644 --- a/examples/mandelbrot.p8 +++ b/examples/mandelbrot.p8 @@ -19,6 +19,10 @@ main { ubyte pixelx ubyte pixely + + ; TODO fix compiler - calculation is broken now + + for pixely in 0 to height-1 { float yy = (pixely as float)/0.4/height - 1.0 diff --git a/examples/test.p8 b/examples/test.p8 index 679408eaa..0e9fe783c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,11 +5,13 @@ main { - sub subje() { - ubyte yy=33 ; TODO reinitialize var here - ubyte zz ; TODO reinitialize var here + sub subje(ubyte arg1) { + ubyte yy=33 + ubyte zz ubyte[5] array1 = [1,2,3,4,5] + c64scr.print_ub(arg1) + c64.CHROUT(',') c64scr.print_ub(yy) c64.CHROUT(',') c64scr.print_ub(zz) @@ -22,10 +24,10 @@ main { sub start() { ubyte zz2 A=zz2 - subje() - subje() - subje() - subje() + subje(111) + subje(112) + subje(113) + subje(114) return ; ubyte ub1 diff --git a/examples/testprints.p8 b/examples/testprints.p8 deleted file mode 100644 index ba2062a45..000000000 --- a/examples/testprints.p8 +++ /dev/null @@ -1,73 +0,0 @@ -%zeropage basicsafe - -main { - - sub start() { - - c64scr.print("print uw0:") - c64scr.print_uw0(53204) - c64.CHROUT(' ') - c64scr.print_uw0(3204) - c64.CHROUT(' ') - c64scr.print_uw0(204) - c64.CHROUT(' ') - c64scr.print_uw0(14) - c64.CHROUT(' ') - c64scr.print_uw0(4) - c64.CHROUT(' ') - c64scr.print_uw0(0) - c64.CHROUT('\n') - - c64scr.print("print uw:") - c64scr.print_uw(53204) - c64.CHROUT(' ') - c64scr.print_uw(3204) - c64.CHROUT(' ') - c64scr.print_uw(204) - c64.CHROUT(' ') - c64scr.print_uw(14) - c64.CHROUT(' ') - c64scr.print_uw(4) - c64.CHROUT(' ') - c64scr.print_uw(0) - c64.CHROUT('\n') - - c64scr.print("print w:") - c64scr.print_w(23204) - c64.CHROUT(' ') - c64scr.print_w(3204) - c64.CHROUT(' ') - c64scr.print_w(204) - c64.CHROUT(' ') - c64scr.print_w(14) - c64.CHROUT(' ') - c64scr.print_w(4) - c64.CHROUT(' ') - c64scr.print_w(0) - c64.CHROUT('\n') - c64scr.print_w(-23204) - c64.CHROUT(' ') - c64scr.print_w(-3204) - c64.CHROUT(' ') - c64scr.print_w(-204) - c64.CHROUT(' ') - c64scr.print_w(-14) - c64.CHROUT(' ') - c64scr.print_w(-4) - c64.CHROUT(' ') - c64scr.print_w(-0) - c64.CHROUT('\n') - - check_eval_stack() - - } - - - sub check_eval_stack() { - if X!=255 { - c64scr.print("stack x=") - c64scr.print_ub(X) - c64scr.print(" error!\n") - } - } -}