From 02e51d82827524751b9d7461b5170df996d10fa0 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 7 Jul 2023 00:05:07 +0200 Subject: [PATCH] ir: fix initial chunk linking --- .../prog8/codegen/intermediate/BuiltinFuncGen.kt | 14 +++++++++++++- docs/source/todo.rst | 3 ++- intermediate/src/prog8/intermediate/IMSyscall.kt | 3 ++- intermediate/src/prog8/intermediate/IRProgram.kt | 5 +++-- virtualmachine/src/prog8/vm/SysCalls.kt | 2 +- virtualmachine/src/prog8/vm/VmProgramLoader.kt | 1 + 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt index fe53384b4..b2886b48c 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/BuiltinFuncGen.kt @@ -29,7 +29,7 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe "rsavex", "rrestore", "rrestorex" -> ExpressionCodeResult.EMPTY // vm doesn't have registers to save/restore - "callfar" -> throw AssemblyError("callfar() is for cx16 target only") + "callfar" -> funcCallfar(call) "msb" -> funcMsb(call) "lsb" -> funcLsb(call) "memory" -> funcMemory(call) @@ -53,6 +53,18 @@ internal class BuiltinFuncGen(private val codeGen: IRCodeGen, private val exprGe } } + private fun funcCallfar(call: PtBuiltinFunctionCall): ExpressionCodeResult { + val result = mutableListOf() + val bankTr = exprGen.translateExpression(call.args[0]) + val addressTr = exprGen.translateExpression(call.args[1]) + val argumentwordTr = exprGen.translateExpression(call.args[2]) + addToResult(result, bankTr, bankTr.resultReg, -1) + addToResult(result, addressTr, addressTr.resultReg, -1) + addToResult(result, argumentwordTr, argumentwordTr.resultReg, -1) + result += codeGen.makeSyscall(IMSyscall.CALLFAR, listOf(IRDataType.BYTE to bankTr.resultReg, IRDataType.WORD to addressTr.resultReg, IRDataType.WORD to argumentwordTr.resultReg), IRDataType.WORD to argumentwordTr.resultReg) + return ExpressionCodeResult(result, IRDataType.WORD, argumentwordTr.resultReg, -1) + } + private fun funcDivmod(call: PtBuiltinFunctionCall, type: IRDataType): ExpressionCodeResult { val result = mutableListOf() val number = call.args[0] diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 3bcc74be3..2822f5e09 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,7 +1,8 @@ TODO ==== -- Fix expericodegen errors (examples/cx16/keyboardhandler, rockrunners etc) +- Fix wrong unused subroutines in expericodegen with cx16 shell +- Fix expericodegen errors (chess, assem, musicdemo, rockrunners etc) ... diff --git a/intermediate/src/prog8/intermediate/IMSyscall.kt b/intermediate/src/prog8/intermediate/IMSyscall.kt index bc9473a83..b738f90c8 100644 --- a/intermediate/src/prog8/intermediate/IMSyscall.kt +++ b/intermediate/src/prog8/intermediate/IMSyscall.kt @@ -26,5 +26,6 @@ enum class IMSyscall(val number: Int) { CLAMP_BYTE(0x1012), CLAMP_UWORD(0x1013), CLAMP_WORD(0x1014), - CLAMP_FLOAT(0x1015) + CLAMP_FLOAT(0x1015), + CALLFAR(0x1016), } diff --git a/intermediate/src/prog8/intermediate/IRProgram.kt b/intermediate/src/prog8/intermediate/IRProgram.kt index 29f036fee..3f3ed1442 100644 --- a/intermediate/src/prog8/intermediate/IRProgram.kt +++ b/intermediate/src/prog8/intermediate/IRProgram.kt @@ -96,9 +96,10 @@ class IRProgram(val name: String, // link globalinits to subsequent chunk val firstBlock = blocks.firstOrNull() if(firstBlock!=null && firstBlock.isNotEmpty()) { - firstBlock.children.forEach { child -> + firstBlock.children.first().let { child -> when(child) { - is IRAsmSubroutine -> throw AssemblyError("cannot link next to asmsub $child") + is IRAsmSubroutine -> + throw AssemblyError("cannot link next to asmsub $child") is IRCodeChunk -> globalInits.next = child is IRInlineAsmChunk -> globalInits.next = child is IRInlineBinaryChunk -> globalInits.next = child diff --git a/virtualmachine/src/prog8/vm/SysCalls.kt b/virtualmachine/src/prog8/vm/SysCalls.kt index 11ba19285..51ceb2cef 100644 --- a/virtualmachine/src/prog8/vm/SysCalls.kt +++ b/virtualmachine/src/prog8/vm/SysCalls.kt @@ -52,7 +52,7 @@ SYSCALLS: 41 = CLAMP_WORD 42 = CLAMP_UWORD 43 = CLAMP_FLOAT -43 = ATAN +44 = ATAN */ enum class Syscall { diff --git a/virtualmachine/src/prog8/vm/VmProgramLoader.kt b/virtualmachine/src/prog8/vm/VmProgramLoader.kt index 448672fab..06072c8cb 100644 --- a/virtualmachine/src/prog8/vm/VmProgramLoader.kt +++ b/virtualmachine/src/prog8/vm/VmProgramLoader.kt @@ -124,6 +124,7 @@ class VmProgramLoader { IMSyscall.CLAMP_WORD.number -> Syscall.CLAMP_WORD IMSyscall.CLAMP_UWORD.number -> Syscall.CLAMP_UWORD IMSyscall.CLAMP_FLOAT.number -> Syscall.CLAMP_FLOAT + IMSyscall.CALLFAR.number -> throw IRParseException("vm doesn't support the callfar() syscall") else -> null }