merge strings in asm output

This commit is contained in:
Irmen de Jong 2019-07-28 21:29:49 +02:00
parent 7cb5702b37
commit 2c25df122a
2 changed files with 37 additions and 84 deletions

View File

@ -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", "<NULL>")}\"")
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<Short>) {
val string = (lastvar.value as ReferenceLiteralValue).str!!
out("${lastvar.name}\t; ${lastvar.datatype} \"${escape(string).replace("\u0000", "<NULL>")}\"")
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<String> {
val array = (decl.value as ReferenceLiteralValue).array!!
return when {

View File

@ -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")
}
}
}