added memoryslabs to symboltable

This commit is contained in:
Irmen de Jong 2022-08-21 19:01:53 +02:00
parent 475efbe007
commit 80b630a1e4
5 changed files with 16 additions and 22 deletions

View File

@ -323,11 +323,11 @@ internal class BuiltinFuncGen(private val codeGen: CodeGen, private val exprGen:
private fun funcMemory(call: PtBuiltinFunctionCall, resultRegister: Int): VmCodeChunk {
val name = (call.args[0] as PtString).value
val size = (call.args[1] as PtNumber).number.toInt()
val align = (call.args[2] as PtNumber).number.toInt()
val slabName = codeGen.allocations.addMemorySlab(name, size, align)
val size = (call.args[1] as PtNumber).number.toUInt()
val align = (call.args[2] as PtNumber).number.toUInt()
val label = codeGen.addMemorySlab(name, size, align, call.position)
val code = VmCodeChunk()
code += VmCodeInstruction(Opcode.LOAD, VmDataType.WORD, reg1=resultRegister, labelSymbol = slabName)
code += VmCodeInstruction(Opcode.LOAD, VmDataType.WORD, reg1=resultRegister, labelSymbol = label)
return code
}

View File

@ -1,5 +1,6 @@
package prog8.codegen.experimental
import prog8.code.StMemorySlab
import prog8.code.StStaticVariable
import prog8.code.SymbolTable
import prog8.code.ast.*
@ -47,7 +48,7 @@ class CodeGen(internal val program: PtProgram,
internal val vmRegisters = VmRegisterPool()
override fun compileToAssembly(): IAssemblyProgram? {
val irProg = IRProgram(program.name, options, program.encoding, symbolTable, allocations.memorySlabs)
val irProg = IRProgram(program.name, options, program.encoding, symbolTable)
if(!options.dontReinitGlobals) {
// collect global variables initializers
@ -72,7 +73,7 @@ class CodeGen(internal val program: PtProgram,
optimizer.optimize()
}
println("IR codegen: virtual registers=${vmRegisters.peekNext()} memory usage=${allocations.freeMem}")
println("IR codegen: virtual registers=${vmRegisters.peekNext()}")
irProg.writeFile()
return DummyAssemblyProgram(irProg.name)
@ -811,5 +812,11 @@ class CodeGen(internal val program: PtProgram,
internal fun isZero(expression: PtExpression): Boolean = expression is PtNumber && expression.number==0.0
internal fun isOne(expression: PtExpression): Boolean = expression is PtNumber && expression.number==1.0
fun addMemorySlab(name: String, size: UInt, align: UInt, position: Position): List<String> {
val scopedName = "prog8_memoryslabs.$name"
symbolTable.add(StMemorySlab(scopedName, size, align, position))
return scopedName.split('.', limit=2)
}
}

View File

@ -17,8 +17,7 @@ import kotlin.io.path.div
class IRProgram(val name: String,
private val options: CompilationOptions,
private val encoding: IStringEncoding,
private val st: SymbolTable,
private val memorySlabs: List<MemorySlab>) {
private val st: SymbolTable) {
private val globalInits = mutableListOf<VmCodeLine>()
private val blocks = mutableListOf<VmCodeChunk>()
@ -93,11 +92,7 @@ class IRProgram(val name: String,
}
out.write("; MEMORY SLABS\n")
memorySlabs.forEach{ slab ->
out.write("MEMORYSLAB _${slab.label.joinToString(".")} ${slab.size} ${slab.align}\n")
}
st.allMemorySlabs.forEach{ slab -> out.write("MEMORYSLAB _${slab.name} ${slab.size} ${slab.align}\n") }
}
private fun BufferedWriter.writeLine(line: VmCodeLine) {
@ -130,8 +125,6 @@ class IRProgram(val name: String,
sealed class VmCodeLine
class MemorySlab(val name: String, val size: Int, val align: Int, val label: List<String>)
class VmCodeInstruction(
opcode: Opcode,
type: VmDataType?=null,

View File

@ -5,7 +5,6 @@ import prog8.code.core.*
class VariableAllocator(st: SymbolTable, memsizer: IMemSizer) {
internal val memorySlabs = mutableListOf<MemorySlab>() // TODO move this to the SymbolTable instead
internal val allocations = mutableMapOf<List<String>, Int>()
private var freeMemoryStart: Int
@ -44,10 +43,4 @@ class VariableAllocator(st: SymbolTable, memsizer: IMemSizer) {
}
fun get(name: List<String>) = allocations.getValue(name)
fun addMemorySlab(name: String, size: Int, align: Int): List<String>? {
val label = listOf("prog8_memoryslabs", name)
memorySlabs.add(MemorySlab(name, size, align, label))
return label
}
}

View File

@ -1,6 +1,7 @@
main {
sub start() {
; TODO should generate address
uword @shared slab1 = memory("slab 1", 2000, 0)
uword @shared slab2 = memory("slab 1", 2000, 0)
uword @shared slab3 = memory("other # slab", 2000, 64)