From 46fbe01df9850bd11bd396061ed232d818c3dc6d Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 2 Oct 2020 22:37:52 +0200 Subject: [PATCH] added codengeration for assigment of array of values to a struct variable (all members at once) --- .../ast/processing/StatementReorderer.kt | 11 +++++++++- docs/source/todo.rst | 6 +++--- examples/test.p8 | 21 ++++++++++--------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/compiler/src/prog8/ast/processing/StatementReorderer.kt b/compiler/src/prog8/ast/processing/StatementReorderer.kt index 5f0fed19b..4546c1f26 100644 --- a/compiler/src/prog8/ast/processing/StatementReorderer.kt +++ b/compiler/src/prog8/ast/processing/StatementReorderer.kt @@ -202,7 +202,16 @@ internal class StatementReorderer(val program: Program) : AstWalker() { } } sourceVar.isArray -> { - TODO("assign struct array $structAssignment") + val array = (sourceVar.value as ArrayLiteralValue).value + return struct.statements.zip(array).map { + val decl = it.first as VarDecl + val mangled = mangledStructMemberName(identifierName, decl.name) + val targetName = IdentifierReference(listOf(mangled), structAssignment.position) + val target = AssignTarget(targetName, null, null, structAssignment.position) + val assign = Assignment(target, it.second, structAssignment.position) + assign.linkParents(structAssignment) + assign + } } else -> { throw FatalAstException("can only assign arrays or structs to structs") diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 74262d978..0469f535f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,7 @@ TODO ==== - get rid of all other TODO's in the code ;-) -- make it possible for array literals to not only contain compile time constants -- further optimize assignment codegeneration +- make it possible for array literals to not only contain compile time constants? - implement @stack for asmsub parameters - make it possible to use cpu opcodes such as 'nop' as variable names by prefixing all asm vars with something such as '_' - option to load the built-in library files from a directory instead of the embedded ones (for easier library development/debugging) @@ -17,7 +16,8 @@ More optimizations Add more compiler optimizations to the existing ones. -- more targeted optimizations for assigment asm code, such as the following: +- further optimize assignment codegeneration, such as the following: +- binexpr splitting (beware self-referencing expressions and asm code ballooning though) - subroutine calling convention? like: 1 byte arg -> pass in A, 2 bytes -> pass in A+Y, return value likewise. - can such parameter passing to subroutines be optimized to avoid copying? - more optimizations on the language AST level diff --git a/examples/test.p8 b/examples/test.p8 index e0d02874e..656ef2307 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -20,21 +20,22 @@ main { sub start() { - txt.print_uwhex(&c1, true) + txt.print_ub(c1.red) txt.chrout('\n') - txt.print_uwhex(&c2, true) + txt.print_ub(c1.green) txt.chrout('\n') - txt.print_uwhex(&c3, true) + txt.print_ub(c1.blue) txt.chrout('\n') - txt.print_uwhex(colors[0], true) - txt.chrout('\n') - txt.print_uwhex(colors[1], true) - txt.chrout('\n') - txt.print_uwhex(colors[2], true) txt.chrout('\n') - c1 = c2 - c1 = [11,22,33] ; TODO implement rewrite into individual struct member assignments + c1 = [99,88,77] + + txt.print_ub(c1.red) + txt.chrout('\n') + txt.print_ub(c1.green) + txt.chrout('\n') + txt.print_ub(c1.blue) + txt.chrout('\n') testX() }