From ba788bcf0fc47d1e94758d06642d33655270b982 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 2 Aug 2023 02:18:13 +0200 Subject: [PATCH] put the original p8 source lines into the generated assembly as comments (not only the line numbers). --- codeCore/src/prog8/code/core/SourceCode.kt | 32 +++++++++++++++++++ .../src/prog8/codegen/cpu6502/AsmGen.kt | 11 +++++-- docs/source/todo.rst | 4 ++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/codeCore/src/prog8/code/core/SourceCode.kt b/codeCore/src/prog8/code/core/SourceCode.kt index da9d21306..1c6802ba9 100644 --- a/codeCore/src/prog8/code/core/SourceCode.kt +++ b/codeCore/src/prog8/code/core/SourceCode.kt @@ -60,6 +60,8 @@ sealed class SourceCode { fun relative(path: Path): Path = curdir.relativize(path.toAbsolutePath()) fun isRegularFilesystemPath(pathString: String) = !(pathString.startsWith(LIBRARYFILEPREFIX) || pathString.startsWith(STRINGSOURCEPREFIX)) + + fun isLibraryResource(path: String) = path.startsWith(LIBRARYFILEPREFIX) } /** @@ -139,3 +141,33 @@ sealed class SourceCode { override val text: String = "" } } + + +object SourceLineCache { + private val cache = mutableMapOf>() + + private fun getCachedFile(file: String): List { + val existing = cache[file] + if(existing!=null) + return existing + if (SourceCode.isRegularFilesystemPath(file)) { + val source = SourceCode.File(Path(file)) + cache[file] = source.text.split('\n', '\r').map { it.trim() } + return cache.getValue(file) + } else if(file.startsWith(SourceCode.LIBRARYFILEPREFIX)) { + val source = SourceCode.Resource(file.drop(SourceCode.LIBRARYFILEPREFIX.length)) + cache[file] = source.text.split('\n', '\r').map { it.trim()} + return cache.getValue(file) + } + return emptyList() + } + + fun retrieveLine(position: Position): String? { + if (position.line>0) { + val lines = getCachedFile(position.file) + if(lines.isNotEmpty()) + return lines[position.line-1] + } + return null + } +} \ No newline at end of file diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 2c6062482..6f1d1784e 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -172,8 +172,6 @@ private fun PtFunctionCall.prefix(parent: PtNode): PtFunctionCall { call.children.addAll(children) call.children.forEach { it.parent = call } call.parent = parent - if(name.endsWith("concat_string")) - println("CONCAT ${this.position}") return call } @@ -241,7 +239,14 @@ class AsmGen6502Internal ( internal fun isTargetCpu(cpu: CpuType) = options.compTarget.machine.cpu == cpu internal fun outputSourceLine(node: PtNode) { - out(" ;\tsrc line: ${node.position.file}:${node.position.line}") + if(node.position===Position.DUMMY) + return + val srcComment = "\t; source: ${node.position.file}:${node.position.line}" + val line = SourceLineCache.retrieveLine(node.position) + if(line==null) + out(srcComment, false) + else + out("$srcComment $line", false) } internal fun out(str: String, splitlines: Boolean = true) { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 484f4060c..c11fbfaac 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,10 @@ TODO ==== -- add a mechanism to put the original p8 source lines into the generated assembly as comments (not only the line numbers). +- add command line option to enable/disable the inclusion of p8 source lines into the generated assembly / p8ir see outputSourceLine() - add a mechanism to pass the original p8 source lines into the p8ir file as comments. Remove the position xml tags. +- try to reduce the number of uses of temp variables for example in array[idx] -= amount / +- investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... / - investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... - IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction - IR: reduce amount of CMP/CMPI after instructions that set the status bits correctly (LOADs? INC? etc), but only after setting the status bits is verified!