From 1f31cb18e4108c718c3dfd12d902c5d3d02a2272 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 6 Nov 2025 00:39:31 +0100 Subject: [PATCH] IR: fix missing source lines in p8ir file --- .../prog8/codegen/intermediate/IRCodeGen.kt | 2 + .../astprocessing/BeforeAsmAstChanger.kt | 4 +- docs/source/todo.rst | 60 ------------------- examples/test.p8 | 27 ++------- .../src/prog8/intermediate/IRFileWriter.kt | 3 +- .../src/prog8/intermediate/IRProgram.kt | 4 +- 6 files changed, 12 insertions(+), 88 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index d9f318373..10e839fe7 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -157,6 +157,7 @@ class IRCodeGen( is IRCodeChunk -> { val replacement = IRCodeChunk(sub.label, first.next) replacement.instructions += first.instructions + replacement.appendSrcPositions(first.sourceLinesPositions) replacement } is IRInlineAsmChunk -> IRInlineAsmChunk(sub.label, first.assembly, first.isIR, first.next) @@ -410,6 +411,7 @@ class IRCodeGen( is IRCodeChunk -> { val newChunk = IRCodeChunk(label, first.next) newChunk.instructions += first.instructions + newChunk.appendSrcPositions(first.sourceLinesPositions) newChunk } is IRInlineAsmChunk -> { diff --git a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt index 97717bb0c..08ae3045f 100644 --- a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt +++ b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt @@ -56,7 +56,7 @@ internal class BeforeAsmAstChanger(val program: Program, private val options: Co if(subroutine.returntypes.isNotEmpty()) errors.err("subroutine is missing a return statement with value(s)", subroutine.position) else { - val returnStmt = Return(arrayOf(), subroutine.position) + val returnStmt = Return(arrayOf(), Position.DUMMY) mods += IAstModification.InsertLast(returnStmt, subroutine) } } else { @@ -68,7 +68,7 @@ internal class BeforeAsmAstChanger(val program: Program, private val options: Co // .... we cannot return this as an error, because that also breaks legitimate cases where the return is done from within a nested scope somewhere // errors.err("subroutine is missing a return statement with value(s)", subroutine.position) } else { - val returnStmt = Return(arrayOf(), subroutine.position) + val returnStmt = Return(arrayOf(), Position.DUMMY) mods += IAstModification.InsertLast(returnStmt, subroutine) } } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 2a6de111a..867ae0ef2 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -97,66 +97,6 @@ IR/VM - the split word arrays are currently also split in _lsb/_msb arrays in the IR, and operations take multiple (byte) instructions that may lead to verbose and slow operation and machine code generation down the line. maybe another representation is needed once actual codegeneration is done from the IR...? Should array operations be encoded in a more high level form in the IR? - ExpressionCodeResult: get rid of the separation between single result register and multiple result registers? maybe not, this requires hundreds of lines to change.. :( -- sometimes source lines end up missing in the output p8ir, for example the first assignment is gone in:: - - sub start() { - cx16.r0L = cx16.r1 as ubyte - cx16.r0sL = cx16.r1s as byte } - -More detailed example: not all source lines are correctly reported in the IR file, -for example the below subroutine only shows the sub() line:: - - sub two() { - cx16.r0 = peekw(ww + cx16.r0L * 2) - } - -and for example the below code omits line 5:: - -[examples/test.p8: line 4 col 6-8] sub start() { -[examples/test.p8: line 6 col 10-13] cx16.r2 = select2() -[examples/test.p8: line 7 col 10-13] cx16.r3 = select3() -[examples/test.p8: line 8 col 10-13] cx16.r4 = select4() -[examples/test.p8: line 9 col 10-13] cx16.r5 = select5() - - -.. code-block:: - - %option enable_floats - - main { - sub start() { - cx16.r1 = select1() - cx16.r2 = select2() - cx16.r3 = select3() - cx16.r4 = select4() - cx16.r5 = select5() - } - - sub select1() -> uword { - cx16.r0L++ - return 2000 - } - - sub select2() -> str { - cx16.r0L++ - return 2000 - } - - sub select3() -> ^^ubyte { - cx16.r0L++ - return 2000 - } - - sub select4() -> ^^bool { - cx16.r0L++ - return 2000 - } - - sub select5() -> ^^float { - cx16.r0L++ - return 2000 - } - } Libraries diff --git a/examples/test.p8 b/examples/test.p8 index 5976e13d2..bbebf1b2d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,26 +1,7 @@ - %import math - %import textio - main { + sub start() { + cx16.r0L = cx16.r1 as ubyte + cx16.r1sL = cx16.r2s as byte + } - sub start() { - - ubyte @shared st = 2 - - on st goto (lblA, lblB, lblC, lblD) - lblA: - txt.print("path a\n") - goto lblDone - lblB: - txt.print("path b\n") - goto 2 goto - lblC: - txt.print("path c\n") - goto lblDone - lblD: - txt.print("path d\n") - - lblDone: - txt.print("done\n") - } } diff --git a/intermediate/src/prog8/intermediate/IRFileWriter.kt b/intermediate/src/prog8/intermediate/IRFileWriter.kt index 978c8f6a0..f9c0b41a2 100644 --- a/intermediate/src/prog8/intermediate/IRFileWriter.kt +++ b/intermediate/src/prog8/intermediate/IRFileWriter.kt @@ -177,7 +177,8 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { xml.writeStartElement("P8SRC") val sourceTxt = StringBuilder("\n") code.sourceLinesPositions - .filter{ pos -> pos.line > 0 } + .asSequence() + .filter { it !== Position.DUMMY } .sortedBy { it.line } .forEach { pos -> val line = ImportFileSystem.retrieveSourceLine(pos) diff --git a/intermediate/src/prog8/intermediate/IRProgram.kt b/intermediate/src/prog8/intermediate/IRProgram.kt index 9c318f788..cf4e9a67c 100644 --- a/intermediate/src/prog8/intermediate/IRProgram.kt +++ b/intermediate/src/prog8/intermediate/IRProgram.kt @@ -572,12 +572,12 @@ class IRCodeChunk(label: String?, next: IRCodeChunkBase?): IRCodeChunkBase(label } fun appendSrcPosition(position: Position) { - if(sourceLinesPositions.lastOrNull()!=position) + if(!sourceLinesPositions.contains(position)) sourceLinesPositions.add(position) } fun appendSrcPositions(positions: Collection) { - positions.forEach { appendSrcPosition(it) } + positions.asSequence().filter { it!==Position.DUMMY }.forEach { appendSrcPosition(it) } } val sourceLinesPositions = mutableListOf()