From a81b82495c47156b43c8e8c08326e2471aadd169 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 17 Apr 2019 00:27:27 +0200 Subject: [PATCH] fix wrong values generated from range expression in array vardecl --- compiler/src/prog8/optimizing/ConstantFolding.kt | 7 +++++-- examples/test.p8 | 6 ++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/src/prog8/optimizing/ConstantFolding.kt b/compiler/src/prog8/optimizing/ConstantFolding.kt index 5eeccd9eb..b7f5c59bb 100644 --- a/compiler/src/prog8/optimizing/ConstantFolding.kt +++ b/compiler/src/prog8/optimizing/ConstantFolding.kt @@ -47,7 +47,10 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W -> { val rangeExpr = decl.value as? RangeExpr if(rangeExpr!=null) { - // convert the initializer range expression to an actual array + // convert the initializer range expression to an actual array (will be put on heap later) + val declArraySize = decl.arraysize?.size() + if(declArraySize!=null && declArraySize!=rangeExpr.size(heap)) + errors.add(ExpressionError("range expression size doesn't match declared array size", decl.value?.position!!)) val constRange = rangeExpr.toConstantIntegerRange(heap) if(constRange!=null) { val eltType = rangeExpr.resultingDatatype(namespace, heap)!! @@ -68,7 +71,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV if(decl.arraysize==null) return decl val size = decl.arraysize.size() - if ((litval==null || !litval.isArray) && size != null) { + if ((litval==null || !litval.isArray) && size != null && rangeExpr==null) { // arraysize initializer is empty or a single int, and we know the size; create the arraysize. val fillvalue = if (litval == null) 0 else litval.asIntegerValue ?: 0 when(decl.datatype){ diff --git a/examples/test.p8 b/examples/test.p8 index c51ee910f..2b3db4975 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,15 +4,13 @@ sub start() { - ubyte[5] array2 = 20 to 100 ; @todo fix this initializer value - word[5] array3 = 3000 to 3100 ; @todo fix this initializer value + ubyte[81] array2 = 20 to 100 + word[] array3 = 3000 to 3100 c64scr.print_uw(len(array2)) c64.CHROUT('\n') c64scr.print_uw(len(array3)) c64.CHROUT('\n') - c64scr.print_uw(len(array4)) - c64.CHROUT('\n') c64.CHROUT('\n') c64scr.print_ub(array2[0]) c64.CHROUT(',')