From 740dedc7a1d35e257d6bed7c67b09070b8fd4096 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 17 Jan 2019 23:05:57 +0100 Subject: [PATCH] fixed stack/branch bug in for loop --- compiler/src/prog8/compiler/Compiler.kt | 14 +++---- examples/cube3d-sprites.p8 | 23 ++++++++++- examples/test.p8 | 54 ++++++++++++++++++++++--- 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index ffd0d335c..a443bddcb 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -2067,13 +2067,13 @@ private class StatementTranslator(private val prog: IntermediateProgram, val opcode = opcodePushvar(targetStatement!!.datatype) prog.instr(opcode, callLabel = targetStatement.scopedname) } - val branch = BranchStatement( - BranchCondition.NZ, - AnonymousScope(mutableListOf(Jump(null, null, loopLabel, range.position)), range.position), - AnonymousScope(mutableListOf(), range.position), - range.position) - branch.linkParents(body) - translate(branch) + // TODO: optimize this to use a compare + branch opcode somehow? + val conditionJumpOpcode = when(targetStatement!!.datatype) { + DataType.UBYTE, DataType.BYTE -> Opcode.JNZ + DataType.UWORD, DataType.WORD -> Opcode.JNZW + else -> throw CompilerException("invalid loopvar datatype (expected byte or word) $lvTarget") + } + prog.instr(conditionJumpOpcode, callLabel = loopLabel) } when (literalStepValue) { diff --git a/examples/cube3d-sprites.p8 b/examples/cube3d-sprites.p8 index 2a025f447..3a6ad37a1 100644 --- a/examples/cube3d-sprites.p8 +++ b/examples/cube3d-sprites.p8 @@ -91,7 +91,7 @@ c64.PLOT(0,0,0) c64scr.print("3d cube! (sprites) ") c64scr.print_ub(c64.TIME_LO) - c64scr.print(" jiffies/frame") + c64scr.print(" jiffies/frame ") } } @@ -133,7 +133,26 @@ sub position_sprites() { ; set each of the 8 sprites to the correct vertex of the cube - ; @todo sort vertices to sprite order so the back/front order is correct as well + + ; first sort vertices to sprite order so the back/front order is correct as well + ; (chose to do a simple bubble sort it's only 8 items to sort) + for ubyte sorti in 6 to 0 step -1 { + for ubyte i1 in 0 to sorti { + ubyte i2 = i1+1 + if(rotatedz[i1] > rotatedz[i2]) { + ; @todo use a swap() builtin function? + word temp = rotatedx[i1] + rotatedx[i1] = rotatedx[i2] + rotatedx[i2] = temp + temp = rotatedy[i1] + rotatedy[i1] = rotatedy[i2] + rotatedy[i2] = temp + temp = rotatedz[i1] + rotatedz[i1] = rotatedz[i2] + rotatedz[i2] = temp + } + } + } for ubyte i in 0 to 7 { word zc = rotatedz[i] diff --git a/examples/test.p8 b/examples/test.p8 index 5a3ce9173..8bf2cd1c9 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,14 +5,58 @@ sub start() { - str s = "hello" - byte[4] ba + word[8] rotatedx = [11,33,55,77,22,44,66,88] + word[8] rotatedy = [11,33,55,77,22,44,66,88] + word[8] rotatedz = [1,3,-5,7,2,4,-6,8] + + printarray() + + c64scr.print_ub(X) + c64.CHROUT('\n') + + for ubyte sorti in 6 to 0 step -1 { + for ubyte i1 in 0 to sorti { + ubyte i2=i1+1 + if(rotatedz[i2]>rotatedz[i1]) { + word t = rotatedx[i1] + rotatedx[i1] = rotatedx[i2] + rotatedx[i2] = t + t = rotatedy[i1] + rotatedy[i1] = rotatedy[i2] + rotatedy[i2] = t + t = rotatedz[i1] + rotatedz[i1] = rotatedz[i2] + rotatedz[i2] = t + } + } + } + + c64scr.print_ub(X) + c64.CHROUT('\n') + + printarray() - float x = 33+s - x = 33+ba - + sub printarray() { + for word a in rotatedx { + c64scr.print_w(a) + c64.CHROUT(',') + } + c64.CHROUT('\n') + for word a in rotatedy { + c64scr.print_w(a) + c64.CHROUT(',') + } + c64.CHROUT('\n') + for word a in rotatedz { + c64scr.print_w(a) + c64.CHROUT(',') + } + c64.CHROUT('\n') + c64.CHROUT('\n') + } } + }