From 755af6010e5a6f09e35f81b8e71d249e29e94024 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 21 Apr 2019 03:04:13 +0200 Subject: [PATCH] fix some more issues with array vardecls without array size specifier --- compiler/src/prog8/ast/AST.kt | 2 +- compiler/src/prog8/ast/AstChecker.kt | 2 +- compiler/src/prog8/ast/StmtReorderer.kt | 11 +++++++++ .../src/prog8/optimizing/ConstantFolding.kt | 4 ++-- examples/test.p8 | 23 +++++++++++++++---- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/compiler/src/prog8/ast/AST.kt b/compiler/src/prog8/ast/AST.kt index f4cdf98c3..58af745ad 100644 --- a/compiler/src/prog8/ast/AST.kt +++ b/compiler/src/prog8/ast/AST.kt @@ -664,7 +664,7 @@ enum class VarDeclType { class VarDecl(val type: VarDeclType, private val declaredDatatype: DataType, val zeropage: Boolean, - val arraysize: ArrayIndex?, + var arraysize: ArrayIndex?, val isUnsizedArray: Boolean, val name: String, var value: IExpression?, diff --git a/compiler/src/prog8/ast/AstChecker.kt b/compiler/src/prog8/ast/AstChecker.kt index 8423d7e95..f778d94aa 100644 --- a/compiler/src/prog8/ast/AstChecker.kt +++ b/compiler/src/prog8/ast/AstChecker.kt @@ -551,7 +551,7 @@ private class AstChecker(private val namespace: INameScope, } VarDeclType.MEMORY -> { if(decl.arraysize!=null) { - val arraySize = decl.arraysize.size() ?: 1 + val arraySize = decl.arraysize!!.size() ?: 1 when(decl.datatype) { DataType.ARRAY_B, DataType.ARRAY_UB -> if(arraySize > 256) diff --git a/compiler/src/prog8/ast/StmtReorderer.kt b/compiler/src/prog8/ast/StmtReorderer.kt index bb29acaf1..5b0a65e5d 100644 --- a/compiler/src/prog8/ast/StmtReorderer.kt +++ b/compiler/src/prog8/ast/StmtReorderer.kt @@ -155,6 +155,17 @@ private class StatementReorderer(private val namespace: INameScope, private val return scope } + override fun process(decl: VarDecl): IStatement { + if(decl.arraysize==null) { + val array = decl.value as? LiteralValue + if(array!=null && array.isArray) { + val size = heap.get(array.heapId!!).arraysize + decl.arraysize = ArrayIndex(LiteralValue.optimalInteger(size, decl.position), decl.position) + } + } + return super.process(decl) + } + private fun sortConstantAssignments(statements: MutableList) { // sort assignments by datatype and value, so multiple initializations with the same value can be optimized (to load the value just once) val result = mutableListOf() diff --git a/compiler/src/prog8/optimizing/ConstantFolding.kt b/compiler/src/prog8/optimizing/ConstantFolding.kt index b7f5c59bb..75d7a2a15 100644 --- a/compiler/src/prog8/optimizing/ConstantFolding.kt +++ b/compiler/src/prog8/optimizing/ConstantFolding.kt @@ -70,7 +70,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV errors.add(ExpressionError("arraysize requires only integers here", litval.position)) if(decl.arraysize==null) return decl - val size = decl.arraysize.size() + val size = decl.arraysize!!.size() 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 @@ -100,7 +100,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV DataType.ARRAY_F -> { if(decl.arraysize==null) return decl - val size = decl.arraysize.size() + val size = decl.arraysize!!.size() if ((litval==null || !litval.isArray) && size != null) { // arraysize initializer is empty or a single int, and we know the size; create the arraysize. val fillvalue = if (litval == null) 0.0 else litval.asNumericValue?.toDouble() ?: 0.0 diff --git a/examples/test.p8 b/examples/test.p8 index bd93cbb7c..aa7ef5c8c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,15 +1,28 @@ %zeropage basicsafe +%option enable_floats +%import c64flt ~ main { - &uword COLORS = $d020 + + float[] fa = [1.1,2.2,3.3] + ubyte[] uba = [10,2,3,4] + byte[] ba = [-10,2,3,4] + uword[] uwa = [100,20,30,40] + word[] wa = [-100,20,30,40] sub start() { - COLORS=12345 - COLORS=12346 - @(COLORS) = 54 + float a + a=avg([1,2,3,4]) + c64flt.print_f(a) + c64.CHROUT('\n') + a=avg([100,200,300,400]) + c64flt.print_f(a) + c64.CHROUT('\n') + a=avg([1.1,2.2,3.3,4.4]) + c64flt.print_f(a) + c64.CHROUT('\n') - return } }