IR: fix missing source lines in p8ir file

This commit is contained in:
Irmen de Jong
2025-11-06 00:39:31 +01:00
parent bc8ba252a5
commit 1f31cb18e4
6 changed files with 12 additions and 88 deletions

View File

@@ -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 -> {

View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -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")
}
}

View File

@@ -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)

View File

@@ -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<Position>) {
positions.forEach { appendSrcPosition(it) }
positions.asSequence().filter { it!==Position.DUMMY }.forEach { appendSrcPosition(it) }
}
val sourceLinesPositions = mutableListOf<Position>()