move memoryslab administration from allocator to symboltable

This commit is contained in:
Irmen de Jong
2022-08-21 19:20:56 +02:00
parent f3960d21a8
commit fc0a0105b3
8 changed files with 48 additions and 40 deletions

View File

@@ -325,17 +325,10 @@ internal class BuiltinFuncGen(private val codeGen: CodeGen, private val exprGen:
val name = (call.args[0] as PtString).value
val size = (call.args[1] as PtNumber).number.toUInt()
val align = (call.args[2] as PtNumber).number.toUInt()
val existing = codeGen.allocations.getMemorySlab(name)
val address = if(existing==null)
codeGen.allocations.allocateMemorySlab(name, size, align)
else if(existing.second!=size || existing.third!=align) {
codeGen.errors.err("memory slab '$name' already exists with a different size or alignment", call.position)
return VmCodeChunk()
}
else
existing.first
val prefixedName = "prog8_memoryslab_$name"
val memorySlab = codeGen.allocations.getMemorySlab(prefixedName) ?: codeGen.allocations.allocateMemorySlab(prefixedName, size, align, call.position)
val code = VmCodeChunk()
code += VmCodeInstruction(Opcode.LOAD, VmDataType.WORD, reg1=resultRegister, value=address.toInt())
code += VmCodeInstruction(Opcode.LOAD, VmDataType.WORD, reg1=resultRegister, value=memorySlab.allocatedAddress!!.toInt())
return code
}

View File

@@ -1,5 +1,7 @@
package prog8.codegen.virtual
import prog8.code.StMemorySlab
import prog8.code.StNodeType
import prog8.code.SymbolTable
import prog8.code.ast.PtProgram
import prog8.code.core.*
@@ -103,20 +105,24 @@ class VariableAllocator(private val st: SymbolTable, private val program: PtProg
return mm
}
private val memorySlabsInternal = mutableMapOf<String, Triple<UInt, UInt, UInt>>()
internal val memorySlabs: Map<String, Triple<UInt, UInt, UInt>> = memorySlabsInternal
fun allocateMemorySlab(name: String, size: UInt, align: UInt): UInt {
fun allocateMemorySlab(name: String, size: UInt, align: UInt, position: Position): StMemorySlab {
val address =
if(align==0u || align==1u)
freeMemoryStart.toUInt()
else
(freeMemoryStart.toUInt() + align-1u) and (0xffffffffu xor (align-1u))
memorySlabsInternal[name] = Triple(address, size, align)
freeMemoryStart = (address + size).toInt()
return address
val slab = StMemorySlab(name, size, align, address, position)
st.add(slab)
return slab
}
fun getMemorySlab(name: String): Triple<UInt, UInt, UInt>? = memorySlabsInternal[name]
fun getMemorySlab(name: String): StMemorySlab? {
val existing = st.children[name]
return if(existing==null || existing.type!=StNodeType.MEMORYSLAB)
null
else
existing as StMemorySlab
}
}