fixed compiler crash when casting float to integer, fixed float to int cast value error on cx16

This commit is contained in:
Irmen de Jong 2022-05-03 23:43:38 +02:00
parent 0869789214
commit 0f9e87d7bb
8 changed files with 37 additions and 20 deletions

View File

@ -560,7 +560,14 @@ internal class AssignmentAsmGen(private val program: Program,
} }
if(target.kind==TargetStorageKind.REGISTER) { if(target.kind==TargetStorageKind.REGISTER) {
assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD) if(valueDt==DataType.FLOAT && target.datatype!=DataType.FLOAT) {
// have to typecast the float number on the fly down to an integer
assignExpressionToRegister(value, RegisterOrPair.FAC1, target.datatype in SignedDatatypes)
assignTypeCastedFloatFAC1("P8ZP_SCRATCH_W1", target.datatype)
assignVariableToRegister("P8ZP_SCRATCH_W1", target.register!!, target.datatype in SignedDatatypes)
} else {
assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD)
}
return return
} }
@ -2294,10 +2301,14 @@ internal class AssignmentAsmGen(private val program: Program,
} }
internal fun assignExpressionToVariable(expr: Expression, asmVarName: String, dt: DataType, scope: Subroutine?) { internal fun assignExpressionToVariable(expr: Expression, asmVarName: String, dt: DataType, scope: Subroutine?) {
val src = AsmAssignSource.fromAstSource(expr, program, asmgen) if(expr.inferType(program) istype DataType.FLOAT && dt!=DataType.FLOAT) {
val tgt = AsmAssignTarget(TargetStorageKind.VARIABLE, program, asmgen, dt, scope, variableAsmName = asmVarName) throw AssemblyError("can't directly assign a FLOAT expression to an integer variable $expr")
val assign = AsmAssignment(src, tgt, false, program.memsizer, expr.position) } else {
translateNormalAssignment(assign) val src = AsmAssignSource.fromAstSource(expr, program, asmgen)
val tgt = AsmAssignTarget(TargetStorageKind.VARIABLE, program, asmgen, dt, scope, variableAsmName = asmVarName)
val assign = AsmAssignment(src, tgt, false, program.memsizer, expr.position)
translateNormalAssignment(assign)
}
} }
internal fun assignVariableToRegister(asmVarName: String, register: RegisterOrPair, signed: Boolean) { internal fun assignVariableToRegister(asmVarName: String, register: RegisterOrPair, signed: Boolean) {

View File

@ -116,8 +116,8 @@ cast_FAC1_as_w_into_ay .proc ; also used for float 2 b
; -- cast fac1 to word into A/Y ; -- cast fac1 to word into A/Y
stx P8ZP_SCRATCH_REG stx P8ZP_SCRATCH_REG
jsr AYINT jsr AYINT
ldy $66 ldy floats.AYINT_facmo
lda $67 lda floats.AYINT_facmo+1
ldx P8ZP_SCRATCH_REG ldx P8ZP_SCRATCH_REG
rts rts
.pend .pend
@ -168,9 +168,9 @@ stack_float2w .proc ; also used for float2b
stx P8ZP_SCRATCH_REG stx P8ZP_SCRATCH_REG
jsr AYINT jsr AYINT
ldx P8ZP_SCRATCH_REG ldx P8ZP_SCRATCH_REG
lda $66 lda floats.AYINT_facmo
sta P8ESTACK_HI,x sta P8ESTACK_HI,x
lda $67 lda floats.AYINT_facmo+1
sta P8ESTACK_LO,x sta P8ESTACK_LO,x
dex dex
rts rts

View File

@ -151,6 +151,7 @@ asmsub FREADUY (ubyte value @Y) {
}} }}
} }
&uword AYINT_facmo = $66 ; $66/$67 contain result of AYINT
%asminclude "library:c128/floats.asm" %asminclude "library:c128/floats.asm"
%asminclude "library:c64/floats_funcs.asm" %asminclude "library:c64/floats_funcs.asm"

View File

@ -113,8 +113,8 @@ cast_FAC1_as_w_into_ay .proc ; also used for float 2 b
; -- cast fac1 to word into A/Y ; -- cast fac1 to word into A/Y
stx P8ZP_SCRATCH_REG stx P8ZP_SCRATCH_REG
jsr AYINT jsr AYINT
ldy $64 ldy floats.AYINT_facmo
lda $65 lda floats.AYINT_facmo+1
ldx P8ZP_SCRATCH_REG ldx P8ZP_SCRATCH_REG
rts rts
.pend .pend
@ -165,9 +165,9 @@ stack_float2w .proc ; also used for float2b
stx P8ZP_SCRATCH_REG stx P8ZP_SCRATCH_REG
jsr AYINT jsr AYINT
ldx P8ZP_SCRATCH_REG ldx P8ZP_SCRATCH_REG
lda $64 lda floats.AYINT_facmo
sta P8ESTACK_HI,x sta P8ESTACK_HI,x
lda $65 lda floats.AYINT_facmo+1
sta P8ESTACK_LO,x sta P8ESTACK_LO,x
dex dex
rts rts

View File

@ -174,6 +174,7 @@ asmsub GETADRAY () clobbers(X) -> uword @ AY {
}} }}
} }
&uword AYINT_facmo = $64 ; $64/$65 contain result of AYINT
%asminclude "library:c64/floats.asm" %asminclude "library:c64/floats.asm"
%asminclude "library:c64/floats_funcs.asm" %asminclude "library:c64/floats_funcs.asm"

View File

@ -161,6 +161,7 @@ asmsub RND() clobbers(A,X,Y) {
}} }}
} }
&uword AYINT_facmo = $c6 ; $c6/$c7 contain result of AYINT
%asminclude "library:c64/floats.asm" %asminclude "library:c64/floats.asm"
%asminclude "library:c64/floats_funcs.asm" %asminclude "library:c64/floats_funcs.asm"

View File

@ -3,7 +3,6 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- compiler: fix crash when print_ub(float as ubyte)
- vm: implement float any, all, reverse, sort - vm: implement float any, all, reverse, sort
- vm: fix test fp calc result being 0 - vm: fix test fp calc result being 0
- vm: get rid of intermediate floats.xxx() functions somehow, instead generate the float instructions directly? - vm: get rid of intermediate floats.xxx() functions somehow, instead generate the float instructions directly?

View File

@ -8,17 +8,21 @@
main { main {
sub start() { sub start() {
float fl = -4.567 float fl = 42.123
float fl2 = fl / 1.0 float fl2 = fl / 1.0
floats.print_f(fl2) txt.print_ub(fl2 as ubyte)
txt.nl() txt.nl()
txt.print_ub(fl2 as ubyte) ; TODO fix crash source dt size must be less or equal to target dt size txt.print_uw(fl2 as uword)
txt.nl() txt.nl()
txt.print_b(fl2 as byte) ; TODO fix crash byte bb = fl2 as byte
txt.print_b(bb)
txt.nl() txt.nl()
txt.print_uw(fl2 as uword) ; TODO fix crash txt.print_b(fl2 as byte)
txt.nl() txt.nl()
txt.print_w(fl2 as word) ; TODO fix crash txt.print_w(fl2 as word)
txt.nl()
word ww = fl2 as word
txt.print_w(ww)
txt.nl() txt.nl()
; txt.print("rad 180 = ") ; txt.print("rad 180 = ")