From 62485b6851aea6d752b3caa02dc8aedeb8fe9596 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 27 Nov 2021 15:41:44 +0100 Subject: [PATCH] allow assigns to asmsub parameters (registers), but this is not very useful in practice. --- .../codegen/assignment/AsmAssignment.kt | 42 +++++++++++-------- examples/test.p8 | 12 +++--- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AsmAssignment.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AsmAssignment.kt index 9fa86f229..f29d601c3 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AsmAssignment.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/assignment/AsmAssignment.kt @@ -56,22 +56,31 @@ internal class AsmAssignTarget(val kind: TargetStorageKind, } companion object { - fun fromAstAssignment(assign: Assignment, program: Program, asmgen: AsmGen): AsmAssignTarget = with(assign.target) { - val idt = inferType(program) - if(!idt.isKnown) - throw AssemblyError("unknown dt") - val dt = idt.getOr(DataType.UNDEFINED) - when { - identifier != null -> { - val parameter = identifier!!.targetVarDecl(program)?.subroutineParameter - if(parameter!=null && parameter.definingSubroutine!!.isAsmSubroutine) { - TODO("ASSIGNTARGET ASMPARAM $parameter :: $assign") + fun fromAstAssignment(assign: Assignment, program: Program, asmgen: AsmGen): AsmAssignTarget { + with(assign.target) { + val idt = inferType(program) + if(!idt.isKnown) + throw AssemblyError("unknown dt") + val dt = idt.getOr(DataType.UNDEFINED) + when { + identifier != null -> { + val parameter = identifier!!.targetVarDecl(program)?.subroutineParameter + if (parameter!=null) { + val sub = parameter.definingSubroutine!! + if (sub.isAsmSubroutine) { + val reg = sub.asmParameterRegisters[sub.parameters.indexOf(parameter)] + if(reg.statusflag!=null) + throw AssemblyError("can't assign value to processor statusflag directly") + else + return AsmAssignTarget(TargetStorageKind.REGISTER, program, asmgen, dt, assign.definingSubroutine, register=reg.registerOrPair, origAstTarget = this) + } + } + return AsmAssignTarget(TargetStorageKind.VARIABLE, program, asmgen, dt, assign.definingSubroutine, variableAsmName = asmgen.asmVariableName(identifier!!), origAstTarget = this) } - AsmAssignTarget(TargetStorageKind.VARIABLE, program, asmgen, dt, assign.definingSubroutine, variableAsmName = asmgen.asmVariableName(identifier!!), origAstTarget = this) + arrayindexed != null -> return AsmAssignTarget(TargetStorageKind.ARRAY, program, asmgen, dt, assign.definingSubroutine, array = arrayindexed, origAstTarget = this) + memoryAddress != null -> return AsmAssignTarget(TargetStorageKind.MEMORY, program, asmgen, dt, assign.definingSubroutine, memory = memoryAddress, origAstTarget = this) + else -> throw AssemblyError("weird target") } - arrayindexed != null -> AsmAssignTarget(TargetStorageKind.ARRAY, program, asmgen, dt, assign.definingSubroutine, array = arrayindexed, origAstTarget = this) - memoryAddress != null -> AsmAssignTarget(TargetStorageKind.MEMORY, program, asmgen, dt, assign.definingSubroutine, memory = memoryAddress, origAstTarget = this) - else -> throw AssemblyError("weird target") } } @@ -140,9 +149,8 @@ internal class AsmAssignSource(val kind: SourceStorageKind, is ArrayLiteralValue -> throw AssemblyError("array literal value should not occur anymore for asm generation") is IdentifierReference -> { val parameter = value.targetVarDecl(program)?.subroutineParameter - if(parameter!=null && parameter.definingSubroutine!!.isAsmSubroutine) { - TODO("ASSIGNSOURCE ASMPARAM $parameter :: $value") - } + if(parameter!=null && parameter.definingSubroutine!!.isAsmSubroutine) + throw AssemblyError("can't assign from a asmsub register parameter") val dt = value.inferType(program).getOr(DataType.UNDEFINED) val varName=asmgen.asmVariableName(value) // special case: "cx16.r[0-15]" are 16-bits virtual registers of the commander X16 system diff --git a/examples/test.p8 b/examples/test.p8 index 2ca2a21b5..37bcc5070 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -2,21 +2,19 @@ main { sub start() { + uword ww - &ubyte derp = $c000 + main.routine2.num = ww+1 + main.routine2.switch=true - @($c000) = not @($c000) - @($c000) = ~ @($c000) + routine2(ww+1, true) - ubyte uu - uu = abs(uu) - routine2(12345, true) repeat { } } - asmsub routine2(uword num @AY, ubyte switch @Pc) { + asmsub routine2(uword num @AY, ubyte switch @X) { %asm {{ adc #20 rts