From def06dbc0b844792ab0253e9753a71f1a2258df0 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 18 Sep 2020 22:10:20 +0200 Subject: [PATCH] allow address-of to be used as a value for a memory pointer variable --- compiler/src/prog8/ast/processing/AstChecker.kt | 8 +++++--- compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index a4c58bb1f..810e93d78 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -584,13 +584,15 @@ internal class AstChecker(private val program: Program, } } - if(decl.value !is NumericLiteralValue) { - err("value of memory var decl is not a numeric literal (it is a ${decl.value!!.javaClass.simpleName}).", decl.value?.position) - } else { + if(decl.value is NumericLiteralValue) { val value = decl.value as NumericLiteralValue if (value.type !in IntegerDatatypes || value.number.toInt() < 0 || value.number.toInt() > 65535) { err("memory address must be valid integer 0..\$ffff", decl.value?.position) } + } else if(decl.value is AddressOf) { + // we allow this too: the address of another variable + } else { + err("value of memory var decl is invalid type.", decl.value?.position) } } } diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index 2ad774a2d..8bbcfa30e 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -355,7 +355,10 @@ internal class AsmGen(private val program: Program, out("\n; memdefs and kernel subroutines") val memvars = statements.filterIsInstance().filter { it.type==VarDeclType.MEMORY || it.type==VarDeclType.CONST } for(m in memvars) { - out(" ${m.name} = ${(m.value as NumericLiteralValue).number.toHex()}") + if(m.value is NumericLiteralValue) + out(" ${m.name} = ${(m.value as NumericLiteralValue).number.toHex()}") + else + out(" ${m.name} = ${asmVariableName((m.value as AddressOf).identifier)}") } val asmSubs = statements.filterIsInstance().filter { it.isAsmSubroutine } for(sub in asmSubs) {