From f7953e4ef3814f7e21ff3fd599767b9e255389ee Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 1 Dec 2020 22:19:03 +0100 Subject: [PATCH] fix float comparison error that creeped in with no longer using the stack for that --- .../src/prog8/ast/statements/AstStatements.kt | 3 +- .../compiler/target/IAssemblyGenerator.kt | 3 +- .../compiler/target/c64/codegen/AsmGen.kt | 6 ++- .../c64/codegen/BuiltinFunctionsAsmGen.kt | 6 +-- .../target/c64/codegen/ExpressionsAsmGen.kt | 50 +++++++++---------- docs/source/todo.rst | 2 - examples/arithmetic/div.p8 | 4 +- examples/arithmetic/minus.p8 | 8 +-- examples/arithmetic/mult.p8 | 6 +-- examples/arithmetic/plus.p8 | 1 - 10 files changed, 45 insertions(+), 44 deletions(-) diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index 446f53cac..97b52423b 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -695,7 +695,8 @@ class AsmGenInfo { var usedRegsaveA = false var usedRegsaveX = false var usedRegsaveY = false - var usedFloatEvalResultVar = false + var usedFloatEvalResultVar1 = false + var usedFloatEvalResultVar2 = false class ArrayIndexerInfo(val name: String, val replaces: ArrayIndex, val partOfStatement: Statement, var used: Int=0) } diff --git a/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt b/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt index 752dcb65c..3f734efa6 100644 --- a/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt +++ b/compiler/src/prog8/compiler/target/IAssemblyGenerator.kt @@ -7,7 +7,8 @@ internal interface IAssemblyGenerator { } internal const val generatedLabelPrefix = "_prog8_label_" -internal const val subroutineFloatEvalResultVar = "_prog8_float_eval_result" +internal const val subroutineFloatEvalResultVar1 = "_prog8_float_eval_result1" +internal const val subroutineFloatEvalResultVar2 = "_prog8_float_eval_result2" internal interface IAssemblyProgram { val name: String diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 7436178ac..a1af4e217 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -800,8 +800,10 @@ internal class AsmGen(private val program: Program, out("_prog8_regsaveX .byte 0") if(sub.asmGenInfo.usedRegsaveY) out("_prog8_regsaveY .byte 0") - if(sub.asmGenInfo.usedFloatEvalResultVar) - out("$subroutineFloatEvalResultVar .byte 0,0,0,0,0") + if(sub.asmGenInfo.usedFloatEvalResultVar1) + out("$subroutineFloatEvalResultVar1 .byte 0,0,0,0,0") + if(sub.asmGenInfo.usedFloatEvalResultVar2) + out("$subroutineFloatEvalResultVar2 .byte 0,0,0,0,0") vardecls2asm(sub.statements) out(" .pend\n") } diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 267ca0556..d5a9c20b0 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -17,7 +17,7 @@ import prog8.compiler.target.c64.codegen.assignment.AsmAssignTarget import prog8.compiler.target.c64.codegen.assignment.AsmAssignment import prog8.compiler.target.c64.codegen.assignment.SourceStorageKind import prog8.compiler.target.c64.codegen.assignment.TargetStorageKind -import prog8.compiler.target.subroutineFloatEvalResultVar +import prog8.compiler.target.subroutineFloatEvalResultVar2 import prog8.compiler.toHex import prog8.functions.FSignature @@ -1033,8 +1033,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val throw AssemblyError("float literals should have been converted into autovar") } else -> { - scope.asmGenInfo.usedFloatEvalResultVar = true - val variable = IdentifierReference(listOf(subroutineFloatEvalResultVar), value.position) + scope.asmGenInfo.usedFloatEvalResultVar2 = true + val variable = IdentifierReference(listOf(subroutineFloatEvalResultVar2), value.position) val addr = AddressOf(variable, value.position) addr.linkParents(value) asmgen.assignExpressionToVariable(value, asmgen.asmVariableName(variable), DataType.FLOAT, scope) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt index 9f528605c..d742a37f3 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt @@ -9,7 +9,7 @@ import prog8.ast.statements.Subroutine import prog8.compiler.AssemblyError import prog8.compiler.target.CompilationTarget import prog8.compiler.target.CpuType -import prog8.compiler.target.subroutineFloatEvalResultVar +import prog8.compiler.target.subroutineFloatEvalResultVar1 import prog8.compiler.toHex import prog8.functions.BuiltinFunctions import kotlin.math.absoluteValue @@ -217,12 +217,12 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine()!! - subroutine.asmGenInfo.usedFloatEvalResultVar = true - asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar, DataType.FLOAT, subroutine) + subroutine.asmGenInfo.usedFloatEvalResultVar1 = true + asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(""" - lda #<$subroutineFloatEvalResultVar - ldy #>$subroutineFloatEvalResultVar + lda #<$subroutineFloatEvalResultVar1 + ldy #>$subroutineFloatEvalResultVar1 jsr floats.var_fac1_less_f beq $jumpIfFalseLabel""") } @@ -262,12 +262,12 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine()!! - subroutine.asmGenInfo.usedFloatEvalResultVar = true - asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar, DataType.FLOAT, subroutine) + subroutine.asmGenInfo.usedFloatEvalResultVar1 = true + asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(""" - lda #<$subroutineFloatEvalResultVar - ldy #>$subroutineFloatEvalResultVar + lda #<$subroutineFloatEvalResultVar1 + ldy #>$subroutineFloatEvalResultVar1 jsr floats.var_fac1_lesseq_f beq $jumpIfFalseLabel""") } @@ -307,12 +307,12 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine()!! - subroutine.asmGenInfo.usedFloatEvalResultVar = true - asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar, DataType.FLOAT, subroutine) + subroutine.asmGenInfo.usedFloatEvalResultVar1 = true + asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(""" - lda #<$subroutineFloatEvalResultVar - ldy #>$subroutineFloatEvalResultVar + lda #<$subroutineFloatEvalResultVar1 + ldy #>$subroutineFloatEvalResultVar1 jsr floats.var_fac1_greater_f beq $jumpIfFalseLabel""") } @@ -352,12 +352,12 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine()!! - subroutine.asmGenInfo.usedFloatEvalResultVar = true - asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar, DataType.FLOAT, subroutine) + subroutine.asmGenInfo.usedFloatEvalResultVar1 = true + asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(""" - lda #<$subroutineFloatEvalResultVar - ldy #>$subroutineFloatEvalResultVar + lda #<$subroutineFloatEvalResultVar1 + ldy #>$subroutineFloatEvalResultVar1 jsr floats.var_fac1_greatereq_f beq $jumpIfFalseLabel""") } @@ -1133,12 +1133,12 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine()!! - subroutine.asmGenInfo.usedFloatEvalResultVar = true - asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar, DataType.FLOAT, subroutine) + subroutine.asmGenInfo.usedFloatEvalResultVar1 = true + asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(""" - lda #<$subroutineFloatEvalResultVar - ldy #>$subroutineFloatEvalResultVar + lda #<$subroutineFloatEvalResultVar1 + ldy #>$subroutineFloatEvalResultVar1 jsr floats.var_fac1_notequal_f bne $jumpIfFalseLabel""") } @@ -1218,12 +1218,12 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge bne $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine()!! - subroutine.asmGenInfo.usedFloatEvalResultVar = true - asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar, DataType.FLOAT, subroutine) + subroutine.asmGenInfo.usedFloatEvalResultVar1 = true + asmgen.assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) asmgen.assignExpressionToRegister(left, RegisterOrPair.FAC1) asmgen.out(""" - lda #<$subroutineFloatEvalResultVar - ldy #>$subroutineFloatEvalResultVar + lda #<$subroutineFloatEvalResultVar1 + ldy #>$subroutineFloatEvalResultVar1 jsr floats.var_fac1_notequal_f beq $jumpIfFalseLabel""") } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index cd192b509..2bc492e6f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,8 +2,6 @@ TODO ==== -- fix errors in arith examples - - see if we can group some errors together for instance the (now single) errors about unidentified symbols - Cx16 target: support full-screen 640x480 and 320x240 graphics? That requires our own custom graphics routines though to draw lines. - make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_' diff --git a/examples/arithmetic/div.p8 b/examples/arithmetic/div.p8 index e8fafacf1..28573758f 100644 --- a/examples/arithmetic/div.p8 +++ b/examples/arithmetic/div.p8 @@ -21,8 +21,8 @@ main { div_word(-20000,500,-40) div_word(-2222,2,-1111) - div_float(0,1,0) ; TODO FIX ERROR - div_float(999.9,111.0,9.008108108108107) ; TODO FIX ERROR + div_float(0,1,0) + div_float(999.9,111.0,9.008108108108107) } sub div_ubyte(ubyte a1, ubyte a2, ubyte c) { diff --git a/examples/arithmetic/minus.p8 b/examples/arithmetic/minus.p8 index bd3d9ff68..8ca1d7f8a 100644 --- a/examples/arithmetic/minus.p8 +++ b/examples/arithmetic/minus.p8 @@ -29,9 +29,9 @@ main { minus_word(0,-3333,3333) minus_word(-3333,0,-3333) - minus_float(0,0,0) ; TODO FIX ERROR - minus_float(2.5,1.5,1.0) ; TODO FIX ERROR - minus_float(-1.5,3.5,-5.0) ; TODO FIX ERROR + minus_float(0,0,0) + minus_float(2.5,1.5,1.0) + minus_float(-1.5,3.5,-5.0) test_stack.test() } @@ -98,7 +98,7 @@ main { sub minus_float(float a1, float a2, float c) { float r = a1-a2 - if abs(r-c)<0.00001 ; TODO FIX COMPARISON (it works when only comparing a var) + if abs(r-c)<0.00001 txt.print(" ok ") else { txt.print("err! ") diff --git a/examples/arithmetic/mult.p8 b/examples/arithmetic/mult.p8 index ab417f027..d12c07e1a 100644 --- a/examples/arithmetic/mult.p8 +++ b/examples/arithmetic/mult.p8 @@ -22,9 +22,9 @@ main { mul_word(-10,1000,-10000) mul_word(1,-3333,-3333) - mul_float(0,0,0) ; TODO FIX ERROR - mul_float(2.5,10,25) ; TODO FIX ERROR - mul_float(-1.5,10,-15) ; TODO FIX ERROR + mul_float(0,0,0) + mul_float(2.5,10,25) + mul_float(-1.5,10,-15) } sub mul_ubyte(ubyte a1, ubyte a2, ubyte c) { diff --git a/examples/arithmetic/plus.p8 b/examples/arithmetic/plus.p8 index ec452602a..d7a6788de 100644 --- a/examples/arithmetic/plus.p8 +++ b/examples/arithmetic/plus.p8 @@ -25,7 +25,6 @@ main { plus_word(0,-3333,-3333) plus_word(-3333,0,-3333) - ; TODO FIX ERRORs in float plus_float(0,0,0) plus_float(1.5,2.5,4.0) plus_float(-1.5,3.5,2.0)