From 8dcd49934ab2d1b37192bb24e925b65d1ba5320c Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 10 Dec 2020 23:30:58 +0100 Subject: [PATCH] added progend() builtin function --- .../prog8/compiler/target/c64/codegen/AsmGen.kt | 1 + .../target/c64/codegen/BuiltinFunctionsAsmGen.kt | 16 ++++++++++++---- compiler/src/prog8/functions/BuiltinFunctions.kt | 1 + docs/source/programming.rst | 4 ++++ docs/source/todo.rst | 2 -- examples/test.p8 | 7 ++++++- syntax-files/IDEA/Prog8.xml | 2 +- syntax-files/IDEA/readme.txt | 3 +++ 8 files changed, 28 insertions(+), 8 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 37913d266..8ed60e808 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -160,6 +160,7 @@ internal class AsmGen(private val program: Program, val floatvalue = flt.key out("${flt.value}\t.byte $floatFill ; float $floatvalue") } + out("prog8_program_end\t; end of program label for progend()") } private fun block2asm(block: Block) { diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index d5a9c20b0..4f05dae82 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -5,10 +5,7 @@ import prog8.ast.Node import prog8.ast.Program import prog8.ast.base.* import prog8.ast.expressions.* -import prog8.ast.statements.ArrayIndex -import prog8.ast.statements.DirectMemoryWrite -import prog8.ast.statements.FunctionCallStatement -import prog8.ast.statements.Subroutine +import prog8.ast.statements.* import prog8.compiler.AssemblyError import prog8.compiler.target.CompilationTarget import prog8.compiler.target.Cx16Target @@ -93,6 +90,17 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val translateArguments(fcall.args, func, scope) asmgen.out(" jmp prog8_lib.func_exit") } + "progend" -> { + if(resultToStack) + asmgen.out(""" + lda #prog8_program_end + sta P8ESTACK_HI,x + dex""") + else + asmgen.out(" lda #prog8_program_end") + } else -> TODO("missing asmgen for builtin func ${func.name}") } } diff --git a/compiler/src/prog8/functions/BuiltinFunctions.kt b/compiler/src/prog8/functions/BuiltinFunctions.kt index 58cdd63db..2a48755a6 100644 --- a/compiler/src/prog8/functions/BuiltinFunctions.kt +++ b/compiler/src/prog8/functions/BuiltinFunctions.kt @@ -141,6 +141,7 @@ private val functionSignatures: List = listOf( FSignature("set_irqd" , false, emptyList(), null), FSignature("clear_irqd" , false, emptyList(), null), FSignature("read_flags" , false, emptyList(), DataType.UBYTE), + FSignature("progend" , true, emptyList(), DataType.UWORD), FSignature("swap" , false, listOf(FParam("first", NumericDatatypes), FParam("second", NumericDatatypes)), null), FSignature("memcopy" , false, listOf( FParam("from", IterableDatatypes + DataType.UWORD), diff --git a/docs/source/programming.rst b/docs/source/programming.rst index e4227bd66..370e93086 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -894,6 +894,10 @@ set_irqd() / clear_irqd() swap(x, y) Swap the values of numerical variables (or memory locations) x and y in a fast way. +progend() + Returns the last address of the program in memory + 1. + Can be used to load dynamic data after the program, instead of hardcoding something. + Library routines ---------------- diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 34da46bf6..69ce21186 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,8 +2,6 @@ TODO ==== -- add minv(a,b) and maxv(a,b) functions to determine the max or min of 2 values -- add progend() builtin function that returns the last address of the program in memory + 1 (to be able to stick dynamic data after the program easily) - see if we can group some errors together for instance the (now single) errors about unidentified symbols - Cx16 target: support full-screen 640x480 and 320x240 graphics? That requires our own custom graphics routines though to draw lines. - hoist all variable declarations up to the subroutine scope *before* even the constant folding takes place (to avoid undefined symbol errors when referring to a variable from another nested scope in the subroutine) diff --git a/examples/test.p8 b/examples/test.p8 index eb0c5e457..e01be7307 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,13 +1,18 @@ %import textio %import diskio %import floats +%import graphics %zeropage basicsafe %import test_stack %option no_sysinit main { - sub start() { + sub start () { + uword xx = progend() + txt.print_uwhex(xx, 1) + txt.print_uwhex(progend(), 1) test_stack.test() } + } diff --git a/syntax-files/IDEA/Prog8.xml b/syntax-files/IDEA/Prog8.xml index aa62d205b..dd2011573 100644 --- a/syntax-files/IDEA/Prog8.xml +++ b/syntax-files/IDEA/Prog8.xml @@ -14,7 +14,7 @@ - + diff --git a/syntax-files/IDEA/readme.txt b/syntax-files/IDEA/readme.txt index 6738952ba..3ca8f5f0f 100644 --- a/syntax-files/IDEA/readme.txt +++ b/syntax-files/IDEA/readme.txt @@ -6,3 +6,6 @@ The exact path may vary with the version of the IDE, but for me it is currently this on Linux: $HOME/.config/JetBrains/IntelliJIdea2020.2/filetypes/ + + +(note the version number in the path, adjust accordingly)