From 1d178080a3b73792261631214c85f7bbc7d8f8f0 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 23 Aug 2019 21:32:21 +0200 Subject: [PATCH] more bitshift asm --- compiler/res/prog8lib/prog8lib.asm | 35 +++++++++ compiler/src/prog8/compiler/Main.kt | 2 +- .../compiler/target/c64/AssemblyProgram.kt | 1 - .../c64/codegen/BuiltinFunctionsAsmGen.kt | 73 +++++++++++-------- examples/arithmetic/bitshift.p8 | 21 ++++-- 5 files changed, 93 insertions(+), 39 deletions(-) diff --git a/compiler/res/prog8lib/prog8lib.asm b/compiler/res/prog8lib/prog8lib.asm index a7fe56890..78cfd381e 100644 --- a/compiler/res/prog8lib/prog8lib.asm +++ b/compiler/res/prog8lib/prog8lib.asm @@ -1671,3 +1671,38 @@ _loop_hi sty c64.SCRATCH_ZPREG rts .pend + +ror2_mem_b .proc + ; -- in-place 8-bit ror of byte at memory location on stack + inx + lda c64.ESTACK_LO,x + sta _mod1+1 + sta _mod2+1 + lda c64.ESTACK_HI,x + sta _mod1+2 + sta _mod2+2 +_mod1 lda $ffff ; modified + lsr a + bcc _mod2 + ora #$80 +_mod2 sta $ffff ; modified + rts + .pend + +rol2_mem_b .proc + ; -- in-place 8-bit rol of byte at memory location on stack + ;" lda ${number.toHex()} | cmp #\$80 | rol a | sta ${number.toHex()}" + inx + lda c64.ESTACK_LO,x + sta _mod1+1 + sta _mod2+1 + lda c64.ESTACK_HI,x + sta _mod1+2 + sta _mod2+2 +_mod1 lda $ffff ; modified + cmp #$80 + rol a +_mod2 sta $ffff ; modified + rts + .pend + diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index b2378292e..48a83bdc6 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -97,7 +97,7 @@ fun compileProgram(filepath: Path, programAst.checkValid(compilerOptions) // check if final tree is valid programAst.checkRecursion() // check if there are recursive subroutine calls - // printAst(programAst) + printAst(programAst) if(writeAssembly) { // asm generation directly from the Ast, no need for intermediate code diff --git a/compiler/src/prog8/compiler/target/c64/AssemblyProgram.kt b/compiler/src/prog8/compiler/target/c64/AssemblyProgram.kt index 89ccc2b85..6c48197d1 100644 --- a/compiler/src/prog8/compiler/target/c64/AssemblyProgram.kt +++ b/compiler/src/prog8/compiler/target/c64/AssemblyProgram.kt @@ -44,7 +44,6 @@ class AssemblyProgram(val name: String, val outputDir: Path) { } } command.addAll(listOf("--output", outFile.toString(), assemblyFile.toString())) - println("ASM COMMAND: $command") // TODO val proc = ProcessBuilder(command).inheritIO().start() val result = proc.waitFor() diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 5c2e344f6..8320bc4d2 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -121,24 +121,6 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val translateFunctionArguments(fcall.arglist, func) asmgen.out(" jsr c64flt.func_$functionName") } -/* - TODO this was the old code for bit rotations: - Opcode.SHL_BYTE -> AsmFragment(" asl $variable+$index", 8) - Opcode.SHR_UBYTE -> AsmFragment(" lsr $variable+$index", 8) - Opcode.SHR_SBYTE -> AsmFragment(" lda $variable+$index | asl a | ror $variable+$index") - Opcode.SHL_WORD -> AsmFragment(" asl $variable+${index * 2 + 1} | rol $variable+${index * 2}", 8) - Opcode.SHR_UWORD -> AsmFragment(" lsr $variable+${index * 2 + 1} | ror $variable+${index * 2}", 8) - Opcode.SHR_SWORD -> AsmFragment(" lda $variable+${index * 2 + 1} | asl a | ror $variable+${index * 2 + 1} | ror $variable+${index * 2}", 8) - Opcode.ROL_BYTE -> AsmFragment(" rol $variable+$index", 8) - Opcode.ROR_BYTE -> AsmFragment(" ror $variable+$index", 8) - Opcode.ROL_WORD -> AsmFragment(" rol $variable+${index * 2 + 1} | rol $variable+${index * 2}", 8) - Opcode.ROR_WORD -> AsmFragment(" ror $variable+${index * 2 + 1} | ror $variable+${index * 2}", 8) - Opcode.ROL2_BYTE -> AsmFragment(" lda $variable+$index | cmp #\$80 | rol $variable+$index", 8) - Opcode.ROR2_BYTE -> AsmFragment(" lda $variable+$index | lsr a | bcc + | ora #\$80 |+ | sta $variable+$index", 10) - Opcode.ROL2_WORD -> AsmFragment(" asl $variable+${index * 2 + 1} | rol $variable+${index * 2} | bcc + | inc $variable+${index * 2 + 1} |+", 20) - Opcode.ROR2_WORD -> AsmFragment(" lsr $variable+${index * 2 + 1} | ror $variable+${index * 2} | bcc + | lda $variable+${index * 2 + 1} | ora #\$80 | sta $variable+${index * 2 + 1} |+", 30) - - */ "lsl" -> { // in-place val what = fcall.arglist.single() @@ -159,12 +141,18 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val number = (what.addressExpression as NumericLiteralValue).number asmgen.out(" asl ${number.toHex()}") } else { - TODO("lsl memory byte $what") + asmgen.translateExpression(what.addressExpression) + asmgen.out(""" + inx + lda $ESTACK_LO_HEX,x + sta (+) + 1 + lda $ESTACK_HI_HEX,x + sta (+) + 2 ++ asl 0 ; modified + """) } } - is ArrayIndexedExpression -> { - TODO("lsl byte array $what") - } + is ArrayIndexedExpression -> TODO("lsl byte array $what") else -> throw AssemblyError("weird type") } } @@ -201,12 +189,18 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val number = (what.addressExpression as NumericLiteralValue).number asmgen.out(" lsr ${number.toHex()}") } else { - TODO("lsr memory byte $what") + asmgen.translateExpression(what.addressExpression) + asmgen.out(""" + inx + lda $ESTACK_LO_HEX,x + sta (+) + 1 + lda $ESTACK_HI_HEX,x + sta (+) + 2 ++ lsr 0 ; modified + """) } } - is ArrayIndexedExpression -> { - TODO("lsr byte array $what") - } + is ArrayIndexedExpression -> TODO("lsr byte array $what") else -> throw AssemblyError("weird type") } } @@ -256,7 +250,15 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val number = (what.addressExpression as NumericLiteralValue).number asmgen.out(" rol ${number.toHex()}") } else { - TODO("rol memory byte $what") + asmgen.translateExpression(what.addressExpression) + asmgen.out(""" + inx + lda $ESTACK_LO_HEX,x + sta (+) + 1 + lda $ESTACK_HI_HEX,x + sta (+) + 2 ++ rol 0 ; modified + """) } } is RegisterExpr -> { @@ -299,7 +301,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val number = (what.addressExpression as NumericLiteralValue).number asmgen.out(" lda ${number.toHex()} | cmp #\$80 | rol a | sta ${number.toHex()}") } else { - TODO("rol2 memory byte $what") + asmgen.translateExpression(what.addressExpression) + asmgen.out(" jsr prog8_lib.rol2_mem_b") } } is RegisterExpr -> { @@ -342,8 +345,15 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val number = (what.addressExpression as NumericLiteralValue).number asmgen.out(" ror ${number.toHex()}") } else { - TODO("ror memory byte $what") - } + asmgen.translateExpression(what.addressExpression) + asmgen.out(""" + inx + lda $ESTACK_LO_HEX,x + sta (+) + 1 + lda $ESTACK_HI_HEX,x + sta (+) + 2 ++ ror 0 ; modified + """) } } is RegisterExpr -> { when(what.register) { @@ -385,7 +395,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val val number = (what.addressExpression as NumericLiteralValue).number asmgen.out(" lda ${number.toHex()} | lsr a | bcc + | ora #\$80 |+ | sta ${number.toHex()}") } else { - TODO("ror2 memory byte $what") + asmgen.translateExpression(what.addressExpression) + asmgen.out(" jsr prog8_lib.ror2_mem_b") } } is RegisterExpr -> { diff --git a/examples/arithmetic/bitshift.p8 b/examples/arithmetic/bitshift.p8 index fff9b1fae..2ed032903 100644 --- a/examples/arithmetic/bitshift.p8 +++ b/examples/arithmetic/bitshift.p8 @@ -15,12 +15,12 @@ main { byte[10] bbarray sub start() { - lsr(A) - lsl(A) - ror(A) - rol(A) - ror2(A) - rol2(A) +; lsr(A) +; lsl(A) +; ror(A) +; rol(A) +; ror2(A) +; rol2(A) lsr(bb) lsl(bb) @@ -37,12 +37,21 @@ main { rol(memword) ror2(memword) rol2(memword) + lsl(@(9999)) lsr(@(9999)) ror(@(9999)) rol(@(9999)) ror2(@(9999)) rol2(@(9999)) + + lsl(@(9999+A)) + lsr(@(9999+A)) + ror(@(9999+A)) + rol(@(9999+A)) + ror2(@(9999+A)) + rol2(@(9999+A)) + lsr(ubarray[1]) lsl(ubarray[1]) ror(ubarray[1])