fix compiler crash on certain typecasting assignment

This commit is contained in:
Irmen de Jong 2022-07-06 00:28:43 +02:00
parent 41f4e22a17
commit 298b25cf7d
3 changed files with 34 additions and 1 deletions

View File

@ -791,6 +791,20 @@ internal class AssignmentAsmGen(private val program: Program,
assignTypeCastedFloatFAC1("P8ZP_SCRATCH_W1", target.datatype) assignTypeCastedFloatFAC1("P8ZP_SCRATCH_W1", target.datatype)
assignVariableToRegister("P8ZP_SCRATCH_W1", target.register!!, target.datatype in SignedDatatypes) assignVariableToRegister("P8ZP_SCRATCH_W1", target.register!!, target.datatype in SignedDatatypes)
} else { } else {
if(!(valueDt isAssignableTo targetDt)) {
if(valueDt in WordDatatypes && targetDt in ByteDatatypes) {
// word to byte, just take the lsb
return assignCastViaLsbFunc(value, target)
} else if(valueDt in WordDatatypes && targetDt in WordDatatypes) {
// word to word, just assign
assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD)
} else if(valueDt in ByteDatatypes && targetDt in ByteDatatypes) {
// byte to byte, just assign
assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD)
}
else
throw AssemblyError("can't cast $valueDt to $targetDt, this should have been checked in the astchecker")
}
assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD) assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD)
} }
return return

View File

@ -16,6 +16,26 @@ import prog8tests.helpers.compileText
class TestTypecasts: FunSpec({ class TestTypecasts: FunSpec({
test("word to byte casts") {
val text="""
%import textio
main {
sub func(ubyte arg) -> word {
return arg-99
}
sub start() {
txt.print_ub(func(0) as ubyte)
txt.print_uw(func(0) as ubyte)
txt.print_w(func(0) as ubyte)
}
}"""
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
val stmts = result.program.entrypoint.statements
stmts.size shouldBe 3
}
test("add missing & to function arguments") { test("add missing & to function arguments") {
val text=""" val text="""
main { main {

View File

@ -3,7 +3,6 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- fix compiler crash txt.print_w(value(99) as ubyte) where value()->word
- fix imageviewer compilation crash - fix imageviewer compilation crash
- petaxian is larger again after introduction of BOOL type (against main branch). WHY??? FIX. - petaxian is larger again after introduction of BOOL type (against main branch). WHY??? FIX.