rest of optimizations following simplification of array indexer

This commit is contained in:
Irmen de Jong 2020-10-19 23:57:00 +02:00
parent 82a28bb555
commit 1a36302cf1
4 changed files with 49 additions and 72 deletions

View File

@ -208,17 +208,6 @@ pop_float_fac2 .proc
jmp CONUPK
.pend
pop_float_to_indexed_var .proc
; -- pop the float on the stack, to the memory in the array at A/Y indexed by the byte on stack
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
jsr prog8_lib.pop_index_times_5
jsr prog8_lib.add_a_to_zpword
lda P8ZP_SCRATCH_W1
ldy P8ZP_SCRATCH_W1+1
jmp pop_float
.pend
copy_float .proc
; -- copies the 5 bytes of the mflt value pointed to by SCRATCH_ZPWORD1,
; into the 5 bytes pointed to by A/Y. Clobbers A,Y.
@ -707,13 +696,12 @@ sign_f .proc
set_0_array_float .proc
; -- set a float in an array to zero (index on stack, array in SCRATCH_ZPWORD1)
inx
lda P8ESTACK_LO,x
; -- set a float in an array to zero (index in A, array in P8ZP_SCRATCH_W1)
sta P8ZP_SCRATCH_B1
asl a
asl a
clc
adc P8ESTACK_LO,x
adc P8ZP_SCRATCH_B1
tay
lda #0
sta (P8ZP_SCRATCH_W1),y
@ -730,13 +718,12 @@ set_0_array_float .proc
set_array_float .proc
; -- set a float in an array to a value (index on stack, float in SCRATCH_ZPWORD1, array in SCRATCH_ZPWORD2)
inx
lda P8ESTACK_LO,x
; -- set a float in an array to a value (index in A, float in P8ZP_SCRATCH_W1, array in P8ZP_SCRATCH_W2)
sta P8ZP_SCRATCH_B1
asl a
asl a
clc
adc P8ESTACK_LO,x
adc P8ZP_SCRATCH_B1
adc P8ZP_SCRATCH_W2
ldy P8ZP_SCRATCH_W2+1
bcc +

View File

@ -40,16 +40,6 @@ add_a_to_zpword .proc
+ rts
.pend
pop_index_times_5 .proc
inx
lda P8ESTACK_LO,x
asl a
asl a
clc
adc P8ESTACK_LO,x
rts
.pend
neg_b .proc
lda #0
sec

View File

@ -311,7 +311,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
adc #<${target.asmVarname}
bcc +
iny
+ jsr floats.pop_float""")
+ jsr floats.pop_float""")
}
else -> throw AssemblyError("weird dt")
}
@ -529,15 +529,23 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
""")
}
TargetStorageKind.ARRAY -> {
// TODO optimize this (array indexer is just a simple number or variable), but the situation doesn't occur very often ****************************************
// if(target.constArrayIndexValue!=null) {
// TODO("const index ${target.constArrayIndexValue}")
// } else if(target.array!!.arrayspec.index is IdentifierReference) {
// TODO("array[var] ${target.constArrayIndexValue}")
// }
asmgen.out(" lda #<$sourceName | ldy #>$sourceName | jsr floats.push_float")
asmgen.translateExpression(target.array!!.indexer)
asmgen.out(" lda #<${target.asmVarname} | ldy #>${target.asmVarname} | jsr floats.pop_float_to_indexed_var")
asmgen.out("""
lda #<$sourceName
ldy #>$sourceName
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
lda #<${target.asmVarname}
ldy #>${target.asmVarname}
sta P8ZP_SCRATCH_W2
sty P8ZP_SCRATCH_W2+1""")
if(target.array!!.indexer.indexNum!=null) {
val index = target.array.indexer.constIndex()!!
asmgen.out(" lda #$index")
} else {
val asmvarname = asmgen.asmVariableName(target.array.indexer.indexVar!!)
asmgen.out(" lda $asmvarname")
}
asmgen.out(" jsr floats.set_array_float")
}
TargetStorageKind.MEMORY -> throw AssemblyError("can't assign float to mem byte")
TargetStorageKind.REGISTER -> throw AssemblyError("can't assign float to register")
@ -824,18 +832,8 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
throw AssemblyError("no asm gen for assign word $word to memory ${target.memory}")
}
TargetStorageKind.ARRAY -> {
// TODO optimize this (array indexer is just a simple number or variable), but the situation doesn't occur very often ****************************************
// if(target.constArrayIndexValue!=null) {
// TODO("const index ${target.constArrayIndexValue}")
// } else if(target.array!!.arrayspec.index is IdentifierReference) {
// TODO("array[var] ${target.constArrayIndexValue}")
// }
asmgen.translateExpression(target.array!!.indexer)
asmgen.loadScaledArrayIndexIntoRegister(target.array!!, DataType.UWORD, CpuRegister.Y)
asmgen.out("""
inx
lda P8ESTACK_LO,x
asl a
tay
lda #<${word.toHex()}
sta ${target.asmVarname},y
lda #>${word.toHex()}
@ -931,13 +929,13 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
sta ${target.asmVarname}+$indexValue+4
""")
} else {
// TODO optimize this (don't use stack eval) as the indexer is only a simple variable ****************************************
asmgen.translateExpression(target.array.indexer.indexVar!!)
val asmvarname = asmgen.asmVariableName(target.array.indexer.indexVar!!)
asmgen.out("""
lda #<${target.asmVarname}
sta P8ZP_SCRATCH_W1
lda #>${target.asmVarname}
sta P8ZP_SCRATCH_W1+1
lda $asmvarname
jsr floats.set_0_array_float
""")
}
@ -984,8 +982,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
sta $arrayVarName+$indexValue+4
""")
} else {
// TODO optimize this (don't use stack eval) as the indexer is only a simple variable ****************************************
asmgen.translateExpression(target.array.indexer.indexVar!!)
val asmvarname = asmgen.asmVariableName(target.array.indexer.indexVar!!)
asmgen.out("""
lda #<${constFloat}
sta P8ZP_SCRATCH_W1
@ -995,6 +992,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
sta P8ZP_SCRATCH_W2
lda #>${arrayVarName}
sta P8ZP_SCRATCH_W2+1
lda $asmvarname
jsr floats.set_array_float
""")
}

View File

@ -7,25 +7,27 @@ main {
sub start() {
uword vv = $1111
uword vv2
vv2 = vv2+(vv/2)
vv2 = vv2+(vv - 1)
vv2 = vv2+(vv + $0200)
vv2 = vv2+(vv - $0400)
txt.print_uw(vv2)
txt.chrout('\n')
uword[] array = [1, 2, 3]
uword fzero = 0.0
uword fnine = 9999
array[0] = 0
ubyte ii = 1
array[ii] = 0
word ww = -$1111
word ww2 = 0
ww2 = ww2 + ww + $0200
ww2 = ww2 +ww - $0400
txt.print_w(ww2)
txt.chrout('\n')
ww2= ww2 + ww + -$0200
ww2= ww2 + ww - -$0400
txt.print_w(ww2)
txt.chrout('\n')
uword ff
for ii in 0 to len(array)-1 {
txt.print_uw(array[ii])
txt.chrout('\n')
}
array[0] = 9
ii = 1
array[ii] = 9
for ii in 0 to len(array)-1 {
txt.print_uw(array[ii])
txt.chrout('\n')
}
testX()
}