Skip to content

Commit 04df3c9

Browse files
committed
vm: implemented in-place array multiplication better
1 parent de3d0b4 commit 04df3c9

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

codeGenIntermediate/src/prog8/codegen/intermediate/AssignmentGen.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,24 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
711711

712712
private fun operatorMultiplyInplace(symbol: String?, array: PtArrayIndexer?, constAddress: Int?, memory: PtMemoryByte?, vmDt: IRDataType, operand: PtExpression): IRCodeChunks? {
713713
if(array!=null) {
714-
TODO("* in array")
714+
val eltSize = codeGen.program.memsizer.memorySize(array.type)
715+
val result = mutableListOf<IRCodeChunkBase>()
716+
if(array.splitWords)
717+
return operatorMultiplyInplaceSplitArray(array, operand)
718+
val eltDt = irType(array.type)
719+
val constIndex = array.index.asConstInteger()
720+
val constValue = operand.asConstInteger()
721+
if(constIndex!=null && constValue!=null) {
722+
if(constValue!=1) {
723+
val valueReg=codeGen.registers.nextFree()
724+
result += IRCodeChunk(null, null).also {
725+
it += IRInstruction(Opcode.LOAD, eltDt, reg1=valueReg, immediate = constValue)
726+
it += IRInstruction(Opcode.MULM, eltDt, reg1=valueReg, labelSymbol = array.variable.name, symbolOffset = constIndex*eltSize)
727+
}
728+
}
729+
return result
730+
}
731+
return null // TODO("inplace array * non-const")
715732
}
716733
if(constAddress==null && memory!=null)
717734
return null // TODO("optimized memory in-place *"")
@@ -813,6 +830,10 @@ internal class AssignmentGen(private val codeGen: IRCodeGen, private val express
813830
return result
814831
}
815832

833+
private fun operatorMultiplyInplaceSplitArray(array: PtArrayIndexer, operand: PtExpression): IRCodeChunks? {
834+
return null // TODO("inplace split word array *")
835+
}
836+
816837
private fun operatorMinusInplaceSplitArray(array: PtArrayIndexer, operand: PtExpression): IRCodeChunks? {
817838
val result = mutableListOf<IRCodeChunkBase>()
818839
val constIndex = array.index.asConstInteger()

compiler/test/TestCompilerOnExamples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class TestCompilerOnExamplesVirtual: FunSpec({
208208
val (displayName, filepath) = prepareTestFiles(it, false, target)
209209
test(displayName) {
210210
val src = filepath.readText()
211-
compileText(target, true, src, writeAssembly = true) shouldNotBe null
211+
compileText(target, false, src, writeAssembly = true) shouldNotBe null
212212
compileText(target, true, src, writeAssembly = true) shouldNotBe null
213213
}
214214
}

examples/test.p8

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
%option no_sysinit
44

55
main {
6-
ubyte tw = other.width()
76
sub start() {
8-
tw++
9-
txt.print_uw(tw)
10-
}
11-
}
7+
ubyte[] ubarray = [11,22,33]
8+
uword[] uwarray = [1111,2222,3333]
9+
uword[] @split suwarray = [1111,2222,3333]
10+
11+
ubarray[1] *= 10
12+
uwarray[1] *= 10
13+
suwarray[1] *= 10
1214

13-
other {
14-
sub width() -> ubyte {
15-
cx16.r0++
16-
return 80
15+
txt.print_ub(ubarray[1])
16+
txt.nl()
17+
txt.print_uw(uwarray[1])
18+
txt.nl()
19+
txt.print_uw(suwarray[1])
20+
txt.nl()
1721
}
1822
}

0 commit comments

Comments
 (0)