fix 6502 inplace pointer variable assignment

This commit is contained in:
Irmen de Jong
2025-08-14 20:38:18 +02:00
parent fd9bd23449
commit e800c165f9
5 changed files with 30 additions and 21 deletions

View File

@@ -1440,7 +1440,7 @@ $repeatLabel""")
if(params!=null) { if(params!=null) {
for(param in params) { for(param in params) {
param as PtSubroutineParameter param as PtSubroutineParameter
if(param.name==name) if(param.scopedName==name)
return param return param
} }
} }

View File

@@ -55,6 +55,10 @@ internal class AsmAssignTarget(val kind: TargetStorageKind,
require(register!=null) require(register!=null)
else else
require(register==null) require(register==null)
if(kind==TargetStorageKind.POINTER)
require(pointer!=null)
if(pointer!=null)
require(kind==TargetStorageKind.POINTER)
} }
companion object { companion object {

View File

@@ -85,22 +85,23 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
} }
} }
} }
target.datatype.isWord -> { target.datatype.isWord || target.datatype.isPointer -> {
val block = target.origAstTarget?.definingBlock() val block = target.origAstTarget?.definingBlock()
val targetDt = if(target.datatype.isWord) target.datatype else DataType.UWORD // pointers themselves that get a new value are just treated as UWORD variables
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALBOOLEAN -> inplacemodificationWordWithLiteralval(target.asmVarname, target.datatype, operator, value.boolean!!.asInt(), block) SourceStorageKind.LITERALBOOLEAN -> inplacemodificationWordWithLiteralval(target.asmVarname, targetDt, operator, value.boolean!!.asInt(), block)
SourceStorageKind.LITERALNUMBER -> inplacemodificationWordWithLiteralval(target.asmVarname, target.datatype, operator, value.number!!.number.toInt(), block) SourceStorageKind.LITERALNUMBER -> inplacemodificationWordWithLiteralval(target.asmVarname, targetDt, operator, value.number!!.number.toInt(), block)
SourceStorageKind.VARIABLE -> inplacemodificationWordWithVariable(target.asmVarname, target.datatype, operator, value.asmVarname, value.datatype, block) SourceStorageKind.VARIABLE -> inplacemodificationWordWithVariable(target.asmVarname, targetDt, operator, value.asmVarname, value.datatype, block)
SourceStorageKind.REGISTER -> inplacemodificationWordWithVariable(target.asmVarname, target.datatype, operator, regName(value), value.datatype, block) SourceStorageKind.REGISTER -> inplacemodificationWordWithVariable(target.asmVarname, targetDt, operator, regName(value), value.datatype, block)
SourceStorageKind.MEMORY -> inplacemodificationWordWithMemread(target.asmVarname, target.datatype, operator, value.memory!!) SourceStorageKind.MEMORY -> inplacemodificationWordWithMemread(target.asmVarname, targetDt, operator, value.memory!!)
SourceStorageKind.ARRAY -> inplacemodificationWordWithValue(target.asmVarname, target.datatype, operator, value.array!!, block) SourceStorageKind.ARRAY -> inplacemodificationWordWithValue(target.asmVarname, targetDt, operator, value.array!!, block)
SourceStorageKind.EXPRESSION -> { SourceStorageKind.EXPRESSION -> {
if(value.expression is PtTypeCast) { if(value.expression is PtTypeCast) {
if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) return if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) return
inplacemodificationWordWithValue(target.asmVarname, target.datatype, operator, value.expression, block) inplacemodificationWordWithValue(target.asmVarname, targetDt, operator, value.expression, block)
} }
else { else {
inplacemodificationWordWithValue(target.asmVarname, target.datatype, operator, value.expression!!, block) inplacemodificationWordWithValue(target.asmVarname, targetDt, operator, value.expression!!, block)
} }
} }
} }
@@ -123,8 +124,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
} }
} }
} }
target.datatype.isPointer ->
ptrgen.inplaceModification(PtrTarget(target), operator, value)
else -> throw AssemblyError("weird type to do in-place modification on ${target.datatype}") else -> throw AssemblyError("weird type to do in-place modification on ${target.datatype}")
} }
} }
@@ -309,7 +308,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
} }
} }
} }
target.datatype.isPointer -> ptrgen.inplaceModification(PtrTarget(target), operator, value) target.datatype.isPointer -> TODO("inplace modification of pointer array ${target.position}")
else -> throw AssemblyError("weird type to do in-place modification on ${target.datatype}") else -> throw AssemblyError("weird type to do in-place modification on ${target.datatype}")
} }
} }

View File

@@ -1390,6 +1390,7 @@ main {
} }
sub test(str argument) -> str { sub test(str argument) -> str {
argument++
return "bye" return "bye"
} }
}""" }"""
@@ -1397,11 +1398,21 @@ main {
val vmtest = vmprg.codegenAst!!.allBlocks().first { it.name == "main" }.children[1] as PtSub val vmtest = vmprg.codegenAst!!.allBlocks().first { it.name == "main" }.children[1] as PtSub
vmtest.signature.returns.single() shouldBe DataType.pointer(BaseDataType.UBYTE) vmtest.signature.returns.single() shouldBe DataType.pointer(BaseDataType.UBYTE)
(vmtest.signature.children.single() as PtSubroutineParameter).type shouldBe DataType.pointer(BaseDataType.UBYTE) (vmtest.signature.children.single() as PtSubroutineParameter).type shouldBe DataType.pointer(BaseDataType.UBYTE)
val vminc = vmtest.children[2] as PtAugmentedAssign
vminc.operator shouldBe "+="
vminc.target.identifier!!.name shouldBe "main.test.argument"
vminc.target.identifier!!.type shouldBe DataType.pointer(BaseDataType.UBYTE)
(vminc.value as PtNumber).number shouldBe 1.0
val c64prg = compileText(C64Target(), false, src, outputDir)!! val c64prg = compileText(C64Target(), false, src, outputDir)!!
val c64test = c64prg.codegenAst!!.allBlocks().first { it.name == "p8b_main" }.children[1] as PtSub val c64test = c64prg.codegenAst!!.allBlocks().first { it.name == "p8b_main" }.children[1] as PtSub
c64test.signature.returns.single() shouldBe DataType.pointer(BaseDataType.UBYTE) c64test.signature.returns.single() shouldBe DataType.pointer(BaseDataType.UBYTE)
(c64test.signature.children.single() as PtSubroutineParameter).type shouldBe DataType.pointer(BaseDataType.UBYTE) (c64test.signature.children.single() as PtSubroutineParameter).type shouldBe DataType.pointer(BaseDataType.UBYTE)
val c64inc = c64test.children[2] as PtAugmentedAssign
c64inc.operator shouldBe "+="
c64inc.target.identifier!!.name shouldBe "p8b_main.p8s_test.p8v_argument"
c64inc.target.identifier!!.type shouldBe DataType.pointer(BaseDataType.UBYTE)
(c64inc.value as PtNumber).number shouldBe 1.0
} }
test("hoist variable decl and initializer correctly in case of pointer type variable as well") { test("hoist variable decl and initializer correctly in case of pointer type variable as well") {

View File

@@ -1,14 +1,9 @@
main { main {
sub start() { sub start() {
console_write("sdf")
label:
uword @shared addr
addr = label
addr = thing
addr = &label
addr = &thing
} }
sub thing() { sub console_write(^^ubyte isoString) {
isoString++
} }
} }