From bcedb5f5e3c339a0bbbc8472807fa9bb392cc93e Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 17 Oct 2018 18:11:36 +0200 Subject: [PATCH] fix some asm --- compiler/examples/test.p8 | 2 +- .../src/prog8/compiler/target/c64/AsmGen.kt | 83 +++++++++++++++---- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index bde920a8f..59a8a8f8c 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -20,7 +20,7 @@ sub start() { A++ X++ - ;AY++ + AY++ A = ~X A = not Y diff --git a/compiler/src/prog8/compiler/target/c64/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/AsmGen.kt index 1fc60c31b..34fdc2d6e 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmGen.kt @@ -311,6 +311,10 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, out("\tdex") } + private fun replaceByteA() { + out("\tsta ${(ESTACK_LO+1).toHex()},x") + } + private fun pushMemByte(address: Int) { out("\tlda ${address.toHex()}") out("\tsta ${ESTACK_LO.toHex()},x") @@ -338,6 +342,12 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, out("\tdex") } + private fun replaceWordAY() { + out("\tsta ${(ESTACK_LO+1).toHex()},x") + out("\ttya") + out("\tsta ${(ESTACK_HI+1).toHex()},x") + } + private fun pushMemWord(address: Int) { out("\tlda ${address.toHex()}") out("\tsta ${ESTACK_LO.toHex()},x") @@ -373,16 +383,40 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, } private fun popByteA() { + // for operations that remove a value from the stack out("\tinx") out("\tlda ${ESTACK_LO.toHex()},x") } + private fun peekByteA() { + // for operations that modify a value on the stack + out("\tlda ${(ESTACK_LO+1).toHex()},x") + } + + private fun peekByte2A() { + // for operations that modify a value on the stack, 1 under the top + out("\tlda ${(ESTACK_LO+2).toHex()},x") + } + private fun popWordAY() { + // for operations that remove a value from the stack out("\ninx") out("\tlda ${ESTACK_LO.toHex()},x") out("\tldy ${ESTACK_HI.toHex()},x") } + private fun peekWordAY() { + // for operations that modify a value on the stack + out("\tlda ${(ESTACK_LO+1).toHex()},x") + out("\tldy ${(ESTACK_HI+1).toHex()},x") + } + + private fun peekWord2AY() { + // for operations that modify a value on the stack, 1 under the top + out("\tlda ${(ESTACK_LO+2).toHex()},x") + out("\tldy ${(ESTACK_HI+2).toHex()},x") + } + private fun instr2asm(insIdx: Int, ins: Instruction, block: IntermediateProgram.ProgramBlock): Int { if(ins is LabelInstr) { if(ins.name==block.shortname) @@ -676,16 +710,16 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, out("\tdec ${ins.callLabel}") } Opcode.ADD_UB, Opcode.ADD_B -> { - out("\tlda ${(ESTACK_LO+2).toHex()},x") + peekByte2A() out("\tclc\n\tadc ${(ESTACK_LO+1).toHex()},x") - out("\tsta ${(ESTACK_LO+2).toHex()},x") out("\tinx") + replaceByteA() } Opcode.SUB_UB, Opcode.SUB_B -> { - out("\tlda ${(ESTACK_LO+2).toHex()},x") + peekByte2A() out("\tsec\n\tsbc ${(ESTACK_LO+1).toHex()},x") - out("\tsta ${(ESTACK_LO+2).toHex()},x") out("\tinx") + replaceByteA() } Opcode.POP_MEM_UB, Opcode.POP_MEM_B -> { popByteA() @@ -703,25 +737,42 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, out("\tsty ${ins.callLabel}+1") } Opcode.NEG_B -> { - popByteA() + peekByteA() out("\teor #\$ff") out("\tsec\n\tadc #0") - pushByteA() + replaceByteA() } - Opcode.INV_BYTE, Opcode.NOT_BYTE -> { - popByteA() + Opcode.INV_BYTE -> { + peekByteA() out("\teor #\$ff") - pushByteA() + replaceByteA() } - Opcode.INV_WORD, Opcode.NOT_WORD -> { - popWordAY() + Opcode.INV_WORD -> { + out("\tlda ${(ESTACK_LO+1).toHex()},x") out("\teor #\$ff") - out("\tpha") - out("\ttya") + out("\tlda ${(ESTACK_HI+1).toHex()},x") out("\teor #\$ff") - out("\ttay") - out("\tpla") - pushWordAY() + out("\tsta ${(ESTACK_LO+1).toHex()},x") + out("\tsta ${(ESTACK_HI+1).toHex()},x") + } + Opcode.NOT_BYTE -> { + peekByteA() + out("\tbeq +") + out("\tlda #0") + out("\tbeq ++") + out("+\tlda #1") + out("++") + replaceByteA() + } + Opcode.NOT_WORD -> { + out("\tlda ${(ESTACK_LO+1).toHex()},x") + out("\tora ${(ESTACK_HI+1).toHex()},x") + out("\tbeq +") + out("\tlda #0") + out("\tbeq ++") + out("+\tlda #1") + out("++\tsta ${(ESTACK_LO+1).toHex()},x") + out("\tsta ${(ESTACK_HI+1).toHex()},x") } else-> TODO("asm for $ins")