From 45c9cc97d9b0280a82bc6400de56c72e3068b194 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 5 Jan 2021 02:44:55 +0100 Subject: [PATCH] fix invalid handling of X register functioncall result value --- .../target/c64/codegen/assignment/AssignmentAsmGen.kt | 7 ++++--- examples/test.p8 | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt index 0914c562c..ab8bff5c3 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt @@ -144,12 +144,11 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen is Subroutine -> { asmgen.saveXbeforeCall(value) asmgen.translateFunctionCall(value) - if(!sub.regXasResult()) - asmgen.restoreXafterCall(value) val returnValue = sub.returntypes.zip(sub.asmReturnvaluesRegisters).singleOrNull { it.second.registerOrPair!=null } ?: sub.returntypes.zip(sub.asmReturnvaluesRegisters).single { it.second.statusflag!=null } when (returnValue.first) { DataType.STR -> { + asmgen.restoreXafterCall(value) when(assign.target.datatype) { DataType.UWORD -> { // assign the address of the string result value @@ -171,9 +170,11 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen } DataType.FLOAT -> { // float result from function sits in FAC1 + asmgen.restoreXafterCall(value) assignFAC1float(assign.target) } else -> { + // do NOT restore X register before assigning the result values first when (returnValue.second.registerOrPair) { RegisterOrPair.A -> assignRegisterByte(assign.target, CpuRegister.A) RegisterOrPair.X -> assignRegisterByte(assign.target, CpuRegister.X) @@ -189,7 +190,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen throw AssemblyError("should be just one register byte result value") } } - // we've processed the result value in the X register by now, so it's now safe to restore it: + // we've processed the result value in the X register by now, so it's now finally safe to restore it asmgen.restoreXafterCall(value) } } diff --git a/examples/test.p8 b/examples/test.p8 index 590dcd7b3..09bc87a70 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,7 +5,14 @@ main { + asmsub opcode(uword instr_info_ptr @AY, ubyte addr_mode @X) clobbers(X) -> ubyte @A, ubyte @Pc { + %asm {{ + rts + }} + } + sub start () { + ubyte xx = opcode(0, 1) test_stack.test()