From 1b14da6c03227fc0a661b8dc09da78aa52dd3941 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 24 Mar 2021 22:01:22 +0100 Subject: [PATCH] compiler warning instead of crash when attempting to assign invalid array value to other array --- .../astprocessing/StatementReorderer.kt | 6 ++- .../assignment/AugmentableAssignmentAsmGen.kt | 44 +++++++++---------- examples/test.p8 | 10 +++-- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt index 02ad6ca79..b1de43438 100644 --- a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt +++ b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt @@ -298,6 +298,10 @@ internal class StatementReorderer(val program: Program, val errors: IErrorReport if(targetVar.arraysize==null) errors.err("array has no defined size", assign.position) + if(assign.value !is IdentifierReference) { + errors.err("invalid array value to assign to other array", assign.value.position) + return noModifications + } val sourceIdent = assign.value as IdentifierReference val sourceVar = sourceIdent.targetVarDecl(program)!! if(!sourceVar.isArray) { @@ -310,7 +314,7 @@ internal class StatementReorderer(val program: Program, val errors: IErrorReport } if(!errors.isEmpty()) - return emptyList() + return noModifications val memcopy = FunctionCallStatement(IdentifierReference(listOf("sys", "memcopy"), assign.position), mutableListOf( diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/assignment/AugmentableAssignmentAsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/assignment/AugmentableAssignmentAsmGen.kt index aaf539299..7a3f57feb 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/assignment/AugmentableAssignmentAsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/assignment/AugmentableAssignmentAsmGen.kt @@ -270,8 +270,8 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, } } } - TargetStorageKind.REGISTER -> TODO("reg in-place modification") - TargetStorageKind.STACK -> TODO("stack in-place modification") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg in-place modification") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack in-place modification") } } @@ -1732,9 +1732,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, } } } - TargetStorageKind.ARRAY -> TODO("in-place not of ubyte array") - TargetStorageKind.REGISTER -> TODO("reg not") - TargetStorageKind.STACK -> TODO("stack not") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place not of ubyte array") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg not") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack not") } } DataType.UWORD -> { @@ -1751,9 +1751,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, sta ${target.asmVarname}+1""") } TargetStorageKind.MEMORY -> throw AssemblyError("no asm gen for uword-memory not") - TargetStorageKind.ARRAY -> TODO("in-place not of uword array") - TargetStorageKind.REGISTER -> TODO("reg not") - TargetStorageKind.STACK -> TODO("stack not") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place not of uword array") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg not") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack not") } } else -> throw AssemblyError("boolean-not of invalid type") @@ -1798,9 +1798,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, } } } - TargetStorageKind.ARRAY -> TODO("in-place invert ubyte array") - TargetStorageKind.REGISTER -> TODO("reg invert") - TargetStorageKind.STACK -> TODO("stack invert") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place invert ubyte array") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg invert") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack invert") } } DataType.UWORD -> { @@ -1815,9 +1815,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, sta ${target.asmVarname}+1""") } TargetStorageKind.MEMORY -> throw AssemblyError("no asm gen for uword-memory invert") - TargetStorageKind.ARRAY -> TODO("in-place invert uword array") - TargetStorageKind.REGISTER -> TODO("reg invert") - TargetStorageKind.STACK -> TODO("stack invert") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place invert uword array") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg invert") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack invert") } } else -> throw AssemblyError("invert of invalid type") @@ -1836,9 +1836,9 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, sta ${target.asmVarname}""") } TargetStorageKind.MEMORY -> throw AssemblyError("can't in-place negate memory ubyte") - TargetStorageKind.ARRAY -> TODO("in-place negate byte array") - TargetStorageKind.REGISTER -> TODO("reg negate") - TargetStorageKind.STACK -> TODO("stack negate") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place negate byte array") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg negate") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack negate") } } DataType.WORD -> { @@ -1853,10 +1853,10 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, sbc ${target.asmVarname}+1 sta ${target.asmVarname}+1""") } - TargetStorageKind.ARRAY -> TODO("in-place negate word array") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place negate word array") TargetStorageKind.MEMORY -> throw AssemblyError("no asm gen for word memory negate") - TargetStorageKind.REGISTER -> TODO("reg negate") - TargetStorageKind.STACK -> TODO("stack negate") + TargetStorageKind.REGISTER -> throw AssemblyError("missing codegen for reg negate") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack negate") } } DataType.FLOAT -> { @@ -1869,8 +1869,8 @@ internal class AugmentableAssignmentAsmGen(private val program: Program, sta ${target.asmVarname}+1 """) } - TargetStorageKind.ARRAY -> TODO("in-place negate float array") - TargetStorageKind.STACK -> TODO("stack float negate") + TargetStorageKind.ARRAY -> throw AssemblyError("missing codegen for in-place negate float array") + TargetStorageKind.STACK -> throw AssemblyError("missing codegen for stack float negate") else -> throw AssemblyError("weird target kind for float") } } diff --git a/examples/test.p8 b/examples/test.p8 index 3598cff5b..82c698f40 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -9,10 +9,14 @@ main { str string2 = "hello" uword xx = $f000 + word ww + + ubyte[] arr = [1,2,3] + arr = ~ arr + cx16.r0 = ~cx16.r0 + ww = -cx16.r0 + cx16.r0 = not cx16.r0 -; string1 = xx -; string1 = string2 - string1 = string2 as uword ; ubyte[] array = [1,2,3,4] ; ubyte ix