optimized zeros array initializer

This commit is contained in:
Irmen de Jong 2020-03-22 02:56:36 +01:00
parent 63c073c93f
commit 954e911eb3
3 changed files with 18 additions and 21 deletions

View File

@ -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<IAstModification> {
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<IAstModification> {
// insert AddressOf (&) expression where required (string params to a UWORD function param etc).
var parentStatement: Node = functionCall

View File

@ -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<IAstModification> {
// 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<IAstModification> {
val leftDt = expr.left.inferType(program)
val rightDt = expr.right.inferType(program)

View File

@ -373,7 +373,14 @@ internal class AsmGen(private val program: Program,
}
private fun makeArrayFillDataUnsigned(decl: VarDecl): List<String> {
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