fix subroutine-local struct types

This commit is contained in:
Irmen de Jong committed 2026-11-21 13:48:11 +01:00
1 parent c7147fe3ea
commit 32f82e9e16
5 files changed
+109 -22

No files matched your search

@@ -1767,6 +1767,26 @@ _after:
false, assignment.position)
return listOf(AstReplaceNode(assignment, memcopy, parent))
}
// named instance -> array element: arr[i] = a --> memcopy(&a, &arr[i], sizeof(struct))
val namedSourceToArrayTarget = assignment.target.arrayindexed
if(sourceIdentifier!=null && namedSourceToArrayTarget!=null) {
val target = AddressOf(namedSourceToArrayTarget.plainarrayvar!!.copy(), namedSourceToArrayTarget.indexer.copy(), null, false, true, assignment.position)
val source = AddressOf(sourceIdentifier.copy(), null, null, false, true, assignment.position)
val memcopy = FunctionCallStatement(IdentifierReference(listOf("sys", "memcopy"), assignment.position),
mutableListOf(source, target, structSizeNum),
false, assignment.position)
return listOf(AstReplaceNode(assignment, memcopy, parent))
}
// named instance -> pointer deref: ptr^^ = a --> memcopy(&a, ptr, sizeof(struct))
val namedSourceToPtrTarget = assignment.target.pointerDereference
if(sourceIdentifier!=null && namedSourceToPtrTarget!=null) {
val targetPtr = IdentifierReference(namedSourceToPtrTarget.chain, assignment.position)
val source = AddressOf(sourceIdentifier.copy(), null, null, false, true, assignment.position)
val memcopy = FunctionCallStatement(IdentifierReference(listOf("sys", "memcopy"), assignment.position),
mutableListOf(source, targetPtr, structSizeNum),
false, assignment.position)
return listOf(AstReplaceNode(assignment, memcopy, parent))
}
val deref = assignment.value as? PtrDereference
if(deref!=null) {
val sourcePtr = IdentifierReference(deref.chain, assignment.position)
@@ -1834,6 +1854,16 @@ _after:
return listOf(AstReplaceNode(assignment, memcopy, parent))
}
}
// array element -> named instance: tmp = points[i] --> memcopy(&points[i], &tmp, sizeof(struct))
val namedTarget = assignment.target.identifier
if(namedTarget!=null) {
val source = AddressOf(sourceIdx.plainarrayvar!!.copy(), sourceIdx.indexer.copy(), null, false, true, assignment.position)
val target = AddressOf(namedTarget.copy(), null, null, false, true, assignment.position)
val memcopy = FunctionCallStatement(IdentifierReference(listOf("sys", "memcopy"), assignment.position),
mutableListOf(source, target, structSizeNum),
false, assignment.position)
return listOf(AstReplaceNode(assignment, memcopy, parent))
}
}
}
@@ -37,9 +37,25 @@ internal class StatementReorderer(
if(decl.dirty && decl.value!=null)
errors.err("dirty variable can't have initialization value", decl.position)
if (decl.datatype.isNumericOrBool || decl.datatype.isPointer) {
if (decl.datatype.isNumericOrBool || decl.datatype.isPointer || decl.datatype.isStructInstance) {
if(decl !in declsProcessedWithInitAssignment) {
declsProcessedWithInitAssignment.add(decl)
if (decl.datatype.isStructInstance) {
// struct instances are never zero-initialized as numeric vars are;
// only split a runtime (non-static) initializer into a separate (memcopy) assignment
val v = decl.value
val isStaticInit = v == null || v is ArrayLiteral || v is StringLiteral || v is IFunctionCall
val isRuntimeInit = v != null && (v is ArrayIndexedExpression || v is IdentifierReference || v is BinaryExpression)
if (!isStaticInit && isRuntimeInit) {
val pos = decl.value!!.position
val identifier = IdentifierReference(listOf(decl.name), pos)
val assign = Assignment(AssignTarget(identifier, null, null, null, false, position = pos),
decl.value!!, AssignmentOrigin.VARINIT, pos)
decl.value = null
return listOf(AstInsert.after(decl, assign, parent as IStatementContainer))
}
return noModifications
}
if (decl.value == null || decl.value?.constValue(program)?.number==0.0) {
decl.value = null
if (decl.origin==VarDeclOrigin.USERCODE && decl.allowInitializeWithZero) {
+52
View File
@@ -179,6 +179,58 @@ main {
}
}
test("struct type declared inside a subroutine can be used for a direct instance") {
val src = """
%option no_sysinit
main {
uword @shared r1, r2
sub start() {
struct Point { uword x ubyte y }
Point p = [100, 20]
p.x = 300
p.y++
r1 = p.x
r2 = p.y as uword
}
}
"""
runVm(src) { vm, allocations ->
vm.memory.getUW(allocations.getValue("main.r1")) shouldBe 300u
vm.memory.getUW(allocations.getValue("main.r2")) shouldBe 21u
}
}
test("whole struct assignment by value including declaration initializer") {
val src = """
%option no_sysinit
main {
struct Point { uword x ubyte y }
Point[2] pts = [[10, 20], [30, 40]]
uword @shared r1, r2, r3, r4
sub start() {
Point tmp = pts[0]
pts[0] = pts[1]
pts[1] = tmp
tmp = pts[0]
pts[0].x = 999
r1 = pts[0].x
r2 = pts[0].y as uword
r3 = pts[1].x
r4 = pts[1].y as uword
}
}
"""
val result = compileText(VMTarget(), false, src, outputDir, writeAssembly = true)!!
val ir = result.compilationOptions.outputDir.resolve(result.compilerAst.name + ".p8ir").readText()
ir shouldContain "sys.memcopy"
runVm(src) { vm, allocations ->
vm.memory.getUW(allocations.getValue("main.r1")) shouldBe 999u
vm.memory.getUW(allocations.getValue("main.r2")) shouldBe 40u
vm.memory.getUW(allocations.getValue("main.r3")) shouldBe 10u
vm.memory.getUW(allocations.getValue("main.r4")) shouldBe 20u
}
}
test("m68k codegen accepts direct struct instance variables") {
val src = """
%option no_sysinit
+4 -18
View File
@@ -7,20 +7,6 @@ Summary of the issues found while adapting ``agabobs``, ``multiplexer`` and ``so
struct instances, and while analyzing the code generated for a single struct-instance variable on
every target. Details and further items live in the sections below.
Fixed (see git history for the exact changes):
- Corruption of float field reads with a runtime variable index on the classic 6502 codegen
(populations printed as 0 in ``sorting.p8``). Two asm peephole passes interacted unsafely:
``optimizeSameAssignments`` removed the ``lda/ldy`` scratch-pointer reloads that ``floats.MOVFM``
relies on, valid for the original register state, and then
``optimizeAddWordToSameVariableOrExtraRegisterLoadInWordStore`` rewrote the pointer add such that
the final ``A``/``Y`` no longer held the address. Fix: run the pointer-add rewrite before the
reload removal in ``AsmOptimizer.kt``. An earlier observed crash of the same codegen
(``AssemblyError: invalid register for lsb: AY`` on certain variable-index struct field reads)
no longer reproduces; ``&&arr[i]`` pointer temporaries remain a safe workaround style.
- new6502 codegen emitted ``jsr prog8_lib.multiply_bytes`` (undefined symbol) when scaling an array
index by a non-power-of-two element size; it now calls ``prog8_math.multiply_bytes`` like the
other sites (``InstrLoadStore.kt``).
Open, with suggested fixes (code quality, not correctness):
- Direct struct instances are not accessed optimally on any backend. The instance address is a
compile-time constant, yet the IR re-materializes it into a pointer register before every field
@@ -43,10 +29,11 @@ Inherent costs, documented rather than fixed:
bytes even with pointer temporaries; under ``-newcodegen`` the adapted examples are not larger
than the pointer-array originals (sorting: 5081 vs 5090; multiplexer is smaller optimized).
Related language gaps already listed below: ``swap()`` rejects struct elements, assigning a whole
struct instance by value is unsupported (``Country tmp = countries[i]``), and declaring a struct
Related language gaps already listed below: ``swap()`` rejects struct elements, and declaring a struct
instance from a struct type declared inside a subroutine crashes in
``SymbolTableMaker.resolveStructForVariable``.
``SymbolTableMaker.resolveStructForVariable``. Whole-struct-instance assignment by value is now
supported (desugared to ``sys.memcopy``), including declaration initializers such as
``Country tmp = countries[i]``.
Future Things and Ideas
@@ -115,7 +102,6 @@ Won't do's or deferred
- Deferred: support arrays-of-struct-instances as struct fields (e.g. ``struct Outer { Inner[4] inners }``). Currently rejected with an error.
- Deferred: support ``&array[i].field`` syntax to take the address of a struct field in an array. Currently only ``&array[i] + offsetof(Struct.field)`` works; ``&array[i].field`` is parsed as a field access on the resulting pointer and rejected by the checker.
- Deferred: support typed pointer arrays as struct fields, curretly requires untyped pointers arrays.
- Bug: declaring a direct struct instance of a struct type declared inside a subroutine can throw an ``InternalCompilerException`` in ``SymbolTableMaker.resolveStructForVariable``; its fallback lookup only searches top-level program blocks.
- Deferred: make a form of "manual generics" possible, see ``ideas/polymorphism.md``
- Deferred: when implementing unsigned longs: remove the unsigned-long constant "hack" that lets unsigned long constants be used as values for signed longs without needing a cast (the ``Antlr2KotlinVisitor.kt:508``/``:548`` "TODO hack" comments, ``AstExpressions.kt:145`` "TODO should be ULONG, but we don't have that type yet", and ``AsmAssignment.kt:129`` ``TODO("unsigned long $pos")``)
- Deferred: make the fixed zero-page scratch locations (``P8ZP_SCRATCH_B1``/``_REG``/``_W1``/``_W2``/``_PTR``) dynamic rather than hardcoded per target. Right now they live at fixed per-target addresses and are always reserved; a ``%zpreserved``/``%zpallowed`` range that excludes them only produces a warning (the codegen would still overwrite that memory). Relocating scratch to an available low-address byte (or making it configurable) when not in conflict would let ``%zpreserved``/``%zpallowed`` fully protect the desired areas. See ``MemoryRegions.kt`` (``Zeropage.checkScratchConflicts`` and the scratch-byte list it maintains) and the per-target ``*Zeropage`` classes.
+6 -3
View File
@@ -216,9 +216,7 @@ class SymbolTableMaker(private val program: PtProgram, private val options: Comp
return stCandidates.single()
val declsInSymbolTable = stCandidates.mapNotNull { it.astNode }.toSet()
val astCandidates = program.allBlocks().flatMap { it.children.filterIsInstance<PtStructDecl>() }
.filter { it !in declsInSymbolTable && matches(it.scopedName) }
.toList()
val astCandidates = collectStructDecls(program).filter { it !in declsInSymbolTable && matches(it.scopedName) }.toList()
val foundNames = (stCandidates.map { SymbolNames.stripPrefixes(it.scopedNameString) } +
astCandidates.map { SymbolNames.stripPrefixes(it.scopedName) }).distinct()
@@ -247,6 +245,11 @@ class SymbolTableMaker(private val program: PtProgram, private val options: Comp
return result
}
private fun collectStructDecls(node: PtNode): Sequence<PtStructDecl> = sequence {
if (node is PtStructDecl) yield(node)
node.children.forEach { yieldAll(collectStructDecls(it)) }
}
private fun handleStructAllocation(node: PtFunctionCall, structOverride: StStruct? = null): StStructInstance? {
require(node.builtin)
val struct = structOverride ?: (node.type.subType as? StStruct ?: return null)