From b911a95fc220e6d00d7d8dbdd8a30bc3e6a91c2e Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 31 Jan 2019 23:47:48 +0100 Subject: [PATCH] fix compiler crash for non-const array literals --- compiler/src/prog8/optimizing/ConstantFolding.kt | 5 +++-- compiler/src/prog8/optimizing/StatementOptimizer.kt | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/src/prog8/optimizing/ConstantFolding.kt b/compiler/src/prog8/optimizing/ConstantFolding.kt index 48ee68756..0a71f8cf7 100644 --- a/compiler/src/prog8/optimizing/ConstantFolding.kt +++ b/compiler/src/prog8/optimizing/ConstantFolding.kt @@ -33,7 +33,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV if(decl.type==VarDeclType.CONST || decl.type==VarDeclType.VAR) { val litval = decl.value as? LiteralValue - if(litval!=null && litval.isArray) + if(litval!=null && litval.isArray && litval.heapId!=null) fixupArrayTypeOnHeap(decl, litval) when(decl.datatype) { DataType.FLOAT -> { @@ -97,9 +97,10 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV private fun fixupArrayTypeOnHeap(decl: VarDecl, litval: LiteralValue) { // fix the type of the array value that's on the heap, to match the vardecl. // notice that checking the bounds of the actual values is not done here, but in the AstChecker later. + if(decl.datatype==litval.type) return // already correct datatype - val heapId = litval.heapId!! + val heapId = litval.heapId ?: throw FatalAstException("expected array to be on heap $litval") val array=heap.get(heapId) when(decl.datatype) { DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W -> { diff --git a/compiler/src/prog8/optimizing/StatementOptimizer.kt b/compiler/src/prog8/optimizing/StatementOptimizer.kt index 5fd019f05..67c043d02 100644 --- a/compiler/src/prog8/optimizing/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizing/StatementOptimizer.kt @@ -8,6 +8,9 @@ import kotlin.math.floor /* + todo: subroutines with 1 or 2 byte args or 1 word arg can be converted to asm sub calling convention (args in registers) + + todo: implement usage counters for blocks, variables, subroutines, heap variables. Then: todo remove unused blocks todo remove unused variables