fix forloop 6502 codegen in case of descending word values

This commit is contained in:
Irmen de Jong 2023-07-11 00:27:30 +02:00
parent 6b6427492d
commit 795f80b4ec
3 changed files with 26 additions and 54 deletions

View File

@ -552,16 +552,16 @@ class IRCodeGen(
addInstr(result, IRInstruction(Opcode.STOREM, loopvarDtIr, reg1=fromTr.resultReg, labelSymbol=loopvarSymbol), null) addInstr(result, IRInstruction(Opcode.STOREM, loopvarDtIr, reg1=fromTr.resultReg, labelSymbol=loopvarSymbol), null)
result += labelFirstChunk(translateNode(forLoop.statements), loopLabel) result += labelFirstChunk(translateNode(forLoop.statements), loopLabel)
addInstr(result, IRInstruction(Opcode.LOADM, loopvarDtIr, reg1 = fromTr.resultReg, labelSymbol = loopvarSymbol), null)
if(step==1 || step==-1) { if(step==1 || step==-1) {
// if endvalue == index, stop loop, else iterate // if endvalue == loopvar, stop loop, else iterate
addInstr(result, IRInstruction(Opcode.LOADM, loopvarDtIr, reg1 = fromTr.resultReg, labelSymbol = loopvarSymbol), null)
addInstr(result, IRInstruction(Opcode.BEQR, loopvarDtIr, reg1=toTr.resultReg, reg2=fromTr.resultReg, labelSymbol = labelAfterFor), null) addInstr(result, IRInstruction(Opcode.BEQR, loopvarDtIr, reg1=toTr.resultReg, reg2=fromTr.resultReg, labelSymbol = labelAfterFor), null)
result += addConstMem(loopvarDtIr, null, loopvarSymbol, step) result += addConstMem(loopvarDtIr, null, loopvarSymbol, step)
addInstr(result, IRInstruction(Opcode.JUMP, labelSymbol = loopLabel), null) addInstr(result, IRInstruction(Opcode.JUMP, labelSymbol = loopLabel), null)
} else { } else {
// ind/dec index, then: // ind/dec index, then:
// ascending: if endvalue >= index, iterate // ascending: if endvalue >= loopvar, iterate
// descending: if index >= endvalue, iterate // descending: if loopvar >= endvalue, iterate
result += addConstMem(loopvarDtIr, null, loopvarSymbol, step) result += addConstMem(loopvarDtIr, null, loopvarSymbol, step)
addInstr(result, IRInstruction(Opcode.LOADM, loopvarDtIr, reg1=fromTr.resultReg, labelSymbol = loopvarSymbol), null) addInstr(result, IRInstruction(Opcode.LOADM, loopvarDtIr, reg1=fromTr.resultReg, labelSymbol = loopvarSymbol), null)
if(step > 0) if(step > 0)

View File

@ -1,11 +1,7 @@
TODO TODO
==== ====
- investigate if the hiscores issue on the forum is a compiler bug or not.
- IR: instructions that do type conversion (SZ etc, CONCAT, SGN) should put the result in a DIFFERENT register. - IR: instructions that do type conversion (SZ etc, CONCAT, SGN) should put the result in a DIFFERENT register.
- IR: optimize forloop codegen to use a reg as loop variable instead of a memory variable, just store it in the mem var before entering the loop body
check repeat loop as well it shouldn't be using a tempvar just a register to count.
... ...

View File

@ -1,56 +1,32 @@
%import string
%import textio %import textio
%import conv
%zeropage basicsafe %zeropage basicsafe
; (127 instructions in 15 chunks, 47 registers)
; 679 steps
main { main {
str[10] names = ["1??","2??","3??","4??","5??","6??","7??","8??","9??","a??"]
str[10] scores = ["9000","8000","7000","6000","5000","4000","3000","2000","1000","50"]
str p_str = "winner"
uword top_score
sub start() { sub start() {
print_hiscores() uword i
ubyte r = check_score(7500) uword n
print_hiscores()
}
sub check_score(uword score) -> ubyte { repeat 10 {
ubyte n txt.chrout('.')
ubyte i }
str tmp = "?????" txt.nl()
n=10
for i in 0 to n step 3 {
txt.print_uw(i)
txt.nl()
}
txt.nl()
if score < top_score { n=0
return(10) } for i in 10 downto n step -3 {
else { txt.print_uw(i)
for n in 0 to 9 { txt.nl()
if score > conv.str2uword(scores[n]) { }
for i in 8 to n step -1 {
names[i+1] = names[i]
scores[i+1] = scores[i]
}
;string.copy(p_str,names[n])
;conv.str_uw0(score)
;string.copy(conv.string_out,scores[n])
return(n)
}
}
}
} }
sub print_hiscores() {
ubyte n
for n in 0 to 9 {
txt.print_ub(n)
txt.spc()
txt.print(names[n])
txt.spc()
txt.print(scores[n])
txt.nl()
}
txt.nl()
}
} }