assign type relax

This commit is contained in:
Irmen de Jong 2020-08-23 13:31:14 +02:00
parent f4dafec645
commit 84ec1be8a4
2 changed files with 15 additions and 6 deletions

View File

@ -124,6 +124,9 @@ internal class AsmAssignSource(val type: AsmSourceStorageType,
AsmSourceStorageType.STACK -> TODO()
}
fun withAdjustedDt(newType: DataType) =
AsmAssignSource(type, program, newType, astVariable, astArray, astMemory, register, numLitval, astExpression)
}
@ -131,4 +134,8 @@ internal class AsmAssignment(val source: AsmAssignSource,
val target: AsmAssignTarget,
val isAugmentable: Boolean,
val position: Position) {
init {
require(source.datatype==target.datatype) {"source and target datatype must be identical"}
}
}

View File

@ -19,18 +19,20 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
private val augmentableAsmGen = AugmentableAssignmentAsmGen(program, this, asmgen)
internal fun translate(assignment: Assignment) {
val source = AsmAssignSource.fromAstSource(assignment.value, program)
var source = AsmAssignSource.fromAstSource(assignment.value, program)
val target = AsmAssignTarget.fromAstAssignment(assignment, program, asmgen)
val assign = AsmAssignment(source, target, assignment.isAugmentable, assignment.position)
// assert that the source and target types are identical (with some signed/unsigned relaxations)
// allow some signed/unsigned relaxations
if(target.datatype!=source.datatype) {
if (!(target.datatype in ByteDatatypes && source.datatype in ByteDatatypes ||
target.datatype in WordDatatypes && source.datatype in WordDatatypes)) {
throw AssemblyError("assignment type incompatibility ${target.datatype}=${source.datatype}")
if(target.datatype in ByteDatatypes && source.datatype in ByteDatatypes) {
source = source.withAdjustedDt(target.datatype)
} else if(target.datatype in WordDatatypes && source.datatype in WordDatatypes) {
source = source.withAdjustedDt(target.datatype)
}
}
val assign = AsmAssignment(source, target, assignment.isAugmentable, assignment.position)
when {
source.type==AsmSourceStorageType.LITERALNUMBER -> translateConstantValueAssignment(assign)
source.type==AsmSourceStorageType.VARIABLE -> translateVariableAssignment(assign)