From e8e25c6fd60899eb96e4e5ed90d17aacd114f482 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 27 Nov 2021 18:09:15 +0100 Subject: [PATCH] added sys.push() and sys.pop() to put values on cpu stack. Added missing builtin functions to syntax-files. --- .../cpu6502/codegen/BuiltinFunctionsAsmGen.kt | 26 ++++++++++++ compiler/res/prog8lib/c64/syslib.p8 | 40 ++++++++++++++++--- compiler/res/prog8lib/cx16/syslib.p8 | 34 ++++++++++++++-- .../compilerinterface/BuiltinFunctions.kt | 4 ++ examples/test.p8 | 21 ++++++++-- syntax-files/IDEA/Prog8.xml | 4 +- syntax-files/NotepadPlusPlus/Prog8.xml | 2 +- syntax-files/Vim/prog8_builtins.vim | 6 +-- 8 files changed, 117 insertions(+), 20 deletions(-) diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt index c1ee8843c..cf5b8818d 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/BuiltinFunctionsAsmGen.kt @@ -67,6 +67,8 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val "peek" -> throw AssemblyError("peek() should have been replaced by @()") "pokew" -> funcPokeW(fcall) "poke" -> throw AssemblyError("poke() should have been replaced by @()") +// "push", "pushw" -> funcPush(fcall, func) +// "pop", "popw" -> funcPop(func) "cmp" -> funcCmp(fcall) "callfar" -> funcCallFar(fcall) "callrom" -> funcCallRom(fcall) @@ -74,6 +76,30 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val } } +// private fun funcPop(func: FSignature) { +// if(func.name=="pop") { +// asmgen.out(" pla") +// } else { +// if (asmgen.isTargetCpu(CpuType.CPU65c02)) +// asmgen.out(" ply | pla") +// else +// asmgen.out(" pla | tay | pla") +// } +// } +// +// private fun funcPush(fcall: IFunctionCall, func: FSignature) { +// if(func.name=="push") { +// asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.A) +// asmgen.out(" pha") +// } else { +// asmgen.assignExpressionToRegister(fcall.args[0], RegisterOrPair.AY) +// if (asmgen.isTargetCpu(CpuType.CPU65c02)) +// asmgen.out(" pha | phy") +// else +// asmgen.out(" pha | tya | pha") +// } +// } + private fun funcCallFar(fcall: IFunctionCall) { if(asmgen.options.compTarget !is Cx16Target) throw AssemblyError("callfar only works on cx16 target at this time") diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index cf02611a6..ecd97e743 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -627,32 +627,60 @@ _longcopy inline asmsub read_flags() -> ubyte @A { %asm {{ - php - pla + php + pla + }} + } + + inline asmsub push(ubyte @A) { + %asm {{ + pha + }} + } + + inline asmsub pop() -> ubyte @A { + %asm {{ + pla + }} + } + + inline asmsub pushw(uword @AY) { + %asm {{ + pha + tya + pha + }} + } + + inline asmsub popw() -> uword @AY { + %asm {{ + pla + tay + pla }} } inline asmsub clear_carry() { %asm {{ - clc + clc }} } inline asmsub set_carry() { %asm {{ - sec + sec }} } inline asmsub clear_irqd() { %asm {{ - cli + cli }} } inline asmsub set_irqd() { %asm {{ - sei + sei }} } diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index 8b697ede4..c59fe5852 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -887,27 +887,53 @@ sys { }} } + inline asmsub push(ubyte value @A) { + %asm {{ + pha + }} + } + + inline asmsub pop() -> ubyte @A { + %asm {{ + pla + }} + } + + inline asmsub pushw(uword value @AY) { + %asm {{ + pha + phy + }} + } + + inline asmsub popw() -> uword @AY { + %asm {{ + ply + pla + }} + } + inline asmsub clear_carry() { %asm {{ - clc + clc }} } inline asmsub set_carry() { %asm {{ - sec + sec }} } inline asmsub clear_irqd() { %asm {{ - cli + cli }} } inline asmsub set_irqd() { %asm {{ - sei + sei }} } diff --git a/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt b/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt index 4cd1c13d3..2f5f5d7c2 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/BuiltinFunctions.kt @@ -142,6 +142,10 @@ private val functionSignatures: List = listOf( FSignature("peekw" , true, listOf(FParam("address", arrayOf(DataType.UWORD))), DataType.UWORD), FSignature("poke" , false, listOf(FParam("address", arrayOf(DataType.UWORD)), FParam("value", arrayOf(DataType.UBYTE))), null), FSignature("pokew" , false, listOf(FParam("address", arrayOf(DataType.UWORD)), FParam("value", arrayOf(DataType.UWORD))), null), +// FSignature("pop" , false, emptyList(), DataType.UBYTE), +// FSignature("popw" , false, emptyList(), DataType.UWORD), +// FSignature("push" , false, listOf(FParam("value", ByteDatatypes)), null), +// FSignature("pushw" , false, listOf(FParam("value", WordDatatypes)), null), FSignature("rnd" , false, emptyList(), DataType.UBYTE), FSignature("rndw" , false, emptyList(), DataType.UWORD), FSignature("rndf" , false, emptyList(), DataType.FLOAT), diff --git a/examples/test.p8 b/examples/test.p8 index 37bcc5070..5c922340c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,13 +1,26 @@ +%import test_stack main { sub start() { - uword ww + test_stack.test() - main.routine2.num = ww+1 - main.routine2.switch=true + sys.push(-22 as ubyte) + sys.push(44) + sys.pushw(-11234 as uword) + sys.pushw(12345) + sys.push(1) + sys.push(2) + ubyte @shared ub = sys.pop() + byte @shared bb = sys.pop() as byte + uword @shared uw = sys.popw() + word @shared ww = sys.popw() as word + void sys.pop() + void sys.pop() - routine2(ww+1, true) + ; routine2(uw+1, true) + + test_stack.test() repeat { } diff --git a/syntax-files/IDEA/Prog8.xml b/syntax-files/IDEA/Prog8.xml index 6b7feb46e..7e612a05c 100644 --- a/syntax-files/IDEA/Prog8.xml +++ b/syntax-files/IDEA/Prog8.xml @@ -14,10 +14,10 @@ - + - + \ No newline at end of file diff --git a/syntax-files/NotepadPlusPlus/Prog8.xml b/syntax-files/NotepadPlusPlus/Prog8.xml index 317befee7..1383412e5 100644 --- a/syntax-files/NotepadPlusPlus/Prog8.xml +++ b/syntax-files/NotepadPlusPlus/Prog8.xml @@ -27,7 +27,7 @@ void const str byte ubyte word uword float zp shared %address %asm %asmbinary %asminclude %breakpoint %import %launcher %option %output %zeropage %zpreserved inline sub asmsub romsub clobbers asm if when else if_cc if_cs if_eq if_mi if_neg if_nz if_pl if_pos if_vc if_vs if_z for in step do while repeat break return goto - abs acos all any asin atan avg callfar callrom ceil cmp cos cos16 cos16u cos8 cos8u deg floor len ln log2 lsb lsl lsr max memory min mkword msb peek peekw poke pokew rad reverse rnd rndf rndw rol rol2 ror ror2 round sgn sin sin16 sin16u sin8 sin8u sizeof sort sqrt sqrt16 sum swap tan + abs acos all any asin atan avg callfar callrom ceil cmp cos cos16 cos16u cos8 cos8u cosr8 cosr8u cosr16 cosr16u deg floor len ln log2 lsb lsl lsr max memory min mkword msb peek peekw poke pokew rad reverse rnd rndf rndw rol rol2 ror ror2 round sgn sin sin16 sin16u sin8 sin8u sinr8 sinr8u sinr16 sinr16u sizeof sort sqrt sqrt16 sum swap tan true false not and or xor as to downto diff --git a/syntax-files/Vim/prog8_builtins.vim b/syntax-files/Vim/prog8_builtins.vim index 8c6b5fc0e..f541b1a3c 100644 --- a/syntax-files/Vim/prog8_builtins.vim +++ b/syntax-files/Vim/prog8_builtins.vim @@ -7,8 +7,8 @@ " Built-in functions " Math functions -syn keyword prog8BuiltInFunc abs atan ceil cos cos8u cos8 cos16u cos16 deg floor -syn keyword prog8BuiltInFunc ln log2 rad round sin sgn sin8u sin8 sin16u sin16 +syn keyword prog8BuiltInFunc abs atan ceil cos cos8u cos8 cos16u cos16 cosr8 cosr8u cosr16 cosr16u deg floor +syn keyword prog8BuiltInFunc ln log2 rad round sin sgn sin8u sin8 sin16u sin16 sinr8 sinr8u sinr16 sinr16u syn keyword prog8BuiltInFunc sqrt16 sqrt tan " Array functions @@ -16,7 +16,7 @@ syn keyword prog8BuiltInFunc any all len max min reverse sum sort " Miscellaneous functions syn keyword prog8BuiltInFunc cmp lsb msb mkword peek peekw poke pokew rnd rndw -syn keyword prog8BuiltInFunc rndf rol rol2 ror ror2 sizeof +syn keyword prog8BuiltInFunc rndf rol rol2 ror ror2 sizeof syn keyword prog8BuiltInFunc swap memory callfar callrom