Skip to content

Commit

Permalink
vm: implemented in-place array multiplication better
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Mar 17, 2024
1 parent de3d0b4 commit 04df3c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,24 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express

private fun operatorMultiplyInplace(symbol: String?, array: PtArrayIndexer?, constAddress: Int?, memory: PtMemoryByte?, vmDt: IRDataType, operand: PtExpression): IRCodeChunks? {
if(array!=null) {
TODO("* in array")
val eltSize = codeGen.program.memsizer.memorySize(array.type)
val result = mutableListOf<IRCodeChunkBase>()
if(array.splitWords)
return operatorMultiplyInplaceSplitArray(array, operand)
val eltDt = irType(array.type)
val constIndex = array.index.asConstInteger()
val constValue = operand.asConstInteger()
if(constIndex!=null && constValue!=null) {
if(constValue!=1) {
val valueReg=codeGen.registers.nextFree()
result += IRCodeChunk(null, null).also {
it += IRInstruction(Opcode.LOAD, eltDt, reg1=valueReg, immediate = constValue)
it += IRInstruction(Opcode.MULM, eltDt, reg1=valueReg, labelSymbol = array.variable.name, symbolOffset = constIndex*eltSize)
}
}
return result
}
return null // TODO("inplace array * non-const")
}
if(constAddress==null && memory!=null)
return null // TODO("optimized memory in-place *"")
Expand Down Expand Up @@ -813,6 +830,10 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
return result
}

private fun operatorMultiplyInplaceSplitArray(array: PtArrayIndexer, operand: PtExpression): IRCodeChunks? {
return null // TODO("inplace split word array *")
}

private fun operatorMinusInplaceSplitArray(array: PtArrayIndexer, operand: PtExpression): IRCodeChunks? {
val result = mutableListOf<IRCodeChunkBase>()
val constIndex = array.index.asConstInteger()
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/TestCompilerOnExamples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class TestCompilerOnExamplesVirtual: FunSpec({
val (displayName, filepath) = prepareTestFiles(it, false, target)
test(displayName) {
val src = filepath.readText()
compileText(target, true, src, writeAssembly = true) shouldNotBe null
compileText(target, false, src, writeAssembly = true) shouldNotBe null
compileText(target, true, src, writeAssembly = true) shouldNotBe null
}
}
Expand Down
22 changes: 13 additions & 9 deletions examples/test.p8
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
%option no_sysinit

main {
ubyte tw = other.width()
sub start() {
tw++
txt.print_uw(tw)
}
}
ubyte[] ubarray = [11,22,33]
uword[] uwarray = [1111,2222,3333]
uword[] @split suwarray = [1111,2222,3333]

ubarray[1] *= 10
uwarray[1] *= 10
suwarray[1] *= 10

other {
sub width() -> ubyte {
cx16.r0++
return 80
txt.print_ub(ubarray[1])
txt.nl()
txt.print_uw(uwarray[1])
txt.nl()
txt.print_uw(suwarray[1])
txt.nl()
}
}

0 comments on commit 04df3c9

Please sign in to comment.