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,7 +235,16 @@ 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
@ -243,10 +252,6 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
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
""") """)
} }
"-" -> { "-" -> {
@ -256,26 +261,36 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.FSUB jsr c64flt.FSUB
""")
}
"*" -> {
asmgen.out("""
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FMULT
""")
}
"/" -> {
asmgen.out("""
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FDIV
""")
}
else -> throw AssemblyError("invalid operator for in-place float modification $operator")
}
// store Fac1 back into memory
asmgen.out("""
ldx #<$name ldx #<$name
ldy #>$name ldy #>$name
jsr c64flt.MOVMF jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X ldx P8ZP_SCRATCH_REG_X
""") """)
} }
"*" -> TODO("mul")// asmgen.out(" jsr prog8_lib.mul_byte") // the optimized routines should have been checked earlier
"/" -> {
TODO("div")
// asmgen.out(if(types==DataType.UBYTE) " jsr prog8_lib.idiv_ub" else " jsr prog8_lib.idiv_b")
}
"%" -> {
TODO("float remainder???")
// if(types==DataType.BYTE)
// 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")
}
}
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) {
val valueDt = ident.targetVarDecl(program.namespace)!!.datatype val valueDt = ident.targetVarDecl(program.namespace)!!.datatype
@ -284,9 +299,39 @@ 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
@ -296,22 +341,44 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
lda #<$otherName lda #<$otherName
ldy #>$otherName ldy #>$otherName
jsr c64flt.FMULT jsr c64flt.FMULT
""")
}
"/" -> {
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$otherName
ldy #>$otherName
jsr c64flt.MOVFM
lda #<$name
ldy #>$name
jsr c64flt.FDIV
""")
}
else -> throw AssemblyError("invalid operator for in-place float modification $operator")
}
// store Fac1 back into memory
asmgen.out("""
ldx #<$name ldx #<$name
ldy #>$name ldy #>$name
jsr c64flt.MOVMF jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X ldx P8ZP_SCRATCH_REG_X
""") """)
} }
"/" -> TODO("div")
"%" -> TODO("float remainder???")
else -> throw AssemblyError("invalid operator for in-place float modification $operator")
}
}
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
@ -323,10 +390,6 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
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
""") """)
} }
"-" -> { "-" -> {
@ -340,26 +403,42 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
lda #<$name lda #<$name
ldy #>$name ldy #>$name
jsr c64flt.FSUB jsr c64flt.FSUB
""")
}
"*" -> {
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.FMULT
""")
}
"/" -> {
if (value == 0.0)
throw AssemblyError("division by zero")
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.MOVFM
lda #<$name
ldy #>$name
jsr c64flt.FDIV
""")
}
else -> throw AssemblyError("invalid operator for in-place float modification $operator")
}
// store Fac1 back into memory
asmgen.out("""
ldx #<$name ldx #<$name
ldy #>$name ldy #>$name
jsr c64flt.MOVMF jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X ldx P8ZP_SCRATCH_REG_X
""") """)
} }
"*" -> TODO("mul")
"/" -> {
if (value == 0.0)
throw AssemblyError("division by zero")
TODO("div")
}
"%" -> {
if (value == 0.0)
throw AssemblyError("division by zero")
TODO("float remainder???")
}
else -> throw AssemblyError("invalid operator for in-place float modification $operator")
}
}
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) {
println("warning: slow stack evaluation used (3): @(${pointervar.nameInSource.last()}) $operator= ${value::class.simpleName} at ${value.position}") // TODO println("warning: slow stack evaluation used (3): @(${pointervar.nameInSource.last()}) $operator= ${value::class.simpleName} at ${value.position}") // TODO

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')
}
}