mirror of
https://github.com/irmen/prog8.git
synced 2026-03-14 11:16:35 +00:00
move memoryslab administration from allocator to symboltable
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user