From 954e911eb3a24632573a3546610366bf4a70ba43 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 22 Mar 2020 02:56:36 +0100 Subject: [PATCH] optimized zeros array initializer --- .../prog8/ast/processing/AddressOfInserter.kt | 20 ------------------- .../prog8/ast/processing/TypecastsAdder.kt | 10 ++++++++++ .../compiler/target/c64/codegen/AsmGen.kt | 9 ++++++++- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/compiler/src/prog8/ast/processing/AddressOfInserter.kt b/compiler/src/prog8/ast/processing/AddressOfInserter.kt index 0e9bd3165..21643f61a 100644 --- a/compiler/src/prog8/ast/processing/AddressOfInserter.kt +++ b/compiler/src/prog8/ast/processing/AddressOfInserter.kt @@ -15,26 +15,6 @@ internal class AddressOfInserter(val program: Program): AstWalker() { // Insert AddressOf (&) expression where required (string params to a UWORD function param etc). // TODO join this into the StatementReorderer? - override fun after(decl: VarDecl, parent: Node): Iterable { - if(decl.isArray && decl.value==null) { - // array datatype without initialization value, add list of zeros - // TODO let the code generator take care of this? - val arraysize = decl.arraysize!!.size()!! - val zero = decl.asDefaultValueDecl(decl).value!! - return listOf(IAstModification.SetExpression( // can't use replaceNode here because value is null - { newExpr -> decl.value = newExpr }, - ArrayLiteralValue(InferredTypes.InferredType.known(decl.datatype), - Array(arraysize) { zero }, - decl.position), - decl)) - } - - if(decl.value!=null && decl.type==VarDeclType.VAR && !decl.isArray) - decl.definingBlock().initialValues += decl - - return emptyList() - } - override fun after(functionCall: FunctionCall, parent: Node): Iterable { // insert AddressOf (&) expression where required (string params to a UWORD function param etc). var parentStatement: Node = functionCall diff --git a/compiler/src/prog8/ast/processing/TypecastsAdder.kt b/compiler/src/prog8/ast/processing/TypecastsAdder.kt index 93587226b..0432d90df 100644 --- a/compiler/src/prog8/ast/processing/TypecastsAdder.kt +++ b/compiler/src/prog8/ast/processing/TypecastsAdder.kt @@ -7,6 +7,7 @@ import prog8.ast.Program import prog8.ast.base.DataType import prog8.ast.base.ErrorReporter import prog8.ast.base.FatalAstException +import prog8.ast.base.VarDeclType import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.functions.BuiltinFunctions @@ -18,6 +19,15 @@ class TypecastsAdder(val program: Program, val errors: ErrorReporter) : AstWalke * (this includes function call arguments) */ + override fun after(decl: VarDecl, parent: Node): Iterable { + + // collect all variables that have an initialisation value + if(decl.value!=null && decl.type== VarDeclType.VAR && !decl.isArray) + decl.definingBlock().initialValues += decl + + return emptyList() + } + override fun after(expr: BinaryExpression, parent: Node): Iterable { val leftDt = expr.left.inferType(program) val rightDt = expr.right.inferType(program) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 6b4e1ab16..b201b00ec 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -373,7 +373,14 @@ internal class AsmGen(private val program: Program, } private fun makeArrayFillDataUnsigned(decl: VarDecl): List { - val array = (decl.value as ArrayLiteralValue).value + val array = + if(decl.value!=null) + (decl.value as ArrayLiteralValue).value + else { + // no array init value specified, use a list of zeros + val zero = decl.asDefaultValueDecl(decl.parent).value!! + Array(decl.arraysize!!.size()!!) { zero } + } return when (decl.datatype) { DataType.ARRAY_UB -> // byte array can never contain pointer-to types, so treat values as all integers