From a6107fcfdfc2bc1d9d5f50dc3c5875a19bcbc659 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 29 Sep 2024 23:53:39 +0200 Subject: [PATCH] taking the address of a romsub is now the constant value of said romsub's declared address --- .../prog8/ast/expressions/AstExpressions.kt | 36 ++++++++------- docs/source/todo.rst | 3 +- examples/test.p8 | 44 +++++++++++-------- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 327c68efd..92a96ba49 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -419,25 +419,31 @@ data class AddressOf(var identifier: IdentifierReference, var arrayIndex: ArrayI override fun copy() = AddressOf(identifier.copy(), arrayIndex?.copy(), position) override fun constValue(program: Program): NumericLiteral? { - val target = this.identifier.targetStatement(program) as? VarDecl - if(target?.type==VarDeclType.MEMORY || target?.type==VarDeclType.CONST) { - var address = target.value?.constValue(program)?.number - if(address!=null) { - if(arrayIndex!=null) { - val index = arrayIndex?.constIndex() - if (index != null) { - address += when (target.datatype) { - DataType.UWORD -> index - in ArrayDatatypes -> program.memsizer.memorySize(target.datatype, index) - else -> throw FatalAstException("need array or uword ptr") - } + val target = this.identifier.targetStatement(program) + val targetVar = target as? VarDecl + if(targetVar!=null) { + if (targetVar.type == VarDeclType.MEMORY || targetVar.type == VarDeclType.CONST) { + var address = targetVar.value?.constValue(program)?.number + if (address != null) { + if (arrayIndex != null) { + val index = arrayIndex?.constIndex() + if (index != null) { + address += when (targetVar.datatype) { + DataType.UWORD -> index + in ArrayDatatypes -> program.memsizer.memorySize(targetVar.datatype, index) + else -> throw FatalAstException("need array or uword ptr") + } + } else + return null } - else - return null + return NumericLiteral(DataType.UWORD, address, position) } - return NumericLiteral(DataType.UWORD, address, position) } } + val targetSub = target as? Subroutine + if(targetSub?.asmAddress!=null) { + return NumericLiteral(DataType.UWORD, targetSub.asmAddress.toDouble(), position) + } return null } override fun referencesIdentifier(nameInSource: List) = identifier.nameInSource==nameInSource || arrayIndex?.referencesIdentifier(nameInSource)==true diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 66e952dce..30fc7523b 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -5,8 +5,7 @@ Regenerate skeleton doc files. "invalid number of arguments" -> print the list of missing arguments -callfar() should allow setting an argument in the X register as well? + similar optimizations that call() got. - +callfar() should allow setting an argument in the X register as well? Add a new SublimeText syntax file for prog8, and also install this for bat: https://github.com/sharkdp/bat?tab=readme-ov-file#adding-new-syntaxes--language-definitions diff --git a/examples/test.p8 b/examples/test.p8 index 2bcf93787..5ad16997f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -8,35 +8,41 @@ main { sub start() { test_stack.test() - example(function1, 42) - example(function1, 99) - example(function2, 42) - example(function2, 99) + + txt.print_uwhex(cbm.CHROUT, true) + txt.print_uwhex(&cbm.CHROUT, true) + txt.nl() + + cx16.r0 = &function1 + callfar(0, $ffd2, $0031) + callfar(0, cbm.CHROUT, $000d) + callfar(0, function1, $6660) + callfar(0, cx16.r0, $ffff) + cx16.r0 -=10 + callfar(0, cx16.r0+10, $eeee) + + cx16.r0 = &function2 + callfar(0, $ffd2, $0032) + callfar(0, cbm.CHROUT, $000d) + callfar(0, function2, $6660) + callfar(0, cx16.r0, $ffff) + cx16.r0 -=10 + callfar(0, cx16.r0+10, $eeee) + test_stack.test() cx16.r0++ - sub function1(ubyte arg) { + sub function1(uword arg) { txt.print("function 1 arg=") - txt.print_ub(arg) + txt.print_uwhex(arg, false) txt.nl() } - sub function2(ubyte arg) { + sub function2(uword arg) { txt.print("function 2 arg=") - txt.print_ub(arg) + txt.print_uwhex(arg, false) txt.nl() } - - sub example(uword function, ubyte value) { - - %asm {{ - lda p8v_value - }} - - call(function) - cx16.r1 = function+10 - call(cx16.r1-10) - } } }