vm: fix array iteration

This commit is contained in:
Irmen de Jong 2022-05-23 21:24:36 +02:00
parent e659b91c4d
commit 3f5877dbcc
3 changed files with 40 additions and 17 deletions

View File

@ -221,17 +221,27 @@ class CodeGen(internal val program: PtProgram,
val elementDt = ArrayToElementTypes.getValue(iterable.type)
val elementSize = program.memsizer.memorySize(elementDt)
val lengthBytes = iterableVar.length!! * elementSize
val lengthReg = vmRegisters.nextFree()
code += VmCodeInstruction(Opcode.LOAD, VmDataType.BYTE, reg1=indexReg, value=0)
code += VmCodeInstruction(Opcode.LOAD, VmDataType.BYTE, reg1=lengthReg, value=lengthBytes)
code += VmCodeLabel(loopLabel)
code += VmCodeInstruction(Opcode.BEQ, VmDataType.BYTE, reg1=indexReg, reg2=lengthReg, labelSymbol = endLabel)
code += VmCodeInstruction(Opcode.LOADX, vmType(elementDt), reg1=tmpReg, reg2=indexReg, value=arrayAddress)
code += VmCodeInstruction(Opcode.STOREM, vmType(elementDt), reg1=tmpReg, value = loopvarAddress)
code += translateNode(forLoop.statements)
code += addConstReg(VmDataType.BYTE, indexReg, elementSize)
code += VmCodeInstruction(Opcode.JUMP, labelSymbol = loopLabel)
code += VmCodeLabel(endLabel)
if(lengthBytes<256) {
val lengthReg = vmRegisters.nextFree()
code += VmCodeInstruction(Opcode.LOAD, VmDataType.BYTE, reg1=indexReg, value=0)
code += VmCodeInstruction(Opcode.LOAD, VmDataType.BYTE, reg1=lengthReg, value=lengthBytes)
code += VmCodeLabel(loopLabel)
code += VmCodeInstruction(Opcode.LOADX, vmType(elementDt), reg1=tmpReg, reg2=indexReg, value=arrayAddress)
code += VmCodeInstruction(Opcode.STOREM, vmType(elementDt), reg1=tmpReg, value = loopvarAddress)
code += translateNode(forLoop.statements)
code += addConstReg(VmDataType.BYTE, indexReg, elementSize)
code += VmCodeInstruction(Opcode.BNE, VmDataType.BYTE, reg1=indexReg, reg2=lengthReg, labelSymbol = loopLabel)
} else if(lengthBytes==256) {
code += VmCodeInstruction(Opcode.LOAD, VmDataType.BYTE, reg1=indexReg, value=0)
code += VmCodeLabel(loopLabel)
code += VmCodeInstruction(Opcode.LOADX, vmType(elementDt), reg1=tmpReg, reg2=indexReg, value=arrayAddress)
code += VmCodeInstruction(Opcode.STOREM, vmType(elementDt), reg1=tmpReg, value = loopvarAddress)
code += translateNode(forLoop.statements)
code += addConstReg(VmDataType.BYTE, indexReg, elementSize)
code += VmCodeInstruction(Opcode.BNZ, VmDataType.BYTE, reg1=indexReg, labelSymbol = loopLabel)
} else {
throw AssemblyError("iterator length should never exceed 256")
}
}
}
else -> throw AssemblyError("weird for iterable")

View File

@ -40,6 +40,10 @@ main {
return 0 ; we wrapped; no more primes available in the sieve
}
txt.print("candidate: ")
txt.print_ub(candidate_prime)
txt.nl()
; found next one, mark the multiples and return it.
sieve[candidate_prime] = true
uword multiple = candidate_prime
@ -49,6 +53,14 @@ main {
sieve[lsb(multiple)] = true
multiple += candidate_prime
}
ubyte xx
for xx in sieve {
txt.print_ub(xx)
txt.spc()
}
txt.nl()
return candidate_prime
}
}

View File

@ -31,16 +31,17 @@ main {
sub start() {
; mcCarthy()
ubyte[256] sieve
ubyte[20] sieve
uword count=0
ubyte xx
for xx in 0 to 255 {
sieve[xx] = false
}
for xx in 0 to 255 {
txt.print_ub(sieve[xx])
for xx in sieve {
txt.print_ub(xx)
txt.spc()
count++
}
txt.nl()
txt.print_uw(count)
txt.nl()
; ; a "pixelshader":