From a1729b65aba6b0593a3cade5db603451ba7e8adf Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 6 Mar 2021 22:37:38 +0100 Subject: [PATCH] fix min(), max(), sum(), abs() --- compiler/res/prog8lib/prog8_funcs.asm | 2 + .../cpu6502/codegen/BuiltinFunctionsAsmGen.kt | 18 +++--- examples/compilerperformance/perf.p8 | 13 ++++- examples/compilerperformance/perf10.p8 | 14 +++-- examples/compilerperformance/perf2.p8 | 13 ++++- examples/compilerperformance/perf3.p8 | 13 ++++- examples/compilerperformance/perf4.p8 | 13 ++++- examples/compilerperformance/perf5.p8 | 13 ++++- examples/compilerperformance/perf6.p8 | 13 ++++- examples/compilerperformance/perf7.p8 | 13 ++++- examples/compilerperformance/perf8.p8 | 13 ++++- examples/compilerperformance/perf9.p8 | 14 +++-- examples/test.p8 | 57 +------------------ 13 files changed, 112 insertions(+), 97 deletions(-) diff --git a/compiler/res/prog8lib/prog8_funcs.asm b/compiler/res/prog8lib/prog8_funcs.asm index f69a2ccc2..c95ce9ec0 100644 --- a/compiler/res/prog8lib/prog8_funcs.asm +++ b/compiler/res/prog8lib/prog8_funcs.asm @@ -440,6 +440,7 @@ func_min_ub_stack .proc func_min_b_into_A .proc ; -- min(barray) -> A. (array in P8ZP_SCRATCH_W1, num elements in A) tay + dey lda #127 sta P8ZP_SCRATCH_B1 - lda (P8ZP_SCRATCH_W1),y @@ -556,6 +557,7 @@ func_min_w_stack .proc func_max_ub_into_A .proc ; -- max(ubarray) -> A (array in P8ZP_SCRATCH_W1, num elements in A) tay + dey lda #0 sta P8ZP_SCRATCH_B1 - lda (P8ZP_SCRATCH_W1),y diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt index 00639817a..69a634f47 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt @@ -483,11 +483,11 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } DataType.ARRAY_UW -> { asmgen.out(" jsr prog8_lib.func_${function.name}_uw_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.ARRAY_W -> { asmgen.out(" jsr prog8_lib.func_${function.name}_w_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.ARRAY_F -> { asmgen.out(" jsr floats.func_${function.name}_f_fac1") @@ -514,19 +514,19 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val when (dt.typeOrElse(DataType.STRUCT)) { DataType.ARRAY_UB, DataType.STR -> { asmgen.out(" jsr prog8_lib.func_sum_ub_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.ARRAY_B -> { asmgen.out(" jsr prog8_lib.func_sum_b_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.ARRAY_UW -> { asmgen.out(" jsr prog8_lib.func_sum_uw_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.ARRAY_W -> { asmgen.out(" jsr prog8_lib.func_sum_w_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.ARRAY_F -> { asmgen.out(" jsr floats.func_sum_f_fac1") @@ -929,15 +929,15 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val when (dt) { in ByteDatatypes -> { asmgen.out(" jsr prog8_lib.abs_b_into_A") - assignAsmGen.assignRegisterByte(AsmAssignTarget.fromRegisters(resultRegister!!, scope, program, asmgen), CpuRegister.A) + assignAsmGen.assignRegisterByte(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.A, scope, program, asmgen), CpuRegister.A) } in WordDatatypes -> { asmgen.out(" jsr prog8_lib.abs_w_into_AY") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister!!, scope, program, asmgen), RegisterOrPair.AY) + assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.AY, scope, program, asmgen), RegisterOrPair.AY) } DataType.FLOAT -> { asmgen.out(" jsr floats.abs_f_fac1") - assignAsmGen.assignRegisterpairWord(AsmAssignTarget.fromRegisters(resultRegister!!, scope, program, asmgen), RegisterOrPair.FAC1) + assignAsmGen.assignFAC1float(AsmAssignTarget.fromRegisters(resultRegister ?: RegisterOrPair.FAC1, scope, program, asmgen)) } else -> throw AssemblyError("weird type") } diff --git a/examples/compilerperformance/perf.p8 b/examples/compilerperformance/perf.p8 index 8ccee0f55..61a2acfb0 100644 --- a/examples/compilerperformance/perf.p8 +++ b/examples/compilerperformance/perf.p8 @@ -406,14 +406,18 @@ galaxy { sub init(ubyte galaxynum) { number = 1 planet.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxynum-1 { nextgalaxy() } } sub nextgalaxy() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -587,7 +591,10 @@ galaxy { planet.species_kind = (planet.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet.goatsoup_seed[0] = lsb(seed[1]) + planet.goatsoup_seed[1] = msb(seed[1]) + planet.goatsoup_seed[2] = lsb(seed[2]) + planet.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf10.p8 b/examples/compilerperformance/perf10.p8 index e2da74771..0b0c57fe9 100644 --- a/examples/compilerperformance/perf10.p8 +++ b/examples/compilerperformance/perf10.p8 @@ -396,14 +396,18 @@ galaxy10 { sub init(ubyte galaxy10num) { number = 1 planet10.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy10num-1 { nextgalaxy10() } } sub nextgalaxy10() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -576,8 +580,10 @@ galaxy10 { planet10.species_look = (seed2_msb ^ msb(seed[1])) & 7 ;bits 0-2 of (w0_hi EOR w1_hi) planet10.species_kind = (planet10.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - - planet10.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet10.goatsoup_seed[0] = lsb(seed[1]) + planet10.goatsoup_seed[1] = msb(seed[1]) + planet10.goatsoup_seed[2] = lsb(seed[2]) + planet10.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf2.p8 b/examples/compilerperformance/perf2.p8 index f4c7754d7..ae5040e4e 100644 --- a/examples/compilerperformance/perf2.p8 +++ b/examples/compilerperformance/perf2.p8 @@ -396,14 +396,18 @@ galaxy2 { sub init(ubyte galaxy2num) { number = 1 planet2.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy2num-1 { nextgalaxy2() } } sub nextgalaxy2() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy2 { planet2.species_kind = (planet2.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet2.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet2.goatsoup_seed[0] = lsb(seed[1]) + planet2.goatsoup_seed[1] = msb(seed[1]) + planet2.goatsoup_seed[2] = lsb(seed[2]) + planet2.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf3.p8 b/examples/compilerperformance/perf3.p8 index 1d4de1996..233cd8136 100644 --- a/examples/compilerperformance/perf3.p8 +++ b/examples/compilerperformance/perf3.p8 @@ -396,14 +396,18 @@ galaxy3 { sub init(ubyte galaxy3num) { number = 1 planet3.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy3num-1 { nextgalaxy3() } } sub nextgalaxy3() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy3 { planet3.species_kind = (planet3.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet3.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet3.goatsoup_seed[0] = lsb(seed[1]) + planet3.goatsoup_seed[1] = msb(seed[1]) + planet3.goatsoup_seed[2] = lsb(seed[2]) + planet3.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf4.p8 b/examples/compilerperformance/perf4.p8 index e1f3e20d6..649a1c26d 100644 --- a/examples/compilerperformance/perf4.p8 +++ b/examples/compilerperformance/perf4.p8 @@ -396,14 +396,18 @@ galaxy4 { sub init(ubyte galaxy4num) { number = 1 planet4.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy4num-1 { nextgalaxy4() } } sub nextgalaxy4() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy4 { planet4.species_kind = (planet4.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet4.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet4.goatsoup_seed[0] = lsb(seed[1]) + planet4.goatsoup_seed[1] = msb(seed[1]) + planet4.goatsoup_seed[2] = lsb(seed[2]) + planet4.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf5.p8 b/examples/compilerperformance/perf5.p8 index 24d232f96..8ced7fe7c 100644 --- a/examples/compilerperformance/perf5.p8 +++ b/examples/compilerperformance/perf5.p8 @@ -396,14 +396,18 @@ galaxy5 { sub init(ubyte galaxy5num) { number = 1 planet5.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy5num-1 { nextgalaxy5() } } sub nextgalaxy5() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy5 { planet5.species_kind = (planet5.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet5.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet5.goatsoup_seed[0] = lsb(seed[1]) + planet5.goatsoup_seed[1] = msb(seed[1]) + planet5.goatsoup_seed[2] = lsb(seed[2]) + planet5.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf6.p8 b/examples/compilerperformance/perf6.p8 index 6c36587ad..056d37c12 100644 --- a/examples/compilerperformance/perf6.p8 +++ b/examples/compilerperformance/perf6.p8 @@ -396,14 +396,18 @@ galaxy6 { sub init(ubyte galaxy6num) { number = 1 planet6.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy6num-1 { nextgalaxy6() } } sub nextgalaxy6() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy6 { planet6.species_kind = (planet6.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet6.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet6.goatsoup_seed[0] = lsb(seed[1]) + planet6.goatsoup_seed[1] = msb(seed[1]) + planet6.goatsoup_seed[2] = lsb(seed[2]) + planet6.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf7.p8 b/examples/compilerperformance/perf7.p8 index 0dd9c5a9e..5de18024b 100644 --- a/examples/compilerperformance/perf7.p8 +++ b/examples/compilerperformance/perf7.p8 @@ -396,14 +396,18 @@ galaxy7 { sub init(ubyte galaxy7num) { number = 1 planet7.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy7num-1 { nextgalaxy7() } } sub nextgalaxy7() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy7 { planet7.species_kind = (planet7.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet7.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet7.goatsoup_seed[0] = lsb(seed[1]) + planet7.goatsoup_seed[1] = msb(seed[1]) + planet7.goatsoup_seed[2] = lsb(seed[2]) + planet7.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf8.p8 b/examples/compilerperformance/perf8.p8 index 26ad74a98..911952457 100644 --- a/examples/compilerperformance/perf8.p8 +++ b/examples/compilerperformance/perf8.p8 @@ -396,14 +396,18 @@ galaxy8 { sub init(ubyte galaxy8num) { number = 1 planet8.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy8num-1 { nextgalaxy8() } } sub nextgalaxy8() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -577,7 +581,10 @@ galaxy8 { planet8.species_kind = (planet8.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - planet8.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet8.goatsoup_seed[0] = lsb(seed[1]) + planet8.goatsoup_seed[1] = msb(seed[1]) + planet8.goatsoup_seed[2] = lsb(seed[2]) + planet8.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/compilerperformance/perf9.p8 b/examples/compilerperformance/perf9.p8 index cb68b7a66..0f12870de 100644 --- a/examples/compilerperformance/perf9.p8 +++ b/examples/compilerperformance/perf9.p8 @@ -396,14 +396,18 @@ galaxy9 { sub init(ubyte galaxy9num) { number = 1 planet9.number = 255 - seed = [base0, base1, base2] + seed[0] = base0 + seed[1] = base1 + seed[2] = base2 repeat galaxy9num-1 { nextgalaxy9() } } sub nextgalaxy9() { - seed = [twist(seed[0]), twist(seed[1]), twist(seed[2])] + seed[0] = twist(seed[0]) + seed[1] = twist(seed[1]) + seed[2] = twist(seed[2]) number++ if number==9 number = 1 @@ -576,8 +580,10 @@ galaxy9 { planet9.species_look = (seed2_msb ^ msb(seed[1])) & 7 ;bits 0-2 of (w0_hi EOR w1_hi) planet9.species_kind = (planet9.species_look + (seed2_msb & 3)) & 7 ;Add bits 0-1 of w2_hi to A from previous step, and take bits 0-2 of the result } - - planet9.goatsoup_seed = [lsb(seed[1]), msb(seed[1]), lsb(seed[2]), seed2_msb] + planet9.goatsoup_seed[0] = lsb(seed[1]) + planet9.goatsoup_seed[1] = msb(seed[1]) + planet9.goatsoup_seed[2] = lsb(seed[2]) + planet9.goatsoup_seed[3] = seed2_msb } sub tweakseed() { diff --git a/examples/test.p8 b/examples/test.p8 index 97303d376..e12961ae4 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,61 +4,6 @@ main { sub start() { - ubyte[] arr1 = [1,2,3] - ubyte[] arr2 = [9,9,9] - - arr1[2]=42 - txt.print_ub(arr2[0]) - txt.chrout(',') - txt.print_ub(arr2[1]) - txt.chrout(',') - txt.print_ub(arr2[2]) - txt.nl() - arr2=[99,88,77] - txt.print_ub(arr2[0]) - txt.chrout(',') - txt.print_ub(arr2[1]) - txt.chrout(',') - txt.print_ub(arr2[2]) - txt.nl() - arr2=arr1 - txt.print_ub(arr2[0]) - txt.chrout(',') - txt.print_ub(arr2[1]) - txt.chrout(',') - txt.print_ub(arr2[2]) - txt.nl() - txt.nl() - - - struct MyType { - uword v1 - uword w1 - uword w2 - } - - MyType m1 = [1, 888, 999] - MyType m2 = [22, 222, 222] - - txt.print_uw(m2.v1) - txt.chrout(',') - txt.print_uw(m2.w1) - txt.chrout(',') - txt.print_uw(m2.w2) - txt.nl() - m2 = [111,222,333] - txt.print_uw(m2.v1) - txt.chrout(',') - txt.print_uw(m2.w1) - txt.chrout(',') - txt.print_uw(m2.w2) - txt.nl() - m2 = m1 - txt.print_uw(m2.v1) - txt.chrout(',') - txt.print_uw(m2.w1) - txt.chrout(',') - txt.print_uw(m2.w2) - txt.nl() + txt.print("hello") } }