implemented remaining float operations

This commit is contained in:
Irmen de Jong 2020-08-27 19:47:50 +02:00
parent 6d17e5307c
commit a77d3c92ad
4 changed files with 173 additions and 83 deletions

View File

@ -235,46 +235,61 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
println("warning: slow stack evaluation used (2): $name $operator= ${value::class.simpleName} at ${value.position}") // TODO println("warning: slow stack evaluation used (2): $name $operator= ${value::class.simpleName} at ${value.position}") // TODO
asmgen.translateExpression(value) asmgen.translateExpression(value)
when (operator) { when (operator) {
"**" -> TODO("pow") "**" -> {
asmgen.out("""
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.CONUPK
jsr c64flt.FPWRT
""")
}
"+" -> { "+" -> {
asmgen.out(""" asmgen.out("""
jsr c64flt.pop_float_fac1 jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X stx P8ZP_SCRATCH_REG_X
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.FADD jsr c64flt.FADD
ldx #<$name """)
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
} }
"-" -> { "-" -> {
asmgen.out(""" asmgen.out("""
jsr c64flt.pop_float_fac1 jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X stx P8ZP_SCRATCH_REG_X
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.FSUB jsr c64flt.FSUB
ldx #<$name """)
ldy #>$name }
jsr c64flt.MOVMF "*" -> {
ldx P8ZP_SCRATCH_REG_X asmgen.out("""
""") jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FMULT
""")
} }
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> { "/" -> {
TODO("div") asmgen.out("""
// asmgen.out(if(types==DataType.UBYTE) " jsr prog8_lib.idiv_ub" else " jsr prog8_lib.idiv_b") jsr c64flt.pop_float_fac1
} stx P8ZP_SCRATCH_REG_X
"%" -> { lda #<$name
TODO("float remainder???") ldy #>$name
// if(types==DataType.BYTE) jsr c64flt.FDIV
// throw AssemblyError("remainder of signed integers is not properly defined/implemented, use unsigned instead") """)
// asmgen.out(" jsr prog8_lib.remainder_ub")
} }
else -> throw AssemblyError("invalid operator for in-place float modification $operator") else -> throw AssemblyError("invalid operator for in-place float modification $operator")
} }
// store Fac1 back into memory
asmgen.out("""
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
} }
private fun inplaceModification_float_variable_to_variable(name: String, operator: String, ident: IdentifierReference) { private fun inplaceModification_float_variable_to_variable(name: String, operator: String, ident: IdentifierReference) {
@ -284,81 +299,145 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
val otherName = asmgen.asmVariableName(ident) val otherName = asmgen.asmVariableName(ident)
when (operator) { when (operator) {
"**" -> TODO("pow") "**" -> {
"+" -> TODO("+") asmgen.out("""
"-" -> TODO("-") stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.CONUPK
lda #<$otherName
ldy #>$otherName
jsr c64flt.FPWR
""")
}
"+" -> {
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$otherName
ldy #>$otherName
jsr c64flt.FADD
""")
}
"-" -> {
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$otherName
ldy #>$otherName
jsr c64flt.MOVFM
lda #<$name
ldy #>$name
jsr c64flt.FSUB
""")
}
"*" -> { "*" -> {
asmgen.out(""" asmgen.out("""
stx P8ZP_SCRATCH_REG_X stx P8ZP_SCRATCH_REG_X
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.MOVFM jsr c64flt.MOVFM
lda #<$otherName lda #<$otherName
ldy #>$otherName ldy #>$otherName
jsr c64flt.FMULT jsr c64flt.FMULT
ldx #<$name """)
ldy #>$name }
jsr c64flt.MOVMF "/" -> {
ldx P8ZP_SCRATCH_REG_X asmgen.out("""
""") stx P8ZP_SCRATCH_REG_X
lda #<$otherName
ldy #>$otherName
jsr c64flt.MOVFM
lda #<$name
ldy #>$name
jsr c64flt.FDIV
""")
} }
"/" -> TODO("div")
"%" -> TODO("float remainder???")
else -> throw AssemblyError("invalid operator for in-place float modification $operator") else -> throw AssemblyError("invalid operator for in-place float modification $operator")
} }
// store Fac1 back into memory
asmgen.out("""
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
} }
private fun inplaceModification_float_litval_to_variable(name: String, operator: String, value: Double) { private fun inplaceModification_float_litval_to_variable(name: String, operator: String, value: Double) {
val constValueName = asmgen.getFloatAsmConst(value) val constValueName = asmgen.getFloatAsmConst(value)
when (operator) { when (operator) {
"**" -> TODO("pow") "**" -> {
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.CONUPK
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.FPWR
""")
}
"+" -> { "+" -> {
if (value == 0.0) if (value == 0.0)
return return
asmgen.out(""" asmgen.out("""
stx P8ZP_SCRATCH_REG_X stx P8ZP_SCRATCH_REG_X
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.MOVFM jsr c64flt.MOVFM
lda #<$constValueName lda #<$constValueName
ldy #>$constValueName ldy #>$constValueName
jsr c64flt.FADD jsr c64flt.FADD
ldx #<$name """)
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
} }
"-" -> { "-" -> {
if (value == 0.0) if (value == 0.0)
return return
asmgen.out(""" asmgen.out("""
stx P8ZP_SCRATCH_REG_X stx P8ZP_SCRATCH_REG_X
lda #<$constValueName lda #<$constValueName
ldy #>$constValueName ldy #>$constValueName
jsr c64flt.MOVFM jsr c64flt.MOVFM
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.FSUB jsr c64flt.FSUB
ldx #<$name """)
ldy #>$name }
jsr c64flt.MOVMF "*" -> {
ldx P8ZP_SCRATCH_REG_X asmgen.out("""
""") stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.FMULT
""")
} }
"*" -> TODO("mul")
"/" -> { "/" -> {
if (value == 0.0) if (value == 0.0)
throw AssemblyError("division by zero") throw AssemblyError("division by zero")
TODO("div") asmgen.out("""
} stx P8ZP_SCRATCH_REG_X
"%" -> { lda #<$constValueName
if (value == 0.0) ldy #>$constValueName
throw AssemblyError("division by zero") jsr c64flt.MOVFM
TODO("float remainder???") lda #<$name
ldy #>$name
jsr c64flt.FDIV
""")
} }
else -> throw AssemblyError("invalid operator for in-place float modification $operator") else -> throw AssemblyError("invalid operator for in-place float modification $operator")
} }
// store Fac1 back into memory
asmgen.out("""
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
} }
private fun inplaceModification_byte_value_to_memory(pointervar: IdentifierReference, operator: String, value: Expression) { private fun inplaceModification_byte_value_to_memory(pointervar: IdentifierReference, operator: String, value: Expression) {

View File

@ -2,8 +2,6 @@
%import c64textio %import c64textio
%zeropage basicsafe %zeropage basicsafe
; TODO implement float MINUS asm generation
main { main {
sub start() { sub start() {

View File

@ -2,8 +2,6 @@
%import c64textio %import c64textio
%zeropage basicsafe %zeropage basicsafe
; TODO implement float PLUS asm generation
main { main {
sub start() { sub start() {

15
examples/test.p8 Normal file
View File

@ -0,0 +1,15 @@
%import c64flt
%zeropage basicsafe
main {
sub start() {
float f1 = 2.2
float f2 = 1.0
float f4 = 4.0
float f5 = 5.0
f1 /= f2+f4
c64flt.print_f(f1)
c64.CHROUT('\n')
}
}