IR fix unneeded register allocated for array indexing with variable

This commit is contained in:
Irmen de Jong 2023-03-14 01:12:52 +01:00
parent a2133f61a8
commit 92276b5769
4 changed files with 19 additions and 40 deletions

View File

@ -197,8 +197,8 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
val chunk = IRCodeChunk(null, null).also { it += IRInstruction(Opcode.STOREZM, vmDt, labelSymbol = "$variable+$offset") }
result += chunk
} else {
val indexReg = codeGen.registers.nextFree()
result += loadIndexReg(targetArray, itemsize, indexReg)
val (code, indexReg) = loadIndexReg(targetArray, itemsize)
result += code
result += IRCodeChunk(null, null).also { it += IRInstruction(Opcode.STOREZX, vmDt, reg1=indexReg, labelSymbol = variable) }
}
} else {
@ -208,8 +208,8 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
val chunk = IRCodeChunk(null, null).also { it += IRInstruction(Opcode.STOREM, vmDt, fpReg1 = valueFpRegister, labelSymbol = "$variable+$offset") }
result += chunk
} else {
val indexReg = codeGen.registers.nextFree()
result += loadIndexReg(targetArray, itemsize, indexReg)
val (code, indexReg) = loadIndexReg(targetArray, itemsize)
result += code
result += IRCodeChunk(null, null).also { it += IRInstruction(Opcode.STOREX, vmDt, reg1 = indexReg, fpReg1 = valueFpRegister, labelSymbol = variable) }
}
} else {
@ -218,8 +218,8 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
val chunk = IRCodeChunk(null, null).also { it += IRInstruction(Opcode.STOREM, vmDt, reg1 = valueRegister, labelSymbol = "$variable+$offset") }
result += chunk
} else {
val indexReg = codeGen.registers.nextFree()
result += loadIndexReg(targetArray, itemsize, indexReg)
val (code, indexReg) = loadIndexReg(targetArray, itemsize)
result += code
result += IRCodeChunk(null, null).also { it += IRInstruction(Opcode.STOREX, vmDt, reg1 = valueRegister, reg2=indexReg, labelSymbol = variable) }
}
}
@ -256,7 +256,8 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
throw AssemblyError("weird assigntarget")
}
private fun loadIndexReg(array: PtArrayIndexer, itemsize: Int, indexReg: Int): IRCodeChunks {
private fun loadIndexReg(array: PtArrayIndexer, itemsize: Int): Pair<IRCodeChunks, Int> {
// returns the code to load the Index into the register, which is also return\ed.
val result = mutableListOf<IRCodeChunkBase>()
val tr = if(itemsize==1) {
expressionEval.translateExpression(array.index)
@ -266,7 +267,7 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
mult.children += PtNumber(DataType.UBYTE, itemsize.toDouble(), array.position)
expressionEval.translateExpression(mult)
}
addToResult(result, tr, indexReg, -1)
return result
addToResult(result, tr, tr.resultReg, -1)
return Pair(result, tr.resultReg)
}
}

View File

@ -3,8 +3,7 @@ TODO
For next minor release
^^^^^^^^^^^^^^^^^^^^^^
- array[var] = 22 generates lareger code now in IR (-> bouncegfx is larger than with 8.10)
- array[var] *= -1 generates lareger code now in IR (-> bouncegfx is larger than with 8.10)
- IR: animals and textelite take more registers now than with 8.10 (code *is* smaller) see test.p8
...

View File

@ -74,7 +74,7 @@ main {
; (this is controlled by returning 0 or 1 in register A here)
;
; see:
; https://github.com/X16Community/x16-docs/tree/X16%20Reference%20-%2002%20-%20Editor.md#custom-keyboard-scancode-handler
; https://github.com/X16Community/x16-docs/blob/master/X16%20Reference%20-%2002%20-%20Editor.md#custom-keyboard-scancode-handler
%asm {{
php

View File

@ -1,32 +1,11 @@
%import textio
%option no_sysinit
%zeropage basicsafe
%import floats
main {
uword[10] answers_animals
sub start() {
uword[] array= [1111,2222,3333]
ubyte idx=1
txt.print_uw(array[idx])
txt.nl()
array[idx]+=1234
txt.print_uw(array[idx])
txt.nl()
ubyte[] array2= [11,22,33]
idx=1
txt.print_ub(array2[idx])
txt.nl()
array2[idx]+=34
txt.print_ub(array2[idx])
txt.nl()
float[] array3= [11.11,22.22,33.33]
idx=1
floats.print_f(array3[idx])
txt.nl()
array3[idx]+=55.66
floats.print_f(array3[idx])
txt.nl()
ubyte current_question = 1
uword previous_animals = 33
current_question = msb(answers_animals[current_question]) ; TODO takes 1 more vm registers than 8.10
answers_animals[current_question] = mkword(msb(previous_animals), 0) ; TODO takes 1 more vm registers than 8.10
; TODO expected result: 7 registers in 8.10, now takes 9 instead
}
}