From 2c25df122a5dffdb56669d8f3e5f03232867d130 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 28 Jul 2019 21:29:49 +0200 Subject: [PATCH] merge strings in asm output --- .../compiler/target/c64/codegen2/AsmGen2.kt | 28 +++++- examples/test.p8 | 93 +++---------------- 2 files changed, 37 insertions(+), 84 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt b/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt index 0c1ffaafb..9bf15d52c 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt @@ -250,10 +250,8 @@ internal class AsmGen2(val program: Program, DataType.STRUCT -> {} // is flattened DataType.STR, DataType.STR_S -> { val string = (decl.value as ReferenceLiteralValue).str!! - val bytes = encodeStr(string, decl.datatype).map { "$" + it.toString(16).padStart(2, '0') } - out("${decl.name}\t; ${decl.datatype} \"${escape(string).replace("\u0000", "")}\"") - for (chunk in bytes.chunked(16)) - out(" .byte " + chunk.joinToString()) + val encoded = encodeStr(string, decl.datatype) + outputStringvar(decl, encoded) } DataType.ARRAY_UB -> { val data = makeArrayFillDataUnsigned(decl) @@ -334,12 +332,32 @@ internal class AsmGen2(val program: Program, val (structMembers, normalVars) = vars.partition { it.struct!=null } structMembers.forEach { vardecl2asm(it) } - normalVars.sortedBy { it.datatype }.forEach { + // special treatment for string types: merge strings that are identical + val encodedstringVars = normalVars + .filter {it.datatype in StringDatatypes } + .map { it to encodeStr((it.value as ReferenceLiteralValue).str!!, it.datatype) } + .groupBy({it.second}, {it.first}) + for((encoded, vars) in encodedstringVars) { + vars.dropLast(1).forEach { out(it.name) } + val lastvar = vars.last() + outputStringvar(lastvar, encoded) + } + + // non-string variables + normalVars.filter{ it.datatype !in StringDatatypes}.sortedBy { it.datatype }.forEach { if(it.scopedname !in allocatedZeropageVariables) vardecl2asm(it) } } + private fun outputStringvar(lastvar: VarDecl, encoded: List) { + val string = (lastvar.value as ReferenceLiteralValue).str!! + out("${lastvar.name}\t; ${lastvar.datatype} \"${escape(string).replace("\u0000", "")}\"") + val outputBytes = encoded.map { "$" + it.toString(16).padStart(2, '0') } + for (chunk in outputBytes.chunked(16)) + out(" .byte " + chunk.joinToString()) + } + private fun makeArrayFillDataUnsigned(decl: VarDecl): List { val array = (decl.value as ReferenceLiteralValue).array!! return when { diff --git a/examples/test.p8 b/examples/test.p8 index aa1e117b5..83a69dae0 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,87 +6,22 @@ sub start() { - str s1 = "irmen" - str_s s2 = "hello" + str s1 = "hello" + str s2 = "hello" + str s3 = "hello" + str s4 = "hello" - byte[4] barray - ubyte[4] ubarray - word[4] warray - uword[4] uwarray - float[4] flarray + if true { + c64scr.print("irmen") + c64scr.print("hello") + c64scr.print("hello2") + } - byte bb - ubyte ub - word ww - uword uw - float fl - - A=s1[2] - ub=s1[2] - ub=s2[2] - bb=barray[2] - ub=ubarray[2] - ww=warray[2] - uw=uwarray[2] - fl=flarray[2] - - A=s1[Y] - ub=s1[A] - ub=s2[A] - bb=barray[A] - ub=ubarray[A] - ww=warray[A] - uw=uwarray[A] - fl=flarray[A] - - A=s1[bb] - ub=s1[bb] - ub=s2[bb] - bb=barray[bb] - ub=ubarray[bb] - ww=warray[bb] - uw=uwarray[bb] - fl=flarray[bb] - - A=s1[bb*3] - ub=s1[bb*3] - ub=s2[bb*3] - bb=barray[bb*3] - ub=ubarray[bb*3] - ww=warray[bb*3] - uw=uwarray[bb*3] - fl=flarray[bb*3] - -; float f1 = 1.1 -; float f2 = 2.2 -; -; @(1024) = f1==f2 -; -; c64.CHROUT('\n') -; c64scr.print_ub(f1==f2) -; c64.CHROUT('\n') -; -; if f1 ==0.0 -; c64scr.print("error\n") -; else -; c64scr.print("ok\n") - -; str s1 = "hello" -; str s2 = "hello" -; str s3 = "hello" -; str s4 = "hello" -; -; if true { -; ubyte ub1 = 33 -; A=ub1 -; c64scr.print("irmen") -; } -; -; if true { -; ubyte ub1 = 33 -; A=ub1 -; c64scr.print("irmen") -; } + if true { + c64scr.print("irmen") + c64scr.print("hello") + c64scr.print("hello2") + } } }