fixed stack/branch bug in for loop

This commit is contained in:
Irmen de Jong 2019-01-17 23:05:57 +01:00
parent f7dcdceaaf
commit 740dedc7a1
3 changed files with 77 additions and 14 deletions

View File

@ -2067,13 +2067,13 @@ private class StatementTranslator(private val prog: IntermediateProgram,
val opcode = opcodePushvar(targetStatement!!.datatype) val opcode = opcodePushvar(targetStatement!!.datatype)
prog.instr(opcode, callLabel = targetStatement.scopedname) prog.instr(opcode, callLabel = targetStatement.scopedname)
} }
val branch = BranchStatement( // TODO: optimize this to use a compare + branch opcode somehow?
BranchCondition.NZ, val conditionJumpOpcode = when(targetStatement!!.datatype) {
AnonymousScope(mutableListOf(Jump(null, null, loopLabel, range.position)), range.position), DataType.UBYTE, DataType.BYTE -> Opcode.JNZ
AnonymousScope(mutableListOf(), range.position), DataType.UWORD, DataType.WORD -> Opcode.JNZW
range.position) else -> throw CompilerException("invalid loopvar datatype (expected byte or word) $lvTarget")
branch.linkParents(body) }
translate(branch) prog.instr(conditionJumpOpcode, callLabel = loopLabel)
} }
when (literalStepValue) { when (literalStepValue) {

View File

@ -91,7 +91,7 @@
c64.PLOT(0,0,0) c64.PLOT(0,0,0)
c64scr.print("3d cube! (sprites) ") c64scr.print("3d cube! (sprites) ")
c64scr.print_ub(c64.TIME_LO) c64scr.print_ub(c64.TIME_LO)
c64scr.print(" jiffies/frame") c64scr.print(" jiffies/frame ")
} }
} }
@ -133,7 +133,26 @@
sub position_sprites() { sub position_sprites() {
; set each of the 8 sprites to the correct vertex of the cube ; 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 { for ubyte i in 0 to 7 {
word zc = rotatedz[i] word zc = rotatedz[i]

View File

@ -5,14 +5,58 @@
sub start() { sub start() {
str s = "hello" word[8] rotatedx = [11,33,55,77,22,44,66,88]
byte[4] ba 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 sub printarray() {
x = 33+ba 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')
}
} }
} }