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
asmgen.translateExpression(value)
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("""
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FADD
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FADD
""")
}
"-" -> {
asmgen.out("""
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FSUB
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
jsr c64flt.pop_float_fac1
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.FSUB
""")
}
"*" -> {
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(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")
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
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
}
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)
when (operator) {
"**" -> TODO("pow")
"+" -> TODO("+")
"-" -> TODO("-")
"**" -> {
asmgen.out("""
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("""
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$otherName
ldy #>$otherName
jsr c64flt.FMULT
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$otherName
ldy #>$otherName
jsr c64flt.FMULT
""")
}
"/" -> {
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")
}
// 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) {
val constValueName = asmgen.getFloatAsmConst(value)
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)
return
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.FADD
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
stx P8ZP_SCRATCH_REG_X
lda #<$name
ldy #>$name
jsr c64flt.MOVFM
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.FADD
""")
}
"-" -> {
if (value == 0.0)
return
asmgen.out("""
stx P8ZP_SCRATCH_REG_X
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.MOVFM
lda #<$name
ldy #>$name
jsr c64flt.FSUB
ldx #<$name
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
stx P8ZP_SCRATCH_REG_X
lda #<$constValueName
ldy #>$constValueName
jsr c64flt.MOVFM
lda #<$name
ldy #>$name
jsr c64flt.FSUB
""")
}
"*" -> {
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)
throw AssemblyError("division by zero")
TODO("div")
}
"%" -> {
if (value == 0.0)
throw AssemblyError("division by zero")
TODO("float remainder???")
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
ldy #>$name
jsr c64flt.MOVMF
ldx P8ZP_SCRATCH_REG_X
""")
}
private fun inplaceModification_byte_value_to_memory(pointervar: IdentifierReference, operator: String, value: Expression) {

View File

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

View File

@ -2,8 +2,6 @@
%import c64textio
%zeropage basicsafe
; TODO implement float PLUS asm generation
main {
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')
}
}