preparing for statically allocating struct instances

This commit is contained in:
Irmen de Jong
2025-05-03 23:44:29 +02:00
parent 4dc82f2c83
commit 55e0dbab27
21 changed files with 166 additions and 61 deletions
@@ -45,10 +45,19 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe
"prog8_lib_stringcompare" -> funcStringCompare(call)
"prog8_lib_square_byte" -> funcSquare(call, IRDataType.BYTE)
"prog8_lib_square_word" -> funcSquare(call, IRDataType.WORD)
"structalloc" -> funcStructAlloc(call)
else -> throw AssemblyError("missing builtinfunc for ${call.name}")
}
}
private fun funcStructAlloc(call: PtBuiltinFunctionCall): ExpressionCodeResult {
val result = mutableListOf<IRCodeChunkBase>()
val pointerReg = codeGen.registers.next(IRDataType.WORD)
val address = 65534 // TODO determine correct address!!
addInstr(result, IRInstruction(Opcode.LOAD, IRDataType.WORD, reg1 = pointerReg, immediate = address), null)
return ExpressionCodeResult(result, IRDataType.WORD, pointerReg, -1)
}
private fun funcSquare(call: PtBuiltinFunctionCall, resultType: IRDataType): ExpressionCodeResult {
val result = mutableListOf<IRCodeChunkBase>()
val valueTr = exprGen.translateExpression(call.args[0])
@@ -14,6 +14,7 @@ fun convertStToIRSt(sourceSt: SymbolTable?): IRSymbolTable {
StNodeType.MEMVAR -> st.add(convert(it.value as StMemVar))
StNodeType.CONSTANT -> st.add(convert(it.value as StConstant))
StNodeType.MEMORYSLAB -> st.add(convert(it.value as StMemorySlab))
StNodeType.STRUCTINSTANCE -> st.add(convert(it.value as StStructInstance))
else -> { }
}
}
@@ -37,12 +38,19 @@ fun convertStToIRSt(sourceSt: SymbolTable?): IRSymbolTable {
}
private fun convert(variable: StStaticVariable): IRStStaticVariable {
private fun convert(instance: StStructInstance): IRStStructInstance {
val values = instance.initialValues.map { convertArrayElt(it) }
return IRStStructInstance(instance.name, values, instance.size.toInt())
}
fun convertArrayElt(elt: StArrayElement): IRStArrayElement = if(elt.boolean!=null)
IRStArrayElement(elt.boolean, null, elt.addressOfSymbol)
else
IRStArrayElement(null, elt.number, elt.addressOfSymbol)
private fun convertArrayElt(elt: StArrayElement): IRStArrayElement = if(elt.boolean!=null)
IRStArrayElement(elt.boolean, null, elt.addressOfSymbol)
else
IRStArrayElement(null, elt.number, elt.addressOfSymbol)
private fun convert(variable: StStaticVariable): IRStStaticVariable {
if('.' in variable.name) {
return IRStStaticVariable(variable.name,