From e05ea887f601401016b3c1353dc7e74fe074b7b4 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 1 Nov 2020 06:27:17 +0100 Subject: [PATCH] implement proper returning of float values via FAC1 --- .../compiler/target/c64/codegen/AsmGen.kt | 13 ++++++++- .../codegen/assignment/AssignmentAsmGen.kt | 5 +++- examples/test.p8 | 28 +++++-------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 162ea47fe..941ce9bb8 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -1171,7 +1171,18 @@ $counterVar .byte 0""") assignmentAsmGen.translateNormalAssignment(AsmAssignment(src, returnValueTarget, false, ret.position)) } DataType.FLOAT -> { - TODO("must return the float's address in AY") + // return the float value via FAC1 + when (returnvalue) { + is NumericLiteralValue -> throw AssemblyError("float literal should have been changed to auto var") + is IdentifierReference -> { + val asmVar = asmVariableName(returnvalue) + out(" lda #<${asmVar} | ldy #>${asmVar} | jsr floats.MOVFM") + } + else -> { + translateExpression(returnvalue) + out(" jsr floats.pop_float_fac1") + } + } } else -> { // all else take its address and assign that also to AY register pair 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 4c5843b00..5a172a823 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/assignment/AssignmentAsmGen.kt @@ -150,6 +150,9 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen } else -> throw AssemblyError("weird target dt") } + } else if(returnValue.first==DataType.FLOAT) { + // float result from function sits in FAC1 + assignFAC1float(assign.target) } else { when (returnValue.second.registerOrPair) { RegisterOrPair.A -> assignRegisterByte(assign.target, CpuRegister.A) @@ -173,7 +176,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen when(returntype.typeOrElse(DataType.STRUCT)) { in ByteDatatypes -> assignRegisterByte(assign.target, CpuRegister.A) // function's byte result is in A in WordDatatypes -> assignRegisterpairWord(assign.target, RegisterOrPair.AY) // function's word result is in AY - DataType.STR -> TODO("assign string => copy string or assign string address") + DataType.STR -> throw AssemblyError("missing code for assign string from builtin func => copy string or assign string address") DataType.FLOAT -> { // float result from function sits in FAC1 assignFAC1float(assign.target) diff --git a/examples/test.p8 b/examples/test.p8 index cec97a0e6..7acce0678 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,4 +1,5 @@ %import textio +%import floats %import syslib %zeropage basicsafe @@ -8,27 +9,10 @@ main { sub start() { - uword num - ubyte ss + float fl - num = 65535 - ss = sqrt16(num) - txt.print_ub(ss) - txt.chrout('\n') - - num = 20000 - ss = sqrt16(num) - txt.print_ub(ss) - txt.chrout('\n') - - num = 9999 - ss = sqrt16(num) - txt.print_ub(ss) - txt.chrout('\n') - - num = 500 - ss = sqrt16(num) - txt.print_ub(ss) + fl = getfloat() + floats.print_f(fl) txt.chrout('\n') testX() @@ -44,7 +28,9 @@ main { } sub getfloat() -> float { - return 123.456789 + float xx + xx = 123.456789 + return xx } sub mcp(uword from, uword dest, ubyte length) {